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