src/HOL/Fun.thy
author hoelzl
Mon Nov 22 10:34:33 2010 +0100 (2010-11-22)
changeset 40702 cf26dd7395e4
parent 40602 91e583511113
child 40703 d1fc454d6735
permissions -rw-r--r--
Replace surj by abbreviation; remove surj_on.
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
haftmann@38620
   172
lemma inj_comp:
haftmann@38620
   173
  "inj f \<Longrightarrow> inj g \<Longrightarrow> inj (f \<circ> g)"
haftmann@38620
   174
  by (simp add: inj_on_def)
haftmann@38620
   175
haftmann@38620
   176
lemma inj_fun: "inj f \<Longrightarrow> inj (\<lambda>x y. f x)"
nipkow@39302
   177
  by (simp add: inj_on_def fun_eq_iff)
haftmann@38620
   178
nipkow@32988
   179
lemma inj_eq: "inj f ==> (f(x) = f(y)) = (x=y)"
nipkow@32988
   180
by (simp add: inj_on_eq_iff)
nipkow@32988
   181
haftmann@26147
   182
lemma inj_on_id[simp]: "inj_on id A"
hoelzl@39076
   183
  by (simp add: inj_on_def)
paulson@13585
   184
haftmann@26147
   185
lemma inj_on_id2[simp]: "inj_on (%x. x) A"
hoelzl@39076
   186
by (simp add: inj_on_def)
haftmann@26147
   187
hoelzl@40702
   188
lemma surj_id: "surj id"
hoelzl@40702
   189
by simp
haftmann@26147
   190
hoelzl@39101
   191
lemma bij_id[simp]: "bij id"
hoelzl@39076
   192
by (simp add: bij_betw_def)
paulson@13585
   193
paulson@13585
   194
lemma inj_onI:
paulson@13585
   195
    "(!! x y. [|  x:A;  y:A;  f(x) = f(y) |] ==> x=y) ==> inj_on f A"
paulson@13585
   196
by (simp add: inj_on_def)
paulson@13585
   197
paulson@13585
   198
lemma inj_on_inverseI: "(!!x. x:A ==> g(f(x)) = x) ==> inj_on f A"
paulson@13585
   199
by (auto dest:  arg_cong [of concl: g] simp add: inj_on_def)
paulson@13585
   200
paulson@13585
   201
lemma inj_onD: "[| inj_on f A;  f(x)=f(y);  x:A;  y:A |] ==> x=y"
paulson@13585
   202
by (unfold inj_on_def, blast)
paulson@13585
   203
paulson@13585
   204
lemma inj_on_iff: "[| inj_on f A;  x:A;  y:A |] ==> (f(x)=f(y)) = (x=y)"
paulson@13585
   205
by (blast dest!: inj_onD)
paulson@13585
   206
paulson@13585
   207
lemma comp_inj_on:
paulson@13585
   208
     "[| inj_on f A;  inj_on g (f`A) |] ==> inj_on (g o f) A"
paulson@13585
   209
by (simp add: comp_def inj_on_def)
paulson@13585
   210
nipkow@15303
   211
lemma inj_on_imageI: "inj_on (g o f) A \<Longrightarrow> inj_on g (f ` A)"
nipkow@15303
   212
apply(simp add:inj_on_def image_def)
nipkow@15303
   213
apply blast
nipkow@15303
   214
done
nipkow@15303
   215
nipkow@15439
   216
lemma inj_on_image_iff: "\<lbrakk> ALL x:A. ALL y:A. (g(f x) = g(f y)) = (g x = g y);
nipkow@15439
   217
  inj_on f A \<rbrakk> \<Longrightarrow> inj_on g (f ` A) = inj_on g A"
nipkow@15439
   218
apply(unfold inj_on_def)
nipkow@15439
   219
apply blast
nipkow@15439
   220
done
nipkow@15439
   221
paulson@13585
   222
lemma inj_on_contraD: "[| inj_on f A;  ~x=y;  x:A;  y:A |] ==> ~ f(x)=f(y)"
paulson@13585
   223
by (unfold inj_on_def, blast)
wenzelm@12258
   224
paulson@13585
   225
lemma inj_singleton: "inj (%s. {s})"
paulson@13585
   226
by (simp add: inj_on_def)
paulson@13585
   227
nipkow@15111
   228
lemma inj_on_empty[iff]: "inj_on f {}"
nipkow@15111
   229
by(simp add: inj_on_def)
nipkow@15111
   230
nipkow@15303
   231
lemma subset_inj_on: "[| inj_on f B; A <= B |] ==> inj_on f A"
paulson@13585
   232
by (unfold inj_on_def, blast)
paulson@13585
   233
nipkow@15111
   234
lemma inj_on_Un:
nipkow@15111
   235
 "inj_on f (A Un B) =
nipkow@15111
   236
  (inj_on f A & inj_on f B & f`(A-B) Int f`(B-A) = {})"
nipkow@15111
   237
apply(unfold inj_on_def)
nipkow@15111
   238
apply (blast intro:sym)
nipkow@15111
   239
done
nipkow@15111
   240
nipkow@15111
   241
lemma inj_on_insert[iff]:
nipkow@15111
   242
  "inj_on f (insert a A) = (inj_on f A & f a ~: f`(A-{a}))"
