Debian Codegraph @

apps/derive/elpi/eqbcorrect.elpi

raw
1 /* equality test correctness and reflexivity proof */
2 /* license: GNU Lesser General Public License Version 2.1 or later */
3 /* ------------------------------------------------------------------------- */
4
5 func feqb.trm->term eqb.trm -> term.
6
7 macro @pi-trm N T F :-
8 pi x xx\ decl x N T => (feqb.trm->term xx x :- !) => F xx x.
9
10 namespace derive.eqbcorrect {
11
12 func has-params? eqb.eqType ->.
13 has-params? (eqb.type-param _).
14 has-params? (eqb.value-param _ _).
15
16 % use:
17 % config Pred Pred_on Pred_body Solver Db
18 % to make the code agnostic on the proof of correctness or reflexivity
19 %
20 % eg: config {{ @eqb_correct }} {{ @eqb_correct_on }} {{ @eqb_body_correct }} "eqb_correct_on__solver" correct-lemma-for
21 pred config o:term, o:term, o:term, o:string, o:(pred i:term, o:term).
22
23 func main gref, string -> list prop.
24 main (indt I) Prefix CLs :- std.do! [
25
26 std.assert! (eqType (indt I) FI) "this inductive is not supported",
27
28 std.assert! (induction-db I Indu) "call derive.induction before",
29
30 /* Correctness */
31
32 (config {{ @eqb_correct }} {{ @eqb_correct_on }} {{ @eqb_body_correct }} "eqb_correct_on__solver" correct-lemma-for =>
33 common FI [] Indu R),
34
35 %std.assert! (ground_term R) "ww",
36 std.assert-ok! (coq.typecheck R Ty) "derive.eqbcorrect: common/correct generates ill typed term",
37 Name is Prefix ^ "eqb_correct",
38 coq.ensure-fresh-global-id Name FName,
39 coq.env.add-const FName R Ty @opaque! Correct,
40
41 if (has-params? FI) (
42
43 (config {{ @eqb_correct }} {{ @eqb_correct_on }} {{ @eqb_body_correct }} "eqb_correct_on__solver" correct-lemma-for =>
44 common-aux FI [] Indu Rx),
45
46 std.assert-ok! (coq.typecheck Rx Tyx) "derive.eqbcorrect: common-aux/corect generates ill typed term",
47 Namex is Prefix ^ "eqb_correct_aux",
48 coq.ensure-fresh-global-id Namex FNamex,
49 coq.env.add-const FNamex Rx Tyx @opaque! Correctx,
50 CL_CORRECT = [correct-lemma-for (global (indt I)) (global (const Correctx))]
51
52 ) (CL_CORRECT = [correct-lemma-for (global (indt I)) (global (const Correct))]),
53
54 /* Reflexivity */
55
56 (config {{ @eqb_reflexive }} {{ @eqb_refl_on }} {{ @eqb_body_refl }} "eqb_refl_on__solver" refl-lemma-for =>
57 common FI [] Indu Rr),
58
59 std.assert-ok! (coq.typecheck Rr Tyr) "derive.eqbcorrect: common/refl generates ill typed term",
60 Namer is Prefix ^ "eqb_refl",
61 coq.ensure-fresh-global-id Namer FNamer,
62 coq.env.add-const FNamer Rr Tyr @opaque! Refl,
63
64 if (has-params? FI) (
65
66 (config {{ @eqb_reflexive }} {{ @eqb_refl_on }} {{ @eqb_body_refl }} "eqb_refl_on__solver" refl-lemma-for =>
67 common-aux FI [] Indu Rrx),
68
69 std.assert-ok! (coq.typecheck Rrx Tyrx) "derive.eqbcorrect: common-aux/refl generates ill typed term",
70 Namerx is Prefix ^ "eqb_refl_aux",
71 coq.ensure-fresh-global-id Namerx FNamerx,
72 coq.env.add-const FNamerx Rrx Tyrx @opaque! Reflx,
73 CL_REFL = [refl-lemma-for (global (indt I)) (global (const Reflx))]
74
75 ) (CL_REFL = [refl-lemma-for (global (indt I)) (global (const Refl))]),
76
77 /* Add the clauses in the database */
78 std.flatten [ [ eqcorrect-for (indt I) Correct Refl ] , CL_CORRECT , CL_REFL ] CLs ,
79
80 std.forall CLs (x\coq.elpi.accumulate _ "derive.eqbcorrect.db" (clause _ _ x)),
81
82 ].
83 main (const C) Prefix [Clause4, ClauseC, ClauseR] :- std.do! [
84 std.assert! (eqb-for (global (const C)) (global (const C)) F) "run eqb first",
85 coq.env.const C (some T) _,
86 search-eqcorrect-for T Correct Refl,
87 std.assert-ok! (coq.typecheck Correct _) "eqbcorrect: illtyped correct",
88 std.assert-ok! (coq.typecheck Refl _) "eqbcorrect: illtyped refl",
89 NameR is Prefix ^ "eqb_refl",
90 NameC is Prefix ^ "eqb_correct",
91 X = (global (const C) : term),
92 coq.ensure-fresh-global-id NameR FNameR,
93 coq.ensure-fresh-global-id NameC FNameC,
94 coq.env.add-const FNameC Correct {{ @eqb_correct lp:X lp:F }} @transparent! CC,
95 coq.env.add-const FNameR Refl {{ @eqb_reflexive lp:X lp:F }} @transparent! CR,
96 Clause4 = (eqcorrect-for (const C) CC CR),
97 ClauseC = (correct-lemma-for (global (const C)) (global (const CC))),
98 ClauseR = (refl-lemma-for (global (const C)) (global (const CR))),
99 coq.elpi.accumulate _ "derive.eqbcorrect.db" (clause _ _ Clause4),
100 coq.elpi.accumulate _ "derive.eqbcorrect.db" (clause _ _ ClauseC),
101 coq.elpi.accumulate _ "derive.eqbcorrect.db" (clause _ _ ClauseR),
102 ].
103 main (indc _) _ _ :- stop "derive.eqbcorrect does not work on a constructor".
104
105 %---------------------------------------------------------------------------
106
107 func search-eqcorrect-for term -> term, term.
108 search-eqcorrect-for (global (indt I)) (global (const C)) (global (const R)) :-
109 std.assert! (eqType (indt I) (eqb.inductive _ _)) "unknown or not applied enough type",
110 eqcorrect-for (indt I) C R.
111
112 search-eqcorrect-for (app[global (indt I)|Args]) CArgs RArgs :-
113 std.assert! (eqType (indt I) F) "unknown",
114 eqcorrect-for (indt I) C R,
115 search-eqcorrect-apply F Args (global (const C)) (global (const R)) CArgs RArgs.
116
117 func search-eqcorrect-apply eqb.eqType, list term, term, term -> term, term.
118 search-eqcorrect-apply (eqb.type-param F) [T|Args] C R C1 R1 :-
119 search-eqcorrect-for T CT RT,
120 pi x\
121 search-eqcorrect-apply (F x) Args {coq.mk-app C [T,_,CT]} {coq.mk-app R [T,_,RT]} C1 R1.
122 search-eqcorrect-apply (eqb.value-param _ F) [T|Args] C R C1 R1 :-
123 pi x\
124 search-eqcorrect-apply (F x) Args {coq.mk-app C [T]} {coq.mk-app R [T]} C1 R1.
125 search-eqcorrect-apply (eqb.inductive _ _) [] C R C R.
126
127 %---------------------------------------------------------------------------
128
129 func run-solver sealed-goal, string ->.
130 run-solver G Name :-
131 if (coq.ltac.open (g\gl\coq.ltac.call Name [] g gl ok) G []) true
132 ((@holes! => coq.sealed-goal->string G SG),
133 std.fatal-error {calc ( "solver " ^ Name ^ " fails on goal:\n" ^ SG )}).
134
135 func coq.sealed-goal->string sealed-goal -> string.
136 coq.sealed-goal->string (nabla G) R :- pi x\ coq.sealed-goal->string (G x) R.
137 coq.sealed-goal->string (seal (goal Ctx _ Ty _ _)) R :-
138 Ctx =!=> (std.map {std.rev Ctx} coq.ctx->string L, coq.term->string Ty G, R is "Lemma foo " ^ {std.string.concat "\n" L} ^ "\n :\n" ^ G ^ ".").
139
140 func coq.ctx->string prop -> string.
141 coq.ctx->string (decl X _ Ty) R :- R is "(" ^ {coq.term->string X} ^ " : " ^ {coq.term->string Ty} ^ ")".
142 coq.ctx->string (def X _ Ty B) R :- R is "(" ^ {coq.term->string X} ^ " : " ^ {coq.term->string Ty} ^ " := " ^ {coq.term->string B} ^ ")".
143
144 pred common-body o:term.
145 pred fields-t o:term, o:term, o:term, o:term.
146
147 %---------------------------------------------------------------------------
148 :index (1)
149 func common eqb.eqType, list term, term -> term.
150 common (eqb.type-param F) Params Ind O :- std.do! [
151 config Pred Pred_on _Pred_body _Solver Db,
152 O = {{ fun (a : lp:Type) (eqA : a -> a -> bool) (eqAc : lp:Pred a eqA) => lp:(R a eqA eqAc) }},
153 Type = sort (typ {coq.univ.new}),
154 @pi-trm `a` Type aa\a\
155 @pi-decl `eqA` {{ lp:a -> lp:a -> bool }} eqA\
156 @pi-decl `eqAc` {{ lp:Pred lp:a lp:eqA }} eqAc\
157 param1-inhab-db {{ lp:Pred_on lp:a lp:eqA }} eqAc =>
158 eqb-for a a eqA =>
159 reali a {{ lp:Pred_on lp:a lp:eqA }} =>
160 prove Db a {{ fun (x: lp:a) (Hx : lp:Pred_on lp:a lp:eqA x) => Hx }} =>
161 common (F aa) [a|Params]
162 {coq.mk-app Ind [a, {{ lp:Pred_on lp:a lp:eqA }} ] }
163 (R a eqA eqAc)
164 ].
165
166 common (eqb.value-param TY F) Params Ind O :- std.do! [
167 feqb.trm->term TY T,
168 O = {{ fun (a : lp:T) => lp:(R a) }},
169 mk-reali T TR,
170 std.assert! (param1-inhab-db TR Is_full) "not trivially inhabited",
171 @pi-trm `a` T aa\a\
172 reali a {{ lp:Is_full lp:a }} =>
173 common (F aa) [a|Params]
174 {{ lp:Ind lp:a (lp:Is_full lp:a) }}
175 (R a)
176 ].
177
178 common (eqb.inductive I Ks) ParamsRev Ind O :- std.do! [
179 config _Pred Pred_on Pred_body _Solver Db,
180 std.rev ParamsRev Params,
181 coq.mk-app (global (indt I)) Params Ty,
182
183 mk-eqb-for Ty Cmp,
184 tag-for I TagC,
185 fields-for I Fields_tC FieldsC ConstructC ConstructPC,
186 coq.mk-app (global (const TagC)) Params Tag,
187 coq.mk-app (global (const Fields_tC)) Params Fields_t,
188 coq.mk-app (global (const FieldsC)) Params Fields,
189 coq.mk-app (global (const ConstructC)) Params Construct,
190 coq.mk-app (global (const ConstructPC)) Params ConstructP,
191 eqb-fields Ty Ty EqbFields,
192 Common = ({{ lp:Pred_body lp:Ty lp:Tag lp:Fields_t lp:Fields lp:Construct lp:ConstructP lp:EqbFields }} : term),
193 std.assert-ok! (coq.typecheck Common CommonTy) "WTF",
194
195 mk-reali (global (indt I)) IR, % param1-db, really
196 coq.safe-dest-app Ind _ RealiArgs,
197 coq.mk-app IR RealiArgs TyR,
198 std.assert! (param1-inhab-db TyR Is_full) "not trivially inhabited",
199 mk-eqb-for Ty Cmp,
200 (@pi-decl `x` Ty x\
201 @pi-def `common` CommonTy Common c\ common-body c => fields-t Tag Fields_t Fields Construct =>
202 prove Db Ty {{ fun (i : lp:Ty) (Hi : lp:Pred_on lp:Ty lp:Cmp i) => Hi }} =>
203 reali Ty {{ lp:Pred_on lp:Ty lp:Cmp }} =>
204 pi i\ (feqb.trm->term i Ty :- !) =>
205 std.do! [
206 std.map (Ks i) (branch Params) (LS c),
207 std.append (LS c) [x, app[Is_full,x]] (Args x c),
208 R x c = (app [Ind, {{ lp:Pred_on lp:Ty lp:Cmp }} | Args x c] : term),
209 ]),
210 O = {{ fun (x :lp:Ty) (common : lp:CommonTy := lp:Common) => lp:(R x common) }},
211 ].
212
213 %---------------------------------------------------------------------------
214
215 :index (1)
216 func common-aux eqb.eqType, list term, term -> term.
217 common-aux (eqb.type-param F) Params Ind O :- std.do! [
218 config _Pred Pred_on _Pred_body _Solver Db,
219 O = {{ fun (a : lp:Type) (eqA : a -> a -> bool) => lp:(R a eqA) }},
220 Type = sort (typ {coq.univ.new}),
221 @pi-trm `a` Type aa\a\
222 @pi-decl `eqA` {{ lp:a -> lp:a -> bool }} eqA\
223 eqb-for a a eqA =>
224 reali a {{ lp:Pred_on lp:a lp:eqA }} =>
225 prove Db a {{ fun (x: lp:a) (Hx : lp:Pred_on lp:a lp:eqA x) => Hx }} =>
226 common-aux (F aa) [a|Params]
227 {coq.mk-app Ind [a, {{ lp:Pred_on lp:a lp:eqA }} ] }
228 (R a eqA)
229 ].
230
231 common-aux (eqb.value-param TY F) Params Ind O :- std.do! [
232 feqb.trm->term TY T,
233 O = {{ fun (a : lp:T) (pa : lp:TR a) => lp:(R a pa) }},
234 mk-reali T TR,
235 @pi-trm `a` T aa\a\
236 @pi-decl `pa` {{ lp:TR lp:a }} pa\
237 reali a pa =>
238 common-aux (F aa) [a|Params]
239 {{ lp:Ind lp:a lp:pa }} (R a pa)
240 ].
241
242 common-aux (eqb.inductive I Ks) ParamsRev Ind O :- std.do! [
243 config _Pred Pred_on Pred_body _Solver Db,
244 std.rev ParamsRev Params,
245 coq.mk-app (global (indt I)) Params Ty,
246 mk-eqb-for Ty Cmp,
247 tag-for I TagC,
248 fields-for I Fields_tC FieldsC ConstructC ConstructPC,
249 coq.mk-app (global (const TagC)) Params Tag,
250 coq.mk-app (global (const Fields_tC)) Params Fields_t,
251 coq.mk-app (global (const FieldsC)) Params Fields,
252 coq.mk-app (global (const ConstructC)) Params Construct,
253 coq.mk-app (global (const ConstructPC)) Params ConstructP,
254 eqb-fields Ty Ty EqbFields,
255 Common = ({{ lp:Pred_body lp:Ty lp:Tag lp:Fields_t lp:Fields lp:Construct lp:ConstructP lp:EqbFields }} : term),
256 std.assert-ok! (coq.typecheck Common CommonTy) "WTF",
257 prove Db Ty {{ fun (i : lp:Ty) (Hi : lp:Pred_on lp:Ty lp:Cmp i) => Hi }} =>
258 reali Ty {{ lp:Pred_on lp:Ty lp:Cmp }} =>
259 (pi i\ (feqb.trm->term i Ty :- !) =>
260 @pi-def `common` CommonTy Common c\ common-body c => fields-t Tag Fields_t Fields Construct =>
261 std.map (Ks i) (branch Params) (LS c)),
262 O = (let `common` CommonTy Common c\ app [Ind, {{ lp:Pred_on lp:Ty lp:Cmp }} | LS c]),
263 ].
264
265 %---------------------------------------------------------------------------
266
267 func branch list term, eqb.constructor -> term.
268 branch Params (eqb.constructor K Args) R :-
269 coq.mk-app (global (indc K)) Params KParams,
270 args Args KParams [] [] [] 0 R.
271
272 :index (1)
273 func args eqb.arguments, term, list term,list term, list term, int -> term.
274 args (eqb.irrelevant TY Args) K As Hs Bs N O :- std.do! [
275 O = {{ fun (x : lp:T) (px : lp:EqbOn x) => lp:(R x px) }},
276 feqb.trm->term TY T,
277 mk-reali T EqbOn,
278 @pi-decl `x` T x\
279 @pi-decl `px` {{ lp:EqbOn lp:x }} px\
280 args Args {coq.mk-app K [x]} [x|As] Hs Bs {calc (N + 1)} (R x px)
281 ].
282
283 args (eqb.regular TY Args) K As Hs Bs N O :- std.do! [
284 config _Pred Pred_on _Pred_body _Solver Db,
285 O = {{ fun (x : lp:T) (px : lp:EqbOn x) (h : lp:EqbOn' x := lp:View x px) => lp:(R x px h) }},
286 feqb.trm->term TY T,
287 mk-reali T EqbOn,
288 mk-eqb-for T Cmp,
289 EqbOn' = {{ lp:Pred_on lp:T lp:Cmp }},
290 if (same_term EqbOn EqbOn') (View = {{ fun (x : lp:T) (px : lp:EqbOn x) => px }}) (prove Db T View),
291 @pi-decl `x` T x\
292 @pi-decl `px` {{ lp:EqbOn lp:x }} px\ % reali x px =>
293 @pi-def `h` {{ lp:EqbOn' lp:x }} {{ lp:View lp:x lp:px }} h\
294 args Args {coq.mk-app K [x]} [x|As] [h|Hs] [{{ lp:Cmp lp:x }}|Bs] N (R x px h)
295 ].
296
297 args (eqb.dependent TY Args) K As Hs Bs N O :- std.do! [
298 config _Pred Pred_on _Pred_body _Solver Db,
299 O = {{ fun (x : lp:T) (px : lp:EqbOn x) (h : lp:EqbOn' x := lp:View x px) => lp:(R x px h) }},
300 feqb.trm->term TY T,
301 mk-reali T EqbOn,
302 mk-eqb-for T Cmp,
303 EqbOn' = {{ lp:Pred_on lp:T lp:Cmp }},
304 if (same_term EqbOn EqbOn') (View = {{ fun (x : lp:T) (px : lp:EqbOn x) => px }}) (prove Db T View),
305 @pi-trm `x` T xx\x\
306 @pi-decl `px` {{ lp:EqbOn lp:x }} px\ reali x px =>
307 @pi-def `h` {{ lp:EqbOn' lp:x }} {{ lp:View lp:x lp:px }} h\
308 args (Args xx) {coq.mk-app K [x]} [x|As] [h|Hs] [{{ lp:Cmp lp:x }}|Bs] {calc (N + 1)} (R x px h)
309 ].
310
311 args (eqb.stop TY) K As Hs Bs 0 {{ lp:B : lp:Pred_on lp:T lp:Cmp lp:K }} :- % no tricky arguments
312 config {{ @eqb_correct }} Pred_on _Pred_body _Solver _Db, !, std.do! [
313 feqb.trm->term TY T,
314 mk-eqb-for T Cmp,
315 fields-t Tag Fields_t Fields Construct,
316 eqb-fields T T EqbFields,
317 std.assert! (common-body Common) "anomaly, no let for common body proof",
318 coq.mk-app Common [K,{{ fun (x : lp:Fields_t (lp:Tag lp:K)) => lp:(Proof x) }}] B,
319 @pi-decl `x` {{ lp:Fields_t (lp:Tag lp:K) }} x\
320 HYP = (x\ {{ @eq bool (lp:EqbFields (lp:Tag lp:K) (lp:Fields lp:K) lp:x) true }} : term -> term),
321 GOAL = (x\ {{ @eq (option lp:T) (@Some lp:T lp:K) (lp:Construct (lp:Tag lp:K) lp:x) }} : term -> term),
322 correct-proof x {{ lp:Fields_t (lp:Tag lp:K) }} HYP GOAL As Bs Hs (Proof x)
323 ].
324 args (eqb.stop TY) K _As _Hs _Bs _ {{ lp:B : lp:Pred_on lp:T lp:Cmp lp:K }} :-
325 config {{ @eqb_correct }} Pred_on _Pred_body Solver _Db, !, std.do! [
326 feqb.trm->term TY T,
327 mk-eqb-for T Cmp,
328 std.assert! (common-body Common) "anomaly, no let for common body proof",
329 coq.mk-app Common [K,Fresh_] B,
330 std.assert-ok! (coq.typecheck {{ lp:B : lp:Pred_on lp:T lp:Cmp lp:K }} _) "illtyped correctness proof",
331 std.assert! (coq.ltac.collect-goals B [G] _) "wrong number of goals",
332 run-solver G Solver,
333 ].
334
335 func correct-proof term, term, (term -> term), (term -> term), list term, list term, list term -> term.
336 correct-proof X TX H G As Bs Hs R :-
337 coq.build-match X TX (cp.rty H G) (cp.bs As Bs Hs G) R.
338
339 func cp.rty (term -> term), (term -> term), term, list term, list term -> term.
340 cp.rty H G _ Vs _ {{ lp:{{ H X }} -> lp:{{ G X }} }} :- std.last Vs X.
341
342 func cp.bs list term, list term, list term, (term -> term), term, term, list term, list term -> term.
343 cp.bs As Bs Hs G K _ Vs Ts {{ @impliesP lp:Bools lp:P lp:Next }} :-
344 std.rev Vs VsRev,
345 mkblistcorrect Bs VsRev {{ bnil }} Bools,
346 P = G {coq.mk-app K Vs },
347 cp.curry {std.rev Bs} As Vs Vs Ts Hs [] G K Next.
348
349 :index (1)
350 func cp.curry list term, list term, list term, list term, list term, list term, list term, (term -> term), term -> term.
351 cp.curry [B|Bs] As [V|Vs] OVs Ts Hs Hs2 G K W :- std.do! [
352 TyH = ({{ @eq bool (lp:B lp:V) true }} : term),
353 (@pi-decl `h` TyH h\ cp.curry Bs As Vs OVs Ts Hs [h|Hs2] G K (R h)),
354 W = {{ fun h : lp:TyH => lp:(R h) }},
355 ].
356 cp.curry [] As [] Vs Ts Hs Hs2 G K R :- std.do! [
357 mktlistcorrect {std.rev Ts} {{ tnil }} Types,
358 mkrewpred Ts K G P,
359 (pi x\ sigma X\G x = {{ @eq lp:T lp:LHS lp:X }}),
360 End = ({{ @eq_refl lp:T lp:LHS }} : term),
361 mkeqns {std.rev As} Vs {std.rev Hs} {std.rev Hs2} Eqns,
362 coq.mk-app {{ @eq_ind_r_nP lp:Types lp:P }} {std.append Eqns [End]} R,
363 ].
364
365 :index (1)
366 func mkrewpred list term, term, (term -> term) -> term.
367 mkrewpred [] K G (G K).
368 mkrewpred [T|Ts] K G {{ fun w : lp:T => lp:(R w) }} :-
369 @pi-decl `w` T w\
370 mkrewpred Ts {coq.mk-app K [w]} G (R w).
371
372 :index (1)
373 func mkeqns list term, list term, list term, list term -> list term.
374 mkeqns [] [] [] [] [].
375 mkeqns [A|As] [V|Vs] [H|Hs] [H2|H2s] [A,V,{{ lp:H lp:V lp:H2 }}|R] :-
376 mkeqns As Vs Hs H2s R.
377
378 :index (1)
379 func mktlistcorrect list term, term -> term.
380 mktlistcorrect [] ACC ACC.
381 mktlistcorrect [X|XS] ACC R :-
382 mktlistcorrect XS {{ tcons lp:X lp:ACC }} R.
383
384 :index (1)
385 func mkblistcorrect list term, list term, term -> term.
386 mkblistcorrect [] [] ACC ACC.
387 mkblistcorrect [X|XS] [V|VS] ACC R :-
388 coq.mk-app X [V] E,
389 mkblistcorrect XS VS {{ bcons lp:E lp:ACC }} R.
390
391 args (eqb.stop TY) K _As Hs Bs _ {{ lp:B : lp:Pred_on lp:T lp:Cmp lp:K }} :-
392 config {{ @eqb_reflexive }} Pred_on _Pred_body _Solver _Db, !, std.do! [
393 feqb.trm->term TY T,
394 mk-eqb-for T Cmp,
395 std.assert! (common-body Common) "anomaly, no let for common body proof",
396 mkblistrefl {std.rev Bs} {{ bnil }} Bools,
397 Proof = (app [ {{ @eqb_refl_statementP }} , Bools | Hs ] : term),
398 coq.mk-app Common [K,Proof] B,
399 ].
400
401 :index (1)
402 func mkblistrefl list term, term -> term.
403 mkblistrefl [] ACC ACC.
404 mkblistrefl [X|XS] ACC R :-
405 coq.safe-dest-app X _ Args,
406 std.last Args A,
407 coq.mk-app X [A] E,
408 mkblistrefl XS {{ bcons lp:E lp:ACC }} R.
409
410 %---------------------------------------------------------------------------
411
412 % prove {{ is_option (seq A) (is_seq A (eqb_correrct A eqA)) }} T
413 % such that T has type {{ forall x (px : is_option ...), eqb_correrct (option (seq A)) x }}
414 func prove (pred i:term, o:term), term -> term.
415 prove Lemma T R :- search Lemma (prove Lemma) T R.
416 %prove T R :- whd1 T T', !, prove T' R.
417
418 :index (_ _ 3)
419 func search (pred i:term, o:term), (func term -> term), term -> term.
420 search What Rec (app [GR|L] as GRL) R :- !, std.do! [
421 What GR Aux,
422 mk-reali GR (global (indt ISGR)),
423 param1-functor-for ISGR Funct BitMask, !,
424 apply-functor (global Funct) BitMask L Rec TOTO,
425 apply-aux Aux L Aux1,
426 R = {{ fun (x : lp:GRL) H => lp:Aux1 x (lp:TOTO x H) }},
427 ].
428 % no params, no aux lemma (no reali argument)
429 search What _Rec (global GR as GRL) {{ fun (x : lp:GRL) (_ : lp:IsGR x) => lp:R x }} :-
430 What (global GR) R,
431 mk-reali (global GR) IsGR, !.
432 search What _ X _ :- coq.safe-dest-app X HD _, std.assert! (What HD _) "run eqbcorrect before".
433
434 func apply-aux term, list term -> term.
435 apply-aux Aux [] Aux.
436 apply-aux Aux [T|L] Aux1 :- !, std.do![
437 std.assert! (eqb-for T T EQB ; reali T EQB) "WTF",
438 apply-aux {coq.mk-app Aux [T, EQB]} L Aux1
439 ].
440
441 func apply-functor term, list bool, list term, (pred i:term, o:term) -> term.
442 apply-functor X _ [] _ X.
443 apply-functor X [ff,tt|Mask] [Y|YS] Rec R :- Rec Y Y1, !, apply-functor {coq.mk-app X [_,_,_,Y1]} Mask YS Rec R.
444 apply-functor X [ff|Mask] [Y|YS] Rec R :- reali Y PY, apply-functor {coq.mk-app X [Y,PY]} Mask YS Rec R.
445
446 %---------------------------------------------------------------------------
447
448 func mk-reali term -> term.
449 mk-reali T R :- reali T R, !.
450 mk-reali T _ :-
451 Msg is "derive.eqbcorrect: no unary parametricity translation for " ^ {coq.term->string T} ^ ", use derive.param1 first",
452 stop Msg.
453
454 func mk-eqb-for term -> term.
455 mk-eqb-for T R :- eqb-for T T R, !.
456 mk-eqb-for T _ :-
457 Msg is "derive.eqbcorrect: missing boolean equality for " ^ {coq.term->string T} ^ ", maybe use derive.eqb first",
458 stop Msg.
459
460 }
461