Skip to content

Commit b92e2c8

Browse files
authored
Merge pull request #556 from FissoreD/warnings+get-inst-prio-api
TC : API coq.TC.get-inst-prio, warnings on hints
2 parents 0369d06 + d03e821 commit b92e2c8

15 files changed

+172
-97
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- Change `tc-instance`, now the type is `gref -> tc-priority -> tc-instance` i.e. the priority is not an integer anymore
1313
- New `coq.ltac.fresh-id` to generate fresh names in the proof context
1414
- New `@no-tc!` attribute supported by `coq.ltac.call-ltac1`
15+
- New `coq.TC.get-inst-prio` returns the `tc-priority` of an instance
1516

1617
### Apps
1718
- New `tc` app providing an implementation of a type class solver written in elpi.

apps/tc/_CoqProject.test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
-arg -w -arg -Not-added
2+
-arg -w -arg -TC.hints
23

34
# Hack to see Coq-Elpi even if it is not installed yet
45
-Q ../../theories elpi

apps/tc/elpi/alias.elpi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/* license: GNU Lesser General Public License Version 2.1 or later */
22
/* ------------------------------------------------------------------------- */
33

4+
accumulate base.
5+
46
pred alias i:term, o:term.
57

68
pred replace-with-alias.aux i:list term, o:list term, o:bool.

apps/tc/elpi/compiler.elpi

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
/* license: GNU Lesser General Public License Version 2.1 or later */
22
/* ------------------------------------------------------------------------- */
33

4+
accumulate base.
5+
accumulate tc_aux.
6+
47
% returns the classes on which the current gref depends
58
pred get-class-dependencies i:gref, o:list gref.
69
get-class-dependencies GR Res :-
@@ -97,7 +100,7 @@ compile-aux (prod N T F) I RevPremises ListVar [] IsPositive IsHead Clause ff :-
97100
(L = [NewPremise | RevPremises])) (L = RevPremises),
98101
compile-aux (F p) I L [p | ListVar] [] IsPositive IsHead (C p) _.
99102
compile-aux Ty I RevPremises ListVar [] _ IsHead Clause tt :-
100-
not (is-option-active {oTC-use-pattern-fragment-compiler}), !,
103+
not (is-option-active oTC-use-pattern-fragment-compiler), !,
101104
std.rev RevPremises Premises,
102105
coq.mk-app I {std.rev ListVar} AppInst,
103106
make-tc IsHead Ty AppInst Premises Clause.

apps/tc/elpi/create_tc_predicate.elpi

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
/* license: GNU Lesser General Public License Version 2.1 or later */
22
/* ------------------------------------------------------------------------- */
3+
4+
accumulate base.
5+
accumulate tc_aux.
6+
37
pred string->coq-mode i:string, o:hint-mode.
48
string->coq-mode "bang" mode-ground :- !.
59
string->coq-mode "plus" mode-input :- !.
@@ -14,14 +18,19 @@ coq-mode->elpi mode-ground (pr in "term"). % approximation
1418
coq-mode->elpi mode-input (pr in "term").
1519
coq-mode->elpi mode-output (pr out "term").
1620

21+
% Here we build the elpi modes a class CL. If CL has either zero or more than
22+
% one mode, then we consider all its parameter to be in output mode. If the
23+
% class has exactly one mode, then it is considered for the signature of the
24+
% predicate for CL
1725
pred modes-of-class i:gref, o:list (pair argument_mode string).
18-
modes-of-class ClassGr Modes :-
19-
coq.hints.modes ClassGr "typeclass_instances" CoqModesList,
26+
modes-of-class ClassGR Modes :-
27+
coq.hints.modes ClassGR "typeclass_instances" CoqModesList,
2028
not (CoqModesList = []),
21-
std.assert! (CoqModesList = [HintModesFst]) "At the moment we only allow TC with one Hint Mode",
29+
not (CoqModesList = [_,_|_], coq.warning "TC.Modes" "At the moment we only allow TC with at most 1 hint Mode (caused by class" {coq.gref->string ClassGR} ")"),
30+
CoqModesList = [HintModesFst],
2231
std.append {std.map HintModesFst coq-mode->elpi} [pr out "term"] Modes.
23-
modes-of-class ClassGr Modes :-
24-
coq.env.typeof ClassGr ClassTy,
32+
modes-of-class ClassGR Modes :-
33+
coq.env.typeof ClassGR ClassTy,
2534
N is {coq.count-prods ClassTy} + 1, % + 1 for the solution
2635
list-init N (x\r\ r = (pr out "term")) Modes.
2736

apps/tc/elpi/parser_addInstances.elpi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
/* license: GNU Lesser General Public License Version 2.1 or later */
22
/* ------------------------------------------------------------------------- */
33

