src/HOL/Codatatype/Tools/bnf_gfp_tactics.ML
author blanchet
Wed Sep 12 11:39:05 2012 +0200 (2012-09-12)
changeset 49328 a1c10b46fecd
parent 49305 8241f21d104b
child 49366 3edd1c90f6e6
permissions -rw-r--r--
avoided duplicate lemma
blanchet@48975
     1
(*  Title:      HOL/Codatatype/Tools/bnf_gfp_tactics.ML
blanchet@48975
     2
    Author:     Dmitriy Traytel, TU Muenchen
blanchet@48975
     3
    Author:     Andrei Popescu, TU Muenchen
blanchet@48975
     4
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@48975
     5
    Copyright   2012
blanchet@48975
     6
blanchet@48975
     7
Tactics for the codatatype construction.
blanchet@48975
     8
*)
blanchet@48975
     9
blanchet@48975
    10
signature BNF_GFP_TACTICS =
blanchet@48975
    11
sig
blanchet@48975
    12
  val mk_Lev_sbd_tac: cterm option list -> thm list -> thm list -> thm list list -> tactic
blanchet@48975
    13
  val mk_bd_card_order_tac: thm -> tactic
blanchet@48975
    14
  val mk_bd_cinfinite_tac: thm -> tactic
blanchet@48975
    15
  val mk_bis_Gr_tac: thm -> thm list -> thm list -> thm list -> thm list ->
blanchet@48975
    16
    {prems: 'a, context: Proof.context} -> tactic
blanchet@48975
    17
  val mk_bis_O_tac: int -> thm -> thm list -> thm list -> tactic
blanchet@48975
    18
  val mk_bis_Union_tac: thm -> thm list -> {prems: 'a, context: Proof.context} -> tactic
blanchet@48975
    19
  val mk_bis_converse_tac: int -> thm -> thm list -> thm list -> tactic
blanchet@48975
    20
  val mk_bis_rel_tac: int -> thm -> thm list -> thm list -> thm list -> thm list list -> tactic
blanchet@48975
    21
  val mk_carT_set_tac: int -> int -> thm -> thm -> thm -> thm ->
blanchet@48975
    22
    {prems: 'a, context: Proof.context} -> tactic
blanchet@48975
    23
  val mk_card_of_carT_tac: int -> thm list -> thm -> thm -> thm -> thm -> thm -> thm list -> tactic
blanchet@48975
    24
  val mk_coalgT_tac: int -> thm list -> thm list -> thm list list ->
blanchet@48975
    25
    {prems: 'a, context: Proof.context} -> tactic
blanchet@48975
    26
  val mk_coalg_final_tac: int -> thm -> thm list -> thm list -> thm list list -> thm list list ->
blanchet@48975
    27
    tactic
blanchet@48975
    28
  val mk_coalg_set_tac: thm -> tactic
blanchet@48975
    29
  val mk_coalg_thePull_tac: int -> thm -> thm list -> thm list list -> (int -> tactic) list ->
blanchet@48975
    30
    {prems: 'a, context: Proof.context} -> tactic
traytel@49104
    31
  val mk_coind_wit_tac: thm -> thm list -> thm list -> thm list ->
traytel@49104
    32
    {prems: 'a, context: Proof.context} -> tactic
blanchet@48975
    33
  val mk_coiter_unique_mor_tac: thm list -> thm -> thm -> thm list -> tactic
blanchet@48975
    34
  val mk_col_bd_tac: int -> int -> cterm option list -> thm list -> thm list -> thm -> thm ->
blanchet@48975
    35
    thm list list -> tactic
blanchet@48975
    36
  val mk_col_natural_tac: cterm option list -> thm list -> thm list -> thm list -> thm list list ->
blanchet@48975
    37
    {prems: 'a, context: Proof.context} -> tactic
blanchet@48975
    38
  val mk_congruent_str_final_tac: int -> thm -> thm -> thm -> thm list -> tactic
blanchet@48975
    39
  val mk_corec_tac: int -> thm list -> thm -> thm -> thm list ->
blanchet@48975
    40
    {prems: 'a, context: Proof.context} -> tactic
blanchet@48975
    41
  val mk_equiv_lsbis_tac: thm -> thm -> thm -> thm -> thm -> thm -> tactic
blanchet@48975
    42
  val mk_hset_minimal_tac: int -> thm list -> thm -> {prems: 'a, context: Proof.context} -> tactic
blanchet@48975
    43
  val mk_hset_rec_minimal_tac: int -> cterm option list -> thm list -> thm list ->
blanchet@48975
    44
    {prems: 'a, context: Proof.context} -> tactic
blanchet@48975
    45
  val mk_in_bd_tac: thm -> thm list -> thm -> thm -> thm -> thm -> thm list -> thm -> thm -> thm ->
blanchet@48975
    46
    thm -> thm -> thm -> tactic
blanchet@48975
    47
  val mk_incl_lsbis_tac: int -> int -> thm -> tactic
blanchet@48975
    48
  val mk_isNode_hset_tac: int -> thm -> thm list -> {prems: 'a, context: Proof.context} -> tactic
blanchet@48975
    49
  val mk_length_Lev'_tac: thm -> tactic
blanchet@48975
    50
  val mk_length_Lev_tac: cterm option list -> thm list -> thm list -> tactic
blanchet@48975
    51
  val mk_map_comp_tac: int -> int -> thm list -> thm list -> thm list -> thm -> tactic
blanchet@48975
    52
  val mk_mcong_tac: int -> (int -> tactic) -> thm list -> thm list -> thm list -> thm list list ->
blanchet@48975
    53
    thm list list -> thm list list list -> tactic
blanchet@48975
    54
  val mk_map_id_tac: thm list -> thm -> thm -> tactic
blanchet@48975
    55
  val mk_map_tac: int -> int -> ctyp option -> thm -> thm -> thm -> tactic
blanchet@48975
    56
  val mk_map_unique_tac: thm -> thm list -> {prems: 'a, context: Proof.context} -> tactic
blanchet@48975
    57
  val mk_mor_Abs_tac: thm list -> thm list -> {prems: 'a, context: Proof.context} -> tactic
blanchet@48975
    58
  val mk_mor_Rep_tac: int -> thm list -> thm list -> thm list -> thm list list -> thm list ->
blanchet@48975
    59
    thm list -> {prems: 'a, context: Proof.context} -> tactic
blanchet@48975
    60
  val mk_mor_T_final_tac: thm -> thm list -> thm list -> tactic
blanchet@48975
    61
  val mk_mor_UNIV_tac: thm list -> thm -> tactic
blanchet@48975
    62
  val mk_mor_beh_tac: int -> thm -> thm -> thm list -> thm list -> thm list -> thm list ->
blanchet@48975
    63
    thm list list -> thm list list -> thm list -> thm list -> thm list -> thm list -> thm list ->
blanchet@48975
    64
    thm list -> thm list -> thm list -> thm list list -> thm list list list -> thm list list list ->
blanchet@48975
    65
    thm list list list -> thm list list -> thm list list -> thm list -> thm list -> thm list ->
blanchet@48975
    66
    {prems: 'a, context: Proof.context} -> tactic
blanchet@48975
    67
  val mk_mor_coiter_tac: int -> thm -> thm list -> thm list -> thm list -> thm list -> thm list ->
blanchet@48975
    68
    thm list -> tactic
blanchet@48975
    69
  val mk_mor_comp_tac: thm -> thm list -> thm list -> thm list -> tactic
blanchet@48975
    70
  val mk_mor_elim_tac: thm -> tactic
blanchet@48975
    71
  val mk_mor_hset_rec_tac: int -> int -> cterm option list -> int -> thm list -> thm list ->
blanchet@48975
    72
    thm list -> thm list list -> thm list list -> tactic
blanchet@48975
    73
  val mk_mor_hset_tac: thm -> thm -> tactic
blanchet@48975
    74
  val mk_mor_incl_tac: thm -> thm list -> tactic
blanchet@48975
    75
  val mk_mor_str_tac: 'a list -> thm -> tactic
blanchet@48975
    76
  val mk_mor_sum_case_tac: 'a list -> thm -> tactic
blanchet@48975
    77
  val mk_mor_thePull_fst_tac: int -> thm -> thm list -> thm list -> (int -> tactic) list ->
blanchet@48975
    78
    {prems: thm list, context: Proof.context} -> tactic
blanchet@48975
    79
  val mk_mor_thePull_snd_tac: int -> thm -> thm list -> thm list -> (int -> tactic) list ->
blanchet@48975
    80
    {prems: thm list, context: Proof.context} -> tactic
blanchet@48975
    81
  val mk_mor_thePull_pick_tac: thm -> thm list -> thm list ->
blanchet@48975
    82
    {prems: 'a, context: Proof.context} -> tactic
blanchet@48975
    83
  val mk_prefCl_Lev_tac: cterm option list -> thm list -> thm list -> tactic
blanchet@48975
    84
  val mk_pickWP_assms_tac: thm list -> thm list -> thm -> (int -> tactic)
blanchet@48975
    85
  val mk_pick_col_tac: int -> int -> cterm option list -> thm list -> thm list -> thm list ->
blanchet@48975
    86
    thm list list -> thm list -> (int -> tactic) list -> {prems: 'a, context: Proof.context} ->
blanchet@48975
    87
    tactic
blanchet@48975
    88
  val mk_raw_coind_tac: thm -> thm -> thm -> thm -> thm -> thm -> thm -> thm -> thm -> thm list ->
blanchet@48975
    89
    thm list -> thm list -> thm -> thm list -> tactic
blanchet@48975
    90
  val mk_rel_coinduct_tac: 'a list -> thm -> thm -> tactic
blanchet@48975
    91
  val mk_rel_coinduct_upto_tac: int -> ctyp option list -> cterm option list -> thm -> thm list ->
blanchet@48975
    92
    thm list -> tactic
blanchet@48975
    93
  val mk_rel_unfold_tac: thm list -> int -> thm -> thm -> thm -> thm -> thm list -> thm -> thm ->
blanchet@48975
    94
    thm list -> thm list -> thm list list -> tactic
blanchet@48975
    95
  val mk_rv_last_tac: ctyp option list -> cterm option list -> thm list -> thm list -> tactic
blanchet@48975
    96
  val mk_sbis_lsbis_tac: thm list -> thm -> thm -> tactic
blanchet@48975
    97
  val mk_set_Lev_tac: cterm option list -> thm list -> thm list -> thm list -> thm list ->
blanchet@48975
    98
    thm list list -> tactic
blanchet@48975
    99
  val mk_set_bd_tac: thm -> thm -> thm -> tactic
blanchet@48975
   100
  val mk_set_hset_incl_hset_tac: int -> thm list -> thm -> int -> tactic
blanchet@48975
   101
  val mk_set_image_Lev_tac: cterm option list -> thm list -> thm list -> thm list -> thm list ->
blanchet@48975
   102
    thm list list -> thm list list -> tactic
blanchet@48975
   103
  val mk_set_incl_hin_tac: thm list -> tactic
blanchet@48975
   104
  val mk_set_incl_hset_tac: thm -> thm -> tactic
blanchet@48975
   105
  val mk_set_le_tac: int -> thm -> thm list -> thm list list -> tactic
blanchet@48975
   106
  val mk_set_natural_tac: thm -> thm -> tactic
blanchet@48975
   107
  val mk_set_rv_Lev_tac: int -> cterm option list -> thm list -> thm list -> thm list -> thm list ->
blanchet@48975
   108
    thm list list -> thm list list -> tactic
blanchet@48975
   109
  val mk_set_simp_tac: int -> thm -> thm -> thm list -> tactic
blanchet@48975
   110
  val mk_strT_hset_tac: int -> int -> int -> ctyp option list -> ctyp option list ->
blanchet@48975
   111
    cterm option list -> thm list -> thm list -> thm list -> thm list -> thm list list ->
blanchet@48975
   112
    thm list list -> thm list list -> thm -> thm list list -> tactic
blanchet@48975
   113
  val mk_unf_coinduct_tac: int -> int list -> thm -> thm -> tactic
blanchet@48975
   114
  val mk_unf_coinduct_upto_tac: int list -> ctyp option list -> cterm option list -> thm -> thm ->
blanchet@48975
   115
    thm -> tactic
blanchet@48975
   116
  val mk_unf_o_fld_tac: thm -> thm -> thm -> thm -> thm list ->
blanchet@48975
   117
    {prems: 'a, context: Proof.context} -> tactic
blanchet@48975
   118
  val mk_unique_mor_tac: thm list -> thm -> tactic
traytel@49104
   119
  val mk_wit_tac: int -> thm list -> thm list -> thm list -> thm list ->
blanchet@48975
   120
    {prems: 'a, context: Proof.context} -> tactic
blanchet@48975
   121
  val mk_wpull_tac: int -> thm -> thm -> thm -> thm -> thm -> thm list -> thm list -> tactic
blanchet@48975
   122
end;
blanchet@48975
   123
blanchet@48975
   124
structure BNF_GFP_Tactics : BNF_GFP_TACTICS =
blanchet@48975
   125
struct
blanchet@48975
   126
blanchet@48975
   127
open BNF_Tactics
blanchet@48975
   128
open BNF_Util
blanchet@49020
   129
open BNF_FP_Util
blanchet@48975
   130
open BNF_GFP_Util
blanchet@48975
   131
blanchet@49305
   132
val Pair_eq_subst_id = @{thm Pair_eq[THEN subst, of "%x. x"]};
blanchet@49305
   133
val fst_convol_fun_cong_sym = @{thm fst_convol} RS fun_cong RS sym;
blanchet@49305
   134
val list_inject_subst_id = @{thm list.inject[THEN subst, of "%x. x"]};
blanchet@49305
   135
val nat_induct = @{thm nat_induct};
blanchet@49305
   136
val o_apply_trans_sym = o_apply RS trans RS sym;
blanchet@49305
   137
val ord_eq_le_trans = @{thm ord_eq_le_trans};
blanchet@49305
   138
val ord_eq_le_trans_trans_fun_cong_image_id_id_apply =
blanchet@49305
   139
  @{thm ord_eq_le_trans[OF trans[OF fun_cong[OF image_id] id_apply]]};
blanchet@49305
   140
val ordIso_ordLeq_trans = @{thm ordIso_ordLeq_trans};
blanchet@49305
   141
val set_mp = @{thm set_mp};
blanchet@49305
   142
val set_rev_mp = @{thm set_rev_mp};
blanchet@49305
   143
val snd_convol_fun_cong_sym = @{thm snd_convol} RS fun_cong RS sym;
blanchet@49305
   144
val ssubst_id = @{thm ssubst[of _ _ "%x. x"]};
blanchet@49305
   145
val subst_id = @{thm subst[of _ _ "%x. x"]};
blanchet@49328
   146
val sum_case_weak_cong = @{thm sum_case_weak_cong};
blanchet@49305
   147
val trans_fun_cong_image_id_id_apply = @{thm trans[OF fun_cong[OF image_id] id_apply]};
blanchet@49305
   148
blanchet@48975
   149
fun mk_coalg_set_tac coalg_def =
blanchet@49305
   150
  dtac (coalg_def RS subst_id) 1 THEN
blanchet@48975
   151
  REPEAT_DETERM (etac conjE 1) THEN
blanchet@48975
   152
  EVERY' [dtac @{thm rev_bspec}, atac] 1 THEN
blanchet@48975
   153
  REPEAT_DETERM (eresolve_tac [CollectE, conjE] 1) THEN atac 1;
blanchet@48975
   154
blanchet@48975
   155
fun mk_mor_elim_tac mor_def =
blanchet@48975
   156
  (dtac (subst OF [mor_def]) THEN'
blanchet@48975
   157
  REPEAT o etac conjE THEN'
blanchet@48975
   158
  TRY o rtac @{thm image_subsetI} THEN'
blanchet@48975
   159
  etac bspec THEN'
blanchet@48975
   160
  atac) 1;
blanchet@48975
   161
blanchet@48975
   162
fun mk_mor_incl_tac mor_def map_id's =
blanchet@48975
   163
  (stac mor_def THEN'
blanchet@48975
   164
  rtac conjI THEN'
blanchet@49305
   165
  CONJ_WRAP' (K (EVERY' [rtac ballI, etac set_mp, stac @{thm id_apply}, atac]))
blanchet@48975
   166
    map_id's THEN'
blanchet@48975
   167
  CONJ_WRAP' (fn thm =>
blanchet@48975
   168
   (EVERY' [rtac ballI, rtac (thm RS trans), rtac sym, rtac (@{thm id_apply} RS arg_cong)]))
blanchet@48975
   169
  map_id's) 1;
blanchet@48975
   170
blanchet@48975
   171
fun mk_mor_comp_tac mor_def mor_images morEs map_comp_ids =
blanchet@48975
   172
  let
blanchet@48975
   173
    fun fbetw_tac image = EVERY' [rtac ballI, stac o_apply, etac image, etac image, atac];
blanchet@48975
   174
    fun mor_tac ((mor_image, morE), map_comp_id) =
blanchet@48975
   175
      EVERY' [rtac ballI, stac o_apply, rtac trans, rtac (map_comp_id RS sym), rtac trans,
blanchet@48975
   176
        etac (morE RS arg_cong), atac, etac morE, etac mor_image, atac];
blanchet@48975
   177
  in
blanchet@48975
   178
    (stac mor_def THEN' rtac conjI THEN'
blanchet@48975
   179
    CONJ_WRAP' fbetw_tac mor_images THEN'
blanchet@48975
   180
    CONJ_WRAP' mor_tac ((mor_images ~~ morEs) ~~ map_comp_ids)) 1
blanchet@48975
   181
  end;
blanchet@48975
   182
blanchet@48975
   183
fun mk_mor_UNIV_tac morEs mor_def =
blanchet@48975
   184
  let
blanchet@48975
   185
    val n = length morEs;
blanchet@48975
   186
    fun mor_tac morE = EVERY' [rtac ext, rtac trans, rtac o_apply, rtac trans, etac morE,
blanchet@48975
   187
      rtac UNIV_I, rtac sym, rtac o_apply];
blanchet@48975
   188
  in
blanchet@48975
   189
    EVERY' [rtac iffI, CONJ_WRAP' mor_tac morEs,
blanchet@48975
   190
    stac mor_def, rtac conjI, CONJ_WRAP' (K (rtac ballI THEN' rtac UNIV_I)) morEs,
blanchet@48975
   191
    CONJ_WRAP' (fn i =>
blanchet@48975
   192
      EVERY' [dtac (mk_conjunctN n i), rtac ballI, etac @{thm pointfreeE}]) (1 upto n)] 1
blanchet@48975
   193
  end;
blanchet@48975
   194
blanchet@48975
   195
fun mk_mor_str_tac ks mor_UNIV =
blanchet@48975
   196
  (stac mor_UNIV THEN' CONJ_WRAP' (K (rtac refl)) ks) 1;
blanchet@48975
   197
blanchet@48975
   198
fun mk_mor_sum_case_tac ks mor_UNIV =
blanchet@48975
   199
  (stac mor_UNIV THEN' CONJ_WRAP' (K (rtac @{thm sum_case_comp_Inl[symmetric]})) ks) 1;
blanchet@48975
   200
blanchet@48975
   201
fun mk_set_incl_hset_tac def rec_Suc =
blanchet@48975
   202
  EVERY' (stac def ::
blanchet@48975
   203
    map rtac [@{thm incl_UNION_I}, UNIV_I, @{thm ord_le_eq_trans}, @{thm Un_upper1},
blanchet@48975
   204
      sym, rec_Suc]) 1;
blanchet@48975
   205
blanchet@48975
   206
fun mk_set_hset_incl_hset_tac n defs rec_Suc i =
blanchet@48975
   207
  EVERY' (map (TRY oo stac) defs @
blanchet@49305
   208
    map rtac [@{thm UN_least}, subsetI, @{thm UN_I}, UNIV_I, set_mp, equalityD2, rec_Suc, UnI2,
blanchet@49305
   209
      mk_UnN n i] @
blanchet@48975
   210
    [etac @{thm UN_I}, atac]) 1;
blanchet@48975
   211
blanchet@48975
   212
fun mk_set_incl_hin_tac incls =
blanchet@48975
   213
  if null incls then rtac @{thm subset_UNIV} 1
blanchet@48975
   214
  else EVERY' [rtac subsetI, rtac CollectI,
blanchet@48975
   215
    CONJ_WRAP' (fn incl => EVERY' [rtac subset_trans, etac incl, atac]) incls] 1;
blanchet@48975
   216
blanchet@48975
   217
fun mk_hset_rec_minimal_tac m cts rec_0s rec_Sucs {context = ctxt, prems = _} =
blanchet@49305
   218
  EVERY' [rtac (Drule.instantiate' [] cts nat_induct),
blanchet@48975
   219
    REPEAT_DETERM o rtac allI,
blanchet@48975
   220
    CONJ_WRAP' (fn thm => EVERY'
blanchet@49305
   221
      [rtac ord_eq_le_trans, rtac thm, rtac @{thm empty_subsetI}]) rec_0s,
blanchet@48975
   222
    REPEAT_DETERM o rtac allI,
blanchet@48975
   223
    CONJ_WRAP' (fn rec_Suc => EVERY'
blanchet@49305
   224
      [rtac ord_eq_le_trans, rtac rec_Suc,
blanchet@48975
   225
        if m = 0 then K all_tac
blanchet@48975
   226
        else (rtac @{thm Un_least} THEN' Goal.assume_rule_tac ctxt),
blanchet@48975
   227
        CONJ_WRAP_GEN' (rtac (Thm.permute_prems 0 1 @{thm Un_least}))
blanchet@48975
   228
          (K (EVERY' [rtac @{thm UN_least}, REPEAT_DETERM o eresolve_tac [allE, conjE],
blanchet@48975
   229
            rtac subset_trans, atac, Goal.assume_rule_tac ctxt])) rec_0s])
blanchet@48975
   230
      rec_Sucs] 1;
blanchet@48975
   231
blanchet@48975
   232
fun mk_hset_minimal_tac n hset_defs hset_rec_minimal {context = ctxt, prems = _} =
blanchet@49305
   233
  (CONJ_WRAP' (fn def => (EVERY' [rtac ord_eq_le_trans, rtac def,
blanchet@48975
   234
    rtac @{thm UN_least}, rtac rev_mp, rtac hset_rec_minimal,
blanchet@48975
   235
    EVERY' (replicate ((n + 1) * n) (Goal.assume_rule_tac ctxt)), rtac impI,
blanchet@48975
   236
    REPEAT_DETERM o eresolve_tac [allE, conjE], atac])) hset_defs) 1
blanchet@48975
   237
blanchet@48975
   238
fun mk_mor_hset_rec_tac m n cts j rec_0s rec_Sucs morEs set_naturalss coalg_setss =
blanchet@49305
   239
  EVERY' [rtac (Drule.instantiate' [] cts nat_induct),
blanchet@48975
   240
    REPEAT_DETERM o rtac allI,
blanchet@48975
   241
    CONJ_WRAP' (fn thm => EVERY' (map rtac [impI, thm RS trans, thm RS sym])) rec_0s,
blanchet@48975
   242
    REPEAT_DETERM o rtac allI,
blanchet@48975
   243
    CONJ_WRAP'
blanchet@48975
   244
      (fn (rec_Suc, (morE, ((passive_set_naturals, active_set_naturals), coalg_sets))) =>
blanchet@48975
   245
        EVERY' [rtac impI, rtac (rec_Suc RS trans), rtac (rec_Suc RS trans RS sym),
blanchet@48975
   246
          if m = 0 then K all_tac
blanchet@48975
   247
          else EVERY' [rtac @{thm Un_cong}, rtac box_equals,
blanchet@48975
   248
            rtac (nth passive_set_naturals (j - 1) RS sym),
blanchet@49305
   249
            rtac trans_fun_cong_image_id_id_apply, etac (morE RS arg_cong), atac],
blanchet@48975
   250
          CONJ_WRAP_GEN' (rtac (Thm.permute_prems 0 1 @{thm Un_cong}))
blanchet@48975
   251
            (fn (i, (set_natural, coalg_set)) =>
blanchet@48975
   252
              EVERY' [rtac sym, rtac trans, rtac (refl RSN (2, @{thm UN_cong})),
blanchet@48975
   253
                etac (morE RS sym RS arg_cong RS trans), atac, rtac set_natural,
blanchet@48975
   254
                rtac (@{thm UN_simps(10)} RS trans), rtac (refl RS @{thm UN_cong}),
blanchet@49305
   255
                ftac coalg_set, atac, dtac set_mp, atac, rtac mp, rtac (mk_conjunctN n i),
blanchet@48975
   256
                REPEAT_DETERM o etac allE, atac, atac])
blanchet@48975
   257
            (rev ((1 upto n) ~~ (active_set_naturals ~~ coalg_sets)))])
blanchet@48975
   258
      (rec_Sucs ~~ (morEs ~~ (map (chop m) set_naturalss ~~ map (drop m) coalg_setss)))] 1;
