Skip to content

Commit 2f7f589

Browse files
authored
Merge pull request #540 from FissoreD/class-declare-TC-command
Locality can be charged to coq.elpi.add-predicate
2 parents 35b0393 + c652dac commit 2f7f589

File tree

8 files changed

+73
-25
lines changed

8 files changed

+73
-25
lines changed

Changelog.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
# Changelog
22

3+
## Unreleased - 09/11/2023
4+
5+
### API
6+
- Change `coq.elpi.add-predicate` now locality can be changed
7+
- Experimental `coq.toposort` returns a valid topological ordering of the nodes
8+
of a graph
9+
- Change `coq.TC.db-for`, now instances are returned sorted wrt their priority
10+
- New `tc-priority`, contains the priority of an instance and if the priority
11+
has been given by the user or computed by `coq`
12+
- Change `tc-instance`, now the type is `gref -> tc-priority -> tc-instance` i.e. the priority is not an integer anymore
13+
14+
### Apps
15+
- New `tc` app providing an implementation of a type class solver written in elpi.
16+
This app is experimental
17+
318
## [1.19.3] - 12/10/2023
419

520
Requires Elpi 1.16.5 and Coq 8.18.

apps/tc/elpi/create_tc_predicate.elpi

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,21 @@ modes-of-class ClassGr Modes :-
2525
N is {coq.count-prods ClassTy} + 1, % + 1 for the solution
2626
list-init N (x\r\ r = (pr out "term")) Modes.
2727

28+
pred get-class-locality o:list prop.
29+
get-class-locality [@local!] :- coq.env.current-section-path [_ | _], !.
30+
get-class-locality [@global!].
31+
2832
pred add-class-gr i:search-mode, i:gref.
2933
add-class-gr SearchMode ClassGR :-
3034
std.assert! (coq.TC.class? ClassGR) "Only gref of type classes can be added as new predicates",
3135
if (class ClassGR _ _) true
3236
(modes-of-class ClassGR Modes,
3337
gref->pred-name ClassGR PredName,
34-
coq.elpi.add-predicate "tc.db" _ PredName Modes,
35-
add-tc-db _ _ (tc-mode ClassGR Modes),
36-
@global! => add-tc-db _ _ (class ClassGR PredName SearchMode)).
38+
get-class-locality Locality,
39+
Locality => (
40+
add-tc-db _ _ (tc-mode ClassGR Modes),
41+
coq.elpi.add-predicate "tc.db" _ PredName Modes,
42+
add-tc-db _ _ (class ClassGR PredName SearchMode))).
3743

3844
pred add-class-str i:search-mode, i:string.
3945
add-class-str SearchMode ClassStr :-
@@ -42,9 +48,9 @@ add-class-str SearchMode ClassStr :-
4248

4349
% Following are predicates for TC.declare
4450

45-
pred attr->deterministic o:search-mode.
46-
attr->deterministic deterministic :- get-option "deterministic" tt, !.
47-
attr->deterministic classic.
51+
pred attr->search-mode o:search-mode.
52+
attr->search-mode deterministic :- get-option "deterministic" tt, !.
53+
attr->search-mode classic.
4854

4955
pred attr->modes o:list hint-mode.
5056
attr->modes CoqModes :-
@@ -63,14 +69,15 @@ pred declare-class-in-coq i:gref.
6369
declare-class-in-coq ClassGR :-
6470
attr->modes CoqModes,
6571
if (CoqModes = []) true
66-
(@global! => coq.hints.add-mode ClassGR "typeclass_instances" CoqModes),
72+
(coq.hints.add-mode ClassGR "typeclass_instances" CoqModes),
6773
% CAVEAT: this triggers the observer
6874
coq.TC.declare-class ClassGR,
69-
attr->deterministic SearchMode,
75+
attr->search-mode SearchMode,
7076
gref->pred-name ClassGR PredName,
7177
% HACK: we override the clauses added by the observer, since it does not know
7278
% the SearchMode.
73-
@global! => add-tc-db _ (after "0") (class ClassGR PredName SearchMode :- !).
79+
get-class-locality Locality,
80+
Locality => add-tc-db _ (after "0") (class ClassGR PredName SearchMode :- !).
7481

7582
pred declare-class i:indt-decl.
7683
declare-class D :- !,

apps/tc/src/coq_elpi_tc_register.ml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,22 @@ let observer_instance ({locality; instance; info; class_name} : instance) : Coq_
5151

5252
let inObservation =
5353
Libobject.declare_object @@
54-
Libobject.local_object "TC_HACK_OBSERVER2"
54+
Libobject.local_object "TC_HACK_OBSERVER_CLASSES"
55+
~cache:(fun (run,cl) -> run @@ observer_class cl)
56+
~discharge:(fun x -> Some x)
57+
58+
let inObservation1 =
59+
Libobject.declare_object @@
60+
Libobject.local_object "TC_HACK_OBSERVER_INSTANCE"
5561
~cache:(fun (run,inst) -> run @@ observer_instance inst)
5662
~discharge:(fun (_,inst as x) -> if inst.locality = Local then None else Some x)
5763

5864
let observer_evt ((loc, name, atts) : loc_name_atts) (x : Event.t) =
5965
let open Coq_elpi_vernacular in
6066
let run_program e = run_program loc name ~atts e in
6167
match x with
62-
| Event.NewClass cl -> run_program @@ observer_class cl
63-
| Event.NewInstance inst -> Lib.add_leaf (inObservation (run_program,inst))
68+
| Event.NewClass cl -> Lib.add_leaf (inObservation (run_program,cl))
69+
| Event.NewInstance inst -> Lib.add_leaf (inObservation1 (run_program,inst))
6470

