src/HOL/Fun.thy
author wenzelm
Wed Mar 19 22:50:42 2008 +0100 (2008-03-19)
changeset 26342 0f65fa163304
parent 26147 ae2bf929e33c
child 26357 19b153ebda0b
permissions -rw-r--r--
more antiquotations;
clasohm@1475
     1
(*  Title:      HOL/Fun.thy
clasohm@923
     2
    ID:         $Id$
clasohm@1475
     3
    Author:     Tobias Nipkow, Cambridge University Computer Laboratory
clasohm@923
     4
    Copyright   1994  University of Cambridge
huffman@18154
     5
*)
clasohm@923
     6
huffman@18154
     7
header {* Notions about functions *}
clasohm@923
     8
paulson@15510
     9
theory Fun
haftmann@22886
    10
imports Set
nipkow@15131
    11
begin
nipkow@2912
    12
haftmann@26147
    13
text{*As a simplification rule, it replaces all function equalities by
haftmann@26147
    14
  first-order equalities.*}
haftmann@26147
    15
lemma expand_fun_eq: "f = g \<longleftrightarrow> (\<forall>x. f x = g x)"
haftmann@26147
    16
apply (rule iffI)
haftmann@26147
    17
apply (simp (no_asm_simp))
haftmann@26147
    18
apply (rule ext)
haftmann@26147
    19
apply (simp (no_asm_simp))
haftmann@26147
    20
done
oheimb@5305
    21
haftmann@26147
    22
lemma apply_inverse:
haftmann@26147
    23
  "f x =u \<Longrightarrow> (\<And>x. P x \<Longrightarrow> g (f x) = x) \<Longrightarrow> P x \<Longrightarrow> x = g u"
haftmann@26147
    24
  by auto
nipkow@2912
    25
wenzelm@12258
    26
haftmann@26147
    27
subsection {* The Identity Function @{text id} *}
paulson@6171
    28
haftmann@22744
    29
definition
haftmann@22744
    30
  id :: "'a \<Rightarrow> 'a"
haftmann@22744
    31
where
haftmann@22744
    32
  "id = (\<lambda>x. x)"
nipkow@13910
    33
haftmann@26147
    34
lemma id_apply [simp]: "id x = x"
haftmann@26147
    35
  by (simp add: id_def)
haftmann@26147
    36
haftmann@26147
    37
lemma image_ident [simp]: "(%x. x) ` Y = Y"
haftmann@26147
    38
by blast
haftmann@26147
    39
haftmann@26147
    40
lemma image_id [simp]: "id ` Y = Y"
haftmann@26147
    41
by (simp add: id_def)
haftmann@26147
    42
haftmann@26147
    43
lemma vimage_ident [simp]: "(%x. x) -` Y = Y"
haftmann@26147
    44
by blast
haftmann@26147
    45
haftmann@26147
    46
lemma vimage_id [simp]: "id -` A = A"
haftmann@26147
    47
by (simp add: id_def)
haftmann@26147
    48
haftmann@26147
    49
haftmann@26147
    50
subsection {* The Composition Operator @{text "f \<circ> g"} *}
haftmann@26147
    51
haftmann@22744
    52
definition
haftmann@22744
    53
  comp :: "('b \<Rightarrow> 'c) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'c" (infixl "o" 55)
haftmann@22744
    54
where
haftmann@22744
    55
  "f o g = (\<lambda>x. f (g x))"
oheimb@11123
    56
wenzelm@21210
    57
notation (xsymbols)
wenzelm@19656
    58
  comp  (infixl "\<circ>" 55)
wenzelm@19656
    59
wenzelm@21210
    60
notation (HTML output)
wenzelm@19656
    61
  comp  (infixl "\<circ>" 55)
wenzelm@19656
    62
paulson@13585
    63
text{*compatibility*}
paulson@13585
    64
lemmas o_def = comp_def
nipkow@2912
    65
paulson@13585
    66
lemma o_apply [simp]: "(f o g) x = f (g x)"
paulson@13585
    67
by (simp add: comp_def)
paulson@13585
    68
paulson@13585
    69
lemma o_assoc: "f o (g o h) = f o g o h"
paulson@13585
    70
by (simp add: comp_def)
paulson@13585
    71
paulson@13585
    72
lemma id_o [simp]: "id o g = g"
paulson@13585
    73
by (simp add: comp_def)
paulson@13585
    74
paulson@13585
    75
lemma o_id [simp]: "f o id = f"
paulson@13585
    76
