src/Pure/type.ML
author wenzelm
Sat May 29 15:05:25 2004 +0200 (2004-05-29 ago)
changeset 14830 faa4865ba1ce
parent 14790 0d984ee030a1
child 14906 2da524f3d785
permissions -rw-r--r--
removed norm_typ; improved output; refer to Pretty.pp;
     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 |
    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 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 |
    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 fun norm_sort (TSig {classes, ...}) = Sorts.norm_sort classes;
   135 
   136 fun cert_class (TSig {classes, ...}) c =
   137   if can (Graph.get_node classes) c then c
   138   else raise TYPE ("Undeclared class: " ^ quote c, [], []);
   139 
   140 fun cert_sort tsig = norm_sort tsig o map (cert_class tsig);
   141 
   142 fun witness_sorts (tsig as TSig {classes, arities, log_types, ...}) =
   143   Sorts.witness_sorts (classes, arities) log_types;
   144 
   145 
   146 (* certified types *)
   147 
   148 fun bad_nargs t = "Bad number of arguments for type constructor: " ^ quote t;
   149 fun undecl_type c = "Undeclared type constructor: " ^ quote c;
   150 
   151 local
   152 
   153 fun inst_typ tye =
   154   let
   155     fun inst (var as (v, _)) =
   156       (case assoc_string_int (tye, v) of
   157         Some U => inst_typ tye U
   158       | None => TVar var);
   159   in map_type_tvar inst end;
   160 
   161 fun norm_typ (tsig as TSig {types, ...}) ty =
   162   let
   163     val idx = Term.maxidx_of_typ ty + 1;
   164 
   165     fun norm (Type (a, Ts)) =
   166           (case Symtab.lookup (types, a) of
   167             Some (Abbreviation (vs, U), _) =>
   168               norm (inst_typ (map (rpair idx) vs ~~ Ts) (incr_tvar idx U))
   169           | _ => Type (a, map norm Ts))
   170       | norm (TFree (x, S)) = TFree (x, norm_sort tsig S)
   171       | norm (TVar (xi, S)) = TVar (xi, norm_sort tsig S);
   172 
   173     val ty' = norm ty;
   174   in if ty = ty' then ty else ty' end;  (*avoid copying of already normal type*)
   175 
   176 fun certify_typ normalize syntax tsig ty =
   177   let
   178     val TSig {types, ...} = tsig;
   179     fun err msg = raise TYPE (msg, [ty], []);
   180 
   181     fun check_sort S = (map (cert_class tsig) S; ());
   182 
   183     fun check_typ (Type (c, Ts)) =
   184           let fun nargs n = if length Ts <> n then err (bad_nargs c) else () in
   185             (case Symtab.lookup (types, c) of
   186               Some (LogicalType n, _) => nargs n
   187             | Some (Abbreviation (vs, _), _) => nargs (length vs)
   188             | Some (Nonterminal, _) => nargs 0
   189             | None => err (undecl_type c));
   190             seq check_typ Ts
   191           end
   192     | check_typ (TFree (_, S)) = check_sort S
   193     | check_typ (TVar ((x, i), S)) =
   194         if i < 0 then err ("Malformed type variable: " ^ quote (Term.string_of_vname (x, i)))
   195         else check_sort S;
   196 
   197     fun no_syntax (Type (c, Ts)) =
   198           (case Symtab.lookup (types, c) of
   199             Some (Nonterminal, _) =>
   200               err ("Illegal occurrence of syntactic type: " ^ quote c)
   201           | _ => seq no_syntax Ts)
   202       | no_syntax _ = ();
   203 
   204     val _ = check_typ ty;
   205     val ty' = if normalize orelse not syntax then norm_typ tsig ty else ty;
   206     val _ = if not syntax then no_syntax ty' else ();
   207   in ty' end;
   208 
   209 in
   210 
   211 val cert_typ         = certify_typ true false;
   212 val cert_typ_syntax  = certify_typ true true;
   213 val cert_typ_raw     = certify_typ false true;
   214 
   215 end;
   216 
   217 
   218 (** special treatment of type vars **)
   219 
   220 (* strip_sorts *)
   221 
   222 fun strip_sorts (Type (a, Ts)) = Type (a, map strip_sorts Ts)
   223   | strip_sorts (TFree (x, _)) = TFree (x, [])
   224   | strip_sorts (TVar (xi, _)) = TVar (xi, []);
   225 
   226 
   227 (* no_tvars *)
   228 
   229 fun no_tvars T =
   230   (case typ_tvars T of [] => T
   231   | vs => raise TYPE ("Illegal schematic type variable(s): " ^
   232       commas_quote (map (Term.string_of_vname o #1) vs), [T], []));
   233 
   234 
   235 (* varify, unvarify *)
   236 
   237 val varifyT = map_type_tfree (fn (a, S) => TVar ((a, 0), S));
   238 
   239 fun unvarifyT (Type (a, Ts)) = Type (a, map unvarifyT Ts)
   240   | unvarifyT (TVar ((a, 0), S)) = TFree (a, S)
   241   | unvarifyT T = T;
   242 
   243 fun varify (t, fixed) =
   244   let
   245     val fs = add_term_tfree_names (t, []) \\ fixed;
   246     val ixns = add_term_tvar_ixns (t, []);
   247     val fmap = fs ~~ map (rpair 0) (variantlist (fs, map #1 ixns))
   248     fun thaw (f as (a, S)) =
   249       (case assoc (fmap, a) of
   250         None => TFree f
   251       | Some b => TVar (b, 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 (assoc (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 (assoc (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 (tvars, ([], used));
   279   in (map_type_tvar (freeze_one alist) T, map_type_tfree (thaw_one (map swap alist))) end;
   280 
   281 fun freeze_thaw t =
   282   let
   283     val used = it_term_types add_typ_tfree_names (t, [])
   284     and tvars = map #1 (it_term_types add_typ_tvars (t, []));
   285     val (alist, _) = foldr new_name (tvars, ([], used));
   286   in
   287     (case alist of
   288       [] => (t, fn x => x) (*nothing to do!*)
   289     | _ => (map_term_types (map_type_tvar (freeze_one alist)) t,
   290       map_term_types (map_type_tfree (thaw_one (map swap alist)))))
   291   end;
   292 
   293 end;
   294 
   295 
   296 
   297 (** matching and unification of types **)
   298 
   299 (* instantiation *)
   300 
   301 fun type_of_sort pp tsig (T, S) =
   302   if of_sort tsig (T, S) then T
   303   else raise TYPE ("Type not of sort " ^ Pretty.string_of_sort pp S, [T], []);
   304 
   305 fun inst_typ_tvars pp tsig tye =
   306   let
   307     fun inst (var as (v, S)) =
   308       (case assoc_string_int (tye, v) of
   309         Some U => type_of_sort pp tsig (U, S)
   310       | None => TVar var);
   311   in map_type_tvar inst end;
   312 
   313 fun inst_term_tvars _ _ [] t = t
   314   | inst_term_tvars pp tsig tye t = map_term_types (inst_typ_tvars pp tsig tye) t;
   315 
   316 
   317 (* matching *)
   318 
   319 exception TYPE_MATCH;
   320 
   321 fun typ_match tsig =
   322   let
   323     fun match (subs, (TVar (v, S), T)) =
   324           (case Vartab.lookup (subs, v) of
   325             None => (Vartab.update_new ((v, type_of_sort Pretty.pp_undef tsig (T, S)), subs)
   326               handle TYPE _ => raise TYPE_MATCH)
   327           | Some U => if U = T then subs else raise TYPE_MATCH)
   328       | match (subs, (Type (a, Ts), Type (b, Us))) =
   329           if a <> b then raise TYPE_MATCH
   330           else foldl match (subs, Ts ~~ Us)
   331       | match (subs, (TFree x, TFree y)) =
   332           if x = y then subs else raise TYPE_MATCH
   333       | match _ = raise TYPE_MATCH;
   334   in match end;
   335 
   336 fun typ_instance tsig (T, U) =
   337   (typ_match tsig (Vartab.empty, (U, T)); true) handle TYPE_MATCH => false;
   338 
   339 
   340 (* unification *)
   341 
   342 exception TUNIFY;
   343 
   344 (*occurs_check*)
   345 fun occurs v tye =
   346   let
   347     fun occ (Type (_, Ts)) = exists occ Ts
   348       | occ (TFree _) = false
   349       | occ (TVar (w, _)) =
   350           eq_ix (v, w) orelse
   351             (case Vartab.lookup (tye, w) of
   352               None => false
   353             | Some U => occ U);
   354   in occ end;
   355 
   356 (*chase variable assignments; if devar returns a type var then it must be unassigned*)
   357 fun devar (T as TVar (v, _), tye) =
   358       (case  Vartab.lookup (tye, v) of
   359         Some U => devar (U, tye)
   360       | None => T)
   361   | devar (T, tye) = T;
   362 
   363 fun unify (tsig as TSig {classes, arities, ...}) (tyenv, maxidx) TU =
   364   let
   365     val tyvar_count = ref maxidx;
   366     fun gen_tyvar S = TVar (("'a", inc tyvar_count), S);
   367 
   368     fun mg_domain a S =
   369       Sorts.mg_domain (classes, arities) a S handle Sorts.DOMAIN _ => raise TUNIFY;
   370 
   371     fun meet ((_, []), tye) = tye
   372       | meet ((TVar (xi, S'), S), tye) =
   373           if Sorts.sort_le classes (S', S) then tye
   374           else Vartab.update_new ((xi,
   375             gen_tyvar (Sorts.inter_sort classes (S', S))), tye)
   376       | meet ((TFree (_, S'), S), tye) =
   377           if Sorts.sort_le classes (S', S) then tye
   378           else raise TUNIFY
   379       | meet ((Type (a, Ts), S), tye) = meets ((Ts, mg_domain a S), tye)
   380     and meets (([], []), tye) = tye
   381       | meets ((T :: Ts, S :: Ss), tye) =
   382           meets ((Ts, Ss), meet ((devar (T, tye), S), tye))
   383       | meets _ = sys_error "meets";
   384 
   385     fun unif ((ty1, ty2), tye) =
   386       (case (devar (ty1, tye), devar (ty2, tye)) of
   387         (T as TVar (v, S1), U as TVar (w, S2)) =>
   388           if eq_ix (v, w) then tye
   389           else if Sorts.sort_le classes (S1, S2) then
   390             Vartab.update_new ((w, T), tye)
   391           else if Sorts.sort_le classes (S2, S1) then
   392             Vartab.update_new ((v, U), tye)
   393           else
   394             let val S = gen_tyvar (Sorts.inter_sort classes (S1, S2)) in
   395               Vartab.update_new ((v, S), Vartab.update_new ((w, S), tye))
   396             end
   397       | (TVar (v, S), T) =>
   398           if occurs v tye T then raise TUNIFY
   399           else meet ((T, S), Vartab.update_new ((v, T), tye))
   400       | (T, TVar (v, S)) =>
   401           if occurs v tye T then raise TUNIFY
   402           else meet ((T, S), Vartab.update_new ((v, T), tye))
   403       | (Type (a, Ts), Type (b, Us)) =>
   404           if a <> b then raise TUNIFY
   405           else foldr unif (Ts ~~ Us, tye)
   406       | (T, U) => if T = U then tye else raise TUNIFY);
   407   in (unif (TU, tyenv), ! tyvar_count) end;
   408 
   409 (*purely structural unification *)
   410 fun raw_unify (ty1, ty2) =
   411   (unify empty_tsig (Vartab.empty, 0) (strip_sorts ty1, strip_sorts ty2); true)
   412     handle TUNIFY => false;
   413 
   414 
   415 
   416 (** extend and merge type signatures **)
   417 
   418 (* arities *)
   419 
   420 local
   421 
   422 fun err_decl t decl = error ("Illegal " ^ str_of_decl decl ^ ": " ^ quote t);
   423 
   424 fun for_classes _ None = ""
   425   | for_classes pp (Some (c1, c2)) =
   426       " for classes " ^ Pretty.string_of_classrel pp [c1, c2];
   427 
   428 fun err_conflict pp t cc (c, Ss) (c', Ss') =
   429   error ("Conflict of type arities" ^ for_classes pp cc ^ ":\n  " ^
   430     Pretty.string_of_arity pp (t, Ss, [c]) ^ " and\n  " ^
   431     Pretty.string_of_arity pp (t, Ss', [c']));
   432 
   433 fun coregular pp C t (c, Ss) ars =
   434   let
   435     fun conflict (c', Ss') =
   436       if Sorts.class_le C (c, c') andalso not (Sorts.sorts_le C (Ss, Ss')) then
   437         Some ((c, c'), (c', Ss'))
   438       else if Sorts.class_le C (c', c) andalso not (Sorts.sorts_le C (Ss', Ss)) then
   439         Some ((c', c), (c', Ss'))
   440       else None;
   441   in
   442     (case Library.get_first conflict ars of
   443       Some ((c1, c2), (c', Ss')) => err_conflict pp t (Some (c1, c2)) (c, Ss) (c', Ss')
   444     | None => (c, Ss) :: ars)
   445   end;
   446 
   447 fun insert pp C t ((c, Ss), ars) =
   448   (case assoc_string (ars, c) of
   449     None => coregular pp C t (c, Ss) ars
   450   | Some Ss' =>
   451       if Sorts.sorts_le C (Ss, Ss') then ars
   452       else if Sorts.sorts_le C (Ss', Ss)
   453       then coregular pp C t (c, Ss) (ars \ (c, Ss'))
   454       else err_conflict pp t None (c, Ss) (c, Ss'));
   455 
   456 fun complete C (c, Ss) = map (rpair Ss) (Graph.all_succs C [c]);
   457 
   458 fun insert_arities pp classes (arities, (t, ars)) =
   459   let val ars' =
   460     Symtab.lookup_multi (arities, t)
   461     |> curry (foldr (insert pp classes t)) (flat (map (complete classes) ars))
   462   in Symtab.update ((t, ars'), arities) end;
   463 
   464 fun insert_table pp classes = Symtab.foldl (fn (arities, (t, ars)) =>
   465   insert_arities pp classes (arities, (t, map (apsnd (map (Sorts.norm_sort classes))) ars)));
   466 
   467 in
   468 
   469 fun add_arities pp decls tsig = tsig |> change_tsig (fn (classes, default, types, arities) =>
   470   let
   471     fun prep (t, Ss, S) =
   472       (case Symtab.lookup (types, t) of
   473         Some (LogicalType n, _) =>
   474           if length Ss = n then
   475             (t, map (cert_sort tsig) Ss, cert_sort tsig S)
   476               handle TYPE (msg, _, _) => error msg
   477           else error (bad_nargs t)
   478       | Some (decl, _) => err_decl t decl
   479       | None => error (undecl_type t));
   480 
   481     val ars = decls |> map ((fn (t, Ss, S) => (t, map (fn c => (c, Ss)) S)) o prep);
   482     val arities' = foldl (insert_arities pp classes) (arities, ars);
   483   in (classes, default, types, arities') end);
   484 
   485 fun rebuild_arities pp classes arities =
   486   insert_table pp classes (Symtab.empty, arities);
   487 
   488 fun merge_arities pp classes (arities1, arities2) =
   489   insert_table pp classes (insert_table pp classes (Symtab.empty, arities1), arities2);
   490 
   491 end;
   492 
   493 
   494 (* classes *)
   495 
   496 local
   497 
   498 fun err_dup_classes cs =
   499   error ("Duplicate declaration of class(es): " ^ commas_quote cs);
   500 
   501 fun err_cyclic_classes pp css =
   502   error (cat_lines (map (fn cs =>
   503     "Cycle in class relation: " ^ Pretty.string_of_classrel pp cs) css));
   504 
   505 fun add_class pp (c, cs) tsig = tsig |> change_tsig (fn (classes, default, types, arities) =>
   506   let
   507     val cs' = map (cert_class tsig) cs
   508       handle TYPE (msg, _, _) => error msg;
   509     val classes' = classes |> Graph.new_node (c, stamp ())
   510       handle Graph.DUP d => err_dup_classes [d];
   511     val classes'' = classes' |> fold Graph.add_edge_trans_acyclic (map (pair c) cs')
   512       handle Graph.CYCLES css => err_cyclic_classes pp css;
   513   in (classes'', default, types, arities) end);
   514 
   515 in
   516 
   517 val add_classes = fold o add_class;
   518 
   519 fun add_classrel pp ps tsig = tsig |> change_tsig (fn (classes, default, types, arities) =>
   520   let
   521     val ps' = map (pairself (cert_class tsig)) ps
   522       handle TYPE (msg, _, _) => error msg;
   523     val classes' = classes |> fold Graph.add_edge_trans_acyclic ps'
   524       handle Graph.CYCLES css => err_cyclic_classes pp css;
   525     val default' = default |> Sorts.norm_sort classes';
   526     val arities' = arities |> rebuild_arities pp classes';
   527   in (classes', default', types, arities') end);
   528 
   529 fun merge_classes pp CC = Graph.merge_trans_acyclic (op =) CC
   530   handle Graph.DUPS cs => err_dup_classes cs
   531     | Graph.CYCLES css => err_cyclic_classes pp css;
   532 
   533 end;
   534 
   535 
   536 (* default sort *)
   537 
   538 fun set_defsort S tsig = tsig |> change_tsig (fn (classes, _, types, arities) =>
   539   (classes, cert_sort tsig S handle TYPE (msg, _, _) => error msg, types, arities));
   540 
   541 
   542 (* types *)
   543 
   544 local
   545 
   546 fun err_neg_args c =
   547   error ("Negative number of arguments in type constructor declaration: " ^ quote c);
   548 
   549 fun err_in_decls c decl decl' =
   550   let
   551     val s = str_of_decl decl;
   552     val s' = str_of_decl decl';
   553   in
   554     if s = s' then
   555       error ("Duplicate declaration of " ^ s ^ ": " ^ quote c)
   556     else error ("Conflict of " ^ s ^ " with " ^ s' ^ ": " ^ quote c)
   557   end;
   558 
   559 fun new_decl (c, decl) types =
   560   (case Symtab.lookup (types, c) of
   561     Some (decl', _) => err_in_decls c decl decl'
   562   | None => Symtab.update ((c, (decl, stamp ())), types));
   563 
   564 fun the_decl types c = fst (the (Symtab.lookup (types, c)));
   565 
   566 fun change_types f = change_tsig (fn (classes, default, types, arities) =>
   567   (classes, default, f types, arities));
   568 
   569 fun add_abbr (a, vs, rhs) tsig = tsig |> change_types (fn types =>
   570   let
   571     fun err msg =
   572       error (msg ^ "\nThe error(s) above occurred in type abbreviation " ^ quote a);
   573     val rhs' = strip_sorts (varifyT (no_tvars (cert_typ_syntax tsig rhs)))
   574       handle TYPE (msg, _, _) => err msg;
   575   in
   576     (case duplicates vs of
   577       [] => []
   578     | dups => err ("Duplicate variables on lhs: " ^ commas_quote dups));
   579     (case gen_rems (op =) (map (#1 o #1) (typ_tvars rhs'), vs) of
   580       [] => []
   581     | extras => err ("Extra variables on rhs: " ^ commas_quote extras));
   582     types |> new_decl (a, Abbreviation (vs, rhs'))
   583   end);
   584 
   585 in
   586 
   587 fun add_types ps = change_types (fold new_decl (ps |> map (fn (c, n) =>
   588   if n < 0 then err_neg_args c else (c, LogicalType n))));
   589 
   590 val add_abbrs = fold add_abbr;
   591 val add_nonterminals = change_types o fold new_decl o map (rpair Nonterminal);
   592 
   593 fun merge_types (types1, types2) =
   594   Symtab.merge Library.eq_snd (types1, types2) handle Symtab.DUPS (d :: _) =>
   595     err_in_decls d (the_decl types1 d) (the_decl types2 d);
   596 
   597 end;
   598 
   599 
   600 (* merge type signatures *)
   601 
   602 fun merge_tsigs pp (tsig1, tsig2) =
   603   let
   604     val (TSig {classes = classes1, default = default1, types = types1, arities = arities1,
   605       log_types = _, witness = _}) = tsig1;
   606     val (TSig {classes = classes2, default = default2, types = types2, arities = arities2,
   607       log_types = _, witness = _}) = tsig2;
   608 
   609     val classes' = merge_classes pp (classes1, classes2);
   610     val default' = Sorts.inter_sort classes' (default1, default2);
   611     val types' = merge_types (types1, types2);
   612     val arities' = merge_arities pp classes' (arities1, arities2);
   613   in build_tsig (classes', default', types', arities') end;
   614 
   615 end;