Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TC] CS projections replaced with uvar #648

Open
wants to merge 32 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
6105b05
[TC] primitive constructor for fold in instance (pre-)compilation
FissoreD Jul 2, 2024
d01790e
[TC] canonical proj solved with coq.unify-eq
FissoreD Jul 2, 2024
681e9c1
[TC] clean-term also for tc.canonical-projection
FissoreD Jul 3, 2024
f8f6439
[TC] compilation of primitive projections to their compatibility cons…
FissoreD Jul 10, 2024
5564012
Revert "[TC] compilation of primitive projections to their compatibil…
FissoreD Jul 10, 2024
11e5ae9
[TC] put cnonical projection in tc namespace
FissoreD Jul 12, 2024
d182f9f
remove reduntant calls to Elpi Typecheck
FissoreD Jul 12, 2024
fc58cf4
[TC] local binders in evar scope during precompile + link.cs
FissoreD Jul 30, 2024
a9525a1
[TC] add TC.AddRecordFields command + better chr for canon struct
FissoreD Aug 2, 2024
d860b4c
[TC] mode ground with ground_term check + tc.link for coercions
FissoreD Aug 8, 2024
9dbf2f1
[TC] clean term in decompilation of goal
FissoreD Aug 8, 2024
431e9ba
[TC] add file deps in tests/importOrder/sameOrderCommand.v
FissoreD Aug 8, 2024
82ef753
[TC] perf: fold-map fully implemented in precompile goal
FissoreD Aug 8, 2024
1aa4bae
[TC] improve perf of goal compilation
FissoreD Aug 10, 2024
b7ea708
[TC] remove goal precomp and goal compile (all is done in ycompile_go…
FissoreD Aug 10, 2024
33973ca
[TC] refactor filenames + reorder import
FissoreD Aug 10, 2024
1c77fec
[TC] refactor of build-eta-llam-links
FissoreD Aug 10, 2024
a496c70
[TC] avoid a backtrack
FissoreD Aug 20, 2024
b4a4dd2
[TC] clean compile-conclusion predicate
FissoreD Sep 19, 2024
35e25ee
[TC] link small refactor
FissoreD Aug 22, 2024
a59eca0
[TC] clean link code + unification with cs links only after a call to…
FissoreD Sep 2, 2024
02dc4cb
add _opam to .gitignore
FissoreD Sep 3, 2024
9b149b4
[TC] clearn-term on pi-decl in decompile-term-aux
FissoreD Sep 3, 2024
568fc98
[TC] dummy clauses are local to sections
FissoreD Sep 19, 2024
9fe73be
[TC] add comment
FissoreD Sep 25, 2024
459da6e
[TC] move time-is-active and coercion-unify in db.v
FissoreD Sep 25, 2024
711ff60
cleanup
gares Oct 2, 2024
279e321
cleanup
gares Oct 2, 2024
2f03fa8
cleanup
gares Oct 2, 2024
7986110
cleanup
gares Oct 2, 2024
eb81aa4
ci
gares Oct 2, 2024
5ac62d2
nix
gares Oct 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@ _build
tmp.out
coq-elpi-tests.opam
coq-elpi-tests.install
coq-elpi.install
coq-elpi.install
2 changes: 1 addition & 1 deletion apps/cs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The `cs` predicate lives in the database `cs.db`
pred cs i:goal-ctx, o:term, o:term.
```

By addings rules for this predicate one can recover from a CS instance search failure
By adding rules for this predicate one can recover from a CS instance search failure
error, that is when `Lhs` and `Rhs` are not unifiable using a canonical structure registered
by Coq.

Expand Down
131 changes: 0 additions & 131 deletions apps/tc/elpi/WIP/force_llam.elpi

This file was deleted.

44 changes: 0 additions & 44 deletions apps/tc/elpi/WIP/modes.elpi

This file was deleted.

42 changes: 17 additions & 25 deletions apps/tc/elpi/base.elpi
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,13 @@ split-at-not-fatal N [X|XS] [X|LN] LM :- !, N1 is N - 1,

pred undup-same i:list A, o:list A.
undup-same [] [].
undup-same [X|Xs] [X|Ys] :-
std.forall Xs (x\ not (x == X)), !,
undup-same Xs Ys.
undup-same [_|Xs] Ys :- undup-same Xs Ys.
undup-same [X|Xs] Ys :- std.exists Xs (x\ x == X), !, undup-same Xs Ys.
undup-same [X|Xs] [X|Ys] :- undup-same Xs Ys.

pred undup i:list A, o:list A.
undup [] [].
undup [X|Xs] Ys :- std.mem! Xs X, !, undup Xs Ys.
undup [X|Xs] [X|Ys] :- undup Xs Ys.

:index (1)
pred is-coq-term i:A.
Expand Down Expand Up @@ -142,24 +145,13 @@ pred count-prod i:term , o:nat.
count-prod (prod _ _ B) (s N) :- !, pi x\ count-prod (B x) N.
count-prod _ z.

pred close-prop i:(A -> list prop), o:list prop.
close-prop (x\ []) [] :- !.
close-prop (x\ [X | Xs x]) [X| Xs'] :- !, close-prop Xs Xs'.
close-prop (x\ [X x | Xs x]) [pi x\ X x | Xs'] :- !, close-prop Xs Xs'.

pred close-prop-no-prune i:(A -> list prop), o:list prop.
close-prop-no-prune (x\ []) [] :- !.
close-prop-no-prune (x\ [X x | Xs x]) [pi x\ X x | Xs'] :- !,
close-prop-no-prune Xs Xs'.

% [close-term-ty (x\ L) Ty R] Ty is the type of x
pred close-term-ty i:(term -> list prop), i:term, o:list prop.
close-term-ty (x\ []) _ [] :- !.
close-term-ty (x\ [X | Xs x]) Ty [X| Xs'] :- !, close-term-ty Xs Ty Xs'.
close-term-ty (x\ [X x | Xs x]) Ty [@pi-decl `x` Ty x\ X x | Xs'] :- !,
close-term-ty Xs Ty Xs'.

pred close-term-no-prune-ty i:(term -> list prop), i:term, o:list prop.
close-term-no-prune-ty (x\ []) _ [] :- !.
close-term-no-prune-ty (x\ [X x | Xs x]) Ty [@pi-decl `x` Ty x\ X x | Xs'] :- !,
close-term-no-prune-ty Xs Ty Xs'.
pred close-prop-no-prune-pi-decl i:(term -> list prop), i:term, o:list prop.
close-prop-no-prune-pi-decl (x\ []) _ [] :- !.
close-prop-no-prune-pi-decl (x\ [X x | Xs x]) Ty [@pi-decl `x` Ty x\ X x | Xs'] :- !,
close-prop-no-prune-pi-decl Xs Ty Xs'.

pred free-names i:term, o:list term.
free-names T L :-
names N,
(pi T L\ fold-map T L T [T|L] :- name T, std.mem! N T, !) => fold-map T [] _ L',
undup {std.rev L'} L.
122 changes: 122 additions & 0 deletions apps/tc/elpi/compile_goal.elpi
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
namespace tc {
namespace compile {
namespace goal {
:index (_ _ 1)
pred may-contract-to i:list term, i:term, i:term.
may-contract-to _ N N :- !.
% TODO: here we should do var V _ Scope and use scope: N can be in Scope but not in S
may-contract-to L N (app [V|S]) :- var V, !,
std.forall [N|L] (x\ exists! S (may-contract-to [] x)).
may-contract-to L N (app [N|A]) :-
std.length A {std.length L},
std.forall2 {std.rev L} A (may-contract-to []).
may-contract-to L N (fun _ _ B) :-
pi x\ may-contract-to [x|L] N (B x).

:index (_ 1)
pred occurs-rigidly i:term, i:term.
occurs-rigidly N N :- name N, !.
occurs-rigidly _ (app [N|_]) :- var N, !, fail.
occurs-rigidly N (app A) :- exists! A (occurs-rigidly N).
occurs-rigidly N (fun _ _ B) :- pi x\ occurs-rigidly N (B x).

pred maybe-eta-aux i:term, i:list term.
% TODO: here we should do var V _ Scope and use Scope: an elt in L can appear in Scope
maybe-eta-aux (app[V|S]) L :- var V, !,
std.forall L (x\ exists! S (y\ may-contract-to [] x y)).
maybe-eta-aux (app [_|A]) L :-
SplitLen is {std.length A} - {std.length L},
split-at-not-fatal SplitLen A HD TL,
std.forall L (x\ not (exists! HD (occurs-rigidly x))),
std.forall2 {std.rev L} TL (may-contract-to []).
maybe-eta-aux (fun _ _ B) L :-
pi x\ maybe-eta-aux (B x) [x|L].

pred maybe-eta i:term.
maybe-eta (fun _ _ B) :- pi x\ maybe-eta-aux (B x) [x].

pred used i:term, i:term.
used X (uvar _ S) :- std.mem! S X, !.
used X (fun _ _ Bo) :- pi x\ used X (Bo x).

pred close-term-prune-safe-fun i:(term -> list term), i:term, o:list term.
close-term-prune-safe-fun (x\ []) _ [] :- !.
close-term-prune-safe-fun (x\ [X x | Xs x]) Ty [fun _ _ X | Xs'] :-
pi x\ used x (X x), !, close-term-prune-safe-fun Xs Ty Xs'.
close-term-prune-safe-fun (x\ [X | Xs x]) Ty [X | Xs'] :-
close-term-prune-safe-fun Xs Ty Xs'.

pred compile-full-aux-close i:(term -> term), i:term, i:list prop, o:(term -> term), o:list prop.
compile-full-aux-close Bo Ty L Bo' L' :-
(pi x\ compile-full-aux (Bo x) [] (Bo' x) (BoL x)),
% TODO: maybe attach to the links the list of used binders, to simply make this check?
% Maybe L' is a pair list links and list binders per link, this way we can easily prune
close-prop-no-prune-pi-decl BoL Ty BoL',
std.append L BoL' L'.

pred compile-full-aux i:term, i:list prop, o:term, o:list prop.
compile-full-aux (global _ as G) L G L :- !.
compile-full-aux (pglobal _ _ as G) L G L :- !.
compile-full-aux (sort _ as G) L G L :- !.

% Link for coercion
compile-full-aux (app [HD|_] as G) L G' [tc.link.cs G' G | L] :- tc.coercion-unify HD, !.

% Link for canonical structure
compile-full-aux (app [global (const C) | _] as G) L G' [tc.link.cs G' G | L] :- coq.env.projection? C _, !.

% Link for primitive projection
compile-full-aux (app [primitive (proj P _) | _] as G) L G' [tc.link.cs G' G | L] :- coq.env.primitive-projection? P _, !.

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

compile-full-aux (fun Name Ty Bo) L (fun Name Ty' Bo') L' :- !,
compile-full-aux Ty L Ty' LTy,
compile-full-aux-close Bo Ty LTy Bo' L'.

compile-full-aux (prod Name Ty Bo) L (prod Name Ty' Bo') L' :- !,
compile-full-aux Ty L Ty' LTy,
compile-full-aux-close Bo Ty LTy Bo' L'.

% TODO: to avoid too many chain for the same var, maybe pass a list into the fold
compile-full-aux (app [(uvar _ _ as V) | S]) L G' L'' :- !,
std.fold-map S L compile-full-aux S' L', % L' = Links built for S
tc.link.build-eta-llam-links (app [V|S']) L' G' L''.

compile-full-aux (app L) A (app L1) A1 :- !, std.fold-map L A compile-full-aux L1 A1.

compile-full-aux (let N T B F) A (let N T1 B1 F1) A3 :- !,
compile-full-aux T A T1 A1, compile-full-aux B A1 B1 A2,
compile-full-aux-close F T A2 F1 A3.

compile-full-aux (fix N Rno Ty F) A (fix N Rno Ty1 F1) A2 :- !,
compile-full-aux Ty A Ty1 A1,
compile-full-aux-close F Ty A1 F1 A2.

compile-full-aux (match T Rty B) A (match T1 Rty1 B1) A3 :- !,
compile-full-aux T A T1 A1,
compile-full-aux Rty A1 Rty1 A2,
std.fold-map B A2 compile-full-aux B1 A3.

compile-full-aux (uvar _ _ as X) A X A :- !.
compile-full-aux X A X A :- name X, !.

compile-full-aux A B _ _ :- coq.error "Fail to compile-full-aux" A B.

% takes a term t and returns a term t' and a list of links.
pred compile-full i:term, o:term, o:list prop.
compile-full Goal Goal' Links :- compile-full-aux Goal [] Goal' Links.
}

% [goal T T' L] takes a term T and returns a new term T' where problematic
% subterms (see this: https://dl.acm.org/doi/10.1145/3678232.3678233)
% are replaced with fresh variables. The list of links is L
pred goal i:term, o:term, o:list prop.
:name "compile-goal"
goal Goal Goal' Links :-
goal.compile-full-aux Goal [] Goal' Links.
}
}
Loading
Loading