by (simp add: comp_def)
paulson@13585
    77
paulson@13585
    78
lemma image_compose: "(f o g) ` r = f`(g`r)"
paulson@13585
    79
by (simp add: comp_def, blast)
paulson@13585
    80
paulson@13585
    81
lemma UN_o: "UNION A (g o f) = UNION (f`A) g"
paulson@13585
    82
by (unfold comp_def, blast)
paulson@13585
    83
paulson@13585
    84
haftmann@26147
    85
subsection {* Injectivity and Surjectivity *}
haftmann@26147
    86
haftmann@26147
    87
constdefs
haftmann@26147
    88
  inj_on :: "['a => 'b, 'a set] => bool"  -- "injective"
haftmann@26147
    89
  "inj_on f A == ! x:A. ! y:A. f(x)=f(y) --> x=y"
haftmann@26147
    90
haftmann@26147
    91
text{*A common special case: functions injective over the entire domain type.*}
haftmann@26147
    92
haftmann@26147
    93
abbreviation
haftmann@26147
    94
  "inj f == inj_on f UNIV"
paulson@13585
    95
haftmann@26147
    96
definition
haftmann@26147
    97
  bij_betw :: "('a => 'b) => 'a set => 'b set => bool" where -- "bijective"
haftmann@26147
    98
  "bij_betw f A B \<longleftrightarrow> inj_on f A & f ` A = B"
haftmann@26147
    99
haftmann@26147
   100
constdefs
haftmann@26147
   101
  surj :: "('a => 'b) => bool"                   (*surjective*)
haftmann@26147
   102
  "surj f == ! y. ? x. y=f(x)"
paulson@13585
   103
haftmann@26147
   104
  bij :: "('a => 'b) => bool"                    (*bijective*)
haftmann@26147
   105
  "bij f == inj f & surj f"
haftmann@26147
   106
haftmann@26147
   107
lemma injI:
haftmann@26147
   108
  assumes "\<And>x y. f x = f y \<Longrightarrow> x = y"
haftmann@26147
   109
  shows "inj f"
haftmann@26147
   110
  using assms unfolding inj_on_def by auto
paulson@13585
   111
paulson@13585
   112
text{*For Proofs in @{text "Tools/datatype_rep_proofs"}*}
paulson@13585
   113
lemma datatype_injI:
paulson@13585
   114
    "(!! x. ALL y. f(x) = f(y) --> x=y) ==> inj(f)"
paulson@13585
   115
by (simp add: inj_on_def)
paulson@13585
   116
berghofe@13637
   117
theorem range_ex1_eq: "inj f \<Longrightarrow> b : range f = (EX! x. b = f x)"
berghofe@13637
   118
  by (unfold inj_on_def, blast)
berghofe@13637
   119
paulson@13585
   120
lemma injD: "[| inj(f); f(x) = f(y) |] ==> x=y"
paulson@13585
   121
by (simp add: inj_on_def)
paulson@13585
   122
paulson@13585
   123
(*Useful with the simplifier*)
paulson@13585
   124
lemma inj_eq: "inj(f) ==> (f(x) = f(y)) = (x=y)"
paulson@13585
   125
by (force simp add: inj_on_def)
paulson@13585
   126
haftmann@26147
   127
lemma inj_on_id[simp]: "inj_on id A"
haftmann@26147
   128
  by (simp add: inj_on_def) 
paulson@13585
   129
haftmann@26147
   130
lemma inj_on_id2[simp]: "inj_on (%x. x) A"
haftmann@26147
   131
by (simp add: inj_on_def) 
haftmann@26147
   132
haftmann@26147
   133
lemma surj_id[simp]: "surj id"
haftmann@26147
   134
by (simp add: surj_def) 
haftmann@26147
   135
haftmann@26147
   136
lemma bij_id[simp]: "bij id"
haftmann@26147
   137
by (simp add: bij_def inj_on_id surj_id) 
paulson@13585
   138
paulson@13585
   139
lemma inj_onI:
paulson@13585
   140
    "(!! x y. [|  x:A;  y:A;  f(x) = f(y) |] ==> x=y) ==> inj_on f A"
paulson@13585
   141
by (simp add: inj_on_def)
paulson@13585
   142
paulson@13585
   143
lemma inj_on_inverseI: "(!!x. x:A ==> g(f(x)) = x) ==> inj_on f A"
paulson@13585
   144
by (auto dest:  arg_cong [of concl: g] simp add: inj_on_def)
paulson@13585
   145
