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