Skip to content

Commit 0c1cd13

Browse files
authored
Merge pull request #111 from engboris/identifiers
Extend identifiers to rays
2 parents 67b707e + 8322ec2 commit 0c1cd13

File tree

6 files changed

+160
-138
lines changed

6 files changed

+160
-138
lines changed

examples/syntax.sg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,7 @@ lemma y : nat =
117117
proof
118118
0.
119119
end proof
120+
121+
'complex identifiers
122+
f(a b) = function(a b).
123+
show #f(a b).

src/stellogen/sgen_ast.ml

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
open Base
22
open Lsc_ast
33

4-
type ident = string
4+
type ident = StellarRays.term
55

66
type idvar = string * int option
77

@@ -36,13 +36,13 @@ and galaxy_expr =
3636
and substitution =
3737
| Extend of ray_prefix
3838
| Reduce of ray_prefix
39-
| SVar of ident * StellarRays.term
39+
| SVar of string * StellarRays.term
4040
| SFunc of (StellarRays.fmark * idfunc) * (StellarRays.fmark * idfunc)
4141
| SGal of ident * galaxy_expr
4242

43-
let reserved_words = [ "clean"; "kill" ]
43+
let reserved_words = [ const "clean"; const "kill" ]
4444

45-
let is_reserved = List.mem reserved_words ~equal:equal_string
45+
let is_reserved = List.mem reserved_words ~equal:equal_ray
4646

