4

I'm trying to write a tactic that returns a value, and in the course of doing so it needs to check if something is an evar.

Unfortunately, I can't use is_evar because then the tactic isn't deemed to return a value (but rather another tactic). An example is below.

Any suggestions?

Ltac reify_wrt values ls :=
  match ls with
  | nil => constr:(@nil nat)
  | ?a :: ?ls' => let i := lookup a values in
                 let idx := reify_wrt values ls' in
                 constr:(i :: idx)
  | ?e :: ?ls' => is_evar e; 
                  let i := constr:(100) in 
                  let idx := reify_wrt values ls' in
                  constr:(i :: idx)
  end.
1
  • I am not sure it could be the best idea, but some advanced users are already trying LTAC2 which could help with this issue. LTAC2 is supported in Gitter / Github.
    – ejgallego
    Aug 30, 2017 at 10:29

2 Answers 2

5

There is a neat (or nasty) little trick that you can use to insert tactic execution into constr construction in Coq >= 8.5: wrap it in match goal.

Ltac reify_wrt values ls :=
  match ls with
  | nil => constr:(@nil nat)
  | ?a :: ?ls' => let i := lookup a values in
                 let idx := reify_wrt values ls' in
                 constr:(i :: idx)
  | ?e :: ?ls' => let check := match goal with _ => is_evar e end in
                  let i := constr:(100) in 
                  let idx := reify_wrt values ls' in
                  constr:(i :: idx)
  end.

Because programming language arcana fascinate me, I'll now tell you more than you probably ever wanted to know about the present and past execution models of Ltac.

There are two phases of tactic evaluation: there is tactic expression evaluation, and tactic running. During tactic running, sequencing, refine, rewrite, etc, are executed. During tactic expression evaluation, the following constructs are evaluated, if found in the head position of the tactic expressing:

  • tactic calls are resolved/followed/inlined/unfolded
  • let ... in ... binds the expression-evaluation of it's argument to the name, and does substitution
  • constr:(...) is evaluated and type checked
  • lazymaytch ... with ... end is evaluated (with back tracking), and returns the first matching branch which successfully expression-evaluates
  • match ... with ... end is evaluated (with backtracking) and the branch is eagerly executed. Note that in this picture, match is strange, because it forces the execution of a tactic to come early. If you've ever seen "immediate match producing tactics not allowed in local definitions" in Coq < 8.5, that's an error message explicitly forbidding the behavior I'm exploiting above. I'm guessing it's because this strange behavior of match is a wart that the original devs implementing Ltac wanted to hide. So the only place you can notice it in Coq 8.4 is if you stick match inside of lazymatch and play with failure levels, and notice that lazymatch will backtrack across failures of tactic execution in the inner match when you would normally expect it to fail.

In Coq 8.5, the tactic engine was rewritten to handle dependent subgoals. This induced subtle changes in the semantics of ; which can only be observed when using evars that are shared between multiple goals. In the rewrite, the devs changed the semantics of lazymatch to mean "match without backtracking", and lifted the restriction on "immediate match producing tactics." Hence you can do strange things like:

let dummy := match goal with _ => rewrite H end in
constr:(true)

and have constr-producing tactics with side effects. However, you can no longer do:

let tac := lazymatch b with
                | true => tac1
                | false => tac2
                end in
tac long args.

because in Coq >= 8.5, lazymatch also eagerly evaluates it's branches.

2

This is a well-known limitation of Ltac: you cannot write a tactic that sometimes returns a value, and sometimes returns another tactic. The solution is to rewrite your tactic in continuation-passing style. I can't give you a detailed explanation on how to do this, unfortunately, but Adam Chlipala's CDPT has a chapter on Ltac that describes the problem; simply look for "continuation" in the text.

5
  • Note that the code above should always return a value (as would the same code with a tryif is_evar a then b else c, which also fails) but it looks like CPDT already noted that "returns a value" is awfully narrowly defined for Ltac.
    – Rand00
    Aug 30, 2017 at 19:26
  • Anyhow, I've managed to write a similar function (which just removes evars) using CPDT's method. Ltac rm_evar ls cont := match ls with | nil => cont ls | ?a :: ?ls' => is_evar a; rm_evar ls' ltac:(fun l => cont l) | ?a :: ?ls' => rm_evar ls' ltac:(fun l => cont (a :: l)) end. Any thoughts on retrieving the value afterwards? (CPDT would call this with rm_evar ls1 ltac:(fun n => pose n) - I guess I could try matching on a hypothesis in the rest of the ltac, but it isn't ideal.)
    – Rand00
    Aug 30, 2017 at 19:32
  • 1
    If I understand what you are trying to do, you need to put whatever code that uses the value inside the top-level continuation. Something like rm_evar ls1 ltac:(fun n => do_something_with_n n). Aug 30, 2017 at 21:23
  • No, this answer is wrong. It is a well-known surface-level limitation in Coq 8.4 and earlier. Even in Coq 8.4, you can get around it with typeclasses as long as the tactic you need to invoke isn't higher-order, i.e., you don't need to pass another tactic to it. But in Coq 8.5 and later, there is no real trouble; see the answer I just posted at stackoverflow.com/a/46178884/377022 Sep 12, 2017 at 14:22
  • To be more precise, this answer is technically correct in Coq 8.5 and later (in part because every tactic that doesn't seem to return a value when run, actually returns the "value" unit), without answering the question (the asker wants a tactic that always returns a value). It is wrong in Coq 8.4 and earlier, as evidenced by the tactic lazymatch goal with |- True => constructor | |- ?G => constr:(G) end, which will solve the goal True if you run it, and will return the value of the goal otherwise (and not succeed in running). Sep 12, 2017 at 14:30

Your Answer

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.