Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coq: a single notation for multiple constructors

Is it possible to define a single notation for multiple constructors in Coq? If the constructors differ by their argument types, they might be inferrable from them. A minimal (non-)working example:

Inductive A : Set := a | b | c: C -> A | d: D -> A
with C: Set := c1 | c2
with D: Set := d1 | d2.

Notation "' x" := (_ x) (at level 19).
Check 'c1. (*?6 c1 : ?8*)

In this case, constructor inference doesn't work. Maybe there's another way to specify a constructor as a variable?

like image 949
jaam Avatar asked Jun 30 '26 02:06

jaam


1 Answers

You can create a typeclass with the constructors as instances and let the instance resolution mechanism infer the constructor to call for you:

Class A_const (X:Type) : Type :=
  a_const : X -> A.
Instance A_const_c : A_const C := c.
Instance A_const_d : A_const D := d.

Check a_const c1.
Check a_const d2.

By the way, with Coq 8.5, if you really want a notation ' x to result in the exact constructor applied to x, rather than e.g. @a_const C A_const_c c1, then you can use ltac-terms to accomplish that:

Notation "' x" := ltac:(match constr:(a_const x) with
                        | @a_const _ ?f _ =>
                          let unfolded := (eval unfold f in f) in
                          exact (unfolded x)
                        end) (at level 0).
Check 'c1. (* c c1 : A *)
Check 'd2. (* d d2 : A *)
like image 71
Daniel Schepler Avatar answered Jul 02 '26 03:07

Daniel Schepler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!