src/HOL/Tools/BNF/bnf_def_tactics.ML
author traytel
Sun Dec 17 08:42:59 2017 +0100 (18 months ago)
changeset 67222 19809bc9d7ff
parent 63714 b62f4f765353
child 67223 711eec20aecd
permissions -rw-r--r--
made tactics more robust
blanchet@55061
     1
(*  Title:      HOL/Tools/BNF/bnf_def_tactics.ML
blanchet@49284
     2
    Author:     Dmitriy Traytel, TU Muenchen
blanchet@49284
     3
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@57668
     4
    Author:     Martin Desharnais, TU Muenchen
traytel@62324
     5
    Author:     Ondrej Kuncar, TU Muenchen
traytel@62324
     6
    Copyright   2012, 2013, 2014, 2015
blanchet@49284
     7
blanchet@49284
     8
Tactics for definition of bounded natural functors.
blanchet@49284
     9
*)
blanchet@49284
    10
blanchet@49284
    11
signature BNF_DEF_TACTICS =
blanchet@49284
    12
sig
traytel@60728
    13
  val mk_collect_set_map_tac: Proof.context -> thm list -> tactic
traytel@60728
    14
  val mk_in_mono_tac: Proof.context -> int -> tactic
desharna@57970
    15
  val mk_inj_map_strong_tac: Proof.context -> thm -> thm list -> thm -> tactic
traytel@60728
    16
  val mk_inj_map_tac: Proof.context -> int -> thm -> thm -> thm -> thm -> tactic
blanchet@53285
    17
  val mk_map_id: thm -> thm
desharna@56903
    18
  val mk_map_ident: Proof.context -> thm -> thm
blanchet@53288
    19
  val mk_map_comp: thm -> thm
wenzelm@51798
    20
  val mk_map_cong_tac: Proof.context -> thm -> tactic
blanchet@53290
    21
  val mk_set_map: thm -> thm
blanchet@49284
    22
traytel@55197
    23
  val mk_rel_Grp_tac: Proof.context -> thm list -> thm -> thm -> thm -> thm -> thm list -> tactic
traytel@60728
    24
  val mk_rel_eq_tac: Proof.context -> int -> thm -> thm -> thm -> tactic
traytel@55197
    25
  val mk_rel_OO_le_tac: Proof.context -> thm list -> thm -> thm -> thm -> thm list -> tactic
traytel@60728
    26
  val mk_rel_conversep_tac: Proof.context -> thm -> thm -> tactic
traytel@55197
    27
  val mk_rel_conversep_le_tac: Proof.context -> thm list -> thm -> thm -> thm -> thm list -> tactic
desharna@57932
    28
  val mk_rel_map0_tac: Proof.context -> int -> thm -> thm -> thm -> thm -> tactic
traytel@60728
    29
  val mk_rel_mono_tac: Proof.context -> thm list -> thm -> tactic
desharna@57967
    30
  val mk_rel_mono_strong0_tac: Proof.context -> thm -> thm list -> tactic
traytel@61242
    31
  val mk_rel_cong_tac: Proof.context -> thm list * thm list -> thm -> tactic
traytel@62324
    32
  val mk_rel_eq_onp_tac: Proof.context -> thm -> thm -> thm -> tactic
traytel@62324
    33
  val mk_pred_mono_strong0_tac: Proof.context -> thm -> thm -> tactic
traytel@63714
    34
  val mk_pred_mono_tac: Proof.context -> thm -> thm -> tactic
traytel@52635
    35
traytel@55197
    36
  val mk_map_transfer_tac: Proof.context -> thm -> thm -> thm list -> thm -> thm -> tactic
traytel@62329
    37
  val mk_pred_transfer_tac: Proof.context -> int -> thm -> thm -> thm -> tactic
traytel@62329
    38
  val mk_rel_transfer_tac: Proof.context -> thm -> thm list -> thm -> tactic
traytel@62329
    39
  val mk_set_transfer_tac: Proof.context -> thm -> thm list -> tactic
traytel@52719
    40
traytel@55197
    41
  val mk_in_bd_tac: Proof.context -> int -> thm -> thm -> thm -> thm -> thm list -> thm list ->
traytel@55197
    42
    thm -> thm -> thm -> thm -> tactic
traytel@54189
    43
traytel@55197
    44
  val mk_trivial_wit_tac: Proof.context -> thm list -> thm list -> tactic
blanchet@49284
    45
end;
blanchet@49284
    46
blanchet@49284
    47
structure BNF_Def_Tactics : BNF_DEF_TACTICS =
blanchet@49284
    48
struct
blanchet@49284
    49
blanchet@49284
    50
open BNF_Util
blanchet@49284
    51
open BNF_Tactics
blanchet@49284
    52
traytel@52635
    53
val ord_eq_le_trans = @{thm ord_eq_le_trans};
traytel@52844
    54
val ord_le_eq_trans = @{thm ord_le_eq_trans};
traytel@52749
    55
val conversep_shift = @{thm conversep_le_swap} RS iffD1;
traytel@52635
    56
blanchet@53285
    57
fun mk_map_id id = mk_trans (fun_cong OF [id]) @{thm id_apply};
desharna@56903
    58
fun mk_map_ident ctxt = unfold_thms ctxt @{thms id_def};
blanchet@55067
    59
