src/HOL/Fun.thy
 author wenzelm Thu Oct 04 20:29:42 2007 +0200 (2007-10-04) changeset 24850 0cfd722ab579 parent 24286 7619080e49f0 child 25886 7753e0d81b7a permissions -rw-r--r--
Name.uu, Name.aT;
1 (*  Title:      HOL/Fun.thy
2     ID:         \$Id\$
3     Author:     Tobias Nipkow, Cambridge University Computer Laboratory
4     Copyright   1994  University of Cambridge
5 *)
9 theory Fun
10 imports Set
11 begin
13 constdefs
14   fun_upd :: "('a => 'b) => 'a => 'b => ('a => 'b)"
15   "fun_upd f a b == % x. if x=a then b else f x"
17 nonterminals
18   updbinds updbind
19 syntax
20   "_updbind" :: "['a, 'a] => updbind"             ("(2_ :=/ _)")
21   ""         :: "updbind => updbinds"             ("_")
22   "_updbinds":: "[updbind, updbinds] => updbinds" ("_,/ _")
23   "_Update"  :: "['a, updbinds] => 'a"            ("_/'((_)')" [1000,0] 900)
25 translations
26   "_Update f (_updbinds b bs)"  == "_Update (_Update f b) bs"
27   "f(x:=y)"                     == "fun_upd f x y"
29 (* Hint: to define the sum of two functions (or maps), use sum_case.
30          A nice infix syntax could be defined (in Datatype.thy or below) by
31 consts
32   fun_sum :: "('a => 'c) => ('b => 'c) => (('a+'b) => 'c)" (infixr "'(+')"80)
33 translations
34  "fun_sum" == sum_case
35 *)
37 definition
38   override_on :: "('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'a set \<Rightarrow> 'a \<Rightarrow> 'b"
39 where
40   "override_on f g A = (\<lambda>a. if a \<in> A then g a else f a)"
42 definition
43   id :: "'a \<Rightarrow> 'a"
44 where
45   "id = (\<lambda>x. x)"
47 definition
48   comp :: "('b \<Rightarrow> 'c) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'c" (infixl "o" 55)
49 where
50   "f o g = (\<lambda>x. f (g x))"
52 notation (xsymbols)
53   comp  (infixl "\<circ>" 55)
55 notation (HTML output)
56   comp  (infixl "\<circ>" 55)
58 text{*compatibility*}
59 lemmas o_def = comp_def
61 constdefs
62   inj_on :: "['a => 'b, 'a set] => bool"         (*injective*)
63   "inj_on f A == ! x:A. ! y:A. f(x)=f(y) --> x=y"
65 text{*A common special case: functions injective over the entire domain type.*}
67 abbreviation
68   "inj f == inj_on f UNIV"
70 constdefs
71   surj :: "('a => 'b) => bool"                   (*surjective*)
72   "surj f == ! y. ? x. y=f(x)"
74   bij :: "('a => 'b) => bool"                    (*bijective*)
75   "bij f == inj f & surj f"
79 text{*As a simplification rule, it replaces all function equalities by
80   first-order equalities.*}
81 lemma expand_fun_eq: "f = g \<longleftrightarrow> (\<forall>x. f x = g x)"
82 apply (rule iffI)
83 apply (simp (no_asm_simp))
84 apply (rule ext)
85 apply (simp (no_asm_simp))
86 done
88 lemma apply_inverse:
89     "[| f(x)=u;  !!x. P(x) ==> g(f(x)) = x;  P(x) |] ==> x=g(u)"
90 by auto
93 text{*The Identity Function: @{term id}*}
94 lemma id_apply [simp]: "id x = x"
97 lemma inj_on_id[simp]: "inj_on id A"
100 lemma inj_on_id2[simp]: "inj_on (%x. x) A"
103 lemma surj_id[simp]: "surj id"
106 lemma bij_id[simp]: "bij id"
107 by (simp add: bij_def inj_on_id surj_id)
111 subsection{*The Composition Operator: @{term "f \<circ> g"}*}
113 lemma o_apply [simp]: "(f o g) x = f (g x)"
116 lemma o_assoc: "f o (g o h) = f o g o h"
119 lemma id_o [simp]: "id o g = g"
122 lemma o_id [simp]: "f o id = f"
125 lemma image_compose: "(f o g) ` r = f`(g`r)"
126 by (simp add: comp_def, blast)
128 lemma image_eq_UN: "f`A = (UN x:A. {f x})"
129 by blast
131 lemma UN_o: "UNION A (g o f) = UNION (f`A) g"
132 by (unfold comp_def, blast)
135 subsection{*The Injectivity Predicate, @{term inj}*}
137 text{*NB: @{term inj} now just translates to @{term inj_on}*}
140 text{*For Proofs in @{text "Tools/datatype_rep_proofs"}*}
141 lemma datatype_injI:
142     "(!! x. ALL y. f(x) = f(y) --> x=y) ==> inj(f)"
145 theorem range_ex1_eq: "inj f \<Longrightarrow> b : range f = (EX! x. b = f x)"
146   by (unfold inj_on_def, blast)
148 lemma injD: "[| inj(f); f(x) = f(y) |] ==> x=y"
151 (*Useful with the simplifier*)
152 lemma inj_eq: "inj(f) ==> (f(x) = f(y)) = (x=y)"
153 by (force simp add: inj_on_def)
156 subsection{*The Predicate @{term inj_on}: Injectivity On A Restricted Domain*}
158 lemma inj_onI:
159     "(!! x y. [|  x:A;  y:A;  f(x) = f(y) |] ==> x=y) ==> inj_on f A"
162 lemma inj_on_inverseI: "(!!x. x:A ==> g(f(x)) = x) ==> inj_on f A"
163 by (auto dest:  arg_cong [of concl: g] simp add: inj_on_def)
165 lemma inj_onD: "[| inj_on f A;  f(x)=f(y);  x:A;  y:A |] ==> x=y"
166 by (unfold inj_on_def, blast)
168 lemma inj_on_iff: "[| inj_on f A;  x:A;  y:A |] ==> (f(x)=f(y)) = (x=y)"
169 by (blast dest!: inj_onD)
171 lemma comp_inj_on:
172      "[| inj_on f A;  inj_on g (f`A) |] ==> inj_on (g o f) A"
173 by (simp add: comp_def inj_on_def)
175 lemma inj_on_imageI: "inj_on (g o f) A \<Longrightarrow> inj_on g (f ` A)"
177 apply blast
178 done
180 lemma inj_on_image_iff: "\<lbrakk> ALL x:A. ALL y:A. (g(f x) = g(f y)) = (g x = g y);
181   inj_on f A \<rbrakk> \<Longrightarrow> inj_on g (f ` A) = inj_on g A"
182 apply(unfold inj_on_def)
183 apply blast
184 done
186 lemma inj_on_contraD: "[| inj_on f A;  ~x=y;  x:A;  y:A |] ==> ~ f(x)=f(y)"
187 by (unfold inj_on_def, blast)
189 lemma inj_singleton: "inj (%s. {s})"
192 lemma inj_on_empty[iff]: "inj_on f {}"
195 lemma subset_inj_on: "[| inj_on f B; A <= B |] ==> inj_on f A"
196 by (unfold inj_on_def, blast)
198 lemma inj_on_Un:
199  "inj_on f (A Un B) =
200   (inj_on f A & inj_on f B & f`(A-B) Int f`(B-A) = {})"
201 apply(unfold inj_on_def)
202 apply (blast intro:sym)
203 done
205 lemma inj_on_insert[iff]:
206   "inj_on f (insert a A) = (inj_on f A & f a ~: f`(A-{a}))"
207 apply(unfold inj_on_def)
208 apply (blast intro:sym)
209 done
211 lemma inj_on_diff: "inj_on f A ==> inj_on f (A-B)"
212 apply(unfold inj_on_def)
213 apply (blast)
214 done
217 subsection{*The Predicate @{term surj}: Surjectivity*}
219 lemma surjI: "(!! x. g(f x) = x) ==> surj g"
221 apply (blast intro: sym)
222 done
224 lemma surj_range: "surj f ==> range f = UNIV"
225 by (auto simp add: surj_def)
227 lemma surjD: "surj f ==> EX x. y = f x"
230 lemma surjE: "surj f ==> (!!x. y = f x ==> C) ==> C"
231 by (simp add: surj_def, blast)
233 lemma comp_surj: "[| surj f;  surj g |] ==> surj (g o f)"
234 apply (simp add: comp_def surj_def, clarify)
235 apply (drule_tac x = y in spec, clarify)
236 apply (drule_tac x = x in spec, blast)
237 done
241 subsection{*The Predicate @{term bij}: Bijectivity*}
243 lemma bijI: "[| inj f; surj f |] ==> bij f"
246 lemma bij_is_inj: "bij f ==> inj f"
249 lemma bij_is_surj: "bij f ==> surj f"
253 subsection{*Facts About the Identity Function*}
255 text{*We seem to need both the @{term id} forms and the @{term "\<lambda>x. x"}
256 forms. The latter can arise by rewriting, while @{term id} may be used
257 explicitly.*}
259 lemma image_ident [simp]: "(%x. x) ` Y = Y"
260 by blast
262 lemma image_id [simp]: "id ` Y = Y"
265 lemma vimage_ident [simp]: "(%x. x) -` Y = Y"
266 by blast
268 lemma vimage_id [simp]: "id -` A = A"
271 lemma vimage_image_eq [noatp]: "f -` (f ` A) = {y. EX x:A. f x = f y}"
272 by (blast intro: sym)
274 lemma image_vimage_subset: "f ` (f -` A) <= A"
275 by blast
277 lemma image_vimage_eq [simp]: "f ` (f -` A) = A Int range f"
278 by blast
280 lemma surj_image_vimage_eq: "surj f ==> f ` (f -` A) = A"
283 lemma inj_vimage_image_eq: "inj f ==> f -` (f ` A) = A"
284 by (simp add: inj_on_def, blast)
286 lemma vimage_subsetD: "surj f ==> f -` B <= A ==> B <= f ` A"
287 apply (unfold surj_def)
288 apply (blast intro: sym)
289 done
291 lemma vimage_subsetI: "inj f ==> B <= f ` A ==> f -` B <= A"
292 by (unfold inj_on_def, blast)
294 lemma vimage_subset_eq: "bij f ==> (f -` B <= A) = (B <= f ` A)"
295 apply (unfold bij_def)
296 apply (blast del: subsetI intro: vimage_subsetI vimage_subsetD)
297 done
299 lemma image_Int_subset: "f`(A Int B) <= f`A Int f`B"
300 by blast
302 lemma image_diff_subset: "f`A - f`B <= f`(A - B)"
303 by blast
305 lemma inj_on_image_Int:
306    "[| inj_on f C;  A<=C;  B<=C |] ==> f`(A Int B) = f`A Int f`B"
307 apply (simp add: inj_on_def, blast)
308 done
310 lemma inj_on_image_set_diff:
311    "[| inj_on f C;  A<=C;  B<=C |] ==> f`(A-B) = f`A - f`B"
312 apply (simp add: inj_on_def, blast)
313 done
315 lemma image_Int: "inj f ==> f`(A Int B) = f`A Int f`B"
316 by (simp add: inj_on_def, blast)
318 lemma image_set_diff: "inj f ==> f`(A-B) = f`A - f`B"
319 by (simp add: inj_on_def, blast)
321 lemma inj_image_mem_iff: "inj f ==> (f a : f`A) = (a : A)"
322 by (blast dest: injD)
324 lemma inj_image_subset_iff: "inj f ==> (f`A <= f`B) = (A<=B)"
325 by (simp add: inj_on_def, blast)
327 lemma inj_image_eq_iff: "inj f ==> (f`A = f`B) = (A = B)"
328 by (blast dest: injD)
330 lemma image_UN: "(f ` (UNION A B)) = (UN x:A.(f ` (B x)))"
331 by blast
333 (*injectivity's required.  Left-to-right inclusion holds even if A is empty*)
334 lemma image_INT:
335    "[| inj_on f C;  ALL x:A. B x <= C;  j:A |]
336     ==> f ` (INTER A B) = (INT x:A. f ` B x)"
337 apply (simp add: inj_on_def, blast)
338 done
340 (*Compare with image_INT: no use of inj_on, and if f is surjective then
341   it doesn't matter whether A is empty*)
342 lemma bij_image_INT: "bij f ==> f ` (INTER A B) = (INT x:A. f ` B x)"
344 apply (simp add: inj_on_def surj_def, blast)
345 done
347 lemma surj_Compl_image_subset: "surj f ==> -(f`A) <= f`(-A)"
348 by (auto simp add: surj_def)
350 lemma inj_image_Compl_subset: "inj f ==> f`(-A) <= -(f`A)"
351 by (auto simp add: inj_on_def)
353 lemma bij_image_Compl_eq: "bij f ==> f`(-A) = -(f`A)"
355 apply (rule equalityI)
356 apply (simp_all (no_asm_simp) add: inj_image_Compl_subset surj_Compl_image_subset)
357 done
360 subsection{*Function Updating*}
362 lemma fun_upd_idem_iff: "(f(x:=y) = f) = (f x = y)"
363 apply (simp add: fun_upd_def, safe)
364 apply (erule subst)
365 apply (rule_tac [2] ext, auto)
366 done
368 (* f x = y ==> f(x:=y) = f *)
369 lemmas fun_upd_idem = fun_upd_idem_iff [THEN iffD2, standard]
371 (* f(x := f x) = f *)
372 lemmas fun_upd_triv = refl [THEN fun_upd_idem]
373 declare fun_upd_triv [iff]
375 lemma fun_upd_apply [simp]: "(f(x:=y))z = (if z=x then y else f z)"
378 (* fun_upd_apply supersedes these two,   but they are useful
379    if fun_upd_apply is intentionally removed from the simpset *)
380 lemma fun_upd_same: "(f(x:=y)) x = y"
381 by simp
383 lemma fun_upd_other: "z~=x ==> (f(x:=y)) z = f z"
384 by simp
386 lemma fun_upd_upd [simp]: "f(x:=y,x:=z) = f(x:=z)"
389 lemma fun_upd_twist: "a ~= c ==> (m(a:=b))(c:=d) = (m(c:=d))(a:=b)"
390 by (rule ext, auto)
392 lemma inj_on_fun_updI: "\<lbrakk> inj_on f A; y \<notin> f`A \<rbrakk> \<Longrightarrow> inj_on (f(x:=y)) A"
393 by(fastsimp simp:inj_on_def image_def)
395 lemma fun_upd_image:
396      "f(x:=y) ` A = (if x \<in> A then insert y (f ` (A-{x})) else f ` A)"
397 by auto
399 subsection{* @{text override_on} *}
401 lemma override_on_emptyset[simp]: "override_on f g {} = f"
404 lemma override_on_apply_notin[simp]: "a ~: A ==> (override_on f g A) a = f a"
407 lemma override_on_apply_in[simp]: "a : A ==> (override_on f g A) a = g a"
410 subsection{* swap *}
412 definition
413   swap :: "'a \<Rightarrow> 'a \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b)"
414 where
415   "swap a b f = f (a := f b, b:= f a)"
417 lemma swap_self: "swap a a f = f"
420 lemma swap_commute: "swap a b f = swap b a f"
421 by (rule ext, simp add: fun_upd_def swap_def)
423 lemma swap_nilpotent [simp]: "swap a b (swap a b f) = f"
424 by (rule ext, simp add: fun_upd_def swap_def)
426 lemma inj_on_imp_inj_on_swap:
427   "[|inj_on f A; a \<in> A; b \<in> A|] ==> inj_on (swap a b f) A"
428 by (simp add: inj_on_def swap_def, blast)
430 lemma inj_on_swap_iff [simp]:
431   assumes A: "a \<in> A" "b \<in> A" shows "inj_on (swap a b f) A = inj_on f A"
432 proof
433   assume "inj_on (swap a b f) A"
434   with A have "inj_on (swap a b (swap a b f)) A"
435     by (iprover intro: inj_on_imp_inj_on_swap)
436   thus "inj_on f A" by simp
437 next
438   assume "inj_on f A"
439   with A show "inj_on (swap a b f) A" by (iprover intro: inj_on_imp_inj_on_swap)
440 qed
442 lemma surj_imp_surj_swap: "surj f ==> surj (swap a b f)"
443 apply (simp add: surj_def swap_def, clarify)
444 apply (rule_tac P = "y = f b" in case_split_thm, blast)
445 apply (rule_tac P = "y = f a" in case_split_thm, auto)
446   --{*We don't yet have @{text case_tac}*}
447 done
449 lemma surj_swap_iff [simp]: "surj (swap a b f) = surj f"
450 proof
451   assume "surj (swap a b f)"
452   hence "surj (swap a b (swap a b f))" by (rule surj_imp_surj_swap)
453   thus "surj f" by simp
454 next
455   assume "surj f"
456   thus "surj (swap a b f)" by (rule surj_imp_surj_swap)
457 qed
459 lemma bij_swap_iff: "bij (swap a b f) = bij f"
463 subsection {* Proof tool setup *}
465 text {* simplifies terms of the form
466   f(...,x:=y,...,x:=z,...) to f(...,x:=z,...) *}
468 simproc_setup fun_upd2 ("f(v := w, x := y)") = {* fn _ =>
469 let
470   fun gen_fun_upd NONE T _ _ = NONE
471     | gen_fun_upd (SOME f) T x y = SOME (Const (@{const_name fun_upd}, T) \$ f \$ x \$ y)
472   fun dest_fun_T1 (Type (_, T :: Ts)) = T
473   fun find_double (t as Const (@{const_name fun_upd},T) \$ f \$ x \$ y) =
474     let
475       fun find (Const (@{const_name fun_upd},T) \$ g \$ v \$ w) =
476             if v aconv x then SOME g else gen_fun_upd (find g) T v w
477         | find t = NONE
478     in (dest_fun_T1 T, gen_fun_upd (find f) T x y) end
480   fun proc ss ct =
481     let
482       val ctxt = Simplifier.the_context ss
483       val t = Thm.term_of ct
484     in
485       case find_double t of
486         (T, NONE) => NONE
487       | (T, SOME rhs) =>
488           SOME (Goal.prove ctxt [] [] (Term.equals T \$ t \$ rhs)
489             (fn _ =>
490               rtac eq_reflection 1 THEN
491               rtac ext 1 THEN
492               simp_tac (Simplifier.inherit_context ss @{simpset}) 1))
493     end
494 in proc end
495 *}
498 subsection {* Code generator setup *}
500 code_const "op \<circ>"
501   (SML infixl 5 "o")
504 code_const "id"
508 subsection {* ML legacy bindings *}
510 ML {*
511 val set_cs = claset() delrules [equalityI]
512 *}
514 ML {*
515 val id_apply = @{thm id_apply}
516 val id_def = @{thm id_def}
517 val o_apply = @{thm o_apply}
518 val o_assoc = @{thm o_assoc}
519 val o_def = @{thm o_def}
520 val injD = @{thm injD}
521 val datatype_injI = @{thm datatype_injI}
522 val range_ex1_eq = @{thm range_ex1_eq}
523 val expand_fun_eq = @{thm expand_fun_eq}
524 *}
526 end