src/HOL/BNF/Tools/bnf_fp_def_sugar_tactics.ML
author blanchet
Fri, 28 Sep 2012 15:14:11 +0200
changeset 49642 9f884142334c
parent 49639 204bd497aa4c
child 49643 71294d8c36fb
permissions -rw-r--r--
fixed simplification of prod and sum relators to avoid issues with e.g. codata ('a, 'b) k = K "'a + 'b"
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49636
b7256a88a84b renamed ML file in preparation for next step
blanchet
parents: 49591
diff changeset
     1
(*  Title:      HOL/BNF/Tools/bnf_fp_def_sugar_tactics.ML
49123
263b0e330d8b more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
diff changeset
     2
    Author:     Jasmin Blanchette, TU Muenchen
263b0e330d8b more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
diff changeset
     3
    Copyright   2012
263b0e330d8b more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
diff changeset
     4
49389
blanchet
parents: 49385
diff changeset
     5
Tactics for datatype and codatatype sugar.
49123
263b0e330d8b more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
diff changeset
     6
*)
263b0e330d8b more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
diff changeset
     7
49636
b7256a88a84b renamed ML file in preparation for next step
blanchet
parents: 49591
diff changeset
     8
signature BNF_FP_DEF_SUGAR_TACTICS =
49123
263b0e330d8b more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
diff changeset
     9
sig
49585
5c4a12550491 generate high-level "maps", "sets", and "rels" properties
blanchet
parents: 49542
diff changeset
    10
  val sum_prod_thms_map: thm list
5c4a12550491 generate high-level "maps", "sets", and "rels" properties
blanchet
parents: 49542
diff changeset
    11
  val sum_prod_thms_set: thm list
5c4a12550491 generate high-level "maps", "sets", and "rels" properties
blanchet
parents: 49542
diff changeset
    12
  val sum_prod_thms_rel: thm list
5c4a12550491 generate high-level "maps", "sets", and "rels" properties
blanchet
parents: 49542
diff changeset
    13
49130
3c26e17b2849 implemented "mk_case_tac" -- and got rid of "cheat_tac"
blanchet
parents: 49127
diff changeset
    14
  val mk_case_tac: Proof.context -> int -> int -> int -> thm -> thm -> thm -> tactic
49591
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
    15
  val mk_coinduct_tac: Proof.context -> thm list -> int -> int list -> thm -> thm list ->
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
    16
    thm list -> thm list -> thm list list -> thm list list list -> thm list list list -> tactic
49504
df9b897fb254 renamed "iter"/"coiter" to "fold"/"unfold" (cf. Wadler)
blanchet
parents: 49501
diff changeset
    17
  val mk_corec_like_tac: thm list -> thm list -> thm -> thm -> thm -> Proof.context -> tactic
49501
acc9635a644a renamed "fld"/"unf" to "ctor"/"dtor"
blanchet
parents: 49484
diff changeset
    18
  val mk_ctor_iff_dtor_tac: Proof.context -> ctyp option list -> cterm -> cterm -> thm -> thm ->
acc9635a644a renamed "fld"/"unf" to "ctor"/"dtor"
blanchet
parents: 49484
diff changeset
    19
    tactic
49504
df9b897fb254 renamed "iter"/"coiter" to "fold"/"unfold" (cf. Wadler)
blanchet
parents: 49501
diff changeset
    20
  val mk_disc_corec_like_iff_tac: thm list -> thm list -> thm list -> Proof.context -> tactic
49161
a8e74375d971 fixed (n + 1)st bug in "mk_exhaust_tac" -- arose with uncurried constructors
blanchet
parents: 49160
diff changeset
    21
  val mk_exhaust_tac: Proof.context -> int -> thm list -> thm -> thm -> tactic
49127
f7326a0d7f19 implemented "mk_half_distinct_tac"
blanchet
parents: 49126
diff changeset
    22
  val mk_half_distinct_tac: Proof.context -> thm -> thm list -> tactic
49590
43e2d0e10876 added coinduction tactic
blanchet
parents: 49589
diff changeset
    23
  val mk_induct_tac: Proof.context -> int -> int list -> int list list -> int list list list ->
43e2d0e10876 added coinduction tactic
blanchet
parents: 49589
diff changeset
    24
    thm list -> thm -> thm list -> thm list list -> tactic
