Skip to content

Commit 9026ada

Browse files
committed
[TC] perf: fold-map fully implemented in precompile goal
1 parent fdbf746 commit 9026ada

File tree

2 files changed

+29
-23
lines changed

2 files changed

+29
-23
lines changed

apps/tc/elpi/ho_compile.elpi

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,6 @@ namespace tc {
342342
get-uva-pair-arity Y L Z.
343343

344344
pred decompile-problematic-term i:term, i:list prop, o:term, o:list prop.
345-
decompile-problematic-term (primitive _ as C) A C A :- !.
346-
347345
% there is no need to decompile T since no precompilation is done inside coercions
348346
decompile-problematic-term (tc.coercion T S) L1 Y [tc.link.cs Y T|L1] :- !,
349347
prune Y S.
@@ -352,43 +350,52 @@ namespace tc {
352350
prune Y S.
353351

354352
decompile-problematic-term (tc.maybe-eta-tm T S) L V [tc.link.eta V T' | L2] :- !,
355-
prune V S, fold-map T L T' L2.
353+
prune V S, decompile-problematic-term T L T' L2.
356354

357355
decompile-problematic-term (tc.prod-range T _) A T' A' :- !,
358-
fold-map T A T' A'.
356+
decompile-problematic-term T A T' A'.
359357

360358
decompile-problematic-term (tc.maybe-llam-tm (app [app[H|S] | NPF]) Sc) L Z [NL|L'] :- !,
361359
prune Z Sc,
362360
get-uva-pair-arity H S Y,
363-
std.fold-map NPF L fold-map Tl L',
361+
std.fold-map NPF L decompile-problematic-term Tl L',
364362
NL = tc.link.llam Z (app[Y | Tl]).
365363

366-
% TODO: complete this fold
367364
decompile-problematic-term (app[X|S]) L Z L :-
368365
var X _ Scope,
369366
std.append Scope S Scope',
370367
distinct_names Scope', !,
371368
get-uva-pair-arity X S Y,
372369
prune Z Scope', var Z Y Scope'.
373370

374-
decompile-problematic-term A L A L :- var A, !.
375-
376-
decompile-problematic-term (fun N Ty Bo) L (fun N Ty' Bo') L3 :-
377-
(pi x\ fold-map (Bo x) [] (Bo' x) (Lx x)),
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)),
378373
close-term-no-prune-ty Lx {tc.compile.instance.clean-term Ty} L1,
379-
fold-map Ty L Ty' L2,
374+
decompile-problematic-term Ty L Ty' L2,
380375
std.append L2 L1 L3.
381376

382-
decompile-problematic-term (prod N Ty Bo) L (prod N Ty' Bo') L3 :-
383-
(pi x\ fold-map (Bo x) [] (Bo' x) (Lx x)),
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)),
384379
close-term-no-prune-ty Lx {tc.compile.instance.clean-term Ty} L1,
385-
fold-map Ty L Ty' L2,
380+
decompile-problematic-term Ty L Ty' L2,
386381
std.append L2 L1 L3.
387382

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+
388397
pred compile i:term, i:list prop, o:term, o:list prop.
389-
compile T L T' L' :-
390-
(pi t l t' l'\ fold-map t l t' l' :- decompile-problematic-term t l t' l', !) =>
391-
fold-map T L T' L'.
398+
compile T L T' L' :- decompile-problematic-term T L T' L'.
392399

393400
% Uva Binders LinkEta
394401
pred build-eta-links-of-vars-aux i:term, i:list term, o:list prop.

apps/tc/elpi/ho_precompile.elpi

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -236,28 +236,26 @@ namespace tc {
236236
close-term-prune-safe-fun Xs Ty Xs'.
237237

238238
pred precompile-aux i:term, i:list term, o:term, o:list term.
239-
precompile-aux X A Y A :- name X, !, X = Y, !. % avoid loading "precompile-aux x A x A" at binders
240239
precompile-aux (global _ as C) A C A :- !.
241240
precompile-aux (pglobal _ _ as C) A C A :- !.
242241
precompile-aux (sort _ as C) A C A :- !.
243242
precompile-aux (primitive _ as C) A C A :- !.
244243

245-
precompile-aux T A (tc.coercion T Scope) A :-
246-
coq.safe-dest-app T HD _, tc.coercion-unify HD, !, names Scope.
244+
precompile-aux (app [HD | _] as T) A (tc.coercion T Scope) A :-
245+
tc.coercion-unify HD, !, names Scope.
247246
precompile-aux (app [global (const C) | _] as T) A (tc.canonical-projection T Scope N) A :-
248247
coq.env.projection? C N, !, names Scope.
249248
precompile-aux (app [primitive (proj P _) | _] as T) A (tc.canonical-projection T Scope 0) A :-
250249
coq.env.primitive-projection? P _, !, names Scope.
251250

252-
253251
% Detect maybe-eta term
254252
precompile-aux (fun Name Ty B as T) N (tc.maybe-eta-tm (fun Name Ty' B') Scope) M :-
255253
maybe-eta T, !,
256254
names Scope,
257255
std.assert! (pi x\ precompile-aux (B x) N (B' x) (MM x), close-term-prune-safe-fun MM Ty M') "[TC] should not fail1",
258256
precompile-aux Ty M' Ty' M.
259257

260-
% Detect maybe-beta term
258+
% Detect maybe-llam term
261259
precompile-aux (app [X|XS]) N (tc.maybe-llam-tm (app [app[X | PF] | NPF1]) Scope1) [X|M] :-
262260
var X _ Scope, split-pf XS Scope PF NPF,
263261
not (NPF = []), !, % else XS is a list of distinct names, i.e. `app [X|XS]` is in PF
@@ -281,7 +279,8 @@ namespace tc {
281279
precompile-aux (match T Rty B) A (match T1 Rty1 B1) A3 :- !,
282280
precompile-aux T A T1 A1, precompile-aux Rty A1 Rty1 A2, std.fold-map B A2 precompile-aux B1 A3.
283281
precompile-aux (primitive _ as C) A C A :- !.
284-
precompile-aux X A X [X|A] :- var X, !.
282+
precompile-aux (uvar _ _ as X) A X [X|A] :- !.
283+
precompile-aux X A Y A :- name X, !, X = Y, !. % avoid loading "precompile-aux x A x A" at binders
285284
}
286285

287286
pred goal i:term, o:term, o:list term.

0 commit comments

Comments
 (0)