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