1995-08-09 08:06:35 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Objective Caml *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* Automatique. Distributed only by permission. *)
|
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Typechecking of type expressions for the core language *)
|
|
|
|
|
1996-04-22 04:15:41 -07:00
|
|
|
open Misc
|
1995-05-04 03:15:53 -07:00
|
|
|
open Parsetree
|
1996-09-23 04:33:27 -07:00
|
|
|
open Types
|
1995-05-04 03:15:53 -07:00
|
|
|
open Ctype
|
|
|
|
|
|
|
|
exception Already_bound
|
|
|
|
|
|
|
|
type error =
|
|
|
|
Unbound_type_variable of string
|
|
|
|
| Unbound_type_constructor of Longident.t
|
|
|
|
| Type_arity_mismatch of Longident.t * int * int
|
1996-04-22 04:15:41 -07:00
|
|
|
| Bound_type_variable of string
|
|
|
|
| Recursive_type
|
|
|
|
| Unbound_class of Longident.t
|
|
|
|
| Unbound_row_variable of Longident.t
|
1997-02-20 12:39:02 -08:00
|
|
|
| Type_mismatch of (type_expr * type_expr) list
|
|
|
|
| Alias_type_mismatch of (type_expr * type_expr) list
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
exception Error of Location.t * error
|
|
|
|
|
|
|
|
(* Translation of type expressions *)
|
|
|
|
|
|
|
|
let type_variables = ref (Tbl.empty : (string, type_expr) Tbl.t)
|
1997-01-20 09:11:47 -08:00
|
|
|
let aliases = ref (Tbl.empty : (string, type_expr) Tbl.t)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1997-01-20 09:11:47 -08:00
|
|
|
let used_variables = ref (Tbl.empty : (string, type_expr) Tbl.t)
|
|
|
|
let bindings = ref ([] : (type_expr * type_expr) list)
|
|
|
|
(* These two variables are used for the "delayed" policy. *)
|
1996-10-26 08:36:11 -07:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
let reset_type_variables () =
|
1996-04-22 04:15:41 -07:00
|
|
|
reset_global_level ();
|
1997-01-20 09:11:47 -08:00
|
|
|
type_variables := Tbl.empty
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1996-05-26 06:42:34 -07:00
|
|
|
let enter_type_variable strict name =
|
1995-05-04 03:15:53 -07:00
|
|
|
try
|
1996-05-26 06:42:34 -07:00
|
|
|
let v = Tbl.find name !type_variables in
|
|
|
|
if strict then raise Already_bound;
|
|
|
|
v
|
1995-05-04 03:15:53 -07:00
|
|
|
with Not_found ->
|
1995-10-31 07:58:31 -08:00
|
|
|
let v = new_global_var() in
|
1995-05-04 03:15:53 -07:00
|
|
|
type_variables := Tbl.add name v !type_variables;
|
|
|
|
v
|
|
|
|
|
1996-04-22 04:15:41 -07:00
|
|
|
let type_variable loc name =
|
|
|
|
try
|
|
|
|
Tbl.find name !type_variables
|
|
|
|
with Not_found ->
|
|
|
|
raise(Error(loc, Unbound_type_variable name))
|
|
|
|
|
1997-01-20 09:11:47 -08:00
|
|
|
type policy = Fixed | Extensible | Delayed
|
1996-04-22 04:15:41 -07:00
|
|
|
|
1997-01-20 09:11:47 -08:00
|
|
|
let rec transl_type env policy styp =
|
1995-05-04 03:15:53 -07:00
|
|
|
match styp.ptyp_desc with
|
1997-02-11 10:24:47 -08:00
|
|
|
Ptyp_any -> new_global_var()
|
|
|
|
| Ptyp_var name ->
|
1997-01-20 09:11:47 -08:00
|
|
|
begin try Tbl.find name !aliases with Not_found ->
|
|
|
|
match policy with
|
|
|
|
Fixed ->
|
|
|
|
begin try
|
|
|
|
Tbl.find name !type_variables
|
|
|
|
with Not_found ->
|
|
|
|
raise(Error(styp.ptyp_loc, Unbound_type_variable name))
|
|
|
|
end
|
|
|
|
| Extensible ->
|
|
|
|
begin try
|
|
|
|
Tbl.find name !type_variables
|
|
|
|
with Not_found ->
|
|
|
|
let v = new_global_var () in
|
|
|
|
type_variables := Tbl.add name v !type_variables;
|
|
|
|
v
|
|
|
|
end
|
|
|
|
| Delayed ->
|
|
|
|
begin try
|
|
|
|
Tbl.find name !used_variables
|
|
|
|
with Not_found -> try
|
|
|
|
let v1 = Tbl.find name !type_variables in
|
|
|
|
let v2 = new_global_var () in
|
|
|
|
used_variables := Tbl.add name v2 !used_variables;
|
|
|
|
bindings := (v1, v2)::!bindings;
|
|
|
|
v2
|
|
|
|
with Not_found ->
|
|
|
|
let v = new_global_var () in
|
|
|
|
type_variables := Tbl.add name v !type_variables;
|
|
|
|
used_variables := Tbl.add name v !used_variables;
|
|
|
|
v
|
|
|
|
end
|
1995-05-04 03:15:53 -07:00
|
|
|
end
|
|
|
|
| Ptyp_arrow(st1, st2) ->
|
1997-01-20 09:11:47 -08:00
|
|
|
let ty1 = transl_type env policy st1 in
|
|
|
|
let ty2 = transl_type env policy st2 in
|
1997-02-20 12:39:02 -08:00
|
|
|
newty (Tarrow(ty1, ty2))
|
1995-05-04 03:15:53 -07:00
|
|
|
| Ptyp_tuple stl ->
|
1997-02-20 12:39:02 -08:00
|
|
|
newty (Ttuple(List.map (transl_type env policy) stl))
|
1997-03-07 14:26:29 -08:00
|
|
|
| Ptyp_constr(lid, stl) ->
|
1995-05-04 03:15:53 -07:00
|
|
|
let (path, decl) =
|
|
|
|
try
|
|
|
|
Env.lookup_type lid env
|
|
|
|
with Not_found ->
|
|
|
|
raise(Error(styp.ptyp_loc, Unbound_type_constructor lid)) in
|
|
|
|
if List.length stl <> decl.type_arity then
|
|
|
|
raise(Error(styp.ptyp_loc, Type_arity_mismatch(lid, decl.type_arity,
|
|
|
|
List.length stl)));
|
1997-03-07 14:26:29 -08:00
|
|
|
let args = List.map (transl_type env policy) stl in
|
|
|
|
let cstr = newty (Tconstr(path, args, ref Mnil)) in
|
1997-02-20 12:39:02 -08:00
|
|
|
let params = Ctype.instance_list decl.type_params in
|
|
|
|
List.iter2
|
|
|
|
(fun (sty, ty) ty' ->
|
|
|
|
try unify env ty ty' with Unify trace ->
|
|
|
|
raise (Error(sty.ptyp_loc, Type_mismatch trace)))
|
|
|
|
(List.combine stl args) params;
|
1996-04-22 04:15:41 -07:00
|
|
|
cstr
|
1997-03-07 14:26:29 -08:00
|
|
|
| Ptyp_object fields ->
|
1997-01-20 09:11:47 -08:00
|
|
|
newobj (transl_fields env policy fields)
|
1997-03-07 14:26:29 -08:00
|
|
|
| Ptyp_class(lid, stl) ->
|
1997-01-20 09:11:47 -08:00
|
|
|
if policy = Fixed then
|
1996-04-22 04:15:41 -07:00
|
|
|
raise(Error(styp.ptyp_loc, Unbound_row_variable lid));
|
|
|
|
let lid2 =
|
|
|
|
match lid with
|
|
|
|
Longident.Lident s -> Longident.Lident ("#" ^ s)
|
1997-02-20 12:39:02 -08:00
|
|
|
| Longident.Ldot(r, s) -> Longident.Ldot (r, "#" ^ s)
|
|
|
|
| Longident.Lapply(_, _) -> fatal_error "Typetexp.transl_type"
|
1996-04-22 04:15:41 -07:00
|
|
|
in
|
|
|
|
let (path, decl) =
|
|
|
|
try
|
|
|
|
Env.lookup_type lid2 env
|
|
|
|
with Not_found ->
|
|
|
|
raise(Error(styp.ptyp_loc, Unbound_class lid)) in
|
|
|
|
if List.length stl <> decl.type_arity then
|
|
|
|
raise(Error(styp.ptyp_loc, Type_arity_mismatch(lid, decl.type_arity,
|
|
|
|
List.length stl)));
|
1997-03-07 14:26:29 -08:00
|
|
|
let cstr' = new_global_var () in
|
|
|
|
let args = List.map (transl_type env policy) stl in
|
|
|
|
let ty = expand_abbrev env path args (ref Mnil) cstr'.level in
|
1997-02-20 12:39:02 -08:00
|
|
|
let params = Ctype.instance_list decl.type_params in
|
|
|
|
List.iter2
|
|
|
|
(fun (sty, ty') ty ->
|
|
|
|
try unify env ty ty' with Unify trace ->
|
|
|
|
raise (Error(sty.ptyp_loc, Type_mismatch trace)))
|
|
|
|
(List.combine stl args) params;
|
1996-04-22 04:15:41 -07:00
|
|
|
ty
|
1997-03-07 14:26:29 -08:00
|
|
|
| Ptyp_alias(st, alias) ->
|
|
|
|
begin try
|
|
|
|
Tbl.find alias !type_variables;
|
|
|
|
raise(Error(styp.ptyp_loc, Bound_type_variable alias))
|
|
|
|
with Not_found -> try
|
|
|
|
Tbl.find alias !aliases;
|
|
|
|
raise(Error(styp.ptyp_loc, Bound_type_variable alias))
|
|
|
|
with Not_found ->
|
|
|
|
let ty' = newvar () in
|
|
|
|
aliases := Tbl.add alias ty' !aliases;
|
|
|
|
let ty = transl_type env policy st in
|
|
|
|
begin try unify env ty ty' with Unify trace ->
|
|
|
|
raise(Error(styp.ptyp_loc, Alias_type_mismatch trace))
|
|
|
|
end;
|
|
|
|
ty
|
|
|
|
end
|
1996-04-22 04:15:41 -07:00
|
|
|
|
1997-01-20 09:11:47 -08:00
|
|
|
and transl_fields env policy =
|
1996-04-22 04:15:41 -07:00
|
|
|
function
|
|
|
|
[] ->
|
|
|
|
newty Tnil
|
|
|
|
| {pfield_desc = Pfield_var} as field::_ ->
|
1997-01-20 09:11:47 -08:00
|
|
|
if policy = Fixed then
|
1996-04-22 04:15:41 -07:00
|
|
|
raise(Error(field.pfield_loc, Unbound_type_variable ".."));
|
|
|
|
newvar ()
|
|
|
|
| {pfield_desc = Pfield(s, e)}::l ->
|
1997-01-20 09:11:47 -08:00
|
|
|
let ty1 = transl_type env policy e in
|
|
|
|
let ty2 = transl_fields env policy l in
|
1997-05-11 14:48:21 -07:00
|
|
|
newty (Tfield (s, Fpresent, ty1, ty2))
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1997-01-20 09:11:47 -08:00
|
|
|
let transl_simple_type env fixed styp =
|
|
|
|
aliases := Tbl.empty;
|
|
|
|
let typ = transl_type env (if fixed then Fixed else Extensible) styp in
|
|
|
|
aliases := Tbl.empty;
|
|
|
|
typ
|
|
|
|
|
|
|
|
let transl_simple_type_delayed env styp =
|
|
|
|
aliases := Tbl.empty;
|
|
|
|
used_variables := Tbl.empty;
|
|
|
|
bindings := [];
|
|
|
|
let typ = transl_type env Delayed styp in
|
|
|
|
let b = !bindings in
|
|
|
|
aliases := Tbl.empty;
|
|
|
|
used_variables := Tbl.empty;
|
|
|
|
bindings := [];
|
|
|
|
(typ,
|
1997-02-20 12:39:02 -08:00
|
|
|
function () -> List.iter (function (t1, t2) -> unify env t1 t2) b)
|
1997-01-20 09:11:47 -08:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
let transl_type_scheme env styp =
|
|
|
|
reset_type_variables();
|
|
|
|
begin_def();
|
|
|
|
let typ = transl_simple_type env false styp in
|
|
|
|
end_def();
|
|
|
|
generalize typ;
|
|
|
|
typ
|
|
|
|
|
|
|
|
(* Error report *)
|
|
|
|
|
|
|
|
open Format
|
|
|
|
open Printtyp
|
|
|
|
|
|
|
|
let report_error = function
|
|
|
|
Unbound_type_variable name ->
|
|
|
|
print_string "Unbound type parameter "; print_string name
|
|
|
|
| Unbound_type_constructor lid ->
|
|
|
|
print_string "Unbound type constructor "; longident lid
|
|
|
|
| Type_arity_mismatch(lid, expected, provided) ->
|
1997-02-04 00:03:29 -08:00
|
|
|
open_box 0;
|
1995-05-04 03:15:53 -07:00
|
|
|
print_string "The type constructor "; longident lid;
|
|
|
|
print_space(); print_string "expects "; print_int expected;
|
|
|
|
print_string " argument(s),"; print_space();
|
|
|
|
print_string "but is here applied to "; print_int provided;
|
|
|
|
print_string " argument(s)";
|
|
|
|
close_box()
|
1996-04-22 04:15:41 -07:00
|
|
|
| Bound_type_variable name ->
|
|
|
|
print_string "Already bound type parameter "; print_string name
|
|
|
|
| Recursive_type ->
|
|
|
|
print_string "This type is recursive"
|
|
|
|
| Unbound_class lid ->
|
|
|
|
print_string "Unbound class "; longident lid
|
|
|
|
| Unbound_row_variable lid ->
|
|
|
|
print_string "Unbound row variable in #"; longident lid
|
1997-02-20 12:39:02 -08:00
|
|
|
| Type_mismatch trace ->
|
|
|
|
Printtyp.unification_error trace
|
|
|
|
(function () ->
|
|
|
|
print_string "This type parameter")
|
|
|
|
(function () ->
|
|
|
|
print_string "should be an instance of type")
|
|
|
|
| Alias_type_mismatch trace ->
|
|
|
|
Printtyp.unification_error trace
|
|
|
|
(function () ->
|
|
|
|
print_string "This alias is bound to type")
|
|
|
|
(function () ->
|
|
|
|
print_string "but is used as an instance of type")
|