src/HOL/Tools/Datatype/datatype_abs_proofs.ML
author haftmann
Sat Aug 28 16:14:32 2010 +0200 (2010-08-28)
changeset 38864 4abe644fcea5
parent 35625 9c818cab0dd0
child 39557 fe5722fce758
permissions -rw-r--r--
formerly unnamed infix equality now named HOL.eq
haftmann@33968
     1
(*  Title:      HOL/Tools/Datatype/datatype_abs_proofs.ML
wenzelm@11539
     2
    Author:     Stefan Berghofer, TU Muenchen
berghofe@5177
     3
haftmann@33968
     4
Datatype package: proofs and defintions independent of concrete
haftmann@33968
     5
representation of datatypes  (i.e. requiring only abstract
haftmann@33968
     6
properties: injectivity / distinctness of constructors and induction).
berghofe@5177
     7
*)
berghofe@5177
     8
berghofe@5177
     9
signature DATATYPE_ABS_PROOFS =
berghofe@5177
    10
sig
haftmann@31737
    11
  include DATATYPE_COMMON
haftmann@31737
    12
  val prove_casedist_thms : config -> string list ->
haftmann@31668
    13
    descr list -> (string * sort) list -> thm ->
wenzelm@18728
    14
    attribute list -> theory -> thm list * theory
haftmann@31737
    15
  val prove_primrec_thms : config -> string list ->
haftmann@31668
    16
    descr list -> (string * sort) list ->
haftmann@32915
    17
      (string -> thm list) -> thm list list -> thm list list * thm list list ->
haftmann@32915
    18
        thm -> theory -> (string list * thm list) * theory
haftmann@31737
    19
  val prove_case_thms : config -> string list ->
haftmann@31668
    20
    descr list -> (string * sort) list ->
haftmann@18314
    21
      string list -> thm list -> theory -> (thm list list * string list) * theory
haftmann@31737
    22
  val prove_split_thms : config -> string list ->
haftmann@31668
    23
    descr list -> (string * sort) list ->
berghofe@5177
    24
      thm list list -> thm list list -> thm list -> thm list list -> theory ->
haftmann@18314
    25
        (thm * thm) list * theory
haftmann@31737
    26
  val prove_nchotomys : config -> string list -> descr list ->
haftmann@18314
    27
    (string * sort) list -> thm list -> theory -> thm list * theory
haftmann@31668
    28
  val prove_weak_case_congs : string list -> descr list ->
haftmann@18314
    29
    (string * sort) list -> theory -> thm list * theory
berghofe@13641
    30
  val prove_case_congs : string list ->
haftmann@31668
    31
    descr list -> (string * sort) list ->
haftmann@18314
    32
      thm list -> thm list list -> theory -> thm list * theory
berghofe@5177
    33
end;
berghofe@5177
    34
haftmann@33968
    35
structure Datatype_Abs_Proofs: DATATYPE_ABS_PROOFS =
berghofe@5177
    36
struct
berghofe@5177
    37
haftmann@33968
    38
open Datatype_Aux;
berghofe@5177
    39
berghofe@5177
    40
(************************ case distinction theorems ***************************)
berghofe@5177
    41
haftmann@31737
    42
fun prove_casedist_thms (config : config) new_type_names descr sorts induct case_names_exhausts thy =
berghofe@5177
    43
  let
haftmann@31668
    44
    val _ = message config "Proving case distinction theorems ...";
berghofe@5177
    45
wenzelm@32952
    46
    val descr' = flat descr;
berghofe@5177
    47
    val recTs = get_rec_types descr' sorts;
haftmann@33957
    48
    val newTs = take (length (hd descr)) recTs;
berghofe@5177
    49
berghofe@8477
    50
    val {maxidx, ...} = rep_thm induct;
wenzelm@8305
    51
    val induct_Ps = map head_of (HOLogic.dest_conj (HOLogic.dest_Trueprop (concl_of induct)));
berghofe@5177
    52
haftmann@33063
    53
    fun prove_casedist_thm (i, (T, t)) =
berghofe@5177
    54
      let
