author  hoelzl 
Thu, 02 Sep 2010 11:54:09 +0200  
changeset 39076  b3a9b6734663 
parent 39075  a18e5946d63c 
child 39101  606432dd1896 
permissions  rwrr 
1475  1 
(* Title: HOL/Fun.thy 
2 
Author: Tobias Nipkow, Cambridge University Computer Laboratory 

923  3 
Copyright 1994 University of Cambridge 
18154  4 
*) 
923  5 

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

15510  8 
theory Fun 
32139  9 
imports Complete_Lattice 
15131  10 
begin 
2912  11 

26147  12 
text{*As a simplification rule, it replaces all function equalities by 
13 
firstorder equalities.*} 

14 
lemma expand_fun_eq: "f = g \<longleftrightarrow> (\<forall>x. f x = g x)" 

15 
apply (rule iffI) 

16 
apply (simp (no_asm_simp)) 

17 
apply (rule ext) 

18 
apply (simp (no_asm_simp)) 

19 
done 

5305  20 

26147  21 
lemma apply_inverse: 
26357  22 
"f x = u \<Longrightarrow> (\<And>x. P x \<Longrightarrow> g (f x) = x) \<Longrightarrow> P x \<Longrightarrow> x = g u" 
26147  23 
by auto 
2912  24 

12258  25 

26147  26 
subsection {* The Identity Function @{text id} *} 
6171  27 

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

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

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

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

31 
"id = (\<lambda>x. x)" 
13910  32 

26147  33 
lemma id_apply [simp]: "id x = x" 
34 
by (simp add: id_def) 

35 

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

37 
by blast 

38 

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

40 
by (simp add: id_def) 

41 

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

43 
by blast 

44 

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

46 
by (simp add: id_def) 

47 

48 

49 
subsection {* The Composition Operator @{text "f \<circ> g"} *} 

50 

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

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

52 
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

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

54 
"f o g = (\<lambda>x. f (g x))" 
11123  55 

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

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

58 

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

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

61 

13585  62 
text{*compatibility*} 
63 
lemmas o_def = comp_def 

2912  64 

13585  65 
lemma o_apply [simp]: "(f o g) x = f (g x)" 
66 
by (simp add: comp_def) 

67 

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

69 
by (simp add: comp_def) 

70 

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

72 
by (simp add: comp_def) 

73 

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

75 
by (simp add: comp_def) 

76 

34150  77 
lemma o_eq_dest: 
78 
"a o b = c o d \<Longrightarrow> a (b v) = c (d v)" 

79 
by (simp only: o_def) (fact fun_cong) 

80 

81 
lemma o_eq_elim: 

82 
"a o b = c o d \<Longrightarrow> ((\<And>v. a (b v) = c (d v)) \<Longrightarrow> R) \<Longrightarrow> R" 

83 
by (erule meta_mp) (fact o_eq_dest) 

84 

13585  85 
lemma image_compose: "(f o g) ` r = f`(g`r)" 
86 
by (simp add: comp_def, blast) 

87 

33044  88 
lemma vimage_compose: "(g \<circ> f) ` x = f ` (g ` x)" 
89 
by auto 

90 

13585  91 
lemma UN_o: "UNION A (g o f) = UNION (f`A) g" 
92 
by (unfold comp_def, blast) 

93 

94 

26588
d83271bfaba5
removed syntax from monad combinators; renamed mbind to scomp
haftmann
parents:
26357
diff
changeset

95 
subsection {* The Forward Composition Operator @{text fcomp} *} 
26357  96 

97 
definition 

37751  98 
fcomp :: "('a \<Rightarrow> 'b) \<Rightarrow> ('b \<Rightarrow> 'c) \<Rightarrow> 'a \<Rightarrow> 'c" (infixl "\<circ>>" 60) 
26357  99 
where 
37751  100 
"f \<circ>> g = (\<lambda>x. g (f x))" 
26357  101 

37751  102 
lemma fcomp_apply [simp]: "(f \<circ>> g) x = g (f x)" 
26357  103 
by (simp add: fcomp_def) 
104 

