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