Skip to content

Commit a01f037

Browse files
committed
Add type_check binding form
This is similar to the previously added `type_error` binding form except that the expression is expected to pass elaboration without errors.
1 parent ad9080b commit a01f037

4 files changed

Lines changed: 15 additions & 8 deletions

File tree

elab.ml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,11 @@ and elab_bind env bind l =
665665
);
666666
s, p, zs, e
667667

668-
| EL.TypeErrorB(exp) ->
668+
| EL.TypeAssertB(true, exp) ->
669+
let _ = elab_exp env exp l in
670+
ExT([], StrT[]), Pure, [], IL.TupE[]
671+
672+
| EL.TypeAssertB(false, exp) ->
669673
(match try Some (elab_exp env exp l) with _ -> None with
670674
| None -> ExT([], StrT[]), Pure, [], IL.TupE[]
671675
| Some (s, _, _, _) ->

lexer.mll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ rule token = parse
265265
| "as" { AS }
266266
| "do" { DO }
267267
| "else" { ELSE }
268+
| "type_check" { TYPE_CHECK }
268269
| "type_error" { TYPE_ERROR }
269270
| "fun" { FUN }
270271
| "if" { IF }

parser.mly

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ let parse_error s = raise (Source.Error (Source.nowhere_region, s))
3434
%token LBRACE RBRACE
3535
%token DOT TICK
3636
%token COMMA SEMI
37-
%token TYPE_ERROR
37+
%token TYPE_CHECK TYPE_ERROR
3838
%token LOCAL
3939
%token IMPORT
4040
%token WRAP_OP UNWRAP_OP
@@ -498,8 +498,10 @@ atbind :
498498
{ inclB($2)@@at() }
499499
| DO exp
500500
{ doB($2)@@at() }
501+
| TYPE_CHECK exp
502+
{ TypeAssertB(true, $2)@@at() }
501503
| TYPE_ERROR exp
502-
{ TypeErrorB($2)@@at() }
504+
{ TypeAssertB(false, $2)@@at() }
503505
| LET bind IN exp
504506
{ inclB(letE($2, $4)@@at())@@at() }
505507
| IMPORT TEXT

syntax.ml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ and bind' =
6161
| SeqB of bind * bind
6262
| VarB of var * exp
6363
| InclB of exp
64-
| TypeErrorB of exp
64+
| TypeAssertB of bool * exp
6565

6666

6767
let uniq_count = ref 0
@@ -73,7 +73,7 @@ let rec every_var pr b =
7373
| SeqB(b1, b2) -> every_var pr b1 && every_var pr b2
7474
| VarB(v, _) -> pr v
7575
| InclB(_) -> false
76-
| TypeErrorB(_) -> true
76+
| TypeAssertB(_) -> true
7777

7878
let index n = "_" ^ string_of_int n
7979

@@ -419,7 +419,7 @@ let label_of_bind b =
419419
| SeqB _ -> "SeqB"
420420
| VarB _ -> "VarB"
421421
| InclB _ -> "InclB"
422-
| TypeErrorB _ -> "TypeErrorB"
422+
| TypeAssertB _ -> "TypeAssertB"
423423

424424

425425
let string_of_var x = "\"" ^ x.it ^ "\""
@@ -480,7 +480,7 @@ and string_of_bind b =
480480
| SeqB(b1, b2) -> node' [string_of_bind b1; string_of_bind b2]
481481
| VarB(x, e) -> node' [string_of_var x; string_of_exp e]
482482
| InclB(e) -> node' [string_of_exp e]
483-
| TypeErrorB(e) -> node' [string_of_exp e]
483+
| TypeAssertB(b, e) -> node' [string_of_bool b; string_of_exp e]
484484

485485
(* Import *)
486486

@@ -528,4 +528,4 @@ and imports_bind bind =
528528
| SeqB(bind1, bind2) -> imports_bind bind1 @ imports_bind bind2
529529
| VarB(_, exp) -> imports_exp exp
530530
| InclB exp -> imports_exp exp
531-
| TypeErrorB exp -> imports_exp exp
531+
| TypeAssertB(_, exp) -> imports_exp exp

0 commit comments

Comments
 (0)