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