author  nipkow 
Thu, 21 Feb 2008 17:33:58 +0100  
changeset 26105  ae06618225ec 
parent 25886  7753e0d81b7a 
child 26147  ae2bf929e33c 
permissions  rwrr 
1475  1 
(* Title: HOL/Fun.thy 
923  2 
ID: $Id$ 
1475  3 
Author: Tobias Nipkow, Cambridge University Computer Laboratory 
923  4 
Copyright 1994 University of Cambridge 
18154  5 
*) 
923  6 

18154  7 
header {* Notions about functions *} 
923  8 

15510  9 
theory Fun 
22886  10 
imports Set 
15131  11 
begin 
2912  12 

13585  13 
constdefs 
14 
fun_upd :: "('a => 'b) => 'a => 'b => ('a => 'b)" 

22886  15 
"fun_upd f a b == % x. if x=a then b else f x" 
6171  16 

9141  17 
nonterminals 
18 
updbinds updbind 

5305  19 
syntax 
13585  20 
"_updbind" :: "['a, 'a] => updbind" ("(2_ :=/ _)") 
21 
"" :: "updbind => updbinds" ("_") 

22 
"_updbinds":: "[updbind, updbinds] => updbinds" ("_,/ _") 

23 
"_Update" :: "['a, updbinds] => 'a" ("_/'((_)')" [1000,0] 900) 

5305  24 

25 
translations 

26 
"_Update f (_updbinds b bs)" == "_Update (_Update f b) bs" 

27 
"f(x:=y)" == "fun_upd f x y" 

2912  28 

9340  29 
(* Hint: to define the sum of two functions (or maps), use sum_case. 
30 
A nice infix syntax could be defined (in Datatype.thy or below) by 

31 
consts 

32 
fun_sum :: "('a => 'c) => ('b => 'c) => (('a+'b) => 'c)" (infixr "'(+')"80) 

33 
translations 

13585  34 
"fun_sum" == sum_case 
9340  35 
*) 
12258  36 

22744
5cbe966d67a2
Isar definitions are now added explicitly to code theorem table
haftmann
parents:
22577
diff
changeset

37 
definition 
5cbe966d67a2
Isar definitions are now added explicitly to code theorem table
haftmann
parents:
22577
diff
changeset

38 
override_on :: "('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'a set \<Rightarrow> 'a \<Rightarrow> 'b" 
5cbe966d67a2
Isar definitions are now added explicitly to code theorem table
haftmann
parents:
22577
diff
changeset

39 
where 
5cbe966d67a2
Isar definitions are now added explicitly to code theorem table
haftmann
parents:
22577
diff
changeset

40 
"override_on f g A = (\<lambda>a. if a \<in> A then g a else f a)" 
6171  41 

22744
5cbe966d67a2
Isar definitions are now added explicitly to code theorem table
haftmann
parents:
22577
diff
changeset

42 
definition 
5cbe966d67a2
Isar definitions are now added explicitly to code theorem table
haftmann
parents:
22577
diff
changeset

43 
id :: "'a \<Rightarrow> 'a" 
5cbe966d67a2
Isar definitions are now added explicitly to code theorem table
haftmann
parents:
22577
diff
changeset

44 
where 
5cbe966d67a2
Isar definitions are now added explicitly to code theorem table
haftmann
parents:
22577
diff
changeset

45 
"id = (\<lambda>x. x)" 
13910  46 

22744
5cbe966d67a2
Isar definitions are now added explicitly to code theorem table
haftmann
parents:
22577
diff
changeset

47 
definition 
5cbe966d67a2
Isar definitions are now added explicitly to code theorem table
haftmann
parents:
22577
diff
changeset

48 
comp :: "('b \<Rightarrow> 'c) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'c" (infixl "o" 55) 
5cbe966d67a2
Isar definitions are now added explicitly to code theorem table
haftmann
parents:
22577
diff
changeset

49 
where 
5cbe966d67a2
Isar definitions are now added explicitly to code theorem table
haftmann
parents:
22577
diff
changeset

50 
"f o g = (\<lambda>x. f (g x))" 
11123  51 

21210  52 
notation (xsymbols) 
19656
09be06943252
tuned concrete syntax  abbreviation/const_syntax;
wenzelm
parents:
19536
diff
changeset

53 
comp (infixl "\<circ>" 55) 
09be06943252
tuned concrete syntax  abbreviation/const_syntax;
wenzelm
parents:
19536
diff
changeset

