Debian Codegraph @

apps/derive/elpi/lens_laws.elpi

raw
1 /* Equations on lenses */
2 /* license: GNU Lesser General Public License Version 2.1 or later */
3 /* ------------------------------------------------------------------------- */
4
5 namespace derive.lens-laws {
6
7 func declare-law1 string, prop.
8 declare-law1 Prefix (lens-db I FC C) :- std.do! [
9 coq.env.indt-decl I Decl,
10 law1 Decl (global (const C)) (global (indt I)) Bo,
11 coq.gref->id (const FC) F,
12 Name is Prefix ^ F ^ "_view_set",
13 coq.env.add-const Name Bo _ @opaque! _,
14 ].
15 func law1 indt-decl, term, term -> term.
16 law1 (parameter ID _ Ty Rest) Lens Ind {{ fun p : lp:Ty => lp:(Bo p) }} :- !,
17 @pi-parameter ID Ty p\ law1 (Rest p) {coq.mk-app Lens [p]} {coq.mk-app Ind [p]} (Bo p).
18 law1 _ Lens IT Bo :-
19 BoS = {{ fun r => lp:{{ {coq.build-match {{r}} IT (law1-rty.aux Lens) law1-bo.aux } }} }},
20 TyS = {{ lib:elpi.derive.lens.view_set lp:Lens }},
21 std.assert-ok! (coq.elaborate-ty-skeleton TyS _ Ty) "derive.lens_laws: law1 statement illtyped",
22 std.assert-ok! (coq.elaborate-skeleton BoS Ty Bo) "derive.lens_laws: law1 proof illtyped".
23 func law1-rty.aux term, term, list term, list term -> term.
24 law1-rty.aux L _ Vs _ {{ lib:elpi.derive.lens.view_set_on lp:L lp:R }} :- std.last Vs R.
25 func law1-bo.aux term, term, list term, list term -> term.
26 law1-bo.aux _ _ _ _ {{ fun x => lib:@elpi.erefl _ _ }}.
27
28 func declare-law2 string, prop.
29 declare-law2 Prefix (lens-db I FC C) :- std.do! [
30 coq.env.indt-decl I Decl,
31 law2 Decl (global (const C)) (global (indt I)) Bo,
32 coq.gref->id (const FC) F,
33 Name is Prefix ^ F ^ "_set_set",
34 coq.env.add-const Name Bo _ @opaque! _,
35 ].
36 func law2 indt-decl, term, term -> term.
37 law2 (parameter ID _ Ty Rest) Lens Ind {{ fun p : lp:Ty => lp:(Bo p) }} :- !,
38 @pi-parameter ID Ty p\ law2 (Rest p) {coq.mk-app Lens [p]} {coq.mk-app Ind [p]} (Bo p).
39 law2 _ Lens IT Bo :-
40 BoS = {{ fun r => lp:{{ {coq.build-match {{r}} IT (law2-rty.aux Lens) law2-bo.aux } }} }},
41 TyS = {{ lib:elpi.derive.lens.set_set lp:Lens }},
42 std.assert-ok! (coq.elaborate-ty-skeleton TyS _ Ty) "derive.lens_laws: law2 statement illtyped",
43 std.assert-ok! (coq.elaborate-skeleton BoS Ty Bo) "derive.lens_laws: law2 proof illtyped".
44 func law2-rty.aux term, term, list term, list term -> term.
45 law2-rty.aux L _ Vs _ {{ lib:elpi.derive.lens.set_set_on lp:L lp:R }} :- std.last Vs R.
46 func law2-bo.aux term, term, list term, list term -> term.
47 law2-bo.aux _ _ _ _ {{ fun x y => lib:@elpi.erefl _ _ }}.
48
49 func declare-law3 string, prop.
50 declare-law3 Prefix (lens-db I FC C) :- std.do! [
51 coq.env.indt-decl I Decl,
52 law3 Decl (global (const C)) (global (indt I)) Bo,
53 coq.gref->id (const FC) F,
54 Name is Prefix ^ F ^ "_set_view",
55 coq.env.add-const Name Bo _ @opaque! _,
56 ].
57 func law3 indt-decl, term, term -> term.
58 law3 (parameter ID _ Ty Rest) Lens Ind {{ fun p : lp:Ty => lp:(Bo p) }} :- !,
59 @pi-parameter ID Ty p\ law3 (Rest p) {coq.mk-app Lens [p]} {coq.mk-app Ind [p]} (Bo p).
60 law3 _ Lens IT Bo :-
61 BoS = {{ fun r => lp:{{ {coq.build-match {{r}} IT (law3-rty.aux Lens) law3-bo.aux } }} }},
62 TyS = {{ lib:elpi.derive.lens.set_view lp:Lens }},
63 std.assert-ok! (coq.elaborate-ty-skeleton TyS _ Ty) "derive.lens_laws: law3 statement illtyped",
64 std.assert-ok! (coq.elaborate-skeleton BoS Ty Bo) "derive.lens_laws: law3 proof illtyped".
65 func law3-rty.aux term, term, list term, list term -> term.
66 law3-rty.aux L _ Vs _ {{ lib:elpi.derive.lens.set_view_on lp:L lp:R }} :- std.last Vs R.
67 func law3-bo.aux term, term, list term, list term -> term.
68 law3-bo.aux _ _ _ _ {{ lib:@elpi.erefl _ _ }}.
69
70 func declare-law4 string, prop, prop.
71 declare-law4 _ (lens-db I F C) (lens-db I F C) :- !.
72 declare-law4 Prefix (lens-db I FC1 C1) (lens-db I FC2 C2) :- std.do! [
73 coq.env.indt-decl I Decl,
74 law4 Decl (global (const C1)) (global (const C2)) (global (indt I)) Bo,
75 coq.gref->id (const FC1) F1,
76 coq.gref->id (const FC2) F2,
77 Name is Prefix ^ F1 ^ "_" ^ F2 ^ "_exchange",
78 coq.env.add-const Name Bo _ @opaque! _,
79 ].
80 func law4 indt-decl, term, term, term -> term.
81 law4 (parameter ID _ Ty Rest) Lens1 Lens2 Ind {{ fun p : lp:Ty => lp:(Bo p) }} :- !,
82 @pi-parameter ID Ty p\ law4 (Rest p) {coq.mk-app Lens1 [p]} {coq.mk-app Lens2 [p]} {coq.mk-app Ind [p]} (Bo p).
83 law4 _ Lens1 Lens2 IT Bo :-
84 BoS = {{ fun r => lp:{{ {coq.build-match {{r}} IT (law4-rty.aux Lens1 Lens2) law4-bo.aux } }} }},
85 TyS = {{ lib:elpi.derive.lens.exchange lp:Lens1 lp:Lens2 }},
86 std.assert-ok! (coq.elaborate-ty-skeleton TyS _ Ty) "derive.lens_laws: law4 statement illtyped",
87 std.assert-ok! (coq.elaborate-skeleton BoS Ty Bo) "derive.lens_laws: law4 proof illtyped".
88 func law4-rty.aux term, term, term, list term, list term -> term.
89 law4-rty.aux L1 L2 _ Vs _ {{ lib:elpi.derive.lens.exchange_on lp:L1 lp:L2 lp:R }} :- std.last Vs R.
90 func law4-bo.aux term, term, list term, list term -> term.
91 law4-bo.aux _ _ _ _ {{ fun x y => lib:@elpi.erefl _ _ }}.
92
93 func main inductive, string -> list prop.
94 main I Prefix [lens-laws-done I] :- std.do! [
95 std.findall (lens-db I F_ L_) Lenses,
96 std.forall Lenses (declare-law1 Prefix),
97 std.forall Lenses (declare-law2 Prefix),
98 std.forall Lenses (declare-law3 Prefix),
99 std.forall Lenses (l1\ std.forall Lenses (l2\ declare-law4 Prefix l1 l2)),
100 coq.elpi.accumulate _ "derive.lens_laws.db" (clause _ _ (lens-laws-done I)),
101 ].
102
103 }