src/HOL/Tools/Old_Datatype/old_rep_datatype.ML
author wenzelm
Sun Nov 09 14:08:00 2014 +0100 (2014-11-09)
changeset 58956 a816aa3ff391
parent 58839 ccda99401bc8
child 58963 26bf09b95dda
permissions -rw-r--r--
proper context for compose_tac, Splitter.split_tac (relevant for unify trace options);
blanchet@58112
     1
(*  Title:      HOL/Tools/Old_Datatype/old_rep_datatype.ML
wenzelm@45890
     2
    Author:     Stefan Berghofer, TU Muenchen
wenzelm@45890
     3
wenzelm@45907
     4
Representation of existing types as datatypes: proofs and definitions
wenzelm@45907
     5
independent of concrete representation of datatypes (i.e. requiring
wenzelm@45907
     6
only abstract properties: injectivity / distinctness of constructors
wenzelm@45907
     7
and induction).
wenzelm@45890
     8
*)
wenzelm@45890
     9
blanchet@58112
    10
signature OLD_REP_DATATYPE =
wenzelm@45890
    11
sig
blanchet@58112
    12
  val derive_datatype_props : Old_Datatype_Aux.config -> string list ->
blanchet@58112
    13
    Old_Datatype_Aux.descr list -> thm -> thm list list -> thm list list -> theory ->
blanchet@58112
    14
    string list * theory
blanchet@58112
    15
  val rep_datatype : Old_Datatype_Aux.config -> (string list -> Proof.context -> Proof.context) ->
wenzelm@45890
    16
    term list -> theory -> Proof.state
blanchet@58112
    17
  val rep_datatype_cmd : Old_Datatype_Aux.config ->
blanchet@58112
    18
    (string list -> Proof.context -> Proof.context) -> string list -> theory -> Proof.state
wenzelm@45890
    19
end;
wenzelm@45890
    20
blanchet@58112
    21
structure Old_Rep_Datatype: OLD_REP_DATATYPE =
wenzelm@45890
    22
struct
wenzelm@45890
    23
wenzelm@45907
    24
(** derived definitions and proofs **)
wenzelm@45907
    25
wenzelm@45907
    26
(* case distinction theorems *)
wenzelm@45907
    27
blanchet@58112
    28
fun prove_casedist_thms (config : Old_Datatype_Aux.config)
wenzelm@45909
    29
    new_type_names descr induct case_names_exhausts thy =
wenzelm@45907
    30
  let
blanchet@58112
    31
    val _ = Old_Datatype_Aux.message config "Proving case distinction theorems ...";
wenzelm@45907
    32
wenzelm@45907
    33
    val descr' = flat descr;
blanchet@58112
    34
    val recTs = Old_Datatype_Aux.get_rec_types descr';
wenzelm@45907
    35
    val newTs = take (length (hd descr)) recTs;
wenzelm@45907
    36
wenzelm@45907
    37
    val maxidx = Thm.maxidx_of induct;
wenzelm@45907
    38
    val induct_Ps = map head_of (HOLogic.dest_conj (HOLogic.dest_Trueprop (concl_of induct)));
wenzelm@45907
    39
wenzelm@45907
    40
    fun prove_casedist_thm (i, (T, t)) =
wenzelm@45907
    41
      let
