Skip to content

Commit f4b1f1e

Browse files
committed
move metadata to signature
1 parent 502ceb1 commit f4b1f1e

File tree

15 files changed

+1345
-41
lines changed

15 files changed

+1345
-41
lines changed

lambdap.elpi

Lines changed: 1292 additions & 0 deletions
Large diffs are not rendered by default.

src/core/coercion.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ open Term
33

44
let coerce : sym =
55
let id = Pos.none "coerce" in
6-
Sign.add_symbol Ghost.sign Public Defin Eager false id mk_Kind [] false
6+
Sign.add_symbol Ghost.sign Public Defin Eager false id mk_Kind []
77

88
let apply a b t : term = add_args (mk_Symb coerce) [a; b; t]
99

src/core/sig_state.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ let add_symbol : sig_state -> expo -> prop -> match_strat
5151
fun ss expo prop mstrat opaq id typ impl tc def ->
5252
let sym =
5353
Sign.add_symbol ss.signature expo prop mstrat opaq id
54-
(cleanup typ) impl tc in
54+
(cleanup typ) impl in
55+
if tc then Sign.add_tc ss.signature sym;
5556
begin
5657
match def with
5758
| Some t -> sym.sym_def := Some (cleanup t)

src/core/sign.ml

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ type t =
3636
; sign_notations: float notation SymMap.t ref
3737
(** Maps symbols to their notation if they have some. *)
3838
; sign_ind : ind_data SymMap.t ref
39+
; sign_tc : SymSet.t ref
40+
; sign_tc_inst : SymSet.t ref
3941
; sign_cp_pos : cp_pos list SymMap.t ref
4042
(** Maps a symbol to the critical pair positions it is heading in the
4143
rules. *) }
@@ -51,6 +53,7 @@ let dummy : unit -> t = fun () ->
5153
{ sign_symbols = ref StrMap.empty; sign_path = []
5254
; sign_deps = ref Path.Map.empty; sign_builtins = ref StrMap.empty
5355
; sign_notations = ref SymMap.empty; sign_ind = ref SymMap.empty
56+
; sign_tc = ref SymSet.empty; sign_tc_inst = ref SymSet.empty
5457
; sign_cp_pos = ref SymMap.empty }
5558

5659
(** [find sign name] finds the symbol named [name] in [sign] if it exists, and
@@ -212,11 +215,11 @@ let unlink : t -> unit = fun sign ->
212215
arguments [impl], no definition and no rules. [name] should not already be
213216
used in [sign]. The created symbol is returned. *)
214217
let add_symbol : t -> expo -> prop -> match_strat -> bool -> strloc -> term ->
215-
bool list -> bool -> sym =
216-
fun sign sym_expo sym_prop sym_mstrat sym_opaq name typ impl sym_tc ->
218+
bool list -> sym =
219+
fun sign sym_expo sym_prop sym_mstrat sym_opaq name typ impl ->
217220
let sym =
218221
create_sym sign.sign_path sym_expo sym_prop sym_mstrat sym_opaq name
219-
(cleanup typ) (minimize_impl impl) sym_tc
222+
(cleanup typ) (minimize_impl impl)
220223
in
221224
sign.sign_symbols := StrMap.add name.elt sym !(sign.sign_symbols);
222225
sym
@@ -265,6 +268,8 @@ let read : string -> t = fun fname ->
265268
unsafe_reset sign.sign_notations;
266269
unsafe_reset sign.sign_ind;
267270
unsafe_reset sign.sign_cp_pos;
271+
unsafe_reset sign.sign_tc;
272+
unsafe_reset sign.sign_tc_inst;
268273
let shallow_reset_sym s =
269274
unsafe_reset s.sym_type;
270275
unsafe_reset s.sym_def;
@@ -301,6 +306,8 @@ let read : string -> t = fun fname ->
301306
StrMap.iter (fun _ s -> reset_sym s) !(sign.sign_symbols);
302307
StrMap.iter (fun _ s -> shallow_reset_sym s) !(sign.sign_builtins);
303308
SymMap.iter (fun s _ -> shallow_reset_sym s) !(sign.sign_notations);
309+
SymSet.iter (fun s -> reset_sym s) !(sign.sign_tc);
310+
SymSet.iter (fun s -> reset_sym s) !(sign.sign_tc_inst);
304311
let f _ sm = StrMap.iter (fun _ rs -> List.iter reset_rule rs) sm in
305312
Path.Map.iter f !(sign.sign_deps);
306313
let reset_ind i =
@@ -382,3 +389,12 @@ let rec dependencies : t -> (Path.t * t) list = fun sign ->
382389
| d::deps -> minimize ((List.filter not_here d) :: acc) deps
383390
in
384391
List.concat (minimize [] deps)
392+
393+
let add_tc : t -> sym -> unit =
394+
fun sign sym ->
395+
sign.sign_tc := SymSet.add sym !(sign.sign_tc)
396+
397+
let add_tc_inst : t -> sym -> unit =
398+
fun sign sym ->
399+
sign.sign_tc_inst := SymSet.add sym !(sign.sign_tc_inst);
400+

