| 1 |
/* param1 holds on the full type */ |
| 2 |
/* license: GNU Lesser General Public License Version 2.1 or later */ |
| 3 |
/* ------------------------------------------------------------------------- */ |
| 4 |
|
| 5 |
shorten std.{zip, assert!, do!, map, map2, rev}. |
| 6 |
|
| 7 |
namespace derive.param1.inhab { |
| 8 |
|
| 9 |
% local data base to map a constructor K of T (applied to params) to |
| 10 |
% the pair isK and its type, eg ({{Zero}} `-> pr {{isZero}} {{isNat Zero}})) |
| 11 |
func (`->) term -> pair term term. |
| 12 |
|
| 13 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 14 |
func search term, term -> term. % given T and isT it builds a proof forall x:T, isT x |
| 15 |
|
| 16 |
search _ Goal P :- |
| 17 |
std.assert! (param1-inhab-db Goal P) "derive.param1_inhab: cannot prove inhabitation". |
| 18 |
|
| 19 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 20 |
func prove-args % for each (K x ..) we must produce (isK x x_isX ..) |
| 21 |
list term, % the variables (x in the example above) |
| 22 |
term % the type of isK |
| 23 |
-> list term. % x and x_isX |
| 24 |
prove-args [] _ []. |
| 25 |
prove-args [V|VS] (prod _ T x\ prod _ _ (px\ F x px)) [V,PV | RS] :- |
| 26 |
reali T TR, !, % out of the type of x we get isX |
| 27 |
search T TR PT, % PT is a proof that forall x, isX x |
| 28 |
PV = app[PT,V], % PV is the x_isX above |
| 29 |
(reali V PV :- !) => prove-args VS (F V PV) RS. |
| 30 |
|
| 31 |
func oty (term -> term), term, list term, list term -> term. |
| 32 |
oty F _ V _ R :- rev V [X|_], R = F X. |
| 33 |
|
| 34 |
func body term, term, list term, list term -> term. |
| 35 |
body K _ V _ R :- |
| 36 |
coq.safe-dest-app K Kname _, |
| 37 |
Kname `-> (pr KR KRT), |
| 38 |
prove-args V KRT Args, |
| 39 |
coq.mk-app KR Args R. |
| 40 |
|
| 41 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 42 |
func body-params |
| 43 |
int, % number of params left |
| 44 |
term, % inductive type applied to parameters treated so far |
| 45 |
term, % inductive type arity to process |
| 46 |
list term, % construcors applied to parameters treated so far |
| 47 |
list term % construcor's types instantiated to parameters treated so far |
| 48 |
-> term. |
| 49 |
|
| 50 |
body-params 0 IsT (prod _ T _\ sort _) K KT R :- coq.safe-dest-app T (global (indt GR)) _, coq.env.recursive? GR, !, |
| 51 |
RT = (x\ {coq.mk-app IsT [x]}), |
| 52 |
R = {{ fix IH (x : lp:T) {struct x} : lp:(RT x) := lp:(Bo IH x) }}, |
| 53 |
coq.env.indt GR _ _ _ _ KX _, |
| 54 |
map2 KX {zip K KT} (a\b\r\ r = (global (indc a)) `-> b) K2KR, |
| 55 |
K2KR =!=> % K `-> (pr isK isKtype) |
| 56 |
@pi-decl `IH` (prod `x` T x\ RT x) f\ |
| 57 |
@pi-decl `x` T x\ |
| 58 |
param1-inhab-db IsT f => |
| 59 |
coq.build-match x T |
| 60 |
(oty RT) |
| 61 |
body |
| 62 |
(Bo f x). |
| 63 |
body-params 0 IsT (prod _ T _\ sort _) K KT R :- !, |
| 64 |
RT = (x\ {coq.mk-app IsT [x]}), |
| 65 |
R = {{ fun (x : lp:T) => lp:(Bo x) }}, |
| 66 |
coq.safe-dest-app T (global (indt GR)) _, |
| 67 |
coq.env.indt GR _ _ _ _ KX _, |
| 68 |
map2 KX {zip K KT} (a\b\r\ r = (global (indc a)) `-> b) K2KR, |
| 69 |
K2KR =!=> % K `-> (pr isK isKtype) |
| 70 |
@pi-decl `x` T x\ |
| 71 |
coq.build-match x T (oty RT) body (Bo x). |
| 72 |
|
| 73 |
% param1 have the form : isT A (P : A -> Type) .. , so we process two |
| 74 |
% binders at a time and we assume (H : trivial P) for each A and P |
| 75 |
body-params N IsT (prod A TA a\ prod P (TP a) (F a)) K KT R :- N > 0, coq.sort? TA, !, |
| 76 |
M is N - 2, |
| 77 |
R = (fun A TA a\ |
| 78 |
fun P (TP a) p\ |
| 79 |
fun _ {{ lib:elpi.derive.full lp:a lp:p }} pf\ Bo a p pf), |
| 80 |
@pi-decl A TA a\ |
| 81 |
@pi-decl P (TP a) p\ |
| 82 |
@pi-decl _ {{ lib:elpi.derive.full lp:a lp:p }} pf\ |
| 83 |
sigma KAP KTAP\ |
| 84 |
map K (k\ coq.mk-app k [a,p]) KAP, |
| 85 |
map KT (coq.subst-prod [a,p]) KTAP, |
| 86 |
(reali a p :- !) => |
| 87 |
param1-inhab-db p pf => % to prove (P x) use (H x) |
| 88 |
body-params M {coq.mk-app IsT [a,p]} (F a p) KAP KTAP (Bo a p pf). |
| 89 |
body-params N IsT (prod A TA a\ prod P (TP a) (F a)) K KT R :- N > 0, !, |
| 90 |
M is N - 2, |
| 91 |
R = (fun A TA a\ |
| 92 |
fun P (TP a) p\ |
| 93 |
Bo a p), |
| 94 |
@pi-decl A TA a\ |
| 95 |
@pi-decl P (TP a) p\ |
| 96 |
sigma KAP KTAP\ |
| 97 |
map K (k\ coq.mk-app k [a,p]) KAP, |
| 98 |
map KT (coq.subst-prod [a,p]) KTAP, |
| 99 |
(reali a p :- !) => |
| 100 |
body-params M {coq.mk-app IsT [a,p]} (F a p) KAP KTAP (Bo a p). |
| 101 |
|
| 102 |
body-params _ IsT _ _ _ _ :- |
| 103 |
M is "derive.param1_inhab: wrong shape " ^ {coq.term->string IsT} ^ |
| 104 |
". It does not look like a unary parametricity translation of an inductive with no indexes.", |
| 105 |
stop M. |
| 106 |
|
| 107 |
func main gref, gref, string -> list prop. |
| 108 |
main _ (indt GR) Suffix [ClauseW, param1-inhab-done (indt GR)] :- do! [ |
| 109 |
|
| 110 |
coq.env.indt GR Ind Lno Luno Arity K KT, |
| 111 |
assert! (Ind = tt) "derive.param1_inhab: Coinductive types are not supported", |
| 112 |
assert! (Lno = Luno) "derive.param1_inhab: Non-uniform parameters not supported", |
| 113 |
body-params Lno (global (indt GR)) Arity {std.map K (k\r\ r = global (indc k))} KT RSkel, |
| 114 |
% coq.say {coq.term->string R}, |
| 115 |
std.assert-ok! (coq.elaborate-skeleton RSkel RT R) "derive.param1_inhab generates illtyped term", |
| 116 |
|
| 117 |
Name is {coq.gref->id (indt GR)} ^ Suffix, |
| 118 |
coq.env.add-const Name R RT @transparent! Witness, |
| 119 |
|
| 120 |
ClauseW = (param1-inhab-db (global (indt GR)) (global (const Witness))), |
| 121 |
coq.elpi.accumulate _ "derive.param1.trivial.db" (clause _ _ ClauseW), |
| 122 |
coq.elpi.accumulate _ "derive.param1.trivial.db" (clause _ _ (param1-inhab-done (indt GR))), |
| 123 |
]. |
| 124 |
|
| 125 |
main (const GR) (const IsGR) Suffix [ClauseW, param1-inhab-done (const IsGR)] :- do! [ |
| 126 |
coq.env.const GR (some T) _, |
| 127 |
coq.env.const IsGR (some IsT) _, |
| 128 |
search T IsT P, |
| 129 |
std.assert-ok! (coq.elaborate-skeleton P RT R) "derive.param1_inhab generates illtyped term", |
| 130 |
Name is {coq.gref->id (const IsGR)} ^ Suffix, |
| 131 |
coq.env.add-const Name R RT @transparent! Witness, |
| 132 |
ClauseW = (param1-inhab-db (global (const IsGR)) (global (const Witness))), |
| 133 |
coq.elpi.accumulate _ "derive.param1.trivial.db" (clause _ _ ClauseW), |
| 134 |
coq.elpi.accumulate _ "derive.param1.trivial.db" (clause _ _ (param1-inhab-done (const IsGR))), |
| 135 |
|
| 136 |
]. |
| 137 |
|
| 138 |
} |
| 139 |
|