paulson@13585
   146
lemma inj_onD: "[| inj_on f A;  f(x)=f(y);  x:A;  y:A |] ==> x=y"
paulson@13585
   147
by (unfold inj_on_def, blast)
paulson@13585
   148
paulson@13585
   149
lemma inj_on_iff: "[| inj_on f A;  x:A;  y:A |] ==> (f(x)=f(y)) = (x=y)"
paulson@13585
   150
by (blast dest!: inj_onD)
paulson@13585
   151
paulson@13585
   152
lemma comp_inj_on:
paulson@13585
   153
     "[| inj_on f A;  inj_on g (f`A) |] ==> inj_on (g o f) A"
paulson@13585
   154
by (simp add: comp_def inj_on_def)
paulson@13585
   155
nipkow@15303
   156
lemma inj_on_imageI: "inj_on (g o f) A \<Longrightarrow> inj_on g (f ` A)"
nipkow@15303
   157
apply(simp add:inj_on_def image_def)
nipkow@15303
   158
apply blast
nipkow@15303
   159
done
nipkow@15303
   160
nipkow@15439
   161
lemma inj_on_image_iff: "\<lbrakk> ALL x:A. ALL y:A. (g(f x) = g(f y)) = (g x = g y);
nipkow@15439
   162
  inj_on f A \<rbrakk> \<Longrightarrow> inj_on g (f ` A) = inj_on g A"
nipkow@15439
   163
apply(unfold inj_on_def)
nipkow@15439
   164
apply blast
nipkow@15439
   165
done
nipkow@15439
   166
paulson@13585
   167
lemma inj_on_contraD: "[| inj_on f A;  ~x=y;  x:A;  y:A |] ==> ~ f(x)=f(y)"
paulson@13585
   168
by (unfold inj_on_def, blast)
wenzelm@12258
   169
paulson@13585
   170
lemma inj_singleton: "inj (%s. {s})"
paulson@13585
   171
by (simp add: inj_on_def)
paulson@13585
   172
nipkow@15111
   173
lemma inj_on_empty[iff]: "inj_on f {}"
nipkow@15111
   174
by(simp add: inj_on_def)
nipkow@15111
   175
nipkow@15303
   176
lemma subset_inj_on: "[| inj_on f B; A <= B |] ==> inj_on f A"
paulson@13585
   177
by (unfold inj_on_def, blast)
paulson@13585
   178
nipkow@15111
   179
lemma inj_on_Un:
nipkow@15111
   180
 "inj_on f (A Un B) =
nipkow@15111
   181
  (inj_on f A & inj_on f B & f`(A-B) Int f`(B-A) = {})"
nipkow@15111
   182
apply(unfold inj_on_def)
nipkow@15111
   183
apply (blast intro:sym)
nipkow@15111
   184
done
nipkow@15111
   185
nipkow@15111
   186
lemma inj_on_insert[iff]:
nipkow@15111
   187
  "inj_on f (insert a A) = (inj_on f A & f a ~: f`(A-{a}))"
nipkow@15111
   188
apply(unfold inj_on_def)
nipkow@15111
   189
apply (blast intro:sym)
nipkow@15111
   190
done
nipkow@15111
   191
nipkow@15111
   192
lemma inj_on_diff: "inj_on f A ==> inj_on f (A-B)"
nipkow@15111
   193
apply(unfold inj_on_def)
nipkow@15111
   194
apply (blast)
nipkow@15111
   195
done
nipkow@15111
   196
paulson@13585
   197
lemma surjI: "(!! x. g(f x) = x) ==> surj g"
paulson@13585
   198
apply (simp add: surj_def)
paulson@13585
   199
apply (blast intro: sym)
paulson@13585
   200
done
paulson@13585
   201
paulson@13585
   202
lemma surj_range: "surj f ==> range f = UNIV"
paulson@13585
   203
by (auto simp add: surj_def)
paulson@13585
   204
paulson@13585
   205
lemma surjD: "surj f ==> EX x. y = f x"
paulson@13585
   206
by (simp add: surj_def)
paulson@13585
   207
paulson@13585
   208
lemma surjE: "surj f ==> (!!x. y = f x ==> C) ==> C"
paulson@13585
   209
by (simp add: surj_def, blast)
paulson@13585
   210
paulson@13585
   211
lemma comp_surj: "[| surj f;  surj g |] ==> surj (g o f)"
paulson@13585
   212
apply (simp add: comp_def surj_def, clarify)
paulson@13585
   213
