src/HOL/Fun.thy
author huffman
Thu Aug 11 09:11:15 2011 -0700 (2011-08-11)
changeset 44165 d26a45f3c835
parent 43991 f4a7697011c5
child 44277 bcb696533579
permissions -rw-r--r--
remove lemma stupid_ext
     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 inj_on_vimage_singleton:
   560   "inj_on f A \<Longrightarrow> f -` {a} \<inter> A \<subseteq> {THE x. x \<in> A \<and> f x = a}"
   561   by (auto simp add: inj_on_def intro: the_equality [symmetric])
   562 
   563 lemma (in ordered_ab_group_add) inj_uminus[simp, intro]: "inj_on uminus A"
   564   by (auto intro!: inj_onI)
   565 
   566 lemma (in linorder) strict_mono_imp_inj_on: "strict_mono f \<Longrightarrow> inj_on f A"
   567   by (auto intro!: inj_onI dest: strict_mono_eq)
   568 
   569 
   570 subsection{*Function Updating*}
   571 
   572 definition
   573   fun_upd :: "('a => 'b) => 'a => 'b => ('a => 'b)" where
   574   "fun_upd f a b == % x. if x=a then b else f x"
   575 
   576 nonterminal updbinds and updbind
   577 
   578 syntax
   579   "_updbind" :: "['a, 'a] => updbind"             ("(2_ :=/ _)")
   580   ""         :: "updbind => updbinds"             ("_")
   581   "_updbinds":: "[updbind, updbinds] => updbinds" ("_,/ _")
   582   "_Update"  :: "['a, updbinds] => 'a"            ("_/'((_)')" [1000, 0] 900)
   583 
   584 translations
   585   "_Update f (_updbinds b bs)" == "_Update (_Update f b) bs"
   586   "f(x:=y)" == "CONST fun_upd f x y"
   587 
   588 (* Hint: to define the sum of two functions (or maps), use sum_case.
   589          A nice infix syntax could be defined (in Datatype.thy or below) by
   590 notation
   591   sum_case  (infixr "'(+')"80)
   592 *)
   593 
   594 lemma fun_upd_idem_iff: "(f(x:=y) = f) = (f x = y)"
   595 apply (simp add: fun_upd_def, safe)
   596 apply (erule subst)
   597 apply (rule_tac [2] ext, auto)
   598 done
   599 
   600 (* f x = y ==> f(x:=y) = f *)
   601 lemmas fun_upd_idem = fun_upd_idem_iff [THEN iffD2, standard]
   602 
   603 (* f(x := f x) = f *)
   604 lemmas fun_upd_triv = refl [THEN fun_upd_idem]
   605 declare fun_upd_triv [iff]
   606 
   607 lemma fun_upd_apply [simp]: "(f(x:=y))z = (if z=x then y else f z)"
   608 by (simp add: fun_upd_def)
   609 
   610 (* fun_upd_apply supersedes these two,   but they are useful
   611    if fun_upd_apply is intentionally removed from the simpset *)
   612 lemma fun_upd_same: "(f(x:=y)) x = y"
   613 by simp
   614 
   615 lemma fun_upd_other: "z~=x ==> (f(x:=y)) z = f z"
   616 by simp
   617 
   618 lemma fun_upd_upd [simp]: "f(x:=y,x:=z) = f(x:=z)"
   619 by (simp add: fun_eq_iff)
   620 
   621 lemma fun_upd_twist: "a ~= c ==> (m(a:=b))(c:=d) = (m(c:=d))(a:=b)"
   622 by (rule ext, auto)
   623 
   624 lemma inj_on_fun_updI: "\<lbrakk> inj_on f A; y \<notin> f`A \<rbrakk> \<Longrightarrow> inj_on (f(x:=y)) A"
   625 by (fastsimp simp:inj_on_def image_def)
   626 
   627 lemma fun_upd_image:
   628      "f(x:=y) ` A = (if x \<in> A then insert y (f ` (A-{x})) else f ` A)"
   629 by auto
   630 
   631 lemma fun_upd_comp: "f \<circ> (g(x := y)) = (f \<circ> g)(x := f y)"
   632 by (auto intro: ext)
   633 
   634 
   635 subsection {* @{text override_on} *}
   636 
   637 definition
   638   override_on :: "('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'a set \<Rightarrow> 'a \<Rightarrow> 'b"
   639 where
   640   "override_on f g A = (\<lambda>a. if a \<in> A then g a else f a)"
   641 
   642 lemma override_on_emptyset[simp]: "override_on f g {} = f"
   643 by(simp add:override_on_def)
   644 
   645 lemma override_on_apply_notin[simp]: "a ~: A ==> (override_on f g A) a = f a"
   646 by(simp add:override_on_def)
   647 
   648 lemma override_on_apply_in[simp]: "a : A ==> (override_on f g A) a = g a"
   649 by(simp add:override_on_def)
   650 
   651 
   652 subsection {* @{text swap} *}
   653 
   654 definition
   655   swap :: "'a \<Rightarrow> 'a \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b)"
   656 where
   657   "swap a b f = f (a := f b, b:= f a)"
   658 
   659 lemma swap_self [simp]: "swap a a f = f"
   660 by (simp add: swap_def)
   661 
   662 lemma swap_commute: "swap a b f = swap b a f"
   663 by (rule ext, simp add: fun_upd_def swap_def)
   664 
   665 lemma swap_nilpotent [simp]: "swap a b (swap a b f) = f"
   666 by (rule ext, simp add: fun_upd_def swap_def)
   667 
   668 lemma swap_triple:
   669   assumes "a \<noteq> c" and "b \<noteq> c"
   670   shows "swap a b (swap b c (swap a b f)) = swap a c f"
   671   using assms by (simp add: fun_eq_iff swap_def)
   672 
   673 lemma comp_swap: "f \<circ> swap a b g = swap a b (f \<circ> g)"
   674 by (rule ext, simp add: fun_upd_def swap_def)
   675 
   676 lemma swap_image_eq [simp]:
   677   assumes "a \<in> A" "b \<in> A" shows "swap a b f ` A = f ` A"
   678 proof -
   679   have subset: "\<And>f. swap a b f ` A \<subseteq> f ` A"
   680     using assms by (auto simp: image_iff swap_def)
   681   then have "swap a b (swap a b f) ` A \<subseteq> (swap a b f) ` A" .
   682   with subset[of f] show ?thesis by auto
   683 qed
   684 
   685 lemma inj_on_imp_inj_on_swap:
   686   "\<lbrakk>inj_on f A; a \<in> A; b \<in> A\<rbrakk> \<Longrightarrow> inj_on (swap a b f) A"
   687   by (simp add: inj_on_def swap_def, blast)
   688 
   689 lemma inj_on_swap_iff [simp]:
   690   assumes A: "a \<in> A" "b \<in> A" shows "inj_on (swap a b f) A \<longleftrightarrow> inj_on f A"
   691 proof
   692   assume "inj_on (swap a b f) A"
   693   with A have "inj_on (swap a b (swap a b f)) A"
   694     by (iprover intro: inj_on_imp_inj_on_swap)
   695   thus "inj_on f A" by simp
   696 next
   697   assume "inj_on f A"
   698   with A show "inj_on (swap a b f) A" by (iprover intro: inj_on_imp_inj_on_swap)
   699 qed
   700 
   701 lemma surj_imp_surj_swap: "surj f \<Longrightarrow> surj (swap a b f)"
   702   by simp
   703 
   704 lemma surj_swap_iff [simp]: "surj (swap a b f) \<longleftrightarrow> surj f"
   705   by simp
   706 
   707 lemma bij_betw_swap_iff [simp]:
   708   "\<lbrakk> x \<in> A; y \<in> A \<rbrakk> \<Longrightarrow> bij_betw (swap x y f) A B \<longleftrightarrow> bij_betw f A B"
   709   by (auto simp: bij_betw_def)
   710 
   711 lemma bij_swap_iff [simp]: "bij (swap a b f) \<longleftrightarrow> bij f"
   712   by simp
   713 
   714 hide_const (open) swap
   715 
   716 subsection {* Inversion of injective functions *}
   717 
   718 definition the_inv_into :: "'a set => ('a => 'b) => ('b => 'a)" where
   719 "the_inv_into A f == %x. THE y. y : A & f y = x"
   720 
   721 lemma the_inv_into_f_f:
   722   "[| inj_on f A;  x : A |] ==> the_inv_into A f (f x) = x"
   723 apply (simp add: the_inv_into_def inj_on_def)
   724 apply blast
   725 done
   726 
   727 lemma f_the_inv_into_f:
   728   "inj_on f A ==> y : f`A  ==> f (the_inv_into A f y) = y"
   729 apply (simp add: the_inv_into_def)
   730 apply (rule the1I2)
   731  apply(blast dest: inj_onD)
   732 apply blast
   733 done
   734 
   735 lemma the_inv_into_into:
   736   "[| inj_on f A; x : f ` A; A <= B |] ==> the_inv_into A f x : B"
   737 apply (simp add: the_inv_into_def)
   738 apply (rule the1I2)
   739  apply(blast dest: inj_onD)
   740 apply blast
   741 done
   742 
   743 lemma the_inv_into_onto[simp]:
   744   "inj_on f A ==> the_inv_into A f ` (f ` A) = A"
   745 by (fast intro:the_inv_into_into the_inv_into_f_f[symmetric])
   746 
   747 lemma the_inv_into_f_eq:
   748   "[| inj_on f A; f x = y; x : A |] ==> the_inv_into A f y = x"
   749   apply (erule subst)
   750   apply (erule the_inv_into_f_f, assumption)
   751   done
   752 
   753 lemma the_inv_into_comp:
   754   "[| inj_on f (g ` A); inj_on g A; x : f ` g ` A |] ==>
   755   the_inv_into A (f o g) x = (the_inv_into A g o the_inv_into (g ` A) f) x"
   756 apply (rule the_inv_into_f_eq)
   757   apply (fast intro: comp_inj_on)
   758  apply (simp add: f_the_inv_into_f the_inv_into_into)
   759 apply (simp add: the_inv_into_into)
   760 done
   761 
   762 lemma inj_on_the_inv_into:
   763   "inj_on f A \<Longrightarrow> inj_on (the_inv_into A f) (f ` A)"
   764 by (auto intro: inj_onI simp: image_def the_inv_into_f_f)
   765 
   766 lemma bij_betw_the_inv_into:
   767   "bij_betw f A B \<Longrightarrow> bij_betw (the_inv_into A f) B A"
   768 by (auto simp add: bij_betw_def inj_on_the_inv_into the_inv_into_into)
   769 
   770 abbreviation the_inv :: "('a \<Rightarrow> 'b) \<Rightarrow> ('b \<Rightarrow> 'a)" where
   771   "the_inv f \<equiv> the_inv_into UNIV f"
   772 
   773 lemma the_inv_f_f:
   774   assumes "inj f"
   775   shows "the_inv f (f x) = x" using assms UNIV_I
   776   by (rule the_inv_into_f_f)
   777 
   778 subsection {* Cantor's Paradox *}
   779 
   780 lemma Cantors_paradox [no_atp]:
   781   "\<not>(\<exists>f. f ` A = Pow A)"
   782 proof clarify
   783   fix f assume "f ` A = Pow A" hence *: "Pow A \<le> f ` A" by blast
   784   let ?X = "{a \<in> A. a \<notin> f a}"
   785   have "?X \<in> Pow A" unfolding Pow_def by auto
   786   with * obtain x where "x \<in> A \<and> f x = ?X" by blast
   787   thus False by best
   788 qed
   789 
   790 subsection {* Setup *} 
   791 
   792 subsubsection {* Proof tools *}
   793 
   794 text {* simplifies terms of the form
   795   f(...,x:=y,...,x:=z,...) to f(...,x:=z,...) *}
   796 
   797 simproc_setup fun_upd2 ("f(v := w, x := y)") = {* fn _ =>
   798 let
   799   fun gen_fun_upd NONE T _ _ = NONE
   800     | gen_fun_upd (SOME f) T x y = SOME (Const (@{const_name fun_upd}, T) $ f $ x $ y)
   801   fun dest_fun_T1 (Type (_, T :: Ts)) = T
   802   fun find_double (t as Const (@{const_name fun_upd},T) $ f $ x $ y) =
   803     let
   804       fun find (Const (@{const_name fun_upd},T) $ g $ v $ w) =
   805             if v aconv x then SOME g else gen_fun_upd (find g) T v w
   806         | find t = NONE
   807     in (dest_fun_T1 T, gen_fun_upd (find f) T x y) end
   808 
   809   fun proc ss ct =
   810     let
   811       val ctxt = Simplifier.the_context ss
   812       val t = Thm.term_of ct
   813     in
   814       case find_double t of
   815         (T, NONE) => NONE
   816       | (T, SOME rhs) =>
   817           SOME (Goal.prove ctxt [] [] (Logic.mk_equals (t, rhs))
   818             (fn _ =>
   819               rtac eq_reflection 1 THEN
   820               rtac ext 1 THEN
   821               simp_tac (Simplifier.inherit_context ss @{simpset}) 1))
   822     end
   823 in proc end
   824 *}
   825 
   826 
   827 subsubsection {* Code generator *}
   828 
   829 types_code
   830   "fun"  ("(_ ->/ _)")
   831 attach (term_of) {*
   832 fun term_of_fun_type _ aT _ bT _ = Free ("<function>", aT --> bT);
   833 *}
   834 attach (test) {*
   835 fun gen_fun_type aF aT bG bT i =
   836   let
   837     val tab = Unsynchronized.ref [];
   838     fun mk_upd (x, (_, y)) t = Const ("Fun.fun_upd",
   839       (aT --> bT) --> aT --> bT --> aT --> bT) $ t $ aF x $ y ()
   840   in
   841     (fn x =>
   842        case AList.lookup op = (!tab) x of
   843          NONE =>
   844            let val p as (y, _) = bG i
   845            in (tab := (x, p) :: !tab; y) end
   846        | SOME (y, _) => y,
   847      fn () => Basics.fold mk_upd (!tab) (Const ("HOL.undefined", aT --> bT)))
   848   end;
   849 *}
   850 
   851 code_const "op \<circ>"
   852   (SML infixl 5 "o")
   853   (Haskell infixr 9 ".")
   854 
   855 code_const "id"
   856   (Haskell "id")
   857 
   858 
   859 subsubsection {* Functorial structure of types *}
   860 
   861 use "Tools/enriched_type.ML"
   862 
   863 end