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