src/HOL/Fun.thy
changeset 61799 4cf66f21b764
parent 61699 a81dc5c4d6a9
child 61955 e96292f32c3c
equal deleted inserted replaced
61798:27f3c10b0b50 61799:4cf66f21b764
    20   by (force intro: theI')
    20   by (force intro: theI')
    21 
    21 
    22 lemma b_uniq_choice: "\<forall>x\<in>S. \<exists>!y. Q x y \<Longrightarrow> \<exists>f. \<forall>x\<in>S. Q x (f x)"
    22 lemma b_uniq_choice: "\<forall>x\<in>S. \<exists>!y. Q x y \<Longrightarrow> \<exists>f. \<forall>x\<in>S. Q x (f x)"
    23   by (force intro: theI')
    23   by (force intro: theI')
    24 
    24 
    25 subsection \<open>The Identity Function @{text id}\<close>
    25 subsection \<open>The Identity Function \<open>id\<close>\<close>
    26 
    26 
    27 definition id :: "'a \<Rightarrow> 'a" where
    27 definition id :: "'a \<Rightarrow> 'a" where
    28   "id = (\<lambda>x. x)"
    28   "id = (\<lambda>x. x)"
    29 
    29 
    30 lemma id_apply [simp]: "id x = x"
    30 lemma id_apply [simp]: "id x = x"
    38 
    38 
    39 code_printing
    39 code_printing
    40   constant id \<rightharpoonup> (Haskell) "id"
    40   constant id \<rightharpoonup> (Haskell) "id"
    41 
    41 
    42 
    42 
    43 subsection \<open>The Composition Operator @{text "f \<circ> g"}\<close>
    43 subsection \<open>The Composition Operator \<open>f \<circ> g\<close>\<close>
    44 
    44 
    45 definition comp :: "('b \<Rightarrow> 'c) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'c" (infixl "o" 55) where
    45 definition comp :: "('b \<Rightarrow> 'c) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'c" (infixl "o" 55) where
    46   "f o g = (\<lambda>x. f (g x))"
    46   "f o g = (\<lambda>x. f (g x))"
    47 
    47 
    48 notation (xsymbols)
    48 notation (xsymbols)
   101 
   101 
   102 code_printing
   102 code_printing
   103   constant comp \<rightharpoonup> (SML) infixl 5 "o" and (Haskell) infixr 9 "."
   103   constant comp \<rightharpoonup> (SML) infixl 5 "o" and (Haskell) infixr 9 "."
   104 
   104 
   105 
   105 
   106 subsection \<open>The Forward Composition Operator @{text fcomp}\<close>
   106 subsection \<open>The Forward Composition Operator \<open>fcomp\<close>\<close>
   107 
   107 
   108 definition fcomp :: "('a \<Rightarrow> 'b) \<Rightarrow> ('b \<Rightarrow> 'c) \<Rightarrow> 'a \<Rightarrow> 'c" (infixl "\<circ>>" 60) where
   108 definition fcomp :: "('a \<Rightarrow> 'b) \<Rightarrow> ('b \<Rightarrow> 'c) \<Rightarrow> 'a \<Rightarrow> 'c" (infixl "\<circ>>" 60) where
   109   "f \<circ>> g = (\<lambda>x. g (f x))"
   109   "f \<circ>> g = (\<lambda>x. g (f x))"
   110 
   110 
   111 lemma fcomp_apply [simp]:  "(f \<circ>> g) x = g (f x)"
   111 lemma fcomp_apply [simp]:  "(f \<circ>> g) x = g (f x)"
   139   by (simp add: map_fun_def)
   139   by (simp add: map_fun_def)
   140 
   140 
   141 
   141 
   142 subsection \<open>Injectivity and Bijectivity\<close>
   142 subsection \<open>Injectivity and Bijectivity\<close>
   143 
   143 
   144 definition inj_on :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a set \<Rightarrow> bool" where -- "injective"
   144 definition inj_on :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a set \<Rightarrow> bool" where \<comment> "injective"
   145   "inj_on f A \<longleftrightarrow> (\<forall>x\<in>A. \<forall>y\<in>A. f x = f y \<longrightarrow> x = y)"
   145   "inj_on f A \<longleftrightarrow> (\<forall>x\<in>A. \<forall>y\<in>A. f x = f y \<longrightarrow> x = y)"
   146 
   146 
   147 definition bij_betw :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a set \<Rightarrow> 'b set \<Rightarrow> bool" where -- "bijective"
   147 definition bij_betw :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a set \<Rightarrow> 'b set \<Rightarrow> bool" where \<comment> "bijective"
   148   "bij_betw f A B \<longleftrightarrow> inj_on f A \<and> f ` A = B"
   148   "bij_betw f A B \<longleftrightarrow> inj_on f A \<and> f ` A = B"
   149 
   149 
   150 text\<open>A common special case: functions injective, surjective or bijective over
   150 text\<open>A common special case: functions injective, surjective or bijective over
   151 the entire domain type.\<close>
   151 the entire domain type.\<close>
   152 
   152 
   153 abbreviation
   153 abbreviation
   154   "inj f \<equiv> inj_on f UNIV"
   154   "inj f \<equiv> inj_on f UNIV"
   155 
   155 
   156 abbreviation surj :: "('a \<Rightarrow> 'b) \<Rightarrow> bool" where -- "surjective"
   156 abbreviation surj :: "('a \<Rightarrow> 'b) \<Rightarrow> bool" where \<comment> "surjective"
   157   "surj f \<equiv> (range f = UNIV)"
   157   "surj f \<equiv> (range f = UNIV)"
   158 
   158 
   159 abbreviation
   159 abbreviation
   160   "bij f \<equiv> bij_betw f UNIV UNIV"
   160   "bij f \<equiv> bij_betw f UNIV UNIV"
   161 
   161 
   288 qed
   288 qed
   289 
   289 
   290 lemma linorder_injI:
   290 lemma linorder_injI:
   291   assumes hyp: "\<And>x y. x < (y::'a::linorder) \<Longrightarrow> f x \<noteq> f y"
   291   assumes hyp: "\<And>x y. x < (y::'a::linorder) \<Longrightarrow> f x \<noteq> f y"
   292   shows "inj f"
   292   shows "inj f"
   293   -- \<open>Courtesy of Stephan Merz\<close>
   293   \<comment> \<open>Courtesy of Stephan Merz\<close>
   294 proof (rule inj_onI)
   294 proof (rule inj_onI)
   295   fix x y
   295   fix x y
   296   assume f_eq: "f x = f y"
   296   assume f_eq: "f x = f y"
   297   show "x = y" by (rule linorder_cases) (auto dest: hyp simp: f_eq)
   297   show "x = y" by (rule linorder_cases) (auto dest: hyp simp: f_eq)
   298 qed
   298 qed
   522 apply (rule equalityI)
   522 apply (rule equalityI)
   523 apply (simp_all (no_asm_simp) add: inj_image_Compl_subset surj_Compl_image_subset)
   523 apply (simp_all (no_asm_simp) add: inj_image_Compl_subset surj_Compl_image_subset)
   524 done
   524 done
   525 
   525 
   526 lemma inj_vimage_singleton: "inj f \<Longrightarrow> f -` {a} \<subseteq> {THE x. f x = a}"
   526 lemma inj_vimage_singleton: "inj f \<Longrightarrow> f -` {a} \<subseteq> {THE x. f x = a}"
   527   -- \<open>The inverse image of a singleton under an injective function
   527   \<comment> \<open>The inverse image of a singleton under an injective function
   528          is included in a singleton.\<close>
   528          is included in a singleton.\<close>
   529   apply (auto simp add: inj_on_def)
   529   apply (auto simp add: inj_on_def)
   530   apply (blast intro: the_equality [symmetric])
   530   apply (blast intro: the_equality [symmetric])
   531   done
   531   done
   532 
   532 
   667   by auto
   667   by auto
   668 
   668 
   669 lemma fun_upd_eqD: "f(x := y) = g(x := z) \<Longrightarrow> y = z"
   669 lemma fun_upd_eqD: "f(x := y) = g(x := z) \<Longrightarrow> y = z"
   670 by(simp add: fun_eq_iff split: split_if_asm)
   670 by(simp add: fun_eq_iff split: split_if_asm)
   671 
   671 
   672 subsection \<open>@{text override_on}\<close>
   672 subsection \<open>\<open>override_on\<close>\<close>
   673 
   673 
   674 definition override_on :: "('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'a set \<Rightarrow> 'a \<Rightarrow> 'b" where
   674 definition override_on :: "('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'a set \<Rightarrow> 'a \<Rightarrow> 'b" where
   675   "override_on f g A = (\<lambda>a. if a \<in> A then g a else f a)"
   675   "override_on f g A = (\<lambda>a. if a \<in> A then g a else f a)"
   676 
   676 
   677 lemma override_on_emptyset[simp]: "override_on f g {} = f"
   677 lemma override_on_emptyset[simp]: "override_on f g {} = f"
   682 
   682 
   683 lemma override_on_apply_in[simp]: "a : A ==> (override_on f g A) a = g a"
   683 lemma override_on_apply_in[simp]: "a : A ==> (override_on f g A) a = g a"
   684 by(simp add:override_on_def)
   684 by(simp add:override_on_def)
   685 
   685 
   686 
   686 
   687 subsection \<open>@{text swap}\<close>
   687 subsection \<open>\<open>swap\<close>\<close>
   688 
   688 
   689 definition swap :: "'a \<Rightarrow> 'a \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b)"
   689 definition swap :: "'a \<Rightarrow> 'a \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b)"
   690 where
   690 where
   691   "swap a b f = f (a := f b, b:= f a)"
   691   "swap a b f = f (a := f b, b:= f a)"
   692 
   692