Skip to content

Commit 016d6d0

Browse files
committed
[TC] add TC.AddRecordFields command + better chr for canon struct
1 parent 9656283 commit 016d6d0

File tree

8 files changed

+78
-8
lines changed

8 files changed

+78
-8
lines changed

apps/cs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ The `cs` predicate lives in the database `cs.db`
1616
pred cs i:goal-ctx, o:term, o:term.
1717
```
1818

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

apps/tc/elpi/ho_link.elpi

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,24 @@ namespace tc {
131131
}
132132

133133
namespace cs {
134+
pred reduce-cs i:term, i:term, i:term, i:constant.
135+
reduce-cs V (app [ProjT, T]) Record Proj :-
136+
coq.unify-eq T Record Err, Err = ok,
137+
Q = [coq.redflags.const Proj],
138+
coq.redflags.add coq.redflags.nored [coq.redflags.delta, coq.redflags.beta, coq.redflags.match | Q] RF,
139+
@redflags! RF => coq.reduction.lazy.whd (app [ProjT, Record]) V',
140+
not (V' = match _ _ _), !,
141+
cs V V'.
142+
reduce-cs V T _ _ :- V = T.
143+
134144
pred cs i:term, i:term.
135-
% TODO: suspend link on vars in T
136-
cs V T :- var V, !, get-vars T Vars, declare_constraint (cs V T) [_, V | Vars].
137-
cs T1 T2 :- coq.unify-eq T1 T2 ok.
145+
cs T1 T2 :- not (var T1), !, coq.unify-eq T1 T2 ok.
146+
cs (uvar _ as V) (app [_, Arg] as T) :- not (ground_term Arg), !, get-vars T Vars, declare_constraint (cs V T) [_, V | Vars].
147+
cs (uvar _ as V) (app [HD | _] as T) :-
148+
if (HD = global (const Proj), tc.proj->record Proj Record)
149+
(reduce-cs V T Record Proj)
150+
(coq.unify-eq V T ok).
151+
cs T1 T2 :- not (T2 = app _), !, coq.unify-eq T1 T2 ok.
138152

139153
% pred unify-rebinding-names i:list prop, i:list prop, i:term, i:term.
140154
% unify-rebinding-names [] T1 [] T2 (unify-eq T1V T2) :- !, copy T1 T1V.

apps/tc/elpi/ho_precompile.elpi

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,17 @@ namespace tc {
224224
split-pf [X|Xs] Old [X|Ys] L :- name X, not (std.mem! Old X), !, split-pf Xs [X|Old] Ys L.
225225
split-pf Xs _ [] Xs.
226226

227+
pred used i:term, i:term.
228+
used X (uvar _ S) :- std.mem! S X, !.
229+
used X (fun _ _ Bo) :- pi x\ used X (Bo x).
230+
231+
pred close-term-prune-safe-fun i:(term -> list term), i:term, o:list term.
232+
close-term-prune-safe-fun (x\ []) _ [] :- !.
233+
close-term-prune-safe-fun (x\ [X x | Xs x]) Ty [fun _ _ X | Xs'] :-
234+
pi x\ used x (X x), !, close-term-prune-safe-fun Xs Ty Xs'.
235+
close-term-prune-safe-fun (x\ [X | Xs x]) Ty [X | Xs'] :-
236+
close-term-prune-safe-fun Xs Ty Xs'.
237+
227238
pred precompile-aux i:term, i:list term, o:term, o:list term.
228239
precompile-aux X A Y A :- name X, !, X = Y, !. % avoid loading "precompile-aux x A x A" at binders
229240
precompile-aux (global _ as C) A C A :- !.
@@ -243,7 +254,7 @@ namespace tc {
243254
precompile-aux (fun Name Ty B as T) N (tc.maybe-eta-tm (fun Name Ty' B') Scope) M :-
244255
maybe-eta T, !,
245256
names Scope,
246-
std.assert! (pi x\ precompile-aux (B x) N (B' x) (MM x), close-term-no-prune-fun MM Ty M') "[TC] should not fail1",
257+
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",
247258
precompile-aux Ty M' Ty' M.
248259

249260
% Detect maybe-beta term
@@ -255,7 +266,7 @@ namespace tc {
255266

256267
precompile-aux (prod Name Ty B) N (tc.prod-range (prod Name Ty' B') (r-ar z MaxAr)) P :- !,
257268
count-prod Ty MaxAr,
258-
std.assert! (pi x\ precompile-aux (B x) N (B' x) (MM x), close-term-no-prune-fun MM Ty M) "[TC] should not fail2",
269+
std.assert! (pi x\ precompile-aux (B x) N (B' x) (MM x), close-term-prune-safe-fun MM Ty M) "[TC] should not fail2",
259270
precompile-aux Ty M Ty' P.
260271

261272
% Working with fun

apps/tc/elpi/solver.elpi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace tc {
1818
pred refine-proof i:term, i:goal, o:list sealed-goal.
1919
refine-proof tc.mode_fail G [seal G] :- !.
2020
refine-proof Proof G GL :-
21+
if-true print-goal-after-search (coq.say "[TC] The goal after instance search is <<<" G ">>>"),
2122
if-true print-solution (coq.say "[TC] The proof is <<<" Proof ">>>"),
2223
if-true print-solution-pp (coq.say "[TC] The proof is <<<" {coq.term->string Proof} ">>>"),
2324

@@ -65,6 +66,7 @@ namespace tc {
6566
pred print-solution-pp. % Print the solution in coq pp
6667
pred print-goal. % Print the goal in HOAS
6768
pred print-goal-pp. % Print the goal with coq pp
69+
pred print-goal-after-search. % Print the goal in HOAS after instance search
6870
pred print-compiled-goal.
6971

7072
}

apps/tc/tests/canonical_struct.v

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Module CS1.
77
}.
88
Canonical ofe_nat : ofe := Ofe nat.
99

10+
TC.AddRecordFields (ofe) (Ofe) 1.
11+
1012
Class C (I : Type).
1113

1214
Instance c : C (ofe_car ofe_nat). Qed.
@@ -126,6 +128,8 @@ End CS6.
126128
Module CS7.
127129
Structure ofe := Ofe { ofe_car :> Type; }.
128130
Structure cmra := Cmra { cmra_car :> Type; }.
131+
TC.AddRecordFields (ofe) (Ofe) 1.
132+
TC.AddRecordFields (cmra) (Cmra) 1.
129133

130134
Coercion cmra_ofeO (A : cmra) := Ofe A.
131135

@@ -142,7 +146,6 @@ Module CS7.
142146
Instance d : D bool := {}.
143147

144148
Instance i: forall (c : cmra), D (cmra_car c) -> C (cmra_ofeO c) := {}.
145-
Elpi Print TC.Solver.
146149

147150
Goal C ofe_bool.
148151
apply _.
@@ -206,6 +209,7 @@ End CS9.
206209
Module CS10.
207210
Structure ofe := Ofe { ofe_car :> Type; }.
208211
Canonical Structure ofe_bool := Ofe bool.
212+
TC.AddRecordFields (ofe) (Ofe) 1.
209213

210214
Class C (i : Type).
211215
Instance i : C bool := {}.
@@ -227,5 +231,5 @@ Module CS11.
227231
(* Here the projection is in the goal *)
228232
Goal exists X, C (ofe_car X). eexists.
229233
apply _.
230-
Show Proof.
231234
Qed.
235+
End CS11.

apps/tc/tests/test.v

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,14 @@ Module HO_12.
289289
Class Unit (i : Prop).
290290
Instance i : Unit (forall x, x) := {}.
291291
Set Printing Existential Instances.
292+
293+
Elpi Accumulate TC.Solver lp:{{
294+
% TODO: this rule should be removed if https://github.com/LPCIC/elpi/issues/256
295+
% is solved
296+
tc-elpi.apps.tc.tests.test.HO_12.tc-Unit {{forall (x:Prop), lp:(X x)}} {{i}} :-
297+
X = (x\x).
298+
}}.
299+
292300
Goal forall (y: Prop), exists (F: Prop -> Prop), Unit (forall x, F x).
293301
intros.
294302
eexists ?[F].

apps/tc/theories/add_commands.v

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,36 @@ Elpi Accumulate lp:{{
150150
}}.
151151
Elpi Typecheck.
152152

153+
Elpi Command TC.AddRecordFields.
154+
Elpi Accumulate Db tc.db.
155+
Elpi Accumulate Db tc_options.db.
156+
Elpi Accumulate File base.
157+
Elpi Accumulate File tc_aux.
158+
Elpi Accumulate lp:{{
159+
pred tc.add_tc.records_unif.aux i:int, i:term, i:list term, i:constant, o:prop.
160+
tc.add_tc.records_unif.aux 0 T Args ProjConstant P :- !,
161+
coq.mk-app T Args T',
162+
P = tc.proj->record ProjConstant T'.
163+
tc.add_tc.records_unif.aux N T Args ProjConstant (pi x\ P x) :- N > 0, !,
164+
N1 is N - 1, pi x\ tc.add_tc.records_unif.aux N1 T [x|Args] ProjConstant (P x).
165+
166+
pred tc.add_tc.records_unif.aux' i:option constant, i:term, i:int.
167+
tc.add_tc.records_unif.aux' none _ _.
168+
tc.add_tc.records_unif.aux' (some ProjConstant) RecordConstr N :-
169+
tc.add_tc.records_unif.aux N RecordConstr [] ProjConstant P, tc.add-tc-db _ _ P.
170+
171+
pred tc.add_tc.records_unif i:term, o:term, i:int.
172+
tc.add_tc.records_unif (global (indt K)) RecordConstr N :-
173+
coq.env.projections K Projs,
174+
std.forall Projs (x\ tc.add_tc.records_unif.aux' x RecordConstr N).
175+
176+
main [trm T1, trm T2, int N] :- !, tc.add_tc.records_unif T1 T2 N.
177+
main L :- coq.error L.
178+
}}.
179+
Elpi Typecheck.
153180

154181
Elpi Export TC.AddAllClasses.
182+
Elpi Export TC.AddRecordFields.
155183
Elpi Export TC.AddAllInstances.
156184
Elpi Export TC.AddClasses.
157185
Elpi Export TC.AddInstances.

apps/tc/theories/db.v

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,8 @@ Elpi Db tc.db lp:{{
9898
pred dummy.
9999

100100
pred ho-link o:term, i:term, o:A.
101+
102+
% relates a projection to the its record
103+
pred proj->record i:constant, o:term.
101104
}
102105
}}.

0 commit comments

Comments
 (0)