src/core/term.ml

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ and sym =
9797
; sym_rules : rule list ref (** Rewriting rules. *)
9898
; sym_mstrat: match_strat (** Matching strategy. *)
9999
; sym_dtree : dtree ref (** Decision tree used for matching. *)
100-
; sym_tc : bool (** Type class *)
101100
; sym_pos : Pos.popt (** Position in source file. *) }
102101

103102
(** {b NOTE} {!field:sym_type} holds a (timed) reference for a technical
@@ -356,12 +355,12 @@ let new_problem : unit -> problem = fun () ->
356355
strategy [mstrat], name [name.elt], type [typ], implicit arguments [impl],
357356
position [name.pos], no definition and no rules. *)
358357
let create_sym : Path.t -> expo -> prop -> match_strat -> bool ->
359-
Pos.strloc -> term -> bool list -> bool -> sym =
358+
Pos.strloc -> term -> bool list -> sym =
360359
fun sym_path sym_expo sym_prop sym_mstrat sym_opaq
361-
{ elt = sym_name; pos = sym_pos } typ sym_impl sym_tc ->
360+
{ elt = sym_name; pos = sym_pos } typ sym_impl ->
362361
{sym_path; sym_name; sym_type = ref typ; sym_impl; sym_def = ref None;
363362
sym_opaq; sym_rules = ref []; sym_dtree = ref Tree_type.empty_dtree;
364-
sym_mstrat; sym_prop; sym_expo; sym_pos; sym_tc }
363+
sym_mstrat; sym_prop; sym_expo; sym_pos }
365364

366365
(** [is_constant s] tells whether [s] is a constant. *)
367366
let is_constant : sym -> bool = fun s -> s.sym_prop = Const
@@ -378,9 +377,6 @@ let is_private : sym -> bool = fun s -> s.sym_expo = Privat
378377
let is_modulo : sym -> bool = fun s ->
379378
match s.sym_prop with Assoc _ | Commu | AC _ -> true | _ -> false
380379

381-
(* [is_tc s] tells whether the symbol [s] is a type class *)
382-
let is_tc : sym -> bool = fun s -> s.sym_tc
383-
384380
(** [unfold t] repeatedly unfolds the definition of the surface constructor of
385381
[t], until a significant {!type:term} constructor is found. The term that
386382
is returned cannot be an instantiated metavariable or term environment nor
@@ -562,8 +558,7 @@ let right_aliens : sym -> term -> term list = fun s ->
562558

563559
(* unit test *)
564560
let _ =
565-
let s =
566-
create_sym [] Privat (AC true) Eager false (Pos.none "+") Kind [] false in
561+
let s = create_sym [] Privat (AC true) Eager false (Pos.none "+") Kind [] in
567562
let t1 = Vari (new_tvar "x1") in
568563
let t2 = Vari (new_tvar "x2") in
569564
let t3 = Vari (new_tvar "x3") in

src/core/term.mli

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ and sym =
9696
; sym_rules : rule list ref (** Rewriting rules. *)
9797
; sym_mstrat: match_strat (** Matching strategy. *)
9898
; sym_dtree : dtree ref (** Decision tree used for matching. *)
99-
; sym_tc : bool (** Type class *)
10099
; sym_pos : Pos.popt (** Position in source file. *) }
101100

