src/HOL/Fun.thy
author wenzelm
Sat Jul 28 20:40:19 2007 +0200 (2007-07-28)
changeset 24017 363287741ebe
parent 23878 bd651ecd4b8a
child 24286 7619080e49f0
permissions -rw-r--r--
simproc_setup fun_upd2;
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
paulson@13585
    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
paulson@13585
    65
text{*A common special case: functions injective over the entire domain type.*}
nipkow@19323
    66
wenzelm@19363
    67
abbreviation
wenzelm@19363
    68
  "inj f == inj_on f UNIV"
paulson@5852
    69
paulson@7374
    70
constdefs
paulson@13585
    71
  surj :: "('a => 'b) => bool"                   (*surjective*)
wenzelm@19363
    72
  "surj f == ! y. ? x. y=f(x)"
wenzelm@12258
    73
paulson@13585
    74
  bij :: "('a => 'b) => bool"                    (*bijective*)
wenzelm@19363
    75
  "bij f == inj f & surj f"
wenzelm@12258
    76
paulson@7374
    77
paulson@13585
    78
paulson@13585
    79
text{*As a simplification rule, it replaces all function equalities by
paulson@13585
    80
  first-order equalities.*}
haftmann@21327
    81
lemma expand_fun_eq: "f = g \<longleftrightarrow> (\<forall>x. f x = g x)"
paulson@13585
    82
apply (rule iffI)
paulson@13585
    83
apply (simp (no_asm_simp))
haftmann@21327
    84
apply (rule ext)
haftmann@21327
    85
apply (simp (no_asm_simp))
paulson@13585
    86
done
paulson@13585
    87
paulson@13585
    88
lemma apply_inverse:
paulson@13585
    89
    "[| f(x)=u;  !!x. P(x) ==> g(f(x)) = x;  P(x) |] ==> x=g(u)"
paulson@13585
    90
by auto
paulson@13585
    91
paulson@13585
    92
paulson@13585
    93
text{*The Identity Function: @{term id}*}
paulson@13585
    94
lemma id_apply [simp]: "id x = x"
paulson@13585
    95
by (simp add: id_def)
paulson@13585
    96
nipkow@16733
    97
lemma inj_on_id[simp]: "inj_on id A"
paulson@15510
    98
by (simp add: inj_on_def) 
paulson@15510
    99
nipkow@16733
   100
lemma inj_on_id2[simp]: "inj_on (%x. x) A"
nipkow@16733
   101
by (simp add: inj_on_def) 
nipkow@16733
   102
nipkow@16733
   103
lemma surj_id[simp]: "surj id"
paulson@15510
   104
by (simp add: surj_def) 
paulson@15510
   105
nipkow@16733
   106
lemma bij_id[simp]: "bij id"
paulson@15510
   107
by (simp add: bij_def inj_on_id surj_id) 
paulson@15510
   108
paulson@15510
   109
paulson@13585
   110
paulson@13585
   111
subsection{*The Composition Operator: @{term "f \<circ> g"}*}
paulson@13585
   112
paulson@13585
   113
lemma o_apply [simp]: "(f o g) x = f (g x)"
paulson@13585
   114
by (simp add: comp_def)
paulson@13585
   115
paulson@13585
   116
lemma o_assoc: "f o (g o h) = f o g o h"
paulson@13585
   117
by (simp add: comp_def)
paulson@13585
   118
paulson@13585
   119
lemma id_o [simp]: "id o g = g"
paulson@13585
   120
by (simp add: comp_def)
paulson@13585
   121
paulson@13585
   122
lemma o_id [simp]: "f o id = f"
paulson@13585
   123
by (simp add: comp_def)
paulson@13585
   124
paulson@13585
   125
lemma image_compose: "(f o g) ` r = f`(g`r)"
paulson@13585
   126
by (simp add: comp_def, blast)
paulson@13585
   127
paulson@13585
   128
lemma image_eq_UN: "f`A = (UN x:A. {f x})"
paulson@13585
   129
by blast
paulson@13585
   130
paulson@13585
   131
lemma UN_o: "UNION A (g o f) = UNION (f`A) g"
paulson@13585
   132
by (unfold comp_def, blast)
paulson@13585
   133
