author  paulson 
Thu, 10 Oct 1996 10:46:14 +0200  
changeset 2082  8659e3063a30 
parent 2054  bf3891343aa5 
child 2097  076a8d2f972b 
permissions  rwrr 
1465  1 
(* Title: HOL/simpdata.ML 
923  2 
ID: $Id$ 
1465  3 
Author: Tobias Nipkow 
923  4 
Copyright 1991 University of Cambridge 
5 

6 
Instantiation of the generic simplifier 

7 
*) 

8 

1984  9 
section "Simplifier"; 
10 

923  11 
open Simplifier; 
12 

1922  13 
(*** Integration of simplifier with classical reasoner ***) 
14 

15 
(*Add a simpset to a classical set!*) 

16 
infix 4 addss; 

17 
fun cs addss ss = cs addbefore asm_full_simp_tac ss 1; 

18 

19 
fun Addss ss = (claset := !claset addbefore asm_full_simp_tac ss 1); 

20 

1968
daa97cc96feb
Beefedup autotactic: now repeatedly simplifies if needed
paulson
parents:
1948
diff
changeset

21 
(*Designed to be idempotent, except if best_tac instantiates variables 
daa97cc96feb
Beefedup autotactic: now repeatedly simplifies if needed
paulson
parents:
1948
diff
changeset

22 
in some of the subgoals*) 
1922  23 
fun auto_tac (cs,ss) = 
24 
ALLGOALS (asm_full_simp_tac ss) THEN 

1968
daa97cc96feb
Beefedup autotactic: now repeatedly simplifies if needed
paulson
parents:
1948
diff
changeset

25 
REPEAT (safe_tac cs THEN ALLGOALS (asm_full_simp_tac ss)) THEN 
2036  26 
REPEAT (FIRSTGOAL (best_tac (cs addss ss))) THEN 
27 
prune_params_tac; 

1922  28 

29 
fun Auto_tac() = auto_tac (!claset, !simpset); 

30 

31 
fun auto() = by (Auto_tac()); 

32 

33 

1984  34 
(*** Addition of rules to simpsets and clasets simultaneously ***) 
35 

36 
(*Takes UNCONDITIONAL theorems of the form A<>B to 

2031  37 
the Safe Intr rule B==>A and 
38 
the Safe Destruct rule A==>B. 

1984  39 
Also ~A goes to the Safe Elim rule A ==> ?R 
40 
Failing other cases, A is added as a Safe Intr rule*) 

41 
local 

42 
val iff_const = HOLogic.eq_const HOLogic.boolT; 

43 

44 
fun addIff th = 

