Debian Codegraph @

apps/derive/elpi/bcongr.elpi

raw
1
2 /* Boolean congruence lemmas */
3 /* license: GNU Lesser General Public License Version 2.1 or later */
4 /* ------------------------------------------------------------------------- */
5
6 shorten std.{nth, map2, assert!, rev, do!}.
7
8 namespace derive.bcongr {
9
10 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
11 % take in input all hyps
12
13 % links an assumption to the two terms being compared and the boolean value
14 % for them: forall x y b (H : reflext (x = y) b), ----> (arg H x y b)
15 kind arg type.
16 type arg term -> term -> term -> term -> arg.
17
18 func bo-args term, term, term, list arg -> term.
19
20 bo-args (prod N S T) K1 K2 Hs (fun `x` S x\ fun `y` S y\ fun `b` {{bool}} b\ R x y b) :- !,
21 @pi-decl `x` S x\
22 @pi-decl `y` S y\
23 @pi-decl `b` {{ Corelib.Init.Datatypes.bool }} b\
24 @pi-decl Hn (TH x y b) h\
25 do! [
26 TH x y b = {{ lib:elpi.reflect (lib:@elpi.eq lp:S lp:x lp:y) lp:b }},
27 R x y b = (fun `h` (TH x y b) h\ Body x y b h),
28 coq.name-suffix `h` N Hn,
29 bo-args (T x) {coq.mk-app K1 [x]} {coq.mk-app K2 [y]} [arg h x y b|Hs]
30 (Body x y b h)
31 ].
32
33 bo-args T K1 K2 HsRev Bo :- (T = global (indt _) ; T = app[global (indt _)|_]), !,
34 rev HsRev Hs,
35 mk-conj Hs Conj,
36 Concl = {{ lib:elpi.reflect (lib:@elpi.eq lp:T lp:K1 lp:K2) lp:Conj }},
37 elim-all 0 Hs Concl Bo.
38
39 bo-args T K1 K2 HsRev Bo :- whd1 T T1, !, bo-args T1 K1 K2 HsRev Bo.
40
41 func mk-conj list arg -> term.
42 mk-conj [] {{ lib:elpi.true }}.
43 mk-conj [arg _ _ _ X] X :- !.
44 mk-conj [arg _ _ _ X|XS] {{ lib:elpi.andb lp:X lp:C }} :-
45 mk-conj XS C.
46
47 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
48 % we case split on each and every assumption (arg H _ _ _)
49
50 func elim-all int, list arg -> term, term.
51 elim-all _ [] {{ lib:elpi.reflect lp:P lp:_}}
52 {{ lib:elpi.ReflectT lp:P (lib:@elpi.erefl lp:T lp:LHS) }} :-
53 coq.safe-dest-app P _ [T,LHS,_].
54
55 elim-all N [arg H X Y B|Hs] P R :-
56 decl H _ TH,
57 (pi x b\ (copy Y x :- !) => (copy B b :- !) => copy P (Pxb x b)), !,
58 coq.build-match H TH (otyR Pxb Y) (branchR Pxb X Y N Hs) R.
59
60 func otyR (term -> term -> term), term, term, list term, list term -> term.
61 otyR F X _ [Idx,_] _ R :-
62 R = F X Idx.
63
64 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
65 % Two branch per hyp: ReflectT or ReflectF
66
67 % we continue
68 func branchR (term -> term -> term), term, term, int, list arg, term ,term, list term, list term -> term.
69
70 branchR Pxb X _ N Hs K _ [E] [ET] R :-
71 coq.safe-dest-app K {{ lib:elpi.ReflectT }} _, !,
72 coq.build-match E ET (otyE Pxb {{ lib:elpi.true}}) (branchE (Pxb X {{ lib:elpi.true }}) N Hs) R.
73
74 % we stop, emit ReflectF and prove false via injection
75 branchR Pxb _ Y N _ K _ [NE] [_] R :-
76 coq.safe-dest-app K {{ lib:elpi.ReflectF }} _, !,
77 Pxb Y {{ lib:elpi.false}} = {{ lib:elpi.reflect lp:P lp:_ }},
78 R = {{ lib:elpi.ReflectF lp:P lp:PNE }},
79 PNE = (fun `h` P h\ app[NE, Inj h]),
80 @pi-decl `h` P h\ do! [
81 ltac.injection h P _ (PEs h),
82 nth N (PEs h) (Inj h) ].
83
84 func branchE term, int, list arg, term ,term, list term, list term -> term.
85 branchE P N Hs _ _ [] [] R :- M is N + 1, elim-all M Hs P R.
86
87 func otyE (term -> term -> term), term, term, list term, list term -> term.
88 otyE F B _ [X,_] _ R :-
89 R = F X B.
90
91 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
92 % we take in input all parameters
93
94 :index (1)
95 func bo-param int, term, term -> term.
96
97 bo-param 0 K T R :- !, bo-args T K K [] R.
98 bo-param N K (prod Name Src T) (fun Name Src T1) :- N > 0, !,
99 M is N - 1,
100 @pi-decl Name Src x\
101 bo-param M {coq.mk-app K [x]} (T x) (T1 x).
102
103 bo-param N K T R :- whd1 T T1, !, bo-param N K T1 R.
104
105 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106 % for each constructor we generate the congruence lemma
107
108 func main-constructor int, string, constructor, term -> prop.
109
110 main-constructor Lno Prefix K Kt Clause :- do! [
111 Kn = global (indc K),
112 % we build the comparison function
113 bo-param Lno Kn Kt R,
114 std.assert-ok! (coq.typecheck R RT) "derive.bcongr generates illtyped term",
115 Name is Prefix ^ "bcongr_" ^ {coq.gref->id (indc K)},
116 coq.ensure-fresh-global-id Name FName,
117 coq.env.add-const FName R RT @opaque! Cong,
118
119 % we register it as a clause
120 Clause = (bcongr-db K (global (const Cong)) :- !),
121 coq.elpi.accumulate _ "derive.bcongr.db"
122 (clause _ (before "bcongr-db:fail") Clause)
123 ].
124
125 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
126
127 func main inductive, string -> list prop.
128 main GR Prefix Clauses :- do! [
129 coq.env.indt GR Ind Lno _ _ Kns Ktys,
130 assert! (Ind = tt) "derive.bcongr: Coinductive types are not supported",
131
132 map2 Kns Ktys (main-constructor Lno Prefix) Clauses
133 ].
134
135 }
136
137 % vim: set spelllang=: