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