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