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