Skip to content

Commit b3a22f8

Browse files
Syntactic sugar for explicit instantiations with (effectful) functions (#269)
Added syntactic sugar for explicit instantiations with (effectful) functions, thus solving #228 and #232. Added 2 tests for this new feature.
1 parent 9c3fc4c commit b3a22f8

File tree

5 files changed

+44
-4
lines changed

5 files changed

+44
-4
lines changed

src/DblParser/Desugar.ml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ and tr_scheme_field (fld : Raw.ty_field) =
197197
make (SA_Val(n, scheme_wildcard fld.pos))
198198
| FldNameVal(n, tp) ->
199199
make (SA_Val(n, tr_scheme_expr tp))
200-
| FldNameAnnot _ | FldModule _ | FldOpen ->
200+
| FldNameAnnot _ | FldModule _ | FldOpen | FldNameFn _ | FldNameEffectFn _ ->
201201
assert false
202202

203203
(** Translate a type expression as a type variable with kind annotation *)
@@ -377,6 +377,8 @@ and tr_named_pattern ~public (fld : Raw.field) =
377377
| FldModule { data = NPName name; _ } -> make (NP_Module(public, name))
378378
| FldModule _ -> Error.fatal (Error.desugar_error fld.pos)
379379
| FldOpen -> make (NP_Open public)
380+
| FldNameFn _ | FldNameEffectFn _ ->
381+
Error.fatal (Error.desugar_error fld.pos)
380382

381383
(** Translate a parameter declaration *)
382384
let tr_param_decl (fld : Raw.field) =
@@ -392,8 +394,8 @@ let tr_param_decl (fld : Raw.field) =
392394
Either.Right (n, ident_of_name n, None)
393395
| FldNameAnnot(n, sch) ->
394396
Either.Right (n, ident_of_name n, Some (tr_scheme_expr sch))
395-
396-
| FldTypeVal _ | FldNameVal _ | FldModule _ | FldOpen ->
397+
| FldTypeVal _ | FldNameVal _ | FldModule _ | FldOpen
398+
| FldNameFn _ | FldNameEffectFn _ ->
397399
Error.fatal (Error.desugar_error fld.pos)
398400

399401
(** Translate an expression as a let-pattern. *)
@@ -675,6 +677,13 @@ and tr_explicit_inst (fld : Raw.field) =
675677
| FldOpen -> make IOpen
676678
| FldNameAnnot _ | FldType(_, Some _) ->
677679
Error.fatal (Error.desugar_error fld.pos)
680+
| FldNameFn (n, es, e) ->
681+
make (IVal(n, tr_poly_expr_def ({pos = fld.pos; data = EFn(es, e)})))
682+
| FldNameEffectFn (n, label, es, resumption, e) ->
683+
make (IVal(n, tr_poly_expr_def
684+
{ pos = fld.pos;
685+
data = EEffect({label; args = es; resumption; body = e})
686+
}))
678687

679688
and tr_def ?(public=false) (def : Raw.def) =
680689
let pos = def.pos in

src/DblParser/Raw.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ type ('tp, 'e) field_data =
7878
| FldNameVal of name * 'e
7979
(** Named implicit parameter together with a value *)
8080

81+
| FldNameFn of name * 'e list * 'e
82+
(** Explicit instantiation with a function *)
83+
84+
| FldNameEffectFn of name * 'e option * 'e list * 'e option * 'e
85+
(** Explicit instantiation with an effectful function *)
86+
8187
| FldNameAnnot of name * 'tp
8288
(** type-annotated implicit parameter *)
8389

src/DblParser/YaccParser.mly

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ op_50
103103

104104
op_60
105105
: OP_60 { make $1 }
106-
| EQ { make "=" }
107106
;
108107

109108

@@ -485,6 +484,11 @@ field
485484
| UID { make (FldType($1, None)) }
486485
| UID EQ ty_expr { make (FldTypeVal($1, $3)) }
487486
| UID COLON kind_expr { make (FldType($1, Some $3)) }
487+
| name expr_250_list1 EQ expr_no_comma
488+
{ make (FldNameFn($1, $2, $4)) }
489+
| effect_label_opt KW_EFFECT name
490+
expr_250_list1 effect_resumption_opt EQ expr_no_comma
491+
{ make (FldNameEffectFn($3, $1, $4, $5, $7)) }
488492
| name { make (FldName $1) }
489493
| name EQ expr_no_comma { make (FldNameVal($1, $3)) }
490494
| name COLON ty_expr { make (FldNameAnnot($1, $3)) }
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
data Triples X Y Z = Triple of X, Y, Z
2+
3+
let _ =
4+
let f {type X, type Y, foo : X -> Y -> Triples X Y Y, bar : X ->[] X} x y =
5+
foo (bar x) y
6+
in
7+
match f {foo x y = (Triple x y y), bar (x : Unit) = x} () () with
8+
| Triple _ _ _ => ()
9+
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
data BT E =
2+
{ flip : Unit ->[E] Bool
3+
, fail : {type X} -> Unit ->[E] X
4+
}
5+
6+
let fun1 {E} (s : BT E) =
7+
s.fail ()
8+
9+
let _ = handle ~bt = BT
10+
{ effect flip () / r = r True; r False
11+
, effect fail () = ()
12+
} in fun1 ~bt

0 commit comments

Comments
 (0)