renamed former datatype.ML to datatype_data.ML; datatype.ML provides uniform view on datatype.ML and datatype_rep_proofs.ML
authorhaftmann
Fri Nov 27 08:41:10 2009 +0100 (2009-11-27)
changeset 33963977b94b64905
parent 33962 abf9fa17452a
child 33964 26acbc11e8be
renamed former datatype.ML to datatype_data.ML; datatype.ML provides uniform view on datatype.ML and datatype_rep_proofs.ML
src/HOL/Datatype.thy
src/HOL/Inductive.thy
src/HOL/IsaMakefile
src/HOL/Tools/Datatype/datatype.ML
src/HOL/Tools/Datatype/datatype_data.ML
src/HOL/Tools/Datatype/datatype_rep_proofs.ML
src/HOL/Tools/inductive_codegen.ML
src/HOL/Tools/old_primrec.ML
src/HOL/Tools/primrec.ML
     1.1 --- a/src/HOL/Datatype.thy	Fri Nov 27 08:41:08 2009 +0100
     1.2 +++ b/src/HOL/Datatype.thy	Fri Nov 27 08:41:10 2009 +0100
     1.3 @@ -11,6 +11,7 @@
     1.4  imports Product_Type Sum_Type Nat
     1.5  uses
     1.6    ("Tools/Datatype/datatype_rep_proofs.ML")
     1.7 +  ("Tools/Datatype/datatype.ML")
     1.8    ("Tools/inductive_realizer.ML")
     1.9    ("Tools/Datatype/datatype_realizer.ML")
    1.10  begin
    1.11 @@ -520,6 +521,7 @@
    1.12  hide (open) const Push Node Atom Leaf Numb Lim Split Case
    1.13  
    1.14  use "Tools/Datatype/datatype_rep_proofs.ML"
    1.15 +use "Tools/Datatype/datatype.ML"
    1.16  
    1.17  use "Tools/inductive_realizer.ML"
    1.18  setup InductiveRealizer.setup
     2.1 --- a/src/HOL/Inductive.thy	Fri Nov 27 08:41:08 2009 +0100
     2.2 +++ b/src/HOL/Inductive.thy	Fri Nov 27 08:41:10 2009 +0100
     2.3 @@ -14,7 +14,7 @@
     2.4    "Tools/Datatype/datatype_prop.ML"
     2.5    "Tools/Datatype/datatype_case.ML"
     2.6    ("Tools/Datatype/datatype_abs_proofs.ML")
     2.7 -  ("Tools/Datatype/datatype.ML")
     2.8 +  ("Tools/Datatype/datatype_data.ML")
     2.9    ("Tools/old_primrec.ML")
    2.10    ("Tools/primrec.ML")
    2.11    ("Tools/Datatype/datatype_codegen.ML")
    2.12 @@ -282,8 +282,8 @@
    2.13  text {* Package setup. *}
    2.14  
    2.15  use "Tools/Datatype/datatype_abs_proofs.ML"
    2.16 -use "Tools/Datatype/datatype.ML"
    2.17 -setup Datatype.setup
    2.18 +use "Tools/Datatype/datatype_data.ML"
    2.19 +setup Datatype_Data.setup
    2.20  
    2.21  use "Tools/old_primrec.ML"
    2.22  use "Tools/primrec.ML"
    2.23 @@ -306,7 +306,7 @@
    2.24    fun fun_tr ctxt [cs] =
    2.25      let
    2.26        val x = Free (Name.variant (Term.add_free_names cs []) "x", dummyT);
    2.27 -      val ft = DatatypeCase.case_tr true Datatype.info_of_constr
    2.28 +      val ft = DatatypeCase.case_tr true Datatype_Data.info_of_constr
    2.29                   ctxt [x, cs]
    2.30      in lambda x ft end
    2.31  in [("_lam_pats_syntax", fun_tr)] end
     3.1 --- a/src/HOL/IsaMakefile	Fri Nov 27 08:41:08 2009 +0100
     3.2 +++ b/src/HOL/IsaMakefile	Fri Nov 27 08:41:10 2009 +0100
     3.3 @@ -168,10 +168,11 @@
     3.4    Tools/Datatype/datatype_aux.ML \
     3.5    Tools/Datatype/datatype_case.ML \
     3.6    Tools/Datatype/datatype_codegen.ML \
     3.7 -  Tools/Datatype/datatype.ML \
     3.8 +  Tools/Datatype/datatype_data.ML \
     3.9    Tools/Datatype/datatype_prop.ML \
    3.10    Tools/Datatype/datatype_realizer.ML \
    3.11    Tools/Datatype/datatype_rep_proofs.ML \
    3.12 +  Tools/Datatype/datatype.ML \
    3.13    Tools/dseq.ML \
    3.14    Tools/Function/context_tree.ML \
    3.15    Tools/Function/decompose.ML \
     4.1 --- a/src/HOL/Tools/Datatype/datatype.ML	Fri Nov 27 08:41:08 2009 +0100
     4.2 +++ b/src/HOL/Tools/Datatype/datatype.ML	Fri Nov 27 08:41:10 2009 +0100
     4.3 @@ -1,471 +1,19 @@
     4.4  (*  Title:      HOL/Tools/datatype.ML
     4.5      Author:     Stefan Berghofer, TU Muenchen
     4.6  
     4.7 -Datatype package for Isabelle/HOL.
     4.8 +Datatype package interface for Isabelle/HOL.
     4.9  *)
    4.10  
    4.11  signature DATATYPE =
    4.12  sig
    4.13 -  include DATATYPE_COMMON
    4.14 -  val derive_datatype_props : config -> string list -> string list option
    4.15 -    -> descr list -> (string * sort) list -> thm -> thm list list -> thm list list
    4.16 -    -> theory -> string list * theory
    4.17 -  val rep_datatype : config -> (string list -> Proof.context -> Proof.context)
    4.18 -    -> string list option -> term list -> theory -> Proof.state
    4.19 -  val rep_datatype_cmd : string list option -> string list -> theory -> Proof.state
    4.20 -  val get_info : theory -> string -> info option
    4.21 -  val the_info : theory -> string -> info
    4.22 -  val the_descr : theory -> string list
    4.23 -    -> descr * (string * sort) list * string list
    4.24 -      * string * (string list * string list) * (typ list * typ list)
    4.25 -  val the_spec : theory -> string -> (string * sort) list * (string * typ list) list
    4.26 -  val all_distincts : theory -> typ list -> thm list list
    4.27 -  val get_constrs : theory -> string -> (string * typ) list option
    4.28 -  val get_all : theory -> info Symtab.table
    4.29 -  val info_of_constr : theory -> string * typ -> info option
    4.30 -  val info_of_case : theory -> string -> info option
    4.31 -  val interpretation : (config -> string list -> theory -> theory) -> theory -> theory
    4.32 -  val make_case :  Proof.context -> DatatypeCase.config -> string list -> term ->
    4.33 -    (term * term) list -> term * (term * (int * bool)) list
    4.34 -  val strip_case : Proof.context -> bool -> term -> (term * (term * term) list) option
    4.35 -  val read_typ: theory -> string -> (string * sort) list -> typ * (string * sort) list
    4.36 -  val cert_typ: theory -> typ -> (string * sort) list -> typ * (string * sort) list
    4.37 -  val mk_case_names_induct: descr -> attribute
    4.38 -  val setup: theory -> theory
    4.39 -end;
    4.40 -
    4.41 -structure Datatype : DATATYPE =
    4.42 -struct
    4.43 -
    4.44 -open DatatypeAux;
    4.45 -
    4.46 -(** theory data **)
    4.47 -
    4.48 -(* data management *)
    4.49 -
    4.50 -structure DatatypesData = Theory_Data
    4.51 -(
    4.52 -  type T =
    4.53 -    {types: info Symtab.table,
    4.54 -     constrs: (string * info) list Symtab.table,
    4.55 -     cases: info Symtab.table};
    4.56 -
    4.57 -  val empty =
    4.58 -    {types = Symtab.empty, constrs = Symtab.empty, cases = Symtab.empty};
    4.59 -  val extend = I;
    4.60 -  fun merge
    4.61 -    ({types = types1, constrs = constrs1, cases = cases1},
    4.62 -     {types = types2, constrs = constrs2, cases = cases2}) : T =
    4.63 -    {types = Symtab.merge (K true) (types1, types2),
    4.64 -     constrs = Symtab.join (K (AList.merge (op =) (K true))) (constrs1, constrs2),
    4.65 -     cases = Symtab.merge (K true) (cases1, cases2)};
    4.66 -);
    4.67 -
    4.68 -val get_all = #types o DatatypesData.get;
    4.69 -val get_info = Symtab.lookup o get_all;
    4.70 -
    4.71 -fun the_info thy name =
    4.72 -  (case get_info thy name of
    4.73 -    SOME info => info
    4.74 -  | NONE => error ("Unknown datatype " ^ quote name));
    4.75 -
    4.76 -fun info_of_constr thy (c, T) =
    4.77 -  let
    4.78 -    val tab = Symtab.lookup_list ((#constrs o DatatypesData.get) thy) c;
    4.79 -    val hint = case strip_type T of (_, Type (tyco, _)) => SOME tyco | _ => NONE;
    4.80 -    val default = if null tab then NONE
    4.81 -      else SOME (snd (Library.last_elem tab))
    4.82 -        (*conservative wrt. overloaded constructors*);
    4.83 -  in case hint
    4.84 -   of NONE => default
    4.85 -    | SOME tyco => case AList.lookup (op =) tab tyco
    4.86 -       of NONE => default (*permissive*)
    4.87 -        | SOME info => SOME info
    4.88 -  end;
    4.89 -
    4.90 -val info_of_case = Symtab.lookup o #cases o DatatypesData.get;
    4.91 -
    4.92 -fun register (dt_infos : (string * info) list) =
    4.93 -  DatatypesData.map (fn {types, constrs, cases} =>
    4.94 -    {types = types |> fold Symtab.update dt_infos,
    4.95 -     constrs = constrs |> fold (fn (constr, dtname_info) =>
    4.96 -         Symtab.map_default (constr, []) (cons dtname_info))
    4.97 -       (maps (fn (dtname, info as {descr, index, ...}) =>
    4.98 -          map (rpair (dtname, info) o fst)
    4.99 -            (#3 (the (AList.lookup op = descr index)))) dt_infos),
   4.100 -     cases = cases |> fold Symtab.update
   4.101 -       (map (fn (_, info as {case_name, ...}) => (case_name, info)) dt_infos)});
   4.102 -
   4.103 -
   4.104 -(* complex queries *)
   4.105 -
   4.106 -fun the_spec thy dtco =
   4.107 -  let
   4.108 -    val info as { descr, index, sorts = raw_sorts, ... } = the_info thy dtco;
   4.109 -    val SOME (_, dtys, raw_cos) = AList.lookup (op =) descr index;
   4.110 -    val sorts = map ((fn v => (v, (the o AList.lookup (op =) raw_sorts) v))
   4.111 -      o DatatypeAux.dest_DtTFree) dtys;
   4.112 -    val cos = map
   4.113 -      (fn (co, tys) => (co, map (DatatypeAux.typ_of_dtyp descr sorts) tys)) raw_cos;
   4.114 -  in (sorts, cos) end;
   4.115 -
   4.116 -fun the_descr thy (raw_tycos as raw_tyco :: _) =
   4.117 -  let
   4.118 -    val info = the_info thy raw_tyco;
   4.119 -    val descr = #descr info;
   4.120 -
   4.121 -    val SOME (_, dtys, raw_cos) = AList.lookup (op =) descr (#index info);
   4.122 -    val vs = map ((fn v => (v, (the o AList.lookup (op =) (#sorts info)) v))
   4.123 -      o dest_DtTFree) dtys;
   4.124 -
   4.125 -    fun is_DtTFree (DtTFree _) = true
   4.126 -      | is_DtTFree _ = false
   4.127 -    val k = find_index (fn (_, (_, dTs, _)) => not (forall is_DtTFree dTs)) descr;
   4.128 -    val protoTs as (dataTs, _) = chop k descr
   4.129 -      |> (pairself o map) (fn (_, (tyco, dTs, _)) => (tyco, map (typ_of_dtyp descr vs) dTs));
   4.130 -    
   4.131 -    val tycos = map fst dataTs;
   4.132 -    val _ = if eq_set (op =) (tycos, raw_tycos) then ()
   4.133 -      else error ("Type constructors " ^ commas (map quote raw_tycos)
   4.134 -        ^ " do not belong exhaustively to one mutual recursive datatype");
   4.135 -
   4.136 -    val (Ts, Us) = (pairself o map) Type protoTs;
   4.137 -
   4.138 -    val names = map Long_Name.base_name (the_default tycos (#alt_names info));
   4.139 -    val (auxnames, _) = Name.make_context names
   4.140 -      |> fold_map (yield_singleton Name.variants o name_of_typ) Us;
   4.141 -    val prefix = space_implode "_" names;
   4.142 -
   4.143 -  in (descr, vs, tycos, prefix, (names, auxnames), (Ts, Us)) end;
   4.144 -
   4.145 -fun all_distincts thy Ts =
   4.146 -  let
   4.147 -    fun add_tycos (Type (tyco, Ts)) = insert (op =) tyco #> fold add_tycos Ts
   4.148 -      | add_tycos _ = I;
   4.149 -    val tycos = fold add_tycos Ts [];
   4.150 -  in map_filter (Option.map #distinct o get_info thy) tycos end;
   4.151 -
   4.152 -fun get_constrs thy dtco =
   4.153 -  case try (the_spec thy) dtco
   4.154 -   of SOME (sorts, cos) =>
   4.155 -        let
   4.156 -          fun subst (v, sort) = TVar ((v, 0), sort);
   4.157 -          fun subst_ty (TFree v) = subst v
   4.158 -            | subst_ty ty = ty;
   4.159 -          val dty = Type (dtco, map subst sorts);
   4.160 -          fun mk_co (co, tys) = (co, map (Term.map_atyps subst_ty) tys ---> dty);
   4.161 -        in SOME (map mk_co cos) end
   4.162 -    | NONE => NONE;
   4.163 -
   4.164 -
   4.165 -
   4.166 -(** various auxiliary **)
   4.167 -
   4.168 -(* prepare datatype specifications *)
   4.169 -
   4.170 -fun read_typ thy str sorts =
   4.171 -  let
   4.172 -    val ctxt = ProofContext.init thy
   4.173 -      |> fold (Variable.declare_typ o TFree) sorts;
   4.174 -    val T = Syntax.read_typ ctxt str;
   4.175 -  in (T, Term.add_tfreesT T sorts) end;
   4.176 -
   4.177 -fun cert_typ sign raw_T sorts =
   4.178 -  let
   4.179 -    val T = Type.no_tvars (Sign.certify_typ sign raw_T)
   4.180 -      handle TYPE (msg, _, _) => error msg;
   4.181 -    val sorts' = Term.add_tfreesT T sorts;
   4.182 -    val _ =
   4.183 -      case duplicates (op =) (map fst sorts') of
   4.184 -        [] => ()
   4.185 -      | dups => error ("Inconsistent sort constraints for " ^ commas dups)
   4.186 -  in (T, sorts') end;
   4.187 -
   4.188 -
   4.189 -(* case names *)
   4.190 -
   4.191 -local
   4.192 -
   4.193 -fun dt_recs (DtTFree _) = []
   4.194 -  | dt_recs (DtType (_, dts)) = maps dt_recs dts
   4.195 -  | dt_recs (DtRec i) = [i];
   4.196 -
   4.197 -fun dt_cases (descr: descr) (_, args, constrs) =
   4.198 -  let
   4.199 -    fun the_bname i = Long_Name.base_name (#1 (the (AList.lookup (op =) descr i)));
   4.200 -    val bnames = map the_bname (distinct (op =) (maps dt_recs args));
   4.201 -  in map (fn (c, _) => space_implode "_" (Long_Name.base_name c :: bnames)) constrs end;
   4.202 -
   4.203 -fun induct_cases descr =
   4.204 -  DatatypeProp.indexify_names (maps (dt_cases descr) (map #2 descr));
   4.205 -
   4.206 -fun exhaust_cases descr i = dt_cases descr (the (AList.lookup (op =) descr i));
   4.207 -
   4.208 -in
   4.209 -
   4.210 -fun mk_case_names_induct descr = Rule_Cases.case_names (induct_cases descr);
   4.211 -
   4.212 -fun mk_case_names_exhausts descr new =
   4.213 -  map (Rule_Cases.case_names o exhaust_cases descr o #1)
   4.214 -    (filter (fn ((_, (name, _, _))) => member (op =) new name) descr);
   4.215 -
   4.216 +  include DATATYPE_DATA
   4.217 +  include DATATYPE_REP_PROOFS
   4.218  end;
   4.219  
   4.220 -
   4.221 -(* translation rules for case *)
   4.222 -
   4.223 -fun make_case ctxt = DatatypeCase.make_case
   4.224 -  (info_of_constr (ProofContext.theory_of ctxt)) ctxt;
   4.225 -
   4.226 -fun strip_case ctxt = DatatypeCase.strip_case
   4.227 -  (info_of_case (ProofContext.theory_of ctxt));
   4.228 -
   4.229 -fun add_case_tr' case_names thy =
   4.230 -  Sign.add_advanced_trfuns ([], [],
   4.231 -    map (fn case_name =>
   4.232 -      let val case_name' = Sign.const_syntax_name thy case_name
   4.233 -      in (case_name', DatatypeCase.case_tr' info_of_case case_name')
   4.234 -      end) case_names, []) thy;
   4.235 -
   4.236 -val trfun_setup =
   4.237 -  Sign.add_advanced_trfuns ([],
   4.238 -    [("_case_syntax", DatatypeCase.case_tr true info_of_constr)],
   4.239 -    [], []);
   4.240 -
   4.241 -
   4.242 -
   4.243 -(** document antiquotation **)
   4.244 -
   4.245 -val _ = ThyOutput.antiquotation "datatype" Args.tyname
   4.246 -  (fn {source = src, context = ctxt, ...} => fn dtco =>
   4.247 -    let
   4.248 -      val thy = ProofContext.theory_of ctxt;
   4.249 -      val (vs, cos) = the_spec thy dtco;
   4.250 -      val ty = Type (dtco, map TFree vs);
   4.251 -      fun pretty_typ_bracket (ty as Type (_, _ :: _)) =
   4.252 -            Pretty.enclose "(" ")" [Syntax.pretty_typ ctxt ty]
   4.253 -        | pretty_typ_bracket ty =
   4.254 -            Syntax.pretty_typ ctxt ty;
   4.255 -      fun pretty_constr (co, tys) =
   4.256 -        (Pretty.block o Pretty.breaks)
   4.257 -          (Syntax.pretty_term ctxt (Const (co, tys ---> ty)) ::
   4.258 -            map pretty_typ_bracket tys);
   4.259 -      val pretty_datatype =
   4.260 -        Pretty.block
   4.261 -          (Pretty.command "datatype" :: Pretty.brk 1 ::
   4.262 -           Syntax.pretty_typ ctxt ty ::
   4.263 -           Pretty.str " =" :: Pretty.brk 1 ::
   4.264 -           flat (separate [Pretty.brk 1, Pretty.str "| "]
   4.265 -             (map (single o pretty_constr) cos)));
   4.266 -    in ThyOutput.output (ThyOutput.maybe_pretty_source (K pretty_datatype) src [()]) end);
   4.267 -
   4.268 -
   4.269 -
   4.270 -(** abstract theory extensions relative to a datatype characterisation **)
   4.271 -
   4.272 -structure Datatype_Interpretation = Interpretation
   4.273 -  (type T = config * string list val eq: T * T -> bool = eq_snd op =);
   4.274 -fun interpretation f = Datatype_Interpretation.interpretation (uncurry f);
   4.275 -
   4.276 -fun make_dt_info alt_names descr sorts induct inducts rec_names rec_rewrites
   4.277 -    (index, (((((((((((_, (tname, _, _))), inject), distinct),
   4.278 -      exhaust), nchotomy), case_name), case_rewrites), case_cong), weak_case_cong),
   4.279 -        (split, split_asm))) =
   4.280 -  (tname,
   4.281 -   {index = index,
   4.282 -    alt_names = alt_names,
   4.283 -    descr = descr,
   4.284 -    sorts = sorts,
   4.285 -    inject = inject,
   4.286 -    distinct = distinct,
   4.287 -    induct = induct,
   4.288 -    inducts = inducts,
   4.289 -    exhaust = exhaust,
   4.290 -    nchotomy = nchotomy,
   4.291 -    rec_names = rec_names,
   4.292 -    rec_rewrites = rec_rewrites,
   4.293 -    case_name = case_name,
   4.294 -    case_rewrites = case_rewrites,
   4.295 -    case_cong = case_cong,
   4.296 -    weak_case_cong = weak_case_cong,
   4.297 -    split = split,
   4.298 -    split_asm = split_asm});
   4.299 -
   4.300 -fun derive_datatype_props config dt_names alt_names descr sorts
   4.301 -    induct inject distinct thy1 =
   4.302 -  let
   4.303 -    val thy2 = thy1 |> Theory.checkpoint;
   4.304 -    val flat_descr = flat descr;
   4.305 -    val new_type_names = map Long_Name.base_name (the_default dt_names alt_names);
   4.306 -    val _ = message config ("Deriving properties for datatype(s) " ^ commas_quote new_type_names);
   4.307 -
   4.308 -    val (exhaust, thy3) = DatatypeAbsProofs.prove_casedist_thms config new_type_names
   4.309 -      descr sorts induct (mk_case_names_exhausts flat_descr dt_names) thy2;
   4.310 -    val (nchotomys, thy4) = DatatypeAbsProofs.prove_nchotomys config new_type_names
   4.311 -      descr sorts exhaust thy3;
   4.312 -    val ((rec_names, rec_rewrites), thy5) = DatatypeAbsProofs.prove_primrec_thms
   4.313 -      config new_type_names descr sorts (#inject o the o Symtab.lookup (get_all thy4))
   4.314 -      inject (distinct, all_distincts thy2 (get_rec_types flat_descr sorts))
   4.315 -      induct thy4;
   4.316 -    val ((case_rewrites, case_names), thy6) = DatatypeAbsProofs.prove_case_thms
   4.317 -      config new_type_names descr sorts rec_names rec_rewrites thy5;
   4.318 -    val (case_congs, thy7) = DatatypeAbsProofs.prove_case_congs new_type_names
   4.319 -      descr sorts nchotomys case_rewrites thy6;
   4.320 -    val (weak_case_congs, thy8) = DatatypeAbsProofs.prove_weak_case_congs new_type_names
   4.321 -      descr sorts thy7;
   4.322 -    val (splits, thy9) = DatatypeAbsProofs.prove_split_thms
   4.323 -      config new_type_names descr sorts inject distinct exhaust case_rewrites thy8;
   4.324 +structure Datatype =
   4.325 +struct
   4.326  
   4.327 -    val inducts = Project_Rule.projections (ProofContext.init thy2) induct;
   4.328 -    val dt_infos = map_index
   4.329 -      (make_dt_info alt_names flat_descr sorts induct inducts rec_names rec_rewrites)
   4.330 -      (hd descr ~~ inject ~~ distinct ~~ exhaust ~~ nchotomys ~~
   4.331 -        case_names ~~ case_rewrites ~~ case_congs ~~ weak_case_congs ~~ splits);
   4.332 -    val dt_names = map fst dt_infos;
   4.333 -    val prfx = Binding.qualify true (space_implode "_" new_type_names);
   4.334 -    val simps = flat (inject @ distinct @ case_rewrites) @ rec_rewrites;
   4.335 -    val named_rules = flat (map_index (fn (index, tname) =>
   4.336 -      [((Binding.empty, [nth inducts index]), [Induct.induct_type tname]),
   4.337 -       ((Binding.empty, [nth exhaust index]), [Induct.cases_type tname])]) dt_names);
   4.338 -    val unnamed_rules = map (fn induct =>
   4.339 -      ((Binding.empty, [induct]), [Rule_Cases.inner_rule, Induct.induct_type ""]))
   4.340 -        (drop (length dt_names) inducts);
   4.341 -  in
   4.342 -    thy9
   4.343 -    |> PureThy.add_thmss ([((prfx (Binding.name "simps"), simps), []),
   4.344 -        ((prfx (Binding.name "inducts"), inducts), []),
   4.345 -        ((prfx (Binding.name "splits"), maps (fn (x, y) => [x, y]) splits), []),
   4.346 -        ((Binding.empty, flat case_rewrites @ flat distinct @ rec_rewrites),
   4.347 -          [Simplifier.simp_add]),
   4.348 -        ((Binding.empty, rec_rewrites), [Code.add_default_eqn_attribute]),
   4.349 -        ((Binding.empty, flat inject), [iff_add]),
   4.350 -        ((Binding.empty, map (fn th => th RS notE) (flat distinct)),
   4.351 -          [Classical.safe_elim NONE]),
   4.352 -        ((Binding.empty, weak_case_congs), [Simplifier.attrib (op addcongs)])]
   4.353 -        @ named_rules @ unnamed_rules)
   4.354 -    |> snd
   4.355 -    |> add_case_tr' case_names
   4.356 -    |> register dt_infos
   4.357 -    |> Datatype_Interpretation.data (config, dt_names)
   4.358 -    |> pair dt_names
   4.359 -  end;
   4.360 -
   4.361 -
   4.362 -
   4.363 -(** declare existing type as datatype **)
   4.364 -
   4.365 -fun prove_rep_datatype config dt_names alt_names descr sorts
   4.366 -    raw_inject half_distinct raw_induct thy1 =
   4.367 -  let
   4.368 -    val raw_distinct = (map o maps) (fn thm => [thm, thm RS not_sym]) half_distinct;
   4.369 -    val new_type_names = map Long_Name.base_name (the_default dt_names alt_names);
   4.370 -    val (((inject, distinct), [induct]), thy2) =
   4.371 -      thy1
   4.372 -      |> store_thmss "inject" new_type_names raw_inject
   4.373 -      ||>> store_thmss "distinct" new_type_names raw_distinct
   4.374 -      ||> Sign.add_path (space_implode "_" new_type_names)
   4.375 -      ||>> PureThy.add_thms [((Binding.name "induct", raw_induct), [mk_case_names_induct descr])]
   4.376 -      ||> Sign.restore_naming thy1;
   4.377 -  in
   4.378 -    thy2
   4.379 -    |> derive_datatype_props config dt_names alt_names [descr] sorts
   4.380 -         induct inject distinct
   4.381 - end;
   4.382 -
   4.383 -fun gen_rep_datatype prep_term config after_qed alt_names raw_ts thy =
   4.384 -  let
   4.385 -    fun constr_of_term (Const (c, T)) = (c, T)
   4.386 -      | constr_of_term t =
   4.387 -          error ("Not a constant: " ^ Syntax.string_of_term_global thy t);
   4.388 -    fun no_constr (c, T) = error ("Bad constructor: "
   4.389 -      ^ Sign.extern_const thy c ^ "::"
   4.390 -      ^ Syntax.string_of_typ_global thy T);
   4.391 -    fun type_of_constr (cT as (_, T)) =
   4.392 -      let
   4.393 -        val frees = OldTerm.typ_tfrees T;
   4.394 -        val (tyco, vs) = ((apsnd o map) (dest_TFree) o dest_Type o snd o strip_type) T
   4.395 -          handle TYPE _ => no_constr cT
   4.396 -        val _ = if has_duplicates (eq_fst (op =)) vs then no_constr cT else ();
   4.397 -        val _ = if length frees <> length vs then no_constr cT else ();
   4.398 -      in (tyco, (vs, cT)) end;
   4.399 -
   4.400 -    val raw_cs = AList.group (op =) (map (type_of_constr o constr_of_term o prep_term thy) raw_ts);
   4.401 -    val _ = case map_filter (fn (tyco, _) =>
   4.402 -        if Symtab.defined (get_all thy) tyco then SOME tyco else NONE) raw_cs
   4.403 -     of [] => ()
   4.404 -      | tycos => error ("Type(s) " ^ commas (map quote tycos)
   4.405 -          ^ " already represented inductivly");
   4.406 -    val raw_vss = maps (map (map snd o fst) o snd) raw_cs;
   4.407 -    val ms = case distinct (op =) (map length raw_vss)
   4.408 -     of [n] => 0 upto n - 1
   4.409 -      | _ => error ("Different types in given constructors");
   4.410 -    fun inter_sort m = map (fn xs => nth xs m) raw_vss
   4.411 -      |> Library.foldr1 (Sorts.inter_sort (Sign.classes_of thy))
   4.412 -    val sorts = map inter_sort ms;
   4.413 -    val vs = Name.names Name.context Name.aT sorts;
   4.414 -
   4.415 -    fun norm_constr (raw_vs, (c, T)) = (c, map_atyps
   4.416 -      (TFree o (the o AList.lookup (op =) (map fst raw_vs ~~ vs)) o fst o dest_TFree) T);
   4.417 -
   4.418 -    val cs = map (apsnd (map norm_constr)) raw_cs;
   4.419 -    val dtyps_of_typ = map (dtyp_of_typ (map (rpair (map fst vs) o fst) cs))
   4.420 -      o fst o strip_type;
   4.421 -    val dt_names = map fst cs;
   4.422 -
   4.423 -    fun mk_spec (i, (tyco, constr)) = (i, (tyco,
   4.424 -      map (DtTFree o fst) vs,
   4.425 -      (map o apsnd) dtyps_of_typ constr))
   4.426 -    val descr = map_index mk_spec cs;
   4.427 -    val injs = DatatypeProp.make_injs [descr] vs;
   4.428 -    val half_distincts = map snd (DatatypeProp.make_distincts [descr] vs);
   4.429 -    val ind = DatatypeProp.make_ind [descr] vs;
   4.430 -    val rules = (map o map o map) Logic.close_form [[[ind]], injs, half_distincts];
   4.431 -
   4.432 -    fun after_qed' raw_thms =
   4.433 -      let
   4.434 -        val [[[raw_induct]], raw_inject, half_distinct] =
   4.435 -          unflat rules (map Drule.zero_var_indexes_list raw_thms);
   4.436 -            (*FIXME somehow dubious*)
   4.437 -      in
   4.438 -        ProofContext.theory_result
   4.439 -          (prove_rep_datatype config dt_names alt_names descr vs
   4.440 -            raw_inject half_distinct raw_induct)
   4.441 -        #-> after_qed
   4.442 -      end;
   4.443 -  in
   4.444 -    thy
   4.445 -    |> ProofContext.init
   4.446 -    |> Proof.theorem_i NONE after_qed' ((map o map) (rpair []) (flat rules))
   4.447 -  end;
   4.448 -
   4.449 -val rep_datatype = gen_rep_datatype Sign.cert_term;
   4.450 -val rep_datatype_cmd = gen_rep_datatype Syntax.read_term_global default_config (K I);
   4.451 -
   4.452 -
   4.453 -
   4.454 -(** package setup **)
   4.455 -
   4.456 -(* setup theory *)
   4.457 -
   4.458 -val setup =
   4.459 -  trfun_setup #>
   4.460 -  Datatype_Interpretation.init;
   4.461 -
   4.462 -
   4.463 -(* outer syntax *)
   4.464 -
   4.465 -local
   4.466 -
   4.467 -structure P = OuterParse and K = OuterKeyword
   4.468 -
   4.469 -in
   4.470 -
   4.471 -val _ =
   4.472 -  OuterSyntax.command "rep_datatype" "represent existing types inductively" K.thy_goal
   4.473 -    (Scan.option (P.$$$ "(" |-- Scan.repeat1 P.name --| P.$$$ ")") -- Scan.repeat1 P.term
   4.474 -      >> (fn (alt_names, ts) => Toplevel.print
   4.475 -           o Toplevel.theory_to_proof (rep_datatype_cmd alt_names ts)));
   4.476 +open Datatype_Data;
   4.477 +open DatatypeRepProofs;
   4.478  
   4.479  end;
   4.480 -
   4.481 -end;
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/src/HOL/Tools/Datatype/datatype_data.ML	Fri Nov 27 08:41:10 2009 +0100
     5.3 @@ -0,0 +1,471 @@
     5.4 +(*  Title:      HOL/Tools/datatype.ML
     5.5 +    Author:     Stefan Berghofer, TU Muenchen
     5.6 +
     5.7 +Datatype package for Isabelle/HOL.
     5.8 +*)
     5.9 +
    5.10 +signature DATATYPE_DATA =
    5.11 +sig
    5.12 +  include DATATYPE_COMMON
    5.13 +  val derive_datatype_props : config -> string list -> string list option
    5.14 +    -> descr list -> (string * sort) list -> thm -> thm list list -> thm list list
    5.15 +    -> theory -> string list * theory
    5.16 +  val rep_datatype : config -> (string list -> Proof.context -> Proof.context)
    5.17 +    -> string list option -> term list -> theory -> Proof.state
    5.18 +  val rep_datatype_cmd : string list option -> string list -> theory -> Proof.state
    5.19 +  val get_info : theory -> string -> info option
    5.20 +  val the_info : theory -> string -> info
    5.21 +  val the_descr : theory -> string list
    5.22 +    -> descr * (string * sort) list * string list
    5.23 +      * string * (string list * string list) * (typ list * typ list)
    5.24 +  val the_spec : theory -> string -> (string * sort) list * (string * typ list) list
    5.25 +  val all_distincts : theory -> typ list -> thm list list
    5.26 +  val get_constrs : theory -> string -> (string * typ) list option
    5.27 +  val get_all : theory -> info Symtab.table
    5.28 +  val info_of_constr : theory -> string * typ -> info option
    5.29 +  val info_of_case : theory -> string -> info option
    5.30 +  val interpretation : (config -> string list -> theory -> theory) -> theory -> theory
    5.31 +  val make_case :  Proof.context -> DatatypeCase.config -> string list -> term ->
    5.32 +    (term * term) list -> term * (term * (int * bool)) list
    5.33 +  val strip_case : Proof.context -> bool -> term -> (term * (term * term) list) option
    5.34 +  val read_typ: theory -> string -> (string * sort) list -> typ * (string * sort) list
    5.35 +  val cert_typ: theory -> typ -> (string * sort) list -> typ * (string * sort) list
    5.36 +  val mk_case_names_induct: descr -> attribute
    5.37 +  val setup: theory -> theory
    5.38 +end;
    5.39 +
    5.40 +structure Datatype_Data: DATATYPE_DATA =
    5.41 +struct
    5.42 +
    5.43 +open DatatypeAux;
    5.44 +
    5.45 +(** theory data **)
    5.46 +
    5.47 +(* data management *)
    5.48 +
    5.49 +structure DatatypesData = Theory_Data
    5.50 +(
    5.51 +  type T =
    5.52 +    {types: info Symtab.table,
    5.53 +     constrs: (string * info) list Symtab.table,
    5.54 +     cases: info Symtab.table};
    5.55 +
    5.56 +  val empty =
    5.57 +    {types = Symtab.empty, constrs = Symtab.empty, cases = Symtab.empty};
    5.58 +  val extend = I;
    5.59 +  fun merge
    5.60 +    ({types = types1, constrs = constrs1, cases = cases1},
    5.61 +     {types = types2, constrs = constrs2, cases = cases2}) : T =
    5.62 +    {types = Symtab.merge (K true) (types1, types2),
    5.63 +     constrs = Symtab.join (K (AList.merge (op =) (K true))) (constrs1, constrs2),
    5.64 +     cases = Symtab.merge (K true) (cases1, cases2)};
    5.65 +);
    5.66 +
    5.67 +val get_all = #types o DatatypesData.get;
    5.68 +val get_info = Symtab.lookup o get_all;
    5.69 +
    5.70 +fun the_info thy name =
    5.71 +  (case get_info thy name of
    5.72 +    SOME info => info
    5.73 +  | NONE => error ("Unknown datatype " ^ quote name));
    5.74 +
    5.75 +fun info_of_constr thy (c, T) =
    5.76 +  let
    5.77 +    val tab = Symtab.lookup_list ((#constrs o DatatypesData.get) thy) c;
    5.78 +    val hint = case strip_type T of (_, Type (tyco, _)) => SOME tyco | _ => NONE;
    5.79 +    val default = if null tab then NONE
    5.80 +      else SOME (snd (Library.last_elem tab))
    5.81 +        (*conservative wrt. overloaded constructors*);
    5.82 +  in case hint
    5.83 +   of NONE => default
    5.84 +    | SOME tyco => case AList.lookup (op =) tab tyco
    5.85 +       of NONE => default (*permissive*)
    5.86 +        | SOME info => SOME info
    5.87 +  end;
    5.88 +
    5.89 +val info_of_case = Symtab.lookup o #cases o DatatypesData.get;
    5.90 +
    5.91 +fun register (dt_infos : (string * info) list) =
    5.92 +  DatatypesData.map (fn {types, constrs, cases} =>
    5.93 +    {types = types |> fold Symtab.update dt_infos,
    5.94 +     constrs = constrs |> fold (fn (constr, dtname_info) =>
    5.95 +         Symtab.map_default (constr, []) (cons dtname_info))
    5.96 +       (maps (fn (dtname, info as {descr, index, ...}) =>
    5.97 +          map (rpair (dtname, info) o fst)
    5.98 +            (#3 (the (AList.lookup op = descr index)))) dt_infos),
    5.99 +     cases = cases |> fold Symtab.update
   5.100 +       (map (fn (_, info as {case_name, ...}) => (case_name, info)) dt_infos)});
   5.101 +
   5.102 +
   5.103 +(* complex queries *)
   5.104 +
   5.105 +fun the_spec thy dtco =
   5.106 +  let
   5.107 +    val info as { descr, index, sorts = raw_sorts, ... } = the_info thy dtco;
   5.108 +    val SOME (_, dtys, raw_cos) = AList.lookup (op =) descr index;
   5.109 +    val sorts = map ((fn v => (v, (the o AList.lookup (op =) raw_sorts) v))
   5.110 +      o DatatypeAux.dest_DtTFree) dtys;
   5.111 +    val cos = map
   5.112 +      (fn (co, tys) => (co, map (DatatypeAux.typ_of_dtyp descr sorts) tys)) raw_cos;
   5.113 +  in (sorts, cos) end;
   5.114 +
   5.115 +fun the_descr thy (raw_tycos as raw_tyco :: _) =
   5.116 +  let
   5.117 +    val info = the_info thy raw_tyco;
   5.118 +    val descr = #descr info;
   5.119 +
   5.120 +    val SOME (_, dtys, raw_cos) = AList.lookup (op =) descr (#index info);
   5.121 +    val vs = map ((fn v => (v, (the o AList.lookup (op =) (#sorts info)) v))
   5.122 +      o dest_DtTFree) dtys;
   5.123 +
   5.124 +    fun is_DtTFree (DtTFree _) = true
   5.125 +      | is_DtTFree _ = false
   5.126 +    val k = find_index (fn (_, (_, dTs, _)) => not (forall is_DtTFree dTs)) descr;
   5.127 +    val protoTs as (dataTs, _) = chop k descr
   5.128 +      |> (pairself o map) (fn (_, (tyco, dTs, _)) => (tyco, map (typ_of_dtyp descr vs) dTs));
   5.129 +    
   5.130 +    val tycos = map fst dataTs;
   5.131 +    val _ = if eq_set (op =) (tycos, raw_tycos) then ()
   5.132 +      else error ("Type constructors " ^ commas (map quote raw_tycos)
   5.133 +        ^ " do not belong exhaustively to one mutual recursive datatype");
   5.134 +
   5.135 +    val (Ts, Us) = (pairself o map) Type protoTs;
   5.136 +
   5.137 +    val names = map Long_Name.base_name (the_default tycos (#alt_names info));
   5.138 +    val (auxnames, _) = Name.make_context names
   5.139 +      |> fold_map (yield_singleton Name.variants o name_of_typ) Us;
   5.140 +    val prefix = space_implode "_" names;
   5.141 +
   5.142 +  in (descr, vs, tycos, prefix, (names, auxnames), (Ts, Us)) end;
   5.143 +
   5.144 +fun all_distincts thy Ts =
   5.145 +  let
   5.146 +    fun add_tycos (Type (tyco, Ts)) = insert (op =) tyco #> fold add_tycos Ts
   5.147 +      | add_tycos _ = I;
   5.148 +    val tycos = fold add_tycos Ts [];
   5.149 +  in map_filter (Option.map #distinct o get_info thy) tycos end;
   5.150 +
   5.151 +fun get_constrs thy dtco =
   5.152 +  case try (the_spec thy) dtco
   5.153 +   of SOME (sorts, cos) =>
   5.154 +        let
   5.155 +          fun subst (v, sort) = TVar ((v, 0), sort);
   5.156 +          fun subst_ty (TFree v) = subst v
   5.157 +            | subst_ty ty = ty;
   5.158 +          val dty = Type (dtco, map subst sorts);
   5.159 +          fun mk_co (co, tys) = (co, map (Term.map_atyps subst_ty) tys ---> dty);
   5.160 +        in SOME (map mk_co cos) end
   5.161 +    | NONE => NONE;
   5.162 +
   5.163 +
   5.164 +
   5.165 +(** various auxiliary **)
   5.166 +
   5.167 +(* prepare datatype specifications *)
   5.168 +
   5.169 +fun read_typ thy str sorts =
   5.170 +  let
   5.171 +    val ctxt = ProofContext.init thy
   5.172 +      |> fold (Variable.declare_typ o TFree) sorts;
   5.173 +    val T = Syntax.read_typ ctxt str;
   5.174 +  in (T, Term.add_tfreesT T sorts) end;
   5.175 +
   5.176 +fun cert_typ sign raw_T sorts =
   5.177 +  let
   5.178 +    val T = Type.no_tvars (Sign.certify_typ sign raw_T)
   5.179 +      handle TYPE (msg, _, _) => error msg;
   5.180 +    val sorts' = Term.add_tfreesT T sorts;
   5.181 +    val _ =
   5.182 +      case duplicates (op =) (map fst sorts') of
   5.183 +        [] => ()
   5.184 +      | dups => error ("Inconsistent sort constraints for " ^ commas dups)
   5.185 +  in (T, sorts') end;
   5.186 +
   5.187 +
   5.188 +(* case names *)
   5.189 +
   5.190 +local
   5.191 +
   5.192 +fun dt_recs (DtTFree _) = []
   5.193 +  | dt_recs (DtType (_, dts)) = maps dt_recs dts
   5.194 +  | dt_recs (DtRec i) = [i];
   5.195 +
   5.196 +fun dt_cases (descr: descr) (_, args, constrs) =
   5.197 +  let
   5.198 +    fun the_bname i = Long_Name.base_name (#1 (the (AList.lookup (op =) descr i)));
   5.199 +    val bnames = map the_bname (distinct (op =) (maps dt_recs args));
   5.200 +  in map (fn (c, _) => space_implode "_" (Long_Name.base_name c :: bnames)) constrs end;
   5.201 +
   5.202 +fun induct_cases descr =
   5.203 +  DatatypeProp.indexify_names (maps (dt_cases descr) (map #2 descr));
   5.204 +
   5.205 +fun exhaust_cases descr i = dt_cases descr (the (AList.lookup (op =) descr i));
   5.206 +
   5.207 +in
   5.208 +
   5.209 +fun mk_case_names_induct descr = Rule_Cases.case_names (induct_cases descr);
   5.210 +
   5.211 +fun mk_case_names_exhausts descr new =
   5.212 +  map (Rule_Cases.case_names o exhaust_cases descr o #1)
   5.213 +    (filter (fn ((_, (name, _, _))) => member (op =) new name) descr);
   5.214 +
   5.215 +end;
   5.216 +
   5.217 +
   5.218 +(* translation rules for case *)
   5.219 +
   5.220 +fun make_case ctxt = DatatypeCase.make_case
   5.221 +  (info_of_constr (ProofContext.theory_of ctxt)) ctxt;
   5.222 +
   5.223 +fun strip_case ctxt = DatatypeCase.strip_case
   5.224 +  (info_of_case (ProofContext.theory_of ctxt));
   5.225 +
   5.226 +fun add_case_tr' case_names thy =
   5.227 +  Sign.add_advanced_trfuns ([], [],
   5.228 +    map (fn case_name =>
   5.229 +      let val case_name' = Sign.const_syntax_name thy case_name
   5.230 +      in (case_name', DatatypeCase.case_tr' info_of_case case_name')
   5.231 +      end) case_names, []) thy;
   5.232 +
   5.233 +val trfun_setup =
   5.234 +  Sign.add_advanced_trfuns ([],
   5.235 +    [("_case_syntax", DatatypeCase.case_tr true info_of_constr)],
   5.236 +    [], []);
   5.237 +
   5.238 +
   5.239 +
   5.240 +(** document antiquotation **)
   5.241 +
   5.242 +val _ = ThyOutput.antiquotation "datatype" Args.tyname
   5.243 +  (fn {source = src, context = ctxt, ...} => fn dtco =>
   5.244 +    let
   5.245 +      val thy = ProofContext.theory_of ctxt;
   5.246 +      val (vs, cos) = the_spec thy dtco;
   5.247 +      val ty = Type (dtco, map TFree vs);
   5.248 +      fun pretty_typ_bracket (ty as Type (_, _ :: _)) =
   5.249 +            Pretty.enclose "(" ")" [Syntax.pretty_typ ctxt ty]
   5.250 +        | pretty_typ_bracket ty =
   5.251 +            Syntax.pretty_typ ctxt ty;
   5.252 +      fun pretty_constr (co, tys) =
   5.253 +        (Pretty.block o Pretty.breaks)
   5.254 +          (Syntax.pretty_term ctxt (Const (co, tys ---> ty)) ::
   5.255 +            map pretty_typ_bracket tys);
   5.256 +      val pretty_datatype =
   5.257 +        Pretty.block
   5.258 +          (Pretty.command "datatype" :: Pretty.brk 1 ::
   5.259 +           Syntax.pretty_typ ctxt ty ::
   5.260 +           Pretty.str " =" :: Pretty.brk 1 ::
   5.261 +           flat (separate [Pretty.brk 1, Pretty.str "| "]
   5.262 +             (map (single o pretty_constr) cos)));
   5.263 +    in ThyOutput.output (ThyOutput.maybe_pretty_source (K pretty_datatype) src [()]) end);
   5.264 +
   5.265 +
   5.266 +
   5.267 +(** abstract theory extensions relative to a datatype characterisation **)
   5.268 +
   5.269 +structure Datatype_Interpretation = Interpretation
   5.270 +  (type T = config * string list val eq: T * T -> bool = eq_snd op =);
   5.271 +fun interpretation f = Datatype_Interpretation.interpretation (uncurry f);
   5.272 +
   5.273 +fun make_dt_info alt_names descr sorts induct inducts rec_names rec_rewrites
   5.274 +    (index, (((((((((((_, (tname, _, _))), inject), distinct),
   5.275 +      exhaust), nchotomy), case_name), case_rewrites), case_cong), weak_case_cong),
   5.276 +        (split, split_asm))) =
   5.277 +  (tname,
   5.278 +   {index = index,
   5.279 +    alt_names = alt_names,
   5.280 +    descr = descr,
   5.281 +    sorts = sorts,
   5.282 +    inject = inject,
   5.283 +    distinct = distinct,
   5.284 +    induct = induct,
   5.285 +    inducts = inducts,
   5.286 +    exhaust = exhaust,
   5.287 +    nchotomy = nchotomy,
   5.288 +    rec_names = rec_names,
   5.289 +    rec_rewrites = rec_rewrites,
   5.290 +    case_name = case_name,
   5.291 +    case_rewrites = case_rewrites,
   5.292 +    case_cong = case_cong,
   5.293 +    weak_case_cong = weak_case_cong,
   5.294 +    split = split,
   5.295 +    split_asm = split_asm});
   5.296 +
   5.297 +fun derive_datatype_props config dt_names alt_names descr sorts
   5.298 +    induct inject distinct thy1 =
   5.299 +  let
   5.300 +    val thy2 = thy1 |> Theory.checkpoint;
   5.301 +    val flat_descr = flat descr;
   5.302 +    val new_type_names = map Long_Name.base_name (the_default dt_names alt_names);
   5.303 +    val _ = message config ("Deriving properties for datatype(s) " ^ commas_quote new_type_names);
   5.304 +
   5.305 +    val (exhaust, thy3) = DatatypeAbsProofs.prove_casedist_thms config new_type_names
   5.306 +      descr sorts induct (mk_case_names_exhausts flat_descr dt_names) thy2;
   5.307 +    val (nchotomys, thy4) = DatatypeAbsProofs.prove_nchotomys config new_type_names
   5.308 +      descr sorts exhaust thy3;
   5.309 +    val ((rec_names, rec_rewrites), thy5) = DatatypeAbsProofs.prove_primrec_thms
   5.310 +      config new_type_names descr sorts (#inject o the o Symtab.lookup (get_all thy4))
   5.311 +      inject (distinct, all_distincts thy2 (get_rec_types flat_descr sorts))
   5.312 +      induct thy4;
   5.313 +    val ((case_rewrites, case_names), thy6) = DatatypeAbsProofs.prove_case_thms
   5.314 +      config new_type_names descr sorts rec_names rec_rewrites thy5;
   5.315 +    val (case_congs, thy7) = DatatypeAbsProofs.prove_case_congs new_type_names
   5.316 +      descr sorts nchotomys case_rewrites thy6;
   5.317 +    val (weak_case_congs, thy8) = DatatypeAbsProofs.prove_weak_case_congs new_type_names
   5.318 +      descr sorts thy7;
   5.319 +    val (splits, thy9) = DatatypeAbsProofs.prove_split_thms
   5.320 +      config new_type_names descr sorts inject distinct exhaust case_rewrites thy8;
   5.321 +
   5.322 +    val inducts = Project_Rule.projections (ProofContext.init thy2) induct;
   5.323 +    val dt_infos = map_index
   5.324 +      (make_dt_info alt_names flat_descr sorts induct inducts rec_names rec_rewrites)
   5.325 +      (hd descr ~~ inject ~~ distinct ~~ exhaust ~~ nchotomys ~~
   5.326 +        case_names ~~ case_rewrites ~~ case_congs ~~ weak_case_congs ~~ splits);
   5.327 +    val dt_names = map fst dt_infos;
   5.328 +    val prfx = Binding.qualify true (space_implode "_" new_type_names);
   5.329 +    val simps = flat (inject @ distinct @ case_rewrites) @ rec_rewrites;
   5.330 +    val named_rules = flat (map_index (fn (index, tname) =>
   5.331 +      [((Binding.empty, [nth inducts index]), [Induct.induct_type tname]),
   5.332 +       ((Binding.empty, [nth exhaust index]), [Induct.cases_type tname])]) dt_names);
   5.333 +    val unnamed_rules = map (fn induct =>
   5.334 +      ((Binding.empty, [induct]), [Rule_Cases.inner_rule, Induct.induct_type ""]))
   5.335 +        (drop (length dt_names) inducts);
   5.336 +  in
   5.337 +    thy9
   5.338 +    |> PureThy.add_thmss ([((prfx (Binding.name "simps"), simps), []),
   5.339 +        ((prfx (Binding.name "inducts"), inducts), []),
   5.340 +        ((prfx (Binding.name "splits"), maps (fn (x, y) => [x, y]) splits), []),
   5.341 +        ((Binding.empty, flat case_rewrites @ flat distinct @ rec_rewrites),
   5.342 +          [Simplifier.simp_add]),
   5.343 +        ((Binding.empty, rec_rewrites), [Code.add_default_eqn_attribute]),
   5.344 +        ((Binding.empty, flat inject), [iff_add]),
   5.345 +        ((Binding.empty, map (fn th => th RS notE) (flat distinct)),
   5.346 +          [Classical.safe_elim NONE]),
   5.347 +        ((Binding.empty, weak_case_congs), [Simplifier.attrib (op addcongs)])]
   5.348 +        @ named_rules @ unnamed_rules)
   5.349 +    |> snd
   5.350 +    |> add_case_tr' case_names
   5.351 +    |> register dt_infos
   5.352 +    |> Datatype_Interpretation.data (config, dt_names)
   5.353 +    |> pair dt_names
   5.354 +  end;
   5.355 +
   5.356 +
   5.357 +
   5.358 +(** declare existing type as datatype **)
   5.359 +
   5.360 +fun prove_rep_datatype config dt_names alt_names descr sorts
   5.361 +    raw_inject half_distinct raw_induct thy1 =
   5.362 +  let
   5.363 +    val raw_distinct = (map o maps) (fn thm => [thm, thm RS not_sym]) half_distinct;
   5.364 +    val new_type_names = map Long_Name.base_name (the_default dt_names alt_names);
   5.365 +    val (((inject, distinct), [induct]), thy2) =
   5.366 +      thy1
   5.367 +      |> store_thmss "inject" new_type_names raw_inject
   5.368 +      ||>> store_thmss "distinct" new_type_names raw_distinct
   5.369 +      ||> Sign.add_path (space_implode "_" new_type_names)
   5.370 +      ||>> PureThy.add_thms [((Binding.name "induct", raw_induct), [mk_case_names_induct descr])]
   5.371 +      ||> Sign.restore_naming thy1;
   5.372 +  in
   5.373 +    thy2
   5.374 +    |> derive_datatype_props config dt_names alt_names [descr] sorts
   5.375 +         induct inject distinct
   5.376 + end;
   5.377 +
   5.378 +fun gen_rep_datatype prep_term config after_qed alt_names raw_ts thy =
   5.379 +  let
   5.380 +    fun constr_of_term (Const (c, T)) = (c, T)
   5.381 +      | constr_of_term t =
   5.382 +          error ("Not a constant: " ^ Syntax.string_of_term_global thy t);
   5.383 +    fun no_constr (c, T) = error ("Bad constructor: "
   5.384 +      ^ Sign.extern_const thy c ^ "::"
   5.385 +      ^ Syntax.string_of_typ_global thy T);
   5.386 +    fun type_of_constr (cT as (_, T)) =
   5.387 +      let
   5.388 +        val frees = OldTerm.typ_tfrees T;
   5.389 +        val (tyco, vs) = ((apsnd o map) (dest_TFree) o dest_Type o snd o strip_type) T
   5.390 +          handle TYPE _ => no_constr cT
   5.391 +        val _ = if has_duplicates (eq_fst (op =)) vs then no_constr cT else ();
   5.392 +        val _ = if length frees <> length vs then no_constr cT else ();
   5.393 +      in (tyco, (vs, cT)) end;
   5.394 +
   5.395 +    val raw_cs = AList.group (op =) (map (type_of_constr o constr_of_term o prep_term thy) raw_ts);
   5.396 +    val _ = case map_filter (fn (tyco, _) =>
   5.397 +        if Symtab.defined (get_all thy) tyco then SOME tyco else NONE) raw_cs
   5.398 +     of [] => ()
   5.399 +      | tycos => error ("Type(s) " ^ commas (map quote tycos)
   5.400 +          ^ " already represented inductivly");
   5.401 +    val raw_vss = maps (map (map snd o fst) o snd) raw_cs;
   5.402 +    val ms = case distinct (op =) (map length raw_vss)
   5.403 +     of [n] => 0 upto n - 1
   5.404 +      | _ => error ("Different types in given constructors");
   5.405 +    fun inter_sort m = map (fn xs => nth xs m) raw_vss
   5.406 +      |> Library.foldr1 (Sorts.inter_sort (Sign.classes_of thy))
   5.407 +    val sorts = map inter_sort ms;
   5.408 +    val vs = Name.names Name.context Name.aT sorts;
   5.409 +
   5.410 +    fun norm_constr (raw_vs, (c, T)) = (c, map_atyps
   5.411 +      (TFree o (the o AList.lookup (op =) (map fst raw_vs ~~ vs)) o fst o dest_TFree) T);
   5.412 +
   5.413 +    val cs = map (apsnd (map norm_constr)) raw_cs;
   5.414 +    val dtyps_of_typ = map (dtyp_of_typ (map (rpair (map fst vs) o fst) cs))
   5.415 +      o fst o strip_type;
   5.416 +    val dt_names = map fst cs;
   5.417 +
   5.418 +    fun mk_spec (i, (tyco, constr)) = (i, (tyco,
   5.419 +      map (DtTFree o fst) vs,
   5.420 +      (map o apsnd) dtyps_of_typ constr))
   5.421 +    val descr = map_index mk_spec cs;
   5.422 +    val injs = DatatypeProp.make_injs [descr] vs;
   5.423 +    val half_distincts = map snd (DatatypeProp.make_distincts [descr] vs);
   5.424 +    val ind = DatatypeProp.make_ind [descr] vs;
   5.425 +    val rules = (map o map o map) Logic.close_form [[[ind]], injs, half_distincts];
   5.426 +
   5.427 +    fun after_qed' raw_thms =
   5.428 +      let
   5.429 +        val [[[raw_induct]], raw_inject, half_distinct] =
   5.430 +          unflat rules (map Drule.zero_var_indexes_list raw_thms);
   5.431 +            (*FIXME somehow dubious*)
   5.432 +      in
   5.433 +        ProofContext.theory_result
   5.434 +          (prove_rep_datatype config dt_names alt_names descr vs
   5.435 +            raw_inject half_distinct raw_induct)
   5.436 +        #-> after_qed
   5.437 +      end;
   5.438 +  in
   5.439 +    thy
   5.440 +    |> ProofContext.init
   5.441 +    |> Proof.theorem_i NONE after_qed' ((map o map) (rpair []) (flat rules))
   5.442 +  end;
   5.443 +
   5.444 +val rep_datatype = gen_rep_datatype Sign.cert_term;
   5.445 +val rep_datatype_cmd = gen_rep_datatype Syntax.read_term_global default_config (K I);
   5.446 +
   5.447 +
   5.448 +
   5.449 +(** package setup **)
   5.450 +
   5.451 +(* setup theory *)
   5.452 +
   5.453 +val setup =
   5.454 +  trfun_setup #>
   5.455 +  Datatype_Interpretation.init;
   5.456 +
   5.457 +
   5.458 +(* outer syntax *)
   5.459 +
   5.460 +local
   5.461 +
   5.462 +structure P = OuterParse and K = OuterKeyword
   5.463 +
   5.464 +in
   5.465 +
   5.466 +val _ =
   5.467 +  OuterSyntax.command "rep_datatype" "represent existing types inductively" K.thy_goal
   5.468 +    (Scan.option (P.$$$ "(" |-- Scan.repeat1 P.name --| P.$$$ ")") -- Scan.repeat1 P.term
   5.469 +      >> (fn (alt_names, ts) => Toplevel.print
   5.470 +           o Toplevel.theory_to_proof (rep_datatype_cmd alt_names ts)));
   5.471 +
   5.472 +end;
   5.473 +
   5.474 +end;
     6.1 --- a/src/HOL/Tools/Datatype/datatype_rep_proofs.ML	Fri Nov 27 08:41:08 2009 +0100
     6.2 +++ b/src/HOL/Tools/Datatype/datatype_rep_proofs.ML	Fri Nov 27 08:41:10 2009 +0100
     6.3 @@ -10,8 +10,7 @@
     6.4  
     6.5  signature DATATYPE_REP_PROOFS =
     6.6  sig
     6.7 -  include DATATYPE_COMMON
     6.8 -  val add_datatype : config -> string list -> (string list * binding * mixfix *
     6.9 +  val add_datatype : DatatypeAux.config -> string list -> (string list * binding * mixfix *
    6.10      (binding * typ list * mixfix) list) list -> theory -> string list * theory
    6.11    val datatype_cmd : string list -> (string list * binding * mixfix *
    6.12      (binding * string list * mixfix) list) list -> theory -> theory
    6.13 @@ -50,6 +49,8 @@
    6.14  val In0_not_In1 = @{thm In0_not_In1};
    6.15  val In1_not_In0 = @{thm In1_not_In0};
    6.16  val Lim_inject = @{thm Lim_inject};
    6.17 +val Inl_inject = @{thm Inl_inject};
    6.18 +val Inr_inject = @{thm Inr_inject};
    6.19  val Suml_inject = @{thm Suml_inject};
    6.20  val Sumr_inject = @{thm Sumr_inject};
    6.21  
    6.22 @@ -697,7 +698,7 @@
    6.23      val (dts', constr_syntax, sorts', i) =
    6.24        fold2 prep_dt_spec dts new_type_names ([], [], [], 0);
    6.25      val sorts = sorts' @ map (rpair (Sign.defaultS tmp_thy)) (subtract (op =) (map fst sorts') tyvars);
    6.26 -    val dt_info = Datatype.get_all thy;
    6.27 +    val dt_info = Datatype_Data.get_all thy;
    6.28      val (descr, _) = unfold_datatypes tmp_thy dts' sorts dt_info dts' i;
    6.29      val _ = check_nonempty descr handle (exn as Datatype_Empty s) =>
    6.30        if #strict config then error ("Nonemptiness check failed for datatype " ^ s)
    6.31 @@ -708,14 +709,14 @@
    6.32    in
    6.33      thy
    6.34      |> representation_proofs config dt_info new_type_names descr sorts
    6.35 -        types_syntax constr_syntax (Datatype.mk_case_names_induct (flat descr))
    6.36 -    |-> (fn (inject, distinct, induct) => Datatype.derive_datatype_props
    6.37 +        types_syntax constr_syntax (Datatype_Data.mk_case_names_induct (flat descr))
    6.38 +    |-> (fn (inject, distinct, induct) => Datatype_Data.derive_datatype_props
    6.39          config dt_names (SOME new_type_names) descr sorts
    6.40          induct inject distinct)
    6.41    end;
    6.42  
    6.43 -val add_datatype = gen_add_datatype Datatype.cert_typ;
    6.44 -val datatype_cmd = snd ooo gen_add_datatype Datatype.read_typ default_config;
    6.45 +val add_datatype = gen_add_datatype Datatype_Data.cert_typ;
    6.46 +val datatype_cmd = snd ooo gen_add_datatype Datatype_Data.read_typ default_config;
    6.47  
    6.48  local
    6.49  
    6.50 @@ -744,11 +745,3 @@
    6.51  end;
    6.52  
    6.53  end;
    6.54 -
    6.55 -structure Datatype =
    6.56 -struct
    6.57 -
    6.58 -open Datatype;
    6.59 -open DatatypeRepProofs;
    6.60 -
    6.61 -end;
     7.1 --- a/src/HOL/Tools/inductive_codegen.ML	Fri Nov 27 08:41:08 2009 +0100
     7.2 +++ b/src/HOL/Tools/inductive_codegen.ML	Fri Nov 27 08:41:10 2009 +0100
     7.3 @@ -104,7 +104,7 @@
     7.4    let
     7.5      val cnstrs = flat (maps
     7.6        (map (fn (_, (_, _, cs)) => map (apsnd length) cs) o #descr o snd)
     7.7 -      (Symtab.dest (Datatype.get_all thy)));
     7.8 +      (Symtab.dest (Datatype_Data.get_all thy)));
     7.9      fun check t = (case strip_comb t of
    7.10          (Var _, []) => true
    7.11        | (Const (s, _), ts) => (case AList.lookup (op =) cnstrs s of
     8.1 --- a/src/HOL/Tools/old_primrec.ML	Fri Nov 27 08:41:08 2009 +0100
     8.2 +++ b/src/HOL/Tools/old_primrec.ML	Fri Nov 27 08:41:10 2009 +0100
     8.3 @@ -246,7 +246,7 @@
     8.4  fun gen_primrec_i note def alt_name eqns_atts thy =
     8.5    let
     8.6      val (eqns, atts) = split_list eqns_atts;
     8.7 -    val dt_info = Datatype.get_all thy;
     8.8 +    val dt_info = Datatype_Data.get_all thy;
     8.9      val rec_eqns = fold_rev (process_eqn thy o snd) eqns [] ;
    8.10      val tnames = distinct (op =) (map (#1 o snd) rec_eqns);
    8.11      val dts = find_dts dt_info tnames tnames;
     9.1 --- a/src/HOL/Tools/primrec.ML	Fri Nov 27 08:41:08 2009 +0100
     9.2 +++ b/src/HOL/Tools/primrec.ML	Fri Nov 27 08:41:10 2009 +0100
     9.3 @@ -220,7 +220,7 @@
     9.4      val eqns = fold_rev (process_eqn (fn v => Variable.is_fixed lthy v
     9.5        orelse exists (fn ((w, _), _) => v = Binding.name_of w) fixes)) eqs [];
     9.6      val tnames = distinct (op =) (map (#1 o snd) eqns);
     9.7 -    val dts = find_dts (Datatype.get_all (ProofContext.theory_of lthy)) tnames tnames;
     9.8 +    val dts = find_dts (Datatype_Data.get_all (ProofContext.theory_of lthy)) tnames tnames;
     9.9      val main_fns = map (fn (tname, {index, ...}) =>
    9.10        (index, (fst o the o find_first (fn (_, x) => #1 x = tname)) eqns)) dts;
    9.11      val {descr, rec_names, rec_rewrites, ...} =