src/HOL/Fun.thy
 author haftmann Fri Jan 21 09:44:12 2011 +0100 (2011-01-21) changeset 41657 89451110ba8e parent 41505 6d19301074cf child 42238 d53dccb38dd1 permissions -rw-r--r--
moved theorem
 clasohm@1475 ` 1` ```(* Title: HOL/Fun.thy ``` clasohm@1475 ` 2` ``` Author: Tobias Nipkow, Cambridge University Computer Laboratory ``` clasohm@923 ` 3` ``` Copyright 1994 University of Cambridge ``` huffman@18154 ` 4` ```*) ``` clasohm@923 ` 5` huffman@18154 ` 6` ```header {* Notions about functions *} ``` clasohm@923 ` 7` paulson@15510 ` 8` ```theory Fun ``` haftmann@32139 ` 9` ```imports Complete_Lattice ``` haftmann@41505 ` 10` ```uses ("Tools/enriched_type.ML") ``` nipkow@15131 ` 11` ```begin ``` nipkow@2912 ` 12` haftmann@26147 ` 13` ```text{*As a simplification rule, it replaces all function equalities by ``` haftmann@26147 ` 14` ``` first-order equalities.*} ``` nipkow@39302 ` 15` ```lemma fun_eq_iff: "f = g \ (\x. f x = g x)" ``` haftmann@26147 ` 16` ```apply (rule iffI) ``` haftmann@26147 ` 17` ```apply (simp (no_asm_simp)) ``` haftmann@26147 ` 18` ```apply (rule ext) ``` haftmann@26147 ` 19` ```apply (simp (no_asm_simp)) ``` haftmann@26147 ` 20` ```done ``` oheimb@5305 ` 21` haftmann@26147 ` 22` ```lemma apply_inverse: ``` haftmann@26357 ` 23` ``` "f x = u \ (\x. P x \ g (f x) = x) \ P x \ x = g u" ``` haftmann@26147 ` 24` ``` by auto ``` nipkow@2912 ` 25` wenzelm@12258 ` 26` haftmann@26147 ` 27` ```subsection {* The Identity Function @{text id} *} ``` paulson@6171 ` 28` haftmann@22744 ` 29` ```definition ``` haftmann@22744 ` 30` ``` id :: "'a \ 'a" ``` haftmann@22744 ` 31` ```where ``` haftmann@22744 ` 32` ``` "id = (\x. x)" ``` nipkow@13910 ` 33` haftmann@26147 ` 34` ```lemma id_apply [simp]: "id x = x" ``` haftmann@26147 ` 35` ``` by (simp add: id_def) ``` haftmann@26147 ` 36` haftmann@26147 ` 37` ```lemma image_ident [simp]: "(%x. x) ` Y = Y" ``` haftmann@26147 ` 38` ```by blast ``` haftmann@26147 ` 39` haftmann@26147 ` 40` ```lemma image_id [simp]: "id ` Y = Y" ``` haftmann@26147 ` 41` ```by (simp add: id_def) ``` haftmann@26147 ` 42` haftmann@26147 ` 43` ```lemma vimage_ident [simp]: "(%x. x) -` Y = Y" ``` haftmann@26147 ` 44` ```by blast ``` haftmann@26147 ` 45` haftmann@26147 ` 46` ```lemma vimage_id [simp]: "id -` A = A" ``` haftmann@26147 ` 47` ```by (simp add: id_def) ``` haftmann@26147 ` 48` haftmann@26147 ` 49` haftmann@26147 ` 50` ```subsection {* The Composition Operator @{text "f \ g"} *} ``` haftmann@26147 ` 51` haftmann@22744 ` 52` ```definition ``` haftmann@22744 ` 53` ``` comp :: "('b \ 'c) \ ('a \ 'b) \ 'a \ 'c" (infixl "o" 55) ``` haftmann@22744 ` 54` ```where ``` haftmann@22744 ` 55` ``` "f o g = (\x. f (g x))" ``` oheimb@11123 ` 56` wenzelm@21210 ` 57` ```notation (xsymbols) ``` wenzelm@19656 ` 58` ``` comp (infixl "\" 55) ``` wenzelm@19656 ` 59` wenzelm@21210 ` 60` ```notation (HTML output) ``` wenzelm@19656 ` 61` ``` comp (infixl "\" 55) ``` wenzelm@19656 ` 62` paulson@13585 ` 63` ```text{*compatibility*} ``` paulson@13585 ` 64` ```lemmas o_def = comp_def ``` nipkow@2912 ` 65` paulson@13585 ` 66` ```lemma o_apply [simp]: "(f o g) x = f (g x)" ``` paulson@13585 ` 67` ```by (simp add: comp_def) ``` paulson@13585 ` 68` paulson@13585 ` 69` ```lemma o_assoc: "f o (g o h) = f o g o h" ``` paulson@13585 ` 70` ```by (simp add: comp_def) ``` paulson@13585 ` 71` paulson@13585 ` 72` ```lemma id_o [simp]: "id o g = g" ``` paulson@13585 ` 73` ```by (simp add: comp_def) ``` paulson@13585 ` 74` paulson@13585 ` 75` ```lemma o_id [simp]: "f o id = f" ``` paulson@13585 ` 76` ```by (simp add: comp_def) ``` paulson@13585 ` 77` haftmann@34150 ` 78` ```lemma o_eq_dest: ``` haftmann@34150 ` 79` ``` "a o b = c o d \ a (b v) = c (d v)" ``` haftmann@34150 ` 80` ``` by (simp only: o_def) (fact fun_cong) ``` haftmann@34150 ` 81` haftmann@34150 ` 82` ```lemma o_eq_elim: ``` haftmann@34150 ` 83` ``` "a o b = c o d \ ((\v. a (b v) = c (d v)) \ R) \ R" ``` haftmann@34150 ` 84` ``` by (erule meta_mp) (fact o_eq_dest) ``` haftmann@34150 ` 85` paulson@13585 ` 86` ```lemma image_compose: "(f o g) ` r = f`(g`r)" ``` paulson@13585 ` 87` ```by (simp add: comp_def, blast) ``` paulson@13585 ` 88` paulson@33044 ` 89` ```lemma vimage_compose: "(g \ f) -` x = f -` (g -` x)" ``` paulson@33044 ` 90` ``` by auto ``` paulson@33044 ` 91` paulson@13585 ` 92` ```lemma UN_o: "UNION A (g o f) = UNION (f`A) g" ``` paulson@13585 ` 93` ```by (unfold comp_def, blast) ``` paulson@13585 ` 94` paulson@13585 ` 95` haftmann@26588 ` 96` ```subsection {* The Forward Composition Operator @{text fcomp} *} ``` haftmann@26357 ` 97` haftmann@26357 ` 98` ```definition ``` haftmann@37751 ` 99` ``` fcomp :: "('a \ 'b) \ ('b \ 'c) \ 'a \ 'c" (infixl "\>" 60) ``` haftmann@26357 ` 100` ```where ``` haftmann@37751 ` 101` ``` "f \> g = (\x. g (f x))" ``` haftmann@26357 ` 102` haftmann@37751 ` 103` ```lemma fcomp_apply [simp]: "(f \> g) x = g (f x)" ``` haftmann@26357 ` 104` ``` by (simp add: fcomp_def) ``` haftmann@26357 ` 105` haftmann@37751 ` 106` ```lemma fcomp_assoc: "(f \> g) \> h = f \> (g \> h)" ``` haftmann@26357 ` 107` ``` by (simp add: fcomp_def) ``` haftmann@26357 ` 108` haftmann@37751 ` 109` ```lemma id_fcomp [simp]: "id \> g = g" ``` haftmann@26357 ` 110` ``` by (simp add: fcomp_def) ``` haftmann@26357 ` 111` haftmann@37751 ` 112` ```lemma fcomp_id [simp]: "f \> id = f" ``` haftmann@26357 ` 113` ``` by (simp add: fcomp_def) ``` haftmann@26357 ` 114` haftmann@31202 ` 115` ```code_const fcomp ``` haftmann@31202 ` 116` ``` (Eval infixl 1 "#>") ``` haftmann@31202 ` 117` haftmann@37751 ` 118` ```no_notation fcomp (infixl "\>" 60) ``` haftmann@26588 ` 119` haftmann@26357 ` 120` haftmann@40602 ` 121` ```subsection {* Mapping functions *} ``` haftmann@40602 ` 122` haftmann@40602 ` 123` ```definition map_fun :: "('c \ 'a) \ ('b \ 'd) \ ('a \ 'b) \ 'c \ 'd" where ``` haftmann@40602 ` 124` ``` "map_fun f g h = g \ h \ f" ``` haftmann@40602 ` 125` haftmann@40602 ` 126` ```lemma map_fun_apply [simp]: ``` haftmann@40602 ` 127` ``` "map_fun f g h x = g (h (f x))" ``` haftmann@40602 ` 128` ``` by (simp add: map_fun_def) ``` haftmann@40602 ` 129` haftmann@40602 ` 130` hoelzl@40702 ` 131` ```subsection {* Injectivity and Bijectivity *} ``` hoelzl@39076 ` 132` hoelzl@39076 ` 133` ```definition inj_on :: "('a \ 'b) \ 'a set \ bool" where -- "injective" ``` hoelzl@39076 ` 134` ``` "inj_on f A \ (\x\A. \y\A. f x = f y \ x = y)" ``` haftmann@26147 ` 135` hoelzl@39076 ` 136` ```definition bij_betw :: "('a \ 'b) \ 'a set \ 'b set \ bool" where -- "bijective" ``` hoelzl@39076 ` 137` ``` "bij_betw f A B \ inj_on f A \ f ` A = B" ``` haftmann@26147 ` 138` hoelzl@40702 ` 139` ```text{*A common special case: functions injective, surjective or bijective over ``` hoelzl@40702 ` 140` ```the entire domain type.*} ``` haftmann@26147 ` 141` haftmann@26147 ` 142` ```abbreviation ``` hoelzl@39076 ` 143` ``` "inj f \ inj_on f UNIV" ``` haftmann@26147 ` 144` hoelzl@40702 ` 145` ```abbreviation surj :: "('a \ 'b) \ bool" where -- "surjective" ``` hoelzl@40702 ` 146` ``` "surj f \ (range f = UNIV)" ``` paulson@13585 ` 147` hoelzl@39076 ` 148` ```abbreviation ``` hoelzl@39076 ` 149` ``` "bij f \ bij_betw f UNIV UNIV" ``` haftmann@26147 ` 150` haftmann@26147 ` 151` ```lemma injI: ``` haftmann@26147 ` 152` ``` assumes "\x y. f x = f y \ x = y" ``` haftmann@26147 ` 153` ``` shows "inj f" ``` haftmann@26147 ` 154` ``` using assms unfolding inj_on_def by auto ``` paulson@13585 ` 155` berghofe@13637 ` 156` ```theorem range_ex1_eq: "inj f \ b : range f = (EX! x. b = f x)" ``` berghofe@13637 ` 157` ``` by (unfold inj_on_def, blast) ``` berghofe@13637 ` 158` paulson@13585 ` 159` ```lemma injD: "[| inj(f); f(x) = f(y) |] ==> x=y" ``` paulson@13585 ` 160` ```by (simp add: inj_on_def) ``` paulson@13585 ` 161` nipkow@32988 ` 162` ```lemma inj_on_eq_iff: "inj_on f A ==> x:A ==> y:A ==> (f(x) = f(y)) = (x=y)" ``` paulson@13585 ` 163` ```by (force simp add: inj_on_def) ``` paulson@13585 ` 164` hoelzl@40703 ` 165` ```lemma inj_on_cong: ``` hoelzl@40703 ` 166` ``` "(\ a. a : A \ f a = g a) \ inj_on f A = inj_on g A" ``` hoelzl@40703 ` 167` ```unfolding inj_on_def by auto ``` hoelzl@40703 ` 168` hoelzl@40703 ` 169` ```lemma inj_on_strict_subset: ``` hoelzl@40703 ` 170` ``` "\ inj_on f B; A < B \ \ f`A < f`B" ``` hoelzl@40703 ` 171` ```unfolding inj_on_def unfolding image_def by blast ``` hoelzl@40703 ` 172` haftmann@38620 ` 173` ```lemma inj_comp: ``` haftmann@38620 ` 174` ``` "inj f \ inj g \ inj (f \ g)" ``` haftmann@38620 ` 175` ``` by (simp add: inj_on_def) ``` haftmann@38620 ` 176` haftmann@38620 ` 177` ```lemma inj_fun: "inj f \ inj (\x y. f x)" ``` nipkow@39302 ` 178` ``` by (simp add: inj_on_def fun_eq_iff) ``` haftmann@38620 ` 179` nipkow@32988 ` 180` ```lemma inj_eq: "inj f ==> (f(x) = f(y)) = (x=y)" ``` nipkow@32988 ` 181` ```by (simp add: inj_on_eq_iff) ``` nipkow@32988 ` 182` haftmann@26147 ` 183` ```lemma inj_on_id[simp]: "inj_on id A" ``` hoelzl@39076 ` 184` ``` by (simp add: inj_on_def) ``` paulson@13585 ` 185` haftmann@26147 ` 186` ```lemma inj_on_id2[simp]: "inj_on (%x. x) A" ``` hoelzl@39076 ` 187` ```by (simp add: inj_on_def) ``` haftmann@26147 ` 188` hoelzl@40703 ` 189` ```lemma inj_on_Int: "\inj_on f A; inj_on f B\ \ inj_on f (A \ B)" ``` hoelzl@40703 ` 190` ```unfolding inj_on_def by blast ``` hoelzl@40703 ` 191` hoelzl@40703 ` 192` ```lemma inj_on_INTER: ``` hoelzl@40703 ` 193` ``` "\I \ {}; \ i. i \ I \ inj_on f (A i)\ \ inj_on f (\ i \ I. A i)" ``` hoelzl@40703 ` 194` ```unfolding inj_on_def by blast ``` hoelzl@40703 ` 195` hoelzl@40703 ` 196` ```lemma inj_on_Inter: ``` hoelzl@40703 ` 197` ``` "\S \ {}; \ A. A \ S \ inj_on f A\ \ inj_on f (Inter S)" ``` hoelzl@40703 ` 198` ```unfolding inj_on_def by blast ``` hoelzl@40703 ` 199` hoelzl@40703 ` 200` ```lemma inj_on_UNION_chain: ``` hoelzl@40703 ` 201` ``` assumes CH: "\ i j. \i \ I; j \ I\ \ A i \ A j \ A j \ A i" and ``` hoelzl@40703 ` 202` ``` INJ: "\ i. i \ I \ inj_on f (A i)" ``` hoelzl@40703 ` 203` ``` shows "inj_on f (\ i \ I. A i)" ``` hoelzl@40703 ` 204` ```proof(unfold inj_on_def UNION_def, auto) ``` hoelzl@40703 ` 205` ``` fix i j x y ``` hoelzl@40703 ` 206` ``` assume *: "i \ I" "j \ I" and **: "x \ A i" "y \ A j" ``` hoelzl@40703 ` 207` ``` and ***: "f x = f y" ``` hoelzl@40703 ` 208` ``` show "x = y" ``` hoelzl@40703 ` 209` ``` proof- ``` hoelzl@40703 ` 210` ``` {assume "A i \ A j" ``` hoelzl@40703 ` 211` ``` with ** have "x \ A j" by auto ``` hoelzl@40703 ` 212` ``` with INJ * ** *** have ?thesis ``` hoelzl@40703 ` 213` ``` by(auto simp add: inj_on_def) ``` hoelzl@40703 ` 214` ``` } ``` hoelzl@40703 ` 215` ``` moreover ``` hoelzl@40703 ` 216` ``` {assume "A j \ A i" ``` hoelzl@40703 ` 217` ``` with ** have "y \ A i" by auto ``` hoelzl@40703 ` 218` ``` with INJ * ** *** have ?thesis ``` hoelzl@40703 ` 219` ``` by(auto simp add: inj_on_def) ``` hoelzl@40703 ` 220` ``` } ``` hoelzl@40703 ` 221` ``` ultimately show ?thesis using CH * by blast ``` hoelzl@40703 ` 222` ``` qed ``` hoelzl@40703 ` 223` ```qed ``` hoelzl@40703 ` 224` hoelzl@40702 ` 225` ```lemma surj_id: "surj id" ``` hoelzl@40702 ` 226` ```by simp ``` haftmann@26147 ` 227` hoelzl@39101 ` 228` ```lemma bij_id[simp]: "bij id" ``` hoelzl@39076 ` 229` ```by (simp add: bij_betw_def) ``` paulson@13585 ` 230` paulson@13585 ` 231` ```lemma inj_onI: ``` paulson@13585 ` 232` ``` "(!! x y. [| x:A; y:A; f(x) = f(y) |] ==> x=y) ==> inj_on f A" ``` paulson@13585 ` 233` ```by (simp add: inj_on_def) ``` paulson@13585 ` 234` paulson@13585 ` 235` ```lemma inj_on_inverseI: "(!!x. x:A ==> g(f(x)) = x) ==> inj_on f A" ``` paulson@13585 ` 236` ```by (auto dest: arg_cong [of concl: g] simp add: inj_on_def) ``` paulson@13585 ` 237` paulson@13585 ` 238` ```lemma inj_onD: "[| inj_on f A; f(x)=f(y); x:A; y:A |] ==> x=y" ``` paulson@13585 ` 239` ```by (unfold inj_on_def, blast) ``` paulson@13585 ` 240` paulson@13585 ` 241` ```lemma inj_on_iff: "[| inj_on f A; x:A; y:A |] ==> (f(x)=f(y)) = (x=y)" ``` paulson@13585 ` 242` ```by (blast dest!: inj_onD) ``` paulson@13585 ` 243` paulson@13585 ` 244` ```lemma comp_inj_on: ``` paulson@13585 ` 245` ``` "[| inj_on f A; inj_on g (f`A) |] ==> inj_on (g o f) A" ``` paulson@13585 ` 246` ```by (simp add: comp_def inj_on_def) ``` paulson@13585 ` 247` nipkow@15303 ` 248` ```lemma inj_on_imageI: "inj_on (g o f) A \ inj_on g (f ` A)" ``` nipkow@15303 ` 249` ```apply(simp add:inj_on_def image_def) ``` nipkow@15303 ` 250` ```apply blast ``` nipkow@15303 ` 251` ```done ``` nipkow@15303 ` 252` nipkow@15439 ` 253` ```lemma inj_on_image_iff: "\ ALL x:A. ALL y:A. (g(f x) = g(f y)) = (g x = g y); ``` nipkow@15439 ` 254` ``` inj_on f A \ \ inj_on g (f ` A) = inj_on g A" ``` nipkow@15439 ` 255` ```apply(unfold inj_on_def) ``` nipkow@15439 ` 256` ```apply blast ``` nipkow@15439 ` 257` ```done ``` nipkow@15439 ` 258` paulson@13585 ` 259` ```lemma inj_on_contraD: "[| inj_on f A; ~x=y; x:A; y:A |] ==> ~ f(x)=f(y)" ``` paulson@13585 ` 260` ```by (unfold inj_on_def, blast) ``` wenzelm@12258 ` 261` paulson@13585 ` 262` ```lemma inj_singleton: "inj (%s. {s})" ``` paulson@13585 ` 263` ```by (simp add: inj_on_def) ``` paulson@13585 ` 264` nipkow@15111 ` 265` ```lemma inj_on_empty[iff]: "inj_on f {}" ``` nipkow@15111 ` 266` ```by(simp add: inj_on_def) ``` nipkow@15111 ` 267` nipkow@15303 ` 268` ```lemma subset_inj_on: "[| inj_on f B; A <= B |] ==> inj_on f A" ``` paulson@13585 ` 269` ```by (unfold inj_on_def, blast) ``` paulson@13585 ` 270` nipkow@15111 ` 271` ```lemma inj_on_Un: ``` nipkow@15111 ` 272` ``` "inj_on f (A Un B) = ``` nipkow@15111 ` 273` ``` (inj_on f A & inj_on f B & f`(A-B) Int f`(B-A) = {})" ``` nipkow@15111 ` 274` ```apply(unfold inj_on_def) ``` nipkow@15111 ` 275` ```apply (blast intro:sym) ``` nipkow@15111 ` 276` ```done ``` nipkow@15111 ` 277` nipkow@15111 ` 278` ```lemma inj_on_insert[iff]: ``` nipkow@15111 ` 279` ``` "inj_on f (insert a A) = (inj_on f A & f a ~: f`(A-{a}))" ``` nipkow@15111 ` 280` ```apply(unfold inj_on_def) ``` nipkow@15111 ` 281` ```apply (blast intro:sym) ``` nipkow@15111 ` 282` ```done ``` nipkow@15111 ` 283` nipkow@15111 ` 284` ```lemma inj_on_diff: "inj_on f A ==> inj_on f (A-B)" ``` nipkow@15111 ` 285` ```apply(unfold inj_on_def) ``` nipkow@15111 ` 286` ```apply (blast) ``` nipkow@15111 ` 287` ```done ``` nipkow@15111 ` 288` hoelzl@40703 ` 289` ```lemma comp_inj_on_iff: ``` hoelzl@40703 ` 290` ``` "inj_on f A \ inj_on f' (f ` A) \ inj_on (f' o f) A" ``` hoelzl@40703 ` 291` ```by(auto simp add: comp_inj_on inj_on_def) ``` hoelzl@40703 ` 292` hoelzl@40703 ` 293` ```lemma inj_on_imageI2: ``` hoelzl@40703 ` 294` ``` "inj_on (f' o f) A \ inj_on f A" ``` hoelzl@40703 ` 295` ```by(auto simp add: comp_inj_on inj_on_def) ``` hoelzl@40703 ` 296` hoelzl@40702 ` 297` ```lemma surj_def: "surj f \ (\y. \x. y = f x)" ``` hoelzl@40702 ` 298` ``` by auto ``` hoelzl@39076 ` 299` hoelzl@40702 ` 300` ```lemma surjI: assumes *: "\ x. g (f x) = x" shows "surj g" ``` hoelzl@40702 ` 301` ``` using *[symmetric] by auto ``` paulson@13585 ` 302` hoelzl@39076 ` 303` ```lemma surjD: "surj f \ \x. y = f x" ``` hoelzl@39076 ` 304` ``` by (simp add: surj_def) ``` paulson@13585 ` 305` hoelzl@39076 ` 306` ```lemma surjE: "surj f \ (\x. y = f x \ C) \ C" ``` hoelzl@39076 ` 307` ``` by (simp add: surj_def, blast) ``` paulson@13585 ` 308` paulson@13585 ` 309` ```lemma comp_surj: "[| surj f; surj g |] ==> surj (g o f)" ``` paulson@13585 ` 310` ```apply (simp add: comp_def surj_def, clarify) ``` paulson@13585 ` 311` ```apply (drule_tac x = y in spec, clarify) ``` paulson@13585 ` 312` ```apply (drule_tac x = x in spec, blast) ``` paulson@13585 ` 313` ```done ``` paulson@13585 ` 314` hoelzl@39074 ` 315` ```lemma bij_betw_imp_surj: "bij_betw f A UNIV \ surj f" ``` hoelzl@40702 ` 316` ``` unfolding bij_betw_def by auto ``` hoelzl@39074 ` 317` hoelzl@40703 ` 318` ```lemma bij_betw_empty1: ``` hoelzl@40703 ` 319` ``` assumes "bij_betw f {} A" ``` hoelzl@40703 ` 320` ``` shows "A = {}" ``` hoelzl@40703 ` 321` ```using assms unfolding bij_betw_def by blast ``` hoelzl@40703 ` 322` hoelzl@40703 ` 323` ```lemma bij_betw_empty2: ``` hoelzl@40703 ` 324` ``` assumes "bij_betw f A {}" ``` hoelzl@40703 ` 325` ``` shows "A = {}" ``` hoelzl@40703 ` 326` ```using assms unfolding bij_betw_def by blast ``` hoelzl@40703 ` 327` hoelzl@40703 ` 328` ```lemma inj_on_imp_bij_betw: ``` hoelzl@40703 ` 329` ``` "inj_on f A \ bij_betw f A (f ` A)" ``` hoelzl@40703 ` 330` ```unfolding bij_betw_def by simp ``` hoelzl@40703 ` 331` hoelzl@39076 ` 332` ```lemma bij_def: "bij f \ inj f \ surj f" ``` hoelzl@40702 ` 333` ``` unfolding bij_betw_def .. ``` hoelzl@39074 ` 334` paulson@13585 ` 335` ```lemma bijI: "[| inj f; surj f |] ==> bij f" ``` paulson@13585 ` 336` ```by (simp add: bij_def) ``` paulson@13585 ` 337` paulson@13585 ` 338` ```lemma bij_is_inj: "bij f ==> inj f" ``` paulson@13585 ` 339` ```by (simp add: bij_def) ``` paulson@13585 ` 340` paulson@13585 ` 341` ```lemma bij_is_surj: "bij f ==> surj f" ``` paulson@13585 ` 342` ```by (simp add: bij_def) ``` paulson@13585 ` 343` nipkow@26105 ` 344` ```lemma bij_betw_imp_inj_on: "bij_betw f A B \ inj_on f A" ``` nipkow@26105 ` 345` ```by (simp add: bij_betw_def) ``` nipkow@26105 ` 346` nipkow@31438 ` 347` ```lemma bij_betw_trans: ``` nipkow@31438 ` 348` ``` "bij_betw f A B \ bij_betw g B C \ bij_betw (g o f) A C" ``` nipkow@31438 ` 349` ```by(auto simp add:bij_betw_def comp_inj_on) ``` nipkow@31438 ` 350` hoelzl@40702 ` 351` ```lemma bij_comp: "bij f \ bij g \ bij (g o f)" ``` hoelzl@40702 ` 352` ``` by (rule bij_betw_trans) ``` hoelzl@40702 ` 353` hoelzl@40703 ` 354` ```lemma bij_betw_comp_iff: ``` hoelzl@40703 ` 355` ``` "bij_betw f A A' \ bij_betw f' A' A'' \ bij_betw (f' o f) A A''" ``` hoelzl@40703 ` 356` ```by(auto simp add: bij_betw_def inj_on_def) ``` hoelzl@40703 ` 357` hoelzl@40703 ` 358` ```lemma bij_betw_comp_iff2: ``` hoelzl@40703 ` 359` ``` assumes BIJ: "bij_betw f' A' A''" and IM: "f ` A \ A'" ``` hoelzl@40703 ` 360` ``` shows "bij_betw f A A' \ bij_betw (f' o f) A A''" ``` hoelzl@40703 ` 361` ```using assms ``` hoelzl@40703 ` 362` ```proof(auto simp add: bij_betw_comp_iff) ``` hoelzl@40703 ` 363` ``` assume *: "bij_betw (f' \ f) A A''" ``` hoelzl@40703 ` 364` ``` thus "bij_betw f A A'" ``` hoelzl@40703 ` 365` ``` using IM ``` hoelzl@40703 ` 366` ``` proof(auto simp add: bij_betw_def) ``` hoelzl@40703 ` 367` ``` assume "inj_on (f' \ f) A" ``` hoelzl@40703 ` 368` ``` thus "inj_on f A" using inj_on_imageI2 by blast ``` hoelzl@40703 ` 369` ``` next ``` hoelzl@40703 ` 370` ``` fix a' assume **: "a' \ A'" ``` hoelzl@40703 ` 371` ``` hence "f' a' \ A''" using BIJ unfolding bij_betw_def by auto ``` hoelzl@40703 ` 372` ``` then obtain a where 1: "a \ A \ f'(f a) = f' a'" using * ``` hoelzl@40703 ` 373` ``` unfolding bij_betw_def by force ``` hoelzl@40703 ` 374` ``` hence "f a \ A'" using IM by auto ``` hoelzl@40703 ` 375` ``` hence "f a = a'" using BIJ ** 1 unfolding bij_betw_def inj_on_def by auto ``` hoelzl@40703 ` 376` ``` thus "a' \ f ` A" using 1 by auto ``` hoelzl@40703 ` 377` ``` qed ``` hoelzl@40703 ` 378` ```qed ``` hoelzl@40703 ` 379` nipkow@26105 ` 380` ```lemma bij_betw_inv: assumes "bij_betw f A B" shows "EX g. bij_betw g B A" ``` nipkow@26105 ` 381` ```proof - ``` nipkow@26105 ` 382` ``` have i: "inj_on f A" and s: "f ` A = B" ``` nipkow@26105 ` 383` ``` using assms by(auto simp:bij_betw_def) ``` nipkow@26105 ` 384` ``` let ?P = "%b a. a:A \ f a = b" let ?g = "%b. The (?P b)" ``` nipkow@26105 ` 385` ``` { fix a b assume P: "?P b a" ``` nipkow@26105 ` 386` ``` hence ex1: "\a. ?P b a" using s unfolding image_def by blast ``` nipkow@26105 ` 387` ``` hence uex1: "\!a. ?P b a" by(blast dest:inj_onD[OF i]) ``` nipkow@26105 ` 388` ``` hence " ?g b = a" using the1_equality[OF uex1, OF P] P by simp ``` nipkow@26105 ` 389` ``` } note g = this ``` nipkow@26105 ` 390` ``` have "inj_on ?g B" ``` nipkow@26105 ` 391` ``` proof(rule inj_onI) ``` nipkow@26105 ` 392` ``` fix x y assume "x:B" "y:B" "?g x = ?g y" ``` nipkow@26105 ` 393` ``` from s `x:B` obtain a1 where a1: "?P x a1" unfolding image_def by blast ``` nipkow@26105 ` 394` ``` from s `y:B` obtain a2 where a2: "?P y a2" unfolding image_def by blast ``` nipkow@26105 ` 395` ``` from g[OF a1] a1 g[OF a2] a2 `?g x = ?g y` show "x=y" by simp ``` nipkow@26105 ` 396` ``` qed ``` nipkow@26105 ` 397` ``` moreover have "?g ` B = A" ``` nipkow@26105 ` 398` ``` proof(auto simp:image_def) ``` nipkow@26105 ` 399` ``` fix b assume "b:B" ``` nipkow@26105 ` 400` ``` with s obtain a where P: "?P b a" unfolding image_def by blast ``` nipkow@26105 ` 401` ``` thus "?g b \ A" using g[OF P] by auto ``` nipkow@26105 ` 402` ``` next ``` nipkow@26105 ` 403` ``` fix a assume "a:A" ``` nipkow@26105 ` 404` ``` then obtain b where P: "?P b a" using s unfolding image_def by blast ``` nipkow@26105 ` 405` ``` then have "b:B" using s unfolding image_def by blast ``` nipkow@26105 ` 406` ``` with g[OF P] show "\b\B. a = ?g b" by blast ``` nipkow@26105 ` 407` ``` qed ``` nipkow@26105 ` 408` ``` ultimately show ?thesis by(auto simp:bij_betw_def) ``` nipkow@26105 ` 409` ```qed ``` nipkow@26105 ` 410` hoelzl@40703 ` 411` ```lemma bij_betw_cong: ``` hoelzl@40703 ` 412` ``` "(\ a. a \ A \ f a = g a) \ bij_betw f A A' = bij_betw g A A'" ``` hoelzl@40703 ` 413` ```unfolding bij_betw_def inj_on_def by force ``` hoelzl@40703 ` 414` hoelzl@40703 ` 415` ```lemma bij_betw_id[intro, simp]: ``` hoelzl@40703 ` 416` ``` "bij_betw id A A" ``` hoelzl@40703 ` 417` ```unfolding bij_betw_def id_def by auto ``` hoelzl@40703 ` 418` hoelzl@40703 ` 419` ```lemma bij_betw_id_iff: ``` hoelzl@40703 ` 420` ``` "bij_betw id A B \ A = B" ``` hoelzl@40703 ` 421` ```by(auto simp add: bij_betw_def) ``` hoelzl@40703 ` 422` hoelzl@39075 ` 423` ```lemma bij_betw_combine: ``` hoelzl@39075 ` 424` ``` assumes "bij_betw f A B" "bij_betw f C D" "B \ D = {}" ``` hoelzl@39075 ` 425` ``` shows "bij_betw f (A \ C) (B \ D)" ``` hoelzl@39075 ` 426` ``` using assms unfolding bij_betw_def inj_on_Un image_Un by auto ``` hoelzl@39075 ` 427` hoelzl@40703 ` 428` ```lemma bij_betw_UNION_chain: ``` hoelzl@40703 ` 429` ``` assumes CH: "\ i j. \i \ I; j \ I\ \ A i \ A j \ A j \ A i" and ``` hoelzl@40703 ` 430` ``` BIJ: "\ i. i \ I \ bij_betw f (A i) (A' i)" ``` hoelzl@40703 ` 431` ``` shows "bij_betw f (\ i \ I. A i) (\ i \ I. A' i)" ``` hoelzl@40703 ` 432` ```proof(unfold bij_betw_def, auto simp add: image_def) ``` hoelzl@40703 ` 433` ``` have "\ i. i \ I \ inj_on f (A i)" ``` hoelzl@40703 ` 434` ``` using BIJ bij_betw_def[of f] by auto ``` hoelzl@40703 ` 435` ``` thus "inj_on f (\ i \ I. A i)" ``` hoelzl@40703 ` 436` ``` using CH inj_on_UNION_chain[of I A f] by auto ``` hoelzl@40703 ` 437` ```next ``` hoelzl@40703 ` 438` ``` fix i x ``` hoelzl@40703 ` 439` ``` assume *: "i \ I" "x \ A i" ``` hoelzl@40703 ` 440` ``` hence "f x \ A' i" using BIJ bij_betw_def[of f] by auto ``` hoelzl@40703 ` 441` ``` thus "\j \ I. f x \ A' j" using * by blast ``` hoelzl@40703 ` 442` ```next ``` hoelzl@40703 ` 443` ``` fix i x' ``` hoelzl@40703 ` 444` ``` assume *: "i \ I" "x' \ A' i" ``` hoelzl@40703 ` 445` ``` hence "\x \ A i. x' = f x" using BIJ bij_betw_def[of f] by blast ``` hoelzl@40703 ` 446` ``` thus "\j \ I. \x \ A j. x' = f x" ``` hoelzl@40703 ` 447` ``` using * by blast ``` hoelzl@40703 ` 448` ```qed ``` hoelzl@40703 ` 449` hoelzl@40703 ` 450` ```lemma bij_betw_Disj_Un: ``` hoelzl@40703 ` 451` ``` assumes DISJ: "A \ B = {}" and DISJ': "A' \ B' = {}" and ``` hoelzl@40703 ` 452` ``` B1: "bij_betw f A A'" and B2: "bij_betw f B B'" ``` hoelzl@40703 ` 453` ``` shows "bij_betw f (A \ B) (A' \ B')" ``` hoelzl@40703 ` 454` ```proof- ``` hoelzl@40703 ` 455` ``` have 1: "inj_on f A \ inj_on f B" ``` hoelzl@40703 ` 456` ``` using B1 B2 by (auto simp add: bij_betw_def) ``` hoelzl@40703 ` 457` ``` have 2: "f`A = A' \ f`B = B'" ``` hoelzl@40703 ` 458` ``` using B1 B2 by (auto simp add: bij_betw_def) ``` hoelzl@40703 ` 459` ``` hence "f`(A - B) \ f`(B - A) = {}" ``` hoelzl@40703 ` 460` ``` using DISJ DISJ' by blast ``` hoelzl@40703 ` 461` ``` hence "inj_on f (A \ B)" ``` hoelzl@40703 ` 462` ``` using 1 by (auto simp add: inj_on_Un) ``` hoelzl@40703 ` 463` ``` (* *) ``` hoelzl@40703 ` 464` ``` moreover ``` hoelzl@40703 ` 465` ``` have "f`(A \ B) = A' \ B'" ``` hoelzl@40703 ` 466` ``` using 2 by auto ``` hoelzl@40703 ` 467` ``` ultimately show ?thesis ``` hoelzl@40703 ` 468` ``` unfolding bij_betw_def by auto ``` hoelzl@40703 ` 469` ```qed ``` hoelzl@40703 ` 470` hoelzl@40703 ` 471` ```lemma bij_betw_subset: ``` hoelzl@40703 ` 472` ``` assumes BIJ: "bij_betw f A A'" and ``` hoelzl@40703 ` 473` ``` SUB: "B \ A" and IM: "f ` B = B'" ``` hoelzl@40703 ` 474` ``` shows "bij_betw f B B'" ``` hoelzl@40703 ` 475` ```using assms ``` hoelzl@40703 ` 476` ```by(unfold bij_betw_def inj_on_def, auto simp add: inj_on_def) ``` hoelzl@40703 ` 477` paulson@13585 ` 478` ```lemma surj_image_vimage_eq: "surj f ==> f ` (f -` A) = A" ``` hoelzl@40702 ` 479` ```by simp ``` paulson@13585 ` 480` paulson@13585 ` 481` ```lemma inj_vimage_image_eq: "inj f ==> f -` (f ` A) = A" ``` paulson@13585 ` 482` ```by (simp add: inj_on_def, blast) ``` paulson@13585 ` 483` paulson@13585 ` 484` ```lemma vimage_subsetD: "surj f ==> f -` B <= A ==> B <= f ` A" ``` hoelzl@40702 ` 485` ```by (blast intro: sym) ``` paulson@13585 ` 486` paulson@13585 ` 487` ```lemma vimage_subsetI: "inj f ==> B <= f ` A ==> f -` B <= A" ``` paulson@13585 ` 488` ```by (unfold inj_on_def, blast) ``` paulson@13585 ` 489` paulson@13585 ` 490` ```lemma vimage_subset_eq: "bij f ==> (f -` B <= A) = (B <= f ` A)" ``` paulson@13585 ` 491` ```apply (unfold bij_def) ``` paulson@13585 ` 492` ```apply (blast del: subsetI intro: vimage_subsetI vimage_subsetD) ``` paulson@13585 ` 493` ```done ``` paulson@13585 ` 494` nipkow@31438 ` 495` ```lemma inj_on_Un_image_eq_iff: "inj_on f (A \ B) \ f ` A = f ` B \ A = B" ``` nipkow@31438 ` 496` ```by(blast dest: inj_onD) ``` nipkow@31438 ` 497` paulson@13585 ` 498` ```lemma inj_on_image_Int: ``` paulson@13585 ` 499` ``` "[| inj_on f C; A<=C; B<=C |] ==> f`(A Int B) = f`A Int f`B" ``` paulson@13585 ` 500` ```apply (simp add: inj_on_def, blast) ``` paulson@13585 ` 501` ```done ``` paulson@13585 ` 502` paulson@13585 ` 503` ```lemma inj_on_image_set_diff: ``` paulson@13585 ` 504` ``` "[| inj_on f C; A<=C; B<=C |] ==> f`(A-B) = f`A - f`B" ``` paulson@13585 ` 505` ```apply (simp add: inj_on_def, blast) ``` paulson@13585 ` 506` ```done ``` paulson@13585 ` 507` paulson@13585 ` 508` ```lemma image_Int: "inj f ==> f`(A Int B) = f`A Int f`B" ``` paulson@13585 ` 509` ```by (simp add: inj_on_def, blast) ``` paulson@13585 ` 510` paulson@13585 ` 511` ```lemma image_set_diff: "inj f ==> f`(A-B) = f`A - f`B" ``` paulson@13585 ` 512` ```by (simp add: inj_on_def, blast) ``` paulson@13585 ` 513` paulson@13585 ` 514` ```lemma inj_image_mem_iff: "inj f ==> (f a : f`A) = (a : A)" ``` paulson@13585 ` 515` ```by (blast dest: injD) ``` paulson@13585 ` 516` paulson@13585 ` 517` ```lemma inj_image_subset_iff: "inj f ==> (f`A <= f`B) = (A<=B)" ``` paulson@13585 ` 518` ```by (simp add: inj_on_def, blast) ``` paulson@13585 ` 519` paulson@13585 ` 520` ```lemma inj_image_eq_iff: "inj f ==> (f`A = f`B) = (A = B)" ``` paulson@13585 ` 521` ```by (blast dest: injD) ``` paulson@13585 ` 522` paulson@13585 ` 523` ```(*injectivity's required. Left-to-right inclusion holds even if A is empty*) ``` paulson@13585 ` 524` ```lemma image_INT: ``` paulson@13585 ` 525` ``` "[| inj_on f C; ALL x:A. B x <= C; j:A |] ``` paulson@13585 ` 526` ``` ==> f ` (INTER A B) = (INT x:A. f ` B x)" ``` paulson@13585 ` 527` ```apply (simp add: inj_on_def, blast) ``` paulson@13585 ` 528` ```done ``` paulson@13585 ` 529` paulson@13585 ` 530` ```(*Compare with image_INT: no use of inj_on, and if f is surjective then ``` paulson@13585 ` 531` ``` it doesn't matter whether A is empty*) ``` paulson@13585 ` 532` ```lemma bij_image_INT: "bij f ==> f ` (INTER A B) = (INT x:A. f ` B x)" ``` paulson@13585 ` 533` ```apply (simp add: bij_def) ``` paulson@13585 ` 534` ```apply (simp add: inj_on_def surj_def, blast) ``` paulson@13585 ` 535` ```done ``` paulson@13585 ` 536` paulson@13585 ` 537` ```lemma surj_Compl_image_subset: "surj f ==> -(f`A) <= f`(-A)" ``` hoelzl@40702 ` 538` ```by auto ``` paulson@13585 ` 539` paulson@13585 ` 540` ```lemma inj_image_Compl_subset: "inj f ==> f`(-A) <= -(f`A)" ``` paulson@13585 ` 541` ```by (auto simp add: inj_on_def) ``` paulson@5852 ` 542` paulson@13585 ` 543` ```lemma bij_image_Compl_eq: "bij f ==> f`(-A) = -(f`A)" ``` paulson@13585 ` 544` ```apply (simp add: bij_def) ``` paulson@13585 ` 545` ```apply (rule equalityI) ``` paulson@13585 ` 546` ```apply (simp_all (no_asm_simp) add: inj_image_Compl_subset surj_Compl_image_subset) ``` paulson@13585 ` 547` ```done ``` paulson@13585 ` 548` haftmann@41657 ` 549` ```lemma inj_vimage_singleton: "inj f \ f -` {a} \ {THE x. f x = a}" ``` haftmann@41657 ` 550` ``` -- {* The inverse image of a singleton under an injective function ``` haftmann@41657 ` 551` ``` is included in a singleton. *} ``` haftmann@41657 ` 552` ``` apply (auto simp add: inj_on_def) ``` haftmann@41657 ` 553` ``` apply (blast intro: the_equality [symmetric]) ``` haftmann@41657 ` 554` ``` done ``` haftmann@41657 ` 555` hoelzl@35584 ` 556` ```lemma (in ordered_ab_group_add) inj_uminus[simp, intro]: "inj_on uminus A" ``` hoelzl@35580 ` 557` ``` by (auto intro!: inj_onI) ``` paulson@13585 ` 558` hoelzl@35584 ` 559` ```lemma (in linorder) strict_mono_imp_inj_on: "strict_mono f \ inj_on f A" ``` hoelzl@35584 ` 560` ``` by (auto intro!: inj_onI dest: strict_mono_eq) ``` hoelzl@35584 ` 561` haftmann@41657 ` 562` paulson@13585 ` 563` ```subsection{*Function Updating*} ``` paulson@13585 ` 564` haftmann@35416 ` 565` ```definition ``` haftmann@35416 ` 566` ``` fun_upd :: "('a => 'b) => 'a => 'b => ('a => 'b)" where ``` haftmann@26147 ` 567` ``` "fun_upd f a b == % x. if x=a then b else f x" ``` haftmann@26147 ` 568` wenzelm@41229 ` 569` ```nonterminal updbinds and updbind ``` wenzelm@41229 ` 570` haftmann@26147 ` 571` ```syntax ``` haftmann@26147 ` 572` ``` "_updbind" :: "['a, 'a] => updbind" ("(2_ :=/ _)") ``` haftmann@26147 ` 573` ``` "" :: "updbind => updbinds" ("_") ``` haftmann@26147 ` 574` ``` "_updbinds":: "[updbind, updbinds] => updbinds" ("_,/ _") ``` wenzelm@35115 ` 575` ``` "_Update" :: "['a, updbinds] => 'a" ("_/'((_)')" [1000, 0] 900) ``` haftmann@26147 ` 576` haftmann@26147 ` 577` ```translations ``` wenzelm@35115 ` 578` ``` "_Update f (_updbinds b bs)" == "_Update (_Update f b) bs" ``` wenzelm@35115 ` 579` ``` "f(x:=y)" == "CONST fun_upd f x y" ``` haftmann@26147 ` 580` haftmann@26147 ` 581` ```(* Hint: to define the sum of two functions (or maps), use sum_case. ``` haftmann@26147 ` 582` ``` A nice infix syntax could be defined (in Datatype.thy or below) by ``` wenzelm@35115 ` 583` ```notation ``` wenzelm@35115 ` 584` ``` sum_case (infixr "'(+')"80) ``` haftmann@26147 ` 585` ```*) ``` haftmann@26147 ` 586` paulson@13585 ` 587` ```lemma fun_upd_idem_iff: "(f(x:=y) = f) = (f x = y)" ``` paulson@13585 ` 588` ```apply (simp add: fun_upd_def, safe) ``` paulson@13585 ` 589` ```apply (erule subst) ``` paulson@13585 ` 590` ```apply (rule_tac [2] ext, auto) ``` paulson@13585 ` 591` ```done ``` paulson@13585 ` 592` paulson@13585 ` 593` ```(* f x = y ==> f(x:=y) = f *) ``` paulson@13585 ` 594` ```lemmas fun_upd_idem = fun_upd_idem_iff [THEN iffD2, standard] ``` paulson@13585 ` 595` paulson@13585 ` 596` ```(* f(x := f x) = f *) ``` paulson@17084 ` 597` ```lemmas fun_upd_triv = refl [THEN fun_upd_idem] ``` paulson@17084 ` 598` ```declare fun_upd_triv [iff] ``` paulson@13585 ` 599` paulson@13585 ` 600` ```lemma fun_upd_apply [simp]: "(f(x:=y))z = (if z=x then y else f z)" ``` paulson@17084 ` 601` ```by (simp add: fun_upd_def) ``` paulson@13585 ` 602` paulson@13585 ` 603` ```(* fun_upd_apply supersedes these two, but they are useful ``` paulson@13585 ` 604` ``` if fun_upd_apply is intentionally removed from the simpset *) ``` paulson@13585 ` 605` ```lemma fun_upd_same: "(f(x:=y)) x = y" ``` paulson@13585 ` 606` ```by simp ``` paulson@13585 ` 607` paulson@13585 ` 608` ```lemma fun_upd_other: "z~=x ==> (f(x:=y)) z = f z" ``` paulson@13585 ` 609` ```by simp ``` paulson@13585 ` 610` paulson@13585 ` 611` ```lemma fun_upd_upd [simp]: "f(x:=y,x:=z) = f(x:=z)" ``` nipkow@39302 ` 612` ```by (simp add: fun_eq_iff) ``` paulson@13585 ` 613` paulson@13585 ` 614` ```lemma fun_upd_twist: "a ~= c ==> (m(a:=b))(c:=d) = (m(c:=d))(a:=b)" ``` paulson@13585 ` 615` ```by (rule ext, auto) ``` paulson@13585 ` 616` nipkow@15303 ` 617` ```lemma inj_on_fun_updI: "\ inj_on f A; y \ f`A \ \ inj_on (f(x:=y)) A" ``` krauss@34209 ` 618` ```by (fastsimp simp:inj_on_def image_def) ``` nipkow@15303 ` 619` paulson@15510 ` 620` ```lemma fun_upd_image: ``` paulson@15510 ` 621` ``` "f(x:=y) ` A = (if x \ A then insert y (f ` (A-{x})) else f ` A)" ``` paulson@15510 ` 622` ```by auto ``` paulson@15510 ` 623` nipkow@31080 ` 624` ```lemma fun_upd_comp: "f \ (g(x := y)) = (f \ g)(x := f y)" ``` krauss@34209 ` 625` ```by (auto intro: ext) ``` nipkow@31080 ` 626` haftmann@26147 ` 627` haftmann@26147 ` 628` ```subsection {* @{text override_on} *} ``` haftmann@26147 ` 629` haftmann@26147 ` 630` ```definition ``` haftmann@26147 ` 631` ``` override_on :: "('a \ 'b) \ ('a \ 'b) \ 'a set \ 'a \ 'b" ``` haftmann@26147 ` 632` ```where ``` haftmann@26147 ` 633` ``` "override_on f g A = (\a. if a \ A then g a else f a)" ``` nipkow@13910 ` 634` nipkow@15691 ` 635` ```lemma override_on_emptyset[simp]: "override_on f g {} = f" ``` nipkow@15691 ` 636` ```by(simp add:override_on_def) ``` nipkow@13910 ` 637` nipkow@15691 ` 638` ```lemma override_on_apply_notin[simp]: "a ~: A ==> (override_on f g A) a = f a" ``` nipkow@15691 ` 639` ```by(simp add:override_on_def) ``` nipkow@13910 ` 640` nipkow@15691 ` 641` ```lemma override_on_apply_in[simp]: "a : A ==> (override_on f g A) a = g a" ``` nipkow@15691 ` 642` ```by(simp add:override_on_def) ``` nipkow@13910 ` 643` haftmann@26147 ` 644` haftmann@26147 ` 645` ```subsection {* @{text swap} *} ``` paulson@15510 ` 646` haftmann@22744 ` 647` ```definition ``` haftmann@22744 ` 648` ``` swap :: "'a \ 'a \ ('a \ 'b) \ ('a \ 'b)" ``` haftmann@22744 ` 649` ```where ``` haftmann@22744 ` 650` ``` "swap a b f = f (a := f b, b:= f a)" ``` paulson@15510 ` 651` huffman@34101 ` 652` ```lemma swap_self [simp]: "swap a a f = f" ``` nipkow@15691 ` 653` ```by (simp add: swap_def) ``` paulson@15510 ` 654` paulson@15510 ` 655` ```lemma swap_commute: "swap a b f = swap b a f" ``` paulson@15510 ` 656` ```by (rule ext, simp add: fun_upd_def swap_def) ``` paulson@15510 ` 657` paulson@15510 ` 658` ```lemma swap_nilpotent [simp]: "swap a b (swap a b f) = f" ``` paulson@15510 ` 659` ```by (rule ext, simp add: fun_upd_def swap_def) ``` paulson@15510 ` 660` huffman@34145 ` 661` ```lemma swap_triple: ``` huffman@34145 ` 662` ``` assumes "a \ c" and "b \ c" ``` huffman@34145 ` 663` ``` shows "swap a b (swap b c (swap a b f)) = swap a c f" ``` nipkow@39302 ` 664` ``` using assms by (simp add: fun_eq_iff swap_def) ``` huffman@34145 ` 665` huffman@34101 ` 666` ```lemma comp_swap: "f \ swap a b g = swap a b (f \ g)" ``` huffman@34101 ` 667` ```by (rule ext, simp add: fun_upd_def swap_def) ``` huffman@34101 ` 668` hoelzl@39076 ` 669` ```lemma swap_image_eq [simp]: ``` hoelzl@39076 ` 670` ``` assumes "a \ A" "b \ A" shows "swap a b f ` A = f ` A" ``` hoelzl@39076 ` 671` ```proof - ``` hoelzl@39076 ` 672` ``` have subset: "\f. swap a b f ` A \ f ` A" ``` hoelzl@39076 ` 673` ``` using assms by (auto simp: image_iff swap_def) ``` hoelzl@39076 ` 674` ``` then have "swap a b (swap a b f) ` A \ (swap a b f) ` A" . ``` hoelzl@39076 ` 675` ``` with subset[of f] show ?thesis by auto ``` hoelzl@39076 ` 676` ```qed ``` hoelzl@39076 ` 677` paulson@15510 ` 678` ```lemma inj_on_imp_inj_on_swap: ``` hoelzl@39076 ` 679` ``` "\inj_on f A; a \ A; b \ A\ \ inj_on (swap a b f) A" ``` hoelzl@39076 ` 680` ``` by (simp add: inj_on_def swap_def, blast) ``` paulson@15510 ` 681` paulson@15510 ` 682` ```lemma inj_on_swap_iff [simp]: ``` hoelzl@39076 ` 683` ``` assumes A: "a \ A" "b \ A" shows "inj_on (swap a b f) A \ inj_on f A" ``` hoelzl@39075 ` 684` ```proof ``` paulson@15510 ` 685` ``` assume "inj_on (swap a b f) A" ``` hoelzl@39075 ` 686` ``` with A have "inj_on (swap a b (swap a b f)) A" ``` hoelzl@39075 ` 687` ``` by (iprover intro: inj_on_imp_inj_on_swap) ``` hoelzl@39075 ` 688` ``` thus "inj_on f A" by simp ``` paulson@15510 ` 689` ```next ``` paulson@15510 ` 690` ``` assume "inj_on f A" ``` krauss@34209 ` 691` ``` with A show "inj_on (swap a b f) A" by (iprover intro: inj_on_imp_inj_on_swap) ``` paulson@15510 ` 692` ```qed ``` paulson@15510 ` 693` hoelzl@39076 ` 694` ```lemma surj_imp_surj_swap: "surj f \ surj (swap a b f)" ``` hoelzl@40702 ` 695` ``` by simp ``` paulson@15510 ` 696` hoelzl@39076 ` 697` ```lemma surj_swap_iff [simp]: "surj (swap a b f) \ surj f" ``` hoelzl@40702 ` 698` ``` by simp ``` haftmann@21547 ` 699` hoelzl@39076 ` 700` ```lemma bij_betw_swap_iff [simp]: ``` hoelzl@39076 ` 701` ``` "\ x \ A; y \ A \ \ bij_betw (swap x y f) A B \ bij_betw f A B" ``` hoelzl@39076 ` 702` ``` by (auto simp: bij_betw_def) ``` hoelzl@39076 ` 703` hoelzl@39076 ` 704` ```lemma bij_swap_iff [simp]: "bij (swap a b f) \ bij f" ``` hoelzl@39076 ` 705` ``` by simp ``` hoelzl@39075 ` 706` wenzelm@36176 ` 707` ```hide_const (open) swap ``` haftmann@21547 ` 708` haftmann@31949 ` 709` ```subsection {* Inversion of injective functions *} ``` haftmann@31949 ` 710` nipkow@33057 ` 711` ```definition the_inv_into :: "'a set => ('a => 'b) => ('b => 'a)" where ``` nipkow@33057 ` 712` ```"the_inv_into A f == %x. THE y. y : A & f y = x" ``` nipkow@32961 ` 713` nipkow@33057 ` 714` ```lemma the_inv_into_f_f: ``` nipkow@33057 ` 715` ``` "[| inj_on f A; x : A |] ==> the_inv_into A f (f x) = x" ``` nipkow@33057 ` 716` ```apply (simp add: the_inv_into_def inj_on_def) ``` krauss@34209 ` 717` ```apply blast ``` nipkow@32961 ` 718` ```done ``` nipkow@32961 ` 719` nipkow@33057 ` 720` ```lemma f_the_inv_into_f: ``` nipkow@33057 ` 721` ``` "inj_on f A ==> y : f`A ==> f (the_inv_into A f y) = y" ``` nipkow@33057 ` 722` ```apply (simp add: the_inv_into_def) ``` nipkow@32961 ` 723` ```apply (rule the1I2) ``` nipkow@32961 ` 724` ``` apply(blast dest: inj_onD) ``` nipkow@32961 ` 725` ```apply blast ``` nipkow@32961 ` 726` ```done ``` nipkow@32961 ` 727` nipkow@33057 ` 728` ```lemma the_inv_into_into: ``` nipkow@33057 ` 729` ``` "[| inj_on f A; x : f ` A; A <= B |] ==> the_inv_into A f x : B" ``` nipkow@33057 ` 730` ```apply (simp add: the_inv_into_def) ``` nipkow@32961 ` 731` ```apply (rule the1I2) ``` nipkow@32961 ` 732` ``` apply(blast dest: inj_onD) ``` nipkow@32961 ` 733` ```apply blast ``` nipkow@32961 ` 734` ```done ``` nipkow@32961 ` 735` nipkow@33057 ` 736` ```lemma the_inv_into_onto[simp]: ``` nipkow@33057 ` 737` ``` "inj_on f A ==> the_inv_into A f ` (f ` A) = A" ``` nipkow@33057 ` 738` ```by (fast intro:the_inv_into_into the_inv_into_f_f[symmetric]) ``` nipkow@32961 ` 739` nipkow@33057 ` 740` ```lemma the_inv_into_f_eq: ``` nipkow@33057 ` 741` ``` "[| inj_on f A; f x = y; x : A |] ==> the_inv_into A f y = x" ``` nipkow@32961 ` 742` ``` apply (erule subst) ``` nipkow@33057 ` 743` ``` apply (erule the_inv_into_f_f, assumption) ``` nipkow@32961 ` 744` ``` done ``` nipkow@32961 ` 745` nipkow@33057 ` 746` ```lemma the_inv_into_comp: ``` nipkow@32961 ` 747` ``` "[| inj_on f (g ` A); inj_on g A; x : f ` g ` A |] ==> ``` nipkow@33057 ` 748` ``` the_inv_into A (f o g) x = (the_inv_into A g o the_inv_into (g ` A) f) x" ``` nipkow@33057 ` 749` ```apply (rule the_inv_into_f_eq) ``` nipkow@32961 ` 750` ``` apply (fast intro: comp_inj_on) ``` nipkow@33057 ` 751` ``` apply (simp add: f_the_inv_into_f the_inv_into_into) ``` nipkow@33057 ` 752` ```apply (simp add: the_inv_into_into) ``` nipkow@32961 ` 753` ```done ``` nipkow@32961 ` 754` nipkow@33057 ` 755` ```lemma inj_on_the_inv_into: ``` nipkow@33057 ` 756` ``` "inj_on f A \ inj_on (the_inv_into A f) (f ` A)" ``` nipkow@33057 ` 757` ```by (auto intro: inj_onI simp: image_def the_inv_into_f_f) ``` nipkow@32961 ` 758` nipkow@33057 ` 759` ```lemma bij_betw_the_inv_into: ``` nipkow@33057 ` 760` ``` "bij_betw f A B \ bij_betw (the_inv_into A f) B A" ``` nipkow@33057 ` 761` ```by (auto simp add: bij_betw_def inj_on_the_inv_into the_inv_into_into) ``` nipkow@32961 ` 762` berghofe@32998 ` 763` ```abbreviation the_inv :: "('a \ 'b) \ ('b \ 'a)" where ``` nipkow@33057 ` 764` ``` "the_inv f \ the_inv_into UNIV f" ``` berghofe@32998 ` 765` berghofe@32998 ` 766` ```lemma the_inv_f_f: ``` berghofe@32998 ` 767` ``` assumes "inj f" ``` berghofe@32998 ` 768` ``` shows "the_inv f (f x) = x" using assms UNIV_I ``` nipkow@33057 ` 769` ``` by (rule the_inv_into_f_f) ``` berghofe@32998 ` 770` hoelzl@40703 ` 771` ```subsection {* Cantor's Paradox *} ``` hoelzl@40703 ` 772` hoelzl@40703 ` 773` ```lemma Cantors_paradox: ``` hoelzl@40703 ` 774` ``` "\(\f. f ` A = Pow A)" ``` hoelzl@40703 ` 775` ```proof clarify ``` hoelzl@40703 ` 776` ``` fix f assume "f ` A = Pow A" hence *: "Pow A \ f ` A" by blast ``` hoelzl@40703 ` 777` ``` let ?X = "{a \ A. a \ f a}" ``` hoelzl@40703 ` 778` ``` have "?X \ Pow A" unfolding Pow_def by auto ``` hoelzl@40703 ` 779` ``` with * obtain x where "x \ A \ f x = ?X" by blast ``` hoelzl@40703 ` 780` ``` thus False by best ``` hoelzl@40703 ` 781` ```qed ``` haftmann@31949 ` 782` haftmann@40969 ` 783` ```subsection {* Setup *} ``` haftmann@40969 ` 784` haftmann@40969 ` 785` ```subsubsection {* Proof tools *} ``` haftmann@22845 ` 786` haftmann@22845 ` 787` ```text {* simplifies terms of the form ``` haftmann@22845 ` 788` ``` f(...,x:=y,...,x:=z,...) to f(...,x:=z,...) *} ``` haftmann@22845 ` 789` wenzelm@24017 ` 790` ```simproc_setup fun_upd2 ("f(v := w, x := y)") = {* fn _ => ``` haftmann@22845 ` 791` ```let ``` haftmann@22845 ` 792` ``` fun gen_fun_upd NONE T _ _ = NONE ``` wenzelm@24017 ` 793` ``` | gen_fun_upd (SOME f) T x y = SOME (Const (@{const_name fun_upd}, T) \$ f \$ x \$ y) ``` haftmann@22845 ` 794` ``` fun dest_fun_T1 (Type (_, T :: Ts)) = T ``` haftmann@22845 ` 795` ``` fun find_double (t as Const (@{const_name fun_upd},T) \$ f \$ x \$ y) = ``` haftmann@22845 ` 796` ``` let ``` haftmann@22845 ` 797` ``` fun find (Const (@{const_name fun_upd},T) \$ g \$ v \$ w) = ``` haftmann@22845 ` 798` ``` if v aconv x then SOME g else gen_fun_upd (find g) T v w ``` haftmann@22845 ` 799` ``` | find t = NONE ``` haftmann@22845 ` 800` ``` in (dest_fun_T1 T, gen_fun_upd (find f) T x y) end ``` wenzelm@24017 ` 801` wenzelm@24017 ` 802` ``` fun proc ss ct = ``` wenzelm@24017 ` 803` ``` let ``` wenzelm@24017 ` 804` ``` val ctxt = Simplifier.the_context ss ``` wenzelm@24017 ` 805` ``` val t = Thm.term_of ct ``` wenzelm@24017 ` 806` ``` in ``` wenzelm@24017 ` 807` ``` case find_double t of ``` wenzelm@24017 ` 808` ``` (T, NONE) => NONE ``` wenzelm@24017 ` 809` ``` | (T, SOME rhs) => ``` wenzelm@27330 ` 810` ``` SOME (Goal.prove ctxt [] [] (Logic.mk_equals (t, rhs)) ``` wenzelm@24017 ` 811` ``` (fn _ => ``` wenzelm@24017 ` 812` ``` rtac eq_reflection 1 THEN ``` wenzelm@24017 ` 813` ``` rtac ext 1 THEN ``` wenzelm@24017 ` 814` ``` simp_tac (Simplifier.inherit_context ss @{simpset}) 1)) ``` wenzelm@24017 ` 815` ``` end ``` wenzelm@24017 ` 816` ```in proc end ``` haftmann@22845 ` 817` ```*} ``` haftmann@22845 ` 818` haftmann@22845 ` 819` haftmann@40969 ` 820` ```subsubsection {* Code generator *} ``` haftmann@21870 ` 821` berghofe@25886 ` 822` ```types_code ``` berghofe@25886 ` 823` ``` "fun" ("(_ ->/ _)") ``` berghofe@25886 ` 824` ```attach (term_of) {* ``` berghofe@25886 ` 825` ```fun term_of_fun_type _ aT _ bT _ = Free ("", aT --> bT); ``` berghofe@25886 ` 826` ```*} ``` berghofe@25886 ` 827` ```attach (test) {* ``` berghofe@25886 ` 828` ```fun gen_fun_type aF aT bG bT i = ``` berghofe@25886 ` 829` ``` let ``` wenzelm@32740 ` 830` ``` val tab = Unsynchronized.ref []; ``` berghofe@25886 ` 831` ``` fun mk_upd (x, (_, y)) t = Const ("Fun.fun_upd", ``` berghofe@25886 ` 832` ``` (aT --> bT) --> aT --> bT --> aT --> bT) \$ t \$ aF x \$ y () ``` berghofe@25886 ` 833` ``` in ``` berghofe@25886 ` 834` ``` (fn x => ``` berghofe@25886 ` 835` ``` case AList.lookup op = (!tab) x of ``` berghofe@25886 ` 836` ``` NONE => ``` berghofe@25886 ` 837` ``` let val p as (y, _) = bG i ``` berghofe@25886 ` 838` ``` in (tab := (x, p) :: !tab; y) end ``` berghofe@25886 ` 839` ``` | SOME (y, _) => y, ``` berghofe@28711 ` 840` ``` fn () => Basics.fold mk_upd (!tab) (Const ("HOL.undefined", aT --> bT))) ``` berghofe@25886 ` 841` ``` end; ``` berghofe@25886 ` 842` ```*} ``` berghofe@25886 ` 843` haftmann@21870 ` 844` ```code_const "op \" ``` haftmann@21870 ` 845` ``` (SML infixl 5 "o") ``` haftmann@21870 ` 846` ``` (Haskell infixr 9 ".") ``` haftmann@21870 ` 847` haftmann@21906 ` 848` ```code_const "id" ``` haftmann@21906 ` 849` ``` (Haskell "id") ``` haftmann@21906 ` 850` haftmann@40969 ` 851` haftmann@40969 ` 852` ```subsubsection {* Functorial structure of types *} ``` haftmann@40969 ` 853` haftmann@41505 ` 854` ```use "Tools/enriched_type.ML" ``` haftmann@40969 ` 855` nipkow@2912 ` 856` ```end ```