Skip to content

Commit f078c81

Browse files
committed
Refactor builtin lookup to use unified API with optional parameters
In response to PR review feedback, this commit consolidates the builtin lookup functions into a single function with optional labeled parameters, replacing the previous split between , , and .
1 parent aa44438 commit f078c81

File tree

10 files changed

+82
-97
lines changed

10 files changed

+82
-97
lines changed

src/core/builtin.ml

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,37 @@ open Common open Error open Pos
66
open Term
77
open Sig_state
88

9-
(** [find_builtin ss pos name] returns the symbol mapped to the builtin [name] by looking through all builtins scope.
10-
It first checks the current signature's path, then searches through all other modules.
9+
(** [get ss ~mp ~pos name] returns the symbol mapped to the builtin [name].
10+
If [mp] is empty (unqualified), it first checks the current signature's path,
11+
then searches through all other modules. If [mp] is specified (qualified),
12+
it looks only in that specific module path.
1113
If the symbol cannot be found then [Fatal] is raised. *)
12-
let find_builtin : sig_state -> popt -> string -> sym = fun ss pos name ->
13-
(* First, try to find in the current signature's path *)
14-
try
15-
StrMap.find name (Path.Map.find ss.signature.sign_path ss.builtins)
16-
with Not_found ->
17-
(* If not found, search through all other paths *)
18-
let exception Found of sym in
19-
try
20-
Path.Map.iter (fun path strmap ->
21-
if path <> ss.signature.sign_path then
22-
match StrMap.find_opt name strmap with
23-
| Some s -> raise (Found s)
24-
| None -> ()
25-
) ss.builtins;
26-
fatal pos "Unknown symbol %s." name
27-
with Found s -> s
28-
29-
(** [get_at ss path pos name] returns the symbol mapped to the builtin [name]
30-
in the module located at [path]. If the symbol cannot be found then
31-
[Fatal] is raised. *)
32-
let get_at : sig_state -> Path.t -> popt -> string -> sym = fun ss path pos name ->
33-
try StrMap.find name (Path.Map.find path ss.builtins) with Not_found ->
34-
fatal pos "Builtin symbol \"%s\" in %a undefined." name Path.pp path
35-
36-
(** [get ss pos name] returns the symbol mapped to the builtin [name] in the current signature from [ss].
37-
If the symbol cannot be found then [Fatal] is raised. *)
38-
let get : sig_state -> popt -> string -> sym = fun ss pos name ->
39-
get_at ss ss.signature.sign_path pos name
40-
41-
(** [get_opt ss name] returns [Some s] where [s] is the symbol mapped to
42-
the builtin [name], and [None] otherwise. *)
43-
let get_opt : sig_state -> string -> sym option = fun ss name ->
44-
try Some (StrMap.find name (Path.Map.find ss.signature.sign_path ss.builtins)) with Not_found -> None
14+
let get : sig_state -> ?mp:Path.t -> ?pos:popt -> string -> sym = fun ss ?(mp=[]) ?(pos=None) name ->
15+
match mp with
16+
| [] ->
17+
(* Unqualified: search current signature first, then all other paths *)
18+
begin
19+
try
20+
StrMap.find name (Path.Map.find ss.signature.sign_path ss.builtins)
21+
with Not_found ->
22+
(* If not found, search through all other paths *)
23+
let exception Found of sym in
24+
try
25+
Path.Map.iter (fun path strmap ->
26+
if path <> ss.signature.sign_path then
27+
match StrMap.find_opt name strmap with
28+
| Some s -> raise (Found s)
29+
| None -> ()
30+
) ss.builtins;
31+
fatal pos "Unknown builtin symbol %s." name
32+
with Found s -> s
33+
end
34+
| path ->
35+
(* Qualified: look in the specific path *)
36+
begin
37+
try StrMap.find name (Path.Map.find path ss.builtins) with Not_found ->
38+
fatal pos "Builtin symbol \"%s\" in %a undefined." name Path.pp path
39+
end
4540

4641
(** Hash-table used to record checking functions for builtins. *)
4742
let htbl : (string, sig_state -> popt -> sym -> unit) Hashtbl.t =

src/handle/command.ml

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,6 @@ open Goal
1111
(** Type alias for a function that compiles a Lambdapi module. *)
1212
type compiler = Path.t -> Sign.t
1313

14-
(** [find_builtin ss name] searches for a builtin symbol named [name] across
15-
all paths in [ss.builtins]. Returns [Some sym] if found, [None] otherwise. *)
16-
let find_builtin : sig_state -> string -> sym option = fun ss name ->
17-
let exception Found of sym in
18-
try
19-
Path.Map.iter (fun _ strmap ->
20-
match StrMap.find_opt name strmap with
21-
| Some s -> raise (Found s)
22-
| None -> ()
23-
) ss.builtins;
24-
None
25-
with Found s -> Some s
26-
2714
(** Register a check for the type of the builtin symbols "nat_zero" and
2815
"nat_succ". *)
2916
let _ =
@@ -34,19 +21,20 @@ let _ =
3421
in
3522
let register = Builtin.register_expected_type eq_noexn term in
3623
let expected_zero_type ss _pos =
37-
match find_builtin ss "nat_succ" with
38-
| Some s ->
39-
(match !(s.sym_type) with
40-
| Prod(a,_) -> a
41-
| _ -> assert false)
42-
| None -> mk_Meta (LibMeta.fresh (new_problem()) mk_Type 0, [||])
24+
try
25+
let s = Builtin.get ss "nat_succ" in
26+
match !(s.sym_type) with
27+
| Prod(a,_) -> a
28+
| _ -> assert false
29+
with Fatal _ -> mk_Meta (LibMeta.fresh (new_problem()) mk_Type 0, [||])
4330
in
4431
register "nat_zero" expected_zero_type;
4532
let expected_succ_type ss _pos =
4633
let typ_0 =
47-
match find_builtin ss "nat_zero" with
48-
| Some s -> !(s.sym_type)
49-
| None -> mk_Meta (LibMeta.fresh (new_problem()) mk_Type 0, [||])
34+
try
35+
let s = Builtin.get ss "nat_zero" in
36+
!(s.sym_type)
37+
with Fatal _ -> mk_Meta (LibMeta.fresh (new_problem()) mk_Type 0, [||])
5038
in
5139
mk_Arro (typ_0, typ_0)
5240
in
@@ -298,10 +286,14 @@ let get_proof_data : compiler -> sig_state -> p_command -> cmd_output =
298286
| P_builtin(n,qid) ->
299287
let s = find_sym ~prt:true ~prv:true ss qid in
300288
begin
301-
match find_builtin ss n with
302-
| Some s' when s' == s ->
303-
fatal pos "Builtin \"%s\" already mapped to %a" n sym s
304-
| _ ->
289+
try
290+
let s' = Builtin.get ss n in
291+
if s' == s then
292+
fatal pos "Builtin \"%s\" already mapped to %a" n sym s
293+
else
294+
fatal pos "Builtin \"%s\" already mapped to another symbol" n
295+
with Fatal _ ->
296+
(* Builtin not yet defined, proceed *)
305297
Builtin.check ss pos n s;
306298
Console.out 2 (Color.gre "builtin \"%s\" ≔ %a") n sym s;
307299
(Sig_state.add_builtin ss n s, None, None)

src/handle/fol.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type config =
2121

2222
(** [get_config ss pos] build the configuration using [ss]. *)
2323
let get_config : Sig_state.t -> Pos.popt -> config = fun ss pos ->
24-
let builtin = Builtin.find_builtin ss pos in
24+
let builtin name = Builtin.get ss ~pos name in
2525
let symb_P = builtin "P" and symb_T = builtin "T" in
2626
let symb_Prop =
2727
match unfold Timed.(!(symb_P.sym_type)) with

src/handle/inductive.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type config =
3030

3131
(** [get_config ss pos] build the configuration using [ss]. *)
3232
let get_config : Sig_state.t -> Pos.popt -> config = fun ss pos ->
33-
let builtin = Builtin.find_builtin ss pos in
33+
let builtin name = Builtin.get ss ~pos name in
3434
{ symb_Prop = builtin "Prop"
3535
; symb_prf = builtin "P" }
3636

src/handle/rewrite.ml

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ type eq_config =
1818
; symb_refl : sym (** Reflexivity of equality. *) }
1919

2020
(** [get_eq_config ss pos] returns the current configuration for
21-
equality, used by tactics such as rewrite or reflexivity. *)
21+
equality, used by tactics such as "rewrite" or "reflexivity". *)
2222
let get_eq_config : Sig_state.t -> popt -> eq_config = fun ss pos ->
23-
let builtin = Builtin.find_builtin ss pos in
23+
let builtin name = Builtin.get ss ~pos name in
2424
{ symb_P = builtin "P"
2525
; symb_T = builtin "T"
2626
; symb_eq = builtin "eq"
@@ -50,10 +50,10 @@ let _ =
5050
let register_builtin =
5151
Builtin.register_expected_type (Eval.eq_modulo []) term
5252
in
53-
let expected_eq_type pos map =
53+
let expected_eq_type ss pos =
5454
(* [Π (a:U), T a → T a → Prop] *)
55-
let symb_T = Builtin.find_builtin pos map "T" in
56-
let symb_P = Builtin.find_builtin pos map "P" in
55+
let symb_T = Builtin.get ss ~pos "T" in
56+
let symb_P = Builtin.get ss ~pos "P" in
5757
let term_U = get_domain_of_type symb_T in
5858
let term_Prop = get_domain_of_type symb_P in
5959
let a = new_var "a" in
@@ -62,11 +62,11 @@ let _ =
6262
mk_Prod (term_U, bind_var a impls)
6363
in
6464
register_builtin "eq" expected_eq_type;
65-
let expected_refl_type pos map =
65+
let expected_refl_type ss pos =
6666
(* [Π (a:U) (x:T a), P (eq a x x)] *)
67-
let symb_T = Builtin.find_builtin pos map "T" in
68-
let symb_P = Builtin.find_builtin pos map "P" in
69-
let symb_eq = Builtin.find_builtin pos map "eq" in
67+
let symb_T = Builtin.get ss ~pos "T" in
68+
let symb_P = Builtin.get ss ~pos "P" in
69+
let symb_eq = Builtin.get ss ~pos "eq" in
7070
let term_U = get_domain_of_type symb_T in
7171
let a = new_var "a" in
7272
let x = new_var "x" in
@@ -78,13 +78,13 @@ let _ =
7878
mk_Prod (term_U, bind_var a prod)
7979
in
8080
register_builtin "refl" expected_refl_type;
81-
let expected_eqind_type pos map =
81+
let expected_eqind_type ss pos =
8282
(* [Π (a:U) (x y:T a), P (eq x y) → Π (p:T a→Prop), P (p y) → P (p x)] *)
83-
let symb_T = Builtin.find_builtin pos map "T" in
83+
let symb_T = Builtin.get ss ~pos "T" in
8484
let term_T = mk_Symb symb_T in
85-
let symb_P = Builtin.find_builtin pos map "P" in
85+
let symb_P = Builtin.get ss ~pos "P" in
8686
let term_P = mk_Symb symb_P in
87-
let symb_eq = Builtin.find_builtin pos map "eq" in
87+
let symb_eq = Builtin.get ss ~pos "eq" in
8888
let term_eq = mk_Symb symb_eq in
8989
let term_U = get_domain_of_type symb_T in
9090
let term_Prop = get_domain_of_type symb_P in

src/handle/tactic.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ type config = (string,tactic) Hashtbl.t
224224
(** [get_config ss pos] build the configuration using [ss]. *)
225225
let get_config (ss:Sig_state.t) (pos:Pos.popt) : config =
226226
let t = Hashtbl.create 17 in
227-
let add n v = let s = Builtin.find_builtin ss pos n in Hashtbl.add t s.sym_name v in
227+
let add n v = let s = Builtin.get ss ~pos n in Hashtbl.add t s.sym_name v in
228228
add "admit" T_admit;
229229
add "and" T_and;
230230
add "apply" T_apply;
@@ -366,7 +366,7 @@ let p_tactic (ss:Sig_state.t) (pos:popt) :int StrMap.t -> term -> p_tactic =
366366
| T_generalize, [_;t] -> P_tac_generalize(p_ident_of_var pos t)
367367
| T_generalize, _ -> assert false
368368
| T_have, [t1;t2] ->
369-
let prf_sym = Builtin.find_builtin ss pos "P" in
369+
let prf_sym = Builtin.get ss ~pos "P" in
370370
let prf = p_term pos idmap (mk_Symb prf_sym) in
371371
let t2 = Pos.make pos (P_Appl(prf, p_term pos idmap t2)) in
372372
P_tac_have(p_ident_of_sym pos t1, t2)

src/parsing/lpLexer.ml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,7 @@ type token =
9696
(* other tokens *)
9797
| DEBUG_FLAGS of (bool * string)
9898
(* Tuple constructor (with parens) required by Menhir. *)
99-
| INT of string
100-
| PINT of (Path.t * string)
99+
| INT of (Path.t * string)
101100
| FLOAT of string
102101
| SIDE of Pratter.associativity
103102
| STRINGLIT of string
@@ -273,7 +272,7 @@ let rec token lb =
273272
(* other tokens *)
274273
| '+', Plus lowercase -> DEBUG_FLAGS(true, remove_first lb)
275274
| '-', Plus lowercase -> DEBUG_FLAGS(false, remove_first lb)
276-
| int -> INT(Utf8.lexeme lb)
275+
| int -> INT(([], Utf8.lexeme lb))
277276
| float -> FLOAT(Utf8.lexeme lb)
278277
| string -> STRINGLIT(Utf8.sub_lexeme lb 1 (lexeme_length lb - 2))
279278

@@ -323,7 +322,7 @@ and qid expl ids lb =
323322
| "/*" -> comment (qid expl ids) 0 lb
324323
| regid, '.' -> qid expl (remove_last lb :: ids) lb
325324
| escid, '.' -> qid expl (remove_useless_escape(remove_last lb) :: ids) lb
326-
| int -> PINT(ids, Utf8.lexeme lb)
325+
| int -> INT(ids, Utf8.lexeme lb)
327326
| regid ->
328327
if expl then QID_EXPL(Utf8.lexeme lb :: ids)
329328
else QID(Utf8.lexeme lb :: ids)

src/parsing/lpParser.mly

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@
8989
// other tokens
9090

9191
%token <bool * string> DEBUG_FLAGS
92-
%token <string> INT
93-
%token <Path.t * string> PINT
92+
%token <Path.t * string> INT
9493
%token <string> FLOAT
9594
%token <Pratter.associativity> SIDE
9695
%token <string> STRINGLIT
@@ -218,8 +217,11 @@ query:
218217
| FLAG s=STRINGLIT b=SWITCH { make_pos $sloc (P_query_flag(s,b)) }
219218
| PROVER s=STRINGLIT { make_pos $sloc (P_query_prover(s)) }
220219
| PROVER_TIMEOUT n=INT
221-
{ make_pos $sloc (P_query_prover_timeout n) }
222-
| VERBOSE n=INT { make_pos $sloc (P_query_verbose n) }
220+
{ let (_, m) = n in
221+
make_pos $sloc (P_query_prover_timeout m) }
222+
| VERBOSE n=INT
223+
{ let (_, m) = n in
224+
make_pos $sloc (P_query_verbose m) }
223225
| TYPE_QUERY t=term
224226
{ make_pos $sloc (P_query_infer(t, {strategy=NONE; steps=None}))}
225227
| SEARCH s=STRINGLIT
@@ -295,11 +297,9 @@ aterm:
295297
make_pos $sloc (P_Patt(i, Option.map Array.of_list e)) }
296298
| L_PAREN t=term R_PAREN { make_pos $sloc (P_Wrap(t)) }
297299
| L_SQ_BRACKET t=term R_SQ_BRACKET { make_pos $sloc (P_Expl(t)) }
298-
| m=PINT {
300+
| m=INT {
299301
let (p, n) = m in
300302
make_pos $sloc (P_NLit(List.rev p, n)) }
301-
| n=INT {
302-
make_pos $sloc (P_NLit([], n)) }
303303
| s=STRINGLIT { make_pos $sloc (P_SLit s) }
304304

305305
env: DOT L_SQ_BRACKET ts=separated_list(SEMICOLON, term) R_SQ_BRACKET { ts }
@@ -425,7 +425,7 @@ notation:
425425

426426
float_or_int:
427427
| s=FLOAT { s }
428-
| s=INT { s }
428+
| s=INT { let (_, m) = s in m }
429429

430430
maybe_generalize:
431431
| g = GENERALIZE?

src/parsing/pretty.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ let rec term : p_term pp = fun ppf t ->
151151
| (P_LLet(x,xs,a,t,u) , `Func) ->
152152
out ppf "@[@[<hv2>let @[<2>%a%a%a@] ≔@ %a@ @]in@ %a@]"
153153
ident x params_list xs typ a func t func u
154-
| (P_NLit(ps, i) , _ ) -> out ppf "%a %s" (raw_path) ps i
154+
| (P_NLit(ps, i) , _ ) -> out ppf "%a %s" (raw_path) ps i
155155
| (P_SLit(s) , _ ) -> out ppf "\"%s\"" s
156156
| (P_Wrap(t) , _ ) -> out ppf "(@[<hv2>%a@])" func t
157157
| (P_Expl(t) , _ ) -> out ppf "[@[<hv2>%a@]]" func t

src/parsing/scope.ml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ and scope_head : ?find_sym:find_sym ->
285285
let sym =
286286
try Sign.find Sign.Ghost.sign s
287287
with Not_found ->
288-
let s_typ = mk_Symb (Builtin.find_builtin ss pos "String") in
288+
let s_typ = mk_Symb (Builtin.get ss ~pos "String") in
289289
Sign.add_symbol Sign.Ghost.sign Public Const Eager true
290290
{elt=s;pos} pos s_typ []
291291
in
@@ -298,12 +298,11 @@ and scope_head : ?find_sym:find_sym ->
298298
let s = if neg then String.sub s 1 (String.length s - 1) else s in
299299
neg, s
300300
in
301-
(* Use Builtin.find_builtin for unqualified numeric literals (path=[]),
302-
and Builtin.find_builtin_at for qualified ones (e.g., Nat.12). *)
301+
(* Use Builtin.get for numeric literals, with optional path parameter.
302+
For unqualified (path=[]), it searches current module first.
303+
For qualified (e.g., Nat.12), it uses the specified path. *)
303304
let sym_of s =
304-
mk_Symb
305-
(if path = [] then Builtin.find_builtin ss pos s
306-
else Builtin.get_at ss path pos s)
305+
mk_Symb (Builtin.get ss ~mp:path ~pos s)
307306
in
308307
let sym = Array.map sym_of strint in
309308
let digit = function

0 commit comments

Comments
 (0)