Skip to content

Commit 30508c0

Browse files
committed
[TC] canonical proj solved with coq.unify-eq
1 parent ee42b2d commit 30508c0

File tree

4 files changed

+116
-1
lines changed

4 files changed

+116
-1
lines changed

apps/tc/elpi/ho_compile.elpi

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ namespace tc {
2222
decompile-term-aux (sort _ as T) L T' L :- !, copy T T', !.
2323
decompile-term-aux (uvar as X) L X L :- !.
2424
decompile-term-aux (primitive _ as X) L X L :- !.
25+
decompile-term-aux (tc.canonical-projection T S _) (pr [X|Xs] L1) Y (pr Xs' L3) :- !,
26+
name Y X S,
27+
decompile-term-aux T (pr Xs L1) T' (pr Xs' L2),
28+
P = coq.unify-eq Y T' ok,
29+
L3 = [P | L2].
2530

2631
decompile-term-aux (tc.maybe-eta-tm T S) (pr [X|XS] L1) Y (pr XS' [NL | L2]) :- !,
2732
name Y X S,

apps/tc/elpi/ho_precompile.elpi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ namespace tc {
105105
precompile-aux _ (pglobal _ _ as C) A C A :- !.
106106
precompile-aux _ (sort _ as C) A C A :- !.
107107
precompile-aux _ (primitive _ as C) A C A :- !.
108+
precompile-aux _ (app [global (const C) | _] as T) A (tc.canonical-projection T Scope N) (s A) :- coq.env.projection? C N, !, free-var Scope.
109+
precompile-aux _ (app [primitive (proj P _) | _] as T) A (tc.canonical-projection T Scope 0) (s A) :- coq.env.primitive-projection? P _, !, free-var Scope.
108110

109111
% Detect maybe-eta term
110112
% TODO: should I precompile also the type of the fun and put it in the output term
@@ -172,7 +174,7 @@ namespace tc {
172174
(tc.prod-range (prod _ T Bo) 3)
173175
since x is applied at most 3 times in Bo
174176
==> This helps charging the right number of `eta-link` for map-deduplication rule
175-
N is the number of problematic terms in T
177+
N is the number of problematic terms in T + nb of projections
176178
*/
177179
pred instance i:term, o:term, o:nat, o:list univ, o:list univ-instance.
178180
instance T T' N UnivConstL UnivInstL :-

apps/tc/elpi/tc_aux.elpi

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,9 @@ type tc.maybe-llam-tm
211211
term -> % The current precompiled subterm: shape is app[app[X,PF],NPF]
212212
list term -> % The eta-expanded version of X, from X^{len(PF)} to X^{len(PF)+len(NPF)}
213213
term.
214+
215+
type tc.canonical-projection
216+
term -> % The current precompiled subterm
217+
list term -> % The list of FV in the precomp subterm
218+
int ->
219+
term.

apps/tc/tests/canonical_struct.v

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
From elpi.apps Require Import tc.
2+
3+
Module CS1.
4+
5+
Structure ofe := Ofe {
6+
ofe_car :> Type;
7+
}.
8+
Canonical ofe_nat : ofe := Ofe nat.
9+
10+
Class C (I : Type).
11+
12+
Instance c : C (ofe_car ofe_nat). Qed.
13+
Goal C (Ofe nat). apply _. Qed. (* No CS search *)
14+
Goal exists x, C (Ofe x). eexists. apply _. Qed. (* CS Search to assign x *)
15+
Goal C (ofe_car ofe_nat). apply _. Qed.
16+
17+
End CS1.
18+
19+
Module CS2.
20+
Structure ofe := Ofe {
21+
ofe_car :> Type;
22+
}.
23+
Canonical ofe_nat : ofe := Ofe nat.
24+
25+
Class C (I : Type).
26+
27+
Instance c : C (Ofe nat). Qed.
28+
Goal C (Ofe nat). apply _. Qed. (* No CS search *)
29+
Goal exists x, C (Ofe x). eexists. apply _. Qed. (* CS Search to assign x *)
30+
Goal C (ofe_car ofe_nat). apply _. Qed.
31+
32+
End CS2.
33+
34+
(* With primitive projection *)
35+
Module CS3.
36+
Class C (I : Type).
37+
38+
#[local] Set Primitive Projections.
39+
Structure ofe := Ofe {
40+
ofe_car :> Type;
41+
}.
42+
Canonical ofe_nat : ofe := Ofe nat.
43+
44+
Instance c : C ofe_nat.(ofe_car). Qed.
45+
Goal C (Ofe nat). apply _. Qed. (* No CS search *)
46+
Goal exists x, C (Ofe x). eexists. apply _. Qed. (* CS Search to assign x *)
47+
Goal C (ofe_car ofe_nat). apply _. Qed.
48+
49+
End CS3.
50+
51+
Module CS4.
52+
Class C (I : Type).
53+
54+
#[local] Set Primitive Projections.
55+
Structure ofe := Ofe {
56+
ofe_car :> Type;
57+
}.
58+
Canonical ofe_nat : ofe := Ofe nat.
59+
60+
Instance c : C (Ofe nat).(ofe_car). Qed.
61+
Goal C (Ofe nat). apply _. Qed. (* No CS search *)
62+
Goal exists x, C (Ofe x). eexists. apply _. Qed. (* CS Search to assign x *)
63+
Goal C (ofe_car ofe_nat). apply _. Qed.
64+
65+
End CS4.
66+
67+
(* With bound variables *)
68+
Module CS5.
69+
Structure ofe := Ofe {
70+
ofe_car :> Type;
71+
}.
72+
Canonical ofe_any x : ofe := Ofe x.
73+
74+
Class C (I : Type -> Type).
75+
Axiom f : ofe -> Type.
76+
77+
Instance c X : C (fun x => (Ofe (f (X x)))). Qed.
78+
Goal C (fun x => (Ofe (f x))). apply _. Qed. (* No CS search *)
79+
Goal C (fun (_: Type) => Ofe (f nat)). apply _. Qed. (* CS Search to assign x *)
80+
Goal exists X, C (fun y => ofe_car (X y)). eexists.
81+
apply _.
82+
Unshelve.
83+
apply (ofe_any nat).
84+
Qed.
85+
86+
Lemma coq_unif_fail: exists X Y, (fun (y: Type) => ofe_car (X y)) = (fun _ => Y).
87+
do 2 eexists.
88+
Fail reflexivity.
89+
Abort.
90+
91+
Goal exists Z, C (fun _ => Z). eexists.
92+
(*
93+
Note that here, coq8.20 unification algorithm would fail to unify
94+
with error
95+
Unable to unify "?Y" with "ofe_car (?X x)" as "ofe_car (?X x)"
96+
contains local variables.
97+
*)
98+
apply _.
99+
Unshelve.
100+
apply (ofe_any nat).
101+
Qed.
102+
End CS5.

0 commit comments

Comments
 (0)