src/HOL/BNF/Tools/bnf_fp_def_sugar_tactics.ML
author blanchet
Mon May 06 21:20:54 2013 +0200 (2013-05-06)
changeset 51884 2928fda12661
parent 51850 106afdf5806c
child 51893 596baae88a88
permissions -rw-r--r--
factor out construction of iterator
blanchet@49636
     1
(*  Title:      HOL/BNF/Tools/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@49585
    10
  val sum_prod_thms_map: thm list
blanchet@49585
    11
  val sum_prod_thms_set: thm list
blanchet@49585
    12
  val sum_prod_thms_rel: thm list
blanchet@49585
    13
blanchet@49130
    14
  val mk_case_tac: Proof.context -> int -> int -> int -> thm -> thm -> thm -> tactic
blanchet@49591
    15
  val mk_coinduct_tac: Proof.context -> thm list -> int -> int list -> thm -> thm list ->
blanchet@49591
    16
    thm list -> thm list -> thm list list -> thm list list list -> thm list list list -> tactic
blanchet@51843
    17
  val mk_coiter_tac: thm list -> thm list -> thm list -> thm list -> thm list -> thm -> thm ->
blanchet@49683
    18
    thm -> Proof.context -> tactic
blanchet@49501
    19
  val mk_ctor_iff_dtor_tac: Proof.context -> ctyp option list -> cterm -> cterm -> thm -> thm ->
blanchet@49501
    20
    tactic
blanchet@51843
    21
  val mk_disc_coiter_iff_tac: thm list -> thm list -> thm list -> Proof.context -> tactic
blanchet@49161
    22
  val mk_exhaust_tac: Proof.context -> int -> thm list -> thm -> thm -> tactic
blanchet@49127
    23
  val mk_half_distinct_tac: Proof.context -> thm -> thm list -> tactic
blanchet@49590
    24
  val mk_induct_tac: Proof.context -> int -> int list -> int list list -> int list list list ->
blanchet@49590
    25
    thm list -> thm -> thm list -> thm list list -> tactic
blanchet@49126
    26
  val mk_inject_tac: Proof.context -> thm -> thm -> tactic
blanchet@51843
    27
  val mk_iter_tac: thm list -> thm list -> thm list -> thm list -> thm -> thm -> Proof.context
blanchet@49670
    28
    -> tactic
blanchet@49123
    29
end;
blanchet@49123
    30
blanchet@49636
    31
structure BNF_FP_Def_Sugar_Tactics : BNF_FP_DEF_SUGAR_TACTICS =
blanchet@49123
    32
struct
blanchet@49123
    33
blanchet@49125
    34
open BNF_Tactics
blanchet@49123
    35
open BNF_Util
blanchet@51850
    36
open BNF_FP_Util
blanchet@49123
    37
blanchet@49590
    38
val basic_simp_thms = @{thms simp_thms(7,8,12,14,22,24)};
blanchet@49590
    39
val more_simp_thms = basic_simp_thms @ @{thms simp_thms(11,15,16,21)};
blanchet@49590
    40
blanchet@49683
    41
val sum_prod_thms_map = @{thms id_apply map_pair_simp prod.cases sum.cases sum_map.simps};
blanchet@49585
    42
val sum_prod_thms_set0 =
blanchet@49585
    43
  @{thms SUP_empty Sup_empty Sup_insert UN_insert Un_empty_left Un_empty_right Un_iff
blanchet@49585
    44
      Union_Un_distrib collect_def[abs_def] image_def o_apply map_pair_simp
blanchet@49585
    45
      mem_Collect_eq mem_UN_compreh_eq prod_set_simps sum_map.simps sum_set_simps};
blanchet@49585
    46
val sum_prod_thms_set = @{thms UN_compreh_eq_eq} @ sum_prod_thms_set0;
blanchet@49643
    47
val sum_prod_thms_rel = @{thms prod_rel_simp sum_rel_simps};
blanchet@49368
    48
wenzelm@51717
    49
val ss_if_True_False = simpset_of (ss_only @{thms if_True if_False} @{context});
blanchet@49589
    50
blanchet@49590
    51
fun mk_proj T k =
blanchet@49590
    52
  let val binders = binder_types T in
blanchet@49590
    53
    fold_rev (fn T => fn t => Abs (Name.uu, T, t)) binders (Bound (length binders - k))
blanchet@49590
    54
  end;
blanchet@49590
    55
blanchet@49668
    56
fun hhf_concl_conv cv ctxt ct =
blanchet@49668
    57
  (case Thm.term_of ct of
blanchet@49668
    58
    Const (@{const_name all}, _) $ Abs _ =>
blanchet@49668
    59
    Conv.arg_conv (Conv.abs_conv (hhf_concl_conv cv o snd) ctxt) ct
blanchet@49668
    60
  | _ => Conv.concl_conv ~1 cv ct);
blanchet@49668
    61
blanchet@49590
    62
fun inst_as_projs ctxt k thm =
blanchet@49368
    63
  let
blanchet@49384
    64
    val fs =
blanchet@49368
    65
      Term.add_vars (prop_of thm) []
blanchet@49368
    66
      |> filter (fn (_, Type (@{type_name fun}, [_, T'])) => T' <> HOLogic.boolT | _ => false);
blanchet@49384
    67
    val cfs =
blanchet@49590
    68
      map (fn f as (_, T) => (certify ctxt (Var f), certify ctxt (mk_proj T k))) fs;
blanchet@49368
    69
  in
blanchet@49384
    70
    Drule.cterm_instantiate cfs thm
blanchet@49368
    71
  end;
blanchet@49368
    72
blanchet@49590
    73
val inst_as_projs_tac = PRIMITIVE oo inst_as_projs;
blanchet@49368
    74
blanchet@49501
    75
fun mk_case_tac ctxt n k m case_def ctr_def dtor_ctor =
blanchet@49504
    76
  unfold_thms_tac ctxt [case_def, ctr_def, dtor_ctor] THEN
blanchet@49264
    77
  (rtac (mk_sum_casesN_balanced n k RS ssubst) THEN'
blanchet@49130
    78
   REPEAT_DETERM_N (Int.max (0, m - 1)) o rtac (@{thm split} RS ssubst) THEN'
blanchet@49130
    79
   rtac refl) 1;
blanchet@49130
    80
blanchet@49501
    81
fun mk_exhaust_tac ctxt n ctr_defs ctor_iff_dtor sumEN' =
blanchet@49504
    82
  unfold_thms_tac ctxt (ctor_iff_dtor :: ctr_defs) THEN rtac sumEN' 1 THEN
blanchet@49504
    83
  unfold_thms_tac ctxt @{thms all_prod_eq} THEN
blanchet@49368
    84
  EVERY' (maps (fn k => [select_prem_tac n (rotate_tac 1) k, REPEAT_DETERM o dtac meta_spec,
blanchet@49368
    85
    etac meta_mp, atac]) (1 upto n)) 1;
blanchet@49125
    86
blanchet@49501
    87
fun mk_ctor_iff_dtor_tac ctxt cTs cctor cdtor ctor_dtor dtor_ctor =
blanchet@49123
    88
  (rtac iffI THEN'
blanchet@49123
    89
   EVERY' (map3 (fn cTs => fn cx => fn th =>
blanchet@49123
    90
     dtac (Drule.instantiate' cTs [NONE, NONE, SOME cx] arg_cong) THEN'
blanchet@49504
    91
     SELECT_GOAL (unfold_thms_tac ctxt [th]) THEN'
blanchet@49501
    92
     atac) [rev cTs, cTs] [cdtor, cctor] [dtor_ctor, ctor_dtor])) 1;
