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