wenzelm@45907
    42
        val dummyPs = map (fn (Var (_, Type (_, [T', T'']))) =>
wenzelm@45907
    43
          Abs ("z", T', Const (@{const_name True}, T''))) induct_Ps;
wenzelm@45907
    44
        val P =
wenzelm@45907
    45
          Abs ("z", T, HOLogic.imp $ HOLogic.mk_eq (Var (("a", maxidx + 1), T), Bound 0) $
wenzelm@45907
    46
            Var (("P", 0), HOLogic.boolT));
wenzelm@45907
    47
        val insts = take i dummyPs @ (P :: drop (i + 1) dummyPs);
wenzelm@45907
    48
        val cert = cterm_of thy;
wenzelm@45907
    49
        val insts' = map cert induct_Ps ~~ map cert insts;
wenzelm@45907
    50
        val induct' =
wenzelm@45907
    51
          refl RS
blanchet@58112
    52
            (nth (Old_Datatype_Aux.split_conj_thm (cterm_instantiate insts' induct)) i
blanchet@58112
    53
             RSN (2, rev_mp));
wenzelm@45907
    54
      in
wenzelm@51551
    55
        Goal.prove_sorry_global thy []
wenzelm@45907
    56
          (Logic.strip_imp_prems t)
wenzelm@45907
    57
          (Logic.strip_imp_concl t)
wenzelm@45907
    58
          (fn {prems, ...} =>
wenzelm@45907
    59
            EVERY
wenzelm@58839
    60
              [resolve_tac [induct'] 1,
wenzelm@58839
    61
               REPEAT (resolve_tac [TrueI] 1),
wenzelm@58839
    62
               REPEAT ((resolve_tac [impI] 1) THEN (eresolve_tac prems 1)),
wenzelm@58839
    63
               REPEAT (resolve_tac [TrueI] 1)])
wenzelm@45907
    64
      end;
wenzelm@45907
    65
wenzelm@45907
    66
    val casedist_thms =
blanchet@58112
    67
      map_index prove_casedist_thm (newTs ~~ Old_Datatype_Prop.make_casedists descr);
wenzelm@45907
    68
  in
wenzelm@45907
    69
    thy
blanchet@58112
    70
    |> Old_Datatype_Aux.store_thms_atts "exhaust" new_type_names
wenzelm@45907
    71
        (map single case_names_exhausts) casedist_thms
wenzelm@45907
    72
  end;
wenzelm@45907
    73
wenzelm@45907
    74
wenzelm@45907
    75
(* primrec combinators *)
wenzelm@45907
    76
blanchet@58112
    77
fun prove_primrec_thms (config : Old_Datatype_Aux.config) new_type_names descr
wenzelm@45907
    78
    injects_of constr_inject (dist_rewrites, other_dist_rewrites) induct thy =
wenzelm@45907
    79
  let
blanchet@58112
    80
    val _ = Old_Datatype_Aux.message config "Constructing primrec combinators ...";
wenzelm@45907
    81
wenzelm@45907
    82
    val big_name = space_implode "_" new_type_names;
wenzelm@45907
    83
    val thy0 = Sign.add_path big_name thy;
wenzelm@45907
    84
wenzelm@45907
    85
    val descr' = flat descr;
blanchet@58112
    86
    val recTs = Old_Datatype_Aux.get_rec_types descr';
wenzelm@45907
    87
    val used = fold Term.add_tfree_namesT recTs [];
wenzelm@45907
    88
    val newTs = take (length (hd descr)) recTs;
wenzelm@45907
    89
wenzelm@45907
    90
    val induct_Ps = map head_of (HOLogic.dest_conj (HOLogic.dest_Trueprop (concl_of induct)));
wenzelm@45907
    91
blanchet@55408
    92
    val big_rec_name' = "rec_set_" ^ big_name;
wenzelm@45907
    93
    val rec_set_names' =
wenzelm@45907
    94
      if length descr' = 1 then [big_rec_name']
wenzelm@45907
    95
      else map (prefix (big_rec_name' ^ "_") o string_of_int) (1 upto length descr');
wenzelm@45907
    96
    val rec_set_names = map (Sign.full_bname thy0) rec_set_names';
wenzelm@45907
    97
blanchet@58112
    98
    val (rec_result_Ts, reccomb_fn_Ts) = Old_Datatype_Prop.make_primrec_Ts descr used;
wenzelm@45907
    99
wenzelm@45907
   100
    val rec_set_Ts =
wenzelm@45907
   101
      map (fn (T1, T2) => (reccomb_fn_Ts @ [T1, T2]) ---> HOLogic.boolT) (recTs ~~ rec_result_Ts);
wenzelm@45907
   102
wenzelm@45907
   103
    val rec_fns =
blanchet@58112
   104
      map (uncurry (Old_Datatype_Aux.mk_Free "f")) (reccomb_fn_Ts ~~ (1 upto length reccomb_fn_Ts));
wenzelm@45907
   105
    val rec_sets' =
wenzelm@45907
   106
      map (fn c => list_comb (Free c, rec_fns)) (rec_set_names' ~~ rec_set_Ts);
wenzelm@45907
   107
    val rec_sets =
wenzelm@45907
   108
      map (fn c => list_comb (Const c, rec_fns)) (rec_set_names ~~ rec_set_Ts);
wenzelm@45907
   109
wenzelm@45907
   110
    (* introduction rules for graph of primrec function *)
wenzelm@45907
   111
wenzelm@45907
   112
    fun make_rec_intr T rec_set (cname, cargs) (rec_intr_ts, l) =
wenzelm@45907
   113
      let
wenzelm@45907
   114
        fun mk_prem (dt, U) (j, k, prems, t1s, t2s) =
blanchet@58112
   115
          let val free1 = Old_Datatype_Aux.mk_Free "x" U j in
blanchet@58112
   116
            (case (Old_Datatype_Aux.strip_dtyp dt, strip_type U) of
blanchet@58112
   117
              ((_, Old_Datatype_Aux.DtRec m), (Us, _)) =>
wenzelm@45907
   118
                let
blanchet@58112
   119
                  val free2 = Old_Datatype_Aux.mk_Free "y" (Us ---> nth rec_result_Ts m) k;
wenzelm@45907
   120
                  val i = length Us;
wenzelm@45907
   121
                in
wenzelm@45907
   122
                  (j + 1, k + 1,
wenzelm@45907
   123
                    HOLogic.mk_Trueprop (HOLogic.list_all
wenzelm@45907
   124
                      (map (pair "x") Us, nth rec_sets' m $
blanchet@58112
   125
                        Old_Datatype_Aux.app_bnds free1 i $
blanchet@58112
   126
                          Old_Datatype_Aux.app_bnds free2 i)) :: prems,
wenzelm@45907
   127
                    free1 :: t1s, free2 :: t2s)
wenzelm@45907
   128
                end
wenzelm@45907
   129
            | _ => (j + 1, k, prems, free1 :: t1s, t2s))
wenzelm@45907
   130
          end;
wenzelm@45907
   131
blanchet@58112
   132
        val Ts = map (Old_Datatype_Aux.typ_of_dtyp descr') cargs;
wenzelm@45907
   133
        val (_, _, prems, t1s, t2s) = fold_rev mk_prem (cargs ~~ Ts) (1, 1, [], [], []);
wenzelm@45907
   134
wenzelm@45907
   135
      in
wenzelm@45907
   136
        (rec_intr_ts @
wenzelm@45907
   137
          [Logic.list_implies (prems, HOLogic.mk_Trueprop
wenzelm@45907
   138
            (rec_set $ list_comb (Const (cname, Ts ---> T), t1s) $
wenzelm@45907
   139
              list_comb (nth rec_fns l, t1s @ t2s)))], l + 1)
wenzelm@45907
   140
      end;
wenzelm@45907
   141
wenzelm@45907
   142
    val (rec_intr_ts, _) =
wenzelm@45907
   143
      fold (fn ((d, T), set_name) =>
wenzelm@45907
   144
        fold (make_rec_intr T set_name) (#3 (snd d))) (descr' ~~ recTs ~~ rec_sets') ([], 0);
wenzelm@45907
   145
wenzelm@45907
   146
    val ({intrs = rec_intrs, elims = rec_elims, ...}, thy1) =
wenzelm@45907
   147
      thy0
wenzelm@45907
   148
      |> Sign.map_naming Name_Space.conceal
wenzelm@45907
   149
      |> Inductive.add_inductive_global
wenzelm@45907
   150
          {quiet_mode = #quiet config, verbose = false, alt_name = Binding.name big_rec_name',
wenzelm@49170
   151
            coind = false, no_elim = false, no_ind = true, skip_mono = true}
wenzelm@45907
   152
          (map (fn (s, T) => ((Binding.name s, T), NoSyn)) (rec_set_names' ~~ rec_set_Ts))
wenzelm@45907
   153
          (map dest_Free rec_fns)
wenzelm@45907
   154
          (map (fn x => (Attrib.empty_binding, x)) rec_intr_ts) []
wenzelm@52788
   155
      ||> Sign.restore_naming thy0;
wenzelm@45907
   156
wenzelm@45907
   157
    (* prove uniqueness and termination of primrec combinators *)
wenzelm@45907
   158
blanchet@58112
   159
    val _ = Old_Datatype_Aux.message config
blanchet@58112
   160
      "Proving termination and uniqueness of primrec functions ...";
wenzelm@45907
   161
wenzelm@51717
   162
    fun mk_unique_tac ctxt ((((i, (tname, _, constrs)), elim), T), T') (tac, intrs) =
wenzelm@45907
   163
      let
wenzelm@45907
   164
        val distinct_tac =
wenzelm@45907
   165
          if i < length newTs then
wenzelm@51717
   166
            full_simp_tac (put_simpset HOL_ss ctxt addsimps (nth dist_rewrites i)) 1
wenzelm@51717
   167
          else full_simp_tac (put_simpset HOL_ss ctxt addsimps (flat other_dist_rewrites)) 1;
wenzelm@45907
   168
wenzelm@45907
   169
        val inject =
wenzelm@45907
   170
          map (fn r => r RS iffD1)
wenzelm@45907
   171
            (if i < length newTs then nth constr_inject i else injects_of tname);
wenzelm@45907
   172
wenzelm@45907
   173
        fun mk_unique_constr_tac n (cname, cargs) (tac, intr :: intrs, j) =
wenzelm@45907
   174
          let
blanchet@58112
   175
            val k = length (filter Old_Datatype_Aux.is_rec_type cargs);
wenzelm@45907
   176
          in
wenzelm@45907
   177
            (EVERY
wenzelm@45907
   178
              [DETERM tac,
wenzelm@58839
   179
                REPEAT (eresolve_tac @{thms ex1E} 1), resolve_tac @{thms ex1I} 1,
wenzelm@45907
   180
                DEPTH_SOLVE_1 (ares_tac [intr] 1),
wenzelm@58839
   181
                REPEAT_DETERM_N k (eresolve_tac [thin_rl] 1 THEN rotate_tac 1 1),
wenzelm@58839
   182
                eresolve_tac [elim] 1,
wenzelm@45907
   183
                REPEAT_DETERM_N j distinct_tac,
wenzelm@45907
   184
                TRY (dresolve_tac inject 1),
wenzelm@58839
   185
                REPEAT (eresolve_tac [conjE] 1), hyp_subst_tac ctxt 1,
wenzelm@58839
   186
                REPEAT (EVERY [eresolve_tac [allE] 1, dresolve_tac [mp] 1, assume_tac 1]),
wenzelm@51798
   187
                TRY (hyp_subst_tac ctxt 1),
wenzelm@58839
   188
                resolve_tac [refl] 1,
wenzelm@45907
   189
                REPEAT_DETERM_N (n - j - 1) distinct_tac],
wenzelm@45907
   190
              intrs, j + 1)
wenzelm@45907
   191
          end;
wenzelm@45907
   192
wenzelm@45907
   193
        val (tac', intrs', _) =
wenzelm@45907
   194
          fold (mk_unique_constr_tac (length constrs)) constrs (tac, intrs, 0);
wenzelm@45907
   195
      in (tac', intrs') end;
wenzelm@45907
   196
wenzelm@45907
   197
    val rec_unique_thms =
wenzelm@45907
   198
      let
wenzelm@45907
   199
        val rec_unique_ts =
wenzelm@45907
   200
          map (fn (((set_t, T1), T2), i) =>
wenzelm@45907
   201
            Const (@{const_name Ex1}, (T2 --> HOLogic.boolT) --> HOLogic.boolT) $
blanchet@58112
   202
              absfree ("y", T2) (set_t $ Old_Datatype_Aux.mk_Free "x" T1 i $ Free ("y", T2)))
wenzelm@45907
   203
                (rec_sets ~~ recTs ~~ rec_result_Ts ~~ (1 upto length recTs));
wenzelm@45907
   204
        val cert = cterm_of thy1;
wenzelm@45907
   205
        val insts =
wenzelm@45907
   206
          map (fn ((i, T), t) => absfree ("x" ^ string_of_int i, T) t)
wenzelm@45907
   207
            ((1 upto length recTs) ~~ recTs ~~ rec_unique_ts);
wenzelm@45907
   208
        val induct' = cterm_instantiate (map cert induct_Ps ~~ map cert insts) induct;
wenzelm@45907
   209
      in
blanchet@58112
   210
        Old_Datatype_Aux.split_conj_thm (Goal.prove_sorry_global thy1 [] []
blanchet@58112
   211
          (HOLogic.mk_Trueprop (Old_Datatype_Aux.mk_conj rec_unique_ts))
wenzelm@51717
   212
          (fn {context = ctxt, ...} =>
wenzelm@51717
   213
            #1 (fold (mk_unique_tac ctxt) (descr' ~~ rec_elims ~~ recTs ~~ rec_result_Ts)
wenzelm@58839
   214
              (((resolve_tac [induct'] THEN_ALL_NEW Object_Logic.atomize_prems_tac ctxt) 1 THEN
wenzelm@54742
   215
                  rewrite_goals_tac ctxt [mk_meta_eq @{thm choice_eq}], rec_intrs)))))
wenzelm@45907
   216
      end;
wenzelm@45907
   217
wenzelm@45907
   218
    val rec_total_thms = map (fn r => r RS @{thm theI'}) rec_unique_thms;
wenzelm@45907
   219
wenzelm@45907
   220
    (* define primrec combinators *)
wenzelm@45907
   221
blanchet@55408
   222
    val big_reccomb_name = "rec_" ^ space_implode "_" new_type_names;
wenzelm@45907
   223
    val reccomb_names =
wenzelm@45907
   224
      map (Sign.full_bname thy1)
wenzelm@45907
   225
        (if length descr' = 1 then [big_reccomb_name]
wenzelm@45907
   226
         else map (prefix (big_reccomb_name ^ "_") o string_of_int) (1 upto length descr'));
wenzelm@45907
   227
    val reccombs =
wenzelm@45907
   228
      map (fn ((name, T), T') => Const (name, reccomb_fn_Ts @ [T] ---> T'))
wenzelm@45907
   229
        (reccomb_names ~~ recTs ~~ rec_result_Ts);
wenzelm@45907
   230
wenzelm@45907
   231
    val (reccomb_defs, thy2) =
wenzelm@45907
   232
      thy1
wenzelm@56239
   233
      |> Sign.add_consts (map (fn ((name, T), T') =>
wenzelm@45907
   234
            (Binding.name (Long_Name.base_name name), reccomb_fn_Ts @ [T] ---> T', NoSyn))
wenzelm@45907
   235
            (reccomb_names ~~ recTs ~~ rec_result_Ts))
wenzelm@45907
   236
      |> (Global_Theory.add_defs false o map Thm.no_attributes)
wenzelm@45907
   237
          (map
wenzelm@45907
   238
            (fn ((((name, comb), set), T), T') =>
wenzelm@46909
   239
              (Binding.name (Thm.def_name (Long_Name.base_name name)),
wenzelm@45907
   240
                Logic.mk_equals (comb, fold_rev lambda rec_fns (absfree ("x", T)
wenzelm@45907
   241
                 (Const (@{const_name The}, (T' --> HOLogic.boolT) --> T') $ absfree ("y", T')
wenzelm@45907
   242
                   (set $ Free ("x", T) $ Free ("y", T')))))))
wenzelm@45907
   243
            (reccomb_names ~~ reccombs ~~ rec_sets ~~ recTs ~~ rec_result_Ts))
wenzelm@52788
   244
      ||> Sign.parent_path;
wenzelm@45907
   245
wenzelm@45907
   246
wenzelm@45907
   247
    (* prove characteristic equations for primrec combinators *)
wenzelm@45907
   248
blanchet@58112
   249
    val _ = Old_Datatype_Aux.message config
blanchet@58112
   250
      "Proving characteristic theorems for primrec combinators ...";
wenzelm@45907
   251
wenzelm@45907
   252
    val rec_thms =
wenzelm@45907
   253
      map (fn t =>
wenzelm@51551
   254
        Goal.prove_sorry_global thy2 [] [] t
wenzelm@54742
   255
          (fn {context = ctxt, ...} => EVERY
wenzelm@54742
   256
            [rewrite_goals_tac ctxt reccomb_defs,
wenzelm@58839
   257
             resolve_tac @{thms the1_equality} 1,
wenzelm@45907
   258
             resolve_tac rec_unique_thms 1,
wenzelm@45907
   259
             resolve_tac rec_intrs 1,
wenzelm@58839
   260
             REPEAT (resolve_tac [allI] 1 ORELSE resolve_tac rec_total_thms 1)]))
blanchet@58112
   261
       (Old_Datatype_Prop.make_primrecs reccomb_names descr thy2);
wenzelm@45907
   262
  in
wenzelm@45907
   263
    thy2
wenzelm@45907
   264
    |> Sign.add_path (space_implode "_" new_type_names)
wenzelm@45907
   265
    |> Global_Theory.note_thmss ""
wenzelm@57964
   266
      [((Binding.name "rec", [Named_Theorems.add @{named_theorems nitpick_simp}]),
wenzelm@57964
   267
          [(rec_thms, [])])]
wenzelm@45907
   268
    ||> Sign.parent_path
wenzelm@45907
   269
    |-> (fn thms => pair (reccomb_names, maps #2 thms))
wenzelm@45907
   270
  end;
wenzelm@45907
   271
wenzelm@45907
   272
wenzelm@45907
   273
(* case combinators *)
wenzelm@45907
   274
blanchet@58112
   275
fun prove_case_thms (config : Old_Datatype_Aux.config)
wenzelm@45909
   276
    new_type_names descr reccomb_names primrec_thms thy =
wenzelm@45907
   277
  let
blanchet@58112
   278
    val _ = Old_Datatype_Aux.message config
blanchet@58112
   279
      "Proving characteristic theorems for case combinators ...";
wenzelm@45907
   280
blanchet@55403
   281
    val ctxt = Proof_Context.init_global thy;
wenzelm@45907
   282
    val thy1 = Sign.add_path (space_implode "_" new_type_names) thy;
wenzelm@45907
   283
wenzelm@45907
   284
    val descr' = flat descr;
blanchet@58112
   285
    val recTs = Old_Datatype_Aux.get_rec_types descr';
wenzelm@45907
   286
    val used = fold Term.add_tfree_namesT recTs [];
wenzelm@45907
   287
    val newTs = take (length (hd descr)) recTs;
wenzelm@56254
   288
    val T' = TFree (singleton (Name.variant_list used) "'t", @{sort type});
wenzelm@45907
   289
blanchet@58112
   290
    fun mk_dummyT dt = binder_types (Old_Datatype_Aux.typ_of_dtyp descr' dt) ---> T';
wenzelm@45907
   291
wenzelm@45907
   292
    val case_dummy_fns =
wenzelm@45907
   293
      map (fn (_, (_, _, constrs)) => map (fn (_, cargs) =>
wenzelm@45907
   294
        let
blanchet@58112
   295
          val Ts = map (Old_Datatype_Aux.typ_of_dtyp descr') cargs;
blanchet@58112
   296
          val Ts' = map mk_dummyT (filter Old_Datatype_Aux.is_rec_type cargs)
wenzelm@45907
   297
        in Const (@{const_name undefined}, Ts @ Ts' ---> T') end) constrs) descr';
wenzelm@45907
   298
blanchet@55408
   299
    val case_names0 = map (fn s => Sign.full_bname thy1 ("case_" ^ s)) new_type_names;
wenzelm@45907
   300
wenzelm@45907
   301
    (* define case combinators via primrec combinators *)
wenzelm@45907
   302
blanchet@55403
   303
    fun def_case ((((i, (_, _, constrs)), T as Type (Tcon, _)), name), recname) (defs, thy) =
blanchet@55403
   304
      if is_some (Ctr_Sugar.ctr_sugar_of ctxt Tcon) then
blanchet@55403
   305
        (defs, thy)
blanchet@55403
   306
      else
blanchet@55403
   307
        let
blanchet@55403
   308
          val (fns1, fns2) = split_list (map (fn ((_, cargs), j) =>
blanchet@55403
   309
            let
blanchet@58112
   310
              val Ts = map (Old_Datatype_Aux.typ_of_dtyp descr') cargs;
blanchet@58112
   311
              val Ts' = Ts @ map mk_dummyT (filter Old_Datatype_Aux.is_rec_type cargs);
blanchet@58112
   312
              val frees' = map2 (Old_Datatype_Aux.mk_Free "x") Ts' (1 upto length Ts');
blanchet@55403
   313
              val frees = take (length cargs) frees';
blanchet@58112
   314
              val free = Old_Datatype_Aux.mk_Free "f" (Ts ---> T') j;
blanchet@55403
   315
            in
blanchet@55403
   316
              (free, fold_rev (absfree o dest_Free) frees' (list_comb (free, frees)))
blanchet@55403
   317
            end) (constrs ~~ (1 upto length constrs)));
wenzelm@45907
   318
blanchet@55403
   319
          val caseT = map (snd o dest_Free) fns1 @ [T] ---> T';
blanchet@55403
   320
          val fns = flat (take i case_dummy_fns) @ fns2 @ flat (drop (i + 1) case_dummy_fns);
blanchet@55403
   321
          val reccomb = Const (recname, (map fastype_of fns) @ [T] ---> T');
blanchet@55403
   322
          val decl = ((Binding.name (Long_Name.base_name name), caseT), NoSyn);
blanchet@55403
   323
          val def =
blanchet@55403
   324
            (Binding.name (Thm.def_name (Long_Name.base_name name)),
blanchet@55403
   325
              Logic.mk_equals (Const (name, caseT),
blanchet@55403
   326
                fold_rev lambda fns1
blanchet@55403
   327
                  (list_comb (reccomb,
blanchet@55403
   328
                    flat (take i case_dummy_fns) @ fns2 @ flat (drop (i + 1) case_dummy_fns)))));
blanchet@55403
   329
          val ([def_thm], thy') =
blanchet@55403
   330
            thy
blanchet@55403
   331
            |> Sign.declare_const_global decl |> snd
blanchet@55403
   332
            |> (Global_Theory.add_defs false o map Thm.no_attributes) [def];
blanchet@55403
   333
        in (defs @ [def_thm], thy') end;
wenzelm@45907
   334
blanchet@55402
   335
    val (case_defs, thy2) =
blanchet@55403
   336
      fold def_case (hd descr ~~ newTs ~~ case_names0 ~~ take (length newTs) reccomb_names)
blanchet@55402
   337
        ([], thy1);
blanchet@55402
   338
blanchet@55403
   339
    fun prove_case t =
blanchet@55402
   340
      Goal.prove_sorry_global thy2 [] [] t (fn {context = ctxt, ...} =>
wenzelm@58839
   341
        EVERY [rewrite_goals_tac ctxt (case_defs @ map mk_meta_eq primrec_thms),
wenzelm@58839
   342
          resolve_tac [refl] 1]);
wenzelm@45907
   343
blanchet@55403
   344
    fun prove_cases (Type (Tcon, _)) ts =
blanchet@55403
   345
      (case Ctr_Sugar.ctr_sugar_of ctxt Tcon of
blanchet@55403
   346
        SOME {case_thms, ...} => case_thms
blanchet@55403
   347
      | NONE => map prove_case ts);
blanchet@55403
   348
wenzelm@45907
   349
    val case_thms =
blanchet@58112
   350
      map2 prove_cases newTs (Old_Datatype_Prop.make_cases case_names0 descr thy2);
blanchet@55403
   351
blanchet@55403
   352
    fun case_name_of (th :: _) =
blanchet@55403
   353
      fst (dest_Const (head_of (fst (HOLogic.dest_eq (HOLogic.dest_Trueprop (prop_of th))))));
blanchet@55403
   354
blanchet@55403
   355
    val case_names = map case_name_of case_thms;
wenzelm@45907
   356
  in
wenzelm@45907
   357
    thy2
wenzelm@57964
   358
    |> Context.theory_map
wenzelm@57964
   359
        ((fold o fold) (Named_Theorems.add_thm @{named_theorems nitpick_simp}) case_thms)
wenzelm@45907
   360
    |> Sign.parent_path
blanchet@58112
   361
    |> Old_Datatype_Aux.store_thmss "case" new_type_names case_thms
wenzelm@45907
   362
    |-> (fn thmss => pair (thmss, case_names))
wenzelm@45907
   363
  end;
wenzelm@45907
   364
wenzelm@45907
   365
wenzelm@45907
   366
(* case splitting *)
wenzelm@45907
   367
blanchet@58112
   368
fun prove_split_thms (config : Old_Datatype_Aux.config)
wenzelm@45907
   369
    new_type_names case_names descr constr_inject dist_rewrites casedist_thms case_thms thy =
wenzelm@45907
   370
  let
blanchet@58112
   371
    val _ = Old_Datatype_Aux.message config "Proving equations for case splitting ...";
wenzelm@45907
   372
wenzelm@45907
   373
    val descr' = flat descr;
blanchet@58112
   374
    val recTs = Old_Datatype_Aux.get_rec_types descr';
wenzelm@45907
   375
    val newTs = take (length (hd descr)) recTs;
wenzelm@45907
   376
wenzelm@45907
   377
    fun prove_split_thms ((((((t1, t2), inject), dist_rewrites'), exhaustion), case_thms'), T) =
wenzelm@45907
   378
      let
wenzelm@45907
   379
        val cert = cterm_of thy;
wenzelm@45907
   380
        val _ $ (_ $ lhs $ _) = hd (Logic.strip_assums_hyp (hd (prems_of exhaustion)));
wenzelm@45907
   381
        val exhaustion' = cterm_instantiate [(cert lhs, cert (Free ("x", T)))] exhaustion;
wenzelm@51717
   382
        fun tac ctxt =
wenzelm@58839
   383
          EVERY [resolve_tac [exhaustion'] 1,
wenzelm@51717
   384
            ALLGOALS (asm_simp_tac
wenzelm@51717
   385
              (put_simpset HOL_ss ctxt addsimps (dist_rewrites' @ inject @ case_thms')))];
wenzelm@45907
   386
      in
wenzelm@51717
   387
        (Goal.prove_sorry_global thy [] [] t1 (tac o #context),
wenzelm@51717
   388
         Goal.prove_sorry_global thy [] [] t2 (tac o #context))
wenzelm@45907
   389
      end;
wenzelm@45907
   390
wenzelm@45907
   391
    val split_thm_pairs =
wenzelm@45907
   392
      map prove_split_thms
blanchet@58112
   393
        (Old_Datatype_Prop.make_splits case_names descr thy ~~ constr_inject ~~
wenzelm@45907
   394
          dist_rewrites ~~ casedist_thms ~~ case_thms ~~ newTs);
wenzelm@45907
   395
wenzelm@45907
   396
    val (split_thms, split_asm_thms) = split_list split_thm_pairs
wenzelm@45907
   397
wenzelm@45907
   398
  in
wenzelm@45907
   399
    thy
blanchet@58112
   400
    |> Old_Datatype_Aux.store_thms "split" new_type_names split_thms
blanchet@58112
   401
    ||>> Old_Datatype_Aux.store_thms "split_asm" new_type_names split_asm_thms
wenzelm@45907
   402
    |-> (fn (thms1, thms2) => pair (thms1 ~~ thms2))
wenzelm@45907
   403
  end;
wenzelm@45907
   404
blanchet@57983
   405
fun prove_case_cong_weaks new_type_names case_names descr thy =
wenzelm@45907
   406
  let
blanchet@57983
   407
    fun prove_case_cong_weak t =
wenzelm@51551
   408
     Goal.prove_sorry_global thy [] (Logic.strip_imp_prems t) (Logic.strip_imp_concl t)
wenzelm@58839
   409
       (fn {prems, ...} => EVERY [resolve_tac [hd prems RS arg_cong] 1]);
wenzelm@45907
   410
blanchet@57983
   411
    val case_cong_weaks =
blanchet@58112
   412
      map prove_case_cong_weak (Old_Datatype_Prop.make_case_cong_weaks case_names descr thy);
wenzelm@45907
   413
blanchet@58112
   414
  in thy |> Old_Datatype_Aux.store_thms "case_cong_weak" new_type_names case_cong_weaks end;
wenzelm@45907
   415
wenzelm@45907
   416
wenzelm@45907
   417
(* additional theorems for TFL *)
wenzelm@45907
   418
blanchet@58112
   419
fun prove_nchotomys (config : Old_Datatype_Aux.config) new_type_names descr casedist_thms thy =
wenzelm@45907
   420
  let
blanchet@58112
   421
    val _ = Old_Datatype_Aux.message config "Proving additional theorems for TFL ...";
wenzelm@45907
   422
wenzelm@45907
   423
    fun prove_nchotomy (t, exhaustion) =
wenzelm@45907
   424
      let
wenzelm@45907
   425
        (* For goal i, select the correct disjunct to attack, then prove it *)
wenzelm@51798
   426
        fun tac ctxt i 0 =
wenzelm@58839
   427
              EVERY [TRY (resolve_tac [disjI1] i), hyp_subst_tac ctxt i,
wenzelm@58839
   428
                REPEAT (resolve_tac [exI] i), resolve_tac [refl] i]
wenzelm@58839
   429
          | tac ctxt i n = resolve_tac [disjI2] i THEN tac ctxt i (n - 1);
wenzelm@45907
   430
      in
wenzelm@51551
   431
        Goal.prove_sorry_global thy [] [] t
wenzelm@51798
   432
          (fn {context = ctxt, ...} =>
wenzelm@58839
   433
            EVERY [resolve_tac [allI] 1,
wenzelm@58956
   434
             Old_Datatype_Aux.exh_tac ctxt (K exhaustion) 1,
wenzelm@51798
   435
             ALLGOALS (fn i => tac ctxt i (i - 1))])
wenzelm@45907
   436
      end;
wenzelm@45907
   437
wenzelm@45907
   438
    val nchotomys =
blanchet@58112
   439
      map prove_nchotomy (Old_Datatype_Prop.make_nchotomys descr ~~ casedist_thms);
wenzelm@45907
   440
blanchet@58112
   441
  in thy |> Old_Datatype_Aux.store_thms "nchotomy" new_type_names nchotomys end;
wenzelm@45907
   442
wenzelm@45907
   443
fun prove_case_congs new_type_names case_names descr nchotomys case_thms thy =
wenzelm@45907
   444
  let
wenzelm@45907
   445
    fun prove_case_cong ((t, nchotomy), case_rewrites) =
wenzelm@45907
   446
      let
wenzelm@56245
   447
        val Const (@{const_name Pure.imp}, _) $ tm $ _ = t;
wenzelm@45907
   448
        val Const (@{const_name Trueprop}, _) $ (Const (@{const_name HOL.eq}, _) $ _ $ Ma) = tm;
wenzelm@45907
   449
        val cert = cterm_of thy;
wenzelm@45907
   450
        val nchotomy' = nchotomy RS spec;
wenzelm@45907
   451
        val [v] = Term.add_vars (concl_of nchotomy') [];
wenzelm@45907
   452
        val nchotomy'' = cterm_instantiate [(cert (Var v), cert Ma)] nchotomy';
wenzelm@45907
   453
      in
wenzelm@51551
   454
        Goal.prove_sorry_global thy [] (Logic.strip_imp_prems t) (Logic.strip_imp_concl t)
wenzelm@51717
   455
          (fn {context = ctxt, prems, ...} =>
wenzelm@51717
   456
            let
wenzelm@51717
   457
              val simplify = asm_simp_tac (put_simpset HOL_ss ctxt addsimps (prems @ case_rewrites))
wenzelm@51717
   458
            in
wenzelm@45907
   459
              EVERY [
wenzelm@51717
   460
                simp_tac (put_simpset HOL_ss ctxt addsimps [hd prems]) 1,
wenzelm@46708
   461
                cut_tac nchotomy'' 1,
wenzelm@58839
   462
                REPEAT (eresolve_tac [disjE] 1 THEN REPEAT (eresolve_tac [exE] 1) THEN simplify 1),
wenzelm@58839
   463
                REPEAT (eresolve_tac [exE] 1) THEN simplify 1 (* Get last disjunct *)]
wenzelm@45907
   464
            end)
wenzelm@45907
   465
      end;
wenzelm@45907
   466
wenzelm@45907
   467
    val case_congs =
wenzelm@45907
   468
      map prove_case_cong
blanchet@58112
   469
        (Old_Datatype_Prop.make_case_congs case_names descr thy ~~ nchotomys ~~ case_thms);
wenzelm@45907
   470
blanchet@58112
   471
  in thy |> Old_Datatype_Aux.store_thms "case_cong" new_type_names case_congs end;
wenzelm@45907
   472
wenzelm@45907
   473
wenzelm@45907
   474
wenzelm@45907
   475
(** derive datatype props **)
wenzelm@45907
   476
wenzelm@45907
   477
local
wenzelm@45907
   478
wenzelm@45890
   479
fun make_dt_info descr induct inducts rec_names rec_rewrites
wenzelm@45890
   480
    (index, (((((((((((_, (tname, _, _))), inject), distinct),
blanchet@57983
   481
      exhaust), nchotomy), case_name), case_rewrites), case_cong), case_cong_weak),
wenzelm@45890
   482
        (split, split_asm))) =
wenzelm@45890
   483
  (tname,
wenzelm@45890
   484
   {index = index,
wenzelm@45890
   485
    descr = descr,
wenzelm@45890
   486
    inject = inject,
wenzelm@45890
   487
    distinct = distinct,
wenzelm@45890
   488
    induct = induct,
wenzelm@45890
   489
    inducts = inducts,
wenzelm@45890
   490
    exhaust = exhaust,
wenzelm@45890
   491
    nchotomy = nchotomy,
wenzelm@45890
   492
    rec_names = rec_names,
wenzelm@45890
   493
    rec_rewrites = rec_rewrites,
wenzelm@45890
   494
    case_name = case_name,
wenzelm@45890
   495
    case_rewrites = case_rewrites,
wenzelm@45890
   496
    case_cong = case_cong,
blanchet@57983
   497
    case_cong_weak = case_cong_weak,
wenzelm@45890
   498
    split = split,
wenzelm@45890
   499
    split_asm = split_asm});
wenzelm@45890
   500
wenzelm@45907
   501
in
wenzelm@45907
   502
wenzelm@52788
   503
fun derive_datatype_props config dt_names descr induct inject distinct thy2 =
wenzelm@45890
   504
  let
wenzelm@45890
   505
    val flat_descr = flat descr;
wenzelm@45890
   506
    val new_type_names = map Long_Name.base_name dt_names;
wenzelm@45890
   507
    val _ =
blanchet@58112
   508
      Old_Datatype_Aux.message config
wenzelm@45890
   509
        ("Deriving properties for datatype(s) " ^ commas_quote new_type_names);
wenzelm@45890
   510
wenzelm@45890
   511
    val (exhaust, thy3) = thy2
wenzelm@45907
   512
      |> prove_casedist_thms config new_type_names descr induct
blanchet@58112
   513
        (Old_Datatype_Data.mk_case_names_exhausts flat_descr dt_names);
wenzelm@45890
   514
    val (nchotomys, thy4) = thy3
wenzelm@45907
   515
      |> prove_nchotomys config new_type_names descr exhaust;
wenzelm@45890
   516
    val ((rec_names, rec_rewrites), thy5) = thy4
wenzelm@45907
   517
      |> prove_primrec_thms config new_type_names descr
blanchet@58112
   518
        (#inject o the o Symtab.lookup (Old_Datatype_Data.get_all thy4)) inject
blanchet@58112
   519
        (distinct,
blanchet@58112
   520
         Old_Datatype_Data.all_distincts thy2 (Old_Datatype_Aux.get_rec_types flat_descr)) induct;
wenzelm@45890
   521
    val ((case_rewrites, case_names), thy6) = thy5
wenzelm@45907
   522
      |> prove_case_thms config new_type_names descr rec_names rec_rewrites;
wenzelm@45890
   523
    val (case_congs, thy7) = thy6
wenzelm@45907
   524
      |> prove_case_congs new_type_names case_names descr nchotomys case_rewrites;
blanchet@57983
   525
    val (case_cong_weaks, thy8) = thy7
blanchet@57983
   526
      |> prove_case_cong_weaks new_type_names case_names descr;
wenzelm@45890
   527
    val (splits, thy9) = thy8
wenzelm@45907
   528
      |> prove_split_thms config new_type_names case_names descr
wenzelm@45907
   529
        inject distinct exhaust case_rewrites;
wenzelm@45890
   530
wenzelm@45890
   531
    val inducts = Project_Rule.projections (Proof_Context.init_global thy2) induct;
wenzelm@45890
   532
    val dt_infos =
wenzelm@45890
   533
      map_index
wenzelm@45890
   534
        (make_dt_info flat_descr induct inducts rec_names rec_rewrites)
wenzelm@45890
   535
        (hd descr ~~ inject ~~ distinct ~~ exhaust ~~ nchotomys ~~
blanchet@57983
   536
          case_names ~~ case_rewrites ~~ case_congs ~~ case_cong_weaks ~~ splits);
wenzelm@45890
   537
    val dt_names = map fst dt_infos;
wenzelm@45890
   538
    val prfx = Binding.qualify true (space_implode "_" new_type_names);
wenzelm@45890
   539
    val simps = flat (inject @ distinct @ case_rewrites) @ rec_rewrites;
wenzelm@45901
   540
    val named_rules = flat (map_index (fn (i, tname) =>
wenzelm@45901
   541
      [((Binding.empty, [Induct.induct_type tname]), [([nth inducts i], [])]),
wenzelm@45901
   542
       ((Binding.empty, [Induct.cases_type tname]), [([nth exhaust i], [])])]) dt_names);
wenzelm@45890
   543
    val unnamed_rules = map (fn induct =>
wenzelm@45901
   544
      ((Binding.empty, [Rule_Cases.inner_rule, Induct.induct_type ""]), [([induct], [])]))
wenzelm@45890
   545
        (drop (length dt_names) inducts);
traytel@51673
   546
traytel@51673
   547
    val ctxt = Proof_Context.init_global thy9;
wenzelm@55954
   548
    val case_combs =
wenzelm@56002
   549
      map (Proof_Context.read_const {proper = true, strict = true} ctxt) case_names;
traytel@51673
   550
    val constrss = map (fn (dtname, {descr, index, ...}) =>
wenzelm@56002
   551
      map (Proof_Context.read_const {proper = true, strict = true} ctxt o fst)
wenzelm@55954
   552
        (#3 (the (AList.lookup op = descr index)))) dt_infos;
wenzelm@45890
   553
  in
wenzelm@45890
   554
    thy9
wenzelm@45901
   555
    |> Global_Theory.note_thmss ""
wenzelm@45901
   556
      ([((prfx (Binding.name "simps"), []), [(simps, [])]),
wenzelm@45901
   557
        ((prfx (Binding.name "inducts"), []), [(inducts, [])]),
wenzelm@45901
   558
        ((prfx (Binding.name "splits"), []), [(maps (fn (x, y) => [x, y]) splits, [])]),
wenzelm@45901
   559
        ((Binding.empty, [Simplifier.simp_add]),
wenzelm@45901
   560
          [(flat case_rewrites @ flat distinct @ rec_rewrites, [])]),
wenzelm@45901
   561
        ((Binding.empty, [Code.add_default_eqn_attribute]), [(rec_rewrites, [])]),
wenzelm@45901
   562
        ((Binding.empty, [iff_add]), [(flat inject, [])]),
wenzelm@45901
   563
        ((Binding.empty, [Classical.safe_elim NONE]),
wenzelm@45901
   564
          [(map (fn th => th RS notE) (flat distinct), [])]),
blanchet@57983
   565
        ((Binding.empty, [Simplifier.cong_add]), [(case_cong_weaks, [])]),
wenzelm@45901
   566
        ((Binding.empty, [Induct.induct_simp_add]), [(flat (distinct @ inject), [])])] @
wenzelm@45890
   567
          named_rules @ unnamed_rules)
wenzelm@45890
   568
    |> snd
blanchet@58112
   569
    |> Old_Datatype_Data.register dt_infos
traytel@51673
   570
    |> Context.theory_map (fold2 Case_Translation.register case_combs constrss)
blanchet@58112
   571
    |> Old_Datatype_Data.interpretation_data (config, dt_names)
wenzelm@45890
   572
    |> pair dt_names
wenzelm@45890
   573
  end;
wenzelm@45890
   574
wenzelm@45907
   575
end;
wenzelm@45907
   576
wenzelm@45890
   577
wenzelm@45890
   578
wenzelm@45890
   579
(** declare existing type as datatype **)
wenzelm@45890
   580
wenzelm@45890
   581
local
wenzelm@45890
   582
wenzelm@45890
   583
fun prove_rep_datatype config dt_names descr raw_inject half_distinct raw_induct thy1 =
wenzelm@45890
   584
  let
wenzelm@45890
   585
    val raw_distinct = (map o maps) (fn thm => [thm, thm RS not_sym]) half_distinct;
wenzelm@45890
   586
    val new_type_names = map Long_Name.base_name dt_names;
wenzelm@45890
   587
    val prfx = Binding.qualify true (space_implode "_" new_type_names);
wenzelm@45901
   588
    val (((inject, distinct), [(_, [induct])]), thy2) =
wenzelm@45890
   589
      thy1
blanchet@58112
   590
      |> Old_Datatype_Aux.store_thmss "inject" new_type_names raw_inject
blanchet@58112
   591
      ||>> Old_Datatype_Aux.store_thmss "distinct" new_type_names raw_distinct
wenzelm@45901
   592
      ||>> Global_Theory.note_thmss ""
blanchet@58112
   593
        [((prfx (Binding.name "induct"), [Old_Datatype_Data.mk_case_names_induct descr]),
wenzelm@45901
   594
          [([raw_induct], [])])];
wenzelm@45890
   595
  in
wenzelm@45890
   596
    thy2
wenzelm@45890
   597
    |> derive_datatype_props config dt_names [descr] induct inject distinct
wenzelm@45890
   598
 end;
wenzelm@45890
   599
wenzelm@45890
   600
fun gen_rep_datatype prep_term config after_qed raw_ts thy =
wenzelm@45890
   601
  let
wenzelm@45890
   602
    val ctxt = Proof_Context.init_global thy;
wenzelm@45890
   603
wenzelm@45890
   604
    fun constr_of_term (Const (c, T)) = (c, T)
wenzelm@45890
   605
      | constr_of_term t = error ("Not a constant: " ^ Syntax.string_of_term ctxt t);
wenzelm@45890
   606
    fun no_constr (c, T) =
wenzelm@55304
   607
      error ("Bad constructor: " ^ Proof_Context.markup_const ctxt c ^ "::" ^
wenzelm@45890
   608
        Syntax.string_of_typ ctxt T);
wenzelm@45890
   609
    fun type_of_constr (cT as (_, T)) =
wenzelm@45890
   610
      let
wenzelm@45890
   611
        val frees = Term.add_tfreesT T [];
wenzelm@45890
   612
        val (tyco, vs) = (apsnd o map) dest_TFree (dest_Type (body_type T))
wenzelm@45890
   613
          handle TYPE _ => no_constr cT
wenzelm@45890
   614
        val _ = if has_duplicates (eq_fst (op =)) vs then no_constr cT else ();
wenzelm@45890
   615
        val _ = if length frees <> length vs then no_constr cT else ();
wenzelm@45890
   616
      in (tyco, (vs, cT)) end;
wenzelm@45890
   617
wenzelm@45890
   618
    val raw_cs =
wenzelm@45890
   619
      AList.group (op =) (map (type_of_constr o constr_of_term o prep_term thy) raw_ts);
wenzelm@45890
   620
    val _ =
wenzelm@45890
   621
      (case map_filter (fn (tyco, _) =>
blanchet@58112
   622
          if Symtab.defined (Old_Datatype_Data.get_all thy) tyco then SOME tyco else NONE) raw_cs of
wenzelm@45890
   623
        [] => ()
blanchet@49020
   624
      | tycos => error ("Type(s) " ^ commas_quote tycos ^ " already represented inductively"));
wenzelm@45890
   625
    val raw_vss = maps (map (map snd o fst) o snd) raw_cs;
wenzelm@45890
   626
    val ms =
wenzelm@45890
   627
      (case distinct (op =) (map length raw_vss) of
wenzelm@45890
   628
         [n] => 0 upto n - 1
wenzelm@45890
   629
      | _ => error "Different types in given constructors");
wenzelm@45890
   630
    fun inter_sort m =
wenzelm@45890
   631
      map (fn xs => nth xs m) raw_vss
wenzelm@45890
   632
      |> foldr1 (Sorts.inter_sort (Sign.classes_of thy));
wenzelm@45890
   633
    val sorts = map inter_sort ms;
wenzelm@45890
   634
    val vs = Name.invent_names Name.context Name.aT sorts;
wenzelm@45890
   635
wenzelm@45890
   636
    fun norm_constr (raw_vs, (c, T)) =
wenzelm@45890
   637
      (c, map_atyps
wenzelm@45890
   638
        (TFree o (the o AList.lookup (op =) (map fst raw_vs ~~ vs)) o fst o dest_TFree) T);
wenzelm@45890
   639
wenzelm@45890
   640
    val cs = map (apsnd (map norm_constr)) raw_cs;
blanchet@58112
   641
    val dtyps_of_typ = map (Old_Datatype_Aux.dtyp_of_typ (map (rpair vs o fst) cs)) o binder_types;
wenzelm@45890
   642
    val dt_names = map fst cs;
wenzelm@45890
   643
wenzelm@45890
   644
    fun mk_spec (i, (tyco, constr)) =
blanchet@58112
   645
      (i, (tyco, map Old_Datatype_Aux.DtTFree vs, (map o apsnd) dtyps_of_typ constr));
wenzelm@45890
   646
    val descr = map_index mk_spec cs;
blanchet@58112
   647
    val injs = Old_Datatype_Prop.make_injs [descr];
blanchet@58112
   648
    val half_distincts = Old_Datatype_Prop.make_distincts [descr];
blanchet@58112
   649
    val ind = Old_Datatype_Prop.make_ind [descr];
wenzelm@45890
   650
    val rules = (map o map o map) Logic.close_form [[[ind]], injs, half_distincts];
wenzelm@45890
   651
wenzelm@45890
   652
    fun after_qed' raw_thms =
wenzelm@45890
   653
      let
wenzelm@45890
   654
        val [[[raw_induct]], raw_inject, half_distinct] =
wenzelm@45890
   655
          unflat rules (map Drule.zero_var_indexes_list raw_thms);
wenzelm@45890
   656
            (*FIXME somehow dubious*)
wenzelm@45890
   657
      in
wenzelm@45890
   658
        Proof_Context.background_theory_result  (* FIXME !? *)
wenzelm@45890
   659
          (prove_rep_datatype config dt_names descr raw_inject half_distinct raw_induct)
wenzelm@45890
   660
        #-> after_qed
wenzelm@45890
   661
      end;
wenzelm@45890
   662
  in
wenzelm@45890
   663
    ctxt
wenzelm@45890
   664
    |> Proof.theorem NONE after_qed' ((map o map) (rpair []) (flat rules))
wenzelm@45890
   665
  end;
wenzelm@45890
   666
wenzelm@45890
   667
in
wenzelm@45890
   668
wenzelm@45890
   669
val rep_datatype = gen_rep_datatype Sign.cert_term;
wenzelm@45890
   670
val rep_datatype_cmd = gen_rep_datatype Syntax.read_term_global;
wenzelm@45890
   671
wenzelm@45890
   672
end;
wenzelm@45890
   673
wenzelm@45890
   674
wenzelm@45890
   675
(* outer syntax *)
wenzelm@45890
   676
wenzelm@45890
   677
val _ =
blanchet@58306
   678
  Outer_Syntax.command @{command_spec "old_rep_datatype"}
blanchet@58306
   679
    "register existing types as old-style datatypes"
wenzelm@45890
   680
    (Scan.repeat1 Parse.term >> (fn ts =>
blanchet@58112
   681
      Toplevel.theory_to_proof (rep_datatype_cmd Old_Datatype_Aux.default_config (K I) ts)));
wenzelm@45890
   682
wenzelm@45890
   683
end;