49126
1bbd7a37fc29 implemented "mk_inject_tac"
blanchet
parents: 49125
diff changeset
    25
  val mk_inject_tac: Proof.context -> thm -> thm -> tactic
49504
df9b897fb254 renamed "iter"/"coiter" to "fold"/"unfold" (cf. Wadler)
blanchet
parents: 49501
diff changeset
    26
  val mk_rec_like_tac: thm list -> thm list -> thm list -> thm -> thm -> Proof.context -> tactic
49123
263b0e330d8b more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
diff changeset
    27
end;
263b0e330d8b more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
diff changeset
    28
49636
b7256a88a84b renamed ML file in preparation for next step
blanchet
parents: 49591
diff changeset
    29
structure BNF_FP_Def_Sugar_Tactics : BNF_FP_DEF_SUGAR_TACTICS =
49123
263b0e330d8b more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
diff changeset
    30
struct
263b0e330d8b more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
diff changeset
    31
49125
5fc5211cf104 implemented "mk_exhaust_tac"
blanchet
parents: 49123
diff changeset
    32
open BNF_Tactics
49123
263b0e330d8b more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
diff changeset
    33
open BNF_Util
49457
1d2825673cec renamed "bnf_fp_util.ML" to "bnf_fp.ML"
blanchet
parents: 49436
diff changeset
    34
open BNF_FP
49123
263b0e330d8b more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
diff changeset
    35
49590
43e2d0e10876 added coinduction tactic
blanchet
parents: 49589
diff changeset
    36
val basic_simp_thms = @{thms simp_thms(7,8,12,14,22,24)};
43e2d0e10876 added coinduction tactic
blanchet
parents: 49589
diff changeset
    37
val more_simp_thms = basic_simp_thms @ @{thms simp_thms(11,15,16,21)};
43e2d0e10876 added coinduction tactic
blanchet
parents: 49589
diff changeset
    38
49585
5c4a12550491 generate high-level "maps", "sets", and "rels" properties
blanchet
parents: 49542
diff changeset
    39
val sum_prod_thms_map = @{thms id_apply map_pair_simp sum_map.simps prod.cases};
5c4a12550491 generate high-level "maps", "sets", and "rels" properties
blanchet
parents: 49542
diff changeset
    40
val sum_prod_thms_set0 =
5c4a12550491 generate high-level "maps", "sets", and "rels" properties
blanchet
parents: 49542
diff changeset
    41
  @{thms SUP_empty Sup_empty Sup_insert UN_insert Un_empty_left Un_empty_right Un_iff
5c4a12550491 generate high-level "maps", "sets", and "rels" properties
blanchet
parents: 49542
diff changeset
    42
      Union_Un_distrib collect_def[abs_def] image_def o_apply map_pair_simp
5c4a12550491 generate high-level "maps", "sets", and "rels" properties
blanchet
parents: 49542
diff changeset
    43
      mem_Collect_eq mem_UN_compreh_eq prod_set_simps sum_map.simps sum_set_simps};
5c4a12550491 generate high-level "maps", "sets", and "rels" properties
blanchet
parents: 49542
diff changeset
    44
val sum_prod_thms_set = @{thms UN_compreh_eq_eq} @ sum_prod_thms_set0;
49642
9f884142334c fixed simplification of prod and sum relators to avoid issues with e.g. codata ('a, 'b) k = K "'a + 'b"
blanchet
parents: 49639
diff changeset
    45
(* FIXME: Need "prod.cases" and "sum.cases"? And why "sum.inject" but no "prod.inject"? *)
49591
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
    46
val sum_prod_thms_rel =
49642
9f884142334c fixed simplification of prod and sum relators to avoid issues with e.g. codata ('a, 'b) k = K "'a + 'b"
blanchet
parents: 49639
diff changeset
    47
  @{thms prod.cases prod_rel_simp sum.cases sum_rel_simps
49591
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
    48
      sum.inject sum.distinct[THEN eq_False[THEN iffD2]]};
49368
df359ce891ac added induct tactic
blanchet
parents: 49363
diff changeset
    49
49590
43e2d0e10876 added coinduction tactic
blanchet
parents: 49589
diff changeset
    50
val ss_if_True_False = ss_only @{thms if_True if_False};
49589
71aa74965bc9 generalized tactic a bit
blanchet
parents: 49586
diff changeset
    51
49590
43e2d0e10876 added coinduction tactic
blanchet
parents: 49589
diff changeset
    52
