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
|
|
|
(* *)
|
1996-04-29 06:24:25 -07:00
|
|
|
(* Xavier Leroy and Jerome Vouillon, projet Cristal, INRIA Rocquencourt*)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
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
|
|
|
(* Operations on core types *)
|
|
|
|
|
|
|
|
open Misc
|
1996-04-22 04:15:41 -07:00
|
|
|
open Asttypes
|
1996-09-23 04:33:27 -07:00
|
|
|
open Types
|
1997-03-24 12:12:00 -08:00
|
|
|
open Btype
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1997-03-13 13:18:06 -08:00
|
|
|
(*
|
|
|
|
Type manipulation after type inference
|
|
|
|
======================================
|
1997-03-14 07:19:48 -08:00
|
|
|
If one wants to manipulate a type after type inference (for
|
1997-03-13 13:18:06 -08:00
|
|
|
instance, during code generation or in the debugger), one must
|
|
|
|
first make sure that the type levels are correct, using the
|
1997-03-14 07:19:48 -08:00
|
|
|
function [correct_levels]. Then, this type can be correctely
|
1997-06-29 06:16:47 -07:00
|
|
|
manipulated by [apply], [expand_head] and [moregeneral].
|
1997-03-13 13:18:06 -08:00
|
|
|
*)
|
|
|
|
|
1997-02-20 12:39:02 -08:00
|
|
|
(*
|
|
|
|
General notes
|
|
|
|
=============
|
1997-01-20 09:11:47 -08:00
|
|
|
- As much sharing as possible should be kept : it makes types
|
|
|
|
smaller and better abbreviated.
|
|
|
|
When necessary, some sharing can be lost. Types will still be
|
1997-03-18 13:05:15 -08:00
|
|
|
printed correctly (+++ TO DO...), and abbreviations defined by a
|
|
|
|
class do not depend on sharing thanks to constrained
|
|
|
|
abbreviations. (Of course, even if some sharing is lost, typing
|
|
|
|
will still be correct.)
|
1997-02-20 12:39:02 -08:00
|
|
|
- All nodes of a type have a level : that way, one know whether a
|
|
|
|
node need to be duplicated or not when instantiating a type.
|
1997-03-07 14:44:02 -08:00
|
|
|
- Levels of a type are decreasing (generic level being considered
|
|
|
|
as greatest).
|
1997-02-20 12:39:02 -08:00
|
|
|
- The level of a type constructor is superior to the binding
|
1997-01-20 09:11:47 -08:00
|
|
|
time of its path.
|
1997-03-08 14:05:39 -08:00
|
|
|
- Recursive types without limitation should be handled (even if
|
1997-03-18 13:05:15 -08:00
|
|
|
there is still an occur check). This avoid treating specially the
|
|
|
|
case for objects, for instance. Furthermore, the occur check
|
|
|
|
policy can then be easily changed.
|
1997-01-20 09:11:47 -08:00
|
|
|
*)
|
|
|
|
|
1997-02-20 12:39:02 -08:00
|
|
|
(*
|
|
|
|
A faire
|
|
|
|
=======
|
1997-01-20 09:11:47 -08:00
|
|
|
- Revoir affichage des types.
|
1997-02-20 12:39:02 -08:00
|
|
|
- Etendre la portee d'un alias [... as 'a] a tout le type englobant.
|
1997-01-20 09:11:47 -08:00
|
|
|
- #-type implementes comme de vraies abreviations.
|
1997-02-20 12:39:02 -08:00
|
|
|
- Niveaux plus fins pour les identificateurs :
|
|
|
|
Champ [global] renomme en [level];
|
|
|
|
Niveau -1 : global
|
|
|
|
0 : module toplevel
|
|
|
|
1 : module contenu dans module toplevel
|
|
|
|
...
|
|
|
|
En fait, incrementer le niveau a chaque fois que l'on rentre dans
|
|
|
|
un module.
|
|
|
|
|
|
|
|
3 4 6
|
|
|
|
\ / /
|
|
|
|
1 2 5
|
|
|
|
\|/
|
|
|
|
0
|
|
|
|
|
|
|
|
[Subst] doit ecreter les niveaux (pour qu'un variable non
|
|
|
|
generalisable dans un module de niveau 2 ne se retrouve pas
|
|
|
|
generalisable lorsque l'on l'utilise au niveau 0).
|
|
|
|
|
|
|
|
- Traitement de la trace de l'unification separe de la fonction
|
|
|
|
[unify].
|
1997-01-20 09:11:47 -08:00
|
|
|
*)
|
|
|
|
|
1997-01-21 05:38:42 -08:00
|
|
|
(**** Errors ****)
|
|
|
|
|
|
|
|
exception Unify of (type_expr * type_expr) list
|
|
|
|
|
1999-11-30 08:07:38 -08:00
|
|
|
exception Tags of label * label
|
|
|
|
|
1996-05-26 06:42:34 -07:00
|
|
|
exception Subtype of
|
|
|
|
(type_expr * type_expr) list * (type_expr * type_expr) list
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1997-01-23 04:46:46 -08:00
|
|
|
exception Cannot_expand
|
|
|
|
|
1997-03-18 13:05:15 -08:00
|
|
|
exception Cannot_apply
|
|
|
|
|
1997-03-08 14:05:39 -08:00
|
|
|
exception Recursive_abbrev
|
|
|
|
|
1997-01-21 05:38:42 -08:00
|
|
|
(**** Type level management ****)
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
let current_level = ref 0
|
1998-06-24 12:22:26 -07:00
|
|
|
let nongen_level = ref 0
|
1996-04-22 04:15:41 -07:00
|
|
|
let global_level = ref 1
|
1998-06-24 12:22:26 -07:00
|
|
|
let saved_level = ref []
|
|
|
|
let saved_global_level = ref []
|
|
|
|
|
|
|
|
let init_def level = current_level := level; nongen_level := level
|
|
|
|
let begin_def () =
|
|
|
|
saved_level := (!current_level, !nongen_level) :: !saved_level;
|
|
|
|
incr current_level; nongen_level := !current_level
|
|
|
|
let begin_class_def () =
|
|
|
|
saved_level := (!current_level, !nongen_level) :: !saved_level;
|
|
|
|
incr current_level
|
|
|
|
let raise_nongen_level () =
|
|
|
|
saved_level := (!current_level, !nongen_level) :: !saved_level;
|
|
|
|
nongen_level := !current_level
|
|
|
|
let end_def () =
|
|
|
|
let (cl, nl) = List.hd !saved_level in
|
|
|
|
saved_level := List.tl !saved_level;
|
|
|
|
current_level := cl; nongen_level := nl
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1996-04-22 04:15:41 -07:00
|
|
|
let reset_global_level () =
|
1998-06-24 12:22:26 -07:00
|
|
|
global_level := !current_level + 1;
|
|
|
|
saved_global_level := []
|
|
|
|
let increase_global_level () =
|
|
|
|
saved_global_level := !global_level :: !saved_global_level;
|
|
|
|
global_level := !current_level
|
|
|
|
let restore_global_level () =
|
|
|
|
match !saved_global_level with
|
|
|
|
gl::rem -> global_level := gl; saved_global_level := rem
|
|
|
|
| [] -> assert false
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1997-01-21 05:38:42 -08:00
|
|
|
(**** Some type creators ****)
|
|
|
|
|
1997-03-24 12:12:00 -08:00
|
|
|
(* Re-export generic type creators *)
|
|
|
|
|
1998-07-03 10:40:39 -07:00
|
|
|
let newty2 = Btype.newty2
|
|
|
|
let newty desc = newty2 !current_level desc
|
|
|
|
let new_global_ty desc = newty2 !global_level desc
|
|
|
|
|
|
|
|
let newvar () = newty2 !current_level Tvar
|
|
|
|
let newvar2 level = newty2 level Tvar
|
|
|
|
let new_global_var () = newty2 !global_level Tvar
|
1997-01-21 05:38:42 -08:00
|
|
|
|
1997-03-08 14:05:39 -08:00
|
|
|
let newobj fields = newty (Tobject (fields, ref None))
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
let newconstr path tyl = newty (Tconstr (path, tyl, ref Mnil))
|
|
|
|
|
1997-01-21 05:38:42 -08:00
|
|
|
let none = newty (Ttuple []) (* Clearly ill-formed type *)
|
|
|
|
|
|
|
|
(**** Representative of a type ****)
|
|
|
|
|
1997-03-24 12:12:00 -08:00
|
|
|
(* Re-export repr *)
|
|
|
|
let repr = repr
|
1997-03-09 08:52:49 -08:00
|
|
|
|
1998-07-03 10:40:39 -07:00
|
|
|
(**** Type maps ****)
|
|
|
|
|
|
|
|
module TypePairs =
|
|
|
|
Hashtbl.Make (struct
|
|
|
|
type t = type_expr * type_expr
|
1998-07-31 00:54:17 -07:00
|
|
|
let equal (t1, t1') (t2, t2') = (t1 == t2) && (t1' == t2')
|
1998-07-03 10:40:39 -07:00
|
|
|
let hash (t, t') = t.id + 93 * t'.id
|
|
|
|
end)
|
|
|
|
|
1997-01-21 05:38:42 -08:00
|
|
|
(**********************************************)
|
|
|
|
(* Miscellaneous operations on object types *)
|
|
|
|
(**********************************************)
|
|
|
|
|
1996-04-22 04:15:41 -07:00
|
|
|
|
1997-01-20 09:11:47 -08:00
|
|
|
(**** Object field manipulation. ****)
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
let object_fields ty =
|
|
|
|
match (repr ty).desc with
|
|
|
|
Tobject (fields, _) -> fields
|
|
|
|
| _ -> assert false
|
|
|
|
|
1996-04-22 04:15:41 -07:00
|
|
|
let flatten_fields ty =
|
|
|
|
let rec flatten l ty =
|
|
|
|
let ty = repr ty in
|
|
|
|
match ty.desc with
|
1998-06-24 12:22:26 -07:00
|
|
|
Tfield(s, k, ty1, ty2) ->
|
|
|
|
flatten ((s, k, ty1)::l) ty2
|
1996-04-22 04:15:41 -07:00
|
|
|
| _ ->
|
1997-03-07 14:44:02 -08:00
|
|
|
(l, ty)
|
1996-04-22 04:15:41 -07:00
|
|
|
in
|
|
|
|
let (l, r) = flatten [] ty in
|
1998-06-24 12:22:26 -07:00
|
|
|
(Sort.list (fun (n, _, _) (n', _, _) -> n < n') l, r)
|
1996-04-22 04:15:41 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
let build_fields level =
|
|
|
|
List.fold_right
|
|
|
|
(fun (s, k, ty1) ty2 -> newty2 level (Tfield(s, k, ty1, ty2)))
|
1996-04-22 04:15:41 -07:00
|
|
|
|
|
|
|
let associate_fields fields1 fields2 =
|
|
|
|
let rec associate p s s' =
|
|
|
|
function
|
|
|
|
(l, []) ->
|
|
|
|
(List.rev p, (List.rev s) @ l, List.rev s')
|
|
|
|
| ([], l') ->
|
|
|
|
(List.rev p, List.rev s, (List.rev s') @ l')
|
1997-05-11 14:35:00 -07:00
|
|
|
| ((n, k, t)::r, (n', k', t')::r') when n = n' ->
|
1998-06-24 12:22:26 -07:00
|
|
|
associate ((n, k, t, k', t')::p) s s' (r, r')
|
1997-05-11 14:35:00 -07:00
|
|
|
| ((n, k, t)::r, ((n', k', t')::_ as l')) when n < n' ->
|
|
|
|
associate p ((n, k, t)::s) s' (r, l')
|
|
|
|
| (((n, k, t)::r as l), (n', k', t')::r') (* when n > n' *) ->
|
|
|
|
associate p s ((n', k', t')::s') (l, r')
|
1998-06-24 12:22:26 -07:00
|
|
|
in
|
|
|
|
associate [] [] [] (fields1, fields2)
|
1996-04-22 04:15:41 -07:00
|
|
|
|
1997-01-20 09:11:47 -08:00
|
|
|
(**** Check whether an object is open ****)
|
1996-04-22 04:15:41 -07:00
|
|
|
|
1997-03-08 14:05:39 -08:00
|
|
|
(* +++ Il faudra penser a eventuellement expanser l'abreviation *)
|
1997-01-20 09:11:47 -08:00
|
|
|
let rec opened_object ty =
|
1996-04-22 04:15:41 -07:00
|
|
|
match (repr ty).desc with
|
1997-05-11 14:35:00 -07:00
|
|
|
Tobject (t, _) -> opened_object t
|
|
|
|
| Tfield(_, _, _, t) -> opened_object t
|
|
|
|
| Tvar -> true
|
|
|
|
| _ -> false
|
1996-04-22 04:15:41 -07:00
|
|
|
|
1997-01-21 05:38:42 -08:00
|
|
|
(**** Close an object ****)
|
|
|
|
|
|
|
|
let close_object ty =
|
|
|
|
let rec close ty =
|
|
|
|
let ty = repr ty in
|
|
|
|
match ty.desc with
|
1998-07-03 10:40:39 -07:00
|
|
|
Tvar -> ty.desc <- Tlink (newty2 ty.level Tnil)
|
1998-06-24 12:22:26 -07:00
|
|
|
| Tfield(_, _, _, ty') -> close ty'
|
|
|
|
| _ -> assert false
|
1997-01-21 05:38:42 -08:00
|
|
|
in
|
|
|
|
match (repr ty).desc with
|
|
|
|
Tobject (ty, _) -> close ty
|
1998-06-24 12:22:26 -07:00
|
|
|
| _ -> assert false
|
|
|
|
|
|
|
|
(**** Row variable of an object type ****)
|
1997-01-21 05:38:42 -08:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
let row_variable ty =
|
|
|
|
let rec find ty =
|
|
|
|
let ty = repr ty in
|
|
|
|
match ty.desc with
|
|
|
|
Tfield (_, _, _, ty) -> find ty
|
|
|
|
| Tvar -> ty
|
|
|
|
| _ -> assert false
|
|
|
|
in
|
|
|
|
match (repr ty).desc with
|
|
|
|
Tobject (fi, _) -> find fi
|
|
|
|
| _ -> assert false
|
1997-01-21 05:38:42 -08:00
|
|
|
|
|
|
|
(**** Object name manipulation ****)
|
1997-03-08 14:05:39 -08:00
|
|
|
(* +++ Bientot obsolete *)
|
1997-01-21 05:38:42 -08:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
let set_object_name id rv params ty =
|
1997-01-21 05:38:42 -08:00
|
|
|
match (repr ty).desc with
|
|
|
|
Tobject (fi, nm) ->
|
|
|
|
begin try
|
1998-06-24 12:22:26 -07:00
|
|
|
nm := Some (Path.Pident id, rv::params)
|
1997-01-21 05:38:42 -08:00
|
|
|
with Not_found ->
|
|
|
|
()
|
|
|
|
end
|
|
|
|
| _ ->
|
1998-06-24 12:22:26 -07:00
|
|
|
assert false
|
1997-01-21 05:38:42 -08:00
|
|
|
|
|
|
|
let remove_object_name ty =
|
|
|
|
match (repr ty).desc with
|
|
|
|
Tobject (_, nm) -> nm := None
|
|
|
|
| Tconstr (_, _, _) -> ()
|
|
|
|
| _ -> fatal_error "Ctype.remove_object_name"
|
|
|
|
|
1997-05-11 14:35:00 -07:00
|
|
|
(**** Hiding of private methods ****)
|
|
|
|
|
|
|
|
let hide_private_methods ty =
|
1998-06-24 12:22:26 -07:00
|
|
|
let (fl, _) = flatten_fields (object_fields ty) in
|
|
|
|
List.iter
|
|
|
|
(function (_, k, _) ->
|
|
|
|
let k = field_kind_repr k in
|
|
|
|
match k with
|
|
|
|
Fvar r -> r := Some Fabsent
|
|
|
|
| _ -> ())
|
|
|
|
fl
|
|
|
|
|
|
|
|
|
|
|
|
(*******************************)
|
|
|
|
(* Operations on class types *)
|
|
|
|
(*******************************)
|
|
|
|
|
|
|
|
|
|
|
|
let rec signature_of_class_type =
|
|
|
|
function
|
|
|
|
Tcty_constr (_, _, cty) -> signature_of_class_type cty
|
|
|
|
| Tcty_signature sign -> sign
|
1999-11-30 08:07:38 -08:00
|
|
|
| Tcty_fun (_, ty, cty) -> signature_of_class_type cty
|
1998-06-24 12:22:26 -07:00
|
|
|
|
|
|
|
let self_type cty =
|
|
|
|
repr (signature_of_class_type cty).cty_self
|
|
|
|
|
|
|
|
let rec class_type_arity =
|
|
|
|
function
|
|
|
|
Tcty_constr (_, _, cty) -> class_type_arity cty
|
|
|
|
| Tcty_signature _ -> 0
|
1999-11-30 08:07:38 -08:00
|
|
|
| Tcty_fun (_, _, cty) -> 1 + class_type_arity cty
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
|
1999-11-30 08:07:38 -08:00
|
|
|
(*******************************************)
|
|
|
|
(* Miscellaneous operations on row types *)
|
|
|
|
(*******************************************)
|
|
|
|
|
|
|
|
let sort_row_fields = Sort.list (fun (p,_) (q,_) -> p < q)
|
|
|
|
|
|
|
|
let merge_row_fields fi1 fi2 =
|
|
|
|
let rec merge r1 r2 pairs fi1 fi2 =
|
|
|
|
match fi1, fi2 with
|
|
|
|
(l1,f1 as p1)::fi1', (l2,f2 as p2)::fi2' ->
|
|
|
|
if l1 = l2 then merge r1 r2 ((l1,f1,f2)::pairs) fi1' fi2' else
|
|
|
|
if l1 < l2 then merge (p1::r1) r2 pairs fi1' fi2 else
|
|
|
|
merge r1 (p2::r2) pairs fi1 fi2'
|
|
|
|
| [], _ -> (List.rev r1, List.rev_append r2 fi2, pairs)
|
|
|
|
| _, [] -> (List.rev_append r1 fi1, List.rev r2, pairs)
|
|
|
|
in
|
|
|
|
merge [] [] [] (sort_row_fields fi1) (sort_row_fields fi2)
|
|
|
|
|
|
|
|
let rec filter_row_fields erase = function
|
|
|
|
[] -> []
|
|
|
|
| (l,f as p)::fi ->
|
|
|
|
let fi = filter_row_fields erase fi in
|
|
|
|
match row_field_repr f with
|
|
|
|
Rabsent -> fi
|
|
|
|
| Reither(_,_,e) when erase -> e := Some Rabsent; fi
|
|
|
|
| _ -> p :: fi
|
1998-06-24 12:22:26 -07:00
|
|
|
|
|
|
|
(**************************************)
|
|
|
|
(* Check genericity of type schemes *)
|
|
|
|
(**************************************)
|
|
|
|
|
|
|
|
|
|
|
|
exception Non_closed
|
|
|
|
|
|
|
|
let rec closed_schema_rec ty =
|
1997-05-11 14:35:00 -07:00
|
|
|
let ty = repr ty in
|
1998-06-24 12:22:26 -07:00
|
|
|
if ty.level >= lowest_level then begin
|
|
|
|
let level = ty.level in
|
|
|
|
ty.level <- pivot_level - level;
|
|
|
|
match ty.desc with
|
|
|
|
Tvar when level <> generic_level ->
|
|
|
|
raise Non_closed
|
|
|
|
| Tfield(_, kind, t1, t2) ->
|
|
|
|
if field_kind_repr kind = Fpresent then
|
|
|
|
closed_schema_rec t1;
|
|
|
|
closed_schema_rec t2
|
1999-11-30 08:07:38 -08:00
|
|
|
| Tvariant row when static_row row ->
|
|
|
|
iter_row closed_schema_rec row
|
1998-06-24 12:22:26 -07:00
|
|
|
| _ ->
|
|
|
|
iter_type_expr closed_schema_rec ty
|
|
|
|
end
|
|
|
|
|
|
|
|
(* Return whether all variables of type [ty] are generic. *)
|
|
|
|
let closed_schema ty =
|
|
|
|
try
|
|
|
|
closed_schema_rec ty;
|
|
|
|
unmark_type ty;
|
|
|
|
true
|
|
|
|
with Non_closed ->
|
|
|
|
unmark_type ty;
|
|
|
|
false
|
|
|
|
|
1998-11-30 05:06:53 -08:00
|
|
|
exception Non_closed of type_expr * bool
|
1998-06-24 12:22:26 -07:00
|
|
|
|
|
|
|
let free_variables = ref []
|
|
|
|
|
1998-11-30 05:06:53 -08:00
|
|
|
let rec free_vars_rec real ty =
|
1998-06-24 12:22:26 -07:00
|
|
|
let ty = repr ty in
|
|
|
|
if ty.level >= lowest_level then begin
|
1998-11-30 05:06:53 -08:00
|
|
|
ty.level <- pivot_level - ty.level;
|
1998-06-24 12:22:26 -07:00
|
|
|
begin match ty.desc with
|
1998-11-30 05:06:53 -08:00
|
|
|
Tvar ->
|
|
|
|
free_variables := (ty, real) :: !free_variables
|
|
|
|
| Tobject(ty, {contents = Some (_, p)}) ->
|
|
|
|
free_vars_rec false ty; List.iter (free_vars_rec true) p
|
|
|
|
| Tobject (ty, _) ->
|
|
|
|
free_vars_rec false ty
|
|
|
|
| Tfield (_, _, ty1, ty2) ->
|
|
|
|
free_vars_rec true ty1; free_vars_rec false ty2
|
1999-11-30 08:07:38 -08:00
|
|
|
| Tvariant row when static_row row ->
|
|
|
|
iter_row (free_vars_rec true) row
|
1998-11-30 05:06:53 -08:00
|
|
|
| _ ->
|
|
|
|
iter_type_expr (free_vars_rec true) ty
|
1998-06-24 12:22:26 -07:00
|
|
|
end;
|
|
|
|
end
|
|
|
|
|
|
|
|
let free_vars ty =
|
|
|
|
free_variables := [];
|
1998-11-30 05:06:53 -08:00
|
|
|
free_vars_rec true ty;
|
1998-06-24 12:22:26 -07:00
|
|
|
let res = !free_variables in
|
|
|
|
free_variables := [];
|
|
|
|
res
|
|
|
|
|
|
|
|
let rec closed_type ty =
|
|
|
|
match free_vars ty with
|
1998-11-30 05:06:53 -08:00
|
|
|
[] -> ()
|
|
|
|
| (v, real) :: _ -> raise (Non_closed (v, real))
|
1998-06-24 12:22:26 -07:00
|
|
|
|
|
|
|
let closed_parameterized_type params ty =
|
|
|
|
List.iter mark_type params;
|
|
|
|
try
|
|
|
|
closed_type ty;
|
|
|
|
List.iter unmark_type params;
|
|
|
|
unmark_type ty;
|
|
|
|
true
|
|
|
|
with Non_closed _ ->
|
|
|
|
List.iter unmark_type params;
|
|
|
|
unmark_type ty;
|
|
|
|
false
|
|
|
|
|
|
|
|
let closed_type_decl decl =
|
|
|
|
try
|
|
|
|
List.iter mark_type decl.type_params;
|
|
|
|
begin match decl.type_kind with
|
|
|
|
Type_abstract ->
|
|
|
|
()
|
|
|
|
| Type_variant v ->
|
|
|
|
List.iter (fun (_, tyl) -> List.iter closed_type tyl) v
|
|
|
|
| Type_record r ->
|
|
|
|
List.iter (fun (_, _, ty) -> closed_type ty) r
|
|
|
|
end;
|
|
|
|
begin match decl.type_manifest with
|
|
|
|
None -> ()
|
|
|
|
| Some ty -> closed_type ty
|
|
|
|
end;
|
|
|
|
unmark_type_decl decl;
|
|
|
|
None
|
1998-11-30 05:06:53 -08:00
|
|
|
with Non_closed (ty, _) ->
|
1998-06-24 12:22:26 -07:00
|
|
|
unmark_type_decl decl;
|
|
|
|
Some ty
|
|
|
|
|
|
|
|
type closed_class_failure =
|
1998-11-30 05:06:53 -08:00
|
|
|
CC_Method of type_expr * bool * string * type_expr
|
|
|
|
| CC_Value of type_expr * bool * string * type_expr
|
1998-06-24 12:22:26 -07:00
|
|
|
|
|
|
|
exception Failure of closed_class_failure
|
|
|
|
|
|
|
|
let closed_class params sign =
|
|
|
|
let ty = object_fields (repr sign.cty_self) in
|
|
|
|
let (fields, rest) = flatten_fields ty in
|
|
|
|
List.iter mark_type params;
|
|
|
|
mark_type rest;
|
|
|
|
List.iter
|
|
|
|
(fun (lab, _, ty) -> if lab = "*dummy method*" then mark_type ty)
|
|
|
|
fields;
|
|
|
|
try
|
1998-10-10 10:57:27 -07:00
|
|
|
mark_type_node (repr sign.cty_self);
|
1998-08-15 22:36:15 -07:00
|
|
|
List.iter
|
|
|
|
(fun (lab, kind, ty) ->
|
|
|
|
if field_kind_repr kind = Fpresent then
|
1998-11-30 05:06:53 -08:00
|
|
|
try closed_type ty with Non_closed (ty0, real) ->
|
|
|
|
raise (Failure (CC_Method (ty0, real, lab, ty))))
|
1998-08-15 22:36:15 -07:00
|
|
|
fields;
|
1998-10-10 10:57:27 -07:00
|
|
|
mark_type_params (repr sign.cty_self);
|
1998-06-24 12:22:26 -07:00
|
|
|
List.iter unmark_type params;
|
|
|
|
unmark_class_signature sign;
|
|
|
|
None
|
|
|
|
with Failure reason ->
|
1998-11-30 05:06:53 -08:00
|
|
|
mark_type_params (repr sign.cty_self);
|
1998-06-24 12:22:26 -07:00
|
|
|
List.iter unmark_type params;
|
|
|
|
unmark_class_signature sign;
|
|
|
|
Some reason
|
|
|
|
|
|
|
|
|
|
|
|
(**********************)
|
|
|
|
(* Type duplication *)
|
|
|
|
(**********************)
|
|
|
|
|
|
|
|
|
|
|
|
(* Duplicate a type, preserving only type variables *)
|
|
|
|
let duplicate_type ty =
|
|
|
|
Subst.type_expr Subst.identity ty
|
|
|
|
|
|
|
|
(* Same, for class types *)
|
|
|
|
let duplicate_class_type ty =
|
|
|
|
Subst.class_type Subst.identity ty
|
1997-05-11 14:35:00 -07:00
|
|
|
|
1997-01-23 04:46:46 -08:00
|
|
|
|
|
|
|
(*****************************)
|
|
|
|
(* Type level manipulation *)
|
|
|
|
(*****************************)
|
|
|
|
|
1997-02-20 12:39:02 -08:00
|
|
|
(*
|
|
|
|
It would be a bit more efficient to remove abbreviation expansions
|
|
|
|
rather than generalizing them: these expansions will usually not be
|
|
|
|
used anymore. However, this is not possible in the general case, as
|
|
|
|
[expand_abbrev] (via [subst]) requires these expansions to be
|
|
|
|
preserved. Does it worth duplicating this code ?
|
|
|
|
*)
|
1998-06-24 12:22:26 -07:00
|
|
|
let rec iter_generalize tyl ty =
|
1997-01-24 12:54:56 -08:00
|
|
|
let ty = repr ty in
|
1997-03-24 12:12:00 -08:00
|
|
|
if (ty.level > !current_level) && (ty.level <> generic_level) then begin
|
1997-01-24 12:54:56 -08:00
|
|
|
ty.level <- generic_level;
|
|
|
|
begin match ty.desc with
|
|
|
|
Tconstr (_, _, abbrev) ->
|
1998-06-24 12:22:26 -07:00
|
|
|
generalize_expans tyl !abbrev
|
1999-11-30 08:07:38 -08:00
|
|
|
| Tvariant row
|
|
|
|
when (repr row.row_more).level > !current_level || static_row row ->
|
|
|
|
let row = row_repr row in
|
|
|
|
let bound =
|
|
|
|
List.fold_left
|
|
|
|
(fun acc (_,f) ->
|
|
|
|
match row_field_repr f with Reither(_,l,_) -> l@acc | _ -> acc)
|
|
|
|
[] row.row_fields in
|
|
|
|
let row = {row with row_bound = bound} in
|
|
|
|
ty.desc <- Tvariant row;
|
1997-01-24 12:54:56 -08:00
|
|
|
| _ -> ()
|
|
|
|
end;
|
1998-06-24 12:22:26 -07:00
|
|
|
iter_type_expr (iter_generalize tyl) ty
|
|
|
|
end else
|
|
|
|
tyl := ty :: !tyl
|
1997-01-24 12:54:56 -08:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
and generalize_expans tyl =
|
1997-01-24 12:54:56 -08:00
|
|
|
function
|
1998-06-24 12:22:26 -07:00
|
|
|
Mnil -> ()
|
|
|
|
| Mcons(_, ty, ty', rem) -> iter_generalize tyl ty;
|
|
|
|
iter_generalize tyl ty';
|
|
|
|
generalize_expans tyl rem
|
|
|
|
| Mlink rem -> generalize_expans tyl !rem
|
|
|
|
|
|
|
|
let rec generalize ty =
|
|
|
|
iter_generalize (ref []) ty
|
|
|
|
|
|
|
|
(* Efficient repeated generalisation of the same type *)
|
|
|
|
let iterative_generalization min_level tyl =
|
|
|
|
let tyl' = ref [] in
|
|
|
|
List.iter (iter_generalize tyl') tyl;
|
|
|
|
List.fold_right (fun ty l -> if ty.level <= min_level then l else ty::l)
|
|
|
|
!tyl' []
|
1997-01-24 12:54:56 -08:00
|
|
|
|
1997-06-29 06:16:47 -07:00
|
|
|
let try_expand_head' = (* Forward declaration *)
|
|
|
|
ref (fun env ty -> raise Cannot_expand)
|
1997-01-23 04:46:46 -08:00
|
|
|
|
1997-02-20 12:39:02 -08:00
|
|
|
(*
|
1997-03-07 14:44:02 -08:00
|
|
|
Lower the levels of a type (assume [level] is not
|
|
|
|
[generic_level]).
|
|
|
|
*)
|
|
|
|
(*
|
|
|
|
The level of a type constructor must be greater than its binding
|
1997-02-20 12:39:02 -08:00
|
|
|
time. That way, a type constructor cannot escape the scope of its
|
|
|
|
definition, as would be the case in
|
|
|
|
let x = ref []
|
|
|
|
module M = struct type t let _ = (x : t list ref) end
|
1997-03-18 13:05:15 -08:00
|
|
|
(without this constraint, the type system would actually be unsound.)
|
1997-02-20 12:39:02 -08:00
|
|
|
*)
|
1997-01-23 04:46:46 -08:00
|
|
|
let rec update_level env level ty =
|
1996-04-22 04:15:41 -07:00
|
|
|
let ty = repr ty in
|
1997-01-21 05:38:42 -08:00
|
|
|
if ty.level > level then begin
|
|
|
|
begin match ty.desc with
|
1997-01-23 04:46:46 -08:00
|
|
|
Tconstr(p, tl, abbrev) when level < Path.binding_time p ->
|
1997-01-24 12:54:56 -08:00
|
|
|
(* Try first to replace an abbreviation by its expansion. *)
|
1997-01-23 04:46:46 -08:00
|
|
|
begin try
|
1997-06-29 06:16:47 -07:00
|
|
|
ty.desc <- Tlink (!try_expand_head' env ty);
|
1997-03-07 14:44:02 -08:00
|
|
|
update_level env level ty
|
1997-01-23 04:46:46 -08:00
|
|
|
with Cannot_expand ->
|
1997-03-09 16:19:08 -08:00
|
|
|
(* +++ Levels should be restored... *)
|
1998-06-24 12:22:26 -07:00
|
|
|
raise (Unify [(ty, newvar2 level)])
|
1997-01-23 04:46:46 -08:00
|
|
|
end
|
1998-06-24 12:22:26 -07:00
|
|
|
| Tfield(_, k, _, _) ->
|
|
|
|
begin match field_kind_repr k with
|
|
|
|
Fvar _ (* {contents = None} *) -> raise (Unify [(ty, newvar2 level)])
|
|
|
|
| _ -> ()
|
|
|
|
end;
|
|
|
|
ty.level <- level;
|
|
|
|
iter_type_expr (update_level env level) ty
|
1997-01-24 12:54:56 -08:00
|
|
|
| _ ->
|
1997-06-29 06:16:47 -07:00
|
|
|
ty.level <- level;
|
1997-01-24 12:54:56 -08:00
|
|
|
iter_type_expr (update_level env level) ty
|
1997-03-08 04:14:57 -08:00
|
|
|
end
|
1996-04-22 04:15:41 -07:00
|
|
|
end
|
|
|
|
|
1997-01-24 12:54:56 -08:00
|
|
|
(*
|
|
|
|
Function [update_level] will never try to expand an abbreviation in
|
1997-03-07 14:44:02 -08:00
|
|
|
this case ([current_level] is greater than the binding time of any
|
1997-01-24 12:54:56 -08:00
|
|
|
type constructor path). So, it can be called with the empty
|
|
|
|
environnement.
|
|
|
|
*)
|
1998-06-24 12:22:26 -07:00
|
|
|
let make_nongen ty =
|
|
|
|
try
|
|
|
|
update_level Env.empty !nongen_level ty
|
|
|
|
with Unify [_, ty'] ->
|
|
|
|
raise (Unify [ty, ty'])
|
1996-04-22 04:15:41 -07:00
|
|
|
|
1997-03-14 07:19:48 -08:00
|
|
|
(* Correct the levels of type [ty]. *)
|
1997-03-24 12:12:00 -08:00
|
|
|
let correct_levels ty =
|
1998-06-24 12:22:26 -07:00
|
|
|
duplicate_type ty
|
|
|
|
|
|
|
|
(* Only generalize the type ty0 in ty *)
|
|
|
|
let limited_generalize ty0 ty =
|
|
|
|
let ty0 = repr ty0 in
|
|
|
|
|
|
|
|
let graph = Hashtbl.create 17 in
|
|
|
|
let idx = ref lowest_level in
|
|
|
|
let roots = ref [] in
|
|
|
|
|
|
|
|
let rec inverse pty ty =
|
|
|
|
let ty = repr ty in
|
|
|
|
if (ty.level > !current_level) || (ty.level = generic_level) then begin
|
|
|
|
decr idx;
|
|
|
|
Hashtbl.add graph !idx (ty, ref pty);
|
|
|
|
if (ty.level = generic_level) || (ty == ty0) then
|
|
|
|
roots := ty :: !roots;
|
|
|
|
ty.level <- !idx;
|
|
|
|
iter_type_expr (inverse [ty]) ty
|
|
|
|
end else if ty.level < lowest_level then begin
|
|
|
|
let (_, parents) = Hashtbl.find graph ty.level in
|
|
|
|
parents := pty @ !parents
|
|
|
|
end
|
|
|
|
|
|
|
|
and generalize_parents ty =
|
|
|
|
let idx = ty.level in
|
|
|
|
if idx <> generic_level then begin
|
|
|
|
ty.level <- generic_level;
|
|
|
|
List.iter generalize_parents !(snd (Hashtbl.find graph idx))
|
|
|
|
end
|
|
|
|
in
|
|
|
|
|
|
|
|
inverse [] ty;
|
|
|
|
if ty0.level < lowest_level then
|
|
|
|
iter_type_expr (inverse []) ty0;
|
|
|
|
List.iter generalize_parents !roots;
|
|
|
|
Hashtbl.iter
|
|
|
|
(fun _ (ty, _) ->
|
|
|
|
if ty.level <> generic_level then
|
|
|
|
ty.level <- !current_level)
|
|
|
|
graph
|
1997-03-13 13:18:06 -08:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1997-01-20 09:11:47 -08:00
|
|
|
(*******************)
|
|
|
|
(* Instantiation *)
|
|
|
|
(*******************)
|
|
|
|
|
1997-01-21 09:43:53 -08:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
let rec find_repr p1 =
|
|
|
|
function
|
|
|
|
Mnil ->
|
|
|
|
None
|
|
|
|
| Mcons (p2, ty, _, _) when Path.same p1 p2 ->
|
|
|
|
Some ty
|
|
|
|
| Mcons (_, _, _, rem) ->
|
|
|
|
find_repr p1 rem
|
|
|
|
| Mlink {contents = rem} ->
|
|
|
|
find_repr p1 rem
|
|
|
|
|
1997-01-21 05:38:42 -08:00
|
|
|
(*
|
|
|
|
Generic nodes are duplicated, while non-generic nodes are left
|
1997-03-24 12:12:00 -08:00
|
|
|
as-is.
|
1997-01-21 05:38:42 -08:00
|
|
|
During instantiation, the description of a generic node is first
|
1999-11-30 08:07:38 -08:00
|
|
|
replaced by a link to a stub ([Tsubst (newvar ())]). Once the
|
1997-03-24 12:12:00 -08:00
|
|
|
copy is made, it replaces the stub.
|
1997-01-21 05:38:42 -08:00
|
|
|
After instantiation, the description of generic node, which was
|
1997-03-24 12:12:00 -08:00
|
|
|
stored by [save_desc], must be put back, using [cleanup_types].
|
1997-01-21 05:38:42 -08:00
|
|
|
*)
|
1995-11-16 05:27:53 -08:00
|
|
|
|
1997-01-21 09:43:53 -08:00
|
|
|
let abbreviations = ref (ref Mnil)
|
1997-01-21 05:38:42 -08:00
|
|
|
(* Abbreviation memorized. *)
|
|
|
|
|
1997-03-14 07:19:48 -08:00
|
|
|
let rec copy ty =
|
1997-03-07 14:44:02 -08:00
|
|
|
let ty = repr ty in
|
1999-11-30 08:07:38 -08:00
|
|
|
match ty.desc with
|
|
|
|
Tsubst ty -> ty
|
|
|
|
| _ ->
|
|
|
|
if ty.level <> generic_level then ty else
|
1997-03-07 14:44:02 -08:00
|
|
|
let desc = ty.desc in
|
1997-03-24 12:12:00 -08:00
|
|
|
save_desc ty desc;
|
1999-11-30 08:07:38 -08:00
|
|
|
let t = newvar() in (* Stub *)
|
|
|
|
ty.desc <- Tsubst t;
|
1997-03-07 14:44:02 -08:00
|
|
|
t.desc <-
|
|
|
|
begin match desc with
|
|
|
|
Tvar ->
|
|
|
|
Tvar
|
1999-11-30 08:07:38 -08:00
|
|
|
| Tarrow (l, t1, t2) ->
|
|
|
|
Tarrow (l, copy t1, copy t2)
|
1997-03-07 14:44:02 -08:00
|
|
|
| Ttuple tl ->
|
1997-03-14 07:19:48 -08:00
|
|
|
Ttuple (List.map copy tl)
|
1997-03-07 14:44:02 -08:00
|
|
|
| Tconstr (p, tl, _) ->
|
1998-06-24 12:22:26 -07:00
|
|
|
begin match find_repr p !(!abbreviations) with
|
1998-08-15 06:43:10 -07:00
|
|
|
Some ty when repr ty != t -> (* XXX Commentaire... *)
|
1998-06-24 12:22:26 -07:00
|
|
|
Tlink ty
|
|
|
|
| _ ->
|
1997-03-07 14:44:02 -08:00
|
|
|
(*
|
|
|
|
One must allocate a new reference, so that abbrevia-
|
|
|
|
tions belonging to different branches of a type are
|
|
|
|
independent.
|
|
|
|
Moreover, a reference containing a [Mcons] must be
|
|
|
|
shared, so that the memorized expansion of an abbrevi-
|
|
|
|
ation can be released by changing the content of just
|
|
|
|
one reference.
|
|
|
|
*)
|
1998-06-24 12:22:26 -07:00
|
|
|
Tconstr (p, List.map copy tl,
|
|
|
|
ref (match !(!abbreviations) with
|
|
|
|
Mcons _ -> Mlink !abbreviations
|
|
|
|
| abbrev -> abbrev))
|
|
|
|
end
|
1997-03-07 14:44:02 -08:00
|
|
|
| Tobject (t1, {contents = name}) ->
|
|
|
|
let name' =
|
|
|
|
match name with
|
|
|
|
None ->
|
|
|
|
None
|
|
|
|
| Some (p, tl) ->
|
1997-03-14 07:19:48 -08:00
|
|
|
Some (p, List.map copy tl)
|
1997-03-07 14:44:02 -08:00
|
|
|
in
|
1997-03-14 07:19:48 -08:00
|
|
|
Tobject (copy t1, ref name')
|
1999-11-30 08:07:38 -08:00
|
|
|
| Tvariant row0 ->
|
|
|
|
let row = row_repr row0 in
|
|
|
|
let more = repr row.row_more in
|
|
|
|
(* We must substitute in a subtle way *)
|
|
|
|
begin match more.desc with
|
|
|
|
Tsubst ty2 ->
|
|
|
|
(* This variant type has been already copied *)
|
|
|
|
ty.desc <- Tsubst ty2; (* avoid Tlink in the new type *)
|
|
|
|
Tlink ty2
|
|
|
|
| _ ->
|
|
|
|
(* We shall really check the level on the row variable *)
|
|
|
|
if more.level <> generic_level then Tvariant row0 else
|
|
|
|
(* We create a new copy *)
|
|
|
|
let fields =
|
|
|
|
List.map
|
|
|
|
(fun (l,fi) -> l,
|
|
|
|
match row_field_repr fi with
|
|
|
|
Rpresent (Some ty) -> Rpresent(Some(copy ty))
|
|
|
|
| Reither(c, l, _) -> Reither(c, List.map copy l, ref None)
|
|
|
|
| fi -> fi)
|
|
|
|
row.row_fields
|
|
|
|
and name =
|
|
|
|
may_map (fun (p,l) -> p, List.map copy l) row.row_name in
|
|
|
|
let var =
|
|
|
|
Tvariant { row_fields = fields; row_more = newvar();
|
|
|
|
row_bound = List.map copy row.row_bound;
|
|
|
|
row_closed = row.row_closed; row_name = name }
|
|
|
|
in
|
|
|
|
(* Remember it for other occurences *)
|
|
|
|
save_desc more more.desc;
|
|
|
|
more.desc <- ty.desc;
|
|
|
|
var
|
|
|
|
end
|
1997-05-11 14:35:00 -07:00
|
|
|
| Tfield (label, kind, t1, t2) ->
|
|
|
|
begin match field_kind_repr kind with
|
|
|
|
Fpresent ->
|
|
|
|
Tfield (label, Fpresent, copy t1, copy t2)
|
1998-06-24 12:22:26 -07:00
|
|
|
| Fvar _ (* {contents = None} *) ->
|
|
|
|
Tfield (label, Fvar (ref None), copy t1, copy t2)
|
|
|
|
| Fabsent ->
|
|
|
|
assert false
|
1997-05-11 14:35:00 -07:00
|
|
|
end
|
1997-03-07 14:44:02 -08:00
|
|
|
| Tnil ->
|
|
|
|
Tnil
|
|
|
|
| Tlink t -> (* Actually unused *)
|
1997-03-14 07:19:48 -08:00
|
|
|
Tlink (copy t)
|
1999-11-30 08:07:38 -08:00
|
|
|
| Tsubst _ ->
|
|
|
|
assert false
|
1997-03-07 14:44:02 -08:00
|
|
|
end;
|
|
|
|
t
|
1996-04-22 04:15:41 -07:00
|
|
|
|
1997-01-21 05:38:42 -08:00
|
|
|
(**** Variants of instantiations ****)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let instance sch =
|
1997-03-14 07:19:48 -08:00
|
|
|
let ty = copy sch in
|
1997-01-21 05:38:42 -08:00
|
|
|
cleanup_types ();
|
1995-05-04 03:15:53 -07:00
|
|
|
ty
|
|
|
|
|
1997-02-20 12:39:02 -08:00
|
|
|
let instance_list schl =
|
1997-03-14 07:19:48 -08:00
|
|
|
let tyl = List.map copy schl in
|
1997-02-20 12:39:02 -08:00
|
|
|
cleanup_types ();
|
|
|
|
tyl
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
let instance_constructor cstr =
|
1997-03-14 07:19:48 -08:00
|
|
|
let ty_res = copy cstr.cstr_res in
|
|
|
|
let ty_args = List.map copy cstr.cstr_args in
|
1997-01-21 05:38:42 -08:00
|
|
|
cleanup_types ();
|
1995-05-04 03:15:53 -07:00
|
|
|
(ty_args, ty_res)
|
|
|
|
|
|
|
|
let instance_label lbl =
|
1997-03-14 07:19:48 -08:00
|
|
|
let ty_res = copy lbl.lbl_res in
|
|
|
|
let ty_arg = copy lbl.lbl_arg in
|
1997-01-21 05:38:42 -08:00
|
|
|
cleanup_types ();
|
1995-05-04 03:15:53 -07:00
|
|
|
(ty_arg, ty_res)
|
|
|
|
|
1996-04-22 04:15:41 -07:00
|
|
|
let instance_parameterized_type sch_args sch =
|
1997-03-14 07:19:48 -08:00
|
|
|
let ty_args = List.map copy sch_args in
|
|
|
|
let ty = copy sch in
|
1997-01-21 05:38:42 -08:00
|
|
|
cleanup_types ();
|
1996-04-22 04:15:41 -07:00
|
|
|
(ty_args, ty)
|
|
|
|
|
|
|
|
let instance_parameterized_type_2 sch_args sch_lst sch =
|
1997-03-14 07:19:48 -08:00
|
|
|
let ty_args = List.map copy sch_args in
|
|
|
|
let ty_lst = List.map copy sch_lst in
|
|
|
|
let ty = copy sch in
|
1997-01-21 05:38:42 -08:00
|
|
|
cleanup_types ();
|
1996-04-22 04:15:41 -07:00
|
|
|
(ty_args, ty_lst, ty)
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
let instance_class params cty =
|
|
|
|
let rec copy_class_type =
|
|
|
|
function
|
|
|
|
Tcty_constr (path, tyl, cty) ->
|
|
|
|
Tcty_constr (path, List.map copy tyl, copy_class_type cty)
|
|
|
|
| Tcty_signature sign ->
|
|
|
|
Tcty_signature
|
|
|
|
{cty_self = copy sign.cty_self;
|
|
|
|
cty_vars =
|
|
|
|
Vars.map (function (mut, ty) -> (mut, copy ty)) sign.cty_vars;
|
|
|
|
cty_concr = sign.cty_concr}
|
1999-11-30 08:07:38 -08:00
|
|
|
| Tcty_fun (l, ty, cty) ->
|
|
|
|
Tcty_fun (l, copy ty, copy_class_type cty)
|
1998-06-24 12:22:26 -07:00
|
|
|
in
|
|
|
|
let params' = List.map copy params in
|
|
|
|
let cty' = copy_class_type cty in
|
1997-01-21 05:38:42 -08:00
|
|
|
cleanup_types ();
|
1998-06-24 12:22:26 -07:00
|
|
|
(params', cty')
|
1996-04-22 04:15:41 -07:00
|
|
|
|
1997-01-23 04:46:46 -08:00
|
|
|
(**** Instantiation with parameter substitution ****)
|
1997-01-21 05:38:42 -08:00
|
|
|
|
1997-02-20 12:39:02 -08:00
|
|
|
let unify' = (* Forward declaration *)
|
|
|
|
ref (fun env ty1 ty2 -> raise (Unify []))
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
let rec subst env level abbrev ty params args body =
|
1997-03-24 12:12:00 -08:00
|
|
|
let old_level = !current_level in
|
|
|
|
current_level := level;
|
|
|
|
try
|
1997-02-20 12:39:02 -08:00
|
|
|
let body0 = newvar () in (* Stub *)
|
1998-06-24 12:22:26 -07:00
|
|
|
begin match ty with
|
1997-02-20 12:39:02 -08:00
|
|
|
None -> ()
|
1998-06-24 12:22:26 -07:00
|
|
|
| Some ({desc = Tconstr (path, _, _)} as ty) ->
|
|
|
|
memorize_abbrev abbrev path ty body0
|
|
|
|
| _ ->
|
|
|
|
assert false
|
1997-02-20 12:39:02 -08:00
|
|
|
end;
|
1997-01-21 05:38:42 -08:00
|
|
|
abbreviations := abbrev;
|
1997-02-20 12:39:02 -08:00
|
|
|
let (params', body') = instance_parameterized_type params body in
|
1997-01-21 09:43:53 -08:00
|
|
|
abbreviations := ref Mnil;
|
1997-02-20 12:39:02 -08:00
|
|
|
!unify' env body0 body';
|
|
|
|
List.iter2 (!unify' env) params' args;
|
1997-01-24 12:54:56 -08:00
|
|
|
current_level := old_level;
|
1997-02-20 12:39:02 -08:00
|
|
|
body'
|
1997-03-24 12:12:00 -08:00
|
|
|
with Unify _ as exn ->
|
|
|
|
current_level := old_level;
|
|
|
|
raise exn
|
1997-01-20 09:11:47 -08:00
|
|
|
|
1997-02-20 12:39:02 -08:00
|
|
|
(*
|
|
|
|
Only the shape of the type matters, not whether is is generic or
|
|
|
|
not. [generic_level] might be somewhat slower, but it ensures
|
|
|
|
invariants on types are enforced (decreasing levels.), and we don't
|
|
|
|
care about efficiency here.
|
|
|
|
*)
|
1997-03-18 13:05:15 -08:00
|
|
|
let apply env params body args =
|
|
|
|
try
|
|
|
|
subst env generic_level (ref Mnil) None params args body
|
|
|
|
with
|
|
|
|
Unify _ -> raise Cannot_apply
|
1997-01-20 09:11:47 -08:00
|
|
|
|
|
|
|
|
1997-01-21 05:38:42 -08:00
|
|
|
(****************************)
|
|
|
|
(* Abbreviation expansion *)
|
|
|
|
(****************************)
|
1997-01-20 09:11:47 -08:00
|
|
|
|
|
|
|
|
1997-01-23 04:46:46 -08:00
|
|
|
(* Search whether the expansion has been memorized. *)
|
1996-04-22 04:15:41 -07:00
|
|
|
let rec find_expans p1 =
|
|
|
|
function
|
1997-01-21 09:43:53 -08:00
|
|
|
Mnil ->
|
1996-04-22 04:15:41 -07:00
|
|
|
None
|
1998-06-24 12:22:26 -07:00
|
|
|
| Mcons (p2, _, ty, _) when Path.same p1 p2 ->
|
1997-01-23 04:46:46 -08:00
|
|
|
Some ty
|
1998-06-24 12:22:26 -07:00
|
|
|
| Mcons (_, _, _, rem) ->
|
1997-01-23 04:46:46 -08:00
|
|
|
find_expans p1 rem
|
1997-01-21 09:43:53 -08:00
|
|
|
| Mlink {contents = rem} ->
|
|
|
|
find_expans p1 rem
|
|
|
|
|
|
|
|
let previous_env = ref Env.empty
|
1996-04-22 04:15:41 -07:00
|
|
|
|
1997-01-23 04:46:46 -08:00
|
|
|
(* Expand an abbreviation. The expansion is memorized. *)
|
1997-03-07 14:44:02 -08:00
|
|
|
(*
|
|
|
|
Assume the level is greater than the path binding time of the
|
|
|
|
expanded abbreviation.
|
|
|
|
*)
|
1997-03-18 13:05:15 -08:00
|
|
|
(*
|
|
|
|
An abbreviation expansion will fail in either of these cases:
|
|
|
|
1. The type constructor does not correspond to a manifest type.
|
|
|
|
2. The type constructor is defined in an external file, and this
|
|
|
|
file is not in the path (missing -I options).
|
|
|
|
3. The type constructor is not in the "local" environment. This can
|
|
|
|
happens when a non-generic type variable has been instantiated
|
|
|
|
afterwards to the not yet defined type constructor. (Actually,
|
|
|
|
this cannot happen at the moment due to the strong constraints
|
|
|
|
between type levels and constructor binding time.)
|
|
|
|
4. The expansion requires the expansion of another abbreviation,
|
|
|
|
and this other expansion fails.
|
|
|
|
*)
|
1998-06-24 12:22:26 -07:00
|
|
|
let expand_abbrev env ty =
|
1997-02-20 12:39:02 -08:00
|
|
|
(*
|
|
|
|
If the environnement has changed, memorized expansions might not
|
|
|
|
be correct anymore, and so we flush the cache. This is safe but
|
1998-06-24 12:22:26 -07:00
|
|
|
quite pessimistic: it would be enough to flush the cache when a
|
|
|
|
type or module definition is overriden in the environnement.
|
1997-02-20 12:39:02 -08:00
|
|
|
*)
|
1997-01-21 09:43:53 -08:00
|
|
|
if env != !previous_env then begin
|
|
|
|
cleanup_abbrev ();
|
|
|
|
previous_env := env
|
|
|
|
end;
|
1998-06-24 12:22:26 -07:00
|
|
|
match ty with
|
|
|
|
{desc = Tconstr (path, args, abbrev); level = level} ->
|
|
|
|
begin match find_expans path !abbrev with
|
|
|
|
Some ty ->
|
|
|
|
if level <> generic_level then
|
1998-11-30 05:06:53 -08:00
|
|
|
begin try
|
|
|
|
update_level env level ty
|
|
|
|
with Unify _ ->
|
|
|
|
(* XXX This should not happen.
|
|
|
|
However, levels are not correctly restored after a
|
|
|
|
typing error *)
|
|
|
|
()
|
|
|
|
end;
|
1998-06-24 12:22:26 -07:00
|
|
|
ty
|
|
|
|
| None ->
|
|
|
|
let (params, body) =
|
|
|
|
try Env.find_type_expansion path env with Not_found ->
|
|
|
|
raise Cannot_expand
|
|
|
|
in
|
1999-11-30 08:07:38 -08:00
|
|
|
let ty' = subst env level abbrev (Some ty) params args body in
|
|
|
|
(* Hack to name the variant type *)
|
|
|
|
begin match repr ty' with
|
|
|
|
{desc=Tvariant row} as ty when static_row row ->
|
|
|
|
ty.desc <- Tvariant { row with row_name = Some (path, args) }
|
|
|
|
| _ -> ()
|
|
|
|
end;
|
|
|
|
ty'
|
1998-06-24 12:22:26 -07:00
|
|
|
end
|
|
|
|
| _ ->
|
|
|
|
assert false
|
1996-04-22 04:15:41 -07:00
|
|
|
|
1997-06-29 06:16:47 -07:00
|
|
|
(* Fully expand the head of a type. Raise an exception if the type
|
|
|
|
cannot be expanded. *)
|
|
|
|
let rec try_expand_head env ty =
|
1996-05-20 09:43:29 -07:00
|
|
|
let ty = repr ty in
|
|
|
|
match ty.desc with
|
1998-06-24 12:22:26 -07:00
|
|
|
Tconstr _ ->
|
|
|
|
let ty' = expand_abbrev env ty in
|
1996-05-20 09:43:29 -07:00
|
|
|
begin try
|
1997-06-29 06:16:47 -07:00
|
|
|
try_expand_head env ty'
|
1996-05-20 09:43:29 -07:00
|
|
|
with Cannot_expand ->
|
1997-06-29 06:16:47 -07:00
|
|
|
repr ty'
|
1996-05-20 09:43:29 -07:00
|
|
|
end
|
|
|
|
| _ ->
|
1997-06-29 06:16:47 -07:00
|
|
|
raise Cannot_expand
|
|
|
|
|
|
|
|
let _ = try_expand_head' := try_expand_head
|
|
|
|
|
|
|
|
(* Fully expand the head of a type. *)
|
|
|
|
let rec expand_head env ty =
|
|
|
|
try try_expand_head env ty with Cannot_expand -> repr ty
|
1996-05-20 09:43:29 -07:00
|
|
|
|
2000-02-05 04:11:34 -08:00
|
|
|
let _ = Env.expand_head := expand_head
|
|
|
|
|
1999-11-08 15:05:03 -08:00
|
|
|
(* Make sure that the type parameters of the type constructor [ty]
|
|
|
|
respect the type constraints *)
|
|
|
|
let enforce_constraints env ty =
|
|
|
|
match ty with
|
|
|
|
{desc = Tconstr (path, args, abbrev); level = level} ->
|
|
|
|
let decl = Env.find_type path env in
|
|
|
|
ignore
|
|
|
|
(subst env level (ref Mnil) None decl.type_params args (newvar2 level))
|
|
|
|
| _ ->
|
|
|
|
assert false
|
|
|
|
|
1997-03-07 14:44:02 -08:00
|
|
|
(* Recursively expand the head of a type.
|
1997-01-20 09:11:47 -08:00
|
|
|
Also expand #-types. *)
|
1996-05-20 09:43:29 -07:00
|
|
|
let rec full_expand env ty =
|
1997-03-07 14:44:02 -08:00
|
|
|
let ty = repr (expand_head env ty) in
|
1996-05-20 09:43:29 -07:00
|
|
|
match ty.desc with
|
1996-05-26 06:42:34 -07:00
|
|
|
Tobject (fi, {contents = Some (_, v::_)}) when (repr v).desc = Tvar ->
|
1998-07-03 10:40:39 -07:00
|
|
|
newty2 ty.level (Tobject (fi, ref None))
|
1996-05-20 09:43:29 -07:00
|
|
|
| _ ->
|
|
|
|
ty
|
|
|
|
|
1997-02-20 12:39:02 -08:00
|
|
|
(*
|
|
|
|
Check whether the abbreviation expands to a well-defined type.
|
1997-01-20 09:11:47 -08:00
|
|
|
During the typing of a class, abbreviations for correspondings
|
1997-02-20 12:39:02 -08:00
|
|
|
types expand to non-generic types.
|
|
|
|
*)
|
1996-04-22 04:15:41 -07:00
|
|
|
let generic_abbrev env path =
|
1995-11-03 05:23:03 -08:00
|
|
|
try
|
1997-04-01 12:52:21 -08:00
|
|
|
let (_, body) = Env.find_type_expansion path env in
|
|
|
|
(repr body).level = generic_level
|
1996-04-22 04:15:41 -07:00
|
|
|
with
|
|
|
|
Not_found ->
|
|
|
|
false
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1997-01-21 05:38:42 -08:00
|
|
|
|
|
|
|
(*****************)
|
1997-03-18 13:05:15 -08:00
|
|
|
(* Occur check *)
|
1997-01-21 05:38:42 -08:00
|
|
|
(*****************)
|
|
|
|
|
|
|
|
|
1997-03-18 13:05:15 -08:00
|
|
|
exception Occur
|
1997-01-21 05:38:42 -08:00
|
|
|
|
1997-03-24 12:12:00 -08:00
|
|
|
(* The marks are already used by [expand_abbrev]... *)
|
|
|
|
let visited = ref []
|
1996-10-28 09:51:55 -08:00
|
|
|
|
1997-03-24 12:12:00 -08:00
|
|
|
let rec non_recursive_abbrev env ty =
|
1997-03-18 13:05:15 -08:00
|
|
|
let ty = repr ty in
|
1997-03-24 12:12:00 -08:00
|
|
|
if ty == none then raise Recursive_abbrev;
|
|
|
|
if not (List.memq ty !visited) then begin
|
1997-03-18 13:05:15 -08:00
|
|
|
let level = ty.level in
|
1997-03-24 12:12:00 -08:00
|
|
|
visited := ty :: !visited;
|
1997-03-18 13:05:15 -08:00
|
|
|
match ty.desc with
|
1997-03-24 12:12:00 -08:00
|
|
|
Tconstr(p, args, abbrev) ->
|
1997-03-18 13:05:15 -08:00
|
|
|
begin try
|
1997-06-29 06:16:47 -07:00
|
|
|
non_recursive_abbrev env (try_expand_head env ty)
|
1997-05-12 04:15:51 -07:00
|
|
|
with Cannot_expand ->
|
1999-11-17 15:53:19 -08:00
|
|
|
iter_type_expr (non_recursive_abbrev env) ty
|
1997-05-12 04:15:51 -07:00
|
|
|
end
|
1999-11-30 08:07:38 -08:00
|
|
|
| Tobject _ | Tvariant _ ->
|
1997-03-18 13:05:15 -08:00
|
|
|
()
|
|
|
|
| _ ->
|
1997-03-24 12:12:00 -08:00
|
|
|
iter_type_expr (non_recursive_abbrev env) ty
|
1997-03-18 13:05:15 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
let correct_abbrev env ident params ty =
|
1999-11-17 15:53:19 -08:00
|
|
|
if not !Clflags.recursive_types then begin
|
|
|
|
visited := [];
|
|
|
|
non_recursive_abbrev env
|
|
|
|
(subst env generic_level
|
|
|
|
(ref (Mcons (Path.Pident ident, none, none, Mnil))) None
|
|
|
|
[] [] ty);
|
|
|
|
visited := []
|
|
|
|
end
|
1997-02-20 12:39:02 -08:00
|
|
|
|
1997-11-18 07:14:56 -08:00
|
|
|
let rec occur_rec env visited ty0 ty =
|
1997-06-29 06:16:47 -07:00
|
|
|
if ty == ty0 then raise Occur;
|
|
|
|
match ty.desc with
|
|
|
|
Tconstr(p, tl, abbrev) ->
|
|
|
|
begin try
|
1997-11-18 07:14:56 -08:00
|
|
|
if List.memq ty visited then raise Occur;
|
|
|
|
iter_type_expr (occur_rec env (ty::visited) ty0) ty
|
1997-06-29 06:16:47 -07:00
|
|
|
with Occur -> try
|
1998-07-03 10:40:39 -07:00
|
|
|
let ty' = try_expand_head env ty in
|
1999-11-25 14:38:15 -08:00
|
|
|
(* Maybe we could simply make a recursive call here,
|
|
|
|
but it seems it could make the occur check loop
|
|
|
|
(see change in rev. 1.58) *)
|
|
|
|
if ty' == ty0 || List.memq ty' visited then raise Occur;
|
|
|
|
match ty'.desc with
|
|
|
|
Tobject _ -> ()
|
1999-11-26 04:51:56 -08:00
|
|
|
| _ -> iter_type_expr (occur_rec env (ty'::visited) ty0) ty'
|
1997-06-29 06:16:47 -07:00
|
|
|
with Cannot_expand ->
|
1999-11-17 15:53:19 -08:00
|
|
|
raise Occur
|
1997-06-29 06:16:47 -07:00
|
|
|
end
|
1999-11-30 08:07:38 -08:00
|
|
|
| Tobject _ | Tvariant _ ->
|
1997-06-29 06:16:47 -07:00
|
|
|
()
|
|
|
|
| _ ->
|
1997-11-18 07:14:56 -08:00
|
|
|
iter_type_expr (occur_rec env visited ty0) ty
|
1997-06-29 06:16:47 -07:00
|
|
|
|
1996-04-22 04:15:41 -07:00
|
|
|
let occur env ty0 ty =
|
1999-11-17 15:53:19 -08:00
|
|
|
if not !Clflags.recursive_types then
|
|
|
|
try occur_rec env [] ty0 ty with Occur -> raise (Unify [])
|
1996-04-22 04:15:41 -07:00
|
|
|
|
1997-03-18 13:05:15 -08:00
|
|
|
|
|
|
|
(*****************)
|
|
|
|
(* Unification *)
|
|
|
|
(*****************)
|
|
|
|
|
|
|
|
|
|
|
|
|
1997-01-20 09:11:47 -08:00
|
|
|
(**** Transform error trace ****)
|
1997-03-08 14:05:39 -08:00
|
|
|
(* +++ Move it to some other place ? *)
|
1997-01-20 09:11:47 -08:00
|
|
|
|
|
|
|
let expand_trace env trace =
|
|
|
|
List.fold_right
|
|
|
|
(fun (t1, t2) rem ->
|
|
|
|
(repr t1, full_expand env t1)::(repr t2, full_expand env t2)::rem)
|
|
|
|
trace []
|
|
|
|
|
|
|
|
(**** Unification ****)
|
|
|
|
|
1997-03-07 14:44:02 -08:00
|
|
|
(* Return whether [t0] occurs in [ty]. Objects are also traversed. *)
|
1997-02-20 12:39:02 -08:00
|
|
|
let deep_occur t0 ty =
|
|
|
|
let rec occur_rec ty =
|
|
|
|
let ty = repr ty in
|
1997-03-08 14:05:39 -08:00
|
|
|
if ty.level >= lowest_level then begin
|
1997-02-20 12:39:02 -08:00
|
|
|
if ty == t0 then raise Occur;
|
1997-03-08 14:05:39 -08:00
|
|
|
ty.level <- pivot_level - ty.level;
|
1997-02-20 12:39:02 -08:00
|
|
|
iter_type_expr occur_rec ty
|
|
|
|
end
|
|
|
|
in
|
|
|
|
try
|
1997-03-07 14:44:02 -08:00
|
|
|
occur_rec ty; unmark_type ty; false
|
1997-02-20 12:39:02 -08:00
|
|
|
with Occur ->
|
1997-03-07 14:44:02 -08:00
|
|
|
unmark_type ty; true
|
1997-02-20 12:39:02 -08:00
|
|
|
|
|
|
|
(*
|
|
|
|
1. When unifying two non-abbreviated types, one type is made a link
|
|
|
|
to the other. When unifying an abbreviated type with a
|
|
|
|
non-abbreviated type, the non-abbreviated type is made a link to
|
|
|
|
the other one. When unifying to abbreviated types, these two
|
|
|
|
types are kept distincts, but they are made to (temporally)
|
|
|
|
expand to the same type.
|
|
|
|
2. Abbreviations with at least one parameter are systematically
|
|
|
|
expanded. The overhead does not seem to high, and that way
|
|
|
|
abbreviations where some parameters does not appear in the
|
|
|
|
expansion, such as ['a t = int], are correctly handled. In
|
|
|
|
particular, for this example, unifying ['a t] with ['b t] keeps
|
|
|
|
['a] and ['b] distincts. (Is it really important ?)
|
|
|
|
3. Unifying an abbreviation ['a t = 'a] with ['a] should not yield
|
|
|
|
['a t as 'a]. Indeed, the type variable would otherwise be lost.
|
|
|
|
This problem occurs for abbreviations expanding to a type
|
|
|
|
variable, but also to many other constrained abbreviations (for
|
|
|
|
instance, [(< x : 'a > -> unit) t = <x : 'a>]). The solution is
|
|
|
|
that, if an abbreviation is unified with some subpart of its
|
|
|
|
parameters, then the parameter actually does not get
|
|
|
|
abbreviated. It would be possible to check whether some
|
|
|
|
information is indeed lost, but it probably does not worth it.
|
|
|
|
*)
|
|
|
|
let rec unify env t1 t2 =
|
|
|
|
(* First step: special cases (optimizations) *)
|
1996-04-22 04:15:41 -07:00
|
|
|
if t1 == t2 then () else
|
1997-01-20 09:11:47 -08:00
|
|
|
let t1 = repr t1 in
|
|
|
|
let t2 = repr t2 in
|
1996-04-22 04:15:41 -07:00
|
|
|
if t1 == t2 then () else
|
1997-02-20 12:39:02 -08:00
|
|
|
|
1996-05-20 09:43:29 -07:00
|
|
|
try
|
|
|
|
match (t1.desc, t2.desc) with
|
1997-02-20 12:39:02 -08:00
|
|
|
(Tvar, Tconstr _) when deep_occur t1 t2 ->
|
|
|
|
unify2 env t1 t2
|
|
|
|
| (Tconstr _, Tvar) when deep_occur t2 t1 ->
|
|
|
|
unify2 env t1 t2
|
|
|
|
| (Tvar, _) ->
|
1997-06-29 06:16:47 -07:00
|
|
|
occur env t1 t2;
|
1997-02-20 12:39:02 -08:00
|
|
|
update_level env t1.level t2;
|
|
|
|
t1.desc <- Tlink t2
|
1996-05-20 09:43:29 -07:00
|
|
|
| (_, Tvar) ->
|
1997-06-29 06:16:47 -07:00
|
|
|
occur env t2 t1;
|
1997-02-20 12:39:02 -08:00
|
|
|
update_level env t2.level t1;
|
|
|
|
t2.desc <- Tlink t1
|
|
|
|
| (Tconstr (p1, [], _), Tconstr (p2, [], _)) when Path.same p1 p2 ->
|
|
|
|
update_level env t1.level t2;
|
|
|
|
t1.desc <- Tlink t2
|
|
|
|
| _ ->
|
|
|
|
unify2 env t1 t2
|
|
|
|
with Unify trace ->
|
|
|
|
raise (Unify ((t1, t2)::trace))
|
|
|
|
|
|
|
|
and unify2 env t1 t2 =
|
|
|
|
(* Second step: expansion of abbreviations *)
|
1997-03-07 14:44:02 -08:00
|
|
|
let t1' = expand_head env t1 in
|
|
|
|
let t2' = expand_head env t2 in
|
1997-03-24 12:12:00 -08:00
|
|
|
(* Expansion may have changed the representative of the types... *)
|
1999-09-09 12:38:29 -07:00
|
|
|
let t1' = expand_head env t1' in
|
|
|
|
let t2' = expand_head env t2' in
|
1997-02-20 12:39:02 -08:00
|
|
|
if t1' == t2' then () else
|
|
|
|
|
1997-03-24 12:12:00 -08:00
|
|
|
let t1 = repr t1 and t2 = repr t2 in
|
1997-02-20 12:39:02 -08:00
|
|
|
if (t1 == t1') || (t2 != t2') then
|
|
|
|
unify3 env t1 t1' t2 t2'
|
|
|
|
else
|
|
|
|
try unify3 env t2 t2' t1 t1' with Unify trace ->
|
|
|
|
raise (Unify (List.map (fun (x, y) -> (y, x)) trace))
|
|
|
|
|
|
|
|
and unify3 env t1 t1' t2 t2' =
|
|
|
|
(* Third step: truly unification *)
|
|
|
|
(* Assumes either [t1 == t1'] or [t2 != t2'] *)
|
|
|
|
let d1 = t1'.desc and d2 = t2'.desc in
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
let create_recursion = (t2 != t2') && (deep_occur t1' t2) in
|
|
|
|
occur env t1' t2;
|
|
|
|
update_level env t1'.level t2;
|
|
|
|
t1'.desc <- Tlink t2;
|
1997-02-20 12:39:02 -08:00
|
|
|
|
1996-04-22 04:15:41 -07:00
|
|
|
try
|
1997-02-20 12:39:02 -08:00
|
|
|
begin match (d1, d2) with
|
|
|
|
(Tvar, _) ->
|
1997-06-29 06:16:47 -07:00
|
|
|
()
|
1997-02-20 12:39:02 -08:00
|
|
|
| (_, Tvar) ->
|
1997-06-29 06:16:47 -07:00
|
|
|
occur env t2' (newty d1);
|
1997-02-20 12:39:02 -08:00
|
|
|
if t1 == t1' then begin
|
|
|
|
(* The variable must be instantiated... *)
|
1998-07-03 10:40:39 -07:00
|
|
|
let ty = newty2 t1'.level d1 in
|
1997-02-20 12:39:02 -08:00
|
|
|
update_level env t2'.level ty;
|
|
|
|
t2'.desc <- Tlink ty
|
|
|
|
end else begin
|
|
|
|
t1'.desc <- d1;
|
|
|
|
update_level env t2'.level t1;
|
|
|
|
t2'.desc <- Tlink t1
|
|
|
|
end
|
1999-11-30 08:07:38 -08:00
|
|
|
| (Tarrow (l1, t1, u1), Tarrow (l2, t2, u2)) when l1 = l2
|
|
|
|
or !Clflags.classic && not (is_optional l1 or is_optional l2) ->
|
1997-02-20 12:39:02 -08:00
|
|
|
unify env t1 t2; unify env u1 u2
|
1996-04-22 04:15:41 -07:00
|
|
|
| (Ttuple tl1, Ttuple tl2) ->
|
|
|
|
unify_list env tl1 tl2
|
1997-02-20 12:39:02 -08:00
|
|
|
| (Tconstr (p1, tl1, _), Tconstr (p2, tl2, _)) when Path.same p1 p2 ->
|
1996-04-22 04:15:41 -07:00
|
|
|
unify_list env tl1 tl2
|
1997-05-11 14:35:00 -07:00
|
|
|
| (Tobject (fi1, nm1), Tobject (fi2, _)) ->
|
1996-09-23 10:15:59 -07:00
|
|
|
unify_fields env fi1 fi2;
|
1997-05-11 14:35:00 -07:00
|
|
|
(* Type [t2'] may have been instantiated by [unify_fields] *)
|
|
|
|
(* XXX One should do some kind of unification... *)
|
|
|
|
begin match (repr t2').desc with
|
|
|
|
Tobject (_, {contents = Some (_, va::_)})
|
|
|
|
when (repr va).desc = Tvar ->
|
|
|
|
()
|
|
|
|
| Tobject (_, nm2) ->
|
|
|
|
nm2 := !nm1
|
|
|
|
| _ ->
|
|
|
|
()
|
1996-04-22 04:15:41 -07:00
|
|
|
end
|
1999-11-30 08:07:38 -08:00
|
|
|
| (Tvariant row1, Tvariant row2) ->
|
|
|
|
unify_row env row1 row2
|
1997-03-08 14:05:39 -08:00
|
|
|
| (Tfield _, Tfield _) -> (* Actually unused *)
|
1997-05-11 14:35:00 -07:00
|
|
|
unify_fields env t1' t2'
|
1997-01-20 09:11:47 -08:00
|
|
|
| (Tnil, Tnil) ->
|
|
|
|
()
|
1996-04-22 04:15:41 -07:00
|
|
|
| (_, _) ->
|
1996-05-20 09:43:29 -07:00
|
|
|
raise (Unify [])
|
1998-06-24 12:22:26 -07:00
|
|
|
end;
|
|
|
|
|
|
|
|
(* XXX Commentaires + changer "create_recursion" *)
|
|
|
|
if create_recursion then begin
|
|
|
|
match t2.desc with
|
|
|
|
Tconstr (p, tl, abbrev) ->
|
|
|
|
forget_abbrev abbrev p;
|
|
|
|
let t2'' = expand_head env t2 in
|
|
|
|
if not (closed_parameterized_type tl t2'') then
|
|
|
|
(repr t2).desc <- Tlink (repr t2')
|
|
|
|
| _ ->
|
|
|
|
assert false
|
1997-03-08 04:14:57 -08:00
|
|
|
end
|
1998-06-24 12:22:26 -07:00
|
|
|
|
1997-02-20 12:39:02 -08:00
|
|
|
(*
|
|
|
|
(*
|
|
|
|
Can only be done afterwards, once the row variable has
|
|
|
|
(possibly) been instantiated.
|
|
|
|
*)
|
|
|
|
if t1 != t1' (* && t2 != t2' *) then begin
|
|
|
|
match (t1.desc, t2.desc) with
|
|
|
|
(Tconstr (p, ty::_, _), _)
|
1997-03-24 12:12:00 -08:00
|
|
|
when ((repr ty).desc <> Tvar)
|
1997-02-20 12:39:02 -08:00
|
|
|
&& weak_abbrev p
|
|
|
|
&& not (deep_occur t1 t2) ->
|
|
|
|
update_level env t1.level t2;
|
|
|
|
t1.desc <- Tlink t2
|
|
|
|
| (_, Tconstr (p, ty::_, _))
|
1997-03-24 12:12:00 -08:00
|
|
|
when ((repr ty).desc <> Tvar)
|
1997-02-20 12:39:02 -08:00
|
|
|
&& weak_abbrev p
|
|
|
|
&& not (deep_occur t2 t1) ->
|
|
|
|
update_level env t2.level t1;
|
|
|
|
t2.desc <- Tlink t1;
|
|
|
|
t1'.desc <- Tlink t2'
|
|
|
|
| _ ->
|
|
|
|
()
|
|
|
|
end
|
|
|
|
*)
|
|
|
|
with Unify trace ->
|
|
|
|
t1'.desc <- d1;
|
|
|
|
raise (Unify trace)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
and unify_list env tl1 tl2 =
|
1996-05-31 05:27:03 -07:00
|
|
|
if List.length tl1 <> List.length tl2 then
|
|
|
|
raise (Unify []);
|
1997-02-20 12:39:02 -08:00
|
|
|
List.iter2 (unify env) tl1 tl2
|
1996-04-22 04:15:41 -07:00
|
|
|
|
1997-01-20 09:11:47 -08:00
|
|
|
and unify_fields env ty1 ty2 = (* Optimization *)
|
1996-04-22 04:15:41 -07:00
|
|
|
let (fields1, rest1) = flatten_fields ty1
|
|
|
|
and (fields2, rest2) = flatten_fields ty2 in
|
|
|
|
let (pairs, miss1, miss2) = associate_fields fields1 fields2 in
|
|
|
|
let va = newvar () in
|
1998-10-10 10:57:27 -07:00
|
|
|
unify env (build_fields (repr ty1).level miss1 va) rest2;
|
|
|
|
unify env rest1 (build_fields (repr ty2).level miss2 va);
|
1998-06-24 12:22:26 -07:00
|
|
|
List.iter
|
|
|
|
(fun (n, k1, t1, k2, t2) ->
|
|
|
|
unify_kind k1 k2;
|
|
|
|
try unify env t1 t2 with Unify trace ->
|
|
|
|
raise (Unify ((newty (Tfield(n, k1, t1, va)),
|
|
|
|
newty (Tfield(n, k2, t2, va)))::trace)))
|
|
|
|
pairs
|
1997-05-11 14:35:00 -07:00
|
|
|
|
|
|
|
and unify_kind k1 k2 =
|
|
|
|
let k1 = field_kind_repr k1 in
|
|
|
|
let k2 = field_kind_repr k2 in
|
|
|
|
match k1, k2 with
|
1998-06-24 12:22:26 -07:00
|
|
|
(Fvar r, (Fvar _ | Fpresent)) -> r := Some k2
|
|
|
|
| (Fpresent, Fvar r) -> r := Some k1
|
|
|
|
| (Fpresent, Fpresent) -> ()
|
|
|
|
| _ -> assert false
|
1996-04-22 04:15:41 -07:00
|
|
|
|
1999-11-30 08:07:38 -08:00
|
|
|
and unify_row env row1 row2 =
|
|
|
|
let row1 = row_repr row1 and row2 = row_repr row2 in
|
2000-02-24 02:18:25 -08:00
|
|
|
let rm1 = row_more row1 and rm2 = row_more row2 in
|
1999-11-30 08:07:38 -08:00
|
|
|
if rm1 == rm2 then () else
|
|
|
|
let r1, r2, pairs = merge_row_fields row1.row_fields row2.row_fields in
|
|
|
|
ignore (List.fold_left
|
|
|
|
(fun hl l ->
|
|
|
|
let h = hash_variant l in
|
|
|
|
try raise(Tags(l,List.assoc h hl))
|
|
|
|
with Not_found -> (h,l)::hl)
|
|
|
|
(List.map (fun (l,_) -> (hash_variant l, l)) row1.row_fields)
|
|
|
|
(List.map fst r2));
|
|
|
|
let more = newty2 (min rm1.level rm2.level) Tvar
|
|
|
|
and closed = row1.row_closed || row2.row_closed in
|
|
|
|
let keep switch =
|
|
|
|
List.for_all
|
|
|
|
(fun (_,f1,f2) ->
|
|
|
|
let f1, f2 = switch f1 f2 in
|
|
|
|
row_field_repr f1 = Rabsent || row_field_repr f2 <> Rabsent)
|
|
|
|
pairs
|
|
|
|
in
|
2000-02-24 02:18:25 -08:00
|
|
|
let empty fields =
|
|
|
|
List.for_all (fun (_,f) -> row_field_repr f = Rabsent) fields in
|
1999-11-30 08:07:38 -08:00
|
|
|
let name =
|
2000-02-24 02:18:25 -08:00
|
|
|
if row1.row_name <> None && (row1.row_closed || empty r2) &&
|
|
|
|
(not row2.row_closed || keep (fun f1 f2 -> f1, f2) && empty r1)
|
|
|
|
then row1.row_name
|
|
|
|
else if row2.row_name <> None && (row2.row_closed || empty r1) &&
|
|
|
|
(not row1.row_closed || keep (fun f1 f2 -> f2, f1) && empty r2)
|
|
|
|
then row1.row_name
|
|
|
|
else None
|
1999-11-30 08:07:38 -08:00
|
|
|
in
|
|
|
|
let bound = row1.row_bound @ row2.row_bound in
|
|
|
|
let row0 = {row_fields = []; row_more = more; row_bound = bound;
|
|
|
|
row_closed = closed; row_name = name} in
|
|
|
|
let more row rest =
|
|
|
|
let rest =
|
|
|
|
if closed then filter_row_fields row.row_closed rest else rest in
|
|
|
|
if rest <> [] && row.row_closed then raise (Unify []);
|
|
|
|
let ty =
|
|
|
|
newty2 generic_level (Tvariant {row0 with row_fields = rest}) in
|
|
|
|
update_level env (repr row.row_more).level ty;
|
|
|
|
ty
|
|
|
|
in
|
|
|
|
let md1 = rm1.desc and md2 = rm2.desc in
|
|
|
|
begin try
|
|
|
|
rm1.desc <- Tlink (more row1 r2);
|
|
|
|
rm2.desc <- Tlink (more row2 r1);
|
|
|
|
List.iter
|
|
|
|
(fun (l,f1,f2) ->
|
|
|
|
let f1 = row_field_repr f1 and f2 = row_field_repr f2 in
|
|
|
|
if f1 == f2 then () else
|
|
|
|
match f1, f2 with
|
|
|
|
Rpresent(Some t1), Rpresent(Some t2) -> unify env t1 t2
|
|
|
|
| Rpresent None, Rpresent None -> ()
|
|
|
|
| Reither(c1, tl1, e1), Reither(c2, tl2, e2) ->
|
|
|
|
if e1 == e2 then () else
|
|
|
|
let tl = tl1 @ tl2 in
|
|
|
|
let tl =
|
|
|
|
List.fold_right
|
|
|
|
(fun t tl ->
|
|
|
|
let t = repr t in if List.memq t tl then tl else t::tl)
|
|
|
|
tl [] in
|
|
|
|
let f = Reither(c1 or c2, tl, ref None) in
|
|
|
|
e1 := Some f; e2 := Some f
|
|
|
|
| Reither(false, tl, e1), Rpresent(Some t2) ->
|
|
|
|
e1 := Some f2;
|
|
|
|
(try List.iter (fun t1 -> unify env t1 t2) tl
|
|
|
|
with exn -> e1 := None; raise exn)
|
|
|
|
| Rpresent(Some t1), Reither(false, tl, e2) ->
|
|
|
|
e2 := Some f1;
|
|
|
|
(try List.iter (unify env t1) tl
|
|
|
|
with exn -> e2 := None; raise exn)
|
|
|
|
| Reither(true, [], e1), Rpresent None -> e1 := Some f2
|
|
|
|
| Rpresent None, Reither(true, [], e2) -> e2 := Some f1
|
|
|
|
| Reither(_, _, e1), Rabsent -> e1 := Some f2
|
|
|
|
| Rabsent, Reither(_, _, e2) -> e2 := Some f1
|
|
|
|
| Rabsent, Rabsent -> ()
|
|
|
|
| _ -> raise (Unify []))
|
|
|
|
pairs
|
|
|
|
with exn ->
|
|
|
|
rm1.desc <- md1; rm2.desc <- md2; raise exn
|
|
|
|
end
|
|
|
|
|
1996-04-22 04:15:41 -07:00
|
|
|
let unify env ty1 ty2 =
|
1996-05-20 09:43:29 -07:00
|
|
|
try
|
1997-02-20 12:39:02 -08:00
|
|
|
unify env ty1 ty2
|
1996-05-20 09:43:29 -07:00
|
|
|
with Unify trace ->
|
1998-06-24 12:22:26 -07:00
|
|
|
raise (Unify (expand_trace env trace))
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1997-02-20 12:39:02 -08:00
|
|
|
let _ = unify' := unify
|
|
|
|
|
1997-01-20 09:11:47 -08:00
|
|
|
(**** Special cases of unification ****)
|
|
|
|
|
1999-11-30 08:07:38 -08:00
|
|
|
(*
|
|
|
|
Unify [t] and [l:'a -> 'b]. Return ['a] and ['b].
|
|
|
|
In modern mode, label mismatch is accepted when
|
|
|
|
(1) the requested label is ""
|
|
|
|
(2) the original label is not optional
|
|
|
|
*)
|
|
|
|
let rec filter_arrow env t l =
|
1997-06-29 06:16:47 -07:00
|
|
|
let t = expand_head env t in
|
1996-04-22 04:15:41 -07:00
|
|
|
match t.desc with
|
|
|
|
Tvar ->
|
|
|
|
let t1 = newvar () and t2 = newvar () in
|
1999-11-30 08:07:38 -08:00
|
|
|
let t' = newty (Tarrow (l, t1, t2)) in
|
1997-01-23 04:46:46 -08:00
|
|
|
update_level env t.level t';
|
1996-04-22 04:15:41 -07:00
|
|
|
t.desc <- Tlink t';
|
1995-05-04 03:15:53 -07:00
|
|
|
(t1, t2)
|
1999-11-30 08:07:38 -08:00
|
|
|
| Tarrow(l', t1, t2)
|
|
|
|
when l = l' || !Clflags.classic && l = "" && not (is_optional l') ->
|
1995-05-04 03:15:53 -07:00
|
|
|
(t1, t2)
|
1996-04-22 04:15:41 -07:00
|
|
|
| _ ->
|
1996-05-20 09:43:29 -07:00
|
|
|
raise (Unify [])
|
1996-04-22 04:15:41 -07:00
|
|
|
|
1997-01-20 09:11:47 -08:00
|
|
|
(* Used by [filter_method]. *)
|
1997-05-11 14:35:00 -07:00
|
|
|
let rec filter_method_field env name priv ty =
|
1996-04-22 04:15:41 -07:00
|
|
|
let ty = repr ty in
|
|
|
|
match ty.desc with
|
|
|
|
Tvar ->
|
1998-06-24 12:22:26 -07:00
|
|
|
let level = ty.level in
|
|
|
|
let ty1 = newvar2 level and ty2 = newvar2 level in
|
|
|
|
let ty' = newty2 level (Tfield (name,
|
|
|
|
begin match priv with
|
|
|
|
Private -> Fvar (ref None)
|
|
|
|
| Public -> Fpresent
|
|
|
|
end,
|
|
|
|
ty1, ty2))
|
1997-05-11 14:35:00 -07:00
|
|
|
in
|
1996-04-22 04:15:41 -07:00
|
|
|
ty.desc <- Tlink ty';
|
|
|
|
ty1
|
1997-05-11 14:35:00 -07:00
|
|
|
| Tfield(n, kind, ty1, ty2) ->
|
|
|
|
let kind = field_kind_repr kind in
|
|
|
|
if (n = name) && (kind <> Fabsent) then begin
|
|
|
|
if priv = Public then
|
|
|
|
unify_kind kind Fpresent;
|
1996-04-22 04:15:41 -07:00
|
|
|
ty1
|
1997-05-11 14:35:00 -07:00
|
|
|
end else
|
|
|
|
filter_method_field env name priv ty2
|
1996-04-22 04:15:41 -07:00
|
|
|
| _ ->
|
1996-05-20 09:43:29 -07:00
|
|
|
raise (Unify [])
|
1996-04-22 04:15:41 -07:00
|
|
|
|
1997-01-20 09:11:47 -08:00
|
|
|
(* Unify [ty] and [< name : 'a; .. >]. Return ['a]. *)
|
1997-05-11 14:35:00 -07:00
|
|
|
let rec filter_method env name priv ty =
|
1997-06-29 06:16:47 -07:00
|
|
|
let ty = expand_head env ty in
|
1996-04-22 04:15:41 -07:00
|
|
|
match ty.desc with
|
|
|
|
Tvar ->
|
1997-01-20 09:11:47 -08:00
|
|
|
let ty1 = newvar () in
|
1996-04-22 04:15:41 -07:00
|
|
|
let ty' = newobj ty1 in
|
1997-01-23 04:46:46 -08:00
|
|
|
update_level env ty.level ty';
|
1996-04-22 04:15:41 -07:00
|
|
|
ty.desc <- Tlink ty';
|
1997-05-11 14:35:00 -07:00
|
|
|
filter_method_field env name priv ty1
|
1996-04-22 04:15:41 -07:00
|
|
|
| Tobject(f, _) ->
|
1997-05-11 14:35:00 -07:00
|
|
|
filter_method_field env name priv f
|
1995-05-04 03:15:53 -07:00
|
|
|
| _ ->
|
1996-05-20 09:43:29 -07:00
|
|
|
raise (Unify [])
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1998-11-12 06:53:46 -08:00
|
|
|
let check_filter_method env name priv ty =
|
1999-02-24 07:21:50 -08:00
|
|
|
ignore(filter_method env name priv ty)
|
1998-11-12 06:53:46 -08:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
let filter_self_method env lab priv meths ty =
|
|
|
|
let ty' = filter_method env lab priv ty in
|
|
|
|
try
|
|
|
|
Meths.find lab !meths
|
|
|
|
with Not_found ->
|
|
|
|
let pair = (Ident.create lab, ty') in
|
|
|
|
meths := Meths.add lab pair !meths;
|
|
|
|
pair
|
|
|
|
|
1997-01-21 05:38:42 -08:00
|
|
|
|
|
|
|
(***********************************)
|
|
|
|
(* Matching between type schemes *)
|
|
|
|
(***********************************)
|
|
|
|
|
1997-03-08 14:05:39 -08:00
|
|
|
(*
|
1999-09-09 08:32:11 -07:00
|
|
|
Update the level of [ty]. First check that the levels of generic
|
|
|
|
variables from the subject are not lowered.
|
1997-03-08 14:05:39 -08:00
|
|
|
*)
|
1997-04-01 12:52:21 -08:00
|
|
|
let moregen_occur env level ty =
|
|
|
|
let rec occur ty =
|
1996-10-25 06:40:04 -07:00
|
|
|
let ty = repr ty in
|
1997-04-01 12:52:21 -08:00
|
|
|
if ty.level > level then begin
|
1999-09-09 08:32:11 -07:00
|
|
|
if ty.desc = Tvar && ty.level >= generic_level - 1 then raise Occur;
|
1997-03-08 14:05:39 -08:00
|
|
|
ty.level <- pivot_level - ty.level;
|
1999-11-30 08:07:38 -08:00
|
|
|
match ty.desc with
|
|
|
|
Tvariant row when static_row row ->
|
|
|
|
iter_row occur row
|
|
|
|
| _ ->
|
|
|
|
iter_type_expr occur ty
|
1997-01-20 09:11:47 -08:00
|
|
|
end
|
1996-04-22 04:15:41 -07:00
|
|
|
in
|
1997-04-01 12:52:21 -08:00
|
|
|
begin try
|
|
|
|
occur ty; unmark_type ty
|
|
|
|
with Occur ->
|
|
|
|
unmark_type ty; raise (Unify [])
|
|
|
|
end;
|
|
|
|
update_level env level ty
|
1995-11-16 05:27:53 -08:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
let rec moregen inst_nongen type_pairs env t1 t2 =
|
|
|
|
if t1 == t2 then () else
|
|
|
|
let t1 = repr t1 in
|
|
|
|
let t2 = repr t2 in
|
|
|
|
if t1 == t2 then () else
|
1997-04-01 12:52:21 -08:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
try
|
1997-04-01 12:52:21 -08:00
|
|
|
match (t1.desc, t2.desc) with
|
|
|
|
(Tvar, _) when if inst_nongen then t1.level <> generic_level - 1
|
|
|
|
else t1.level = generic_level ->
|
1997-03-08 14:05:39 -08:00
|
|
|
moregen_occur env t1.level t2;
|
|
|
|
t1.desc <- Tlink t2
|
1997-04-01 12:52:21 -08:00
|
|
|
| (Tconstr (p1, [], _), Tconstr (p2, [], _)) when Path.same p1 p2 ->
|
1997-03-08 14:05:39 -08:00
|
|
|
()
|
1997-04-01 12:52:21 -08:00
|
|
|
| _ ->
|
|
|
|
let t1' = expand_head env t1 in
|
|
|
|
let t2' = expand_head env t2 in
|
|
|
|
(* Expansion may have changed the representative of the types... *)
|
|
|
|
let t1' = repr t1' and t2' = repr t2' in
|
|
|
|
if t1' == t2' then () else
|
1998-07-03 10:40:39 -07:00
|
|
|
begin try
|
|
|
|
TypePairs.find type_pairs (t1', t2')
|
|
|
|
with Not_found ->
|
|
|
|
TypePairs.add type_pairs (t1', t2') ();
|
1997-04-01 12:52:21 -08:00
|
|
|
match (t1'.desc, t2'.desc) with
|
|
|
|
(Tvar, _) when if inst_nongen then t1'.level <> generic_level - 1
|
|
|
|
else t1'.level = generic_level ->
|
|
|
|
moregen_occur env t1'.level t2;
|
|
|
|
t1'.desc <- Tlink t2
|
1999-11-30 08:07:38 -08:00
|
|
|
| (Tarrow (l1, t1, u1), Tarrow (l2, t2, u2)) when l1 = l2
|
|
|
|
or !Clflags.classic && not (is_optional l1 or is_optional l2) ->
|
1998-06-24 12:22:26 -07:00
|
|
|
moregen inst_nongen type_pairs env t1 t2;
|
|
|
|
moregen inst_nongen type_pairs env u1 u2
|
1997-04-01 12:52:21 -08:00
|
|
|
| (Ttuple tl1, Ttuple tl2) ->
|
1998-06-24 12:22:26 -07:00
|
|
|
moregen_list inst_nongen type_pairs env tl1 tl2
|
1997-04-01 12:52:21 -08:00
|
|
|
| (Tconstr (p1, tl1, _), Tconstr (p2, tl2, _))
|
|
|
|
when Path.same p1 p2 ->
|
1998-06-24 12:22:26 -07:00
|
|
|
moregen_list inst_nongen type_pairs env tl1 tl2
|
1999-11-30 08:07:38 -08:00
|
|
|
| (Tvariant row1, Tvariant row2) ->
|
|
|
|
moregen_row inst_nongen type_pairs env row1 row2
|
1997-04-01 12:52:21 -08:00
|
|
|
| (Tobject (fi1, nm1), Tobject (fi2, nm2)) ->
|
1998-06-24 12:22:26 -07:00
|
|
|
moregen_fields inst_nongen type_pairs env fi1 fi2
|
1997-04-01 12:52:21 -08:00
|
|
|
| (Tfield _, Tfield _) -> (* Actually unused *)
|
1998-06-24 12:22:26 -07:00
|
|
|
moregen_fields inst_nongen type_pairs env t1' t2'
|
|
|
|
| (Tfield (_, kind, _, t1''), _)
|
|
|
|
when field_kind_repr kind = Fabsent ->
|
|
|
|
moregen inst_nongen type_pairs env t1'' t2'
|
|
|
|
| (_, Tfield (_, kind, _, t2''))
|
|
|
|
when field_kind_repr kind = Fabsent ->
|
|
|
|
moregen inst_nongen type_pairs env t1' t2''
|
1997-04-01 12:52:21 -08:00
|
|
|
| (Tnil, Tnil) ->
|
|
|
|
()
|
|
|
|
| (_, _) ->
|
|
|
|
raise (Unify [])
|
|
|
|
end
|
1998-06-24 12:22:26 -07:00
|
|
|
with Unify trace ->
|
|
|
|
raise (Unify ((t1, t2)::trace))
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
and moregen_list inst_nongen type_pairs env tl1 tl2 =
|
|
|
|
if List.length tl1 <> List.length tl2 then
|
|
|
|
raise (Unify []);
|
|
|
|
List.iter2 (moregen inst_nongen type_pairs env) tl1 tl2
|
1997-05-11 14:35:00 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
and moregen_fields inst_nongen type_pairs env ty1 ty2 =
|
|
|
|
let (fields1, rest1) = flatten_fields ty1
|
|
|
|
and (fields2, rest2) = flatten_fields ty2 in
|
|
|
|
let (pairs, miss1, miss2) = associate_fields fields1 fields2 in
|
|
|
|
if miss1 <> [] then raise (Unify []);
|
|
|
|
moregen inst_nongen type_pairs env rest1
|
1998-10-10 10:57:27 -07:00
|
|
|
(build_fields (repr ty2).level miss2 rest2);
|
1998-06-24 12:22:26 -07:00
|
|
|
List.iter
|
|
|
|
(fun (n, k1, t1, k2, t2) ->
|
|
|
|
moregen_kind k1 k2;
|
|
|
|
try moregen inst_nongen type_pairs env t1 t2 with Unify trace ->
|
|
|
|
raise (Unify ((newty (Tfield(n, k1, t1, rest2)),
|
|
|
|
newty (Tfield(n, k2, t2, rest2)))::trace)))
|
|
|
|
pairs
|
|
|
|
|
|
|
|
and moregen_kind k1 k2 =
|
|
|
|
let k1 = field_kind_repr k1 in
|
|
|
|
let k2 = field_kind_repr k2 in
|
|
|
|
match k1, k2 with
|
|
|
|
(Fvar r, (Fvar _ | Fpresent)) -> r := Some k2
|
|
|
|
| (Fpresent, Fpresent) -> ()
|
|
|
|
| _ -> raise (Unify [])
|
1996-04-22 04:15:41 -07:00
|
|
|
|
1999-11-30 08:07:38 -08:00
|
|
|
and moregen_row inst_nongen type_pairs env row1 row2 =
|
|
|
|
let row1 = row_repr row1 and row2 = row_repr row2 in
|
|
|
|
let r1, r2, pairs = merge_row_fields row1.row_fields row2.row_fields in
|
|
|
|
let r1, r2 =
|
|
|
|
if row2.row_closed then
|
|
|
|
filter_row_fields true r1, filter_row_fields false r2
|
|
|
|
else r1, r2
|
|
|
|
in
|
|
|
|
if r1 <> [] || row1.row_closed && (not row2.row_closed || r2 <> [])
|
|
|
|
then raise (Unify []);
|
|
|
|
let rm1 = repr row1.row_more and rm2 = repr row2.row_more in
|
|
|
|
let ext =
|
|
|
|
if not (static_row row2) then moregen_occur env rm1.level rm2;
|
|
|
|
if r2 = [] then rm2 else
|
|
|
|
let ty = newty2 generic_level (Tvariant{row2 with row_fields = r2}) in
|
|
|
|
moregen_occur env rm1.level ty;
|
|
|
|
ty
|
|
|
|
in
|
|
|
|
if ext != rm1 then rm1.desc <- Tlink ext;
|
|
|
|
List.iter
|
|
|
|
(fun (l,f1,f2) ->
|
|
|
|
let f1 = row_field_repr f1 and f2 = row_field_repr f2 in
|
|
|
|
if f1 == f2 then () else
|
|
|
|
match f1, f2 with
|
|
|
|
Rpresent(Some t1), Rpresent(Some t2) ->
|
|
|
|
moregen inst_nongen type_pairs env t1 t2
|
|
|
|
| Rpresent None, Rpresent None -> ()
|
|
|
|
| Reither(false, tl1, e1), Rpresent(Some t2) ->
|
|
|
|
e1 := Some f2;
|
|
|
|
List.iter (fun t1 -> moregen inst_nongen type_pairs env t1 t2) tl1
|
|
|
|
| Reither(c1, tl1, e1), Reither(c2, tl2, e2) ->
|
|
|
|
if c1 && not c2 then raise(Unify []);
|
|
|
|
e1 := Some f2;
|
|
|
|
begin match tl2 with
|
|
|
|
[t2] when tl1 <> [] -> List.iter
|
|
|
|
(fun t1 -> moregen inst_nongen type_pairs env t1 t2) tl1
|
|
|
|
| _ ->
|
|
|
|
if List.length tl1 <> List.length tl2 then raise (Unify []);
|
|
|
|
List.iter2 (moregen inst_nongen type_pairs env) tl1 tl2
|
|
|
|
end
|
|
|
|
| Reither(true, [], e1), Rpresent None -> e1 := Some f2
|
|
|
|
| Reither(_, _, e1), Rabsent -> e1 := Some f2
|
|
|
|
| Rabsent, Rabsent -> ()
|
|
|
|
| _ -> raise (Unify []))
|
|
|
|
pairs
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
(*
|
|
|
|
Non-generic variable can be instanciated only if [inst_nongen] is
|
|
|
|
true. So, [inst_nongen] should be set to false if the subject might
|
|
|
|
contain non-generic variables (and we do not want them to be
|
|
|
|
instanciated).
|
|
|
|
Usually, the subject is given by the user, and the pattern
|
|
|
|
is unimportant. So, no need to propagate abbreviations.
|
|
|
|
*)
|
|
|
|
let moregeneral env inst_nongen pat_sch subj_sch =
|
1997-03-24 12:12:00 -08:00
|
|
|
let old_level = !current_level in
|
|
|
|
current_level := generic_level - 1;
|
1997-04-01 12:52:21 -08:00
|
|
|
(*
|
|
|
|
Generic variables are first duplicated with [instance]. So,
|
|
|
|
their levels are lowered to [generic_level - 1]. The subject is
|
1998-06-24 12:22:26 -07:00
|
|
|
then copied with [duplicate_type]. That way, its levels won't be
|
1997-04-01 12:52:21 -08:00
|
|
|
changed.
|
|
|
|
*)
|
1998-06-24 12:22:26 -07:00
|
|
|
let subj = duplicate_type (instance subj_sch) in
|
1997-03-24 12:12:00 -08:00
|
|
|
current_level := generic_level;
|
1997-04-01 12:52:21 -08:00
|
|
|
(* Duplicate generic variables *)
|
|
|
|
let patt = instance pat_sch in
|
1998-06-24 12:22:26 -07:00
|
|
|
let res =
|
1998-07-03 10:40:39 -07:00
|
|
|
try moregen inst_nongen (TypePairs.create 13) env patt subj; true with
|
1998-06-24 12:22:26 -07:00
|
|
|
Unify _ -> false
|
|
|
|
in
|
1997-04-01 12:52:21 -08:00
|
|
|
current_level := old_level;
|
|
|
|
res
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1997-01-21 05:38:42 -08:00
|
|
|
|
|
|
|
(*********************************************)
|
|
|
|
(* Equivalence between parameterized types *)
|
|
|
|
(*********************************************)
|
|
|
|
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
let rec eqtype rename type_pairs subst env t1 t2 =
|
|
|
|
if t1 == t2 then () else
|
|
|
|
let t1 = repr t1 in
|
|
|
|
let t2 = repr t2 in
|
|
|
|
if t1 == t2 then () else
|
1997-03-09 08:52:49 -08:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
try
|
1997-04-12 05:39:20 -07:00
|
|
|
match (t1.desc, t2.desc) with
|
1998-06-24 12:22:26 -07:00
|
|
|
(Tvar, Tvar) when rename ->
|
1997-04-12 05:39:20 -07:00
|
|
|
begin try
|
1998-06-24 12:22:26 -07:00
|
|
|
if List.assq t1 !subst != t2 then raise (Unify [])
|
1997-04-12 05:39:20 -07:00
|
|
|
with Not_found ->
|
1998-06-24 12:22:26 -07:00
|
|
|
subst := (t1, t2) :: !subst
|
1997-04-12 05:39:20 -07:00
|
|
|
end
|
|
|
|
| (Tconstr (p1, [], _), Tconstr (p2, [], _)) when Path.same p1 p2 ->
|
1998-06-24 12:22:26 -07:00
|
|
|
()
|
1997-04-12 05:39:20 -07:00
|
|
|
| _ ->
|
1998-06-24 12:22:26 -07:00
|
|
|
let t1' = expand_head env t1 in
|
|
|
|
let t2' = expand_head env t2 in
|
1997-04-12 05:39:20 -07:00
|
|
|
(* Expansion may have changed the representative of the types... *)
|
1998-06-24 12:22:26 -07:00
|
|
|
let t1' = repr t1' and t2' = repr t2' in
|
|
|
|
if t1' == t2' then () else
|
1998-07-03 10:40:39 -07:00
|
|
|
begin try
|
|
|
|
TypePairs.find type_pairs (t1', t2')
|
|
|
|
with Not_found ->
|
|
|
|
TypePairs.add type_pairs (t1', t2') ();
|
1998-06-24 12:22:26 -07:00
|
|
|
match (t1'.desc, t2'.desc) with
|
1997-04-12 05:39:20 -07:00
|
|
|
(Tvar, Tvar) when rename ->
|
|
|
|
begin try
|
1998-06-24 12:22:26 -07:00
|
|
|
if List.assq t1' !subst == t2' then raise (Unify [])
|
1997-04-12 05:39:20 -07:00
|
|
|
with Not_found ->
|
1998-06-24 12:22:26 -07:00
|
|
|
subst := (t1', t2') :: !subst
|
1997-04-12 05:39:20 -07:00
|
|
|
end
|
1999-11-30 08:07:38 -08:00
|
|
|
| (Tarrow (l1, t1, u1), Tarrow (l2, t2, u2)) when l1 = l2
|
|
|
|
or !Clflags.classic && not (is_optional l1 or is_optional l2) ->
|
1998-06-24 12:22:26 -07:00
|
|
|
eqtype rename type_pairs subst env t1 t2;
|
|
|
|
eqtype rename type_pairs subst env u1 u2;
|
1997-04-12 05:39:20 -07:00
|
|
|
| (Ttuple tl1, Ttuple tl2) ->
|
1998-06-24 12:22:26 -07:00
|
|
|
eqtype_list rename type_pairs subst env tl1 tl2
|
1997-04-12 05:39:20 -07:00
|
|
|
| (Tconstr (p1, tl1, _), Tconstr (p2, tl2, _))
|
|
|
|
when Path.same p1 p2 ->
|
1998-06-24 12:22:26 -07:00
|
|
|
eqtype_list rename type_pairs subst env tl1 tl2
|
1999-11-30 08:07:38 -08:00
|
|
|
| (Tvariant row1, Tvariant row2) ->
|
|
|
|
eqtype_row rename type_pairs subst env row1 row2
|
1997-04-12 05:39:20 -07:00
|
|
|
| (Tobject (fi1, nm1), Tobject (fi2, nm2)) ->
|
1998-06-24 12:22:26 -07:00
|
|
|
eqtype_fields rename type_pairs subst env fi1 fi2
|
1997-04-12 05:39:20 -07:00
|
|
|
| (Tfield _, Tfield _) -> (* Actually unused *)
|
1998-06-24 12:22:26 -07:00
|
|
|
eqtype_fields rename type_pairs subst env t1' t2'
|
|
|
|
| (Tfield (_, kind, _, t1''), _)
|
1997-05-11 14:35:00 -07:00
|
|
|
when field_kind_repr kind = Fabsent ->
|
1998-06-24 12:22:26 -07:00
|
|
|
eqtype rename type_pairs subst env t1'' t2'
|
|
|
|
| (_, Tfield (_, kind, _, t2''))
|
1997-05-11 14:35:00 -07:00
|
|
|
when field_kind_repr kind = Fabsent ->
|
1998-06-24 12:22:26 -07:00
|
|
|
eqtype rename type_pairs subst env t1' t2''
|
1997-04-12 05:39:20 -07:00
|
|
|
| (Tnil, Tnil) ->
|
1998-06-24 12:22:26 -07:00
|
|
|
()
|
1997-04-12 05:39:20 -07:00
|
|
|
| (_, _) ->
|
1998-06-24 12:22:26 -07:00
|
|
|
raise (Unify [])
|
1997-04-12 05:39:20 -07:00
|
|
|
end
|
1998-06-24 12:22:26 -07:00
|
|
|
with Unify trace ->
|
|
|
|
raise (Unify ((t1, t2)::trace))
|
1997-03-09 08:52:49 -08:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
and eqtype_list rename type_pairs subst env tl1 tl2 =
|
|
|
|
if List.length tl1 <> List.length tl2 then
|
|
|
|
raise (Unify []);
|
|
|
|
List.iter2 (eqtype rename type_pairs subst env) tl1 tl2
|
1997-03-09 08:52:49 -08:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
and eqtype_fields rename type_pairs subst env ty1 ty2 =
|
|
|
|
let (fields1, rest1) = flatten_fields ty1
|
|
|
|
and (fields2, rest2) = flatten_fields ty2 in
|
|
|
|
let (pairs, miss1, miss2) = associate_fields fields1 fields2 in
|
|
|
|
eqtype rename type_pairs subst env rest1 rest2;
|
|
|
|
if (miss1 <> []) || (miss2 <> []) then raise (Unify []);
|
|
|
|
List.iter
|
|
|
|
(function (n, k1, t1, k2, t2) ->
|
|
|
|
eqtype_kind k1 k2;
|
|
|
|
try eqtype rename type_pairs subst env t1 t2 with Unify trace ->
|
|
|
|
raise (Unify ((newty (Tfield(n, k1, t1, rest2)),
|
|
|
|
newty (Tfield(n, k2, t2, rest2)))::trace)))
|
|
|
|
pairs
|
|
|
|
|
|
|
|
and eqtype_kind k1 k2 =
|
|
|
|
let k1 = field_kind_repr k1 in
|
|
|
|
let k2 = field_kind_repr k2 in
|
|
|
|
match k1, k2 with
|
|
|
|
(Fvar _, Fvar _)
|
|
|
|
| (Fpresent, Fpresent) -> ()
|
|
|
|
| _ -> raise (Unify [])
|
|
|
|
|
1999-11-30 08:07:38 -08:00
|
|
|
and eqtype_row rename type_pairs subst env row1 row2 =
|
|
|
|
let row1 = row_repr row1 and row2 = row_repr row2 in
|
|
|
|
let r1, r2, pairs = merge_row_fields row1.row_fields row2.row_fields in
|
|
|
|
if row1.row_closed <> row2.row_closed
|
|
|
|
|| not row1.row_closed && (r1 <> [] || r2 <> [])
|
|
|
|
|| filter_row_fields false (r1 @ r2) <> []
|
|
|
|
then raise (Unify []);
|
|
|
|
eqtype rename type_pairs subst env row1.row_more row2.row_more;
|
|
|
|
List.iter
|
|
|
|
(fun (_,f1,f2) ->
|
|
|
|
match row_field_repr f1, row_field_repr f2 with
|
|
|
|
Rpresent(Some t1), Rpresent(Some t2) ->
|
|
|
|
eqtype rename type_pairs subst env t1 t2
|
|
|
|
| Reither(c1, tl1,_), Reither(c2, tl2,_)
|
|
|
|
when c1 = c2 && List.length tl1 = List.length tl2 ->
|
|
|
|
List.iter2 (eqtype rename type_pairs subst env) tl1 tl2
|
|
|
|
| Rpresent None, Rpresent None -> ()
|
|
|
|
| Rabsent, Rabsent -> ()
|
|
|
|
| _ -> raise (Unify []))
|
|
|
|
pairs
|
|
|
|
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
(* Two modes: with or without renaming of variables *)
|
|
|
|
let equal env rename tyl1 tyl2 =
|
1998-07-03 10:40:39 -07:00
|
|
|
try
|
|
|
|
eqtype_list rename (TypePairs.create 11) (ref []) env tyl1 tyl2; true
|
|
|
|
with
|
1998-06-24 12:22:26 -07:00
|
|
|
Unify _ -> false
|
|
|
|
|
|
|
|
|
|
|
|
(*************************)
|
|
|
|
(* Class type matching *)
|
|
|
|
(*************************)
|
|
|
|
|
|
|
|
|
|
|
|
type class_match_failure =
|
|
|
|
CM_Virtual_class
|
|
|
|
| CM_Parameter_arity_mismatch of int * int
|
|
|
|
| CM_Type_parameter_mismatch of (type_expr * type_expr) list
|
|
|
|
| CM_Class_type_mismatch of class_type * class_type
|
|
|
|
| CM_Parameter_mismatch of (type_expr * type_expr) list
|
|
|
|
| CM_Val_type_mismatch of string * (type_expr * type_expr) list
|
|
|
|
| CM_Meth_type_mismatch of string * (type_expr * type_expr) list
|
|
|
|
| CM_Non_mutable_value of string
|
|
|
|
| CM_Missing_value of string
|
|
|
|
| CM_Missing_method of string
|
|
|
|
| CM_Hide_public of string
|
|
|
|
| CM_Hide_virtual of string
|
|
|
|
| CM_Public_method of string
|
|
|
|
| CM_Private_method of string
|
|
|
|
| CM_Virtual_method of string
|
|
|
|
|
|
|
|
exception Failure of class_match_failure list
|
|
|
|
|
|
|
|
let rec moregen_clty trace type_pairs env cty1 cty2 =
|
|
|
|
try
|
|
|
|
match cty1, cty2 with
|
|
|
|
Tcty_constr (_, _, cty1), _ ->
|
|
|
|
moregen_clty true type_pairs env cty1 cty2
|
|
|
|
| _, Tcty_constr (_, _, cty2) ->
|
|
|
|
moregen_clty true type_pairs env cty1 cty2
|
1999-11-30 08:07:38 -08:00
|
|
|
| Tcty_fun (l1, ty1, cty1'), Tcty_fun (l2, ty2, cty2') when l1 = l2 ->
|
1998-06-24 12:22:26 -07:00
|
|
|
begin try moregen true type_pairs env ty1 ty2 with Unify trace ->
|
|
|
|
raise (Failure [CM_Parameter_mismatch (expand_trace env trace)])
|
|
|
|
end;
|
|
|
|
moregen_clty false type_pairs env cty1' cty2'
|
|
|
|
| Tcty_signature sign1, Tcty_signature sign2 ->
|
|
|
|
let ty1 = object_fields (repr sign1.cty_self) in
|
|
|
|
let ty2 = object_fields (repr sign2.cty_self) in
|
|
|
|
let (fields1, rest1) = flatten_fields ty1
|
|
|
|
and (fields2, rest2) = flatten_fields ty2 in
|
|
|
|
let (pairs, miss1, miss2) = associate_fields fields1 fields2 in
|
|
|
|
List.iter
|
|
|
|
(fun (lab, k1, t1, k2, t2) ->
|
|
|
|
begin try moregen true type_pairs env t1 t2 with Unify trace ->
|
|
|
|
raise (Failure [CM_Meth_type_mismatch
|
|
|
|
(lab, expand_trace env trace)])
|
|
|
|
end)
|
|
|
|
pairs;
|
|
|
|
Vars.iter
|
|
|
|
(fun lab (mut, ty) ->
|
|
|
|
let (mut', ty') = Vars.find lab sign1.cty_vars in
|
1999-09-09 08:32:11 -07:00
|
|
|
try moregen true type_pairs env ty' ty with Unify trace ->
|
1998-06-24 12:22:26 -07:00
|
|
|
raise (Failure [CM_Val_type_mismatch
|
|
|
|
(lab, expand_trace env trace)]))
|
|
|
|
sign2.cty_vars
|
|
|
|
| _ ->
|
|
|
|
raise (Failure [])
|
|
|
|
with
|
|
|
|
Failure error when trace ->
|
|
|
|
raise (Failure (CM_Class_type_mismatch (cty1, cty2)::error))
|
|
|
|
|
|
|
|
let match_class_types env pat_sch subj_sch =
|
1998-07-03 10:40:39 -07:00
|
|
|
let type_pairs = TypePairs.create 53 in
|
1998-06-24 12:22:26 -07:00
|
|
|
let old_level = !current_level in
|
|
|
|
current_level := generic_level - 1;
|
|
|
|
(*
|
|
|
|
Generic variables are first duplicated with [instance]. So,
|
|
|
|
their levels are lowered to [generic_level - 1]. The subject is
|
|
|
|
then copied with [duplicate_type]. That way, its levels won't be
|
|
|
|
changed.
|
|
|
|
*)
|
|
|
|
let (_, subj_inst) = instance_class [] subj_sch in
|
|
|
|
let subj = duplicate_class_type subj_inst in
|
|
|
|
current_level := generic_level;
|
|
|
|
(* Duplicate generic variables *)
|
|
|
|
let (_, patt) = instance_class [] pat_sch in
|
|
|
|
let res =
|
|
|
|
let sign1 = signature_of_class_type patt in
|
|
|
|
let sign2 = signature_of_class_type subj in
|
|
|
|
let t1 = repr sign1.cty_self in
|
|
|
|
let t2 = repr sign2.cty_self in
|
1998-07-03 10:40:39 -07:00
|
|
|
TypePairs.add type_pairs (t1, t2) ();
|
1998-06-24 12:22:26 -07:00
|
|
|
let (fields1, rest1) = flatten_fields (object_fields t1)
|
|
|
|
and (fields2, rest2) = flatten_fields (object_fields t2) in
|
1997-02-20 12:39:02 -08:00
|
|
|
let (pairs, miss1, miss2) = associate_fields fields1 fields2 in
|
1998-06-24 12:22:26 -07:00
|
|
|
let error =
|
|
|
|
List.fold_right
|
|
|
|
(fun (lab, k, _) err ->
|
|
|
|
let err =
|
|
|
|
let k = field_kind_repr k in
|
|
|
|
begin match k with
|
|
|
|
Fvar r -> r := Some Fabsent; err
|
|
|
|
| _ -> CM_Hide_public lab::err
|
|
|
|
end
|
|
|
|
in
|
|
|
|
if Concr.mem lab sign1.cty_concr then err
|
|
|
|
else CM_Hide_virtual lab::err)
|
|
|
|
miss1 []
|
|
|
|
in
|
|
|
|
let missing_method = List.map (fun (m, _, _) -> m) miss2 in
|
|
|
|
let error =
|
|
|
|
(List.map (fun m -> CM_Missing_method m) missing_method) @ error
|
|
|
|
in
|
|
|
|
(* Always succeeds *)
|
|
|
|
moregen true type_pairs env rest1 rest2;
|
|
|
|
let error =
|
|
|
|
List.fold_right
|
|
|
|
(fun (lab, k1, t1, k2, t2) err ->
|
|
|
|
try moregen_kind k1 k2; err with
|
|
|
|
Unify _ -> CM_Public_method lab::err)
|
|
|
|
pairs error
|
|
|
|
in
|
|
|
|
let error =
|
|
|
|
Vars.fold
|
|
|
|
(fun lab (mut, ty) err ->
|
|
|
|
try
|
|
|
|
let (mut', ty') = Vars.find lab sign1.cty_vars in
|
|
|
|
if mut = Mutable && mut' <> Mutable then
|
|
|
|
CM_Non_mutable_value lab::err
|
|
|
|
else
|
|
|
|
err
|
|
|
|
with Not_found ->
|
|
|
|
CM_Missing_value lab::err)
|
|
|
|
sign2.cty_vars error
|
|
|
|
in
|
|
|
|
let error =
|
|
|
|
List.fold_right
|
|
|
|
(fun e l ->
|
|
|
|
if List.mem e missing_method then l else CM_Virtual_method e::l)
|
|
|
|
(Concr.elements (Concr.diff sign2.cty_concr sign1.cty_concr))
|
|
|
|
error
|
|
|
|
in
|
|
|
|
match error with
|
|
|
|
[] ->
|
|
|
|
begin try
|
|
|
|
moregen_clty true type_pairs env patt subj;
|
|
|
|
[]
|
|
|
|
with
|
|
|
|
Failure r -> r
|
|
|
|
end
|
|
|
|
| error ->
|
|
|
|
CM_Class_type_mismatch (patt, subj)::error
|
|
|
|
in
|
|
|
|
current_level := old_level;
|
|
|
|
res
|
|
|
|
|
|
|
|
let rec equal_clty trace type_pairs subst env cty1 cty2 =
|
|
|
|
try
|
|
|
|
match cty1, cty2 with
|
|
|
|
Tcty_constr (_, _, cty1), Tcty_constr (_, _, cty2) ->
|
|
|
|
equal_clty true type_pairs subst env cty1 cty2
|
|
|
|
| Tcty_constr (_, _, cty1), _ ->
|
|
|
|
equal_clty true type_pairs subst env cty1 cty2
|
|
|
|
| _, Tcty_constr (_, _, cty2) ->
|
|
|
|
equal_clty true type_pairs subst env cty1 cty2
|
1999-11-30 08:07:38 -08:00
|
|
|
| Tcty_fun (l1, ty1, cty1'), Tcty_fun (l2, ty2, cty2') when l1 = l2 ->
|
1998-06-24 12:22:26 -07:00
|
|
|
begin try eqtype true type_pairs subst env ty1 ty2 with Unify trace ->
|
|
|
|
raise (Failure [CM_Parameter_mismatch (expand_trace env trace)])
|
|
|
|
end;
|
|
|
|
equal_clty false type_pairs subst env cty1' cty2'
|
|
|
|
| Tcty_signature sign1, Tcty_signature sign2 ->
|
|
|
|
let ty1 = object_fields (repr sign1.cty_self) in
|
|
|
|
let ty2 = object_fields (repr sign2.cty_self) in
|
|
|
|
let (fields1, rest1) = flatten_fields ty1
|
|
|
|
and (fields2, rest2) = flatten_fields ty2 in
|
|
|
|
let (pairs, miss1, miss2) = associate_fields fields1 fields2 in
|
|
|
|
List.iter
|
|
|
|
(fun (lab, k1, t1, k2, t2) ->
|
|
|
|
begin try eqtype true type_pairs subst env t1 t2 with
|
|
|
|
Unify trace ->
|
|
|
|
raise (Failure [CM_Meth_type_mismatch
|
|
|
|
(lab, expand_trace env trace)])
|
|
|
|
end)
|
|
|
|
pairs;
|
|
|
|
Vars.iter
|
|
|
|
(fun lab (mut, ty) ->
|
|
|
|
let (mut', ty') = Vars.find lab sign1.cty_vars in
|
|
|
|
try eqtype true type_pairs subst env ty ty' with Unify trace ->
|
|
|
|
raise (Failure [CM_Val_type_mismatch
|
|
|
|
(lab, expand_trace env trace)]))
|
|
|
|
sign2.cty_vars
|
|
|
|
| _ ->
|
1999-11-03 10:32:21 -08:00
|
|
|
raise
|
|
|
|
(Failure (if trace then []
|
|
|
|
else [CM_Class_type_mismatch (cty1, cty2)]))
|
1998-06-24 12:22:26 -07:00
|
|
|
with
|
|
|
|
Failure error when trace ->
|
|
|
|
raise (Failure (CM_Class_type_mismatch (cty1, cty2)::error))
|
1997-04-12 05:39:20 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
(* XXX On pourrait autoriser l'instantiation du type des parametres... *)
|
1998-10-10 10:57:27 -07:00
|
|
|
(* XXX Correct ? (variables de type dans parametres et corps de classe *)
|
1998-06-24 12:22:26 -07:00
|
|
|
let match_class_declarations env patt_params patt_type subj_params subj_type =
|
1998-07-03 10:40:39 -07:00
|
|
|
let type_pairs = TypePairs.create 53 in
|
1998-06-24 12:22:26 -07:00
|
|
|
let subst = ref [] in
|
|
|
|
let sign1 = signature_of_class_type patt_type in
|
|
|
|
let sign2 = signature_of_class_type subj_type in
|
|
|
|
let t1 = repr sign1.cty_self in
|
|
|
|
let t2 = repr sign2.cty_self in
|
1998-07-03 10:40:39 -07:00
|
|
|
TypePairs.add type_pairs (t1, t2) ();
|
1998-06-24 12:22:26 -07:00
|
|
|
let (fields1, rest1) = flatten_fields (object_fields t1)
|
|
|
|
and (fields2, rest2) = flatten_fields (object_fields t2) in
|
|
|
|
let (pairs, miss1, miss2) = associate_fields fields1 fields2 in
|
|
|
|
let error =
|
|
|
|
List.fold_right
|
|
|
|
(fun (lab, k, _) err ->
|
|
|
|
let err =
|
|
|
|
let k = field_kind_repr k in
|
|
|
|
begin match k with
|
|
|
|
Fvar r -> err
|
|
|
|
| _ -> CM_Hide_public lab::err
|
|
|
|
end
|
|
|
|
in
|
|
|
|
if Concr.mem lab sign1.cty_concr then err
|
|
|
|
else CM_Hide_virtual lab::err)
|
|
|
|
miss1 []
|
|
|
|
in
|
|
|
|
let missing_method = List.map (fun (m, _, _) -> m) miss2 in
|
|
|
|
let error =
|
|
|
|
(List.map (fun m -> CM_Missing_method m) missing_method) @ error
|
|
|
|
in
|
|
|
|
(* Always succeeds *)
|
|
|
|
eqtype true type_pairs subst env rest1 rest2;
|
|
|
|
let error =
|
|
|
|
List.fold_right
|
|
|
|
(fun (lab, k1, t1, k2, t2) err ->
|
|
|
|
let k1 = field_kind_repr k1 in
|
|
|
|
let k2 = field_kind_repr k2 in
|
|
|
|
match k1, k2 with
|
|
|
|
(Fvar _, Fvar _)
|
|
|
|
| (Fpresent, Fpresent) -> err
|
|
|
|
| (Fvar _, Fpresent) -> CM_Private_method lab::err
|
1998-11-30 05:06:53 -08:00
|
|
|
| (Fpresent, Fvar _) -> CM_Public_method lab::err
|
1998-06-24 12:22:26 -07:00
|
|
|
| _ -> assert false)
|
|
|
|
pairs error
|
|
|
|
in
|
|
|
|
let error =
|
|
|
|
Vars.fold
|
|
|
|
(fun lab (mut, ty) err ->
|
|
|
|
try
|
|
|
|
let (mut', ty') = Vars.find lab sign1.cty_vars in
|
|
|
|
if mut = Mutable && mut' <> Mutable then
|
|
|
|
CM_Non_mutable_value lab::err
|
|
|
|
else
|
|
|
|
err
|
|
|
|
with Not_found ->
|
|
|
|
CM_Missing_value lab::err)
|
|
|
|
sign2.cty_vars error
|
1995-05-04 03:15:53 -07:00
|
|
|
in
|
1998-06-24 12:22:26 -07:00
|
|
|
let error =
|
|
|
|
List.fold_right
|
|
|
|
(fun e l ->
|
|
|
|
if List.mem e missing_method then l else CM_Virtual_method e::l)
|
|
|
|
(Concr.elements (Concr.diff sign2.cty_concr sign1.cty_concr))
|
|
|
|
error
|
|
|
|
in
|
|
|
|
match error with
|
|
|
|
[] ->
|
|
|
|
begin try
|
|
|
|
let lp = List.length patt_params in
|
|
|
|
let ls = List.length subj_params in
|
|
|
|
if lp <> ls then
|
|
|
|
raise (Failure [CM_Parameter_arity_mismatch (lp, ls)]);
|
|
|
|
List.iter2 (fun p s ->
|
|
|
|
try eqtype true type_pairs subst env p s with Unify trace ->
|
|
|
|
raise (Failure [CM_Type_parameter_mismatch
|
|
|
|
(expand_trace env trace)]))
|
|
|
|
patt_params subj_params;
|
|
|
|
equal_clty false type_pairs subst env patt_type subj_type;
|
|
|
|
[]
|
|
|
|
with
|
|
|
|
Failure r -> r
|
|
|
|
end
|
|
|
|
| error ->
|
|
|
|
error
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1997-01-21 05:38:42 -08:00
|
|
|
|
1997-01-20 09:11:47 -08:00
|
|
|
(***************)
|
|
|
|
(* Subtyping *)
|
|
|
|
(***************)
|
|
|
|
|
|
|
|
|
1997-01-21 05:38:42 -08:00
|
|
|
(**** Build a subtype of a given type. ****)
|
1996-04-22 04:15:41 -07:00
|
|
|
|
1996-05-28 07:30:52 -07:00
|
|
|
let subtypes = ref []
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1999-11-17 15:53:19 -08:00
|
|
|
let rec build_subtype env visited t =
|
1997-01-20 09:11:47 -08:00
|
|
|
let t = repr t in
|
1996-04-22 04:15:41 -07:00
|
|
|
match t.desc with
|
1997-01-20 09:11:47 -08:00
|
|
|
Tlink t' -> (* Redundant ! *)
|
1999-11-17 15:53:19 -08:00
|
|
|
build_subtype env visited t'
|
1996-04-22 04:15:41 -07:00
|
|
|
| Tvar ->
|
|
|
|
(t, false)
|
1999-11-30 08:07:38 -08:00
|
|
|
| Tarrow(l, t1, t2) ->
|
1999-11-17 15:53:19 -08:00
|
|
|
if List.memq t visited then (t, false) else
|
1996-10-27 01:43:07 -08:00
|
|
|
let (t1', c1) = (t1, false) in
|
1999-11-17 15:53:19 -08:00
|
|
|
let (t2', c2) = build_subtype env (t::visited) t2 in
|
1999-11-30 08:07:38 -08:00
|
|
|
if c1 or c2 then (newty (Tarrow(l, t1', t2')), true)
|
1996-04-22 04:15:41 -07:00
|
|
|
else (t, false)
|
|
|
|
| Ttuple tlist ->
|
1999-11-17 15:53:19 -08:00
|
|
|
if List.memq t visited then (t, false) else
|
1996-04-22 04:15:41 -07:00
|
|
|
let (tlist', clist) =
|
1999-11-17 15:53:19 -08:00
|
|
|
List.split (List.map (build_subtype env (t::visited)) tlist)
|
1996-04-22 04:15:41 -07:00
|
|
|
in
|
|
|
|
if List.exists (function c -> c) clist then
|
1998-06-24 12:22:26 -07:00
|
|
|
(newty (Ttuple tlist'), true)
|
1996-04-22 04:15:41 -07:00
|
|
|
else (t, false)
|
1997-01-23 04:46:46 -08:00
|
|
|
| Tconstr(p, tl, abbrev) when generic_abbrev env p ->
|
1998-06-24 12:22:26 -07:00
|
|
|
let t' = expand_abbrev env t in
|
1999-11-17 15:53:19 -08:00
|
|
|
let (t'', c) = build_subtype env visited t' in
|
1997-01-23 04:46:46 -08:00
|
|
|
if c then (t'', true)
|
|
|
|
else (t, false)
|
1996-04-22 04:15:41 -07:00
|
|
|
| Tconstr(p, tl, abbrev) ->
|
1997-01-23 04:46:46 -08:00
|
|
|
(t, false)
|
1999-11-30 08:07:38 -08:00
|
|
|
| Tvariant row ->
|
|
|
|
let row = row_repr row in
|
|
|
|
if not (static_row row) then (t, false) else
|
|
|
|
let bound = ref row.row_bound in
|
|
|
|
let fields =
|
|
|
|
List.map
|
|
|
|
(fun (l,f) -> l, match row_field_repr f with
|
|
|
|
Rpresent None ->
|
|
|
|
Reither(true, [], ref None)
|
|
|
|
| Rpresent(Some t) ->
|
|
|
|
bound := t :: !bound;
|
|
|
|
Reither(false, [t], ref None)
|
|
|
|
| _ -> assert false)
|
|
|
|
(filter_row_fields false row.row_fields)
|
|
|
|
in
|
|
|
|
if fields = [] then (t, false) else
|
|
|
|
let row =
|
|
|
|
{row with row_fields = fields; row_more = newvar(); row_bound = !bound}
|
|
|
|
in
|
|
|
|
(newty (Tvariant row), true)
|
1997-01-23 04:46:46 -08:00
|
|
|
| Tobject (t1, _) when opened_object t1 ->
|
|
|
|
(t, false)
|
1996-04-22 04:15:41 -07:00
|
|
|
| Tobject (t1, _) ->
|
1997-01-23 04:46:46 -08:00
|
|
|
(begin try
|
|
|
|
List.assq t !subtypes
|
|
|
|
with Not_found ->
|
1998-06-24 12:22:26 -07:00
|
|
|
let t' = newvar () in
|
1997-01-23 04:46:46 -08:00
|
|
|
subtypes := (t, t')::!subtypes;
|
1999-11-17 15:53:19 -08:00
|
|
|
let (t1', _) = build_subtype env visited t1 in
|
1997-01-23 04:46:46 -08:00
|
|
|
t'.desc <- Tobject (t1', ref None);
|
|
|
|
t'
|
|
|
|
end,
|
|
|
|
true)
|
1997-05-11 14:35:00 -07:00
|
|
|
| Tfield(s, _, t1, t2) (* Always present *) ->
|
1999-11-17 15:53:19 -08:00
|
|
|
let (t1', _) = build_subtype env visited t1 in
|
|
|
|
let (t2', _) = build_subtype env visited t2 in
|
1998-06-24 12:22:26 -07:00
|
|
|
(newty (Tfield(s, Fpresent, t1', t2')), true)
|
1996-04-22 04:15:41 -07:00
|
|
|
| Tnil ->
|
1998-06-24 12:22:26 -07:00
|
|
|
let v = newvar () in
|
1996-04-22 04:15:41 -07:00
|
|
|
(v, true)
|
1999-11-30 08:07:38 -08:00
|
|
|
| Tsubst _ ->
|
|
|
|
assert false
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1997-01-20 09:11:47 -08:00
|
|
|
let enlarge_type env ty =
|
1996-10-27 01:43:07 -08:00
|
|
|
subtypes := [];
|
1999-11-17 15:53:19 -08:00
|
|
|
let (ty', _) = build_subtype env [] ty in
|
1996-10-27 01:43:07 -08:00
|
|
|
subtypes := [];
|
1996-05-28 07:30:52 -07:00
|
|
|
ty'
|
1996-04-22 04:15:41 -07:00
|
|
|
|
1997-01-21 05:38:42 -08:00
|
|
|
(**** Check whether a type is a subtype of another type. ****)
|
1997-01-20 09:11:47 -08:00
|
|
|
|
1997-01-21 09:43:53 -08:00
|
|
|
(*
|
1997-01-23 04:46:46 -08:00
|
|
|
During the traversal, a trace of visited types is maintained. It
|
|
|
|
is printed in case of error.
|
1997-01-21 09:43:53 -08:00
|
|
|
Constraints (pairs of types that must be equals) are accumulated
|
|
|
|
rather than being enforced straight. Indeed, the result would
|
|
|
|
otherwise depend on the order in which these constraints are
|
|
|
|
enforced.
|
|
|
|
A function enforcing these constraints is returned. That way, type
|
|
|
|
variables can be bound to their actual values before this function
|
1997-01-23 04:46:46 -08:00
|
|
|
is called (see Typecore).
|
|
|
|
Only well-defined abbreviations are expanded (hence the tests
|
|
|
|
[generic_abbrev ...]).
|
1997-01-21 09:43:53 -08:00
|
|
|
*)
|
|
|
|
|
1998-07-03 10:40:39 -07:00
|
|
|
let subtypes = TypePairs.create 17
|
1996-04-22 04:15:41 -07:00
|
|
|
|
1997-01-20 09:11:47 -08:00
|
|
|
let subtype_error env trace =
|
|
|
|
raise (Subtype (expand_trace env (List.rev trace), []))
|
1996-10-26 12:39:26 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
let rec subtype_rec env trace t1 t2 cstrs =
|
1997-01-20 09:11:47 -08:00
|
|
|
let t1 = repr t1 in
|
|
|
|
let t2 = repr t2 in
|
|
|
|
if t1 == t2 then [] else
|
1998-07-03 10:40:39 -07:00
|
|
|
|
|
|
|
begin try
|
|
|
|
TypePairs.find subtypes (t1, t2);
|
1998-06-24 12:22:26 -07:00
|
|
|
cstrs
|
1998-07-03 10:40:39 -07:00
|
|
|
with Not_found ->
|
|
|
|
TypePairs.add subtypes (t1, t2) ();
|
1996-04-22 04:15:41 -07:00
|
|
|
match (t1.desc, t2.desc) with
|
1997-01-20 09:11:47 -08:00
|
|
|
(Tvar, _) | (_, Tvar) ->
|
1998-06-24 12:22:26 -07:00
|
|
|
(trace, t1, t2)::cstrs
|
1999-11-30 08:07:38 -08:00
|
|
|
| (Tarrow(l1, t1, u1), Tarrow(l2, t2, u2)) when l1 = l2
|
|
|
|
or !Clflags.classic && not (is_optional l1 or is_optional l2) ->
|
1998-06-24 12:22:26 -07:00
|
|
|
let cstrs = subtype_rec env ((t2, t1)::trace) t2 t1 cstrs in
|
|
|
|
subtype_rec env ((u1, u2)::trace) u1 u2 cstrs
|
1996-04-22 04:15:41 -07:00
|
|
|
| (Ttuple tl1, Ttuple tl2) ->
|
1998-06-24 12:22:26 -07:00
|
|
|
subtype_list env trace tl1 tl2 cstrs
|
1998-07-03 10:40:39 -07:00
|
|
|
| (Tconstr(p1, [], _), Tconstr(p2, [], _)) when Path.same p1 p2 ->
|
|
|
|
cstrs
|
1997-01-23 04:46:46 -08:00
|
|
|
| (Tconstr(p1, tl1, abbrev1), _) when generic_abbrev env p1 ->
|
1998-06-24 12:22:26 -07:00
|
|
|
subtype_rec env trace (expand_abbrev env t1) t2 cstrs
|
1998-08-15 06:43:10 -07:00
|
|
|
| (_, Tconstr(p2, tl2, abbrev2)) when generic_abbrev env p2 ->
|
1998-06-24 12:22:26 -07:00
|
|
|
subtype_rec env trace t1 (expand_abbrev env t2) cstrs
|
1997-01-23 04:46:46 -08:00
|
|
|
| (Tobject (f1, _), Tobject (f2, _))
|
|
|
|
when opened_object f1 & opened_object f2 ->
|
|
|
|
(* Same row variable implies same object. *)
|
1998-06-24 12:22:26 -07:00
|
|
|
(trace, t1, t2)::cstrs
|
1996-04-22 04:15:41 -07:00
|
|
|
| (Tobject (f1, _), Tobject (f2, _)) ->
|
1998-06-24 12:22:26 -07:00
|
|
|
subtype_fields env trace f1 f2 cstrs
|
1999-11-30 08:07:38 -08:00
|
|
|
| (Tvariant row1, Tvariant row2) ->
|
|
|
|
let row1 = row_repr row1 and row2 = row_repr row2 in
|
|
|
|
begin try
|
|
|
|
if not row1.row_closed then raise Exit;
|
|
|
|
let r1, r2, pairs =
|
|
|
|
merge_row_fields row1.row_fields row2.row_fields in
|
|
|
|
if filter_row_fields false r1 <> [] then raise Exit;
|
|
|
|
List.fold_left
|
|
|
|
(fun cstrs (_,f1,f2) ->
|
|
|
|
match row_field_repr f1, row_field_repr f2 with
|
|
|
|
(Rpresent None|Reither(true,_,_)), Rpresent None ->
|
|
|
|
cstrs
|
|
|
|
| Rpresent(Some t1), Rpresent(Some t2) ->
|
|
|
|
subtype_rec env ((t1, t2)::trace) t1 t2 cstrs
|
|
|
|
| Reither(false, t1::_, _), Rpresent(Some t2) ->
|
|
|
|
subtype_rec env ((t1, t2)::trace) t1 t2 cstrs
|
|
|
|
| Rabsent, _ -> cstrs
|
|
|
|
| _ -> raise Exit)
|
|
|
|
cstrs pairs
|
|
|
|
with Exit ->
|
|
|
|
(trace, t1, t2)::cstrs
|
|
|
|
end
|
1996-04-22 04:15:41 -07:00
|
|
|
| (_, _) ->
|
1998-08-15 06:43:10 -07:00
|
|
|
(trace, t1, t2)::cstrs
|
1997-01-20 09:11:47 -08:00
|
|
|
end
|
1996-04-22 04:15:41 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
and subtype_list env trace tl1 tl2 cstrs =
|
1996-05-31 05:27:03 -07:00
|
|
|
if List.length tl1 <> List.length tl2 then
|
1997-01-20 09:11:47 -08:00
|
|
|
subtype_error env trace;
|
|
|
|
List.fold_left2
|
1998-06-24 12:22:26 -07:00
|
|
|
(fun cstrs t1 t2 -> subtype_rec env ((t1, t2)::trace) t1 t2 cstrs)
|
|
|
|
cstrs tl1 tl2
|
1996-04-22 04:15:41 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
and subtype_fields env trace ty1 ty2 cstrs =
|
1996-04-22 04:15:41 -07:00
|
|
|
let (fields1, rest1) = flatten_fields ty1 in
|
|
|
|
let (fields2, rest2) = flatten_fields ty2 in
|
|
|
|
let (pairs, miss1, miss2) = associate_fields fields1 fields2 in
|
1998-10-10 10:57:27 -07:00
|
|
|
[(trace, rest1, build_fields (repr ty2).level miss2 (newvar ()))]
|
1997-01-20 09:11:47 -08:00
|
|
|
@
|
1997-03-07 14:44:02 -08:00
|
|
|
begin match rest2.desc with
|
|
|
|
Tnil -> []
|
1998-10-10 10:57:27 -07:00
|
|
|
| _ -> [(trace, build_fields (repr ty1).level miss1 rest1, rest2)]
|
1997-03-07 14:44:02 -08:00
|
|
|
end
|
1997-01-20 09:11:47 -08:00
|
|
|
@
|
|
|
|
(List.fold_left
|
1998-06-24 12:22:26 -07:00
|
|
|
(fun cstrs (_, k1, t1, k2, t2) ->
|
1997-05-11 14:35:00 -07:00
|
|
|
(* Theses fields are always present *)
|
1998-06-24 12:22:26 -07:00
|
|
|
subtype_rec env ((t1, t2)::trace) t1 t2 cstrs)
|
|
|
|
cstrs pairs)
|
1997-01-20 09:11:47 -08:00
|
|
|
|
|
|
|
let subtype env ty1 ty2 =
|
1998-07-03 10:40:39 -07:00
|
|
|
TypePairs.clear subtypes;
|
1997-01-20 09:11:47 -08:00
|
|
|
(* Build constraint set. *)
|
1998-06-24 12:22:26 -07:00
|
|
|
let cstrs = subtype_rec env [(ty1, ty2)] ty1 ty2 [] in
|
1997-01-20 09:11:47 -08:00
|
|
|
(* Enforce constraints. *)
|
|
|
|
function () ->
|
|
|
|
List.iter
|
|
|
|
(function (trace0, t1, t2) ->
|
|
|
|
try unify env t1 t2 with Unify trace ->
|
|
|
|
raise (Subtype (expand_trace env (List.rev trace0),
|
|
|
|
List.tl (List.tl trace))))
|
1998-06-24 12:22:26 -07:00
|
|
|
(List.rev cstrs);
|
1998-07-03 10:40:39 -07:00
|
|
|
TypePairs.clear subtypes
|
1997-01-20 09:11:47 -08:00
|
|
|
|
1997-01-21 05:38:42 -08:00
|
|
|
|
1997-03-09 08:52:49 -08:00
|
|
|
(*******************)
|
|
|
|
(* Miscellaneous *)
|
|
|
|
(*******************)
|
|
|
|
|
|
|
|
|
|
|
|
let unalias ty =
|
|
|
|
let ty = repr ty in
|
|
|
|
match ty.desc with
|
|
|
|
Tvar ->
|
|
|
|
ty
|
|
|
|
| _ ->
|
1998-07-03 10:40:39 -07:00
|
|
|
newty2 ty.level ty.desc
|
1997-03-09 08:52:49 -08:00
|
|
|
|
|
|
|
let unroll_abbrev id tl ty =
|
|
|
|
let ty = repr ty in
|
1997-10-21 04:33:03 -07:00
|
|
|
if (ty.desc = Tvar) || (List.exists (deep_occur ty) tl) then
|
|
|
|
ty
|
|
|
|
else
|
1998-07-03 10:40:39 -07:00
|
|
|
let ty' = newty2 ty.level ty.desc in
|
|
|
|
ty.desc <- Tlink (newty2 ty.level
|
|
|
|
(Tconstr (Path.Pident id, tl, ref Mnil)));
|
1997-10-21 04:33:03 -07:00
|
|
|
ty'
|
1997-03-09 08:52:49 -08:00
|
|
|
|
|
|
|
(* Return the arity (as for curried functions) of the given type. *)
|
|
|
|
let rec arity ty =
|
|
|
|
match (repr ty).desc with
|
1999-11-30 08:07:38 -08:00
|
|
|
Tarrow(_, t1, t2) -> 1 + arity t2
|
1997-03-09 08:52:49 -08:00
|
|
|
| _ -> 0
|
|
|
|
|
1997-06-29 06:16:47 -07:00
|
|
|
(* Check whether an abbreviation expands to itself. *)
|
|
|
|
let rec cyclic_abbrev env id ty =
|
|
|
|
let ty = repr ty in
|
|
|
|
match ty.desc with
|
|
|
|
Tconstr (Path.Pident id', _, _) when Ident.same id id' ->
|
|
|
|
true
|
|
|
|
| Tconstr (p, tl, abbrev) ->
|
|
|
|
begin try
|
|
|
|
cyclic_abbrev env id (try_expand_head env ty)
|
|
|
|
with Cannot_expand ->
|
|
|
|
false
|
|
|
|
end
|
|
|
|
| _ ->
|
|
|
|
false
|
|
|
|
|
2000-02-24 02:18:25 -08:00
|
|
|
let rec normalize_type_rec env ty =
|
|
|
|
let ty = repr ty in
|
|
|
|
if ty.level >= lowest_level then begin
|
|
|
|
mark_type_node ty;
|
|
|
|
begin match ty.desc with Tvariant row ->
|
|
|
|
let row = row_repr row in
|
|
|
|
List.iter
|
|
|
|
(fun (_,f) ->
|
|
|
|
match row_field_repr f with Reither(b, ty::(_::_ as tyl), e) ->
|
|
|
|
let tyl' =
|
|
|
|
List.fold_left
|
|
|
|
(fun tyl ty ->
|
|
|
|
if List.exists (fun ty' -> equal env false [ty] [ty']) tyl
|
|
|
|
then tyl else ty::tyl)
|
|
|
|
[ty] tyl
|
|
|
|
in
|
|
|
|
if List.length tyl' < List.length tyl + 1 then
|
|
|
|
e := Some(Reither(b, List.rev tyl', ref None))
|
|
|
|
| _ -> ())
|
|
|
|
row.row_fields
|
|
|
|
| _ -> ()
|
|
|
|
end;
|
|
|
|
iter_type_expr (normalize_type_rec env) ty
|
|
|
|
end
|
|
|
|
|
|
|
|
let normalize_type env ty = ()
|
|
|
|
normalize_type_rec env ty;
|
|
|
|
unmark_type ty
|
|
|
|
|
1997-03-09 08:52:49 -08:00
|
|
|
|
1997-01-21 05:38:42 -08:00
|
|
|
(*************************)
|
|
|
|
(* Remove dependencies *)
|
|
|
|
(*************************)
|
|
|
|
|
1996-04-22 04:15:41 -07:00
|
|
|
|
1997-03-09 08:52:49 -08:00
|
|
|
(*
|
|
|
|
Variables are left unchanged. Other type nodes are duplicated, with
|
|
|
|
levels set to generic level.
|
|
|
|
During copying, the description of a (non-variable) node is first
|
1999-11-30 08:07:38 -08:00
|
|
|
replaced by a link to a stub ([Tsubst (newgenvar ())]).
|
1997-03-24 12:12:00 -08:00
|
|
|
Once the copy is made, it replaces the stub.
|
|
|
|
After copying, the description of node, which was stored by
|
1999-11-30 08:07:38 -08:00
|
|
|
[save_desc], must be put back, using [cleanup_types].
|
1997-03-09 08:52:49 -08:00
|
|
|
*)
|
|
|
|
|
1996-04-22 04:15:41 -07:00
|
|
|
let rec nondep_type_rec env id ty =
|
|
|
|
let ty = repr ty in
|
1999-11-30 08:07:38 -08:00
|
|
|
match ty.desc with
|
|
|
|
Tvar -> ty
|
|
|
|
| Tsubst ty -> ty
|
|
|
|
| _ ->
|
1997-03-09 08:52:49 -08:00
|
|
|
let desc = ty.desc in
|
1997-03-24 12:12:00 -08:00
|
|
|
save_desc ty desc;
|
1999-11-30 08:07:38 -08:00
|
|
|
let ty' = newgenvar () in (* Stub *)
|
|
|
|
ty.desc <- Tsubst ty';
|
1996-04-22 04:15:41 -07:00
|
|
|
ty'.desc <-
|
1997-03-09 08:52:49 -08:00
|
|
|
begin match desc with
|
1996-04-22 04:15:41 -07:00
|
|
|
Tvar ->
|
1997-03-09 08:52:49 -08:00
|
|
|
fatal_error "Ctype.nondep_type_rec"
|
1999-11-30 08:07:38 -08:00
|
|
|
| Tarrow(l, t1, t2) ->
|
|
|
|
Tarrow(l, nondep_type_rec env id t1, nondep_type_rec env id t2)
|
1996-04-22 04:15:41 -07:00
|
|
|
| Ttuple tl ->
|
|
|
|
Ttuple(List.map (nondep_type_rec env id) tl)
|
|
|
|
| Tconstr(p, tl, abbrev) ->
|
|
|
|
if Path.isfree id p then
|
|
|
|
begin try
|
1997-03-09 08:52:49 -08:00
|
|
|
Tlink (nondep_type_rec env id
|
1998-07-03 10:40:39 -07:00
|
|
|
(expand_abbrev env (newty2 ty.level desc)))
|
1997-03-09 08:52:49 -08:00
|
|
|
(*
|
|
|
|
The [Tlink] is important. The expanded type may be a
|
|
|
|
variable, or may not be completely copied yet
|
|
|
|
(recursive type), so one cannot just take its
|
|
|
|
description.
|
|
|
|
*)
|
1996-04-22 04:15:41 -07:00
|
|
|
with Cannot_expand ->
|
|
|
|
raise Not_found
|
|
|
|
end
|
|
|
|
else
|
1997-01-21 09:43:53 -08:00
|
|
|
Tconstr(p, List.map (nondep_type_rec env id) tl, ref Mnil)
|
1996-04-22 04:15:41 -07:00
|
|
|
| Tobject (t1, name) ->
|
|
|
|
Tobject (nondep_type_rec env id t1,
|
|
|
|
ref (match !name with
|
|
|
|
None -> None
|
|
|
|
| Some (p, tl) ->
|
|
|
|
if Path.isfree id p then None
|
|
|
|
else Some (p, List.map (nondep_type_rec env id) tl)))
|
1999-11-30 08:07:38 -08:00
|
|
|
| Tvariant row ->
|
|
|
|
let row = row_repr row in
|
|
|
|
let more = repr row.row_more in
|
|
|
|
(* We must substitute in a subtle way *)
|
|
|
|
begin match more.desc with
|
|
|
|
Tsubst ty2 ->
|
|
|
|
(* This variant type has been already copied *)
|
|
|
|
ty.desc <- Tsubst ty2; (* avoid Tlink in the new type *)
|
|
|
|
Tlink ty2
|
|
|
|
| _ ->
|
|
|
|
(* We create a new copy *)
|
|
|
|
let bound = ref [] in
|
|
|
|
let fields =
|
|
|
|
List.map
|
|
|
|
(fun (l,fi) -> l,
|
|
|
|
match row_field_repr fi with
|
|
|
|
Rpresent (Some ty) ->
|
|
|
|
Rpresent(Some (nondep_type_rec env id ty))
|
|
|
|
| Reither(c, l, _) ->
|
|
|
|
let l = List.map (nondep_type_rec env id) l in
|
|
|
|
bound := l @ !bound;
|
|
|
|
Reither(c, l, ref None)
|
|
|
|
| fi -> fi)
|
|
|
|
row.row_fields
|
|
|
|
and name =
|
|
|
|
match row.row_name with
|
|
|
|
Some (p,l) when Path.isfree id p ->
|
|
|
|
Some (p, List.map (nondep_type_rec env id) l)
|
|
|
|
| _ -> None
|
|
|
|
in
|
|
|
|
let var =
|
|
|
|
Tvariant { row_fields = fields; row_more = newgenvar();
|
|
|
|
row_bound = !bound;
|
|
|
|
row_closed = row.row_closed; row_name = name }
|
|
|
|
in
|
|
|
|
(* Remember it for other occurences *)
|
|
|
|
save_desc more more.desc;
|
|
|
|
more.desc <- ty.desc;
|
|
|
|
var
|
|
|
|
end
|
1997-05-11 14:35:00 -07:00
|
|
|
| Tfield(label, kind, t1, t2) ->
|
|
|
|
begin match field_kind_repr kind with
|
|
|
|
Fpresent ->
|
|
|
|
Tfield(label, Fpresent, nondep_type_rec env id t1,
|
1998-06-24 12:22:26 -07:00
|
|
|
nondep_type_rec env id t2)
|
|
|
|
| Fvar _ (* {contents = None} *) as k ->
|
|
|
|
Tfield(label, k, nondep_type_rec env id t1,
|
|
|
|
nondep_type_rec env id t2)
|
|
|
|
| Fabsent ->
|
|
|
|
assert false
|
1997-05-11 14:35:00 -07:00
|
|
|
end
|
1997-03-09 08:52:49 -08:00
|
|
|
| Tnil ->
|
|
|
|
Tnil
|
|
|
|
| Tlink ty -> (* Actually unused *)
|
|
|
|
Tlink(nondep_type_rec env id ty)
|
1999-11-30 08:07:38 -08:00
|
|
|
| Tsubst _ ->
|
|
|
|
assert false
|
1997-03-09 08:52:49 -08:00
|
|
|
end;
|
|
|
|
ty'
|
1996-04-22 04:15:41 -07:00
|
|
|
|
|
|
|
let nondep_type env id ty =
|
1997-03-09 08:52:49 -08:00
|
|
|
try
|
|
|
|
let ty' = nondep_type_rec env id ty in
|
|
|
|
cleanup_types ();
|
|
|
|
unmark_type ty';
|
|
|
|
ty'
|
|
|
|
with Not_found ->
|
|
|
|
cleanup_types ();
|
|
|
|
raise Not_found
|
|
|
|
|
1997-03-09 10:42:51 -08:00
|
|
|
(* Preserve sharing inside type declarations. *)
|
1997-03-09 08:52:49 -08:00
|
|
|
let nondep_type_decl env mid id is_covariant decl =
|
|
|
|
try
|
|
|
|
let params = List.map (nondep_type_rec env mid) decl.type_params in
|
|
|
|
let decl =
|
|
|
|
{ type_params = params;
|
|
|
|
type_arity = decl.type_arity;
|
|
|
|
type_kind =
|
|
|
|
begin try
|
|
|
|
match decl.type_kind with
|
|
|
|
Type_abstract ->
|
|
|
|
Type_abstract
|
|
|
|
| Type_variant cstrs ->
|
|
|
|
Type_variant(List.map
|
|
|
|
(fun (c, tl) -> (c, List.map (nondep_type_rec env mid) tl))
|
|
|
|
cstrs)
|
|
|
|
| Type_record lbls ->
|
|
|
|
Type_record(List.map
|
|
|
|
(fun (c, mut, t) -> (c, mut, nondep_type_rec env mid t))
|
|
|
|
lbls)
|
|
|
|
with Not_found when is_covariant ->
|
|
|
|
Type_abstract
|
|
|
|
end;
|
|
|
|
type_manifest =
|
|
|
|
begin try
|
|
|
|
match decl.type_manifest with
|
|
|
|
None -> None
|
|
|
|
| Some ty ->
|
|
|
|
Some (unroll_abbrev id params (nondep_type_rec env mid ty))
|
|
|
|
with Not_found when is_covariant ->
|
|
|
|
None
|
|
|
|
end }
|
|
|
|
in
|
|
|
|
cleanup_types ();
|
1997-03-09 10:42:51 -08:00
|
|
|
List.iter unmark_type decl.type_params;
|
1997-03-09 08:52:49 -08:00
|
|
|
begin match decl.type_kind with
|
|
|
|
Type_abstract -> ()
|
|
|
|
| Type_variant cstrs ->
|
|
|
|
List.iter (fun (c, tl) -> List.iter unmark_type tl) cstrs
|
|
|
|
| Type_record lbls ->
|
|
|
|
List.iter (fun (c, mut, t) -> unmark_type t) lbls
|
|
|
|
end;
|
|
|
|
begin match decl.type_manifest with
|
|
|
|
None -> ()
|
|
|
|
| Some ty -> unmark_type ty
|
|
|
|
end;
|
|
|
|
decl
|
|
|
|
with Not_found ->
|
|
|
|
cleanup_types ();
|
|
|
|
raise Not_found
|
1996-04-22 04:15:41 -07:00
|
|
|
|
1997-03-09 10:42:51 -08:00
|
|
|
(* Preserve sharing inside class types. *)
|
1998-06-24 12:22:26 -07:00
|
|
|
let nondep_class_signature env id sign =
|
|
|
|
{ cty_self = nondep_type_rec env id sign.cty_self;
|
|
|
|
cty_vars =
|
|
|
|
Vars.map (function (m, t) -> (m, nondep_type_rec env id t))
|
|
|
|
sign.cty_vars;
|
|
|
|
cty_concr = sign.cty_concr }
|
|
|
|
|
|
|
|
let rec nondep_class_type env id =
|
|
|
|
function
|
|
|
|
Tcty_constr (p, _, cty) when Path.isfree id p ->
|
|
|
|
nondep_class_type env id cty
|
|
|
|
| Tcty_constr (p, tyl, cty) ->
|
|
|
|
Tcty_constr (p, List.map (nondep_type_rec env id) tyl,
|
|
|
|
nondep_class_type env id cty)
|
|
|
|
| Tcty_signature sign ->
|
|
|
|
Tcty_signature (nondep_class_signature env id sign)
|
1999-11-30 08:07:38 -08:00
|
|
|
| Tcty_fun (l, ty, cty) ->
|
|
|
|
Tcty_fun (l, nondep_type_rec env id ty, nondep_class_type env id cty)
|
1998-06-24 12:22:26 -07:00
|
|
|
|
|
|
|
let nondep_class_declaration env id decl =
|
|
|
|
assert (not (Path.isfree id decl.cty_path));
|
|
|
|
let decl =
|
|
|
|
{ cty_params = List.map (nondep_type_rec env id) decl.cty_params;
|
|
|
|
cty_type = nondep_class_type env id decl.cty_type;
|
|
|
|
cty_path = decl.cty_path;
|
|
|
|
cty_new =
|
|
|
|
begin match decl.cty_new with
|
|
|
|
None -> None
|
|
|
|
| Some ty -> Some (nondep_type_rec env id ty)
|
|
|
|
end }
|
|
|
|
in
|
|
|
|
cleanup_types ();
|
|
|
|
List.iter unmark_type decl.cty_params;
|
|
|
|
unmark_class_type decl.cty_type;
|
|
|
|
begin match decl.cty_new with
|
|
|
|
None -> ()
|
|
|
|
| Some ty -> unmark_type ty
|
|
|
|
end;
|
|
|
|
decl
|
|
|
|
|
|
|
|
let nondep_cltype_declaration env id decl =
|
|
|
|
assert (not (Path.isfree id decl.clty_path));
|
|
|
|
let decl =
|
|
|
|
{ clty_params = List.map (nondep_type_rec env id) decl.clty_params;
|
|
|
|
clty_type = nondep_class_type env id decl.clty_type;
|
|
|
|
clty_path = decl.clty_path }
|
|
|
|
in
|
|
|
|
cleanup_types ();
|
|
|
|
List.iter unmark_type decl.clty_params;
|
|
|
|
unmark_class_type decl.clty_type;
|
|
|
|
decl
|