src/Pure/type.ML
author wenzelm
Sat Nov 21 15:49:29 2009 +0100 (2009-11-21)
changeset 33832 cff42395c246
parent 33537 06c87d2c5b5a
child 33941 40408e6b833b
permissions -rw-r--r--
explicitly mark some legacy freeze operations;
     1 (*  Title:      Pure/type.ML
     2     Author:     Tobias Nipkow, Lawrence C Paulson, and Markus Wenzel
     3 
     4 Type signatures and certified types, special treatment of type vars,
     5 matching and unification of types, extend and merge type signatures.
     6 *)
     7 
     8 signature TYPE =
     9 sig
    10   (*type signatures and certified types*)
    11   datatype decl =
    12     LogicalType of int |
    13     Abbreviation of string list * typ * bool |
    14     Nonterminal
    15   type tsig
    16   val rep_tsig: tsig ->
    17    {classes: Name_Space.T * Sorts.algebra,
    18     default: sort,
    19     types: decl Name_Space.table,
    20     log_types: string list}
    21   val empty_tsig: tsig
    22   val defaultS: tsig -> sort
    23   val logical_types: tsig -> string list
    24   val eq_sort: tsig -> sort * sort -> bool
    25   val subsort: tsig -> sort * sort -> bool
    26   val of_sort: tsig -> typ * sort -> bool
    27   val inter_sort: tsig -> sort * sort -> sort
    28   val cert_class: tsig -> class -> class
    29   val cert_sort: tsig -> sort -> sort
    30   val witness_sorts: tsig -> (typ * sort) list -> sort list -> (typ * sort) list
    31   type mode
    32   val mode_default: mode
    33   val mode_syntax: mode
    34   val mode_abbrev: mode
    35   val get_mode: Proof.context -> mode
    36   val set_mode: mode -> Proof.context -> Proof.context
    37   val restore_mode: Proof.context -> Proof.context -> Proof.context
    38   val cert_typ_mode: mode -> tsig -> typ -> typ
    39   val cert_typ: tsig -> typ -> typ
    40   val arity_number: tsig -> string -> int
    41   val arity_sorts: Pretty.pp -> tsig -> string -> sort -> sort list
    42 
    43   (*special treatment of type vars*)
    44   val strip_sorts: typ -> typ
    45   val no_tvars: typ -> typ
    46   val varify: (string * sort) list -> term -> ((string * sort) * indexname) list * term
    47   val legacy_freeze_thaw_type: typ -> typ * (typ -> typ)
    48   val legacy_freeze_type: typ -> typ
    49   val legacy_freeze_thaw: term -> term * (term -> term)
    50   val legacy_freeze: term -> term
    51 
    52   (*matching and unification*)
    53   exception TYPE_MATCH
    54   type tyenv = (sort * typ) Vartab.table
    55   val lookup: tyenv -> indexname * sort -> typ option
    56   val devar: tyenv -> typ -> typ
    57   val typ_match: tsig -> typ * typ -> tyenv -> tyenv
    58   val typ_instance: tsig -> typ * typ -> bool
    59   val raw_match: typ * typ -> tyenv -> tyenv
    60   val raw_matches: typ list * typ list -> tyenv -> tyenv
    61   val raw_instance: typ * typ -> bool
    62   exception TUNIFY
    63   val unify: tsig -> typ * typ -> tyenv * int -> tyenv * int
    64   val raw_unify: typ * typ -> tyenv -> tyenv
    65   val raw_unifys: typ list * typ list -> tyenv -> tyenv
    66   val could_unify: typ * typ -> bool
    67   val could_unifys: typ list * typ list -> bool
    68   val eq_type: tyenv -> typ * typ -> bool
    69 
    70   (*extend and merge type signatures*)
    71   val add_class: Pretty.pp -> Name_Space.naming -> binding * class list -> tsig -> tsig
    72   val hide_class: bool -> string -> tsig -> tsig
    73   val set_defsort: sort -> tsig -> tsig
    74   val add_type: Name_Space.naming -> binding * int -> tsig -> tsig
    75   val add_abbrev: Name_Space.naming -> binding * string list * typ -> tsig -> tsig
    76   val add_nonterminal: Name_Space.naming -> binding -> tsig -> tsig
    77   val hide_type: bool -> string -> tsig -> tsig
    78   val add_arity: Pretty.pp -> arity -> tsig -> tsig
    79   val add_classrel: Pretty.pp -> class * class -> tsig -> tsig
    80   val merge_tsigs: Pretty.pp -> tsig * tsig -> tsig
    81 end;
    82 
    83 structure Type: TYPE =
    84 struct
    85 
    86 (** type signatures and certified types **)
    87 
    88 (* type declarations *)
    89 
    90 datatype decl =
    91   LogicalType of int |
    92   Abbreviation of string list * typ * bool |
    93   Nonterminal;
    94 
    95 
    96 (* type tsig *)
    97 
    98 datatype tsig =
    99   TSig of {
   100     classes: Name_Space.T * Sorts.algebra,  (*order-sorted algebra of type classes*)
   101     default: sort,                          (*default sort on input*)
   102     types: decl Name_Space.table,           (*declared types*)
   103     log_types: string list};                (*logical types sorted by number of arguments*)
   104 
   105 fun rep_tsig (TSig comps) = comps;
   106 
   107 fun make_tsig (classes, default, types, log_types) =
   108   TSig {classes = classes, default = default, types = types, log_types = log_types};
   109 
   110 fun build_tsig (classes, default, types) =
   111   let
   112     val log_types =
   113       Symtab.fold (fn (c, LogicalType n) => cons (c, n) | _ => I) (snd types) []
   114       |> Library.sort (int_ord o pairself snd) |> map fst;
   115   in make_tsig (classes, default, types, log_types) end;
   116 
   117 fun map_tsig f (TSig {classes, default, types, log_types = _}) =
   118   build_tsig (f (classes, default, types));
   119 
   120 val empty_tsig =
   121   build_tsig ((Name_Space.empty "class", Sorts.empty_algebra), [], Name_Space.empty_table "type");
   122 
   123 
   124 (* classes and sorts *)
   125 
   126 fun defaultS (TSig {default, ...}) = default;
   127 fun logical_types (TSig {log_types, ...}) = log_types;
   128 
   129 fun eq_sort (TSig {classes, ...}) = Sorts.sort_eq (#2 classes);
   130 fun subsort (TSig {classes, ...}) = Sorts.sort_le (#2 classes);
   131 fun of_sort (TSig {classes, ...}) = Sorts.of_sort (#2 classes);
   132 fun inter_sort (TSig {classes, ...}) = Sorts.inter_sort (#2 classes);
   133 
   134 fun cert_class (TSig {classes, ...}) = Sorts.certify_class (#2 classes);
   135 fun cert_sort (TSig {classes, ...}) = Sorts.certify_sort (#2 classes);
   136 
   137 fun witness_sorts (TSig {classes, log_types, ...}) =
   138   Sorts.witness_sorts (#2 classes) log_types;
   139 
   140 
   141 (* certification mode *)
   142 
   143 datatype mode = Mode of {normalize: bool, logical: bool};
   144 
   145 val mode_default = Mode {normalize = true, logical = true};
   146 val mode_syntax = Mode {normalize = true, logical = false};
   147 val mode_abbrev = Mode {normalize = false, logical = false};
   148 
   149 structure Mode = Proof_Data
   150 (
   151   type T = mode;
   152   fun init _ = mode_default;
   153 );
   154 
   155 val get_mode = Mode.get;
   156 fun set_mode mode = Mode.map (K mode);
   157 fun restore_mode ctxt = set_mode (get_mode ctxt);
   158 
   159 
   160 (* lookup types *)
   161 
   162 fun undecl_type c = "Undeclared type constructor: " ^ quote c;
   163 
   164 fun lookup_type (TSig {types = (_, types), ...}) = Symtab.lookup types;
   165 
   166 
   167 (* certified types *)
   168 
   169 fun bad_nargs t = "Bad number of arguments for type constructor: " ^ quote t;
   170 
   171 local
   172 
   173 fun inst_typ env (Type (c, Ts)) = Type (c, map (inst_typ env) Ts)
   174   | inst_typ env (T as TFree (x, _)) = the_default T (AList.lookup (op =) env x)
   175   | inst_typ _ T = T;
   176 
   177 in
   178 
   179 fun cert_typ_mode (Mode {normalize, logical}) tsig ty =
   180   let
   181     fun err msg = raise TYPE (msg, [ty], []);
   182 
   183     val check_logical =
   184       if logical then fn c => err ("Illegal occurrence of syntactic type: " ^ quote c)
   185       else fn _ => ();
   186 
   187     fun cert (T as Type (c, Ts)) =
   188           let
   189             val Ts' = map cert Ts;
   190             fun nargs n = if length Ts <> n then err (bad_nargs c) else ();
   191           in
   192             (case lookup_type tsig c of
   193               SOME (LogicalType n) => (nargs n; Type (c, Ts'))
   194             | SOME (Abbreviation (vs, U, syn)) =>
   195                (nargs (length vs);
   196                 if syn then check_logical c else ();
   197                 if normalize then inst_typ (vs ~~ Ts') U
   198                 else Type (c, Ts'))
   199             | SOME Nonterminal => (nargs 0; check_logical c; T)
   200             | NONE => err (undecl_type c))
   201           end
   202       | cert (TFree (x, S)) = TFree (x, cert_sort tsig S)
   203       | cert (TVar (xi as (_, i), S)) =
   204           if i < 0 then
   205             err ("Malformed type variable: " ^ quote (Term.string_of_vname xi))
   206           else TVar (xi, cert_sort tsig S);
   207 
   208     val ty' = cert ty;
   209   in if ty = ty' then ty else ty' end;  (*avoid copying of already normal type*)
   210 
   211 val cert_typ = cert_typ_mode mode_default;
   212 
   213 end;
   214 
   215 
   216 (* type arities *)
   217 
   218 fun arity_number tsig a =
   219   (case lookup_type tsig a of
   220     SOME (LogicalType n) => n
   221   | _ => error (undecl_type a));
   222 
   223 fun arity_sorts _ tsig a [] = replicate (arity_number tsig a) []
   224   | arity_sorts pp (TSig {classes, ...}) a S = Sorts.mg_domain (#2 classes) a S
   225       handle Sorts.CLASS_ERROR err => error (Sorts.class_error pp err);
   226 
   227 
   228 
   229 (** special treatment of type vars **)
   230 
   231 (* strip_sorts *)
   232 
   233 fun strip_sorts (Type (a, Ts)) = Type (a, map strip_sorts Ts)
   234   | strip_sorts (TFree (x, _)) = TFree (x, [])
   235   | strip_sorts (TVar (xi, _)) = TVar (xi, []);
   236 
   237 
   238 (* no_tvars *)
   239 
   240 fun no_tvars T =
   241   (case Term.add_tvarsT T [] of [] => T
   242   | vs => raise TYPE ("Illegal schematic type variable(s): " ^
   243       commas_quote (map (Term.string_of_vname o #1) (rev vs)), [T], []));
   244 
   245 
   246 (* varify *)
   247 
   248 fun varify fixed t =
   249   let
   250     val fs = Term.fold_types (Term.fold_atyps
   251       (fn TFree v => if member (op =) fixed v then I else insert (op =) v | _ => I)) t [];
   252     val used = Name.context
   253       |> fold_types (fold_atyps (fn TVar ((a, _), _) => Name.declare a | _ => I)) t;
   254     val fmap = fs ~~ map (rpair 0) (#1 (Name.variants (map fst fs) used));
   255     fun thaw (f as (_, S)) =
   256       (case AList.lookup (op =) fmap f of
   257         NONE => TFree f
   258       | SOME xi => TVar (xi, S));
   259   in (fmap, map_types (map_type_tfree thaw) t) end;
   260 
   261 
   262 (* freeze_thaw: freeze TVars in a term; return the "thaw" inverse *)
   263 
   264 local
   265 
   266 fun new_name (ix, (pairs, used)) =
   267   let val v = Name.variant used (string_of_indexname ix)
   268   in ((ix, v) :: pairs, v :: used) end;
   269 
   270 fun freeze_one alist (ix, sort) =
   271   TFree (the (AList.lookup (op =) alist ix), sort)
   272     handle Option =>
   273       raise TYPE ("Failure during freezing of ?" ^ string_of_indexname ix, [], []);
   274 
   275 fun thaw_one alist (a, sort) = TVar (the (AList.lookup (op =) alist a), sort)
   276   handle Option => TFree (a, sort);
   277 
   278 in
   279 
   280 fun legacy_freeze_thaw_type T =
   281   let
   282     val used = OldTerm.add_typ_tfree_names (T, [])
   283     and tvars = map #1 (OldTerm.add_typ_tvars (T, []));
   284     val (alist, _) = List.foldr new_name ([], used) tvars;
   285   in (map_type_tvar (freeze_one alist) T, map_type_tfree (thaw_one (map swap alist))) end;
   286 
   287 val legacy_freeze_type = #1 o legacy_freeze_thaw_type;
   288 
   289 fun legacy_freeze_thaw t =
   290   let
   291     val used = OldTerm.it_term_types OldTerm.add_typ_tfree_names (t, [])
   292     and tvars = map #1 (OldTerm.it_term_types OldTerm.add_typ_tvars (t, []));
   293     val (alist, _) = List.foldr new_name ([], used) tvars;
   294   in
   295     (case alist of
   296       [] => (t, fn x => x) (*nothing to do!*)
   297     | _ => (map_types (map_type_tvar (freeze_one alist)) t,
   298       map_types (map_type_tfree (thaw_one (map swap alist)))))
   299   end;
   300 
   301 val legacy_freeze = #1 o legacy_freeze_thaw;
   302 
   303 end;
   304 
   305 
   306 
   307 (** matching and unification of types **)
   308 
   309 type tyenv = (sort * typ) Vartab.table;
   310 
   311 fun tvar_clash ixn S S' = raise TYPE ("Type variable " ^
   312   quote (Term.string_of_vname ixn) ^ " has two distinct sorts",
   313   [TVar (ixn, S), TVar (ixn, S')], []);
   314 
   315 fun lookup tye (ixn, S) =
   316   (case Vartab.lookup tye ixn of
   317     NONE => NONE
   318   | SOME (S', T) => if S = S' then SOME T else tvar_clash ixn S S');
   319 
   320 
   321 (* matching *)
   322 
   323 exception TYPE_MATCH;
   324 
   325 fun typ_match tsig =
   326   let
   327     fun match (TVar (v, S), T) subs =
   328           (case lookup subs (v, S) of
   329             NONE =>
   330               if of_sort tsig (T, S) then Vartab.update_new (v, (S, T)) subs
   331               else raise TYPE_MATCH
   332           | SOME U => if U = T then subs else raise TYPE_MATCH)
   333       | match (Type (a, Ts), Type (b, Us)) subs =
   334           if a <> b then raise TYPE_MATCH
   335           else matches (Ts, Us) subs
   336       | match (TFree x, TFree y) subs =
   337           if x = y then subs else raise TYPE_MATCH
   338       | match _ _ = raise TYPE_MATCH
   339     and matches (T :: Ts, U :: Us) subs = matches (Ts, Us) (match (T, U) subs)
   340       | matches _ subs = subs;
   341   in match end;
   342 
   343 fun typ_instance tsig (T, U) =
   344   (typ_match tsig (U, T) Vartab.empty; true) handle TYPE_MATCH => false;
   345 
   346 (*purely structural matching*)
   347 fun raw_match (TVar (v, S), T) subs =
   348       (case lookup subs (v, S) of
   349         NONE => Vartab.update_new (v, (S, T)) subs
   350       | SOME U => if U = T then subs else raise TYPE_MATCH)
   351   | raw_match (Type (a, Ts), Type (b, Us)) subs =
   352       if a <> b then raise TYPE_MATCH
   353       else raw_matches (Ts, Us) subs
   354   | raw_match (TFree x, TFree y) subs =
   355       if x = y then subs else raise TYPE_MATCH
   356   | raw_match _ _ = raise TYPE_MATCH
   357 and raw_matches (T :: Ts, U :: Us) subs = raw_matches (Ts, Us) (raw_match (T, U) subs)
   358   | raw_matches ([], []) subs = subs
   359   | raw_matches _ _ = raise TYPE_MATCH;
   360 
   361 fun raw_instance (T, U) =
   362   (raw_match (U, T) Vartab.empty; true) handle TYPE_MATCH => false;
   363 
   364 
   365 (* unification *)
   366 
   367 exception TUNIFY;
   368 
   369 (*occurs check*)
   370 fun occurs v tye =
   371   let
   372     fun occ (Type (_, Ts)) = exists occ Ts
   373       | occ (TFree _) = false
   374       | occ (TVar (w, S)) =
   375           Term.eq_ix (v, w) orelse
   376             (case lookup tye (w, S) of
   377               NONE => false
   378             | SOME U => occ U);
   379   in occ end;
   380 
   381 (*chase variable assignments; if devar returns a type var then it must be unassigned*)
   382 fun devar tye (T as TVar v) =
   383       (case lookup tye v of
   384         SOME U => devar tye U
   385       | NONE => T)
   386   | devar _ T = T;
   387 
   388 (*order-sorted unification*)
   389 fun unify (TSig {classes = (_, classes), ...}) TU (tyenv, maxidx) =
   390   let
   391     val tyvar_count = Unsynchronized.ref maxidx;
   392     fun gen_tyvar S = TVar ((Name.aT, Unsynchronized.inc tyvar_count), S);
   393 
   394     fun mg_domain a S = Sorts.mg_domain classes a S
   395       handle Sorts.CLASS_ERROR _ => raise TUNIFY;
   396 
   397     fun meet (_, []) tye = tye
   398       | meet (TVar (xi, S'), S) tye =
   399           if Sorts.sort_le classes (S', S) then tye
   400           else Vartab.update_new
   401             (xi, (S', gen_tyvar (Sorts.inter_sort classes (S', S)))) tye
   402       | meet (TFree (_, S'), S) tye =
   403           if Sorts.sort_le classes (S', S) then tye
   404           else raise TUNIFY
   405       | meet (Type (a, Ts), S) tye = meets (Ts, mg_domain a S) tye
   406     and meets (T :: Ts, S :: Ss) tye = meets (Ts, Ss) (meet (devar tye T, S) tye)
   407       | meets _ tye = tye;
   408 
   409     fun unif (ty1, ty2) tye =
   410       (case (devar tye ty1, devar tye ty2) of
   411         (T as TVar (v, S1), U as TVar (w, S2)) =>
   412           if Term.eq_ix (v, w) then
   413             if S1 = S2 then tye else tvar_clash v S1 S2
   414           else if Sorts.sort_le classes (S1, S2) then
   415             Vartab.update_new (w, (S2, T)) tye
   416           else if Sorts.sort_le classes (S2, S1) then
   417             Vartab.update_new (v, (S1, U)) tye
   418           else
   419             let val S = gen_tyvar (Sorts.inter_sort classes (S1, S2)) in
   420               Vartab.update_new (v, (S1, S)) (Vartab.update_new (w, (S2, S)) tye)
   421             end
   422       | (TVar (v, S), T) =>
   423           if occurs v tye T then raise TUNIFY
   424           else meet (T, S) (Vartab.update_new (v, (S, T)) tye)
   425       | (T, TVar (v, S)) =>
   426           if occurs v tye T then raise TUNIFY
   427           else meet (T, S) (Vartab.update_new (v, (S, T)) tye)
   428       | (Type (a, Ts), Type (b, Us)) =>
   429           if a <> b then raise TUNIFY
   430           else unifs (Ts, Us) tye
   431       | (T, U) => if T = U then tye else raise TUNIFY)
   432     and unifs (T :: Ts, U :: Us) tye = unifs (Ts, Us) (unif (T, U) tye)
   433       | unifs _ tye = tye;
   434   in (unif TU tyenv, ! tyvar_count) end;
   435 
   436 (*purely structural unification*)
   437 fun raw_unify (ty1, ty2) tye =
   438   (case (devar tye ty1, devar tye ty2) of
   439     (T as TVar (v, S1), TVar (w, S2)) =>
   440       if Term.eq_ix (v, w) then
   441         if S1 = S2 then tye else tvar_clash v S1 S2
   442       else Vartab.update_new (w, (S2, T)) tye
   443   | (TVar (v, S), T) =>
   444       if occurs v tye T then raise TUNIFY
   445       else Vartab.update_new (v, (S, T)) tye
   446   | (T, TVar (v, S)) =>
   447       if occurs v tye T then raise TUNIFY
   448       else Vartab.update_new (v, (S, T)) tye
   449   | (Type (a, Ts), Type (b, Us)) =>
   450       if a <> b then raise TUNIFY
   451       else raw_unifys (Ts, Us) tye
   452   | (T, U) => if T = U then tye else raise TUNIFY)
   453 and raw_unifys (T :: Ts, U :: Us) tye = raw_unifys (Ts, Us) (raw_unify (T, U) tye)
   454   | raw_unifys ([], []) tye = tye
   455   | raw_unifys _ _ = raise TUNIFY;
   456 
   457 (*fast unification filter*)
   458 fun could_unify (Type (a, Ts), Type (b, Us)) = a = b andalso could_unifys (Ts, Us)
   459   | could_unify (TFree (a, _), TFree (b, _)) = a = b
   460   | could_unify (TVar _, _) = true
   461   | could_unify (_, TVar _) = true
   462   | could_unify _ = false
   463 and could_unifys (T :: Ts, U :: Us) = could_unify (T, U) andalso could_unifys (Ts, Us)
   464   | could_unifys ([], []) = true
   465   | could_unifys _ = false;
   466 
   467 
   468 (*equality with respect to a type environment*)
   469 fun equal_type tye (T, T') =
   470   (case (devar tye T, devar tye T') of
   471      (Type (s, Ts), Type (s', Ts')) =>
   472        s = s' andalso ListPair.all (equal_type tye) (Ts, Ts')
   473    | (U, U') => U = U');
   474 
   475 fun eq_type tye =
   476   if Vartab.is_empty tye then op = else equal_type tye;
   477 
   478 
   479 
   480 (** extend and merge type signatures **)
   481 
   482 (* classes *)
   483 
   484 fun add_class pp naming (c, cs) tsig =
   485   tsig |> map_tsig (fn ((space, classes), default, types) =>
   486     let
   487       val cs' = map (cert_class tsig) cs
   488         handle TYPE (msg, _, _) => error msg;
   489       val (c', space') = space |> Name_Space.declare true naming c;
   490       val classes' = classes |> Sorts.add_class pp (c', cs');
   491     in ((space', classes'), default, types) end);
   492 
   493 fun hide_class fully c = map_tsig (fn ((space, classes), default, types) =>
   494   ((Name_Space.hide fully c space, classes), default, types));
   495 
   496 
   497 (* arities *)
   498 
   499 fun add_arity pp (t, Ss, S) tsig = tsig |> map_tsig (fn ((space, classes), default, types) =>
   500   let
   501     val _ =
   502       (case lookup_type tsig t of
   503         SOME (LogicalType n) => if length Ss <> n then error (bad_nargs t) else ()
   504       | SOME _ => error ("Logical type constructor expected: " ^ quote t)
   505       | NONE => error (undecl_type t));
   506     val (Ss', S') = (map (cert_sort tsig) Ss, cert_sort tsig S)
   507       handle TYPE (msg, _, _) => error msg;
   508     val classes' = classes |> Sorts.add_arities pp ((t, map (fn c' => (c', Ss')) S'));
   509   in ((space, classes'), default, types) end);
   510 
   511 
   512 (* classrel *)
   513 
   514 fun add_classrel pp rel tsig =
   515   tsig |> map_tsig (fn ((space, classes), default, types) =>
   516     let
   517       val rel' = pairself (cert_class tsig) rel
   518         handle TYPE (msg, _, _) => error msg;
   519       val classes' = classes |> Sorts.add_classrel pp rel';
   520     in ((space, classes'), default, types) end);
   521 
   522 
   523 (* default sort *)
   524 
   525 fun set_defsort S tsig = tsig |> map_tsig (fn (classes, _, types) =>
   526   (classes, cert_sort tsig S handle TYPE (msg, _, _) => error msg, types));
   527 
   528 
   529 (* types *)
   530 
   531 local
   532 
   533 fun new_decl naming (c, decl) types =
   534   #2 (Name_Space.define true naming (c, decl) types);
   535 
   536 fun map_types f = map_tsig (fn (classes, default, types) =>
   537   let
   538     val (space', tab') = f types;
   539     val _ = Name_Space.intern space' "dummy" = "dummy" orelse
   540       error "Illegal declaration of dummy type";
   541   in (classes, default, (space', tab')) end);
   542 
   543 fun syntactic types (Type (c, Ts)) =
   544       (case Symtab.lookup types c of SOME Nonterminal => true | _ => false)
   545         orelse exists (syntactic types) Ts
   546   | syntactic _ _ = false;
   547 
   548 in
   549 
   550 fun add_type naming (c, n) =
   551   if n < 0 then error ("Bad type constructor declaration " ^ quote (Binding.str_of c))
   552   else map_types (new_decl naming (c, LogicalType n));
   553 
   554 fun add_abbrev naming (a, vs, rhs) tsig = tsig |> map_types (fn types =>
   555   let
   556     fun err msg =
   557       cat_error msg ("The error(s) above occurred in type abbreviation " ^
   558         quote (Binding.str_of a));
   559     val rhs' = strip_sorts (no_tvars (cert_typ_mode mode_syntax tsig rhs))
   560       handle TYPE (msg, _, _) => err msg;
   561     val _ =
   562       (case duplicates (op =) vs of
   563         [] => []
   564       | dups => err ("Duplicate variables on lhs: " ^ commas_quote dups));
   565     val _ =
   566       (case subtract (op =) vs (map #1 (Term.add_tfreesT rhs' [])) of
   567         [] => []
   568       | extras => err ("Extra variables on rhs: " ^ commas_quote extras));
   569   in types |> new_decl naming (a, Abbreviation (vs, rhs', syntactic (#2 types) rhs')) end);
   570 
   571 fun add_nonterminal naming = map_types o new_decl naming o rpair Nonterminal;
   572 
   573 end;
   574 
   575 fun hide_type fully c = map_tsig (fn (classes, default, (space, types)) =>
   576   (classes, default, (Name_Space.hide fully c space, types)));
   577 
   578 
   579 (* merge type signatures *)
   580 
   581 fun merge_tsigs pp (tsig1, tsig2) =
   582   let
   583     val (TSig {classes = (space1, classes1), default = default1, types = types1,
   584       log_types = _}) = tsig1;
   585     val (TSig {classes = (space2, classes2), default = default2, types = types2,
   586       log_types = _}) = tsig2;
   587 
   588     val space' = Name_Space.merge (space1, space2);
   589     val classes' = Sorts.merge_algebra pp (classes1, classes2);
   590     val default' = Sorts.inter_sort classes' (default1, default2);
   591     val types' = Name_Space.merge_tables (types1, types2);
   592   in build_tsig ((space', classes'), default', types') end;
   593 
   594 end;