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