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