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