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