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