src/HOL/Fun.thy
author wenzelm
Thu Feb 11 23:00:22 2010 +0100 (2010-02-11)
changeset 35115 446c5063e4fd
parent 34209 c7f621786035
child 35416 d8d7d1b785af
permissions -rw-r--r--
modernized translations;
formal markup of @{syntax_const} and @{const_syntax};
minor tuning;
     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)" == "CONST 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 notation
   399   sum_case  (infixr "'(+')"80)
   400 *)
   401 
   402 lemma fun_upd_idem_iff: "(f(x:=y) = f) = (f x = y)"
   403 apply (simp add: fun_upd_def, safe)
   404 apply (erule subst)
   405 apply (rule_tac [2] ext, auto)
   406 done
   407 
   408 (* f x = y ==> f(x:=y) = f *)
   409 lemmas fun_upd_idem = fun_upd_idem_iff [THEN iffD2, standard]
   410 
   411 (* f(x := f x) = f *)
   412 lemmas fun_upd_triv = refl [THEN fun_upd_idem]
   413 declare fun_upd_triv [iff]
   414 
   415 lemma fun_upd_apply [simp]: "(f(x:=y))z = (if z=x then y else f z)"
   416 by (simp add: fun_upd_def)
   417 
   418 (* fun_upd_apply supersedes these two,   but they are useful
   419    if fun_upd_apply is intentionally removed from the simpset *)
   420 lemma fun_upd_same: "(f(x:=y)) x = y"
   421 by simp
   422 
   423 lemma fun_upd_other: "z~=x ==> (f(x:=y)) z = f z"
   424 by simp
   425 
   426 lemma fun_upd_upd [simp]: "f(x:=y,x:=z) = f(x:=z)"
   427 by (simp add: expand_fun_eq)
   428 
   429 lemma fun_upd_twist: "a ~= c ==> (m(a:=b))(c:=d) = (m(c:=d))(a:=b)"
   430 by (rule ext, auto)
   431 
   432 lemma inj_on_fun_updI: "\<lbrakk> inj_on f A; y \<notin> f`A \<rbrakk> \<Longrightarrow> inj_on (f(x:=y)) A"
   433 by (fastsimp simp:inj_on_def image_def)
   434 
   435 lemma fun_upd_image:
   436      "f(x:=y) ` A = (if x \<in> A then insert y (f ` (A-{x})) else f ` A)"
   437 by auto
   438 
   439 lemma fun_upd_comp: "f \<circ> (g(x := y)) = (f \<circ> g)(x := f y)"
   440 by (auto intro: ext)
   441 
   442 
   443 subsection {* @{text override_on} *}
   444 
   445 definition
   446   override_on :: "('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'a set \<Rightarrow> 'a \<Rightarrow> 'b"
   447 where
   448   "override_on f g A = (\<lambda>a. if a \<in> A then g a else f a)"
   449 
   450 lemma override_on_emptyset[simp]: "override_on f g {} = f"
   451 by(simp add:override_on_def)
   452 
   453 lemma override_on_apply_notin[simp]: "a ~: A ==> (override_on f g A) a = f a"
   454 by(simp add:override_on_def)
   455 
   456 lemma override_on_apply_in[simp]: "a : A ==> (override_on f g A) a = g a"
   457 by(simp add:override_on_def)
   458 
   459 
   460 subsection {* @{text swap} *}
   461 
   462 definition
   463   swap :: "'a \<Rightarrow> 'a \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b)"
   464 where
   465   "swap a b f = f (a := f b, b:= f a)"
   466 
   467 lemma swap_self [simp]: "swap a a f = f"
   468 by (simp add: swap_def)
   469 
   470 lemma swap_commute: "swap a b f = swap b a f"
   471 by (rule ext, simp add: fun_upd_def swap_def)
   472 
   473 lemma swap_nilpotent [simp]: "swap a b (swap a b f) = f"
   474 by (rule ext, simp add: fun_upd_def swap_def)
   475 
   476 lemma swap_triple:
   477   assumes "a \<noteq> c" and "b \<noteq> c"
   478   shows "swap a b (swap b c (swap a b f)) = swap a c f"
   479   using assms by (simp add: expand_fun_eq swap_def)
   480 
   481 lemma comp_swap: "f \<circ> swap a b g = swap a b (f \<circ> g)"
   482 by (rule ext, simp add: fun_upd_def swap_def)
   483 
   484 lemma inj_on_imp_inj_on_swap:
   485   "[|inj_on f A; a \<in> A; b \<in> A|] ==> inj_on (swap a b f) A"
   486 by (simp add: inj_on_def swap_def, blast)
   487 
   488 lemma inj_on_swap_iff [simp]:
   489   assumes A: "a \<in> A" "b \<in> A" shows "inj_on (swap a b f) A = inj_on f A"
   490 proof 
   491   assume "inj_on (swap a b f) A"
   492   with A have "inj_on (swap a b (swap a b f)) A" 
   493     by (iprover intro: inj_on_imp_inj_on_swap) 
   494   thus "inj_on f A" by simp 
   495 next
   496   assume "inj_on f A"
   497   with A show "inj_on (swap a b f) A" by (iprover intro: inj_on_imp_inj_on_swap)
   498 qed
   499 
   500 lemma surj_imp_surj_swap: "surj f ==> surj (swap a b f)"
   501 apply (simp add: surj_def swap_def, clarify)
   502 apply (case_tac "y = f b", blast)
   503 apply (case_tac "y = f a", auto)
   504 done
   505 
   506 lemma surj_swap_iff [simp]: "surj (swap a b f) = surj f"
   507 proof 
   508   assume "surj (swap a b f)"
   509   hence "surj (swap a b (swap a b f))" by (rule surj_imp_surj_swap) 
   510   thus "surj f" by simp 
   511 next
   512   assume "surj f"
   513   thus "surj (swap a b f)" by (rule surj_imp_surj_swap) 
   514 qed
   515 
   516 lemma bij_swap_iff: "bij (swap a b f) = bij f"
   517 by (simp add: bij_def)
   518 
   519 hide (open) const swap
   520 
   521 
   522 subsection {* Inversion of injective functions *}
   523 
   524 definition the_inv_into :: "'a set => ('a => 'b) => ('b => 'a)" where
   525 "the_inv_into A f == %x. THE y. y : A & f y = x"
   526 
   527 lemma the_inv_into_f_f:
   528   "[| inj_on f A;  x : A |] ==> the_inv_into A f (f x) = x"
   529 apply (simp add: the_inv_into_def inj_on_def)
   530 apply blast
   531 done
   532 
   533 lemma f_the_inv_into_f:
   534   "inj_on f A ==> y : f`A  ==> f (the_inv_into A f y) = y"
   535 apply (simp add: the_inv_into_def)
   536 apply (rule the1I2)
   537  apply(blast dest: inj_onD)
   538 apply blast
   539 done
   540 
   541 lemma the_inv_into_into:
   542   "[| inj_on f A; x : f ` A; A <= B |] ==> the_inv_into A f x : B"
   543 apply (simp add: the_inv_into_def)
   544 apply (rule the1I2)
   545  apply(blast dest: inj_onD)
   546 apply blast
   547 done
   548 
   549 lemma the_inv_into_onto[simp]:
   550   "inj_on f A ==> the_inv_into A f ` (f ` A) = A"
   551 by (fast intro:the_inv_into_into the_inv_into_f_f[symmetric])
   552 
   553 lemma the_inv_into_f_eq:
   554   "[| inj_on f A; f x = y; x : A |] ==> the_inv_into A f y = x"
   555   apply (erule subst)
   556   apply (erule the_inv_into_f_f, assumption)
   557   done
   558 
   559 lemma the_inv_into_comp:
   560   "[| inj_on f (g ` A); inj_on g A; x : f ` g ` A |] ==>
   561   the_inv_into A (f o g) x = (the_inv_into A g o the_inv_into (g ` A) f) x"
   562 apply (rule the_inv_into_f_eq)
   563   apply (fast intro: comp_inj_on)
   564  apply (simp add: f_the_inv_into_f the_inv_into_into)
   565 apply (simp add: the_inv_into_into)
   566 done
   567 
   568 lemma inj_on_the_inv_into:
   569   "inj_on f A \<Longrightarrow> inj_on (the_inv_into A f) (f ` A)"
   570 by (auto intro: inj_onI simp: image_def the_inv_into_f_f)
   571 
   572 lemma bij_betw_the_inv_into:
   573   "bij_betw f A B \<Longrightarrow> bij_betw (the_inv_into A f) B A"
   574 by (auto simp add: bij_betw_def inj_on_the_inv_into the_inv_into_into)
   575 
   576 abbreviation the_inv :: "('a \<Rightarrow> 'b) \<Rightarrow> ('b \<Rightarrow> 'a)" where
   577   "the_inv f \<equiv> the_inv_into UNIV f"
   578 
   579 lemma the_inv_f_f:
   580   assumes "inj f"
   581   shows "the_inv f (f x) = x" using assms UNIV_I
   582   by (rule the_inv_into_f_f)
   583 
   584 
   585 subsection {* Proof tool setup *} 
   586 
   587 text {* simplifies terms of the form
   588   f(...,x:=y,...,x:=z,...) to f(...,x:=z,...) *}
   589 
   590 simproc_setup fun_upd2 ("f(v := w, x := y)") = {* fn _ =>
   591 let
   592   fun gen_fun_upd NONE T _ _ = NONE
   593     | gen_fun_upd (SOME f) T x y = SOME (Const (@{const_name fun_upd}, T) $ f $ x $ y)
   594   fun dest_fun_T1 (Type (_, T :: Ts)) = T
   595   fun find_double (t as Const (@{const_name fun_upd},T) $ f $ x $ y) =
   596     let
   597       fun find (Const (@{const_name fun_upd},T) $ g $ v $ w) =
   598             if v aconv x then SOME g else gen_fun_upd (find g) T v w
   599         | find t = NONE
   600     in (dest_fun_T1 T, gen_fun_upd (find f) T x y) end
   601 
   602   fun proc ss ct =
   603     let
   604       val ctxt = Simplifier.the_context ss
   605       val t = Thm.term_of ct
   606     in
   607       case find_double t of
   608         (T, NONE) => NONE
   609       | (T, SOME rhs) =>
   610           SOME (Goal.prove ctxt [] [] (Logic.mk_equals (t, rhs))
   611             (fn _ =>
   612               rtac eq_reflection 1 THEN
   613               rtac ext 1 THEN
   614               simp_tac (Simplifier.inherit_context ss @{simpset}) 1))
   615     end
   616 in proc end
   617 *}
   618 
   619 
   620 subsection {* Code generator setup *}
   621 
   622 types_code
   623   "fun"  ("(_ ->/ _)")
   624 attach (term_of) {*
   625 fun term_of_fun_type _ aT _ bT _ = Free ("<function>", aT --> bT);
   626 *}
   627 attach (test) {*
   628 fun gen_fun_type aF aT bG bT i =
   629   let
   630     val tab = Unsynchronized.ref [];
   631     fun mk_upd (x, (_, y)) t = Const ("Fun.fun_upd",
   632       (aT --> bT) --> aT --> bT --> aT --> bT) $ t $ aF x $ y ()
   633   in
   634     (fn x =>
   635        case AList.lookup op = (!tab) x of
   636          NONE =>
   637            let val p as (y, _) = bG i
   638            in (tab := (x, p) :: !tab; y) end
   639        | SOME (y, _) => y,
   640      fn () => Basics.fold mk_upd (!tab) (Const ("HOL.undefined", aT --> bT)))
   641   end;
   642 *}
   643 
   644 code_const "op \<circ>"
   645   (SML infixl 5 "o")
   646   (Haskell infixr 9 ".")
   647 
   648 code_const "id"
   649   (Haskell "id")
   650 
   651 end