| 1 |
/* param1 holds on the full type */ |
| 2 |
/* license: GNU Lesser General Public License Version 2.1 or later */ |
| 3 |
/* ------------------------------------------------------------------------- */ |
| 4 |
|
| 5 |
shorten std.{assert!, do!, drop-last}. |
| 6 |
|
| 7 |
namespace derive.param1.trivial { |
| 8 |
|
| 9 |
func prove-args list term, list term -> list term. |
| 10 |
prove-args [] [] []. |
| 11 |
prove-args [X,PX|Rest] [TX,TPX_X|TRest] [X,P1,PX,P2|PRest] :- |
| 12 |
coq.safe-dest-app TPX_X HD ARGS, |
| 13 |
drop-last 1 ARGS ARG, |
| 14 |
coq.mk-app HD ARG TPX, |
| 15 |
param1-trivial-db TPX P, !, |
| 16 |
P1 = {{ lib:elpi.derive.trivial_full lp:TX lp:TPX lp:P lp:X }}, |
| 17 |
P2 = {{ lib:elpi.derive.trivial_uniq lp:TX lp:TPX lp:P lp:X lp:PX }}, |
| 18 |
prove-args Rest TRest PRest. |
| 19 |
|
| 20 |
func oty term, term, term, list term, list term -> term. |
| 21 |
oty IsT W _ [Idx,V] _ {{ lib:@elpi.eq lp:Ty lp:Wx lp:V }} :- |
| 22 |
Ty = app[IsT,Idx], Wx = app[W,Idx]. |
| 23 |
|
| 24 |
func body term, term, term, term, list term, list term -> term. |
| 25 |
body _ _ K (app _) V VT R :- std.do! [ |
| 26 |
/* |
| 27 |
coq.safe-dest-app K (global (indc Kname)) Params, |
| 28 |
|
| 29 |
prove-args V VT P, |
| 30 |
assert! (param1-congr-db Kname CongrK) "run derive.param1.congr first", |
| 31 |
coq.mk-app CongrK Params Rhd, |
| 32 |
coq.mk-app Rhd P R,*/ |
| 33 |
trivialize-param1 V VT Triv, |
| 34 |
do-args {std.rev V} {std.rev VT} {std.rev Triv} [] K R, |
| 35 |
]. |
| 36 |
|
| 37 |
func trivialize-param1 list term, list term -> list term. |
| 38 |
trivialize-param1 [] [] []. |
| 39 |
trivialize-param1 [_,_|XS] [_,TPX|PS] [TPX1,T|TS] :- std.do! [ |
| 40 |
coq.safe-dest-app TPX HD ARGS, |
| 41 |
drop-last 1 ARGS ARG, |
| 42 |
coq.mk-app HD ARG TPX1, |
| 43 |
param1-trivial-db TPX1 T, |
| 44 |
trivialize-param1 XS PS TS, |
| 45 |
]. |
| 46 |
|
| 47 |
func do-args list term, list term, list term, list term, term -> term. |
| 48 |
do-args [] [] [] _ _ {{ lib:@elpi.erefl _ _ }}. |
| 49 |
do-args [PX,X|XS] [_,TX|PS] [Triv,P|Trivs] Old K R :- std.do! [ |
| 50 |
Q = {{ lib:elpi.derive.trivial_uniq lp:TX lp:P lp:Triv lp:X lp:PX }}, |
| 51 |
F = {{ lib:elpi.derive.trivial_full lp:TX lp:P lp:Triv lp:X }}, |
| 52 |
std.assert-ok! (coq.typecheck Q TQ) "wtf", |
| 53 |
(pi v\ |
| 54 |
coq.mk-app K {std.rev {std.append {std.append Old [v,X]} XS}} (K1 v)), |
| 55 |
std.append Old [F,X] Old1, |
| 56 |
coq.build-match Q TQ (do-oty K1 PX) (do-body XS PS Trivs Old1 K) R, |
| 57 |
]. |
| 58 |
|
| 59 |
func do-oty (term -> term), term, term, list term, list term -> term. |
| 60 |
do-oty K E _ [V,_] _ {{ lib:@elpi.eq _ _ lp:KV }} :- (copy E V :- !) => copy (K V) KV. |
| 61 |
|
| 62 |
func do-body list term, list term, list term, list term, term, term, term, list term, list term -> term. |
| 63 |
do-body XS PS Trivs Old K _ _ _ _ R :- do-args XS PS Trivs Old K R. |
| 64 |
|
| 65 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 66 |
func body-params |
| 67 |
int, % number of params left |
| 68 |
term, % inductive type applied to parameters treated so far |
| 69 |
term % inductive type arity to process |
| 70 |
-> term. |
| 71 |
|
| 72 |
body-params 0 IsT (prod _ T _\ sort _) R :- !, std.do! [ |
| 73 |
std.assert! (param1-inhab-db IsT W) "www", |
| 74 |
R = {{ fun x : lp:T => |
| 75 |
lib:elpi.derive.contracts lp:T lp:IsT x (lp:W x) |
| 76 |
((fix IH (x : lp:T) (y : lp:IsT x) {struct y} : |
| 77 |
lib:@elpi.eq (lp:IsT x) (lp:W x) y := lp:{{ { |
| 78 |
|
| 79 |
param1-trivial-db IsT {{ fun x : lp:T => |
| 80 |
lib:elpi.derive.contracts lp:T lp:IsT x (lp:W x) (IH x) }} => |
| 81 |
coq.build-match {{y}} {{lp:IsT x}} (oty IsT W) (body IsT W) |
| 82 |
} }} |
| 83 |
) x) }}, |
| 84 |
]. |
| 85 |
|
| 86 |
% param1 have the form : isT A (P : A -> Type) .. , so we process two |
| 87 |
% binders at a time and we assume (H : trivial P) for each A and P |
| 88 |
body-params N T (prod A TA a\ prod P (TP a) (F a)) R :- N > 0, coq.sort? TA, !, |
| 89 |
M is N - 2, |
| 90 |
R = (fun A TA a\ |
| 91 |
fun P (TP a) p\ |
| 92 |
fun _ {{ lib:elpi.derive.trivial lp:a lp:p }} pf\ { |
| 93 |
decl a A TA => |
| 94 |
decl p P (TP a) => |
| 95 |
decl pf `_` {{ lib:elpi.derive.trivial lp:a lp:p }} => |
| 96 |
(reali a p :- !) => |
| 97 |
param1-trivial-db p pf => % to prove (P x) use (H x) |
| 98 |
param1-inhab-db p {{ lib:elpi.derive.trivial_full lp:a lp:p lp:pf }} => % to prove (P x) use (H x) |
| 99 |
body-params M {coq.mk-app T [a,p]} |
| 100 |
(F a p)}). |
| 101 |
body-params N T (prod A TA a\ prod P (TP a) (F a)) R :- N > 0, !, |
| 102 |
M is N - 2, |
| 103 |
R = (fun A TA a\ |
| 104 |
fun P (TP a) p\ { |
| 105 |
decl a A TA => |
| 106 |
decl p P (TP a) => |
| 107 |
(reali a p :- !) => |
| 108 |
body-params M {coq.mk-app T [a,p]} (F a p)}). |
| 109 |
|
| 110 |
body-params _ T _ _ :- |
| 111 |
coq.say "derive.param1_trivial: wrong shape " {coq.term->string T} |
| 112 |
". It does not look like a unary parametricity translation of an inductive with no indexes.", |
| 113 |
fail. |
| 114 |
|
| 115 |
func main gref, gref, string -> list prop. |
| 116 |
main _ (indt GR) Suffix [Clause,param1-trivial-done (indt GR)] :- do! [ |
| 117 |
coq.env.indt GR _ Lno _ Arity _ _, |
| 118 |
body-params Lno (global (indt GR)) Arity R, |
| 119 |
% coq.say {coq.term->string R}, |
| 120 |
std.assert-ok! (coq.typecheck R RT) "derive.param1_trivial generates illtyped term", |
| 121 |
|
| 122 |
Name is {coq.gref->id (indt GR)} ^ Suffix, |
| 123 |
coq.env.add-const Name R RT @transparent! Cst, |
| 124 |
Clause = (param1-trivial-db (global (indt GR)) (global (const Cst))), |
| 125 |
coq.elpi.accumulate _ "derive.param1.trivial.db" (clause _ _ Clause), |
| 126 |
coq.elpi.accumulate _ "derive.param1.trivial.db" (clause _ _ (param1-trivial-done (indt GR))), |
| 127 |
]. |
| 128 |
main (const _GR) (const IsGR) Suffix [Clause,param1-trivial-done (const IsGR)] :- do! [ |
| 129 |
coq.env.const IsGR (some IsT) _, |
| 130 |
Name is {coq.gref->id (const IsGR)} ^ Suffix, |
| 131 |
|
| 132 |
std.assert! (param1-trivial-db IsT R) "derive.param1_trivial not already generated", |
| 133 |
|
| 134 |
std.assert-ok! (coq.typecheck R RT) "derive.param1_trivial generates illtyped term", |
| 135 |
coq.env.add-const Name R RT @transparent! Cst, |
| 136 |
Clause = (param1-trivial-db (global (const IsGR)) (global (const Cst))), |
| 137 |
coq.elpi.accumulate _ "derive.param1.trivial.db" (clause _ _ Clause), |
| 138 |
coq.elpi.accumulate _ "derive.param1.trivial.db" (clause _ _ (param1-trivial-done (const IsGR))), |
| 139 |
|
| 140 |
]. |
| 141 |
|
| 142 |
} |