fun mk_map_comp comp = @{thm comp_eq_dest_lhs} OF [mk_sym comp];
wenzelm@51798
    60
fun mk_map_cong_tac ctxt cong0 =
traytel@60728
    61
  (hyp_subst_tac ctxt THEN' rtac ctxt cong0 THEN'
wenzelm@60752
    62
   REPEAT_DETERM o (dtac ctxt meta_spec THEN' etac ctxt meta_mp THEN' assume_tac ctxt)) 1;
blanchet@53290
    63
fun mk_set_map set_map0 = set_map0 RS @{thm comp_eq_dest};
wenzelm@60757
    64
wenzelm@60757
    65
fun mk_in_mono_tac ctxt n =
wenzelm@60757
    66
  if n = 0 then rtac ctxt subset_UNIV 1
wenzelm@60757
    67
  else
wenzelm@63399
    68
   (rtac ctxt @{thm subsetI} THEN' rtac ctxt @{thm CollectI}) 1 THEN
wenzelm@63399
    69
   REPEAT_DETERM (eresolve_tac ctxt @{thms CollectE conjE} 1) THEN
wenzelm@60757
    70
   REPEAT_DETERM_N (n - 1)
wenzelm@63399
    71
     ((rtac ctxt conjI THEN' etac ctxt @{thm subset_trans} THEN' assume_tac ctxt) 1) THEN
wenzelm@63399
    72
   (etac ctxt @{thm subset_trans} THEN' assume_tac ctxt) 1;
blanchet@49284
    73
traytel@60728
    74
fun mk_inj_map_tac ctxt n map_id map_comp map_cong0 map_cong =
blanchet@56635
    75
  let
blanchet@56635
    76
    val map_cong' = map_cong OF (asm_rl :: replicate n refl);
blanchet@56635
    77
    val map_cong0' = map_cong0 OF (replicate n @{thm the_inv_f_o_f_id});
blanchet@56635
    78
  in
traytel@60728
    79
    HEADGOAL (rtac ctxt @{thm injI} THEN' etac ctxt (map_cong' RS box_equals) THEN'
traytel@60728
    80
      REPEAT_DETERM_N 2 o (rtac ctxt (box_equals OF [map_cong0', map_comp RS sym, map_id]) THEN'
wenzelm@60752
    81
        REPEAT_DETERM_N n o assume_tac ctxt))
blanchet@56635
    82
  end;
blanchet@56635
    83
desharna@57970
    84
fun mk_inj_map_strong_tac ctxt rel_eq rel_maps rel_mono_strong =
desharna@57970
    85
  let
desharna@57970
    86
    val rel_eq' = rel_eq RS @{thm predicate2_eqD};
desharna@57970
    87
    val rel_maps' = map (fn thm => thm RS iffD1) rel_maps;
desharna@57970
    88
  in
traytel@60728
    89
    HEADGOAL (dtac ctxt (rel_eq' RS iffD2) THEN' rtac ctxt (rel_eq' RS iffD1)) THEN
traytel@60728
    90
    EVERY (map (HEADGOAL o dtac ctxt) rel_maps') THEN
traytel@60728
    91
    HEADGOAL (etac ctxt rel_mono_strong) THEN
desharna@57970
    92
    TRYALL (Goal.assume_rule_tac ctxt)
desharna@57970
    93
  end;
desharna@57970
    94
traytel@60728
    95
fun mk_collect_set_map_tac ctxt set_map0s =
traytel@60728
    96
  (rtac ctxt (@{thm collect_comp} RS trans) THEN' rtac ctxt @{thm arg_cong[of _ _ collect]} THEN'
blanchet@53289
    97
  EVERY' (map (fn set_map0 =>
traytel@60728
    98
    rtac ctxt (mk_trans @{thm image_insert} @{thm arg_cong2[of _ _ _ _ insert]}) THEN'
traytel@60728
    99
    rtac ctxt set_map0) set_map0s) THEN'
traytel@60728
   100
  rtac ctxt @{thm image_empty}) 1;
blanchet@49284
   101
traytel@55197
   102
fun mk_rel_Grp_tac ctxt rel_OO_Grps map_id0 map_cong0 map_id map_comp set_maps =
blanchet@49284
   103
  let
traytel@54792
   104
    val n = length set_maps;
blanchet@61760
   105
    val rel_OO_Grps_tac =
blanchet@61760
   106
      if null rel_OO_Grps then K all_tac else rtac ctxt (hd rel_OO_Grps RS trans);
blanchet@49284
   107
  in
traytel@54792
   108
    if null set_maps then
blanchet@53270
   109
      unfold_thms_tac ctxt ((map_id0 RS @{thm Grp_UNIV_id}) :: rel_OO_Grps) THEN
blanchet@62617
   110
      resolve_tac ctxt @{thms refl Grp_UNIV_idI[OF refl]} 1
traytel@52844
   111
    else
traytel@60728
   112
      EVERY' [rel_OO_Grps_tac, rtac ctxt @{thm antisym}, rtac ctxt @{thm predicate2I},
wenzelm@63399
   113
        REPEAT_DETERM o eresolve_tac ctxt @{thms CollectE exE conjE GrpE relcomppE conversepE},
blanchet@61760
   114
        hyp_subst_tac ctxt, rtac ctxt @{thm GrpI}, rtac ctxt trans, rtac ctxt map_comp,
blanchet@61760
   115
          rtac ctxt map_cong0,
blanchet@61760
   116
        REPEAT_DETERM_N n o EVERY' [rtac ctxt @{thm Collect_case_prod_Grp_eqD},
blanchet@61760
   117
          etac ctxt @{thm set_mp}, assume_tac ctxt],
wenzelm@63399
   118
        rtac ctxt @{thm CollectI},
traytel@60728
   119
        CONJ_WRAP' (fn thm => EVERY' [rtac ctxt (thm RS ord_eq_le_trans),
haftmann@61424
   120
          rtac ctxt @{thm image_subsetI}, rtac ctxt @{thm Collect_case_prod_Grp_in},
wenzelm@60752
   121
          etac ctxt @{thm set_mp}, assume_tac ctxt])
traytel@54792
   122
        set_maps,
traytel@60728
   123
        rtac ctxt @{thm predicate2I}, REPEAT_DETERM o eresolve_tac ctxt [@{thm GrpE}, exE, conjE],
traytel@51893
   124
        hyp_subst_tac ctxt,
traytel@60728
   125
        rtac ctxt @{thm relcomppI}, rtac ctxt @{thm conversepI},
blanchet@53270
   126
        EVERY' (map2 (fn convol => fn map_id0 =>
traytel@60728
   127
          EVERY' [rtac ctxt @{thm GrpI},
traytel@60728
   128
            rtac ctxt (@{thm box_equals} OF [map_cong0, map_comp RS sym, map_id0]),
traytel@60728
   129
            REPEAT_DETERM_N n o rtac ctxt (convol RS fun_cong),
wenzelm@63399
   130
            REPEAT_DETERM o eresolve_tac ctxt @{thms CollectE conjE},
wenzelm@63399
   131
            rtac ctxt @{thm CollectI},
traytel@49495
   132
            CONJ_WRAP' (fn thm =>
traytel@60728
   133
              EVERY' [rtac ctxt ord_eq_le_trans, rtac ctxt thm, rtac ctxt @{thm image_subsetI},
wenzelm@60752
   134
                rtac ctxt @{thm convol_mem_GrpI}, etac ctxt set_mp, assume_tac ctxt])
traytel@54792
   135
            set_maps])
