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