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