2

TLDR: I want to be able to compare two terms -- one with a hole and the other without the hole -- and extract the actual lambda term that complete the term. Either in Coq or in OCaml or a Coq plugin or in anyway really.

For example, as a toy example say I have the theorem:

Theorem add_easy_0'':
forall n:nat,
  0 + n = n.
Proof.

The (lambda term) proof for this is:

fun n : nat => eq_refl : 0 + n = n

if you had a partial proof script say:

Theorem add_easy_0'':
forall n:nat,
  0 + n = n.
Proof.
    Show Proof.
    intros.
    Show Proof.

Inspected the proof you would get as your partial lambda proof as:

(fun n : nat => ?Goal)

but in fact you can close the proof and therefore implicitly complete the term with the ddt unification algorithm using apply:

Theorem add_easy_0'':
forall n:nat,
  0 + n = n.
Proof.
    Show Proof.
    intros.
    Show Proof.
    apply (fun n : nat => eq_refl : 0 + n = n).
    Show Proof.
Qed.

This closes the proof but goes not give you the solution for ?Goal -- though obviously Coq must have solved the CIC/ddt/Coq unification problem implicitly and closes the goals. I want to get the substitution solution from apply.

How does one do this from Coq's internals? Ideally while remaining in Coq but OCaml internals or coq plugin solutions or in fact any solution I am happy with.


Appendix 1: how did I realize apply must use some sort of "coq unification"

I knew that apply must be doing this because in the description of the apply tactic I know apply must be using unification due to it saying this:

This tactic applies to any goal. The argument term is a term well-formed in the local context. The tactic apply tries to match the current goal against the conclusion of the type of term. If it succeeds, then the tactic returns as many subgoals as the number of non-dependent premises of the type of term.

This is very very similar to what I once saw in a lecture for unification in Isabelle:

enter image description here

with some notes on what that means:

