src/HOL/Fun.thy
 author paulson Tue Jun 28 15:27:45 2005 +0200 (2005-06-28) changeset 16587 b34c8aa657a5 parent 15691 900cf45ff0a6 child 16733 236dfafbeb63 permissions -rw-r--r--
Constant "If" is now local
1 (*  Title:      HOL/Fun.thy
2     ID:         \$Id\$
3     Author:     Tobias Nipkow, Cambridge University Computer Laboratory
4     Copyright   1994  University of Cambridge
7 *)
9 theory Fun
10 imports Typedef
11 begin
13 instance set :: (type) order
14   by (intro_classes,
15       (assumption | rule subset_refl subset_trans subset_antisym psubset_eq)+)
17 constdefs
18   fun_upd :: "('a => 'b) => 'a => 'b => ('a => 'b)"
19    "fun_upd f a b == % x. if x=a then b else f x"
21 nonterminals
22   updbinds updbind
23 syntax
24   "_updbind" :: "['a, 'a] => updbind"             ("(2_ :=/ _)")
25   ""         :: "updbind => updbinds"             ("_")
26   "_updbinds":: "[updbind, updbinds] => updbinds" ("_,/ _")
27   "_Update"  :: "['a, updbinds] => 'a"            ("_/'((_)')" [1000,0] 900)
29 translations
30   "_Update f (_updbinds b bs)"  == "_Update (_Update f b) bs"
31   "f(x:=y)"                     == "fun_upd f x y"
33 (* Hint: to define the sum of two functions (or maps), use sum_case.
34          A nice infix syntax could be defined (in Datatype.thy or below) by
35 consts
36   fun_sum :: "('a => 'c) => ('b => 'c) => (('a+'b) => 'c)" (infixr "'(+')"80)
37 translations
38  "fun_sum" == sum_case
39 *)
41 constdefs
42  override_on :: "('a => 'b) => ('a => 'b) => 'a set => ('a => 'b)"
43 "override_on f g A == %a. if a : A then g a else f a"
45  id :: "'a => 'a"
46 "id == %x. x"
48  comp :: "['b => 'c, 'a => 'b, 'a] => 'c"   (infixl "o" 55)
49 "f o g == %x. f(g(x))"
51 text{*compatibility*}
52 lemmas o_def = comp_def
54 syntax (xsymbols)
55   comp :: "['b => 'c, 'a => 'b, 'a] => 'c"        (infixl "\<circ>" 55)
56 syntax (HTML output)
57   comp :: "['b => 'c, 'a => 'b, 'a] => 'c"        (infixl "\<circ>" 55)
60 constdefs
61   inj_on :: "['a => 'b, 'a set] => bool"         (*injective*)
62     "inj_on f A == ! x:A. ! y:A. f(x)=f(y) --> x=y"
64 text{*A common special case: functions injective over the entire domain type.*}
65 syntax inj   :: "('a => 'b) => bool"
66 translations
67   "inj f" == "inj_on f UNIV"
69 constdefs
70   surj :: "('a => 'b) => bool"                   (*surjective*)
71     "surj f == ! y. ? x. y=f(x)"
73   bij :: "('a => 'b) => bool"                    (*bijective*)
74     "bij f == inj f & surj f"
78 text{*As a simplification rule, it replaces all function equalities by
79   first-order equalities.*}
80 lemma expand_fun_eq: "(f = g) = (! x. f(x)=g(x))"
81 apply (rule iffI)
82 apply (simp (no_asm_simp))
83 apply (rule ext, simp (no_asm_simp))
84 done
86 lemma apply_inverse:
87     "[| f(x)=u;  !!x. P(x) ==> g(f(x)) = x;  P(x) |] ==> x=g(u)"
88 by auto
91 text{*The Identity Function: @{term id}*}
92 lemma id_apply [simp]: "id x = x"
95 lemma inj_on_id: "inj_on id A"
98 lemma surj_id: "surj id"
101 lemma bij_id: "bij id"
102 by (simp add: bij_def inj_on_id surj_id)
106 subsection{*The Composition Operator: @{term "f \<circ> g"}*}
108 lemma o_apply [simp]: "(f o g) x = f (g x)"
111 lemma o_assoc: "f o (g o h) = f o g o h"
114 lemma id_o [simp]: "id o g = g"
117 lemma o_id [simp]: "f o id = f"
120 lemma image_compose: "(f o g) ` r = f`(g`r)"
121 by (simp add: comp_def, blast)
123 lemma image_eq_UN: "f`A = (UN x:A. {f x})"
124 by blast
126 lemma UN_o: "UNION A (g o f) = UNION (f`A) g"
127 by (unfold comp_def, blast)
130 subsection{*The Injectivity Predicate, @{term inj}*}
132 text{*NB: @{term inj} now just translates to @{term inj_on}*}
135 text{*For Proofs in @{text "Tools/datatype_rep_proofs"}*}
136 lemma datatype_injI:
137     "(!! x. ALL y. f(x) = f(y) --> x=y) ==> inj(f)"
140 theorem range_ex1_eq: "inj f \<Longrightarrow> b : range f = (EX! x. b = f x)"
141   by (unfold inj_on_def, blast)
143 lemma injD: "[| inj(f); f(x) = f(y) |] ==> x=y"
146 (*Useful with the simplifier*)
147 lemma inj_eq: "inj(f) ==> (f(x) = f(y)) = (x=y)"
148 by (force simp add: inj_on_def)
151 subsection{*The Predicate @{term inj_on}: Injectivity On A Restricted Domain*}
153 lemma inj_onI:
154     "(!! x y. [|  x:A;  y:A;  f(x) = f(y) |] ==> x=y) ==> inj_on f A"
157 lemma inj_on_inverseI: "(!!x. x:A ==> g(f(x)) = x) ==> inj_on f A"
158 by (auto dest:  arg_cong [of concl: g] simp add: inj_on_def)
160 lemma inj_onD: "[| inj_on f A;  f(x)=f(y);  x:A;  y:A |] ==> x=y"
161 by (unfold inj_on_def, blast)
163 lemma inj_on_iff: "[| inj_on f A;  x:A;  y:A |] ==> (f(x)=f(y)) = (x=y)"
164 by (blast dest!: inj_onD)
166 lemma comp_inj_on:
167      "[| inj_on f A;  inj_on g (f`A) |] ==> inj_on (g o f) A"
168 by (simp add: comp_def inj_on_def)
170 lemma inj_on_imageI: "inj_on (g o f) A \<Longrightarrow> inj_on g (f ` A)"
172 apply blast
173 done
175 lemma inj_on_image_iff: "\<lbrakk> ALL x:A. ALL y:A. (g(f x) = g(f y)) = (g x = g y);
176   inj_on f A \<rbrakk> \<Longrightarrow> inj_on g (f ` A) = inj_on g A"
177 apply(unfold inj_on_def)
178 apply blast
179 done
181 lemma inj_on_contraD: "[| inj_on f A;  ~x=y;  x:A;  y:A |] ==> ~ f(x)=f(y)"
182 by (unfold inj_on_def, blast)
184 lemma inj_singleton: "inj (%s. {s})"
187 lemma inj_on_empty[iff]: "inj_on f {}"
190 lemma subset_inj_on: "[| inj_on f B; A <= B |] ==> inj_on f A"
191 by (unfold inj_on_def, blast)
193 lemma inj_on_Un:
194  "inj_on f (A Un B) =
195   (inj_on f A & inj_on f B & f`(A-B) Int f`(B-A) = {})"
196 apply(unfold inj_on_def)
197 apply (blast intro:sym)
198 done
200 lemma inj_on_insert[iff]:
201   "inj_on f (insert a A) = (inj_on f A & f a ~: f`(A-{a}))"
202 apply(unfold inj_on_def)
203 apply (blast intro:sym)
204 done
206 lemma inj_on_diff: "inj_on f A ==> inj_on f (A-B)"
207 apply(unfold inj_on_def)
208 apply (blast)
209 done
212 subsection{*The Predicate @{term surj}: Surjectivity*}
214 lemma surjI: "(!! x. g(f x) = x) ==> surj g"
216 apply (blast intro: sym)
217 done
219 lemma surj_range: "surj f ==> range f = UNIV"
220 by (auto simp add: surj_def)
222 lemma surjD: "surj f ==> EX x. y = f x"
225 lemma surjE: "surj f ==> (!!x. y = f x ==> C) ==> C"
226 by (simp add: surj_def, blast)
228 lemma comp_surj: "[| surj f;  surj g |] ==> surj (g o f)"
229 apply (simp add: comp_def surj_def, clarify)
230 apply (drule_tac x = y in spec, clarify)
231 apply (drule_tac x = x in spec, blast)
232 done
236 subsection{*The Predicate @{term bij}: Bijectivity*}
238 lemma bijI: "[| inj f; surj f |] ==> bij f"
241 lemma bij_is_inj: "bij f ==> inj f"
244 lemma bij_is_surj: "bij f ==> surj f"
248 subsection{*Facts About the Identity Function*}
250 text{*We seem to need both the @{term id} forms and the @{term "\<lambda>x. x"}
251 forms. The latter can arise by rewriting, while @{term id} may be used
252 explicitly.*}
254 lemma image_ident [simp]: "(%x. x) ` Y = Y"
255 by blast
257 lemma image_id [simp]: "id ` Y = Y"
260 lemma vimage_ident [simp]: "(%x. x) -` Y = Y"
261 by blast
263 lemma vimage_id [simp]: "id -` A = A"
266 lemma vimage_image_eq: "f -` (f ` A) = {y. EX x:A. f x = f y}"
267 by (blast intro: sym)
269 lemma image_vimage_subset: "f ` (f -` A) <= A"
270 by blast
272 lemma image_vimage_eq [simp]: "f ` (f -` A) = A Int range f"
273 by blast
275 lemma surj_image_vimage_eq: "surj f ==> f ` (f -` A) = A"
278 lemma inj_vimage_image_eq: "inj f ==> f -` (f ` A) = A"
279 by (simp add: inj_on_def, blast)
281 lemma vimage_subsetD: "surj f ==> f -` B <= A ==> B <= f ` A"
282 apply (unfold surj_def)
283 apply (blast intro: sym)
284 done
286 lemma vimage_subsetI: "inj f ==> B <= f ` A ==> f -` B <= A"
287 by (unfold inj_on_def, blast)
289 lemma vimage_subset_eq: "bij f ==> (f -` B <= A) = (B <= f ` A)"
290 apply (unfold bij_def)
291 apply (blast del: subsetI intro: vimage_subsetI vimage_subsetD)
292 done
294 lemma image_Int_subset: "f`(A Int B) <= f`A Int f`B"
295 by blast
297 lemma image_diff_subset: "f`A - f`B <= f`(A - B)"
298 by blast
300 lemma inj_on_image_Int:
301    "[| inj_on f C;  A<=C;  B<=C |] ==> f`(A Int B) = f`A Int f`B"
302 apply (simp add: inj_on_def, blast)
303 done
305 lemma inj_on_image_set_diff:
306    "[| inj_on f C;  A<=C;  B<=C |] ==> f`(A-B) = f`A - f`B"
307 apply (simp add: inj_on_def, blast)
308 done
310 lemma image_Int: "inj f ==> f`(A Int B) = f`A Int f`B"
311 by (simp add: inj_on_def, blast)
313 lemma image_set_diff: "inj f ==> f`(A-B) = f`A - f`B"
314 by (simp add: inj_on_def, blast)
316 lemma inj_image_mem_iff: "inj f ==> (f a : f`A) = (a : A)"
317 by (blast dest: injD)
319 lemma inj_image_subset_iff: "inj f ==> (f`A <= f`B) = (A<=B)"
320 by (simp add: inj_on_def, blast)
322 lemma inj_image_eq_iff: "inj f ==> (f`A = f`B) = (A = B)"
323 by (blast dest: injD)
325 lemma image_UN: "(f ` (UNION A B)) = (UN x:A.(f ` (B x)))"
326 by blast
328 (*injectivity's required.  Left-to-right inclusion holds even if A is empty*)
329 lemma image_INT:
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)
333 done
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)"
339 apply (simp add: inj_on_def surj_def, blast)
340 done
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)"
350 apply (rule equalityI)
351 apply (simp_all (no_asm_simp) add: inj_image_Compl_subset surj_Compl_image_subset)
352 done
355 subsection{*Function Updating*}
357 lemma fun_upd_idem_iff: "(f(x:=y) = f) = (f x = y)"
358 apply (simp add: fun_upd_def, safe)
359 apply (erule subst)
360 apply (rule_tac [2] ext, auto)
361 done
363 (* f x = y ==> f(x:=y) = f *)
364 lemmas fun_upd_idem = fun_upd_idem_iff [THEN iffD2, standard]
366 (* f(x := f x) = f *)
367 declare refl [THEN fun_upd_idem, iff]
369 lemma fun_upd_apply [simp]: "(f(x:=y))z = (if z=x then y else f z)"
370 apply (simp (no_asm) add: fun_upd_def)
371 done
373 (* fun_upd_apply supersedes these two,   but they are useful
374    if fun_upd_apply is intentionally removed from the simpset *)
375 lemma fun_upd_same: "(f(x:=y)) x = y"
376 by simp
378 lemma fun_upd_other: "z~=x ==> (f(x:=y)) z = f z"
379 by simp
381 lemma fun_upd_upd [simp]: "f(x:=y,x:=z) = f(x:=z)"
384 lemma fun_upd_twist: "a ~= c ==> (m(a:=b))(c:=d) = (m(c:=d))(a:=b)"
385 by (rule ext, auto)
387 lemma inj_on_fun_updI: "\<lbrakk> inj_on f A; y \<notin> f`A \<rbrakk> \<Longrightarrow> inj_on (f(x:=y)) A"
388 by(fastsimp simp:inj_on_def image_def)
390 lemma fun_upd_image:
391      "f(x:=y) ` A = (if x \<in> A then insert y (f ` (A-{x})) else f ` A)"
392 by auto
394 subsection{* @{text override_on} *}
396 lemma override_on_emptyset[simp]: "override_on f g {} = f"
399 lemma override_on_apply_notin[simp]: "a ~: A ==> (override_on f g A) a = f a"
402 lemma override_on_apply_in[simp]: "a : A ==> (override_on f g A) a = g a"
405 subsection{* swap *}
407 constdefs
408   swap :: "['a, 'a, 'a => 'b] => ('a => 'b)"
409    "swap a b f == f(a := f b, b:= f a)"
411 lemma swap_self: "swap a a f = f"
414 lemma swap_commute: "swap a b f = swap b a f"
415 by (rule ext, simp add: fun_upd_def swap_def)
417 lemma swap_nilpotent [simp]: "swap a b (swap a b f) = f"
418 by (rule ext, simp add: fun_upd_def swap_def)
420 lemma inj_on_imp_inj_on_swap:
421      "[|inj_on f A; a \<in> A; b \<in> A|] ==> inj_on (swap a b f) A"
422 by (simp add: inj_on_def swap_def, blast)
424 lemma inj_on_swap_iff [simp]:
425   assumes A: "a \<in> A" "b \<in> A" shows "inj_on (swap a b f) A = inj_on f A"
426 proof
427   assume "inj_on (swap a b f) A"
428   with A have "inj_on (swap a b (swap a b f)) A"
429     by (rules intro: inj_on_imp_inj_on_swap)
430   thus "inj_on f A" by simp
431 next
432   assume "inj_on f A"
433   with A show "inj_on (swap a b f) A" by (rules intro: inj_on_imp_inj_on_swap)
434 qed
436 lemma surj_imp_surj_swap: "surj f ==> surj (swap a b f)"
437 apply (simp add: surj_def swap_def, clarify)
438 apply (rule_tac P = "y = f b" in case_split_thm, blast)
439 apply (rule_tac P = "y = f a" in case_split_thm, auto)
440   --{*We don't yet have @{text case_tac}*}
441 done
443 lemma surj_swap_iff [simp]: "surj (swap a b f) = surj f"
444 proof
445   assume "surj (swap a b f)"
446   hence "surj (swap a b (swap a b f))" by (rule surj_imp_surj_swap)
447   thus "surj f" by simp
448 next
449   assume "surj f"
450   thus "surj (swap a b f)" by (rule surj_imp_surj_swap)
451 qed
453 lemma bij_swap_iff: "bij (swap a b f) = bij f"
457 text{*The ML section includes some compatibility bindings and a simproc
458 for function updates, in addition to the usual ML-bindings of theorems.*}
459 ML
460 {*
461 val id_def = thm "id_def";
462 val inj_on_def = thm "inj_on_def";
463 val surj_def = thm "surj_def";
464 val bij_def = thm "bij_def";
465 val fun_upd_def = thm "fun_upd_def";
467 val o_def = thm "comp_def";
468 val injI = thm "inj_onI";
469 val inj_inverseI = thm "inj_on_inverseI";
470 val set_cs = claset() delrules [equalityI];
472 val print_translation = [("Pi", dependent_tr' ("@Pi", "op funcset"))];
474 (* simplifies terms of the form f(...,x:=y,...,x:=z,...) to f(...,x:=z,...) *)
475 local
476   fun gen_fun_upd NONE T _ _ = NONE
477     | gen_fun_upd (SOME f) T x y = SOME (Const ("Fun.fun_upd",T) \$ f \$ x \$ y)
478   fun dest_fun_T1 (Type (_, T :: Ts)) = T
479   fun find_double (t as Const ("Fun.fun_upd",T) \$ f \$ x \$ y) =
480     let
481       fun find (Const ("Fun.fun_upd",T) \$ g \$ v \$ w) =
482             if v aconv x then SOME g else gen_fun_upd (find g) T v w
483         | find t = NONE
484     in (dest_fun_T1 T, gen_fun_upd (find f) T x y) end
486   val ss = simpset ()
487   val fun_upd_prover = K (rtac eq_reflection 1 THEN rtac ext 1 THEN simp_tac ss 1)
488 in
489   val fun_upd2_simproc =
490     Simplifier.simproc (Theory.sign_of (the_context ()))
491       "fun_upd2" ["f(v := w, x := y)"]
492       (fn sg => fn _ => fn t =>
493         case find_double t of (T, NONE) => NONE
494         | (T, SOME rhs) => SOME (Tactic.prove sg [] [] (Term.equals T \$ t \$ rhs) fun_upd_prover))
495 end;
498 val expand_fun_eq = thm "expand_fun_eq";
499 val apply_inverse = thm "apply_inverse";
500 val id_apply = thm "id_apply";
501 val o_apply = thm "o_apply";
502 val o_assoc = thm "o_assoc";
503 val id_o = thm "id_o";
504 val o_id = thm "o_id";
505 val image_compose = thm "image_compose";
506 val image_eq_UN = thm "image_eq_UN";
507 val UN_o = thm "UN_o";
508 val datatype_injI = thm "datatype_injI";
509 val injD = thm "injD";
510 val inj_eq = thm "inj_eq";
511 val inj_onI = thm "inj_onI";
512 val inj_on_inverseI = thm "inj_on_inverseI";
513 val inj_onD = thm "inj_onD";
514 val inj_on_iff = thm "inj_on_iff";
515 val comp_inj_on = thm "comp_inj_on";
517 val inj_singleton = thm "inj_singleton";
518 val subset_inj_on = thm "subset_inj_on";
519 val surjI = thm "surjI";
520 val surj_range = thm "surj_range";
521 val surjD = thm "surjD";
522 val surjE = thm "surjE";
523 val comp_surj = thm "comp_surj";
524 val bijI = thm "bijI";
525 val bij_is_inj = thm "bij_is_inj";
526 val bij_is_surj = thm "bij_is_surj";
527 val image_ident = thm "image_ident";
528 val image_id = thm "image_id";
529 val vimage_ident = thm "vimage_ident";
530 val vimage_id = thm "vimage_id";
531 val vimage_image_eq = thm "vimage_image_eq";
532 val image_vimage_subset = thm "image_vimage_subset";
533 val image_vimage_eq = thm "image_vimage_eq";
534 val surj_image_vimage_eq = thm "surj_image_vimage_eq";
535 val inj_vimage_image_eq = thm "inj_vimage_image_eq";
536 val vimage_subsetD = thm "vimage_subsetD";
537 val vimage_subsetI = thm "vimage_subsetI";
538 val vimage_subset_eq = thm "vimage_subset_eq";
539 val image_Int_subset = thm "image_Int_subset";
540 val image_diff_subset = thm "image_diff_subset";
541 val inj_on_image_Int = thm "inj_on_image_Int";
542 val inj_on_image_set_diff = thm "inj_on_image_set_diff";
543 val image_Int = thm "image_Int";
544 val image_set_diff = thm "image_set_diff";
545 val inj_image_mem_iff = thm "inj_image_mem_iff";
546 val inj_image_subset_iff = thm "inj_image_subset_iff";
547 val inj_image_eq_iff = thm "inj_image_eq_iff";
548 val image_UN = thm "image_UN";
549 val image_INT = thm "image_INT";
550 val bij_image_INT = thm "bij_image_INT";
551 val surj_Compl_image_subset = thm "surj_Compl_image_subset";
552 val inj_image_Compl_subset = thm "inj_image_Compl_subset";
553 val bij_image_Compl_eq = thm "bij_image_Compl_eq";
554 val fun_upd_idem_iff = thm "fun_upd_idem_iff";
555 val fun_upd_idem = thm "fun_upd_idem";
556 val fun_upd_apply = thm "fun_upd_apply";
557 val fun_upd_same = thm "fun_upd_same";
558 val fun_upd_other = thm "fun_upd_other";
559 val fun_upd_upd = thm "fun_upd_upd";
560 val fun_upd_twist = thm "fun_upd_twist";
561 val range_ex1_eq = thm "range_ex1_eq";
562 *}
564 end