37751  105 
lemma fcomp_assoc: "(f \<circ>> g) \<circ>> h = f \<circ>> (g \<circ>> h)" 
26357  106 
by (simp add: fcomp_def) 
107 

37751  108 
lemma id_fcomp [simp]: "id \<circ>> g = g" 
26357  109 
by (simp add: fcomp_def) 
110 

37751  111 
lemma fcomp_id [simp]: "f \<circ>> id = f" 
26357  112 
by (simp add: fcomp_def) 
113 

31202
52d332f8f909
pretty printing of functional combinators for evaluation code
haftmann
parents:
31080
diff
changeset

114 
code_const fcomp 
52d332f8f909
pretty printing of functional combinators for evaluation code
haftmann
parents:
31080
diff
changeset

115 
(Eval infixl 1 "#>") 
52d332f8f909
pretty printing of functional combinators for evaluation code
haftmann
parents:
31080
diff
changeset

116 

37751  117 
no_notation fcomp (infixl "\<circ>>" 60) 
26588
d83271bfaba5
removed syntax from monad combinators; renamed mbind to scomp
haftmann
parents:
26357
diff
changeset

118 

26357  119 

39076
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

120 
subsection {* Injectivity, Surjectivity and Bijectivity *} 
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

121 

b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

122 
definition inj_on :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a set \<Rightarrow> bool" where  "injective" 
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

123 
"inj_on f A \<longleftrightarrow> (\<forall>x\<in>A. \<forall>y\<in>A. f x = f y \<longrightarrow> x = y)" 
26147  124 

39076
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

125 
definition surj_on :: "('a \<Rightarrow> 'b) \<Rightarrow> 'b set \<Rightarrow> bool" where  "surjective" 
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

126 
"surj_on f B \<longleftrightarrow> B \<subseteq> range f" 
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

127 

b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

128 
definition bij_betw :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a set \<Rightarrow> 'b set \<Rightarrow> bool" where  "bijective" 
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

129 
"bij_betw f A B \<longleftrightarrow> inj_on f A \<and> f ` A = B" 
26147  130 

131 
text{*A common special case: functions injective over the entire domain type.*} 

132 

133 
abbreviation 

39076
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

134 
"inj f \<equiv> inj_on f UNIV" 
26147  135 

39076
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

136 
abbreviation 
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

137 
"surj f \<equiv> surj_on f UNIV" 
13585  138 

39076
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

139 
abbreviation 
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

140 
"bij f \<equiv> bij_betw f UNIV UNIV" 
26147  141 

142 
lemma injI: 

143 
assumes "\<And>x y. f x = f y \<Longrightarrow> x = y" 

144 
shows "inj f" 

145 
using assms unfolding inj_on_def by auto 

13585  146 

31775  147 
text{*For Proofs in @{text "Tools/Datatype/datatype_rep_proofs"}*} 
13585  148 
lemma datatype_injI: 
149 
"(!! x. ALL y. f(x) = f(y) > x=y) ==> inj(f)" 

150 
by (simp add: inj_on_def) 

151 

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

154 

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

157 

32988  158 
lemma inj_on_eq_iff: "inj_on f A ==> x:A ==> y:A ==> (f(x) = f(y)) = (x=y)" 
13585  159 
by (force simp add: inj_on_def) 
160 

38620  161 
lemma inj_comp: 
162 
"inj f \<Longrightarrow> inj g \<Longrightarrow> inj (f \<circ> g)" 

163 
by (simp add: inj_on_def) 

164 

165 
lemma inj_fun: "inj f \<Longrightarrow> inj (\<lambda>x y. f x)" 

166 
by (simp add: inj_on_def expand_fun_eq) 

167 

32988  168 
lemma inj_eq: "inj f ==> (f(x) = f(y)) = (x=y)" 
169 
by (simp add: inj_on_eq_iff) 

170 

26147  171 
lemma inj_on_id[simp]: "inj_on id A" 
39076
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

172 
by (simp add: inj_on_def) 
13585  173 

26147  174 
lemma inj_on_id2[simp]: "inj_on (%x. x) A" 
39076
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

