1997-03-24 12:11:22 -08:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Objective Caml *)
|
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy and Jerome Vouillon, projet Cristal, INRIA Rocquencourt*)
|
|
|
|
(* *)
|
|
|
|
(* 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. *)
|
1997-03-24 12:11:22 -08:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
|
|
|
(* Basic operations on core types *)
|
|
|
|
|
|
|
|
open Types
|
|
|
|
|
|
|
|
(**** Type level management ****)
|
|
|
|
|
|
|
|
let generic_level = 100000000
|
|
|
|
|
|
|
|
(* Used to mark a type during a traversal. *)
|
|
|
|
let lowest_level = 0
|
|
|
|
let pivot_level = 2 * lowest_level - 1
|
|
|
|
(* pivot_level - lowest_level < lowest_level *)
|
|
|
|
|
|
|
|
(**** Some type creators ****)
|
|
|
|
|
1998-07-03 10:40:39 -07:00
|
|
|
let new_id = ref (-1)
|
|
|
|
|
|
|
|
let newty2 level desc =
|
|
|
|
incr new_id; { desc = desc; level = level; id = !new_id }
|
|
|
|
let newgenty desc = newty2 generic_level desc
|
1997-03-24 12:11:22 -08:00
|
|
|
let newgenvar () = newgenty Tvar
|
1999-11-30 08:07:38 -08:00
|
|
|
(*
|
1998-07-03 10:40:39 -07:00
|
|
|
let newmarkedvar level =
|
|
|
|
incr new_id; { desc = Tvar; level = pivot_level - level; id = !new_id }
|
|
|
|
let newmarkedgenvar () =
|
|
|
|
incr new_id;
|
|
|
|
{ desc = Tvar; level = pivot_level - generic_level; id = !new_id }
|
1999-11-30 08:07:38 -08:00
|
|
|
*)
|
1997-03-24 12:11:22 -08:00
|
|
|
|
|
|
|
(**** Representative of a type ****)
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
let rec field_kind_repr =
|
|
|
|
function
|
|
|
|
Fvar {contents = Some kind} -> field_kind_repr kind
|
|
|
|
| kind -> kind
|
|
|
|
|
1997-03-24 12:11:22 -08:00
|
|
|
let rec repr =
|
|
|
|
function
|
|
|
|
{desc = Tlink t'} ->
|
|
|
|
(*
|
|
|
|
We do no path compression. Path compression does not seem to
|
|
|
|
improve notably efficiency, and it prevents from changing a
|
|
|
|
[Tlink] into another type (for instance, for undoing a
|
|
|
|
unification).
|
|
|
|
*)
|
|
|
|
repr t'
|
1998-06-24 12:22:26 -07:00
|
|
|
| {desc = Tfield (_, k, _, t')} when field_kind_repr k = Fabsent ->
|
|
|
|
repr t'
|
1997-03-24 12:11:22 -08:00
|
|
|
| t -> t
|
|
|
|
|
2001-04-19 01:34:21 -07:00
|
|
|
let rec commu_repr = function
|
|
|
|
Clink r when !r <> Cunknown -> commu_repr !r
|
|
|
|
| c -> c
|
|
|
|
|
2001-11-22 20:35:48 -08:00
|
|
|
let rec row_field_repr_aux tl = function
|
|
|
|
Reither(_, tl', _, {contents = Some fi}) ->
|
|
|
|
row_field_repr_aux (tl@tl') fi
|
2001-11-23 06:28:21 -08:00
|
|
|
| Reither(c, tl', m, r) ->
|
|
|
|
Reither(c, tl@tl', m, r)
|
2001-11-22 20:35:48 -08:00
|
|
|
| Rpresent (Some _) when tl <> [] ->
|
|
|
|
Rpresent (Some (List.hd tl))
|
1999-11-30 08:07:38 -08:00
|
|
|
| fi -> fi
|
|
|
|
|
2001-11-22 20:35:48 -08:00
|
|
|
let row_field_repr fi = row_field_repr_aux [] fi
|
|
|
|
|
2004-01-06 05:41:40 -08:00
|
|
|
let rec rev_concat l ll =
|
|
|
|
match ll with
|
|
|
|
[] -> l
|
|
|
|
| l'::ll -> rev_concat (l'@l) ll
|
|
|
|
|
|
|
|
let rec row_repr_aux ll row =
|
1999-11-30 08:07:38 -08:00
|
|
|
match (repr row.row_more).desc with
|
|
|
|
| Tvariant row' ->
|
2004-01-06 05:41:40 -08:00
|
|
|
let f = row.row_fields in
|
|
|
|
row_repr_aux (if f = [] then ll else f::ll) row'
|
|
|
|
| _ ->
|
|
|
|
if ll = [] then row else
|
|
|
|
{row with row_fields = rev_concat row.row_fields ll}
|
|
|
|
|
|
|
|
let row_repr row = row_repr_aux [] row
|
|
|
|
|
|
|
|
let rec row_field tag row =
|
|
|
|
let rec find = function
|
|
|
|
| (tag',f) :: fields ->
|
|
|
|
if tag = tag' then row_field_repr f else find fields
|
|
|
|
| [] ->
|
|
|
|
match repr row.row_more with
|
|
|
|
| {desc=Tvariant row'} -> row_field tag row'
|
|
|
|
| _ -> Rabsent
|
|
|
|
in find row.row_fields
|
1999-11-30 08:07:38 -08:00
|
|
|
|
|
|
|
let rec row_more row =
|
|
|
|
match repr row.row_more with
|
|
|
|
| {desc=Tvariant row'} -> row_more row'
|
|
|
|
| ty -> ty
|
|
|
|
|
|
|
|
let static_row row =
|
|
|
|
let row = row_repr row in
|
|
|
|
row.row_closed &&
|
|
|
|
List.for_all
|
|
|
|
(fun (_,f) -> match row_field_repr f with Reither _ -> false | _ -> true)
|
|
|
|
row.row_fields
|
|
|
|
|
|
|
|
let hash_variant s =
|
|
|
|
let accu = ref 0 in
|
|
|
|
for i = 0 to String.length s - 1 do
|
|
|
|
accu := 223 * !accu + Char.code s.[i]
|
|
|
|
done;
|
|
|
|
(* reduce to 31 bits *)
|
|
|
|
accu := !accu land (1 lsl 31 - 1);
|
|
|
|
(* make it signed for 64 bits architectures *)
|
|
|
|
if !accu > 0x3FFFFFFF then !accu - (1 lsl 31) else !accu
|
|
|
|
|
2003-05-19 02:21:17 -07:00
|
|
|
let proxy ty =
|
2005-03-10 00:19:22 -08:00
|
|
|
let ty0 = repr ty in
|
|
|
|
match ty0.desc with
|
|
|
|
| Tvariant row when not (static_row row) ->
|
|
|
|
row_more row
|
2003-05-19 02:21:17 -07:00
|
|
|
| Tobject (ty, _) ->
|
|
|
|
let rec proxy_obj ty =
|
|
|
|
match ty.desc with
|
|
|
|
Tfield (_, _, _, ty) | Tlink ty -> proxy_obj ty
|
2005-03-10 00:19:22 -08:00
|
|
|
| Tvar | Tunivar | Tconstr _ -> ty
|
|
|
|
| Tnil -> ty0
|
2003-05-19 02:21:17 -07:00
|
|
|
| _ -> assert false
|
|
|
|
in proxy_obj ty
|
2005-03-10 00:19:22 -08:00
|
|
|
| _ -> ty0
|
2003-05-19 02:21:17 -07:00
|
|
|
|
2005-03-22 19:08:37 -08:00
|
|
|
(**** Utilities for private types ****)
|
|
|
|
|
|
|
|
let has_constr_row t =
|
|
|
|
match (repr t).desc with
|
|
|
|
Tobject(t,_) ->
|
|
|
|
let rec check_row t =
|
|
|
|
match (repr t).desc with
|
|
|
|
Tfield(_,_,_,t) -> check_row t
|
|
|
|
| Tconstr _ -> true
|
|
|
|
| _ -> false
|
|
|
|
in check_row t
|
|
|
|
| Tvariant row ->
|
|
|
|
(match row_more row with {desc=Tconstr _} -> true | _ -> false)
|
|
|
|
| _ ->
|
|
|
|
false
|
|
|
|
|
|
|
|
let is_row_name s =
|
|
|
|
let l = String.length s in
|
|
|
|
if l < 4 then false else String.sub s (l-4) 4 = "#row"
|
|
|
|
|
1997-03-24 12:11:22 -08:00
|
|
|
|
|
|
|
(**********************************)
|
|
|
|
(* Utilities for type traversal *)
|
|
|
|
(**********************************)
|
|
|
|
|
1999-11-30 08:07:38 -08:00
|
|
|
let rec iter_row f row =
|
|
|
|
List.iter
|
|
|
|
(fun (_, fi) ->
|
|
|
|
match row_field_repr fi with
|
|
|
|
| Rpresent(Some ty) -> f ty
|
2001-03-02 16:14:35 -08:00
|
|
|
| Reither(_, tl, _, _) -> List.iter f tl
|
1999-11-30 08:07:38 -08:00
|
|
|
| _ -> ())
|
|
|
|
row.row_fields;
|
|
|
|
match (repr row.row_more).desc with
|
|
|
|
Tvariant row -> iter_row f row
|
2005-03-22 19:08:37 -08:00
|
|
|
| Tvar | Tunivar | Tsubst _ | Tconstr _ ->
|
1999-11-30 08:07:38 -08:00
|
|
|
Misc.may (fun (_,l) -> List.iter f l) row.row_name;
|
|
|
|
List.iter f row.row_bound
|
|
|
|
| _ -> assert false
|
1997-03-24 12:11:22 -08:00
|
|
|
|
|
|
|
let iter_type_expr f ty =
|
|
|
|
match ty.desc with
|
1999-11-30 08:07:38 -08:00
|
|
|
Tvar -> ()
|
2001-04-19 01:34:21 -07:00
|
|
|
| Tarrow (_, ty1, ty2, _) -> f ty1; f ty2
|
1999-11-30 08:07:38 -08:00
|
|
|
| Ttuple l -> List.iter f l
|
|
|
|
| Tconstr (_, l, _) -> List.iter f l
|
1997-03-24 12:11:22 -08:00
|
|
|
| Tobject(ty, {contents = Some (_, p)})
|
1999-11-30 08:07:38 -08:00
|
|
|
-> f ty; List.iter f p
|
|
|
|
| Tobject (ty, _) -> f ty
|
|
|
|
| Tvariant row -> iter_row f row; f (row_more row)
|
1997-05-11 14:48:21 -07:00
|
|
|
| Tfield (_, _, ty1, ty2) -> f ty1; f ty2
|
1999-11-30 08:07:38 -08:00
|
|
|
| Tnil -> ()
|
|
|
|
| Tlink ty -> f ty
|
2000-05-24 20:09:28 -07:00
|
|
|
| Tsubst ty -> f ty
|
2002-04-18 00:27:47 -07:00
|
|
|
| Tunivar -> ()
|
|
|
|
| Tpoly (ty, tyl) -> f ty; List.iter f tyl
|
1997-03-24 12:11:22 -08:00
|
|
|
|
2002-04-18 00:27:47 -07:00
|
|
|
let rec iter_abbrev f = function
|
|
|
|
Mnil -> ()
|
|
|
|
| Mcons(_, ty, ty', rem) -> f ty; f ty'; iter_abbrev f rem
|
|
|
|
| Mlink rem -> iter_abbrev f !rem
|
|
|
|
|
|
|
|
let copy_row f fixed row keep more =
|
2002-04-04 05:43:11 -08:00
|
|
|
let bound = ref [] in
|
2001-11-22 02:41:29 -08:00
|
|
|
let fields = List.map
|
|
|
|
(fun (l, fi) -> l,
|
|
|
|
match row_field_repr fi with
|
|
|
|
| Rpresent(Some ty) -> Rpresent(Some(f ty))
|
2001-11-22 20:35:48 -08:00
|
|
|
| Reither(c, tl, m, e) ->
|
|
|
|
let e = if keep then e else ref None in
|
2002-04-18 00:27:47 -07:00
|
|
|
let m = if row.row_fixed then fixed else m in
|
2002-04-04 05:43:11 -08:00
|
|
|
let tl = List.map f tl in
|
|
|
|
bound := List.filter
|
|
|
|
(function {desc=Tconstr(_,[],_)} -> false | _ -> true)
|
|
|
|
(List.map repr tl)
|
|
|
|
@ !bound;
|
|
|
|
Reither(c, tl, m, e)
|
2001-11-22 02:41:29 -08:00
|
|
|
| _ -> fi)
|
|
|
|
row.row_fields in
|
|
|
|
let name =
|
|
|
|
match row.row_name with None -> None
|
|
|
|
| Some (path, tl) -> Some (path, List.map f tl) in
|
2002-04-18 00:27:47 -07:00
|
|
|
{ row_fields = fields; row_more = more;
|
|
|
|
row_bound = !bound; row_fixed = row.row_fixed && fixed;
|
2001-11-22 02:41:29 -08:00
|
|
|
row_closed = row.row_closed; row_name = name; }
|
|
|
|
|
|
|
|
let rec copy_kind = function
|
|
|
|
Fvar{contents = Some k} -> copy_kind k
|
|
|
|
| Fvar _ -> Fvar (ref None)
|
|
|
|
| Fpresent -> Fpresent
|
|
|
|
| Fabsent -> assert false
|
|
|
|
|
|
|
|
let copy_commu c =
|
|
|
|
if commu_repr c = Cok then Cok else Clink (ref Cunknown)
|
|
|
|
|
2003-06-25 00:52:27 -07:00
|
|
|
(* Since univars may be used as row variables, we need to do some
|
|
|
|
encoding during substitution *)
|
2002-09-10 23:09:26 -07:00
|
|
|
let rec norm_univar ty =
|
|
|
|
match ty.desc with
|
|
|
|
Tunivar | Tsubst _ -> ty
|
|
|
|
| Tlink ty -> norm_univar ty
|
2003-06-25 00:52:27 -07:00
|
|
|
| Ttuple (ty :: _) -> norm_univar ty
|
2002-09-10 23:09:26 -07:00
|
|
|
| _ -> assert false
|
|
|
|
|
2001-11-22 02:41:29 -08:00
|
|
|
let rec copy_type_desc f = function
|
|
|
|
Tvar -> Tvar
|
|
|
|
| Tarrow (p, ty1, ty2, c)-> Tarrow (p, f ty1, f ty2, copy_commu c)
|
|
|
|
| Ttuple l -> Ttuple (List.map f l)
|
|
|
|
| Tconstr (p, l, _) -> Tconstr (p, List.map f l, ref Mnil)
|
|
|
|
| Tobject(ty, {contents = Some (p, tl)})
|
|
|
|
-> Tobject (f ty, ref (Some(p, List.map f tl)))
|
|
|
|
| Tobject (ty, _) -> Tobject (f ty, ref None)
|
2006-01-04 08:55:50 -08:00
|
|
|
| Tvariant row -> assert false (* too ambiguous *)
|
|
|
|
| Tfield (p, k, ty1, ty2) -> (* the kind is kept shared *)
|
|
|
|
Tfield (p, field_kind_repr k, f ty1, f ty2)
|
2001-11-22 02:41:29 -08:00
|
|
|
| Tnil -> Tnil
|
|
|
|
| Tlink ty -> copy_type_desc f ty.desc
|
|
|
|
| Tsubst ty -> assert false
|
2002-04-18 00:27:47 -07:00
|
|
|
| Tunivar -> Tunivar
|
2002-09-10 23:09:26 -07:00
|
|
|
| Tpoly (ty, tyl) ->
|
|
|
|
let tyl = List.map (fun x -> norm_univar (f x)) tyl in
|
|
|
|
Tpoly (f ty, tyl)
|
2001-11-22 02:41:29 -08:00
|
|
|
|
2002-04-03 19:49:58 -08:00
|
|
|
|
|
|
|
(* Utilities for copying *)
|
|
|
|
|
1997-03-24 12:11:22 -08:00
|
|
|
let saved_desc = ref []
|
|
|
|
(* Saved association of generic nodes with their description. *)
|
|
|
|
|
|
|
|
let save_desc ty desc =
|
|
|
|
saved_desc := (ty, desc)::!saved_desc
|
|
|
|
|
2006-01-04 08:55:50 -08:00
|
|
|
let saved_kinds = ref [] (* duplicated kind variables *)
|
|
|
|
let new_kinds = ref [] (* new kind variables *)
|
|
|
|
let dup_kind r =
|
|
|
|
(match !r with None -> () | Some _ -> assert false);
|
|
|
|
if not (List.memq r !new_kinds) then begin
|
|
|
|
saved_kinds := r :: !saved_kinds;
|
|
|
|
let r' = ref None in
|
|
|
|
new_kinds := r' :: !new_kinds;
|
|
|
|
r := Some (Fvar r')
|
|
|
|
end
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
(* Restored type descriptions. *)
|
1997-03-24 12:11:22 -08:00
|
|
|
let cleanup_types () =
|
|
|
|
List.iter (fun (ty, desc) -> ty.desc <- desc) !saved_desc;
|
2006-01-04 08:55:50 -08:00
|
|
|
List.iter (fun r -> r := None) !saved_kinds;
|
|
|
|
saved_desc := []; saved_kinds := []; new_kinds := []
|
1997-03-24 12:11:22 -08:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
(* Mark a type. *)
|
|
|
|
let rec mark_type ty =
|
|
|
|
let ty = repr ty in
|
|
|
|
if ty.level >= lowest_level then begin
|
|
|
|
ty.level <- pivot_level - ty.level;
|
|
|
|
iter_type_expr mark_type ty
|
|
|
|
end
|
|
|
|
|
1998-10-10 10:57:27 -07:00
|
|
|
let mark_type_node ty =
|
|
|
|
let ty = repr ty in
|
|
|
|
if ty.level >= lowest_level then begin
|
|
|
|
ty.level <- pivot_level - ty.level;
|
|
|
|
end
|
|
|
|
|
|
|
|
let mark_type_params ty =
|
|
|
|
iter_type_expr mark_type ty
|
|
|
|
|
1997-03-24 12:11:22 -08:00
|
|
|
(* Remove marks from a type. *)
|
|
|
|
let rec unmark_type ty =
|
|
|
|
let ty = repr ty in
|
|
|
|
if ty.level < lowest_level then begin
|
|
|
|
ty.level <- pivot_level - ty.level;
|
|
|
|
iter_type_expr unmark_type ty
|
|
|
|
end
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
let unmark_type_decl decl =
|
|
|
|
List.iter unmark_type decl.type_params;
|
2003-07-02 02:14:35 -07:00
|
|
|
begin match decl.type_kind with
|
1998-06-24 12:22:26 -07:00
|
|
|
Type_abstract -> ()
|
2003-07-02 02:14:35 -07:00
|
|
|
| Type_variant (cstrs, priv) ->
|
1998-06-24 12:22:26 -07:00
|
|
|
List.iter (fun (c, tl) -> List.iter unmark_type tl) cstrs
|
2003-07-02 02:14:35 -07:00
|
|
|
| Type_record(lbls, rep, priv) ->
|
1998-06-24 12:22:26 -07:00
|
|
|
List.iter (fun (c, mut, t) -> unmark_type t) lbls
|
2003-07-02 02:14:35 -07:00
|
|
|
end;
|
1998-06-24 12:22:26 -07:00
|
|
|
begin match decl.type_manifest with
|
|
|
|
None -> ()
|
|
|
|
| Some ty -> unmark_type ty
|
|
|
|
end
|
|
|
|
|
|
|
|
let unmark_class_signature sign =
|
|
|
|
unmark_type sign.cty_self;
|
2006-04-04 19:28:13 -07:00
|
|
|
Vars.iter (fun l (m, v, t) -> unmark_type t) sign.cty_vars
|
1998-06-24 12:22:26 -07:00
|
|
|
|
|
|
|
let rec unmark_class_type =
|
|
|
|
function
|
|
|
|
Tcty_constr (p, tyl, cty) ->
|
|
|
|
List.iter unmark_type tyl; unmark_class_type cty
|
|
|
|
| Tcty_signature sign ->
|
|
|
|
unmark_class_signature sign
|
1999-11-30 08:07:38 -08:00
|
|
|
| Tcty_fun (_, ty, cty) ->
|
1998-06-24 12:22:26 -07:00
|
|
|
unmark_type ty; unmark_class_type cty
|
|
|
|
|
|
|
|
|
1997-03-24 12:11:22 -08:00
|
|
|
(*******************************************)
|
|
|
|
(* Memorization of abbreviation expansion *)
|
|
|
|
(*******************************************)
|
|
|
|
|
2003-11-06 17:07:32 -08:00
|
|
|
(* Search whether the expansion has been memorized. *)
|
|
|
|
let rec find_expans p1 = function
|
|
|
|
Mnil -> None
|
|
|
|
| Mcons (p2, ty0, ty, _) when Path.same p1 p2 -> Some ty
|
|
|
|
| Mcons (_, _, _, rem) -> find_expans p1 rem
|
|
|
|
| Mlink {contents = rem} -> find_expans p1 rem
|
|
|
|
|
|
|
|
(* debug: check for cycles in abbreviation. only works with -principal
|
|
|
|
let rec check_expans visited ty =
|
|
|
|
let ty = repr ty in
|
|
|
|
assert (not (List.memq ty visited));
|
|
|
|
match ty.desc with
|
|
|
|
Tconstr (path, args, abbrev) ->
|
|
|
|
begin match find_expans path !abbrev with
|
|
|
|
Some ty' -> check_expans (ty :: visited) ty'
|
|
|
|
| None -> ()
|
|
|
|
end
|
|
|
|
| _ -> ()
|
|
|
|
*)
|
|
|
|
|
1997-03-24 12:11:22 -08:00
|
|
|
let memo = ref []
|
|
|
|
(* Contains the list of saved abbreviation expansions. *)
|
|
|
|
|
|
|
|
let cleanup_abbrev () =
|
|
|
|
(* Remove all memorized abbreviation expansions. *)
|
|
|
|
List.iter (fun abbr -> abbr := Mnil) !memo;
|
|
|
|
memo := []
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
let memorize_abbrev mem path v v' =
|
1997-03-24 12:11:22 -08:00
|
|
|
(* Memorize the expansion of an abbreviation. *)
|
1998-06-24 12:22:26 -07:00
|
|
|
mem := Mcons (path, v, v', !mem);
|
2003-11-06 17:07:32 -08:00
|
|
|
(* check_expans [] v; *)
|
1997-03-24 12:11:22 -08:00
|
|
|
memo := mem :: !memo
|
1998-06-24 12:22:26 -07:00
|
|
|
|
|
|
|
let rec forget_abbrev_rec mem path =
|
|
|
|
match mem with
|
|
|
|
Mnil ->
|
|
|
|
assert false
|
|
|
|
| Mcons (path', _, _, rem) when Path.same path path' ->
|
|
|
|
rem
|
2002-02-14 00:45:57 -08:00
|
|
|
| Mcons (path', v, v', rem) ->
|
|
|
|
Mcons (path', v, v', forget_abbrev_rec rem path)
|
1998-06-24 12:22:26 -07:00
|
|
|
| Mlink mem' ->
|
|
|
|
mem' := forget_abbrev_rec !mem' path;
|
|
|
|
raise Exit
|
|
|
|
|
|
|
|
let forget_abbrev mem path =
|
|
|
|
try mem := forget_abbrev_rec !mem path with Exit -> ()
|
1999-11-30 08:07:38 -08:00
|
|
|
|
2003-11-06 17:07:32 -08:00
|
|
|
(* debug: check for invalid abbreviations
|
|
|
|
let rec check_abbrev_rec = function
|
2003-08-09 04:47:57 -07:00
|
|
|
Mnil -> true
|
2003-11-06 17:07:32 -08:00
|
|
|
| Mcons (_, ty1, ty2, rem) ->
|
|
|
|
repr ty1 != repr ty2
|
2003-08-09 04:47:57 -07:00
|
|
|
| Mlink mem' ->
|
2003-11-06 17:07:32 -08:00
|
|
|
check_abbrev_rec !mem'
|
2003-08-09 04:47:57 -07:00
|
|
|
|
2003-11-06 17:07:32 -08:00
|
|
|
let check_memorized_abbrevs () =
|
|
|
|
List.for_all (fun mem -> check_abbrev_rec !mem) !memo
|
|
|
|
*)
|
1999-11-30 08:07:38 -08:00
|
|
|
|
|
|
|
(**********************************)
|
|
|
|
(* Utilities for labels *)
|
|
|
|
(**********************************)
|
|
|
|
|
|
|
|
let is_optional l =
|
|
|
|
String.length l > 0 && l.[0] = '?'
|
|
|
|
|
|
|
|
let label_name l =
|
|
|
|
if is_optional l then String.sub l 1 (String.length l - 1)
|
|
|
|
else l
|
|
|
|
|
|
|
|
let rec extract_label_aux hd l = function
|
|
|
|
[] -> raise Not_found
|
|
|
|
| (l',t as p) :: ls ->
|
|
|
|
if label_name l' = l then (l', t, List.rev hd, ls)
|
|
|
|
else extract_label_aux (p::hd) l ls
|
|
|
|
|
|
|
|
let extract_label l ls = extract_label_aux [] l ls
|
2002-11-20 21:39:01 -08:00
|
|
|
|
|
|
|
|
|
|
|
(**********************************)
|
|
|
|
(* Utilities for backtracking *)
|
|
|
|
(**********************************)
|
|
|
|
|
|
|
|
type change =
|
|
|
|
Ctype of type_expr * type_desc
|
|
|
|
| Clevel of type_expr * int
|
|
|
|
| Cname of
|
|
|
|
(Path.t * type_expr list) option ref * (Path.t * type_expr list) option
|
|
|
|
| Crow of row_field option ref * row_field option
|
|
|
|
| Ckind of field_kind option ref * field_kind option
|
|
|
|
| Ccommu of commutable ref * commutable
|
|
|
|
| Cuniv of type_expr option ref * type_expr option
|
|
|
|
|
|
|
|
let undo_change = function
|
|
|
|
Ctype (ty, desc) -> ty.desc <- desc
|
|
|
|
| Clevel (ty, level) -> ty.level <- level
|
|
|
|
| Cname (r, v) -> r := v
|
|
|
|
| Crow (r, v) -> r := v
|
|
|
|
| Ckind (r, v) -> r := v
|
|
|
|
| Ccommu (r, v) -> r := v
|
|
|
|
| Cuniv (r, v) -> r := v
|
|
|
|
|
|
|
|
type changes =
|
|
|
|
Change of change * changes ref
|
|
|
|
| Unchanged
|
|
|
|
| Invalid
|
|
|
|
|
2003-02-04 00:16:17 -08:00
|
|
|
type snapshot = changes ref * int
|
2002-11-20 21:39:01 -08:00
|
|
|
|
|
|
|
let trail = Weak.create 1
|
|
|
|
let last_snapshot = ref 0
|
|
|
|
|
|
|
|
let log_change ch =
|
|
|
|
match Weak.get trail 0 with None -> ()
|
|
|
|
| Some r ->
|
|
|
|
let r' = ref Unchanged in
|
|
|
|
r := Change (ch, r');
|
|
|
|
Weak.set trail 0 (Some r')
|
|
|
|
|
|
|
|
let log_type ty =
|
|
|
|
if ty.id <= !last_snapshot then log_change (Ctype (ty, ty.desc))
|
|
|
|
let link_type ty ty' = log_type ty; ty.desc <- Tlink ty'
|
2003-11-06 17:07:32 -08:00
|
|
|
(* ; assert (check_memorized_abbrevs ()) *)
|
|
|
|
(* ; check_expans [] ty' *)
|
2002-11-20 21:39:01 -08:00
|
|
|
let set_level ty level =
|
|
|
|
if ty.id <= !last_snapshot then log_change (Clevel (ty, ty.level));
|
|
|
|
ty.level <- level
|
|
|
|
let set_univar rty ty =
|
|
|
|
log_change (Cuniv (rty, !rty)); rty := Some ty
|
2002-11-20 22:22:02 -08:00
|
|
|
let set_name nm v =
|
|
|
|
log_change (Cname (nm, !nm)); nm := v
|
2002-11-20 21:39:01 -08:00
|
|
|
let set_row_field e v =
|
|
|
|
log_change (Crow (e, !e)); e := Some v
|
|
|
|
let set_kind rk k =
|
|
|
|
log_change (Ckind (rk, !rk)); rk := Some k
|
|
|
|
let set_commu rc c =
|
|
|
|
log_change (Ccommu (rc, !rc)); rc := c
|
|
|
|
|
|
|
|
let snapshot () =
|
2003-02-04 00:16:17 -08:00
|
|
|
let old = !last_snapshot in
|
2002-11-20 21:39:01 -08:00
|
|
|
last_snapshot := !new_id;
|
2003-02-04 00:16:17 -08:00
|
|
|
match Weak.get trail 0 with Some r -> (r, old)
|
2002-11-20 21:39:01 -08:00
|
|
|
| None ->
|
|
|
|
let r = ref Unchanged in
|
|
|
|
Weak.set trail 0 (Some r);
|
2003-02-04 00:16:17 -08:00
|
|
|
(r, old)
|
2002-11-20 21:39:01 -08:00
|
|
|
|
|
|
|
let rec rev_log accu = function
|
|
|
|
Unchanged -> accu
|
|
|
|
| Invalid -> assert false
|
|
|
|
| Change (ch, next) ->
|
|
|
|
let d = !next in
|
|
|
|
next := Invalid;
|
2002-12-02 18:57:23 -08:00
|
|
|
rev_log (ch::accu) d
|
2002-11-20 21:39:01 -08:00
|
|
|
|
2003-02-04 00:16:17 -08:00
|
|
|
let backtrack (changes, old) =
|
2002-11-20 21:39:01 -08:00
|
|
|
match !changes with
|
2003-02-04 00:16:17 -08:00
|
|
|
Unchanged -> last_snapshot := old
|
2002-11-20 21:39:01 -08:00
|
|
|
| Invalid -> failwith "Btype.backtrack"
|
|
|
|
| Change _ as change ->
|
|
|
|
cleanup_abbrev ();
|
|
|
|
let backlog = rev_log [] change in
|
|
|
|
List.iter undo_change backlog;
|
2002-12-02 18:57:23 -08:00
|
|
|
changes := Unchanged;
|
2003-02-04 00:16:17 -08:00
|
|
|
last_snapshot := old;
|
2002-12-02 18:57:23 -08:00
|
|
|
Weak.set trail 0 (Some changes)
|