src/HOL/Tools/BNF/bnf_fp_def_sugar_tactics.ML
author wenzelm
Fri Mar 21 20:33:56 2014 +0100 (2014-03-21)
changeset 56245 84fc7dfa3cd4
parent 55966 972f0aa7091b
child 56765 644f0d4820a1
permissions -rw-r--r--
more qualified names;
blanchet@55061
     1
(*  Title:      HOL/Tools/BNF/bnf_fp_def_sugar_tactics.ML
blanchet@49123
     2
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@49123
     3
    Copyright   2012
blanchet@49123
     4
blanchet@49389
     5
Tactics for datatype and codatatype sugar.
blanchet@49123
     6
*)
blanchet@49123
     7
blanchet@49636
     8
signature BNF_FP_DEF_SUGAR_TACTICS =
blanchet@49123
     9
sig
blanchet@55966
    10
  val sumprod_thms_map: thm list
blanchet@55966
    11
  val sumprod_thms_set: thm list
blanchet@55966
    12
  val sumprod_thms_rel: thm list
blanchet@49585
    13
blanchet@49591
    14
  val mk_coinduct_tac: Proof.context -> thm list -> int -> int list -> thm -> thm list ->
traytel@55803
    15
    thm list -> thm list -> thm list -> thm list -> thm list list -> thm list list list ->
traytel@55803
    16
    thm list list list -> tactic
blanchet@55867
    17
  val mk_corec_tac: thm list -> thm list -> thm -> thm -> thm -> thm -> Proof.context -> tactic
blanchet@49501
    18
  val mk_ctor_iff_dtor_tac: Proof.context -> ctyp option list -> cterm -> cterm -> thm -> thm ->
blanchet@49501
    19
    tactic
blanchet@55867
    20
  val mk_disc_corec_iff_tac: thm list -> thm list -> thm list -> Proof.context -> tactic
blanchet@49161
    21
  val mk_exhaust_tac: Proof.context -> int -> thm list -> thm -> thm -> tactic
traytel@55803
    22
  val mk_half_distinct_tac: Proof.context -> thm -> thm -> thm list -> tactic
blanchet@49590
    23
  val mk_induct_tac: Proof.context -> int -> int list -> int list list -> int list list list ->
traytel@55803
    24
    thm list -> thm -> thm list -> thm list -> thm list -> thm list list -> tactic
traytel@55803
    25
  val mk_inject_tac: Proof.context -> thm -> thm -> thm -> tactic
blanchet@55867
    26
  val mk_rec_tac: thm list -> thm list -> thm list -> thm -> thm -> thm -> thm -> Proof.context ->
traytel@55803
    27
    tactic
blanchet@49123
    28
end;
blanchet@49123
    29
blanchet@49636
    30
structure BNF_FP_Def_Sugar_Tactics : BNF_FP_DEF_SUGAR_TACTICS =
blanchet@49123
    31
struct
blanchet@49123
    32
blanchet@49125
    33
open BNF_Tactics
blanchet@49123
    34
open BNF_Util
blanchet@51850
    35
open BNF_FP_Util
blanchet@49123
    36
blanchet@49590
    37
val basic_simp_thms = @{thms simp_thms(7,8,12,14,22,24)};
blanchet@49590
    38
val more_simp_thms = basic_simp_thms @ @{thms simp_thms(11,15,16,21)};
blanchet@49590
    39
blanchet@55966
    40
val sumprod_thms_map = @{thms id_apply map_prod_simp prod.case sum.case map_sum.simps};
blanchet@55966
    41
val sumprod_thms_set =
traytel@55930
    42
  @{thms UN_empty UN_insert Un_empty_left Un_empty_right Un_iff UN_simps(10) UN_iff
blanchet@55932
    43
      Union_Un_distrib image_iff o_apply map_prod_simp
blanchet@55931
    44
      mem_Collect_eq prod_set_simps map_sum.simps sum_set_simps};
blanchet@55966
    45
val sumprod_thms_rel = @{thms rel_prod_apply rel_sum_simps id_apply};
blanchet@49368
    46
blanchet@49668
    47
fun hhf_concl_conv cv ctxt ct =
blanchet@49668
    48
  (case Thm.term_of ct of
wenzelm@56245
    49
    Const (@{const_name Pure.all}, _) $ Abs _ =>
blanchet@49668
    50
    Conv.arg_conv (Conv.abs_conv (hhf_concl_conv cv o snd) ctxt) ct
blanchet@49668
    51
  | _ => Conv.concl_conv ~1 cv ct);
blanchet@49668
    52
blanchet@54922
    53
