coq-club AT inria.fr
Subject: The Coq mailing list
List archive
- From: Jason Gross <jasongross9 AT gmail.com>
- To: coq-club <coq-club AT inria.fr>
- Cc: Eddy Westbrook <westbrook AT galois.com>
- Subject: Re: [Coq-Club] Finding a pattern-matching fix in a term?
- Date: Mon, 1 Jun 2020 17:47:12 -0400
- Authentication-results: mail2-smtp-roc.national.inria.fr; spf=None smtp.pra=jasongross9 AT gmail.com; spf=Pass smtp.mailfrom=jasongross9 AT gmail.com; spf=None smtp.helo=postmaster AT mail-ej1-f46.google.com
- Ironport-phdr: 9a23:2+K9DxDhJYSRWE1g2KPyUyQJP3N1i/DPJgcQr6AfoPdwSPT4r8bcNUDSrc9gkEXOFd2Cra4d1qyP6P6ocFdDyK7JiGoFfp1IWk1NouQttCtkPvS4D1bmJuXhdS0wEZcKflZk+3amLRodQ56mNBXdrXKo8DEdBAj0OxZrKeTpAI7SiNm82/yv95HJbAhEmTqwbalvIBi4rAjaq9Ubj5ZlJqst0BXCv2FGe/5RxWNmJFKTmwjz68Kt95N98Cpepuws+ddYXar1Y6o3Q7pYDC87M28u/83kqQPDTQqU6XQCVGgdjwdFDBLE7BH+WZfxrzf6u+9g0ySUIcH6UbY5Uim54qx1VBHnljsINz8h8GHWlMNwir5boAm8rBB72oLYfZ2ZOOZ7cq7bYNgUR3dOXtxJWiNODIOzbYsBAeQCM+hFsYfyu0ADogGiCQS2Hu7j1iNEi33w0KYn0+ohCwbG3Ak4EtwKqnvUt9L1NKEPWu601qnIyi7Db+hL0jr66ojHaAshofaKXbJ/a8rRzlIvFxjBg1iLpozlOima1uUJs2SB8+VgUuevhnchpgpsrTeh2t0ihZPVhoIJ1F/E7yN5zZ43KNC6VEN2YNGpHZlRuiyGK4Z6X8MvTmVstSs51LAItpy2ciYXxZkkwxPTdfyJfpaI7B/jSeqcLyp0iXN5dL+wgRu57EuuyvXkW8WqzFpHqjBJn9rMu3wXyRDf98uKRuFy80qg3zuEyhrd5fteIU8ukKrWM54hzaA0lpoUqUnDGzX5mETyjKOPaEok+PWk5/3pYrjpp5KQLYB0igb5MqQhnsywH/40PRQJX2ie4ei81bvj8lPlQLhSkPE6jq3UvIrZKMkbvKK1HRJZ34U55xuwDjqqyNEYkmMGLFJBdhKHlY/pO1TWLfD6F/i/mFOskDFkx/DcOb3hGY7NI3zYnbfueLZy8U9cyA4pwd9D4JJUD6kNIOjvVU/pqNzYEhg5PhSozObgEdVxz58RWWaSAqCCK67Sql+J5uc3I+aWfoMVuTD9K+Ik5/H0l3M5l0UdLuGV2s4cb2n9FfB7KQ3Nan31x9wFDG0ivwwkTeWshkfUAhBJYHPnfasn4TdzJ5ihFpyLEoKkm7uH0z28BYYHTm9DA1GIV3zvctPXCL83dCuOL5o5wXQ/Xr+7RtpkjEn27VOo+/9cNuPRvxYgm9f7ztEsvr/ckBgz8Xp/CMHPizjQHVExpXsBQnoN5I46oUF5zQ3eg61xgvgdGN4Kov0VDEE1MpnTy+E8ANf3CFqYL4W5DW2+S9DjOgkfC9c4wtsAeUF4QozwgRXK3i7sCLgQxeWG
You can also write something like:
Ltac is_fix_at_head x :=
lazymatch x with
| ?f _ => is_fix_at_head f
| _ => is_fix x
end.
Goal forall x y, x + y = y + x.
unfold "+".
intros.
match goal with
| [ |- context[?f] ] => is_fix_at_head f; idtac f
end.
lazymatch x with
| ?f _ => is_fix_at_head f
| _ => is_fix x
end.
Goal forall x y, x + y = y + x.
unfold "+".
intros.
match goal with
| [ |- context[?f] ] => is_fix_at_head f; idtac f
end.
and then your tactic looks like:
Ltac head x :=
lazymatch x with
| ?f _ => head f
| _ => x
end.
Ltac which_arg_struct f :=
lazymatch f with
(* python -c 'print("\n".join(" | fix F %s {struct %s} := _ => constr:(%d%%nat)" % (" ".join("x%d" % i for i in range(n)), "x%d" % m, m) for n in range(4) for m in range(n)))' *)
| fix F x0 {struct x0} := _ => constr:(0%nat)
| fix F x0 x1 {struct x0} := _ => constr:(0%nat)
| fix F x0 x1 {struct x1} := _ => constr:(1%nat)
| fix F x0 x1 x2 {struct x0} := _ => constr:(0%nat)
| fix F x0 x1 x2 {struct x1} := _ => constr:(1%nat)
| fix F x0 x1 x2 {struct x2} := _ => constr:(2%nat)
| ?f => fail 0 "We only support up to 3-argument fix, not" f
end.
Ltac on_nth_arg_cps f n tac K :=
lazymatch f with
| ?f ?x => on_nth_arg_cps
f n tac
ltac:(fun n => lazymatch n with
| 0 => tac x
| S ?n => K n
end)
| _ => K n
end.
Ltac nth_arg f n :=
on_nth_arg_cps f n ltac:(fun x => x) ltac:(fun n => lazymatch goal with _ => fail 0 "Not enough arguments; needed" n "extra arguments for" f end).
Ltac on_struct_arg fx tac :=
let f := head fx in
is_fix f;
let n := which_arg_struct f in
let arg := nth_arg fx n in
tac arg.
Ltac destruct_struct_arg x := on_struct_arg x ltac:(fun x => destruct x).
Ltac induction_struct_arg x := on_struct_arg x ltac:(fun x => induction x).
Goal forall x y, x + y = y + x.
unfold "+".
intros.
match goal with
lazymatch x with
| ?f _ => head f
| _ => x
end.
Ltac which_arg_struct f :=
lazymatch f with
(* python -c 'print("\n".join(" | fix F %s {struct %s} := _ => constr:(%d%%nat)" % (" ".join("x%d" % i for i in range(n)), "x%d" % m, m) for n in range(4) for m in range(n)))' *)
| fix F x0 {struct x0} := _ => constr:(0%nat)
| fix F x0 x1 {struct x0} := _ => constr:(0%nat)
| fix F x0 x1 {struct x1} := _ => constr:(1%nat)
| fix F x0 x1 x2 {struct x0} := _ => constr:(0%nat)
| fix F x0 x1 x2 {struct x1} := _ => constr:(1%nat)
| fix F x0 x1 x2 {struct x2} := _ => constr:(2%nat)
| ?f => fail 0 "We only support up to 3-argument fix, not" f
end.
Ltac on_nth_arg_cps f n tac K :=
lazymatch f with
| ?f ?x => on_nth_arg_cps
f n tac
ltac:(fun n => lazymatch n with
| 0 => tac x
| S ?n => K n
end)
| _ => K n
end.
Ltac nth_arg f n :=
on_nth_arg_cps f n ltac:(fun x => x) ltac:(fun n => lazymatch goal with _ => fail 0 "Not enough arguments; needed" n "extra arguments for" f end).
Ltac on_struct_arg fx tac :=
let f := head fx in
is_fix f;
let n := which_arg_struct f in
let arg := nth_arg fx n in
tac arg.
Ltac destruct_struct_arg x := on_struct_arg x ltac:(fun x => destruct x).
Ltac induction_struct_arg x := on_struct_arg x ltac:(fun x => induction x).
Goal forall x y, x + y = y + x.
unfold "+".
intros.
match goal with
| [ |- context[?f] ] => induction_struct_arg f
end.
end.
On Mon, Jun 1, 2020 at 5:24 PM Jason Gross <jasongross9 AT gmail.com> wrote:
Note that the following works in Coq 8.11:Goal forall x y, x + y = y + x.
unfold "+".
intros.
lazymatch goal with
| [ |- context[fix f (a : ?A) (b : ?B) {struct a} : ?C := _] ] => idtac
end.
lazymatch goal with
| [ |- context[fix f a b {struct a} := _] ] => idtac
end.
lazymatch goal with
| [ |- context[fix f a b {struct a} := match a with _ => _ end] ] => idtac
end.
lazymatch goal with
| [ |- context[fix f a b {struct a} := match a with O => @?O_case a b | S n => _ end] ] => idtac
end.
lazymatch goal with
| [ |- context[fix f a b {struct a} := match a with O => @?O_case f a b | S n => @?S_case f a b n end] ] => idtac
end.Note that the struct annotation is mandatory, and so you need separate cases for different numbers of arguments and different recursive arguments.On Mon, Jun 1, 2020 at 3:44 PM Gregory Malecha <gregory AT bedrocksystems.com> wrote:It is late, but you can do this in template-coq simply by quoting the term and writing a function to check what you want. It wouldn't be truly gorgeous, but it doesn't require anything that is unsafe. Note that what template-coq gives you is a Gallina inductive type that reifies a Gallina term, so inspecting a term is *literally* just Coq pattern matching. That said, this is definitely not unification, so if you want "definitionally equal to" rather than "syntactically equal to you are currently out of luck with template-coq.On Mon, May 18, 2020 at 5:40 AM Pierre-Marie Pédrot <pierre-marie.pedrot AT inria.fr> wrote:Hi,
On 16/05/2020 03:17, Eddy Westbrook wrote:
> Is there any way to pattern-match on a fix + match term like (fix foo
> (x1:A1) … := match xi with … ) in a term? I am trying to write some
> tactics to determine if a particular argument is being destructed in
> a term, in order to apply the destruct or induction tactic on it, but
> it seems that Ltac at least won’t let me search for fix terms…?
if you're using Coq >= 8.11, it is possible to do this in Ltac2, albeit
in a way deemed "unsafe". Once done, you can also wrap the resulting
Ltac2 tactic into a proper Ltac1 tactic, so that it is transparent for
the end-user. Look at the Ltac2.Constr.Unsafe module for more details.
PMP
--Gregory Malecha
Mail: gregory AT bedrocksystems.com
BedRock
Systems Inc
FORMALLY SECURED COMPUTING
UNBREAKABLE FOUNDATION FOR
- Re: [Coq-Club] Finding a pattern-matching fix in a term?, Gregory Malecha, 06/01/2020
- Re: [Coq-Club] Finding a pattern-matching fix in a term?, Jason Gross, 06/01/2020
- Re: [Coq-Club] Finding a pattern-matching fix in a term?, Jason Gross, 06/01/2020
- Re: [Coq-Club] Finding a pattern-matching fix in a term?, Eddy Westbrook, 06/03/2020
- Re: [Coq-Club] Finding a pattern-matching fix in a term?, Jason Gross, 06/03/2020
- Re: [Coq-Club] Finding a pattern-matching fix in a term?, Jason Gross, 06/03/2020
- Re: [Coq-Club] Finding a pattern-matching fix in a term?, Jason Gross, 06/03/2020
- Re: [Coq-Club] Finding a pattern-matching fix in a term?, Eddy Westbrook, 06/03/2020
- Re: [Coq-Club] Finding a pattern-matching fix in a term?, Jason Gross, 06/01/2020
- Re: [Coq-Club] Finding a pattern-matching fix in a term?, Jason Gross, 06/01/2020
Archive powered by MHonArc 2.6.19+.