2 Author: Tobias Nipkow, Cambridge University Computer Laboratory
3 Copyright 1994 University of Cambridge
6 header {* Notions about functions *}
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)"
16 apply (simp (no_asm_simp))
18 apply (simp (no_asm_simp))
22 "f x = u \<Longrightarrow> (\<And>x. P x \<Longrightarrow> g (f x) = x) \<Longrightarrow> P x \<Longrightarrow> x = g u"
26 subsection {* The Identity Function @{text id} *}
29 id :: "'a \<Rightarrow> 'a"
31 "id = (\<lambda>x. x)"
33 lemma id_apply [simp]: "id x = x"
36 lemma image_ident [simp]: "(%x. x) ` Y = Y"
39 lemma image_id [simp]: "id ` Y = Y"
42 lemma vimage_ident [simp]: "(%x. x) -` Y = Y"
45 lemma vimage_id [simp]: "id -` A = A"
49 subsection {* The Composition Operator @{text "f \<circ> g"} *}
52 comp :: "('b \<Rightarrow> 'c) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'c" (infixl "o" 55)
54 "f o g = (\<lambda>x. f (g x))"
57 comp (infixl "\<circ>" 55)
59 notation (HTML output)
60 comp (infixl "\<circ>" 55)
63 lemmas o_def = comp_def
65 lemma o_apply [simp]: "(f o g) x = f (g x)"
66 by (simp add: comp_def)
68 lemma o_assoc: "f o (g o h) = f o g o h"
69 by (simp add: comp_def)
71 lemma id_o [simp]: "id o g = g"
72 by (simp add: comp_def)
74 lemma o_id [simp]: "f o id = f"
75 by (simp add: comp_def)
77 lemma image_compose: "(f o g) ` r = f`(g`r)"
78 by (simp add: comp_def, blast)
80 lemma UN_o: "UNION A (g o f) = UNION (f`A) g"
81 by (unfold comp_def, blast)
84 subsection {* The Forward Composition Operator @{text fcomp} *}
87 fcomp :: "('a \<Rightarrow> 'b) \<Rightarrow> ('b \<Rightarrow> 'c) \<Rightarrow> 'a \<Rightarrow> 'c" (infixl "o>" 60)
89 "f o> g = (\<lambda>x. g (f x))"
91 lemma fcomp_apply: "(f o> g) x = g (f x)"
92 by (simp add: fcomp_def)
94 lemma fcomp_assoc: "(f o> g) o> h = f o> (g o> h)"
95 by (simp add: fcomp_def)
97 lemma id_fcomp [simp]: "id o> g = g"
98 by (simp add: fcomp_def)
100 lemma fcomp_id [simp]: "f o> id = f"
101 by (simp add: fcomp_def)
106 no_notation fcomp (infixl "o>" 60)
109 subsection {* Injectivity and Surjectivity *}
112 inj_on :: "['a => 'b, 'a set] => bool" -- "injective"
113 "inj_on f A == ! x:A. ! y:A. f(x)=f(y) --> x=y"
115 text{*A common special case: functions injective over the entire domain type.*}
118 "inj f == inj_on f UNIV"
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"
125 surj :: "('a => 'b) => bool" (*surjective*)
126 "surj f == ! y. ? x. y=f(x)"
128 bij :: "('a => 'b) => bool" (*bijective*)
129 "bij f == inj f & surj f"
132 assumes "\<And>x y. f x = f y \<Longrightarrow> x = y"
134 using assms unfolding inj_on_def by auto
136 text{*For Proofs in @{text "Tools/datatype_rep_proofs"}*}
138 "(!! x. ALL y. f(x) = f(y) --> x=y) ==> inj(f)"
139 by (simp add: inj_on_def)
141 theorem range_ex1_eq: "inj f \<Longrightarrow> b : range f = (EX! x. b = f x)"
142 by (unfold inj_on_def, blast)
144 lemma injD: "[| inj(f); f(x) = f(y) |] ==> x=y"
145 by (simp add: inj_on_def)
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)
151 lemma inj_on_id[simp]: "inj_on id A"
152 by (simp add: inj_on_def)
154 lemma inj_on_id2[simp]: "inj_on (%x. x) A"
155 by (simp add: inj_on_def)
157 lemma surj_id[simp]: "surj id"
158 by (simp add: surj_def)
160 lemma bij_id[simp]: "bij id"
161 by (simp add: bij_def inj_on_id surj_id)
164 "(!! x y. [| x:A; y:A; f(x) = f(y) |] ==> x=y) ==> inj_on f A"
165 by (simp add: inj_on_def)
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)
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)
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)
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)
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)
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)
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)
194 lemma inj_singleton: "inj (%s. {s})"
195 by (simp add: inj_on_def)
197 lemma inj_on_empty[iff]: "inj_on f {}"
198 by(simp add: inj_on_def)
200 lemma subset_inj_on: "[| inj_on f B; A <= B |] ==> inj_on f A"
201 by (unfold inj_on_def, blast)
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)
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)
216 lemma inj_on_diff: "inj_on f A ==> inj_on f (A-B)"
217 apply(unfold inj_on_def)
221 lemma surjI: "(!! x. g(f x) = x) ==> surj g"
222 apply (simp add: surj_def)
223 apply (blast intro: sym)
226 lemma surj_range: "surj f ==> range f = UNIV"
227 by (auto simp add: surj_def)
229 lemma surjD: "surj f ==> EX x. y = f x"
230 by (simp add: surj_def)
232 lemma surjE: "surj f ==> (!!x. y = f x ==> C) ==> C"
233 by (simp add: surj_def, blast)
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)
241 lemma bijI: "[| inj f; surj f |] ==> bij f"
242 by (simp add: bij_def)
244 lemma bij_is_inj: "bij f ==> inj f"
245 by (simp add: bij_def)
247 lemma bij_is_surj: "bij f ==> surj f"
248 by (simp add: bij_def)
250 lemma bij_betw_imp_inj_on: "bij_betw f A B \<Longrightarrow> inj_on f A"
251 by (simp add: bij_betw_def)
253 lemma bij_betw_inv: assumes "bij_betw f A B" shows "EX g. bij_betw g B A"
255 have i: "inj_on f A" and s: "f ` A = B"
256 using assms by(auto simp:bij_betw_def)
257 let ?P = "%b a. a:A \<and> f a = b" let ?g = "%b. The (?P b)"
258 { fix a b assume P: "?P b a"
259 hence ex1: "\<exists>a. ?P b a" using s unfolding image_def by blast
260 hence uex1: "\<exists>!a. ?P b a" by(blast dest:inj_onD[OF i])
261 hence " ?g b = a" using the1_equality[OF uex1, OF P] P by simp
265 fix x y assume "x:B" "y:B" "?g x = ?g y"
266 from s `x:B` obtain a1 where a1: "?P x a1" unfolding image_def by blast
267 from s `y:B` obtain a2 where a2: "?P y a2" unfolding image_def by blast
268 from g[OF a1] a1 g[OF a2] a2 `?g x = ?g y` show "x=y" by simp
270 moreover have "?g ` B = A"
271 proof(auto simp:image_def)
273 with s obtain a where P: "?P b a" unfolding image_def by blast
274 thus "?g b \<in> A" using g[OF P] by auto
277 then obtain b where P: "?P b a" using s unfolding image_def by blast
278 then have "b:B" using s unfolding image_def by blast
279 with g[OF P] show "\<exists>b\<in>B. a = ?g b" by blast
281 ultimately show ?thesis by(auto simp:bij_betw_def)
284 lemma surj_image_vimage_eq: "surj f ==> f ` (f -` A) = A"
285 by (simp add: surj_range)
287 lemma inj_vimage_image_eq: "inj f ==> f -` (f ` A) = A"
288 by (simp add: inj_on_def, blast)
290 lemma vimage_subsetD: "surj f ==> f -` B <= A ==> B <= f ` A"
291 apply (unfold surj_def)
292 apply (blast intro: sym)
295 lemma vimage_subsetI: "inj f ==> B <= f ` A ==> f -` B <= A"
296 by (unfold inj_on_def, blast)
298 lemma vimage_subset_eq: "bij f ==> (f -` B <= A) = (B <= f ` A)"
299 apply (unfold bij_def)
300 apply (blast del: subsetI intro: vimage_subsetI vimage_subsetD)
303 lemma inj_on_image_Int:
304 "[| inj_on f C; A<=C; B<=C |] ==> f`(A Int B) = f`A Int f`B"
305 apply (simp add: inj_on_def, blast)
308 lemma inj_on_image_set_diff:
309 "[| inj_on f C; A<=C; B<=C |] ==> f`(A-B) = f`A - f`B"
310 apply (simp add: inj_on_def, blast)
313 lemma image_Int: "inj f ==> f`(A Int B) = f`A Int f`B"
314 by (simp add: inj_on_def, blast)
316 lemma image_set_diff: "inj f ==> f`(A-B) = f`A - f`B"
317 by (simp add: inj_on_def, blast)
319 lemma inj_image_mem_iff: "inj f ==> (f a : f`A) = (a : A)"
320 by (blast dest: injD)
322 lemma inj_image_subset_iff: "inj f ==> (f`A <= f`B) = (A<=B)"
323 by (simp add: inj_on_def, blast)
325 lemma inj_image_eq_iff: "inj f ==> (f`A = f`B) = (A = B)"
326 by (blast dest: injD)
328 (*injectivity's required. Left-to-right inclusion holds even if A is empty*)
330 "[| inj_on f C; ALL x:A. B x <= C; j:A |]
331 ==> f ` (INTER A B) = (INT x:A. f ` B x)"
332 apply (simp add: inj_on_def, blast)
335 (*Compare with image_INT: no use of inj_on, and if f is surjective then
336 it doesn't matter whether A is empty*)
337 lemma bij_image_INT: "bij f ==> f ` (INTER A B) = (INT x:A. f ` B x)"
338 apply (simp add: bij_def)
339 apply (simp add: inj_on_def surj_def, blast)
342 lemma surj_Compl_image_subset: "surj f ==> -(f`A) <= f`(-A)"
343 by (auto simp add: surj_def)
345 lemma inj_image_Compl_subset: "inj f ==> f`(-A) <= -(f`A)"
346 by (auto simp add: inj_on_def)
348 lemma bij_image_Compl_eq: "bij f ==> f`(-A) = -(f`A)"
349 apply (simp add: bij_def)
350 apply (rule equalityI)
351 apply (simp_all (no_asm_simp) add: inj_image_Compl_subset surj_Compl_image_subset)
355 subsection{*Function Updating*}
358 fun_upd :: "('a => 'b) => 'a => 'b => ('a => 'b)"
359 "fun_upd f a b == % x. if x=a then b else f x"
364 "_updbind" :: "['a, 'a] => updbind" ("(2_ :=/ _)")
365 "" :: "updbind => updbinds" ("_")
366 "_updbinds":: "[updbind, updbinds] => updbinds" ("_,/ _")
367 "_Update" :: "['a, updbinds] => 'a" ("_/'((_)')" [1000,0] 900)
370 "_Update f (_updbinds b bs)" == "_Update (_Update f b) bs"
371 "f(x:=y)" == "fun_upd f x y"
373 (* Hint: to define the sum of two functions (or maps), use sum_case.
374 A nice infix syntax could be defined (in Datatype.thy or below) by
376 fun_sum :: "('a => 'c) => ('b => 'c) => (('a+'b) => 'c)" (infixr "'(+')"80)
378 "fun_sum" == sum_case
381 lemma fun_upd_idem_iff: "(f(x:=y) = f) = (f x = y)"
382 apply (simp add: fun_upd_def, safe)
384 apply (rule_tac [2] ext, auto)
387 (* f x = y ==> f(x:=y) = f *)
388 lemmas fun_upd_idem = fun_upd_idem_iff [THEN iffD2, standard]
390 (* f(x := f x) = f *)
391 lemmas fun_upd_triv = refl [THEN fun_upd_idem]
392 declare fun_upd_triv [iff]
394 lemma fun_upd_apply [simp]: "(f(x:=y))z = (if z=x then y else f z)"
395 by (simp add: fun_upd_def)
397 (* fun_upd_apply supersedes these two, but they are useful
398 if fun_upd_apply is intentionally removed from the simpset *)
399 lemma fun_upd_same: "(f(x:=y)) x = y"
402 lemma fun_upd_other: "z~=x ==> (f(x:=y)) z = f z"
405 lemma fun_upd_upd [simp]: "f(x:=y,x:=z) = f(x:=z)"
406 by (simp add: expand_fun_eq)
408 lemma fun_upd_twist: "a ~= c ==> (m(a:=b))(c:=d) = (m(c:=d))(a:=b)"
411 lemma inj_on_fun_updI: "\<lbrakk> inj_on f A; y \<notin> f`A \<rbrakk> \<Longrightarrow> inj_on (f(x:=y)) A"
412 by(fastsimp simp:inj_on_def image_def)
415 "f(x:=y) ` A = (if x \<in> A then insert y (f ` (A-{x})) else f ` A)"
418 lemma fun_upd_comp: "f \<circ> (g(x := y)) = (f \<circ> g)(x := f y)"
422 subsection {* @{text override_on} *}
425 override_on :: "('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'a set \<Rightarrow> 'a \<Rightarrow> 'b"
427 "override_on f g A = (\<lambda>a. if a \<in> A then g a else f a)"
429 lemma override_on_emptyset[simp]: "override_on f g {} = f"
430 by(simp add:override_on_def)
432 lemma override_on_apply_notin[simp]: "a ~: A ==> (override_on f g A) a = f a"
433 by(simp add:override_on_def)
435 lemma override_on_apply_in[simp]: "a : A ==> (override_on f g A) a = g a"
436 by(simp add:override_on_def)
439 subsection {* @{text swap} *}
442 swap :: "'a \<Rightarrow> 'a \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b)"
444 "swap a b f = f (a := f b, b:= f a)"
446 lemma swap_self: "swap a a f = f"
447 by (simp add: swap_def)
449 lemma swap_commute: "swap a b f = swap b a f"
450 by (rule ext, simp add: fun_upd_def swap_def)
452 lemma swap_nilpotent [simp]: "swap a b (swap a b f) = f"
453 by (rule ext, simp add: fun_upd_def swap_def)
455 lemma inj_on_imp_inj_on_swap:
456 "[|inj_on f A; a \<in> A; b \<in> A|] ==> inj_on (swap a b f) A"
457 by (simp add: inj_on_def swap_def, blast)
459 lemma inj_on_swap_iff [simp]:
460 assumes A: "a \<in> A" "b \<in> A" shows "inj_on (swap a b f) A = inj_on f A"
462 assume "inj_on (swap a b f) A"
463 with A have "inj_on (swap a b (swap a b f)) A"
464 by (iprover intro: inj_on_imp_inj_on_swap)
465 thus "inj_on f A" by simp
468 with A show "inj_on (swap a b f) A" by(iprover intro: inj_on_imp_inj_on_swap)
471 lemma surj_imp_surj_swap: "surj f ==> surj (swap a b f)"
472 apply (simp add: surj_def swap_def, clarify)
473 apply (case_tac "y = f b", blast)
474 apply (case_tac "y = f a", auto)
477 lemma surj_swap_iff [simp]: "surj (swap a b f) = surj f"
479 assume "surj (swap a b f)"
480 hence "surj (swap a b (swap a b f))" by (rule surj_imp_surj_swap)
481 thus "surj f" by simp
484 thus "surj (swap a b f)" by (rule surj_imp_surj_swap)
487 lemma bij_swap_iff: "bij (swap a b f) = bij f"
488 by (simp add: bij_def)
490 hide (open) const swap
492 subsection {* Proof tool setup *}
494 text {* simplifies terms of the form
495 f(...,x:=y,...,x:=z,...) to f(...,x:=z,...) *}
497 simproc_setup fun_upd2 ("f(v := w, x := y)") = {* fn _ =>
499 fun gen_fun_upd NONE T _ _ = NONE
500 | gen_fun_upd (SOME f) T x y = SOME (Const (@{const_name fun_upd}, T) $ f $ x $ y)
501 fun dest_fun_T1 (Type (_, T :: Ts)) = T
502 fun find_double (t as Const (@{const_name fun_upd},T) $ f $ x $ y) =
504 fun find (Const (@{const_name fun_upd},T) $ g $ v $ w) =
505 if v aconv x then SOME g else gen_fun_upd (find g) T v w
507 in (dest_fun_T1 T, gen_fun_upd (find f) T x y) end
511 val ctxt = Simplifier.the_context ss
512 val t = Thm.term_of ct
514 case find_double t of
517 SOME (Goal.prove ctxt [] [] (Logic.mk_equals (t, rhs))
519 rtac eq_reflection 1 THEN
521 simp_tac (Simplifier.inherit_context ss @{simpset}) 1))
527 subsection {* Code generator setup *}
532 fun term_of_fun_type _ aT _ bT _ = Free ("<function>", aT --> bT);
535 fun gen_fun_type aF aT bG bT i =
538 fun mk_upd (x, (_, y)) t = Const ("Fun.fun_upd",
539 (aT --> bT) --> aT --> bT --> aT --> bT) $ t $ aF x $ y ()
542 case AList.lookup op = (!tab) x of
544 let val p as (y, _) = bG i
545 in (tab := (x, p) :: !tab; y) end
547 fn () => Basics.fold mk_upd (!tab) (Const ("HOL.undefined", aT --> bT)))
551 code_const "op \<circ>"
553 (Haskell infixr 9 ".")