Hi, is it possible to splice the kername of an identifier in gallina code? I'd like to use something similar to <% %>
notation, but for resolving names.
Can you give a concrete an example of what you'd like to be able to do?
I've realised that I'm actually looking for a convenient way of pattern matching on certain references in an AST (let's say inductives). I'd imagine writing something like this:
fun t => match t with
| tApp <? list ?> [ty] => ...
| ...
end
where <? ?> would expand to a pattern (which I'd rather not write myself):
(tInd
{|
inductive_mind := (MPfile ["Datatypes"; "Init"; "Coq"], "list");
inductive_ind := 0
|} [])
Perhaps there is already a convenient way to do this? Right now, I'm doing something like:
Notation listPat := (tInd
{|
inductive_mind := (MPfile ["Datatypes"; "Init"; "Coq"], "list");
inductive_ind := 0
|} []).
And use listPat in pattern matching.
You can probably use this amazing notation by @Yannick Forster:
Notation "'$quote' x" :=
ltac:((let p y := exact y in quote_term x p))
(at level 0).
Then $quote list
is the term you want.
Interesting! I'm afraid it is not working for patterns though, for example:
Definition g : term -> bool :=
fun t => match t with
| $quote list => true
| _ => false
end.
Fails with Pattern "_" is redundant in this clause.
, it seems to be matching anything.
coq <8.18 confuses ltac:()
and _
when used in patterns (with ltac:()
only being possible through a notation)
after 8.18 it should instead say Quotations not supported in patterns.
Interesting, for me it says "quotations not supported in patterns". But it still doesn't work indeed. But you can still test for equality with term_eqb
.
Right, I'm on a coq<8.18 right now.
Me too, I'm on 8.16.1.
I've considered using term_eqb, but that limits the possibilities of doing pattern matching, e.g. in the original example, I also want to match on a list of length one (list
s only type parameter)
edit: this example:
fun t => match t with
| tApp <? list ?> [ty] => ...
| ...
end
actually the "Quotations not supported in patterns" is since 8.15
You need to use term_eqb
, no way around it
So tApp c [ty] => if c == $quote list...
with == being notation for a suitable call to term_eqb
I realise now, that anyway it's better for performance because pattern matching on strings is very inefficient right?
Yep, the pattern match will be way too inefficient
We could think about having quotation of patterns such that you can do match t with $quote_pattern cons x nil =>...
, but for my purposes term_eqb
has usually be sufficient
The question is just how much inspection people want to write with MetaCoq, and is pattern quotation predictable enough
Would the pattern match performance problem be solved by doing the plugin extraction approach?
Maybe, I don't think we ever tried :)
Last updated: Oct 13 2024 at 01:02 UTC