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