src/Pure/type.ML
author wenzelm
Thu, 09 Jun 2011 16:34:49 +0200
changeset 43324 2b47822868e4
parent 42468 aea61c5f88c3
child 43326 47cf4bc789aa
permissions -rw-r--r--
discontinued Name.variant to emphasize that this is old-style / indirect;

(*  Title:      Pure/type.ML
    Author:     Tobias Nipkow, Lawrence C Paulson, and Markus Wenzel

Type signatures and certified types, special treatment of type vars,
matching and unification of types, extend and merge type signatures.
*)

signature TYPE =
sig
  (*constraints*)
  val mark_polymorphic: typ -> typ
  val constraint: typ -> term -> term
  val strip_constraints: term -> term
  val appl_error: Proof.context -> term -> typ -> term -> typ -> string
  (*type signatures and certified types*)
  datatype decl =
    LogicalType of int |
    Abbreviation of string list * typ * bool |
    Nonterminal
  type tsig
  val eq_tsig: tsig * tsig -> bool
  val rep_tsig: tsig ->
   {classes: Name_Space.T * Sorts.algebra,
    default: sort,
    types: decl Name_Space.table,
    log_types: string list}
  val empty_tsig: tsig
  val class_space: tsig -> Name_Space.T
  val class_alias: Name_Space.naming -> binding -> string -> tsig -> tsig
  val intern_class: tsig -> xstring -> string
  val extern_class: Proof.context -> tsig -> string -> xstring
  val defaultS: tsig -> sort
  val logical_types: tsig -> string list
  val eq_sort: tsig -> sort * sort -> bool
  val subsort: tsig -> sort * sort -> bool
  val of_sort: tsig -> typ * sort -> bool
  val inter_sort: tsig -> sort * sort -> sort
  val cert_class: tsig -> class -> class
  val cert_sort: tsig -> sort -> sort
  val minimize_sort: tsig -> sort -> sort
  val witness_sorts: tsig -> (typ * sort) list -> sort list -> (typ * sort) list
  type mode
  val mode_default: mode
  val mode_syntax: mode
  val mode_abbrev: mode
  val get_mode: Proof.context -> mode
  val set_mode: mode -> Proof.context -> Proof.context
  val restore_mode: Proof.context -> Proof.context -> Proof.context
  val type_space: tsig -> Name_Space.T
  val type_alias: Name_Space.naming -> binding -> string -> tsig -> tsig
  val intern_type: tsig -> xstring -> string
  val extern_type: Proof.context -> tsig -> string -> xstring
  val is_logtype: tsig -> string -> bool
  val the_decl: tsig -> string * Position.T -> decl
  val cert_typ_mode: mode -> tsig -> typ -> typ
  val cert_typ: tsig -> typ -> typ
  val arity_number: tsig -> string -> int
  val arity_sorts: Context.pretty -> tsig -> string -> sort -> sort list

  (*special treatment of type vars*)
  val sort_of_atyp: typ -> sort
  val strip_sorts: typ -> typ
  val no_tvars: typ -> typ
  val varify_global: (string * sort) list -> term -> ((string * sort) * indexname) list * term
  val legacy_freeze_thaw_type: typ -> typ * (typ -> typ)
  val legacy_freeze_type: typ -> typ
  val legacy_freeze_thaw: term -> term * (term -> term)
  val legacy_freeze: term -> term

  (*matching and unification*)
  exception TYPE_MATCH
  type tyenv = (sort * typ) Vartab.table
  val lookup: tyenv -> indexname * sort -> typ option
  val devar: tyenv -> typ -> typ
  val typ_match: tsig -> typ * typ -> tyenv -> tyenv
  val typ_instance: tsig -> typ * typ -> bool
  val raw_match: typ * typ -> tyenv -> tyenv
  val raw_matches: typ list * typ list -> tyenv -> tyenv
  val raw_instance: typ * typ -> bool
  exception TUNIFY
  val unify: tsig -> typ * typ -> tyenv * int -> tyenv * int
  val raw_unify: typ * typ -> tyenv -> tyenv
  val raw_unifys: typ list * typ list -> tyenv -> tyenv
  val could_unify: typ * typ -> bool
  val could_unifys: typ list * typ list -> bool
  val eq_type: tyenv -> typ * typ -> bool

  (*extend and merge type signatures*)
  val add_class: Proof.context -> Name_Space.naming -> binding * class list -> tsig -> tsig
  val hide_class: bool -> string -> tsig -> tsig
  val set_defsort: sort -> tsig -> tsig
  val add_type: Proof.context -> Name_Space.naming -> binding * int -> tsig -> tsig
  val add_abbrev: Proof.context -> Name_Space.naming -> binding * string list * typ -> tsig -> tsig
  val add_nonterminal: Proof.context -> Name_Space.naming -> binding -> tsig -> tsig
  val hide_type: bool -> string -> tsig -> tsig
  val add_arity: Proof.context -> arity -> tsig -> tsig
  val add_classrel: Proof.context -> class * class -> tsig -> tsig
  val merge_tsig: Proof.context -> tsig * tsig -> tsig
