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