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