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