paulson@13585
   134
paulson@13585
   135
subsection{*The Injectivity Predicate, @{term inj}*}
paulson@13585
   136
paulson@13585
   137
text{*NB: @{term inj} now just translates to @{term inj_on}*}
paulson@13585
   138
paulson@13585
   139
paulson@13585
   140
text{*For Proofs in @{text "Tools/datatype_rep_proofs"}*}
paulson@13585
   141
lemma datatype_injI:
paulson@13585
   142
    "(!! x. ALL y. f(x) = f(y) --> x=y) ==> inj(f)"
paulson@13585
   143
by (simp add: inj_on_def)
paulson@13585
   144
berghofe@13637
   145
theorem range_ex1_eq: "inj f \<Longrightarrow> b : range f = (EX! x. b = f x)"
berghofe@13637
   146
  by (unfold inj_on_def, blast)
berghofe@13637
   147
paulson@13585
   148
lemma injD: "[| inj(f); f(x) = f(y) |] ==> x=y"
paulson@13585
   149
by (simp add: inj_on_def)
paulson@13585
   150
paulson@13585
   151
(*Useful with the simplifier*)
paulson@13585
   152
lemma inj_eq: "inj(f) ==> (f(x) = f(y)) = (x=y)"
paulson@13585
   153
by (force simp add: inj_on_def)
paulson@13585
   154
paulson@13585
   155
paulson@13585
   156
subsection{*The Predicate @{term inj_on}: Injectivity On A Restricted Domain*}
paulson@13585
   157
paulson@13585
   158
lemma inj_onI:
paulson@13585
   159
    "(!! x y. [|  x:A;  y:A;  f(x) = f(y) |] ==> x=y) ==> inj_on f A"
paulson@13585
   160
by (simp add: inj_on_def)
paulson@13585
   161
paulson@13585
   162
lemma inj_on_inverseI: "(!!x. x:A ==> g(f(x)) = x) ==> inj_on f A"
paulson@13585
   163
by (auto dest:  arg_cong [of concl: g] simp add: inj_on_def)
paulson@13585
   164
paulson@13585
   165
lemma inj_onD: "[| inj_on f A;  f(x)=f(y);  x:A;  y:A |] ==> x=y"
paulson@13585
   166
by (unfold inj_on_def, blast)
paulson@13585
   167
paulson@13585
   168
lemma inj_on_iff: "[| inj_on f A;  x:A;  y:A |] ==> (f(x)=f(y)) = (x=y)"
paulson@13585
   169
by (blast dest!: inj_onD)
paulson@13585
   170
paulson@13585
   171
lemma comp_inj_on:
paulson@13585
   172
     "[| inj_on f A;  inj_on g (f`A) |] ==> inj_on (g o f) A"
paulson@13585
   173
by (simp add: comp_def inj_on_def)
paulson@13585
   174
nipkow@15303
   175
lemma inj_on_imageI: "inj_on (g o f) A \<Longrightarrow> inj_on g (f ` A)"
nipkow@15303
   176
apply(simp add:inj_on_def image_def)
nipkow@15303
   177
apply blast
nipkow@15303
   178
done
nipkow@15303
   179
nipkow@15439
   180
lemma inj_on_image_iff: "\<lbrakk> ALL x:A. ALL y:A. (g(f x) = g(f y)) = (g x = g y);
nipkow@15439
   181
  inj_on f A \<rbrakk> \<Longrightarrow> inj_on g (f ` A) = inj_on g A"
nipkow@15439
   182
apply(unfold inj_on_def)
nipkow@15439
   183
apply blast
nipkow@15439
   184
done
nipkow@15439
   185
paulson@13585
   186
lemma inj_on_contraD: "[| inj_on f A;  ~x=y;  x:A;  y:A |] ==> ~ f(x)=f(y)"
paulson@13585
   187
by (unfold inj_on_def, blast)
wenzelm@12258
   188
paulson@13585
   189
lemma inj_singleton: "inj (%s. {s})"
paulson@13585
   190
by (simp add: inj_on_def)
paulson@13585
   191
nipkow@15111
   192
lemma inj_on_empty[iff]: "inj_on f {}"
nipkow@15111
   193
by(simp add: inj_on_def)
nipkow@15111
   194
nipkow@15303
   195
lemma subset_inj_on: "[| inj_on f B; A <= B |] ==> inj_on f A"
paulson@13585
   196
by (unfold inj_on_def, blast)
paulson@13585
   197
nipkow@15111
   198
lemma inj_on_Un:
nipkow@15111
   199
 "inj_on f (A Un B) =
nipkow@15111
   200
  (inj_on f A & inj_on f B & f`(A-B) Int f`(B-A) = {})"
nipkow@15111
   201
apply(unfold inj_on_def)
nipkow@15111
   202
apply (blast intro:sym)
nipkow@15111
   203
done
nipkow@15111
   204
nipkow@15111
   205
lemma inj_on_insert[iff]:
nipkow@15111
   206
  "inj_on f (insert a A) = (inj_on f A & f a ~: f`(A-{a}))"
