
hoelzl@44791: (*  Title:      HOL/Library/Extended_Real.thy
wenzelm@42854:     Author:     Johannes Hölzl, TU München
wenzelm@42854:     Author:     Robert Himmelmann, TU München
wenzelm@42854:     Author:     Armin Heller, TU München
wenzelm@42854:     Author:     Bogdan Grechuk, University of Edinburgh
wenzelm@42854: *)
hoelzl@42844: 
hoelzl@42844: header {* Extended real number line *}
hoelzl@42844: 
hoelzl@44791: theory Extended_Real
haftmann@44812: imports Complex_Main Extended_Nat
hoelzl@42844: begin
hoelzl@42844: 
hoelzl@42851: text {*
hoelzl@42851: 
hoelzl@42851: For more lemmas about the extended real numbers go to
bulwahn@43471:   @{text "src/HOL/Multivariate_Analysis/Extended_Real_Limits.thy"}
hoelzl@42851: 
hoelzl@42851: *}
hoelzl@42851: 
hoelzl@42850: lemma (in complete_lattice) atLeast_eq_UNIV_iff: "{x..} = UNIV \<longleftrightarrow> x = bot"
hoelzl@42850: proof
hoelzl@42850:   assume "{x..} = UNIV"
hoelzl@42850:   show "x = bot"
hoelzl@42850:   proof (rule ccontr)
hoelzl@42850:     assume "x \<noteq> bot" then have "bot \<notin> {x..}" by (simp add: le_less)
hoelzl@42850:     then show False using `{x..} = UNIV` by simp
hoelzl@42850:   qed
hoelzl@42850: qed auto
hoelzl@42850: 
hoelzl@42850: lemma SUPR_pair:
hoelzl@42850:   "(SUP i : A. SUP j : B. f i j) = (SUP p : A \<times> B. f (fst p) (snd p))"
hoelzl@42851:   by (rule antisym) (auto intro!: SUP_leI le_SUPI2)
hoelzl@42850: 
hoelzl@42850: lemma INFI_pair:
hoelzl@42850:   "(INF i : A. INF j : B. f i j) = (INF p : A \<times> B. f (fst p) (snd p))"
hoelzl@42851:   by (rule antisym) (auto intro!: le_INFI INF_leI2)
hoelzl@42850: 
hoelzl@42844: subsection {* Definition and basic properties *}
hoelzl@42844: 
hoelzl@44791: datatype ereal = ereal real | PInfty | MInfty
hoelzl@42844: 
hoelzl@44791: instantiation ereal :: uminus
hoelzl@42844: begin
hoelzl@44791:   fun uminus_ereal where
hoelzl@44791:     "- (ereal r) = ereal (- r)"
hoelzl@44794:   | "- PInfty = MInfty"
hoelzl@44794:   | "- MInfty = PInfty"
hoelzl@42844:   instance ..
hoelzl@42844: end
hoelzl@42844: 
hoelzl@44794: instantiation ereal :: infinity
hoelzl@44794: begin
hoelzl@44794:   definition "(\<infinity>::ereal) = PInfty"
hoelzl@44794:   instance ..
hoelzl@44794: end
hoelzl@42847: 
hoelzl@44795: definition "ereal_of_enat n = (case n of enat n \<Rightarrow> ereal (real n) | \<infinity> \<Rightarrow> \<infinity>)"
hoelzl@42844: 
hoelzl@44794: declare [[coercion "ereal :: real \<Rightarrow> ereal"]]
hoelzl@44794: declare [[coercion "ereal_of_enat :: enat \<Rightarrow> ereal"]]
hoelzl@44794: declare [[coercion "(\<lambda>n. ereal (of_nat n)) :: nat \<Rightarrow> ereal"]]
hoelzl@42844: 
hoelzl@44791: lemma ereal_uminus_uminus[simp]:
hoelzl@44791:   fixes a :: ereal shows "- (- a) = a"
hoelzl@42844:   by (cases a) simp_all
hoelzl@42844: 
hoelzl@44794: lemma
hoelzl@44794:   shows PInfty_eq_infinity[simp]: "PInfty = \<infinity>"
hoelzl@44794:     and MInfty_eq_minfinity[simp]: "MInfty = - \<infinity>"
hoelzl@44794:     and MInfty_neq_PInfty[simp]: "\<infinity> \<noteq> - (\<infinity>::ereal)" "- \<infinity> \<noteq> (\<infinity>::ereal)"
hoelzl@44794:     and MInfty_neq_ereal[simp]: "ereal r \<noteq> - \<infinity>" "- \<infinity> \<noteq> ereal r"
hoelzl@44794:     and PInfty_neq_ereal[simp]: "ereal r \<noteq> \<infinity>" "\<infinity> \<noteq> ereal r"
hoelzl@44794:     and PInfty_cases[simp]: "(case \<infinity> of ereal r \<Rightarrow> f r | PInfty \<Rightarrow> y | MInfty \<Rightarrow> z) = y"
hoelzl@44794:     and MInfty_cases[simp]: "(case - \<infinity> of ereal r \<Rightarrow> f r | PInfty \<Rightarrow> y | MInfty \<Rightarrow> z) = z"
hoelzl@44794:   by (simp_all add: infinity_ereal_def)
hoelzl@42844: 
hoelzl@44804: declare
hoelzl@44804:   PInfty_eq_infinity[code_post]
hoelzl@44804:   MInfty_eq_minfinity[code_post]
hoelzl@44804: 
hoelzl@44804: lemma [code_unfold]:
hoelzl@44804:   "\<infinity> = PInfty"
hoelzl@44804:   "-PInfty = MInfty"
hoelzl@44804:   by simp_all
hoelzl@44804: 
hoelzl@44794: lemma inj_ereal[simp]: "inj_on ereal A"
hoelzl@44794:   unfolding inj_on_def by auto
hoelzl@42844: 
hoelzl@44791: lemma ereal_cases[case_names real PInf MInf, cases type: ereal]:
hoelzl@44791:   assumes "\<And>r. x = ereal r \<Longrightarrow> P"
hoelzl@42844:   assumes "x = \<infinity> \<Longrightarrow> P"
hoelzl@42844:   assumes "x = -\<infinity> \<Longrightarrow> P"
hoelzl@42844:   shows P
hoelzl@42844:   using assms by (cases x) auto
hoelzl@42844: 
hoelzl@44791: lemmas ereal2_cases = ereal_cases[case_product ereal_cases]
hoelzl@44791: lemmas ereal3_cases = ereal2_cases[case_product ereal_cases]
hoelzl@42844: 
hoelzl@44791: lemma ereal_uminus_eq_iff[simp]:
hoelzl@44791:   fixes a b :: ereal shows "-a = -b \<longleftrightarrow> a = b"
hoelzl@44791:   by (cases rule: ereal2_cases[of a b]) simp_all
hoelzl@42844: 
hoelzl@44791: function of_ereal :: "ereal \<Rightarrow> real" where
hoelzl@44791: "of_ereal (ereal r) = r" |
hoelzl@44791: "of_ereal \<infinity> = 0" |
hoelzl@44791: "of_ereal (-\<infinity>) = 0"
hoelzl@44791:   by (auto intro: ereal_cases)
hoelzl@42844: termination proof qed (rule wf_empty)
hoelzl@42844: 
hoelzl@42844: defs (overloaded)
hoelzl@44791:   real_of_ereal_def [code_unfold]: "real \<equiv> of_ereal"
hoelzl@42844: 
hoelzl@44791: lemma real_of_ereal[simp]:
hoelzl@44791:     "real (- x :: ereal) = - (real x)"
hoelzl@44791:     "real (ereal r) = r"
hoelzl@44794:     "real (\<infinity>::ereal) = 0"
hoelzl@44791:   by (cases x) (simp_all add: real_of_ereal_def)
hoelzl@42844: 
hoelzl@44791: lemma range_ereal[simp]: "range ereal = UNIV - {\<infinity>, -\<infinity>}"
hoelzl@42844: proof safe
hoelzl@44791:   fix x assume "x \<notin> range ereal" "x \<noteq> \<infinity>"
hoelzl@42844:   then show "x = -\<infinity>" by (cases x) auto
hoelzl@42844: qed auto
hoelzl@42844: 
hoelzl@44791: lemma ereal_range_uminus[simp]: "range uminus = (UNIV::ereal set)"
hoelzl@42850: proof safe
hoelzl@44791:   fix x :: ereal show "x \<in> range uminus" by (intro image_eqI[of _ _ "-x"]) auto
hoelzl@42850: qed auto
hoelzl@42850: 
hoelzl@44791: instantiation ereal :: number
hoelzl@42844: begin
hoelzl@44791: definition [simp]: "number_of x = ereal (number_of x)"
hoelzl@42844: instance proof qed
hoelzl@42844: end
hoelzl@42844: 
hoelzl@44791: instantiation ereal :: abs
hoelzl@42847: begin
hoelzl@44791:   function abs_ereal where
hoelzl@44791:     "\<bar>ereal r\<bar> = ereal \<bar>r\<bar>"
hoelzl@44794:   | "\<bar>-\<infinity>\<bar> = (\<infinity>::ereal)"
hoelzl@44794:   | "\<bar>\<infinity>\<bar> = (\<infinity>::ereal)"
hoelzl@44791:   by (auto intro: ereal_cases)
hoelzl@42847:   termination proof qed (rule wf_empty)
hoelzl@42847:   instance ..
hoelzl@42847: end
hoelzl@42847: 
hoelzl@44794: lemma abs_eq_infinity_cases[elim!]: "\<lbrakk> \<bar>x :: ereal\<bar> = \<infinity> ; x = \<infinity> \<Longrightarrow> P ; x = -\<infinity> \<Longrightarrow> P \<rbrakk> \<Longrightarrow> P"
hoelzl@42847:   by (cases x) auto
hoelzl@42847: 
hoelzl@44794: lemma abs_neq_infinity_cases[elim!]: "\<lbrakk> \<bar>x :: ereal\<bar> \<noteq> \<infinity> ; \<And>r. x = ereal r \<Longrightarrow> P \<rbrakk> \<Longrightarrow> P"
hoelzl@42847:   by (cases x) auto
hoelzl@42847: 
hoelzl@44791: lemma abs_ereal_uminus[simp]: "\<bar>- x\<bar> = \<bar>x::ereal\<bar>"
hoelzl@42847:   by (cases x) auto
hoelzl@42847: 
hoelzl@42844: subsubsection "Addition"
hoelzl@42844: 
hoelzl@44791: instantiation ereal :: comm_monoid_add
hoelzl@42844: begin
hoelzl@42844: 
hoelzl@44791: definition "0 = ereal 0"
hoelzl@42844: 
hoelzl@44791: function plus_ereal where
hoelzl@44791: "ereal r + ereal p = ereal (r + p)" |
hoelzl@44794: "\<infinity> + a = (\<infinity>::ereal)" |
hoelzl@44794: "a + \<infinity> = (\<infinity>::ereal)" |
hoelzl@44791: "ereal r + -\<infinity> = - \<infinity>" |
hoelzl@44794: "-\<infinity> + ereal p = -(\<infinity>::ereal)" |
hoelzl@44794: "-\<infinity> + -\<infinity> = -(\<infinity>::ereal)"
hoelzl@42844: proof -
hoelzl@42844:   case (goal1 P x)
hoelzl@42844:   moreover then obtain a b where "x = (a, b)" by (cases x) auto
hoelzl@42844:   ultimately show P
hoelzl@44791:    by (cases rule: ereal2_cases[of a b]) auto
hoelzl@42844: qed auto
hoelzl@42844: termination proof qed (rule wf_empty)
hoelzl@42844: 
hoelzl@42844: lemma Infty_neq_0[simp]:
hoelzl@44794:   "(\<infinity>::ereal) \<noteq> 0" "0 \<noteq> (\<infinity>::ereal)"
hoelzl@44794:   "-(\<infinity>::ereal) \<noteq> 0" "0 \<noteq> -(\<infinity>::ereal)"
hoelzl@44791:   by (simp_all add: zero_ereal_def)
hoelzl@42844: 
hoelzl@44791: lemma ereal_eq_0[simp]:
hoelzl@44791:   "ereal r = 0 \<longleftrightarrow> r = 0"
hoelzl@44791:   "0 = ereal r \<longleftrightarrow> r = 0"
hoelzl@44791:   unfolding zero_ereal_def by simp_all
hoelzl@42844: 
hoelzl@42844: instance
hoelzl@42844: proof
hoelzl@44791:   fix a :: ereal show "0 + a = a"
hoelzl@44791:     by (cases a) (simp_all add: zero_ereal_def)
hoelzl@44791:   fix b :: ereal show "a + b = b + a"
hoelzl@44791:     by (cases rule: ereal2_cases[of a b]) simp_all
hoelzl@44791:   fix c :: ereal show "a + b + c = a + (b + c)"
hoelzl@44791:     by (cases rule: ereal3_cases[of a b c]) simp_all
hoelzl@42844: qed
hoelzl@42844: end
hoelzl@42844: 
hoelzl@44791: lemma real_of_ereal_0[simp]: "real (0::ereal) = 0"
hoelzl@44791:   unfolding real_of_ereal_def zero_ereal_def by simp
hoelzl@43791: 
hoelzl@44791: lemma abs_ereal_zero[simp]: "\<bar>0\<bar> = (0::ereal)"
hoelzl@44791:   unfolding zero_ereal_def abs_ereal.simps by simp
hoelzl@42847: 
hoelzl@44791: lemma ereal_uminus_zero[simp]:
hoelzl@44791:   "- 0 = (0::ereal)"
hoelzl@44791:   by (simp add: zero_ereal_def)
hoelzl@42844: 
hoelzl@44791: lemma ereal_uminus_zero_iff[simp]:
hoelzl@44791:   fixes a :: ereal shows "-a = 0 \<longleftrightarrow> a = 0"
hoelzl@42844:   by (cases a) simp_all
hoelzl@42844: 
hoelzl@44791: lemma ereal_plus_eq_PInfty[simp]:
hoelzl@44794:   fixes a b :: ereal shows "a + b = \<infinity> \<longleftrightarrow> a = \<infinity> \<or> b = \<infinity>"
hoelzl@44791:   by (cases rule: ereal2_cases[of a b]) auto
hoelzl@42844: 
hoelzl@44791: lemma ereal_plus_eq_MInfty[simp]:
hoelzl@44794:   fixes a b :: ereal shows "a + b = -\<infinity> \<longleftrightarrow>
hoelzl@42844:     (a = -\<infinity> \<or> b = -\<infinity>) \<and> a \<noteq> \<infinity> \<and> b \<noteq> \<infinity>"
hoelzl@44791:   by (cases rule: ereal2_cases[of a b]) auto
hoelzl@42844: 
hoelzl@44791: lemma ereal_add_cancel_left:
hoelzl@44794:   fixes a b :: ereal assumes "a \<noteq> -\<infinity>"
hoelzl@42844:   shows "a + b = a + c \<longleftrightarrow> (a = \<infinity> \<or> b = c)"
hoelzl@44791:   using assms by (cases rule: ereal3_cases[of a b c]) auto
hoelzl@42844: 
hoelzl@44791: lemma ereal_add_cancel_right:
hoelzl@44794:   fixes a b :: ereal assumes "a \<noteq> -\<infinity>"
hoelzl@42844:   shows "b + a = c + a \<longleftrightarrow> (a = \<infinity> \<or> b = c)"
hoelzl@44791:   using assms by (cases rule: ereal3_cases[of a b c]) auto
hoelzl@42844: 
hoelzl@44791: lemma ereal_real:
hoelzl@44791:   "ereal (real x) = (if \<bar>x\<bar> = \<infinity> then 0 else x)"
hoelzl@42844:   by (cases x) simp_all
hoelzl@42844: 
hoelzl@44791: lemma real_of_ereal_add:
hoelzl@44791:   fixes a b :: ereal
hoelzl@42850:   shows "real (a + b) = (if (\<bar>a\<bar> = \<infinity>) \<and> (\<bar>b\<bar> = \<infinity>) \<or> (\<bar>a\<bar> \<noteq> \<infinity>) \<and> (\<bar>b\<bar> \<noteq> \<infinity>) then real a + real b else 0)"
hoelzl@44791:   by (cases rule: ereal2_cases[of a b]) auto
hoelzl@42850: 
hoelzl@44791: subsubsection "Linear order on @{typ ereal}"
hoelzl@42844: 
hoelzl@44791: instantiation ereal :: linorder
hoelzl@42844: begin
hoelzl@42844: 
hoelzl@44791: function less_ereal where
hoelzl@44794: "   ereal x < ereal y     \<longleftrightarrow> x < y" |
hoelzl@44794: "(\<infinity>::ereal) < a           \<longleftrightarrow> False" |
hoelzl@44794: "         a < -(\<infinity>::ereal) \<longleftrightarrow> False" |
hoelzl@44794: "ereal x    < \<infinity>           \<longleftrightarrow> True" |
hoelzl@44794: "        -\<infinity> < ereal r     \<longleftrightarrow> True" |
hoelzl@44794: "        -\<infinity> < (\<infinity>::ereal) \<longleftrightarrow> True"
hoelzl@42844: proof -
hoelzl@42844:   case (goal1 P x)
hoelzl@42844:   moreover then obtain a b where "x = (a,b)" by (cases x) auto
hoelzl@44791:   ultimately show P by (cases rule: ereal2_cases[of a b]) auto
hoelzl@42844: qed simp_all
hoelzl@42844: termination by (relation "{}") simp
hoelzl@42844: 
hoelzl@44791: definition "x \<le> (y::ereal) \<longleftrightarrow> x < y \<or> x = y"
hoelzl@42844: 
hoelzl@44791: lemma ereal_infty_less[simp]:
hoelzl@44794:   fixes x :: ereal
hoelzl@44794:   shows "x < \<infinity> \<longleftrightarrow> (x \<noteq> \<infinity>)"
hoelzl@44794:     "-\<infinity> < x \<longleftrightarrow> (x \<noteq> -\<infinity>)"
hoelzl@42844:   by (cases x, simp_all) (cases x, simp_all)
hoelzl@42844: 
hoelzl@44791: lemma ereal_infty_less_eq[simp]:
hoelzl@44794:   fixes x :: ereal
hoelzl@44794:   shows "\<infinity> \<le> x \<longleftrightarrow> x = \<infinity>"
hoelzl@42844:   "x \<le> -\<infinity> \<longleftrightarrow> x = -\<infinity>"
hoelzl@44791:   by (auto simp add: less_eq_ereal_def)
hoelzl@42844: 
hoelzl@44791: lemma ereal_less[simp]:
hoelzl@44791:   "ereal r < 0 \<longleftrightarrow> (r < 0)"
hoelzl@44791:   "0 < ereal r \<longleftrightarrow> (0 < r)"
hoelzl@44794:   "0 < (\<infinity>::ereal)"
hoelzl@44794:   "-(\<infinity>::ereal) < 0"
hoelzl@44791:   by (simp_all add: zero_ereal_def)
hoelzl@42844: 
hoelzl@44791: lemma ereal_less_eq[simp]:
hoelzl@44794:   "x \<le> (\<infinity>::ereal)"
hoelzl@44794:   "-(\<infinity>::ereal) \<le> x"
hoelzl@44791:   "ereal r \<le> ereal p \<longleftrightarrow> r \<le> p"
hoelzl@44791:   "ereal r \<le> 0 \<longleftrightarrow> r \<le> 0"
hoelzl@44791:   "0 \<le> ereal r \<longleftrightarrow> 0 \<le> r"
hoelzl@44791:   by (auto simp add: less_eq_ereal_def zero_ereal_def)
hoelzl@42844: 
hoelzl@44791: lemma ereal_infty_less_eq2:
hoelzl@44794:   "a \<le> b \<Longrightarrow> a = \<infinity> \<Longrightarrow> b = (\<infinity>::ereal)"
hoelzl@44794:   "a \<le> b \<Longrightarrow> b = -\<infinity> \<Longrightarrow> a = -(\<infinity>::ereal)"
hoelzl@42844:   by simp_all
hoelzl@42844: 
hoelzl@42844: instance
hoelzl@42844: proof
hoelzl@44791:   fix x :: ereal show "x \<le> x"
hoelzl@42844:     by (cases x) simp_all
hoelzl@44791:   fix y :: ereal show "x < y \<longleftrightarrow> x \<le> y \<and> \<not> y \<le> x"
hoelzl@44791:     by (cases rule: ereal2_cases[of x y]) auto
hoelzl@42844:   show "x \<le> y \<or> y \<le> x "
hoelzl@44791:     by (cases rule: ereal2_cases[of x y]) auto
hoelzl@42844:   { assume "x \<le> y" "y \<le> x" then show "x = y"
hoelzl@44791:     by (cases rule: ereal2_cases[of x y]) auto }
hoelzl@42844:   { fix z assume "x \<le> y" "y \<le> z" then show "x \<le> z"
hoelzl@44791:     by (cases rule: ereal3_cases[of x y z]) auto }
hoelzl@42844: qed
hoelzl@42844: end
hoelzl@42844: 
hoelzl@44791: instance ereal :: ordered_ab_semigroup_add
hoelzl@42849: proof
hoelzl@44791:   fix a b c :: ereal assume "a \<le> b" then show "c + a \<le> c + b"
hoelzl@44791:     by (cases rule: ereal3_cases[of a b c]) auto
hoelzl@42849: qed
hoelzl@42849: 
hoelzl@44791: lemma real_of_ereal_positive_mono:
hoelzl@44794:   fixes x y :: ereal shows "\<lbrakk>0 \<le> x; x \<le> y; y \<noteq> \<infinity>\<rbrakk> \<Longrightarrow> real x \<le> real y"
hoelzl@44791:   by (cases rule: ereal2_cases[of x y]) auto
hoelzl@43791: 
hoelzl@44791: lemma ereal_MInfty_lessI[intro, simp]:
hoelzl@44794:   fixes a :: ereal shows "a \<noteq> -\<infinity> \<Longrightarrow> -\<infinity> < a"
hoelzl@42844:   by (cases a) auto
hoelzl@42844: 
hoelzl@44791: lemma ereal_less_PInfty[intro, simp]:
hoelzl@44794:   fixes a :: ereal shows "a \<noteq> \<infinity> \<Longrightarrow> a < \<infinity>"
hoelzl@42844:   by (cases a) auto
hoelzl@42844: 
hoelzl@44791: lemma ereal_less_ereal_Ex:
hoelzl@44791:   fixes a b :: ereal
hoelzl@44791:   shows "x < ereal r \<longleftrightarrow> x = -\<infinity> \<or> (\<exists>p. p < r \<and> x = ereal p)"
hoelzl@42844:   by (cases x) auto
hoelzl@42844: 
hoelzl@44791: lemma less_PInf_Ex_of_nat: "x \<noteq> \<infinity> \<longleftrightarrow> (\<exists>n::nat. x < ereal (real n))"
hoelzl@42850: proof (cases x)
hoelzl@42850:   case (real r) then show ?thesis
hoelzl@42851:     using reals_Archimedean2[of r] by simp
hoelzl@42850: qed simp_all
hoelzl@42850: 
hoelzl@44791: lemma ereal_add_mono:
hoelzl@44791:   fixes a b c d :: ereal assumes "a \<le> b" "c \<le> d" shows "a + c \<le> b + d"
hoelzl@42844:   using assms
hoelzl@42844:   apply (cases a)
hoelzl@44791:   apply (cases rule: ereal3_cases[of b c d], auto)
hoelzl@44791:   apply (cases rule: ereal3_cases[of b c d], auto)
hoelzl@42844:   done
hoelzl@42844: 
hoelzl@44791: lemma ereal_minus_le_minus[simp]:
hoelzl@44791:   fixes a b :: ereal shows "- a \<le> - b \<longleftrightarrow> b \<le> a"
hoelzl@44791:   by (cases rule: ereal2_cases[of a b]) auto
hoelzl@42844: 
hoelzl@44791: lemma ereal_minus_less_minus[simp]:
hoelzl@44791:   fixes a b :: ereal shows "- a < - b \<longleftrightarrow> b < a"
hoelzl@44791:   by (cases rule: ereal2_cases[of a b]) auto
hoelzl@42844: 
hoelzl@44791: lemma ereal_le_real_iff:
hoelzl@44791:   "x \<le> real y \<longleftrightarrow> ((\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> ereal x \<le> y) \<and> (\<bar>y\<bar> = \<infinity> \<longrightarrow> x \<le> 0))"
hoelzl@42844:   by (cases y) auto
hoelzl@42844: 
hoelzl@44791: lemma real_le_ereal_iff:
hoelzl@44791:   "real y \<le> x \<longleftrightarrow> ((\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> y \<le> ereal x) \<and> (\<bar>y\<bar> = \<infinity> \<longrightarrow> 0 \<le> x))"
hoelzl@42844:   by (cases y) auto
hoelzl@42844: 
hoelzl@44791: lemma ereal_less_real_iff:
hoelzl@44791:   "x < real y \<longleftrightarrow> ((\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> ereal x < y) \<and> (\<bar>y\<bar> = \<infinity> \<longrightarrow> x < 0))"
hoelzl@42844:   by (cases y) auto
hoelzl@42844: 
hoelzl@44791: lemma real_less_ereal_iff:
hoelzl@44791:   "real y < x \<longleftrightarrow> ((\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> y < ereal x) \<and> (\<bar>y\<bar> = \<infinity> \<longrightarrow> 0 < x))"
hoelzl@42844:   by (cases y) auto
hoelzl@42844: 
hoelzl@44791: lemma real_of_ereal_pos:
hoelzl@44791:   fixes x :: ereal shows "0 \<le> x \<Longrightarrow> 0 \<le> real x" by (cases x) auto
hoelzl@42850: 
hoelzl@44791: lemmas real_of_ereal_ord_simps =
hoelzl@44791:   ereal_le_real_iff real_le_ereal_iff ereal_less_real_iff real_less_ereal_iff
hoelzl@42844: 
hoelzl@44791: lemma abs_ereal_ge0[simp]: "0 \<le> x \<Longrightarrow> \<bar>x :: ereal\<bar> = x"
hoelzl@43791:   by (cases x) auto
hoelzl@43791: 
hoelzl@44791: lemma abs_ereal_less0[simp]: "x < 0 \<Longrightarrow> \<bar>x :: ereal\<bar> = -x"
hoelzl@43791:   by (cases x) auto
hoelzl@43791: 
hoelzl@44791: lemma abs_ereal_pos[simp]: "0 \<le> \<bar>x :: ereal\<bar>"
hoelzl@43791:   by (cases x) auto
hoelzl@43791: 
hoelzl@44794: lemma real_of_ereal_le_0[simp]: "real (x :: ereal) \<le> 0 \<longleftrightarrow> (x \<le> 0 \<or> x = \<infinity>)"
hoelzl@44794:   by (cases x) auto
hoelzl@43791: 
hoelzl@44794: lemma abs_real_of_ereal[simp]: "\<bar>real (x :: ereal)\<bar> = real \<bar>x\<bar>"
hoelzl@44794:   by (cases x) auto
hoelzl@43791: 
hoelzl@44794: lemma zero_less_real_of_ereal:
hoelzl@44794:   fixes x :: ereal shows "0 < real x \<longleftrightarrow> (0 < x \<and> x \<noteq> \<infinity>)"
hoelzl@44794:   by (cases x) auto
hoelzl@43791: 
hoelzl@44791: lemma ereal_0_le_uminus_iff[simp]:
hoelzl@44791:   fixes a :: ereal shows "0 \<le> -a \<longleftrightarrow> a \<le> 0"
hoelzl@44791:   by (cases rule: ereal2_cases[of a]) auto
hoelzl@43791: 
hoelzl@44791: lemma ereal_uminus_le_0_iff[simp]:
hoelzl@44791:   fixes a :: ereal shows "-a \<le> 0 \<longleftrightarrow> 0 \<le> a"
hoelzl@44791:   by (cases rule: ereal2_cases[of a]) auto
hoelzl@43791: 
hoelzl@44794: lemma ereal_dense2: "x < y \<Longrightarrow> \<exists>z. x < ereal z \<and> ereal z < y"
hoelzl@44794:   using lt_ex gt_ex dense by (cases x y rule: ereal2_cases) auto
hoelzl@44794: 
hoelzl@44791: lemma ereal_dense:
hoelzl@44791:   fixes x y :: ereal assumes "x < y"
hoelzl@44794:   shows "\<exists>z. x < z \<and> z < y"
hoelzl@44794:   using ereal_dense2[OF `x < y`] by blast
hoelzl@42844: 
hoelzl@44791: lemma ereal_add_strict_mono:
hoelzl@44791:   fixes a b c d :: ereal
hoelzl@42850:   assumes "a = b" "0 \<le> a" "a \<noteq> \<infinity>" "c < d"
hoelzl@42850:   shows "a + c < b + d"
hoelzl@44791:   using assms by (cases rule: ereal3_cases[case_product ereal_cases, of a b c d]) auto
hoelzl@42850: 
hoelzl@44794: lemma ereal_less_add: 
hoelzl@44794:   fixes a b c :: ereal shows "\<bar>a\<bar> \<noteq> \<infinity> \<Longrightarrow> c < b \<Longrightarrow> a + c < a + b"
hoelzl@44791:   by (cases rule: ereal2_cases[of b c]) auto
hoelzl@42850: 
hoelzl@44791: lemma ereal_uminus_eq_reorder: "- a = b \<longleftrightarrow> a = (-b::ereal)" by auto
hoelzl@42850: 
hoelzl@44791: lemma ereal_uminus_less_reorder: "- a < b \<longleftrightarrow> -b < (a::ereal)"
hoelzl@44791:   by (subst (3) ereal_uminus_uminus[symmetric]) (simp only: ereal_minus_less_minus)
hoelzl@42850: 
hoelzl@44791: lemma ereal_uminus_le_reorder: "- a \<le> b \<longleftrightarrow> -b \<le> (a::ereal)"
hoelzl@44791:   by (subst (3) ereal_uminus_uminus[symmetric]) (simp only: ereal_minus_le_minus)
hoelzl@42850: 
hoelzl@44791: lemmas ereal_uminus_reorder =
hoelzl@44791:   ereal_uminus_eq_reorder ereal_uminus_less_reorder ereal_uminus_le_reorder
hoelzl@42850: 
hoelzl@44791: lemma ereal_bot:
hoelzl@44791:   fixes x :: ereal assumes "\<And>B. x \<le> ereal B" shows "x = - \<infinity>"
hoelzl@42850: proof (cases x)
hoelzl@42850:   case (real r) with assms[of "r - 1"] show ?thesis by auto
hoelzl@42850: next case PInf with assms[of 0] show ?thesis by auto
hoelzl@42850: next case MInf then show ?thesis by simp
hoelzl@42850: qed
hoelzl@42850: 
hoelzl@44791: lemma ereal_top:
hoelzl@44791:   fixes x :: ereal assumes "\<And>B. x \<ge> ereal B" shows "x = \<infinity>"
hoelzl@42850: proof (cases x)
hoelzl@42850:   case (real r) with assms[of "r + 1"] show ?thesis by auto
hoelzl@42850: next case MInf with assms[of 0] show ?thesis by auto
hoelzl@42850: next case PInf then show ?thesis by simp
hoelzl@42850: qed
hoelzl@42850: 
hoelzl@42850: lemma
hoelzl@44791:   shows ereal_max[simp]: "ereal (max x y) = max (ereal x) (ereal y)"
hoelzl@44791:     and ereal_min[simp]: "ereal (min x y) = min (ereal x) (ereal y)"
hoelzl@42850:   by (simp_all add: min_def max_def)
hoelzl@42850: 
hoelzl@44791: lemma ereal_max_0: "max 0 (ereal r) = ereal (max 0 r)"
hoelzl@44791:   by (auto simp: zero_ereal_def)
hoelzl@42850: 
hoelzl@42849: lemma
hoelzl@44791:   fixes f :: "nat \<Rightarrow> ereal"
hoelzl@42849:   shows incseq_uminus[simp]: "incseq (\<lambda>x. - f x) \<longleftrightarrow> decseq f"
hoelzl@42849:   and decseq_uminus[simp]: "decseq (\<lambda>x. - f x) \<longleftrightarrow> incseq f"
hoelzl@42849:   unfolding decseq_def incseq_def by auto
hoelzl@42849: 
hoelzl@44791: lemma incseq_ereal: "incseq f \<Longrightarrow> incseq (\<lambda>x. ereal (f x))"
hoelzl@43791:   unfolding incseq_def by auto
hoelzl@43791: 
hoelzl@44791: lemma ereal_add_nonneg_nonneg:
hoelzl@44791:   fixes a b :: ereal shows "0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> 0 \<le> a + b"
hoelzl@42849:   using add_mono[of 0 a 0 b] by simp
hoelzl@42849: 
hoelzl@42849: lemma image_eqD: "f ` A = B \<Longrightarrow> (\<forall>x\<in>A. f x \<in> B)"
hoelzl@42849:   by auto
hoelzl@42849: 
hoelzl@42849: lemma incseq_setsumI:
hoelzl@42850:   fixes f :: "nat \<Rightarrow> 'a::{comm_monoid_add, ordered_ab_semigroup_add}"
hoelzl@42849:   assumes "\<And>i. 0 \<le> f i"
hoelzl@42849:   shows "incseq (\<lambda>i. setsum f {..< i})"
hoelzl@42849: proof (intro incseq_SucI)
hoelzl@42849:   fix n have "setsum f {..< n} + 0 \<le> setsum f {..<n} + f n"
hoelzl@42849:     using assms by (rule add_left_mono)
hoelzl@42849:   then show "setsum f {..< n} \<le> setsum f {..< Suc n}"
hoelzl@42849:     by auto
hoelzl@42849: qed
hoelzl@42849: 
hoelzl@42850: lemma incseq_setsumI2:
hoelzl@42850:   fixes f :: "'i \<Rightarrow> nat \<Rightarrow> 'a::{comm_monoid_add, ordered_ab_semigroup_add}"
hoelzl@42850:   assumes "\<And>n. n \<in> A \<Longrightarrow> incseq (f n)"
hoelzl@42850:   shows "incseq (\<lambda>i. \<Sum>n\<in>A. f n i)"
hoelzl@42850:   using assms unfolding incseq_def by (auto intro: setsum_mono)
hoelzl@42850: 
hoelzl@42844: subsubsection "Multiplication"
hoelzl@42844: 
hoelzl@44791: instantiation ereal :: "{comm_monoid_mult, sgn}"
hoelzl@42844: begin
hoelzl@42844: 
hoelzl@44791: definition "1 = ereal 1"
hoelzl@42844: 
hoelzl@44791: function sgn_ereal where
hoelzl@44791:   "sgn (ereal r) = ereal (sgn r)"
hoelzl@44794: | "sgn (\<infinity>::ereal) = 1"
hoelzl@44794: | "sgn (-\<infinity>::ereal) = -1"
hoelzl@44791: by (auto intro: ereal_cases)
hoelzl@42847: termination proof qed (rule wf_empty)
hoelzl@42847: 
hoelzl@44791: function times_ereal where
hoelzl@44791: "ereal r * ereal p = ereal (r * p)" |
hoelzl@44791: "ereal r * \<infinity> = (if r = 0 then 0 else if r > 0 then \<infinity> else -\<infinity>)" |
hoelzl@44791: "\<infinity> * ereal r = (if r = 0 then 0 else if r > 0 then \<infinity> else -\<infinity>)" |
hoelzl@44791: "ereal r * -\<infinity> = (if r = 0 then 0 else if r > 0 then -\<infinity> else \<infinity>)" |
hoelzl@44791: "-\<infinity> * ereal r = (if r = 0 then 0 else if r > 0 then -\<infinity> else \<infinity>)" |
hoelzl@44794: "(\<infinity>::ereal) * \<infinity> = \<infinity>" |
hoelzl@44794: "-(\<infinity>::ereal) * \<infinity> = -\<infinity>" |
hoelzl@44794: "(\<infinity>::ereal) * -\<infinity> = -\<infinity>" |
hoelzl@44794: "-(\<infinity>::ereal) * -\<infinity> = \<infinity>"
hoelzl@42844: proof -
hoelzl@42844:   case (goal1 P x)
hoelzl@42844:   moreover then obtain a b where "x = (a, b)" by (cases x) auto
hoelzl@44791:   ultimately show P by (cases rule: ereal2_cases[of a b]) auto
hoelzl@42844: qed simp_all
hoelzl@42844: termination by (relation "{}") simp
hoelzl@42844: 
hoelzl@42844: instance
hoelzl@42844: proof
hoelzl@44791:   fix a :: ereal show "1 * a = a"
hoelzl@44791:     by (cases a) (simp_all add: one_ereal_def)
hoelzl@44791:   fix b :: ereal show "a * b = b * a"
hoelzl@44791:     by (cases rule: ereal2_cases[of a b]) simp_all
hoelzl@44791:   fix c :: ereal show "a * b * c = a * (b * c)"
hoelzl@44791:     by (cases rule: ereal3_cases[of a b c])
hoelzl@44791:        (simp_all add: zero_ereal_def zero_less_mult_iff)
hoelzl@42844: qed
hoelzl@42844: end
hoelzl@42844: 
hoelzl@44791: lemma real_of_ereal_le_1:
hoelzl@44791:   fixes a :: ereal shows "a \<le> 1 \<Longrightarrow> real a \<le> 1"
hoelzl@44791:   by (cases a) (auto simp: one_ereal_def)
hoelzl@43791: 
hoelzl@44791: lemma abs_ereal_one[simp]: "\<bar>1\<bar> = (1::ereal)"
hoelzl@44791:   unfolding one_ereal_def by simp
hoelzl@42847: 
hoelzl@44791: lemma ereal_mult_zero[simp]:
hoelzl@44791:   fixes a :: ereal shows "a * 0 = 0"
hoelzl@44791:   by (cases a) (simp_all add: zero_ereal_def)
hoelzl@42844: 
hoelzl@44791: lemma ereal_zero_mult[simp]:
hoelzl@44791:   fixes a :: ereal shows "0 * a = 0"
hoelzl@44791:   by (cases a) (simp_all add: zero_ereal_def)
hoelzl@42844: 
hoelzl@44791: lemma ereal_m1_less_0[simp]:
hoelzl@44791:   "-(1::ereal) < 0"
hoelzl@44791:   by (simp add: zero_ereal_def one_ereal_def)
hoelzl@42844: 
hoelzl@44791: lemma ereal_zero_m1[simp]:
hoelzl@44791:   "1 \<noteq> (0::ereal)"
hoelzl@44791:   by (simp add: zero_ereal_def one_ereal_def)
hoelzl@42844: 
hoelzl@44791: lemma ereal_times_0[simp]:
hoelzl@44791:   fixes x :: ereal shows "0 * x = 0"
hoelzl@44791:   by (cases x) (auto simp: zero_ereal_def)
hoelzl@42844: 
hoelzl@44791: lemma ereal_times[simp]:
hoelzl@44794:   "1 \<noteq> (\<infinity>::ereal)" "(\<infinity>::ereal) \<noteq> 1"
hoelzl@44794:   "1 \<noteq> -(\<infinity>::ereal)" "-(\<infinity>::ereal) \<noteq> 1"
hoelzl@44791:   by (auto simp add: times_ereal_def one_ereal_def)
hoelzl@42844: 
hoelzl@44791: lemma ereal_plus_1[simp]:
hoelzl@44791:   "1 + ereal r = ereal (r + 1)" "ereal r + 1 = ereal (r + 1)"
hoelzl@44794:   "1 + -(\<infinity>::ereal) = -\<infinity>" "-(\<infinity>::ereal) + 1 = -\<infinity>"
hoelzl@44791:   unfolding one_ereal_def by auto
hoelzl@42844: 
hoelzl@44791: lemma ereal_zero_times[simp]:
hoelzl@44791:   fixes a b :: ereal shows "a * b = 0 \<longleftrightarrow> a = 0 \<or> b = 0"
hoelzl@44791:   by (cases rule: ereal2_cases[of a b]) auto
hoelzl@42844: 
hoelzl@44791: lemma ereal_mult_eq_PInfty[simp]:
hoelzl@44794:   shows "a * b = (\<infinity>::ereal) \<longleftrightarrow>
hoelzl@42844:     (a = \<infinity> \<and> b > 0) \<or> (a > 0 \<and> b = \<infinity>) \<or> (a = -\<infinity> \<and> b < 0) \<or> (a < 0 \<and> b = -\<infinity>)"
hoelzl@44791:   by (cases rule: ereal2_cases[of a b]) auto
hoelzl@42844: 
hoelzl@44791: lemma ereal_mult_eq_MInfty[simp]:
hoelzl@44794:   shows "a * b = -(\<infinity>::ereal) \<longleftrightarrow>
hoelzl@42844:     (a = \<infinity> \<and> b < 0) \<or> (a < 0 \<and> b = \<infinity>) \<or> (a = -\<infinity> \<and> b > 0) \<or> (a > 0 \<and> b = -\<infinity>)"
hoelzl@44791:   by (cases rule: ereal2_cases[of a b]) auto
hoelzl@42844: 
hoelzl@44791: lemma ereal_0_less_1[simp]: "0 < (1::ereal)"
hoelzl@44791:   by (simp_all add: zero_ereal_def one_ereal_def)
hoelzl@42844: 
hoelzl@44791: lemma ereal_zero_one[simp]: "0 \<noteq> (1::ereal)"
hoelzl@44791:   by (simp_all add: zero_ereal_def one_ereal_def)
hoelzl@42844: 
hoelzl@44791: lemma ereal_mult_minus_left[simp]:
hoelzl@44791:   fixes a b :: ereal shows "-a * b = - (a * b)"
hoelzl@44791:   by (cases rule: ereal2_cases[of a b]) auto
hoelzl@42844: 
hoelzl@44791: lemma ereal_mult_minus_right[simp]:
hoelzl@44791:   fixes a b :: ereal shows "a * -b = - (a * b)"
hoelzl@44791:   by (cases rule: ereal2_cases[of a b]) auto
hoelzl@42844: 
hoelzl@44791: lemma ereal_mult_infty[simp]:
hoelzl@44794:   "a * (\<infinity>::ereal) = (if a = 0 then 0 else if 0 < a then \<infinity> else - \<infinity>)"
hoelzl@42844:   by (cases a) auto
hoelzl@42844: 
hoelzl@44791: lemma ereal_infty_mult[simp]:
hoelzl@44794:   "(\<infinity>::ereal) * a = (if a = 0 then 0 else if 0 < a then \<infinity> else - \<infinity>)"
hoelzl@42844:   by (cases a) auto
hoelzl@42844: 
hoelzl@44791: lemma ereal_mult_strict_right_mono:
hoelzl@44794:   assumes "a < b" and "0 < c" "c < (\<infinity>::ereal)"
hoelzl@42844:   shows "a * c < b * c"
hoelzl@42844:   using assms
hoelzl@44791:   by (cases rule: ereal3_cases[of a b c])
hoelzl@44791:      (auto simp: zero_le_mult_iff ereal_less_PInfty)
hoelzl@42844: 
hoelzl@44791: lemma ereal_mult_strict_left_mono:
hoelzl@44794:   "\<lbrakk> a < b ; 0 < c ; c < (\<infinity>::ereal)\<rbrakk> \<Longrightarrow> c * a < c * b"
hoelzl@44791:   using ereal_mult_strict_right_mono by (simp add: mult_commute[of c])
hoelzl@42844: 
hoelzl@44791: lemma ereal_mult_right_mono:
hoelzl@44791:   fixes a b c :: ereal shows "\<lbrakk>a \<le> b; 0 \<le> c\<rbrakk> \<Longrightarrow> a*c \<le> b*c"
hoelzl@42844:   using assms
hoelzl@42844:   apply (cases "c = 0") apply simp
hoelzl@44791:   by (cases rule: ereal3_cases[of a b c])
hoelzl@44791:      (auto simp: zero_le_mult_iff ereal_less_PInfty)
hoelzl@42844: 
hoelzl@44791: lemma ereal_mult_left_mono:
hoelzl@44791:   fixes a b c :: ereal shows "\<lbrakk>a \<le> b; 0 \<le> c\<rbrakk> \<Longrightarrow> c * a \<le> c * b"
hoelzl@44791:   using ereal_mult_right_mono by (simp add: mult_commute[of c])
hoelzl@42844: 
hoelzl@44791: lemma zero_less_one_ereal[simp]: "0 \<le> (1::ereal)"
hoelzl@44791:   by (simp add: one_ereal_def zero_ereal_def)
hoelzl@42849: 
hoelzl@44791: lemma ereal_0_le_mult[simp]: "0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> 0 \<le> a * (b :: ereal)"
hoelzl@44791:   by (cases rule: ereal2_cases[of a b]) (auto simp: mult_nonneg_nonneg)
hoelzl@42850: 
hoelzl@44791: lemma ereal_right_distrib:
hoelzl@44791:   fixes r a b :: ereal shows "0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> r * (a + b) = r * a + r * b"
hoelzl@44791:   by (cases rule: ereal3_cases[of r a b]) (simp_all add: field_simps)
hoelzl@42850: 
hoelzl@44791: lemma ereal_left_distrib:
hoelzl@44791:   fixes r a b :: ereal shows "0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> (a + b) * r = a * r + b * r"
hoelzl@44791:   by (cases rule: ereal3_cases[of r a b]) (simp_all add: field_simps)
hoelzl@42850: 
hoelzl@44791: lemma ereal_mult_le_0_iff:
hoelzl@44791:   fixes a b :: ereal
hoelzl@42850:   shows "a * b \<le> 0 \<longleftrightarrow> (0 \<le> a \<and> b \<le> 0) \<or> (a \<le> 0 \<and> 0 \<le> b)"
hoelzl@44791:   by (cases rule: ereal2_cases[of a b]) (simp_all add: mult_le_0_iff)
hoelzl@42850: 
hoelzl@44791: lemma ereal_zero_le_0_iff:
hoelzl@44791:   fixes a b :: ereal
hoelzl@42850:   shows "0 \<le> a * b \<longleftrightarrow> (0 \<le> a \<and> 0 \<le> b) \<or> (a \<le> 0 \<and> b \<le> 0)"
hoelzl@44791:   by (cases rule: ereal2_cases[of a b]) (simp_all add: zero_le_mult_iff)
hoelzl@42850: 
hoelzl@44791: lemma ereal_mult_less_0_iff:
hoelzl@44791:   fixes a b :: ereal
hoelzl@42850:   shows "a * b < 0 \<longleftrightarrow> (0 < a \<and> b < 0) \<or> (a < 0 \<and> 0 < b)"
hoelzl@44791:   by (cases rule: ereal2_cases[of a b]) (simp_all add: mult_less_0_iff)
hoelzl@42850: 
hoelzl@44791: lemma ereal_zero_less_0_iff:
hoelzl@44791:   fixes a b :: ereal
hoelzl@42850:   shows "0 < a * b \<longleftrightarrow> (0 < a \<and> 0 < b) \<or> (a < 0 \<and> b < 0)"
hoelzl@44791:   by (cases rule: ereal2_cases[of a b]) (simp_all add: zero_less_mult_iff)
hoelzl@42850: 
hoelzl@44791: lemma ereal_distrib:
hoelzl@44791:   fixes a b c :: ereal
hoelzl@42850:   assumes "a \<noteq> \<infinity> \<or> b \<noteq> -\<infinity>" "a \<noteq> -\<infinity> \<or> b \<noteq> \<infinity>" "\<bar>c\<bar> \<noteq> \<infinity>"
hoelzl@42850:   shows "(a + b) * c = a * c + b * c"
hoelzl@42850:   using assms
hoelzl@44791:   by (cases rule: ereal3_cases[of a b c]) (simp_all add: field_simps)
hoelzl@42850: 
hoelzl@44791: lemma ereal_le_epsilon:
hoelzl@44791:   fixes x y :: ereal
hoelzl@42850:   assumes "ALL e. 0 < e --> x <= y + e"
hoelzl@42850:   shows "x <= y"
hoelzl@42850: proof-
hoelzl@44791: { assume a: "EX r. y = ereal r"
hoelzl@44791:   from this obtain r where r_def: "y = ereal r" by auto
hoelzl@42850:   { assume "x=(-\<infinity>)" hence ?thesis by auto }
hoelzl@42850:   moreover
hoelzl@42850:   { assume "~(x=(-\<infinity>))"
hoelzl@44791:     from this obtain p where p_def: "x = ereal p"
hoelzl@42850:     using a assms[rule_format, of 1] by (cases x) auto
hoelzl@42850:     { fix e have "0 < e --> p <= r + e"
hoelzl@44791:       using assms[rule_format, of "ereal e"] p_def r_def by auto }
hoelzl@42850:     hence "p <= r" apply (subst field_le_epsilon) by auto
hoelzl@42850:     hence ?thesis using r_def p_def by auto
hoelzl@42850:   } ultimately have ?thesis by blast
hoelzl@42850: }
hoelzl@42850: moreover
hoelzl@42850: { assume "y=(-\<infinity>) | y=\<infinity>" hence ?thesis
hoelzl@42850:     using assms[rule_format, of 1] by (cases x) auto
hoelzl@42850: } ultimately show ?thesis by (cases y) auto
hoelzl@42850: qed
hoelzl@42850: 
hoelzl@42850: 
hoelzl@44791: lemma ereal_le_epsilon2:
hoelzl@44791:   fixes x y :: ereal
hoelzl@44791:   assumes "ALL e. 0 < e --> x <= y + ereal e"
hoelzl@42850:   shows "x <= y"
hoelzl@42850: proof-
hoelzl@44791: { fix e :: ereal assume "e>0"
hoelzl@42850:   { assume "e=\<infinity>" hence "x<=y+e" by auto }
hoelzl@42850:   moreover
hoelzl@42850:   { assume "e~=\<infinity>"
hoelzl@44791:     from this obtain r where "e = ereal r" using `e>0` apply (cases e) by auto
hoelzl@42850:     hence "x<=y+e" using assms[rule_format, of r] `e>0` by auto
hoelzl@42850:   } ultimately have "x<=y+e" by blast
hoelzl@44791: } from this show ?thesis using ereal_le_epsilon by auto
hoelzl@42850: qed
hoelzl@42850: 
hoelzl@44791: lemma ereal_le_real:
hoelzl@44791:   fixes x y :: ereal
hoelzl@44791:   assumes "ALL z. x <= ereal z --> y <= ereal z"
hoelzl@42850:   shows "y <= x"
hoelzl@44794: by (metis assms ereal_bot ereal_cases ereal_infty_less_eq ereal_less_eq linorder_le_cases)
hoelzl@42850: 
hoelzl@44791: lemma ereal_le_ereal:
hoelzl@44791:   fixes x y :: ereal
hoelzl@42850:   assumes "\<And>B. B < x \<Longrightarrow> B <= y"
hoelzl@42850:   shows "x <= y"
hoelzl@44791: by (metis assms ereal_dense leD linorder_le_less_linear)
hoelzl@42850: 
hoelzl@44791: lemma ereal_ge_ereal:
hoelzl@44791:   fixes x y :: ereal
hoelzl@42850:   assumes "ALL B. B>x --> B >= y"
hoelzl@42850:   shows "x >= y"
hoelzl@44791: by (metis assms ereal_dense leD linorder_le_less_linear)
hoelzl@42849: 
hoelzl@44791: lemma setprod_ereal_0:
hoelzl@44791:   fixes f :: "'a \<Rightarrow> ereal"
hoelzl@43791:   shows "(\<Prod>i\<in>A. f i) = 0 \<longleftrightarrow> (finite A \<and> (\<exists>i\<in>A. f i = 0))"
hoelzl@43791: proof cases
hoelzl@43791:   assume "finite A"
hoelzl@43791:   then show ?thesis by (induct A) auto
hoelzl@43791: qed auto
hoelzl@43791: 
hoelzl@44791: lemma setprod_ereal_pos:
hoelzl@44791:   fixes f :: "'a \<Rightarrow> ereal" assumes pos: "\<And>i. i \<in> I \<Longrightarrow> 0 \<le> f i" shows "0 \<le> (\<Prod>i\<in>I. f i)"
hoelzl@43791: proof cases
hoelzl@43791:   assume "finite I" from this pos show ?thesis by induct auto
hoelzl@43791: qed simp
hoelzl@43791: 
hoelzl@43791: lemma setprod_PInf:
hoelzl@44794:   fixes f :: "'a \<Rightarrow> ereal"
hoelzl@43791:   assumes "\<And>i. i \<in> I \<Longrightarrow> 0 \<le> f i"
hoelzl@43791:   shows "(\<Prod>i\<in>I. f i) = \<infinity> \<longleftrightarrow> finite I \<and> (\<exists>i\<in>I. f i = \<infinity>) \<and> (\<forall>i\<in>I. f i \<noteq> 0)"
hoelzl@43791: proof cases
hoelzl@43791:   assume "finite I" from this assms show ?thesis
hoelzl@43791:   proof (induct I)
hoelzl@43791:     case (insert i I)
hoelzl@44791:     then have pos: "0 \<le> f i" "0 \<le> setprod f I" by (auto intro!: setprod_ereal_pos)
hoelzl@43791:     from insert have "(\<Prod>j\<in>insert i I. f j) = \<infinity> \<longleftrightarrow> setprod f I * f i = \<infinity>" by auto
hoelzl@43791:     also have "\<dots> \<longleftrightarrow> (setprod f I = \<infinity> \<or> f i = \<infinity>) \<and> f i \<noteq> 0 \<and> setprod f I \<noteq> 0"
hoelzl@44791:       using setprod_ereal_pos[of I f] pos
hoelzl@44791:       by (cases rule: ereal2_cases[of "f i" "setprod f I"]) auto
hoelzl@43791:     also have "\<dots> \<longleftrightarrow> finite (insert i I) \<and> (\<exists>j\<in>insert i I. f j = \<infinity>) \<and> (\<forall>j\<in>insert i I. f j \<noteq> 0)"
hoelzl@44791:       using insert by (auto simp: setprod_ereal_0)
hoelzl@43791:     finally show ?case .
hoelzl@43791:   qed simp
hoelzl@43791: qed simp
hoelzl@43791: 
hoelzl@44791: lemma setprod_ereal: "(\<Prod>i\<in>A. ereal (f i)) = ereal (setprod f A)"
hoelzl@43791: proof cases
hoelzl@43791:   assume "finite A" then show ?thesis
hoelzl@44791:     by induct (auto simp: one_ereal_def)
hoelzl@44791: qed (simp add: one_ereal_def)
hoelzl@43791: 
hoelzl@42849: subsubsection {* Power *}
hoelzl@42849: 
hoelzl@44791: instantiation ereal :: power
hoelzl@42849: begin
hoelzl@44791: primrec power_ereal where
hoelzl@44791:   "power_ereal x 0 = 1" |
hoelzl@44791:   "power_ereal x (Suc n) = x * x ^ n"
hoelzl@42849: instance ..
hoelzl@42849: end
hoelzl@42849: 
hoelzl@44791: lemma ereal_power[simp]: "(ereal x) ^ n = ereal (x^n)"
hoelzl@44791:   by (induct n) (auto simp: one_ereal_def)
hoelzl@42849: 
hoelzl@44794: lemma ereal_power_PInf[simp]: "(\<infinity>::ereal) ^ n = (if n = 0 then 1 else \<infinity>)"
hoelzl@44791:   by (induct n) (auto simp: one_ereal_def)
hoelzl@42849: 
hoelzl@44791: lemma ereal_power_uminus[simp]:
hoelzl@44791:   fixes x :: ereal
hoelzl@42849:   shows "(- x) ^ n = (if even n then x ^ n else - (x^n))"
hoelzl@44791:   by (induct n) (auto simp: one_ereal_def)
hoelzl@42849: 
hoelzl@44791: lemma ereal_power_number_of[simp]:
hoelzl@44791:   "(number_of num :: ereal) ^ n = ereal (number_of num ^ n)"
hoelzl@44791:   by (induct n) (auto simp: one_ereal_def)
hoelzl@42850: 
hoelzl@44791: lemma zero_le_power_ereal[simp]:
hoelzl@44791:   fixes a :: ereal assumes "0 \<le> a"
hoelzl@42850:   shows "0 \<le> a ^ n"
hoelzl@44791:   using assms by (induct n) (auto simp: ereal_zero_le_0_iff)
hoelzl@42850: 
hoelzl@42844: subsubsection {* Subtraction *}
hoelzl@42844: 
hoelzl@44791: lemma ereal_minus_minus_image[simp]:
hoelzl@44791:   fixes S :: "ereal set"
hoelzl@42844:   shows "uminus ` uminus ` S = S"
hoelzl@42844:   by (auto simp: image_iff)
hoelzl@42844: 
hoelzl@44791: lemma ereal_uminus_lessThan[simp]:
hoelzl@44791:   fixes a :: ereal shows "uminus ` {..<a} = {-a<..}"
hoelzl@42844: proof (safe intro!: image_eqI)
hoelzl@42844:   fix x assume "-a < x"
hoelzl@44791:   then have "- x < - (- a)" by (simp del: ereal_uminus_uminus)
hoelzl@42844:   then show "- x < a" by simp
hoelzl@42844: qed auto
hoelzl@42844: 
hoelzl@44791: lemma ereal_uminus_greaterThan[simp]:
hoelzl@44791:   "uminus ` {(a::ereal)<..} = {..<-a}"
hoelzl@44791:   by (metis ereal_uminus_lessThan ereal_uminus_uminus
hoelzl@44791:             ereal_minus_minus_image)
hoelzl@42844: 
hoelzl@44791: instantiation ereal :: minus
hoelzl@42844: begin
hoelzl@44791: definition "x - y = x + -(y::ereal)"
hoelzl@42844: instance ..
hoelzl@42844: end
hoelzl@42844: 
hoelzl@44791: lemma ereal_minus[simp]:
hoelzl@44791:   "ereal r - ereal p = ereal (r - p)"
hoelzl@44791:   "-\<infinity> - ereal r = -\<infinity>"
hoelzl@44791:   "ereal r - \<infinity> = -\<infinity>"
hoelzl@44794:   "(\<infinity>::ereal) - x = \<infinity>"
hoelzl@44794:   "-(\<infinity>::ereal) - \<infinity> = -\<infinity>"
hoelzl@42844:   "x - -y = x + y"
hoelzl@42844:   "x - 0 = x"
hoelzl@42844:   "0 - x = -x"
hoelzl@44791:   by (simp_all add: minus_ereal_def)
hoelzl@42844: 
hoelzl@44791: lemma ereal_x_minus_x[simp]:
hoelzl@44794:   "x - x = (if \<bar>x\<bar> = \<infinity> then \<infinity> else 0::ereal)"
hoelzl@42844:   by (cases x) simp_all
hoelzl@42844: 
hoelzl@44791: lemma ereal_eq_minus_iff:
hoelzl@44791:   fixes x y z :: ereal
hoelzl@42844:   shows "x = z - y \<longleftrightarrow>
hoelzl@42847:     (\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> x + y = z) \<and>
hoelzl@42844:     (y = -\<infinity> \<longrightarrow> x = \<infinity>) \<and>
hoelzl@42844:     (y = \<infinity> \<longrightarrow> z = \<infinity> \<longrightarrow> x = \<infinity>) \<and>
hoelzl@42844:     (y = \<infinity> \<longrightarrow> z \<noteq> \<infinity> \<longrightarrow> x = -\<infinity>)"
hoelzl@44791:   by (cases rule: ereal3_cases[of x y z]) auto
hoelzl@42844: 
hoelzl@44791: lemma ereal_eq_minus:
hoelzl@44791:   fixes x y z :: ereal
hoelzl@42847:   shows "\<bar>y\<bar> \<noteq> \<infinity> \<Longrightarrow> x = z - y \<longleftrightarrow> x + y = z"
hoelzl@44791:   by (auto simp: ereal_eq_minus_iff)
hoelzl@42844: 
hoelzl@44791: lemma ereal_less_minus_iff:
hoelzl@44791:   fixes x y z :: ereal
hoelzl@42844:   shows "x < z - y \<longleftrightarrow>
hoelzl@42844:     (y = \<infinity> \<longrightarrow> z = \<infinity> \<and> x \<noteq> \<infinity>) \<and>
hoelzl@42844:     (y = -\<infinity> \<longrightarrow> x \<noteq> \<infinity>) \<and>
hoelzl@42847:     (\<bar>y\<bar> \<noteq> \<infinity>\<longrightarrow> x + y < z)"
hoelzl@44791:   by (cases rule: ereal3_cases[of x y z]) auto
hoelzl@42844: 
hoelzl@44791: lemma ereal_less_minus:
hoelzl@44791:   fixes x y z :: ereal
hoelzl@42847:   shows "\<bar>y\<bar> \<noteq> \<infinity> \<Longrightarrow> x < z - y \<longleftrightarrow> x + y < z"
hoelzl@44791:   by (auto simp: ereal_less_minus_iff)
hoelzl@42844: 
hoelzl@44791: lemma ereal_le_minus_iff:
hoelzl@44791:   fixes x y z :: ereal
hoelzl@42844:   shows "x \<le> z - y \<longleftrightarrow>
hoelzl@42844:     (y = \<infinity> \<longrightarrow> z \<noteq> \<infinity> \<longrightarrow> x = -\<infinity>) \<and>
hoelzl@42847:     (\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> x + y \<le> z)"
hoelzl@44791:   by (cases rule: ereal3_cases[of x y z]) auto
hoelzl@42844: 
hoelzl@44791: lemma ereal_le_minus:
hoelzl@44791:   fixes x y z :: ereal
hoelzl@42847:   shows "\<bar>y\<bar> \<noteq> \<infinity> \<Longrightarrow> x \<le> z - y \<longleftrightarrow> x + y \<le> z"
hoelzl@44791:   by (auto simp: ereal_le_minus_iff)
hoelzl@42844: 
hoelzl@44791: lemma ereal_minus_less_iff:
hoelzl@44791:   fixes x y z :: ereal
hoelzl@42844:   shows "x - y < z \<longleftrightarrow>
hoelzl@42844:     y \<noteq> -\<infinity> \<and> (y = \<infinity> \<longrightarrow> x \<noteq> \<infinity> \<and> z \<noteq> -\<infinity>) \<and>
hoelzl@42844:     (y \<noteq> \<infinity> \<longrightarrow> x < z + y)"
hoelzl@44791:   by (cases rule: ereal3_cases[of x y z]) auto
hoelzl@42844: 
hoelzl@44791: lemma ereal_minus_less:
hoelzl@44791:   fixes x y z :: ereal
hoelzl@42847:   shows "\<bar>y\<bar> \<noteq> \<infinity> \<Longrightarrow> x - y < z \<longleftrightarrow> x < z + y"
hoelzl@44791:   by (auto simp: ereal_minus_less_iff)
hoelzl@42844: 
hoelzl@44791: lemma ereal_minus_le_iff:
hoelzl@44791:   fixes x y z :: ereal
hoelzl@42844:   shows "x - y \<le> z \<longleftrightarrow>
hoelzl@42844:     (y = -\<infinity> \<longrightarrow> z = \<infinity>) \<and>
hoelzl@42844:     (y = \<infinity> \<longrightarrow> x = \<infinity> \<longrightarrow> z = \<infinity>) \<and>
hoelzl@42847:     (\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> x \<le> z + y)"
hoelzl@44791:   by (cases rule: ereal3_cases[of x y z]) auto
hoelzl@42844: 
hoelzl@44791: lemma ereal_minus_le:
hoelzl@44791:   fixes x y z :: ereal
hoelzl@42847:   shows "\<bar>y\<bar> \<noteq> \<infinity> \<Longrightarrow> x - y \<le> z \<longleftrightarrow> x \<le> z + y"
hoelzl@44791:   by (auto simp: ereal_minus_le_iff)
hoelzl@42844: 
hoelzl@44791: lemma ereal_minus_eq_minus_iff:
hoelzl@44791:   fixes a b c :: ereal
hoelzl@42844:   shows "a - b = a - c \<longleftrightarrow>
hoelzl@42844:     b = c \<or> a = \<infinity> \<or> (a = -\<infinity> \<and> b \<noteq> -\<infinity> \<and> c \<noteq> -\<infinity>)"
hoelzl@44791:   by (cases rule: ereal3_cases[of a b c]) auto
hoelzl@42844: 
hoelzl@44791: lemma ereal_add_le_add_iff:
hoelzl@44794:   fixes a b c :: ereal
hoelzl@44794:   shows "c + a \<le> c + b \<longleftrightarrow>
hoelzl@42844:     a \<le> b \<or> c = \<infinity> \<or> (c = -\<infinity> \<and> a \<noteq> \<infinity> \<and> b \<noteq> \<infinity>)"
hoelzl@44791:   by (cases rule: ereal3_cases[of a b c]) (simp_all add: field_simps)
hoelzl@42844: 
hoelzl@44791: lemma ereal_mult_le_mult_iff:
hoelzl@44794:   fixes a b c :: ereal
hoelzl@44794:   shows "\<bar>c\<bar> \<noteq> \<infinity> \<Longrightarrow> c * a \<le> c * b \<longleftrightarrow> (0 < c \<longrightarrow> a \<le> b) \<and> (c < 0 \<longrightarrow> b \<le> a)"
hoelzl@44791:   by (cases rule: ereal3_cases[of a b c]) (simp_all add: mult_le_cancel_left)
hoelzl@42844: 
hoelzl@44791: lemma ereal_minus_mono:
hoelzl@44791:   fixes A B C D :: ereal assumes "A \<le> B" "D \<le> C"
hoelzl@42850:   shows "A - C \<le> B - D"
hoelzl@42850:   using assms
hoelzl@44791:   by (cases rule: ereal3_cases[case_product ereal_cases, of A B C D]) simp_all
hoelzl@42850: 
hoelzl@44791: lemma real_of_ereal_minus:
hoelzl@44794:   fixes a b :: ereal
hoelzl@44794:   shows "real (a - b) = (if \<bar>a\<bar> = \<infinity> \<or> \<bar>b\<bar> = \<infinity> then 0 else real a - real b)"
hoelzl@44791:   by (cases rule: ereal2_cases[of a b]) auto
hoelzl@42850: 
hoelzl@44791: lemma ereal_diff_positive:
hoelzl@44791:   fixes a b :: ereal shows "a \<le> b \<Longrightarrow> 0 \<le> b - a"
hoelzl@44791:   by (cases rule: ereal2_cases[of a b]) auto
hoelzl@42850: 
hoelzl@44791: lemma ereal_between:
hoelzl@44791:   fixes x e :: ereal
hoelzl@42847:   assumes "\<bar>x\<bar> \<noteq> \<infinity>" "0 < e"
hoelzl@42844:   shows "x - e < x" "x < x + e"
hoelzl@42844: using assms apply (cases x, cases e) apply auto
hoelzl@42844: using assms by (cases x, cases e) auto
hoelzl@42844: 
hoelzl@42844: subsubsection {* Division *}
hoelzl@42844: 
hoelzl@44791: instantiation ereal :: inverse
hoelzl@42844: begin
hoelzl@42844: 
hoelzl@44791: function inverse_ereal where
hoelzl@44791: "inverse (ereal r) = (if r = 0 then \<infinity> else ereal (inverse r))" |
hoelzl@44794: "inverse (\<infinity>::ereal) = 0" |
hoelzl@44794: "inverse (-\<infinity>::ereal) = 0"
hoelzl@44791:   by (auto intro: ereal_cases)
hoelzl@42844: termination by (relation "{}") simp
hoelzl@42844: 
hoelzl@44791: definition "x / y = x * inverse (y :: ereal)"
hoelzl@42844: 
hoelzl@42844: instance proof qed
hoelzl@42844: end
hoelzl@42844: 
hoelzl@44791: lemma real_of_ereal_inverse[simp]:
hoelzl@44791:   fixes a :: ereal
hoelzl@43791:   shows "real (inverse a) = 1 / real a"
hoelzl@43791:   by (cases a) (auto simp: inverse_eq_divide)
hoelzl@43791: 
hoelzl@44791: lemma ereal_inverse[simp]:
hoelzl@44794:   "inverse (0::ereal) = \<infinity>"
hoelzl@44791:   "inverse (1::ereal) = 1"
hoelzl@44791:   by (simp_all add: one_ereal_def zero_ereal_def)
hoelzl@42844: 
hoelzl@44791: lemma ereal_divide[simp]:
hoelzl@44791:   "ereal r / ereal p = (if p = 0 then ereal r * \<infinity> else ereal (r / p))"
hoelzl@44791:   unfolding divide_ereal_def by (auto simp: divide_real_def)
hoelzl@42844: 
hoelzl@44791: lemma ereal_divide_same[simp]:
hoelzl@44794:   fixes x :: ereal shows "x / x = (if \<bar>x\<bar> = \<infinity> \<or> x = 0 then 0 else 1)"
hoelzl@42844:   by (cases x)
hoelzl@44791:      (simp_all add: divide_real_def divide_ereal_def one_ereal_def)
hoelzl@42844: 
hoelzl@44791: lemma ereal_inv_inv[simp]:
hoelzl@44794:   fixes x :: ereal shows "inverse (inverse x) = (if x \<noteq> -\<infinity> then x else \<infinity>)"
hoelzl@42844:   by (cases x) auto
hoelzl@42844: 
hoelzl@44791: lemma ereal_inverse_minus[simp]:
hoelzl@44794:   fixes x :: ereal shows "inverse (- x) = (if x = 0 then \<infinity> else -inverse x)"
hoelzl@42844:   by (cases x) simp_all
hoelzl@42844: 
hoelzl@44791: lemma ereal_uminus_divide[simp]:
hoelzl@44791:   fixes x y :: ereal shows "- x / y = - (x / y)"
hoelzl@44791:   unfolding divide_ereal_def by simp
hoelzl@42844: 
hoelzl@44791: lemma ereal_divide_Infty[simp]:
hoelzl@44794:   fixes x :: ereal shows "x / \<infinity> = 0" "x / -\<infinity> = 0"
hoelzl@44791:   unfolding divide_ereal_def by simp_all
hoelzl@42844: 
hoelzl@44791: lemma ereal_divide_one[simp]:
hoelzl@44791:   "x / 1 = (x::ereal)"
hoelzl@44791:   unfolding divide_ereal_def by simp
hoelzl@42844: 
hoelzl@44791: lemma ereal_divide_ereal[simp]:
hoelzl@44791:   "\<infinity> / ereal r = (if 0 \<le> r then \<infinity> else -\<infinity>)"
hoelzl@44791:   unfolding divide_ereal_def by simp
hoelzl@42844: 
hoelzl@44791: lemma zero_le_divide_ereal[simp]:
hoelzl@44791:   fixes a :: ereal assumes "0 \<le> a" "0 \<le> b"
hoelzl@42849:   shows "0 \<le> a / b"
hoelzl@44791:   using assms by (cases rule: ereal2_cases[of a b]) (auto simp: zero_le_divide_iff)
hoelzl@42849: 
hoelzl@44791: lemma ereal_le_divide_pos:
hoelzl@44794:   fixes x y z :: ereal shows "x > 0 \<Longrightarrow> x \<noteq> \<infinity> \<Longrightarrow> y \<le> z / x \<longleftrightarrow> x * y \<le> z"
hoelzl@44791:   by (cases rule: ereal3_cases[of x y z]) (auto simp: field_simps)
hoelzl@42844: 
hoelzl@44791: lemma ereal_divide_le_pos:
hoelzl@44794:   fixes x y z :: ereal shows "x > 0 \<Longrightarrow> x \<noteq> \<infinity> \<Longrightarrow> z / x \<le> y \<longleftrightarrow> z \<le> x * y"
hoelzl@44791:   by (cases rule: ereal3_cases[of x y z]) (auto simp: field_simps)
hoelzl@42844: 
hoelzl@44791: lemma ereal_le_divide_neg:
hoelzl@44794:   fixes x y z :: ereal shows "x < 0 \<Longrightarrow> x \<noteq> -\<infinity> \<Longrightarrow> y \<le> z / x \<longleftrightarrow> z \<le> x * y"
hoelzl@44791:   by (cases rule: ereal3_cases[of x y z]) (auto simp: field_simps)
hoelzl@42844: 
hoelzl@44791: lemma ereal_divide_le_neg:
hoelzl@44794:   fixes x y z :: ereal shows "x < 0 \<Longrightarrow> x \<noteq> -\<infinity> \<Longrightarrow> z / x \<le> y \<longleftrightarrow> x * y \<le> z"
hoelzl@44791:   by (cases rule: ereal3_cases[of x y z]) (auto simp: field_simps)
hoelzl@42844: 
hoelzl@44791: lemma ereal_inverse_antimono_strict:
hoelzl@44791:   fixes x y :: ereal
hoelzl@42844:   shows "0 \<le> x \<Longrightarrow> x < y \<Longrightarrow> inverse y < inverse x"
hoelzl@44791:   by (cases rule: ereal2_cases[of x y]) auto
hoelzl@42844: 
hoelzl@44791: lemma ereal_inverse_antimono:
hoelzl@44791:   fixes x y :: ereal
hoelzl@42844:   shows "0 \<le> x \<Longrightarrow> x <= y \<Longrightarrow> inverse y <= inverse x"
hoelzl@44791:   by (cases rule: ereal2_cases[of x y]) auto
hoelzl@42844: 
hoelzl@42844: lemma inverse_inverse_Pinfty_iff[simp]:
hoelzl@44794:   fixes x :: ereal shows "inverse x = \<infinity> \<longleftrightarrow> x = 0"
hoelzl@42844:   by (cases x) auto
hoelzl@42844: 
hoelzl@44791: lemma ereal_inverse_eq_0:
hoelzl@44794:   fixes x :: ereal shows "inverse x = 0 \<longleftrightarrow> x = \<infinity> \<or> x = -\<infinity>"
hoelzl@42844:   by (cases x) auto
hoelzl@42844: 
hoelzl@44791: lemma ereal_0_gt_inverse:
hoelzl@44791:   fixes x :: ereal shows "0 < inverse x \<longleftrightarrow> x \<noteq> \<infinity> \<and> 0 \<le> x"
hoelzl@42850:   by (cases x) auto
hoelzl@42850: 
hoelzl@44791: lemma ereal_mult_less_right:
hoelzl@44794:   fixes a b c :: ereal
hoelzl@42844:   assumes "b * a < c * a" "0 < a" "a < \<infinity>"
hoelzl@42844:   shows "b < c"
hoelzl@42844:   using assms
hoelzl@44791:   by (cases rule: ereal3_cases[of a b c])
hoelzl@42844:      (auto split: split_if_asm simp: zero_less_mult_iff zero_le_mult_iff)
hoelzl@42844: 
hoelzl@44791: lemma ereal_power_divide:
hoelzl@44794:   fixes x y :: ereal shows "y \<noteq> 0 \<Longrightarrow> (x / y) ^ n = x^n / y^n"
hoelzl@44791:   by (cases rule: ereal2_cases[of x y])
hoelzl@44791:      (auto simp: one_ereal_def zero_ereal_def power_divide not_le
hoelzl@42850:                  power_less_zero_eq zero_le_power_iff)
hoelzl@42850: 
hoelzl@44791: lemma ereal_le_mult_one_interval:
hoelzl@44791:   fixes x y :: ereal
hoelzl@42850:   assumes y: "y \<noteq> -\<infinity>"
hoelzl@42850:   assumes z: "\<And>z. \<lbrakk> 0 < z ; z < 1 \<rbrakk> \<Longrightarrow> z * x \<le> y"
hoelzl@42850:   shows "x \<le> y"
hoelzl@42850: proof (cases x)
hoelzl@44791:   case PInf with z[of "1 / 2"] show "x \<le> y" by (simp add: one_ereal_def)
hoelzl@42850: next
hoelzl@42850:   case (real r) note r = this
hoelzl@42850:   show "x \<le> y"
hoelzl@42850:   proof (cases y)
hoelzl@42850:     case (real p) note p = this
hoelzl@42850:     have "r \<le> p"
hoelzl@42850:     proof (rule field_le_mult_one_interval)
hoelzl@42850:       fix z :: real assume "0 < z" and "z < 1"
hoelzl@44791:       with z[of "ereal z"]
hoelzl@44791:       show "z * r \<le> p" using p r by (auto simp: zero_le_mult_iff one_ereal_def)
hoelzl@42850:     qed
hoelzl@42850:     then show "x \<le> y" using p r by simp
hoelzl@42850:   qed (insert y, simp_all)
hoelzl@42850: qed simp
hoelzl@42849: 
hoelzl@42844: subsection "Complete lattice"
hoelzl@42844: 
hoelzl@44791: instantiation ereal :: lattice
hoelzl@42844: begin
hoelzl@44791: definition [simp]: "sup x y = (max x y :: ereal)"
hoelzl@44791: definition [simp]: "inf x y = (min x y :: ereal)"
hoelzl@42844: instance proof qed simp_all
hoelzl@42844: end
hoelzl@42844: 
hoelzl@44791: instantiation ereal :: complete_lattice
hoelzl@42844: begin
hoelzl@42844: 
hoelzl@44794: definition "bot = (-\<infinity>::ereal)"
hoelzl@44794: definition "top = (\<infinity>::ereal)"
hoelzl@42844: 
hoelzl@44794: definition "Sup S = (LEAST z. \<forall>x\<in>S. x \<le> z :: ereal)"
hoelzl@44794: definition "Inf S = (GREATEST z. \<forall>x\<in>S. z \<le> x :: ereal)"
hoelzl@42844: 
hoelzl@44791: lemma ereal_complete_Sup:
hoelzl@44791:   fixes S :: "ereal set" assumes "S \<noteq> {}"
hoelzl@42844:   shows "\<exists>x. (\<forall>y\<in>S. y \<le> x) \<and> (\<forall>z. (\<forall>y\<in>S. y \<le> z) \<longrightarrow> x \<le> z)"
hoelzl@42844: proof cases
hoelzl@44791:   assume "\<exists>x. \<forall>a\<in>S. a \<le> ereal x"
hoelzl@44791:   then obtain y where y: "\<And>a. a\<in>S \<Longrightarrow> a \<le> ereal y" by auto
hoelzl@42844:   then have "\<infinity> \<notin> S" by force
hoelzl@42844:   show ?thesis
hoelzl@42844:   proof cases
hoelzl@42844:     assume "S = {-\<infinity>}"
hoelzl@42844:     then show ?thesis by (auto intro!: exI[of _ "-\<infinity>"])
hoelzl@42844:   next
hoelzl@42844:     assume "S \<noteq> {-\<infinity>}"
hoelzl@42844:     with `S \<noteq> {}` `\<infinity> \<notin> S` obtain x where "x \<in> S - {-\<infinity>}" "x \<noteq> \<infinity>" by auto
hoelzl@42844:     with y `\<infinity> \<notin> S` have "\<forall>z\<in>real ` (S - {-\<infinity>}). z \<le> y"
hoelzl@44791:       by (auto simp: real_of_ereal_ord_simps)
hoelzl@42844:     with reals_complete2[of "real ` (S - {-\<infinity>})"] `x \<in> S - {-\<infinity>}`
hoelzl@42844:     obtain s where s:
hoelzl@42844:        "\<forall>y\<in>S - {-\<infinity>}. real y \<le> s" "\<And>z. (\<forall>y\<in>S - {-\<infinity>}. real y \<le> z) \<Longrightarrow> s \<le> z"
hoelzl@42844:        by auto
hoelzl@42844:     show ?thesis
hoelzl@44791:     proof (safe intro!: exI[of _ "ereal s"])
hoelzl@44791:       fix z assume "z \<in> S" with `\<infinity> \<notin> S` show "z \<le> ereal s"
hoelzl@42844:       proof (cases z)
hoelzl@42844:         case (real r)
hoelzl@42844:         then show ?thesis
hoelzl@44791:           using s(1)[rule_format, of z] `z \<in> S` `z = ereal r` by auto
hoelzl@42844:       qed auto
hoelzl@42844:     next
hoelzl@42844:       fix z assume *: "\<forall>y\<in>S. y \<le> z"
hoelzl@44791:       with `S \<noteq> {-\<infinity>}` `S \<noteq> {}` show "ereal s \<le> z"
hoelzl@42844:       proof (cases z)
hoelzl@42844:         case (real u)
hoelzl@42844:         with * have "s \<le> u"
hoelzl@44791:           by (intro s(2)[of u]) (auto simp: real_of_ereal_ord_simps)
hoelzl@42844:         then show ?thesis using real by simp
hoelzl@42844:       qed auto
hoelzl@42844:     qed
hoelzl@42844:   qed
hoelzl@42844: next
hoelzl@44791:   assume *: "\<not> (\<exists>x. \<forall>a\<in>S. a \<le> ereal x)"
hoelzl@42844:   show ?thesis
hoelzl@42844:   proof (safe intro!: exI[of _ \<infinity>])
hoelzl@42844:     fix y assume **: "\<forall>z\<in>S. z \<le> y"
hoelzl@42844:     with * show "\<infinity> \<le> y"
hoelzl@42844:     proof (cases y)
hoelzl@42844:       case MInf with * ** show ?thesis by (force simp: not_le)
hoelzl@42844:     qed auto
hoelzl@42844:   qed simp
hoelzl@42844: qed
hoelzl@42844: 
hoelzl@44791: lemma ereal_complete_Inf:
hoelzl@44791:   fixes S :: "ereal set" assumes "S ~= {}"
hoelzl@42844:   shows "EX x. (ALL y:S. x <= y) & (ALL z. (ALL y:S. z <= y) --> z <= x)"
hoelzl@42844: proof-
hoelzl@42844: def S1 == "uminus ` S"
hoelzl@42844: hence "S1 ~= {}" using assms by auto
hoelzl@42844: from this obtain x where x_def: "(ALL y:S1. y <= x) & (ALL z. (ALL y:S1. y <= z) --> x <= z)"
hoelzl@44791:    using ereal_complete_Sup[of S1] by auto
hoelzl@42844: { fix z assume "ALL y:S. z <= y"
hoelzl@42844:   hence "ALL y:S1. y <= -z" unfolding S1_def by auto
hoelzl@42844:   hence "x <= -z" using x_def by auto
hoelzl@42844:   hence "z <= -x"
hoelzl@44791:     apply (subst ereal_uminus_uminus[symmetric])
hoelzl@44791:     unfolding ereal_minus_le_minus . }
hoelzl@42844: moreover have "(ALL y:S. -x <= y)"
hoelzl@42844:    using x_def unfolding S1_def
hoelzl@42844:    apply simp
hoelzl@44791:    apply (subst (3) ereal_uminus_uminus[symmetric])
hoelzl@44791:    unfolding ereal_minus_le_minus by simp
hoelzl@42844: ultimately show ?thesis by auto
hoelzl@42844: qed
hoelzl@42844: 
hoelzl@44791: lemma ereal_complete_uminus_eq:
hoelzl@44791:   fixes S :: "ereal set"
hoelzl@42844:   shows "(\<forall>y\<in>uminus`S. y \<le> x) \<and> (\<forall>z. (\<forall>y\<in>uminus`S. y \<le> z) \<longrightarrow> x \<le> z)
hoelzl@42844:      \<longleftrightarrow> (\<forall>y\<in>S. -x \<le> y) \<and> (\<forall>z. (\<forall>y\<in>S. z \<le> y) \<longrightarrow> z \<le> -x)"
hoelzl@44791:   by simp (metis ereal_minus_le_minus ereal_uminus_uminus)
hoelzl@42844: 
hoelzl@44791: lemma ereal_Sup_uminus_image_eq:
hoelzl@44791:   fixes S :: "ereal set"
hoelzl@42844:   shows "Sup (uminus ` S) = - Inf S"
hoelzl@42844: proof cases
hoelzl@42844:   assume "S = {}"
hoelzl@44791:   moreover have "(THE x. All (op \<le> x)) = (-\<infinity>::ereal)"
hoelzl@44791:     by (rule the_equality) (auto intro!: ereal_bot)
hoelzl@44791:   moreover have "(SOME x. \<forall>y. y \<le> x) = (\<infinity>::ereal)"
hoelzl@44791:     by (rule some_equality) (auto intro!: ereal_top)
hoelzl@44791:   ultimately show ?thesis unfolding Inf_ereal_def Sup_ereal_def
hoelzl@42844:     Least_def Greatest_def GreatestM_def by simp
hoelzl@42844: next
hoelzl@42844:   assume "S \<noteq> {}"
hoelzl@44791:   with ereal_complete_Sup[of "uminus`S"]
hoelzl@42844:   obtain x where x: "(\<forall>y\<in>S. -x \<le> y) \<and> (\<forall>z. (\<forall>y\<in>S. z \<le> y) \<longrightarrow> z \<le> -x)"
hoelzl@44791:     unfolding ereal_complete_uminus_eq by auto
hoelzl@42844:   show "Sup (uminus ` S) = - Inf S"
hoelzl@44791:     unfolding Inf_ereal_def Greatest_def GreatestM_def
hoelzl@42844:   proof (intro someI2[of _ _ "\<lambda>x. Sup (uminus`S) = - x"])
hoelzl@42844:     show "(\<forall>y\<in>S. -x \<le> y) \<and> (\<forall>y. (\<forall>z\<in>S. y \<le> z) \<longrightarrow> y \<le> -x)"
hoelzl@42844:       using x .
hoelzl@42844:     fix x' assume "(\<forall>y\<in>S. x' \<le> y) \<and> (\<forall>y. (\<forall>z\<in>S. y \<le> z) \<longrightarrow> y \<le> x')"
hoelzl@42844:     then have "(\<forall>y\<in>uminus`S. y \<le> - x') \<and> (\<forall>y. (\<forall>z\<in>uminus`S. z \<le> y) \<longrightarrow> - x' \<le> y)"
hoelzl@44791:       unfolding ereal_complete_uminus_eq by simp
hoelzl@42844:     then show "Sup (uminus ` S) = -x'"
hoelzl@44791:       unfolding Sup_ereal_def ereal_uminus_eq_iff
hoelzl@42844:       by (intro Least_equality) auto
hoelzl@42844:   qed
hoelzl@42844: qed
hoelzl@42844: 
hoelzl@42844: instance
hoelzl@42844: proof
hoelzl@44791:   { fix x :: ereal and A
hoelzl@44791:     show "bot <= x" by (cases x) (simp_all add: bot_ereal_def)
hoelzl@44791:     show "x <= top" by (simp add: top_ereal_def) }
hoelzl@42844: 
hoelzl@44791:   { fix x :: ereal and A assume "x : A"
hoelzl@44791:     with ereal_complete_Sup[of A]
hoelzl@42844:     obtain s where s: "\<forall>y\<in>A. y <= s" "\<forall>z. (\<forall>y\<in>A. y <= z) \<longrightarrow> s <= z" by auto
hoelzl@42844:     hence "x <= s" using `x : A` by auto
hoelzl@44791:     also have "... = Sup A" using s unfolding Sup_ereal_def
hoelzl@42844:       by (auto intro!: Least_equality[symmetric])
hoelzl@42844:     finally show "x <= Sup A" . }
hoelzl@42844:   note le_Sup = this
hoelzl@42844: 
hoelzl@44791:   { fix x :: ereal and A assume *: "!!z. (z : A ==> z <= x)"
hoelzl@42844:     show "Sup A <= x"
hoelzl@42844:     proof (cases "A = {}")
hoelzl@42844:       case True
hoelzl@44791:       hence "Sup A = -\<infinity>" unfolding Sup_ereal_def
hoelzl@42844:         by (auto intro!: Least_equality)
hoelzl@42844:       thus "Sup A <= x" by simp
hoelzl@42844:     next
hoelzl@42844:       case False
hoelzl@44791:       with ereal_complete_Sup[of A]
hoelzl@42844:       obtain s where s: "\<forall>y\<in>A. y <= s" "\<forall>z. (\<forall>y\<in>A. y <= z) \<longrightarrow> s <= z" by auto
hoelzl@42844:       hence "Sup A = s"
hoelzl@44791:         unfolding Sup_ereal_def by (auto intro!: Least_equality)
hoelzl@42844:       also have "s <= x" using * s by auto
hoelzl@42844:       finally show "Sup A <= x" .
hoelzl@42844:     qed }
hoelzl@42844:   note Sup_le = this
hoelzl@42844: 
hoelzl@44791:   { fix x :: ereal and A assume "x \<in> A"
hoelzl@42844:     with le_Sup[of "-x" "uminus`A"] show "Inf A \<le> x"
hoelzl@44791:       unfolding ereal_Sup_uminus_image_eq by simp }
hoelzl@42844: 
hoelzl@44791:   { fix x :: ereal and A assume *: "!!z. (z : A ==> x <= z)"
hoelzl@42844:     with Sup_le[of "uminus`A" "-x"] show "x \<le> Inf A"
hoelzl@44791:       unfolding ereal_Sup_uminus_image_eq by force }
hoelzl@42844: qed
haftmann@44812: 
hoelzl@42844: end
hoelzl@42844: 
haftmann@44812: instance ereal :: complete_linorder ..
haftmann@44812: 
hoelzl@44791: lemma ereal_SUPR_uminus:
hoelzl@44791:   fixes f :: "'a => ereal"
hoelzl@42844:   shows "(SUP i : R. -(f i)) = -(INF i : R. f i)"
hoelzl@42844:   unfolding SUPR_def INFI_def
hoelzl@44791:   using ereal_Sup_uminus_image_eq[of "f`R"]
hoelzl@42844:   by (simp add: image_image)
hoelzl@42844: 
hoelzl@44791: lemma ereal_INFI_uminus:
hoelzl@44791:   fixes f :: "'a => ereal"
hoelzl@42844:   shows "(INF i : R. -(f i)) = -(SUP i : R. f i)"
hoelzl@44791:   using ereal_SUPR_uminus[of _ "\<lambda>x. - f x"] by simp
hoelzl@42844: 
hoelzl@44791: lemma ereal_Inf_uminus_image_eq: "Inf (uminus ` S) = - Sup (S::ereal set)"
hoelzl@44791:   using ereal_Sup_uminus_image_eq[of "uminus ` S"] by (simp add: image_image)
hoelzl@42850: 
hoelzl@44791: lemma ereal_inj_on_uminus[intro, simp]: "inj_on uminus (A :: ereal set)"
hoelzl@42844:   by (auto intro!: inj_onI)
hoelzl@42844: 
hoelzl@44791: lemma ereal_image_uminus_shift:
hoelzl@44791:   fixes X Y :: "ereal set" shows "uminus ` X = Y \<longleftrightarrow> X = uminus ` Y"
hoelzl@42844: proof
hoelzl@42844:   assume "uminus ` X = Y"
hoelzl@42844:   then have "uminus ` uminus ` X = uminus ` Y"
hoelzl@42844:     by (simp add: inj_image_eq_iff)
hoelzl@42844:   then show "X = uminus ` Y" by (simp add: image_image)
hoelzl@42844: qed (simp add: image_image)
hoelzl@42844: 
hoelzl@44791: lemma Inf_ereal_iff:
hoelzl@44791:   fixes z :: ereal
hoelzl@42844:   shows "(!!x. x:X ==> z <= x) ==> (EX x:X. x<y) <-> Inf X < y"
hoelzl@42844:   by (metis complete_lattice_class.Inf_greatest complete_lattice_class.Inf_lower less_le_not_le linear
hoelzl@42844:             order_less_le_trans)
hoelzl@42844: 
hoelzl@42844: lemma Sup_eq_MInfty:
hoelzl@44791:   fixes S :: "ereal set" shows "Sup S = -\<infinity> \<longleftrightarrow> S = {} \<or> S = {-\<infinity>}"
hoelzl@42844: proof
hoelzl@42844:   assume a: "Sup S = -\<infinity>"
hoelzl@42844:   with complete_lattice_class.Sup_upper[of _ S]
hoelzl@42844:   show "S={} \<or> S={-\<infinity>}" by auto
hoelzl@42844: next
hoelzl@42844:   assume "S={} \<or> S={-\<infinity>}" then show "Sup S = -\<infinity>"
hoelzl@44791:     unfolding Sup_ereal_def by (auto intro!: Least_equality)
hoelzl@42844: qed
hoelzl@42844: 
hoelzl@42844: lemma Inf_eq_PInfty:
hoelzl@44791:   fixes S :: "ereal set" shows "Inf S = \<infinity> \<longleftrightarrow> S = {} \<or> S = {\<infinity>}"
hoelzl@42844:   using Sup_eq_MInfty[of "uminus`S"]
hoelzl@44791:   unfolding ereal_Sup_uminus_image_eq ereal_image_uminus_shift by simp
hoelzl@42844: 
hoelzl@44794: lemma Inf_eq_MInfty: 
hoelzl@44794:   fixes S :: "ereal set" shows "-\<infinity> \<in> S \<Longrightarrow> Inf S = -\<infinity>"
hoelzl@44791:   unfolding Inf_ereal_def
hoelzl@42844:   by (auto intro!: Greatest_equality)
hoelzl@42844: 
hoelzl@44794: lemma Sup_eq_PInfty:
hoelzl@44794:   fixes S :: "ereal set" shows "\<infinity> \<in> S \<Longrightarrow> Sup S = \<infinity>"
hoelzl@44791:   unfolding Sup_ereal_def
hoelzl@42844:   by (auto intro!: Least_equality)
hoelzl@42844: 
hoelzl@44791: lemma ereal_SUPI:
hoelzl@44791:   fixes x :: ereal
hoelzl@42844:   assumes "!!i. i : A ==> f i <= x"
hoelzl@42844:   assumes "!!y. (!!i. i : A ==> f i <= y) ==> x <= y"
hoelzl@42844:   shows "(SUP i:A. f i) = x"
hoelzl@44791:   unfolding SUPR_def Sup_ereal_def
hoelzl@42844:   using assms by (auto intro!: Least_equality)
hoelzl@42844: 
hoelzl@44791: lemma ereal_INFI:
hoelzl@44791:   fixes x :: ereal
hoelzl@42844:   assumes "!!i. i : A ==> f i >= x"
hoelzl@42844:   assumes "!!y. (!!i. i : A ==> f i >= y) ==> x >= y"
hoelzl@42844:   shows "(INF i:A. f i) = x"
hoelzl@44791:   unfolding INFI_def Inf_ereal_def
hoelzl@42844:   using assms by (auto intro!: Greatest_equality)
hoelzl@42844: 
hoelzl@44791: lemma Sup_ereal_close:
hoelzl@44791:   fixes e :: ereal
hoelzl@42847:   assumes "0 < e" and S: "\<bar>Sup S\<bar> \<noteq> \<infinity>" "S \<noteq> {}"
hoelzl@42844:   shows "\<exists>x\<in>S. Sup S - e < x"
hoelzl@42847:   using assms by (cases e) (auto intro!: less_Sup_iff[THEN iffD1])
hoelzl@42844: 
hoelzl@44791: lemma Inf_ereal_close:
hoelzl@44791:   fixes e :: ereal assumes "\<bar>Inf X\<bar> \<noteq> \<infinity>" "0 < e"
hoelzl@42844:   shows "\<exists>x\<in>X. x < Inf X + e"
hoelzl@42844: proof (rule Inf_less_iff[THEN iffD1])
hoelzl@42844:   show "Inf X < Inf X + e" using assms
hoelzl@42847:     by (cases e) auto
hoelzl@42844: qed
hoelzl@42844: 
hoelzl@44791: lemma SUP_nat_Infty: "(SUP i::nat. ereal (real i)) = \<infinity>"
hoelzl@42844: proof -
hoelzl@44794:   { fix x ::ereal assume "x \<noteq> \<infinity>"
hoelzl@44791:     then have "\<exists>k::nat. x < ereal (real k)"
hoelzl@42844:     proof (cases x)
hoelzl@42844:       case MInf then show ?thesis by (intro exI[of _ 0]) auto
hoelzl@42844:     next
hoelzl@42844:       case (real r)
hoelzl@42844:       moreover obtain k :: nat where "r < real k"
hoelzl@42844:         using ex_less_of_nat by (auto simp: real_eq_of_nat)
hoelzl@42844:       ultimately show ?thesis by auto
hoelzl@42844:     qed simp }
hoelzl@42844:   then show ?thesis
hoelzl@44791:     using SUP_eq_top_iff[of UNIV "\<lambda>n::nat. ereal (real n)"]
hoelzl@44791:     by (auto simp: top_ereal_def)
hoelzl@42844: qed
hoelzl@42844: 
hoelzl@44791: lemma ereal_le_Sup:
hoelzl@44791:   fixes x :: ereal
hoelzl@42844:   shows "(x <= (SUP i:A. f i)) <-> (ALL y. y < x --> (EX i. i : A & y <= f i))"
hoelzl@42844: (is "?lhs <-> ?rhs")
hoelzl@42844: proof-
hoelzl@42844: { assume "?rhs"
hoelzl@42844:   { assume "~(x <= (SUP i:A. f i))" hence "(SUP i:A. f i)<x" by (simp add: not_le)
hoelzl@44791:     from this obtain y where y_def: "(SUP i:A. f i)<y & y<x" using ereal_dense by auto
hoelzl@42844:     from this obtain i where "i : A & y <= f i" using `?rhs` by auto
hoelzl@42844:     hence "y <= (SUP i:A. f i)" using le_SUPI[of i A f] by auto
hoelzl@42844:     hence False using y_def by auto
hoelzl@42844:   } hence "?lhs" by auto
hoelzl@42844: }
hoelzl@42844: moreover
hoelzl@42844: { assume "?lhs" hence "?rhs"
hoelzl@42844:   by (metis Collect_def Collect_mem_eq SUP_leI assms atLeastatMost_empty atLeastatMost_empty_iff
hoelzl@42844:       inf_sup_ord(4) linorder_le_cases sup_absorb1 xt1(8))
hoelzl@42844: } ultimately show ?thesis by auto
hoelzl@42844: qed
hoelzl@42844: 
hoelzl@44791: lemma ereal_Inf_le:
hoelzl@44791:   fixes x :: ereal
hoelzl@42844:   shows "((INF i:A. f i) <= x) <-> (ALL y. x < y --> (EX i. i : A & f i <= y))"
hoelzl@42844: (is "?lhs <-> ?rhs")
hoelzl@42844: proof-
hoelzl@42844: { assume "?rhs"
hoelzl@42844:   { assume "~((INF i:A. f i) <= x)" hence "x < (INF i:A. f i)" by (simp add: not_le)
hoelzl@44791:     from this obtain y where y_def: "x<y & y<(INF i:A. f i)" using ereal_dense by auto
hoelzl@42844:     from this obtain i where "i : A & f i <= y" using `?rhs` by auto
hoelzl@42844:     hence "(INF i:A. f i) <= y" using INF_leI[of i A f] by auto
hoelzl@42844:     hence False using y_def by auto
hoelzl@42844:   } hence "?lhs" by auto
hoelzl@42844: }
hoelzl@42844: moreover
hoelzl@42844: { assume "?lhs" hence "?rhs"
hoelzl@42844:   by (metis Collect_def Collect_mem_eq le_INFI assms atLeastatMost_empty atLeastatMost_empty_iff
hoelzl@42844:       inf_sup_ord(4) linorder_le_cases sup_absorb1 xt1(8))
hoelzl@42844: } ultimately show ?thesis by auto
hoelzl@42844: qed
hoelzl@42844: 
hoelzl@42844: lemma Inf_less:
hoelzl@44791:   fixes x :: ereal
hoelzl@42844:   assumes "(INF i:A. f i) < x"
hoelzl@42844:   shows "EX i. i : A & f i <= x"
hoelzl@42844: proof(rule ccontr)
hoelzl@42844:   assume "~ (EX i. i : A & f i <= x)"
hoelzl@42844:   hence "ALL i:A. f i > x" by auto
hoelzl@42844:   hence "(INF i:A. f i) >= x" apply (subst le_INFI) by auto
hoelzl@42844:   thus False using assms by auto
hoelzl@42844: qed
hoelzl@42844: 
hoelzl@42844: lemma same_INF:
hoelzl@42844:   assumes "ALL e:A. f e = g e"
hoelzl@42844:   shows "(INF e:A. f e) = (INF e:A. g e)"
hoelzl@42844: proof-
hoelzl@42844: have "f ` A = g ` A" unfolding image_def using assms by auto
hoelzl@42844: thus ?thesis unfolding INFI_def by auto
hoelzl@42844: qed
hoelzl@42844: 
hoelzl@42844: lemma same_SUP:
hoelzl@42844:   assumes "ALL e:A. f e = g e"
hoelzl@42844:   shows "(SUP e:A. f e) = (SUP e:A. g e)"
hoelzl@42844: proof-
hoelzl@42844: have "f ` A = g ` A" unfolding image_def using assms by auto
hoelzl@42844: thus ?thesis unfolding SUPR_def by auto
hoelzl@42844: qed
hoelzl@42844: 
hoelzl@42850: lemma SUPR_eq:
hoelzl@42850:   assumes "\<forall>i\<in>A. \<exists>j\<in>B. f i \<le> g j"
hoelzl@42850:   assumes "\<forall>j\<in>B. \<exists>i\<in>A. g j \<le> f i"
hoelzl@42850:   shows "(SUP i:A. f i) = (SUP j:B. g j)"
hoelzl@42850: proof (intro antisym)
hoelzl@42850:   show "(SUP i:A. f i) \<le> (SUP j:B. g j)"
hoelzl@42851:     using assms by (metis SUP_leI le_SUPI2)
hoelzl@42850:   show "(SUP i:B. g i) \<le> (SUP j:A. f j)"
hoelzl@42851:     using assms by (metis SUP_leI le_SUPI2)
hoelzl@42850: qed
hoelzl@42850: 
hoelzl@44791: lemma SUP_ereal_le_addI:
hoelzl@44794:   fixes f :: "'i \<Rightarrow> ereal"
hoelzl@42849:   assumes "\<And>i. f i + y \<le> z" and "y \<noteq> -\<infinity>"
hoelzl@42849:   shows "SUPR UNIV f + y \<le> z"
hoelzl@42849: proof (cases y)
hoelzl@42849:   case (real r)
hoelzl@44791:   then have "\<And>i. f i \<le> z - y" using assms by (simp add: ereal_le_minus_iff)
hoelzl@42849:   then have "SUPR UNIV f \<le> z - y" by (rule SUP_leI)
hoelzl@44791:   then show ?thesis using real by (simp add: ereal_le_minus_iff)
hoelzl@42849: qed (insert assms, auto)
hoelzl@42849: 
hoelzl@44791: lemma SUPR_ereal_add:
hoelzl@44791:   fixes f g :: "nat \<Rightarrow> ereal"
hoelzl@42850:   assumes "incseq f" "incseq g" and pos: "\<And>i. f i \<noteq> -\<infinity>" "\<And>i. g i \<noteq> -\<infinity>"
hoelzl@42849:   shows "(SUP i. f i + g i) = SUPR UNIV f + SUPR UNIV g"
hoelzl@44791: proof (rule ereal_SUPI)
hoelzl@42849:   fix y assume *: "\<And>i. i \<in> UNIV \<Longrightarrow> f i + g i \<le> y"
hoelzl@42849:   have f: "SUPR UNIV f \<noteq> -\<infinity>" using pos
hoelzl@42849:     unfolding SUPR_def Sup_eq_MInfty by (auto dest: image_eqD)
hoelzl@42849:   { fix j
hoelzl@42849:     { fix i
hoelzl@42849:       have "f i + g j \<le> f i + g (max i j)"
hoelzl@42849:         using `incseq g`[THEN incseqD] by (rule add_left_mono) auto
hoelzl@42849:       also have "\<dots> \<le> f (max i j) + g (max i j)"
hoelzl@42849:         using `incseq f`[THEN incseqD] by (rule add_right_mono) auto
hoelzl@42849:       also have "\<dots> \<le> y" using * by auto
hoelzl@42849:       finally have "f i + g j \<le> y" . }
hoelzl@42849:     then have "SUPR UNIV f + g j \<le> y"
hoelzl@44791:       using assms(4)[of j] by (intro SUP_ereal_le_addI) auto
hoelzl@42849:     then have "g j + SUPR UNIV f \<le> y" by (simp add: ac_simps) }
hoelzl@42849:   then have "SUPR UNIV g + SUPR UNIV f \<le> y"
hoelzl@44791:     using f by (rule SUP_ereal_le_addI)
hoelzl@42849:   then show "SUPR UNIV f + SUPR UNIV g \<le> y" by (simp add: ac_simps)
hoelzl@42849: qed (auto intro!: add_mono le_SUPI)
hoelzl@42849: 
hoelzl@44791: lemma SUPR_ereal_add_pos:
hoelzl@44791:   fixes f g :: "nat \<Rightarrow> ereal"
hoelzl@42850:   assumes inc: "incseq f" "incseq g" and pos: "\<And>i. 0 \<le> f i" "\<And>i. 0 \<le> g i"
hoelzl@42850:   shows "(SUP i. f i + g i) = SUPR UNIV f + SUPR UNIV g"
hoelzl@44791: proof (intro SUPR_ereal_add inc)
hoelzl@42850:   fix i show "f i \<noteq> -\<infinity>" "g i \<noteq> -\<infinity>" using pos[of i] by auto
hoelzl@42850: qed
hoelzl@42850: 
hoelzl@44791: lemma SUPR_ereal_setsum:
hoelzl@44791:   fixes f g :: "'a \<Rightarrow> nat \<Rightarrow> ereal"
hoelzl@42850:   assumes "\<And>n. n \<in> A \<Longrightarrow> incseq (f n)" and pos: "\<And>n i. n \<in> A \<Longrightarrow> 0 \<le> f n i"
hoelzl@42850:   shows "(SUP i. \<Sum>n\<in>A. f n i) = (\<Sum>n\<in>A. SUPR UNIV (f n))"
hoelzl@42850: proof cases
hoelzl@42850:   assume "finite A" then show ?thesis using assms
hoelzl@44791:     by induct (auto simp: incseq_setsumI2 setsum_nonneg SUPR_ereal_add_pos)
hoelzl@42850: qed simp
hoelzl@42850: 
hoelzl@44791: lemma SUPR_ereal_cmult:
hoelzl@44791:   fixes f :: "nat \<Rightarrow> ereal" assumes "\<And>i. 0 \<le> f i" "0 \<le> c"
hoelzl@42849:   shows "(SUP i. c * f i) = c * SUPR UNIV f"
hoelzl@44791: proof (rule ereal_SUPI)
hoelzl@42849:   fix i have "f i \<le> SUPR UNIV f" by (rule le_SUPI) auto
hoelzl@42849:   then show "c * f i \<le> c * SUPR UNIV f"
hoelzl@44791:     using `0 \<le> c` by (rule ereal_mult_left_mono)
hoelzl@42849: next
hoelzl@42849:   fix y assume *: "\<And>i. i \<in> UNIV \<Longrightarrow> c * f i \<le> y"
hoelzl@42849:   show "c * SUPR UNIV f \<le> y"
hoelzl@42849:   proof cases
hoelzl@42849:     assume c: "0 < c \<and> c \<noteq> \<infinity>"
hoelzl@42849:     with * have "SUPR UNIV f \<le> y / c"
hoelzl@44791:       by (intro SUP_leI) (auto simp: ereal_le_divide_pos)
hoelzl@42849:     with c show ?thesis
hoelzl@44791:       by (auto simp: ereal_le_divide_pos)
hoelzl@42849:   next
hoelzl@42849:     { assume "c = \<infinity>" have ?thesis
hoelzl@42849:       proof cases
hoelzl@42849:         assume "\<forall>i. f i = 0"
hoelzl@42849:         moreover then have "range f = {0}" by auto
hoelzl@42849:         ultimately show "c * SUPR UNIV f \<le> y" using * by (auto simp: SUPR_def)
hoelzl@42849:       next
hoelzl@42849:         assume "\<not> (\<forall>i. f i = 0)"
hoelzl@42849:         then obtain i where "f i \<noteq> 0" by auto
hoelzl@42849:         with *[of i] `c = \<infinity>` `0 \<le> f i` show ?thesis by (auto split: split_if_asm)
hoelzl@42849:       qed }
hoelzl@42849:     moreover assume "\<not> (0 < c \<and> c \<noteq> \<infinity>)"
hoelzl@42849:     ultimately show ?thesis using * `0 \<le> c` by auto
hoelzl@42849:   qed
hoelzl@42849: qed
hoelzl@42849: 
hoelzl@42850: lemma SUP_PInfty:
hoelzl@44791:   fixes f :: "'a \<Rightarrow> ereal"
hoelzl@44791:   assumes "\<And>n::nat. \<exists>i\<in>A. ereal (real n) \<le> f i"
hoelzl@42850:   shows "(SUP i:A. f i) = \<infinity>"
hoelzl@44791:   unfolding SUPR_def Sup_eq_top_iff[where 'a=ereal, unfolded top_ereal_def]
hoelzl@42850:   apply simp
hoelzl@42850: proof safe
hoelzl@44794:   fix x :: ereal assume "x \<noteq> \<infinity>"
hoelzl@42850:   show "\<exists>i\<in>A. x < f i"
hoelzl@42850:   proof (cases x)
hoelzl@42850:     case PInf with `x \<noteq> \<infinity>` show ?thesis by simp
hoelzl@42850:   next
hoelzl@42850:     case MInf with assms[of "0"] show ?thesis by force
hoelzl@42850:   next
hoelzl@42850:     case (real r)
hoelzl@44791:     with less_PInf_Ex_of_nat[of x] obtain n :: nat where "x < ereal (real n)" by auto
hoelzl@42850:     moreover from assms[of n] guess i ..
hoelzl@42850:     ultimately show ?thesis
hoelzl@42850:       by (auto intro!: bexI[of _ i])
hoelzl@42850:   qed
hoelzl@42850: qed
hoelzl@42850: 
hoelzl@42850: lemma Sup_countable_SUPR:
hoelzl@42850:   assumes "A \<noteq> {}"
hoelzl@44791:   shows "\<exists>f::nat \<Rightarrow> ereal. range f \<subseteq> A \<and> Sup A = SUPR UNIV f"
hoelzl@42850: proof (cases "Sup A")
hoelzl@42850:   case (real r)
hoelzl@44791:   have "\<forall>n::nat. \<exists>x. x \<in> A \<and> Sup A < x + 1 / ereal (real n)"
hoelzl@42850:   proof
hoelzl@44791:     fix n ::nat have "\<exists>x\<in>A. Sup A - 1 / ereal (real n) < x"
hoelzl@44791:       using assms real by (intro Sup_ereal_close) (auto simp: one_ereal_def)
hoelzl@42850:     then guess x ..
hoelzl@44791:     then show "\<exists>x. x \<in> A \<and> Sup A < x + 1 / ereal (real n)"
hoelzl@44791:       by (auto intro!: exI[of _ x] simp: ereal_minus_less_iff)
hoelzl@42850:   qed
hoelzl@42850:   from choice[OF this] guess f .. note f = this
hoelzl@42850:   have "SUPR UNIV f = Sup A"
hoelzl@44791:   proof (rule ereal_SUPI)
hoelzl@42850:     fix i show "f i \<le> Sup A" using f
hoelzl@42850:       by (auto intro!: complete_lattice_class.Sup_upper)
hoelzl@42850:   next
hoelzl@42850:     fix y assume bound: "\<And>i. i \<in> UNIV \<Longrightarrow> f i \<le> y"
hoelzl@42850:     show "Sup A \<le> y"
hoelzl@44791:     proof (rule ereal_le_epsilon, intro allI impI)
hoelzl@44791:       fix e :: ereal assume "0 < e"
hoelzl@42850:       show "Sup A \<le> y + e"
hoelzl@42850:       proof (cases e)
hoelzl@42850:         case (real r)
hoelzl@42850:         hence "0 < r" using `0 < e` by auto
hoelzl@42850:         then obtain n ::nat where *: "1 / real n < r" "0 < n"
hoelzl@42850:           using ex_inverse_of_nat_less by (auto simp: real_eq_of_nat inverse_eq_divide)
hoelzl@44791:         have "Sup A \<le> f n + 1 / ereal (real n)" using f[THEN spec, of n] by auto
hoelzl@44791:         also have "1 / ereal (real n) \<le> e" using real * by (auto simp: one_ereal_def )
hoelzl@44791:         with bound have "f n + 1 / ereal (real n) \<le> y + e" by (rule add_mono) simp
hoelzl@42850:         finally show "Sup A \<le> y + e" .
hoelzl@42850:       qed (insert `0 < e`, auto)
hoelzl@42850:     qed
hoelzl@42850:   qed
hoelzl@42850:   with f show ?thesis by (auto intro!: exI[of _ f])
hoelzl@42850: next
hoelzl@42850:   case PInf
hoelzl@42850:   from `A \<noteq> {}` obtain x where "x \<in> A" by auto
hoelzl@42850:   show ?thesis
hoelzl@42850:   proof cases
hoelzl@42850:     assume "\<infinity> \<in> A"
hoelzl@42850:     moreover then have "\<infinity> \<le> Sup A" by (intro complete_lattice_class.Sup_upper)
hoelzl@42850:     ultimately show ?thesis by (auto intro!: exI[of _ "\<lambda>x. \<infinity>"])
hoelzl@42850:   next
hoelzl@42850:     assume "\<infinity> \<notin> A"
hoelzl@42850:     have "\<exists>x\<in>A. 0 \<le> x"
hoelzl@44791:       by (metis Infty_neq_0 PInf complete_lattice_class.Sup_least ereal_infty_less_eq2 linorder_linear)
hoelzl@42850:     then obtain x where "x \<in> A" "0 \<le> x" by auto
hoelzl@44791:     have "\<forall>n::nat. \<exists>f. f \<in> A \<and> x + ereal (real n) \<le> f"
hoelzl@42850:     proof (rule ccontr)
hoelzl@42850:       assume "\<not> ?thesis"
hoelzl@44791:       then have "\<exists>n::nat. Sup A \<le> x + ereal (real n)"
hoelzl@42850:         by (simp add: Sup_le_iff not_le less_imp_le Ball_def) (metis less_imp_le)
hoelzl@42850:       then show False using `x \<in> A` `\<infinity> \<notin> A` PInf
hoelzl@42850:         by(cases x) auto
hoelzl@42850:     qed
hoelzl@42850:     from choice[OF this] guess f .. note f = this
hoelzl@42850:     have "SUPR UNIV f = \<infinity>"
hoelzl@42850:     proof (rule SUP_PInfty)
hoelzl@44791:       fix n :: nat show "\<exists>i\<in>UNIV. ereal (real n) \<le> f i"
hoelzl@42850:         using f[THEN spec, of n] `0 \<le> x`
hoelzl@44791:         by (cases rule: ereal2_cases[of "f n" x]) (auto intro!: exI[of _ n])
hoelzl@42850:     qed
hoelzl@42850:     then show ?thesis using f PInf by (auto intro!: exI[of _ f])
hoelzl@42850:   qed
hoelzl@42850: next
hoelzl@42850:   case MInf
hoelzl@42850:   with `A \<noteq> {}` have "A = {-\<infinity>}" by (auto simp: Sup_eq_MInfty)
hoelzl@42850:   then show ?thesis using MInf by (auto intro!: exI[of _ "\<lambda>x. -\<infinity>"])
hoelzl@42850: qed
hoelzl@42850: 
hoelzl@42850: lemma SUPR_countable_SUPR:
hoelzl@44791:   "A \<noteq> {} \<Longrightarrow> \<exists>f::nat \<Rightarrow> ereal. range f \<subseteq> g`A \<and> SUPR A g = SUPR UNIV f"
hoelzl@42850:   using Sup_countable_SUPR[of "g`A"] by (auto simp: SUPR_def)
hoelzl@42850: 
hoelzl@44791: lemma Sup_ereal_cadd:
hoelzl@44791:   fixes A :: "ereal set" assumes "A \<noteq> {}" and "a \<noteq> -\<infinity>"
hoelzl@42850:   shows "Sup ((\<lambda>x. a + x) ` A) = a + Sup A"
hoelzl@42850: proof (rule antisym)
hoelzl@44791:   have *: "\<And>a::ereal. \<And>A. Sup ((\<lambda>x. a + x) ` A) \<le> a + Sup A"
hoelzl@42850:     by (auto intro!: add_mono complete_lattice_class.Sup_least complete_lattice_class.Sup_upper)
hoelzl@42850:   then show "Sup ((\<lambda>x. a + x) ` A) \<le> a + Sup A" .
hoelzl@42850:   show "a + Sup A \<le> Sup ((\<lambda>x. a + x) ` A)"
hoelzl@42850:   proof (cases a)
hoelzl@42850:     case PInf with `A \<noteq> {}` show ?thesis by (auto simp: image_constant)
hoelzl@42850:   next
hoelzl@42850:     case (real r)
hoelzl@42850:     then have **: "op + (- a) ` op + a ` A = A"
hoelzl@44791:       by (auto simp: image_iff ac_simps zero_ereal_def[symmetric])
hoelzl@42850:     from *[of "-a" "(\<lambda>x. a + x) ` A"] real show ?thesis unfolding **
hoelzl@44791:       by (cases rule: ereal2_cases[of "Sup A" "Sup (op + a ` A)"]) auto
hoelzl@42850:   qed (insert `a \<noteq> -\<infinity>`, auto)
hoelzl@42850: qed
hoelzl@42850: 
hoelzl@44791: lemma Sup_ereal_cminus:
hoelzl@44791:   fixes A :: "ereal set" assumes "A \<noteq> {}" and "a \<noteq> -\<infinity>"
hoelzl@42850:   shows "Sup ((\<lambda>x. a - x) ` A) = a - Inf A"
hoelzl@44791:   using Sup_ereal_cadd[of "uminus ` A" a] assms
hoelzl@44791:   by (simp add: comp_def image_image minus_ereal_def
hoelzl@44791:                  ereal_Sup_uminus_image_eq)
hoelzl@42850: 
hoelzl@44791: lemma SUPR_ereal_cminus:
hoelzl@44794:   fixes f :: "'i \<Rightarrow> ereal"
hoelzl@42850:   fixes A assumes "A \<noteq> {}" and "a \<noteq> -\<infinity>"
hoelzl@42850:   shows "(SUP x:A. a - f x) = a - (INF x:A. f x)"
hoelzl@44791:   using Sup_ereal_cminus[of "f`A" a] assms
hoelzl@42850:   unfolding SUPR_def INFI_def image_image by auto
hoelzl@42850: 
hoelzl@44791: lemma Inf_ereal_cminus:
hoelzl@44791:   fixes A :: "ereal set" assumes "A \<noteq> {}" and "\<bar>a\<bar> \<noteq> \<infinity>"
hoelzl@42850:   shows "Inf ((\<lambda>x. a - x) ` A) = a - Sup A"
hoelzl@42850: proof -
hoelzl@42850:   { fix x have "-a - -x = -(a - x)" using assms by (cases x) auto }
hoelzl@42850:   moreover then have "(\<lambda>x. -a - x)`uminus`A = uminus ` (\<lambda>x. a - x) ` A"
hoelzl@42850:     by (auto simp: image_image)
hoelzl@42850:   ultimately show ?thesis
hoelzl@44791:     using Sup_ereal_cminus[of "uminus ` A" "-a"] assms
hoelzl@44791:     by (auto simp add: ereal_Sup_uminus_image_eq ereal_Inf_uminus_image_eq)
hoelzl@42850: qed
hoelzl@42850: 
hoelzl@44791: lemma INFI_ereal_cminus:
hoelzl@44794:   fixes a :: ereal assumes "A \<noteq> {}" and "\<bar>a\<bar> \<noteq> \<infinity>"
hoelzl@42850:   shows "(INF x:A. a - f x) = a - (SUP x:A. f x)"
hoelzl@44791:   using Inf_ereal_cminus[of "f`A" a] assms
hoelzl@42850:   unfolding SUPR_def INFI_def image_image
hoelzl@42850:   by auto
hoelzl@42850: 
hoelzl@44791: lemma uminus_ereal_add_uminus_uminus:
hoelzl@44791:   fixes a b :: ereal shows "a \<noteq> \<infinity> \<Longrightarrow> b \<noteq> \<infinity> \<Longrightarrow> - (- a + - b) = a + b"
hoelzl@44791:   by (cases rule: ereal2_cases[of a b]) auto
hoelzl@43791: 
hoelzl@44791: lemma INFI_ereal_add:
hoelzl@44794:   fixes f :: "nat \<Rightarrow> ereal"
hoelzl@43791:   assumes "decseq f" "decseq g" and fin: "\<And>i. f i \<noteq> \<infinity>" "\<And>i. g i \<noteq> \<infinity>"
hoelzl@43791:   shows "(INF i. f i + g i) = INFI UNIV f + INFI UNIV g"
hoelzl@43791: proof -
hoelzl@43791:   have INF_less: "(INF i. f i) < \<infinity>" "(INF i. g i) < \<infinity>"
hoelzl@43791:     using assms unfolding INF_less_iff by auto
hoelzl@43791:   { fix i from fin[of i] have "- ((- f i) + (- g i)) = f i + g i"
hoelzl@44791:       by (rule uminus_ereal_add_uminus_uminus) }
hoelzl@43791:   then have "(INF i. f i + g i) = (INF i. - ((- f i) + (- g i)))"
hoelzl@43791:     by simp
hoelzl@43791:   also have "\<dots> = INFI UNIV f + INFI UNIV g"
hoelzl@44791:     unfolding ereal_INFI_uminus
hoelzl@43791:     using assms INF_less
hoelzl@44791:     by (subst SUPR_ereal_add)
hoelzl@44791:        (auto simp: ereal_SUPR_uminus intro!: uminus_ereal_add_uminus_uminus)
hoelzl@43791:   finally show ?thesis .
hoelzl@43791: qed
hoelzl@43791: 
hoelzl@44791: subsection "Limits on @{typ ereal}"
hoelzl@42844: 
hoelzl@42844: subsubsection "Topological space"
hoelzl@42844: 
hoelzl@44791: instantiation ereal :: topological_space
hoelzl@42844: begin
hoelzl@42844: 
hoelzl@44791: definition "open A \<longleftrightarrow> open (ereal -` A)
hoelzl@44791:        \<and> (\<infinity> \<in> A \<longrightarrow> (\<exists>x. {ereal x <..} \<subseteq> A))
hoelzl@44791:        \<and> (-\<infinity> \<in> A \<longrightarrow> (\<exists>x. {..<ereal x} \<subseteq> A))"
hoelzl@42844: 
hoelzl@44791: lemma open_PInfty: "open A \<Longrightarrow> \<infinity> \<in> A \<Longrightarrow> (\<exists>x. {ereal x<..} \<subseteq> A)"
hoelzl@44791:   unfolding open_ereal_def by auto
hoelzl@42844: 
hoelzl@44791: lemma open_MInfty: "open A \<Longrightarrow> -\<infinity> \<in> A \<Longrightarrow> (\<exists>x. {..<ereal x} \<subseteq> A)"
hoelzl@44791:   unfolding open_ereal_def by auto
hoelzl@42844: 
hoelzl@44791: lemma open_PInfty2: assumes "open A" "\<infinity> \<in> A" obtains x where "{ereal x<..} \<subseteq> A"
hoelzl@42844:   using open_PInfty[OF assms] by auto
hoelzl@42844: 
hoelzl@44791: lemma open_MInfty2: assumes "open A" "-\<infinity> \<in> A" obtains x where "{..<ereal x} \<subseteq> A"
hoelzl@42844:   using open_MInfty[OF assms] by auto
hoelzl@42844: 
hoelzl@44791: lemma ereal_openE: assumes "open A" obtains x y where
hoelzl@44791:   "open (ereal -` A)"
hoelzl@44791:   "\<infinity> \<in> A \<Longrightarrow> {ereal x<..} \<subseteq> A"
hoelzl@44791:   "-\<infinity> \<in> A \<Longrightarrow> {..<ereal y} \<subseteq> A"
hoelzl@44791:   using assms open_ereal_def by auto
hoelzl@42844: 
hoelzl@42844: instance
hoelzl@42844: proof
hoelzl@44791:   let ?U = "UNIV::ereal set"
hoelzl@44791:   show "open ?U" unfolding open_ereal_def
hoelzl@42846:     by (auto intro!: exI[of _ 0])
hoelzl@42844: next
hoelzl@44791:   fix S T::"ereal set" assume "open S" and "open T"
hoelzl@44791:   from `open S`[THEN ereal_openE] guess xS yS .
hoelzl@44791:   moreover from `open T`[THEN ereal_openE] guess xT yT .
hoelzl@42846:   ultimately have
hoelzl@44791:     "open (ereal -` (S \<inter> T))"
hoelzl@44791:     "\<infinity> \<in> S \<inter> T \<Longrightarrow> {ereal (max xS xT) <..} \<subseteq> S \<inter> T"
hoelzl@44791:     "-\<infinity> \<in> S \<inter> T \<Longrightarrow> {..< ereal (min yS yT)} \<subseteq> S \<inter> T"
hoelzl@42846:     by auto
hoelzl@44791:   then show "open (S Int T)" unfolding open_ereal_def by blast
hoelzl@42844: next
hoelzl@44791:   fix K :: "ereal set set" assume "\<forall>S\<in>K. open S"
hoelzl@44791:   then have *: "\<forall>S. \<exists>x y. S \<in> K \<longrightarrow> open (ereal -` S) \<and>
hoelzl@44791:     (\<infinity> \<in> S \<longrightarrow> {ereal x <..} \<subseteq> S) \<and> (-\<infinity> \<in> S \<longrightarrow> {..< ereal y} \<subseteq> S)"
hoelzl@44791:     by (auto simp: open_ereal_def)
hoelzl@44791:   then show "open (Union K)" unfolding open_ereal_def
hoelzl@42846:   proof (intro conjI impI)
hoelzl@44791:     show "open (ereal -` \<Union>K)"
hoelzl@42851:       using *[THEN choice] by (auto simp: vimage_Union)
hoelzl@42846:   qed ((metis UnionE Union_upper subset_trans *)+)
hoelzl@42844: qed
hoelzl@42844: end
hoelzl@42844: 
hoelzl@44791: lemma open_ereal: "open S \<Longrightarrow> open (ereal ` S)"
hoelzl@44791:   by (auto simp: inj_vimage_image_eq open_ereal_def)
hoelzl@42847: 
hoelzl@44791: lemma open_ereal_vimage: "open S \<Longrightarrow> open (ereal -` S)"
hoelzl@44791:   unfolding open_ereal_def by auto
hoelzl@42847: 
hoelzl@44791: lemma open_ereal_lessThan[intro, simp]: "open {..< a :: ereal}"
hoelzl@42846: proof -
hoelzl@44791:   have "\<And>x. ereal -` {..<ereal x} = {..< x}"
hoelzl@44791:     "ereal -` {..< \<infinity>} = UNIV" "ereal -` {..< -\<infinity>} = {}" by auto
hoelzl@44791:   then show ?thesis by (cases a) (auto simp: open_ereal_def)
hoelzl@42846: qed
hoelzl@42846: 
hoelzl@44791: lemma open_ereal_greaterThan[intro, simp]:
hoelzl@44791:   "open {a :: ereal <..}"
hoelzl@42846: proof -
hoelzl@44791:   have "\<And>x. ereal -` {ereal x<..} = {x<..}"
hoelzl@44791:     "ereal -` {\<infinity><..} = {}" "ereal -` {-\<infinity><..} = UNIV" by auto
hoelzl@44791:   then show ?thesis by (cases a) (auto simp: open_ereal_def)
hoelzl@42846: qed
hoelzl@42846: 
hoelzl@44791: lemma ereal_open_greaterThanLessThan[intro, simp]: "open {a::ereal <..< b}"
hoelzl@42844:   unfolding greaterThanLessThan_def by auto
hoelzl@42844: 
hoelzl@44791: lemma closed_ereal_atLeast[simp, intro]: "closed {a :: ereal ..}"
hoelzl@42844: proof -
hoelzl@42844:   have "- {a ..} = {..< a}" by auto
hoelzl@42844:   then show "closed {a ..}"
hoelzl@44791:     unfolding closed_def using open_ereal_lessThan by auto
hoelzl@42844: qed
hoelzl@42844: 
hoelzl@44791: lemma closed_ereal_atMost[simp, intro]: "closed {.. b :: ereal}"
hoelzl@42844: proof -
hoelzl@42844:   have "- {.. b} = {b <..}" by auto
hoelzl@42844:   then show "closed {.. b}"
hoelzl@44791:     unfolding closed_def using open_ereal_greaterThan by auto
hoelzl@42844: qed
hoelzl@42844: 
hoelzl@44791: lemma closed_ereal_atLeastAtMost[simp, intro]:
hoelzl@44791:   shows "closed {a :: ereal .. b}"
hoelzl@42844:   unfolding atLeastAtMost_def by auto
hoelzl@42844: 
hoelzl@44791: lemma closed_ereal_singleton:
hoelzl@44791:   "closed {a :: ereal}"
hoelzl@44791: by (metis atLeastAtMost_singleton closed_ereal_atLeastAtMost)
hoelzl@42844: 
hoelzl@44791: lemma ereal_open_cont_interval:
hoelzl@44794:   fixes S :: "ereal set"
hoelzl@42847:   assumes "open S" "x \<in> S" "\<bar>x\<bar> \<noteq> \<infinity>"
hoelzl@42844:   obtains e where "e>0" "{x-e <..< x+e} \<subseteq> S"
hoelzl@42844: proof-
hoelzl@44791:   from `open S` have "open (ereal -` S)" by (rule ereal_openE)
hoelzl@44791:   then obtain e where "0 < e" and e: "\<And>y. dist y (real x) < e \<Longrightarrow> ereal y \<in> S"
hoelzl@42851:     using assms unfolding open_dist by force
hoelzl@42846:   show thesis
hoelzl@42846:   proof (intro that subsetI)
hoelzl@44791:     show "0 < ereal e" using `0 < e` by auto
hoelzl@44791:     fix y assume "y \<in> {x - ereal e<..<x + ereal e}"
hoelzl@44791:     with assms obtain t where "y = ereal t" "dist t (real x) < e"
hoelzl@42851:       apply (cases y) by (auto simp: dist_real_def)
hoelzl@42851:     then show "y \<in> S" using e[of t] by auto
hoelzl@42846:   qed
hoelzl@42844: qed
hoelzl@42844: 
hoelzl@44791: lemma ereal_open_cont_interval2:
hoelzl@44794:   fixes S :: "ereal set"
hoelzl@42847:   assumes "open S" "x \<in> S" and x: "\<bar>x\<bar> \<noteq> \<infinity>"
hoelzl@42844:   obtains a b where "a < x" "x < b" "{a <..< b} \<subseteq> S"
hoelzl@42844: proof-
hoelzl@44791:   guess e using ereal_open_cont_interval[OF assms] .
hoelzl@44791:   with that[of "x-e" "x+e"] ereal_between[OF x, of e]
hoelzl@42844:   show thesis by auto
hoelzl@42844: qed
hoelzl@42844: 
hoelzl@44791: instance ereal :: t2_space
hoelzl@42844: proof
hoelzl@44791:   fix x y :: ereal assume "x ~= y"
hoelzl@44791:   let "?P x (y::ereal)" = "EX U V. open U & open V & x : U & y : V & U Int V = {}"
hoelzl@42844: 
hoelzl@44791:   { fix x y :: ereal assume "x < y"
hoelzl@44791:     from ereal_dense[OF this] obtain z where z: "x < z" "z < y" by auto
hoelzl@42844:     have "?P x y"
hoelzl@42844:       apply (rule exI[of _ "{..<z}"])
hoelzl@42844:       apply (rule exI[of _ "{z<..}"])
hoelzl@42844:       using z by auto }
hoelzl@42844:   note * = this
hoelzl@42844: 
hoelzl@42844:   from `x ~= y`
hoelzl@42844:   show "EX U V. open U & open V & x : U & y : V & U Int V = {}"
hoelzl@42844:   proof (cases rule: linorder_cases)
hoelzl@42844:     assume "x = y" with `x ~= y` show ?thesis by simp
hoelzl@42844:   next assume "x < y" from *[OF this] show ?thesis by auto
hoelzl@42844:   next assume "y < x" from *[OF this] show ?thesis by auto
hoelzl@42844:   qed
hoelzl@42844: qed
hoelzl@42844: 
hoelzl@42844: subsubsection {* Convergent sequences *}
hoelzl@42844: 
hoelzl@44791: lemma lim_ereal[simp]:
hoelzl@44791:   "((\<lambda>n. ereal (f n)) ---> ereal x) net \<longleftrightarrow> (f ---> x) net" (is "?l = ?r")
hoelzl@42844: proof (intro iffI topological_tendstoI)
hoelzl@42844:   fix S assume "?l" "open S" "x \<in> S"
hoelzl@42844:   then show "eventually (\<lambda>x. f x \<in> S) net"
hoelzl@44791:     using `?l`[THEN topological_tendstoD, OF open_ereal, OF `open S`]
hoelzl@42844:     by (simp add: inj_image_mem_iff)
hoelzl@42844: next
hoelzl@44791:   fix S assume "?r" "open S" "ereal x \<in> S"
hoelzl@44791:   show "eventually (\<lambda>x. ereal (f x) \<in> S) net"
hoelzl@44791:     using `?r`[THEN topological_tendstoD, OF open_ereal_vimage, OF `open S`]
hoelzl@44791:     using `ereal x \<in> S` by auto
hoelzl@42844: qed
hoelzl@42844: 
hoelzl@44791: lemma lim_real_of_ereal[simp]:
hoelzl@44791:   assumes lim: "(f ---> ereal x) net"
hoelzl@42844:   shows "((\<lambda>x. real (f x)) ---> x) net"
hoelzl@42844: proof (intro topological_tendstoI)
hoelzl@42844:   fix S assume "open S" "x \<in> S"
hoelzl@44791:   then have S: "open S" "ereal x \<in> ereal ` S"
hoelzl@42844:     by (simp_all add: inj_image_mem_iff)
hoelzl@44791:   have "\<forall>x. f x \<in> ereal ` S \<longrightarrow> real (f x) \<in> S" by auto
hoelzl@44791:   from this lim[THEN topological_tendstoD, OF open_ereal, OF S]
hoelzl@42844:   show "eventually (\<lambda>x. real (f x) \<in> S) net"
hoelzl@42844:     by (rule eventually_mono)
hoelzl@42844: qed
hoelzl@42844: 
hoelzl@44791: lemma Lim_PInfty: "f ----> \<infinity> <-> (ALL B. EX N. ALL n>=N. f n >= ereal B)" (is "?l = ?r")
hoelzl@44794: proof
hoelzl@44794:   assume ?r
hoelzl@44794:   show ?l
hoelzl@44794:     apply(rule topological_tendstoI)
hoelzl@42844:     unfolding eventually_sequentially
hoelzl@44794:   proof-
hoelzl@44794:     fix S :: "ereal set" assume "open S" "\<infinity> : S"
hoelzl@42844:     from open_PInfty[OF this] guess B .. note B=this
hoelzl@42844:     from `?r`[rule_format,of "B+1"] guess N .. note N=this
hoelzl@42844:     show "EX N. ALL n>=N. f n : S" apply(rule_tac x=N in exI)
hoelzl@42844:     proof safe case goal1
hoelzl@44791:       have "ereal B < ereal (B + 1)" by auto
hoelzl@42844:       also have "... <= f n" using goal1 N by auto
hoelzl@42844:       finally show ?case using B by fastsimp
hoelzl@42844:     qed
hoelzl@42844:   qed
hoelzl@44794: next
hoelzl@44794:   assume ?l
hoelzl@44794:   show ?r
hoelzl@44791:   proof fix B::real have "open {ereal B<..}" "\<infinity> : {ereal B<..}" by auto
hoelzl@42844:     from topological_tendstoD[OF `?l` this,unfolded eventually_sequentially]
hoelzl@42844:     guess N .. note N=this
hoelzl@44791:     show "EX N. ALL n>=N. ereal B <= f n" apply(rule_tac x=N in exI) using N by auto
hoelzl@42844:   qed
hoelzl@42844: qed
hoelzl@42844: 
hoelzl@42844: 
hoelzl@44791: lemma Lim_MInfty: "f ----> (-\<infinity>) <-> (ALL B. EX N. ALL n>=N. f n <= ereal B)" (is "?l = ?r")
hoelzl@44794: proof
hoelzl@44794:   assume ?r
hoelzl@44794:   show ?l
hoelzl@44794:     apply(rule topological_tendstoI)
hoelzl@42844:     unfolding eventually_sequentially
hoelzl@44794:   proof-
hoelzl@44794:     fix S :: "ereal set"
hoelzl@44794:     assume "open S" "(-\<infinity>) : S"
hoelzl@42844:     from open_MInfty[OF this] guess B .. note B=this
hoelzl@42844:     from `?r`[rule_format,of "B-(1::real)"] guess N .. note N=this
hoelzl@42844:     show "EX N. ALL n>=N. f n : S" apply(rule_tac x=N in exI)
hoelzl@42844:     proof safe case goal1
hoelzl@44791:       have "ereal (B - 1) >= f n" using goal1 N by auto
hoelzl@44791:       also have "... < ereal B" by auto
hoelzl@42844:       finally show ?case using B by fastsimp
hoelzl@42844:     qed
hoelzl@42844:   qed
hoelzl@42844: next assume ?l show ?r
hoelzl@44791:   proof fix B::real have "open {..<ereal B}" "(-\<infinity>) : {..<ereal B}" by auto
hoelzl@42844:     from topological_tendstoD[OF `?l` this,unfolded eventually_sequentially]
hoelzl@42844:     guess N .. note N=this
hoelzl@44791:     show "EX N. ALL n>=N. ereal B >= f n" apply(rule_tac x=N in exI) using N by auto
hoelzl@42844:   qed
hoelzl@42844: qed
hoelzl@42844: 
hoelzl@42844: 
hoelzl@44791: lemma Lim_bounded_PInfty: assumes lim:"f ----> l" and "!!n. f n <= ereal B" shows "l ~= \<infinity>"
hoelzl@42844: proof(rule ccontr,unfold not_not) let ?B = "B + 1" assume as:"l=\<infinity>"
hoelzl@42844:   from lim[unfolded this Lim_PInfty,rule_format,of "?B"]
hoelzl@42844:   guess N .. note N=this[rule_format,OF le_refl]
hoelzl@44791:   hence "ereal ?B <= ereal B" using assms(2)[of N] by(rule order_trans)
hoelzl@44791:   hence "ereal ?B < ereal ?B" apply (rule le_less_trans) by auto
hoelzl@42844:   thus False by auto
hoelzl@42844: qed
hoelzl@42844: 
hoelzl@42844: 
hoelzl@44791: lemma Lim_bounded_MInfty: assumes lim:"f ----> l" and "!!n. f n >= ereal B" shows "l ~= (-\<infinity>)"
hoelzl@42844: proof(rule ccontr,unfold not_not) let ?B = "B - 1" assume as:"l=(-\<infinity>)"
hoelzl@42844:   from lim[unfolded this Lim_MInfty,rule_format,of "?B"]
hoelzl@42844:   guess N .. note N=this[rule_format,OF le_refl]
hoelzl@44791:   hence "ereal B <= ereal ?B" using assms(2)[of N] order_trans[of "ereal B" "f N" "ereal(B - 1)"] by blast
hoelzl@42844:   thus False by auto
hoelzl@42844: qed
hoelzl@42844: 
hoelzl@42844: 
hoelzl@42844: lemma tendsto_explicit:
hoelzl@42844:   "f ----> f0 <-> (ALL S. open S --> f0 : S --> (EX N. ALL n>=N. f n : S))"
hoelzl@42844:   unfolding tendsto_def eventually_sequentially by auto
hoelzl@42844: 
hoelzl@42844: 
hoelzl@42844: lemma tendsto_obtains_N:
hoelzl@42844:   assumes "f ----> f0"
hoelzl@42844:   assumes "open S" "f0 : S"
hoelzl@42844:   obtains N where "ALL n>=N. f n : S"
hoelzl@42844:   using tendsto_explicit[of f f0] assms by auto
hoelzl@42844: 
hoelzl@42844: 
hoelzl@42844: lemma tail_same_limit:
hoelzl@42844:   fixes X Y N
hoelzl@42844:   assumes "X ----> L" "ALL n>=N. X n = Y n"
hoelzl@42844:   shows "Y ----> L"
hoelzl@42844: proof-
hoelzl@42844: { fix S assume "open S" and "L:S"
hoelzl@42844:   from this obtain N1 where "ALL n>=N1. X n : S"
hoelzl@42844:      using assms unfolding tendsto_def eventually_sequentially by auto
hoelzl@42844:   hence "ALL n>=max N N1. Y n : S" using assms by auto
hoelzl@42844:   hence "EX N. ALL n>=N. Y n : S" apply(rule_tac x="max N N1" in exI) by auto
hoelzl@42844: }
hoelzl@42844: thus ?thesis using tendsto_explicit by auto
hoelzl@42844: qed
hoelzl@42844: 
hoelzl@42844: 
hoelzl@42844: lemma Lim_bounded_PInfty2:
hoelzl@44791: assumes lim:"f ----> l" and "ALL n>=N. f n <= ereal B"
hoelzl@42844: shows "l ~= \<infinity>"
hoelzl@42844: proof-
hoelzl@44791:   def g == "(%n. if n>=N then f n else ereal B)"
hoelzl@42844:   hence "g ----> l" using tail_same_limit[of f l N g] lim by auto
hoelzl@44791:   moreover have "!!n. g n <= ereal B" using g_def assms by auto
hoelzl@42844:   ultimately show ?thesis using  Lim_bounded_PInfty by auto
hoelzl@42844: qed
hoelzl@42844: 
hoelzl@44791: lemma Lim_bounded_ereal:
hoelzl@44791:   assumes lim:"f ----> (l :: ereal)"
hoelzl@42844:   and "ALL n>=M. f n <= C"
hoelzl@42844:   shows "l<=C"
hoelzl@42844: proof-
hoelzl@42844: { assume "l=(-\<infinity>)" hence ?thesis by auto }
hoelzl@42844: moreover
hoelzl@42844: { assume "~(l=(-\<infinity>))"
hoelzl@42844:   { assume "C=\<infinity>" hence ?thesis by auto }
hoelzl@42844:   moreover
hoelzl@42844:   { assume "C=(-\<infinity>)" hence "ALL n>=M. f n = (-\<infinity>)" using assms by auto
hoelzl@42844:     hence "l=(-\<infinity>)" using assms
hoelzl@42851:        tendsto_unique[OF trivial_limit_sequentially] tail_same_limit[of "\<lambda>n. -\<infinity>" "-\<infinity>" M f, OF tendsto_const] by auto
hoelzl@42844:     hence ?thesis by auto }
hoelzl@42844:   moreover
hoelzl@44791:   { assume "EX B. C = ereal B"
hoelzl@44791:     from this obtain B where B_def: "C=ereal B" by auto
hoelzl@42844:     hence "~(l=\<infinity>)" using Lim_bounded_PInfty2 assms by auto
hoelzl@44791:     from this obtain m where m_def: "ereal m=l" using `~(l=(-\<infinity>))` by (cases l) auto
hoelzl@44791:     from this obtain N where N_def: "ALL n>=N. f n : {ereal(m - 1) <..< ereal(m+1)}"
hoelzl@44791:        apply (subst tendsto_obtains_N[of f l "{ereal(m - 1) <..< ereal(m+1)}"]) using assms by auto
hoelzl@42844:     { fix n assume "n>=N"
hoelzl@44791:       hence "EX r. ereal r = f n" using N_def by (cases "f n") auto
hoelzl@44791:     } from this obtain g where g_def: "ALL n>=N. ereal (g n) = f n" by metis
hoelzl@44791:     hence "(%n. ereal (g n)) ----> l" using tail_same_limit[of f l N] assms by auto
hoelzl@42844:     hence *: "(%n. g n) ----> m" using m_def by auto
hoelzl@42844:     { fix n assume "n>=max N M"
hoelzl@44791:       hence "ereal (g n) <= ereal B" using assms g_def B_def by auto
hoelzl@42844:       hence "g n <= B" by auto
hoelzl@42844:     } hence "EX N. ALL n>=N. g n <= B" by blast
hoelzl@42844:     hence "m<=B" using * LIMSEQ_le_const2[of g m B] by auto
hoelzl@42844:     hence ?thesis using m_def B_def by auto
hoelzl@42844:   } ultimately have ?thesis by (cases C) auto
hoelzl@42844: } ultimately show ?thesis by blast
hoelzl@42844: qed
hoelzl@42844: 
hoelzl@44791: lemma real_of_ereal_mult[simp]:
hoelzl@44791:   fixes a b :: ereal shows "real (a * b) = real a * real b"
hoelzl@44791:   by (cases rule: ereal2_cases[of a b]) auto
hoelzl@42844: 
hoelzl@44791: lemma real_of_ereal_eq_0:
hoelzl@44794:   fixes x :: ereal shows "real x = 0 \<longleftrightarrow> x = \<infinity> \<or> x = -\<infinity> \<or> x = 0"
hoelzl@42844:   by (cases x) auto
hoelzl@42844: 
hoelzl@44791: lemma tendsto_ereal_realD:
hoelzl@44791:   fixes f :: "'a \<Rightarrow> ereal"
hoelzl@44791:   assumes "x \<noteq> 0" and tendsto: "((\<lambda>x. ereal (real (f x))) ---> x) net"
hoelzl@42844:   shows "(f ---> x) net"
hoelzl@42844: proof (intro topological_tendstoI)
hoelzl@42844:   fix S assume S: "open S" "x \<in> S"
hoelzl@42844:   with `x \<noteq> 0` have "open (S - {0})" "x \<in> S - {0}" by auto
hoelzl@42844:   from tendsto[THEN topological_tendstoD, OF this]
hoelzl@42844:   show "eventually (\<lambda>x. f x \<in> S) net"
hoelzl@44791:     by (rule eventually_rev_mp) (auto simp: ereal_real real_of_ereal_0)
hoelzl@42844: qed
hoelzl@42844: 
hoelzl@44791: lemma tendsto_ereal_realI:
hoelzl@44791:   fixes f :: "'a \<Rightarrow> ereal"
hoelzl@42847:   assumes x: "\<bar>x\<bar> \<noteq> \<infinity>" and tendsto: "(f ---> x) net"
hoelzl@44791:   shows "((\<lambda>x. ereal (real (f x))) ---> x) net"
hoelzl@42844: proof (intro topological_tendstoI)
hoelzl@42844:   fix S assume "open S" "x \<in> S"
hoelzl@42844:   with x have "open (S - {\<infinity>, -\<infinity>})" "x \<in> S - {\<infinity>, -\<infinity>}" by auto
hoelzl@42844:   from tendsto[THEN topological_tendstoD, OF this]
hoelzl@44791:   show "eventually (\<lambda>x. ereal (real (f x)) \<in> S) net"
hoelzl@44791:     by (elim eventually_elim1) (auto simp: ereal_real)
hoelzl@42844: qed
hoelzl@42844: 
hoelzl@44791: lemma ereal_mult_cancel_left:
hoelzl@44791:   fixes a b c :: ereal shows "a * b = a * c \<longleftrightarrow>
hoelzl@42847:     ((\<bar>a\<bar> = \<infinity> \<and> 0 < b * c) \<or> a = 0 \<or> b = c)"
hoelzl@44791:   by (cases rule: ereal3_cases[of a b c])
hoelzl@42844:      (simp_all add: zero_less_mult_iff)
hoelzl@42844: 
hoelzl@44791: lemma ereal_inj_affinity:
hoelzl@44794:   fixes m t :: ereal
hoelzl@42847:   assumes "\<bar>m\<bar> \<noteq> \<infinity>" "m \<noteq> 0" "\<bar>t\<bar> \<noteq> \<infinity>"
hoelzl@42844:   shows "inj_on (\<lambda>x. m * x + t) A"
hoelzl@42844:   using assms
hoelzl@44791:   by (cases rule: ereal2_cases[of m t])
hoelzl@44791:      (auto intro!: inj_onI simp: ereal_add_cancel_right ereal_mult_cancel_left)
hoelzl@42844: 
hoelzl@44791: lemma ereal_PInfty_eq_plus[simp]:
hoelzl@44794:   fixes a b :: ereal
hoelzl@42844:   shows "\<infinity> = a + b \<longleftrightarrow> a = \<infinity> \<or> b = \<infinity>"
hoelzl@44791:   by (cases rule: ereal2_cases[of a b]) auto
hoelzl@42844: 
hoelzl@44791: lemma ereal_MInfty_eq_plus[simp]:
hoelzl@44794:   fixes a b :: ereal
hoelzl@42844:   shows "-\<infinity> = a + b \<longleftrightarrow> (a = -\<infinity> \<and> b \<noteq> \<infinity>) \<or> (b = -\<infinity> \<and> a \<noteq> \<infinity>)"
hoelzl@44791:   by (cases rule: ereal2_cases[of a b]) auto
hoelzl@42844: 
hoelzl@44791: lemma ereal_less_divide_pos:
hoelzl@44794:   fixes x y :: ereal
hoelzl@44794:   shows "x > 0 \<Longrightarrow> x \<noteq> \<infinity> \<Longrightarrow> y < z / x \<longleftrightarrow> x * y < z"
hoelzl@44791:   by (cases rule: ereal3_cases[of x y z]) (auto simp: field_simps)
hoelzl@42844: 
hoelzl@44791: lemma ereal_divide_less_pos:
hoelzl@44794:   fixes x y z :: ereal
hoelzl@44794:   shows "x > 0 \<Longrightarrow> x \<noteq> \<infinity> \<Longrightarrow> y / x < z \<longleftrightarrow> y < x * z"
hoelzl@44791:   by (cases rule: ereal3_cases[of x y z]) (auto simp: field_simps)
hoelzl@42844: 
hoelzl@44791: lemma ereal_divide_eq:
hoelzl@44794:   fixes a b c :: ereal
hoelzl@44794:   shows "b \<noteq> 0 \<Longrightarrow> \<bar>b\<bar> \<noteq> \<infinity> \<Longrightarrow> a / b = c \<longleftrightarrow> a = b * c"
hoelzl@44791:   by (cases rule: ereal3_cases[of a b c])
hoelzl@42844:      (simp_all add: field_simps)
hoelzl@42844: 
hoelzl@44794: lemma ereal_inverse_not_MInfty[simp]: "inverse (a::ereal) \<noteq> -\<infinity>"
hoelzl@42844:   by (cases a) auto
hoelzl@42844: 
hoelzl@44791: lemma ereal_mult_m1[simp]: "x * ereal (-1) = -x"
hoelzl@42844:   by (cases x) auto
hoelzl@42844: 
hoelzl@44791: lemma ereal_LimI_finite:
hoelzl@44794:   fixes x :: ereal
hoelzl@42847:   assumes "\<bar>x\<bar> \<noteq> \<infinity>"
hoelzl@42844:   assumes "!!r. 0 < r ==> EX N. ALL n>=N. u n < x + r & x < u n + r"
hoelzl@42844:   shows "u ----> x"
hoelzl@42844: proof (rule topological_tendstoI, unfold eventually_sequentially)
hoelzl@44791:   obtain rx where rx_def: "x=ereal rx" using assms by (cases x) auto
hoelzl@42844:   fix S assume "open S" "x : S"
hoelzl@44791:   then have "open (ereal -` S)" unfolding open_ereal_def by auto
hoelzl@44791:   with `x \<in> S` obtain r where "0 < r" and dist: "!!y. dist y rx < r ==> ereal y \<in> S"
hoelzl@42846:     unfolding open_real_def rx_def by auto
hoelzl@42844:   then obtain n where
hoelzl@44791:     upper: "!!N. n <= N ==> u N < x + ereal r" and
hoelzl@44791:     lower: "!!N. n <= N ==> x < u N + ereal r" using assms(2)[of "ereal r"] by auto
hoelzl@42844:   show "EX N. ALL n>=N. u n : S"
hoelzl@42844:   proof (safe intro!: exI[of _ n])
hoelzl@42844:     fix N assume "n <= N"
hoelzl@42844:     from upper[OF this] lower[OF this] assms `0 < r`
hoelzl@42844:     have "u N ~: {\<infinity>,(-\<infinity>)}" by auto
hoelzl@44791:     from this obtain ra where ra_def: "(u N) = ereal ra" by (cases "u N") auto
hoelzl@42844:     hence "rx < ra + r" and "ra < rx + r"
hoelzl@42844:        using rx_def assms `0 < r` lower[OF `n <= N`] upper[OF `n <= N`] by auto
hoelzl@42846:     hence "dist (real (u N)) rx < r"
hoelzl@42844:       using rx_def ra_def
hoelzl@42844:       by (auto simp: dist_real_def abs_diff_less_iff field_simps)
hoelzl@42847:     from dist[OF this] show "u N : S" using `u N  ~: {\<infinity>, -\<infinity>}`
hoelzl@44791:       by (auto simp: ereal_real split: split_if_asm)
hoelzl@42844:   qed
hoelzl@42844: qed
hoelzl@42844: 
hoelzl@44791: lemma ereal_LimI_finite_iff:
hoelzl@44794:   fixes x :: ereal
hoelzl@42847:   assumes "\<bar>x\<bar> \<noteq> \<infinity>"
hoelzl@42844:   shows "u ----> x <-> (ALL r. 0 < r --> (EX N. ALL n>=N. u n < x + r & x < u n + r))"
hoelzl@42844:   (is "?lhs <-> ?rhs")
hoelzl@42847: proof
hoelzl@42847:   assume lim: "u ----> x"
hoelzl@44791:   { fix r assume "(r::ereal)>0"
hoelzl@42844:     from this obtain N where N_def: "ALL n>=N. u n : {x - r <..< x + r}"
hoelzl@42844:        apply (subst tendsto_obtains_N[of u x "{x - r <..< x + r}"])
hoelzl@44791:        using lim ereal_between[of x r] assms `r>0` by auto
hoelzl@42844:     hence "EX N. ALL n>=N. u n < x + r & x < u n + r"
hoelzl@44791:       using ereal_minus_less[of r x] by (cases r) auto
hoelzl@42847:   } then show "?rhs" by auto
hoelzl@42847: next
hoelzl@42847:   assume ?rhs then show "u ----> x"
hoelzl@44791:     using ereal_LimI_finite[of x] assms by auto
hoelzl@42844: qed
hoelzl@42844: 
hoelzl@42844: 
hoelzl@42844: subsubsection {* @{text Liminf} and @{text Limsup} *}
hoelzl@42844: 
hoelzl@42844: definition
hoelzl@42844:   "Liminf net f = (GREATEST l. \<forall>y<l. eventually (\<lambda>x. y < f x) net)"
hoelzl@42844: 
hoelzl@42844: definition
hoelzl@42844:   "Limsup net f = (LEAST l. \<forall>y>l. eventually (\<lambda>x. f x < y) net)"
hoelzl@42844: 
hoelzl@42844: lemma Liminf_Sup:
haftmann@44812:   fixes f :: "'a => 'b::complete_linorder"
hoelzl@42844:   shows "Liminf net f = Sup {l. \<forall>y<l. eventually (\<lambda>x. y < f x) net}"
hoelzl@42844:   by (auto intro!: Greatest_equality complete_lattice_class.Sup_upper simp: less_Sup_iff Liminf_def)
hoelzl@42844: 
hoelzl@42844: lemma Limsup_Inf:
haftmann@44812:   fixes f :: "'a => 'b::complete_linorder"
hoelzl@42844:   shows "Limsup net f = Inf {l. \<forall>y>l. eventually (\<lambda>x. f x < y) net}"
hoelzl@42844:   by (auto intro!: Least_equality complete_lattice_class.Inf_lower simp: Inf_less_iff Limsup_def)
hoelzl@42844: 
hoelzl@44791: lemma ereal_SupI:
hoelzl@44791:   fixes x :: ereal
hoelzl@42844:   assumes "\<And>y. y \<in> A \<Longrightarrow> y \<le> x"
hoelzl@42844:   assumes "\<And>y. (\<And>z. z \<in> A \<Longrightarrow> z \<le> y) \<Longrightarrow> x \<le> y"
hoelzl@42844:   shows "Sup A = x"
hoelzl@44791:   unfolding Sup_ereal_def
hoelzl@42844:   using assms by (auto intro!: Least_equality)
hoelzl@42844: 
hoelzl@44791: lemma ereal_InfI:
hoelzl@44791:   fixes x :: ereal
hoelzl@42844:   assumes "\<And>i. i \<in> A \<Longrightarrow> x \<le> i"
hoelzl@42844:   assumes "\<And>y. (\<And>i. i \<in> A \<Longrightarrow> y \<le> i) \<Longrightarrow> y \<le> x"
hoelzl@42844:   shows "Inf A = x"
hoelzl@44791:   unfolding Inf_ereal_def
hoelzl@42844:   using assms by (auto intro!: Greatest_equality)
hoelzl@42844: 
hoelzl@42844: lemma Limsup_const:
haftmann@44812:   fixes c :: "'a::complete_linorder"
hoelzl@42844:   assumes ntriv: "\<not> trivial_limit net"
hoelzl@42844:   shows "Limsup net (\<lambda>x. c) = c"
hoelzl@42844:   unfolding Limsup_Inf
hoelzl@42844: proof (safe intro!: antisym complete_lattice_class.Inf_greatest complete_lattice_class.Inf_lower)
hoelzl@42844:   fix x assume *: "\<forall>y>x. eventually (\<lambda>_. c < y) net"
hoelzl@42844:   show "c \<le> x"
hoelzl@42844:   proof (rule ccontr)
hoelzl@42844:     assume "\<not> c \<le> x" then have "x < c" by auto
hoelzl@42844:     then show False using ntriv * by (auto simp: trivial_limit_def)
hoelzl@42844:   qed
hoelzl@42844: qed auto
hoelzl@42844: 
hoelzl@42844: lemma Liminf_const:
haftmann@44812:   fixes c :: "'a::complete_linorder"
hoelzl@42844:   assumes ntriv: "\<not> trivial_limit net"
hoelzl@42844:   shows "Liminf net (\<lambda>x. c) = c"
hoelzl@42844:   unfolding Liminf_Sup
hoelzl@42844: proof (safe intro!: antisym complete_lattice_class.Sup_least complete_lattice_class.Sup_upper)
hoelzl@42844:   fix x assume *: "\<forall>y<x. eventually (\<lambda>_. y < c) net"
hoelzl@42844:   show "x \<le> c"
hoelzl@42844:   proof (rule ccontr)
hoelzl@42844:     assume "\<not> x \<le> c" then have "c < x" by auto
hoelzl@42844:     then show False using ntriv * by (auto simp: trivial_limit_def)
hoelzl@42844:   qed
hoelzl@42844: qed auto
hoelzl@42844: 
haftmann@44812: lemma (in order) mono_set:
haftmann@44812:   "mono S \<longleftrightarrow> (\<forall>x y. x \<le> y \<longrightarrow> x \<in> S \<longrightarrow> y \<in> S)"
hoelzl@42844:   by (auto simp: mono_def mem_def)
hoelzl@42844: 
haftmann@44812: lemma (in order) mono_greaterThan [intro, simp]: "mono {B<..}" unfolding mono_set by auto
haftmann@44812: lemma (in order) mono_atLeast [intro, simp]: "mono {B..}" unfolding mono_set by auto
haftmann@44812: lemma (in order) mono_UNIV [intro, simp]: "mono UNIV" unfolding mono_set by auto
haftmann@44812: lemma (in order) mono_empty [intro, simp]: "mono {}" unfolding mono_set by auto
hoelzl@42844: 
haftmann@44812: lemma (in complete_linorder) mono_set_iff:
haftmann@44812:   fixes S :: "'a set"
hoelzl@42844:   defines "a \<equiv> Inf S"
hoelzl@42844:   shows "mono S \<longleftrightarrow> (S = {a <..} \<or> S = {a..})" (is "_ = ?c")
hoelzl@42844: proof
hoelzl@42844:   assume "mono S"
hoelzl@42844:   then have mono: "\<And>x y. x \<le> y \<Longrightarrow> x \<in> S \<Longrightarrow> y \<in> S" by (auto simp: mono_set)
hoelzl@42844:   show ?c
hoelzl@42844:   proof cases
hoelzl@42844:     assume "a \<in> S"
hoelzl@42844:     show ?c
hoelzl@42844:       using mono[OF _ `a \<in> S`]
haftmann@44812:       by (auto intro: Inf_lower simp: a_def)
hoelzl@42844:   next
hoelzl@42844:     assume "a \<notin> S"
hoelzl@42844:     have "S = {a <..}"
hoelzl@42844:     proof safe
hoelzl@42844:       fix x assume "x \<in> S"
haftmann@44812:       then have "a \<le> x" unfolding a_def by (rule Inf_lower)
hoelzl@42844:       then show "a < x" using `x \<in> S` `a \<notin> S` by (cases "a = x") auto
hoelzl@42844:     next
hoelzl@42844:       fix x assume "a < x"
hoelzl@42844:       then obtain y where "y < x" "y \<in> S" unfolding a_def Inf_less_iff ..
hoelzl@42844:       with mono[of y x] show "x \<in> S" by auto
hoelzl@42844:     qed
hoelzl@42844:     then show ?c ..
hoelzl@42844:   qed
hoelzl@42844: qed auto
hoelzl@42844: 
hoelzl@42844: lemma lim_imp_Liminf:
hoelzl@44791:   fixes f :: "'a \<Rightarrow> ereal"
hoelzl@42844:   assumes ntriv: "\<not> trivial_limit net"
hoelzl@42844:   assumes lim: "(f ---> f0) net"
hoelzl@42844:   shows "Liminf net f = f0"
hoelzl@42844:   unfolding Liminf_Sup
hoelzl@44791: proof (safe intro!: ereal_SupI)
hoelzl@42844:   fix y assume *: "\<forall>y'<y. eventually (\<lambda>x. y' < f x) net"
hoelzl@42844:   show "y \<le> f0"
hoelzl@44791:   proof (rule ereal_le_ereal)
hoelzl@42844:     fix B assume "B < y"
hoelzl@42844:     { assume "f0 < B"
hoelzl@42844:       then have "eventually (\<lambda>x. f x < B \<and> B < f x) net"
hoelzl@42844:          using topological_tendstoD[OF lim, of "{..<B}"] *[rule_format, OF `B < y`]
hoelzl@42844:          by (auto intro: eventually_conj)
hoelzl@42844:       also have "(\<lambda>x. f x < B \<and> B < f x) = (\<lambda>x. False)" by (auto simp: fun_eq_iff)
hoelzl@42844:       finally have False using ntriv[unfolded trivial_limit_def] by auto
hoelzl@42844:     } then show "B \<le> f0" by (metis linorder_le_less_linear)
hoelzl@42844:   qed
hoelzl@42844: next
hoelzl@42844:   fix y assume *: "\<forall>z. z \<in> {l. \<forall>y<l. eventually (\<lambda>x. y < f x) net} \<longrightarrow> z \<le> y"
hoelzl@42844:   show "f0 \<le> y"
hoelzl@42844:   proof (safe intro!: *[rule_format])
hoelzl@42844:     fix y assume "y < f0" then show "eventually (\<lambda>x. y < f x) net"
hoelzl@42844:       using lim[THEN topological_tendstoD, of "{y <..}"] by auto
hoelzl@42844:   qed
hoelzl@42844: qed
hoelzl@42844: 
hoelzl@44791: lemma ereal_Liminf_le_Limsup:
hoelzl@44791:   fixes f :: "'a \<Rightarrow> ereal"
hoelzl@42844:   assumes ntriv: "\<not> trivial_limit net"
hoelzl@42844:   shows "Liminf net f \<le> Limsup net f"
hoelzl@42844:   unfolding Limsup_Inf Liminf_Sup
hoelzl@42844: proof (safe intro!: complete_lattice_class.Inf_greatest  complete_lattice_class.Sup_least)
hoelzl@42844:   fix u v assume *: "\<forall>y<u. eventually (\<lambda>x. y < f x) net" "\<forall>y>v. eventually (\<lambda>x. f x < y) net"
hoelzl@42844:   show "u \<le> v"
hoelzl@42844:   proof (rule ccontr)
hoelzl@42844:     assume "\<not> u \<le> v"
hoelzl@42844:     then obtain t where "t < u" "v < t"
hoelzl@44791:       using ereal_dense[of v u] by (auto simp: not_le)
hoelzl@42844:     then have "eventually (\<lambda>x. t < f x \<and> f x < t) net"
hoelzl@42844:       using * by (auto intro: eventually_conj)
hoelzl@42844:     also have "(\<lambda>x. t < f x \<and> f x < t) = (\<lambda>x. False)" by (auto simp: fun_eq_iff)
hoelzl@42844:     finally show False using ntriv by (auto simp: trivial_limit_def)
hoelzl@42844:   qed
hoelzl@42844: qed
hoelzl@42844: 
hoelzl@42844: lemma Liminf_mono:
hoelzl@44791:   fixes f g :: "'a => ereal"
hoelzl@42844:   assumes ev: "eventually (\<lambda>x. f x \<le> g x) net"
hoelzl@42844:   shows "Liminf net f \<le> Liminf net g"
hoelzl@42844:   unfolding Liminf_Sup
hoelzl@42844: proof (safe intro!: Sup_mono bexI)
hoelzl@42844:   fix a y assume "\<forall>y<a. eventually (\<lambda>x. y < f x) net" and "y < a"
hoelzl@42844:   then have "eventually (\<lambda>x. y < f x) net" by auto
hoelzl@42844:   then show "eventually (\<lambda>x. y < g x) net"
hoelzl@42844:     by (rule eventually_rev_mp) (rule eventually_mono[OF _ ev], auto)
hoelzl@42844: qed simp
hoelzl@42844: 
hoelzl@42844: lemma Liminf_eq:
hoelzl@44791:   fixes f g :: "'a \<Rightarrow> ereal"
hoelzl@42844:   assumes "eventually (\<lambda>x. f x = g x) net"
hoelzl@42844:   shows "Liminf net f = Liminf net g"
hoelzl@42844:   by (intro antisym Liminf_mono eventually_mono[OF _ assms]) auto
hoelzl@42844: 
hoelzl@42844: lemma Liminf_mono_all:
hoelzl@44791:   fixes f g :: "'a \<Rightarrow> ereal"
hoelzl@42844:   assumes "\<And>x. f x \<le> g x"
hoelzl@42844:   shows "Liminf net f \<le> Liminf net g"
hoelzl@42844:   using assms by (intro Liminf_mono always_eventually) auto
hoelzl@42844: 
hoelzl@42844: lemma Limsup_mono:
hoelzl@44791:   fixes f g :: "'a \<Rightarrow> ereal"
hoelzl@42844:   assumes ev: "eventually (\<lambda>x. f x \<le> g x) net"
hoelzl@42844:   shows "Limsup net f \<le> Limsup net g"
hoelzl@42844:   unfolding Limsup_Inf
hoelzl@42844: proof (safe intro!: Inf_mono bexI)
hoelzl@42844:   fix a y assume "\<forall>y>a. eventually (\<lambda>x. g x < y) net" and "a < y"
hoelzl@42844:   then have "eventually (\<lambda>x. g x < y) net" by auto
hoelzl@42844:   then show "eventually (\<lambda>x. f x < y) net"
hoelzl@42844:     by (rule eventually_rev_mp) (rule eventually_mono[OF _ ev], auto)
hoelzl@42844: qed simp
hoelzl@42844: 
hoelzl@42844: lemma Limsup_mono_all:
hoelzl@44791:   fixes f g :: "'a \<Rightarrow> ereal"
hoelzl@42844:   assumes "\<And>x. f x \<le> g x"
hoelzl@42844:   shows "Limsup net f \<le> Limsup net g"
hoelzl@42844:   using assms by (intro Limsup_mono always_eventually) auto
hoelzl@42844: 
hoelzl@42844: lemma Limsup_eq:
hoelzl@44791:   fixes f g :: "'a \<Rightarrow> ereal"
hoelzl@42844:   assumes "eventually (\<lambda>x. f x = g x) net"
hoelzl@42844:   shows "Limsup net f = Limsup net g"
hoelzl@42844:   by (intro antisym Limsup_mono eventually_mono[OF _ assms]) auto
hoelzl@42844: 
hoelzl@42844: abbreviation "liminf \<equiv> Liminf sequentially"
hoelzl@42844: 
hoelzl@42844: abbreviation "limsup \<equiv> Limsup sequentially"
hoelzl@42844: 
hoelzl@42844: lemma liminf_SUPR_INFI:
hoelzl@44791:   fixes f :: "nat \<Rightarrow> ereal"
hoelzl@42844:   shows "liminf f = (SUP n. INF m:{n..}. f m)"
hoelzl@42844:   unfolding Liminf_Sup eventually_sequentially
hoelzl@42844: proof (safe intro!: antisym complete_lattice_class.Sup_least)
hoelzl@42844:   fix x assume *: "\<forall>y<x. \<exists>N. \<forall>n\<ge>N. y < f n" show "x \<le> (SUP n. INF m:{n..}. f m)"
hoelzl@44791:   proof (rule ereal_le_ereal)
hoelzl@42844:     fix y assume "y < x"
hoelzl@42844:     with * obtain N where "\<And>n. N \<le> n \<Longrightarrow> y < f n" by auto
hoelzl@42844:     then have "y \<le> (INF m:{N..}. f m)" by (force simp: le_INF_iff)
hoelzl@42844:     also have "\<dots> \<le> (SUP n. INF m:{n..}. f m)" by (intro le_SUPI) auto
hoelzl@42844:     finally show "y \<le> (SUP n. INF m:{n..}. f m)" .
hoelzl@42844:   qed
hoelzl@42844: next
hoelzl@42844:   show "(SUP n. INF m:{n..}. f m) \<le> Sup {l. \<forall>y<l. \<exists>N. \<forall>n\<ge>N. y < f n}"
hoelzl@42844:   proof (unfold SUPR_def, safe intro!: Sup_mono bexI)
hoelzl@42844:     fix y n assume "y < INFI {n..} f"
hoelzl@42844:     from less_INFD[OF this] show "\<exists>N. \<forall>n\<ge>N. y < f n" by (intro exI[of _ n]) auto
hoelzl@42844:   qed (rule order_refl)
hoelzl@42844: qed
hoelzl@42844: 
hoelzl@42844: lemma tail_same_limsup:
hoelzl@44791:   fixes X Y :: "nat => ereal"
hoelzl@42844:   assumes "\<And>n. N \<le> n \<Longrightarrow> X n = Y n"
hoelzl@42844:   shows "limsup X = limsup Y"
hoelzl@42844:   using Limsup_eq[of X Y sequentially] eventually_sequentially assms by auto
hoelzl@42844: 
hoelzl@42844: lemma tail_same_liminf:
hoelzl@44791:   fixes X Y :: "nat => ereal"
hoelzl@42844:   assumes "\<And>n. N \<le> n \<Longrightarrow> X n = Y n"
hoelzl@42844:   shows "liminf X = liminf Y"
hoelzl@42844:   using Liminf_eq[of X Y sequentially] eventually_sequentially assms by auto
hoelzl@42844: 
hoelzl@42844: lemma liminf_mono:
hoelzl@44791:   fixes X Y :: "nat \<Rightarrow> ereal"
hoelzl@42844:   assumes "\<And>n. N \<le> n \<Longrightarrow> X n <= Y n"
hoelzl@42844:   shows "liminf X \<le> liminf Y"
hoelzl@42844:   using Liminf_mono[of X Y sequentially] eventually_sequentially assms by auto
hoelzl@42844: 
hoelzl@42844: lemma limsup_mono:
hoelzl@44791:   fixes X Y :: "nat => ereal"
hoelzl@42844:   assumes "\<And>n. N \<le> n \<Longrightarrow> X n <= Y n"
hoelzl@42844:   shows "limsup X \<le> limsup Y"
hoelzl@42844:   using Limsup_mono[of X Y sequentially] eventually_sequentially assms by auto
hoelzl@42844: 
hoelzl@42844: declare trivial_limit_sequentially[simp]
hoelzl@42844: 
hoelzl@42849: lemma
hoelzl@44791:   fixes X :: "nat \<Rightarrow> ereal"
hoelzl@44791:   shows ereal_incseq_uminus[simp]: "incseq (\<lambda>i. - X i) = decseq X"
hoelzl@44791:     and ereal_decseq_uminus[simp]: "decseq (\<lambda>i. - X i) = incseq X"
hoelzl@42849:   unfolding incseq_def decseq_def by auto
hoelzl@42849: 
hoelzl@42844: lemma liminf_bounded:
hoelzl@44791:   fixes X Y :: "nat \<Rightarrow> ereal"
hoelzl@42844:   assumes "\<And>n. N \<le> n \<Longrightarrow> C \<le> X n"
hoelzl@42844:   shows "C \<le> liminf X"
hoelzl@42844:   using liminf_mono[of N "\<lambda>n. C" X] assms Liminf_const[of sequentially C] by simp
hoelzl@42844: 
hoelzl@42844: lemma limsup_bounded:
hoelzl@44791:   fixes X Y :: "nat => ereal"
hoelzl@42844:   assumes "\<And>n. N \<le> n \<Longrightarrow> X n <= C"
hoelzl@42844:   shows "limsup X \<le> C"
hoelzl@42844:   using limsup_mono[of N X "\<lambda>n. C"] assms Limsup_const[of sequentially C] by simp
hoelzl@42844: 
hoelzl@42844: lemma liminf_bounded_iff:
hoelzl@44791:   fixes x :: "nat \<Rightarrow> ereal"
hoelzl@42844:   shows "C \<le> liminf x \<longleftrightarrow> (\<forall>B<C. \<exists>N. \<forall>n\<ge>N. B < x n)" (is "?lhs <-> ?rhs")
hoelzl@42844: proof safe
hoelzl@42844:   fix B assume "B < C" "C \<le> liminf x"
hoelzl@42844:   then have "B < liminf x" by auto
hoelzl@42844:   then obtain N where "B < (INF m:{N..}. x m)"
hoelzl@42844:     unfolding liminf_SUPR_INFI SUPR_def less_Sup_iff by auto
hoelzl@42844:   from less_INFD[OF this] show "\<exists>N. \<forall>n\<ge>N. B < x n" by auto
hoelzl@42844: next
hoelzl@42844:   assume *: "\<forall>B<C. \<exists>N. \<forall>n\<ge>N. B < x n"
hoelzl@42844:   { fix B assume "B<C"
hoelzl@42844:     then obtain N where "\<forall>n\<ge>N. B < x n" using `?rhs` by auto
hoelzl@42844:     hence "B \<le> (INF m:{N..}. x m)" by (intro le_INFI) auto
hoelzl@42844:     also have "... \<le> liminf x" unfolding liminf_SUPR_INFI by (intro le_SUPI) simp
hoelzl@42844:     finally have "B \<le> liminf x" .
hoelzl@42844:   } then show "?lhs" by (metis * leD liminf_bounded linorder_le_less_linear)
hoelzl@42844: qed
hoelzl@42844: 
hoelzl@42844: lemma liminf_subseq_mono:
hoelzl@44791:   fixes X :: "nat \<Rightarrow> ereal"
hoelzl@42844:   assumes "subseq r"
hoelzl@42844:   shows "liminf X \<le> liminf (X \<circ> r) "
hoelzl@42844: proof-
hoelzl@42844:   have "\<And>n. (INF m:{n..}. X m) \<le> (INF m:{n..}. (X \<circ> r) m)"
hoelzl@42844:   proof (safe intro!: INF_mono)
hoelzl@42844:     fix n m :: nat assume "n \<le> m" then show "\<exists>ma\<in>{n..}. X ma \<le> (X \<circ> r) m"
hoelzl@42844:       using seq_suble[OF `subseq r`, of m] by (intro bexI[of _ "r m"]) auto
hoelzl@42844:   qed
hoelzl@42844:   then show ?thesis by (auto intro!: SUP_mono simp: liminf_SUPR_INFI comp_def)
hoelzl@42844: qed
hoelzl@42844: 
hoelzl@44791: lemma ereal_real': assumes "\<bar>x\<bar> \<noteq> \<infinity>" shows "ereal (real x) = x"
hoelzl@42847:   using assms by auto
hoelzl@42844: 
hoelzl@44791: lemma ereal_le_ereal_bounded:
hoelzl@44791:   fixes x y z :: ereal
hoelzl@42849:   assumes "z \<le> y"
hoelzl@42849:   assumes *: "\<And>B. z < B \<Longrightarrow> B < x \<Longrightarrow> B \<le> y"
hoelzl@42849:   shows "x \<le> y"
hoelzl@44791: proof (rule ereal_le_ereal)
hoelzl@42849:   fix B assume "B < x"
hoelzl@42849:   show "B \<le> y"
hoelzl@42849:   proof cases
hoelzl@42849:     assume "z < B" from *[OF this `B < x`] show "B \<le> y" .
hoelzl@42847:   next
hoelzl@42849:     assume "\<not> z < B" with `z \<le> y` show "B \<le> y" by auto
hoelzl@42847:   qed
hoelzl@42844: qed
hoelzl@42844: 
hoelzl@44791: lemma fixes x y :: ereal
hoelzl@42849:   shows Sup_atMost[simp]: "Sup {.. y} = y"
hoelzl@42849:     and Sup_lessThan[simp]: "Sup {..< y} = y"
hoelzl@42849:     and Sup_atLeastAtMost[simp]: "x \<le> y \<Longrightarrow> Sup { x .. y} = y"
hoelzl@42849:     and Sup_greaterThanAtMost[simp]: "x < y \<Longrightarrow> Sup { x <.. y} = y"
hoelzl@42849:     and Sup_atLeastLessThan[simp]: "x < y \<Longrightarrow> Sup { x ..< y} = y"
hoelzl@44791:   by (auto simp: Sup_ereal_def intro!: Least_equality
hoelzl@44791:            intro: ereal_le_ereal ereal_le_ereal_bounded[of x])
hoelzl@42849: 
hoelzl@42849: lemma Sup_greaterThanlessThan[simp]:
hoelzl@44791:   fixes x y :: ereal assumes "x < y" shows "Sup { x <..< y} = y"
hoelzl@44791:   unfolding Sup_ereal_def
hoelzl@44791: proof (intro Least_equality ereal_le_ereal_bounded[of _ _ y])
hoelzl@42849:   fix z assume z: "\<forall>u\<in>{x<..<y}. u \<le> z"
hoelzl@44791:   from ereal_dense[OF `x < y`] guess w .. note w = this
hoelzl@42849:   with z[THEN bspec, of w] show "x \<le> z" by auto
hoelzl@42849: qed auto
hoelzl@42849: 
hoelzl@44791: lemma real_ereal_id: "real o ereal = id"
hoelzl@42844: proof-
hoelzl@44791: { fix x have "(real o ereal) x = id x" by auto }
hoelzl@42844: from this show ?thesis using ext by blast
hoelzl@42844: qed
hoelzl@42844: 
hoelzl@44794: lemma open_image_ereal: "open(UNIV-{ \<infinity> , (-\<infinity> :: ereal)})"
hoelzl@44791: by (metis range_ereal open_ereal open_UNIV)
hoelzl@42844: 
hoelzl@44791: lemma ereal_le_distrib:
hoelzl@44791:   fixes a b c :: ereal shows "c * (a + b) \<le> c * a + c * b"
hoelzl@44791:   by (cases rule: ereal3_cases[of a b c])
hoelzl@42844:      (auto simp add: field_simps not_le mult_le_0_iff mult_less_0_iff)
hoelzl@42844: 
hoelzl@44791: lemma ereal_pos_distrib:
hoelzl@44791:   fixes a b c :: ereal assumes "0 \<le> c" "c \<noteq> \<infinity>" shows "c * (a + b) = c * a + c * b"
hoelzl@44791:   using assms by (cases rule: ereal3_cases[of a b c])
hoelzl@42844:                  (auto simp add: field_simps not_le mult_le_0_iff mult_less_0_iff)
hoelzl@42844: 
hoelzl@44791: lemma ereal_pos_le_distrib:
hoelzl@44791: fixes a b c :: ereal
hoelzl@42844: assumes "c>=0"
hoelzl@42844: shows "c * (a + b) <= c * a + c * b"
hoelzl@44791:   using assms by (cases rule: ereal3_cases[of a b c])
hoelzl@42844:                  (auto simp add: field_simps)
hoelzl@42844: 
hoelzl@44791: lemma ereal_max_mono:
hoelzl@44791:   "[| (a::ereal) <= b; c <= d |] ==> max a c <= max b d"
hoelzl@44791:   by (metis sup_ereal_def sup_mono)
hoelzl@42844: 
hoelzl@42844: 
hoelzl@44791: lemma ereal_max_least:
hoelzl@44791:   "[| (a::ereal) <= x; c <= x |] ==> max a c <= x"
hoelzl@44791:   by (metis sup_ereal_def sup_least)
hoelzl@42844: 
hoelzl@44804: subsubsection {* Tests for code generator *}
hoelzl@44804: 
hoelzl@44804: (* A small list of simple arithmetic expressions *)
hoelzl@44804: 
hoelzl@44804: value [code] "- \<infinity> :: ereal"
hoelzl@44804: value [code] "\<bar>-\<infinity>\<bar> :: ereal"
hoelzl@44804: value [code] "4 + 5 / 4 - ereal 2 :: ereal"
hoelzl@44804: value [code] "ereal 3 < \<infinity>"
hoelzl@44804: value [code] "real (\<infinity>::ereal) = 0"
hoelzl@44804: 
hoelzl@42844: end




