separate rep_datatype.ML;
authorwenzelm
Thu Dec 15 17:37:14 2011 +0100 (2011-12-15)
changeset 458905f70aaecae26
parent 45889 4ff377493dbb
child 45891 d73605c829cc
separate rep_datatype.ML;
tuned signature;
src/HOL/Inductive.thy
src/HOL/IsaMakefile
src/HOL/Nominal/nominal_datatype.ML
src/HOL/Tools/Datatype/datatype.ML
src/HOL/Tools/Datatype/datatype_data.ML
src/HOL/Tools/Datatype/rep_datatype.ML
     1.1 --- a/src/HOL/Inductive.thy	Thu Dec 15 14:11:57 2011 +0100
     1.2 +++ b/src/HOL/Inductive.thy	Thu Dec 15 17:37:14 2011 +0100
     1.3 @@ -14,6 +14,7 @@
     1.4    "Tools/Datatype/datatype_case.ML"
     1.5    ("Tools/Datatype/datatype_abs_proofs.ML")
     1.6    ("Tools/Datatype/datatype_data.ML")
     1.7 +  ("Tools/Datatype/rep_datatype.ML")
     1.8    ("Tools/primrec.ML")
     1.9    ("Tools/Datatype/datatype_codegen.ML")
    1.10  begin
    1.11 @@ -279,6 +280,8 @@
    1.12  use "Tools/Datatype/datatype_data.ML"
    1.13  setup Datatype_Data.setup
    1.14  
    1.15 +use "Tools/Datatype/rep_datatype.ML"
    1.16 +
    1.17  use "Tools/Datatype/datatype_codegen.ML"
    1.18  setup Datatype_Codegen.setup
    1.19  
     2.1 --- a/src/HOL/IsaMakefile	Thu Dec 15 14:11:57 2011 +0100
     2.2 +++ b/src/HOL/IsaMakefile	Thu Dec 15 17:37:14 2011 +0100
     2.3 @@ -218,6 +218,7 @@
     2.4    Tools/Datatype/datatype_data.ML \
     2.5    Tools/Datatype/datatype_prop.ML \
     2.6    Tools/Datatype/datatype_realizer.ML \
     2.7 +  Tools/Datatype/rep_datatype.ML \
     2.8    Tools/Function/context_tree.ML \
     2.9    Tools/Function/fun.ML \
    2.10    Tools/Function/function.ML \
    2.11 @@ -247,6 +248,7 @@
    2.12    Tools/arith_data.ML \
    2.13    Tools/cnf_funcs.ML \
    2.14    Tools/dseq.ML \
    2.15 +  Tools/enriched_type.ML \
    2.16    Tools/inductive.ML \
    2.17    Tools/inductive_realizer.ML \
    2.18    Tools/inductive_set.ML \
    2.19 @@ -263,7 +265,6 @@
    2.20    Tools/split_rule.ML \
    2.21    Tools/try_methods.ML \
    2.22    Tools/typedef.ML \
    2.23 -  Tools/enriched_type.ML \
    2.24    Transitive_Closure.thy \
    2.25    Typedef.thy \
    2.26    Wellfounded.thy
     3.1 --- a/src/HOL/Nominal/nominal_datatype.ML	Thu Dec 15 14:11:57 2011 +0100
     3.2 +++ b/src/HOL/Nominal/nominal_datatype.ML	Thu Dec 15 17:37:14 2011 +0100
     3.3 @@ -38,35 +38,6 @@
     3.4  
     3.5  open NominalAtoms;
     3.6  
     3.7 -(** FIXME: Datatype should export this function **)
     3.8 -
     3.9 -local
    3.10 -
    3.11 -fun dt_recs (Datatype_Aux.DtTFree _) = []
    3.12 -  | dt_recs (Datatype_Aux.DtType (_, dts)) = maps dt_recs dts
    3.13 -  | dt_recs (Datatype_Aux.DtRec i) = [i];
    3.14 -
    3.15 -fun dt_cases (descr: Datatype_Aux.descr) (_, args, constrs) =
    3.16 -  let
    3.17 -    fun the_bname i = Long_Name.base_name (#1 (the (AList.lookup (op =) descr i)));
    3.18 -    val bnames = map the_bname (distinct op = (maps dt_recs args));
    3.19 -  in map (fn (c, _) => space_implode "_" (Long_Name.base_name c :: bnames)) constrs end;
    3.20 -
    3.21 -
    3.22 -fun induct_cases descr =
    3.23 -  Datatype_Prop.indexify_names (maps (dt_cases descr) (map #2 descr));
    3.24 -
    3.25 -fun exhaust_cases descr i = dt_cases descr (the (AList.lookup (op =) descr i));
    3.26 -
    3.27 -in
    3.28 -
    3.29 -fun mk_case_names_induct descr = Rule_Cases.case_names (induct_cases descr);
    3.30 -
    3.31 -fun mk_case_names_exhausts descr new =
    3.32 -  map (Rule_Cases.case_names o exhaust_cases descr o #1)
    3.33 -    (filter (fn ((_, (name, _, _))) => member (op =) new name) descr);
    3.34 -
    3.35 -end;
    3.36  
    3.37  (* theory data *)
    3.38  
    3.39 @@ -1074,7 +1045,7 @@
    3.40              DEPTH_SOLVE_1 (ares_tac [prem] 1 ORELSE etac allE 1)]))
    3.41                  (prems ~~ constr_defs))]);
    3.42  
    3.43 -    val case_names_induct = mk_case_names_induct descr'';
    3.44 +    val case_names_induct = Datatype_Data.mk_case_names_induct descr'';
    3.45  
    3.46      (**** prove that new datatypes have finite support ****)
    3.47  
     4.1 --- a/src/HOL/Tools/Datatype/datatype.ML	Thu Dec 15 14:11:57 2011 +0100
     4.2 +++ b/src/HOL/Tools/Datatype/datatype.ML	Thu Dec 15 17:37:14 2011 +0100
     4.3 @@ -779,7 +779,7 @@
     4.4      |> representation_proofs config dt_info descr types_syntax constr_syntax
     4.5        (Datatype_Data.mk_case_names_induct (flat descr))
     4.6      |-> (fn (inject, distinct, induct) =>
     4.7 -      Datatype_Data.derive_datatype_props config dt_names descr induct inject distinct)
     4.8 +      Rep_Datatype.derive_datatype_props config dt_names descr induct inject distinct)
     4.9    end;
    4.10  
    4.11  val add_datatype = gen_add_datatype check_specs;
     5.1 --- a/src/HOL/Tools/Datatype/datatype_data.ML	Thu Dec 15 14:11:57 2011 +0100
     5.2 +++ b/src/HOL/Tools/Datatype/datatype_data.ML	Thu Dec 15 17:37:14 2011 +0100
     5.3 @@ -1,35 +1,33 @@
     5.4  (*  Title:      HOL/Tools/Datatype/datatype_data.ML
     5.5      Author:     Stefan Berghofer, TU Muenchen
     5.6  
     5.7 -Datatype package: bookkeeping; interpretation of existing types as datatypes.
     5.8 +Datatype package bookkeeping.
     5.9  *)
    5.10  
    5.11  signature DATATYPE_DATA =
    5.12  sig
    5.13    include DATATYPE_COMMON
    5.14 -  val derive_datatype_props : config -> string list -> descr list ->
    5.15 -    thm -> thm list list -> thm list list -> theory -> string list * theory
    5.16 -  val rep_datatype : config -> (string list -> Proof.context -> Proof.context) ->
    5.17 -    term list -> theory -> Proof.state
    5.18 -  val rep_datatype_cmd : config -> (string list -> Proof.context -> Proof.context) ->
    5.19 -    string list -> theory -> Proof.state
    5.20 +  val get_all : theory -> info Symtab.table
    5.21    val get_info : theory -> string -> info option
    5.22    val the_info : theory -> string -> info
    5.23 +  val info_of_constr : theory -> string * typ -> info option
    5.24 +  val info_of_constr_permissive : theory -> string * typ -> info option
    5.25 +  val info_of_case : theory -> string -> info option
    5.26 +  val register: (string * info) list -> theory -> theory
    5.27 +  val the_spec : theory -> string -> (string * sort) list * (string * typ list) list
    5.28    val the_descr : theory -> string list ->
    5.29      descr * (string * sort) list * string list * string *
    5.30      (string list * string list) * (typ list * typ list)
    5.31 -  val the_spec : theory -> string -> (string * sort) list * (string * typ list) list
    5.32    val all_distincts : theory -> typ list -> thm list list
    5.33    val get_constrs : theory -> string -> (string * typ) list option
    5.34 -  val get_all : theory -> info Symtab.table
    5.35 -  val info_of_constr : theory -> string * typ -> info option
    5.36 -  val info_of_constr_permissive : theory -> string * typ -> info option
    5.37 -  val info_of_case : theory -> string -> info option
    5.38 +  val mk_case_names_induct: descr -> attribute
    5.39 +  val mk_case_names_exhausts: descr -> string list -> attribute list
    5.40    val interpretation : (config -> string list -> theory -> theory) -> theory -> theory
    5.41 +  val interpretation_data : config * string list -> theory -> theory
    5.42    val make_case :  Proof.context -> Datatype_Case.config -> string list -> term ->
    5.43      (term * term) list -> term
    5.44    val strip_case : Proof.context -> bool -> term -> (term * (term * term) list) option
    5.45 -  val mk_case_names_induct: descr -> attribute
    5.46 +  val add_case_tr' : string list -> theory -> theory
    5.47    val setup: theory -> theory
    5.48  end;
    5.49  
    5.50 @@ -40,7 +38,7 @@
    5.51  
    5.52  (* data management *)
    5.53  
    5.54 -structure DatatypesData = Theory_Data
    5.55 +structure Data = Theory_Data
    5.56  (
    5.57    type T =
    5.58      {types: Datatype_Aux.info Symtab.table,
    5.59 @@ -58,7 +56,7 @@
    5.60       cases = Symtab.merge (K true) (cases1, cases2)};
    5.61  );
    5.62  
    5.63 -val get_all = #types o DatatypesData.get;
    5.64 +val get_all = #types o Data.get;
    5.65  val get_info = Symtab.lookup o get_all;
    5.66  
    5.67  fun the_info thy name =
    5.68 @@ -68,7 +66,7 @@
    5.69  
    5.70  fun info_of_constr thy (c, T) =
    5.71    let
    5.72 -    val tab = Symtab.lookup_list (#constrs (DatatypesData.get thy)) c;
    5.73 +    val tab = Symtab.lookup_list (#constrs (Data.get thy)) c;
    5.74    in
    5.75      (case body_type T of
    5.76        Type (tyco, _) => AList.lookup (op =) tab tyco
    5.77 @@ -77,7 +75,7 @@
    5.78  
    5.79  fun info_of_constr_permissive thy (c, T) =
    5.80    let
    5.81 -    val tab = Symtab.lookup_list (#constrs (DatatypesData.get thy)) c;
    5.82 +    val tab = Symtab.lookup_list (#constrs (Data.get thy)) c;
    5.83      val hint = (case body_type T of Type (tyco, _) => SOME tyco | _ => NONE);
    5.84      val default = if null tab then NONE else SOME (snd (List.last tab));
    5.85      (*conservative wrt. overloaded constructors*)
    5.86 @@ -90,10 +88,10 @@
    5.87          | SOME info => SOME info))
    5.88    end;
    5.89  
    5.90 -val info_of_case = Symtab.lookup o #cases o DatatypesData.get;
    5.91 +val info_of_case = Symtab.lookup o #cases o Data.get;
    5.92  
    5.93  fun register (dt_infos : (string * Datatype_Aux.info) list) =
    5.94 -  DatatypesData.map (fn {types, constrs, cases} =>
    5.95 +  Data.map (fn {types, constrs, cases} =>
    5.96      {types = types |> fold Symtab.update dt_infos,
    5.97       constrs = constrs |> fold (fn (constr, dtname_info) =>
    5.98           Symtab.map_default (constr, []) (cons dtname_info))
    5.99 @@ -257,205 +255,17 @@
   5.100    val eq: T * T -> bool = eq_snd (op =);
   5.101  );
   5.102  fun interpretation f = Datatype_Interpretation.interpretation (uncurry f);
   5.103 -
   5.104 -fun make_dt_info descr induct inducts rec_names rec_rewrites
   5.105 -    (index, (((((((((((_, (tname, _, _))), inject), distinct),
   5.106 -      exhaust), nchotomy), case_name), case_rewrites), case_cong), weak_case_cong),
   5.107 -        (split, split_asm))) =
   5.108 -  (tname,
   5.109 -   {index = index,
   5.110 -    descr = descr,
   5.111 -    inject = inject,
   5.112 -    distinct = distinct,
   5.113 -    induct = induct,
   5.114 -    inducts = inducts,
   5.115 -    exhaust = exhaust,
   5.116 -    nchotomy = nchotomy,
   5.117 -    rec_names = rec_names,
   5.118 -    rec_rewrites = rec_rewrites,
   5.119 -    case_name = case_name,
   5.120 -    case_rewrites = case_rewrites,
   5.121 -    case_cong = case_cong,
   5.122 -    weak_case_cong = weak_case_cong,
   5.123 -    split = split,
   5.124 -    split_asm = split_asm});
   5.125 -
   5.126 -fun derive_datatype_props config dt_names descr induct inject distinct thy1 =
   5.127 -  let
   5.128 -    val thy2 = thy1 |> Theory.checkpoint;
   5.129 -    val flat_descr = flat descr;
   5.130 -    val new_type_names = map Long_Name.base_name dt_names;
   5.131 -    val _ =
   5.132 -      Datatype_Aux.message config
   5.133 -        ("Deriving properties for datatype(s) " ^ commas_quote new_type_names);
   5.134 -
   5.135 -    val (exhaust, thy3) = thy2
   5.136 -      |> Datatype_Abs_Proofs.prove_casedist_thms config new_type_names
   5.137 -        descr induct (mk_case_names_exhausts flat_descr dt_names);
   5.138 -    val (nchotomys, thy4) = thy3
   5.139 -      |> Datatype_Abs_Proofs.prove_nchotomys config new_type_names descr exhaust;
   5.140 -    val ((rec_names, rec_rewrites), thy5) = thy4
   5.141 -      |> Datatype_Abs_Proofs.prove_primrec_thms
   5.142 -        config new_type_names descr (#inject o the o Symtab.lookup (get_all thy4))
   5.143 -        inject (distinct, all_distincts thy2 (Datatype_Aux.get_rec_types flat_descr)) induct;
   5.144 -    val ((case_rewrites, case_names), thy6) = thy5
   5.145 -      |> Datatype_Abs_Proofs.prove_case_thms config new_type_names descr rec_names rec_rewrites;
   5.146 -    val (case_congs, thy7) = thy6
   5.147 -      |> Datatype_Abs_Proofs.prove_case_congs new_type_names case_names descr
   5.148 -        nchotomys case_rewrites;
   5.149 -    val (weak_case_congs, thy8) = thy7
   5.150 -      |> Datatype_Abs_Proofs.prove_weak_case_congs new_type_names case_names descr;
   5.151 -    val (splits, thy9) = thy8
   5.152 -      |> Datatype_Abs_Proofs.prove_split_thms
   5.153 -        config new_type_names case_names descr inject distinct exhaust case_rewrites;
   5.154 -
   5.155 -    val inducts = Project_Rule.projections (Proof_Context.init_global thy2) induct;
   5.156 -    val dt_infos = map_index
   5.157 -      (make_dt_info flat_descr induct inducts rec_names rec_rewrites)
   5.158 -      (hd descr ~~ inject ~~ distinct ~~ exhaust ~~ nchotomys ~~
   5.159 -        case_names ~~ case_rewrites ~~ case_congs ~~ weak_case_congs ~~ splits);
   5.160 -    val dt_names = map fst dt_infos;
   5.161 -    val prfx = Binding.qualify true (space_implode "_" new_type_names);
   5.162 -    val simps = flat (inject @ distinct @ case_rewrites) @ rec_rewrites;
   5.163 -    val named_rules = flat (map_index (fn (index, tname) =>
   5.164 -      [((Binding.empty, [nth inducts index]), [Induct.induct_type tname]),
   5.165 -       ((Binding.empty, [nth exhaust index]), [Induct.cases_type tname])]) dt_names);
   5.166 -    val unnamed_rules = map (fn induct =>
   5.167 -      ((Binding.empty, [induct]), [Rule_Cases.inner_rule, Induct.induct_type ""]))
   5.168 -        (drop (length dt_names) inducts);
   5.169 -  in
   5.170 -    thy9
   5.171 -    |> Global_Theory.add_thmss ([((prfx (Binding.name "simps"), simps), []),
   5.172 -        ((prfx (Binding.name "inducts"), inducts), []),
   5.173 -        ((prfx (Binding.name "splits"), maps (fn (x, y) => [x, y]) splits), []),
   5.174 -        ((Binding.empty, flat case_rewrites @ flat distinct @ rec_rewrites),
   5.175 -          [Simplifier.simp_add]),
   5.176 -        ((Binding.empty, rec_rewrites), [Code.add_default_eqn_attribute]),
   5.177 -        ((Binding.empty, flat inject), [iff_add]),
   5.178 -        ((Binding.empty, map (fn th => th RS notE) (flat distinct)),
   5.179 -          [Classical.safe_elim NONE]),
   5.180 -        ((Binding.empty, weak_case_congs), [Simplifier.cong_add]),
   5.181 -        ((Binding.empty, flat (distinct @ inject)), [Induct.induct_simp_add])] @
   5.182 -          named_rules @ unnamed_rules)
   5.183 -    |> snd
   5.184 -    |> add_case_tr' case_names
   5.185 -    |> register dt_infos
   5.186 -    |> Datatype_Interpretation.data (config, dt_names)
   5.187 -    |> pair dt_names
   5.188 -  end;
   5.189 +val interpretation_data = Datatype_Interpretation.data;
   5.190  
   5.191  
   5.192  
   5.193 -(** declare existing type as datatype **)
   5.194 -
   5.195 -fun prove_rep_datatype config dt_names descr raw_inject half_distinct raw_induct thy1 =
   5.196 -  let
   5.197 -    val raw_distinct = (map o maps) (fn thm => [thm, thm RS not_sym]) half_distinct;
   5.198 -    val new_type_names = map Long_Name.base_name dt_names;
   5.199 -    val prfx = Binding.qualify true (space_implode "_" new_type_names);
   5.200 -    val (((inject, distinct), [induct]), thy2) =
   5.201 -      thy1
   5.202 -      |> Datatype_Aux.store_thmss "inject" new_type_names raw_inject
   5.203 -      ||>> Datatype_Aux.store_thmss "distinct" new_type_names raw_distinct
   5.204 -      ||>> Global_Theory.add_thms
   5.205 -        [((prfx (Binding.name "induct"), raw_induct),
   5.206 -          [mk_case_names_induct descr])];
   5.207 -  in
   5.208 -    thy2
   5.209 -    |> derive_datatype_props config dt_names [descr] induct inject distinct
   5.210 - end;
   5.211 -
   5.212 -fun gen_rep_datatype prep_term config after_qed raw_ts thy =
   5.213 -  let
   5.214 -    val ctxt = Proof_Context.init_global thy;
   5.215 -
   5.216 -    fun constr_of_term (Const (c, T)) = (c, T)
   5.217 -      | constr_of_term t = error ("Not a constant: " ^ Syntax.string_of_term ctxt t);
   5.218 -    fun no_constr (c, T) =
   5.219 -      error ("Bad constructor: " ^ Proof_Context.extern_const ctxt c ^ "::" ^
   5.220 -        Syntax.string_of_typ ctxt T);
   5.221 -    fun type_of_constr (cT as (_, T)) =
   5.222 -      let
   5.223 -        val frees = Term.add_tfreesT T [];
   5.224 -        val (tyco, vs) = (apsnd o map) dest_TFree (dest_Type (body_type T))
   5.225 -          handle TYPE _ => no_constr cT
   5.226 -        val _ = if has_duplicates (eq_fst (op =)) vs then no_constr cT else ();
   5.227 -        val _ = if length frees <> length vs then no_constr cT else ();
   5.228 -      in (tyco, (vs, cT)) end;
   5.229 -
   5.230 -    val raw_cs =
   5.231 -      AList.group (op =) (map (type_of_constr o constr_of_term o prep_term thy) raw_ts);
   5.232 -    val _ =
   5.233 -      (case map_filter (fn (tyco, _) =>
   5.234 -          if Symtab.defined (get_all thy) tyco then SOME tyco else NONE) raw_cs of
   5.235 -        [] => ()
   5.236 -      | tycos => error ("Type(s) " ^ commas_quote tycos ^ " already represented inductivly"));
   5.237 -    val raw_vss = maps (map (map snd o fst) o snd) raw_cs;
   5.238 -    val ms =
   5.239 -      (case distinct (op =) (map length raw_vss) of
   5.240 -         [n] => 0 upto n - 1
   5.241 -      | _ => error "Different types in given constructors");
   5.242 -    fun inter_sort m =
   5.243 -      map (fn xs => nth xs m) raw_vss
   5.244 -      |> foldr1 (Sorts.inter_sort (Sign.classes_of thy));
   5.245 -    val sorts = map inter_sort ms;
   5.246 -    val vs = Name.invent_names Name.context Name.aT sorts;
   5.247 -
   5.248 -    fun norm_constr (raw_vs, (c, T)) =
   5.249 -      (c, map_atyps
   5.250 -        (TFree o (the o AList.lookup (op =) (map fst raw_vs ~~ vs)) o fst o dest_TFree) T);
   5.251 -
   5.252 -    val cs = map (apsnd (map norm_constr)) raw_cs;
   5.253 -    val dtyps_of_typ = map (Datatype_Aux.dtyp_of_typ (map (rpair vs o fst) cs)) o binder_types;
   5.254 -    val dt_names = map fst cs;
   5.255 -
   5.256 -    fun mk_spec (i, (tyco, constr)) =
   5.257 -      (i, (tyco, map Datatype_Aux.DtTFree vs, (map o apsnd) dtyps_of_typ constr));
   5.258 -    val descr = map_index mk_spec cs;
   5.259 -    val injs = Datatype_Prop.make_injs [descr];
   5.260 -    val half_distincts = Datatype_Prop.make_distincts [descr];
   5.261 -    val ind = Datatype_Prop.make_ind [descr];
   5.262 -    val rules = (map o map o map) Logic.close_form [[[ind]], injs, half_distincts];
   5.263 -
   5.264 -    fun after_qed' raw_thms =
   5.265 -      let
   5.266 -        val [[[raw_induct]], raw_inject, half_distinct] =
   5.267 -          unflat rules (map Drule.zero_var_indexes_list raw_thms);
   5.268 -            (*FIXME somehow dubious*)
   5.269 -      in
   5.270 -        Proof_Context.background_theory_result  (* FIXME !? *)
   5.271 -          (prove_rep_datatype config dt_names descr raw_inject half_distinct raw_induct)
   5.272 -        #-> after_qed
   5.273 -      end;
   5.274 -  in
   5.275 -    ctxt
   5.276 -    |> Proof.theorem NONE after_qed' ((map o map) (rpair []) (flat rules))
   5.277 -  end;
   5.278 -
   5.279 -val rep_datatype = gen_rep_datatype Sign.cert_term;
   5.280 -val rep_datatype_cmd = gen_rep_datatype Syntax.read_term_global;
   5.281 -
   5.282 -
   5.283 -
   5.284 -(** package setup **)
   5.285 -
   5.286 -(* setup theory *)
   5.287 +(** setup theory **)
   5.288  
   5.289  val setup =
   5.290    trfun_setup #>
   5.291    antiq_setup #>
   5.292    Datatype_Interpretation.init;
   5.293  
   5.294 -
   5.295 -(* outer syntax *)
   5.296 -
   5.297 -val _ =
   5.298 -  Outer_Syntax.command "rep_datatype" "represent existing types inductively" Keyword.thy_goal
   5.299 -    (Scan.repeat1 Parse.term >> (fn ts =>
   5.300 -      Toplevel.print o
   5.301 -      Toplevel.theory_to_proof (rep_datatype_cmd Datatype_Aux.default_config (K I) ts)));
   5.302 -
   5.303 -
   5.304  open Datatype_Aux;
   5.305  
   5.306  end;
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/src/HOL/Tools/Datatype/rep_datatype.ML	Thu Dec 15 17:37:14 2011 +0100
     6.3 @@ -0,0 +1,214 @@
     6.4 +(*  Title:      HOL/Tools/Datatype/rep_datatype.ML
     6.5 +    Author:     Stefan Berghofer, TU Muenchen
     6.6 +
     6.7 +Representation of existing types as datatypes.
     6.8 +*)
     6.9 +
    6.10 +signature REP_DATATYPE =
    6.11 +sig
    6.12 +  val derive_datatype_props : Datatype_Aux.config -> string list -> Datatype_Aux.descr list ->
    6.13 +    thm -> thm list list -> thm list list -> theory -> string list * theory
    6.14 +  val rep_datatype : Datatype_Aux.config -> (string list -> Proof.context -> Proof.context) ->
    6.15 +    term list -> theory -> Proof.state
    6.16 +  val rep_datatype_cmd : Datatype_Aux.config -> (string list -> Proof.context -> Proof.context) ->
    6.17 +    string list -> theory -> Proof.state
    6.18 +end;
    6.19 +
    6.20 +structure Rep_Datatype: REP_DATATYPE =
    6.21 +struct
    6.22 +
    6.23 +fun make_dt_info descr induct inducts rec_names rec_rewrites
    6.24 +    (index, (((((((((((_, (tname, _, _))), inject), distinct),
    6.25 +      exhaust), nchotomy), case_name), case_rewrites), case_cong), weak_case_cong),
    6.26 +        (split, split_asm))) =
    6.27 +  (tname,
    6.28 +   {index = index,
    6.29 +    descr = descr,
    6.30 +    inject = inject,
    6.31 +    distinct = distinct,
    6.32 +    induct = induct,
    6.33 +    inducts = inducts,
    6.34 +    exhaust = exhaust,
    6.35 +    nchotomy = nchotomy,
    6.36 +    rec_names = rec_names,
    6.37 +    rec_rewrites = rec_rewrites,
    6.38 +    case_name = case_name,
    6.39 +    case_rewrites = case_rewrites,
    6.40 +    case_cong = case_cong,
    6.41 +    weak_case_cong = weak_case_cong,
    6.42 +    split = split,
    6.43 +    split_asm = split_asm});
    6.44 +
    6.45 +fun derive_datatype_props config dt_names descr induct inject distinct thy1 =
    6.46 +  let
    6.47 +    val thy2 = thy1 |> Theory.checkpoint;
    6.48 +    val flat_descr = flat descr;
    6.49 +    val new_type_names = map Long_Name.base_name dt_names;
    6.50 +    val _ =
    6.51 +      Datatype_Aux.message config
    6.52 +        ("Deriving properties for datatype(s) " ^ commas_quote new_type_names);
    6.53 +
    6.54 +    val (exhaust, thy3) = thy2
    6.55 +      |> Datatype_Abs_Proofs.prove_casedist_thms config new_type_names
    6.56 +        descr induct (Datatype_Data.mk_case_names_exhausts flat_descr dt_names);
    6.57 +    val (nchotomys, thy4) = thy3
    6.58 +      |> Datatype_Abs_Proofs.prove_nchotomys config new_type_names descr exhaust;
    6.59 +    val ((rec_names, rec_rewrites), thy5) = thy4
    6.60 +      |> Datatype_Abs_Proofs.prove_primrec_thms
    6.61 +        config new_type_names descr (#inject o the o Symtab.lookup (Datatype_Data.get_all thy4))
    6.62 +        inject (distinct, Datatype_Data.all_distincts thy2 (Datatype_Aux.get_rec_types flat_descr))
    6.63 +        induct;
    6.64 +    val ((case_rewrites, case_names), thy6) = thy5
    6.65 +      |> Datatype_Abs_Proofs.prove_case_thms config new_type_names descr rec_names rec_rewrites;
    6.66 +    val (case_congs, thy7) = thy6
    6.67 +      |> Datatype_Abs_Proofs.prove_case_congs new_type_names case_names descr
    6.68 +        nchotomys case_rewrites;
    6.69 +    val (weak_case_congs, thy8) = thy7
    6.70 +      |> Datatype_Abs_Proofs.prove_weak_case_congs new_type_names case_names descr;
    6.71 +    val (splits, thy9) = thy8
    6.72 +      |> Datatype_Abs_Proofs.prove_split_thms
    6.73 +        config new_type_names case_names descr inject distinct exhaust case_rewrites;
    6.74 +
    6.75 +    val inducts = Project_Rule.projections (Proof_Context.init_global thy2) induct;
    6.76 +    val dt_infos =
    6.77 +      map_index
    6.78 +        (make_dt_info flat_descr induct inducts rec_names rec_rewrites)
    6.79 +        (hd descr ~~ inject ~~ distinct ~~ exhaust ~~ nchotomys ~~
    6.80 +          case_names ~~ case_rewrites ~~ case_congs ~~ weak_case_congs ~~ splits);
    6.81 +    val dt_names = map fst dt_infos;
    6.82 +    val prfx = Binding.qualify true (space_implode "_" new_type_names);
    6.83 +    val simps = flat (inject @ distinct @ case_rewrites) @ rec_rewrites;
    6.84 +    val named_rules = flat (map_index (fn (index, tname) =>
    6.85 +      [((Binding.empty, [nth inducts index]), [Induct.induct_type tname]),
    6.86 +       ((Binding.empty, [nth exhaust index]), [Induct.cases_type tname])]) dt_names);
    6.87 +    val unnamed_rules = map (fn induct =>
    6.88 +      ((Binding.empty, [induct]), [Rule_Cases.inner_rule, Induct.induct_type ""]))
    6.89 +        (drop (length dt_names) inducts);
    6.90 +  in
    6.91 +    thy9
    6.92 +    |> Global_Theory.add_thmss ([((prfx (Binding.name "simps"), simps), []),
    6.93 +        ((prfx (Binding.name "inducts"), inducts), []),
    6.94 +        ((prfx (Binding.name "splits"), maps (fn (x, y) => [x, y]) splits), []),
    6.95 +        ((Binding.empty, flat case_rewrites @ flat distinct @ rec_rewrites),
    6.96 +          [Simplifier.simp_add]),
    6.97 +        ((Binding.empty, rec_rewrites), [Code.add_default_eqn_attribute]),
    6.98 +        ((Binding.empty, flat inject), [iff_add]),
    6.99 +        ((Binding.empty, map (fn th => th RS notE) (flat distinct)),
   6.100 +          [Classical.safe_elim NONE]),
   6.101 +        ((Binding.empty, weak_case_congs), [Simplifier.cong_add]),
   6.102 +        ((Binding.empty, flat (distinct @ inject)), [Induct.induct_simp_add])] @
   6.103 +          named_rules @ unnamed_rules)
   6.104 +    |> snd
   6.105 +    |> Datatype_Data.add_case_tr' case_names
   6.106 +    |> Datatype_Data.register dt_infos
   6.107 +    |> Datatype_Data.interpretation_data (config, dt_names)
   6.108 +    |> pair dt_names
   6.109 +  end;
   6.110 +
   6.111 +
   6.112 +
   6.113 +(** declare existing type as datatype **)
   6.114 +
   6.115 +local
   6.116 +
   6.117 +fun prove_rep_datatype config dt_names descr raw_inject half_distinct raw_induct thy1 =
   6.118 +  let
   6.119 +    val raw_distinct = (map o maps) (fn thm => [thm, thm RS not_sym]) half_distinct;
   6.120 +    val new_type_names = map Long_Name.base_name dt_names;
   6.121 +    val prfx = Binding.qualify true (space_implode "_" new_type_names);
   6.122 +    val (((inject, distinct), [induct]), thy2) =
   6.123 +      thy1
   6.124 +      |> Datatype_Aux.store_thmss "inject" new_type_names raw_inject
   6.125 +      ||>> Datatype_Aux.store_thmss "distinct" new_type_names raw_distinct
   6.126 +      ||>> Global_Theory.add_thms
   6.127 +        [((prfx (Binding.name "induct"), raw_induct),
   6.128 +          [Datatype_Data.mk_case_names_induct descr])];
   6.129 +  in
   6.130 +    thy2
   6.131 +    |> derive_datatype_props config dt_names [descr] induct inject distinct
   6.132 + end;
   6.133 +
   6.134 +fun gen_rep_datatype prep_term config after_qed raw_ts thy =
   6.135 +  let
   6.136 +    val ctxt = Proof_Context.init_global thy;
   6.137 +
   6.138 +    fun constr_of_term (Const (c, T)) = (c, T)
   6.139 +      | constr_of_term t = error ("Not a constant: " ^ Syntax.string_of_term ctxt t);
   6.140 +    fun no_constr (c, T) =
   6.141 +      error ("Bad constructor: " ^ Proof_Context.extern_const ctxt c ^ "::" ^
   6.142 +        Syntax.string_of_typ ctxt T);
   6.143 +    fun type_of_constr (cT as (_, T)) =
   6.144 +      let
   6.145 +        val frees = Term.add_tfreesT T [];
   6.146 +        val (tyco, vs) = (apsnd o map) dest_TFree (dest_Type (body_type T))
   6.147 +          handle TYPE _ => no_constr cT
   6.148 +        val _ = if has_duplicates (eq_fst (op =)) vs then no_constr cT else ();
   6.149 +        val _ = if length frees <> length vs then no_constr cT else ();
   6.150 +      in (tyco, (vs, cT)) end;
   6.151 +
   6.152 +    val raw_cs =
   6.153 +      AList.group (op =) (map (type_of_constr o constr_of_term o prep_term thy) raw_ts);
   6.154 +    val _ =
   6.155 +      (case map_filter (fn (tyco, _) =>
   6.156 +          if Symtab.defined (Datatype_Data.get_all thy) tyco then SOME tyco else NONE) raw_cs of
   6.157 +        [] => ()
   6.158 +      | tycos => error ("Type(s) " ^ commas_quote tycos ^ " already represented inductivly"));
   6.159 +    val raw_vss = maps (map (map snd o fst) o snd) raw_cs;
   6.160 +    val ms =
   6.161 +      (case distinct (op =) (map length raw_vss) of
   6.162 +         [n] => 0 upto n - 1
   6.163 +      | _ => error "Different types in given constructors");
   6.164 +    fun inter_sort m =
   6.165 +      map (fn xs => nth xs m) raw_vss
   6.166 +      |> foldr1 (Sorts.inter_sort (Sign.classes_of thy));
   6.167 +    val sorts = map inter_sort ms;
   6.168 +    val vs = Name.invent_names Name.context Name.aT sorts;
   6.169 +
   6.170 +    fun norm_constr (raw_vs, (c, T)) =
   6.171 +      (c, map_atyps
   6.172 +        (TFree o (the o AList.lookup (op =) (map fst raw_vs ~~ vs)) o fst o dest_TFree) T);
   6.173 +
   6.174 +    val cs = map (apsnd (map norm_constr)) raw_cs;
   6.175 +    val dtyps_of_typ = map (Datatype_Aux.dtyp_of_typ (map (rpair vs o fst) cs)) o binder_types;
   6.176 +    val dt_names = map fst cs;
   6.177 +
   6.178 +    fun mk_spec (i, (tyco, constr)) =
   6.179 +      (i, (tyco, map Datatype_Aux.DtTFree vs, (map o apsnd) dtyps_of_typ constr));
   6.180 +    val descr = map_index mk_spec cs;
   6.181 +    val injs = Datatype_Prop.make_injs [descr];
   6.182 +    val half_distincts = Datatype_Prop.make_distincts [descr];
   6.183 +    val ind = Datatype_Prop.make_ind [descr];
   6.184 +    val rules = (map o map o map) Logic.close_form [[[ind]], injs, half_distincts];
   6.185 +
   6.186 +    fun after_qed' raw_thms =
   6.187 +      let
   6.188 +        val [[[raw_induct]], raw_inject, half_distinct] =
   6.189 +          unflat rules (map Drule.zero_var_indexes_list raw_thms);
   6.190 +            (*FIXME somehow dubious*)
   6.191 +      in
   6.192 +        Proof_Context.background_theory_result  (* FIXME !? *)
   6.193 +          (prove_rep_datatype config dt_names descr raw_inject half_distinct raw_induct)
   6.194 +        #-> after_qed
   6.195 +      end;
   6.196 +  in
   6.197 +    ctxt
   6.198 +    |> Proof.theorem NONE after_qed' ((map o map) (rpair []) (flat rules))
   6.199 +  end;
   6.200 +
   6.201 +in
   6.202 +
   6.203 +val rep_datatype = gen_rep_datatype Sign.cert_term;
   6.204 +val rep_datatype_cmd = gen_rep_datatype Syntax.read_term_global;
   6.205 +
   6.206 +end;
   6.207 +
   6.208 +
   6.209 +(* outer syntax *)
   6.210 +
   6.211 +val _ =
   6.212 +  Outer_Syntax.command "rep_datatype" "represent existing types inductively" Keyword.thy_goal
   6.213 +    (Scan.repeat1 Parse.term >> (fn ts =>
   6.214 +      Toplevel.print o
   6.215 +      Toplevel.theory_to_proof (rep_datatype_cmd Datatype_Aux.default_config (K I) ts)));
   6.216 +
   6.217 +end;