blanchet@53285
   136
          @{thms fst_convol snd_convol} [map_id, refl])] 1
blanchet@49284
   137
  end;
blanchet@49284
   138
traytel@60728
   139
fun mk_rel_eq_tac ctxt n rel_Grp rel_cong map_id0 =
traytel@60728
   140
  (EVERY' (rtac ctxt (rel_cong RS trans) :: replicate n (rtac ctxt @{thm eq_alt})) THEN'
traytel@60728
   141
  rtac ctxt (rel_Grp RSN (2, @{thm box_equals[OF _ sym sym[OF eq_alt]]})) THEN'
blanchet@62617
   142
  (if n = 0 then SELECT_GOAL (unfold_thms_tac ctxt (no_refl [map_id0])) THEN' rtac ctxt refl
traytel@60728
   143
  else EVERY' [rtac ctxt @{thm arg_cong2[of _ _ _ _ "Grp"]},
wenzelm@63399
   144
    rtac ctxt @{thm equalityI}, rtac ctxt subset_UNIV,
wenzelm@63399
   145
    rtac ctxt @{thm subsetI}, rtac ctxt @{thm CollectI},
traytel@60728
   146
    CONJ_WRAP' (K (rtac ctxt subset_UNIV)) (1 upto n), rtac ctxt map_id0])) 1;
blanchet@49284
   147
desharna@57932
   148
fun mk_rel_map0_tac ctxt live rel_compp rel_conversep rel_Grp map_id =
desharna@57932
   149
  if live = 0 then
desharna@57932
   150
    HEADGOAL (Goal.conjunction_tac) THEN
desharna@57932
   151
    unfold_thms_tac ctxt @{thms id_apply} THEN
traytel@60728
   152
    ALLGOALS (rtac ctxt refl)
desharna@57932
   153
  else
desharna@57932
   154
    let
desharna@57932
   155
      val ks = 1 upto live;
desharna@57932
   156
    in
desharna@57932
   157
      Goal.conjunction_tac 1 THEN
desharna@57932
   158
      unfold_thms_tac ctxt [rel_compp, rel_conversep, rel_Grp, @{thm vimage2p_Grp}] THEN
traytel@60728
   159
      TRYALL (EVERY' [rtac ctxt iffI, rtac ctxt @{thm relcomppI}, rtac ctxt @{thm GrpI},
wenzelm@63399
   160
        resolve_tac ctxt [map_id, refl], rtac ctxt @{thm CollectI},
blanchet@61760
   161
        CONJ_WRAP' (K (rtac ctxt @{thm subset_UNIV})) ks, rtac ctxt @{thm relcomppI},
blanchet@61760
   162
        assume_tac ctxt, rtac ctxt @{thm conversepI}, rtac ctxt @{thm GrpI},
wenzelm@63399
   163
        resolve_tac ctxt [map_id, refl], rtac ctxt @{thm CollectI},
traytel@60728
   164
        CONJ_WRAP' (K (rtac ctxt @{thm subset_UNIV})) ks,
wenzelm@59498
   165
        REPEAT_DETERM o eresolve_tac ctxt @{thms relcomppE conversepE GrpE},
wenzelm@60752
   166
        dtac ctxt (trans OF [sym, map_id]), hyp_subst_tac ctxt, assume_tac ctxt])
desharna@57932
   167
    end;
desharna@57932
   168
traytel@60728
   169
fun mk_rel_mono_tac ctxt rel_OO_Grps in_mono =
traytel@52844
   170
  let
traytel@52844
   171
    val rel_OO_Grps_tac = if null rel_OO_Grps then K all_tac
traytel@60728
   172
      else rtac ctxt (hd rel_OO_Grps RS ord_eq_le_trans) THEN'
traytel@60728
   173
        rtac ctxt (hd rel_OO_Grps RS sym RSN (2, ord_le_eq_trans));
traytel@52844
   174
  in
traytel@60728
   175
    EVERY' [rel_OO_Grps_tac, rtac ctxt @{thm relcompp_mono}, rtac ctxt @{thm iffD2[OF conversep_mono]},
haftmann@61424
   176
      rtac ctxt @{thm Grp_mono}, rtac ctxt in_mono, REPEAT_DETERM o etac ctxt @{thm Collect_case_prod_mono},
haftmann@61424
   177
      rtac ctxt @{thm Grp_mono}, rtac ctxt in_mono, REPEAT_DETERM o etac ctxt @{thm Collect_case_prod_mono}] 1
