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