nipkow@15111
   243
apply(unfold inj_on_def)
nipkow@15111
   244
apply (blast intro:sym)
nipkow@15111
   245
done
nipkow@15111
   246
nipkow@15111
   247
lemma inj_on_diff: "inj_on f A ==> inj_on f (A-B)"
nipkow@15111
   248
apply(unfold inj_on_def)
nipkow@15111
   249
apply (blast)
nipkow@15111
   250
done
nipkow@15111
   251
hoelzl@40702
   252
lemma surj_def: "surj f \<longleftrightarrow> (\<forall>y. \<exists>x. y = f x)"
hoelzl@40702
   253
  by auto
hoelzl@39076
   254
hoelzl@40702
   255
lemma surjI: assumes *: "\<And> x. g (f x) = x" shows "surj g"
hoelzl@40702
   256
  using *[symmetric] by auto
paulson@13585
   257
hoelzl@39076
   258
lemma surjD: "surj f \<Longrightarrow> \<exists>x. y = f x"
hoelzl@39076
   259
  by (simp add: surj_def)
paulson@13585
   260
hoelzl@39076
   261
lemma surjE: "surj f \<Longrightarrow> (\<And>x. y = f x \<Longrightarrow> C) \<Longrightarrow> C"
hoelzl@39076
   262
  by (simp add: surj_def, blast)
paulson@13585
   263
paulson@13585
   264
lemma comp_surj: "[| surj f;  surj g |] ==> surj (g o f)"
paulson@13585
   265
apply (simp add: comp_def surj_def, clarify)
paulson@13585
   266
apply (drule_tac x = y in spec, clarify)
paulson@13585
   267
apply (drule_tac x = x in spec, blast)
paulson@13585
   268
done
paulson@13585
   269
hoelzl@39074
   270
lemma bij_betw_imp_surj: "bij_betw f A UNIV \<Longrightarrow> surj f"
hoelzl@40702
   271
  unfolding bij_betw_def by auto
hoelzl@39074
   272
hoelzl@39076
   273
lemma bij_def: "bij f \<longleftrightarrow> inj f \<and> surj f"
hoelzl@40702
   274
  unfolding bij_betw_def ..
hoelzl@39074
   275
paulson@13585
   276
lemma bijI: "[| inj f; surj f |] ==> bij f"
paulson@13585
   277
by (simp add: bij_def)
paulson@13585
   278
paulson@13585
   279
lemma bij_is_inj: "bij f ==> inj f"
paulson@13585
   280
by (simp add: bij_def)
paulson@13585
   281
paulson@13585
   282
lemma bij_is_surj: "bij f ==> surj f"
paulson@13585
   283
by (simp add: bij_def)
paulson@13585
   284
nipkow@26105
   285
lemma bij_betw_imp_inj_on: "bij_betw f A B \<Longrightarrow> inj_on f A"
nipkow@26105
   286
by (simp add: bij_betw_def)
nipkow@26105
   287
nipkow@31438
   288
lemma bij_betw_trans:
nipkow@31438
   289
  "bij_betw f A B \<Longrightarrow> bij_betw g B C \<Longrightarrow> bij_betw (g o f) A C"
nipkow@31438
   290
by(auto simp add:bij_betw_def comp_inj_on)
nipkow@31438
   291
hoelzl@40702
   292
lemma bij_comp: "bij f \<Longrightarrow> bij g \<Longrightarrow> bij (g o f)"
hoelzl@40702
   293
  by (rule bij_betw_trans)
hoelzl@40702
   294
nipkow@26105
   295
lemma bij_betw_inv: assumes "bij_betw f A B" shows "EX g. bij_betw g B A"
nipkow@26105
   296