4+
accumulate base.
5+
accumulate tc_aux.
6+
accumulate compiler.
7+
48
kind enum type.
59
type path string -> string -> enum.
610
type addInstPrio int -> string -> enum.

apps/tc/elpi/rewrite_forward.elpi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/* license: GNU Lesser General Public License Version 2.1 or later */
22
/* ------------------------------------------------------------------------- */
33

4+
accumulate base.
5+
46
pred forward i:term, o:term, o:list (pair (list term) term).
57

68
% Auxiliary function for rewrite-forward

apps/tc/elpi/solver.elpi

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
/* license: GNU Lesser General Public License Version 2.1 or later */
22
/* ------------------------------------------------------------------------- */
33

4+
accumulate base.
5+
6+
pred time-solve i:prop.
7+
time-solve P :-
8+
std.time P Time,
9+
if (is-option-active oTC-time) (coq.say "[TC] Total resolution time is:" Time) true.
10+
411
msolve L N :- !,
5-
coq.ltac.all (coq.ltac.open solve) {std.rev L} N.
12+
time-solve (coq.ltac.all (coq.ltac.open solve-aux) {std.rev L} N).
13+
14+
solve A L :- time-solve (solve-aux A L).
15+
616

717
pred build-context-clauses i:list prop, o:list prop.
818
% Add the section's definition to the given context
@@ -20,48 +30,49 @@ tc-recursive-search Ty Sol :-
2030
std.time (coq.safe-dest-app Ty (global TC) TL',
2131
std.append TL' [Sol] TL,
2232
coq.elpi.predicate {gref->pred-name TC} TL Q, Q) Time,
23-
if (is-option-active {oTC-resolution-time}) (coq.say "Instance search" Time) true.
33+
if (is-option-active oTC-time-instance-search) (coq.say "[TC] Instance search time is:" Time) true.
2434

2535
:if "solve-print-goal"
2636
solve (goal Ctx _ Ty _ _) _ :-
2737
coq.say "Ctx" Ctx "Ty" Ty, fail.
2838

