Skip to content

Commit

Permalink
Add sugar for type params in pats like in binds and decls
Browse files Browse the repository at this point in the history
For example, previously one could write

    foo (id: 'a => a => a) = ;; ...
    bar ({id: 'a => a => a}) = ;; ...

now one could also write

    foo (id 'a: a => a) = ;; ...
    bar ({id 'a: a => a}) = ;; ...

similarly to what can be written in declarations

    type FUN = {
      id 'a: a => a;
    }

The main motivation for this addition is to make it possible to
copy/cut-and-paste between declarations and patterns.
  • Loading branch information
polytypic committed Feb 23, 2020
1 parent 1e8299d commit 61931a7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
15 changes: 10 additions & 5 deletions parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,13 @@ paramlist :
| param paramlist
{ $1::$2 }
;
typparam :
| param
{ typParam $1 }
;
typparamlist :
| paramlist
{ List.map (fun p ->
match p.it with
| (b, {it = HoleT; at}, i) -> (b, TypT@@at, i)@@p.at
| _ -> p
) $1 }
{ typParamList $1 }
;
arrow :
| ARROW
Expand Down Expand Up @@ -386,6 +386,8 @@ atpat :
{ strP($2)@@at() }
| LPAR RPAR
{ strP([])@@at() }
| LPAR head typparam typparamlist COLON typ RPAR
{ annotP(varP($2)@@$2.at, funT($3::$4, $6, Pure@@at())@@at())@@at() }
| LPAR patlist RPAR
{ match $2 with [p] -> p | ps -> tupP(ps)@@at() }
| LPAR TYPE head typparamlist RPAR
Expand Down Expand Up @@ -429,6 +431,9 @@ atdecon :
{ [($1, annotP($5, $3)@@span[ati 2; ati 5])@@at()] }
| name COLON typ
{ [($1, annotP(varP($1.it@@ati 1)@@ati 1, $3)@@at())@@at()] }
| name typparam typparamlist COLON typ
{ [($1, annotP(varP($1)@@$1.at,
funT($2::$3, $5, Pure@@at())@@at())@@at())@@at()] }
| TYPE name typparamlist
{ [($2, annotP(varP($2.it@@ati 2)@@ati 2,
funT($3, TypT@@ati 1, Pure@@ati 1)@@at())@@at())@@at()] }
Expand Down
5 changes: 4 additions & 1 deletion regression.1ml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ Equivalence: {

hole_is_allowed_type_pattern (type _ _) = ();

typparams_allowed (id 'a: a => a) ({alsoId 'x: x => x}) =
alsoId (id 1, id "one", alsoId true);

;;

type_error { type_error 101 };
Expand Down Expand Up @@ -167,7 +170,7 @@ in {

ListN = {
...ListN;
map 'x 'y (xy: x -> y) = rec (map: 'n => t x n -> t y n) =>
map 'x 'y (xy: x -> y) = rec (map 'n: t x n -> t y n) =>
case (t _) {
nil = nil;
(::) x xs = xy x :: map xs;
Expand Down
9 changes: 9 additions & 0 deletions syntax.ml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ let var s =

let index n = "_" ^ string_of_int n

(* Helpers *)

let typParam param =
match param.it with
| (b, {it = HoleT; at}, i) -> (b, TypT@@at, i)@@param.at
| _ -> param

let typParamList paramList =
List.map typParam paramList

(* Sugar *)

Expand Down

0 comments on commit 61931a7

Please sign in to comment.