proof -
nipkow@26105
   297
  have i: "inj_on f A" and s: "f ` A = B"
nipkow@26105
   298
    using assms by(auto simp:bij_betw_def)
nipkow@26105
   299
  let ?P = "%b a. a:A \<and> f a = b" let ?g = "%b. The (?P b)"
nipkow@26105
   300
  { fix a b assume P: "?P b a"
nipkow@26105
   301
    hence ex1: "\<exists>a. ?P b a" using s unfolding image_def by blast
nipkow@26105
   302
    hence uex1: "\<exists>!a. ?P b a" by(blast dest:inj_onD[OF i])
nipkow@26105
   303
    hence " ?g b = a" using the1_equality[OF uex1, OF P] P by simp
nipkow@26105
   304
  } note g = this
nipkow@26105
   305
  have "inj_on ?g B"
nipkow@26105
   306
  proof(rule inj_onI)
nipkow@26105
   307
    fix x y assume "x:B" "y:B" "?g x = ?g y"
nipkow@26105
   308
    from s `x:B` obtain a1 where a1: "?P x a1" unfolding image_def by blast
nipkow@26105
   309
    from s `y:B` obtain a2 where a2: "?P y a2" unfolding image_def by blast
nipkow@26105
   310
    from g[OF a1] a1 g[OF a2] a2 `?g x = ?g y` show "x=y" by simp
nipkow@26105
   311
  qed
nipkow@26105
   312
  moreover have "?g ` B = A"
nipkow@26105
   313
  proof(auto simp:image_def)
nipkow@26105
   314
    fix b assume "b:B"
nipkow@26105
   315
    with s obtain a where P: "?P b a" unfolding image_def by blast
nipkow@26105
   316
    thus "?g b \<in> A" using g[OF P] by auto
nipkow@26105
   317
  next
nipkow@26105
   318
    fix a assume "a:A"
nipkow@26105
   319
    then obtain b where P: "?P b a" using s unfolding image_def by blast
nipkow@26105
   320
    then have "b:B" using s unfolding image_def by blast
nipkow@26105
   321
    with g[OF P] show "\<exists>b\<in>B. a = ?g b" by blast
nipkow@26105
   322
  qed
nipkow@26105
   323
  ultimately show ?thesis by(auto simp:bij_betw_def)
nipkow@26105
   324
qed
nipkow@26105
   325
hoelzl@39075
   326
lemma bij_betw_combine:
hoelzl@39075
   327
  assumes "bij_betw f A B" "bij_betw f C D" "B \<inter> D = {}"
hoelzl@39075
   328
  shows "bij_betw f (A \<union> C) (B \<union> D)"
hoelzl@39075
   329
  using assms unfolding bij_betw_def inj_on_Un image_Un by auto
hoelzl@39075
   330
paulson@13585
   331
lemma surj_image_vimage_eq: "surj f ==> f ` (f -` A) = A"
hoelzl@40702
   332
by simp
paulson@13585
   333
paulson@13585
   334
lemma inj_vimage_image_eq: "inj f ==> f -` (f ` A) = A"
paulson@13585
   335
by (simp add: inj_on_def, blast)
paulson@13585
   336
paulson@13585
   337
lemma vimage_subsetD: "surj f ==> f -` B <= A ==> B <= f ` A"
hoelzl@40702
   338
by (blast intro: sym)
paulson@13585
   339
paulson@13585
   340
lemma vimage_subsetI: "inj f ==> B <= f ` A ==> f -` B <= A"
paulson@13585
   341
by (unfold inj_on_def, blast)
paulson@13585
   342
paulson@13585
   343
lemma vimage_subset_eq: "bij f ==> (f -` B <= A) = (B <= f ` A)"
paulson@13585
   344
apply (unfold bij_def)
paulson@13585
   345
apply (blast del: subsetI intro: vimage_subsetI vimage_subsetD)
paulson@13585
   346
done
paulson@13585
   347
nipkow@31438
   348
lemma inj_on_Un_image_eq_iff: "inj_on f (A \<union> B) \<Longrightarrow> f ` A = f ` B \<longleftrightarrow> A = B"
nipkow@31438
   349
by(blast dest: inj_onD)
nipkow@31438
   350
paulson@13585
   351
lemma inj_on_image_Int:
paulson@13585
   352
   "[| inj_on f C;  A<=C;  B<=C |] ==> f`(A Int B) = f`A Int f`B"