blanchet@49123
    93
blanchet@49501
    94
fun mk_half_distinct_tac ctxt ctor_inject ctr_defs =
blanchet@49504
    95
  unfold_thms_tac ctxt (ctor_inject :: @{thms sum.inject} @ ctr_defs) THEN
blanchet@49127
    96
  rtac @{thm sum.distinct(1)} 1;
blanchet@49127
    97
blanchet@49501
    98
fun mk_inject_tac ctxt ctr_def ctor_inject =
blanchet@49504
    99
  unfold_thms_tac ctxt [ctr_def] THEN rtac (ctor_inject RS ssubst) 1 THEN
blanchet@49504
   100
  unfold_thms_tac ctxt @{thms sum.inject Pair_eq conj_assoc} THEN rtac refl 1;
blanchet@49126
   101
blanchet@49585
   102
(*TODO: Try "sum_prod_thms_map" here, enriched with a few theorems*)
blanchet@51843
   103
val iter_unfold_thms =
blanchet@49672
   104
  @{thms comp_def convol_def fst_conv id_def map_pair_simp prod_case_Pair_iden snd_conv split_conv
blanchet@49670
   105
      sum.simps(5,6) sum_map.simps unit_case_Unity};
blanchet@49205
   106
blanchet@51843
   107
fun mk_iter_tac pre_map_defs map_comp's map_ids'' iter_defs ctor_iter ctr_def ctxt =
blanchet@51843
   108
  unfold_thms_tac ctxt (ctr_def :: ctor_iter :: iter_defs @ pre_map_defs @ map_comp's @
blanchet@51843
   109
    map_ids'' @ iter_unfold_thms) THEN rtac refl 1;
