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