Debian Codegraph @

apps/tc/elpi/ho_precompile.elpi

raw
1 namespace tc {
2 shorten tc.{r-ar, range-arity}.
3
4 namespace precomp {
5
6 namespace instance {
7 % Tells if the current name is a bound variables
8 pred is-name o:term.
9 % Tells if the current name stands for a uvar
10 pred is-uvar o:term.
11
12 :index (_ _ 1)
13 func may-contract-to list term, term, term ->.
14 may-contract-to _ N N :- !.
15 may-contract-to L N (app [V|S]) :- var V, !,
16 std.forall [N|L] (x\ std.exists! S (may-contract-to [] x)).
17 may-contract-to L N (app [V|S]) :- is-uvar V, !,
18 std.forall [N|L] (x\ std.exists! S (may-contract-to [] x)).
19 may-contract-to L N (app [N|A]) :-
20 std.length A {std.length L},
21 std.forall2 {std.rev L} A (may-contract-to []).
22 may-contract-to L N (fun _ _ B) :-
23 pi x\ may-contract-to [x|L] N (B x).
24
25 :index (_ 1)
26 func occurs-rigidly term, term ->.
27 occurs-rigidly N N :- name N, !.
28 occurs-rigidly _ (app [N|_]) :- is-uvar N, !, fail.
29 occurs-rigidly _ (app [N|_]) :- var N, !, fail.
30 occurs-rigidly N (app A) :- std.exists! A (occurs-rigidly N).
31 occurs-rigidly N (fun _ _ B) :- pi x\ occurs-rigidly N (B x).
32
33 func maybe-eta-aux term, list term ->.
34 maybe-eta-aux (app[V|S]) L :- is-uvar V, !,
35 std.forall L (x\ std.exists! S (y\ may-contract-to [] x y)).
36 maybe-eta-aux (app[V|S]) L :- var V, !,
37 std.forall L (x\ std.exists! S (y\ may-contract-to [] x y)).
38 maybe-eta-aux (app [_|A]) L :-
39 SplitLen is {std.length A} - {std.length L},
40 split-at-not-fatal SplitLen A HD TL,
41 std.forall L (x\ not (std.exists! HD (occurs-rigidly x))),
42 std.forall2 {std.rev L} TL (may-contract-to []).
43 maybe-eta-aux (fun _ _ B) L :-
44 pi x\ maybe-eta-aux (B x) [x|L].
45
46 func maybe-eta term ->.
47 maybe-eta (fun _ _ B) :- pi x\ maybe-eta-aux (B x) [x].
48
49 func free-var -> list term.
50 free-var L :-
51 std.findall (is-name _) T,
52 std.map T (x\y\ x = is-name y) L.
53
54 func split-pf list term, list term -> list term, list term.
55 split-pf [] _ [] [] :- !.
56 split-pf [X|Xs] Old [X|Ys] L :- is-name X, not (std.mem! Old X), !, split-pf Xs [X|Old] Ys L.
57 split-pf Xs _ [] Xs.
58
59 kind positivity type.
60 type is_pos positivity.
61 type is_neg positivity.
62 type is_neg_fix positivity.
63
64 :index (1 _)
65 func neg positivity -> positivity.
66 neg is_pos is_neg :- !.
67 neg is_neg is_pos :- !.
68 neg is_neg_fix is_neg_fix :- !.
69
70 macro @max-min :- r-ar inf z.
71
72 func min-max-nat range-arity, range-arity -> range-arity.
73 min-max-nat (r-ar A B) (r-ar A' B') (r-ar A'' B'') :- !,
74 min-nat A A' A'', max-nat B B' B''.
75
76 % TODO: this is incomplete: it lacks of some term constructors
77 :index (_ 1)
78 func get-range-arity-aux term, term -> range-arity.
79 get-range-arity-aux N N (r-ar z z) :- !.
80 get-range-arity-aux _ N @max-min :- name N, !.
81 get-range-arity-aux T (app [T|L]) R :- !,
82 length-nat L Len,
83 std.fold L (r-ar Len Len) (x\y\w\ sigma M\ get-range-arity-aux T x M, min-max-nat y M w) R.
84 get-range-arity-aux T (app [_|L]) R :- !,
85 std.fold L @max-min (x\y\w\ sigma M\ get-range-arity-aux T x M, min-max-nat y M w) R.
86 get-range-arity-aux T (fun _ Ty B) R2 :- !,
87 get-range-arity-aux T Ty R,
88 (pi x\ get-range-arity-aux T (B x) R1),
89 min-max-nat R R1 R2.
90 get-range-arity-aux T (prod _ Ty B) R2 :- !,
91 get-range-arity-aux T Ty R,
92 (pi x\ get-range-arity-aux T (B x) R1),
93 min-max-nat R R1 R2.
94 get-range-arity-aux _ (global _) @max-min :- !.
95 get-range-arity-aux _ uvar @max-min :- !.
96 get-range-arity-aux _ (sort _) @max-min :- !.
97 get-range-arity-aux _ (pglobal _ _) @max-min :- !.
98 get-range-arity-aux X (let _ T Ty B) R4 :- !,
99 get-range-arity-aux X Ty R1,
100 get-range-arity-aux X T R2,
101 (pi x\ get-range-arity-aux T (B x) R3),
102 min-max-nat R1 R2 R12,
103 min-max-nat R12 R3 R4.
104 get-range-arity-aux A B _ :- coq.error "Count maximal arity failure" A B.
105
106 func get-range-arity term, term, term -> range-arity.
107 get-range-arity _ Ty _ (r-ar z N) :- tc.get-TC-of-inst-type Ty _, !, count-prod Ty N.
108 get-range-arity B _ T N :- !, get-range-arity-aux B T N.
109
110 :index (_ 1)
111 func precompile-aux positivity, term, nat -> term, nat.
112 precompile-aux _ X A Y A :- name X, !, X = Y, !. % avoid loading "precompile-aux x A x A" at binders
113 precompile-aux _ (global _ as C) A C A :- !.
114 precompile-aux _ (pglobal _ _ as C) A C A :- !.
115 precompile-aux _ (sort _ as C) A C A :- !.
116
117 % Detect maybe-eta term
118 % TODO: should I precompile also the type of the fun and put it in the output term
119 precompile-aux _ (fun Name Ty B as T) N (tc.maybe-eta-tm (fun Name Ty B') Scope) (s M) :-
120 maybe-eta T, !,
121 free-var Scope,
122 precompile-aux is_neg_fix Ty N _ N',
123 (pi x\ is-name x => precompile-aux is_neg_fix (B x) N' (B' x) M).
124
125 precompile-aux _ (app [X|XS]) N (tc.maybe-llam-tm (app [app[X | PF] | NPF1]) Scope) (s M) :-
126 if (is-uvar X) (Sc = []) (var X _ Sc), split-pf XS Sc PF NPF,
127 not (NPF = []), !, % else XS is a list of distinct names, i.e. `app [X|XS]` is in PF
128 free-var Scope,
129 std.fold-map NPF N (precompile-aux is_neg_fix) NPF1 M.
130
131 % Charge if we work with unification variable or local name
132 % And returns the subterms is a prod-range
133 precompile-aux IsP (prod Name Ty B) N (tc.prod-range (prod Name Ty' B') MaxAr) P :- !,
134 std.assert! (pi x\ get-range-arity x Ty (B x) MaxAr) "[TC] get-range-arity should not fail",
135 if (IsP = is_pos) (C = x\ is-uvar x) (C = x\ is-name x),
136 std.assert! (pi x\ C x => precompile-aux IsP (B x) N (B' x) M) "[TC] should not fail",
137 precompile-aux {neg IsP} Ty M Ty' P.
138
139 % Working with fun
140 precompile-aux _ (fun N T F) A (fun N T1 F1) A2 :- !,
141 precompile-aux is_neg_fix T A T1 A1, pi x\ is-name x => precompile-aux is_neg_fix (F x) A1 (F1 x) A2.
142
143 precompile-aux _ (app L) A (app L1) A1 :- !, std.fold-map L A (precompile-aux is_neg_fix) L1 A1.
144 precompile-aux _ X A X A :- var X, !.
145
146 precompile-aux _ (let N T Ty Bo) A (let N T' Ty' Bo') A3 :- !,
147 precompile-aux is_neg_fix T A T' A1,
148 precompile-aux is_neg_fix Ty A1 Ty' A2,
149 pi x\ is-name x => precompile-aux is_neg_fix (Bo x) A2 (Bo' x) A3.
150
151
152 % TODO: what about the following constructors?
153 % precompile-aux IsP (let N T B F) A (let N T1 B1 F1) A3 :- !,
154 % precompile-aux IsP T A T1 A1, precompile-aux IsP B A1 B1 A2, pi x\ is-name x => precompile-aux IsP (F x) A2 (F1 x) A3.
155 % precompile-aux IsP (fix N Rno Ty F) A (fix N Rno Ty1 F1) A2 :- !,
156 % precompile-aux IsP Ty A Ty1 A1, pi x\ is-name x => precompile-aux IsP (F x) A1 (F1 x) A2.
157 % precompile-aux IsP (match T Rty B) A (match T1 Rty1 B1) A3 :- !,
158 % precompile-aux IsP T A T1 A1, precompile-aux IsP Rty A1 Rty1 A2, std.fold-map B A2 (precompile-aux IsP) B1 A3.
159 precompile-aux _ (primitive _ as C) A C A :- !.
160 % precompile-aux IsP (uvar M L as X) A W A1 :- var X, !, std.fold-map L A (precompile-aux IsP) L1 A1, coq.mk-app-uvar M L1 W.
161 % % when used in CHR rules
162 % precompile-aux IsP (uvar X L) A (uvar X L1) A1 :- std.fold-map L A (precompile-aux IsP) L1 A1.
163
164 func get-univ-instances term -> list univ-instance.
165 get-univ-instances T L :-
166 (pi x L\ fold-map (pglobal _ x) L _ [x | L] :- !) => fold-map T [] _ L, !.
167
168 func get-univ term -> list univ.
169 get-univ T L :-
170 coq.univ.variable.set.elements {coq.univ.variable.of-term T} Vars,
171 std.map Vars (x\r\ coq.univ.variable r x) L.
172
173 }
174
175 /*
176 [tc.precomp.instance T T' N]
177 Returns T' N from T, where:
178 T' is obtained by the replacement of
179 - all maybe-eta term `t1` with (tc.maybe-eta-tm `t1` `s`) where `s` = FV(`t1`)
180 ==> This helps knowing if a subterm should be replaced with a `eta-link`
181 - all `prod _ Ty (x\ Bo x)` with (tc.prod-range (prod _ Ty (x\ Bo x)) N),
182 where N is represent the "maximal" application of `x` in `Bo`
183 for example:
184 let Ty = {{Type -> Type -> Type -> Type -> Type}},
185 and Bo = x\ c1 (x nat bool) (x nat) (x nat nat bool)
186 the term `prod _ Ty Bo` is replaced with
187 (tc.prod-range (prod _ T Bo) 3)
188 since x is applied at most 3 times in Bo
189 ==> This helps charging the right number of `eta-link` for map-deduplication rule
190 N is the number of problematic terms in T
191 */
192 func instance term -> term, nat, list univ, list univ-instance.
193 instance T T' N UnivConstL UnivInstL :-
194 tc.precomp.instance.get-univ T UnivConstL,
195 tc.precomp.instance.get-univ-instances T UnivInstL,
196 std.assert!(instance.precompile-aux instance.is_pos T z T' N) "[TC] cannot precompile instance".
197
198 namespace goal {
199 :index (_ _ 1)
200 func may-contract-to list term, term, term ->.
201 may-contract-to _ N N :- !.
202 % TODO: here we should do var V _ Scope and use scope: N can be in Scope but not in S
203 may-contract-to L N (app [V|S]) :- var V, !,
204 std.forall [N|L] (x\ std.exists! S (may-contract-to [] x)).
205 may-contract-to L N (app [N|A]) :-
206 std.length A {std.length L},
207 std.forall2 {std.rev L} A (may-contract-to []).
208 may-contract-to L N (fun _ _ B) :-
209 pi x\ may-contract-to [x|L] N (B x).
210
211 :index (_ 1)
212 func occurs-rigidly term, term ->.
213 occurs-rigidly N N :- name N, !.
214 occurs-rigidly _ (app [N|_]) :- var N, !, fail.
215 occurs-rigidly N (app A) :- std.exists! A (occurs-rigidly N).
216 occurs-rigidly N (fun _ _ B) :- pi x\ occurs-rigidly N (B x).
217
218 func maybe-eta-aux term, list term ->.
219 % TODO: here we should do var V _ Scope and use Scope: an elt in L can appear in Scope
220 maybe-eta-aux (app[V|S]) L :- var V, !,
221 std.forall L (x\ std.exists! S (y\ may-contract-to [] x y)).
222 maybe-eta-aux (app [_|A]) L :-
223 SplitLen is {std.length A} - {std.length L},
224 split-at-not-fatal SplitLen A HD TL,
225 std.forall L (x\ not (std.exists! HD (occurs-rigidly x))),
226 std.forall2 {std.rev L} TL (may-contract-to []).
227 maybe-eta-aux (fun _ _ B) L :-
228 pi x\ maybe-eta-aux (B x) [x|L].
229
230 func maybe-eta term ->.
231 maybe-eta (fun _ _ B) :- pi x\ maybe-eta-aux (B x) [x].
232
233 func split-pf list term, list term -> list term, list term.
234 split-pf [] _ [] [] :- !.
235 split-pf [X|Xs] Old [X|Ys] L :- name X, not (std.mem! Old X), !, split-pf Xs [X|Old] Ys L.
236 split-pf Xs _ [] Xs.
237
238 func precompile-aux term, list term -> term, list term.
239 precompile-aux X A Y A :- name X, !, X = Y, !. % avoid loading "precompile-aux x A x A" at binders
240 precompile-aux (global _ as C) A C A :- !.
241 precompile-aux (pglobal _ _ as C) A C A :- !.
242 precompile-aux (sort _ as C) A C A :- !.
243
244 % Detect maybe-eta term
245 precompile-aux (fun Name Ty B as T) N (tc.maybe-eta-tm (fun Name Ty' B') Scope) M :-
246 maybe-eta T, !,
247 names Scope,
248 (pi x\ precompile-aux (B x) N (B' x) M'),
249 precompile-aux Ty M' Ty' M.
250
251 % Detect maybe-beta term
252 precompile-aux (app [X|XS]) N (tc.maybe-llam-tm (app [app[X | PF] | NPF1]) Scope1) [X|M] :-
253 var X _ Scope, split-pf XS Scope PF NPF,
254 not (NPF = []), !, % else XS is a list of distinct names, i.e. `app [X|XS]` is in PF
255 names Scope1,
256 std.fold-map NPF N precompile-aux NPF1 M.
257
258 % In the goal there are
259 precompile-aux (prod Name Ty B) N (tc.prod-range (prod Name Ty' B') (r-ar z MaxAr)) P :- !,
260 count-prod Ty MaxAr,
261 std.assert! (pi x\ precompile-aux (B x) N (B' x) M) "[TC] should not fail",
262 precompile-aux Ty M Ty' P.
263
264 % Working with fun
265 precompile-aux (fun N T F) A (fun N T F1) A2 :- !, A = A1,
266 /*precompile-aux IsP T A T1 A1,*/ pi x\ precompile-aux (F x) A1 (F1 x) A2.
267
268 precompile-aux (app L) A (app L1) A1 :- !, std.fold-map L A precompile-aux L1 A1.
269 precompile-aux (let N T B F) A (let N T1 B1 F1) A3 :- !,
270 precompile-aux T A T1 A1, precompile-aux B A1 B1 A2, pi x\ precompile-aux (F x) A2 (F1 x) A3.
271 precompile-aux (fix N Rno Ty F) A (fix N Rno Ty1 F1) A2 :- !,
272 precompile-aux Ty A Ty1 A1, pi x\ precompile-aux (F x) A1 (F1 x) A2.
273 precompile-aux (match T Rty B) A (match T1 Rty1 B1) A3 :- !,
274 precompile-aux T A T1 A1, precompile-aux Rty A1 Rty1 A2, std.fold-map B A2 precompile-aux B1 A3.
275 precompile-aux (primitive _ as C) A C A :- !.
276 precompile-aux X A X [X|A] :- var X, !.
277 }
278
279 func goal term -> term, list term.
280 goal T T' Vars' :- std.assert!(goal.precompile-aux T [] T' Vars) "[TC] cannot precompile goal", undup-same Vars Vars'.
281 }
282 }