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