src/HOL/Codatatype/Tools/bnf_fp_sugar_tactics.ML
author blanchet
Sat Sep 15 21:10:26 2012 +0200 (2012-09-15)
changeset 49391 3a96797fd53e
parent 49389 da621dc65146
child 49426 6d05b8452cf3
permissions -rw-r--r--
tuned induction tactic
     1 (*  Title:      HOL/Codatatype/Tools/bnf_fp_sugar_tactics.ML
     2     Author:     Jasmin Blanchette, TU Muenchen
     3     Copyright   2012
     4 
     5 Tactics for datatype and codatatype sugar.
     6 *)
     7 
     8 signature BNF_FP_SUGAR_TACTICS =
     9 sig
    10   val mk_case_tac: Proof.context -> int -> int -> int -> thm -> thm -> thm -> tactic
    11   val mk_coiter_like_tac: thm list -> thm list -> thm -> thm -> thm -> Proof.context -> tactic
    12   val mk_exhaust_tac: Proof.context -> int -> thm list -> thm -> thm -> tactic
    13   val mk_fld_iff_unf_tac: Proof.context -> ctyp option list -> cterm -> cterm -> thm -> thm ->
    14     tactic
    15   val mk_half_distinct_tac: Proof.context -> thm -> thm list -> tactic
    16   val mk_induct_tac: Proof.context -> int list -> int list list ->
    17     ((int * int) * (int * int)) list list list -> thm list -> thm -> thm list -> thm list list ->
    18     tactic
    19   val mk_inject_tac: Proof.context -> thm -> thm -> tactic
    20   val mk_iter_like_tac: thm list -> thm list -> thm list -> thm -> thm -> Proof.context -> tactic
    21 end;
    22 
    23 structure BNF_FP_Sugar_Tactics : BNF_FP_SUGAR_TACTICS =
    24 struct
    25 
    26 open BNF_Tactics
    27 open BNF_Util
    28 open BNF_FP_Util
    29 
    30 val meta_mp = @{thm meta_mp};
    31 val meta_spec = @{thm meta_spec};
    32 
    33 fun inst_spurious_fs lthy thm =
    34   let
    35     val fs =
    36       Term.add_vars (prop_of thm) []
    37       |> filter (fn (_, Type (@{type_name fun}, [_, T'])) => T' <> HOLogic.boolT | _ => false);
    38     val cfs =
    39       map (fn f as (_, T) => (certify lthy (Var f), certify lthy (id_abs (domain_type T)))) fs;
    40   in
    41     Drule.cterm_instantiate cfs thm
    42   end;
    43 
    44 val inst_spurious_fs_tac = PRIMITIVE o inst_spurious_fs;
    45 
    46 fun mk_case_tac ctxt n k m case_def ctr_def unf_fld =
    47   Local_Defs.unfold_tac ctxt [case_def, ctr_def, unf_fld] THEN
    48   (rtac (mk_sum_casesN_balanced n k RS ssubst) THEN'
    49    REPEAT_DETERM_N (Int.max (0, m - 1)) o rtac (@{thm split} RS ssubst) THEN'
    50    rtac refl) 1;
    51 
    52 fun mk_exhaust_tac ctxt n ctr_defs fld_iff_unf sumEN' =
    53   Local_Defs.unfold_tac ctxt (fld_iff_unf :: ctr_defs) THEN rtac sumEN' 1 THEN
    54   Local_Defs.unfold_tac ctxt @{thms all_prod_eq} THEN
    55   EVERY' (maps (fn k => [select_prem_tac n (rotate_tac 1) k, REPEAT_DETERM o dtac meta_spec,
    56     etac meta_mp, atac]) (1 upto n)) 1;
    57 
    58 fun mk_fld_iff_unf_tac ctxt cTs cfld cunf fld_unf unf_fld =
    59   (rtac iffI THEN'
    60    EVERY' (map3 (fn cTs => fn cx => fn th =>
    61      dtac (Drule.instantiate' cTs [NONE, NONE, SOME cx] arg_cong) THEN'
    62      SELECT_GOAL (Local_Defs.unfold_tac ctxt [th]) THEN'
    63      atac) [rev cTs, cTs] [cunf, cfld] [unf_fld, fld_unf])) 1;
    64 
    65 fun mk_half_distinct_tac ctxt fld_inject ctr_defs =
    66   Local_Defs.unfold_tac ctxt (fld_inject :: @{thms sum.inject} @ ctr_defs) THEN
    67   rtac @{thm sum.distinct(1)} 1;
    68 
    69 fun mk_inject_tac ctxt ctr_def fld_inject =
    70   Local_Defs.unfold_tac ctxt [ctr_def] THEN rtac (fld_inject RS ssubst) 1 THEN
    71   Local_Defs.unfold_tac ctxt @{thms sum.inject Pair_eq conj_assoc} THEN rtac refl 1;
    72 
    73 val iter_like_thms =
    74   @{thms case_unit comp_def convol_def id_apply map_pair_def sum.simps(5,6) sum_map.simps
    75       split_conv};
    76 
    77 fun mk_iter_like_tac pre_map_defs map_ids iter_like_defs fld_iter_like ctr_def ctxt =
    78   Local_Defs.unfold_tac ctxt (ctr_def :: fld_iter_like :: iter_like_defs @ pre_map_defs @ map_ids @
    79     iter_like_thms) THEN Local_Defs.unfold_tac ctxt @{thms id_def} THEN rtac refl 1;
    80 
    81 val coiter_like_ss = ss_only @{thms if_True if_False};
    82 val coiter_like_thms = @{thms id_apply map_pair_def sum_map.simps prod.cases};
    83 
    84 fun mk_coiter_like_tac coiter_like_defs map_ids fld_unf_coiter_like pre_map_def ctr_def ctxt =
    85   Local_Defs.unfold_tac ctxt (ctr_def :: coiter_like_defs) THEN
    86   subst_tac ctxt [fld_unf_coiter_like] 1 THEN asm_simp_tac coiter_like_ss 1 THEN
    87   Local_Defs.unfold_tac ctxt (pre_map_def :: coiter_like_thms @ map_ids) THEN
    88   Local_Defs.unfold_tac ctxt @{thms id_def} THEN
    89   TRY ((rtac refl ORELSE' subst_tac ctxt @{thms unit_eq} THEN' rtac refl) 1);
    90 
    91 (* TODO: Get rid of "auto_tac" (or at least use a naked context) *)
    92 
    93 fun mk_induct_prem_prem_endgame_tac _ 0 = atac ORELSE' rtac @{thm singletonI}
    94   | mk_induct_prem_prem_endgame_tac ctxt qq =
    95     REPEAT_DETERM_N qq o
    96       (SELECT_GOAL (Local_Defs.unfold_tac ctxt @{thms Union_iff bex_simps(6)}) THEN'
    97        etac @{thm induct_set_step}) THEN'
    98     atac ORELSE' SELECT_GOAL (auto_tac ctxt);
    99 
   100 val induct_prem_prem_thms =
   101   @{thms SUP_empty Sup_empty Sup_insert UN_compreh_bex UN_insert Un_assoc[symmetric] Un_empty_left
   102       Un_empty_right Union_Un_distrib collect_def[abs_def] fst_conv image_def o_apply snd_conv
   103       snd_prod_fun sum.cases sup_bot_right fst_map_pair map_pair_simp sum_map.simps};
   104 
   105 (* These rules interfere with the "set_natural'" properties of "sum" and "prod", so we explicitly
   106    delay them. *)
   107 val induct_prem_prem_thms_delayed =
   108   @{thms fsts_def[abs_def] snds_def[abs_def] sum_setl_def[abs_def] sum_setr_def[abs_def]};
   109 
   110 fun mk_induct_leverage_prem_prems_tac ctxt nn ppjjqqkks set_natural's pre_set_defs =
   111   EVERY' (maps (fn ((pp, jj), (qq, kk)) =>
   112     [select_prem_tac nn (dtac meta_spec) kk, etac meta_mp,
   113      SELECT_GOAL (Local_Defs.unfold_tac ctxt
   114        (pre_set_defs @ set_natural's @ induct_prem_prem_thms)),
   115      SELECT_GOAL (Local_Defs.unfold_tac ctxt
   116        (induct_prem_prem_thms_delayed @ induct_prem_prem_thms)),
   117      rtac (mk_UnIN pp jj) THEN' mk_induct_prem_prem_endgame_tac ctxt qq ORELSE'
   118        SELECT_GOAL (auto_tac ctxt)]) (rev ppjjqqkks)) 1;
   119 
   120 fun mk_induct_discharge_prem_tac ctxt nn n set_natural's pre_set_defs m k ppjjqqkks =
   121   let val r = length ppjjqqkks in
   122     EVERY' [select_prem_tac n (rotate_tac 1) k, rotate_tac ~1, hyp_subst_tac,
   123       REPEAT_DETERM_N m o (dtac meta_spec THEN' rotate_tac ~1)] 1 THEN
   124     EVERY [REPEAT_DETERM_N r
   125         (rotate_tac ~1 1 THEN dtac meta_mp 1 THEN rotate_tac 1 1 THEN prefer_tac 2),
   126       if r > 0 then PRIMITIVE Raw_Simplifier.norm_hhf else all_tac, atac 1,
   127       mk_induct_leverage_prem_prems_tac ctxt nn ppjjqqkks set_natural's pre_set_defs]
   128   end;
   129 
   130 fun mk_induct_tac ctxt ns mss ppjjqqkksss ctr_defs fld_induct' set_natural's pre_set_defss =
   131   let
   132     val nn = length ns;
   133     val n = Integer.sum ns;
   134   in
   135     Local_Defs.unfold_tac ctxt ctr_defs THEN rtac fld_induct' 1 THEN inst_spurious_fs_tac ctxt THEN
   136     EVERY (map4 (EVERY oooo map3 o mk_induct_discharge_prem_tac ctxt nn n set_natural's)
   137       pre_set_defss mss (unflat mss (1 upto n)) ppjjqqkksss)
   138   end;
   139 
   140 end;