54 

21210  55 
notation (HTML output) 
19656
09be06943252
tuned concrete syntax  abbreviation/const_syntax;
wenzelm
parents:
19536
diff
changeset

56 
comp (infixl "\<circ>" 55) 
09be06943252
tuned concrete syntax  abbreviation/const_syntax;
wenzelm
parents:
19536
diff
changeset

57 

13585  58 
text{*compatibility*} 
59 
lemmas o_def = comp_def 

2912  60 

13585  61 
constdefs 
26105
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset

62 
inj_on :: "['a => 'b, 'a set] => bool"  "injective" 
19363  63 
"inj_on f A == ! x:A. ! y:A. f(x)=f(y) > x=y" 
6171  64 

26105
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset

65 
definition 
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset

66 
bij_betw :: "('a => 'b) => 'a set => 'b set => bool" where  "bijective" 
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset

67 
"bij_betw f A B \<longleftrightarrow> inj_on f A & f ` A = B" 
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset

68 

ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset

69 

13585  70 
text{*A common special case: functions injective over the entire domain type.*} 
19323  71 

19363  72 
abbreviation 
73 
"inj f == inj_on f UNIV" 

5852  74 

7374  75 
constdefs 
13585  76 
surj :: "('a => 'b) => bool" (*surjective*) 
19363  77 
"surj f == ! y. ? x. y=f(x)" 
12258  78 

13585  79 
bij :: "('a => 'b) => bool" (*bijective*) 
19363  80 
"bij f == inj f & surj f" 
12258  81 

7374  82 

13585  83 

84 
text{*As a simplification rule, it replaces all function equalities by 

85 
firstorder equalities.*} 

21327  86 
lemma expand_fun_eq: "f = g \<longleftrightarrow> (\<forall>x. f x = g x)" 
13585  87 
apply (rule iffI) 
88 
apply (simp (no_asm_simp)) 

21327  89 
apply (rule ext) 
90 
apply (simp (no_asm_simp)) 

13585  91 
done 
92 

93 
lemma apply_inverse: 

94 
"[ f(x)=u; !!x. P(x) ==> g(f(x)) = x; P(x) ] ==> x=g(u)" 

95 
by auto 

96 

97 

98 
text{*The Identity Function: @{term id}*} 

99 
lemma id_apply [simp]: "id x = x" 

100 
by (simp add: id_def) 

101 

16733
236dfafbeb63
linear arithmetic now takes "&" in assumptions apart.
nipkow
parents:
15691
diff
changeset

102 
lemma inj_on_id[simp]: "inj_on id A" 
15510  103 
by (simp add: inj_on_def) 
104 

16733
236dfafbeb63
linear arithmetic now takes "&" in assumptions apart.
nipkow
parents:
15691
diff
changeset

105 
lemma inj_on_id2[simp]: "inj_on (%x. x) A" 
236dfafbeb63
linear arithmetic now takes "&" in assumptions apart.
nipkow
parents:
15691
diff
changeset

106 
by (simp add: inj_on_def) 
236dfafbeb63
linear arithmetic now takes "&" in assumptions apart.
nipkow
parents:
15691
diff
changeset

107 

236dfafbeb63
linear arithmetic now takes "&" in assumptions apart.
nipkow
parents:
15691
diff
changeset

108 
lemma surj_id[simp]: "surj id" 
15510  109 
by (simp add: surj_def) 
110 

16733
236dfafbeb63
linear arithmetic now takes "&" in assumptions apart.
nipkow
parents:
15691
diff
changeset

111 
lemma bij_id[simp]: "bij id" 
15510  112 
by (simp add: bij_def inj_on_id surj_id) 
113 

114 

13585  115 

116 
subsection{*The Composition Operator: @{term "f \<circ> g"}*} 

117 

118 
lemma o_apply [simp]: "(f o g) x = f (g x)" 

119 
by (simp add: comp_def) 

120 

121 
lemma o_assoc: "f o (g o h) = f o g o h" 

122 
by (simp add: comp_def) 

123 

124 
lemma id_o [simp]: "id o g = g" 

125 
by (simp add: comp_def) 

126 

127 
lemma o_id [simp]: "f o id = f" 

128 
by (simp add: comp_def) 

129 

130 
lemma image_compose: "(f o g) ` r = f`(g`r)" 

131 
by (simp add: comp_def, blast) 

132 

133 
lemma image_eq_UN: "f`A = (UN x:A. {f x})" 