175 
by (simp add: inj_on_def) 
26147  176 

39076
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

177 
lemma surj_id[simp]: "surj_on id A" 
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

178 
by (simp add: surj_on_def) 
26147  179 

39076
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

180 
lemma bij_id[simp]: "bij_betw id A A" 
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

181 
by (simp add: bij_betw_def) 
13585  182 

183 
lemma inj_onI: 

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

185 
by (simp add: inj_on_def) 

186 

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

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

189 

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

191 
by (unfold inj_on_def, blast) 

192 

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

194 
by (blast dest!: inj_onD) 

195 

196 
lemma comp_inj_on: 

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

198 
by (simp add: comp_def inj_on_def) 

199 

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

202 
apply blast 

203 
done 

204 

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

207 
apply(unfold inj_on_def) 

208 
apply blast 

209 
done 

210 

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

12258  213 

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

216 

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

219 

15303  220 
lemma subset_inj_on: "[ inj_on f B; A <= B ] ==> inj_on f A" 
13585  221 
by (unfold inj_on_def, blast) 
222 

15111  223 
lemma inj_on_Un: 
224 
"inj_on f (A Un B) = 

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

226 
apply(unfold inj_on_def) 

227 
apply (blast intro:sym) 

228 
done 

229 

230 
lemma inj_on_insert[iff]: 

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

232 
apply(unfold inj_on_def) 

233 
apply (blast intro:sym) 

234 
done 

235 

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

237 
apply(unfold inj_on_def) 

238 
apply (blast) 

239 
done 

240 

39076
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

241 
lemma surj_onI: "(\<And>x. x \<in> B \<Longrightarrow> g (f x) = x) \<Longrightarrow> surj_on g B" 
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

242 
by (simp add: surj_on_def) (blast intro: sym) 
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

243 

b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

244 
lemma surj_onD: "surj_on f B \<Longrightarrow> y \<in> B \<Longrightarrow> \<exists>x. y = f x" 
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

245 
by (auto simp: surj_on_def) 
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

246 

b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

247 
lemma surj_on_range_iff: "surj_on f B \<longleftrightarrow> (\<exists>A. f ` A = B)" 
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

248 
unfolding surj_on_def by (auto intro!: exI[of _ "f ` B"]) 
13585  249 

39076
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

250 
lemma surj_def: "surj f \<longleftrightarrow> (\<forall>y. \<exists>x. y = f x)" 
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

251 
by (simp add: surj_on_def subset_eq image_iff) 
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

252 

b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

253 
lemma surjI: "(\<And> x. g (f x) = x) \<Longrightarrow> surj g" 
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

254 
by (blast intro: surj_onI) 
13585  255 

39076
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

256 
lemma surjD: "surj f \<Longrightarrow> \<exists>x. y = f x" 
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

257 
by (simp add: surj_def) 
13585  258 

39076
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

259 
lemma surjE: "surj f \<Longrightarrow> (\<And>x. y = f x \<Longrightarrow> C) \<Longrightarrow> C" 
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

260 
by (simp add: surj_def, blast) 
13585  261 

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

263 
apply (simp add: comp_def surj_def, clarify) 

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

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

266 
done 

267 

39076
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

268 
lemma surj_range: "surj f \<Longrightarrow> range f = UNIV" 
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

269 
by (auto simp add: surj_on_def) 
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

270 

39074  271 
lemma surj_range_iff: "surj f \<longleftrightarrow> range f = UNIV" 
39076
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

272 
unfolding surj_on_def by auto 
39074  273 

274 
lemma bij_betw_imp_surj: "bij_betw f A UNIV \<Longrightarrow> surj f" 

275 
unfolding bij_betw_def surj_range_iff by auto 

276 

39076
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

277 
lemma bij_def: "bij f \<longleftrightarrow> inj f \<and> surj f" 
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

278 
unfolding surj_range_iff bij_betw_def .. 
39074  279 

13585  280 
lemma bijI: "[ inj f; surj f ] ==> bij f" 
281 
by (simp add: bij_def) 

282 

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

284 
by (simp add: bij_def) 