45 
(case HOLogic.dest_Trueprop (#prop(rep_thm th)) of 

2031  46 
(Const("not",_) $ A) => 
47 
AddSEs [zero_var_indexes (th RS notE)] 

48 
 (con $ _ $ _) => 

49 
if con=iff_const 

50 
then (AddSIs [zero_var_indexes (th RS iffD2)]; 

51 
AddSDs [zero_var_indexes (th RS iffD1)]) 

52 
else AddSIs [th] 

53 
 _ => AddSIs [th]; 

1984  54 
Addsimps [th]) 
55 
handle _ => error ("AddIffs: theorem must be unconditional\n" ^ 

2031  56 
string_of_thm th) 
1984  57 

58 
fun delIff th = 

59 
(case HOLogic.dest_Trueprop (#prop(rep_thm th)) of 

2031  60 
(Const("not",_) $ A) => 
61 
Delrules [zero_var_indexes (th RS notE)] 

62 
 (con $ _ $ _) => 

63 
if con=iff_const 

64 
then Delrules [zero_var_indexes (th RS iffD2), 

65 
zero_var_indexes (th RS iffD1)] 

66 
else Delrules [th] 

67 
 _ => Delrules [th]; 

1984  68 
Delsimps [th]) 
69 
handle _ => warning("DelIffs: ignoring conditional theorem\n" ^ 

2031  70 
string_of_thm th) 
1984  71 
in 
72 
val AddIffs = seq addIff 

73 
val DelIffs = seq delIff 

74 
end; 

75 

76 

923  77 
local 
78 

1922  79 
fun prover s = prove_goal HOL.thy s (fn _ => [fast_tac HOL_cs 1]); 
923  80 

1922  81 
val P_imp_P_iff_True = prover "P > (P = True)" RS mp; 
82 
val P_imp_P_eq_True = P_imp_P_iff_True RS eq_reflection; 

923  83 

1922  84 
val not_P_imp_P_iff_F = prover "~P > (P = False)" RS mp; 
85 
val not_P_imp_P_eq_False = not_P_imp_P_iff_F RS eq_reflection; 

923  86 

1922  87 
fun atomize pairs = 
88 
let fun atoms th = 

2031  89 
(case concl_of th of 
90 
Const("Trueprop",_) $ p => 

91 
(case head_of p of 

92 
Const(a,_) => 

93 
(case assoc(pairs,a) of 

94 
Some(rls) => flat (map atoms ([th] RL rls)) 

95 
 None => [th]) 

96 
 _ => [th]) 

97 
 _ => [th]) 

1922  98 
in atoms end; 
923  99 

1922  100 
fun mk_meta_eq r = case concl_of r of 
2031  101 
Const("==",_)$_$_ => r 
1922  102 
 _$(Const("op =",_)$_$_) => r RS eq_reflection 
103 
 _$(Const("not",_)$_) => r RS not_P_imp_P_eq_False 

104 
 _ => r RS P_imp_P_eq_True; 

105 
(* last 2 lines requires all formulae to be of the from Trueprop(.) *) 

923  106 

1922  107 
fun gen_all th = forall_elim_vars (#maxidx(rep_thm th)+1) th; 
923  108 

2082  109 
in 
923  110 

2082  111 
val simp_thms = map prover 
112 
[ "(x=x) = True", 

113 
"(~True) = False", "(~False) = True", "(~ ~ P) = P", 

114 
"(~P) ~= P", "P ~= (~P)", "(P ~= Q) = (P = (~Q))", 

115 
"(True=P) = P", "(P=True) = P", 

116 
"(True > P) = P", "(False > P) = True", 

117 
"(P > True) = True", "(P > P) = True", 

118 
"(P > False) = (~P)", "(P > ~P) = (~P)", 

119 
"(P & True) = P", "(True & P) = P", 

120 
"(P & False) = False", "(False & P) = False", "(P & P) = P", 

121 
"(P  True) = True", "(True  P) = True", 

122 
"(P  False) = P", "(False  P) = P", "(P  P) = P", 

123 
"((~P) = (~Q)) = (P=Q)", 

124 
"(!x.P) = P", "(? x.P) = P", "? x. x=t", 

125 
"(? x. x=t & P(x)) = P(t)", "(? x. t=x & P(x)) = P(t)", 

126 
"(! x. x=t > P(x)) = P(t)", "(! x. t=x > P(x)) = P(t)" ]; 

923  127 

128 
val meta_eq_to_obj_eq = prove_goal HOL.thy "x==y ==> x=y" 

129 
(fn [prem] => [rewtac prem, rtac refl 1]); 

130 

131 
val eq_sym_conv = prover "(x=y) = (y=x)"; 

132 

133 
val conj_assoc = prover "((P&Q)&R) = (P&(Q&R))"; 

134 

1922  135 
val disj_assoc = prover "((PQ)R) = (P(QR))"; 
136 

137 
val imp_disj = prover "(PQ > R) = ((P>R)&(Q>R))"; 

138 

1948
78e5bfcbc1e9
Added miniscoping to the simplifier: quantifiers are now pushed in
paulson
parents:
1922
diff
changeset

139 
(*Avoids duplication of subgoals after expand_if, when the true and false 
78e5bfcbc1e9
Added miniscoping to the simplifier: quantifiers are now pushed in
paulson
parents:
1922
diff
changeset

140 
cases boil down to the same thing.*) 
78e5bfcbc1e9
Added miniscoping to the simplifier: quantifiers are now pushed in
paulson
parents:
1922
diff
changeset

141 
val cases_simp = prover "((P > Q) & (~P > Q)) = Q"; 
1922  142 

965  143 
val if_True = prove_goalw HOL.thy [if_def] "(if True then x else y) = x" 
923  144 
(fn _=>[fast_tac (HOL_cs addIs [select_equality]) 1]); 
145 

965  146 
val if_False = prove_goalw HOL.thy [if_def] "(if False then x else y) = y" 
923  147 
(fn _=>[fast_tac (HOL_cs addIs [select_equality]) 1]); 
148 

965  149 
val if_P = prove_goal HOL.thy "P ==> (if P then x else y) = x" 
923  150 
(fn [prem] => [ stac (prem RS eqTrueI) 1, rtac if_True 1 ]); 
151 

965  152 
val if_not_P = prove_goal HOL.thy "~P ==> (if P then x else y) = y" 
923  153 
(fn [prem] => [ stac (prem RS not_P_imp_P_iff_F) 1, rtac if_False 1 ]); 
154 

155 
val expand_if = prove_goal HOL.thy 

965  156 
"P(if Q then x else y) = ((Q > P(x)) & (~Q > P(y)))" 
923  157 
(fn _=> [ (res_inst_tac [("Q","Q")] (excluded_middle RS disjE) 1), 
2031  158 
stac if_P 2, 
159 
stac if_not_P 1, 

1465  160 
REPEAT(fast_tac HOL_cs 1) ]); 
923  161 

965  162 
val if_bool_eq = prove_goal HOL.thy 
163 
"(if P then Q else R) = ((P>Q) & (~P>R))" 

164 
(fn _ => [rtac expand_if 1]); 

923  165 

988  166 
(*Add congruence rules for = (instead of ==) *) 
167 
infix 4 addcongs; 

923  168 
fun ss addcongs congs = ss addeqcongs (congs RL [eq_reflection]); 
169 

1264  170 
fun Addcongs congs = (simpset := !simpset addcongs congs); 
171 

923  172 
val mksimps_pairs = 
173 
[("op >", [mp]), ("op &", [conjunct1,conjunct2]), 

174 
("All", [spec]), ("True", []), ("False", []), 

965  175 
("If", [if_bool_eq RS iffD1])]; 
923  176 

177 
fun mksimps pairs = map mk_meta_eq o atomize pairs o gen_all; 

178 

1922  179 
val imp_cong = impI RSN 
180 
(2, prove_goal HOL.thy "(P=P')> (P'> (Q=Q'))> ((P>Q) = (P'>Q'))" 

181 
(fn _=> [fast_tac HOL_cs 1]) RS mp RS mp); 

182 

183 
val o_apply = prove_goalw HOL.thy [o_def] "(f o g)(x) = f(g(x))" 

184 
(fn _ => [rtac refl 1]); 

185 

1948
78e5bfcbc1e9
Added miniscoping to the simplifier: quantifiers are now pushed in
paulson
parents:
1922
diff
changeset

186 
(*Miniscoping: pushing in existential quantifiers*) 
78e5bfcbc1e9
Added miniscoping to the simplifier: quantifiers are now pushed in
paulson
parents:
1922
diff
changeset

187 
val ex_simps = map prover 
2031  188 
["(EX x. P x & Q) = ((EX x.P x) & Q)", 
189 
"(EX x. P & Q x) = (P & (EX x.Q x))", 

190 
"(EX x. P x  Q) = ((EX x.P x)  Q)", 

191 
"(EX x. P  Q x) = (P  (EX x.Q x))", 

192 
"(EX x. P x > Q) = ((ALL x.P x) > Q)", 

193 
"(EX x. P > Q x) = (P > (EX x.Q x))"]; 

1948
78e5bfcbc1e9
Added miniscoping to the simplifier: quantifiers are now pushed in
paulson
parents:
1922
diff
changeset

194 

78e5bfcbc1e9
Added miniscoping to the simplifier: quantifiers are now pushed in
paulson
parents:
1922
diff
changeset

195 
(*Miniscoping: pushing in universal quantifiers*) 
78e5bfcbc1e9
Added miniscoping to the simplifier: quantifiers are now pushed in
paulson
parents:
1922
diff
changeset

196 
val all_simps = map prover 
2031  197 
["(ALL x. P x & Q) = ((ALL x.P x) & Q)", 
198 
"(ALL x. P & Q x) = (P & (ALL x.Q x))", 

199 
"(ALL x. P x  Q) = ((ALL x.P x)  Q)", 

200 
"(ALL x. P  Q x) = (P  (ALL x.Q x))", 

201 
"(ALL x. P x > Q) = ((EX x.P x) > Q)", 

202 
"(ALL x. P > Q x) = (P > (ALL x.Q x))"]; 

1948
78e5bfcbc1e9
Added miniscoping to the simplifier: quantifiers are now pushed in
paulson
parents:
1922
diff
changeset

203 

1922  204 
(*In general it seems wrong to add distributive laws by default: they 
1948
78e5bfcbc1e9
Added miniscoping to the simplifier: quantifiers are now pushed in
paulson
parents:
1922
diff
changeset

205 
might cause exponential blowup. But imp_disj has been in for a while 
1922  206 
and cannot be removed without affecting existing proofs. Moreover, 
207 
rewriting by "(PQ > R) = ((P>R)&(Q>R))" might be justified on the 

208 
grounds that it allows simplification of R in the two cases.*) 

209 

210 

941  211 
local val mktac = mk_case_split_tac (meta_eq_to_obj_eq RS iffD2) 
212 
in 

213 
fun split_tac splits = mktac (map mk_meta_eq splits) 

214 
end; 

215 

1722  216 
local val mktac = mk_case_split_inside_tac (meta_eq_to_obj_eq RS iffD2) 
217 
in 

218 
fun split_inside_tac splits = mktac (map mk_meta_eq splits) 

219 
end; 

220 

923  221 

2022  222 
(* elimination of existential quantifiers in assumptions *) 
923  223 

224 
val ex_all_equiv = 

225 
let val lemma1 = prove_goal HOL.thy 

226 
"(? x. P(x) ==> PROP Q) ==> (!!x. P(x) ==> PROP Q)" 

227 
(fn prems => [resolve_tac prems 1, etac exI 1]); 

228 
val lemma2 = prove_goalw HOL.thy [Ex_def] 

229 
"(!!x. P(x) ==> PROP Q) ==> (? x. P(x) ==> PROP Q)" 

230 
(fn prems => [REPEAT(resolve_tac prems 1)]) 

231 
in equal_intr lemma1 lemma2 end; 

232 

233 
(* '&' congruence rule: not included by default! 

234 
May slow rewrite proofs down by as much as 50% *) 

235 

2022  236 
val conj_cong = 
237 
let val th = prove_goal HOL.thy 

238 
"(P=P')> (P'> (Q=Q'))> ((P&Q) = (P'&Q'))" 

2031  239 
(fn _=> [fast_tac HOL_cs 1]) 
2022  240 
in standard (impI RSN (2, th RS mp RS mp)) end; 
923  241 

2022  242 
val rev_conj_cong = 
243 
let val th = prove_goal HOL.thy 

244 
"(Q=Q')> (Q'> (P=P'))> ((P&Q) = (P'&Q'))" 

2031  245 
(fn _=> [fast_tac HOL_cs 1]) 
2022  246 
in standard (impI RSN (2, th RS mp RS mp)) end; 
247 

248 
(* '' congruence rule: not included by default! *) 

249 

250 
val disj_cong = 

251 
let val th = prove_goal HOL.thy 

252 
"(P=P')> (~P'> (Q=Q'))> ((PQ) = (P'Q'))" 

2031  253 
(fn _=> [fast_tac HOL_cs 1]) 
2022  254 
in standard (impI RSN (2, th RS mp RS mp)) end; 
1548  255 

923  256 
(** 'if' congruence rules: neither included by default! *) 
257 

258 
(*Simplifies x assuming c and y assuming ~c*) 

259 
val if_cong = prove_goal HOL.thy 

965  260 
"[ b=c; c ==> x=u; ~c ==> y=v ] ==>\ 
261 
\ (if b then x else y) = (if c then u else v)" 

923  262 
(fn rew::prems => 
263 
[stac rew 1, stac expand_if 1, stac expand_if 1, 

264 
fast_tac (HOL_cs addDs prems) 1]); 

265 

266 
(*Prevents simplification of x and y: much faster*) 

267 
val if_weak_cong = prove_goal HOL.thy 

965  268 
"b=c ==> (if b then x else y) = (if c then x else y)" 
923  269 
(fn [prem] => [rtac (prem RS arg_cong) 1]); 
270 

271 
(*Prevents simplification of t: much faster*) 

272 
val let_weak_cong = prove_goal HOL.thy 

273 
"a = b ==> (let x=a in t(x)) = (let x=b in t(x))" 

274 
(fn [prem] => [rtac (prem RS arg_cong) 1]); 

275 

276 
end; 

277 

278 
fun prove nm thm = qed_goal nm HOL.thy thm (fn _ => [fast_tac HOL_cs 1]); 

279 

280 
prove "conj_commute" "(P&Q) = (Q&P)"; 

281 
prove "conj_left_commute" "(P&(Q&R)) = (Q&(P&R))"; 

282 
val conj_comms = [conj_commute, conj_left_commute]; 

283 

1922  284 
prove "disj_commute" "(PQ) = (QP)"; 
285 
prove "disj_left_commute" "(P(QR)) = (Q(PR))"; 

286 
val disj_comms = [disj_commute, disj_left_commute]; 

287 

923  288 
prove "conj_disj_distribL" "(P&(QR)) = (P&Q  P&R)"; 
289 
prove "conj_disj_distribR" "((PQ)&R) = (P&R  Q&R)"; 

1485
240cc98b94a7
Added qed_spec_mp to avoid renaming of bound vars in 'th RS spec'
nipkow
parents:
1465
diff
changeset

290 

1892  291 
prove "disj_conj_distribL" "(P(Q&R)) = ((PQ) & (PR))"; 
292 
prove "disj_conj_distribR" "((P&Q)R) = ((PR) & (QR))"; 

293 

294 
prove "imp_conj_distrib" "(P > (Q&R)) = ((P>Q) & (P>R))"; 

1922  295 
prove "imp_conj" "((P&Q)>R) = (P > (Q > R))"; 
1892  296 

1485
240cc98b94a7
Added qed_spec_mp to avoid renaming of bound vars in 'th RS spec'
nipkow
parents:
1465
diff
changeset

297 
prove "de_Morgan_disj" "(~(P  Q)) = (~P & ~Q)"; 
240cc98b94a7
Added qed_spec_mp to avoid renaming of bound vars in 'th RS spec'
nipkow
parents:
1465
diff
changeset

298 
prove "de_Morgan_conj" "(~(P & Q)) = (~P  ~Q)"; 
1922  299 
prove "not_iff" "(P~=Q) = (P = (~Q))"; 
1485
240cc98b94a7
Added qed_spec_mp to avoid renaming of bound vars in 'th RS spec'
nipkow
parents:
1465
diff
changeset

300 

1660  301 
prove "not_all" "(~ (! x.P(x))) = (? x.~P(x))"; 
1922  302 
prove "imp_all" "((! x. P x) > Q) = (? x. P x > Q)"; 
1660  303 
prove "not_ex" "(~ (? x.P(x))) = (! x.~P(x))"; 
1922  304 
prove "imp_ex" "((? x. P x) > Q) = (! x. P x > Q)"; 
1660  305 

1655  306 
prove "ex_disj_distrib" "(? x. P(x)  Q(x)) = ((? x. P(x))  (? x. Q(x)))"; 
307 
prove "all_conj_distrib" "(!x. P(x) & Q(x)) = ((! x. P(x)) & (! x. Q(x)))"; 

308 

1758  309 

2082  310 
val HOL_ss = empty_ss 
311 
setmksimps (mksimps mksimps_pairs) 

312 
setsolver (fn prems => resolve_tac (TrueI::refl::prems) ORELSE' atac 

313 
ORELSE' etac FalseE) 

314 
setsubgoaler asm_simp_tac 

315 
addsimps ([if_True, if_False, o_apply, imp_disj, conj_assoc, disj_assoc, 

316 
de_Morgan_conj, de_Morgan_disj, not_all, not_ex, cases_simp] 

317 
@ ex_simps @ all_simps @ simp_thms) 

318 
addcongs [imp_cong]; 

319 

320 

1655  321 
qed_goal "if_cancel" HOL.thy "(if c then x else x) = x" 
322 
(fn _ => [simp_tac (HOL_ss setloop (split_tac [expand_if])) 1]); 

323 

324 
qed_goal "if_distrib" HOL.thy 

325 
"f(if c then x else y) = (if c then f x else f y)" 

326 
(fn _ => [simp_tac (HOL_ss setloop (split_tac [expand_if])) 1]); 

327 

1874  328 
qed_goalw "o_assoc" HOL.thy [o_def] "f o (g o h) = (f o g o h)" 
1655  329 
(fn _=>[rtac ext 1, rtac refl 1]); 
1984  330 

331 

332 

333 

334 
(*** Install simpsets and datatypes in theory structure ***) 

335 

336 
simpset := HOL_ss; 

337 

338 
exception SS_DATA of simpset; 

339 

340 
let fun merge [] = SS_DATA empty_ss 

341 
 merge ss = let val ss = map (fn SS_DATA x => x) ss; 

342 
in SS_DATA (foldl merge_ss (hd ss, tl ss)) end; 

343 

344 
fun put (SS_DATA ss) = simpset := ss; 

345 

346 
fun get () = SS_DATA (!simpset); 

347 
in add_thydata "HOL" 

348 
("simpset", ThyMethods {merge = merge, put = put, get = get}) 

349 
end; 

350 

351 
type dtype_info = {case_const:term, case_rewrites:thm list, 

352 
constructors:term list, nchotomy:thm, case_cong:thm}; 

353 

354 
exception DT_DATA of (string * dtype_info) list; 

355 
val datatypes = ref [] : (string * dtype_info) list ref; 

356 

357 
let fun merge [] = DT_DATA [] 

358 
 merge ds = 

359 
let val ds = map (fn DT_DATA x => x) ds; 

360 
in DT_DATA (foldl (gen_union eq_fst) (hd ds, tl ds)) end; 

361 

362 
fun put (DT_DATA ds) = datatypes := ds; 

363 

364 
fun get () = DT_DATA (!datatypes); 

365 
in add_thydata "HOL" 

366 
("datatypes", ThyMethods {merge = merge, put = put, get = get}) 

367 
end; 

368 

369 

370 
add_thy_reader_file "thy_data.ML"; 