end;

structure Type: TYPE =
struct

(** constraints **)

(*indicate polymorphic Vars*)
fun mark_polymorphic T = Type ("_polymorphic_", [T]);

fun constraint T t =
  if T = dummyT then t
  else Const ("_type_constraint_", T --> T) $ t;

fun strip_constraints (Const ("_type_constraint_", _) $ t) = strip_constraints t
  | strip_constraints (t $ u) = strip_constraints t $ strip_constraints u
  | strip_constraints (Abs (x, T, t)) = Abs (x, T, strip_constraints t)
  | strip_constraints a = a;

fun appl_error ctxt (Const ("_type_constraint_", Type ("fun", [T, _]))) _ u U =
      cat_lines
       ["Failed to meet type constraint:", "",
        Pretty.string_of (Pretty.block
         [Pretty.str "Term:", Pretty.brk 2, Syntax.pretty_term ctxt u,
          Pretty.str " ::", Pretty.brk 1, Syntax.pretty_typ ctxt U]),
        Pretty.string_of (Pretty.block
         [Pretty.str "Type:", Pretty.brk 2, Syntax.pretty_typ ctxt T])]
  | appl_error ctxt t T u U =
      cat_lines
       ["Type error in application: " ^
          (case T of
            Type ("fun", _) => "incompatible operand type"
          | _ => "operator not of function type"),
        "",
        Pretty.string_of (Pretty.block
          [Pretty.str "Operator:", Pretty.brk 2, Syntax.pretty_term ctxt t,
            Pretty.str " ::", Pretty.brk 1, Syntax.pretty_typ ctxt T]),
        Pretty.string_of (Pretty.block
          [Pretty.str "Operand:", Pretty.brk 3, Syntax.pretty_term ctxt u,
            Pretty.str " ::", Pretty.brk 1, Syntax.pretty_typ ctxt U])];



(** type signatures and certified types **)

(* type declarations *)

datatype decl =
  LogicalType of int |
  Abbreviation of string list * typ * bool |
  Nonterminal;


(* type tsig *)

datatype tsig =
  TSig of {
    classes: Name_Space.T * Sorts.algebra,  (*order-sorted algebra of type classes*)
    default: sort,                          (*default sort on input*)
    types: decl Name_Space.table,           (*declared types*)
    log_types: string list};                (*logical types sorted by number of arguments*)

fun eq_tsig
   (TSig {classes = classes1, default = default1, types = types1, log_types = _},
    TSig {classes = classes2, default = default2, types = types2, log_types = _}) =
  pointer_eq (classes1, classes2) andalso
  default1 = default2 andalso
  pointer_eq (types1, types2);

fun rep_tsig (TSig comps) = comps;

fun make_tsig (classes, default, types, log_types) =
  TSig {classes = classes, default = default, types = types, log_types = log_types};

fun build_tsig (classes, default, types) =
  let
    val log_types =
      Symtab.fold (fn (c, LogicalType n) => cons (c, n) | _ => I) (snd types) []
      |> Library.sort (int_ord o pairself snd) |> map fst;
  in make_tsig (classes, default, types, log_types) end;

