src/HOL/Fun.thy
author nipkow
Wed Aug 18 11:09:40 2004 +0200 (2004-08-18)
changeset 15140 322485b816ac
parent 15131 c69542757a4d
child 15303 eedbb8d22ca2
permissions -rw-r--r--
import -> imports
     1 (*  Title:      HOL/Fun.thy
     2     ID:         $Id$
     3     Author:     Tobias Nipkow, Cambridge University Computer Laboratory
     4     Copyright   1994  University of Cambridge
     5 
     6 Notions about functions.
     7 *)
     8 
     9 theory Fun 
    10 imports Typedef
    11 begin
    12 
    13 instance set :: (type) order
    14   by (intro_classes,
    15       (assumption | rule subset_refl subset_trans subset_antisym psubset_eq)+)
    16 
    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"
    20 
    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)
    28 
    29 translations
    30   "_Update f (_updbinds b bs)"  == "_Update (_Update f b) bs"
    31   "f(x:=y)"                     == "fun_upd f x y"
    32 
    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 *)
    40 
    41 constdefs
    42  overwrite :: "('a => 'b) => ('a => 'b) => 'a set => ('a => 'b)"
    43               ("_/'(_|/_')"  [900,0,0]900)
    44 "f(g|A) == %a. if a : A then g a else f a"
    45 
    46  id :: "'a => 'a"
    47 "id == %x. x"
    48 
    49  comp :: "['b => 'c, 'a => 'b, 'a] => 'c"   (infixl "o" 55)
    50 "f o g == %x. f(g(x))"
    51 
    52 text{*compatibility*}
    53 lemmas o_def = comp_def
    54 
    55 syntax (xsymbols)
    56   comp :: "['b => 'c, 'a => 'b, 'a] => 'c"        (infixl "\<circ>" 55)
    57 syntax (HTML output)
    58   comp :: "['b => 'c, 'a => 'b, 'a] => 'c"        (infixl "\<circ>" 55)
    59 
    60 
    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"
    64 
    65 text{*A common special case: functions injective over the entire domain type.*}
    66 syntax inj   :: "('a => 'b) => bool"
    67 translations
    68   "inj f" == "inj_on f UNIV"
    69 
    70 constdefs
    71   surj :: "('a => 'b) => bool"                   (*surjective*)
    72     "surj f == ! y. ? x. y=f(x)"
    73 
    74   bij :: "('a => 'b) => bool"                    (*bijective*)
    75     "bij f == inj f & surj f"
    76 
    77 
    78 
    79 text{*As a simplification rule, it replaces all function equalities by
    80   first-order equalities.*}
    81 lemma expand_fun_eq: "(f = g) = (! x. f(x)=g(x))"
    82 apply (rule iffI)
    83 apply (simp (no_asm_simp))
    84 apply (rule ext, simp (no_asm_simp))
    85 done
    86 
    87 lemma apply_inverse:
    88     "[| f(x)=u;  !!x. P(x) ==> g(f(x)) = x;  P(x) |] ==> x=g(u)"
    89 by auto
    90 
    91 
    92 text{*The Identity Function: @{term id}*}
    93 lemma id_apply [simp]: "id x = x"
    94 by (simp add: id_def)
    95 
    96 
    97 subsection{*The Composition Operator: @{term "f \<circ> g"}*}
    98 
    99 lemma o_apply [simp]: "(f o g) x = f (g x)"
   100 by (simp add: comp_def)
   101 
   102 lemma o_assoc: "f o (g o h) = f o g o h"
   103 by (simp add: comp_def)
   104 
   105 lemma id_o [simp]: "id o g = g"
   106 by (simp add: comp_def)
   107 
   108 lemma o_id [simp]: "f o id = f"
   109 by (simp add: comp_def)
   110 
   111 lemma image_compose: "(f o g) ` r = f`(g`r)"
   112 by (simp add: comp_def, blast)
   113 
   114 lemma image_eq_UN: "f`A = (UN x:A. {f x})"
   115 by blast
   116 
   117 lemma UN_o: "UNION A (g o f) = UNION (f`A) g"
   118 by (unfold comp_def, blast)
   119 
   120 
   121 subsection{*The Injectivity Predicate, @{term inj}*}
   122 
   123 text{*NB: @{term inj} now just translates to @{term inj_on}*}
   124 
   125 
   126 text{*For Proofs in @{text "Tools/datatype_rep_proofs"}*}
   127 lemma datatype_injI:
   128     "(!! x. ALL y. f(x) = f(y) --> x=y) ==> inj(f)"
   129 by (simp add: inj_on_def)
   130 
   131 theorem range_ex1_eq: "inj f \<Longrightarrow> b : range f = (EX! x. b = f x)"
   132   by (unfold inj_on_def, blast)
   133 
   134 lemma injD: "[| inj(f); f(x) = f(y) |] ==> x=y"
   135 by (simp add: inj_on_def)
   136 
   137 (*Useful with the simplifier*)
   138 lemma inj_eq: "inj(f) ==> (f(x) = f(y)) = (x=y)"
   139 by (force simp add: inj_on_def)
   140 
   141 
   142 subsection{*The Predicate @{term inj_on}: Injectivity On A Restricted Domain*}
   143 
   144 lemma inj_onI:
   145     "(!! x y. [|  x:A;  y:A;  f(x) = f(y) |] ==> x=y) ==> inj_on f A"
   146 by (simp add: inj_on_def)
   147 
   148 lemma inj_on_inverseI: "(!!x. x:A ==> g(f(x)) = x) ==> inj_on f A"
   149 by (auto dest:  arg_cong [of concl: g] simp add: inj_on_def)
   150 
   151 lemma inj_onD: "[| inj_on f A;  f(x)=f(y);  x:A;  y:A |] ==> x=y"
   152 by (unfold inj_on_def, blast)
   153 
   154 lemma inj_on_iff: "[| inj_on f A;  x:A;  y:A |] ==> (f(x)=f(y)) = (x=y)"
   155 by (blast dest!: inj_onD)
   156 
   157 lemma comp_inj_on:
   158      "[| inj_on f A;  inj_on g (f`A) |] ==> inj_on (g o f) A"
   159 by (simp add: comp_def inj_on_def)
   160 
   161 lemma inj_on_contraD: "[| inj_on f A;  ~x=y;  x:A;  y:A |] ==> ~ f(x)=f(y)"
   162 by (unfold inj_on_def, blast)
   163 
   164 lemma inj_singleton: "inj (%s. {s})"
   165 by (simp add: inj_on_def)
   166 
   167 lemma inj_on_empty[iff]: "inj_on f {}"
   168 by(simp add: inj_on_def)
   169 
   170 lemma subset_inj_on: "[| A<=B; inj_on f B |] ==> inj_on f A"
   171 by (unfold inj_on_def, blast)
   172 
   173 lemma inj_on_Un:
   174  "inj_on f (A Un B) =
   175   (inj_on f A & inj_on f B & f`(A-B) Int f`(B-A) = {})"
   176 apply(unfold inj_on_def)
   177 apply (blast intro:sym)
   178 done
   179 
   180 lemma inj_on_insert[iff]:
   181   "inj_on f (insert a A) = (inj_on f A & f a ~: f`(A-{a}))"
   182 apply(unfold inj_on_def)
   183 apply (blast intro:sym)
   184 done
   185 
   186 lemma inj_on_diff: "inj_on f A ==> inj_on f (A-B)"
   187 apply(unfold inj_on_def)
   188 apply (blast)
   189 done
   190 
   191 
   192 subsection{*The Predicate @{term surj}: Surjectivity*}
   193 
   194 lemma surjI: "(!! x. g(f x) = x) ==> surj g"
   195 apply (simp add: surj_def)
   196 apply (blast intro: sym)
   197 done
   198 
   199 lemma surj_range: "surj f ==> range f = UNIV"
   200 by (auto simp add: surj_def)
   201 
   202 lemma surjD: "surj f ==> EX x. y = f x"
   203 by (simp add: surj_def)
   204 
   205 lemma surjE: "surj f ==> (!!x. y = f x ==> C) ==> C"
   206 by (simp add: surj_def, blast)
   207 
   208 lemma comp_surj: "[| surj f;  surj g |] ==> surj (g o f)"
   209 apply (simp add: comp_def surj_def, clarify)
   210 apply (drule_tac x = y in spec, clarify)
   211 apply (drule_tac x = x in spec, blast)
   212 done
   213 
   214 
   215 
   216 subsection{*The Predicate @{term bij}: Bijectivity*}
   217 
   218 lemma bijI: "[| inj f; surj f |] ==> bij f"
   219 by (simp add: bij_def)
   220 
   221 lemma bij_is_inj: "bij f ==> inj f"
   222 by (simp add: bij_def)
   223 
   224 lemma bij_is_surj: "bij f ==> surj f"
   225 by (simp add: bij_def)
   226 
   227 
   228 subsection{*Facts About the Identity Function*}
   229 
   230 text{*We seem to need both the @{term id} forms and the @{term "\<lambda>x. x"}
   231 forms. The latter can arise by rewriting, while @{term id} may be used
   232 explicitly.*}
   233 
   234 lemma image_ident [simp]: "(%x. x) ` Y = Y"
   235 by blast
   236 
   237 lemma image_id [simp]: "id ` Y = Y"
   238 by (simp add: id_def)
   239 
   240 lemma vimage_ident [simp]: "(%x. x) -` Y = Y"
   241 by blast
   242 
   243 lemma vimage_id [simp]: "id -` A = A"
   244 by (simp add: id_def)
   245 
   246 lemma vimage_image_eq: "f -` (f ` A) = {y. EX x:A. f x = f y}"
   247 by (blast intro: sym)
   248 
   249 lemma image_vimage_subset: "f ` (f -` A) <= A"
   250 by blast
   251 
   252 lemma image_vimage_eq [simp]: "f ` (f -` A) = A Int range f"
   253 by blast
   254 
   255 lemma surj_image_vimage_eq: "surj f ==> f ` (f -` A) = A"
   256 by (simp add: surj_range)
   257 
   258 lemma inj_vimage_image_eq: "inj f ==> f -` (f ` A) = A"
   259 by (simp add: inj_on_def, blast)
   260 
   261 lemma vimage_subsetD: "surj f ==> f -` B <= A ==> B <= f ` A"
   262 apply (unfold surj_def)
   263 apply (blast intro: sym)
   264 done
   265 
   266 lemma vimage_subsetI: "inj f ==> B <= f ` A ==> f -` B <= A"
   267 by (unfold inj_on_def, blast)
   268 
   269 lemma vimage_subset_eq: "bij f ==> (f -` B <= A) = (B <= f ` A)"
   270 apply (unfold bij_def)
   271 apply (blast del: subsetI intro: vimage_subsetI vimage_subsetD)
   272 done
   273 
   274 lemma image_Int_subset: "f`(A Int B) <= f`A Int f`B"
   275 by blast
   276 
   277 lemma image_diff_subset: "f`A - f`B <= f`(A - B)"
   278 by blast
   279 
   280 lemma inj_on_image_Int:
   281    "[| inj_on f C;  A<=C;  B<=C |] ==> f`(A Int B) = f`A Int f`B"
   282 apply (simp add: inj_on_def, blast)
   283 done
   284 
   285 lemma inj_on_image_set_diff:
   286    "[| inj_on f C;  A<=C;  B<=C |] ==> f`(A-B) = f`A - f`B"
   287 apply (simp add: inj_on_def, blast)
   288 done
   289 
   290 lemma image_Int: "inj f ==> f`(A Int B) = f`A Int f`B"
   291 by (simp add: inj_on_def, blast)
   292 
   293 lemma image_set_diff: "inj f ==> f`(A-B) = f`A - f`B"
   294 by (simp add: inj_on_def, blast)
   295 
   296 lemma inj_image_mem_iff: "inj f ==> (f a : f`A) = (a : A)"
   297 by (blast dest: injD)
   298 
   299 lemma inj_image_subset_iff: "inj f ==> (f`A <= f`B) = (A<=B)"
   300 by (simp add: inj_on_def, blast)
   301 
   302 lemma inj_image_eq_iff: "inj f ==> (f`A = f`B) = (A = B)"
   303 by (blast dest: injD)
   304 
   305 lemma image_UN: "(f ` (UNION A B)) = (UN x:A.(f ` (B x)))"
   306 by blast
   307 
   308 (*injectivity's required.  Left-to-right inclusion holds even if A is empty*)
   309 lemma image_INT:
   310    "[| inj_on f C;  ALL x:A. B x <= C;  j:A |]
   311     ==> f ` (INTER A B) = (INT x:A. f ` B x)"
   312 apply (simp add: inj_on_def, blast)
   313 done
   314 
   315 (*Compare with image_INT: no use of inj_on, and if f is surjective then
   316   it doesn't matter whether A is empty*)
   317 lemma bij_image_INT: "bij f ==> f ` (INTER A B) = (INT x:A. f ` B x)"
   318 apply (simp add: bij_def)
   319 apply (simp add: inj_on_def surj_def, blast)
   320 done
   321 
   322 lemma surj_Compl_image_subset: "surj f ==> -(f`A) <= f`(-A)"
   323 by (auto simp add: surj_def)
   324 
   325 lemma inj_image_Compl_subset: "inj f ==> f`(-A) <= -(f`A)"
   326 by (auto simp add: inj_on_def)
   327 
   328 lemma bij_image_Compl_eq: "bij f ==> f`(-A) = -(f`A)"
   329 apply (simp add: bij_def)
   330 apply (rule equalityI)
   331 apply (simp_all (no_asm_simp) add: inj_image_Compl_subset surj_Compl_image_subset)
   332 done
   333 
   334 
   335 subsection{*Function Updating*}
   336 
   337 lemma fun_upd_idem_iff: "(f(x:=y) = f) = (f x = y)"
   338 apply (simp add: fun_upd_def, safe)
   339 apply (erule subst)
   340 apply (rule_tac [2] ext, auto)
   341 done
   342 
   343 (* f x = y ==> f(x:=y) = f *)
   344 lemmas fun_upd_idem = fun_upd_idem_iff [THEN iffD2, standard]
   345 
   346 (* f(x := f x) = f *)
   347 declare refl [THEN fun_upd_idem, iff]
   348 
   349 lemma fun_upd_apply [simp]: "(f(x:=y))z = (if z=x then y else f z)"
   350 apply (simp (no_asm) add: fun_upd_def)
   351 done
   352 
   353 (* fun_upd_apply supersedes these two,   but they are useful
   354    if fun_upd_apply is intentionally removed from the simpset *)
   355 lemma fun_upd_same: "(f(x:=y)) x = y"
   356 by simp
   357 
   358 lemma fun_upd_other: "z~=x ==> (f(x:=y)) z = f z"
   359 by simp
   360 
   361 lemma fun_upd_upd [simp]: "f(x:=y,x:=z) = f(x:=z)"
   362 by (simp add: expand_fun_eq)
   363 
   364 lemma fun_upd_twist: "a ~= c ==> (m(a:=b))(c:=d) = (m(c:=d))(a:=b)"
   365 by (rule ext, auto)
   366 
   367 subsection{* overwrite *}
   368 
   369 lemma overwrite_emptyset[simp]: "f(g|{}) = f"
   370 by(simp add:overwrite_def)
   371 
   372 lemma overwrite_apply_notin[simp]: "a ~: A ==> (f(g|A)) a = f a"
   373 by(simp add:overwrite_def)
   374 
   375 lemma overwrite_apply_in[simp]: "a : A ==> (f(g|A)) a = g a"
   376 by(simp add:overwrite_def)
   377 
   378 text{*The ML section includes some compatibility bindings and a simproc
   379 for function updates, in addition to the usual ML-bindings of theorems.*}
   380 ML
   381 {*
   382 val id_def = thm "id_def";
   383 val inj_on_def = thm "inj_on_def";
   384 val surj_def = thm "surj_def";
   385 val bij_def = thm "bij_def";
   386 val fun_upd_def = thm "fun_upd_def";
   387 
   388 val o_def = thm "comp_def";
   389 val injI = thm "inj_onI";
   390 val inj_inverseI = thm "inj_on_inverseI";
   391 val set_cs = claset() delrules [equalityI];
   392 
   393 val print_translation = [("Pi", dependent_tr' ("@Pi", "op funcset"))];
   394 
   395 (* simplifies terms of the form f(...,x:=y,...,x:=z,...) to f(...,x:=z,...) *)
   396 local
   397   fun gen_fun_upd None T _ _ = None
   398     | gen_fun_upd (Some f) T x y = Some (Const ("Fun.fun_upd",T) $ f $ x $ y)
   399   fun dest_fun_T1 (Type (_, T :: Ts)) = T
   400   fun find_double (t as Const ("Fun.fun_upd",T) $ f $ x $ y) =
   401     let
   402       fun find (Const ("Fun.fun_upd",T) $ g $ v $ w) =
   403             if v aconv x then Some g else gen_fun_upd (find g) T v w
   404         | find t = None
   405     in (dest_fun_T1 T, gen_fun_upd (find f) T x y) end
   406 
   407   val ss = simpset ()
   408   val fun_upd_prover = K (rtac eq_reflection 1 THEN rtac ext 1 THEN simp_tac ss 1)
   409 in
   410   val fun_upd2_simproc =
   411     Simplifier.simproc (Theory.sign_of (the_context ()))
   412       "fun_upd2" ["f(v := w, x := y)"]
   413       (fn sg => fn _ => fn t =>
   414         case find_double t of (T, None) => None
   415         | (T, Some rhs) => Some (Tactic.prove sg [] [] (Term.equals T $ t $ rhs) fun_upd_prover))
   416 end;
   417 Addsimprocs[fun_upd2_simproc];
   418 
   419 val expand_fun_eq = thm "expand_fun_eq";
   420 val apply_inverse = thm "apply_inverse";
   421 val id_apply = thm "id_apply";
   422 val o_apply = thm "o_apply";
   423 val o_assoc = thm "o_assoc";
   424 val id_o = thm "id_o";
   425 val o_id = thm "o_id";
   426 val image_compose = thm "image_compose";
   427 val image_eq_UN = thm "image_eq_UN";
   428 val UN_o = thm "UN_o";
   429 val datatype_injI = thm "datatype_injI";
   430 val injD = thm "injD";
   431 val inj_eq = thm "inj_eq";
   432 val inj_onI = thm "inj_onI";
   433 val inj_on_inverseI = thm "inj_on_inverseI";
   434 val inj_onD = thm "inj_onD";
   435 val inj_on_iff = thm "inj_on_iff";
   436 val comp_inj_on = thm "comp_inj_on";
   437 val inj_on_contraD = thm "inj_on_contraD";
   438 val inj_singleton = thm "inj_singleton";
   439 val subset_inj_on = thm "subset_inj_on";
   440 val surjI = thm "surjI";
   441 val surj_range = thm "surj_range";
   442 val surjD = thm "surjD";
   443 val surjE = thm "surjE";
   444 val comp_surj = thm "comp_surj";
   445 val bijI = thm "bijI";
   446 val bij_is_inj = thm "bij_is_inj";
   447 val bij_is_surj = thm "bij_is_surj";
   448 val image_ident = thm "image_ident";
   449 val image_id = thm "image_id";
   450 val vimage_ident = thm "vimage_ident";
   451 val vimage_id = thm "vimage_id";
   452 val vimage_image_eq = thm "vimage_image_eq";
   453 val image_vimage_subset = thm "image_vimage_subset";
   454 val image_vimage_eq = thm "image_vimage_eq";
   455 val surj_image_vimage_eq = thm "surj_image_vimage_eq";
   456 val inj_vimage_image_eq = thm "inj_vimage_image_eq";
   457 val vimage_subsetD = thm "vimage_subsetD";
   458 val vimage_subsetI = thm "vimage_subsetI";
   459 val vimage_subset_eq = thm "vimage_subset_eq";
   460 val image_Int_subset = thm "image_Int_subset";
   461 val image_diff_subset = thm "image_diff_subset";
   462 val inj_on_image_Int = thm "inj_on_image_Int";
   463 val inj_on_image_set_diff = thm "inj_on_image_set_diff";
   464 val image_Int = thm "image_Int";
   465 val image_set_diff = thm "image_set_diff";
   466 val inj_image_mem_iff = thm "inj_image_mem_iff";
   467 val inj_image_subset_iff = thm "inj_image_subset_iff";
   468 val inj_image_eq_iff = thm "inj_image_eq_iff";
   469 val image_UN = thm "image_UN";
   470 val image_INT = thm "image_INT";
   471 val bij_image_INT = thm "bij_image_INT";
   472 val surj_Compl_image_subset = thm "surj_Compl_image_subset";
   473 val inj_image_Compl_subset = thm "inj_image_Compl_subset";
   474 val bij_image_Compl_eq = thm "bij_image_Compl_eq";
   475 val fun_upd_idem_iff = thm "fun_upd_idem_iff";
   476 val fun_upd_idem = thm "fun_upd_idem";
   477 val fun_upd_apply = thm "fun_upd_apply";
   478 val fun_upd_same = thm "fun_upd_same";
   479 val fun_upd_other = thm "fun_upd_other";
   480 val fun_upd_upd = thm "fun_upd_upd";
   481 val fun_upd_twist = thm "fun_upd_twist";
   482 val range_ex1_eq = thm "range_ex1_eq";
   483 *}
   484 
   485 end