Skip to content

Commit 7f922d0

Browse files
committed
Polymorphic parameters
1 parent 51aeb04 commit 7f922d0

File tree

26 files changed

+20292
-14955
lines changed

26 files changed

+20292
-14955
lines changed

boot/menhir/menhirLib.ml

Lines changed: 129 additions & 176 deletions
Large diffs are not rendered by default.

boot/menhir/menhirLib.mli

Lines changed: 117 additions & 152 deletions
Large diffs are not rendered by default.

boot/menhir/parser.ml

Lines changed: 19503 additions & 14397 deletions
Large diffs are not rendered by default.

lambda/translcore.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1241,7 +1241,6 @@ and transl_let ~scopes ?(add_regions=false) ?(in_structure=false)
12411241
List.map
12421242
(fun {vb_pat=pat} -> match pat.pat_desc with
12431243
Tpat_var (id,_) -> id
1244-
| Tpat_alias ({pat_desc=Tpat_any}, id,_) -> id
12451244
| _ -> assert false)
12461245
pat_expr_list in
12471246
let transl_case {vb_expr=expr; vb_attributes; vb_loc; vb_pat} id =

parsing/parser.mly

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2299,6 +2299,12 @@ labeled_simple_pattern:
22992299
{ (Nolabel, None, $1) }
23002300
| LPAREN LOCAL let_pattern RPAREN
23012301
{ (Nolabel, None, mkpat_stack $3 (make_loc $loc($2))) }
2302+
| LABEL LPAREN poly_pattern RPAREN
2303+
{ (Labelled $1, None, $3) }
2304+
| LABEL LPAREN LOCAL poly_pattern RPAREN
2305+
{ (Labelled $1, None, mkpat_stack $4 (make_loc $loc($2))) }
2306+
| LPAREN poly_pattern RPAREN
2307+
{ (Nolabel, None, $2) }
23022308
;
23032309

23042310
pattern_var:
@@ -2319,6 +2325,11 @@ label_let_pattern:
23192325
{ let lab, pat = x in
23202326
lab,
23212327
mkpat ~loc:$sloc (Ppat_constraint (pat, cty)) }
2328+
| x = label_var COLON
2329+
cty = mktyp (vars = typevar_list DOT ty = core_type { Ptyp_poly(vars, ty) })
2330+
{ let lab, pat = x in
2331+
lab,
2332+
mkpat ~loc:$sloc (Ppat_constraint (pat, cty)) }
23222333
;
23232334
%inline label_var:
23242335
mkrhs(LIDENT)
@@ -2330,6 +2341,17 @@ let_pattern:
23302341
| mkpat(pattern COLON core_type
23312342
{ Ppat_constraint($1, $3) })
23322343
{ $1 }
2344+
| poly_pattern
2345+
{ $1 }
2346+
;
2347+
%inline poly_pattern:
2348+
mkpat(
2349+
pat = pattern
2350+
COLON
2351+
cty = mktyp(vars = typevar_list DOT ty = core_type
2352+
{ Ptyp_poly(vars, ty) })
2353+
{ Ppat_constraint(pat, cty) })
2354+
{ $1 }
23332355
;
23342356

