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