author | blanchet |
Mon, 04 Nov 2013 16:53:43 +0100 | |
changeset 54246 | 8fdb4dc08ed1 |
parent 54174 | src/HOL/BNF/Tools/bnf_fp_rec_sugar_tactics.ML@c6291ae7cd18 |
child 54279 | 3ffb74b52ed6 |
permissions | -rw-r--r-- |
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54174
diff
changeset
|
1 |
(* Title: HOL/BNF/Tools/bnf_gfp_rec_sugar_tactics.ML |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
2 |
Author: Jasmin Blanchette, TU Muenchen |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
3 |
Copyright 2013 |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
4 |
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54174
diff
changeset
|
5 |
Tactics for corecursor sugar. |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
6 |
*) |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
7 |
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54174
diff
changeset
|
8 |
signature BNF_GFP_REC_SUGAR_TACTICS = |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
9 |
sig |
54044
93ab44e992ae
strengthened tactic (primcorec_sel_tac) + tuning
blanchet
parents:
54043
diff
changeset
|
10 |
val mk_primcorec_assumption_tac: Proof.context -> thm list -> int -> tactic |
54174 | 11 |
val mk_primcorec_code_of_raw_code_tac: Proof.context -> thm list -> thm list -> thm -> tactic |
53722
e176d6d3345f
generate more theorems (e.g. for types with only one constructor)
panny
parents:
53720
diff
changeset
|
12 |
val mk_primcorec_ctr_of_dtr_tac: Proof.context -> int -> thm -> thm option -> thm list -> tactic |
53693 | 13 |
val mk_primcorec_disc_tac: Proof.context -> thm list -> thm -> int -> int -> thm list list list -> |
14 |
tactic |
|
54043 | 15 |
val mk_primcorec_raw_code_of_ctr_tac: Proof.context -> thm list -> thm list -> thm list -> |
16 |
thm list -> int list -> thm list -> tactic |
|
53910 | 17 |
val mk_primcorec_sel_tac: Proof.context -> thm list -> thm list -> thm list -> thm list -> |
18 |
thm list -> thm list -> thm list -> thm -> int -> int -> thm list list list -> tactic |
|
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
19 |
end; |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
20 |
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
54174
diff
changeset
|
21 |
structure BNF_GFP_Rec_Sugar_Tactics : BNF_GFP_REC_SUGAR_TACTICS = |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
22 |
struct |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
23 |
|
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
24 |
open BNF_Util |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
25 |
open BNF_Tactics |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
26 |
|
53905 | 27 |
val falseEs = @{thms not_TrueE FalseE}; |
54174 | 28 |
val Let_def = @{thm Let_def}; |
53910 | 29 |
val neq_eq_eq_contradict = @{thm neq_eq_eq_contradict}; |
53902
396999552212
use standard "split" properties instead of ad hoc "eq_...I"
blanchet
parents:
53901
diff
changeset
|
30 |
val split_if = @{thm split_if}; |
396999552212
use standard "split" properties instead of ad hoc "eq_...I"
blanchet
parents:
53901
diff
changeset
|
31 |
val split_if_asm = @{thm split_if_asm}; |
396999552212
use standard "split" properties instead of ad hoc "eq_...I"
blanchet
parents:
53901
diff
changeset
|
32 |
val split_connectI = @{thms allI impI conjI}; |
396999552212
use standard "split" properties instead of ad hoc "eq_...I"
blanchet
parents:
53901
diff
changeset
|
33 |
|
53903 | 34 |
fun mk_primcorec_assumption_tac ctxt discIs = |
54044
93ab44e992ae
strengthened tactic (primcorec_sel_tac) + tuning
blanchet
parents:
54043
diff
changeset
|
35 |
SELECT_GOAL (unfold_thms_tac ctxt |
54069
3fd3b1683d2b
strengthen the tactics to bring them in sync with the simplifications taking place in the code (and weaken the simplifications a bit -- let's not deal with implies)
blanchet
parents:
54044
diff
changeset
|
36 |
@{thms not_not not_False_eq_True not_True_eq_False de_Morgan_conj de_Morgan_disj} THEN |
53926 | 37 |
SOLVE (HEADGOAL (REPEAT o (rtac refl ORELSE' atac ORELSE' etac conjE ORELSE' |
54117
32730ba3ab85
strengthened tactic to deal with 'False ==> ...'
blanchet
parents:
54103
diff
changeset
|
38 |
eresolve_tac falseEs ORELSE' |
53903 | 39 |
resolve_tac @{thms TrueI conjI disjI1 disjI2} ORELSE' |
53929 | 40 |
dresolve_tac discIs THEN' atac ORELSE' |
41 |
etac notE THEN' atac ORELSE' |
|
54044
93ab44e992ae
strengthened tactic (primcorec_sel_tac) + tuning
blanchet
parents:
54043
diff
changeset
|
42 |
etac disjE)))); |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
43 |
|
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
44 |
fun mk_primcorec_same_case_tac m = |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
45 |
HEADGOAL (if m = 0 then rtac TrueI |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
46 |
else REPEAT_DETERM_N (m - 1) o (rtac conjI THEN' atac) THEN' atac); |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
47 |
|
54069
3fd3b1683d2b
strengthen the tactics to bring them in sync with the simplifications taking place in the code (and weaken the simplifications a bit -- let's not deal with implies)
blanchet
parents:
54044
diff
changeset
|
48 |
fun mk_primcorec_different_case_tac ctxt m excl = |
3fd3b1683d2b
strengthen the tactics to bring them in sync with the simplifications taking place in the code (and weaken the simplifications a bit -- let's not deal with implies)
blanchet
parents:
54044
diff
changeset
|
49 |
HEADGOAL (if m = 0 then mk_primcorec_assumption_tac ctxt [] |
3fd3b1683d2b
strengthen the tactics to bring them in sync with the simplifications taking place in the code (and weaken the simplifications a bit -- let's not deal with implies)
blanchet
parents:
54044
diff
changeset
|
50 |
else dtac excl THEN' (REPEAT_DETERM_N (m - 1) o atac) THEN' mk_primcorec_assumption_tac ctxt []); |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
51 |
|
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
52 |
fun mk_primcorec_cases_tac ctxt k m exclsss = |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
53 |
let val n = length exclsss in |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
54 |
EVERY (map (fn [] => if k = n then all_tac else mk_primcorec_same_case_tac m |
54069
3fd3b1683d2b
strengthen the tactics to bring them in sync with the simplifications taking place in the code (and weaken the simplifications a bit -- let's not deal with implies)
blanchet
parents:
54044
diff
changeset
|
55 |
| [excl] => mk_primcorec_different_case_tac ctxt m excl) |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
56 |
(take k (nth exclsss (k - 1)))) |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
57 |
end; |
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
58 |
|
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
59 |
fun mk_primcorec_prelude ctxt defs thm = |
54024 | 60 |
unfold_thms_tac ctxt defs THEN HEADGOAL (rtac thm) THEN |
61 |
unfold_thms_tac ctxt @{thms Let_def split}; |
|
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
62 |
|
53706 | 63 |
fun mk_primcorec_disc_tac ctxt defs disc_corec k m exclsss = |
64 |
mk_primcorec_prelude ctxt defs disc_corec THEN mk_primcorec_cases_tac ctxt k m exclsss; |
|
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
65 |
|
53910 | 66 |
fun mk_primcorec_sel_tac ctxt defs distincts splits split_asms maps map_idents map_comps f_sel k m |
67 |
exclsss = |
|
53901 | 68 |
mk_primcorec_prelude ctxt defs (f_sel RS trans) THEN |
53693 | 69 |
mk_primcorec_cases_tac ctxt k m exclsss THEN |
54018
bd2e127389f2
strengthened tactic for right-hand sides involving lambdas
blanchet
parents:
53961
diff
changeset
|
70 |
HEADGOAL (REPEAT_DETERM o (rtac refl ORELSE' rtac ext ORELSE' |
53905 | 71 |
eresolve_tac falseEs ORELSE' |
53902
396999552212
use standard "split" properties instead of ad hoc "eq_...I"
blanchet
parents:
53901
diff
changeset
|
72 |
resolve_tac split_connectI ORELSE' |
396999552212
use standard "split" properties instead of ad hoc "eq_...I"
blanchet
parents:
53901
diff
changeset
|
73 |
Splitter.split_asm_tac (split_if_asm :: split_asms) ORELSE' |
396999552212
use standard "split" properties instead of ad hoc "eq_...I"
blanchet
parents:
53901
diff
changeset
|
74 |
Splitter.split_tac (split_if :: splits) ORELSE' |
53910 | 75 |
eresolve_tac (map (fn thm => thm RS neq_eq_eq_contradict) distincts) THEN' atac ORELSE' |
54044
93ab44e992ae
strengthened tactic (primcorec_sel_tac) + tuning
blanchet
parents:
54043
diff
changeset
|
76 |
etac notE THEN' atac ORELSE' |
93ab44e992ae
strengthened tactic (primcorec_sel_tac) + tuning
blanchet
parents:
54043
diff
changeset
|
77 |
(CHANGED o SELECT_GOAL (unfold_thms_tac ctxt |
54164
800106c17419
avoid tactic failure for equations that contain an unapplied 'id'
blanchet
parents:
54138
diff
changeset
|
78 |
(@{thms id_def o_def split_def sum.cases} @ maps @ map_comps @ map_idents))))); |
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
79 |
|
53722
e176d6d3345f
generate more theorems (e.g. for types with only one constructor)
panny
parents:
53720
diff
changeset
|
80 |
fun mk_primcorec_ctr_of_dtr_tac ctxt m collapse maybe_disc_f sel_fs = |
53720 | 81 |
HEADGOAL (rtac ((if null sel_fs then collapse else collapse RS sym) RS trans) THEN' |
53722
e176d6d3345f
generate more theorems (e.g. for types with only one constructor)
panny
parents:
53720
diff
changeset
|
82 |
(the_default (K all_tac) (Option.map rtac maybe_disc_f)) THEN' REPEAT_DETERM_N m o atac) THEN |
54174 | 83 |
unfold_thms_tac ctxt (Let_def :: sel_fs) THEN HEADGOAL (rtac refl); |
53706 | 84 |
|
54138
c7119e1cde3e
avoid confusion between outer ifs and inner ifs, that caused tactic to go berserk
blanchet
parents:
54133
diff
changeset
|
85 |
fun inst_split_eq ctxt split = |
c7119e1cde3e
avoid confusion between outer ifs and inner ifs, that caused tactic to go berserk
blanchet
parents:
54133
diff
changeset
|
86 |
(case prop_of split of |
c7119e1cde3e
avoid confusion between outer ifs and inner ifs, that caused tactic to go berserk
blanchet
parents:
54133
diff
changeset
|
87 |
@{const Trueprop} $ (Const (@{const_name HOL.eq}, _) $ (Var (_, Type (_, [T, _])) $ _) $ _) => |
c7119e1cde3e
avoid confusion between outer ifs and inner ifs, that caused tactic to go berserk
blanchet
parents:
54133
diff
changeset
|
88 |
let |
c7119e1cde3e
avoid confusion between outer ifs and inner ifs, that caused tactic to go berserk
blanchet
parents:
54133
diff
changeset
|
89 |
val s = Name.uu; |
c7119e1cde3e
avoid confusion between outer ifs and inner ifs, that caused tactic to go berserk
blanchet
parents:
54133
diff
changeset
|
90 |
val eq = Abs (Name.uu, T, HOLogic.mk_eq (Free (s, T), Bound 0)); |
c7119e1cde3e
avoid confusion between outer ifs and inner ifs, that caused tactic to go berserk
blanchet
parents:
54133
diff
changeset
|
91 |
val split' = Drule.instantiate' [] [SOME (certify ctxt eq)] split; |
c7119e1cde3e
avoid confusion between outer ifs and inner ifs, that caused tactic to go berserk
blanchet
parents:
54133
diff
changeset
|
92 |
in |
c7119e1cde3e
avoid confusion between outer ifs and inner ifs, that caused tactic to go berserk
blanchet
parents:
54133
diff
changeset
|
93 |
Thm.generalize ([], [s]) (Thm.maxidx_of split' + 1) split' |
c7119e1cde3e
avoid confusion between outer ifs and inner ifs, that caused tactic to go berserk
blanchet
parents:
54133
diff
changeset
|
94 |
end |
c7119e1cde3e
avoid confusion between outer ifs and inner ifs, that caused tactic to go berserk
blanchet
parents:
54133
diff
changeset
|
95 |
| _ => split); |
c7119e1cde3e
avoid confusion between outer ifs and inner ifs, that caused tactic to go berserk
blanchet
parents:
54133
diff
changeset
|
96 |
|
54174 | 97 |
fun distinct_in_prems_tac distincts = |
98 |
eresolve_tac (map (fn thm => thm RS neq_eq_eq_contradict) distincts) THEN' atac; |
|
99 |
||
53908 | 100 |
(* TODO: reduce code duplication with selector tactic above *) |
54043 | 101 |
fun mk_primcorec_raw_code_of_ctr_single_tac ctxt distincts discIs splits split_asms m f_ctr = |
54138
c7119e1cde3e
avoid confusion between outer ifs and inner ifs, that caused tactic to go berserk
blanchet
parents:
54133
diff
changeset
|
102 |
let |
c7119e1cde3e
avoid confusion between outer ifs and inner ifs, that caused tactic to go berserk
blanchet
parents:
54133
diff
changeset
|
103 |
val splits' = |
c7119e1cde3e
avoid confusion between outer ifs and inner ifs, that caused tactic to go berserk
blanchet
parents:
54133
diff
changeset
|
104 |
map (fn th => th RS iffD2) (@{thm split_if_eq2} :: map (inst_split_eq ctxt) splits) |
c7119e1cde3e
avoid confusion between outer ifs and inner ifs, that caused tactic to go berserk
blanchet
parents:
54133
diff
changeset
|
105 |
in |
c7119e1cde3e
avoid confusion between outer ifs and inner ifs, that caused tactic to go berserk
blanchet
parents:
54133
diff
changeset
|
106 |
HEADGOAL (REPEAT o (resolve_tac (splits' @ split_connectI))) THEN |
c7119e1cde3e
avoid confusion between outer ifs and inner ifs, that caused tactic to go berserk
blanchet
parents:
54133
diff
changeset
|
107 |
mk_primcorec_prelude ctxt [] (f_ctr RS trans) THEN |
c7119e1cde3e
avoid confusion between outer ifs and inner ifs, that caused tactic to go berserk
blanchet
parents:
54133
diff
changeset
|
108 |
HEADGOAL ((REPEAT_DETERM_N m o mk_primcorec_assumption_tac ctxt discIs) THEN' |
c7119e1cde3e
avoid confusion between outer ifs and inner ifs, that caused tactic to go berserk
blanchet
parents:
54133
diff
changeset
|
109 |
SELECT_GOAL (SOLVE (HEADGOAL (REPEAT_DETERM o |
c7119e1cde3e
avoid confusion between outer ifs and inner ifs, that caused tactic to go berserk
blanchet
parents:
54133
diff
changeset
|
110 |
(rtac refl ORELSE' atac ORELSE' |
54174 | 111 |
resolve_tac (@{thm Code.abort_def} :: split_connectI) ORELSE' |
112 |
Splitter.split_tac (split_if :: splits) ORELSE' |
|
54138
c7119e1cde3e
avoid confusion between outer ifs and inner ifs, that caused tactic to go berserk
blanchet
parents:
54133
diff
changeset
|
113 |
Splitter.split_asm_tac (split_if_asm :: split_asms) ORELSE' |
c7119e1cde3e
avoid confusion between outer ifs and inner ifs, that caused tactic to go berserk
blanchet
parents:
54133
diff
changeset
|
114 |
mk_primcorec_assumption_tac ctxt discIs ORELSE' |
54174 | 115 |
distinct_in_prems_tac distincts ORELSE' |
54138
c7119e1cde3e
avoid confusion between outer ifs and inner ifs, that caused tactic to go berserk
blanchet
parents:
54133
diff
changeset
|
116 |
(TRY o dresolve_tac discIs) THEN' etac notE THEN' atac))))) |
c7119e1cde3e
avoid confusion between outer ifs and inner ifs, that caused tactic to go berserk
blanchet
parents:
54133
diff
changeset
|
117 |
end; |
53903 | 118 |
|
54133 | 119 |
fun mk_primcorec_raw_code_of_ctr_tac ctxt distincts discIs splits split_asms ms f_ctrs = |
120 |
EVERY (map2 (mk_primcorec_raw_code_of_ctr_single_tac ctxt distincts discIs splits split_asms) ms |
|
121 |
f_ctrs) THEN |
|
122 |
IF_UNSOLVED (unfold_thms_tac ctxt @{thms Code.abort_def} THEN |
|
123 |
HEADGOAL (REPEAT_DETERM o resolve_tac (refl :: split_connectI))); |
|
53693 | 124 |
|
54174 | 125 |
fun mk_primcorec_code_of_raw_code_tac ctxt distincts splits raw = |
54101 | 126 |
HEADGOAL (rtac raw ORELSE' rtac (raw RS trans) THEN' |
54174 | 127 |
SELECT_GOAL (unfold_thms_tac ctxt [Let_def]) THEN' REPEAT_DETERM o |
128 |
(rtac refl ORELSE' atac ORELSE' |
|
53904 | 129 |
resolve_tac split_connectI ORELSE' |
130 |
Splitter.split_tac (split_if :: splits) ORELSE' |
|
54174 | 131 |
distinct_in_prems_tac distincts ORELSE' |
132 |
rtac sym THEN' atac ORELSE' |
|
54042 | 133 |
etac notE THEN' atac)); |
53921 | 134 |
|
53303
ae49b835ca01
moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff
changeset
|
135 |
end; |