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