author  paulson 
Tue, 10 Sep 1996 10:48:07 +0200  
changeset 1968  daa97cc96feb 
parent 1948  78e5bfcbc1e9 
child 1984  5cf82dc3ce67 
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 

9 
open Simplifier; 

10 

1922  11 
(*** Integration of simplifier with classical reasoner ***) 
12 

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

14 
infix 4 addss; 

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

16 

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

18 

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

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

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

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

23 
REPEAT (safe_tac cs THEN ALLGOALS (asm_full_simp_tac ss)) THEN 
1922  24 
REPEAT (FIRSTGOAL (best_tac (cs addss ss))); 
25 

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

27 

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

29 

30 

923  31 
local 
32 

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

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

923  37 

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

923  40 

1922  41 
fun atomize pairs = 
42 
let fun atoms th = 

43 
(case concl_of th of 

44 
Const("Trueprop",_) $ p => 

45 
(case head_of p of 

46 
Const(a,_) => 

47 
(case assoc(pairs,a) of 

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

49 
 None => [th]) 

50 
 _ => [th]) 

51 
 _ => [th]) 

52 
in atoms end; 

923  53 

1922  54 
fun mk_meta_eq r = case concl_of r of 
55 
Const("==",_)$_$_ => r 

56 
 _$(Const("op =",_)$_$_) => r RS eq_reflection 

57 
 _$(Const("not",_)$_) => r RS not_P_imp_P_eq_False 

58 
 _ => r RS P_imp_P_eq_True; 

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

923  60 

