src/HOL/BNF/Tools/bnf_fp_def_sugar_tactics.ML
author blanchet
Tue, 02 Oct 2012 01:00:18 +0200
changeset 49683 78a3d5006cf1
parent 49680 00290dc6bfad
child 51717 9e7d1c139569
permissions -rw-r--r--
continued changing type of corec type
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
49683
78a3d5006cf1 continued changing type of corec type
blanchet
parents: 49680
diff changeset
    17
  val mk_corec_like_tac: thm list -> thm list -> thm list -> thm list -> thm list -> thm -> thm ->
78a3d5006cf1 continued changing type of corec type
blanchet
parents: 49680
diff changeset
    18
    thm -> Proof.context -> tactic
49501
acc9635a644a renamed "fld"/"unf" to "ctor"/"dtor"
blanchet
parents: 49484
diff changeset
    19
  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
    20
    tactic
49504
df9b897fb254 renamed "iter"/"coiter" to "fold"/"unfold" (cf. Wadler)
blanchet
parents: 49501
diff changeset
    21
  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
    22
  val mk_exhaust_tac: Proof.context -> int -> thm list -> thm -> thm -> tactic
49127
f7326a0d7f19 implemented "mk_half_distinct_tac"
blanchet
parents: 49126
diff changeset
    23
  val mk_half_distinct_tac: Proof.context -> thm -> thm list -> tactic
49590
43e2d0e10876 added coinduction tactic
blanchet
parents: 49589
diff changeset
    24
  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
    25
    thm list -> thm -> thm list -> thm list list -> tactic
49126
1bbd7a37fc29 implemented "mk_inject_tac"
blanchet
parents: 49125
diff changeset
    26
  val mk_inject_tac: Proof.context -> thm -> thm -> tactic
49670
c7a034d01936 changed the type of the recursor for nested recursion
blanchet
parents: 49668
diff changeset
    27
  val mk_rec_like_tac: thm list -> thm list -> thm list -> thm list -> thm -> thm -> Proof.context
c7a034d01936 changed the type of the recursor for nested recursion
blanchet
parents: 49668
diff changeset
    28
    -> tactic
49123
263b0e330d8b more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
diff changeset
    29
end;
263b0e330d8b more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
diff changeset
    30
49636
b7256a88a84b renamed ML file in preparation for next step
blanchet
parents: 49591
diff changeset
    31
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
    32
struct
263b0e330d8b more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
diff changeset
    33
49125
5fc5211cf104 implemented "mk_exhaust_tac"
blanchet
parents: 49123
diff changeset
    34
open BNF_Tactics
49123
263b0e330d8b more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
diff changeset
    35
open BNF_Util
49457
1d2825673cec renamed "bnf_fp_util.ML" to "bnf_fp.ML"
blanchet
parents: 49436
diff changeset
    36
open BNF_FP
49123
263b0e330d8b more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
diff changeset
    37
49590
43e2d0e10876 added coinduction tactic
blanchet
parents: 49589
diff changeset
    38
val basic_simp_thms = @{thms simp_thms(7,8,12,14,22,24)};
43e2d0e10876 added coinduction tactic
blanchet
parents: 49589
diff changeset
    39
val more_simp_thms = basic_simp_thms @ @{thms simp_thms(11,15,16,21)};
43e2d0e10876 added coinduction tactic
blanchet
parents: 49589
diff changeset
    40
49683
78a3d5006cf1 continued changing type of corec type
blanchet
parents: 49680
diff changeset
    41
val sum_prod_thms_map = @{thms id_apply map_pair_simp prod.cases sum.cases sum_map.simps};
49585
5c4a12550491 generate high-level "maps", "sets", and "rels" properties
blanchet
parents: 49542
diff changeset
    42