blanchet@48975
   259
blanchet@48975
   260
fun mk_mor_hset_tac hset_def mor_hset_rec =
blanchet@48975
   261
  EVERY' [rtac (hset_def RS trans), rtac (refl RS @{thm UN_cong} RS trans), etac mor_hset_rec,
blanchet@48975
   262
    atac, atac, rtac (hset_def RS sym)] 1
blanchet@48975
   263
blanchet@48975
   264
fun mk_bis_rel_tac m bis_def rel_defs map_comps map_congs set_naturalss =
blanchet@48975
   265
  let
blanchet@48975
   266
    val n = length rel_defs;
blanchet@48975
   267
    val thms = ((1 upto n) ~~ map_comps ~~ map_congs ~~ set_naturalss ~~ rel_defs);
blanchet@48975
   268
blanchet@48975
   269
    fun mk_if_tac ((((i, map_comp), map_cong), set_naturals), rel_def) =
blanchet@48975
   270
      EVERY' [rtac allI, rtac allI, rtac impI, dtac (mk_conjunctN n i),
blanchet@48975
   271
        etac allE, etac allE, etac impE, atac, etac bexE, etac conjE,
blanchet@49305
   272
        rtac (rel_def RS equalityD2 RS set_mp),
blanchet@48975
   273
        rtac @{thm relcompI}, rtac @{thm converseI},
blanchet@48975
   274
        EVERY' (map (fn thm =>
blanchet@48975
   275
          EVERY' [rtac @{thm GrI}, REPEAT_DETERM o eresolve_tac [CollectE, conjE],
blanchet@48975
   276
            rtac CollectI,
blanchet@48975
   277
            CONJ_WRAP' (fn (i, thm) =>
blanchet@48975
   278
              if i <= m
blanchet@49305
   279
              then EVERY' [rtac ord_eq_le_trans, rtac thm, rtac subset_trans,
blanchet@48975
   280
                etac @{thm image_mono}, rtac @{thm image_subsetI}, etac @{thm diagI}]
blanchet@49305
   281
              else EVERY' [rtac ord_eq_le_trans, rtac trans, rtac thm,
blanchet@49305
   282
                rtac trans_fun_cong_image_id_id_apply, atac])
blanchet@48975
   283
            (1 upto (m + n) ~~ set_naturals),
blanchet@48975
   284
            rtac trans, rtac trans, rtac map_comp, rtac map_cong, REPEAT_DETERM_N m o rtac thm,
blanchet@48975
   285
            REPEAT_DETERM_N n o rtac (@{thm o_id} RS fun_cong), atac])
blanchet@48975
   286
          @{thms fst_diag_id snd_diag_id})];
blanchet@48975
   287
blanchet@48975
   288
    fun mk_only_if_tac ((((i, map_comp), map_cong), set_naturals), rel_def) =
blanchet@48975
   289
      EVERY' [dtac (mk_conjunctN n i), rtac allI, rtac allI, rtac impI,
blanchet@48975
   290
        etac allE, etac allE, etac impE, atac,
blanchet@49305
   291
        dtac (rel_def RS equalityD1 RS set_mp),
blanchet@48975
   292
        REPEAT_DETERM o eresolve_tac [CollectE, @{thm relcompE}, @{thm converseE}],
blanchet@48975
   293
        REPEAT_DETERM o eresolve_tac [@{thm GrE}, exE, conjE],
blanchet@49305
   294
        REPEAT_DETERM o dtac Pair_eq_subst_id,
blanchet@48975
   295
        REPEAT_DETERM o etac conjE,
blanchet@48975
   296
        hyp_subst_tac,
blanchet@48975
   297
        REPEAT_DETERM o eresolve_tac [CollectE, conjE],
blanchet@48975
   298
        rtac bexI, rtac conjI, rtac trans, rtac map_comp,
blanchet@48975
   299
        REPEAT_DETERM_N m o stac @{thm id_o},
blanchet@48975
   300
        REPEAT_DETERM_N n o stac @{thm o_id},
blanchet@48975
   301
        etac sym, rtac trans, rtac map_comp,
blanchet@48975
   302
        REPEAT_DETERM_N m o stac @{thm id_o},
blanchet@48975
   303
        REPEAT_DETERM_N n o stac @{thm o_id},
blanchet@48975
   304
        rtac trans, rtac map_cong,
blanchet@49305
   305
        REPEAT_DETERM_N m o EVERY' [rtac @{thm diagE'}, etac set_mp, atac],
blanchet@48975
   306
        REPEAT_DETERM_N n o rtac refl,
blanchet@48975
   307
        etac sym, rtac CollectI,
blanchet@48975
   308
        CONJ_WRAP' (fn (i, thm) =>
blanchet@48975
   309
          if i <= m
blanchet@49305
   310
          then EVERY' [rtac ord_eq_le_trans, rtac thm, rtac @{thm image_subsetI},
blanchet@49305
   311
            rtac @{thm diag_fst}, etac set_mp, atac]
blanchet@49305
   312
          else EVERY' [rtac ord_eq_le_trans, rtac trans, rtac thm,
blanchet@49305
   313
            rtac trans_fun_cong_image_id_id_apply, atac])
blanchet@48975
   314
        (1 upto (m + n) ~~ set_naturals)];
blanchet@48975
   315
  in
blanchet@48975
   316
    EVERY' [rtac (bis_def RS trans),
blanchet@48975
   317
      rtac iffI, etac conjE, etac conjI, CONJ_WRAP' mk_if_tac thms,
blanchet@48975
   318
      etac conjE, etac conjI, CONJ_WRAP' mk_only_if_tac thms] 1
blanchet@48975
   319
  end;
blanchet@48975
   320
blanchet@48975
   321
fun mk_bis_converse_tac m bis_rel rel_congs rel_converses =
blanchet@49305
   322
  EVERY' [stac bis_rel, dtac (bis_rel RS subst_id),
blanchet@48975
   323
    REPEAT_DETERM o etac conjE, rtac conjI,
blanchet@48975
   324
    CONJ_WRAP' (K (EVERY' [rtac @{thm converse_shift}, etac subset_trans,
blanchet@48975
   325
      rtac equalityD2, rtac @{thm converse_Times}])) rel_congs,
blanchet@48975
   326
    CONJ_WRAP' (fn (rel_cong, rel_converse) =>
blanchet@48975
   327
      EVERY' [rtac allI, rtac allI, rtac impI, rtac @{thm set_mp[OF equalityD2]},
blanchet@48975
   328
        rtac (rel_cong RS trans),
blanchet@48975
   329
        REPEAT_DETERM_N m o rtac @{thm diag_converse},
blanchet@48975
   330
        REPEAT_DETERM_N (length rel_congs) o rtac refl,
blanchet@48975
   331
        rtac rel_converse,
blanchet@48975
   332
        REPEAT_DETERM o etac allE,
blanchet@48975
   333
        rtac @{thm converseI}, etac mp, etac @{thm converseD}]) (rel_congs ~~ rel_converses)] 1;
blanchet@48975
   334
blanchet@48975
   335
fun mk_bis_O_tac m bis_rel rel_congs rel_Os =
blanchet@49305
   336
  EVERY' [stac bis_rel, REPEAT_DETERM o dtac (bis_rel RS subst_id),
blanchet@48975
   337
    REPEAT_DETERM o etac conjE, rtac conjI,
blanchet@48975
   338
    CONJ_WRAP' (K (EVERY' [etac @{thm relcomp_subset_Sigma}, atac])) rel_congs,
blanchet@48975
   339
    CONJ_WRAP' (fn (rel_cong, rel_O) =>
blanchet@48975
   340
      EVERY' [rtac allI, rtac allI, rtac impI, rtac @{thm set_mp[OF equalityD2]},
blanchet@48975
   341
        rtac (rel_cong RS trans),
blanchet@48975
   342
        REPEAT_DETERM_N m o rtac @{thm diag_Comp},
blanchet@48975
   343
        REPEAT_DETERM_N (length rel_congs) o rtac refl,
blanchet@48975
   344
        rtac rel_O,
blanchet@48975
   345
        etac @{thm relcompE},
blanchet@49305
   346
        REPEAT_DETERM o dtac Pair_eq_subst_id,
blanchet@48975
   347
        etac conjE, hyp_subst_tac,
blanchet@48975
   348
        REPEAT_DETERM o etac allE, rtac @{thm relcompI},
blanchet@48975
   349
        etac mp, atac, etac mp, atac]) (rel_congs ~~ rel_Os)] 1;
blanchet@48975
   350
blanchet@48975
   351
fun mk_bis_Gr_tac bis_rel rel_Grs mor_images morEs coalg_ins
blanchet@48975
   352
  {context = ctxt, prems = _} =
blanchet@48975
   353
  Local_Defs.unfold_tac ctxt (bis_rel :: @{thm diag_Gr} :: rel_Grs) THEN
blanchet@48975
   354
  EVERY' [rtac conjI,
blanchet@48975
   355
    CONJ_WRAP' (fn thm => rtac (@{thm Gr_incl} RS ssubst) THEN' etac thm) mor_images,
blanchet@48975
   356
    CONJ_WRAP' (fn (coalg_in, morE) =>
blanchet@48975
   357
      EVERY' [rtac allI, rtac allI, rtac impI, rtac @{thm GrI}, etac coalg_in,
blanchet@48975
   358
        etac @{thm GrD1}, etac (morE RS trans), etac @{thm GrD1},
blanchet@48975
   359
        etac (@{thm GrD2} RS arg_cong)]) (coalg_ins ~~ morEs)] 1;
blanchet@48975
   360
blanchet@48975
   361
fun mk_bis_Union_tac bis_def in_monos {context = ctxt, prems = _} =
blanchet@48975
   362
  let
blanchet@48975
   363
    val n = length in_monos;
blanchet@48975
   364
    val ks = 1 upto n;
blanchet@48975
   365
  in
blanchet@48975
   366
    Local_Defs.unfold_tac ctxt [bis_def] THEN
blanchet@48975
   367
    EVERY' [rtac conjI,
blanchet@48975
   368
      CONJ_WRAP' (fn i =>
blanchet@48975
   369
        EVERY' [rtac @{thm UN_least}, dtac bspec, atac,
blanchet@48975
   370
          dtac conjunct1, etac (mk_conjunctN n i)]) ks,
blanchet@48975
   371
      CONJ_WRAP' (fn (i, in_mono) =>
blanchet@48975
   372
        EVERY' [rtac allI, rtac allI, rtac impI, etac @{thm UN_E}, dtac bspec, atac,
blanchet@48975
   373
          dtac conjunct2, dtac (mk_conjunctN n i), etac allE, etac allE, dtac mp,
blanchet@48975
   374
          atac, etac bexE, rtac bexI, atac, rtac in_mono,
blanchet@48975
   375
          REPEAT_DETERM_N n o etac @{thm incl_UNION_I[OF _ subset_refl]},
blanchet@48975
   376
          atac]) (ks ~~ in_monos)] 1
blanchet@48975
   377
  end;
blanchet@48975
   378
blanchet@48975
   379
fun mk_sbis_lsbis_tac lsbis_defs bis_Union bis_cong =
blanchet@48975
   380
  let
blanchet@48975
   381
    val n = length lsbis_defs;
blanchet@48975
   382
  in
blanchet@48975
   383
    EVERY' [rtac (Thm.permute_prems 0 1 bis_cong), EVERY' (map rtac lsbis_defs),
blanchet@48975
   384
      rtac bis_Union, rtac ballI, REPEAT_DETERM o eresolve_tac [CollectE, conjE, exE],
blanchet@48975
   385
      hyp_subst_tac, etac bis_cong, EVERY' (map (rtac o mk_nth_conv n) (1 upto n))] 1
blanchet@48975
   386
  end;
blanchet@48975
   387
blanchet@48975
   388
fun mk_incl_lsbis_tac n i lsbis_def =
blanchet@48975
   389
  EVERY' [rtac @{thm xt1(3)}, rtac lsbis_def, rtac @{thm incl_UNION_I}, rtac CollectI,
blanchet@48975
   390
    REPEAT_DETERM_N n o rtac exI, rtac conjI, rtac refl, atac, rtac equalityD2,
blanchet@48975
   391
    rtac (mk_nth_conv n i)] 1;
blanchet@48975
   392
blanchet@48975
   393
fun mk_equiv_lsbis_tac sbis_lsbis lsbis_incl incl_lsbis bis_diag bis_converse bis_O =
blanchet@49305
   394
  EVERY' [rtac (@{thm equiv_def} RS ssubst_id),
blanchet@48975
   395
blanchet@49305
   396
    rtac conjI, rtac (@{thm refl_on_def} RS ssubst_id),
blanchet@49305
   397
    rtac conjI, rtac lsbis_incl, rtac ballI, rtac set_mp,
blanchet@48975
   398
    rtac incl_lsbis, rtac bis_diag, atac, etac @{thm diagI},
blanchet@48975
   399
blanchet@49305
   400
    rtac conjI, rtac (@{thm sym_def} RS ssubst_id),
blanchet@49305
   401
    rtac allI, rtac allI, rtac impI, rtac set_mp,
blanchet@48975
   402
    rtac incl_lsbis, rtac bis_converse, rtac sbis_lsbis, etac @{thm converseI},
blanchet@48975
   403
blanchet@49305
   404
    rtac (@{thm trans_def} RS ssubst_id),
blanchet@49305
   405
    rtac allI, rtac allI, rtac allI, rtac impI, rtac impI, rtac set_mp,
blanchet@48975
   406
    rtac incl_lsbis, rtac bis_O, rtac sbis_lsbis, rtac sbis_lsbis,
blanchet@48975
   407
    etac @{thm relcompI}, atac] 1;
blanchet@48975
   408
blanchet@48975
   409
fun mk_coalgT_tac m defs strT_defs set_naturalss {context = ctxt, prems = _} =
blanchet@48975
   410
  let
blanchet@48975
   411
    val n = length strT_defs;
blanchet@48975
   412
    val ks = 1 upto n;
blanchet@48975
   413
    fun coalg_tac (i, ((passive_sets, active_sets), def)) =
blanchet@48975
   414
      EVERY' [rtac ballI, REPEAT_DETERM o eresolve_tac [CollectE, exE, conjE],
blanchet@48975
   415
        hyp_subst_tac, rtac (def RS trans RS @{thm ssubst_mem}), etac (arg_cong RS trans),
blanchet@48975
   416
        rtac (mk_sum_casesN n i), rtac CollectI,