29-
% solve (goal C _ (prod N Ty F) S _ as _G) _L GL :- !,
39+
pred solve-aux i:goal, o:list sealed-goal.
40+
% solve-aux (goal C _ (prod N Ty F) S _ as _G) _L GL :- !,
3041
% @pi-decl N Ty x\
3142
% declare-evar [decl x N Ty|C] (Raw x) (F x) (Sol x),
32-
% solve (goal [decl x N Ty|C] (Raw x) (F x) (Sol x) []) _L GL,
43+
% solve-aux (goal [decl x N Ty|C] (Raw x) (F x) (Sol x) []) _L GL,
3344
% if (Sol x = app [HD, x]) (S = HD) (S = fun N Ty Sol).
34-
% solve (goal C _ (prod N Ty F) XX _ as G) _L GL :- !,
45+
% solve-aux (goal C _ (prod N Ty F) XX _ as G) _L GL :- !,
3546
% % intros_if_needed Prod C []
3647
% (@pi-decl N Ty x\
3748
% declare-evar [decl x N Ty|C] (Raw x) (F x) (Sol x),
38-
% solve (goal [decl x N Ty|C] (Raw x) (F x) (Sol x) []) _L _,
49+
% solve-aux (goal [decl x N Ty|C] (Raw x) (F x) (Sol x) []) _L _,
3950
% coq.safe-dest-app (Sol x) Hd (Args x)),
4051
% if (pi x\ last-no-error (Args x) x, std.drop-last 1 (Args x) NewArgs)
4152
% (coq.mk-app Hd NewArgs Out, refine Out G GL) (
4253
% % coq.say "Not eta" (Sol x) x (fun N Ty Sol),
4354
% XX = (fun N Ty Sol)).
44-
% solve (goal C _ (prod N _ _ as P) _ A as G) _L GL :- !,
55+
% solve-aux (goal C _ (prod N _ _ as P) _ A as G) _L GL :- !,
4556
% declare-evar C T P S',
4657
% G' = (goal C T P S' A),
4758
% refine (fun N _ _) G' GL1,
48-
% coq.ltac.all (coq.ltac.open solve) GL1 _,
59+
% coq.ltac.all (coq.ltac.open solve-aux) GL1 _,
4960
% refine S' G GL.
50-
solve (goal C _ (prod N Ty F) _ _ as G) GL :- !,
61+
solve-aux (goal C _ (prod N Ty F) _ _ as G) GL :- !,
5162
(@pi-decl N Ty x\
5263
declare-evar [decl x N Ty|C] (Raw x) (F x) (Sol x),
53-
solve (goal [decl x N Ty|C] (Raw x) (F x) (Sol x) []) _),
64+
solve-aux (goal [decl x N Ty|C] (Raw x) (F x) (Sol x) []) _),
5465
if (pi x\
5566
% also check the head does not contain x
5667
coq.safe-dest-app (Sol x) Hd (Args x),
5768
last-no-error (Args x) x,
5869
std.drop-last 1 (Args x) NewArgs)
5970
(coq.mk-app Hd NewArgs Out, refine Out G GL1) (refine (fun N _ _) G GL1),
60-
coq.ltac.all (coq.ltac.open solve) GL1 GL.
61-
% solve (goal _ _ (prod N _ _) _ _ as G) GL :- !,
71+
coq.ltac.all (coq.ltac.open solve-aux) GL1 GL.
72+
% solve-aux (goal _ _ (prod N _ _) _ _ as G) GL :- !,
6273
% refine (fun N _ _) G GL1,
63-
% coq.ltac.all (coq.ltac.open solve) GL1 GL.
64-
solve (goal Ctx _ Ty Sol _ as G) GL :-
74+
% coq.ltac.all (coq.ltac.open solve-aux) GL1 GL.
75+
solve-aux (goal Ctx _ Ty Sol _ as G) GL :-
6576
var Sol,
6677
build-context-clauses Ctx Clauses,
6778
% @redflags! coq.redflags.beta => coq.reduction.lazy.norm Ty Ty1,
@@ -71,10 +82,10 @@ solve (goal Ctx _ Ty Sol _ as G) GL :-
7182
% coq.say "Solution " X "end" X' "caio",
7283
% std.assert! (ground_term X') "solution not complete",
7384
% (pi F\ (copy (fun _ _ x\ (app [F, x])) F :- !)) => copy X X',
74-
if (is-option-active {oTC-ignore-eta-reduction})
85+
if (is-option-active oTC-ignore-eta-reduction)
7586
(Proof' = Proof) (coq.reduction.eta-contract Proof Proof'),
7687
std.time (refine Proof' G GL) Refine-Time,
77-
if (is-option-active {oTC-time-refine}) (coq.say "Refine Time" Refine-Time) true;
88+
if (is-option-active oTC-time-refine) (coq.say "[TC] Refine time is:" Refine-Time) true;
7889
coq.error "illtyped solution:" {coq.term->string Proof}
7990
)
8091
(GL = [seal G]).

apps/tc/elpi/tc_aux.elpi

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
/* license: GNU Lesser General Public License Version 2.1 or later */
22
/* ------------------------------------------------------------------------- */
33

4+
accumulate base.
5+
46
% Contains the list of classes that
57
% cannot be compiled
68

79
% returns the TC from the type of an instance
10+
% TODO: This gould be replaced with an api
11+
% coq.TC.get-class-of-inst i:gref, o:gref
812
pred get-TC-of-inst-type i:term, o:gref.
913
get-TC-of-inst-type (prod _ _ A) GR:-
1014
pi x\ get-TC-of-inst-type (A x) GR.
@@ -67,12 +71,15 @@ get-last (app L) R :-
6771
% TC preds are on the form tc-[PATH_TO_TC].tc-[TC-Name]
6872
pred gref->pred-name i:gref, o:string.
6973
gref->pred-name Gr S :-
70-
if (is-option-active {oTC-clauseNameShortName})
74+
if (is-option-active oTC-clauseNameShortName)
7175
(Path = "")
7276
(coq.gref->path Gr [Hd | Tl],
7377
std.fold Tl Hd (x\acc\r\ r is acc ^ "." ^ x) Path',
7478
Path is Path' ^ ".tc-"),
75-
S is "tc-" ^ Path ^ {coq.gref->id Gr}.
79+
% CAVEAT : Non-ascii caractars can't be part of a pred
80+
% name, we replace ö with o
81+
rex.replace "ö" "o" {coq.gref->id Gr} GrStr,
82+
S is "tc-" ^ Path ^ GrStr.
7683

7784
pred no-backtrack i:list prop, o:list prop.
7885
no-backtrack [] [].
@@ -90,23 +97,20 @@ make-tc _IsHead Ty Inst Hyp Clause :-
9097
(Hyp = [Hd]) (Clause = (Q :- Hd))
9198
(Clause = (Q :- Hyp)).
9299

100+
pred unwrap-prio i:tc-priority, o:int.
101+
unwrap-prio (tc-priority-given Prio) Prio.
102+
unwrap-prio (tc-priority-computed Prio) Prio.
103+
93104
% returns the priority of an instance from the gref of an instance
94105
pred get-inst-prio i:gref, o:int.
95-
get-inst-prio InstGr Prio :-
96-
coq.env.typeof InstGr InstTy,
97-
get-TC-of-inst-type InstTy TC,
98-
find-opt {coq.TC.db-for TC}
99-
(x\ tc-instance InstGr (tc-priority-given Prio) = x ;
100-
tc-instance InstGr (tc-priority-computed Prio) = x)
101-
(some _), !.
102-
get-inst-prio _ 0.
103-
104-
% TODO: @FissoreD improve this method complexity
106+
get-inst-prio InstGR Prio :-
107+
coq.env.typeof InstGR InstTy,
108+
get-TC-of-inst-type InstTy ClassGR,
109+
unwrap-prio {coq.TC.get-inst-prio ClassGR InstGR} Prio.
110+
105111
pred get-full-path i:gref, o:string.
106-
% :if "get-full-path"
107112
get-full-path Gr Res' :-
108113
coq.gref->string Gr Path,
109114
coq.env.current-section-path SectionPath,
110115
std.fold SectionPath "" (e\acc\r\ r is acc ^ "." ^ e) Res,
111116
Res' is Res ^ Path.
112-
% get-full-path _ _.

apps/tc/theories/add_commands.v

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,15 @@
33

44
From elpi.apps Require Import db.
55

6-
From elpi.apps.tc Extra Dependency "base.elpi" as base.
6+
From elpi.apps.tc Extra Dependency "tc_aux.elpi" as tc_aux.
77
From elpi.apps.tc Extra Dependency "compiler.elpi" as compiler.
88
From elpi.apps.tc Extra Dependency "parser_addInstances.elpi" as parser_addInstances.
99
From elpi.apps.tc Extra Dependency "solver.elpi" as solver.
10-
From elpi.apps.tc Extra Dependency "tc_aux.elpi" as tc_aux.
1110
From elpi.apps.tc Extra Dependency "create_tc_predicate.elpi" as create_tc_predicate.
1211

1312
Elpi Command TC.AddAllInstances.
1413
Elpi Accumulate Db tc.db.
1514
Elpi Accumulate Db tc_options.db.
16-
Elpi Accumulate File base.
17-
Elpi Accumulate File tc_aux.
1815
Elpi Accumulate File compiler.
1916
Elpi Accumulate lp:{{
2017
main L :-
@@ -26,9 +23,6 @@ Elpi Typecheck.
2623
Elpi Command TC.AddInstances.
2724
Elpi Accumulate Db tc.db.
2825
Elpi Accumulate Db tc_options.db.
29-
Elpi Accumulate File base.
30-
Elpi Accumulate File tc_aux.
31-
Elpi Accumulate File compiler.
3226
Elpi Accumulate File parser_addInstances.
3327
Elpi Accumulate lp:{{
3428
main Arguments :-
@@ -39,21 +33,18 @@ Elpi Typecheck.
3933
Elpi Command TC.AddAllClasses.
4034
Elpi Accumulate Db tc.db.
4135
Elpi Accumulate Db tc_options.db.
42-
Elpi Accumulate File base.
43-
Elpi Accumulate File tc_aux.
4436
Elpi Accumulate File create_tc_predicate.
4537
Elpi Accumulate lp:{{
46-
main _ :-
47-
coq.TC.db-tc TC,
48-
std.forall TC (add-class-gr classic).
38+
% Ignore is the list of classes we do not want to add
39+
main IgnoreStr :-
40+
std.map IgnoreStr (x\r\ sigma S\ str S = x, coq.locate S r) IgnoreGR,
41+
std.forall {coq.TC.db-tc} (x\ if (std.mem IgnoreGR x) true (add-class-gr classic x)).
4942
}}.
5043
Elpi Typecheck.
5144

5245
Elpi Command TC.AddClasses.
5346
Elpi Accumulate Db tc.db.
5447
Elpi Accumulate Db tc_options.db.
55-
Elpi Accumulate File base.
56-
Elpi Accumulate File tc_aux.
5748
Elpi Accumulate File create_tc_predicate.
5849
Elpi Accumulate lp:{{
5950
main L :-
@@ -67,7 +58,6 @@ Elpi Typecheck.
6758
Elpi Command TC.AddHook.
6859
Elpi Accumulate Db tc.db.
6960
Elpi Accumulate Db tc_options.db.
70-
Elpi Accumulate File base.
7161
Elpi Accumulate File tc_aux.
7262
Elpi Accumulate lp:{{
7363
pred addHook i:grafting, i:string.
@@ -98,8 +88,6 @@ Elpi Typecheck.
9888
Elpi Command TC.Declare.
9989
Elpi Accumulate Db tc.db.
10090
Elpi Accumulate Db tc_options.db.
101-
Elpi Accumulate File base.
102-
Elpi Accumulate File tc_aux.
10391
Elpi Accumulate File create_tc_predicate.
10492
Elpi Accumulate lp:{{
10593
main [indt-decl D] :- declare-class D.

0 commit comments

Comments
 (0)