fun map_tsig f (TSig {classes, default, types, log_types = _}) =
  build_tsig (f (classes, default, types));

val empty_tsig =
  build_tsig ((Name_Space.empty "class", Sorts.empty_algebra), [], Name_Space.empty_table "type");


(* classes and sorts *)

val class_space = #1 o #classes o rep_tsig;

fun class_alias naming binding name = map_tsig (fn ((space, classes), default, types) =>
  ((Name_Space.alias naming binding name space, classes), default, types));

val intern_class = Name_Space.intern o class_space;
fun extern_class ctxt = Name_Space.extern ctxt o class_space;

fun defaultS (TSig {default, ...}) = default;
fun logical_types (TSig {log_types, ...}) = log_types;

fun eq_sort (TSig {classes, ...}) = Sorts.sort_eq (#2 classes);
fun subsort (TSig {classes, ...}) = Sorts.sort_le (#2 classes);
fun of_sort (TSig {classes, ...}) = Sorts.of_sort (#2 classes);
fun inter_sort (TSig {classes, ...}) = Sorts.inter_sort (#2 classes);

fun cert_class (TSig {classes, ...}) = Sorts.certify_class (#2 classes);
fun cert_sort (TSig {classes, ...}) = Sorts.certify_sort (#2 classes);
fun minimize_sort (TSig {classes, ...}) = Sorts.minimize_sort (#2 classes);

fun witness_sorts (TSig {classes, log_types, ...}) =
  Sorts.witness_sorts (#2 classes) log_types;


(* certification mode *)

datatype mode = Mode of {normalize: bool, logical: bool};

val mode_default = Mode {normalize = true, logical = true};
val mode_syntax = Mode {normalize = true, logical = false};
val mode_abbrev = Mode {normalize = false, logical = false};

structure Mode = Proof_Data
(
  type T = mode;
  fun init _ = mode_default;
);

val get_mode = Mode.get;
fun set_mode mode = Mode.map (K mode);
fun restore_mode ctxt = set_mode (get_mode ctxt);


(* types *)

val type_space = #1 o #types o rep_tsig;

fun type_alias naming binding name = map_tsig (fn (classes, default, (space, types)) =>
  (classes, default, (Name_Space.alias naming binding name space, types)));

val intern_type = Name_Space.intern o type_space;
fun extern_type ctxt = Name_Space.extern ctxt o type_space;

val is_logtype = member (op =) o logical_types;


fun undecl_type c = "Undeclared type constructor: " ^ quote c;

fun lookup_type (TSig {types = (_, types), ...}) = Symtab.lookup types;

fun the_decl tsig (c, pos) =
  (case lookup_type tsig c of
    NONE => error (undecl_type c ^ Position.str_of pos)
  | SOME decl => decl);


(* certified types *)

fun bad_nargs t = "Bad number of arguments for type constructor: " ^ quote t;

local

fun inst_typ env (Type (c, Ts)) = Type (c, map (inst_typ env) Ts)
  | inst_typ env (T as TFree (x, _)) = the_default T (AList.lookup (op =) env x)
  | inst_typ _ T = T;

in

fun cert_typ_mode (Mode {normalize, logical}) tsig ty =
  let
    fun err msg = raise TYPE (msg, [ty], []);

    val check_logical =
      if logical then fn c => err ("Illegal occurrence of syntactic type: " ^ quote c)
      else fn _ => ();

    fun cert (T as Type (c, Ts)) =
          let
            val Ts' = map cert Ts;
            fun nargs n = if length Ts <> n then err (bad_nargs c) else ();
          in
            (case the_decl tsig (c, Position.none) of
              LogicalType n => (nargs n; Type (c, Ts'))
            | Abbreviation (vs, U, syn) =>
               (nargs (length vs);
                if syn then check_logical c else ();
                if normalize then inst_typ (vs ~~ Ts') U
                else Type (c, Ts'))
            | Nonterminal => (nargs 0; check_logical c; T))
          end
      | cert (TFree (x, S)) = TFree (x, cert_sort tsig S)
      | cert (TVar (xi as (_, i), S)) =
          if i < 0 then
            err ("Malformed type variable: " ^ quote (Term.string_of_vname xi))
          else TVar (xi, cert_sort tsig S);

    val ty' = cert ty;
  in if ty = ty' then ty else ty' end;  (*avoid copying of already normal type*)

val cert_typ = cert_typ_mode mode_default;

end;


(* type arities *)

fun arity_number tsig a =
  (case lookup_type tsig a of
    SOME (LogicalType n) => n
  | _ => error (undecl_type a));

fun arity_sorts _ tsig a [] = replicate (arity_number tsig a) []
  | arity_sorts pp (TSig {classes, ...}) a S = Sorts.mg_domain (#2 classes) a S
      handle Sorts.CLASS_ERROR err => error (Sorts.class_error (Syntax.init_pretty pp) err);



(** special treatment of type vars **)

(* sort_of_atyp *)

fun sort_of_atyp (TFree (_, S)) = S
  | sort_of_atyp (TVar (_, S)) = S
  | sort_of_atyp T = raise TYPE ("sort_of_atyp", [T], []);


(* strip_sorts *)

fun strip_sorts (Type (a, Ts)) = Type (a, map strip_sorts Ts)
  | strip_sorts (TFree (x, _)) = TFree (x, [])
  | strip_sorts (TVar (xi, _)) = TVar (xi, []);


(* no_tvars *)

fun no_tvars T =
  (case Term.add_tvarsT T [] of [] => T
  | vs => raise TYPE ("Illegal schematic type variable(s): " ^
      commas_quote (map (Term.string_of_vname o #1) (rev vs)), [T], []));


(* varify_global *)

fun varify_global fixed t =
  let
    val fs = Term.fold_types (Term.fold_atyps
      (fn TFree v => if member (op =) fixed v then I else insert (op =) v | _ => I)) t [];
    val used = Name.context
      |> fold_types (fold_atyps (fn TVar ((a, _), _) => Name.declare a | _ => I)) t;
    val fmap = fs ~~ map (rpair 0) (#1 (Name.variants (map fst fs) used));
    fun thaw (f as (_, S)) =
      (case AList.lookup (op =) fmap f of
        NONE => TFree f
      | SOME xi => TVar (xi, S));
  in (fmap, map_types (map_type_tfree thaw) t) end;


(* freeze_thaw: freeze TVars in a term; return the "thaw" inverse *)

local

fun new_name (ix, (pairs, used)) =
  let val v = singleton (Name.variant_list used) (string_of_indexname ix)
  in ((ix, v) :: pairs, v :: used) end;

fun freeze_one alist (ix, sort) =
  TFree (the (AList.lookup (op =) alist ix), sort)
    handle Option =>
      raise TYPE ("Failure during freezing of ?" ^ string_of_indexname ix, [], []);

fun thaw_one alist (a, sort) = TVar (the (AList.lookup (op =) alist a), sort)
  handle Option => TFree (a, sort);

in

fun legacy_freeze_thaw_type T =
  let
    val used = OldTerm.add_typ_tfree_names (T, [])
    and tvars = map #1 (OldTerm.add_typ_tvars (T, []));
    val (alist, _) = List.foldr new_name ([], used) tvars;
  in (map_type_tvar (freeze_one alist) T, map_type_tfree (thaw_one (map swap alist))) end;

val legacy_freeze_type = #1 o legacy_freeze_thaw_type;

fun legacy_freeze_thaw t =
  let
    val used = OldTerm.it_term_types OldTerm.add_typ_tfree_names (t, [])
    and tvars = map #1 (OldTerm.it_term_types OldTerm.add_typ_tvars (t, []));
    val (alist, _) = List.foldr new_name ([], used) tvars;
  in
    (case alist of
      [] => (t, fn x => x) (*nothing to do!*)
    | _ => (map_types (map_type_tvar (freeze_one alist)) t,
      map_types (map_type_tfree (thaw_one (map swap alist)))))
  end;

val legacy_freeze = #1 o legacy_freeze_thaw;

end;



(** matching and unification of types **)

type tyenv = (sort * typ) Vartab.table;

fun tvar_clash ixn S S' = raise TYPE ("Type variable " ^
  quote (Term.string_of_vname ixn) ^ " has two distinct sorts",
  [TVar (ixn, S), TVar (ixn, S')], []);

fun lookup tye (ixn, S) =
  (case Vartab.lookup tye ixn of
    NONE => NONE
  | SOME (S', T) => if S = S' then SOME T else tvar_clash ixn S S');


(* matching *)

exception TYPE_MATCH;

fun typ_match tsig =
  let
    fun match (V as TVar (v, S), T) subs =
          (case lookup subs (v, S) of
            NONE =>
              if V = T then subs
              else if of_sort tsig (T, S) then Vartab.update_new (v, (S, T)) subs
              else raise TYPE_MATCH
          | SOME U => if U = T then subs else raise TYPE_MATCH)
      | match (Type (a, Ts), Type (b, Us)) subs =
          if a <> b then raise TYPE_MATCH
          else matches (Ts, Us) subs
      | match (TFree x, TFree y) subs =
          if x = y then subs else raise TYPE_MATCH
      | match _ _ = raise TYPE_MATCH
    and matches (T :: Ts, U :: Us) subs = matches (Ts, Us) (match (T, U) subs)
      | matches _ subs = subs;
  in match end;

fun typ_instance tsig (T, U) =
  (typ_match tsig (U, T) Vartab.empty; true) handle TYPE_MATCH => false;

(*purely structural matching*)
fun raw_match (V as TVar (v, S), T) subs =
      (case lookup subs (v, S) of
        NONE => if V = T then subs else Vartab.update_new (v, (S, T)) subs
      | SOME U => if U = T then subs else raise TYPE_MATCH)
  | raw_match (Type (a, Ts), Type (b, Us)) subs =
      if a <> b then raise TYPE_MATCH
      else raw_matches (Ts, Us) subs
  | raw_match (TFree x, TFree y) subs =
      if x = y then subs else raise TYPE_MATCH
  | raw_match _ _ = raise TYPE_MATCH
and raw_matches (T :: Ts, U :: Us) subs = raw_matches (Ts, Us) (raw_match (T, U) subs)
  | raw_matches ([], []) subs = subs
  | raw_matches _ _ = raise TYPE_MATCH;

fun raw_instance (T, U) =
  (raw_match (U, T) Vartab.empty; true) handle TYPE_MATCH => false;


(* unification *)

exception TUNIFY;

(*occurs check*)
fun occurs v tye =
  let
    fun occ (Type (_, Ts)) = exists occ Ts
      | occ (TFree _) = false
      | occ (TVar (w, S)) =
          Term.eq_ix (v, w) orelse
            (case lookup tye (w, S) of
              NONE => false
            | SOME U => occ U);
  in occ end;

(*chase variable assignments; if devar returns a type var then it must be unassigned*)
fun devar tye (T as TVar v) =
      (case lookup tye v of
        SOME U => devar tye U
      | NONE => T)
  | devar _ T = T;

(*order-sorted unification*)
fun unify (TSig {classes = (_, classes), ...}) TU (tyenv, maxidx) =
  let
    val tyvar_count = Unsynchronized.ref maxidx;
    fun gen_tyvar S = TVar ((Name.aT, Unsynchronized.inc tyvar_count), S);

    fun mg_domain a S = Sorts.mg_domain classes a S
      handle Sorts.CLASS_ERROR _ => raise TUNIFY;

    fun meet (_, []) tye = tye
      | meet (TVar (xi, S'), S) tye =
          if Sorts.sort_le classes (S', S) then tye
          else Vartab.update_new
            (xi, (S', gen_tyvar (Sorts.inter_sort classes (S', S)))) tye
      | meet (TFree (_, S'), S) tye =
          if Sorts.sort_le classes (S', S) then tye
          else raise TUNIFY
      | meet (Type (a, Ts), S) tye = meets (Ts, mg_domain a S) tye
    and meets (T :: Ts, S :: Ss) tye = meets (Ts, Ss) (meet (devar tye T, S) tye)
      | meets _ tye = tye;

    fun unif (ty1, ty2) tye =
      (case (devar tye ty1, devar tye ty2) of
        (T as TVar (v, S1), U as TVar (w, S2)) =>
          if Term.eq_ix (v, w) then
            if S1 = S2 then tye else tvar_clash v S1 S2
          else if Sorts.sort_le classes (S1, S2) then
            Vartab.update_new (w, (S2, T)) tye
          else if Sorts.sort_le classes (S2, S1) then
            Vartab.update_new (v, (S1, U)) tye
          else
            let val S = gen_tyvar (Sorts.inter_sort classes (S1, S2)) in
              Vartab.update_new (v, (S1, S)) (Vartab.update_new (w, (S2, S)) tye)
            end
      | (TVar (v, S), T) =>
          if occurs v tye T then raise TUNIFY
          else meet (T, S) (Vartab.update_new (v, (S, T)) tye)
      | (T, TVar (v, S)) =>
          if occurs v tye T then raise TUNIFY
          else meet (T, S) (Vartab.update_new (v, (S, T)) tye)
      | (Type (a, Ts), Type (b, Us)) =>
          if a <> b then raise TUNIFY
          else unifs (Ts, Us) tye
      | (T, U) => if T = U then tye else raise TUNIFY)
    and unifs (T :: Ts, U :: Us) tye = unifs (Ts, Us) (unif (T, U) tye)
      | unifs _ tye = tye;
  in (unif TU tyenv, ! tyvar_count) end;

(*purely structural unification*)
fun raw_unify (ty1, ty2) tye =
  (case (devar tye ty1, devar tye ty2) of
    (T as TVar (v, S1), TVar (w, S2)) =>
      if Term.eq_ix (v, w) then
        if S1 = S2 then tye else tvar_clash v S1 S2
      else Vartab.update_new (w, (S2, T)) tye
  | (TVar (v, S), T) =>
      if occurs v tye T then raise TUNIFY
      else Vartab.update_new (v, (S, T)) tye
  | (T, TVar (v, S)) =>
      if occurs v tye T then raise TUNIFY
      else Vartab.update_new (v, (S, T)) tye
  | (Type (a, Ts), Type (b, Us)) =>
      if a <> b then raise TUNIFY
      else raw_unifys (Ts, Us) tye
  | (T, U) => if T = U then tye else raise TUNIFY)
and raw_unifys (T :: Ts, U :: Us) tye = raw_unifys (Ts, Us) (raw_unify (T, U) tye)
  | raw_unifys ([], []) tye = tye
  | raw_unifys _ _ = raise TUNIFY;

(*fast unification filter*)
fun could_unify (Type (a, Ts), Type (b, Us)) = a = b andalso could_unifys (Ts, Us)
  | could_unify (TFree (a, _), TFree (b, _)) = a = b
  | could_unify (TVar _, _) = true
  | could_unify (_, TVar _) = true
  | could_unify _ = false
and could_unifys (T :: Ts, U :: Us) = could_unify (T, U) andalso could_unifys (Ts, Us)
  | could_unifys ([], []) = true
  | could_unifys _ = false;


(*equality with respect to a type environment*)
fun equal_type tye (T, T') =
  (case (devar tye T, devar tye T') of
     (Type (s, Ts), Type (s', Ts')) =>
       s = s' andalso ListPair.all (equal_type tye) (Ts, Ts')
   | (U, U') => U = U');

fun eq_type tye =
  if Vartab.is_empty tye then op = else equal_type tye;



(** extend and merge type signatures **)

(* classes *)

fun add_class ctxt naming (c, cs) tsig =
  tsig |> map_tsig (fn ((space, classes), default, types) =>
    let
      val cs' = map (cert_class tsig) cs
        handle TYPE (msg, _, _) => error msg;
      val _ = Binding.check c;
      val (c', space') = space |> Name_Space.declare ctxt true naming c;
      val classes' = classes |> Sorts.add_class ctxt (c', cs');
    in ((space', classes'), default, types) end);

fun hide_class fully c = map_tsig (fn ((space, classes), default, types) =>
  ((Name_Space.hide fully c space, classes), default, types));


(* arities *)

fun add_arity ctxt (t, Ss, S) tsig = tsig |> map_tsig (fn ((space, classes), default, types) =>
  let
    val _ =
      (case lookup_type tsig t of
        SOME (LogicalType n) => if length Ss <> n then error (bad_nargs t) else ()
      | SOME _ => error ("Logical type constructor expected: " ^ quote t)
      | NONE => error (undecl_type t));
    val (Ss', S') = (map (cert_sort tsig) Ss, cert_sort tsig S)
      handle TYPE (msg, _, _) => error msg;
    val classes' = classes |> Sorts.add_arities ctxt ((t, map (fn c' => (c', Ss')) S'));
  in ((space, classes'), default, types) end);


(* classrel *)

fun add_classrel ctxt rel tsig =
  tsig |> map_tsig (fn ((space, classes), default, types) =>
    let
      val rel' = pairself (cert_class tsig) rel
        handle TYPE (msg, _, _) => error msg;
      val classes' = classes |> Sorts.add_classrel ctxt rel';
    in ((space, classes'), default, types) end);


(* default sort *)

fun set_defsort S tsig = tsig |> map_tsig (fn (classes, _, types) =>
  (classes, cert_sort tsig S handle TYPE (msg, _, _) => error msg, types));


(* types *)

local

fun new_decl ctxt naming (c, decl) types =
  (Binding.check c; #2 (Name_Space.define ctxt true naming (c, decl) types));

fun map_types f = map_tsig (fn (classes, default, types) =>
  let
    val (space', tab') = f types;
    val _ = Name_Space.intern space' "dummy" = "dummy" orelse
      error "Illegal declaration of dummy type";
  in (classes, default, (space', tab')) end);

fun syntactic types (Type (c, Ts)) =
      (case Symtab.lookup types c of SOME Nonterminal => true | _ => false)
        orelse exists (syntactic types) Ts
  | syntactic _ _ = false;

in

fun add_type ctxt naming (c, n) =
  if n < 0 then error ("Bad type constructor declaration " ^ Binding.print c)
  else map_types (new_decl ctxt naming (c, LogicalType n));

fun add_abbrev ctxt naming (a, vs, rhs) tsig = tsig |> map_types (fn types =>
  let
    fun err msg =
      cat_error msg ("The error(s) above occurred in type abbreviation " ^ Binding.print a);
    val rhs' = strip_sorts (no_tvars (cert_typ_mode mode_syntax tsig rhs))
      handle TYPE (msg, _, _) => err msg;
    val _ =
      (case duplicates (op =) vs of
        [] => []
      | dups => err ("Duplicate variables on lhs: " ^ commas_quote dups));
    val _ =
      (case subtract (op =) vs (map #1 (Term.add_tfreesT rhs' [])) of
        [] => []
      | extras => err ("Extra variables on rhs: " ^ commas_quote extras));
  in types |> new_decl ctxt naming (a, Abbreviation (vs, rhs', syntactic (#2 types) rhs')) end);

fun add_nonterminal ctxt naming = map_types o new_decl ctxt naming o rpair Nonterminal;

end;

fun hide_type fully c = map_tsig (fn (classes, default, (space, types)) =>
  (classes, default, (Name_Space.hide fully c space, types)));


(* merge type signatures *)

fun merge_tsig ctxt (tsig1, tsig2) =
  let
    val (TSig {classes = (space1, classes1), default = default1, types = types1,
      log_types = _}) = tsig1;
    val (TSig {classes = (space2, classes2), default = default2, types = types2,
      log_types = _}) = tsig2;

    val space' = Name_Space.merge (space1, space2);
    val classes' = Sorts.merge_algebra ctxt (classes1, classes2);
    val default' = Sorts.inter_sort classes' (default1, default2);
    val types' = Name_Space.merge_tables (types1, types2);
  in build_tsig ((space', classes'), default', types') end;

end;