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