134 
by blast 

135 

136 
lemma UN_o: "UNION A (g o f) = UNION (f`A) g" 

137 
by (unfold comp_def, blast) 

138 

139 

140 
subsection{*The Injectivity Predicate, @{term inj}*} 

141 

142 
text{*NB: @{term inj} now just translates to @{term inj_on}*} 

143 

144 

145 
text{*For Proofs in @{text "Tools/datatype_rep_proofs"}*} 

146 
lemma datatype_injI: 

147 
"(!! x. ALL y. f(x) = f(y) > x=y) ==> inj(f)" 

148 
by (simp add: inj_on_def) 

149 

13637  150 
theorem range_ex1_eq: "inj f \<Longrightarrow> b : range f = (EX! x. b = f x)" 
151 
by (unfold inj_on_def, blast) 

152 

13585  153 
lemma injD: "[ inj(f); f(x) = f(y) ] ==> x=y" 
154 
by (simp add: inj_on_def) 

155 

156 
(*Useful with the simplifier*) 

157 
lemma inj_eq: "inj(f) ==> (f(x) = f(y)) = (x=y)" 

158 
by (force simp add: inj_on_def) 

159 

160 

161 
subsection{*The Predicate @{term inj_on}: Injectivity On A Restricted Domain*} 

162 

163 
lemma inj_onI: 

164 
"(!! x y. [ x:A; y:A; f(x) = f(y) ] ==> x=y) ==> inj_on f A" 

165 
by (simp add: inj_on_def) 

166 

167 
lemma inj_on_inverseI: "(!!x. x:A ==> g(f(x)) = x) ==> inj_on f A" 

168 
by (auto dest: arg_cong [of concl: g] simp add: inj_on_def) 

169 

170 
lemma inj_onD: "[ inj_on f A; f(x)=f(y); x:A; y:A ] ==> x=y" 

171 
by (unfold inj_on_def, blast) 

172 

173 
lemma inj_on_iff: "[ inj_on f A; x:A; y:A ] ==> (f(x)=f(y)) = (x=y)" 

174 
by (blast dest!: inj_onD) 

175 

176 
lemma comp_inj_on: 

177 
"[ inj_on f A; inj_on g (f`A) ] ==> inj_on (g o f) A" 

178 
by (simp add: comp_def inj_on_def) 

179 

15303  180 
lemma inj_on_imageI: "inj_on (g o f) A \<Longrightarrow> inj_on g (f ` A)" 
181 
apply(simp add:inj_on_def image_def) 

182 
apply blast 

183 
done 

184 

15439  185 
lemma inj_on_image_iff: "\<lbrakk> ALL x:A. ALL y:A. (g(f x) = g(f y)) = (g x = g y); 
186 
inj_on f A \<rbrakk> \<Longrightarrow> inj_on g (f ` A) = inj_on g A" 

187 
apply(unfold inj_on_def) 

188 
apply blast 

189 
done 

190 

13585  191 
lemma inj_on_contraD: "[ inj_on f A; ~x=y; x:A; y:A ] ==> ~ f(x)=f(y)" 
192 
by (unfold inj_on_def, blast) 

12258  193 

13585  194 
lemma inj_singleton: "inj (%s. {s})" 
195 
by (simp add: inj_on_def) 

196 

15111  197 
lemma inj_on_empty[iff]: "inj_on f {}" 
198 
by(simp add: inj_on_def) 

199 

15303  200 
lemma subset_inj_on: "[ inj_on f B; A <= B ] ==> inj_on f A" 
13585  201 
by (unfold inj_on_def, blast) 
202 

15111  203 
lemma inj_on_Un: 
204 
"inj_on f (A Un B) = 

205 
(inj_on f A & inj_on f B & f`(AB) Int f`(BA) = {})" 

206 
apply(unfold inj_on_def) 

207 
apply (blast intro:sym) 

208 
done 

209 

210 
lemma inj_on_insert[iff]: 

211 
"inj_on f (insert a A) = (inj_on f A & f a ~: f`(A{a}))" 

212 
apply(unfold inj_on_def) 

213 
apply (blast intro:sym) 

214 
done 

215 

216 
lemma inj_on_diff: "inj_on f A ==> inj_on f (AB)" 

217 
apply(unfold inj_on_def) 

218 
apply (blast) 

219 
done 

220 

13585  221 

222 
subsection{*The Predicate @{term surj}: Surjectivity*} 

223 