23352357
%inline indexop_expr(dot, index, right):
@@ -3508,7 +3530,7 @@ strict_function_type:
35083530
| mktyp(
35093531
label = arg_label
35103532
local = optional_local
3511-
domain = extra_rhs(tuple_type)
3533+
domain = extra_rhs(param_type)
35123534
MINUSGREATER
35133535
codomain = strict_function_type
35143536
{ Ptyp_arrow(label, mktyp_local_if local domain $loc(local), codomain) }
@@ -3517,7 +3539,7 @@ strict_function_type:
35173539
| mktyp(
35183540
label = arg_label
35193541
arg_local = optional_local
3520-
domain = extra_rhs(tuple_type)
3542+
domain = extra_rhs(param_type)
35213543
MINUSGREATER
35223544
ret_local = optional_local
35233545
codomain = tuple_type
@@ -3543,6 +3565,15 @@ strict_function_type:
35433565
| LOCAL
35443566
{ true }
35453567
;
3568+
%inline param_type:
3569+
| mktyp(
3570+
LPAREN vars = typevar_list DOT ty = core_type RPAREN
3571+
{ Ptyp_poly(vars, ty) }
3572+
)
3573+
{ $1 }
3574+
| ty = tuple_type
3575+
{ ty }
3576+
;
35463577
(* Tuple types include:
35473578
- atomic types (see below);
35483579
- proper tuple types: int * int * int list

testsuite/tests/typing-misc/typecore_errors.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,18 @@ Error: Uninterpreted extension 'ext'.
7474

7575
let rec f x = ( (), () : _ -> _ -> _ )
7676
[%%expect{|
77-
Line 3, characters 14-38:
77+
Line 3, characters 16-22:
7878
3 | let rec f x = ( (), () : _ -> _ -> _ )
79-
^^^^^^^^^^^^^^^^^^^^^^^^
79+
^^^^^^
8080
Error: This expression has type 'a * 'b
8181
but an expression was expected of type 'c -> 'd -> 'e
8282
|}]
8383

8484
let rec g x = ( ((), ()) : _ -> _ :> _ )
8585
[%%expect{|
86-
Line 1, characters 14-40:
86+
Line 1, characters 16-24:
8787
1 | let rec g x = ( ((), ()) : _ -> _ :> _ )
88-
^^^^^^^^^^^^^^^^^^^^^^^^^^
88+
^^^^^^^^
8989
Error: This expression has type 'a * 'b
9090
but an expression was expected of type 'c -> 'd
9191
|}]

testsuite/tests/typing-objects/Exemples.ml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -541,10 +541,7 @@ Error: Type
541541
< cmp : int_comparable2 -> int; set_x : int -> unit; x : int >
542542
is not a subtype of
543543
int_comparable = < cmp : int_comparable -> int; x : int >
544-
Type int_comparable = < cmp : int_comparable -> int; x : int >
545-
is not a subtype of
546-
int_comparable2 =
547-
< cmp : int_comparable2 -> int; set_x : int -> unit; x : int >
544+
Type int_comparable is not a subtype of int_comparable2
548545
The first object type has no method set_x
549546
|}];; (* Fail : 'a comp2 is not a subtype *)
550547
(new sorted_list ())#add c2;;

toplevel/topdirs.ml

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,13 @@ let rec extract_last_arrow desc =
199199
try extract_last_arrow r
200200
with Bad_printing_function -> res
201201

202-
let extract_target_type ty = fst (extract_last_arrow ty)
202+
let extract_target_type ty =
203+
let ty = fst (extract_last_arrow ty) in
204+
match Ctype.filter_mono ty with
205+
| exception Ctype.Filter_mono_failed ->
206+
raise Bad_printing_function
207+
| ty -> ty
208+
203209
let extract_target_parameters ty =
204210
let ty = extract_target_type ty |> Ctype.expand_head !toplevel_env in
205211
match get_desc ty with
@@ -246,10 +252,12 @@ let match_generic_printer_type desc path args printer_type =
246252
List.map (fun ty_var -> Ctype.newconstr printer_type [ty_var]) args in
247253
let ty_expected =
248254
List.fold_right
249-
(fun ty_arg ty -> Ctype.newty
250-
(Tarrow ((Asttypes.Nolabel,Alloc_mode.global,Alloc_mode.global),
251-
ty_arg, ty,
252-
commu_var ())))
255+
(fun ty_arg ty ->
256+
let arrow_desc =
257+
Asttypes.Nolabel,Alloc_mode.global,Alloc_mode.global
258+
in
259+
Ctype.newty
260+
(Tarrow (arrow_desc, Ctype.newmono ty_arg, ty, commu_var ())))
253261
ty_args (Ctype.newconstr printer_type [ty_target]) in
254262
begin try
255263
Ctype.unify !toplevel_env

typing/btype.ml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ let newmarkedgenvar () =
124124
let is_Tvar ty = match get_desc ty with Tvar _ -> true | _ -> false
125125
let is_Tunivar ty = match get_desc ty with Tunivar _ -> true | _ -> false
126126
let is_Tconstr ty = match get_desc ty with Tconstr _ -> true | _ -> false
127+
let is_Tpoly ty = match get_desc ty with Tpoly _ -> true | _ -> false
127128

128129
let dummy_method = "*dummy method*"
129130

@@ -601,6 +602,7 @@ let rec signature_of_class_type =
601602
| Cty_signature sign -> sign
602603
| Cty_arrow (_, _, cty) -> signature_of_class_type cty
603604

605+
604606
let rec class_body cty =
605607
match cty with
606608
Cty_constr _ ->
@@ -702,6 +704,26 @@ let instance_variable_type label sign =
702704
| (_, _, ty) -> ty
703705
| exception Not_found -> assert false
704706

707+
(********************************)
708+
(* Utilities for poly types *)
709+
(********************************)
710+
711+
let tpoly_is_mono ty =
712+
match get_desc ty with
713+
| Tpoly(_, []) -> true
714+
| Tpoly(_, _ :: _) -> false
715+
| _ -> assert false
716+
717+
let tpoly_get_poly ty =
718+
match get_desc ty with
719+
| Tpoly(ty, vars) -> (ty, vars)
720+
| _ -> assert false
721+
722+
let tpoly_get_mono ty =
723+
match get_desc ty with
724+
| Tpoly(ty, []) -> ty
725+
| _ -> assert false
726+
705727
(**********************************)
706728
(* Utilities for level-marking *)
707729
(**********************************)

typing/btype.mli

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ val newmarkedgenvar: unit -> type_expr
7676
val is_Tvar: type_expr -> bool
7777
val is_Tunivar: type_expr -> bool
7878
val is_Tconstr: type_expr -> bool
79+
val is_Tpoly: type_expr -> bool
80+
7981
val dummy_method: label
8082

8183
(**** polymorphic variants ****)
@@ -105,6 +107,13 @@ val proxy: type_expr -> type_expr
105107
(* Return the proxy representative of the type: either itself
106108
or a row variable *)
107109

110+
(* Poly types. *)
111+
112+
(* These three functions can only be called on [Tpoly] nodes. *)
113+
val tpoly_is_mono : type_expr -> bool
114+
val tpoly_get_mono : type_expr -> type_expr
115+
val tpoly_get_poly : type_expr -> type_expr * type_expr list
116+
108117
(**** Utilities for private abbreviations with fixed rows ****)
109118
val row_of_type: type_expr -> type_expr
110119
val has_constr_row: type_expr -> bool

0 commit comments

Comments
 (0)