fun mk_proj T k =
43e2d0e10876 added coinduction tactic
blanchet
parents: 49589
diff changeset
    53
  let val binders = binder_types T in
43e2d0e10876 added coinduction tactic
blanchet
parents: 49589
diff changeset
    54
    fold_rev (fn T => fn t => Abs (Name.uu, T, t)) binders (Bound (length binders - k))
43e2d0e10876 added coinduction tactic
blanchet
parents: 49589
diff changeset
    55
  end;
43e2d0e10876 added coinduction tactic
blanchet
parents: 49589
diff changeset
    56
43e2d0e10876 added coinduction tactic
blanchet
parents: 49589
diff changeset
    57
fun inst_as_projs ctxt k thm =
49368
df359ce891ac added induct tactic
blanchet
parents: 49363
diff changeset
    58
  let
49384
94ad5ba23541 took out one rotate_tac
blanchet
parents: 49383
diff changeset
    59
    val fs =
49368
df359ce891ac added induct tactic
blanchet
parents: 49363
diff changeset
    60
      Term.add_vars (prop_of thm) []
df359ce891ac added induct tactic
blanchet
parents: 49363
diff changeset
    61
      |> filter (fn (_, Type (@{type_name fun}, [_, T'])) => T' <> HOLogic.boolT | _ => false);
49384
94ad5ba23541 took out one rotate_tac
blanchet
parents: 49383
diff changeset
    62
    val cfs =
49590
43e2d0e10876 added coinduction tactic
blanchet
parents: 49589
diff changeset
    63
      map (fn f as (_, T) => (certify ctxt (Var f), certify ctxt (mk_proj T k))) fs;
49368
df359ce891ac added induct tactic
blanchet
parents: 49363
diff changeset
    64
  in
49384
94ad5ba23541 took out one rotate_tac
blanchet
parents: 49383
diff changeset
    65
    Drule.cterm_instantiate cfs thm
49368
df359ce891ac added induct tactic
blanchet
parents: 49363
diff changeset
    66
  end;
df359ce891ac added induct tactic
blanchet
parents: 49363
diff changeset
    67
49590
43e2d0e10876 added coinduction tactic
blanchet
parents: 49589
diff changeset
    68
val inst_as_projs_tac = PRIMITIVE oo inst_as_projs;
49368
df359ce891ac added induct tactic
blanchet
parents: 49363
diff changeset
    69
49501
acc9635a644a renamed "fld"/"unf" to "ctor"/"dtor"
blanchet
parents: 49484
diff changeset
    70
fun mk_case_tac ctxt n k m case_def ctr_def dtor_ctor =
49504
df9b897fb254 renamed "iter"/"coiter" to "fold"/"unfold" (cf. Wadler)
blanchet
parents: 49501
diff changeset
    71
  unfold_thms_tac ctxt [case_def, ctr_def, dtor_ctor] THEN
49264
9059e0dbdbc1 implemented and use "mk_sum_casesN_balanced"
blanchet
parents: 49263
diff changeset
    72
  (rtac (mk_sum_casesN_balanced n k RS ssubst) THEN'
49130
3c26e17b2849 implemented "mk_case_tac" -- and got rid of "cheat_tac"
blanchet
parents: 49127
diff changeset
    73
   REPEAT_DETERM_N (Int.max (0, m - 1)) o rtac (@{thm split} RS ssubst) THEN'
3c26e17b2849 implemented "mk_case_tac" -- and got rid of "cheat_tac"
blanchet
parents: 49127
diff changeset
    74
   rtac refl) 1;
3c26e17b2849 implemented "mk_case_tac" -- and got rid of "cheat_tac"
blanchet
parents: 49127
diff changeset
    75
49501
acc9635a644a renamed "fld"/"unf" to "ctor"/"dtor"
blanchet
parents: 49484
diff changeset
    76
fun mk_exhaust_tac ctxt n ctr_defs ctor_iff_dtor sumEN' =
49504
df9b897fb254 renamed "iter"/"coiter" to "fold"/"unfold" (cf. Wadler)
blanchet
parents: 49501
diff changeset
    77
  unfold_thms_tac ctxt (ctor_iff_dtor :: ctr_defs) THEN rtac sumEN' 1 THEN
df9b897fb254 renamed "iter"/"coiter" to "fold"/"unfold" (cf. Wadler)
blanchet
parents: 49501
diff changeset
    78
  unfold_thms_tac ctxt @{thms all_prod_eq} THEN
49368
df359ce891ac added induct tactic
blanchet
parents: 49363
diff changeset
    79
  EVERY' (maps (fn k => [select_prem_tac n (rotate_tac 1) k, REPEAT_DETERM o dtac meta_spec,
df359ce891ac added induct tactic
blanchet
parents: 49363
diff changeset
    80
    etac meta_mp, atac]) (1 upto n)) 1;