102101
(** {b NOTE} that {!field:sym_type} holds a (timed) reference for a technical
@@ -301,7 +300,7 @@ module SymMap : Map.S with type key = sym
301300
arguments [impl], position [name.pos], typeclass [tc] no definition and no
302301
rules. *)
303302
val create_sym : Path.t -> expo -> prop -> match_strat -> bool ->
304-
Pos.strloc -> term -> bool list -> bool -> sym
303+
Pos.strloc -> term -> bool list -> sym
305304

306305
(** [is_constant s] tells whether the symbol is a constant. *)
307306
val is_constant : sym -> bool
@@ -316,9 +315,6 @@ val is_private : sym -> bool
316315
(** [is_modulo s] tells whether the symbol [s] is modulo some equations. *)
317316
val is_modulo : sym -> bool
318317

319-
(* [is_tc s] tells whether the symbol [s] is a type class *)
320-
val is_tc : sym -> bool
321-
322318
(** Sets and maps of metavariables. *)
323319
module Meta : Map.OrderedType with type t = meta
324320
module MetaSet : Set.S with type elt = Meta.t

src/core/unif_rule.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ open Term
1111
let equiv : sym =
1212
let id = Pos.none "" in
1313
let s =
14-
Sign.add_symbol Ghost.sign Public Defin Eager false id mk_Kind [] false in
14+
Sign.add_symbol Ghost.sign Public Defin Eager false id mk_Kind [] in
1515
Sign.add_notation Ghost.sign s (Infix(Pratter.Neither, 2.0)); s
1616

1717
(** Symbol ";". *)
1818
let cons : sym =
1919
let id = Pos.none ";" in
2020
let s =
21-
Sign.add_symbol Ghost.sign Public Const Eager true id mk_Kind [] false in
21+
Sign.add_symbol Ghost.sign Public Const Eager true id mk_Kind [] in
2222
Sign.add_notation Ghost.sign s (Infix(Pratter.Right, 1.0)); s
2323