285 

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

287 
by (simp add: bij_def) 

288 

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

289 
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

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

291 

39076
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

292 
lemma bij_betw_imp_surj_on: "bij_betw f A B \<Longrightarrow> surj_on f B" 
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

293 
by (auto simp: bij_betw_def surj_on_range_iff) 
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

294 

32337  295 
lemma bij_comp: "bij f \<Longrightarrow> bij g \<Longrightarrow> bij (g o f)" 
296 
by(fastsimp intro: comp_inj_on comp_surj simp: bij_def surj_range) 

297 

31438  298 
lemma bij_betw_trans: 
299 
"bij_betw f A B \<Longrightarrow> bij_betw g B C \<Longrightarrow> bij_betw (g o f) A C" 

300 
by(auto simp add:bij_betw_def comp_inj_on) 

301 

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

302 
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

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

304 
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

305 
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

306 
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

307 
{ 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

308 
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

309 
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

310 
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

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

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

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

314 
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

315 
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

316 
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

317 
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

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

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

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

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

322 
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

323 
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

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

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

326 
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

327 
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

328 
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

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

330 
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

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

332 

39075  333 
lemma bij_betw_combine: 
334 
assumes "bij_betw f A B" "bij_betw f C D" "B \<inter> D = {}" 

335 
shows "bij_betw f (A \<union> C) (B \<union> D)" 

336 
using assms unfolding bij_betw_def inj_on_Un image_Un by auto 

337 

13585  338 
lemma surj_image_vimage_eq: "surj f ==> f ` (f ` A) = A" 
339 
by (simp add: surj_range) 

340 

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

342 
by (simp add: inj_on_def, blast) 

343 

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

345 
apply (unfold surj_def) 

346 
apply (blast intro: sym) 

347 
done 

348 

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

350 
by (unfold inj_on_def, blast) 

351 

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

353 
apply (unfold bij_def) 

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

355 
done 

356 

31438  357 
lemma inj_on_Un_image_eq_iff: "inj_on f (A \<union> B) \<Longrightarrow> f ` A = f ` B \<longleftrightarrow> A = B" 
358 
by(blast dest: inj_onD) 

359 

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

362 
apply (simp add: inj_on_def, blast) 

363 
done 

364 

365 
lemma inj_on_image_set_diff: 

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

367 
apply (simp add: inj_on_def, blast) 

368 
done 

369 

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

371 
by (simp add: inj_on_def, blast) 

372 

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

374 
by (simp add: inj_on_def, blast) 

375 

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

377 
by (blast dest: injD) 

378 

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

380 
by (simp add: inj_on_def, blast) 

381 

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

383 
by (blast dest: injD) 

384 

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

386 
lemma image_INT: 

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

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

389 
apply (simp add: inj_on_def, blast) 

390 
done 

391 

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

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

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

395 
apply (simp add: bij_def) 

396 
apply (simp add: inj_on_def surj_def, blast) 

397 
done 

398 

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

400 
by (auto simp add: surj_def) 

401 

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

403 
by (auto simp add: inj_on_def) 

5852  404 

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

407 
apply (rule equalityI) 

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

409 
done 

410 

35584
768f8d92b767
generalized inj_uminus; added strict_mono_imp_inj_on
hoelzl
parents:
35580
diff
changeset

411 
lemma (in ordered_ab_group_add) inj_uminus[simp, intro]: "inj_on uminus A" 
35580  412 
by (auto intro!: inj_onI) 
13585  413 

35584
768f8d92b767
generalized inj_uminus; added strict_mono_imp_inj_on
hoelzl
parents:
35580
diff
changeset

414 
lemma (in linorder) strict_mono_imp_inj_on: "strict_mono f \<Longrightarrow> inj_on f A" 
768f8d92b767
generalized inj_uminus; added strict_mono_imp_inj_on
hoelzl
parents:
35580
diff
changeset

415 
by (auto intro!: inj_onI dest: strict_mono_eq) 
768f8d92b767
generalized inj_uminus; added strict_mono_imp_inj_on
hoelzl
parents:
35580
diff
changeset

416 

13585  417 
subsection{*Function Updating*} 
418 

35416
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35115
diff
changeset

419 
definition 
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents:
35115
diff
changeset

420 
fun_upd :: "('a => 'b) => 'a => 'b => ('a => 'b)" where 
26147  421 
"fun_upd f a b == % x. if x=a then b else f x" 
422 

423 
nonterminals 

424 
updbinds updbind 

425 
syntax 

426 
"_updbind" :: "['a, 'a] => updbind" ("(2_ :=/ _)") 

427 
"" :: "updbind => updbinds" ("_") 

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

35115  429 
"_Update" :: "['a, updbinds] => 'a" ("_/'((_)')" [1000, 0] 900) 
26147  430 

431 
translations 

35115  432 
"_Update f (_updbinds b bs)" == "_Update (_Update f b) bs" 
433 
"f(x:=y)" == "CONST fun_upd f x y" 

26147  434 

435 
(* Hint: to define the sum of two functions (or maps), use sum_case. 

436 
A nice infix syntax could be defined (in Datatype.thy or below) by 

35115  437 
notation 
438 
sum_case (infixr "'(+')"80) 

26147  439 
*) 
440 

13585  441 
lemma fun_upd_idem_iff: "(f(x:=y) = f) = (f x = y)" 
442 
apply (simp add: fun_upd_def, safe) 

443 
apply (erule subst) 

444 
apply (rule_tac [2] ext, auto) 

445 
done 

446 

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

448 
lemmas fun_upd_idem = fun_upd_idem_iff [THEN iffD2, standard] 

449 

450 
(* f(x := f x) = f *) 

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

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

452 
declare fun_upd_triv [iff] 
13585  453 

454 
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

455 
by (simp add: fun_upd_def) 
13585  456 

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

458 
if fun_upd_apply is intentionally removed from the simpset *) 

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