4747
type env =
4848
{ objs : (ident * galaxy_expr) list
@@ -51,11 +51,14 @@ type env =
5151

5252
let expect (g : galaxy_expr) : galaxy_expr =
5353
Raw
54-
(Galaxy [ GLabelDef ("interaction", Id "tested"); GLabelDef ("expect", g) ])
54+
(Galaxy
55+
[ GLabelDef (const "interaction", Id (const "tested"))
56+
; GLabelDef (const "expect", g)
57+
] )
5558

5659
let initial_env =
57-
{ objs = [ ("^empty", Raw (Const [])) ]
58-
; types = [ ("^empty", ([ "^empty" ], None)) ]
60+
{ objs = [ (const "^empty", Raw (Const [])) ]
61+
; types = [ (const "^empty", ([ const "^empty" ], None)) ]
5962
}
6063

6164
type declaration =

src/stellogen/sgen_err.ml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
open Base
22
open Sgen_ast
33

4-
type ident = string
5-
64
type err =
75
| IllFormedChecker
86
| ExpectedGalaxy
9-
| ReservedWord of ident
10-
| UnknownField of ident
11-
| UnknownID of ident
12-
| TestFailed of ident * ident * ident * galaxy * galaxy
7+
| ReservedWord of string
8+
| UnknownField of string
9+
| UnknownID of string
10+
| TestFailed of string * string * string * galaxy * galaxy
1311
| LscError of Lsc_err.err_effect

src/stellogen/sgen_eval.ml

Lines changed: 66 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ open Out_channel
99

1010
let ( let* ) x f = Result.bind x ~f
1111

12-
let add_obj env x e = List.Assoc.add ~equal:equal_string env.objs x e
12+
let add_obj env x e = List.Assoc.add ~equal:equal_ray env.objs x e
1313

14-
let get_obj env x = List.Assoc.find ~equal:equal_string env.objs x
14+
let get_obj env x = List.Assoc.find ~equal:equal_ray env.objs x
1515

16-
let add_type env x e = List.Assoc.add ~equal:equal_string env.types x e
16+
let add_type env x e = List.Assoc.add ~equal:equal_ray env.types x e
1717

18-
let get_type env x = List.Assoc.find ~equal:equal_string env.types x
18+
let get_type env x = List.Assoc.find ~equal:equal_ray env.types x
1919

2020
let rec map_galaxy env ~f : galaxy -> (galaxy, err) Result.t = function
2121
| Const mcs -> Const (f mcs) |> Result.return
@@ -42,7 +42,7 @@ and map_galaxy_expr env ~f : galaxy_expr -> (galaxy_expr, err) Result.t =
4242
| Id x when is_reserved x -> Ok (Id x)
4343
| Id x -> begin
4444
match get_obj env x with
45-
| None -> Error (UnknownID x)
45+
| None -> Error (UnknownID (string_of_ray x))
4646
| Some g -> map_galaxy_expr env ~f g
4747
end
4848
| Exec e ->
@@ -82,7 +82,7 @@ let rec replace_id env (_from : ident) (_to : galaxy_expr) e :
8282
(galaxy_expr, err) Result.t =
8383
match e with
8484
| Id x when is_reserved x -> Ok (Id x)
85-
| Id x when equal_string x _from -> Ok _to
85+
| Id x when equal_ray x _from -> Ok _to
8686
| Access (g, x) ->
8787
let* g' = replace_id env _from _to g in
8888
Access (g', x) |> Result.return
@@ -113,7 +113,9 @@ let subst_vars env _from _to =
113113
let subst_funcs env _from _to =
114114
map_galaxy_expr env ~f:(subst_all_funcs [ (_from, _to) ])
115115

116-
let group_galaxy =
116+
let group_galaxy :
117+
galaxy_declaration list
118+
-> type_declaration list * (StellarRays.term * galaxy_expr) list =
117119
List.fold_left ~init:([], []) ~f:(function types, fields ->
118120
(function
119121
| GTypeDef d -> (d :: types, fields)
@@ -126,14 +128,14 @@ let rec typecheck_galaxy ~notyping env (g : galaxy_declaration list) :
126128
| TExp (x, g) ->
127129
let checker = expect g in
128130
let new_env = { types = env.types; objs = fields @ env.objs } in
129-
typecheck ~notyping new_env x "^empty" checker
131+
typecheck ~notyping new_env x (const "^empty") checker
130132
| TDef (x, ts, ck) ->
131133
let* checker =
132134
match ck with
133135
| None -> Ok default_checker
134136
| Some xck -> begin
135137
match get_obj env xck with
136-
| None -> Error (UnknownID xck)
138+
| None -> Error (UnknownID (string_of_ray xck))
137139
| Some g -> Ok g
138140
end
139141
in
@@ -142,7 +144,7 @@ let rec typecheck_galaxy ~notyping env (g : galaxy_declaration list) :
142144
|> Result.all_unit )
143145
|> Result.all_unit
144146

145-
and pp_err ~notyping e : (ident, err) Result.t =
147+
and pp_err ~notyping e : (string, err) Result.t =
146148
match e with
147149
| IllFormedChecker -> "Ill-formed checker.\n" |> Result.return
148150
| ExpectedGalaxy -> "Expected galaxy.\n" |> Result.return
@@ -178,21 +180,21 @@ and eval_galaxy_expr ~notyping (env : env) :
178180
| Raw (Interface _) -> Ok (Interface [])
179181
| Access (e, x) -> begin
180182
match eval_galaxy_expr ~notyping env e with
181-
| Ok (Const _) -> Error (UnknownField x)
182-
| Ok (Interface _) -> Error (UnknownField x)
183+
| Ok (Const _) -> Error (UnknownField (string_of_ray x))
184+
| Ok (Interface _) -> Error (UnknownField (string_of_ray x))
183185
| Ok (Galaxy g) -> (
184186
let _, fields = group_galaxy g in
185187
try
186188
fields |> fun g ->
187-
List.Assoc.find_exn ~equal:equal_string g x
189+
List.Assoc.find_exn ~equal:equal_ray g x
188190
|> eval_galaxy_expr ~notyping env
189-
with Not_found_s _ -> Error (UnknownField x) )
191+
with Not_found_s _ -> Error (UnknownField (string_of_ray x)) )
190192
| Error e -> Error e
191193
end
192194
| Id x -> begin
193195
begin
194196
match get_obj env x with
195-
| None -> Error (UnknownID x)
197+
| None -> Error (UnknownID (string_of_ray x))
196198
| Some g -> eval_galaxy_expr ~notyping env g
197199
end
198200
end
@@ -231,8 +233,9 @@ and eval_galaxy_expr ~notyping (env : env) :
231233
List.fold_left t ~init:(Ok init) ~f:(fun acc x ->
232234
let* acc = acc in
233235
match x with
234-
| Id "kill" -> acc |> remove_mark_all |> kill |> focus |> Result.return
235-
| Id "clean" ->
236+
| Id (Func ((Muted, (Null, "kill")), [])) ->
237+
acc |> remove_mark_all |> kill |> focus |> Result.return
238+
| Id (Func ((Muted, (Null, "clean")), [])) ->
236239
acc |> remove_mark_all |> clean |> focus |> Result.return
237240
| _ ->
238241
let origin = acc |> remove_mark_all |> focus in
@@ -294,93 +297,111 @@ and check_interface ~notyping env x i =
294297
match get_obj env x with
295298
| Some (Raw (Galaxy g)) -> Ok g
296299
| Some _ -> Error ExpectedGalaxy
297-
| None -> Error (UnknownID x)
300+
| None -> Error (UnknownID (string_of_ray x))
298301
in
299302
let type_decls = List.map i ~f:(fun t -> GTypeDef t) in
300303
typecheck_galaxy ~notyping env (type_decls @ g)
301304

302-
and typecheck ~notyping env x t (ck : galaxy_expr) : (unit, err) Result.t =
303-
let* gtests =
305+
and typecheck ~notyping env x (t : StellarRays.term) (ck : galaxy_expr) :
306+
(unit, err) Result.t =
307+
let* gtests : (StellarRays.term * galaxy_expr) list =
304308
match get_obj env t with
305-
| Some (Raw (Const mcs)) -> Ok [ ("_", Raw (Const mcs)) ]
309+
| Some (Raw (Const mcs)) -> Ok [ (const "_", Raw (Const mcs)) ]
306310
| Some (Raw (Interface i)) ->
307311
let* _ = check_interface ~notyping env x i in
308312
Ok []
309313
| Some (Raw (Galaxy gtests)) -> group_galaxy gtests |> snd |> Result.return
310314
| Some e ->
311315
let* eval_e = eval_galaxy_expr ~notyping env e in
312316
let* mcs = galaxy_to_constellation ~notyping env eval_e in
313-
[ ("test", Raw (Const mcs)) ] |> Result.return
314-
| None -> Error (UnknownID x)
317+
[ (const "test", Raw (Const mcs)) ] |> Result.return
318+
| None -> Error (UnknownID (string_of_ray x))
315319
in
316320
let testing =
317321
List.map gtests ~f:(fun (idtest, test) ->
318322
match ck with
319323
| Raw (Galaxy gck) ->
320324
let format =
321325
try
322-
List.Assoc.find_exn ~equal:equal_string
326+
List.Assoc.find_exn ~equal:equal_ray
323327
(group_galaxy gck |> snd)
324-
"interaction"
328+
(const "interaction")
325329
with Not_found_s _ -> default_interaction
326330
in
327331
begin
328332
match get_obj env x with
329-
| None -> Error (UnknownID x)
333+
| None -> Error (UnknownID (string_of_ray x))
330334
| Some obj_x ->
331335
Ok
332336
( idtest
333337
, Exec
334338
(Subst
335-
( Subst (format, SGal ("test", test))
336-
, SGal ("tested", obj_x) ) )
339+
( Subst (format, SGal (const "test", test))
340+
, SGal (const "tested", obj_x) ) )
337341
|> eval_galaxy_expr ~notyping env )
338342
end
339343
| _ -> Error IllFormedChecker )
340344
in
341-
let expect = Access (ck, "expect") in
345+
let expect = Access (ck, const "expect") in
342346
let* eval_exp = eval_galaxy_expr ~notyping env expect in
343347
List.map testing ~f:(function
344348
| Ok (idtest, got) ->
345349
let* got = got in
346350
let* eq = equal_galaxy ~notyping env got eval_exp in
347-
if not eq then Error (TestFailed (x, t, idtest, got, eval_exp)) else Ok ()
351+
if not eq then
352+
Error
353+
(TestFailed
354+
( string_of_ray x
355+
, string_of_ray t
356+
, string_of_ray idtest
357+
, got
358+
, eval_exp ) )
359+
else Ok ()
348360
| Error e -> Error e )
349361
|> Result.all_unit
350362

351-
and default_interaction = Union (Focus (Id "tested"), Id "test")
363+
and default_interaction = Union (Focus (Id (const "tested")), Id (const "test"))
352364

353365
and default_expect =
354366
Raw (Const [ Unmarked { content = [ func "ok" [] ]; bans = [] } ])
355367

356368
and default_checker : galaxy_expr =
357369
Raw
358370
(Galaxy
359-
[ GLabelDef ("interaction", default_interaction)
360-
; GLabelDef ("expect", default_expect)
371+
[ GLabelDef (const "interaction", default_interaction)
372+
; GLabelDef (const "expect", default_expect)
361373
] )
362374

363375
and string_of_type_declaration ~notyping env = function
364376
| TDef (x, ts, None) ->
365-
Printf.sprintf " %s :: %s.\n" x (string_of_list Fn.id "," ts)
377+
let str_x = string_of_ray x in
378+
let str_ts = List.map ts ~f:string_of_ray in
379+
Printf.sprintf " %s :: %s.\n" str_x (string_of_list Fn.id "," str_ts)
366380
| TDef (x, ts, Some xck) ->
367-
Printf.sprintf " %s :: %s [%s].\n" x (string_of_list Fn.id "," ts) xck
381+
let str_x = string_of_ray x in
382+
let str_xck = string_of_ray xck in
383+
let str_ts = List.map ts ~f:string_of_ray in
384+
Printf.sprintf " %s :: %s [%s].\n" str_x
385+
(string_of_list Fn.id "," str_ts)
386+
str_xck
368387
| TExp (x, g) -> (
369388
match eval_galaxy_expr ~notyping env g with
370389
| Error _ -> failwith "Error: string_of_type_declaration"
371390
| Ok eval_g ->
372-
Printf.sprintf "%s :=: %s" x (string_of_galaxy ~notyping env eval_g) )
391+
let str_x = string_of_ray x in
392+
Printf.sprintf "%s :=: %s" str_x (string_of_galaxy ~notyping env eval_g) )
373393

374394
and string_of_galaxy_declaration ~notyping env = function
375395
| GLabelDef (k, v) -> begin
376396
match eval_galaxy_expr ~notyping env v with
377397
| Error _ -> failwith "Error: string_of_galaxy_declaration"
378398
| Ok eval_v ->
379-
Printf.sprintf " %s = %s\n" k (string_of_galaxy ~notyping env eval_v)
399+
let str_k = string_of_ray k in
400+
Printf.sprintf " %s = %s\n" str_k (string_of_galaxy ~notyping env eval_v)
380401
end
381402
| GTypeDef decl -> string_of_type_declaration ~notyping env decl
382403

383-
and string_of_galaxy ~notyping env : galaxy -> ident = function
404+
and string_of_galaxy ~notyping env : galaxy -> string = function
384405
| Const mcs -> mcs |> remove_mark_all |> string_of_constellation
385406
| Interface i ->
386407
let content =
@@ -393,13 +414,13 @@ and string_of_galaxy ~notyping env : galaxy -> ident = function
393414

394415
let rec eval_decl ~typecheckonly ~notyping env :
395416
declaration -> (env, err) Result.t = function
396-
| Def (x, _) when is_reserved x -> Error (ReservedWord x)
417+
| Def (x, _) when is_reserved x -> Error (ReservedWord (string_of_ray x))
397418
| Def (x, e) ->
398419
let env = { objs = add_obj env x e; types = env.types } in
399420
let* _ =
400421
if notyping then Ok ()
401422
else
402-
List.filter env.types ~f:(fun (y, _) -> equal_string x y)
423+
List.filter env.types ~f:(fun (y, _) -> equal_ray x y)
403424
|> List.map ~f:(fun (_, (ts, ck)) ->
404425
match ck with
405426
| None ->
@@ -408,7 +429,7 @@ let rec eval_decl ~typecheckonly ~notyping env :
408429
|> Result.all_unit
409430
| Some xck -> begin
410431
match get_obj env xck with
411-
| None -> Error (UnknownID xck)
432+
| None -> Error (UnknownID (string_of_ray xck))
412433
| Some obj_xck ->
413434
List.map ts ~f:(fun t -> typecheck ~notyping env x t obj_xck)
414435
|> Result.all_unit
@@ -419,7 +440,7 @@ let rec eval_decl ~typecheckonly ~notyping env :
419440
| Show _ when typecheckonly -> Ok env
420441
| Show (Id x) -> begin
421442
match get_obj env x with
422-
| None -> Error (UnknownID x)
443+
| None -> Error (UnknownID (string_of_ray x))
423444
| Some e -> eval_decl ~typecheckonly ~notyping env (Show e)
424445
end
425446
| Show (Raw (Galaxy g)) ->
@@ -459,14 +480,15 @@ let rec eval_decl ~typecheckonly ~notyping env :
459480
Ok { objs = env.objs; types = add_type env x (ts, ck) }
460481
| TypeDef (TExp (x, mcs)) ->
461482
Ok
462-
{ objs = add_obj env "^expect" (expect mcs)
463-
; types = add_type env x ([ "^empty" ], Some "^expect")
483+
{ objs = add_obj env (const "^expect") (expect mcs)
484+
; types = add_type env x ([ const "^empty" ], Some (const "^expect"))
464485
}
465486
| ProofDef (x, ts, ck, g) ->
466487
eval_decl ~typecheckonly ~notyping
467488
{ objs = add_obj env x g; types = add_type env x (ts, ck) }
468489
(Def (x, g))
469490
| Use path ->
491+
let path = List.map path ~f:string_of_ray in
470492
let formatted_filename = String.concat ~sep:"/" path ^ ".sg" in
471493
let lexbuf = Lexing.from_channel (Stdlib.open_in formatted_filename) in
472494
lexbuf.lex_curr_p <-

0 commit comments

Comments
 (0)