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