berghofe@5177
    55
        val dummyPs = map (fn (Var (_, Type (_, [T', T'']))) =>
wenzelm@35364
    56
          Abs ("z", T', Const (@{const_name True}, T''))) induct_Ps;
berghofe@8477
    57
        val P = Abs ("z", T, HOLogic.imp $ HOLogic.mk_eq (Var (("a", maxidx+1), T), Bound 0) $
berghofe@5177
    58
          Var (("P", 0), HOLogic.boolT))
haftmann@33957
    59
        val insts = take i dummyPs @ (P::(drop (i + 1) dummyPs));
wenzelm@17985
    60
        val cert = cterm_of thy;
berghofe@5177
    61
        val insts' = (map cert induct_Ps) ~~ (map cert insts);
haftmann@32905
    62
        val induct' = refl RS ((nth
haftmann@32905
    63
          (split_conj_thm (cterm_instantiate insts' induct)) i) RSN (2, rev_mp))
berghofe@5177
    64
wenzelm@17985
    65
      in
wenzelm@32970
    66
        Skip_Proof.prove_global thy [] (Logic.strip_imp_prems t) (Logic.strip_imp_concl t)
wenzelm@26711
    67
          (fn {prems, ...} => EVERY
wenzelm@17985
    68
            [rtac induct' 1,
wenzelm@17985
    69
             REPEAT (rtac TrueI 1),
wenzelm@17985
    70
             REPEAT ((rtac impI 1) THEN (eresolve_tac prems 1)),
wenzelm@20046
    71
             REPEAT (rtac TrueI 1)])
berghofe@5177
    72
      end;
berghofe@5177
    73
haftmann@33063
    74
    val casedist_thms = map_index prove_casedist_thm
haftmann@33968
    75
      (newTs ~~ Datatype_Prop.make_casedists descr sorts)
haftmann@18314
    76
  in
haftmann@18314
    77
    thy
haftmann@18314
    78
    |> store_thms_atts "exhaust" new_type_names (map single case_names_exhausts) casedist_thms
haftmann@18314
    79
  end;
berghofe@5177
    80
berghofe@5177
    81
berghofe@5177
    82
(*************************** primrec combinators ******************************)
berghofe@5177
    83
haftmann@31737
    84
fun prove_primrec_thms (config : config) new_type_names descr sorts
haftmann@32915
    85
    injects_of constr_inject (dist_rewrites, other_dist_rewrites) induct thy =
berghofe@5177
    86
  let
haftmann@31668
    87
    val _ = message config "Constructing primrec combinators ...";
berghofe@5661
    88
berghofe@5661
    89
    val big_name = space_implode "_" new_type_names;
haftmann@32124
    90
    val thy0 = Sign.add_path big_name thy;
berghofe@5177
    91
wenzelm@32952
    92
    val descr' = flat descr;
berghofe@5177
    93
    val recTs = get_rec_types descr' sorts;
wenzelm@30190
    94
    val used = List.foldr OldTerm.add_typ_tfree_names [] recTs;
haftmann@33957
    95
    val newTs = take (length (hd descr)) recTs;
berghofe@5177
    96
wenzelm@8305
    97
    val induct_Ps = map head_of (HOLogic.dest_conj (HOLogic.dest_Trueprop (concl_of induct)));
berghofe@5177
    98
berghofe@5661
    99
    val big_rec_name' = big_name ^ "_rec_set";
berghofe@21021
   100
    val rec_set_names' =
berghofe@21021
   101
      if length descr' = 1 then [big_rec_name'] else
berghofe@21021
   102
        map ((curry (op ^) (big_rec_name' ^ "_")) o string_of_int)
berghofe@21021
   103
          (1 upto (length descr'));
haftmann@28965
   104
    val rec_set_names = map (Sign.full_bname thy0) rec_set_names';
berghofe@5177
   105
haftmann@33968
   106
    val (rec_result_Ts, reccomb_fn_Ts) = Datatype_Prop.make_primrec_Ts descr sorts used;
berghofe@5177
   107
berghofe@21021
   108
    val rec_set_Ts = map (fn (T1, T2) =>
berghofe@21021
   109
      reccomb_fn_Ts @ [T1, T2] ---> HOLogic.boolT) (recTs ~~ rec_result_Ts);
berghofe@5177
   110
berghofe@5177
   111
    val rec_fns = map (uncurry (mk_Free "f"))
berghofe@5177
   112
      (reccomb_fn_Ts ~~ (1 upto (length reccomb_fn_Ts)));
berghofe@21021
   113
    val rec_sets' = map (fn c => list_comb (Free c, rec_fns))
berghofe@21021
   114
      (rec_set_names' ~~ rec_set_Ts);
berghofe@5177
   115
    val rec_sets = map (fn c => list_comb (Const c, rec_fns))
berghofe@5177
   116
      (rec_set_names ~~ rec_set_Ts);
berghofe@5177
   117
berghofe@5177
   118
    (* introduction rules for graph of primrec function *)
berghofe@5177
   119
haftmann@32906
   120
    fun make_rec_intr T rec_set (cname, cargs) (rec_intr_ts, l) =
berghofe@5177
   121
      let
wenzelm@33338
   122
        fun mk_prem (dt, U) (j, k, prems, t1s, t2s) =
berghofe@7015
   123
          let val free1 = mk_Free "x" U j
berghofe@13641
   124
          in (case (strip_dtyp dt, strip_type U) of
berghofe@13641
   125
             ((_, DtRec m), (Us, _)) =>
berghofe@13641
   126
               let
haftmann@32905
   127
                 val free2 = mk_Free "y" (Us ---> nth rec_result_Ts m) k;
berghofe@13641
   128
                 val i = length Us
berghofe@13641
   129
               in (j + 1, k + 1, HOLogic.mk_Trueprop (HOLogic.list_all
haftmann@32905
   130
                     (map (pair "x") Us, nth rec_sets' m $
berghofe@21021
   131
                       app_bnds free1 i $ app_bnds free2 i)) :: prems,
berghofe@5177
   132
                   free1::t1s, free2::t2s)
berghofe@5177
   133
               end
berghofe@5177
   134
           | _ => (j + 1, k, prems, free1::t1s, t2s))
berghofe@5177
   135
          end;
berghofe@5177
   136
berghofe@5177
   137
        val Ts = map (typ_of_dtyp descr' sorts) cargs;
wenzelm@33338
   138
        val (_, _, prems, t1s, t2s) = fold_rev mk_prem (cargs ~~ Ts) (1, 1, [], [], [])
berghofe@5177
   139
berghofe@21021
   140
      in (rec_intr_ts @ [Logic.list_implies (prems, HOLogic.mk_Trueprop
berghofe@21021
   141
        (rec_set $ list_comb (Const (cname, Ts ---> T), t1s) $
haftmann@32905
   142
          list_comb (nth rec_fns l, t1s @ t2s)))], l + 1)
berghofe@5177
   143
      end;
berghofe@5177
   144
haftmann@32906
   145
    val (rec_intr_ts, _) = fold (fn ((d, T), set_name) =>
haftmann@32906
   146
      fold (make_rec_intr T set_name) (#3 (snd d)))
haftmann@32906
   147
        (descr' ~~ recTs ~~ rec_sets') ([], 0);
berghofe@5177
   148
wenzelm@21365
   149
    val ({intrs = rec_intrs, elims = rec_elims, ...}, thy1) =
wenzelm@33278
   150
      thy0
wenzelm@33278
   151
      |> Sign.map_naming Name_Space.conceal
wenzelm@33726
   152
      |> Inductive.add_inductive_global
wenzelm@33669
   153
          {quiet_mode = #quiet config, verbose = false, alt_name = Binding.name big_rec_name',
wenzelm@33669
   154
            coind = false, no_elim = false, no_ind = true, skip_mono = true, fork_mono = false}
haftmann@28965
   155
          (map (fn (s, T) => ((Binding.name s, T), NoSyn)) (rec_set_names' ~~ rec_set_Ts))
wenzelm@26128
   156
          (map dest_Free rec_fns)
wenzelm@33278
   157
          (map (fn x => (Attrib.empty_binding, x)) rec_intr_ts) []
wenzelm@33278
   158
      ||> Sign.restore_naming thy0
wenzelm@33278
   159
      ||> Theory.checkpoint;
berghofe@5177
   160
berghofe@5177
   161
    (* prove uniqueness and termination of primrec combinators *)
berghofe@5177
   162
haftmann@31668
   163
    val _ = message config "Proving termination and uniqueness of primrec functions ...";
berghofe@5177
   164
haftmann@32906
   165
    fun mk_unique_tac ((((i, (tname, _, constrs)), elim), T), T') (tac, intrs) =
berghofe@5177
   166
      let
berghofe@21021
   167
        val distinct_tac =
berghofe@5177
   168
          (if i < length newTs then
haftmann@32905
   169
             full_simp_tac (HOL_ss addsimps (nth dist_rewrites i)) 1
haftmann@32915
   170
           else full_simp_tac (HOL_ss addsimps (flat other_dist_rewrites)) 1);
berghofe@5177
   171
berghofe@5177
   172
        val inject = map (fn r => r RS iffD1)
haftmann@32905
   173
          (if i < length newTs then nth constr_inject i
haftmann@32729
   174
            else injects_of tname);
berghofe@5177
   175
haftmann@32906
   176
        fun mk_unique_constr_tac n (cname, cargs) (tac, intr::intrs, j) =
berghofe@5177
   177
          let
haftmann@32729
   178
            val k = length (filter is_rec_type cargs)
berghofe@5177
   179
berghofe@5177
   180
          in (EVERY [DETERM tac,
berghofe@5177
   181
                REPEAT (etac ex1E 1), rtac ex1I 1,
berghofe@5177
   182
                DEPTH_SOLVE_1 (ares_tac [intr] 1),
berghofe@13641
   183
                REPEAT_DETERM_N k (etac thin_rl 1 THEN rotate_tac 1 1),
berghofe@5177
   184
                etac elim 1,
berghofe@5177
   185
                REPEAT_DETERM_N j distinct_tac,
berghofe@21021
   186
                TRY (dresolve_tac inject 1),
berghofe@5177
   187
                REPEAT (etac conjE 1), hyp_subst_tac 1,
berghofe@13641
   188
                REPEAT (EVERY [etac allE 1, dtac mp 1, atac 1]),
berghofe@5177
   189
                TRY (hyp_subst_tac 1),
berghofe@5177
   190
                rtac refl 1,
berghofe@5177
   191
                REPEAT_DETERM_N (n - j - 1) distinct_tac],
berghofe@5177
   192
              intrs, j + 1)
berghofe@5177
   193
          end;
berghofe@5177
   194
haftmann@32906
   195
        val (tac', intrs', _) = fold (mk_unique_constr_tac (length constrs))
haftmann@32906
   196
          constrs (tac, intrs, 0);
berghofe@5177
   197
berghofe@5177
   198
      in (tac', intrs') end;
berghofe@5177
   199
berghofe@5177
   200
    val rec_unique_thms =
berghofe@5177
   201
      let
berghofe@5177
   202
        val rec_unique_ts = map (fn (((set_t, T1), T2), i) =>
wenzelm@35364
   203
          Const (@{const_name Ex1}, (T2 --> HOLogic.boolT) --> HOLogic.boolT) $
berghofe@21021
   204
            absfree ("y", T2, set_t $ mk_Free "x" T1 i $ Free ("y", T2)))
berghofe@21021
   205
              (rec_sets ~~ recTs ~~ rec_result_Ts ~~ (1 upto length recTs));
wenzelm@17985
   206
        val cert = cterm_of thy1
berghofe@5177
   207
        val insts = map (fn ((i, T), t) => absfree ("x" ^ (string_of_int i), T, t))
berghofe@5177
   208
          ((1 upto length recTs) ~~ recTs ~~ rec_unique_ts);
berghofe@5177
   209
        val induct' = cterm_instantiate ((map cert induct_Ps) ~~
berghofe@5177
   210
          (map cert insts)) induct;
haftmann@32906
   211
        val (tac, _) = fold mk_unique_tac (descr' ~~ rec_elims ~~ recTs ~~ rec_result_Ts)
wenzelm@35625
   212
           (((rtac induct' THEN_ALL_NEW Object_Logic.atomize_prems_tac) 1
wenzelm@35410
   213
              THEN rewrite_goals_tac [mk_meta_eq @{thm choice_eq}], rec_intrs));
berghofe@5177
   214
wenzelm@32970
   215
      in split_conj_thm (Skip_Proof.prove_global thy1 [] []
wenzelm@20046
   216
        (HOLogic.mk_Trueprop (mk_conj rec_unique_ts)) (K tac))
berghofe@5177
   217
      end;
berghofe@5177
   218
wenzelm@35410
   219
    val rec_total_thms = map (fn r => r RS @{thm theI'}) rec_unique_thms;
berghofe@5177
   220
berghofe@5177
   221
    (* define primrec combinators *)
berghofe@5177
   222
berghofe@5177
   223
    val big_reccomb_name = (space_implode "_" new_type_names) ^ "_rec";
haftmann@28965
   224
    val reccomb_names = map (Sign.full_bname thy1)
berghofe@5177
   225
      (if length descr' = 1 then [big_reccomb_name] else
berghofe@5177
   226
        (map ((curry (op ^) (big_reccomb_name ^ "_")) o string_of_int)
berghofe@5177
   227
          (1 upto (length descr'))));
berghofe@5177
   228
    val reccombs = map (fn ((name, T), T') => list_comb
berghofe@5177
   229
      (Const (name, reccomb_fn_Ts @ [T] ---> T'), rec_fns))
berghofe@5177
   230
        (reccomb_names ~~ recTs ~~ rec_result_Ts);
berghofe@5177
   231
haftmann@18358
   232
    val (reccomb_defs, thy2) =
haftmann@18358
   233
      thy1
wenzelm@24712
   234
      |> Sign.add_consts_i (map (fn ((name, T), T') =>
wenzelm@30364
   235
          (Binding.name (Long_Name.base_name name), reccomb_fn_Ts @ [T] ---> T', NoSyn))
haftmann@18358
   236
          (reccomb_names ~~ recTs ~~ rec_result_Ts))
haftmann@27691
   237
      |> (PureThy.add_defs false o map Thm.no_attributes) (map (fn ((((name, comb), set), T), T') =>
wenzelm@30364
   238
          (Binding.name (Long_Name.base_name name ^ "_def"), Logic.mk_equals (comb, absfree ("x", T,
wenzelm@35364
   239
           Const (@{const_name The}, (T' --> HOLogic.boolT) --> T') $ absfree ("y", T',
berghofe@21021
   240
             set $ Free ("x", T) $ Free ("y", T'))))))
haftmann@18358
   241
               (reccomb_names ~~ reccombs ~~ rec_sets ~~ recTs ~~ rec_result_Ts))
haftmann@32124
   242
      ||> Sign.parent_path
wenzelm@28361
   243
      ||> Theory.checkpoint;
berghofe@5177
   244
berghofe@5177
   245
berghofe@5177
   246
    (* prove characteristic equations for primrec combinators *)
berghofe@5177
   247
haftmann@31668
   248
    val _ = message config "Proving characteristic theorems for primrec combinators ..."
berghofe@5177
   249
wenzelm@32970
   250
    val rec_thms = map (fn t => Skip_Proof.prove_global thy2 [] [] t
wenzelm@17985
   251
      (fn _ => EVERY
wenzelm@17985
   252
        [rewrite_goals_tac reccomb_defs,
wenzelm@35410
   253
         rtac @{thm the1_equality} 1,
berghofe@5177
   254
         resolve_tac rec_unique_thms 1,
berghofe@5177
   255
         resolve_tac rec_intrs 1,
wenzelm@20046
   256
         REPEAT (rtac allI 1 ORELSE resolve_tac rec_total_thms 1)]))
haftmann@33968
   257
           (Datatype_Prop.make_primrecs new_type_names descr sorts thy2)
berghofe@5177
   258
berghofe@5177
   259
  in
haftmann@18314
   260
    thy2
wenzelm@24712
   261
    |> Sign.add_path (space_implode "_" new_type_names)
blanchet@33056
   262
    |> PureThy.add_thmss [((Binding.name "recs", rec_thms), [Nitpick_Simps.add])]
wenzelm@24712
   263
    ||> Sign.parent_path
wenzelm@28361
   264
    ||> Theory.checkpoint
haftmann@32906
   265
    |-> (fn thms => pair (reccomb_names, flat thms))
berghofe@5177
   266
  end;
berghofe@5177
   267
berghofe@8477
   268
berghofe@5177
   269
(***************************** case combinators *******************************)
berghofe@5177
   270
haftmann@31737
   271
fun prove_case_thms (config : config) new_type_names descr sorts reccomb_names primrec_thms thy =
berghofe@5177
   272
  let
haftmann@31668
   273
    val _ = message config "Proving characteristic theorems for case combinators ...";
berghofe@5661
   274
haftmann@32124
   275
    val thy1 = Sign.add_path (space_implode "_" new_type_names) thy;
berghofe@5177
   276
wenzelm@32952
   277
    val descr' = flat descr;
berghofe@5177
   278
    val recTs = get_rec_types descr' sorts;
wenzelm@30190
   279
    val used = List.foldr OldTerm.add_typ_tfree_names [] recTs;
haftmann@33957
   280
    val newTs = take (length (hd descr)) recTs;
wenzelm@20071
   281
    val T' = TFree (Name.variant used "'t", HOLogic.typeS);
berghofe@5177
   282
berghofe@13641
   283
    fun mk_dummyT dt = binder_types (typ_of_dtyp descr' sorts dt) ---> T';
berghofe@7015
   284
berghofe@5177
   285
    val case_dummy_fns = map (fn (_, (_, _, constrs)) => map (fn (_, cargs) =>
berghofe@5177
   286
      let
berghofe@5177
   287
        val Ts = map (typ_of_dtyp descr' sorts) cargs;
wenzelm@33317
   288
        val Ts' = map mk_dummyT (filter is_rec_type cargs)
haftmann@28524
   289
      in Const (@{const_name undefined}, Ts @ Ts' ---> T')
berghofe@5177
   290
      end) constrs) descr';
berghofe@5177
   291
haftmann@28965
   292
    val case_names = map (fn s => Sign.full_bname thy1 (s ^ "_case")) new_type_names;
berghofe@5177
   293
berghofe@5177
   294
    (* define case combinators via primrec combinators *)
berghofe@5177
   295
haftmann@32906
   296
    val (case_defs, thy2) = fold (fn ((((i, (_, _, constrs)), T), name), recname) => fn (defs, thy) =>
berghofe@5177
   297
        let
haftmann@32906
   298
          val (fns1, fns2) = split_list (map (fn ((_, cargs), j) =>
berghofe@5177
   299
            let
berghofe@5177
   300
              val Ts = map (typ_of_dtyp descr' sorts) cargs;
wenzelm@33317
   301
              val Ts' = Ts @ map mk_dummyT (filter is_rec_type cargs);
haftmann@33957
   302
              val frees' = map2 (mk_Free "x") Ts' (1 upto length Ts');
haftmann@33957
   303
              val frees = take (length cargs) frees';
berghofe@5177
   304
              val free = mk_Free "f" (Ts ---> T') j
berghofe@5177
   305
            in
berghofe@5177
   306
             (free, list_abs_free (map dest_Free frees',
berghofe@5177
   307
               list_comb (free, frees)))
berghofe@5177
   308
            end) (constrs ~~ (1 upto length constrs)));
berghofe@5177
   309
berghofe@5177
   310
          val caseT = (map (snd o dest_Free) fns1) @ [T] ---> T';
haftmann@33957
   311
          val fns = flat (take i case_dummy_fns) @
haftmann@33957
   312
            fns2 @ flat (drop (i + 1) case_dummy_fns);
berghofe@5177
   313
          val reccomb = Const (recname, (map fastype_of fns) @ [T] ---> T');
wenzelm@30364
   314
          val decl = ((Binding.name (Long_Name.base_name name), caseT), NoSyn);
wenzelm@30364
   315
          val def = (Binding.name (Long_Name.base_name name ^ "_def"),
berghofe@5177
   316
            Logic.mk_equals (list_comb (Const (name, caseT), fns1),
haftmann@33957
   317
              list_comb (reccomb, (flat (take i case_dummy_fns)) @
haftmann@33957
   318
                fns2 @ (flat (drop (i + 1) case_dummy_fns)))));
haftmann@18358
   319
          val ([def_thm], thy') =
haftmann@18358
   320
            thy
wenzelm@33173
   321
            |> Sign.declare_const decl |> snd
haftmann@27691
   322
            |> (PureThy.add_defs false o map Thm.no_attributes) [def];
berghofe@5177
   323
wenzelm@8436
   324
        in (defs @ [def_thm], thy')
haftmann@32906
   325
        end) (hd descr ~~ newTs ~~ case_names ~~
haftmann@33957
   326
          take (length newTs) reccomb_names) ([], thy1)
wenzelm@28361
   327
      ||> Theory.checkpoint;
berghofe@5177
   328
wenzelm@32970
   329
    val case_thms = map (map (fn t => Skip_Proof.prove_global thy2 [] [] t
wenzelm@20046
   330
      (fn _ => EVERY [rewrite_goals_tac (case_defs @ map mk_meta_eq primrec_thms), rtac refl 1])))
haftmann@33968
   331
          (Datatype_Prop.make_cases new_type_names descr sorts thy2)
berghofe@8477
   332
  in
haftmann@18314
   333
    thy2
wenzelm@33459
   334
    |> Context.theory_map ((fold o fold) Nitpick_Simps.add_thm case_thms)
haftmann@32124
   335
    |> Sign.parent_path
haftmann@18314
   336
    |> store_thmss "cases" new_type_names case_thms
haftmann@18314
   337
    |-> (fn thmss => pair (thmss, case_names))
berghofe@8477
   338
  end;
berghofe@5177
   339
berghofe@5177
   340
berghofe@5177
   341
(******************************* case splitting *******************************)
berghofe@5177
   342
haftmann@31737
   343
fun prove_split_thms (config : config) new_type_names descr sorts constr_inject dist_rewrites
berghofe@5177
   344
    casedist_thms case_thms thy =
berghofe@5177
   345
  let
haftmann@31668
   346
    val _ = message config "Proving equations for case splitting ...";
berghofe@5177
   347
haftmann@31668
   348
    val descr' = flat descr;
berghofe@5177
   349
    val recTs = get_rec_types descr' sorts;
haftmann@33957
   350
    val newTs = take (length (hd descr)) recTs;
berghofe@5177
   351
berghofe@5177
   352
    fun prove_split_thms ((((((t1, t2), inject), dist_rewrites'),
berghofe@5177
   353
        exhaustion), case_thms'), T) =
berghofe@5177
   354
      let
wenzelm@17985
   355
        val cert = cterm_of thy;
berghofe@5177
   356
        val _ $ (_ $ lhs $ _) = hd (Logic.strip_assums_hyp (hd (prems_of exhaustion)));
berghofe@5177
   357
        val exhaustion' = cterm_instantiate
berghofe@5177
   358
          [(cert lhs, cert (Free ("x", T)))] exhaustion;
wenzelm@17985
   359
        val tacf = K (EVERY [rtac exhaustion' 1, ALLGOALS (asm_simp_tac
wenzelm@17985
   360
          (HOL_ss addsimps (dist_rewrites' @ inject @ case_thms')))])
berghofe@5177
   361
      in
wenzelm@32970
   362
        (Skip_Proof.prove_global thy [] [] t1 tacf,
wenzelm@32970
   363
         Skip_Proof.prove_global thy [] [] t2 tacf)
berghofe@5177
   364
      end;
berghofe@5177
   365
berghofe@5177
   366
    val split_thm_pairs = map prove_split_thms
haftmann@33968
   367
      ((Datatype_Prop.make_splits new_type_names descr sorts thy) ~~ constr_inject ~~
berghofe@5177
   368
        dist_rewrites ~~ casedist_thms ~~ case_thms ~~ newTs);
berghofe@5177
   369
berghofe@5177
   370
    val (split_thms, split_asm_thms) = ListPair.unzip split_thm_pairs
berghofe@5177
   371
berghofe@5177
   372
  in
haftmann@18314
   373
    thy
haftmann@18314
   374
    |> store_thms "split" new_type_names split_thms
haftmann@18314
   375
    ||>> store_thms "split_asm" new_type_names split_asm_thms
haftmann@18314
   376
    |-> (fn (thms1, thms2) => pair (thms1 ~~ thms2))
berghofe@5177
   377
  end;
berghofe@5177
   378
nipkow@8601
   379
fun prove_weak_case_congs new_type_names descr sorts thy =
nipkow@8601
   380
  let
nipkow@8601
   381
    fun prove_weak_case_cong t =
wenzelm@32970
   382
       Skip_Proof.prove_global thy [] (Logic.strip_imp_prems t) (Logic.strip_imp_concl t)
wenzelm@26711
   383
         (fn {prems, ...} => EVERY [rtac ((hd prems) RS arg_cong) 1])
nipkow@8601
   384
haftmann@33968
   385
    val weak_case_congs = map prove_weak_case_cong (Datatype_Prop.make_weak_case_congs
nipkow@8601
   386
      new_type_names descr sorts thy)
nipkow@8601
   387
nipkow@8601
   388
  in thy |> store_thms "weak_case_cong" new_type_names weak_case_congs end;
berghofe@8477
   389
berghofe@5177
   390
(************************* additional theorems for TFL ************************)
berghofe@5177
   391
haftmann@31737
   392
fun prove_nchotomys (config : config) new_type_names descr sorts casedist_thms thy =
berghofe@5177
   393
  let
haftmann@31668
   394
    val _ = message config "Proving additional theorems for TFL ...";
berghofe@5177
   395
berghofe@5177
   396
    fun prove_nchotomy (t, exhaustion) =
berghofe@5177
   397
      let
berghofe@5177
   398
        (* For goal i, select the correct disjunct to attack, then prove it *)
berghofe@5177
   399
        fun tac i 0 = EVERY [TRY (rtac disjI1 i),
berghofe@5177
   400
              hyp_subst_tac i, REPEAT (rtac exI i), rtac refl i]
berghofe@5177
   401
          | tac i n = rtac disjI2 i THEN tac i (n - 1)
berghofe@5177
   402
      in 
wenzelm@32970
   403
        Skip_Proof.prove_global thy [] [] t (fn _ =>
wenzelm@17985
   404
          EVERY [rtac allI 1,
berghofe@5177
   405
           exh_tac (K exhaustion) 1,
wenzelm@20046
   406
           ALLGOALS (fn i => tac i (i-1))])
berghofe@5177
   407
      end;
berghofe@5177
   408
berghofe@5177
   409
    val nchotomys =
haftmann@33968
   410
      map prove_nchotomy (Datatype_Prop.make_nchotomys descr sorts ~~ casedist_thms)
berghofe@5177
   411
wenzelm@8436
   412
  in thy |> store_thms "nchotomy" new_type_names nchotomys end;
berghofe@5177
   413
berghofe@5177
   414
fun prove_case_congs new_type_names descr sorts nchotomys case_thms thy =
berghofe@5177
   415
  let
berghofe@5177
   416
    fun prove_case_cong ((t, nchotomy), case_rewrites) =
berghofe@5177
   417
      let
berghofe@5177
   418
        val (Const ("==>", _) $ tm $ _) = t;
haftmann@38864
   419
        val (Const (@{const_name Trueprop}, _) $ (Const (@{const_name HOL.eq}, _) $ _ $ Ma)) = tm;
wenzelm@22578
   420
        val cert = cterm_of thy;
berghofe@5177
   421
        val nchotomy' = nchotomy RS spec;
wenzelm@29264
   422
        val [v] = Term.add_vars (concl_of nchotomy') [];
wenzelm@29264
   423
        val nchotomy'' = cterm_instantiate [(cert (Var v), cert Ma)] nchotomy'
berghofe@5177
   424
      in
wenzelm@32970
   425
        Skip_Proof.prove_global thy [] (Logic.strip_imp_prems t) (Logic.strip_imp_concl t)
wenzelm@26711
   426
          (fn {prems, ...} => 
wenzelm@17985
   427
            let val simplify = asm_simp_tac (HOL_ss addsimps (prems @ case_rewrites))
wenzelm@17985
   428
            in EVERY [simp_tac (HOL_ss addsimps [hd prems]) 1,
wenzelm@17985
   429
                cut_facts_tac [nchotomy''] 1,
wenzelm@17985
   430
                REPEAT (etac disjE 1 THEN REPEAT (etac exE 1) THEN simplify 1),
wenzelm@17985
   431
                REPEAT (etac exE 1) THEN simplify 1 (* Get last disjunct *)]
wenzelm@20046
   432
            end)
berghofe@5177
   433
      end;
berghofe@5177
   434
haftmann@33968
   435
    val case_congs = map prove_case_cong (Datatype_Prop.make_case_congs
berghofe@5177
   436
      new_type_names descr sorts thy ~~ nchotomys ~~ case_thms)
berghofe@5177
   437
wenzelm@8436
   438
  in thy |> store_thms "case_cong" new_type_names case_congs end;
berghofe@5177
   439
berghofe@5177
   440
end;