apply (drule_tac x = y in spec, clarify)
paulson@13585
   214
apply (drule_tac x = x in spec, blast)
paulson@13585
   215
done
paulson@13585
   216
paulson@13585
   217
lemma bijI: "[| inj f; surj f |] ==> bij f"
paulson@13585
   218
by (simp add: bij_def)
paulson@13585
   219
paulson@13585
   220
lemma bij_is_inj: "bij f ==> inj f"
paulson@13585
   221
by (simp add: bij_def)
paulson@13585
   222
paulson@13585
   223
lemma bij_is_surj: "bij f ==> surj f"
paulson@13585
   224
by (simp add: bij_def)
paulson@13585
   225
nipkow@26105
   226
lemma bij_betw_imp_inj_on: "bij_betw f A B \<Longrightarrow> inj_on f A"
nipkow@26105
   227
by (simp add: bij_betw_def)
nipkow@26105
   228
nipkow@26105
   229
lemma bij_betw_inv: assumes "bij_betw f A B" shows "EX g. bij_betw g B A"
nipkow@26105
   230
proof -
nipkow@26105
   231
  have i: "inj_on f A" and s: "f ` A = B"
nipkow@26105
   232
    using assms by(auto simp:bij_betw_def)
nipkow@26105
   233
  let ?P = "%b a. a:A \<and> f a = b" let ?g = "%b. The (?P b)"
nipkow@26105
   234
  { fix a b assume P: "?P b a"
nipkow@26105
   235
    hence ex1: "\<exists>a. ?P b a" using s unfolding image_def by blast
nipkow@26105
   236
    hence uex1: "\<exists>!a. ?P b a" by(blast dest:inj_onD[OF i])
nipkow@26105
   237
    hence " ?g b = a" using the1_equality[OF uex1, OF P] P by simp
nipkow@26105
   238
  } note g = this
nipkow@26105
   239
  have "inj_on ?g B"
nipkow@26105
   240
  proof(rule inj_onI)
nipkow@26105
   241
    fix x y assume "x:B" "y:B" "?g x = ?g y"
nipkow@26105
   242
    from s `x:B` obtain a1 where a1: "?P x a1" unfolding image_def by blast
nipkow@26105
   243
    from s `y:B` obtain a2 where a2: "?P y a2" unfolding image_def by blast
nipkow@26105
   244
    from g[OF a1] a1 g[OF a2] a2 `?g x = ?g y` show "x=y" by simp
nipkow@26105
   245
  qed
nipkow@26105
   246
  moreover have "?g ` B = A"
nipkow@26105
   247
  proof(auto simp:image_def)
nipkow@26105
   248
    fix b assume "b:B"
nipkow@26105
   249
    with s obtain a where P: "?P b a" unfolding image_def by blast
nipkow@26105
   250
    thus "?g b \<in> A" using g[OF P] by auto
nipkow@26105
   251
  next
nipkow@26105
   252
    fix a assume "a:A"
nipkow@26105
   253
    then obtain b where P: "?P b a" using s unfolding image_def by blast
nipkow@26105
   254
    then have "b:B" using s unfolding image_def by blast
nipkow@26105
   255
    with g[OF P] show "\<exists>b\<in>B. a = ?g b" by blast
nipkow@26105
   256
  qed
nipkow@26105
   257
  ultimately show ?thesis by(auto simp:bij_betw_def)
nipkow@26105
   258
qed
nipkow@26105
   259
paulson@13585
   260
lemma surj_image_vimage_eq: "surj f ==> f ` (f -` A) = A"
paulson@13585
   261
by (simp add: surj_range)
paulson@13585
   262
paulson@13585
   263
lemma inj_vimage_image_eq: "inj f ==> f -` (f ` A) = A"
paulson@13585
   264
by (simp add: inj_on_def, blast)
paulson@13585
   265
paulson@13585
   266
lemma vimage_subsetD: "surj f ==> f -` B <= A ==> B <= f ` A"
paulson@13585
   267
apply (unfold surj_def)
paulson@13585
   268
apply (blast intro: sym)
paulson@13585
   269
done
paulson@13585
   270
paulson@13585
   271
lemma vimage_subsetI: "inj f ==> B <= f ` A ==> f -` B <= A"
paulson@13585
   272
by (unfold inj_on_def, blast)
paulson@13585
   273
paulson@13585
   274
lemma vimage_subset_eq: "bij f ==> (f -` B <= A) = (B <= f ` A)"
paulson@13585
   275