nipkow@15111
   207
apply(unfold inj_on_def)
nipkow@15111
   208
apply (blast intro:sym)
nipkow@15111
   209
done
nipkow@15111
   210
nipkow@15111
   211
lemma inj_on_diff: "inj_on f A ==> inj_on f (A-B)"
nipkow@15111
   212
apply(unfold inj_on_def)
nipkow@15111
   213
apply (blast)
nipkow@15111
   214
done
nipkow@15111
   215
paulson@13585
   216
paulson@13585
   217
subsection{*The Predicate @{term surj}: Surjectivity*}
paulson@13585
   218
paulson@13585
   219
lemma surjI: "(!! x. g(f x) = x) ==> surj g"
paulson@13585
   220
apply (simp add: surj_def)
paulson@13585
   221
apply (blast intro: sym)
paulson@13585
   222
done
paulson@13585
   223
paulson@13585
   224
lemma surj_range: "surj f ==> range f = UNIV"
paulson@13585
   225
by (auto simp add: surj_def)
paulson@13585
   226
paulson@13585
   227
lemma surjD: "surj f ==> EX x. y = f x"
paulson@13585
   228
by (simp add: surj_def)
paulson@13585
   229
paulson@13585
   230
lemma surjE: "surj f ==> (!!x. y = f x ==> C) ==> C"
paulson@13585
   231
by (simp add: surj_def, blast)
paulson@13585
   232
paulson@13585
   233
lemma comp_surj: "[| surj f;  surj g |] ==> surj (g o f)"
paulson@13585
   234
apply (simp add: comp_def surj_def, clarify)
paulson@13585
   235
apply (drule_tac x = y in spec, clarify)
paulson@13585
   236
apply (drule_tac x = x in spec, blast)
paulson@13585
   237
done
paulson@13585
   238
paulson@13585
   239
paulson@13585
   240
paulson@13585
   241
subsection{*The Predicate @{term bij}: Bijectivity*}
paulson@13585
   242
paulson@13585
   243
lemma bijI: "[| inj f; surj f |] ==> bij f"
paulson@13585
   244
by (simp add: bij_def)
paulson@13585
   245
paulson@13585
   246
lemma bij_is_inj: "bij f ==> inj f"
paulson@13585
   247
by (simp add: bij_def)
paulson@13585
   248
paulson@13585
   249
lemma bij_is_surj: "bij f ==> surj f"
paulson@13585
   250
by (simp add: bij_def)
paulson@13585
   251
paulson@13585
   252
paulson@13585
   253
subsection{*Facts About the Identity Function*}
paulson@5852
   254
paulson@13585
   255
text{*We seem to need both the @{term id} forms and the @{term "\<lambda>x. x"}
paulson@13585
   256
forms. The latter can arise by rewriting, while @{term id} may be used
paulson@13585
   257
explicitly.*}
paulson@13585
   258
paulson@13585
   259
lemma image_ident [simp]: "(%x. x) ` Y = Y"
paulson@13585
   260
by blast
paulson@13585
   261
paulson@13585
   262
lemma image_id [simp]: "id ` Y = Y"
paulson@13585
   263
by (simp add: id_def)
paulson@13585
   264
paulson@13585
   265
lemma vimage_ident [simp]: "(%x. x) -` Y = Y"
paulson@13585
   266
