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