Debian Codegraph @

apps/tc/elpi/ho_compile.elpi

raw
1 namespace tc {
2 shorten tc.{r-ar, range-arity}.
3
4 namespace compile {
5
6 namespace instance {
7 func is-name term ->.
8 func is-uvar term ->.
9
10 % [name-pair H0 Hn Ar]
11 % the variable H0 (applied 0 time) is associated to the variable Hn which is
12 % applied Ar times.
13 pred name-pair o:term, o:term, o:nat.
14
15 namespace decompile {
16
17 func decompile-term-aux term, pair (list term) (list prop) -> term, pair (list term) (list prop).
18
19 decompile-term-aux X A Y A :- name X, !, X = Y, !. % avoid loading "decompile-term-aux x A x A" at binders
20 decompile-term-aux (global _ as C) A C A :- !.
21 decompile-term-aux (pglobal _ _ as T) L T' L :- !, copy T T', !.
22 decompile-term-aux (sort _ as T) L T' L :- !, copy T T', !.
23 decompile-term-aux (uvar as X) L X L :- !.
24 decompile-term-aux (primitive _ as P) L P L :- !.
25
26 decompile-term-aux (tc.maybe-eta-tm T S) (pr [X|XS] L1) Y (pr XS' [NL | L2]) :- !,
27 name Y X S,
28 decompile-term-aux T (pr XS L1) T' (pr XS' L2),
29 NL = tc.link.eta Y T'.
30
31 decompile-term-aux (tc.prod-range T _) A T' A' :- !,
32 decompile-term-aux T A T' A'.
33
34 % Maybe-llam when H is a coq unif variable quantified in the instance type
35 % In the following instance, X is a HO variable applied to a constant (not a name)
36 % Instance i : forall (X : T1 -> T2) (a : T1), c (X a).
37 decompile-term-aux (tc.maybe-llam-tm (app[app[H | PF] | NPF]) S) (pr [X|XS] L1) Y (pr XS' [NL | L2]) :-
38 not (var H), !, % is-uvar H, holds
39 name Y X S,
40 length-nat PF Len,
41 std.assert!(name-pair H V Len) "[TC] fail to find name-pair",
42 name Hd V PF,
43 std.fold-map NPF (pr XS L1) decompile-term-aux Tl (pr XS' L2),
44 NL = tc.link.llam Y (app [Hd|Tl]).
45
46 % Maybe-llam when H is a hole appearing in the shelved goals
47 % This happens when the instance to be compiled comes from the context
48 % Example: Goal exists (X : T1 -> T2), forall a, c (X a) -> ...
49 % intros; eexists. (* In the context we have the instance `H: c (?X a)` *)
50 decompile-term-aux (tc.maybe-llam-tm (app[app[H | PF] | NPF]) S) A Z (pr XS' [NL | L3]) :- !,
51 var H _ Scope, !,
52 std.append Scope S S',
53 prune Z S',
54 tc.compile.goal.make-pairs [T] Pairs,
55 % We build on the fly the eta-links for T
56 (Pairs =>
57 (tc.compile.goal.build-eta-links-of-vars [T] P,
58 tc.compile.goal.get-uva-pair-arity T PF Y)),
59 std.fold-map NPF A decompile-term-aux Tl (pr XS' L2),
60 std.append P L2 L3,
61 NL = tc.link.llam Z (app [Y|Tl]).
62
63 decompile-term-aux (fun Name Ty Bo) (pr XS L) (fun Name Ty' Bo') (pr XS2 L3) :- !,
64 (pi x\ is-name x => decompile-term-aux (Bo x) (pr XS []) (Bo' x) (pr XS1 (L1x x))),
65 close-term-no-prune-ty L1x Ty L1,
66 decompile-term-aux Ty (pr XS1 L) Ty' (pr XS2 L2),
67 std.append L1 L2 L3.
68
69 decompile-term-aux (prod Name Ty Bo) (pr XS L) (prod Name Ty' Bo') (pr XS2 L3) :- !,
70 (pi x\ is-name x => decompile-term-aux (Bo x) (pr XS []) (Bo' x) (pr XS1 (L1x x))),
71 close-term-no-prune-ty L1x Ty L1,
72 decompile-term-aux Ty (pr XS1 L) Ty' (pr XS2 L2),
73 std.append L1 L2 L3.
74
75 % HO var when H is a quantified variable in the instance type being in PF
76 % Example: Instance i: forall (X : T1 -> T2), (forall a, c1 (X a)) -> c2.
77 % Note: X is the HO var taken into account which is applied to the list of
78 % distinct_names [a]
79 decompile-term-aux (app [H|L]) N R N :-
80 is-uvar H,
81 std.forall L is-name, % Not needed, since precompile does this check
82 distinct_names L, !, % Not needed, since precompile does this check
83 length-nat L Len,
84 std.assert! (name-pair H V Len) "[TC] name-pair not found",
85 name R V L.
86
87 % HO var when H is a hole appearing in the shelved goals
88 decompile-term-aux (app [T|L]) (pr A B) Z (pr A B') :-
89 var T _ Scope,
90 std.forall L is-name, % Not needed, since decompile for llam leaves only PF
91 distinct_names L, !, % Not needed, since decompile for llam leaves only PF
92 std.append Scope L Scope',
93 prune Z Scope',
94 tc.compile.goal.make-pairs [T] Pairs,
95 % We build on the fly the eta-links for `T`
96 (Pairs =>
97 (tc.compile.goal.build-eta-links-of-vars [T] P,
98 tc.compile.goal.get-uva-pair-arity T L Y)),
99 var Z Y Scope',
100 std.append P B B'.
101
102 decompile-term-aux (app L) PR (app L') PR' :- !,
103 std.fold-map L PR decompile-term-aux L' PR'.
104
105 decompile-term-aux (let N T Ty Bo) PR (let N T' Ty' Bo') PR3 :- !,
106 decompile-term-aux T PR T' PR1,
107 decompile-term-aux Ty PR1 Ty' PR2,
108 (pi x\ is-name x => decompile-term-aux (Bo x) PR2 (Bo' x) PR3).
109
110 decompile-term-aux A B _ _ :- coq.error "[TC] cannot decompile-term-aux of" A B.
111
112 :functional
113 pred decompile-term i:list term, o:list term, i:term, o:term, o:list prop.
114 decompile-term L L' T R Links :-
115 decompile-term-aux T (pr L []) R (pr L' Links).
116
117 }
118
119 % TODO: also replace (sort (typ X)) and (pglobal _ X) with holes in the place of X
120 func clean-term term -> term.
121 clean-term A B :-
122 (pi t s r \ copy (tc.maybe-eta-tm t s) r :- !, copy t r, !) =>
123 (pi t s r \ copy (tc.prod-range t s) r :- !, copy t r, !) =>
124 (pi t s r \ copy (tc.maybe-llam-tm t s) r :- !, copy t r, !) =>
125 std.assert! (copy A B) "[TC] clean-term error".
126
127 func main
128 nat, % the number of problematic terms
129 term, % the type of the instance
130 term, % the global gref of the instance
131 list univ, % the list of univ variable to be replaced with elpi fresh vars
132 list univ-instance % the list of univ-instance to be replaced with elpi fresh vars
133 -> prop. % the compiled clause for the instance
134
135 main N Ty ProofHd [] [] Clause :-
136 add-pi-problematic-terms N [] Ty ProofHd Clause.
137 main N Ty ProofHd [Univ | UnivL] UnivInstL (pi x\ Clause x) :- !,
138 pi x\ (copy (sort (typ Univ)) (sort (typ x)) :- !) =>
139 main N Ty ProofHd UnivL UnivInstL (Clause x).
140 main N Ty ProofHd [] [UnivInst | UnivInstL] (pi x\ Clause x) :- !,
141 pi x\ (copy (pglobal A UnivInst) (pglobal A x) :- !) =>
142 main N Ty ProofHd [] UnivInstL (Clause x).
143
144
145 % Start to charge the right number of pi for the resulting clause:
146 % This number is equal to the number of problematic terms + number of subterms with shape `sort _` and `pglobal _ _`
147 func add-pi-problematic-terms
148 nat, % the number of pi to quantify
149 list term, % the list of quantified pi
150 term, % the fuel of the compilation (the type of the instance)
151 term % the global gref of the current instance
152 -> prop. % the compiled clause for the instance
153
154 add-pi-problematic-terms z L Ty ProofHd Clause :-
155 compile-ty L _ ProofHd tt Ty [] [] Clause.
156 add-pi-problematic-terms (s N) L Ty ProofHd (pi x\ Clause x) :-
157 pi x\ is-uvar x => add-pi-problematic-terms N [x|L] Ty ProofHd (Clause x).
158
159 % Builds a eta link between the varibale A whose type _must_ be of type `prod`
160 % A is linked with B : A =_eta (fun (x : Ty) => B_x)
161 func make-eta-link-aux
162 term, % A : The variable to eta-expand
163 term, % prod _ Ty Bo : The type of A
164 pair term name, % pr B Bn : The eta-expanded version of B with its name (they are fresh names)
165 list term % L : The list of name in the scope of A and B
166 -> prop, % Link : The new eta-link
167 term, % Ty' : The cleaned version of the binder in Ty
168 (term -> term). % Bo : the body of the type of A
169 make-eta-link-aux A (prod _ Ty Bo) (pr B Name) L Link Ty' Bo :- !,
170 clean-term Ty Ty',
171 name A' A {std.rev L},
172 Link = tc.link.eta A' (fun Name Ty' B'),
173 pi x\ sigma L'\ std.rev [x|L] L', name (B' x) B L'.
174 % Going under prod-range
175 make-eta-link-aux A (tc.prod-range Prod _) BN L Link Ty' Bo :- !,
176 make-eta-link-aux A Prod BN L Link Ty' Bo.
177 % The type of a higher order variable can be hidden behind a definition
178 % In this case we unfold this definition to get the prod constructor
179 make-eta-link-aux A T BN L Link Ty' Bo :-
180 coq.safe-dest-app T Hd Ag,
181 (@redflags! coq.redflags.delta => coq.reduction.lazy.whd Hd Hd'),
182 not (Hd = Hd'), !,
183 coq.mk-app Hd' Ag TT',
184 make-eta-link-aux A TT' BN L Link Ty' Bo.
185 make-eta-link-aux _ T _ _ _ _ _ :- coq.error "[TC] make-eta-link-aux of" T.
186
187 % Create spine of eta-links
188 :index (_ _ 1)
189 func make-eta-link term, term, list (pair term name), list term, list prop -> prop.
190 make-eta-link P PTy [Hd] L Links (do [Link1|Links]) :- !,
191 make-eta-link-aux P PTy Hd L Link1 _ _.
192 make-eta-link P PTy [(pr B _ as Hd)|Tl] L Links (pi x\ decl x `x` PTy' => Res x) :- !,
193 make-eta-link-aux P PTy Hd L Link1 PTy' Bo,
194 pi x\ make-eta-link B (Bo x) Tl [x|L] [Link1|Links] (Res x).
195 make-eta-link P PTy _ _ _ _ :- coq.error "[TC] make-eta-link error : empty list of pairs" P PTy.
196
197 % Accumulates pi for eta-links
198 :index (_ 1)
199 func add-link-eta-dedup
200 (func list prop -> prop),
201 range-arity, term, term, (list (pair term name)), list prop -> prop.
202 % Base case when the variable is always used at same arity
203 add-link-eta-dedup F (r-ar _ z) _ _ [] PremR Clause :- !,
204 F PremR Clause.
205 add-link-eta-dedup F (r-ar _ z) P Pty Acc PremR Clause :- !,
206 make-eta-link P Pty Acc [] [] LinkEtaDedup,
207 F [LinkEtaDedup|PremR] Clause.
208 add-link-eta-dedup F (r-ar M (s N)) P PTy Acc PremR (pi x y\ Clause x y) :- !,
209 pi x y\ name-pair P x (s N) => is-uvar x => add-link-eta-dedup F (r-ar M N) P PTy [pr x y|Acc] PremR (Clause x y).
210 add-link-eta-dedup _ Ar P PTy _ _ _ :- coq.error "[TC] add-link-eta-dedup error" Ar P PTy.
211
212 :functional
213 pred compile-premise
214 i:list term,
215 o:list term,
216 i:term,
217 i:term,
218 i:term,
219 i:bool,
220 i:term,
221 i:list term,
222 i:list prop,
223 o:prop.
224 compile-premise L L2 P PTy ProofHd IsPositive ITy ProofTlR PremR Clause :-
225 ((pi a b c\ tc.get-TC-of-inst-type (tc.prod-range a c) b :- !, tc.get-TC-of-inst-type a b) =>
226 tc.get-TC-of-inst-type PTy TC), !,
227 compile-ty L L1 P {neg IsPositive} PTy [] [] NewPrem,
228 if (tc.class TC _ tc.deterministic _)
229 (NewPrem' = std.once NewPrem)
230 (NewPrem' = NewPrem), !,
231 compile-ty L1 L2 ProofHd IsPositive ITy ProofTlR [NewPrem' | PremR] Clause.
232 compile-premise L L1 _ _ ProofHd IsPositive ITy ProofTlR PremR Clause :-
233 compile-ty L L1 ProofHd IsPositive ITy ProofTlR PremR Clause.
234
235 :index (_ _ _ _ 1)
236 func compile-ty
237 list term,
238 list term,
239 term,
240 bool,
241 term,
242 list term,
243 list prop
244 -> prop.
245 compile-ty L L1 ProofHd IsPositive (tc.prod-range (prod N Ty Bo) Arity) ProofTlR PremR Clause :- !,
246 std.do![
247 if (IsPositive = tt)
248 (Clause = (pi x\ C x), E = is-uvar)
249 (clean-term Ty Ty', Clause = (pi x\ decl x N Ty' => C x), E = is-name),
250 pi p\ sigma F\
251 F = compile-premise L L1 p Ty ProofHd IsPositive (Bo p) [p|ProofTlR],
252 decl p N Ty' =>
253 name-pair p p z => E p => add-link-eta-dedup F Arity p Ty [] PremR (C p)
254 ].
255 compile-ty L L1 ProofHd IsPositive (let _ Ty T Bo) ProofTlR PremR Clause :- !,
256 if (IsPositive = tt)
257 (Clause = (pi x\ C x), E = is-uvar)
258 (clean-term Ty Ty', Clause = (pi x\ decl x N Ty' => C x), E = is-name), !,
259 pi p\ sigma F NewPrem\
260 (decl p N Ty' :- !) => (E p :- !) => (
261 NewPrem = tc.link.unif-eq T p,
262 compile-premise L L1 p Ty ProofHd IsPositive (Bo p) ProofTlR [NewPrem | PremR] (C p)
263 ).
264
265 compile-ty L L2 ProofHd IsPositive Goal ProofTlR PremR Clause :-
266 std.do![
267 coq.mk-app ProofHd {std.rev ProofTlR} Proof,
268 decompile.decompile-term L L1 Proof Proof' Prem1,
269 decompile.decompile-term L1 L2 Goal Goal' Prem2,
270 compile-conclusion IsPositive Goal' Proof' Prem2 Prem1 {std.rev PremR} Clause
271 ].
272
273 func compile-conclusion
274 bool, % tt if the term is in positive position
275 term, % the goal (invariant: it is a constant or a application)
276 term, % the proof
277 list prop, % the list of HOPremises in input mode
278 list prop, % the list of HOPremises in output mode
279 list prop % the premises
280 -> prop. % the compiled clause for the instance
281
282 compile-conclusion tt Goal Proof HOPremisesIn HOPremisesOut Premises Clause :-
283 std.append {std.append HOPremisesIn Premises} HOPremisesOut AllPremises,
284 tc.make-tc Goal Proof AllPremises tt Clause.
285 compile-conclusion ff Goal Proof HOPremisesIn HOPremisesOut Premises Clause :-
286 tc.make-tc Goal Proof Premises ff Clause1,
287 Clause = (do HOPremisesIn, Clause1, do HOPremisesOut).
288
289 func context goal-ctx -> list prop.
290 context [] [].
291 context [X | Xs] [Clause | ResTl] :-
292 (decl Var _ Ty = X; def Var _ Ty _ = X),
293 tc.is-instance-term Ty, !,
294 std.assert! (compile.instance Ty Var Clause) "[TC] cannot compile instance of context",
295 context Xs ResTl.
296 context [_ | Tl] L :- context Tl L.
297 }
298
299 % build a list of Clauses of type tc to be temporarly added to the
300 % database, used in theorems having assumptions.
301 func context goal-ctx -> list prop.
302 :name "tc-compile-context"
303 context Ctx Clauses :-
304 std.assert! (instance.context Ctx Clauses) "[TC] cannot compile context".
305
306 func instance term, term -> prop.
307 instance Ty ProofHd Clause :-
308 tc.time-it tc.oTC-time-compile-instance (
309 tc.normalize-ty Ty Ty',
310 tc.precomp.instance Ty' Ty'' N UnivConst UnivInst,
311 instance.main N Ty'' ProofHd UnivConst UnivInst Clause
312 ) "Compile Instance", !.
313
314 func instance-gr gref -> prop.
315 % If the instance is polymorphic, we wrap its gref into the pglobal constructor
316 instance-gr InstGR (pi x\ Clause x) :- coq.env.univpoly? InstGR _, !,
317 coq.env.typeof InstGR Ty,
318 pi x\ tc.compile.instance Ty (pglobal InstGR x) (Clause x).
319 instance-gr InstGR Clause :-
320 coq.env.typeof InstGR Ty,
321 tc.compile.instance Ty (global InstGR) Clause.
322
323 namespace goal {
324 % [uvar-pair V1 Ty V2] List of uvar for link-eta-dedup
325 % V1 has arity n and V2 has arity n+1
326 % If V1 has type A -> B, then A = Ty
327 pred uvar-pair i:term, o:term, o:term.
328
329 % Type Var Cnt uvar-pair-list
330 func make-pairs-aux term, term -> list prop.
331 make-pairs-aux (prod _ Ty Bo) V [pi x\ uvar-pair x Ty X' :- x == V, ! | L] :- !,
332 pi x\ make-pairs-aux (Bo x) X' L.
333 make-pairs-aux _ _ [].
334
335 func make-pairs list term -> list prop.
336 make-pairs [] [] :- !.
337 make-pairs [X|Xs] L :- !,
338 coq.typecheck X Ty ok,
339 make-pairs-aux Ty X L',
340 make-pairs Xs L'',
341 std.append L' L'' L.
342
343 func get-uva-pair-arity term, list term -> term.
344 get-uva-pair-arity X [] X :- !.
345 get-uva-pair-arity X [_|L] Z :- uvar-pair X _ Y, !,
346 get-uva-pair-arity Y L Z.
347
348 func decompile-problematic-term term, list prop -> term, list prop.
349 decompile-problematic-term (tc.maybe-eta-tm T S) L V [tc.link.eta V T' | L2] :-
350 prune V S, !, fold-map T L T' L2.
351
352 decompile-problematic-term (tc.prod-range T _) A T' A' :- !,
353 fold-map T A T' A'.
354
355 decompile-problematic-term (tc.maybe-llam-tm (app [app[H|S] | NPF]) Sc) L Z [NL|L'] :- !,
356 prune Z Sc,
357 get-uva-pair-arity H S Y,
358 std.fold-map NPF L fold-map Tl L',
359 NL = tc.link.llam Z (app[Y | Tl]).
360
361 % TODO: complete this fold
362 decompile-problematic-term (app[X|S]) L Z L :-
363 var X _ Scope,
364 std.append Scope S Scope',
365 distinct_names Scope', !,
366 get-uva-pair-arity X S Y,
367 prune Z Scope', var Z Y Scope'.
368
369 decompile-problematic-term A L A L :- var A, !.
370
371 decompile-problematic-term (fun N Ty Bo) L (fun N Ty' Bo') L3 :-
372 fold-map Ty L Ty' L1,
373 (pi x\ fold-map (Bo x) [] (Bo' x) (Lx x)),
374 close-term-no-prune-ty Lx Ty' L2,
375 std.append L1 L2 L3.
376
377 decompile-problematic-term (prod N Ty Bo) L (prod N Ty' Bo') L3 :-
378 (pi x\ fold-map (Bo x) [] (Bo' x) (Lx x)),
379 close-term-no-prune-ty Lx Ty L1,
380 fold-map Ty L Ty' L2,
381 std.append L2 L1 L3.
382
383 func compile term, list prop -> term, list prop.
384 compile T L T' L' :-
385 (pi t l t' l'\ fold-map t l t' l' :- decompile-problematic-term t l t' l', !) =>
386 fold-map T L T' L'.
387
388 % Uva Binders LinkEta
389 func build-eta-links-of-vars-aux term, list term -> list prop.
390 build-eta-links-of-vars-aux Old L [Hd | Xs] :-
391 uvar-pair Old Ty Next, !,
392 prune OldScope L,
393 prune Name L,
394 var OldScope Old L,
395 Hd = tc.link.eta OldScope (fun Name Ty (x\ NextScope x)),
396 pi x\ sigma L'\
397 std.append L [x] L',
398 prune (NextScope x) L',
399 var (NextScope x) Next L',
400 build-eta-links-of-vars-aux Next L' (Ys x), !,
401 sigma Closed\ (close-term-no-prune-ty Ys Ty Closed),
402 Xs = Closed.
403 build-eta-links-of-vars-aux _ _ [].
404
405 func build-eta-links-of-vars list term -> list prop.
406 build-eta-links-of-vars [] [].
407 build-eta-links-of-vars [V|Vars] L :-
408 var V Hd S,
409 build-eta-links-of-vars-aux Hd S L',
410 build-eta-links-of-vars Vars L'',
411 std.append L' L'' L.
412 }
413
414 % Goal Goal' Links
415 func goal term -> term, list prop.
416 :name "compile-goal"
417 goal Goal Goal' Links :-
418 tc.precomp.goal Goal GoalPrecomp Vars, !,
419 goal.make-pairs Vars Pairs,
420 Pairs => (
421 std.assert!(goal.build-eta-links-of-vars Vars EtaLinks) "[TC] cannot build eta-links",
422 std.assert!(goal.compile GoalPrecomp EtaLinks Goal' Links) "[TC] cannot compile goal"
423 ).
424 }
425 }