224 
lemma surjI: "(!! x. g(f x) = x) ==> surj g" 

225 
apply (simp add: surj_def) 

226 
apply (blast intro: sym) 

227 
done 

228 

229 
lemma surj_range: "surj f ==> range f = UNIV" 

230 
by (auto simp add: surj_def) 

231 

232 
lemma surjD: "surj f ==> EX x. y = f x" 

233 
by (simp add: surj_def) 

234 

235 
lemma surjE: "surj f ==> (!!x. y = f x ==> C) ==> C" 

236 
by (simp add: surj_def, blast) 

237 

238 
lemma comp_surj: "[ surj f; surj g ] ==> surj (g o f)" 

239 
apply (simp add: comp_def surj_def, clarify) 

240 
apply (drule_tac x = y in spec, clarify) 

241 
apply (drule_tac x = x in spec, blast) 

242 
done 

243 

244 

26105
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset

245 
subsection{*The Predicate @{const bij}: Bijectivity*} 
13585  246 

247 
lemma bijI: "[ inj f; surj f ] ==> bij f" 

248 
by (simp add: bij_def) 

249 

250 
lemma bij_is_inj: "bij f ==> inj f" 

251 
by (simp add: bij_def) 

252 

253 
lemma bij_is_surj: "bij f ==> surj f" 

254 
by (simp add: bij_def) 

255 

256 

26105
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset

257 
subsection{*The Predicate @{const bij_betw}: Bijectivity*} 
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset

258 

ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset

259 
lemma bij_betw_imp_inj_on: "bij_betw f A B \<Longrightarrow> inj_on f A" 
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset

260 
by (simp add: bij_betw_def) 
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset

261 

ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset

262 
lemma bij_betw_inv: assumes "bij_betw f A B" shows "EX g. bij_betw g B A" 
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset

263 
proof  
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset

264 
have i: "inj_on f A" and s: "f ` A = B" 
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset

265 
using assms by(auto simp:bij_betw_def) 
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset

266 
let ?P = "%b a. a:A \<and> f a = b" let ?g = "%b. The (?P b)" 
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset

267 
{ fix a b assume P: "?P b a" 
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset

268 
hence ex1: "\<exists>a. ?P b a" using s unfolding image_def by blast 
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset

269 
hence uex1: "\<exists>!a. ?P b a" by(blast dest:inj_onD[OF i]) 
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset

270 
hence " ?g b = a" using the1_equality[OF uex1, OF P] P by simp 
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset

271 
} note g = this 
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset

272 
have "inj_on ?g B" 
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset

273 
proof(rule inj_onI) 
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset

274 
fix x y assume "x:B" "y:B" "?g x = ?g y" 
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset

275 
from s `x:B` obtain a1 where a1: "?P x a1" unfolding image_def by blast 
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset

276 
from s `y:B` obtain a2 where a2: "?P y a2" unfolding image_def by blast 
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset

277 
from g[OF a1] a1 g[OF a2] a2 `?g x = ?g y` show "x=y" by simp 
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset

278 
qed 
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset

279 
moreover have "?g ` B = A" 
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset

280 
proof(auto simp:image_def) 
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset

281 
fix b assume "b:B" 
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset

282 
with s obtain a where P: "?P b a" unfolding image_def by blast 
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset

283 
thus "?g b \<in> A" using g[OF P] by auto 
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset

284 
next 
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset

285 
fix a assume "a:A" 
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset

286 
then obtain b where P: "?P b a" using s unfolding image_def by blast 
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset

287 
then have "b:B" using s unfolding image_def by blast 
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset

288 
with g[OF P] show "\<exists>b\<in>B. a = ?g b" by blast 
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset

289 
qed 
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset

290 
ultimately show ?thesis by(auto simp:bij_betw_def) 
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset

291 
qed 
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset

292 

ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset

293 

13585  294 
subsection{*Facts About the Identity Function*} 
5852  295 

13585  296 
text{*We seem to need both the @{term id} forms and the @{term "\<lambda>x. x"} 
297 
forms. The latter can arise by rewriting, while @{term id} may be used 

298 
explicitly.*} 

299 

300 
lemma image_ident [simp]: "(%x. x) ` Y = Y" 

301 
by blast 

302 

303 
lemma image_id [simp]: "id ` Y = Y" 

304 
by (simp add: id_def) 

305 

306 
lemma vimage_ident [simp]: "(%x. x) ` Y = Y" 

307 
by blast 

308 

