Debian Codegraph @

apps/derive/elpi/param1.elpi

raw
1 /* Unary parametricity translation (Realizability) */
2 /* license: GNU Lesser General Public License Version 2.1 or later */
3 /* ------------------------------------------------------------------------- */
4
5 % Author: Cyril Cohen
6
7 shorten std.{forall, forall2, do!, rev, map2, map, append}.
8
9 :before "subst-fun:fail"
10 coq.subst-fun XS T TXS :- !, coq.mk-app T XS TXS.
11
12 % this is outside the namespace since the predicate is also the db-one
13 reali (sort prop as P) (fun `s` P x\ prod `s1` x _\ P) :- !.
14 reali (sort _) (fun `s` (sort (typ U)) x\ prod `s1` x _\ (sort (typ V))) :- !,
15 coq.univ.new U, coq.univ.new V.
16
17 reali (fun N T B) (fun N T x\ fun N1 (TRsubst x) xR\ BR x xR) :- !, do! [
18 coq.name-suffix `P` N N1,
19 reali T TR,
20 (pi x xR\ reali x xR => reali (B x) (BR x xR)),
21 (TRsubst = x\ {coq.subst-fun [x] TR})
22 ].
23
24 reali (prod N T P as Prod) ProdR :- !, do! [
25 coq.name-suffix `P` N N1,
26 reali T TR,
27 (pi x xR\ reali x xR => reali (P x) (PR x xR)),
28 ProdR = fun `f` Prod f\
29 prod N T x\ prod N1 {coq.subst-fun [x] TR} xR\
30 {coq.subst-fun [{coq.mk-app f [x]}] (PR x xR)}
31 ].
32
33 reali (app [A|Bs]) ARBsR :- !, do! [
34 reali A AR,
35 derive.param1.reali-args Bs BsR,
36 coq.mk-app AR BsR ARBsR
37 ].
38
39 reali (let N T V B) LetR :- !, std.do! [
40 coq.name-suffix `P` N N1,
41 reali T TR,
42 reali V VR,
43 (pi x xR\ reali x xR => reali (B x) (BR x xR)),
44 LetR = let N T V x\ let N1 {coq.mk-app TR [x]} VR xR\ BR x xR
45 ].
46
47 reali (match T P Bs) MR :- !, do! [
48 reali T TR,
49 derive.param1.reali-match P PRM,
50 reali T TR => derive.param1.reali-map Bs BsR,
51 MR = match TR (PRM (x\ match x P Bs)) BsR
52 ].
53
54 reali (fix N Rno T F as Fix) FixR :- !, std.do! [
55 RnoR is 2 * Rno + 1,
56 RnoR1 is RnoR + 1,
57 reali T TR,
58 (pi x xR\ reali x xR => reali (F x) (FR x xR)),
59 (TRsubst = f\ {coq.subst-fun [f] TR}),
60 (pi f xR\ FixBody f xR = let N (TRsubst (F f)) (FR f xR) fr\
61 {paramX.mk-trivial-match RnoR (TRsubst f) [] fr}),
62 (pi f xR\ coq.mk-eta RnoR1 (TRsubst f) (FixBody f xR) (EtaFixBody f xR)),
63 coq.name-suffix N 1 N1,
64 FixR = (let N T Fix f\ fix N1 RnoR (TRsubst f) xR\ EtaFixBody f xR),
65 ].
66
67 namespace derive.param1 {
68
69 pred reali-args o:list term, o:list term.
70 reali-args [] [].
71 reali-args [X|Xs] [X,XR|XsR] :- do! [
72 reali X XR,
73 reali-args Xs XsR
74 ].
75
76 pred reali-map o:list term, o:list term.
77 reali-map [] [].
78 reali-map [X|Xs] [XR|XsR] :- do! [
79 reali X XR,
80 reali-map Xs XsR
81 ].
82
83 % helpers for match return type
84 pred reali-match i:term, o:((term -> term) -> term).
85 reali-match (fun N T B) PRM :- pi x\ not (B x = fun _ _ _), !, do! [
86 reali T TR,
87 (pi x xR\ reali x xR => reali (B x) (BR x xR)),
88 coq.name-suffix `P` N N1,
89 (pi z z1\ PRM z =
90 fun N T x\ fun N1 {coq.subst-fun [x] TR} xR\ {coq.mk-app (BR x xR) [z x]})
91 ].
92
93 reali-match (fun N T B) PRM :- do! [
94 reali T TR,
95 (pi x xR\ (reali x xR ==> reali-match (B x) (BR x xR))),
96 coq.name-suffix N 1 N1,
97 (pi z \ PRM z = fun N T x\ fun N1 {coq.subst-fun [x] TR} xR\ BR x xR z)
98 ].
99
100 % Storage: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
101
102 func reali-store
103 string, % Name suffix for the type class
104 term, % Term
105 term -> . % Translation
106 reali-store N X XR :- !,
107 Nreali is "reali_" ^ N,
108 Args = [_, _, X, XR],
109 T1 = app [{{ lib:@param1.store_reali }}|Args],
110 std.assert-ok! (coq.typecheck T1 T2) "reali-store: T1 illtyped",
111 coq.ensure-fresh-global-id Nreali FNreali,
112 coq.env.add-const FNreali T1 T2 _ GR,
113 @global! => coq.TC.declare-instance (const GR) 0.
114
115 pred reali-store-indc i:string, i:constructor, i:constructor.
116 reali-store-indc Prefix K XR :-
117 reali-store {calc (Prefix ^ {coq.gref->id (indc K)})} (global (indc K)) (global (indc XR)).
118
119 kind raw-mindt-spec type.
120 type raw-mindt-spec inductive -> id -> bool -> term -> list constructor -> list (pair constructor id) -> list term -> raw-mindt-spec.
121
122 func build-raw-mutual-spec string, inductive -> raw-mindt-spec.
123 build-raw-mutual-spec Prefix GR (raw-mindt-spec GR FNewName IsInd Ty KNames KNamesR KTypes) :-
124 coq.env.indt GR IsInd _ _ Ty KNames KTypes,
125 coq.gref->id (indt GR) ID,
126 std.map KNames (prefix-indc Prefix) KNamesR,
127 NewName is Prefix ^ ID,
128 coq.ensure-fresh-global-id NewName FNewName.
129
130 func translate-mutual-spec raw-mindt-spec, inductive -> coq.indt-spec.
131 translate-mutual-spec (raw-mindt-spec GR FNewName IsInd Ty KNames KNamesR KTypes) NewGR
132 (coq.indt-spec NewGR FNewName IsInd TyOut KNamesR KTypesR) :-
133 Ind = global (indt GR),
134 reali Ty TyR,
135 coq.mk-app TyR [Ind] TyOut,
136 map2 KNames KTypes (k\ ty\ r\ sigma tyr\
137 reali ty tyr,
138 coq.mk-app tyr [global (indc k)] r) KTypesR.
139
140 func main-mblock list raw-mindt-spec, list raw-mindt-spec, list inductive -> list prop.
141 main-mblock All [] Acc Clauses :- !,
142 rev Acc NewGRs,
143 main-mblock-aux All NewGRs Clauses.
144 main-mblock All [raw-mindt-spec GR _ _ _ _ _ _|Rest] Acc Clauses :-
145 % a fresh name for each inductive
146 pi newGR\ (reali (global (indt GR)) (global (indt newGR)) :- !) => main-mblock All Rest [newGR|Acc] Clauses.
147
148 func reali-store-mutual-indt list raw-mindt-spec, list inductive -> .
149 reali-store-mutual-indt [] [].
150 reali-store-mutual-indt [raw-mindt-spec GR FNewName _ _ _ _ _|MoreInd] [RealGR|MoreGR] :-
151 reali-store FNewName (global (indt GR)) (global (indt RealGR)),
152 reali-store-mutual-indt MoreInd MoreGR.
153
154 func reali-store-mutual-indcs string, list constructor, list constructor -> .
155 reali-store-mutual-indcs _ [] [].
156 reali-store-mutual-indcs Prefix [K|KS] [KR|KRS] :-
157 N is Prefix ^ {coq.gref->id (indc K)},
158 reali-store N (global (indc K)) (global (indc KR)),
159 reali-store-mutual-indcs Prefix KS KRS.
160
161 func reali-store-mutual-indc list raw-mindt-spec, list inductive -> .
162 reali-store-mutual-indc [] [].
163 reali-store-mutual-indc [raw-mindt-spec _ FNewName _ _ KNames _ _|Raws] [RealGR|RealGRs] :-
164 coq.env.indt RealGR _ _ _ _ RealKNames _,
165 Prefix1 is FNewName ^ "_",
166 reali-store-mutual-indcs Prefix1 KNames RealKNames,
167 reali-store-mutual-indc Raws RealGRs.
168
169 func build-and-accumulate-clauses list raw-mindt-spec, list inductive -> list prop.
170 build-and-accumulate-clauses [] [] [].
171 build-and-accumulate-clauses [raw-mindt-spec GR _ _ _ KNames _ _|Raws] [RealGR|RealGRs] Clauses :-
172 coq.env.indt RealGR _ _ _ _ RealKNames _,
173 map2 KNames RealKNames (a\ b\ r\ r = (reali (global (indc a)) (global (indc b)) :- !)) CK,
174 CDone = reali-done (indt GR),
175 CReali = (reali (global (indt GR)) (global (indt RealGR)) :- !),
176 CRealiR = (realiR (global (indt GR)) (global (indt RealGR)) :- !),
177 coq.elpi.accumulate _ "derive.param1.db" (clause _ _ CDone),
178 coq.elpi.accumulate _ "derive.param1.db" (clause _ (before "realiR:fail") CRealiR),
179 std.forall [CReali|CK] (x\coq.elpi.accumulate _ "derive.param1.db" (clause _ (before "reali:fail") x)),
180 build-and-accumulate-clauses Raws RealGRs Rest,
181 append[CDone, CReali, CRealiR | CK] Rest Clauses.
182
183 func main-mblock-aux list raw-mindt-spec, list inductive -> list prop.
184 main-mblock-aux Raws NewGRs Clauses :- std.do! [
185 map2 Raws NewGRs translate-mutual-spec Specs,
186 coq.build-mindt-decl Specs 0 0 DeclR,
187 std.assert-ok! (coq.typecheck-indt-decl DeclR) "derive.param1 generates illtyped mutual inductive",
188 coq.env.add-indt DeclR RealGR,
189 coq.env.indt-block RealGR _ _ _ RealGRs _ _ _,
190 reali-store-mutual-indt Raws RealGRs,
191 reali-store-mutual-indc Raws RealGRs,
192 build-and-accumulate-clauses Raws RealGRs Clauses,
193 ].
194
195 func prefix-indc string, constructor -> pair constructor id.
196 prefix-indc Prefix K (pr K FNewName) :-
197 coq.gref->id (indc K) Name,
198 NewName is Prefix ^ Name,
199 coq.ensure-fresh-global-id NewName FNewName.
200
201 % toplevel predicates %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
202
203 :index (1)
204 func dispatch
205 gref, % input of the translation
206 string % the name
207 -> list prop. % the clause
208
209 dispatch (const GR) Prefix Clauses :- !, do! [
210 Term = global (const GR),
211 Name is Prefix ^ {coq.gref->id (const GR)},
212 std.assert! (coq.env.const GR (some V) Ty) "param1: cannot handle axioms",
213
214 reali V VR,
215 reali Ty TyR,
216 coq.mk-app TyR [Term] TyRV,
217 % coq.typecheck is needed to add universe constraints
218 std.assert-ok! (coq.typecheck TyRV _) "param1: illtyped param type",
219 % apparently calling the type checker with the expected type is weaker in this case
220 std.assert-ok! (coq.typecheck VR VRTy) "param1: illtyped constant",
221 std.assert-ok! (coq.unify-leq VRTy TyRV) "param1: constant does not have the right type",
222
223 coq.ensure-fresh-global-id Name FName,
224 coq.env.add-const FName VR TyRV _ TermR,
225
226 reali-store Name Term (global (const TermR)),
227
228 C1 = (reali Term (global (const TermR)) :- !),
229 coq.elpi.accumulate _ "derive.param1.db" (clause _ (before "reali:fail") C1),
230 C2 = (realiR Term (global (const TermR)) :- !),
231 coq.elpi.accumulate _ "derive.param1.db" (clause _ (before "realiR:fail") C2),
232 coq.elpi.accumulate _ "derive.param1.db" (clause _ _ (reali-done (const GR))),
233 Clauses = [C1, C2, reali-done (const GR)]
234 ].
235
236 dispatch (indt GR) Prefix Clauses :- coq.env.indt-block GR _ Lno Luno GRS _ _ _, std.length GRS N, N > 1, !, do! [
237 map GRS (build-raw-mutual-spec Prefix) Raws,
238 std.assert! (Lno = Luno) "derive.param1: Non-uniform parameters not supported",
239 main-mblock Raws Raws [] Clauses
240 ].
241
242 dispatch (indt GR) Prefix Clauses :- !, do! [
243 Ind = global (indt GR),
244 coq.env.indt GR _ _ Lno Ty Knames Ktypes,
245
246 LnoR is 2 * Lno,
247
248 pi new_name\ sigma KnamesR KtypesR TyR\ (
249 (reali Ind (global (indt new_name)) ==>
250 reali Ty TyR,
251 map2 Knames Ktypes (k\ ty\ r\ sigma tyr\
252 reali ty tyr, coq.subst-fun [global (indc k)] tyr r)
253 KtypesR),
254 map Knames (prefix-indc Prefix) KnamesR,
255
256 NewName is Prefix ^ {coq.gref->id (indt GR)},
257
258 coq.ensure-fresh-global-id NewName FNewName,
259 coq.build-indt-decl (coq.indt-spec new_name FNewName tt {coq.subst-fun [Ind] TyR} KnamesR KtypesR) LnoR LnoR DeclR
260 ),
261
262 std.assert-ok! (coq.typecheck-indt-decl DeclR) "derive.param1 generates illtyped inductive",
263
264 coq.env.add-indt DeclR GRR,
265
266 reali-store NewName Ind (global (indt GRR)),
267 coq.env.indt GRR _ _ _ _ RealNamesR _,
268 Prefix1 is NewName ^ "_",
269 forall2 Knames RealNamesR (reali-store-indc Prefix1),
270 C1 = (reali Ind (global (indt GRR)) :- !),
271 coq.elpi.accumulate _ "derive.param1.db" (clause _ (before "reali:fail") C1),
272 C2 = (realiR Ind (global (indt GRR)) :- !),
273 coq.elpi.accumulate _ "derive.param1.db" (clause _ (before "realiR:fail") C2),
274 map2 Knames RealNamesR (a\ b\ r\ r = (reali (global (indc a)) (global (indc b)) :- !)) CK,
275 forall CK (c\
276 coq.elpi.accumulate _ "derive.param1.db" (clause _ (before "reali:fail") c)),
277 coq.elpi.accumulate _ "derive.param1.db" (clause _ _ (reali-done (indt GR))),
278 Clauses = [reali-done (indt GR), C1,C2|CK]
279 ].
280
281 dispatch (indc _) _ _ :-
282 coq.error "derive.param1: cannot translate a constructor".
283
284 func main gref, string -> list prop.
285 main T _ Clauses :-
286 dispatch T "is_" Clauses.
287
288 }
289