49125
5fc5211cf104 implemented "mk_exhaust_tac"
blanchet
parents: 49123
diff changeset
    81
49501
acc9635a644a renamed "fld"/"unf" to "ctor"/"dtor"
blanchet
parents: 49484
diff changeset
    82
fun mk_ctor_iff_dtor_tac ctxt cTs cctor cdtor ctor_dtor dtor_ctor =
49123
263b0e330d8b more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
diff changeset
    83
  (rtac iffI THEN'
263b0e330d8b more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
diff changeset
    84
   EVERY' (map3 (fn cTs => fn cx => fn th =>
263b0e330d8b more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
diff changeset
    85
     dtac (Drule.instantiate' cTs [NONE, NONE, SOME cx] arg_cong) THEN'
49504
df9b897fb254 renamed "iter"/"coiter" to "fold"/"unfold" (cf. Wadler)
blanchet
parents: 49501
diff changeset
    86
     SELECT_GOAL (unfold_thms_tac ctxt [th]) THEN'
49501
acc9635a644a renamed "fld"/"unf" to "ctor"/"dtor"
blanchet
parents: 49484
diff changeset
    87
     atac) [rev cTs, cTs] [cdtor, cctor] [dtor_ctor, ctor_dtor])) 1;
49123
263b0e330d8b more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
diff changeset
    88
49501
acc9635a644a renamed "fld"/"unf" to "ctor"/"dtor"
blanchet
parents: 49484
diff changeset
    89
fun mk_half_distinct_tac ctxt ctor_inject ctr_defs =
49504
df9b897fb254 renamed "iter"/"coiter" to "fold"/"unfold" (cf. Wadler)
blanchet
parents: 49501
diff changeset
    90
  unfold_thms_tac ctxt (ctor_inject :: @{thms sum.inject} @ ctr_defs) THEN
49127
f7326a0d7f19 implemented "mk_half_distinct_tac"
blanchet
parents: 49126
diff changeset
    91
  rtac @{thm sum.distinct(1)} 1;
f7326a0d7f19 implemented "mk_half_distinct_tac"
blanchet
parents: 49126
diff changeset
    92
49501
acc9635a644a renamed "fld"/"unf" to "ctor"/"dtor"
blanchet
parents: 49484
diff changeset
    93
fun mk_inject_tac ctxt ctr_def ctor_inject =
49504
df9b897fb254 renamed "iter"/"coiter" to "fold"/"unfold" (cf. Wadler)
blanchet
parents: 49501
diff changeset
    94
  unfold_thms_tac ctxt [ctr_def] THEN rtac (ctor_inject RS ssubst) 1 THEN
df9b897fb254 renamed "iter"/"coiter" to "fold"/"unfold" (cf. Wadler)
blanchet
parents: 49501
diff changeset
    95
  unfold_thms_tac ctxt @{thms sum.inject Pair_eq conj_assoc} THEN rtac refl 1;
49126
1bbd7a37fc29 implemented "mk_inject_tac"
blanchet
parents: 49125
diff changeset
    96
49585
5c4a12550491 generate high-level "maps", "sets", and "rels" properties
blanchet
parents: 49542
diff changeset
    97
(*TODO: Try "sum_prod_thms_map" here, enriched with a few theorems*)
49504
df9b897fb254 renamed "iter"/"coiter" to "fold"/"unfold" (cf. Wadler)
blanchet
parents: 49501
diff changeset
    98
val rec_like_unfold_thms =
49539
be6cbf960aa7 fixed bug in "fold" tactic with nested products (beyond the sum of product corresponding to constructors)
blanchet
parents: 49510
diff changeset
    99
  @{thms comp_def convol_def id_apply map_pair_def prod_case_Pair_iden sum.simps(5,6) sum_map.simps
be6cbf960aa7 fixed bug in "fold" tactic with nested products (beyond the sum of product corresponding to constructors)
blanchet
parents: 49510
diff changeset
   100
      split_conv unit_case_Unity};