by blast
paulson@13585
   267
paulson@13585
   268
lemma vimage_id [simp]: "id -` A = A"
paulson@13585
   269
by (simp add: id_def)
paulson@13585
   270
paulson@13585
   271
lemma vimage_image_eq: "f -` (f ` A) = {y. EX x:A. f x = f y}"
paulson@13585
   272
by (blast intro: sym)
paulson@13585
   273
paulson@13585
   274
lemma image_vimage_subset: "f ` (f -` A) <= A"
paulson@13585
   275
by blast
paulson@13585
   276
paulson@13585
   277
lemma image_vimage_eq [simp]: "f ` (f -` A) = A Int range f"
paulson@13585
   278
by blast
paulson@13585
   279
paulson@13585
   280
lemma surj_image_vimage_eq: "surj f ==> f ` (f -` A) = A"
paulson@13585
   281
by (simp add: surj_range)
paulson@13585
   282
paulson@13585
   283
lemma inj_vimage_image_eq: "inj f ==> f -` (f ` A) = A"
paulson@13585
   284
by (simp add: inj_on_def, blast)
paulson@13585
   285
paulson@13585
   286
lemma vimage_subsetD: "surj f ==> f -` B <= A ==> B <= f ` A"
paulson@13585
   287
apply (unfold surj_def)
paulson@13585
   288
apply (blast intro: sym)
paulson@13585
   289
done
paulson@13585
   290
paulson@13585
   291
lemma vimage_subsetI: "inj f ==> B <= f ` A ==> f -` B <= A"
paulson@13585
   292
by (unfold inj_on_def, blast)
paulson@13585
   293
paulson@13585
   294
lemma vimage_subset_eq: "bij f ==> (f -` B <= A) = (B <= f ` A)"
paulson@13585
   295
apply (unfold bij_def)
paulson@13585
   296
apply (blast del: subsetI intro: vimage_subsetI vimage_subsetD)
paulson@13585
   297
done
paulson@13585
   298
paulson@13585
   299
lemma image_Int_subset: "f`(A Int B) <= f`A Int f`B"
paulson@13585
   300
by blast
paulson@13585
   301
paulson@13585
   302
lemma image_diff_subset: "f`A - f`B <= f`(A - B)"
paulson@13585
   303
by blast
paulson@5852
   304
paulson@13585
   305
lemma inj_on_image_Int:
paulson@13585
   306
   "[| inj_on f C;  A<=C;  B<=C |] ==> f`(A Int B) = f`A Int f`B"
paulson@13585
   307
apply (simp add: inj_on_def, blast)
paulson@13585
   308
done
paulson@13585
   309
paulson@13585
   310
lemma inj_on_image_set_diff:
paulson@13585
   311
   "[| inj_on f C;  A<=C;  B<=C |] ==> f`(A-B) = f`A - f`B"
paulson@13585
   312
apply (simp add: inj_on_def, blast)
paulson@13585
   313
done
paulson@13585
   314
paulson@13585
   315
lemma image_Int: "inj f ==> f`(A Int B) = f`A Int f`B"
paulson@13585
   316
by (simp add: inj_on_def, blast)
paulson@13585
   317
paulson@13585
   318
lemma image_set_diff: "inj f ==> f`(A-B) = f`A - f`B"
paulson@13585
   319
by (simp add: inj_on_def, blast)
paulson@13585
   320
paulson@13585
   321
lemma inj_image_mem_iff: "inj f ==> (f a : f`A) = (a : A)"
paulson@13585
   322
by (blast dest: injD)
paulson@13585
   323
paulson@13585
   324
lemma inj_image_subset_iff: "inj f ==> (f`A <= f`B) = (A<=B)"
paulson@13585
   325
by (simp add: inj_on_def, blast)
paulson@13585
   326
paulson@13585
   327
lemma inj_image_eq_iff: "inj f ==> (f`A = f`B) = (A = B)"
paulson@13585
   328
by (blast dest: injD)
paulson@13585
   329
paulson@13585
   330
lemma image_UN: "(f ` (UNION A B)) = (UN x:A.(f ` (B x)))"
paulson@13585
   331
by blast
paulson@13585
   332
