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