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