49205
674f04c737e0 implemented "mk_iter_or_rec_tac"
blanchet
parents: 49161
diff changeset
   101
49504
df9b897fb254 renamed "iter"/"coiter" to "fold"/"unfold" (cf. Wadler)
blanchet
parents: 49501
diff changeset
   102
fun mk_rec_like_tac pre_map_defs map_ids rec_like_defs ctor_rec_like ctr_def ctxt =
df9b897fb254 renamed "iter"/"coiter" to "fold"/"unfold" (cf. Wadler)
blanchet
parents: 49501
diff changeset
   103
  unfold_thms_tac ctxt (ctr_def :: ctor_rec_like :: rec_like_defs @ pre_map_defs @ map_ids @
df9b897fb254 renamed "iter"/"coiter" to "fold"/"unfold" (cf. Wadler)
blanchet
parents: 49501
diff changeset
   104
    rec_like_unfold_thms) THEN unfold_thms_tac ctxt @{thms id_def} THEN rtac refl 1;
49205
674f04c737e0 implemented "mk_iter_or_rec_tac"
blanchet
parents: 49161
diff changeset
   105
49504
df9b897fb254 renamed "iter"/"coiter" to "fold"/"unfold" (cf. Wadler)
blanchet
parents: 49501
diff changeset
   106
fun mk_corec_like_tac corec_like_defs map_ids ctor_dtor_corec_like pre_map_def ctr_def ctxt =
df9b897fb254 renamed "iter"/"coiter" to "fold"/"unfold" (cf. Wadler)
blanchet
parents: 49501
diff changeset
   107
  unfold_thms_tac ctxt (ctr_def :: corec_like_defs) THEN
