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