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