blanchet@49305
   417
        EVERY' (map (fn thm => EVERY' [rtac conjI, rtac (thm RS ord_eq_le_trans),
blanchet@49305
   418
          etac ((trans OF [@{thm image_id} RS fun_cong, @{thm id_apply}]) RS ord_eq_le_trans)])
blanchet@49305
   419
          passive_sets),
blanchet@49305
   420
        CONJ_WRAP' (fn (i, thm) => EVERY' [rtac (thm RS ord_eq_le_trans),
blanchet@48975
   421
          rtac @{thm image_subsetI}, rtac CollectI, rtac exI, rtac exI, rtac conjI, rtac refl,
blanchet@48975
   422
          rtac conjI,
blanchet@49305
   423
          rtac conjI, etac @{thm empty_Shift}, dtac set_rev_mp,
blanchet@48975
   424
            etac equalityD1, etac CollectD,
blanchet@48975
   425
          rtac conjI, etac @{thm Shift_clists},
blanchet@48975
   426
          rtac conjI, etac @{thm Shift_prefCl},
blanchet@48975
   427
          rtac conjI, rtac ballI,
blanchet@48975
   428
            rtac conjI, dtac @{thm iffD1[OF ball_conj_distrib]}, dtac conjunct1,
blanchet@48975
   429
            SELECT_GOAL (Local_Defs.unfold_tac ctxt @{thms Succ_Shift shift_def}),
blanchet@48975
   430
            etac bspec, etac @{thm ShiftD},
blanchet@48975
   431
            CONJ_WRAP' (fn i => EVERY' [rtac ballI, etac CollectE, dtac @{thm ShiftD},
blanchet@48975
   432
              dtac bspec, etac thin_rl, atac, dtac conjunct2, dtac (mk_conjunctN n i),
blanchet@48975
   433
              dtac bspec, rtac CollectI, etac @{thm set_mp[OF equalityD1[OF Succ_Shift]]},
blanchet@48975
   434
              REPEAT_DETERM o eresolve_tac [exE, conjE], rtac exI,
blanchet@48975
   435
              rtac conjI, rtac (@{thm shift_def} RS fun_cong RS trans),
blanchet@48975
   436
              rtac (@{thm append_Cons} RS sym RS arg_cong RS trans), atac,
blanchet@48975
   437
              REPEAT_DETERM_N m o (rtac conjI THEN' atac),
blanchet@48975
   438
              CONJ_WRAP' (K (EVERY' [etac trans, rtac @{thm Collect_cong},
blanchet@48975
   439
                rtac @{thm eqset_imp_iff}, rtac sym, rtac trans, rtac @{thm Succ_Shift},
blanchet@48975
   440
                rtac (@{thm append_Cons} RS sym RS arg_cong)])) ks]) ks,
blanchet@48975
   441
          rtac allI, rtac impI, REPEAT_DETERM o eresolve_tac [allE, impE],
blanchet@48975
   442
          etac @{thm not_in_Shift}, rtac trans, rtac (@{thm shift_def} RS fun_cong), atac,
blanchet@48975
   443
          dtac bspec, atac, dtac conjunct2, dtac (mk_conjunctN n i), dtac bspec,
blanchet@48975
   444
          etac @{thm set_mp[OF equalityD1]}, atac,
blanchet@48975
   445
          REPEAT_DETERM o eresolve_tac [exE, conjE], rtac exI,
blanchet@48975
   446
          rtac conjI, rtac (@{thm shift_def} RS fun_cong RS trans),
blanchet@48975
   447
          etac (@{thm append_Nil} RS sym RS arg_cong RS trans),
blanchet@48975
   448
          REPEAT_DETERM_N m o (rtac conjI THEN' atac),
blanchet@48975
   449
          CONJ_WRAP' (K (EVERY' [etac trans, rtac @{thm Collect_cong},
blanchet@48975
   450
            rtac @{thm eqset_imp_iff}, rtac sym, rtac trans, rtac @{thm Succ_Shift},
blanchet@48975
   451
            rtac (@{thm append_Nil} RS sym RS arg_cong)])) ks]) (ks ~~ active_sets)];
blanchet@48975
   452
  in
blanchet@48975
   453
    Local_Defs.unfold_tac ctxt defs THEN
blanchet@48975
   454
    CONJ_WRAP' coalg_tac (ks ~~ (map (chop m) set_naturalss ~~ strT_defs)) 1
blanchet@48975
   455
  end;
blanchet@48975
   456
blanchet@48975
   457
fun mk_card_of_carT_tac m isNode_defs sbd_sbd
blanchet@48975
   458
  sbd_card_order sbd_Card_order sbd_Cinfinite sbd_Cnotzero in_sbds =
blanchet@48975
   459
  let
blanchet@48975
   460
    val n = length isNode_defs;
blanchet@48975
   461
  in
blanchet@48975
   462
    EVERY' [rtac (Thm.permute_prems 0 1 ctrans),
blanchet@48975
   463
      rtac @{thm card_of_Sigma_ordLeq_Cinfinite}, rtac @{thm Cinfinite_cexp},
blanchet@48975
   464
      if m = 0 then rtac @{thm ordLeq_refl} else rtac @{thm ordLeq_csum2},
blanchet@48975
   465
      rtac @{thm Card_order_ctwo}, rtac @{thm Cinfinite_cexp},
blanchet@48975
   466
      rtac @{thm ctwo_ordLeq_Cinfinite}, rtac sbd_Cinfinite, rtac sbd_Cinfinite,
blanchet@48975
   467
      rtac ctrans, rtac @{thm card_of_diff},
blanchet@49305
   468
      rtac ordIso_ordLeq_trans, rtac @{thm card_of_Field_ordIso},
blanchet@49305
   469
      rtac @{thm Card_order_cpow}, rtac ordIso_ordLeq_trans,
blanchet@48975
   470
      rtac @{thm cpow_cexp_ctwo}, rtac ctrans, rtac @{thm cexp_mono1_Cnotzero},
blanchet@48975
   471
      if m = 0 then rtac @{thm ordLeq_refl} else rtac @{thm ordLeq_csum2},
blanchet@48975
   472
      rtac @{thm Card_order_ctwo}, rtac @{thm ctwo_Cnotzero}, rtac @{thm Card_order_clists},
blanchet@49305
   473
      rtac @{thm cexp_mono2_Cnotzero}, rtac ordIso_ordLeq_trans,
blanchet@48975
   474
      rtac @{thm clists_Cinfinite},
blanchet@48975
   475
      if n = 1 then rtac sbd_Cinfinite else rtac (sbd_Cinfinite RS @{thm Cinfinite_csum1}),
blanchet@49305
   476
      rtac ordIso_ordLeq_trans, rtac sbd_sbd, rtac @{thm infinite_ordLeq_cexp},
blanchet@48975
   477
      rtac sbd_Cinfinite,
blanchet@48975
   478
      if m = 0 then rtac @{thm ctwo_Cnotzero} else rtac @{thm csum_Cnotzero2[OF ctwo_Cnotzero]},
blanchet@48975
   479
      rtac @{thm Cnotzero_clists},
blanchet@49305
   480
      rtac ballI, rtac ordIso_ordLeq_trans, rtac @{thm card_of_Func_Ffunc},
blanchet@49305
   481
      rtac ordIso_ordLeq_trans, rtac @{thm Func_cexp},
blanchet@48975
   482
      rtac ctrans, rtac @{thm cexp_mono},
blanchet@48975
   483
      rtac @{thm ordLeq_ordIso_trans},
blanchet@48975
   484
      CONJ_WRAP_GEN' (rtac (Thm.permute_prems 0 1
blanchet@48975
   485
          (sbd_Cinfinite RS @{thm Cinfinite_cexp[OF ordLeq_csum2[OF Card_order_ctwo]]}
blanchet@48975
   486
        RSN (3, @{thm Un_Cinfinite_bound}))))
blanchet@48975
   487
        (fn thm => EVERY' [rtac ctrans, rtac @{thm card_of_image}, rtac thm]) (rev in_sbds),
blanchet@48975
   488
      rtac @{thm cexp_cong1_Cnotzero}, rtac @{thm csum_cong1},
blanchet@48975
   489
      REPEAT_DETERM_N m o rtac @{thm csum_cong2},
blanchet@48975
   490
      CONJ_WRAP_GEN' (rtac @{thm csum_cong})
blanchet@48975
   491
        (K (rtac (sbd_Card_order RS @{thm card_of_Field_ordIso}))) in_sbds,
blanchet@48975
   492
      rtac sbd_Card_order,
blanchet@48975
   493
      rtac @{thm csum_Cnotzero2}, rtac @{thm ctwo_Cnotzero},
blanchet@48975
   494
      rtac @{thm csum_Cnotzero2}, rtac @{thm ctwo_Cnotzero},
blanchet@48975
   495
      rtac @{thm ordLeq_ordIso_trans}, etac @{thm clists_bound},
blanchet@48975
   496
      rtac @{thm clists_Cinfinite}, TRY o rtac @{thm Cinfinite_csum1}, rtac sbd_Cinfinite,
blanchet@48975
   497
      rtac disjI2, rtac @{thm cone_ordLeq_cexp}, rtac @{thm cone_ordLeq_cexp},
blanchet@48975
   498
      rtac ctrans, rtac @{thm cone_ordLeq_ctwo}, rtac @{thm ordLeq_csum2},
blanchet@48975
   499
      rtac @{thm Card_order_ctwo}, rtac FalseE, etac @{thm cpow_clists_czero}, atac,
blanchet@48975
   500
      rtac @{thm card_of_Card_order},
blanchet@49305
   501
      rtac ordIso_ordLeq_trans, rtac @{thm cexp_cprod},
blanchet@48975
   502
      rtac @{thm csum_Cnotzero2}, rtac @{thm ctwo_Cnotzero},
blanchet@49305
   503
      rtac ordIso_ordLeq_trans, rtac @{thm cexp_cong2_Cnotzero},
blanchet@48975
   504
      rtac @{thm ordIso_transitive}, rtac @{thm cprod_cong2}, rtac sbd_sbd,
blanchet@48975
   505
      rtac @{thm cprod_infinite}, rtac sbd_Cinfinite,
blanchet@48975
   506
      rtac @{thm csum_Cnotzero2}, rtac @{thm ctwo_Cnotzero}, rtac @{thm Card_order_cprod},
blanchet@48975
   507
      rtac ctrans, rtac @{thm cexp_mono1_Cnotzero},
blanchet@49305
   508
      rtac ordIso_ordLeq_trans, rtac @{thm ordIso_transitive}, rtac @{thm csum_cong1},
blanchet@48975
   509
      rtac @{thm ordIso_transitive},
blanchet@48975
   510
      REPEAT_DETERM_N m o rtac @{thm csum_cong2},
blanchet@48975
   511
      rtac sbd_sbd,
blanchet@48975
   512
      BNF_Tactics.mk_rotate_eq_tac (rtac @{thm ordIso_refl} THEN'
blanchet@48975
   513
        FIRST' [rtac @{thm card_of_Card_order},
blanchet@48975
   514
          rtac @{thm Card_order_csum}, rtac sbd_Card_order])
blanchet@48975
   515
        @{thm ordIso_transitive} @{thm csum_assoc} @{thm csum_com} @{thm csum_cong}
blanchet@48975
   516
        (1 upto m + 1) (m + 1 :: (1 upto m)),
blanchet@48975
   517
      if m = 0 then K all_tac else EVERY' [rtac @{thm ordIso_transitive}, rtac @{thm csum_assoc}],
blanchet@48975
   518
      rtac @{thm csum_com}, rtac @{thm csum_cexp'}, rtac sbd_Cinfinite,
blanchet@48975
   519
      if m = 0 then rtac @{thm Card_order_ctwo} else rtac @{thm Card_order_csum},
blanchet@48975
   520
      if m = 0 then rtac @{thm ordLeq_refl} else rtac @{thm ordLeq_csum2},
blanchet@48975
   521
      rtac @{thm Card_order_ctwo},
blanchet@48975
   522
      rtac @{thm csum_Cnotzero2}, rtac @{thm ctwo_Cnotzero}, rtac sbd_Card_order,
blanchet@49305
   523
      rtac ordIso_ordLeq_trans, rtac @{thm cexp_cprod_ordLeq},
blanchet@48975
   524
      if m = 0 then rtac @{thm ctwo_Cnotzero} else rtac @{thm csum_Cnotzero2[OF ctwo_Cnotzero]},
blanchet@48975
   525
      rtac sbd_Cinfinite, rtac sbd_Cnotzero, rtac @{thm ordLeq_refl}, rtac sbd_Card_order,
blanchet@48975
   526
      rtac @{thm cexp_mono2_Cnotzero}, rtac @{thm infinite_ordLeq_cexp},
blanchet@48975
   527
      rtac sbd_Cinfinite,
blanchet@48975
   528
      if m = 0 then rtac @{thm ctwo_Cnotzero} else rtac @{thm csum_Cnotzero2[OF ctwo_Cnotzero]},
blanchet@48975
   529
      rtac sbd_Cnotzero,
blanchet@48975
   530
      rtac @{thm card_of_mono1}, rtac subsetI,
blanchet@48975
   531
      REPEAT_DETERM o eresolve_tac [CollectE, exE, conjE, @{thm prod_caseE}], hyp_subst_tac,
blanchet@49305
   532
      rtac @{thm SigmaI}, rtac @{thm DiffI}, rtac set_mp, rtac equalityD2,
blanchet@48975
   533
      rtac (@{thm cpow_def} RS arg_cong RS trans), rtac (@{thm Pow_def} RS arg_cong RS trans),
blanchet@48975
   534
      rtac @{thm Field_card_of}, rtac CollectI, atac, rtac notI, etac @{thm singletonE},
blanchet@49305
   535
      hyp_subst_tac, etac @{thm emptyE}, rtac (@{thm Ffunc_def} RS equalityD2 RS set_mp),
blanchet@48975
   536
      rtac CollectI, rtac conjI, rtac ballI, dtac bspec, etac thin_rl, atac, dtac conjunct1,
blanchet@48975
   537
      CONJ_WRAP_GEN' (etac disjE) (fn (i, def) => EVERY'
blanchet@49305
   538
        [rtac (mk_UnN n i), dtac (def RS subst_id),
blanchet@48975
   539
        REPEAT_DETERM o eresolve_tac [exE, conjE], rtac @{thm image_eqI}, atac, rtac CollectI,
blanchet@48975
   540
        REPEAT_DETERM_N m o (rtac conjI THEN' atac),
blanchet@49305
   541
        CONJ_WRAP' (K (EVERY' [etac ord_eq_le_trans, rtac subset_trans,
blanchet@48975
   542
          rtac @{thm subset_UNIV}, rtac equalityD2, rtac @{thm Field_card_order},
blanchet@48975
   543
          rtac sbd_card_order])) isNode_defs]) (1 upto n ~~ isNode_defs),
blanchet@48975
   544
      atac] 1
blanchet@48975
   545
  end;
blanchet@48975
   546
blanchet@48975
   547
fun mk_carT_set_tac n i carT_def strT_def isNode_def set_natural {context = ctxt, prems = _}=
blanchet@49305
   548
  EVERY' [dtac (carT_def RS equalityD1 RS set_mp),
blanchet@48975
   549
    REPEAT_DETERM o eresolve_tac [CollectE, exE, conjE],
blanchet@49305
   550
    dtac Pair_eq_subst_id,
blanchet@48975
   551
    etac conjE, hyp_subst_tac,
blanchet@49305
   552
    dtac (isNode_def RS subst_id),
blanchet@48975
   553
    REPEAT_DETERM o eresolve_tac [exE, conjE],
blanchet@49305
   554
    rtac (equalityD2 RS set_mp),
blanchet@48975
   555
    rtac (strT_def RS arg_cong RS trans),
blanchet@48975
   556
    etac (arg_cong RS trans),
blanchet@48975
   557
    fo_rtac (mk_sum_casesN n i RS arg_cong RS trans) ctxt,
blanchet@49305
   558
    rtac set_natural, rtac imageI, etac (equalityD2 RS set_mp), rtac CollectI,
blanchet@48975
   559
    etac @{thm prefCl_Succ}, atac] 1;
blanchet@48975
   560
blanchet@48975
   561
fun mk_strT_hset_tac n m j arg_cong_cTs cTs cts carT_defs strT_defs isNode_defs
blanchet@48975
   562
  set_incl_hsets set_hset_incl_hsetss coalg_setss carT_setss coalgT set_naturalss =
blanchet@48975
   563
  let
blanchet@48975
   564
    val set_naturals = map (fn xs => nth xs (j - 1)) set_naturalss;
blanchet@48975
   565
    val ks = 1 upto n;
blanchet@48975
   566
    fun base_tac (i, (cT, (strT_def, (set_incl_hset, set_natural)))) =
blanchet@48975
   567
      CONJ_WRAP' (fn (i', (carT_def, isNode_def)) => rtac impI THEN' etac conjE THEN'
blanchet@48975
   568
        (if i = i'
blanchet@48975
   569
        then EVERY' [rtac @{thm xt1(4)}, rtac set_incl_hset,
blanchet@48975
   570
          rtac (strT_def RS arg_cong RS trans), etac (arg_cong RS trans),
blanchet@48975
   571
          rtac (Thm.permute_prems 0 1 (set_natural RS box_equals)),
blanchet@48975
   572
          rtac (trans OF [@{thm image_id} RS fun_cong, @{thm id_apply}]),
blanchet@48975
   573
          rtac (mk_sum_casesN n i RS (Drule.instantiate' [cT] [] arg_cong) RS sym)]
blanchet@49305
   574
        else EVERY' [dtac (carT_def RS equalityD1 RS set_mp),
blanchet@48975
   575
          REPEAT_DETERM o eresolve_tac [CollectE, exE], etac conjE,
blanchet@49305
   576
          dtac conjunct2, dtac Pair_eq_subst_id, etac conjE,
blanchet@49305
   577
          hyp_subst_tac, dtac (isNode_def RS subst_id),
blanchet@48975
   578
          REPEAT_DETERM o eresolve_tac [exE, conjE],
blanchet@48975
   579
          rtac (mk_InN_not_InM i i' RS notE), etac (sym RS trans), atac]))
blanchet@48975
   580
      (ks ~~ (carT_defs ~~ isNode_defs));
blanchet@48975
   581
    fun step_tac (i, (coalg_sets, (carT_sets, set_hset_incl_hsets))) =
blanchet@48975
   582
      dtac (mk_conjunctN n i) THEN'
blanchet@48975
   583
      CONJ_WRAP' (fn (coalg_set, (carT_set, set_hset_incl_hset)) =>
blanchet@48975
   584
        EVERY' [rtac impI, etac conjE, etac impE, rtac conjI,
blanchet@49305
   585
          rtac (coalgT RS coalg_set RS set_mp), atac, etac carT_set, atac, atac,
blanchet@48975
   586
          etac (@{thm shift_def} RS fun_cong RS trans), etac subset_trans,
blanchet@48975
   587
          rtac set_hset_incl_hset, etac carT_set, atac, atac])
blanchet@48975
   588
      (coalg_sets ~~ (carT_sets ~~ set_hset_incl_hsets));
blanchet@48975
   589
  in
blanchet@48975
   590
    EVERY' [rtac (Drule.instantiate' cTs cts @{thm list.induct}),
blanchet@48975
   591
      REPEAT_DETERM o rtac allI, rtac impI,
blanchet@48975
   592
      CONJ_WRAP' base_tac
blanchet@48975
   593
        (ks ~~ (arg_cong_cTs ~~ (strT_defs ~~ (set_incl_hsets ~~ set_naturals)))),
blanchet@48975
   594
      REPEAT_DETERM o rtac allI, rtac impI,
blanchet@48975
   595
      REPEAT_DETERM o eresolve_tac [allE, impE], etac @{thm ShiftI},
blanchet@48975
   596
      CONJ_WRAP' (fn i => dtac (mk_conjunctN n i) THEN' rtac (mk_sumEN n) THEN'
blanchet@48975
   597
        CONJ_WRAP_GEN' (K all_tac) step_tac
blanchet@48975
   598
          (ks ~~ (drop m coalg_setss ~~ (carT_setss ~~ set_hset_incl_hsetss)))) ks] 1
