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