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