blanchet@48975
   599
  end;
blanchet@48975
   600
blanchet@48975
   601
fun mk_isNode_hset_tac n isNode_def strT_hsets {context = ctxt, prems = _} =
blanchet@48975
   602
  let
blanchet@48975
   603
    val m = length strT_hsets;
blanchet@48975
   604
  in
blanchet@48975
   605
    if m = 0 then atac 1
blanchet@48975
   606
    else (Local_Defs.unfold_tac ctxt [isNode_def] THEN
blanchet@48975
   607
      EVERY' [REPEAT_DETERM o eresolve_tac [CollectE, exE, conjE],
blanchet@48975
   608
        rtac exI, rtac conjI, atac,
blanchet@48975
   609
        CONJ_WRAP' (fn (thm, i) =>  if i > m then atac
blanchet@48975
   610
          else EVERY' [rtac (thm RS subset_trans), atac, rtac conjI, atac, atac, atac])
blanchet@48975
   611
        (strT_hsets @ (replicate n mp) ~~ (1 upto (m + n)))] 1)
blanchet@48975
   612
  end;
blanchet@48975
   613
blanchet@48975
   614
fun mk_Lev_sbd_tac cts Lev_0s Lev_Sucs to_sbdss =
blanchet@48975
   615
  let
blanchet@48975
   616
    val n = length Lev_0s;
blanchet@48975
   617
    val ks = 1 upto n;
blanchet@48975
   618
  in
blanchet@49305
   619
    EVERY' [rtac (Drule.instantiate' [] cts nat_induct),
blanchet@48975
   620
      REPEAT_DETERM o rtac allI,
blanchet@48975
   621
      CONJ_WRAP' (fn Lev_0 =>
blanchet@49305
   622
        EVERY' (map rtac [ord_eq_le_trans, Lev_0, @{thm Nil_clists}])) Lev_0s,
blanchet@48975
   623
      REPEAT_DETERM o rtac allI,
blanchet@48975
   624
      CONJ_WRAP' (fn (Lev_Suc, to_sbds) =>
blanchet@49305
   625
        EVERY' [rtac ord_eq_le_trans, rtac Lev_Suc,
blanchet@48975
   626
          CONJ_WRAP_GEN' (rtac (Thm.permute_prems 0 1 @{thm Un_least}))
blanchet@48975
   627
            (fn (i, to_sbd) => EVERY' [rtac subsetI,
blanchet@48975
   628
              REPEAT_DETERM o eresolve_tac [CollectE, exE, conjE], hyp_subst_tac,
blanchet@48975
   629
              rtac @{thm Cons_clists}, rtac (mk_InN_Field n i), etac to_sbd,
blanchet@49305
   630
              etac set_rev_mp, REPEAT_DETERM o etac allE,
blanchet@48975
   631
              etac (mk_conjunctN n i)])
blanchet@48975
   632
          (rev (ks ~~ to_sbds))])
blanchet@48975
   633
      (Lev_Sucs ~~ to_sbdss)] 1
blanchet@48975
   634
  end;
blanchet@48975
   635
blanchet@48975
   636
fun mk_length_Lev_tac cts Lev_0s Lev_Sucs =
blanchet@48975
   637
  let
blanchet@48975
   638
    val n = length Lev_0s;
blanchet@48975
   639
    val ks = n downto 1;
blanchet@48975
   640
  in
blanchet@49305
   641
    EVERY' [rtac (Drule.instantiate' [] cts nat_induct),
blanchet@48975
   642
      REPEAT_DETERM o rtac allI,
blanchet@48975
   643
      CONJ_WRAP' (fn Lev_0 =>
blanchet@49305
   644
        EVERY' [rtac impI, dtac (Lev_0 RS equalityD1 RS set_mp),
blanchet@48975
   645
          etac @{thm singletonE}, etac ssubst, rtac @{thm list.size(3)}]) Lev_0s,
blanchet@48975
   646
      REPEAT_DETERM o rtac allI,
blanchet@48975
   647
      CONJ_WRAP' (fn Lev_Suc =>
blanchet@49305
   648
        EVERY' [rtac impI, dtac (Lev_Suc RS equalityD1 RS set_mp),
blanchet@48975
   649
          CONJ_WRAP_GEN' (etac (Thm.permute_prems 1 1 UnE))
blanchet@48975
   650
            (fn i => EVERY' [REPEAT_DETERM o eresolve_tac [CollectE, exE, conjE], hyp_subst_tac,
blanchet@48975
   651
              rtac trans, rtac @{thm length_Cons}, rtac @{thm arg_cong[of _ _ Suc]},
blanchet@48975
   652
              REPEAT_DETERM o etac allE, dtac (mk_conjunctN n i), etac mp, atac]) ks])
blanchet@48975
   653
      Lev_Sucs] 1
blanchet@48975
   654
  end;
blanchet@48975
   655
blanchet@48975
   656
fun mk_length_Lev'_tac length_Lev =
blanchet@48975
   657
  EVERY' [ftac length_Lev, etac ssubst, atac] 1;
blanchet@48975
   658
blanchet@48975
   659
fun mk_prefCl_Lev_tac cts Lev_0s Lev_Sucs =
blanchet@48975
   660
  let
blanchet@48975
   661
    val n = length Lev_0s;
blanchet@48975
   662
    val ks = n downto 1;
blanchet@48975
   663
  in
blanchet@49305
   664
    EVERY' [rtac (Drule.instantiate' [] cts nat_induct),
blanchet@48975
   665
      REPEAT_DETERM o rtac allI,
blanchet@48975
   666
      CONJ_WRAP' (fn Lev_0 =>
blanchet@49305
   667
        EVERY' [rtac impI, etac conjE, dtac (Lev_0 RS equalityD1 RS set_mp),
Christian@49091
   668
          etac @{thm singletonE}, hyp_subst_tac, dtac @{thm prefix_Nil[THEN subst, of "%x. x"]},
blanchet@48975
   669
          hyp_subst_tac, rtac @{thm set_mp[OF equalityD2[OF trans[OF arg_cong[OF list.size(3)]]]]},
blanchet@48975
   670
          rtac Lev_0, rtac @{thm singletonI}]) Lev_0s,
blanchet@48975
   671
      REPEAT_DETERM o rtac allI,
blanchet@48975
   672
      CONJ_WRAP' (fn (Lev_0, Lev_Suc) =>
blanchet@49305
   673
        EVERY' [rtac impI, etac conjE, dtac (Lev_Suc RS equalityD1 RS set_mp),
blanchet@48975
   674
          CONJ_WRAP_GEN' (etac (Thm.permute_prems 1 1 UnE))
blanchet@48975
   675
            (fn i => EVERY' [REPEAT_DETERM o eresolve_tac [CollectE, exE, conjE], hyp_subst_tac,
Christian@49091
   676
              dtac @{thm prefix_Cons[THEN subst, of "%x. x"]}, etac disjE, hyp_subst_tac,
blanchet@48975
   677
              rtac @{thm set_mp[OF equalityD2[OF trans[OF arg_cong[OF list.size(3)]]]]},
blanchet@48975
   678
              rtac Lev_0, rtac @{thm singletonI},
blanchet@48975
   679
              REPEAT_DETERM o eresolve_tac [exE, conjE], hyp_subst_tac,
blanchet@48975
   680
              rtac @{thm set_mp[OF equalityD2[OF trans[OF arg_cong[OF length_Cons]]]]},
blanchet@48975
   681
              rtac Lev_Suc, rtac (mk_UnN n i), rtac CollectI, REPEAT_DETERM o rtac exI, rtac conjI,
blanchet@48975
   682
              rtac refl, etac conjI, REPEAT_DETERM o etac allE, dtac (mk_conjunctN n i),
blanchet@48975
   683
              etac mp, etac conjI, atac]) ks])
blanchet@48975
   684
      (Lev_0s ~~ Lev_Sucs)] 1
blanchet@48975
   685
  end;
blanchet@48975
   686
blanchet@48975
   687
fun mk_rv_last_tac cTs cts rv_Nils rv_Conss =
blanchet@48975
   688
  let
blanchet@48975
   689
    val n = length rv_Nils;
blanchet@48975
   690
    val ks = 1 upto n;
blanchet@48975
   691
  in
blanchet@48975
   692
    EVERY' [rtac (Drule.instantiate' cTs cts @{thm list.induct}),
blanchet@48975
   693
      REPEAT_DETERM o rtac allI,
blanchet@48975
   694
      CONJ_WRAP' (fn rv_Cons =>
blanchet@48975
   695
        CONJ_WRAP' (fn (i, rv_Nil) => (EVERY' [rtac exI,
blanchet@48975
   696
          rtac (@{thm append_Nil} RS arg_cong RS trans),
blanchet@48975
   697
          rtac (rv_Cons RS trans), rtac (mk_sum_casesN n i RS arg_cong RS trans), rtac rv_Nil]))
blanchet@48975
   698
        (ks ~~ rv_Nils))
blanchet@48975
   699
      rv_Conss,
blanchet@48975
   700
      REPEAT_DETERM o rtac allI, rtac (mk_sumEN n),
blanchet@48975
   701
      EVERY' (map (fn i =>
blanchet@48975
   702
        CONJ_WRAP' (fn rv_Cons => EVERY' [REPEAT_DETERM o etac allE, dtac (mk_conjunctN n i),
blanchet@48975
   703
          CONJ_WRAP' (fn i' => EVERY' [dtac (mk_conjunctN n i'), etac exE, rtac exI,
blanchet@48975
   704
            rtac (@{thm append_Cons} RS arg_cong RS trans),
blanchet@49328
   705
            rtac (rv_Cons RS trans), etac (sum_case_weak_cong RS arg_cong RS trans),
blanchet@48975
   706
            rtac (mk_sum_casesN n i RS arg_cong RS trans), atac])
blanchet@48975
   707
          ks])
blanchet@48975
   708
        rv_Conss)
blanchet@48975
   709
      ks)] 1
blanchet@48975
   710
  end;
blanchet@48975
   711
blanchet@48975
   712
fun mk_set_rv_Lev_tac m cts Lev_0s Lev_Sucs rv_Nils rv_Conss coalg_setss from_to_sbdss =
blanchet@48975
   713
  let
blanchet@48975
   714
    val n = length Lev_0s;
blanchet@48975
   715
    val ks = 1 upto n;
blanchet@48975
   716
  in
blanchet@49305
   717
    EVERY' [rtac (Drule.instantiate' [] cts nat_induct),
blanchet@48975
   718
      REPEAT_DETERM o rtac allI,
blanchet@48975
   719
      CONJ_WRAP' (fn (i, ((Lev_0, rv_Nil), coalg_sets)) =>
blanchet@48975
   720
        EVERY' [rtac impI, REPEAT_DETERM o etac conjE,
blanchet@49305
   721
          dtac (Lev_0 RS equalityD1 RS set_mp), etac @{thm singletonE}, etac ssubst,
blanchet@49305
   722
          rtac (rv_Nil RS arg_cong RS ssubst_id),
blanchet@49305
   723
          rtac (mk_sum_casesN n i RS ssubst_id),
blanchet@48975
   724
          CONJ_WRAP' (fn thm => etac thm THEN' atac) (take m coalg_sets)])
blanchet@48975
   725
      (ks ~~ ((Lev_0s ~~ rv_Nils) ~~ coalg_setss)),
blanchet@48975
   726
      REPEAT_DETERM o rtac allI,
blanchet@48975
   727
      CONJ_WRAP' (fn ((Lev_Suc, rv_Cons), (from_to_sbds, coalg_sets)) =>
blanchet@49305
   728
        EVERY' [rtac impI, etac conjE, dtac (Lev_Suc RS equalityD1 RS set_mp),
blanchet@48975
   729
          CONJ_WRAP_GEN' (etac (Thm.permute_prems 1 1 UnE))
blanchet@48975
   730
            (fn (i, (from_to_sbd, coalg_set)) =>
blanchet@48975
   731
              EVERY' [REPEAT_DETERM o eresolve_tac [CollectE, exE, conjE], hyp_subst_tac,
blanchet@49305
   732
              rtac (rv_Cons RS arg_cong RS ssubst_id),
blanchet@49305
   733
              rtac (mk_sum_casesN n i RS arg_cong RS trans RS ssubst_id),
blanchet@48975
   734
              etac (from_to_sbd RS arg_cong), REPEAT_DETERM o etac allE,
blanchet@49305
   735
              dtac (mk_conjunctN n i), etac mp, etac conjI, etac set_rev_mp,
blanchet@48975
   736
              etac coalg_set, atac])
blanchet@48975
   737
          (rev (ks ~~ (from_to_sbds ~~ drop m coalg_sets)))])
blanchet@48975
   738
      ((Lev_Sucs ~~ rv_Conss) ~~ (from_to_sbdss ~~ coalg_setss))] 1
blanchet@48975
   739
  end;
blanchet@48975
   740
blanchet@48975
   741
fun mk_set_Lev_tac cts Lev_0s Lev_Sucs rv_Nils rv_Conss from_to_sbdss =
blanchet@48975
   742
  let
blanchet@48975
   743
    val n = length Lev_0s;
blanchet@48975
   744
    val ks = 1 upto n;
blanchet@48975
   745
  in
blanchet@49305
   746
    EVERY' [rtac (Drule.instantiate' [] cts nat_induct),
blanchet@48975
   747
      REPEAT_DETERM o rtac allI,
blanchet@48975
   748
      CONJ_WRAP' (fn ((i, (Lev_0, Lev_Suc)), rv_Nil) =>
blanchet@49305
   749
        EVERY' [rtac impI, dtac (Lev_0 RS equalityD1 RS set_mp),
blanchet@48975
   750
          etac @{thm singletonE}, hyp_subst_tac,
blanchet@48975
   751
          CONJ_WRAP' (fn i' => rtac impI THEN' dtac (sym RS trans) THEN' rtac rv_Nil THEN'
blanchet@48975
   752
            (if i = i'
blanchet@48975
   753
            then EVERY' [dtac (mk_InN_inject n i), hyp_subst_tac,
blanchet@48975
   754
              CONJ_WRAP' (fn (i'', Lev_0'') =>
blanchet@48975
   755
                EVERY' [rtac impI, rtac @{thm ssubst_mem[OF append_Nil]},
blanchet@49305
   756
                  rtac (Lev_Suc RS equalityD2 RS set_mp), rtac (mk_UnN n i''),
blanchet@48975
   757
                  rtac CollectI, REPEAT_DETERM o rtac exI, rtac conjI, rtac refl,
blanchet@49305
   758
                  etac conjI, rtac (Lev_0'' RS equalityD2 RS set_mp),
blanchet@48975
   759
                  rtac @{thm singletonI}])
blanchet@48975
   760
              (ks ~~ Lev_0s)]
blanchet@48975
   761
            else etac (mk_InN_not_InM i' i RS notE)))
blanchet@48975
   762
          ks])
blanchet@48975
   763
      ((ks ~~ (Lev_0s ~~ Lev_Sucs)) ~~ rv_Nils),
blanchet@48975
   764
      REPEAT_DETERM o rtac allI,
blanchet@48975
   765
      CONJ_WRAP' (fn ((Lev_Suc, rv_Cons), from_to_sbds) =>
blanchet@49305
   766
        EVERY' [rtac impI, dtac (Lev_Suc RS equalityD1 RS set_mp),
blanchet@48975
   767
          CONJ_WRAP_GEN' (etac (Thm.permute_prems 1 1 UnE))
blanchet@48975
   768
            (fn (i, from_to_sbd) =>
blanchet@48975
   769
              EVERY' [REPEAT_DETERM o eresolve_tac [CollectE, exE, conjE], hyp_subst_tac,
blanchet@48975
   770
                CONJ_WRAP' (fn i' => rtac impI THEN'
blanchet@48975
   771
                  CONJ_WRAP' (fn i'' =>
blanchet@49305
   772
                    EVERY' [rtac impI, rtac (Lev_Suc RS equalityD2 RS set_mp),
blanchet@48975
   773
                      rtac @{thm ssubst_mem[OF append_Cons]}, rtac (mk_UnN n i),
blanchet@48975
   774
                      rtac CollectI, REPEAT_DETERM o rtac exI, rtac conjI, rtac refl,
blanchet@48975
   775
                      rtac conjI, atac, dtac (sym RS trans RS sym),
blanchet@48975
   776
                      rtac (rv_Cons RS trans), rtac (mk_sum_casesN n i RS trans),
blanchet@48975
   777
                      etac (from_to_sbd RS arg_cong), REPEAT_DETERM o etac allE,
blanchet@48975
   778
                      dtac (mk_conjunctN n i), dtac mp, atac,
blanchet@48975
   779
                      dtac (mk_conjunctN n i'), dtac mp, atac,
blanchet@48975
   780
                      dtac (mk_conjunctN n i''), etac mp, atac])
blanchet@48975
   781
                  ks)
blanchet@48975
   782
                ks])
blanchet@48975
   783
          (rev (ks ~~ from_to_sbds))])
blanchet@48975
   784
      ((Lev_Sucs ~~ rv_Conss) ~~ from_to_sbdss)] 1
blanchet@48975
   785
  end;
blanchet@48975
   786
blanchet@48975
   787
fun mk_set_image_Lev_tac cts Lev_0s Lev_Sucs rv_Nils rv_Conss from_to_sbdss to_sbd_injss =
blanchet@48975
   788
  let
blanchet@48975
   789
    val n = length Lev_0s;
blanchet@48975
   790
    val ks = 1 upto n;
blanchet@48975
   791
  in
blanchet@49305
   792
    EVERY' [rtac (Drule.instantiate' [] cts nat_induct),
blanchet@48975
   793
      REPEAT_DETERM o rtac allI,
blanchet@48975
   794
      CONJ_WRAP' (fn ((i, (Lev_0, Lev_Suc)), rv_Nil) =>
blanchet@49305
   795
        EVERY' [rtac impI, dtac (Lev_0 RS equalityD1 RS set_mp),
blanchet@48975
   796
          etac @{thm singletonE}, hyp_subst_tac,
blanchet@48975
   797
          CONJ_WRAP' (fn i' => rtac impI THEN'
blanchet@48975
   798
            CONJ_WRAP' (fn i'' => rtac impI  THEN' dtac (sym RS trans) THEN' rtac rv_Nil THEN'
blanchet@48975
   799
              (if i = i''
blanchet@48975
   800
              then EVERY' [dtac @{thm ssubst_mem[OF sym[OF append_Nil]]},
blanchet@49305
   801
                dtac (Lev_Suc RS equalityD1 RS set_mp), dtac (mk_InN_inject n i),
blanchet@48975
   802
                hyp_subst_tac,
blanchet@48975
   803
                CONJ_WRAP_GEN' (etac (Thm.permute_prems 1 1 UnE))
blanchet@48975
   804
                  (fn k => REPEAT_DETERM o eresolve_tac [CollectE, exE, conjE] THEN'
blanchet@49305
   805
                    dtac list_inject_subst_id THEN' etac conjE THEN'
blanchet@48975
   806
                    (if k = i'
blanchet@48975
   807
                    then EVERY' [dtac (mk_InN_inject n k), hyp_subst_tac, etac imageI]
blanchet@48975
   808
                    else etac (mk_InN_not_InM i' k RS notE)))
blanchet@48975
   809
                (rev ks)]
blanchet@48975
   810
              else etac (mk_InN_not_InM i'' i RS notE)))
blanchet@48975
   811
            ks)
blanchet@48975
   812
          ks])
