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