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