Merged.
--- a/NEWS Fri Feb 06 15:57:47 2009 +0100
+++ b/NEWS Fri Feb 06 15:59:49 2009 +0100
@@ -197,7 +197,13 @@
are treated as expected by the 'class' command.
* Theory "Reflection" now resides in HOL/Library. Common reflection examples
-(Cooper, MIR, Ferrack) now in distinct session directory HOL/Reflection.
+(Cooper, MIR, Ferrack, Approximation) now in distinct session directory
+HOL/Reflection. Here Approximation provides the new proof method
+"approximation". It proves formulas on real values by using interval arithmetic.
+In the formulas are also the transcendental functions sin, cos, tan, atan, ln,
+exp and the constant pi are allowed. For examples see
+src/HOL/ex/ApproximationEx.thy. To reach this the Leibnitz's Series for Pi and
+the arcus tangens and logarithm series is now proved in Isabelle.
* Entry point to Word library now simply named "Word". INCOMPATIBILITY.
--- a/src/HOL/Dense_Linear_Order.thy Fri Feb 06 15:57:47 2009 +0100
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,879 +0,0 @@
-(* Title : HOL/Dense_Linear_Order.thy
- Author : Amine Chaieb, TU Muenchen
-*)
-
-header {* Dense linear order without endpoints
- and a quantifier elimination procedure in Ferrante and Rackoff style *}
-
-theory Dense_Linear_Order
-imports Plain Groebner_Basis Main
-uses
- "~~/src/HOL/Tools/Qelim/langford_data.ML"
- "~~/src/HOL/Tools/Qelim/ferrante_rackoff_data.ML"
- ("~~/src/HOL/Tools/Qelim/langford.ML")
- ("~~/src/HOL/Tools/Qelim/ferrante_rackoff.ML")
-begin
-
-setup {* Langford_Data.setup #> Ferrante_Rackoff_Data.setup *}
-
-context linorder
-begin
-
-lemma less_not_permute: "\<not> (x < y \<and> y < x)" by (simp add: not_less linear)
-
-lemma gather_simps:
- shows
- "(\<exists>x. (\<forall>y \<in> L. y < x) \<and> (\<forall>y \<in> U. x < y) \<and> x < u \<and> P x) \<longleftrightarrow> (\<exists>x. (\<forall>y \<in> L. y < x) \<and> (\<forall>y \<in> (insert u U). x < y) \<and> P x)"
- and "(\<exists>x. (\<forall>y \<in> L. y < x) \<and> (\<forall>y \<in> U. x < y) \<and> l < x \<and> P x) \<longleftrightarrow> (\<exists>x. (\<forall>y \<in> (insert l L). y < x) \<and> (\<forall>y \<in> U. x < y) \<and> P x)"
- "(\<exists>x. (\<forall>y \<in> L. y < x) \<and> (\<forall>y \<in> U. x < y) \<and> x < u) \<longleftrightarrow> (\<exists>x. (\<forall>y \<in> L. y < x) \<and> (\<forall>y \<in> (insert u U). x < y))"
- and "(\<exists>x. (\<forall>y \<in> L. y < x) \<and> (\<forall>y \<in> U. x < y) \<and> l < x) \<longleftrightarrow> (\<exists>x. (\<forall>y \<in> (insert l L). y < x) \<and> (\<forall>y \<in> U. x < y))" by auto
-
-lemma
- gather_start: "(\<exists>x. P x) \<equiv> (\<exists>x. (\<forall>y \<in> {}. y < x) \<and> (\<forall>y\<in> {}. x < y) \<and> P x)"
- by simp
-
-text{* Theorems for @{text "\<exists>z. \<forall>x. x < z \<longrightarrow> (P x \<longleftrightarrow> P\<^bsub>-\<infinity>\<^esub>)"}*}
-lemma minf_lt: "\<exists>z . \<forall>x. x < z \<longrightarrow> (x < t \<longleftrightarrow> True)" by auto
-lemma minf_gt: "\<exists>z . \<forall>x. x < z \<longrightarrow> (t < x \<longleftrightarrow> False)"
- by (simp add: not_less) (rule exI[where x="t"], auto simp add: less_le)
-
-lemma minf_le: "\<exists>z. \<forall>x. x < z \<longrightarrow> (x \<le> t \<longleftrightarrow> True)" by (auto simp add: less_le)
-lemma minf_ge: "\<exists>z. \<forall>x. x < z \<longrightarrow> (t \<le> x \<longleftrightarrow> False)"
- by (auto simp add: less_le not_less not_le)
-lemma minf_eq: "\<exists>z. \<forall>x. x < z \<longrightarrow> (x = t \<longleftrightarrow> False)" by auto
-lemma minf_neq: "\<exists>z. \<forall>x. x < z \<longrightarrow> (x \<noteq> t \<longleftrightarrow> True)" by auto
-lemma minf_P: "\<exists>z. \<forall>x. x < z \<longrightarrow> (P \<longleftrightarrow> P)" by blast
-
-text{* Theorems for @{text "\<exists>z. \<forall>x. x < z \<longrightarrow> (P x \<longleftrightarrow> P\<^bsub>+\<infinity>\<^esub>)"}*}
-lemma pinf_gt: "\<exists>z . \<forall>x. z < x \<longrightarrow> (t < x \<longleftrightarrow> True)" by auto
-lemma pinf_lt: "\<exists>z . \<forall>x. z < x \<longrightarrow> (x < t \<longleftrightarrow> False)"
- by (simp add: not_less) (rule exI[where x="t"], auto simp add: less_le)
-
-lemma pinf_ge: "\<exists>z. \<forall>x. z < x \<longrightarrow> (t \<le> x \<longleftrightarrow> True)" by (auto simp add: less_le)
-lemma pinf_le: "\<exists>z. \<forall>x. z < x \<longrightarrow> (x \<le> t \<longleftrightarrow> False)"
- by (auto simp add: less_le not_less not_le)
-lemma pinf_eq: "\<exists>z. \<forall>x. z < x \<longrightarrow> (x = t \<longleftrightarrow> False)" by auto
-lemma pinf_neq: "\<exists>z. \<forall>x. z < x \<longrightarrow> (x \<noteq> t \<longleftrightarrow> True)" by auto
-lemma pinf_P: "\<exists>z. \<forall>x. z < x \<longrightarrow> (P \<longleftrightarrow> P)" by blast
-
-lemma nmi_lt: "t \<in> U \<Longrightarrow> \<forall>x. \<not>True \<and> x < t \<longrightarrow> (\<exists> u\<in> U. u \<le> x)" by auto
-lemma nmi_gt: "t \<in> U \<Longrightarrow> \<forall>x. \<not>False \<and> t < x \<longrightarrow> (\<exists> u\<in> U. u \<le> x)"
- by (auto simp add: le_less)
-lemma nmi_le: "t \<in> U \<Longrightarrow> \<forall>x. \<not>True \<and> x\<le> t \<longrightarrow> (\<exists> u\<in> U. u \<le> x)" by auto
-lemma nmi_ge: "t \<in> U \<Longrightarrow> \<forall>x. \<not>False \<and> t\<le> x \<longrightarrow> (\<exists> u\<in> U. u \<le> x)" by auto
-lemma nmi_eq: "t \<in> U \<Longrightarrow> \<forall>x. \<not>False \<and> x = t \<longrightarrow> (\<exists> u\<in> U. u \<le> x)" by auto
-lemma nmi_neq: "t \<in> U \<Longrightarrow>\<forall>x. \<not>True \<and> x \<noteq> t \<longrightarrow> (\<exists> u\<in> U. u \<le> x)" by auto
-lemma nmi_P: "\<forall> x. ~P \<and> P \<longrightarrow> (\<exists> u\<in> U. u \<le> x)" by auto
-lemma nmi_conj: "\<lbrakk>\<forall>x. \<not>P1' \<and> P1 x \<longrightarrow> (\<exists> u\<in> U. u \<le> x) ;
- \<forall>x. \<not>P2' \<and> P2 x \<longrightarrow> (\<exists> u\<in> U. u \<le> x)\<rbrakk> \<Longrightarrow>
- \<forall>x. \<not>(P1' \<and> P2') \<and> (P1 x \<and> P2 x) \<longrightarrow> (\<exists> u\<in> U. u \<le> x)" by auto
-lemma nmi_disj: "\<lbrakk>\<forall>x. \<not>P1' \<and> P1 x \<longrightarrow> (\<exists> u\<in> U. u \<le> x) ;
- \<forall>x. \<not>P2' \<and> P2 x \<longrightarrow> (\<exists> u\<in> U. u \<le> x)\<rbrakk> \<Longrightarrow>
- \<forall>x. \<not>(P1' \<or> P2') \<and> (P1 x \<or> P2 x) \<longrightarrow> (\<exists> u\<in> U. u \<le> x)" by auto
-
-lemma npi_lt: "t \<in> U \<Longrightarrow> \<forall>x. \<not>False \<and> x < t \<longrightarrow> (\<exists> u\<in> U. x \<le> u)" by (auto simp add: le_less)
-lemma npi_gt: "t \<in> U \<Longrightarrow> \<forall>x. \<not>True \<and> t < x \<longrightarrow> (\<exists> u\<in> U. x \<le> u)" by auto
-lemma npi_le: "t \<in> U \<Longrightarrow> \<forall>x. \<not>False \<and> x \<le> t \<longrightarrow> (\<exists> u\<in> U. x \<le> u)" by auto
-lemma npi_ge: "t \<in> U \<Longrightarrow> \<forall>x. \<not>True \<and> t \<le> x \<longrightarrow> (\<exists> u\<in> U. x \<le> u)" by auto
-lemma npi_eq: "t \<in> U \<Longrightarrow> \<forall>x. \<not>False \<and> x = t \<longrightarrow> (\<exists> u\<in> U. x \<le> u)" by auto
-lemma npi_neq: "t \<in> U \<Longrightarrow> \<forall>x. \<not>True \<and> x \<noteq> t \<longrightarrow> (\<exists> u\<in> U. x \<le> u )" by auto
-lemma npi_P: "\<forall> x. ~P \<and> P \<longrightarrow> (\<exists> u\<in> U. x \<le> u)" by auto
-lemma npi_conj: "\<lbrakk>\<forall>x. \<not>P1' \<and> P1 x \<longrightarrow> (\<exists> u\<in> U. x \<le> u) ; \<forall>x. \<not>P2' \<and> P2 x \<longrightarrow> (\<exists> u\<in> U. x \<le> u)\<rbrakk>
- \<Longrightarrow> \<forall>x. \<not>(P1' \<and> P2') \<and> (P1 x \<and> P2 x) \<longrightarrow> (\<exists> u\<in> U. x \<le> u)" by auto
-lemma npi_disj: "\<lbrakk>\<forall>x. \<not>P1' \<and> P1 x \<longrightarrow> (\<exists> u\<in> U. x \<le> u) ; \<forall>x. \<not>P2' \<and> P2 x \<longrightarrow> (\<exists> u\<in> U. x \<le> u)\<rbrakk>
- \<Longrightarrow> \<forall>x. \<not>(P1' \<or> P2') \<and> (P1 x \<or> P2 x) \<longrightarrow> (\<exists> u\<in> U. x \<le> u)" by auto
-
-lemma lin_dense_lt: "t \<in> U \<Longrightarrow> \<forall>x l u. (\<forall> t. l < t \<and> t < u \<longrightarrow> t \<notin> U) \<and> l< x \<and> x < u \<and> x < t \<longrightarrow> (\<forall> y. l < y \<and> y < u \<longrightarrow> y < t)"
-proof(clarsimp)
- fix x l u y assume tU: "t \<in> U" and noU: "\<forall>t. l < t \<and> t < u \<longrightarrow> t \<notin> U" and lx: "l < x"
- and xu: "x<u" and px: "x < t" and ly: "l<y" and yu:"y < u"
- from tU noU ly yu have tny: "t\<noteq>y" by auto
- {assume H: "t < y"
- from less_trans[OF lx px] less_trans[OF H yu]
- have "l < t \<and> t < u" by simp
- with tU noU have "False" by auto}
- hence "\<not> t < y" by auto hence "y \<le> t" by (simp add: not_less)
- thus "y < t" using tny by (simp add: less_le)
-qed
-
-lemma lin_dense_gt: "t \<in> U \<Longrightarrow> \<forall>x l u. (\<forall> t. l < t \<and> t< u \<longrightarrow> t \<notin> U) \<and> l < x \<and> x < u \<and> t < x \<longrightarrow> (\<forall> y. l < y \<and> y < u \<longrightarrow> t < y)"
-proof(clarsimp)
- fix x l u y
- assume tU: "t \<in> U" and noU: "\<forall>t. l < t \<and> t < u \<longrightarrow> t \<notin> U" and lx: "l < x" and xu: "x<u"
- and px: "t < x" and ly: "l<y" and yu:"y < u"
- from tU noU ly yu have tny: "t\<noteq>y" by auto
- {assume H: "y< t"
- from less_trans[OF ly H] less_trans[OF px xu] have "l < t \<and> t < u" by simp
- with tU noU have "False" by auto}
- hence "\<not> y<t" by auto hence "t \<le> y" by (auto simp add: not_less)
- thus "t < y" using tny by (simp add:less_le)
-qed
-
-lemma lin_dense_le: "t \<in> U \<Longrightarrow> \<forall>x l u. (\<forall> t. l < t \<and> t< u \<longrightarrow> t \<notin> U) \<and> l< x \<and> x < u \<and> x \<le> t \<longrightarrow> (\<forall> y. l < y \<and> y < u \<longrightarrow> y\<le> t)"
-proof(clarsimp)
- fix x l u y
- assume tU: "t \<in> U" and noU: "\<forall>t. l < t \<and> t < u \<longrightarrow> t \<notin> U" and lx: "l < x" and xu: "x<u"
- and px: "x \<le> t" and ly: "l<y" and yu:"y < u"
- from tU noU ly yu have tny: "t\<noteq>y" by auto
- {assume H: "t < y"
- from less_le_trans[OF lx px] less_trans[OF H yu]
- have "l < t \<and> t < u" by simp
- with tU noU have "False" by auto}
- hence "\<not> t < y" by auto thus "y \<le> t" by (simp add: not_less)
-qed
-
-lemma lin_dense_ge: "t \<in> U \<Longrightarrow> \<forall>x l u. (\<forall> t. l < t \<and> t< u \<longrightarrow> t \<notin> U) \<and> l< x \<and> x < u \<and> t \<le> x \<longrightarrow> (\<forall> y. l < y \<and> y < u \<longrightarrow> t \<le> y)"
-proof(clarsimp)
- fix x l u y
- assume tU: "t \<in> U" and noU: "\<forall>t. l < t \<and> t < u \<longrightarrow> t \<notin> U" and lx: "l < x" and xu: "x<u"
- and px: "t \<le> x" and ly: "l<y" and yu:"y < u"
- from tU noU ly yu have tny: "t\<noteq>y" by auto
- {assume H: "y< t"
- from less_trans[OF ly H] le_less_trans[OF px xu]
- have "l < t \<and> t < u" by simp
- with tU noU have "False" by auto}
- hence "\<not> y<t" by auto thus "t \<le> y" by (simp add: not_less)
-qed
-lemma lin_dense_eq: "t \<in> U \<Longrightarrow> \<forall>x l u. (\<forall> t. l < t \<and> t< u \<longrightarrow> t \<notin> U) \<and> l< x \<and> x < u \<and> x = t \<longrightarrow> (\<forall> y. l < y \<and> y < u \<longrightarrow> y= t)" by auto
-lemma lin_dense_neq: "t \<in> U \<Longrightarrow> \<forall>x l u. (\<forall> t. l < t \<and> t< u \<longrightarrow> t \<notin> U) \<and> l< x \<and> x < u \<and> x \<noteq> t \<longrightarrow> (\<forall> y. l < y \<and> y < u \<longrightarrow> y\<noteq> t)" by auto
-lemma lin_dense_P: "\<forall>x l u. (\<forall> t. l < t \<and> t< u \<longrightarrow> t \<notin> U) \<and> l< x \<and> x < u \<and> P \<longrightarrow> (\<forall> y. l < y \<and> y < u \<longrightarrow> P)" by auto
-
-lemma lin_dense_conj:
- "\<lbrakk>\<forall>x l u. (\<forall> t. l < t \<and> t< u \<longrightarrow> t \<notin> U) \<and> l< x \<and> x < u \<and> P1 x
- \<longrightarrow> (\<forall> y. l < y \<and> y < u \<longrightarrow> P1 y) ;
- \<forall>x l u. (\<forall> t. l < t \<and> t< u \<longrightarrow> t \<notin> U) \<and> l< x \<and> x < u \<and> P2 x
- \<longrightarrow> (\<forall> y. l < y \<and> y < u \<longrightarrow> P2 y)\<rbrakk> \<Longrightarrow>
- \<forall>x l u. (\<forall> t. l < t \<and> t< u \<longrightarrow> t \<notin> U) \<and> l< x \<and> x < u \<and> (P1 x \<and> P2 x)
- \<longrightarrow> (\<forall> y. l < y \<and> y < u \<longrightarrow> (P1 y \<and> P2 y))"
- by blast
-lemma lin_dense_disj:
- "\<lbrakk>\<forall>x l u. (\<forall> t. l < t \<and> t< u \<longrightarrow> t \<notin> U) \<and> l< x \<and> x < u \<and> P1 x
- \<longrightarrow> (\<forall> y. l < y \<and> y < u \<longrightarrow> P1 y) ;
- \<forall>x l u. (\<forall> t. l < t \<and> t< u \<longrightarrow> t \<notin> U) \<and> l< x \<and> x < u \<and> P2 x
- \<longrightarrow> (\<forall> y. l < y \<and> y < u \<longrightarrow> P2 y)\<rbrakk> \<Longrightarrow>
- \<forall>x l u. (\<forall> t. l < t \<and> t< u \<longrightarrow> t \<notin> U) \<and> l< x \<and> x < u \<and> (P1 x \<or> P2 x)
- \<longrightarrow> (\<forall> y. l < y \<and> y < u \<longrightarrow> (P1 y \<or> P2 y))"
- by blast
-
-lemma npmibnd: "\<lbrakk>\<forall>x. \<not> MP \<and> P x \<longrightarrow> (\<exists> u\<in> U. u \<le> x); \<forall>x. \<not>PP \<and> P x \<longrightarrow> (\<exists> u\<in> U. x \<le> u)\<rbrakk>
- \<Longrightarrow> \<forall>x. \<not> MP \<and> \<not>PP \<and> P x \<longrightarrow> (\<exists> u\<in> U. \<exists> u' \<in> U. u \<le> x \<and> x \<le> u')"
-by auto
-
-lemma finite_set_intervals:
- assumes px: "P x" and lx: "l \<le> x" and xu: "x \<le> u" and linS: "l\<in> S"
- and uinS: "u \<in> S" and fS:"finite S" and lS: "\<forall> x\<in> S. l \<le> x" and Su: "\<forall> x\<in> S. x \<le> u"
- shows "\<exists> a \<in> S. \<exists> b \<in> S. (\<forall> y. a < y \<and> y < b \<longrightarrow> y \<notin> S) \<and> a \<le> x \<and> x \<le> b \<and> P x"
-proof-
- let ?Mx = "{y. y\<in> S \<and> y \<le> x}"
- let ?xM = "{y. y\<in> S \<and> x \<le> y}"
- let ?a = "Max ?Mx"
- let ?b = "Min ?xM"
- have MxS: "?Mx \<subseteq> S" by blast
- hence fMx: "finite ?Mx" using fS finite_subset by auto
- from lx linS have linMx: "l \<in> ?Mx" by blast
- hence Mxne: "?Mx \<noteq> {}" by blast
- have xMS: "?xM \<subseteq> S" by blast
- hence fxM: "finite ?xM" using fS finite_subset by auto
- from xu uinS have linxM: "u \<in> ?xM" by blast
- hence xMne: "?xM \<noteq> {}" by blast
- have ax:"?a \<le> x" using Mxne fMx by auto
- have xb:"x \<le> ?b" using xMne fxM by auto
- have "?a \<in> ?Mx" using Max_in[OF fMx Mxne] by simp hence ainS: "?a \<in> S" using MxS by blast
- have "?b \<in> ?xM" using Min_in[OF fxM xMne] by simp hence binS: "?b \<in> S" using xMS by blast
- have noy:"\<forall> y. ?a < y \<and> y < ?b \<longrightarrow> y \<notin> S"
- proof(clarsimp)
- fix y assume ay: "?a < y" and yb: "y < ?b" and yS: "y \<in> S"
- from yS have "y\<in> ?Mx \<or> y\<in> ?xM" by (auto simp add: linear)
- moreover {assume "y \<in> ?Mx" hence "y \<le> ?a" using Mxne fMx by auto with ay have "False" by (simp add: not_le[symmetric])}
- moreover {assume "y \<in> ?xM" hence "?b \<le> y" using xMne fxM by auto with yb have "False" by (simp add: not_le[symmetric])}
- ultimately show "False" by blast
- qed
- from ainS binS noy ax xb px show ?thesis by blast
-qed
-
-lemma finite_set_intervals2:
- assumes px: "P x" and lx: "l \<le> x" and xu: "x \<le> u" and linS: "l\<in> S"
- and uinS: "u \<in> S" and fS:"finite S" and lS: "\<forall> x\<in> S. l \<le> x" and Su: "\<forall> x\<in> S. x \<le> u"
- shows "(\<exists> s\<in> S. P s) \<or> (\<exists> a \<in> S. \<exists> b \<in> S. (\<forall> y. a < y \<and> y < b \<longrightarrow> y \<notin> S) \<and> a < x \<and> x < b \<and> P x)"
-proof-
- from finite_set_intervals[where P="P", OF px lx xu linS uinS fS lS Su]
- obtain a and b where
- as: "a\<in> S" and bs: "b\<in> S" and noS:"\<forall>y. a < y \<and> y < b \<longrightarrow> y \<notin> S"
- and axb: "a \<le> x \<and> x \<le> b \<and> P x" by auto
- from axb have "x= a \<or> x= b \<or> (a < x \<and> x < b)" by (auto simp add: le_less)
- thus ?thesis using px as bs noS by blast
-qed
-
-end
-
-section {* The classical QE after Langford for dense linear orders *}
-
-context dense_linear_order
-begin
-
-lemma interval_empty_iff:
- "{y. x < y \<and> y < z} = {} \<longleftrightarrow> \<not> x < z"
- by (auto dest: dense)
-
-lemma dlo_qe_bnds:
- assumes ne: "L \<noteq> {}" and neU: "U \<noteq> {}" and fL: "finite L" and fU: "finite U"
- shows "(\<exists>x. (\<forall>y \<in> L. y < x) \<and> (\<forall>y \<in> U. x < y)) \<equiv> (\<forall> l \<in> L. \<forall>u \<in> U. l < u)"
-proof (simp only: atomize_eq, rule iffI)
- assume H: "\<exists>x. (\<forall>y\<in>L. y < x) \<and> (\<forall>y\<in>U. x < y)"
- then obtain x where xL: "\<forall>y\<in>L. y < x" and xU: "\<forall>y\<in>U. x < y" by blast
- {fix l u assume l: "l \<in> L" and u: "u \<in> U"
- have "l < x" using xL l by blast
- also have "x < u" using xU u by blast
- finally (less_trans) have "l < u" .}
- thus "\<forall>l\<in>L. \<forall>u\<in>U. l < u" by blast
-next
- assume H: "\<forall>l\<in>L. \<forall>u\<in>U. l < u"
- let ?ML = "Max L"
- let ?MU = "Min U"
- from fL ne have th1: "?ML \<in> L" and th1': "\<forall>l\<in>L. l \<le> ?ML" by auto
- from fU neU have th2: "?MU \<in> U" and th2': "\<forall>u\<in>U. ?MU \<le> u" by auto
- from th1 th2 H have "?ML < ?MU" by auto
- with dense obtain w where th3: "?ML < w" and th4: "w < ?MU" by blast
- from th3 th1' have "\<forall>l \<in> L. l < w" by auto
- moreover from th4 th2' have "\<forall>u \<in> U. w < u" by auto
- ultimately show "\<exists>x. (\<forall>y\<in>L. y < x) \<and> (\<forall>y\<in>U. x < y)" by auto
-qed
-
-lemma dlo_qe_noub:
- assumes ne: "L \<noteq> {}" and fL: "finite L"
- shows "(\<exists>x. (\<forall>y \<in> L. y < x) \<and> (\<forall>y \<in> {}. x < y)) \<equiv> True"
-proof(simp add: atomize_eq)
- from gt_ex[of "Max L"] obtain M where M: "Max L < M" by blast
- from ne fL have "\<forall>x \<in> L. x \<le> Max L" by simp
- with M have "\<forall>x\<in>L. x < M" by (auto intro: le_less_trans)
- thus "\<exists>x. \<forall>y\<in>L. y < x" by blast
-qed
-
-lemma dlo_qe_nolb:
- assumes ne: "U \<noteq> {}" and fU: "finite U"
- shows "(\<exists>x. (\<forall>y \<in> {}. y < x) \<and> (\<forall>y \<in> U. x < y)) \<equiv> True"
-proof(simp add: atomize_eq)
- from lt_ex[of "Min U"] obtain M where M: "M < Min U" by blast
- from ne fU have "\<forall>x \<in> U. Min U \<le> x" by simp
- with M have "\<forall>x\<in>U. M < x" by (auto intro: less_le_trans)
- thus "\<exists>x. \<forall>y\<in>U. x < y" by blast
-qed
-
-lemma exists_neq: "\<exists>(x::'a). x \<noteq> t" "\<exists>(x::'a). t \<noteq> x"
- using gt_ex[of t] by auto
-
-lemmas dlo_simps = order_refl less_irrefl not_less not_le exists_neq
- le_less neq_iff linear less_not_permute
-
-lemma axiom: "dense_linear_order (op \<le>) (op <)" by (rule dense_linear_order_axioms)
-lemma atoms:
- shows "TERM (less :: 'a \<Rightarrow> _)"
- and "TERM (less_eq :: 'a \<Rightarrow> _)"
- and "TERM (op = :: 'a \<Rightarrow> _)" .
-
-declare axiom[langford qe: dlo_qe_bnds dlo_qe_nolb dlo_qe_noub gather: gather_start gather_simps atoms: atoms]
-declare dlo_simps[langfordsimp]
-
-end
-
-(* FIXME: Move to HOL -- together with the conj_aci_rule in langford.ML *)
-lemma dnf:
- "(P & (Q | R)) = ((P&Q) | (P&R))"
- "((Q | R) & P) = ((Q&P) | (R&P))"
- by blast+
-
-lemmas weak_dnf_simps = simp_thms dnf
-
-lemma nnf_simps:
- "(\<not>(P \<and> Q)) = (\<not>P \<or> \<not>Q)" "(\<not>(P \<or> Q)) = (\<not>P \<and> \<not>Q)" "(P \<longrightarrow> Q) = (\<not>P \<or> Q)"
- "(P = Q) = ((P \<and> Q) \<or> (\<not>P \<and> \<not> Q))" "(\<not> \<not>(P)) = P"
- by blast+
-
-lemma ex_distrib: "(\<exists>x. P x \<or> Q x) \<longleftrightarrow> ((\<exists>x. P x) \<or> (\<exists>x. Q x))" by blast
-
-lemmas dnf_simps = weak_dnf_simps nnf_simps ex_distrib
-
-use "~~/src/HOL/Tools/Qelim/langford.ML"
-method_setup dlo = {*
- Method.ctxt_args (Method.SIMPLE_METHOD' o LangfordQE.dlo_tac)
-*} "Langford's algorithm for quantifier elimination in dense linear orders"
-
-
-section {* Contructive dense linear orders yield QE for linear arithmetic over ordered Fields -- see @{text "Arith_Tools.thy"} *}
-
-text {* Linear order without upper bounds *}
-
-locale linorder_stupid_syntax = linorder
-begin
-notation
- less_eq ("op \<sqsubseteq>") and
- less_eq ("(_/ \<sqsubseteq> _)" [51, 51] 50) and
- less ("op \<sqsubset>") and
- less ("(_/ \<sqsubset> _)" [51, 51] 50)
-
-end
-
-locale linorder_no_ub = linorder_stupid_syntax +
- assumes gt_ex: "\<exists>y. less x y"
-begin
-lemma ge_ex: "\<exists>y. x \<sqsubseteq> y" using gt_ex by auto
-
-text {* Theorems for @{text "\<exists>z. \<forall>x. z \<sqsubset> x \<longrightarrow> (P x \<longleftrightarrow> P\<^bsub>+\<infinity>\<^esub>)"} *}
-lemma pinf_conj:
- assumes ex1: "\<exists>z1. \<forall>x. z1 \<sqsubset> x \<longrightarrow> (P1 x \<longleftrightarrow> P1')"
- and ex2: "\<exists>z2. \<forall>x. z2 \<sqsubset> x \<longrightarrow> (P2 x \<longleftrightarrow> P2')"
- shows "\<exists>z. \<forall>x. z \<sqsubset> x \<longrightarrow> ((P1 x \<and> P2 x) \<longleftrightarrow> (P1' \<and> P2'))"
-proof-
- from ex1 ex2 obtain z1 and z2 where z1: "\<forall>x. z1 \<sqsubset> x \<longrightarrow> (P1 x \<longleftrightarrow> P1')"
- and z2: "\<forall>x. z2 \<sqsubset> x \<longrightarrow> (P2 x \<longleftrightarrow> P2')" by blast
- from gt_ex obtain z where z:"ord.max less_eq z1 z2 \<sqsubset> z" by blast
- from z have zz1: "z1 \<sqsubset> z" and zz2: "z2 \<sqsubset> z" by simp_all
- {fix x assume H: "z \<sqsubset> x"
- from less_trans[OF zz1 H] less_trans[OF zz2 H]
- have "(P1 x \<and> P2 x) \<longleftrightarrow> (P1' \<and> P2')" using z1 zz1 z2 zz2 by auto
- }
- thus ?thesis by blast
-qed
-
-lemma pinf_disj:
- assumes ex1: "\<exists>z1. \<forall>x. z1 \<sqsubset> x \<longrightarrow> (P1 x \<longleftrightarrow> P1')"
- and ex2: "\<exists>z2. \<forall>x. z2 \<sqsubset> x \<longrightarrow> (P2 x \<longleftrightarrow> P2')"
- shows "\<exists>z. \<forall>x. z \<sqsubset> x \<longrightarrow> ((P1 x \<or> P2 x) \<longleftrightarrow> (P1' \<or> P2'))"
-proof-
- from ex1 ex2 obtain z1 and z2 where z1: "\<forall>x. z1 \<sqsubset> x \<longrightarrow> (P1 x \<longleftrightarrow> P1')"
- and z2: "\<forall>x. z2 \<sqsubset> x \<longrightarrow> (P2 x \<longleftrightarrow> P2')" by blast
- from gt_ex obtain z where z:"ord.max less_eq z1 z2 \<sqsubset> z" by blast
- from z have zz1: "z1 \<sqsubset> z" and zz2: "z2 \<sqsubset> z" by simp_all
- {fix x assume H: "z \<sqsubset> x"
- from less_trans[OF zz1 H] less_trans[OF zz2 H]
- have "(P1 x \<or> P2 x) \<longleftrightarrow> (P1' \<or> P2')" using z1 zz1 z2 zz2 by auto
- }
- thus ?thesis by blast
-qed
-
-lemma pinf_ex: assumes ex:"\<exists>z. \<forall>x. z \<sqsubset> x \<longrightarrow> (P x \<longleftrightarrow> P1)" and p1: P1 shows "\<exists> x. P x"
-proof-
- from ex obtain z where z: "\<forall>x. z \<sqsubset> x \<longrightarrow> (P x \<longleftrightarrow> P1)" by blast
- from gt_ex obtain x where x: "z \<sqsubset> x" by blast
- from z x p1 show ?thesis by blast
-qed
-
-end
-
-text {* Linear order without upper bounds *}
-
-locale linorder_no_lb = linorder_stupid_syntax +
- assumes lt_ex: "\<exists>y. less y x"
-begin
-lemma le_ex: "\<exists>y. y \<sqsubseteq> x" using lt_ex by auto
-
-
-text {* Theorems for @{text "\<exists>z. \<forall>x. x \<sqsubset> z \<longrightarrow> (P x \<longleftrightarrow> P\<^bsub>-\<infinity>\<^esub>)"} *}
-lemma minf_conj:
- assumes ex1: "\<exists>z1. \<forall>x. x \<sqsubset> z1 \<longrightarrow> (P1 x \<longleftrightarrow> P1')"
- and ex2: "\<exists>z2. \<forall>x. x \<sqsubset> z2 \<longrightarrow> (P2 x \<longleftrightarrow> P2')"
- shows "\<exists>z. \<forall>x. x \<sqsubset> z \<longrightarrow> ((P1 x \<and> P2 x) \<longleftrightarrow> (P1' \<and> P2'))"
-proof-
- from ex1 ex2 obtain z1 and z2 where z1: "\<forall>x. x \<sqsubset> z1 \<longrightarrow> (P1 x \<longleftrightarrow> P1')"and z2: "\<forall>x. x \<sqsubset> z2 \<longrightarrow> (P2 x \<longleftrightarrow> P2')" by blast
- from lt_ex obtain z where z:"z \<sqsubset> ord.min less_eq z1 z2" by blast
- from z have zz1: "z \<sqsubset> z1" and zz2: "z \<sqsubset> z2" by simp_all
- {fix x assume H: "x \<sqsubset> z"
- from less_trans[OF H zz1] less_trans[OF H zz2]
- have "(P1 x \<and> P2 x) \<longleftrightarrow> (P1' \<and> P2')" using z1 zz1 z2 zz2 by auto
- }
- thus ?thesis by blast
-qed
-
-lemma minf_disj:
- assumes ex1: "\<exists>z1. \<forall>x. x \<sqsubset> z1 \<longrightarrow> (P1 x \<longleftrightarrow> P1')"
- and ex2: "\<exists>z2. \<forall>x. x \<sqsubset> z2 \<longrightarrow> (P2 x \<longleftrightarrow> P2')"
- shows "\<exists>z. \<forall>x. x \<sqsubset> z \<longrightarrow> ((P1 x \<or> P2 x) \<longleftrightarrow> (P1' \<or> P2'))"
-proof-
- from ex1 ex2 obtain z1 and z2 where z1: "\<forall>x. x \<sqsubset> z1 \<longrightarrow> (P1 x \<longleftrightarrow> P1')"and z2: "\<forall>x. x \<sqsubset> z2 \<longrightarrow> (P2 x \<longleftrightarrow> P2')" by blast
- from lt_ex obtain z where z:"z \<sqsubset> ord.min less_eq z1 z2" by blast
- from z have zz1: "z \<sqsubset> z1" and zz2: "z \<sqsubset> z2" by simp_all
- {fix x assume H: "x \<sqsubset> z"
- from less_trans[OF H zz1] less_trans[OF H zz2]
- have "(P1 x \<or> P2 x) \<longleftrightarrow> (P1' \<or> P2')" using z1 zz1 z2 zz2 by auto
- }
- thus ?thesis by blast
-qed
-
-lemma minf_ex: assumes ex:"\<exists>z. \<forall>x. x \<sqsubset> z \<longrightarrow> (P x \<longleftrightarrow> P1)" and p1: P1 shows "\<exists> x. P x"
-proof-
- from ex obtain z where z: "\<forall>x. x \<sqsubset> z \<longrightarrow> (P x \<longleftrightarrow> P1)" by blast
- from lt_ex obtain x where x: "x \<sqsubset> z" by blast
- from z x p1 show ?thesis by blast
-qed
-
-end
-
-
-locale constr_dense_linear_order = linorder_no_lb + linorder_no_ub +
- fixes between
- assumes between_less: "less x y \<Longrightarrow> less x (between x y) \<and> less (between x y) y"
- and between_same: "between x x = x"
-
-sublocale constr_dense_linear_order < dense_linear_order
- apply unfold_locales
- using gt_ex lt_ex between_less
- by (auto, rule_tac x="between x y" in exI, simp)
-
-context constr_dense_linear_order
-begin
-
-lemma rinf_U:
- assumes fU: "finite U"
- and lin_dense: "\<forall>x l u. (\<forall> t. l \<sqsubset> t \<and> t\<sqsubset> u \<longrightarrow> t \<notin> U) \<and> l\<sqsubset> x \<and> x \<sqsubset> u \<and> P x
- \<longrightarrow> (\<forall> y. l \<sqsubset> y \<and> y \<sqsubset> u \<longrightarrow> P y )"
- and nmpiU: "\<forall>x. \<not> MP \<and> \<not>PP \<and> P x \<longrightarrow> (\<exists> u\<in> U. \<exists> u' \<in> U. u \<sqsubseteq> x \<and> x \<sqsubseteq> u')"
- and nmi: "\<not> MP" and npi: "\<not> PP" and ex: "\<exists> x. P x"
- shows "\<exists> u\<in> U. \<exists> u' \<in> U. P (between u u')"
-proof-
- from ex obtain x where px: "P x" by blast
- from px nmi npi nmpiU have "\<exists> u\<in> U. \<exists> u' \<in> U. u \<sqsubseteq> x \<and> x \<sqsubseteq> u'" by auto
- then obtain u and u' where uU:"u\<in> U" and uU': "u' \<in> U" and ux:"u \<sqsubseteq> x" and xu':"x \<sqsubseteq> u'" by auto
- from uU have Une: "U \<noteq> {}" by auto
- term "linorder.Min less_eq"
- let ?l = "linorder.Min less_eq U"
- let ?u = "linorder.Max less_eq U"
- have linM: "?l \<in> U" using fU Une by simp
- have uinM: "?u \<in> U" using fU Une by simp
- have lM: "\<forall> t\<in> U. ?l \<sqsubseteq> t" using Une fU by auto
- have Mu: "\<forall> t\<in> U. t \<sqsubseteq> ?u" using Une fU by auto
- have th:"?l \<sqsubseteq> u" using uU Une lM by auto
- from order_trans[OF th ux] have lx: "?l \<sqsubseteq> x" .
- have th: "u' \<sqsubseteq> ?u" using uU' Une Mu by simp
- from order_trans[OF xu' th] have xu: "x \<sqsubseteq> ?u" .
- from finite_set_intervals2[where P="P",OF px lx xu linM uinM fU lM Mu]
- have "(\<exists> s\<in> U. P s) \<or>
- (\<exists> t1\<in> U. \<exists> t2 \<in> U. (\<forall> y. t1 \<sqsubset> y \<and> y \<sqsubset> t2 \<longrightarrow> y \<notin> U) \<and> t1 \<sqsubset> x \<and> x \<sqsubset> t2 \<and> P x)" .
- moreover { fix u assume um: "u\<in>U" and pu: "P u"
- have "between u u = u" by (simp add: between_same)
- with um pu have "P (between u u)" by simp
- with um have ?thesis by blast}
- moreover{
- assume "\<exists> t1\<in> U. \<exists> t2 \<in> U. (\<forall> y. t1 \<sqsubset> y \<and> y \<sqsubset> t2 \<longrightarrow> y \<notin> U) \<and> t1 \<sqsubset> x \<and> x \<sqsubset> t2 \<and> P x"
- then obtain t1 and t2 where t1M: "t1 \<in> U" and t2M: "t2\<in> U"
- and noM: "\<forall> y. t1 \<sqsubset> y \<and> y \<sqsubset> t2 \<longrightarrow> y \<notin> U" and t1x: "t1 \<sqsubset> x" and xt2: "x \<sqsubset> t2" and px: "P x"
- by blast
- from less_trans[OF t1x xt2] have t1t2: "t1 \<sqsubset> t2" .
- let ?u = "between t1 t2"
- from between_less t1t2 have t1lu: "t1 \<sqsubset> ?u" and ut2: "?u \<sqsubset> t2" by auto
- from lin_dense noM t1x xt2 px t1lu ut2 have "P ?u" by blast
- with t1M t2M have ?thesis by blast}
- ultimately show ?thesis by blast
- qed
-
-theorem fr_eq:
- assumes fU: "finite U"
- and lin_dense: "\<forall>x l u. (\<forall> t. l \<sqsubset> t \<and> t\<sqsubset> u \<longrightarrow> t \<notin> U) \<and> l\<sqsubset> x \<and> x \<sqsubset> u \<and> P x
- \<longrightarrow> (\<forall> y. l \<sqsubset> y \<and> y \<sqsubset> u \<longrightarrow> P y )"
- and nmibnd: "\<forall>x. \<not> MP \<and> P x \<longrightarrow> (\<exists> u\<in> U. u \<sqsubseteq> x)"
- and npibnd: "\<forall>x. \<not>PP \<and> P x \<longrightarrow> (\<exists> u\<in> U. x \<sqsubseteq> u)"
- and mi: "\<exists>z. \<forall>x. x \<sqsubset> z \<longrightarrow> (P x = MP)" and pi: "\<exists>z. \<forall>x. z \<sqsubset> x \<longrightarrow> (P x = PP)"
- shows "(\<exists> x. P x) \<equiv> (MP \<or> PP \<or> (\<exists> u \<in> U. \<exists> u'\<in> U. P (between u u')))"
- (is "_ \<equiv> (_ \<or> _ \<or> ?F)" is "?E \<equiv> ?D")
-proof-
- {
- assume px: "\<exists> x. P x"
- have "MP \<or> PP \<or> (\<not> MP \<and> \<not> PP)" by blast
- moreover {assume "MP \<or> PP" hence "?D" by blast}
- moreover {assume nmi: "\<not> MP" and npi: "\<not> PP"
- from npmibnd[OF nmibnd npibnd]
- have nmpiU: "\<forall>x. \<not> MP \<and> \<not>PP \<and> P x \<longrightarrow> (\<exists> u\<in> U. \<exists> u' \<in> U. u \<sqsubseteq> x \<and> x \<sqsubseteq> u')" .
- from rinf_U[OF fU lin_dense nmpiU nmi npi px] have "?D" by blast}
- ultimately have "?D" by blast}
- moreover
- { assume "?D"
- moreover {assume m:"MP" from minf_ex[OF mi m] have "?E" .}
- moreover {assume p: "PP" from pinf_ex[OF pi p] have "?E" . }
- moreover {assume f:"?F" hence "?E" by blast}
- ultimately have "?E" by blast}
- ultimately have "?E = ?D" by blast thus "?E \<equiv> ?D" by simp
-qed
-
-lemmas minf_thms = minf_conj minf_disj minf_eq minf_neq minf_lt minf_le minf_gt minf_ge minf_P
-lemmas pinf_thms = pinf_conj pinf_disj pinf_eq pinf_neq pinf_lt pinf_le pinf_gt pinf_ge pinf_P
-
-lemmas nmi_thms = nmi_conj nmi_disj nmi_eq nmi_neq nmi_lt nmi_le nmi_gt nmi_ge nmi_P
-lemmas npi_thms = npi_conj npi_disj npi_eq npi_neq npi_lt npi_le npi_gt npi_ge npi_P
-lemmas lin_dense_thms = lin_dense_conj lin_dense_disj lin_dense_eq lin_dense_neq lin_dense_lt lin_dense_le lin_dense_gt lin_dense_ge lin_dense_P
-
-lemma ferrack_axiom: "constr_dense_linear_order less_eq less between"
- by (rule constr_dense_linear_order_axioms)
-lemma atoms:
- shows "TERM (less :: 'a \<Rightarrow> _)"
- and "TERM (less_eq :: 'a \<Rightarrow> _)"
- and "TERM (op = :: 'a \<Rightarrow> _)" .
-
-declare ferrack_axiom [ferrack minf: minf_thms pinf: pinf_thms
- nmi: nmi_thms npi: npi_thms lindense:
- lin_dense_thms qe: fr_eq atoms: atoms]
-
-declaration {*
-let
-fun simps phi = map (Morphism.thm phi) [@{thm "not_less"}, @{thm "not_le"}]
-fun generic_whatis phi =
- let
- val [lt, le] = map (Morphism.term phi) [@{term "op \<sqsubset>"}, @{term "op \<sqsubseteq>"}]
- fun h x t =
- case term_of t of
- Const("op =", _)$y$z => if term_of x aconv y then Ferrante_Rackoff_Data.Eq
- else Ferrante_Rackoff_Data.Nox
- | @{term "Not"}$(Const("op =", _)$y$z) => if term_of x aconv y then Ferrante_Rackoff_Data.NEq
- else Ferrante_Rackoff_Data.Nox
- | b$y$z => if Term.could_unify (b, lt) then
- if term_of x aconv y then Ferrante_Rackoff_Data.Lt
- else if term_of x aconv z then Ferrante_Rackoff_Data.Gt
- else Ferrante_Rackoff_Data.Nox
- else if Term.could_unify (b, le) then
- if term_of x aconv y then Ferrante_Rackoff_Data.Le
- else if term_of x aconv z then Ferrante_Rackoff_Data.Ge
- else Ferrante_Rackoff_Data.Nox
- else Ferrante_Rackoff_Data.Nox
- | _ => Ferrante_Rackoff_Data.Nox
- in h end
- fun ss phi = HOL_ss addsimps (simps phi)
-in
- Ferrante_Rackoff_Data.funs @{thm "ferrack_axiom"}
- {isolate_conv = K (K (K Thm.reflexive)), whatis = generic_whatis, simpset = ss}
-end
-*}
-
-end
-
-use "~~/src/HOL/Tools/Qelim/ferrante_rackoff.ML"
-
-method_setup ferrack = {*
- Method.ctxt_args (Method.SIMPLE_METHOD' o FerranteRackoff.dlo_tac)
-*} "Ferrante and Rackoff's algorithm for quantifier elimination in dense linear orders"
-
-subsection {* Ferrante and Rackoff algorithm over ordered fields *}
-
-lemma neg_prod_lt:"(c\<Colon>'a\<Colon>ordered_field) < 0 \<Longrightarrow> ((c*x < 0) == (x > 0))"
-proof-
- assume H: "c < 0"
- have "c*x < 0 = (0/c < x)" by (simp only: neg_divide_less_eq[OF H] algebra_simps)
- also have "\<dots> = (0 < x)" by simp
- finally show "(c*x < 0) == (x > 0)" by simp
-qed
-
-lemma pos_prod_lt:"(c\<Colon>'a\<Colon>ordered_field) > 0 \<Longrightarrow> ((c*x < 0) == (x < 0))"
-proof-
- assume H: "c > 0"
- hence "c*x < 0 = (0/c > x)" by (simp only: pos_less_divide_eq[OF H] algebra_simps)
- also have "\<dots> = (0 > x)" by simp
- finally show "(c*x < 0) == (x < 0)" by simp
-qed
-
-lemma neg_prod_sum_lt: "(c\<Colon>'a\<Colon>ordered_field) < 0 \<Longrightarrow> ((c*x + t< 0) == (x > (- 1/c)*t))"
-proof-
- assume H: "c < 0"
- have "c*x + t< 0 = (c*x < -t)" by (subst less_iff_diff_less_0 [of "c*x" "-t"], simp)
- also have "\<dots> = (-t/c < x)" by (simp only: neg_divide_less_eq[OF H] algebra_simps)
- also have "\<dots> = ((- 1/c)*t < x)" by simp
- finally show "(c*x + t < 0) == (x > (- 1/c)*t)" by simp
-qed
-
-lemma pos_prod_sum_lt:"(c\<Colon>'a\<Colon>ordered_field) > 0 \<Longrightarrow> ((c*x + t < 0) == (x < (- 1/c)*t))"
-proof-
- assume H: "c > 0"
- have "c*x + t< 0 = (c*x < -t)" by (subst less_iff_diff_less_0 [of "c*x" "-t"], simp)
- also have "\<dots> = (-t/c > x)" by (simp only: pos_less_divide_eq[OF H] algebra_simps)
- also have "\<dots> = ((- 1/c)*t > x)" by simp
- finally show "(c*x + t < 0) == (x < (- 1/c)*t)" by simp
-qed
-
-lemma sum_lt:"((x::'a::pordered_ab_group_add) + t < 0) == (x < - t)"
- using less_diff_eq[where a= x and b=t and c=0] by simp
-
-lemma neg_prod_le:"(c\<Colon>'a\<Colon>ordered_field) < 0 \<Longrightarrow> ((c*x <= 0) == (x >= 0))"
-proof-
- assume H: "c < 0"
- have "c*x <= 0 = (0/c <= x)" by (simp only: neg_divide_le_eq[OF H] algebra_simps)
- also have "\<dots> = (0 <= x)" by simp
- finally show "(c*x <= 0) == (x >= 0)" by simp
-qed
-
-lemma pos_prod_le:"(c\<Colon>'a\<Colon>ordered_field) > 0 \<Longrightarrow> ((c*x <= 0) == (x <= 0))"
-proof-
- assume H: "c > 0"
- hence "c*x <= 0 = (0/c >= x)" by (simp only: pos_le_divide_eq[OF H] algebra_simps)
- also have "\<dots> = (0 >= x)" by simp
- finally show "(c*x <= 0) == (x <= 0)" by simp
-qed
-
-lemma neg_prod_sum_le: "(c\<Colon>'a\<Colon>ordered_field) < 0 \<Longrightarrow> ((c*x + t <= 0) == (x >= (- 1/c)*t))"
-proof-
- assume H: "c < 0"
- have "c*x + t <= 0 = (c*x <= -t)" by (subst le_iff_diff_le_0 [of "c*x" "-t"], simp)
- also have "\<dots> = (-t/c <= x)" by (simp only: neg_divide_le_eq[OF H] algebra_simps)
- also have "\<dots> = ((- 1/c)*t <= x)" by simp
- finally show "(c*x + t <= 0) == (x >= (- 1/c)*t)" by simp
-qed
-
-lemma pos_prod_sum_le:"(c\<Colon>'a\<Colon>ordered_field) > 0 \<Longrightarrow> ((c*x + t <= 0) == (x <= (- 1/c)*t))"
-proof-
- assume H: "c > 0"
- have "c*x + t <= 0 = (c*x <= -t)" by (subst le_iff_diff_le_0 [of "c*x" "-t"], simp)
- also have "\<dots> = (-t/c >= x)" by (simp only: pos_le_divide_eq[OF H] algebra_simps)
- also have "\<dots> = ((- 1/c)*t >= x)" by simp
- finally show "(c*x + t <= 0) == (x <= (- 1/c)*t)" by simp
-qed
-
-lemma sum_le:"((x::'a::pordered_ab_group_add) + t <= 0) == (x <= - t)"
- using le_diff_eq[where a= x and b=t and c=0] by simp
-
-lemma nz_prod_eq:"(c\<Colon>'a\<Colon>ordered_field) \<noteq> 0 \<Longrightarrow> ((c*x = 0) == (x = 0))" by simp
-lemma nz_prod_sum_eq: "(c\<Colon>'a\<Colon>ordered_field) \<noteq> 0 \<Longrightarrow> ((c*x + t = 0) == (x = (- 1/c)*t))"
-proof-
- assume H: "c \<noteq> 0"
- have "c*x + t = 0 = (c*x = -t)" by (subst eq_iff_diff_eq_0 [of "c*x" "-t"], simp)
- also have "\<dots> = (x = -t/c)" by (simp only: nonzero_eq_divide_eq[OF H] algebra_simps)
- finally show "(c*x + t = 0) == (x = (- 1/c)*t)" by simp
-qed
-lemma sum_eq:"((x::'a::pordered_ab_group_add) + t = 0) == (x = - t)"
- using eq_diff_eq[where a= x and b=t and c=0] by simp
-
-
-interpretation class_ordered_field_dense_linear_order!: constr_dense_linear_order
- "op <=" "op <"
- "\<lambda> x y. 1/2 * ((x::'a::{ordered_field,recpower,number_ring}) + y)"
-proof (unfold_locales, dlo, dlo, auto)
- fix x y::'a assume lt: "x < y"
- from less_half_sum[OF lt] show "x < (x + y) /2" by simp
-next
- fix x y::'a assume lt: "x < y"
- from gt_half_sum[OF lt] show "(x + y) /2 < y" by simp
-qed
-
-declaration{*
-let
-fun earlier [] x y = false
- | earlier (h::t) x y =
- if h aconvc y then false else if h aconvc x then true else earlier t x y;
-
-fun dest_frac ct = case term_of ct of
- Const (@{const_name "HOL.divide"},_) $ a $ b=>
- Rat.rat_of_quotient (snd (HOLogic.dest_number a), snd (HOLogic.dest_number b))
- | t => Rat.rat_of_int (snd (HOLogic.dest_number t))
-
-fun mk_frac phi cT x =
- let val (a, b) = Rat.quotient_of_rat x
- in if b = 1 then Numeral.mk_cnumber cT a
- else Thm.capply
- (Thm.capply (Drule.cterm_rule (instantiate' [SOME cT] []) @{cpat "op /"})
- (Numeral.mk_cnumber cT a))
- (Numeral.mk_cnumber cT b)
- end
-
-fun whatis x ct = case term_of ct of
- Const(@{const_name "HOL.plus"}, _)$(Const(@{const_name "HOL.times"},_)$_$y)$_ =>
- if y aconv term_of x then ("c*x+t",[(funpow 2 Thm.dest_arg1) ct, Thm.dest_arg ct])
- else ("Nox",[])
-| Const(@{const_name "HOL.plus"}, _)$y$_ =>
- if y aconv term_of x then ("x+t",[Thm.dest_arg ct])
- else ("Nox",[])
-| Const(@{const_name "HOL.times"}, _)$_$y =>
- if y aconv term_of x then ("c*x",[Thm.dest_arg1 ct])
- else ("Nox",[])
-| t => if t aconv term_of x then ("x",[]) else ("Nox",[]);
-
-fun xnormalize_conv ctxt [] ct = reflexive ct
-| xnormalize_conv ctxt (vs as (x::_)) ct =
- case term_of ct of
- Const(@{const_name HOL.less},_)$_$Const(@{const_name "HOL.zero"},_) =>
- (case whatis x (Thm.dest_arg1 ct) of
- ("c*x+t",[c,t]) =>
- let
- val cr = dest_frac c
- val clt = Thm.dest_fun2 ct
- val cz = Thm.dest_arg ct
- val neg = cr </ Rat.zero
- val cthp = Simplifier.rewrite (local_simpset_of ctxt)
- (Thm.capply @{cterm "Trueprop"}
- (if neg then Thm.capply (Thm.capply clt c) cz
- else Thm.capply (Thm.capply clt cz) c))
- val cth = equal_elim (symmetric cthp) TrueI
- val th = implies_elim (instantiate' [SOME (ctyp_of_term x)] (map SOME [c,x,t])
- (if neg then @{thm neg_prod_sum_lt} else @{thm pos_prod_sum_lt})) cth
- val rth = Conv.fconv_rule (Conv.arg_conv (Conv.binop_conv
- (Normalizer.semiring_normalize_ord_conv ctxt (earlier vs)))) th
- in rth end
- | ("x+t",[t]) =>
- let
- val T = ctyp_of_term x
- val th = instantiate' [SOME T] [SOME x, SOME t] @{thm "sum_lt"}
- val rth = Conv.fconv_rule (Conv.arg_conv (Conv.binop_conv
- (Normalizer.semiring_normalize_ord_conv ctxt (earlier vs)))) th
- in rth end
- | ("c*x",[c]) =>
- let
- val cr = dest_frac c
- val clt = Thm.dest_fun2 ct
- val cz = Thm.dest_arg ct
- val neg = cr </ Rat.zero
- val cthp = Simplifier.rewrite (local_simpset_of ctxt)
- (Thm.capply @{cterm "Trueprop"}
- (if neg then Thm.capply (Thm.capply clt c) cz
- else Thm.capply (Thm.capply clt cz) c))
- val cth = equal_elim (symmetric cthp) TrueI
- val th = implies_elim (instantiate' [SOME (ctyp_of_term x)] (map SOME [c,x])
- (if neg then @{thm neg_prod_lt} else @{thm pos_prod_lt})) cth
- val rth = th
- in rth end
- | _ => reflexive ct)
-
-
-| Const(@{const_name HOL.less_eq},_)$_$Const(@{const_name "HOL.zero"},_) =>
- (case whatis x (Thm.dest_arg1 ct) of
- ("c*x+t",[c,t]) =>
- let
- val T = ctyp_of_term x
- val cr = dest_frac c
- val clt = Drule.cterm_rule (instantiate' [SOME T] []) @{cpat "op <"}
- val cz = Thm.dest_arg ct
- val neg = cr </ Rat.zero
- val cthp = Simplifier.rewrite (local_simpset_of ctxt)
- (Thm.capply @{cterm "Trueprop"}
- (if neg then Thm.capply (Thm.capply clt c) cz
- else Thm.capply (Thm.capply clt cz) c))
- val cth = equal_elim (symmetric cthp) TrueI
- val th = implies_elim (instantiate' [SOME T] (map SOME [c,x,t])
- (if neg then @{thm neg_prod_sum_le} else @{thm pos_prod_sum_le})) cth
- val rth = Conv.fconv_rule (Conv.arg_conv (Conv.binop_conv
- (Normalizer.semiring_normalize_ord_conv ctxt (earlier vs)))) th
- in rth end
- | ("x+t",[t]) =>
- let
- val T = ctyp_of_term x
- val th = instantiate' [SOME T] [SOME x, SOME t] @{thm "sum_le"}
- val rth = Conv.fconv_rule (Conv.arg_conv (Conv.binop_conv
- (Normalizer.semiring_normalize_ord_conv ctxt (earlier vs)))) th
- in rth end
- | ("c*x",[c]) =>
- let
- val T = ctyp_of_term x
- val cr = dest_frac c
- val clt = Drule.cterm_rule (instantiate' [SOME T] []) @{cpat "op <"}
- val cz = Thm.dest_arg ct
- val neg = cr </ Rat.zero
- val cthp = Simplifier.rewrite (local_simpset_of ctxt)
- (Thm.capply @{cterm "Trueprop"}
- (if neg then Thm.capply (Thm.capply clt c) cz
- else Thm.capply (Thm.capply clt cz) c))
- val cth = equal_elim (symmetric cthp) TrueI
- val th = implies_elim (instantiate' [SOME (ctyp_of_term x)] (map SOME [c,x])
- (if neg then @{thm neg_prod_le} else @{thm pos_prod_le})) cth
- val rth = th
- in rth end
- | _ => reflexive ct)
-
-| Const("op =",_)$_$Const(@{const_name "HOL.zero"},_) =>
- (case whatis x (Thm.dest_arg1 ct) of
- ("c*x+t",[c,t]) =>
- let
- val T = ctyp_of_term x
- val cr = dest_frac c
- val ceq = Thm.dest_fun2 ct
- val cz = Thm.dest_arg ct
- val cthp = Simplifier.rewrite (local_simpset_of ctxt)
- (Thm.capply @{cterm "Trueprop"}
- (Thm.capply @{cterm "Not"} (Thm.capply (Thm.capply ceq c) cz)))
- val cth = equal_elim (symmetric cthp) TrueI
- val th = implies_elim
- (instantiate' [SOME T] (map SOME [c,x,t]) @{thm nz_prod_sum_eq}) cth
- val rth = Conv.fconv_rule (Conv.arg_conv (Conv.binop_conv
- (Normalizer.semiring_normalize_ord_conv ctxt (earlier vs)))) th
- in rth end
- | ("x+t",[t]) =>
- let
- val T = ctyp_of_term x
- val th = instantiate' [SOME T] [SOME x, SOME t] @{thm "sum_eq"}
- val rth = Conv.fconv_rule (Conv.arg_conv (Conv.binop_conv
- (Normalizer.semiring_normalize_ord_conv ctxt (earlier vs)))) th
- in rth end
- | ("c*x",[c]) =>
- let
- val T = ctyp_of_term x
- val cr = dest_frac c
- val ceq = Thm.dest_fun2 ct
- val cz = Thm.dest_arg ct
- val cthp = Simplifier.rewrite (local_simpset_of ctxt)
- (Thm.capply @{cterm "Trueprop"}
- (Thm.capply @{cterm "Not"} (Thm.capply (Thm.capply ceq c) cz)))
- val cth = equal_elim (symmetric cthp) TrueI
- val rth = implies_elim
- (instantiate' [SOME T] (map SOME [c,x]) @{thm nz_prod_eq}) cth
- in rth end
- | _ => reflexive ct);
-
-local
- val less_iff_diff_less_0 = mk_meta_eq @{thm "less_iff_diff_less_0"}
- val le_iff_diff_le_0 = mk_meta_eq @{thm "le_iff_diff_le_0"}
- val eq_iff_diff_eq_0 = mk_meta_eq @{thm "eq_iff_diff_eq_0"}
-in
-fun field_isolate_conv phi ctxt vs ct = case term_of ct of
- Const(@{const_name HOL.less},_)$a$b =>
- let val (ca,cb) = Thm.dest_binop ct
- val T = ctyp_of_term ca
- val th = instantiate' [SOME T] [SOME ca, SOME cb] less_iff_diff_less_0
- val nth = Conv.fconv_rule
- (Conv.arg_conv (Conv.arg1_conv
- (Normalizer.semiring_normalize_ord_conv @{context} (earlier vs)))) th
- val rth = transitive nth (xnormalize_conv ctxt vs (Thm.rhs_of nth))
- in rth end
-| Const(@{const_name HOL.less_eq},_)$a$b =>
- let val (ca,cb) = Thm.dest_binop ct
- val T = ctyp_of_term ca
- val th = instantiate' [SOME T] [SOME ca, SOME cb] le_iff_diff_le_0
- val nth = Conv.fconv_rule
- (Conv.arg_conv (Conv.arg1_conv
- (Normalizer.semiring_normalize_ord_conv @{context} (earlier vs)))) th
- val rth = transitive nth (xnormalize_conv ctxt vs (Thm.rhs_of nth))
- in rth end
-
-| Const("op =",_)$a$b =>
- let val (ca,cb) = Thm.dest_binop ct
- val T = ctyp_of_term ca
- val th = instantiate' [SOME T] [SOME ca, SOME cb] eq_iff_diff_eq_0
- val nth = Conv.fconv_rule
- (Conv.arg_conv (Conv.arg1_conv
- (Normalizer.semiring_normalize_ord_conv @{context} (earlier vs)))) th
- val rth = transitive nth (xnormalize_conv ctxt vs (Thm.rhs_of nth))
- in rth end
-| @{term "Not"} $(Const("op =",_)$a$b) => Conv.arg_conv (field_isolate_conv phi ctxt vs) ct
-| _ => reflexive ct
-end;
-
-fun classfield_whatis phi =
- let
- fun h x t =
- case term_of t of
- Const("op =", _)$y$z => if term_of x aconv y then Ferrante_Rackoff_Data.Eq
- else Ferrante_Rackoff_Data.Nox
- | @{term "Not"}$(Const("op =", _)$y$z) => if term_of x aconv y then Ferrante_Rackoff_Data.NEq
- else Ferrante_Rackoff_Data.Nox
- | Const(@{const_name HOL.less},_)$y$z =>
- if term_of x aconv y then Ferrante_Rackoff_Data.Lt
- else if term_of x aconv z then Ferrante_Rackoff_Data.Gt
- else Ferrante_Rackoff_Data.Nox
- | Const (@{const_name HOL.less_eq},_)$y$z =>
- if term_of x aconv y then Ferrante_Rackoff_Data.Le
- else if term_of x aconv z then Ferrante_Rackoff_Data.Ge
- else Ferrante_Rackoff_Data.Nox
- | _ => Ferrante_Rackoff_Data.Nox
- in h end;
-fun class_field_ss phi =
- HOL_basic_ss addsimps ([@{thm "linorder_not_less"}, @{thm "linorder_not_le"}])
- addsplits [@{thm "abs_split"},@{thm "split_max"}, @{thm "split_min"}]
-
-in
-Ferrante_Rackoff_Data.funs @{thm "class_ordered_field_dense_linear_order.ferrack_axiom"}
- {isolate_conv = field_isolate_conv, whatis = classfield_whatis, simpset = class_field_ss}
-end
-*}
-
-
-end
--- a/src/HOL/Deriv.thy Fri Feb 06 15:57:47 2009 +0100
+++ b/src/HOL/Deriv.thy Fri Feb 06 15:59:49 2009 +0100
@@ -1038,6 +1038,15 @@
qed
qed
+lemma MVT2:
+ "[| a < b; \<forall>x. a \<le> x & x \<le> b --> DERIV f x :> f'(x) |]
+ ==> \<exists>z::real. a < z & z < b & (f b - f a = (b - a) * f'(z))"
+apply (drule MVT)
+apply (blast intro: DERIV_isCont)
+apply (force dest: order_less_imp_le simp add: differentiable_def)
+apply (blast dest: DERIV_unique order_less_imp_le)
+done
+
text{*A function is constant if its derivative is 0 over an interval.*}
@@ -1073,6 +1082,30 @@
apply (blast dest: DERIV_isconst1)
done
+lemma DERIV_isconst3: fixes a b x y :: real
+ assumes "a < b" and "x \<in> {a <..< b}" and "y \<in> {a <..< b}"
+ assumes derivable: "\<And>x. x \<in> {a <..< b} \<Longrightarrow> DERIV f x :> 0"
+ shows "f x = f y"
+proof (cases "x = y")
+ case False
+ let ?a = "min x y"
+ let ?b = "max x y"
+
+ have "\<forall>z. ?a \<le> z \<and> z \<le> ?b \<longrightarrow> DERIV f z :> 0"
+ proof (rule allI, rule impI)
+ fix z :: real assume "?a \<le> z \<and> z \<le> ?b"
+ hence "a < z" and "z < b" using `x \<in> {a <..< b}` and `y \<in> {a <..< b}` by auto
+ hence "z \<in> {a<..<b}" by auto
+ thus "DERIV f z :> 0" by (rule derivable)
+ qed
+ hence isCont: "\<forall>z. ?a \<le> z \<and> z \<le> ?b \<longrightarrow> isCont f z"
+ and DERIV: "\<forall>z. ?a < z \<and> z < ?b \<longrightarrow> DERIV f z :> 0" using DERIV_isCont by auto
+
+ have "?a < ?b" using `x \<noteq> y` by auto
+ from DERIV_isconst2[OF this isCont DERIV, of x] and DERIV_isconst2[OF this isCont DERIV, of y]
+ show ?thesis by auto
+qed auto
+
lemma DERIV_isconst_all:
fixes f :: "real => real"
shows "\<forall>x. DERIV f x :> 0 ==> f(x) = f(y)"
--- a/src/HOL/Extraction/Higman.thy Fri Feb 06 15:57:47 2009 +0100
+++ b/src/HOL/Extraction/Higman.thy Fri Feb 06 15:59:49 2009 +0100
@@ -1,5 +1,4 @@
(* Title: HOL/Extraction/Higman.thy
- ID: $Id$
Author: Stefan Berghofer, TU Muenchen
Monika Seisenberger, LMU Muenchen
*)
@@ -7,7 +6,7 @@
header {* Higman's lemma *}
theory Higman
-imports Main "~~/src/HOL/ex/Random"
+imports Main State_Monad Random
begin
text {*
--- a/src/HOL/Fundamental_Theorem_Algebra.thy Fri Feb 06 15:57:47 2009 +0100
+++ b/src/HOL/Fundamental_Theorem_Algebra.thy Fri Feb 06 15:59:49 2009 +0100
@@ -3,7 +3,7 @@
header{*Fundamental Theorem of Algebra*}
theory Fundamental_Theorem_Algebra
-imports Polynomial Dense_Linear_Order Complex
+imports Polynomial Complex
begin
subsection {* Square root of complex numbers *}
@@ -59,7 +59,8 @@
by (rule of_real_power [symmetric])
lemma real_down2: "(0::real) < d1 \<Longrightarrow> 0 < d2 ==> EX e. 0 < e & e < d1 & e < d2"
- apply ferrack apply arith done
+ apply (rule exI[where x = "min d1 d2 / 2"])
+ by (simp add: field_simps min_def)
text{* The triangle inequality for cmod *}
lemma complex_mod_triangle_sub: "cmod w \<le> cmod (w + z) + norm z"
--- a/src/HOL/Imperative_HOL/Array.thy Fri Feb 06 15:57:47 2009 +0100
+++ b/src/HOL/Imperative_HOL/Array.thy Fri Feb 06 15:59:49 2009 +0100
@@ -124,47 +124,47 @@
subsubsection {* Logical intermediate layer *}
definition new' where
- [code del]: "new' = Array.new o nat_of_index"
+ [code del]: "new' = Array.new o Code_Index.nat_of"
hide (open) const new'
lemma [code]:
- "Array.new = Array.new' o index_of_nat"
+ "Array.new = Array.new' o Code_Index.of_nat"
by (simp add: new'_def o_def)
definition of_list' where
- [code del]: "of_list' i xs = Array.of_list (take (nat_of_index i) xs)"
+ [code del]: "of_list' i xs = Array.of_list (take (Code_Index.nat_of i) xs)"
hide (open) const of_list'
lemma [code]:
- "Array.of_list xs = Array.of_list' (index_of_nat (List.length xs)) xs"
+ "Array.of_list xs = Array.of_list' (Code_Index.of_nat (List.length xs)) xs"
by (simp add: of_list'_def)
definition make' where
- [code del]: "make' i f = Array.make (nat_of_index i) (f o index_of_nat)"
+ [code del]: "make' i f = Array.make (Code_Index.nat_of i) (f o Code_Index.of_nat)"
hide (open) const make'
lemma [code]:
- "Array.make n f = Array.make' (index_of_nat n) (f o nat_of_index)"
+ "Array.make n f = Array.make' (Code_Index.of_nat n) (f o Code_Index.nat_of)"
by (simp add: make'_def o_def)
definition length' where
- [code del]: "length' = Array.length \<guillemotright>== liftM index_of_nat"
+ [code del]: "length' = Array.length \<guillemotright>== liftM Code_Index.of_nat"
hide (open) const length'
lemma [code]:
- "Array.length = Array.length' \<guillemotright>== liftM nat_of_index"
+ "Array.length = Array.length' \<guillemotright>== liftM Code_Index.nat_of"
by (simp add: length'_def monad_simp',
simp add: liftM_def comp_def monad_simp,
simp add: monad_simp')
definition nth' where
- [code del]: "nth' a = Array.nth a o nat_of_index"
+ [code del]: "nth' a = Array.nth a o Code_Index.nat_of"
hide (open) const nth'
lemma [code]:
- "Array.nth a n = Array.nth' a (index_of_nat n)"
+ "Array.nth a n = Array.nth' a (Code_Index.of_nat n)"
by (simp add: nth'_def)
definition upd' where
- [code del]: "upd' a i x = Array.upd (nat_of_index i) x a \<guillemotright> return ()"
+ [code del]: "upd' a i x = Array.upd (Code_Index.nat_of i) x a \<guillemotright> return ()"
hide (open) const upd'
lemma [code]:
- "Array.upd i x a = Array.upd' a (index_of_nat i) x \<guillemotright> return a"
+ "Array.upd i x a = Array.upd' a (Code_Index.of_nat i) x \<guillemotright> return a"
by (simp add: upd'_def monad_simp upd_return)
--- a/src/HOL/Integration.thy Fri Feb 06 15:57:47 2009 +0100
+++ b/src/HOL/Integration.thy Fri Feb 06 15:59:49 2009 +0100
@@ -557,8 +557,8 @@
apply (drule_tac n = m in partition_lt_gen, auto)
apply (frule partition_eq_bound)
apply (drule_tac [2] partition_gt, auto)
-apply (metis dense_linear_order_class.dlo_simps(8) not_less partition_rhs partition_rhs2)
-apply (metis le_less_trans dense_linear_order_class.dlo_simps(8) nat_le_linear partition_eq_bound partition_rhs2)
+apply (metis linear not_less partition_rhs partition_rhs2)
+apply (metis add_diff_inverse diff_is_0_eq le0 le_diff_conv nat_add_commute partition partition_eq_bound partition_rhs real_less_def termination_basic_simps(5))
done
lemma lemma_additivity4_psize_eq:
--- a/src/HOL/IsaMakefile Fri Feb 06 15:57:47 2009 +0100
+++ b/src/HOL/IsaMakefile Fri Feb 06 15:59:49 2009 +0100
@@ -284,7 +284,6 @@
Series.thy \
Taylor.thy \
Transcendental.thy \
- Dense_Linear_Order.thy \
GCD.thy \
Order_Relation.thy \
Parity.thy \
@@ -311,11 +310,12 @@
HOL-Library: HOL $(LOG)/HOL-Library.gz
+
$(LOG)/HOL-Library.gz: $(OUT)/HOL Library/SetsAndFunctions.thy \
Library/Abstract_Rat.thy \
Library/BigO.thy Library/ContNotDenum.thy Library/Efficient_Nat.thy \
Library/Executable_Set.thy Library/Infinite_Set.thy \
- Library/FuncSet.thy \
+ Library/FuncSet.thy Library/Dense_Linear_Order.thy \
Library/Library.thy Library/List_Prefix.thy Library/State_Monad.thy \
Library/Multiset.thy Library/Permutation.thy \
Library/Primes.thy Library/Pocklington.thy Library/Quotient.thy \
@@ -335,6 +335,7 @@
Library/Mapping.thy Library/Numeral_Type.thy Library/Reflection.thy \
Library/Boolean_Algebra.thy Library/Countable.thy \
Library/RBT.thy Library/Univ_Poly.thy \
+ Library/Random.thy Library/Quickcheck.thy \
Library/Enum.thy Library/Float.thy $(SRC)/Tools/float.ML $(SRC)/HOL/Tools/float_arith.ML \
Library/reify_data.ML Library/reflection.ML
@cd Library; $(ISABELLE_TOOL) usedir $(OUT)/HOL Library
@@ -684,6 +685,7 @@
HOL-Reflection: HOL $(LOG)/HOL-Reflection.gz
$(LOG)/HOL-Reflection.gz: $(OUT)/HOL \
+ Reflection/Approximation.thy \
Reflection/Cooper.thy \
Reflection/cooper_tac.ML \
Reflection/Ferrack.thy \
@@ -814,7 +816,7 @@
ex/Abstract_NAT.thy ex/Antiquote.thy ex/Arith_Examples.thy ex/BT.thy \
ex/BinEx.thy ex/CTL.thy ex/Chinese.thy ex/Classical.thy \
ex/Coherent.thy ex/Dense_Linear_Order_Ex.thy ex/Eval_Examples.thy \
- ex/Groebner_Examples.thy ex/Random.thy ex/Quickcheck.thy \
+ ex/Groebner_Examples.thy ex/Quickcheck_Generators.thy \
ex/Codegenerator.thy ex/Codegenerator_Pretty.thy \
ex/CodegenSML_Test.thy ex/Formal_Power_Series_Examples.thy \
ex/Commutative_RingEx.thy ex/Efficient_Nat_examples.thy \
@@ -836,7 +838,8 @@
ex/svc_funcs.ML ex/svc_test.thy \
ex/ImperativeQuicksort.thy \
ex/Arithmetic_Series_Complex.thy ex/HarmonicSeries.thy \
- ex/Sqrt.thy ex/Sqrt_Script.thy
+ ex/Sqrt.thy ex/Sqrt_Script.thy \
+ ex/ApproximationEx.thy
@$(ISABELLE_TOOL) usedir $(OUT)/HOL ex
@@ -883,6 +886,7 @@
$(SRC)/Tools/Compute_Oracle/am_ghc.ML \
$(SRC)/Tools/Compute_Oracle/am_sml.ML \
$(SRC)/Tools/Compute_Oracle/compute.ML \
+ Tools/ComputeFloat.thy Tools/float_arith.ML \
Matrix/Matrix.thy Matrix/SparseMatrix.thy Matrix/LP.thy \
Matrix/document/root.tex Matrix/ROOT.ML Matrix/cplex/Cplex.thy \
Matrix/cplex/CplexMatrixConverter.ML Matrix/cplex/Cplex_tools.ML \
--- a/src/HOL/Library/Code_Index.thy Fri Feb 06 15:57:47 2009 +0100
+++ b/src/HOL/Library/Code_Index.thy Fri Feb 06 15:59:49 2009 +0100
@@ -1,6 +1,4 @@
-(* ID: $Id$
- Author: Florian Haftmann, TU Muenchen
-*)
+(* Author: Florian Haftmann, TU Muenchen *)
header {* Type of indices *}
@@ -15,78 +13,77 @@
subsection {* Datatype of indices *}
-typedef index = "UNIV \<Colon> nat set"
- morphisms nat_of_index index_of_nat by rule
+typedef (open) index = "UNIV \<Colon> nat set"
+ morphisms nat_of of_nat by rule
-lemma index_of_nat_nat_of_index [simp]:
- "index_of_nat (nat_of_index k) = k"
- by (rule nat_of_index_inverse)
+lemma of_nat_nat_of [simp]:
+ "of_nat (nat_of k) = k"
+ by (rule nat_of_inverse)
-lemma nat_of_index_index_of_nat [simp]:
- "nat_of_index (index_of_nat n) = n"
- by (rule index_of_nat_inverse)
- (unfold index_def, rule UNIV_I)
+lemma nat_of_of_nat [simp]:
+ "nat_of (of_nat n) = n"
+ by (rule of_nat_inverse) (rule UNIV_I)
lemma [measure_function]:
- "is_measure nat_of_index" by (rule is_measure_trivial)
+ "is_measure nat_of" by (rule is_measure_trivial)
lemma index:
- "(\<And>n\<Colon>index. PROP P n) \<equiv> (\<And>n\<Colon>nat. PROP P (index_of_nat n))"
+ "(\<And>n\<Colon>index. PROP P n) \<equiv> (\<And>n\<Colon>nat. PROP P (of_nat n))"
proof
fix n :: nat
assume "\<And>n\<Colon>index. PROP P n"
- then show "PROP P (index_of_nat n)" .
+ then show "PROP P (of_nat n)" .
next
fix n :: index
- assume "\<And>n\<Colon>nat. PROP P (index_of_nat n)"
- then have "PROP P (index_of_nat (nat_of_index n))" .
+ assume "\<And>n\<Colon>nat. PROP P (of_nat n)"
+ then have "PROP P (of_nat (nat_of n))" .
then show "PROP P n" by simp
qed
lemma index_case:
- assumes "\<And>n. k = index_of_nat n \<Longrightarrow> P"
+ assumes "\<And>n. k = of_nat n \<Longrightarrow> P"
shows P
- by (rule assms [of "nat_of_index k"]) simp
+ by (rule assms [of "nat_of k"]) simp
lemma index_induct_raw:
- assumes "\<And>n. P (index_of_nat n)"
+ assumes "\<And>n. P (of_nat n)"
shows "P k"
proof -
- from assms have "P (index_of_nat (nat_of_index k))" .
+ from assms have "P (of_nat (nat_of k))" .
then show ?thesis by simp
qed
-lemma nat_of_index_inject [simp]:
- "nat_of_index k = nat_of_index l \<longleftrightarrow> k = l"
- by (rule nat_of_index_inject)
+lemma nat_of_inject [simp]:
+ "nat_of k = nat_of l \<longleftrightarrow> k = l"
+ by (rule nat_of_inject)
-lemma index_of_nat_inject [simp]:
- "index_of_nat n = index_of_nat m \<longleftrightarrow> n = m"
- by (auto intro!: index_of_nat_inject simp add: index_def)
+lemma of_nat_inject [simp]:
+ "of_nat n = of_nat m \<longleftrightarrow> n = m"
+ by (rule of_nat_inject) (rule UNIV_I)+
instantiation index :: zero
begin
definition [simp, code del]:
- "0 = index_of_nat 0"
+ "0 = of_nat 0"
instance ..
end
definition [simp]:
- "Suc_index k = index_of_nat (Suc (nat_of_index k))"
+ "Suc_index k = of_nat (Suc (nat_of k))"
rep_datatype "0 \<Colon> index" Suc_index
proof -
fix P :: "index \<Rightarrow> bool"
fix k :: index
- assume "P 0" then have init: "P (index_of_nat 0)" by simp
+ assume "P 0" then have init: "P (of_nat 0)" by simp
assume "\<And>k. P k \<Longrightarrow> P (Suc_index k)"
- then have "\<And>n. P (index_of_nat n) \<Longrightarrow> P (Suc_index (index_of_nat n))" .
- then have step: "\<And>n. P (index_of_nat n) \<Longrightarrow> P (index_of_nat (Suc n))" by simp
- from init step have "P (index_of_nat (nat_of_index k))"
- by (induct "nat_of_index k") simp_all
+ then have "\<And>n. P (of_nat n) \<Longrightarrow> P (Suc_index (of_nat n))" .
+ then have step: "\<And>n. P (of_nat n) \<Longrightarrow> P (of_nat (Suc n))" by simp
+ from init step have "P (of_nat (nat_of k))"
+ by (induct "nat_of k") simp_all
then show "P k" by simp
qed simp_all
@@ -96,25 +93,25 @@
declare index.induct [case_names nat, induct type: index]
lemma [code]:
- "index_size = nat_of_index"
+ "index_size = nat_of"
proof (rule ext)
fix k
- have "index_size k = nat_size (nat_of_index k)"
+ have "index_size k = nat_size (nat_of k)"
by (induct k rule: index.induct) (simp_all del: zero_index_def Suc_index_def, simp_all)
- also have "nat_size (nat_of_index k) = nat_of_index k" by (induct "nat_of_index k") simp_all
- finally show "index_size k = nat_of_index k" .
+ also have "nat_size (nat_of k) = nat_of k" by (induct "nat_of k") simp_all
+ finally show "index_size k = nat_of k" .
qed
lemma [code]:
- "size = nat_of_index"
+ "size = nat_of"
proof (rule ext)
fix k
- show "size k = nat_of_index k"
+ show "size k = nat_of k"
by (induct k) (simp_all del: zero_index_def Suc_index_def, simp_all)
qed
lemma [code]:
- "eq_class.eq k l \<longleftrightarrow> eq_class.eq (nat_of_index k) (nat_of_index l)"
+ "eq_class.eq k l \<longleftrightarrow> eq_class.eq (nat_of k) (nat_of l)"
by (cases k, cases l) (simp add: eq)
lemma [code nbe]:
@@ -128,14 +125,14 @@
begin
definition
- "number_of = index_of_nat o nat"
+ "number_of = of_nat o nat"
instance ..
end
-lemma nat_of_index_number [simp]:
- "nat_of_index (number_of k) = number_of k"
+lemma nat_of_number [simp]:
+ "nat_of (number_of k) = number_of k"
by (simp add: number_of_index_def nat_number_of_def number_of_is_id)
code_datatype "number_of \<Colon> int \<Rightarrow> index"
@@ -147,30 +144,31 @@
begin
definition [simp, code del]:
- "(1\<Colon>index) = index_of_nat 1"
+ "(1\<Colon>index) = of_nat 1"
definition [simp, code del]:
- "n + m = index_of_nat (nat_of_index n + nat_of_index m)"
+ "n + m = of_nat (nat_of n + nat_of m)"
definition [simp, code del]:
- "n - m = index_of_nat (nat_of_index n - nat_of_index m)"
+ "n - m = of_nat (nat_of n - nat_of m)"
definition [simp, code del]:
- "n * m = index_of_nat (nat_of_index n * nat_of_index m)"
+ "n * m = of_nat (nat_of n * nat_of m)"
definition [simp, code del]:
- "n div m = index_of_nat (nat_of_index n div nat_of_index m)"
+ "n div m = of_nat (nat_of n div nat_of m)"
definition [simp, code del]:
- "n mod m = index_of_nat (nat_of_index n mod nat_of_index m)"
+ "n mod m = of_nat (nat_of n mod nat_of m)"
definition [simp, code del]:
- "n \<le> m \<longleftrightarrow> nat_of_index n \<le> nat_of_index m"
+ "n \<le> m \<longleftrightarrow> nat_of n \<le> nat_of m"
definition [simp, code del]:
- "n < m \<longleftrightarrow> nat_of_index n < nat_of_index m"
+ "n < m \<longleftrightarrow> nat_of n < nat_of m"
-instance by default (auto simp add: left_distrib index)
+instance proof
+qed (auto simp add: left_distrib)
end
@@ -187,14 +185,14 @@
using one_index_code ..
lemma plus_index_code [code nbe]:
- "index_of_nat n + index_of_nat m = index_of_nat (n + m)"
+ "of_nat n + of_nat m = of_nat (n + m)"
by simp
definition subtract_index :: "index \<Rightarrow> index \<Rightarrow> index" where
[simp, code del]: "subtract_index = op -"
lemma subtract_index_code [code nbe]:
- "subtract_index (index_of_nat n) (index_of_nat m) = index_of_nat (n - m)"
+ "subtract_index (of_nat n) (of_nat m) = of_nat (n - m)"
by simp
lemma minus_index_code [code]:
@@ -202,42 +200,42 @@
by simp
lemma times_index_code [code nbe]:
- "index_of_nat n * index_of_nat m = index_of_nat (n * m)"
+ "of_nat n * of_nat m = of_nat (n * m)"
by simp
lemma less_eq_index_code [code nbe]:
- "index_of_nat n \<le> index_of_nat m \<longleftrightarrow> n \<le> m"
+ "of_nat n \<le> of_nat m \<longleftrightarrow> n \<le> m"
by simp
lemma less_index_code [code nbe]:
- "index_of_nat n < index_of_nat m \<longleftrightarrow> n < m"
+ "of_nat n < of_nat m \<longleftrightarrow> n < m"
by simp
lemma Suc_index_minus_one: "Suc_index n - 1 = n" by simp
-lemma index_of_nat_code [code]:
- "index_of_nat = of_nat"
+lemma of_nat_code [code]:
+ "of_nat = Nat.of_nat"
proof
fix n :: nat
- have "of_nat n = index_of_nat n"
+ have "Nat.of_nat n = of_nat n"
by (induct n) simp_all
- then show "index_of_nat n = of_nat n"
+ then show "of_nat n = Nat.of_nat n"
by (rule sym)
qed
-lemma index_not_eq_zero: "i \<noteq> index_of_nat 0 \<longleftrightarrow> i \<ge> 1"
+lemma index_not_eq_zero: "i \<noteq> of_nat 0 \<longleftrightarrow> i \<ge> 1"
by (cases i) auto
-definition nat_of_index_aux :: "index \<Rightarrow> nat \<Rightarrow> nat" where
- "nat_of_index_aux i n = nat_of_index i + n"
+definition nat_of_aux :: "index \<Rightarrow> nat \<Rightarrow> nat" where
+ "nat_of_aux i n = nat_of i + n"
-lemma nat_of_index_aux_code [code]:
- "nat_of_index_aux i n = (if i = 0 then n else nat_of_index_aux (i - 1) (Suc n))"
- by (auto simp add: nat_of_index_aux_def index_not_eq_zero)
+lemma nat_of_aux_code [code]:
+ "nat_of_aux i n = (if i = 0 then n else nat_of_aux (i - 1) (Suc n))"
+ by (auto simp add: nat_of_aux_def index_not_eq_zero)
-lemma nat_of_index_code [code]:
- "nat_of_index i = nat_of_index_aux i 0"
- by (simp add: nat_of_index_aux_def)
+lemma nat_of_code [code]:
+ "nat_of i = nat_of_aux i 0"
+ by (simp add: nat_of_aux_def)
definition div_mod_index :: "index \<Rightarrow> index \<Rightarrow> index \<times> index" where
[code del]: "div_mod_index n m = (n div m, n mod m)"
@@ -254,6 +252,7 @@
"n mod m = snd (div_mod_index n m)"
unfolding div_mod_index_def by simp
+hide (open) const of_nat nat_of
subsection {* ML interface *}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Library/Dense_Linear_Order.thy Fri Feb 06 15:59:49 2009 +0100
@@ -0,0 +1,879 @@
+(* Title : HOL/Dense_Linear_Order.thy
+ Author : Amine Chaieb, TU Muenchen
+*)
+
+header {* Dense linear order without endpoints
+ and a quantifier elimination procedure in Ferrante and Rackoff style *}
+
+theory Dense_Linear_Order
+imports Plain Groebner_Basis Main
+uses
+ "~~/src/HOL/Tools/Qelim/langford_data.ML"
+ "~~/src/HOL/Tools/Qelim/ferrante_rackoff_data.ML"
+ ("~~/src/HOL/Tools/Qelim/langford.ML")
+ ("~~/src/HOL/Tools/Qelim/ferrante_rackoff.ML")
+begin
+
+setup {* Langford_Data.setup #> Ferrante_Rackoff_Data.setup *}
+
+context linorder
+begin
+
+lemma less_not_permute[noatp]: "\<not> (x < y \<and> y < x)" by (simp add: not_less linear)
+
+lemma gather_simps[noatp]:
+ shows
+ "(\<exists>x. (\<forall>y \<in> L. y < x) \<and> (\<forall>y \<in> U. x < y) \<and> x < u \<and> P x) \<longleftrightarrow> (\<exists>x. (\<forall>y \<in> L. y < x) \<and> (\<forall>y \<in> (insert u U). x < y) \<and> P x)"
+ and "(\<exists>x. (\<forall>y \<in> L. y < x) \<and> (\<forall>y \<in> U. x < y) \<and> l < x \<and> P x) \<longleftrightarrow> (\<exists>x. (\<forall>y \<in> (insert l L). y < x) \<and> (\<forall>y \<in> U. x < y) \<and> P x)"
+ "(\<exists>x. (\<forall>y \<in> L. y < x) \<and> (\<forall>y \<in> U. x < y) \<and> x < u) \<longleftrightarrow> (\<exists>x. (\<forall>y \<in> L. y < x) \<and> (\<forall>y \<in> (insert u U). x < y))"
+ and "(\<exists>x. (\<forall>y \<in> L. y < x) \<and> (\<forall>y \<in> U. x < y) \<and> l < x) \<longleftrightarrow> (\<exists>x. (\<forall>y \<in> (insert l L). y < x) \<and> (\<forall>y \<in> U. x < y))" by auto
+
+lemma
+ gather_start[noatp]: "(\<exists>x. P x) \<equiv> (\<exists>x. (\<forall>y \<in> {}. y < x) \<and> (\<forall>y\<in> {}. x < y) \<and> P x)"
+ by simp
+
+text{* Theorems for @{text "\<exists>z. \<forall>x. x < z \<longrightarrow> (P x \<longleftrightarrow> P\<^bsub>-\<infinity>\<^esub>)"}*}
+lemma minf_lt[noatp]: "\<exists>z . \<forall>x. x < z \<longrightarrow> (x < t \<longleftrightarrow> True)" by auto
+lemma minf_gt[noatp]: "\<exists>z . \<forall>x. x < z \<longrightarrow> (t < x \<longleftrightarrow> False)"
+ by (simp add: not_less) (rule exI[where x="t"], auto simp add: less_le)
+
+lemma minf_le[noatp]: "\<exists>z. \<forall>x. x < z \<longrightarrow> (x \<le> t \<longleftrightarrow> True)" by (auto simp add: less_le)
+lemma minf_ge[noatp]: "\<exists>z. \<forall>x. x < z \<longrightarrow> (t \<le> x \<longleftrightarrow> False)"
+ by (auto simp add: less_le not_less not_le)
+lemma minf_eq[noatp]: "\<exists>z. \<forall>x. x < z \<longrightarrow> (x = t \<longleftrightarrow> False)" by auto
+lemma minf_neq[noatp]: "\<exists>z. \<forall>x. x < z \<longrightarrow> (x \<noteq> t \<longleftrightarrow> True)" by auto
+lemma minf_P[noatp]: "\<exists>z. \<forall>x. x < z \<longrightarrow> (P \<longleftrightarrow> P)" by blast
+
+text{* Theorems for @{text "\<exists>z. \<forall>x. x < z \<longrightarrow> (P x \<longleftrightarrow> P\<^bsub>+\<infinity>\<^esub>)"}*}
+lemma pinf_gt[noatp]: "\<exists>z . \<forall>x. z < x \<longrightarrow> (t < x \<longleftrightarrow> True)" by auto
+lemma pinf_lt[noatp]: "\<exists>z . \<forall>x. z < x \<longrightarrow> (x < t \<longleftrightarrow> False)"
+ by (simp add: not_less) (rule exI[where x="t"], auto simp add: less_le)
+
+lemma pinf_ge[noatp]: "\<exists>z. \<forall>x. z < x \<longrightarrow> (t \<le> x \<longleftrightarrow> True)" by (auto simp add: less_le)
+lemma pinf_le[noatp]: "\<exists>z. \<forall>x. z < x \<longrightarrow> (x \<le> t \<longleftrightarrow> False)"
+ by (auto simp add: less_le not_less not_le)
+lemma pinf_eq[noatp]: "\<exists>z. \<forall>x. z < x \<longrightarrow> (x = t \<longleftrightarrow> False)" by auto
+lemma pinf_neq[noatp]: "\<exists>z. \<forall>x. z < x \<longrightarrow> (x \<noteq> t \<longleftrightarrow> True)" by auto
+lemma pinf_P[noatp]: "\<exists>z. \<forall>x. z < x \<longrightarrow> (P \<longleftrightarrow> P)" by blast
+
+lemma nmi_lt[noatp]: "t \<in> U \<Longrightarrow> \<forall>x. \<not>True \<and> x < t \<longrightarrow> (\<exists> u\<in> U. u \<le> x)" by auto
+lemma nmi_gt[noatp]: "t \<in> U \<Longrightarrow> \<forall>x. \<not>False \<and> t < x \<longrightarrow> (\<exists> u\<in> U. u \<le> x)"
+ by (auto simp add: le_less)
+lemma nmi_le[noatp]: "t \<in> U \<Longrightarrow> \<forall>x. \<not>True \<and> x\<le> t \<longrightarrow> (\<exists> u\<in> U. u \<le> x)" by auto
+lemma nmi_ge[noatp]: "t \<in> U \<Longrightarrow> \<forall>x. \<not>False \<and> t\<le> x \<longrightarrow> (\<exists> u\<in> U. u \<le> x)" by auto
+lemma nmi_eq[noatp]: "t \<in> U \<Longrightarrow> \<forall>x. \<not>False \<and> x = t \<longrightarrow> (\<exists> u\<in> U. u \<le> x)" by auto
+lemma nmi_neq[noatp]: "t \<in> U \<Longrightarrow>\<forall>x. \<not>True \<and> x \<noteq> t \<longrightarrow> (\<exists> u\<in> U. u \<le> x)" by auto
+lemma nmi_P[noatp]: "\<forall> x. ~P \<and> P \<longrightarrow> (\<exists> u\<in> U. u \<le> x)" by auto
+lemma nmi_conj[noatp]: "\<lbrakk>\<forall>x. \<not>P1' \<and> P1 x \<longrightarrow> (\<exists> u\<in> U. u \<le> x) ;
+ \<forall>x. \<not>P2' \<and> P2 x \<longrightarrow> (\<exists> u\<in> U. u \<le> x)\<rbrakk> \<Longrightarrow>
+ \<forall>x. \<not>(P1' \<and> P2') \<and> (P1 x \<and> P2 x) \<longrightarrow> (\<exists> u\<in> U. u \<le> x)" by auto
+lemma nmi_disj[noatp]: "\<lbrakk>\<forall>x. \<not>P1' \<and> P1 x \<longrightarrow> (\<exists> u\<in> U. u \<le> x) ;
+ \<forall>x. \<not>P2' \<and> P2 x \<longrightarrow> (\<exists> u\<in> U. u \<le> x)\<rbrakk> \<Longrightarrow>
+ \<forall>x. \<not>(P1' \<or> P2') \<and> (P1 x \<or> P2 x) \<longrightarrow> (\<exists> u\<in> U. u \<le> x)" by auto
+
+lemma npi_lt[noatp]: "t \<in> U \<Longrightarrow> \<forall>x. \<not>False \<and> x < t \<longrightarrow> (\<exists> u\<in> U. x \<le> u)" by (auto simp add: le_less)
+lemma npi_gt[noatp]: "t \<in> U \<Longrightarrow> \<forall>x. \<not>True \<and> t < x \<longrightarrow> (\<exists> u\<in> U. x \<le> u)" by auto
+lemma npi_le[noatp]: "t \<in> U \<Longrightarrow> \<forall>x. \<not>False \<and> x \<le> t \<longrightarrow> (\<exists> u\<in> U. x \<le> u)" by auto
+lemma npi_ge[noatp]: "t \<in> U \<Longrightarrow> \<forall>x. \<not>True \<and> t \<le> x \<longrightarrow> (\<exists> u\<in> U. x \<le> u)" by auto
+lemma npi_eq[noatp]: "t \<in> U \<Longrightarrow> \<forall>x. \<not>False \<and> x = t \<longrightarrow> (\<exists> u\<in> U. x \<le> u)" by auto
+lemma npi_neq[noatp]: "t \<in> U \<Longrightarrow> \<forall>x. \<not>True \<and> x \<noteq> t \<longrightarrow> (\<exists> u\<in> U. x \<le> u )" by auto
+lemma npi_P[noatp]: "\<forall> x. ~P \<and> P \<longrightarrow> (\<exists> u\<in> U. x \<le> u)" by auto
+lemma npi_conj[noatp]: "\<lbrakk>\<forall>x. \<not>P1' \<and> P1 x \<longrightarrow> (\<exists> u\<in> U. x \<le> u) ; \<forall>x. \<not>P2' \<and> P2 x \<longrightarrow> (\<exists> u\<in> U. x \<le> u)\<rbrakk>
+ \<Longrightarrow> \<forall>x. \<not>(P1' \<and> P2') \<and> (P1 x \<and> P2 x) \<longrightarrow> (\<exists> u\<in> U. x \<le> u)" by auto
+lemma npi_disj[noatp]: "\<lbrakk>\<forall>x. \<not>P1' \<and> P1 x \<longrightarrow> (\<exists> u\<in> U. x \<le> u) ; \<forall>x. \<not>P2' \<and> P2 x \<longrightarrow> (\<exists> u\<in> U. x \<le> u)\<rbrakk>
+ \<Longrightarrow> \<forall>x. \<not>(P1' \<or> P2') \<and> (P1 x \<or> P2 x) \<longrightarrow> (\<exists> u\<in> U. x \<le> u)" by auto
+
+lemma lin_dense_lt[noatp]: "t \<in> U \<Longrightarrow> \<forall>x l u. (\<forall> t. l < t \<and> t < u \<longrightarrow> t \<notin> U) \<and> l< x \<and> x < u \<and> x < t \<longrightarrow> (\<forall> y. l < y \<and> y < u \<longrightarrow> y < t)"
+proof(clarsimp)
+ fix x l u y assume tU: "t \<in> U" and noU: "\<forall>t. l < t \<and> t < u \<longrightarrow> t \<notin> U" and lx: "l < x"
+ and xu: "x<u" and px: "x < t" and ly: "l<y" and yu:"y < u"
+ from tU noU ly yu have tny: "t\<noteq>y" by auto
+ {assume H: "t < y"
+ from less_trans[OF lx px] less_trans[OF H yu]
+ have "l < t \<and> t < u" by simp
+ with tU noU have "False" by auto}
+ hence "\<not> t < y" by auto hence "y \<le> t" by (simp add: not_less)
+ thus "y < t" using tny by (simp add: less_le)
+qed
+
+lemma lin_dense_gt[noatp]: "t \<in> U \<Longrightarrow> \<forall>x l u. (\<forall> t. l < t \<and> t< u \<longrightarrow> t \<notin> U) \<and> l < x \<and> x < u \<and> t < x \<longrightarrow> (\<forall> y. l < y \<and> y < u \<longrightarrow> t < y)"
+proof(clarsimp)
+ fix x l u y
+ assume tU: "t \<in> U" and noU: "\<forall>t. l < t \<and> t < u \<longrightarrow> t \<notin> U" and lx: "l < x" and xu: "x<u"
+ and px: "t < x" and ly: "l<y" and yu:"y < u"
+ from tU noU ly yu have tny: "t\<noteq>y" by auto
+ {assume H: "y< t"
+ from less_trans[OF ly H] less_trans[OF px xu] have "l < t \<and> t < u" by simp
+ with tU noU have "False" by auto}
+ hence "\<not> y<t" by auto hence "t \<le> y" by (auto simp add: not_less)
+ thus "t < y" using tny by (simp add:less_le)
+qed
+
+lemma lin_dense_le[noatp]: "t \<in> U \<Longrightarrow> \<forall>x l u. (\<forall> t. l < t \<and> t< u \<longrightarrow> t \<notin> U) \<and> l< x \<and> x < u \<and> x \<le> t \<longrightarrow> (\<forall> y. l < y \<and> y < u \<longrightarrow> y\<le> t)"
+proof(clarsimp)
+ fix x l u y
+ assume tU: "t \<in> U" and noU: "\<forall>t. l < t \<and> t < u \<longrightarrow> t \<notin> U" and lx: "l < x" and xu: "x<u"
+ and px: "x \<le> t" and ly: "l<y" and yu:"y < u"
+ from tU noU ly yu have tny: "t\<noteq>y" by auto
+ {assume H: "t < y"
+ from less_le_trans[OF lx px] less_trans[OF H yu]
+ have "l < t \<and> t < u" by simp
+ with tU noU have "False" by auto}
+ hence "\<not> t < y" by auto thus "y \<le> t" by (simp add: not_less)
+qed
+
+lemma lin_dense_ge[noatp]: "t \<in> U \<Longrightarrow> \<forall>x l u. (\<forall> t. l < t \<and> t< u \<longrightarrow> t \<notin> U) \<and> l< x \<and> x < u \<and> t \<le> x \<longrightarrow> (\<forall> y. l < y \<and> y < u \<longrightarrow> t \<le> y)"
+proof(clarsimp)
+ fix x l u y
+ assume tU: "t \<in> U" and noU: "\<forall>t. l < t \<and> t < u \<longrightarrow> t \<notin> U" and lx: "l < x" and xu: "x<u"
+ and px: "t \<le> x" and ly: "l<y" and yu:"y < u"
+ from tU noU ly yu have tny: "t\<noteq>y" by auto
+ {assume H: "y< t"
+ from less_trans[OF ly H] le_less_trans[OF px xu]
+ have "l < t \<and> t < u" by simp
+ with tU noU have "False" by auto}
+ hence "\<not> y<t" by auto thus "t \<le> y" by (simp add: not_less)
+qed
+lemma lin_dense_eq[noatp]: "t \<in> U \<Longrightarrow> \<forall>x l u. (\<forall> t. l < t \<and> t< u \<longrightarrow> t \<notin> U) \<and> l< x \<and> x < u \<and> x = t \<longrightarrow> (\<forall> y. l < y \<and> y < u \<longrightarrow> y= t)" by auto
+lemma lin_dense_neq[noatp]: "t \<in> U \<Longrightarrow> \<forall>x l u. (\<forall> t. l < t \<and> t< u \<longrightarrow> t \<notin> U) \<and> l< x \<and> x < u \<and> x \<noteq> t \<longrightarrow> (\<forall> y. l < y \<and> y < u \<longrightarrow> y\<noteq> t)" by auto
+lemma lin_dense_P[noatp]: "\<forall>x l u. (\<forall> t. l < t \<and> t< u \<longrightarrow> t \<notin> U) \<and> l< x \<and> x < u \<and> P \<longrightarrow> (\<forall> y. l < y \<and> y < u \<longrightarrow> P)" by auto
+
+lemma lin_dense_conj[noatp]:
+ "\<lbrakk>\<forall>x l u. (\<forall> t. l < t \<and> t< u \<longrightarrow> t \<notin> U) \<and> l< x \<and> x < u \<and> P1 x
+ \<longrightarrow> (\<forall> y. l < y \<and> y < u \<longrightarrow> P1 y) ;
+ \<forall>x l u. (\<forall> t. l < t \<and> t< u \<longrightarrow> t \<notin> U) \<and> l< x \<and> x < u \<and> P2 x
+ \<longrightarrow> (\<forall> y. l < y \<and> y < u \<longrightarrow> P2 y)\<rbrakk> \<Longrightarrow>
+ \<forall>x l u. (\<forall> t. l < t \<and> t< u \<longrightarrow> t \<notin> U) \<and> l< x \<and> x < u \<and> (P1 x \<and> P2 x)
+ \<longrightarrow> (\<forall> y. l < y \<and> y < u \<longrightarrow> (P1 y \<and> P2 y))"
+ by blast
+lemma lin_dense_disj[noatp]:
+ "\<lbrakk>\<forall>x l u. (\<forall> t. l < t \<and> t< u \<longrightarrow> t \<notin> U) \<and> l< x \<and> x < u \<and> P1 x
+ \<longrightarrow> (\<forall> y. l < y \<and> y < u \<longrightarrow> P1 y) ;
+ \<forall>x l u. (\<forall> t. l < t \<and> t< u \<longrightarrow> t \<notin> U) \<and> l< x \<and> x < u \<and> P2 x
+ \<longrightarrow> (\<forall> y. l < y \<and> y < u \<longrightarrow> P2 y)\<rbrakk> \<Longrightarrow>
+ \<forall>x l u. (\<forall> t. l < t \<and> t< u \<longrightarrow> t \<notin> U) \<and> l< x \<and> x < u \<and> (P1 x \<or> P2 x)
+ \<longrightarrow> (\<forall> y. l < y \<and> y < u \<longrightarrow> (P1 y \<or> P2 y))"
+ by blast
+
+lemma npmibnd[noatp]: "\<lbrakk>\<forall>x. \<not> MP \<and> P x \<longrightarrow> (\<exists> u\<in> U. u \<le> x); \<forall>x. \<not>PP \<and> P x \<longrightarrow> (\<exists> u\<in> U. x \<le> u)\<rbrakk>
+ \<Longrightarrow> \<forall>x. \<not> MP \<and> \<not>PP \<and> P x \<longrightarrow> (\<exists> u\<in> U. \<exists> u' \<in> U. u \<le> x \<and> x \<le> u')"
+by auto
+
+lemma finite_set_intervals[noatp]:
+ assumes px: "P x" and lx: "l \<le> x" and xu: "x \<le> u" and linS: "l\<in> S"
+ and uinS: "u \<in> S" and fS:"finite S" and lS: "\<forall> x\<in> S. l \<le> x" and Su: "\<forall> x\<in> S. x \<le> u"
+ shows "\<exists> a \<in> S. \<exists> b \<in> S. (\<forall> y. a < y \<and> y < b \<longrightarrow> y \<notin> S) \<and> a \<le> x \<and> x \<le> b \<and> P x"
+proof-
+ let ?Mx = "{y. y\<in> S \<and> y \<le> x}"
+ let ?xM = "{y. y\<in> S \<and> x \<le> y}"
+ let ?a = "Max ?Mx"
+ let ?b = "Min ?xM"
+ have MxS: "?Mx \<subseteq> S" by blast
+ hence fMx: "finite ?Mx" using fS finite_subset by auto
+ from lx linS have linMx: "l \<in> ?Mx" by blast
+ hence Mxne: "?Mx \<noteq> {}" by blast
+ have xMS: "?xM \<subseteq> S" by blast
+ hence fxM: "finite ?xM" using fS finite_subset by auto
+ from xu uinS have linxM: "u \<in> ?xM" by blast
+ hence xMne: "?xM \<noteq> {}" by blast
+ have ax:"?a \<le> x" using Mxne fMx by auto
+ have xb:"x \<le> ?b" using xMne fxM by auto
+ have "?a \<in> ?Mx" using Max_in[OF fMx Mxne] by simp hence ainS: "?a \<in> S" using MxS by blast
+ have "?b \<in> ?xM" using Min_in[OF fxM xMne] by simp hence binS: "?b \<in> S" using xMS by blast
+ have noy:"\<forall> y. ?a < y \<and> y < ?b \<longrightarrow> y \<notin> S"
+ proof(clarsimp)
+ fix y assume ay: "?a < y" and yb: "y < ?b" and yS: "y \<in> S"
+ from yS have "y\<in> ?Mx \<or> y\<in> ?xM" by (auto simp add: linear)
+ moreover {assume "y \<in> ?Mx" hence "y \<le> ?a" using Mxne fMx by auto with ay have "False" by (simp add: not_le[symmetric])}
+ moreover {assume "y \<in> ?xM" hence "?b \<le> y" using xMne fxM by auto with yb have "False" by (simp add: not_le[symmetric])}
+ ultimately show "False" by blast
+ qed
+ from ainS binS noy ax xb px show ?thesis by blast
+qed
+
+lemma finite_set_intervals2[noatp]:
+ assumes px: "P x" and lx: "l \<le> x" and xu: "x \<le> u" and linS: "l\<in> S"
+ and uinS: "u \<in> S" and fS:"finite S" and lS: "\<forall> x\<in> S. l \<le> x" and Su: "\<forall> x\<in> S. x \<le> u"
+ shows "(\<exists> s\<in> S. P s) \<or> (\<exists> a \<in> S. \<exists> b \<in> S. (\<forall> y. a < y \<and> y < b \<longrightarrow> y \<notin> S) \<and> a < x \<and> x < b \<and> P x)"
+proof-
+ from finite_set_intervals[where P="P", OF px lx xu linS uinS fS lS Su]
+ obtain a and b where
+ as: "a\<in> S" and bs: "b\<in> S" and noS:"\<forall>y. a < y \<and> y < b \<longrightarrow> y \<notin> S"
+ and axb: "a \<le> x \<and> x \<le> b \<and> P x" by auto
+ from axb have "x= a \<or> x= b \<or> (a < x \<and> x < b)" by (auto simp add: le_less)
+ thus ?thesis using px as bs noS by blast
+qed
+
+end
+
+section {* The classical QE after Langford for dense linear orders *}
+
+context dense_linear_order
+begin
+
+lemma interval_empty_iff:
+ "{y. x < y \<and> y < z} = {} \<longleftrightarrow> \<not> x < z"
+ by (auto dest: dense)
+
+lemma dlo_qe_bnds[noatp]:
+ assumes ne: "L \<noteq> {}" and neU: "U \<noteq> {}" and fL: "finite L" and fU: "finite U"
+ shows "(\<exists>x. (\<forall>y \<in> L. y < x) \<and> (\<forall>y \<in> U. x < y)) \<equiv> (\<forall> l \<in> L. \<forall>u \<in> U. l < u)"
+proof (simp only: atomize_eq, rule iffI)
+ assume H: "\<exists>x. (\<forall>y\<in>L. y < x) \<and> (\<forall>y\<in>U. x < y)"
+ then obtain x where xL: "\<forall>y\<in>L. y < x" and xU: "\<forall>y\<in>U. x < y" by blast
+ {fix l u assume l: "l \<in> L" and u: "u \<in> U"
+ have "l < x" using xL l by blast
+ also have "x < u" using xU u by blast
+ finally (less_trans) have "l < u" .}
+ thus "\<forall>l\<in>L. \<forall>u\<in>U. l < u" by blast
+next
+ assume H: "\<forall>l\<in>L. \<forall>u\<in>U. l < u"
+ let ?ML = "Max L"
+ let ?MU = "Min U"
+ from fL ne have th1: "?ML \<in> L" and th1': "\<forall>l\<in>L. l \<le> ?ML" by auto
+ from fU neU have th2: "?MU \<in> U" and th2': "\<forall>u\<in>U. ?MU \<le> u" by auto
+ from th1 th2 H have "?ML < ?MU" by auto
+ with dense obtain w where th3: "?ML < w" and th4: "w < ?MU" by blast
+ from th3 th1' have "\<forall>l \<in> L. l < w" by auto
+ moreover from th4 th2' have "\<forall>u \<in> U. w < u" by auto
+ ultimately show "\<exists>x. (\<forall>y\<in>L. y < x) \<and> (\<forall>y\<in>U. x < y)" by auto
+qed
+
+lemma dlo_qe_noub[noatp]:
+ assumes ne: "L \<noteq> {}" and fL: "finite L"
+ shows "(\<exists>x. (\<forall>y \<in> L. y < x) \<and> (\<forall>y \<in> {}. x < y)) \<equiv> True"
+proof(simp add: atomize_eq)
+ from gt_ex[of "Max L"] obtain M where M: "Max L < M" by blast
+ from ne fL have "\<forall>x \<in> L. x \<le> Max L" by simp
+ with M have "\<forall>x\<in>L. x < M" by (auto intro: le_less_trans)
+ thus "\<exists>x. \<forall>y\<in>L. y < x" by blast
+qed
+
+lemma dlo_qe_nolb[noatp]:
+ assumes ne: "U \<noteq> {}" and fU: "finite U"
+ shows "(\<exists>x. (\<forall>y \<in> {}. y < x) \<and> (\<forall>y \<in> U. x < y)) \<equiv> True"
+proof(simp add: atomize_eq)
+ from lt_ex[of "Min U"] obtain M where M: "M < Min U" by blast
+ from ne fU have "\<forall>x \<in> U. Min U \<le> x" by simp
+ with M have "\<forall>x\<in>U. M < x" by (auto intro: less_le_trans)
+ thus "\<exists>x. \<forall>y\<in>U. x < y" by blast
+qed
+
+lemma exists_neq[noatp]: "\<exists>(x::'a). x \<noteq> t" "\<exists>(x::'a). t \<noteq> x"
+ using gt_ex[of t] by auto
+
+lemmas dlo_simps[noatp] = order_refl less_irrefl not_less not_le exists_neq
+ le_less neq_iff linear less_not_permute
+
+lemma axiom[noatp]: "dense_linear_order (op \<le>) (op <)" by (rule dense_linear_order_axioms)
+lemma atoms[noatp]:
+ shows "TERM (less :: 'a \<Rightarrow> _)"
+ and "TERM (less_eq :: 'a \<Rightarrow> _)"
+ and "TERM (op = :: 'a \<Rightarrow> _)" .
+
+declare axiom[langford qe: dlo_qe_bnds dlo_qe_nolb dlo_qe_noub gather: gather_start gather_simps atoms: atoms]
+declare dlo_simps[langfordsimp]
+
+end
+
+(* FIXME: Move to HOL -- together with the conj_aci_rule in langford.ML *)
+lemma dnf[noatp]:
+ "(P & (Q | R)) = ((P&Q) | (P&R))"
+ "((Q | R) & P) = ((Q&P) | (R&P))"
+ by blast+
+
+lemmas weak_dnf_simps[noatp] = simp_thms dnf
+
+lemma nnf_simps[noatp]:
+ "(\<not>(P \<and> Q)) = (\<not>P \<or> \<not>Q)" "(\<not>(P \<or> Q)) = (\<not>P \<and> \<not>Q)" "(P \<longrightarrow> Q) = (\<not>P \<or> Q)"
+ "(P = Q) = ((P \<and> Q) \<or> (\<not>P \<and> \<not> Q))" "(\<not> \<not>(P)) = P"
+ by blast+
+
+lemma ex_distrib[noatp]: "(\<exists>x. P x \<or> Q x) \<longleftrightarrow> ((\<exists>x. P x) \<or> (\<exists>x. Q x))" by blast
+
+lemmas dnf_simps[noatp] = weak_dnf_simps nnf_simps ex_distrib
+
+use "~~/src/HOL/Tools/Qelim/langford.ML"
+method_setup dlo = {*
+ Method.ctxt_args (Method.SIMPLE_METHOD' o LangfordQE.dlo_tac)
+*} "Langford's algorithm for quantifier elimination in dense linear orders"
+
+
+section {* Contructive dense linear orders yield QE for linear arithmetic over ordered Fields -- see @{text "Arith_Tools.thy"} *}
+
+text {* Linear order without upper bounds *}
+
+locale linorder_stupid_syntax = linorder
+begin
+notation
+ less_eq ("op \<sqsubseteq>") and
+ less_eq ("(_/ \<sqsubseteq> _)" [51, 51] 50) and
+ less ("op \<sqsubset>") and
+ less ("(_/ \<sqsubset> _)" [51, 51] 50)
+
+end
+
+locale linorder_no_ub = linorder_stupid_syntax +
+ assumes gt_ex: "\<exists>y. less x y"
+begin
+lemma ge_ex[noatp]: "\<exists>y. x \<sqsubseteq> y" using gt_ex by auto
+
+text {* Theorems for @{text "\<exists>z. \<forall>x. z \<sqsubset> x \<longrightarrow> (P x \<longleftrightarrow> P\<^bsub>+\<infinity>\<^esub>)"} *}
+lemma pinf_conj[noatp]:
+ assumes ex1: "\<exists>z1. \<forall>x. z1 \<sqsubset> x \<longrightarrow> (P1 x \<longleftrightarrow> P1')"
+ and ex2: "\<exists>z2. \<forall>x. z2 \<sqsubset> x \<longrightarrow> (P2 x \<longleftrightarrow> P2')"
+ shows "\<exists>z. \<forall>x. z \<sqsubset> x \<longrightarrow> ((P1 x \<and> P2 x) \<longleftrightarrow> (P1' \<and> P2'))"
+proof-
+ from ex1 ex2 obtain z1 and z2 where z1: "\<forall>x. z1 \<sqsubset> x \<longrightarrow> (P1 x \<longleftrightarrow> P1')"
+ and z2: "\<forall>x. z2 \<sqsubset> x \<longrightarrow> (P2 x \<longleftrightarrow> P2')" by blast
+ from gt_ex obtain z where z:"ord.max less_eq z1 z2 \<sqsubset> z" by blast
+ from z have zz1: "z1 \<sqsubset> z" and zz2: "z2 \<sqsubset> z" by simp_all
+ {fix x assume H: "z \<sqsubset> x"
+ from less_trans[OF zz1 H] less_trans[OF zz2 H]
+ have "(P1 x \<and> P2 x) \<longleftrightarrow> (P1' \<and> P2')" using z1 zz1 z2 zz2 by auto
+ }
+ thus ?thesis by blast
+qed
+
+lemma pinf_disj[noatp]:
+ assumes ex1: "\<exists>z1. \<forall>x. z1 \<sqsubset> x \<longrightarrow> (P1 x \<longleftrightarrow> P1')"
+ and ex2: "\<exists>z2. \<forall>x. z2 \<sqsubset> x \<longrightarrow> (P2 x \<longleftrightarrow> P2')"
+ shows "\<exists>z. \<forall>x. z \<sqsubset> x \<longrightarrow> ((P1 x \<or> P2 x) \<longleftrightarrow> (P1' \<or> P2'))"
+proof-
+ from ex1 ex2 obtain z1 and z2 where z1: "\<forall>x. z1 \<sqsubset> x \<longrightarrow> (P1 x \<longleftrightarrow> P1')"
+ and z2: "\<forall>x. z2 \<sqsubset> x \<longrightarrow> (P2 x \<longleftrightarrow> P2')" by blast
+ from gt_ex obtain z where z:"ord.max less_eq z1 z2 \<sqsubset> z" by blast
+ from z have zz1: "z1 \<sqsubset> z" and zz2: "z2 \<sqsubset> z" by simp_all
+ {fix x assume H: "z \<sqsubset> x"
+ from less_trans[OF zz1 H] less_trans[OF zz2 H]
+ have "(P1 x \<or> P2 x) \<longleftrightarrow> (P1' \<or> P2')" using z1 zz1 z2 zz2 by auto
+ }
+ thus ?thesis by blast
+qed
+
+lemma pinf_ex[noatp]: assumes ex:"\<exists>z. \<forall>x. z \<sqsubset> x \<longrightarrow> (P x \<longleftrightarrow> P1)" and p1: P1 shows "\<exists> x. P x"
+proof-
+ from ex obtain z where z: "\<forall>x. z \<sqsubset> x \<longrightarrow> (P x \<longleftrightarrow> P1)" by blast
+ from gt_ex obtain x where x: "z \<sqsubset> x" by blast
+ from z x p1 show ?thesis by blast
+qed
+
+end
+
+text {* Linear order without upper bounds *}
+
+locale linorder_no_lb = linorder_stupid_syntax +
+ assumes lt_ex: "\<exists>y. less y x"
+begin
+lemma le_ex[noatp]: "\<exists>y. y \<sqsubseteq> x" using lt_ex by auto
+
+
+text {* Theorems for @{text "\<exists>z. \<forall>x. x \<sqsubset> z \<longrightarrow> (P x \<longleftrightarrow> P\<^bsub>-\<infinity>\<^esub>)"} *}
+lemma minf_conj[noatp]:
+ assumes ex1: "\<exists>z1. \<forall>x. x \<sqsubset> z1 \<longrightarrow> (P1 x \<longleftrightarrow> P1')"
+ and ex2: "\<exists>z2. \<forall>x. x \<sqsubset> z2 \<longrightarrow> (P2 x \<longleftrightarrow> P2')"
+ shows "\<exists>z. \<forall>x. x \<sqsubset> z \<longrightarrow> ((P1 x \<and> P2 x) \<longleftrightarrow> (P1' \<and> P2'))"
+proof-
+ from ex1 ex2 obtain z1 and z2 where z1: "\<forall>x. x \<sqsubset> z1 \<longrightarrow> (P1 x \<longleftrightarrow> P1')"and z2: "\<forall>x. x \<sqsubset> z2 \<longrightarrow> (P2 x \<longleftrightarrow> P2')" by blast
+ from lt_ex obtain z where z:"z \<sqsubset> ord.min less_eq z1 z2" by blast
+ from z have zz1: "z \<sqsubset> z1" and zz2: "z \<sqsubset> z2" by simp_all
+ {fix x assume H: "x \<sqsubset> z"
+ from less_trans[OF H zz1] less_trans[OF H zz2]
+ have "(P1 x \<and> P2 x) \<longleftrightarrow> (P1' \<and> P2')" using z1 zz1 z2 zz2 by auto
+ }
+ thus ?thesis by blast
+qed
+
+lemma minf_disj[noatp]:
+ assumes ex1: "\<exists>z1. \<forall>x. x \<sqsubset> z1 \<longrightarrow> (P1 x \<longleftrightarrow> P1')"
+ and ex2: "\<exists>z2. \<forall>x. x \<sqsubset> z2 \<longrightarrow> (P2 x \<longleftrightarrow> P2')"
+ shows "\<exists>z. \<forall>x. x \<sqsubset> z \<longrightarrow> ((P1 x \<or> P2 x) \<longleftrightarrow> (P1' \<or> P2'))"
+proof-
+ from ex1 ex2 obtain z1 and z2 where z1: "\<forall>x. x \<sqsubset> z1 \<longrightarrow> (P1 x \<longleftrightarrow> P1')"and z2: "\<forall>x. x \<sqsubset> z2 \<longrightarrow> (P2 x \<longleftrightarrow> P2')" by blast
+ from lt_ex obtain z where z:"z \<sqsubset> ord.min less_eq z1 z2" by blast
+ from z have zz1: "z \<sqsubset> z1" and zz2: "z \<sqsubset> z2" by simp_all
+ {fix x assume H: "x \<sqsubset> z"
+ from less_trans[OF H zz1] less_trans[OF H zz2]
+ have "(P1 x \<or> P2 x) \<longleftrightarrow> (P1' \<or> P2')" using z1 zz1 z2 zz2 by auto
+ }
+ thus ?thesis by blast
+qed
+
+lemma minf_ex[noatp]: assumes ex:"\<exists>z. \<forall>x. x \<sqsubset> z \<longrightarrow> (P x \<longleftrightarrow> P1)" and p1: P1 shows "\<exists> x. P x"
+proof-
+ from ex obtain z where z: "\<forall>x. x \<sqsubset> z \<longrightarrow> (P x \<longleftrightarrow> P1)" by blast
+ from lt_ex obtain x where x: "x \<sqsubset> z" by blast
+ from z x p1 show ?thesis by blast
+qed
+
+end
+
+
+locale constr_dense_linear_order = linorder_no_lb + linorder_no_ub +
+ fixes between
+ assumes between_less: "less x y \<Longrightarrow> less x (between x y) \<and> less (between x y) y"
+ and between_same: "between x x = x"
+
+sublocale constr_dense_linear_order < dense_linear_order
+ apply unfold_locales
+ using gt_ex lt_ex between_less
+ by (auto, rule_tac x="between x y" in exI, simp)
+
+context constr_dense_linear_order
+begin
+
+lemma rinf_U[noatp]:
+ assumes fU: "finite U"
+ and lin_dense: "\<forall>x l u. (\<forall> t. l \<sqsubset> t \<and> t\<sqsubset> u \<longrightarrow> t \<notin> U) \<and> l\<sqsubset> x \<and> x \<sqsubset> u \<and> P x
+ \<longrightarrow> (\<forall> y. l \<sqsubset> y \<and> y \<sqsubset> u \<longrightarrow> P y )"
+ and nmpiU: "\<forall>x. \<not> MP \<and> \<not>PP \<and> P x \<longrightarrow> (\<exists> u\<in> U. \<exists> u' \<in> U. u \<sqsubseteq> x \<and> x \<sqsubseteq> u')"
+ and nmi: "\<not> MP" and npi: "\<not> PP" and ex: "\<exists> x. P x"
+ shows "\<exists> u\<in> U. \<exists> u' \<in> U. P (between u u')"
+proof-
+ from ex obtain x where px: "P x" by blast
+ from px nmi npi nmpiU have "\<exists> u\<in> U. \<exists> u' \<in> U. u \<sqsubseteq> x \<and> x \<sqsubseteq> u'" by auto
+ then obtain u and u' where uU:"u\<in> U" and uU': "u' \<in> U" and ux:"u \<sqsubseteq> x" and xu':"x \<sqsubseteq> u'" by auto
+ from uU have Une: "U \<noteq> {}" by auto
+ term "linorder.Min less_eq"
+ let ?l = "linorder.Min less_eq U"
+ let ?u = "linorder.Max less_eq U"
+ have linM: "?l \<in> U" using fU Une by simp
+ have uinM: "?u \<in> U" using fU Une by simp
+ have lM: "\<forall> t\<in> U. ?l \<sqsubseteq> t" using Une fU by auto
+ have Mu: "\<forall> t\<in> U. t \<sqsubseteq> ?u" using Une fU by auto
+ have th:"?l \<sqsubseteq> u" using uU Une lM by auto
+ from order_trans[OF th ux] have lx: "?l \<sqsubseteq> x" .
+ have th: "u' \<sqsubseteq> ?u" using uU' Une Mu by simp
+ from order_trans[OF xu' th] have xu: "x \<sqsubseteq> ?u" .
+ from finite_set_intervals2[where P="P",OF px lx xu linM uinM fU lM Mu]
+ have "(\<exists> s\<in> U. P s) \<or>
+ (\<exists> t1\<in> U. \<exists> t2 \<in> U. (\<forall> y. t1 \<sqsubset> y \<and> y \<sqsubset> t2 \<longrightarrow> y \<notin> U) \<and> t1 \<sqsubset> x \<and> x \<sqsubset> t2 \<and> P x)" .
+ moreover { fix u assume um: "u\<in>U" and pu: "P u"
+ have "between u u = u" by (simp add: between_same)
+ with um pu have "P (between u u)" by simp
+ with um have ?thesis by blast}
+ moreover{
+ assume "\<exists> t1\<in> U. \<exists> t2 \<in> U. (\<forall> y. t1 \<sqsubset> y \<and> y \<sqsubset> t2 \<longrightarrow> y \<notin> U) \<and> t1 \<sqsubset> x \<and> x \<sqsubset> t2 \<and> P x"
+ then obtain t1 and t2 where t1M: "t1 \<in> U" and t2M: "t2\<in> U"
+ and noM: "\<forall> y. t1 \<sqsubset> y \<and> y \<sqsubset> t2 \<longrightarrow> y \<notin> U" and t1x: "t1 \<sqsubset> x" and xt2: "x \<sqsubset> t2" and px: "P x"
+ by blast
+ from less_trans[OF t1x xt2] have t1t2: "t1 \<sqsubset> t2" .
+ let ?u = "between t1 t2"
+ from between_less t1t2 have t1lu: "t1 \<sqsubset> ?u" and ut2: "?u \<sqsubset> t2" by auto
+ from lin_dense noM t1x xt2 px t1lu ut2 have "P ?u" by blast
+ with t1M t2M have ?thesis by blast}
+ ultimately show ?thesis by blast
+ qed
+
+theorem fr_eq[noatp]:
+ assumes fU: "finite U"
+ and lin_dense: "\<forall>x l u. (\<forall> t. l \<sqsubset> t \<and> t\<sqsubset> u \<longrightarrow> t \<notin> U) \<and> l\<sqsubset> x \<and> x \<sqsubset> u \<and> P x
+ \<longrightarrow> (\<forall> y. l \<sqsubset> y \<and> y \<sqsubset> u \<longrightarrow> P y )"
+ and nmibnd: "\<forall>x. \<not> MP \<and> P x \<longrightarrow> (\<exists> u\<in> U. u \<sqsubseteq> x)"
+ and npibnd: "\<forall>x. \<not>PP \<and> P x \<longrightarrow> (\<exists> u\<in> U. x \<sqsubseteq> u)"
+ and mi: "\<exists>z. \<forall>x. x \<sqsubset> z \<longrightarrow> (P x = MP)" and pi: "\<exists>z. \<forall>x. z \<sqsubset> x \<longrightarrow> (P x = PP)"
+ shows "(\<exists> x. P x) \<equiv> (MP \<or> PP \<or> (\<exists> u \<in> U. \<exists> u'\<in> U. P (between u u')))"
+ (is "_ \<equiv> (_ \<or> _ \<or> ?F)" is "?E \<equiv> ?D")
+proof-
+ {
+ assume px: "\<exists> x. P x"
+ have "MP \<or> PP \<or> (\<not> MP \<and> \<not> PP)" by blast
+ moreover {assume "MP \<or> PP" hence "?D" by blast}
+ moreover {assume nmi: "\<not> MP" and npi: "\<not> PP"
+ from npmibnd[OF nmibnd npibnd]
+ have nmpiU: "\<forall>x. \<not> MP \<and> \<not>PP \<and> P x \<longrightarrow> (\<exists> u\<in> U. \<exists> u' \<in> U. u \<sqsubseteq> x \<and> x \<sqsubseteq> u')" .
+ from rinf_U[OF fU lin_dense nmpiU nmi npi px] have "?D" by blast}
+ ultimately have "?D" by blast}
+ moreover
+ { assume "?D"
+ moreover {assume m:"MP" from minf_ex[OF mi m] have "?E" .}
+ moreover {assume p: "PP" from pinf_ex[OF pi p] have "?E" . }
+ moreover {assume f:"?F" hence "?E" by blast}
+ ultimately have "?E" by blast}
+ ultimately have "?E = ?D" by blast thus "?E \<equiv> ?D" by simp
+qed
+
+lemmas minf_thms[noatp] = minf_conj minf_disj minf_eq minf_neq minf_lt minf_le minf_gt minf_ge minf_P
+lemmas pinf_thms[noatp] = pinf_conj pinf_disj pinf_eq pinf_neq pinf_lt pinf_le pinf_gt pinf_ge pinf_P
+
+lemmas nmi_thms[noatp] = nmi_conj nmi_disj nmi_eq nmi_neq nmi_lt nmi_le nmi_gt nmi_ge nmi_P
+lemmas npi_thms[noatp] = npi_conj npi_disj npi_eq npi_neq npi_lt npi_le npi_gt npi_ge npi_P
+lemmas lin_dense_thms[noatp] = lin_dense_conj lin_dense_disj lin_dense_eq lin_dense_neq lin_dense_lt lin_dense_le lin_dense_gt lin_dense_ge lin_dense_P
+
+lemma ferrack_axiom[noatp]: "constr_dense_linear_order less_eq less between"
+ by (rule constr_dense_linear_order_axioms)
+lemma atoms[noatp]:
+ shows "TERM (less :: 'a \<Rightarrow> _)"
+ and "TERM (less_eq :: 'a \<Rightarrow> _)"
+ and "TERM (op = :: 'a \<Rightarrow> _)" .
+
+declare ferrack_axiom [ferrack minf: minf_thms pinf: pinf_thms
+ nmi: nmi_thms npi: npi_thms lindense:
+ lin_dense_thms qe: fr_eq atoms: atoms]
+
+declaration {*
+let
+fun simps phi = map (Morphism.thm phi) [@{thm "not_less"}, @{thm "not_le"}]
+fun generic_whatis phi =
+ let
+ val [lt, le] = map (Morphism.term phi) [@{term "op \<sqsubset>"}, @{term "op \<sqsubseteq>"}]
+ fun h x t =
+ case term_of t of
+ Const("op =", _)$y$z => if term_of x aconv y then Ferrante_Rackoff_Data.Eq
+ else Ferrante_Rackoff_Data.Nox
+ | @{term "Not"}$(Const("op =", _)$y$z) => if term_of x aconv y then Ferrante_Rackoff_Data.NEq
+ else Ferrante_Rackoff_Data.Nox
+ | b$y$z => if Term.could_unify (b, lt) then
+ if term_of x aconv y then Ferrante_Rackoff_Data.Lt
+ else if term_of x aconv z then Ferrante_Rackoff_Data.Gt
+ else Ferrante_Rackoff_Data.Nox
+ else if Term.could_unify (b, le) then
+ if term_of x aconv y then Ferrante_Rackoff_Data.Le
+ else if term_of x aconv z then Ferrante_Rackoff_Data.Ge
+ else Ferrante_Rackoff_Data.Nox
+ else Ferrante_Rackoff_Data.Nox
+ | _ => Ferrante_Rackoff_Data.Nox
+ in h end
+ fun ss phi = HOL_ss addsimps (simps phi)
+in
+ Ferrante_Rackoff_Data.funs @{thm "ferrack_axiom"}
+ {isolate_conv = K (K (K Thm.reflexive)), whatis = generic_whatis, simpset = ss}
+end
+*}
+
+end
+
+use "~~/src/HOL/Tools/Qelim/ferrante_rackoff.ML"
+
+method_setup ferrack = {*
+ Method.ctxt_args (Method.SIMPLE_METHOD' o FerranteRackoff.dlo_tac)
+*} "Ferrante and Rackoff's algorithm for quantifier elimination in dense linear orders"
+
+subsection {* Ferrante and Rackoff algorithm over ordered fields *}
+
+lemma neg_prod_lt:"(c\<Colon>'a\<Colon>ordered_field) < 0 \<Longrightarrow> ((c*x < 0) == (x > 0))"
+proof-
+ assume H: "c < 0"
+ have "c*x < 0 = (0/c < x)" by (simp only: neg_divide_less_eq[OF H] algebra_simps)
+ also have "\<dots> = (0 < x)" by simp
+ finally show "(c*x < 0) == (x > 0)" by simp
+qed
+
+lemma pos_prod_lt:"(c\<Colon>'a\<Colon>ordered_field) > 0 \<Longrightarrow> ((c*x < 0) == (x < 0))"
+proof-
+ assume H: "c > 0"
+ hence "c*x < 0 = (0/c > x)" by (simp only: pos_less_divide_eq[OF H] algebra_simps)
+ also have "\<dots> = (0 > x)" by simp
+ finally show "(c*x < 0) == (x < 0)" by simp
+qed
+
+lemma neg_prod_sum_lt: "(c\<Colon>'a\<Colon>ordered_field) < 0 \<Longrightarrow> ((c*x + t< 0) == (x > (- 1/c)*t))"
+proof-
+ assume H: "c < 0"
+ have "c*x + t< 0 = (c*x < -t)" by (subst less_iff_diff_less_0 [of "c*x" "-t"], simp)
+ also have "\<dots> = (-t/c < x)" by (simp only: neg_divide_less_eq[OF H] algebra_simps)
+ also have "\<dots> = ((- 1/c)*t < x)" by simp
+ finally show "(c*x + t < 0) == (x > (- 1/c)*t)" by simp
+qed
+
+lemma pos_prod_sum_lt:"(c\<Colon>'a\<Colon>ordered_field) > 0 \<Longrightarrow> ((c*x + t < 0) == (x < (- 1/c)*t))"
+proof-
+ assume H: "c > 0"
+ have "c*x + t< 0 = (c*x < -t)" by (subst less_iff_diff_less_0 [of "c*x" "-t"], simp)
+ also have "\<dots> = (-t/c > x)" by (simp only: pos_less_divide_eq[OF H] algebra_simps)
+ also have "\<dots> = ((- 1/c)*t > x)" by simp
+ finally show "(c*x + t < 0) == (x < (- 1/c)*t)" by simp
+qed
+
+lemma sum_lt:"((x::'a::pordered_ab_group_add) + t < 0) == (x < - t)"
+ using less_diff_eq[where a= x and b=t and c=0] by simp
+
+lemma neg_prod_le:"(c\<Colon>'a\<Colon>ordered_field) < 0 \<Longrightarrow> ((c*x <= 0) == (x >= 0))"
+proof-
+ assume H: "c < 0"
+ have "c*x <= 0 = (0/c <= x)" by (simp only: neg_divide_le_eq[OF H] algebra_simps)
+ also have "\<dots> = (0 <= x)" by simp
+ finally show "(c*x <= 0) == (x >= 0)" by simp
+qed
+
+lemma pos_prod_le:"(c\<Colon>'a\<Colon>ordered_field) > 0 \<Longrightarrow> ((c*x <= 0) == (x <= 0))"
+proof-
+ assume H: "c > 0"
+ hence "c*x <= 0 = (0/c >= x)" by (simp only: pos_le_divide_eq[OF H] algebra_simps)
+ also have "\<dots> = (0 >= x)" by simp
+ finally show "(c*x <= 0) == (x <= 0)" by simp
+qed
+
+lemma neg_prod_sum_le: "(c\<Colon>'a\<Colon>ordered_field) < 0 \<Longrightarrow> ((c*x + t <= 0) == (x >= (- 1/c)*t))"
+proof-
+ assume H: "c < 0"
+ have "c*x + t <= 0 = (c*x <= -t)" by (subst le_iff_diff_le_0 [of "c*x" "-t"], simp)
+ also have "\<dots> = (-t/c <= x)" by (simp only: neg_divide_le_eq[OF H] algebra_simps)
+ also have "\<dots> = ((- 1/c)*t <= x)" by simp
+ finally show "(c*x + t <= 0) == (x >= (- 1/c)*t)" by simp
+qed
+
+lemma pos_prod_sum_le:"(c\<Colon>'a\<Colon>ordered_field) > 0 \<Longrightarrow> ((c*x + t <= 0) == (x <= (- 1/c)*t))"
+proof-
+ assume H: "c > 0"
+ have "c*x + t <= 0 = (c*x <= -t)" by (subst le_iff_diff_le_0 [of "c*x" "-t"], simp)
+ also have "\<dots> = (-t/c >= x)" by (simp only: pos_le_divide_eq[OF H] algebra_simps)
+ also have "\<dots> = ((- 1/c)*t >= x)" by simp
+ finally show "(c*x + t <= 0) == (x <= (- 1/c)*t)" by simp
+qed
+
+lemma sum_le:"((x::'a::pordered_ab_group_add) + t <= 0) == (x <= - t)"
+ using le_diff_eq[where a= x and b=t and c=0] by simp
+
+lemma nz_prod_eq:"(c\<Colon>'a\<Colon>ordered_field) \<noteq> 0 \<Longrightarrow> ((c*x = 0) == (x = 0))" by simp
+lemma nz_prod_sum_eq: "(c\<Colon>'a\<Colon>ordered_field) \<noteq> 0 \<Longrightarrow> ((c*x + t = 0) == (x = (- 1/c)*t))"
+proof-
+ assume H: "c \<noteq> 0"
+ have "c*x + t = 0 = (c*x = -t)" by (subst eq_iff_diff_eq_0 [of "c*x" "-t"], simp)
+ also have "\<dots> = (x = -t/c)" by (simp only: nonzero_eq_divide_eq[OF H] algebra_simps)
+ finally show "(c*x + t = 0) == (x = (- 1/c)*t)" by simp
+qed
+lemma sum_eq:"((x::'a::pordered_ab_group_add) + t = 0) == (x = - t)"
+ using eq_diff_eq[where a= x and b=t and c=0] by simp
+
+
+interpretation class_ordered_field_dense_linear_order!: constr_dense_linear_order
+ "op <=" "op <"
+ "\<lambda> x y. 1/2 * ((x::'a::{ordered_field,recpower,number_ring}) + y)"
+proof (unfold_locales, dlo, dlo, auto)
+ fix x y::'a assume lt: "x < y"
+ from less_half_sum[OF lt] show "x < (x + y) /2" by simp
+next
+ fix x y::'a assume lt: "x < y"
+ from gt_half_sum[OF lt] show "(x + y) /2 < y" by simp
+qed
+
+declaration{*
+let
+fun earlier [] x y = false
+ | earlier (h::t) x y =
+ if h aconvc y then false else if h aconvc x then true else earlier t x y;
+
+fun dest_frac ct = case term_of ct of
+ Const (@{const_name "HOL.divide"},_) $ a $ b=>
+ Rat.rat_of_quotient (snd (HOLogic.dest_number a), snd (HOLogic.dest_number b))
+ | t => Rat.rat_of_int (snd (HOLogic.dest_number t))
+
+fun mk_frac phi cT x =
+ let val (a, b) = Rat.quotient_of_rat x
+ in if b = 1 then Numeral.mk_cnumber cT a
+ else Thm.capply
+ (Thm.capply (Drule.cterm_rule (instantiate' [SOME cT] []) @{cpat "op /"})
+ (Numeral.mk_cnumber cT a))
+ (Numeral.mk_cnumber cT b)
+ end
+
+fun whatis x ct = case term_of ct of
+ Const(@{const_name "HOL.plus"}, _)$(Const(@{const_name "HOL.times"},_)$_$y)$_ =>
+ if y aconv term_of x then ("c*x+t",[(funpow 2 Thm.dest_arg1) ct, Thm.dest_arg ct])
+ else ("Nox",[])
+| Const(@{const_name "HOL.plus"}, _)$y$_ =>
+ if y aconv term_of x then ("x+t",[Thm.dest_arg ct])
+ else ("Nox",[])
+| Const(@{const_name "HOL.times"}, _)$_$y =>
+ if y aconv term_of x then ("c*x",[Thm.dest_arg1 ct])
+ else ("Nox",[])
+| t => if t aconv term_of x then ("x",[]) else ("Nox",[]);
+
+fun xnormalize_conv ctxt [] ct = reflexive ct
+| xnormalize_conv ctxt (vs as (x::_)) ct =
+ case term_of ct of
+ Const(@{const_name HOL.less},_)$_$Const(@{const_name "HOL.zero"},_) =>
+ (case whatis x (Thm.dest_arg1 ct) of
+ ("c*x+t",[c,t]) =>
+ let
+ val cr = dest_frac c
+ val clt = Thm.dest_fun2 ct
+ val cz = Thm.dest_arg ct
+ val neg = cr </ Rat.zero
+ val cthp = Simplifier.rewrite (local_simpset_of ctxt)
+ (Thm.capply @{cterm "Trueprop"}
+ (if neg then Thm.capply (Thm.capply clt c) cz
+ else Thm.capply (Thm.capply clt cz) c))
+ val cth = equal_elim (symmetric cthp) TrueI
+ val th = implies_elim (instantiate' [SOME (ctyp_of_term x)] (map SOME [c,x,t])
+ (if neg then @{thm neg_prod_sum_lt} else @{thm pos_prod_sum_lt})) cth
+ val rth = Conv.fconv_rule (Conv.arg_conv (Conv.binop_conv
+ (Normalizer.semiring_normalize_ord_conv ctxt (earlier vs)))) th
+ in rth end
+ | ("x+t",[t]) =>
+ let
+ val T = ctyp_of_term x
+ val th = instantiate' [SOME T] [SOME x, SOME t] @{thm "sum_lt"}
+ val rth = Conv.fconv_rule (Conv.arg_conv (Conv.binop_conv
+ (Normalizer.semiring_normalize_ord_conv ctxt (earlier vs)))) th
+ in rth end
+ | ("c*x",[c]) =>
+ let
+ val cr = dest_frac c
+ val clt = Thm.dest_fun2 ct
+ val cz = Thm.dest_arg ct
+ val neg = cr </ Rat.zero
+ val cthp = Simplifier.rewrite (local_simpset_of ctxt)
+ (Thm.capply @{cterm "Trueprop"}
+ (if neg then Thm.capply (Thm.capply clt c) cz
+ else Thm.capply (Thm.capply clt cz) c))
+ val cth = equal_elim (symmetric cthp) TrueI
+ val th = implies_elim (instantiate' [SOME (ctyp_of_term x)] (map SOME [c,x])
+ (if neg then @{thm neg_prod_lt} else @{thm pos_prod_lt})) cth
+ val rth = th
+ in rth end
+ | _ => reflexive ct)
+
+
+| Const(@{const_name HOL.less_eq},_)$_$Const(@{const_name "HOL.zero"},_) =>
+ (case whatis x (Thm.dest_arg1 ct) of
+ ("c*x+t",[c,t]) =>
+ let
+ val T = ctyp_of_term x
+ val cr = dest_frac c
+ val clt = Drule.cterm_rule (instantiate' [SOME T] []) @{cpat "op <"}
+ val cz = Thm.dest_arg ct
+ val neg = cr </ Rat.zero
+ val cthp = Simplifier.rewrite (local_simpset_of ctxt)
+ (Thm.capply @{cterm "Trueprop"}
+ (if neg then Thm.capply (Thm.capply clt c) cz
+ else Thm.capply (Thm.capply clt cz) c))
+ val cth = equal_elim (symmetric cthp) TrueI
+ val th = implies_elim (instantiate' [SOME T] (map SOME [c,x,t])
+ (if neg then @{thm neg_prod_sum_le} else @{thm pos_prod_sum_le})) cth
+ val rth = Conv.fconv_rule (Conv.arg_conv (Conv.binop_conv
+ (Normalizer.semiring_normalize_ord_conv ctxt (earlier vs)))) th
+ in rth end
+ | ("x+t",[t]) =>
+ let
+ val T = ctyp_of_term x
+ val th = instantiate' [SOME T] [SOME x, SOME t] @{thm "sum_le"}
+ val rth = Conv.fconv_rule (Conv.arg_conv (Conv.binop_conv
+ (Normalizer.semiring_normalize_ord_conv ctxt (earlier vs)))) th
+ in rth end
+ | ("c*x",[c]) =>
+ let
+ val T = ctyp_of_term x
+ val cr = dest_frac c
+ val clt = Drule.cterm_rule (instantiate' [SOME T] []) @{cpat "op <"}
+ val cz = Thm.dest_arg ct
+ val neg = cr </ Rat.zero
+ val cthp = Simplifier.rewrite (local_simpset_of ctxt)
+ (Thm.capply @{cterm "Trueprop"}
+ (if neg then Thm.capply (Thm.capply clt c) cz
+ else Thm.capply (Thm.capply clt cz) c))
+ val cth = equal_elim (symmetric cthp) TrueI
+ val th = implies_elim (instantiate' [SOME (ctyp_of_term x)] (map SOME [c,x])
+ (if neg then @{thm neg_prod_le} else @{thm pos_prod_le})) cth
+ val rth = th
+ in rth end
+ | _ => reflexive ct)
+
+| Const("op =",_)$_$Const(@{const_name "HOL.zero"},_) =>
+ (case whatis x (Thm.dest_arg1 ct) of
+ ("c*x+t",[c,t]) =>
+ let
+ val T = ctyp_of_term x
+ val cr = dest_frac c
+ val ceq = Thm.dest_fun2 ct
+ val cz = Thm.dest_arg ct
+ val cthp = Simplifier.rewrite (local_simpset_of ctxt)
+ (Thm.capply @{cterm "Trueprop"}
+ (Thm.capply @{cterm "Not"} (Thm.capply (Thm.capply ceq c) cz)))
+ val cth = equal_elim (symmetric cthp) TrueI
+ val th = implies_elim
+ (instantiate' [SOME T] (map SOME [c,x,t]) @{thm nz_prod_sum_eq}) cth
+ val rth = Conv.fconv_rule (Conv.arg_conv (Conv.binop_conv
+ (Normalizer.semiring_normalize_ord_conv ctxt (earlier vs)))) th
+ in rth end
+ | ("x+t",[t]) =>
+ let
+ val T = ctyp_of_term x
+ val th = instantiate' [SOME T] [SOME x, SOME t] @{thm "sum_eq"}
+ val rth = Conv.fconv_rule (Conv.arg_conv (Conv.binop_conv
+ (Normalizer.semiring_normalize_ord_conv ctxt (earlier vs)))) th
+ in rth end
+ | ("c*x",[c]) =>
+ let
+ val T = ctyp_of_term x
+ val cr = dest_frac c
+ val ceq = Thm.dest_fun2 ct
+ val cz = Thm.dest_arg ct
+ val cthp = Simplifier.rewrite (local_simpset_of ctxt)
+ (Thm.capply @{cterm "Trueprop"}
+ (Thm.capply @{cterm "Not"} (Thm.capply (Thm.capply ceq c) cz)))
+ val cth = equal_elim (symmetric cthp) TrueI
+ val rth = implies_elim
+ (instantiate' [SOME T] (map SOME [c,x]) @{thm nz_prod_eq}) cth
+ in rth end
+ | _ => reflexive ct);
+
+local
+ val less_iff_diff_less_0 = mk_meta_eq @{thm "less_iff_diff_less_0"}
+ val le_iff_diff_le_0 = mk_meta_eq @{thm "le_iff_diff_le_0"}
+ val eq_iff_diff_eq_0 = mk_meta_eq @{thm "eq_iff_diff_eq_0"}
+in
+fun field_isolate_conv phi ctxt vs ct = case term_of ct of
+ Const(@{const_name HOL.less},_)$a$b =>
+ let val (ca,cb) = Thm.dest_binop ct
+ val T = ctyp_of_term ca
+ val th = instantiate' [SOME T] [SOME ca, SOME cb] less_iff_diff_less_0
+ val nth = Conv.fconv_rule
+ (Conv.arg_conv (Conv.arg1_conv
+ (Normalizer.semiring_normalize_ord_conv @{context} (earlier vs)))) th
+ val rth = transitive nth (xnormalize_conv ctxt vs (Thm.rhs_of nth))
+ in rth end
+| Const(@{const_name HOL.less_eq},_)$a$b =>
+ let val (ca,cb) = Thm.dest_binop ct
+ val T = ctyp_of_term ca
+ val th = instantiate' [SOME T] [SOME ca, SOME cb] le_iff_diff_le_0
+ val nth = Conv.fconv_rule
+ (Conv.arg_conv (Conv.arg1_conv
+ (Normalizer.semiring_normalize_ord_conv @{context} (earlier vs)))) th
+ val rth = transitive nth (xnormalize_conv ctxt vs (Thm.rhs_of nth))
+ in rth end
+
+| Const("op =",_)$a$b =>
+ let val (ca,cb) = Thm.dest_binop ct
+ val T = ctyp_of_term ca
+ val th = instantiate' [SOME T] [SOME ca, SOME cb] eq_iff_diff_eq_0
+ val nth = Conv.fconv_rule
+ (Conv.arg_conv (Conv.arg1_conv
+ (Normalizer.semiring_normalize_ord_conv @{context} (earlier vs)))) th
+ val rth = transitive nth (xnormalize_conv ctxt vs (Thm.rhs_of nth))
+ in rth end
+| @{term "Not"} $(Const("op =",_)$a$b) => Conv.arg_conv (field_isolate_conv phi ctxt vs) ct
+| _ => reflexive ct
+end;
+
+fun classfield_whatis phi =
+ let
+ fun h x t =
+ case term_of t of
+ Const("op =", _)$y$z => if term_of x aconv y then Ferrante_Rackoff_Data.Eq
+ else Ferrante_Rackoff_Data.Nox
+ | @{term "Not"}$(Const("op =", _)$y$z) => if term_of x aconv y then Ferrante_Rackoff_Data.NEq
+ else Ferrante_Rackoff_Data.Nox
+ | Const(@{const_name HOL.less},_)$y$z =>
+ if term_of x aconv y then Ferrante_Rackoff_Data.Lt
+ else if term_of x aconv z then Ferrante_Rackoff_Data.Gt
+ else Ferrante_Rackoff_Data.Nox
+ | Const (@{const_name HOL.less_eq},_)$y$z =>
+ if term_of x aconv y then Ferrante_Rackoff_Data.Le
+ else if term_of x aconv z then Ferrante_Rackoff_Data.Ge
+ else Ferrante_Rackoff_Data.Nox
+ | _ => Ferrante_Rackoff_Data.Nox
+ in h end;
+fun class_field_ss phi =
+ HOL_basic_ss addsimps ([@{thm "linorder_not_less"}, @{thm "linorder_not_le"}])
+ addsplits [@{thm "abs_split"},@{thm "split_max"}, @{thm "split_min"}]
+
+in
+Ferrante_Rackoff_Data.funs @{thm "class_ordered_field_dense_linear_order.ferrack_axiom"}
+ {isolate_conv = field_isolate_conv, whatis = classfield_whatis, simpset = class_field_ss}
+end
+*}
+
+
+end
--- a/src/HOL/Library/Efficient_Nat.thy Fri Feb 06 15:57:47 2009 +0100
+++ b/src/HOL/Library/Efficient_Nat.thy Fri Feb 06 15:59:49 2009 +0100
@@ -376,12 +376,12 @@
text {* Conversion from and to indices. *}
-code_const index_of_nat
+code_const Code_Index.of_nat
(SML "IntInf.toInt")
(OCaml "Big'_int.int'_of'_big'_int")
(Haskell "fromEnum")
-code_const nat_of_index
+code_const Code_Index.nat_of
(SML "IntInf.fromInt")
(OCaml "Big'_int.big'_int'_of'_int")
(Haskell "toEnum")
--- a/src/HOL/Library/Float.thy Fri Feb 06 15:57:47 2009 +0100
+++ b/src/HOL/Library/Float.thy Fri Feb 06 15:59:49 2009 +0100
@@ -1,30 +1,56 @@
-(* Title: HOL/Real/Float.thy
- Author: Steven Obua
-*)
-
-header {* Floating Point Representation of the Reals *}
-
+(* Title: HOL/Library/Float.thy
+ * Author: Steven Obua 2008
+ * Johannes Hölzl, TU Muenchen <hoelzl@in.tum.de> 2008 / 2009
+ *)
theory Float
imports Complex_Main
-uses "~~/src/Tools/float.ML" ("~~/src/HOL/Tools/float_arith.ML")
begin
definition
pow2 :: "int \<Rightarrow> real" where
- "pow2 a = (if (0 <= a) then (2^(nat a)) else (inverse (2^(nat (-a)))))"
+ [simp]: "pow2 a = (if (0 <= a) then (2^(nat a)) else (inverse (2^(nat (-a)))))"
+
+datatype float = Float int int
+
+fun Ifloat :: "float \<Rightarrow> real" where
+"Ifloat (Float a b) = real a * pow2 b"
-definition
- float :: "int * int \<Rightarrow> real" where
- "float x = real (fst x) * pow2 (snd x)"
+instantiation float :: zero begin
+definition zero_float where "0 = Float 0 0"
+instance ..
+end
+
+instantiation float :: one begin
+definition one_float where "1 = Float 1 0"
+instance ..
+end
+
+instantiation float :: number begin
+definition number_of_float where "number_of n = Float n 0"
+instance ..
+end
-lemma pow2_0[simp]: "pow2 0 = 1"
-by (simp add: pow2_def)
+fun mantissa :: "float \<Rightarrow> int" where
+"mantissa (Float a b) = a"
+
+fun scale :: "float \<Rightarrow> int" where
+"scale (Float a b) = b"
+
+lemma Ifloat_neg_exp: "e < 0 \<Longrightarrow> Ifloat (Float m e) = real m * inverse (2^nat (-e))" by auto
+lemma Ifloat_nge0_exp: "\<not> 0 \<le> e \<Longrightarrow> Ifloat (Float m e) = real m * inverse (2^nat (-e))" by auto
+lemma Ifloat_ge0_exp: "0 \<le> e \<Longrightarrow> Ifloat (Float m e) = real m * (2^nat e)" by auto
-lemma pow2_1[simp]: "pow2 1 = 2"
-by (simp add: pow2_def)
+lemma Float_num[simp]: shows
+ "Ifloat (Float 1 0) = 1" and "Ifloat (Float 1 1) = 2" and "Ifloat (Float 1 2) = 4" and
+ "Ifloat (Float 1 -1) = 1/2" and "Ifloat (Float 1 -2) = 1/4" and "Ifloat (Float 1 -3) = 1/8" and
+ "Ifloat (Float -1 0) = -1" and "Ifloat (Float (number_of n) 0) = number_of n"
+ by auto
-lemma pow2_neg: "pow2 x = inverse (pow2 (-x))"
-by (simp add: pow2_def)
+lemma pow2_0[simp]: "pow2 0 = 1" by simp
+lemma pow2_1[simp]: "pow2 1 = 2" by simp
+lemma pow2_neg: "pow2 x = inverse (pow2 (-x))" by simp
+
+declare pow2_def[simp del]
lemma pow2_add1: "pow2 (1 + a) = 2 * (pow2 a)"
proof -
@@ -96,281 +122,43 @@
qed
qed
-lemma "float (a, e) + float (b, e) = float (a + b, e)"
-by (simp add: float_def algebra_simps)
+lemma float_components[simp]: "Float (mantissa f) (scale f) = f" by (cases f, auto)
+
+lemma float_split: "\<exists> a b. x = Float a b" by (cases x, auto)
-definition
- int_of_real :: "real \<Rightarrow> int" where
- "int_of_real x = (SOME y. real y = x)"
+lemma float_split2: "(\<forall> a b. x \<noteq> Float a b) = False" by (auto simp add: float_split)
+
+lemma float_zero[simp]: "Ifloat (Float 0 e) = 0" by simp
+
+lemma abs_div_2_less: "a \<noteq> 0 \<Longrightarrow> a \<noteq> -1 \<Longrightarrow> abs((a::int) div 2) < abs a"
+by arith
-definition
- real_is_int :: "real \<Rightarrow> bool" where
- "real_is_int x = (EX (u::int). x = real u)"
+function normfloat :: "float \<Rightarrow> float" where
+"normfloat (Float a b) = (if a \<noteq> 0 \<and> even a then normfloat (Float (a div 2) (b+1)) else if a=0 then Float 0 0 else Float a b)"
+by pat_completeness auto
+termination by (relation "measure (nat o abs o mantissa)") (auto intro: abs_div_2_less)
+declare normfloat.simps[simp del]
-lemma real_is_int_def2: "real_is_int x = (x = real (int_of_real x))"
-by (auto simp add: real_is_int_def int_of_real_def)
+theorem normfloat[symmetric, simp]: "Ifloat f = Ifloat (normfloat f)"
+proof (induct f rule: normfloat.induct)
+ case (1 a b)
+ have real2: "2 = real (2::int)"
+ by auto
+ show ?case
+ apply (subst normfloat.simps)
+ apply (auto simp add: float_zero)
+ apply (subst 1[symmetric])
+ apply (auto simp add: pow2_add even_def)
+ done
+qed
-lemma float_transfer: "real_is_int ((real a)*(pow2 c)) \<Longrightarrow> float (a, b) = float (int_of_real ((real a)*(pow2 c)), b - c)"
-by (simp add: float_def real_is_int_def2 pow2_add[symmetric])
+lemma pow2_neq_zero[simp]: "pow2 x \<noteq> 0"
+ by (auto simp add: pow2_def)
lemma pow2_int: "pow2 (int c) = 2^c"
by (simp add: pow2_def)
-lemma float_transfer_nat: "float (a, b) = float (a * 2^c, b - int c)"
-by (simp add: float_def pow2_int[symmetric] pow2_add[symmetric])
-
-lemma real_is_int_real[simp]: "real_is_int (real (x::int))"
-by (auto simp add: real_is_int_def int_of_real_def)
-
-lemma int_of_real_real[simp]: "int_of_real (real x) = x"
-by (simp add: int_of_real_def)
-
-lemma real_int_of_real[simp]: "real_is_int x \<Longrightarrow> real (int_of_real x) = x"
-by (auto simp add: int_of_real_def real_is_int_def)
-
-lemma real_is_int_add_int_of_real: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> (int_of_real (a+b)) = (int_of_real a) + (int_of_real b)"
-by (auto simp add: int_of_real_def real_is_int_def)
-
-lemma real_is_int_add[simp]: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> real_is_int (a+b)"
-apply (subst real_is_int_def2)
-apply (simp add: real_is_int_add_int_of_real real_int_of_real)
-done
-
-lemma int_of_real_sub: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> (int_of_real (a-b)) = (int_of_real a) - (int_of_real b)"
-by (auto simp add: int_of_real_def real_is_int_def)
-
-lemma real_is_int_sub[simp]: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> real_is_int (a-b)"
-apply (subst real_is_int_def2)
-apply (simp add: int_of_real_sub real_int_of_real)
-done
-
-lemma real_is_int_rep: "real_is_int x \<Longrightarrow> ?! (a::int). real a = x"
-by (auto simp add: real_is_int_def)
-
-lemma int_of_real_mult:
- assumes "real_is_int a" "real_is_int b"
- shows "(int_of_real (a*b)) = (int_of_real a) * (int_of_real b)"
-proof -
- from prems have a: "?! (a'::int). real a' = a" by (rule_tac real_is_int_rep, auto)
- from prems have b: "?! (b'::int). real b' = b" by (rule_tac real_is_int_rep, auto)
- from a obtain a'::int where a':"a = real a'" by auto
- from b obtain b'::int where b':"b = real b'" by auto
- have r: "real a' * real b' = real (a' * b')" by auto
- show ?thesis
- apply (simp add: a' b')
- apply (subst r)
- apply (simp only: int_of_real_real)
- done
-qed
-
-lemma real_is_int_mult[simp]: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> real_is_int (a*b)"
-apply (subst real_is_int_def2)
-apply (simp add: int_of_real_mult)
-done
-
-lemma real_is_int_0[simp]: "real_is_int (0::real)"
-by (simp add: real_is_int_def int_of_real_def)
-
-lemma real_is_int_1[simp]: "real_is_int (1::real)"
-proof -
- have "real_is_int (1::real) = real_is_int(real (1::int))" by auto
- also have "\<dots> = True" by (simp only: real_is_int_real)
- ultimately show ?thesis by auto
-qed
-
-lemma real_is_int_n1: "real_is_int (-1::real)"
-proof -
- have "real_is_int (-1::real) = real_is_int(real (-1::int))" by auto
- also have "\<dots> = True" by (simp only: real_is_int_real)
- ultimately show ?thesis by auto
-qed
-
-lemma real_is_int_number_of[simp]: "real_is_int ((number_of \<Colon> int \<Rightarrow> real) x)"
-proof -
- have neg1: "real_is_int (-1::real)"
- proof -
- have "real_is_int (-1::real) = real_is_int(real (-1::int))" by auto
- also have "\<dots> = True" by (simp only: real_is_int_real)
- ultimately show ?thesis by auto
- qed
-
- {
- fix x :: int
- have "real_is_int ((number_of \<Colon> int \<Rightarrow> real) x)"
- unfolding number_of_eq
- apply (induct x)
- apply (induct_tac n)
- apply (simp)
- apply (simp)
- apply (induct_tac n)
- apply (simp add: neg1)
- proof -
- fix n :: nat
- assume rn: "(real_is_int (of_int (- (int (Suc n)))))"
- have s: "-(int (Suc (Suc n))) = -1 + - (int (Suc n))" by simp
- show "real_is_int (of_int (- (int (Suc (Suc n)))))"
- apply (simp only: s of_int_add)
- apply (rule real_is_int_add)
- apply (simp add: neg1)
- apply (simp only: rn)
- done
- qed
- }
- note Abs_Bin = this
- {
- fix x :: int
- have "? u. x = u"
- apply (rule exI[where x = "x"])
- apply (simp)
- done
- }
- then obtain u::int where "x = u" by auto
- with Abs_Bin show ?thesis by auto
-qed
-
-lemma int_of_real_0[simp]: "int_of_real (0::real) = (0::int)"
-by (simp add: int_of_real_def)
-
-lemma int_of_real_1[simp]: "int_of_real (1::real) = (1::int)"
-proof -
- have 1: "(1::real) = real (1::int)" by auto
- show ?thesis by (simp only: 1 int_of_real_real)
-qed
-
-lemma int_of_real_number_of[simp]: "int_of_real (number_of b) = number_of b"
-proof -
- have "real_is_int (number_of b)" by simp
- then have uu: "?! u::int. number_of b = real u" by (auto simp add: real_is_int_rep)
- then obtain u::int where u:"number_of b = real u" by auto
- have "number_of b = real ((number_of b)::int)"
- by (simp add: number_of_eq real_of_int_def)
- have ub: "number_of b = real ((number_of b)::int)"
- by (simp add: number_of_eq real_of_int_def)
- from uu u ub have unb: "u = number_of b"
- by blast
- have "int_of_real (number_of b) = u" by (simp add: u)
- with unb show ?thesis by simp
-qed
-
-lemma float_transfer_even: "even a \<Longrightarrow> float (a, b) = float (a div 2, b+1)"
- apply (subst float_transfer[where a="a" and b="b" and c="-1", simplified])
- apply (simp_all add: pow2_def even_def real_is_int_def algebra_simps)
- apply (auto)
-proof -
- fix q::int
- have a:"b - (-1\<Colon>int) = (1\<Colon>int) + b" by arith
- show "(float (q, (b - (-1\<Colon>int)))) = (float (q, ((1\<Colon>int) + b)))"
- by (simp add: a)
-qed
-
-lemma int_div_zdiv: "int (a div b) = (int a) div (int b)"
-by (rule zdiv_int)
-
-lemma int_mod_zmod: "int (a mod b) = (int a) mod (int b)"
-by (rule zmod_int)
-
-lemma abs_div_2_less: "a \<noteq> 0 \<Longrightarrow> a \<noteq> -1 \<Longrightarrow> abs((a::int) div 2) < abs a"
-by arith
-
-function norm_float :: "int \<Rightarrow> int \<Rightarrow> int \<times> int" where
- "norm_float a b = (if a \<noteq> 0 \<and> even a then norm_float (a div 2) (b + 1)
- else if a = 0 then (0, 0) else (a, b))"
-by auto
-
-termination by (relation "measure (nat o abs o fst)")
- (auto intro: abs_div_2_less)
-
-lemma norm_float: "float x = float (split norm_float x)"
-proof -
- {
- fix a b :: int
- have norm_float_pair: "float (a, b) = float (norm_float a b)"
- proof (induct a b rule: norm_float.induct)
- case (1 u v)
- show ?case
- proof cases
- assume u: "u \<noteq> 0 \<and> even u"
- with prems have ind: "float (u div 2, v + 1) = float (norm_float (u div 2) (v + 1))" by auto
- with u have "float (u,v) = float (u div 2, v+1)" by (simp add: float_transfer_even)
- then show ?thesis
- apply (subst norm_float.simps)
- apply (simp add: ind)
- done
- next
- assume "~(u \<noteq> 0 \<and> even u)"
- then show ?thesis
- by (simp add: prems float_def)
- qed
- qed
- }
- note helper = this
- have "? a b. x = (a,b)" by auto
- then obtain a b where "x = (a, b)" by blast
- then show ?thesis by (simp add: helper)
-qed
-
-lemma float_add_l0: "float (0, e) + x = x"
- by (simp add: float_def)
-
-lemma float_add_r0: "x + float (0, e) = x"
- by (simp add: float_def)
-
-lemma float_add:
- "float (a1, e1) + float (a2, e2) =
- (if e1<=e2 then float (a1+a2*2^(nat(e2-e1)), e1)
- else float (a1*2^(nat (e1-e2))+a2, e2))"
- apply (simp add: float_def algebra_simps)
- apply (auto simp add: pow2_int[symmetric] pow2_add[symmetric])
- done
-
-lemma float_add_assoc1:
- "(x + float (y1, e1)) + float (y2, e2) = (float (y1, e1) + float (y2, e2)) + x"
- by simp
-
-lemma float_add_assoc2:
- "(float (y1, e1) + x) + float (y2, e2) = (float (y1, e1) + float (y2, e2)) + x"
- by simp
-
-lemma float_add_assoc3:
- "float (y1, e1) + (x + float (y2, e2)) = (float (y1, e1) + float (y2, e2)) + x"
- by simp
-
-lemma float_add_assoc4:
- "float (y1, e1) + (float (y2, e2) + x) = (float (y1, e1) + float (y2, e2)) + x"
- by simp
-
-lemma float_mult_l0: "float (0, e) * x = float (0, 0)"
- by (simp add: float_def)
-
-lemma float_mult_r0: "x * float (0, e) = float (0, 0)"
- by (simp add: float_def)
-
-definition
- lbound :: "real \<Rightarrow> real"
-where
- "lbound x = min 0 x"
-
-definition
- ubound :: "real \<Rightarrow> real"
-where
- "ubound x = max 0 x"
-
-lemma lbound: "lbound x \<le> x"
- by (simp add: lbound_def)
-
-lemma ubound: "x \<le> ubound x"
- by (simp add: ubound_def)
-
-lemma float_mult:
- "float (a1, e1) * float (a2, e2) =
- (float (a1 * a2, e1 + e2))"
- by (simp add: float_def pow2_add)
-
-lemma float_minus:
- "- (float (a,b)) = float (-a, b)"
- by (simp add: float_def)
-
-lemma zero_less_pow2:
+lemma zero_less_pow2[simp]:
"0 < pow2 x"
proof -
{
@@ -387,182 +175,1258 @@
done
qed
+lemma normfloat_imp_odd_or_zero: "normfloat f = Float a b \<Longrightarrow> odd a \<or> (a = 0 \<and> b = 0)"
+proof (induct f rule: normfloat.induct)
+ case (1 u v)
+ from 1 have ab: "normfloat (Float u v) = Float a b" by auto
+ {
+ assume eu: "even u"
+ assume z: "u \<noteq> 0"
+ have "normfloat (Float u v) = normfloat (Float (u div 2) (v + 1))"
+ apply (subst normfloat.simps)
+ by (simp add: eu z)
+ with ab have "normfloat (Float (u div 2) (v + 1)) = Float a b" by simp
+ with 1 eu z have ?case by auto
+ }
+ note case1 = this
+ {
+ assume "odd u \<or> u = 0"
+ then have ou: "\<not> (u \<noteq> 0 \<and> even u)" by auto
+ have "normfloat (Float u v) = (if u = 0 then Float 0 0 else Float u v)"
+ apply (subst normfloat.simps)
+ apply (simp add: ou)
+ done
+ with ab have "Float a b = (if u = 0 then Float 0 0 else Float u v)" by auto
+ then have ?case
+ apply (case_tac "u=0")
+ apply (auto)
+ by (insert ou, auto)
+ }
+ note case2 = this
+ show ?case
+ apply (case_tac "odd u \<or> u = 0")
+ apply (rule case2)
+ apply simp
+ apply (rule case1)
+ apply auto
+ done
+qed
+
+lemma float_eq_odd_helper:
+ assumes odd: "odd a'"
+ and floateq: "Ifloat (Float a b) = Ifloat (Float a' b')"
+ shows "b \<le> b'"
+proof -
+ {
+ assume bcmp: "b > b'"
+ from floateq have eq: "real a * pow2 b = real a' * pow2 b'" by simp
+ {
+ fix x y z :: real
+ assume "y \<noteq> 0"
+ then have "(x * inverse y = z) = (x = z * y)"
+ by auto
+ }
+ note inverse = this
+ have eq': "real a * (pow2 (b - b')) = real a'"
+ apply (subst diff_int_def)
+ apply (subst pow2_add)
+ apply (subst pow2_neg[where x = "-b'"])
+ apply simp
+ apply (subst mult_assoc[symmetric])
+ apply (subst inverse)
+ apply (simp_all add: eq)
+ done
+ have "\<exists> z > 0. pow2 (b-b') = 2^z"
+ apply (rule exI[where x="nat (b - b')"])
+ apply (auto)
+ apply (insert bcmp)
+ apply simp
+ apply (subst pow2_int[symmetric])
+ apply auto
+ done
+ then obtain z where z: "z > 0 \<and> pow2 (b-b') = 2^z" by auto
+ with eq' have "real a * 2^z = real a'"
+ by auto
+ then have "real a * real ((2::int)^z) = real a'"
+ by auto
+ then have "real (a * 2^z) = real a'"
+ apply (subst real_of_int_mult)
+ apply simp
+ done
+ then have a'_rep: "a * 2^z = a'" by arith
+ then have "a' = a*2^z" by simp
+ with z have "even a'" by simp
+ with odd have False by auto
+ }
+ then show ?thesis by arith
+qed
+
+lemma float_eq_odd:
+ assumes odd1: "odd a"
+ and odd2: "odd a'"
+ and floateq: "Ifloat (Float a b) = Ifloat (Float a' b')"
+ shows "a = a' \<and> b = b'"
+proof -
+ from
+ float_eq_odd_helper[OF odd2 floateq]
+ float_eq_odd_helper[OF odd1 floateq[symmetric]]
+ have beq: "b = b'" by arith
+ with floateq show ?thesis by auto
+qed
+
+theorem normfloat_unique:
+ assumes Ifloat_eq: "Ifloat f = Ifloat g"
+ shows "normfloat f = normfloat g"
+proof -
+ from float_split[of "normfloat f"] obtain a b where normf:"normfloat f = Float a b" by auto
+ from float_split[of "normfloat g"] obtain a' b' where normg:"normfloat g = Float a' b'" by auto
+ have "Ifloat (normfloat f) = Ifloat (normfloat g)"
+ by (simp add: Ifloat_eq)
+ then have float_eq: "Ifloat (Float a b) = Ifloat (Float a' b')"
+ by (simp add: normf normg)
+ have ab: "odd a \<or> (a = 0 \<and> b = 0)" by (rule normfloat_imp_odd_or_zero[OF normf])
+ have ab': "odd a' \<or> (a' = 0 \<and> b' = 0)" by (rule normfloat_imp_odd_or_zero[OF normg])
+ {
+ assume odd: "odd a"
+ then have "a \<noteq> 0" by (simp add: even_def, arith)
+ with float_eq have "a' \<noteq> 0" by auto
+ with ab' have "odd a'" by simp
+ from odd this float_eq have "a = a' \<and> b = b'" by (rule float_eq_odd)
+ }
+ note odd_case = this
+ {
+ assume even: "even a"
+ with ab have a0: "a = 0" by simp
+ with float_eq have a0': "a' = 0" by auto
+ from a0 a0' ab ab' have "a = a' \<and> b = b'" by auto
+ }
+ note even_case = this
+ from odd_case even_case show ?thesis
+ apply (simp add: normf normg)
+ apply (case_tac "even a")
+ apply auto
+ done
+qed
+
+instantiation float :: plus begin
+fun plus_float where
+[simp del]: "(Float a_m a_e) + (Float b_m b_e) =
+ (if a_e \<le> b_e then Float (a_m + b_m * 2^(nat(b_e - a_e))) a_e
+ else Float (a_m * 2^(nat (a_e - b_e)) + b_m) b_e)"
+instance ..
+end
+
+instantiation float :: uminus begin
+fun uminus_float where [simp del]: "uminus_float (Float m e) = Float (-m) e"
+instance ..
+end
+
+instantiation float :: minus begin
+fun minus_float where [simp del]: "(z::float) - w = z + (- w)"
+instance ..
+end
+
+instantiation float :: times begin
+fun times_float where [simp del]: "(Float a_m a_e) * (Float b_m b_e) = Float (a_m * b_m) (a_e + b_e)"
+instance ..
+end
+
+fun float_pprt :: "float \<Rightarrow> float" where
+"float_pprt (Float a e) = (if 0 <= a then (Float a e) else 0)"
+
+fun float_nprt :: "float \<Rightarrow> float" where
+"float_nprt (Float a e) = (if 0 <= a then 0 else (Float a e))"
+
+instantiation float :: ord begin
+definition le_float_def: "z \<le> w \<equiv> Ifloat z \<le> Ifloat w"
+definition less_float_def: "z < w \<equiv> Ifloat z < Ifloat w"
+instance ..
+end
+
+lemma Ifloat_add[simp]: "Ifloat (a + b) = Ifloat a + Ifloat b"
+ by (cases a, cases b, simp add: algebra_simps plus_float.simps,
+ auto simp add: pow2_int[symmetric] pow2_add[symmetric])
+
+lemma Ifloat_minus[simp]: "Ifloat (- a) = - Ifloat a"
+ by (cases a, simp add: uminus_float.simps)
+
+lemma Ifloat_sub[simp]: "Ifloat (a - b) = Ifloat a - Ifloat b"
+ by (cases a, cases b, simp add: minus_float.simps)
+
+lemma Ifloat_mult[simp]: "Ifloat (a*b) = Ifloat a * Ifloat b"
+ by (cases a, cases b, simp add: times_float.simps pow2_add)
+
+lemma Ifloat_0[simp]: "Ifloat 0 = 0"
+ by (auto simp add: zero_float_def float_zero)
+
+lemma Ifloat_1[simp]: "Ifloat 1 = 1"
+ by (auto simp add: one_float_def)
+
lemma zero_le_float:
- "(0 <= float (a,b)) = (0 <= a)"
- apply (auto simp add: float_def)
- apply (auto simp add: zero_le_mult_iff zero_less_pow2)
+ "(0 <= Ifloat (Float a b)) = (0 <= a)"
+ apply auto
+ apply (auto simp add: zero_le_mult_iff)
apply (insert zero_less_pow2[of b])
apply (simp_all)
done
lemma float_le_zero:
- "(float (a,b) <= 0) = (a <= 0)"
- apply (auto simp add: float_def)
+ "(Ifloat (Float a b) <= 0) = (a <= 0)"
+ apply auto
apply (auto simp add: mult_le_0_iff)
apply (insert zero_less_pow2[of b])
apply auto
done
-lemma float_abs:
- "abs (float (a,b)) = (if 0 <= a then (float (a,b)) else (float (-a,b)))"
- apply (auto simp add: abs_if)
- apply (simp_all add: zero_le_float[symmetric, of a b] float_minus)
+declare Ifloat.simps[simp del]
+
+lemma Ifloat_pprt[simp]: "Ifloat (float_pprt a) = pprt (Ifloat a)"
+ by (cases a, auto simp add: float_pprt.simps zero_le_float float_le_zero float_zero)
+
+lemma Ifloat_nprt[simp]: "Ifloat (float_nprt a) = nprt (Ifloat a)"
+ by (cases a, auto simp add: float_nprt.simps zero_le_float float_le_zero float_zero)
+
+instance float :: ab_semigroup_add
+proof (intro_classes)
+ fix a b c :: float
+ show "a + b + c = a + (b + c)"
+ by (cases a, cases b, cases c, auto simp add: algebra_simps power_add[symmetric] plus_float.simps)
+next
+ fix a b :: float
+ show "a + b = b + a"
+ by (cases a, cases b, simp add: plus_float.simps)
+qed
+
+instance float :: comm_monoid_mult
+proof (intro_classes)
+ fix a b c :: float
+ show "a * b * c = a * (b * c)"
+ by (cases a, cases b, cases c, simp add: times_float.simps)
+next
+ fix a b :: float
+ show "a * b = b * a"
+ by (cases a, cases b, simp add: times_float.simps)
+next
+ fix a :: float
+ show "1 * a = a"
+ by (cases a, simp add: times_float.simps one_float_def)
+qed
+
+(* Floats do NOT form a cancel_semigroup_add: *)
+lemma "0 + Float 0 1 = 0 + Float 0 2"
+ by (simp add: plus_float.simps zero_float_def)
+
+instance float :: comm_semiring
+proof (intro_classes)
+ fix a b c :: float
+ show "(a + b) * c = a * c + b * c"
+ by (cases a, cases b, cases c, simp, simp add: plus_float.simps times_float.simps algebra_simps)
+qed
+
+(* Floats do NOT form an order, because "(x < y) = (x <= y & x <> y)" does NOT hold *)
+
+instance float :: zero_neq_one
+proof (intro_classes)
+ show "(0::float) \<noteq> 1"
+ by (simp add: zero_float_def one_float_def)
+qed
+
+lemma float_le_simp: "((x::float) \<le> y) = (0 \<le> y - x)"
+ by (auto simp add: le_float_def)
+
+lemma float_less_simp: "((x::float) < y) = (0 < y - x)"
+ by (auto simp add: less_float_def)
+
+lemma Ifloat_min: "Ifloat (min x y) = min (Ifloat x) (Ifloat y)" unfolding min_def le_float_def by auto
+lemma Ifloat_max: "Ifloat (max a b) = max (Ifloat a) (Ifloat b)" unfolding max_def le_float_def by auto
+
+instantiation float :: power begin
+fun power_float where [simp del]: "(Float m e) ^ n = Float (m ^ n) (e * int n)"
+instance ..
+end
+
+instance float :: recpower
+proof (intro_classes)
+ fix a :: float show "a ^ 0 = 1" by (cases a, auto simp add: power_float.simps one_float_def)
+next
+ fix a :: float and n :: nat show "a ^ (Suc n) = a * a ^ n"
+ by (cases a, auto simp add: power_float.simps times_float.simps algebra_simps)
+qed
+
+lemma float_power: "Ifloat (x ^ n) = (Ifloat x) ^ n"
+proof (cases x)
+ case (Float m e)
+
+ have "pow2 e ^ n = pow2 (e * int n)"
+ proof (cases "e >= 0")
+ case True hence e_nat: "e = int (nat e)" by auto
+ hence "pow2 e ^ n = (2 ^ nat e) ^ n" using pow2_int[of "nat e"] by auto
+ thus ?thesis unfolding power_mult[symmetric] unfolding pow2_int[symmetric] int_mult e_nat[symmetric] .
+ next
+ case False hence e_minus: "-e = int (nat (-e))" by auto
+ hence "pow2 (-e) ^ n = (2 ^ nat (-e)) ^ n" using pow2_int[of "nat (-e)"] by auto
+ hence "pow2 (-e) ^ n = pow2 ((-e) * int n)" unfolding power_mult[symmetric] unfolding pow2_int[symmetric] int_mult e_minus[symmetric] zmult_zminus .
+ thus ?thesis unfolding pow2_neg[of "-e"] pow2_neg[of "-e * int n"] unfolding zmult_zminus zminus_zminus nonzero_power_inverse[OF pow2_neq_zero, symmetric]
+ using nonzero_inverse_eq_imp_eq[OF _ pow2_neq_zero pow2_neq_zero] by auto
+ qed
+ thus ?thesis by (auto simp add: Float power_mult_distrib Ifloat.simps power_float.simps)
+qed
+
+lemma zero_le_pow2[simp]: "0 \<le> pow2 s"
+ apply (subgoal_tac "0 < pow2 s")
+ apply (auto simp only:)
+ apply auto
done
-lemma float_zero:
- "float (0, b) = 0"
- by (simp add: float_def)
-
-lemma float_pprt:
- "pprt (float (a, b)) = (if 0 <= a then (float (a,b)) else (float (0, b)))"
- by (auto simp add: zero_le_float float_le_zero float_zero)
-
-lemma pprt_lbound: "pprt (lbound x) = float (0, 0)"
- apply (simp add: float_def)
- apply (rule pprt_eq_0)
- apply (simp add: lbound_def)
+lemma pow2_less_0_eq_False[simp]: "(pow2 s < 0) = False"
+ apply auto
+ apply (subgoal_tac "0 \<le> pow2 s")
+ apply simp
+ apply simp
done
-lemma nprt_ubound: "nprt (ubound x) = float (0, 0)"
- apply (simp add: float_def)
- apply (rule nprt_eq_0)
- apply (simp add: ubound_def)
+lemma pow2_le_0_eq_False[simp]: "(pow2 s \<le> 0) = False"
+ apply auto
+ apply (subgoal_tac "0 < pow2 s")
+ apply simp
+ apply simp
done
-lemma float_nprt:
- "nprt (float (a, b)) = (if 0 <= a then (float (0,b)) else (float (a, b)))"
- by (auto simp add: zero_le_float float_le_zero float_zero)
-
-lemma norm_0_1: "(0::_::number_ring) = Numeral0 & (1::_::number_ring) = Numeral1"
+lemma float_pos_m_pos: "0 < Float m e \<Longrightarrow> 0 < m"
+ unfolding less_float_def Ifloat.simps Ifloat_0 zero_less_mult_iff
by auto
-lemma add_left_zero: "0 + a = (a::'a::comm_monoid_add)"
- by simp
+lemma float_pos_less1_e_neg: assumes "0 < Float m e" and "Float m e < 1" shows "e < 0"
+proof -
+ have "0 < m" using float_pos_m_pos `0 < Float m e` by auto
+ hence "0 \<le> real m" and "1 \<le> real m" by auto
+
+ show "e < 0"
+ proof (rule ccontr)
+ assume "\<not> e < 0" hence "0 \<le> e" by auto
+ hence "1 \<le> pow2 e" unfolding pow2_def by auto
+ from mult_mono[OF `1 \<le> real m` this `0 \<le> real m`]
+ have "1 \<le> Float m e" by (simp add: le_float_def Ifloat.simps)
+ thus False using `Float m e < 1` unfolding less_float_def le_float_def by auto
+ qed
+qed
+
+lemma float_less1_mantissa_bound: assumes "0 < Float m e" "Float m e < 1" shows "m < 2^(nat (-e))"
+proof -
+ have "e < 0" using float_pos_less1_e_neg assms by auto
+ have "\<And>x. (0::real) < 2^x" by auto
+ have "real m < 2^(nat (-e))" using `Float m e < 1`
+ unfolding less_float_def Ifloat_neg_exp[OF `e < 0`] Ifloat_1
+ real_mult_less_iff1[of _ _ 1, OF `0 < 2^(nat (-e))`, symmetric]
+ real_mult_assoc by auto
+ thus ?thesis unfolding real_of_int_less_iff[symmetric] by auto
+qed
+
+function bitlen :: "int \<Rightarrow> int" where
+"bitlen 0 = 0" |
+"bitlen -1 = 1" |
+"0 < x \<Longrightarrow> bitlen x = 1 + (bitlen (x div 2))" |
+"x < -1 \<Longrightarrow> bitlen x = 1 + (bitlen (x div 2))"
+ apply (case_tac "x = 0 \<or> x = -1 \<or> x < -1 \<or> x > 0")
+ apply auto
+ done
+termination by (relation "measure (nat o abs)", auto)
+
+lemma bitlen_ge0: "0 \<le> bitlen x" by (induct x rule: bitlen.induct, auto)
+lemma bitlen_ge1: "x \<noteq> 0 \<Longrightarrow> 1 \<le> bitlen x" by (induct x rule: bitlen.induct, auto simp add: bitlen_ge0)
+
+lemma bitlen_bounds': assumes "0 < x" shows "2^nat (bitlen x - 1) \<le> x \<and> x + 1 \<le> 2^nat (bitlen x)" (is "?P x")
+ using `0 < x`
+proof (induct x rule: bitlen.induct)
+ fix x
+ assume "0 < x" and hyp: "0 < x div 2 \<Longrightarrow> ?P (x div 2)" hence "0 \<le> x" and "x \<noteq> 0" by auto
+ { fix x have "0 \<le> 1 + bitlen x" using bitlen_ge0[of x] by auto } note gt0_pls1 = this
+
+ have "0 < (2::int)" by auto
-lemma add_right_zero: "a + 0 = (a::'a::comm_monoid_add)"
- by simp
+ show "?P x"
+ proof (cases "x = 1")
+ case True show "?P x" unfolding True by auto
+ next
+ case False hence "2 \<le> x" using `0 < x` `x \<noteq> 1` by auto
+ hence "2 div 2 \<le> x div 2" by (rule zdiv_mono1, auto)
+ hence "0 < x div 2" and "x div 2 \<noteq> 0" by auto
+ hence bitlen_s1_ge0: "0 \<le> bitlen (x div 2) - 1" using bitlen_ge1[OF `x div 2 \<noteq> 0`] by auto
-lemma mult_left_one: "1 * a = (a::'a::semiring_1)"
- by simp
+ { from hyp[OF `0 < x div 2`]
+ have "2 ^ nat (bitlen (x div 2) - 1) \<le> x div 2" by auto
+ hence "2 ^ nat (bitlen (x div 2) - 1) * 2 \<le> x div 2 * 2" by (rule mult_right_mono, auto)
+ also have "\<dots> \<le> x" using `0 < x` by auto
+ finally have "2^nat (1 + bitlen (x div 2) - 1) \<le> x" unfolding power_Suc2[symmetric] Suc_nat_eq_nat_zadd1[OF bitlen_s1_ge0] by auto
+ } moreover
+ { have "x + 1 \<le> x - x mod 2 + 2"
+ proof -
+ have "x mod 2 < 2" using `0 < x` by auto
+ hence "x < x - x mod 2 + 2" unfolding algebra_simps by auto
+ thus ?thesis by auto
+ qed
+ also have "x - x mod 2 + 2 = (x div 2 + 1) * 2" unfolding algebra_simps using `0 < x` zdiv_zmod_equality2[of x 2 0] by auto
+ also have "\<dots> \<le> 2^nat (bitlen (x div 2)) * 2" using hyp[OF `0 < x div 2`, THEN conjunct2] by (rule mult_right_mono, auto)
+ also have "\<dots> = 2^(1 + nat (bitlen (x div 2)))" unfolding power_Suc2[symmetric] by auto
+ finally have "x + 1 \<le> 2^(1 + nat (bitlen (x div 2)))" .
+ }
+ ultimately show ?thesis
+ unfolding bitlen.simps(3)[OF `0 < x`] nat_add_distrib[OF zero_le_one bitlen_ge0]
+ unfolding add_commute nat_add_distrib[OF zero_le_one gt0_pls1]
+ by auto
+ qed
+next
+ fix x :: int assume "x < -1" and "0 < x" hence False by auto
+ thus "?P x" by auto
+qed auto
+
+lemma bitlen_bounds: assumes "0 < x" shows "2^nat (bitlen x - 1) \<le> x \<and> x < 2^nat (bitlen x)"
+ using bitlen_bounds'[OF `0<x`] by auto
+
+lemma bitlen_div: assumes "0 < m" shows "1 \<le> real m / 2^nat (bitlen m - 1)" and "real m / 2^nat (bitlen m - 1) < 2"
+proof -
+ let ?B = "2^nat(bitlen m - 1)"
+
+ have "?B \<le> m" using bitlen_bounds[OF `0 <m`] ..
+ hence "1 * ?B \<le> real m" unfolding real_of_int_le_iff[symmetric] by auto
+ thus "1 \<le> real m / ?B" by auto
+
+ have "m \<noteq> 0" using assms by auto
+ have "0 \<le> bitlen m - 1" using bitlen_ge1[OF `m \<noteq> 0`] by auto
-lemma mult_right_one: "a * 1 = (a::'a::semiring_1)"
- by simp
+ have "m < 2^nat(bitlen m)" using bitlen_bounds[OF `0 <m`] ..
+ also have "\<dots> = 2^nat(bitlen m - 1 + 1)" using bitlen_ge1[OF `m \<noteq> 0`] by auto
+ also have "\<dots> = ?B * 2" unfolding nat_add_distrib[OF `0 \<le> bitlen m - 1` zero_le_one] by auto
+ finally have "real m < 2 * ?B" unfolding real_of_int_less_iff[symmetric] by auto
+ hence "real m / ?B < 2 * ?B / ?B" by (rule divide_strict_right_mono, auto)
+ thus "real m / ?B < 2" by auto
+qed
+
+lemma float_gt1_scale: assumes "1 \<le> Float m e"
+ shows "0 \<le> e + (bitlen m - 1)"
+proof (cases "0 \<le> e")
+ have "0 < Float m e" using assms unfolding less_float_def le_float_def by auto
+ hence "0 < m" using float_pos_m_pos by auto
+ hence "m \<noteq> 0" by auto
+ case True with bitlen_ge1[OF `m \<noteq> 0`] show ?thesis by auto
+next
+ have "0 < Float m e" using assms unfolding less_float_def le_float_def by auto
+ hence "0 < m" using float_pos_m_pos by auto
+ hence "m \<noteq> 0" and "1 < (2::int)" by auto
+ case False let ?S = "2^(nat (-e))"
+ have "1 \<le> real m * inverse ?S" using assms unfolding le_float_def Ifloat_nge0_exp[OF False] by auto
+ hence "1 * ?S \<le> real m * inverse ?S * ?S" by (rule mult_right_mono, auto)
+ hence "?S \<le> real m" unfolding mult_assoc by auto
+ hence "?S \<le> m" unfolding real_of_int_le_iff[symmetric] by auto
+ from this bitlen_bounds[OF `0 < m`, THEN conjunct2]
+ have "nat (-e) < (nat (bitlen m))" unfolding power_strict_increasing_iff[OF `1 < 2`, symmetric] by (rule order_le_less_trans)
+ hence "-e < bitlen m" using False bitlen_ge0 by auto
+ thus ?thesis by auto
+qed
+
+lemma normalized_float: assumes "m \<noteq> 0" shows "Ifloat (Float m (- (bitlen m - 1))) = real m / 2^nat (bitlen m - 1)"
+proof (cases "- (bitlen m - 1) = 0")
+ case True show ?thesis unfolding Ifloat.simps pow2_def using True by auto
+next
+ case False hence P: "\<not> 0 \<le> - (bitlen m - 1)" using bitlen_ge1[OF `m \<noteq> 0`] by auto
+ show ?thesis unfolding Ifloat_nge0_exp[OF P] real_divide_def by auto
+qed
+
+lemma bitlen_Pls: "bitlen (Int.Pls) = Int.Pls" by (subst Pls_def, subst Pls_def, simp)
+
+lemma bitlen_Min: "bitlen (Int.Min) = Int.Bit1 Int.Pls" by (subst Min_def, simp add: Bit1_def)
+
+lemma bitlen_B0: "bitlen (Int.Bit0 b) = (if iszero b then Int.Pls else Int.succ (bitlen b))"
+ apply (auto simp add: iszero_def succ_def)
+ apply (simp add: Bit0_def Pls_def)
+ apply (subst Bit0_def)
+ apply simp
+ apply (subgoal_tac "0 < 2 * b \<or> 2 * b < -1")
+ apply auto
+ done
-lemma int_pow_0: "(a::int)^(Numeral0) = 1"
- by simp
+lemma bitlen_B1: "bitlen (Int.Bit1 b) = (if iszero (Int.succ b) then Int.Bit1 Int.Pls else Int.succ (bitlen b))"
+proof -
+ have h: "! x. (2*x + 1) div 2 = (x::int)"
+ by arith
+ show ?thesis
+ apply (auto simp add: iszero_def succ_def)
+ apply (subst Bit1_def)+
+ apply simp
+ apply (subgoal_tac "2 * b + 1 = -1")
+ apply (simp only:)
+ apply simp_all
+ apply (subst Bit1_def)
+ apply simp
+ apply (subgoal_tac "0 < 2 * b + 1 \<or> 2 * b + 1 < -1")
+ apply (auto simp add: h)
+ done
+qed
+
+lemma bitlen_number_of: "bitlen (number_of w) = number_of (bitlen w)"
+ by (simp add: number_of_is_id)
-lemma int_pow_1: "(a::int)^(Numeral1) = a"
- by simp
+lemma [code]: "bitlen x =
+ (if x = 0 then 0
+ else if x = -1 then 1
+ else (1 + (bitlen (x div 2))))"
+ by (cases "x = 0 \<or> x = -1 \<or> 0 < x") auto
+
+definition lapprox_posrat :: "nat \<Rightarrow> int \<Rightarrow> int \<Rightarrow> float"
+where
+ "lapprox_posrat prec x y =
+ (let
+ l = nat (int prec + bitlen y - bitlen x) ;
+ d = (x * 2^l) div y
+ in normfloat (Float d (- (int l))))"
+
+lemma pow2_minus: "pow2 (-x) = inverse (pow2 x)"
+ unfolding pow2_neg[of "-x"] by auto
+
+lemma lapprox_posrat:
+ assumes x: "0 \<le> x"
+ and y: "0 < y"
+ shows "Ifloat (lapprox_posrat prec x y) \<le> real x / real y"
+proof -
+ let ?l = "nat (int prec + bitlen y - bitlen x)"
+
+ have "real (x * 2^?l div y) * inverse (2^?l) \<le> (real (x * 2^?l) / real y) * inverse (2^?l)"
+ by (rule mult_right_mono, fact real_of_int_div4, simp)
+ also have "\<dots> \<le> (real x / real y) * 2^?l * inverse (2^?l)" by auto
+ finally have "real (x * 2^?l div y) * inverse (2^?l) \<le> real x / real y" unfolding real_mult_assoc by auto
+ thus ?thesis unfolding lapprox_posrat_def Let_def normfloat Ifloat.simps
+ unfolding pow2_minus pow2_int minus_minus .
+qed
-lemma zero_eq_Numeral0_nring: "(0::'a::number_ring) = Numeral0"
- by simp
+lemma real_of_int_div_mult:
+ fixes x y c :: int assumes "0 < y" and "0 < c"
+ shows "real (x div y) \<le> real (x * c div y) * inverse (real c)"
+proof -
+ have "c * (x div y) + 0 \<le> c * x div y" unfolding zdiv_zmult1_eq[of c x y]
+ by (rule zadd_left_mono,
+ auto intro!: mult_nonneg_nonneg
+ simp add: pos_imp_zdiv_nonneg_iff[OF `0 < y`] `0 < c`[THEN less_imp_le] pos_mod_sign[OF `0 < y`])
+ hence "real (x div y) * real c \<le> real (x * c div y)"
+ unfolding real_of_int_mult[symmetric] real_of_int_le_iff zmult_commute by auto
+ hence "real (x div y) * real c * inverse (real c) \<le> real (x * c div y) * inverse (real c)"
+ using `0 < c` by auto
+ thus ?thesis unfolding real_mult_assoc using `0 < c` by auto
+qed
+
+lemma lapprox_posrat_bottom: assumes "0 < y"
+ shows "real (x div y) \<le> Ifloat (lapprox_posrat n x y)"
+proof -
+ have pow: "\<And>x. (0::int) < 2^x" by auto
+ show ?thesis
+ unfolding lapprox_posrat_def Let_def Ifloat_add normfloat Ifloat.simps pow2_minus pow2_int
+ using real_of_int_div_mult[OF `0 < y` pow] by auto
+qed
+
+lemma lapprox_posrat_nonneg: assumes "0 \<le> x" and "0 < y"
+ shows "0 \<le> Ifloat (lapprox_posrat n x y)"
+proof -
+ show ?thesis
+ unfolding lapprox_posrat_def Let_def Ifloat_add normfloat Ifloat.simps pow2_minus pow2_int
+ using pos_imp_zdiv_nonneg_iff[OF `0 < y`] assms by (auto intro!: mult_nonneg_nonneg)
+qed
+
+definition rapprox_posrat :: "nat \<Rightarrow> int \<Rightarrow> int \<Rightarrow> float"
+where
+ "rapprox_posrat prec x y = (let
+ l = nat (int prec + bitlen y - bitlen x) ;
+ X = x * 2^l ;
+ d = X div y ;
+ m = X mod y
+ in normfloat (Float (d + (if m = 0 then 0 else 1)) (- (int l))))"
-lemma one_eq_Numeral1_nring: "(1::'a::number_ring) = Numeral1"
- by simp
+lemma rapprox_posrat:
+ assumes x: "0 \<le> x"
+ and y: "0 < y"
+ shows "real x / real y \<le> Ifloat (rapprox_posrat prec x y)"
+proof -
+ let ?l = "nat (int prec + bitlen y - bitlen x)" let ?X = "x * 2^?l"
+ show ?thesis
+ proof (cases "?X mod y = 0")
+ case True hence "y \<noteq> 0" and "y dvd ?X" using `0 < y` by auto
+ from real_of_int_div[OF this]
+ have "real (?X div y) * inverse (2 ^ ?l) = real ?X / real y * inverse (2 ^ ?l)" by auto
+ also have "\<dots> = real x / real y * (2^?l * inverse (2^?l))" by auto
+ finally have "real (?X div y) * inverse (2^?l) = real x / real y" by auto
+ thus ?thesis unfolding rapprox_posrat_def Let_def normfloat if_P[OF True]
+ unfolding Ifloat.simps pow2_minus pow2_int minus_minus by auto
+ next
+ case False
+ have "0 \<le> real y" and "real y \<noteq> 0" using `0 < y` by auto
+ have "0 \<le> real y * 2^?l" by (rule mult_nonneg_nonneg, rule `0 \<le> real y`, auto)
-lemma zero_eq_Numeral0_nat: "(0::nat) = Numeral0"
- by simp
+ have "?X = y * (?X div y) + ?X mod y" by auto
+ also have "\<dots> \<le> y * (?X div y) + y" by (rule add_mono, auto simp add: pos_mod_bound[OF `0 < y`, THEN less_imp_le])
+ also have "\<dots> = y * (?X div y + 1)" unfolding zadd_zmult_distrib2 by auto
+ finally have "real ?X \<le> real y * real (?X div y + 1)" unfolding real_of_int_le_iff real_of_int_mult[symmetric] .
+ hence "real ?X / (real y * 2^?l) \<le> real y * real (?X div y + 1) / (real y * 2^?l)"
+ by (rule divide_right_mono, simp only: `0 \<le> real y * 2^?l`)
+ also have "\<dots> = real y * real (?X div y + 1) / real y / 2^?l" by auto
+ also have "\<dots> = real (?X div y + 1) * inverse (2^?l)" unfolding nonzero_mult_divide_cancel_left[OF `real y \<noteq> 0`]
+ unfolding real_divide_def ..
+ finally show ?thesis unfolding rapprox_posrat_def Let_def normfloat Ifloat.simps if_not_P[OF False]
+ unfolding pow2_minus pow2_int minus_minus by auto
+ qed
+qed
+
+lemma rapprox_posrat_le1: assumes "0 \<le> x" and "0 < y" and "x \<le> y"
+ shows "Ifloat (rapprox_posrat n x y) \<le> 1"
+proof -
+ let ?l = "nat (int n + bitlen y - bitlen x)" let ?X = "x * 2^?l"
+ show ?thesis
+ proof (cases "?X mod y = 0")
+ case True hence "y \<noteq> 0" and "y dvd ?X" using `0 < y` by auto
+ from real_of_int_div[OF this]
+ have "real (?X div y) * inverse (2 ^ ?l) = real ?X / real y * inverse (2 ^ ?l)" by auto
+ also have "\<dots> = real x / real y * (2^?l * inverse (2^?l))" by auto
+ finally have "real (?X div y) * inverse (2^?l) = real x / real y" by auto
+ also have "real x / real y \<le> 1" using `0 \<le> x` and `0 < y` and `x \<le> y` by auto
+ finally show ?thesis unfolding rapprox_posrat_def Let_def normfloat if_P[OF True]
+ unfolding Ifloat.simps pow2_minus pow2_int minus_minus by auto
+ next
+ case False
+ have "x \<noteq> y"
+ proof (rule ccontr)
+ assume "\<not> x \<noteq> y" hence "x = y" by auto
+ have "?X mod y = 0" unfolding `x = y` using zmod_zmult_self2 by auto
+ thus False using False by auto
+ qed
+ hence "x < y" using `x \<le> y` by auto
+ hence "real x / real y < 1" using `0 < y` and `0 \<le> x` by auto
-lemma one_eq_Numeral1_nat: "(1::nat) = Numeral1"
- by simp
+ from real_of_int_div4[of "?X" y]
+ have "real (?X div y) \<le> (real x / real y) * 2^?l" unfolding real_of_int_mult times_divide_eq_left real_of_int_power[symmetric] real_number_of .
+ also have "\<dots> < 1 * 2^?l" using `real x / real y < 1` by (rule mult_strict_right_mono, auto)
+ finally have "?X div y < 2^?l" unfolding real_of_int_less_iff[of _ "2^?l", symmetric] by auto
+ hence "?X div y + 1 \<le> 2^?l" by auto
+ hence "real (?X div y + 1) * inverse (2^?l) \<le> 2^?l * inverse (2^?l)"
+ unfolding real_of_int_le_iff[of _ "2^?l", symmetric] real_of_int_power[symmetric] real_number_of
+ by (rule mult_right_mono, auto)
+ hence "real (?X div y + 1) * inverse (2^?l) \<le> 1" by auto
+ thus ?thesis unfolding rapprox_posrat_def Let_def normfloat Ifloat.simps if_not_P[OF False]
+ unfolding pow2_minus pow2_int minus_minus by auto
+ qed
+qed
-lemma zpower_Pls: "(z::int)^Numeral0 = Numeral1"
- by simp
+lemma zdiv_greater_zero: fixes a b :: int assumes "0 < a" and "a \<le> b"
+ shows "0 < b div a"
+proof (rule ccontr)
+ have "0 \<le> b" using assms by auto
+ assume "\<not> 0 < b div a" hence "b div a = 0" using `0 \<le> b`[unfolded pos_imp_zdiv_nonneg_iff[OF `0<a`, of b, symmetric]] by auto
+ have "b = a * (b div a) + b mod a" by auto
+ hence "b = b mod a" unfolding `b div a = 0` by auto
+ hence "b < a" using `0 < a`[THEN pos_mod_bound, of b] by auto
+ thus False using `a \<le> b` by auto
+qed
+
+lemma rapprox_posrat_less1: assumes "0 \<le> x" and "0 < y" and "2 * x < y" and "0 < n"
+ shows "Ifloat (rapprox_posrat n x y) < 1"
+proof (cases "x = 0")
+ case True thus ?thesis unfolding rapprox_posrat_def True Let_def normfloat Ifloat.simps by auto
+next
+ case False hence "0 < x" using `0 \<le> x` by auto
+ hence "x < y" using assms by auto
+
+ let ?l = "nat (int n + bitlen y - bitlen x)" let ?X = "x * 2^?l"
+ show ?thesis
+ proof (cases "?X mod y = 0")
+ case True hence "y \<noteq> 0" and "y dvd ?X" using `0 < y` by auto
+ from real_of_int_div[OF this]
+ have "real (?X div y) * inverse (2 ^ ?l) = real ?X / real y * inverse (2 ^ ?l)" by auto
+ also have "\<dots> = real x / real y * (2^?l * inverse (2^?l))" by auto
+ finally have "real (?X div y) * inverse (2^?l) = real x / real y" by auto
+ also have "real x / real y < 1" using `0 \<le> x` and `0 < y` and `x < y` by auto
+ finally show ?thesis unfolding rapprox_posrat_def Let_def normfloat Ifloat.simps if_P[OF True]
+ unfolding pow2_minus pow2_int minus_minus by auto
+ next
+ case False
+ hence "(real x / real y) < 1 / 2" using `0 < y` and `0 \<le> x` `2 * x < y` by auto
-lemma zpower_Min: "(z::int)^((-1)::nat) = Numeral1"
+ have "0 < ?X div y"
+ proof -
+ have "2^nat (bitlen x - 1) \<le> y" and "y < 2^nat (bitlen y)"
+ using bitlen_bounds[OF `0 < x`, THEN conjunct1] bitlen_bounds[OF `0 < y`, THEN conjunct2] `x < y` by auto
+ hence "(2::int)^nat (bitlen x - 1) < 2^nat (bitlen y)" by (rule order_le_less_trans)
+ hence "bitlen x \<le> bitlen y" by auto
+ hence len_less: "nat (bitlen x - 1) \<le> nat (int (n - 1) + bitlen y)" by auto
+
+ have "x \<noteq> 0" and "y \<noteq> 0" using `0 < x` `0 < y` by auto
+
+ have exp_eq: "nat (int (n - 1) + bitlen y) - nat (bitlen x - 1) = ?l"
+ using `bitlen x \<le> bitlen y` bitlen_ge1[OF `x \<noteq> 0`] bitlen_ge1[OF `y \<noteq> 0`] `0 < n` by auto
+
+ have "y * 2^nat (bitlen x - 1) \<le> y * x"
+ using bitlen_bounds[OF `0 < x`, THEN conjunct1] `0 < y`[THEN less_imp_le] by (rule mult_left_mono)
+ also have "\<dots> \<le> 2^nat (bitlen y) * x" using bitlen_bounds[OF `0 < y`, THEN conjunct2, THEN less_imp_le] `0 \<le> x` by (rule mult_right_mono)
+ also have "\<dots> \<le> x * 2^nat (int (n - 1) + bitlen y)" unfolding mult_commute[of x] by (rule mult_right_mono, auto simp add: `0 \<le> x`)
+ finally have "real y * 2^nat (bitlen x - 1) * inverse (2^nat (bitlen x - 1)) \<le> real x * 2^nat (int (n - 1) + bitlen y) * inverse (2^nat (bitlen x - 1))"
+ unfolding real_of_int_le_iff[symmetric] by auto
+ hence "real y \<le> real x * (2^nat (int (n - 1) + bitlen y) / (2^nat (bitlen x - 1)))"
+ unfolding real_mult_assoc real_divide_def by auto
+ also have "\<dots> = real x * (2^(nat (int (n - 1) + bitlen y) - nat (bitlen x - 1)))" using power_diff[of "2::real", OF _ len_less] by auto
+ finally have "y \<le> x * 2^?l" unfolding exp_eq unfolding real_of_int_le_iff[symmetric] by auto
+ thus ?thesis using zdiv_greater_zero[OF `0 < y`] by auto
+ qed
+
+ from real_of_int_div4[of "?X" y]
+ have "real (?X div y) \<le> (real x / real y) * 2^?l" unfolding real_of_int_mult times_divide_eq_left real_of_int_power[symmetric] real_number_of .
+ also have "\<dots> < 1/2 * 2^?l" using `real x / real y < 1/2` by (rule mult_strict_right_mono, auto)
+ finally have "?X div y * 2 < 2^?l" unfolding real_of_int_less_iff[of _ "2^?l", symmetric] by auto
+ hence "?X div y + 1 < 2^?l" using `0 < ?X div y` by auto
+ hence "real (?X div y + 1) * inverse (2^?l) < 2^?l * inverse (2^?l)"
+ unfolding real_of_int_less_iff[of _ "2^?l", symmetric] real_of_int_power[symmetric] real_number_of
+ by (rule mult_strict_right_mono, auto)
+ hence "real (?X div y + 1) * inverse (2^?l) < 1" by auto
+ thus ?thesis unfolding rapprox_posrat_def Let_def normfloat Ifloat.simps if_not_P[OF False]
+ unfolding pow2_minus pow2_int minus_minus by auto
+ qed
+qed
+
+lemma approx_rat_pattern: fixes P and ps :: "nat * int * int"
+ assumes Y: "\<And>y prec x. \<lbrakk>y = 0; ps = (prec, x, 0)\<rbrakk> \<Longrightarrow> P"
+ and A: "\<And>x y prec. \<lbrakk>0 \<le> x; 0 < y; ps = (prec, x, y)\<rbrakk> \<Longrightarrow> P"
+ and B: "\<And>x y prec. \<lbrakk>x < 0; 0 < y; ps = (prec, x, y)\<rbrakk> \<Longrightarrow> P"
+ and C: "\<And>x y prec. \<lbrakk>x < 0; y < 0; ps = (prec, x, y)\<rbrakk> \<Longrightarrow> P"
+ and D: "\<And>x y prec. \<lbrakk>0 \<le> x; y < 0; ps = (prec, x, y)\<rbrakk> \<Longrightarrow> P"
+ shows P
proof -
- have 1:"((-1)::nat) = 0"
- by simp
- show ?thesis by (simp add: 1)
+ obtain prec x y where [simp]: "ps = (prec, x, y)" by (cases ps, auto)
+ from Y have "y = 0 \<Longrightarrow> P" by auto
+ moreover { assume "0 < y" have P proof (cases "0 \<le> x") case True with A and `0 < y` show P by auto next case False with B and `0 < y` show P by auto qed }
+ moreover { assume "y < 0" have P proof (cases "0 \<le> x") case True with D and `y < 0` show P by auto next case False with C and `y < 0` show P by auto qed }
+ ultimately show P by (cases "y = 0 \<or> 0 < y \<or> y < 0", auto)
qed
-lemma fst_cong: "a=a' \<Longrightarrow> fst (a,b) = fst (a',b)"
- by simp
+function lapprox_rat :: "nat \<Rightarrow> int \<Rightarrow> int \<Rightarrow> float"
+where
+ "y = 0 \<Longrightarrow> lapprox_rat prec x y = 0"
+| "0 \<le> x \<Longrightarrow> 0 < y \<Longrightarrow> lapprox_rat prec x y = lapprox_posrat prec x y"
+| "x < 0 \<Longrightarrow> 0 < y \<Longrightarrow> lapprox_rat prec x y = - (rapprox_posrat prec (-x) y)"
+| "x < 0 \<Longrightarrow> y < 0 \<Longrightarrow> lapprox_rat prec x y = lapprox_posrat prec (-x) (-y)"
+| "0 \<le> x \<Longrightarrow> y < 0 \<Longrightarrow> lapprox_rat prec x y = - (rapprox_posrat prec x (-y))"
+apply simp_all by (rule approx_rat_pattern)
+termination by lexicographic_order
-lemma snd_cong: "b=b' \<Longrightarrow> snd (a,b) = snd (a,b')"
- by simp
+lemma compute_lapprox_rat[code]:
+ "lapprox_rat prec x y = (if y = 0 then 0 else if 0 \<le> x then (if 0 < y then lapprox_posrat prec x y else - (rapprox_posrat prec x (-y)))
+ else (if 0 < y then - (rapprox_posrat prec (-x) y) else lapprox_posrat prec (-x) (-y)))"
+ by auto
+
+lemma lapprox_rat: "Ifloat (lapprox_rat prec x y) \<le> real x / real y"
+proof -
+ have h[rule_format]: "! a b b'. b' \<le> b \<longrightarrow> a \<le> b' \<longrightarrow> a \<le> (b::real)" by auto
+ show ?thesis
+ apply (case_tac "y = 0")
+ apply simp
+ apply (case_tac "0 \<le> x \<and> 0 < y")
+ apply (simp add: lapprox_posrat)
+ apply (case_tac "x < 0 \<and> 0 < y")
+ apply simp
+ apply (subst minus_le_iff)
+ apply (rule h[OF rapprox_posrat])
+ apply (simp_all)
+ apply (case_tac "x < 0 \<and> y < 0")
+ apply simp
+ apply (rule h[OF _ lapprox_posrat])
+ apply (simp_all)
+ apply (case_tac "0 \<le> x \<and> y < 0")
+ apply (simp)
+ apply (subst minus_le_iff)
+ apply (rule h[OF rapprox_posrat])
+ apply simp_all
+ apply arith
+ done
+qed
-lemma lift_bool: "x \<Longrightarrow> x=True"
- by simp
+lemma lapprox_rat_bottom: assumes "0 \<le> x" and "0 < y"
+ shows "real (x div y) \<le> Ifloat (lapprox_rat n x y)"
+ unfolding lapprox_rat.simps(2)[OF assms] using lapprox_posrat_bottom[OF `0<y`] .
+
+function rapprox_rat :: "nat \<Rightarrow> int \<Rightarrow> int \<Rightarrow> float"
+where
+ "y = 0 \<Longrightarrow> rapprox_rat prec x y = 0"
+| "0 \<le> x \<Longrightarrow> 0 < y \<Longrightarrow> rapprox_rat prec x y = rapprox_posrat prec x y"
+| "x < 0 \<Longrightarrow> 0 < y \<Longrightarrow> rapprox_rat prec x y = - (lapprox_posrat prec (-x) y)"
+| "x < 0 \<Longrightarrow> y < 0 \<Longrightarrow> rapprox_rat prec x y = rapprox_posrat prec (-x) (-y)"
+| "0 \<le> x \<Longrightarrow> y < 0 \<Longrightarrow> rapprox_rat prec x y = - (lapprox_posrat prec x (-y))"
+apply simp_all by (rule approx_rat_pattern)
+termination by lexicographic_order
-lemma nlift_bool: "~x \<Longrightarrow> x=False"
- by simp
+lemma compute_rapprox_rat[code]:
+ "rapprox_rat prec x y = (if y = 0 then 0 else if 0 \<le> x then (if 0 < y then rapprox_posrat prec x y else - (lapprox_posrat prec x (-y))) else
+ (if 0 < y then - (lapprox_posrat prec (-x) y) else rapprox_posrat prec (-x) (-y)))"
+ by auto
-lemma not_false_eq_true: "(~ False) = True" by simp
+lemma rapprox_rat: "real x / real y \<le> Ifloat (rapprox_rat prec x y)"
+proof -
+ have h[rule_format]: "! a b b'. b' \<le> b \<longrightarrow> a \<le> b' \<longrightarrow> a \<le> (b::real)" by auto
+ show ?thesis
+ apply (case_tac "y = 0")
+ apply simp
+ apply (case_tac "0 \<le> x \<and> 0 < y")
+ apply (simp add: rapprox_posrat)
+ apply (case_tac "x < 0 \<and> 0 < y")
+ apply simp
+ apply (subst le_minus_iff)
+ apply (rule h[OF _ lapprox_posrat])
+ apply (simp_all)
+ apply (case_tac "x < 0 \<and> y < 0")
+ apply simp
+ apply (rule h[OF rapprox_posrat])
+ apply (simp_all)
+ apply (case_tac "0 \<le> x \<and> y < 0")
+ apply (simp)
+ apply (subst le_minus_iff)
+ apply (rule h[OF _ lapprox_posrat])
+ apply simp_all
+ apply arith
+ done
+qed
-lemma not_true_eq_false: "(~ True) = False" by simp
+lemma rapprox_rat_le1: assumes "0 \<le> x" and "0 < y" and "x \<le> y"
+ shows "Ifloat (rapprox_rat n x y) \<le> 1"
+ unfolding rapprox_rat.simps(2)[OF `0 \<le> x` `0 < y`] using rapprox_posrat_le1[OF assms] .
+
+lemma rapprox_rat_neg: assumes "x < 0" and "0 < y"
+ shows "Ifloat (rapprox_rat n x y) \<le> 0"
+ unfolding rapprox_rat.simps(3)[OF assms] using lapprox_posrat_nonneg[of "-x" y n] assms by auto
+
+lemma rapprox_rat_nonneg_neg: assumes "0 \<le> x" and "y < 0"
+ shows "Ifloat (rapprox_rat n x y) \<le> 0"
+ unfolding rapprox_rat.simps(5)[OF assms] using lapprox_posrat_nonneg[of x "-y" n] assms by auto
-lemmas binarith =
- normalize_bin_simps
- pred_bin_simps succ_bin_simps
- add_bin_simps minus_bin_simps mult_bin_simps
+lemma rapprox_rat_nonpos_pos: assumes "x \<le> 0" and "0 < y"
+ shows "Ifloat (rapprox_rat n x y) \<le> 0"
+proof (cases "x = 0")
+ case True hence "0 \<le> x" by auto show ?thesis unfolding rapprox_rat.simps(2)[OF `0 \<le> x` `0 < y`]
+ unfolding True rapprox_posrat_def Let_def by auto
+next
+ case False hence "x < 0" using assms by auto
+ show ?thesis using rapprox_rat_neg[OF `x < 0` `0 < y`] .
+qed
+
+fun float_divl :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float"
+where
+ "float_divl prec (Float m1 s1) (Float m2 s2) =
+ (let
+ l = lapprox_rat prec m1 m2;
+ f = Float 1 (s1 - s2)
+ in
+ f * l)"
-lemma int_eq_number_of_eq:
- "(((number_of v)::int)=(number_of w)) = iszero ((number_of (v + uminus w))::int)"
- by (rule eq_number_of_eq)
+lemma float_divl: "Ifloat (float_divl prec x y) \<le> Ifloat x / Ifloat y"
+proof -
+ from float_split[of x] obtain mx sx where x: "x = Float mx sx" by auto
+ from float_split[of y] obtain my sy where y: "y = Float my sy" by auto
+ have "real mx / real my \<le> (real mx * pow2 sx / (real my * pow2 sy)) / (pow2 (sx - sy))"
+ apply (case_tac "my = 0")
+ apply simp
+ apply (case_tac "my > 0")
+ apply (subst pos_le_divide_eq)
+ apply simp
+ apply (subst pos_le_divide_eq)
+ apply (simp add: mult_pos_pos)
+ apply simp
+ apply (subst pow2_add[symmetric])
+ apply simp
+ apply (subgoal_tac "my < 0")
+ apply auto
+ apply (simp add: field_simps)
+ apply (subst pow2_add[symmetric])
+ apply (simp add: field_simps)
+ done
+ then have "Ifloat (lapprox_rat prec mx my) \<le> (real mx * pow2 sx / (real my * pow2 sy)) / (pow2 (sx - sy))"
+ by (rule order_trans[OF lapprox_rat])
+ then have "Ifloat (lapprox_rat prec mx my) * pow2 (sx - sy) \<le> real mx * pow2 sx / (real my * pow2 sy)"
+ apply (subst pos_le_divide_eq[symmetric])
+ apply simp_all
+ done
+ then have "pow2 (sx - sy) * Ifloat (lapprox_rat prec mx my) \<le> real mx * pow2 sx / (real my * pow2 sy)"
+ by (simp add: algebra_simps)
+ then show ?thesis
+ by (simp add: x y Let_def Ifloat.simps)
+qed
-lemma int_iszero_number_of_Pls: "iszero (Numeral0::int)"
- by (simp only: iszero_number_of_Pls)
+lemma float_divl_lower_bound: assumes "0 \<le> x" and "0 < y" shows "0 \<le> float_divl prec x y"
+proof (cases x, cases y)
+ fix xm xe ym ye :: int
+ assume x_eq: "x = Float xm xe" and y_eq: "y = Float ym ye"
+ have "0 \<le> xm" using `0 \<le> x`[unfolded x_eq le_float_def Ifloat.simps Ifloat_0 zero_le_mult_iff] by auto
+ have "0 < ym" using `0 < y`[unfolded y_eq less_float_def Ifloat.simps Ifloat_0 zero_less_mult_iff] by auto
-lemma int_nonzero_number_of_Min: "~(iszero ((-1)::int))"
- by simp
+ have "\<And>n. 0 \<le> Ifloat (Float 1 n)" unfolding Ifloat.simps using zero_le_pow2 by auto
+ moreover have "0 \<le> Ifloat (lapprox_rat prec xm ym)" by (rule order_trans[OF _ lapprox_rat_bottom[OF `0 \<le> xm` `0 < ym`]], auto simp add: `0 \<le> xm` pos_imp_zdiv_nonneg_iff[OF `0 < ym`])
+ ultimately show "0 \<le> float_divl prec x y"
+ unfolding x_eq y_eq float_divl.simps Let_def le_float_def Ifloat_0 by (auto intro!: mult_nonneg_nonneg)
+qed
+
+lemma float_divl_pos_less1_bound: assumes "0 < x" and "x < 1" and "0 < prec" shows "1 \<le> float_divl prec 1 x"
+proof (cases x)
+ case (Float m e)
+ from `0 < x` `x < 1` have "0 < m" "e < 0" using float_pos_m_pos float_pos_less1_e_neg unfolding Float by auto
+ let ?b = "nat (bitlen m)" and ?e = "nat (-e)"
+ have "1 \<le> m" and "m \<noteq> 0" using `0 < m` by auto
+ with bitlen_bounds[OF `0 < m`] have "m < 2^?b" and "(2::int) \<le> 2^?b" by auto
+ hence "1 \<le> bitlen m" using power_le_imp_le_exp[of "2::int" 1 ?b] by auto
+ hence pow_split: "nat (int prec + bitlen m - 1) = (prec - 1) + ?b" using `0 < prec` by auto
+
+ have pow_not0: "\<And>x. (2::real)^x \<noteq> 0" by auto
-lemma int_iszero_number_of_Bit0: "iszero ((number_of (Int.Bit0 w))::int) = iszero ((number_of w)::int)"
- by simp
+ from float_less1_mantissa_bound `0 < x` `x < 1` Float
+ have "m < 2^?e" by auto
+ with bitlen_bounds[OF `0 < m`, THEN conjunct1]
+ have "(2::int)^nat (bitlen m - 1) < 2^?e" by (rule order_le_less_trans)
+ from power_less_imp_less_exp[OF _ this]
+ have "bitlen m \<le> - e" by auto
+ hence "(2::real)^?b \<le> 2^?e" by auto
+ hence "(2::real)^?b * inverse (2^?b) \<le> 2^?e * inverse (2^?b)" by (rule mult_right_mono, auto)
+ hence "(1::real) \<le> 2^?e * inverse (2^?b)" by auto
+ also
+ let ?d = "real (2 ^ nat (int prec + bitlen m - 1) div m) * inverse (2 ^ nat (int prec + bitlen m - 1))"
+ { have "2^(prec - 1) * m \<le> 2^(prec - 1) * 2^?b" using `m < 2^?b`[THEN less_imp_le] by (rule mult_left_mono, auto)
+ also have "\<dots> = 2 ^ nat (int prec + bitlen m - 1)" unfolding pow_split zpower_zadd_distrib by auto
+ finally have "2^(prec - 1) * m div m \<le> 2 ^ nat (int prec + bitlen m - 1) div m" using `0 < m` by (rule zdiv_mono1)
+ hence "2^(prec - 1) \<le> 2 ^ nat (int prec + bitlen m - 1) div m" unfolding zdiv_zmult_self1[OF `m \<noteq> 0`] .
+ hence "2^(prec - 1) * inverse (2 ^ nat (int prec + bitlen m - 1)) \<le> ?d"
+ unfolding real_of_int_le_iff[of "2^(prec - 1)", symmetric] by auto }
+ from mult_left_mono[OF this[unfolded pow_split power_add inverse_mult_distrib real_mult_assoc[symmetric] right_inverse[OF pow_not0] real_mult_1], of "2^?e"]
+ have "2^?e * inverse (2^?b) \<le> 2^?e * ?d" unfolding pow_split power_add by auto
+ finally have "1 \<le> 2^?e * ?d" .
+
+ have e_nat: "0 - e = int (nat (-e))" using `e < 0` by auto
+ have "bitlen 1 = 1" using bitlen.simps by auto
+
+ show ?thesis
+ unfolding one_float_def Float float_divl.simps Let_def lapprox_rat.simps(2)[OF zero_le_one `0 < m`] lapprox_posrat_def `bitlen 1 = 1`
+ unfolding le_float_def Ifloat_mult normfloat Ifloat.simps pow2_minus pow2_int e_nat
+ using `1 \<le> 2^?e * ?d` by (auto simp add: pow2_def)
+qed
-lemma int_iszero_number_of_Bit1: "\<not> iszero ((number_of (Int.Bit1 w))::int)"
- by simp
+fun float_divr :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float"
+where
+ "float_divr prec (Float m1 s1) (Float m2 s2) =
+ (let
+ r = rapprox_rat prec m1 m2;
+ f = Float 1 (s1 - s2)
+ in
+ f * r)"
-lemma int_less_number_of_eq_neg: "(((number_of x)::int) < number_of y) = neg ((number_of (x + (uminus y)))::int)"
- unfolding neg_def number_of_is_id by simp
+lemma float_divr: "Ifloat x / Ifloat y \<le> Ifloat (float_divr prec x y)"
+proof -
+ from float_split[of x] obtain mx sx where x: "x = Float mx sx" by auto
+ from float_split[of y] obtain my sy where y: "y = Float my sy" by auto
+ have "real mx / real my \<ge> (real mx * pow2 sx / (real my * pow2 sy)) / (pow2 (sx - sy))"
+ apply (case_tac "my = 0")
+ apply simp
+ apply (case_tac "my > 0")
+ apply auto
+ apply (subst pos_divide_le_eq)
+ apply (rule mult_pos_pos)+
+ apply simp_all
+ apply (subst pow2_add[symmetric])
+ apply simp
+ apply (subgoal_tac "my < 0")
+ apply auto
+ apply (simp add: field_simps)
+ apply (subst pow2_add[symmetric])
+ apply (simp add: field_simps)
+ done
+ then have "Ifloat (rapprox_rat prec mx my) \<ge> (real mx * pow2 sx / (real my * pow2 sy)) / (pow2 (sx - sy))"
+ by (rule order_trans[OF _ rapprox_rat])
+ then have "Ifloat (rapprox_rat prec mx my) * pow2 (sx - sy) \<ge> real mx * pow2 sx / (real my * pow2 sy)"
+ apply (subst pos_divide_le_eq[symmetric])
+ apply simp_all
+ done
+ then show ?thesis
+ by (simp add: x y Let_def algebra_simps Ifloat.simps)
+qed
-lemma int_not_neg_number_of_Pls: "\<not> (neg (Numeral0::int))"
- by simp
+lemma float_divr_pos_less1_lower_bound: assumes "0 < x" and "x < 1" shows "1 \<le> float_divr prec 1 x"
+proof -
+ have "1 \<le> 1 / Ifloat x" using `0 < x` and `x < 1` unfolding less_float_def by auto
+ also have "\<dots> \<le> Ifloat (float_divr prec 1 x)" using float_divr[where x=1 and y=x] by auto
+ finally show ?thesis unfolding le_float_def by auto
+qed
+
+lemma float_divr_nonpos_pos_upper_bound: assumes "x \<le> 0" and "0 < y" shows "float_divr prec x y \<le> 0"
+proof (cases x, cases y)
+ fix xm xe ym ye :: int
+ assume x_eq: "x = Float xm xe" and y_eq: "y = Float ym ye"
+ have "xm \<le> 0" using `x \<le> 0`[unfolded x_eq le_float_def Ifloat.simps Ifloat_0 mult_le_0_iff] by auto
+ have "0 < ym" using `0 < y`[unfolded y_eq less_float_def Ifloat.simps Ifloat_0 zero_less_mult_iff] by auto
+
+ have "\<And>n. 0 \<le> Ifloat (Float 1 n)" unfolding Ifloat.simps using zero_le_pow2 by auto
+ moreover have "Ifloat (rapprox_rat prec xm ym) \<le> 0" using rapprox_rat_nonpos_pos[OF `xm \<le> 0` `0 < ym`] .
+ ultimately show "float_divr prec x y \<le> 0"
+ unfolding x_eq y_eq float_divr.simps Let_def le_float_def Ifloat_0 Ifloat_mult by (auto intro!: mult_nonneg_nonpos)
+qed
-lemma int_neg_number_of_Min: "neg (-1::int)"
- by simp
+lemma float_divr_nonneg_neg_upper_bound: assumes "0 \<le> x" and "y < 0" shows "float_divr prec x y \<le> 0"
+proof (cases x, cases y)
+ fix xm xe ym ye :: int
+ assume x_eq: "x = Float xm xe" and y_eq: "y = Float ym ye"
+ have "0 \<le> xm" using `0 \<le> x`[unfolded x_eq le_float_def Ifloat.simps Ifloat_0 zero_le_mult_iff] by auto
+ have "ym < 0" using `y < 0`[unfolded y_eq less_float_def Ifloat.simps Ifloat_0 mult_less_0_iff] by auto
+ hence "0 < - ym" by auto
+
+ have "\<And>n. 0 \<le> Ifloat (Float 1 n)" unfolding Ifloat.simps using zero_le_pow2 by auto
+ moreover have "Ifloat (rapprox_rat prec xm ym) \<le> 0" using rapprox_rat_nonneg_neg[OF `0 \<le> xm` `ym < 0`] .
+ ultimately show "float_divr prec x y \<le> 0"
+ unfolding x_eq y_eq float_divr.simps Let_def le_float_def Ifloat_0 Ifloat_mult by (auto intro!: mult_nonneg_nonpos)
+qed
+
+fun round_down :: "nat \<Rightarrow> float \<Rightarrow> float" where
+"round_down prec (Float m e) = (let d = bitlen m - int prec in
+ if 0 < d then let P = 2^nat d ; n = m div P in Float n (e + d)
+ else Float m e)"
+
+fun round_up :: "nat \<Rightarrow> float \<Rightarrow> float" where
+"round_up prec (Float m e) = (let d = bitlen m - int prec in
+ if 0 < d then let P = 2^nat d ; n = m div P ; r = m mod P in Float (n + (if r = 0 then 0 else 1)) (e + d)
+ else Float m e)"
-lemma int_neg_number_of_Bit0: "neg ((number_of (Int.Bit0 w))::int) = neg ((number_of w)::int)"
- by simp
-
-lemma int_neg_number_of_Bit1: "neg ((number_of (Int.Bit1 w))::int) = neg ((number_of w)::int)"
- by simp
+lemma round_up: "Ifloat x \<le> Ifloat (round_up prec x)"
+proof (cases x)
+ case (Float m e)
+ let ?d = "bitlen m - int prec"
+ let ?p = "(2::int)^nat ?d"
+ have "0 < ?p" by auto
+ show "?thesis"
+ proof (cases "0 < ?d")
+ case True
+ hence pow_d: "pow2 ?d = real ?p" unfolding pow2_int[symmetric] power_real_number_of[symmetric] by auto
+ show ?thesis
+ proof (cases "m mod ?p = 0")
+ case True
+ have m: "m = m div ?p * ?p + 0" unfolding True[symmetric] using zdiv_zmod_equality2[where k=0, unfolded monoid_add_class.add_0_right, symmetric] .
+ have "Ifloat (Float m e) = Ifloat (Float (m div ?p) (e + ?d))" unfolding Ifloat.simps arg_cong[OF m, of real]
+ by (auto simp add: pow2_add `0 < ?d` pow_d)
+ thus ?thesis
+ unfolding Float round_up.simps Let_def if_P[OF `m mod ?p = 0`] if_P[OF `0 < ?d`]
+ by auto
+ next
+ case False
+ have "m = m div ?p * ?p + m mod ?p" unfolding zdiv_zmod_equality2[where k=0, unfolded monoid_add_class.add_0_right] ..
+ also have "\<dots> \<le> (m div ?p + 1) * ?p" unfolding left_distrib zmult_1 by (rule add_left_mono, rule pos_mod_bound[OF `0 < ?p`, THEN less_imp_le])
+ finally have "Ifloat (Float m e) \<le> Ifloat (Float (m div ?p + 1) (e + ?d))" unfolding Ifloat.simps add_commute[of e]
+ unfolding pow2_add mult_assoc[symmetric] real_of_int_le_iff[of m, symmetric]
+ by (auto intro!: mult_mono simp add: pow2_add `0 < ?d` pow_d)
+ thus ?thesis
+ unfolding Float round_up.simps Let_def if_not_P[OF `\<not> m mod ?p = 0`] if_P[OF `0 < ?d`] .
+ qed
+ next
+ case False
+ show ?thesis
+ unfolding Float round_up.simps Let_def if_not_P[OF False] ..
+ qed
+qed
-lemma int_le_number_of_eq: "(((number_of x)::int) \<le> number_of y) = (\<not> neg ((number_of (y + (uminus x)))::int))"
- unfolding neg_def number_of_is_id by (simp add: not_less)
+lemma round_down: "Ifloat (round_down prec x) \<le> Ifloat x"
+proof (cases x)
+ case (Float m e)
+ let ?d = "bitlen m - int prec"
+ let ?p = "(2::int)^nat ?d"
+ have "0 < ?p" by auto
+ show "?thesis"
+ proof (cases "0 < ?d")
+ case True
+ hence pow_d: "pow2 ?d = real ?p" unfolding pow2_int[symmetric] power_real_number_of[symmetric] by auto
+ have "m div ?p * ?p \<le> m div ?p * ?p + m mod ?p" by (auto simp add: pos_mod_bound[OF `0 < ?p`, THEN less_imp_le])
+ also have "\<dots> \<le> m" unfolding zdiv_zmod_equality2[where k=0, unfolded monoid_add_class.add_0_right] ..
+ finally have "Ifloat (Float (m div ?p) (e + ?d)) \<le> Ifloat (Float m e)" unfolding Ifloat.simps add_commute[of e]
+ unfolding pow2_add mult_assoc[symmetric] real_of_int_le_iff[of _ m, symmetric]
+ by (auto intro!: mult_mono simp add: pow2_add `0 < ?d` pow_d)
+ thus ?thesis
+ unfolding Float round_down.simps Let_def if_P[OF `0 < ?d`] .
+ next
+ case False
+ show ?thesis
+ unfolding Float round_down.simps Let_def if_not_P[OF False] ..
+ qed
+qed
+
+definition lb_mult :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float" where
+"lb_mult prec x y = (case normfloat (x * y) of Float m e \<Rightarrow> let
+ l = bitlen m - int prec
+ in if l > 0 then Float (m div (2^nat l)) (e + l)
+ else Float m e)"
-lemmas intarithrel =
- int_eq_number_of_eq
- lift_bool[OF int_iszero_number_of_Pls] nlift_bool[OF int_nonzero_number_of_Min] int_iszero_number_of_Bit0
- lift_bool[OF int_iszero_number_of_Bit1] int_less_number_of_eq_neg nlift_bool[OF int_not_neg_number_of_Pls] lift_bool[OF int_neg_number_of_Min]
- int_neg_number_of_Bit0 int_neg_number_of_Bit1 int_le_number_of_eq
+definition ub_mult :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float" where
+"ub_mult prec x y = (case normfloat (x * y) of Float m e \<Rightarrow> let
+ l = bitlen m - int prec
+ in if l > 0 then Float (m div (2^nat l) + 1) (e + l)
+ else Float m e)"
-lemma int_number_of_add_sym: "((number_of v)::int) + number_of w = number_of (v + w)"
- by simp
+lemma lb_mult: "Ifloat (lb_mult prec x y) \<le> Ifloat (x * y)"
+proof (cases "normfloat (x * y)")
+ case (Float m e)
+ hence "odd m \<or> (m = 0 \<and> e = 0)" by (rule normfloat_imp_odd_or_zero)
+ let ?l = "bitlen m - int prec"
+ have "Ifloat (lb_mult prec x y) \<le> Ifloat (normfloat (x * y))"
+ proof (cases "?l > 0")
+ case False thus ?thesis unfolding lb_mult_def Float Let_def float.cases by auto
+ next
+ case True
+ have "real (m div 2^(nat ?l)) * pow2 ?l \<le> real m"
+ proof -
+ have "real (m div 2^(nat ?l)) * pow2 ?l = real (2^(nat ?l) * (m div 2^(nat ?l)))" unfolding real_of_int_mult real_of_int_power[symmetric] real_number_of unfolding pow2_int[symmetric]
+ using `?l > 0` by auto
+ also have "\<dots> \<le> real (2^(nat ?l) * (m div 2^(nat ?l)) + m mod 2^(nat ?l))" unfolding real_of_int_add by auto
+ also have "\<dots> = real m" unfolding zmod_zdiv_equality[symmetric] ..
+ finally show ?thesis by auto
+ qed
+ thus ?thesis unfolding lb_mult_def Float Let_def float.cases if_P[OF True] Ifloat.simps pow2_add real_mult_commute real_mult_assoc by auto
+ qed
+ also have "\<dots> = Ifloat (x * y)" unfolding normfloat ..
+ finally show ?thesis .
+qed
-lemma int_number_of_diff_sym: "((number_of v)::int) - number_of w = number_of (v + (uminus w))"
- by simp
+lemma ub_mult: "Ifloat (x * y) \<le> Ifloat (ub_mult prec x y)"
+proof (cases "normfloat (x * y)")
+ case (Float m e)
+ hence "odd m \<or> (m = 0 \<and> e = 0)" by (rule normfloat_imp_odd_or_zero)
+ let ?l = "bitlen m - int prec"
+ have "Ifloat (x * y) = Ifloat (normfloat (x * y))" unfolding normfloat ..
+ also have "\<dots> \<le> Ifloat (ub_mult prec x y)"
+ proof (cases "?l > 0")
+ case False thus ?thesis unfolding ub_mult_def Float Let_def float.cases by auto
+ next
+ case True
+ have "real m \<le> real (m div 2^(nat ?l) + 1) * pow2 ?l"
+ proof -
+ have "m mod 2^(nat ?l) < 2^(nat ?l)" by (rule pos_mod_bound) auto
+ hence mod_uneq: "real (m mod 2^(nat ?l)) \<le> 1 * 2^(nat ?l)" unfolding zmult_1 real_of_int_less_iff[symmetric] by auto
+
+ have "real m = real (2^(nat ?l) * (m div 2^(nat ?l)) + m mod 2^(nat ?l))" unfolding zmod_zdiv_equality[symmetric] ..
+ also have "\<dots> = real (m div 2^(nat ?l)) * 2^(nat ?l) + real (m mod 2^(nat ?l))" unfolding real_of_int_add by auto
+ also have "\<dots> \<le> (real (m div 2^(nat ?l)) + 1) * 2^(nat ?l)" unfolding real_add_mult_distrib using mod_uneq by auto
+ finally show ?thesis unfolding pow2_int[symmetric] using True by auto
+ qed
+ thus ?thesis unfolding ub_mult_def Float Let_def float.cases if_P[OF True] Ifloat.simps pow2_add real_mult_commute real_mult_assoc by auto
+ qed
+ finally show ?thesis .
+qed
+
+fun float_abs :: "float \<Rightarrow> float" where
+"float_abs (Float m e) = Float \<bar>m\<bar> e"
+
+instantiation float :: abs begin
+definition abs_float_def: "\<bar>x\<bar> = float_abs x"
+instance ..
+end
-lemma int_number_of_mult_sym: "((number_of v)::int) * number_of w = number_of (v * w)"
- by simp
+lemma Ifloat_abs: "Ifloat \<bar>x\<bar> = \<bar>Ifloat x\<bar>"
+proof (cases x)
+ case (Float m e)
+ have "\<bar>real m\<bar> * pow2 e = \<bar>real m * pow2 e\<bar>" unfolding abs_mult by auto
+ thus ?thesis unfolding Float abs_float_def float_abs.simps Ifloat.simps by auto
+qed
+
+fun floor_fl :: "float \<Rightarrow> float" where
+"floor_fl (Float m e) = (if 0 \<le> e then Float m e
+ else Float (m div (2 ^ (nat (-e)))) 0)"
-lemma int_number_of_minus_sym: "- ((number_of v)::int) = number_of (uminus v)"
- by simp
+lemma floor_fl: "Ifloat (floor_fl x) \<le> Ifloat x"
+proof (cases x)
+ case (Float m e)
+ show ?thesis
+ proof (cases "0 \<le> e")
+ case False
+ hence me_eq: "pow2 (-e) = pow2 (int (nat (-e)))" by auto
+ have "Ifloat (Float (m div (2 ^ (nat (-e)))) 0) = real (m div 2 ^ (nat (-e)))" unfolding Ifloat.simps by auto
+ also have "\<dots> \<le> real m / real ((2::int) ^ (nat (-e)))" using real_of_int_div4 .
+ also have "\<dots> = real m * inverse (2 ^ (nat (-e)))" unfolding power_real_number_of[symmetric] real_divide_def ..
+ also have "\<dots> = Ifloat (Float m e)" unfolding Ifloat.simps me_eq pow2_int pow2_neg[of e] ..
+ finally show ?thesis unfolding Float floor_fl.simps if_not_P[OF `\<not> 0 \<le> e`] .
+ next
+ case True thus ?thesis unfolding Float by auto
+ qed
+qed
-lemmas intarith = int_number_of_add_sym int_number_of_minus_sym int_number_of_diff_sym int_number_of_mult_sym
+lemma floor_pos_exp: assumes floor: "Float m e = floor_fl x" shows "0 \<le> e"
+proof (cases x)
+ case (Float mx me)
+ from floor[unfolded Float floor_fl.simps] show ?thesis by (cases "0 \<le> me", auto)
+qed
+
+declare floor_fl.simps[simp del]
-lemmas natarith = add_nat_number_of diff_nat_number_of mult_nat_number_of eq_nat_number_of less_nat_number_of
+fun ceiling_fl :: "float \<Rightarrow> float" where
+"ceiling_fl (Float m e) = (if 0 \<le> e then Float m e
+ else Float (m div (2 ^ (nat (-e))) + 1) 0)"
-lemmas powerarith = nat_number_of zpower_number_of_even
- zpower_number_of_odd[simplified zero_eq_Numeral0_nring one_eq_Numeral1_nring]
- zpower_Pls zpower_Min
+lemma ceiling_fl: "Ifloat x \<le> Ifloat (ceiling_fl x)"
+proof (cases x)
+ case (Float m e)
+ show ?thesis
+ proof (cases "0 \<le> e")
+ case False
+ hence me_eq: "pow2 (-e) = pow2 (int (nat (-e)))" by auto
+ have "Ifloat (Float m e) = real m * inverse (2 ^ (nat (-e)))" unfolding Ifloat.simps me_eq pow2_int pow2_neg[of e] ..
+ also have "\<dots> = real m / real ((2::int) ^ (nat (-e)))" unfolding power_real_number_of[symmetric] real_divide_def ..
+ also have "\<dots> \<le> 1 + real (m div 2 ^ (nat (-e)))" using real_of_int_div3[unfolded diff_le_eq] .
+ also have "\<dots> = Ifloat (Float (m div (2 ^ (nat (-e))) + 1) 0)" unfolding Ifloat.simps by auto
+ finally show ?thesis unfolding Float ceiling_fl.simps if_not_P[OF `\<not> 0 \<le> e`] .
+ next
+ case True thus ?thesis unfolding Float by auto
+ qed
+qed
+
+declare ceiling_fl.simps[simp del]
+
+definition lb_mod :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float" where
+"lb_mod prec x ub lb = x - ceiling_fl (float_divr prec x lb) * ub"
+
+definition ub_mod :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float" where
+"ub_mod prec x ub lb = x - floor_fl (float_divl prec x ub) * lb"
-lemmas floatarith[simplified norm_0_1] = float_add float_add_l0 float_add_r0 float_mult float_mult_l0 float_mult_r0
- float_minus float_abs zero_le_float float_pprt float_nprt pprt_lbound nprt_ubound
+lemma lb_mod: fixes k :: int assumes "0 \<le> Ifloat x" and "real k * y \<le> Ifloat x" (is "?k * y \<le> ?x")
+ assumes "0 < Ifloat lb" "Ifloat lb \<le> y" (is "?lb \<le> y") "y \<le> Ifloat ub" (is "y \<le> ?ub")
+ shows "Ifloat (lb_mod prec x ub lb) \<le> ?x - ?k * y"
+proof -
+ have "?lb \<le> ?ub" by (auto!)
+ have "0 \<le> ?lb" and "?lb \<noteq> 0" by (auto!)
+ have "?k * y \<le> ?x" using assms by auto
+ also have "\<dots> \<le> ?x / ?lb * ?ub" by (metis mult_left_mono[OF `?lb \<le> ?ub` `0 \<le> ?x`] divide_right_mono[OF _ `0 \<le> ?lb` ] times_divide_eq_left nonzero_mult_divide_cancel_right[OF `?lb \<noteq> 0`])
+ also have "\<dots> \<le> Ifloat (ceiling_fl (float_divr prec x lb)) * ?ub" by (metis mult_right_mono order_trans `0 \<le> ?lb` `?lb \<le> ?ub` float_divr ceiling_fl)
+ finally show ?thesis unfolding lb_mod_def Ifloat_sub Ifloat_mult by auto
+qed
-(* for use with the compute oracle *)
-lemmas arith = binarith intarith intarithrel natarith powerarith floatarith not_false_eq_true not_true_eq_false
+lemma ub_mod: fixes k :: int assumes "0 \<le> Ifloat x" and "Ifloat x \<le> real k * y" (is "?x \<le> ?k * y")
+ assumes "0 < Ifloat lb" "Ifloat lb \<le> y" (is "?lb \<le> y") "y \<le> Ifloat ub" (is "y \<le> ?ub")
+ shows "?x - ?k * y \<le> Ifloat (ub_mod prec x ub lb)"
+proof -
+ have "?lb \<le> ?ub" by (auto!)
+ hence "0 \<le> ?lb" and "0 \<le> ?ub" and "?ub \<noteq> 0" by (auto!)
+ have "Ifloat (floor_fl (float_divl prec x ub)) * ?lb \<le> ?x / ?ub * ?lb" by (metis mult_right_mono order_trans `0 \<le> ?lb` `?lb \<le> ?ub` float_divl floor_fl)
+ also have "\<dots> \<le> ?x" by (metis mult_left_mono[OF `?lb \<le> ?ub` `0 \<le> ?x`] divide_right_mono[OF _ `0 \<le> ?ub` ] times_divide_eq_left nonzero_mult_divide_cancel_right[OF `?ub \<noteq> 0`])
+ also have "\<dots> \<le> ?k * y" using assms by auto
+ finally show ?thesis unfolding ub_mod_def Ifloat_sub Ifloat_mult by auto
+qed
-use "~~/src/HOL/Tools/float_arith.ML"
+lemma le_float_def': "f \<le> g = (case f - g of Float a b \<Rightarrow> a \<le> 0)"
+proof -
+ have le_transfer: "(f \<le> g) = (Ifloat (f - g) \<le> 0)" by (auto simp add: le_float_def)
+ from float_split[of "f - g"] obtain a b where f_diff_g: "f - g = Float a b" by auto
+ with le_transfer have le_transfer': "f \<le> g = (Ifloat (Float a b) \<le> 0)" by simp
+ show ?thesis by (simp add: le_transfer' f_diff_g float_le_zero)
+qed
+
+lemma float_less_zero:
+ "(Ifloat (Float a b) < 0) = (a < 0)"
+ apply (auto simp add: mult_less_0_iff Ifloat.simps)
+ done
+
+lemma less_float_def': "f < g = (case f - g of Float a b \<Rightarrow> a < 0)"
+proof -
+ have less_transfer: "(f < g) = (Ifloat (f - g) < 0)" by (auto simp add: less_float_def)
+ from float_split[of "f - g"] obtain a b where f_diff_g: "f - g = Float a b" by auto
+ with less_transfer have less_transfer': "f < g = (Ifloat (Float a b) < 0)" by simp
+ show ?thesis by (simp add: less_transfer' f_diff_g float_less_zero)
+qed
end
--- a/src/HOL/Library/Library.thy Fri Feb 06 15:57:47 2009 +0100
+++ b/src/HOL/Library/Library.thy Fri Feb 06 15:59:49 2009 +0100
@@ -15,6 +15,7 @@
Continuity
ContNotDenum
Countable
+ Dense_Linear_Order
Efficient_Nat
Enum
Eval_Witness
@@ -34,9 +35,11 @@
Permutation
Pocklington
Primes
+ Quickcheck
Quicksort
Quotient
Ramsey
+ Random
Reflection
RBT
State_Monad
--- a/src/HOL/Library/Mapping.thy Fri Feb 06 15:57:47 2009 +0100
+++ b/src/HOL/Library/Mapping.thy Fri Feb 06 15:59:49 2009 +0100
@@ -33,6 +33,9 @@
definition size :: "('a, 'b) map \<Rightarrow> nat" where
"size m = (if finite (keys m) then card (keys m) else 0)"
+definition replace :: "'a \<Rightarrow> 'b \<Rightarrow> ('a, 'b) map \<Rightarrow> ('a, 'b) map" where
+ "replace k v m = (if lookup m k = None then m else update k v m)"
+
definition tabulate :: "'a list \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) map" where
"tabulate ks f = Map (map_of (map (\<lambda>k. (k, f k)) ks))"
@@ -65,6 +68,11 @@
"k \<noteq> l \<Longrightarrow> update k v (update l w m) = update l w (update k v m)"
by (cases m, simp add: expand_fun_eq)+
+lemma replace_update:
+ "lookup m k = None \<Longrightarrow> replace k v m = m"
+ "lookup m k \<noteq> None \<Longrightarrow> replace k v m = update k v m"
+ by (auto simp add: replace_def)
+
lemma delete_empty [simp]:
"delete k empty = empty"
by (simp add: empty_def)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Library/Quickcheck.thy Fri Feb 06 15:59:49 2009 +0100
@@ -0,0 +1,85 @@
+(* Author: Florian Haftmann, TU Muenchen *)
+
+header {* A simple counterexample generator *}
+
+theory Quickcheck
+imports Random Code_Eval Map
+begin
+
+subsection {* The @{text random} class *}
+
+class random = typerep +
+ fixes random :: "index \<Rightarrow> seed \<Rightarrow> ('a \<times> (unit \<Rightarrow> term)) \<times> seed"
+
+text {* Type @{typ "'a itself"} *}
+
+instantiation itself :: ("{type, typerep}") random
+begin
+
+definition
+ "random _ = return (TYPE('a), \<lambda>u. Code_Eval.Const (STR ''TYPE'') TYPEREP('a))"
+
+instance ..
+
+end
+
+
+subsection {* Quickcheck generator *}
+
+ML {*
+structure StateMonad =
+struct
+
+fun liftT T sT = sT --> HOLogic.mk_prodT (T, sT);
+fun liftT' sT = sT --> sT;
+
+fun return T sT x = Const (@{const_name Pair}, T --> liftT T sT) $ x;
+
+fun scomp T1 T2 sT f g = Const (@{const_name scomp},
+ liftT T1 sT --> (T1 --> liftT T2 sT) --> liftT T2 sT) $ f $ g;
+
+end;
+
+structure Quickcheck =
+struct
+
+open Quickcheck;
+
+val eval_ref : (unit -> int -> int * int -> term list option * (int * int)) option ref = ref NONE;
+
+fun mk_generator_expr thy prop tys =
+ let
+ val bound_max = length tys - 1;
+ val bounds = map_index (fn (i, ty) =>
+ (2 * (bound_max - i) + 1, 2 * (bound_max - i), 2 * i, ty)) tys;
+ val result = list_comb (prop, map (fn (i, _, _, _) => Bound i) bounds);
+ val terms = HOLogic.mk_list @{typ term} (map (fn (_, i, _, _) => Bound i $ @{term "()"}) bounds);
+ val check = @{term "If \<Colon> bool \<Rightarrow> term list option \<Rightarrow> term list option \<Rightarrow> term list option"}
+ $ result $ @{term "None \<Colon> term list option"} $ (@{term "Some \<Colon> term list \<Rightarrow> term list option "} $ terms);
+ val return = @{term "Pair \<Colon> term list option \<Rightarrow> seed \<Rightarrow> term list option \<times> seed"};
+ fun mk_termtyp ty = HOLogic.mk_prodT (ty, @{typ "unit \<Rightarrow> term"});
+ fun mk_split ty = Sign.mk_const thy
+ (@{const_name split}, [ty, @{typ "unit \<Rightarrow> term"}, StateMonad.liftT @{typ "term list option"} @{typ seed}]);
+ fun mk_scomp_split ty t t' =
+ StateMonad.scomp (mk_termtyp ty) @{typ "term list option"} @{typ seed} t (*FIXME*)
+ (mk_split ty $ Abs ("", ty, Abs ("", @{typ "unit \<Rightarrow> term"}, t')));
+ fun mk_bindclause (_, _, i, ty) = mk_scomp_split ty
+ (Sign.mk_const thy (@{const_name random}, [ty]) $ Bound i)
+ val t = fold_rev mk_bindclause bounds (return $ check);
+ in Abs ("n", @{typ index}, t) end;
+
+fun compile_generator_expr thy t =
+ let
+ val tys = (map snd o fst o strip_abs) t;
+ val t' = mk_generator_expr thy t tys;
+ val f = Code_ML.eval_term ("Quickcheck.eval_ref", eval_ref) thy t' [];
+ in f #> Random_Engine.run #> (Option.map o map) (Code.postprocess_term thy) end;
+
+end
+*}
+
+setup {*
+ Quickcheck.add_generator ("code", Quickcheck.compile_generator_expr o ProofContext.theory_of)
+*}
+
+end
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Library/Random.thy Fri Feb 06 15:59:49 2009 +0100
@@ -0,0 +1,181 @@
+(* Author: Florian Haftmann, TU Muenchen *)
+
+header {* A HOL random engine *}
+
+theory Random
+imports State_Monad Code_Index
+begin
+
+subsection {* Auxiliary functions *}
+
+definition
+ inc_shift :: "index \<Rightarrow> index \<Rightarrow> index"
+where
+ "inc_shift v k = (if v = k then 1 else k + 1)"
+
+definition
+ minus_shift :: "index \<Rightarrow> index \<Rightarrow> index \<Rightarrow> index"
+where
+ "minus_shift r k l = (if k < l then r + k - l else k - l)"
+
+fun
+ log :: "index \<Rightarrow> index \<Rightarrow> index"
+where
+ "log b i = (if b \<le> 1 \<or> i < b then 1 else 1 + log b (i div b))"
+
+subsection {* Random seeds *}
+
+types seed = "index \<times> index"
+
+primrec
+ "next" :: "seed \<Rightarrow> index \<times> seed"
+where
+ "next (v, w) = (let
+ k = v div 53668;
+ v' = minus_shift 2147483563 (40014 * (v mod 53668)) (k * 12211);
+ l = w div 52774;
+ w' = minus_shift 2147483399 (40692 * (w mod 52774)) (l * 3791);
+ z = minus_shift 2147483562 v' (w' + 1) + 1
+ in (z, (v', w')))"
+
+lemma next_not_0:
+ "fst (next s) \<noteq> 0"
+apply (cases s)
+apply (auto simp add: minus_shift_def Let_def)
+done
+
+primrec
+ seed_invariant :: "seed \<Rightarrow> bool"
+where
+ "seed_invariant (v, w) \<longleftrightarrow> 0 < v \<and> v < 9438322952 \<and> 0 < w \<and> True"
+
+lemma if_same:
+ "(if b then f x else f y) = f (if b then x else y)"
+ by (cases b) simp_all
+
+definition
+ split_seed :: "seed \<Rightarrow> seed \<times> seed"
+where
+ "split_seed s = (let
+ (v, w) = s;
+ (v', w') = snd (next s);
+ v'' = inc_shift 2147483562 v;
+ s'' = (v'', w');
+ w'' = inc_shift 2147483398 w;
+ s''' = (v', w'')
+ in (s'', s'''))"
+
+
+subsection {* Base selectors *}
+
+function
+ range_aux :: "index \<Rightarrow> index \<Rightarrow> seed \<Rightarrow> index \<times> seed"
+where
+ "range_aux k l s = (if k = 0 then (l, s) else
+ let (v, s') = next s
+ in range_aux (k - 1) (v + l * 2147483561) s')"
+by pat_completeness auto
+termination
+ by (relation "measure (Code_Index.nat_of o fst)")
+ (auto simp add: index)
+
+definition
+ range :: "index \<Rightarrow> seed \<Rightarrow> index \<times> seed"
+where
+ "range k = (do
+ v \<leftarrow> range_aux (log 2147483561 k) 1;
+ return (v mod k)
+ done)"
+
+lemma range:
+ assumes "k > 0"
+ shows "fst (range k s) < k"
+proof -
+ obtain v w where range_aux:
+ "range_aux (log 2147483561 k) 1 s = (v, w)"
+ by (cases "range_aux (log 2147483561 k) 1 s")
+ with assms show ?thesis
+ by (simp add: monad_collapse range_def del: range_aux.simps log.simps)
+qed
+
+definition
+ select :: "'a list \<Rightarrow> seed \<Rightarrow> 'a \<times> seed"
+where
+ "select xs = (do
+ k \<leftarrow> range (Code_Index.of_nat (length xs));
+ return (nth xs (Code_Index.nat_of k))
+ done)"
+
+lemma select:
+ assumes "xs \<noteq> []"
+ shows "fst (select xs s) \<in> set xs"
+proof -
+ from assms have "Code_Index.of_nat (length xs) > 0" by simp
+ with range have
+ "fst (range (Code_Index.of_nat (length xs)) s) < Code_Index.of_nat (length xs)" by best
+ then have
+ "Code_Index.nat_of (fst (range (Code_Index.of_nat (length xs)) s)) < length xs" by simp
+ then show ?thesis
+ by (auto simp add: monad_collapse select_def)
+qed
+
+definition
+ select_default :: "index \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> seed \<Rightarrow> 'a \<times> seed"
+where
+ [code del]: "select_default k x y = (do
+ l \<leftarrow> range k;
+ return (if l + 1 < k then x else y)
+ done)"
+
+lemma select_default_zero:
+ "fst (select_default 0 x y s) = y"
+ by (simp add: monad_collapse select_default_def)
+
+lemma select_default_code [code]:
+ "select_default k x y = (if k = 0 then do
+ _ \<leftarrow> range 1;
+ return y
+ done else do
+ l \<leftarrow> range k;
+ return (if l + 1 < k then x else y)
+ done)"
+proof (cases "k = 0")
+ case False then show ?thesis by (simp add: select_default_def)
+next
+ case True then show ?thesis
+ by (simp add: monad_collapse select_default_def range_def)
+qed
+
+
+subsection {* @{text ML} interface *}
+
+ML {*
+structure Random_Engine =
+struct
+
+type seed = int * int;
+
+local
+
+val seed = ref
+ (let
+ val now = Time.toMilliseconds (Time.now ());
+ val (q, s1) = IntInf.divMod (now, 2147483562);
+ val s2 = q mod 2147483398;
+ in (s1 + 1, s2 + 1) end);
+
+in
+
+fun run f =
+ let
+ val (x, seed') = f (! seed);
+ val _ = seed := seed'
+ in x end;
+
+end;
+
+end;
+*}
+
+end
+
--- a/src/HOL/Library/reflection.ML Fri Feb 06 15:57:47 2009 +0100
+++ b/src/HOL/Library/reflection.ML Fri Feb 06 15:59:49 2009 +0100
@@ -306,8 +306,8 @@
fun genreify_tac ctxt eqs to i = (fn st =>
let
- val P = HOLogic.dest_Trueprop (List.nth (prems_of st, i - 1))
- val t = (case to of NONE => P | SOME x => x)
+ fun P () = HOLogic.dest_Trueprop (List.nth (prems_of st, i - 1))
+ val t = (case to of NONE => P () | SOME x => x)
val th = (genreif ctxt eqs t) RS ssubst
in rtac th i st
end);
--- a/src/HOL/Lim.thy Fri Feb 06 15:57:47 2009 +0100
+++ b/src/HOL/Lim.thy Fri Feb 06 15:59:49 2009 +0100
@@ -532,6 +532,44 @@
lemma isCont_abs [simp]: "isCont abs (a::real)"
by (rule isCont_rabs [OF isCont_ident])
+lemma isCont_setsum: fixes A :: "nat set" assumes "finite A"
+ shows "\<forall> i \<in> A. isCont (f i) x \<Longrightarrow> isCont (\<lambda> x. \<Sum> i \<in> A. f i x) x"
+ using `finite A`
+proof induct
+ case (insert a F) show "isCont (\<lambda> x. \<Sum> i \<in> (insert a F). f i x) x"
+ unfolding setsum_insert[OF `finite F` `a \<notin> F`] by (rule isCont_add, auto simp add: insert)
+qed auto
+
+lemma LIM_less_bound: fixes f :: "real \<Rightarrow> real" assumes "b < x"
+ and all_le: "\<forall> x' \<in> { b <..< x}. 0 \<le> f x'" and isCont: "isCont f x"
+ shows "0 \<le> f x"
+proof (rule ccontr)
+ assume "\<not> 0 \<le> f x" hence "f x < 0" by auto
+ hence "0 < - f x / 2" by auto
+ from isCont[unfolded isCont_def, THEN LIM_D, OF this]
+ obtain s where "s > 0" and s_D: "\<And>x'. \<lbrakk> x' \<noteq> x ; \<bar> x' - x \<bar> < s \<rbrakk> \<Longrightarrow> \<bar> f x' - f x \<bar> < - f x / 2" by auto
+
+ let ?x = "x - min (s / 2) ((x - b) / 2)"
+ have "?x < x" and "\<bar> ?x - x \<bar> < s"
+ using `b < x` and `0 < s` by auto
+ have "b < ?x"
+ proof (cases "s < x - b")
+ case True thus ?thesis using `0 < s` by auto
+ next
+ case False hence "s / 2 \<ge> (x - b) / 2" by auto
+ from inf_absorb2[OF this, unfolded inf_real_def]
+ have "?x = (x + b) / 2" by auto
+ thus ?thesis using `b < x` by auto
+ qed
+ hence "0 \<le> f ?x" using all_le `?x < x` by auto
+ moreover have "\<bar>f ?x - f x\<bar> < - f x / 2"
+ using s_D[OF _ `\<bar> ?x - x \<bar> < s`] `?x < x` by auto
+ hence "f ?x - f x < - f x / 2" by auto
+ hence "f ?x < f x / 2" by auto
+ hence "f ?x < 0" using `f x < 0` by auto
+ thus False using `0 \<le> f ?x` by auto
+qed
+
subsection {* Uniform Continuity *}
--- a/src/HOL/MacLaurin.thy Fri Feb 06 15:57:47 2009 +0100
+++ b/src/HOL/MacLaurin.thy Fri Feb 06 15:59:49 2009 +0100
@@ -6,7 +6,7 @@
header{*MacLaurin Series*}
theory MacLaurin
-imports Dense_Linear_Order Transcendental
+imports Transcendental
begin
subsection{*Maclaurin's Theorem with Lagrange Form of Remainder*}
@@ -389,15 +389,6 @@
subsection{*Version for Sine Function*}
-lemma MVT2:
- "[| a < b; \<forall>x. a \<le> x & x \<le> b --> DERIV f x :> f'(x) |]
- ==> \<exists>z::real. a < z & z < b & (f b - f a = (b - a) * f'(z))"
-apply (drule MVT)
-apply (blast intro: DERIV_isCont)
-apply (force dest: order_less_imp_le simp add: differentiable_def)
-apply (blast dest: DERIV_unique order_less_imp_le)
-done
-
lemma mod_exhaust_less_4:
"m mod 4 = 0 | m mod 4 = 1 | m mod 4 = 2 | m mod 4 = (3::nat)"
by auto
--- a/src/HOL/Matrix/cplex/Cplex.thy Fri Feb 06 15:57:47 2009 +0100
+++ b/src/HOL/Matrix/cplex/Cplex.thy Fri Feb 06 15:59:49 2009 +0100
@@ -4,7 +4,7 @@
*)
theory Cplex
-imports SparseMatrix LP "~~/src/HOL/Real/Float" "~~/src/HOL/Tools/ComputeNumeral"
+imports SparseMatrix LP "~~/src/HOL/Tools/ComputeFloat" "~~/src/HOL/Tools/ComputeNumeral"
uses "Cplex_tools.ML" "CplexMatrixConverter.ML" "FloatSparseMatrixBuilder.ML"
"fspmlp.ML" ("matrixlp.ML")
begin
--- a/src/HOL/Matrix/cplex/matrixlp.ML Fri Feb 06 15:57:47 2009 +0100
+++ b/src/HOL/Matrix/cplex/matrixlp.ML Fri Feb 06 15:59:49 2009 +0100
@@ -67,7 +67,7 @@
local
val ths = ComputeHOL.prep_thms @{thms "ComputeHOL.compute_list_case" "ComputeHOL.compute_let"
- "ComputeHOL.compute_if" "Float.arith" "SparseMatrix.sparse_row_matrix_arith_simps"
+ "ComputeHOL.compute_if" "ComputeFloat.arith" "SparseMatrix.sparse_row_matrix_arith_simps"
"ComputeHOL.compute_bool" "ComputeHOL.compute_pair"
"SparseMatrix.sorted_sp_simps" "ComputeNumeral.number_norm"
"ComputeNumeral.natnorm"};
--- a/src/HOL/PReal.thy Fri Feb 06 15:57:47 2009 +0100
+++ b/src/HOL/PReal.thy Fri Feb 06 15:59:49 2009 +0100
@@ -9,7 +9,7 @@
header {* Positive real numbers *}
theory PReal
-imports Rational Dense_Linear_Order
+imports Rational
begin
text{*Could be generalized and moved to @{text Ring_and_Field}*}
@@ -22,6 +22,11 @@
A < {r. 0 < r} &
(\<forall>y \<in> A. ((\<forall>z. 0<z & z < y --> z \<in> A) & (\<exists>u \<in> A. y < u))))"
+lemma interval_empty_iff:
+ "{y. (x::'a::dense_linear_order) < y \<and> y < z} = {} \<longleftrightarrow> \<not> x < z"
+ by (auto dest: dense)
+
+
lemma cut_of_rat:
assumes q: "0 < q" shows "cut {r::rat. 0 < r & r < q}" (is "cut ?A")
proof -
--- a/src/HOL/Parity.thy Fri Feb 06 15:57:47 2009 +0100
+++ b/src/HOL/Parity.thy Fri Feb 06 15:59:49 2009 +0100
@@ -291,6 +291,40 @@
apply simp
done
+lemma power_mono_even: fixes x y :: "'a :: {recpower, ordered_idom}"
+ assumes "even n" and "\<bar>x\<bar> \<le> \<bar>y\<bar>"
+ shows "x^n \<le> y^n"
+proof -
+ have "0 \<le> \<bar>x\<bar>" by auto
+ with `\<bar>x\<bar> \<le> \<bar>y\<bar>`
+ have "\<bar>x\<bar>^n \<le> \<bar>y\<bar>^n" by (rule power_mono)
+ thus ?thesis unfolding power_even_abs[OF `even n`] .
+qed
+
+lemma odd_pos: "odd (n::nat) \<Longrightarrow> 0 < n" by presburger
+
+lemma power_mono_odd: fixes x y :: "'a :: {recpower, ordered_idom}"
+ assumes "odd n" and "x \<le> y"
+ shows "x^n \<le> y^n"
+proof (cases "y < 0")
+ case True with `x \<le> y` have "-y \<le> -x" and "0 \<le> -y" by auto
+ hence "(-y)^n \<le> (-x)^n" by (rule power_mono)
+ thus ?thesis unfolding power_minus_odd[OF `odd n`] by auto
+next
+ case False
+ show ?thesis
+ proof (cases "x < 0")
+ case True hence "n \<noteq> 0" and "x \<le> 0" using `odd n`[THEN odd_pos] by auto
+ hence "x^n \<le> 0" unfolding power_le_zero_eq using `odd n` by auto
+ moreover
+ from `\<not> y < 0` have "0 \<le> y" by auto
+ hence "0 \<le> y^n" by auto
+ ultimately show ?thesis by auto
+ next
+ case False hence "0 \<le> x" by auto
+ with `x \<le> y` show ?thesis using power_mono by auto
+ qed
+qed
subsection {* General Lemmas About Division *}
@@ -405,8 +439,6 @@
subsection {* Miscellaneous *}
-lemma odd_pos: "odd (n::nat) \<Longrightarrow> 0 < n" by presburger
-
lemma [presburger]:"(x + 1) div 2 = x div 2 \<longleftrightarrow> even (x::int)" by presburger
lemma [presburger]: "(x + 1) div 2 = x div 2 + 1 \<longleftrightarrow> odd (x::int)" by presburger
lemma even_plus_one_div_two: "even (x::int) ==> (x + 1) div 2 = x div 2" by presburger
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Reflection/Approximation.thy Fri Feb 06 15:59:49 2009 +0100
@@ -0,0 +1,2507 @@
+(* Title: HOL/Reflection/Approximation.thy
+ * Author: Johannes Hölzl <hoelzl@in.tum.de> 2008 / 2009
+ *)
+header {* Prove unequations about real numbers by computation *}
+theory Approximation
+imports Complex_Main Float Reflection Efficient_Nat
+begin
+
+section "Horner Scheme"
+
+subsection {* Define auxiliary helper @{text horner} function *}
+
+fun horner :: "(nat \<Rightarrow> nat) \<Rightarrow> (nat \<Rightarrow> nat \<Rightarrow> nat) \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> real \<Rightarrow> real" where
+"horner F G 0 i k x = 0" |
+"horner F G (Suc n) i k x = 1 / real k - x * horner F G n (F i) (G i k) x"
+
+lemma horner_schema': fixes x :: real and a :: "nat \<Rightarrow> real"
+ shows "a 0 - x * (\<Sum> i=0..<n. (-1)^i * a (Suc i) * x^i) = (\<Sum> i=0..<Suc n. (-1)^i * a i * x^i)"
+proof -
+ have shift_pow: "\<And>i. - (x * ((-1)^i * a (Suc i) * x ^ i)) = (-1)^(Suc i) * a (Suc i) * x ^ (Suc i)" by auto
+ show ?thesis unfolding setsum_right_distrib shift_pow real_diff_def setsum_negf[symmetric] setsum_head_upt_Suc[OF zero_less_Suc]
+ setsum_reindex[OF inj_Suc, unfolded comp_def, symmetric, of "\<lambda> n. (-1)^n *a n * x^n"] by auto
+qed
+
+lemma horner_schema: fixes f :: "nat \<Rightarrow> nat" and G :: "nat \<Rightarrow> nat \<Rightarrow> nat" and F :: "nat \<Rightarrow> nat"
+ assumes f_Suc: "\<And>n. f (Suc n) = G ((F^n) s) (f n)"
+ shows "horner F G n ((F^j') s) (f j') x = (\<Sum> j = 0..< n. -1^j * (1 / real (f (j' + j))) * x^j)"
+proof (induct n arbitrary: i k j')
+ case (Suc n)
+
+ show ?case unfolding horner.simps Suc[where j'="Suc j'", unfolded funpow.simps comp_def f_Suc]
+ using horner_schema'[of "\<lambda> j. 1 / real (f (j' + j))"] by auto
+qed auto
+
+lemma horner_bounds':
+ assumes "0 \<le> Ifloat x" and f_Suc: "\<And>n. f (Suc n) = G ((F^n) s) (f n)"
+ and lb_0: "\<And> i k x. lb 0 i k x = 0"
+ and lb_Suc: "\<And> n i k x. lb (Suc n) i k x = lapprox_rat prec 1 (int k) - x * (ub n (F i) (G i k) x)"
+ and ub_0: "\<And> i k x. ub 0 i k x = 0"
+ and ub_Suc: "\<And> n i k x. ub (Suc n) i k x = rapprox_rat prec 1 (int k) - x * (lb n (F i) (G i k) x)"
+ shows "Ifloat (lb n ((F^j') s) (f j') x) \<le> horner F G n ((F^j') s) (f j') (Ifloat x) \<and>
+ horner F G n ((F^j') s) (f j') (Ifloat x) \<le> Ifloat (ub n ((F^j') s) (f j') x)"
+ (is "?lb n j' \<le> ?horner n j' \<and> ?horner n j' \<le> ?ub n j'")
+proof (induct n arbitrary: j')
+ case 0 thus ?case unfolding lb_0 ub_0 horner.simps by auto
+next
+ case (Suc n)
+ have "?lb (Suc n) j' \<le> ?horner (Suc n) j'" unfolding lb_Suc ub_Suc horner.simps Ifloat_sub diff_def
+ proof (rule add_mono)
+ show "Ifloat (lapprox_rat prec 1 (int (f j'))) \<le> 1 / real (f j')" using lapprox_rat[of prec 1 "int (f j')"] by auto
+ from Suc[where j'="Suc j'", unfolded funpow.simps comp_def f_Suc, THEN conjunct2] `0 \<le> Ifloat x`
+ show "- Ifloat (x * ub n (F ((F ^ j') s)) (G ((F ^ j') s) (f j')) x) \<le> - (Ifloat x * horner F G n (F ((F ^ j') s)) (G ((F ^ j') s) (f j')) (Ifloat x))"
+ unfolding Ifloat_mult neg_le_iff_le by (rule mult_left_mono)
+ qed
+ moreover have "?horner (Suc n) j' \<le> ?ub (Suc n) j'" unfolding ub_Suc ub_Suc horner.simps Ifloat_sub diff_def
+ proof (rule add_mono)
+ show "1 / real (f j') \<le> Ifloat (rapprox_rat prec 1 (int (f j')))" using rapprox_rat[of 1 "int (f j')" prec] by auto
+ from Suc[where j'="Suc j'", unfolded funpow.simps comp_def f_Suc, THEN conjunct1] `0 \<le> Ifloat x`
+ show "- (Ifloat x * horner F G n (F ((F ^ j') s)) (G ((F ^ j') s) (f j')) (Ifloat x)) \<le>
+ - Ifloat (x * lb n (F ((F ^ j') s)) (G ((F ^ j') s) (f j')) x)"
+ unfolding Ifloat_mult neg_le_iff_le by (rule mult_left_mono)
+ qed
+ ultimately show ?case by blast
+qed
+
+subsection "Theorems for floating point functions implementing the horner scheme"
+
+text {*
+
+Here @{term_type "f :: nat \<Rightarrow> nat"} is the sequence defining the Taylor series, the coefficients are
+all alternating and reciprocs. We use @{term G} and @{term F} to describe the computation of @{term f}.
+
+*}
+
+lemma horner_bounds: fixes F :: "nat \<Rightarrow> nat" and G :: "nat \<Rightarrow> nat \<Rightarrow> nat"
+ assumes "0 \<le> Ifloat x" and f_Suc: "\<And>n. f (Suc n) = G ((F^n) s) (f n)"
+ and lb_0: "\<And> i k x. lb 0 i k x = 0"
+ and lb_Suc: "\<And> n i k x. lb (Suc n) i k x = lapprox_rat prec 1 (int k) - x * (ub n (F i) (G i k) x)"
+ and ub_0: "\<And> i k x. ub 0 i k x = 0"
+ and ub_Suc: "\<And> n i k x. ub (Suc n) i k x = rapprox_rat prec 1 (int k) - x * (lb n (F i) (G i k) x)"
+ shows "Ifloat (lb n ((F^j') s) (f j') x) \<le> (\<Sum>j=0..<n. -1^j * (1 / real (f (j' + j))) * (Ifloat x)^j)" (is "?lb") and
+ "(\<Sum>j=0..<n. -1^j * (1 / real (f (j' + j))) * (Ifloat x)^j) \<le> Ifloat (ub n ((F^j') s) (f j') x)" (is "?ub")
+proof -
+ have "?lb \<and> ?ub"
+ using horner_bounds'[where lb=lb, OF `0 \<le> Ifloat x` f_Suc lb_0 lb_Suc ub_0 ub_Suc]
+ unfolding horner_schema[where f=f, OF f_Suc] .
+ thus "?lb" and "?ub" by auto
+qed
+
+lemma horner_bounds_nonpos: fixes F :: "nat \<Rightarrow> nat" and G :: "nat \<Rightarrow> nat \<Rightarrow> nat"
+ assumes "Ifloat x \<le> 0" and f_Suc: "\<And>n. f (Suc n) = G ((F^n) s) (f n)"
+ and lb_0: "\<And> i k x. lb 0 i k x = 0"
+ and lb_Suc: "\<And> n i k x. lb (Suc n) i k x = lapprox_rat prec 1 (int k) + x * (ub n (F i) (G i k) x)"
+ and ub_0: "\<And> i k x. ub 0 i k x = 0"
+ and ub_Suc: "\<And> n i k x. ub (Suc n) i k x = rapprox_rat prec 1 (int k) + x * (lb n (F i) (G i k) x)"
+ shows "Ifloat (lb n ((F^j') s) (f j') x) \<le> (\<Sum>j=0..<n. (1 / real (f (j' + j))) * (Ifloat x)^j)" (is "?lb") and
+ "(\<Sum>j=0..<n. (1 / real (f (j' + j))) * (Ifloat x)^j) \<le> Ifloat (ub n ((F^j') s) (f j') x)" (is "?ub")
+proof -
+ { fix x y z :: float have "x - y * z = x + - y * z"
+ by (cases x, cases y, cases z, simp add: plus_float.simps minus_float.simps uminus_float.simps times_float.simps algebra_simps)
+ } note diff_mult_minus = this
+
+ { fix x :: float have "- (- x) = x" by (cases x, auto simp add: uminus_float.simps) } note minus_minus = this
+
+ have move_minus: "Ifloat (-x) = -1 * Ifloat x" by auto
+
+ have sum_eq: "(\<Sum>j=0..<n. (1 / real (f (j' + j))) * (Ifloat x)^j) =
+ (\<Sum>j = 0..<n. -1 ^ j * (1 / real (f (j' + j))) * Ifloat (- x) ^ j)"
+ proof (rule setsum_cong, simp)
+ fix j assume "j \<in> {0 ..< n}"
+ show "1 / real (f (j' + j)) * Ifloat x ^ j = -1 ^ j * (1 / real (f (j' + j))) * Ifloat (- x) ^ j"
+ unfolding move_minus power_mult_distrib real_mult_assoc[symmetric]
+ unfolding real_mult_commute unfolding real_mult_assoc[of "-1^j", symmetric] power_mult_distrib[symmetric]
+ by auto
+ qed
+
+ have "0 \<le> Ifloat (-x)" using assms by auto
+ from horner_bounds[where G=G and F=F and f=f and s=s and prec=prec
+ and lb="\<lambda> n i k x. lb n i k (-x)" and ub="\<lambda> n i k x. ub n i k (-x)", unfolded lb_Suc ub_Suc diff_mult_minus,
+ OF this f_Suc lb_0 refl ub_0 refl]
+ show "?lb" and "?ub" unfolding minus_minus sum_eq
+ by auto
+qed
+
+subsection {* Selectors for next even or odd number *}
+
+text {*
+
+The horner scheme computes alternating series. To get the upper and lower bounds we need to
+guarantee to access a even or odd member. To do this we use @{term get_odd} and @{term get_even}.
+
+*}
+
+definition get_odd :: "nat \<Rightarrow> nat" where
+ "get_odd n = (if odd n then n else (Suc n))"
+
+definition get_even :: "nat \<Rightarrow> nat" where
+ "get_even n = (if even n then n else (Suc n))"
+
+lemma get_odd[simp]: "odd (get_odd n)" unfolding get_odd_def by (cases "odd n", auto)
+lemma get_even[simp]: "even (get_even n)" unfolding get_even_def by (cases "even n", auto)
+lemma get_odd_ex: "\<exists> k. Suc k = get_odd n \<and> odd (Suc k)"
+proof (cases "odd n")
+ case True hence "0 < n" by (rule odd_pos)
+ from gr0_implies_Suc[OF this] obtain k where "Suc k = n" by auto
+ thus ?thesis unfolding get_odd_def if_P[OF True] using True[unfolded `Suc k = n`[symmetric]] by blast
+next
+ case False hence "odd (Suc n)" by auto
+ thus ?thesis unfolding get_odd_def if_not_P[OF False] by blast
+qed
+
+lemma get_even_double: "\<exists>i. get_even n = 2 * i" using get_even[unfolded even_mult_two_ex] .
+lemma get_odd_double: "\<exists>i. get_odd n = 2 * i + 1" using get_odd[unfolded odd_Suc_mult_two_ex] by auto
+
+section "Power function"
+
+definition float_power_bnds :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float * float" where
+"float_power_bnds n l u = (if odd n \<or> 0 < l then (l ^ n, u ^ n)
+ else if u < 0 then (u ^ n, l ^ n)
+ else (0, (max (-l) u) ^ n))"
+
+lemma float_power_bnds: assumes "(l1, u1) = float_power_bnds n l u" and "x \<in> {Ifloat l .. Ifloat u}"
+ shows "x^n \<in> {Ifloat l1..Ifloat u1}"
+proof (cases "even n")
+ case True
+ show ?thesis
+ proof (cases "0 < l")
+ case True hence "odd n \<or> 0 < l" and "0 \<le> Ifloat l" unfolding less_float_def by auto
+ have u1: "u1 = u ^ n" and l1: "l1 = l ^ n" using assms unfolding float_power_bnds_def if_P[OF `odd n \<or> 0 < l`] by auto
+ have "Ifloat l^n \<le> x^n" and "x^n \<le> Ifloat u^n " using `0 \<le> Ifloat l` and assms unfolding atLeastAtMost_iff using power_mono[of "Ifloat l" x] power_mono[of x "Ifloat u"] by auto
+ thus ?thesis using assms `0 < l` unfolding atLeastAtMost_iff l1 u1 float_power less_float_def by auto
+ next
+ case False hence P: "\<not> (odd n \<or> 0 < l)" using `even n` by auto
+ show ?thesis
+ proof (cases "u < 0")
+ case True hence "0 \<le> - Ifloat u" and "- Ifloat u \<le> - x" and "0 \<le> - x" and "-x \<le> - Ifloat l" using assms unfolding less_float_def by auto
+ hence "Ifloat u^n \<le> x^n" and "x^n \<le> Ifloat l^n" using power_mono[of "-x" "-Ifloat l" n] power_mono[of "-Ifloat u" "-x" n]
+ unfolding power_minus_even[OF `even n`] by auto
+ moreover have u1: "u1 = l ^ n" and l1: "l1 = u ^ n" using assms unfolding float_power_bnds_def if_not_P[OF P] if_P[OF True] by auto
+ ultimately show ?thesis using float_power by auto
+ next
+ case False
+ have "\<bar>x\<bar> \<le> Ifloat (max (-l) u)"
+ proof (cases "-l \<le> u")
+ case True thus ?thesis unfolding max_def if_P[OF True] using assms unfolding le_float_def by auto
+ next
+ case False thus ?thesis unfolding max_def if_not_P[OF False] using assms unfolding le_float_def by auto
+ qed
+ hence x_abs: "\<bar>x\<bar> \<le> \<bar>Ifloat (max (-l) u)\<bar>" by auto
+ have u1: "u1 = (max (-l) u) ^ n" and l1: "l1 = 0" using assms unfolding float_power_bnds_def if_not_P[OF P] if_not_P[OF False] by auto
+ show ?thesis unfolding atLeastAtMost_iff l1 u1 float_power using zero_le_even_power[OF `even n`] power_mono_even[OF `even n` x_abs] by auto
+ qed
+ qed
+next
+ case False hence "odd n \<or> 0 < l" by auto
+ have u1: "u1 = u ^ n" and l1: "l1 = l ^ n" using assms unfolding float_power_bnds_def if_P[OF `odd n \<or> 0 < l`] by auto
+ have "Ifloat l^n \<le> x^n" and "x^n \<le> Ifloat u^n " using assms unfolding atLeastAtMost_iff using power_mono_odd[OF False] by auto
+ thus ?thesis unfolding atLeastAtMost_iff l1 u1 float_power less_float_def by auto
+qed
+
+lemma bnds_power: "\<forall> x l u. (l1, u1) = float_power_bnds n l u \<and> x \<in> {Ifloat l .. Ifloat u} \<longrightarrow> Ifloat l1 \<le> x^n \<and> x^n \<le> Ifloat u1"
+ using float_power_bnds by auto
+
+section "Square root"
+
+text {*
+
+The square root computation is implemented as newton iteration. As first first step we use the
+nearest power of two greater than the square root.
+
+*}
+
+fun sqrt_iteration :: "nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float" where
+"sqrt_iteration prec 0 (Float m e) = Float 1 ((e + bitlen m) div 2 + 1)" |
+"sqrt_iteration prec (Suc m) x = (let y = sqrt_iteration prec m x
+ in Float 1 -1 * (y + float_divr prec x y))"
+
+definition ub_sqrt :: "nat \<Rightarrow> float \<Rightarrow> float option" where
+"ub_sqrt prec x = (if 0 < x then Some (sqrt_iteration prec prec x) else if x < 0 then None else Some 0)"
+
+definition lb_sqrt :: "nat \<Rightarrow> float \<Rightarrow> float option" where
+"lb_sqrt prec x = (if 0 < x then Some (float_divl prec x (sqrt_iteration prec prec x)) else if x < 0 then None else Some 0)"
+
+lemma sqrt_ub_pos_pos_1:
+ assumes "sqrt x < b" and "0 < b" and "0 < x"
+ shows "sqrt x < (b + x / b)/2"
+proof -
+ from assms have "0 < (b - sqrt x) ^ 2 " by simp
+ also have "\<dots> = b ^ 2 - 2 * b * sqrt x + (sqrt x) ^ 2" by algebra
+ also have "\<dots> = b ^ 2 - 2 * b * sqrt x + x" using assms by (simp add: real_sqrt_pow2)
+ finally have "0 < b ^ 2 - 2 * b * sqrt x + x" by assumption
+ hence "0 < b / 2 - sqrt x + x / (2 * b)" using assms
+ by (simp add: field_simps power2_eq_square)
+ thus ?thesis by (simp add: field_simps)
+qed
+
+lemma sqrt_iteration_bound: assumes "0 < Ifloat x"
+ shows "sqrt (Ifloat x) < Ifloat (sqrt_iteration prec n x)"
+proof (induct n)
+ case 0
+ show ?case
+ proof (cases x)
+ case (Float m e)
+ hence "0 < m" using float_pos_m_pos[unfolded less_float_def] assms by auto
+ hence "0 < sqrt (real m)" by auto
+
+ have int_nat_bl: "int (nat (bitlen m)) = bitlen m" using bitlen_ge0 by auto
+
+ have "Ifloat x = (real m / 2^nat (bitlen m)) * pow2 (e + int (nat (bitlen m)))"
+ unfolding pow2_add pow2_int Float Ifloat.simps by auto
+ also have "\<dots> < 1 * pow2 (e + int (nat (bitlen m)))"
+ proof (rule mult_strict_right_mono, auto)
+ show "real m < 2^nat (bitlen m)" using bitlen_bounds[OF `0 < m`, THEN conjunct2]
+ unfolding real_of_int_less_iff[of m, symmetric] by auto
+ qed
+ finally have "sqrt (Ifloat x) < sqrt (pow2 (e + bitlen m))" unfolding int_nat_bl by auto
+ also have "\<dots> \<le> pow2 ((e + bitlen m) div 2 + 1)"
+ proof -
+ let ?E = "e + bitlen m"
+ have E_mod_pow: "pow2 (?E mod 2) < 4"
+ proof (cases "?E mod 2 = 1")
+ case True thus ?thesis by auto
+ next
+ case False
+ have "0 \<le> ?E mod 2" by auto
+ have "?E mod 2 < 2" by auto
+ from this[THEN zless_imp_add1_zle]
+ have "?E mod 2 \<le> 0" using False by auto
+ from xt1(5)[OF `0 \<le> ?E mod 2` this]
+ show ?thesis by auto
+ qed
+ hence "sqrt (pow2 (?E mod 2)) < sqrt (2 * 2)" by auto
+ hence E_mod_pow: "sqrt (pow2 (?E mod 2)) < 2" unfolding real_sqrt_abs2 by auto
+
+ have E_eq: "pow2 ?E = pow2 (?E div 2 + ?E div 2 + ?E mod 2)" by auto
+ have "sqrt (pow2 ?E) = sqrt (pow2 (?E div 2) * pow2 (?E div 2) * pow2 (?E mod 2))"
+ unfolding E_eq unfolding pow2_add ..
+ also have "\<dots> = pow2 (?E div 2) * sqrt (pow2 (?E mod 2))"
+ unfolding real_sqrt_mult[of _ "pow2 (?E mod 2)"] real_sqrt_abs2 by auto
+ also have "\<dots> < pow2 (?E div 2) * 2"
+ by (rule mult_strict_left_mono, auto intro: E_mod_pow)
+ also have "\<dots> = pow2 (?E div 2 + 1)" unfolding zadd_commute[of _ 1] pow2_add1 by auto
+ finally show ?thesis by auto
+ qed
+ finally show ?thesis
+ unfolding Float sqrt_iteration.simps Ifloat.simps by auto
+ qed
+next
+ case (Suc n)
+ let ?b = "sqrt_iteration prec n x"
+ have "0 < sqrt (Ifloat x)" using `0 < Ifloat x` by auto
+ also have "\<dots> < Ifloat ?b" using Suc .
+ finally have "sqrt (Ifloat x) < (Ifloat ?b + Ifloat x / Ifloat ?b)/2" using sqrt_ub_pos_pos_1[OF Suc _ `0 < Ifloat x`] by auto
+ also have "\<dots> \<le> (Ifloat ?b + Ifloat (float_divr prec x ?b))/2" by (rule divide_right_mono, auto simp add: float_divr)
+ also have "\<dots> = Ifloat (Float 1 -1) * (Ifloat ?b + Ifloat (float_divr prec x ?b))" by auto
+ finally show ?case unfolding sqrt_iteration.simps Let_def Ifloat_mult Ifloat_add right_distrib .
+qed
+
+lemma sqrt_iteration_lower_bound: assumes "0 < Ifloat x"
+ shows "0 < Ifloat (sqrt_iteration prec n x)" (is "0 < ?sqrt")
+proof -
+ have "0 < sqrt (Ifloat x)" using assms by auto
+ also have "\<dots> < ?sqrt" using sqrt_iteration_bound[OF assms] .
+ finally show ?thesis .
+qed
+
+lemma lb_sqrt_lower_bound: assumes "0 \<le> Ifloat x"
+ shows "0 \<le> Ifloat (the (lb_sqrt prec x))"
+proof (cases "0 < x")
+ case True hence "0 < Ifloat x" and "0 \<le> x" using `0 \<le> Ifloat x` unfolding less_float_def le_float_def by auto
+ hence "0 < sqrt_iteration prec prec x" unfolding less_float_def using sqrt_iteration_lower_bound by auto
+ hence "0 \<le> Ifloat (float_divl prec x (sqrt_iteration prec prec x))" using float_divl_lower_bound[OF `0 \<le> x`] unfolding le_float_def by auto
+ thus ?thesis unfolding lb_sqrt_def using True by auto
+next
+ case False with `0 \<le> Ifloat x` have "Ifloat x = 0" unfolding less_float_def by auto
+ thus ?thesis unfolding lb_sqrt_def less_float_def by auto
+qed
+
+lemma lb_sqrt_upper_bound: assumes "0 \<le> Ifloat x"
+ shows "Ifloat (the (lb_sqrt prec x)) \<le> sqrt (Ifloat x)"
+proof (cases "0 < x")
+ case True hence "0 < Ifloat x" and "0 \<le> Ifloat x" unfolding less_float_def by auto
+ hence sqrt_gt0: "0 < sqrt (Ifloat x)" by auto
+ hence sqrt_ub: "sqrt (Ifloat x) < Ifloat (sqrt_iteration prec prec x)" using sqrt_iteration_bound by auto
+
+ have "Ifloat (float_divl prec x (sqrt_iteration prec prec x)) \<le> Ifloat x / Ifloat (sqrt_iteration prec prec x)" by (rule float_divl)
+ also have "\<dots> < Ifloat x / sqrt (Ifloat x)"
+ by (rule divide_strict_left_mono[OF sqrt_ub `0 < Ifloat x` mult_pos_pos[OF order_less_trans[OF sqrt_gt0 sqrt_ub] sqrt_gt0]])
+ also have "\<dots> = sqrt (Ifloat x)" unfolding inverse_eq_iff_eq[of _ "sqrt (Ifloat x)", symmetric] sqrt_divide_self_eq[OF `0 \<le> Ifloat x`, symmetric] by auto
+ finally show ?thesis unfolding lb_sqrt_def if_P[OF `0 < x`] by auto
+next
+ case False with `0 \<le> Ifloat x`
+ have "\<not> x < 0" unfolding less_float_def le_float_def by auto
+ show ?thesis unfolding lb_sqrt_def if_not_P[OF False] if_not_P[OF `\<not> x < 0`] using assms by auto
+qed
+
+lemma lb_sqrt: assumes "Some y = lb_sqrt prec x"
+ shows "Ifloat y \<le> sqrt (Ifloat x)" and "0 \<le> Ifloat x"
+proof -
+ show "0 \<le> Ifloat x"
+ proof (rule ccontr)
+ assume "\<not> 0 \<le> Ifloat x"
+ hence "lb_sqrt prec x = None" unfolding lb_sqrt_def less_float_def by auto
+ thus False using assms by auto
+ qed
+ from lb_sqrt_upper_bound[OF this, of prec]
+ show "Ifloat y \<le> sqrt (Ifloat x)" unfolding assms[symmetric] by auto
+qed
+
+lemma ub_sqrt_lower_bound: assumes "0 \<le> Ifloat x"
+ shows "sqrt (Ifloat x) \<le> Ifloat (the (ub_sqrt prec x))"
+proof (cases "0 < x")
+ case True hence "0 < Ifloat x" unfolding less_float_def by auto
+ hence "0 < sqrt (Ifloat x)" by auto
+ hence "sqrt (Ifloat x) < Ifloat (sqrt_iteration prec prec x)" using sqrt_iteration_bound by auto
+ thus ?thesis unfolding ub_sqrt_def if_P[OF `0 < x`] by auto
+next
+ case False with `0 \<le> Ifloat x`
+ have "Ifloat x = 0" unfolding less_float_def le_float_def by auto
+ thus ?thesis unfolding ub_sqrt_def less_float_def le_float_def by auto
+qed
+
+lemma ub_sqrt: assumes "Some y = ub_sqrt prec x"
+ shows "sqrt (Ifloat x) \<le> Ifloat y" and "0 \<le> Ifloat x"
+proof -
+ show "0 \<le> Ifloat x"
+ proof (rule ccontr)
+ assume "\<not> 0 \<le> Ifloat x"
+ hence "ub_sqrt prec x = None" unfolding ub_sqrt_def less_float_def by auto
+ thus False using assms by auto
+ qed
+ from ub_sqrt_lower_bound[OF this, of prec]
+ show "sqrt (Ifloat x) \<le> Ifloat y" unfolding assms[symmetric] by auto
+qed
+
+lemma bnds_sqrt: "\<forall> x lx ux. (Some l, Some u) = (lb_sqrt prec lx, ub_sqrt prec ux) \<and> x \<in> {Ifloat lx .. Ifloat ux} \<longrightarrow> Ifloat l \<le> sqrt x \<and> sqrt x \<le> Ifloat u"
+proof (rule allI, rule allI, rule allI, rule impI)
+ fix x lx ux
+ assume "(Some l, Some u) = (lb_sqrt prec lx, ub_sqrt prec ux) \<and> x \<in> {Ifloat lx .. Ifloat ux}"
+ hence l: "Some l = lb_sqrt prec lx " and u: "Some u = ub_sqrt prec ux" and x: "x \<in> {Ifloat lx .. Ifloat ux}" by auto
+
+ have "Ifloat lx \<le> x" and "x \<le> Ifloat ux" using x by auto
+
+ from lb_sqrt(1)[OF l] real_sqrt_le_mono[OF `Ifloat lx \<le> x`]
+ have "Ifloat l \<le> sqrt x" by (rule order_trans)
+ moreover
+ from real_sqrt_le_mono[OF `x \<le> Ifloat ux`] ub_sqrt(1)[OF u]
+ have "sqrt x \<le> Ifloat u" by (rule order_trans)
+ ultimately show "Ifloat l \<le> sqrt x \<and> sqrt x \<le> Ifloat u" ..
+qed
+
+section "Arcus tangens and \<pi>"
+
+subsection "Compute arcus tangens series"
+
+text {*
+
+As first step we implement the computation of the arcus tangens series. This is only valid in the range
+@{term "{-1 :: real .. 1}"}. This is used to compute \<pi> and then the entire arcus tangens.
+
+*}
+
+fun ub_arctan_horner :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float"
+and lb_arctan_horner :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float" where
+ "ub_arctan_horner prec 0 k x = 0"
+| "ub_arctan_horner prec (Suc n) k x =
+ (rapprox_rat prec 1 (int k)) - x * (lb_arctan_horner prec n (k + 2) x)"
+| "lb_arctan_horner prec 0 k x = 0"
+| "lb_arctan_horner prec (Suc n) k x =
+ (lapprox_rat prec 1 (int k)) - x * (ub_arctan_horner prec n (k + 2) x)"
+
+lemma arctan_0_1_bounds': assumes "0 \<le> Ifloat x" "Ifloat x \<le> 1" and "even n"
+ shows "arctan (Ifloat x) \<in> {Ifloat (x * lb_arctan_horner prec n 1 (x * x)) .. Ifloat (x * ub_arctan_horner prec (Suc n) 1 (x * x))}"
+proof -
+ let "?c i" = "-1^i * (1 / real (i * 2 + 1) * Ifloat x ^ (i * 2 + 1))"
+ let "?S n" = "\<Sum> i=0..<n. ?c i"
+
+ have "0 \<le> Ifloat (x * x)" by auto
+ from `even n` obtain m where "2 * m = n" unfolding even_mult_two_ex by auto
+
+ have "arctan (Ifloat x) \<in> { ?S n .. ?S (Suc n) }"
+ proof (cases "Ifloat x = 0")
+ case False
+ hence "0 < Ifloat x" using `0 \<le> Ifloat x` by auto
+ hence prem: "0 < 1 / real (0 * 2 + (1::nat)) * Ifloat x ^ (0 * 2 + 1)" by auto
+
+ have "\<bar> Ifloat x \<bar> \<le> 1" using `0 \<le> Ifloat x` `Ifloat x \<le> 1` by auto
+ from mp[OF summable_Leibniz(2)[OF zeroseq_arctan_series[OF this] monoseq_arctan_series[OF this]] prem, THEN spec, of m, unfolded `2 * m = n`]
+ show ?thesis unfolding arctan_series[OF `\<bar> Ifloat x \<bar> \<le> 1`] Suc_plus1 .
+ qed auto
+ note arctan_bounds = this[unfolded atLeastAtMost_iff]
+
+ have F: "\<And>n. 2 * Suc n + 1 = 2 * n + 1 + 2" by auto
+
+ note bounds = horner_bounds[where s=1 and f="\<lambda>i. 2 * i + 1" and j'=0
+ and lb="\<lambda>n i k x. lb_arctan_horner prec n k x"
+ and ub="\<lambda>n i k x. ub_arctan_horner prec n k x",
+ OF `0 \<le> Ifloat (x*x)` F lb_arctan_horner.simps ub_arctan_horner.simps]
+
+ { have "Ifloat (x * lb_arctan_horner prec n 1 (x*x)) \<le> ?S n"
+ using bounds(1) `0 \<le> Ifloat x`
+ unfolding Ifloat_mult power_add power_one_right real_mult_assoc[symmetric] setsum_left_distrib[symmetric]
+ unfolding real_mult_commute mult_commute[of _ "2::nat"] power_mult power2_eq_square[of "Ifloat x"]
+ by (auto intro!: mult_left_mono)
+ also have "\<dots> \<le> arctan (Ifloat x)" using arctan_bounds ..
+ finally have "Ifloat (x * lb_arctan_horner prec n 1 (x*x)) \<le> arctan (Ifloat x)" . }
+ moreover
+ { have "arctan (Ifloat x) \<le> ?S (Suc n)" using arctan_bounds ..
+ also have "\<dots> \<le> Ifloat (x * ub_arctan_horner prec (Suc n) 1 (x*x))"
+ using bounds(2)[of "Suc n"] `0 \<le> Ifloat x`
+ unfolding Ifloat_mult power_add power_one_right real_mult_assoc[symmetric] setsum_left_distrib[symmetric]
+ unfolding real_mult_commute mult_commute[of _ "2::nat"] power_mult power2_eq_square[of "Ifloat x"]
+ by (auto intro!: mult_left_mono)
+ finally have "arctan (Ifloat x) \<le> Ifloat (x * ub_arctan_horner prec (Suc n) 1 (x*x))" . }
+ ultimately show ?thesis by auto
+qed
+
+lemma arctan_0_1_bounds: assumes "0 \<le> Ifloat x" "Ifloat x \<le> 1"
+ shows "arctan (Ifloat x) \<in> {Ifloat (x * lb_arctan_horner prec (get_even n) 1 (x * x)) .. Ifloat (x * ub_arctan_horner prec (get_odd n) 1 (x * x))}"
+proof (cases "even n")
+ case True
+ obtain n' where "Suc n' = get_odd n" and "odd (Suc n')" using get_odd_ex by auto
+ hence "even n'" unfolding even_nat_Suc by auto
+ have "arctan (Ifloat x) \<le> Ifloat (x * ub_arctan_horner prec (get_odd n) 1 (x * x))"
+ unfolding `Suc n' = get_odd n`[symmetric] using arctan_0_1_bounds'[OF `0 \<le> Ifloat x` `Ifloat x \<le> 1` `even n'`] by auto
+ moreover
+ have "Ifloat (x * lb_arctan_horner prec (get_even n) 1 (x * x)) \<le> arctan (Ifloat x)"
+ unfolding get_even_def if_P[OF True] using arctan_0_1_bounds'[OF `0 \<le> Ifloat x` `Ifloat x \<le> 1` `even n`] by auto
+ ultimately show ?thesis by auto
+next
+ case False hence "0 < n" by (rule odd_pos)
+ from gr0_implies_Suc[OF this] obtain n' where "n = Suc n'" ..
+ from False[unfolded this even_nat_Suc]
+ have "even n'" and "even (Suc (Suc n'))" by auto
+ have "get_odd n = Suc n'" unfolding get_odd_def if_P[OF False] using `n = Suc n'` .
+
+ have "arctan (Ifloat x) \<le> Ifloat (x * ub_arctan_horner prec (get_odd n) 1 (x * x))"
+ unfolding `get_odd n = Suc n'` using arctan_0_1_bounds'[OF `0 \<le> Ifloat x` `Ifloat x \<le> 1` `even n'`] by auto
+ moreover
+ have "Ifloat (x * lb_arctan_horner prec (get_even n) 1 (x * x)) \<le> arctan (Ifloat x)"
+ unfolding get_even_def if_not_P[OF False] unfolding `n = Suc n'` using arctan_0_1_bounds'[OF `0 \<le> Ifloat x` `Ifloat x \<le> 1` `even (Suc (Suc n'))`] by auto
+ ultimately show ?thesis by auto
+qed
+
+subsection "Compute \<pi>"
+
+definition ub_pi :: "nat \<Rightarrow> float" where
+ "ub_pi prec = (let A = rapprox_rat prec 1 5 ;
+ B = lapprox_rat prec 1 239
+ in ((Float 1 2) * ((Float 1 2) * A * (ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (A * A)) -
+ B * (lb_arctan_horner prec (get_even (prec div 14 + 1)) 1 (B * B)))))"
+
+definition lb_pi :: "nat \<Rightarrow> float" where
+ "lb_pi prec = (let A = lapprox_rat prec 1 5 ;
+ B = rapprox_rat prec 1 239
+ in ((Float 1 2) * ((Float 1 2) * A * (lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (A * A)) -
+ B * (ub_arctan_horner prec (get_odd (prec div 14 + 1)) 1 (B * B)))))"
+
+lemma pi_boundaries: "pi \<in> {Ifloat (lb_pi n) .. Ifloat (ub_pi n)}"
+proof -
+ have machin_pi: "pi = 4 * (4 * arctan (1 / 5) - arctan (1 / 239))" unfolding machin[symmetric] by auto
+
+ { fix prec n :: nat fix k :: int assume "1 < k" hence "0 \<le> k" and "0 < k" and "1 \<le> k" by auto
+ let ?k = "rapprox_rat prec 1 k"
+ have "1 div k = 0" using div_pos_pos_trivial[OF _ `1 < k`] by auto
+
+ have "0 \<le> Ifloat ?k" by (rule order_trans[OF _ rapprox_rat], auto simp add: `0 \<le> k`)
+ have "Ifloat ?k \<le> 1" unfolding rapprox_rat.simps(2)[OF zero_le_one `0 < k`]
+ by (rule rapprox_posrat_le1, auto simp add: `0 < k` `1 \<le> k`)
+
+ have "1 / real k \<le> Ifloat ?k" using rapprox_rat[where x=1 and y=k] by auto
+ hence "arctan (1 / real k) \<le> arctan (Ifloat ?k)" by (rule arctan_monotone')
+ also have "\<dots> \<le> Ifloat (?k * ub_arctan_horner prec (get_odd n) 1 (?k * ?k))"
+ using arctan_0_1_bounds[OF `0 \<le> Ifloat ?k` `Ifloat ?k \<le> 1`] by auto
+ finally have "arctan (1 / (real k)) \<le> Ifloat (?k * ub_arctan_horner prec (get_odd n) 1 (?k * ?k))" .
+ } note ub_arctan = this
+
+ { fix prec n :: nat fix k :: int assume "1 < k" hence "0 \<le> k" and "0 < k" by auto
+ let ?k = "lapprox_rat prec 1 k"
+ have "1 div k = 0" using div_pos_pos_trivial[OF _ `1 < k`] by auto
+ have "1 / real k \<le> 1" using `1 < k` by auto
+
+ have "\<And>n. 0 \<le> Ifloat ?k" using lapprox_rat_bottom[where x=1 and y=k, OF zero_le_one `0 < k`] by (auto simp add: `1 div k = 0`)
+ have "\<And>n. Ifloat ?k \<le> 1" using lapprox_rat by (rule order_trans, auto simp add: `1 / real k \<le> 1`)
+
+ have "Ifloat ?k \<le> 1 / real k" using lapprox_rat[where x=1 and y=k] by auto
+
+ have "Ifloat (?k * lb_arctan_horner prec (get_even n) 1 (?k * ?k)) \<le> arctan (Ifloat ?k)"
+ using arctan_0_1_bounds[OF `0 \<le> Ifloat ?k` `Ifloat ?k \<le> 1`] by auto
+ also have "\<dots> \<le> arctan (1 / real k)" using `Ifloat ?k \<le> 1 / real k` by (rule arctan_monotone')
+ finally have "Ifloat (?k * lb_arctan_horner prec (get_even n) 1 (?k * ?k)) \<le> arctan (1 / (real k))" .
+ } note lb_arctan = this
+
+ have "pi \<le> Ifloat (ub_pi n)"
+ unfolding ub_pi_def machin_pi Let_def Ifloat_mult Ifloat_sub unfolding Float_num
+ using lb_arctan[of 239] ub_arctan[of 5]
+ by (auto intro!: mult_left_mono add_mono simp add: diff_minus simp del: lapprox_rat.simps rapprox_rat.simps)
+ moreover
+ have "Ifloat (lb_pi n) \<le> pi"
+ unfolding lb_pi_def machin_pi Let_def Ifloat_mult Ifloat_sub Float_num
+ using lb_arctan[of 5] ub_arctan[of 239]
+ by (auto intro!: mult_left_mono add_mono simp add: diff_minus simp del: lapprox_rat.simps rapprox_rat.simps)
+ ultimately show ?thesis by auto
+qed
+
+subsection "Compute arcus tangens in the entire domain"
+
+function lb_arctan :: "nat \<Rightarrow> float \<Rightarrow> float" and ub_arctan :: "nat \<Rightarrow> float \<Rightarrow> float" where
+ "lb_arctan prec x = (let ub_horner = \<lambda> x. x * ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (x * x) ;
+ lb_horner = \<lambda> x. x * lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (x * x)
+ in (if x < 0 then - ub_arctan prec (-x) else
+ if x \<le> Float 1 -1 then lb_horner x else
+ if x \<le> Float 1 1 then Float 1 1 * lb_horner (float_divl prec x (1 + the (ub_sqrt prec (1 + x * x))))
+ else (let inv = float_divr prec 1 x
+ in if inv > 1 then 0
+ else lb_pi prec * Float 1 -1 - ub_horner inv)))"
+
+| "ub_arctan prec x = (let lb_horner = \<lambda> x. x * lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (x * x) ;
+ ub_horner = \<lambda> x. x * ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (x * x)
+ in (if x < 0 then - lb_arctan prec (-x) else
+ if x \<le> Float 1 -1 then ub_horner x else
+ if x \<le> Float 1 1 then let y = float_divr prec x (1 + the (lb_sqrt prec (1 + x * x)))
+ in if y > 1 then ub_pi prec * Float 1 -1
+ else Float 1 1 * ub_horner y
+ else ub_pi prec * Float 1 -1 - lb_horner (float_divl prec 1 x)))"
+by pat_completeness auto
+termination by (relation "measure (\<lambda> v. let (prec, x) = sum_case id id v in (if x < 0 then 1 else 0))", auto simp add: less_float_def)
+
+declare ub_arctan_horner.simps[simp del]
+declare lb_arctan_horner.simps[simp del]
+
+lemma lb_arctan_bound': assumes "0 \<le> Ifloat x"
+ shows "Ifloat (lb_arctan prec x) \<le> arctan (Ifloat x)"
+proof -
+ have "\<not> x < 0" and "0 \<le> x" unfolding less_float_def le_float_def using `0 \<le> Ifloat x` by auto
+ let "?ub_horner x" = "x * ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (x * x)"
+ and "?lb_horner x" = "x * lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (x * x)"
+
+ show ?thesis
+ proof (cases "x \<le> Float 1 -1")
+ case True hence "Ifloat x \<le> 1" unfolding le_float_def Float_num by auto
+ show ?thesis unfolding lb_arctan.simps Let_def if_not_P[OF `\<not> x < 0`] if_P[OF True]
+ using arctan_0_1_bounds[OF `0 \<le> Ifloat x` `Ifloat x \<le> 1`] by auto
+ next
+ case False hence "0 < Ifloat x" unfolding le_float_def Float_num by auto
+ let ?R = "1 + sqrt (1 + Ifloat x * Ifloat x)"
+ let ?fR = "1 + the (ub_sqrt prec (1 + x * x))"
+ let ?DIV = "float_divl prec x ?fR"
+
+ have sqr_ge0: "0 \<le> 1 + Ifloat x * Ifloat x" using sum_power2_ge_zero[of 1 "Ifloat x", unfolded numeral_2_eq_2] by auto
+ hence divisor_gt0: "0 < ?R" by (auto intro: add_pos_nonneg)
+
+ have "sqrt (Ifloat (1 + x * x)) \<le> Ifloat (the (ub_sqrt prec (1 + x * x)))" by (rule ub_sqrt_lower_bound, auto simp add: sqr_ge0)
+ hence "?R \<le> Ifloat ?fR" by auto
+ hence "0 < ?fR" and "0 < Ifloat ?fR" unfolding less_float_def using `0 < ?R` by auto
+
+ have monotone: "Ifloat (float_divl prec x ?fR) \<le> Ifloat x / ?R"
+ proof -
+ have "Ifloat ?DIV \<le> Ifloat x / Ifloat ?fR" by (rule float_divl)
+ also have "\<dots> \<le> Ifloat x / ?R" by (rule divide_left_mono[OF `?R \<le> Ifloat ?fR` `0 \<le> Ifloat x` mult_pos_pos[OF order_less_le_trans[OF divisor_gt0 `?R \<le> Ifloat ?fR`] divisor_gt0]])
+ finally show ?thesis .
+ qed
+
+ show ?thesis
+ proof (cases "x \<le> Float 1 1")
+ case True
+
+ have "Ifloat x \<le> sqrt (Ifloat (1 + x * x))" using real_sqrt_sum_squares_ge2[where x=1, unfolded numeral_2_eq_2] by auto
+ also have "\<dots> \<le> Ifloat (the (ub_sqrt prec (1 + x * x)))" by (rule ub_sqrt_lower_bound, auto simp add: sqr_ge0)
+ finally have "Ifloat x \<le> Ifloat ?fR" by auto
+ moreover have "Ifloat ?DIV \<le> Ifloat x / Ifloat ?fR" by (rule float_divl)
+ ultimately have "Ifloat ?DIV \<le> 1" unfolding divide_le_eq_1_pos[OF `0 < Ifloat ?fR`, symmetric] by auto
+
+ have "0 \<le> Ifloat ?DIV" using float_divl_lower_bound[OF `0 \<le> x` `0 < ?fR`] unfolding le_float_def by auto
+
+ have "Ifloat (Float 1 1 * ?lb_horner ?DIV) \<le> 2 * arctan (Ifloat (float_divl prec x ?fR))" unfolding Ifloat_mult[of "Float 1 1"] Float_num
+ using arctan_0_1_bounds[OF `0 \<le> Ifloat ?DIV` `Ifloat ?DIV \<le> 1`] by auto
+ also have "\<dots> \<le> 2 * arctan (Ifloat x / ?R)"
+ using arctan_monotone'[OF monotone] by (auto intro!: mult_left_mono)
+ also have "2 * arctan (Ifloat x / ?R) = arctan (Ifloat x)" using arctan_half[symmetric] unfolding numeral_2_eq_2 power_Suc2 realpow_0 real_mult_1 .
+ finally show ?thesis unfolding lb_arctan.simps Let_def if_not_P[OF `\<not> x < 0`] if_not_P[OF `\<not> x \<le> Float 1 -1`] if_P[OF True] .
+ next
+ case False
+ hence "2 < Ifloat x" unfolding le_float_def Float_num by auto
+ hence "1 \<le> Ifloat x" by auto
+
+ let "?invx" = "float_divr prec 1 x"
+ have "0 \<le> arctan (Ifloat x)" using arctan_monotone'[OF `0 \<le> Ifloat x`] using arctan_tan[of 0, unfolded tan_zero] by auto
+
+ show ?thesis
+ proof (cases "1 < ?invx")
+ case True
+ show ?thesis unfolding lb_arctan.simps Let_def if_not_P[OF `\<not> x < 0`] if_not_P[OF `\<not> x \<le> Float 1 -1`] if_not_P[OF False] if_P[OF True]
+ using `0 \<le> arctan (Ifloat x)` by auto
+ next
+ case False
+ hence "Ifloat ?invx \<le> 1" unfolding less_float_def by auto
+ have "0 \<le> Ifloat ?invx" by (rule order_trans[OF _ float_divr], auto simp add: `0 \<le> Ifloat x`)
+
+ have "1 / Ifloat x \<noteq> 0" and "0 < 1 / Ifloat x" using `0 < Ifloat x` by auto
+
+ have "arctan (1 / Ifloat x) \<le> arctan (Ifloat ?invx)" unfolding Ifloat_1[symmetric] by (rule arctan_monotone', rule float_divr)
+ also have "\<dots> \<le> Ifloat (?ub_horner ?invx)" using arctan_0_1_bounds[OF `0 \<le> Ifloat ?invx` `Ifloat ?invx \<le> 1`] by auto
+ finally have "pi / 2 - Ifloat (?ub_horner ?invx) \<le> arctan (Ifloat x)"
+ using `0 \<le> arctan (Ifloat x)` arctan_inverse[OF `1 / Ifloat x \<noteq> 0`]
+ unfolding real_sgn_pos[OF `0 < 1 / Ifloat x`] le_diff_eq by auto
+ moreover
+ have "Ifloat (lb_pi prec * Float 1 -1) \<le> pi / 2" unfolding Ifloat_mult Float_num times_divide_eq_right real_mult_1 using pi_boundaries by auto
+ ultimately
+ show ?thesis unfolding lb_arctan.simps Let_def if_not_P[OF `\<not> x < 0`] if_not_P[OF `\<not> x \<le> Float 1 -1`] if_not_P[OF `\<not> x \<le> Float 1 1`] if_not_P[OF False]
+ by auto
+ qed
+ qed
+ qed
+qed
+
+lemma ub_arctan_bound': assumes "0 \<le> Ifloat x"
+ shows "arctan (Ifloat x) \<le> Ifloat (ub_arctan prec x)"
+proof -
+ have "\<not> x < 0" and "0 \<le> x" unfolding less_float_def le_float_def using `0 \<le> Ifloat x` by auto
+
+ let "?ub_horner x" = "x * ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (x * x)"
+ and "?lb_horner x" = "x * lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (x * x)"
+
+ show ?thesis
+ proof (cases "x \<le> Float 1 -1")
+ case True hence "Ifloat x \<le> 1" unfolding le_float_def Float_num by auto
+ show ?thesis unfolding ub_arctan.simps Let_def if_not_P[OF `\<not> x < 0`] if_P[OF True]
+ using arctan_0_1_bounds[OF `0 \<le> Ifloat x` `Ifloat x \<le> 1`] by auto
+ next
+ case False hence "0 < Ifloat x" unfolding le_float_def Float_num by auto
+ let ?R = "1 + sqrt (1 + Ifloat x * Ifloat x)"
+ let ?fR = "1 + the (lb_sqrt prec (1 + x * x))"
+ let ?DIV = "float_divr prec x ?fR"
+
+ have sqr_ge0: "0 \<le> 1 + Ifloat x * Ifloat x" using sum_power2_ge_zero[of 1 "Ifloat x", unfolded numeral_2_eq_2] by auto
+ hence "0 \<le> Ifloat (1 + x*x)" by auto
+
+ hence divisor_gt0: "0 < ?R" by (auto intro: add_pos_nonneg)
+
+ have "Ifloat (the (lb_sqrt prec (1 + x * x))) \<le> sqrt (Ifloat (1 + x * x))" by (rule lb_sqrt_upper_bound, auto simp add: sqr_ge0)
+ hence "Ifloat ?fR \<le> ?R" by auto
+ have "0 < Ifloat ?fR" unfolding Ifloat_add Ifloat_1 by (rule order_less_le_trans[OF zero_less_one], auto simp add: lb_sqrt_lower_bound[OF `0 \<le> Ifloat (1 + x*x)`])
+
+ have monotone: "Ifloat x / ?R \<le> Ifloat (float_divr prec x ?fR)"
+ proof -
+ from divide_left_mono[OF `Ifloat ?fR \<le> ?R` `0 \<le> Ifloat x` mult_pos_pos[OF divisor_gt0 `0 < Ifloat ?fR`]]
+ have "Ifloat x / ?R \<le> Ifloat x / Ifloat ?fR" .
+ also have "\<dots> \<le> Ifloat ?DIV" by (rule float_divr)
+ finally show ?thesis .
+ qed
+
+ show ?thesis
+ proof (cases "x \<le> Float 1 1")
+ case True
+ show ?thesis
+ proof (cases "?DIV > 1")
+ case True
+ have "pi / 2 \<le> Ifloat (ub_pi prec * Float 1 -1)" unfolding Ifloat_mult Float_num times_divide_eq_right real_mult_1 using pi_boundaries by auto
+ from order_less_le_trans[OF arctan_ubound this, THEN less_imp_le]
+ show ?thesis unfolding ub_arctan.simps Let_def if_not_P[OF `\<not> x < 0`] if_not_P[OF `\<not> x \<le> Float 1 -1`] if_P[OF `x \<le> Float 1 1`] if_P[OF True] .
+ next
+ case False
+ hence "Ifloat ?DIV \<le> 1" unfolding less_float_def by auto
+
+ have "0 \<le> Ifloat x / ?R" using `0 \<le> Ifloat x` `0 < ?R` unfolding real_0_le_divide_iff by auto
+ hence "0 \<le> Ifloat ?DIV" using monotone by (rule order_trans)
+
+ have "arctan (Ifloat x) = 2 * arctan (Ifloat x / ?R)" using arctan_half unfolding numeral_2_eq_2 power_Suc2 realpow_0 real_mult_1 .
+ also have "\<dots> \<le> 2 * arctan (Ifloat ?DIV)"
+ using arctan_monotone'[OF monotone] by (auto intro!: mult_left_mono)
+ also have "\<dots> \<le> Ifloat (Float 1 1 * ?ub_horner ?DIV)" unfolding Ifloat_mult[of "Float 1 1"] Float_num
+ using arctan_0_1_bounds[OF `0 \<le> Ifloat ?DIV` `Ifloat ?DIV \<le> 1`] by auto
+ finally show ?thesis unfolding ub_arctan.simps Let_def if_not_P[OF `\<not> x < 0`] if_not_P[OF `\<not> x \<le> Float 1 -1`] if_P[OF `x \<le> Float 1 1`] if_not_P[OF False] .
+ qed
+ next
+ case False
+ hence "2 < Ifloat x" unfolding le_float_def Float_num by auto
+ hence "1 \<le> Ifloat x" by auto
+ hence "0 < Ifloat x" by auto
+ hence "0 < x" unfolding less_float_def by auto
+
+ let "?invx" = "float_divl prec 1 x"
+ have "0 \<le> arctan (Ifloat x)" using arctan_monotone'[OF `0 \<le> Ifloat x`] using arctan_tan[of 0, unfolded tan_zero] by auto
+
+ have "Ifloat ?invx \<le> 1" unfolding less_float_def by (rule order_trans[OF float_divl], auto simp add: `1 \<le> Ifloat x` divide_le_eq_1_pos[OF `0 < Ifloat x`])
+ have "0 \<le> Ifloat ?invx" unfolding Ifloat_0[symmetric] by (rule float_divl_lower_bound[unfolded le_float_def], auto simp add: `0 < x`)
+
+ have "1 / Ifloat x \<noteq> 0" and "0 < 1 / Ifloat x" using `0 < Ifloat x` by auto
+
+ have "Ifloat (?lb_horner ?invx) \<le> arctan (Ifloat ?invx)" using arctan_0_1_bounds[OF `0 \<le> Ifloat ?invx` `Ifloat ?invx \<le> 1`] by auto
+ also have "\<dots> \<le> arctan (1 / Ifloat x)" unfolding Ifloat_1[symmetric] by (rule arctan_monotone', rule float_divl)
+ finally have "arctan (Ifloat x) \<le> pi / 2 - Ifloat (?lb_horner ?invx)"
+ using `0 \<le> arctan (Ifloat x)` arctan_inverse[OF `1 / Ifloat x \<noteq> 0`]
+ unfolding real_sgn_pos[OF `0 < 1 / Ifloat x`] le_diff_eq by auto
+ moreover
+ have "pi / 2 \<le> Ifloat (ub_pi prec * Float 1 -1)" unfolding Ifloat_mult Float_num times_divide_eq_right mult_1_right using pi_boundaries by auto
+ ultimately
+ show ?thesis unfolding ub_arctan.simps Let_def if_not_P[OF `\<not> x < 0`] if_not_P[OF `\<not> x \<le> Float 1 -1`] if_not_P[OF `\<not> x \<le> Float 1 1`] if_not_P[OF False]
+ by auto
+ qed
+ qed
+qed
+
+lemma arctan_boundaries:
+ "arctan (Ifloat x) \<in> {Ifloat (lb_arctan prec x) .. Ifloat (ub_arctan prec x)}"
+proof (cases "0 \<le> x")
+ case True hence "0 \<le> Ifloat x" unfolding le_float_def by auto
+ show ?thesis using ub_arctan_bound'[OF `0 \<le> Ifloat x`] lb_arctan_bound'[OF `0 \<le> Ifloat x`] unfolding atLeastAtMost_iff by auto
+next
+ let ?mx = "-x"
+ case False hence "x < 0" and "0 \<le> Ifloat ?mx" unfolding le_float_def less_float_def by auto
+ hence bounds: "Ifloat (lb_arctan prec ?mx) \<le> arctan (Ifloat ?mx) \<and> arctan (Ifloat ?mx) \<le> Ifloat (ub_arctan prec ?mx)"
+ using ub_arctan_bound'[OF `0 \<le> Ifloat ?mx`] lb_arctan_bound'[OF `0 \<le> Ifloat ?mx`] by auto
+ show ?thesis unfolding Ifloat_minus arctan_minus lb_arctan.simps[where x=x] ub_arctan.simps[where x=x] Let_def if_P[OF `x < 0`]
+ unfolding atLeastAtMost_iff using bounds[unfolded Ifloat_minus arctan_minus] by auto
+qed
+
+lemma bnds_arctan: "\<forall> x lx ux. (l, u) = (lb_arctan prec lx, ub_arctan prec ux) \<and> x \<in> {Ifloat lx .. Ifloat ux} \<longrightarrow> Ifloat l \<le> arctan x \<and> arctan x \<le> Ifloat u"
+proof (rule allI, rule allI, rule allI, rule impI)
+ fix x lx ux
+ assume "(l, u) = (lb_arctan prec lx, ub_arctan prec ux) \<and> x \<in> {Ifloat lx .. Ifloat ux}"
+ hence l: "lb_arctan prec lx = l " and u: "ub_arctan prec ux = u" and x: "x \<in> {Ifloat lx .. Ifloat ux}" by auto
+
+ { from arctan_boundaries[of lx prec, unfolded l]
+ have "Ifloat l \<le> arctan (Ifloat lx)" by (auto simp del: lb_arctan.simps)
+ also have "\<dots> \<le> arctan x" using x by (auto intro: arctan_monotone')
+ finally have "Ifloat l \<le> arctan x" .
+ } moreover
+ { have "arctan x \<le> arctan (Ifloat ux)" using x by (auto intro: arctan_monotone')
+ also have "\<dots> \<le> Ifloat u" using arctan_boundaries[of ux prec, unfolded u] by (auto simp del: ub_arctan.simps)
+ finally have "arctan x \<le> Ifloat u" .
+ } ultimately show "Ifloat l \<le> arctan x \<and> arctan x \<le> Ifloat u" ..
+qed
+
+section "Sinus and Cosinus"
+
+subsection "Compute the cosinus and sinus series"
+
+fun ub_sin_cos_aux :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float"
+and lb_sin_cos_aux :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float" where
+ "ub_sin_cos_aux prec 0 i k x = 0"
+| "ub_sin_cos_aux prec (Suc n) i k x =
+ (rapprox_rat prec 1 (int k)) - x * (lb_sin_cos_aux prec n (i + 2) (k * i * (i + 1)) x)"
+| "lb_sin_cos_aux prec 0 i k x = 0"
+| "lb_sin_cos_aux prec (Suc n) i k x =
+ (lapprox_rat prec 1 (int k)) - x * (ub_sin_cos_aux prec n (i + 2) (k * i * (i + 1)) x)"
+
+lemma cos_aux:
+ shows "Ifloat (lb_sin_cos_aux prec n 1 1 (x * x)) \<le> (\<Sum> i=0..<n. -1^i * (1/real (fact (2 * i))) * (Ifloat x)^(2 * i))" (is "?lb")
+ and "(\<Sum> i=0..<n. -1^i * (1/real (fact (2 * i))) * (Ifloat x)^(2 * i)) \<le> Ifloat (ub_sin_cos_aux prec n 1 1 (x * x))" (is "?ub")
+proof -
+ have "0 \<le> Ifloat (x * x)" unfolding Ifloat_mult by auto
+ let "?f n" = "fact (2 * n)"
+
+ { fix n
+ have F: "\<And>m. ((\<lambda>i. i + 2) ^ n) m = m + 2 * n" by (induct n arbitrary: m, auto)
+ have "?f (Suc n) = ?f n * ((\<lambda>i. i + 2) ^ n) 1 * (((\<lambda>i. i + 2) ^ n) 1 + 1)"
+ unfolding F by auto } note f_eq = this
+
+ from horner_bounds[where lb="lb_sin_cos_aux prec" and ub="ub_sin_cos_aux prec" and j'=0,
+ OF `0 \<le> Ifloat (x * x)` f_eq lb_sin_cos_aux.simps ub_sin_cos_aux.simps]
+ show "?lb" and "?ub" by (auto simp add: power_mult power2_eq_square[of "Ifloat x"])
+qed
+
+lemma cos_boundaries: assumes "0 \<le> Ifloat x" and "Ifloat x \<le> pi / 2"
+ shows "cos (Ifloat x) \<in> {Ifloat (lb_sin_cos_aux prec (get_even n) 1 1 (x * x)) .. Ifloat (ub_sin_cos_aux prec (get_odd n) 1 1 (x * x))}"
+proof (cases "Ifloat x = 0")
+ case False hence "Ifloat x \<noteq> 0" by auto
+ hence "0 < x" and "0 < Ifloat x" using `0 \<le> Ifloat x` unfolding less_float_def by auto
+ have "0 < x * x" using `0 < x` unfolding less_float_def Ifloat_mult Ifloat_0
+ using mult_pos_pos[where a="Ifloat x" and b="Ifloat x"] by auto
+
+ { fix x n have "(\<Sum> i=0..<n. -1^i * (1/real (fact (2 * i))) * x^(2 * i))
+ = (\<Sum> i = 0 ..< 2 * n. (if even(i) then (-1 ^ (i div 2))/(real (fact i)) else 0) * x ^ i)" (is "?sum = ?ifsum")
+ proof -
+ have "?sum = ?sum + (\<Sum> j = 0 ..< n. 0)" by auto
+ also have "\<dots> =
+ (\<Sum> j = 0 ..< n. -1 ^ ((2 * j) div 2) / (real (fact (2 * j))) * x ^(2 * j)) + (\<Sum> j = 0 ..< n. 0)" by auto
+ also have "\<dots> = (\<Sum> i = 0 ..< 2 * n. if even i then -1 ^ (i div 2) / (real (fact i)) * x ^ i else 0)"
+ unfolding sum_split_even_odd ..
+ also have "\<dots> = (\<Sum> i = 0 ..< 2 * n. (if even i then -1 ^ (i div 2) / (real (fact i)) else 0) * x ^ i)"
+ by (rule setsum_cong2) auto
+ finally show ?thesis by assumption
+ qed } note morph_to_if_power = this
+
+
+ { fix n :: nat assume "0 < n"
+ hence "0 < 2 * n" by auto
+ obtain t where "0 < t" and "t < Ifloat x" and
+ cos_eq: "cos (Ifloat x) = (\<Sum> i = 0 ..< 2 * n. (if even(i) then (-1 ^ (i div 2))/(real (fact i)) else 0) * (Ifloat x) ^ i)
+ + (cos (t + 1/2 * real (2 * n) * pi) / real (fact (2*n))) * (Ifloat x)^(2*n)"
+ (is "_ = ?SUM + ?rest / ?fact * ?pow")
+ using Maclaurin_cos_expansion2[OF `0 < Ifloat x` `0 < 2 * n`] by auto
+
+ have "cos t * -1^n = cos t * cos (real n * pi) + sin t * sin (real n * pi)" by auto
+ also have "\<dots> = cos (t + real n * pi)" using cos_add by auto
+ also have "\<dots> = ?rest" by auto
+ finally have "cos t * -1^n = ?rest" .
+ moreover
+ have "t \<le> pi / 2" using `t < Ifloat x` and `Ifloat x \<le> pi / 2` by auto
+ hence "0 \<le> cos t" using `0 < t` and cos_ge_zero by auto
+ ultimately have even: "even n \<Longrightarrow> 0 \<le> ?rest" and odd: "odd n \<Longrightarrow> 0 \<le> - ?rest " by auto
+
+ have "0 < ?fact" by auto
+ have "0 < ?pow" using `0 < Ifloat x` by auto
+
+ {
+ assume "even n"
+ have "Ifloat (lb_sin_cos_aux prec n 1 1 (x * x)) \<le> ?SUM"
+ unfolding morph_to_if_power[symmetric] using cos_aux by auto
+ also have "\<dots> \<le> cos (Ifloat x)"
+ proof -
+ from even[OF `even n`] `0 < ?fact` `0 < ?pow`
+ have "0 \<le> (?rest / ?fact) * ?pow" by (metis mult_nonneg_nonneg divide_nonneg_pos less_imp_le)
+ thus ?thesis unfolding cos_eq by auto
+ qed
+ finally have "Ifloat (lb_sin_cos_aux prec n 1 1 (x * x)) \<le> cos (Ifloat x)" .
+ } note lb = this
+
+ {
+ assume "odd n"
+ have "cos (Ifloat x) \<le> ?SUM"
+ proof -
+ from `0 < ?fact` and `0 < ?pow` and odd[OF `odd n`]
+ have "0 \<le> (- ?rest) / ?fact * ?pow"
+ by (metis mult_nonneg_nonneg divide_nonneg_pos less_imp_le)
+ thus ?thesis unfolding cos_eq by auto
+ qed
+ also have "\<dots> \<le> Ifloat (ub_sin_cos_aux prec n 1 1 (x * x))"
+ unfolding morph_to_if_power[symmetric] using cos_aux by auto
+ finally have "cos (Ifloat x) \<le> Ifloat (ub_sin_cos_aux prec n 1 1 (x * x))" .
+ } note ub = this and lb
+ } note ub = this(1) and lb = this(2)
+
+ have "cos (Ifloat x) \<le> Ifloat (ub_sin_cos_aux prec (get_odd n) 1 1 (x * x))" using ub[OF odd_pos[OF get_odd] get_odd] .
+ moreover have "Ifloat (lb_sin_cos_aux prec (get_even n) 1 1 (x * x)) \<le> cos (Ifloat x)"
+ proof (cases "0 < get_even n")
+ case True show ?thesis using lb[OF True get_even] .
+ next
+ case False
+ hence "get_even n = 0" by auto
+ have "- (pi / 2) \<le> Ifloat x" by (rule order_trans[OF _ `0 < Ifloat x`[THEN less_imp_le]], auto)
+ with `Ifloat x \<le> pi / 2`
+ show ?thesis unfolding `get_even n = 0` lb_sin_cos_aux.simps Ifloat_minus Ifloat_0 using cos_ge_zero by auto
+ qed
+ ultimately show ?thesis by auto
+next
+ case True
+ show ?thesis
+ proof (cases "n = 0")
+ case True
+ thus ?thesis unfolding `n = 0` get_even_def get_odd_def using `Ifloat x = 0` lapprox_rat[where x="-1" and y=1] by auto
+ next
+ case False with not0_implies_Suc obtain m where "n = Suc m" by blast
+ thus ?thesis unfolding `n = Suc m` get_even_def get_odd_def using `Ifloat x = 0` rapprox_rat[where x=1 and y=1] lapprox_rat[where x=1 and y=1] by (cases "even (Suc m)", auto)
+ qed
+qed
+
+lemma sin_aux: assumes "0 \<le> Ifloat x"
+ shows "Ifloat (x * lb_sin_cos_aux prec n 2 1 (x * x)) \<le> (\<Sum> i=0..<n. -1^i * (1/real (fact (2 * i + 1))) * (Ifloat x)^(2 * i + 1))" (is "?lb")
+ and "(\<Sum> i=0..<n. -1^i * (1/real (fact (2 * i + 1))) * (Ifloat x)^(2 * i + 1)) \<le> Ifloat (x * ub_sin_cos_aux prec n 2 1 (x * x))" (is "?ub")
+proof -
+ have "0 \<le> Ifloat (x * x)" unfolding Ifloat_mult by auto
+ let "?f n" = "fact (2 * n + 1)"
+
+ { fix n
+ have F: "\<And>m. ((\<lambda>i. i + 2) ^ n) m = m + 2 * n" by (induct n arbitrary: m, auto)
+ have "?f (Suc n) = ?f n * ((\<lambda>i. i + 2) ^ n) 2 * (((\<lambda>i. i + 2) ^ n) 2 + 1)"
+ unfolding F by auto } note f_eq = this
+
+ from horner_bounds[where lb="lb_sin_cos_aux prec" and ub="ub_sin_cos_aux prec" and j'=0,
+ OF `0 \<le> Ifloat (x * x)` f_eq lb_sin_cos_aux.simps ub_sin_cos_aux.simps]
+ show "?lb" and "?ub" using `0 \<le> Ifloat x` unfolding Ifloat_mult
+ unfolding power_add power_one_right real_mult_assoc[symmetric] setsum_left_distrib[symmetric]
+ unfolding real_mult_commute
+ by (auto intro!: mult_left_mono simp add: power_mult power2_eq_square[of "Ifloat x"])
+qed
+
+lemma sin_boundaries: assumes "0 \<le> Ifloat x" and "Ifloat x \<le> pi / 2"
+ shows "sin (Ifloat x) \<in> {Ifloat (x * lb_sin_cos_aux prec (get_even n) 2 1 (x * x)) .. Ifloat (x * ub_sin_cos_aux prec (get_odd n) 2 1 (x * x))}"
+proof (cases "Ifloat x = 0")
+ case False hence "Ifloat x \<noteq> 0" by auto
+ hence "0 < x" and "0 < Ifloat x" using `0 \<le> Ifloat x` unfolding less_float_def by auto
+ have "0 < x * x" using `0 < x` unfolding less_float_def Ifloat_mult Ifloat_0
+ using mult_pos_pos[where a="Ifloat x" and b="Ifloat x"] by auto
+
+ { fix x n have "(\<Sum> j = 0 ..< n. -1 ^ (((2 * j + 1) - Suc 0) div 2) / (real (fact (2 * j + 1))) * x ^(2 * j + 1))
+ = (\<Sum> i = 0 ..< 2 * n. (if even(i) then 0 else (-1 ^ ((i - Suc 0) div 2))/(real (fact i))) * x ^ i)" (is "?SUM = _")
+ proof -
+ have pow: "!!i. x ^ (2 * i + 1) = x * x ^ (2 * i)" by auto
+ have "?SUM = (\<Sum> j = 0 ..< n. 0) + ?SUM" by auto
+ also have "\<dots> = (\<Sum> i = 0 ..< 2 * n. if even i then 0 else -1 ^ ((i - Suc 0) div 2) / (real (fact i)) * x ^ i)"
+ unfolding sum_split_even_odd ..
+ also have "\<dots> = (\<Sum> i = 0 ..< 2 * n. (if even i then 0 else -1 ^ ((i - Suc 0) div 2) / (real (fact i))) * x ^ i)"
+ by (rule setsum_cong2) auto
+ finally show ?thesis by assumption
+ qed } note setsum_morph = this
+
+ { fix n :: nat assume "0 < n"
+ hence "0 < 2 * n + 1" by auto
+ obtain t where "0 < t" and "t < Ifloat x" and
+ sin_eq: "sin (Ifloat x) = (\<Sum> i = 0 ..< 2 * n + 1. (if even(i) then 0 else (-1 ^ ((i - Suc 0) div 2))/(real (fact i))) * (Ifloat x) ^ i)
+ + (sin (t + 1/2 * real (2 * n + 1) * pi) / real (fact (2*n + 1))) * (Ifloat x)^(2*n + 1)"
+ (is "_ = ?SUM + ?rest / ?fact * ?pow")
+ using Maclaurin_sin_expansion3[OF `0 < 2 * n + 1` `0 < Ifloat x`] by auto
+
+ have "?rest = cos t * -1^n" unfolding sin_add cos_add real_of_nat_add left_distrib right_distrib by auto
+ moreover
+ have "t \<le> pi / 2" using `t < Ifloat x` and `Ifloat x \<le> pi / 2` by auto
+ hence "0 \<le> cos t" using `0 < t` and cos_ge_zero by auto
+ ultimately have even: "even n \<Longrightarrow> 0 \<le> ?rest" and odd: "odd n \<Longrightarrow> 0 \<le> - ?rest " by auto
+
+ have "0 < ?fact" by (rule real_of_nat_fact_gt_zero)
+ have "0 < ?pow" using `0 < Ifloat x` by (rule zero_less_power)
+
+ {
+ assume "even n"
+ have "Ifloat (x * lb_sin_cos_aux prec n 2 1 (x * x)) \<le>
+ (\<Sum> i = 0 ..< 2 * n. (if even(i) then 0 else (-1 ^ ((i - Suc 0) div 2))/(real (fact i))) * (Ifloat x) ^ i)"
+ using sin_aux[OF `0 \<le> Ifloat x`] unfolding setsum_morph[symmetric] by auto
+ also have "\<dots> \<le> ?SUM" by auto
+ also have "\<dots> \<le> sin (Ifloat x)"
+ proof -
+ from even[OF `even n`] `0 < ?fact` `0 < ?pow`
+ have "0 \<le> (?rest / ?fact) * ?pow" by (metis mult_nonneg_nonneg divide_nonneg_pos less_imp_le)
+ thus ?thesis unfolding sin_eq by auto
+ qed
+ finally have "Ifloat (x * lb_sin_cos_aux prec n 2 1 (x * x)) \<le> sin (Ifloat x)" .
+ } note lb = this
+
+ {
+ assume "odd n"
+ have "sin (Ifloat x) \<le> ?SUM"
+ proof -
+ from `0 < ?fact` and `0 < ?pow` and odd[OF `odd n`]
+ have "0 \<le> (- ?rest) / ?fact * ?pow"
+ by (metis mult_nonneg_nonneg divide_nonneg_pos less_imp_le)
+ thus ?thesis unfolding sin_eq by auto
+ qed
+ also have "\<dots> \<le> (\<Sum> i = 0 ..< 2 * n. (if even(i) then 0 else (-1 ^ ((i - Suc 0) div 2))/(real (fact i))) * (Ifloat x) ^ i)"
+ by auto
+ also have "\<dots> \<le> Ifloat (x * ub_sin_cos_aux prec n 2 1 (x * x))"
+ using sin_aux[OF `0 \<le> Ifloat x`] unfolding setsum_morph[symmetric] by auto
+ finally have "sin (Ifloat x) \<le> Ifloat (x * ub_sin_cos_aux prec n 2 1 (x * x))" .
+ } note ub = this and lb
+ } note ub = this(1) and lb = this(2)
+
+ have "sin (Ifloat x) \<le> Ifloat (x * ub_sin_cos_aux prec (get_odd n) 2 1 (x * x))" using ub[OF odd_pos[OF get_odd] get_odd] .
+ moreover have "Ifloat (x * lb_sin_cos_aux prec (get_even n) 2 1 (x * x)) \<le> sin (Ifloat x)"
+ proof (cases "0 < get_even n")
+ case True show ?thesis using lb[OF True get_even] .
+ next
+ case False
+ hence "get_even n = 0" by auto
+ with `Ifloat x \<le> pi / 2` `0 \<le> Ifloat x`
+ show ?thesis unfolding `get_even n = 0` ub_sin_cos_aux.simps Ifloat_minus Ifloat_0 using sin_ge_zero by auto
+ qed
+ ultimately show ?thesis by auto
+next
+ case True
+ show ?thesis
+ proof (cases "n = 0")
+ case True
+ thus ?thesis unfolding `n = 0` get_even_def get_odd_def using `Ifloat x = 0` lapprox_rat[where x="-1" and y=1] by auto
+ next
+ case False with not0_implies_Suc obtain m where "n = Suc m" by blast
+ thus ?thesis unfolding `n = Suc m` get_even_def get_odd_def using `Ifloat x = 0` rapprox_rat[where x=1 and y=1] lapprox_rat[where x=1 and y=1] by (cases "even (Suc m)", auto)
+ qed
+qed
+
+subsection "Compute the cosinus in the entire domain"
+
+definition lb_cos :: "nat \<Rightarrow> float \<Rightarrow> float" where
+"lb_cos prec x = (let
+ horner = \<lambda> x. lb_sin_cos_aux prec (get_even (prec div 4 + 1)) 1 1 (x * x) ;
+ half = \<lambda> x. if x < 0 then - 1 else Float 1 1 * x * x - 1
+ in if x < Float 1 -1 then horner x
+else if x < 1 then half (horner (x * Float 1 -1))
+ else half (half (horner (x * Float 1 -2))))"
+
+definition ub_cos :: "nat \<Rightarrow> float \<Rightarrow> float" where
+"ub_cos prec x = (let
+ horner = \<lambda> x. ub_sin_cos_aux prec (get_odd (prec div 4 + 1)) 1 1 (x * x) ;
+ half = \<lambda> x. Float 1 1 * x * x - 1
+ in if x < Float 1 -1 then horner x
+else if x < 1 then half (horner (x * Float 1 -1))
+ else half (half (horner (x * Float 1 -2))))"
+
+definition bnds_cos :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float * float" where
+"bnds_cos prec lx ux = (let lpi = lb_pi prec
+ in if lx < -lpi \<or> ux > lpi then (Float -1 0, Float 1 0)
+ else if ux \<le> 0 then (lb_cos prec (-lx), ub_cos prec (-ux))
+ else if 0 \<le> lx then (lb_cos prec ux, ub_cos prec lx)
+ else (min (lb_cos prec (-lx)) (lb_cos prec ux), Float 1 0))"
+
+lemma lb_cos: assumes "0 \<le> Ifloat x" and "Ifloat x \<le> pi"
+ shows "cos (Ifloat x) \<in> {Ifloat (lb_cos prec x) .. Ifloat (ub_cos prec x)}" (is "?cos x \<in> { Ifloat (?lb x) .. Ifloat (?ub x) }")
+proof -
+ { fix x :: real
+ have "cos x = cos (x / 2 + x / 2)" by auto
+ also have "\<dots> = cos (x / 2) * cos (x / 2) + sin (x / 2) * sin (x / 2) - sin (x / 2) * sin (x / 2) + cos (x / 2) * cos (x / 2) - 1"
+ unfolding cos_add by auto
+ also have "\<dots> = 2 * cos (x / 2) * cos (x / 2) - 1" by algebra
+ finally have "cos x = 2 * cos (x / 2) * cos (x / 2) - 1" .
+ } note x_half = this[symmetric]
+
+ have "\<not> x < 0" using `0 \<le> Ifloat x` unfolding less_float_def by auto
+ let "?ub_horner x" = "ub_sin_cos_aux prec (get_odd (prec div 4 + 1)) 1 1 (x * x)"
+ let "?lb_horner x" = "lb_sin_cos_aux prec (get_even (prec div 4 + 1)) 1 1 (x * x)"
+ let "?ub_half x" = "Float 1 1 * x * x - 1"
+ let "?lb_half x" = "if x < 0 then - 1 else Float 1 1 * x * x - 1"
+
+ show ?thesis
+ proof (cases "x < Float 1 -1")
+ case True hence "Ifloat x \<le> pi / 2" unfolding less_float_def using pi_ge_two by auto
+ show ?thesis unfolding lb_cos_def[where x=x] ub_cos_def[where x=x] if_not_P[OF `\<not> x < 0`] if_P[OF `x < Float 1 -1`] Let_def
+ using cos_boundaries[OF `0 \<le> Ifloat x` `Ifloat x \<le> pi / 2`] .
+ next
+ case False
+
+ { fix y x :: float let ?x2 = "Ifloat (x * Float 1 -1)"
+ assume "Ifloat y \<le> cos ?x2" and "-pi \<le> Ifloat x" and "Ifloat x \<le> pi"
+ hence "- (pi / 2) \<le> ?x2" and "?x2 \<le> pi / 2" using pi_ge_two unfolding Ifloat_mult Float_num by auto
+ hence "0 \<le> cos ?x2" by (rule cos_ge_zero)
+
+ have "Ifloat (?lb_half y) \<le> cos (Ifloat x)"
+ proof (cases "y < 0")
+ case True show ?thesis using cos_ge_minus_one unfolding if_P[OF True] by auto
+ next
+ case False
+ hence "0 \<le> Ifloat y" unfolding less_float_def by auto
+ from mult_mono[OF `Ifloat y \<le> cos ?x2` `Ifloat y \<le> cos ?x2` `0 \<le> cos ?x2` this]
+ have "Ifloat y * Ifloat y \<le> cos ?x2 * cos ?x2" .
+ hence "2 * Ifloat y * Ifloat y \<le> 2 * cos ?x2 * cos ?x2" by auto
+ hence "2 * Ifloat y * Ifloat y - 1 \<le> 2 * cos (Ifloat x / 2) * cos (Ifloat x / 2) - 1" unfolding Float_num Ifloat_mult by auto
+ thus ?thesis unfolding if_not_P[OF False] x_half Float_num Ifloat_mult Ifloat_sub by auto
+ qed
+ } note lb_half = this
+
+ { fix y x :: float let ?x2 = "Ifloat (x * Float 1 -1)"
+ assume ub: "cos ?x2 \<le> Ifloat y" and "- pi \<le> Ifloat x" and "Ifloat x \<le> pi"
+ hence "- (pi / 2) \<le> ?x2" and "?x2 \<le> pi / 2" using pi_ge_two unfolding Ifloat_mult Float_num by auto
+ hence "0 \<le> cos ?x2" by (rule cos_ge_zero)
+
+ have "cos (Ifloat x) \<le> Ifloat (?ub_half y)"
+ proof -
+ have "0 \<le> Ifloat y" using `0 \<le> cos ?x2` ub by (rule order_trans)
+ from mult_mono[OF ub ub this `0 \<le> cos ?x2`]
+ have "cos ?x2 * cos ?x2 \<le> Ifloat y * Ifloat y" .
+ hence "2 * cos ?x2 * cos ?x2 \<le> 2 * Ifloat y * Ifloat y" by auto
+ hence "2 * cos (Ifloat x / 2) * cos (Ifloat x / 2) - 1 \<le> 2 * Ifloat y * Ifloat y - 1" unfolding Float_num Ifloat_mult by auto
+ thus ?thesis unfolding x_half Ifloat_mult Float_num Ifloat_sub by auto
+ qed
+ } note ub_half = this
+
+ let ?x2 = "x * Float 1 -1"
+ let ?x4 = "x * Float 1 -1 * Float 1 -1"
+
+ have "-pi \<le> Ifloat x" using pi_ge_zero[THEN le_imp_neg_le, unfolded minus_zero] `0 \<le> Ifloat x` by (rule order_trans)
+
+ show ?thesis
+ proof (cases "x < 1")
+ case True hence "Ifloat x \<le> 1" unfolding less_float_def by auto
+ have "0 \<le> Ifloat ?x2" and "Ifloat ?x2 \<le> pi / 2" using pi_ge_two `0 \<le> Ifloat x` unfolding Ifloat_mult Float_num using assms by auto
+ from cos_boundaries[OF this]
+ have lb: "Ifloat (?lb_horner ?x2) \<le> ?cos ?x2" and ub: "?cos ?x2 \<le> Ifloat (?ub_horner ?x2)" by auto
+
+ have "Ifloat (?lb x) \<le> ?cos x"
+ proof -
+ from lb_half[OF lb `-pi \<le> Ifloat x` `Ifloat x \<le> pi`]
+ show ?thesis unfolding lb_cos_def[where x=x] Let_def using `\<not> x < 0` `\<not> x < Float 1 -1` `x < 1` by auto
+ qed
+ moreover have "?cos x \<le> Ifloat (?ub x)"
+ proof -
+ from ub_half[OF ub `-pi \<le> Ifloat x` `Ifloat x \<le> pi`]
+ show ?thesis unfolding ub_cos_def[where x=x] Let_def using `\<not> x < 0` `\<not> x < Float 1 -1` `x < 1` by auto
+ qed
+ ultimately show ?thesis by auto
+ next
+ case False
+ have "0 \<le> Ifloat ?x4" and "Ifloat ?x4 \<le> pi / 2" using pi_ge_two `0 \<le> Ifloat x` `Ifloat x \<le> pi` unfolding Ifloat_mult Float_num by auto
+ from cos_boundaries[OF this]
+ have lb: "Ifloat (?lb_horner ?x4) \<le> ?cos ?x4" and ub: "?cos ?x4 \<le> Ifloat (?ub_horner ?x4)" by auto
+
+ have eq_4: "?x2 * Float 1 -1 = x * Float 1 -2" by (cases x, auto simp add: times_float.simps)
+
+ have "Ifloat (?lb x) \<le> ?cos x"
+ proof -
+ have "-pi \<le> Ifloat ?x2" and "Ifloat ?x2 \<le> pi" unfolding Ifloat_mult Float_num using pi_ge_two `0 \<le> Ifloat x` `Ifloat x \<le> pi` by auto
+ from lb_half[OF lb_half[OF lb this] `-pi \<le> Ifloat x` `Ifloat x \<le> pi`, unfolded eq_4]
+ show ?thesis unfolding lb_cos_def[where x=x] if_not_P[OF `\<not> x < 0`] if_not_P[OF `\<not> x < Float 1 -1`] if_not_P[OF `\<not> x < 1`] Let_def .
+ qed
+ moreover have "?cos x \<le> Ifloat (?ub x)"
+ proof -
+ have "-pi \<le> Ifloat ?x2" and "Ifloat ?x2 \<le> pi" unfolding Ifloat_mult Float_num using pi_ge_two `0 \<le> Ifloat x` `Ifloat x \<le> pi` by auto
+ from ub_half[OF ub_half[OF ub this] `-pi \<le> Ifloat x` `Ifloat x \<le> pi`, unfolded eq_4]
+ show ?thesis unfolding ub_cos_def[where x=x] if_not_P[OF `\<not> x < 0`] if_not_P[OF `\<not> x < Float 1 -1`] if_not_P[OF `\<not> x < 1`] Let_def .
+ qed
+ ultimately show ?thesis by auto
+ qed
+ qed
+qed
+
+lemma lb_cos_minus: assumes "-pi \<le> Ifloat x" and "Ifloat x \<le> 0"
+ shows "cos (Ifloat (-x)) \<in> {Ifloat (lb_cos prec (-x)) .. Ifloat (ub_cos prec (-x))}"
+proof -
+ have "0 \<le> Ifloat (-x)" and "Ifloat (-x) \<le> pi" using `-pi \<le> Ifloat x` `Ifloat x \<le> 0` by auto
+ from lb_cos[OF this] show ?thesis .
+qed
+
+lemma bnds_cos: "\<forall> x lx ux. (l, u) = bnds_cos prec lx ux \<and> x \<in> {Ifloat lx .. Ifloat ux} \<longrightarrow> Ifloat l \<le> cos x \<and> cos x \<le> Ifloat u"
+proof (rule allI, rule allI, rule allI, rule impI)
+ fix x lx ux
+ assume "(l, u) = bnds_cos prec lx ux \<and> x \<in> {Ifloat lx .. Ifloat ux}"
+ hence bnds: "(l, u) = bnds_cos prec lx ux" and x: "x \<in> {Ifloat lx .. Ifloat ux}" by auto
+
+ let ?lpi = "lb_pi prec"
+ have [intro!]: "Ifloat lx \<le> Ifloat ux" using x by auto
+ hence "lx \<le> ux" unfolding le_float_def .
+
+ show "Ifloat l \<le> cos x \<and> cos x \<le> Ifloat u"
+ proof (cases "lx < -?lpi \<or> ux > ?lpi")
+ case True
+ show ?thesis using bnds unfolding bnds_cos_def if_P[OF True] Let_def using cos_le_one cos_ge_minus_one by auto
+ next
+ case False note not_out = this
+ hence lpi_lx: "- Ifloat ?lpi \<le> Ifloat lx" and lpi_ux: "Ifloat ux \<le> Ifloat ?lpi" unfolding le_float_def less_float_def by auto
+
+ from pi_boundaries[unfolded atLeastAtMost_iff, THEN conjunct1, THEN le_imp_neg_le] lpi_lx
+ have "- pi \<le> Ifloat lx" by (rule order_trans)
+ hence "- pi \<le> x" and "- pi \<le> Ifloat ux" and "x \<le> Ifloat ux" using x by auto
+
+ from lpi_ux pi_boundaries[unfolded atLeastAtMost_iff, THEN conjunct1]
+ have "Ifloat ux \<le> pi" by (rule order_trans)
+ hence "x \<le> pi" and "Ifloat lx \<le> pi" and "Ifloat lx \<le> x" using x by auto
+
+ note lb_cos_minus_bottom = lb_cos_minus[unfolded atLeastAtMost_iff, THEN conjunct1]
+ note lb_cos_minus_top = lb_cos_minus[unfolded atLeastAtMost_iff, THEN conjunct2]
+ note lb_cos_bottom = lb_cos[unfolded atLeastAtMost_iff, THEN conjunct1]
+ note lb_cos_top = lb_cos[unfolded atLeastAtMost_iff, THEN conjunct2]
+
+ show ?thesis
+ proof (cases "ux \<le> 0")
+ case True hence "Ifloat ux \<le> 0" unfolding le_float_def by auto
+ hence "x \<le> 0" and "Ifloat lx \<le> 0" using x by auto
+
+ { have "Ifloat (lb_cos prec (-lx)) \<le> cos (Ifloat (-lx))" using lb_cos_minus_bottom[OF `-pi \<le> Ifloat lx` `Ifloat lx \<le> 0`] .
+ also have "\<dots> \<le> cos x" unfolding Ifloat_minus cos_minus using cos_monotone_minus_pi_0'[OF `- pi \<le> Ifloat lx` `Ifloat lx \<le> x` `x \<le> 0`] .
+ finally have "Ifloat (lb_cos prec (-lx)) \<le> cos x" . }
+ moreover
+ { have "cos x \<le> cos (Ifloat (-ux))" unfolding Ifloat_minus cos_minus using cos_monotone_minus_pi_0'[OF `- pi \<le> x` `x \<le> Ifloat ux` `Ifloat ux \<le> 0`] .
+ also have "\<dots> \<le> Ifloat (ub_cos prec (-ux))" using lb_cos_minus_top[OF `-pi \<le> Ifloat ux` `Ifloat ux \<le> 0`] .
+ finally have "cos x \<le> Ifloat (ub_cos prec (-ux))" . }
+ ultimately show ?thesis using bnds unfolding bnds_cos_def Let_def if_not_P[OF not_out] if_P[OF True] by auto
+ next
+ case False note not_ux = this
+
+ show ?thesis
+ proof (cases "0 \<le> lx")
+ case True hence "0 \<le> Ifloat lx" unfolding le_float_def by auto
+ hence "0 \<le> x" and "0 \<le> Ifloat ux" using x by auto
+
+ { have "Ifloat (lb_cos prec ux) \<le> cos (Ifloat ux)" using lb_cos_bottom[OF `0 \<le> Ifloat ux` `Ifloat ux \<le> pi`] .
+ also have "\<dots> \<le> cos x" using cos_monotone_0_pi'[OF `0 \<le> x` `x \<le> Ifloat ux` `Ifloat ux \<le> pi`] .
+ finally have "Ifloat (lb_cos prec ux) \<le> cos x" . }
+ moreover
+ { have "cos x \<le> cos (Ifloat lx)" using cos_monotone_0_pi'[OF `0 \<le> Ifloat lx` `Ifloat lx \<le> x` `x \<le> pi`] .
+ also have "\<dots> \<le> Ifloat (ub_cos prec lx)" using lb_cos_top[OF `0 \<le> Ifloat lx` `Ifloat lx \<le> pi`] .
+ finally have "cos x \<le> Ifloat (ub_cos prec lx)" . }
+ ultimately show ?thesis using bnds unfolding bnds_cos_def Let_def if_not_P[OF not_out] if_not_P[OF not_ux] if_P[OF True] by auto
+ next
+ case False with not_ux
+ have "Ifloat lx \<le> 0" and "0 \<le> Ifloat ux" unfolding le_float_def by auto
+
+ have "Ifloat (min (lb_cos prec (-lx)) (lb_cos prec ux)) \<le> cos x"
+ proof (cases "x \<le> 0")
+ case True
+ have "Ifloat (lb_cos prec (-lx)) \<le> cos (Ifloat (-lx))" using lb_cos_minus_bottom[OF `-pi \<le> Ifloat lx` `Ifloat lx \<le> 0`] .
+ also have "\<dots> \<le> cos x" unfolding Ifloat_minus cos_minus using cos_monotone_minus_pi_0'[OF `- pi \<le> Ifloat lx` `Ifloat lx \<le> x` `x \<le> 0`] .
+ finally show ?thesis unfolding Ifloat_min by auto
+ next
+ case False hence "0 \<le> x" by auto
+ have "Ifloat (lb_cos prec ux) \<le> cos (Ifloat ux)" using lb_cos_bottom[OF `0 \<le> Ifloat ux` `Ifloat ux \<le> pi`] .
+ also have "\<dots> \<le> cos x" using cos_monotone_0_pi'[OF `0 \<le> x` `x \<le> Ifloat ux` `Ifloat ux \<le> pi`] .
+ finally show ?thesis unfolding Ifloat_min by auto
+ qed
+ moreover have "cos x \<le> Ifloat (Float 1 0)" by auto
+ ultimately show ?thesis using bnds unfolding bnds_cos_def Let_def if_not_P[OF not_out] if_not_P[OF not_ux] if_not_P[OF False] by auto
+ qed
+ qed
+ qed
+qed
+
+subsection "Compute the sinus in the entire domain"
+
+function lb_sin :: "nat \<Rightarrow> float \<Rightarrow> float" and ub_sin :: "nat \<Rightarrow> float \<Rightarrow> float" where
+"lb_sin prec x = (let sqr_diff = \<lambda> x. if x > 1 then 0 else 1 - x * x
+ in if x < 0 then - ub_sin prec (- x)
+else if x \<le> Float 1 -1 then x * lb_sin_cos_aux prec (get_even (prec div 4 + 1)) 2 1 (x * x)
+ else the (lb_sqrt prec (sqr_diff (ub_cos prec x))))" |
+
+"ub_sin prec x = (let sqr_diff = \<lambda> x. if x < 0 then 1 else 1 - x * x
+ in if x < 0 then - lb_sin prec (- x)
+else if x \<le> Float 1 -1 then x * ub_sin_cos_aux prec (get_odd (prec div 4 + 1)) 2 1 (x * x)
+ else the (ub_sqrt prec (sqr_diff (lb_cos prec x))))"
+by pat_completeness auto
+termination by (relation "measure (\<lambda> v. let (prec, x) = sum_case id id v in (if x < 0 then 1 else 0))", auto simp add: less_float_def)
+
+definition bnds_sin :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float * float" where
+"bnds_sin prec lx ux = (let
+ lpi = lb_pi prec ;
+ half_pi = lpi * Float 1 -1
+ in if lx \<le> - half_pi \<or> half_pi \<le> ux then (Float -1 0, Float 1 0)
+ else (lb_sin prec lx, ub_sin prec ux))"
+
+lemma lb_sin: assumes "- (pi / 2) \<le> Ifloat x" and "Ifloat x \<le> pi / 2"
+ shows "sin (Ifloat x) \<in> { Ifloat (lb_sin prec x) .. Ifloat (ub_sin prec x) }" (is "?sin x \<in> { ?lb x .. ?ub x}")
+proof -
+ { fix x :: float assume "0 \<le> Ifloat x" and "Ifloat x \<le> pi / 2"
+ hence "\<not> (x < 0)" and "- (pi / 2) \<le> Ifloat x" unfolding less_float_def using pi_ge_two by auto
+
+ have "Ifloat x \<le> pi" using `Ifloat x \<le> pi / 2` using pi_ge_two by auto
+
+ have "?sin x \<in> { ?lb x .. ?ub x}"
+ proof (cases "x \<le> Float 1 -1")
+ case True from sin_boundaries[OF `0 \<le> Ifloat x` `Ifloat x \<le> pi / 2`]
+ show ?thesis unfolding lb_sin.simps[of prec x] ub_sin.simps[of prec x] if_not_P[OF `\<not> (x < 0)`] if_P[OF True] Let_def .
+ next
+ case False
+ have "0 \<le> cos (Ifloat x)" using cos_ge_zero[OF _ `Ifloat x \<le> pi /2`] `0 \<le> Ifloat x` pi_ge_two by auto
+ have "0 \<le> sin (Ifloat x)" using `0 \<le> Ifloat x` and `Ifloat x \<le> pi / 2` using sin_ge_zero by auto
+
+ have "?sin x \<le> ?ub x"
+ proof (cases "lb_cos prec x < 0")
+ case True
+ have "?sin x \<le> 1" using sin_le_one .
+ also have "\<dots> \<le> Ifloat (the (ub_sqrt prec 1))" using ub_sqrt_lower_bound[where prec=prec and x=1] unfolding Ifloat_1 by auto
+ finally show ?thesis unfolding ub_sin.simps if_not_P[OF `\<not> (x < 0)`] if_not_P[OF `\<not> x \<le> Float 1 -1`] if_P[OF True] Let_def .
+ next
+ case False hence "0 \<le> Ifloat (lb_cos prec x)" unfolding less_float_def by auto
+
+ have "sin (Ifloat x) = sqrt (1 - cos (Ifloat x) ^ 2)" unfolding sin_squared_eq[symmetric] real_sqrt_abs using `0 \<le> sin (Ifloat x)` by auto
+ also have "\<dots> \<le> sqrt (Ifloat (1 - lb_cos prec x * lb_cos prec x))"
+ proof (rule real_sqrt_le_mono)
+ have "Ifloat (lb_cos prec x * lb_cos prec x) \<le> cos (Ifloat x) ^ 2" unfolding numeral_2_eq_2 power_Suc2 realpow_0 Ifloat_mult
+ using `0 \<le> Ifloat (lb_cos prec x)` lb_cos[OF `0 \<le> Ifloat x` `Ifloat x \<le> pi`] `0 \<le> cos (Ifloat x)` by(auto intro!: mult_mono)
+ thus "1 - cos (Ifloat x) ^ 2 \<le> Ifloat (1 - lb_cos prec x * lb_cos prec x)" unfolding Ifloat_sub Ifloat_1 by auto
+ qed
+ also have "\<dots> \<le> Ifloat (the (ub_sqrt prec (1 - lb_cos prec x * lb_cos prec x)))"
+ proof (rule ub_sqrt_lower_bound)
+ have "Ifloat (lb_cos prec x) \<le> cos (Ifloat x)" using lb_cos[OF `0 \<le> Ifloat x` `Ifloat x \<le> pi`] by auto
+ from mult_mono[OF order_trans[OF this cos_le_one] order_trans[OF this cos_le_one]]
+ have "Ifloat (lb_cos prec x) * Ifloat (lb_cos prec x) \<le> 1" using `0 \<le> Ifloat (lb_cos prec x)` by auto
+ thus "0 \<le> Ifloat (1 - lb_cos prec x * lb_cos prec x)" by auto
+ qed
+ finally show ?thesis unfolding ub_sin.simps if_not_P[OF `\<not> (x < 0)`] if_not_P[OF `\<not> x \<le> Float 1 -1`] if_not_P[OF False] Let_def .
+ qed
+ moreover
+ have "?lb x \<le> ?sin x"
+ proof (cases "1 < ub_cos prec x")
+ case True
+ show ?thesis unfolding lb_sin.simps if_not_P[OF `\<not> (x < 0)`] if_not_P[OF `\<not> x \<le> Float 1 -1`] if_P[OF True] Let_def
+ by (rule order_trans[OF _ sin_ge_zero[OF `0 \<le> Ifloat x` `Ifloat x \<le> pi`]])
+ (auto simp add: lb_sqrt_upper_bound[where prec=prec and x=0, unfolded Ifloat_0 real_sqrt_zero])
+ next
+ case False hence "Ifloat (ub_cos prec x) \<le> 1" unfolding less_float_def by auto
+ have "0 \<le> Ifloat (ub_cos prec x)" using order_trans[OF `0 \<le> cos (Ifloat x)`] lb_cos `0 \<le> Ifloat x` `Ifloat x \<le> pi` by auto
+
+ have "Ifloat (the (lb_sqrt prec (1 - ub_cos prec x * ub_cos prec x))) \<le> sqrt (Ifloat (1 - ub_cos prec x * ub_cos prec x))"
+ proof (rule lb_sqrt_upper_bound)
+ from mult_mono[OF `Ifloat (ub_cos prec x) \<le> 1` `Ifloat (ub_cos prec x) \<le> 1`] `0 \<le> Ifloat (ub_cos prec x)`
+ have "Ifloat (ub_cos prec x) * Ifloat (ub_cos prec x) \<le> 1" by auto
+ thus "0 \<le> Ifloat (1 - ub_cos prec x * ub_cos prec x)" by auto
+ qed
+ also have "\<dots> \<le> sqrt (1 - cos (Ifloat x) ^ 2)"
+ proof (rule real_sqrt_le_mono)
+ have "cos (Ifloat x) ^ 2 \<le> Ifloat (ub_cos prec x * ub_cos prec x)" unfolding numeral_2_eq_2 power_Suc2 realpow_0 Ifloat_mult
+ using `0 \<le> Ifloat (ub_cos prec x)` lb_cos[OF `0 \<le> Ifloat x` `Ifloat x \<le> pi`] `0 \<le> cos (Ifloat x)` by(auto intro!: mult_mono)
+ thus "Ifloat (1 - ub_cos prec x * ub_cos prec x) \<le> 1 - cos (Ifloat x) ^ 2" unfolding Ifloat_sub Ifloat_1 by auto
+ qed
+ also have "\<dots> = sin (Ifloat x)" unfolding sin_squared_eq[symmetric] real_sqrt_abs using `0 \<le> sin (Ifloat x)` by auto
+ finally show ?thesis unfolding lb_sin.simps if_not_P[OF `\<not> (x < 0)`] if_not_P[OF `\<not> x \<le> Float 1 -1`] if_not_P[OF False] Let_def .
+ qed
+ ultimately show ?thesis by auto
+ qed
+ } note for_pos = this
+
+ show ?thesis
+ proof (cases "x < 0")
+ case True
+ hence "0 \<le> Ifloat (-x)" and "Ifloat (- x) \<le> pi / 2" using `-(pi/2) \<le> Ifloat x` unfolding less_float_def by auto
+ from for_pos[OF this]
+ show ?thesis unfolding Ifloat_minus sin_minus lb_sin.simps[of prec x] ub_sin.simps[of prec x] if_P[OF True] Let_def atLeastAtMost_iff by auto
+ next
+ case False hence "0 \<le> Ifloat x" unfolding less_float_def by auto
+ from for_pos[OF this `Ifloat x \<le> pi /2`]
+ show ?thesis .
+ qed
+qed
+
+lemma bnds_sin: "\<forall> x lx ux. (l, u) = bnds_sin prec lx ux \<and> x \<in> {Ifloat lx .. Ifloat ux} \<longrightarrow> Ifloat l \<le> sin x \<and> sin x \<le> Ifloat u"
+proof (rule allI, rule allI, rule allI, rule impI)
+ fix x lx ux
+ assume "(l, u) = bnds_sin prec lx ux \<and> x \<in> {Ifloat lx .. Ifloat ux}"
+ hence bnds: "(l, u) = bnds_sin prec lx ux" and x: "x \<in> {Ifloat lx .. Ifloat ux}" by auto
+ show "Ifloat l \<le> sin x \<and> sin x \<le> Ifloat u"
+ proof (cases "lx \<le> - (lb_pi prec * Float 1 -1) \<or> lb_pi prec * Float 1 -1 \<le> ux")
+ case True show ?thesis using bnds unfolding bnds_sin_def if_P[OF True] Let_def by auto
+ next
+ case False
+ hence "- lb_pi prec * Float 1 -1 \<le> lx" and "ux \<le> lb_pi prec * Float 1 -1" unfolding le_float_def by auto
+ moreover have "Ifloat (lb_pi prec * Float 1 -1) \<le> pi / 2" unfolding Ifloat_mult using pi_boundaries by auto
+ ultimately have "- (pi / 2) \<le> Ifloat lx" and "Ifloat ux \<le> pi / 2" and "Ifloat lx \<le> Ifloat ux" unfolding le_float_def using x by auto
+ hence "- (pi / 2) \<le> Ifloat ux" and "Ifloat lx \<le> pi / 2" by auto
+
+ have "- (pi / 2) \<le> x""x \<le> pi / 2" using `Ifloat ux \<le> pi / 2` `- (pi /2) \<le> Ifloat lx` x by auto
+
+ { have "Ifloat (lb_sin prec lx) \<le> sin (Ifloat lx)" using lb_sin[OF `- (pi / 2) \<le> Ifloat lx` `Ifloat lx \<le> pi / 2`] unfolding atLeastAtMost_iff by auto
+ also have "\<dots> \<le> sin x" using sin_monotone_2pi' `- (pi / 2) \<le> Ifloat lx` x `x \<le> pi / 2` by auto
+ finally have "Ifloat (lb_sin prec lx) \<le> sin x" . }
+ moreover
+ { have "sin x \<le> sin (Ifloat ux)" using sin_monotone_2pi' `- (pi / 2) \<le> x` x `Ifloat ux \<le> pi / 2` by auto
+ also have "\<dots> \<le> Ifloat (ub_sin prec ux)" using lb_sin[OF `- (pi / 2) \<le> Ifloat ux` `Ifloat ux \<le> pi / 2`] unfolding atLeastAtMost_iff by auto
+ finally have "sin x \<le> Ifloat (ub_sin prec ux)" . }
+ ultimately
+ show ?thesis using bnds unfolding bnds_sin_def if_not_P[OF False] Let_def by auto
+ qed
+qed
+
+section "Exponential function"
+
+subsection "Compute the series of the exponential function"
+
+fun ub_exp_horner :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float" and lb_exp_horner :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float" where
+"ub_exp_horner prec 0 i k x = 0" |
+"ub_exp_horner prec (Suc n) i k x = rapprox_rat prec 1 (int k) + x * lb_exp_horner prec n (i + 1) (k * i) x" |
+"lb_exp_horner prec 0 i k x = 0" |
+"lb_exp_horner prec (Suc n) i k x = lapprox_rat prec 1 (int k) + x * ub_exp_horner prec n (i + 1) (k * i) x"
+
+lemma bnds_exp_horner: assumes "Ifloat x \<le> 0"
+ shows "exp (Ifloat x) \<in> { Ifloat (lb_exp_horner prec (get_even n) 1 1 x) .. Ifloat (ub_exp_horner prec (get_odd n) 1 1 x) }"
+proof -
+ { fix n
+ have F: "\<And> m. ((\<lambda>i. i + 1) ^ n) m = n + m" by (induct n, auto)
+ have "fact (Suc n) = fact n * ((\<lambda>i. i + 1) ^ n) 1" unfolding F by auto } note f_eq = this
+
+ note bounds = horner_bounds_nonpos[where f="fact" and lb="lb_exp_horner prec" and ub="ub_exp_horner prec" and j'=0 and s=1,
+ OF assms f_eq lb_exp_horner.simps ub_exp_horner.simps]
+
+ { have "Ifloat (lb_exp_horner prec (get_even n) 1 1 x) \<le> (\<Sum>j = 0..<get_even n. 1 / real (fact j) * Ifloat x ^ j)"
+ using bounds(1) by auto
+ also have "\<dots> \<le> exp (Ifloat x)"
+ proof -
+ obtain t where "\<bar>t\<bar> \<le> \<bar>Ifloat x\<bar>" and "exp (Ifloat x) = (\<Sum>m = 0..<get_even n. (Ifloat x) ^ m / real (fact m)) + exp t / real (fact (get_even n)) * (Ifloat x) ^ (get_even n)"
+ using Maclaurin_exp_le by blast
+ moreover have "0 \<le> exp t / real (fact (get_even n)) * (Ifloat x) ^ (get_even n)"
+ by (auto intro!: mult_nonneg_nonneg divide_nonneg_pos simp add: get_even zero_le_even_power exp_gt_zero)
+ ultimately show ?thesis
+ using get_odd exp_gt_zero by (auto intro!: pordered_cancel_semiring_class.mult_nonneg_nonneg)
+ qed
+ finally have "Ifloat (lb_exp_horner prec (get_even n) 1 1 x) \<le> exp (Ifloat x)" .
+ } moreover
+ {
+ have x_less_zero: "Ifloat x ^ get_odd n \<le> 0"
+ proof (cases "Ifloat x = 0")
+ case True
+ have "(get_odd n) \<noteq> 0" using get_odd[THEN odd_pos] by auto
+ thus ?thesis unfolding True power_0_left by auto
+ next
+ case False hence "Ifloat x < 0" using `Ifloat x \<le> 0` by auto
+ show ?thesis by (rule less_imp_le, auto simp add: power_less_zero_eq get_odd `Ifloat x < 0`)
+ qed
+
+ obtain t where "\<bar>t\<bar> \<le> \<bar>Ifloat x\<bar>" and "exp (Ifloat x) = (\<Sum>m = 0..<get_odd n. (Ifloat x) ^ m / real (fact m)) + exp t / real (fact (get_odd n)) * (Ifloat x) ^ (get_odd n)"
+ using Maclaurin_exp_le by blast
+ moreover have "exp t / real (fact (get_odd n)) * (Ifloat x) ^ (get_odd n) \<le> 0"
+ by (auto intro!: mult_nonneg_nonpos divide_nonpos_pos simp add: x_less_zero exp_gt_zero)
+ ultimately have "exp (Ifloat x) \<le> (\<Sum>j = 0..<get_odd n. 1 / real (fact j) * Ifloat x ^ j)"
+ using get_odd exp_gt_zero by (auto intro!: pordered_cancel_semiring_class.mult_nonneg_nonneg)
+ also have "\<dots> \<le> Ifloat (ub_exp_horner prec (get_odd n) 1 1 x)"
+ using bounds(2) by auto
+ finally have "exp (Ifloat x) \<le> Ifloat (ub_exp_horner prec (get_odd n) 1 1 x)" .
+ } ultimately show ?thesis by auto
+qed
+
+subsection "Compute the exponential function on the entire domain"
+
+function ub_exp :: "nat \<Rightarrow> float \<Rightarrow> float" and lb_exp :: "nat \<Rightarrow> float \<Rightarrow> float" where
+"lb_exp prec x = (if 0 < x then float_divl prec 1 (ub_exp prec (-x))
+ else let
+ horner = (\<lambda> x. let y = lb_exp_horner prec (get_even (prec + 2)) 1 1 x in if y \<le> 0 then Float 1 -2 else y)
+ in if x < - 1 then (case floor_fl x of (Float m e) \<Rightarrow> (horner (float_divl prec x (- Float m e))) ^ (nat (-m) * 2 ^ nat e))
+ else horner x)" |
+"ub_exp prec x = (if 0 < x then float_divr prec 1 (lb_exp prec (-x))
+ else if x < - 1 then (case floor_fl x of (Float m e) \<Rightarrow>
+ (ub_exp_horner prec (get_odd (prec + 2)) 1 1 (float_divr prec x (- Float m e))) ^ (nat (-m) * 2 ^ nat e))
+ else ub_exp_horner prec (get_odd (prec + 2)) 1 1 x)"
+by pat_completeness auto
+termination by (relation "measure (\<lambda> v. let (prec, x) = sum_case id id v in (if 0 < x then 1 else 0))", auto simp add: less_float_def)
+
+lemma exp_m1_ge_quarter: "(1 / 4 :: real) \<le> exp (- 1)"
+proof -
+ have eq4: "4 = Suc (Suc (Suc (Suc 0)))" by auto
+
+ have "1 / 4 = Ifloat (Float 1 -2)" unfolding Float_num by auto
+ also have "\<dots> \<le> Ifloat (lb_exp_horner 1 (get_even 4) 1 1 (- 1))"
+ unfolding get_even_def eq4
+ by (auto simp add: lapprox_posrat_def rapprox_posrat_def normfloat.simps)
+ also have "\<dots> \<le> exp (Ifloat (- 1))" using bnds_exp_horner[where x="- 1"] by auto
+ finally show ?thesis unfolding Ifloat_minus Ifloat_1 .
+qed
+
+lemma lb_exp_pos: assumes "\<not> 0 < x" shows "0 < lb_exp prec x"
+proof -
+ let "?lb_horner x" = "lb_exp_horner prec (get_even (prec + 2)) 1 1 x"
+ let "?horner x" = "let y = ?lb_horner x in if y \<le> 0 then Float 1 -2 else y"
+ have pos_horner: "\<And> x. 0 < ?horner x" unfolding Let_def by (cases "?lb_horner x \<le> 0", auto simp add: le_float_def less_float_def)
+ moreover { fix x :: float fix num :: nat
+ have "0 < Ifloat (?horner x) ^ num" using `0 < ?horner x`[unfolded less_float_def Ifloat_0] by (rule zero_less_power)
+ also have "\<dots> = Ifloat ((?horner x) ^ num)" using float_power by auto
+ finally have "0 < Ifloat ((?horner x) ^ num)" .
+ }
+ ultimately show ?thesis
+ unfolding lb_exp.simps if_not_P[OF `\<not> 0 < x`] Let_def by (cases "floor_fl x", cases "x < - 1", auto simp add: le_float_def less_float_def normfloat)
+qed
+
+lemma exp_boundaries': assumes "x \<le> 0"
+ shows "exp (Ifloat x) \<in> { Ifloat (lb_exp prec x) .. Ifloat (ub_exp prec x)}"
+proof -
+ let "?lb_exp_horner x" = "lb_exp_horner prec (get_even (prec + 2)) 1 1 x"
+ let "?ub_exp_horner x" = "ub_exp_horner prec (get_odd (prec + 2)) 1 1 x"
+
+ have "Ifloat x \<le> 0" and "\<not> x > 0" using `x \<le> 0` unfolding le_float_def less_float_def by auto
+ show ?thesis
+ proof (cases "x < - 1")
+ case False hence "- 1 \<le> Ifloat x" unfolding less_float_def by auto
+ show ?thesis
+ proof (cases "?lb_exp_horner x \<le> 0")
+ from `\<not> x < - 1` have "- 1 \<le> Ifloat x" unfolding less_float_def by auto
+ hence "exp (- 1) \<le> exp (Ifloat x)" unfolding exp_le_cancel_iff .
+ from order_trans[OF exp_m1_ge_quarter this]
+ have "Ifloat (Float 1 -2) \<le> exp (Ifloat x)" unfolding Float_num .
+ moreover case True
+ ultimately show ?thesis using bnds_exp_horner `Ifloat x \<le> 0` `\<not> x > 0` `\<not> x < - 1` by auto
+ next
+ case False thus ?thesis using bnds_exp_horner `Ifloat x \<le> 0` `\<not> x > 0` `\<not> x < - 1` by (auto simp add: Let_def)
+ qed
+ next
+ case True
+
+ obtain m e where Float_floor: "floor_fl x = Float m e" by (cases "floor_fl x", auto)
+ let ?num = "nat (- m) * 2 ^ nat e"
+
+ have "Ifloat (floor_fl x) < - 1" using floor_fl `x < - 1` unfolding le_float_def less_float_def Ifloat_minus Ifloat_1 by (rule order_le_less_trans)
+ hence "Ifloat (floor_fl x) < 0" unfolding Float_floor Ifloat.simps using zero_less_pow2[of xe] by auto
+ hence "m < 0"
+ unfolding less_float_def Ifloat_0 Float_floor Ifloat.simps
+ unfolding pos_prod_lt[OF zero_less_pow2[of e], unfolded real_mult_commute] by auto
+ hence "1 \<le> - m" by auto
+ hence "0 < nat (- m)" by auto
+ moreover
+ have "0 \<le> e" using floor_pos_exp Float_floor[symmetric] by auto
+ hence "(0::nat) < 2 ^ nat e" by auto
+ ultimately have "0 < ?num" by auto
+ hence "real ?num \<noteq> 0" by auto
+ have e_nat: "int (nat e) = e" using `0 \<le> e` by auto
+ have num_eq: "real ?num = Ifloat (- floor_fl x)" using `0 < nat (- m)`
+ unfolding Float_floor Ifloat_minus Ifloat.simps real_of_nat_mult pow2_int[of "nat e", unfolded e_nat] realpow_real_of_nat[symmetric] by auto
+ have "0 < - floor_fl x" using `0 < ?num`[unfolded real_of_nat_less_iff[symmetric]] unfolding less_float_def num_eq[symmetric] Ifloat_0 real_of_nat_zero .
+ hence "Ifloat (floor_fl x) < 0" unfolding less_float_def by auto
+
+ have "exp (Ifloat x) \<le> Ifloat (ub_exp prec x)"
+ proof -
+ have div_less_zero: "Ifloat (float_divr prec x (- floor_fl x)) \<le> 0"
+ using float_divr_nonpos_pos_upper_bound[OF `x \<le> 0` `0 < - floor_fl x`] unfolding le_float_def Ifloat_0 .
+
+ have "exp (Ifloat x) = exp (real ?num * (Ifloat x / real ?num))" using `real ?num \<noteq> 0` by auto
+ also have "\<dots> = exp (Ifloat x / real ?num) ^ ?num" unfolding exp_real_of_nat_mult ..
+ also have "\<dots> \<le> exp (Ifloat (float_divr prec x (- floor_fl x))) ^ ?num" unfolding num_eq
+ by (rule power_mono, rule exp_le_cancel_iff[THEN iffD2], rule float_divr) auto
+ also have "\<dots> \<le> Ifloat ((?ub_exp_horner (float_divr prec x (- floor_fl x))) ^ ?num)" unfolding float_power
+ by (rule power_mono, rule bnds_exp_horner[OF div_less_zero, unfolded atLeastAtMost_iff, THEN conjunct2], auto)
+ finally show ?thesis unfolding ub_exp.simps if_not_P[OF `\<not> 0 < x`] if_P[OF `x < - 1`] float.cases Float_floor Let_def .
+ qed
+ moreover
+ have "Ifloat (lb_exp prec x) \<le> exp (Ifloat x)"
+ proof -
+ let ?divl = "float_divl prec x (- Float m e)"
+ let ?horner = "?lb_exp_horner ?divl"
+
+ show ?thesis
+ proof (cases "?horner \<le> 0")
+ case False hence "0 \<le> Ifloat ?horner" unfolding le_float_def by auto
+
+ have div_less_zero: "Ifloat (float_divl prec x (- floor_fl x)) \<le> 0"
+ using `Ifloat (floor_fl x) < 0` `Ifloat x \<le> 0` by (auto intro!: order_trans[OF float_divl] divide_nonpos_neg)
+
+ have "Ifloat ((?lb_exp_horner (float_divl prec x (- floor_fl x))) ^ ?num) \<le>
+ exp (Ifloat (float_divl prec x (- floor_fl x))) ^ ?num" unfolding float_power
+ using `0 \<le> Ifloat ?horner`[unfolded Float_floor[symmetric]] bnds_exp_horner[OF div_less_zero, unfolded atLeastAtMost_iff, THEN conjunct1] by (auto intro!: power_mono)
+ also have "\<dots> \<le> exp (Ifloat x / real ?num) ^ ?num" unfolding num_eq
+ using float_divl by (auto intro!: power_mono simp del: Ifloat_minus)
+ also have "\<dots> = exp (real ?num * (Ifloat x / real ?num))" unfolding exp_real_of_nat_mult ..
+ also have "\<dots> = exp (Ifloat x)" using `real ?num \<noteq> 0` by auto
+ finally show ?thesis
+ unfolding lb_exp.simps if_not_P[OF `\<not> 0 < x`] if_P[OF `x < - 1`] float.cases Float_floor Let_def if_not_P[OF False] by auto
+ next
+ case True
+ have "Ifloat (floor_fl x) \<noteq> 0" and "Ifloat (floor_fl x) \<le> 0" using `Ifloat (floor_fl x) < 0` by auto
+ from divide_right_mono_neg[OF floor_fl[of x] `Ifloat (floor_fl x) \<le> 0`, unfolded divide_self[OF `Ifloat (floor_fl x) \<noteq> 0`]]
+ have "- 1 \<le> Ifloat x / Ifloat (- floor_fl x)" unfolding Ifloat_minus by auto
+ from order_trans[OF exp_m1_ge_quarter this[unfolded exp_le_cancel_iff[where x="- 1", symmetric]]]
+ have "Ifloat (Float 1 -2) \<le> exp (Ifloat x / Ifloat (- floor_fl x))" unfolding Float_num .
+ hence "Ifloat (Float 1 -2) ^ ?num \<le> exp (Ifloat x / Ifloat (- floor_fl x)) ^ ?num"
+ by (auto intro!: power_mono simp add: Float_num)
+ also have "\<dots> = exp (Ifloat x)" unfolding num_eq exp_real_of_nat_mult[symmetric] using `Ifloat (floor_fl x) \<noteq> 0` by auto
+ finally show ?thesis
+ unfolding lb_exp.simps if_not_P[OF `\<not> 0 < x`] if_P[OF `x < - 1`] float.cases Float_floor Let_def if_P[OF True] float_power .
+ qed
+ qed
+ ultimately show ?thesis by auto
+ qed
+qed
+
+lemma exp_boundaries: "exp (Ifloat x) \<in> { Ifloat (lb_exp prec x) .. Ifloat (ub_exp prec x)}"
+proof -
+ show ?thesis
+ proof (cases "0 < x")
+ case False hence "x \<le> 0" unfolding less_float_def le_float_def by auto
+ from exp_boundaries'[OF this] show ?thesis .
+ next
+ case True hence "-x \<le> 0" unfolding less_float_def le_float_def by auto
+
+ have "Ifloat (lb_exp prec x) \<le> exp (Ifloat x)"
+ proof -
+ from exp_boundaries'[OF `-x \<le> 0`]
+ have ub_exp: "exp (- Ifloat x) \<le> Ifloat (ub_exp prec (-x))" unfolding atLeastAtMost_iff Ifloat_minus by auto
+
+ have "Ifloat (float_divl prec 1 (ub_exp prec (-x))) \<le> Ifloat 1 / Ifloat (ub_exp prec (-x))" using float_divl .
+ also have "Ifloat 1 / Ifloat (ub_exp prec (-x)) \<le> exp (Ifloat x)"
+ using ub_exp[unfolded inverse_le_iff_le[OF order_less_le_trans[OF exp_gt_zero ub_exp] exp_gt_zero, symmetric]]
+ unfolding exp_minus nonzero_inverse_inverse_eq[OF exp_not_eq_zero] inverse_eq_divide by auto
+ finally show ?thesis unfolding lb_exp.simps if_P[OF True] .
+ qed
+ moreover
+ have "exp (Ifloat x) \<le> Ifloat (ub_exp prec x)"
+ proof -
+ have "\<not> 0 < -x" using `0 < x` unfolding less_float_def by auto
+
+ from exp_boundaries'[OF `-x \<le> 0`]
+ have lb_exp: "Ifloat (lb_exp prec (-x)) \<le> exp (- Ifloat x)" unfolding atLeastAtMost_iff Ifloat_minus by auto
+
+ have "exp (Ifloat x) \<le> Ifloat 1 / Ifloat (lb_exp prec (-x))"
+ using lb_exp[unfolded inverse_le_iff_le[OF exp_gt_zero lb_exp_pos[OF `\<not> 0 < -x`, unfolded less_float_def Ifloat_0], symmetric]]
+ unfolding exp_minus nonzero_inverse_inverse_eq[OF exp_not_eq_zero] inverse_eq_divide Ifloat_1 by auto
+ also have "\<dots> \<le> Ifloat (float_divr prec 1 (lb_exp prec (-x)))" using float_divr .
+ finally show ?thesis unfolding ub_exp.simps if_P[OF True] .
+ qed
+ ultimately show ?thesis by auto
+ qed
+qed
+
+lemma bnds_exp: "\<forall> x lx ux. (l, u) = (lb_exp prec lx, ub_exp prec ux) \<and> x \<in> {Ifloat lx .. Ifloat ux} \<longrightarrow> Ifloat l \<le> exp x \<and> exp x \<le> Ifloat u"
+proof (rule allI, rule allI, rule allI, rule impI)
+ fix x lx ux
+ assume "(l, u) = (lb_exp prec lx, ub_exp prec ux) \<and> x \<in> {Ifloat lx .. Ifloat ux}"
+ hence l: "lb_exp prec lx = l " and u: "ub_exp prec ux = u" and x: "x \<in> {Ifloat lx .. Ifloat ux}" by auto
+
+ { from exp_boundaries[of lx prec, unfolded l]
+ have "Ifloat l \<le> exp (Ifloat lx)" by (auto simp del: lb_exp.simps)
+ also have "\<dots> \<le> exp x" using x by auto
+ finally have "Ifloat l \<le> exp x" .
+ } moreover
+ { have "exp x \<le> exp (Ifloat ux)" using x by auto
+ also have "\<dots> \<le> Ifloat u" using exp_boundaries[of ux prec, unfolded u] by (auto simp del: ub_exp.simps)
+ finally have "exp x \<le> Ifloat u" .
+ } ultimately show "Ifloat l \<le> exp x \<and> exp x \<le> Ifloat u" ..
+qed
+
+section "Logarithm"
+
+subsection "Compute the logarithm series"
+
+fun ub_ln_horner :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float"
+and lb_ln_horner :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float" where
+"ub_ln_horner prec 0 i x = 0" |
+"ub_ln_horner prec (Suc n) i x = rapprox_rat prec 1 (int i) - x * lb_ln_horner prec n (Suc i) x" |
+"lb_ln_horner prec 0 i x = 0" |
+"lb_ln_horner prec (Suc n) i x = lapprox_rat prec 1 (int i) - x * ub_ln_horner prec n (Suc i) x"
+
+lemma ln_bounds:
+ assumes "0 \<le> x" and "x < 1"
+ shows "(\<Sum>i=0..<2*n. -1^i * (1 / real (i + 1)) * x^(Suc i)) \<le> ln (x + 1)" (is "?lb")
+ and "ln (x + 1) \<le> (\<Sum>i=0..<2*n + 1. -1^i * (1 / real (i + 1)) * x^(Suc i))" (is "?ub")
+proof -
+ let "?a n" = "(1/real (n +1)) * x^(Suc n)"
+
+ have ln_eq: "(\<Sum> i. -1^i * ?a i) = ln (x + 1)"
+ using ln_series[of "x + 1"] `0 \<le> x` `x < 1` by auto
+
+ have "norm x < 1" using assms by auto
+ have "?a ----> 0" unfolding Suc_plus1[symmetric] inverse_eq_divide[symmetric]
+ using LIMSEQ_mult[OF LIMSEQ_inverse_real_of_nat LIMSEQ_Suc[OF LIMSEQ_power_zero[OF `norm x < 1`]]] by auto
+ { fix n have "0 \<le> ?a n" by (rule mult_nonneg_nonneg, auto intro!: mult_nonneg_nonneg simp add: `0 \<le> x`) }
+ { fix n have "?a (Suc n) \<le> ?a n" unfolding inverse_eq_divide[symmetric]
+ proof (rule mult_mono)
+ show "0 \<le> x ^ Suc (Suc n)" by (auto intro!: mult_nonneg_nonneg simp add: `0 \<le> x`)
+ have "x ^ Suc (Suc n) \<le> x ^ Suc n * 1" unfolding power_Suc2 real_mult_assoc[symmetric]
+ by (rule mult_left_mono, fact less_imp_le[OF `x < 1`], auto intro!: mult_nonneg_nonneg simp add: `0 \<le> x`)
+ thus "x ^ Suc (Suc n) \<le> x ^ Suc n" by auto
+ qed auto }
+ from summable_Leibniz'(2,4)[OF `?a ----> 0` `\<And>n. 0 \<le> ?a n`, OF `\<And>n. ?a (Suc n) \<le> ?a n`, unfolded ln_eq]
+ show "?lb" and "?ub" by auto
+qed
+
+lemma ln_float_bounds:
+ assumes "0 \<le> Ifloat x" and "Ifloat x < 1"
+ shows "Ifloat (x * lb_ln_horner prec (get_even n) 1 x) \<le> ln (Ifloat x + 1)" (is "?lb \<le> ?ln")
+ and "ln (Ifloat x + 1) \<le> Ifloat (x * ub_ln_horner prec (get_odd n) 1 x)" (is "?ln \<le> ?ub")
+proof -
+ obtain ev where ev: "get_even n = 2 * ev" using get_even_double ..
+ obtain od where od: "get_odd n = 2 * od + 1" using get_odd_double ..
+
+ let "?s n" = "-1^n * (1 / real (1 + n)) * (Ifloat x)^(Suc n)"
+
+ have "?lb \<le> setsum ?s {0 ..< 2 * ev}" unfolding power_Suc2 real_mult_assoc[symmetric] Ifloat_mult setsum_left_distrib[symmetric] unfolding real_mult_commute[of "Ifloat x"] ev
+ using horner_bounds(1)[where G="\<lambda> i k. Suc k" and F="\<lambda>x. x" and f="\<lambda>x. x" and lb="\<lambda>n i k x. lb_ln_horner prec n k x" and ub="\<lambda>n i k x. ub_ln_horner prec n k x" and j'=1 and n="2*ev",
+ OF `0 \<le> Ifloat x` refl lb_ln_horner.simps ub_ln_horner.simps] `0 \<le> Ifloat x`
+ by (rule mult_right_mono)
+ also have "\<dots> \<le> ?ln" using ln_bounds(1)[OF `0 \<le> Ifloat x` `Ifloat x < 1`] by auto
+ finally show "?lb \<le> ?ln" .
+
+ have "?ln \<le> setsum ?s {0 ..< 2 * od + 1}" using ln_bounds(2)[OF `0 \<le> Ifloat x` `Ifloat x < 1`] by auto
+ also have "\<dots> \<le> ?ub" unfolding power_Suc2 real_mult_assoc[symmetric] Ifloat_mult setsum_left_distrib[symmetric] unfolding real_mult_commute[of "Ifloat x"] od
+ using horner_bounds(2)[where G="\<lambda> i k. Suc k" and F="\<lambda>x. x" and f="\<lambda>x. x" and lb="\<lambda>n i k x. lb_ln_horner prec n k x" and ub="\<lambda>n i k x. ub_ln_horner prec n k x" and j'=1 and n="2*od+1",
+ OF `0 \<le> Ifloat x` refl lb_ln_horner.simps ub_ln_horner.simps] `0 \<le> Ifloat x`
+ by (rule mult_right_mono)
+ finally show "?ln \<le> ?ub" .
+qed
+
+lemma ln_add: assumes "0 < x" and "0 < y" shows "ln (x + y) = ln x + ln (1 + y / x)"
+proof -
+ have "x \<noteq> 0" using assms by auto
+ have "x + y = x * (1 + y / x)" unfolding right_distrib times_divide_eq_right nonzero_mult_divide_cancel_left[OF `x \<noteq> 0`] by auto
+ moreover
+ have "0 < y / x" using assms divide_pos_pos by auto
+ hence "0 < 1 + y / x" by auto
+ ultimately show ?thesis using ln_mult assms by auto
+qed
+
+subsection "Compute the logarithm of 2"
+
+definition ub_ln2 where "ub_ln2 prec = (let third = rapprox_rat (max prec 1) 1 3
+ in (Float 1 -1 * ub_ln_horner prec (get_odd prec) 1 (Float 1 -1)) +
+ (third * ub_ln_horner prec (get_odd prec) 1 third))"
+definition lb_ln2 where "lb_ln2 prec = (let third = lapprox_rat prec 1 3
+ in (Float 1 -1 * lb_ln_horner prec (get_even prec) 1 (Float 1 -1)) +
+ (third * lb_ln_horner prec (get_even prec) 1 third))"
+
+lemma ub_ln2: "ln 2 \<le> Ifloat (ub_ln2 prec)" (is "?ub_ln2")
+ and lb_ln2: "Ifloat (lb_ln2 prec) \<le> ln 2" (is "?lb_ln2")
+proof -
+ let ?uthird = "rapprox_rat (max prec 1) 1 3"
+ let ?lthird = "lapprox_rat prec 1 3"
+
+ have ln2_sum: "ln 2 = ln (1/2 + 1) + ln (1 / 3 + 1)"
+ using ln_add[of "3 / 2" "1 / 2"] by auto
+ have lb3: "Ifloat ?lthird \<le> 1 / 3" using lapprox_rat[of prec 1 3] by auto
+ hence lb3_ub: "Ifloat ?lthird < 1" by auto
+ have lb3_lb: "0 \<le> Ifloat ?lthird" using lapprox_rat_bottom[of 1 3] by auto
+ have ub3: "1 / 3 \<le> Ifloat ?uthird" using rapprox_rat[of 1 3] by auto
+ hence ub3_lb: "0 \<le> Ifloat ?uthird" by auto
+
+ have lb2: "0 \<le> Ifloat (Float 1 -1)" and ub2: "Ifloat (Float 1 -1) < 1" unfolding Float_num by auto
+
+ have "0 \<le> (1::int)" and "0 < (3::int)" by auto
+ have ub3_ub: "Ifloat ?uthird < 1" unfolding rapprox_rat.simps(2)[OF `0 \<le> 1` `0 < 3`]
+ by (rule rapprox_posrat_less1, auto)
+
+ have third_gt0: "(0 :: real) < 1 / 3 + 1" by auto
+ have uthird_gt0: "0 < Ifloat ?uthird + 1" using ub3_lb by auto
+ have lthird_gt0: "0 < Ifloat ?lthird + 1" using lb3_lb by auto
+
+ show ?ub_ln2 unfolding ub_ln2_def Let_def Ifloat_add ln2_sum Float_num(4)[symmetric]
+ proof (rule add_mono, fact ln_float_bounds(2)[OF lb2 ub2])
+ have "ln (1 / 3 + 1) \<le> ln (Ifloat ?uthird + 1)" unfolding ln_le_cancel_iff[OF third_gt0 uthird_gt0] using ub3 by auto
+ also have "\<dots> \<le> Ifloat (?uthird * ub_ln_horner prec (get_odd prec) 1 ?uthird)"
+ using ln_float_bounds(2)[OF ub3_lb ub3_ub] .
+ finally show "ln (1 / 3 + 1) \<le> Ifloat (?uthird * ub_ln_horner prec (get_odd prec) 1 ?uthird)" .
+ qed
+ show ?lb_ln2 unfolding lb_ln2_def Let_def Ifloat_add ln2_sum Float_num(4)[symmetric]
+ proof (rule add_mono, fact ln_float_bounds(1)[OF lb2 ub2])
+ have "Ifloat (?lthird * lb_ln_horner prec (get_even prec) 1 ?lthird) \<le> ln (Ifloat ?lthird + 1)"
+ using ln_float_bounds(1)[OF lb3_lb lb3_ub] .
+ also have "\<dots> \<le> ln (1 / 3 + 1)" unfolding ln_le_cancel_iff[OF lthird_gt0 third_gt0] using lb3 by auto
+ finally show "Ifloat (?lthird * lb_ln_horner prec (get_even prec) 1 ?lthird) \<le> ln (1 / 3 + 1)" .
+ qed
+qed
+
+subsection "Compute the logarithm in the entire domain"
+
+function ub_ln :: "nat \<Rightarrow> float \<Rightarrow> float option" and lb_ln :: "nat \<Rightarrow> float \<Rightarrow> float option" where
+"ub_ln prec x = (if x \<le> 0 then None
+ else if x < 1 then Some (- the (lb_ln prec (float_divl (max prec 1) 1 x)))
+ else let horner = \<lambda>x. (x - 1) * ub_ln_horner prec (get_odd prec) 1 (x - 1) in
+ if x < Float 1 1 then Some (horner x)
+ else let l = bitlen (mantissa x) - 1 in
+ Some (ub_ln2 prec * (Float (scale x + l) 0) + horner (Float (mantissa x) (- l))))" |
+"lb_ln prec x = (if x \<le> 0 then None
+ else if x < 1 then Some (- the (ub_ln prec (float_divr prec 1 x)))
+ else let horner = \<lambda>x. (x - 1) * lb_ln_horner prec (get_even prec) 1 (x - 1) in
+ if x < Float 1 1 then Some (horner x)
+ else let l = bitlen (mantissa x) - 1 in
+ Some (lb_ln2 prec * (Float (scale x + l) 0) + horner (Float (mantissa x) (- l))))"
+by pat_completeness auto
+
+termination proof (relation "measure (\<lambda> v. let (prec, x) = sum_case id id v in (if x < 1 then 1 else 0))", auto)
+ fix prec x assume "\<not> x \<le> 0" and "x < 1" and "float_divl (max prec (Suc 0)) 1 x < 1"
+ hence "0 < x" and "0 < max prec (Suc 0)" unfolding less_float_def le_float_def by auto
+ from float_divl_pos_less1_bound[OF `0 < x` `x < 1` `0 < max prec (Suc 0)`]
+ show False using `float_divl (max prec (Suc 0)) 1 x < 1` unfolding less_float_def le_float_def by auto
+next
+ fix prec x assume "\<not> x \<le> 0" and "x < 1" and "float_divr prec 1 x < 1"
+ hence "0 < x" unfolding less_float_def le_float_def by auto
+ from float_divr_pos_less1_lower_bound[OF `0 < x` `x < 1`, of prec]
+ show False using `float_divr prec 1 x < 1` unfolding less_float_def le_float_def by auto
+qed
+
+lemma ln_shifted_float: assumes "0 < m" shows "ln (Ifloat (Float m e)) = ln 2 * real (e + (bitlen m - 1)) + ln (Ifloat (Float m (- (bitlen m - 1))))"
+proof -
+ let ?B = "2^nat (bitlen m - 1)"
+ have "0 < real m" and "\<And>X. (0 :: real) < 2^X" and "0 < (2 :: real)" and "m \<noteq> 0" using assms by auto
+ hence "0 \<le> bitlen m - 1" using bitlen_ge1[OF `m \<noteq> 0`] by auto
+ show ?thesis
+ proof (cases "0 \<le> e")
+ case True
+ show ?thesis unfolding normalized_float[OF `m \<noteq> 0`]
+ unfolding ln_div[OF `0 < real m` `0 < ?B`] real_of_int_add ln_realpow[OF `0 < 2`]
+ unfolding Ifloat_ge0_exp[OF True] ln_mult[OF `0 < real m` `0 < 2^nat e`]
+ ln_realpow[OF `0 < 2`] algebra_simps using `0 \<le> bitlen m - 1` True by auto
+ next
+ case False hence "0 < -e" by auto
+ hence pow_gt0: "(0::real) < 2^nat (-e)" by auto
+ hence inv_gt0: "(0::real) < inverse (2^nat (-e))" by auto
+ show ?thesis unfolding normalized_float[OF `m \<noteq> 0`]
+ unfolding ln_div[OF `0 < real m` `0 < ?B`] real_of_int_add ln_realpow[OF `0 < 2`]
+ unfolding Ifloat_nge0_exp[OF False] ln_mult[OF `0 < real m` inv_gt0] ln_inverse[OF pow_gt0]
+ ln_realpow[OF `0 < 2`] algebra_simps using `0 \<le> bitlen m - 1` False by auto
+ qed
+qed
+
+lemma ub_ln_lb_ln_bounds': assumes "1 \<le> x"
+ shows "Ifloat (the (lb_ln prec x)) \<le> ln (Ifloat x) \<and> ln (Ifloat x) \<le> Ifloat (the (ub_ln prec x))"
+ (is "?lb \<le> ?ln \<and> ?ln \<le> ?ub")
+proof (cases "x < Float 1 1")
+ case True hence "Ifloat (x - 1) < 1" unfolding less_float_def Float_num by auto
+ have "\<not> x \<le> 0" and "\<not> x < 1" using `1 \<le> x` unfolding less_float_def le_float_def by auto
+ hence "0 \<le> Ifloat (x - 1)" using `1 \<le> x` unfolding less_float_def Float_num by auto
+ show ?thesis unfolding lb_ln.simps unfolding ub_ln.simps Let_def
+ using ln_float_bounds[OF `0 \<le> Ifloat (x - 1)` `Ifloat (x - 1) < 1`] `\<not> x \<le> 0` `\<not> x < 1` True by auto
+next
+ case False
+ have "\<not> x \<le> 0" and "\<not> x < 1" "0 < x" using `1 \<le> x` unfolding less_float_def le_float_def by auto
+ show ?thesis
+ proof (cases x)
+ case (Float m e)
+ let ?s = "Float (e + (bitlen m - 1)) 0"
+ let ?x = "Float m (- (bitlen m - 1))"
+
+ have "0 < m" and "m \<noteq> 0" using float_pos_m_pos `0 < x` Float by auto
+
+ {
+ have "Ifloat (lb_ln2 prec * ?s) \<le> ln 2 * real (e + (bitlen m - 1))" (is "?lb2 \<le> _")
+ unfolding Ifloat_mult Ifloat_ge0_exp[OF order_refl] nat_0 realpow_0 mult_1_right
+ using lb_ln2[of prec]
+ proof (rule mult_right_mono)
+ have "1 \<le> Float m e" using `1 \<le> x` Float unfolding le_float_def by auto
+ from float_gt1_scale[OF this]
+ show "0 \<le> real (e + (bitlen m - 1))" by auto
+ qed
+ moreover
+ from bitlen_div[OF `0 < m`, unfolded normalized_float[OF `m \<noteq> 0`, symmetric]]
+ have "0 \<le> Ifloat (?x - 1)" and "Ifloat (?x - 1) < 1" by auto
+ from ln_float_bounds(1)[OF this]
+ have "Ifloat ((?x - 1) * lb_ln_horner prec (get_even prec) 1 (?x - 1)) \<le> ln (Ifloat ?x)" (is "?lb_horner \<le> _") by auto
+ ultimately have "?lb2 + ?lb_horner \<le> ln (Ifloat x)"
+ unfolding Float ln_shifted_float[OF `0 < m`, of e] by auto
+ }
+ moreover
+ {
+ from bitlen_div[OF `0 < m`, unfolded normalized_float[OF `m \<noteq> 0`, symmetric]]
+ have "0 \<le> Ifloat (?x - 1)" and "Ifloat (?x - 1) < 1" by auto
+ from ln_float_bounds(2)[OF this]
+ have "ln (Ifloat ?x) \<le> Ifloat ((?x - 1) * ub_ln_horner prec (get_odd prec) 1 (?x - 1))" (is "_ \<le> ?ub_horner") by auto
+ moreover
+ have "ln 2 * real (e + (bitlen m - 1)) \<le> Ifloat (ub_ln2 prec * ?s)" (is "_ \<le> ?ub2")
+ unfolding Ifloat_mult Ifloat_ge0_exp[OF order_refl] nat_0 realpow_0 mult_1_right
+ using ub_ln2[of prec]
+ proof (rule mult_right_mono)
+ have "1 \<le> Float m e" using `1 \<le> x` Float unfolding le_float_def by auto
+ from float_gt1_scale[OF this]
+ show "0 \<le> real (e + (bitlen m - 1))" by auto
+ qed
+ ultimately have "ln (Ifloat x) \<le> ?ub2 + ?ub_horner"
+ unfolding Float ln_shifted_float[OF `0 < m`, of e] by auto
+ }
+ ultimately show ?thesis unfolding lb_ln.simps unfolding ub_ln.simps
+ unfolding if_not_P[OF `\<not> x \<le> 0`] if_not_P[OF `\<not> x < 1`] if_not_P[OF False] Let_def
+ unfolding scale.simps[of m e, unfolded Float[symmetric]] mantissa.simps[of m e, unfolded Float[symmetric]] Ifloat_add by auto
+ qed
+qed
+
+lemma ub_ln_lb_ln_bounds: assumes "0 < x"
+ shows "Ifloat (the (lb_ln prec x)) \<le> ln (Ifloat x) \<and> ln (Ifloat x) \<le> Ifloat (the (ub_ln prec x))"
+ (is "?lb \<le> ?ln \<and> ?ln \<le> ?ub")
+proof (cases "x < 1")
+ case False hence "1 \<le> x" unfolding less_float_def le_float_def by auto
+ show ?thesis using ub_ln_lb_ln_bounds'[OF `1 \<le> x`] .
+next
+ case True have "\<not> x \<le> 0" using `0 < x` unfolding less_float_def le_float_def by auto
+
+ have "0 < Ifloat x" and "Ifloat x \<noteq> 0" using `0 < x` unfolding less_float_def by auto
+ hence A: "0 < 1 / Ifloat x" by auto
+
+ {
+ let ?divl = "float_divl (max prec 1) 1 x"
+ have A': "1 \<le> ?divl" using float_divl_pos_less1_bound[OF `0 < x` `x < 1`] unfolding le_float_def less_float_def by auto
+ hence B: "0 < Ifloat ?divl" unfolding le_float_def by auto
+
+ have "ln (Ifloat ?divl) \<le> ln (1 / Ifloat x)" unfolding ln_le_cancel_iff[OF B A] using float_divl[of _ 1 x] by auto
+ hence "ln (Ifloat x) \<le> - ln (Ifloat ?divl)" unfolding nonzero_inverse_eq_divide[OF `Ifloat x \<noteq> 0`, symmetric] ln_inverse[OF `0 < Ifloat x`] by auto
+ from this ub_ln_lb_ln_bounds'[OF A', THEN conjunct1, THEN le_imp_neg_le]
+ have "?ln \<le> Ifloat (- the (lb_ln prec ?divl))" unfolding Ifloat_minus by (rule order_trans)
+ } moreover
+ {
+ let ?divr = "float_divr prec 1 x"
+ have A': "1 \<le> ?divr" using float_divr_pos_less1_lower_bound[OF `0 < x` `x < 1`] unfolding le_float_def less_float_def by auto
+ hence B: "0 < Ifloat ?divr" unfolding le_float_def by auto
+
+ have "ln (1 / Ifloat x) \<le> ln (Ifloat ?divr)" unfolding ln_le_cancel_iff[OF A B] using float_divr[of 1 x] by auto
+ hence "- ln (Ifloat ?divr) \<le> ln (Ifloat x)" unfolding nonzero_inverse_eq_divide[OF `Ifloat x \<noteq> 0`, symmetric] ln_inverse[OF `0 < Ifloat x`] by auto
+ from ub_ln_lb_ln_bounds'[OF A', THEN conjunct2, THEN le_imp_neg_le] this
+ have "Ifloat (- the (ub_ln prec ?divr)) \<le> ?ln" unfolding Ifloat_minus by (rule order_trans)
+ }
+ ultimately show ?thesis unfolding lb_ln.simps[where x=x] ub_ln.simps[where x=x]
+ unfolding if_not_P[OF `\<not> x \<le> 0`] if_P[OF True] by auto
+qed
+
+lemma lb_ln: assumes "Some y = lb_ln prec x"
+ shows "Ifloat y \<le> ln (Ifloat x)" and "0 < Ifloat x"
+proof -
+ have "0 < x"
+ proof (rule ccontr)
+ assume "\<not> 0 < x" hence "x \<le> 0" unfolding le_float_def less_float_def by auto
+ thus False using assms by auto
+ qed
+ thus "0 < Ifloat x" unfolding less_float_def by auto
+ have "Ifloat (the (lb_ln prec x)) \<le> ln (Ifloat x)" using ub_ln_lb_ln_bounds[OF `0 < x`] ..
+ thus "Ifloat y \<le> ln (Ifloat x)" unfolding assms[symmetric] by auto
+qed
+
+lemma ub_ln: assumes "Some y = ub_ln prec x"
+ shows "ln (Ifloat x) \<le> Ifloat y" and "0 < Ifloat x"
+proof -
+ have "0 < x"
+ proof (rule ccontr)
+ assume "\<not> 0 < x" hence "x \<le> 0" unfolding le_float_def less_float_def by auto
+ thus False using assms by auto
+ qed
+ thus "0 < Ifloat x" unfolding less_float_def by auto
+ have "ln (Ifloat x) \<le> Ifloat (the (ub_ln prec x))" using ub_ln_lb_ln_bounds[OF `0 < x`] ..
+ thus "ln (Ifloat x) \<le> Ifloat y" unfolding assms[symmetric] by auto
+qed
+
+lemma bnds_ln: "\<forall> x lx ux. (Some l, Some u) = (lb_ln prec lx, ub_ln prec ux) \<and> x \<in> {Ifloat lx .. Ifloat ux} \<longrightarrow> Ifloat l \<le> ln x \<and> ln x \<le> Ifloat u"
+proof (rule allI, rule allI, rule allI, rule impI)
+ fix x lx ux
+ assume "(Some l, Some u) = (lb_ln prec lx, ub_ln prec ux) \<and> x \<in> {Ifloat lx .. Ifloat ux}"
+ hence l: "Some l = lb_ln prec lx " and u: "Some u = ub_ln prec ux" and x: "x \<in> {Ifloat lx .. Ifloat ux}" by auto
+
+ have "ln (Ifloat ux) \<le> Ifloat u" and "0 < Ifloat ux" using ub_ln u by auto
+ have "Ifloat l \<le> ln (Ifloat lx)" and "0 < Ifloat lx" and "0 < x" using lb_ln[OF l] x by auto
+
+ from ln_le_cancel_iff[OF `0 < Ifloat lx` `0 < x`] `Ifloat l \<le> ln (Ifloat lx)`
+ have "Ifloat l \<le> ln x" using x unfolding atLeastAtMost_iff by auto
+ moreover
+ from ln_le_cancel_iff[OF `0 < x` `0 < Ifloat ux`] `ln (Ifloat ux) \<le> Ifloat u`
+ have "ln x \<le> Ifloat u" using x unfolding atLeastAtMost_iff by auto
+ ultimately show "Ifloat l \<le> ln x \<and> ln x \<le> Ifloat u" ..
+qed
+
+
+section "Implement floatarith"
+
+subsection "Define syntax and semantics"
+
+datatype floatarith
+ = Add floatarith floatarith
+ | Minus floatarith
+ | Mult floatarith floatarith
+ | Inverse floatarith
+ | Sin floatarith
+ | Cos floatarith
+ | Arctan floatarith
+ | Abs floatarith
+ | Max floatarith floatarith
+ | Min floatarith floatarith
+ | Pi
+ | Sqrt floatarith
+ | Exp floatarith
+ | Ln floatarith
+ | Power floatarith nat
+ | Atom nat
+ | Num float
+
+fun Ifloatarith :: "floatarith \<Rightarrow> real list \<Rightarrow> real"
+where
+"Ifloatarith (Add a b) vs = (Ifloatarith a vs) + (Ifloatarith b vs)" |
+"Ifloatarith (Minus a) vs = - (Ifloatarith a vs)" |
+"Ifloatarith (Mult a b) vs = (Ifloatarith a vs) * (Ifloatarith b vs)" |
+"Ifloatarith (Inverse a) vs = inverse (Ifloatarith a vs)" |
+"Ifloatarith (Sin a) vs = sin (Ifloatarith a vs)" |
+"Ifloatarith (Cos a) vs = cos (Ifloatarith a vs)" |
+"Ifloatarith (Arctan a) vs = arctan (Ifloatarith a vs)" |
+"Ifloatarith (Min a b) vs = min (Ifloatarith a vs) (Ifloatarith b vs)" |
+"Ifloatarith (Max a b) vs = max (Ifloatarith a vs) (Ifloatarith b vs)" |
+"Ifloatarith (Abs a) vs = abs (Ifloatarith a vs)" |
+"Ifloatarith Pi vs = pi" |
+"Ifloatarith (Sqrt a) vs = sqrt (Ifloatarith a vs)" |
+"Ifloatarith (Exp a) vs = exp (Ifloatarith a vs)" |
+"Ifloatarith (Ln a) vs = ln (Ifloatarith a vs)" |
+"Ifloatarith (Power a n) vs = (Ifloatarith a vs)^n" |
+"Ifloatarith (Num f) vs = Ifloat f" |
+"Ifloatarith (Atom n) vs = vs ! n"
+
+subsection "Implement approximation function"
+
+fun lift_bin :: "(float * float) option \<Rightarrow> (float * float) option \<Rightarrow> (float \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float \<Rightarrow> (float option * float option)) \<Rightarrow> (float * float) option" where
+"lift_bin (Some (l1, u1)) (Some (l2, u2)) f = (case (f l1 u1 l2 u2) of (Some l, Some u) \<Rightarrow> Some (l, u)
+ | t \<Rightarrow> None)" |
+"lift_bin a b f = None"
+
+fun lift_bin' :: "(float * float) option \<Rightarrow> (float * float) option \<Rightarrow> (float \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float \<Rightarrow> (float * float)) \<Rightarrow> (float * float) option" where
+"lift_bin' (Some (l1, u1)) (Some (l2, u2)) f = Some (f l1 u1 l2 u2)" |
+"lift_bin' a b f = None"
+
+fun lift_un :: "(float * float) option \<Rightarrow> (float \<Rightarrow> float \<Rightarrow> ((float option) * (float option))) \<Rightarrow> (float * float) option" where
+"lift_un (Some (l1, u1)) f = (case (f l1 u1) of (Some l, Some u) \<Rightarrow> Some (l, u)
+ | t \<Rightarrow> None)" |
+"lift_un b f = None"
+
+fun lift_un' :: "(float * float) option \<Rightarrow> (float \<Rightarrow> float \<Rightarrow> (float * float)) \<Rightarrow> (float * float) option" where
+"lift_un' (Some (l1, u1)) f = Some (f l1 u1)" |
+"lift_un' b f = None"
+
+fun bounded_by :: "real list \<Rightarrow> (float * float) list \<Rightarrow> bool " where
+bounded_by_Cons: "bounded_by (v#vs) ((l, u)#bs) = ((Ifloat l \<le> v \<and> v \<le> Ifloat u) \<and> bounded_by vs bs)" |
+bounded_by_Nil: "bounded_by [] [] = True" |
+"bounded_by _ _ = False"
+
+lemma bounded_by: assumes "bounded_by vs bs" and "i < length bs"
+ shows "Ifloat (fst (bs ! i)) \<le> vs ! i \<and> vs ! i \<le> Ifloat (snd (bs ! i))"
+ using `bounded_by vs bs` and `i < length bs`
+proof (induct arbitrary: i rule: bounded_by.induct)
+ fix v :: real and vs :: "real list" and l u :: float and bs :: "(float * float) list" and i :: nat
+ assume hyp: "\<And>i. \<lbrakk>bounded_by vs bs; i < length bs\<rbrakk> \<Longrightarrow> Ifloat (fst (bs ! i)) \<le> vs ! i \<and> vs ! i \<le> Ifloat (snd (bs ! i))"
+ assume bounded: "bounded_by (v # vs) ((l, u) # bs)" and length: "i < length ((l, u) # bs)"
+ show "Ifloat (fst (((l, u) # bs) ! i)) \<le> (v # vs) ! i \<and> (v # vs) ! i \<le> Ifloat (snd (((l, u) # bs) ! i))"
+ proof (cases i)
+ case 0
+ show ?thesis using bounded unfolding 0 nth_Cons_0 fst_conv snd_conv bounded_by.simps ..
+ next
+ case (Suc i) with length have "i < length bs" by auto
+ show ?thesis unfolding Suc nth_Cons_Suc bounded_by.simps
+ using hyp[OF bounded[unfolded bounded_by.simps, THEN conjunct2] `i < length bs`] .
+ qed
+qed auto
+
+fun approx approx' :: "nat \<Rightarrow> floatarith \<Rightarrow> (float * float) list \<Rightarrow> (float * float) option" where
+"approx' prec a bs = (case (approx prec a bs) of Some (l, u) \<Rightarrow> Some (round_down prec l, round_up prec u) | None \<Rightarrow> None)" |
+"approx prec (Add a b) bs = lift_bin' (approx' prec a bs) (approx' prec b bs) (\<lambda> l1 u1 l2 u2. (l1 + l2, u1 + u2))" |
+"approx prec (Minus a) bs = lift_un' (approx' prec a bs) (\<lambda> l u. (-u, -l))" |
+"approx prec (Mult a b) bs = lift_bin' (approx' prec a bs) (approx' prec b bs)
+ (\<lambda> a1 a2 b1 b2. (float_nprt a1 * float_pprt b2 + float_nprt a2 * float_nprt b2 + float_pprt a1 * float_pprt b1 + float_pprt a2 * float_nprt b1,
+ float_pprt a2 * float_pprt b2 + float_pprt a1 * float_nprt b2 + float_nprt a2 * float_pprt b1 + float_nprt a1 * float_nprt b1))" |
+"approx prec (Inverse a) bs = lift_un (approx' prec a bs) (\<lambda> l u. if (0 < l \<or> u < 0) then (Some (float_divl prec 1 u), Some (float_divr prec 1 l)) else (None, None))" |
+"approx prec (Sin a) bs = lift_un' (approx' prec a bs) (bnds_sin prec)" |
+"approx prec (Cos a) bs = lift_un' (approx' prec a bs) (bnds_cos prec)" |
+"approx prec Pi bs = Some (lb_pi prec, ub_pi prec)" |
+"approx prec (Min a b) bs = lift_bin' (approx' prec a bs) (approx' prec b bs) (\<lambda> l1 u1 l2 u2. (min l1 l2, min u1 u2))" |
+"approx prec (Max a b) bs = lift_bin' (approx' prec a bs) (approx' prec b bs) (\<lambda> l1 u1 l2 u2. (max l1 l2, max u1 u2))" |
+"approx prec (Abs a) bs = lift_un' (approx' prec a bs) (\<lambda>l u. (if l < 0 \<and> 0 < u then 0 else min \<bar>l\<bar> \<bar>u\<bar>, max \<bar>l\<bar> \<bar>u\<bar>))" |
+"approx prec (Arctan a) bs = lift_un' (approx' prec a bs) (\<lambda> l u. (lb_arctan prec l, ub_arctan prec u))" |
+"approx prec (Sqrt a) bs = lift_un (approx' prec a bs) (\<lambda> l u. (lb_sqrt prec l, ub_sqrt prec u))" |
+"approx prec (Exp a) bs = lift_un' (approx' prec a bs) (\<lambda> l u. (lb_exp prec l, ub_exp prec u))" |
+"approx prec (Ln a) bs = lift_un (approx' prec a bs) (\<lambda> l u. (lb_ln prec l, ub_ln prec u))" |
+"approx prec (Power a n) bs = lift_un' (approx' prec a bs) (float_power_bnds n)" |
+"approx prec (Num f) bs = Some (f, f)" |
+"approx prec (Atom i) bs = (if i < length bs then Some (bs ! i) else None)"
+
+lemma lift_bin'_ex:
+ assumes lift_bin'_Some: "Some (l, u) = lift_bin' a b f"
+ shows "\<exists> l1 u1 l2 u2. Some (l1, u1) = a \<and> Some (l2, u2) = b"
+proof (cases a)
+ case None hence "None = lift_bin' a b f" unfolding None lift_bin'.simps ..
+ thus ?thesis using lift_bin'_Some by auto
+next
+ case (Some a')
+ show ?thesis
+ proof (cases b)
+ case None hence "None = lift_bin' a b f" unfolding None lift_bin'.simps ..
+ thus ?thesis using lift_bin'_Some by auto
+ next
+ case (Some b')
+ obtain la ua where a': "a' = (la, ua)" by (cases a', auto)
+ obtain lb ub where b': "b' = (lb, ub)" by (cases b', auto)
+ thus ?thesis unfolding `a = Some a'` `b = Some b'` a' b' by auto
+ qed
+qed
+
+lemma lift_bin'_f:
+ assumes lift_bin'_Some: "Some (l, u) = lift_bin' (g a) (g b) f"
+ and Pa: "\<And>l u. Some (l, u) = g a \<Longrightarrow> P l u a" and Pb: "\<And>l u. Some (l, u) = g b \<Longrightarrow> P l u b"
+ shows "\<exists> l1 u1 l2 u2. P l1 u1 a \<and> P l2 u2 b \<and> l = fst (f l1 u1 l2 u2) \<and> u = snd (f l1 u1 l2 u2)"
+proof -
+ obtain l1 u1 l2 u2
+ where Sa: "Some (l1, u1) = g a" and Sb: "Some (l2, u2) = g b" using lift_bin'_ex[OF assms(1)] by auto
+ have lu: "(l, u) = f l1 u1 l2 u2" using lift_bin'_Some[unfolded Sa[symmetric] Sb[symmetric] lift_bin'.simps] by auto
+ have "l = fst (f l1 u1 l2 u2)" and "u = snd (f l1 u1 l2 u2)" unfolding lu[symmetric] by auto
+ thus ?thesis using Pa[OF Sa] Pb[OF Sb] by auto
+qed
+
+lemma approx_approx':
+ assumes Pa: "\<And>l u. Some (l, u) = approx prec a vs \<Longrightarrow> Ifloat l \<le> Ifloatarith a xs \<and> Ifloatarith a xs \<le> Ifloat u"
+ and approx': "Some (l, u) = approx' prec a vs"
+ shows "Ifloat l \<le> Ifloatarith a xs \<and> Ifloatarith a xs \<le> Ifloat u"
+proof -
+ obtain l' u' where S: "Some (l', u') = approx prec a vs"
+ using approx' unfolding approx'.simps by (cases "approx prec a vs", auto)
+ have l': "l = round_down prec l'" and u': "u = round_up prec u'"
+ using approx' unfolding approx'.simps S[symmetric] by auto
+ show ?thesis unfolding l' u'
+ using order_trans[OF Pa[OF S, THEN conjunct2] round_up[of u']]
+ using order_trans[OF round_down[of _ l'] Pa[OF S, THEN conjunct1]] by auto
+qed
+
+lemma lift_bin':
+ assumes lift_bin'_Some: "Some (l, u) = lift_bin' (approx' prec a bs) (approx' prec b bs) f"
+ and Pa: "\<And>l u. Some (l, u) = approx prec a bs \<Longrightarrow> Ifloat l \<le> Ifloatarith a xs \<and> Ifloatarith a xs \<le> Ifloat u" (is "\<And>l u. _ = ?g a \<Longrightarrow> ?P l u a")
+ and Pb: "\<And>l u. Some (l, u) = approx prec b bs \<Longrightarrow> Ifloat l \<le> Ifloatarith b xs \<and> Ifloatarith b xs \<le> Ifloat u"
+ shows "\<exists> l1 u1 l2 u2. (Ifloat l1 \<le> Ifloatarith a xs \<and> Ifloatarith a xs \<le> Ifloat u1) \<and>
+ (Ifloat l2 \<le> Ifloatarith b xs \<and> Ifloatarith b xs \<le> Ifloat u2) \<and>
+ l = fst (f l1 u1 l2 u2) \<and> u = snd (f l1 u1 l2 u2)"
+proof -
+ { fix l u assume "Some (l, u) = approx' prec a bs"
+ with approx_approx'[of prec a bs, OF _ this] Pa
+ have "Ifloat l \<le> Ifloatarith a xs \<and> Ifloatarith a xs \<le> Ifloat u" by auto } note Pa = this
+ { fix l u assume "Some (l, u) = approx' prec b bs"
+ with approx_approx'[of prec b bs, OF _ this] Pb
+ have "Ifloat l \<le> Ifloatarith b xs \<and> Ifloatarith b xs \<le> Ifloat u" by auto } note Pb = this
+
+ from lift_bin'_f[where g="\<lambda>a. approx' prec a bs" and P = ?P, OF lift_bin'_Some, OF Pa Pb]
+ show ?thesis by auto
+qed
+
+lemma lift_un'_ex:
+ assumes lift_un'_Some: "Some (l, u) = lift_un' a f"
+ shows "\<exists> l u. Some (l, u) = a"
+proof (cases a)
+ case None hence "None = lift_un' a f" unfolding None lift_un'.simps ..
+ thus ?thesis using lift_un'_Some by auto
+next
+ case (Some a')
+ obtain la ua where a': "a' = (la, ua)" by (cases a', auto)
+ thus ?thesis unfolding `a = Some a'` a' by auto
+qed
+
+lemma lift_un'_f:
+ assumes lift_un'_Some: "Some (l, u) = lift_un' (g a) f"
+ and Pa: "\<And>l u. Some (l, u) = g a \<Longrightarrow> P l u a"
+ shows "\<exists> l1 u1. P l1 u1 a \<and> l = fst (f l1 u1) \<and> u = snd (f l1 u1)"
+proof -
+ obtain l1 u1 where Sa: "Some (l1, u1) = g a" using lift_un'_ex[OF assms(1)] by auto
+ have lu: "(l, u) = f l1 u1" using lift_un'_Some[unfolded Sa[symmetric] lift_un'.simps] by auto
+ have "l = fst (f l1 u1)" and "u = snd (f l1 u1)" unfolding lu[symmetric] by auto
+ thus ?thesis using Pa[OF Sa] by auto
+qed
+
+lemma lift_un':
+ assumes lift_un'_Some: "Some (l, u) = lift_un' (approx' prec a bs) f"
+ and Pa: "\<And>l u. Some (l, u) = approx prec a bs \<Longrightarrow> Ifloat l \<le> Ifloatarith a xs \<and> Ifloatarith a xs \<le> Ifloat u" (is "\<And>l u. _ = ?g a \<Longrightarrow> ?P l u a")
+ shows "\<exists> l1 u1. (Ifloat l1 \<le> Ifloatarith a xs \<and> Ifloatarith a xs \<le> Ifloat u1) \<and>
+ l = fst (f l1 u1) \<and> u = snd (f l1 u1)"
+proof -
+ { fix l u assume "Some (l, u) = approx' prec a bs"
+ with approx_approx'[of prec a bs, OF _ this] Pa
+ have "Ifloat l \<le> Ifloatarith a xs \<and> Ifloatarith a xs \<le> Ifloat u" by auto } note Pa = this
+ from lift_un'_f[where g="\<lambda>a. approx' prec a bs" and P = ?P, OF lift_un'_Some, OF Pa]
+ show ?thesis by auto
+qed
+
+lemma lift_un'_bnds:
+ assumes bnds: "\<forall> x lx ux. (l, u) = f lx ux \<and> x \<in> { Ifloat lx .. Ifloat ux } \<longrightarrow> Ifloat l \<le> f' x \<and> f' x \<le> Ifloat u"
+ and lift_un'_Some: "Some (l, u) = lift_un' (approx' prec a bs) f"
+ and Pa: "\<And>l u. Some (l, u) = approx prec a bs \<Longrightarrow> Ifloat l \<le> Ifloatarith a xs \<and> Ifloatarith a xs \<le> Ifloat u"
+ shows "Ifloat l \<le> f' (Ifloatarith a xs) \<and> f' (Ifloatarith a xs) \<le> Ifloat u"
+proof -
+ from lift_un'[OF lift_un'_Some Pa]
+ obtain l1 u1 where "Ifloat l1 \<le> Ifloatarith a xs" and "Ifloatarith a xs \<le> Ifloat u1" and "l = fst (f l1 u1)" and "u = snd (f l1 u1)" by blast
+ hence "(l, u) = f l1 u1" and "Ifloatarith a xs \<in> {Ifloat l1 .. Ifloat u1}" by auto
+ thus ?thesis using bnds by auto
+qed
+
+lemma lift_un_ex:
+ assumes lift_un_Some: "Some (l, u) = lift_un a f"
+ shows "\<exists> l u. Some (l, u) = a"
+proof (cases a)
+ case None hence "None = lift_un a f" unfolding None lift_un.simps ..
+ thus ?thesis using lift_un_Some by auto
+next
+ case (Some a')
+ obtain la ua where a': "a' = (la, ua)" by (cases a', auto)
+ thus ?thesis unfolding `a = Some a'` a' by auto
+qed
+
+lemma lift_un_f:
+ assumes lift_un_Some: "Some (l, u) = lift_un (g a) f"
+ and Pa: "\<And>l u. Some (l, u) = g a \<Longrightarrow> P l u a"
+ shows "\<exists> l1 u1. P l1 u1 a \<and> Some l = fst (f l1 u1) \<and> Some u = snd (f l1 u1)"
+proof -
+ obtain l1 u1 where Sa: "Some (l1, u1) = g a" using lift_un_ex[OF assms(1)] by auto
+ have "fst (f l1 u1) \<noteq> None \<and> snd (f l1 u1) \<noteq> None"
+ proof (rule ccontr)
+ assume "\<not> (fst (f l1 u1) \<noteq> None \<and> snd (f l1 u1) \<noteq> None)"
+ hence or: "fst (f l1 u1) = None \<or> snd (f l1 u1) = None" by auto
+ hence "lift_un (g a) f = None"
+ proof (cases "fst (f l1 u1) = None")
+ case True
+ then obtain b where b: "f l1 u1 = (None, b)" by (cases "f l1 u1", auto)
+ thus ?thesis unfolding Sa[symmetric] lift_un.simps b by auto
+ next
+ case False hence "snd (f l1 u1) = None" using or by auto
+ with False obtain b where b: "f l1 u1 = (Some b, None)" by (cases "f l1 u1", auto)
+ thus ?thesis unfolding Sa[symmetric] lift_un.simps b by auto
+ qed
+ thus False using lift_un_Some by auto
+ qed
+ then obtain a' b' where f: "f l1 u1 = (Some a', Some b')" by (cases "f l1 u1", auto)
+ from lift_un_Some[unfolded Sa[symmetric] lift_un.simps f]
+ have "Some l = fst (f l1 u1)" and "Some u = snd (f l1 u1)" unfolding f by auto
+ thus ?thesis unfolding Sa[symmetric] lift_un.simps using Pa[OF Sa] by auto
+qed
+
+lemma lift_un:
+ assumes lift_un_Some: "Some (l, u) = lift_un (approx' prec a bs) f"
+ and Pa: "\<And>l u. Some (l, u) = approx prec a bs \<Longrightarrow> Ifloat l \<le> Ifloatarith a xs \<and> Ifloatarith a xs \<le> Ifloat u" (is "\<And>l u. _ = ?g a \<Longrightarrow> ?P l u a")
+ shows "\<exists> l1 u1. (Ifloat l1 \<le> Ifloatarith a xs \<and> Ifloatarith a xs \<le> Ifloat u1) \<and>
+ Some l = fst (f l1 u1) \<and> Some u = snd (f l1 u1)"
+proof -
+ { fix l u assume "Some (l, u) = approx' prec a bs"
+ with approx_approx'[of prec a bs, OF _ this] Pa
+ have "Ifloat l \<le> Ifloatarith a xs \<and> Ifloatarith a xs \<le> Ifloat u" by auto } note Pa = this
+ from lift_un_f[where g="\<lambda>a. approx' prec a bs" and P = ?P, OF lift_un_Some, OF Pa]
+ show ?thesis by auto
+qed
+
+lemma lift_un_bnds:
+ assumes bnds: "\<forall> x lx ux. (Some l, Some u) = f lx ux \<and> x \<in> { Ifloat lx .. Ifloat ux } \<longrightarrow> Ifloat l \<le> f' x \<and> f' x \<le> Ifloat u"
+ and lift_un_Some: "Some (l, u) = lift_un (approx' prec a bs) f"
+ and Pa: "\<And>l u. Some (l, u) = approx prec a bs \<Longrightarrow> Ifloat l \<le> Ifloatarith a xs \<and> Ifloatarith a xs \<le> Ifloat u"
+ shows "Ifloat l \<le> f' (Ifloatarith a xs) \<and> f' (Ifloatarith a xs) \<le> Ifloat u"
+proof -
+ from lift_un[OF lift_un_Some Pa]
+ obtain l1 u1 where "Ifloat l1 \<le> Ifloatarith a xs" and "Ifloatarith a xs \<le> Ifloat u1" and "Some l = fst (f l1 u1)" and "Some u = snd (f l1 u1)" by blast
+ hence "(Some l, Some u) = f l1 u1" and "Ifloatarith a xs \<in> {Ifloat l1 .. Ifloat u1}" by auto
+ thus ?thesis using bnds by auto
+qed
+
+lemma approx:
+ assumes "bounded_by xs vs"
+ and "Some (l, u) = approx prec arith vs" (is "_ = ?g arith")
+ shows "Ifloat l \<le> Ifloatarith arith xs \<and> Ifloatarith arith xs \<le> Ifloat u" (is "?P l u arith")
+ using `Some (l, u) = approx prec arith vs`
+proof (induct arith arbitrary: l u x)
+ case (Add a b)
+ from lift_bin'[OF Add.prems[unfolded approx.simps]] Add.hyps
+ obtain l1 u1 l2 u2 where "l = l1 + l2" and "u = u1 + u2"
+ "Ifloat l1 \<le> Ifloatarith a xs" and "Ifloatarith a xs \<le> Ifloat u1"
+ "Ifloat l2 \<le> Ifloatarith b xs" and "Ifloatarith b xs \<le> Ifloat u2" unfolding fst_conv snd_conv by blast
+ thus ?case unfolding Ifloatarith.simps by auto
+next
+ case (Minus a)
+ from lift_un'[OF Minus.prems[unfolded approx.simps]] Minus.hyps
+ obtain l1 u1 where "l = -u1" and "u = -l1"
+ "Ifloat l1 \<le> Ifloatarith a xs" and "Ifloatarith a xs \<le> Ifloat u1" unfolding fst_conv snd_conv by blast
+ thus ?case unfolding Ifloatarith.simps using Ifloat_minus by auto
+next
+ case (Mult a b)
+ from lift_bin'[OF Mult.prems[unfolded approx.simps]] Mult.hyps
+ obtain l1 u1 l2 u2
+ where l: "l = float_nprt l1 * float_pprt u2 + float_nprt u1 * float_nprt u2 + float_pprt l1 * float_pprt l2 + float_pprt u1 * float_nprt l2"
+ and u: "u = float_pprt u1 * float_pprt u2 + float_pprt l1 * float_nprt u2 + float_nprt u1 * float_pprt l2 + float_nprt l1 * float_nprt l2"
+ and "Ifloat l1 \<le> Ifloatarith a xs" and "Ifloatarith a xs \<le> Ifloat u1"
+ and "Ifloat l2 \<le> Ifloatarith b xs" and "Ifloatarith b xs \<le> Ifloat u2" unfolding fst_conv snd_conv by blast
+ thus ?case unfolding Ifloatarith.simps l u Ifloat_add Ifloat_mult Ifloat_nprt Ifloat_pprt
+ using mult_le_prts mult_ge_prts by auto
+next
+ case (Inverse a)
+ from lift_un[OF Inverse.prems[unfolded approx.simps], unfolded if_distrib[of fst] if_distrib[of snd] fst_conv snd_conv] Inverse.hyps
+ obtain l1 u1 where l': "Some l = (if 0 < l1 \<or> u1 < 0 then Some (float_divl prec 1 u1) else None)"
+ and u': "Some u = (if 0 < l1 \<or> u1 < 0 then Some (float_divr prec 1 l1) else None)"
+ and l1: "Ifloat l1 \<le> Ifloatarith a xs" and u1: "Ifloatarith a xs \<le> Ifloat u1" by blast
+ have either: "0 < l1 \<or> u1 < 0" proof (rule ccontr) assume P: "\<not> (0 < l1 \<or> u1 < 0)" show False using l' unfolding if_not_P[OF P] by auto qed
+ moreover have l1_le_u1: "Ifloat l1 \<le> Ifloat u1" using l1 u1 by auto
+ ultimately have "Ifloat l1 \<noteq> 0" and "Ifloat u1 \<noteq> 0" unfolding less_float_def by auto
+
+ have inv: "inverse (Ifloat u1) \<le> inverse (Ifloatarith a xs)
+ \<and> inverse (Ifloatarith a xs) \<le> inverse (Ifloat l1)"
+ proof (cases "0 < l1")
+ case True hence "0 < Ifloat u1" and "0 < Ifloat l1" "0 < Ifloatarith a xs"
+ unfolding less_float_def using l1_le_u1 l1 by auto
+ show ?thesis
+ unfolding inverse_le_iff_le[OF `0 < Ifloat u1` `0 < Ifloatarith a xs`]
+ inverse_le_iff_le[OF `0 < Ifloatarith a xs` `0 < Ifloat l1`]
+ using l1 u1 by auto
+ next
+ case False hence "u1 < 0" using either by blast
+ hence "Ifloat u1 < 0" and "Ifloat l1 < 0" "Ifloatarith a xs < 0"
+ unfolding less_float_def using l1_le_u1 u1 by auto
+ show ?thesis
+ unfolding inverse_le_iff_le_neg[OF `Ifloat u1 < 0` `Ifloatarith a xs < 0`]
+ inverse_le_iff_le_neg[OF `Ifloatarith a xs < 0` `Ifloat l1 < 0`]
+ using l1 u1 by auto
+ qed
+
+ from l' have "l = float_divl prec 1 u1" by (cases "0 < l1 \<or> u1 < 0", auto)
+ hence "Ifloat l \<le> inverse (Ifloat u1)" unfolding nonzero_inverse_eq_divide[OF `Ifloat u1 \<noteq> 0`] using float_divl[of prec 1 u1] by auto
+ also have "\<dots> \<le> inverse (Ifloatarith a xs)" using inv by auto
+ finally have "Ifloat l \<le> inverse (Ifloatarith a xs)" .
+ moreover
+ from u' have "u = float_divr prec 1 l1" by (cases "0 < l1 \<or> u1 < 0", auto)
+ hence "inverse (Ifloat l1) \<le> Ifloat u" unfolding nonzero_inverse_eq_divide[OF `Ifloat l1 \<noteq> 0`] using float_divr[of 1 l1 prec] by auto
+ hence "inverse (Ifloatarith a xs) \<le> Ifloat u" by (rule order_trans[OF inv[THEN conjunct2]])
+ ultimately show ?case unfolding Ifloatarith.simps using l1 u1 by auto
+next
+ case (Abs x)
+ from lift_un'[OF Abs.prems[unfolded approx.simps], unfolded fst_conv snd_conv] Abs.hyps
+ obtain l1 u1 where l': "l = (if l1 < 0 \<and> 0 < u1 then 0 else min \<bar>l1\<bar> \<bar>u1\<bar>)" and u': "u = max \<bar>l1\<bar> \<bar>u1\<bar>"
+ and l1: "Ifloat l1 \<le> Ifloatarith x xs" and u1: "Ifloatarith x xs \<le> Ifloat u1" by blast
+ thus ?case unfolding l' u' by (cases "l1 < 0 \<and> 0 < u1", auto simp add: Ifloat_min Ifloat_max Ifloat_abs less_float_def)
+next
+ case (Min a b)
+ from lift_bin'[OF Min.prems[unfolded approx.simps], unfolded fst_conv snd_conv] Min.hyps
+ obtain l1 u1 l2 u2 where l': "l = min l1 l2" and u': "u = min u1 u2"
+ and l1: "Ifloat l1 \<le> Ifloatarith a xs" and u1: "Ifloatarith a xs \<le> Ifloat u1"
+ and l1: "Ifloat l2 \<le> Ifloatarith b xs" and u1: "Ifloatarith b xs \<le> Ifloat u2" by blast
+ thus ?case unfolding l' u' by (auto simp add: Ifloat_min)
+next
+ case (Max a b)
+ from lift_bin'[OF Max.prems[unfolded approx.simps], unfolded fst_conv snd_conv] Max.hyps
+ obtain l1 u1 l2 u2 where l': "l = max l1 l2" and u': "u = max u1 u2"
+ and l1: "Ifloat l1 \<le> Ifloatarith a xs" and u1: "Ifloatarith a xs \<le> Ifloat u1"
+ and l1: "Ifloat l2 \<le> Ifloatarith b xs" and u1: "Ifloatarith b xs \<le> Ifloat u2" by blast
+ thus ?case unfolding l' u' by (auto simp add: Ifloat_max)
+next case (Sin a) with lift_un'_bnds[OF bnds_sin] show ?case by auto
+next case (Cos a) with lift_un'_bnds[OF bnds_cos] show ?case by auto
+next case (Arctan a) with lift_un'_bnds[OF bnds_arctan] show ?case by auto
+next case Pi with pi_boundaries show ?case by auto
+next case (Sqrt a) with lift_un_bnds[OF bnds_sqrt] show ?case by auto
+next case (Exp a) with lift_un'_bnds[OF bnds_exp] show ?case by auto
+next case (Ln a) with lift_un_bnds[OF bnds_ln] show ?case by auto
+next case (Power a n) with lift_un'_bnds[OF bnds_power] show ?case by auto
+next case (Num f) thus ?case by auto
+next
+ case (Atom n)
+ show ?case
+ proof (cases "n < length vs")
+ case True
+ with Atom have "vs ! n = (l, u)" by auto
+ thus ?thesis using bounded_by[OF assms(1) True] by auto
+ next
+ case False thus ?thesis using Atom by auto
+ qed
+qed
+
+datatype ApproxEq = Less floatarith floatarith
+ | LessEqual floatarith floatarith
+
+fun uneq :: "ApproxEq \<Rightarrow> real list \<Rightarrow> bool" where
+"uneq (Less a b) vs = (Ifloatarith a vs < Ifloatarith b vs)" |
+"uneq (LessEqual a b) vs = (Ifloatarith a vs \<le> Ifloatarith b vs)"
+
+fun uneq' :: "nat \<Rightarrow> ApproxEq \<Rightarrow> (float * float) list \<Rightarrow> bool" where
+"uneq' prec (Less a b) bs = (case (approx prec a bs, approx prec b bs) of (Some (l, u), Some (l', u')) \<Rightarrow> u < l' | _ \<Rightarrow> False)" |
+"uneq' prec (LessEqual a b) bs = (case (approx prec a bs, approx prec b bs) of (Some (l, u), Some (l', u')) \<Rightarrow> u \<le> l' | _ \<Rightarrow> False)"
+
+lemma uneq_approx: fixes m :: nat assumes "bounded_by vs bs" and "uneq' prec eq bs"
+ shows "uneq eq vs"
+proof (cases eq)
+ case (Less a b)
+ show ?thesis
+ proof (cases "\<exists> u l u' l'. approx prec a bs = Some (l, u) \<and>
+ approx prec b bs = Some (l', u')")
+ case True
+ then obtain l u l' u' where a_approx: "approx prec a bs = Some (l, u)"
+ and b_approx: "approx prec b bs = Some (l', u') " by auto
+ with `uneq' prec eq bs` have "Ifloat u < Ifloat l'"
+ unfolding Less uneq'.simps less_float_def by auto
+ moreover from a_approx[symmetric] and b_approx[symmetric] and `bounded_by vs bs`
+ have "Ifloatarith a vs \<le> Ifloat u" and "Ifloat l' \<le> Ifloatarith b vs"
+ using approx by auto
+ ultimately show ?thesis unfolding uneq.simps Less by auto
+ next
+ case False
+ hence "approx prec a bs = None \<or> approx prec b bs = None"
+ unfolding not_Some_eq[symmetric] by auto
+ hence "\<not> uneq' prec eq bs" unfolding Less uneq'.simps
+ by (cases "approx prec a bs = None", auto)
+ thus ?thesis using assms by auto
+ qed
+next
+ case (LessEqual a b)
+ show ?thesis
+ proof (cases "\<exists> u l u' l'. approx prec a bs = Some (l, u) \<and>
+ approx prec b bs = Some (l', u')")
+ case True
+ then obtain l u l' u' where a_approx: "approx prec a bs = Some (l, u)"
+ and b_approx: "approx prec b bs = Some (l', u') " by auto
+ with `uneq' prec eq bs` have "Ifloat u \<le> Ifloat l'"
+ unfolding LessEqual uneq'.simps le_float_def by auto
+ moreover from a_approx[symmetric] and b_approx[symmetric] and `bounded_by vs bs`
+ have "Ifloatarith a vs \<le> Ifloat u" and "Ifloat l' \<le> Ifloatarith b vs"
+ using approx by auto
+ ultimately show ?thesis unfolding uneq.simps LessEqual by auto
+ next
+ case False
+ hence "approx prec a bs = None \<or> approx prec b bs = None"
+ unfolding not_Some_eq[symmetric] by auto
+ hence "\<not> uneq' prec eq bs" unfolding LessEqual uneq'.simps
+ by (cases "approx prec a bs = None", auto)
+ thus ?thesis using assms by auto
+ qed
+qed
+
+lemma Ifloatarith_divide: "Ifloatarith (Mult a (Inverse b)) vs = (Ifloatarith a vs) / (Ifloatarith b vs)"
+ unfolding real_divide_def Ifloatarith.simps ..
+
+lemma Ifloatarith_diff: "Ifloatarith (Add a (Minus b)) vs = (Ifloatarith a vs) - (Ifloatarith b vs)"
+ unfolding real_diff_def Ifloatarith.simps ..
+
+lemma Ifloatarith_tan: "Ifloatarith (Mult (Sin a) (Inverse (Cos a))) vs = tan (Ifloatarith a vs)"
+ unfolding tan_def Ifloatarith.simps real_divide_def ..
+
+lemma Ifloatarith_powr: "Ifloatarith (Exp (Mult b (Ln a))) vs = (Ifloatarith a vs) powr (Ifloatarith b vs)"
+ unfolding powr_def Ifloatarith.simps ..
+
+lemma Ifloatarith_log: "Ifloatarith ((Mult (Ln x) (Inverse (Ln b)))) vs = log (Ifloatarith b vs) (Ifloatarith x vs)"
+ unfolding log_def Ifloatarith.simps real_divide_def ..
+
+lemma Ifloatarith_num: shows "Ifloatarith (Num (Float 0 0)) vs = 0" and "Ifloatarith (Num (Float 1 0)) vs = 1" and "Ifloatarith (Num (Float (number_of a) 0)) vs = number_of a" by auto
+
+subsection {* Implement proof method \texttt{approximation} *}
+
+lemma bounded_divl: assumes "Ifloat a / Ifloat b \<le> x" shows "Ifloat (float_divl p a b) \<le> x" by (rule order_trans[OF _ assms], rule float_divl)
+lemma bounded_divr: assumes "x \<le> Ifloat a / Ifloat b" shows "x \<le> Ifloat (float_divr p a b)" by (rule order_trans[OF assms _], rule float_divr)
+lemma bounded_num: shows "Ifloat (Float 5 1) = 10" and "Ifloat (Float 0 0) = 0" and "Ifloat (Float 1 0) = 1" and "Ifloat (Float (number_of n) 0) = (number_of n)"
+ and "0 * pow2 e = Ifloat (Float 0 e)" and "1 * pow2 e = Ifloat (Float 1 e)" and "number_of m * pow2 e = Ifloat (Float (number_of m) e)"
+ by (auto simp add: Ifloat.simps pow2_def)
+
+lemmas bounded_by_equations = bounded_by_Cons bounded_by_Nil float_power bounded_divl bounded_divr bounded_num HOL.simp_thms
+lemmas uneq_equations = uneq.simps Ifloatarith.simps Ifloatarith_num Ifloatarith_divide Ifloatarith_diff Ifloatarith_tan Ifloatarith_powr Ifloatarith_log
+
+lemma "x div (0::int) = 0" by auto -- "What happens in the zero case for div"
+lemma "x mod (0::int) = x" by auto -- "What happens in the zero case for mod"
+
+text {* The following equations must hold for div & mod
+ -- see "The Definition of Standard ML" by R. Milner, M. Tofte and R. Harper (pg. 79) *}
+lemma "d * (i div d) + i mod d = (i::int)" by auto
+lemma "0 < (d :: int) \<Longrightarrow> 0 \<le> i mod d \<and> i mod d < d" by auto
+lemma "(d :: int) < 0 \<Longrightarrow> d < i mod d \<and> i mod d \<le> 0" by auto
+
+code_const "op div :: int \<Rightarrow> int \<Rightarrow> int" (SML "(fn i => fn d => if d = 0 then 0 else i div d)")
+code_const "op mod :: int \<Rightarrow> int \<Rightarrow> int" (SML "(fn i => fn d => if d = 0 then i else i mod d)")
+code_const "divmod :: int \<Rightarrow> int \<Rightarrow> (int * int)" (SML "(fn i => fn d => if d = 0 then (0, i) else IntInf.divMod (i, d))")
+
+ML {*
+ val uneq_equations = PureThy.get_thms @{theory} "uneq_equations";
+ val bounded_by_equations = PureThy.get_thms @{theory} "bounded_by_equations";
+ val bounded_by_simpset = (HOL_basic_ss addsimps bounded_by_equations)
+
+ fun reify_uneq ctxt i = (fn st =>
+ let
+ val to = HOLogic.dest_Trueprop (Logic.strip_imp_concl (List.nth (prems_of st, i - 1)))
+ in (Reflection.genreify_tac ctxt uneq_equations (SOME to) i) st
+ end)
+
+ fun rule_uneq ctxt prec i thm = let
+ fun conv_num typ = HOLogic.dest_number #> snd #> HOLogic.mk_number typ
+ val to_natc = conv_num @{typ "nat"} #> Thm.cterm_of (ProofContext.theory_of ctxt)
+ val to_nat = conv_num @{typ "nat"}
+ val to_int = conv_num @{typ "int"}
+
+ val prec' = to_nat prec
+
+ fun bot_float (Const (@{const_name "times"}, _) $ mantisse $ (Const (@{const_name "pow2"}, _) $ exp))
+ = @{term "Float"} $ to_int mantisse $ to_int exp
+ | bot_float (Const (@{const_name "divide"}, _) $ mantisse $ (Const (@{const_name "power"}, _) $ ten $ exp))
+ = @{term "float_divl"} $ prec' $ (@{term "Float"} $ to_int mantisse $ @{term "0 :: int"}) $ (@{term "power_float (Float 5 1)"} $ to_nat exp)
+ | bot_float (Const (@{const_name "divide"}, _) $ mantisse $ ten)
+ = @{term "float_divl"} $ prec' $ (@{term "Float"} $ to_int mantisse $ @{term "0 :: int"}) $ @{term "Float 5 1"}
+ | bot_float mantisse = @{term "Float"} $ to_int mantisse $ @{term "0 :: int"}
+
+ fun top_float (Const (@{const_name "times"}, _) $ mantisse $ (Const (@{const_name "pow2"}, _) $ exp))
+ = @{term "Float"} $ to_int mantisse $ to_int exp
+ | top_float (Const (@{const_name "divide"}, _) $ mantisse $ (Const (@{const_name "power"}, _) $ ten $ exp))
+ = @{term "float_divr"} $ prec' $ (@{term "Float"} $ to_int mantisse $ @{term "0 :: int"}) $ (@{term "power_float (Float 5 1)"} $ to_nat exp)
+ | top_float (Const (@{const_name "divide"}, _) $ mantisse $ ten)
+ = @{term "float_divr"} $ prec' $ (@{term "Float"} $ to_int mantisse $ @{term "0 :: int"}) $ @{term "Float 5 1"}
+ | top_float mantisse = @{term "Float"} $ to_int mantisse $ @{term "0 :: int"}
+
+ val goal' : term = List.nth (prems_of thm, i - 1)
+
+ fun lift_bnd (t as (Const (@{const_name "op &"}, _) $
+ (Const (@{const_name "less_eq"}, _) $
+ bottom $ (Free (name, _))) $
+ (Const (@{const_name "less_eq"}, _) $ _ $ top)))
+ = ((name, HOLogic.mk_prod (bot_float bottom, top_float top))
+ handle TERM (txt, ts) => raise TERM ("Premisse needs format '<num> <= <var> & <var> <= <num>', but found " ^
+ (Syntax.string_of_term ctxt t), [t]))
+ | lift_bnd t = raise TERM ("Premisse needs format '<num> <= <var> & <var> <= <num>', but found " ^
+ (Syntax.string_of_term ctxt t), [t])
+ val bound_eqs = map (HOLogic.dest_Trueprop #> lift_bnd) (Logic.strip_imp_prems goal')
+
+ fun lift_var (Free (varname, _)) = (case AList.lookup (op =) bound_eqs varname of
+ SOME bound => bound
+ | NONE => raise TERM ("No bound equations found for " ^ varname, []))
+ | lift_var t = raise TERM ("Can not convert expression " ^
+ (Syntax.string_of_term ctxt t), [t])
+
+ val _ $ vs = HOLogic.dest_Trueprop (Logic.strip_imp_concl goal')
+
+ val bs = (HOLogic.dest_list #> map lift_var #> HOLogic.mk_list @{typ "float * float"}) vs
+ val map = [(@{cpat "?prec::nat"}, to_natc prec),
+ (@{cpat "?bs::(float * float) list"}, Thm.cterm_of (ProofContext.theory_of ctxt) bs)]
+ in rtac (Thm.instantiate ([], map) @{thm "uneq_approx"}) i thm end
+
+ val eval_tac = CSUBGOAL (fn (ct, i) => rtac (eval_oracle ct) i)
+
+ fun gen_eval_tac conv ctxt = CONVERSION (Conv.params_conv (~1) (K (Conv.concl_conv (~1) conv)) ctxt)
+ THEN' rtac TrueI
+
+*}
+
+method_setup approximation = {* fn src =>
+ Method.syntax Args.term src #>
+ (fn (prec, ctxt) => let
+ in Method.SIMPLE_METHOD' (fn i =>
+ (DETERM (reify_uneq ctxt i)
+ THEN rule_uneq ctxt prec i
+ THEN Simplifier.asm_full_simp_tac bounded_by_simpset i
+ THEN (TRY (filter_prems_tac (fn t => false) i))
+ THEN (gen_eval_tac eval_oracle ctxt) i))
+ end)
+*} "real number approximation"
+
+end
--- a/src/HOL/Reflection/Ferrack.thy Fri Feb 06 15:57:47 2009 +0100
+++ b/src/HOL/Reflection/Ferrack.thy Fri Feb 06 15:59:49 2009 +0100
@@ -3,7 +3,7 @@
*)
theory Ferrack
-imports Complex_Main Efficient_Nat
+imports Complex_Main Dense_Linear_Order Efficient_Nat
uses ("ferrack_tac.ML")
begin
--- a/src/HOL/Reflection/MIR.thy Fri Feb 06 15:57:47 2009 +0100
+++ b/src/HOL/Reflection/MIR.thy Fri Feb 06 15:59:49 2009 +0100
@@ -3,7 +3,7 @@
*)
theory MIR
-imports Complex_Main Efficient_Nat
+imports Complex_Main Dense_Linear_Order Efficient_Nat
uses ("mir_tac.ML")
begin
--- a/src/HOL/Reflection/ROOT.ML Fri Feb 06 15:57:47 2009 +0100
+++ b/src/HOL/Reflection/ROOT.ML Fri Feb 06 15:59:49 2009 +0100
@@ -1,2 +1,2 @@
-use_thys ["Cooper", "Ferrack", "MIR"];
\ No newline at end of file
+use_thys ["Cooper", "Ferrack", "MIR", "Approximation"];
--- a/src/HOL/SEQ.thy Fri Feb 06 15:57:47 2009 +0100
+++ b/src/HOL/SEQ.thy Fri Feb 06 15:59:49 2009 +0100
@@ -346,6 +346,11 @@
lemma LIMSEQ_Suc_iff: "(\<lambda>n. f (Suc n)) ----> l = f ----> l"
by (blast intro: LIMSEQ_imp_Suc LIMSEQ_Suc)
+lemma LIMSEQ_linear: "\<lbrakk> X ----> x ; l > 0 \<rbrakk> \<Longrightarrow> (\<lambda> n. X (n * l)) ----> x"
+ unfolding LIMSEQ_def
+ by (metis div_le_dividend div_mult_self1_is_m le_trans nat_mult_commute)
+
+
lemma add_diff_add:
fixes a b c d :: "'a::ab_group_add"
shows "(a + c) - (b + d) = (a - b) + (c - d)"
@@ -678,6 +683,69 @@
lemma mono_SucI2: "\<forall>n. X (Suc n) \<le> X n ==> monoseq X"
by (simp add: monoseq_Suc)
+lemma monoseq_minus: assumes "monoseq a"
+ shows "monoseq (\<lambda> n. - a n)"
+proof (cases "\<forall> m. \<forall> n \<ge> m. a m \<le> a n")
+ case True
+ hence "\<forall> m. \<forall> n \<ge> m. - a n \<le> - a m" by auto
+ thus ?thesis by (rule monoI2)
+next
+ case False
+ hence "\<forall> m. \<forall> n \<ge> m. - a m \<le> - a n" using `monoseq a`[unfolded monoseq_def] by auto
+ thus ?thesis by (rule monoI1)
+qed
+
+lemma monoseq_le: assumes "monoseq a" and "a ----> x"
+ shows "((\<forall> n. a n \<le> x) \<and> (\<forall>m. \<forall>n\<ge>m. a m \<le> a n)) \<or>
+ ((\<forall> n. x \<le> a n) \<and> (\<forall>m. \<forall>n\<ge>m. a n \<le> a m))"
+proof -
+ { fix x n fix a :: "nat \<Rightarrow> real"
+ assume "a ----> x" and "\<forall> m. \<forall> n \<ge> m. a m \<le> a n"
+ hence monotone: "\<And> m n. m \<le> n \<Longrightarrow> a m \<le> a n" by auto
+ have "a n \<le> x"
+ proof (rule ccontr)
+ assume "\<not> a n \<le> x" hence "x < a n" by auto
+ hence "0 < a n - x" by auto
+ from `a ----> x`[THEN LIMSEQ_D, OF this]
+ obtain no where "\<And>n'. no \<le> n' \<Longrightarrow> norm (a n' - x) < a n - x" by blast
+ hence "norm (a (max no n) - x) < a n - x" by auto
+ moreover
+ { fix n' have "n \<le> n' \<Longrightarrow> x < a n'" using monotone[where m=n and n=n'] and `x < a n` by auto }
+ hence "x < a (max no n)" by auto
+ ultimately
+ have "a (max no n) < a n" by auto
+ with monotone[where m=n and n="max no n"]
+ show False by auto
+ qed
+ } note top_down = this
+ { fix x n m fix a :: "nat \<Rightarrow> real"
+ assume "a ----> x" and "monoseq a" and "a m < x"
+ have "a n \<le> x \<and> (\<forall> m. \<forall> n \<ge> m. a m \<le> a n)"
+ proof (cases "\<forall> m. \<forall> n \<ge> m. a m \<le> a n")
+ case True with top_down and `a ----> x` show ?thesis by auto
+ next
+ case False with `monoseq a`[unfolded monoseq_def] have "\<forall> m. \<forall> n \<ge> m. - a m \<le> - a n" by auto
+ hence "- a m \<le> - x" using top_down[OF LIMSEQ_minus[OF `a ----> x`]] by blast
+ hence False using `a m < x` by auto
+ thus ?thesis ..
+ qed
+ } note when_decided = this
+
+ show ?thesis
+ proof (cases "\<exists> m. a m \<noteq> x")
+ case True then obtain m where "a m \<noteq> x" by auto
+ show ?thesis
+ proof (cases "a m < x")
+ case True with when_decided[OF `a ----> x` `monoseq a`, where m2=m]
+ show ?thesis by blast
+ next
+ case False hence "- a m < - x" using `a m \<noteq> x` by auto
+ with when_decided[OF LIMSEQ_minus[OF `a ----> x`] monoseq_minus[OF `monoseq a`], where m2=m]
+ show ?thesis by auto
+ qed
+ qed auto
+qed
+
text{*Bounded Sequence*}
lemma BseqD: "Bseq X ==> \<exists>K. 0 < K & (\<forall>n. norm (X n) \<le> K)"
--- a/src/HOL/Series.thy Fri Feb 06 15:57:47 2009 +0100
+++ b/src/HOL/Series.thy Fri Feb 06 15:59:49 2009 +0100
@@ -140,6 +140,24 @@
suminf f = (SUM i = 0..< k. f i) + suminf (%n. f(n + k))"
by (auto simp add: suminf_minus_initial_segment)
+lemma suminf_exist_split: fixes r :: real assumes "0 < r" and "summable a"
+ shows "\<exists> N. \<forall> n \<ge> N. \<bar> \<Sum> i. a (i + n) \<bar> < r"
+proof -
+ from LIMSEQ_D[OF summable_sumr_LIMSEQ_suminf[OF `summable a`] `0 < r`]
+ obtain N :: nat where "\<forall> n \<ge> N. norm (setsum a {0..<n} - suminf a) < r" by auto
+ thus ?thesis unfolding suminf_minus_initial_segment[OF `summable a` refl] abs_minus_commute real_norm_def
+ by auto
+qed
+
+lemma sums_Suc: assumes sumSuc: "(\<lambda> n. f (Suc n)) sums l" shows "f sums (l + f 0)"
+proof -
+ from sumSuc[unfolded sums_def]
+ have "(\<lambda>i. \<Sum>n = Suc 0..<Suc i. f n) ----> l" unfolding setsum_reindex[OF inj_Suc] image_Suc_atLeastLessThan[symmetric] comp_def .
+ from LIMSEQ_add_const[OF this, where b="f 0"]
+ have "(\<lambda>i. \<Sum>n = 0..<Suc i. f n) ----> l + f 0" unfolding add_commute setsum_head_upt_Suc[OF zero_less_Suc] .
+ thus ?thesis unfolding sums_def by (rule LIMSEQ_imp_Suc)
+qed
+
lemma series_zero:
"(\<forall>m. n \<le> m --> f(m) = 0) ==> f sums (setsum f {0..<n})"
apply (simp add: sums_def LIMSEQ_def diff_minus[symmetric], safe)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Tools/ComputeFloat.thy Fri Feb 06 15:59:49 2009 +0100
@@ -0,0 +1,568 @@
+(* Title: HOL/Tools/ComputeFloat.thy
+ Author: Steven Obua
+*)
+
+header {* Floating Point Representation of the Reals *}
+
+theory ComputeFloat
+imports Complex_Main
+uses "~~/src/Tools/float.ML" ("~~/src/HOL/Tools/float_arith.ML")
+begin
+
+definition
+ pow2 :: "int \<Rightarrow> real" where
+ "pow2 a = (if (0 <= a) then (2^(nat a)) else (inverse (2^(nat (-a)))))"
+
+definition
+ float :: "int * int \<Rightarrow> real" where
+ "float x = real (fst x) * pow2 (snd x)"
+
+lemma pow2_0[simp]: "pow2 0 = 1"
+by (simp add: pow2_def)
+
+lemma pow2_1[simp]: "pow2 1 = 2"
+by (simp add: pow2_def)
+
+lemma pow2_neg: "pow2 x = inverse (pow2 (-x))"
+by (simp add: pow2_def)
+
+lemma pow2_add1: "pow2 (1 + a) = 2 * (pow2 a)"
+proof -
+ have h: "! n. nat (2 + int n) - Suc 0 = nat (1 + int n)" by arith
+ have g: "! a b. a - -1 = a + (1::int)" by arith
+ have pos: "! n. pow2 (int n + 1) = 2 * pow2 (int n)"
+ apply (auto, induct_tac n)
+ apply (simp_all add: pow2_def)
+ apply (rule_tac m1="2" and n1="nat (2 + int na)" in ssubst[OF realpow_num_eq_if])
+ by (auto simp add: h)
+ show ?thesis
+ proof (induct a)
+ case (1 n)
+ from pos show ?case by (simp add: algebra_simps)
+ next
+ case (2 n)
+ show ?case
+ apply (auto)
+ apply (subst pow2_neg[of "- int n"])
+ apply (subst pow2_neg[of "-1 - int n"])
+ apply (auto simp add: g pos)
+ done
+ qed
+qed
+
+lemma pow2_add: "pow2 (a+b) = (pow2 a) * (pow2 b)"
+proof (induct b)
+ case (1 n)
+ show ?case
+ proof (induct n)
+ case 0
+ show ?case by simp
+ next
+ case (Suc m)
+ show ?case by (auto simp add: algebra_simps pow2_add1 prems)
+ qed
+next
+ case (2 n)
+ show ?case
+ proof (induct n)
+ case 0
+ show ?case
+ apply (auto)
+ apply (subst pow2_neg[of "a + -1"])
+ apply (subst pow2_neg[of "-1"])
+ apply (simp)
+ apply (insert pow2_add1[of "-a"])
+ apply (simp add: algebra_simps)
+ apply (subst pow2_neg[of "-a"])
+ apply (simp)
+ done
+ case (Suc m)
+ have a: "int m - (a + -2) = 1 + (int m - a + 1)" by arith
+ have b: "int m - -2 = 1 + (int m + 1)" by arith
+ show ?case
+ apply (auto)
+ apply (subst pow2_neg[of "a + (-2 - int m)"])
+ apply (subst pow2_neg[of "-2 - int m"])
+ apply (auto simp add: algebra_simps)
+ apply (subst a)
+ apply (subst b)
+ apply (simp only: pow2_add1)
+ apply (subst pow2_neg[of "int m - a + 1"])
+ apply (subst pow2_neg[of "int m + 1"])
+ apply auto
+ apply (insert prems)
+ apply (auto simp add: algebra_simps)
+ done
+ qed
+qed
+
+lemma "float (a, e) + float (b, e) = float (a + b, e)"
+by (simp add: float_def algebra_simps)
+
+definition
+ int_of_real :: "real \<Rightarrow> int" where
+ "int_of_real x = (SOME y. real y = x)"
+
+definition
+ real_is_int :: "real \<Rightarrow> bool" where
+ "real_is_int x = (EX (u::int). x = real u)"
+
+lemma real_is_int_def2: "real_is_int x = (x = real (int_of_real x))"
+by (auto simp add: real_is_int_def int_of_real_def)
+
+lemma float_transfer: "real_is_int ((real a)*(pow2 c)) \<Longrightarrow> float (a, b) = float (int_of_real ((real a)*(pow2 c)), b - c)"
+by (simp add: float_def real_is_int_def2 pow2_add[symmetric])
+
+lemma pow2_int: "pow2 (int c) = 2^c"
+by (simp add: pow2_def)
+
+lemma float_transfer_nat: "float (a, b) = float (a * 2^c, b - int c)"
+by (simp add: float_def pow2_int[symmetric] pow2_add[symmetric])
+
+lemma real_is_int_real[simp]: "real_is_int (real (x::int))"
+by (auto simp add: real_is_int_def int_of_real_def)
+
+lemma int_of_real_real[simp]: "int_of_real (real x) = x"
+by (simp add: int_of_real_def)
+
+lemma real_int_of_real[simp]: "real_is_int x \<Longrightarrow> real (int_of_real x) = x"
+by (auto simp add: int_of_real_def real_is_int_def)
+
+lemma real_is_int_add_int_of_real: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> (int_of_real (a+b)) = (int_of_real a) + (int_of_real b)"
+by (auto simp add: int_of_real_def real_is_int_def)
+
+lemma real_is_int_add[simp]: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> real_is_int (a+b)"
+apply (subst real_is_int_def2)
+apply (simp add: real_is_int_add_int_of_real real_int_of_real)
+done
+
+lemma int_of_real_sub: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> (int_of_real (a-b)) = (int_of_real a) - (int_of_real b)"
+by (auto simp add: int_of_real_def real_is_int_def)
+
+lemma real_is_int_sub[simp]: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> real_is_int (a-b)"
+apply (subst real_is_int_def2)
+apply (simp add: int_of_real_sub real_int_of_real)
+done
+
+lemma real_is_int_rep: "real_is_int x \<Longrightarrow> ?! (a::int). real a = x"
+by (auto simp add: real_is_int_def)
+
+lemma int_of_real_mult:
+ assumes "real_is_int a" "real_is_int b"
+ shows "(int_of_real (a*b)) = (int_of_real a) * (int_of_real b)"
+proof -
+ from prems have a: "?! (a'::int). real a' = a" by (rule_tac real_is_int_rep, auto)
+ from prems have b: "?! (b'::int). real b' = b" by (rule_tac real_is_int_rep, auto)
+ from a obtain a'::int where a':"a = real a'" by auto
+ from b obtain b'::int where b':"b = real b'" by auto
+ have r: "real a' * real b' = real (a' * b')" by auto
+ show ?thesis
+ apply (simp add: a' b')
+ apply (subst r)
+ apply (simp only: int_of_real_real)
+ done
+qed
+
+lemma real_is_int_mult[simp]: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> real_is_int (a*b)"
+apply (subst real_is_int_def2)
+apply (simp add: int_of_real_mult)
+done
+
+lemma real_is_int_0[simp]: "real_is_int (0::real)"
+by (simp add: real_is_int_def int_of_real_def)
+
+lemma real_is_int_1[simp]: "real_is_int (1::real)"
+proof -
+ have "real_is_int (1::real) = real_is_int(real (1::int))" by auto
+ also have "\<dots> = True" by (simp only: real_is_int_real)
+ ultimately show ?thesis by auto
+qed
+
+lemma real_is_int_n1: "real_is_int (-1::real)"
+proof -
+ have "real_is_int (-1::real) = real_is_int(real (-1::int))" by auto
+ also have "\<dots> = True" by (simp only: real_is_int_real)
+ ultimately show ?thesis by auto
+qed
+
+lemma real_is_int_number_of[simp]: "real_is_int ((number_of \<Colon> int \<Rightarrow> real) x)"
+proof -
+ have neg1: "real_is_int (-1::real)"
+ proof -
+ have "real_is_int (-1::real) = real_is_int(real (-1::int))" by auto
+ also have "\<dots> = True" by (simp only: real_is_int_real)
+ ultimately show ?thesis by auto
+ qed
+
+ {
+ fix x :: int
+ have "real_is_int ((number_of \<Colon> int \<Rightarrow> real) x)"
+ unfolding number_of_eq
+ apply (induct x)
+ apply (induct_tac n)
+ apply (simp)
+ apply (simp)
+ apply (induct_tac n)
+ apply (simp add: neg1)
+ proof -
+ fix n :: nat
+ assume rn: "(real_is_int (of_int (- (int (Suc n)))))"
+ have s: "-(int (Suc (Suc n))) = -1 + - (int (Suc n))" by simp
+ show "real_is_int (of_int (- (int (Suc (Suc n)))))"
+ apply (simp only: s of_int_add)
+ apply (rule real_is_int_add)
+ apply (simp add: neg1)
+ apply (simp only: rn)
+ done
+ qed
+ }
+ note Abs_Bin = this
+ {
+ fix x :: int
+ have "? u. x = u"
+ apply (rule exI[where x = "x"])
+ apply (simp)
+ done
+ }
+ then obtain u::int where "x = u" by auto
+ with Abs_Bin show ?thesis by auto
+qed
+
+lemma int_of_real_0[simp]: "int_of_real (0::real) = (0::int)"
+by (simp add: int_of_real_def)
+
+lemma int_of_real_1[simp]: "int_of_real (1::real) = (1::int)"
+proof -
+ have 1: "(1::real) = real (1::int)" by auto
+ show ?thesis by (simp only: 1 int_of_real_real)
+qed
+
+lemma int_of_real_number_of[simp]: "int_of_real (number_of b) = number_of b"
+proof -
+ have "real_is_int (number_of b)" by simp
+ then have uu: "?! u::int. number_of b = real u" by (auto simp add: real_is_int_rep)
+ then obtain u::int where u:"number_of b = real u" by auto
+ have "number_of b = real ((number_of b)::int)"
+ by (simp add: number_of_eq real_of_int_def)
+ have ub: "number_of b = real ((number_of b)::int)"
+ by (simp add: number_of_eq real_of_int_def)
+ from uu u ub have unb: "u = number_of b"
+ by blast
+ have "int_of_real (number_of b) = u" by (simp add: u)
+ with unb show ?thesis by simp
+qed
+
+lemma float_transfer_even: "even a \<Longrightarrow> float (a, b) = float (a div 2, b+1)"
+ apply (subst float_transfer[where a="a" and b="b" and c="-1", simplified])
+ apply (simp_all add: pow2_def even_def real_is_int_def algebra_simps)
+ apply (auto)
+proof -
+ fix q::int
+ have a:"b - (-1\<Colon>int) = (1\<Colon>int) + b" by arith
+ show "(float (q, (b - (-1\<Colon>int)))) = (float (q, ((1\<Colon>int) + b)))"
+ by (simp add: a)
+qed
+
+lemma int_div_zdiv: "int (a div b) = (int a) div (int b)"
+by (rule zdiv_int)
+
+lemma int_mod_zmod: "int (a mod b) = (int a) mod (int b)"
+by (rule zmod_int)
+
+lemma abs_div_2_less: "a \<noteq> 0 \<Longrightarrow> a \<noteq> -1 \<Longrightarrow> abs((a::int) div 2) < abs a"
+by arith
+
+function norm_float :: "int \<Rightarrow> int \<Rightarrow> int \<times> int" where
+ "norm_float a b = (if a \<noteq> 0 \<and> even a then norm_float (a div 2) (b + 1)
+ else if a = 0 then (0, 0) else (a, b))"
+by auto
+
+termination by (relation "measure (nat o abs o fst)")
+ (auto intro: abs_div_2_less)
+
+lemma norm_float: "float x = float (split norm_float x)"
+proof -
+ {
+ fix a b :: int
+ have norm_float_pair: "float (a, b) = float (norm_float a b)"
+ proof (induct a b rule: norm_float.induct)
+ case (1 u v)
+ show ?case
+ proof cases
+ assume u: "u \<noteq> 0 \<and> even u"
+ with prems have ind: "float (u div 2, v + 1) = float (norm_float (u div 2) (v + 1))" by auto
+ with u have "float (u,v) = float (u div 2, v+1)" by (simp add: float_transfer_even)
+ then show ?thesis
+ apply (subst norm_float.simps)
+ apply (simp add: ind)
+ done
+ next
+ assume "~(u \<noteq> 0 \<and> even u)"
+ then show ?thesis
+ by (simp add: prems float_def)
+ qed
+ qed
+ }
+ note helper = this
+ have "? a b. x = (a,b)" by auto
+ then obtain a b where "x = (a, b)" by blast
+ then show ?thesis by (simp add: helper)
+qed
+
+lemma float_add_l0: "float (0, e) + x = x"
+ by (simp add: float_def)
+
+lemma float_add_r0: "x + float (0, e) = x"
+ by (simp add: float_def)
+
+lemma float_add:
+ "float (a1, e1) + float (a2, e2) =
+ (if e1<=e2 then float (a1+a2*2^(nat(e2-e1)), e1)
+ else float (a1*2^(nat (e1-e2))+a2, e2))"
+ apply (simp add: float_def algebra_simps)
+ apply (auto simp add: pow2_int[symmetric] pow2_add[symmetric])
+ done
+
+lemma float_add_assoc1:
+ "(x + float (y1, e1)) + float (y2, e2) = (float (y1, e1) + float (y2, e2)) + x"
+ by simp
+
+lemma float_add_assoc2:
+ "(float (y1, e1) + x) + float (y2, e2) = (float (y1, e1) + float (y2, e2)) + x"
+ by simp
+
+lemma float_add_assoc3:
+ "float (y1, e1) + (x + float (y2, e2)) = (float (y1, e1) + float (y2, e2)) + x"
+ by simp
+
+lemma float_add_assoc4:
+ "float (y1, e1) + (float (y2, e2) + x) = (float (y1, e1) + float (y2, e2)) + x"
+ by simp
+
+lemma float_mult_l0: "float (0, e) * x = float (0, 0)"
+ by (simp add: float_def)
+
+lemma float_mult_r0: "x * float (0, e) = float (0, 0)"
+ by (simp add: float_def)
+
+definition
+ lbound :: "real \<Rightarrow> real"
+where
+ "lbound x = min 0 x"
+
+definition
+ ubound :: "real \<Rightarrow> real"
+where
+ "ubound x = max 0 x"
+
+lemma lbound: "lbound x \<le> x"
+ by (simp add: lbound_def)
+
+lemma ubound: "x \<le> ubound x"
+ by (simp add: ubound_def)
+
+lemma float_mult:
+ "float (a1, e1) * float (a2, e2) =
+ (float (a1 * a2, e1 + e2))"
+ by (simp add: float_def pow2_add)
+
+lemma float_minus:
+ "- (float (a,b)) = float (-a, b)"
+ by (simp add: float_def)
+
+lemma zero_less_pow2:
+ "0 < pow2 x"
+proof -
+ {
+ fix y
+ have "0 <= y \<Longrightarrow> 0 < pow2 y"
+ by (induct y, induct_tac n, simp_all add: pow2_add)
+ }
+ note helper=this
+ show ?thesis
+ apply (case_tac "0 <= x")
+ apply (simp add: helper)
+ apply (subst pow2_neg)
+ apply (simp add: helper)
+ done
+qed
+
+lemma zero_le_float:
+ "(0 <= float (a,b)) = (0 <= a)"
+ apply (auto simp add: float_def)
+ apply (auto simp add: zero_le_mult_iff zero_less_pow2)
+ apply (insert zero_less_pow2[of b])
+ apply (simp_all)
+ done
+
+lemma float_le_zero:
+ "(float (a,b) <= 0) = (a <= 0)"
+ apply (auto simp add: float_def)
+ apply (auto simp add: mult_le_0_iff)
+ apply (insert zero_less_pow2[of b])
+ apply auto
+ done
+
+lemma float_abs:
+ "abs (float (a,b)) = (if 0 <= a then (float (a,b)) else (float (-a,b)))"
+ apply (auto simp add: abs_if)
+ apply (simp_all add: zero_le_float[symmetric, of a b] float_minus)
+ done
+
+lemma float_zero:
+ "float (0, b) = 0"
+ by (simp add: float_def)
+
+lemma float_pprt:
+ "pprt (float (a, b)) = (if 0 <= a then (float (a,b)) else (float (0, b)))"
+ by (auto simp add: zero_le_float float_le_zero float_zero)
+
+lemma pprt_lbound: "pprt (lbound x) = float (0, 0)"
+ apply (simp add: float_def)
+ apply (rule pprt_eq_0)
+ apply (simp add: lbound_def)
+ done
+
+lemma nprt_ubound: "nprt (ubound x) = float (0, 0)"
+ apply (simp add: float_def)
+ apply (rule nprt_eq_0)
+ apply (simp add: ubound_def)
+ done
+
+lemma float_nprt:
+ "nprt (float (a, b)) = (if 0 <= a then (float (0,b)) else (float (a, b)))"
+ by (auto simp add: zero_le_float float_le_zero float_zero)
+
+lemma norm_0_1: "(0::_::number_ring) = Numeral0 & (1::_::number_ring) = Numeral1"
+ by auto
+
+lemma add_left_zero: "0 + a = (a::'a::comm_monoid_add)"
+ by simp
+
+lemma add_right_zero: "a + 0 = (a::'a::comm_monoid_add)"
+ by simp
+
+lemma mult_left_one: "1 * a = (a::'a::semiring_1)"
+ by simp
+
+lemma mult_right_one: "a * 1 = (a::'a::semiring_1)"
+ by simp
+
+lemma int_pow_0: "(a::int)^(Numeral0) = 1"
+ by simp
+
+lemma int_pow_1: "(a::int)^(Numeral1) = a"
+ by simp
+
+lemma zero_eq_Numeral0_nring: "(0::'a::number_ring) = Numeral0"
+ by simp
+
+lemma one_eq_Numeral1_nring: "(1::'a::number_ring) = Numeral1"
+ by simp
+
+lemma zero_eq_Numeral0_nat: "(0::nat) = Numeral0"
+ by simp
+
+lemma one_eq_Numeral1_nat: "(1::nat) = Numeral1"
+ by simp
+
+lemma zpower_Pls: "(z::int)^Numeral0 = Numeral1"
+ by simp
+
+lemma zpower_Min: "(z::int)^((-1)::nat) = Numeral1"
+proof -
+ have 1:"((-1)::nat) = 0"
+ by simp
+ show ?thesis by (simp add: 1)
+qed
+
+lemma fst_cong: "a=a' \<Longrightarrow> fst (a,b) = fst (a',b)"
+ by simp
+
+lemma snd_cong: "b=b' \<Longrightarrow> snd (a,b) = snd (a,b')"
+ by simp
+
+lemma lift_bool: "x \<Longrightarrow> x=True"
+ by simp
+
+lemma nlift_bool: "~x \<Longrightarrow> x=False"
+ by simp
+
+lemma not_false_eq_true: "(~ False) = True" by simp
+
+lemma not_true_eq_false: "(~ True) = False" by simp
+
+lemmas binarith =
+ normalize_bin_simps
+ pred_bin_simps succ_bin_simps
+ add_bin_simps minus_bin_simps mult_bin_simps
+
+lemma int_eq_number_of_eq:
+ "(((number_of v)::int)=(number_of w)) = iszero ((number_of (v + uminus w))::int)"
+ by (rule eq_number_of_eq)
+
+lemma int_iszero_number_of_Pls: "iszero (Numeral0::int)"
+ by (simp only: iszero_number_of_Pls)
+
+lemma int_nonzero_number_of_Min: "~(iszero ((-1)::int))"
+ by simp
+
+lemma int_iszero_number_of_Bit0: "iszero ((number_of (Int.Bit0 w))::int) = iszero ((number_of w)::int)"
+ by simp
+
+lemma int_iszero_number_of_Bit1: "\<not> iszero ((number_of (Int.Bit1 w))::int)"
+ by simp
+
+lemma int_less_number_of_eq_neg: "(((number_of x)::int) < number_of y) = neg ((number_of (x + (uminus y)))::int)"
+ unfolding neg_def number_of_is_id by simp
+
+lemma int_not_neg_number_of_Pls: "\<not> (neg (Numeral0::int))"
+ by simp
+
+lemma int_neg_number_of_Min: "neg (-1::int)"
+ by simp
+
+lemma int_neg_number_of_Bit0: "neg ((number_of (Int.Bit0 w))::int) = neg ((number_of w)::int)"
+ by simp
+
+lemma int_neg_number_of_Bit1: "neg ((number_of (Int.Bit1 w))::int) = neg ((number_of w)::int)"
+ by simp
+
+lemma int_le_number_of_eq: "(((number_of x)::int) \<le> number_of y) = (\<not> neg ((number_of (y + (uminus x)))::int))"
+ unfolding neg_def number_of_is_id by (simp add: not_less)
+
+lemmas intarithrel =
+ int_eq_number_of_eq
+ lift_bool[OF int_iszero_number_of_Pls] nlift_bool[OF int_nonzero_number_of_Min] int_iszero_number_of_Bit0
+ lift_bool[OF int_iszero_number_of_Bit1] int_less_number_of_eq_neg nlift_bool[OF int_not_neg_number_of_Pls] lift_bool[OF int_neg_number_of_Min]
+ int_neg_number_of_Bit0 int_neg_number_of_Bit1 int_le_number_of_eq
+
+lemma int_number_of_add_sym: "((number_of v)::int) + number_of w = number_of (v + w)"
+ by simp
+
+lemma int_number_of_diff_sym: "((number_of v)::int) - number_of w = number_of (v + (uminus w))"
+ by simp
+
+lemma int_number_of_mult_sym: "((number_of v)::int) * number_of w = number_of (v * w)"
+ by simp
+
+lemma int_number_of_minus_sym: "- ((number_of v)::int) = number_of (uminus v)"
+ by simp
+
+lemmas intarith = int_number_of_add_sym int_number_of_minus_sym int_number_of_diff_sym int_number_of_mult_sym
+
+lemmas natarith = add_nat_number_of diff_nat_number_of mult_nat_number_of eq_nat_number_of less_nat_number_of
+
+lemmas powerarith = nat_number_of zpower_number_of_even
+ zpower_number_of_odd[simplified zero_eq_Numeral0_nring one_eq_Numeral1_nring]
+ zpower_Pls zpower_Min
+
+lemmas floatarith[simplified norm_0_1] = float_add float_add_l0 float_add_r0 float_mult float_mult_l0 float_mult_r0
+ float_minus float_abs zero_le_float float_pprt float_nprt pprt_lbound nprt_ubound
+
+(* for use with the compute oracle *)
+lemmas arith = binarith intarith intarithrel natarith powerarith floatarith not_false_eq_true not_true_eq_false
+
+use "~~/src/HOL/Tools/float_arith.ML"
+
+end
--- a/src/HOL/Tools/ComputeNumeral.thy Fri Feb 06 15:57:47 2009 +0100
+++ b/src/HOL/Tools/ComputeNumeral.thy Fri Feb 06 15:59:49 2009 +0100
@@ -1,5 +1,5 @@
theory ComputeNumeral
-imports ComputeHOL Float
+imports ComputeHOL ComputeFloat
begin
(* normalization of bit strings *)
--- a/src/HOL/Tools/float_arith.ML Fri Feb 06 15:57:47 2009 +0100
+++ b/src/HOL/Tools/float_arith.ML Fri Feb 06 15:59:49 2009 +0100
@@ -1,5 +1,4 @@
-(* Title: HOL/Real/float_arith.ML
- ID: $Id$
+(* Title: HOL/Tools/float_arith.ML
Author: Steven Obua
*)
--- a/src/HOL/Tools/primrec_package.ML Fri Feb 06 15:57:47 2009 +0100
+++ b/src/HOL/Tools/primrec_package.ML Fri Feb 06 15:59:49 2009 +0100
@@ -247,12 +247,12 @@
val _ = if gen_eq_set (op =) (names1, names2) then ()
else primrec_error ("functions " ^ commas_quote names2 ^
"\nare not mutually recursive");
- val qualify = Binding.qualify
- (space_implode "_" (map (Sign.base_name o #1) defs));
- val spec' = (map o apfst o apfst) qualify spec;
+ val prefix = space_implode "_" (map (Sign.base_name o #1) defs);
+ val qualify = Binding.qualify prefix;
+ val spec' = (map o apfst)
+ (fn (b, attrs) => (qualify b, Code.add_default_eqn_attrib :: attrs)) spec;
val simp_atts = map (Attrib.internal o K)
- [Simplifier.simp_add, Code.add_default_eqn_attribute,
- Nitpick_Const_Simps_Thms.add];
+ [Simplifier.simp_add, Nitpick_Const_Simps_Thms.add];
in
lthy
|> set_group ? LocalTheory.set_group (serial_string ())
--- a/src/HOL/Transcendental.thy Fri Feb 06 15:57:47 2009 +0100
+++ b/src/HOL/Transcendental.thy Fri Feb 06 15:59:49 2009 +0100
@@ -113,6 +113,208 @@
==> summable (%n. f(n) * (z ^ n))"
by (rule powser_insidea [THEN summable_norm_cancel])
+lemma sum_split_even_odd: fixes f :: "nat \<Rightarrow> real" shows
+ "(\<Sum> i = 0 ..< 2 * n. if even i then f i else g i) =
+ (\<Sum> i = 0 ..< n. f (2 * i)) + (\<Sum> i = 0 ..< n. g (2 * i + 1))"
+proof (induct n)
+ case (Suc n)
+ have "(\<Sum> i = 0 ..< 2 * Suc n. if even i then f i else g i) =
+ (\<Sum> i = 0 ..< n. f (2 * i)) + (\<Sum> i = 0 ..< n. g (2 * i + 1)) + (f (2 * n) + g (2 * n + 1))"
+ using Suc.hyps by auto
+ also have "\<dots> = (\<Sum> i = 0 ..< Suc n. f (2 * i)) + (\<Sum> i = 0 ..< Suc n. g (2 * i + 1))" by auto
+ finally show ?case .
+qed auto
+
+lemma sums_if': fixes g :: "nat \<Rightarrow> real" assumes "g sums x"
+ shows "(\<lambda> n. if even n then 0 else g ((n - 1) div 2)) sums x"
+ unfolding sums_def
+proof (rule LIMSEQ_I)
+ fix r :: real assume "0 < r"
+ from `g sums x`[unfolded sums_def, THEN LIMSEQ_D, OF this]
+ obtain no where no_eq: "\<And> n. n \<ge> no \<Longrightarrow> (norm (setsum g { 0..<n } - x) < r)" by blast
+
+ let ?SUM = "\<lambda> m. \<Sum> i = 0 ..< m. if even i then 0 else g ((i - 1) div 2)"
+ { fix m assume "m \<ge> 2 * no" hence "m div 2 \<ge> no" by auto
+ have sum_eq: "?SUM (2 * (m div 2)) = setsum g { 0 ..< m div 2 }"
+ using sum_split_even_odd by auto
+ hence "(norm (?SUM (2 * (m div 2)) - x) < r)" using no_eq unfolding sum_eq using `m div 2 \<ge> no` by auto
+ moreover
+ have "?SUM (2 * (m div 2)) = ?SUM m"
+ proof (cases "even m")
+ case True show ?thesis unfolding even_nat_div_two_times_two[OF True, unfolded numeral_2_eq_2[symmetric]] ..
+ next
+ case False hence "even (Suc m)" by auto
+ from even_nat_div_two_times_two[OF this, unfolded numeral_2_eq_2[symmetric]] odd_nat_plus_one_div_two[OF False, unfolded numeral_2_eq_2[symmetric]]
+ have eq: "Suc (2 * (m div 2)) = m" by auto
+ hence "even (2 * (m div 2))" using `odd m` by auto
+ have "?SUM m = ?SUM (Suc (2 * (m div 2)))" unfolding eq ..
+ also have "\<dots> = ?SUM (2 * (m div 2))" using `even (2 * (m div 2))` by auto
+ finally show ?thesis by auto
+ qed
+ ultimately have "(norm (?SUM m - x) < r)" by auto
+ }
+ thus "\<exists> no. \<forall> m \<ge> no. norm (?SUM m - x) < r" by blast
+qed
+
+lemma sums_if: fixes g :: "nat \<Rightarrow> real" assumes "g sums x" and "f sums y"
+ shows "(\<lambda> n. if even n then f (n div 2) else g ((n - 1) div 2)) sums (x + y)"
+proof -
+ let ?s = "\<lambda> n. if even n then 0 else f ((n - 1) div 2)"
+ { fix B T E have "(if B then (0 :: real) else E) + (if B then T else 0) = (if B then T else E)"
+ by (cases B) auto } note if_sum = this
+ have g_sums: "(\<lambda> n. if even n then 0 else g ((n - 1) div 2)) sums x" using sums_if'[OF `g sums x`] .
+ {
+ have "?s 0 = 0" by auto
+ have Suc_m1: "\<And> n. Suc n - 1 = n" by auto
+ { fix B T E have "(if \<not> B then T else E) = (if B then E else T)" by auto } note if_eq = this
+
+ have "?s sums y" using sums_if'[OF `f sums y`] .
+ from this[unfolded sums_def, THEN LIMSEQ_Suc]
+ have "(\<lambda> n. if even n then f (n div 2) else 0) sums y"
+ unfolding sums_def setsum_shift_lb_Suc0_0_upt[where f="?s", OF `?s 0 = 0`, symmetric]
+ image_Suc_atLeastLessThan[symmetric] setsum_reindex[OF inj_Suc, unfolded comp_def]
+ even_nat_Suc Suc_m1 if_eq .
+ } from sums_add[OF g_sums this]
+ show ?thesis unfolding if_sum .
+qed
+
+subsection {* Alternating series test / Leibniz formula *}
+
+lemma sums_alternating_upper_lower:
+ fixes a :: "nat \<Rightarrow> real"
+ assumes mono: "\<And>n. a (Suc n) \<le> a n" and a_pos: "\<And>n. 0 \<le> a n" and "a ----> 0"
+ shows "\<exists>l. ((\<forall>n. (\<Sum>i=0..<2*n. -1^i*a i) \<le> l) \<and> (\<lambda> n. \<Sum>i=0..<2*n. -1^i*a i) ----> l) \<and>
+ ((\<forall>n. l \<le> (\<Sum>i=0..<2*n + 1. -1^i*a i)) \<and> (\<lambda> n. \<Sum>i=0..<2*n + 1. -1^i*a i) ----> l)"
+ (is "\<exists>l. ((\<forall>n. ?f n \<le> l) \<and> _) \<and> ((\<forall>n. l \<le> ?g n) \<and> _)")
+proof -
+ have fg_diff: "\<And>n. ?f n - ?g n = - a (2 * n)" by auto
+
+ have "\<forall> n. ?f n \<le> ?f (Suc n)"
+ proof fix n show "?f n \<le> ?f (Suc n)" using mono[of "2*n"] by auto qed
+ moreover
+ have "\<forall> n. ?g (Suc n) \<le> ?g n"
+ proof fix n show "?g (Suc n) \<le> ?g n" using mono[of "Suc (2*n)"] by auto qed
+ moreover
+ have "\<forall> n. ?f n \<le> ?g n"
+ proof fix n show "?f n \<le> ?g n" using fg_diff a_pos by auto qed
+ moreover
+ have "(\<lambda> n. ?f n - ?g n) ----> 0" unfolding fg_diff
+ proof (rule LIMSEQ_I)
+ fix r :: real assume "0 < r"
+ with `a ----> 0`[THEN LIMSEQ_D]
+ obtain N where "\<And> n. n \<ge> N \<Longrightarrow> norm (a n - 0) < r" by auto
+ hence "\<forall> n \<ge> N. norm (- a (2 * n) - 0) < r" by auto
+ thus "\<exists> N. \<forall> n \<ge> N. norm (- a (2 * n) - 0) < r" by auto
+ qed
+ ultimately
+ show ?thesis by (rule lemma_nest_unique)
+qed
+
+lemma summable_Leibniz': fixes a :: "nat \<Rightarrow> real"
+ assumes a_zero: "a ----> 0" and a_pos: "\<And> n. 0 \<le> a n"
+ and a_monotone: "\<And> n. a (Suc n) \<le> a n"
+ shows summable: "summable (\<lambda> n. (-1)^n * a n)"
+ and "\<And>n. (\<Sum>i=0..<2*n. (-1)^i*a i) \<le> (\<Sum>i. (-1)^i*a i)"
+ and "(\<lambda>n. \<Sum>i=0..<2*n. (-1)^i*a i) ----> (\<Sum>i. (-1)^i*a i)"
+ and "\<And>n. (\<Sum>i. (-1)^i*a i) \<le> (\<Sum>i=0..<2*n+1. (-1)^i*a i)"
+ and "(\<lambda>n. \<Sum>i=0..<2*n+1. (-1)^i*a i) ----> (\<Sum>i. (-1)^i*a i)"
+proof -
+ let "?S n" = "(-1)^n * a n"
+ let "?P n" = "\<Sum>i=0..<n. ?S i"
+ let "?f n" = "?P (2 * n)"
+ let "?g n" = "?P (2 * n + 1)"
+ obtain l :: real where below_l: "\<forall> n. ?f n \<le> l" and "?f ----> l" and above_l: "\<forall> n. l \<le> ?g n" and "?g ----> l"
+ using sums_alternating_upper_lower[OF a_monotone a_pos a_zero] by blast
+
+ let ?Sa = "\<lambda> m. \<Sum> n = 0..<m. ?S n"
+ have "?Sa ----> l"
+ proof (rule LIMSEQ_I)
+ fix r :: real assume "0 < r"
+
+ with `?f ----> l`[THEN LIMSEQ_D]
+ obtain f_no where f: "\<And> n. n \<ge> f_no \<Longrightarrow> norm (?f n - l) < r" by auto
+
+ from `0 < r` `?g ----> l`[THEN LIMSEQ_D]
+ obtain g_no where g: "\<And> n. n \<ge> g_no \<Longrightarrow> norm (?g n - l) < r" by auto
+
+ { fix n :: nat
+ assume "n \<ge> (max (2 * f_no) (2 * g_no))" hence "n \<ge> 2 * f_no" and "n \<ge> 2 * g_no" by auto
+ have "norm (?Sa n - l) < r"
+ proof (cases "even n")
+ case True from even_nat_div_two_times_two[OF this]
+ have n_eq: "2 * (n div 2) = n" unfolding numeral_2_eq_2[symmetric] by auto
+ with `n \<ge> 2 * f_no` have "n div 2 \<ge> f_no" by auto
+ from f[OF this]
+ show ?thesis unfolding n_eq atLeastLessThanSuc_atLeastAtMost .
+ next
+ case False hence "even (n - 1)" using even_num_iff odd_pos by auto
+ from even_nat_div_two_times_two[OF this]
+ have n_eq: "2 * ((n - 1) div 2) = n - 1" unfolding numeral_2_eq_2[symmetric] by auto
+ hence range_eq: "n - 1 + 1 = n" using odd_pos[OF False] by auto
+
+ from n_eq `n \<ge> 2 * g_no` have "(n - 1) div 2 \<ge> g_no" by auto
+ from g[OF this]
+ show ?thesis unfolding n_eq atLeastLessThanSuc_atLeastAtMost range_eq .
+ qed
+ }
+ thus "\<exists> no. \<forall> n \<ge> no. norm (?Sa n - l) < r" by blast
+ qed
+ hence sums_l: "(\<lambda>i. (-1)^i * a i) sums l" unfolding sums_def atLeastLessThanSuc_atLeastAtMost[symmetric] .
+ thus "summable ?S" using summable_def by auto
+
+ have "l = suminf ?S" using sums_unique[OF sums_l] .
+
+ { fix n show "suminf ?S \<le> ?g n" unfolding sums_unique[OF sums_l, symmetric] using above_l by auto }
+ { fix n show "?f n \<le> suminf ?S" unfolding sums_unique[OF sums_l, symmetric] using below_l by auto }
+ show "?g ----> suminf ?S" using `?g ----> l` `l = suminf ?S` by auto
+ show "?f ----> suminf ?S" using `?f ----> l` `l = suminf ?S` by auto
+qed
+
+theorem summable_Leibniz: fixes a :: "nat \<Rightarrow> real"
+ assumes a_zero: "a ----> 0" and "monoseq a"
+ shows "summable (\<lambda> n. (-1)^n * a n)" (is "?summable")
+ and "0 < a 0 \<longrightarrow> (\<forall>n. (\<Sum>i. -1^i*a i) \<in> { \<Sum>i=0..<2*n. -1^i * a i .. \<Sum>i=0..<2*n+1. -1^i * a i})" (is "?pos")
+ and "a 0 < 0 \<longrightarrow> (\<forall>n. (\<Sum>i. -1^i*a i) \<in> { \<Sum>i=0..<2*n+1. -1^i * a i .. \<Sum>i=0..<2*n. -1^i * a i})" (is "?neg")
+ and "(\<lambda>n. \<Sum>i=0..<2*n. -1^i*a i) ----> (\<Sum>i. -1^i*a i)" (is "?f")
+ and "(\<lambda>n. \<Sum>i=0..<2*n+1. -1^i*a i) ----> (\<Sum>i. -1^i*a i)" (is "?g")
+proof -
+ have "?summable \<and> ?pos \<and> ?neg \<and> ?f \<and> ?g"
+ proof (cases "(\<forall> n. 0 \<le> a n) \<and> (\<forall>m. \<forall>n\<ge>m. a n \<le> a m)")
+ case True
+ hence ord: "\<And>n m. m \<le> n \<Longrightarrow> a n \<le> a m" and ge0: "\<And> n. 0 \<le> a n" by auto
+ { fix n have "a (Suc n) \<le> a n" using ord[where n="Suc n" and m=n] by auto }
+ note leibniz = summable_Leibniz'[OF `a ----> 0` ge0] and mono = this
+ from leibniz[OF mono]
+ show ?thesis using `0 \<le> a 0` by auto
+ next
+ let ?a = "\<lambda> n. - a n"
+ case False
+ with monoseq_le[OF `monoseq a` `a ----> 0`]
+ have "(\<forall> n. a n \<le> 0) \<and> (\<forall>m. \<forall>n\<ge>m. a m \<le> a n)" by auto
+ hence ord: "\<And>n m. m \<le> n \<Longrightarrow> ?a n \<le> ?a m" and ge0: "\<And> n. 0 \<le> ?a n" by auto
+ { fix n have "?a (Suc n) \<le> ?a n" using ord[where n="Suc n" and m=n] by auto }
+ note monotone = this
+ note leibniz = summable_Leibniz'[OF _ ge0, of "\<lambda>x. x", OF LIMSEQ_minus[OF `a ----> 0`, unfolded minus_zero] monotone]
+ have "summable (\<lambda> n. (-1)^n * ?a n)" using leibniz(1) by auto
+ then obtain l where "(\<lambda> n. (-1)^n * ?a n) sums l" unfolding summable_def by auto
+ from this[THEN sums_minus]
+ have "(\<lambda> n. (-1)^n * a n) sums -l" by auto
+ hence ?summable unfolding summable_def by auto
+ moreover
+ have "\<And> a b :: real. \<bar> - a - - b \<bar> = \<bar>a - b\<bar>" unfolding minus_diff_minus by auto
+
+ from suminf_minus[OF leibniz(1), unfolded mult_minus_right minus_minus]
+ have move_minus: "(\<Sum>n. - (-1 ^ n * a n)) = - (\<Sum>n. -1 ^ n * a n)" by auto
+
+ have ?pos using `0 \<le> ?a 0` by auto
+ moreover have ?neg using leibniz(2,4) unfolding mult_minus_right setsum_negf move_minus neg_le_iff_le by auto
+ moreover have ?f and ?g using leibniz(3,5)[unfolded mult_minus_right setsum_negf move_minus, THEN LIMSEQ_minus_cancel] by auto
+ ultimately show ?thesis by auto
+ qed
+ from this[THEN conjunct1] this[THEN conjunct2, THEN conjunct1] this[THEN conjunct2, THEN conjunct2, THEN conjunct1] this[THEN conjunct2, THEN conjunct2, THEN conjunct2, THEN conjunct1]
+ this[THEN conjunct2, THEN conjunct2, THEN conjunct2, THEN conjunct2]
+ show ?summable and ?pos and ?neg and ?f and ?g .
+qed
subsection {* Term-by-Term Differentiability of Power Series *}
@@ -432,6 +634,188 @@
lemma inv_real_of_nat_fact_ge_zero [simp]: "0 \<le> inverse (real (fact n))"
by (auto intro: order_less_imp_le)
+subsection {* Derivability of power series *}
+
+lemma DERIV_series': fixes f :: "real \<Rightarrow> nat \<Rightarrow> real"
+ assumes DERIV_f: "\<And> n. DERIV (\<lambda> x. f x n) x0 :> (f' x0 n)"
+ and allf_summable: "\<And> x. x \<in> {a <..< b} \<Longrightarrow> summable (f x)" and x0_in_I: "x0 \<in> {a <..< b}"
+ and "summable (f' x0)"
+ and "summable L" and L_def: "\<And> n x y. \<lbrakk> x \<in> { a <..< b} ; y \<in> { a <..< b} \<rbrakk> \<Longrightarrow> \<bar> f x n - f y n \<bar> \<le> L n * \<bar> x - y \<bar>"
+ shows "DERIV (\<lambda> x. suminf (f x)) x0 :> (suminf (f' x0))"
+ unfolding deriv_def
+proof (rule LIM_I)
+ fix r :: real assume "0 < r" hence "0 < r/3" by auto
+
+ obtain N_L where N_L: "\<And> n. N_L \<le> n \<Longrightarrow> \<bar> \<Sum> i. L (i + n) \<bar> < r/3"
+ using suminf_exist_split[OF `0 < r/3` `summable L`] by auto
+
+ obtain N_f' where N_f': "\<And> n. N_f' \<le> n \<Longrightarrow> \<bar> \<Sum> i. f' x0 (i + n) \<bar> < r/3"
+ using suminf_exist_split[OF `0 < r/3` `summable (f' x0)`] by auto
+
+ let ?N = "Suc (max N_L N_f')"
+ have "\<bar> \<Sum> i. f' x0 (i + ?N) \<bar> < r/3" (is "?f'_part < r/3") and
+ L_estimate: "\<bar> \<Sum> i. L (i + ?N) \<bar> < r/3" using N_L[of "?N"] and N_f' [of "?N"] by auto
+
+ let "?diff i x" = "(f (x0 + x) i - f x0 i) / x"
+
+ let ?r = "r / (3 * real ?N)"
+ have "0 < 3 * real ?N" by auto
+ from divide_pos_pos[OF `0 < r` this]
+ have "0 < ?r" .
+
+ let "?s n" = "SOME s. 0 < s \<and> (\<forall> x. x \<noteq> 0 \<and> \<bar> x \<bar> < s \<longrightarrow> \<bar> ?diff n x - f' x0 n \<bar> < ?r)"
+ def S' \<equiv> "Min (?s ` { 0 ..< ?N })"
+
+ have "0 < S'" unfolding S'_def
+ proof (rule iffD2[OF Min_gr_iff])
+ show "\<forall> x \<in> (?s ` { 0 ..< ?N }). 0 < x"
+ proof (rule ballI)
+ fix x assume "x \<in> ?s ` {0..<?N}"
+ then obtain n where "x = ?s n" and "n \<in> {0..<?N}" using image_iff[THEN iffD1] by blast
+ from DERIV_D[OF DERIV_f[where n=n], THEN LIM_D, OF `0 < ?r`, unfolded real_norm_def]
+ obtain s where s_bound: "0 < s \<and> (\<forall>x. x \<noteq> 0 \<and> \<bar>x\<bar> < s \<longrightarrow> \<bar>?diff n x - f' x0 n\<bar> < ?r)" by auto
+ have "0 < ?s n" by (rule someI2[where a=s], auto simp add: s_bound)
+ thus "0 < x" unfolding `x = ?s n` .
+ qed
+ qed auto
+
+ def S \<equiv> "min (min (x0 - a) (b - x0)) S'"
+ hence "0 < S" and S_a: "S \<le> x0 - a" and S_b: "S \<le> b - x0" and "S \<le> S'" using x0_in_I and `0 < S'`
+ by auto
+
+ { fix x assume "x \<noteq> 0" and "\<bar> x \<bar> < S"
+ hence x_in_I: "x0 + x \<in> { a <..< b }" using S_a S_b by auto
+
+ note diff_smbl = summable_diff[OF allf_summable[OF x_in_I] allf_summable[OF x0_in_I]]
+ note div_smbl = summable_divide[OF diff_smbl]
+ note all_smbl = summable_diff[OF div_smbl `summable (f' x0)`]
+ note ign = summable_ignore_initial_segment[where k="?N"]
+ note diff_shft_smbl = summable_diff[OF ign[OF allf_summable[OF x_in_I]] ign[OF allf_summable[OF x0_in_I]]]
+ note div_shft_smbl = summable_divide[OF diff_shft_smbl]
+ note all_shft_smbl = summable_diff[OF div_smbl ign[OF `summable (f' x0)`]]
+
+ { fix n
+ have "\<bar> ?diff (n + ?N) x \<bar> \<le> L (n + ?N) * \<bar> (x0 + x) - x0 \<bar> / \<bar> x \<bar>"
+ using divide_right_mono[OF L_def[OF x_in_I x0_in_I] abs_ge_zero] unfolding abs_divide .
+ hence "\<bar> ( \<bar> ?diff (n + ?N) x \<bar>) \<bar> \<le> L (n + ?N)" using `x \<noteq> 0` by auto
+ } note L_ge = summable_le2[OF allI[OF this] ign[OF `summable L`]]
+ from order_trans[OF summable_rabs[OF conjunct1[OF L_ge]] L_ge[THEN conjunct2]]
+ have "\<bar> \<Sum> i. ?diff (i + ?N) x \<bar> \<le> (\<Sum> i. L (i + ?N))" .
+ hence "\<bar> \<Sum> i. ?diff (i + ?N) x \<bar> \<le> r / 3" (is "?L_part \<le> r/3") using L_estimate by auto
+
+ have "\<bar>\<Sum>n \<in> { 0 ..< ?N}. ?diff n x - f' x0 n \<bar> \<le> (\<Sum>n \<in> { 0 ..< ?N}. \<bar>?diff n x - f' x0 n \<bar>)" ..
+ also have "\<dots> < (\<Sum>n \<in> { 0 ..< ?N}. ?r)"
+ proof (rule setsum_strict_mono)
+ fix n assume "n \<in> { 0 ..< ?N}"
+ have "\<bar> x \<bar> < S" using `\<bar> x \<bar> < S` .
+ also have "S \<le> S'" using `S \<le> S'` .
+ also have "S' \<le> ?s n" unfolding S'_def
+ proof (rule Min_le_iff[THEN iffD2])
+ have "?s n \<in> (?s ` {0..<?N}) \<and> ?s n \<le> ?s n" using `n \<in> { 0 ..< ?N}` by auto
+ thus "\<exists> a \<in> (?s ` {0..<?N}). a \<le> ?s n" by blast
+ qed auto
+ finally have "\<bar> x \<bar> < ?s n" .
+
+ from DERIV_D[OF DERIV_f[where n=n], THEN LIM_D, OF `0 < ?r`, unfolded real_norm_def diff_0_right, unfolded some_eq_ex[symmetric], THEN conjunct2]
+ have "\<forall>x. x \<noteq> 0 \<and> \<bar>x\<bar> < ?s n \<longrightarrow> \<bar>?diff n x - f' x0 n\<bar> < ?r" .
+ with `x \<noteq> 0` and `\<bar>x\<bar> < ?s n`
+ show "\<bar>?diff n x - f' x0 n\<bar> < ?r" by blast
+ qed auto
+ also have "\<dots> = of_nat (card {0 ..< ?N}) * ?r" by (rule setsum_constant)
+ also have "\<dots> = real ?N * ?r" unfolding real_eq_of_nat by auto
+ also have "\<dots> = r/3" by auto
+ finally have "\<bar>\<Sum>n \<in> { 0 ..< ?N}. ?diff n x - f' x0 n \<bar> < r / 3" (is "?diff_part < r / 3") .
+
+ from suminf_diff[OF allf_summable[OF x_in_I] allf_summable[OF x0_in_I]]
+ have "\<bar> (suminf (f (x0 + x)) - (suminf (f x0))) / x - suminf (f' x0) \<bar> =
+ \<bar> \<Sum>n. ?diff n x - f' x0 n \<bar>" unfolding suminf_diff[OF div_smbl `summable (f' x0)`, symmetric] using suminf_divide[OF diff_smbl, symmetric] by auto
+ also have "\<dots> \<le> ?diff_part + \<bar> (\<Sum>n. ?diff (n + ?N) x) - (\<Sum> n. f' x0 (n + ?N)) \<bar>" unfolding suminf_split_initial_segment[OF all_smbl, where k="?N"] unfolding suminf_diff[OF div_shft_smbl ign[OF `summable (f' x0)`]] by (rule abs_triangle_ineq)
+ also have "\<dots> \<le> ?diff_part + ?L_part + ?f'_part" using abs_triangle_ineq4 by auto
+ also have "\<dots> < r /3 + r/3 + r/3"
+ using `?diff_part < r/3` `?L_part \<le> r/3` and `?f'_part < r/3` by auto
+ finally have "\<bar> (suminf (f (x0 + x)) - (suminf (f x0))) / x - suminf (f' x0) \<bar> < r"
+ by auto
+ } thus "\<exists> s > 0. \<forall> x. x \<noteq> 0 \<and> norm (x - 0) < s \<longrightarrow>
+ norm (((\<Sum>n. f (x0 + x) n) - (\<Sum>n. f x0 n)) / x - (\<Sum>n. f' x0 n)) < r" using `0 < S`
+ unfolding real_norm_def diff_0_right by blast
+qed
+
+lemma DERIV_power_series': fixes f :: "nat \<Rightarrow> real"
+ assumes converges: "\<And> x. x \<in> {-R <..< R} \<Longrightarrow> summable (\<lambda> n. f n * real (Suc n) * x^n)"
+ and x0_in_I: "x0 \<in> {-R <..< R}" and "0 < R"
+ shows "DERIV (\<lambda> x. (\<Sum> n. f n * x^(Suc n))) x0 :> (\<Sum> n. f n * real (Suc n) * x0^n)"
+ (is "DERIV (\<lambda> x. (suminf (?f x))) x0 :> (suminf (?f' x0))")
+proof -
+ { fix R' assume "0 < R'" and "R' < R" and "-R' < x0" and "x0 < R'"
+ hence "x0 \<in> {-R' <..< R'}" and "R' \<in> {-R <..< R}" and "x0 \<in> {-R <..< R}" by auto
+ have "DERIV (\<lambda> x. (suminf (?f x))) x0 :> (suminf (?f' x0))"
+ proof (rule DERIV_series')
+ show "summable (\<lambda> n. \<bar>f n * real (Suc n) * R'^n\<bar>)"
+ proof -
+ have "(R' + R) / 2 < R" and "0 < (R' + R) / 2" using `0 < R'` `0 < R` `R' < R` by auto
+ hence in_Rball: "(R' + R) / 2 \<in> {-R <..< R}" using `R' < R` by auto
+ have "norm R' < norm ((R' + R) / 2)" using `0 < R'` `0 < R` `R' < R` by auto
+ from powser_insidea[OF converges[OF in_Rball] this] show ?thesis by auto
+ qed
+ { fix n x y assume "x \<in> {-R' <..< R'}" and "y \<in> {-R' <..< R'}"
+ show "\<bar>?f x n - ?f y n\<bar> \<le> \<bar>f n * real (Suc n) * R'^n\<bar> * \<bar>x-y\<bar>"
+ proof -
+ have "\<bar>f n * x ^ (Suc n) - f n * y ^ (Suc n)\<bar> = (\<bar>f n\<bar> * \<bar>x-y\<bar>) * \<bar>\<Sum>p = 0..<Suc n. x ^ p * y ^ (n - p)\<bar>"
+ unfolding right_diff_distrib[symmetric] lemma_realpow_diff_sumr2 abs_mult by auto
+ also have "\<dots> \<le> (\<bar>f n\<bar> * \<bar>x-y\<bar>) * (\<bar>real (Suc n)\<bar> * \<bar>R' ^ n\<bar>)"
+ proof (rule mult_left_mono)
+ have "\<bar>\<Sum>p = 0..<Suc n. x ^ p * y ^ (n - p)\<bar> \<le> (\<Sum>p = 0..<Suc n. \<bar>x ^ p * y ^ (n - p)\<bar>)" by (rule setsum_abs)
+ also have "\<dots> \<le> (\<Sum>p = 0..<Suc n. R' ^ n)"
+ proof (rule setsum_mono)
+ fix p assume "p \<in> {0..<Suc n}" hence "p \<le> n" by auto
+ { fix n fix x :: real assume "x \<in> {-R'<..<R'}"
+ hence "\<bar>x\<bar> \<le> R'" by auto
+ hence "\<bar>x^n\<bar> \<le> R'^n" unfolding power_abs by (rule power_mono, auto)
+ } from mult_mono[OF this[OF `x \<in> {-R'<..<R'}`, of p] this[OF `y \<in> {-R'<..<R'}`, of "n-p"]] `0 < R'`
+ have "\<bar>x^p * y^(n-p)\<bar> \<le> R'^p * R'^(n-p)" unfolding abs_mult by auto
+ thus "\<bar>x^p * y^(n-p)\<bar> \<le> R'^n" unfolding power_add[symmetric] using `p \<le> n` by auto
+ qed
+ also have "\<dots> = real (Suc n) * R' ^ n" unfolding setsum_constant card_atLeastLessThan real_of_nat_def by auto
+ finally show "\<bar>\<Sum>p = 0..<Suc n. x ^ p * y ^ (n - p)\<bar> \<le> \<bar>real (Suc n)\<bar> * \<bar>R' ^ n\<bar>" unfolding abs_real_of_nat_cancel abs_of_nonneg[OF zero_le_power[OF less_imp_le[OF `0 < R'`]]] .
+ show "0 \<le> \<bar>f n\<bar> * \<bar>x - y\<bar>" unfolding abs_mult[symmetric] by auto
+ qed
+ also have "\<dots> = \<bar>f n * real (Suc n) * R' ^ n\<bar> * \<bar>x - y\<bar>" unfolding abs_mult real_mult_assoc[symmetric] by algebra
+ finally show ?thesis .
+ qed }
+ { fix n
+ from DERIV_pow[of "Suc n" x0, THEN DERIV_cmult[where c="f n"]]
+ show "DERIV (\<lambda> x. ?f x n) x0 :> (?f' x0 n)" unfolding real_mult_assoc by auto }
+ { fix x assume "x \<in> {-R' <..< R'}" hence "R' \<in> {-R <..< R}" and "norm x < norm R'" using assms `R' < R` by auto
+ have "summable (\<lambda> n. f n * x^n)"
+ proof (rule summable_le2[THEN conjunct1, OF _ powser_insidea[OF converges[OF `R' \<in> {-R <..< R}`] `norm x < norm R'`]], rule allI)
+ fix n
+ have le: "\<bar>f n\<bar> * 1 \<le> \<bar>f n\<bar> * real (Suc n)" by (rule mult_left_mono, auto)
+ show "\<bar>f n * x ^ n\<bar> \<le> norm (f n * real (Suc n) * x ^ n)" unfolding real_norm_def abs_mult
+ by (rule mult_right_mono, auto simp add: le[unfolded mult_1_right])
+ qed
+ from this[THEN summable_mult2[where c=x], unfolded real_mult_assoc, unfolded real_mult_commute]
+ show "summable (?f x)" by auto }
+ show "summable (?f' x0)" using converges[OF `x0 \<in> {-R <..< R}`] .
+ show "x0 \<in> {-R' <..< R'}" using `x0 \<in> {-R' <..< R'}` .
+ qed
+ } note for_subinterval = this
+ let ?R = "(R + \<bar>x0\<bar>) / 2"
+ have "\<bar>x0\<bar> < ?R" using assms by auto
+ hence "- ?R < x0"
+ proof (cases "x0 < 0")
+ case True
+ hence "- x0 < ?R" using `\<bar>x0\<bar> < ?R` by auto
+ thus ?thesis unfolding neg_less_iff_less[symmetric, of "- x0"] by auto
+ next
+ case False
+ have "- ?R < 0" using assms by auto
+ also have "\<dots> \<le> x0" using False by auto
+ finally show ?thesis .
+ qed
+ hence "0 < ?R" "?R < R" "- ?R < x0" and "x0 < ?R" using assms by auto
+ from for_subinterval[OF this]
+ show ?thesis .
+qed
subsection {* Exponential Function *}
@@ -830,6 +1214,37 @@
apply (simp_all add: abs_if isCont_ln)
done
+lemma ln_series: assumes "0 < x" and "x < 2"
+ shows "ln x = (\<Sum> n. (-1)^n * (1 / real (n + 1)) * (x - 1)^(Suc n))" (is "ln x = suminf (?f (x - 1))")
+proof -
+ let "?f' x n" = "(-1)^n * (x - 1)^n"
+
+ have "ln x - suminf (?f (x - 1)) = ln 1 - suminf (?f (1 - 1))"
+ proof (rule DERIV_isconst3[where x=x])
+ fix x :: real assume "x \<in> {0 <..< 2}" hence "0 < x" and "x < 2" by auto
+ have "norm (1 - x) < 1" using `0 < x` and `x < 2` by auto
+ have "1 / x = 1 / (1 - (1 - x))" by auto
+ also have "\<dots> = (\<Sum> n. (1 - x)^n)" using geometric_sums[OF `norm (1 - x) < 1`] by (rule sums_unique)
+ also have "\<dots> = suminf (?f' x)" unfolding power_mult_distrib[symmetric] by (rule arg_cong[where f=suminf], rule arg_cong[where f="op ^"], auto)
+ finally have "DERIV ln x :> suminf (?f' x)" using DERIV_ln[OF `0 < x`] unfolding real_divide_def by auto
+ moreover
+ have repos: "\<And> h x :: real. h - 1 + x = h + x - 1" by auto
+ have "DERIV (\<lambda>x. suminf (?f x)) (x - 1) :> (\<Sum>n. (-1)^n * (1 / real (n + 1)) * real (Suc n) * (x - 1) ^ n)"
+ proof (rule DERIV_power_series')
+ show "x - 1 \<in> {- 1<..<1}" and "(0 :: real) < 1" using `0 < x` `x < 2` by auto
+ { fix x :: real assume "x \<in> {- 1<..<1}" hence "norm (-x) < 1" by auto
+ show "summable (\<lambda>n. -1 ^ n * (1 / real (n + 1)) * real (Suc n) * x ^ n)"
+ by (auto simp del: power_mult_distrib simp add: power_mult_distrib[symmetric] summable_geometric[OF `norm (-x) < 1`])
+ }
+ qed
+ hence "DERIV (\<lambda>x. suminf (?f x)) (x - 1) :> suminf (?f' x)" by auto
+ hence "DERIV (\<lambda>x. suminf (?f (x - 1))) x :> suminf (?f' x)" unfolding DERIV_iff repos .
+ ultimately have "DERIV (\<lambda>x. ln x - suminf (?f (x - 1))) x :> (suminf (?f' x) - suminf (?f' x))"
+ by (rule DERIV_diff)
+ thus "DERIV (\<lambda>x. ln x - suminf (?f (x - 1))) x :> 0" by auto
+ qed (auto simp add: assms)
+ thus ?thesis by (auto simp add: suminf_zero)
+qed
subsection {* Sine and Cosine *}
@@ -1378,6 +1793,12 @@
lemma minus_pi_half_less_zero: "-(pi/2) < 0"
by simp
+lemma m2pi_less_pi: "- (2 * pi) < pi"
+proof -
+ have "- (2 * pi) < 0" and "0 < pi" by auto
+ from order_less_trans[OF this] show ?thesis .
+qed
+
lemma sin_pi_half [simp]: "sin(pi/2) = 1"
apply (cut_tac x = "pi/2" in sin_cos_squared_add2)
apply (cut_tac sin_gt_zero [OF pi_half_gt_zero pi_half_less_two])
@@ -1487,6 +1908,24 @@
apply (auto intro!: cos_gt_zero_pi simp del: sin_cos_eq [symmetric])
done
+
+lemma pi_ge_two: "2 \<le> pi"
+proof (rule ccontr)
+ assume "\<not> 2 \<le> pi" hence "pi < 2" by auto
+ have "\<exists>y > pi. y < 2 \<and> y < 2 * pi"
+ proof (cases "2 < 2 * pi")
+ case True with dense[OF `pi < 2`] show ?thesis by auto
+ next
+ case False have "pi < 2 * pi" by auto
+ from dense[OF this] and False show ?thesis by auto
+ qed
+ then obtain y where "pi < y" and "y < 2" and "y < 2 * pi" by blast
+ hence "0 < sin y" using sin_gt_zero by auto
+ moreover
+ have "sin y < 0" using sin_gt_zero_pi[of "y - pi"] `pi < y` and `y < 2 * pi` sin_periodic_pi[of "y - pi"] by auto
+ ultimately show False by auto
+qed
+
lemma sin_ge_zero: "[| 0 \<le> x; x \<le> pi |] ==> 0 \<le> sin x"
by (auto simp add: order_le_less sin_gt_zero_pi)
@@ -1586,6 +2025,48 @@
apply (auto simp add: even_mult_two_ex)
done
+lemma cos_monotone_0_pi: assumes "0 \<le> y" and "y < x" and "x \<le> pi"
+ shows "cos x < cos y"
+proof -
+ have "- (x - y) < 0" by (auto!)
+
+ from MVT2[OF `y < x` DERIV_cos[THEN impI, THEN allI]]
+ obtain z where "y < z" and "z < x" and cos_diff: "cos x - cos y = (x - y) * - sin z" by auto
+ hence "0 < z" and "z < pi" by (auto!)
+ hence "0 < sin z" using sin_gt_zero_pi by auto
+ hence "cos x - cos y < 0" unfolding cos_diff minus_mult_commute[symmetric] using `- (x - y) < 0` by (rule mult_pos_neg2)
+ thus ?thesis by auto
+qed
+
+lemma cos_monotone_0_pi': assumes "0 \<le> y" and "y \<le> x" and "x \<le> pi" shows "cos x \<le> cos y"
+proof (cases "y < x")
+ case True show ?thesis using cos_monotone_0_pi[OF `0 \<le> y` True `x \<le> pi`] by auto
+next
+ case False hence "y = x" using `y \<le> x` by auto
+ thus ?thesis by auto
+qed
+
+lemma cos_monotone_minus_pi_0: assumes "-pi \<le> y" and "y < x" and "x \<le> 0"
+ shows "cos y < cos x"
+proof -
+ have "0 \<le> -x" and "-x < -y" and "-y \<le> pi" by (auto!)
+ from cos_monotone_0_pi[OF this]
+ show ?thesis unfolding cos_minus .
+qed
+
+lemma cos_monotone_minus_pi_0': assumes "-pi \<le> y" and "y \<le> x" and "x \<le> 0" shows "cos y \<le> cos x"
+proof (cases "y < x")
+ case True show ?thesis using cos_monotone_minus_pi_0[OF `-pi \<le> y` True `x \<le> 0`] by auto
+next
+ case False hence "y = x" using `y \<le> x` by auto
+ thus ?thesis by auto
+qed
+
+lemma sin_monotone_2pi': assumes "- (pi / 2) \<le> y" and "y \<le> x" and "x \<le> pi / 2" shows "sin y \<le> sin x"
+proof -
+ have "0 \<le> y + pi / 2" and "y + pi / 2 \<le> x + pi / 2" and "x + pi /2 \<le> pi" using pi_ge_two by (auto!)
+ from cos_monotone_0_pi'[OF this] show ?thesis unfolding minus_sin_cos_eq[symmetric] by auto
+qed
subsection {* Tangent *}
@@ -1653,6 +2134,22 @@
thus ?thesis by simp
qed
+lemma tan_half: fixes x :: real assumes "- (pi / 2) < x" and "x < pi / 2"
+ shows "tan x = sin (2 * x) / (cos (2 * x) + 1)"
+proof -
+ from cos_gt_zero_pi[OF `- (pi / 2) < x` `x < pi / 2`]
+ have "cos x \<noteq> 0" by auto
+
+ have minus_cos_2x: "\<And>X. X - cos (2*x) = X - (cos x) ^ 2 + (sin x) ^ 2" unfolding cos_double by algebra
+
+ have "tan x = (tan x + tan x) / 2" by auto
+ also have "\<dots> = sin (x + x) / (cos x * cos x) / 2" unfolding add_tan_eq[OF `cos x \<noteq> 0` `cos x \<noteq> 0`] ..
+ also have "\<dots> = sin (2 * x) / ((cos x) ^ 2 + (cos x) ^ 2 + cos (2*x) - cos (2*x))" unfolding divide_divide_eq_left numeral_2_eq_2 by auto
+ also have "\<dots> = sin (2 * x) / ((cos x) ^ 2 + cos (2*x) + (sin x)^2)" unfolding minus_cos_2x by auto
+ also have "\<dots> = sin (2 * x) / (cos (2*x) + 1)" by auto
+ finally show ?thesis .
+qed
+
lemma lemma_DERIV_tan:
"cos x \<noteq> 0 ==> DERIV (%x. sin(x)/cos(x)) x :> inverse((cos x)\<twosuperior>)"
apply (rule lemma_DERIV_subst)
@@ -1726,6 +2223,73 @@
simp add: cos_gt_zero_pi [THEN less_imp_neq, THEN not_sym])
done
+lemma tan_monotone: assumes "- (pi / 2) < y" and "y < x" and "x < pi / 2"
+ shows "tan y < tan x"
+proof -
+ have "\<forall> x'. y \<le> x' \<and> x' \<le> x \<longrightarrow> DERIV tan x' :> inverse (cos x'^2)"
+ proof (rule allI, rule impI)
+ fix x' :: real assume "y \<le> x' \<and> x' \<le> x"
+ hence "-(pi/2) < x'" and "x' < pi/2" by (auto!)
+ from cos_gt_zero_pi[OF this]
+ have "cos x' \<noteq> 0" by auto
+ thus "DERIV tan x' :> inverse (cos x'^2)" by (rule DERIV_tan)
+ qed
+ from MVT2[OF `y < x` this]
+ obtain z where "y < z" and "z < x" and tan_diff: "tan x - tan y = (x - y) * inverse ((cos z)\<twosuperior>)" by auto
+ hence "- (pi / 2) < z" and "z < pi / 2" by (auto!)
+ hence "0 < cos z" using cos_gt_zero_pi by auto
+ hence inv_pos: "0 < inverse ((cos z)\<twosuperior>)" by auto
+ have "0 < x - y" using `y < x` by auto
+ from real_mult_order[OF this inv_pos]
+ have "0 < tan x - tan y" unfolding tan_diff by auto
+ thus ?thesis by auto
+qed
+
+lemma tan_monotone': assumes "- (pi / 2) < y" and "y < pi / 2" and "- (pi / 2) < x" and "x < pi / 2"
+ shows "(y < x) = (tan y < tan x)"
+proof
+ assume "y < x" thus "tan y < tan x" using tan_monotone and `- (pi / 2) < y` and `x < pi / 2` by auto
+next
+ assume "tan y < tan x"
+ show "y < x"
+ proof (rule ccontr)
+ assume "\<not> y < x" hence "x \<le> y" by auto
+ hence "tan x \<le> tan y"
+ proof (cases "x = y")
+ case True thus ?thesis by auto
+ next
+ case False hence "x < y" using `x \<le> y` by auto
+ from tan_monotone[OF `- (pi/2) < x` this `y < pi / 2`] show ?thesis by auto
+ qed
+ thus False using `tan y < tan x` by auto
+ qed
+qed
+
+lemma tan_inverse: "1 / (tan y) = tan (pi / 2 - y)" unfolding tan_def sin_cos_eq[of y] cos_sin_eq[of y] by auto
+
+lemma tan_periodic_pi[simp]: "tan (x + pi) = tan x"
+ by (simp add: tan_def)
+
+lemma tan_periodic_nat[simp]: fixes n :: nat shows "tan (x + real n * pi) = tan x"
+proof (induct n arbitrary: x)
+ case (Suc n)
+ have split_pi_off: "x + real (Suc n) * pi = (x + real n * pi) + pi" unfolding Suc_plus1 real_of_nat_add real_of_one real_add_mult_distrib by auto
+ show ?case unfolding split_pi_off using Suc by auto
+qed auto
+
+lemma tan_periodic_int[simp]: fixes i :: int shows "tan (x + real i * pi) = tan x"
+proof (cases "0 \<le> i")
+ case True hence i_nat: "real i = real (nat i)" by auto
+ show ?thesis unfolding i_nat by auto
+next
+ case False hence i_nat: "real i = - real (nat (-i))" by auto
+ have "tan x = tan (x + real i * pi - real i * pi)" by auto
+ also have "\<dots> = tan (x + real i * pi)" unfolding i_nat mult_minus_left diff_minus_eq_add by (rule tan_periodic_nat)
+ finally show ?thesis by auto
+qed
+
+lemma tan_periodic_n[simp]: "tan (x + number_of n * pi) = tan x"
+ using tan_periodic_int[of _ "number_of n" ] unfolding real_number_of .
subsection {* Inverse Trigonometric Functions *}
@@ -1968,7 +2532,6 @@
apply (simp, simp, simp, rule isCont_arctan)
done
-
subsection {* More Theorems about Sin and Cos *}
lemma cos_45: "cos (pi / 4) = sqrt 2 / 2"
@@ -2115,6 +2678,464 @@
lemma cos_one_sin_zero: "cos x = 1 ==> sin x = 0"
by (cut_tac x = x in sin_cos_squared_add3, auto)
+subsection {* Machins formula *}
+
+lemma tan_total_pi4: assumes "\<bar>x\<bar> < 1"
+ shows "\<exists> z. - (pi / 4) < z \<and> z < pi / 4 \<and> tan z = x"
+proof -
+ obtain z where "- (pi / 2) < z" and "z < pi / 2" and "tan z = x" using tan_total by blast
+ have "tan (pi / 4) = 1" and "tan (- (pi / 4)) = - 1" using tan_45 tan_minus by auto
+ have "z \<noteq> pi / 4"
+ proof (rule ccontr)
+ assume "\<not> (z \<noteq> pi / 4)" hence "z = pi / 4" by auto
+ have "tan z = 1" unfolding `z = pi / 4` `tan (pi / 4) = 1` ..
+ thus False unfolding `tan z = x` using `\<bar>x\<bar> < 1` by auto
+ qed
+ have "z \<noteq> - (pi / 4)"
+ proof (rule ccontr)
+ assume "\<not> (z \<noteq> - (pi / 4))" hence "z = - (pi / 4)" by auto
+ have "tan z = - 1" unfolding `z = - (pi / 4)` `tan (- (pi / 4)) = - 1` ..
+ thus False unfolding `tan z = x` using `\<bar>x\<bar> < 1` by auto
+ qed
+
+ have "z < pi / 4"
+ proof (rule ccontr)
+ assume "\<not> (z < pi / 4)" hence "pi / 4 < z" using `z \<noteq> pi / 4` by auto
+ have "- (pi / 2) < pi / 4" using m2pi_less_pi by auto
+ from tan_monotone[OF this `pi / 4 < z` `z < pi / 2`]
+ have "1 < x" unfolding `tan z = x` `tan (pi / 4) = 1` .
+ thus False using `\<bar>x\<bar> < 1` by auto
+ qed
+ moreover
+ have "-(pi / 4) < z"
+ proof (rule ccontr)
+ assume "\<not> (-(pi / 4) < z)" hence "z < - (pi / 4)" using `z \<noteq> - (pi / 4)` by auto
+ have "-(pi / 4) < pi / 2" using m2pi_less_pi by auto
+ from tan_monotone[OF `-(pi / 2) < z` `z < -(pi / 4)` this]
+ have "x < - 1" unfolding `tan z = x` `tan (-(pi / 4)) = - 1` .
+ thus False using `\<bar>x\<bar> < 1` by auto
+ qed
+ ultimately show ?thesis using `tan z = x` by auto
+qed
+
+lemma arctan_add: assumes "\<bar>x\<bar> \<le> 1" and "\<bar>y\<bar> < 1"
+ shows "arctan x + arctan y = arctan ((x + y) / (1 - x * y))"
+proof -
+ obtain y' where "-(pi/4) < y'" and "y' < pi/4" and "tan y' = y" using tan_total_pi4[OF `\<bar>y\<bar> < 1`] by blast
+
+ have "pi / 4 < pi / 2" by auto
+
+ have "\<exists> x'. -(pi/4) \<le> x' \<and> x' \<le> pi/4 \<and> tan x' = x"
+ proof (cases "\<bar>x\<bar> < 1")
+ case True from tan_total_pi4[OF this] obtain x' where "-(pi/4) < x'" and "x' < pi/4" and "tan x' = x" by blast
+ hence "-(pi/4) \<le> x'" and "x' \<le> pi/4" and "tan x' = x" by auto
+ thus ?thesis by auto
+ next
+ case False
+ show ?thesis
+ proof (cases "x = 1")
+ case True hence "tan (pi/4) = x" using tan_45 by auto
+ moreover
+ have "- pi \<le> pi" unfolding minus_le_self_iff by auto
+ hence "-(pi/4) \<le> pi/4" and "pi/4 \<le> pi/4" by auto
+ ultimately show ?thesis by blast
+ next
+ case False hence "x = -1" using `\<not> \<bar>x\<bar> < 1` and `\<bar>x\<bar> \<le> 1` by auto
+ hence "tan (-(pi/4)) = x" using tan_45 tan_minus by auto
+ moreover
+ have "- pi \<le> pi" unfolding minus_le_self_iff by auto
+ hence "-(pi/4) \<le> pi/4" and "-(pi/4) \<le> -(pi/4)" by auto
+ ultimately show ?thesis by blast
+ qed
+ qed
+ then obtain x' where "-(pi/4) \<le> x'" and "x' \<le> pi/4" and "tan x' = x" by blast
+ hence "-(pi/2) < x'" and "x' < pi/2" using order_le_less_trans[OF `x' \<le> pi/4` `pi / 4 < pi / 2`] by auto
+
+ have "cos x' \<noteq> 0" using cos_gt_zero_pi[THEN less_imp_neq] and `-(pi/2) < x'` and `x' < pi/2` by auto
+ moreover have "cos y' \<noteq> 0" using cos_gt_zero_pi[THEN less_imp_neq] and `-(pi/4) < y'` and `y' < pi/4` by auto
+ ultimately have "cos x' * cos y' \<noteq> 0" by auto
+
+ have divide_nonzero_divide: "\<And> A B C :: real. C \<noteq> 0 \<Longrightarrow> (A / C) / (B / C) = A / B" by auto
+ have divide_mult_commute: "\<And> A B C D :: real. A * B / (C * D) = (A / C) * (B / D)" by auto
+
+ have "tan (x' + y') = sin (x' + y') / (cos x' * cos y' - sin x' * sin y')" unfolding tan_def cos_add ..
+ also have "\<dots> = (tan x' + tan y') / ((cos x' * cos y' - sin x' * sin y') / (cos x' * cos y'))" unfolding add_tan_eq[OF `cos x' \<noteq> 0` `cos y' \<noteq> 0`] divide_nonzero_divide[OF `cos x' * cos y' \<noteq> 0`] ..
+ also have "\<dots> = (tan x' + tan y') / (1 - tan x' * tan y')" unfolding tan_def diff_divide_distrib divide_self[OF `cos x' * cos y' \<noteq> 0`] unfolding divide_mult_commute ..
+ finally have tan_eq: "tan (x' + y') = (x + y) / (1 - x * y)" unfolding `tan y' = y` `tan x' = x` .
+
+ have "arctan (tan (x' + y')) = x' + y'" using `-(pi/4) < y'` `-(pi/4) \<le> x'` `y' < pi/4` and `x' \<le> pi/4` by (auto intro!: arctan_tan)
+ moreover have "arctan (tan (x')) = x'" using `-(pi/2) < x'` and `x' < pi/2` by (auto intro!: arctan_tan)
+ moreover have "arctan (tan (y')) = y'" using `-(pi/4) < y'` and `y' < pi/4` by (auto intro!: arctan_tan)
+ ultimately have "arctan x + arctan y = arctan (tan (x' + y'))" unfolding `tan y' = y` [symmetric] `tan x' = x`[symmetric] by auto
+ thus "arctan x + arctan y = arctan ((x + y) / (1 - x * y))" unfolding tan_eq .
+qed
+
+lemma arctan1_eq_pi4: "arctan 1 = pi / 4" unfolding tan_45[symmetric] by (rule arctan_tan, auto simp add: m2pi_less_pi)
+
+theorem machin: "pi / 4 = 4 * arctan (1/5) - arctan (1 / 239)"
+proof -
+ have "\<bar>1 / 5\<bar> < (1 :: real)" by auto
+ from arctan_add[OF less_imp_le[OF this] this]
+ have "2 * arctan (1 / 5) = arctan (5 / 12)" by auto
+ moreover
+ have "\<bar>5 / 12\<bar> < (1 :: real)" by auto
+ from arctan_add[OF less_imp_le[OF this] this]
+ have "2 * arctan (5 / 12) = arctan (120 / 119)" by auto
+ moreover
+ have "\<bar>1\<bar> \<le> (1::real)" and "\<bar>1 / 239\<bar> < (1::real)" by auto
+ from arctan_add[OF this]
+ have "arctan 1 + arctan (1 / 239) = arctan (120 / 119)" by auto
+ ultimately have "arctan 1 + arctan (1 / 239) = 4 * arctan (1 / 5)" by auto
+ thus ?thesis unfolding arctan1_eq_pi4 by algebra
+qed
+subsection {* Introducing the arcus tangens power series *}
+
+lemma monoseq_arctan_series: fixes x :: real
+ assumes "\<bar>x\<bar> \<le> 1" shows "monoseq (\<lambda> n. 1 / real (n*2+1) * x^(n*2+1))" (is "monoseq ?a")
+proof (cases "x = 0") case True thus ?thesis unfolding monoseq_def by auto
+next
+ case False
+ have "norm x \<le> 1" and "x \<le> 1" and "-1 \<le> x" using assms by auto
+ show "monoseq ?a"
+ proof -
+ { fix n fix x :: real assume "0 \<le> x" and "x \<le> 1"
+ have "1 / real (Suc (Suc n * 2)) * x ^ Suc (Suc n * 2) \<le> 1 / real (Suc (n * 2)) * x ^ Suc (n * 2)"
+ proof (rule mult_mono)
+ show "1 / real (Suc (Suc n * 2)) \<le> 1 / real (Suc (n * 2))" by (rule frac_le) simp_all
+ show "0 \<le> 1 / real (Suc (n * 2))" by auto
+ show "x ^ Suc (Suc n * 2) \<le> x ^ Suc (n * 2)" by (rule power_decreasing) (simp_all add: `0 \<le> x` `x \<le> 1`)
+ show "0 \<le> x ^ Suc (Suc n * 2)" by (rule zero_le_power) (simp add: `0 \<le> x`)
+ qed
+ } note mono = this
+
+ show ?thesis
+ proof (cases "0 \<le> x")
+ case True from mono[OF this `x \<le> 1`, THEN allI]
+ show ?thesis unfolding Suc_plus1[symmetric] by (rule mono_SucI2)
+ next
+ case False hence "0 \<le> -x" and "-x \<le> 1" using `-1 \<le> x` by auto
+ from mono[OF this]
+ have "\<And>n. 1 / real (Suc (Suc n * 2)) * x ^ Suc (Suc n * 2) \<ge> 1 / real (Suc (n * 2)) * x ^ Suc (n * 2)" using `0 \<le> -x` by auto
+ thus ?thesis unfolding Suc_plus1[symmetric] by (rule mono_SucI1[OF allI])
+ qed
+ qed
+qed
+
+lemma zeroseq_arctan_series: fixes x :: real
+ assumes "\<bar>x\<bar> \<le> 1" shows "(\<lambda> n. 1 / real (n*2+1) * x^(n*2+1)) ----> 0" (is "?a ----> 0")
+proof (cases "x = 0") case True thus ?thesis by (auto simp add: LIMSEQ_const)
+next
+ case False
+ have "norm x \<le> 1" and "x \<le> 1" and "-1 \<le> x" using assms by auto
+ show "?a ----> 0"
+ proof (cases "\<bar>x\<bar> < 1")
+ case True hence "norm x < 1" by auto
+ from LIMSEQ_mult[OF LIMSEQ_inverse_real_of_nat LIMSEQ_power_zero[OF `norm x < 1`, THEN LIMSEQ_Suc]]
+ show ?thesis unfolding inverse_eq_divide Suc_plus1 using LIMSEQ_linear[OF _ pos2] by auto
+ next
+ case False hence "x = -1 \<or> x = 1" using `\<bar>x\<bar> \<le> 1` by auto
+ hence n_eq: "\<And> n. x ^ (n * 2 + 1) = x" by auto
+ from LIMSEQ_mult[OF LIMSEQ_inverse_real_of_nat[THEN LIMSEQ_linear, OF pos2, unfolded inverse_eq_divide] LIMSEQ_const[of x]]
+ show ?thesis unfolding n_eq by auto
+ qed
+qed
+
+lemma summable_arctan_series: fixes x :: real and n :: nat
+ assumes "\<bar>x\<bar> \<le> 1" shows "summable (\<lambda> k. (-1)^k * (1 / real (k*2+1) * x ^ (k*2+1)))" (is "summable (?c x)")
+ by (rule summable_Leibniz(1), rule zeroseq_arctan_series[OF assms], rule monoseq_arctan_series[OF assms])
+
+lemma less_one_imp_sqr_less_one: fixes x :: real assumes "\<bar>x\<bar> < 1" shows "x^2 < 1"
+proof -
+ from mult_mono1[OF less_imp_le[OF `\<bar>x\<bar> < 1`] abs_ge_zero[of x]]
+ have "\<bar> x^2 \<bar> < 1" using `\<bar> x \<bar> < 1` unfolding numeral_2_eq_2 power_Suc2 by auto
+ thus ?thesis using zero_le_power2 by auto
+qed
+
+lemma DERIV_arctan_series: assumes "\<bar> x \<bar> < 1"
+ shows "DERIV (\<lambda> x'. \<Sum> k. (-1)^k * (1 / real (k*2+1) * x' ^ (k*2+1))) x :> (\<Sum> k. (-1)^k * x^(k*2))" (is "DERIV ?arctan _ :> ?Int")
+proof -
+ let "?f n" = "if even n then (-1)^(n div 2) * 1 / real (Suc n) else 0"
+
+ { fix n :: nat assume "even n" hence "2 * (n div 2) = n" by presburger } note n_even=this
+ have if_eq: "\<And> n x'. ?f n * real (Suc n) * x'^n = (if even n then (-1)^(n div 2) * x'^(2 * (n div 2)) else 0)" using n_even by auto
+
+ { fix x :: real assume "\<bar>x\<bar> < 1" hence "x^2 < 1" by (rule less_one_imp_sqr_less_one)
+ have "summable (\<lambda> n. -1 ^ n * (x^2) ^n)"
+ by (rule summable_Leibniz(1), auto intro!: LIMSEQ_realpow_zero monoseq_realpow `x^2 < 1` order_less_imp_le[OF `x^2 < 1`])
+ hence "summable (\<lambda> n. -1 ^ n * x^(2*n))" unfolding power_mult .
+ } note summable_Integral = this
+
+ { fix f :: "nat \<Rightarrow> real"
+ have "\<And> x. f sums x = (\<lambda> n. if even n then f (n div 2) else 0) sums x"
+ proof
+ fix x :: real assume "f sums x"
+ from sums_if[OF sums_zero this]
+ show "(\<lambda> n. if even n then f (n div 2) else 0) sums x" by auto
+ next
+ fix x :: real assume "(\<lambda> n. if even n then f (n div 2) else 0) sums x"
+ from LIMSEQ_linear[OF this[unfolded sums_def] pos2, unfolded sum_split_even_odd[unfolded mult_commute]]
+ show "f sums x" unfolding sums_def by auto
+ qed
+ hence "op sums f = op sums (\<lambda> n. if even n then f (n div 2) else 0)" ..
+ } note sums_even = this
+
+ have Int_eq: "(\<Sum> n. ?f n * real (Suc n) * x^n) = ?Int" unfolding if_eq mult_commute[of _ 2] suminf_def sums_even[of "\<lambda> n. -1 ^ n * x ^ (2 * n)", symmetric]
+ by auto
+
+ { fix x :: real
+ have if_eq': "\<And> n. (if even n then -1 ^ (n div 2) * 1 / real (Suc n) else 0) * x ^ Suc n =
+ (if even n then -1 ^ (n div 2) * (1 / real (Suc (2 * (n div 2))) * x ^ Suc (2 * (n div 2))) else 0)"
+ using n_even by auto
+ have idx_eq: "\<And> n. n * 2 + 1 = Suc (2 * n)" by auto
+ have "(\<Sum> n. ?f n * x^(Suc n)) = ?arctan x" unfolding if_eq' idx_eq suminf_def sums_even[of "\<lambda> n. -1 ^ n * (1 / real (Suc (2 * n)) * x ^ Suc (2 * n))", symmetric]
+ by auto
+ } note arctan_eq = this
+
+ have "DERIV (\<lambda> x. \<Sum> n. ?f n * x^(Suc n)) x :> (\<Sum> n. ?f n * real (Suc n) * x^n)"
+ proof (rule DERIV_power_series')
+ show "x \<in> {- 1 <..< 1}" using `\<bar> x \<bar> < 1` by auto
+ { fix x' :: real assume x'_bounds: "x' \<in> {- 1 <..< 1}"
+ hence "\<bar>x'\<bar> < 1" by auto
+
+ let ?S = "\<Sum> n. (-1)^n * x'^(2 * n)"
+ show "summable (\<lambda> n. ?f n * real (Suc n) * x'^n)" unfolding if_eq
+ by (rule sums_summable[where l="0 + ?S"], rule sums_if, rule sums_zero, rule summable_sums, rule summable_Integral[OF `\<bar>x'\<bar> < 1`])
+ }
+ qed auto
+ thus ?thesis unfolding Int_eq arctan_eq .
+qed
+
+lemma arctan_series: assumes "\<bar> x \<bar> \<le> 1"
+ shows "arctan x = (\<Sum> k. (-1)^k * (1 / real (k*2+1) * x ^ (k*2+1)))" (is "_ = suminf (\<lambda> n. ?c x n)")
+proof -
+ let "?c' x n" = "(-1)^n * x^(n*2)"
+
+ { fix r x :: real assume "0 < r" and "r < 1" and "\<bar> x \<bar> < r"
+ have "\<bar>x\<bar> < 1" using `r < 1` and `\<bar>x\<bar> < r` by auto
+ from DERIV_arctan_series[OF this]
+ have "DERIV (\<lambda> x. suminf (?c x)) x :> (suminf (?c' x))" .
+ } note DERIV_arctan_suminf = this
+
+ { fix x :: real assume "\<bar>x\<bar> \<le> 1" note summable_Leibniz[OF zeroseq_arctan_series[OF this] monoseq_arctan_series[OF this]] }
+ note arctan_series_borders = this
+
+ { fix x :: real assume "\<bar>x\<bar> < 1" have "arctan x = (\<Sum> k. ?c x k)"
+ proof -
+ obtain r where "\<bar>x\<bar> < r" and "r < 1" using dense[OF `\<bar>x\<bar> < 1`] by blast
+ hence "0 < r" and "-r < x" and "x < r" by auto
+
+ have suminf_eq_arctan_bounded: "\<And> x a b. \<lbrakk> -r < a ; b < r ; a < b ; a \<le> x ; x \<le> b \<rbrakk> \<Longrightarrow> suminf (?c x) - arctan x = suminf (?c a) - arctan a"
+ proof -
+ fix x a b assume "-r < a" and "b < r" and "a < b" and "a \<le> x" and "x \<le> b"
+ hence "\<bar>x\<bar> < r" by auto
+ show "suminf (?c x) - arctan x = suminf (?c a) - arctan a"
+ proof (rule DERIV_isconst2[of "a" "b"])
+ show "a < b" and "a \<le> x" and "x \<le> b" using `a < b` `a \<le> x` `x \<le> b` by auto
+ have "\<forall> x. -r < x \<and> x < r \<longrightarrow> DERIV (\<lambda> x. suminf (?c x) - arctan x) x :> 0"
+ proof (rule allI, rule impI)
+ fix x assume "-r < x \<and> x < r" hence "\<bar>x\<bar> < r" by auto
+ hence "\<bar>x\<bar> < 1" using `r < 1` by auto
+ have "\<bar> - (x^2) \<bar> < 1" using less_one_imp_sqr_less_one[OF `\<bar>x\<bar> < 1`] by auto
+ hence "(\<lambda> n. (- (x^2)) ^ n) sums (1 / (1 - (- (x^2))))" unfolding real_norm_def[symmetric] by (rule geometric_sums)
+ hence "(?c' x) sums (1 / (1 - (- (x^2))))" unfolding power_mult_distrib[symmetric] power_mult nat_mult_commute[of _ 2] by auto
+ hence suminf_c'_eq_geom: "inverse (1 + x^2) = suminf (?c' x)" using sums_unique unfolding inverse_eq_divide by auto
+ have "DERIV (\<lambda> x. suminf (?c x)) x :> (inverse (1 + x^2))" unfolding suminf_c'_eq_geom
+ by (rule DERIV_arctan_suminf[OF `0 < r` `r < 1` `\<bar>x\<bar> < r`])
+ from DERIV_add_minus[OF this DERIV_arctan]
+ show "DERIV (\<lambda> x. suminf (?c x) - arctan x) x :> 0" unfolding diff_minus by auto
+ qed
+ hence DERIV_in_rball: "\<forall> y. a \<le> y \<and> y \<le> b \<longrightarrow> DERIV (\<lambda> x. suminf (?c x) - arctan x) y :> 0" using `-r < a` `b < r` by auto
+ thus "\<forall> y. a < y \<and> y < b \<longrightarrow> DERIV (\<lambda> x. suminf (?c x) - arctan x) y :> 0" using `\<bar>x\<bar> < r` by auto
+ show "\<forall> y. a \<le> y \<and> y \<le> b \<longrightarrow> isCont (\<lambda> x. suminf (?c x) - arctan x) y" using DERIV_in_rball DERIV_isCont by auto
+ qed
+ qed
+
+ have suminf_arctan_zero: "suminf (?c 0) - arctan 0 = 0"
+ unfolding Suc_plus1[symmetric] power_Suc2 mult_zero_right arctan_zero_zero suminf_zero by auto
+
+ have "suminf (?c x) - arctan x = 0"
+ proof (cases "x = 0")
+ case True thus ?thesis using suminf_arctan_zero by auto
+ next
+ case False hence "0 < \<bar>x\<bar>" and "- \<bar>x\<bar> < \<bar>x\<bar>" by auto
+ have "suminf (?c (-\<bar>x\<bar>)) - arctan (-\<bar>x\<bar>) = suminf (?c 0) - arctan 0"
+ by (rule suminf_eq_arctan_bounded[where x="0" and a="-\<bar>x\<bar>" and b="\<bar>x\<bar>", symmetric], auto simp add: `\<bar>x\<bar> < r` `-\<bar>x\<bar> < \<bar>x\<bar>`)
+ moreover
+ have "suminf (?c x) - arctan x = suminf (?c (-\<bar>x\<bar>)) - arctan (-\<bar>x\<bar>)"
+ by (rule suminf_eq_arctan_bounded[where x="x" and a="-\<bar>x\<bar>" and b="\<bar>x\<bar>"], auto simp add: `\<bar>x\<bar> < r` `-\<bar>x\<bar> < \<bar>x\<bar>`)
+ ultimately
+ show ?thesis using suminf_arctan_zero by auto
+ qed
+ thus ?thesis by auto
+ qed } note when_less_one = this
+
+ show "arctan x = suminf (\<lambda> n. ?c x n)"
+ proof (cases "\<bar>x\<bar> < 1")
+ case True thus ?thesis by (rule when_less_one)
+ next case False hence "\<bar>x\<bar> = 1" using `\<bar>x\<bar> \<le> 1` by auto
+ let "?a x n" = "\<bar>1 / real (n*2+1) * x^(n*2+1)\<bar>"
+ let "?diff x n" = "\<bar> arctan x - (\<Sum> i = 0..<n. ?c x i)\<bar>"
+ { fix n :: nat
+ have "0 < (1 :: real)" by auto
+ moreover
+ { fix x :: real assume "0 < x" and "x < 1" hence "\<bar>x\<bar> \<le> 1" and "\<bar>x\<bar> < 1" by auto
+ from `0 < x` have "0 < 1 / real (0 * 2 + (1::nat)) * x ^ (0 * 2 + 1)" by auto
+ note bounds = mp[OF arctan_series_borders(2)[OF `\<bar>x\<bar> \<le> 1`] this, unfolded when_less_one[OF `\<bar>x\<bar> < 1`, symmetric], THEN spec]
+ have "0 < 1 / real (n*2+1) * x^(n*2+1)" by (rule mult_pos_pos, auto simp only: zero_less_power[OF `0 < x`], auto)
+ hence a_pos: "?a x n = 1 / real (n*2+1) * x^(n*2+1)" by (rule abs_of_pos)
+ have "?diff x n \<le> ?a x n"
+ proof (cases "even n")
+ case True hence sgn_pos: "(-1)^n = (1::real)" by auto
+ from `even n` obtain m where "2 * m = n" unfolding even_mult_two_ex by auto
+ from bounds[of m, unfolded this atLeastAtMost_iff]
+ have "\<bar>arctan x - (\<Sum>i = 0..<n. (?c x i))\<bar> \<le> (\<Sum>i = 0..<n + 1. (?c x i)) - (\<Sum>i = 0..<n. (?c x i))" by auto
+ also have "\<dots> = ?c x n" by auto
+ also have "\<dots> = ?a x n" unfolding sgn_pos a_pos by auto
+ finally show ?thesis .
+ next
+ case False hence sgn_neg: "(-1)^n = (-1::real)" by auto
+ from `odd n` obtain m where m_def: "2 * m + 1 = n" unfolding odd_Suc_mult_two_ex by auto
+ hence m_plus: "2 * (m + 1) = n + 1" by auto
+ from bounds[of "m + 1", unfolded this atLeastAtMost_iff, THEN conjunct1] bounds[of m, unfolded m_def atLeastAtMost_iff, THEN conjunct2]
+ have "\<bar>arctan x - (\<Sum>i = 0..<n. (?c x i))\<bar> \<le> (\<Sum>i = 0..<n. (?c x i)) - (\<Sum>i = 0..<n+1. (?c x i))" by auto
+ also have "\<dots> = - ?c x n" by auto
+ also have "\<dots> = ?a x n" unfolding sgn_neg a_pos by auto
+ finally show ?thesis .
+ qed
+ hence "0 \<le> ?a x n - ?diff x n" by auto
+ }
+ hence "\<forall> x \<in> { 0 <..< 1 }. 0 \<le> ?a x n - ?diff x n" by auto
+ moreover have "\<And>x. isCont (\<lambda> x. ?a x n - ?diff x n) x"
+ unfolding real_diff_def divide_inverse
+ by (auto intro!: isCont_add isCont_rabs isCont_ident isCont_minus isCont_arctan isCont_inverse isCont_mult isCont_power isCont_const isCont_setsum)
+ ultimately have "0 \<le> ?a 1 n - ?diff 1 n" by (rule LIM_less_bound)
+ hence "?diff 1 n \<le> ?a 1 n" by auto
+ }
+ have "?a 1 ----> 0" unfolding LIMSEQ_rabs_zero power_one divide_inverse by (auto intro!: LIMSEQ_mult LIMSEQ_linear LIMSEQ_inverse_real_of_nat)
+ have "?diff 1 ----> 0"
+ proof (rule LIMSEQ_I)
+ fix r :: real assume "0 < r"
+ obtain N :: nat where N_I: "\<And> n. N \<le> n \<Longrightarrow> ?a 1 n < r" using LIMSEQ_D[OF `?a 1 ----> 0` `0 < r`] by auto
+ { fix n assume "N \<le> n" from `?diff 1 n \<le> ?a 1 n` N_I[OF this]
+ have "norm (?diff 1 n - 0) < r" by auto }
+ thus "\<exists> N. \<forall> n \<ge> N. norm (?diff 1 n - 0) < r" by blast
+ qed
+ from this[unfolded LIMSEQ_rabs_zero real_diff_def add_commute[of "arctan 1"], THEN LIMSEQ_add_const, of "- arctan 1", THEN LIMSEQ_minus]
+ have "(?c 1) sums (arctan 1)" unfolding sums_def by auto
+ hence "arctan 1 = (\<Sum> i. ?c 1 i)" by (rule sums_unique)
+
+ show ?thesis
+ proof (cases "x = 1", simp add: `arctan 1 = (\<Sum> i. ?c 1 i)`)
+ assume "x \<noteq> 1" hence "x = -1" using `\<bar>x\<bar> = 1` by auto
+
+ have "- (pi / 2) < 0" using pi_gt_zero by auto
+ have "- (2 * pi) < 0" using pi_gt_zero by auto
+
+ have c_minus_minus: "\<And> i. ?c (- 1) i = - ?c 1 i" by auto
+
+ have "arctan (- 1) = arctan (tan (-(pi / 4)))" unfolding tan_45 tan_minus ..
+ also have "\<dots> = - (pi / 4)" by (rule arctan_tan, auto simp add: order_less_trans[OF `- (pi / 2) < 0` pi_gt_zero])
+ also have "\<dots> = - (arctan (tan (pi / 4)))" unfolding neg_equal_iff_equal by (rule arctan_tan[symmetric], auto simp add: order_less_trans[OF `- (2 * pi) < 0` pi_gt_zero])
+ also have "\<dots> = - (arctan 1)" unfolding tan_45 ..
+ also have "\<dots> = - (\<Sum> i. ?c 1 i)" using `arctan 1 = (\<Sum> i. ?c 1 i)` by auto
+ also have "\<dots> = (\<Sum> i. ?c (- 1) i)" using suminf_minus[OF sums_summable[OF `(?c 1) sums (arctan 1)`]] unfolding c_minus_minus by auto
+ finally show ?thesis using `x = -1` by auto
+ qed
+ qed
+qed
+
+lemma arctan_half: fixes x :: real
+ shows "arctan x = 2 * arctan (x / (1 + sqrt(1 + x^2)))"
+proof -
+ obtain y where low: "- (pi / 2) < y" and high: "y < pi / 2" and y_eq: "tan y = x" using tan_total by blast
+ hence low2: "- (pi / 2) < y / 2" and high2: "y / 2 < pi / 2" by auto
+
+ have divide_nonzero_divide: "\<And> A B C :: real. C \<noteq> 0 \<Longrightarrow> A / B = (A / C) / (B / C)" by auto
+
+ have "0 < cos y" using cos_gt_zero_pi[OF low high] .
+ hence "cos y \<noteq> 0" and cos_sqrt: "sqrt ((cos y) ^ 2) = cos y" by auto
+
+ have "1 + (tan y)^2 = 1 + sin y^2 / cos y^2" unfolding tan_def power_divide ..
+ also have "\<dots> = cos y^2 / cos y^2 + sin y^2 / cos y^2" using `cos y \<noteq> 0` by auto
+ also have "\<dots> = 1 / cos y^2" unfolding add_divide_distrib[symmetric] sin_cos_squared_add2 ..
+ finally have "1 + (tan y)^2 = 1 / cos y^2" .
+
+ have "sin y / (cos y + 1) = tan y / ((cos y + 1) / cos y)" unfolding tan_def divide_nonzero_divide[OF `cos y \<noteq> 0`, symmetric] ..
+ also have "\<dots> = tan y / (1 + 1 / cos y)" using `cos y \<noteq> 0` unfolding add_divide_distrib by auto
+ also have "\<dots> = tan y / (1 + 1 / sqrt(cos y^2))" unfolding cos_sqrt ..
+ also have "\<dots> = tan y / (1 + sqrt(1 / cos y^2))" unfolding real_sqrt_divide by auto
+ finally have eq: "sin y / (cos y + 1) = tan y / (1 + sqrt(1 + (tan y)^2))" unfolding `1 + (tan y)^2 = 1 / cos y^2` .
+
+ have "arctan x = y" using arctan_tan low high y_eq by auto
+ also have "\<dots> = 2 * (arctan (tan (y/2)))" using arctan_tan[OF low2 high2] by auto
+ also have "\<dots> = 2 * (arctan (sin y / (cos y + 1)))" unfolding tan_half[OF low2 high2] by auto
+ finally show ?thesis unfolding eq `tan y = x` .
+qed
+
+lemma arctan_monotone: assumes "x < y"
+ shows "arctan x < arctan y"
+proof -
+ obtain z where "-(pi / 2) < z" and "z < pi / 2" and "tan z = x" using tan_total by blast
+ obtain w where "-(pi / 2) < w" and "w < pi / 2" and "tan w = y" using tan_total by blast
+ have "z < w" unfolding tan_monotone'[OF `-(pi / 2) < z` `z < pi / 2` `-(pi / 2) < w` `w < pi / 2`] `tan z = x` `tan w = y` using `x < y` .
+ thus ?thesis
+ unfolding `tan z = x`[symmetric] arctan_tan[OF `-(pi / 2) < z` `z < pi / 2`]
+ unfolding `tan w = y`[symmetric] arctan_tan[OF `-(pi / 2) < w` `w < pi / 2`] .
+qed
+
+lemma arctan_monotone': assumes "x \<le> y" shows "arctan x \<le> arctan y"
+proof (cases "x = y")
+ case False hence "x < y" using `x \<le> y` by auto from arctan_monotone[OF this] show ?thesis by auto
+qed auto
+
+lemma arctan_minus: "arctan (- x) = - arctan x"
+proof -
+ obtain y where "- (pi / 2) < y" and "y < pi / 2" and "tan y = x" using tan_total by blast
+ thus ?thesis unfolding `tan y = x`[symmetric] tan_minus[symmetric] using arctan_tan[of y] arctan_tan[of "-y"] by auto
+qed
+
+lemma arctan_inverse: assumes "x \<noteq> 0" shows "arctan (1 / x) = sgn x * pi / 2 - arctan x"
+proof -
+ obtain y where "- (pi / 2) < y" and "y < pi / 2" and "tan y = x" using tan_total by blast
+ hence "y = arctan x" unfolding `tan y = x`[symmetric] using arctan_tan by auto
+
+ { fix y x :: real assume "0 < y" and "y < pi /2" and "y = arctan x" and "tan y = x" hence "- (pi / 2) < y" by auto
+ have "tan y > 0" using tan_monotone'[OF _ _ `- (pi / 2) < y` `y < pi / 2`, of 0] tan_zero `0 < y` by auto
+ hence "x > 0" using `tan y = x` by auto
+
+ have "- (pi / 2) < pi / 2 - y" using `y > 0` `y < pi / 2` by auto
+ moreover have "pi / 2 - y < pi / 2" using `y > 0` `y < pi / 2` by auto
+ ultimately have "arctan (1 / x) = pi / 2 - y" unfolding `tan y = x`[symmetric] tan_inverse using arctan_tan by auto
+ hence "arctan (1 / x) = sgn x * pi / 2 - arctan x" unfolding `y = arctan x` real_sgn_pos[OF `x > 0`] by auto
+ } note pos_y = this
+
+ show ?thesis
+ proof (cases "y > 0")
+ case True from pos_y[OF this `y < pi / 2` `y = arctan x` `tan y = x`] show ?thesis .
+ next
+ case False hence "y \<le> 0" by auto
+ moreover have "y \<noteq> 0"
+ proof (rule ccontr)
+ assume "\<not> y \<noteq> 0" hence "y = 0" by auto
+ have "x = 0" unfolding `tan y = x`[symmetric] `y = 0` tan_zero ..
+ thus False using `x \<noteq> 0` by auto
+ qed
+ ultimately have "y < 0" by auto
+ hence "0 < - y" and "-y < pi / 2" using `- (pi / 2) < y` by auto
+ moreover have "-y = arctan (-x)" unfolding arctan_minus `y = arctan x` ..
+ moreover have "tan (-y) = -x" unfolding tan_minus `tan y = x` ..
+ ultimately have "arctan (1 / -x) = sgn (-x) * pi / 2 - arctan (-x)" using pos_y by blast
+ hence "arctan (- (1 / x)) = - (sgn x * pi / 2 - arctan x)" unfolding arctan_minus[of x] divide_minus_right sgn_minus by auto
+ thus ?thesis unfolding arctan_minus neg_equal_iff_equal .
+ qed
+qed
+
+theorem pi_series: "pi / 4 = (\<Sum> k. (-1)^k * 1 / real (k*2+1))" (is "_ = ?SUM")
+proof -
+ have "pi / 4 = arctan 1" using arctan1_eq_pi4 by auto
+ also have "\<dots> = ?SUM" using arctan_series[of 1] by auto
+ finally show ?thesis by auto
+qed
subsection {* Existence of Polar Coordinates *}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/ex/ApproximationEx.thy Fri Feb 06 15:59:49 2009 +0100
@@ -0,0 +1,46 @@
+(* Title: HOL/ex/ApproximationEx.thy
+ Author: Johannes Hoelzl <hoelzl@in.tum.de> 2009
+*)
+theory ApproximationEx
+imports "~~/src/HOL/Reflection/Approximation"
+begin
+
+text {*
+
+Here are some examples how to use the approximation method.
+
+The parameter passed to the method specifies the precision used by the computations, it is specified
+as number of bits to calculate. When a variable is used it needs to be bounded by an interval. This
+interval is specified as a conjunction of the lower and upper bound. It must have the form
+@{text "\<lbrakk> l\<^isub>1 \<le> x\<^isub>1 \<and> x\<^isub>1 \<le> u\<^isub>1 ; \<dots> ; l\<^isub>n \<le> x\<^isub>n \<and> x\<^isub>n \<le> u\<^isub>n \<rbrakk> \<Longrightarrow> F"} where @{term F} is the formula, and
+@{text "x\<^isub>1, \<dots>, x\<^isub>n"} are the variables. The lower bounds @{text "l\<^isub>1, \<dots>, l\<^isub>n"} and the upper bounds
+@{text "u\<^isub>1, \<dots>, u\<^isub>n"} must either be integer numerals, floating point numbers or of the form
+@{term "m * pow2 e"} to specify a exact floating point value.
+
+*}
+
+section "Compute some transcendental values"
+
+lemma "\<bar> ln 2 - 544531980202654583340825686620847 / 785593587443817081832229725798400 \<bar> < inverse (2^51) "
+ by (approximation 80)
+
+lemma "\<bar> exp 1.626 - 5.083499996273 \<bar> < (inverse 10 ^ 10 :: real)"
+ by (approximation 80)
+
+lemma "\<bar> sqrt 2 - 1.4142135623730951 \<bar> < inverse 10 ^ 16"
+ by (approximation 80)
+
+lemma "\<bar> pi - 3.1415926535897932385 \<bar> < inverse 10 ^ 18"
+ by (approximation 80)
+
+section "Use variable ranges"
+
+lemma "0.5 \<le> x \<and> x \<le> 4.5 \<Longrightarrow> \<bar> arctan x - 0.91 \<bar> < 0.455"
+ by (approximation 10)
+
+lemma "0 \<le> x \<and> x \<le> 1 \<Longrightarrow> 0 \<le> sin x"
+ by (approximation 10)
+
+
+end
+
--- a/src/HOL/ex/Quickcheck.thy Fri Feb 06 15:57:47 2009 +0100
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,413 +0,0 @@
-(* Author: Florian Haftmann, TU Muenchen *)
-
-header {* A simple counterexample generator *}
-
-theory Quickcheck
-imports Random Code_Eval Map
-begin
-
-subsection {* The @{text random} class *}
-
-class random = typerep +
- fixes random :: "index \<Rightarrow> seed \<Rightarrow> ('a \<times> (unit \<Rightarrow> term)) \<times> seed"
-
-text {* Type @{typ "'a itself"} *}
-
-instantiation itself :: ("{type, typerep}") random
-begin
-
-definition
- "random _ = return (TYPE('a), \<lambda>u. Code_Eval.Const (STR ''TYPE'') TYPEREP('a))"
-
-instance ..
-
-end
-
-text {* Type @{typ "'a \<Rightarrow> 'b"} *}
-
-ML {*
-structure Random_Engine =
-struct
-
-open Random_Engine;
-
-fun random_fun (T1 : typ) (T2 : typ) (eq : 'a -> 'a -> bool) (term_of : 'a -> term)
- (random : Random_Engine.seed -> ('b * (unit -> term)) * Random_Engine.seed)
- (random_split : Random_Engine.seed -> Random_Engine.seed * Random_Engine.seed)
- (seed : Random_Engine.seed) =
- let
- val (seed', seed'') = random_split seed;
- val state = ref (seed', [], Const (@{const_name undefined}, T1 --> T2));
- val fun_upd = Const (@{const_name fun_upd},
- (T1 --> T2) --> T1 --> T2 --> T1 --> T2);
- fun random_fun' x =
- let
- val (seed, fun_map, f_t) = ! state;
- in case AList.lookup (uncurry eq) fun_map x
- of SOME y => y
- | NONE => let
- val t1 = term_of x;
- val ((y, t2), seed') = random seed;
- val fun_map' = (x, y) :: fun_map;
- val f_t' = fun_upd $ f_t $ t1 $ t2 ();
- val _ = state := (seed', fun_map', f_t');
- in y end
- end;
- fun term_fun' () = #3 (! state);
- in ((random_fun', term_fun'), seed'') end;
-
-end
-*}
-
-axiomatization
- random_fun_aux :: "typerep \<Rightarrow> typerep \<Rightarrow> ('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> term)
- \<Rightarrow> (seed \<Rightarrow> ('b \<times> (unit \<Rightarrow> term)) \<times> seed) \<Rightarrow> (seed \<Rightarrow> seed \<times> seed)
- \<Rightarrow> seed \<Rightarrow> (('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)) \<times> seed"
-
-code_const random_fun_aux (SML "Random'_Engine.random'_fun")
-
-instantiation "fun" :: ("{eq, term_of}", "{type, random}") random
-begin
-
-definition random_fun :: "index \<Rightarrow> seed \<Rightarrow> (('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)) \<times> seed" where
- "random n = random_fun_aux TYPEREP('a) TYPEREP('b) (op =) Code_Eval.term_of (random n) split_seed"
-
-instance ..
-
-end
-
-code_reserved SML Random_Engine
-
-text {* Datatypes *}
-
-definition
- collapse :: "('a \<Rightarrow> ('a \<Rightarrow> 'b \<times> 'a) \<times> 'a) \<Rightarrow> 'a \<Rightarrow> 'b \<times> 'a" where
- "collapse f = (do g \<leftarrow> f; g done)"
-
-ML {*
-structure StateMonad =
-struct
-
-fun liftT T sT = sT --> HOLogic.mk_prodT (T, sT);
-fun liftT' sT = sT --> sT;
-
-fun return T sT x = Const (@{const_name return}, T --> liftT T sT) $ x;
-
-fun scomp T1 T2 sT f g = Const (@{const_name scomp},
- liftT T1 sT --> (T1 --> liftT T2 sT) --> liftT T2 sT) $ f $ g;
-
-end;
-*}
-
-lemma random'_if:
- fixes random' :: "index \<Rightarrow> index \<Rightarrow> seed \<Rightarrow> ('a \<times> (unit \<Rightarrow> term)) \<times> seed"
- assumes "random' 0 j = (\<lambda>s. undefined)"
- and "\<And>i. random' (Suc_index i) j = rhs2 i"
- shows "random' i j s = (if i = 0 then undefined else rhs2 (i - 1) s)"
- by (cases i rule: index.exhaust) (insert assms, simp_all)
-
-setup {*
-let
- exception REC of string;
- exception TYP of string;
- fun mk_collapse thy ty = Sign.mk_const thy
- (@{const_name collapse}, [@{typ seed}, ty]);
- fun term_ty ty = HOLogic.mk_prodT (ty, @{typ "unit \<Rightarrow> term"});
- fun mk_split thy ty ty' = Sign.mk_const thy
- (@{const_name split}, [ty, @{typ "unit \<Rightarrow> term"}, StateMonad.liftT (term_ty ty') @{typ seed}]);
- fun mk_scomp_split thy ty ty' t t' =
- StateMonad.scomp (term_ty ty) (term_ty ty') @{typ seed} t
- (mk_split thy ty ty' $ Abs ("", ty, Abs ("", @{typ "unit \<Rightarrow> term"}, t')))
- fun mk_cons thy this_ty (c, args) =
- let
- val tys = map (fst o fst) args;
- val c_ty = tys ---> this_ty;
- val c = Const (c, tys ---> this_ty);
- val t_indices = map (curry ( op * ) 2) (length tys - 1 downto 0);
- val c_indices = map (curry ( op + ) 1) t_indices;
- val c_t = list_comb (c, map Bound c_indices);
- val t_t = Abs ("", @{typ unit}, Eval.mk_term Free Typerep.typerep
- (list_comb (c, map (fn k => Bound (k + 1)) t_indices))
- |> map_aterms (fn t as Bound _ => t $ @{term "()"} | t => t));
- val return = StateMonad.return (term_ty this_ty) @{typ seed}
- (HOLogic.mk_prod (c_t, t_t));
- val t = fold_rev (fn ((ty, _), random) =>
- mk_scomp_split thy ty this_ty random)
- args return;
- val is_rec = exists (snd o fst) args;
- in (is_rec, t) end;
- fun mk_conss thy ty [] = NONE
- | mk_conss thy ty [(_, t)] = SOME t
- | mk_conss thy ty ts = SOME (mk_collapse thy (term_ty ty) $
- (Sign.mk_const thy (@{const_name select}, [StateMonad.liftT (term_ty ty) @{typ seed}]) $
- HOLogic.mk_list (StateMonad.liftT (term_ty ty) @{typ seed}) (map snd ts)));
- fun mk_clauses thy ty (tyco, (ts_rec, ts_atom)) =
- let
- val SOME t_atom = mk_conss thy ty ts_atom;
- in case mk_conss thy ty ts_rec
- of SOME t_rec => mk_collapse thy (term_ty ty) $
- (Sign.mk_const thy (@{const_name select_default}, [StateMonad.liftT (term_ty ty) @{typ seed}]) $
- @{term "i\<Colon>index"} $ t_rec $ t_atom)
- | NONE => t_atom
- end;
- fun mk_random_eqs thy vs tycos =
- let
- val this_ty = Type (hd tycos, map TFree vs);
- val this_ty' = StateMonad.liftT (term_ty this_ty) @{typ seed};
- val random_name = NameSpace.base @{const_name random};
- val random'_name = random_name ^ "_" ^ Class.type_name (hd tycos) ^ "'";
- fun random ty = Sign.mk_const thy (@{const_name random}, [ty]);
- val random' = Free (random'_name,
- @{typ index} --> @{typ index} --> this_ty');
- fun atom ty = if Sign.of_sort thy (ty, @{sort random})
- then ((ty, false), random ty $ @{term "j\<Colon>index"})
- else raise TYP
- ("Will not generate random elements for type(s) " ^ quote (hd tycos));
- fun dtyp tyco = ((this_ty, true), random' $ @{term "i\<Colon>index"} $ @{term "j\<Colon>index"});
- fun rtyp tyco tys = raise REC
- ("Will not generate random elements for mutual recursive type " ^ quote (hd tycos));
- val rhss = DatatypePackage.construction_interpretation thy
- { atom = atom, dtyp = dtyp, rtyp = rtyp } vs tycos
- |> (map o apsnd o map) (mk_cons thy this_ty)
- |> (map o apsnd) (List.partition fst)
- |> map (mk_clauses thy this_ty)
- val eqss = map ((apsnd o map) (HOLogic.mk_Trueprop o HOLogic.mk_eq) o (fn rhs => ((this_ty, random'), [
- (random' $ @{term "0\<Colon>index"} $ @{term "j\<Colon>index"}, Abs ("s", @{typ seed},
- Const (@{const_name undefined}, HOLogic.mk_prodT (term_ty this_ty, @{typ seed})))),
- (random' $ @{term "Suc_index i"} $ @{term "j\<Colon>index"}, rhs)
- ]))) rhss;
- in eqss end;
- fun random_inst [tyco] thy =
- let
- val (raw_vs, _) = DatatypePackage.the_datatype_spec thy tyco;
- val vs = (map o apsnd)
- (curry (Sorts.inter_sort (Sign.classes_of thy)) @{sort random}) raw_vs;
- val ((this_ty, random'), eqs') = singleton (mk_random_eqs thy vs) tyco;
- val eq = (HOLogic.mk_Trueprop o HOLogic.mk_eq)
- (Sign.mk_const thy (@{const_name random}, [this_ty]) $ @{term "i\<Colon>index"},
- random' $ @{term "i\<Colon>index"} $ @{term "i\<Colon>index"})
- val del_func = Attrib.internal (fn _ => Thm.declaration_attribute
- (fn thm => Context.mapping (Code.del_eqn thm) I));
- fun add_code simps lthy =
- let
- val thy = ProofContext.theory_of lthy;
- val thm = @{thm random'_if}
- |> Drule.instantiate' [SOME (Thm.ctyp_of thy this_ty)] [SOME (Thm.cterm_of thy random')]
- |> (fn thm => thm OF simps)
- |> singleton (ProofContext.export lthy (ProofContext.init thy));
- val c = (fst o dest_Const o fst o strip_comb o fst
- o HOLogic.dest_eq o HOLogic.dest_Trueprop o Thm.prop_of) thm;
- in
- lthy
- |> LocalTheory.theory (Code.del_eqns c
- #> PureThy.add_thm ((Binding.name (fst (dest_Free random') ^ "_code"), thm), [Thm.kind_internal])
- #-> Code.add_eqn)
- end;
- in
- thy
- |> TheoryTarget.instantiation ([tyco], vs, @{sort random})
- |> PrimrecPackage.add_primrec
- [(Binding.name (fst (dest_Free random')), SOME (snd (dest_Free random')), NoSyn)]
- (map (fn eq => ((Binding.empty, [del_func]), eq)) eqs')
- |-> add_code
- |> `(fn lthy => Syntax.check_term lthy eq)
- |-> (fn eq => Specification.definition (NONE, (Attrib.empty_binding, eq)))
- |> snd
- |> Class.prove_instantiation_instance (K (Class.intro_classes_tac []))
- |> LocalTheory.exit_global
- end
- | random_inst tycos thy = raise REC
- ("Will not generate random elements for mutual recursive type(s) " ^ commas (map quote tycos));
- fun add_random_inst tycos thy = random_inst tycos thy
- handle REC msg => (warning msg; thy)
- | TYP msg => (warning msg; thy)
-in DatatypePackage.interpretation add_random_inst end
-*}
-
-text {* Type @{typ int} *}
-
-instantiation int :: random
-begin
-
-definition
- "random n = (do
- (b, _) \<leftarrow> random n;
- (m, t) \<leftarrow> random n;
- return (if b then (int m, \<lambda>u. Code_Eval.App (Code_Eval.Const (STR ''Int.int'') TYPEREP(nat \<Rightarrow> int)) (t ()))
- else (- int m, \<lambda>u. Code_Eval.App (Code_Eval.Const (STR ''HOL.uminus_class.uminus'') TYPEREP(int \<Rightarrow> int))
- (Code_Eval.App (Code_Eval.Const (STR ''Int.int'') TYPEREP(nat \<Rightarrow> int)) (t ()))))
- done)"
-
-instance ..
-
-end
-
-
-subsection {* Quickcheck generator *}
-
-ML {*
-structure Quickcheck =
-struct
-
-open Quickcheck;
-
-val eval_ref : (unit -> int -> int * int -> term list option * (int * int)) option ref = ref NONE;
-
-fun mk_generator_expr thy prop tys =
- let
- val bound_max = length tys - 1;
- val bounds = map_index (fn (i, ty) =>
- (2 * (bound_max - i) + 1, 2 * (bound_max - i), 2 * i, ty)) tys;
- val result = list_comb (prop, map (fn (i, _, _, _) => Bound i) bounds);
- val terms = HOLogic.mk_list @{typ term} (map (fn (_, i, _, _) => Bound i $ @{term "()"}) bounds);
- val check = @{term "If \<Colon> bool \<Rightarrow> term list option \<Rightarrow> term list option \<Rightarrow> term list option"}
- $ result $ @{term "None \<Colon> term list option"} $ (@{term "Some \<Colon> term list \<Rightarrow> term list option "} $ terms);
- val return = @{term "Pair \<Colon> term list option \<Rightarrow> seed \<Rightarrow> term list option \<times> seed"};
- fun mk_termtyp ty = HOLogic.mk_prodT (ty, @{typ "unit \<Rightarrow> term"});
- fun mk_split ty = Sign.mk_const thy
- (@{const_name split}, [ty, @{typ "unit \<Rightarrow> term"}, StateMonad.liftT @{typ "term list option"} @{typ seed}]);
- fun mk_scomp_split ty t t' =
- StateMonad.scomp (mk_termtyp ty) @{typ "term list option"} @{typ seed} t (*FIXME*)
- (mk_split ty $ Abs ("", ty, Abs ("", @{typ "unit \<Rightarrow> term"}, t')));
- fun mk_bindclause (_, _, i, ty) = mk_scomp_split ty
- (Sign.mk_const thy (@{const_name random}, [ty]) $ Bound i)
- val t = fold_rev mk_bindclause bounds (return $ check);
- in Abs ("n", @{typ index}, t) end;
-
-fun compile_generator_expr thy t =
- let
- val tys = (map snd o fst o strip_abs) t;
- val t' = mk_generator_expr thy t tys;
- val f = Code_ML.eval_term ("Quickcheck.eval_ref", eval_ref) thy t' [];
- in f #> Random_Engine.run #> (Option.map o map) (Code.postprocess_term thy) end;
-
-end
-*}
-
-setup {*
- Quickcheck.add_generator ("code", Quickcheck.compile_generator_expr o ProofContext.theory_of)
-*}
-
-subsection {* Examples *}
-
-theorem "map g (map f xs) = map (g o f) xs"
- quickcheck [generator = code]
- by (induct xs) simp_all
-
-theorem "map g (map f xs) = map (f o g) xs"
- quickcheck [generator = code]
- oops
-
-theorem "rev (xs @ ys) = rev ys @ rev xs"
- quickcheck [generator = code]
- by simp
-
-theorem "rev (xs @ ys) = rev xs @ rev ys"
- quickcheck [generator = code]
- oops
-
-theorem "rev (rev xs) = xs"
- quickcheck [generator = code]
- by simp
-
-theorem "rev xs = xs"
- quickcheck [generator = code]
- oops
-
-primrec app :: "('a \<Rightarrow> 'a) list \<Rightarrow> 'a \<Rightarrow> 'a" where
- "app [] x = x"
- | "app (f # fs) x = app fs (f x)"
-
-lemma "app (fs @ gs) x = app gs (app fs x)"
- quickcheck [generator = code]
- by (induct fs arbitrary: x) simp_all
-
-lemma "app (fs @ gs) x = app fs (app gs x)"
- quickcheck [generator = code]
- oops
-
-primrec occurs :: "'a \<Rightarrow> 'a list \<Rightarrow> nat" where
- "occurs a [] = 0"
- | "occurs a (x#xs) = (if (x=a) then Suc(occurs a xs) else occurs a xs)"
-
-primrec del1 :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
- "del1 a [] = []"
- | "del1 a (x#xs) = (if (x=a) then xs else (x#del1 a xs))"
-
-lemma "Suc (occurs a (del1 a xs)) = occurs a xs"
- -- {* Wrong. Precondition needed.*}
- quickcheck [generator = code]
- oops
-
-lemma "xs ~= [] \<longrightarrow> Suc (occurs a (del1 a xs)) = occurs a xs"
- quickcheck [generator = code]
- -- {* Also wrong.*}
- oops
-
-lemma "0 < occurs a xs \<longrightarrow> Suc (occurs a (del1 a xs)) = occurs a xs"
- quickcheck [generator = code]
- by (induct xs) auto
-
-primrec replace :: "'a \<Rightarrow> 'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
- "replace a b [] = []"
- | "replace a b (x#xs) = (if (x=a) then (b#(replace a b xs))
- else (x#(replace a b xs)))"
-
-lemma "occurs a xs = occurs b (replace a b xs)"
- quickcheck [generator = code]
- -- {* Wrong. Precondition needed.*}
- oops
-
-lemma "occurs b xs = 0 \<or> a=b \<longrightarrow> occurs a xs = occurs b (replace a b xs)"
- quickcheck [generator = code]
- by (induct xs) simp_all
-
-
-subsection {* Trees *}
-
-datatype 'a tree = Twig | Leaf 'a | Branch "'a tree" "'a tree"
-
-primrec leaves :: "'a tree \<Rightarrow> 'a list" where
- "leaves Twig = []"
- | "leaves (Leaf a) = [a]"
- | "leaves (Branch l r) = (leaves l) @ (leaves r)"
-
-primrec plant :: "'a list \<Rightarrow> 'a tree" where
- "plant [] = Twig "
- | "plant (x#xs) = Branch (Leaf x) (plant xs)"
-
-primrec mirror :: "'a tree \<Rightarrow> 'a tree" where
- "mirror (Twig) = Twig "
- | "mirror (Leaf a) = Leaf a "
- | "mirror (Branch l r) = Branch (mirror r) (mirror l)"
-
-theorem "plant (rev (leaves xt)) = mirror xt"
- quickcheck [generator = code]
- --{* Wrong! *}
- oops
-
-theorem "plant (leaves xt @ leaves yt) = Branch xt yt"
- quickcheck [generator = code]
- --{* Wrong! *}
- oops
-
-datatype 'a ntree = Tip "'a" | Node "'a" "'a ntree" "'a ntree"
-
-primrec inOrder :: "'a ntree \<Rightarrow> 'a list" where
- "inOrder (Tip a)= [a]"
- | "inOrder (Node f x y) = (inOrder x)@[f]@(inOrder y)"
-
-primrec root :: "'a ntree \<Rightarrow> 'a" where
- "root (Tip a) = a"
- | "root (Node f x y) = f"
-
-theorem "hd (inOrder xt) = root xt"
- quickcheck [generator = code]
- --{* Wrong! *}
- oops
-
-lemma "int (f k) = k"
- quickcheck [generator = code]
- oops
-
-end
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/ex/Quickcheck_Generators.thy Fri Feb 06 15:59:49 2009 +0100
@@ -0,0 +1,353 @@
+(* Author: Florian Haftmann, TU Muenchen *)
+
+header {* Experimental counterexample generators *}
+
+theory Quickcheck_Generators
+imports Quickcheck State_Monad
+begin
+
+subsection {* Type @{typ "'a \<Rightarrow> 'b"} *}
+
+ML {*
+structure Random_Engine =
+struct
+
+open Random_Engine;
+
+fun random_fun (T1 : typ) (T2 : typ) (eq : 'a -> 'a -> bool) (term_of : 'a -> term)
+ (random : Random_Engine.seed -> ('b * (unit -> term)) * Random_Engine.seed)
+ (random_split : Random_Engine.seed -> Random_Engine.seed * Random_Engine.seed)
+ (seed : Random_Engine.seed) =
+ let
+ val (seed', seed'') = random_split seed;
+ val state = ref (seed', [], Const (@{const_name undefined}, T1 --> T2));
+ val fun_upd = Const (@{const_name fun_upd},
+ (T1 --> T2) --> T1 --> T2 --> T1 --> T2);
+ fun random_fun' x =
+ let
+ val (seed, fun_map, f_t) = ! state;
+ in case AList.lookup (uncurry eq) fun_map x
+ of SOME y => y
+ | NONE => let
+ val t1 = term_of x;
+ val ((y, t2), seed') = random seed;
+ val fun_map' = (x, y) :: fun_map;
+ val f_t' = fun_upd $ f_t $ t1 $ t2 ();
+ val _ = state := (seed', fun_map', f_t');
+ in y end
+ end;
+ fun term_fun' () = #3 (! state);
+ in ((random_fun', term_fun'), seed'') end;
+
+end
+*}
+
+axiomatization
+ random_fun_aux :: "typerep \<Rightarrow> typerep \<Rightarrow> ('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> term)
+ \<Rightarrow> (seed \<Rightarrow> ('b \<times> (unit \<Rightarrow> term)) \<times> seed) \<Rightarrow> (seed \<Rightarrow> seed \<times> seed)
+ \<Rightarrow> seed \<Rightarrow> (('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)) \<times> seed"
+
+code_const random_fun_aux (SML "Random'_Engine.random'_fun")
+
+instantiation "fun" :: ("{eq, term_of}", "{type, random}") random
+begin
+
+definition random_fun :: "index \<Rightarrow> seed \<Rightarrow> (('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)) \<times> seed" where
+ "random n = random_fun_aux TYPEREP('a) TYPEREP('b) (op =) Code_Eval.term_of (random n) split_seed"
+
+instance ..
+
+end
+
+code_reserved SML Random_Engine
+
+
+subsection {* Datatypes *}
+
+definition
+ collapse :: "('a \<Rightarrow> ('a \<Rightarrow> 'b \<times> 'a) \<times> 'a) \<Rightarrow> 'a \<Rightarrow> 'b \<times> 'a" where
+ "collapse f = (do g \<leftarrow> f; g done)"
+
+ML {*
+structure StateMonad =
+struct
+
+fun liftT T sT = sT --> HOLogic.mk_prodT (T, sT);
+fun liftT' sT = sT --> sT;
+
+fun return T sT x = Const (@{const_name return}, T --> liftT T sT) $ x;
+
+fun scomp T1 T2 sT f g = Const (@{const_name scomp},
+ liftT T1 sT --> (T1 --> liftT T2 sT) --> liftT T2 sT) $ f $ g;
+
+end;
+*}
+
+lemma random'_if:
+ fixes random' :: "index \<Rightarrow> index \<Rightarrow> seed \<Rightarrow> ('a \<times> (unit \<Rightarrow> term)) \<times> seed"
+ assumes "random' 0 j = (\<lambda>s. undefined)"
+ and "\<And>i. random' (Suc_index i) j = rhs2 i"
+ shows "random' i j s = (if i = 0 then undefined else rhs2 (i - 1) s)"
+ by (cases i rule: index.exhaust) (insert assms, simp_all)
+
+setup {*
+let
+ exception REC of string;
+ exception TYP of string;
+ fun mk_collapse thy ty = Sign.mk_const thy
+ (@{const_name collapse}, [@{typ seed}, ty]);
+ fun term_ty ty = HOLogic.mk_prodT (ty, @{typ "unit \<Rightarrow> term"});
+ fun mk_split thy ty ty' = Sign.mk_const thy
+ (@{const_name split}, [ty, @{typ "unit \<Rightarrow> term"}, StateMonad.liftT (term_ty ty') @{typ seed}]);
+ fun mk_scomp_split thy ty ty' t t' =
+ StateMonad.scomp (term_ty ty) (term_ty ty') @{typ seed} t
+ (mk_split thy ty ty' $ Abs ("", ty, Abs ("", @{typ "unit \<Rightarrow> term"}, t')))
+ fun mk_cons thy this_ty (c, args) =
+ let
+ val tys = map (fst o fst) args;
+ val c_ty = tys ---> this_ty;
+ val c = Const (c, tys ---> this_ty);
+ val t_indices = map (curry ( op * ) 2) (length tys - 1 downto 0);
+ val c_indices = map (curry ( op + ) 1) t_indices;
+ val c_t = list_comb (c, map Bound c_indices);
+ val t_t = Abs ("", @{typ unit}, Eval.mk_term Free Typerep.typerep
+ (list_comb (c, map (fn k => Bound (k + 1)) t_indices))
+ |> map_aterms (fn t as Bound _ => t $ @{term "()"} | t => t));
+ val return = StateMonad.return (term_ty this_ty) @{typ seed}
+ (HOLogic.mk_prod (c_t, t_t));
+ val t = fold_rev (fn ((ty, _), random) =>
+ mk_scomp_split thy ty this_ty random)
+ args return;
+ val is_rec = exists (snd o fst) args;
+ in (is_rec, t) end;
+ fun mk_conss thy ty [] = NONE
+ | mk_conss thy ty [(_, t)] = SOME t
+ | mk_conss thy ty ts = SOME (mk_collapse thy (term_ty ty) $
+ (Sign.mk_const thy (@{const_name select}, [StateMonad.liftT (term_ty ty) @{typ seed}]) $
+ HOLogic.mk_list (StateMonad.liftT (term_ty ty) @{typ seed}) (map snd ts)));
+ fun mk_clauses thy ty (tyco, (ts_rec, ts_atom)) =
+ let
+ val SOME t_atom = mk_conss thy ty ts_atom;
+ in case mk_conss thy ty ts_rec
+ of SOME t_rec => mk_collapse thy (term_ty ty) $
+ (Sign.mk_const thy (@{const_name select_default}, [StateMonad.liftT (term_ty ty) @{typ seed}]) $
+ @{term "i\<Colon>index"} $ t_rec $ t_atom)
+ | NONE => t_atom
+ end;
+ fun mk_random_eqs thy vs tycos =
+ let
+ val this_ty = Type (hd tycos, map TFree vs);
+ val this_ty' = StateMonad.liftT (term_ty this_ty) @{typ seed};
+ val random_name = NameSpace.base @{const_name random};
+ val random'_name = random_name ^ "_" ^ Class.type_name (hd tycos) ^ "'";
+ fun random ty = Sign.mk_const thy (@{const_name random}, [ty]);
+ val random' = Free (random'_name,
+ @{typ index} --> @{typ index} --> this_ty');
+ fun atom ty = if Sign.of_sort thy (ty, @{sort random})
+ then ((ty, false), random ty $ @{term "j\<Colon>index"})
+ else raise TYP
+ ("Will not generate random elements for type(s) " ^ quote (hd tycos));
+ fun dtyp tyco = ((this_ty, true), random' $ @{term "i\<Colon>index"} $ @{term "j\<Colon>index"});
+ fun rtyp tyco tys = raise REC
+ ("Will not generate random elements for mutual recursive type " ^ quote (hd tycos));
+ val rhss = DatatypePackage.construction_interpretation thy
+ { atom = atom, dtyp = dtyp, rtyp = rtyp } vs tycos
+ |> (map o apsnd o map) (mk_cons thy this_ty)
+ |> (map o apsnd) (List.partition fst)
+ |> map (mk_clauses thy this_ty)
+ val eqss = map ((apsnd o map) (HOLogic.mk_Trueprop o HOLogic.mk_eq) o (fn rhs => ((this_ty, random'), [
+ (random' $ @{term "0\<Colon>index"} $ @{term "j\<Colon>index"}, Abs ("s", @{typ seed},
+ Const (@{const_name undefined}, HOLogic.mk_prodT (term_ty this_ty, @{typ seed})))),
+ (random' $ @{term "Suc_index i"} $ @{term "j\<Colon>index"}, rhs)
+ ]))) rhss;
+ in eqss end;
+ fun random_inst [tyco] thy =
+ let
+ val (raw_vs, _) = DatatypePackage.the_datatype_spec thy tyco;
+ val vs = (map o apsnd)
+ (curry (Sorts.inter_sort (Sign.classes_of thy)) @{sort random}) raw_vs;
+ val ((this_ty, random'), eqs') = singleton (mk_random_eqs thy vs) tyco;
+ val eq = (HOLogic.mk_Trueprop o HOLogic.mk_eq)
+ (Sign.mk_const thy (@{const_name random}, [this_ty]) $ @{term "i\<Colon>index"},
+ random' $ @{term "i\<Colon>index"} $ @{term "i\<Colon>index"})
+ val del_func = Attrib.internal (fn _ => Thm.declaration_attribute
+ (fn thm => Context.mapping (Code.del_eqn thm) I));
+ fun add_code simps lthy =
+ let
+ val thy = ProofContext.theory_of lthy;
+ val thm = @{thm random'_if}
+ |> Drule.instantiate' [SOME (Thm.ctyp_of thy this_ty)] [SOME (Thm.cterm_of thy random')]
+ |> (fn thm => thm OF simps)
+ |> singleton (ProofContext.export lthy (ProofContext.init thy));
+ val c = (fst o dest_Const o fst o strip_comb o fst
+ o HOLogic.dest_eq o HOLogic.dest_Trueprop o Thm.prop_of) thm;
+ in
+ lthy
+ |> LocalTheory.theory (Code.del_eqns c
+ #> PureThy.add_thm ((Binding.name (fst (dest_Free random') ^ "_code"), thm), [Thm.kind_internal])
+ #-> Code.add_eqn)
+ end;
+ in
+ thy
+ |> TheoryTarget.instantiation ([tyco], vs, @{sort random})
+ |> PrimrecPackage.add_primrec
+ [(Binding.name (fst (dest_Free random')), SOME (snd (dest_Free random')), NoSyn)]
+ (map (fn eq => ((Binding.empty, [del_func]), eq)) eqs')
+ |-> add_code
+ |> `(fn lthy => Syntax.check_term lthy eq)
+ |-> (fn eq => Specification.definition (NONE, (Attrib.empty_binding, eq)))
+ |> snd
+ |> Class.prove_instantiation_instance (K (Class.intro_classes_tac []))
+ |> LocalTheory.exit_global
+ end
+ | random_inst tycos thy = raise REC
+ ("Will not generate random elements for mutual recursive type(s) " ^ commas (map quote tycos));
+ fun add_random_inst tycos thy = random_inst tycos thy
+ handle REC msg => (warning msg; thy)
+ | TYP msg => (warning msg; thy)
+in DatatypePackage.interpretation add_random_inst end
+*}
+
+
+subsection {* Type @{typ int} *}
+
+instantiation int :: random
+begin
+
+definition
+ "random n = (do
+ (b, _) \<leftarrow> random n;
+ (m, t) \<leftarrow> random n;
+ return (if b then (int m, \<lambda>u. Code_Eval.App (Code_Eval.Const (STR ''Int.int'') TYPEREP(nat \<Rightarrow> int)) (t ()))
+ else (- int m, \<lambda>u. Code_Eval.App (Code_Eval.Const (STR ''HOL.uminus_class.uminus'') TYPEREP(int \<Rightarrow> int))
+ (Code_Eval.App (Code_Eval.Const (STR ''Int.int'') TYPEREP(nat \<Rightarrow> int)) (t ()))))
+ done)"
+
+instance ..
+
+end
+
+
+subsection {* Examples *}
+
+theorem "map g (map f xs) = map (g o f) xs"
+ quickcheck [generator = code]
+ by (induct xs) simp_all
+
+theorem "map g (map f xs) = map (f o g) xs"
+ quickcheck [generator = code]
+ oops
+
+theorem "rev (xs @ ys) = rev ys @ rev xs"
+ quickcheck [generator = code]
+ by simp
+
+theorem "rev (xs @ ys) = rev xs @ rev ys"
+ quickcheck [generator = code]
+ oops
+
+theorem "rev (rev xs) = xs"
+ quickcheck [generator = code]
+ by simp
+
+theorem "rev xs = xs"
+ quickcheck [generator = code]
+ oops
+
+primrec app :: "('a \<Rightarrow> 'a) list \<Rightarrow> 'a \<Rightarrow> 'a" where
+ "app [] x = x"
+ | "app (f # fs) x = app fs (f x)"
+
+lemma "app (fs @ gs) x = app gs (app fs x)"
+ quickcheck [generator = code]
+ by (induct fs arbitrary: x) simp_all
+
+lemma "app (fs @ gs) x = app fs (app gs x)"
+ quickcheck [generator = code]
+ oops
+
+primrec occurs :: "'a \<Rightarrow> 'a list \<Rightarrow> nat" where
+ "occurs a [] = 0"
+ | "occurs a (x#xs) = (if (x=a) then Suc(occurs a xs) else occurs a xs)"
+
+primrec del1 :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
+ "del1 a [] = []"
+ | "del1 a (x#xs) = (if (x=a) then xs else (x#del1 a xs))"
+
+lemma "Suc (occurs a (del1 a xs)) = occurs a xs"
+ -- {* Wrong. Precondition needed.*}
+ quickcheck [generator = code]
+ oops
+
+lemma "xs ~= [] \<longrightarrow> Suc (occurs a (del1 a xs)) = occurs a xs"
+ quickcheck [generator = code]
+ -- {* Also wrong.*}
+ oops
+
+lemma "0 < occurs a xs \<longrightarrow> Suc (occurs a (del1 a xs)) = occurs a xs"
+ quickcheck [generator = code]
+ by (induct xs) auto
+
+primrec replace :: "'a \<Rightarrow> 'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
+ "replace a b [] = []"
+ | "replace a b (x#xs) = (if (x=a) then (b#(replace a b xs))
+ else (x#(replace a b xs)))"
+
+lemma "occurs a xs = occurs b (replace a b xs)"
+ quickcheck [generator = code]
+ -- {* Wrong. Precondition needed.*}
+ oops
+
+lemma "occurs b xs = 0 \<or> a=b \<longrightarrow> occurs a xs = occurs b (replace a b xs)"
+ quickcheck [generator = code]
+ by (induct xs) simp_all
+
+
+subsection {* Trees *}
+
+datatype 'a tree = Twig | Leaf 'a | Branch "'a tree" "'a tree"
+
+primrec leaves :: "'a tree \<Rightarrow> 'a list" where
+ "leaves Twig = []"
+ | "leaves (Leaf a) = [a]"
+ | "leaves (Branch l r) = (leaves l) @ (leaves r)"
+
+primrec plant :: "'a list \<Rightarrow> 'a tree" where
+ "plant [] = Twig "
+ | "plant (x#xs) = Branch (Leaf x) (plant xs)"
+
+primrec mirror :: "'a tree \<Rightarrow> 'a tree" where
+ "mirror (Twig) = Twig "
+ | "mirror (Leaf a) = Leaf a "
+ | "mirror (Branch l r) = Branch (mirror r) (mirror l)"
+
+theorem "plant (rev (leaves xt)) = mirror xt"
+ quickcheck [generator = code]
+ --{* Wrong! *}
+ oops
+
+theorem "plant (leaves xt @ leaves yt) = Branch xt yt"
+ quickcheck [generator = code]
+ --{* Wrong! *}
+ oops
+
+datatype 'a ntree = Tip "'a" | Node "'a" "'a ntree" "'a ntree"
+
+primrec inOrder :: "'a ntree \<Rightarrow> 'a list" where
+ "inOrder (Tip a)= [a]"
+ | "inOrder (Node f x y) = (inOrder x)@[f]@(inOrder y)"
+
+primrec root :: "'a ntree \<Rightarrow> 'a" where
+ "root (Tip a) = a"
+ | "root (Node f x y) = f"
+
+theorem "hd (inOrder xt) = root xt"
+ quickcheck [generator = code]
+ --{* Wrong! *}
+ oops
+
+lemma "int (f k) = k"
+ quickcheck [generator = code]
+ oops
+
+end
--- a/src/HOL/ex/ROOT.ML Fri Feb 06 15:57:47 2009 +0100
+++ b/src/HOL/ex/ROOT.ML Fri Feb 06 15:59:49 2009 +0100
@@ -10,7 +10,7 @@
"FuncSet",
"Word",
"Eval_Examples",
- "Quickcheck",
+ "Quickcheck_Generators",
"Term_Of_Syntax",
"Codegenerator",
"Codegenerator_Pretty",
--- a/src/HOL/ex/Random.thy Fri Feb 06 15:57:47 2009 +0100
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,183 +0,0 @@
-(* ID: $Id$
- Author: Florian Haftmann, TU Muenchen
-*)
-
-header {* A HOL random engine *}
-
-theory Random
-imports State_Monad Code_Index
-begin
-
-subsection {* Auxiliary functions *}
-
-definition
- inc_shift :: "index \<Rightarrow> index \<Rightarrow> index"
-where
- "inc_shift v k = (if v = k then 1 else k + 1)"
-
-definition
- minus_shift :: "index \<Rightarrow> index \<Rightarrow> index \<Rightarrow> index"
-where
- "minus_shift r k l = (if k < l then r + k - l else k - l)"
-
-fun
- log :: "index \<Rightarrow> index \<Rightarrow> index"
-where
- "log b i = (if b \<le> 1 \<or> i < b then 1 else 1 + log b (i div b))"
-
-subsection {* Random seeds *}
-
-types seed = "index \<times> index"
-
-primrec
- "next" :: "seed \<Rightarrow> index \<times> seed"
-where
- "next (v, w) = (let
- k = v div 53668;
- v' = minus_shift 2147483563 (40014 * (v mod 53668)) (k * 12211);
- l = w div 52774;
- w' = minus_shift 2147483399 (40692 * (w mod 52774)) (l * 3791);
- z = minus_shift 2147483562 v' (w' + 1) + 1
- in (z, (v', w')))"
-
-lemma next_not_0:
- "fst (next s) \<noteq> 0"
-apply (cases s)
-apply (auto simp add: minus_shift_def Let_def)
-done
-
-primrec
- seed_invariant :: "seed \<Rightarrow> bool"
-where
- "seed_invariant (v, w) \<longleftrightarrow> 0 < v \<and> v < 9438322952 \<and> 0 < w \<and> True"
-
-lemma if_same:
- "(if b then f x else f y) = f (if b then x else y)"
- by (cases b) simp_all
-
-definition
- split_seed :: "seed \<Rightarrow> seed \<times> seed"
-where
- "split_seed s = (let
- (v, w) = s;
- (v', w') = snd (next s);
- v'' = inc_shift 2147483562 v;
- s'' = (v'', w');
- w'' = inc_shift 2147483398 w;
- s''' = (v', w'')
- in (s'', s'''))"
-
-
-subsection {* Base selectors *}
-
-function
- range_aux :: "index \<Rightarrow> index \<Rightarrow> seed \<Rightarrow> index \<times> seed"
-where
- "range_aux k l s = (if k = 0 then (l, s) else
- let (v, s') = next s
- in range_aux (k - 1) (v + l * 2147483561) s')"
-by pat_completeness auto
-termination
- by (relation "measure (nat_of_index o fst)")
- (auto simp add: index)
-
-definition
- range :: "index \<Rightarrow> seed \<Rightarrow> index \<times> seed"
-where
- "range k = (do
- v \<leftarrow> range_aux (log 2147483561 k) 1;
- return (v mod k)
- done)"
-
-lemma range:
- assumes "k > 0"
- shows "fst (range k s) < k"
-proof -
- obtain v w where range_aux:
- "range_aux (log 2147483561 k) 1 s = (v, w)"
- by (cases "range_aux (log 2147483561 k) 1 s")
- with assms show ?thesis
- by (simp add: monad_collapse range_def del: range_aux.simps log.simps)
-qed
-
-definition
- select :: "'a list \<Rightarrow> seed \<Rightarrow> 'a \<times> seed"
-where
- "select xs = (do
- k \<leftarrow> range (index_of_nat (length xs));
- return (nth xs (nat_of_index k))
- done)"
-
-lemma select:
- assumes "xs \<noteq> []"
- shows "fst (select xs s) \<in> set xs"
-proof -
- from assms have "index_of_nat (length xs) > 0" by simp
- with range have
- "fst (range (index_of_nat (length xs)) s) < index_of_nat (length xs)" by best
- then have
- "nat_of_index (fst (range (index_of_nat (length xs)) s)) < length xs" by simp
- then show ?thesis
- by (auto simp add: monad_collapse select_def)
-qed
-
-definition
- select_default :: "index \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> seed \<Rightarrow> 'a \<times> seed"
-where
- [code del]: "select_default k x y = (do
- l \<leftarrow> range k;
- return (if l + 1 < k then x else y)
- done)"
-
-lemma select_default_zero:
- "fst (select_default 0 x y s) = y"
- by (simp add: monad_collapse select_default_def)
-
-lemma select_default_code [code]:
- "select_default k x y = (if k = 0 then do
- _ \<leftarrow> range 1;
- return y
- done else do
- l \<leftarrow> range k;
- return (if l + 1 < k then x else y)
- done)"
-proof (cases "k = 0")
- case False then show ?thesis by (simp add: select_default_def)
-next
- case True then show ?thesis
- by (simp add: monad_collapse select_default_def range_def)
-qed
-
-
-subsection {* @{text ML} interface *}
-
-ML {*
-structure Random_Engine =
-struct
-
-type seed = int * int;
-
-local
-
-val seed = ref
- (let
- val now = Time.toMilliseconds (Time.now ());
- val (q, s1) = IntInf.divMod (now, 2147483562);
- val s2 = q mod 2147483398;
- in (s1 + 1, s2 + 1) end);
-
-in
-
-fun run f =
- let
- val (x, seed') = f (! seed);
- val _ = seed := seed'
- in x end;
-
-end;
-
-end;
-*}
-
-end
-
--- a/src/Pure/Isar/class.ML Fri Feb 06 15:57:47 2009 +0100
+++ b/src/Pure/Isar/class.ML Fri Feb 06 15:59:49 2009 +0100
@@ -122,19 +122,19 @@
val inferred_sort = extract
(fn TVar (_, sort) => curry (Sorts.inter_sort algebra) sort | _ => I);
val fixate_sort = if null tfrees then inferred_sort
- else let val a_sort = (snd o the_single) tfrees
- in if Sorts.sort_le algebra (a_sort, inferred_sort)
- then Sorts.inter_sort algebra (a_sort, inferred_sort)
- else error ("Type inference imposes additional sort constraint "
- ^ Syntax.string_of_sort_global thy inferred_sort
- ^ " of type parameter " ^ Name.aT ^ " of sort "
- ^ Syntax.string_of_sort_global thy a_sort)
- end
+ else case tfrees
+ of [(_, a_sort)] => if Sorts.sort_le algebra (a_sort, inferred_sort)
+ then Sorts.inter_sort algebra (a_sort, inferred_sort)
+ else error ("Type inference imposes additional sort constraint "
+ ^ Syntax.string_of_sort_global thy inferred_sort
+ ^ " of type parameter " ^ Name.aT ^ " of sort "
+ ^ Syntax.string_of_sort_global thy a_sort ^ ".")
+ | _ => error "Multiple type variables in class specification.";
in (map o map_atyps) (K (TFree (Name.aT, fixate_sort))) Ts end;
fun add_typ_check level name f = Context.proof_map (Syntax.add_typ_check level name (fn xs => fn ctxt =>
let val xs' = f xs in if eq_list (op =) (xs, xs') then NONE else SOME (xs', ctxt) end));
- (* preproceesing elements, retrieving base sort from type-checked elements *)
+ (* preprocessing elements, retrieving base sort from type-checked elements *)
val init_class_body = fold (ProofContext.add_const_constraint o apsnd SOME) base_constraints
#> redeclare_operations thy sups
#> add_typ_check 10 "reject_bcd_etc" reject_bcd_etc
@@ -188,9 +188,6 @@
(* process elements as class specification *)
val class_ctxt = begin sups base_sort (ProofContext.init thy)
val ((_, _, syntax_elems), _) = class_ctxt
- (*#> fold (Variable.declare_constraints o Free) ((map o apsnd o map_atyps)
- (K (TFree (Name.aT, base_sort))) supparams) FIXME really necessary?*)
- (*FIXME should constraints be issued in begin?*)
|> Expression.cert_declaration supexpr I inferred_elems;
fun check_vars e vs = if null vs
then error ("No type variable in part of specification element "
@@ -210,7 +207,7 @@
val (elems, global_syntax) = fold_map fork_syn syntax_elems [];
val constrain = Element.Constrains ((map o apsnd o map_atyps)
(K (TFree (Name.aT, base_sort))) supparams);
- (*FIXME 2009 perhaps better: control type variable by explicit
+ (*FIXME perhaps better: control type variable by explicit
parameter instantiation of import expression*)
in (((sups, supparam_names), (sup_sort, base_sort, supexpr)), (constrain :: elems, global_syntax)) end;
@@ -223,7 +220,7 @@
fun add_consts bname class base_sort sups supparams global_syntax thy =
let
- (*FIXME 2009 simplify*)
+ (*FIXME simplify*)
val supconsts = supparams
|> AList.make (snd o the o AList.lookup (op =) (these_params thy sups))
|> (map o apsnd o apsnd o map_atyps o K o TFree) (Name.aT, [class]);
@@ -253,7 +250,7 @@
fun adjungate_axclass bname class base_sort sups supsort supparams global_syntax thy =
let
- (*FIXME 2009 simplify*)
+ (*FIXME simplify*)
fun globalize param_map = map_aterms
(fn Free (v, ty) => Const ((fst o the o AList.lookup (op =) param_map) v, ty)
| t => t);