Skip to content

Commit 5dae6c3

Browse files
committed
refactor: simplify examples
1 parent e190580 commit 5dae6c3

File tree

2 files changed

+29
-34
lines changed

2 files changed

+29
-34
lines changed

examples/stlc/Checker.ml

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@ struct
1313
let bind_var nm tp k =
1414
Reader.scope (fun env -> Snoc(env, (nm, tp))) k
1515

16-
let lookup ?loc nm =
16+
let lookup nm =
1717
let ctx = Reader.read () in
1818
match Bwd.find_opt (fun (nm', _) -> String.equal nm nm') ctx with
1919
| Some (_, tp) -> tp
2020
| None ->
21-
Reporter.fatalf ?loc `UnboundVariable "variable '%s' is not in scope" nm
21+
Reporter.fatalf UnboundVariable "variable '%s' is not in scope" nm
2222

23-
let expected_connective ?loc conn tp =
24-
Reporter.fatalf ?loc `TypeError "expected a %s, but got %a" conn pp_tp tp
23+
let expected_connective conn tp =
24+
Reporter.fatalf TypeError "expected a %s, but got %a" conn pp_tp tp
2525

26-
let rec equate ?loc expected actual =
27-
Reporter.tracef ?loc "when equating terms" @@ fun () ->
26+
let rec equate expected actual =
27+
Reporter.trace "when equating terms" @@ fun () ->
2828
match expected, actual with
2929
| Fun (a0, b0), Fun (a1, b1) ->
3030
equate a0 a1;
@@ -35,7 +35,7 @@ struct
3535
| Nat, Nat ->
3636
()
3737
| _, _ ->
38-
Reporter.fatalf ?loc `TypeError "expected type %a, but got %a" pp_tp expected pp_tp actual
38+
Reporter.fatalf TypeError "expected type %a, but got %a" pp_tp expected pp_tp actual
3939

4040
let rec chk (tm : tm) (tp : tp) : unit =
4141
Reporter.tracef ?loc:tm.loc "when checking it against %a" Syntax.pp_tp tp @@ fun () ->
@@ -44,53 +44,53 @@ struct
4444
bind_var nm a @@ fun () ->
4545
chk body b
4646
| Lam (_, _), _ ->
47-
expected_connective ?loc:tm.loc "function type" tp
47+
expected_connective "function type" tp
4848
| Pair (l, r), Tuple (a, b) ->
4949
chk l a;
5050
chk r b;
5151
| Pair (_, _), _ ->
52-
expected_connective ?loc:tm.loc "pair type" tp
52+
expected_connective "pair type" tp
5353
| Lit _, Nat ->
5454
()
5555
| Lit _, _ ->
56-
expected_connective ?loc:tm.loc "" tp
56+
expected_connective "" tp
5757
| Suc n, Nat ->
5858
chk n Nat
5959
| Suc _, _ ->
60-
expected_connective ?loc:tm.loc "" tp
60+
expected_connective "" tp
6161
| _ ->
6262
let actual_tp = syn tm in
63-
equate ?loc:tm.loc tp actual_tp
63+
equate tp actual_tp
6464

6565
and syn (tm : tm) : tp =
66-
Reporter.tracef ?loc:tm.loc "when synthesizing its type" @@ fun () ->
66+
Reporter.trace ?loc:tm.loc "when synthesizing its type" @@ fun () ->
6767
match tm.value with
6868
| Var nm ->
69-
lookup ?loc:tm.loc nm
69+
lookup nm
7070
| Ap (fn, arg) ->
7171
begin
7272
match syn fn with
7373
| Fun (a, b) ->
7474
chk arg a;
7575
b
7676
| tp ->
77-
expected_connective ?loc:tm.loc "function type" tp
77+
expected_connective "function type" tp
7878
end
7979
| Fst tm ->
8080
begin
8181
match syn tm with
8282
| Tuple (l, _) ->
8383
l
8484
| tp ->
85-
expected_connective ?loc:tm.loc "pair type" tp
85+
expected_connective "pair type" tp
8686
end
8787
| Snd tm ->
8888
begin
8989
match syn tm with
9090
| Tuple (_, r) ->
9191
r
9292
| tp ->
93-
expected_connective ?loc:tm.loc "pair type" tp
93+
expected_connective "pair type" tp
9494
end
9595
| NatRec (z, s, scrut) ->
9696
begin
@@ -100,7 +100,7 @@ struct
100100
mot
101101
end
102102
| _ ->
103-
Reporter.fatalf ?loc:tm.loc `TypeError "unable to infer its type"
103+
Reporter.fatal TypeError "unable to infer its type"
104104
end
105105

106106
module Driver =
@@ -111,12 +111,10 @@ struct
111111
let (tm, tp) =
112112
try Grammar.defn Lex.token lexbuf with
113113
| Lex.SyntaxError tok ->
114-
Reporter.fatalf ~loc:(Asai.Range.of_lexbuf lexbuf) `LexingError "unrecognized token %S" tok
114+
Reporter.fatalf ~loc:(Asai.Range.of_lexbuf lexbuf) ParsingError "unrecognized token %S" tok
115115
| Grammar.Error ->
116-
Reporter.fatalf ~loc:(Asai.Range.of_lexbuf lexbuf) `LexingError "failed to parse"
117-
in
118-
Elab.Reader.run ~env:Emp @@ fun () ->
119-
Elab.chk tm tp
116+
Reporter.fatal ~loc:(Asai.Range.of_lexbuf lexbuf) ParsingError "failed to parse"
117+
in Elab.Reader.run ~env:Emp @@ fun () -> Elab.chk tm tp
120118

121119
let load mode filepath =
122120
let display : Reporter.Message.t Asai.Diagnostic.t -> unit =

examples/stlc/Reporter.ml

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
module Message =
22
struct
33
type t =
4-
[ `TypeError (* Type checking failed *)
5-
| `UnboundVariable (* Unbound variable *)
6-
| `RequiresAnnotation (* Unable to infer the type *)
7-
| `LexingError (* The lexer encountered an error *)
8-
| `ParsingError (* Parsing errors *)
9-
]
4+
| TypeError (* Type checking failed *)
5+
| UnboundVariable (* Unbound variable *)
6+
| RequiresAnnotation (* Unable to infer the type *)
7+
| ParsingError (* Parsing errors *)
108

119
let default_severity _ = Asai.Diagnostic.Error
1210

1311
let short_code : t -> string =
1412
function
15-
| `TypeError -> "E001"
16-
| `UnboundVariable -> "E002"
17-
| `RequiresAnnotation -> "E003"
18-
| `LexingError -> "E004"
19-
| `ParsingError -> "E005"
13+
| TypeError -> "E001"
14+
| UnboundVariable -> "E002"
15+
| RequiresAnnotation -> "E003"
16+
| ParsingError -> "E004"
2017
end
2118

2219
include Asai.Reporter.Make(Message)

0 commit comments

Comments
 (0)