paulson@13585
   353
apply (simp add: inj_on_def, blast)
paulson@13585
   354
done
paulson@13585
   355
paulson@13585
   356
lemma inj_on_image_set_diff:
paulson@13585
   357
   "[| inj_on f C;  A<=C;  B<=C |] ==> f`(A-B) = f`A - f`B"
paulson@13585
   358
apply (simp add: inj_on_def, blast)
paulson@13585
   359
done
paulson@13585
   360
paulson@13585
   361
lemma image_Int: "inj f ==> f`(A Int B) = f`A Int f`B"
paulson@13585
   362
by (simp add: inj_on_def, blast)
paulson@13585
   363
paulson@13585
   364
lemma image_set_diff: "inj f ==> f`(A-B) = f`A - f`B"
paulson@13585
   365
by (simp add: inj_on_def, blast)
paulson@13585
   366
paulson@13585
   367
lemma inj_image_mem_iff: "inj f ==> (f a : f`A) = (a : A)"
paulson@13585
   368
by (blast dest: injD)
paulson@13585
   369
paulson@13585
   370
lemma inj_image_subset_iff: "inj f ==> (f`A <= f`B) = (A<=B)"
paulson@13585
   371
by (simp add: inj_on_def, blast)
paulson@13585
   372
paulson@13585
   373
lemma inj_image_eq_iff: "inj f ==> (f`A = f`B) = (A = B)"
paulson@13585
   374
by (blast dest: injD)
paulson@13585
   375
paulson@13585
   376
(*injectivity's required.  Left-to-right inclusion holds even if A is empty*)
paulson@13585
   377
lemma image_INT:
paulson@13585
   378
   "[| inj_on f C;  ALL x:A. B x <= C;  j:A |]
paulson@13585
   379
    ==> f ` (INTER A B) = (INT x:A. f ` B x)"
paulson@13585
   380
apply (simp add: inj_on_def, blast)
paulson@13585
   381
done
paulson@13585
   382
paulson@13585
   383
