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