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