src/HOL/BNF/Tools/bnf_fp_def_sugar_tactics.ML
changeset 55058 4e700eb471d4
parent 55057 6b0fcbeebaba
child 55059 ef2e0fb783c6
     1.1 --- a/src/HOL/BNF/Tools/bnf_fp_def_sugar_tactics.ML	Mon Jan 20 18:24:55 2014 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,181 +0,0 @@
     1.4 -(*  Title:      HOL/BNF/Tools/bnf_fp_def_sugar_tactics.ML
     1.5 -    Author:     Jasmin Blanchette, TU Muenchen
     1.6 -    Copyright   2012
     1.7 -
     1.8 -Tactics for datatype and codatatype sugar.
     1.9 -*)
    1.10 -
    1.11 -signature BNF_FP_DEF_SUGAR_TACTICS =
    1.12 -sig
    1.13 -  val sum_prod_thms_map: thm list
    1.14 -  val sum_prod_thms_set: thm list
    1.15 -  val sum_prod_thms_rel: thm list
    1.16 -
    1.17 -  val mk_coinduct_tac: Proof.context -> thm list -> int -> int list -> thm -> thm list ->
    1.18 -    thm list -> thm list -> thm list list -> thm list list list -> thm list list list -> tactic
    1.19 -  val mk_coiter_tac: thm list -> thm list -> thm -> thm -> thm -> Proof.context -> tactic
    1.20 -  val mk_ctor_iff_dtor_tac: Proof.context -> ctyp option list -> cterm -> cterm -> thm -> thm ->
    1.21 -    tactic
    1.22 -  val mk_disc_coiter_iff_tac: thm list -> thm list -> thm list -> Proof.context -> tactic
    1.23 -  val mk_exhaust_tac: Proof.context -> int -> thm list -> thm -> thm -> tactic
    1.24 -  val mk_half_distinct_tac: Proof.context -> thm -> thm list -> tactic
    1.25 -  val mk_induct_tac: Proof.context -> int -> int list -> int list list -> int list list list ->
    1.26 -    thm list -> thm -> thm list -> thm list list -> tactic
    1.27 -  val mk_inject_tac: Proof.context -> thm -> thm -> tactic
    1.28 -  val mk_iter_tac: thm list -> thm list -> thm list -> thm -> thm -> Proof.context -> tactic
    1.29 -end;
    1.30 -
    1.31 -structure BNF_FP_Def_Sugar_Tactics : BNF_FP_DEF_SUGAR_TACTICS =
    1.32 -struct
    1.33 -
    1.34 -open BNF_Tactics
    1.35 -open BNF_Util
    1.36 -open BNF_FP_Util
    1.37 -
    1.38 -val basic_simp_thms = @{thms simp_thms(7,8,12,14,22,24)};
    1.39 -val more_simp_thms = basic_simp_thms @ @{thms simp_thms(11,15,16,21)};
    1.40 -
    1.41 -val sum_prod_thms_map = @{thms id_apply map_pair_simp prod.cases sum.cases sum_map.simps};
    1.42 -val sum_prod_thms_set0 =
    1.43 -  @{thms SUP_empty Sup_empty Sup_insert UN_insert Un_empty_left Un_empty_right Un_iff
    1.44 -      Union_Un_distrib collect_def[abs_def] image_def o_apply map_pair_simp
    1.45 -      mem_Collect_eq mem_UN_compreh_eq prod_set_simps sum_map.simps sum_set_simps};
    1.46 -val sum_prod_thms_set = @{thms UN_compreh_eq_eq} @ sum_prod_thms_set0;
    1.47 -val sum_prod_thms_rel = @{thms prod_rel_simp sum_rel_simps id_apply};
    1.48 -
    1.49 -fun hhf_concl_conv cv ctxt ct =
    1.50 -  (case Thm.term_of ct of
    1.51 -    Const (@{const_name all}, _) $ Abs _ =>
    1.52 -    Conv.arg_conv (Conv.abs_conv (hhf_concl_conv cv o snd) ctxt) ct
    1.53 -  | _ => Conv.concl_conv ~1 cv ct);
    1.54 -
    1.55 -fun co_induct_inst_as_projs ctxt k thm =
    1.56 -  let
    1.57 -    val fs = Term.add_vars (prop_of thm) []
    1.58 -      |> filter (fn (_, Type (@{type_name fun}, [_, T'])) => T' <> HOLogic.boolT | _ => false);
    1.59 -    fun mk_cfp (f as (_, T)) =
    1.60 -      (certify ctxt (Var f), certify ctxt (mk_proj T (num_binder_types T) k));
    1.61 -    val cfps = map mk_cfp fs;
    1.62 -  in
    1.63 -    Drule.cterm_instantiate cfps thm
    1.64 -  end;
    1.65 -
    1.66 -val co_induct_inst_as_projs_tac = PRIMITIVE oo co_induct_inst_as_projs;
    1.67 -
    1.68 -fun mk_exhaust_tac ctxt n ctr_defs ctor_iff_dtor sumEN' =
    1.69 -  unfold_thms_tac ctxt (ctor_iff_dtor :: ctr_defs) THEN HEADGOAL (rtac sumEN') THEN
    1.70 -  unfold_thms_tac ctxt @{thms split_paired_all} THEN
    1.71 -  HEADGOAL (EVERY' (maps (fn k => [select_prem_tac n (rotate_tac 1) k,
    1.72 -    REPEAT_DETERM o dtac meta_spec, etac meta_mp, atac]) (1 upto n)));
    1.73 -
    1.74 -fun mk_ctor_iff_dtor_tac ctxt cTs cctor cdtor ctor_dtor dtor_ctor =
    1.75 -  HEADGOAL (rtac iffI THEN'
    1.76 -    EVERY' (map3 (fn cTs => fn cx => fn th =>
    1.77 -      dtac (Drule.instantiate' cTs [NONE, NONE, SOME cx] arg_cong) THEN'
    1.78 -      SELECT_GOAL (unfold_thms_tac ctxt [th]) THEN'
    1.79 -      atac) [rev cTs, cTs] [cdtor, cctor] [dtor_ctor, ctor_dtor]));
    1.80 -
    1.81 -fun mk_half_distinct_tac ctxt ctor_inject ctr_defs =
    1.82 -  unfold_thms_tac ctxt (ctor_inject :: @{thms sum.inject} @ ctr_defs) THEN
    1.83 -  HEADGOAL (rtac @{thm sum.distinct(1)});
    1.84 -
    1.85 -fun mk_inject_tac ctxt ctr_def ctor_inject =
    1.86 -  unfold_thms_tac ctxt [ctr_def] THEN HEADGOAL (rtac (ctor_inject RS ssubst)) THEN
    1.87 -  unfold_thms_tac ctxt @{thms sum.inject Pair_eq conj_assoc} THEN HEADGOAL (rtac refl);
    1.88 -
    1.89 -val iter_unfold_thms =
    1.90 -  @{thms comp_def convol_def fst_conv id_def prod_case_Pair_iden snd_conv
    1.91 -      split_conv unit_case_Unity} @ sum_prod_thms_map;
    1.92 -
    1.93 -fun mk_iter_tac pre_map_defs map_idents iter_defs ctor_iter ctr_def ctxt =
    1.94 -  unfold_thms_tac ctxt (ctr_def :: ctor_iter :: iter_defs @ pre_map_defs @ map_idents @
    1.95 -    iter_unfold_thms) THEN HEADGOAL (rtac refl);
    1.96 -
    1.97 -val coiter_unfold_thms = @{thms id_def} @ sum_prod_thms_map;
    1.98 -val ss_if_True_False = simpset_of (ss_only @{thms if_True if_False} @{context});
    1.99 -
   1.100 -fun mk_coiter_tac coiter_defs map_idents ctor_dtor_coiter pre_map_def ctr_def ctxt =
   1.101 -  unfold_thms_tac ctxt (ctr_def :: coiter_defs) THEN
   1.102 -  HEADGOAL (rtac (ctor_dtor_coiter RS trans) THEN'
   1.103 -    asm_simp_tac (put_simpset ss_if_True_False ctxt)) THEN_MAYBE
   1.104 -  (unfold_thms_tac ctxt (pre_map_def :: map_idents @ coiter_unfold_thms) THEN
   1.105 -   HEADGOAL (rtac refl ORELSE' rtac (@{thm unit_eq} RS arg_cong)));
   1.106 -
   1.107 -fun mk_disc_coiter_iff_tac case_splits' coiters discs ctxt =
   1.108 -  EVERY (map3 (fn case_split_tac => fn coiter_thm => fn disc =>
   1.109 -      HEADGOAL case_split_tac THEN unfold_thms_tac ctxt [coiter_thm] THEN
   1.110 -      HEADGOAL (asm_simp_tac (ss_only basic_simp_thms ctxt)) THEN
   1.111 -      (if is_refl disc then all_tac else HEADGOAL (rtac disc)))
   1.112 -    (map rtac case_splits' @ [K all_tac]) coiters discs);
   1.113 -
   1.114 -fun solve_prem_prem_tac ctxt =
   1.115 -  REPEAT o (eresolve_tac @{thms bexE rev_bexI} ORELSE' rtac @{thm rev_bexI[OF UNIV_I]} ORELSE'
   1.116 -    hyp_subst_tac ctxt ORELSE' resolve_tac @{thms disjI1 disjI2}) THEN'
   1.117 -  (rtac refl ORELSE' atac ORELSE' rtac @{thm singletonI});
   1.118 -
   1.119 -fun mk_induct_leverage_prem_prems_tac ctxt nn kks set_maps pre_set_defs =
   1.120 -  HEADGOAL (EVERY' (maps (fn kk => [select_prem_tac nn (dtac meta_spec) kk, etac meta_mp,
   1.121 -    SELECT_GOAL (unfold_thms_tac ctxt (pre_set_defs @ set_maps @ sum_prod_thms_set0)),
   1.122 -    solve_prem_prem_tac ctxt]) (rev kks)));
   1.123 -
   1.124 -fun mk_induct_discharge_prem_tac ctxt nn n set_maps pre_set_defs m k kks =
   1.125 -  let val r = length kks in
   1.126 -    HEADGOAL (EVERY' [select_prem_tac n (rotate_tac 1) k, rotate_tac ~1, hyp_subst_tac ctxt,
   1.127 -      REPEAT_DETERM_N m o (dtac meta_spec THEN' rotate_tac ~1)]) THEN
   1.128 -    EVERY [REPEAT_DETERM_N r
   1.129 -        (HEADGOAL (rotate_tac ~1 THEN' dtac meta_mp THEN' rotate_tac 1) THEN prefer_tac 2),
   1.130 -      if r > 0 then ALLGOALS (Goal.norm_hhf_tac ctxt) else all_tac, HEADGOAL atac,
   1.131 -      mk_induct_leverage_prem_prems_tac ctxt nn kks set_maps pre_set_defs]
   1.132 -  end;
   1.133 -
   1.134 -fun mk_induct_tac ctxt nn ns mss kkss ctr_defs ctor_induct' set_maps pre_set_defss =
   1.135 -  let val n = Integer.sum ns in
   1.136 -    unfold_thms_tac ctxt ctr_defs THEN HEADGOAL (rtac ctor_induct') THEN
   1.137 -    co_induct_inst_as_projs_tac ctxt 0 THEN
   1.138 -    EVERY (map4 (EVERY oooo map3 o mk_induct_discharge_prem_tac ctxt nn n set_maps) pre_set_defss
   1.139 -      mss (unflat mss (1 upto n)) kkss)
   1.140 -  end;
   1.141 -
   1.142 -fun mk_coinduct_same_ctr_tac ctxt rel_eqs pre_rel_def dtor_ctor ctr_def discs sels =
   1.143 -  hyp_subst_tac ctxt THEN'
   1.144 -  CONVERSION (hhf_concl_conv
   1.145 -    (Conv.top_conv (K (Conv.try_conv (Conv.rewr_conv ctr_def))) ctxt) ctxt) THEN'
   1.146 -  SELECT_GOAL (unfold_thms_tac ctxt (pre_rel_def :: dtor_ctor :: sels)) THEN'
   1.147 -  SELECT_GOAL (unfold_thms_tac ctxt (pre_rel_def :: dtor_ctor :: sels @ sum_prod_thms_rel)) THEN'
   1.148 -  (atac ORELSE' REPEAT o etac conjE THEN'
   1.149 -     full_simp_tac
   1.150 -       (ss_only (@{thm prod.inject} :: no_refl discs @ rel_eqs @ more_simp_thms) ctxt) THEN'
   1.151 -     REPEAT o etac conjE THEN_MAYBE' REPEAT o hyp_subst_tac ctxt THEN'
   1.152 -     REPEAT o (resolve_tac [refl, conjI] ORELSE' atac));
   1.153 -
   1.154 -fun mk_coinduct_distinct_ctrs_tac ctxt discs discs' =
   1.155 -  let
   1.156 -    val discs'' = map (perhaps (try (fn th => th RS @{thm notnotD}))) (discs @ discs')
   1.157 -      |> distinct Thm.eq_thm_prop;
   1.158 -  in
   1.159 -    hyp_subst_tac ctxt THEN' REPEAT o etac conjE THEN'
   1.160 -    full_simp_tac (ss_only (refl :: no_refl discs'' @ basic_simp_thms) ctxt)
   1.161 -  end;
   1.162 -
   1.163 -fun mk_coinduct_discharge_prem_tac ctxt rel_eqs' nn kk n pre_rel_def dtor_ctor exhaust ctr_defs
   1.164 -    discss selss =
   1.165 -  let val ks = 1 upto n in
   1.166 -    EVERY' ([rtac allI, rtac allI, rtac impI, select_prem_tac nn (dtac meta_spec) kk,
   1.167 -        dtac meta_spec, dtac meta_mp, atac, rtac exhaust, K (co_induct_inst_as_projs_tac ctxt 0),
   1.168 -        hyp_subst_tac ctxt] @
   1.169 -      map4 (fn k => fn ctr_def => fn discs => fn sels =>
   1.170 -        EVERY' ([rtac exhaust, K (co_induct_inst_as_projs_tac ctxt 1)] @
   1.171 -          map2 (fn k' => fn discs' =>
   1.172 -            if k' = k then
   1.173 -              mk_coinduct_same_ctr_tac ctxt rel_eqs' pre_rel_def dtor_ctor ctr_def discs sels
   1.174 -            else
   1.175 -              mk_coinduct_distinct_ctrs_tac ctxt discs discs') ks discss)) ks ctr_defs discss selss)
   1.176 -  end;
   1.177 -
   1.178 -fun mk_coinduct_tac ctxt rel_eqs' nn ns dtor_coinduct' pre_rel_defs dtor_ctors exhausts ctr_defss
   1.179 -    discsss selsss =
   1.180 -  HEADGOAL (rtac dtor_coinduct' THEN'
   1.181 -    EVERY' (map8 (mk_coinduct_discharge_prem_tac ctxt rel_eqs' nn)
   1.182 -      (1 upto nn) ns pre_rel_defs dtor_ctors exhausts ctr_defss discsss selsss));
   1.183 -
   1.184 -end;