309 
lemma vimage_id [simp]: "id ` A = A" 

310 
by (simp add: id_def) 

311 

24286
7619080e49f0
ATP blacklisting is now in theory data, attribute noatp
paulson
parents:
24017
diff
changeset

312 
lemma vimage_image_eq [noatp]: "f ` (f ` A) = {y. EX x:A. f x = f y}" 
13585  313 
by (blast intro: sym) 
314 

315 
lemma image_vimage_subset: "f ` (f ` A) <= A" 

316 
by blast 

317 

318 
lemma image_vimage_eq [simp]: "f ` (f ` A) = A Int range f" 

319 
by blast 

320 

321 
lemma surj_image_vimage_eq: "surj f ==> f ` (f ` A) = A" 

322 
by (simp add: surj_range) 

323 

324 
lemma inj_vimage_image_eq: "inj f ==> f ` (f ` A) = A" 

325 
by (simp add: inj_on_def, blast) 

326 

327 
lemma vimage_subsetD: "surj f ==> f ` B <= A ==> B <= f ` A" 

328 
apply (unfold surj_def) 

329 
apply (blast intro: sym) 

330 
done 

331 

332 
lemma vimage_subsetI: "inj f ==> B <= f ` A ==> f ` B <= A" 

333 
by (unfold inj_on_def, blast) 

334 

335 
lemma vimage_subset_eq: "bij f ==> (f ` B <= A) = (B <= f ` A)" 

336 
apply (unfold bij_def) 

337 
apply (blast del: subsetI intro: vimage_subsetI vimage_subsetD) 

338 
done 

339 

340 
lemma image_Int_subset: "f`(A Int B) <= f`A Int f`B" 

341 
by blast 

342 

343 
lemma image_diff_subset: "f`A  f`B <= f`(A  B)" 

344 
by blast 

5852  345 

13585  346 
lemma inj_on_image_Int: 
347 
"[ inj_on f C; A<=C; B<=C ] ==> f`(A Int B) = f`A Int f`B" 

348 
apply (simp add: inj_on_def, blast) 

349 
done 

350 

351 
lemma inj_on_image_set_diff: 

352 
"[ inj_on f C; A<=C; B<=C ] ==> f`(AB) = f`A  f`B" 

353 
apply (simp add: inj_on_def, blast) 

354 
done 

355 

356 
lemma image_Int: "inj f ==> f`(A Int B) = f`A Int f`B" 

357 
by (simp add: inj_on_def, blast) 

358 

359 
lemma image_set_diff: "inj f ==> f`(AB) = f`A  f`B" 

360 
by (simp add: inj_on_def, blast) 

361 

362 
lemma inj_image_mem_iff: "inj f ==> (f a : f`A) = (a : A)" 

363 
by (blast dest: injD) 

364 

365 
lemma inj_image_subset_iff: "inj f ==> (f`A <= f`B) = (A<=B)" 

366 
by (simp add: inj_on_def, blast) 

367 

368 
lemma inj_image_eq_iff: "inj f ==> (f`A = f`B) = (A = B)" 

369 
by (blast dest: injD) 

370 

371 
lemma image_UN: "(f ` (UNION A B)) = (UN x:A.(f ` (B x)))" 

372 
by blast 

373 

374 
(*injectivity's required. Lefttoright inclusion holds even if A is empty*) 

375 
lemma image_INT: 

376 
"[ inj_on f C; ALL x:A. B x <= C; j:A ] 

377 
==> f ` (INTER A B) = (INT x:A. f ` B x)" 

378 
apply (simp add: inj_on_def, blast) 

379 
done 

380 