apply (unfold bij_def)
paulson@13585
   276
apply (blast del: subsetI intro: vimage_subsetI vimage_subsetD)
paulson@13585
   277
done
paulson@13585
   278
paulson@13585
   279
lemma inj_on_image_Int:
paulson@13585
   280
   "[| inj_on f C;  A<=C;  B<=C |] ==> f`(A Int B) = f`A Int f`B"
paulson@13585
   281
apply (simp add: inj_on_def, blast)
paulson@13585
   282
done
paulson@13585
   283
paulson@13585
   284
lemma inj_on_image_set_diff:
paulson@13585
   285
   "[| inj_on f C;  A<=C;  B<=C |] ==> f`(A-B) = f`A - f`B"
paulson@13585
   286
apply (simp add: inj_on_def, blast)
paulson@13585
   287
done
paulson@13585
   288
paulson@13585
   289
lemma image_Int: "inj f ==> f`(A Int B) = f`A Int f`B"
paulson@13585
   290
by (simp add: inj_on_def, blast)
paulson@13585
   291
paulson@13585
   292
lemma image_set_diff: "inj f ==> f`(A-B) = f`A - f`B"
paulson@13585
   293
by (simp add: inj_on_def, blast)
paulson@13585
   294
paulson@13585
   295
lemma inj_image_mem_iff: "inj f ==> (f a : f`A) = (a : A)"
paulson@13585
   296
by (blast dest: injD)
paulson@13585
   297
paulson@13585
   298
lemma inj_image_subset_iff: "inj f ==> (f`A <= f`B) = (A<=B)"
paulson@13585
   299
by (simp add: inj_on_def, blast)
paulson@13585
   300
paulson@13585
   301
lemma inj_image_eq_iff: "inj f ==> (f`A = f`B) = (A = B)"
paulson@13585
   302
by (blast dest: injD)
paulson@13585
   303
paulson@13585
   304
(*injectivity's required.  Left-to-right inclusion holds even if A is empty*)
paulson@13585
   305
lemma image_INT:
paulson@13585
   306
   "[| inj_on f C;  ALL x:A. B x <= C;  j:A |]
paulson@13585
   307
    ==> f ` (INTER A B) = (INT x:A. f ` B x)"
paulson@13585
   308
apply (simp add: inj_on_def, blast)
paulson@13585
   309
done
paulson@13585
   310
paulson@13585
   311
