Skip to Content.
Sympa Menu

coq-club - Re: [Coq-Club] Finding a pattern-matching fix in a term?

coq-club AT inria.fr

Subject: The Coq mailing list

List archive

Re: [Coq-Club] Finding a pattern-matching fix in a term?


Chronological Thread 
  • From: Jason Gross <jasongross9 AT gmail.com>
  • To: Eddy Westbrook <westbrook AT galois.com>
  • Cc: coq-club <coq-club AT inria.fr>
  • Subject: Re: [Coq-Club] Finding a pattern-matching fix in a term?
  • Date: Tue, 2 Jun 2020 21:04:08 -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-f54.google.com
  • Ironport-phdr: 9a23:KWAtuRTDJNEt2sy6KMPMt1xuttpsv+yvbD5Q0YIujvd0So/mwa6yYRKN2/xhgRfzUJnB7Loc0qyK6v2mADdQqszd+Fk5M7V0HycfjssXmwFySOWkMmbcaMDQUiohAc5ZX0Vk9XzoeWJcGcL5ekGA6ibqtW1aFRrwLxd6KfroEYDOkcu3y/qy+5rOaAlUmTaxe7x/IAi2oAnLq8Ubj4hvJqkvxhbGv3BFZ/lYyWR0KFyJgh3y/N2w/Jlt8yRRv/Iu6ctNWrjkcqo7ULJVEi0oP3g668P3uxbDSxCP5mYHXWUNjhVIGQnF4wrkUZr3ryD3q/By2CiePc3xULA0RTGv5LplRRP0lCsKMSMy/XrJgcJskq1UvBOhpwR+w4HKZoGVKOF+db7Zcd8DWGZNQtpdWylHD4ihbYUAEvABMP5XoInzpVQArRWwCwqxCu3x1jBFnWX50bEg3uk7DQ3KwA4tEtQTu3rUttX1M6ISXPixwqbS1jXDaPVW0ir85ojSdRAhuuqMVq93fMrT00YvDATFjlOfqYz/ODOVzf8NvmyA4upvUOKgkW8nqwVrrjezwccsj5DEi4QIwV/L6St32pw6JcGkSEFle96kFoNduiOYOoZqQ84vTGNltScmx7AapJK2fCwHxZAkyhPBZPKKcYaG7w/gWeuVPzt2hGxodrK/ihqv/0Ws1+zxWtW73VtMsyFLkcHMu2gT2xDP7sWLUPhw80e71TqRygze6ftILEA7mKfdNpUv2KQ/loAJvkTGBiL2mFv5jKuRdkg8/+in8eXnYrH/qpCCNY94lxjyMqoul8GwG+g4PQ8OX2+U+eS4yrLv51H2QLJPjvEuk6nZto7VJdgDq6KnHwNY1pwv5hW/Aju8ztgUgHoKIEhFdR+Il4TpPkvBIPH8DfexmVSslzJryujEPrL7GZXNKX7DkLj7fbZ68E5c0w4+wMtQ55JREL4BIfbzVlXtu9zfCx81Kxa0zPr/CNVhyoMeXnqCDbOeMKPLqFOH+uYvI/SXa4IOozb8K/0l5+b0gnMjmF8de7Op3ZoNZ3yiEPRmORbRXX25r9sNAC81sxEiRvbtj17KBSFefGq4RaU75Rk0AYahDJyFTYeo1u+vxiC+S79ffWdAQn+WFmzzP9GGUuwLbi2IJdR6wxQLULGgT8kq0hT451yy8KZuMueBon5QjpnkztUgorSLzUhjpwwxNNyU1iS2d08xm2oJQzEs26Um+B5yz16C1e5zhPkKTIUPtcMMaR8zMNvn98I/E8r7A1uTcdKASVLgSdKjU2loE4ABhuQWakM4IO2MyxDO2y3wXu0Qnr2PQZE2q+fSgiS3KMF6xHLLkqImigt+Tw==

If the question you want to ask is "is there any fixpoint which, after reduction via the structural argument, then matches on my variable?", I'd suggest you take a different approach: rather than asking "is this variable matched on?" you can instead ask "please find all matched on variables and case/induct on them".  You'll have to jump through a couple of hoops to avoid infinite loops, but I think this approach is much more feasible than trying to determine, at introduction time, if later computation will lead to this variable being matched on.

On Tue, Jun 2, 2020, 21:01 Jason Gross <jasongross9 AT gmail.com> wrote:
You can search the goal for
context [match x with _ => _ end]
This will tell you if it's the discriminee of a match.  Note that fixpoints won't reduce unless you destruct their structural argument, so it actually doesn't matter whether or not the fixpoint body matches on the argument you care about, it only matters whether the argument occurs in the structural location.

On Tue, Jun 2, 2020, 20:39 Eddy Westbrook <westbrook AT galois.com> wrote:
Jason,

Interesting. The context[?f] pattern combined with is_fix_at_head f is going to apply is_fix_at_head to every subterm of the goal, right? I guess I could also write a tactic that manually crawls the subterms to see if any of them is a fix term applied to the argument I care about.

Is there any way to tell if the fix is destructing a specific argument? What I’m actually try to determine in my tactic (apologies if it wasn’t clear earlier) is whether, in the current goal foorall x, P x, the variable x is destructed by pattern-matching or a fix (containing a pattern-match) somewhere in P. Then, when I intro x, I can also destruct x. Does that make sense?

Thanks to everyone for the help so far, btw.
-Eddy

On Jun 1, 2020, at 2:47 PM, Jason Gross <jasongross9 AT gmail.com> wrote:

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.

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
  | [ |- context[?f] ] => induction_struct_arg f
  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
UNBREAKABLE FOUNDATION FOR

FORMALLY SECURED COMPUTING




Archive powered by MHonArc 2.6.19+.

Top of Page