src/Pure/type.ML
author wenzelm
Fri Jun 17 18:33:29 2005 +0200 (2005-06-17 ago)
changeset 16444 80c8f742c6fc
parent 16370 033d890fe91f
child 16650 bd4f7149ba1e
permissions -rw-r--r--
Symtab.fold;
     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.classes,
    19     default: sort,
    20     types: (decl * stamp) NameSpace.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_abbrev: 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_type: typ -> typ
    47   val freeze_thaw: term -> term * (term -> term)
    48   val freeze: term -> term
    49 
    50   (*matching and unification*)
    51   exception TYPE_MATCH
    52   type tyenv
    53   val lookup: tyenv * (indexname * sort) -> typ option
    54   val typ_match: tsig -> tyenv * (typ * typ) -> tyenv
    55   val typ_instance: tsig -> typ * typ -> bool
    56   exception TUNIFY
    57   val unify: tsig -> tyenv * int -> typ * typ -> tyenv * int
    58   val raw_unify: typ * typ -> bool
    59 
    60   (*extend and merge type signatures*)
    61   val add_classes: Pretty.pp -> NameSpace.naming -> (bstring * class list) list -> tsig -> tsig
    62   val hide_classes: bool -> string list -> tsig -> tsig
    63   val add_classrel: Pretty.pp -> (class * class) list -> tsig -> tsig
    64   val set_defsort: sort -> tsig -> tsig
    65   val add_types: NameSpace.naming -> (bstring * int) list -> tsig -> tsig
    66   val add_abbrevs: NameSpace.naming -> (string * string list * typ) list -> tsig -> tsig
    67   val add_nonterminals: NameSpace.naming -> string list -> tsig -> tsig
    68   val hide_types: bool -> string list -> tsig -> tsig
    69   val add_arities: Pretty.pp -> arity list -> tsig -> tsig
    70   val merge_tsigs: Pretty.pp -> tsig * tsig -> tsig
    71 end;
    72 
    73 structure Type: TYPE =
    74 struct
    75 
    76 (** type signatures and certified types **)
    77 
    78 (* type declarations *)
    79 
    80 datatype decl =
    81   LogicalType of int |
    82   Abbreviation of string list * typ * bool |
    83   Nonterminal;
    84 
    85 fun str_of_decl (LogicalType _) = "logical type constructor"
    86   | str_of_decl (Abbreviation _) = "type abbreviation"
    87   | str_of_decl Nonterminal = "syntactic type";
    88 
    89 
    90 (* type tsig *)
    91 
    92 datatype tsig =
    93   TSig of {
    94     classes: NameSpace.T * Sorts.classes,   (*declared classes with proper subclass relation*)
    95     default: sort,                          (*default sort on input*)
    96     types: (decl * stamp) NameSpace.table,  (*declared types*)
    97     arities: Sorts.arities,                 (*image specification of types wrt. sorts*)
    98     log_types: string list,                 (*logical types sorted by number of arguments*)
    99     witness: (typ * sort) option};          (*witness for non-emptiness of strictest sort*)
   100 
   101 fun rep_tsig (TSig comps) = comps;
   102 
   103 fun make_tsig (classes, default, types, arities, log_types, witness) =
   104   TSig {classes = classes, default = default, types = types, arities = arities,
   105     log_types = log_types, witness = witness};
   106 
   107 fun build_tsig (classes, default, types, arities) =
   108   let
   109     val log_types =
   110       Symtab.fold (fn (c, (LogicalType n, _)) => cons (c, n) | _ => I) (#2 types) []
   111       |> Library.sort (Library.int_ord o pairself #2) |> map #1;
   112     val witness =
   113       (case Sorts.witness_sorts (#2 classes, arities) log_types [] [Graph.keys (#2 classes)] of
   114         [w] => SOME w | _ => NONE);
   115   in make_tsig (classes, default, types, arities, log_types, witness) end;
   116 
   117 fun map_tsig f (TSig {classes, default, types, arities, log_types = _, witness = _}) =
   118   build_tsig (f (classes, default, types, arities));
   119 
   120 val empty_tsig =
   121   build_tsig ((NameSpace.empty, Graph.empty), [], NameSpace.empty_table, 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 (#2 classes);
   132 fun subsort (TSig {classes, ...}) = Sorts.sort_le (#2 classes);
   133 fun of_sort (TSig {classes, arities, ...}) = Sorts.of_sort (#2 classes, arities);
   134 
   135 fun cert_class (TSig {classes, ...}) = Sorts.certify_class (#2 classes);
   136 fun cert_sort (TSig {classes, ...}) = Sorts.certify_sort (#2 classes);
   137 
   138 fun witness_sorts (tsig as TSig {classes, arities, log_types, ...}) =
   139   Sorts.witness_sorts (#2 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, _)) = if_none (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 = (_, classes), types = (_, 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_abbrev = 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 (the (assoc_string_int (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 (the (assoc_string (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 val freeze_type = #1 o freeze_thaw_type;
   259 
   260 fun freeze_thaw t =
   261   let
   262     val used = it_term_types add_typ_tfree_names (t, [])
   263     and tvars = map #1 (it_term_types add_typ_tvars (t, []));
   264     val (alist, _) = foldr new_name ([], used) tvars;
   265   in
   266     (case alist of
   267       [] => (t, fn x => x) (*nothing to do!*)
   268     | _ => (map_term_types (map_type_tvar (freeze_one alist)) t,
   269       map_term_types (map_type_tfree (thaw_one (map swap alist)))))
   270   end;
   271 
   272 val freeze = #1 o freeze_thaw;
   273 
   274 end;
   275 
   276 
   277 
   278 (** matching and unification of types **)
   279 
   280 type tyenv = (sort * typ) Vartab.table;
   281 
   282 fun tvar_clash ixn S S' = raise TYPE ("Type variable " ^
   283   quote (Term.string_of_vname ixn) ^ " has two distinct sorts",
   284   [TVar (ixn, S), TVar (ixn, S')], []);
   285 
   286 fun lookup (tye, (ixn, S)) =
   287   (case Vartab.lookup (tye, ixn) of
   288     NONE => NONE
   289   | SOME (S', T) => if S = S' then SOME T else tvar_clash ixn S S');
   290 
   291 
   292 (* matching *)
   293 
   294 exception TYPE_MATCH;
   295 
   296 fun typ_match tsig (tyenv, TU) =
   297   let
   298     fun match (TVar (v, S), T) subs =
   299           (case lookup (subs, (v, S)) of
   300             NONE =>
   301               if of_sort tsig (T, S) then Vartab.update_new ((v, (S, T)), subs)
   302               else raise TYPE_MATCH
   303           | SOME U => if U = T then subs else raise TYPE_MATCH)
   304       | match (Type (a, Ts), Type (b, Us)) subs =
   305           if a <> b then raise TYPE_MATCH
   306           else fold match (Ts ~~ Us) subs
   307       | match (TFree x, TFree y) subs =
   308           if x = y then subs else raise TYPE_MATCH
   309       | match _ _ = raise TYPE_MATCH;
   310   in match TU tyenv end;
   311 
   312 fun typ_instance tsig (T, U) =
   313   (typ_match tsig (Vartab.empty, (U, T)); true) handle TYPE_MATCH => false;
   314 
   315 
   316 (* unification *)
   317 
   318 exception TUNIFY;
   319 
   320 (*occurs_check*)
   321 fun occurs v tye =
   322   let
   323     fun occ (Type (_, Ts)) = exists occ Ts
   324       | occ (TFree _) = false
   325       | occ (TVar (w, S)) =
   326           eq_ix (v, w) orelse
   327             (case lookup (tye, (w, S)) of
   328               NONE => false
   329             | SOME U => occ U);
   330   in occ end;
   331 
   332 (*chase variable assignments; if devar returns a type var then it must be unassigned*)
   333 fun devar (T as TVar v, tye) =
   334       (case  lookup (tye, v) of
   335         SOME U => devar (U, tye)
   336       | NONE => T)
   337   | devar (T, tye) = T;
   338 
   339 fun unify (tsig as TSig {classes = (_, classes), arities, ...}) (tyenv, maxidx) TU =
   340   let
   341     val tyvar_count = ref maxidx;
   342     fun gen_tyvar S = TVar (("'a", inc tyvar_count), S);
   343 
   344     fun mg_domain a S =
   345       Sorts.mg_domain (classes, arities) a S handle Sorts.DOMAIN _ => raise TUNIFY;
   346 
   347     fun meet ((_, []), tye) = tye
   348       | meet ((TVar (xi, S'), S), tye) =
   349           if Sorts.sort_le classes (S', S) then tye
   350           else Vartab.update_new ((xi, (S',
   351             gen_tyvar (Sorts.inter_sort classes (S', S)))), tye)
   352       | meet ((TFree (_, S'), S), tye) =
   353           if Sorts.sort_le classes (S', S) then tye
   354           else raise TUNIFY
   355       | meet ((Type (a, Ts), S), tye) = meets ((Ts, mg_domain a S), tye)
   356     and meets (([], []), tye) = tye
   357       | meets ((T :: Ts, S :: Ss), tye) =
   358           meets ((Ts, Ss), meet ((devar (T, tye), S), tye))
   359       | meets _ = sys_error "meets";
   360 
   361     fun unif ((ty1, ty2), tye) =
   362       (case (devar (ty1, tye), devar (ty2, tye)) of
   363         (T as TVar (v, S1), U as TVar (w, S2)) =>
   364           if eq_ix (v, w) then
   365             if S1 = S2 then tye else tvar_clash v S1 S2
   366           else if Sorts.sort_le classes (S1, S2) then
   367             Vartab.update_new ((w, (S2, T)), tye)
   368           else if Sorts.sort_le classes (S2, S1) then
   369             Vartab.update_new ((v, (S1, U)), tye)
   370           else
   371             let val S = gen_tyvar (Sorts.inter_sort classes (S1, S2)) in
   372               Vartab.update_new ((v, (S1, S)), Vartab.update_new ((w, (S2, S)), tye))
   373             end
   374       | (TVar (v, S), T) =>
   375           if occurs v tye T then raise TUNIFY
   376           else meet ((T, S), Vartab.update_new ((v, (S, T)), tye))
   377       | (T, TVar (v, S)) =>
   378           if occurs v tye T then raise TUNIFY
   379           else meet ((T, S), Vartab.update_new ((v, (S, T)), tye))
   380       | (Type (a, Ts), Type (b, Us)) =>
   381           if a <> b then raise TUNIFY
   382           else foldr unif tye (Ts ~~ Us)
   383       | (T, U) => if T = U then tye else raise TUNIFY);
   384   in (unif (TU, tyenv), ! tyvar_count) end;
   385 
   386 (*purely structural unification *)
   387 fun raw_unify (ty1, ty2) =
   388   (unify empty_tsig (Vartab.empty, 0) (strip_sorts ty1, strip_sorts ty2); true)
   389     handle TUNIFY => false;
   390 
   391 
   392 
   393 (** extend and merge type signatures **)
   394 
   395 (* arities *)
   396 
   397 local
   398 
   399 fun err_decl t decl = error ("Illegal " ^ str_of_decl decl ^ ": " ^ quote t);
   400 
   401 fun for_classes _ NONE = ""
   402   | for_classes pp (SOME (c1, c2)) =
   403       " for classes " ^ Pretty.string_of_classrel pp [c1, c2];
   404 
   405 fun err_conflict pp t cc (c, Ss) (c', Ss') =
   406   error ("Conflict of type arities" ^ for_classes pp cc ^ ":\n  " ^
   407     Pretty.string_of_arity pp (t, Ss, [c]) ^ " and\n  " ^
   408     Pretty.string_of_arity pp (t, Ss', [c']));
   409 
   410 fun coregular pp C t (c, Ss) ars =
   411   let
   412     fun conflict (c', Ss') =
   413       if Sorts.class_le C (c, c') andalso not (Sorts.sorts_le C (Ss, Ss')) then
   414         SOME ((c, c'), (c', Ss'))
   415       else if Sorts.class_le C (c', c) andalso not (Sorts.sorts_le C (Ss', Ss)) then
   416         SOME ((c', c), (c', Ss'))
   417       else NONE;
   418   in
   419     (case Library.get_first conflict ars of
   420       SOME ((c1, c2), (c', Ss')) => err_conflict pp t (SOME (c1, c2)) (c, Ss) (c', Ss')
   421     | NONE => (c, Ss) :: ars)
   422   end;
   423 
   424 fun insert pp C t (c, Ss) ars =
   425   (case assoc_string (ars, c) of
   426     NONE => coregular pp C t (c, Ss) ars
   427   | SOME Ss' =>
   428       if Sorts.sorts_le C (Ss, Ss') then ars
   429       else if Sorts.sorts_le C (Ss', Ss)
   430       then coregular pp C t (c, Ss) (ars \ (c, Ss'))
   431       else err_conflict pp t NONE (c, Ss) (c, Ss'));
   432 
   433 fun complete C (c, Ss) = map (rpair Ss) (Graph.all_succs C [c]);
   434 
   435 fun insert_arities pp classes (t, ars) arities =
   436   let val ars' =
   437     Symtab.lookup_multi (arities, t)
   438     |> fold_rev (fold_rev (insert pp classes t)) (map (complete classes) ars)
   439   in Symtab.update ((t, ars'), arities) end;
   440 
   441 fun insert_table pp classes = Symtab.fold (fn (t, ars) =>
   442   insert_arities pp classes (t, map (apsnd (map (Sorts.norm_sort classes))) ars));
   443 
   444 in
   445 
   446 fun add_arities pp decls tsig = tsig |> map_tsig (fn (classes, default, types, arities) =>
   447   let
   448     fun prep (t, Ss, S) =
   449       (case Symtab.lookup (#2 types, t) of
   450         SOME (LogicalType n, _) =>
   451           if length Ss = n then
   452             (t, map (cert_sort tsig) Ss, cert_sort tsig S)
   453               handle TYPE (msg, _, _) => error msg
   454           else error (bad_nargs t)
   455       | SOME (decl, _) => err_decl t decl
   456       | NONE => error (undecl_type t));
   457 
   458     val ars = decls |> map ((fn (t, Ss, S) => (t, map (fn c => (c, Ss)) S)) o prep);
   459     val arities' = fold (insert_arities pp (#2 classes)) ars arities;
   460   in (classes, default, types, arities') end);
   461 
   462 fun rebuild_arities pp classes arities =
   463   Symtab.empty
   464   |> insert_table pp classes arities;
   465 
   466 fun merge_arities pp classes (arities1, arities2) =
   467   Symtab.empty
   468   |> insert_table pp classes arities1
   469   |> insert_table pp classes arities2;
   470 
   471 end;
   472 
   473 
   474 (* classes *)
   475 
   476 local
   477 
   478 fun err_dup_classes cs =
   479   error ("Duplicate declaration of class(es): " ^ commas_quote cs);
   480 
   481 fun err_cyclic_classes pp css =
   482   error (cat_lines (map (fn cs =>
   483     "Cycle in class relation: " ^ Pretty.string_of_classrel pp cs) css));
   484 
   485 fun add_class pp naming (c, cs) tsig =
   486   tsig |> map_tsig (fn ((space, classes), default, types, arities) =>
   487     let
   488       val c' = NameSpace.full naming c;
   489       val cs' = map (cert_class tsig) cs
   490         handle TYPE (msg, _, _) => error msg;
   491       val space' = space |> NameSpace.declare naming c';
   492       val classes' = classes |> Graph.new_node (c', stamp ())
   493         handle Graph.DUP dup => err_dup_classes [dup];
   494       val classes'' = classes' |> fold Graph.add_edge_trans_acyclic (map (pair c') cs')
   495         handle Graph.CYCLES css => err_cyclic_classes pp css;
   496     in ((space', classes''), default, types, arities) end);
   497 
   498 in
   499 
   500 val add_classes = fold oo add_class;
   501 
   502 fun add_classrel pp ps tsig =
   503   tsig |> map_tsig (fn ((space, classes), default, types, arities) =>
   504     let
   505       val ps' = map (pairself (cert_class tsig)) ps
   506         handle TYPE (msg, _, _) => error msg;
   507       val classes' = classes |> fold Graph.add_edge_trans_acyclic ps'
   508         handle Graph.CYCLES css => err_cyclic_classes pp css;
   509       val default' = default |> Sorts.norm_sort classes';
   510       val arities' = arities |> rebuild_arities pp classes';
   511     in ((space, classes'), default', types, arities') end);
   512 
   513 fun merge_classes pp ((space1, classes1), (space2, classes2)) =
   514   let
   515     val space = NameSpace.merge (space1, space2);
   516     val classes =
   517       Graph.merge_trans_acyclic (op =) (classes1, classes2)
   518         handle Graph.DUPS cs => err_dup_classes cs
   519           | Graph.CYCLES css => err_cyclic_classes pp css;
   520   in (space, classes) end;    
   521 
   522 end;
   523 
   524 fun hide_classes fully cs = map_tsig (fn ((space, classes), default, types, arities) =>
   525   ((fold (NameSpace.hide fully) cs space, classes), default, types, arities));
   526 
   527 
   528 (* default sort *)
   529 
   530 fun set_defsort S tsig = tsig |> map_tsig (fn (classes, _, types, arities) =>
   531   (classes, cert_sort tsig S handle TYPE (msg, _, _) => error msg, types, arities));
   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, stamp ())), types));
   555   in (space', types') end;
   556 
   557 fun the_decl (_, types) c = fst (the (Symtab.lookup (types, c)));
   558 
   559 fun change_types f = map_tsig (fn (classes, default, types, arities) =>
   560   (classes, default, f types, arities));
   561 
   562 fun syntactic types (Type (c, Ts)) =
   563       (case Symtab.lookup (types, c) of SOME (Nonterminal, _) => true | _ => false)
   564         orelse exists (syntactic types) Ts
   565   | syntactic _ _ = false;
   566 
   567 fun add_abbrev naming (a, vs, rhs) tsig = tsig |> change_types (fn types =>
   568   let
   569     fun err msg =
   570       error (msg ^ "\nThe error(s) above occurred in type abbreviation: " ^ quote a);
   571     val rhs' = compress_type (strip_sorts (no_tvars (cert_typ_syntax tsig rhs)))
   572       handle TYPE (msg, _, _) => err msg;
   573   in
   574     (case duplicates vs of
   575       [] => []
   576     | dups => err ("Duplicate variables on lhs: " ^ commas_quote dups));
   577     (case gen_rems (op =) (map (#1 o #1) (typ_tvars rhs'), vs) of
   578       [] => []
   579     | extras => err ("Extra variables on rhs: " ^ commas_quote extras));
   580     types |> new_decl naming (a, Abbreviation (vs, rhs', syntactic (#2 types) rhs'))
   581   end);
   582 
   583 in
   584 
   585 fun add_types naming ps = change_types (fold (new_decl naming) (ps |> map (fn (c, n) =>
   586   if n < 0 then err_neg_args c else (c, LogicalType n))));
   587 
   588 val add_abbrevs = fold o add_abbrev;
   589 
   590 fun add_nonterminals naming = change_types o fold (new_decl naming) o map (rpair Nonterminal);
   591 
   592 fun merge_types (types1, types2) =
   593   NameSpace.merge_tables Library.eq_snd (types1, types2) handle Symtab.DUPS (d :: _) =>
   594     err_in_decls d (the_decl types1 d) (the_decl types2 d);
   595 
   596 end;
   597 
   598 fun hide_types fully cs = map_tsig (fn (classes, default, (space, types), arities) =>
   599   (classes, default, (fold (NameSpace.hide fully) cs space, types), arities));
   600 
   601 
   602 (* merge type signatures *)
   603 
   604 fun merge_tsigs pp (tsig1, tsig2) =
   605   let
   606     val (TSig {classes = classes1, default = default1, types = types1, arities = arities1,
   607       log_types = _, witness = _}) = tsig1;
   608     val (TSig {classes = classes2, default = default2, types = types2, arities = arities2,
   609       log_types = _, witness = _}) = tsig2;
   610 
   611     val classes' = merge_classes pp (classes1, classes2);
   612     val default' = Sorts.inter_sort (#2 classes') (default1, default2);
   613     val types' = merge_types (types1, types2);
   614     val arities' = merge_arities pp (#2 classes') (arities1, arities2);
   615   in build_tsig (classes', default', types', arities') end;
   616 
   617 end;