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