460 
by simp 

461 

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

463 
by simp 

464 

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

466 
by (simp add: expand_fun_eq) 

467 

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

469 
by (rule ext, auto) 

470 

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

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

476 
by auto 

477 

31080  478 
lemma fun_upd_comp: "f \<circ> (g(x := y)) = (f \<circ> g)(x := f y)" 
34209  479 
by (auto intro: ext) 
31080  480 

26147  481 

482 
subsection {* @{text override_on} *} 

483 

484 
definition 

485 
override_on :: "('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'a set \<Rightarrow> 'a \<Rightarrow> 'b" 

486 
where 

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

13910  488 

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

13910  491 

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

13910  494 

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

13910  497 

26147  498 

499 
subsection {* @{text swap} *} 

15510  500 

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

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

502 
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

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

504 
"swap a b f = f (a := f b, b:= f a)" 
15510  505 

34101  506 
lemma swap_self [simp]: "swap a a f = f" 
15691  507 
by (simp add: swap_def) 
15510  508 

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

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

511 

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

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

514 

34145  515 
lemma swap_triple: 
516 
assumes "a \<noteq> c" and "b \<noteq> c" 

517 
shows "swap a b (swap b c (swap a b f)) = swap a c f" 

518 
using assms by (simp add: expand_fun_eq swap_def) 

519 

34101  520 
lemma comp_swap: "f \<circ> swap a b g = swap a b (f \<circ> g)" 
521 
by (rule ext, simp add: fun_upd_def swap_def) 

522 

39076
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

523 
lemma swap_image_eq [simp]: 
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

524 
assumes "a \<in> A" "b \<in> A" shows "swap a b f ` A = f ` A" 
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

525 
proof  
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

526 
have subset: "\<And>f. swap a b f ` A \<subseteq> f ` A" 
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

527 
using assms by (auto simp: image_iff swap_def) 
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

528 
then have "swap a b (swap a b f) ` A \<subseteq> (swap a b f) ` A" . 
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

529 
with subset[of f] show ?thesis by auto 
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

530 
qed 
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

531 

15510  532 
lemma inj_on_imp_inj_on_swap: 
39076
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