paulson@13585
   333
(*injectivity's required.  Left-to-right inclusion holds even if A is empty*)
paulson@13585
   334
lemma image_INT:
paulson@13585
   335
   "[| inj_on f C;  ALL x:A. B x <= C;  j:A |]
paulson@13585
   336
    ==> f ` (INTER A B) = (INT x:A. f ` B x)"
paulson@13585
   337
apply (simp add: inj_on_def, blast)
paulson@13585
   338
done
paulson@13585
   339
paulson@13585
   340
(*Compare with image_INT: no use of inj_on, and if f is surjective then
paulson@13585
   341
  it doesn't matter whether A is empty*)
paulson@13585
   342
lemma bij_image_INT: "bij f ==> f ` (INTER A B) = (INT x:A. f ` B x)"
paulson@13585
   343
apply (simp add: bij_def)
paulson@13585
   344
apply (simp add: inj_on_def surj_def, blast)
paulson@13585
   345
done
paulson@13585
   346
paulson@13585
   347
lemma surj_Compl_image_subset: "surj f ==> -(f`A) <= f`(-A)"
paulson@13585
   348
by (auto simp add: surj_def)
paulson@13585
   349
paulson@13585
   350
lemma inj_image_Compl_subset: "inj f ==> f`(-A) <= -(f`A)"
paulson@13585
   351
by (auto simp add: inj_on_def)
paulson@5852
   352
paulson@13585
   353
lemma bij_image_Compl_eq: "bij f ==> f`(-A) = -(f`A)"
paulson@13585
   354
apply (simp add: bij_def)
paulson@13585
   355
apply (rule equalityI)
paulson@13585
   356
apply (simp_all (no_asm_simp) add: inj_image_Compl_subset surj_Compl_image_subset)
paulson@13585
   357
done
paulson@13585
   358
paulson@13585
   359
paulson@13585
   360
subsection{*Function Updating*}
paulson@13585
   361
paulson@13585
   362
lemma fun_upd_idem_iff: "(f(x:=y) = f) = (f x = y)"
paulson@13585
   363
apply (simp add: fun_upd_def, safe)
paulson@13585
   364
apply (erule subst)
paulson@13585
   365
apply (rule_tac [2] ext, auto)
paulson@13585
   366
done
paulson@13585
   367
paulson@13585
   368
(* f x = y ==> f(x:=y) = f *)
paulson@13585
   369
lemmas fun_upd_idem = fun_upd_idem_iff [THEN iffD2, standard]
paulson@13585
   370
paulson@13585
   371
(* f(x := f x) = f *)
paulson@17084
   372
lemmas fun_upd_triv = refl [THEN fun_upd_idem]
paulson@17084
   373
declare fun_upd_triv [iff]
paulson@13585
   374
paulson@13585
   375
lemma fun_upd_apply [simp]: "(f(x:=y))z = (if z=x then y else f z)"
paulson@17084
   376
by (simp add: fun_upd_def)
paulson@13585
   377
paulson@13585
   378
(* fun_upd_apply supersedes these two,   but they are useful
paulson@13585
   379
   if fun_upd_apply is intentionally removed from the simpset *)
paulson@13585
   380
lemma fun_upd_same: "(f(x:=y)) x = y"
paulson@13585
   381
by simp
paulson@13585
   382
paulson@13585
   383
lemma fun_upd_other: "z~=x ==> (f(x:=y)) z = f z"
paulson@13585
   384
by simp
paulson@13585
   385
paulson@13585
   386
lemma fun_upd_upd [simp]: "f(x:=y,x:=z) = f(x:=z)"
paulson@13585
   387
by (simp add: expand_fun_eq)
paulson@13585
   388
paulson@13585
   389
lemma fun_upd_twist: "a ~= c ==> (m(a:=b))(c:=d) = (m(c:=d))(a:=b)"
paulson@13585
   390
by (rule ext, auto)
paulson@13585
   391
nipkow@15303
   392
lemma inj_on_fun_updI: "\<lbrakk> inj_on f A; y \<notin> f`A \<rbrakk> \<Longrightarrow> inj_on (f(x:=y)) A"
nipkow@15303
   393
by(fastsimp simp:inj_on_def image_def)
nipkow@15303
   394
paulson@15510
   395
lemma fun_upd_image:
paulson@15510
   396
     "f(x:=y) ` A = (if x \<in> A then insert y (f ` (A-{x})) else f ` A)"
paulson@15510
   397
by auto
paulson@15510
   398
nipkow@15691
   399
subsection{* @{text override_on} *}
nipkow@13910
   400
nipkow@15691
   401
lemma override_on_emptyset[simp]: "override_on f g {} = f"
nipkow@15691
   402
by(simp add:override_on_def)
nipkow@13910
   403
nipkow@15691
   404
lemma override_on_apply_notin[simp]: "a ~: A ==> (override_on f g A) a = f a"
nipkow@15691
   405
by(simp add:override_on_def)
nipkow@13910
   406
nipkow@15691
   407
lemma override_on_apply_in[simp]: "a : A ==> (override_on f g A) a = g a"
nipkow@15691
   408
by(simp add:override_on_def)
nipkow@13910
   409
paulson@15510
   410
subsection{* swap *}
paulson@15510
   411
haftmann@22744
   412
definition
haftmann@22744
   413
  swap :: "'a \<Rightarrow> 'a \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b)"
haftmann@22744
   414
where
haftmann@22744
   415
  "swap a b f = f (a := f b, b:= f a)"
paulson@15510
   416
paulson@15510
   417
lemma swap_self: "swap a a f = f"
nipkow@15691
   418
by (simp add: swap_def)
paulson@15510
   419
paulson@15510
   420
lemma swap_commute: "swap a b f = swap b a f"
paulson@15510
   421
by (rule ext, simp add: fun_upd_def swap_def)
paulson@15510
   422
paulson@15510
   423
lemma swap_nilpotent [simp]: "swap a b (swap a b f) = f"
paulson@15510
   424
by (rule ext, simp add: fun_upd_def swap_def)
paulson@15510
   425
paulson@15510
   426
lemma inj_on_imp_inj_on_swap:
haftmann@22744
   427
  "[|inj_on f A; a \<in> A; b \<in> A|] ==> inj_on (swap a b f) A"
paulson@15510
   428
by (simp add: inj_on_def swap_def, blast)
paulson@15510
   429
paulson@15510
   430
lemma inj_on_swap_iff [simp]:
paulson@15510
   431
  assumes A: "a \<in> A" "b \<in> A" shows "inj_on (swap a b f) A = inj_on f A"
paulson@15510
   432
proof 
paulson@15510
   433
  assume "inj_on (swap a b f) A"
paulson@15510
   434
  with A have "inj_on (swap a b (swap a b f)) A" 
nipkow@17589
   435
    by (iprover intro: inj_on_imp_inj_on_swap) 
paulson@15510
   436
  thus "inj_on f A" by simp 
paulson@15510
   437
next
paulson@15510
   438
  assume "inj_on f A"
nipkow@17589
   439
  with A show "inj_on (swap a b f) A" by (iprover intro: inj_on_imp_inj_on_swap)
paulson@15510
   440
qed
paulson@15510
   441
paulson@15510
   442
lemma surj_imp_surj_swap: "surj f ==> surj (swap a b f)"
paulson@15510
   443
apply (simp add: surj_def swap_def, clarify)
paulson@15510
   444
apply (rule_tac P = "y = f b" in case_split_thm, blast)
paulson@15510
   445
apply (rule_tac P = "y = f a" in case_split_thm, auto)
paulson@15510
   446
  --{*We don't yet have @{text case_tac}*}
paulson@15510
   447
done
paulson@15510
   448
paulson@15510
   449
lemma surj_swap_iff [simp]: "surj (swap a b f) = surj f"
paulson@15510
   450
proof 
paulson@15510
   451
  assume "surj (swap a b f)"
paulson@15510
   452
  hence "surj (swap a b (swap a b f))" by (rule surj_imp_surj_swap) 
paulson@15510
   453
  thus "surj f" by simp 
paulson@15510
   454
next
paulson@15510
   455
  assume "surj f"
paulson@15510
   456
  thus "surj (swap a b f)" by (rule surj_imp_surj_swap) 
paulson@15510
   457
qed
paulson@15510
   458
paulson@15510
   459
lemma bij_swap_iff: "bij (swap a b f) = bij f"
paulson@15510
   460
by (simp add: bij_def)
haftmann@21547
   461
haftmann@21547
   462
haftmann@22845
   463
subsection {* Proof tool setup *} 
haftmann@22845
   464
haftmann@22845
   465
text {* simplifies terms of the form
haftmann@22845
   466
  f(...,x:=y,...,x:=z,...) to f(...,x:=z,...) *}
haftmann@22845
   467
wenzelm@24017
   468
simproc_setup fun_upd2 ("f(v := w, x := y)") = {* fn _ =>
haftmann@22845
   469
let
haftmann@22845
   470
  fun gen_fun_upd NONE T _ _ = NONE
wenzelm@24017
   471
    | gen_fun_upd (SOME f) T x y = SOME (Const (@{const_name fun_upd}, T) $ f $ x $ y)
haftmann@22845
   472
  fun dest_fun_T1 (Type (_, T :: Ts)) = T
haftmann@22845
   473
  fun find_double (t as Const (@{const_name fun_upd},T) $ f $ x $ y) =
haftmann@22845
   474
    let
haftmann@22845
   475
      fun find (Const (@{const_name fun_upd},T) $ g $ v $ w) =
haftmann@22845
   476
            if v aconv x then SOME g else gen_fun_upd (find g) T v w
haftmann@22845
   477
        | find t = NONE
haftmann@22845
   478
    in (dest_fun_T1 T, gen_fun_upd (find f) T x y) end
wenzelm@24017
   479
wenzelm@24017
   480
  fun proc ss ct =
wenzelm@24017
   481
    let
wenzelm@24017
   482
      val ctxt = Simplifier.the_context ss
wenzelm@24017
   483
      val t = Thm.term_of ct
wenzelm@24017
   484
    in
wenzelm@24017
   485
      case find_double t of
wenzelm@24017
   486
        (T, NONE) => NONE
wenzelm@24017
   487
      | (T, SOME rhs) =>
wenzelm@24017
   488
          SOME (Goal.prove ctxt [] [] (Term.equals T $ t $ rhs)
wenzelm@24017
   489
            (fn _ =>
wenzelm@24017
   490
              rtac eq_reflection 1 THEN
wenzelm@24017
   491
              rtac ext 1 THEN
wenzelm@24017
   492
              simp_tac (Simplifier.inherit_context ss @{simpset}) 1))
wenzelm@24017
   493
    end
wenzelm@24017
   494
in proc end
haftmann@22845
   495
*}
haftmann@22845
   496
haftmann@22845
   497
haftmann@21870
   498
subsection {* Code generator setup *}
haftmann@21870
   499
haftmann@21870
   500
code_const "op \<circ>"
haftmann@21870
   501
  (SML infixl 5 "o")
haftmann@21870
   502
  (Haskell infixr 9 ".")
haftmann@21870
   503
haftmann@21906
   504
code_const "id"
haftmann@21906
   505
  (Haskell "id")
haftmann@21906
   506
haftmann@21870
   507
haftmann@21547
   508
subsection {* ML legacy bindings *} 
paulson@15510
   509
haftmann@22845
   510
ML {*
haftmann@22845
   511
val set_cs = claset() delrules [equalityI]
haftmann@22845
   512
*}
paulson@5852
   513
haftmann@22845
   514
ML {*
haftmann@22845
   515
val id_apply = @{thm id_apply}
haftmann@22845
   516
val id_def = @{thm id_def}
haftmann@22845
   517
val o_apply = @{thm o_apply}
haftmann@22845
   518
val o_assoc = @{thm o_assoc}
haftmann@22845
   519
val o_def = @{thm o_def}
haftmann@22845
   520
val injD = @{thm injD}
haftmann@22845
   521
val datatype_injI = @{thm datatype_injI}
haftmann@22845
   522
val range_ex1_eq = @{thm range_ex1_eq}
haftmann@22845
   523
val expand_fun_eq = @{thm expand_fun_eq}
paulson@13585
   524
*}
paulson@5852
   525
nipkow@2912
   526
end