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