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