Is "printf-debugging" possible in Ltac?

488 views Asked by At

Is there a way to print the value of a variable (whether a hypothesis, tactic, term) in the middle of an Ltac procedure?

1

There are 1 answers

1
Tej Chajed On

Yes, use the idtac tactic.

You can pass idtac a constant string to print. It can also print identifiers (like hypothesis names) if you pattern match them, and it can print their types if you get access to them with pattern matching or type of. You can also print terms or the contents of Ltac let-bound variables. Finally, you can pass idtac multiple arguments to print them all out. You mentioned printing tactics - this is one thing that unfortunately you can't print with idtac. If you try to, you'll just get <tactic closure>.

Here are a bunch of examples:

Goal True -> False. intro Htrue. idtac "hello world". (* prints hello world *) match goal with | [ H: True |- _ ] => idtac H (* prints Htrue *) end. match goal with | |- ?g => idtac g (* prints False *) end. let t := type of Htrue in idtac t. (* prints True *) let x := constr:(1 + 1) in idtac x. (* prints (1 + 1) *) idtac "hello" "there". (* prints hello there *) (* note that this is an Ltac-level function, not a Gallina function *) let x := (fun _ => fail) in idtac x. (* prints <tactic closure> *) Abort.