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