author | haftmann |
Wed, 25 Nov 2009 11:16:57 +0100 | |
changeset 33960 | 53993394ac19 |
parent 33318 | ddd97d9dfbfb |
child 34101 | d689f0b33047 |
permissions | -rw-r--r-- |
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 |
first-order 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 |
||
77 |
lemma image_compose: "(f o g) ` r = f`(g`r)" |
|
78 |
by (simp add: comp_def, blast) |
|
79 |
||
33044 | 80 |
lemma vimage_compose: "(g \<circ> f) -` x = f -` (g -` x)" |
81 |
by auto |
|
82 |
||
13585 | 83 |
lemma UN_o: "UNION A (g o f) = UNION (f`A) g" |
84 |
by (unfold comp_def, blast) |
|
85 |
||
86 |
||
26588
d83271bfaba5
removed syntax from monad combinators; renamed mbind to scomp
haftmann
parents:
26357
diff
changeset
|
87 |
subsection {* The Forward Composition Operator @{text fcomp} *} |
26357 | 88 |
|
89 |
definition |
|
90 |
fcomp :: "('a \<Rightarrow> 'b) \<Rightarrow> ('b \<Rightarrow> 'c) \<Rightarrow> 'a \<Rightarrow> 'c" (infixl "o>" 60) |
|
91 |
where |
|
92 |
"f o> g = (\<lambda>x. g (f x))" |
|
93 |
||
94 |
lemma fcomp_apply: "(f o> g) x = g (f x)" |
|
95 |
by (simp add: fcomp_def) |
|
96 |
||
97 |
lemma fcomp_assoc: "(f o> g) o> h = f o> (g o> h)" |
|
98 |
by (simp add: fcomp_def) |
|
99 |
||
100 |
lemma id_fcomp [simp]: "id o> g = g" |
|
101 |
by (simp add: fcomp_def) |
|
102 |
||
103 |
lemma fcomp_id [simp]: "f o> id = f" |
|
104 |
by (simp add: fcomp_def) |
|
105 |
||
31202
52d332f8f909
pretty printing of functional combinators for evaluation code
haftmann
parents:
31080
diff
changeset
|
106 |
code_const fcomp |
52d332f8f909
pretty printing of functional combinators for evaluation code
haftmann
parents:
31080
diff
changeset
|
107 |
(Eval infixl 1 "#>") |
52d332f8f909
pretty printing of functional combinators for evaluation code
haftmann
parents:
31080
diff
changeset
|
108 |
|
26588
d83271bfaba5
removed syntax from monad combinators; renamed mbind to scomp
haftmann
parents:
26357
diff
changeset
|
109 |
no_notation fcomp (infixl "o>" 60) |
d83271bfaba5
removed syntax from monad combinators; renamed mbind to scomp
haftmann
parents:
26357
diff
changeset
|
110 |
|
26357 | 111 |
|
26147 | 112 |
subsection {* Injectivity and Surjectivity *} |
113 |
||
114 |
constdefs |
|
115 |
inj_on :: "['a => 'b, 'a set] => bool" -- "injective" |
|
116 |
"inj_on f A == ! x:A. ! y:A. f(x)=f(y) --> x=y" |
|
117 |
||
118 |
text{*A common special case: functions injective over the entire domain type.*} |
|
119 |
||
120 |
abbreviation |
|
121 |
"inj f == inj_on f UNIV" |
|
13585 | 122 |
|
26147 | 123 |
definition |
124 |
bij_betw :: "('a => 'b) => 'a set => 'b set => bool" where -- "bijective" |
|
28562 | 125 |
[code del]: "bij_betw f A B \<longleftrightarrow> inj_on f A & f ` A = B" |
26147 | 126 |
|
127 |
constdefs |
|
128 |
surj :: "('a => 'b) => bool" (*surjective*) |
|
129 |
"surj f == ! y. ? x. y=f(x)" |
|
13585 | 130 |
|
26147 | 131 |
bij :: "('a => 'b) => bool" (*bijective*) |
132 |
"bij f == inj f & surj f" |
|
133 |
||
134 |
lemma injI: |
|
135 |
assumes "\<And>x y. f x = f y \<Longrightarrow> x = y" |
|
136 |
shows "inj f" |
|
137 |
using assms unfolding inj_on_def by auto |
|
13585 | 138 |
|
31775 | 139 |
text{*For Proofs in @{text "Tools/Datatype/datatype_rep_proofs"}*} |
13585 | 140 |
lemma datatype_injI: |
141 |
"(!! x. ALL y. f(x) = f(y) --> x=y) ==> inj(f)" |
|
142 |
by (simp add: inj_on_def) |
|
143 |
||
13637 | 144 |
theorem range_ex1_eq: "inj f \<Longrightarrow> b : range f = (EX! x. b = f x)" |
145 |
by (unfold inj_on_def, blast) |
|
146 |
||
13585 | 147 |
lemma injD: "[| inj(f); f(x) = f(y) |] ==> x=y" |
148 |
by (simp add: inj_on_def) |
|
149 |
||
32988 | 150 |
lemma inj_on_eq_iff: "inj_on f A ==> x:A ==> y:A ==> (f(x) = f(y)) = (x=y)" |
13585 | 151 |
by (force simp add: inj_on_def) |
152 |
||
32988 | 153 |
lemma inj_eq: "inj f ==> (f(x) = f(y)) = (x=y)" |
154 |
by (simp add: inj_on_eq_iff) |
|
155 |
||
26147 | 156 |
lemma inj_on_id[simp]: "inj_on id A" |
157 |
by (simp add: inj_on_def) |
|
13585 | 158 |
|
26147 | 159 |
lemma inj_on_id2[simp]: "inj_on (%x. x) A" |
160 |
by (simp add: inj_on_def) |
|
161 |
||
162 |
lemma surj_id[simp]: "surj id" |
|
163 |
by (simp add: surj_def) |
|
164 |
||
165 |
lemma bij_id[simp]: "bij id" |
|
166 |
by (simp add: bij_def inj_on_id surj_id) |
|
13585 | 167 |
|
168 |
lemma inj_onI: |
|
169 |
"(!! x y. [| x:A; y:A; f(x) = f(y) |] ==> x=y) ==> inj_on f A" |
|
170 |
by (simp add: inj_on_def) |
|
171 |
||
172 |
lemma inj_on_inverseI: "(!!x. x:A ==> g(f(x)) = x) ==> inj_on f A" |
|
173 |
by (auto dest: arg_cong [of concl: g] simp add: inj_on_def) |
|
174 |
||
175 |
lemma inj_onD: "[| inj_on f A; f(x)=f(y); x:A; y:A |] ==> x=y" |
|
176 |
by (unfold inj_on_def, blast) |
|
177 |
||
178 |
lemma inj_on_iff: "[| inj_on f A; x:A; y:A |] ==> (f(x)=f(y)) = (x=y)" |
|
179 |
by (blast dest!: inj_onD) |
|
180 |
||
181 |
lemma comp_inj_on: |
|
182 |
"[| inj_on f A; inj_on g (f`A) |] ==> inj_on (g o f) A" |
|
183 |
by (simp add: comp_def inj_on_def) |
|
184 |
||
15303 | 185 |
lemma inj_on_imageI: "inj_on (g o f) A \<Longrightarrow> inj_on g (f ` A)" |
186 |
apply(simp add:inj_on_def image_def) |
|
187 |
apply blast |
|
188 |
done |
|
189 |
||
15439 | 190 |
lemma inj_on_image_iff: "\<lbrakk> ALL x:A. ALL y:A. (g(f x) = g(f y)) = (g x = g y); |
191 |
inj_on f A \<rbrakk> \<Longrightarrow> inj_on g (f ` A) = inj_on g A" |
|
192 |
apply(unfold inj_on_def) |
|
193 |
apply blast |
|
194 |
done |
|
195 |
||
13585 | 196 |
lemma inj_on_contraD: "[| inj_on f A; ~x=y; x:A; y:A |] ==> ~ f(x)=f(y)" |
197 |
by (unfold inj_on_def, blast) |
|
12258 | 198 |
|
13585 | 199 |
lemma inj_singleton: "inj (%s. {s})" |
200 |
by (simp add: inj_on_def) |
|
201 |
||
15111 | 202 |
lemma inj_on_empty[iff]: "inj_on f {}" |
203 |
by(simp add: inj_on_def) |
|
204 |
||
15303 | 205 |
lemma subset_inj_on: "[| inj_on f B; A <= B |] ==> inj_on f A" |
13585 | 206 |
by (unfold inj_on_def, blast) |
207 |
||
15111 | 208 |
lemma inj_on_Un: |
209 |
"inj_on f (A Un B) = |
|
210 |
(inj_on f A & inj_on f B & f`(A-B) Int f`(B-A) = {})" |
|
211 |
apply(unfold inj_on_def) |
|
212 |
apply (blast intro:sym) |
|
213 |
done |
|
214 |
||
215 |
lemma inj_on_insert[iff]: |
|
216 |
"inj_on f (insert a A) = (inj_on f A & f a ~: f`(A-{a}))" |
|
217 |
apply(unfold inj_on_def) |
|
218 |
apply (blast intro:sym) |
|
219 |
done |
|
220 |
||
221 |
lemma inj_on_diff: "inj_on f A ==> inj_on f (A-B)" |
|
222 |
apply(unfold inj_on_def) |
|
223 |
apply (blast) |
|
224 |
done |
|
225 |
||
13585 | 226 |
lemma surjI: "(!! x. g(f x) = x) ==> surj g" |
227 |
apply (simp add: surj_def) |
|
228 |
apply (blast intro: sym) |
|
229 |
done |
|
230 |
||
231 |
lemma surj_range: "surj f ==> range f = UNIV" |
|
232 |
by (auto simp add: surj_def) |
|
233 |
||
234 |
lemma surjD: "surj f ==> EX x. y = f x" |
|
235 |
by (simp add: surj_def) |
|
236 |
||
237 |
lemma surjE: "surj f ==> (!!x. y = f x ==> C) ==> C" |
|
238 |
by (simp add: surj_def, blast) |
|
239 |
||
240 |
lemma comp_surj: "[| surj f; surj g |] ==> surj (g o f)" |
|
241 |
apply (simp add: comp_def surj_def, clarify) |
|
242 |
apply (drule_tac x = y in spec, clarify) |
|
243 |
apply (drule_tac x = x in spec, blast) |
|
244 |
done |
|
245 |
||
246 |
lemma bijI: "[| inj f; surj f |] ==> bij f" |
|
247 |
by (simp add: bij_def) |
|
248 |
||
249 |
lemma bij_is_inj: "bij f ==> inj f" |
|
250 |
by (simp add: bij_def) |
|
251 |
||
252 |
lemma bij_is_surj: "bij f ==> surj f" |
|
253 |
by (simp add: bij_def) |
|
254 |
||
26105
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset
|
255 |
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
|
256 |
by (simp add: bij_betw_def) |
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset
|
257 |
|
32337 | 258 |
lemma bij_comp: "bij f \<Longrightarrow> bij g \<Longrightarrow> bij (g o f)" |
259 |
by(fastsimp intro: comp_inj_on comp_surj simp: bij_def surj_range) |
|
260 |
||
31438 | 261 |
lemma bij_betw_trans: |
262 |
"bij_betw f A B \<Longrightarrow> bij_betw g B C \<Longrightarrow> bij_betw (g o f) A C" |
|
263 |
by(auto simp add:bij_betw_def comp_inj_on) |
|
264 |
||
26105
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset
|
265 |
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
|
266 |
proof - |
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset
|
267 |
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
|
268 |
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
|
269 |
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
|
270 |
{ 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
|
271 |
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
|
272 |
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
|
273 |
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
|
274 |
} note g = this |
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset
|
275 |
have "inj_on ?g B" |
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset
|
276 |
proof(rule inj_onI) |
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset
|
277 |
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
|
278 |
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
|
279 |
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
|
280 |
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
|
281 |
qed |
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset
|
282 |
moreover have "?g ` B = A" |
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset
|
283 |
proof(auto simp:image_def) |
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset
|
284 |
fix b assume "b:B" |
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset
|
285 |
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
|
286 |
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
|
287 |
next |
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset
|
288 |
fix a assume "a:A" |
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset
|
289 |
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
|
290 |
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
|
291 |
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
|
292 |
qed |
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset
|
293 |
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
|
294 |
qed |
ae06618225ec
moved bij_betw from Library/FuncSet to Fun, redistributed some lemmas, and
nipkow
parents:
25886
diff
changeset
|
295 |
|
13585 | 296 |
lemma surj_image_vimage_eq: "surj f ==> f ` (f -` A) = A" |
297 |
by (simp add: surj_range) |
|
298 |
||
299 |
lemma inj_vimage_image_eq: "inj f ==> f -` (f ` A) = A" |
|
300 |
by (simp add: inj_on_def, blast) |
|
301 |
||
302 |
lemma vimage_subsetD: "surj f ==> f -` B <= A ==> B <= f ` A" |
|
303 |
apply (unfold surj_def) |
|
304 |
apply (blast intro: sym) |
|
305 |
done |
|
306 |
||
307 |
lemma vimage_subsetI: "inj f ==> B <= f ` A ==> f -` B <= A" |
|
308 |
by (unfold inj_on_def, blast) |
|
309 |
||
310 |
lemma vimage_subset_eq: "bij f ==> (f -` B <= A) = (B <= f ` A)" |
|
311 |
apply (unfold bij_def) |
|
312 |
apply (blast del: subsetI intro: vimage_subsetI vimage_subsetD) |
|
313 |
done |
|
314 |
||
31438 | 315 |
lemma inj_on_Un_image_eq_iff: "inj_on f (A \<union> B) \<Longrightarrow> f ` A = f ` B \<longleftrightarrow> A = B" |
316 |
by(blast dest: inj_onD) |
|
317 |
||
13585 | 318 |
lemma inj_on_image_Int: |
319 |
"[| inj_on f C; A<=C; B<=C |] ==> f`(A Int B) = f`A Int f`B" |
|
320 |
apply (simp add: inj_on_def, blast) |
|
321 |
done |
|
322 |
||
323 |
lemma inj_on_image_set_diff: |
|
324 |
"[| inj_on f C; A<=C; B<=C |] ==> f`(A-B) = f`A - f`B" |
|
325 |
apply (simp add: inj_on_def, blast) |
|
326 |
done |
|
327 |
||
328 |
lemma image_Int: "inj f ==> f`(A Int B) = f`A Int f`B" |
|
329 |
by (simp add: inj_on_def, blast) |
|
330 |
||
331 |
lemma image_set_diff: "inj f ==> f`(A-B) = f`A - f`B" |
|
332 |
by (simp add: inj_on_def, blast) |
|
333 |
||
334 |
lemma inj_image_mem_iff: "inj f ==> (f a : f`A) = (a : A)" |
|
335 |
by (blast dest: injD) |
|
336 |
||
337 |
lemma inj_image_subset_iff: "inj f ==> (f`A <= f`B) = (A<=B)" |
|
338 |
by (simp add: inj_on_def, blast) |
|
339 |
||
340 |
lemma inj_image_eq_iff: "inj f ==> (f`A = f`B) = (A = B)" |
|
341 |
by (blast dest: injD) |
|
342 |
||
343 |
(*injectivity's required. Left-to-right inclusion holds even if A is empty*) |
|
344 |
lemma image_INT: |
|
345 |
"[| inj_on f C; ALL x:A. B x <= C; j:A |] |
|
346 |
==> f ` (INTER A B) = (INT x:A. f ` B x)" |
|
347 |
apply (simp add: inj_on_def, blast) |
|
348 |
done |
|
349 |
||
350 |
(*Compare with image_INT: no use of inj_on, and if f is surjective then |
|
351 |
it doesn't matter whether A is empty*) |
|
352 |
lemma bij_image_INT: "bij f ==> f ` (INTER A B) = (INT x:A. f ` B x)" |
|
353 |
apply (simp add: bij_def) |
|
354 |
apply (simp add: inj_on_def surj_def, blast) |
|
355 |
done |
|
356 |
||
357 |
lemma surj_Compl_image_subset: "surj f ==> -(f`A) <= f`(-A)" |
|
358 |
by (auto simp add: surj_def) |
|
359 |
||
360 |
lemma inj_image_Compl_subset: "inj f ==> f`(-A) <= -(f`A)" |
|
361 |
by (auto simp add: inj_on_def) |
|
5852 | 362 |
|
13585 | 363 |
lemma bij_image_Compl_eq: "bij f ==> f`(-A) = -(f`A)" |
364 |
apply (simp add: bij_def) |
|
365 |
apply (rule equalityI) |
|
366 |
apply (simp_all (no_asm_simp) add: inj_image_Compl_subset surj_Compl_image_subset) |
|
367 |
done |
|
368 |
||
369 |
||
370 |
subsection{*Function Updating*} |
|
371 |
||
26147 | 372 |
constdefs |
373 |
fun_upd :: "('a => 'b) => 'a => 'b => ('a => 'b)" |
|
374 |
"fun_upd f a b == % x. if x=a then b else f x" |
|
375 |
||
376 |
nonterminals |
|
377 |
updbinds updbind |
|
378 |
syntax |
|
379 |
"_updbind" :: "['a, 'a] => updbind" ("(2_ :=/ _)") |
|
380 |
"" :: "updbind => updbinds" ("_") |
|
381 |
"_updbinds":: "[updbind, updbinds] => updbinds" ("_,/ _") |
|
382 |
"_Update" :: "['a, updbinds] => 'a" ("_/'((_)')" [1000,0] 900) |
|
383 |
||
384 |
translations |
|
385 |
"_Update f (_updbinds b bs)" == "_Update (_Update f b) bs" |
|
386 |
"f(x:=y)" == "fun_upd f x y" |
|
387 |
||
388 |
(* Hint: to define the sum of two functions (or maps), use sum_case. |
|
389 |
A nice infix syntax could be defined (in Datatype.thy or below) by |
|
390 |
consts |
|
391 |
fun_sum :: "('a => 'c) => ('b => 'c) => (('a+'b) => 'c)" (infixr "'(+')"80) |
|
392 |
translations |
|
393 |
"fun_sum" == sum_case |
|
394 |
*) |
|
395 |
||
13585 | 396 |
lemma fun_upd_idem_iff: "(f(x:=y) = f) = (f x = y)" |
397 |
apply (simp add: fun_upd_def, safe) |
|
398 |
apply (erule subst) |
|
399 |
apply (rule_tac [2] ext, auto) |
|
400 |
done |
|
401 |
||
402 |
(* f x = y ==> f(x:=y) = f *) |
|
403 |
lemmas fun_upd_idem = fun_upd_idem_iff [THEN iffD2, standard] |
|
404 |
||
405 |
(* f(x := f x) = f *) |
|
17084
fb0a80aef0be
classical rules must have names for ATP integration
paulson
parents:
16973
diff
changeset
|
406 |
lemmas fun_upd_triv = refl [THEN fun_upd_idem] |
fb0a80aef0be
classical rules must have names for ATP integration
paulson
parents:
16973
diff
changeset
|
407 |
declare fun_upd_triv [iff] |
13585 | 408 |
|
409 |
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
|
410 |
by (simp add: fun_upd_def) |
13585 | 411 |
|
412 |
(* fun_upd_apply supersedes these two, but they are useful |
|
413 |
if fun_upd_apply is intentionally removed from the simpset *) |
|
414 |
lemma fun_upd_same: "(f(x:=y)) x = y" |
|
415 |
by simp |
|
416 |
||
417 |
lemma fun_upd_other: "z~=x ==> (f(x:=y)) z = f z" |
|
418 |
by simp |
|
419 |
||
420 |
lemma fun_upd_upd [simp]: "f(x:=y,x:=z) = f(x:=z)" |
|
421 |
by (simp add: expand_fun_eq) |
|
422 |
||
423 |
lemma fun_upd_twist: "a ~= c ==> (m(a:=b))(c:=d) = (m(c:=d))(a:=b)" |
|
424 |
by (rule ext, auto) |
|
425 |
||
15303 | 426 |
lemma inj_on_fun_updI: "\<lbrakk> inj_on f A; y \<notin> f`A \<rbrakk> \<Longrightarrow> inj_on (f(x:=y)) A" |
427 |
by(fastsimp simp:inj_on_def image_def) |
|
428 |
||
15510 | 429 |
lemma fun_upd_image: |
430 |
"f(x:=y) ` A = (if x \<in> A then insert y (f ` (A-{x})) else f ` A)" |
|
431 |
by auto |
|
432 |
||
31080 | 433 |
lemma fun_upd_comp: "f \<circ> (g(x := y)) = (f \<circ> g)(x := f y)" |
434 |
by(auto intro: ext) |
|
435 |
||
26147 | 436 |
|
437 |
subsection {* @{text override_on} *} |
|
438 |
||
439 |
definition |
|
440 |
override_on :: "('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'a set \<Rightarrow> 'a \<Rightarrow> 'b" |
|
441 |
where |
|
442 |
"override_on f g A = (\<lambda>a. if a \<in> A then g a else f a)" |
|
13910 | 443 |
|
15691 | 444 |
lemma override_on_emptyset[simp]: "override_on f g {} = f" |
445 |
by(simp add:override_on_def) |
|
13910 | 446 |
|
15691 | 447 |
lemma override_on_apply_notin[simp]: "a ~: A ==> (override_on f g A) a = f a" |
448 |
by(simp add:override_on_def) |
|
13910 | 449 |
|
15691 | 450 |
lemma override_on_apply_in[simp]: "a : A ==> (override_on f g A) a = g a" |
451 |
by(simp add:override_on_def) |
|
13910 | 452 |
|
26147 | 453 |
|
454 |
subsection {* @{text swap} *} |
|
15510 | 455 |
|
22744
5cbe966d67a2
Isar definitions are now added explicitly to code theorem table
haftmann
parents:
22577
diff
changeset
|
456 |
definition |
5cbe966d67a2
Isar definitions are now added explicitly to code theorem table
haftmann
parents:
22577
diff
changeset
|
457 |
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
|
458 |
where |
5cbe966d67a2
Isar definitions are now added explicitly to code theorem table
haftmann
parents:
22577
diff
changeset
|
459 |
"swap a b f = f (a := f b, b:= f a)" |
15510 | 460 |
|
461 |
lemma swap_self: "swap a a f = f" |
|
15691 | 462 |
by (simp add: swap_def) |
15510 | 463 |
|
464 |
lemma swap_commute: "swap a b f = swap b a f" |
|
465 |
by (rule ext, simp add: fun_upd_def swap_def) |
|
466 |
||
467 |
lemma swap_nilpotent [simp]: "swap a b (swap a b f) = f" |
|
468 |
by (rule ext, simp add: fun_upd_def swap_def) |
|
469 |
||
470 |
lemma inj_on_imp_inj_on_swap: |
|
22744
5cbe966d67a2
Isar definitions are now added explicitly to code theorem table
haftmann
parents:
22577
diff
changeset
|
471 |
"[|inj_on f A; a \<in> A; b \<in> A|] ==> inj_on (swap a b f) A" |
15510 | 472 |
by (simp add: inj_on_def swap_def, blast) |
473 |
||
474 |
lemma inj_on_swap_iff [simp]: |
|
475 |
assumes A: "a \<in> A" "b \<in> A" shows "inj_on (swap a b f) A = inj_on f A" |
|
476 |
proof |
|
477 |
assume "inj_on (swap a b f) A" |
|
478 |
with A have "inj_on (swap a b (swap a b f)) A" |
|
17589 | 479 |
by (iprover intro: inj_on_imp_inj_on_swap) |
15510 | 480 |
thus "inj_on f A" by simp |
481 |
next |
|
482 |
assume "inj_on f A" |
|
27165 | 483 |
with A show "inj_on (swap a b f) A" by(iprover intro: inj_on_imp_inj_on_swap) |
15510 | 484 |
qed |
485 |
||
486 |
lemma surj_imp_surj_swap: "surj f ==> surj (swap a b f)" |
|
487 |
apply (simp add: surj_def swap_def, clarify) |
|
27125 | 488 |
apply (case_tac "y = f b", blast) |
489 |
apply (case_tac "y = f a", auto) |
|
15510 | 490 |
done |
491 |
||
492 |
lemma surj_swap_iff [simp]: "surj (swap a b f) = surj f" |
|
493 |
proof |
|
494 |
assume "surj (swap a b f)" |
|
495 |
hence "surj (swap a b (swap a b f))" by (rule surj_imp_surj_swap) |
|
496 |
thus "surj f" by simp |
|
497 |
next |
|
498 |
assume "surj f" |
|
499 |
thus "surj (swap a b f)" by (rule surj_imp_surj_swap) |
|
500 |
qed |
|
501 |
||
502 |
lemma bij_swap_iff: "bij (swap a b f) = bij f" |
|
503 |
by (simp add: bij_def) |
|
21547
9c9fdf4c2949
moved order arities for fun and bool to Fun/Orderings
haftmann
parents:
21327
diff
changeset
|
504 |
|
27188 | 505 |
hide (open) const swap |
21547
9c9fdf4c2949
moved order arities for fun and bool to Fun/Orderings
haftmann
parents:
21327
diff
changeset
|
506 |
|
31949 | 507 |
|
508 |
subsection {* Inversion of injective functions *} |
|
509 |
||
33057 | 510 |
definition the_inv_into :: "'a set => ('a => 'b) => ('b => 'a)" where |
511 |
"the_inv_into A f == %x. THE y. y : A & f y = x" |
|
32961 | 512 |
|
33057 | 513 |
lemma the_inv_into_f_f: |
514 |
"[| inj_on f A; x : A |] ==> the_inv_into A f (f x) = x" |
|
515 |
apply (simp add: the_inv_into_def inj_on_def) |
|
32961 | 516 |
apply (blast intro: the_equality) |
517 |
done |
|
518 |
||
33057 | 519 |
lemma f_the_inv_into_f: |
520 |
"inj_on f A ==> y : f`A ==> f (the_inv_into A f y) = y" |
|
521 |
apply (simp add: the_inv_into_def) |
|
32961 | 522 |
apply (rule the1I2) |
523 |
apply(blast dest: inj_onD) |
|
524 |
apply blast |
|
525 |
done |
|
526 |
||
33057 | 527 |
lemma the_inv_into_into: |
528 |
"[| inj_on f A; x : f ` A; A <= B |] ==> the_inv_into A f x : B" |
|
529 |
apply (simp add: the_inv_into_def) |
|
32961 | 530 |
apply (rule the1I2) |
531 |
apply(blast dest: inj_onD) |
|
532 |
apply blast |
|
533 |
done |
|
534 |
||
33057 | 535 |
lemma the_inv_into_onto[simp]: |
536 |
"inj_on f A ==> the_inv_into A f ` (f ` A) = A" |
|
537 |
by (fast intro:the_inv_into_into the_inv_into_f_f[symmetric]) |
|
32961 | 538 |
|
33057 | 539 |
lemma the_inv_into_f_eq: |
540 |
"[| inj_on f A; f x = y; x : A |] ==> the_inv_into A f y = x" |
|
32961 | 541 |
apply (erule subst) |
33057 | 542 |
apply (erule the_inv_into_f_f, assumption) |
32961 | 543 |
done |
544 |
||
33057 | 545 |
lemma the_inv_into_comp: |
32961 | 546 |
"[| inj_on f (g ` A); inj_on g A; x : f ` g ` A |] ==> |
33057 | 547 |
the_inv_into A (f o g) x = (the_inv_into A g o the_inv_into (g ` A) f) x" |
548 |
apply (rule the_inv_into_f_eq) |
|
32961 | 549 |
apply (fast intro: comp_inj_on) |
33057 | 550 |
apply (simp add: f_the_inv_into_f the_inv_into_into) |
551 |
apply (simp add: the_inv_into_into) |
|
32961 | 552 |
done |
553 |
||
33057 | 554 |
lemma inj_on_the_inv_into: |
555 |
"inj_on f A \<Longrightarrow> inj_on (the_inv_into A f) (f ` A)" |
|
556 |
by (auto intro: inj_onI simp: image_def the_inv_into_f_f) |
|
32961 | 557 |
|
33057 | 558 |
lemma bij_betw_the_inv_into: |
559 |
"bij_betw f A B \<Longrightarrow> bij_betw (the_inv_into A f) B A" |
|
560 |
by (auto simp add: bij_betw_def inj_on_the_inv_into the_inv_into_into) |
|
32961 | 561 |
|
32998
31b19fa0de0b
Renamed inv to the_inv and turned it into an abbreviation (based on the_inv_onto).
berghofe
parents:
32988
diff
changeset
|
562 |
abbreviation the_inv :: "('a \<Rightarrow> 'b) \<Rightarrow> ('b \<Rightarrow> 'a)" where |
33057 | 563 |
"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
|
564 |
|
31b19fa0de0b
Renamed inv to the_inv and turned it into an abbreviation (based on the_inv_onto).
berghofe
parents:
32988
diff
changeset
|
565 |
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
|
566 |
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
|
567 |
shows "the_inv f (f x) = x" using assms UNIV_I |
33057 | 568 |
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
|
569 |
|
31949 | 570 |
|
22845 | 571 |
subsection {* Proof tool setup *} |
572 |
||
573 |
text {* simplifies terms of the form |
|
574 |
f(...,x:=y,...,x:=z,...) to f(...,x:=z,...) *} |
|
575 |
||
24017 | 576 |
simproc_setup fun_upd2 ("f(v := w, x := y)") = {* fn _ => |
22845 | 577 |
let |
578 |
fun gen_fun_upd NONE T _ _ = NONE |
|
24017 | 579 |
| gen_fun_upd (SOME f) T x y = SOME (Const (@{const_name fun_upd}, T) $ f $ x $ y) |
22845 | 580 |
fun dest_fun_T1 (Type (_, T :: Ts)) = T |
581 |
fun find_double (t as Const (@{const_name fun_upd},T) $ f $ x $ y) = |
|
582 |
let |
|
583 |
fun find (Const (@{const_name fun_upd},T) $ g $ v $ w) = |
|
584 |
if v aconv x then SOME g else gen_fun_upd (find g) T v w |
|
585 |
| find t = NONE |
|
586 |
in (dest_fun_T1 T, gen_fun_upd (find f) T x y) end |
|
24017 | 587 |
|
588 |
fun proc ss ct = |
|
589 |
let |
|
590 |
val ctxt = Simplifier.the_context ss |
|
591 |
val t = Thm.term_of ct |
|
592 |
in |
|
593 |
case find_double t of |
|
594 |
(T, NONE) => NONE |
|
595 |
| (T, SOME rhs) => |
|
27330 | 596 |
SOME (Goal.prove ctxt [] [] (Logic.mk_equals (t, rhs)) |
24017 | 597 |
(fn _ => |
598 |
rtac eq_reflection 1 THEN |
|
599 |
rtac ext 1 THEN |
|
600 |
simp_tac (Simplifier.inherit_context ss @{simpset}) 1)) |
|
601 |
end |
|
602 |
in proc end |
|
22845 | 603 |
*} |
604 |
||
605 |
||
21870 | 606 |
subsection {* Code generator setup *} |
607 |
||
25886
7753e0d81b7a
Added test data generator for function type (from Pure/codegen.ML).
berghofe
parents:
24286
diff
changeset
|
608 |
types_code |
7753e0d81b7a
Added test data generator for function type (from Pure/codegen.ML).
berghofe
parents:
24286
diff
changeset
|
609 |
"fun" ("(_ ->/ _)") |
7753e0d81b7a
Added test data generator for function type (from Pure/codegen.ML).
berghofe
parents:
24286
diff
changeset
|
610 |
attach (term_of) {* |
7753e0d81b7a
Added test data generator for function type (from Pure/codegen.ML).
berghofe
parents:
24286
diff
changeset
|
611 |
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
|
612 |
*} |
7753e0d81b7a
Added test data generator for function type (from Pure/codegen.ML).
berghofe
parents:
24286
diff
changeset
|
613 |
attach (test) {* |
7753e0d81b7a
Added test data generator for function type (from Pure/codegen.ML).
berghofe
parents:
24286
diff
changeset
|
614 |
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
|
615 |
let |
32740 | 616 |
val tab = Unsynchronized.ref []; |
25886
7753e0d81b7a
Added test data generator for function type (from Pure/codegen.ML).
berghofe
parents:
24286
diff
changeset
|
617 |
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
|
618 |
(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
|
619 |
in |
7753e0d81b7a
Added test data generator for function type (from Pure/codegen.ML).
berghofe
parents:
24286
diff
changeset
|
620 |
(fn x => |
7753e0d81b7a
Added test data generator for function type (from Pure/codegen.ML).
berghofe
parents:
24286
diff
changeset
|
621 |
case AList.lookup op = (!tab) x of |
7753e0d81b7a
Added test data generator for function type (from Pure/codegen.ML).
berghofe
parents:
24286
diff
changeset
|
622 |
NONE => |
7753e0d81b7a
Added test data generator for function type (from Pure/codegen.ML).
berghofe
parents:
24286
diff
changeset
|
623 |
let val p as (y, _) = bG i |
7753e0d81b7a
Added test data generator for function type (from Pure/codegen.ML).
berghofe
parents:
24286
diff
changeset
|
624 |
in (tab := (x, p) :: !tab; y) end |
7753e0d81b7a
Added test data generator for function type (from Pure/codegen.ML).
berghofe
parents:
24286
diff
changeset
|
625 |
| SOME (y, _) => y, |
28711 | 626 |
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
|
627 |
end; |
7753e0d81b7a
Added test data generator for function type (from Pure/codegen.ML).
berghofe
parents:
24286
diff
changeset
|
628 |
*} |
7753e0d81b7a
Added test data generator for function type (from Pure/codegen.ML).
berghofe
parents:
24286
diff
changeset
|
629 |
|
21870 | 630 |
code_const "op \<circ>" |
631 |
(SML infixl 5 "o") |
|
632 |
(Haskell infixr 9 ".") |
|
633 |
||
21906 | 634 |
code_const "id" |
635 |
(Haskell "id") |
|
636 |
||
2912 | 637 |
end |