modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
authorhaftmann
Mon Nov 30 11:42:49 2009 +0100 (2009-11-30)
changeset 33968f94fb13ecbb3
parent 33967 e191b400a8e0
child 33969 1e7ca47c6c3d
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
src/HOL/Datatype.thy
src/HOL/Inductive.thy
src/HOL/IsaMakefile
src/HOL/List.thy
src/HOL/Nominal/nominal_datatype.ML
src/HOL/Nominal/nominal_inductive.ML
src/HOL/Nominal/nominal_inductive2.ML
src/HOL/Nominal/nominal_primrec.ML
src/HOL/Tools/Datatype/datatype.ML
src/HOL/Tools/Datatype/datatype_abs_proofs.ML
src/HOL/Tools/Datatype/datatype_aux.ML
src/HOL/Tools/Datatype/datatype_case.ML
src/HOL/Tools/Datatype/datatype_codegen.ML
src/HOL/Tools/Datatype/datatype_data.ML
src/HOL/Tools/Datatype/datatype_prop.ML
src/HOL/Tools/Datatype/datatype_realizer.ML
src/HOL/Tools/Datatype/datatype_rep_proofs.ML
src/HOL/Tools/Function/size.ML
src/HOL/Tools/Nitpick/nitpick_hol.ML
src/HOL/Tools/Predicate_Compile/predicate_compile_core.ML
src/HOL/Tools/inductive_realizer.ML
src/HOL/Tools/old_primrec.ML
src/HOL/Tools/primrec.ML
src/HOL/Tools/quickcheck_generators.ML
src/HOL/Tools/refute.ML
     1.1 --- a/src/HOL/Datatype.thy	Mon Nov 30 11:42:48 2009 +0100
     1.2 +++ b/src/HOL/Datatype.thy	Mon Nov 30 11:42:49 2009 +0100
     1.3 @@ -1,16 +1,13 @@
     1.4  (*  Title:      HOL/Datatype.thy
     1.5      Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     1.6      Author:     Stefan Berghofer and Markus Wenzel, TU Muenchen
     1.7 -
     1.8 -Could <*> be generalized to a general summation (Sigma)?
     1.9  *)
    1.10  
    1.11 -header {* Analogues of the Cartesian Product and Disjoint Sum for Datatypes *}
    1.12 +header {* Datatype package: constructing datatypes from Cartesian Products and Disjoint Sums *}
    1.13  
    1.14  theory Datatype
    1.15  imports Product_Type Sum_Type Nat
    1.16  uses
    1.17 -  ("Tools/Datatype/datatype_rep_proofs.ML")
    1.18    ("Tools/Datatype/datatype.ML")
    1.19    ("Tools/inductive_realizer.ML")
    1.20    ("Tools/Datatype/datatype_realizer.ML")
    1.21 @@ -520,13 +517,12 @@
    1.22  hide (open) type node item
    1.23  hide (open) const Push Node Atom Leaf Numb Lim Split Case
    1.24  
    1.25 -use "Tools/Datatype/datatype_rep_proofs.ML"
    1.26  use "Tools/Datatype/datatype.ML"
    1.27  
    1.28  use "Tools/inductive_realizer.ML"
    1.29  setup InductiveRealizer.setup
    1.30  
    1.31  use "Tools/Datatype/datatype_realizer.ML"
    1.32 -setup DatatypeRealizer.setup
    1.33 +setup Datatype_Realizer.setup
    1.34  
    1.35  end
     2.1 --- a/src/HOL/Inductive.thy	Mon Nov 30 11:42:48 2009 +0100
     2.2 +++ b/src/HOL/Inductive.thy	Mon Nov 30 11:42:49 2009 +0100
     2.3 @@ -280,12 +280,12 @@
     2.4  use "Tools/Datatype/datatype_data.ML"
     2.5  setup Datatype_Data.setup
     2.6  
     2.7 +use "Tools/Datatype/datatype_codegen.ML"
     2.8 +setup Datatype_Codegen.setup
     2.9 +
    2.10  use "Tools/old_primrec.ML"
    2.11  use "Tools/primrec.ML"
    2.12  
    2.13 -use "Tools/Datatype/datatype_codegen.ML"
    2.14 -setup DatatypeCodegen.setup
    2.15 -
    2.16  use "Tools/inductive_codegen.ML"
    2.17  setup InductiveCodegen.setup
    2.18  
    2.19 @@ -301,7 +301,7 @@
    2.20    fun fun_tr ctxt [cs] =
    2.21      let
    2.22        val x = Free (Name.variant (Term.add_free_names cs []) "x", dummyT);
    2.23 -      val ft = DatatypeCase.case_tr true Datatype_Data.info_of_constr
    2.24 +      val ft = Datatype_Case.case_tr true Datatype_Data.info_of_constr
    2.25                   ctxt [x, cs]
    2.26      in lambda x ft end
    2.27  in [("_lam_pats_syntax", fun_tr)] end
     3.1 --- a/src/HOL/IsaMakefile	Mon Nov 30 11:42:48 2009 +0100
     3.2 +++ b/src/HOL/IsaMakefile	Mon Nov 30 11:42:49 2009 +0100
     3.3 @@ -171,7 +171,6 @@
     3.4    Tools/Datatype/datatype_data.ML \
     3.5    Tools/Datatype/datatype_prop.ML \
     3.6    Tools/Datatype/datatype_realizer.ML \
     3.7 -  Tools/Datatype/datatype_rep_proofs.ML \
     3.8    Tools/Datatype/datatype.ML \
     3.9    Tools/dseq.ML \
    3.10    Tools/Function/context_tree.ML \
     4.1 --- a/src/HOL/List.thy	Mon Nov 30 11:42:48 2009 +0100
     4.2 +++ b/src/HOL/List.thy	Mon Nov 30 11:42:49 2009 +0100
     4.3 @@ -366,7 +366,7 @@
     4.4        val case2 = Syntax.const "_case1" $ Syntax.const Term.dummy_patternN
     4.5                                          $ NilC;
     4.6        val cs = Syntax.const "_case2" $ case1 $ case2
     4.7 -      val ft = DatatypeCase.case_tr false Datatype.info_of_constr
     4.8 +      val ft = Datatype_Case.case_tr false Datatype.info_of_constr
     4.9                   ctxt [x, cs]
    4.10      in lambda x ft end;
    4.11  
     5.1 --- a/src/HOL/Nominal/nominal_datatype.ML	Mon Nov 30 11:42:48 2009 +0100
     5.2 +++ b/src/HOL/Nominal/nominal_datatype.ML	Mon Nov 30 11:42:49 2009 +0100
     5.3 @@ -37,7 +37,7 @@
     5.4  val Collect_False_empty = @{thm empty_def [THEN sym, THEN eq_reflection]};
     5.5  val empty_iff = thm "empty_iff";
     5.6  
     5.7 -open DatatypeAux;
     5.8 +open Datatype_Aux;
     5.9  open NominalAtoms;
    5.10  
    5.11  (** FIXME: Datatype should export this function **)
    5.12 @@ -56,7 +56,7 @@
    5.13  
    5.14  
    5.15  fun induct_cases descr =
    5.16 -  DatatypeProp.indexify_names (maps (dt_cases descr) (map #2 descr));
    5.17 +  Datatype_Prop.indexify_names (maps (dt_cases descr) (map #2 descr));
    5.18  
    5.19  fun exhaust_cases descr i = dt_cases descr (the (AList.lookup (op =) descr i));
    5.20  
    5.21 @@ -258,7 +258,7 @@
    5.22      val perm_types = map (fn (i, _) =>
    5.23        let val T = nth_dtyp i
    5.24        in permT --> T --> T end) descr;
    5.25 -    val perm_names' = DatatypeProp.indexify_names (map (fn (i, _) =>
    5.26 +    val perm_names' = Datatype_Prop.indexify_names (map (fn (i, _) =>
    5.27        "perm_" ^ name_of_typ (nth_dtyp i)) descr);
    5.28      val perm_names = replicate (length new_type_names) "Nominal.perm" @
    5.29        map (Sign.full_bname thy1) (List.drop (perm_names', length new_type_names));
    5.30 @@ -270,7 +270,7 @@
    5.31        in map (fn (cname, dts) =>
    5.32          let
    5.33            val Ts = map (typ_of_dtyp descr sorts) dts;
    5.34 -          val names = Name.variant_list ["pi"] (DatatypeProp.make_tnames Ts);
    5.35 +          val names = Name.variant_list ["pi"] (Datatype_Prop.make_tnames Ts);
    5.36            val args = map Free (names ~~ Ts);
    5.37            val c = Const (cname, Ts ---> T);
    5.38            fun perm_arg (dt, x) =
    5.39 @@ -307,7 +307,7 @@
    5.40      val _ = warning ("length descr: " ^ string_of_int (length descr));
    5.41      val _ = warning ("length new_type_names: " ^ string_of_int (length new_type_names));
    5.42  
    5.43 -    val perm_indnames = DatatypeProp.make_tnames (map body_type perm_types);
    5.44 +    val perm_indnames = Datatype_Prop.make_tnames (map body_type perm_types);
    5.45      val perm_fun_def = PureThy.get_thm thy2 "perm_fun_def";
    5.46  
    5.47      val unfolded_perm_eq_thms =
    5.48 @@ -502,10 +502,10 @@
    5.49  
    5.50      val _ = warning "representing sets";
    5.51  
    5.52 -    val rep_set_names = DatatypeProp.indexify_names
    5.53 +    val rep_set_names = Datatype_Prop.indexify_names
    5.54        (map (fn (i, _) => name_of_typ (nth_dtyp i) ^ "_set") descr);
    5.55      val big_rep_name =
    5.56 -      space_implode "_" (DatatypeProp.indexify_names (map_filter
    5.57 +      space_implode "_" (Datatype_Prop.indexify_names (map_filter
    5.58          (fn (i, ("Nominal.noption", _, _)) => NONE
    5.59            | (i, _) => SOME (name_of_typ (nth_dtyp i))) descr)) ^ "_set";
    5.60      val _ = warning ("big_rep_name: " ^ big_rep_name);
    5.61 @@ -867,7 +867,7 @@
    5.62  
    5.63      (* prove distinctness theorems *)
    5.64  
    5.65 -    val distinct_props = DatatypeProp.make_distincts descr' sorts;
    5.66 +    val distinct_props = Datatype_Prop.make_distincts descr' sorts;
    5.67      val dist_rewrites = map2 (fn rep_thms => fn dist_lemma =>
    5.68        dist_lemma :: rep_thms @ [In0_eq, In1_eq, In0_not_In1, In1_not_In0])
    5.69          constr_rep_thmss dist_lemmas;
    5.70 @@ -1067,7 +1067,7 @@
    5.71  
    5.72      val Abs_inverse_thms' = map (fn r => r RS subst) Abs_inverse_thms;
    5.73  
    5.74 -    val dt_induct_prop = DatatypeProp.make_ind descr' sorts;
    5.75 +    val dt_induct_prop = Datatype_Prop.make_ind descr' sorts;
    5.76      val dt_induct = Goal.prove_global thy8 []
    5.77        (Logic.strip_imp_prems dt_induct_prop) (Logic.strip_imp_concl dt_induct_prop)
    5.78        (fn {prems, ...} => EVERY
    5.79 @@ -1085,7 +1085,7 @@
    5.80  
    5.81      val _ = warning "proving finite support for the new datatype";
    5.82  
    5.83 -    val indnames = DatatypeProp.make_tnames recTs;
    5.84 +    val indnames = Datatype_Prop.make_tnames recTs;
    5.85  
    5.86      val abs_supp = PureThy.get_thms thy8 "abs_supp";
    5.87      val supp_atm = PureThy.get_thms thy8 "supp_atm";
    5.88 @@ -1120,12 +1120,12 @@
    5.89        PureThy.add_thms [((Binding.name "induct", dt_induct), [case_names_induct])] ||>>
    5.90        PureThy.add_thmss [((Binding.name "inducts", projections dt_induct), [case_names_induct])] ||>
    5.91        Sign.parent_path ||>>
    5.92 -      DatatypeAux.store_thmss_atts "distinct" new_type_names simp_atts distinct_thms ||>>
    5.93 -      DatatypeAux.store_thmss "constr_rep" new_type_names constr_rep_thmss ||>>
    5.94 -      DatatypeAux.store_thmss_atts "perm" new_type_names simp_eqvt_atts perm_simps' ||>>
    5.95 -      DatatypeAux.store_thmss "inject" new_type_names inject_thms ||>>
    5.96 -      DatatypeAux.store_thmss "supp" new_type_names supp_thms ||>>
    5.97 -      DatatypeAux.store_thmss_atts "fresh" new_type_names simp_atts fresh_thms ||>
    5.98 +      Datatype_Aux.store_thmss_atts "distinct" new_type_names simp_atts distinct_thms ||>>
    5.99 +      Datatype_Aux.store_thmss "constr_rep" new_type_names constr_rep_thmss ||>>
   5.100 +      Datatype_Aux.store_thmss_atts "perm" new_type_names simp_eqvt_atts perm_simps' ||>>
   5.101 +      Datatype_Aux.store_thmss "inject" new_type_names inject_thms ||>>
   5.102 +      Datatype_Aux.store_thmss "supp" new_type_names supp_thms ||>>
   5.103 +      Datatype_Aux.store_thmss_atts "fresh" new_type_names simp_atts fresh_thms ||>
   5.104        fold (fn (atom, ths) => fn thy =>
   5.105          let
   5.106            val class = fs_class_of thy atom;
   5.107 @@ -1145,7 +1145,7 @@
   5.108      val fsT' = TFree ("'n", HOLogic.typeS);
   5.109  
   5.110      val fresh_fs = map (fn (s, T) => (T, Free (s, fsT' --> HOLogic.mk_setT T)))
   5.111 -      (DatatypeProp.indexify_names (replicate (length dt_atomTs) "f") ~~ dt_atomTs);
   5.112 +      (Datatype_Prop.indexify_names (replicate (length dt_atomTs) "f") ~~ dt_atomTs);
   5.113  
   5.114      fun make_pred fsT i T =
   5.115        Free (List.nth (pnames, i), fsT --> T --> HOLogic.boolT);
   5.116 @@ -1167,7 +1167,7 @@
   5.117          val recs = filter is_rec_type cargs;
   5.118          val Ts = map (typ_of_dtyp descr'' sorts) cargs;
   5.119          val recTs' = map (typ_of_dtyp descr'' sorts) recs;
   5.120 -        val tnames = Name.variant_list pnames (DatatypeProp.make_tnames Ts);
   5.121 +        val tnames = Name.variant_list pnames (Datatype_Prop.make_tnames Ts);
   5.122          val rec_tnames = map fst (filter (is_rec_type o snd) (tnames ~~ cargs));
   5.123          val frees = tnames ~~ Ts;
   5.124          val frees' = partition_cargs idxs frees;
   5.125 @@ -1196,7 +1196,7 @@
   5.126        map (make_ind_prem fsT (fn T => fn t => fn u =>
   5.127          fresh_const T fsT $ t $ u) i T)
   5.128            (constrs ~~ idxss)) (descr'' ~~ ndescr ~~ recTs);
   5.129 -    val tnames = DatatypeProp.make_tnames recTs;
   5.130 +    val tnames = Datatype_Prop.make_tnames recTs;
   5.131      val zs = Name.variant_list tnames (replicate (length descr'') "z");
   5.132      val ind_concl = HOLogic.mk_Trueprop (foldr1 (HOLogic.mk_binop "op &")
   5.133        (map (fn ((((i, _), T), tname), z) =>
   5.134 @@ -1220,7 +1220,7 @@
   5.135      val induct' = Logic.list_implies (ind_prems', ind_concl');
   5.136  
   5.137      val aux_ind_vars =
   5.138 -      (DatatypeProp.indexify_names (replicate (length dt_atomTs) "pi") ~~
   5.139 +      (Datatype_Prop.indexify_names (replicate (length dt_atomTs) "pi") ~~
   5.140         map mk_permT dt_atomTs) @ [("z", fsT')];
   5.141      val aux_ind_Ts = rev (map snd aux_ind_vars);
   5.142      val aux_ind_concl = HOLogic.mk_Trueprop (foldr1 (HOLogic.mk_binop "op &")
   5.143 @@ -1418,7 +1418,7 @@
   5.144  
   5.145      val used = List.foldr OldTerm.add_typ_tfree_names [] recTs;
   5.146  
   5.147 -    val (rec_result_Ts', rec_fn_Ts') = DatatypeProp.make_primrec_Ts descr' sorts used;
   5.148 +    val (rec_result_Ts', rec_fn_Ts') = Datatype_Prop.make_primrec_Ts descr' sorts used;
   5.149  
   5.150      val rec_sort = if null dt_atomTs then HOLogic.typeS else
   5.151        Sign.certify_sort thy10 pt_cp_sort;
   5.152 @@ -1664,10 +1664,10 @@
   5.153      val fun_tuple = foldr1 HOLogic.mk_prod (rec_ctxt :: rec_fns);
   5.154      val fun_tupleT = fastype_of fun_tuple;
   5.155      val rec_unique_frees =
   5.156 -      DatatypeProp.indexify_names (replicate (length recTs) "x") ~~ recTs;
   5.157 +      Datatype_Prop.indexify_names (replicate (length recTs) "x") ~~ recTs;
   5.158      val rec_unique_frees'' = map (fn (s, T) => (s ^ "'", T)) rec_unique_frees;
   5.159      val rec_unique_frees' =
   5.160 -      DatatypeProp.indexify_names (replicate (length recTs) "y") ~~ rec_result_Ts;
   5.161 +      Datatype_Prop.indexify_names (replicate (length recTs) "y") ~~ rec_result_Ts;
   5.162      val rec_unique_concls = map (fn ((x, U), R) =>
   5.163          Const ("Ex1", (U --> HOLogic.boolT) --> HOLogic.boolT) $
   5.164            Abs ("y", U, R $ Free x $ Bound 0))
   5.165 @@ -2048,7 +2048,7 @@
   5.166               resolve_tac rec_intrs 1,
   5.167               REPEAT (solve (prems @ rec_total_thms) prems 1)])
   5.168        end) (rec_eq_prems ~~
   5.169 -        DatatypeProp.make_primrecs new_type_names descr' sorts thy12);
   5.170 +        Datatype_Prop.make_primrecs new_type_names descr' sorts thy12);
   5.171  
   5.172      val dt_infos = map_index (make_dt_info pdescr sorts induct reccomb_names rec_thms)
   5.173        (descr1 ~~ distinct_thms ~~ inject_thms);
     6.1 --- a/src/HOL/Nominal/nominal_inductive.ML	Mon Nov 30 11:42:48 2009 +0100
     6.2 +++ b/src/HOL/Nominal/nominal_inductive.ML	Mon Nov 30 11:42:49 2009 +0100
     6.3 @@ -247,7 +247,7 @@
     6.4        end) prems);
     6.5  
     6.6      val ind_vars =
     6.7 -      (DatatypeProp.indexify_names (replicate (length atomTs) "pi") ~~
     6.8 +      (Datatype_Prop.indexify_names (replicate (length atomTs) "pi") ~~
     6.9         map NominalAtoms.mk_permT atomTs) @ [("z", fsT)];
    6.10      val ind_Ts = rev (map snd ind_vars);
    6.11  
    6.12 @@ -647,7 +647,7 @@
    6.13      val thss = map (fn atom =>
    6.14        let val pi' = Free (pi, NominalAtoms.mk_permT (Type (atom, [])))
    6.15        in map (fn th => zero_var_indexes (th RS mp))
    6.16 -        (DatatypeAux.split_conj_thm (Goal.prove ctxt' [] []
    6.17 +        (Datatype_Aux.split_conj_thm (Goal.prove ctxt' [] []
    6.18            (HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj (map (fn p =>
    6.19              let
    6.20                val (h, ts) = strip_comb p;
     7.1 --- a/src/HOL/Nominal/nominal_inductive2.ML	Mon Nov 30 11:42:48 2009 +0100
     7.2 +++ b/src/HOL/Nominal/nominal_inductive2.ML	Mon Nov 30 11:42:49 2009 +0100
     7.3 @@ -263,7 +263,7 @@
     7.4        in abs_params params' prem end) prems);
     7.5  
     7.6      val ind_vars =
     7.7 -      (DatatypeProp.indexify_names (replicate (length atomTs) "pi") ~~
     7.8 +      (Datatype_Prop.indexify_names (replicate (length atomTs) "pi") ~~
     7.9         map NominalAtoms.mk_permT atomTs) @ [("z", fsT)];
    7.10      val ind_Ts = rev (map snd ind_vars);
    7.11  
     8.1 --- a/src/HOL/Nominal/nominal_primrec.ML	Mon Nov 30 11:42:48 2009 +0100
     8.2 +++ b/src/HOL/Nominal/nominal_primrec.ML	Mon Nov 30 11:42:49 2009 +0100
     8.3 @@ -21,7 +21,7 @@
     8.4  structure NominalPrimrec : NOMINAL_PRIMREC =
     8.5  struct
     8.6  
     8.7 -open DatatypeAux;
     8.8 +open Datatype_Aux;
     8.9  
    8.10  exception RecError of string;
    8.11  
     9.1 --- a/src/HOL/Tools/Datatype/datatype.ML	Mon Nov 30 11:42:48 2009 +0100
     9.2 +++ b/src/HOL/Tools/Datatype/datatype.ML	Mon Nov 30 11:42:49 2009 +0100
     9.3 @@ -1,19 +1,748 @@
     9.4 -(*  Title:      HOL/Tools/datatype.ML
     9.5 +(*  Title:      HOL/Tools/Datatype/datatype.ML
     9.6      Author:     Stefan Berghofer, TU Muenchen
     9.7  
     9.8 -Datatype package interface for Isabelle/HOL.
     9.9 +Datatype package: definitional introduction of datatypes
    9.10 +with proof of characteristic theorems: injectivity / distinctness
    9.11 +of constructors and induction.  Main interface to datatypes
    9.12 +after full bootstrap of datatype package.
    9.13  *)
    9.14  
    9.15  signature DATATYPE =
    9.16  sig
    9.17    include DATATYPE_DATA
    9.18 -  include DATATYPE_REP_PROOFS
    9.19 +  val add_datatype : config -> string list -> (string list * binding * mixfix *
    9.20 +    (binding * typ list * mixfix) list) list -> theory -> string list * theory
    9.21 +  val datatype_cmd : string list -> (string list * binding * mixfix *
    9.22 +    (binding * string list * mixfix) list) list -> theory -> theory
    9.23  end;
    9.24  
    9.25 -structure Datatype =
    9.26 +structure Datatype : DATATYPE =
    9.27  struct
    9.28  
    9.29 +(** auxiliary **)
    9.30 +
    9.31 +open Datatype_Aux;
    9.32  open Datatype_Data;
    9.33 -open DatatypeRepProofs;
    9.34 +
    9.35 +val (_ $ (_ $ (_ $ (distinct_f $ _) $ _))) = hd (prems_of distinct_lemma);
    9.36 +
    9.37 +val collect_simp = rewrite_rule [mk_meta_eq mem_Collect_eq];
    9.38 +
    9.39 +fun exh_thm_of (dt_info : info Symtab.table) tname =
    9.40 +  #exhaust (the (Symtab.lookup dt_info tname));
    9.41 +
    9.42 +val node_name = @{type_name "Datatype.node"};
    9.43 +val In0_name = @{const_name "Datatype.In0"};
    9.44 +val In1_name = @{const_name "Datatype.In1"};
    9.45 +val Scons_name = @{const_name "Datatype.Scons"};
    9.46 +val Leaf_name = @{const_name "Datatype.Leaf"};
    9.47 +val Numb_name = @{const_name "Datatype.Numb"};
    9.48 +val Lim_name = @{const_name "Datatype.Lim"};
    9.49 +val Suml_name = @{const_name "Sum_Type.Suml"};
    9.50 +val Sumr_name = @{const_name "Sum_Type.Sumr"};
    9.51 +
    9.52 +val In0_inject = @{thm In0_inject};
    9.53 +val In1_inject = @{thm In1_inject};
    9.54 +val Scons_inject = @{thm Scons_inject};
    9.55 +val Leaf_inject = @{thm Leaf_inject};
    9.56 +val In0_eq = @{thm In0_eq};
    9.57 +val In1_eq = @{thm In1_eq};
    9.58 +val In0_not_In1 = @{thm In0_not_In1};
    9.59 +val In1_not_In0 = @{thm In1_not_In0};
    9.60 +val Lim_inject = @{thm Lim_inject};
    9.61 +val Inl_inject = @{thm Inl_inject};
    9.62 +val Inr_inject = @{thm Inr_inject};
    9.63 +val Suml_inject = @{thm Suml_inject};
    9.64 +val Sumr_inject = @{thm Sumr_inject};
    9.65 +
    9.66 +
    9.67 +
    9.68 +(** proof of characteristic theorems **)
    9.69 +
    9.70 +fun representation_proofs (config : config) (dt_info : info Symtab.table)
    9.71 +      new_type_names descr sorts types_syntax constr_syntax case_names_induct thy =
    9.72 +  let
    9.73 +    val descr' = flat descr;
    9.74 +    val big_name = space_implode "_" new_type_names;
    9.75 +    val thy1 = Sign.add_path big_name thy;
    9.76 +    val big_rec_name = big_name ^ "_rep_set";
    9.77 +    val rep_set_names' =
    9.78 +      (if length descr' = 1 then [big_rec_name] else
    9.79 +        (map ((curry (op ^) (big_rec_name ^ "_")) o string_of_int)
    9.80 +          (1 upto (length descr'))));
    9.81 +    val rep_set_names = map (Sign.full_bname thy1) rep_set_names';
    9.82 +
    9.83 +    val tyvars = map (fn (_, (_, Ts, _)) => map dest_DtTFree Ts) (hd descr);
    9.84 +    val leafTs' = get_nonrec_types descr' sorts;
    9.85 +    val branchTs = get_branching_types descr' sorts;
    9.86 +    val branchT = if null branchTs then HOLogic.unitT
    9.87 +      else Balanced_Tree.make (fn (T, U) => Type ("+", [T, U])) branchTs;
    9.88 +    val arities = remove (op =) 0 (get_arities descr');
    9.89 +    val unneeded_vars =
    9.90 +      subtract (op =) (List.foldr OldTerm.add_typ_tfree_names [] (leafTs' @ branchTs)) (hd tyvars);
    9.91 +    val leafTs = leafTs' @ map (fn n => TFree (n, (the o AList.lookup (op =) sorts) n)) unneeded_vars;
    9.92 +    val recTs = get_rec_types descr' sorts;
    9.93 +    val (newTs, oldTs) = chop (length (hd descr)) recTs;
    9.94 +    val sumT = if null leafTs then HOLogic.unitT
    9.95 +      else Balanced_Tree.make (fn (T, U) => Type ("+", [T, U])) leafTs;
    9.96 +    val Univ_elT = HOLogic.mk_setT (Type (node_name, [sumT, branchT]));
    9.97 +    val UnivT = HOLogic.mk_setT Univ_elT;
    9.98 +    val UnivT' = Univ_elT --> HOLogic.boolT;
    9.99 +    val Collect = Const (@{const_name Collect}, UnivT' --> UnivT);
   9.100 +
   9.101 +    val In0 = Const (In0_name, Univ_elT --> Univ_elT);
   9.102 +    val In1 = Const (In1_name, Univ_elT --> Univ_elT);
   9.103 +    val Leaf = Const (Leaf_name, sumT --> Univ_elT);
   9.104 +    val Lim = Const (Lim_name, (branchT --> Univ_elT) --> Univ_elT);
   9.105 +
   9.106 +    (* make injections needed for embedding types in leaves *)
   9.107 +
   9.108 +    fun mk_inj T' x =
   9.109 +      let
   9.110 +        fun mk_inj' T n i =
   9.111 +          if n = 1 then x else
   9.112 +          let val n2 = n div 2;
   9.113 +              val Type (_, [T1, T2]) = T
   9.114 +          in
   9.115 +            if i <= n2 then
   9.116 +              Const (@{const_name "Sum_Type.Inl"}, T1 --> T) $ (mk_inj' T1 n2 i)
   9.117 +            else
   9.118 +              Const (@{const_name "Sum_Type.Inr"}, T2 --> T) $ (mk_inj' T2 (n - n2) (i - n2))
   9.119 +          end
   9.120 +      in mk_inj' sumT (length leafTs) (1 + find_index (fn T'' => T'' = T') leafTs)
   9.121 +      end;
   9.122 +
   9.123 +    (* make injections for constructors *)
   9.124 +
   9.125 +    fun mk_univ_inj ts = Balanced_Tree.access
   9.126 +      {left = fn t => In0 $ t,
   9.127 +        right = fn t => In1 $ t,
   9.128 +        init =
   9.129 +          if ts = [] then Const (@{const_name undefined}, Univ_elT)
   9.130 +          else foldr1 (HOLogic.mk_binop Scons_name) ts};
   9.131 +
   9.132 +    (* function spaces *)
   9.133 +
   9.134 +    fun mk_fun_inj T' x =
   9.135 +      let
   9.136 +        fun mk_inj T n i =
   9.137 +          if n = 1 then x else
   9.138 +          let
   9.139 +            val n2 = n div 2;
   9.140 +            val Type (_, [T1, T2]) = T;
   9.141 +            fun mkT U = (U --> Univ_elT) --> T --> Univ_elT
   9.142 +          in
   9.143 +            if i <= n2 then Const (Suml_name, mkT T1) $ mk_inj T1 n2 i
   9.144 +            else Const (Sumr_name, mkT T2) $ mk_inj T2 (n - n2) (i - n2)
   9.145 +          end
   9.146 +      in mk_inj branchT (length branchTs) (1 + find_index (fn T'' => T'' = T') branchTs)
   9.147 +      end;
   9.148 +
   9.149 +    fun mk_lim t Ts = fold_rev (fn T => fn t => Lim $ mk_fun_inj T (Abs ("x", T, t))) Ts t;
   9.150 +
   9.151 +    (************** generate introduction rules for representing set **********)
   9.152 +
   9.153 +    val _ = message config "Constructing representing sets ...";
   9.154 +
   9.155 +    (* make introduction rule for a single constructor *)
   9.156 +
   9.157 +    fun make_intr s n (i, (_, cargs)) =
   9.158 +      let
   9.159 +        fun mk_prem dt (j, prems, ts) =
   9.160 +          (case strip_dtyp dt of
   9.161 +            (dts, DtRec k) =>
   9.162 +              let
   9.163 +                val Ts = map (typ_of_dtyp descr' sorts) dts;
   9.164 +                val free_t =
   9.165 +                  app_bnds (mk_Free "x" (Ts ---> Univ_elT) j) (length Ts)
   9.166 +              in (j + 1, list_all (map (pair "x") Ts,
   9.167 +                  HOLogic.mk_Trueprop
   9.168 +                    (Free (nth rep_set_names' k, UnivT') $ free_t)) :: prems,
   9.169 +                mk_lim free_t Ts :: ts)
   9.170 +              end
   9.171 +          | _ =>
   9.172 +              let val T = typ_of_dtyp descr' sorts dt
   9.173 +              in (j + 1, prems, (Leaf $ mk_inj T (mk_Free "x" T j))::ts)
   9.174 +              end);
   9.175 +
   9.176 +        val (_, prems, ts) = fold_rev mk_prem cargs (1, [], []);
   9.177 +        val concl = HOLogic.mk_Trueprop
   9.178 +          (Free (s, UnivT') $ mk_univ_inj ts n i)
   9.179 +      in Logic.list_implies (prems, concl)
   9.180 +      end;
   9.181 +
   9.182 +    val intr_ts = maps (fn ((_, (_, _, constrs)), rep_set_name) =>
   9.183 +      map (make_intr rep_set_name (length constrs))
   9.184 +        ((1 upto (length constrs)) ~~ constrs)) (descr' ~~ rep_set_names');
   9.185 +
   9.186 +    val ({raw_induct = rep_induct, intrs = rep_intrs, ...}, thy2) =
   9.187 +      thy1
   9.188 +      |> Sign.map_naming Name_Space.conceal
   9.189 +      |> Inductive.add_inductive_global
   9.190 +          {quiet_mode = #quiet config, verbose = false, alt_name = Binding.name big_rec_name,
   9.191 +           coind = false, no_elim = true, no_ind = false, skip_mono = true, fork_mono = false}
   9.192 +          (map (fn s => ((Binding.name s, UnivT'), NoSyn)) rep_set_names') []
   9.193 +          (map (fn x => (Attrib.empty_binding, x)) intr_ts) []
   9.194 +      ||> Sign.restore_naming thy1
   9.195 +      ||> Theory.checkpoint;
   9.196 +
   9.197 +    (********************************* typedef ********************************)
   9.198 +
   9.199 +    val (typedefs, thy3) = thy2 |>
   9.200 +      Sign.parent_path |>
   9.201 +      fold_map (fn ((((name, mx), tvs), c), name') =>
   9.202 +          Typedef.add_typedef false (SOME (Binding.name name')) (name, tvs, mx)
   9.203 +            (Collect $ Const (c, UnivT')) NONE
   9.204 +            (rtac exI 1 THEN rtac CollectI 1 THEN
   9.205 +              QUIET_BREADTH_FIRST (has_fewer_prems 1)
   9.206 +              (resolve_tac rep_intrs 1)))
   9.207 +                (types_syntax ~~ tyvars ~~
   9.208 +                  (take (length newTs) rep_set_names) ~~ new_type_names) ||>
   9.209 +      Sign.add_path big_name;
   9.210 +
   9.211 +    (*********************** definition of constructors ***********************)
   9.212 +
   9.213 +    val big_rep_name = (space_implode "_" new_type_names) ^ "_Rep_";
   9.214 +    val rep_names = map (curry op ^ "Rep_") new_type_names;
   9.215 +    val rep_names' = map (fn i => big_rep_name ^ (string_of_int i))
   9.216 +      (1 upto (length (flat (tl descr))));
   9.217 +    val all_rep_names = map (Sign.intern_const thy3) rep_names @
   9.218 +      map (Sign.full_bname thy3) rep_names';
   9.219 +
   9.220 +    (* isomorphism declarations *)
   9.221 +
   9.222 +    val iso_decls = map (fn (T, s) => (Binding.name s, T --> Univ_elT, NoSyn))
   9.223 +      (oldTs ~~ rep_names');
   9.224 +
   9.225 +    (* constructor definitions *)
   9.226 +
   9.227 +    fun make_constr_def tname T n ((cname, cargs), (cname', mx)) (thy, defs, eqns, i) =
   9.228 +      let
   9.229 +        fun constr_arg dt (j, l_args, r_args) =
   9.230 +          let val T = typ_of_dtyp descr' sorts dt;
   9.231 +              val free_t = mk_Free "x" T j
   9.232 +          in (case (strip_dtyp dt, strip_type T) of
   9.233 +              ((_, DtRec m), (Us, U)) => (j + 1, free_t :: l_args, mk_lim
   9.234 +                (Const (nth all_rep_names m, U --> Univ_elT) $
   9.235 +                   app_bnds free_t (length Us)) Us :: r_args)
   9.236 +            | _ => (j + 1, free_t::l_args, (Leaf $ mk_inj T free_t)::r_args))
   9.237 +          end;
   9.238 +
   9.239 +        val (_, l_args, r_args) = fold_rev constr_arg cargs (1, [], []);
   9.240 +        val constrT = (map (typ_of_dtyp descr' sorts) cargs) ---> T;
   9.241 +        val abs_name = Sign.intern_const thy ("Abs_" ^ tname);
   9.242 +        val rep_name = Sign.intern_const thy ("Rep_" ^ tname);
   9.243 +        val lhs = list_comb (Const (cname, constrT), l_args);
   9.244 +        val rhs = mk_univ_inj r_args n i;
   9.245 +        val def = Logic.mk_equals (lhs, Const (abs_name, Univ_elT --> T) $ rhs);
   9.246 +        val def_name = Long_Name.base_name cname ^ "_def";
   9.247 +        val eqn = HOLogic.mk_Trueprop (HOLogic.mk_eq
   9.248 +          (Const (rep_name, T --> Univ_elT) $ lhs, rhs));
   9.249 +        val ([def_thm], thy') =
   9.250 +          thy
   9.251 +          |> Sign.add_consts_i [(cname', constrT, mx)]
   9.252 +          |> (PureThy.add_defs false o map Thm.no_attributes) [(Binding.name def_name, def)];
   9.253 +
   9.254 +      in (thy', defs @ [def_thm], eqns @ [eqn], i + 1) end;
   9.255 +
   9.256 +    (* constructor definitions for datatype *)
   9.257 +
   9.258 +    fun dt_constr_defs ((((_, (_, _, constrs)), tname), T), constr_syntax)
   9.259 +        (thy, defs, eqns, rep_congs, dist_lemmas) =
   9.260 +      let
   9.261 +        val _ $ (_ $ (cong_f $ _) $ _) = concl_of arg_cong;
   9.262 +        val rep_const = cterm_of thy
   9.263 +          (Const (Sign.intern_const thy ("Rep_" ^ tname), T --> Univ_elT));
   9.264 +        val cong' =
   9.265 +          Drule.standard (cterm_instantiate [(cterm_of thy cong_f, rep_const)] arg_cong);
   9.266 +        val dist =
   9.267 +          Drule.standard (cterm_instantiate [(cterm_of thy distinct_f, rep_const)] distinct_lemma);
   9.268 +        val (thy', defs', eqns', _) = fold ((make_constr_def tname T) (length constrs))
   9.269 +          (constrs ~~ constr_syntax) (Sign.add_path tname thy, defs, [], 1);
   9.270 +      in
   9.271 +        (Sign.parent_path thy', defs', eqns @ [eqns'],
   9.272 +          rep_congs @ [cong'], dist_lemmas @ [dist])
   9.273 +      end;
   9.274 +
   9.275 +    val (thy4, constr_defs, constr_rep_eqns, rep_congs, dist_lemmas) =
   9.276 +      fold dt_constr_defs
   9.277 +        (hd descr ~~ new_type_names ~~ newTs ~~ constr_syntax)
   9.278 +        (thy3 |> Sign.add_consts_i iso_decls |> Sign.parent_path, [], [], [], []);
   9.279 +
   9.280 +
   9.281 +    (*********** isomorphisms for new types (introduced by typedef) ***********)
   9.282 +
   9.283 +    val _ = message config "Proving isomorphism properties ...";
   9.284 +
   9.285 +    val newT_iso_axms = map (fn (_, td) =>
   9.286 +      (collect_simp (#Abs_inverse td), #Rep_inverse td,
   9.287 +       collect_simp (#Rep td))) typedefs;
   9.288 +
   9.289 +    val newT_iso_inj_thms = map (fn (_, td) =>
   9.290 +      (collect_simp (#Abs_inject td) RS iffD1, #Rep_inject td RS iffD1)) typedefs;
   9.291 +
   9.292 +    (********* isomorphisms between existing types and "unfolded" types *******)
   9.293 +
   9.294 +    (*---------------------------------------------------------------------*)
   9.295 +    (* isomorphisms are defined using primrec-combinators:                 *)
   9.296 +    (* generate appropriate functions for instantiating primrec-combinator *)
   9.297 +    (*                                                                     *)
   9.298 +    (*   e.g.  dt_Rep_i = list_rec ... (%h t y. In1 (Scons (Leaf h) y))    *)
   9.299 +    (*                                                                     *)
   9.300 +    (* also generate characteristic equations for isomorphisms             *)
   9.301 +    (*                                                                     *)
   9.302 +    (*   e.g.  dt_Rep_i (cons h t) = In1 (Scons (dt_Rep_j h) (dt_Rep_i t)) *)
   9.303 +    (*---------------------------------------------------------------------*)
   9.304 +
   9.305 +    fun make_iso_def k ks n (cname, cargs) (fs, eqns, i) =
   9.306 +      let
   9.307 +        val argTs = map (typ_of_dtyp descr' sorts) cargs;
   9.308 +        val T = nth recTs k;
   9.309 +        val rep_name = nth all_rep_names k;
   9.310 +        val rep_const = Const (rep_name, T --> Univ_elT);
   9.311 +        val constr = Const (cname, argTs ---> T);
   9.312 +
   9.313 +        fun process_arg ks' dt (i2, i2', ts, Ts) =
   9.314 +          let
   9.315 +            val T' = typ_of_dtyp descr' sorts dt;
   9.316 +            val (Us, U) = strip_type T'
   9.317 +          in (case strip_dtyp dt of
   9.318 +              (_, DtRec j) => if j mem ks' then
   9.319 +                  (i2 + 1, i2' + 1, ts @ [mk_lim (app_bnds
   9.320 +                     (mk_Free "y" (Us ---> Univ_elT) i2') (length Us)) Us],
   9.321 +                   Ts @ [Us ---> Univ_elT])
   9.322 +                else
   9.323 +                  (i2 + 1, i2', ts @ [mk_lim
   9.324 +                     (Const (nth all_rep_names j, U --> Univ_elT) $
   9.325 +                        app_bnds (mk_Free "x" T' i2) (length Us)) Us], Ts)
   9.326 +            | _ => (i2 + 1, i2', ts @ [Leaf $ mk_inj T' (mk_Free "x" T' i2)], Ts))
   9.327 +          end;
   9.328 +
   9.329 +        val (i2, i2', ts, Ts) = fold (process_arg ks) cargs (1, 1, [], []);
   9.330 +        val xs = map (uncurry (mk_Free "x")) (argTs ~~ (1 upto (i2 - 1)));
   9.331 +        val ys = map (uncurry (mk_Free "y")) (Ts ~~ (1 upto (i2' - 1)));
   9.332 +        val f = list_abs_free (map dest_Free (xs @ ys), mk_univ_inj ts n i);
   9.333 +
   9.334 +        val (_, _, ts', _) = fold (process_arg []) cargs (1, 1, [], []);
   9.335 +        val eqn = HOLogic.mk_Trueprop (HOLogic.mk_eq
   9.336 +          (rep_const $ list_comb (constr, xs), mk_univ_inj ts' n i))
   9.337 +
   9.338 +      in (fs @ [f], eqns @ [eqn], i + 1) end;
   9.339 +
   9.340 +    (* define isomorphisms for all mutually recursive datatypes in list ds *)
   9.341 +
   9.342 +    fun make_iso_defs ds (thy, char_thms) =
   9.343 +      let
   9.344 +        val ks = map fst ds;
   9.345 +        val (_, (tname, _, _)) = hd ds;
   9.346 +        val {rec_rewrites, rec_names, ...} = the (Symtab.lookup dt_info tname);
   9.347 +
   9.348 +        fun process_dt (k, (tname, _, constrs)) (fs, eqns, isos) =
   9.349 +          let
   9.350 +            val (fs', eqns', _) =
   9.351 +              fold (make_iso_def k ks (length constrs)) constrs (fs, eqns, 1);
   9.352 +            val iso = (nth recTs k, nth all_rep_names k)
   9.353 +          in (fs', eqns', isos @ [iso]) end;
   9.354 +        
   9.355 +        val (fs, eqns, isos) = fold process_dt ds ([], [], []);
   9.356 +        val fTs = map fastype_of fs;
   9.357 +        val defs = map (fn (rec_name, (T, iso_name)) => (Binding.name (Long_Name.base_name iso_name ^ "_def"),
   9.358 +          Logic.mk_equals (Const (iso_name, T --> Univ_elT),
   9.359 +            list_comb (Const (rec_name, fTs @ [T] ---> Univ_elT), fs)))) (rec_names ~~ isos);
   9.360 +        val (def_thms, thy') =
   9.361 +          apsnd Theory.checkpoint ((PureThy.add_defs false o map Thm.no_attributes) defs thy);
   9.362 +
   9.363 +        (* prove characteristic equations *)
   9.364 +
   9.365 +        val rewrites = def_thms @ (map mk_meta_eq rec_rewrites);
   9.366 +        val char_thms' = map (fn eqn => Skip_Proof.prove_global thy' [] [] eqn
   9.367 +          (fn _ => EVERY [rewrite_goals_tac rewrites, rtac refl 1])) eqns;
   9.368 +
   9.369 +      in (thy', char_thms' @ char_thms) end;
   9.370 +
   9.371 +    val (thy5, iso_char_thms) = apfst Theory.checkpoint (fold_rev make_iso_defs
   9.372 +        (tl descr) (Sign.add_path big_name thy4, []));
   9.373 +
   9.374 +    (* prove isomorphism properties *)
   9.375 +
   9.376 +    fun mk_funs_inv thy thm =
   9.377 +      let
   9.378 +        val prop = Thm.prop_of thm;
   9.379 +        val _ $ (_ $ ((S as Const (_, Type (_, [U, _]))) $ _ )) $
   9.380 +          (_ $ (_ $ (r $ (a $ _)) $ _)) = Type.legacy_freeze prop;
   9.381 +        val used = OldTerm.add_term_tfree_names (a, []);
   9.382 +
   9.383 +        fun mk_thm i =
   9.384 +          let
   9.385 +            val Ts = map (TFree o rpair HOLogic.typeS)
   9.386 +              (Name.variant_list used (replicate i "'t"));
   9.387 +            val f = Free ("f", Ts ---> U)
   9.388 +          in Skip_Proof.prove_global thy [] [] (Logic.mk_implies
   9.389 +            (HOLogic.mk_Trueprop (HOLogic.list_all
   9.390 +               (map (pair "x") Ts, S $ app_bnds f i)),
   9.391 +             HOLogic.mk_Trueprop (HOLogic.mk_eq (list_abs (map (pair "x") Ts,
   9.392 +               r $ (a $ app_bnds f i)), f))))
   9.393 +            (fn _ => EVERY [REPEAT_DETERM_N i (rtac ext 1),
   9.394 +               REPEAT (etac allE 1), rtac thm 1, atac 1])
   9.395 +          end
   9.396 +      in map (fn r => r RS subst) (thm :: map mk_thm arities) end;
   9.397 +
   9.398 +    (* prove  inj dt_Rep_i  and  dt_Rep_i x : dt_rep_set_i *)
   9.399 +
   9.400 +    val fun_congs = map (fn T => make_elim (Drule.instantiate'
   9.401 +      [SOME (ctyp_of thy5 T)] [] fun_cong)) branchTs;
   9.402 +
   9.403 +    fun prove_iso_thms ds (inj_thms, elem_thms) =
   9.404 +      let
   9.405 +        val (_, (tname, _, _)) = hd ds;
   9.406 +        val induct = (#induct o the o Symtab.lookup dt_info) tname;
   9.407 +
   9.408 +        fun mk_ind_concl (i, _) =
   9.409 +          let
   9.410 +            val T = nth recTs i;
   9.411 +            val Rep_t = Const (nth all_rep_names i, T --> Univ_elT);
   9.412 +            val rep_set_name = nth rep_set_names i
   9.413 +          in (HOLogic.all_const T $ Abs ("y", T, HOLogic.imp $
   9.414 +                HOLogic.mk_eq (Rep_t $ mk_Free "x" T i, Rep_t $ Bound 0) $
   9.415 +                  HOLogic.mk_eq (mk_Free "x" T i, Bound 0)),
   9.416 +              Const (rep_set_name, UnivT') $ (Rep_t $ mk_Free "x" T i))
   9.417 +          end;
   9.418 +
   9.419 +        val (ind_concl1, ind_concl2) = ListPair.unzip (map mk_ind_concl ds);
   9.420 +
   9.421 +        val rewrites = map mk_meta_eq iso_char_thms;
   9.422 +        val inj_thms' = map snd newT_iso_inj_thms @
   9.423 +          map (fn r => r RS @{thm injD}) inj_thms;
   9.424 +
   9.425 +        val inj_thm = Skip_Proof.prove_global thy5 [] []
   9.426 +          (HOLogic.mk_Trueprop (mk_conj ind_concl1)) (fn _ => EVERY
   9.427 +            [(indtac induct [] THEN_ALL_NEW ObjectLogic.atomize_prems_tac) 1,
   9.428 +             REPEAT (EVERY
   9.429 +               [rtac allI 1, rtac impI 1,
   9.430 +                exh_tac (exh_thm_of dt_info) 1,
   9.431 +                REPEAT (EVERY
   9.432 +                  [hyp_subst_tac 1,
   9.433 +                   rewrite_goals_tac rewrites,
   9.434 +                   REPEAT (dresolve_tac [In0_inject, In1_inject] 1),
   9.435 +                   (eresolve_tac [In0_not_In1 RS notE, In1_not_In0 RS notE] 1)
   9.436 +                   ORELSE (EVERY
   9.437 +                     [REPEAT (eresolve_tac (Scons_inject ::
   9.438 +                        map make_elim [Leaf_inject, Inl_inject, Inr_inject]) 1),
   9.439 +                      REPEAT (cong_tac 1), rtac refl 1,
   9.440 +                      REPEAT (atac 1 ORELSE (EVERY
   9.441 +                        [REPEAT (rtac ext 1),
   9.442 +                         REPEAT (eresolve_tac (mp :: allE ::
   9.443 +                           map make_elim (Suml_inject :: Sumr_inject ::
   9.444 +                             Lim_inject :: inj_thms') @ fun_congs) 1),
   9.445 +                         atac 1]))])])])]);
   9.446 +
   9.447 +        val inj_thms'' = map (fn r => r RS @{thm datatype_injI})
   9.448 +                             (split_conj_thm inj_thm);
   9.449 +
   9.450 +        val elem_thm = 
   9.451 +            Skip_Proof.prove_global thy5 [] [] (HOLogic.mk_Trueprop (mk_conj ind_concl2))
   9.452 +              (fn _ =>
   9.453 +               EVERY [(indtac induct [] THEN_ALL_NEW ObjectLogic.atomize_prems_tac) 1,
   9.454 +                rewrite_goals_tac rewrites,
   9.455 +                REPEAT ((resolve_tac rep_intrs THEN_ALL_NEW
   9.456 +                  ((REPEAT o etac allE) THEN' ares_tac elem_thms)) 1)]);
   9.457 +
   9.458 +      in (inj_thms'' @ inj_thms, elem_thms @ (split_conj_thm elem_thm))
   9.459 +      end;
   9.460 +
   9.461 +    val (iso_inj_thms_unfolded, iso_elem_thms) =
   9.462 +      fold_rev prove_iso_thms (tl descr) ([], map #3 newT_iso_axms);
   9.463 +    val iso_inj_thms = map snd newT_iso_inj_thms @
   9.464 +      map (fn r => r RS @{thm injD}) iso_inj_thms_unfolded;
   9.465 +
   9.466 +    (* prove  dt_rep_set_i x --> x : range dt_Rep_i *)
   9.467 +
   9.468 +    fun mk_iso_t (((set_name, iso_name), i), T) =
   9.469 +      let val isoT = T --> Univ_elT
   9.470 +      in HOLogic.imp $ 
   9.471 +        (Const (set_name, UnivT') $ mk_Free "x" Univ_elT i) $
   9.472 +          (if i < length newTs then HOLogic.true_const
   9.473 +           else HOLogic.mk_mem (mk_Free "x" Univ_elT i,
   9.474 +             Const (@{const_name image}, isoT --> HOLogic.mk_setT T --> UnivT) $
   9.475 +               Const (iso_name, isoT) $ Const (@{const_name UNIV}, HOLogic.mk_setT T)))
   9.476 +      end;
   9.477 +
   9.478 +    val iso_t = HOLogic.mk_Trueprop (mk_conj (map mk_iso_t
   9.479 +      (rep_set_names ~~ all_rep_names ~~ (0 upto (length descr' - 1)) ~~ recTs)));
   9.480 +
   9.481 +    (* all the theorems are proved by one single simultaneous induction *)
   9.482 +
   9.483 +    val range_eqs = map (fn r => mk_meta_eq (r RS @{thm range_ex1_eq}))
   9.484 +      iso_inj_thms_unfolded;
   9.485 +
   9.486 +    val iso_thms = if length descr = 1 then [] else
   9.487 +      drop (length newTs) (split_conj_thm
   9.488 +        (Skip_Proof.prove_global thy5 [] [] iso_t (fn _ => EVERY
   9.489 +           [(indtac rep_induct [] THEN_ALL_NEW ObjectLogic.atomize_prems_tac) 1,
   9.490 +            REPEAT (rtac TrueI 1),
   9.491 +            rewrite_goals_tac (mk_meta_eq choice_eq ::
   9.492 +              symmetric (mk_meta_eq @{thm expand_fun_eq}) :: range_eqs),
   9.493 +            rewrite_goals_tac (map symmetric range_eqs),
   9.494 +            REPEAT (EVERY
   9.495 +              [REPEAT (eresolve_tac ([rangeE, ex1_implies_ex RS exE] @
   9.496 +                 maps (mk_funs_inv thy5 o #1) newT_iso_axms) 1),
   9.497 +               TRY (hyp_subst_tac 1),
   9.498 +               rtac (sym RS range_eqI) 1,
   9.499 +               resolve_tac iso_char_thms 1])])));
   9.500 +
   9.501 +    val Abs_inverse_thms' =
   9.502 +      map #1 newT_iso_axms @
   9.503 +      map2 (fn r_inj => fn r => @{thm f_the_inv_into_f} OF [r_inj, r RS mp])
   9.504 +        iso_inj_thms_unfolded iso_thms;
   9.505 +
   9.506 +    val Abs_inverse_thms = maps (mk_funs_inv thy5) Abs_inverse_thms';
   9.507 +
   9.508 +    (******************* freeness theorems for constructors *******************)
   9.509 +
   9.510 +    val _ = message config "Proving freeness of constructors ...";
   9.511 +
   9.512 +    (* prove theorem  Rep_i (Constr_j ...) = Inj_j ...  *)
   9.513 +    
   9.514 +    fun prove_constr_rep_thm eqn =
   9.515 +      let
   9.516 +        val inj_thms = map fst newT_iso_inj_thms;
   9.517 +        val rewrites = @{thm o_def} :: constr_defs @ (map (mk_meta_eq o #2) newT_iso_axms)
   9.518 +      in Skip_Proof.prove_global thy5 [] [] eqn (fn _ => EVERY
   9.519 +        [resolve_tac inj_thms 1,
   9.520 +         rewrite_goals_tac rewrites,
   9.521 +         rtac refl 3,
   9.522 +         resolve_tac rep_intrs 2,
   9.523 +         REPEAT (resolve_tac iso_elem_thms 1)])
   9.524 +      end;
   9.525 +
   9.526 +    (*--------------------------------------------------------------*)
   9.527 +    (* constr_rep_thms and rep_congs are used to prove distinctness *)
   9.528 +    (* of constructors.                                             *)
   9.529 +    (*--------------------------------------------------------------*)
   9.530 +
   9.531 +    val constr_rep_thms = map (map prove_constr_rep_thm) constr_rep_eqns;
   9.532 +
   9.533 +    val dist_rewrites = map (fn (rep_thms, dist_lemma) =>
   9.534 +      dist_lemma::(rep_thms @ [In0_eq, In1_eq, In0_not_In1, In1_not_In0]))
   9.535 +        (constr_rep_thms ~~ dist_lemmas);
   9.536 +
   9.537 +    fun prove_distinct_thms dist_rewrites' (k, ts) =
   9.538 +      let
   9.539 +        fun prove [] = []
   9.540 +          | prove (t :: ts) =
   9.541 +              let
   9.542 +                val dist_thm = Skip_Proof.prove_global thy5 [] [] t (fn _ =>
   9.543 +                  EVERY [simp_tac (HOL_ss addsimps dist_rewrites') 1])
   9.544 +              in dist_thm :: Drule.standard (dist_thm RS not_sym) :: prove ts end;
   9.545 +      in prove ts end;
   9.546 +
   9.547 +    val distinct_thms = map2 (prove_distinct_thms)
   9.548 +      dist_rewrites (Datatype_Prop.make_distincts descr sorts);
   9.549 +
   9.550 +    (* prove injectivity of constructors *)
   9.551 +
   9.552 +    fun prove_constr_inj_thm rep_thms t =
   9.553 +      let val inj_thms = Scons_inject :: (map make_elim
   9.554 +        (iso_inj_thms @
   9.555 +          [In0_inject, In1_inject, Leaf_inject, Inl_inject, Inr_inject,
   9.556 +           Lim_inject, Suml_inject, Sumr_inject]))
   9.557 +      in Skip_Proof.prove_global thy5 [] [] t (fn _ => EVERY
   9.558 +        [rtac iffI 1,
   9.559 +         REPEAT (etac conjE 2), hyp_subst_tac 2, rtac refl 2,
   9.560 +         dresolve_tac rep_congs 1, dtac box_equals 1,
   9.561 +         REPEAT (resolve_tac rep_thms 1),
   9.562 +         REPEAT (eresolve_tac inj_thms 1),
   9.563 +         REPEAT (ares_tac [conjI] 1 ORELSE (EVERY [REPEAT (rtac ext 1),
   9.564 +           REPEAT (eresolve_tac (make_elim fun_cong :: inj_thms) 1),
   9.565 +           atac 1]))])
   9.566 +      end;
   9.567 +
   9.568 +    val constr_inject = map (fn (ts, thms) => map (prove_constr_inj_thm thms) ts)
   9.569 +      ((Datatype_Prop.make_injs descr sorts) ~~ constr_rep_thms);
   9.570 +
   9.571 +    val ((constr_inject', distinct_thms'), thy6) =
   9.572 +      thy5
   9.573 +      |> Sign.parent_path
   9.574 +      |> store_thmss "inject" new_type_names constr_inject
   9.575 +      ||>> store_thmss "distinct" new_type_names distinct_thms;
   9.576 +
   9.577 +    (*************************** induction theorem ****************************)
   9.578 +
   9.579 +    val _ = message config "Proving induction rule for datatypes ...";
   9.580 +
   9.581 +    val Rep_inverse_thms = (map (fn (_, iso, _) => iso RS subst) newT_iso_axms) @
   9.582 +      (map (fn r => r RS @{thm the_inv_f_f} RS subst) iso_inj_thms_unfolded);
   9.583 +    val Rep_inverse_thms' = map (fn r => r RS @{thm the_inv_f_f}) iso_inj_thms_unfolded;
   9.584 +
   9.585 +    fun mk_indrule_lemma ((i, _), T) (prems, concls) =
   9.586 +      let
   9.587 +        val Rep_t = Const (nth all_rep_names i, T --> Univ_elT) $
   9.588 +          mk_Free "x" T i;
   9.589 +
   9.590 +        val Abs_t = if i < length newTs then
   9.591 +            Const (Sign.intern_const thy6
   9.592 +              ("Abs_" ^ (nth new_type_names i)), Univ_elT --> T)
   9.593 +          else Const (@{const_name the_inv_into},
   9.594 +              [HOLogic.mk_setT T, T --> Univ_elT, Univ_elT] ---> T) $
   9.595 +            HOLogic.mk_UNIV T $ Const (nth all_rep_names i, T --> Univ_elT)
   9.596 +
   9.597 +      in (prems @ [HOLogic.imp $
   9.598 +            (Const (nth rep_set_names i, UnivT') $ Rep_t) $
   9.599 +              (mk_Free "P" (T --> HOLogic.boolT) (i + 1) $ (Abs_t $ Rep_t))],
   9.600 +          concls @ [mk_Free "P" (T --> HOLogic.boolT) (i + 1) $ mk_Free "x" T i])
   9.601 +      end;
   9.602 +
   9.603 +    val (indrule_lemma_prems, indrule_lemma_concls) =
   9.604 +      fold mk_indrule_lemma (descr' ~~ recTs) ([], []);
   9.605 +
   9.606 +    val cert = cterm_of thy6;
   9.607 +
   9.608 +    val indrule_lemma = Skip_Proof.prove_global thy6 [] []
   9.609 +      (Logic.mk_implies
   9.610 +        (HOLogic.mk_Trueprop (mk_conj indrule_lemma_prems),
   9.611 +         HOLogic.mk_Trueprop (mk_conj indrule_lemma_concls))) (fn _ => EVERY
   9.612 +           [REPEAT (etac conjE 1),
   9.613 +            REPEAT (EVERY
   9.614 +              [TRY (rtac conjI 1), resolve_tac Rep_inverse_thms 1,
   9.615 +               etac mp 1, resolve_tac iso_elem_thms 1])]);
   9.616 +
   9.617 +    val Ps = map head_of (HOLogic.dest_conj (HOLogic.dest_Trueprop (concl_of indrule_lemma)));
   9.618 +    val frees = if length Ps = 1 then [Free ("P", snd (dest_Var (hd Ps)))] else
   9.619 +      map (Free o apfst fst o dest_Var) Ps;
   9.620 +    val indrule_lemma' = cterm_instantiate (map cert Ps ~~ map cert frees) indrule_lemma;
   9.621 +
   9.622 +    val dt_induct_prop = Datatype_Prop.make_ind descr sorts;
   9.623 +    val dt_induct = Skip_Proof.prove_global thy6 []
   9.624 +      (Logic.strip_imp_prems dt_induct_prop) (Logic.strip_imp_concl dt_induct_prop)
   9.625 +      (fn {prems, ...} => EVERY
   9.626 +        [rtac indrule_lemma' 1,
   9.627 +         (indtac rep_induct [] THEN_ALL_NEW ObjectLogic.atomize_prems_tac) 1,
   9.628 +         EVERY (map (fn (prem, r) => (EVERY
   9.629 +           [REPEAT (eresolve_tac Abs_inverse_thms 1),
   9.630 +            simp_tac (HOL_basic_ss addsimps ((symmetric r)::Rep_inverse_thms')) 1,
   9.631 +            DEPTH_SOLVE_1 (ares_tac [prem] 1 ORELSE etac allE 1)]))
   9.632 +                (prems ~~ (constr_defs @ (map mk_meta_eq iso_char_thms))))]);
   9.633 +
   9.634 +    val ([dt_induct'], thy7) =
   9.635 +      thy6
   9.636 +      |> Sign.add_path big_name
   9.637 +      |> PureThy.add_thms [((Binding.name "induct", dt_induct), [case_names_induct])]
   9.638 +      ||> Sign.parent_path
   9.639 +      ||> Theory.checkpoint;
   9.640 +
   9.641 +  in
   9.642 +    ((constr_inject', distinct_thms', dt_induct'), thy7)
   9.643 +  end;
   9.644 +
   9.645 +
   9.646 +
   9.647 +(** definitional introduction of datatypes **)
   9.648 +
   9.649 +fun gen_add_datatype prep_typ config new_type_names dts thy =
   9.650 +  let
   9.651 +    val _ = Theory.requires thy "Datatype" "datatype definitions";
   9.652 +
   9.653 +    (* this theory is used just for parsing *)
   9.654 +    val tmp_thy = thy |>
   9.655 +      Theory.copy |>
   9.656 +      Sign.add_types (map (fn (tvs, tname, mx, _) =>
   9.657 +        (tname, length tvs, mx)) dts);
   9.658 +
   9.659 +    val (tyvars, _, _, _)::_ = dts;
   9.660 +    val (new_dts, types_syntax) = ListPair.unzip (map (fn (tvs, tname, mx, _) =>
   9.661 +      let val full_tname = Sign.full_name tmp_thy (Binding.map_name (Syntax.type_name mx) tname)
   9.662 +      in
   9.663 +        (case duplicates (op =) tvs of
   9.664 +          [] =>
   9.665 +            if eq_set (op =) (tyvars, tvs) then ((full_tname, tvs), (tname, mx))
   9.666 +            else error ("Mutually recursive datatypes must have same type parameters")
   9.667 +        | dups => error ("Duplicate parameter(s) for datatype " ^ quote (Binding.str_of tname) ^
   9.668 +            " : " ^ commas dups))
   9.669 +      end) dts);
   9.670 +    val dt_names = map fst new_dts;
   9.671 +
   9.672 +    val _ =
   9.673 +      (case duplicates (op =) (map fst new_dts) @ duplicates (op =) new_type_names of
   9.674 +        [] => ()
   9.675 +      | dups => error ("Duplicate datatypes: " ^ commas dups));
   9.676 +
   9.677 +    fun prep_dt_spec (tvs, tname, mx, constrs) tname' (dts', constr_syntax, sorts, i) =
   9.678 +      let
   9.679 +        fun prep_constr (cname, cargs, mx') (constrs, constr_syntax', sorts') =
   9.680 +          let
   9.681 +            val (cargs', sorts'') = fold_map (prep_typ tmp_thy) cargs sorts';
   9.682 +            val _ =
   9.683 +              (case subtract (op =) tvs (fold (curry OldTerm.add_typ_tfree_names) cargs' []) of
   9.684 +                [] => ()
   9.685 +              | vs => error ("Extra type variables on rhs: " ^ commas vs))
   9.686 +          in (constrs @ [(Sign.full_name_path tmp_thy tname'
   9.687 +                  (Binding.map_name (Syntax.const_name mx') cname),
   9.688 +                   map (dtyp_of_typ new_dts) cargs')],
   9.689 +              constr_syntax' @ [(cname, mx')], sorts'')
   9.690 +          end handle ERROR msg => cat_error msg
   9.691 +           ("The error above occured in constructor " ^ quote (Binding.str_of cname) ^
   9.692 +            " of datatype " ^ quote (Binding.str_of tname));
   9.693 +
   9.694 +        val (constrs', constr_syntax', sorts') =
   9.695 +          fold prep_constr constrs ([], [], sorts)
   9.696 +
   9.697 +      in
   9.698 +        case duplicates (op =) (map fst constrs') of
   9.699 +           [] =>
   9.700 +             (dts' @ [(i, (Sign.full_name tmp_thy (Binding.map_name (Syntax.type_name mx) tname),
   9.701 +                map DtTFree tvs, constrs'))],
   9.702 +              constr_syntax @ [constr_syntax'], sorts', i + 1)
   9.703 +         | dups => error ("Duplicate constructors " ^ commas dups ^
   9.704 +             " in datatype " ^ quote (Binding.str_of tname))
   9.705 +      end;
   9.706 +
   9.707 +    val (dts', constr_syntax, sorts', i) =
   9.708 +      fold2 prep_dt_spec dts new_type_names ([], [], [], 0);
   9.709 +    val sorts = sorts' @ map (rpair (Sign.defaultS tmp_thy)) (subtract (op =) (map fst sorts') tyvars);
   9.710 +    val dt_info = Datatype_Data.get_all thy;
   9.711 +    val (descr, _) = unfold_datatypes tmp_thy dts' sorts dt_info dts' i;
   9.712 +    val _ = check_nonempty descr handle (exn as Datatype_Empty s) =>
   9.713 +      if #strict config then error ("Nonemptiness check failed for datatype " ^ s)
   9.714 +      else raise exn;
   9.715 +
   9.716 +    val _ = message config ("Constructing datatype(s) " ^ commas_quote new_type_names);
   9.717 +
   9.718 +  in
   9.719 +    thy
   9.720 +    |> representation_proofs config dt_info new_type_names descr sorts
   9.721 +        types_syntax constr_syntax (Datatype_Data.mk_case_names_induct (flat descr))
   9.722 +    |-> (fn (inject, distinct, induct) => Datatype_Data.derive_datatype_props
   9.723 +        config dt_names (SOME new_type_names) descr sorts
   9.724 +        induct inject distinct)
   9.725 +  end;
   9.726 +
   9.727 +val add_datatype = gen_add_datatype Datatype_Data.cert_typ;
   9.728 +val datatype_cmd = snd ooo gen_add_datatype Datatype_Data.read_typ default_config;
   9.729 +
   9.730 +local
   9.731 +
   9.732 +structure P = OuterParse and K = OuterKeyword
   9.733 +
   9.734 +fun prep_datatype_decls args =
   9.735 +  let
   9.736 +    val names = map
   9.737 +      (fn ((((NONE, _), t), _), _) => Binding.name_of t | ((((SOME t, _), _), _), _) => t) args;
   9.738 +    val specs = map (fn ((((_, vs), t), mx), cons) =>
   9.739 +      (vs, t, mx, map (fn ((x, y), z) => (x, y, z)) cons)) args;
   9.740 +  in (names, specs) end;
   9.741 +
   9.742 +val parse_datatype_decl =
   9.743 +  (Scan.option (P.$$$ "(" |-- P.name --| P.$$$ ")") -- P.type_args -- P.binding -- P.opt_infix --
   9.744 +    (P.$$$ "=" |-- P.enum1 "|" (P.binding -- Scan.repeat P.typ -- P.opt_mixfix)));
   9.745 +
   9.746 +val parse_datatype_decls = P.and_list1 parse_datatype_decl >> prep_datatype_decls;
   9.747 +
   9.748 +in
   9.749 +
   9.750 +val _ =
   9.751 +  OuterSyntax.command "datatype" "define inductive datatypes" K.thy_decl
   9.752 +    (parse_datatype_decls >> (fn (names, specs) => Toplevel.theory (datatype_cmd names specs)));
   9.753  
   9.754  end;
   9.755 +
   9.756 +end;
    10.1 --- a/src/HOL/Tools/Datatype/datatype_abs_proofs.ML	Mon Nov 30 11:42:48 2009 +0100
    10.2 +++ b/src/HOL/Tools/Datatype/datatype_abs_proofs.ML	Mon Nov 30 11:42:49 2009 +0100
    10.3 @@ -1,15 +1,9 @@
    10.4 -(*  Title:      HOL/Tools/datatype_abs_proofs.ML
    10.5 +(*  Title:      HOL/Tools/Datatype/datatype_abs_proofs.ML
    10.6      Author:     Stefan Berghofer, TU Muenchen
    10.7  
    10.8 -Proofs and defintions independent of concrete representation
    10.9 -of datatypes  (i.e. requiring only abstract properties such as
   10.10 -injectivity / distinctness of constructors and induction)
   10.11 -
   10.12 - - case distinction (exhaustion) theorems
   10.13 - - characteristic equations for primrec combinators
   10.14 - - characteristic equations for case combinators
   10.15 - - equations for splitting "P (case ...)" expressions
   10.16 - - "nchotomy" and "case_cong" theorems for TFL
   10.17 +Datatype package: proofs and defintions independent of concrete
   10.18 +representation of datatypes  (i.e. requiring only abstract
   10.19 +properties: injectivity / distinctness of constructors and induction).
   10.20  *)
   10.21  
   10.22  signature DATATYPE_ABS_PROOFS =
   10.23 @@ -38,10 +32,10 @@
   10.24        thm list -> thm list list -> theory -> thm list * theory
   10.25  end;
   10.26  
   10.27 -structure DatatypeAbsProofs: DATATYPE_ABS_PROOFS =
   10.28 +structure Datatype_Abs_Proofs: DATATYPE_ABS_PROOFS =
   10.29  struct
   10.30  
   10.31 -open DatatypeAux;
   10.32 +open Datatype_Aux;
   10.33  
   10.34  (************************ case distinction theorems ***************************)
   10.35  
   10.36 @@ -78,7 +72,7 @@
   10.37        end;
   10.38  
   10.39      val casedist_thms = map_index prove_casedist_thm
   10.40 -      (newTs ~~ DatatypeProp.make_casedists descr sorts)
   10.41 +      (newTs ~~ Datatype_Prop.make_casedists descr sorts)
   10.42    in
   10.43      thy
   10.44      |> store_thms_atts "exhaust" new_type_names (map single case_names_exhausts) casedist_thms
   10.45 @@ -109,7 +103,7 @@
   10.46            (1 upto (length descr'));
   10.47      val rec_set_names = map (Sign.full_bname thy0) rec_set_names';
   10.48  
   10.49 -    val (rec_result_Ts, reccomb_fn_Ts) = DatatypeProp.make_primrec_Ts descr sorts used;
   10.50 +    val (rec_result_Ts, reccomb_fn_Ts) = Datatype_Prop.make_primrec_Ts descr sorts used;
   10.51  
   10.52      val rec_set_Ts = map (fn (T1, T2) =>
   10.53        reccomb_fn_Ts @ [T1, T2] ---> HOLogic.boolT) (recTs ~~ rec_result_Ts);
   10.54 @@ -260,7 +254,7 @@
   10.55           resolve_tac rec_unique_thms 1,
   10.56           resolve_tac rec_intrs 1,
   10.57           REPEAT (rtac allI 1 ORELSE resolve_tac rec_total_thms 1)]))
   10.58 -           (DatatypeProp.make_primrecs new_type_names descr sorts thy2)
   10.59 +           (Datatype_Prop.make_primrecs new_type_names descr sorts thy2)
   10.60  
   10.61    in
   10.62      thy2
   10.63 @@ -334,7 +328,7 @@
   10.64  
   10.65      val case_thms = map (map (fn t => Skip_Proof.prove_global thy2 [] [] t
   10.66        (fn _ => EVERY [rewrite_goals_tac (case_defs @ map mk_meta_eq primrec_thms), rtac refl 1])))
   10.67 -          (DatatypeProp.make_cases new_type_names descr sorts thy2)
   10.68 +          (Datatype_Prop.make_cases new_type_names descr sorts thy2)
   10.69    in
   10.70      thy2
   10.71      |> Context.theory_map ((fold o fold) Nitpick_Simps.add_thm case_thms)
   10.72 @@ -370,7 +364,7 @@
   10.73        end;
   10.74  
   10.75      val split_thm_pairs = map prove_split_thms
   10.76 -      ((DatatypeProp.make_splits new_type_names descr sorts thy) ~~ constr_inject ~~
   10.77 +      ((Datatype_Prop.make_splits new_type_names descr sorts thy) ~~ constr_inject ~~
   10.78          dist_rewrites ~~ casedist_thms ~~ case_thms ~~ newTs);
   10.79  
   10.80      val (split_thms, split_asm_thms) = ListPair.unzip split_thm_pairs
   10.81 @@ -388,7 +382,7 @@
   10.82         Skip_Proof.prove_global thy [] (Logic.strip_imp_prems t) (Logic.strip_imp_concl t)
   10.83           (fn {prems, ...} => EVERY [rtac ((hd prems) RS arg_cong) 1])
   10.84  
   10.85 -    val weak_case_congs = map prove_weak_case_cong (DatatypeProp.make_weak_case_congs
   10.86 +    val weak_case_congs = map prove_weak_case_cong (Datatype_Prop.make_weak_case_congs
   10.87        new_type_names descr sorts thy)
   10.88  
   10.89    in thy |> store_thms "weak_case_cong" new_type_names weak_case_congs end;
   10.90 @@ -413,7 +407,7 @@
   10.91        end;
   10.92  
   10.93      val nchotomys =
   10.94 -      map prove_nchotomy (DatatypeProp.make_nchotomys descr sorts ~~ casedist_thms)
   10.95 +      map prove_nchotomy (Datatype_Prop.make_nchotomys descr sorts ~~ casedist_thms)
   10.96  
   10.97    in thy |> store_thms "nchotomy" new_type_names nchotomys end;
   10.98  
   10.99 @@ -438,7 +432,7 @@
  10.100              end)
  10.101        end;
  10.102  
  10.103 -    val case_congs = map prove_case_cong (DatatypeProp.make_case_congs
  10.104 +    val case_congs = map prove_case_cong (Datatype_Prop.make_case_congs
  10.105        new_type_names descr sorts thy ~~ nchotomys ~~ case_thms)
  10.106  
  10.107    in thy |> store_thms "case_cong" new_type_names case_congs end;
    11.1 --- a/src/HOL/Tools/Datatype/datatype_aux.ML	Mon Nov 30 11:42:48 2009 +0100
    11.2 +++ b/src/HOL/Tools/Datatype/datatype_aux.ML	Mon Nov 30 11:42:49 2009 +0100
    11.3 @@ -1,7 +1,7 @@
    11.4 -(*  Title:      HOL/Tools/datatype_aux.ML
    11.5 +(*  Title:      HOL/Tools/Datatype/datatype_aux.ML
    11.6      Author:     Stefan Berghofer, TU Muenchen
    11.7  
    11.8 -Auxiliary functions for defining datatypes.
    11.9 +Datatype package: auxiliary data structures and functions.
   11.10  *)
   11.11  
   11.12  signature DATATYPE_COMMON =
   11.13 @@ -79,9 +79,10 @@
   11.14    val unfold_datatypes : 
   11.15      theory -> descr -> (string * sort) list -> info Symtab.table ->
   11.16        descr -> int -> descr list * int
   11.17 +  val find_shortest_path : descr -> int -> (string * int) option
   11.18  end;
   11.19  
   11.20 -structure DatatypeAux : DATATYPE_AUX =
   11.21 +structure Datatype_Aux : DATATYPE_AUX =
   11.22  struct
   11.23  
   11.24  (* datatype option flags *)
   11.25 @@ -365,4 +366,24 @@
   11.26  
   11.27    in (descr' :: descrs, i') end;
   11.28  
   11.29 +(* find shortest path to constructor with no recursive arguments *)
   11.30 +
   11.31 +fun find_nonempty descr is i =
   11.32 +  let
   11.33 +    val (_, _, constrs) = the (AList.lookup (op =) descr i);
   11.34 +    fun arg_nonempty (_, DtRec i) = if member (op =) is i
   11.35 +          then NONE
   11.36 +          else Option.map (Integer.add 1 o snd) (find_nonempty descr (i::is) i)
   11.37 +      | arg_nonempty _ = SOME 0;
   11.38 +    fun max xs = Library.foldl
   11.39 +      (fn (NONE, _) => NONE
   11.40 +        | (SOME i, SOME j) => SOME (Int.max (i, j))
   11.41 +        | (_, NONE) => NONE) (SOME 0, xs);
   11.42 +    val xs = sort (int_ord o pairself snd)
   11.43 +      (map_filter (fn (s, dts) => Option.map (pair s)
   11.44 +        (max (map (arg_nonempty o strip_dtyp) dts))) constrs)
   11.45 +  in case xs of [] => NONE | x :: _ => SOME x end;
   11.46 +
   11.47 +fun find_shortest_path descr i = find_nonempty descr [i] i;
   11.48 +
   11.49  end;
    12.1 --- a/src/HOL/Tools/Datatype/datatype_case.ML	Mon Nov 30 11:42:48 2009 +0100
    12.2 +++ b/src/HOL/Tools/Datatype/datatype_case.ML	Mon Nov 30 11:42:49 2009 +0100
    12.3 @@ -1,30 +1,32 @@
    12.4 -(*  Title:      HOL/Tools/datatype_case.ML
    12.5 +(*  Title:      HOL/Tools/Datatype/datatype_case.ML
    12.6      Author:     Konrad Slind, Cambridge University Computer Laboratory
    12.7      Author:     Stefan Berghofer, TU Muenchen
    12.8  
    12.9 -Nested case expressions on datatypes.
   12.10 +Datatype package: nested case expressions on datatypes.
   12.11  *)
   12.12  
   12.13  signature DATATYPE_CASE =
   12.14  sig
   12.15 -  datatype config = Error | Warning | Quiet;
   12.16 -  val make_case: (string * typ -> DatatypeAux.info option) ->
   12.17 +  datatype config = Error | Warning | Quiet
   12.18 +  type info = Datatype_Aux.info
   12.19 +  val make_case: (string * typ -> info option) ->
   12.20      Proof.context -> config -> string list -> term -> (term * term) list ->
   12.21      term * (term * (int * bool)) list
   12.22 -  val dest_case: (string -> DatatypeAux.info option) -> bool ->
   12.23 +  val dest_case: (string -> info option) -> bool ->
   12.24      string list -> term -> (term * (term * term) list) option
   12.25 -  val strip_case: (string -> DatatypeAux.info option) -> bool ->
   12.26 +  val strip_case: (string -> info option) -> bool ->
   12.27      term -> (term * (term * term) list) option
   12.28 -  val case_tr: bool -> (theory -> string * typ -> DatatypeAux.info option)
   12.29 -    -> Proof.context -> term list -> term
   12.30 -  val case_tr': (theory -> string -> DatatypeAux.info option) ->
   12.31 +  val case_tr: bool -> (theory -> string * typ -> info option) ->
   12.32 +    Proof.context -> term list -> term
   12.33 +  val case_tr': (theory -> string -> info option) ->
   12.34      string -> Proof.context -> term list -> term
   12.35  end;
   12.36  
   12.37 -structure DatatypeCase : DATATYPE_CASE =
   12.38 +structure Datatype_Case : DATATYPE_CASE =
   12.39  struct
   12.40  
   12.41  datatype config = Error | Warning | Quiet;
   12.42 +type info = Datatype_Aux.info;
   12.43  
   12.44  exception CASE_ERROR of string * int;
   12.45  
   12.46 @@ -36,10 +38,10 @@
   12.47  
   12.48  fun ty_info tab sT =
   12.49    case tab sT of
   12.50 -    SOME ({descr, case_name, index, sorts, ...} : DatatypeAux.info) =>
   12.51 +    SOME ({descr, case_name, index, sorts, ...} : info) =>
   12.52        let
   12.53          val (_, (tname, dts, constrs)) = nth descr index;
   12.54 -        val mk_ty = DatatypeAux.typ_of_dtyp descr sorts;
   12.55 +        val mk_ty = Datatype_Aux.typ_of_dtyp descr sorts;
   12.56          val T = Type (tname, map mk_ty dts)
   12.57        in
   12.58          SOME {case_name = case_name,
   12.59 @@ -84,7 +86,7 @@
   12.60      val (_, Ty) = dest_Const c
   12.61      val Ts = binder_types Ty;
   12.62      val names = Name.variant_list used
   12.63 -      (DatatypeProp.make_tnames (map Logic.unvarifyT Ts));
   12.64 +      (Datatype_Prop.make_tnames (map Logic.unvarifyT Ts));
   12.65      val ty = body_type Ty;
   12.66      val ty_theta = ty_match ty colty handle Type.TYPE_MATCH =>
   12.67        raise CASE_ERROR ("type mismatch", ~1)
    13.1 --- a/src/HOL/Tools/Datatype/datatype_codegen.ML	Mon Nov 30 11:42:48 2009 +0100
    13.2 +++ b/src/HOL/Tools/Datatype/datatype_codegen.ML	Mon Nov 30 11:42:49 2009 +0100
    13.3 @@ -1,4 +1,4 @@
    13.4 -(*  Title:      HOL/Tools/datatype_codegen.ML
    13.5 +(*  Title:      HOL/Tools/Datatype/datatype_codegen.ML
    13.6      Author:     Stefan Berghofer and Florian Haftmann, TU Muenchen
    13.7  
    13.8  Code generator facilities for inductive datatypes.
    13.9 @@ -6,48 +6,23 @@
   13.10  
   13.11  signature DATATYPE_CODEGEN =
   13.12  sig
   13.13 -  include DATATYPE_COMMON
   13.14 -  val find_shortest_path: descr -> int -> (string * int) option
   13.15    val setup: theory -> theory
   13.16  end;
   13.17  
   13.18 -structure DatatypeCodegen : DATATYPE_CODEGEN =
   13.19 +structure Datatype_Codegen : DATATYPE_CODEGEN =
   13.20  struct
   13.21  
   13.22 -open DatatypeAux
   13.23 -
   13.24 -(** find shortest path to constructor with no recursive arguments **)
   13.25 -
   13.26 -fun find_nonempty (descr: descr) is i =
   13.27 -  let
   13.28 -    val (_, _, constrs) = the (AList.lookup (op =) descr i);
   13.29 -    fun arg_nonempty (_, DatatypeAux.DtRec i) = if member (op =) is i
   13.30 -          then NONE
   13.31 -          else Option.map (Integer.add 1 o snd) (find_nonempty descr (i::is) i)
   13.32 -      | arg_nonempty _ = SOME 0;
   13.33 -    fun max xs = Library.foldl
   13.34 -      (fn (NONE, _) => NONE
   13.35 -        | (SOME i, SOME j) => SOME (Int.max (i, j))
   13.36 -        | (_, NONE) => NONE) (SOME 0, xs);
   13.37 -    val xs = sort (int_ord o pairself snd)
   13.38 -      (map_filter (fn (s, dts) => Option.map (pair s)
   13.39 -        (max (map (arg_nonempty o DatatypeAux.strip_dtyp) dts))) constrs)
   13.40 -  in case xs of [] => NONE | x :: _ => SOME x end;
   13.41 -
   13.42 -fun find_shortest_path descr i = find_nonempty descr [i] i;
   13.43 -
   13.44 -
   13.45  (** SML code generator **)
   13.46  
   13.47  open Codegen;
   13.48  
   13.49  (* datatype definition *)
   13.50  
   13.51 -fun add_dt_defs thy defs dep module (descr: descr) sorts gr =
   13.52 +fun add_dt_defs thy defs dep module descr sorts gr =
   13.53    let
   13.54 -    val descr' = filter (can (map DatatypeAux.dest_DtTFree o #2 o snd)) descr;
   13.55 +    val descr' = filter (can (map Datatype_Aux.dest_DtTFree o #2 o snd)) descr;
   13.56      val rtnames = map (#1 o snd) (filter (fn (_, (_, _, cs)) =>
   13.57 -      exists (exists DatatypeAux.is_rec_type o snd) cs) descr');
   13.58 +      exists (exists Datatype_Aux.is_rec_type o snd) cs) descr');
   13.59  
   13.60      val (_, (tname, _, _)) :: _ = descr';
   13.61      val node_id = tname ^ " (type)";
   13.62 @@ -56,8 +31,8 @@
   13.63      fun mk_dtdef prfx [] gr = ([], gr)
   13.64        | mk_dtdef prfx ((_, (tname, dts, cs))::xs) gr =
   13.65            let
   13.66 -            val tvs = map DatatypeAux.dest_DtTFree dts;
   13.67 -            val cs' = map (apsnd (map (DatatypeAux.typ_of_dtyp descr sorts))) cs;
   13.68 +            val tvs = map Datatype_Aux.dest_DtTFree dts;
   13.69 +            val cs' = map (apsnd (map (Datatype_Aux.typ_of_dtyp descr sorts))) cs;
   13.70              val ((_, type_id), gr') = mk_type_id module' tname gr;
   13.71              val (ps, gr'') = gr' |>
   13.72                fold_map (fn (cname, cargs) =>
   13.73 @@ -87,8 +62,8 @@
   13.74      fun mk_term_of_def gr prfx [] = []
   13.75        | mk_term_of_def gr prfx ((_, (tname, dts, cs)) :: xs) =
   13.76            let
   13.77 -            val cs' = map (apsnd (map (DatatypeAux.typ_of_dtyp descr sorts))) cs;
   13.78 -            val dts' = map (DatatypeAux.typ_of_dtyp descr sorts) dts;
   13.79 +            val cs' = map (apsnd (map (Datatype_Aux.typ_of_dtyp descr sorts))) cs;
   13.80 +            val dts' = map (Datatype_Aux.typ_of_dtyp descr sorts) dts;
   13.81              val T = Type (tname, dts');
   13.82              val rest = mk_term_of_def gr "and " xs;
   13.83              val (eqs, _) = fold_map (fn (cname, Ts) => fn prfx =>
   13.84 @@ -110,12 +85,12 @@
   13.85      fun mk_gen_of_def gr prfx [] = []
   13.86        | mk_gen_of_def gr prfx ((i, (tname, dts, cs)) :: xs) =
   13.87            let
   13.88 -            val tvs = map DatatypeAux.dest_DtTFree dts;
   13.89 -            val Us = map (DatatypeAux.typ_of_dtyp descr sorts) dts;
   13.90 +            val tvs = map Datatype_Aux.dest_DtTFree dts;
   13.91 +            val Us = map (Datatype_Aux.typ_of_dtyp descr sorts) dts;
   13.92              val T = Type (tname, Us);
   13.93              val (cs1, cs2) =
   13.94 -              List.partition (exists DatatypeAux.is_rec_type o snd) cs;
   13.95 -            val SOME (cname, _) = find_shortest_path descr i;
   13.96 +              List.partition (exists Datatype_Aux.is_rec_type o snd) cs;
   13.97 +            val SOME (cname, _) = Datatype_Aux.find_shortest_path descr i;
   13.98  
   13.99              fun mk_delay p = Pretty.block
  13.100                [str "fn () =>", Pretty.brk 1, p];
  13.101 @@ -125,14 +100,14 @@
  13.102              fun mk_constr s b (cname, dts) =
  13.103                let
  13.104                  val gs = map (fn dt => mk_app false (mk_gen gr module' false rtnames s
  13.105 -                    (DatatypeAux.typ_of_dtyp descr sorts dt))
  13.106 -                  [str (if b andalso DatatypeAux.is_rec_type dt then "0"
  13.107 +                    (Datatype_Aux.typ_of_dtyp descr sorts dt))
  13.108 +                  [str (if b andalso Datatype_Aux.is_rec_type dt then "0"
  13.109                       else "j")]) dts;
  13.110 -                val Ts = map (DatatypeAux.typ_of_dtyp descr sorts) dts;
  13.111 +                val Ts = map (Datatype_Aux.typ_of_dtyp descr sorts) dts;
  13.112                  val xs = map str
  13.113 -                  (DatatypeProp.indexify_names (replicate (length dts) "x"));
  13.114 +                  (Datatype_Prop.indexify_names (replicate (length dts) "x"));
  13.115                  val ts = map str
  13.116 -                  (DatatypeProp.indexify_names (replicate (length dts) "t"));
  13.117 +                  (Datatype_Prop.indexify_names (replicate (length dts) "t"));
  13.118                  val (_, id) = get_const_id gr cname
  13.119                in
  13.120                  mk_let
  13.121 @@ -378,10 +353,10 @@
  13.122         | _ => NONE) cos;
  13.123      fun prep_inject (trueprop $ (equiv $ (_ $ t1 $ t2) $ rhs)) =
  13.124        trueprop $ (equiv $ mk_eq (t1, t2) $ rhs);
  13.125 -    val injects = map prep_inject (nth (DatatypeProp.make_injs [descr] vs) index);
  13.126 +    val injects = map prep_inject (nth (Datatype_Prop.make_injs [descr] vs) index);
  13.127      fun prep_distinct (trueprop $ (not $ (_ $ t1 $ t2))) =
  13.128        [trueprop $ false_eq (t1, t2), trueprop $ false_eq (t2, t1)];
  13.129 -    val distincts = maps prep_distinct (snd (nth (DatatypeProp.make_distincts [descr] vs) index));
  13.130 +    val distincts = maps prep_distinct (snd (nth (Datatype_Prop.make_distincts [descr] vs) index));
  13.131      val refl = HOLogic.mk_Trueprop (true_eq (Free ("x", ty), Free ("x", ty)));
  13.132      val simpset = Simplifier.context (ProofContext.init thy) (HOL_basic_ss addsimps 
  13.133        (map Simpdata.mk_eq (@{thm eq} :: @{thm eq_True} :: inject_thms @ distinct_thms)));
  13.134 @@ -436,7 +411,7 @@
  13.135    in
  13.136      if null css then thy
  13.137      else thy
  13.138 -      |> tap (fn _ => DatatypeAux.message config "Registering datatype for code generator ...")
  13.139 +      |> tap (fn _ => Datatype_Aux.message config "Registering datatype for code generator ...")
  13.140        |> fold Code.add_datatype css
  13.141        |> fold_rev Code.add_default_eqn case_rewrites
  13.142        |> fold Code.add_case certs
    14.1 --- a/src/HOL/Tools/Datatype/datatype_data.ML	Mon Nov 30 11:42:48 2009 +0100
    14.2 +++ b/src/HOL/Tools/Datatype/datatype_data.ML	Mon Nov 30 11:42:49 2009 +0100
    14.3 @@ -1,7 +1,7 @@
    14.4 -(*  Title:      HOL/Tools/datatype.ML
    14.5 +(*  Title:      HOL/Tools/Datatype/datatype_data.ML
    14.6      Author:     Stefan Berghofer, TU Muenchen
    14.7  
    14.8 -Datatype package for Isabelle/HOL.
    14.9 +Datatype package: bookkeeping; interpretation of existing types as datatypes.
   14.10  *)
   14.11  
   14.12  signature DATATYPE_DATA =
   14.13 @@ -25,7 +25,7 @@
   14.14    val info_of_constr : theory -> string * typ -> info option
   14.15    val info_of_case : theory -> string -> info option
   14.16    val interpretation : (config -> string list -> theory -> theory) -> theory -> theory
   14.17 -  val make_case :  Proof.context -> DatatypeCase.config -> string list -> term ->
   14.18 +  val make_case :  Proof.context -> Datatype_Case.config -> string list -> term ->
   14.19      (term * term) list -> term * (term * (int * bool)) list
   14.20    val strip_case : Proof.context -> bool -> term -> (term * (term * term) list) option
   14.21    val read_typ: theory -> string -> (string * sort) list -> typ * (string * sort) list
   14.22 @@ -37,7 +37,7 @@
   14.23  structure Datatype_Data: DATATYPE_DATA =
   14.24  struct
   14.25  
   14.26 -open DatatypeAux;
   14.27 +open Datatype_Aux;
   14.28  
   14.29  (** theory data **)
   14.30  
   14.31 @@ -104,9 +104,9 @@
   14.32      val info as { descr, index, sorts = raw_sorts, ... } = the_info thy dtco;
   14.33      val SOME (_, dtys, raw_cos) = AList.lookup (op =) descr index;
   14.34      val sorts = map ((fn v => (v, (the o AList.lookup (op =) raw_sorts) v))
   14.35 -      o DatatypeAux.dest_DtTFree) dtys;
   14.36 +      o Datatype_Aux.dest_DtTFree) dtys;
   14.37      val cos = map
   14.38 -      (fn (co, tys) => (co, map (DatatypeAux.typ_of_dtyp descr sorts) tys)) raw_cos;
   14.39 +      (fn (co, tys) => (co, map (Datatype_Aux.typ_of_dtyp descr sorts) tys)) raw_cos;
   14.40    in (sorts, cos) end;
   14.41  
   14.42  fun the_descr thy (raw_tycos as raw_tyco :: _) =
   14.43 @@ -197,7 +197,7 @@
   14.44    in map (fn (c, _) => space_implode "_" (Long_Name.base_name c :: bnames)) constrs end;
   14.45  
   14.46  fun induct_cases descr =
   14.47 -  DatatypeProp.indexify_names (maps (dt_cases descr) (map #2 descr));
   14.48 +  Datatype_Prop.indexify_names (maps (dt_cases descr) (map #2 descr));
   14.49  
   14.50  fun exhaust_cases descr i = dt_cases descr (the (AList.lookup (op =) descr i));
   14.51  
   14.52 @@ -214,22 +214,22 @@
   14.53  
   14.54  (* translation rules for case *)
   14.55  
   14.56 -fun make_case ctxt = DatatypeCase.make_case
   14.57 +fun make_case ctxt = Datatype_Case.make_case
   14.58    (info_of_constr (ProofContext.theory_of ctxt)) ctxt;
   14.59  
   14.60 -fun strip_case ctxt = DatatypeCase.strip_case
   14.61 +fun strip_case ctxt = Datatype_Case.strip_case
   14.62    (info_of_case (ProofContext.theory_of ctxt));
   14.63  
   14.64  fun add_case_tr' case_names thy =
   14.65    Sign.add_advanced_trfuns ([], [],
   14.66      map (fn case_name =>
   14.67        let val case_name' = Sign.const_syntax_name thy case_name
   14.68 -      in (case_name', DatatypeCase.case_tr' info_of_case case_name')
   14.69 +      in (case_name', Datatype_Case.case_tr' info_of_case case_name')
   14.70        end) case_names, []) thy;
   14.71  
   14.72  val trfun_setup =
   14.73    Sign.add_advanced_trfuns ([],
   14.74 -    [("_case_syntax", DatatypeCase.case_tr true info_of_constr)],
   14.75 +    [("_case_syntax", Datatype_Case.case_tr true info_of_constr)],
   14.76      [], []);
   14.77  
   14.78  
   14.79 @@ -299,21 +299,21 @@
   14.80      val new_type_names = map Long_Name.base_name (the_default dt_names alt_names);
   14.81      val _ = message config ("Deriving properties for datatype(s) " ^ commas_quote new_type_names);
   14.82  
   14.83 -    val (exhaust, thy3) = DatatypeAbsProofs.prove_casedist_thms config new_type_names
   14.84 +    val (exhaust, thy3) = Datatype_Abs_Proofs.prove_casedist_thms config new_type_names
   14.85        descr sorts induct (mk_case_names_exhausts flat_descr dt_names) thy2;
   14.86 -    val (nchotomys, thy4) = DatatypeAbsProofs.prove_nchotomys config new_type_names
   14.87 +    val (nchotomys, thy4) = Datatype_Abs_Proofs.prove_nchotomys config new_type_names
   14.88        descr sorts exhaust thy3;
   14.89 -    val ((rec_names, rec_rewrites), thy5) = DatatypeAbsProofs.prove_primrec_thms
   14.90 +    val ((rec_names, rec_rewrites), thy5) = Datatype_Abs_Proofs.prove_primrec_thms
   14.91        config new_type_names descr sorts (#inject o the o Symtab.lookup (get_all thy4))
   14.92        inject (distinct, all_distincts thy2 (get_rec_types flat_descr sorts))
   14.93        induct thy4;
   14.94 -    val ((case_rewrites, case_names), thy6) = DatatypeAbsProofs.prove_case_thms
   14.95 +    val ((case_rewrites, case_names), thy6) = Datatype_Abs_Proofs.prove_case_thms
   14.96        config new_type_names descr sorts rec_names rec_rewrites thy5;
   14.97 -    val (case_congs, thy7) = DatatypeAbsProofs.prove_case_congs new_type_names
   14.98 +    val (case_congs, thy7) = Datatype_Abs_Proofs.prove_case_congs new_type_names
   14.99        descr sorts nchotomys case_rewrites thy6;
  14.100 -    val (weak_case_congs, thy8) = DatatypeAbsProofs.prove_weak_case_congs new_type_names
  14.101 +    val (weak_case_congs, thy8) = Datatype_Abs_Proofs.prove_weak_case_congs new_type_names
  14.102        descr sorts thy7;
  14.103 -    val (splits, thy9) = DatatypeAbsProofs.prove_split_thms
  14.104 +    val (splits, thy9) = Datatype_Abs_Proofs.prove_split_thms
  14.105        config new_type_names descr sorts inject distinct exhaust case_rewrites thy8;
  14.106  
  14.107      val inducts = Project_Rule.projections (ProofContext.init thy2) induct;
  14.108 @@ -416,9 +416,9 @@
  14.109        map (DtTFree o fst) vs,
  14.110        (map o apsnd) dtyps_of_typ constr))
  14.111      val descr = map_index mk_spec cs;
  14.112 -    val injs = DatatypeProp.make_injs [descr] vs;
  14.113 -    val half_distincts = map snd (DatatypeProp.make_distincts [descr] vs);
  14.114 -    val ind = DatatypeProp.make_ind [descr] vs;
  14.115 +    val injs = Datatype_Prop.make_injs [descr] vs;
  14.116 +    val half_distincts = map snd (Datatype_Prop.make_distincts [descr] vs);
  14.117 +    val ind = Datatype_Prop.make_ind [descr] vs;
  14.118      val rules = (map o map o map) Logic.close_form [[[ind]], injs, half_distincts];
  14.119  
  14.120      fun after_qed' raw_thms =
    15.1 --- a/src/HOL/Tools/Datatype/datatype_prop.ML	Mon Nov 30 11:42:48 2009 +0100
    15.2 +++ b/src/HOL/Tools/Datatype/datatype_prop.ML	Mon Nov 30 11:42:49 2009 +0100
    15.3 @@ -1,38 +1,39 @@
    15.4 -(*  Title:      HOL/Tools/datatype_prop.ML
    15.5 +(*  Title:      HOL/Tools/Datatype/datatype_prop.ML
    15.6      Author:     Stefan Berghofer, TU Muenchen
    15.7  
    15.8 -Characteristic properties of datatypes.
    15.9 +Datatype package: characteristic properties of datatypes.
   15.10  *)
   15.11  
   15.12  signature DATATYPE_PROP =
   15.13  sig
   15.14 +  include DATATYPE_COMMON
   15.15    val indexify_names: string list -> string list
   15.16    val make_tnames: typ list -> string list
   15.17 -  val make_injs : DatatypeAux.descr list -> (string * sort) list -> term list list
   15.18 -  val make_distincts : DatatypeAux.descr list ->
   15.19 +  val make_injs : descr list -> (string * sort) list -> term list list
   15.20 +  val make_distincts : descr list ->
   15.21      (string * sort) list -> (int * term list) list (*no symmetric inequalities*)
   15.22 -  val make_ind : DatatypeAux.descr list -> (string * sort) list -> term
   15.23 -  val make_casedists : DatatypeAux.descr list -> (string * sort) list -> term list
   15.24 -  val make_primrec_Ts : DatatypeAux.descr list -> (string * sort) list ->
   15.25 +  val make_ind : descr list -> (string * sort) list -> term
   15.26 +  val make_casedists : descr list -> (string * sort) list -> term list
   15.27 +  val make_primrec_Ts : descr list -> (string * sort) list ->
   15.28      string list -> typ list * typ list
   15.29 -  val make_primrecs : string list -> DatatypeAux.descr list ->
   15.30 +  val make_primrecs : string list -> descr list ->
   15.31      (string * sort) list -> theory -> term list
   15.32 -  val make_cases : string list -> DatatypeAux.descr list ->
   15.33 +  val make_cases : string list -> descr list ->
   15.34      (string * sort) list -> theory -> term list list
   15.35 -  val make_splits : string list -> DatatypeAux.descr list ->
   15.36 +  val make_splits : string list -> descr list ->
   15.37      (string * sort) list -> theory -> (term * term) list
   15.38 -  val make_weak_case_congs : string list -> DatatypeAux.descr list ->
   15.39 +  val make_weak_case_congs : string list -> descr list ->
   15.40      (string * sort) list -> theory -> term list
   15.41 -  val make_case_congs : string list -> DatatypeAux.descr list ->
   15.42 +  val make_case_congs : string list -> descr list ->
   15.43      (string * sort) list -> theory -> term list
   15.44 -  val make_nchotomys : DatatypeAux.descr list ->
   15.45 +  val make_nchotomys : descr list ->
   15.46      (string * sort) list -> term list
   15.47  end;
   15.48  
   15.49 -structure DatatypeProp : DATATYPE_PROP =
   15.50 +structure Datatype_Prop : DATATYPE_PROP =
   15.51  struct
   15.52  
   15.53 -open DatatypeAux;
   15.54 +open Datatype_Aux;
   15.55  
   15.56  fun indexify_names names =
   15.57    let
    16.1 --- a/src/HOL/Tools/Datatype/datatype_realizer.ML	Mon Nov 30 11:42:48 2009 +0100
    16.2 +++ b/src/HOL/Tools/Datatype/datatype_realizer.ML	Mon Nov 30 11:42:49 2009 +0100
    16.3 @@ -1,8 +1,8 @@
    16.4 -(*  Title:      HOL/Tools/datatype_realizer.ML
    16.5 +(*  Title:      HOL/Tools/Datatype/datatype_realizer.ML
    16.6      Author:     Stefan Berghofer, TU Muenchen
    16.7  
    16.8 -Porgram extraction from proofs involving datatypes:
    16.9 -Realizers for induction and case analysis
   16.10 +Program extraction from proofs involving datatypes:
   16.11 +realizers for induction and case analysis.
   16.12  *)
   16.13  
   16.14  signature DATATYPE_REALIZER =
   16.15 @@ -11,10 +11,10 @@
   16.16    val setup: theory -> theory
   16.17  end;
   16.18  
   16.19 -structure DatatypeRealizer : DATATYPE_REALIZER =
   16.20 +structure Datatype_Realizer : DATATYPE_REALIZER =
   16.21  struct
   16.22  
   16.23 -open DatatypeAux;
   16.24 +open Datatype_Aux;
   16.25  
   16.26  fun subsets i j = if i <= j then
   16.27         let val is = subsets (i+1) j
   16.28 @@ -60,7 +60,7 @@
   16.29        (fn ((i, (_, _, constrs)), T) => fold_map (fn (cname, cargs) => fn j =>
   16.30          let
   16.31            val Ts = map (typ_of_dtyp descr sorts) cargs;
   16.32 -          val tnames = Name.variant_list pnames (DatatypeProp.make_tnames Ts);
   16.33 +          val tnames = Name.variant_list pnames (Datatype_Prop.make_tnames Ts);
   16.34            val recs = filter (is_rec_type o fst o fst) (cargs ~~ tnames ~~ Ts);
   16.35            val frees = tnames ~~ Ts;
   16.36  
   16.37 @@ -97,7 +97,7 @@
   16.38            if (j: int) = i then HOLogic.mk_fst t
   16.39            else mk_proj j is (HOLogic.mk_snd t);
   16.40  
   16.41 -    val tnames = DatatypeProp.make_tnames recTs;
   16.42 +    val tnames = Datatype_Prop.make_tnames recTs;
   16.43      val fTs = map fastype_of rec_fns;
   16.44      val ps = map (fn ((((i, _), T), U), s) => Abs ("x", T, make_pred i U T
   16.45        (list_comb (Const (s, fTs ---> T --> U), rec_fns) $ Bound 0) (Bound 0)))
   16.46 @@ -132,7 +132,7 @@
   16.47          (Binding.qualified_name (space_implode "_" (ind_name :: vs @ ["correctness"])), thm)
   16.48        ||> Sign.restore_naming thy;
   16.49  
   16.50 -    val ivs = rev (Term.add_vars (Logic.varify (DatatypeProp.make_ind [descr] sorts)) []);
   16.51 +    val ivs = rev (Term.add_vars (Logic.varify (Datatype_Prop.make_ind [descr] sorts)) []);
   16.52      val rvs = rev (Thm.fold_terms Term.add_vars thm' []);
   16.53      val ivs1 = map Var (filter_out (fn (_, T) =>
   16.54        tname_of (body_type T) mem ["set", "bool"]) ivs);
   16.55 @@ -169,7 +169,7 @@
   16.56      fun make_casedist_prem T (cname, cargs) =
   16.57        let
   16.58          val Ts = map (typ_of_dtyp descr sorts) cargs;
   16.59 -        val frees = Name.variant_list ["P", "y"] (DatatypeProp.make_tnames Ts) ~~ Ts;
   16.60 +        val frees = Name.variant_list ["P", "y"] (Datatype_Prop.make_tnames Ts) ~~ Ts;
   16.61          val free_ts = map Free frees;
   16.62          val r = Free ("r" ^ Long_Name.base_name cname, Ts ---> rT)
   16.63        in (r, list_all_free (frees, Logic.mk_implies (HOLogic.mk_Trueprop
    17.1 --- a/src/HOL/Tools/Datatype/datatype_rep_proofs.ML	Mon Nov 30 11:42:48 2009 +0100
    17.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    17.3 @@ -1,747 +0,0 @@
    17.4 -(*  Title:      HOL/Tools/datatype_rep_proofs.ML
    17.5 -    Author:     Stefan Berghofer, TU Muenchen
    17.6 -
    17.7 -Definitional introduction of datatypes with proof of characteristic theorems:
    17.8 -
    17.9 - - injectivity of constructors
   17.10 - - distinctness of constructors
   17.11 - - induction theorem
   17.12 -*)
   17.13 -
   17.14 -signature DATATYPE_REP_PROOFS =
   17.15 -sig
   17.16 -  val add_datatype : DatatypeAux.config -> string list -> (string list * binding * mixfix *
   17.17 -    (binding * typ list * mixfix) list) list -> theory -> string list * theory
   17.18 -  val datatype_cmd : string list -> (string list * binding * mixfix *
   17.19 -    (binding * string list * mixfix) list) list -> theory -> theory
   17.20 -end;
   17.21 -
   17.22 -structure DatatypeRepProofs : DATATYPE_REP_PROOFS =
   17.23 -struct
   17.24 -
   17.25 -(** auxiliary **)
   17.26 -
   17.27 -open DatatypeAux;
   17.28 -
   17.29 -val (_ $ (_ $ (_ $ (distinct_f $ _) $ _))) = hd (prems_of distinct_lemma);
   17.30 -
   17.31 -val collect_simp = rewrite_rule [mk_meta_eq mem_Collect_eq];
   17.32 -
   17.33 -fun exh_thm_of (dt_info : info Symtab.table) tname =
   17.34 -  #exhaust (the (Symtab.lookup dt_info tname));
   17.35 -
   17.36 -val node_name = @{type_name "Datatype.node"};
   17.37 -val In0_name = @{const_name "Datatype.In0"};
   17.38 -val In1_name = @{const_name "Datatype.In1"};
   17.39 -val Scons_name = @{const_name "Datatype.Scons"};
   17.40 -val Leaf_name = @{const_name "Datatype.Leaf"};
   17.41 -val Numb_name = @{const_name "Datatype.Numb"};
   17.42 -val Lim_name = @{const_name "Datatype.Lim"};
   17.43 -val Suml_name = @{const_name "Sum_Type.Suml"};
   17.44 -val Sumr_name = @{const_name "Sum_Type.Sumr"};
   17.45 -
   17.46 -val In0_inject = @{thm In0_inject};
   17.47 -val In1_inject = @{thm In1_inject};
   17.48 -val Scons_inject = @{thm Scons_inject};
   17.49 -val Leaf_inject = @{thm Leaf_inject};
   17.50 -val In0_eq = @{thm In0_eq};
   17.51 -val In1_eq = @{thm In1_eq};
   17.52 -val In0_not_In1 = @{thm In0_not_In1};
   17.53 -val In1_not_In0 = @{thm In1_not_In0};
   17.54 -val Lim_inject = @{thm Lim_inject};
   17.55 -val Inl_inject = @{thm Inl_inject};
   17.56 -val Inr_inject = @{thm Inr_inject};
   17.57 -val Suml_inject = @{thm Suml_inject};
   17.58 -val Sumr_inject = @{thm Sumr_inject};
   17.59 -
   17.60 -
   17.61 -
   17.62 -(** proof of characteristic theorems **)
   17.63 -
   17.64 -fun representation_proofs (config : config) (dt_info : info Symtab.table)
   17.65 -      new_type_names descr sorts types_syntax constr_syntax case_names_induct thy =
   17.66 -  let
   17.67 -    val descr' = flat descr;
   17.68 -    val big_name = space_implode "_" new_type_names;
   17.69 -    val thy1 = Sign.add_path big_name thy;
   17.70 -    val big_rec_name = big_name ^ "_rep_set";
   17.71 -    val rep_set_names' =
   17.72 -      (if length descr' = 1 then [big_rec_name] else
   17.73 -        (map ((curry (op ^) (big_rec_name ^ "_")) o string_of_int)
   17.74 -          (1 upto (length descr'))));
   17.75 -    val rep_set_names = map (Sign.full_bname thy1) rep_set_names';
   17.76 -
   17.77 -    val tyvars = map (fn (_, (_, Ts, _)) => map dest_DtTFree Ts) (hd descr);
   17.78 -    val leafTs' = get_nonrec_types descr' sorts;
   17.79 -    val branchTs = get_branching_types descr' sorts;
   17.80 -    val branchT = if null branchTs then HOLogic.unitT
   17.81 -      else Balanced_Tree.make (fn (T, U) => Type ("+", [T, U])) branchTs;
   17.82 -    val arities = remove (op =) 0 (get_arities descr');
   17.83 -    val unneeded_vars =
   17.84 -      subtract (op =) (List.foldr OldTerm.add_typ_tfree_names [] (leafTs' @ branchTs)) (hd tyvars);
   17.85 -    val leafTs = leafTs' @ map (fn n => TFree (n, (the o AList.lookup (op =) sorts) n)) unneeded_vars;
   17.86 -    val recTs = get_rec_types descr' sorts;
   17.87 -    val (newTs, oldTs) = chop (length (hd descr)) recTs;
   17.88 -    val sumT = if null leafTs then HOLogic.unitT
   17.89 -      else Balanced_Tree.make (fn (T, U) => Type ("+", [T, U])) leafTs;
   17.90 -    val Univ_elT = HOLogic.mk_setT (Type (node_name, [sumT, branchT]));
   17.91 -    val UnivT = HOLogic.mk_setT Univ_elT;
   17.92 -    val UnivT' = Univ_elT --> HOLogic.boolT;
   17.93 -    val Collect = Const (@{const_name Collect}, UnivT' --> UnivT);
   17.94 -
   17.95 -    val In0 = Const (In0_name, Univ_elT --> Univ_elT);
   17.96 -    val In1 = Const (In1_name, Univ_elT --> Univ_elT);
   17.97 -    val Leaf = Const (Leaf_name, sumT --> Univ_elT);
   17.98 -    val Lim = Const (Lim_name, (branchT --> Univ_elT) --> Univ_elT);
   17.99 -
  17.100 -    (* make injections needed for embedding types in leaves *)
  17.101 -
  17.102 -    fun mk_inj T' x =
  17.103 -      let
  17.104 -        fun mk_inj' T n i =
  17.105 -          if n = 1 then x else
  17.106 -          let val n2 = n div 2;
  17.107 -              val Type (_, [T1, T2]) = T
  17.108 -          in
  17.109 -            if i <= n2 then
  17.110 -              Const (@{const_name "Sum_Type.Inl"}, T1 --> T) $ (mk_inj' T1 n2 i)
  17.111 -            else
  17.112 -              Const (@{const_name "Sum_Type.Inr"}, T2 --> T) $ (mk_inj' T2 (n - n2) (i - n2))
  17.113 -          end
  17.114 -      in mk_inj' sumT (length leafTs) (1 + find_index (fn T'' => T'' = T') leafTs)
  17.115 -      end;
  17.116 -
  17.117 -    (* make injections for constructors *)
  17.118 -
  17.119 -    fun mk_univ_inj ts = Balanced_Tree.access
  17.120 -      {left = fn t => In0 $ t,
  17.121 -        right = fn t => In1 $ t,
  17.122 -        init =
  17.123 -          if ts = [] then Const (@{const_name undefined}, Univ_elT)
  17.124 -          else foldr1 (HOLogic.mk_binop Scons_name) ts};
  17.125 -
  17.126 -    (* function spaces *)
  17.127 -
  17.128 -    fun mk_fun_inj T' x =
  17.129 -      let
  17.130 -        fun mk_inj T n i =
  17.131 -          if n = 1 then x else
  17.132 -          let
  17.133 -            val n2 = n div 2;
  17.134 -            val Type (_, [T1, T2]) = T;
  17.135 -            fun mkT U = (U --> Univ_elT) --> T --> Univ_elT
  17.136 -          in
  17.137 -            if i <= n2 then Const (Suml_name, mkT T1) $ mk_inj T1 n2 i
  17.138 -            else Const (Sumr_name, mkT T2) $ mk_inj T2 (n - n2) (i - n2)
  17.139 -          end
  17.140 -      in mk_inj branchT (length branchTs) (1 + find_index (fn T'' => T'' = T') branchTs)
  17.141 -      end;
  17.142 -
  17.143 -    fun mk_lim t Ts = fold_rev (fn T => fn t => Lim $ mk_fun_inj T (Abs ("x", T, t))) Ts t;
  17.144 -
  17.145 -    (************** generate introduction rules for representing set **********)
  17.146 -
  17.147 -    val _ = message config "Constructing representing sets ...";
  17.148 -
  17.149 -    (* make introduction rule for a single constructor *)
  17.150 -
  17.151 -    fun make_intr s n (i, (_, cargs)) =
  17.152 -      let
  17.153 -        fun mk_prem dt (j, prems, ts) =
  17.154 -          (case strip_dtyp dt of
  17.155 -            (dts, DtRec k) =>
  17.156 -              let
  17.157 -                val Ts = map (typ_of_dtyp descr' sorts) dts;
  17.158 -                val free_t =
  17.159 -                  app_bnds (mk_Free "x" (Ts ---> Univ_elT) j) (length Ts)
  17.160 -              in (j + 1, list_all (map (pair "x") Ts,
  17.161 -                  HOLogic.mk_Trueprop
  17.162 -                    (Free (nth rep_set_names' k, UnivT') $ free_t)) :: prems,
  17.163 -                mk_lim free_t Ts :: ts)
  17.164 -              end
  17.165 -          | _ =>
  17.166 -              let val T = typ_of_dtyp descr' sorts dt
  17.167 -              in (j + 1, prems, (Leaf $ mk_inj T (mk_Free "x" T j))::ts)
  17.168 -              end);
  17.169 -
  17.170 -        val (_, prems, ts) = fold_rev mk_prem cargs (1, [], []);
  17.171 -        val concl = HOLogic.mk_Trueprop
  17.172 -          (Free (s, UnivT') $ mk_univ_inj ts n i)
  17.173 -      in Logic.list_implies (prems, concl)
  17.174 -      end;
  17.175 -
  17.176 -    val intr_ts = maps (fn ((_, (_, _, constrs)), rep_set_name) =>
  17.177 -      map (make_intr rep_set_name (length constrs))
  17.178 -        ((1 upto (length constrs)) ~~ constrs)) (descr' ~~ rep_set_names');
  17.179 -
  17.180 -    val ({raw_induct = rep_induct, intrs = rep_intrs, ...}, thy2) =
  17.181 -      thy1
  17.182 -      |> Sign.map_naming Name_Space.conceal
  17.183 -      |> Inductive.add_inductive_global
  17.184 -          {quiet_mode = #quiet config, verbose = false, alt_name = Binding.name big_rec_name,
  17.185 -           coind = false, no_elim = true, no_ind = false, skip_mono = true, fork_mono = false}
  17.186 -          (map (fn s => ((Binding.name s, UnivT'), NoSyn)) rep_set_names') []
  17.187 -          (map (fn x => (Attrib.empty_binding, x)) intr_ts) []
  17.188 -      ||> Sign.restore_naming thy1
  17.189 -      ||> Theory.checkpoint;
  17.190 -
  17.191 -    (********************************* typedef ********************************)
  17.192 -
  17.193 -    val (typedefs, thy3) = thy2 |>
  17.194 -      Sign.parent_path |>
  17.195 -      fold_map (fn ((((name, mx), tvs), c), name') =>
  17.196 -          Typedef.add_typedef false (SOME (Binding.name name')) (name, tvs, mx)
  17.197 -            (Collect $ Const (c, UnivT')) NONE
  17.198 -            (rtac exI 1 THEN rtac CollectI 1 THEN
  17.199 -              QUIET_BREADTH_FIRST (has_fewer_prems 1)
  17.200 -              (resolve_tac rep_intrs 1)))
  17.201 -                (types_syntax ~~ tyvars ~~
  17.202 -                  (take (length newTs) rep_set_names) ~~ new_type_names) ||>
  17.203 -      Sign.add_path big_name;
  17.204 -
  17.205 -    (*********************** definition of constructors ***********************)
  17.206 -
  17.207 -    val big_rep_name = (space_implode "_" new_type_names) ^ "_Rep_";
  17.208 -    val rep_names = map (curry op ^ "Rep_") new_type_names;
  17.209 -    val rep_names' = map (fn i => big_rep_name ^ (string_of_int i))
  17.210 -      (1 upto (length (flat (tl descr))));
  17.211 -    val all_rep_names = map (Sign.intern_const thy3) rep_names @
  17.212 -      map (Sign.full_bname thy3) rep_names';
  17.213 -
  17.214 -    (* isomorphism declarations *)
  17.215 -
  17.216 -    val iso_decls = map (fn (T, s) => (Binding.name s, T --> Univ_elT, NoSyn))
  17.217 -      (oldTs ~~ rep_names');
  17.218 -
  17.219 -    (* constructor definitions *)
  17.220 -
  17.221 -    fun make_constr_def tname T n ((cname, cargs), (cname', mx)) (thy, defs, eqns, i) =
  17.222 -      let
  17.223 -        fun constr_arg dt (j, l_args, r_args) =
  17.224 -          let val T = typ_of_dtyp descr' sorts dt;
  17.225 -              val free_t = mk_Free "x" T j
  17.226 -          in (case (strip_dtyp dt, strip_type T) of
  17.227 -              ((_, DtRec m), (Us, U)) => (j + 1, free_t :: l_args, mk_lim
  17.228 -                (Const (nth all_rep_names m, U --> Univ_elT) $
  17.229 -                   app_bnds free_t (length Us)) Us :: r_args)
  17.230 -            | _ => (j + 1, free_t::l_args, (Leaf $ mk_inj T free_t)::r_args))
  17.231 -          end;
  17.232 -
  17.233 -        val (_, l_args, r_args) = fold_rev constr_arg cargs (1, [], []);
  17.234 -        val constrT = (map (typ_of_dtyp descr' sorts) cargs) ---> T;
  17.235 -        val abs_name = Sign.intern_const thy ("Abs_" ^ tname);
  17.236 -        val rep_name = Sign.intern_const thy ("Rep_" ^ tname);
  17.237 -        val lhs = list_comb (Const (cname, constrT), l_args);
  17.238 -        val rhs = mk_univ_inj r_args n i;
  17.239 -        val def = Logic.mk_equals (lhs, Const (abs_name, Univ_elT --> T) $ rhs);
  17.240 -        val def_name = Long_Name.base_name cname ^ "_def";
  17.241 -        val eqn = HOLogic.mk_Trueprop (HOLogic.mk_eq
  17.242 -          (Const (rep_name, T --> Univ_elT) $ lhs, rhs));
  17.243 -        val ([def_thm], thy') =
  17.244 -          thy
  17.245 -          |> Sign.add_consts_i [(cname', constrT, mx)]
  17.246 -          |> (PureThy.add_defs false o map Thm.no_attributes) [(Binding.name def_name, def)];
  17.247 -
  17.248 -      in (thy', defs @ [def_thm], eqns @ [eqn], i + 1) end;
  17.249 -
  17.250 -    (* constructor definitions for datatype *)
  17.251 -
  17.252 -    fun dt_constr_defs ((((_, (_, _, constrs)), tname), T), constr_syntax)
  17.253 -        (thy, defs, eqns, rep_congs, dist_lemmas) =
  17.254 -      let
  17.255 -        val _ $ (_ $ (cong_f $ _) $ _) = concl_of arg_cong;
  17.256 -        val rep_const = cterm_of thy
  17.257 -          (Const (Sign.intern_const thy ("Rep_" ^ tname), T --> Univ_elT));
  17.258 -        val cong' =
  17.259 -          Drule.standard (cterm_instantiate [(cterm_of thy cong_f, rep_const)] arg_cong);
  17.260 -        val dist =
  17.261 -          Drule.standard (cterm_instantiate [(cterm_of thy distinct_f, rep_const)] distinct_lemma);
  17.262 -        val (thy', defs', eqns', _) = fold ((make_constr_def tname T) (length constrs))
  17.263 -          (constrs ~~ constr_syntax) (Sign.add_path tname thy, defs, [], 1);
  17.264 -      in
  17.265 -        (Sign.parent_path thy', defs', eqns @ [eqns'],
  17.266 -          rep_congs @ [cong'], dist_lemmas @ [dist])
  17.267 -      end;
  17.268 -
  17.269 -    val (thy4, constr_defs, constr_rep_eqns, rep_congs, dist_lemmas) =
  17.270 -      fold dt_constr_defs
  17.271 -        (hd descr ~~ new_type_names ~~ newTs ~~ constr_syntax)
  17.272 -        (thy3 |> Sign.add_consts_i iso_decls |> Sign.parent_path, [], [], [], []);
  17.273 -
  17.274 -
  17.275 -    (*********** isomorphisms for new types (introduced by typedef) ***********)
  17.276 -
  17.277 -    val _ = message config "Proving isomorphism properties ...";
  17.278 -
  17.279 -    val newT_iso_axms = map (fn (_, td) =>
  17.280 -      (collect_simp (#Abs_inverse td), #Rep_inverse td,
  17.281 -       collect_simp (#Rep td))) typedefs;
  17.282 -
  17.283 -    val newT_iso_inj_thms = map (fn (_, td) =>
  17.284 -      (collect_simp (#Abs_inject td) RS iffD1, #Rep_inject td RS iffD1)) typedefs;
  17.285 -
  17.286 -    (********* isomorphisms between existing types and "unfolded" types *******)
  17.287 -
  17.288 -    (*---------------------------------------------------------------------*)
  17.289 -    (* isomorphisms are defined using primrec-combinators:                 *)
  17.290 -    (* generate appropriate functions for instantiating primrec-combinator *)
  17.291 -    (*                                                                     *)
  17.292 -    (*   e.g.  dt_Rep_i = list_rec ... (%h t y. In1 (Scons (Leaf h) y))    *)
  17.293 -    (*                                                                     *)
  17.294 -    (* also generate characteristic equations for isomorphisms             *)
  17.295 -    (*                                                                     *)
  17.296 -    (*   e.g.  dt_Rep_i (cons h t) = In1 (Scons (dt_Rep_j h) (dt_Rep_i t)) *)
  17.297 -    (*---------------------------------------------------------------------*)
  17.298 -
  17.299 -    fun make_iso_def k ks n (cname, cargs) (fs, eqns, i) =
  17.300 -      let
  17.301 -        val argTs = map (typ_of_dtyp descr' sorts) cargs;
  17.302 -        val T = nth recTs k;
  17.303 -        val rep_name = nth all_rep_names k;
  17.304 -        val rep_const = Const (rep_name, T --> Univ_elT);
  17.305 -        val constr = Const (cname, argTs ---> T);
  17.306 -
  17.307 -        fun process_arg ks' dt (i2, i2', ts, Ts) =
  17.308 -          let
  17.309 -            val T' = typ_of_dtyp descr' sorts dt;
  17.310 -            val (Us, U) = strip_type T'
  17.311 -          in (case strip_dtyp dt of
  17.312 -              (_, DtRec j) => if j mem ks' then
  17.313 -                  (i2 + 1, i2' + 1, ts @ [mk_lim (app_bnds
  17.314 -                     (mk_Free "y" (Us ---> Univ_elT) i2') (length Us)) Us],
  17.315 -                   Ts @ [Us ---> Univ_elT])
  17.316 -                else
  17.317 -                  (i2 + 1, i2', ts @ [mk_lim
  17.318 -                     (Const (nth all_rep_names j, U --> Univ_elT) $
  17.319 -                        app_bnds (mk_Free "x" T' i2) (length Us)) Us], Ts)
  17.320 -            | _ => (i2 + 1, i2', ts @ [Leaf $ mk_inj T' (mk_Free "x" T' i2)], Ts))
  17.321 -          end;
  17.322 -
  17.323 -        val (i2, i2', ts, Ts) = fold (process_arg ks) cargs (1, 1, [], []);
  17.324 -        val xs = map (uncurry (mk_Free "x")) (argTs ~~ (1 upto (i2 - 1)));
  17.325 -        val ys = map (uncurry (mk_Free "y")) (Ts ~~ (1 upto (i2' - 1)));
  17.326 -        val f = list_abs_free (map dest_Free (xs @ ys), mk_univ_inj ts n i);
  17.327 -
  17.328 -        val (_, _, ts', _) = fold (process_arg []) cargs (1, 1, [], []);
  17.329 -        val eqn = HOLogic.mk_Trueprop (HOLogic.mk_eq
  17.330 -          (rep_const $ list_comb (constr, xs), mk_univ_inj ts' n i))
  17.331 -
  17.332 -      in (fs @ [f], eqns @ [eqn], i + 1) end;
  17.333 -
  17.334 -    (* define isomorphisms for all mutually recursive datatypes in list ds *)
  17.335 -
  17.336 -    fun make_iso_defs ds (thy, char_thms) =
  17.337 -      let
  17.338 -        val ks = map fst ds;
  17.339 -        val (_, (tname, _, _)) = hd ds;
  17.340 -        val {rec_rewrites, rec_names, ...} = the (Symtab.lookup dt_info tname);
  17.341 -
  17.342 -        fun process_dt (k, (tname, _, constrs)) (fs, eqns, isos) =
  17.343 -          let
  17.344 -            val (fs', eqns', _) =
  17.345 -              fold (make_iso_def k ks (length constrs)) constrs (fs, eqns, 1);
  17.346 -            val iso = (nth recTs k, nth all_rep_names k)
  17.347 -          in (fs', eqns', isos @ [iso]) end;
  17.348 -        
  17.349 -        val (fs, eqns, isos) = fold process_dt ds ([], [], []);
  17.350 -        val fTs = map fastype_of fs;
  17.351 -        val defs = map (fn (rec_name, (T, iso_name)) => (Binding.name (Long_Name.base_name iso_name ^ "_def"),
  17.352 -          Logic.mk_equals (Const (iso_name, T --> Univ_elT),
  17.353 -            list_comb (Const (rec_name, fTs @ [T] ---> Univ_elT), fs)))) (rec_names ~~ isos);
  17.354 -        val (def_thms, thy') =
  17.355 -          apsnd Theory.checkpoint ((PureThy.add_defs false o map Thm.no_attributes) defs thy);
  17.356 -
  17.357 -        (* prove characteristic equations *)
  17.358 -
  17.359 -        val rewrites = def_thms @ (map mk_meta_eq rec_rewrites);
  17.360 -        val char_thms' = map (fn eqn => Skip_Proof.prove_global thy' [] [] eqn
  17.361 -          (fn _ => EVERY [rewrite_goals_tac rewrites, rtac refl 1])) eqns;
  17.362 -
  17.363 -      in (thy', char_thms' @ char_thms) end;
  17.364 -
  17.365 -    val (thy5, iso_char_thms) = apfst Theory.checkpoint (fold_rev make_iso_defs
  17.366 -        (tl descr) (Sign.add_path big_name thy4, []));
  17.367 -
  17.368 -    (* prove isomorphism properties *)
  17.369 -
  17.370 -    fun mk_funs_inv thy thm =
  17.371 -      let
  17.372 -        val prop = Thm.prop_of thm;
  17.373 -        val _ $ (_ $ ((S as Const (_, Type (_, [U, _]))) $ _ )) $
  17.374 -          (_ $ (_ $ (r $ (a $ _)) $ _)) = Type.legacy_freeze prop;
  17.375 -        val used = OldTerm.add_term_tfree_names (a, []);
  17.376 -
  17.377 -        fun mk_thm i =
  17.378 -          let
  17.379 -            val Ts = map (TFree o rpair HOLogic.typeS)
  17.380 -              (Name.variant_list used (replicate i "'t"));
  17.381 -            val f = Free ("f", Ts ---> U)
  17.382 -          in Skip_Proof.prove_global thy [] [] (Logic.mk_implies
  17.383 -            (HOLogic.mk_Trueprop (HOLogic.list_all
  17.384 -               (map (pair "x") Ts, S $ app_bnds f i)),
  17.385 -             HOLogic.mk_Trueprop (HOLogic.mk_eq (list_abs (map (pair "x") Ts,
  17.386 -               r $ (a $ app_bnds f i)), f))))
  17.387 -            (fn _ => EVERY [REPEAT_DETERM_N i (rtac ext 1),
  17.388 -               REPEAT (etac allE 1), rtac thm 1, atac 1])
  17.389 -          end
  17.390 -      in map (fn r => r RS subst) (thm :: map mk_thm arities) end;
  17.391 -
  17.392 -    (* prove  inj dt_Rep_i  and  dt_Rep_i x : dt_rep_set_i *)
  17.393 -
  17.394 -    val fun_congs = map (fn T => make_elim (Drule.instantiate'
  17.395 -      [SOME (ctyp_of thy5 T)] [] fun_cong)) branchTs;
  17.396 -
  17.397 -    fun prove_iso_thms ds (inj_thms, elem_thms) =
  17.398 -      let
  17.399 -        val (_, (tname, _, _)) = hd ds;
  17.400 -        val induct = (#induct o the o Symtab.lookup dt_info) tname;
  17.401 -
  17.402 -        fun mk_ind_concl (i, _) =
  17.403 -          let
  17.404 -            val T = nth recTs i;
  17.405 -            val Rep_t = Const (nth all_rep_names i, T --> Univ_elT);
  17.406 -            val rep_set_name = nth rep_set_names i
  17.407 -          in (HOLogic.all_const T $ Abs ("y", T, HOLogic.imp $
  17.408 -                HOLogic.mk_eq (Rep_t $ mk_Free "x" T i, Rep_t $ Bound 0) $
  17.409 -                  HOLogic.mk_eq (mk_Free "x" T i, Bound 0)),
  17.410 -              Const (rep_set_name, UnivT') $ (Rep_t $ mk_Free "x" T i))
  17.411 -          end;
  17.412 -
  17.413 -        val (ind_concl1, ind_concl2) = ListPair.unzip (map mk_ind_concl ds);
  17.414 -
  17.415 -        val rewrites = map mk_meta_eq iso_char_thms;
  17.416 -        val inj_thms' = map snd newT_iso_inj_thms @
  17.417 -          map (fn r => r RS @{thm injD}) inj_thms;
  17.418 -
  17.419 -        val inj_thm = Skip_Proof.prove_global thy5 [] []
  17.420 -          (HOLogic.mk_Trueprop (mk_conj ind_concl1)) (fn _ => EVERY
  17.421 -            [(indtac induct [] THEN_ALL_NEW ObjectLogic.atomize_prems_tac) 1,
  17.422 -             REPEAT (EVERY
  17.423 -               [rtac allI 1, rtac impI 1,
  17.424 -                exh_tac (exh_thm_of dt_info) 1,
  17.425 -                REPEAT (EVERY
  17.426 -                  [hyp_subst_tac 1,
  17.427 -                   rewrite_goals_tac rewrites,
  17.428 -                   REPEAT (dresolve_tac [In0_inject, In1_inject] 1),
  17.429 -                   (eresolve_tac [In0_not_In1 RS notE, In1_not_In0 RS notE] 1)
  17.430 -                   ORELSE (EVERY
  17.431 -                     [REPEAT (eresolve_tac (Scons_inject ::
  17.432 -                        map make_elim [Leaf_inject, Inl_inject, Inr_inject]) 1),
  17.433 -                      REPEAT (cong_tac 1), rtac refl 1,
  17.434 -                      REPEAT (atac 1 ORELSE (EVERY
  17.435 -                        [REPEAT (rtac ext 1),
  17.436 -                         REPEAT (eresolve_tac (mp :: allE ::
  17.437 -                           map make_elim (Suml_inject :: Sumr_inject ::
  17.438 -                             Lim_inject :: inj_thms') @ fun_congs) 1),
  17.439 -                         atac 1]))])])])]);
  17.440 -
  17.441 -        val inj_thms'' = map (fn r => r RS @{thm datatype_injI})
  17.442 -                             (split_conj_thm inj_thm);
  17.443 -
  17.444 -        val elem_thm = 
  17.445 -            Skip_Proof.prove_global thy5 [] [] (HOLogic.mk_Trueprop (mk_conj ind_concl2))
  17.446 -              (fn _ =>
  17.447 -               EVERY [(indtac induct [] THEN_ALL_NEW ObjectLogic.atomize_prems_tac) 1,
  17.448 -                rewrite_goals_tac rewrites,
  17.449 -                REPEAT ((resolve_tac rep_intrs THEN_ALL_NEW
  17.450 -                  ((REPEAT o etac allE) THEN' ares_tac elem_thms)) 1)]);
  17.451 -
  17.452 -      in (inj_thms'' @ inj_thms, elem_thms @ (split_conj_thm elem_thm))
  17.453 -      end;
  17.454 -
  17.455 -    val (iso_inj_thms_unfolded, iso_elem_thms) =
  17.456 -      fold_rev prove_iso_thms (tl descr) ([], map #3 newT_iso_axms);
  17.457 -    val iso_inj_thms = map snd newT_iso_inj_thms @
  17.458 -      map (fn r => r RS @{thm injD}) iso_inj_thms_unfolded;
  17.459 -
  17.460 -    (* prove  dt_rep_set_i x --> x : range dt_Rep_i *)
  17.461 -
  17.462 -    fun mk_iso_t (((set_name, iso_name), i), T) =
  17.463 -      let val isoT = T --> Univ_elT
  17.464 -      in HOLogic.imp $ 
  17.465 -        (Const (set_name, UnivT') $ mk_Free "x" Univ_elT i) $
  17.466 -          (if i < length newTs then HOLogic.true_const
  17.467 -           else HOLogic.mk_mem (mk_Free "x" Univ_elT i,
  17.468 -             Const (@{const_name image}, isoT --> HOLogic.mk_setT T --> UnivT) $
  17.469 -               Const (iso_name, isoT) $ Const (@{const_name UNIV}, HOLogic.mk_setT T)))
  17.470 -      end;
  17.471 -
  17.472 -    val iso_t = HOLogic.mk_Trueprop (mk_conj (map mk_iso_t
  17.473 -      (rep_set_names ~~ all_rep_names ~~ (0 upto (length descr' - 1)) ~~ recTs)));
  17.474 -
  17.475 -    (* all the theorems are proved by one single simultaneous induction *)
  17.476 -
  17.477 -    val range_eqs = map (fn r => mk_meta_eq (r RS @{thm range_ex1_eq}))
  17.478 -      iso_inj_thms_unfolded;
  17.479 -
  17.480 -    val iso_thms = if length descr = 1 then [] else
  17.481 -      drop (length newTs) (split_conj_thm
  17.482 -        (Skip_Proof.prove_global thy5 [] [] iso_t (fn _ => EVERY
  17.483 -           [(indtac rep_induct [] THEN_ALL_NEW ObjectLogic.atomize_prems_tac) 1,
  17.484 -            REPEAT (rtac TrueI 1),
  17.485 -            rewrite_goals_tac (mk_meta_eq choice_eq ::
  17.486 -              symmetric (mk_meta_eq @{thm expand_fun_eq}) :: range_eqs),
  17.487 -            rewrite_goals_tac (map symmetric range_eqs),
  17.488 -            REPEAT (EVERY
  17.489 -              [REPEAT (eresolve_tac ([rangeE, ex1_implies_ex RS exE] @
  17.490 -                 maps (mk_funs_inv thy5 o #1) newT_iso_axms) 1),
  17.491 -               TRY (hyp_subst_tac 1),
  17.492 -               rtac (sym RS range_eqI) 1,
  17.493 -               resolve_tac iso_char_thms 1])])));
  17.494 -
  17.495 -    val Abs_inverse_thms' =
  17.496 -      map #1 newT_iso_axms @
  17.497 -      map2 (fn r_inj => fn r => @{thm f_the_inv_into_f} OF [r_inj, r RS mp])
  17.498 -        iso_inj_thms_unfolded iso_thms;
  17.499 -
  17.500 -    val Abs_inverse_thms = maps (mk_funs_inv thy5) Abs_inverse_thms';
  17.501 -
  17.502 -    (******************* freeness theorems for constructors *******************)
  17.503 -
  17.504 -    val _ = message config "Proving freeness of constructors ...";
  17.505 -
  17.506 -    (* prove theorem  Rep_i (Constr_j ...) = Inj_j ...  *)
  17.507 -    
  17.508 -    fun prove_constr_rep_thm eqn =
  17.509 -      let
  17.510 -        val inj_thms = map fst newT_iso_inj_thms;
  17.511 -        val rewrites = @{thm o_def} :: constr_defs @ (map (mk_meta_eq o #2) newT_iso_axms)
  17.512 -      in Skip_Proof.prove_global thy5 [] [] eqn (fn _ => EVERY
  17.513 -        [resolve_tac inj_thms 1,
  17.514 -         rewrite_goals_tac rewrites,
  17.515 -         rtac refl 3,
  17.516 -         resolve_tac rep_intrs 2,
  17.517 -         REPEAT (resolve_tac iso_elem_thms 1)])
  17.518 -      end;
  17.519 -
  17.520 -    (*--------------------------------------------------------------*)
  17.521 -    (* constr_rep_thms and rep_congs are used to prove distinctness *)
  17.522 -    (* of constructors.                                             *)
  17.523 -    (*--------------------------------------------------------------*)
  17.524 -
  17.525 -    val constr_rep_thms = map (map prove_constr_rep_thm) constr_rep_eqns;
  17.526 -
  17.527 -    val dist_rewrites = map (fn (rep_thms, dist_lemma) =>
  17.528 -      dist_lemma::(rep_thms @ [In0_eq, In1_eq, In0_not_In1, In1_not_In0]))
  17.529 -        (constr_rep_thms ~~ dist_lemmas);
  17.530 -
  17.531 -    fun prove_distinct_thms dist_rewrites' (k, ts) =
  17.532 -      let
  17.533 -        fun prove [] = []
  17.534 -          | prove (t :: ts) =
  17.535 -              let
  17.536 -                val dist_thm = Skip_Proof.prove_global thy5 [] [] t (fn _ =>
  17.537 -                  EVERY [simp_tac (HOL_ss addsimps dist_rewrites') 1])
  17.538 -              in dist_thm :: Drule.standard (dist_thm RS not_sym) :: prove ts end;
  17.539 -      in prove ts end;
  17.540 -
  17.541 -    val distinct_thms = map2 (prove_distinct_thms)
  17.542 -      dist_rewrites (DatatypeProp.make_distincts descr sorts);
  17.543 -
  17.544 -    (* prove injectivity of constructors *)
  17.545 -
  17.546 -    fun prove_constr_inj_thm rep_thms t =
  17.547 -      let val inj_thms = Scons_inject :: (map make_elim
  17.548 -        (iso_inj_thms @
  17.549 -          [In0_inject, In1_inject, Leaf_inject, Inl_inject, Inr_inject,
  17.550 -           Lim_inject, Suml_inject, Sumr_inject]))
  17.551 -      in Skip_Proof.prove_global thy5 [] [] t (fn _ => EVERY
  17.552 -        [rtac iffI 1,
  17.553 -         REPEAT (etac conjE 2), hyp_subst_tac 2, rtac refl 2,
  17.554 -         dresolve_tac rep_congs 1, dtac box_equals 1,
  17.555 -         REPEAT (resolve_tac rep_thms 1),
  17.556 -         REPEAT (eresolve_tac inj_thms 1),
  17.557 -         REPEAT (ares_tac [conjI] 1 ORELSE (EVERY [REPEAT (rtac ext 1),
  17.558 -           REPEAT (eresolve_tac (make_elim fun_cong :: inj_thms) 1),
  17.559 -           atac 1]))])
  17.560 -      end;
  17.561 -
  17.562 -    val constr_inject = map (fn (ts, thms) => map (prove_constr_inj_thm thms) ts)
  17.563 -      ((DatatypeProp.make_injs descr sorts) ~~ constr_rep_thms);
  17.564 -
  17.565 -    val ((constr_inject', distinct_thms'), thy6) =
  17.566 -      thy5
  17.567 -      |> Sign.parent_path
  17.568 -      |> store_thmss "inject" new_type_names constr_inject
  17.569 -      ||>> store_thmss "distinct" new_type_names distinct_thms;
  17.570 -
  17.571 -    (*************************** induction theorem ****************************)
  17.572 -
  17.573 -    val _ = message config "Proving induction rule for datatypes ...";
  17.574 -
  17.575 -    val Rep_inverse_thms = (map (fn (_, iso, _) => iso RS subst) newT_iso_axms) @
  17.576 -      (map (fn r => r RS @{thm the_inv_f_f} RS subst) iso_inj_thms_unfolded);
  17.577 -    val Rep_inverse_thms' = map (fn r => r RS @{thm the_inv_f_f}) iso_inj_thms_unfolded;
  17.578 -
  17.579 -    fun mk_indrule_lemma ((i, _), T) (prems, concls) =
  17.580 -      let
  17.581 -        val Rep_t = Const (nth all_rep_names i, T --> Univ_elT) $
  17.582 -          mk_Free "x" T i;
  17.583 -
  17.584 -        val Abs_t = if i < length newTs then
  17.585 -            Const (Sign.intern_const thy6
  17.586 -              ("Abs_" ^ (nth new_type_names i)), Univ_elT --> T)
  17.587 -          else Const (@{const_name the_inv_into},
  17.588 -              [HOLogic.mk_setT T, T --> Univ_elT, Univ_elT] ---> T) $
  17.589 -            HOLogic.mk_UNIV T $ Const (nth all_rep_names i, T --> Univ_elT)
  17.590 -
  17.591 -      in (prems @ [HOLogic.imp $
  17.592 -            (Const (nth rep_set_names i, UnivT') $ Rep_t) $
  17.593 -              (mk_Free "P" (T --> HOLogic.boolT) (i + 1) $ (Abs_t $ Rep_t))],
  17.594 -          concls @ [mk_Free "P" (T --> HOLogic.boolT) (i + 1) $ mk_Free "x" T i])
  17.595 -      end;
  17.596 -
  17.597 -    val (indrule_lemma_prems, indrule_lemma_concls) =
  17.598 -      fold mk_indrule_lemma (descr' ~~ recTs) ([], []);
  17.599 -
  17.600 -    val cert = cterm_of thy6;
  17.601 -
  17.602 -    val indrule_lemma = Skip_Proof.prove_global thy6 [] []
  17.603 -      (Logic.mk_implies
  17.604 -        (HOLogic.mk_Trueprop (mk_conj indrule_lemma_prems),
  17.605 -         HOLogic.mk_Trueprop (mk_conj indrule_lemma_concls))) (fn _ => EVERY
  17.606 -           [REPEAT (etac conjE 1),
  17.607 -            REPEAT (EVERY
  17.608 -              [TRY (rtac conjI 1), resolve_tac Rep_inverse_thms 1,
  17.609 -               etac mp 1, resolve_tac iso_elem_thms 1])]);
  17.610 -
  17.611 -    val Ps = map head_of (HOLogic.dest_conj (HOLogic.dest_Trueprop (concl_of indrule_lemma)));
  17.612 -    val frees = if length Ps = 1 then [Free ("P", snd (dest_Var (hd Ps)))] else
  17.613 -      map (Free o apfst fst o dest_Var) Ps;
  17.614 -    val indrule_lemma' = cterm_instantiate (map cert Ps ~~ map cert frees) indrule_lemma;
  17.615 -
  17.616 -    val dt_induct_prop = DatatypeProp.make_ind descr sorts;
  17.617 -    val dt_induct = Skip_Proof.prove_global thy6 []
  17.618 -      (Logic.strip_imp_prems dt_induct_prop) (Logic.strip_imp_concl dt_induct_prop)
  17.619 -      (fn {prems, ...} => EVERY
  17.620 -        [rtac indrule_lemma' 1,
  17.621 -         (indtac rep_induct [] THEN_ALL_NEW ObjectLogic.atomize_prems_tac) 1,
  17.622 -         EVERY (map (fn (prem, r) => (EVERY
  17.623 -           [REPEAT (eresolve_tac Abs_inverse_thms 1),
  17.624 -            simp_tac (HOL_basic_ss addsimps ((symmetric r)::Rep_inverse_thms')) 1,
  17.625 -            DEPTH_SOLVE_1 (ares_tac [prem] 1 ORELSE etac allE 1)]))
  17.626 -                (prems ~~ (constr_defs @ (map mk_meta_eq iso_char_thms))))]);
  17.627 -
  17.628 -    val ([dt_induct'], thy7) =
  17.629 -      thy6
  17.630 -      |> Sign.add_path big_name
  17.631 -      |> PureThy.add_thms [((Binding.name "induct", dt_induct), [case_names_induct])]
  17.632 -      ||> Sign.parent_path
  17.633 -      ||> Theory.checkpoint;
  17.634 -
  17.635 -  in
  17.636 -    ((constr_inject', distinct_thms', dt_induct'), thy7)
  17.637 -  end;
  17.638 -
  17.639 -
  17.640 -
  17.641 -(** definitional introduction of datatypes **)
  17.642 -
  17.643 -fun gen_add_datatype prep_typ config new_type_names dts thy =
  17.644 -  let
  17.645 -    val _ = Theory.requires thy "Datatype" "datatype definitions";
  17.646 -
  17.647 -    (* this theory is used just for parsing *)
  17.648 -    val tmp_thy = thy |>
  17.649 -      Theory.copy |>
  17.650 -      Sign.add_types (map (fn (tvs, tname, mx, _) =>
  17.651 -        (tname, length tvs, mx)) dts);
  17.652 -
  17.653 -    val (tyvars, _, _, _)::_ = dts;
  17.654 -    val (new_dts, types_syntax) = ListPair.unzip (map (fn (tvs, tname, mx, _) =>
  17.655 -      let val full_tname = Sign.full_name tmp_thy (Binding.map_name (Syntax.type_name mx) tname)
  17.656 -      in
  17.657 -        (case duplicates (op =) tvs of
  17.658 -          [] =>
  17.659 -            if eq_set (op =) (tyvars, tvs) then ((full_tname, tvs), (tname, mx))
  17.660 -            else error ("Mutually recursive datatypes must have same type parameters")
  17.661 -        | dups => error ("Duplicate parameter(s) for datatype " ^ quote (Binding.str_of tname) ^
  17.662 -            " : " ^ commas dups))
  17.663 -      end) dts);
  17.664 -    val dt_names = map fst new_dts;
  17.665 -
  17.666 -    val _ =
  17.667 -      (case duplicates (op =) (map fst new_dts) @ duplicates (op =) new_type_names of
  17.668 -        [] => ()
  17.669 -      | dups => error ("Duplicate datatypes: " ^ commas dups));
  17.670 -
  17.671 -    fun prep_dt_spec (tvs, tname, mx, constrs) tname' (dts', constr_syntax, sorts, i) =
  17.672 -      let
  17.673 -        fun prep_constr (cname, cargs, mx') (constrs, constr_syntax', sorts') =
  17.674 -          let
  17.675 -            val (cargs', sorts'') = fold_map (prep_typ tmp_thy) cargs sorts';
  17.676 -            val _ =
  17.677 -              (case subtract (op =) tvs (fold (curry OldTerm.add_typ_tfree_names) cargs' []) of
  17.678 -                [] => ()
  17.679 -              | vs => error ("Extra type variables on rhs: " ^ commas vs))
  17.680 -          in (constrs @ [(Sign.full_name_path tmp_thy tname'
  17.681 -                  (Binding.map_name (Syntax.const_name mx') cname),
  17.682 -                   map (dtyp_of_typ new_dts) cargs')],
  17.683 -              constr_syntax' @ [(cname, mx')], sorts'')
  17.684 -          end handle ERROR msg => cat_error msg
  17.685 -           ("The error above occured in constructor " ^ quote (Binding.str_of cname) ^
  17.686 -            " of datatype " ^ quote (Binding.str_of tname));
  17.687 -
  17.688 -        val (constrs', constr_syntax', sorts') =
  17.689 -          fold prep_constr constrs ([], [], sorts)
  17.690 -
  17.691 -      in
  17.692 -        case duplicates (op =) (map fst constrs') of
  17.693 -           [] =>
  17.694 -             (dts' @ [(i, (Sign.full_name tmp_thy (Binding.map_name (Syntax.type_name mx) tname),
  17.695 -                map DtTFree tvs, constrs'))],
  17.696 -              constr_syntax @ [constr_syntax'], sorts', i + 1)
  17.697 -         | dups => error ("Duplicate constructors " ^ commas dups ^
  17.698 -             " in datatype " ^ quote (Binding.str_of tname))
  17.699 -      end;
  17.700 -
  17.701 -    val (dts', constr_syntax, sorts', i) =
  17.702 -      fold2 prep_dt_spec dts new_type_names ([], [], [], 0);
  17.703 -    val sorts = sorts' @ map (rpair (Sign.defaultS tmp_thy)) (subtract (op =) (map fst sorts') tyvars);
  17.704 -    val dt_info = Datatype_Data.get_all thy;
  17.705 -    val (descr, _) = unfold_datatypes tmp_thy dts' sorts dt_info dts' i;
  17.706 -    val _ = check_nonempty descr handle (exn as Datatype_Empty s) =>
  17.707 -      if #strict config then error ("Nonemptiness check failed for datatype " ^ s)
  17.708 -      else raise exn;
  17.709 -
  17.710 -    val _ = message config ("Constructing datatype(s) " ^ commas_quote new_type_names);
  17.711 -
  17.712 -  in
  17.713 -    thy
  17.714 -    |> representation_proofs config dt_info new_type_names descr sorts
  17.715 -        types_syntax constr_syntax (Datatype_Data.mk_case_names_induct (flat descr))
  17.716 -    |-> (fn (inject, distinct, induct) => Datatype_Data.derive_datatype_props
  17.717 -        config dt_names (SOME new_type_names) descr sorts
  17.718 -        induct inject distinct)
  17.719 -  end;
  17.720 -
  17.721 -val add_datatype = gen_add_datatype Datatype_Data.cert_typ;
  17.722 -val datatype_cmd = snd ooo gen_add_datatype Datatype_Data.read_typ default_config;
  17.723 -
  17.724 -local
  17.725 -
  17.726 -structure P = OuterParse and K = OuterKeyword
  17.727 -
  17.728 -fun prep_datatype_decls args =
  17.729 -  let
  17.730 -    val names = map
  17.731 -      (fn ((((NONE, _), t), _), _) => Binding.name_of t | ((((SOME t, _), _), _), _) => t) args;
  17.732 -    val specs = map (fn ((((_, vs), t), mx), cons) =>
  17.733 -      (vs, t, mx, map (fn ((x, y), z) => (x, y, z)) cons)) args;
  17.734 -  in (names, specs) end;
  17.735 -
  17.736 -val parse_datatype_decl =
  17.737 -  (Scan.option (P.$$$ "(" |-- P.name --| P.$$$ ")") -- P.type_args -- P.binding -- P.opt_infix --
  17.738 -    (P.$$$ "=" |-- P.enum1 "|" (P.binding -- Scan.repeat P.typ -- P.opt_mixfix)));
  17.739 -
  17.740 -val parse_datatype_decls = P.and_list1 parse_datatype_decl >> prep_datatype_decls;
  17.741 -
  17.742 -in
  17.743 -
  17.744 -val _ =
  17.745 -  OuterSyntax.command "datatype" "define inductive datatypes" K.thy_decl
  17.746 -    (parse_datatype_decls >> (fn (names, specs) => Toplevel.theory (datatype_cmd names specs)));
  17.747 -
  17.748 -end;
  17.749 -
  17.750 -end;
    18.1 --- a/src/HOL/Tools/Function/size.ML	Mon Nov 30 11:42:48 2009 +0100
    18.2 +++ b/src/HOL/Tools/Function/size.ML	Mon Nov 30 11:42:49 2009 +0100
    18.3 @@ -13,7 +13,7 @@
    18.4  structure Size: SIZE =
    18.5  struct
    18.6  
    18.7 -open DatatypeAux;
    18.8 +open Datatype_Aux;
    18.9  
   18.10  structure SizeData = Theory_Data
   18.11  (
   18.12 @@ -177,7 +177,7 @@
   18.13      fun gen_mk_size_eq p size_of size_ofp size_const T (cname, cargs) =
   18.14        let
   18.15          val Ts = map (typ_of_dtyp descr sorts) cargs;
   18.16 -        val tnames = Name.variant_list f_names (DatatypeProp.make_tnames Ts);
   18.17 +        val tnames = Name.variant_list f_names (Datatype_Prop.make_tnames Ts);
   18.18          val ts = map_filter (fn (sT as (s, T), dt) =>
   18.19            Option.map (fn sz => sz $ Free sT)
   18.20              (if p dt then size_of_type size_of extra_size size_ofp T
    19.1 --- a/src/HOL/Tools/Nitpick/nitpick_hol.ML	Mon Nov 30 11:42:48 2009 +0100
    19.2 +++ b/src/HOL/Tools/Nitpick/nitpick_hol.ML	Mon Nov 30 11:42:49 2009 +0100
    19.3 @@ -516,7 +516,7 @@
    19.4    | NONE => NONE
    19.5  
    19.6  (* FIXME: use antiquotation for "code_numeral" below or detect "rep_datatype",
    19.7 -   e.g., by adding a field to "DatatypeAux.info". *)
    19.8 +   e.g., by adding a field to "Datatype_Aux.info". *)
    19.9  (* string -> bool *)
   19.10  fun is_basic_datatype s =
   19.11      s mem [@{type_name "*"}, @{type_name bool}, @{type_name unit},
    20.1 --- a/src/HOL/Tools/Predicate_Compile/predicate_compile_core.ML	Mon Nov 30 11:42:48 2009 +0100
    20.2 +++ b/src/HOL/Tools/Predicate_Compile/predicate_compile_core.ML	Mon Nov 30 11:42:49 2009 +0100
    20.3 @@ -1282,7 +1282,7 @@
    20.4      val v' = Free (name', T);
    20.5    in
    20.6      lambda v (fst (Datatype.make_case
    20.7 -      (ProofContext.init thy) DatatypeCase.Quiet [] v
    20.8 +      (ProofContext.init thy) Datatype_Case.Quiet [] v
    20.9        [(HOLogic.mk_tuple out_ts,
   20.10          if null eqs'' then success_t
   20.11          else Const (@{const_name HOL.If}, HOLogic.boolT --> U --> U --> U) $
    21.1 --- a/src/HOL/Tools/inductive_realizer.ML	Mon Nov 30 11:42:48 2009 +0100
    21.2 +++ b/src/HOL/Tools/inductive_realizer.ML	Mon Nov 30 11:42:49 2009 +0100
    21.3 @@ -243,7 +243,7 @@
    21.4        thy
    21.5        |> f (map snd dts)
    21.6        |-> (fn dtinfo => pair (map fst dts, SOME dtinfo))
    21.7 -    handle DatatypeAux.Datatype_Empty name' =>
    21.8 +    handle Datatype_Aux.Datatype_Empty name' =>
    21.9        let
   21.10          val name = Long_Name.base_name name';
   21.11          val dname = Name.variant used "Dummy";
   21.12 @@ -398,7 +398,7 @@
   21.13          val (thm', thy') = PureThy.store_thm (Binding.qualified_name (space_implode "_"
   21.14            (Long_Name.qualify qualifier "induct" :: vs' @ Ps @ ["correctness"])), thm) thy;
   21.15          val thms = map (fn th => zero_var_indexes (rotate_prems ~1 (th RS mp)))
   21.16 -          (DatatypeAux.split_conj_thm thm');
   21.17 +          (Datatype_Aux.split_conj_thm thm');
   21.18          val ([thms'], thy'') = PureThy.add_thmss
   21.19            [((Binding.qualified_name (space_implode "_"
   21.20               (Long_Name.qualify qualifier "inducts" :: vs' @ Ps @
    22.1 --- a/src/HOL/Tools/old_primrec.ML	Mon Nov 30 11:42:48 2009 +0100
    22.2 +++ b/src/HOL/Tools/old_primrec.ML	Mon Nov 30 11:42:49 2009 +0100
    22.3 @@ -21,7 +21,7 @@
    22.4  structure OldPrimrec : OLD_PRIMREC =
    22.5  struct
    22.6  
    22.7 -open DatatypeAux;
    22.8 +open Datatype_Aux;
    22.9  
   22.10  exception RecError of string;
   22.11  
    23.1 --- a/src/HOL/Tools/primrec.ML	Mon Nov 30 11:42:48 2009 +0100
    23.2 +++ b/src/HOL/Tools/primrec.ML	Mon Nov 30 11:42:49 2009 +0100
    23.3 @@ -23,7 +23,7 @@
    23.4  structure Primrec : PRIMREC =
    23.5  struct
    23.6  
    23.7 -open DatatypeAux;
    23.8 +open Datatype_Aux;
    23.9  
   23.10  exception PrimrecError of string * term option;
   23.11  
    24.1 --- a/src/HOL/Tools/quickcheck_generators.ML	Mon Nov 30 11:42:48 2009 +0100
    24.2 +++ b/src/HOL/Tools/quickcheck_generators.ML	Mon Nov 30 11:42:49 2009 +0100
    24.3 @@ -246,10 +246,10 @@
    24.4                mk_const @{const_name random_fun_lift} [fTs ---> T, fT] $
    24.5                  mk_random_fun_lift fTs t;
    24.6          val t = mk_random_fun_lift fTs (nth random_auxs k $ size_pred $ size');
    24.7 -        val size = Option.map snd (DatatypeCodegen.find_shortest_path descr k)
    24.8 +        val size = Option.map snd (Datatype_Aux.find_shortest_path descr k)
    24.9            |> the_default 0;
   24.10        in (SOME size, (t, fTs ---> T)) end;
   24.11 -    val tss = DatatypeAux.interpret_construction descr vs
   24.12 +    val tss = Datatype_Aux.interpret_construction descr vs
   24.13        { atyp = mk_random_call, dtyp = mk_random_aux_call };
   24.14      fun mk_consexpr simpleT (c, xs) =
   24.15        let
   24.16 @@ -287,9 +287,9 @@
   24.17  
   24.18  fun mk_random_datatype config descr vs tycos prfx (names, auxnames) (Ts, Us) thy =
   24.19    let
   24.20 -    val _ = DatatypeAux.message config "Creating quickcheck generators ...";
   24.21 +    val _ = Datatype_Aux.message config "Creating quickcheck generators ...";
   24.22      val mk_prop_eq = HOLogic.mk_Trueprop o HOLogic.mk_eq;
   24.23 -    fun mk_size_arg k = case DatatypeCodegen.find_shortest_path descr k
   24.24 +    fun mk_size_arg k = case Datatype_Aux.find_shortest_path descr k
   24.25       of SOME (_, l) => if l = 0 then size
   24.26            else @{term "max :: code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"}
   24.27              $ HOLogic.mk_number @{typ code_numeral} l $ size
   24.28 @@ -328,10 +328,10 @@
   24.29      val typerep_vs = (map o apsnd)
   24.30        (curry (Sorts.inter_sort algebra) @{sort typerep}) raw_vs;
   24.31      val random_insts = (map (rpair @{sort random}) o flat o maps snd o maps snd)
   24.32 -      (DatatypeAux.interpret_construction descr typerep_vs
   24.33 +      (Datatype_Aux.interpret_construction descr typerep_vs
   24.34          { atyp = single, dtyp = (K o K o K) [] });
   24.35      val term_of_insts = (map (rpair @{sort term_of}) o flat o maps snd o maps snd)
   24.36 -      (DatatypeAux.interpret_construction descr typerep_vs
   24.37 +      (Datatype_Aux.interpret_construction descr typerep_vs
   24.38          { atyp = K [], dtyp = K o K });
   24.39      val has_inst = exists (fn tyco =>
   24.40        can (Sorts.mg_domain algebra tyco) @{sort random}) tycos;
    25.1 --- a/src/HOL/Tools/refute.ML	Mon Nov 30 11:42:48 2009 +0100
    25.2 +++ b/src/HOL/Tools/refute.ML	Mon Nov 30 11:42:49 2009 +0100
    25.3 @@ -406,12 +406,12 @@
    25.4  (* TRANSLATION HOL -> PROPOSITIONAL LOGIC, BOOLEAN ASSIGNMENT -> MODEL       *)
    25.5  (* ------------------------------------------------------------------------- *)
    25.6  
    25.7 -  fun typ_of_dtyp descr typ_assoc (DatatypeAux.DtTFree a) =
    25.8 +  fun typ_of_dtyp descr typ_assoc (Datatype_Aux.DtTFree a) =
    25.9      (* replace a 'DtTFree' variable by the associated type *)
   25.10 -    the (AList.lookup (op =) typ_assoc (DatatypeAux.DtTFree a))
   25.11 -    | typ_of_dtyp descr typ_assoc (DatatypeAux.DtType (s, ds)) =
   25.12 +    the (AList.lookup (op =) typ_assoc (Datatype_Aux.DtTFree a))
   25.13 +    | typ_of_dtyp descr typ_assoc (Datatype_Aux.DtType (s, ds)) =
   25.14      Type (s, map (typ_of_dtyp descr typ_assoc) ds)
   25.15 -    | typ_of_dtyp descr typ_assoc (DatatypeAux.DtRec i) =
   25.16 +    | typ_of_dtyp descr typ_assoc (Datatype_Aux.DtRec i) =
   25.17      let
   25.18        val (s, ds, _) = the (AList.lookup (op =) descr i)
   25.19      in
   25.20 @@ -946,7 +946,7 @@
   25.21              (* sanity check: every element in 'dtyps' must be a *)
   25.22              (* 'DtTFree'                                        *)
   25.23              val _ = if Library.exists (fn d =>
   25.24 -              case d of DatatypeAux.DtTFree _ => false | _ => true) typs then
   25.25 +              case d of Datatype_Aux.DtTFree _ => false | _ => true) typs then
   25.26                raise REFUTE ("ground_types", "datatype argument (for type "
   25.27                  ^ Syntax.string_of_typ_global thy T ^ ") is not a variable")
   25.28              else ()
   25.29 @@ -958,11 +958,11 @@
   25.30                val dT = typ_of_dtyp descr typ_assoc d
   25.31              in
   25.32                case d of
   25.33 -                DatatypeAux.DtTFree _ =>
   25.34 +                Datatype_Aux.DtTFree _ =>
   25.35                  collect_types dT acc
   25.36 -              | DatatypeAux.DtType (_, ds) =>
   25.37 +              | Datatype_Aux.DtType (_, ds) =>
   25.38                  collect_types dT (fold_rev collect_dtyp ds acc)
   25.39 -              | DatatypeAux.DtRec i =>
   25.40 +              | Datatype_Aux.DtRec i =>
   25.41                  if dT mem acc then
   25.42                    acc  (* prevent infinite recursion *)
   25.43                  else
   25.44 @@ -971,7 +971,7 @@
   25.45                      (* if the current type is a recursive IDT (i.e. a depth *)
   25.46                      (* is required), add it to 'acc'                        *)
   25.47                      val acc_dT = if Library.exists (fn (_, ds) =>
   25.48 -                      Library.exists DatatypeAux.is_rec_type ds) dconstrs then
   25.49 +                      Library.exists Datatype_Aux.is_rec_type ds) dconstrs then
   25.50                          insert (op =) dT acc
   25.51                        else acc
   25.52                      (* collect argument types *)
   25.53 @@ -985,7 +985,7 @@
   25.54            in
   25.55              (* argument types 'Ts' could be added here, but they are also *)
   25.56              (* added by 'collect_dtyp' automatically                      *)
   25.57 -            collect_dtyp (DatatypeAux.DtRec index) acc
   25.58 +            collect_dtyp (Datatype_Aux.DtRec index) acc
   25.59            end
   25.60          | NONE =>
   25.61            (* not an inductive datatype, e.g. defined via "typedef" or *)
   25.62 @@ -1157,7 +1157,7 @@
   25.63              in
   25.64                (* recursive datatype? *)
   25.65                Library.exists (fn (_, ds) =>
   25.66 -                Library.exists DatatypeAux.is_rec_type ds) constrs
   25.67 +                Library.exists Datatype_Aux.is_rec_type ds) constrs
   25.68              end
   25.69            | NONE => false)
   25.70          | _ => false) types
   25.71 @@ -1952,7 +1952,7 @@
   25.72                val typ_assoc           = dtyps ~~ Ts
   25.73                (* sanity check: every element in 'dtyps' must be a 'DtTFree' *)
   25.74                val _ = if Library.exists (fn d =>
   25.75 -                  case d of DatatypeAux.DtTFree _ => false | _ => true) dtyps
   25.76 +                  case d of Datatype_Aux.DtTFree _ => false | _ => true) dtyps
   25.77                  then
   25.78                    raise REFUTE ("IDT_interpreter",
   25.79                      "datatype argument (for type "
   25.80 @@ -2076,7 +2076,7 @@
   25.81                val typ_assoc           = dtyps ~~ Ts
   25.82                (* sanity check: every element in 'dtyps' must be a 'DtTFree' *)
   25.83                val _ = if Library.exists (fn d =>
   25.84 -                  case d of DatatypeAux.DtTFree _ => false | _ => true) dtyps
   25.85 +                  case d of Datatype_Aux.DtTFree _ => false | _ => true) dtyps
   25.86                  then
   25.87                    raise REFUTE ("IDT_constructor_interpreter",
   25.88                      "datatype argument (for type "
   25.89 @@ -2135,7 +2135,7 @@
   25.90                val typ_assoc           = dtyps ~~ Ts'
   25.91                (* sanity check: every element in 'dtyps' must be a 'DtTFree' *)
   25.92                val _ = if Library.exists (fn d =>
   25.93 -                  case d of DatatypeAux.DtTFree _ => false | _ => true) dtyps
   25.94 +                  case d of Datatype_Aux.DtTFree _ => false | _ => true) dtyps
   25.95                  then
   25.96                    raise REFUTE ("IDT_constructor_interpreter",
   25.97                      "datatype argument (for type "
   25.98 @@ -2350,7 +2350,7 @@
   25.99                    (* sanity check: every element in 'dtyps' must be a *)
  25.100                    (*               'DtTFree'                          *)
  25.101                    val _ = if Library.exists (fn d =>
  25.102 -                    case d of DatatypeAux.DtTFree _ => false
  25.103 +                    case d of Datatype_Aux.DtTFree _ => false
  25.104                              | _ => true) dtyps
  25.105                      then
  25.106                        raise REFUTE ("IDT_recursion_interpreter",
  25.107 @@ -2372,10 +2372,10 @@
  25.108                      (case AList.lookup op= acc d of
  25.109                        NONE =>
  25.110                        (case d of
  25.111 -                        DatatypeAux.DtTFree _ =>
  25.112 +                        Datatype_Aux.DtTFree _ =>
  25.113                          (* add the association, proceed *)
  25.114                          rec_typ_assoc ((d, T)::acc) xs
  25.115 -                      | DatatypeAux.DtType (s, ds) =>
  25.116 +                      | Datatype_Aux.DtType (s, ds) =>
  25.117                          let
  25.118                            val (s', Ts) = dest_Type T
  25.119                          in
  25.120 @@ -2385,7 +2385,7 @@
  25.121                              raise REFUTE ("IDT_recursion_interpreter",
  25.122                                "DtType/Type mismatch")
  25.123                          end
  25.124 -                      | DatatypeAux.DtRec i =>
  25.125 +                      | Datatype_Aux.DtRec i =>
  25.126                          let
  25.127                            val (_, ds, _) = the (AList.lookup (op =) descr i)
  25.128                            val (_, Ts)    = dest_Type T
  25.129 @@ -2401,7 +2401,7 @@
  25.130                          raise REFUTE ("IDT_recursion_interpreter",
  25.131                            "different type associations for the same dtyp"))
  25.132                    val typ_assoc = filter
  25.133 -                    (fn (DatatypeAux.DtTFree _, _) => true | (_, _) => false)
  25.134 +                    (fn (Datatype_Aux.DtTFree _, _) => true | (_, _) => false)
  25.135                      (rec_typ_assoc []
  25.136                        (#2 (the (AList.lookup (op =) descr idt_index)) ~~ (snd o dest_Type) IDT))
  25.137                    (* sanity check: typ_assoc must associate types to the   *)
  25.138 @@ -2417,7 +2417,7 @@
  25.139                    val mc_intrs = map (fn (idx, (_, _, cs)) =>
  25.140                      let
  25.141                        val c_return_typ = typ_of_dtyp descr typ_assoc
  25.142 -                        (DatatypeAux.DtRec idx)
  25.143 +                        (Datatype_Aux.DtRec idx)
  25.144                      in
  25.145                        (idx, map (fn (cname, cargs) =>
  25.146                          (#1 o interpret thy (typs, []) {maxvars=0,
  25.147 @@ -2471,7 +2471,7 @@
  25.148                    val _ = map (fn (idx, intrs) =>
  25.149                      let
  25.150                        val T = typ_of_dtyp descr typ_assoc
  25.151 -                        (DatatypeAux.DtRec idx)
  25.152 +                        (Datatype_Aux.DtRec idx)
  25.153                      in
  25.154                        if length intrs <> size_of_type thy (typs, []) T then
  25.155                          raise REFUTE ("IDT_recursion_interpreter",
  25.156 @@ -2552,7 +2552,7 @@
  25.157                          val (_, _, constrs) = the (AList.lookup (op =) descr idx)
  25.158                          val (_, dtyps)      = List.nth (constrs, c)
  25.159                          val rec_dtyps_args  = filter
  25.160 -                          (DatatypeAux.is_rec_type o fst) (dtyps ~~ args)
  25.161 +                          (Datatype_Aux.is_rec_type o fst) (dtyps ~~ args)
  25.162                          (* map those indices to interpretations *)
  25.163                          val rec_dtyps_intrs = map (fn (dtyp, arg) =>
  25.164                            let
  25.165 @@ -2565,18 +2565,18 @@
  25.166                          (* takes the dtyp and interpretation of an element, *)
  25.167                          (* and computes the interpretation for the          *)
  25.168                          (* corresponding recursive argument                 *)
  25.169 -                        fun rec_intr (DatatypeAux.DtRec i) (Leaf xs) =
  25.170 +                        fun rec_intr (Datatype_Aux.DtRec i) (Leaf xs) =
  25.171                            (* recursive argument is "rec_i params elem" *)
  25.172                            compute_array_entry i (find_index (fn x => x = True) xs)
  25.173 -                          | rec_intr (DatatypeAux.DtRec _) (Node _) =
  25.174 +                          | rec_intr (Datatype_Aux.DtRec _) (Node _) =
  25.175                            raise REFUTE ("IDT_recursion_interpreter",
  25.176                              "interpretation for IDT is a node")
  25.177 -                          | rec_intr (DatatypeAux.DtType ("fun", [dt1, dt2]))
  25.178 +                          | rec_intr (Datatype_Aux.DtType ("fun", [dt1, dt2]))
  25.179                              (Node xs) =
  25.180                            (* recursive argument is something like     *)
  25.181                            (* "\<lambda>x::dt1. rec_? params (elem x)" *)
  25.182                            Node (map (rec_intr dt2) xs)
  25.183 -                          | rec_intr (DatatypeAux.DtType ("fun", [_, _]))
  25.184 +                          | rec_intr (Datatype_Aux.DtType ("fun", [_, _]))
  25.185                              (Leaf _) =
  25.186                            raise REFUTE ("IDT_recursion_interpreter",
  25.187                              "interpretation for function dtyp is a leaf")
  25.188 @@ -3169,7 +3169,7 @@
  25.189            val typ_assoc           = dtyps ~~ Ts
  25.190            (* sanity check: every element in 'dtyps' must be a 'DtTFree' *)
  25.191            val _ = if Library.exists (fn d =>
  25.192 -              case d of DatatypeAux.DtTFree _ => false | _ => true) dtyps
  25.193 +              case d of Datatype_Aux.DtTFree _ => false | _ => true) dtyps
  25.194              then
  25.195                raise REFUTE ("IDT_printer", "datatype argument (for type " ^
  25.196                  Syntax.string_of_typ_global thy (Type (s, Ts)) ^ ") is not a variable")