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 *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
|
|
|
(* under the terms of the Q Public License version 1.0. *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $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
|
1999-11-30 08:07:38 -08:00
|
|
|
| Present_has_conjunction of string
|
|
|
|
| Present_has_no_type of string
|
|
|
|
| Multiple_constructor of string
|
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)
|
1998-06-24 12:22:26 -07:00
|
|
|
let saved_type_variables = ref ([] : (string, type_expr) Tbl.t list)
|
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)
|
1998-11-30 05:06:53 -08:00
|
|
|
let bindings = ref ([] : (Location.t * type_expr * type_expr) list)
|
1997-01-20 09:11:47 -08:00
|
|
|
(* 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 ();
|
1998-06-24 12:22:26 -07:00
|
|
|
type_variables := Tbl.empty;
|
|
|
|
saved_type_variables := []
|
|
|
|
|
|
|
|
let narrow () =
|
|
|
|
increase_global_level ();
|
|
|
|
saved_type_variables := !type_variables :: !saved_type_variables
|
|
|
|
|
|
|
|
let widen () =
|
|
|
|
restore_global_level ();
|
|
|
|
match !saved_type_variables with
|
|
|
|
tv :: rem -> type_variables := tv; saved_type_variables := rem
|
|
|
|
| [] -> assert false
|
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 ->
|
1998-08-31 12:41:24 -07:00
|
|
|
raise(Error(loc, Unbound_type_variable ("'" ^ name)))
|
1996-04-22 04:15:41 -07:00
|
|
|
|
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
|
1999-11-30 08:07:38 -08:00
|
|
|
Ptyp_any -> Ctype.newvar ()
|
1997-02-11 10:24:47 -08:00
|
|
|
| Ptyp_var name ->
|
1999-11-30 08:07:38 -08:00
|
|
|
begin
|
1997-01-20 09:11:47 -08:00
|
|
|
match policy with
|
|
|
|
Fixed ->
|
|
|
|
begin try
|
|
|
|
Tbl.find name !type_variables
|
|
|
|
with Not_found ->
|
1998-08-31 12:41:24 -07:00
|
|
|
raise(Error(styp.ptyp_loc, Unbound_type_variable ("'" ^ name)))
|
1997-01-20 09:11:47 -08:00
|
|
|
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;
|
1998-11-30 05:06:53 -08:00
|
|
|
bindings := (styp.ptyp_loc, v1, v2)::!bindings;
|
1997-01-20 09:11:47 -08:00
|
|
|
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
|
1999-11-30 08:07:38 -08:00
|
|
|
| Ptyp_arrow(l, 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
|
1999-11-30 08:07:38 -08:00
|
|
|
newty (Tarrow(l, 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
|
1998-06-24 12:22:26 -07:00
|
|
|
let params = List.map (fun _ -> Ctype.newvar ()) args in
|
|
|
|
let cstr = newty (Tconstr(path, params, ref Mnil)) in
|
1999-02-24 07:21:50 -08:00
|
|
|
begin try
|
1999-11-08 15:05:03 -08:00
|
|
|
Ctype.enforce_constraints env cstr
|
1999-02-24 07:21:50 -08:00
|
|
|
with Unify trace ->
|
|
|
|
raise (Error(styp.ptyp_loc, Type_mismatch trace))
|
|
|
|
end;
|
1997-02-20 12:39:02 -08:00
|
|
|
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)
|
1999-11-30 08:07:38 -08:00
|
|
|
| Ptyp_class(lid, stl, present) ->
|
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));
|
1999-11-30 08:07:38 -08:00
|
|
|
let (path, decl, is_variant) =
|
1996-04-22 04:15:41 -07:00
|
|
|
try
|
1999-11-30 08:07:38 -08:00
|
|
|
let (path, decl) = Env.lookup_type lid env in
|
|
|
|
match decl.type_manifest with
|
|
|
|
None -> raise Not_found
|
|
|
|
| Some ty ->
|
|
|
|
match (repr ty).desc with
|
|
|
|
Tvariant row when Btype.static_row row -> (path, decl, true)
|
|
|
|
| _ -> raise Not_found
|
|
|
|
with Not_found -> try
|
|
|
|
if present <> [] then raise Not_found;
|
|
|
|
let lid2 =
|
|
|
|
match lid with
|
|
|
|
Longident.Lident s -> Longident.Lident ("#" ^ s)
|
|
|
|
| Longident.Ldot(r, s) -> Longident.Ldot (r, "#" ^ s)
|
|
|
|
| Longident.Lapply(_, _) -> fatal_error "Typetexp.transl_type"
|
|
|
|
in
|
|
|
|
let (path, decl) = Env.lookup_type lid2 env in
|
|
|
|
(path, decl, false)
|
1996-04-22 04:15:41 -07:00
|
|
|
with Not_found ->
|
1999-11-30 08:07:38 -08:00
|
|
|
raise(Error(styp.ptyp_loc, Unbound_class lid))
|
|
|
|
in
|
1996-04-22 04:15:41 -07:00
|
|
|
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
|
1999-11-30 08:07:38 -08:00
|
|
|
let cstr = newty (Tconstr(path, args, ref Mnil)) in
|
1998-11-30 05:06:53 -08:00
|
|
|
let ty =
|
1999-11-30 08:07:38 -08:00
|
|
|
try Ctype.expand_head env cstr
|
1998-11-30 05:06:53 -08:00
|
|
|
with Unify trace ->
|
|
|
|
raise (Error(styp.ptyp_loc, Type_mismatch trace))
|
|
|
|
in
|
1997-02-20 12:39:02 -08:00
|
|
|
let params = Ctype.instance_list decl.type_params in
|
|
|
|
List.iter2
|
|
|
|
(fun (sty, ty') ty ->
|
1999-11-30 08:07:38 -08:00
|
|
|
try unify env ty' ty with Unify trace ->
|
1997-02-20 12:39:02 -08:00
|
|
|
raise (Error(sty.ptyp_loc, Type_mismatch trace)))
|
|
|
|
(List.combine stl args) params;
|
1999-11-30 08:07:38 -08:00
|
|
|
begin match ty.desc with
|
|
|
|
Tvariant row ->
|
|
|
|
let row = Btype.row_repr row in
|
|
|
|
List.iter
|
|
|
|
(fun l -> if not (List.mem_assoc l row.row_fields) then
|
|
|
|
raise(Error(styp.ptyp_loc, Present_has_no_type l)))
|
|
|
|
present;
|
|
|
|
let bound = ref row.row_bound in
|
|
|
|
let fields =
|
|
|
|
List.map
|
|
|
|
(fun (l,f) -> l,
|
|
|
|
if List.mem l present then f else
|
|
|
|
match Btype.row_field_repr f with
|
|
|
|
| Rpresent (Some ty) ->
|
|
|
|
bound := ty :: !bound;
|
|
|
|
Reither(false, [ty], ref None)
|
|
|
|
| Rpresent None ->
|
|
|
|
Reither (true, [], ref None)
|
|
|
|
| _ -> f)
|
|
|
|
row.row_fields
|
|
|
|
in
|
|
|
|
let row = { row with row_fields = fields; row_bound = !bound;
|
|
|
|
row_name = Some (path, args) } in
|
|
|
|
newty (Tvariant row)
|
|
|
|
| _ ->
|
|
|
|
ty
|
|
|
|
end
|
1997-03-07 14:26:29 -08:00
|
|
|
| Ptyp_alias(st, alias) ->
|
1999-11-30 08:07:38 -08:00
|
|
|
if Tbl.mem alias !type_variables then
|
1997-03-07 14:26:29 -08:00
|
|
|
raise(Error(styp.ptyp_loc, Bound_type_variable alias))
|
1998-11-05 00:07:15 -08:00
|
|
|
else
|
1999-11-30 08:07:38 -08:00
|
|
|
let ty' = new_global_var () in
|
|
|
|
type_variables := Tbl.add alias ty' !type_variables;
|
1997-03-07 14:26:29 -08:00
|
|
|
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
|
1999-11-30 08:07:38 -08:00
|
|
|
| Ptyp_variant(fields, closed, present) ->
|
|
|
|
let bound = ref [] in
|
|
|
|
ignore (List.fold_left
|
|
|
|
(fun (ll,hl) (l,_,_) ->
|
|
|
|
if List.mem l ll then
|
|
|
|
raise(Error(styp.ptyp_loc, Multiple_constructor l));
|
|
|
|
let h = Btype.hash_variant l in
|
|
|
|
if List.mem h hl then
|
|
|
|
raise(Ctype.Tags(l, List.assoc h (List.combine hl ll)));
|
|
|
|
(l::ll, h::hl))
|
|
|
|
([],[])
|
|
|
|
fields);
|
|
|
|
let fields =
|
|
|
|
List.map
|
|
|
|
(fun (l, c, stl) ->
|
|
|
|
l, if List.mem l present then begin
|
|
|
|
if List.length stl > 1 || c && stl <> [] then
|
|
|
|
raise(Error(styp.ptyp_loc, Present_has_conjunction l));
|
|
|
|
match stl with [] -> Rpresent None
|
|
|
|
| st::_ -> Rpresent(Some(transl_type env policy st))
|
|
|
|
end else begin
|
|
|
|
let tl = List.map (transl_type env policy) stl in
|
|
|
|
bound := tl @ !bound;
|
|
|
|
Reither(c, tl, ref None)
|
|
|
|
end)
|
|
|
|
fields
|
|
|
|
in
|
|
|
|
List.iter
|
|
|
|
(fun l -> if not (List.mem_assoc l fields) then
|
|
|
|
raise(Error(styp.ptyp_loc, Present_has_no_type l)))
|
|
|
|
present;
|
|
|
|
let row =
|
|
|
|
{ row_fields = fields; row_more = newvar ();
|
|
|
|
row_bound = !bound; row_closed = closed; row_name = None } in
|
|
|
|
if policy = Fixed && not (Btype.static_row row) then
|
|
|
|
raise(Error(styp.ptyp_loc, Unbound_type_variable "[..]"));
|
|
|
|
newty (Tvariant row)
|
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
|
1999-11-30 08:07:38 -08:00
|
|
|
raise(Error(field.pfield_loc, Unbound_type_variable "<..>"));
|
1996-04-22 04:15:41 -07:00
|
|
|
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 =
|
|
|
|
let typ = transl_type env (if fixed then Fixed else Extensible) styp in
|
|
|
|
typ
|
|
|
|
|
|
|
|
let transl_simple_type_delayed env styp =
|
|
|
|
used_variables := Tbl.empty;
|
|
|
|
bindings := [];
|
|
|
|
let typ = transl_type env Delayed styp in
|
|
|
|
let b = !bindings in
|
|
|
|
used_variables := Tbl.empty;
|
|
|
|
bindings := [];
|
|
|
|
(typ,
|
1998-11-30 05:06:53 -08:00
|
|
|
function () ->
|
|
|
|
List.iter
|
|
|
|
(function (loc, t1, t2) ->
|
|
|
|
try unify env t1 t2 with Unify trace ->
|
|
|
|
raise (Error(loc, Type_mismatch trace)))
|
|
|
|
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 *)
|
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
open Format
|
1995-05-04 03:15:53 -07:00
|
|
|
open Printtyp
|
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
let report_error ppf = function
|
|
|
|
| Unbound_type_variable name ->
|
|
|
|
fprintf ppf "Unbound type parameter %s" name
|
1995-05-04 03:15:53 -07:00
|
|
|
| Unbound_type_constructor lid ->
|
2000-03-06 14:12:09 -08:00
|
|
|
fprintf ppf "Unbound type constructor %a" longident lid
|
1995-05-04 03:15:53 -07:00
|
|
|
| Type_arity_mismatch(lid, expected, provided) ->
|
2000-03-06 14:12:09 -08:00
|
|
|
fprintf ppf
|
|
|
|
"@[The type constructor %a@ expects %i argument(s),@ \
|
|
|
|
but is here applied to %i argument(s)@]"
|
|
|
|
longident lid expected provided
|
1996-04-22 04:15:41 -07:00
|
|
|
| Bound_type_variable name ->
|
2000-03-06 14:12:09 -08:00
|
|
|
fprintf ppf "Already bound type parameter %s" name
|
1996-04-22 04:15:41 -07:00
|
|
|
| Recursive_type ->
|
2000-03-06 14:12:09 -08:00
|
|
|
fprintf ppf "This type is recursive"
|
1996-04-22 04:15:41 -07:00
|
|
|
| Unbound_class lid ->
|
2000-03-06 14:12:09 -08:00
|
|
|
fprintf ppf "Unbound class %a" longident lid
|
1996-04-22 04:15:41 -07:00
|
|
|
| Unbound_row_variable lid ->
|
2000-03-06 14:12:09 -08:00
|
|
|
fprintf ppf "Unbound row variable in #%a" longident lid
|
1997-02-20 12:39:02 -08:00
|
|
|
| Type_mismatch trace ->
|
1998-08-15 06:49:41 -07:00
|
|
|
Printtyp.unification_error true trace
|
2000-03-06 14:12:09 -08:00
|
|
|
(function ppf ->
|
|
|
|
fprintf ppf "This type")
|
|
|
|
ppf
|
|
|
|
(function ppf ->
|
|
|
|
fprintf ppf "should be an instance of type")
|
1997-02-20 12:39:02 -08:00
|
|
|
| Alias_type_mismatch trace ->
|
1998-08-15 06:49:41 -07:00
|
|
|
Printtyp.unification_error true trace
|
2000-03-06 14:12:09 -08:00
|
|
|
(function ppf ->
|
|
|
|
fprintf ppf "This alias is bound to type")
|
|
|
|
ppf
|
|
|
|
(function ppf ->
|
|
|
|
fprintf ppf "but is used as an instance of type")
|
1999-11-30 08:07:38 -08:00
|
|
|
| Present_has_conjunction l ->
|
2000-03-06 14:12:09 -08:00
|
|
|
fprintf ppf "The present constructor %s has a conjunctive type" l
|
1999-11-30 08:07:38 -08:00
|
|
|
| Present_has_no_type l ->
|
2000-03-06 14:12:09 -08:00
|
|
|
fprintf ppf "The present constructor %s has no type" l
|
1999-11-30 08:07:38 -08:00
|
|
|
| Multiple_constructor l ->
|
2000-03-06 14:12:09 -08:00
|
|
|
fprintf ppf "The variant constructor %s is multiply defined" l
|