fun co_induct_inst_as_projs ctxt k thm =
blanchet@49368
    54
  let
blanchet@54922
    55
    val fs = Term.add_vars (prop_of thm) []
blanchet@49368
    56
      |> filter (fn (_, Type (@{type_name fun}, [_, T'])) => T' <> HOLogic.boolT | _ => false);
blanchet@54923
    57
    fun mk_cfp (f as (_, T)) =
blanchet@54923
    58
      (certify ctxt (Var f), certify ctxt (mk_proj T (num_binder_types T) k));
blanchet@54923
    59
    val cfps = map mk_cfp fs;
blanchet@49368
    60
  in
blanchet@54923
    61
    Drule.cterm_instantiate cfps thm
blanchet@49368
    62
  end;
blanchet@49368
    63
blanchet@54922
    64
val co_induct_inst_as_projs_tac = PRIMITIVE oo co_induct_inst_as_projs;
blanchet@49368
    65
blanchet@49501
    66
fun mk_exhaust_tac ctxt n ctr_defs ctor_iff_dtor sumEN' =
blanchet@52324
    67
  unfold_thms_tac ctxt (ctor_iff_dtor :: ctr_defs) THEN HEADGOAL (rtac sumEN') THEN
blanchet@52324
    68
  HEADGOAL (EVERY' (maps (fn k => [select_prem_tac n (rotate_tac 1) k,
blanchet@52324
    69
    REPEAT_DETERM o dtac meta_spec, etac meta_mp, atac]) (1 upto n)));
blanchet@49125
    70
blanchet@49501
    71
fun mk_ctor_iff_dtor_tac ctxt cTs cctor cdtor ctor_dtor dtor_ctor =
blanchet@52324
    72
  HEADGOAL (rtac iffI THEN'
blanchet@52324
    73
    EVERY' (map3 (fn cTs => fn cx => fn th =>
blanchet@52324
    74
      dtac (Drule.instantiate' cTs [NONE, NONE, SOME cx] arg_cong) THEN'
blanchet@52324
    75
      SELECT_GOAL (unfold_thms_tac ctxt [th]) THEN'
blanchet@52324
    76
      atac) [rev cTs, cTs] [cdtor, cctor] [dtor_ctor, ctor_dtor]));
blanchet@49123
    77
traytel@55803
    78
fun mk_half_distinct_tac ctxt ctor_inject abs_inject ctr_defs =
traytel@55803
    79
  unfold_thms_tac ctxt (ctor_inject :: abs_inject :: @{thms sum.inject} @ ctr_defs) THEN
blanchet@52324
    80
  HEADGOAL (rtac @{thm sum.distinct(1)});
blanchet@49127
    81
traytel@55803
    82
fun mk_inject_tac ctxt ctr_def ctor_inject abs_inject =
traytel@55803
    83
  unfold_thms_tac ctxt [ctr_def] THEN
traytel@55803
    84
  HEADGOAL (rtac (ctor_inject RS ssubst)) THEN
traytel@55803
    85
  unfold_thms_tac ctxt (abs_inject :: @{thms sum.inject Pair_eq conj_assoc}) THEN
traytel@55803
    86
  HEADGOAL (rtac refl);
blanchet@49126
    87
blanchet@55867
    88
val rec_unfold_thms =
blanchet@55414
    89
  @{thms comp_def convol_def fst_conv id_def case_prod_Pair_iden snd_conv split_conv
blanchet@55966
    90
      case_unit_Unity} @ sumprod_thms_map;
blanchet@49205
    91
blanchet@55867
    92
fun mk_rec_tac pre_map_defs map_idents rec_defs ctor_rec fp_abs_inverse abs_inverse ctr_def ctxt =
blanchet@55867
    93
  unfold_thms_tac ctxt (ctr_def :: ctor_rec :: fp_abs_inverse :: abs_inverse :: rec_defs @
blanchet@55867
    94
    pre_map_defs @ map_idents @ rec_unfold_thms) THEN HEADGOAL (rtac refl);
blanchet@49205
    95
blanchet@55966
    96
val corec_unfold_thms = @{thms id_def} @ sumprod_thms_map;
blanchet@49683
    97
blanchet@55867
    98
fun mk_corec_tac corec_defs map_idents ctor_dtor_corec pre_map_def abs_inverse ctr_def ctxt =
traytel@55803
    99
  let
blanchet@55867
   100
    val ss = ss_only (pre_map_def :: abs_inverse :: map_idents @ corec_unfold_thms @
traytel@55803
   101
      @{thms o_apply vimage2p_def if_True if_False}) ctxt;
traytel@55803
   102
  in
