Coq-Equations icon indicating copy to clipboard operation
Coq-Equations copied to clipboard

Make equalities available in with clause ("inspect" pattern)

Open yforster opened this issue 5 years ago • 2 comments

The Program mode automatically makes the equations behind a match accessible in proofs:

Program Definition test (n : nat) : {n = 0} + {n <> 0} :=
  match Nat.eqb n 0 with
    | true => left _ 
    | false => right _
  end.
Next Obligation.
  symmetry in Heq_anonymous. now eapply EqNat.beq_nat_true in Heq_anonymous.
Qed.
Next Obligation.
  symmetry in Heq_anonymous. now eapply EqNat.beq_nat_false in Heq_anonymous.
Qed.

To replay this in Equations, one has to use a trick which is apparently known as the "inspect pattern":

Notation "( a ; b )" := (exist _ a b).

Definition inspect {Y} (x : Y) : {y | x = y}.
Proof. now exists x. Qed.

Equations test (n : nat) : {n = 0} + {n <> 0} :=
  test n with inspect (Nat.eqb n 0) => {
    test n (true;H) => left _ ;
    test n (false;H) => right _
  }.
Next Obligation.
  now eapply EqNat.beq_nat_true in H.
Qed.
Next Obligation.
  now eapply EqNat.beq_nat_false in H.
Qed.

It would be nice if Equations would generate this equations automatically as well, for instance by introducing a witheq syntax, or even something more explicit where a user can choose the name of the equation.

yforster avatar Jul 18 '19 16:07 yforster

I agree. I added an example in example/Basics.v to illustrate the inspect pattern, but programming with it is error-prone.

ybertot avatar Jul 02 '20 13:07 ybertot

Here is a proposition for syntax:

Equations test (n : nat) : {n = 0} + {n <> 0} :=
  test n with_inspect (Nat.eqb n 0) as eq_name => {
    test n true => left _;
    test n false => right _
  }.
Next Obligation.
  now eapply EqNat.beq_nat_true in eq_name.

and so on. This forces blocking with_inspect as a key word and otherwise it fixes the name of the equation once and for all in the (as ...) clause.

ybertot avatar Jul 03 '20 08:07 ybertot