(*Compare with image_INT: no use of inj_on, and if f is surjective then
paulson@13585
   384
  it doesn't matter whether A is empty*)
paulson@13585
   385
lemma bij_image_INT: "bij f ==> f ` (INTER A B) = (INT x:A. f ` B x)"
paulson@13585
   386
apply (simp add: bij_def)
paulson@13585
   387
apply (simp add: inj_on_def surj_def, blast)
paulson@13585
   388
done
paulson@13585
   389
paulson@13585
   390
lemma surj_Compl_image_subset: "surj f ==> -(f`A) <= f`(-A)"
hoelzl@40702
   391
by auto
paulson@13585
   392
paulson@13585
   393
lemma inj_image_Compl_subset: "inj f ==> f`(-A) <= -(f`A)"
paulson@13585
   394
by (auto simp add: inj_on_def)
paulson@5852
   395
paulson@13585
   396
lemma bij_image_Compl_eq: "bij f ==> f`(-A) = -(f`A)"
paulson@13585
   397
apply (simp add: bij_def)
paulson@13585
   398
apply (rule equalityI)
paulson@13585
   399
apply (simp_all (no_asm_simp) add: inj_image_Compl_subset surj_Compl_image_subset)
paulson@13585
   400
done
paulson@13585
   401
hoelzl@35584
   402
lemma (in ordered_ab_group_add) inj_uminus[simp, intro]: "inj_on uminus A"
hoelzl@35580
   403
  by (auto intro!: inj_onI)
paulson@13585
   404
hoelzl@35584
   405
lemma (in linorder) strict_mono_imp_inj_on: "strict_mono f \<Longrightarrow> inj_on f A"
hoelzl@35584
   406
  by (auto intro!: inj_onI dest: strict_mono_eq)
hoelzl@35584
   407
paulson@13585
   408
subsection{*Function Updating*}
paulson@13585
   409
haftmann@35416
   410
definition
haftmann@35416
   411
  fun_upd :: "('a => 'b) => 'a => 'b => ('a => 'b)" where
haftmann@26147
   412
  "fun_upd f a b == % x. if x=a then b else f x"
haftmann@26147
   413
haftmann@26147
   414
nonterminals
haftmann@26147
   415
  updbinds updbind
haftmann@26147
   416
syntax
haftmann@26147
   417
  "_updbind" :: "['a, 'a] => updbind"             ("(2_ :=/ _)")
haftmann@26147
   418
  ""         :: "updbind => updbinds"             ("_")
haftmann@26147
   419
  "_updbinds":: "[updbind, updbinds] => updbinds" ("_,/ _")
wenzelm@35115
   420
  "_Update"  :: "['a, updbinds] => 'a"            ("_/'((_)')" [1000, 0] 900)
haftmann@26147
   421
haftmann@26147
   422
translations
wenzelm@35115
   423
  "_Update f (_updbinds b bs)" == "_Update (_Update f b) bs"
wenzelm@35115
   424
  "f(x:=y)" == "CONST fun_upd f x y"
haftmann@26147
   425
haftmann@26147
   426
(* Hint: to define the sum of two functions (or maps), use sum_case.
haftmann@26147
   427
         A nice infix syntax could be defined (in Datatype.thy or below) by
wenzelm@35115
   428
notation
wenzelm@35115
   429
  sum_case  (infixr "'(+')"80)
haftmann@26147
   430
*)
haftmann@26147
   431
paulson@13585
   432
lemma fun_upd_idem_iff: "(f(x:=y) = f) = (f x = y)"
paulson@13585
   433
apply (simp add: fun_upd_def, safe)
paulson@13585
   434
apply (erule subst)
paulson@13585
   435
apply (rule_tac [2] ext, auto)
paulson@13585
   436
done
paulson@13585
   437
paulson@13585
   438
(* f x = y ==> f(x:=y) = f *)
paulson@13585
   439
lemmas fun_upd_idem = fun_upd_idem_iff [THEN iffD2, standard]
paulson@13585
   440
paulson@13585
   441
(* f(x := f x) = f *)
paulson@17084
   442
lemmas fun_upd_triv = refl [THEN fun_upd_idem]
paulson@17084
   443
declare fun_upd_triv [iff]
paulson@13585
   444
paulson@13585
   445
lemma fun_upd_apply [simp]: "(f(x:=y))z = (if z=x then y else f z)"
paulson@17084
   446
by (simp add: fun_upd_def)
paulson@13585
   447
paulson@13585
   448
(* fun_upd_apply supersedes these two,   but they are useful
paulson@13585
   449
   if fun_upd_apply is intentionally removed from the simpset *)
paulson@13585
   450
lemma fun_upd_same: "(f(x:=y)) x = y"
paulson@13585
   451
by simp
paulson@13585
   452
paulson@13585
   453
lemma fun_upd_other: "z~=x ==> (f(x:=y)) z = f z"
paulson@13585
   454
by simp
paulson@13585
   455
paulson@13585
   456
lemma fun_upd_upd [simp]: "f(x:=y,x:=z) = f(x:=z)"
nipkow@39302
   457
by (simp add: fun_eq_iff)
paulson@13585
   458
paulson@13585
   459
lemma fun_upd_twist: "a ~= c ==> (m(a:=b))(c:=d) = (m(c:=d))(a:=b)"
paulson@13585
   460
by (rule ext, auto)
paulson@13585
   461
nipkow@15303
   462
lemma inj_on_fun_updI: "\<lbrakk> inj_on f A; y \<notin> f`A \<rbrakk> \<Longrightarrow> inj_on (f(x:=y)) A"
krauss@34209
   463
by (fastsimp simp:inj_on_def image_def)
nipkow@15303
   464
paulson@15510
   465
lemma fun_upd_image:
paulson@15510
   466
     "f(x:=y) ` A = (if x \<in> A then insert y (f ` (A-{x})) else f ` A)"
paulson@15510
   467
by auto
paulson@15510
   468
nipkow@31080
   469
lemma fun_upd_comp: "f \<circ> (g(x := y)) = (f \<circ> g)(x := f y)"
krauss@34209
   470
by (auto intro: ext)
nipkow@31080
   471
haftmann@26147
   472
haftmann@26147
   473
subsection {* @{text override_on} *}
haftmann@26147
   474
haftmann@26147
   475
definition
haftmann@26147
   476
  override_on :: "('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'a set \<Rightarrow> 'a \<Rightarrow> 'b"
haftmann@26147
   477
where
haftmann@26147
   478
  "override_on f g A = (\<lambda>a. if a \<in> A then g a else f a)"
nipkow@13910
   479
nipkow@15691
   480
lemma override_on_emptyset[simp]: "override_on f g {} = f"
nipkow@15691
   481
by(simp add:override_on_def)
nipkow@13910
   482
nipkow@15691
   483
lemma override_on_apply_notin[simp]: "a ~: A ==> (override_on f g A) a = f a"
nipkow@15691
   484
by(simp add:override_on_def)
nipkow@13910
   485
nipkow@15691
   486
lemma override_on_apply_in[simp]: "a : A ==> (override_on f g A) a = g a"
nipkow@15691
   487
by(simp add:override_on_def)
nipkow@13910
   488
haftmann@26147
   489
haftmann@26147
   490
subsection {* @{text swap} *}
paulson@15510
   491
haftmann@22744
   492
definition
haftmann@22744
   493
  swap :: "'a \<Rightarrow> 'a \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b)"
haftmann@22744
   494
where
haftmann@22744
   495
  "swap a b f = f (a := f b, b:= f a)"
paulson@15510
   496
huffman@34101
   497
lemma swap_self [simp]: "swap a a f = f"
nipkow@15691
   498
by (simp add: swap_def)
paulson@15510
   499
paulson@15510
   500
lemma swap_commute: "swap a b f = swap b a f"
paulson@15510
   501
by (rule ext, simp add: fun_upd_def swap_def)
paulson@15510
   502
paulson@15510
   503
lemma swap_nilpotent [simp]: "swap a b (swap a b f) = f"
paulson@15510
   504
by (rule ext, simp add: fun_upd_def swap_def)
paulson@15510
   505
huffman@34145
   506
lemma swap_triple:
huffman@34145
   507
  assumes "a \<noteq> c" and "b \<noteq> c"
huffman@34145
   508
  shows "swap a b (swap b c (swap a b f)) = swap a c f"
nipkow@39302
   509
  using assms by (simp add: fun_eq_iff swap_def)
huffman@34145
   510
huffman@34101
   511
lemma comp_swap: "f \<circ> swap a b g = swap a b (f \<circ> g)"
huffman@34101
   512
by (rule ext, simp add: fun_upd_def swap_def)
huffman@34101
   513
hoelzl@39076
   514
lemma swap_image_eq [simp]:
hoelzl@39076
   515
  assumes "a \<in> A" "b \<in> A" shows "swap a b f ` A = f ` A"
hoelzl@39076
   516
proof -
hoelzl@39076
   517
  have subset: "\<And>f. swap a b f ` A \<subseteq> f ` A"
hoelzl@39076
   518
    using assms by (auto simp: image_iff swap_def)
hoelzl@39076
   519
  then have "swap a b (swap a b f) ` A \<subseteq> (swap a b f) ` A" .
hoelzl@39076
   520
  with subset[of f] show ?thesis by auto
hoelzl@39076
   521
qed
hoelzl@39076
   522
paulson@15510
   523
lemma inj_on_imp_inj_on_swap:
hoelzl@39076
   524
  "\<lbrakk>inj_on f A; a \<in> A; b \<in> A\<rbrakk> \<Longrightarrow> inj_on (swap a b f) A"
hoelzl@39076
   525
  by (simp add: inj_on_def swap_def, blast)
paulson@15510
   526
paulson@15510
   527
lemma inj_on_swap_iff [simp]:
hoelzl@39076
   528
  assumes A: "a \<in> A" "b \<in> A" shows "inj_on (swap a b f) A \<longleftrightarrow> inj_on f A"
hoelzl@39075
   529
proof
paulson@15510
   530
  assume "inj_on (swap a b f) A"
hoelzl@39075
   531
  with A have "inj_on (swap a b (swap a b f)) A"
hoelzl@39075
   532
    by (iprover intro: inj_on_imp_inj_on_swap)
hoelzl@39075
   533
  thus "inj_on f A" by simp
paulson@15510
   534
next
paulson@15510
   535
  assume "inj_on f A"
krauss@34209
   536
  with A show "inj_on (swap a b f) A" by (iprover intro: inj_on_imp_inj_on_swap)
paulson@15510
   537
qed
paulson@15510
   538
hoelzl@39076
   539
lemma surj_imp_surj_swap: "surj f \<Longrightarrow> surj (swap a b f)"
hoelzl@40702
   540
  by simp
paulson@15510
   541
hoelzl@39076
   542
lemma surj_swap_iff [simp]: "surj (swap a b f) \<longleftrightarrow> surj f"
hoelzl@40702
   543
  by simp
haftmann@21547
   544
hoelzl@39076
   545
lemma bij_betw_swap_iff [simp]:
hoelzl@39076
   546
  "\<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
   547
  by (auto simp: bij_betw_def)
hoelzl@39076
   548
hoelzl@39076
   549
lemma bij_swap_iff [simp]: "bij (swap a b f) \<longleftrightarrow> bij f"
hoelzl@39076
   550
  by simp
hoelzl@39075
   551
wenzelm@36176
   552
hide_const (open) swap
haftmann@21547
   553
haftmann@31949
   554
subsection {* Inversion of injective functions *}
haftmann@31949
   555
nipkow@33057
   556
definition the_inv_into :: "'a set => ('a => 'b) => ('b => 'a)" where
nipkow@33057
   557
"the_inv_into A f == %x. THE y. y : A & f y = x"
nipkow@32961
   558
nipkow@33057
   559
lemma the_inv_into_f_f:
nipkow@33057
   560
  "[| inj_on f A;  x : A |] ==> the_inv_into A f (f x) = x"
nipkow@33057
   561
apply (simp add: the_inv_into_def inj_on_def)
krauss@34209
   562
apply blast
nipkow@32961
   563
done
nipkow@32961
   564
nipkow@33057
   565
lemma f_the_inv_into_f:
nipkow@33057
   566
  "inj_on f A ==> y : f`A  ==> f (the_inv_into A f y) = y"
nipkow@33057
   567
apply (simp add: the_inv_into_def)
nipkow@32961
   568
apply (rule the1I2)
nipkow@32961
   569
 apply(blast dest: inj_onD)
nipkow@32961
   570
apply blast
nipkow@32961
   571
done
nipkow@32961
   572
nipkow@33057
   573
lemma the_inv_into_into:
nipkow@33057
   574
  "[| inj_on f A; x : f ` A; A <= B |] ==> the_inv_into A f x : B"
nipkow@33057
   575
apply (simp add: the_inv_into_def)
nipkow@32961
   576
apply (rule the1I2)
nipkow@32961
   577
 apply(blast dest: inj_onD)
nipkow@32961
   578
apply blast
nipkow@32961
   579
done
nipkow@32961
   580
nipkow@33057
   581
lemma the_inv_into_onto[simp]:
nipkow@33057
   582
  "inj_on f A ==> the_inv_into A f ` (f ` A) = A"
nipkow@33057
   583
by (fast intro:the_inv_into_into the_inv_into_f_f[symmetric])
nipkow@32961
   584
nipkow@33057
   585
lemma the_inv_into_f_eq:
nipkow@33057
   586
  "[| inj_on f A; f x = y; x : A |] ==> the_inv_into A f y = x"
nipkow@32961
   587
  apply (erule subst)
nipkow@33057
   588
  apply (erule the_inv_into_f_f, assumption)
nipkow@32961
   589
  done
nipkow@32961
   590
nipkow@33057
   591
lemma the_inv_into_comp:
nipkow@32961
   592
  "[| inj_on f (g ` A); inj_on g A; x : f ` g ` A |] ==>
nipkow@33057
   593
  the_inv_into A (f o g) x = (the_inv_into A g o the_inv_into (g ` A) f) x"
nipkow@33057
   594
apply (rule the_inv_into_f_eq)
nipkow@32961
   595
  apply (fast intro: comp_inj_on)
nipkow@33057
   596
 apply (simp add: f_the_inv_into_f the_inv_into_into)
nipkow@33057
   597
apply (simp add: the_inv_into_into)
nipkow@32961
   598
done
nipkow@32961
   599
nipkow@33057
   600
lemma inj_on_the_inv_into:
nipkow@33057
   601
  "inj_on f A \<Longrightarrow> inj_on (the_inv_into A f) (f ` A)"
nipkow@33057
   602
by (auto intro: inj_onI simp: image_def the_inv_into_f_f)
nipkow@32961
   603
nipkow@33057
   604
lemma bij_betw_the_inv_into:
nipkow@33057
   605
  "bij_betw f A B \<Longrightarrow> bij_betw (the_inv_into A f) B A"
nipkow@33057
   606
by (auto simp add: bij_betw_def inj_on_the_inv_into the_inv_into_into)
nipkow@32961
   607
berghofe@32998
   608
abbreviation the_inv :: "('a \<Rightarrow> 'b) \<Rightarrow> ('b \<Rightarrow> 'a)" where
nipkow@33057
   609
  "the_inv f \<equiv> the_inv_into UNIV f"
berghofe@32998
   610
berghofe@32998
   611
lemma the_inv_f_f:
berghofe@32998
   612
  assumes "inj f"
berghofe@32998
   613
  shows "the_inv f (f x) = x" using assms UNIV_I
nipkow@33057
   614
  by (rule the_inv_into_f_f)
berghofe@32998
   615
haftmann@31949
   616
haftmann@22845
   617
subsection {* Proof tool setup *} 
haftmann@22845
   618
haftmann@22845
   619
text {* simplifies terms of the form
haftmann@22845
   620
  f(...,x:=y,...,x:=z,...) to f(...,x:=z,...) *}
haftmann@22845
   621
wenzelm@24017
   622
simproc_setup fun_upd2 ("f(v := w, x := y)") = {* fn _ =>
haftmann@22845
   623
let
haftmann@22845
   624
  fun gen_fun_upd NONE T _ _ = NONE
wenzelm@24017
   625
    | gen_fun_upd (SOME f) T x y = SOME (Const (@{const_name fun_upd}, T) $ f $ x $ y)
haftmann@22845
   626
  fun dest_fun_T1 (Type (_, T :: Ts)) = T
haftmann@22845
   627
  fun find_double (t as Const (@{const_name fun_upd},T) $ f $ x $ y) =
haftmann@22845
   628
    let
haftmann@22845
   629
      fun find (Const (@{const_name fun_upd},T) $ g $ v $ w) =
haftmann@22845
   630
            if v aconv x then SOME g else gen_fun_upd (find g) T v w
haftmann@22845
   631
        | find t = NONE
haftmann@22845
   632
    in (dest_fun_T1 T, gen_fun_upd (find f) T x y) end
wenzelm@24017
   633
wenzelm@24017
   634
  fun proc ss ct =
wenzelm@24017
   635
    let
wenzelm@24017
   636
      val ctxt = Simplifier.the_context ss
wenzelm@24017
   637
      val t = Thm.term_of ct
wenzelm@24017
   638
    in
wenzelm@24017
   639
      case find_double t of
wenzelm@24017
   640
        (T, NONE) => NONE
wenzelm@24017
   641
      | (T, SOME rhs) =>
wenzelm@27330
   642
          SOME (Goal.prove ctxt [] [] (Logic.mk_equals (t, rhs))
wenzelm@24017
   643
            (fn _ =>
wenzelm@24017
   644
              rtac eq_reflection 1 THEN
wenzelm@24017
   645
              rtac ext 1 THEN
wenzelm@24017
   646
              simp_tac (Simplifier.inherit_context ss @{simpset}) 1))
wenzelm@24017
   647
    end
wenzelm@24017
   648
in proc end
haftmann@22845
   649
*}
haftmann@22845
   650
haftmann@22845
   651
haftmann@21870
   652
subsection {* Code generator setup *}
haftmann@21870
   653
berghofe@25886
   654
types_code
berghofe@25886
   655
  "fun"  ("(_ ->/ _)")
berghofe@25886
   656
attach (term_of) {*
berghofe@25886
   657
fun term_of_fun_type _ aT _ bT _ = Free ("<function>", aT --> bT);
berghofe@25886
   658
*}
berghofe@25886
   659
attach (test) {*
berghofe@25886
   660
fun gen_fun_type aF aT bG bT i =
berghofe@25886
   661
  let
wenzelm@32740
   662
    val tab = Unsynchronized.ref [];
berghofe@25886
   663
    fun mk_upd (x, (_, y)) t = Const ("Fun.fun_upd",
berghofe@25886
   664
      (aT --> bT) --> aT --> bT --> aT --> bT) $ t $ aF x $ y ()
berghofe@25886
   665
  in
berghofe@25886
   666
    (fn x =>
berghofe@25886
   667
       case AList.lookup op = (!tab) x of
berghofe@25886
   668
         NONE =>
berghofe@25886
   669
           let val p as (y, _) = bG i
berghofe@25886
   670
           in (tab := (x, p) :: !tab; y) end
berghofe@25886
   671
       | SOME (y, _) => y,
berghofe@28711
   672
     fn () => Basics.fold mk_upd (!tab) (Const ("HOL.undefined", aT --> bT)))
berghofe@25886
   673
  end;
berghofe@25886
   674
*}
berghofe@25886
   675
haftmann@21870
   676
code_const "op \<circ>"
haftmann@21870
   677
  (SML infixl 5 "o")
haftmann@21870
   678
  (Haskell infixr 9 ".")
haftmann@21870
   679
haftmann@21906
   680
code_const "id"
haftmann@21906
   681
  (Haskell "id")
haftmann@21906
   682
nipkow@2912
   683
end