src/HOL/Fun.thy
author haftmann
Thu Nov 18 17:01:15 2010 +0100 (2010-11-18)
changeset 40602 91e583511113
parent 39302 d7728f65b353
child 40702 cf26dd7395e4
permissions -rw-r--r--
map_fun combinator in theory Fun
     1 (*  Title:      HOL/Fun.thy
     2     Author:     Tobias Nipkow, Cambridge University Computer Laboratory
     3     Copyright   1994  University of Cambridge
     4 *)
     5 
     6 header {* Notions about functions *}
     7 
     8 theory Fun
     9 imports Complete_Lattice
    10 begin
    11 
    12 text{*As a simplification rule, it replaces all function equalities by
    13   first-order equalities.*}
    14 lemma fun_eq_iff: "f = g \<longleftrightarrow> (\<forall>x. f x = g x)"
    15 apply (rule iffI)
    16 apply (simp (no_asm_simp))
    17 apply (rule ext)
    18 apply (simp (no_asm_simp))
    19 done
    20 
    21 lemma apply_inverse:
    22   "f x = u \<Longrightarrow> (\<And>x. P x \<Longrightarrow> g (f x) = x) \<Longrightarrow> P x \<Longrightarrow> x = g u"
    23   by auto
    24 
    25 
    26 subsection {* The Identity Function @{text id} *}
    27 
    28 definition
    29   id :: "'a \<Rightarrow> 'a"
    30 where
    31   "id = (\<lambda>x. x)"
    32 
    33 lemma id_apply [simp]: "id x = x"
    34   by (simp add: id_def)
    35 
    36 lemma image_ident [simp]: "(%x. x) ` Y = Y"
    37 by blast
    38 
    39 lemma image_id [simp]: "id ` Y = Y"
    40 by (simp add: id_def)
    41 
    42 lemma vimage_ident [simp]: "(%x. x) -` Y = Y"
    43 by blast
    44 
    45 lemma vimage_id [simp]: "id -` A = A"
    46 by (simp add: id_def)
    47 
    48 
    49 subsection {* The Composition Operator @{text "f \<circ> g"} *}
    50 
    51 definition
    52   comp :: "('b \<Rightarrow> 'c) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'c" (infixl "o" 55)
    53 where
    54   "f o g = (\<lambda>x. f (g x))"
    55 
    56 notation (xsymbols)
    57   comp  (infixl "\<circ>" 55)
    58 
    59 notation (HTML output)
    60   comp  (infixl "\<circ>" 55)
    61 
    62 text{*compatibility*}
    63 lemmas o_def = comp_def
    64 
    65 lemma o_apply [simp]: "(f o g) x = f (g x)"
    66 by (simp add: comp_def)
    67 
    68 lemma o_assoc: "f o (g o h) = f o g o h"
    69 by (simp add: comp_def)
    70 
    71 lemma id_o [simp]: "id o g = g"
    72 by (simp add: comp_def)
    73 
    74 lemma o_id [simp]: "f o id = f"
    75 by (simp add: comp_def)
    76 
    77 lemma o_eq_dest:
    78   "a o b = c o d \<Longrightarrow> a (b v) = c (d v)"
    79   by (simp only: o_def) (fact fun_cong)
    80 
    81 lemma o_eq_elim:
    82   "a o b = c o d \<Longrightarrow> ((\<And>v. a (b v) = c (d v)) \<Longrightarrow> R) \<Longrightarrow> R"
    83   by (erule meta_mp) (fact o_eq_dest) 
    84 
    85 lemma image_compose: "(f o g) ` r = f`(g`r)"
    86 by (simp add: comp_def, blast)
    87 
    88 lemma vimage_compose: "(g \<circ> f) -` x = f -` (g -` x)"
    89   by auto
    90 
    91 lemma UN_o: "UNION A (g o f) = UNION (f`A) g"
    92 by (unfold comp_def, blast)
    93 
    94 
    95 subsection {* The Forward Composition Operator @{text fcomp} *}
    96 
    97 definition
    98   fcomp :: "('a \<Rightarrow> 'b) \<Rightarrow> ('b \<Rightarrow> 'c) \<Rightarrow> 'a \<Rightarrow> 'c" (infixl "\<circ>>" 60)
    99 where
   100   "f \<circ>> g = (\<lambda>x. g (f x))"
   101 
   102 lemma fcomp_apply [simp]:  "(f \<circ>> g) x = g (f x)"
   103   by (simp add: fcomp_def)
   104 
   105 lemma fcomp_assoc: "(f \<circ>> g) \<circ>> h = f \<circ>> (g \<circ>> h)"
   106   by (simp add: fcomp_def)
   107 
   108 lemma id_fcomp [simp]: "id \<circ>> g = g"
   109   by (simp add: fcomp_def)
   110 
   111 lemma fcomp_id [simp]: "f \<circ>> id = f"
   112   by (simp add: fcomp_def)
   113 
   114 code_const fcomp
   115   (Eval infixl 1 "#>")
   116 
   117 no_notation fcomp (infixl "\<circ>>" 60)
   118 
   119 
   120 subsection {* Mapping functions *}
   121 
   122 definition map_fun :: "('c \<Rightarrow> 'a) \<Rightarrow> ('b \<Rightarrow> 'd) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'c \<Rightarrow> 'd" where
   123   "map_fun f g h = g \<circ> h \<circ> f"
   124 
   125 lemma map_fun_apply [simp]:
   126   "map_fun f g h x = g (h (f x))"
   127   by (simp add: map_fun_def)
   128 
   129 type_mapper map_fun
   130   by (simp_all add: fun_eq_iff)
   131 
   132 
   133 subsection {* Injectivity, Surjectivity and Bijectivity *}
   134 
   135 definition inj_on :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a set \<Rightarrow> bool" where -- "injective"
   136   "inj_on f A \<longleftrightarrow> (\<forall>x\<in>A. \<forall>y\<in>A. f x = f y \<longrightarrow> x = y)"
   137 
   138 definition surj_on :: "('a \<Rightarrow> 'b) \<Rightarrow> 'b set \<Rightarrow> bool" where -- "surjective"
   139   "surj_on f B \<longleftrightarrow> B \<subseteq> range f"
   140 
   141 definition bij_betw :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a set \<Rightarrow> 'b set \<Rightarrow> bool" where -- "bijective"
   142   "bij_betw f A B \<longleftrightarrow> inj_on f A \<and> f ` A = B"
   143 
   144 text{*A common special case: functions injective over the entire domain type.*}
   145 
   146 abbreviation
   147   "inj f \<equiv> inj_on f UNIV"
   148 
   149 abbreviation
   150   "surj f \<equiv> surj_on f UNIV"
   151 
   152 abbreviation
   153   "bij f \<equiv> bij_betw f UNIV UNIV"
   154 
   155 lemma injI:
   156   assumes "\<And>x y. f x = f y \<Longrightarrow> x = y"
   157   shows "inj f"
   158   using assms unfolding inj_on_def by auto
   159 
   160 text{*For Proofs in @{text "Tools/Datatype/datatype_rep_proofs"}*}
   161 lemma datatype_injI:
   162     "(!! x. ALL y. f(x) = f(y) --> x=y) ==> inj(f)"
   163 by (simp add: inj_on_def)
   164 
   165 theorem range_ex1_eq: "inj f \<Longrightarrow> b : range f = (EX! x. b = f x)"
   166   by (unfold inj_on_def, blast)
   167 
   168 lemma injD: "[| inj(f); f(x) = f(y) |] ==> x=y"
   169 by (simp add: inj_on_def)
   170 
   171 lemma inj_on_eq_iff: "inj_on f A ==> x:A ==> y:A ==> (f(x) = f(y)) = (x=y)"
   172 by (force simp add: inj_on_def)
   173 
   174 lemma inj_comp:
   175   "inj f \<Longrightarrow> inj g \<Longrightarrow> inj (f \<circ> g)"
   176   by (simp add: inj_on_def)
   177 
   178 lemma inj_fun: "inj f \<Longrightarrow> inj (\<lambda>x y. f x)"
   179   by (simp add: inj_on_def fun_eq_iff)
   180 
   181 lemma inj_eq: "inj f ==> (f(x) = f(y)) = (x=y)"
   182 by (simp add: inj_on_eq_iff)
   183 
   184 lemma inj_on_id[simp]: "inj_on id A"
   185   by (simp add: inj_on_def)
   186 
   187 lemma inj_on_id2[simp]: "inj_on (%x. x) A"
   188 by (simp add: inj_on_def)
   189 
   190 lemma surj_id[simp]: "surj_on id A"
   191 by (simp add: surj_on_def)
   192 
   193 lemma bij_id[simp]: "bij id"
   194 by (simp add: bij_betw_def)
   195 
   196 lemma inj_onI:
   197     "(!! x y. [|  x:A;  y:A;  f(x) = f(y) |] ==> x=y) ==> inj_on f A"
   198 by (simp add: inj_on_def)
   199 
   200 lemma inj_on_inverseI: "(!!x. x:A ==> g(f(x)) = x) ==> inj_on f A"
   201 by (auto dest:  arg_cong [of concl: g] simp add: inj_on_def)
   202 
   203 lemma inj_onD: "[| inj_on f A;  f(x)=f(y);  x:A;  y:A |] ==> x=y"
   204 by (unfold inj_on_def, blast)
   205 
   206 lemma inj_on_iff: "[| inj_on f A;  x:A;  y:A |] ==> (f(x)=f(y)) = (x=y)"
   207 by (blast dest!: inj_onD)
   208 
   209 lemma comp_inj_on:
   210      "[| inj_on f A;  inj_on g (f`A) |] ==> inj_on (g o f) A"
   211 by (simp add: comp_def inj_on_def)
   212 
   213 lemma inj_on_imageI: "inj_on (g o f) A \<Longrightarrow> inj_on g (f ` A)"
   214 apply(simp add:inj_on_def image_def)
   215 apply blast
   216 done
   217 
   218 lemma inj_on_image_iff: "\<lbrakk> ALL x:A. ALL y:A. (g(f x) = g(f y)) = (g x = g y);
   219   inj_on f A \<rbrakk> \<Longrightarrow> inj_on g (f ` A) = inj_on g A"
   220 apply(unfold inj_on_def)
   221 apply blast
   222 done
   223 
   224 lemma inj_on_contraD: "[| inj_on f A;  ~x=y;  x:A;  y:A |] ==> ~ f(x)=f(y)"
   225 by (unfold inj_on_def, blast)
   226 
   227 lemma inj_singleton: "inj (%s. {s})"
   228 by (simp add: inj_on_def)
   229 
   230 lemma inj_on_empty[iff]: "inj_on f {}"
   231 by(simp add: inj_on_def)
   232 
   233 lemma subset_inj_on: "[| inj_on f B; A <= B |] ==> inj_on f A"
   234 by (unfold inj_on_def, blast)
   235 
   236 lemma inj_on_Un:
   237  "inj_on f (A Un B) =
   238   (inj_on f A & inj_on f B & f`(A-B) Int f`(B-A) = {})"
   239 apply(unfold inj_on_def)
   240 apply (blast intro:sym)
   241 done
   242 
   243 lemma inj_on_insert[iff]:
   244   "inj_on f (insert a A) = (inj_on f A & f a ~: f`(A-{a}))"
   245 apply(unfold inj_on_def)
   246 apply (blast intro:sym)
   247 done
   248 
   249 lemma inj_on_diff: "inj_on f A ==> inj_on f (A-B)"
   250 apply(unfold inj_on_def)
   251 apply (blast)
   252 done
   253 
   254 lemma surj_onI: "(\<And>x. x \<in> B \<Longrightarrow> g (f x) = x) \<Longrightarrow> surj_on g B"
   255   by (simp add: surj_on_def) (blast intro: sym)
   256 
   257 lemma surj_onD: "surj_on f B \<Longrightarrow> y \<in> B \<Longrightarrow> \<exists>x. y = f x"
   258   by (auto simp: surj_on_def)
   259 
   260 lemma surj_on_range_iff: "surj_on f B \<longleftrightarrow> (\<exists>A. f ` A = B)"
   261   unfolding surj_on_def by (auto intro!: exI[of _ "f -` B"])
   262 
   263 lemma surj_def: "surj f \<longleftrightarrow> (\<forall>y. \<exists>x. y = f x)"
   264   by (simp add: surj_on_def subset_eq image_iff)
   265 
   266 lemma surjI: "(\<And> x. g (f x) = x) \<Longrightarrow> surj g"
   267   by (blast intro: surj_onI)
   268 
   269 lemma surjD: "surj f \<Longrightarrow> \<exists>x. y = f x"
   270   by (simp add: surj_def)
   271 
   272 lemma surjE: "surj f \<Longrightarrow> (\<And>x. y = f x \<Longrightarrow> C) \<Longrightarrow> C"
   273   by (simp add: surj_def, blast)
   274 
   275 lemma comp_surj: "[| surj f;  surj g |] ==> surj (g o f)"
   276 apply (simp add: comp_def surj_def, clarify)
   277 apply (drule_tac x = y in spec, clarify)
   278 apply (drule_tac x = x in spec, blast)
   279 done
   280 
   281 lemma surj_range: "surj f \<Longrightarrow> range f = UNIV"
   282   by (auto simp add: surj_on_def)
   283 
   284 lemma surj_range_iff: "surj f \<longleftrightarrow> range f = UNIV"
   285   unfolding surj_on_def by auto
   286 
   287 lemma bij_betw_imp_surj: "bij_betw f A UNIV \<Longrightarrow> surj f"
   288   unfolding bij_betw_def surj_range_iff by auto
   289 
   290 lemma bij_def: "bij f \<longleftrightarrow> inj f \<and> surj f"
   291   unfolding surj_range_iff bij_betw_def ..
   292 
   293 lemma bijI: "[| inj f; surj f |] ==> bij f"
   294 by (simp add: bij_def)
   295 
   296 lemma bij_is_inj: "bij f ==> inj f"
   297 by (simp add: bij_def)
   298 
   299 lemma bij_is_surj: "bij f ==> surj f"
   300 by (simp add: bij_def)
   301 
   302 lemma bij_betw_imp_inj_on: "bij_betw f A B \<Longrightarrow> inj_on f A"
   303 by (simp add: bij_betw_def)
   304 
   305 lemma bij_betw_imp_surj_on: "bij_betw f A B \<Longrightarrow> surj_on f B"
   306 by (auto simp: bij_betw_def surj_on_range_iff)
   307 
   308 lemma bij_comp: "bij f \<Longrightarrow> bij g \<Longrightarrow> bij (g o f)"
   309 by(fastsimp intro: comp_inj_on comp_surj simp: bij_def surj_range)
   310 
   311 lemma bij_betw_trans:
   312   "bij_betw f A B \<Longrightarrow> bij_betw g B C \<Longrightarrow> bij_betw (g o f) A C"
   313 by(auto simp add:bij_betw_def comp_inj_on)
   314 
   315 lemma bij_betw_inv: assumes "bij_betw f A B" shows "EX g. bij_betw g B A"
   316 proof -
   317   have i: "inj_on f A" and s: "f ` A = B"
   318     using assms by(auto simp:bij_betw_def)
   319   let ?P = "%b a. a:A \<and> f a = b" let ?g = "%b. The (?P b)"
   320   { fix a b assume P: "?P b a"
   321     hence ex1: "\<exists>a. ?P b a" using s unfolding image_def by blast
   322     hence uex1: "\<exists>!a. ?P b a" by(blast dest:inj_onD[OF i])
   323     hence " ?g b = a" using the1_equality[OF uex1, OF P] P by simp
   324   } note g = this
   325   have "inj_on ?g B"
   326   proof(rule inj_onI)
   327     fix x y assume "x:B" "y:B" "?g x = ?g y"
   328     from s `x:B` obtain a1 where a1: "?P x a1" unfolding image_def by blast
   329     from s `y:B` obtain a2 where a2: "?P y a2" unfolding image_def by blast
   330     from g[OF a1] a1 g[OF a2] a2 `?g x = ?g y` show "x=y" by simp
   331   qed
   332   moreover have "?g ` B = A"
   333   proof(auto simp:image_def)
   334     fix b assume "b:B"
   335     with s obtain a where P: "?P b a" unfolding image_def by blast
   336     thus "?g b \<in> A" using g[OF P] by auto
   337   next
   338     fix a assume "a:A"
   339     then obtain b where P: "?P b a" using s unfolding image_def by blast
   340     then have "b:B" using s unfolding image_def by blast
   341     with g[OF P] show "\<exists>b\<in>B. a = ?g b" by blast
   342   qed
   343   ultimately show ?thesis by(auto simp:bij_betw_def)
   344 qed
   345 
   346 lemma bij_betw_combine:
   347   assumes "bij_betw f A B" "bij_betw f C D" "B \<inter> D = {}"
   348   shows "bij_betw f (A \<union> C) (B \<union> D)"
   349   using assms unfolding bij_betw_def inj_on_Un image_Un by auto
   350 
   351 lemma surj_image_vimage_eq: "surj f ==> f ` (f -` A) = A"
   352 by (simp add: surj_range)
   353 
   354 lemma inj_vimage_image_eq: "inj f ==> f -` (f ` A) = A"
   355 by (simp add: inj_on_def, blast)
   356 
   357 lemma vimage_subsetD: "surj f ==> f -` B <= A ==> B <= f ` A"
   358 apply (unfold surj_def)
   359 apply (blast intro: sym)
   360 done
   361 
   362 lemma vimage_subsetI: "inj f ==> B <= f ` A ==> f -` B <= A"
   363 by (unfold inj_on_def, blast)
   364 
   365 lemma vimage_subset_eq: "bij f ==> (f -` B <= A) = (B <= f ` A)"
   366 apply (unfold bij_def)
   367 apply (blast del: subsetI intro: vimage_subsetI vimage_subsetD)
   368 done
   369 
   370 lemma inj_on_Un_image_eq_iff: "inj_on f (A \<union> B) \<Longrightarrow> f ` A = f ` B \<longleftrightarrow> A = B"
   371 by(blast dest: inj_onD)
   372 
   373 lemma inj_on_image_Int:
   374    "[| inj_on f C;  A<=C;  B<=C |] ==> f`(A Int B) = f`A Int f`B"
   375 apply (simp add: inj_on_def, blast)
   376 done
   377 
   378 lemma inj_on_image_set_diff:
   379    "[| inj_on f C;  A<=C;  B<=C |] ==> f`(A-B) = f`A - f`B"
   380 apply (simp add: inj_on_def, blast)
   381 done
   382 
   383 lemma image_Int: "inj f ==> f`(A Int B) = f`A Int f`B"
   384 by (simp add: inj_on_def, blast)
   385 
   386 lemma image_set_diff: "inj f ==> f`(A-B) = f`A - f`B"
   387 by (simp add: inj_on_def, blast)
   388 
   389 lemma inj_image_mem_iff: "inj f ==> (f a : f`A) = (a : A)"
   390 by (blast dest: injD)
   391 
   392 lemma inj_image_subset_iff: "inj f ==> (f`A <= f`B) = (A<=B)"
   393 by (simp add: inj_on_def, blast)
   394 
   395 lemma inj_image_eq_iff: "inj f ==> (f`A = f`B) = (A = B)"
   396 by (blast dest: injD)
   397 
   398 (*injectivity's required.  Left-to-right inclusion holds even if A is empty*)
   399 lemma image_INT:
   400    "[| inj_on f C;  ALL x:A. B x <= C;  j:A |]
   401     ==> f ` (INTER A B) = (INT x:A. f ` B x)"
   402 apply (simp add: inj_on_def, blast)
   403 done
   404 
   405 (*Compare with image_INT: no use of inj_on, and if f is surjective then
   406   it doesn't matter whether A is empty*)
   407 lemma bij_image_INT: "bij f ==> f ` (INTER A B) = (INT x:A. f ` B x)"
   408 apply (simp add: bij_def)
   409 apply (simp add: inj_on_def surj_def, blast)
   410 done
   411 
   412 lemma surj_Compl_image_subset: "surj f ==> -(f`A) <= f`(-A)"
   413 by (auto simp add: surj_def)
   414 
   415 lemma inj_image_Compl_subset: "inj f ==> f`(-A) <= -(f`A)"
   416 by (auto simp add: inj_on_def)
   417 
   418 lemma bij_image_Compl_eq: "bij f ==> f`(-A) = -(f`A)"
   419 apply (simp add: bij_def)
   420 apply (rule equalityI)
   421 apply (simp_all (no_asm_simp) add: inj_image_Compl_subset surj_Compl_image_subset)
   422 done
   423 
   424 lemma (in ordered_ab_group_add) inj_uminus[simp, intro]: "inj_on uminus A"
   425   by (auto intro!: inj_onI)
   426 
   427 lemma (in linorder) strict_mono_imp_inj_on: "strict_mono f \<Longrightarrow> inj_on f A"
   428   by (auto intro!: inj_onI dest: strict_mono_eq)
   429 
   430 subsection{*Function Updating*}
   431 
   432 definition
   433   fun_upd :: "('a => 'b) => 'a => 'b => ('a => 'b)" where
   434   "fun_upd f a b == % x. if x=a then b else f x"
   435 
   436 nonterminals
   437   updbinds updbind
   438 syntax
   439   "_updbind" :: "['a, 'a] => updbind"             ("(2_ :=/ _)")
   440   ""         :: "updbind => updbinds"             ("_")
   441   "_updbinds":: "[updbind, updbinds] => updbinds" ("_,/ _")
   442   "_Update"  :: "['a, updbinds] => 'a"            ("_/'((_)')" [1000, 0] 900)
   443 
   444 translations
   445   "_Update f (_updbinds b bs)" == "_Update (_Update f b) bs"
   446   "f(x:=y)" == "CONST fun_upd f x y"
   447 
   448 (* Hint: to define the sum of two functions (or maps), use sum_case.
   449          A nice infix syntax could be defined (in Datatype.thy or below) by
   450 notation
   451   sum_case  (infixr "'(+')"80)
   452 *)
   453 
   454 lemma fun_upd_idem_iff: "(f(x:=y) = f) = (f x = y)"
   455 apply (simp add: fun_upd_def, safe)
   456 apply (erule subst)
   457 apply (rule_tac [2] ext, auto)
   458 done
   459 
   460 (* f x = y ==> f(x:=y) = f *)
   461 lemmas fun_upd_idem = fun_upd_idem_iff [THEN iffD2, standard]
   462 
   463 (* f(x := f x) = f *)
   464 lemmas fun_upd_triv = refl [THEN fun_upd_idem]
   465 declare fun_upd_triv [iff]
   466 
   467 lemma fun_upd_apply [simp]: "(f(x:=y))z = (if z=x then y else f z)"
   468 by (simp add: fun_upd_def)
   469 
   470 (* fun_upd_apply supersedes these two,   but they are useful
   471    if fun_upd_apply is intentionally removed from the simpset *)
   472 lemma fun_upd_same: "(f(x:=y)) x = y"
   473 by simp
   474 
   475 lemma fun_upd_other: "z~=x ==> (f(x:=y)) z = f z"
   476 by simp
   477 
   478 lemma fun_upd_upd [simp]: "f(x:=y,x:=z) = f(x:=z)"
   479 by (simp add: fun_eq_iff)
   480 
   481 lemma fun_upd_twist: "a ~= c ==> (m(a:=b))(c:=d) = (m(c:=d))(a:=b)"
   482 by (rule ext, auto)
   483 
   484 lemma inj_on_fun_updI: "\<lbrakk> inj_on f A; y \<notin> f`A \<rbrakk> \<Longrightarrow> inj_on (f(x:=y)) A"
   485 by (fastsimp simp:inj_on_def image_def)
   486 
   487 lemma fun_upd_image:
   488      "f(x:=y) ` A = (if x \<in> A then insert y (f ` (A-{x})) else f ` A)"
   489 by auto
   490 
   491 lemma fun_upd_comp: "f \<circ> (g(x := y)) = (f \<circ> g)(x := f y)"
   492 by (auto intro: ext)
   493 
   494 
   495 subsection {* @{text override_on} *}
   496 
   497 definition
   498   override_on :: "('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'a set \<Rightarrow> 'a \<Rightarrow> 'b"
   499 where
   500   "override_on f g A = (\<lambda>a. if a \<in> A then g a else f a)"
   501 
   502 lemma override_on_emptyset[simp]: "override_on f g {} = f"
   503 by(simp add:override_on_def)
   504 
   505 lemma override_on_apply_notin[simp]: "a ~: A ==> (override_on f g A) a = f a"
   506 by(simp add:override_on_def)
   507 
   508 lemma override_on_apply_in[simp]: "a : A ==> (override_on f g A) a = g a"
   509 by(simp add:override_on_def)
   510 
   511 
   512 subsection {* @{text swap} *}
   513 
   514 definition
   515   swap :: "'a \<Rightarrow> 'a \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b)"
   516 where
   517   "swap a b f = f (a := f b, b:= f a)"
   518 
   519 lemma swap_self [simp]: "swap a a f = f"
   520 by (simp add: swap_def)
   521 
   522 lemma swap_commute: "swap a b f = swap b a f"
   523 by (rule ext, simp add: fun_upd_def swap_def)
   524 
   525 lemma swap_nilpotent [simp]: "swap a b (swap a b f) = f"
   526 by (rule ext, simp add: fun_upd_def swap_def)
   527 
   528 lemma swap_triple:
   529   assumes "a \<noteq> c" and "b \<noteq> c"
   530   shows "swap a b (swap b c (swap a b f)) = swap a c f"
   531   using assms by (simp add: fun_eq_iff swap_def)
   532 
   533 lemma comp_swap: "f \<circ> swap a b g = swap a b (f \<circ> g)"
   534 by (rule ext, simp add: fun_upd_def swap_def)
   535 
   536 lemma swap_image_eq [simp]:
   537   assumes "a \<in> A" "b \<in> A" shows "swap a b f ` A = f ` A"
   538 proof -
   539   have subset: "\<And>f. swap a b f ` A \<subseteq> f ` A"
   540     using assms by (auto simp: image_iff swap_def)
   541   then have "swap a b (swap a b f) ` A \<subseteq> (swap a b f) ` A" .
   542   with subset[of f] show ?thesis by auto
   543 qed
   544 
   545 lemma inj_on_imp_inj_on_swap:
   546   "\<lbrakk>inj_on f A; a \<in> A; b \<in> A\<rbrakk> \<Longrightarrow> inj_on (swap a b f) A"
   547   by (simp add: inj_on_def swap_def, blast)
   548 
   549 lemma inj_on_swap_iff [simp]:
   550   assumes A: "a \<in> A" "b \<in> A" shows "inj_on (swap a b f) A \<longleftrightarrow> inj_on f A"
   551 proof
   552   assume "inj_on (swap a b f) A"
   553   with A have "inj_on (swap a b (swap a b f)) A"
   554     by (iprover intro: inj_on_imp_inj_on_swap)
   555   thus "inj_on f A" by simp
   556 next
   557   assume "inj_on f A"
   558   with A show "inj_on (swap a b f) A" by (iprover intro: inj_on_imp_inj_on_swap)
   559 qed
   560 
   561 lemma surj_imp_surj_swap: "surj f \<Longrightarrow> surj (swap a b f)"
   562   unfolding surj_range_iff by simp
   563 
   564 lemma surj_swap_iff [simp]: "surj (swap a b f) \<longleftrightarrow> surj f"
   565   unfolding surj_range_iff by simp
   566 
   567 lemma bij_betw_swap_iff [simp]:
   568   "\<lbrakk> x \<in> A; y \<in> A \<rbrakk> \<Longrightarrow> bij_betw (swap x y f) A B \<longleftrightarrow> bij_betw f A B"
   569   by (auto simp: bij_betw_def)
   570 
   571 lemma bij_swap_iff [simp]: "bij (swap a b f) \<longleftrightarrow> bij f"
   572   by simp
   573 
   574 hide_const (open) swap
   575 
   576 subsection {* Inversion of injective functions *}
   577 
   578 definition the_inv_into :: "'a set => ('a => 'b) => ('b => 'a)" where
   579 "the_inv_into A f == %x. THE y. y : A & f y = x"
   580 
   581 lemma the_inv_into_f_f:
   582   "[| inj_on f A;  x : A |] ==> the_inv_into A f (f x) = x"
   583 apply (simp add: the_inv_into_def inj_on_def)
   584 apply blast
   585 done
   586 
   587 lemma f_the_inv_into_f:
   588   "inj_on f A ==> y : f`A  ==> f (the_inv_into A f y) = y"
   589 apply (simp add: the_inv_into_def)
   590 apply (rule the1I2)
   591  apply(blast dest: inj_onD)
   592 apply blast
   593 done
   594 
   595 lemma the_inv_into_into:
   596   "[| inj_on f A; x : f ` A; A <= B |] ==> the_inv_into A f x : B"
   597 apply (simp add: the_inv_into_def)
   598 apply (rule the1I2)
   599  apply(blast dest: inj_onD)
   600 apply blast
   601 done
   602 
   603 lemma the_inv_into_onto[simp]:
   604   "inj_on f A ==> the_inv_into A f ` (f ` A) = A"
   605 by (fast intro:the_inv_into_into the_inv_into_f_f[symmetric])
   606 
   607 lemma the_inv_into_f_eq:
   608   "[| inj_on f A; f x = y; x : A |] ==> the_inv_into A f y = x"
   609   apply (erule subst)
   610   apply (erule the_inv_into_f_f, assumption)
   611   done
   612 
   613 lemma the_inv_into_comp:
   614   "[| inj_on f (g ` A); inj_on g A; x : f ` g ` A |] ==>
   615   the_inv_into A (f o g) x = (the_inv_into A g o the_inv_into (g ` A) f) x"
   616 apply (rule the_inv_into_f_eq)
   617   apply (fast intro: comp_inj_on)
   618  apply (simp add: f_the_inv_into_f the_inv_into_into)
   619 apply (simp add: the_inv_into_into)
   620 done
   621 
   622 lemma inj_on_the_inv_into:
   623   "inj_on f A \<Longrightarrow> inj_on (the_inv_into A f) (f ` A)"
   624 by (auto intro: inj_onI simp: image_def the_inv_into_f_f)
   625 
   626 lemma bij_betw_the_inv_into:
   627   "bij_betw f A B \<Longrightarrow> bij_betw (the_inv_into A f) B A"
   628 by (auto simp add: bij_betw_def inj_on_the_inv_into the_inv_into_into)
   629 
   630 abbreviation the_inv :: "('a \<Rightarrow> 'b) \<Rightarrow> ('b \<Rightarrow> 'a)" where
   631   "the_inv f \<equiv> the_inv_into UNIV f"
   632 
   633 lemma the_inv_f_f:
   634   assumes "inj f"
   635   shows "the_inv f (f x) = x" using assms UNIV_I
   636   by (rule the_inv_into_f_f)
   637 
   638 
   639 subsection {* Proof tool setup *} 
   640 
   641 text {* simplifies terms of the form
   642   f(...,x:=y,...,x:=z,...) to f(...,x:=z,...) *}
   643 
   644 simproc_setup fun_upd2 ("f(v := w, x := y)") = {* fn _ =>
   645 let
   646   fun gen_fun_upd NONE T _ _ = NONE
   647     | gen_fun_upd (SOME f) T x y = SOME (Const (@{const_name fun_upd}, T) $ f $ x $ y)
   648   fun dest_fun_T1 (Type (_, T :: Ts)) = T
   649   fun find_double (t as Const (@{const_name fun_upd},T) $ f $ x $ y) =
   650     let
   651       fun find (Const (@{const_name fun_upd},T) $ g $ v $ w) =
   652             if v aconv x then SOME g else gen_fun_upd (find g) T v w
   653         | find t = NONE
   654     in (dest_fun_T1 T, gen_fun_upd (find f) T x y) end
   655 
   656   fun proc ss ct =
   657     let
   658       val ctxt = Simplifier.the_context ss
   659       val t = Thm.term_of ct
   660     in
   661       case find_double t of
   662         (T, NONE) => NONE
   663       | (T, SOME rhs) =>
   664           SOME (Goal.prove ctxt [] [] (Logic.mk_equals (t, rhs))
   665             (fn _ =>
   666               rtac eq_reflection 1 THEN
   667               rtac ext 1 THEN
   668               simp_tac (Simplifier.inherit_context ss @{simpset}) 1))
   669     end
   670 in proc end
   671 *}
   672 
   673 
   674 subsection {* Code generator setup *}
   675 
   676 types_code
   677   "fun"  ("(_ ->/ _)")
   678 attach (term_of) {*
   679 fun term_of_fun_type _ aT _ bT _ = Free ("<function>", aT --> bT);
   680 *}
   681 attach (test) {*
   682 fun gen_fun_type aF aT bG bT i =
   683   let
   684     val tab = Unsynchronized.ref [];
   685     fun mk_upd (x, (_, y)) t = Const ("Fun.fun_upd",
   686       (aT --> bT) --> aT --> bT --> aT --> bT) $ t $ aF x $ y ()
   687   in
   688     (fn x =>
   689        case AList.lookup op = (!tab) x of
   690          NONE =>
   691            let val p as (y, _) = bG i
   692            in (tab := (x, p) :: !tab; y) end
   693        | SOME (y, _) => y,
   694      fn () => Basics.fold mk_upd (!tab) (Const ("HOL.undefined", aT --> bT)))
   695   end;
   696 *}
   697 
   698 code_const "op \<circ>"
   699   (SML infixl 5 "o")
   700   (Haskell infixr 9 ".")
   701 
   702 code_const "id"
   703   (Haskell "id")
   704 
   705 end