traytel@52844
   178
  end;
blanchet@49284
   179
traytel@55197
   180
fun mk_rel_conversep_le_tac ctxt rel_OO_Grps rel_eq map_cong0 map_comp set_maps =
blanchet@49284
   181
  let
traytel@54792
   182
    val n = length set_maps;
traytel@52844
   183
    val rel_OO_Grps_tac = if null rel_OO_Grps then K all_tac
traytel@60728
   184
      else rtac ctxt (hd rel_OO_Grps RS ord_eq_le_trans) THEN'
traytel@60728
   185
        rtac ctxt (hd rel_OO_Grps RS sym RS @{thm arg_cong[of _ _ conversep]} RSN (2, ord_le_eq_trans));
blanchet@49284
   186
  in
traytel@60728
   187
    if null set_maps then rtac ctxt (rel_eq RS @{thm leq_conversepI}) 1
traytel@52844
   188
    else
traytel@60728
   189
      EVERY' [rel_OO_Grps_tac, rtac ctxt @{thm predicate2I},
traytel@51893
   190
        REPEAT_DETERM o
wenzelm@63399
   191
          eresolve_tac ctxt @{thms CollectE exE conjE GrpE relcomppE conversepE},
traytel@60728
   192
        hyp_subst_tac ctxt, rtac ctxt @{thm conversepI}, rtac ctxt @{thm relcomppI}, rtac ctxt @{thm conversepI},
traytel@60728
   193
        EVERY' (map (fn thm => EVERY' [rtac ctxt @{thm GrpI}, rtac ctxt sym, rtac ctxt trans,
traytel@60728
   194
          rtac ctxt map_cong0, REPEAT_DETERM_N n o rtac ctxt thm,
wenzelm@63399
   195
          rtac ctxt (map_comp RS sym), rtac ctxt @{thm CollectI},
traytel@60728
   196
          CONJ_WRAP' (fn thm => EVERY' [rtac ctxt (thm RS ord_eq_le_trans),
traytel@60728
   197
            etac ctxt @{thm flip_pred}]) set_maps]) [@{thm snd_fst_flip}, @{thm fst_snd_flip}])] 1
blanchet@49284
   198
  end;
blanchet@49284
   199
traytel@60728
   200
fun mk_rel_conversep_tac ctxt le_conversep rel_mono =
traytel@60728
   201
  EVERY' [rtac ctxt @{thm antisym}, rtac ctxt le_conversep, rtac ctxt @{thm xt1(6)}, rtac ctxt conversep_shift,
traytel@60728
   202
    rtac ctxt le_conversep, rtac ctxt @{thm iffD2[OF conversep_mono]}, rtac ctxt rel_mono,
traytel@60728
   203
    REPEAT_DETERM o rtac ctxt @{thm eq_refl[OF sym[OF conversep_conversep]]}] 1;
blanchet@49284
   204
traytel@55197
   205
fun mk_rel_OO_le_tac ctxt rel_OO_Grps rel_eq map_cong0 map_comp set_maps =
blanchet@49284
   206
  let
traytel@54792
   207
    val n = length set_maps;
wenzelm@63399
   208
    fun in_tac nthO_in = rtac ctxt @{thm CollectI} THEN'
traytel@60728
   209
        CONJ_WRAP' (fn thm => EVERY' [rtac ctxt (thm RS ord_eq_le_trans),
wenzelm@60752
   210
          rtac ctxt @{thm image_subsetI}, rtac ctxt nthO_in, etac ctxt set_mp, assume_tac ctxt]) set_maps;
traytel@52844
   211
    val rel_OO_Grps_tac = if null rel_OO_Grps then K all_tac
traytel@60728
   212
      else rtac ctxt (hd rel_OO_Grps RS ord_eq_le_trans) THEN'
