src/HOL/Codatatype/Tools/bnf_fp_sugar_tactics.ML
author blanchet
Fri Sep 14 22:23:11 2012 +0200 (2012-09-14)
changeset 49385 83b867707af2
parent 49384 94ad5ba23541
child 49389 da621dc65146
permissions -rw-r--r--
merged two unfold steps
     1 (*  Title:      HOL/Codatatype/Tools/bnf_fp_sugar_tactics.ML
     2     Author:     Jasmin Blanchette, TU Muenchen
     3     Copyright   2012
     4 
     5 Tactics for the LFP/GFP 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 smash_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 smash_spurious_fs_tac = PRIMITIVE o smash_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 fun mk_induct_prelude_tac ctxt ctr_defs fld_induct' =
    92   Local_Defs.unfold_tac ctxt ctr_defs THEN rtac fld_induct' 1 THEN smash_spurious_fs_tac ctxt;
    93 
    94 fun mk_induct_prepare_prem_tac n m k =
    95   EVERY' [select_prem_tac n (rotate_tac 1) k, rotate_tac ~1, hyp_subst_tac,
    96     REPEAT_DETERM_N m o (dtac meta_spec THEN' rotate_tac ~1)] 1;
    97 
    98 (* FIXME: why not in "Pure"? *)
    99 fun prefer_tac i = defer_tac i THEN PRIMITIVE (Thm.permute_prems 0 ~1);
   100 
   101 fun mk_induct_prepare_prem_prems_tac r =
   102   REPEAT_DETERM_N r (rotate_tac ~1 1 THEN dtac meta_mp 1 THEN rotate_tac 1 1 THEN prefer_tac 2) THEN
   103   PRIMITIVE Raw_Simplifier.norm_hhf;
   104 
   105 val induct_prem_prem_thms =
   106   @{thms SUP_empty Sup_empty Sup_insert UN_compreh_bex UN_insert Un_assoc[symmetric] Un_empty_left
   107       Un_empty_right Union_Un_distrib collect_def[abs_def] fst_conv image_def o_apply snd_conv
   108       snd_prod_fun sum.cases sup_bot_right fst_map_pair map_pair_simp sum_map.simps};
   109 
   110 (* These rules interfere with the "set_natural'" properties of "sum" and "prod", so we explicitly
   111    delay them. *)
   112 val induct_prem_prem_thms_delayed =
   113   @{thms fsts_def[abs_def] snds_def[abs_def] sum_setl_def[abs_def] sum_setr_def[abs_def]};
   114 
   115 (* TODO: Get rid of "auto_tac" (or at least use a naked context) *)
   116 fun mk_induct_prem_prem_endgame_tac _ 0 = atac ORELSE' rtac @{thm singletonI}
   117   | mk_induct_prem_prem_endgame_tac ctxt qq =
   118     REPEAT_DETERM_N qq o
   119       (SELECT_GOAL (Local_Defs.unfold_tac ctxt @{thms Union_iff bex_simps(6)}) THEN'
   120        etac @{thm induct_set_step}) THEN'
   121     atac ORELSE' SELECT_GOAL (auto_tac ctxt);
   122 
   123 fun mk_induct_discharge_prem_prems_tac ctxt nn ixs set_natural's pre_set_defs =
   124   EVERY' (maps (fn ((pp, jj), (qq, kk)) =>
   125       [select_prem_tac nn (dtac meta_spec) kk, etac meta_mp,
   126        SELECT_GOAL (Local_Defs.unfold_tac ctxt
   127          (pre_set_defs @ set_natural's @ induct_prem_prem_thms)),
   128        SELECT_GOAL (Local_Defs.unfold_tac ctxt
   129          (induct_prem_prem_thms_delayed @ induct_prem_prem_thms)),
   130        rtac (mk_UnIN pp jj) THEN' mk_induct_prem_prem_endgame_tac ctxt qq ORELSE'
   131          SELECT_GOAL (auto_tac ctxt)])
   132     (rev ixs)) 1;
   133 
   134 fun mk_induct_discharge_prem_tac ctxt nn n set_natural's pre_set_defs m k ixs =
   135   EVERY [mk_induct_prepare_prem_tac n m k,
   136     mk_induct_prepare_prem_prems_tac (length ixs), atac 1,
   137     mk_induct_discharge_prem_prems_tac ctxt nn ixs set_natural's pre_set_defs];
   138 
   139 fun mk_induct_tac ctxt ns mss ixsss ctr_defs fld_induct' set_natural's pre_set_defss =
   140   let
   141     val nn = length ns;
   142     val n = Integer.sum ns;
   143   in
   144     mk_induct_prelude_tac ctxt ctr_defs fld_induct' THEN
   145     EVERY (map4 (EVERY oooo map3 o mk_induct_discharge_prem_tac ctxt nn n set_natural's)
   146       pre_set_defss mss (unflat mss (1 upto n)) ixsss)
   147   end;
   148 
   149 end;