381 
(*Compare with image_INT: no use of inj_on, and if f is surjective then 

382 
it doesn't matter whether A is empty*) 

383 
lemma bij_image_INT: "bij f ==> f ` (INTER A B) = (INT x:A. f ` B x)" 

384 
apply (simp add: bij_def) 

385 
apply (simp add: inj_on_def surj_def, blast) 

386 
done 

387 

388 
lemma surj_Compl_image_subset: "surj f ==> (f`A) <= f`(A)" 

389 
by (auto simp add: surj_def) 

390 

391 
lemma inj_image_Compl_subset: "inj f ==> f`(A) <= (f`A)" 

392 
by (auto simp add: inj_on_def) 

5852  393 

13585  394 
lemma bij_image_Compl_eq: "bij f ==> f`(A) = (f`A)" 
395 
apply (simp add: bij_def) 

396 
apply (rule equalityI) 

397 
apply (simp_all (no_asm_simp) add: inj_image_Compl_subset surj_Compl_image_subset) 

398 
done 

399 

400 

401 
subsection{*Function Updating*} 

402 

403 
lemma fun_upd_idem_iff: "(f(x:=y) = f) = (f x = y)" 

404 
apply (simp add: fun_upd_def, safe) 

405 
apply (erule subst) 

406 
apply (rule_tac [2] ext, auto) 

407 
done 

408 

409 
(* f x = y ==> f(x:=y) = f *) 

410 
lemmas fun_upd_idem = fun_upd_idem_iff [THEN iffD2, standard] 

411 

412 
(* f(x := f x) = f *) 

17084
fb0a80aef0be
classical rules must have names for ATP integration
paulson
parents:
16973
diff
changeset

413 
lemmas fun_upd_triv = refl [THEN fun_upd_idem] 
fb0a80aef0be
classical rules must have names for ATP integration
paulson
parents:
16973
diff
changeset

414 
declare fun_upd_triv [iff] 
13585  415 

416 
lemma fun_upd_apply [simp]: "(f(x:=y))z = (if z=x then y else f z)" 

17084
fb0a80aef0be
classical rules must have names for ATP integration
paulson
parents:
16973
diff
changeset

417 
by (simp add: fun_upd_def) 
13585  418 

419 
(* fun_upd_apply supersedes these two, but they are useful 

420 
if fun_upd_apply is intentionally removed from the simpset *) 

421 
lemma fun_upd_same: "(f(x:=y)) x = y" 

422 
by simp 

423 

424 
lemma fun_upd_other: "z~=x ==> (f(x:=y)) z = f z" 

425 
by simp 

426 

427 
lemma fun_upd_upd [simp]: "f(x:=y,x:=z) = f(x:=z)" 

428 
by (simp add: expand_fun_eq) 

429 

430 
lemma fun_upd_twist: "a ~= c ==> (m(a:=b))(c:=d) = (m(c:=d))(a:=b)" 

431 
by (rule ext, auto) 

432 

15303  433 
lemma inj_on_fun_updI: "\<lbrakk> inj_on f A; y \<notin> f`A \<rbrakk> \<Longrightarrow> inj_on (f(x:=y)) A" 
434 
by(fastsimp simp:inj_on_def image_def) 

435 

15510  436 
lemma fun_upd_image: 
437 
"f(x:=y) ` A = (if x \<in> A then insert y (f ` (A{x})) else f ` A)" 

438 
by auto 

439 

15691  440 
subsection{* @{text override_on} *} 
13910  441 

15691  442 
lemma override_on_emptyset[simp]: "override_on f g {} = f" 
443 
by(simp add:override_on_def) 

13910  444 

15691  445 
lemma override_on_apply_notin[simp]: "a ~: A ==> (override_on f g A) a = f a" 
446 
by(simp add:override_on_def) 

13910  447 

15691  448 
lemma override_on_apply_in[simp]: "a : A ==> (override_on f g A) a = g a" 
449 
by(simp add:override_on_def) 

13910  450 

15510  451 
subsection{* swap *} 
452 

22744
5cbe966d67a2
Isar definitions are now added explicitly to code theorem table
haftmann
parents:
22577
diff
changeset

453 
definition 
5cbe966d67a2
Isar definitions are now added explicitly to code theorem table
haftmann
parents:
22577
diff
changeset

454 
swap :: "'a \<Rightarrow> 'a \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b)" 
5cbe966d67a2
Isar definitions are now added explicitly to code theorem table
haftmann
parents:
22577
diff
changeset

455 
where 
5cbe966d67a2
Isar definitions are now added explicitly to code theorem table
haftmann
parents:
22577
diff
changeset

456 
"swap a b f = f (a := f b, b:= f a)" 
15510  457 

458 
lemma swap_self: "swap a a f = f" 

15691  459 
by (simp add: swap_def) 
15510  460 

461 
lemma swap_commute: "swap a b f = swap b a f" 

462 
by (rule ext, simp add: fun_upd_def swap_def) 

463 

464 
lemma swap_nilpotent [simp]: "swap a b (swap a b f) = f" 

465 
by (rule ext, simp add: fun_upd_def swap_def) 

466 

467 
lemma inj_on_imp_inj_on_swap: 

22744
5cbe966d67a2
Isar definitions are now added explicitly to code theorem table
haftmann
parents:
22577
diff
changeset

468 
"[inj_on f A; a \<in> A; b \<in> A] ==> inj_on (swap a b f) A" 
15510  469 
by (simp add: inj_on_def swap_def, blast) 
470 

471 
lemma inj_on_swap_iff [simp]: 

472 
assumes A: "a \<in> A" "b \<in> A" shows "inj_on (swap a b f) A = inj_on f A" 

473 
proof 

474 
assume "inj_on (swap a b f) A" 

475 
with A have "inj_on (swap a b (swap a b f)) A" 

17589  476 
by (iprover intro: inj_on_imp_inj_on_swap) 
15510  477 
thus "inj_on f A" by simp 
478 
next 

479 
assume "inj_on f A" 

17589  480 
with A show "inj_on (swap a b f) A" by (iprover intro: inj_on_imp_inj_on_swap) 
15510  481 
qed 
482 

483 
lemma surj_imp_surj_swap: "surj f ==> surj (swap a b f)" 

484 
apply (simp add: surj_def swap_def, clarify) 

485 
apply (rule_tac P = "y = f b" in case_split_thm, blast) 

486 
apply (rule_tac P = "y = f a" in case_split_thm, auto) 

487 
{*We don't yet have @{text case_tac}*} 

488 
done 

489 

490 
lemma surj_swap_iff [simp]: "surj (swap a b f) = surj f" 

491 
proof 

492 
assume "surj (swap a b f)" 

493 
hence "surj (swap a b (swap a b f))" by (rule surj_imp_surj_swap) 

494 
thus "surj f" by simp 

495 
next 

496 
assume "surj f" 

497 
thus "surj (swap a b f)" by (rule surj_imp_surj_swap) 

498 
qed 

499 

500 
lemma bij_swap_iff: "bij (swap a b f) = bij f" 

501 
by (simp add: bij_def) 

21547
9c9fdf4c2949
moved order arities for fun and bool to Fun/Orderings
haftmann
parents:
21327
diff
changeset

502 

9c9fdf4c2949
moved order arities for fun and bool to Fun/Orderings
haftmann
parents:
21327
diff
changeset

503 

22845  504 
subsection {* Proof tool setup *} 
505 

506 
text {* simplifies terms of the form 

507 
f(...,x:=y,...,x:=z,...) to f(...,x:=z,...) *} 

508 

24017  509 
simproc_setup fun_upd2 ("f(v := w, x := y)") = {* fn _ => 
22845  510 
let 
511 
fun gen_fun_upd NONE T _ _ = NONE 

24017  512 
 gen_fun_upd (SOME f) T x y = SOME (Const (@{const_name fun_upd}, T) $ f $ x $ y) 
22845  513 
fun dest_fun_T1 (Type (_, T :: Ts)) = T 
514 
fun find_double (t as Const (@{const_name fun_upd},T) $ f $ x $ y) = 

515 
let 

516 
fun find (Const (@{const_name fun_upd},T) $ g $ v $ w) = 

517 
if v aconv x then SOME g else gen_fun_upd (find g) T v w 

518 
 find t = NONE 

519 
in (dest_fun_T1 T, gen_fun_upd (find f) T x y) end 

24017  520 

521 
fun proc ss ct = 

522 
let 

523 
val ctxt = Simplifier.the_context ss 

524 
val t = Thm.term_of ct 

525 
in 

526 
case find_double t of 

527 
(T, NONE) => NONE 

528 
 (T, SOME rhs) => 

529 
SOME (Goal.prove ctxt [] [] (Term.equals T $ t $ rhs) 

530 
(fn _ => 

531 
rtac eq_reflection 1 THEN 

532 
rtac ext 1 THEN 

533 
simp_tac (Simplifier.inherit_context ss @{simpset}) 1)) 

534 
end 

535 
in proc end 

22845  536 
*} 
537 

538 

21870  539 
subsection {* Code generator setup *} 
540 

25886
7753e0d81b7a
Added test data generator for function type (from Pure/codegen.ML).
berghofe
parents:
24286
diff
changeset

541 
types_code 
7753e0d81b7a
Added test data generator for function type (from Pure/codegen.ML).
berghofe
parents:
24286
diff
changeset

542 
"fun" ("(_ >/ _)") 
7753e0d81b7a
Added test data generator for function type (from Pure/codegen.ML).
berghofe
parents:
24286
diff
changeset

543 
attach (term_of) {* 
7753e0d81b7a
Added test data generator for function type (from Pure/codegen.ML).
berghofe
parents:
24286
diff
changeset

544 
fun term_of_fun_type _ aT _ bT _ = Free ("<function>", aT > bT); 
7753e0d81b7a
Added test data generator for function type (from Pure/codegen.ML).
berghofe
parents:
24286
diff
changeset

545 
*} 
7753e0d81b7a
Added test data generator for function type (from Pure/codegen.ML).
berghofe
parents:
24286
diff
changeset

546 
attach (test) {* 
7753e0d81b7a
Added test data generator for function type (from Pure/codegen.ML).
berghofe
parents:
24286
diff
changeset

547 
fun gen_fun_type aF aT bG bT i = 
7753e0d81b7a
Added test data generator for function type (from Pure/codegen.ML).
berghofe
parents:
24286
diff
changeset

548 
let 
7753e0d81b7a
Added test data generator for function type (from Pure/codegen.ML).
berghofe
parents:
24286
diff
changeset

549 
val tab = ref []; 
7753e0d81b7a
Added test data generator for function type (from Pure/codegen.ML).
berghofe
parents:
24286
diff
changeset

550 
fun mk_upd (x, (_, y)) t = Const ("Fun.fun_upd", 
7753e0d81b7a
Added test data generator for function type (from Pure/codegen.ML).
berghofe
parents:
24286
diff
changeset

551 
(aT > bT) > aT > bT > aT > bT) $ t $ aF x $ y () 
7753e0d81b7a
Added test data generator for function type (from Pure/codegen.ML).
berghofe
parents:
24286
diff
changeset

552 
in 
7753e0d81b7a
Added test data generator for function type (from Pure/codegen.ML).
berghofe
parents:
24286
diff
changeset

553 
(fn x => 
7753e0d81b7a
Added test data generator for function type (from Pure/codegen.ML).
berghofe
parents:
24286
diff
changeset

554 
case AList.lookup op = (!tab) x of 
7753e0d81b7a
Added test data generator for function type (from Pure/codegen.ML).
berghofe
parents:
24286
diff
changeset

555 
NONE => 
7753e0d81b7a
Added test data generator for function type (from Pure/codegen.ML).
berghofe
parents:
24286
diff
changeset

556 
let val p as (y, _) = bG i 
7753e0d81b7a
Added test data generator for function type (from Pure/codegen.ML).
berghofe
parents:
24286
diff
changeset

557 
in (tab := (x, p) :: !tab; y) end 
7753e0d81b7a
Added test data generator for function type (from Pure/codegen.ML).
berghofe
parents:
24286
diff
changeset

558 
 SOME (y, _) => y, 
7753e0d81b7a
Added test data generator for function type (from Pure/codegen.ML).
berghofe
parents:
24286
diff
changeset

559 
fn () => Basics.fold mk_upd (!tab) (Const ("arbitrary", aT > bT))) 
7753e0d81b7a
Added test data generator for function type (from Pure/codegen.ML).
berghofe
parents:
24286
diff
changeset

560 
end; 
7753e0d81b7a
Added test data generator for function type (from Pure/codegen.ML).
berghofe
parents:
24286
diff
changeset

561 
*} 
7753e0d81b7a
Added test data generator for function type (from Pure/codegen.ML).
berghofe
parents:
24286
diff
changeset

562 

21870  563 
code_const "op \<circ>" 
564 
(SML infixl 5 "o") 

565 
(Haskell infixr 9 ".") 

566 

21906  567 
code_const "id" 
568 
(Haskell "id") 

569 

21870  570 

21547
9c9fdf4c2949
moved order arities for fun and bool to Fun/Orderings
haftmann
parents:
21327
diff
changeset

571 
subsection {* ML legacy bindings *} 
15510  572 

22845  573 
ML {* 
574 
val set_cs = claset() delrules [equalityI] 

575 
*} 

5852  576 

22845  577 
ML {* 
578 
val id_apply = @{thm id_apply} 

579 
val id_def = @{thm id_def} 

580 
val o_apply = @{thm o_apply} 

581 
val o_assoc = @{thm o_assoc} 

582 
val o_def = @{thm o_def} 

583 
val injD = @{thm injD} 

584 
val datatype_injI = @{thm datatype_injI} 

585 
val range_ex1_eq = @{thm range_ex1_eq} 

586 
val expand_fun_eq = @{thm expand_fun_eq} 

13585  587 
*} 
5852  588 

2912  589 
end 