49638
e592e9822ae4 tuned tactic
traytel
parents: 49636
diff changeset
   108
  (rtac (ctor_dtor_corec_like RS trans) THEN' asm_simp_tac ss_if_True_False) 1 THEN
49585
5c4a12550491 generate high-level "maps", "sets", and "rels" properties
blanchet
parents: 49542
diff changeset
   109
  unfold_thms_tac ctxt (pre_map_def :: sum_prod_thms_map @ map_ids) THEN
49504
df9b897fb254 renamed "iter"/"coiter" to "fold"/"unfold" (cf. Wadler)
blanchet
parents: 49501
diff changeset
   110
  unfold_thms_tac ctxt @{thms id_def} THEN
49639
204bd497aa4c tuned tactic
traytel
parents: 49638
diff changeset
   111
  (rtac refl ORELSE' rtac (@{thm unit_eq} RS arg_cong)) 1;
49213
975ccb0130cb some work on coiter tactic
blanchet
parents: 49211
diff changeset
   112
49504
df9b897fb254 renamed "iter"/"coiter" to "fold"/"unfold" (cf. Wadler)
blanchet
parents: 49501
diff changeset
   113
fun mk_disc_corec_like_iff_tac case_splits' corec_likes discs ctxt =
df9b897fb254 renamed "iter"/"coiter" to "fold"/"unfold" (cf. Wadler)
blanchet
parents: 49501
diff changeset
   114
  EVERY (map3 (fn case_split_tac => fn corec_like_thm => fn disc =>
df9b897fb254 renamed "iter"/"coiter" to "fold"/"unfold" (cf. Wadler)
blanchet
parents: 49501
diff changeset
   115
      case_split_tac 1 THEN unfold_thms_tac ctxt [corec_like_thm] THEN
49590
43e2d0e10876 added coinduction tactic
blanchet
parents: 49589
diff changeset
   116
      asm_simp_tac (ss_only basic_simp_thms) 1 THEN
49484
0194a18f80cf finished "disc_coiter_iff" etc. generation
blanchet
parents: 49482
diff changeset
   117
      (if is_refl disc then all_tac else rtac disc 1))
49504
df9b897fb254 renamed "iter"/"coiter" to "fold"/"unfold" (cf. Wadler)
blanchet
parents: 49501
diff changeset
   118
    (map rtac case_splits' @ [K all_tac]) corec_likes discs);
49482
e6d6869eed08 generate coiter_iff and corec_iff theorems
blanchet
parents: 49463
diff changeset
   119
49429
64ac3471005a cleaner way of dealing with the set functions of sums and products
blanchet
parents: 49428
diff changeset
   120
val solve_prem_prem_tac =
64ac3471005a cleaner way of dealing with the set functions of sums and products
blanchet
parents: 49428
diff changeset
   121
  REPEAT o (eresolve_tac @{thms bexE rev_bexI} ORELSE' rtac @{thm rev_bexI[OF UNIV_I]} ORELSE'
64ac3471005a cleaner way of dealing with the set functions of sums and products
blanchet
parents: 49428
diff changeset
   122
    hyp_subst_tac ORELSE' resolve_tac @{thms disjI1 disjI2}) THEN'
64ac3471005a cleaner way of dealing with the set functions of sums and products
blanchet
parents: 49428
diff changeset
   123
  (rtac refl ORELSE' atac ORELSE' rtac @{thm singletonI});
49426
6d05b8452cf3 got rid of one "auto" in induction tactic
blanchet
parents: 49391
diff changeset
   124
49429
64ac3471005a cleaner way of dealing with the set functions of sums and products
blanchet
parents: 49428
diff changeset
   125
fun mk_induct_leverage_prem_prems_tac ctxt nn kks set_natural's pre_set_defs =
64ac3471005a cleaner way of dealing with the set functions of sums and products
blanchet
parents: 49428
diff changeset
   126
  EVERY' (maps (fn kk => [select_prem_tac nn (dtac meta_spec) kk, etac meta_mp,
49585
5c4a12550491 generate high-level "maps", "sets", and "rels" properties
blanchet
parents: 49542
diff changeset
   127
     SELECT_GOAL (unfold_thms_tac ctxt (pre_set_defs @ set_natural's @ sum_prod_thms_set0)),
49429
64ac3471005a cleaner way of dealing with the set functions of sums and products
blanchet
parents: 49428
diff changeset
   128
     solve_prem_prem_tac]) (rev kks)) 1;
49368
df359ce891ac added induct tactic
blanchet
parents: 49363
diff changeset
   129
49429
64ac3471005a cleaner way of dealing with the set functions of sums and products
blanchet
parents: 49428
diff changeset
   130
fun mk_induct_discharge_prem_tac ctxt nn n set_natural's pre_set_defs m k kks =
64ac3471005a cleaner way of dealing with the set functions of sums and products
blanchet
parents: 49428
diff changeset
   131
  let val r = length kks in
49391
3a96797fd53e tuned induction tactic
blanchet
parents: 49389
diff changeset
   132
    EVERY' [select_prem_tac n (rotate_tac 1) k, rotate_tac ~1, hyp_subst_tac,
3a96797fd53e tuned induction tactic
blanchet
parents: 49389
diff changeset
   133
      REPEAT_DETERM_N m o (dtac meta_spec THEN' rotate_tac ~1)] 1 THEN
3a96797fd53e tuned induction tactic
blanchet
parents: 49389
diff changeset
   134
    EVERY [REPEAT_DETERM_N r
3a96797fd53e tuned induction tactic
blanchet
parents: 49389
diff changeset
   135
        (rotate_tac ~1 1 THEN dtac meta_mp 1 THEN rotate_tac 1 1 THEN prefer_tac 2),
3a96797fd53e tuned induction tactic
blanchet
parents: 49389
diff changeset
   136
      if r > 0 then PRIMITIVE Raw_Simplifier.norm_hhf else all_tac, atac 1,
49429
64ac3471005a cleaner way of dealing with the set functions of sums and products
blanchet
parents: 49428
diff changeset
   137
      mk_induct_leverage_prem_prems_tac ctxt nn kks set_natural's pre_set_defs]
49391
3a96797fd53e tuned induction tactic
blanchet
parents: 49389
diff changeset
   138
  end;
49368
df359ce891ac added induct tactic
blanchet
parents: 49363
diff changeset
   139
49590
43e2d0e10876 added coinduction tactic
blanchet
parents: 49589
diff changeset
   140
fun mk_induct_tac ctxt nn ns mss kkss ctr_defs ctor_induct' set_natural's pre_set_defss =
43e2d0e10876 added coinduction tactic
blanchet
parents: 49589
diff changeset
   141
  let val n = Integer.sum ns in
43e2d0e10876 added coinduction tactic
blanchet
parents: 49589
diff changeset
   142
    unfold_thms_tac ctxt ctr_defs THEN rtac ctor_induct' 1 THEN inst_as_projs_tac ctxt 1 THEN
49376
c6366fd0415a select the right premise in "mk_induct_discharge_prem_prems_tac" instead of relying on backtracking
blanchet
parents: 49375
diff changeset
   143
    EVERY (map4 (EVERY oooo map3 o mk_induct_discharge_prem_tac ctxt nn n set_natural's)
49429
64ac3471005a cleaner way of dealing with the set functions of sums and products
blanchet
parents: 49428
diff changeset
   144
      pre_set_defss mss (unflat mss (1 upto n)) kkss)
49368
df359ce891ac added induct tactic
blanchet
parents: 49363
diff changeset
   145
  end;
df359ce891ac added induct tactic
blanchet
parents: 49363
diff changeset
   146
49591
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
   147
fun mk_coinduct_same_ctr ctxt rel_eqs pre_rel_def dtor_ctor ctr_def discs sels =
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
   148
  hyp_subst_tac THEN'
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
   149
  subst_tac ctxt (SOME [1, 2]) [ctr_def] THEN'
49642
9f884142334c fixed simplification of prod and sum relators to avoid issues with e.g. codata ('a, 'b) k = K "'a + 'b"
blanchet
parents: 49639
diff changeset
   150
  SELECT_GOAL (unfold_thms_tac ctxt (pre_rel_def :: dtor_ctor :: sels)) THEN'
49591
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
   151
  SELECT_GOAL (unfold_thms_tac ctxt (pre_rel_def :: dtor_ctor :: sels @ sum_prod_thms_rel)) THEN'
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
   152
  (atac ORELSE' REPEAT o etac conjE THEN'
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
   153
     full_simp_tac
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
   154
       (ss_only (@{thm prod.inject} :: no_refl discs @ rel_eqs @ more_simp_thms)) THEN_MAYBE'
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
   155
     REPEAT o hyp_subst_tac THEN' REPEAT o rtac conjI THEN' REPEAT o rtac refl);
49590
43e2d0e10876 added coinduction tactic
blanchet
parents: 49589
diff changeset
   156
49591
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
   157
fun mk_coinduct_distinct_ctrs discs discs' =
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
   158
  hyp_subst_tac THEN' REPEAT o etac conjE THEN'
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
   159
  full_simp_tac (ss_only (refl :: no_refl (discs @ discs') @ basic_simp_thms));
49590
43e2d0e10876 added coinduction tactic
blanchet
parents: 49589
diff changeset
   160
49591
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
   161
fun mk_coinduct_discharge_prem_tac ctxt rel_eqs' nn kk n pre_rel_def dtor_ctor exhaust ctr_defs
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
   162
    discss selss =
49590
43e2d0e10876 added coinduction tactic
blanchet
parents: 49589
diff changeset
   163
  let val ks = 1 upto n in
49591
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
   164
    EVERY' ([rtac allI, rtac allI, rtac impI, select_prem_tac nn (dtac meta_spec) kk, dtac
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
   165
        meta_spec, dtac meta_mp, atac, rtac exhaust, K (inst_as_projs_tac ctxt 1), hyp_subst_tac] @
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
   166
      map4 (fn k => fn ctr_def => fn discs => fn sels =>
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
   167
        EVERY' ([rtac exhaust, K (inst_as_projs_tac ctxt 2)] @
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
   168
          map2 (fn k' => fn discs' =>
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
   169
            if k' = k then
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
   170
              mk_coinduct_same_ctr ctxt rel_eqs' pre_rel_def dtor_ctor ctr_def discs sels
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
   171
            else
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
   172
              mk_coinduct_distinct_ctrs discs discs') ks discss)) ks ctr_defs discss selss)
49590
43e2d0e10876 added coinduction tactic
blanchet
parents: 49589
diff changeset
   173
  end;
43e2d0e10876 added coinduction tactic
blanchet
parents: 49589
diff changeset
   174
49591
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
   175
fun mk_coinduct_tac ctxt rel_eqs' nn ns dtor_coinduct' pre_rel_defs dtor_ctors exhausts ctr_defss
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
   176
    discsss selsss =
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
   177
  (rtac dtor_coinduct' THEN'
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
   178
   EVERY' (map8 (mk_coinduct_discharge_prem_tac ctxt rel_eqs' nn)
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
   179
     (1 upto nn) ns pre_rel_defs dtor_ctors exhausts ctr_defss discsss selsss)) 1;
49590
43e2d0e10876 added coinduction tactic
blanchet
parents: 49589
diff changeset
   180
49123
263b0e330d8b more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
diff changeset
   181
end;