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