Debian Codegraph @

apps/derive/elpi/eqcorrect.elpi

raw
1 /* Correctness of comparison functions */
2 /* license: GNU Lesser General Public License Version 2.1 or later */
3 /* ------------------------------------------------------------------------- */
4
5 shorten std.{map, rev, assert!, do!, appendR}.
6
7 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8 % link param1-functor-db and eqcorrect-db
9
10 func pointfree (func term, term -> term), term, term -> term.
11
12 pointfree F A B R :-
13 coq.safe-dest-app A HDA ARGSA,
14 coq.safe-dest-app B HDB ARGSB,
15 appendR AA [_] ARGSA,
16 appendR BB [_] ARGSB,
17 coq.mk-app HDA AA LEFT,
18 coq.mk-app HDB BB RIGHT, !,
19 if (LEFT = RIGHT) (R = {{ fun _ x => x }}) (F LEFT RIGHT R).
20
21 param1-functor-db {{ lib:elpi.derive.eq_axiom_at lp:A lp:F }}
22 {{ lib:elpi.derive.eq_axiom_at lp:_ lp:F }}
23 {{ fun (x : lp:A) (px : lib:elpi.derive.eq_axiom_at lp:A lp:F x) => px }} :- !.
24
25 param1-functor-db HypTy {{ lib:elpi.derive.eq_axiom_at lp:A lp:_ }}
26 {{ fun (x : lp:A) (px : lp:(PA x)) => lp:(R x px) }} :-
27 (pi x\ coq.mk-app HypTy [x] (PA x)),
28
29 coq.safe-dest-app HypTy (global (indt KR)) Args,
30 realiR (global KGR) (global (indt KR)), !,
31
32 Msg is "derive.eqcorrect: no eqcorrect for " ^ {coq.gref->string KGR},
33 assert! (eqcorrect-db KGR OK) Msg,
34
35 (derive.eqcorrect.mk-app-eqfun OK Args OKLemma),
36 coq.typecheck OKLemma OKLemmaTy ok,
37 % we do the HO inference of P in elpi, since Coq is unable to do it
38 OKLemmaTy = (prod _ _ x\ prod _ (P x) _),
39 (pi x\ pointfree param1-functor-db {coq.mk-app HypTy [x]} (P x) Map),
40 pi x px\ coq.mk-app OKLemma [x,{coq.mk-app Map [x,px]}] (R x px).
41
42 namespace derive.eqcorrect {
43
44 :index (_ 1)
45 func mk-app-eqfun term, list term -> term.
46 mk-app-eqfun X [] X.
47 mk-app-eqfun X [Y] R :- coq.mk-app X [Y] R.
48 mk-app-eqfun X [Y,_|YS] R :-
49 eq-db Y Y F, !,
50 coq.mk-app X [Y,F] X1, mk-app-eqfun X1 YS R.
51
52
53 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
54 %
55 func branch term, term, term -> term.
56 branch (prod N T x\ prod M (P x) (B x)) (prod _ _ y\ prod _ (Q y) (Lty y)) L
57 (fun N T x\ fun M (P x) (R x)) :- !,
58 @pi-decl `x` T x\
59 @pi-decl `px` (P x) px\
60 sigma Proof ProofXPX\
61 (pointfree param1-functor-db (P x) (Q x) Proof,
62 coq.mk-app Proof [x,px] ProofXPX,
63 branch (B x px) (Lty x ProofXPX) {coq.mk-app L [x,ProofXPX]} (R x px)).
64
65 branch _ _ X X.
66
67 func branches list term, term -> list term.
68 branches [] _ [].
69 branches [Lemma | Lemmas] (prod _ S T) [P|PS] :-
70 coq.typecheck Lemma LemmaTy ok,
71 branch S LemmaTy Lemma P,
72 branches Lemmas (T P) PS.
73
74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
75 % apply the induction principle to the P and the fill in all branches
76 % using eqK lemmas
77
78 func eqK-lem list term, term -> term.
79 eqK-lem Args K Lemma :-
80 coq.safe-dest-app K (global (indc Kname)) _,
81 eqK-db Kname Lem,
82 coq.mk-app Lem Args Lemma.
83
84 func idx term, term, list term, term, list term -> term.
85 idx (sort _) IT K IndP A R :- !,
86 eq-db IT IT Cmp, !,
87 coq.mk-app IndP [ {{ lib:elpi.derive.eq_axiom_at lp:IT lp:Cmp }} ] Induction,
88 coq.typecheck Induction Inductionty ok,
89 branches {map K (eqK-lem A)} Inductionty KArgs,
90 coq.mk-app Induction KArgs R.
91
92 idx Arity IT K IndP A R :- whd1 Arity Arity1, !,
93 idx Arity1 IT K IndP A R.
94
95 idx _ _ _ _ _ _ :-
96 assert! false "derive.eqcorrect: indexed data not supported".
97
98 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
99 % Input paremeters and their equality tests
100
101 pred params i:int, i:term, i:term, i:list term, i:term, i:list term, o:term.
102 params L (prod N S T) I K IndP A R :- L > 0, !,
103 M is L - 1,
104 R = {{ fun (a : lp:S) (fa : a -> a -> bool) => lp:(Bo a fa) }},
105 @pi-decl N S a\
106 @pi-decl `fa` {{ lp:a -> lp:a -> bool }} fa\
107 (eq-db a a fa :- !) =>
108 params M (T a) {coq.mk-app I [a]}
109 {map K (x\ coq.mk-app x [a])}
110 {coq.mk-app IndP [a, {{ lib:elpi.derive.eq_axiom_at lp:a lp:fa }}]}
111 [fa,a|A] (Bo a fa).
112
113 params 0 Arity T K IndP A R :-
114 idx Arity T K IndP {rev A} R.
115
116 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117
118 func main inductive, string -> list prop.
119 main GR Name [C] :- do! [
120 T = global (indt GR),
121 assert! (induction-db GR IndP) "derive.eqcorrect: use derive.induction first",
122 coq.env.indt GR Ind Lno _ Arity K _KT,
123 assert! (Ind = tt) "derive.eqcorrect: co-inductive types not supported",
124
125 % derive and define the lemma
126 params Lno Arity T {std.map K (k\r\ r = global (indc k))} IndP [] CmpOK,
127 std.assert-ok! (coq.typecheck CmpOK CmpTy) "derive.eqcorrect generates illtyped term",
128 coq.env.add-const Name CmpOK CmpTy @opaque! Thm,
129
130 % add a clause to the db
131 C = (eqcorrect-db (indt GR) (global (const Thm)) :- !),
132 coq.elpi.accumulate _ "derive.eqcorrect.db"
133 (clause _ (before "eqcorrect-db:fail") C)
134 ].
135
136 }