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