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