blanchet@48975
   813
      ((ks ~~ (Lev_0s ~~ Lev_Sucs)) ~~ rv_Nils),
blanchet@48975
   814
      REPEAT_DETERM o rtac allI,
blanchet@48975
   815
      CONJ_WRAP' (fn ((Lev_Suc, rv_Cons), (from_to_sbds, to_sbd_injs)) =>
blanchet@49305
   816
        EVERY' [rtac impI, dtac (Lev_Suc RS equalityD1 RS set_mp),
blanchet@48975
   817
          CONJ_WRAP_GEN' (etac (Thm.permute_prems 1 1 UnE))
blanchet@48975
   818
            (fn (i, (from_to_sbd, to_sbd_inj)) =>
blanchet@48975
   819
              REPEAT_DETERM o eresolve_tac [CollectE, exE, conjE] THEN' hyp_subst_tac THEN'
blanchet@48975
   820
              CONJ_WRAP' (fn i' => rtac impI THEN'
blanchet@48975
   821
                dtac @{thm ssubst_mem[OF sym[OF append_Cons]]} THEN'
blanchet@49305
   822
                dtac (Lev_Suc RS equalityD1 RS set_mp) THEN'
blanchet@48975
   823
                CONJ_WRAP_GEN' (etac (Thm.permute_prems 1 1 UnE)) (fn k =>
blanchet@48975
   824
                  REPEAT_DETERM o eresolve_tac [CollectE, exE, conjE] THEN'
blanchet@49305
   825
                  dtac list_inject_subst_id THEN' etac conjE THEN'
blanchet@48975
   826
                  (if k = i
blanchet@48975
   827
                  then EVERY' [dtac (mk_InN_inject n i),
blanchet@49305
   828
                    dtac (Thm.permute_prems 0 2 (to_sbd_inj RS subst_id)),
blanchet@48975
   829
                    atac, atac, hyp_subst_tac] THEN'
blanchet@48975
   830
                    CONJ_WRAP' (fn i'' =>
blanchet@48975
   831
                      EVERY' [rtac impI, dtac (sym RS trans),
blanchet@48975
   832
                        rtac (rv_Cons RS trans), rtac (mk_sum_casesN n i RS arg_cong RS trans),
blanchet@48975
   833
                        etac (from_to_sbd RS arg_cong),
blanchet@48975
   834
                        REPEAT_DETERM o etac allE,
blanchet@48975
   835
                        dtac (mk_conjunctN n i), dtac mp, atac,
blanchet@48975
   836
                        dtac (mk_conjunctN n i'), dtac mp, atac,
blanchet@48975
   837
                        dtac (mk_conjunctN n i''), etac mp, etac sym])
blanchet@48975
   838
                    ks
blanchet@48975
   839
                  else etac (mk_InN_not_InM i k RS notE)))
blanchet@48975
   840
                (rev ks))
blanchet@48975
   841
              ks)
blanchet@48975
   842
          (rev (ks ~~ (from_to_sbds ~~ to_sbd_injs)))])
blanchet@48975
   843
      ((Lev_Sucs ~~ rv_Conss) ~~ (from_to_sbdss ~~ to_sbd_injss))] 1
blanchet@48975
   844
  end;
blanchet@48975
   845
blanchet@48975
   846
fun mk_mor_beh_tac m mor_def mor_cong beh_defs carT_defs strT_defs isNode_defs
blanchet@48975
   847
  to_sbd_injss from_to_sbdss Lev_0s Lev_Sucs rv_Nils rv_Conss Lev_sbds length_Levs length_Lev's
blanchet@48975
   848
  prefCl_Levs rv_lastss set_rv_Levsss set_Levsss set_image_Levsss set_naturalss coalg_setss
blanchet@48975
   849
  map_comp_ids map_congs map_arg_congs {context = ctxt, prems = _} =
blanchet@48975
   850
  let
blanchet@48975
   851
    val n = length beh_defs;
blanchet@48975
   852
    val ks = 1 upto n;
blanchet@48975
   853
blanchet@48975
   854
    fun fbetw_tac (i, (carT_def, (isNode_def, (Lev_0, (rv_Nil, (Lev_sbd,
blanchet@48975
   855
      ((length_Lev, length_Lev'), (prefCl_Lev, (rv_lasts, (set_naturals,
blanchet@48975
   856
        (coalg_sets, (set_rv_Levss, (set_Levss, set_image_Levss))))))))))))) =
blanchet@49305
   857
      EVERY' [rtac ballI, rtac (carT_def RS equalityD2 RS set_mp),
blanchet@48975
   858
        rtac CollectI, REPEAT_DETERM o rtac exI, rtac conjI, rtac refl, rtac conjI,
blanchet@48975
   859
        rtac conjI,
blanchet@49305
   860
          rtac @{thm UN_I}, rtac UNIV_I, rtac (Lev_0 RS equalityD2 RS set_mp),
blanchet@48975
   861
          rtac @{thm singletonI},
blanchet@48975
   862
        rtac conjI,
blanchet@48975
   863
          rtac @{thm UN_least}, rtac Lev_sbd,
blanchet@48975
   864
        rtac conjI,
blanchet@48975
   865
          rtac @{thm prefCl_UN}, rtac ssubst, rtac @{thm PrefCl_def}, REPEAT_DETERM o rtac allI,
blanchet@48975
   866
          rtac impI, etac conjE, rtac exI, rtac conjI, rtac @{thm ord_le_eq_trans},
Christian@49091
   867
          etac @{thm prefix_length_le}, etac length_Lev, rtac prefCl_Lev, etac conjI, atac,
blanchet@48975
   868
        rtac conjI,
blanchet@48975
   869
          rtac ballI, etac @{thm UN_E}, rtac conjI,
blanchet@48975
   870
          if n = 1 then K all_tac else rtac (mk_sumEN n),
blanchet@48975
   871
          EVERY' (map6 (fn i => fn isNode_def => fn set_naturals =>
blanchet@48975
   872
            fn set_rv_Levs => fn set_Levs => fn set_image_Levs =>
blanchet@48975
   873
            EVERY' [rtac (mk_disjIN n i), rtac (isNode_def RS ssubst),
blanchet@48975
   874
              rtac exI, rtac conjI,
blanchet@48975
   875
              (if n = 1 then rtac @{thm if_P} THEN' etac length_Lev'
blanchet@48975
   876
              else rtac (@{thm if_P} RS arg_cong RS trans) THEN' etac length_Lev' THEN'
blanchet@49328
   877
                etac (sum_case_weak_cong RS trans) THEN' rtac (mk_sum_casesN n i)),
blanchet@48975
   878
              EVERY' (map2 (fn set_natural => fn set_rv_Lev =>
blanchet@49305
   879
                EVERY' [rtac conjI, rtac ord_eq_le_trans, rtac (set_natural RS trans),
blanchet@49305
   880
                  rtac trans_fun_cong_image_id_id_apply,
blanchet@48975
   881
                  etac set_rv_Lev, TRY o atac, etac conjI, atac])
blanchet@48975
   882
              (take m set_naturals) set_rv_Levs),
blanchet@48975
   883
              CONJ_WRAP' (fn (set_natural, (set_Lev, set_image_Lev)) =>
blanchet@48975
   884
                EVERY' [rtac (set_natural RS trans), rtac equalityI, rtac @{thm image_subsetI},
blanchet@48975
   885
                  rtac CollectI, rtac @{thm SuccI}, rtac @{thm UN_I}, rtac UNIV_I, etac set_Lev,
blanchet@48975
   886
                  if n = 1 then rtac refl else atac, atac, rtac subsetI,
blanchet@48975
   887
                  REPEAT_DETERM o eresolve_tac [CollectE, @{thm SuccE}, @{thm UN_E}],
blanchet@48975
   888
                  rtac set_image_Lev, atac, dtac length_Lev, hyp_subst_tac, dtac length_Lev',
blanchet@48975
   889
                  etac @{thm set_mp[OF equalityD1[OF arg_cong[OF length_append_singleton]]]},
blanchet@48975
   890
                  if n = 1 then rtac refl else atac])
blanchet@48975
   891
              (drop m set_naturals ~~ (set_Levs ~~ set_image_Levs))])
blanchet@48975
   892
          ks isNode_defs set_naturalss set_rv_Levss set_Levss set_image_Levss),
blanchet@48975
   893
          CONJ_WRAP' (fn (i, (rv_last, (isNode_def, (set_naturals,
blanchet@48975
   894
            (set_rv_Levs, (set_Levs, set_image_Levs)))))) =>
blanchet@48975
   895
            EVERY' [rtac ballI,
blanchet@48975
   896
              REPEAT_DETERM o eresolve_tac [CollectE, @{thm SuccE}, @{thm UN_E}],
blanchet@48975
   897
              rtac (rev_mp OF [rv_last, impI]), etac exE, rtac (isNode_def RS ssubst),
blanchet@48975
   898
              rtac exI, rtac conjI,
blanchet@48975
   899
              (if n = 1 then rtac @{thm if_P} THEN' etac length_Lev'
blanchet@48975
   900
              else rtac (@{thm if_P} RS trans) THEN' etac length_Lev' THEN'
blanchet@49328
   901
                etac (sum_case_weak_cong RS trans) THEN' rtac (mk_sum_casesN n i)),
blanchet@48975
   902
              EVERY' (map2 (fn set_natural => fn set_rv_Lev =>
blanchet@49305
   903
                EVERY' [rtac conjI, rtac ord_eq_le_trans, rtac (set_natural RS trans),
blanchet@49305
   904
                  rtac trans_fun_cong_image_id_id_apply,
blanchet@48975
   905
                  etac set_rv_Lev, TRY o atac, etac conjI, atac])
blanchet@48975
   906
              (take m set_naturals) set_rv_Levs),
blanchet@48975
   907
              CONJ_WRAP' (fn (set_natural, (set_Lev, set_image_Lev)) =>
blanchet@48975
   908
                EVERY' [rtac (set_natural RS trans), rtac equalityI, rtac @{thm image_subsetI},
blanchet@48975
   909
                  rtac CollectI, rtac @{thm SuccI}, rtac @{thm UN_I}, rtac UNIV_I, etac set_Lev,
blanchet@48975
   910
                  if n = 1 then rtac refl else atac, atac, rtac subsetI,
blanchet@48975
   911
                  REPEAT_DETERM o eresolve_tac [CollectE, @{thm SuccE}, @{thm UN_E}],
blanchet@48975
   912
                  REPEAT_DETERM_N 4 o etac thin_rl,
blanchet@48975
   913
                  rtac set_image_Lev,
blanchet@48975
   914
                  atac, dtac length_Lev, hyp_subst_tac, dtac length_Lev',
blanchet@48975
   915
                  etac @{thm set_mp[OF equalityD1[OF arg_cong[OF length_append_singleton]]]},
blanchet@48975
   916
                  if n = 1 then rtac refl else atac])
blanchet@48975
   917
              (drop m set_naturals ~~ (set_Levs ~~ set_image_Levs))])
blanchet@48975
   918
          (ks ~~ (rv_lasts ~~ (isNode_defs ~~ (set_naturalss ~~
blanchet@48975
   919
            (set_rv_Levss ~~ (set_Levss ~~ set_image_Levss)))))),
blanchet@48975
   920
        (**)
blanchet@48975
   921
          rtac allI, rtac impI, rtac @{thm if_not_P}, rtac notI,
blanchet@48975
   922
          etac notE, etac @{thm UN_I[OF UNIV_I]},
blanchet@48975
   923
        (*root isNode*)
blanchet@48975
   924
          rtac (isNode_def RS ssubst), rtac exI, rtac conjI, rtac (@{thm if_P} RS trans),
blanchet@49305
   925
          rtac length_Lev', rtac (Lev_0 RS equalityD2 RS set_mp), rtac @{thm singletonI},
blanchet@48975
   926
          CONVERSION (Conv.top_conv
blanchet@48975
   927
            (K (Conv.try_conv (Conv.rewr_conv (rv_Nil RS eq_reflection)))) ctxt),
blanchet@48975
   928
          if n = 1 then rtac refl else rtac (mk_sum_casesN n i),
blanchet@48975
   929
          EVERY' (map2 (fn set_natural => fn coalg_set =>
blanchet@49305
   930
            EVERY' [rtac conjI, rtac ord_eq_le_trans, rtac (set_natural RS trans),
blanchet@49305
   931
              rtac trans_fun_cong_image_id_id_apply, etac coalg_set, atac])
blanchet@48975
   932
            (take m set_naturals) (take m coalg_sets)),
blanchet@48975
   933
          CONJ_WRAP' (fn (set_natural, (set_Lev, set_image_Lev)) =>
blanchet@48975
   934
            EVERY' [rtac (set_natural RS trans), rtac equalityI, rtac @{thm image_subsetI},
blanchet@48975
   935
              rtac CollectI, rtac @{thm SuccI}, rtac @{thm UN_I}, rtac UNIV_I, rtac set_Lev,
blanchet@49305
   936
              rtac (Lev_0 RS equalityD2 RS set_mp), rtac @{thm singletonI}, rtac rv_Nil,
blanchet@48975
   937
              atac, rtac subsetI,
blanchet@48975
   938
              REPEAT_DETERM o eresolve_tac [CollectE, @{thm SuccE}, @{thm UN_E}],
blanchet@49305
   939
              rtac set_image_Lev, rtac (Lev_0 RS equalityD2 RS set_mp),
blanchet@48975
   940
              rtac @{thm singletonI}, dtac length_Lev',
blanchet@48975
   941
              etac @{thm set_mp[OF equalityD1[OF arg_cong[OF
blanchet@48975
   942
                trans[OF length_append_singleton arg_cong[of _ _ Suc, OF list.size(3)]]]]]},
blanchet@48975
   943
              rtac rv_Nil])
blanchet@48975
   944
          (drop m set_naturals ~~ (nth set_Levss (i - 1) ~~ nth set_image_Levss (i - 1)))];
blanchet@48975
   945
blanchet@48975
   946
    fun mor_tac (i, (strT_def, (((Lev_0, Lev_Suc), (rv_Nil, rv_Cons)),
blanchet@48975
   947
      ((map_comp_id, (map_cong, map_arg_cong)), (length_Lev', (from_to_sbds, to_sbd_injs)))))) =
blanchet@48975
   948
      EVERY' [rtac ballI, rtac sym, rtac trans, rtac strT_def,
blanchet@48975
   949
        rtac (@{thm if_P} RS
blanchet@49328
   950
          (if n = 1 then map_arg_cong else sum_case_weak_cong) RS trans),
blanchet@49305
   951
        rtac (@{thm list.size(3)} RS arg_cong RS trans RS equalityD2 RS set_mp),
blanchet@48975
   952
        rtac Lev_0, rtac @{thm singletonI},
blanchet@48975
   953
        CONVERSION (Conv.top_conv
blanchet@48975
   954
          (K (Conv.try_conv (Conv.rewr_conv (rv_Nil RS eq_reflection)))) ctxt),
blanchet@48975
   955
        if n = 1 then K all_tac
blanchet@49328
   956
        else (rtac (sum_case_weak_cong RS trans) THEN'
blanchet@48975
   957
          rtac (mk_sum_casesN n i) THEN' rtac (mk_sum_casesN n i RS trans)),
blanchet@48975
   958
        rtac (map_comp_id RS trans), rtac (map_cong OF replicate m refl),
blanchet@48975
   959
        EVERY' (map3 (fn i' => fn to_sbd_inj => fn from_to_sbd =>
blanchet@48975
   960
          DETERM o EVERY' [rtac trans, rtac o_apply, rtac ssubst, rtac @{thm Pair_eq}, rtac conjI,
blanchet@48975
   961
            rtac trans, rtac @{thm Shift_def},
blanchet@48975
   962
            rtac equalityI, rtac subsetI, etac thin_rl, etac thin_rl,
blanchet@48975
   963
            REPEAT_DETERM o eresolve_tac [CollectE, @{thm UN_E}], dtac length_Lev', dtac asm_rl,
blanchet@48975
   964
            etac thin_rl, dtac @{thm set_rev_mp[OF _ equalityD1]},
blanchet@48975
   965
            rtac (@{thm length_Cons} RS arg_cong RS trans), rtac Lev_Suc,
blanchet@48975
   966
            CONJ_WRAP_GEN' (etac (Thm.permute_prems 1 1 UnE)) (fn i'' =>
blanchet@48975
   967
              EVERY' [REPEAT_DETERM o eresolve_tac [CollectE, exE, conjE],
blanchet@49305
   968
                dtac list_inject_subst_id, etac conjE,
blanchet@48975
   969
                if i' = i'' then EVERY' [dtac (mk_InN_inject n i'),
blanchet@49305
   970
                  dtac (Thm.permute_prems 0 2 (to_sbd_inj RS subst_id)),
blanchet@48975
   971
                  atac, atac, hyp_subst_tac, etac @{thm UN_I[OF UNIV_I]}]
blanchet@48975
   972
                else etac (mk_InN_not_InM i' i'' RS notE)])
blanchet@48975
   973
            (rev ks),
blanchet@48975
   974
            rtac @{thm UN_least}, rtac subsetI, rtac CollectI, rtac @{thm UN_I[OF UNIV_I]},
blanchet@49305
   975
            rtac (Lev_Suc RS equalityD2 RS set_mp), rtac (mk_UnN n i'), rtac CollectI,
blanchet@48975
   976
            REPEAT_DETERM o rtac exI, rtac conjI, rtac refl, etac conjI, atac,
blanchet@48975
   977
            rtac trans, rtac @{thm shift_def}, rtac ssubst, rtac @{thm fun_eq_iff}, rtac allI,
blanchet@48975
   978
            rtac @{thm if_cong}, rtac (@{thm length_Cons} RS arg_cong RS trans), rtac iffI,
blanchet@48975
   979
            dtac asm_rl, dtac asm_rl, dtac asm_rl,
blanchet@49305
   980
            dtac (Lev_Suc RS equalityD1 RS set_mp),
blanchet@48975
   981
            CONJ_WRAP_GEN' (etac (Thm.permute_prems 1 1 UnE)) (fn i'' =>
blanchet@48975
   982
              EVERY' [REPEAT_DETERM o eresolve_tac [CollectE, exE, conjE],
blanchet@49305
   983
                dtac list_inject_subst_id, etac conjE,
blanchet@48975
   984
                if i' = i'' then EVERY' [dtac (mk_InN_inject n i'),
blanchet@49305
   985
                  dtac (Thm.permute_prems 0 2 (to_sbd_inj RS subst_id)),
blanchet@48975
   986
                  atac, atac, hyp_subst_tac, atac]
blanchet@48975
   987
                else etac (mk_InN_not_InM i' i'' RS notE)])
blanchet@48975
   988
            (rev ks),
blanchet@49305
   989
            rtac (Lev_Suc RS equalityD2 RS set_mp), rtac (mk_UnN n i'), rtac CollectI,
blanchet@48975
   990
            REPEAT_DETERM o rtac exI, rtac conjI, rtac refl, etac conjI, atac,
blanchet@48975
   991
            CONVERSION (Conv.top_conv
blanchet@48975
   992
              (K (Conv.try_conv (Conv.rewr_conv (rv_Cons RS eq_reflection)))) ctxt),
blanchet@48975
   993
            if n = 1 then K all_tac
blanchet@49328
   994
            else rtac sum_case_weak_cong THEN' rtac (mk_sum_casesN n i' RS trans),
blanchet@48975
   995
            SELECT_GOAL (Local_Defs.unfold_tac ctxt [from_to_sbd]), rtac refl,
blanchet@48975
   996
            rtac refl])
blanchet@48975
   997
        ks to_sbd_injs from_to_sbds)];
blanchet@48975
   998
  in
blanchet@48975
   999
    (rtac mor_cong THEN'
blanchet@48975
  1000
    EVERY' (map (fn thm => rtac (thm RS ext)) beh_defs) THEN'
blanchet@48975
  1001
    stac mor_def THEN' rtac conjI THEN'
blanchet@48975
  1002
    CONJ_WRAP' fbetw_tac
blanchet@48975
  1003
      (ks ~~ (carT_defs ~~ (isNode_defs ~~ (Lev_0s ~~ (rv_Nils ~~ (Lev_sbds ~~
blanchet@48975
  1004
        ((length_Levs ~~ length_Lev's) ~~ (prefCl_Levs ~~ (rv_lastss ~~
blanchet@48975
  1005
          (set_naturalss ~~ (coalg_setss ~~
blanchet@48975
  1006
            (set_rv_Levsss ~~ (set_Levsss ~~ set_image_Levsss))))))))))))) THEN'
blanchet@48975
  1007
    CONJ_WRAP' mor_tac
blanchet@48975
  1008
      (ks ~~ (strT_defs ~~ (((Lev_0s ~~ Lev_Sucs) ~~ (rv_Nils ~~ rv_Conss)) ~~
blanchet@48975
  1009
        ((map_comp_ids ~~ (map_congs ~~ map_arg_congs)) ~~
blanchet@48975
  1010
          (length_Lev's ~~ (from_to_sbdss ~~ to_sbd_injss))))))) 1
blanchet@48975
  1011
  end;
blanchet@48975
  1012
blanchet@48975
  1013
fun mk_congruent_str_final_tac m lsbisE map_comp_id map_cong equiv_LSBISs =
blanchet@48975
  1014
  EVERY' [rtac @{thm congruentI}, dtac lsbisE,
blanchet@48975
  1015
    REPEAT_DETERM o eresolve_tac [CollectE, conjE, bexE], rtac (o_apply RS trans),
blanchet@48975
  1016
    etac (sym RS arg_cong RS trans), rtac (map_comp_id RS trans),
blanchet@48975
  1017
    rtac (map_cong RS trans), REPEAT_DETERM_N m o rtac refl,
blanchet@48975
  1018
    EVERY' (map (fn equiv_LSBIS =>
blanchet@49305
  1019
      EVERY' [rtac @{thm equiv_proj}, rtac equiv_LSBIS, etac set_mp, atac])
blanchet@48975
  1020
    equiv_LSBISs), rtac sym, rtac (o_apply RS trans),
blanchet@48975
  1021
    etac (sym RS arg_cong RS trans), rtac map_comp_id] 1;
blanchet@48975
  1022
blanchet@48975
  1023
fun mk_coalg_final_tac m coalg_def congruent_str_finals equiv_LSBISs set_naturalss coalgT_setss =
blanchet@48975
  1024
  EVERY' [stac coalg_def,
blanchet@48975
  1025
    CONJ_WRAP' (fn ((set_naturals, coalgT_sets), (equiv_LSBIS, congruent_str_final)) =>
blanchet@48975
  1026
      EVERY' [rtac @{thm univ_preserves}, rtac equiv_LSBIS, rtac congruent_str_final,
blanchet@48975
  1027
        rtac ballI, rtac @{thm ssubst_mem}, rtac o_apply, rtac CollectI,
blanchet@48975
  1028
        EVERY' (map2 (fn set_natural => fn coalgT_set =>
blanchet@49305
  1029
          EVERY' [rtac conjI, rtac (set_natural RS ord_eq_le_trans),
blanchet@49305
  1030
            rtac ord_eq_le_trans_trans_fun_cong_image_id_id_apply,
blanchet@48975
  1031
            etac coalgT_set])
blanchet@48975
  1032
        (take m set_naturals) (take m coalgT_sets)),
blanchet@48975
  1033
        CONJ_WRAP' (fn (equiv_LSBIS, (set_natural, coalgT_set)) =>
blanchet@49305
  1034
          EVERY' [rtac (set_natural RS ord_eq_le_trans),
blanchet@48975
  1035
            rtac @{thm image_subsetI}, rtac ssubst, rtac @{thm proj_in_iff},
blanchet@49305
  1036
            rtac equiv_LSBIS, etac set_rev_mp, etac coalgT_set])
blanchet@48975
  1037
        (equiv_LSBISs ~~ drop m (set_naturals ~~ coalgT_sets))])
blanchet@48975
  1038
    ((set_naturalss ~~ coalgT_setss) ~~ (equiv_LSBISs ~~ congruent_str_finals))] 1;
blanchet@48975
  1039
blanchet@48975
  1040
fun mk_mor_T_final_tac mor_def congruent_str_finals equiv_LSBISs =
blanchet@48975
  1041
  EVERY' [stac mor_def, rtac conjI,
blanchet@48975
  1042
    CONJ_WRAP' (fn equiv_LSBIS =>
blanchet@48975
  1043
      EVERY' [rtac ballI, rtac ssubst, rtac @{thm proj_in_iff}, rtac equiv_LSBIS, atac])
blanchet@48975
  1044
    equiv_LSBISs,
blanchet@48975
  1045
    CONJ_WRAP' (fn (equiv_LSBIS, congruent_str_final) =>
blanchet@48975
  1046
      EVERY' [rtac ballI, rtac sym, rtac trans, rtac @{thm univ_commute}, rtac equiv_LSBIS,
blanchet@48975
  1047
        rtac congruent_str_final, atac, rtac o_apply])