2424
(** [unpack eqs] transforms a term of the form

src/handle/elpi_handle.ml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,12 @@ fun ss file predicate arg ->
142142

143143
let extend (cctx) v ?def ty = (v, ty, def) :: cctx
144144

145-
let is_tc_instance : Term.ctxt -> Term.meta -> bool = fun c m ->
145+
let is_tc_instance : Sig_state.t -> Term.ctxt -> Term.meta -> bool =
146+
fun ss c m ->
146147
let open Timed in
147148
let open Term in
149+
let is_tc symb =
150+
SymSet.mem symb !(ss.Sig_state.signature.Sign.sign_tc) in
148151
let rec aux c t =
149152
match get_args (unfold t) with
150153
| Symb s, _ -> is_tc s
@@ -156,12 +159,13 @@ let is_tc_instance : Term.ctxt -> Term.meta -> bool = fun c m ->
156159
in
157160
aux c !(m.meta_type)
158161

159-
let tc_metas_of_term : Term.ctxt -> Term.term -> Term.meta list = fun c t ->
162+
let tc_metas_of_term : Sig_state.t -> Term.ctxt -> Term.term -> Term.meta list =
163+
fun ss c t ->
160164
let open Term in
161165
let acc = ref [] in
162166
let rec aux c t =
163167
match unfold t with
164-
| Meta(m,_) when is_tc_instance c m && not (List.memq m !acc) ->
168+
| Meta(m,_) when is_tc_instance ss c m && not (List.memq m !acc) ->
165169
acc := m :: !acc
166170
| Abst (dom, b) | Prod(dom, b) ->
167171
aux c dom;
@@ -184,10 +188,10 @@ let tc_metas_of_term : Term.ctxt -> Term.term -> Term.meta list = fun c t ->
184188

185189
let scope_ref : (Parsing.Syntax.p_term -> Term.term * (int * string) list) ref = ref (fun _ -> assert false)
186190

187-
let solve_tc : ?scope:(Parsing.Syntax.p_term -> Term.term * (int * string) list) -> Common.Pos.popt -> Term.problem -> Term.ctxt ->
191+
let solve_tc : ?scope:(Parsing.Syntax.p_term -> Term.term * (int * string) list) -> Sig_state.t -> Common.Pos.popt -> Term.problem -> Term.ctxt ->
188192
Term.term * Term.term -> Term.term * Term.term =
189-
fun ?scope pos _pb ctxt (t,ty) ->
190-
let tc = tc_metas_of_term ctxt t in
193+
fun ?scope ss pos _pb ctxt (t,ty) ->
194+
let tc = tc_metas_of_term ss ctxt t in
191195
if tc <> [] then begin
192196
let elpi = match !elpi with None -> assert false | Some x -> x in
193197
Option.iter (fun f -> scope_ref := f) scope;

src/handle/query.ml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ open Proof
77
open Lplib open Base
88
open Timed
99

10-
let infer : ?scope:(Parsing.Syntax.p_term -> Term.term * (int * string) list) -> Pos.popt -> problem -> ctxt -> term -> term * term =
11-
fun ?scope pos p ctx t ->
10+
let infer : ?scope:(Parsing.Syntax.p_term -> Term.term * (int * string) list) -> Sig_state.t -> Pos.popt -> problem -> ctxt -> term -> term * term =
11+
fun ?scope ss pos p ctx t ->
1212
match Infer.infer_noexn p ctx t with
1313
| None -> fatal pos "%a is not typable." term t
1414
| Some (t, a) ->
1515
if Unif.solve_noexn p then
1616
begin
1717
if !p.unsolved = [] then begin
18-
let t, a = Elpi_handle.solve_tc ?scope pos p ctx (t, a) in
18+
let t, a = Elpi_handle.solve_tc ?scope ss pos p ctx (t, a) in
1919
match Infer.infer_noexn p ctx t with
2020
| None -> fatal pos "%a is not typable AFTER TC RESOLUTION!!!!" term t
2121
| _ -> Common.Console.out 1 "TC RESOLUTION OK!@ ";
@@ -184,9 +184,9 @@ let handle : Sig_state.t -> proof_state option -> p_query -> result =
184184
Console.out 2 "assertion: it is %b that %a" (not must_fail)
185185
constr (ctxt, t, u);
186186
(* Check that [t] is typable. *)
187-
let (t, a) = infer pt.pos p ctxt t in
187+
let (t, a) = infer ss pt.pos p ctxt t in
188188
(* Check that [u] is typable. *)
189-
let (u, b) = infer pu.pos p ctxt u in
189+
let (u, b) = infer ss pu.pos p ctxt u in
190190
(* Check that [t] and [u] have the same type. *)
191191
p := {!p with to_solve = (ctxt,a,b)::!p.to_solve};
192192
if Unif.solve_noexn p then
@@ -206,11 +206,11 @@ let handle : Sig_state.t -> proof_state option -> p_query -> result =
206206
| P_query_infer(pt, cfg) ->
207207
let t = scope pt in
208208
let scope = Scope.scope_term_w_pats ~typ:false ~mok true ss env in
209-
let t, ty = infer ~scope pt.pos p ctxt t in
209+
let t, ty = infer ss ~scope pt.pos p ctxt t in
210210
let t = Eval.eval cfg ctxt t in
211211
let ty = Eval.eval cfg ctxt ty in
212212
return typing (ctxt,t,ty)
213213
| P_query_normalize(pt, cfg) ->
214214
let t = scope pt in
215-
let t, _ = infer pt.pos p ctxt t in
215+
let t, _ = infer ss pt.pos p ctxt t in
216216
return term (Eval.eval cfg ctxt t)

src/handle/query.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ open Proof
1010
refinement of [t]. Note that [p] gets modified. Context [c] must well
1111
sorted.
1212
@raise Fatal if [t] cannot be typed. *)
13-
val infer : ?scope:(Parsing.Syntax.p_term -> Core.Term.term * (int * string) list) -> popt -> problem -> ctxt -> term -> term * term
13+
val infer : ?scope:(Parsing.Syntax.p_term -> Core.Term.term * (int * string) list) -> Sig_state.t -> popt -> problem -> ctxt -> term -> term * term
1414

1515
(** [check pos p c t a] checks that the term [t] has type [a] in context [c]
1616
and under the constraints of [p], and returns [t] refined. Context [c]

0 commit comments

Comments
 (0)