author | wenzelm |
Sat, 16 Apr 2011 18:11:20 +0200 | |
changeset 42364 | 8c674b3b8e44 |
parent 41840 | f69045fdc836 |
child 42365 | bfd28ba57859 |
permissions | -rw-r--r-- |
23150 | 1 |
(* Title: HOL/Tools/TFL/casesplit.ML |
2 |
Author: Lucas Dixon, University of Edinburgh |
|
3 |
||
41840
f69045fdc836
removed dead code/unused exports/speculative generality
krauss
parents:
41228
diff
changeset
|
4 |
Extra case splitting for TFL. |
23150 | 5 |
*) |
6 |
||
7 |
signature CASE_SPLIT = |
|
8 |
sig |
|
9 |
||
10 |
(* try to recursively split conjectured thm to given list of thms *) |
|
11 |
val splitto : thm list -> thm -> thm |
|
12 |
||
13 |
end; |
|
14 |
||
41840
f69045fdc836
removed dead code/unused exports/speculative generality
krauss
parents:
41228
diff
changeset
|
15 |
structure CaseSplit: CASE_SPLIT = |
23150 | 16 |
struct |
17 |
||
41840
f69045fdc836
removed dead code/unused exports/speculative generality
krauss
parents:
41228
diff
changeset
|
18 |
val rulify_goals = Raw_Simplifier.rewrite_goals_rule @{thms induct_rulify}; |
f69045fdc836
removed dead code/unused exports/speculative generality
krauss
parents:
41228
diff
changeset
|
19 |
val atomize_goals = Raw_Simplifier.rewrite_goals_rule @{thms induct_atomize}; |
23150 | 20 |
|
21 |
(* beta-eta contract the theorem *) |
|
22 |
fun beta_eta_contract thm = |
|
23 |
let |
|
36945 | 24 |
val thm2 = Thm.equal_elim (Thm.beta_conversion true (Thm.cprop_of thm)) thm |
25 |
val thm3 = Thm.equal_elim (Thm.eta_conversion (Thm.cprop_of thm2)) thm2 |
|
23150 | 26 |
in thm3 end; |
27 |
||
28 |
(* make a casethm from an induction thm *) |
|
29 |
val cases_thm_of_induct_thm = |
|
30 |
Seq.hd o (ALLGOALS (fn i => REPEAT (etac Drule.thin_rl i))); |
|
31 |
||
32 |
(* get the case_thm (my version) from a type *) |
|
31784 | 33 |
fun case_thm_of_ty thy ty = |
23150 | 34 |
let |
35 |
val ty_str = case ty of |
|
36 |
Type(ty_str, _) => ty_str |
|
37 |
| TFree(s,_) => error ("Free type: " ^ s) |
|
38 |
| TVar((s,i),_) => error ("Free variable: " ^ s) |
|
31784 | 39 |
val dt = Datatype.the_info thy ty_str |
23150 | 40 |
in |
32727 | 41 |
cases_thm_of_induct_thm (#induct dt) |
23150 | 42 |
end; |
43 |
||
44 |
||
45 |
(* for use when there are no prems to the subgoal *) |
|
46 |
(* does a case split on the given variable *) |
|
47 |
fun mk_casesplit_goal_thm sgn (vstr,ty) gt = |
|
48 |
let |
|
49 |
val x = Free(vstr,ty) |
|
50 |
val abst = Abs(vstr, ty, Term.abstract_over (x, gt)); |
|
51 |
||
52 |
val ctermify = Thm.cterm_of sgn; |
|
53 |
val ctypify = Thm.ctyp_of sgn; |
|
54 |
val case_thm = case_thm_of_ty sgn ty; |
|
55 |
||
56 |
val abs_ct = ctermify abst; |
|
57 |
val free_ct = ctermify x; |
|
58 |
||
29265
5b4247055bd7
moved old add_term_vars, add_term_frees etc. to structure OldTerm;
wenzelm
parents:
23150
diff
changeset
|
59 |
val casethm_vars = rev (OldTerm.term_vars (Thm.concl_of case_thm)); |
23150 | 60 |
|
29270
0eade173f77e
moved old add_type_XXX, add_term_XXX etc. to structure OldTerm;
wenzelm
parents:
29265
diff
changeset
|
61 |
val casethm_tvars = OldTerm.term_tvars (Thm.concl_of case_thm); |
23150 | 62 |
val (Pv, Dv, type_insts) = |
63 |
case (Thm.concl_of case_thm) of |
|
64 |
(_ $ ((Pv as Var(P,Pty)) $ (Dv as Var(D, Dty)))) => |
|
65 |
(Pv, Dv, |
|
66 |
Sign.typ_match sgn (Dty, ty) Vartab.empty) |
|
67 |
| _ => error "not a valid case thm"; |
|
68 |
val type_cinsts = map (fn (ixn, (S, T)) => (ctypify (TVar (ixn, S)), ctypify T)) |
|
69 |
(Vartab.dest type_insts); |
|
32035 | 70 |
val cPv = ctermify (Envir.subst_term_types type_insts Pv); |
71 |
val cDv = ctermify (Envir.subst_term_types type_insts Dv); |
|
23150 | 72 |
in |
73 |
(beta_eta_contract |
|
74 |
(case_thm |
|
75 |
|> Thm.instantiate (type_cinsts, []) |
|
76 |
|> Thm.instantiate ([], [(cPv, abs_ct), (cDv, free_ct)]))) |
|
77 |
end; |
|
78 |
||
79 |
||
80 |
(* the find_XXX_split functions are simply doing a lightwieght (I |
|
81 |
think) term matching equivalent to find where to do the next split *) |
|
82 |
||
83 |
(* assuming two twems are identical except for a free in one at a |
|
84 |
subterm, or constant in another, ie assume that one term is a plit of |
|
85 |
another, then gives back the free variable that has been split. *) |
|
86 |
exception find_split_exp of string |
|
87 |
fun find_term_split (Free v, _ $ _) = SOME v |
|
88 |
| find_term_split (Free v, Const _) = SOME v |
|
89 |
| find_term_split (Free v, Abs _) = SOME v (* do we really want this case? *) |
|
90 |
| find_term_split (Free v, Var _) = NONE (* keep searching *) |
|
91 |
| find_term_split (a $ b, a2 $ b2) = |
|
92 |
(case find_term_split (a, a2) of |
|
93 |
NONE => find_term_split (b,b2) |
|
94 |
| vopt => vopt) |
|
95 |
| find_term_split (Abs(_,ty,t1), Abs(_,ty2,t2)) = |
|
96 |
find_term_split (t1, t2) |
|
97 |
| find_term_split (Const (x,ty), Const(x2,ty2)) = |
|
98 |
if x = x2 then NONE else (* keep searching *) |
|
99 |
raise find_split_exp (* stop now *) |
|
100 |
"Terms are not identical upto a free varaible! (Consts)" |
|
101 |
| find_term_split (Bound i, Bound j) = |
|
102 |
if i = j then NONE else (* keep searching *) |
|
103 |
raise find_split_exp (* stop now *) |
|
104 |
"Terms are not identical upto a free varaible! (Bound)" |
|
105 |
| find_term_split (a, b) = |
|
106 |
raise find_split_exp (* stop now *) |
|
107 |
"Terms are not identical upto a free varaible! (Other)"; |
|
108 |
||
109 |
(* assume that "splitth" is a case split form of subgoal i of "genth", |
|
110 |
then look for a free variable to split, breaking the subgoal closer to |
|
111 |
splitth. *) |
|
112 |
fun find_thm_split splitth i genth = |
|
113 |
find_term_split (Logic.get_goal (Thm.prop_of genth) i, |
|
114 |
Thm.concl_of splitth) handle find_split_exp _ => NONE; |
|
115 |
||
116 |
(* as above but searches "splitths" for a theorem that suggest a case split *) |
|
117 |
fun find_thms_split splitths i genth = |
|
118 |
Library.get_first (fn sth => find_thm_split sth i genth) splitths; |
|
119 |
||
120 |
||
121 |
(* split the subgoal i of "genth" until we get to a member of |
|
122 |
splitths. Assumes that genth will be a general form of splitths, that |
|
123 |
can be case-split, as needed. Otherwise fails. Note: We assume that |
|
124 |
all of "splitths" are split to the same level, and thus it doesn't |
|
125 |
matter which one we choose to look for the next split. Simply add |
|
126 |
search on splitthms and split variable, to change this. *) |
|
127 |
(* Note: possible efficiency measure: when a case theorem is no longer |
|
128 |
useful, drop it? *) |
|
129 |
(* Note: This should not be a separate tactic but integrated into the |
|
130 |
case split done during recdef's case analysis, this would avoid us |
|
131 |
having to (re)search for variables to split. *) |
|
132 |
fun splitto splitths genth = |
|
133 |
let |
|
134 |
val _ = not (null splitths) orelse error "splitto: no given splitths"; |
|
42364 | 135 |
val thy = Thm.theory_of_thm genth; |
23150 | 136 |
|
137 |
(* check if we are a member of splitths - FIXME: quicker and |
|
138 |
more flexible with discrim net. *) |
|
139 |
fun solve_by_splitth th split = |
|
42364 | 140 |
Thm.biresolution false [(false,split)] 1 th; |
23150 | 141 |
|
142 |
fun split th = |
|
42364 | 143 |
(case find_thms_split splitths 1 th of |
144 |
NONE => |
|
145 |
(writeln (cat_lines |
|
146 |
(["th:", Display.string_of_thm_without_context th, "split ths:"] @ |
|
147 |
map Display.string_of_thm_without_context splitths @ ["\n--"])); |
|
148 |
error "splitto: cannot find variable to split on") |
|
149 |
| SOME v => |
|
150 |
let |
|
151 |
val gt = HOLogic.dest_Trueprop (nth (Thm.prems_of th) 0); |
|
152 |
val split_thm = mk_casesplit_goal_thm thy v gt; |
|
153 |
val (subthms, expf) = IsaND.fixed_subgoal_thms split_thm; |
|
154 |
in |
|
155 |
expf (map recsplitf subthms) |
|
156 |
end) |
|
23150 | 157 |
|
158 |
and recsplitf th = |
|
42364 | 159 |
(* note: multiple unifiers! we only take the first element, |
160 |
probably fine -- there is probably only one anyway. *) |
|
161 |
(case get_first (Seq.pull o solve_by_splitth th) splitths of |
|
162 |
NONE => split th |
|
163 |
| SOME (solved_th, more) => solved_th); |
|
23150 | 164 |
in |
165 |
recsplitf genth |
|
166 |
end; |
|
167 |
||
168 |
end; |