blanchet@48975
  1048
    (equiv_LSBISs ~~ congruent_str_finals)] 1;
blanchet@48975
  1049
blanchet@48975
  1050
fun mk_mor_Rep_tac m defs Reps Abs_inverses coalg_final_setss map_comp_ids map_congLs
blanchet@48975
  1051
  {context = ctxt, prems = _} =
blanchet@48975
  1052
  Local_Defs.unfold_tac ctxt defs THEN
blanchet@48975
  1053
  EVERY' [rtac conjI,
blanchet@48975
  1054
    CONJ_WRAP' (fn thm => rtac ballI THEN' rtac thm) Reps,
blanchet@48975
  1055
    CONJ_WRAP' (fn (Rep, ((map_comp_id, map_congL), coalg_final_sets)) =>
blanchet@48975
  1056
      EVERY' [rtac ballI, rtac (map_comp_id RS trans), rtac map_congL,
blanchet@48975
  1057
        EVERY' (map2 (fn Abs_inverse => fn coalg_final_set =>
blanchet@48975
  1058
          EVERY' [rtac ballI, rtac (o_apply RS trans), rtac Abs_inverse,
blanchet@49305
  1059
            etac set_rev_mp, rtac coalg_final_set, rtac Rep])
blanchet@48975
  1060
        Abs_inverses (drop m coalg_final_sets))])
blanchet@48975
  1061
    (Reps ~~ ((map_comp_ids ~~ map_congLs) ~~ coalg_final_setss))] 1;
blanchet@48975
  1062
blanchet@48975
  1063
fun mk_mor_Abs_tac defs Abs_inverses {context = ctxt, prems = _} =
blanchet@48975
  1064
  Local_Defs.unfold_tac ctxt defs THEN
blanchet@48975
  1065
  EVERY' [rtac conjI,
blanchet@48975
  1066
    CONJ_WRAP' (K (rtac ballI THEN' rtac UNIV_I)) Abs_inverses,
blanchet@48975
  1067
    CONJ_WRAP' (fn thm => rtac ballI THEN' etac (thm RS arg_cong RS sym)) Abs_inverses] 1;
blanchet@48975
  1068
blanchet@48975
  1069
fun mk_mor_coiter_tac m mor_UNIV unf_defs coiter_defs Abs_inverses morEs map_comp_ids map_congs =
blanchet@49305
  1070
  EVERY' [rtac ssubst_id, rtac mor_UNIV,
blanchet@48975
  1071
    CONJ_WRAP' (fn ((Abs_inverse, morE), ((unf_def, coiter_def), (map_comp_id, map_cong))) =>
blanchet@48975
  1072
      EVERY' [rtac ext, rtac (o_apply RS trans RS sym), rtac (unf_def RS trans),
blanchet@48975
  1073
        rtac (coiter_def RS arg_cong RS trans), rtac (Abs_inverse RS arg_cong RS trans),
blanchet@48975
  1074
        rtac (morE RS arg_cong RS trans), rtac (map_comp_id RS trans),
blanchet@48975
  1075
        rtac (o_apply RS trans RS sym), rtac map_cong,
blanchet@48975
  1076
        REPEAT_DETERM_N m o rtac refl,
blanchet@48975
  1077
        EVERY' (map (fn thm => rtac (thm RS trans) THEN' rtac (o_apply RS sym)) coiter_defs)])
blanchet@48975
  1078
    ((Abs_inverses ~~ morEs) ~~ ((unf_defs ~~ coiter_defs) ~~ (map_comp_ids ~~ map_congs)))] 1;
blanchet@48975
  1079
blanchet@48975
  1080
fun mk_raw_coind_tac bis_def bis_cong bis_O bis_converse bis_Gr tcoalg coalgT mor_T_final
blanchet@48975
  1081
  sbis_lsbis lsbis_incls incl_lsbiss equiv_LSBISs mor_Rep Rep_injects =
blanchet@48975
  1082
  let
blanchet@48975
  1083
    val n = length Rep_injects;
blanchet@48975
  1084
  in
blanchet@49305
  1085
    EVERY' [rtac rev_mp, ftac (bis_def RS subst_id),
blanchet@48975
  1086
      REPEAT_DETERM o etac conjE, rtac bis_cong, rtac bis_O, rtac bis_converse,
blanchet@48975
  1087
      rtac bis_Gr, rtac tcoalg, rtac mor_Rep, rtac bis_O, atac, rtac bis_Gr, rtac tcoalg,
blanchet@48975
  1088
      rtac mor_Rep, REPEAT_DETERM_N n o etac @{thm relImage_Gr},
blanchet@48975
  1089
      rtac impI, rtac rev_mp, rtac bis_cong, rtac bis_O, rtac bis_Gr, rtac coalgT,
blanchet@48975
  1090
      rtac mor_T_final, rtac bis_O, rtac sbis_lsbis, rtac bis_converse, rtac bis_Gr, rtac coalgT,
blanchet@48975
  1091
      rtac mor_T_final, EVERY' (map (fn thm => rtac (thm RS @{thm relInvImage_Gr})) lsbis_incls),
blanchet@48975
  1092
      rtac impI,
blanchet@48975
  1093
      CONJ_WRAP' (fn (Rep_inject, (equiv_LSBIS , (incl_lsbis, lsbis_incl))) =>
blanchet@48975
  1094
        EVERY' [rtac subset_trans, rtac @{thm relInvImage_UNIV_relImage}, rtac subset_trans,
blanchet@48975
  1095
          rtac @{thm relInvImage_mono}, rtac subset_trans, etac incl_lsbis,
blanchet@49305
  1096
          rtac ord_eq_le_trans, rtac @{thm sym[OF relImage_relInvImage]},
blanchet@48975
  1097
          rtac @{thm xt1(3)}, rtac @{thm Sigma_cong},
blanchet@48975
  1098
          rtac @{thm proj_image}, rtac @{thm proj_image}, rtac lsbis_incl,
blanchet@48975
  1099
          rtac subset_trans, rtac @{thm relImage_mono}, rtac incl_lsbis, atac,
blanchet@48975
  1100
          rtac @{thm relImage_proj}, rtac equiv_LSBIS, rtac @{thm relInvImage_diag},
blanchet@48975
  1101
          rtac Rep_inject])
blanchet@48975
  1102
      (Rep_injects ~~ (equiv_LSBISs ~~ (incl_lsbiss ~~ lsbis_incls)))] 1
blanchet@48975
  1103
  end;
blanchet@48975
  1104
blanchet@48975
  1105
fun mk_unique_mor_tac raw_coinds bis =
blanchet@48975
  1106
  CONJ_WRAP' (fn raw_coind =>
blanchet@49305
  1107
    EVERY' [rtac impI, rtac (bis RS raw_coind RS set_mp RS @{thm IdD}), atac,
blanchet@48975
  1108
      etac conjunct1, atac, etac conjunct2, rtac @{thm image2_eqI}, rtac refl, rtac refl, atac])
blanchet@48975
  1109
  raw_coinds 1;
blanchet@48975
  1110
blanchet@48975
  1111
fun mk_coiter_unique_mor_tac raw_coinds bis mor coiter_defs =
blanchet@48975
  1112
  CONJ_WRAP' (fn (raw_coind, coiter_def) =>
blanchet@49305
  1113
    EVERY' [rtac ext, etac (bis RS raw_coind RS set_mp RS @{thm IdD}), rtac mor,
blanchet@48975
  1114
      rtac @{thm image2_eqI}, rtac refl, rtac (coiter_def RS arg_cong RS trans),
blanchet@48975
  1115
      rtac (o_apply RS sym), rtac UNIV_I]) (raw_coinds ~~ coiter_defs) 1;
blanchet@48975
  1116
blanchet@48975
  1117
fun mk_unf_o_fld_tac fld_def coiter map_comp_id map_congL coiter_o_unfs
blanchet@48975
  1118
  {context = ctxt, prems = _} =
blanchet@48975
  1119
  Local_Defs.unfold_tac ctxt [fld_def] THEN EVERY' [rtac ext, rtac trans, rtac o_apply,
blanchet@48975
  1120
    rtac trans, rtac coiter, rtac trans, rtac map_comp_id, rtac trans, rtac map_congL,
blanchet@48975
  1121
    EVERY' (map (fn thm =>
blanchet@48975
  1122
      rtac ballI THEN' rtac (trans OF [thm RS fun_cong, @{thm id_apply}])) coiter_o_unfs),
blanchet@48975
  1123
    rtac sym, rtac @{thm id_apply}] 1;
blanchet@48975
  1124
blanchet@48975
  1125
fun mk_corec_tac m corec_defs coiter map_cong corec_Inls {context = ctxt, prems = _} =
blanchet@48975
  1126
  Local_Defs.unfold_tac ctxt corec_defs THEN EVERY' [rtac trans, rtac (o_apply RS arg_cong),
blanchet@48975
  1127
    rtac trans, rtac coiter, fo_rtac (@{thm sum.cases(2)} RS arg_cong RS trans) ctxt, rtac map_cong,
blanchet@48975
  1128
    REPEAT_DETERM_N m o rtac refl,
blanchet@48975
  1129
    EVERY' (map (fn thm => rtac @{thm sum_case_expand_Inr} THEN' rtac thm) corec_Inls)] 1;
blanchet@48975
  1130
blanchet@48975
  1131
fun mk_rel_coinduct_tac ks raw_coind bis_rel =
blanchet@48975
  1132
  EVERY' [rtac rev_mp, rtac raw_coind, rtac ssubst, rtac bis_rel, rtac conjI,
blanchet@48975
  1133
    CONJ_WRAP' (K (rtac @{thm ord_le_eq_trans[OF subset_UNIV UNIV_Times_UNIV[THEN sym]]})) ks,
blanchet@48975
  1134
    CONJ_WRAP' (K (EVERY' [rtac allI, rtac allI, rtac impI,
blanchet@48975
  1135
      REPEAT_DETERM o etac allE, etac mp, etac CollectE, etac @{thm splitD}])) ks,
blanchet@48975
  1136
    rtac impI, REPEAT_DETERM o etac conjE,
blanchet@49305
  1137
    CONJ_WRAP' (K (EVERY' [rtac impI, rtac @{thm IdD}, etac set_mp,
blanchet@48975
  1138
      rtac CollectI, etac @{thm prod_caseI}])) ks] 1;
blanchet@48975
  1139
blanchet@48975
  1140
fun mk_rel_coinduct_upto_tac m cTs cts rel_coinduct rel_monos rel_Ids =
blanchet@48975
  1141
  EVERY' [rtac rev_mp, rtac (Drule.instantiate' cTs cts rel_coinduct),
blanchet@48975
  1142
    EVERY' (map2 (fn rel_mono => fn rel_Id =>
blanchet@48975
  1143
      EVERY' [REPEAT_DETERM o resolve_tac [allI, impI], REPEAT_DETERM o etac allE,
blanchet@49305
  1144
        etac disjE, etac mp, atac, hyp_subst_tac, rtac (rel_mono RS set_mp),
blanchet@48975
  1145
        REPEAT_DETERM_N m o rtac @{thm subset_refl},
blanchet@48975
  1146
        REPEAT_DETERM_N (length rel_monos) o rtac @{thm Id_subset},
blanchet@49305
  1147
        rtac (rel_Id RS equalityD2 RS set_mp), rtac @{thm IdI}])
blanchet@48975
  1148
    rel_monos rel_Ids),
blanchet@48975
  1149
    rtac impI, REPEAT_DETERM o etac conjE,
blanchet@48975
  1150
    CONJ_WRAP' (K (rtac impI THEN' etac mp THEN' etac disjI1)) rel_Ids] 1;
blanchet@48975
  1151
blanchet@48975
  1152
fun mk_unf_coinduct_tac m ks raw_coind bis_def =
blanchet@48975
  1153
  let
blanchet@48975
  1154
    val n = length ks;
blanchet@48975
  1155
  in
blanchet@48975
  1156
    EVERY' [rtac rev_mp, rtac raw_coind, rtac ssubst, rtac bis_def, rtac conjI,
blanchet@48975
  1157
      CONJ_WRAP' (K (rtac @{thm ord_le_eq_trans[OF subset_UNIV UNIV_Times_UNIV[THEN sym]]})) ks,
blanchet@48975
  1158
      CONJ_WRAP' (fn i => EVERY' [select_prem_tac n (dtac asm_rl) i, REPEAT_DETERM o rtac allI,
blanchet@48975
  1159
        rtac impI, REPEAT_DETERM o dtac @{thm meta_spec}, etac CollectE, etac @{thm meta_impE},
blanchet@48975
  1160
        atac, etac exE, etac conjE, etac conjE, rtac bexI, rtac conjI,
blanchet@48975
  1161
        etac @{thm fst_conv[THEN subst]}, etac @{thm snd_conv[THEN subst]},
blanchet@48975
  1162
        rtac CollectI, REPEAT_DETERM_N m o (rtac conjI THEN' rtac @{thm subset_UNIV}),
blanchet@48975
  1163
        CONJ_WRAP' (fn i' => EVERY' [rtac subsetI, rtac CollectI, dtac (mk_conjunctN n i'),
blanchet@48975
  1164
          REPEAT_DETERM o etac allE, etac mp, rtac @{thm ssubst_mem[OF pair_collapse]}, atac])
blanchet@48975
  1165
        ks])
blanchet@48975
  1166
      ks,
blanchet@48975
  1167
      rtac impI,
blanchet@48975
  1168
      CONJ_WRAP' (fn i => EVERY' [rtac impI, dtac (mk_conjunctN n i),
blanchet@49305
  1169
        rtac @{thm subst[OF pair_in_Id_conv]}, etac set_mp,
blanchet@48975
  1170
        rtac CollectI, etac (refl RSN (2, @{thm subst_Pair}))]) ks] 1
blanchet@48975
  1171
  end;
blanchet@48975
  1172
blanchet@48975
  1173
fun mk_unf_coinduct_upto_tac ks cTs cts unf_coinduct bis_def bis_diag =
blanchet@48975
  1174
  EVERY' [rtac rev_mp, rtac (Drule.instantiate' cTs cts unf_coinduct),
blanchet@48975
  1175
    EVERY' (map (fn i =>
blanchet@48975
  1176
      EVERY' [etac disjE, REPEAT_DETERM o dtac @{thm meta_spec}, etac @{thm meta_mp},
blanchet@48975
  1177
        atac, rtac rev_mp, rtac subst, rtac bis_def, rtac bis_diag,
blanchet@48975
  1178
        rtac impI, dtac conjunct2, dtac (mk_conjunctN (length ks) i), REPEAT_DETERM o etac allE,