val sum_prod_thms_set0 =
5c4a12550491 generate high-level "maps", "sets", and "rels" properties
blanchet
parents: 49542
diff changeset
    43
  @{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
    44
      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
    45
      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
    46
val sum_prod_thms_set = @{thms UN_compreh_eq_eq} @ sum_prod_thms_set0;
49643
71294d8c36fb simplified simpset
blanchet
parents: 49642
diff changeset
    47
val sum_prod_thms_rel = @{thms prod_rel_simp sum_rel_simps};
49368
df359ce891ac added induct tactic
blanchet
parents: 49363
diff changeset
    48
49590
43e2d0e10876 added coinduction tactic
blanchet
parents: 49589
diff changeset
    49
val ss_if_True_False = ss_only @{thms if_True if_False};
49589
71aa74965bc9 generalized tactic a bit
blanchet
parents: 49586
diff changeset
    50
49590
43e2d0e10876 added coinduction tactic
blanchet
parents: 49589
diff changeset
    51
fun mk_proj T k =
43e2d0e10876 added coinduction tactic
blanchet
parents: 49589
diff changeset
    52
  let val binders = binder_types T in
43e2d0e10876 added coinduction tactic
blanchet
parents: 49589
diff changeset
    53
    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
    54
  end;
43e2d0e10876 added coinduction tactic
blanchet
parents: 49589
diff changeset
    55
49668
blanchet
parents: 49665
diff changeset
    56
fun hhf_concl_conv cv ctxt ct =
blanchet
parents: 49665
diff changeset
    57
  (case Thm.term_of ct of
blanchet
parents: 49665
diff changeset
    58
    Const (@{const_name all}, _) $ Abs _ =>
blanchet
parents: 49665
diff changeset
    59
    Conv.arg_conv (Conv.abs_conv (hhf_concl_conv cv o snd) ctxt) ct
blanchet
parents: 49665
diff changeset
    60
  | _ => Conv.concl_conv ~1 cv ct);
blanchet
parents: 49665
diff changeset
    61
49590
43e2d0e10876 added coinduction tactic
blanchet
parents: 49589
diff changeset
    62
fun inst_as_projs ctxt k thm =
49368
df359ce891ac added induct tactic
blanchet
parents: 49363
diff changeset
    63
  let
49384
94ad5ba23541 took out one rotate_tac
blanchet
parents: 49383
diff changeset
    64
    val fs =
49368
df359ce891ac added induct tactic
blanchet
parents: 49363
diff changeset
    65
      Term.add_vars (prop_of thm) []
df359ce891ac added induct tactic
blanchet
parents: 49363
diff changeset
    66
      |> filter (fn (_, Type (@{type_name fun}, [_, T'])) => T' <> HOLogic.boolT | _ => false);
49384
94ad5ba23541 took out one rotate_tac
blanchet
parents: 49383
diff changeset
    67
    val cfs =
49590
43e2d0e10876 added coinduction tactic
blanchet
parents: 49589
diff changeset
    68
      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
    69
  in
49384
94ad5ba23541 took out one rotate_tac
blanchet
parents: 49383
diff changeset
    70
    Drule.cterm_instantiate cfs thm
49368
df359ce891ac added induct tactic
blanchet
parents: 49363
diff changeset
    71
  end;
df359ce891ac added induct tactic
blanchet
parents: 49363
diff changeset
    72
49590
43e2d0e10876 added coinduction tactic
blanchet
parents: 49589
diff changeset
    73
val inst_as_projs_tac = PRIMITIVE oo inst_as_projs;
49368
df359ce891ac added induct tactic
blanchet
parents: 49363
diff changeset
    74
49501
acc9635a644a renamed "fld"/"unf" to "ctor"/"dtor"
blanchet
parents: 49484
diff changeset
    75
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
    76
  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
    77
  (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
    78
   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
    79
   rtac refl) 1;
3c26e17b2849 implemented "mk_case_tac" -- and got rid of "cheat_tac"
blanchet
parents: 49127
diff changeset
    80
49501
acc9635a644a renamed "fld"/"unf" to "ctor"/"dtor"
blanchet
parents: 49484
diff changeset
    81
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
    82
  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
    83
  unfold_thms_tac ctxt @{thms all_prod_eq} THEN
49368
df359ce891ac added induct tactic
blanchet
parents: 49363
diff changeset
    84
  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
    85
    etac meta_mp, atac]) (1 upto n)) 1;
49125
5fc5211cf104 implemented "mk_exhaust_tac"
blanchet
parents: 49123
diff changeset
    86
49501
acc9635a644a renamed "fld"/"unf" to "ctor"/"dtor"
blanchet
parents: 49484
diff changeset
    87
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
    88
  (rtac iffI THEN'
263b0e330d8b more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
diff changeset
    89
   EVERY' (map3 (fn cTs => fn cx => fn th =>
263b0e330d8b more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
diff changeset
    90
     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
    91
     SELECT_GOAL (unfold_thms_tac ctxt [th]) THEN'
49501
acc9635a644a renamed "fld"/"unf" to "ctor"/"dtor"
blanchet
parents: 49484
diff changeset
    92
     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
    93
49501
acc9635a644a renamed "fld"/"unf" to "ctor"/"dtor"
blanchet
parents: 49484
diff changeset
    94
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
    95
  unfold_thms_tac ctxt (ctor_inject :: @{thms sum.inject} @ ctr_defs) THEN
49127
f7326a0d7f19 implemented "mk_half_distinct_tac"
blanchet
parents: 49126
diff changeset
    96
  rtac @{thm sum.distinct(1)} 1;
f7326a0d7f19 implemented "mk_half_distinct_tac"
blanchet
parents: 49126
diff changeset
    97
49501
acc9635a644a renamed "fld"/"unf" to "ctor"/"dtor"
blanchet
parents: 49484
diff changeset
    98
fun mk_inject_tac ctxt ctr_def ctor_inject =
49504
df9b897fb254 renamed "iter"/"coiter" to "fold"/"unfold" (cf. Wadler)
blanchet
parents: 49501
diff changeset
    99
  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
   100
  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
   101
49585
5c4a12550491 generate high-level "maps", "sets", and "rels" properties
blanchet
parents: 49542
diff changeset
   102
(*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
   103
val rec_like_unfold_thms =
49672
902b24e0ffb4 fixed recursor definition for datatypes with inner products (e.g. "'a trm" from the lambda-term example)
blanchet
parents: 49671
diff changeset
   104
  @{thms comp_def convol_def fst_conv id_def map_pair_simp prod_case_Pair_iden snd_conv split_conv
49670
c7a034d01936 changed the type of the recursor for nested recursion
blanchet
parents: 49668
diff changeset
   105
      sum.simps(5,6) sum_map.simps unit_case_Unity};
49205
674f04c737e0 implemented "mk_iter_or_rec_tac"
blanchet
parents: 49161
diff changeset
   106
49670
c7a034d01936 changed the type of the recursor for nested recursion
blanchet
parents: 49668
diff changeset
   107
fun mk_rec_like_tac pre_map_defs map_comp's map_ids'' rec_like_defs ctor_rec_like ctr_def ctxt =
c7a034d01936 changed the type of the recursor for nested recursion
blanchet
parents: 49668
diff changeset
   108
  unfold_thms_tac ctxt (ctr_def :: ctor_rec_like :: rec_like_defs @ pre_map_defs @ map_comp's @
c7a034d01936 changed the type of the recursor for nested recursion
blanchet
parents: 49668
diff changeset
   109
    map_ids'' @ rec_like_unfold_thms) THEN rtac refl 1;
49205
674f04c737e0 implemented "mk_iter_or_rec_tac"
blanchet
parents: 49161
diff changeset
   110
49683
78a3d5006cf1 continued changing type of corec type
blanchet
parents: 49680
diff changeset
   111
(*TODO: sum_case_if needed?*)
78a3d5006cf1 continued changing type of corec type
blanchet
parents: 49680
diff changeset
   112
val corec_like_unfold_thms =
78a3d5006cf1 continued changing type of corec type
blanchet
parents: 49680
diff changeset
   113
  @{thms id_def ident_o_ident sum_case_if sum_case_o_inj} @ sum_prod_thms_map;
78a3d5006cf1 continued changing type of corec type
blanchet
parents: 49680
diff changeset
   114
78a3d5006cf1 continued changing type of corec type
blanchet
parents: 49680
diff changeset
   115
fun mk_corec_like_tac corec_like_defs map_comps'' map_comp's map_ids'' map_if_distribs
78a3d5006cf1 continued changing type of corec type
blanchet
parents: 49680
diff changeset
   116
    ctor_dtor_corec_like pre_map_def ctr_def ctxt =
49504
df9b897fb254 renamed "iter"/"coiter" to "fold"/"unfold" (cf. Wadler)
blanchet
parents: 49501
diff changeset
   117
  unfold_thms_tac ctxt (ctr_def :: corec_like_defs) THEN
49680
00290dc6bfad made (co)rec tactic more robust when the simplifier succeeds early
blanchet
parents: 49672
diff changeset
   118
  (rtac (ctor_dtor_corec_like RS trans) THEN' asm_simp_tac ss_if_True_False) 1 THEN_MAYBE
49683
78a3d5006cf1 continued changing type of corec type
blanchet
parents: 49680
diff changeset
   119
  (unfold_thms_tac ctxt (pre_map_def :: map_comp's @ map_comps'' @ map_ids'' @ map_if_distribs @
78a3d5006cf1 continued changing type of corec type
blanchet
parents: 49680
diff changeset
   120
    corec_like_unfold_thms) THEN
49680
00290dc6bfad made (co)rec tactic more robust when the simplifier succeeds early
blanchet
parents: 49672
diff changeset
   121
   (rtac refl ORELSE' rtac (@{thm unit_eq} RS arg_cong)) 1);
49213
975ccb0130cb some work on coiter tactic
blanchet
parents: 49211
diff changeset
   122
49504
df9b897fb254 renamed "iter"/"coiter" to "fold"/"unfold" (cf. Wadler)
blanchet
parents: 49501
diff changeset
   123
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
   124
  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
   125
      case_split_tac 1 THEN unfold_thms_tac ctxt [corec_like_thm] THEN
49590
43e2d0e10876 added coinduction tactic
blanchet
parents: 49589
diff changeset
   126
      asm_simp_tac (ss_only basic_simp_thms) 1 THEN
49484
0194a18f80cf finished "disc_coiter_iff" etc. generation
blanchet
parents: 49482
diff changeset
   127
      (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
   128
    (map rtac case_splits' @ [K all_tac]) corec_likes discs);
49482
e6d6869eed08 generate coiter_iff and corec_iff theorems
blanchet
parents: 49463
diff changeset
   129
49429
64ac3471005a cleaner way of dealing with the set functions of sums and products
blanchet
parents: 49428
diff changeset
   130
val solve_prem_prem_tac =
64ac3471005a cleaner way of dealing with the set functions of sums and products
blanchet
parents: 49428
diff changeset
   131
  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
   132
    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
   133
  (rtac refl ORELSE' atac ORELSE' rtac @{thm singletonI});
49426
6d05b8452cf3 got rid of one "auto" in induction tactic
blanchet
parents: 49391
diff changeset
   134
49429
64ac3471005a cleaner way of dealing with the set functions of sums and products
blanchet
parents: 49428
diff changeset
   135
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
   136
  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
   137
     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
   138
     solve_prem_prem_tac]) (rev kks)) 1;
49368
df359ce891ac added induct tactic
blanchet
parents: 49363
diff changeset
   139
49429
64ac3471005a cleaner way of dealing with the set functions of sums and products
blanchet
parents: 49428
diff changeset
   140
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
   141
  let val r = length kks in
49391
3a96797fd53e tuned induction tactic
blanchet
parents: 49389
diff changeset
   142
    EVERY' [select_prem_tac n (rotate_tac 1) k, rotate_tac ~1, hyp_subst_tac,
3a96797fd53e tuned induction tactic
blanchet
parents: 49389
diff changeset
   143
      REPEAT_DETERM_N m o (dtac meta_spec THEN' rotate_tac ~1)] 1 THEN
3a96797fd53e tuned induction tactic
blanchet
parents: 49389
diff changeset
   144
    EVERY [REPEAT_DETERM_N r
3a96797fd53e tuned induction tactic
blanchet
parents: 49389
diff changeset
   145
        (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
   146
      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
   147
      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
   148
  end;
49368
df359ce891ac added induct tactic
blanchet
parents: 49363
diff changeset
   149
49590
43e2d0e10876 added coinduction tactic
blanchet
parents: 49589
diff changeset
   150
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
   151
  let val n = Integer.sum ns in
43e2d0e10876 added coinduction tactic
blanchet
parents: 49589
diff changeset
   152
    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
   153
    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
   154
      pre_set_defss mss (unflat mss (1 upto n)) kkss)
49368
df359ce891ac added induct tactic
blanchet
parents: 49363
diff changeset
   155
  end;
df359ce891ac added induct tactic
blanchet
parents: 49363
diff changeset
   156
49591
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
   157
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
   158
  hyp_subst_tac THEN'
49665
869c7a2e2945 tuned tactic
traytel
parents: 49643
diff changeset
   159
  CONVERSION (hhf_concl_conv
869c7a2e2945 tuned tactic
traytel
parents: 49643
diff changeset
   160
    (Conv.top_conv (K (Conv.try_conv (Conv.rewr_conv ctr_def))) ctxt) ctxt) 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
   161
  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
   162
  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
   163
  (atac ORELSE' REPEAT o etac conjE THEN'
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
   164
     full_simp_tac
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
   165
       (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
   166
     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
   167
49591
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
   168
fun mk_coinduct_distinct_ctrs discs discs' =
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
   169
  hyp_subst_tac THEN' REPEAT o etac conjE THEN'
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
   170
  full_simp_tac (ss_only (refl :: no_refl (discs @ discs') @ basic_simp_thms));
49590
43e2d0e10876 added coinduction tactic
blanchet
parents: 49589
diff changeset
   171
49591
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
   172
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
   173
    discss selss =
49590
43e2d0e10876 added coinduction tactic
blanchet
parents: 49589
diff changeset
   174
  let val ks = 1 upto n in
49591
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
   175
    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
   176
        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
   177
      map4 (fn k => fn ctr_def => fn discs => fn sels =>
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
   178
        EVERY' ([rtac exhaust, K (inst_as_projs_tac ctxt 2)] @
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
   179
          map2 (fn k' => fn discs' =>
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
   180
            if k' = k then
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
   181
              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
   182
            else
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
   183
              mk_coinduct_distinct_ctrs discs discs') ks discss)) ks ctr_defs discss selss)
49590
43e2d0e10876 added coinduction tactic
blanchet
parents: 49589
diff changeset
   184
  end;
43e2d0e10876 added coinduction tactic
blanchet
parents: 49589
diff changeset
   185
49591
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
   186
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
   187
    discsss selsss =
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
   188
  (rtac dtor_coinduct' THEN'
91b228e26348 generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents: 49590
diff changeset
   189
   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
   190
     (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
   191
49123
263b0e330d8b more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
diff changeset
   192
end;