Debian Codegraph @

examples/example_import_projections.v

raw
1 From elpi Require Import elpi.
2
3 (* "import" a record instance by naming it's applied projections *)
4
5 Elpi Command import.projections.
6 Elpi Accumulate lp:{{
7 main [str S] :-
8 coq.locate S GR,
9 coq.env.typeof GR Ty,
10 main-import-projections (global GR) Ty.
11 main [trm TSkel] :-
12 % input terms are not elaborated yet
13 std.assert-ok! (coq.elaborate-skeleton TSkel Ty T) "input term illtyped",
14 main-import-projections T Ty.
15
16 pred main-import-projections i:term, i:term.
17 main-import-projections T Ty :-
18 std.assert! (coq.safe-dest-app Ty (global (indt I)) Args) "not an inductive term",
19 std.assert! (coq.env.record? I PrimProjs) "not a record",
20 coq.env.indt I _ _ NParams _ _ _,
21 std.assert! (std.length Args NParams) "the record is not fully appplied",
22 coq.env.projections I Ps, % get the projections generated by Coq
23 if (PrimProjs = tt)
24 (std.forall Ps (declare-abbrev {std.append {coq.mk-n-holes NParams} [T]}))
25 (std.forall Ps (declare-abbrev {std.append Args [T]})).
26
27 pred declare-abbrev i:list term, i:option constant.
28 declare-abbrev _ none.
29 declare-abbrev Args (some Proj) :-
30 coq.gref->id (const Proj) ID, % get the short name of the projection
31 OnlyParsing = tt,
32 coq.mk-app (global (const Proj)) Args T, % handles the case Args = []
33 @local! ==> coq.notation.add-abbreviation ID 0 T OnlyParsing _.
34 }}.
35
36 Elpi Export import.projections. (* make the command available *)
37
38 (**************************** usage examples *********************************)
39
40 Record r T (t : T) := Build {
41 p1 : nat;
42 p2 : t = t;
43 _ : p2 = refl_equal;
44 }.
45
46 Section test.
47 Variable x : r nat 3.
48 import.projections x.
49 Print p2. (* Notation p2 := (readme.p2 nat 3 x) *)
50 Check p1 : nat. (* check p1 is already applied to x *)
51 End test.
52
53 Fail Check p1 : nat. (* the abbreviation is gone *)
54 Check p1 : forall (T : Type) (t : T), r T t -> nat. (* p1 points again to the original projection *)
55
56 Module test.
57 import.projections (Build bool false 3 (refl_equal _) (refl_equal _)).
58 Check refl_equal _ : p1 = 3. (* check the value of p1 is 3 *)
59 End test.
60
61 Set Primitive Projections.
62 Unset Auto Template Polymorphism.
63 Record r1 (A : Type) : Type := {
64 f1 : A;
65 f2 : nat;
66 }.
67
68 Section test2.
69 Variable x : r1 bool.
70 import.projections x.
71 Unset Printing Primitive Projection Parameters.
72 Check f1. (* there is an _, hence it is the primitive projection *)
73 End test2.