blanchet@48975
  1179
        etac impE, etac @{thm diag_UNIV_I}, REPEAT_DETERM o eresolve_tac [bexE, conjE, CollectE],
blanchet@48975
  1180
        rtac exI, rtac conjI, etac conjI, atac,
blanchet@48975
  1181
        CONJ_WRAP' (K (EVERY' [REPEAT_DETERM o resolve_tac [allI, impI],
blanchet@49305
  1182
          rtac disjI2, rtac @{thm diagE}, etac set_mp, atac])) ks])
blanchet@48975
  1183
    ks),
blanchet@48975
  1184
    rtac impI, REPEAT_DETERM o etac conjE,
blanchet@48975
  1185
    CONJ_WRAP' (K (rtac impI THEN' etac mp THEN' etac disjI1)) ks] 1;
blanchet@48975
  1186
blanchet@48975
  1187
fun mk_map_tac m n cT coiter map_comp' map_cong =
blanchet@48975
  1188
  EVERY' [rtac ext, rtac (o_apply RS trans RS sym), rtac (o_apply RS trans RS sym),
blanchet@48975
  1189
    rtac (coiter RS trans), rtac (Thm.permute_prems 0 1 (map_comp' RS box_equals)), rtac map_cong,
blanchet@48975
  1190
    REPEAT_DETERM_N m o rtac (@{thm id_o} RS fun_cong),
blanchet@48975
  1191
    REPEAT_DETERM_N n o rtac (@{thm o_id} RS fun_cong),
blanchet@48975
  1192
    rtac (o_apply RS (Drule.instantiate' [cT] [] arg_cong) RS sym)] 1;
blanchet@48975
  1193
blanchet@48975
  1194
fun mk_set_le_tac n hset_minimal set_hsets set_hset_hsetss =
blanchet@48975
  1195
  EVERY' [rtac hset_minimal,
blanchet@48975
  1196
    REPEAT_DETERM_N n o rtac @{thm Un_upper1},
blanchet@48975
  1197
    REPEAT_DETERM_N n o
blanchet@48975
  1198
      EVERY' (map3 (fn i => fn set_hset => fn set_hset_hsets =>
blanchet@48975
  1199
        EVERY' [rtac subsetI, rtac @{thm UnI2}, rtac (mk_UnN n i), etac @{thm UN_I},
blanchet@48975
  1200
          etac UnE, etac set_hset, REPEAT_DETERM_N (n - 1) o etac UnE,
blanchet@48975
  1201
          EVERY' (map (fn thm => EVERY' [etac @{thm UN_E}, etac thm, atac]) set_hset_hsets)])
blanchet@48975
  1202
      (1 upto n) set_hsets set_hset_hsetss)] 1;
blanchet@48975
  1203
blanchet@48975
  1204
fun mk_set_simp_tac n set_le set_incl_hset set_hset_incl_hsets =
blanchet@48975
  1205
  EVERY' [rtac equalityI, rtac set_le, rtac @{thm Un_least}, rtac set_incl_hset,
blanchet@48975
  1206
    REPEAT_DETERM_N (n - 1) o rtac @{thm Un_least},
blanchet@48975
  1207
    EVERY' (map (fn thm => rtac @{thm UN_least} THEN' etac thm) set_hset_incl_hsets)] 1;
blanchet@48975
  1208
blanchet@48975
  1209
fun mk_map_id_tac maps coiter_unique coiter_unf =
blanchet@48975
  1210
  EVERY' [rtac (coiter_unique RS trans), EVERY' (map (fn thm => rtac (thm RS sym)) maps),
blanchet@48975
  1211
    rtac coiter_unf] 1;
blanchet@48975
  1212
blanchet@48975
  1213
fun mk_map_comp_tac m n maps map_comps map_congs coiter_unique =
blanchet@48975
  1214
  EVERY' [rtac coiter_unique,
blanchet@48975
  1215
    EVERY' (map3 (fn map_thm => fn map_comp => fn map_cong =>
blanchet@48975
  1216
      EVERY' (map rtac
blanchet@48975
  1217
        ([@{thm o_assoc} RS trans,
blanchet@48975
  1218
        @{thm arg_cong2[of _ _ _ _ "op o"]} OF [map_comp RS sym, refl] RS trans,
blanchet@48975
  1219
        @{thm o_assoc} RS trans RS sym,
blanchet@48975
  1220
        @{thm arg_cong2[of _ _ _ _ "op o"]} OF [map_thm, refl] RS trans,
blanchet@48975
  1221
        @{thm o_assoc} RS sym RS trans, map_thm RS arg_cong RS trans, @{thm o_assoc} RS trans,
blanchet@48975
  1222
        @{thm arg_cong2[of _ _ _ _ "op o"]} OF [map_comp RS sym, refl] RS trans,
blanchet@48975
  1223
        ext, o_apply RS trans,  o_apply RS trans RS sym, map_cong] @
blanchet@48975
  1224
        replicate m (@{thm id_o} RS fun_cong) @
blanchet@48975
  1225
        replicate n (@{thm o_id} RS fun_cong))))
blanchet@48975
  1226
    maps map_comps map_congs)] 1;
blanchet@48975
  1227
blanchet@48975
  1228
fun mk_mcong_tac m coinduct_tac map_comp's map_simps map_congs set_naturalss set_hsetss
blanchet@48975
  1229
  set_hset_hsetsss =
blanchet@48975
  1230
  let
blanchet@48975
  1231
    val n = length map_comp's;
blanchet@48975
  1232
    val ks = 1 upto n;
blanchet@48975
  1233
  in
blanchet@48975
  1234
    EVERY' ([rtac rev_mp,
blanchet@48975
  1235
      coinduct_tac] @
blanchet@48975
  1236
      maps (fn (((((map_comp'_trans, map_simps_trans), map_cong), set_naturals), set_hsets),
blanchet@48975
  1237
        set_hset_hsetss) =>
blanchet@48975
  1238
        [REPEAT_DETERM o eresolve_tac [exE, conjE], hyp_subst_tac, rtac exI, rtac conjI, rtac conjI,
blanchet@48975
  1239
         rtac map_comp'_trans, rtac sym, rtac map_simps_trans, rtac map_cong,
blanchet@48975
  1240
         REPEAT_DETERM_N m o (rtac o_apply_trans_sym THEN' rtac @{thm id_apply}),
blanchet@48975
  1241
         REPEAT_DETERM_N n o rtac fst_convol_fun_cong_sym,
blanchet@48975
  1242
         rtac map_comp'_trans, rtac sym, rtac map_simps_trans, rtac map_cong,
blanchet@48975
  1243
         EVERY' (maps (fn set_hset =>
blanchet@48975
  1244
           [rtac o_apply_trans_sym, rtac (@{thm id_apply} RS trans), etac CollectE,
blanchet@48975
  1245
           REPEAT_DETERM o etac conjE, etac bspec, etac set_hset]) set_hsets),
blanchet@48975
  1246
         REPEAT_DETERM_N n o rtac snd_convol_fun_cong_sym,
blanchet@48975
  1247
         CONJ_WRAP' (fn (set_natural, set_hset_hsets) =>
blanchet@48975
  1248
           EVERY' [REPEAT_DETERM o rtac allI, rtac impI, rtac @{thm image_convolD},
blanchet@49305
  1249
             etac set_rev_mp, rtac ord_eq_le_trans, rtac set_natural,
blanchet@48975
  1250
             rtac @{thm image_mono}, rtac subsetI, rtac CollectI, etac CollectE,
blanchet@48975
  1251
             REPEAT_DETERM o etac conjE,
blanchet@48975
  1252
             CONJ_WRAP' (fn set_hset_hset =>
blanchet@48975
  1253
               EVERY' [rtac ballI, etac bspec, etac set_hset_hset, atac]) set_hset_hsets])
blanchet@48975
  1254
           (drop m set_naturals ~~ set_hset_hsetss)])
blanchet@48975
  1255
        (map (fn th => th RS trans) map_comp's ~~ map (fn th => th RS trans) map_simps ~~
blanchet@48975
  1256
          map_congs ~~ set_naturalss ~~ set_hsetss ~~ set_hset_hsetsss) @
blanchet@48975
  1257
      [rtac impI,
blanchet@48975
  1258
       CONJ_WRAP' (fn k =>
blanchet@48975
  1259
         EVERY' [rtac impI, dtac (mk_conjunctN n k), etac mp, rtac exI, rtac conjI, etac CollectI,
blanchet@48975
  1260
           rtac conjI, rtac refl, rtac refl]) ks]) 1
blanchet@48975
  1261
  end
blanchet@48975
  1262
blanchet@48975
  1263
fun mk_map_unique_tac coiter_unique map_comps {context = ctxt, prems = _} =
blanchet@48975
  1264
  rtac coiter_unique 1 THEN
blanchet@48975
  1265
  Local_Defs.unfold_tac ctxt (map (fn thm => thm RS sym) map_comps @ @{thms o_assoc id_o o_id}) THEN
blanchet@48975
  1266
  ALLGOALS (etac sym);
blanchet@48975
  1267
blanchet@48975
  1268
fun mk_col_natural_tac cts rec_0s rec_Sucs map_simps set_naturalss
blanchet@48975
  1269
  {context = ctxt, prems = _} =
blanchet@48975
  1270
  let
blanchet@48975
  1271
    val n = length map_simps;
blanchet@48975
  1272
  in
blanchet@49305
  1273
    EVERY' [rtac (Drule.instantiate' [] cts nat_induct),
blanchet@48975
  1274
      REPEAT_DETERM o rtac allI, SELECT_GOAL (Local_Defs.unfold_tac ctxt rec_0s),
blanchet@48975
  1275
      CONJ_WRAP' (K (rtac @{thm image_empty})) rec_0s,
blanchet@48975
  1276
      REPEAT_DETERM o rtac allI,
blanchet@48975
  1277
      CONJ_WRAP' (fn (rec_Suc, (map_simp, set_nats)) => EVERY'
blanchet@48975
  1278
        [SELECT_GOAL (Local_Defs.unfold_tac ctxt
blanchet@48975
  1279
          (rec_Suc :: map_simp :: set_nats @ @{thms image_Un image_UN UN_simps(10)})),
blanchet@48975
  1280
        rtac @{thm Un_cong}, rtac refl,
blanchet@48975
  1281
        CONJ_WRAP_GEN' (rtac (Thm.permute_prems 0 1 @{thm Un_cong}))
blanchet@48975
  1282
          (fn i => EVERY' [rtac @{thm UN_cong[OF refl]},
blanchet@48975
  1283
            REPEAT_DETERM o etac allE, etac (mk_conjunctN n i)]) (n downto 1)])
blanchet@48975
  1284
      (rec_Sucs ~~ (map_simps ~~ set_naturalss))] 1
blanchet@48975
  1285
  end;
blanchet@48975
  1286
blanchet@48975
  1287
fun mk_set_natural_tac hset_def col_natural =
blanchet@48975
  1288
  EVERY' (map rtac [ext, (o_apply RS trans), (hset_def RS trans), sym,
blanchet@48975
  1289
    (o_apply RS trans), (@{thm image_cong} OF [hset_def, refl] RS trans),
blanchet@48975
  1290
    (@{thm image_UN} RS trans), (refl RS @{thm UN_cong}), col_natural]) 1;
blanchet@48975
  1291
blanchet@48975
  1292
fun mk_col_bd_tac m j cts rec_0s rec_Sucs sbd_Card_order sbd_Cinfinite set_sbdss =
blanchet@48975
  1293
  let
blanchet@48975
  1294
    val n = length rec_0s;
blanchet@48975
  1295
  in
blanchet@49305
  1296
    EVERY' [rtac (Drule.instantiate' [] cts nat_induct),
blanchet@48975
  1297
      REPEAT_DETERM o rtac allI,
blanchet@49305
  1298
      CONJ_WRAP' (fn rec_0 => EVERY' (map rtac [ordIso_ordLeq_trans,
blanchet@48975
  1299
        @{thm card_of_ordIso_subst}, rec_0, @{thm Card_order_empty}, sbd_Card_order])) rec_0s,
blanchet@48975
  1300
      REPEAT_DETERM o rtac allI,
blanchet@48975
  1301
      CONJ_WRAP' (fn (rec_Suc, set_sbds) => EVERY'
blanchet@49305
  1302
        [rtac ordIso_ordLeq_trans, rtac @{thm card_of_ordIso_subst}, rtac rec_Suc,
blanchet@48975
  1303
        rtac (sbd_Cinfinite RSN (3, @{thm Un_Cinfinite_bound})), rtac (nth set_sbds (j - 1)),
blanchet@48975
  1304
        REPEAT_DETERM_N (n - 1) o rtac (sbd_Cinfinite RSN (3, @{thm Un_Cinfinite_bound})),
blanchet@48975
  1305
        EVERY' (map2 (fn i => fn set_sbd => EVERY' [rtac @{thm UNION_Cinfinite_bound},
blanchet@48975
  1306
          rtac set_sbd, rtac ballI, REPEAT_DETERM o etac allE,
blanchet@48975
  1307
          etac (mk_conjunctN n i), rtac sbd_Cinfinite]) (1 upto n) (drop m set_sbds))])
blanchet@48975
  1308
      (rec_Sucs ~~ set_sbdss)] 1
blanchet@48975
  1309
  end;
blanchet@48975
  1310
blanchet@48975
  1311
fun mk_set_bd_tac sbd_Cinfinite hset_def col_bd =
blanchet@49305
  1312
  EVERY' (map rtac [ordIso_ordLeq_trans, @{thm card_of_ordIso_subst}, hset_def,
blanchet@49305
  1313
    ctrans, @{thm UNION_Cinfinite_bound}, ordIso_ordLeq_trans, @{thm card_of_nat},
blanchet@48975
  1314
    @{thm natLeq_ordLeq_cinfinite}, sbd_Cinfinite, ballI, col_bd, sbd_Cinfinite,
blanchet@48975
  1315
    ctrans, @{thm infinite_ordLeq_cexp}, sbd_Cinfinite, @{thm cexp_ordLeq_ccexp}]) 1;
blanchet@48975
  1316
blanchet@48975
  1317
fun mk_in_bd_tac isNode_hset isNode_hsets carT_def card_of_carT mor_image Rep_inverse mor_hsets
blanchet@48975
  1318
  sbd_Cnotzero sbd_Card_order mor_Rep coalgT mor_T_final tcoalg =
blanchet@48975
  1319
  let
blanchet@48975
  1320
    val n = length isNode_hsets;
blanchet@48975
  1321
    val in_hin_tac = rtac CollectI THEN'
blanchet@48975
  1322
      CONJ_WRAP' (fn mor_hset => EVERY' (map etac
blanchet@49305
  1323
        [mor_hset OF [coalgT, mor_T_final] RS sym RS ord_eq_le_trans,
blanchet@49305
  1324
        arg_cong RS sym RS ord_eq_le_trans,
blanchet@49305
  1325
        mor_hset OF [tcoalg, mor_Rep, UNIV_I] RS ord_eq_le_trans])) mor_hsets;
blanchet@48975
  1326
  in
blanchet@48975
  1327
    EVERY' [rtac (Thm.permute_prems 0 1 @{thm ordLeq_transitive}), rtac ctrans,
blanchet@49305
  1328
      rtac @{thm card_of_image}, rtac ordIso_ordLeq_trans,
blanchet@48975
  1329
      rtac @{thm card_of_ordIso_subst}, rtac @{thm sym[OF proj_image]}, rtac ctrans,
blanchet@48975
  1330
      rtac @{thm card_of_image}, rtac ctrans, rtac card_of_carT, rtac @{thm cexp_mono2_Cnotzero},
blanchet@48975
  1331
      rtac @{thm cexp_ordLeq_ccexp},  rtac @{thm csum_Cnotzero2}, rtac @{thm ctwo_Cnotzero},
blanchet@48975
  1332
      rtac @{thm Cnotzero_cexp}, rtac sbd_Cnotzero, rtac sbd_Card_order,
blanchet@48975
  1333
      rtac @{thm card_of_mono1}, rtac subsetI, rtac @{thm image_eqI}, rtac sym,
blanchet@48975
  1334
      rtac Rep_inverse, REPEAT_DETERM o eresolve_tac [CollectE, conjE],
blanchet@49305
  1335
      rtac set_mp, rtac equalityD2, rtac @{thm sym[OF proj_image]}, rtac imageE,
blanchet@49305
  1336
      rtac set_rev_mp, rtac mor_image, rtac mor_Rep, rtac UNIV_I, rtac equalityD2,
blanchet@48975
  1337
      rtac @{thm proj_image},  rtac @{thm image_eqI}, atac,
blanchet@49305
  1338
      ftac (carT_def RS equalityD1 RS set_mp),
blanchet@48975
  1339
      REPEAT_DETERM o eresolve_tac [CollectE, exE, conjE], hyp_subst_tac,
blanchet@49305
  1340
      rtac (carT_def RS equalityD2 RS set_mp), rtac CollectI, REPEAT_DETERM o rtac exI,
blanchet@48975
  1341
      rtac conjI, rtac refl, rtac conjI, etac conjI, etac conjI, etac conjI, rtac conjI,
blanchet@48975
  1342
      rtac ballI, dtac bspec, atac, REPEAT_DETERM o etac conjE, rtac conjI,
blanchet@48975
  1343
      CONJ_WRAP_GEN' (etac disjE) (fn (i, isNode_hset) =>
blanchet@48975
  1344
        EVERY' [rtac (mk_disjIN n i), rtac isNode_hset, atac, atac, atac, in_hin_tac])
blanchet@48975
  1345
      (1 upto n ~~ isNode_hsets),
blanchet@48975
  1346
      CONJ_WRAP' (fn isNode_hset =>
blanchet@48975
  1347
        EVERY' [rtac ballI, rtac isNode_hset, atac, ftac CollectD, etac @{thm SuccD},
blanchet@48975
  1348
          etac bspec, atac, in_hin_tac])
blanchet@48975
  1349
      isNode_hsets,
blanchet@48975
  1350
      atac, rtac isNode_hset, atac, atac, atac, in_hin_tac] 1
blanchet@48975
  1351
  end;
blanchet@48975
  1352
blanchet@48975
  1353
fun mk_bd_card_order_tac sbd_card_order =
blanchet@48975
  1354
  EVERY' (map rtac [@{thm card_order_ccexp}, sbd_card_order, sbd_card_order]) 1;
blanchet@48975
  1355
blanchet@48975
  1356
fun mk_bd_cinfinite_tac sbd_Cinfinite =
blanchet@48975
  1357
  EVERY' (map rtac [@{thm cinfinite_ccexp}, @{thm ctwo_ordLeq_Cinfinite},
blanchet@48975
  1358
    sbd_Cinfinite, sbd_Cinfinite]) 1;
blanchet@48975
  1359
blanchet@48975
  1360
fun mk_pickWP_assms_tac set_incl_hsets set_incl_hins map_eq =
blanchet@48975
  1361
  let
blanchet@48975
  1362
    val m = length set_incl_hsets;
blanchet@48975
  1363
  in
blanchet@48975
  1364
    EVERY' [REPEAT_DETERM o eresolve_tac [CollectE, conjE], rtac CollectI,
blanchet@48975
  1365
      EVERY' (map (fn thm => rtac conjI THEN' etac (thm RS @{thm subset_trans})) set_incl_hsets),
blanchet@48975
  1366
      CONJ_WRAP' (fn thm => rtac thm THEN' REPEAT_DETERM_N m o atac) set_incl_hins,
blanchet@48975
  1367
      REPEAT_DETERM o eresolve_tac [CollectE, conjE], rtac CollectI,
blanchet@48975
  1368
      EVERY' (map (fn thm => rtac conjI THEN' etac (thm RS @{thm subset_trans})) set_incl_hsets),
