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