(*Compare with image_INT: no use of inj_on, and if f is surjective then
paulson@13585
   312
  it doesn't matter whether A is empty*)
paulson@13585
   313
lemma bij_image_INT: "bij f ==> f ` (INTER A B) = (INT x:A. f ` B x)"
paulson@13585
   314
apply (simp add: bij_def)
paulson@13585
   315
apply (simp add: inj_on_def surj_def, blast)
paulson@13585
   316
done
paulson@13585
   317
paulson@13585
   318
lemma surj_Compl_image_subset: "surj f ==> -(f`A) <= f`(-A)"
paulson@13585
   319
by (auto simp add: surj_def)
paulson@13585
   320
paulson@13585
   321
lemma inj_image_Compl_subset: "inj f ==> f`(-A) <= -(f`A)"
paulson@13585
   322
by (auto simp add: inj_on_def)
paulson@5852
   323
paulson@13585
   324
lemma bij_image_Compl_eq: "bij f ==> f`(-A) = -(f`A)"
paulson@13585
   325
apply (simp add: bij_def)
paulson@13585
   326
apply (rule equalityI)
paulson@13585
   327
apply (simp_all (no_asm_simp) add: inj_image_Compl_subset surj_Compl_image_subset)
paulson@13585
   328
done
paulson@13585
   329
paulson@13585
   330
paulson@13585
   331
subsection{*Function Updating*}
paulson@13585
   332
haftmann@26147
   333
constdefs
haftmann@26147
   334
  fun_upd :: "('a => 'b) => 'a => 'b => ('a => 'b)"
haftmann@26147
   335
  "fun_upd f a b == % x. if x=a then b else f x"
haftmann@26147
   336
haftmann@26147
   337
nonterminals
haftmann@26147
   338
  updbinds updbind
haftmann@26147
   339
syntax
haftmann@26147
   340
  "_updbind" :: "['a, 'a] => updbind"             ("(2_ :=/ _)")
haftmann@26147
   341
  ""         :: "updbind => updbinds"             ("_")
haftmann@26147
   342
  "_updbinds":: "[updbind, updbinds] => updbinds" ("_,/ _")
haftmann@26147
   343
  "_Update"  :: "['a, updbinds] => 'a"            ("_/'((_)')" [1000,0] 900)
haftmann@26147
   344
haftmann@26147
   345
translations
haftmann@26147
   346
  "_Update f (_updbinds b bs)"  == "_Update (_Update f b) bs"
haftmann@26147
   347
  "f(x:=y)"                     == "fun_upd f x y"
haftmann@26147
   348
haftmann@26147
   349
(* Hint: to define the sum of two functions (or maps), use sum_case.
haftmann@26147
   350
         A nice infix syntax could be defined (in Datatype.thy or below) by
haftmann@26147
   351
consts
haftmann@26147
   352
  fun_sum :: "('a => 'c) => ('b => 'c) => (('a+'b) => 'c)" (infixr "'(+')"80)
haftmann@26147
   353
translations
haftmann@26147
   354
 "fun_sum" == sum_case
haftmann@26147
   355
*)
haftmann@26147
   356
paulson@13585
   357
lemma fun_upd_idem_iff: "(f(x:=y) = f) = (f x = y)"
paulson@13585
   358
apply (simp add: fun_upd_def, safe)
paulson@13585
   359
apply (erule subst)
paulson@13585
   360
apply (rule_tac [2] ext, auto)
paulson@13585
   361
done
paulson@13585
   362
paulson@13585
   363
(* f x = y ==> f(x:=y) = f *)
paulson@13585
   364
lemmas fun_upd_idem = fun_upd_idem_iff [THEN iffD2, standard]
paulson@13585
   365
paulson@13585
   366
(* f(x := f x) = f *)
paulson@17084
   367
lemmas fun_upd_triv = refl [THEN fun_upd_idem]
paulson@17084
   368
declare fun_upd_triv [iff]
paulson@13585
   369
paulson@13585
   370
lemma fun_upd_apply [simp]: "(f(x:=y))z = (if z=x then y else f z)"
paulson@17084
   371
by (simp add: fun_upd_def)
paulson@13585
   372
paulson@13585
   373
(* fun_upd_apply supersedes these two,   but they are useful
paulson@13585
   374
   if fun_upd_apply is intentionally removed from the simpset *)
paulson@13585
   375
lemma fun_upd_same: "(f(x:=y)) x = y"
paulson@13585
   376
by simp
paulson@13585
   377
paulson@13585
   378
lemma fun_upd_other: "z~=x ==> (f(x:=y)) z = f z"
paulson@13585
   379
by simp
paulson@13585
   380
paulson@13585
   381
lemma fun_upd_upd [simp]: "f(x:=y,x:=z) = f(x:=z)"
paulson@13585
   382
by (simp add: expand_fun_eq)
paulson@13585
   383
paulson@13585
   384
lemma fun_upd_twist: "a ~= c ==> (m(a:=b))(c:=d) = (m(c:=d))(a:=b)"
paulson@13585
   385
by (rule ext, auto)
paulson@13585
   386
nipkow@15303
   387
lemma inj_on_fun_updI: "\<lbrakk> inj_on f A; y \<notin> f`A \<rbrakk> \<Longrightarrow> inj_on (f(x:=y)) A"
nipkow@15303
   388
by(fastsimp simp:inj_on_def image_def)
nipkow@15303
   389
paulson@15510
   390
lemma fun_upd_image:
paulson@15510
   391
     "f(x:=y) ` A = (if x \<in> A then insert y (f ` (A-{x})) else f ` A)"
paulson@15510
   392
by auto
paulson@15510
   393
haftmann@26147
   394
haftmann@26147
   395
subsection {* @{text override_on} *}
haftmann@26147
   396
haftmann@26147
   397
definition
haftmann@26147
   398
  override_on :: "('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'a set \<Rightarrow> 'a \<Rightarrow> 'b"
haftmann@26147
   399
where
haftmann@26147
   400
  "override_on f g A = (\<lambda>a. if a \<in> A then g a else f a)"
nipkow@13910
   401
nipkow@15691
   402
lemma override_on_emptyset[simp]: "override_on f g {} = f"
nipkow@15691
   403
by(simp add:override_on_def)
nipkow@13910
   404
nipkow@15691
   405
lemma override_on_apply_notin[simp]: "a ~: A ==> (override_on f g A) a = f a"
nipkow@15691
   406
by(simp add:override_on_def)
nipkow@13910
   407
nipkow@15691
   408
lemma override_on_apply_in[simp]: "a : A ==> (override_on f g A) a = g a"
nipkow@15691
   409
by(simp add:override_on_def)
nipkow@13910
   410
haftmann@26147
   411
haftmann@26147
   412
subsection {* @{text swap} *}
paulson@15510
   413
haftmann@22744
   414
definition
haftmann@22744
   415
  swap :: "'a \<Rightarrow> 'a \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b)"
haftmann@22744
   416
where
haftmann@22744
   417
  "swap a b f = f (a := f b, b:= f a)"
paulson@15510
   418
paulson@15510
   419
lemma swap_self: "swap a a f = f"
nipkow@15691
   420
by (simp add: swap_def)
paulson@15510
   421
paulson@15510
   422
lemma swap_commute: "swap a b f = swap b a f"
paulson@15510
   423
by (rule ext, simp add: fun_upd_def swap_def)
paulson@15510
   424
paulson@15510
   425
lemma swap_nilpotent [simp]: "swap a b (swap a b f) = f"
paulson@15510
   426
by (rule ext, simp add: fun_upd_def swap_def)
paulson@15510
   427
paulson@15510
   428
lemma inj_on_imp_inj_on_swap:
haftmann@22744
   429
  "[|inj_on f A; a \<in> A; b \<in> A|] ==> inj_on (swap a b f) A"
paulson@15510
   430
by (simp add: inj_on_def swap_def, blast)
paulson@15510
   431
paulson@15510
   432
lemma inj_on_swap_iff [simp]:
paulson@15510
   433
  assumes A: "a \<in> A" "b \<in> A" shows "inj_on (swap a b f) A = inj_on f A"
paulson@15510
   434
proof 
paulson@15510
   435
  assume "inj_on (swap a b f) A"
paulson@15510
   436
  with A have "inj_on (swap a b (swap a b f)) A" 
nipkow@17589
   437
    by (iprover intro: inj_on_imp_inj_on_swap) 
paulson@15510
   438
  thus "inj_on f A" by simp 
paulson@15510
   439
next
paulson@15510
   440
  assume "inj_on f A"
nipkow@17589
   441
  with A show "inj_on (swap a b f) A" by (iprover intro: inj_on_imp_inj_on_swap)
paulson@15510
   442
qed
paulson@15510
   443
paulson@15510
   444
lemma surj_imp_surj_swap: "surj f ==> surj (swap a b f)"
paulson@15510
   445
apply (simp add: surj_def swap_def, clarify)
paulson@15510
   446
apply (rule_tac P = "y = f b" in case_split_thm, blast)
paulson@15510
   447
apply (rule_tac P = "y = f a" in case_split_thm, auto)
paulson@15510
   448
  --{*We don't yet have @{text case_tac}*}
paulson@15510
   449
done
paulson@15510
   450
paulson@15510
   451
lemma surj_swap_iff [simp]: "surj (swap a b f) = surj f"
paulson@15510
   452
proof 
paulson@15510
   453
  assume "surj (swap a b f)"
paulson@15510
   454
  hence "surj (swap a b (swap a b f))" by (rule surj_imp_surj_swap) 
paulson@15510
   455
  thus "surj f" by simp 
paulson@15510
   456
next
paulson@15510
   457
  assume "surj f"
paulson@15510
   458
  thus "surj (swap a b f)" by (rule surj_imp_surj_swap) 
paulson@15510
   459
qed
paulson@15510
   460
paulson@15510
   461
lemma bij_swap_iff: "bij (swap a b f) = bij f"
paulson@15510
   462
by (simp add: bij_def)
haftmann@21547
   463
haftmann@21547
   464
haftmann@22845
   465
subsection {* Proof tool setup *} 
haftmann@22845
   466
haftmann@22845
   467
text {* simplifies terms of the form
haftmann@22845
   468
  f(...,x:=y,...,x:=z,...) to f(...,x:=z,...) *}
haftmann@22845
   469
wenzelm@24017
   470
simproc_setup fun_upd2 ("f(v := w, x := y)") = {* fn _ =>
haftmann@22845
   471
let
haftmann@22845
   472
  fun gen_fun_upd NONE T _ _ = NONE
wenzelm@24017
   473
    | gen_fun_upd (SOME f) T x y = SOME (Const (@{const_name fun_upd}, T) $ f $ x $ y)
haftmann@22845
   474
  fun dest_fun_T1 (Type (_, T :: Ts)) = T
haftmann@22845
   475
  fun find_double (t as Const (@{const_name fun_upd},T) $ f $ x $ y) =
haftmann@22845
   476
    let
haftmann@22845
   477
      fun find (Const (@{const_name fun_upd},T) $ g $ v $ w) =
haftmann@22845
   478
            if v aconv x then SOME g else gen_fun_upd (find g) T v w
haftmann@22845
   479
        | find t = NONE
haftmann@22845
   480
    in (dest_fun_T1 T, gen_fun_upd (find f) T x y) end
wenzelm@24017
   481
wenzelm@24017
   482
  fun proc ss ct =
wenzelm@24017
   483
    let
wenzelm@24017
   484
      val ctxt = Simplifier.the_context ss
wenzelm@24017
   485
      val t = Thm.term_of ct
wenzelm@24017
   486
    in
wenzelm@24017
   487
      case find_double t of
wenzelm@24017
   488
        (T, NONE) => NONE
wenzelm@24017
   489
      | (T, SOME rhs) =>
wenzelm@24017
   490
          SOME (Goal.prove ctxt [] [] (Term.equals T $ t $ rhs)
wenzelm@24017
   491
            (fn _ =>
wenzelm@24017
   492
              rtac eq_reflection 1 THEN
wenzelm@24017
   493
              rtac ext 1 THEN
wenzelm@24017
   494
              simp_tac (Simplifier.inherit_context ss @{simpset}) 1))
wenzelm@24017
   495
    end
wenzelm@24017
   496
in proc end
haftmann@22845
   497
*}
haftmann@22845
   498
haftmann@22845
   499
haftmann@21870
   500
subsection {* Code generator setup *}
haftmann@21870
   501
berghofe@25886
   502
types_code
berghofe@25886
   503
  "fun"  ("(_ ->/ _)")
berghofe@25886
   504
attach (term_of) {*
berghofe@25886
   505
fun term_of_fun_type _ aT _ bT _ = Free ("<function>", aT --> bT);
berghofe@25886
   506
*}
berghofe@25886
   507
attach (test) {*
berghofe@25886
   508
fun gen_fun_type aF aT bG bT i =
berghofe@25886
   509
  let
berghofe@25886
   510
    val tab = ref [];
berghofe@25886
   511
    fun mk_upd (x, (_, y)) t = Const ("Fun.fun_upd",
berghofe@25886
   512
      (aT --> bT) --> aT --> bT --> aT --> bT) $ t $ aF x $ y ()
berghofe@25886
   513
  in
berghofe@25886
   514
    (fn x =>
berghofe@25886
   515
       case AList.lookup op = (!tab) x of
berghofe@25886
   516
         NONE =>
berghofe@25886
   517
           let val p as (y, _) = bG i
berghofe@25886
   518
           in (tab := (x, p) :: !tab; y) end
berghofe@25886
   519
       | SOME (y, _) => y,
berghofe@25886
   520
     fn () => Basics.fold mk_upd (!tab) (Const ("arbitrary", aT --> bT)))
berghofe@25886
   521
  end;
berghofe@25886
   522
*}
berghofe@25886
   523
haftmann@21870
   524
code_const "op \<circ>"
haftmann@21870
   525
  (SML infixl 5 "o")
haftmann@21870
   526
  (Haskell infixr 9 ".")
haftmann@21870
   527
haftmann@21906
   528
code_const "id"
haftmann@21906
   529
  (Haskell "id")
haftmann@21906
   530
haftmann@21870
   531
haftmann@21547
   532
subsection {* ML legacy bindings *} 
paulson@15510
   533
haftmann@22845
   534
ML {*
wenzelm@26342
   535
val set_cs = @{claset} delrules [@{thm equalityI}]
haftmann@22845
   536
*}
paulson@5852
   537
haftmann@22845
   538
ML {*
haftmann@22845
   539
val id_apply = @{thm id_apply}
haftmann@22845
   540
val id_def = @{thm id_def}
haftmann@22845
   541
val o_apply = @{thm o_apply}
haftmann@22845
   542
val o_assoc = @{thm o_assoc}
haftmann@22845
   543
val o_def = @{thm o_def}
haftmann@22845
   544
val injD = @{thm injD}
haftmann@22845
   545
val datatype_injI = @{thm datatype_injI}
haftmann@22845
   546
val range_ex1_eq = @{thm range_ex1_eq}
haftmann@22845
   547
val expand_fun_eq = @{thm expand_fun_eq}
paulson@13585
   548
*}
paulson@5852
   549
nipkow@2912
   550
end