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