blanchet@48975
  1369
      CONJ_WRAP' (fn thm => rtac thm THEN' REPEAT_DETERM_N m o atac) set_incl_hins,
blanchet@48975
  1370
      REPEAT_DETERM o eresolve_tac [CollectE, conjE], etac map_eq]
blanchet@48975
  1371
  end;
blanchet@48975
  1372
blanchet@48975
  1373
fun mk_coalg_thePull_tac m coalg_def map_wpulls set_naturalss pickWP_assms_tacs
blanchet@48975
  1374
  {context = ctxt, prems = _} =
blanchet@48975
  1375
  Local_Defs.unfold_tac ctxt [coalg_def] THEN
blanchet@48975
  1376
  CONJ_WRAP' (fn (map_wpull, (pickWP_assms_tac, set_naturals)) =>
blanchet@48975
  1377
    EVERY' [rtac ballI, dtac @{thm set_mp[OF equalityD1[OF thePull_def]]},
blanchet@48975
  1378
      REPEAT_DETERM o eresolve_tac [CollectE, @{thm prod_caseE}],
blanchet@48975
  1379
      hyp_subst_tac, rtac rev_mp, rtac (map_wpull RS @{thm pickWP(1)}),
blanchet@48975
  1380
      EVERY' (map (etac o mk_conjunctN m) (1 upto m)),
blanchet@48975
  1381
      pickWP_assms_tac,
blanchet@48975
  1382
      SELECT_GOAL (Local_Defs.unfold_tac ctxt @{thms o_apply prod.cases}), rtac impI,
blanchet@48975
  1383
      REPEAT_DETERM o eresolve_tac [CollectE, conjE],
blanchet@48975
  1384
      rtac CollectI,
blanchet@48975
  1385
      REPEAT_DETERM_N m o (rtac conjI THEN' rtac @{thm subset_UNIV}),
blanchet@48975
  1386
      CONJ_WRAP' (fn set_natural =>
blanchet@49305
  1387
        EVERY' [rtac ord_eq_le_trans, rtac trans, rtac set_natural,
blanchet@49305
  1388
          rtac trans_fun_cong_image_id_id_apply, atac])
blanchet@48975
  1389
      (drop m set_naturals)])
blanchet@48975
  1390
  (map_wpulls ~~ (pickWP_assms_tacs ~~ set_naturalss)) 1;
blanchet@48975
  1391
blanchet@48975
  1392
fun mk_mor_thePull_nth_tac conv pick m mor_def map_wpulls map_comps pickWP_assms_tacs
blanchet@48975
  1393
  {context = ctxt, prems = _} =
blanchet@48975
  1394
  let
blanchet@48975
  1395
    val n = length map_comps;
blanchet@48975
  1396
  in
blanchet@48975
  1397
    Local_Defs.unfold_tac ctxt [mor_def] THEN
blanchet@48975
  1398
    EVERY' [rtac conjI,
blanchet@48975
  1399
      CONJ_WRAP' (K (rtac ballI THEN' rtac UNIV_I)) (1 upto n),
blanchet@48975
  1400
      CONJ_WRAP' (fn (map_wpull, (pickWP_assms_tac, map_comp)) =>
blanchet@48975
  1401
        EVERY' [rtac ballI, dtac @{thm set_mp[OF equalityD1[OF thePull_def]]},
blanchet@48975
  1402
          REPEAT_DETERM o eresolve_tac [CollectE, @{thm prod_caseE}, conjE],
blanchet@48975
  1403
          hyp_subst_tac,
blanchet@48975
  1404
          SELECT_GOAL (Local_Defs.unfold_tac ctxt @{thms o_apply prod.cases}),
blanchet@48975
  1405
          rtac (map_comp RS trans),
blanchet@48975
  1406
          SELECT_GOAL (Local_Defs.unfold_tac ctxt (conv :: @{thms o_id id_o})),
blanchet@48975
  1407
          rtac (map_wpull RS pick), REPEAT_DETERM_N m o atac,
blanchet@48975
  1408
          pickWP_assms_tac])
blanchet@48975
  1409
      (map_wpulls ~~ (pickWP_assms_tacs ~~ map_comps))] 1
blanchet@48975
  1410
  end;
blanchet@48975
  1411
blanchet@48975
  1412
val mk_mor_thePull_fst_tac = mk_mor_thePull_nth_tac @{thm fst_conv} @{thm pickWP(2)};
blanchet@48975
  1413
val mk_mor_thePull_snd_tac = mk_mor_thePull_nth_tac @{thm snd_conv} @{thm pickWP(3)};
blanchet@48975
  1414
blanchet@48975
  1415
fun mk_mor_thePull_pick_tac mor_def coiters map_comps {context = ctxt, prems = _} =
blanchet@48975
  1416
  Local_Defs.unfold_tac ctxt [mor_def, @{thm thePull_def}] THEN rtac conjI 1 THEN
blanchet@48975
  1417
  CONJ_WRAP' (K (rtac ballI THEN' rtac UNIV_I)) coiters 1 THEN
blanchet@48975
  1418
  CONJ_WRAP' (fn (coiter, map_comp) =>
blanchet@48975
  1419
    EVERY' [rtac ballI, REPEAT_DETERM o eresolve_tac [CollectE, @{thm prod_caseE}, conjE],
blanchet@48975
  1420
      hyp_subst_tac,
blanchet@48975
  1421
      SELECT_GOAL (Local_Defs.unfold_tac ctxt (coiter :: map_comp :: @{thms comp_def id_def})),
blanchet@48975
  1422
      rtac refl])
blanchet@48975
  1423
  (coiters ~~ map_comps) 1;
blanchet@48975
  1424
blanchet@48975
  1425
fun mk_pick_col_tac m j cts rec_0s rec_Sucs coiters set_naturalss map_wpulls pickWP_assms_tacs
blanchet@48975
  1426
  {context = ctxt, prems = _} =
blanchet@48975
  1427
  let
blanchet@48975
  1428
    val n = length rec_0s;
blanchet@48975
  1429
    val ks = n downto 1;
blanchet@48975
  1430
  in
blanchet@49305
  1431
    EVERY' [rtac (Drule.instantiate' [] cts nat_induct),
blanchet@48975
  1432
      REPEAT_DETERM o rtac allI,
blanchet@48975
  1433
      CONJ_WRAP' (fn thm => EVERY'
blanchet@49305
  1434
        [rtac impI, rtac ord_eq_le_trans, rtac thm, rtac @{thm empty_subsetI}]) rec_0s,
blanchet@48975
  1435
      REPEAT_DETERM o rtac allI,
blanchet@48975
  1436
      CONJ_WRAP' (fn (rec_Suc, ((coiter, set_naturals), (map_wpull, pickWP_assms_tac))) =>
blanchet@48975
  1437
        EVERY' [rtac impI, dtac @{thm set_mp[OF equalityD1[OF thePull_def]]},
blanchet@48975
  1438
          REPEAT_DETERM o eresolve_tac [CollectE, @{thm prod_caseE}],
blanchet@48975
  1439
          hyp_subst_tac, rtac rev_mp, rtac (map_wpull RS @{thm pickWP(1)}),
blanchet@48975
  1440
          EVERY' (map (etac o mk_conjunctN m) (1 upto m)),
blanchet@48975
  1441
          pickWP_assms_tac,
blanchet@48975
  1442
          rtac impI, REPEAT_DETERM o eresolve_tac [CollectE, conjE],
blanchet@49305
  1443
          rtac ord_eq_le_trans, rtac rec_Suc,
blanchet@48975
  1444
          rtac @{thm Un_least},
blanchet@48975
  1445
          SELECT_GOAL (Local_Defs.unfold_tac ctxt [coiter, nth set_naturals (j - 1),
blanchet@48975
  1446
            @{thm prod.cases}]),
blanchet@49305
  1447
          etac ord_eq_le_trans_trans_fun_cong_image_id_id_apply,
blanchet@48975
  1448
          CONJ_WRAP_GEN' (rtac (Thm.permute_prems 0 1 @{thm Un_least})) (fn (i, set_natural) =>
blanchet@48975
  1449
            EVERY' [rtac @{thm UN_least},
blanchet@48975
  1450
              SELECT_GOAL (Local_Defs.unfold_tac ctxt [coiter, set_natural, @{thm prod.cases}]),
blanchet@48975
  1451
              etac imageE, hyp_subst_tac, REPEAT_DETERM o etac allE,
blanchet@49305
  1452
              dtac (mk_conjunctN n i), etac mp, etac set_mp, atac])
blanchet@48975
  1453
          (ks ~~ rev (drop m set_naturals))])
blanchet@48975
  1454
      (rec_Sucs ~~ ((coiters ~~ set_naturalss) ~~ (map_wpulls ~~ pickWP_assms_tacs)))] 1
blanchet@48975
  1455
  end;
blanchet@48975
  1456
blanchet@48975
  1457
fun mk_wpull_tac m coalg_thePull mor_thePull_fst mor_thePull_snd mor_thePull_pick
blanchet@48975
  1458
  mor_unique pick_cols hset_defs =
blanchet@49305
  1459
  EVERY' [rtac (@{thm wpull_def} RS ssubst_id), REPEAT_DETERM o rtac allI, rtac impI,
blanchet@48975
  1460
    REPEAT_DETERM o etac conjE, rtac bexI, rtac conjI,
blanchet@48975
  1461
    rtac box_equals, rtac mor_unique,
blanchet@48975
  1462
    rtac coalg_thePull, REPEAT_DETERM_N (m - 1) o etac conjI, atac,
blanchet@48975
  1463
    rtac mor_thePull_pick, REPEAT_DETERM_N (m - 1) o etac conjI, atac,
blanchet@48975
  1464
    rtac mor_thePull_fst, REPEAT_DETERM_N (m - 1) o etac conjI, atac,
blanchet@48975
  1465
    rtac @{thm set_mp[OF equalityD2[OF thePull_def]]}, rtac CollectI,
blanchet@48975
  1466
    rtac @{thm prod_caseI}, etac conjI, etac conjI, atac, rtac o_apply, rtac @{thm fst_conv},
blanchet@48975
  1467
    rtac box_equals, rtac mor_unique,
blanchet@48975
  1468
    rtac coalg_thePull, REPEAT_DETERM_N (m - 1) o etac conjI, atac,
blanchet@48975
  1469
    rtac mor_thePull_pick, REPEAT_DETERM_N (m - 1) o etac conjI, atac,
blanchet@48975
  1470
    rtac mor_thePull_snd, REPEAT_DETERM_N (m - 1) o etac conjI, atac,
blanchet@48975
  1471
    rtac @{thm set_mp[OF equalityD2[OF thePull_def]]}, rtac CollectI,
blanchet@48975
  1472
    rtac @{thm prod_caseI}, etac conjI, etac conjI, atac, rtac o_apply, rtac @{thm snd_conv},
blanchet@48975
  1473
    rtac CollectI,
blanchet@48975
  1474
    CONJ_WRAP' (fn (pick, def) =>
blanchet@49305
  1475
      EVERY' [rtac (def RS ord_eq_le_trans), rtac @{thm UN_least},
blanchet@48975
  1476
        rtac pick, REPEAT_DETERM_N (m - 1) o etac conjI, atac,
blanchet@48975
  1477
        rtac @{thm set_mp[OF equalityD2[OF thePull_def]]}, rtac CollectI,
blanchet@48975
  1478
        rtac @{thm prod_caseI}, etac conjI, etac conjI, atac])
blanchet@48975
  1479
    (pick_cols ~~ hset_defs)] 1;
blanchet@48975
  1480
traytel@49104
  1481
fun mk_wit_tac n unf_flds set_simp wit coind_wits {context = ctxt, prems = _} =
traytel@49104
  1482
  ALLGOALS (TRY o (eresolve_tac coind_wits THEN' rtac refl)) THEN
blanchet@48975
  1483
  REPEAT_DETERM (atac 1 ORELSE
blanchet@49305
  1484
    EVERY' [dtac set_rev_mp, rtac equalityD1, resolve_tac set_simp,
blanchet@48975
  1485
    K (Local_Defs.unfold_tac ctxt unf_flds),
blanchet@48975
  1486
    REPEAT_DETERM_N n o etac UnE,
blanchet@48975
  1487
    REPEAT_DETERM o
blanchet@48975
  1488
      (TRY o REPEAT_DETERM o etac UnE THEN' TRY o etac @{thm UN_E} THEN'
blanchet@48975
  1489
        (eresolve_tac wit ORELSE'
blanchet@48975
  1490
        (dresolve_tac wit THEN'
blanchet@48975
  1491
          (etac FalseE ORELSE'
blanchet@49305
  1492
          EVERY' [hyp_subst_tac, dtac set_rev_mp, rtac equalityD1, resolve_tac set_simp,
blanchet@48975
  1493
            K (Local_Defs.unfold_tac ctxt unf_flds), REPEAT_DETERM_N n o etac UnE]))))] 1);
blanchet@48975
  1494
traytel@49104
  1495
fun mk_coind_wit_tac induct coiters set_nats wits {context = ctxt, prems = _} =
traytel@49104
  1496
  rtac induct 1 THEN ALLGOALS (TRY o rtac impI THEN' TRY o hyp_subst_tac) THEN
traytel@49104
  1497
  Local_Defs.unfold_tac ctxt (coiters @ set_nats @ @{thms image_id id_apply}) THEN
traytel@49104
  1498
  ALLGOALS (REPEAT_DETERM o etac imageE THEN' TRY o hyp_subst_tac) THEN
traytel@49104
  1499
  ALLGOALS (TRY o
traytel@49104
  1500
    FIRST' [rtac TrueI, rtac refl, etac (refl RSN (2, mp)), dresolve_tac wits THEN' etac FalseE])
traytel@49104
  1501
blanchet@48975
  1502
fun mk_rel_unfold_tac in_Jrels i in_rel map_comp map_cong map_simp set_simps unf_inject unf_fld
blanchet@48975
  1503
  set_naturals set_incls set_set_inclss =
blanchet@48975
  1504
  let
blanchet@48975
  1505
    val m = length set_incls;
blanchet@48975
  1506
    val n = length set_set_inclss;
blanchet@48975
  1507
    val (passive_set_naturals, active_set_naturals) = chop m set_naturals;
blanchet@48975
  1508
    val in_Jrel = nth in_Jrels (i - 1);
blanchet@48975
  1509
    val if_tac =
blanchet@48975
  1510
      EVERY' [dtac (in_Jrel RS iffD1), REPEAT_DETERM o eresolve_tac [exE, conjE, CollectE],
blanchet@48975
  1511
        rtac (in_rel RS iffD2), rtac exI, rtac conjI, rtac CollectI,
blanchet@48975
  1512
        EVERY' (map2 (fn set_natural => fn set_incl =>
blanchet@49305
  1513
          EVERY' [rtac conjI, rtac ord_eq_le_trans, rtac set_natural,
blanchet@49305
  1514
            rtac ord_eq_le_trans, rtac trans_fun_cong_image_id_id_apply,
blanchet@48975
  1515
            etac (set_incl RS @{thm subset_trans})])
blanchet@48975
  1516
        passive_set_naturals set_incls),
blanchet@48975
  1517
        CONJ_WRAP' (fn (in_Jrel, (set_natural, set_set_incls)) =>
blanchet@49305
  1518
          EVERY' [rtac ord_eq_le_trans, rtac set_natural, rtac @{thm image_subsetI},
blanchet@48975
  1519
            rtac (in_Jrel RS iffD2), rtac exI, rtac conjI, rtac CollectI,
blanchet@48975
  1520
            CONJ_WRAP' (fn thm => etac (thm RS @{thm subset_trans}) THEN' atac) set_set_incls,
blanchet@48975
  1521
            rtac conjI, rtac refl, rtac refl])
blanchet@48975
  1522
        (in_Jrels ~~ (active_set_naturals ~~ set_set_inclss)),
blanchet@48975
  1523
        CONJ_WRAP' (fn conv =>
blanchet@48975
  1524
          EVERY' [rtac trans, rtac map_comp, rtac trans, rtac map_cong,
blanchet@48975
  1525
          REPEAT_DETERM_N m o rtac @{thm fun_cong[OF o_id]},
blanchet@48975
  1526
          REPEAT_DETERM_N n o EVERY' (map rtac [trans, o_apply, conv]),
blanchet@48975
  1527
          rtac trans, rtac sym, rtac map_simp, rtac (unf_inject RS iffD2), atac])
blanchet@48975
  1528
        @{thms fst_conv snd_conv}];
blanchet@48975
  1529
    val only_if_tac =
blanchet@48975
  1530
      EVERY' [dtac (in_rel RS iffD1), REPEAT_DETERM o eresolve_tac [exE, conjE, CollectE],
blanchet@48975
  1531
        rtac (in_Jrel RS iffD2), rtac exI, rtac conjI, rtac CollectI,
blanchet@48975
  1532
        CONJ_WRAP' (fn (set_simp, passive_set_natural) =>
blanchet@49305
  1533
          EVERY' [rtac ord_eq_le_trans, rtac set_simp, rtac @{thm Un_least},
blanchet@49305
  1534
            rtac ord_eq_le_trans, rtac box_equals, rtac passive_set_natural,
blanchet@49305
  1535
            rtac (unf_fld RS sym RS arg_cong), rtac trans_fun_cong_image_id_id_apply, atac,
blanchet@48975
  1536
            CONJ_WRAP_GEN' (rtac (Thm.permute_prems 0 1 @{thm Un_least}))
blanchet@49305
  1537
              (fn (active_set_natural, in_Jrel) => EVERY' [rtac ord_eq_le_trans,
blanchet@48975
  1538
                rtac @{thm UN_cong[OF _ refl]}, rtac @{thm box_equals[OF _ _ refl]},
blanchet@48975
  1539
                rtac active_set_natural, rtac (unf_fld RS sym RS arg_cong), rtac @{thm UN_least},
blanchet@49305
  1540
                dtac set_rev_mp, etac @{thm image_mono}, etac imageE,
blanchet@48975
  1541
                dtac @{thm ssubst_mem[OF pair_collapse]}, dtac (in_Jrel RS iffD1),
blanchet@48975
  1542
                dtac @{thm someI_ex}, REPEAT_DETERM o etac conjE,
blanchet@48975
  1543
                dtac (Thm.permute_prems 0 1 @{thm ssubst_mem}), atac,
blanchet@48975
  1544
                hyp_subst_tac, REPEAT_DETERM o eresolve_tac [CollectE, conjE], atac])
blanchet@48975
  1545
            (rev (active_set_naturals ~~ in_Jrels))])
blanchet@48975
  1546
        (set_simps ~~ passive_set_naturals),
blanchet@48975
  1547
        rtac conjI,
blanchet@48975
  1548
        REPEAT_DETERM_N 2 o EVERY'[rtac (unf_inject RS iffD1), rtac trans, rtac map_simp,
blanchet@48975
  1549
          rtac box_equals, rtac map_comp, rtac (unf_fld RS sym RS arg_cong), rtac trans,
blanchet@48975
  1550
          rtac map_cong, REPEAT_DETERM_N m o rtac @{thm fun_cong[OF o_id]},
blanchet@49305
  1551
          EVERY' (map (fn in_Jrel => EVERY' [rtac trans, rtac o_apply, dtac set_rev_mp, atac,
blanchet@48975
  1552
            dtac @{thm ssubst_mem[OF pair_collapse]}, dtac (in_Jrel RS iffD1), dtac @{thm someI_ex},
blanchet@48975
  1553
            REPEAT_DETERM o etac conjE, atac]) in_Jrels),
blanchet@48975
  1554
          atac]]
blanchet@48975
  1555
  in
blanchet@48975
  1556
    EVERY' [rtac iffI, if_tac, only_if_tac] 1
blanchet@48975
  1557
  end;
blanchet@48975
  1558
blanchet@48975
  1559
end;