533 
"\<lbrakk>inj_on f A; a \<in> A; b \<in> A\<rbrakk> \<Longrightarrow> inj_on (swap a b f) A" 
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

534 
by (simp add: inj_on_def swap_def, blast) 
15510  535 

536 
lemma inj_on_swap_iff [simp]: 

39076
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

537 
assumes A: "a \<in> A" "b \<in> A" shows "inj_on (swap a b f) A \<longleftrightarrow> inj_on f A" 
39075  538 
proof 
15510  539 
assume "inj_on (swap a b f) A" 
39075  540 
with A have "inj_on (swap a b (swap a b f)) A" 
541 
by (iprover intro: inj_on_imp_inj_on_swap) 

542 
thus "inj_on f A" by simp 

15510  543 
next 
544 
assume "inj_on f A" 

34209  545 
with A show "inj_on (swap a b f) A" by (iprover intro: inj_on_imp_inj_on_swap) 
15510  546 
qed 
547 

39076
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

548 
lemma surj_imp_surj_swap: "surj f \<Longrightarrow> surj (swap a b f)" 
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

549 
unfolding surj_range_iff by simp 
15510  550 

39076
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

551 
lemma surj_swap_iff [simp]: "surj (swap a b f) \<longleftrightarrow> surj f" 
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

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

553 

39076
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

554 
lemma bij_betw_swap_iff [simp]: 
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

555 
"\<lbrakk> x \<in> A; y \<in> A \<rbrakk> \<Longrightarrow> bij_betw (swap x y f) A B \<longleftrightarrow> bij_betw f A B" 
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

556 
by (auto simp: bij_betw_def) 
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

557 

b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

558 
lemma bij_swap_iff [simp]: "bij (swap a b f) \<longleftrightarrow> bij f" 
b3a9b6734663
Introduce surj_on and replace surj and bij by abbreviations.
hoelzl
parents:
39075
diff
changeset

559 
by simp 
39075  560 

36176
3fe7e97ccca8
replaced generic 'hide' command by more conventional 'hide_class', 'hide_type', 'hide_const', 'hide_fact'  frees some popular keywords;
wenzelm
parents:
35584
diff
changeset

561 
hide_const (open) swap 
21547
9c9fdf4c2949
moved order arities for fun and bool to Fun/Orderings
haftmann
parents:
21327
diff
changeset

562 

31949  563 
subsection {* Inversion of injective functions *} 
564 

33057  565 
definition the_inv_into :: "'a set => ('a => 'b) => ('b => 'a)" where 
566 
"the_inv_into A f == %x. THE y. y : A & f y = x" 

32961  567 

33057  568 
lemma the_inv_into_f_f: 
569 
"[ inj_on f A; x : A ] ==> the_inv_into A f (f x) = x" 

570 
apply (simp add: the_inv_into_def inj_on_def) 

34209  571 
apply blast 
32961  572 
done 
573 

33057  574 
lemma f_the_inv_into_f: 
575 
"inj_on f A ==> y : f`A ==> f (the_inv_into A f y) = y" 

576 
apply (simp add: the_inv_into_def) 

32961  577 
apply (rule the1I2) 
578 
apply(blast dest: inj_onD) 

579 
apply blast 

580 
done 

581 

33057  582 
lemma the_inv_into_into: 
583 
"[ inj_on f A; x : f ` A; A <= B ] ==> the_inv_into A f x : B" 

584 
apply (simp add: the_inv_into_def) 

32961  585 
apply (rule the1I2) 
586 
apply(blast dest: inj_onD) 

587 
apply blast 

588 
done 

589 

33057  590 
lemma the_inv_into_onto[simp]: 
591 
"inj_on f A ==> the_inv_into A f ` (f ` A) = A" 

592 
by (fast intro:the_inv_into_into the_inv_into_f_f[symmetric]) 

32961  593 

33057  594 
lemma the_inv_into_f_eq: 
595 
"[ inj_on f A; f x = y; x : A ] ==> the_inv_into A f y = x" 

32961  596 
apply (erule subst) 
33057  597 
apply (erule the_inv_into_f_f, assumption) 
32961  598 
done 
599 

