Debian Codegraph @

apps/derive/elpi/eqbOK.elpi

raw
1 /* equality test soundness proof */
2 /* license: GNU Lesser General Public License Version 2.1 or later */
3 /* ------------------------------------------------------------------------- */
4
5
6 func feqb.trm->term eqb.trm -> term.
7
8 macro @pi-trm N T F :-
9 pi x xx\ decl x N T => (feqb.trm->term xx x :- !) => F xx x.
10
11 namespace derive.eqbOK {
12
13 :index (1)
14 func add-reflect eqb.eqType, term, term -> term.
15 add-reflect (eqb.type-param F) Correct Refl {{
16 fun (a : lp:Type) (eqA: a -> a -> bool) (heqA : lp:(HeqA a eqA)) => lp:(R a eqA heqA)
17 }} :-
18 Type = sort (typ {coq.univ.new}),
19 HeqA = (a\eqA\ {{ forall x1 x2 : lp:a, lib:elpi.reflect (@eq lp:a x1 x2) (lp:eqA x1 x2) }}),
20 @pi-trm `a` Type aa\a\
21 @pi-decl `eqA` {{ lp:a -> lp:a -> bool }} eqA\
22 @pi-decl `heqA` (HeqA a eqA) heqA\
23 add-reflect (F aa)
24 {{lp:Correct lp:a lp:eqA (fun (a1 a2 : lp:a) => @elimT (@eq lp:a a1 a2) (lp:eqA a1 a2) (lp:heqA a1 a2))}}
25 {{lp:Refl lp:a lp:eqA (fun (a1: lp:a) => @introT (@eq lp:a a1 a1) (lp:eqA a1 a1) (lp:heqA a1 a1) (@refl_equal lp:a a1))}}
26 (R a eqA heqA).
27 add-reflect (eqb.value-param TY F) Correct Refl {{ fun x : lp:Ty => lp:(R x) }} :-
28 feqb.trm->term TY Ty,
29 @pi-trm `x` Ty xx\x\
30 add-reflect (F xx)
31 {{lp:Correct lp:x}}
32 {{lp:Refl lp:x}}
33 (R x).
34 add-reflect (eqb.inductive _ _) Correct Refl {{iffP2 lp:Correct lp:Refl}}.
35
36 pred add-sumbool i:eqb.eqType, i:term, o:term.
37 add-sumbool (eqb.type-param F) Reflect {{
38 fun (a : lp:Type) (heqA : lp:(HeqA a)) => lp:(R a heqA)
39 }} :-
40 Type = sort (typ {coq.univ.new}),
41 HeqA = (a\ {{ forall x1 x2 : lp:a, { @eq lp:a x1 x2 } + { ~ (@eq lp:a x1 x2) } }}),
42 @pi-trm `a` Type aa\a\
43 @pi-decl `heqA` (HeqA a) heqA\
44 add-sumbool (F aa)
45 {{lp:Reflect lp:a (eqb_of_dec lp:a lp:heqA) (dec_reflect lp:a lp:heqA) }}
46 (R a heqA).
47 add-sumbool (eqb.value-param TY F) Reflect {{ fun x : lp:Ty => lp:(R x) }} :-
48 feqb.trm->term TY Ty,
49 @pi-trm `x` Ty xx\x\
50 add-sumbool (F xx)
51 {{lp:Reflect lp:x}}
52 (R x).
53 add-sumbool (eqb.inductive _ _) Reflect {{reflect_dec _ _ lp:Reflect}}.
54
55 func main gref, string -> list prop.
56 main (indt I) Prefix [CL] :- std.do! [
57 std.assert! (eqType (indt I) FI) "this inductive is not supported",
58 std.assert! (eqcorrect-for (indt I) Correct Refl) "run eqbcorrect before",
59
60 add-reflect FI (global (const Correct)) (global (const Refl)) Breflect,
61
62 std.assert-ok! (coq.typecheck Breflect Treflect) "eqbOK generates illtyped term",
63 coq.ensure-fresh-global-id (Prefix ^ "eqb_OK") Namerf,
64 coq.env.add-const Namerf Breflect Treflect @opaque! Reflect,
65
66
67 add-sumbool FI (global (const Reflect)) Breflect_sb,
68 std.assert-ok! (coq.typecheck Breflect_sb Treflect_sb) "eqbOK generates illtyped term",
69 coq.ensure-fresh-global-id (Prefix ^ "eqb_OK_sumbool") Namerf_sb,
70 coq.env.add-const Namerf_sb Breflect_sb Treflect_sb @opaque! Reflect_sb,
71
72 CL = eqbok-for (indt I) Reflect,
73
74 coq.elpi.accumulate _ "derive.eqbOK.db" (clause _ _ CL),
75
76 coq.register (const Reflect_sb) (coq.register.scheme (indt I) coq.register.scheme.eq_dec),
77
78 ].
79 main (const C) Prefix [CL] :- std.do! [
80 std.assert! (eqb-for (global (const C)) (global (const C)) F) "run eqb before",
81 std.assert! (eqcorrect-for (const C) Correct Refl) "run eqbcorrect before",
82 add-reflect (eqb.inductive _ _) (global (const Correct)) (global (const Refl)) Breflect,
83
84 std.assert-ok! (coq.typecheck Breflect _) "eqbOK generates illtyped term",
85 coq.ensure-fresh-global-id (Prefix ^ "eqb_OK") Namerf,
86 X = (global (const C) : term),
87 coq.env.add-const Namerf Breflect {{ forall a b : lp:X, lib:elpi.reflect (@eq lp:X a b) (lp:F a b) }} @opaque! Reflect,
88 CL = eqbok-for (const C) Reflect,
89 coq.elpi.accumulate _ "derive.eqbOK.db" (clause _ _ CL),
90 coq.elpi.accumulate _ "derive.eqType.db" (clause _ _ (eqType (const C) eqb.axiom)),
91
92 ].
93 main (indc _) _ _ :- stop "cannot call eqbOK on a constructor".
94
95 }