- You have/know rule [[A1; … ;An]] => A (*)
    - that says: that given A1; …; An facts then you can conclude A
    - or in backwards reasoning, if you want to conclude A, then you must give a proof of A1; …;An (or know Ai's are true)
- you want to close the proof of [[B1; …; Bm]] => C (**) (since thats your subgoal)
    - so you already have the assumptions B1; …; Bm lying around for you, but you wish to be able to conclude C
- Say you want to transform subgoal (**) using rule (*). Then this is what’s going on:
    - first you need to see if your subgoal (**) is a "special case" of your rule (*).  You commence by checking if the conclusion (targets) of the rules are "equivalent". If the conclusions match then instead of showing C you can now show A instead. But for you to have (or show) A, you now need to show A1; … ;An using the substitution that made C and A match. The reason you need to show A1;...;An is because if you show them you get A automatically according to rule (*) -- which by the "match" (unification) shows the original goal you were after. The main catch is that you need to do this by using the substitution that made A and C match. So:
    - first see if you can “match” A and C. The conclusions from both side must match. This matching is called unification and returns a substitution sig that makes the terms equal
        - sig = Unify(A,C) s.t. sig(A) = sig(C)
    - then because we transformed the subgoal (**) using rule (*), we must then proceed to prove the obligations from the rule (*) we used to match to conclusion of the subgoal (**). from the assumptions of the original subgoal in (**) (since those are still true) but using the substitution sig that makes the rules match.
- so the new subgoals if we match the current subgoal (*) with rule (**) is:
    - [[sig(B1); … ; sig(Bm) ]] => sigm(A1)
    - ...
    - [[sig(B1); … ; sig(Bm) ]] => sigm(An)
- Completing/closing the proof above (i.e. proving it) shows/proves:
    - [[sig(B1); …;sig(Bm) ]] => sig(C)
- Command: apply (rule <(*)>) where (*) stands for the rule names

Appendix2: why not exact?

Note that initially I thought exact was the Coq tactic I wanted to intercept but I was wrong I believe. My notes on exact:

- exact p. (assuming p has type U). 
    - closes a proof if the goal term T (i.e. a Type) matches the type of the given term p.
        - succeeds iff T and U are convertible (basically, intuitively if they unify https://coq.inria.fr/refman/language/core/conversion.html#conversion-rules since are saying if T is convertible to U)

conversion seems to be equality check not really unification i.e. it doesn't try to solve a system of symbolic equations.


Appendix 3: Recall unification

brief notes:

- unification https://en.wikipedia.org/wiki/Unification_(computer_science)
    - an algorithm that solves a system of equations between symbolic expressions/terms
        - i.e. you want 
            - cons2( cons1( x, y, ...,) ..., cons3(a, b, c), ... ) = cons1(x, nil) 
            - x = y
            - basically a bunch of term LHS term RHS and want to know if you can make them all equal given the terms/values and variables in them...
                - term1 = term2, term3 = term4 ? with some variables perhaps.
            - the solution is the substitution of the variables that satisfies all the equations

bounty

I'm genuinely curious about intercepting the apply tactic or call its unification algorithm.

4

1 Answer 1

1
+1000

apply indeed solve a unification, according to the document.

The tactic apply relies on first-order unification with dependent types unless the conclusion of the type of term is of the form P (t1 ... tn) with P to be instantiated.

Note that generally, the apply will turn one "hole" to several "hole"s that each cooresponds to a subgoal generated by it.


I have no idea how to access the internal progress of apply and get the substitution it uses.

However, You can call unify t u to do unification maully. you can refer to the official document. As far as I am concerned, the unicoq plugin provides another unification algorithm, and you can use munify t u to find unification between two items, see the Unicoq official repo.

An example of using unify and mutify:

From Unicoq Require Import Unicoq.

Theorem add_easy_0'':
forall n:nat,
  0 + n = n.
Proof.
    Show Proof.
    intros.
    Show Proof.
    refine ?[my_goal].
    Show my_goal.
    munify (fun t : nat => eq_refl : 0 + t = t) (fun n : nat => ?my_goal).
(*     unify (fun t : nat => eq_refl : 0 + t = t) (fun n : nat => ?my_goal). *)
Qed.

However, I wonder whether I have understand your question correctly.

Do you want to name the goal?

If you want to "extract the actual lambda term that complete the (parial) term". The so-called "lamda term" is the goal at that time, isn't it? If so, why to you want to "extract" it? It is just over there! Do you want to store the current subgoal and name it? If so, the abstract tactic perhaps helps, as mentioned in How to save the current goal / subgoal as an `assert` lemma

For example:

Theorem add_easy_0'':
forall n:nat,
  0 + n = n.
Proof.
    Show Proof.
    intros.
    Show Proof.
    abstract apply eq_refl using my_name.
    Check my_name.
    (*my_name : forall n : nat, 0 + n = n*)
    Show Proof.
    (*(fun n : nat => my_name n)*)
Qed.

Do you want to get the substituion?

Are you asking a substituion that make the goal term and the conclusion of the theorem applied match? For example:

Require Import Arith.
Lemma example4 : 3 <= 3.
Proof.
Show Proof.
Check le_n.
(* le_n : forall n : nat, n <= n *)
apply le_n.

Are you looking forward to get something like n=3? If you want to get such a "substitution", I am afraid the two tactics mentioned above will not help. Writing OCaml codes should be needed.

Do you want store the prove of current goal?

Or are you looking forward to store the proof of the current goal? Perhaps you can try assert, as mentioned in Using a proven subgoal in another subgoal in Coq.

13
  • might not have time to try it before bounty end but if it works I'm happy to start another one and simply give it to you. The easiest test would be to try a proof term that is complete and another one with a hole and see if it completes the the hole AND provides the substitution back of what ?GOAL might be. If they are multiple then all of them. Jul 18, 2022 at 14:39
  • Can you provide a full functioning (super trivial and minimal is fine) of how to use this? I've never used a plugin before so I'd need to know how to run it in coq (and even install it). Links to that would be amazing if you have time. Jul 18, 2022 at 14:40
  • ok here is were I'm documenting my current attempt at this: github.com/unicoq/unicoq/issues/69 , at least the install part. Jul 18, 2022 at 14:48
  • fyi the easiest way to test this is to see if it can unify these goals: (fun n : nat => ?Goal) and fun n : nat => eq_refl : 0 + n = n (provided in the question). Jul 18, 2022 at 14:51
  • running out of time, but will check it out later, thanks! The main thing I want to know is to compute the difference between a proof term with meta-variables/holes and the entire proof term. Whatever way I can do it is fine, hacky or not is fine. You current approach does seem quite reasonable though. But need to go through it a little more. Jul 19, 2022 at 22:10

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.