33057  600 
lemma the_inv_into_comp: 
32961  601 
"[ inj_on f (g ` A); inj_on g A; x : f ` g ` A ] ==> 
33057  602 
the_inv_into A (f o g) x = (the_inv_into A g o the_inv_into (g ` A) f) x" 
603 
apply (rule the_inv_into_f_eq) 

32961  604 
apply (fast intro: comp_inj_on) 
33057  605 
apply (simp add: f_the_inv_into_f the_inv_into_into) 
606 
apply (simp add: the_inv_into_into) 

32961  607 
done 
608 

33057  609 
lemma inj_on_the_inv_into: 
610 
"inj_on f A \<Longrightarrow> inj_on (the_inv_into A f) (f ` A)" 

611 
by (auto intro: inj_onI simp: image_def the_inv_into_f_f) 

32961  612 

33057  613 
lemma bij_betw_the_inv_into: 
614 
"bij_betw f A B \<Longrightarrow> bij_betw (the_inv_into A f) B A" 

615 
by (auto simp add: bij_betw_def inj_on_the_inv_into the_inv_into_into) 

32961  616 

32998
31b19fa0de0b
Renamed inv to the_inv and turned it into an abbreviation (based on the_inv_onto).
berghofe
parents:
32988
diff
changeset

617 
abbreviation the_inv :: "('a \<Rightarrow> 'b) \<Rightarrow> ('b \<Rightarrow> 'a)" where 
33057  618 
"the_inv f \<equiv> the_inv_into UNIV f" 
32998
31b19fa0de0b
Renamed inv to the_inv and turned it into an abbreviation (based on the_inv_onto).
berghofe
parents:
32988
diff
changeset

619 

31b19fa0de0b
Renamed inv to the_inv and turned it into an abbreviation (based on the_inv_onto).
berghofe
parents:
32988
diff
changeset

620 
lemma the_inv_f_f: 
31b19fa0de0b
Renamed inv to the_inv and turned it into an abbreviation (based on the_inv_onto).
berghofe
parents:
32988
diff
changeset

621 
assumes "inj f" 
31b19fa0de0b
Renamed inv to the_inv and turned it into an abbreviation (based on the_inv_onto).
berghofe
parents:
32988
diff
changeset

622 
shows "the_inv f (f x) = x" using assms UNIV_I 
33057  623 
by (rule the_inv_into_f_f) 
32998
31b19fa0de0b
Renamed inv to the_inv and turned it into an abbreviation (based on the_inv_onto).
berghofe
parents:
32988
diff
changeset

624 

31949  625 

22845  626 
subsection {* Proof tool setup *} 
627 

628 
text {* simplifies terms of the form 

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

630 

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

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

637 
let 

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

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

640 
 find t = NONE 

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

24017  642 

643 
fun proc ss ct = 

644 
let 

645 
val ctxt = Simplifier.the_context ss 

646 
val t = Thm.term_of ct 

647 
in 

648 
case find_double t of 

649 
(T, NONE) => NONE 

650 
 (T, SOME rhs) => 

27330  651 
SOME (Goal.prove ctxt [] [] (Logic.mk_equals (t, rhs)) 
24017  652 
(fn _ => 
653 
rtac eq_reflection 1 THEN 

654 
rtac ext 1 THEN 

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

656 
end 

657 
in proc end 

22845  658 
*} 
659 

660 

21870  661 
subsection {* Code generator setup *} 
662 

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

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

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

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

666 
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

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

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

669 
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

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

672 
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

673 
(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

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

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

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

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

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

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

680 
 SOME (y, _) => y, 
28711  681 
fn () => Basics.fold mk_upd (!tab) (Const ("HOL.undefined", aT > bT))) 
25886
7753e0d81b7a
Added test data generator for function type (from Pure/codegen.ML).
berghofe
parents:
24286
diff
changeset

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

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

684 

21870  685 
code_const "op \<circ>" 
686 
(SML infixl 5 "o") 

687 
(Haskell infixr 9 ".") 

688 

21906  689 
code_const "id" 
690 
(Haskell "id") 

691 

2912  692 
end 