Skip to content

Commit f53d5f1

Browse files
committed
[TC] remove goal precomp and goal compile (all is done in ycompile_goal.elpi)
1 parent a8502e9 commit f53d5f1

File tree

6 files changed

+49
-294
lines changed

6 files changed

+49
-294
lines changed

apps/tc/elpi/compile_goal.elpi

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
namespace tc {
2-
shorten tc.{r-ar, range-arity}.
3-
42
namespace compile {
53
namespace goall {
64
:index (_ _ 1)
@@ -76,7 +74,7 @@ namespace tc {
7674
compile-full-aux (app [primitive (proj P _) | _] as G) L G' [tc.link.cs G' G | L] :- coq.env.primitive-projection? P _, !.
7775

7876
% Link for eta-redex
79-
compile-full-aux (fun Name Ty Bo as G) L G' [tc.link.eta G' (fun Name Ty' Bo') | L'] :- maybe-eta G, !,
77+
compile-full-aux (fun Name_ Ty Bo as G) L G' [tc.link.eta G' (fun `_` Ty' Bo') | L'] :- maybe-eta G, !,
8078
compile-full-aux Ty L Ty' LTy,
8179
compile-full-aux-close Bo Ty LTy Bo' L'.
8280

@@ -111,37 +109,16 @@ namespace tc {
111109
% a |- Z x y a w =L (app[Y x y a, c w d])
112110
% The returned variable is Z x y a w
113111

114-
pred free-names i:term, o:list term.
115-
free-names T L :-
116-
names N,
117-
(pi T L\ fold-map T L T [T|L] :- name T, std.mem! N T, !) => fold-map T [] _ L',
118-
undup {std.rev L'} L.
119-
120-
:index(_ _ _ _ 3)
121-
% TODO: the argument Ty is unused, i.e. the binders of the eta links have no type...
122-
% LHS Scope Ty Names PF NPF OLDLINKS NEW_VAR
123-
pred build-eta-llam-links i:term, i:list term, o:term, i:list term, i:list term, i:list term, i:list prop, o:term, o:list prop.
124-
build-eta-llam-links LHS _ _ Names [] NPF OL HD [tc.link.llam T (app [LHS | NPF]) | OL] :- !,
125-
std.assert! (not (NPF = [])) "[TC] NPF List should not be empty",
126-
prune T Names,
127-
var T HD _.
128-
build-eta-llam-links LHS SC _Ty _ [X] [] OL HD [tc.link.eta LHS (fun _ _ (x\ V x)) | OL] :- !,
129-
prune V SC, var (V X) HD _. % TODO...
130-
build-eta-llam-links LHS SC _Ty Names [X|XS] NPF OL HD [tc.link.eta LHS (fun _ _ (x\ LHS' x)) | L] :- !,
131-
% TODO: unfold Ty if needed...
132-
std.append SC [X] SC',
133-
prune LHS' SC, build-eta-llam-links (LHS' X) SC' _ Names XS NPF OL HD L.
134-
135112
% Link for beta-redex (Uvar in PF)
136113
% BUILD CHAIN OF LINKS-ETA from X to V...
137114
% TODO: to avoid too many chain for the same var, maybe pass a list into the fold
138115
compile-full-aux (app [(uvar _ Scope as V) | S] as T) L G' L'' :- !,
139116
% By construction the scope of a uvar is a list of distinct name
140117
std.fold-map S L compile-full-aux S' L', % LS = Links built for S
141-
% std.assert-ok! (coq.typecheck V Ty) "Not typecheckable variable",
118+
coq.typecheck V Ty ok,
142119
split-pf S' Scope PF NPF,
143120
free-names T Names,
144-
build-eta-llam-links V Scope _ Names PF NPF L' G'' L'',
121+
tc.link.build-eta-llam-links V Scope Ty Names PF NPF L' G'' L'',
145122
prune G' Names,
146123
var G' G'' Names.
147124

apps/tc/elpi/ho_compile.elpi

Lines changed: 19 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,23 @@ namespace tc {
5959
% This happens when the instance to be compiled comes from the context
6060
% Example: Goal exists (X : T1 -> T2), forall a, c (X a) -> ...
6161
% intros; eexists. (* In the context we have the instance `H: c (?X a)` *)
62-
decompile-term-aux (tc.maybe-llam-tm (app[app[H | PF] | NPF]) S) A Z (pr XS' [NL | L3]) :- !,
63-
var H _ Scope, !,
64-
std.append Scope S S',
65-
prune Z S',
66-
tc.compile.goal.make-pairs [T] Pairs,
67-
% We build on the fly the eta-links for T
68-
Pairs =>
69-
(tc.compile.goal.build-eta-links-of-vars [T] P,
70-
tc.compile.goal.get-uva-pair-arity T PF Y),
71-
std.fold-map NPF A decompile-term-aux Tl (pr XS' L2),
72-
std.append P L2 L3,
73-
NL = tc.link.llam Z (app [Y|Tl]).
62+
decompile-term-aux (tc.maybe-llam-tm (app[app[V | PF] | NPF] as T) S_) L G'' (pr XS' L'') :- !,
63+
var V _ Scope, !,
64+
std.fold-map NPF L decompile-term-aux NPF' (pr XS' L'),
65+
free-names T Names,
66+
coq.typecheck V Ty ok,
67+
tc.link.build-eta-llam-links V Scope Ty Names PF NPF' L' G' L'',
68+
prune G'' Names,
69+
var G'' G' Names.
70+
71+
% HO var when H is a hole appearing in the shelved goals
72+
decompile-term-aux (app [V|PF] as T) (pr XS' L') G'' (pr XS' L'') :-
73+
var V _ Scope, !,
74+
free-names T Names,
75+
coq.typecheck V Ty ok,
76+
tc.link.build-eta-llam-links V Scope Ty Names PF [] L' G' L'',
77+
prune G'' Names,
78+
var G'' G' Names.
7479

7580
decompile-term-aux (fun Name Ty Bo) (pr XS L) (fun Name Ty' Bo') (pr XS2 L3) :- !,
7681
(pi x\ is-name x => decompile-term-aux (Bo x) (pr XS []) (Bo' x) (pr XS1 (L1x x))),
@@ -96,21 +101,6 @@ namespace tc {
96101
std.assert! (name-pair H V Len) "[TC] name-pair not found",
97102
name R V L.
98103

99-
% HO var when H is a hole appearing in the shelved goals
100-
decompile-term-aux (app [T|L]) (pr A B) Z (pr A B') :-
101-
var T _ Scope,
102-
std.forall L is-name, % Not needed, since decompile for llam leaves only PF
103-
distinct_names L, !, % Not needed, since decompile for llam leaves only PF
104-
std.append Scope L Scope',
105-
prune Z Scope',
106-
tc.compile.goal.make-pairs [T] Pairs,
107-
% We build on the fly the eta-links for `T`
108-
Pairs =>
109-
(tc.compile.goal.build-eta-links-of-vars [T] P,
110-
tc.compile.goal.get-uva-pair-arity T L Y),
111-
var Z Y Scope',
112-
std.append P B B'.
113-
114104
decompile-term-aux (app L) PR (app L') PR' :- !,
115105
std.fold-map L PR decompile-term-aux L' PR'.
116106

@@ -174,10 +164,10 @@ namespace tc {
174164
o:prop, % Link : The new eta-link
175165
o:term, % Ty' : The cleaned version of the binder in Ty
176166
o:(term -> term). % Bo : the body of the type of A
177-
make-eta-link-aux A (prod _ Ty Bo) (pr B Name) L Link Ty' Bo :- !,
167+
make-eta-link-aux A (prod _ Ty Bo) (pr B Name_) L Link Ty' Bo :- !,
178168
clean-term Ty Ty',
179169
name A' A {std.rev L},
180-
Link = tc.link.eta A' (fun Name Ty' B'),
170+
Link = tc.link.eta A' (fun `_` Ty' B'),
181171
pi x\ sigma L'\ std.rev [x|L] L', name (B' x) B L'.
182172
% Going under prod-range
183173
make-eta-link-aux A (tc.prod-range Prod _) BN L Link Ty' Bo :- !,
@@ -313,129 +303,5 @@ namespace tc {
313303
instance-gr InstGR Clause :-
314304
coq.env.typeof InstGR Ty,
315305
tc.compile.instance Ty (global InstGR) Clause.
316-
317-
namespace goal {
318-
% [uvar-pair V1 Ty V2] List of uvar for link-eta-dedup
319-
% V1 has arity n and V2 has arity n+1
320-
% If V1 has type A -> B, then A = Ty
321-
pred uvar-pair i:term, o:term, o:term.
322-
323-
% Type Var Cnt uvar-pair-list
324-
pred make-pairs-aux i:term, i:term, o:list prop.
325-
make-pairs-aux Ty (fun _ _ IBo) L' :- !,
326-
pi x\ make-pairs-aux Ty (IBo x) (L x), close-prop-no-prune L L'.
327-
make-pairs-aux (prod _ Ty Bo) V [pi x\ uvar-pair x Ty X' :- x == V, ! | L] :- !,
328-
pi x\ make-pairs-aux (Bo x) X' L.
329-
make-pairs-aux _ _ [].
330-
331-
pred make-pairs i:list term, o:list prop.
332-
make-pairs [] [] :- !.
333-
make-pairs [X|Xs] L :- !,
334-
coq.typecheck X Ty ok,
335-
make-pairs-aux Ty X L',
336-
make-pairs Xs L'',
337-
std.append L' L'' L.
338-
339-
pred get-uva-pair-arity i:term, i:list term, o:term.
340-
get-uva-pair-arity X [] X :- !.
341-
get-uva-pair-arity X [_|L] Z :- uvar-pair X _ Y, !,
342-
get-uva-pair-arity Y L Z.
343-
344-
pred decompile-problematic-term i:term, i:list prop, o:term, o:list prop.
345-
% there is no need to decompile T since no precompilation is done inside coercions
346-
decompile-problematic-term (tc.coercion T S) L1 Y [tc.link.cs Y T|L1] :- !,
347-
prune Y S.
348-
% there is no need to decompile T since no precompilation is done inside CS
349-
decompile-problematic-term (tc.canonical-projection T S _) L1 Y [tc.link.cs Y T|L1] :- !,
350-
prune Y S.
351-
352-
decompile-problematic-term (tc.maybe-eta-tm T S) L V [tc.link.eta V T' | L2] :- !,
353-
prune V S, decompile-problematic-term T L T' L2.
354-
355-
decompile-problematic-term (tc.prod-range T _) A T' A' :- !,
356-
decompile-problematic-term T A T' A'.
357-
358-
decompile-problematic-term (tc.maybe-llam-tm (app [app[H|S] | NPF]) Sc) L Z [NL|L'] :- !,
359-
prune Z Sc,
360-
get-uva-pair-arity H S Y,
361-
std.fold-map NPF L decompile-problematic-term Tl L',
362-
NL = tc.link.llam Z (app[Y | Tl]).
363-
364-
decompile-problematic-term (app[X|S]) L Z L :-
365-
var X _ Scope,
366-
std.append Scope S Scope',
367-
distinct_names Scope', !,
368-
get-uva-pair-arity X S Y,
369-
prune Z Scope', var Z Y Scope'.
370-
371-
decompile-problematic-term (fun N Ty Bo) L (fun N Ty' Bo') L3 :- !,
372-
(pi x\ decompile-problematic-term (Bo x) [] (Bo' x) (Lx x)),
373-
close-term-no-prune-ty Lx {tc.compile.instance.clean-term Ty} L1,
374-
decompile-problematic-term Ty L Ty' L2,
375-
std.append L2 L1 L3.
376-
377-
decompile-problematic-term (prod N Ty Bo) L (prod N Ty' Bo') L3 :- !,
378-
(pi x\ decompile-problematic-term (Bo x) [] (Bo' x) (Lx x)),
379-
close-term-no-prune-ty Lx {tc.compile.instance.clean-term Ty} L1,
380-
decompile-problematic-term Ty L Ty' L2,
381-
std.append L2 L1 L3.
382-
383-
decompile-problematic-term (global _ as C) A C A :- !.
384-
decompile-problematic-term (pglobal _ _ as C) A C A :- !.
385-
decompile-problematic-term (sort _ as C) A C A :- !.
386-
decompile-problematic-term (let N T B F) A (let N T1 B1 F1) A3 :- !,
387-
decompile-problematic-term T A T1 A1, decompile-problematic-term B A1 B1 A2, pi x\ decompile-problematic-term (F x) A2 (F1 x) A3.
388-
decompile-problematic-term (app L) A (app L1) A1 :- !, std.fold-map L A decompile-problematic-term L1 A1.
389-
decompile-problematic-term (fix N Rno Ty F) A (fix N Rno Ty1 F1) A2 :- !,
390-
decompile-problematic-term Ty A Ty1 A1, pi x\ decompile-problematic-term (F x) A1 (F1 x) A2.
391-
decompile-problematic-term (match T Rty B) A (match T1 Rty1 B1) A3 :- !,
392-
decompile-problematic-term T A T1 A1, decompile-problematic-term Rty A1 Rty1 A2, std.fold-map B A2 decompile-problematic-term B1 A3.
393-
decompile-problematic-term (primitive _ as C) A C A :- !.
394-
decompile-problematic-term (uvar _ _ as V) A V A :- !.
395-
decompile-problematic-term X A Y A :- name X, !, X = Y, !.
396-
397-
pred compile i:term, i:list prop, o:term, o:list prop.
398-
compile T L T' L' :- decompile-problematic-term T L T' L'.
399-
400-
% Uva Binders LinkEta
401-
pred build-eta-links-of-vars-aux i:term, i:list term, o:list prop.
402-
build-eta-links-of-vars-aux Old L [Hd | Xs] :-
403-
uvar-pair Old Ty Next, !,
404-
prune OldScope L,
405-
prune Name L,
406-
var OldScope Old L,
407-
Hd = tc.link.eta OldScope (fun Name Ty (x\ NextScope x)),
408-
pi x\ sigma L'\
409-
std.append L [x] L',
410-
prune (NextScope x) L',
411-
var (NextScope x) Next L',
412-
build-eta-links-of-vars-aux Next L' (Ys x), !,
413-
sigma Closed\ (close-term-no-prune-ty Ys Ty Closed),
414-
Xs = Closed.
415-
build-eta-links-of-vars-aux _ _ [].
416-
417-
pred build-eta-links-of-vars i:list term, o:list prop.
418-
build-eta-links-of-vars [] [].
419-
build-eta-links-of-vars [V|Vars] L :-
420-
var V Hd S,
421-
build-eta-links-of-vars-aux Hd S L',
422-
build-eta-links-of-vars Vars L'',
423-
std.append L' L'' L.
424-
build-eta-links-of-vars [fun _ Ty Bo|Vars] L :-
425-
(pi x\ build-eta-links-of-vars [Bo x] (L' x), close-term-no-prune-ty L' Ty L''),
426-
build-eta-links-of-vars Vars L''',
427-
std.append L'' L''' L.
428-
}
429-
430-
% Goal Goal' Links
431-
pred goal i:term, o:term, o:list prop.
432-
:name "compile-goal"
433-
goal Goal Goal' Links :-
434-
tc.precomp.goal Goal GoalPrecomp Vars, !,
435-
goal.make-pairs Vars Pairs,
436-
Pairs => (
437-
std.assert!(goal.build-eta-links-of-vars Vars EtaLinks) "[TC] cannot build eta-links",
438-
std.assert!(goal.compile GoalPrecomp EtaLinks Goal' Links) "[TC] cannot compile goal"
439-
).
440306
}
441307
}

apps/tc/elpi/ho_link.elpi

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,29 @@ namespace tc {
99
(pi X H L Ign\ fold-map X L X [H|L] :- var X H Ign, !) =>
1010
fold-map T [] _ R.
1111

12+
% [build-eta-llam-links LHS Scope Ty Names PF NPF OldLinks NewVar NewLinks]
13+
% Builds the eta/llam links at runtime for a term having the shape
14+
% `app[(uvar X Scope as V) | L]`.
15+
% LHS should be V, Scope the scope of V, TODO: finish to document this...
16+
:index(_ _ _ _ 3)
17+
% TODO: the argument Ty is unused, i.e. the binders of the eta links have no type...
18+
% LHS Scope Ty Names PF NPF OldLinks NewVar
19+
pred build-eta-llam-links i:term, i:list term, o:term, i:list term, i:list term, i:list term, i:list prop, o:term, o:list prop.
20+
build-eta-llam-links LHS _ _ Names [] NPF OL HD [tc.link.llam T (app [LHS | NPF]) | OL] :- !,
21+
std.assert! (not (NPF = [])) "[TC] NPF List should not be empty",
22+
prune T Names,
23+
var T HD _.
24+
build-eta-llam-links LHS SC (prod _ Ty _) _ [X] [] OL HD [tc.link.eta LHS (fun `_` Ty (x\ V x)) | OL] :- !,
25+
prune V SC, var (V X) HD _.
26+
build-eta-llam-links LHS SC (prod _ Ty Bo) Names [X|XS] NPF OL HD [tc.link.eta LHS (fun `_` Ty (x\ LHS' x)) | L] :- !,
27+
% TODO: unfold Ty if needed...
28+
std.append SC [X] SC',
29+
prune LHS' SC, build-eta-llam-links (LHS' X) SC' (Bo X) Names XS NPF OL HD L.
30+
build-eta-llam-links LHS SC Ty Names ([_|_] as PF) NPF OL HD L :-
31+
Ty' = prod _ _ _, coq.unify-eq Ty Ty' ok, !,
32+
build-eta-llam-links LHS SC Ty' Names PF NPF OL HD L.
33+
34+
1235
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1336
% ETA LINK %
1437
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -73,7 +96,7 @@ namespace tc {
7396

7497
pred unify-eta i:term, i:term.
7598
% unify-eta A B :- coq.say "Unify-eta" "A"A"B"B, fail.
76-
unify-eta A B :- var A, !, A = B, !.
99+
unify-eta (uvar _ _ as A) B :- !, A = B, !.
77100
unify-eta (fun _ _ A) (fun _ _ B) :- !, pi x\ unify-eta (A x) (B x).
78101
unify-eta A (fun _ _ _ as B) :- !, eta-expand A A', unify-eta A' B.
79102
unify-eta A B :- A = B.
@@ -103,7 +126,6 @@ namespace tc {
103126
pred llam i:term, i:term.
104127
llam A (uvar _ S as T) :- distinct_names S, !, A = T.
105128
llam A (app [H|L] as T) :- var A, var H, !, get-vars T Vars,
106-
coq.say "Declaring constraint" (llam A (app [H|L])) [_,A|Vars],
107129
declare_constraint (llam A (app [H|L])) [_,A|Vars].
108130
llam (fun _ _ _ as F) (app [H | TL]) :-
109131
var H _ Scope, !,

0 commit comments

Comments
 (0)