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