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