blanchet@55867
   103
    unfold_thms_tac ctxt (ctr_def :: corec_defs) THEN
blanchet@55867
   104
    HEADGOAL (rtac (ctor_dtor_corec RS trans) THEN' asm_simp_tac ss) THEN_MAYBE
traytel@55803
   105
    HEADGOAL (rtac refl ORELSE' rtac (@{thm unit_eq} RS arg_cong))
traytel@55803
   106
  end;
blanchet@49213
   107
blanchet@55867
   108
fun mk_disc_corec_iff_tac case_splits' corecs discs ctxt =
blanchet@55867
   109
  EVERY (map3 (fn case_split_tac => fn corec_thm => fn disc =>
blanchet@55867
   110
      HEADGOAL case_split_tac THEN unfold_thms_tac ctxt [corec_thm] THEN
blanchet@52324
   111
      HEADGOAL (asm_simp_tac (ss_only basic_simp_thms ctxt)) THEN
blanchet@52324
   112
      (if is_refl disc then all_tac else HEADGOAL (rtac disc)))
blanchet@55867
   113
    (map rtac case_splits' @ [K all_tac]) corecs discs);
blanchet@49482
   114
wenzelm@51798
   115
fun solve_prem_prem_tac ctxt =
blanchet@49429
   116
  REPEAT o (eresolve_tac @{thms bexE rev_bexI} ORELSE' rtac @{thm rev_bexI[OF UNIV_I]} ORELSE'
wenzelm@51798
   117
    hyp_subst_tac ctxt ORELSE' resolve_tac @{thms disjI1 disjI2}) THEN'
blanchet@49429
   118
  (rtac refl ORELSE' atac ORELSE' rtac @{thm singletonI});
blanchet@49426
   119
traytel@55803
   120
fun mk_induct_leverage_prem_prems_tac ctxt nn kks fp_abs_inverses abs_inverses set_maps
traytel@55803
   121
    pre_set_defs =
blanchet@52324
   122
  HEADGOAL (EVERY' (maps (fn kk => [select_prem_tac nn (dtac meta_spec) kk, etac meta_mp,
traytel@55803
   123
    SELECT_GOAL (unfold_thms_tac ctxt (pre_set_defs @ fp_abs_inverses @ abs_inverses @ set_maps @
blanchet@55966
   124
      sumprod_thms_set)),
blanchet@52324
   125
    solve_prem_prem_tac ctxt]) (rev kks)));
blanchet@49368
   126
traytel@55803
   127
fun mk_induct_discharge_prem_tac ctxt nn n fp_abs_inverses abs_inverses set_maps pre_set_defs m k
traytel@55803
   128
    kks =
blanchet@49429
   129
  let val r = length kks in
blanchet@52324
   130
    HEADGOAL (EVERY' [select_prem_tac n (rotate_tac 1) k, rotate_tac ~1, hyp_subst_tac ctxt,
blanchet@52324
   131
      REPEAT_DETERM_N m o (dtac meta_spec THEN' rotate_tac ~1)]) THEN
blanchet@49391
   132
    EVERY [REPEAT_DETERM_N r
blanchet@52324
   133
        (HEADGOAL (rotate_tac ~1 THEN' dtac meta_mp THEN' rotate_tac 1) THEN prefer_tac 2),
wenzelm@54742
   134
      if r > 0 then ALLGOALS (Goal.norm_hhf_tac ctxt) else all_tac, HEADGOAL atac,
traytel@55803
   135
      mk_induct_leverage_prem_prems_tac ctxt nn kks fp_abs_inverses abs_inverses set_maps
traytel@55803
   136
        pre_set_defs]
blanchet@49391
   137
  end;
blanchet@49368
   138
traytel@55803
   139
fun mk_induct_tac ctxt nn ns mss kkss ctr_defs ctor_induct' fp_abs_inverses abs_inverses set_maps
traytel@55803
   140
    pre_set_defss =
blanchet@49590
   141
  let val n = Integer.sum ns in
blanchet@54922
   142
    unfold_thms_tac ctxt ctr_defs THEN HEADGOAL (rtac ctor_induct') THEN
blanchet@54923
   143
    co_induct_inst_as_projs_tac ctxt 0 THEN
traytel@55803
   144
    EVERY (map4 (EVERY oooo map3 o
traytel@55803
   145
        mk_induct_discharge_prem_tac ctxt nn n fp_abs_inverses abs_inverses set_maps)
traytel@55803
   146
      pre_set_defss mss (unflat mss (1 upto n)) kkss)
blanchet@49368
   147
  end;
blanchet@49368
   148
traytel@55803
   149
fun mk_coinduct_same_ctr_tac ctxt rel_eqs pre_rel_def fp_abs_inverse abs_inverse dtor_ctor ctr_def
traytel@55803
   150
    discs sels =
wenzelm@51798
   151
  hyp_subst_tac ctxt THEN'
traytel@49665
   152
  CONVERSION (hhf_concl_conv
traytel@49665
   153
    (Conv.top_conv (K (Conv.try_conv (Conv.rewr_conv ctr_def))) ctxt) ctxt) THEN'
blanchet@49642
   154
  SELECT_GOAL (unfold_thms_tac ctxt (pre_rel_def :: dtor_ctor :: sels)) THEN'
traytel@55803
   155
  SELECT_GOAL (unfold_thms_tac ctxt (pre_rel_def :: fp_abs_inverse :: abs_inverse :: dtor_ctor ::
blanchet@55966
   156
    sels @ sumprod_thms_rel @ @{thms o_apply vimage2p_def})) THEN'
blanchet@49591
   157
  (atac ORELSE' REPEAT o etac conjE THEN'
blanchet@49591
   158
     full_simp_tac
blanchet@52324
   159
       (ss_only (@{thm prod.inject} :: no_refl discs @ rel_eqs @ more_simp_thms) ctxt) THEN'
blanchet@54241
   160
     REPEAT o etac conjE THEN_MAYBE' REPEAT o hyp_subst_tac ctxt THEN'
blanchet@54241
   161
     REPEAT o (resolve_tac [refl, conjI] ORELSE' atac));
blanchet@49590
   162
blanchet@52966
   163
fun mk_coinduct_distinct_ctrs_tac ctxt discs discs' =
blanchet@54198
   164
  let
blanchet@54198
   165
    val discs'' = map (perhaps (try (fn th => th RS @{thm notnotD}))) (discs @ discs')
blanchet@54198
   166
      |> distinct Thm.eq_thm_prop;
blanchet@54198
   167
  in
blanchet@54198
   168
    hyp_subst_tac ctxt THEN' REPEAT o etac conjE THEN'
blanchet@54198
   169
    full_simp_tac (ss_only (refl :: no_refl discs'' @ basic_simp_thms) ctxt)
blanchet@54198
   170
  end;
blanchet@49590
   171
traytel@55803
   172
fun mk_coinduct_discharge_prem_tac ctxt rel_eqs' nn kk n pre_rel_def fp_abs_inverse abs_inverse
traytel@55803
   173
    dtor_ctor exhaust ctr_defs discss selss =
blanchet@49590
   174
  let val ks = 1 upto n in
blanchet@54837
   175
    EVERY' ([rtac allI, rtac allI, rtac impI, select_prem_tac nn (dtac meta_spec) kk,
blanchet@54923
   176
        dtac meta_spec, dtac meta_mp, atac, rtac exhaust, K (co_induct_inst_as_projs_tac ctxt 0),
wenzelm@51798
   177
        hyp_subst_tac ctxt] @
blanchet@49591
   178
      map4 (fn k => fn ctr_def => fn discs => fn sels =>
blanchet@54923
   179
        EVERY' ([rtac exhaust, K (co_induct_inst_as_projs_tac ctxt 1)] @
blanchet@49591
   180
          map2 (fn k' => fn discs' =>
blanchet@49591
   181
            if k' = k then
traytel@55803
   182
              mk_coinduct_same_ctr_tac ctxt rel_eqs' pre_rel_def fp_abs_inverse abs_inverse
traytel@55803
   183
                dtor_ctor ctr_def discs sels
blanchet@49591
   184
            else
blanchet@52966
   185
              mk_coinduct_distinct_ctrs_tac ctxt discs discs') ks discss)) ks ctr_defs discss selss)
blanchet@49590
   186
  end;
blanchet@49590
   187
traytel@55803
   188
fun mk_coinduct_tac ctxt rel_eqs' nn ns dtor_coinduct' pre_rel_defs fp_abs_inverses abs_inverses
traytel@55803
   189
    dtor_ctors exhausts ctr_defss discsss selsss =
blanchet@52324
   190
  HEADGOAL (rtac dtor_coinduct' THEN'
traytel@55803
   191
    EVERY' (map10 (mk_coinduct_discharge_prem_tac ctxt rel_eqs' nn)
traytel@55803
   192
      (1 upto nn) ns pre_rel_defs fp_abs_inverses abs_inverses dtor_ctors exhausts ctr_defss discsss
traytel@55803
   193
      selsss));
blanchet@49590
   194
blanchet@49123
   195
end;