traytel@60728
   213
        rtac ctxt (@{thm arg_cong2[of _ _ _ _ "op OO"]} OF (replicate 2 (hd rel_OO_Grps RS sym)) RSN
traytel@54841
   214
          (2, ord_le_eq_trans));
blanchet@49284
   215
  in
traytel@60728
   216
    if null set_maps then rtac ctxt (rel_eq RS @{thm leq_OOI}) 1
traytel@52844
   217
    else
traytel@60728
   218
      EVERY' [rel_OO_Grps_tac, rtac ctxt @{thm predicate2I},
wenzelm@63399
   219
        REPEAT_DETERM o eresolve_tac ctxt @{thms CollectE exE conjE GrpE relcomppE conversepE},
traytel@51893
   220
        hyp_subst_tac ctxt,
traytel@60728
   221
        rtac ctxt @{thm relcomppI}, rtac ctxt @{thm relcomppI}, rtac ctxt @{thm conversepI}, rtac ctxt @{thm GrpI},
traytel@60728
   222
        rtac ctxt trans, rtac ctxt map_comp, rtac ctxt sym, rtac ctxt map_cong0,
traytel@60728
   223
        REPEAT_DETERM_N n o rtac ctxt @{thm fst_fstOp},
traytel@51893
   224
        in_tac @{thm fstOp_in},
traytel@60728
   225
        rtac ctxt @{thm GrpI}, rtac ctxt trans, rtac ctxt map_comp, rtac ctxt map_cong0,
traytel@60728
   226
        REPEAT_DETERM_N n o EVERY' [rtac ctxt trans, rtac ctxt o_apply,
wenzelm@63399
   227
          rtac ctxt @{thm ballE}, rtac ctxt subst,
wenzelm@60752
   228
          rtac ctxt @{thm csquare_def}, rtac ctxt @{thm csquare_fstOp_sndOp}, assume_tac ctxt,
wenzelm@60752
   229
          etac ctxt notE, etac ctxt set_mp, assume_tac ctxt],
traytel@51893
   230
        in_tac @{thm fstOp_in},
traytel@60728
   231
        rtac ctxt @{thm relcomppI}, rtac ctxt @{thm conversepI}, rtac ctxt @{thm GrpI},
traytel@60728
   232
        rtac ctxt trans, rtac ctxt map_comp, rtac ctxt map_cong0,
traytel@60728
   233
        REPEAT_DETERM_N n o rtac ctxt o_apply,
traytel@51893
   234
        in_tac @{thm sndOp_in},
traytel@60728
   235
        rtac ctxt @{thm GrpI}, rtac ctxt trans, rtac ctxt map_comp, rtac ctxt sym, rtac ctxt map_cong0,
traytel@60728
   236
        REPEAT_DETERM_N n o rtac ctxt @{thm snd_sndOp},
traytel@54841
   237
        in_tac @{thm sndOp_in}] 1
blanchet@49284
   238
  end;
blanchet@49284
   239
desharna@57967
   240
fun mk_rel_mono_strong0_tac ctxt in_rel set_maps =
wenzelm@60752
   241
  if null set_maps then assume_tac ctxt 1
traytel@51916
   242
  else
wenzelm@54998
   243
    unfold_tac ctxt [in_rel] THEN
wenzelm@63399
   244
    REPEAT_DETERM (eresolve_tac ctxt @{thms exE CollectE conjE} 1) THEN
traytel@51916
   245
    hyp_subst_tac ctxt 1 THEN
traytel@60728
   246
    EVERY' [rtac ctxt exI, rtac ctxt @{thm conjI[OF CollectI conjI[OF refl refl]]},
traytel@55163
   247
      CONJ_WRAP' (fn thm =>
wenzelm@60752
   248
        (etac ctxt (@{thm Collect_split_mono_strong} OF [thm, thm]) THEN' assume_tac ctxt))
traytel@55163
   249
      set_maps] 1;
traytel@51916
   250
desharna@58104
   251
fun mk_rel_transfer_tac ctxt in_rel rel_map rel_mono_strong =
desharna@58104
   252
  let
desharna@58104
   253
    fun last_tac iffD =
traytel@60728
   254
      HEADGOAL (etac ctxt rel_mono_strong) THEN
traytel@60728
   255
      REPEAT_DETERM (HEADGOAL (etac ctxt (@{thm predicate2_transferD} RS iffD) THEN'
wenzelm@60752
   256
        REPEAT_DETERM o assume_tac ctxt));
desharna@58104
   257
  in
traytel@60728
   258
    REPEAT_DETERM (HEADGOAL (rtac ctxt rel_funI)) THEN