6571
module StringMap = Map.Make(String)
6672

apps/tc/tests/section_in_out.v

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,12 @@ End A.
5252

5353
Elpi len_test 4.
5454

55-
56-
55+
Section ClassPersistence.
56+
Section S1.
57+
Context (X : Type) (A : X).
58+
Class class (A : X).
59+
Definition x : class A. apply Build_class. Qed.
60+
Elpi TC.AddInstances x.
61+
Goal exists x, class x. eexists. apply _. Qed.
62+
End S1.
63+
End ClassPersistence.

coq-builtin.elpi

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,10 +1739,13 @@ type in argument_mode.
17391739
type out argument_mode.
17401740

17411741
% [coq.elpi.add-predicate Db Indexing PredName Spec] Declares a new
1742-
% predicate PredName in the data base Db. Indexing can be left unspecified.
1743-
% Spec gathers a mode and a type for each argument. CAVEAT: types and
1744-
% indexing are strings instead of proper data types; beware parsing errors
1745-
% are fatal
1742+
% predicate PredName in the data base Db.
1743+
% Indexing can be left unspecified. Spec gathers a mode and a
1744+
% type for each argument. CAVEAT: types and indexing are strings
1745+
% instead of proper data types; beware parsing errors are fatal.
1746+
% Supported attributes:
1747+
% - @local! (default: false, discard at the end of section or module)
1748+
% - @global! (default: false, always active
17461749
external pred coq.elpi.add-predicate i:string, i:string, i:string,
17471750
i:list (pair argument_mode string).
17481751

src/coq_elpi_builtins.ml

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ let argument_mode = let open Conv in let open API.AlgebraicData in declare {
604604

605605

606606
let set_accumulate_text_to_db, get_accumulate_text_to_db =
607-
let f = ref (fun _ _ -> assert false) in
607+
let f = ref (fun _ _ _ -> assert false) in
608608
(fun x -> f := x),
609609
(fun () -> !f)
610610

@@ -4084,10 +4084,19 @@ Supported attributes:
40844084
In(B.unspec B.string,"Indexing",
40854085
In(B.string,"PredName",
40864086
In(B.list (B.pair argument_mode B.string),"Spec",
4087-
Full(global,"Declares a new predicate PredName in the data base Db. Indexing can be left unspecified. Spec gathers a mode and a type for each argument. CAVEAT: types and indexing are strings instead of proper data types; beware parsing errors are fatal"))))),
4088-
(fun dbname indexing predname spec ~depth _ _ state ->
4087+
Full(global,{|Declares a new predicate PredName in the data base Db.
4088+
Indexing can be left unspecified. Spec gathers a mode and a
4089+
type for each argument. CAVEAT: types and indexing are strings
4090+
instead of proper data types; beware parsing errors are fatal.
4091+
Supported attributes:
4092+
- @local! (default: false, discard at the end of section or module)
4093+
- @global! (default: false, always active|}))))),
4094+
(fun dbname indexing predname spec ~depth ctx _ state ->
40894095
let dbname = Coq_elpi_utils.string_split_on_char '.' dbname in
40904096
let f = get_accumulate_text_to_db () in
4097+
let local = ctx.options.local = Some true in
4098+
let super_global = ctx.options.local = Some false in
4099+
if local && super_global then CErrors.user_err Pp.(str "coq.elpi.add-predicate: @global! incompatible with @local!");
40914100
let indexing =
40924101
match indexing with
40934102
| B.Given str -> ":index ("^str^") "
@@ -4100,7 +4109,8 @@ Supported attributes:
41004109
mode ^ "(" ^ ty ^ ")") in
41014110
let spec = String.concat ", " spec in
41024111
let text = indexing ^ "pred " ^ predname ^ " " ^ spec ^ "." in
4103-
f dbname text;
4112+
let scope = if local then Local else if super_global then SuperGlobal else Regular in
4113+
f dbname text scope;
41044114
state, (), []
41054115
)),
41064116
DocAbove);

src/coq_elpi_builtins.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ val clauses_for_later :
1313
(qualified_name * Ast.program * Names.Id.t list * Coq_elpi_utils.clause_scope) list State.component
1414
val set_accumulate_to_db :
1515
(((qualified_name * Ast.program * Names.Id.t list * Coq_elpi_utils.clause_scope) list -> unit)) -> unit
16-
val set_accumulate_text_to_db : ((string list -> string -> unit)) -> unit
16+
val set_accumulate_text_to_db : ((string list -> string -> Coq_elpi_utils.clause_scope -> unit)) -> unit
1717

1818
type attribute_data =
1919
| AttributeString of string

src/coq_elpi_programs.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -629,8 +629,8 @@ let () = Coq_elpi_builtins.set_accumulate_to_db (fun clauses_to_add ->
629629
clauses_to_add |> List.iter (fun (dbname,units,vs,scope) ->
630630
accumulate_to_db dbname units vs ~scope))
631631

632-
let () = Coq_elpi_builtins.set_accumulate_text_to_db (fun n txt ->
632+
let () = Coq_elpi_builtins.set_accumulate_text_to_db (fun n txt scope ->
633633
let elpi = ensure_initialized () in
634634
let loc = API.Ast.Loc.initial "(elpi.add_predicate)" in
635635
let u = unit_from_string ~elpi loc txt in
636-
accumulate_to_db n [u] [] ~scope:Regular)
636+
accumulate_to_db n [u] [] ~scope)

0 commit comments

Comments
 (0)