1922  61 
fun gen_all th = forall_elim_vars (#maxidx(rep_thm th)+1) th; 
923  62 

1922  63 
val simp_thms = map prover 
64 
[ "(x=x) = True", 

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

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

67 
"(True=P) = P", "(P=True) = P", 

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

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

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

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

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

73 
"(P  True) = True", "(True  P) = True", 

74 
"(P  False) = P", "(False  P) = P", "(P  P) = P", 

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

75 
"((~P) = (~Q)) = (P=Q)", 
1922  76 
"(!x.P) = P", "(? x.P) = P", "? x. x=t", 
77 
"(? x. x=t & P(x)) = P(t)", "(! x. x=t > P(x)) = P(t)" ]; 

923  78 

79 
in 

80 

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

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

83 

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

85 

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

87 

1922  88 
val disj_assoc = prover "((PQ)R) = (P(QR))"; 
89 

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

91 

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

92 
(*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

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

94 
val cases_simp = prover "((P > Q) & (~P > Q)) = Q"; 
1922  95 

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

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

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

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

108 
val expand_if = prove_goal HOL.thy 

965  109 
"P(if Q then x else y) = ((Q > P(x)) & (~Q > P(y)))" 
923  110 
(fn _=> [ (res_inst_tac [("Q","Q")] (excluded_middle RS disjE) 1), 
1465  111 
rtac (if_P RS ssubst) 2, 
112 
rtac (if_not_P RS ssubst) 1, 

113 
REPEAT(fast_tac HOL_cs 1) ]); 

923  114 

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

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

923  118 

988  119 
(*Add congruence rules for = (instead of ==) *) 
120 
infix 4 addcongs; 

923  121 
fun ss addcongs congs = ss addeqcongs (congs RL [eq_reflection]); 
122 

1264  123 
fun Addcongs congs = (simpset := !simpset addcongs congs); 
124 

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

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

965  128 
("If", [if_bool_eq RS iffD1])]; 
923  129 

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

131 

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

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

135 

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

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

138 

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

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

140 
val ex_simps = map prover 
78e5bfcbc1e9
Added miniscoping to the simplifier: quantifiers are now pushed in
paulson
parents:
1922
diff
changeset

141 
["(EX x. P x & Q) = ((EX x.P x) & Q)", 
78e5bfcbc1e9
Added miniscoping to the simplifier: quantifiers are now pushed in
paulson
parents:
1922
diff
changeset

142 
"(EX x. P & Q x) = (P & (EX x.Q x))", 
78e5bfcbc1e9
Added miniscoping to the simplifier: quantifiers are now pushed in
paulson
parents:
1922
diff
changeset

143 
"(EX x. P x  Q) = ((EX x.P x)  Q)", 
78e5bfcbc1e9
Added miniscoping to the simplifier: quantifiers are now pushed in
paulson
parents:
1922
diff
changeset

144 
"(EX x. P  Q x) = (P  (EX x.Q x))", 
78e5bfcbc1e9
Added miniscoping to the simplifier: quantifiers are now pushed in
paulson
parents:
1922
diff
changeset

145 
"(EX x. P x > Q) = ((ALL x.P x) > Q)", 
78e5bfcbc1e9
Added miniscoping to the simplifier: quantifiers are now pushed in
paulson
parents:
1922
diff
changeset

146 
"(EX x. P > Q x) = (P > (EX x.Q x))"]; 
78e5bfcbc1e9
Added miniscoping to the simplifier: quantifiers are now pushed in
paulson
parents:
1922
diff
changeset

147 

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

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

149 
val all_simps = map prover 
78e5bfcbc1e9
Added miniscoping to the simplifier: quantifiers are now pushed in
paulson
parents:
1922
diff
changeset

150 
["(ALL x. P x & Q) = ((ALL x.P x) & Q)", 
78e5bfcbc1e9
Added miniscoping to the simplifier: quantifiers are now pushed in
paulson
parents:
1922
diff
changeset

151 
"(ALL x. P & Q x) = (P & (ALL x.Q x))", 
78e5bfcbc1e9
Added miniscoping to the simplifier: quantifiers are now pushed in
paulson
parents:
1922
diff
changeset

152 
"(ALL x. P x  Q) = ((ALL x.P x)  Q)", 
78e5bfcbc1e9
Added miniscoping to the simplifier: quantifiers are now pushed in
paulson
parents:
1922
diff
changeset

153 
"(ALL x. P  Q x) = (P  (ALL x.Q x))", 
78e5bfcbc1e9
Added miniscoping to the simplifier: quantifiers are now pushed in
paulson
parents:
1922
diff
changeset

154 
"(ALL x. P x > Q) = ((EX x.P x) > Q)", 
78e5bfcbc1e9
Added miniscoping to the simplifier: quantifiers are now pushed in
paulson
parents:
1922
diff
changeset

155 
"(ALL x. P > Q x) = (P > (ALL x.Q x))"]; 
78e5bfcbc1e9
Added miniscoping to the simplifier: quantifiers are now pushed in
paulson
parents:
1922
diff
changeset

156 

923  157 
val HOL_ss = empty_ss 
158 
setmksimps (mksimps mksimps_pairs) 

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

160 
ORELSE' etac FalseE) 

161 
setsubgoaler asm_simp_tac 

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

162 
addsimps ([if_True, if_False, o_apply, imp_disj, conj_assoc, disj_assoc, 
78e5bfcbc1e9
Added miniscoping to the simplifier: quantifiers are now pushed in
paulson
parents:
1922
diff
changeset

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

164 
@ ex_simps @ all_simps @ simp_thms) 
923  165 
addcongs [imp_cong]; 
166 

1922  167 

168 
(*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

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

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

173 

174 

941  175 
local val mktac = mk_case_split_tac (meta_eq_to_obj_eq RS iffD2) 
176 
in 

177 
fun split_tac splits = mktac (map mk_meta_eq splits) 

178 
end; 

179 

1722  180 
local val mktac = mk_case_split_inside_tac (meta_eq_to_obj_eq RS iffD2) 
181 
in 

182 
fun split_inside_tac splits = mktac (map mk_meta_eq splits) 

183 
end; 

184 

923  185 

186 
(* eliminiation of existential quantifiers in assumptions *) 

187 

188 
val ex_all_equiv = 

189 
let val lemma1 = prove_goal HOL.thy 

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

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

192 
val lemma2 = prove_goalw HOL.thy [Ex_def] 

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

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

195 
in equal_intr lemma1 lemma2 end; 

196 

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

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

199 

200 
val conj_cong = impI RSN 

201 
(2, prove_goal HOL.thy "(P=P')> (P'> (Q=Q'))> ((P&Q) = (P'&Q'))" 

1465  202 
(fn _=> [fast_tac HOL_cs 1]) RS mp RS mp); 
923  203 

1548  204 
val rev_conj_cong = impI RSN 
205 
(2, prove_goal HOL.thy "(Q=Q')> (Q'> (P=P'))> ((P&Q) = (P'&Q'))" 

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

207 

923  208 
(** 'if' congruence rules: neither included by default! *) 
209 

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

211 
val if_cong = prove_goal HOL.thy 

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

923  214 
(fn rew::prems => 
215 
[stac rew 1, stac expand_if 1, stac expand_if 1, 

216 
fast_tac (HOL_cs addDs prems) 1]); 

217 

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

219 
val if_weak_cong = prove_goal HOL.thy 

965  220 
"b=c ==> (if b then x else y) = (if c then x else y)" 
923  221 
(fn [prem] => [rtac (prem RS arg_cong) 1]); 
222 

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

224 
val let_weak_cong = prove_goal HOL.thy 

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

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

227 

228 
end; 

229 

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

231 

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

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

234 
val conj_comms = [conj_commute, conj_left_commute]; 

235 

1922  236 
prove "disj_commute" "(PQ) = (QP)"; 
237 
prove "disj_left_commute" "(P(QR)) = (Q(PR))"; 

238 
val disj_comms = [disj_commute, disj_left_commute]; 

239 

923  240 
prove "conj_disj_distribL" "(P&(QR)) = (P&Q  P&R)"; 
241 
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

242 

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

245 

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

1922  247 
prove "imp_conj" "((P&Q)>R) = (P > (Q > R))"; 
1892  248 

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

249 
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

250 
prove "de_Morgan_conj" "(~(P & Q)) = (~P  ~Q)"; 
1922  251 
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

252 

1660  253 
prove "not_all" "(~ (! x.P(x))) = (? x.~P(x))"; 
1922  254 
prove "imp_all" "((! x. P x) > Q) = (? x. P x > Q)"; 
1660  255 
prove "not_ex" "(~ (? x.P(x))) = (! x.~P(x))"; 
1922  256 
prove "imp_ex" "((? x. P x) > Q) = (! x. P x > Q)"; 
1660  257 

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

260 

1758  261 

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

264 

265 
qed_goal "if_distrib" HOL.thy 

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

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

268 

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