traytel@60728
   259
    (HEADGOAL (hyp_subst_tac ctxt THEN' rtac ctxt refl) ORELSE
wenzelm@59498
   260
     REPEAT_DETERM (HEADGOAL (eresolve_tac ctxt (Tactic.make_elim (in_rel RS iffD1) ::
desharna@58104
   261
       @{thms exE conjE CollectE}))) THEN
traytel@67222
   262
     HEADGOAL (hyp_subst_tac ctxt) THEN
traytel@67222
   263
     REPEAT_DETERM (HEADGOAL (resolve_tac ctxt (maps (fn thm =>
traytel@67222
   264
       [thm RS trans, thm RS @{thm trans[rotated, OF sym]}]) rel_map))) THEN
traytel@67222
   265
     HEADGOAL (rtac ctxt iffI) THEN
traytel@67222
   266
     last_tac iffD1 THEN  print_tac ctxt "baz" THEN last_tac iffD2)
desharna@58104
   267
  end;
desharna@58104
   268
traytel@55197
   269
fun mk_map_transfer_tac ctxt rel_mono in_rel set_maps map_cong0 map_comp =
traytel@52719
   270
  let
blanchet@53290
   271
    val n = length set_maps;
wenzelm@63399
   272
    val in_tac =
wenzelm@63399
   273
      if n = 0 then rtac ctxt @{thm UNIV_I}
wenzelm@63399
   274
      else
wenzelm@63399
   275
        rtac ctxt @{thm CollectI} THEN' CONJ_WRAP' (fn thm =>
wenzelm@63399
   276
          etac ctxt (thm RS
wenzelm@63399
   277
            @{thm ord_eq_le_trans[OF _ subset_trans[OF image_mono convol_image_vimage2p]]}))
wenzelm@63399
   278
        set_maps;
traytel@52719
   279
  in
traytel@60728
   280
    REPEAT_DETERM_N n (HEADGOAL (rtac ctxt rel_funI)) THEN
blanchet@55945
   281
    unfold_thms_tac ctxt @{thms rel_fun_iff_leq_vimage2p} THEN
wenzelm@60752
   282
    HEADGOAL (EVERY' [rtac ctxt @{thm order_trans}, rtac ctxt rel_mono,
wenzelm@60752
   283
      REPEAT_DETERM_N n o assume_tac ctxt,
traytel@60728
   284
      rtac ctxt @{thm predicate2I}, dtac ctxt (in_rel RS iffD1),
wenzelm@63399
   285
      REPEAT_DETERM o eresolve_tac ctxt @{thms exE CollectE conjE}, hyp_subst_tac ctxt,
traytel@60728
   286
      rtac ctxt @{thm vimage2pI}, rtac ctxt (in_rel RS iffD2), rtac ctxt exI, rtac ctxt conjI, in_tac,
traytel@60728
   287
      rtac ctxt conjI,
traytel@52719
   288
      EVERY' (map (fn convol =>
traytel@60728
   289
        rtac ctxt (@{thm box_equals} OF [map_cong0, map_comp RS sym, map_comp RS sym]) THEN'
traytel@60728
   290
        REPEAT_DETERM_N n o rtac ctxt (convol RS fun_cong)) @{thms fst_convol snd_convol})])
traytel@52719
   291
  end;
traytel@52719
   292
traytel@55197
   293
fun mk_in_bd_tac ctxt live surj_imp_ordLeq_inst map_comp map_id map_cong0 set_maps set_bds
traytel@55197
   294
  bd_card_order bd_Card_order bd_Cinfinite bd_Cnotzero =
traytel@52635
   295
  if live = 0 then
traytel@60728
   296
    rtac ctxt @{thm ordLeq_transitive[OF ordLeq_csum2[OF card_of_Card_order]
traytel@52635
   297
      ordLeq_cexp2[OF ordLeq_refl[OF Card_order_ctwo] Card_order_csum]]} 1
traytel@52635
   298
  else
traytel@52635
   299
    let
traytel@52635
   300
      val bd'_Cinfinite = bd_Cinfinite RS @{thm Cinfinite_csum1};
traytel@52635
   301
      val inserts =
desharna@57970
   302
        map (fn set_bd =>
traytel@52635
   303
          iffD2 OF [@{thm card_of_ordLeq}, @{thm ordLeq_ordIso_trans} OF
traytel@52635
   304
            [set_bd, bd_Card_order RS @{thm card_of_Field_ordIso} RS @{thm ordIso_symmetric}]])
desharna@57970
   305
        set_bds;
traytel@52635
   306
    in
traytel@60728
   307
      EVERY' [rtac ctxt (Drule.rotate_prems 1 ctrans), rtac ctxt @{thm cprod_cinfinite_bound},
traytel@60728
   308
        rtac ctxt (ctrans OF @{thms ordLeq_csum2 ordLeq_cexp2}), rtac ctxt @{thm card_of_Card_order},
traytel@60728
   309
        rtac ctxt @{thm ordLeq_csum2}, rtac ctxt @{thm Card_order_ctwo}, rtac ctxt @{thm Card_order_csum},
traytel@60728
   310
        rtac ctxt @{thm ordIso_ordLeq_trans}, rtac ctxt @{thm cexp_cong1},
traytel@60728
   311
        if live = 1 then rtac ctxt @{thm ordIso_refl[OF Card_order_csum]}
traytel@52635
   312
        else
traytel@60728
   313
          REPEAT_DETERM_N (live - 2) o rtac ctxt @{thm ordIso_transitive[OF csum_cong2]} THEN'
traytel@60728
   314
          REPEAT_DETERM_N (live - 1) o rtac ctxt @{thm csum_csum},
traytel@60728
   315
        rtac ctxt bd_Card_order, rtac ctxt (@{thm cexp_mono2_Cnotzero} RS ctrans), rtac ctxt @{thm ordLeq_csum1},
traytel@60728
   316
        rtac ctxt bd_Card_order, rtac ctxt @{thm Card_order_csum}, rtac ctxt bd_Cnotzero,
