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