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