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