src/HOL/Fun.thy
 author wenzelm Thu Mar 14 16:55:06 2019 +0100 (5 weeks ago) changeset 69913 ca515cf61651 parent 69768 7e4966eaf781 permissions -rw-r--r--
more specific keyword kinds;
```     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_defn
```
```    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 ((`) 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 :: "('a \<Rightarrow> 'b) \<Rightarrow> bool"
```
```   152   where "inj f \<equiv> inj_on f UNIV"
```
```   153
```
```   154 abbreviation surj :: "('a \<Rightarrow> 'b) \<Rightarrow> bool"
```
```   155   where "surj f \<equiv> range f = UNIV"
```
```   156
```
```   157 translations \<comment> \<open>The negated case:\<close>
```
```   158   "\<not> CONST surj f" \<leftharpoondown> "CONST range f \<noteq> CONST UNIV"
```
```   159
```
```   160 abbreviation bij :: "('a \<Rightarrow> 'b) \<Rightarrow> bool"
```
```   161   where "bij f \<equiv> bij_betw f UNIV UNIV"
```
```   162
```
```   163 lemma inj_def: "inj f \<longleftrightarrow> (\<forall>x y. f x = f y \<longrightarrow> x = y)"
```
```   164   unfolding inj_on_def by blast
```
```   165
```
```   166 lemma injI: "(\<And>x y. f x = f y \<Longrightarrow> x = y) \<Longrightarrow> inj f"
```
```   167   unfolding inj_def by blast
```
```   168
```
```   169 theorem range_ex1_eq: "inj f \<Longrightarrow> b \<in> range f \<longleftrightarrow> (\<exists>!x. b = f x)"
```
```   170   unfolding inj_def by blast
```
```   171
```
```   172 lemma injD: "inj f \<Longrightarrow> f x = f y \<Longrightarrow> x = y"
```
```   173   by (simp add: inj_def)
```
```   174
```
```   175 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"
```
```   176   by (auto simp: inj_on_def)
```
```   177
```
```   178 lemma inj_on_cong: "(\<And>a. a \<in> A \<Longrightarrow> f a = g a) \<Longrightarrow> inj_on f A \<longleftrightarrow> inj_on g A"
```
```   179   by (auto simp: inj_on_def)
```
```   180
```
```   181 lemma inj_on_strict_subset: "inj_on f B \<Longrightarrow> A \<subset> B \<Longrightarrow> f ` A \<subset> f ` B"
```
```   182   unfolding inj_on_def by blast
```
```   183
```
```   184 lemma inj_compose: "inj f \<Longrightarrow> inj g \<Longrightarrow> inj (f \<circ> g)"
```
```   185   by (simp add: inj_def)
```
```   186
```
```   187 lemma inj_fun: "inj f \<Longrightarrow> inj (\<lambda>x y. f x)"
```
```   188   by (simp add: inj_def fun_eq_iff)
```
```   189
```
```   190 lemma inj_eq: "inj f \<Longrightarrow> f x = f y \<longleftrightarrow> x = y"
```
```   191   by (simp add: inj_on_eq_iff)
```
```   192
```
```   193 lemma inj_on_id[simp]: "inj_on id A"
```
```   194   by (simp add: inj_on_def)
```
```   195
```
```   196 lemma inj_on_id2[simp]: "inj_on (\<lambda>x. x) A"
```
```   197   by (simp add: inj_on_def)
```
```   198
```
```   199 lemma inj_on_Int: "inj_on f A \<or> inj_on f B \<Longrightarrow> inj_on f (A \<inter> B)"
```
```   200   unfolding inj_on_def by blast
```
```   201
```
```   202 lemma surj_id: "surj id"
```
```   203   by simp
```
```   204
```
```   205 lemma bij_id[simp]: "bij id"
```
```   206   by (simp add: bij_betw_def)
```
```   207
```
```   208 lemma bij_uminus: "bij (uminus :: 'a \<Rightarrow> 'a::ab_group_add)"
```
```   209   unfolding bij_betw_def inj_on_def
```
```   210   by (force intro: minus_minus [symmetric])
```
```   211
```
```   212 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"
```
```   213   by (simp add: inj_on_def)
```
```   214
```
```   215 lemma inj_on_inverseI: "(\<And>x. x \<in> A \<Longrightarrow> g (f x) = x) \<Longrightarrow> inj_on f A"
```
```   216   by (auto dest: arg_cong [of concl: g] simp add: inj_on_def)
```
```   217
```
```   218 lemma inj_onD: "inj_on f A \<Longrightarrow> f x = f y \<Longrightarrow> x \<in> A \<Longrightarrow> y \<in> A \<Longrightarrow> x = y"
```
```   219   unfolding inj_on_def by blast
```
```   220
```
```   221 lemma inj_on_subset:
```
```   222   assumes "inj_on f A"
```
```   223     and "B \<subseteq> A"
```
```   224   shows "inj_on f B"
```
```   225 proof (rule inj_onI)
```
```   226   fix a b
```
```   227   assume "a \<in> B" and "b \<in> B"
```
```   228   with assms have "a \<in> A" and "b \<in> A"
```
```   229     by auto
```
```   230   moreover assume "f a = f b"
```
```   231   ultimately show "a = b"
```
```   232     using assms by (auto dest: inj_onD)
```
```   233 qed
```
```   234
```
```   235 lemma comp_inj_on: "inj_on f A \<Longrightarrow> inj_on g (f ` A) \<Longrightarrow> inj_on (g \<circ> f) A"
```
```   236   by (simp add: comp_def inj_on_def)
```
```   237
```
```   238 lemma inj_on_imageI: "inj_on (g \<circ> f) A \<Longrightarrow> inj_on g (f ` A)"
```
```   239   by (auto simp add: inj_on_def)
```
```   240
```
```   241 lemma inj_on_image_iff:
```
```   242   "\<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) \<longleftrightarrow> inj_on g A"
```
```   243   unfolding inj_on_def by blast
```
```   244
```
```   245 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"
```
```   246   unfolding inj_on_def by blast
```
```   247
```
```   248 lemma inj_singleton [simp]: "inj_on (\<lambda>x. {x}) A"
```
```   249   by (simp add: inj_on_def)
```
```   250
```
```   251 lemma inj_on_empty[iff]: "inj_on f {}"
```
```   252   by (simp add: inj_on_def)
```
```   253
```
```   254 lemma subset_inj_on: "inj_on f B \<Longrightarrow> A \<subseteq> B \<Longrightarrow> inj_on f A"
```
```   255   unfolding inj_on_def by blast
```
```   256
```
```   257 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) = {}"
```
```   258   unfolding inj_on_def by (blast intro: sym)
```
```   259
```
```   260 lemma inj_on_insert [iff]: "inj_on f (insert a A) \<longleftrightarrow> inj_on f A \<and> f a \<notin> f ` (A - {a})"
```
```   261   unfolding inj_on_def by (blast intro: sym)
```
```   262
```
```   263 lemma inj_on_diff: "inj_on f A \<Longrightarrow> inj_on f (A - B)"
```
```   264   unfolding inj_on_def by blast
```
```   265
```
```   266 lemma comp_inj_on_iff: "inj_on f A \<Longrightarrow> inj_on f' (f ` A) \<longleftrightarrow> inj_on (f' \<circ> f) A"
```
```   267   by (auto simp: comp_inj_on inj_on_def)
```
```   268
```
```   269 lemma inj_on_imageI2: "inj_on (f' \<circ> f) A \<Longrightarrow> inj_on f A"
```
```   270   by (auto simp: comp_inj_on inj_on_def)
```
```   271
```
```   272 lemma inj_img_insertE:
```
```   273   assumes "inj_on f A"
```
```   274   assumes "x \<notin> B"
```
```   275     and "insert x B = f ` A"
```
```   276   obtains x' A' where "x' \<notin> A'" and "A = insert x' A'" and "x = f x'" and "B = f ` A'"
```
```   277 proof -
```
```   278   from assms have "x \<in> f ` A" by auto
```
```   279   then obtain x' where *: "x' \<in> A" "x = f x'" by auto
```
```   280   then have A: "A = insert x' (A - {x'})" by auto
```
```   281   with assms * have B: "B = f ` (A - {x'})" by (auto dest: inj_on_contraD)
```
```   282   have "x' \<notin> A - {x'}" by simp
```
```   283   from this A \<open>x = f x'\<close> B show ?thesis ..
```
```   284 qed
```
```   285
```
```   286 lemma linorder_injI:
```
```   287   assumes "\<And>x y::'a::linorder. x < y \<Longrightarrow> f x \<noteq> f y"
```
```   288   shows "inj f"
```
```   289   \<comment> \<open>Courtesy of Stephan Merz\<close>
```
```   290 proof (rule inj_onI)
```
```   291   show "x = y" if "f x = f y" for x y
```
```   292    by (rule linorder_cases) (auto dest: assms simp: that)
```
```   293 qed
```
```   294
```
```   295
```
```   296 lemma inj_on_image_Pow: "inj_on f A \<Longrightarrow>inj_on (image f) (Pow A)"
```
```   297   unfolding Pow_def inj_on_def by blast
```
```   298
```
```   299 lemma bij_betw_image_Pow: "bij_betw f A B \<Longrightarrow> bij_betw (image f) (Pow A) (Pow B)"
```
```   300   by (auto simp add: bij_betw_def inj_on_image_Pow image_Pow_surj)
```
```   301
```
```   302 lemma surj_def: "surj f \<longleftrightarrow> (\<forall>y. \<exists>x. y = f x)"
```
```   303   by auto
```
```   304
```
```   305 lemma surjI:
```
```   306   assumes "\<And>x. g (f x) = x"
```
```   307   shows "surj g"
```
```   308   using assms [symmetric] by auto
```
```   309
```
```   310 lemma surjD: "surj f \<Longrightarrow> \<exists>x. y = f x"
```
```   311   by (simp add: surj_def)
```
```   312
```
```   313 lemma surjE: "surj f \<Longrightarrow> (\<And>x. y = f x \<Longrightarrow> C) \<Longrightarrow> C"
```
```   314   by (simp add: surj_def) blast
```
```   315
```
```   316 lemma comp_surj: "surj f \<Longrightarrow> surj g \<Longrightarrow> surj (g \<circ> f)"
```
```   317   using image_comp [of g f UNIV] by simp
```
```   318
```
```   319 lemma bij_betw_imageI: "inj_on f A \<Longrightarrow> f ` A = B \<Longrightarrow> bij_betw f A B"
```
```   320   unfolding bij_betw_def by clarify
```
```   321
```
```   322 lemma bij_betw_imp_surj_on: "bij_betw f A B \<Longrightarrow> f ` A = B"
```
```   323   unfolding bij_betw_def by clarify
```
```   324
```
```   325 lemma bij_betw_imp_surj: "bij_betw f A UNIV \<Longrightarrow> surj f"
```
```   326   unfolding bij_betw_def by auto
```
```   327
```
```   328 lemma bij_betw_empty1: "bij_betw f {} A \<Longrightarrow> A = {}"
```
```   329   unfolding bij_betw_def by blast
```
```   330
```
```   331 lemma bij_betw_empty2: "bij_betw f A {} \<Longrightarrow> A = {}"
```
```   332   unfolding bij_betw_def by blast
```
```   333
```
```   334 lemma inj_on_imp_bij_betw: "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   by (rule bij_betw_def)
```
```   339
```
```   340 lemma bijI: "inj f \<Longrightarrow> surj f \<Longrightarrow> bij f"
```
```   341   by (rule bij_betw_imageI)
```
```   342
```
```   343 lemma bij_is_inj: "bij f \<Longrightarrow> inj f"
```
```   344   by (simp add: bij_def)
```
```   345
```
```   346 lemma bij_is_surj: "bij f \<Longrightarrow> 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: "bij_betw f A B \<Longrightarrow> bij_betw g B C \<Longrightarrow> bij_betw (g \<circ> f) A C"
```
```   353   by (auto simp add:bij_betw_def comp_inj_on)
```
```   354
```
```   355 lemma bij_comp: "bij f \<Longrightarrow> bij g \<Longrightarrow> bij (g \<circ> f)"
```
```   356   by (rule bij_betw_trans)
```
```   357
```
```   358 lemma bij_betw_comp_iff: "bij_betw f A A' \<Longrightarrow> bij_betw f' A' A'' \<longleftrightarrow> bij_betw (f' \<circ> f) A A''"
```
```   359   by (auto simp add: bij_betw_def inj_on_def)
```
```   360
```
```   361 lemma bij_betw_comp_iff2:
```
```   362   assumes bij: "bij_betw f' A' A''"
```
```   363     and img: "f ` A \<le> A'"
```
```   364   shows "bij_betw f A A' \<longleftrightarrow> bij_betw (f' \<circ> f) A A''"
```
```   365   using assms
```
```   366 proof (auto simp add: bij_betw_comp_iff)
```
```   367   assume *: "bij_betw (f' \<circ> f) A A''"
```
```   368   then show "bij_betw f A A'"
```
```   369     using img
```
```   370   proof (auto simp add: bij_betw_def)
```
```   371     assume "inj_on (f' \<circ> f) A"
```
```   372     then show "inj_on f A"
```
```   373       using inj_on_imageI2 by blast
```
```   374   next
```
```   375     fix a'
```
```   376     assume **: "a' \<in> A'"
```
```   377     with bij have "f' a' \<in> A''"
```
```   378       unfolding bij_betw_def by auto
```
```   379     with * obtain a where 1: "a \<in> A \<and> f' (f a) = f' a'"
```
```   380       unfolding bij_betw_def by force
```
```   381     with img have "f a \<in> A'" by auto
```
```   382     with bij ** 1 have "f a = a'"
```
```   383       unfolding bij_betw_def inj_on_def by auto
```
```   384     with 1 show "a' \<in> f ` A" by auto
```
```   385   qed
```
```   386 qed
```
```   387
```
```   388 lemma bij_betw_inv:
```
```   389   assumes "bij_betw f A B"
```
```   390   shows "\<exists>g. bij_betw g B A"
```
```   391 proof -
```
```   392   have i: "inj_on f A" and s: "f ` A = B"
```
```   393     using assms by (auto simp: bij_betw_def)
```
```   394   let ?P = "\<lambda>b a. a \<in> A \<and> f a = b"
```
```   395   let ?g = "\<lambda>b. The (?P b)"
```
```   396   have g: "?g b = a" if P: "?P b a" for a b
```
```   397   proof -
```
```   398     from that s have ex1: "\<exists>a. ?P b a" by blast
```
```   399     then have uex1: "\<exists>!a. ?P b a" by (blast dest:inj_onD[OF i])
```
```   400     then show ?thesis
```
```   401       using the1_equality[OF uex1, OF P] P by simp
```
```   402   qed
```
```   403   have "inj_on ?g B"
```
```   404   proof (rule inj_onI)
```
```   405     fix x y
```
```   406     assume "x \<in> B" "y \<in> B" "?g x = ?g y"
```
```   407     from s \<open>x \<in> B\<close> obtain a1 where a1: "?P x a1" by blast
```
```   408     from s \<open>y \<in> B\<close> obtain a2 where a2: "?P y a2" by blast
```
```   409     from g [OF a1] a1 g [OF a2] a2 \<open>?g x = ?g y\<close> show "x = y" by simp
```
```   410   qed
```
```   411   moreover have "?g ` B = A"
```
```   412   proof (auto simp: image_def)
```
```   413     fix b
```
```   414     assume "b \<in> B"
```
```   415     with s obtain a where P: "?P b a" by blast
```
```   416     with g[OF P] show "?g b \<in> A" by auto
```
```   417   next
```
```   418     fix a
```
```   419     assume "a \<in> A"
```
```   420     with s obtain b where P: "?P b a" by blast
```
```   421     with s have "b \<in> B" by blast
```
```   422     with g[OF P] show "\<exists>b\<in>B. a = ?g b" by blast
```
```   423   qed
```
```   424   ultimately show ?thesis
```
```   425     by (auto simp: bij_betw_def)
```
```   426 qed
```
```   427
```
```   428 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'"
```
```   429   unfolding bij_betw_def inj_on_def by safe force+  (* somewhat slow *)
```
```   430
```
```   431 lemma bij_betw_id[intro, simp]: "bij_betw id A A"
```
```   432   unfolding bij_betw_def id_def by auto
```
```   433
```
```   434 lemma bij_betw_id_iff: "bij_betw id A B \<longleftrightarrow> A = B"
```
```   435   by (auto simp add: bij_betw_def)
```
```   436
```
```   437 lemma bij_betw_combine:
```
```   438   "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)"
```
```   439   unfolding bij_betw_def inj_on_Un image_Un by auto
```
```   440
```
```   441 lemma bij_betw_subset: "bij_betw f A A' \<Longrightarrow> B \<subseteq> A \<Longrightarrow> f ` B = B' \<Longrightarrow> bij_betw f B B'"
```
```   442   by (auto simp add: bij_betw_def inj_on_def)
```
```   443
```
```   444 lemma bij_pointE:
```
```   445   assumes "bij f"
```
```   446   obtains x where "y = f x" and "\<And>x'. y = f x' \<Longrightarrow> x' = x"
```
```   447 proof -
```
```   448   from assms have "inj f" by (rule bij_is_inj)
```
```   449   moreover from assms have "surj f" by (rule bij_is_surj)
```
```   450   then have "y \<in> range f" by simp
```
```   451   ultimately have "\<exists>!x. y = f x" by (simp add: range_ex1_eq)
```
```   452   with that show thesis by blast
```
```   453 qed
```
```   454
```
```   455 lemma surj_image_vimage_eq: "surj f \<Longrightarrow> f ` (f -` A) = A"
```
```   456   by simp
```
```   457
```
```   458 lemma surj_vimage_empty:
```
```   459   assumes "surj f"
```
```   460   shows "f -` A = {} \<longleftrightarrow> A = {}"
```
```   461   using surj_image_vimage_eq [OF \<open>surj f\<close>, of A]
```
```   462   by (intro iffI) fastforce+
```
```   463
```
```   464 lemma inj_vimage_image_eq: "inj f \<Longrightarrow> f -` (f ` A) = A"
```
```   465   unfolding inj_def by blast
```
```   466
```
```   467 lemma vimage_subsetD: "surj f \<Longrightarrow> f -` B \<subseteq> A \<Longrightarrow> B \<subseteq> f ` A"
```
```   468   by (blast intro: sym)
```
```   469
```
```   470 lemma vimage_subsetI: "inj f \<Longrightarrow> B \<subseteq> f ` A \<Longrightarrow> f -` B \<subseteq> A"
```
```   471   unfolding inj_def by blast
```
```   472
```
```   473 lemma vimage_subset_eq: "bij f \<Longrightarrow> f -` B \<subseteq> A \<longleftrightarrow> B \<subseteq> f ` A"
```
```   474   unfolding bij_def by (blast del: subsetI intro: vimage_subsetI vimage_subsetD)
```
```   475
```
```   476 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"
```
```   477   by (fastforce simp: inj_on_def)
```
```   478
```
```   479 lemma inj_on_Un_image_eq_iff: "inj_on f (A \<union> B) \<Longrightarrow> f ` A = f ` B \<longleftrightarrow> A = B"
```
```   480   by (erule inj_on_image_eq_iff) simp_all
```
```   481
```
```   482 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"
```
```   483   unfolding inj_on_def by blast
```
```   484
```
```   485 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"
```
```   486   unfolding inj_on_def by blast
```
```   487
```
```   488 lemma image_Int: "inj f \<Longrightarrow> f ` (A \<inter> B) = f ` A \<inter> f ` B"
```
```   489   unfolding inj_def by blast
```
```   490
```
```   491 lemma image_set_diff: "inj f \<Longrightarrow> f ` (A - B) = f ` A - f ` B"
```
```   492   unfolding inj_def by blast
```
```   493
```
```   494 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"
```
```   495   by (auto simp: inj_on_def)
```
```   496
```
```   497 (*FIXME DELETE*)
```
```   498 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"
```
```   499   by (blast dest: inj_onD)
```
```   500
```
```   501 lemma inj_image_mem_iff: "inj f \<Longrightarrow> f a \<in> f ` A \<longleftrightarrow> a \<in> A"
```
```   502   by (blast dest: injD)
```
```   503
```
```   504 lemma inj_image_subset_iff: "inj f \<Longrightarrow> f ` A \<subseteq> f ` B \<longleftrightarrow> A \<subseteq> B"
```
```   505   by (blast dest: injD)
```
```   506
```
```   507 lemma inj_image_eq_iff: "inj f \<Longrightarrow> f ` A = f ` B \<longleftrightarrow> A = B"
```
```   508   by (blast dest: injD)
```
```   509
```
```   510 lemma surj_Compl_image_subset: "surj f \<Longrightarrow> - (f ` A) \<subseteq> f ` (- A)"
```
```   511   by auto
```
```   512
```
```   513 lemma inj_image_Compl_subset: "inj f \<Longrightarrow> f ` (- A) \<subseteq> - (f ` A)"
```
```   514   by (auto simp: inj_def)
```
```   515
```
```   516 lemma bij_image_Compl_eq: "bij f \<Longrightarrow> f ` (- A) = - (f ` A)"
```
```   517   by (simp add: bij_def inj_image_Compl_subset surj_Compl_image_subset equalityI)
```
```   518
```
```   519 lemma inj_vimage_singleton: "inj f \<Longrightarrow> f -` {a} \<subseteq> {THE x. f x = a}"
```
```   520   \<comment> \<open>The inverse image of a singleton under an injective function is included in a singleton.\<close>
```
```   521   by (simp add: inj_def) (blast intro: the_equality [symmetric])
```
```   522
```
```   523 lemma inj_on_vimage_singleton: "inj_on f A \<Longrightarrow> f -` {a} \<inter> A \<subseteq> {THE x. x \<in> A \<and> f x = a}"
```
```   524   by (auto simp add: inj_on_def intro: the_equality [symmetric])
```
```   525
```
```   526 lemma (in ordered_ab_group_add) inj_uminus[simp, intro]: "inj_on uminus A"
```
```   527   by (auto intro!: inj_onI)
```
```   528
```
```   529 lemma (in linorder) strict_mono_imp_inj_on: "strict_mono f \<Longrightarrow> inj_on f A"
```
```   530   by (auto intro!: inj_onI dest: strict_mono_eq)
```
```   531
```
```   532 lemma bij_betw_byWitness:
```
```   533   assumes left: "\<forall>a \<in> A. f' (f a) = a"
```
```   534     and right: "\<forall>a' \<in> A'. f (f' a') = a'"
```
```   535     and "f ` A \<subseteq> A'"
```
```   536     and img2: "f' ` A' \<subseteq> A"
```
```   537   shows "bij_betw f A A'"
```
```   538   using assms
```
```   539   unfolding bij_betw_def inj_on_def
```
```   540 proof safe
```
```   541   fix a b
```
```   542   assume "a \<in> A" "b \<in> A"
```
```   543   with left have "a = f' (f a) \<and> b = f' (f b)" by simp
```
```   544   moreover assume "f a = f b"
```
```   545   ultimately show "a = b" by simp
```
```   546 next
```
```   547   fix a' assume *: "a' \<in> A'"
```
```   548   with img2 have "f' a' \<in> A" by blast
```
```   549   moreover from * right have "a' = f (f' a')" by simp
```
```   550   ultimately show "a' \<in> f ` A" by blast
```
```   551 qed
```
```   552
```
```   553 corollary notIn_Un_bij_betw:
```
```   554   assumes "b \<notin> A"
```
```   555     and "f b \<notin> A'"
```
```   556     and "bij_betw f A A'"
```
```   557   shows "bij_betw f (A \<union> {b}) (A' \<union> {f b})"
```
```   558 proof -
```
```   559   have "bij_betw f {b} {f b}"
```
```   560     unfolding bij_betw_def inj_on_def by simp
```
```   561   with assms show ?thesis
```
```   562     using bij_betw_combine[of f A A' "{b}" "{f b}"] by blast
```
```   563 qed
```
```   564
```
```   565 lemma notIn_Un_bij_betw3:
```
```   566   assumes "b \<notin> A"
```
```   567     and "f b \<notin> A'"
```
```   568   shows "bij_betw f A A' = bij_betw f (A \<union> {b}) (A' \<union> {f b})"
```
```   569 proof
```
```   570   assume "bij_betw f A A'"
```
```   571   then show "bij_betw f (A \<union> {b}) (A' \<union> {f b})"
```
```   572     using assms notIn_Un_bij_betw [of b A f A'] by blast
```
```   573 next
```
```   574   assume *: "bij_betw f (A \<union> {b}) (A' \<union> {f b})"
```
```   575   have "f ` A = A'"
```
```   576   proof auto
```
```   577     fix a
```
```   578     assume **: "a \<in> A"
```
```   579     then have "f a \<in> A' \<union> {f b}"
```
```   580       using * unfolding bij_betw_def by blast
```
```   581     moreover
```
```   582     have False if "f a = f b"
```
```   583     proof -
```
```   584       have "a = b"
```
```   585         using * ** that unfolding bij_betw_def inj_on_def by blast
```
```   586       with \<open>b \<notin> A\<close> ** show ?thesis by blast
```
```   587     qed
```
```   588     ultimately show "f a \<in> A'" by blast
```
```   589   next
```
```   590     fix a'
```
```   591     assume **: "a' \<in> A'"
```
```   592     then have "a' \<in> f ` (A \<union> {b})"
```
```   593       using * by (auto simp add: bij_betw_def)
```
```   594     then obtain a where 1: "a \<in> A \<union> {b} \<and> f a = a'" by blast
```
```   595     moreover
```
```   596     have False if "a = b" using 1 ** \<open>f b \<notin> A'\<close> that by blast
```
```   597     ultimately have "a \<in> A" by blast
```
```   598     with 1 show "a' \<in> f ` A" by blast
```
```   599   qed
```
```   600   then show "bij_betw f A A'"
```
```   601     using * bij_betw_subset[of f "A \<union> {b}" _ A] by blast
```
```   602 qed
```
```   603
```
```   604 text \<open>Important examples\<close>
```
```   605
```
```   606 context cancel_semigroup_add
```
```   607 begin
```
```   608
```
```   609 lemma inj_on_add [simp]:
```
```   610   "inj_on ((+) a) A"
```
```   611   by (rule inj_onI) simp
```
```   612
```
```   613 lemma inj_add_left:
```
```   614   \<open>inj ((+) a)\<close>
```
```   615   by simp
```
```   616
```
```   617 lemma inj_on_add' [simp]:
```
```   618   "inj_on (\<lambda>b. b + a) A"
```
```   619   by (rule inj_onI) simp
```
```   620
```
```   621 lemma bij_betw_add [simp]:
```
```   622   "bij_betw ((+) a) A B \<longleftrightarrow> (+) a ` A = B"
```
```   623   by (simp add: bij_betw_def)
```
```   624
```
```   625 end
```
```   626
```
```   627 context ab_group_add
```
```   628 begin
```
```   629
```
```   630 lemma surj_plus [simp]:
```
```   631   "surj ((+) a)"
```
```   632   by (auto intro!: range_eqI [of b "(+) a" "b - a" for b]) (simp add: algebra_simps)
```
```   633
```
```   634 lemma inj_diff_right [simp]:
```
```   635   \<open>inj (\<lambda>b. b - a)\<close>
```
```   636 proof -
```
```   637   have \<open>inj ((+) (- a))\<close>
```
```   638     by (fact inj_add_left)
```
```   639   also have \<open>(+) (- a) = (\<lambda>b. b - a)\<close>
```
```   640     by (simp add: fun_eq_iff)
```
```   641   finally show ?thesis .
```
```   642 qed
```
```   643
```
```   644 lemma surj_diff_right [simp]:
```
```   645   "surj (\<lambda>x. x - a)"
```
```   646   using surj_plus [of "- a"] by (simp cong: image_cong_simp)
```
```   647
```
```   648 lemma translation_Compl:
```
```   649   "(+) a ` (- t) = - ((+) a ` t)"
```
```   650 proof (rule set_eqI)
```
```   651   fix b
```
```   652   show "b \<in> (+) a ` (- t) \<longleftrightarrow> b \<in> - (+) a ` t"
```
```   653     by (auto simp: image_iff algebra_simps intro!: bexI [of _ "b - a"])
```
```   654 qed
```
```   655
```
```   656 lemma translation_subtract_Compl:
```
```   657   "(\<lambda>x. x - a) ` (- t) = - ((\<lambda>x. x - a) ` t)"
```
```   658   using translation_Compl [of "- a" t] by (simp cong: image_cong_simp)
```
```   659
```
```   660 lemma translation_diff:
```
```   661   "(+) a ` (s - t) = ((+) a ` s) - ((+) a ` t)"
```
```   662   by auto
```
```   663
```
```   664 lemma translation_subtract_diff:
```
```   665   "(\<lambda>x. x - a) ` (s - t) = ((\<lambda>x. x - a) ` s) - ((\<lambda>x. x - a) ` t)"
```
```   666   using translation_diff [of "- a"] by (simp cong: image_cong_simp)
```
```   667
```
```   668 lemma translation_Int:
```
```   669   "(+) a ` (s \<inter> t) = ((+) a ` s) \<inter> ((+) a ` t)"
```
```   670   by auto
```
```   671
```
```   672 lemma translation_subtract_Int:
```
```   673   "(\<lambda>x. x - a) ` (s \<inter> t) = ((\<lambda>x. x - a) ` s) \<inter> ((\<lambda>x. x - a) ` t)"
```
```   674   using translation_Int [of " -a"] by (simp cong: image_cong_simp)
```
```   675
```
```   676 end
```
```   677
```
```   678
```
```   679 subsection \<open>Function Updating\<close>
```
```   680
```
```   681 definition fun_upd :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> ('a \<Rightarrow> 'b)"
```
```   682   where "fun_upd f a b = (\<lambda>x. if x = a then b else f x)"
```
```   683
```
```   684 nonterminal updbinds and updbind
```
```   685
```
```   686 syntax
```
```   687   "_updbind" :: "'a \<Rightarrow> 'a \<Rightarrow> updbind"             ("(2_ :=/ _)")
```
```   688   ""         :: "updbind \<Rightarrow> updbinds"             ("_")
```
```   689   "_updbinds":: "updbind \<Rightarrow> updbinds \<Rightarrow> updbinds" ("_,/ _")
```
```   690   "_Update"  :: "'a \<Rightarrow> updbinds \<Rightarrow> 'a"            ("_/'((_)')" [1000, 0] 900)
```
```   691
```
```   692 translations
```
```   693   "_Update f (_updbinds b bs)" \<rightleftharpoons> "_Update (_Update f b) bs"
```
```   694   "f(x:=y)" \<rightleftharpoons> "CONST fun_upd f x y"
```
```   695
```
```   696 (* Hint: to define the sum of two functions (or maps), use case_sum.
```
```   697          A nice infix syntax could be defined by
```
```   698 notation
```
```   699   case_sum  (infixr "'(+')"80)
```
```   700 *)
```
```   701
```
```   702 lemma fun_upd_idem_iff: "f(x:=y) = f \<longleftrightarrow> f x = y"
```
```   703   unfolding fun_upd_def
```
```   704   apply safe
```
```   705    apply (erule subst)
```
```   706    apply (rule_tac [2] ext)
```
```   707    apply auto
```
```   708   done
```
```   709
```
```   710 lemma fun_upd_idem: "f x = y \<Longrightarrow> f(x := y) = f"
```
```   711   by (simp only: fun_upd_idem_iff)
```
```   712
```
```   713 lemma fun_upd_triv [iff]: "f(x := f x) = f"
```
```   714   by (simp only: fun_upd_idem)
```
```   715
```
```   716 lemma fun_upd_apply [simp]: "(f(x := y)) z = (if z = x then y else f z)"
```
```   717   by (simp add: fun_upd_def)
```
```   718
```
```   719 (* fun_upd_apply supersedes these two, but they are useful
```
```   720    if fun_upd_apply is intentionally removed from the simpset *)
```
```   721 lemma fun_upd_same: "(f(x := y)) x = y"
```
```   722   by simp
```
```   723
```
```   724 lemma fun_upd_other: "z \<noteq> x \<Longrightarrow> (f(x := y)) z = f z"
```
```   725   by simp
```
```   726
```
```   727 lemma fun_upd_upd [simp]: "f(x := y, x := z) = f(x := z)"
```
```   728   by (simp add: fun_eq_iff)
```
```   729
```
```   730 lemma fun_upd_twist: "a \<noteq> c \<Longrightarrow> (m(a := b))(c := d) = (m(c := d))(a := b)"
```
```   731   by (rule ext) auto
```
```   732
```
```   733 lemma inj_on_fun_updI: "inj_on f A \<Longrightarrow> y \<notin> f ` A \<Longrightarrow> inj_on (f(x := y)) A"
```
```   734   by (auto simp: inj_on_def)
```
```   735
```
```   736 lemma fun_upd_image: "f(x := y) ` A = (if x \<in> A then insert y (f ` (A - {x})) else f ` A)"
```
```   737   by auto
```
```   738
```
```   739 lemma fun_upd_comp: "f \<circ> (g(x := y)) = (f \<circ> g)(x := f y)"
```
```   740   by auto
```
```   741
```
```   742 lemma fun_upd_eqD: "f(x := y) = g(x := z) \<Longrightarrow> y = z"
```
```   743   by (simp add: fun_eq_iff split: if_split_asm)
```
```   744
```
```   745
```
```   746 subsection \<open>\<open>override_on\<close>\<close>
```
```   747
```
```   748 definition override_on :: "('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'a set \<Rightarrow> 'a \<Rightarrow> 'b"
```
```   749   where "override_on f g A = (\<lambda>a. if a \<in> A then g a else f a)"
```
```   750
```
```   751 lemma override_on_emptyset[simp]: "override_on f g {} = f"
```
```   752   by (simp add: override_on_def)
```
```   753
```
```   754 lemma override_on_apply_notin[simp]: "a \<notin> A \<Longrightarrow> (override_on f g A) a = f a"
```
```   755   by (simp add: override_on_def)
```
```   756
```
```   757 lemma override_on_apply_in[simp]: "a \<in> A \<Longrightarrow> (override_on f g A) a = g a"
```
```   758   by (simp add: override_on_def)
```
```   759
```
```   760 lemma override_on_insert: "override_on f g (insert x X) = (override_on f g X)(x:=g x)"
```
```   761   by (simp add: override_on_def fun_eq_iff)
```
```   762
```
```   763 lemma override_on_insert': "override_on f g (insert x X) = (override_on (f(x:=g x)) g X)"
```
```   764   by (simp add: override_on_def fun_eq_iff)
```
```   765
```
```   766
```
```   767 subsection \<open>\<open>swap\<close>\<close>
```
```   768
```
```   769 definition swap :: "'a \<Rightarrow> 'a \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b)"
```
```   770   where "swap a b f = f (a := f b, b:= f a)"
```
```   771
```
```   772 lemma swap_apply [simp]:
```
```   773   "swap a b f a = f b"
```
```   774   "swap a b f b = f a"
```
```   775   "c \<noteq> a \<Longrightarrow> c \<noteq> b \<Longrightarrow> swap a b f c = f c"
```
```   776   by (simp_all add: swap_def)
```
```   777
```
```   778 lemma swap_self [simp]: "swap a a f = f"
```
```   779   by (simp add: swap_def)
```
```   780
```
```   781 lemma swap_commute: "swap a b f = swap b a f"
```
```   782   by (simp add: fun_upd_def swap_def fun_eq_iff)
```
```   783
```
```   784 lemma swap_nilpotent [simp]: "swap a b (swap a b f) = f"
```
```   785   by (rule ext) (simp add: fun_upd_def swap_def)
```
```   786
```
```   787 lemma swap_comp_involutory [simp]: "swap a b \<circ> swap a b = id"
```
```   788   by (rule ext) simp
```
```   789
```
```   790 lemma swap_triple:
```
```   791   assumes "a \<noteq> c" and "b \<noteq> c"
```
```   792   shows "swap a b (swap b c (swap a b f)) = swap a c f"
```
```   793   using assms by (simp add: fun_eq_iff swap_def)
```
```   794
```
```   795 lemma comp_swap: "f \<circ> swap a b g = swap a b (f \<circ> g)"
```
```   796   by (rule ext) (simp add: fun_upd_def swap_def)
```
```   797
```
```   798 lemma swap_image_eq [simp]:
```
```   799   assumes "a \<in> A" "b \<in> A"
```
```   800   shows "swap a b f ` A = f ` A"
```
```   801 proof -
```
```   802   have subset: "\<And>f. swap a b f ` A \<subseteq> f ` A"
```
```   803     using assms by (auto simp: image_iff swap_def)
```
```   804   then have "swap a b (swap a b f) ` A \<subseteq> (swap a b f) ` A" .
```
```   805   with subset[of f] show ?thesis by auto
```
```   806 qed
```
```   807
```
```   808 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"
```
```   809   by (auto simp add: inj_on_def swap_def)
```
```   810
```
```   811 lemma inj_on_swap_iff [simp]:
```
```   812   assumes A: "a \<in> A" "b \<in> A"
```
```   813   shows "inj_on (swap a b f) A \<longleftrightarrow> inj_on f A"
```
```   814 proof
```
```   815   assume "inj_on (swap a b f) A"
```
```   816   with A have "inj_on (swap a b (swap a b f)) A"
```
```   817     by (iprover intro: inj_on_imp_inj_on_swap)
```
```   818   then show "inj_on f A" by simp
```
```   819 next
```
```   820   assume "inj_on f A"
```
```   821   with A show "inj_on (swap a b f) A"
```
```   822     by (iprover intro: inj_on_imp_inj_on_swap)
```
```   823 qed
```
```   824
```
```   825 lemma surj_imp_surj_swap: "surj f \<Longrightarrow> surj (swap a b f)"
```
```   826   by simp
```
```   827
```
```   828 lemma surj_swap_iff [simp]: "surj (swap a b f) \<longleftrightarrow> surj f"
```
```   829   by simp
```
```   830
```
```   831 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"
```
```   832   by (auto simp: bij_betw_def)
```
```   833
```
```   834 lemma bij_swap_iff [simp]: "bij (swap a b f) \<longleftrightarrow> bij f"
```
```   835   by simp
```
```   836
```
```   837 hide_const (open) swap
```
```   838
```
```   839
```
```   840 subsection \<open>Inversion of injective functions\<close>
```
```   841
```
```   842 definition the_inv_into :: "'a set \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('b \<Rightarrow> 'a)"
```
```   843   where "the_inv_into A f = (\<lambda>x. THE y. y \<in> A \<and> f y = x)"
```
```   844
```
```   845 lemma the_inv_into_f_f: "inj_on f A \<Longrightarrow> x \<in> A \<Longrightarrow> the_inv_into A f (f x) = x"
```
```   846   unfolding the_inv_into_def inj_on_def by blast
```
```   847
```
```   848 lemma f_the_inv_into_f: "inj_on f A \<Longrightarrow> y \<in> f ` A  \<Longrightarrow> f (the_inv_into A f y) = y"
```
```   849   apply (simp add: the_inv_into_def)
```
```   850   apply (rule the1I2)
```
```   851    apply (blast dest: inj_onD)
```
```   852   apply blast
```
```   853   done
```
```   854
```
```   855 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"
```
```   856   apply (simp add: the_inv_into_def)
```
```   857   apply (rule the1I2)
```
```   858    apply (blast dest: inj_onD)
```
```   859   apply blast
```
```   860   done
```
```   861
```
```   862 lemma the_inv_into_onto [simp]: "inj_on f A \<Longrightarrow> the_inv_into A f ` (f ` A) = A"
```
```   863   by (fast intro: the_inv_into_into the_inv_into_f_f [symmetric])
```
```   864
```
```   865 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"
```
```   866   apply (erule subst)
```
```   867   apply (erule the_inv_into_f_f)
```
```   868   apply assumption
```
```   869   done
```
```   870
```
```   871 lemma the_inv_into_comp:
```
```   872   "inj_on f (g ` A) \<Longrightarrow> inj_on g A \<Longrightarrow> x \<in> f ` g ` A \<Longrightarrow>
```
```   873     the_inv_into A (f \<circ> g) x = (the_inv_into A g \<circ> the_inv_into (g ` A) f) x"
```
```   874   apply (rule the_inv_into_f_eq)
```
```   875     apply (fast intro: comp_inj_on)
```
```   876    apply (simp add: f_the_inv_into_f the_inv_into_into)
```
```   877   apply (simp add: the_inv_into_into)
```
```   878   done
```
```   879
```
```   880 lemma inj_on_the_inv_into: "inj_on f A \<Longrightarrow> inj_on (the_inv_into A f) (f ` A)"
```
```   881   by (auto intro: inj_onI simp: the_inv_into_f_f)
```
```   882
```
```   883 lemma bij_betw_the_inv_into: "bij_betw f A B \<Longrightarrow> bij_betw (the_inv_into A f) B A"
```
```   884   by (auto simp add: bij_betw_def inj_on_the_inv_into the_inv_into_into)
```
```   885
```
```   886 abbreviation the_inv :: "('a \<Rightarrow> 'b) \<Rightarrow> ('b \<Rightarrow> 'a)"
```
```   887   where "the_inv f \<equiv> the_inv_into UNIV f"
```
```   888
```
```   889 lemma the_inv_f_f: "the_inv f (f x) = x" if "inj f"
```
```   890   using that UNIV_I by (rule the_inv_into_f_f)
```
```   891
```
```   892
```
```   893 subsection \<open>Cantor's Paradox\<close>
```
```   894
```
```   895 theorem Cantors_paradox: "\<nexists>f. f ` A = Pow A"
```
```   896 proof
```
```   897   assume "\<exists>f. f ` A = Pow A"
```
```   898   then obtain f where f: "f ` A = Pow A" ..
```
```   899   let ?X = "{a \<in> A. a \<notin> f a}"
```
```   900   have "?X \<in> Pow A" by blast
```
```   901   then have "?X \<in> f ` A" by (simp only: f)
```
```   902   then obtain x where "x \<in> A" and "f x = ?X" by blast
```
```   903   then show False by blast
```
```   904 qed
```
```   905
```
```   906
```
```   907 subsection \<open>Setup\<close>
```
```   908
```
```   909 subsubsection \<open>Proof tools\<close>
```
```   910
```
```   911 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>
```
```   912
```
```   913 simproc_setup fun_upd2 ("f(v := w, x := y)") = \<open>fn _ =>
```
```   914   let
```
```   915     fun gen_fun_upd NONE T _ _ = NONE
```
```   916       | gen_fun_upd (SOME f) T x y = SOME (Const (\<^const_name>\<open>fun_upd\<close>, T) \$ f \$ x \$ y)
```
```   917     fun dest_fun_T1 (Type (_, T :: Ts)) = T
```
```   918     fun find_double (t as Const (\<^const_name>\<open>fun_upd\<close>,T) \$ f \$ x \$ y) =
```
```   919       let
```
```   920         fun find (Const (\<^const_name>\<open>fun_upd\<close>,T) \$ g \$ v \$ w) =
```
```   921               if v aconv x then SOME g else gen_fun_upd (find g) T v w
```
```   922           | find t = NONE
```
```   923       in (dest_fun_T1 T, gen_fun_upd (find f) T x y) end
```
```   924
```
```   925     val ss = simpset_of \<^context>
```
```   926
```
```   927     fun proc ctxt ct =
```
```   928       let
```
```   929         val t = Thm.term_of ct
```
```   930       in
```
```   931         (case find_double t of
```
```   932           (T, NONE) => NONE
```
```   933         | (T, SOME rhs) =>
```
```   934             SOME (Goal.prove ctxt [] [] (Logic.mk_equals (t, rhs))
```
```   935               (fn _ =>
```
```   936                 resolve_tac ctxt [eq_reflection] 1 THEN
```
```   937                 resolve_tac ctxt @{thms ext} 1 THEN
```
```   938                 simp_tac (put_simpset ss ctxt) 1)))
```
```   939       end
```
```   940   in proc end
```
```   941 \<close>
```
```   942
```
```   943
```
```   944 subsubsection \<open>Functorial structure of types\<close>
```
```   945
```
```   946 ML_file \<open>Tools/functor.ML\<close>
```
```   947
```
```   948 functor map_fun: map_fun
```
```   949   by (simp_all add: fun_eq_iff)
```
```   950
```
```   951 functor vimage
```
```   952   by (simp_all add: fun_eq_iff vimage_comp)
```
```   953
```
```   954
```
```   955 text \<open>Legacy theorem names\<close>
```
```   956
```
```   957 lemmas o_def = comp_def
```
```   958 lemmas o_apply = comp_apply
```
```   959 lemmas o_assoc = comp_assoc [symmetric]
```
```   960 lemmas id_o = id_comp
```
```   961 lemmas o_id = comp_id
```
```   962 lemmas o_eq_dest = comp_eq_dest
```
```   963 lemmas o_eq_elim = comp_eq_elim
```
```   964 lemmas o_eq_dest_lhs = comp_eq_dest_lhs
```
```   965 lemmas o_eq_id_dest = comp_eq_id_dest
```
```   966
```
```   967 end
```