blanchet@49205
   110
blanchet@49683
   111
(*TODO: sum_case_if needed?*)
blanchet@51843
   112
val coiter_unfold_thms =
blanchet@49683
   113
  @{thms id_def ident_o_ident sum_case_if sum_case_o_inj} @ sum_prod_thms_map;
blanchet@49683
   114
blanchet@51843
   115
fun mk_coiter_tac coiter_defs map_comps'' map_comp's map_ids'' map_if_distribs
blanchet@51843
   116
    ctor_dtor_coiter pre_map_def ctr_def ctxt =
blanchet@51843
   117
  unfold_thms_tac ctxt (ctr_def :: coiter_defs) THEN
blanchet@51843
   118
  (rtac (ctor_dtor_coiter RS trans) THEN'
wenzelm@51717
   119
    asm_simp_tac (put_simpset ss_if_True_False ctxt)) 1 THEN_MAYBE
blanchet@49683
   120
  (unfold_thms_tac ctxt (pre_map_def :: map_comp's @ map_comps'' @ map_ids'' @ map_if_distribs @
blanchet@51843
   121
    coiter_unfold_thms) THEN
blanchet@49680
   122
   (rtac refl ORELSE' rtac (@{thm unit_eq} RS arg_cong)) 1);
blanchet@49213
   123
blanchet@51843
   124
fun mk_disc_coiter_iff_tac case_splits' coiters discs ctxt =
blanchet@51843
   125
  EVERY (map3 (fn case_split_tac => fn coiter_thm => fn disc =>
blanchet@51843
   126
      case_split_tac 1 THEN unfold_thms_tac ctxt [coiter_thm] THEN
wenzelm@51717
   127
      asm_simp_tac (ss_only basic_simp_thms ctxt) 1 THEN
blanchet@49484
   128
      (if is_refl disc then all_tac else rtac disc 1))
blanchet@51843
   129
    (map rtac case_splits' @ [K all_tac]) coiters discs);
blanchet@49482
   130
wenzelm@51798
   131
fun solve_prem_prem_tac ctxt =
blanchet@49429
   132
  REPEAT o (eresolve_tac @{thms bexE rev_bexI} ORELSE' rtac @{thm rev_bexI[OF UNIV_I]} ORELSE'
wenzelm@51798
   133
    hyp_subst_tac ctxt ORELSE' resolve_tac @{thms disjI1 disjI2}) THEN'
blanchet@49429
   134
  (rtac refl ORELSE' atac ORELSE' rtac @{thm singletonI});
blanchet@49426
   135
blanchet@51766
   136
fun mk_induct_leverage_prem_prems_tac ctxt nn kks set_map's pre_set_defs =
blanchet@49429
   137
  EVERY' (maps (fn kk => [select_prem_tac nn (dtac meta_spec) kk, etac meta_mp,
blanchet@51766
   138
     SELECT_GOAL (unfold_thms_tac ctxt (pre_set_defs @ set_map's @ sum_prod_thms_set0)),
wenzelm@51798
   139
     solve_prem_prem_tac ctxt]) (rev kks)) 1;
blanchet@49368
   140
blanchet@51766
   141
fun mk_induct_discharge_prem_tac ctxt nn n set_map's pre_set_defs m k kks =
blanchet@49429
   142
  let val r = length kks in
wenzelm@51798
   143
    EVERY' [select_prem_tac n (rotate_tac 1) k, rotate_tac ~1, hyp_subst_tac ctxt,
blanchet@49391
   144
      REPEAT_DETERM_N m o (dtac meta_spec THEN' rotate_tac ~1)] 1 THEN
blanchet@49391
   145
    EVERY [REPEAT_DETERM_N r
blanchet@49391
   146
        (rotate_tac ~1 1 THEN dtac meta_mp 1 THEN rotate_tac 1 1 THEN prefer_tac 2),
blanchet@49391
   147
      if r > 0 then PRIMITIVE Raw_Simplifier.norm_hhf else all_tac, atac 1,
blanchet@51766
   148
      mk_induct_leverage_prem_prems_tac ctxt nn kks set_map's pre_set_defs]
blanchet@49391
   149
  end;
blanchet@49368
   150
blanchet@51766
   151
fun mk_induct_tac ctxt nn ns mss kkss ctr_defs ctor_induct' set_map's pre_set_defss =
blanchet@49590
   152
  let val n = Integer.sum ns in
blanchet@49590
   153
    unfold_thms_tac ctxt ctr_defs THEN rtac ctor_induct' 1 THEN inst_as_projs_tac ctxt 1 THEN
blanchet@51766
   154
    EVERY (map4 (EVERY oooo map3 o mk_induct_discharge_prem_tac ctxt nn n set_map's) pre_set_defss
blanchet@51766
   155
      mss (unflat mss (1 upto n)) kkss)
blanchet@49368
   156
  end;
blanchet@49368
   157
blanchet@49591
   158
fun mk_coinduct_same_ctr ctxt rel_eqs pre_rel_def dtor_ctor ctr_def discs sels =
wenzelm@51798
   159
  hyp_subst_tac ctxt THEN'
traytel@49665
   160
  CONVERSION (hhf_concl_conv
traytel@49665
   161
    (Conv.top_conv (K (Conv.try_conv (Conv.rewr_conv ctr_def))) ctxt) ctxt) THEN'
blanchet@49642
   162
  SELECT_GOAL (unfold_thms_tac ctxt (pre_rel_def :: dtor_ctor :: sels)) THEN'
blanchet@49591
   163
  SELECT_GOAL (unfold_thms_tac ctxt (pre_rel_def :: dtor_ctor :: sels @ sum_prod_thms_rel)) THEN'
blanchet@49591
   164
  (atac ORELSE' REPEAT o etac conjE THEN'
blanchet@49591
   165
     full_simp_tac
wenzelm@51717
   166
       (ss_only (@{thm prod.inject} :: no_refl discs @ rel_eqs @ more_simp_thms) ctxt) THEN_MAYBE'
wenzelm@51798
   167
     REPEAT o hyp_subst_tac ctxt THEN' REPEAT o rtac conjI THEN' REPEAT o rtac refl);
blanchet@49590
   168
wenzelm@51717
   169
fun mk_coinduct_distinct_ctrs ctxt discs discs' =
wenzelm@51798
   170
  hyp_subst_tac ctxt THEN' REPEAT o etac conjE THEN'
wenzelm@51717
   171
  full_simp_tac (ss_only (refl :: no_refl (discs @ discs') @ basic_simp_thms) ctxt);
blanchet@49590
   172
blanchet@49591
   173
fun mk_coinduct_discharge_prem_tac ctxt rel_eqs' nn kk n pre_rel_def dtor_ctor exhaust ctr_defs
blanchet@49591
   174
    discss selss =
blanchet@49590
   175
  let val ks = 1 upto n in
blanchet@49591
   176
    EVERY' ([rtac allI, rtac allI, rtac impI, select_prem_tac nn (dtac meta_spec) kk, dtac
wenzelm@51798
   177
        meta_spec, dtac meta_mp, atac, rtac exhaust, K (inst_as_projs_tac ctxt 1),
wenzelm@51798
   178
        hyp_subst_tac ctxt] @
blanchet@49591
   179
      map4 (fn k => fn ctr_def => fn discs => fn sels =>
blanchet@49591
   180
        EVERY' ([rtac exhaust, K (inst_as_projs_tac ctxt 2)] @
blanchet@49591
   181
          map2 (fn k' => fn discs' =>
blanchet@49591
   182
            if k' = k then
blanchet@49591
   183
              mk_coinduct_same_ctr ctxt rel_eqs' pre_rel_def dtor_ctor ctr_def discs sels
blanchet@49591
   184
            else
wenzelm@51717
   185
              mk_coinduct_distinct_ctrs ctxt discs discs') ks discss)) ks ctr_defs discss selss)
blanchet@49590
   186
  end;
blanchet@49590
   187
blanchet@49591
   188
fun mk_coinduct_tac ctxt rel_eqs' nn ns dtor_coinduct' pre_rel_defs dtor_ctors exhausts ctr_defss
blanchet@49591
   189
    discsss selsss =
blanchet@49591
   190
  (rtac dtor_coinduct' THEN'
blanchet@49591
   191
   EVERY' (map8 (mk_coinduct_discharge_prem_tac ctxt rel_eqs' nn)
blanchet@49591
   192
     (1 upto nn) ns pre_rel_defs dtor_ctors exhausts ctr_defss discsss selsss)) 1;
blanchet@49590
   193
blanchet@49123
   194
end;