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