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