src/HOL/Tools/Old_Datatype/old_rep_datatype.ML
author blanchet
Thu Sep 11 18:54:36 2014 +0200 (2014-09-11)
changeset 58306 117ba6cbe414
parent 58112 8081087096ad
child 58839 ccda99401bc8
permissions -rw-r--r--
renamed 'rep_datatype' to 'old_rep_datatype' (HOL)
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@45907
    60
              [rtac induct' 1,
wenzelm@45907
    61
               REPEAT (rtac TrueI 1),
wenzelm@45907
    62
               REPEAT ((rtac impI 1) THEN (eresolve_tac prems 1)),
wenzelm@45907
    63
               REPEAT (rtac 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@55990
   179
                REPEAT (etac @{thm ex1E} 1), rtac @{thm ex1I} 1,
wenzelm@45907
   180
                DEPTH_SOLVE_1 (ares_tac [intr] 1),
wenzelm@45907
   181
                REPEAT_DETERM_N k (etac thin_rl 1 THEN rotate_tac 1 1),
wenzelm@45907
   182
                etac elim 1,
wenzelm@45907
   183
                REPEAT_DETERM_N j distinct_tac,
wenzelm@45907
   184
                TRY (dresolve_tac inject 1),
wenzelm@51798
   185
                REPEAT (etac conjE 1), hyp_subst_tac ctxt 1,
wenzelm@45907
   186
                REPEAT (EVERY [etac allE 1, dtac mp 1, atac 1]),
wenzelm@51798
   187
                TRY (hyp_subst_tac ctxt 1),
wenzelm@45907
   188
                rtac 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@54742
   214
              (((rtac 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@45907
   257
             rtac @{thm the1_equality} 1,
wenzelm@45907
   258
             resolve_tac rec_unique_thms 1,
wenzelm@45907
   259
             resolve_tac rec_intrs 1,
wenzelm@45907
   260
             REPEAT (rtac 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, ...} =>
blanchet@55402
   341
        EVERY [rewrite_goals_tac ctxt (case_defs @ map mk_meta_eq primrec_thms), rtac refl 1]);
wenzelm@45907
   342
blanchet@55403
   343
    fun prove_cases (Type (Tcon, _)) ts =
blanchet@55403
   344
      (case Ctr_Sugar.ctr_sugar_of ctxt Tcon of
blanchet@55403
   345
        SOME {case_thms, ...} => case_thms
blanchet@55403
   346
      | NONE => map prove_case ts);
blanchet@55403
   347
wenzelm@45907
   348
    val case_thms =
blanchet@58112
   349
      map2 prove_cases newTs (Old_Datatype_Prop.make_cases case_names0 descr thy2);
blanchet@55403
   350
blanchet@55403
   351
    fun case_name_of (th :: _) =
blanchet@55403
   352
      fst (dest_Const (head_of (fst (HOLogic.dest_eq (HOLogic.dest_Trueprop (prop_of th))))));
blanchet@55403
   353
blanchet@55403
   354
    val case_names = map case_name_of case_thms;
wenzelm@45907
   355
  in
wenzelm@45907
   356
    thy2
wenzelm@57964
   357
    |> Context.theory_map
wenzelm@57964
   358
        ((fold o fold) (Named_Theorems.add_thm @{named_theorems nitpick_simp}) case_thms)
wenzelm@45907
   359
    |> Sign.parent_path
blanchet@58112
   360
    |> Old_Datatype_Aux.store_thmss "case" new_type_names case_thms
wenzelm@45907
   361
    |-> (fn thmss => pair (thmss, case_names))
wenzelm@45907
   362
  end;
wenzelm@45907
   363
wenzelm@45907
   364
wenzelm@45907
   365
(* case splitting *)
wenzelm@45907
   366
blanchet@58112
   367
fun prove_split_thms (config : Old_Datatype_Aux.config)
wenzelm@45907
   368
    new_type_names case_names descr constr_inject dist_rewrites casedist_thms case_thms thy =
wenzelm@45907
   369
  let
blanchet@58112
   370
    val _ = Old_Datatype_Aux.message config "Proving equations for case splitting ...";
wenzelm@45907
   371
wenzelm@45907
   372
    val descr' = flat descr;
blanchet@58112
   373
    val recTs = Old_Datatype_Aux.get_rec_types descr';
wenzelm@45907
   374
    val newTs = take (length (hd descr)) recTs;
wenzelm@45907
   375
wenzelm@45907
   376
    fun prove_split_thms ((((((t1, t2), inject), dist_rewrites'), exhaustion), case_thms'), T) =
wenzelm@45907
   377
      let
wenzelm@45907
   378
        val cert = cterm_of thy;
wenzelm@45907
   379
        val _ $ (_ $ lhs $ _) = hd (Logic.strip_assums_hyp (hd (prems_of exhaustion)));
wenzelm@45907
   380
        val exhaustion' = cterm_instantiate [(cert lhs, cert (Free ("x", T)))] exhaustion;
wenzelm@51717
   381
        fun tac ctxt =
wenzelm@45907
   382
          EVERY [rtac exhaustion' 1,
wenzelm@51717
   383
            ALLGOALS (asm_simp_tac
wenzelm@51717
   384
              (put_simpset HOL_ss ctxt addsimps (dist_rewrites' @ inject @ case_thms')))];
wenzelm@45907
   385
      in
wenzelm@51717
   386
        (Goal.prove_sorry_global thy [] [] t1 (tac o #context),
wenzelm@51717
   387
         Goal.prove_sorry_global thy [] [] t2 (tac o #context))
wenzelm@45907
   388
      end;
wenzelm@45907
   389
wenzelm@45907
   390
    val split_thm_pairs =
wenzelm@45907
   391
      map prove_split_thms
blanchet@58112
   392
        (Old_Datatype_Prop.make_splits case_names descr thy ~~ constr_inject ~~
wenzelm@45907
   393
          dist_rewrites ~~ casedist_thms ~~ case_thms ~~ newTs);
wenzelm@45907
   394
wenzelm@45907
   395
    val (split_thms, split_asm_thms) = split_list split_thm_pairs
wenzelm@45907
   396
wenzelm@45907
   397
  in
wenzelm@45907
   398
    thy
blanchet@58112
   399
    |> Old_Datatype_Aux.store_thms "split" new_type_names split_thms
blanchet@58112
   400
    ||>> Old_Datatype_Aux.store_thms "split_asm" new_type_names split_asm_thms
wenzelm@45907
   401
    |-> (fn (thms1, thms2) => pair (thms1 ~~ thms2))
wenzelm@45907
   402
  end;
wenzelm@45907
   403
blanchet@57983
   404
fun prove_case_cong_weaks new_type_names case_names descr thy =
wenzelm@45907
   405
  let
blanchet@57983
   406
    fun prove_case_cong_weak t =
wenzelm@51551
   407
     Goal.prove_sorry_global thy [] (Logic.strip_imp_prems t) (Logic.strip_imp_concl t)
wenzelm@45907
   408
       (fn {prems, ...} => EVERY [rtac (hd prems RS arg_cong) 1]);
wenzelm@45907
   409
blanchet@57983
   410
    val case_cong_weaks =
blanchet@58112
   411
      map prove_case_cong_weak (Old_Datatype_Prop.make_case_cong_weaks case_names descr thy);
wenzelm@45907
   412
blanchet@58112
   413
  in thy |> Old_Datatype_Aux.store_thms "case_cong_weak" new_type_names case_cong_weaks end;
wenzelm@45907
   414
wenzelm@45907
   415
wenzelm@45907
   416
(* additional theorems for TFL *)
wenzelm@45907
   417
blanchet@58112
   418
fun prove_nchotomys (config : Old_Datatype_Aux.config) new_type_names descr casedist_thms thy =
wenzelm@45907
   419
  let
blanchet@58112
   420
    val _ = Old_Datatype_Aux.message config "Proving additional theorems for TFL ...";
wenzelm@45907
   421
wenzelm@45907
   422
    fun prove_nchotomy (t, exhaustion) =
wenzelm@45907
   423
      let
wenzelm@45907
   424
        (* For goal i, select the correct disjunct to attack, then prove it *)
wenzelm@51798
   425
        fun tac ctxt i 0 =
wenzelm@51798
   426
              EVERY [TRY (rtac disjI1 i), hyp_subst_tac ctxt i, REPEAT (rtac exI i), rtac refl i]
wenzelm@51798
   427
          | tac ctxt i n = rtac disjI2 i THEN tac ctxt i (n - 1);
wenzelm@45907
   428
      in
wenzelm@51551
   429
        Goal.prove_sorry_global thy [] [] t
wenzelm@51798
   430
          (fn {context = ctxt, ...} =>
wenzelm@45907
   431
            EVERY [rtac allI 1,
blanchet@58112
   432
             Old_Datatype_Aux.exh_tac (K exhaustion) 1,
wenzelm@51798
   433
             ALLGOALS (fn i => tac ctxt i (i - 1))])
wenzelm@45907
   434
      end;
wenzelm@45907
   435
wenzelm@45907
   436
    val nchotomys =
blanchet@58112
   437
      map prove_nchotomy (Old_Datatype_Prop.make_nchotomys descr ~~ casedist_thms);
wenzelm@45907
   438
blanchet@58112
   439
  in thy |> Old_Datatype_Aux.store_thms "nchotomy" new_type_names nchotomys end;
wenzelm@45907
   440
wenzelm@45907
   441
fun prove_case_congs new_type_names case_names descr nchotomys case_thms thy =
wenzelm@45907
   442
  let
wenzelm@45907
   443
    fun prove_case_cong ((t, nchotomy), case_rewrites) =
wenzelm@45907
   444
      let
wenzelm@56245
   445
        val Const (@{const_name Pure.imp}, _) $ tm $ _ = t;
wenzelm@45907
   446
        val Const (@{const_name Trueprop}, _) $ (Const (@{const_name HOL.eq}, _) $ _ $ Ma) = tm;
wenzelm@45907
   447
        val cert = cterm_of thy;
wenzelm@45907
   448
        val nchotomy' = nchotomy RS spec;
wenzelm@45907
   449
        val [v] = Term.add_vars (concl_of nchotomy') [];
wenzelm@45907
   450
        val nchotomy'' = cterm_instantiate [(cert (Var v), cert Ma)] nchotomy';
wenzelm@45907
   451
      in
wenzelm@51551
   452
        Goal.prove_sorry_global thy [] (Logic.strip_imp_prems t) (Logic.strip_imp_concl t)
wenzelm@51717
   453
          (fn {context = ctxt, prems, ...} =>
wenzelm@51717
   454
            let
wenzelm@51717
   455
              val simplify = asm_simp_tac (put_simpset HOL_ss ctxt addsimps (prems @ case_rewrites))
wenzelm@51717
   456
            in
wenzelm@45907
   457
              EVERY [
wenzelm@51717
   458
                simp_tac (put_simpset HOL_ss ctxt addsimps [hd prems]) 1,
wenzelm@46708
   459
                cut_tac nchotomy'' 1,
wenzelm@45907
   460
                REPEAT (etac disjE 1 THEN REPEAT (etac exE 1) THEN simplify 1),
wenzelm@45907
   461
                REPEAT (etac exE 1) THEN simplify 1 (* Get last disjunct *)]
wenzelm@45907
   462
            end)
wenzelm@45907
   463
      end;
wenzelm@45907
   464
wenzelm@45907
   465
    val case_congs =
wenzelm@45907
   466
      map prove_case_cong
blanchet@58112
   467
        (Old_Datatype_Prop.make_case_congs case_names descr thy ~~ nchotomys ~~ case_thms);
wenzelm@45907
   468
blanchet@58112
   469
  in thy |> Old_Datatype_Aux.store_thms "case_cong" new_type_names case_congs end;
wenzelm@45907
   470
wenzelm@45907
   471
wenzelm@45907
   472
wenzelm@45907
   473
(** derive datatype props **)
wenzelm@45907
   474
wenzelm@45907
   475
local
wenzelm@45907
   476
wenzelm@45890
   477
fun make_dt_info descr induct inducts rec_names rec_rewrites
wenzelm@45890
   478
    (index, (((((((((((_, (tname, _, _))), inject), distinct),
blanchet@57983
   479
      exhaust), nchotomy), case_name), case_rewrites), case_cong), case_cong_weak),
wenzelm@45890
   480
        (split, split_asm))) =
wenzelm@45890
   481
  (tname,
wenzelm@45890
   482
   {index = index,
wenzelm@45890
   483
    descr = descr,
wenzelm@45890
   484
    inject = inject,
wenzelm@45890
   485
    distinct = distinct,
wenzelm@45890
   486
    induct = induct,
wenzelm@45890
   487
    inducts = inducts,
wenzelm@45890
   488
    exhaust = exhaust,
wenzelm@45890
   489
    nchotomy = nchotomy,
wenzelm@45890
   490
    rec_names = rec_names,
wenzelm@45890
   491
    rec_rewrites = rec_rewrites,
wenzelm@45890
   492
    case_name = case_name,
wenzelm@45890
   493
    case_rewrites = case_rewrites,
wenzelm@45890
   494
    case_cong = case_cong,
blanchet@57983
   495
    case_cong_weak = case_cong_weak,
wenzelm@45890
   496
    split = split,
wenzelm@45890
   497
    split_asm = split_asm});
wenzelm@45890
   498
wenzelm@45907
   499
in
wenzelm@45907
   500
wenzelm@52788
   501
fun derive_datatype_props config dt_names descr induct inject distinct thy2 =
wenzelm@45890
   502
  let
wenzelm@45890
   503
    val flat_descr = flat descr;
wenzelm@45890
   504
    val new_type_names = map Long_Name.base_name dt_names;
wenzelm@45890
   505
    val _ =
blanchet@58112
   506
      Old_Datatype_Aux.message config
wenzelm@45890
   507
        ("Deriving properties for datatype(s) " ^ commas_quote new_type_names);
wenzelm@45890
   508
wenzelm@45890
   509
    val (exhaust, thy3) = thy2
wenzelm@45907
   510
      |> prove_casedist_thms config new_type_names descr induct
blanchet@58112
   511
        (Old_Datatype_Data.mk_case_names_exhausts flat_descr dt_names);
wenzelm@45890
   512
    val (nchotomys, thy4) = thy3
wenzelm@45907
   513
      |> prove_nchotomys config new_type_names descr exhaust;
wenzelm@45890
   514
    val ((rec_names, rec_rewrites), thy5) = thy4
wenzelm@45907
   515
      |> prove_primrec_thms config new_type_names descr
blanchet@58112
   516
        (#inject o the o Symtab.lookup (Old_Datatype_Data.get_all thy4)) inject
blanchet@58112
   517
        (distinct,
blanchet@58112
   518
         Old_Datatype_Data.all_distincts thy2 (Old_Datatype_Aux.get_rec_types flat_descr)) induct;
wenzelm@45890
   519
    val ((case_rewrites, case_names), thy6) = thy5
wenzelm@45907
   520
      |> prove_case_thms config new_type_names descr rec_names rec_rewrites;
wenzelm@45890
   521
    val (case_congs, thy7) = thy6
wenzelm@45907
   522
      |> prove_case_congs new_type_names case_names descr nchotomys case_rewrites;
blanchet@57983
   523
    val (case_cong_weaks, thy8) = thy7
blanchet@57983
   524
      |> prove_case_cong_weaks new_type_names case_names descr;
wenzelm@45890
   525
    val (splits, thy9) = thy8
wenzelm@45907
   526
      |> prove_split_thms config new_type_names case_names descr
wenzelm@45907
   527
        inject distinct exhaust case_rewrites;
wenzelm@45890
   528
wenzelm@45890
   529
    val inducts = Project_Rule.projections (Proof_Context.init_global thy2) induct;
wenzelm@45890
   530
    val dt_infos =
wenzelm@45890
   531
      map_index
wenzelm@45890
   532
        (make_dt_info flat_descr induct inducts rec_names rec_rewrites)
wenzelm@45890
   533
        (hd descr ~~ inject ~~ distinct ~~ exhaust ~~ nchotomys ~~
blanchet@57983
   534
          case_names ~~ case_rewrites ~~ case_congs ~~ case_cong_weaks ~~ splits);
wenzelm@45890
   535
    val dt_names = map fst dt_infos;
wenzelm@45890
   536
    val prfx = Binding.qualify true (space_implode "_" new_type_names);
wenzelm@45890
   537
    val simps = flat (inject @ distinct @ case_rewrites) @ rec_rewrites;
wenzelm@45901
   538
    val named_rules = flat (map_index (fn (i, tname) =>
wenzelm@45901
   539
      [((Binding.empty, [Induct.induct_type tname]), [([nth inducts i], [])]),
wenzelm@45901
   540
       ((Binding.empty, [Induct.cases_type tname]), [([nth exhaust i], [])])]) dt_names);
wenzelm@45890
   541
    val unnamed_rules = map (fn induct =>
wenzelm@45901
   542
      ((Binding.empty, [Rule_Cases.inner_rule, Induct.induct_type ""]), [([induct], [])]))
wenzelm@45890
   543
        (drop (length dt_names) inducts);
traytel@51673
   544
traytel@51673
   545
    val ctxt = Proof_Context.init_global thy9;
wenzelm@55954
   546
    val case_combs =
wenzelm@56002
   547
      map (Proof_Context.read_const {proper = true, strict = true} ctxt) case_names;
traytel@51673
   548
    val constrss = map (fn (dtname, {descr, index, ...}) =>
wenzelm@56002
   549
      map (Proof_Context.read_const {proper = true, strict = true} ctxt o fst)
wenzelm@55954
   550
        (#3 (the (AList.lookup op = descr index)))) dt_infos;
wenzelm@45890
   551
  in
wenzelm@45890
   552
    thy9
wenzelm@45901
   553
    |> Global_Theory.note_thmss ""
wenzelm@45901
   554
      ([((prfx (Binding.name "simps"), []), [(simps, [])]),
wenzelm@45901
   555
        ((prfx (Binding.name "inducts"), []), [(inducts, [])]),
wenzelm@45901
   556
        ((prfx (Binding.name "splits"), []), [(maps (fn (x, y) => [x, y]) splits, [])]),
wenzelm@45901
   557
        ((Binding.empty, [Simplifier.simp_add]),
wenzelm@45901
   558
          [(flat case_rewrites @ flat distinct @ rec_rewrites, [])]),
wenzelm@45901
   559
        ((Binding.empty, [Code.add_default_eqn_attribute]), [(rec_rewrites, [])]),
wenzelm@45901
   560
        ((Binding.empty, [iff_add]), [(flat inject, [])]),
wenzelm@45901
   561
        ((Binding.empty, [Classical.safe_elim NONE]),
wenzelm@45901
   562
          [(map (fn th => th RS notE) (flat distinct), [])]),
blanchet@57983
   563
        ((Binding.empty, [Simplifier.cong_add]), [(case_cong_weaks, [])]),
wenzelm@45901
   564
        ((Binding.empty, [Induct.induct_simp_add]), [(flat (distinct @ inject), [])])] @
wenzelm@45890
   565
          named_rules @ unnamed_rules)
wenzelm@45890
   566
    |> snd
blanchet@58112
   567
    |> Old_Datatype_Data.register dt_infos
traytel@51673
   568
    |> Context.theory_map (fold2 Case_Translation.register case_combs constrss)
blanchet@58112
   569
    |> Old_Datatype_Data.interpretation_data (config, dt_names)
wenzelm@45890
   570
    |> pair dt_names
wenzelm@45890
   571
  end;
wenzelm@45890
   572
wenzelm@45907
   573
end;
wenzelm@45907
   574
wenzelm@45890
   575
wenzelm@45890
   576
wenzelm@45890
   577
(** declare existing type as datatype **)
wenzelm@45890
   578
wenzelm@45890
   579
local
wenzelm@45890
   580
wenzelm@45890
   581
fun prove_rep_datatype config dt_names descr raw_inject half_distinct raw_induct thy1 =
wenzelm@45890
   582
  let
wenzelm@45890
   583
    val raw_distinct = (map o maps) (fn thm => [thm, thm RS not_sym]) half_distinct;
wenzelm@45890
   584
    val new_type_names = map Long_Name.base_name dt_names;
wenzelm@45890
   585
    val prfx = Binding.qualify true (space_implode "_" new_type_names);
wenzelm@45901
   586
    val (((inject, distinct), [(_, [induct])]), thy2) =
wenzelm@45890
   587
      thy1
blanchet@58112
   588
      |> Old_Datatype_Aux.store_thmss "inject" new_type_names raw_inject
blanchet@58112
   589
      ||>> Old_Datatype_Aux.store_thmss "distinct" new_type_names raw_distinct
wenzelm@45901
   590
      ||>> Global_Theory.note_thmss ""
blanchet@58112
   591
        [((prfx (Binding.name "induct"), [Old_Datatype_Data.mk_case_names_induct descr]),
wenzelm@45901
   592
          [([raw_induct], [])])];
wenzelm@45890
   593
  in
wenzelm@45890
   594
    thy2
wenzelm@45890
   595
    |> derive_datatype_props config dt_names [descr] induct inject distinct
wenzelm@45890
   596
 end;
wenzelm@45890
   597
wenzelm@45890
   598
fun gen_rep_datatype prep_term config after_qed raw_ts thy =
wenzelm@45890
   599
  let
wenzelm@45890
   600
    val ctxt = Proof_Context.init_global thy;
wenzelm@45890
   601
wenzelm@45890
   602
    fun constr_of_term (Const (c, T)) = (c, T)
wenzelm@45890
   603
      | constr_of_term t = error ("Not a constant: " ^ Syntax.string_of_term ctxt t);
wenzelm@45890
   604
    fun no_constr (c, T) =
wenzelm@55304
   605
      error ("Bad constructor: " ^ Proof_Context.markup_const ctxt c ^ "::" ^
wenzelm@45890
   606
        Syntax.string_of_typ ctxt T);
wenzelm@45890
   607
    fun type_of_constr (cT as (_, T)) =
wenzelm@45890
   608
      let
wenzelm@45890
   609
        val frees = Term.add_tfreesT T [];
wenzelm@45890
   610
        val (tyco, vs) = (apsnd o map) dest_TFree (dest_Type (body_type T))
wenzelm@45890
   611
          handle TYPE _ => no_constr cT
wenzelm@45890
   612
        val _ = if has_duplicates (eq_fst (op =)) vs then no_constr cT else ();
wenzelm@45890
   613
        val _ = if length frees <> length vs then no_constr cT else ();
wenzelm@45890
   614
      in (tyco, (vs, cT)) end;
wenzelm@45890
   615
wenzelm@45890
   616
    val raw_cs =
wenzelm@45890
   617
      AList.group (op =) (map (type_of_constr o constr_of_term o prep_term thy) raw_ts);
wenzelm@45890
   618
    val _ =
wenzelm@45890
   619
      (case map_filter (fn (tyco, _) =>
blanchet@58112
   620
          if Symtab.defined (Old_Datatype_Data.get_all thy) tyco then SOME tyco else NONE) raw_cs of
wenzelm@45890
   621
        [] => ()
blanchet@49020
   622
      | tycos => error ("Type(s) " ^ commas_quote tycos ^ " already represented inductively"));
wenzelm@45890
   623
    val raw_vss = maps (map (map snd o fst) o snd) raw_cs;
wenzelm@45890
   624
    val ms =
wenzelm@45890
   625
      (case distinct (op =) (map length raw_vss) of
wenzelm@45890
   626
         [n] => 0 upto n - 1
wenzelm@45890
   627
      | _ => error "Different types in given constructors");
wenzelm@45890
   628
    fun inter_sort m =
wenzelm@45890
   629
      map (fn xs => nth xs m) raw_vss
wenzelm@45890
   630
      |> foldr1 (Sorts.inter_sort (Sign.classes_of thy));
wenzelm@45890
   631
    val sorts = map inter_sort ms;
wenzelm@45890
   632
    val vs = Name.invent_names Name.context Name.aT sorts;
wenzelm@45890
   633
wenzelm@45890
   634
    fun norm_constr (raw_vs, (c, T)) =
wenzelm@45890
   635
      (c, map_atyps
wenzelm@45890
   636
        (TFree o (the o AList.lookup (op =) (map fst raw_vs ~~ vs)) o fst o dest_TFree) T);
wenzelm@45890
   637
wenzelm@45890
   638
    val cs = map (apsnd (map norm_constr)) raw_cs;
blanchet@58112
   639
    val dtyps_of_typ = map (Old_Datatype_Aux.dtyp_of_typ (map (rpair vs o fst) cs)) o binder_types;
wenzelm@45890
   640
    val dt_names = map fst cs;
wenzelm@45890
   641
wenzelm@45890
   642
    fun mk_spec (i, (tyco, constr)) =
blanchet@58112
   643
      (i, (tyco, map Old_Datatype_Aux.DtTFree vs, (map o apsnd) dtyps_of_typ constr));
wenzelm@45890
   644
    val descr = map_index mk_spec cs;
blanchet@58112
   645
    val injs = Old_Datatype_Prop.make_injs [descr];
blanchet@58112
   646
    val half_distincts = Old_Datatype_Prop.make_distincts [descr];
blanchet@58112
   647
    val ind = Old_Datatype_Prop.make_ind [descr];
wenzelm@45890
   648
    val rules = (map o map o map) Logic.close_form [[[ind]], injs, half_distincts];
wenzelm@45890
   649
wenzelm@45890
   650
    fun after_qed' raw_thms =
wenzelm@45890
   651
      let
wenzelm@45890
   652
        val [[[raw_induct]], raw_inject, half_distinct] =
wenzelm@45890
   653
          unflat rules (map Drule.zero_var_indexes_list raw_thms);
wenzelm@45890
   654
            (*FIXME somehow dubious*)
wenzelm@45890
   655
      in
wenzelm@45890
   656
        Proof_Context.background_theory_result  (* FIXME !? *)
wenzelm@45890
   657
          (prove_rep_datatype config dt_names descr raw_inject half_distinct raw_induct)
wenzelm@45890
   658
        #-> after_qed
wenzelm@45890
   659
      end;
wenzelm@45890
   660
  in
wenzelm@45890
   661
    ctxt
wenzelm@45890
   662
    |> Proof.theorem NONE after_qed' ((map o map) (rpair []) (flat rules))
wenzelm@45890
   663
  end;
wenzelm@45890
   664
wenzelm@45890
   665
in
wenzelm@45890
   666
wenzelm@45890
   667
val rep_datatype = gen_rep_datatype Sign.cert_term;
wenzelm@45890
   668
val rep_datatype_cmd = gen_rep_datatype Syntax.read_term_global;
wenzelm@45890
   669
wenzelm@45890
   670
end;
wenzelm@45890
   671
wenzelm@45890
   672
wenzelm@45890
   673
(* outer syntax *)
wenzelm@45890
   674
wenzelm@45890
   675
val _ =
blanchet@58306
   676
  Outer_Syntax.command @{command_spec "old_rep_datatype"}
blanchet@58306
   677
    "register existing types as old-style datatypes"
wenzelm@45890
   678
    (Scan.repeat1 Parse.term >> (fn ts =>
blanchet@58112
   679
      Toplevel.theory_to_proof (rep_datatype_cmd Old_Datatype_Aux.default_config (K I) ts)));
wenzelm@45890
   680
wenzelm@45890
   681
end;