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