traytel@60728
   317
        rtac ctxt @{thm csum_Cfinite_cexp_Cinfinite},
traytel@60728
   318
        rtac ctxt (if live = 1 then @{thm card_of_Card_order} else @{thm Card_order_csum}),
traytel@60728
   319
        CONJ_WRAP_GEN' (rtac ctxt @{thm Cfinite_csum}) (K (rtac ctxt @{thm Cfinite_cone})) set_maps,
traytel@60728
   320
        rtac ctxt bd'_Cinfinite, rtac ctxt @{thm card_of_Card_order},
traytel@60728
   321
        rtac ctxt @{thm Card_order_cexp}, rtac ctxt @{thm Cinfinite_cexp}, rtac ctxt @{thm ordLeq_csum2},
traytel@60728
   322
        rtac ctxt @{thm Card_order_ctwo}, rtac ctxt bd'_Cinfinite,
traytel@60728
   323
        rtac ctxt (Drule.rotate_prems 1 (@{thm cprod_mono2} RSN (2, ctrans))),
traytel@52635
   324
        REPEAT_DETERM_N (live - 1) o
traytel@60728
   325
          (rtac ctxt (bd_Cinfinite RS @{thm cprod_cexp_csum_cexp_Cinfinite} RSN (2, ctrans)) THEN'
traytel@60728
   326
           rtac ctxt @{thm ordLeq_ordIso_trans[OF cprod_mono2 ordIso_symmetric[OF cprod_cexp]]}),
traytel@60728
   327
        rtac ctxt @{thm ordLeq_refl[OF Card_order_cexp]}] 1 THEN
traytel@52635
   328
      unfold_thms_tac ctxt [bd_card_order RS @{thm card_order_csum_cone_cexp_def}] THEN
traytel@52635
   329
      unfold_thms_tac ctxt @{thms cprod_def Field_card_of} THEN
wenzelm@63399
   330
      EVERY' [rtac ctxt (Drule.rotate_prems 1 ctrans), rtac ctxt surj_imp_ordLeq_inst,
wenzelm@63399
   331
        rtac ctxt @{thm subsetI},
wenzelm@61841
   332
        Method.insert_tac ctxt inserts, REPEAT_DETERM o dtac ctxt meta_spec,
wenzelm@63399
   333
        REPEAT_DETERM o eresolve_tac ctxt [exE, Tactic.make_elim conjunct1],
wenzelm@63399
   334
        etac ctxt @{thm CollectE},
traytel@52635
   335
        if live = 1 then K all_tac
traytel@60728
   336
        else REPEAT_DETERM_N (live - 2) o (etac ctxt conjE THEN' rotate_tac ~1) THEN' etac ctxt conjE,
traytel@60728
   337
        rtac ctxt (Drule.rotate_prems 1 @{thm image_eqI}), rtac ctxt @{thm SigmaI}, rtac ctxt @{thm UNIV_I},
traytel@60728
   338
        CONJ_WRAP_GEN' (rtac ctxt @{thm SigmaI})
wenzelm@60752
   339
          (K (etac ctxt @{thm If_the_inv_into_in_Func} THEN' assume_tac ctxt)) set_maps,
traytel@60728
   340
        rtac ctxt sym,
traytel@60728
   341
        rtac ctxt (Drule.rotate_prems 1
wenzelm@55990
   342
           ((@{thm box_equals} OF [map_cong0 OF replicate live @{thm If_the_inv_into_f_f},
blanchet@53288
   343
             map_comp RS sym, map_id]) RSN (2, trans))),
wenzelm@60752
   344
        REPEAT_DETERM_N (2 * live) o assume_tac ctxt,
traytel@60728
   345
        REPEAT_DETERM_N live o rtac ctxt (@{thm prod.case} RS trans),
traytel@60728
   346
        rtac ctxt refl,
wenzelm@63399
   347
        rtac ctxt @{thm surj_imp_ordLeq},
wenzelm@63399
   348
        rtac ctxt @{thm subsetI},
wenzelm@63399
   349
        rtac ctxt (Drule.rotate_prems 1 @{thm image_eqI}),
wenzelm@63399
   350
        REPEAT_DETERM o eresolve_tac ctxt @{thms CollectE conjE}, rtac ctxt @{thm CollectI},
traytel@52635
   351
        CONJ_WRAP' (fn thm =>
traytel@60728
   352
          rtac ctxt (thm RS ord_eq_le_trans) THEN' etac ctxt @{thm subset_trans[OF image_mono Un_upper1]})
blanchet@53290
   353
        set_maps,
traytel@60728
   354
        rtac ctxt sym,
traytel@60728
   355
        rtac ctxt (@{thm box_equals} OF [map_cong0 OF replicate live @{thm fun_cong[OF case_sum_o_inj(1)]},
blanchet@53288
   356
           map_comp RS sym, map_id])] 1
traytel@52635
   357
  end;
traytel@51916
   358
traytel@55197
   359
fun mk_trivial_wit_tac ctxt wit_defs set_maps =
wenzelm@60752
   360
  unfold_thms_tac ctxt wit_defs THEN
wenzelm@60752
   361
  HEADGOAL (EVERY' (map (fn thm =>
wenzelm@63399
   362
    dtac ctxt (thm RS @{thm equalityD1} RS set_mp) THEN'
wenzelm@63399
   363
    etac ctxt @{thm imageE} THEN' assume_tac ctxt) set_maps)) THEN
wenzelm@60752
   364
  ALLGOALS (assume_tac ctxt);
traytel@54189
   365
desharna@58106
   366
fun mk_set_transfer_tac ctxt in_rel set_maps =
desharna@58106
   367
  Goal.conjunction_tac 1 THEN
traytel@60728
   368
  EVERY (map (fn set_map => HEADGOAL (rtac ctxt rel_funI) THEN
wenzelm@59498
   369
  REPEAT_DETERM (HEADGOAL (eresolve_tac ctxt (Tactic.make_elim (in_rel RS iffD1) ::
desharna@58106
   370
    @{thms exE conjE CollectE}))) THEN
traytel@60728
   371
  HEADGOAL (hyp_subst_tac ctxt THEN' rtac ctxt (@{thm iffD2[OF arg_cong2]} OF [set_map, set_map]) THEN'
traytel@60728
   372
    rtac ctxt @{thm rel_setI}) THEN
wenzelm@63399
   373
  REPEAT (HEADGOAL (etac ctxt @{thm imageE} THEN' dtac ctxt @{thm set_mp} THEN' assume_tac ctxt THEN'
wenzelm@59498
   374
    REPEAT_DETERM o (eresolve_tac ctxt @{thms CollectE case_prodE}) THEN' hyp_subst_tac ctxt THEN'
wenzelm@63399
   375
    rtac ctxt @{thm bexI} THEN' etac ctxt @{thm subst_Pair[OF _ refl]} THEN' etac ctxt @{thm imageI})))
wenzelm@63399
   376
    set_maps);
desharna@58106
   377
traytel@61242
   378
fun mk_rel_cong_tac ctxt (eqs, prems) mono =
traytel@61242
   379
  let
traytel@61242
   380
    fun mk_tac thm = etac ctxt thm THEN_ALL_NEW assume_tac ctxt;
traytel@62324
   381
    fun mk_tacs iffD = etac ctxt mono ::
traytel@61242
   382
      map (fn thm => (unfold_thms ctxt @{thms simp_implies_def} thm RS iffD)
traytel@61242
   383
        |> Drule.rotate_prems ~1 |> mk_tac) prems;
traytel@61242
   384
  in
traytel@61242
   385
    unfold_thms_tac ctxt eqs THEN
traytel@61242
   386
    HEADGOAL (EVERY' (rtac ctxt iffI :: mk_tacs iffD1 @ mk_tacs iffD2))
traytel@61242
   387
  end;
traytel@61242
   388
traytel@62324
   389
fun subst_conv ctxt thm =
traytel@62324
   390
  Conv.arg_conv (Conv.arg_conv
traytel@62324
   391
   (Conv.top_sweep_conv (K (Conv.rewr_conv (safe_mk_meta_eq thm))) ctxt));
traytel@62324
   392
traytel@62324
   393
fun mk_rel_eq_onp_tac ctxt pred_def map_id0 rel_Grp =
traytel@62324
   394
  HEADGOAL (EVERY'
traytel@62324
   395
   [SELECT_GOAL (unfold_thms_tac ctxt (pred_def :: @{thms UNIV_def eq_onp_Grp Ball_Collect})),
traytel@62324
   396
   CONVERSION (subst_conv ctxt (map_id0 RS sym)),
traytel@62324
   397
   rtac ctxt (unfold_thms ctxt @{thms UNIV_def} rel_Grp)]);
traytel@62324
   398
traytel@62324
   399
fun mk_pred_mono_strong0_tac ctxt pred_rel rel_mono_strong0 =
traytel@62324
   400
   unfold_thms_tac ctxt [pred_rel] THEN
traytel@62324
   401
   HEADGOAL (etac ctxt rel_mono_strong0 THEN_ALL_NEW etac ctxt @{thm eq_onp_mono0});
traytel@62324
   402
traytel@63714
   403
fun mk_pred_mono_tac ctxt rel_eq_onp rel_mono =
traytel@63714
   404
  unfold_thms_tac ctxt (map mk_sym [@{thm eq_onp_mono_iff}, rel_eq_onp]) THEN
traytel@63714
   405
  HEADGOAL (rtac ctxt rel_mono THEN_ALL_NEW assume_tac ctxt);
traytel@63714
   406
traytel@62329
   407
fun mk_pred_transfer_tac ctxt n in_rel pred_map pred_cong =
traytel@62329
   408
  HEADGOAL (EVERY' [REPEAT_DETERM_N (n + 1) o rtac ctxt rel_funI, dtac ctxt (in_rel RS iffD1),
wenzelm@63399
   409
    REPEAT_DETERM o eresolve_tac ctxt @{thms exE conjE CollectE}, hyp_subst_tac ctxt,
traytel@62329
   410
    rtac ctxt (box_equals OF [@{thm _}, pred_map RS sym, pred_map RS sym]),
traytel@62329
   411
    rtac ctxt (refl RS pred_cong), REPEAT_DETERM_N n o
traytel@62329
   412
      (etac ctxt @{thm rel_fun_Collect_case_prodD[where B="op ="]} THEN_ALL_NEW assume_tac ctxt)]);
traytel@62329
   413
blanchet@49284
   414
end;