session Reflecion renamed to Decision_Procs, moved Dense_Linear_Order there
authorhaftmann
Fri Feb 06 15:15:32 2009 +0100 (2009-02-06)
changeset 298230ab754d13ccd
parent 29822 c45845743f04
child 29824 2cf979ed69b8
session Reflecion renamed to Decision_Procs, moved Dense_Linear_Order there
NEWS
src/HOL/Decision_Procs/Approximation.thy
src/HOL/Decision_Procs/Cooper.thy
src/HOL/Decision_Procs/Dense_Linear_Order.thy
src/HOL/Decision_Procs/Ferrack.thy
src/HOL/Decision_Procs/MIR.thy
src/HOL/Decision_Procs/ROOT.ML
src/HOL/Decision_Procs/cooper_tac.ML
src/HOL/Decision_Procs/ferrack_tac.ML
src/HOL/Decision_Procs/mir_tac.ML
src/HOL/Extraction/Pigeonhole.thy
src/HOL/Extraction/Warshall.thy
src/HOL/IsaMakefile
src/HOL/Library/Code_Index.thy
src/HOL/Library/Dense_Linear_Order.thy
src/HOL/Library/Library.thy
src/HOL/Library/Quickcheck.thy
src/HOL/Library/Random.thy
src/HOL/MetisExamples/BigO.thy
src/HOL/Orderings.thy
src/HOL/Tools/Qelim/generated_cooper.ML
src/HOL/ex/Dense_Linear_Order_Ex.thy
     1.1 --- a/NEWS	Fri Feb 06 15:15:27 2009 +0100
     1.2 +++ b/NEWS	Fri Feb 06 15:15:32 2009 +0100
     1.3 @@ -196,14 +196,17 @@
     1.4  * Auxiliary class "itself" has disappeared -- classes without any parameter
     1.5  are treated as expected by the 'class' command.
     1.6  
     1.7 -* Theory "Reflection" now resides in HOL/Library.  Common reflection examples
     1.8 -(Cooper, MIR, Ferrack, Approximation) now in distinct session directory
     1.9 -HOL/Reflection. Here Approximation provides the new proof method
    1.10 -"approximation". It proves formulas on real values by using interval arithmetic.
    1.11 +* Leibnitz's Series for Pi and the arcus tangens and logarithm series.
    1.12 +
    1.13 +* Common decision procedures (Cooper, MIR, Ferrack, Approximation, Dense_Linear_Order)
    1.14 +now in directory HOL/Decision_Procs.
    1.15 +
    1.16 +* Theory HOL/Decisioin_Procs/Approximation.thy provides the new proof method
    1.17 +"approximation".  It proves formulas on real values by using interval arithmetic.
    1.18  In the formulas are also the transcendental functions sin, cos, tan, atan, ln,
    1.19 -exp and the constant pi are allowed. For examples see
    1.20 -src/HOL/ex/ApproximationEx.thy. To reach this the Leibnitz's Series for Pi and
    1.21 -the arcus tangens and logarithm series is now proved in Isabelle.
    1.22 +exp and the constant pi are allowed.  For examples see HOL/ex/ApproximationEx.thy.
    1.23 +
    1.24 +* Theory "Reflection" now resides in HOL/Library.
    1.25  
    1.26  * Entry point to Word library now simply named "Word".  INCOMPATIBILITY.
    1.27  
    1.28 @@ -212,7 +215,6 @@
    1.29  
    1.30      src/HOL/Library/RType.thy ~> src/HOL/Typerep.thy
    1.31      src/HOL/Library/Code_Message.thy ~> src/HOL/
    1.32 -    src/HOL/Library/Dense_Linear_Order.thy ~> src/HOL/
    1.33      src/HOL/Library/GCD.thy ~> src/HOL/
    1.34      src/HOL/Library/Order_Relation.thy ~> src/HOL/
    1.35      src/HOL/Library/Parity.thy ~> src/HOL/
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/HOL/Decision_Procs/Approximation.thy	Fri Feb 06 15:15:32 2009 +0100
     2.3 @@ -0,0 +1,2507 @@
     2.4 +(* Title:     HOL/Reflection/Approximation.thy
     2.5 + * Author:    Johannes Hölzl <hoelzl@in.tum.de> 2008 / 2009
     2.6 + *)
     2.7 +header {* Prove unequations about real numbers by computation *}
     2.8 +theory Approximation
     2.9 +imports Complex_Main Float Reflection Dense_Linear_Order Efficient_Nat
    2.10 +begin
    2.11 +
    2.12 +section "Horner Scheme"
    2.13 +
    2.14 +subsection {* Define auxiliary helper @{text horner} function *}
    2.15 +
    2.16 +fun horner :: "(nat \<Rightarrow> nat) \<Rightarrow> (nat \<Rightarrow> nat \<Rightarrow> nat) \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> real \<Rightarrow> real" where
    2.17 +"horner F G 0 i k x       = 0" |
    2.18 +"horner F G (Suc n) i k x = 1 / real k - x * horner F G n (F i) (G i k) x"
    2.19 +
    2.20 +lemma horner_schema': fixes x :: real  and a :: "nat \<Rightarrow> real"
    2.21 +  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)"
    2.22 +proof -
    2.23 +  have shift_pow: "\<And>i. - (x * ((-1)^i * a (Suc i) * x ^ i)) = (-1)^(Suc i) * a (Suc i) * x ^ (Suc i)" by auto
    2.24 +  show ?thesis unfolding setsum_right_distrib shift_pow real_diff_def setsum_negf[symmetric] setsum_head_upt_Suc[OF zero_less_Suc]
    2.25 +    setsum_reindex[OF inj_Suc, unfolded comp_def, symmetric, of "\<lambda> n. (-1)^n  *a n * x^n"] by auto
    2.26 +qed
    2.27 +
    2.28 +lemma horner_schema: fixes f :: "nat \<Rightarrow> nat" and G :: "nat \<Rightarrow> nat \<Rightarrow> nat" and F :: "nat \<Rightarrow> nat"
    2.29 +  assumes f_Suc: "\<And>n. f (Suc n) = G ((F^n) s) (f n)"
    2.30 +  shows "horner F G n ((F^j') s) (f j') x = (\<Sum> j = 0..< n. -1^j * (1 / real (f (j' + j))) * x^j)"
    2.31 +proof (induct n arbitrary: i k j')
    2.32 +  case (Suc n)
    2.33 +
    2.34 +  show ?case unfolding horner.simps Suc[where j'="Suc j'", unfolded funpow.simps comp_def f_Suc]
    2.35 +    using horner_schema'[of "\<lambda> j. 1 / real (f (j' + j))"] by auto
    2.36 +qed auto
    2.37 +
    2.38 +lemma horner_bounds':
    2.39 +  assumes "0 \<le> Ifloat x" and f_Suc: "\<And>n. f (Suc n) = G ((F^n) s) (f n)"
    2.40 +  and lb_0: "\<And> i k x. lb 0 i k x = 0"
    2.41 +  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)"
    2.42 +  and ub_0: "\<And> i k x. ub 0 i k x = 0"
    2.43 +  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)"
    2.44 +  shows "Ifloat (lb n ((F^j') s) (f j') x) \<le> horner F G n ((F^j') s) (f j') (Ifloat x) \<and> 
    2.45 +         horner F G n ((F^j') s) (f j') (Ifloat x) \<le> Ifloat (ub n ((F^j') s) (f j') x)"
    2.46 +  (is "?lb n j' \<le> ?horner n j' \<and> ?horner n j' \<le> ?ub n j'")
    2.47 +proof (induct n arbitrary: j')
    2.48 +  case 0 thus ?case unfolding lb_0 ub_0 horner.simps by auto
    2.49 +next
    2.50 +  case (Suc n)
    2.51 +  have "?lb (Suc n) j' \<le> ?horner (Suc n) j'" unfolding lb_Suc ub_Suc horner.simps Ifloat_sub diff_def
    2.52 +  proof (rule add_mono)
    2.53 +    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
    2.54 +    from Suc[where j'="Suc j'", unfolded funpow.simps comp_def f_Suc, THEN conjunct2] `0 \<le> Ifloat x`
    2.55 +    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))"
    2.56 +      unfolding Ifloat_mult neg_le_iff_le by (rule mult_left_mono)
    2.57 +  qed
    2.58 +  moreover have "?horner (Suc n) j' \<le> ?ub (Suc n) j'" unfolding ub_Suc ub_Suc horner.simps Ifloat_sub diff_def
    2.59 +  proof (rule add_mono)
    2.60 +    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
    2.61 +    from Suc[where j'="Suc j'", unfolded funpow.simps comp_def f_Suc, THEN conjunct1] `0 \<le> Ifloat x`
    2.62 +    show "- (Ifloat x * horner F G n (F ((F ^ j') s)) (G ((F ^ j') s) (f j')) (Ifloat x)) \<le> 
    2.63 +          - Ifloat (x * lb n (F ((F ^ j') s)) (G ((F ^ j') s) (f j')) x)"
    2.64 +      unfolding Ifloat_mult neg_le_iff_le by (rule mult_left_mono)
    2.65 +  qed
    2.66 +  ultimately show ?case by blast
    2.67 +qed
    2.68 +
    2.69 +subsection "Theorems for floating point functions implementing the horner scheme"
    2.70 +
    2.71 +text {*
    2.72 +
    2.73 +Here @{term_type "f :: nat \<Rightarrow> nat"} is the sequence defining the Taylor series, the coefficients are
    2.74 +all alternating and reciprocs. We use @{term G} and @{term F} to describe the computation of @{term f}.
    2.75 +
    2.76 +*}
    2.77 +
    2.78 +lemma horner_bounds: fixes F :: "nat \<Rightarrow> nat" and G :: "nat \<Rightarrow> nat \<Rightarrow> nat"
    2.79 +  assumes "0 \<le> Ifloat x" and f_Suc: "\<And>n. f (Suc n) = G ((F^n) s) (f n)"
    2.80 +  and lb_0: "\<And> i k x. lb 0 i k x = 0"
    2.81 +  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)"
    2.82 +  and ub_0: "\<And> i k x. ub 0 i k x = 0"
    2.83 +  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)"
    2.84 +  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 
    2.85 +        "(\<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")
    2.86 +proof -
    2.87 +  have "?lb  \<and> ?ub" 
    2.88 +    using horner_bounds'[where lb=lb, OF `0 \<le> Ifloat x` f_Suc lb_0 lb_Suc ub_0 ub_Suc]
    2.89 +    unfolding horner_schema[where f=f, OF f_Suc] .
    2.90 +  thus "?lb" and "?ub" by auto
    2.91 +qed
    2.92 +
    2.93 +lemma horner_bounds_nonpos: fixes F :: "nat \<Rightarrow> nat" and G :: "nat \<Rightarrow> nat \<Rightarrow> nat"
    2.94 +  assumes "Ifloat x \<le> 0" and f_Suc: "\<And>n. f (Suc n) = G ((F^n) s) (f n)"
    2.95 +  and lb_0: "\<And> i k x. lb 0 i k x = 0"
    2.96 +  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)"
    2.97 +  and ub_0: "\<And> i k x. ub 0 i k x = 0"
    2.98 +  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)"
    2.99 +  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 
   2.100 +        "(\<Sum>j=0..<n. (1 / real (f (j' + j))) * (Ifloat x)^j) \<le> Ifloat (ub n ((F^j') s) (f j') x)" (is "?ub")
   2.101 +proof -
   2.102 +  { fix x y z :: float have "x - y * z = x + - y * z"
   2.103 +      by (cases x, cases y, cases z, simp add: plus_float.simps minus_float.simps uminus_float.simps times_float.simps algebra_simps)
   2.104 +  } note diff_mult_minus = this
   2.105 +
   2.106 +  { fix x :: float have "- (- x) = x" by (cases x, auto simp add: uminus_float.simps) } note minus_minus = this
   2.107 +
   2.108 +  have move_minus: "Ifloat (-x) = -1 * Ifloat x" by auto
   2.109 +
   2.110 +  have sum_eq: "(\<Sum>j=0..<n. (1 / real (f (j' + j))) * (Ifloat x)^j) = 
   2.111 +    (\<Sum>j = 0..<n. -1 ^ j * (1 / real (f (j' + j))) * Ifloat (- x) ^ j)"
   2.112 +  proof (rule setsum_cong, simp)
   2.113 +    fix j assume "j \<in> {0 ..< n}"
   2.114 +    show "1 / real (f (j' + j)) * Ifloat x ^ j = -1 ^ j * (1 / real (f (j' + j))) * Ifloat (- x) ^ j"
   2.115 +      unfolding move_minus power_mult_distrib real_mult_assoc[symmetric]
   2.116 +      unfolding real_mult_commute unfolding real_mult_assoc[of "-1^j", symmetric] power_mult_distrib[symmetric]
   2.117 +      by auto
   2.118 +  qed
   2.119 +
   2.120 +  have "0 \<le> Ifloat (-x)" using assms by auto
   2.121 +  from horner_bounds[where G=G and F=F and f=f and s=s and prec=prec
   2.122 +    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,
   2.123 +    OF this f_Suc lb_0 refl ub_0 refl]
   2.124 +  show "?lb" and "?ub" unfolding minus_minus sum_eq
   2.125 +    by auto
   2.126 +qed
   2.127 +
   2.128 +subsection {* Selectors for next even or odd number *}
   2.129 +
   2.130 +text {*
   2.131 +
   2.132 +The horner scheme computes alternating series. To get the upper and lower bounds we need to
   2.133 +guarantee to access a even or odd member. To do this we use @{term get_odd} and @{term get_even}.
   2.134 +
   2.135 +*}
   2.136 +
   2.137 +definition get_odd :: "nat \<Rightarrow> nat" where
   2.138 +  "get_odd n = (if odd n then n else (Suc n))"
   2.139 +
   2.140 +definition get_even :: "nat \<Rightarrow> nat" where
   2.141 +  "get_even n = (if even n then n else (Suc n))"
   2.142 +
   2.143 +lemma get_odd[simp]: "odd (get_odd n)" unfolding get_odd_def by (cases "odd n", auto)
   2.144 +lemma get_even[simp]: "even (get_even n)" unfolding get_even_def by (cases "even n", auto)
   2.145 +lemma get_odd_ex: "\<exists> k. Suc k = get_odd n \<and> odd (Suc k)"
   2.146 +proof (cases "odd n")
   2.147 +  case True hence "0 < n" by (rule odd_pos)
   2.148 +  from gr0_implies_Suc[OF this] obtain k where "Suc k = n" by auto 
   2.149 +  thus ?thesis unfolding get_odd_def if_P[OF True] using True[unfolded `Suc k = n`[symmetric]] by blast
   2.150 +next
   2.151 +  case False hence "odd (Suc n)" by auto
   2.152 +  thus ?thesis unfolding get_odd_def if_not_P[OF False] by blast
   2.153 +qed
   2.154 +
   2.155 +lemma get_even_double: "\<exists>i. get_even n = 2 * i" using get_even[unfolded even_mult_two_ex] .
   2.156 +lemma get_odd_double: "\<exists>i. get_odd n = 2 * i + 1" using get_odd[unfolded odd_Suc_mult_two_ex] by auto
   2.157 +
   2.158 +section "Power function"
   2.159 +
   2.160 +definition float_power_bnds :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float * float" where
   2.161 +"float_power_bnds n l u = (if odd n \<or> 0 < l then (l ^ n, u ^ n)
   2.162 +                      else if u < 0         then (u ^ n, l ^ n)
   2.163 +                                            else (0, (max (-l) u) ^ n))"
   2.164 +
   2.165 +lemma float_power_bnds: assumes "(l1, u1) = float_power_bnds n l u" and "x \<in> {Ifloat l .. Ifloat u}"
   2.166 +  shows "x^n \<in> {Ifloat l1..Ifloat u1}"
   2.167 +proof (cases "even n")
   2.168 +  case True 
   2.169 +  show ?thesis
   2.170 +  proof (cases "0 < l")
   2.171 +    case True hence "odd n \<or> 0 < l" and "0 \<le> Ifloat l" unfolding less_float_def by auto
   2.172 +    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
   2.173 +    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
   2.174 +    thus ?thesis using assms `0 < l` unfolding atLeastAtMost_iff l1 u1 float_power less_float_def by auto
   2.175 +  next
   2.176 +    case False hence P: "\<not> (odd n \<or> 0 < l)" using `even n` by auto
   2.177 +    show ?thesis
   2.178 +    proof (cases "u < 0")
   2.179 +      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
   2.180 +      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] 
   2.181 +	unfolding power_minus_even[OF `even n`] by auto
   2.182 +      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
   2.183 +      ultimately show ?thesis using float_power by auto
   2.184 +    next
   2.185 +      case False 
   2.186 +      have "\<bar>x\<bar> \<le> Ifloat (max (-l) u)"
   2.187 +      proof (cases "-l \<le> u")
   2.188 +	case True thus ?thesis unfolding max_def if_P[OF True] using assms unfolding le_float_def by auto
   2.189 +      next
   2.190 +	case False thus ?thesis unfolding max_def if_not_P[OF False] using assms unfolding le_float_def by auto
   2.191 +      qed
   2.192 +      hence x_abs: "\<bar>x\<bar> \<le> \<bar>Ifloat (max (-l) u)\<bar>" by auto
   2.193 +      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
   2.194 +      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
   2.195 +    qed
   2.196 +  qed
   2.197 +next
   2.198 +  case False hence "odd n \<or> 0 < l" by auto
   2.199 +  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
   2.200 +  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
   2.201 +  thus ?thesis unfolding atLeastAtMost_iff l1 u1 float_power less_float_def by auto
   2.202 +qed
   2.203 +
   2.204 +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"
   2.205 +  using float_power_bnds by auto
   2.206 +
   2.207 +section "Square root"
   2.208 +
   2.209 +text {*
   2.210 +
   2.211 +The square root computation is implemented as newton iteration. As first first step we use the
   2.212 +nearest power of two greater than the square root.
   2.213 +
   2.214 +*}
   2.215 +
   2.216 +fun sqrt_iteration :: "nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float" where
   2.217 +"sqrt_iteration prec 0 (Float m e) = Float 1 ((e + bitlen m) div 2 + 1)" |
   2.218 +"sqrt_iteration prec (Suc m) x = (let y = sqrt_iteration prec m x 
   2.219 +                                  in Float 1 -1 * (y + float_divr prec x y))"
   2.220 +
   2.221 +definition ub_sqrt :: "nat \<Rightarrow> float \<Rightarrow> float option" where 
   2.222 +"ub_sqrt prec x = (if 0 < x then Some (sqrt_iteration prec prec x) else if x < 0 then None else Some 0)"
   2.223 +
   2.224 +definition lb_sqrt :: "nat \<Rightarrow> float \<Rightarrow> float option" where
   2.225 +"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)"
   2.226 +
   2.227 +lemma sqrt_ub_pos_pos_1:
   2.228 +  assumes "sqrt x < b" and "0 < b" and "0 < x"
   2.229 +  shows "sqrt x < (b + x / b)/2"
   2.230 +proof -
   2.231 +  from assms have "0 < (b - sqrt x) ^ 2 " by simp
   2.232 +  also have "\<dots> = b ^ 2 - 2 * b * sqrt x + (sqrt x) ^ 2" by algebra
   2.233 +  also have "\<dots> = b ^ 2 - 2 * b * sqrt x + x" using assms by (simp add: real_sqrt_pow2)
   2.234 +  finally have "0 < b ^ 2 - 2 * b * sqrt x + x" by assumption
   2.235 +  hence "0 < b / 2 - sqrt x + x / (2 * b)" using assms
   2.236 +    by (simp add: field_simps power2_eq_square)
   2.237 +  thus ?thesis by (simp add: field_simps)
   2.238 +qed
   2.239 +
   2.240 +lemma sqrt_iteration_bound: assumes "0 < Ifloat x"
   2.241 +  shows "sqrt (Ifloat x) < Ifloat (sqrt_iteration prec n x)"
   2.242 +proof (induct n)
   2.243 +  case 0
   2.244 +  show ?case
   2.245 +  proof (cases x)
   2.246 +    case (Float m e)
   2.247 +    hence "0 < m" using float_pos_m_pos[unfolded less_float_def] assms by auto
   2.248 +    hence "0 < sqrt (real m)" by auto
   2.249 +
   2.250 +    have int_nat_bl: "int (nat (bitlen m)) = bitlen m" using bitlen_ge0 by auto
   2.251 +
   2.252 +    have "Ifloat x = (real m / 2^nat (bitlen m)) * pow2 (e + int (nat (bitlen m)))"
   2.253 +      unfolding pow2_add pow2_int Float Ifloat.simps by auto
   2.254 +    also have "\<dots> < 1 * pow2 (e + int (nat (bitlen m)))"
   2.255 +    proof (rule mult_strict_right_mono, auto)
   2.256 +      show "real m < 2^nat (bitlen m)" using bitlen_bounds[OF `0 < m`, THEN conjunct2] 
   2.257 +	unfolding real_of_int_less_iff[of m, symmetric] by auto
   2.258 +    qed
   2.259 +    finally have "sqrt (Ifloat x) < sqrt (pow2 (e + bitlen m))" unfolding int_nat_bl by auto
   2.260 +    also have "\<dots> \<le> pow2 ((e + bitlen m) div 2 + 1)"
   2.261 +    proof -
   2.262 +      let ?E = "e + bitlen m"
   2.263 +      have E_mod_pow: "pow2 (?E mod 2) < 4"
   2.264 +      proof (cases "?E mod 2 = 1")
   2.265 +	case True thus ?thesis by auto
   2.266 +      next
   2.267 +	case False 
   2.268 +	have "0 \<le> ?E mod 2" by auto 
   2.269 +	have "?E mod 2 < 2" by auto
   2.270 +	from this[THEN zless_imp_add1_zle]
   2.271 +	have "?E mod 2 \<le> 0" using False by auto
   2.272 +	from xt1(5)[OF `0 \<le> ?E mod 2` this]
   2.273 +	show ?thesis by auto
   2.274 +      qed
   2.275 +      hence "sqrt (pow2 (?E mod 2)) < sqrt (2 * 2)" by auto
   2.276 +      hence E_mod_pow: "sqrt (pow2 (?E mod 2)) < 2" unfolding real_sqrt_abs2 by auto
   2.277 +
   2.278 +      have E_eq: "pow2 ?E = pow2 (?E div 2 + ?E div 2 + ?E mod 2)" by auto
   2.279 +      have "sqrt (pow2 ?E) = sqrt (pow2 (?E div 2) * pow2 (?E div 2) * pow2 (?E mod 2))"
   2.280 +	unfolding E_eq unfolding pow2_add ..
   2.281 +      also have "\<dots> = pow2 (?E div 2) * sqrt (pow2 (?E mod 2))"
   2.282 +	unfolding real_sqrt_mult[of _ "pow2 (?E mod 2)"] real_sqrt_abs2 by auto
   2.283 +      also have "\<dots> < pow2 (?E div 2) * 2" 
   2.284 +	by (rule mult_strict_left_mono, auto intro: E_mod_pow)
   2.285 +      also have "\<dots> = pow2 (?E div 2 + 1)" unfolding zadd_commute[of _ 1] pow2_add1 by auto
   2.286 +      finally show ?thesis by auto
   2.287 +    qed
   2.288 +    finally show ?thesis 
   2.289 +      unfolding Float sqrt_iteration.simps Ifloat.simps by auto
   2.290 +  qed
   2.291 +next
   2.292 +  case (Suc n)
   2.293 +  let ?b = "sqrt_iteration prec n x"
   2.294 +  have "0 < sqrt (Ifloat x)" using `0 < Ifloat x` by auto
   2.295 +  also have "\<dots> < Ifloat ?b" using Suc .
   2.296 +  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
   2.297 +  also have "\<dots> \<le> (Ifloat ?b + Ifloat (float_divr prec x ?b))/2" by (rule divide_right_mono, auto simp add: float_divr)
   2.298 +  also have "\<dots> = Ifloat (Float 1 -1) * (Ifloat ?b + Ifloat (float_divr prec x ?b))" by auto
   2.299 +  finally show ?case unfolding sqrt_iteration.simps Let_def Ifloat_mult Ifloat_add right_distrib .
   2.300 +qed
   2.301 +
   2.302 +lemma sqrt_iteration_lower_bound: assumes "0 < Ifloat x"
   2.303 +  shows "0 < Ifloat (sqrt_iteration prec n x)" (is "0 < ?sqrt")
   2.304 +proof -
   2.305 +  have "0 < sqrt (Ifloat x)" using assms by auto
   2.306 +  also have "\<dots> < ?sqrt" using sqrt_iteration_bound[OF assms] .
   2.307 +  finally show ?thesis .
   2.308 +qed
   2.309 +
   2.310 +lemma lb_sqrt_lower_bound: assumes "0 \<le> Ifloat x"
   2.311 +  shows "0 \<le> Ifloat (the (lb_sqrt prec x))"
   2.312 +proof (cases "0 < x")
   2.313 +  case True hence "0 < Ifloat x" and "0 \<le> x" using `0 \<le> Ifloat x` unfolding less_float_def le_float_def by auto
   2.314 +  hence "0 < sqrt_iteration prec prec x" unfolding less_float_def using sqrt_iteration_lower_bound by auto 
   2.315 +  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
   2.316 +  thus ?thesis unfolding lb_sqrt_def using True by auto
   2.317 +next
   2.318 +  case False with `0 \<le> Ifloat x` have "Ifloat x = 0" unfolding less_float_def by auto
   2.319 +  thus ?thesis unfolding lb_sqrt_def less_float_def by auto
   2.320 +qed
   2.321 +
   2.322 +lemma lb_sqrt_upper_bound: assumes "0 \<le> Ifloat x"
   2.323 +  shows "Ifloat (the (lb_sqrt prec x)) \<le> sqrt (Ifloat x)"
   2.324 +proof (cases "0 < x")
   2.325 +  case True hence "0 < Ifloat x" and "0 \<le> Ifloat x" unfolding less_float_def by auto
   2.326 +  hence sqrt_gt0: "0 < sqrt (Ifloat x)" by auto
   2.327 +  hence sqrt_ub: "sqrt (Ifloat x) < Ifloat (sqrt_iteration prec prec x)" using sqrt_iteration_bound by auto
   2.328 +  
   2.329 +  have "Ifloat (float_divl prec x (sqrt_iteration prec prec x)) \<le> Ifloat x / Ifloat (sqrt_iteration prec prec x)" by (rule float_divl)
   2.330 +  also have "\<dots> < Ifloat x / sqrt (Ifloat x)" 
   2.331 +    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]])
   2.332 +  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
   2.333 +  finally show ?thesis unfolding lb_sqrt_def if_P[OF `0 < x`] by auto
   2.334 +next
   2.335 +  case False with `0 \<le> Ifloat x`
   2.336 +  have "\<not> x < 0" unfolding less_float_def le_float_def by auto
   2.337 +  show ?thesis unfolding lb_sqrt_def if_not_P[OF False] if_not_P[OF `\<not> x < 0`] using assms by auto
   2.338 +qed
   2.339 +
   2.340 +lemma lb_sqrt: assumes "Some y = lb_sqrt prec x"
   2.341 +  shows "Ifloat y \<le> sqrt (Ifloat x)" and "0 \<le> Ifloat x"
   2.342 +proof -
   2.343 +  show "0 \<le> Ifloat x"
   2.344 +  proof (rule ccontr)
   2.345 +    assume "\<not> 0 \<le> Ifloat x"
   2.346 +    hence "lb_sqrt prec x = None" unfolding lb_sqrt_def less_float_def by auto
   2.347 +    thus False using assms by auto
   2.348 +  qed
   2.349 +  from lb_sqrt_upper_bound[OF this, of prec]
   2.350 +  show "Ifloat y \<le> sqrt (Ifloat x)" unfolding assms[symmetric] by auto
   2.351 +qed
   2.352 +
   2.353 +lemma ub_sqrt_lower_bound: assumes "0 \<le> Ifloat x"
   2.354 +  shows "sqrt (Ifloat x) \<le> Ifloat (the (ub_sqrt prec x))"
   2.355 +proof (cases "0 < x")
   2.356 +  case True hence "0 < Ifloat x" unfolding less_float_def by auto
   2.357 +  hence "0 < sqrt (Ifloat x)" by auto
   2.358 +  hence "sqrt (Ifloat x) < Ifloat (sqrt_iteration prec prec x)" using sqrt_iteration_bound by auto
   2.359 +  thus ?thesis unfolding ub_sqrt_def if_P[OF `0 < x`] by auto
   2.360 +next
   2.361 +  case False with `0 \<le> Ifloat x`
   2.362 +  have "Ifloat x = 0" unfolding less_float_def le_float_def by auto
   2.363 +  thus ?thesis unfolding ub_sqrt_def less_float_def le_float_def by auto
   2.364 +qed
   2.365 +
   2.366 +lemma ub_sqrt: assumes "Some y = ub_sqrt prec x"
   2.367 +  shows "sqrt (Ifloat x) \<le> Ifloat y" and "0 \<le> Ifloat x"
   2.368 +proof -
   2.369 +  show "0 \<le> Ifloat x"
   2.370 +  proof (rule ccontr)
   2.371 +    assume "\<not> 0 \<le> Ifloat x"
   2.372 +    hence "ub_sqrt prec x = None" unfolding ub_sqrt_def less_float_def by auto
   2.373 +    thus False using assms by auto
   2.374 +  qed
   2.375 +  from ub_sqrt_lower_bound[OF this, of prec]
   2.376 +  show "sqrt (Ifloat x) \<le> Ifloat y" unfolding assms[symmetric] by auto
   2.377 +qed
   2.378 +
   2.379 +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"
   2.380 +proof (rule allI, rule allI, rule allI, rule impI)
   2.381 +  fix x lx ux
   2.382 +  assume "(Some l, Some u) = (lb_sqrt prec lx, ub_sqrt prec ux) \<and> x \<in> {Ifloat lx .. Ifloat ux}"
   2.383 +  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
   2.384 +  
   2.385 +  have "Ifloat lx \<le> x" and "x \<le> Ifloat ux" using x by auto
   2.386 +
   2.387 +  from lb_sqrt(1)[OF l] real_sqrt_le_mono[OF `Ifloat lx \<le> x`]
   2.388 +  have "Ifloat l \<le> sqrt x" by (rule order_trans)
   2.389 +  moreover
   2.390 +  from real_sqrt_le_mono[OF `x \<le> Ifloat ux`] ub_sqrt(1)[OF u]
   2.391 +  have "sqrt x \<le> Ifloat u" by (rule order_trans)
   2.392 +  ultimately show "Ifloat l \<le> sqrt x \<and> sqrt x \<le> Ifloat u" ..
   2.393 +qed
   2.394 +
   2.395 +section "Arcus tangens and \<pi>"
   2.396 +
   2.397 +subsection "Compute arcus tangens series"
   2.398 +
   2.399 +text {*
   2.400 +
   2.401 +As first step we implement the computation of the arcus tangens series. This is only valid in the range
   2.402 +@{term "{-1 :: real .. 1}"}. This is used to compute \<pi> and then the entire arcus tangens.
   2.403 +
   2.404 +*}
   2.405 +
   2.406 +fun ub_arctan_horner :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float"
   2.407 +and lb_arctan_horner :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float" where
   2.408 +  "ub_arctan_horner prec 0 k x = 0"
   2.409 +| "ub_arctan_horner prec (Suc n) k x = 
   2.410 +    (rapprox_rat prec 1 (int k)) - x * (lb_arctan_horner prec n (k + 2) x)"
   2.411 +| "lb_arctan_horner prec 0 k x = 0"
   2.412 +| "lb_arctan_horner prec (Suc n) k x = 
   2.413 +    (lapprox_rat prec 1 (int k)) - x * (ub_arctan_horner prec n (k + 2) x)"
   2.414 +
   2.415 +lemma arctan_0_1_bounds': assumes "0 \<le> Ifloat x" "Ifloat x \<le> 1" and "even n"
   2.416 +  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))}"
   2.417 +proof -
   2.418 +  let "?c i" = "-1^i * (1 / real (i * 2 + 1) * Ifloat x ^ (i * 2 + 1))"
   2.419 +  let "?S n" = "\<Sum> i=0..<n. ?c i"
   2.420 +
   2.421 +  have "0 \<le> Ifloat (x * x)" by auto
   2.422 +  from `even n` obtain m where "2 * m = n" unfolding even_mult_two_ex by auto
   2.423 +  
   2.424 +  have "arctan (Ifloat x) \<in> { ?S n .. ?S (Suc n) }"
   2.425 +  proof (cases "Ifloat x = 0")
   2.426 +    case False
   2.427 +    hence "0 < Ifloat x" using `0 \<le> Ifloat x` by auto
   2.428 +    hence prem: "0 < 1 / real (0 * 2 + (1::nat)) * Ifloat x ^ (0 * 2 + 1)" by auto 
   2.429 +
   2.430 +    have "\<bar> Ifloat x \<bar> \<le> 1"  using `0 \<le> Ifloat x` `Ifloat x \<le> 1` by auto
   2.431 +    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`]
   2.432 +    show ?thesis unfolding arctan_series[OF `\<bar> Ifloat x \<bar> \<le> 1`] Suc_plus1  .
   2.433 +  qed auto
   2.434 +  note arctan_bounds = this[unfolded atLeastAtMost_iff]
   2.435 +
   2.436 +  have F: "\<And>n. 2 * Suc n + 1 = 2 * n + 1 + 2" by auto
   2.437 +
   2.438 +  note bounds = horner_bounds[where s=1 and f="\<lambda>i. 2 * i + 1" and j'=0 
   2.439 +    and lb="\<lambda>n i k x. lb_arctan_horner prec n k x"
   2.440 +    and ub="\<lambda>n i k x. ub_arctan_horner prec n k x", 
   2.441 +    OF `0 \<le> Ifloat (x*x)` F lb_arctan_horner.simps ub_arctan_horner.simps]
   2.442 +
   2.443 +  { have "Ifloat (x * lb_arctan_horner prec n 1 (x*x)) \<le> ?S n"
   2.444 +      using bounds(1) `0 \<le> Ifloat x`
   2.445 +      unfolding Ifloat_mult power_add power_one_right real_mult_assoc[symmetric] setsum_left_distrib[symmetric]
   2.446 +      unfolding real_mult_commute mult_commute[of _ "2::nat"] power_mult power2_eq_square[of "Ifloat x"]
   2.447 +      by (auto intro!: mult_left_mono)
   2.448 +    also have "\<dots> \<le> arctan (Ifloat x)" using arctan_bounds ..
   2.449 +    finally have "Ifloat (x * lb_arctan_horner prec n 1 (x*x)) \<le> arctan (Ifloat x)" . }
   2.450 +  moreover
   2.451 +  { have "arctan (Ifloat x) \<le> ?S (Suc n)" using arctan_bounds ..
   2.452 +    also have "\<dots> \<le> Ifloat (x * ub_arctan_horner prec (Suc n) 1 (x*x))"
   2.453 +      using bounds(2)[of "Suc n"] `0 \<le> Ifloat x`
   2.454 +      unfolding Ifloat_mult power_add power_one_right real_mult_assoc[symmetric] setsum_left_distrib[symmetric]
   2.455 +      unfolding real_mult_commute mult_commute[of _ "2::nat"] power_mult power2_eq_square[of "Ifloat x"]
   2.456 +      by (auto intro!: mult_left_mono)
   2.457 +    finally have "arctan (Ifloat x) \<le> Ifloat (x * ub_arctan_horner prec (Suc n) 1 (x*x))" . }
   2.458 +  ultimately show ?thesis by auto
   2.459 +qed
   2.460 +
   2.461 +lemma arctan_0_1_bounds: assumes "0 \<le> Ifloat x" "Ifloat x \<le> 1"
   2.462 +  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))}"
   2.463 +proof (cases "even n")
   2.464 +  case True
   2.465 +  obtain n' where "Suc n' = get_odd n" and "odd (Suc n')" using get_odd_ex by auto
   2.466 +  hence "even n'" unfolding even_nat_Suc by auto
   2.467 +  have "arctan (Ifloat x) \<le> Ifloat (x * ub_arctan_horner prec (get_odd n) 1 (x * x))"
   2.468 +    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
   2.469 +  moreover
   2.470 +  have "Ifloat (x * lb_arctan_horner prec (get_even n) 1 (x * x)) \<le> arctan (Ifloat x)"
   2.471 +    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
   2.472 +  ultimately show ?thesis by auto
   2.473 +next
   2.474 +  case False hence "0 < n" by (rule odd_pos)
   2.475 +  from gr0_implies_Suc[OF this] obtain n' where "n = Suc n'" ..
   2.476 +  from False[unfolded this even_nat_Suc]
   2.477 +  have "even n'" and "even (Suc (Suc n'))" by auto
   2.478 +  have "get_odd n = Suc n'" unfolding get_odd_def if_P[OF False] using `n = Suc n'` .
   2.479 +
   2.480 +  have "arctan (Ifloat x) \<le> Ifloat (x * ub_arctan_horner prec (get_odd n) 1 (x * x))"
   2.481 +    unfolding `get_odd n = Suc n'` using arctan_0_1_bounds'[OF `0 \<le> Ifloat x` `Ifloat x \<le> 1` `even n'`] by auto
   2.482 +  moreover
   2.483 +  have "Ifloat (x * lb_arctan_horner prec (get_even n) 1 (x * x)) \<le> arctan (Ifloat x)"
   2.484 +    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
   2.485 +  ultimately show ?thesis by auto
   2.486 +qed
   2.487 +
   2.488 +subsection "Compute \<pi>"
   2.489 +
   2.490 +definition ub_pi :: "nat \<Rightarrow> float" where
   2.491 +  "ub_pi prec = (let A = rapprox_rat prec 1 5 ; 
   2.492 +                     B = lapprox_rat prec 1 239
   2.493 +                 in ((Float 1 2) * ((Float 1 2) * A * (ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (A * A)) - 
   2.494 +                                                  B * (lb_arctan_horner prec (get_even (prec div 14 + 1)) 1 (B * B)))))"
   2.495 +
   2.496 +definition lb_pi :: "nat \<Rightarrow> float" where
   2.497 +  "lb_pi prec = (let A = lapprox_rat prec 1 5 ; 
   2.498 +                     B = rapprox_rat prec 1 239
   2.499 +                 in ((Float 1 2) * ((Float 1 2) * A * (lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (A * A)) - 
   2.500 +                                                  B * (ub_arctan_horner prec (get_odd (prec div 14 + 1)) 1 (B * B)))))"
   2.501 +
   2.502 +lemma pi_boundaries: "pi \<in> {Ifloat (lb_pi n) .. Ifloat (ub_pi n)}"
   2.503 +proof -
   2.504 +  have machin_pi: "pi = 4 * (4 * arctan (1 / 5) - arctan (1 / 239))" unfolding machin[symmetric] by auto
   2.505 +
   2.506 +  { fix prec n :: nat fix k :: int assume "1 < k" hence "0 \<le> k" and "0 < k" and "1 \<le> k" by auto
   2.507 +    let ?k = "rapprox_rat prec 1 k"
   2.508 +    have "1 div k = 0" using div_pos_pos_trivial[OF _ `1 < k`] by auto
   2.509 +      
   2.510 +    have "0 \<le> Ifloat ?k" by (rule order_trans[OF _ rapprox_rat], auto simp add: `0 \<le> k`)
   2.511 +    have "Ifloat ?k \<le> 1" unfolding rapprox_rat.simps(2)[OF zero_le_one `0 < k`]
   2.512 +      by (rule rapprox_posrat_le1, auto simp add: `0 < k` `1 \<le> k`)
   2.513 +
   2.514 +    have "1 / real k \<le> Ifloat ?k" using rapprox_rat[where x=1 and y=k] by auto
   2.515 +    hence "arctan (1 / real k) \<le> arctan (Ifloat ?k)" by (rule arctan_monotone')
   2.516 +    also have "\<dots> \<le> Ifloat (?k * ub_arctan_horner prec (get_odd n) 1 (?k * ?k))"
   2.517 +      using arctan_0_1_bounds[OF `0 \<le> Ifloat ?k` `Ifloat ?k \<le> 1`] by auto
   2.518 +    finally have "arctan (1 / (real k)) \<le> Ifloat (?k * ub_arctan_horner prec (get_odd n) 1 (?k * ?k))" .
   2.519 +  } note ub_arctan = this
   2.520 +
   2.521 +  { fix prec n :: nat fix k :: int assume "1 < k" hence "0 \<le> k" and "0 < k" by auto
   2.522 +    let ?k = "lapprox_rat prec 1 k"
   2.523 +    have "1 div k = 0" using div_pos_pos_trivial[OF _ `1 < k`] by auto
   2.524 +    have "1 / real k \<le> 1" using `1 < k` by auto
   2.525 +
   2.526 +    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`)
   2.527 +    have "\<And>n. Ifloat ?k \<le> 1" using lapprox_rat by (rule order_trans, auto simp add: `1 / real k \<le> 1`)
   2.528 +
   2.529 +    have "Ifloat ?k \<le> 1 / real k" using lapprox_rat[where x=1 and y=k] by auto
   2.530 +
   2.531 +    have "Ifloat (?k * lb_arctan_horner prec (get_even n) 1 (?k * ?k)) \<le> arctan (Ifloat ?k)"
   2.532 +      using arctan_0_1_bounds[OF `0 \<le> Ifloat ?k` `Ifloat ?k \<le> 1`] by auto
   2.533 +    also have "\<dots> \<le> arctan (1 / real k)" using `Ifloat ?k \<le> 1 / real k` by (rule arctan_monotone')
   2.534 +    finally have "Ifloat (?k * lb_arctan_horner prec (get_even n) 1 (?k * ?k)) \<le> arctan (1 / (real k))" .
   2.535 +  } note lb_arctan = this
   2.536 +
   2.537 +  have "pi \<le> Ifloat (ub_pi n)"
   2.538 +    unfolding ub_pi_def machin_pi Let_def Ifloat_mult Ifloat_sub unfolding Float_num
   2.539 +    using lb_arctan[of 239] ub_arctan[of 5]
   2.540 +    by (auto intro!: mult_left_mono add_mono simp add: diff_minus simp del: lapprox_rat.simps rapprox_rat.simps)
   2.541 +  moreover
   2.542 +  have "Ifloat (lb_pi n) \<le> pi"
   2.543 +    unfolding lb_pi_def machin_pi Let_def Ifloat_mult Ifloat_sub Float_num
   2.544 +    using lb_arctan[of 5] ub_arctan[of 239]
   2.545 +    by (auto intro!: mult_left_mono add_mono simp add: diff_minus simp del: lapprox_rat.simps rapprox_rat.simps)
   2.546 +  ultimately show ?thesis by auto
   2.547 +qed
   2.548 +
   2.549 +subsection "Compute arcus tangens in the entire domain"
   2.550 +
   2.551 +function lb_arctan :: "nat \<Rightarrow> float \<Rightarrow> float" and ub_arctan :: "nat \<Rightarrow> float \<Rightarrow> float" where 
   2.552 +  "lb_arctan prec x = (let ub_horner = \<lambda> x. x * ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (x * x) ;
   2.553 +                           lb_horner = \<lambda> x. x * lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (x * x)
   2.554 +    in (if x < 0          then - ub_arctan prec (-x) else
   2.555 +        if x \<le> Float 1 -1 then lb_horner x else
   2.556 +        if x \<le> Float 1 1  then Float 1 1 * lb_horner (float_divl prec x (1 + the (ub_sqrt prec (1 + x * x))))
   2.557 +                          else (let inv = float_divr prec 1 x 
   2.558 +                                in if inv > 1 then 0 
   2.559 +                                              else lb_pi prec * Float 1 -1 - ub_horner inv)))"
   2.560 +
   2.561 +| "ub_arctan prec x = (let lb_horner = \<lambda> x. x * lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (x * x) ;
   2.562 +                           ub_horner = \<lambda> x. x * ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (x * x)
   2.563 +    in (if x < 0          then - lb_arctan prec (-x) else
   2.564 +        if x \<le> Float 1 -1 then ub_horner x else
   2.565 +        if x \<le> Float 1 1  then let y = float_divr prec x (1 + the (lb_sqrt prec (1 + x * x)))
   2.566 +                               in if y > 1 then ub_pi prec * Float 1 -1 
   2.567 +                                           else Float 1 1 * ub_horner y 
   2.568 +                          else ub_pi prec * Float 1 -1 - lb_horner (float_divl prec 1 x)))"
   2.569 +by pat_completeness auto
   2.570 +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)
   2.571 +
   2.572 +declare ub_arctan_horner.simps[simp del]
   2.573 +declare lb_arctan_horner.simps[simp del]
   2.574 +
   2.575 +lemma lb_arctan_bound': assumes "0 \<le> Ifloat x"
   2.576 +  shows "Ifloat (lb_arctan prec x) \<le> arctan (Ifloat x)"
   2.577 +proof -
   2.578 +  have "\<not> x < 0" and "0 \<le> x" unfolding less_float_def le_float_def using `0 \<le> Ifloat x` by auto
   2.579 +  let "?ub_horner x" = "x * ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (x * x)"
   2.580 +    and "?lb_horner x" = "x * lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (x * x)"
   2.581 +
   2.582 +  show ?thesis
   2.583 +  proof (cases "x \<le> Float 1 -1")
   2.584 +    case True hence "Ifloat x \<le> 1" unfolding le_float_def Float_num by auto
   2.585 +    show ?thesis unfolding lb_arctan.simps Let_def if_not_P[OF `\<not> x < 0`] if_P[OF True]
   2.586 +      using arctan_0_1_bounds[OF `0 \<le> Ifloat x` `Ifloat x \<le> 1`] by auto
   2.587 +  next
   2.588 +    case False hence "0 < Ifloat x" unfolding le_float_def Float_num by auto
   2.589 +    let ?R = "1 + sqrt (1 + Ifloat x * Ifloat x)"
   2.590 +    let ?fR = "1 + the (ub_sqrt prec (1 + x * x))"
   2.591 +    let ?DIV = "float_divl prec x ?fR"
   2.592 +    
   2.593 +    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
   2.594 +    hence divisor_gt0: "0 < ?R" by (auto intro: add_pos_nonneg)
   2.595 +
   2.596 +    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)
   2.597 +    hence "?R \<le> Ifloat ?fR" by auto
   2.598 +    hence "0 < ?fR" and "0 < Ifloat ?fR" unfolding less_float_def using `0 < ?R` by auto
   2.599 +
   2.600 +    have monotone: "Ifloat (float_divl prec x ?fR) \<le> Ifloat x / ?R"
   2.601 +    proof -
   2.602 +      have "Ifloat ?DIV \<le> Ifloat x / Ifloat ?fR" by (rule float_divl)
   2.603 +      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]])
   2.604 +      finally show ?thesis .
   2.605 +    qed
   2.606 +
   2.607 +    show ?thesis
   2.608 +    proof (cases "x \<le> Float 1 1")
   2.609 +      case True
   2.610 +      
   2.611 +      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
   2.612 +      also have "\<dots> \<le> Ifloat (the (ub_sqrt prec (1 + x * x)))" by (rule ub_sqrt_lower_bound, auto simp add: sqr_ge0)
   2.613 +      finally have "Ifloat x \<le> Ifloat ?fR" by auto
   2.614 +      moreover have "Ifloat ?DIV \<le> Ifloat x / Ifloat ?fR" by (rule float_divl)
   2.615 +      ultimately have "Ifloat ?DIV \<le> 1" unfolding divide_le_eq_1_pos[OF `0 < Ifloat ?fR`, symmetric] by auto
   2.616 +
   2.617 +      have "0 \<le> Ifloat ?DIV" using float_divl_lower_bound[OF `0 \<le> x` `0 < ?fR`] unfolding le_float_def by auto
   2.618 +
   2.619 +      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
   2.620 +	using arctan_0_1_bounds[OF `0 \<le> Ifloat ?DIV` `Ifloat ?DIV \<le> 1`] by auto
   2.621 +      also have "\<dots> \<le> 2 * arctan (Ifloat x / ?R)"
   2.622 +	using arctan_monotone'[OF monotone] by (auto intro!: mult_left_mono)
   2.623 +      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 . 
   2.624 +      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] .
   2.625 +    next
   2.626 +      case False
   2.627 +      hence "2 < Ifloat x" unfolding le_float_def Float_num by auto
   2.628 +      hence "1 \<le> Ifloat x" by auto
   2.629 +
   2.630 +      let "?invx" = "float_divr prec 1 x"
   2.631 +      have "0 \<le> arctan (Ifloat x)" using arctan_monotone'[OF `0 \<le> Ifloat x`] using arctan_tan[of 0, unfolded tan_zero] by auto
   2.632 +
   2.633 +      show ?thesis
   2.634 +      proof (cases "1 < ?invx")
   2.635 +	case True
   2.636 +	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] 
   2.637 +	  using `0 \<le> arctan (Ifloat x)` by auto
   2.638 +      next
   2.639 +	case False
   2.640 +	hence "Ifloat ?invx \<le> 1" unfolding less_float_def by auto
   2.641 +	have "0 \<le> Ifloat ?invx" by (rule order_trans[OF _ float_divr], auto simp add: `0 \<le> Ifloat x`)
   2.642 +
   2.643 +	have "1 / Ifloat x \<noteq> 0" and "0 < 1 / Ifloat x" using `0 < Ifloat x` by auto
   2.644 +	
   2.645 +	have "arctan (1 / Ifloat x) \<le> arctan (Ifloat ?invx)" unfolding Ifloat_1[symmetric] by (rule arctan_monotone', rule float_divr)
   2.646 +	also have "\<dots> \<le> Ifloat (?ub_horner ?invx)" using arctan_0_1_bounds[OF `0 \<le> Ifloat ?invx` `Ifloat ?invx \<le> 1`] by auto
   2.647 +	finally have "pi / 2 - Ifloat (?ub_horner ?invx) \<le> arctan (Ifloat x)" 
   2.648 +	  using `0 \<le> arctan (Ifloat x)` arctan_inverse[OF `1 / Ifloat x \<noteq> 0`] 
   2.649 +	  unfolding real_sgn_pos[OF `0 < 1 / Ifloat x`] le_diff_eq by auto
   2.650 +	moreover
   2.651 +	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
   2.652 +	ultimately
   2.653 +	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]
   2.654 +	  by auto
   2.655 +      qed
   2.656 +    qed
   2.657 +  qed
   2.658 +qed
   2.659 +
   2.660 +lemma ub_arctan_bound': assumes "0 \<le> Ifloat x"
   2.661 +  shows "arctan (Ifloat x) \<le> Ifloat (ub_arctan prec x)"
   2.662 +proof -
   2.663 +  have "\<not> x < 0" and "0 \<le> x" unfolding less_float_def le_float_def using `0 \<le> Ifloat x` by auto
   2.664 +
   2.665 +  let "?ub_horner x" = "x * ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (x * x)"
   2.666 +    and "?lb_horner x" = "x * lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (x * x)"
   2.667 +
   2.668 +  show ?thesis
   2.669 +  proof (cases "x \<le> Float 1 -1")
   2.670 +    case True hence "Ifloat x \<le> 1" unfolding le_float_def Float_num by auto
   2.671 +    show ?thesis unfolding ub_arctan.simps Let_def if_not_P[OF `\<not> x < 0`] if_P[OF True]
   2.672 +      using arctan_0_1_bounds[OF `0 \<le> Ifloat x` `Ifloat x \<le> 1`] by auto
   2.673 +  next
   2.674 +    case False hence "0 < Ifloat x" unfolding le_float_def Float_num by auto
   2.675 +    let ?R = "1 + sqrt (1 + Ifloat x * Ifloat x)"
   2.676 +    let ?fR = "1 + the (lb_sqrt prec (1 + x * x))"
   2.677 +    let ?DIV = "float_divr prec x ?fR"
   2.678 +    
   2.679 +    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
   2.680 +    hence "0 \<le> Ifloat (1 + x*x)" by auto
   2.681 +    
   2.682 +    hence divisor_gt0: "0 < ?R" by (auto intro: add_pos_nonneg)
   2.683 +
   2.684 +    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)
   2.685 +    hence "Ifloat ?fR \<le> ?R" by auto
   2.686 +    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)`])
   2.687 +
   2.688 +    have monotone: "Ifloat x / ?R \<le> Ifloat (float_divr prec x ?fR)"
   2.689 +    proof -
   2.690 +      from divide_left_mono[OF `Ifloat ?fR \<le> ?R` `0 \<le> Ifloat x` mult_pos_pos[OF divisor_gt0 `0 < Ifloat ?fR`]]
   2.691 +      have "Ifloat x / ?R \<le> Ifloat x / Ifloat ?fR" .
   2.692 +      also have "\<dots> \<le> Ifloat ?DIV" by (rule float_divr)
   2.693 +      finally show ?thesis .
   2.694 +    qed
   2.695 +
   2.696 +    show ?thesis
   2.697 +    proof (cases "x \<le> Float 1 1")
   2.698 +      case True
   2.699 +      show ?thesis
   2.700 +      proof (cases "?DIV > 1")
   2.701 +	case True
   2.702 +	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
   2.703 +	from order_less_le_trans[OF arctan_ubound this, THEN less_imp_le]
   2.704 +	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] .
   2.705 +      next
   2.706 +	case False
   2.707 +	hence "Ifloat ?DIV \<le> 1" unfolding less_float_def by auto
   2.708 +      
   2.709 +	have "0 \<le> Ifloat x / ?R" using `0 \<le> Ifloat x` `0 < ?R` unfolding real_0_le_divide_iff by auto
   2.710 +	hence "0 \<le> Ifloat ?DIV" using monotone by (rule order_trans)
   2.711 +
   2.712 +	have "arctan (Ifloat x) = 2 * arctan (Ifloat x / ?R)" using arctan_half unfolding numeral_2_eq_2 power_Suc2 realpow_0 real_mult_1 .
   2.713 +	also have "\<dots> \<le> 2 * arctan (Ifloat ?DIV)"
   2.714 +	  using arctan_monotone'[OF monotone] by (auto intro!: mult_left_mono)
   2.715 +	also have "\<dots> \<le> Ifloat (Float 1 1 * ?ub_horner ?DIV)" unfolding Ifloat_mult[of "Float 1 1"] Float_num
   2.716 +	  using arctan_0_1_bounds[OF `0 \<le> Ifloat ?DIV` `Ifloat ?DIV \<le> 1`] by auto
   2.717 +	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] .
   2.718 +      qed
   2.719 +    next
   2.720 +      case False
   2.721 +      hence "2 < Ifloat x" unfolding le_float_def Float_num by auto
   2.722 +      hence "1 \<le> Ifloat x" by auto
   2.723 +      hence "0 < Ifloat x" by auto
   2.724 +      hence "0 < x" unfolding less_float_def by auto
   2.725 +
   2.726 +      let "?invx" = "float_divl prec 1 x"
   2.727 +      have "0 \<le> arctan (Ifloat x)" using arctan_monotone'[OF `0 \<le> Ifloat x`] using arctan_tan[of 0, unfolded tan_zero] by auto
   2.728 +
   2.729 +      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`])
   2.730 +      have "0 \<le> Ifloat ?invx" unfolding Ifloat_0[symmetric] by (rule float_divl_lower_bound[unfolded le_float_def], auto simp add: `0 < x`)
   2.731 +	
   2.732 +      have "1 / Ifloat x \<noteq> 0" and "0 < 1 / Ifloat x" using `0 < Ifloat x` by auto
   2.733 +      
   2.734 +      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
   2.735 +      also have "\<dots> \<le> arctan (1 / Ifloat x)" unfolding Ifloat_1[symmetric] by (rule arctan_monotone', rule float_divl)
   2.736 +      finally have "arctan (Ifloat x) \<le> pi / 2 - Ifloat (?lb_horner ?invx)"
   2.737 +	using `0 \<le> arctan (Ifloat x)` arctan_inverse[OF `1 / Ifloat x \<noteq> 0`] 
   2.738 +	unfolding real_sgn_pos[OF `0 < 1 / Ifloat x`] le_diff_eq by auto
   2.739 +      moreover
   2.740 +      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
   2.741 +      ultimately
   2.742 +      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]
   2.743 +	by auto
   2.744 +    qed
   2.745 +  qed
   2.746 +qed
   2.747 +
   2.748 +lemma arctan_boundaries:
   2.749 +  "arctan (Ifloat x) \<in> {Ifloat (lb_arctan prec x) .. Ifloat (ub_arctan prec x)}"
   2.750 +proof (cases "0 \<le> x")
   2.751 +  case True hence "0 \<le> Ifloat x" unfolding le_float_def by auto
   2.752 +  show ?thesis using ub_arctan_bound'[OF `0 \<le> Ifloat x`] lb_arctan_bound'[OF `0 \<le> Ifloat x`] unfolding atLeastAtMost_iff by auto
   2.753 +next
   2.754 +  let ?mx = "-x"
   2.755 +  case False hence "x < 0" and "0 \<le> Ifloat ?mx" unfolding le_float_def less_float_def by auto
   2.756 +  hence bounds: "Ifloat (lb_arctan prec ?mx) \<le> arctan (Ifloat ?mx) \<and> arctan (Ifloat ?mx) \<le> Ifloat (ub_arctan prec ?mx)"
   2.757 +    using ub_arctan_bound'[OF `0 \<le> Ifloat ?mx`] lb_arctan_bound'[OF `0 \<le> Ifloat ?mx`] by auto
   2.758 +  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`]
   2.759 +    unfolding atLeastAtMost_iff using bounds[unfolded Ifloat_minus arctan_minus] by auto
   2.760 +qed
   2.761 +
   2.762 +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"
   2.763 +proof (rule allI, rule allI, rule allI, rule impI)
   2.764 +  fix x lx ux
   2.765 +  assume "(l, u) = (lb_arctan prec lx, ub_arctan prec ux) \<and> x \<in> {Ifloat lx .. Ifloat ux}"
   2.766 +  hence l: "lb_arctan prec lx = l " and u: "ub_arctan prec ux = u" and x: "x \<in> {Ifloat lx .. Ifloat ux}" by auto
   2.767 +
   2.768 +  { from arctan_boundaries[of lx prec, unfolded l]
   2.769 +    have "Ifloat l \<le> arctan (Ifloat lx)" by (auto simp del: lb_arctan.simps)
   2.770 +    also have "\<dots> \<le> arctan x" using x by (auto intro: arctan_monotone')
   2.771 +    finally have "Ifloat l \<le> arctan x" .
   2.772 +  } moreover
   2.773 +  { have "arctan x \<le> arctan (Ifloat ux)" using x by (auto intro: arctan_monotone')
   2.774 +    also have "\<dots> \<le> Ifloat u" using arctan_boundaries[of ux prec, unfolded u] by (auto simp del: ub_arctan.simps)
   2.775 +    finally have "arctan x \<le> Ifloat u" .
   2.776 +  } ultimately show "Ifloat l \<le> arctan x \<and> arctan x \<le> Ifloat u" ..
   2.777 +qed
   2.778 +
   2.779 +section "Sinus and Cosinus"
   2.780 +
   2.781 +subsection "Compute the cosinus and sinus series"
   2.782 +
   2.783 +fun ub_sin_cos_aux :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float"
   2.784 +and lb_sin_cos_aux :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float" where
   2.785 +  "ub_sin_cos_aux prec 0 i k x = 0"
   2.786 +| "ub_sin_cos_aux prec (Suc n) i k x = 
   2.787 +    (rapprox_rat prec 1 (int k)) - x * (lb_sin_cos_aux prec n (i + 2) (k * i * (i + 1)) x)"
   2.788 +| "lb_sin_cos_aux prec 0 i k x = 0"
   2.789 +| "lb_sin_cos_aux prec (Suc n) i k x = 
   2.790 +    (lapprox_rat prec 1 (int k)) - x * (ub_sin_cos_aux prec n (i + 2) (k * i * (i + 1)) x)"
   2.791 +
   2.792 +lemma cos_aux:
   2.793 +  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")
   2.794 +  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")
   2.795 +proof -
   2.796 +  have "0 \<le> Ifloat (x * x)" unfolding Ifloat_mult by auto
   2.797 +  let "?f n" = "fact (2 * n)"
   2.798 +
   2.799 +  { fix n 
   2.800 +    have F: "\<And>m. ((\<lambda>i. i + 2) ^ n) m = m + 2 * n" by (induct n arbitrary: m, auto)
   2.801 +    have "?f (Suc n) = ?f n * ((\<lambda>i. i + 2) ^ n) 1 * (((\<lambda>i. i + 2) ^ n) 1 + 1)"
   2.802 +      unfolding F by auto } note f_eq = this
   2.803 +    
   2.804 +  from horner_bounds[where lb="lb_sin_cos_aux prec" and ub="ub_sin_cos_aux prec" and j'=0, 
   2.805 +    OF `0 \<le> Ifloat (x * x)` f_eq lb_sin_cos_aux.simps ub_sin_cos_aux.simps]
   2.806 +  show "?lb" and "?ub" by (auto simp add: power_mult power2_eq_square[of "Ifloat x"])
   2.807 +qed
   2.808 +
   2.809 +lemma cos_boundaries: assumes "0 \<le> Ifloat x" and "Ifloat x \<le> pi / 2"
   2.810 +  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))}"
   2.811 +proof (cases "Ifloat x = 0")
   2.812 +  case False hence "Ifloat x \<noteq> 0" by auto
   2.813 +  hence "0 < x" and "0 < Ifloat x" using `0 \<le> Ifloat x` unfolding less_float_def by auto
   2.814 +  have "0 < x * x" using `0 < x` unfolding less_float_def Ifloat_mult Ifloat_0
   2.815 +    using mult_pos_pos[where a="Ifloat x" and b="Ifloat x"] by auto
   2.816 +
   2.817 +  { fix x n have "(\<Sum> i=0..<n. -1^i * (1/real (fact (2 * i))) * x^(2 * i))
   2.818 +    = (\<Sum> i = 0 ..< 2 * n. (if even(i) then (-1 ^ (i div 2))/(real (fact i)) else 0) * x ^ i)" (is "?sum = ?ifsum")
   2.819 +  proof -
   2.820 +    have "?sum = ?sum + (\<Sum> j = 0 ..< n. 0)" by auto
   2.821 +    also have "\<dots> = 
   2.822 +      (\<Sum> j = 0 ..< n. -1 ^ ((2 * j) div 2) / (real (fact (2 * j))) * x ^(2 * j)) + (\<Sum> j = 0 ..< n. 0)" by auto
   2.823 +    also have "\<dots> = (\<Sum> i = 0 ..< 2 * n. if even i then -1 ^ (i div 2) / (real (fact i)) * x ^ i else 0)"
   2.824 +      unfolding sum_split_even_odd ..
   2.825 +    also have "\<dots> = (\<Sum> i = 0 ..< 2 * n. (if even i then -1 ^ (i div 2) / (real (fact i)) else 0) * x ^ i)"
   2.826 +      by (rule setsum_cong2) auto
   2.827 +    finally show ?thesis by assumption
   2.828 +  qed } note morph_to_if_power = this
   2.829 +
   2.830 +
   2.831 +  { fix n :: nat assume "0 < n"
   2.832 +    hence "0 < 2 * n" by auto
   2.833 +    obtain t where "0 < t" and "t < Ifloat x" and
   2.834 +      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) 
   2.835 +      + (cos (t + 1/2 * real (2 * n) * pi) / real (fact (2*n))) * (Ifloat x)^(2*n)" 
   2.836 +      (is "_ = ?SUM + ?rest / ?fact * ?pow")
   2.837 +      using Maclaurin_cos_expansion2[OF `0 < Ifloat x` `0 < 2 * n`] by auto
   2.838 +
   2.839 +    have "cos t * -1^n = cos t * cos (real n * pi) + sin t * sin (real n * pi)" by auto
   2.840 +    also have "\<dots> = cos (t + real n * pi)"  using cos_add by auto
   2.841 +    also have "\<dots> = ?rest" by auto
   2.842 +    finally have "cos t * -1^n = ?rest" .
   2.843 +    moreover
   2.844 +    have "t \<le> pi / 2" using `t < Ifloat x` and `Ifloat x \<le> pi / 2` by auto
   2.845 +    hence "0 \<le> cos t" using `0 < t` and cos_ge_zero by auto
   2.846 +    ultimately have even: "even n \<Longrightarrow> 0 \<le> ?rest" and odd: "odd n \<Longrightarrow> 0 \<le> - ?rest " by auto
   2.847 +
   2.848 +    have "0 < ?fact" by auto
   2.849 +    have "0 < ?pow" using `0 < Ifloat x` by auto
   2.850 +
   2.851 +    {
   2.852 +      assume "even n"
   2.853 +      have "Ifloat (lb_sin_cos_aux prec n 1 1 (x * x)) \<le> ?SUM"
   2.854 +	unfolding morph_to_if_power[symmetric] using cos_aux by auto 
   2.855 +      also have "\<dots> \<le> cos (Ifloat x)"
   2.856 +      proof -
   2.857 +	from even[OF `even n`] `0 < ?fact` `0 < ?pow`
   2.858 +	have "0 \<le> (?rest / ?fact) * ?pow" by (metis mult_nonneg_nonneg divide_nonneg_pos less_imp_le)
   2.859 +	thus ?thesis unfolding cos_eq by auto
   2.860 +      qed
   2.861 +      finally have "Ifloat (lb_sin_cos_aux prec n 1 1 (x * x)) \<le> cos (Ifloat x)" .
   2.862 +    } note lb = this
   2.863 +
   2.864 +    {
   2.865 +      assume "odd n"
   2.866 +      have "cos (Ifloat x) \<le> ?SUM"
   2.867 +      proof -
   2.868 +	from `0 < ?fact` and `0 < ?pow` and odd[OF `odd n`]
   2.869 +	have "0 \<le> (- ?rest) / ?fact * ?pow"
   2.870 +	  by (metis mult_nonneg_nonneg divide_nonneg_pos less_imp_le)
   2.871 +	thus ?thesis unfolding cos_eq by auto
   2.872 +      qed
   2.873 +      also have "\<dots> \<le> Ifloat (ub_sin_cos_aux prec n 1 1 (x * x))"
   2.874 +	unfolding morph_to_if_power[symmetric] using cos_aux by auto
   2.875 +      finally have "cos (Ifloat x) \<le> Ifloat (ub_sin_cos_aux prec n 1 1 (x * x))" .
   2.876 +    } note ub = this and lb
   2.877 +  } note ub = this(1) and lb = this(2)
   2.878 +
   2.879 +  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] .
   2.880 +  moreover have "Ifloat (lb_sin_cos_aux prec (get_even n) 1 1 (x * x)) \<le> cos (Ifloat x)" 
   2.881 +  proof (cases "0 < get_even n")
   2.882 +    case True show ?thesis using lb[OF True get_even] .
   2.883 +  next
   2.884 +    case False
   2.885 +    hence "get_even n = 0" by auto
   2.886 +    have "- (pi / 2) \<le> Ifloat x" by (rule order_trans[OF _ `0 < Ifloat x`[THEN less_imp_le]], auto)
   2.887 +    with `Ifloat x \<le> pi / 2`
   2.888 +    show ?thesis unfolding `get_even n = 0` lb_sin_cos_aux.simps Ifloat_minus Ifloat_0 using cos_ge_zero by auto
   2.889 +  qed
   2.890 +  ultimately show ?thesis by auto
   2.891 +next
   2.892 +  case True
   2.893 +  show ?thesis
   2.894 +  proof (cases "n = 0")
   2.895 +    case True 
   2.896 +    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
   2.897 +  next
   2.898 +    case False with not0_implies_Suc obtain m where "n = Suc m" by blast
   2.899 +    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)
   2.900 +  qed
   2.901 +qed
   2.902 +
   2.903 +lemma sin_aux: assumes "0 \<le> Ifloat x"
   2.904 +  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")
   2.905 +  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")
   2.906 +proof -
   2.907 +  have "0 \<le> Ifloat (x * x)" unfolding Ifloat_mult by auto
   2.908 +  let "?f n" = "fact (2 * n + 1)"
   2.909 +
   2.910 +  { fix n 
   2.911 +    have F: "\<And>m. ((\<lambda>i. i + 2) ^ n) m = m + 2 * n" by (induct n arbitrary: m, auto)
   2.912 +    have "?f (Suc n) = ?f n * ((\<lambda>i. i + 2) ^ n) 2 * (((\<lambda>i. i + 2) ^ n) 2 + 1)"
   2.913 +      unfolding F by auto } note f_eq = this
   2.914 +    
   2.915 +  from horner_bounds[where lb="lb_sin_cos_aux prec" and ub="ub_sin_cos_aux prec" and j'=0,
   2.916 +    OF `0 \<le> Ifloat (x * x)` f_eq lb_sin_cos_aux.simps ub_sin_cos_aux.simps]
   2.917 +  show "?lb" and "?ub" using `0 \<le> Ifloat x` unfolding Ifloat_mult
   2.918 +    unfolding power_add power_one_right real_mult_assoc[symmetric] setsum_left_distrib[symmetric]
   2.919 +    unfolding real_mult_commute
   2.920 +    by (auto intro!: mult_left_mono simp add: power_mult power2_eq_square[of "Ifloat x"])
   2.921 +qed
   2.922 +
   2.923 +lemma sin_boundaries: assumes "0 \<le> Ifloat x" and "Ifloat x \<le> pi / 2"
   2.924 +  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))}"
   2.925 +proof (cases "Ifloat x = 0")
   2.926 +  case False hence "Ifloat x \<noteq> 0" by auto
   2.927 +  hence "0 < x" and "0 < Ifloat x" using `0 \<le> Ifloat x` unfolding less_float_def by auto
   2.928 +  have "0 < x * x" using `0 < x` unfolding less_float_def Ifloat_mult Ifloat_0
   2.929 +    using mult_pos_pos[where a="Ifloat x" and b="Ifloat x"] by auto
   2.930 +
   2.931 +  { 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))
   2.932 +    = (\<Sum> i = 0 ..< 2 * n. (if even(i) then 0 else (-1 ^ ((i - Suc 0) div 2))/(real (fact i))) * x ^ i)" (is "?SUM = _")
   2.933 +    proof -
   2.934 +      have pow: "!!i. x ^ (2 * i + 1) = x * x ^ (2 * i)" by auto
   2.935 +      have "?SUM = (\<Sum> j = 0 ..< n. 0) + ?SUM" by auto
   2.936 +      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)"
   2.937 +	unfolding sum_split_even_odd ..
   2.938 +      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)"
   2.939 +	by (rule setsum_cong2) auto
   2.940 +      finally show ?thesis by assumption
   2.941 +    qed } note setsum_morph = this
   2.942 +
   2.943 +  { fix n :: nat assume "0 < n"
   2.944 +    hence "0 < 2 * n + 1" by auto
   2.945 +    obtain t where "0 < t" and "t < Ifloat x" and
   2.946 +      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) 
   2.947 +      + (sin (t + 1/2 * real (2 * n + 1) * pi) / real (fact (2*n + 1))) * (Ifloat x)^(2*n + 1)" 
   2.948 +      (is "_ = ?SUM + ?rest / ?fact * ?pow")
   2.949 +      using Maclaurin_sin_expansion3[OF `0 < 2 * n + 1` `0 < Ifloat x`] by auto
   2.950 +
   2.951 +    have "?rest = cos t * -1^n" unfolding sin_add cos_add real_of_nat_add left_distrib right_distrib by auto
   2.952 +    moreover
   2.953 +    have "t \<le> pi / 2" using `t < Ifloat x` and `Ifloat x \<le> pi / 2` by auto
   2.954 +    hence "0 \<le> cos t" using `0 < t` and cos_ge_zero by auto
   2.955 +    ultimately have even: "even n \<Longrightarrow> 0 \<le> ?rest" and odd: "odd n \<Longrightarrow> 0 \<le> - ?rest " by auto
   2.956 +
   2.957 +    have "0 < ?fact" by (rule real_of_nat_fact_gt_zero)
   2.958 +    have "0 < ?pow" using `0 < Ifloat x` by (rule zero_less_power)
   2.959 +
   2.960 +    {
   2.961 +      assume "even n"
   2.962 +      have "Ifloat (x * lb_sin_cos_aux prec n 2 1 (x * x)) \<le> 
   2.963 +            (\<Sum> i = 0 ..< 2 * n. (if even(i) then 0 else (-1 ^ ((i - Suc 0) div 2))/(real (fact i))) * (Ifloat x) ^ i)"
   2.964 +	using sin_aux[OF `0 \<le> Ifloat x`] unfolding setsum_morph[symmetric] by auto
   2.965 +      also have "\<dots> \<le> ?SUM" by auto
   2.966 +      also have "\<dots> \<le> sin (Ifloat x)"
   2.967 +      proof -
   2.968 +	from even[OF `even n`] `0 < ?fact` `0 < ?pow`
   2.969 +	have "0 \<le> (?rest / ?fact) * ?pow" by (metis mult_nonneg_nonneg divide_nonneg_pos less_imp_le)
   2.970 +	thus ?thesis unfolding sin_eq by auto
   2.971 +      qed
   2.972 +      finally have "Ifloat (x * lb_sin_cos_aux prec n 2 1 (x * x)) \<le> sin (Ifloat x)" .
   2.973 +    } note lb = this
   2.974 +
   2.975 +    {
   2.976 +      assume "odd n"
   2.977 +      have "sin (Ifloat x) \<le> ?SUM"
   2.978 +      proof -
   2.979 +	from `0 < ?fact` and `0 < ?pow` and odd[OF `odd n`]
   2.980 +	have "0 \<le> (- ?rest) / ?fact * ?pow"
   2.981 +	  by (metis mult_nonneg_nonneg divide_nonneg_pos less_imp_le)
   2.982 +	thus ?thesis unfolding sin_eq by auto
   2.983 +      qed
   2.984 +      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)"
   2.985 +	 by auto
   2.986 +      also have "\<dots> \<le> Ifloat (x * ub_sin_cos_aux prec n 2 1 (x * x))" 
   2.987 +	using sin_aux[OF `0 \<le> Ifloat x`] unfolding setsum_morph[symmetric] by auto
   2.988 +      finally have "sin (Ifloat x) \<le> Ifloat (x * ub_sin_cos_aux prec n 2 1 (x * x))" .
   2.989 +    } note ub = this and lb
   2.990 +  } note ub = this(1) and lb = this(2)
   2.991 +
   2.992 +  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] .
   2.993 +  moreover have "Ifloat (x * lb_sin_cos_aux prec (get_even n) 2 1 (x * x)) \<le> sin (Ifloat x)" 
   2.994 +  proof (cases "0 < get_even n")
   2.995 +    case True show ?thesis using lb[OF True get_even] .
   2.996 +  next
   2.997 +    case False
   2.998 +    hence "get_even n = 0" by auto
   2.999 +    with `Ifloat x \<le> pi / 2` `0 \<le> Ifloat x`
  2.1000 +    show ?thesis unfolding `get_even n = 0` ub_sin_cos_aux.simps Ifloat_minus Ifloat_0 using sin_ge_zero by auto
  2.1001 +  qed
  2.1002 +  ultimately show ?thesis by auto
  2.1003 +next
  2.1004 +  case True
  2.1005 +  show ?thesis
  2.1006 +  proof (cases "n = 0")
  2.1007 +    case True 
  2.1008 +    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
  2.1009 +  next
  2.1010 +    case False with not0_implies_Suc obtain m where "n = Suc m" by blast
  2.1011 +    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)
  2.1012 +  qed
  2.1013 +qed
  2.1014 +
  2.1015 +subsection "Compute the cosinus in the entire domain"
  2.1016 +
  2.1017 +definition lb_cos :: "nat \<Rightarrow> float \<Rightarrow> float" where
  2.1018 +"lb_cos prec x = (let
  2.1019 +    horner = \<lambda> x. lb_sin_cos_aux prec (get_even (prec div 4 + 1)) 1 1 (x * x) ;
  2.1020 +    half = \<lambda> x. if x < 0 then - 1 else Float 1 1 * x * x - 1
  2.1021 +  in if x < Float 1 -1 then horner x
  2.1022 +else if x < 1          then half (horner (x * Float 1 -1))
  2.1023 +                       else half (half (horner (x * Float 1 -2))))"
  2.1024 +
  2.1025 +definition ub_cos :: "nat \<Rightarrow> float \<Rightarrow> float" where
  2.1026 +"ub_cos prec x = (let
  2.1027 +    horner = \<lambda> x. ub_sin_cos_aux prec (get_odd (prec div 4 + 1)) 1 1 (x * x) ;
  2.1028 +    half = \<lambda> x. Float 1 1 * x * x - 1
  2.1029 +  in if x < Float 1 -1 then horner x
  2.1030 +else if x < 1          then half (horner (x * Float 1 -1))
  2.1031 +                       else half (half (horner (x * Float 1 -2))))"
  2.1032 +
  2.1033 +definition bnds_cos :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float * float" where
  2.1034 +"bnds_cos prec lx ux = (let  lpi = lb_pi prec
  2.1035 +  in   if lx < -lpi \<or> ux > lpi   then (Float -1 0, Float 1 0)
  2.1036 +  else if ux \<le> 0                 then (lb_cos prec (-lx), ub_cos prec (-ux))
  2.1037 +  else if 0 \<le> lx                 then (lb_cos prec ux, ub_cos prec lx)
  2.1038 +                                 else (min (lb_cos prec (-lx)) (lb_cos prec ux), Float 1 0))"
  2.1039 +
  2.1040 +lemma lb_cos: assumes "0 \<le> Ifloat x" and "Ifloat x \<le> pi" 
  2.1041 +  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) }")
  2.1042 +proof -
  2.1043 +  { fix x :: real
  2.1044 +    have "cos x = cos (x / 2 + x / 2)" by auto
  2.1045 +    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"
  2.1046 +      unfolding cos_add by auto
  2.1047 +    also have "\<dots> = 2 * cos (x / 2) * cos (x / 2) - 1" by algebra
  2.1048 +    finally have "cos x = 2 * cos (x / 2) * cos (x / 2) - 1" .
  2.1049 +  } note x_half = this[symmetric]
  2.1050 +
  2.1051 +  have "\<not> x < 0" using `0 \<le> Ifloat x` unfolding less_float_def by auto
  2.1052 +  let "?ub_horner x" = "ub_sin_cos_aux prec (get_odd (prec div 4 + 1)) 1 1 (x * x)"
  2.1053 +  let "?lb_horner x" = "lb_sin_cos_aux prec (get_even (prec div 4 + 1)) 1 1 (x * x)"
  2.1054 +  let "?ub_half x" = "Float 1 1 * x * x - 1"
  2.1055 +  let "?lb_half x" = "if x < 0 then - 1 else Float 1 1 * x * x - 1"
  2.1056 +
  2.1057 +  show ?thesis
  2.1058 +  proof (cases "x < Float 1 -1")
  2.1059 +    case True hence "Ifloat x \<le> pi / 2" unfolding less_float_def using pi_ge_two by auto
  2.1060 +    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
  2.1061 +      using cos_boundaries[OF `0 \<le> Ifloat x` `Ifloat x \<le> pi / 2`] .
  2.1062 +  next
  2.1063 +    case False
  2.1064 +    
  2.1065 +    { fix y x :: float let ?x2 = "Ifloat (x * Float 1 -1)"
  2.1066 +      assume "Ifloat y \<le> cos ?x2" and "-pi \<le> Ifloat x" and "Ifloat x \<le> pi"
  2.1067 +      hence "- (pi / 2) \<le> ?x2" and "?x2 \<le> pi / 2" using pi_ge_two unfolding Ifloat_mult Float_num by auto
  2.1068 +      hence "0 \<le> cos ?x2" by (rule cos_ge_zero)
  2.1069 +      
  2.1070 +      have "Ifloat (?lb_half y) \<le> cos (Ifloat x)"
  2.1071 +      proof (cases "y < 0")
  2.1072 +	case True show ?thesis using cos_ge_minus_one unfolding if_P[OF True] by auto
  2.1073 +      next
  2.1074 +	case False
  2.1075 +	hence "0 \<le> Ifloat y" unfolding less_float_def by auto
  2.1076 +	from mult_mono[OF `Ifloat y \<le> cos ?x2` `Ifloat y \<le> cos ?x2` `0 \<le> cos ?x2` this]
  2.1077 +	have "Ifloat y * Ifloat y \<le> cos ?x2 * cos ?x2" .
  2.1078 +	hence "2 * Ifloat y * Ifloat y \<le> 2 * cos ?x2 * cos ?x2" by auto
  2.1079 +	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
  2.1080 +	thus ?thesis unfolding if_not_P[OF False] x_half Float_num Ifloat_mult Ifloat_sub by auto
  2.1081 +      qed
  2.1082 +    } note lb_half = this
  2.1083 +    
  2.1084 +    { fix y x :: float let ?x2 = "Ifloat (x * Float 1 -1)"
  2.1085 +      assume ub: "cos ?x2 \<le> Ifloat y" and "- pi \<le> Ifloat x" and "Ifloat x \<le> pi"
  2.1086 +      hence "- (pi / 2) \<le> ?x2" and "?x2 \<le> pi / 2" using pi_ge_two unfolding Ifloat_mult Float_num by auto
  2.1087 +      hence "0 \<le> cos ?x2" by (rule cos_ge_zero)
  2.1088 +      
  2.1089 +      have "cos (Ifloat x) \<le> Ifloat (?ub_half y)"
  2.1090 +      proof -
  2.1091 +	have "0 \<le> Ifloat y" using `0 \<le> cos ?x2` ub by (rule order_trans)
  2.1092 +	from mult_mono[OF ub ub this `0 \<le> cos ?x2`]
  2.1093 +	have "cos ?x2 * cos ?x2 \<le> Ifloat y * Ifloat y" .
  2.1094 +	hence "2 * cos ?x2 * cos ?x2 \<le> 2 * Ifloat y * Ifloat y" by auto
  2.1095 +	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
  2.1096 +	thus ?thesis unfolding x_half Ifloat_mult Float_num Ifloat_sub by auto
  2.1097 +      qed
  2.1098 +    } note ub_half = this
  2.1099 +    
  2.1100 +    let ?x2 = "x * Float 1 -1"
  2.1101 +    let ?x4 = "x * Float 1 -1 * Float 1 -1"
  2.1102 +    
  2.1103 +    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)
  2.1104 +    
  2.1105 +    show ?thesis
  2.1106 +    proof (cases "x < 1")
  2.1107 +      case True hence "Ifloat x \<le> 1" unfolding less_float_def by auto
  2.1108 +      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
  2.1109 +      from cos_boundaries[OF this]
  2.1110 +      have lb: "Ifloat (?lb_horner ?x2) \<le> ?cos ?x2" and ub: "?cos ?x2 \<le> Ifloat (?ub_horner ?x2)" by auto
  2.1111 +      
  2.1112 +      have "Ifloat (?lb x) \<le> ?cos x"
  2.1113 +      proof -
  2.1114 +	from lb_half[OF lb `-pi \<le> Ifloat x` `Ifloat x \<le> pi`]
  2.1115 +	show ?thesis unfolding lb_cos_def[where x=x] Let_def using `\<not> x < 0` `\<not> x < Float 1 -1` `x < 1` by auto
  2.1116 +      qed
  2.1117 +      moreover have "?cos x \<le> Ifloat (?ub x)"
  2.1118 +      proof -
  2.1119 +	from ub_half[OF ub `-pi \<le> Ifloat x` `Ifloat x \<le> pi`]
  2.1120 +	show ?thesis unfolding ub_cos_def[where x=x] Let_def using `\<not> x < 0` `\<not> x < Float 1 -1` `x < 1` by auto 
  2.1121 +      qed
  2.1122 +      ultimately show ?thesis by auto
  2.1123 +    next
  2.1124 +      case False
  2.1125 +      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
  2.1126 +      from cos_boundaries[OF this]
  2.1127 +      have lb: "Ifloat (?lb_horner ?x4) \<le> ?cos ?x4" and ub: "?cos ?x4 \<le> Ifloat (?ub_horner ?x4)" by auto
  2.1128 +      
  2.1129 +      have eq_4: "?x2 * Float 1 -1 = x * Float 1 -2" by (cases x, auto simp add: times_float.simps)
  2.1130 +      
  2.1131 +      have "Ifloat (?lb x) \<le> ?cos x"
  2.1132 +      proof -
  2.1133 +	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
  2.1134 +	from lb_half[OF lb_half[OF lb this] `-pi \<le> Ifloat x` `Ifloat x \<le> pi`, unfolded eq_4]
  2.1135 +	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 .
  2.1136 +      qed
  2.1137 +      moreover have "?cos x \<le> Ifloat (?ub x)"
  2.1138 +      proof -
  2.1139 +	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
  2.1140 +	from ub_half[OF ub_half[OF ub this] `-pi \<le> Ifloat x` `Ifloat x \<le> pi`, unfolded eq_4]
  2.1141 +	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 .
  2.1142 +      qed
  2.1143 +      ultimately show ?thesis by auto
  2.1144 +    qed
  2.1145 +  qed
  2.1146 +qed
  2.1147 +
  2.1148 +lemma lb_cos_minus: assumes "-pi \<le> Ifloat x" and "Ifloat x \<le> 0" 
  2.1149 +  shows "cos (Ifloat (-x)) \<in> {Ifloat (lb_cos prec (-x)) .. Ifloat (ub_cos prec (-x))}"
  2.1150 +proof -
  2.1151 +  have "0 \<le> Ifloat (-x)" and "Ifloat (-x) \<le> pi" using `-pi \<le> Ifloat x` `Ifloat x \<le> 0` by auto
  2.1152 +  from lb_cos[OF this] show ?thesis .
  2.1153 +qed
  2.1154 +
  2.1155 +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"
  2.1156 +proof (rule allI, rule allI, rule allI, rule impI)
  2.1157 +  fix x lx ux
  2.1158 +  assume "(l, u) = bnds_cos prec lx ux \<and> x \<in> {Ifloat lx .. Ifloat ux}"
  2.1159 +  hence bnds: "(l, u) = bnds_cos prec lx ux" and x: "x \<in> {Ifloat lx .. Ifloat ux}" by auto
  2.1160 +
  2.1161 +  let ?lpi = "lb_pi prec"  
  2.1162 +  have [intro!]: "Ifloat lx \<le> Ifloat ux" using x by auto
  2.1163 +  hence "lx \<le> ux" unfolding le_float_def .
  2.1164 +
  2.1165 +  show "Ifloat l \<le> cos x \<and> cos x \<le> Ifloat u"
  2.1166 +  proof (cases "lx < -?lpi \<or> ux > ?lpi")
  2.1167 +    case True
  2.1168 +    show ?thesis using bnds unfolding bnds_cos_def if_P[OF True] Let_def using cos_le_one cos_ge_minus_one by auto
  2.1169 +  next
  2.1170 +    case False note not_out = this
  2.1171 +    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
  2.1172 +
  2.1173 +    from pi_boundaries[unfolded atLeastAtMost_iff, THEN conjunct1, THEN le_imp_neg_le] lpi_lx
  2.1174 +    have "- pi \<le> Ifloat lx" by (rule order_trans)
  2.1175 +    hence "- pi \<le> x" and "- pi \<le> Ifloat ux" and "x \<le> Ifloat ux" using x by auto
  2.1176 +    
  2.1177 +    from lpi_ux pi_boundaries[unfolded atLeastAtMost_iff, THEN conjunct1]
  2.1178 +    have "Ifloat ux \<le> pi" by (rule order_trans)
  2.1179 +    hence "x \<le> pi" and "Ifloat lx \<le> pi" and "Ifloat lx \<le> x" using x by auto
  2.1180 +
  2.1181 +    note lb_cos_minus_bottom = lb_cos_minus[unfolded atLeastAtMost_iff, THEN conjunct1]
  2.1182 +    note lb_cos_minus_top = lb_cos_minus[unfolded atLeastAtMost_iff, THEN conjunct2]
  2.1183 +    note lb_cos_bottom = lb_cos[unfolded atLeastAtMost_iff, THEN conjunct1]
  2.1184 +    note lb_cos_top = lb_cos[unfolded atLeastAtMost_iff, THEN conjunct2]
  2.1185 +
  2.1186 +    show ?thesis
  2.1187 +    proof (cases "ux \<le> 0")
  2.1188 +      case True hence "Ifloat ux \<le> 0" unfolding le_float_def by auto
  2.1189 +      hence "x \<le> 0" and "Ifloat lx \<le> 0" using x by auto
  2.1190 +      
  2.1191 +      { have "Ifloat (lb_cos prec (-lx)) \<le> cos (Ifloat (-lx))" using lb_cos_minus_bottom[OF `-pi \<le> Ifloat lx` `Ifloat lx \<le> 0`] .
  2.1192 +	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`] .
  2.1193 +	finally have "Ifloat (lb_cos prec (-lx)) \<le> cos x" . }
  2.1194 +      moreover
  2.1195 +      { 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`] .
  2.1196 +	also have "\<dots> \<le> Ifloat (ub_cos prec (-ux))" using lb_cos_minus_top[OF `-pi \<le> Ifloat ux` `Ifloat ux \<le> 0`] .
  2.1197 +	finally have "cos x \<le> Ifloat (ub_cos prec (-ux))" . }
  2.1198 +      ultimately show ?thesis using bnds unfolding bnds_cos_def Let_def if_not_P[OF not_out] if_P[OF True] by auto
  2.1199 +    next
  2.1200 +      case False note not_ux = this
  2.1201 +      
  2.1202 +      show ?thesis
  2.1203 +      proof (cases "0 \<le> lx")
  2.1204 +	case True hence "0 \<le> Ifloat lx" unfolding le_float_def by auto
  2.1205 +	hence "0 \<le> x" and "0 \<le> Ifloat ux" using x by auto
  2.1206 +      
  2.1207 +	{ have "Ifloat (lb_cos prec ux) \<le> cos (Ifloat ux)" using lb_cos_bottom[OF `0 \<le> Ifloat ux` `Ifloat ux \<le> pi`] .
  2.1208 +	  also have "\<dots> \<le> cos x" using cos_monotone_0_pi'[OF `0 \<le> x` `x \<le> Ifloat ux` `Ifloat ux \<le> pi`] .
  2.1209 +	  finally have "Ifloat (lb_cos prec ux) \<le> cos x" . }
  2.1210 +	moreover
  2.1211 +	{ have "cos x \<le> cos (Ifloat lx)" using cos_monotone_0_pi'[OF `0 \<le> Ifloat lx` `Ifloat lx \<le> x` `x \<le> pi`] .
  2.1212 +	  also have "\<dots> \<le> Ifloat (ub_cos prec lx)" using lb_cos_top[OF `0 \<le> Ifloat lx` `Ifloat lx \<le> pi`] .
  2.1213 +	  finally have "cos x \<le> Ifloat (ub_cos prec lx)" . }
  2.1214 +	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
  2.1215 +      next
  2.1216 +	case False with not_ux
  2.1217 +	have "Ifloat lx \<le> 0" and "0 \<le> Ifloat ux" unfolding le_float_def by auto
  2.1218 +
  2.1219 +	have "Ifloat (min (lb_cos prec (-lx)) (lb_cos prec ux)) \<le> cos x"
  2.1220 +	proof (cases "x \<le> 0")
  2.1221 +	  case True
  2.1222 +	  have "Ifloat (lb_cos prec (-lx)) \<le> cos (Ifloat (-lx))" using lb_cos_minus_bottom[OF `-pi \<le> Ifloat lx` `Ifloat lx \<le> 0`] .
  2.1223 +	  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`] .
  2.1224 +	  finally show ?thesis unfolding Ifloat_min by auto
  2.1225 +	next
  2.1226 +	  case False hence "0 \<le> x" by auto
  2.1227 +	  have "Ifloat (lb_cos prec ux) \<le> cos (Ifloat ux)" using lb_cos_bottom[OF `0 \<le> Ifloat ux` `Ifloat ux \<le> pi`] .
  2.1228 +	  also have "\<dots> \<le> cos x" using cos_monotone_0_pi'[OF `0 \<le> x` `x \<le> Ifloat ux` `Ifloat ux \<le> pi`] .
  2.1229 +	  finally show ?thesis unfolding Ifloat_min by auto
  2.1230 +	qed
  2.1231 +	moreover have "cos x \<le> Ifloat (Float 1 0)" by auto
  2.1232 +	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
  2.1233 +      qed
  2.1234 +    qed
  2.1235 +  qed
  2.1236 +qed
  2.1237 +
  2.1238 +subsection "Compute the sinus in the entire domain"
  2.1239 +
  2.1240 +function lb_sin :: "nat \<Rightarrow> float \<Rightarrow> float" and ub_sin :: "nat \<Rightarrow> float \<Rightarrow> float" where
  2.1241 +"lb_sin prec x = (let sqr_diff = \<lambda> x. if x > 1 then 0 else 1 - x * x 
  2.1242 +  in if x < 0           then - ub_sin prec (- x)
  2.1243 +else if x \<le> Float 1 -1  then x * lb_sin_cos_aux prec (get_even (prec div 4 + 1)) 2 1 (x * x)
  2.1244 +                        else the (lb_sqrt prec (sqr_diff (ub_cos prec x))))" |
  2.1245 +
  2.1246 +"ub_sin prec x = (let sqr_diff = \<lambda> x. if x < 0 then 1 else 1 - x * x
  2.1247 +  in if x < 0           then - lb_sin prec (- x)
  2.1248 +else if x \<le> Float 1 -1  then x * ub_sin_cos_aux prec (get_odd (prec div 4 + 1)) 2 1 (x * x)
  2.1249 +                        else the (ub_sqrt prec (sqr_diff (lb_cos prec x))))"
  2.1250 +by pat_completeness auto
  2.1251 +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)
  2.1252 +
  2.1253 +definition bnds_sin :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float * float" where
  2.1254 +"bnds_sin prec lx ux = (let 
  2.1255 +    lpi = lb_pi prec ;
  2.1256 +    half_pi = lpi * Float 1 -1
  2.1257 +  in if lx \<le> - half_pi \<or> half_pi \<le> ux then (Float -1 0, Float 1 0)
  2.1258 +                                       else (lb_sin prec lx, ub_sin prec ux))"
  2.1259 +
  2.1260 +lemma lb_sin: assumes "- (pi / 2) \<le> Ifloat x" and "Ifloat x \<le> pi / 2"
  2.1261 +  shows "sin (Ifloat x) \<in> { Ifloat (lb_sin prec x) .. Ifloat (ub_sin prec x) }" (is "?sin x \<in> { ?lb x .. ?ub x}")
  2.1262 +proof -
  2.1263 +  { fix x :: float assume "0 \<le> Ifloat x" and "Ifloat x \<le> pi / 2"
  2.1264 +    hence "\<not> (x < 0)" and "- (pi / 2) \<le> Ifloat x" unfolding less_float_def using pi_ge_two by auto
  2.1265 +
  2.1266 +    have "Ifloat x \<le> pi" using `Ifloat x \<le> pi / 2` using pi_ge_two by auto
  2.1267 +
  2.1268 +    have "?sin x \<in> { ?lb x .. ?ub x}"
  2.1269 +    proof (cases "x \<le> Float 1 -1")
  2.1270 +      case True from sin_boundaries[OF `0 \<le> Ifloat x` `Ifloat x \<le> pi / 2`]
  2.1271 +      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 .
  2.1272 +    next
  2.1273 +      case False
  2.1274 +      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
  2.1275 +      have "0 \<le> sin (Ifloat x)" using `0 \<le> Ifloat x` and `Ifloat x \<le> pi / 2` using sin_ge_zero by auto
  2.1276 +      
  2.1277 +      have "?sin x \<le> ?ub x"
  2.1278 +      proof (cases "lb_cos prec x < 0")
  2.1279 +	case True
  2.1280 +	have "?sin x \<le> 1" using sin_le_one .
  2.1281 +	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
  2.1282 +	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 .
  2.1283 +      next
  2.1284 +	case False hence "0 \<le> Ifloat (lb_cos prec x)" unfolding less_float_def by auto
  2.1285 +	
  2.1286 +	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
  2.1287 +	also have "\<dots> \<le> sqrt (Ifloat (1 - lb_cos prec x * lb_cos prec x))" 
  2.1288 +	proof (rule real_sqrt_le_mono)
  2.1289 +	  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
  2.1290 +	    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)
  2.1291 +	  thus "1 - cos (Ifloat x) ^ 2 \<le> Ifloat (1 - lb_cos prec x * lb_cos prec x)" unfolding Ifloat_sub Ifloat_1 by auto
  2.1292 +	qed
  2.1293 +	also have "\<dots> \<le> Ifloat (the (ub_sqrt prec (1 - lb_cos prec x * lb_cos prec x)))"
  2.1294 +	proof (rule ub_sqrt_lower_bound)
  2.1295 +	  have "Ifloat (lb_cos prec x) \<le> cos (Ifloat x)" using lb_cos[OF `0 \<le> Ifloat x` `Ifloat x \<le> pi`] by auto
  2.1296 +	  from mult_mono[OF order_trans[OF this cos_le_one] order_trans[OF this cos_le_one]]
  2.1297 +	  have "Ifloat (lb_cos prec x) * Ifloat (lb_cos prec x) \<le> 1" using `0 \<le> Ifloat (lb_cos prec x)` by auto
  2.1298 +	  thus "0 \<le> Ifloat (1 - lb_cos prec x * lb_cos prec x)" by auto
  2.1299 +	qed
  2.1300 +	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 .
  2.1301 +      qed
  2.1302 +      moreover
  2.1303 +      have "?lb x \<le> ?sin x"
  2.1304 +      proof (cases "1 < ub_cos prec x")
  2.1305 +	case True
  2.1306 +	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 
  2.1307 +	  by (rule order_trans[OF _ sin_ge_zero[OF `0 \<le> Ifloat x` `Ifloat x \<le> pi`]]) 
  2.1308 +        (auto simp add: lb_sqrt_upper_bound[where prec=prec and x=0, unfolded Ifloat_0 real_sqrt_zero])
  2.1309 +      next
  2.1310 +	case False hence "Ifloat (ub_cos prec x) \<le> 1" unfolding less_float_def by auto
  2.1311 +	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
  2.1312 +	
  2.1313 +	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))"
  2.1314 +	proof (rule lb_sqrt_upper_bound)
  2.1315 +	  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)`
  2.1316 +	  have "Ifloat (ub_cos prec x) * Ifloat (ub_cos prec x) \<le> 1" by auto
  2.1317 +	  thus "0 \<le> Ifloat (1 - ub_cos prec x * ub_cos prec x)" by auto
  2.1318 +	qed
  2.1319 +	also have "\<dots> \<le> sqrt (1 - cos (Ifloat x) ^ 2)"
  2.1320 +	proof (rule real_sqrt_le_mono)
  2.1321 +	  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
  2.1322 +	    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)
  2.1323 +	  thus "Ifloat (1 - ub_cos prec x * ub_cos prec x) \<le> 1 - cos (Ifloat x) ^ 2" unfolding Ifloat_sub Ifloat_1 by auto
  2.1324 +	qed
  2.1325 +	also have "\<dots> = sin (Ifloat x)" unfolding sin_squared_eq[symmetric] real_sqrt_abs using `0 \<le> sin (Ifloat x)` by auto
  2.1326 +	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 .
  2.1327 +      qed
  2.1328 +      ultimately show ?thesis by auto
  2.1329 +    qed
  2.1330 +  } note for_pos = this
  2.1331 +
  2.1332 +  show ?thesis
  2.1333 +  proof (cases "x < 0")
  2.1334 +    case True 
  2.1335 +    hence "0 \<le> Ifloat (-x)" and "Ifloat (- x) \<le> pi / 2" using `-(pi/2) \<le> Ifloat x` unfolding less_float_def by auto
  2.1336 +    from for_pos[OF this]
  2.1337 +    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
  2.1338 +  next
  2.1339 +    case False hence "0 \<le> Ifloat x" unfolding less_float_def by auto
  2.1340 +    from for_pos[OF this `Ifloat x \<le> pi /2`]
  2.1341 +    show ?thesis .
  2.1342 +  qed
  2.1343 +qed
  2.1344 +
  2.1345 +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"
  2.1346 +proof (rule allI, rule allI, rule allI, rule impI)
  2.1347 +  fix x lx ux
  2.1348 +  assume "(l, u) = bnds_sin prec lx ux \<and> x \<in> {Ifloat lx .. Ifloat ux}"
  2.1349 +  hence bnds: "(l, u) = bnds_sin prec lx ux" and x: "x \<in> {Ifloat lx .. Ifloat ux}" by auto
  2.1350 +  show "Ifloat l \<le> sin x \<and> sin x \<le> Ifloat u"
  2.1351 +  proof (cases "lx \<le> - (lb_pi prec * Float 1 -1) \<or> lb_pi prec * Float 1 -1 \<le> ux")
  2.1352 +    case True show ?thesis using bnds unfolding bnds_sin_def if_P[OF True] Let_def by auto
  2.1353 +  next
  2.1354 +    case False
  2.1355 +    hence "- lb_pi prec * Float 1 -1 \<le> lx" and "ux \<le> lb_pi prec * Float 1 -1" unfolding le_float_def by auto
  2.1356 +    moreover have "Ifloat (lb_pi prec * Float 1 -1) \<le> pi / 2" unfolding Ifloat_mult using pi_boundaries by auto
  2.1357 +    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
  2.1358 +    hence "- (pi / 2) \<le> Ifloat ux" and "Ifloat lx \<le> pi / 2" by auto
  2.1359 +    
  2.1360 +    have "- (pi / 2) \<le> x""x \<le> pi / 2" using `Ifloat ux \<le> pi / 2` `- (pi /2) \<le> Ifloat lx` x by auto
  2.1361 +    
  2.1362 +    { 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
  2.1363 +      also have "\<dots> \<le> sin x" using sin_monotone_2pi' `- (pi / 2) \<le> Ifloat lx` x `x \<le> pi / 2` by auto
  2.1364 +      finally have "Ifloat (lb_sin prec lx) \<le> sin x" . }
  2.1365 +    moreover
  2.1366 +    { have "sin x \<le> sin (Ifloat ux)" using sin_monotone_2pi' `- (pi / 2) \<le> x` x `Ifloat ux \<le> pi / 2` by auto
  2.1367 +      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
  2.1368 +      finally have "sin x \<le> Ifloat (ub_sin prec ux)" . }
  2.1369 +    ultimately
  2.1370 +    show ?thesis using bnds unfolding bnds_sin_def if_not_P[OF False] Let_def by auto
  2.1371 +  qed
  2.1372 +qed
  2.1373 +
  2.1374 +section "Exponential function"
  2.1375 +
  2.1376 +subsection "Compute the series of the exponential function"
  2.1377 +
  2.1378 +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
  2.1379 +"ub_exp_horner prec 0 i k x       = 0" |
  2.1380 +"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" |
  2.1381 +"lb_exp_horner prec 0 i k x       = 0" |
  2.1382 +"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"
  2.1383 +
  2.1384 +lemma bnds_exp_horner: assumes "Ifloat x \<le> 0"
  2.1385 +  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) }"
  2.1386 +proof -
  2.1387 +  { fix n
  2.1388 +    have F: "\<And> m. ((\<lambda>i. i + 1) ^ n) m = n + m" by (induct n, auto)
  2.1389 +    have "fact (Suc n) = fact n * ((\<lambda>i. i + 1) ^ n) 1" unfolding F by auto } note f_eq = this
  2.1390 +    
  2.1391 +  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,
  2.1392 +    OF assms f_eq lb_exp_horner.simps ub_exp_horner.simps]
  2.1393 +
  2.1394 +  { 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)"
  2.1395 +      using bounds(1) by auto
  2.1396 +    also have "\<dots> \<le> exp (Ifloat x)"
  2.1397 +    proof -
  2.1398 +      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)"
  2.1399 +	using Maclaurin_exp_le by blast
  2.1400 +      moreover have "0 \<le> exp t / real (fact (get_even n)) * (Ifloat x) ^ (get_even n)"
  2.1401 +	by (auto intro!: mult_nonneg_nonneg divide_nonneg_pos simp add: get_even zero_le_even_power exp_gt_zero)
  2.1402 +      ultimately show ?thesis
  2.1403 +	using get_odd exp_gt_zero by (auto intro!: pordered_cancel_semiring_class.mult_nonneg_nonneg)
  2.1404 +    qed
  2.1405 +    finally have "Ifloat (lb_exp_horner prec (get_even n) 1 1 x) \<le> exp (Ifloat x)" .
  2.1406 +  } moreover
  2.1407 +  { 
  2.1408 +    have x_less_zero: "Ifloat x ^ get_odd n \<le> 0"
  2.1409 +    proof (cases "Ifloat x = 0")
  2.1410 +      case True
  2.1411 +      have "(get_odd n) \<noteq> 0" using get_odd[THEN odd_pos] by auto
  2.1412 +      thus ?thesis unfolding True power_0_left by auto
  2.1413 +    next
  2.1414 +      case False hence "Ifloat x < 0" using `Ifloat x \<le> 0` by auto
  2.1415 +      show ?thesis by (rule less_imp_le, auto simp add: power_less_zero_eq get_odd `Ifloat x < 0`)
  2.1416 +    qed
  2.1417 +
  2.1418 +    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)"
  2.1419 +      using Maclaurin_exp_le by blast
  2.1420 +    moreover have "exp t / real (fact (get_odd n)) * (Ifloat x) ^ (get_odd n) \<le> 0"
  2.1421 +      by (auto intro!: mult_nonneg_nonpos divide_nonpos_pos simp add: x_less_zero exp_gt_zero)
  2.1422 +    ultimately have "exp (Ifloat x) \<le> (\<Sum>j = 0..<get_odd n. 1 / real (fact j) * Ifloat x ^ j)"
  2.1423 +      using get_odd exp_gt_zero by (auto intro!: pordered_cancel_semiring_class.mult_nonneg_nonneg)
  2.1424 +    also have "\<dots> \<le> Ifloat (ub_exp_horner prec (get_odd n) 1 1 x)"
  2.1425 +      using bounds(2) by auto
  2.1426 +    finally have "exp (Ifloat x) \<le> Ifloat (ub_exp_horner prec (get_odd n) 1 1 x)" .
  2.1427 +  } ultimately show ?thesis by auto
  2.1428 +qed
  2.1429 +
  2.1430 +subsection "Compute the exponential function on the entire domain"
  2.1431 +
  2.1432 +function ub_exp :: "nat \<Rightarrow> float \<Rightarrow> float" and lb_exp :: "nat \<Rightarrow> float \<Rightarrow> float" where
  2.1433 +"lb_exp prec x = (if 0 < x then float_divl prec 1 (ub_exp prec (-x))
  2.1434 +             else let 
  2.1435 +                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)
  2.1436 +             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))
  2.1437 +                           else horner x)" |
  2.1438 +"ub_exp prec x = (if 0 < x    then float_divr prec 1 (lb_exp prec (-x))
  2.1439 +             else if x < - 1  then (case floor_fl x of (Float m e) \<Rightarrow> 
  2.1440 +                                    (ub_exp_horner prec (get_odd (prec + 2)) 1 1 (float_divr prec x (- Float m e))) ^ (nat (-m) * 2 ^ nat e))
  2.1441 +                              else ub_exp_horner prec (get_odd (prec + 2)) 1 1 x)"
  2.1442 +by pat_completeness auto
  2.1443 +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)
  2.1444 +
  2.1445 +lemma exp_m1_ge_quarter: "(1 / 4 :: real) \<le> exp (- 1)"
  2.1446 +proof -
  2.1447 +  have eq4: "4 = Suc (Suc (Suc (Suc 0)))" by auto
  2.1448 +
  2.1449 +  have "1 / 4 = Ifloat (Float 1 -2)" unfolding Float_num by auto
  2.1450 +  also have "\<dots> \<le> Ifloat (lb_exp_horner 1 (get_even 4) 1 1 (- 1))"
  2.1451 +    unfolding get_even_def eq4 
  2.1452 +    by (auto simp add: lapprox_posrat_def rapprox_posrat_def normfloat.simps)
  2.1453 +  also have "\<dots> \<le> exp (Ifloat (- 1))" using bnds_exp_horner[where x="- 1"] by auto
  2.1454 +  finally show ?thesis unfolding Ifloat_minus Ifloat_1 . 
  2.1455 +qed
  2.1456 +
  2.1457 +lemma lb_exp_pos: assumes "\<not> 0 < x" shows "0 < lb_exp prec x"
  2.1458 +proof -
  2.1459 +  let "?lb_horner x" = "lb_exp_horner prec (get_even (prec + 2)) 1 1 x"
  2.1460 +  let "?horner x" = "let  y = ?lb_horner x  in if y \<le> 0 then Float 1 -2 else y"
  2.1461 +  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)
  2.1462 +  moreover { fix x :: float fix num :: nat
  2.1463 +    have "0 < Ifloat (?horner x) ^ num" using `0 < ?horner x`[unfolded less_float_def Ifloat_0] by (rule zero_less_power)
  2.1464 +    also have "\<dots> = Ifloat ((?horner x) ^ num)" using float_power by auto
  2.1465 +    finally have "0 < Ifloat ((?horner x) ^ num)" .
  2.1466 +  }
  2.1467 +  ultimately show ?thesis
  2.1468 +    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) 
  2.1469 +qed
  2.1470 +
  2.1471 +lemma exp_boundaries': assumes "x \<le> 0"
  2.1472 +  shows "exp (Ifloat x) \<in> { Ifloat (lb_exp prec x) .. Ifloat (ub_exp prec x)}"
  2.1473 +proof -
  2.1474 +  let "?lb_exp_horner x" = "lb_exp_horner prec (get_even (prec + 2)) 1 1 x"
  2.1475 +  let "?ub_exp_horner x" = "ub_exp_horner prec (get_odd (prec + 2)) 1 1 x"
  2.1476 +
  2.1477 +  have "Ifloat x \<le> 0" and "\<not> x > 0" using `x \<le> 0` unfolding le_float_def less_float_def by auto
  2.1478 +  show ?thesis
  2.1479 +  proof (cases "x < - 1")
  2.1480 +    case False hence "- 1 \<le> Ifloat x" unfolding less_float_def by auto
  2.1481 +    show ?thesis
  2.1482 +    proof (cases "?lb_exp_horner x \<le> 0")
  2.1483 +      from `\<not> x < - 1` have "- 1 \<le> Ifloat x" unfolding less_float_def by auto
  2.1484 +      hence "exp (- 1) \<le> exp (Ifloat x)" unfolding exp_le_cancel_iff .
  2.1485 +      from order_trans[OF exp_m1_ge_quarter this]
  2.1486 +      have "Ifloat (Float 1 -2) \<le> exp (Ifloat x)" unfolding Float_num .
  2.1487 +      moreover case True
  2.1488 +      ultimately show ?thesis using bnds_exp_horner `Ifloat x \<le> 0` `\<not> x > 0` `\<not> x < - 1` by auto
  2.1489 +    next
  2.1490 +      case False thus ?thesis using bnds_exp_horner `Ifloat x \<le> 0` `\<not> x > 0` `\<not> x < - 1` by (auto simp add: Let_def)
  2.1491 +    qed
  2.1492 +  next
  2.1493 +    case True
  2.1494 +    
  2.1495 +    obtain m e where Float_floor: "floor_fl x = Float m e" by (cases "floor_fl x", auto)
  2.1496 +    let ?num = "nat (- m) * 2 ^ nat e"
  2.1497 +    
  2.1498 +    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)
  2.1499 +    hence "Ifloat (floor_fl x) < 0" unfolding Float_floor Ifloat.simps using zero_less_pow2[of xe] by auto
  2.1500 +    hence "m < 0"
  2.1501 +      unfolding less_float_def Ifloat_0 Float_floor Ifloat.simps
  2.1502 +      unfolding pos_prod_lt[OF zero_less_pow2[of e], unfolded real_mult_commute] by auto
  2.1503 +    hence "1 \<le> - m" by auto
  2.1504 +    hence "0 < nat (- m)" by auto
  2.1505 +    moreover
  2.1506 +    have "0 \<le> e" using floor_pos_exp Float_floor[symmetric] by auto
  2.1507 +    hence "(0::nat) < 2 ^ nat e" by auto
  2.1508 +    ultimately have "0 < ?num"  by auto
  2.1509 +    hence "real ?num \<noteq> 0" by auto
  2.1510 +    have e_nat: "int (nat e) = e" using `0 \<le> e` by auto
  2.1511 +    have num_eq: "real ?num = Ifloat (- floor_fl x)" using `0 < nat (- m)`
  2.1512 +      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
  2.1513 +    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 .
  2.1514 +    hence "Ifloat (floor_fl x) < 0" unfolding less_float_def by auto
  2.1515 +    
  2.1516 +    have "exp (Ifloat x) \<le> Ifloat (ub_exp prec x)"
  2.1517 +    proof -
  2.1518 +      have div_less_zero: "Ifloat (float_divr prec x (- floor_fl x)) \<le> 0" 
  2.1519 +	using float_divr_nonpos_pos_upper_bound[OF `x \<le> 0` `0 < - floor_fl x`] unfolding le_float_def Ifloat_0 .
  2.1520 +      
  2.1521 +      have "exp (Ifloat x) = exp (real ?num * (Ifloat x / real ?num))" using `real ?num \<noteq> 0` by auto
  2.1522 +      also have "\<dots> = exp (Ifloat x / real ?num) ^ ?num" unfolding exp_real_of_nat_mult ..
  2.1523 +      also have "\<dots> \<le> exp (Ifloat (float_divr prec x (- floor_fl x))) ^ ?num" unfolding num_eq
  2.1524 +	by (rule power_mono, rule exp_le_cancel_iff[THEN iffD2], rule float_divr) auto
  2.1525 +      also have "\<dots> \<le> Ifloat ((?ub_exp_horner (float_divr prec x (- floor_fl x))) ^ ?num)" unfolding float_power
  2.1526 +	by (rule power_mono, rule bnds_exp_horner[OF div_less_zero, unfolded atLeastAtMost_iff, THEN conjunct2], auto)
  2.1527 +      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 .
  2.1528 +    qed
  2.1529 +    moreover 
  2.1530 +    have "Ifloat (lb_exp prec x) \<le> exp (Ifloat x)"
  2.1531 +    proof -
  2.1532 +      let ?divl = "float_divl prec x (- Float m e)"
  2.1533 +      let ?horner = "?lb_exp_horner ?divl"
  2.1534 +      
  2.1535 +      show ?thesis
  2.1536 +      proof (cases "?horner \<le> 0")
  2.1537 +	case False hence "0 \<le> Ifloat ?horner" unfolding le_float_def by auto
  2.1538 +	
  2.1539 +	have div_less_zero: "Ifloat (float_divl prec x (- floor_fl x)) \<le> 0"
  2.1540 +	  using `Ifloat (floor_fl x) < 0` `Ifloat x \<le> 0` by (auto intro!: order_trans[OF float_divl] divide_nonpos_neg)
  2.1541 +	
  2.1542 +	have "Ifloat ((?lb_exp_horner (float_divl prec x (- floor_fl x))) ^ ?num) \<le>  
  2.1543 +          exp (Ifloat (float_divl prec x (- floor_fl x))) ^ ?num" unfolding float_power 
  2.1544 +	  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)
  2.1545 +	also have "\<dots> \<le> exp (Ifloat x / real ?num) ^ ?num" unfolding num_eq
  2.1546 +	  using float_divl by (auto intro!: power_mono simp del: Ifloat_minus)
  2.1547 +	also have "\<dots> = exp (real ?num * (Ifloat x / real ?num))" unfolding exp_real_of_nat_mult ..
  2.1548 +	also have "\<dots> = exp (Ifloat x)" using `real ?num \<noteq> 0` by auto
  2.1549 +	finally show ?thesis
  2.1550 +	  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
  2.1551 +      next
  2.1552 +	case True
  2.1553 +	have "Ifloat (floor_fl x) \<noteq> 0" and "Ifloat (floor_fl x) \<le> 0" using `Ifloat (floor_fl x) < 0` by auto
  2.1554 +	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`]]
  2.1555 +	have "- 1 \<le> Ifloat x / Ifloat (- floor_fl x)" unfolding Ifloat_minus by auto
  2.1556 +	from order_trans[OF exp_m1_ge_quarter this[unfolded exp_le_cancel_iff[where x="- 1", symmetric]]]
  2.1557 +	have "Ifloat (Float 1 -2) \<le> exp (Ifloat x / Ifloat (- floor_fl x))" unfolding Float_num .
  2.1558 +	hence "Ifloat (Float 1 -2) ^ ?num \<le> exp (Ifloat x / Ifloat (- floor_fl x)) ^ ?num"
  2.1559 +	  by (auto intro!: power_mono simp add: Float_num)
  2.1560 +	also have "\<dots> = exp (Ifloat x)" unfolding num_eq exp_real_of_nat_mult[symmetric] using `Ifloat (floor_fl x) \<noteq> 0` by auto
  2.1561 +	finally show ?thesis
  2.1562 +	  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 .
  2.1563 +      qed
  2.1564 +    qed
  2.1565 +    ultimately show ?thesis by auto
  2.1566 +  qed
  2.1567 +qed
  2.1568 +
  2.1569 +lemma exp_boundaries: "exp (Ifloat x) \<in> { Ifloat (lb_exp prec x) .. Ifloat (ub_exp prec x)}"
  2.1570 +proof -
  2.1571 +  show ?thesis
  2.1572 +  proof (cases "0 < x")
  2.1573 +    case False hence "x \<le> 0" unfolding less_float_def le_float_def by auto 
  2.1574 +    from exp_boundaries'[OF this] show ?thesis .
  2.1575 +  next
  2.1576 +    case True hence "-x \<le> 0" unfolding less_float_def le_float_def by auto
  2.1577 +    
  2.1578 +    have "Ifloat (lb_exp prec x) \<le> exp (Ifloat x)"
  2.1579 +    proof -
  2.1580 +      from exp_boundaries'[OF `-x \<le> 0`]
  2.1581 +      have ub_exp: "exp (- Ifloat x) \<le> Ifloat (ub_exp prec (-x))" unfolding atLeastAtMost_iff Ifloat_minus by auto
  2.1582 +      
  2.1583 +      have "Ifloat (float_divl prec 1 (ub_exp prec (-x))) \<le> Ifloat 1 / Ifloat (ub_exp prec (-x))" using float_divl .
  2.1584 +      also have "Ifloat 1 / Ifloat (ub_exp prec (-x)) \<le> exp (Ifloat x)"
  2.1585 +	using ub_exp[unfolded inverse_le_iff_le[OF order_less_le_trans[OF exp_gt_zero ub_exp] exp_gt_zero, symmetric]]
  2.1586 +	unfolding exp_minus nonzero_inverse_inverse_eq[OF exp_not_eq_zero] inverse_eq_divide by auto
  2.1587 +      finally show ?thesis unfolding lb_exp.simps if_P[OF True] .
  2.1588 +    qed
  2.1589 +    moreover
  2.1590 +    have "exp (Ifloat x) \<le> Ifloat (ub_exp prec x)"
  2.1591 +    proof -
  2.1592 +      have "\<not> 0 < -x" using `0 < x` unfolding less_float_def by auto
  2.1593 +      
  2.1594 +      from exp_boundaries'[OF `-x \<le> 0`]
  2.1595 +      have lb_exp: "Ifloat (lb_exp prec (-x)) \<le> exp (- Ifloat x)" unfolding atLeastAtMost_iff Ifloat_minus by auto
  2.1596 +      
  2.1597 +      have "exp (Ifloat x) \<le> Ifloat 1 / Ifloat (lb_exp prec (-x))"
  2.1598 +	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]]
  2.1599 +	unfolding exp_minus nonzero_inverse_inverse_eq[OF exp_not_eq_zero] inverse_eq_divide Ifloat_1 by auto
  2.1600 +      also have "\<dots> \<le> Ifloat (float_divr prec 1 (lb_exp prec (-x)))" using float_divr .
  2.1601 +      finally show ?thesis unfolding ub_exp.simps if_P[OF True] .
  2.1602 +    qed
  2.1603 +    ultimately show ?thesis by auto
  2.1604 +  qed
  2.1605 +qed
  2.1606 +
  2.1607 +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"
  2.1608 +proof (rule allI, rule allI, rule allI, rule impI)
  2.1609 +  fix x lx ux
  2.1610 +  assume "(l, u) = (lb_exp prec lx, ub_exp prec ux) \<and> x \<in> {Ifloat lx .. Ifloat ux}"
  2.1611 +  hence l: "lb_exp prec lx = l " and u: "ub_exp prec ux = u" and x: "x \<in> {Ifloat lx .. Ifloat ux}" by auto
  2.1612 +
  2.1613 +  { from exp_boundaries[of lx prec, unfolded l]
  2.1614 +    have "Ifloat l \<le> exp (Ifloat lx)" by (auto simp del: lb_exp.simps)
  2.1615 +    also have "\<dots> \<le> exp x" using x by auto
  2.1616 +    finally have "Ifloat l \<le> exp x" .
  2.1617 +  } moreover
  2.1618 +  { have "exp x \<le> exp (Ifloat ux)" using x by auto
  2.1619 +    also have "\<dots> \<le> Ifloat u" using exp_boundaries[of ux prec, unfolded u] by (auto simp del: ub_exp.simps)
  2.1620 +    finally have "exp x \<le> Ifloat u" .
  2.1621 +  } ultimately show "Ifloat l \<le> exp x \<and> exp x \<le> Ifloat u" ..
  2.1622 +qed
  2.1623 +
  2.1624 +section "Logarithm"
  2.1625 +
  2.1626 +subsection "Compute the logarithm series"
  2.1627 +
  2.1628 +fun ub_ln_horner :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float" 
  2.1629 +and lb_ln_horner :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float" where
  2.1630 +"ub_ln_horner prec 0 i x       = 0" |
  2.1631 +"ub_ln_horner prec (Suc n) i x = rapprox_rat prec 1 (int i) - x * lb_ln_horner prec n (Suc i) x" |
  2.1632 +"lb_ln_horner prec 0 i x       = 0" |
  2.1633 +"lb_ln_horner prec (Suc n) i x = lapprox_rat prec 1 (int i) - x * ub_ln_horner prec n (Suc i) x"
  2.1634 +
  2.1635 +lemma ln_bounds:
  2.1636 +  assumes "0 \<le> x" and "x < 1"
  2.1637 +  shows "(\<Sum>i=0..<2*n. -1^i * (1 / real (i + 1)) * x^(Suc i)) \<le> ln (x + 1)" (is "?lb")
  2.1638 +  and "ln (x + 1) \<le> (\<Sum>i=0..<2*n + 1. -1^i * (1 / real (i + 1)) * x^(Suc i))" (is "?ub")
  2.1639 +proof -
  2.1640 +  let "?a n" = "(1/real (n +1)) * x^(Suc n)"
  2.1641 +
  2.1642 +  have ln_eq: "(\<Sum> i. -1^i * ?a i) = ln (x + 1)"
  2.1643 +    using ln_series[of "x + 1"] `0 \<le> x` `x < 1` by auto
  2.1644 +
  2.1645 +  have "norm x < 1" using assms by auto
  2.1646 +  have "?a ----> 0" unfolding Suc_plus1[symmetric] inverse_eq_divide[symmetric] 
  2.1647 +    using LIMSEQ_mult[OF LIMSEQ_inverse_real_of_nat LIMSEQ_Suc[OF LIMSEQ_power_zero[OF `norm x < 1`]]] by auto
  2.1648 +  { fix n have "0 \<le> ?a n" by (rule mult_nonneg_nonneg, auto intro!: mult_nonneg_nonneg simp add: `0 \<le> x`) }
  2.1649 +  { fix n have "?a (Suc n) \<le> ?a n" unfolding inverse_eq_divide[symmetric]
  2.1650 +    proof (rule mult_mono)
  2.1651 +      show "0 \<le> x ^ Suc (Suc n)" by (auto intro!: mult_nonneg_nonneg simp add: `0 \<le> x`)
  2.1652 +      have "x ^ Suc (Suc n) \<le> x ^ Suc n * 1" unfolding power_Suc2 real_mult_assoc[symmetric] 
  2.1653 +	by (rule mult_left_mono, fact less_imp_le[OF `x < 1`], auto intro!: mult_nonneg_nonneg simp add: `0 \<le> x`)
  2.1654 +      thus "x ^ Suc (Suc n) \<le> x ^ Suc n" by auto
  2.1655 +    qed auto }
  2.1656 +  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]
  2.1657 +  show "?lb" and "?ub" by auto
  2.1658 +qed
  2.1659 +
  2.1660 +lemma ln_float_bounds: 
  2.1661 +  assumes "0 \<le> Ifloat x" and "Ifloat x < 1"
  2.1662 +  shows "Ifloat (x * lb_ln_horner prec (get_even n) 1 x) \<le> ln (Ifloat x + 1)" (is "?lb \<le> ?ln")
  2.1663 +  and "ln (Ifloat x + 1) \<le> Ifloat (x * ub_ln_horner prec (get_odd n) 1 x)" (is "?ln \<le> ?ub")
  2.1664 +proof -
  2.1665 +  obtain ev where ev: "get_even n = 2 * ev" using get_even_double ..
  2.1666 +  obtain od where od: "get_odd n = 2 * od + 1" using get_odd_double ..
  2.1667 +
  2.1668 +  let "?s n" = "-1^n * (1 / real (1 + n)) * (Ifloat x)^(Suc n)"
  2.1669 +
  2.1670 +  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
  2.1671 +    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",
  2.1672 +      OF `0 \<le> Ifloat x` refl lb_ln_horner.simps ub_ln_horner.simps] `0 \<le> Ifloat x`
  2.1673 +    by (rule mult_right_mono)
  2.1674 +  also have "\<dots> \<le> ?ln" using ln_bounds(1)[OF `0 \<le> Ifloat x` `Ifloat x < 1`] by auto
  2.1675 +  finally show "?lb \<le> ?ln" . 
  2.1676 +
  2.1677 +  have "?ln \<le> setsum ?s {0 ..< 2 * od + 1}" using ln_bounds(2)[OF `0 \<le> Ifloat x` `Ifloat x < 1`] by auto
  2.1678 +  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
  2.1679 +    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",
  2.1680 +      OF `0 \<le> Ifloat x` refl lb_ln_horner.simps ub_ln_horner.simps] `0 \<le> Ifloat x`
  2.1681 +    by (rule mult_right_mono)
  2.1682 +  finally show "?ln \<le> ?ub" . 
  2.1683 +qed
  2.1684 +
  2.1685 +lemma ln_add: assumes "0 < x" and "0 < y" shows "ln (x + y) = ln x + ln (1 + y / x)"
  2.1686 +proof -
  2.1687 +  have "x \<noteq> 0" using assms by auto
  2.1688 +  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
  2.1689 +  moreover 
  2.1690 +  have "0 < y / x" using assms divide_pos_pos by auto
  2.1691 +  hence "0 < 1 + y / x" by auto
  2.1692 +  ultimately show ?thesis using ln_mult assms by auto
  2.1693 +qed
  2.1694 +
  2.1695 +subsection "Compute the logarithm of 2"
  2.1696 +
  2.1697 +definition ub_ln2 where "ub_ln2 prec = (let third = rapprox_rat (max prec 1) 1 3 
  2.1698 +                                        in (Float 1 -1 * ub_ln_horner prec (get_odd prec) 1 (Float 1 -1)) + 
  2.1699 +                                           (third * ub_ln_horner prec (get_odd prec) 1 third))"
  2.1700 +definition lb_ln2 where "lb_ln2 prec = (let third = lapprox_rat prec 1 3 
  2.1701 +                                        in (Float 1 -1 * lb_ln_horner prec (get_even prec) 1 (Float 1 -1)) + 
  2.1702 +                                           (third * lb_ln_horner prec (get_even prec) 1 third))"
  2.1703 +
  2.1704 +lemma ub_ln2: "ln 2 \<le> Ifloat (ub_ln2 prec)" (is "?ub_ln2")
  2.1705 +  and lb_ln2: "Ifloat (lb_ln2 prec) \<le> ln 2" (is "?lb_ln2")
  2.1706 +proof -
  2.1707 +  let ?uthird = "rapprox_rat (max prec 1) 1 3"
  2.1708 +  let ?lthird = "lapprox_rat prec 1 3"
  2.1709 +
  2.1710 +  have ln2_sum: "ln 2 = ln (1/2 + 1) + ln (1 / 3 + 1)"
  2.1711 +    using ln_add[of "3 / 2" "1 / 2"] by auto
  2.1712 +  have lb3: "Ifloat ?lthird \<le> 1 / 3" using lapprox_rat[of prec 1 3] by auto
  2.1713 +  hence lb3_ub: "Ifloat ?lthird < 1" by auto
  2.1714 +  have lb3_lb: "0 \<le> Ifloat ?lthird" using lapprox_rat_bottom[of 1 3] by auto
  2.1715 +  have ub3: "1 / 3 \<le> Ifloat ?uthird" using rapprox_rat[of 1 3] by auto
  2.1716 +  hence ub3_lb: "0 \<le> Ifloat ?uthird" by auto
  2.1717 +
  2.1718 +  have lb2: "0 \<le> Ifloat (Float 1 -1)" and ub2: "Ifloat (Float 1 -1) < 1" unfolding Float_num by auto
  2.1719 +
  2.1720 +  have "0 \<le> (1::int)" and "0 < (3::int)" by auto
  2.1721 +  have ub3_ub: "Ifloat ?uthird < 1" unfolding rapprox_rat.simps(2)[OF `0 \<le> 1` `0 < 3`]
  2.1722 +    by (rule rapprox_posrat_less1, auto)
  2.1723 +
  2.1724 +  have third_gt0: "(0 :: real) < 1 / 3 + 1" by auto
  2.1725 +  have uthird_gt0: "0 < Ifloat ?uthird + 1" using ub3_lb by auto
  2.1726 +  have lthird_gt0: "0 < Ifloat ?lthird + 1" using lb3_lb by auto
  2.1727 +
  2.1728 +  show ?ub_ln2 unfolding ub_ln2_def Let_def Ifloat_add ln2_sum Float_num(4)[symmetric]
  2.1729 +  proof (rule add_mono, fact ln_float_bounds(2)[OF lb2 ub2])
  2.1730 +    have "ln (1 / 3 + 1) \<le> ln (Ifloat ?uthird + 1)" unfolding ln_le_cancel_iff[OF third_gt0 uthird_gt0] using ub3 by auto
  2.1731 +    also have "\<dots> \<le> Ifloat (?uthird * ub_ln_horner prec (get_odd prec) 1 ?uthird)"
  2.1732 +      using ln_float_bounds(2)[OF ub3_lb ub3_ub] .
  2.1733 +    finally show "ln (1 / 3 + 1) \<le> Ifloat (?uthird * ub_ln_horner prec (get_odd prec) 1 ?uthird)" .
  2.1734 +  qed
  2.1735 +  show ?lb_ln2 unfolding lb_ln2_def Let_def Ifloat_add ln2_sum Float_num(4)[symmetric]
  2.1736 +  proof (rule add_mono, fact ln_float_bounds(1)[OF lb2 ub2])
  2.1737 +    have "Ifloat (?lthird * lb_ln_horner prec (get_even prec) 1 ?lthird) \<le> ln (Ifloat ?lthird + 1)"
  2.1738 +      using ln_float_bounds(1)[OF lb3_lb lb3_ub] .
  2.1739 +    also have "\<dots> \<le> ln (1 / 3 + 1)" unfolding ln_le_cancel_iff[OF lthird_gt0 third_gt0] using lb3 by auto
  2.1740 +    finally show "Ifloat (?lthird * lb_ln_horner prec (get_even prec) 1 ?lthird) \<le> ln (1 / 3 + 1)" .
  2.1741 +  qed
  2.1742 +qed
  2.1743 +
  2.1744 +subsection "Compute the logarithm in the entire domain"
  2.1745 +
  2.1746 +function ub_ln :: "nat \<Rightarrow> float \<Rightarrow> float option" and lb_ln :: "nat \<Rightarrow> float \<Rightarrow> float option" where
  2.1747 +"ub_ln prec x = (if x \<le> 0         then None
  2.1748 +            else if x < 1         then Some (- the (lb_ln prec (float_divl (max prec 1) 1 x)))
  2.1749 +            else let horner = \<lambda>x. (x - 1) * ub_ln_horner prec (get_odd prec) 1 (x - 1) in
  2.1750 +                 if x < Float 1 1 then Some (horner x)
  2.1751 +                                  else let l = bitlen (mantissa x) - 1 in 
  2.1752 +                                       Some (ub_ln2 prec * (Float (scale x + l) 0) + horner (Float (mantissa x) (- l))))" |
  2.1753 +"lb_ln prec x = (if x \<le> 0         then None
  2.1754 +            else if x < 1         then Some (- the (ub_ln prec (float_divr prec 1 x)))
  2.1755 +            else let horner = \<lambda>x. (x - 1) * lb_ln_horner prec (get_even prec) 1 (x - 1) in
  2.1756 +                 if x < Float 1 1 then Some (horner x)
  2.1757 +                                  else let l = bitlen (mantissa x) - 1 in 
  2.1758 +                                       Some (lb_ln2 prec * (Float (scale x + l) 0) + horner (Float (mantissa x) (- l))))"
  2.1759 +by pat_completeness auto
  2.1760 +
  2.1761 +termination proof (relation "measure (\<lambda> v. let (prec, x) = sum_case id id v in (if x < 1 then 1 else 0))", auto)
  2.1762 +  fix prec x assume "\<not> x \<le> 0" and "x < 1" and "float_divl (max prec (Suc 0)) 1 x < 1"
  2.1763 +  hence "0 < x" and "0 < max prec (Suc 0)" unfolding less_float_def le_float_def by auto
  2.1764 +  from float_divl_pos_less1_bound[OF `0 < x` `x < 1` `0 < max prec (Suc 0)`]
  2.1765 +  show False using `float_divl (max prec (Suc 0)) 1 x < 1` unfolding less_float_def le_float_def by auto
  2.1766 +next
  2.1767 +  fix prec x assume "\<not> x \<le> 0" and "x < 1" and "float_divr prec 1 x < 1"
  2.1768 +  hence "0 < x" unfolding less_float_def le_float_def by auto
  2.1769 +  from float_divr_pos_less1_lower_bound[OF `0 < x` `x < 1`, of prec]
  2.1770 +  show False using `float_divr prec 1 x < 1` unfolding less_float_def le_float_def by auto
  2.1771 +qed
  2.1772 +
  2.1773 +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))))"
  2.1774 +proof -
  2.1775 +  let ?B = "2^nat (bitlen m - 1)"
  2.1776 +  have "0 < real m" and "\<And>X. (0 :: real) < 2^X" and "0 < (2 :: real)" and "m \<noteq> 0" using assms by auto
  2.1777 +  hence "0 \<le> bitlen m - 1" using bitlen_ge1[OF `m \<noteq> 0`] by auto
  2.1778 +  show ?thesis 
  2.1779 +  proof (cases "0 \<le> e")
  2.1780 +    case True
  2.1781 +    show ?thesis unfolding normalized_float[OF `m \<noteq> 0`]
  2.1782 +      unfolding ln_div[OF `0 < real m` `0 < ?B`] real_of_int_add ln_realpow[OF `0 < 2`] 
  2.1783 +      unfolding Ifloat_ge0_exp[OF True] ln_mult[OF `0 < real m` `0 < 2^nat e`] 
  2.1784 +      ln_realpow[OF `0 < 2`] algebra_simps using `0 \<le> bitlen m - 1` True by auto
  2.1785 +  next
  2.1786 +    case False hence "0 < -e" by auto
  2.1787 +    hence pow_gt0: "(0::real) < 2^nat (-e)" by auto
  2.1788 +    hence inv_gt0: "(0::real) < inverse (2^nat (-e))" by auto
  2.1789 +    show ?thesis unfolding normalized_float[OF `m \<noteq> 0`]
  2.1790 +      unfolding ln_div[OF `0 < real m` `0 < ?B`] real_of_int_add ln_realpow[OF `0 < 2`] 
  2.1791 +      unfolding Ifloat_nge0_exp[OF False] ln_mult[OF `0 < real m` inv_gt0] ln_inverse[OF pow_gt0]
  2.1792 +      ln_realpow[OF `0 < 2`] algebra_simps using `0 \<le> bitlen m - 1` False by auto
  2.1793 +  qed
  2.1794 +qed
  2.1795 +
  2.1796 +lemma ub_ln_lb_ln_bounds': assumes "1 \<le> x"
  2.1797 +  shows "Ifloat (the (lb_ln prec x)) \<le> ln (Ifloat x) \<and> ln (Ifloat x) \<le> Ifloat (the (ub_ln prec x))"
  2.1798 +  (is "?lb \<le> ?ln \<and> ?ln \<le> ?ub")
  2.1799 +proof (cases "x < Float 1 1")
  2.1800 +  case True hence "Ifloat (x - 1) < 1" unfolding less_float_def Float_num by auto
  2.1801 +  have "\<not> x \<le> 0" and "\<not> x < 1" using `1 \<le> x` unfolding less_float_def le_float_def by auto
  2.1802 +  hence "0 \<le> Ifloat (x - 1)" using `1 \<le> x` unfolding less_float_def Float_num by auto
  2.1803 +  show ?thesis unfolding lb_ln.simps unfolding ub_ln.simps Let_def
  2.1804 +    using ln_float_bounds[OF `0 \<le> Ifloat (x - 1)` `Ifloat (x - 1) < 1`] `\<not> x \<le> 0` `\<not> x < 1` True by auto
  2.1805 +next
  2.1806 +  case False
  2.1807 +  have "\<not> x \<le> 0" and "\<not> x < 1" "0 < x" using `1 \<le> x` unfolding less_float_def le_float_def by auto
  2.1808 +  show ?thesis
  2.1809 +  proof (cases x)
  2.1810 +    case (Float m e)
  2.1811 +    let ?s = "Float (e + (bitlen m - 1)) 0"
  2.1812 +    let ?x = "Float m (- (bitlen m - 1))"
  2.1813 +
  2.1814 +    have "0 < m" and "m \<noteq> 0" using float_pos_m_pos `0 < x` Float by auto
  2.1815 +
  2.1816 +    {
  2.1817 +      have "Ifloat (lb_ln2 prec * ?s) \<le> ln 2 * real (e + (bitlen m - 1))" (is "?lb2 \<le> _")
  2.1818 +	unfolding Ifloat_mult Ifloat_ge0_exp[OF order_refl] nat_0 realpow_0 mult_1_right
  2.1819 +	using lb_ln2[of prec]
  2.1820 +      proof (rule mult_right_mono)
  2.1821 +	have "1 \<le> Float m e" using `1 \<le> x` Float unfolding le_float_def by auto
  2.1822 +	from float_gt1_scale[OF this]
  2.1823 +	show "0 \<le> real (e + (bitlen m - 1))" by auto
  2.1824 +      qed
  2.1825 +      moreover
  2.1826 +      from bitlen_div[OF `0 < m`, unfolded normalized_float[OF `m \<noteq> 0`, symmetric]]
  2.1827 +      have "0 \<le> Ifloat (?x - 1)" and "Ifloat (?x - 1) < 1" by auto
  2.1828 +      from ln_float_bounds(1)[OF this]
  2.1829 +      have "Ifloat ((?x - 1) * lb_ln_horner prec (get_even prec) 1 (?x - 1)) \<le> ln (Ifloat ?x)" (is "?lb_horner \<le> _") by auto
  2.1830 +      ultimately have "?lb2 + ?lb_horner \<le> ln (Ifloat x)"
  2.1831 +	unfolding Float ln_shifted_float[OF `0 < m`, of e] by auto
  2.1832 +    } 
  2.1833 +    moreover
  2.1834 +    {
  2.1835 +      from bitlen_div[OF `0 < m`, unfolded normalized_float[OF `m \<noteq> 0`, symmetric]]
  2.1836 +      have "0 \<le> Ifloat (?x - 1)" and "Ifloat (?x - 1) < 1" by auto
  2.1837 +      from ln_float_bounds(2)[OF this]
  2.1838 +      have "ln (Ifloat ?x) \<le> Ifloat ((?x - 1) * ub_ln_horner prec (get_odd prec) 1 (?x - 1))" (is "_ \<le> ?ub_horner") by auto
  2.1839 +      moreover
  2.1840 +      have "ln 2 * real (e + (bitlen m - 1)) \<le> Ifloat (ub_ln2 prec * ?s)" (is "_ \<le> ?ub2")
  2.1841 +	unfolding Ifloat_mult Ifloat_ge0_exp[OF order_refl] nat_0 realpow_0 mult_1_right
  2.1842 +	using ub_ln2[of prec] 
  2.1843 +      proof (rule mult_right_mono)
  2.1844 +	have "1 \<le> Float m e" using `1 \<le> x` Float unfolding le_float_def by auto
  2.1845 +	from float_gt1_scale[OF this]
  2.1846 +	show "0 \<le> real (e + (bitlen m - 1))" by auto
  2.1847 +      qed
  2.1848 +      ultimately have "ln (Ifloat x) \<le> ?ub2 + ?ub_horner"
  2.1849 +	unfolding Float ln_shifted_float[OF `0 < m`, of e] by auto
  2.1850 +    }
  2.1851 +    ultimately show ?thesis unfolding lb_ln.simps unfolding ub_ln.simps
  2.1852 +      unfolding if_not_P[OF `\<not> x \<le> 0`] if_not_P[OF `\<not> x < 1`] if_not_P[OF False] Let_def
  2.1853 +      unfolding scale.simps[of m e, unfolded Float[symmetric]] mantissa.simps[of m e, unfolded Float[symmetric]] Ifloat_add by auto
  2.1854 +  qed
  2.1855 +qed
  2.1856 +
  2.1857 +lemma ub_ln_lb_ln_bounds: assumes "0 < x"
  2.1858 +  shows "Ifloat (the (lb_ln prec x)) \<le> ln (Ifloat x) \<and> ln (Ifloat x) \<le> Ifloat (the (ub_ln prec x))"
  2.1859 +  (is "?lb \<le> ?ln \<and> ?ln \<le> ?ub")
  2.1860 +proof (cases "x < 1")
  2.1861 +  case False hence "1 \<le> x" unfolding less_float_def le_float_def by auto
  2.1862 +  show ?thesis using ub_ln_lb_ln_bounds'[OF `1 \<le> x`] .
  2.1863 +next
  2.1864 +  case True have "\<not> x \<le> 0" using `0 < x` unfolding less_float_def le_float_def by auto
  2.1865 +
  2.1866 +  have "0 < Ifloat x" and "Ifloat x \<noteq> 0" using `0 < x` unfolding less_float_def by auto
  2.1867 +  hence A: "0 < 1 / Ifloat x" by auto
  2.1868 +
  2.1869 +  {
  2.1870 +    let ?divl = "float_divl (max prec 1) 1 x"
  2.1871 +    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
  2.1872 +    hence B: "0 < Ifloat ?divl" unfolding le_float_def by auto
  2.1873 +    
  2.1874 +    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
  2.1875 +    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
  2.1876 +    from this ub_ln_lb_ln_bounds'[OF A', THEN conjunct1, THEN le_imp_neg_le] 
  2.1877 +    have "?ln \<le> Ifloat (- the (lb_ln prec ?divl))" unfolding Ifloat_minus by (rule order_trans)
  2.1878 +  } moreover
  2.1879 +  {
  2.1880 +    let ?divr = "float_divr prec 1 x"
  2.1881 +    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
  2.1882 +    hence B: "0 < Ifloat ?divr" unfolding le_float_def by auto
  2.1883 +    
  2.1884 +    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
  2.1885 +    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
  2.1886 +    from ub_ln_lb_ln_bounds'[OF A', THEN conjunct2, THEN le_imp_neg_le] this
  2.1887 +    have "Ifloat (- the (ub_ln prec ?divr)) \<le> ?ln" unfolding Ifloat_minus by (rule order_trans)
  2.1888 +  }
  2.1889 +  ultimately show ?thesis unfolding lb_ln.simps[where x=x]  ub_ln.simps[where x=x]
  2.1890 +    unfolding if_not_P[OF `\<not> x \<le> 0`] if_P[OF True] by auto
  2.1891 +qed
  2.1892 +
  2.1893 +lemma lb_ln: assumes "Some y = lb_ln prec x"
  2.1894 +  shows "Ifloat y \<le> ln (Ifloat x)" and "0 < Ifloat x"
  2.1895 +proof -
  2.1896 +  have "0 < x"
  2.1897 +  proof (rule ccontr)
  2.1898 +    assume "\<not> 0 < x" hence "x \<le> 0" unfolding le_float_def less_float_def by auto
  2.1899 +    thus False using assms by auto
  2.1900 +  qed
  2.1901 +  thus "0 < Ifloat x" unfolding less_float_def by auto
  2.1902 +  have "Ifloat (the (lb_ln prec x)) \<le> ln (Ifloat x)" using ub_ln_lb_ln_bounds[OF `0 < x`] ..
  2.1903 +  thus "Ifloat y \<le> ln (Ifloat x)" unfolding assms[symmetric] by auto
  2.1904 +qed
  2.1905 +
  2.1906 +lemma ub_ln: assumes "Some y = ub_ln prec x"
  2.1907 +  shows "ln (Ifloat x) \<le> Ifloat y" and "0 < Ifloat x"
  2.1908 +proof -
  2.1909 +  have "0 < x"
  2.1910 +  proof (rule ccontr)
  2.1911 +    assume "\<not> 0 < x" hence "x \<le> 0" unfolding le_float_def less_float_def by auto
  2.1912 +    thus False using assms by auto
  2.1913 +  qed
  2.1914 +  thus "0 < Ifloat x" unfolding less_float_def by auto
  2.1915 +  have "ln (Ifloat x) \<le> Ifloat (the (ub_ln prec x))" using ub_ln_lb_ln_bounds[OF `0 < x`] ..
  2.1916 +  thus "ln (Ifloat x) \<le> Ifloat y" unfolding assms[symmetric] by auto
  2.1917 +qed
  2.1918 +
  2.1919 +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"
  2.1920 +proof (rule allI, rule allI, rule allI, rule impI)
  2.1921 +  fix x lx ux
  2.1922 +  assume "(Some l, Some u) = (lb_ln prec lx, ub_ln prec ux) \<and> x \<in> {Ifloat lx .. Ifloat ux}"
  2.1923 +  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
  2.1924 +
  2.1925 +  have "ln (Ifloat ux) \<le> Ifloat u" and "0 < Ifloat ux" using ub_ln u by auto
  2.1926 +  have "Ifloat l \<le> ln (Ifloat lx)" and "0 < Ifloat lx" and "0 < x" using lb_ln[OF l] x by auto
  2.1927 +
  2.1928 +  from ln_le_cancel_iff[OF `0 < Ifloat lx` `0 < x`] `Ifloat l \<le> ln (Ifloat lx)` 
  2.1929 +  have "Ifloat l \<le> ln x" using x unfolding atLeastAtMost_iff by auto
  2.1930 +  moreover
  2.1931 +  from ln_le_cancel_iff[OF `0 < x` `0 < Ifloat ux`] `ln (Ifloat ux) \<le> Ifloat u` 
  2.1932 +  have "ln x \<le> Ifloat u" using x unfolding atLeastAtMost_iff by auto
  2.1933 +  ultimately show "Ifloat l \<le> ln x \<and> ln x \<le> Ifloat u" ..
  2.1934 +qed
  2.1935 +
  2.1936 +
  2.1937 +section "Implement floatarith"
  2.1938 +
  2.1939 +subsection "Define syntax and semantics"
  2.1940 +
  2.1941 +datatype floatarith
  2.1942 +  = Add floatarith floatarith
  2.1943 +  | Minus floatarith
  2.1944 +  | Mult floatarith floatarith
  2.1945 +  | Inverse floatarith
  2.1946 +  | Sin floatarith
  2.1947 +  | Cos floatarith
  2.1948 +  | Arctan floatarith
  2.1949 +  | Abs floatarith
  2.1950 +  | Max floatarith floatarith
  2.1951 +  | Min floatarith floatarith
  2.1952 +  | Pi
  2.1953 +  | Sqrt floatarith
  2.1954 +  | Exp floatarith
  2.1955 +  | Ln floatarith
  2.1956 +  | Power floatarith nat
  2.1957 +  | Atom nat
  2.1958 +  | Num float
  2.1959 +
  2.1960 +fun Ifloatarith :: "floatarith \<Rightarrow> real list \<Rightarrow> real"
  2.1961 +where
  2.1962 +"Ifloatarith (Add a b) vs   = (Ifloatarith a vs) + (Ifloatarith b vs)" |
  2.1963 +"Ifloatarith (Minus a) vs    = - (Ifloatarith a vs)" |
  2.1964 +"Ifloatarith (Mult a b) vs   = (Ifloatarith a vs) * (Ifloatarith b vs)" |
  2.1965 +"Ifloatarith (Inverse a) vs  = inverse (Ifloatarith a vs)" |
  2.1966 +"Ifloatarith (Sin a) vs      = sin (Ifloatarith a vs)" |
  2.1967 +"Ifloatarith (Cos a) vs      = cos (Ifloatarith a vs)" |
  2.1968 +"Ifloatarith (Arctan a) vs   = arctan (Ifloatarith a vs)" |
  2.1969 +"Ifloatarith (Min a b) vs    = min (Ifloatarith a vs) (Ifloatarith b vs)" |
  2.1970 +"Ifloatarith (Max a b) vs    = max (Ifloatarith a vs) (Ifloatarith b vs)" |
  2.1971 +"Ifloatarith (Abs a) vs      = abs (Ifloatarith a vs)" |
  2.1972 +"Ifloatarith Pi vs           = pi" |
  2.1973 +"Ifloatarith (Sqrt a) vs     = sqrt (Ifloatarith a vs)" |
  2.1974 +"Ifloatarith (Exp a) vs      = exp (Ifloatarith a vs)" |
  2.1975 +"Ifloatarith (Ln a) vs       = ln (Ifloatarith a vs)" |
  2.1976 +"Ifloatarith (Power a n) vs  = (Ifloatarith a vs)^n" |
  2.1977 +"Ifloatarith (Num f) vs      = Ifloat f" |
  2.1978 +"Ifloatarith (Atom n) vs     = vs ! n"
  2.1979 +
  2.1980 +subsection "Implement approximation function"
  2.1981 +
  2.1982 +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
  2.1983 +"lift_bin (Some (l1, u1)) (Some (l2, u2)) f = (case (f l1 u1 l2 u2) of (Some l, Some u) \<Rightarrow> Some (l, u)
  2.1984 +                                                                     | t \<Rightarrow> None)" |
  2.1985 +"lift_bin a b f = None"
  2.1986 +
  2.1987 +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
  2.1988 +"lift_bin' (Some (l1, u1)) (Some (l2, u2)) f = Some (f l1 u1 l2 u2)" |
  2.1989 +"lift_bin' a b f = None"
  2.1990 +
  2.1991 +fun lift_un :: "(float * float) option \<Rightarrow> (float \<Rightarrow> float \<Rightarrow> ((float option) * (float option))) \<Rightarrow> (float * float) option" where
  2.1992 +"lift_un (Some (l1, u1)) f = (case (f l1 u1) of (Some l, Some u) \<Rightarrow> Some (l, u)
  2.1993 +                                             | t \<Rightarrow> None)" |
  2.1994 +"lift_un b f = None"
  2.1995 +
  2.1996 +fun lift_un' :: "(float * float) option \<Rightarrow> (float \<Rightarrow> float \<Rightarrow> (float * float)) \<Rightarrow> (float * float) option" where
  2.1997 +"lift_un' (Some (l1, u1)) f = Some (f l1 u1)" |
  2.1998 +"lift_un' b f = None"
  2.1999 +
  2.2000 +fun bounded_by :: "real list \<Rightarrow> (float * float) list \<Rightarrow> bool " where
  2.2001 +bounded_by_Cons: "bounded_by (v#vs) ((l, u)#bs) = ((Ifloat l \<le> v \<and> v \<le> Ifloat u) \<and> bounded_by vs bs)" |
  2.2002 +bounded_by_Nil: "bounded_by [] [] = True" |
  2.2003 +"bounded_by _ _ = False"
  2.2004 +
  2.2005 +lemma bounded_by: assumes "bounded_by vs bs" and "i < length bs"
  2.2006 +  shows "Ifloat (fst (bs ! i)) \<le> vs ! i \<and> vs ! i \<le> Ifloat (snd (bs ! i))"
  2.2007 +  using `bounded_by vs bs` and `i < length bs`
  2.2008 +proof (induct arbitrary: i rule: bounded_by.induct)
  2.2009 +  fix v :: real and vs :: "real list" and l u :: float and bs :: "(float * float) list" and i :: nat
  2.2010 +  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))"
  2.2011 +  assume bounded: "bounded_by (v # vs) ((l, u) # bs)" and length: "i < length ((l, u) # bs)"
  2.2012 +  show "Ifloat (fst (((l, u) # bs) ! i)) \<le> (v # vs) ! i \<and> (v # vs) ! i \<le> Ifloat (snd (((l, u) # bs) ! i))"
  2.2013 +  proof (cases i)
  2.2014 +    case 0
  2.2015 +    show ?thesis using bounded unfolding 0 nth_Cons_0 fst_conv snd_conv bounded_by.simps ..
  2.2016 +  next
  2.2017 +    case (Suc i) with length have "i < length bs" by auto
  2.2018 +    show ?thesis unfolding Suc nth_Cons_Suc bounded_by.simps
  2.2019 +      using hyp[OF bounded[unfolded bounded_by.simps, THEN conjunct2] `i < length bs`] .
  2.2020 +  qed
  2.2021 +qed auto
  2.2022 +
  2.2023 +fun approx approx' :: "nat \<Rightarrow> floatarith \<Rightarrow> (float * float) list \<Rightarrow> (float * float) option" where
  2.2024 +"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)" |
  2.2025 +"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))" | 
  2.2026 +"approx prec (Minus a) bs   = lift_un' (approx' prec a bs) (\<lambda> l u. (-u, -l))" |
  2.2027 +"approx prec (Mult a b) bs  = lift_bin' (approx' prec a bs) (approx' prec b bs)
  2.2028 +                                    (\<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, 
  2.2029 +                                                     float_pprt a2 * float_pprt b2 + float_pprt a1 * float_nprt b2 + float_nprt a2 * float_pprt b1 + float_nprt a1 * float_nprt b1))" |
  2.2030 +"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))" |
  2.2031 +"approx prec (Sin a) bs     = lift_un' (approx' prec a bs) (bnds_sin prec)" |
  2.2032 +"approx prec (Cos a) bs     = lift_un' (approx' prec a bs) (bnds_cos prec)" |
  2.2033 +"approx prec Pi bs          = Some (lb_pi prec, ub_pi prec)" |
  2.2034 +"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))" |
  2.2035 +"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))" |
  2.2036 +"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>))" |
  2.2037 +"approx prec (Arctan a) bs  = lift_un' (approx' prec a bs) (\<lambda> l u. (lb_arctan prec l, ub_arctan prec u))" |
  2.2038 +"approx prec (Sqrt a) bs    = lift_un (approx' prec a bs) (\<lambda> l u. (lb_sqrt prec l, ub_sqrt prec u))" |
  2.2039 +"approx prec (Exp a) bs     = lift_un' (approx' prec a bs) (\<lambda> l u. (lb_exp prec l, ub_exp prec u))" |
  2.2040 +"approx prec (Ln a) bs      = lift_un (approx' prec a bs) (\<lambda> l u. (lb_ln prec l, ub_ln prec u))" |
  2.2041 +"approx prec (Power a n) bs = lift_un' (approx' prec a bs) (float_power_bnds n)" |
  2.2042 +"approx prec (Num f) bs     = Some (f, f)" |
  2.2043 +"approx prec (Atom i) bs    = (if i < length bs then Some (bs ! i) else None)"
  2.2044 +
  2.2045 +lemma lift_bin'_ex:
  2.2046 +  assumes lift_bin'_Some: "Some (l, u) = lift_bin' a b f"
  2.2047 +  shows "\<exists> l1 u1 l2 u2. Some (l1, u1) = a \<and> Some (l2, u2) = b"
  2.2048 +proof (cases a)
  2.2049 +  case None hence "None = lift_bin' a b f" unfolding None lift_bin'.simps ..
  2.2050 +  thus ?thesis using lift_bin'_Some by auto
  2.2051 +next
  2.2052 +  case (Some a')
  2.2053 +  show ?thesis
  2.2054 +  proof (cases b)
  2.2055 +    case None hence "None = lift_bin' a b f" unfolding None lift_bin'.simps ..
  2.2056 +    thus ?thesis using lift_bin'_Some by auto
  2.2057 +  next
  2.2058 +    case (Some b')
  2.2059 +    obtain la ua where a': "a' = (la, ua)" by (cases a', auto)
  2.2060 +    obtain lb ub where b': "b' = (lb, ub)" by (cases b', auto)
  2.2061 +    thus ?thesis unfolding `a = Some a'` `b = Some b'` a' b' by auto
  2.2062 +  qed
  2.2063 +qed
  2.2064 +
  2.2065 +lemma lift_bin'_f:
  2.2066 +  assumes lift_bin'_Some: "Some (l, u) = lift_bin' (g a) (g b) f"
  2.2067 +  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"
  2.2068 +  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)"
  2.2069 +proof -
  2.2070 +  obtain l1 u1 l2 u2
  2.2071 +    where Sa: "Some (l1, u1) = g a" and Sb: "Some (l2, u2) = g b" using lift_bin'_ex[OF assms(1)] by auto
  2.2072 +  have lu: "(l, u) = f l1 u1 l2 u2" using lift_bin'_Some[unfolded Sa[symmetric] Sb[symmetric] lift_bin'.simps] by auto 
  2.2073 +  have "l = fst (f l1 u1 l2 u2)" and "u = snd (f l1 u1 l2 u2)" unfolding lu[symmetric] by auto
  2.2074 +  thus ?thesis using Pa[OF Sa] Pb[OF Sb] by auto 
  2.2075 +qed
  2.2076 +
  2.2077 +lemma approx_approx':
  2.2078 +  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"
  2.2079 +  and approx': "Some (l, u) = approx' prec a vs"
  2.2080 +  shows "Ifloat l \<le> Ifloatarith a xs \<and> Ifloatarith a xs \<le> Ifloat u"
  2.2081 +proof -
  2.2082 +  obtain l' u' where S: "Some (l', u') = approx prec a vs"
  2.2083 +    using approx' unfolding approx'.simps by (cases "approx prec a vs", auto)
  2.2084 +  have l': "l = round_down prec l'" and u': "u = round_up prec u'"
  2.2085 +    using approx' unfolding approx'.simps S[symmetric] by auto
  2.2086 +  show ?thesis unfolding l' u' 
  2.2087 +    using order_trans[OF Pa[OF S, THEN conjunct2] round_up[of u']]
  2.2088 +    using order_trans[OF round_down[of _ l'] Pa[OF S, THEN conjunct1]] by auto
  2.2089 +qed
  2.2090 +
  2.2091 +lemma lift_bin':
  2.2092 +  assumes lift_bin'_Some: "Some (l, u) = lift_bin' (approx' prec a bs) (approx' prec b bs) f"
  2.2093 +  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")
  2.2094 +  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"
  2.2095 +  shows "\<exists> l1 u1 l2 u2. (Ifloat l1 \<le> Ifloatarith a xs \<and> Ifloatarith a xs \<le> Ifloat u1) \<and> 
  2.2096 +                        (Ifloat l2 \<le> Ifloatarith b xs \<and> Ifloatarith b xs \<le> Ifloat u2) \<and> 
  2.2097 +                        l = fst (f l1 u1 l2 u2) \<and> u = snd (f l1 u1 l2 u2)"
  2.2098 +proof -
  2.2099 +  { fix l u assume "Some (l, u) = approx' prec a bs"
  2.2100 +    with approx_approx'[of prec a bs, OF _ this] Pa
  2.2101 +    have "Ifloat l \<le> Ifloatarith a xs \<and> Ifloatarith a xs \<le> Ifloat u" by auto } note Pa = this
  2.2102 +  { fix l u assume "Some (l, u) = approx' prec b bs"
  2.2103 +    with approx_approx'[of prec b bs, OF _ this] Pb
  2.2104 +    have "Ifloat l \<le> Ifloatarith b xs \<and> Ifloatarith b xs \<le> Ifloat u" by auto } note Pb = this
  2.2105 +
  2.2106 +  from lift_bin'_f[where g="\<lambda>a. approx' prec a bs" and P = ?P, OF lift_bin'_Some, OF Pa Pb]
  2.2107 +  show ?thesis by auto
  2.2108 +qed
  2.2109 +
  2.2110 +lemma lift_un'_ex:
  2.2111 +  assumes lift_un'_Some: "Some (l, u) = lift_un' a f"
  2.2112 +  shows "\<exists> l u. Some (l, u) = a"
  2.2113 +proof (cases a)
  2.2114 +  case None hence "None = lift_un' a f" unfolding None lift_un'.simps ..
  2.2115 +  thus ?thesis using lift_un'_Some by auto
  2.2116 +next
  2.2117 +  case (Some a')
  2.2118 +  obtain la ua where a': "a' = (la, ua)" by (cases a', auto)
  2.2119 +  thus ?thesis unfolding `a = Some a'` a' by auto
  2.2120 +qed
  2.2121 +
  2.2122 +lemma lift_un'_f:
  2.2123 +  assumes lift_un'_Some: "Some (l, u) = lift_un' (g a) f"
  2.2124 +  and Pa: "\<And>l u. Some (l, u) = g a \<Longrightarrow> P l u a"
  2.2125 +  shows "\<exists> l1 u1. P l1 u1 a \<and> l = fst (f l1 u1) \<and> u = snd (f l1 u1)"
  2.2126 +proof -
  2.2127 +  obtain l1 u1 where Sa: "Some (l1, u1) = g a" using lift_un'_ex[OF assms(1)] by auto
  2.2128 +  have lu: "(l, u) = f l1 u1" using lift_un'_Some[unfolded Sa[symmetric] lift_un'.simps] by auto
  2.2129 +  have "l = fst (f l1 u1)" and "u = snd (f l1 u1)" unfolding lu[symmetric] by auto
  2.2130 +  thus ?thesis using Pa[OF Sa] by auto
  2.2131 +qed
  2.2132 +
  2.2133 +lemma lift_un':
  2.2134 +  assumes lift_un'_Some: "Some (l, u) = lift_un' (approx' prec a bs) f"
  2.2135 +  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")
  2.2136 +  shows "\<exists> l1 u1. (Ifloat l1 \<le> Ifloatarith a xs \<and> Ifloatarith a xs \<le> Ifloat u1) \<and> 
  2.2137 +                        l = fst (f l1 u1) \<and> u = snd (f l1 u1)"
  2.2138 +proof -
  2.2139 +  { fix l u assume "Some (l, u) = approx' prec a bs"
  2.2140 +    with approx_approx'[of prec a bs, OF _ this] Pa
  2.2141 +    have "Ifloat l \<le> Ifloatarith a xs \<and> Ifloatarith a xs \<le> Ifloat u" by auto } note Pa = this
  2.2142 +  from lift_un'_f[where g="\<lambda>a. approx' prec a bs" and P = ?P, OF lift_un'_Some, OF Pa]
  2.2143 +  show ?thesis by auto
  2.2144 +qed
  2.2145 +
  2.2146 +lemma lift_un'_bnds:
  2.2147 +  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"
  2.2148 +  and lift_un'_Some: "Some (l, u) = lift_un' (approx' prec a bs) f"
  2.2149 +  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"
  2.2150 +  shows "Ifloat l \<le> f' (Ifloatarith a xs) \<and> f' (Ifloatarith a xs) \<le> Ifloat u"
  2.2151 +proof -
  2.2152 +  from lift_un'[OF lift_un'_Some Pa]
  2.2153 +  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
  2.2154 +  hence "(l, u) = f l1 u1" and "Ifloatarith a xs \<in> {Ifloat l1 .. Ifloat u1}" by auto
  2.2155 +  thus ?thesis using bnds by auto
  2.2156 +qed
  2.2157 +
  2.2158 +lemma lift_un_ex:
  2.2159 +  assumes lift_un_Some: "Some (l, u) = lift_un a f"
  2.2160 +  shows "\<exists> l u. Some (l, u) = a"
  2.2161 +proof (cases a)
  2.2162 +  case None hence "None = lift_un a f" unfolding None lift_un.simps ..
  2.2163 +  thus ?thesis using lift_un_Some by auto
  2.2164 +next
  2.2165 +  case (Some a')
  2.2166 +  obtain la ua where a': "a' = (la, ua)" by (cases a', auto)
  2.2167 +  thus ?thesis unfolding `a = Some a'` a' by auto
  2.2168 +qed
  2.2169 +
  2.2170 +lemma lift_un_f:
  2.2171 +  assumes lift_un_Some: "Some (l, u) = lift_un (g a) f"
  2.2172 +  and Pa: "\<And>l u. Some (l, u) = g a \<Longrightarrow> P l u a"
  2.2173 +  shows "\<exists> l1 u1. P l1 u1 a \<and> Some l = fst (f l1 u1) \<and> Some u = snd (f l1 u1)"
  2.2174 +proof -
  2.2175 +  obtain l1 u1 where Sa: "Some (l1, u1) = g a" using lift_un_ex[OF assms(1)] by auto
  2.2176 +  have "fst (f l1 u1) \<noteq> None \<and> snd (f l1 u1) \<noteq> None"
  2.2177 +  proof (rule ccontr)
  2.2178 +    assume "\<not> (fst (f l1 u1) \<noteq> None \<and> snd (f l1 u1) \<noteq> None)"
  2.2179 +    hence or: "fst (f l1 u1) = None \<or> snd (f l1 u1) = None" by auto
  2.2180 +    hence "lift_un (g a) f = None" 
  2.2181 +    proof (cases "fst (f l1 u1) = None")
  2.2182 +      case True
  2.2183 +      then obtain b where b: "f l1 u1 = (None, b)" by (cases "f l1 u1", auto)
  2.2184 +      thus ?thesis unfolding Sa[symmetric] lift_un.simps b by auto
  2.2185 +    next
  2.2186 +      case False hence "snd (f l1 u1) = None" using or by auto
  2.2187 +      with False obtain b where b: "f l1 u1 = (Some b, None)" by (cases "f l1 u1", auto)
  2.2188 +      thus ?thesis unfolding Sa[symmetric] lift_un.simps b by auto
  2.2189 +    qed
  2.2190 +    thus False using lift_un_Some by auto
  2.2191 +  qed
  2.2192 +  then obtain a' b' where f: "f l1 u1 = (Some a', Some b')" by (cases "f l1 u1", auto)
  2.2193 +  from lift_un_Some[unfolded Sa[symmetric] lift_un.simps f]
  2.2194 +  have "Some l = fst (f l1 u1)" and "Some u = snd (f l1 u1)" unfolding f by auto
  2.2195 +  thus ?thesis unfolding Sa[symmetric] lift_un.simps using Pa[OF Sa] by auto
  2.2196 +qed
  2.2197 +
  2.2198 +lemma lift_un:
  2.2199 +  assumes lift_un_Some: "Some (l, u) = lift_un (approx' prec a bs) f"
  2.2200 +  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")
  2.2201 +  shows "\<exists> l1 u1. (Ifloat l1 \<le> Ifloatarith a xs \<and> Ifloatarith a xs \<le> Ifloat u1) \<and> 
  2.2202 +                  Some l = fst (f l1 u1) \<and> Some u = snd (f l1 u1)"
  2.2203 +proof -
  2.2204 +  { fix l u assume "Some (l, u) = approx' prec a bs"
  2.2205 +    with approx_approx'[of prec a bs, OF _ this] Pa
  2.2206 +    have "Ifloat l \<le> Ifloatarith a xs \<and> Ifloatarith a xs \<le> Ifloat u" by auto } note Pa = this
  2.2207 +  from lift_un_f[where g="\<lambda>a. approx' prec a bs" and P = ?P, OF lift_un_Some, OF Pa]
  2.2208 +  show ?thesis by auto
  2.2209 +qed
  2.2210 +
  2.2211 +lemma lift_un_bnds:
  2.2212 +  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"
  2.2213 +  and lift_un_Some: "Some (l, u) = lift_un (approx' prec a bs) f"
  2.2214 +  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"
  2.2215 +  shows "Ifloat l \<le> f' (Ifloatarith a xs) \<and> f' (Ifloatarith a xs) \<le> Ifloat u"
  2.2216 +proof -
  2.2217 +  from lift_un[OF lift_un_Some Pa]
  2.2218 +  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
  2.2219 +  hence "(Some l, Some u) = f l1 u1" and "Ifloatarith a xs \<in> {Ifloat l1 .. Ifloat u1}" by auto
  2.2220 +  thus ?thesis using bnds by auto
  2.2221 +qed
  2.2222 +
  2.2223 +lemma approx:
  2.2224 +  assumes "bounded_by xs vs"
  2.2225 +  and "Some (l, u) = approx prec arith vs" (is "_ = ?g arith")
  2.2226 +  shows "Ifloat l \<le> Ifloatarith arith xs \<and> Ifloatarith arith xs \<le> Ifloat u" (is "?P l u arith")
  2.2227 +  using `Some (l, u) = approx prec arith vs` 
  2.2228 +proof (induct arith arbitrary: l u x)
  2.2229 +  case (Add a b)
  2.2230 +  from lift_bin'[OF Add.prems[unfolded approx.simps]] Add.hyps
  2.2231 +  obtain l1 u1 l2 u2 where "l = l1 + l2" and "u = u1 + u2"
  2.2232 +    "Ifloat l1 \<le> Ifloatarith a xs" and "Ifloatarith a xs \<le> Ifloat u1"
  2.2233 +    "Ifloat l2 \<le> Ifloatarith b xs" and "Ifloatarith b xs \<le> Ifloat u2" unfolding fst_conv snd_conv by blast
  2.2234 +  thus ?case unfolding Ifloatarith.simps by auto
  2.2235 +next
  2.2236 +  case (Minus a)
  2.2237 +  from lift_un'[OF Minus.prems[unfolded approx.simps]] Minus.hyps
  2.2238 +  obtain l1 u1 where "l = -u1" and "u = -l1"
  2.2239 +    "Ifloat l1 \<le> Ifloatarith a xs" and "Ifloatarith a xs \<le> Ifloat u1" unfolding fst_conv snd_conv by blast
  2.2240 +  thus ?case unfolding Ifloatarith.simps using Ifloat_minus by auto
  2.2241 +next
  2.2242 +  case (Mult a b)
  2.2243 +  from lift_bin'[OF Mult.prems[unfolded approx.simps]] Mult.hyps
  2.2244 +  obtain l1 u1 l2 u2 
  2.2245 +    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"
  2.2246 +    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"
  2.2247 +    and "Ifloat l1 \<le> Ifloatarith a xs" and "Ifloatarith a xs \<le> Ifloat u1"
  2.2248 +    and "Ifloat l2 \<le> Ifloatarith b xs" and "Ifloatarith b xs \<le> Ifloat u2" unfolding fst_conv snd_conv by blast
  2.2249 +  thus ?case unfolding Ifloatarith.simps l u Ifloat_add Ifloat_mult Ifloat_nprt Ifloat_pprt 
  2.2250 +    using mult_le_prts mult_ge_prts by auto
  2.2251 +next
  2.2252 +  case (Inverse a)
  2.2253 +  from lift_un[OF Inverse.prems[unfolded approx.simps], unfolded if_distrib[of fst] if_distrib[of snd] fst_conv snd_conv] Inverse.hyps
  2.2254 +  obtain l1 u1 where l': "Some l = (if 0 < l1 \<or> u1 < 0 then Some (float_divl prec 1 u1) else None)" 
  2.2255 +    and u': "Some u = (if 0 < l1 \<or> u1 < 0 then Some (float_divr prec 1 l1) else None)"
  2.2256 +    and l1: "Ifloat l1 \<le> Ifloatarith a xs" and u1: "Ifloatarith a xs \<le> Ifloat u1" by blast
  2.2257 +  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
  2.2258 +  moreover have l1_le_u1: "Ifloat l1 \<le> Ifloat u1" using l1 u1 by auto
  2.2259 +  ultimately have "Ifloat l1 \<noteq> 0" and "Ifloat u1 \<noteq> 0" unfolding less_float_def by auto
  2.2260 +
  2.2261 +  have inv: "inverse (Ifloat u1) \<le> inverse (Ifloatarith a xs)
  2.2262 +           \<and> inverse (Ifloatarith a xs) \<le> inverse (Ifloat l1)"
  2.2263 +  proof (cases "0 < l1")
  2.2264 +    case True hence "0 < Ifloat u1" and "0 < Ifloat l1" "0 < Ifloatarith a xs" 
  2.2265 +      unfolding less_float_def using l1_le_u1 l1 by auto
  2.2266 +    show ?thesis
  2.2267 +      unfolding inverse_le_iff_le[OF `0 < Ifloat u1` `0 < Ifloatarith a xs`]
  2.2268 +	inverse_le_iff_le[OF `0 < Ifloatarith a xs` `0 < Ifloat l1`]
  2.2269 +      using l1 u1 by auto
  2.2270 +  next
  2.2271 +    case False hence "u1 < 0" using either by blast
  2.2272 +    hence "Ifloat u1 < 0" and "Ifloat l1 < 0" "Ifloatarith a xs < 0" 
  2.2273 +      unfolding less_float_def using l1_le_u1 u1 by auto
  2.2274 +    show ?thesis
  2.2275 +      unfolding inverse_le_iff_le_neg[OF `Ifloat u1 < 0` `Ifloatarith a xs < 0`]
  2.2276 +	inverse_le_iff_le_neg[OF `Ifloatarith a xs < 0` `Ifloat l1 < 0`]
  2.2277 +      using l1 u1 by auto
  2.2278 +  qed
  2.2279 +    
  2.2280 +  from l' have "l = float_divl prec 1 u1" by (cases "0 < l1 \<or> u1 < 0", auto)
  2.2281 +  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
  2.2282 +  also have "\<dots> \<le> inverse (Ifloatarith a xs)" using inv by auto
  2.2283 +  finally have "Ifloat l \<le> inverse (Ifloatarith a xs)" .
  2.2284 +  moreover
  2.2285 +  from u' have "u = float_divr prec 1 l1" by (cases "0 < l1 \<or> u1 < 0", auto)
  2.2286 +  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
  2.2287 +  hence "inverse (Ifloatarith a xs) \<le> Ifloat u" by (rule order_trans[OF inv[THEN conjunct2]])
  2.2288 +  ultimately show ?case unfolding Ifloatarith.simps using l1 u1 by auto
  2.2289 +next
  2.2290 +  case (Abs x)
  2.2291 +  from lift_un'[OF Abs.prems[unfolded approx.simps], unfolded fst_conv snd_conv] Abs.hyps
  2.2292 +  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>"
  2.2293 +    and l1: "Ifloat l1 \<le> Ifloatarith x xs" and u1: "Ifloatarith x xs \<le> Ifloat u1" by blast
  2.2294 +  thus ?case unfolding l' u' by (cases "l1 < 0 \<and> 0 < u1", auto simp add: Ifloat_min Ifloat_max Ifloat_abs less_float_def)
  2.2295 +next
  2.2296 +  case (Min a b)
  2.2297 +  from lift_bin'[OF Min.prems[unfolded approx.simps], unfolded fst_conv snd_conv] Min.hyps
  2.2298 +  obtain l1 u1 l2 u2 where l': "l = min l1 l2" and u': "u = min u1 u2"
  2.2299 +    and l1: "Ifloat l1 \<le> Ifloatarith a xs" and u1: "Ifloatarith a xs \<le> Ifloat u1"
  2.2300 +    and l1: "Ifloat l2 \<le> Ifloatarith b xs" and u1: "Ifloatarith b xs \<le> Ifloat u2" by blast
  2.2301 +  thus ?case unfolding l' u' by (auto simp add: Ifloat_min)
  2.2302 +next
  2.2303 +  case (Max a b)
  2.2304 +  from lift_bin'[OF Max.prems[unfolded approx.simps], unfolded fst_conv snd_conv] Max.hyps
  2.2305 +  obtain l1 u1 l2 u2 where l': "l = max l1 l2" and u': "u = max u1 u2"
  2.2306 +    and l1: "Ifloat l1 \<le> Ifloatarith a xs" and u1: "Ifloatarith a xs \<le> Ifloat u1"
  2.2307 +    and l1: "Ifloat l2 \<le> Ifloatarith b xs" and u1: "Ifloatarith b xs \<le> Ifloat u2" by blast
  2.2308 +  thus ?case unfolding l' u' by (auto simp add: Ifloat_max)
  2.2309 +next case (Sin a) with lift_un'_bnds[OF bnds_sin] show ?case by auto
  2.2310 +next case (Cos a) with lift_un'_bnds[OF bnds_cos] show ?case by auto
  2.2311 +next case (Arctan a) with lift_un'_bnds[OF bnds_arctan] show ?case by auto
  2.2312 +next case Pi with pi_boundaries show ?case by auto
  2.2313 +next case (Sqrt a) with lift_un_bnds[OF bnds_sqrt] show ?case by auto
  2.2314 +next case (Exp a) with lift_un'_bnds[OF bnds_exp] show ?case by auto
  2.2315 +next case (Ln a) with lift_un_bnds[OF bnds_ln] show ?case by auto
  2.2316 +next case (Power a n) with lift_un'_bnds[OF bnds_power] show ?case by auto
  2.2317 +next case (Num f) thus ?case by auto
  2.2318 +next
  2.2319 +  case (Atom n) 
  2.2320 +  show ?case
  2.2321 +  proof (cases "n < length vs")
  2.2322 +    case True
  2.2323 +    with Atom have "vs ! n = (l, u)" by auto
  2.2324 +    thus ?thesis using bounded_by[OF assms(1) True] by auto
  2.2325 +  next
  2.2326 +    case False thus ?thesis using Atom by auto
  2.2327 +  qed
  2.2328 +qed
  2.2329 +
  2.2330 +datatype ApproxEq = Less floatarith floatarith 
  2.2331 +                  | LessEqual floatarith floatarith 
  2.2332 +
  2.2333 +fun uneq :: "ApproxEq \<Rightarrow> real list \<Rightarrow> bool" where 
  2.2334 +"uneq (Less a b) vs                   = (Ifloatarith a vs < Ifloatarith b vs)" |
  2.2335 +"uneq (LessEqual a b) vs              = (Ifloatarith a vs \<le> Ifloatarith b vs)"
  2.2336 +
  2.2337 +fun uneq' :: "nat \<Rightarrow> ApproxEq \<Rightarrow> (float * float) list \<Rightarrow> bool" where 
  2.2338 +"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)" |
  2.2339 +"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)"
  2.2340 +
  2.2341 +lemma uneq_approx: fixes m :: nat assumes "bounded_by vs bs" and "uneq' prec eq bs"
  2.2342 +  shows "uneq eq vs"
  2.2343 +proof (cases eq)
  2.2344 +  case (Less a b)
  2.2345 +  show ?thesis
  2.2346 +  proof (cases "\<exists> u l u' l'. approx prec a bs = Some (l, u) \<and> 
  2.2347 +                             approx prec b bs = Some (l', u')")
  2.2348 +    case True
  2.2349 +    then obtain l u l' u' where a_approx: "approx prec a bs = Some (l, u)"
  2.2350 +      and b_approx: "approx prec b bs = Some (l', u') " by auto
  2.2351 +    with `uneq' prec eq bs` have "Ifloat u < Ifloat l'"
  2.2352 +      unfolding Less uneq'.simps less_float_def by auto
  2.2353 +    moreover from a_approx[symmetric] and b_approx[symmetric] and `bounded_by vs bs`
  2.2354 +    have "Ifloatarith a vs \<le> Ifloat u" and "Ifloat l' \<le> Ifloatarith b vs"
  2.2355 +      using approx by auto
  2.2356 +    ultimately show ?thesis unfolding uneq.simps Less by auto
  2.2357 +  next
  2.2358 +    case False
  2.2359 +    hence "approx prec a bs = None \<or> approx prec b bs = None"
  2.2360 +      unfolding not_Some_eq[symmetric] by auto
  2.2361 +    hence "\<not> uneq' prec eq bs" unfolding Less uneq'.simps 
  2.2362 +      by (cases "approx prec a bs = None", auto)
  2.2363 +    thus ?thesis using assms by auto
  2.2364 +  qed
  2.2365 +next
  2.2366 +  case (LessEqual a b)
  2.2367 +  show ?thesis
  2.2368 +  proof (cases "\<exists> u l u' l'. approx prec a bs = Some (l, u) \<and> 
  2.2369 +                             approx prec b bs = Some (l', u')")
  2.2370 +    case True
  2.2371 +    then obtain l u l' u' where a_approx: "approx prec a bs = Some (l, u)"
  2.2372 +      and b_approx: "approx prec b bs = Some (l', u') " by auto
  2.2373 +    with `uneq' prec eq bs` have "Ifloat u \<le> Ifloat l'"
  2.2374 +      unfolding LessEqual uneq'.simps le_float_def by auto
  2.2375 +    moreover from a_approx[symmetric] and b_approx[symmetric] and `bounded_by vs bs`
  2.2376 +    have "Ifloatarith a vs \<le> Ifloat u" and "Ifloat l' \<le> Ifloatarith b vs"
  2.2377 +      using approx by auto
  2.2378 +    ultimately show ?thesis unfolding uneq.simps LessEqual by auto
  2.2379 +  next
  2.2380 +    case False
  2.2381 +    hence "approx prec a bs = None \<or> approx prec b bs = None"
  2.2382 +      unfolding not_Some_eq[symmetric] by auto
  2.2383 +    hence "\<not> uneq' prec eq bs" unfolding LessEqual uneq'.simps 
  2.2384 +      by (cases "approx prec a bs = None", auto)
  2.2385 +    thus ?thesis using assms by auto
  2.2386 +  qed
  2.2387 +qed
  2.2388 +
  2.2389 +lemma Ifloatarith_divide: "Ifloatarith (Mult a (Inverse b)) vs = (Ifloatarith a vs) / (Ifloatarith b vs)"
  2.2390 +  unfolding real_divide_def Ifloatarith.simps ..
  2.2391 +
  2.2392 +lemma Ifloatarith_diff: "Ifloatarith (Add a (Minus b)) vs = (Ifloatarith a vs) - (Ifloatarith b vs)"
  2.2393 +  unfolding real_diff_def Ifloatarith.simps ..
  2.2394 +
  2.2395 +lemma Ifloatarith_tan: "Ifloatarith (Mult (Sin a) (Inverse (Cos a))) vs = tan (Ifloatarith a vs)"
  2.2396 +  unfolding tan_def Ifloatarith.simps real_divide_def ..
  2.2397 +
  2.2398 +lemma Ifloatarith_powr: "Ifloatarith (Exp (Mult b (Ln a))) vs = (Ifloatarith a vs) powr (Ifloatarith b vs)"
  2.2399 +  unfolding powr_def Ifloatarith.simps ..
  2.2400 +
  2.2401 +lemma Ifloatarith_log: "Ifloatarith ((Mult (Ln x) (Inverse (Ln b)))) vs = log (Ifloatarith b vs) (Ifloatarith x vs)"
  2.2402 +  unfolding log_def Ifloatarith.simps real_divide_def ..
  2.2403 +
  2.2404 +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
  2.2405 +
  2.2406 +subsection {* Implement proof method \texttt{approximation} *}
  2.2407 +
  2.2408 +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)
  2.2409 +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)
  2.2410 +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)"
  2.2411 +                     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)"
  2.2412 +  by (auto simp add: Ifloat.simps pow2_def)
  2.2413 +
  2.2414 +lemmas bounded_by_equations = bounded_by_Cons bounded_by_Nil float_power bounded_divl bounded_divr bounded_num HOL.simp_thms
  2.2415 +lemmas uneq_equations = uneq.simps Ifloatarith.simps Ifloatarith_num Ifloatarith_divide Ifloatarith_diff Ifloatarith_tan Ifloatarith_powr Ifloatarith_log
  2.2416 +
  2.2417 +lemma "x div (0::int) = 0" by auto -- "What happens in the zero case for div"
  2.2418 +lemma "x mod (0::int) = x" by auto -- "What happens in the zero case for mod"
  2.2419 +
  2.2420 +text {* The following equations must hold for div & mod 
  2.2421 +        -- see "The Definition of Standard ML" by R. Milner, M. Tofte and R. Harper (pg. 79) *}
  2.2422 +lemma "d * (i div d) + i mod d = (i::int)" by auto
  2.2423 +lemma "0 < (d :: int) \<Longrightarrow> 0 \<le> i mod d \<and> i mod d < d" by auto
  2.2424 +lemma "(d :: int) < 0 \<Longrightarrow> d < i mod d \<and> i mod d \<le> 0" by auto
  2.2425 +
  2.2426 +code_const "op div :: int \<Rightarrow> int \<Rightarrow> int" (SML "(fn i => fn d => if d = 0 then 0 else i div d)")
  2.2427 +code_const "op mod :: int \<Rightarrow> int \<Rightarrow> int" (SML "(fn i => fn d => if d = 0 then i else i mod d)")
  2.2428 +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))")
  2.2429 +
  2.2430 +ML {*
  2.2431 +  val uneq_equations = PureThy.get_thms @{theory} "uneq_equations";
  2.2432 +  val bounded_by_equations = PureThy.get_thms @{theory} "bounded_by_equations";
  2.2433 +  val bounded_by_simpset = (HOL_basic_ss addsimps bounded_by_equations)
  2.2434 +
  2.2435 +  fun reify_uneq ctxt i = (fn st =>
  2.2436 +    let
  2.2437 +      val to = HOLogic.dest_Trueprop (Logic.strip_imp_concl (List.nth (prems_of st, i - 1)))
  2.2438 +    in (Reflection.genreify_tac ctxt uneq_equations (SOME to) i) st
  2.2439 +    end)
  2.2440 +
  2.2441 +  fun rule_uneq ctxt prec i thm = let
  2.2442 +    fun conv_num typ = HOLogic.dest_number #> snd #> HOLogic.mk_number typ
  2.2443 +    val to_natc = conv_num @{typ "nat"} #> Thm.cterm_of (ProofContext.theory_of ctxt)
  2.2444 +    val to_nat = conv_num @{typ "nat"}
  2.2445 +    val to_int = conv_num @{typ "int"}
  2.2446 +
  2.2447 +    val prec' = to_nat prec
  2.2448 +
  2.2449 +    fun bot_float (Const (@{const_name "times"}, _) $ mantisse $ (Const (@{const_name "pow2"}, _) $ exp))
  2.2450 +                   = @{term "Float"} $ to_int mantisse $ to_int exp
  2.2451 +      | bot_float (Const (@{const_name "divide"}, _) $ mantisse $ (Const (@{const_name "power"}, _) $ ten $ exp))
  2.2452 +                   = @{term "float_divl"} $ prec' $ (@{term "Float"} $ to_int mantisse $ @{term "0 :: int"}) $ (@{term "power_float (Float 5 1)"} $ to_nat exp)
  2.2453 +      | bot_float (Const (@{const_name "divide"}, _) $ mantisse $ ten)
  2.2454 +                   = @{term "float_divl"} $ prec' $ (@{term "Float"} $ to_int mantisse $ @{term "0 :: int"}) $ @{term "Float 5 1"}
  2.2455 +      | bot_float mantisse = @{term "Float"} $ to_int mantisse $ @{term "0 :: int"}
  2.2456 +
  2.2457 +    fun top_float (Const (@{const_name "times"}, _) $ mantisse $ (Const (@{const_name "pow2"}, _) $ exp))
  2.2458 +                   = @{term "Float"} $ to_int mantisse $ to_int exp
  2.2459 +      | top_float (Const (@{const_name "divide"}, _) $ mantisse $ (Const (@{const_name "power"}, _) $ ten $ exp))
  2.2460 +                   = @{term "float_divr"} $ prec' $ (@{term "Float"} $ to_int mantisse $ @{term "0 :: int"}) $ (@{term "power_float (Float 5 1)"} $ to_nat exp)
  2.2461 +      | top_float (Const (@{const_name "divide"}, _) $ mantisse $ ten)
  2.2462 +                   = @{term "float_divr"} $ prec' $ (@{term "Float"} $ to_int mantisse $ @{term "0 :: int"}) $ @{term "Float 5 1"}
  2.2463 +      | top_float mantisse = @{term "Float"} $ to_int mantisse $ @{term "0 :: int"}
  2.2464 +
  2.2465 +    val goal' : term = List.nth (prems_of thm, i - 1)
  2.2466 +
  2.2467 +    fun lift_bnd (t as (Const (@{const_name "op &"}, _) $ 
  2.2468 +                        (Const (@{const_name "less_eq"}, _) $ 
  2.2469 +                         bottom $ (Free (name, _))) $ 
  2.2470 +                        (Const (@{const_name "less_eq"}, _) $ _ $ top)))
  2.2471 +         = ((name, HOLogic.mk_prod (bot_float bottom, top_float top))
  2.2472 +            handle TERM (txt, ts) => raise TERM ("Premisse needs format '<num> <= <var> & <var> <= <num>', but found " ^
  2.2473 +                                  (Syntax.string_of_term ctxt t), [t]))
  2.2474 +      | lift_bnd t = raise TERM ("Premisse needs format '<num> <= <var> & <var> <= <num>', but found " ^
  2.2475 +                                 (Syntax.string_of_term ctxt t), [t])
  2.2476 +    val bound_eqs = map (HOLogic.dest_Trueprop #> lift_bnd)  (Logic.strip_imp_prems goal')
  2.2477 +
  2.2478 +    fun lift_var (Free (varname, _)) = (case AList.lookup (op =) bound_eqs varname of
  2.2479 +                                          SOME bound => bound
  2.2480 +                                        | NONE => raise TERM ("No bound equations found for " ^ varname, []))
  2.2481 +      | lift_var t = raise TERM ("Can not convert expression " ^ 
  2.2482 +                                 (Syntax.string_of_term ctxt t), [t])
  2.2483 +
  2.2484 +    val _ $ vs = HOLogic.dest_Trueprop (Logic.strip_imp_concl goal')
  2.2485 +
  2.2486 +    val bs = (HOLogic.dest_list #> map lift_var #> HOLogic.mk_list @{typ "float * float"}) vs
  2.2487 +    val map = [(@{cpat "?prec::nat"}, to_natc prec),
  2.2488 +               (@{cpat "?bs::(float * float) list"}, Thm.cterm_of (ProofContext.theory_of ctxt) bs)]
  2.2489 +  in rtac (Thm.instantiate ([], map) @{thm "uneq_approx"}) i thm end
  2.2490 +
  2.2491 +  val eval_tac = CSUBGOAL (fn (ct, i) => rtac (eval_oracle ct) i)
  2.2492 +
  2.2493 +  fun gen_eval_tac conv ctxt = CONVERSION (Conv.params_conv (~1) (K (Conv.concl_conv (~1) conv)) ctxt)
  2.2494 +                               THEN' rtac TrueI
  2.2495 +
  2.2496 +*}
  2.2497 +
  2.2498 +method_setup approximation = {* fn src => 
  2.2499 +  Method.syntax Args.term src #>
  2.2500 +  (fn (prec, ctxt) => let
  2.2501 +   in Method.SIMPLE_METHOD' (fn i =>
  2.2502 +     (DETERM (reify_uneq ctxt i)
  2.2503 +      THEN rule_uneq ctxt prec i
  2.2504 +      THEN Simplifier.asm_full_simp_tac bounded_by_simpset i 
  2.2505 +      THEN (TRY (filter_prems_tac (fn t => false) i))
  2.2506 +      THEN (gen_eval_tac eval_oracle ctxt) i))
  2.2507 +   end)
  2.2508 +*} "real number approximation"
  2.2509 +
  2.2510 +end
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/HOL/Decision_Procs/Cooper.thy	Fri Feb 06 15:15:32 2009 +0100
     3.3 @@ -0,0 +1,2174 @@
     3.4 +(*  Title:      HOL/Reflection/Cooper.thy
     3.5 +    Author:     Amine Chaieb
     3.6 +*)
     3.7 +
     3.8 +theory Cooper
     3.9 +imports Complex_Main Efficient_Nat
    3.10 +uses ("cooper_tac.ML")
    3.11 +begin
    3.12 +
    3.13 +function iupt :: "int \<Rightarrow> int \<Rightarrow> int list" where
    3.14 +  "iupt i j = (if j < i then [] else i # iupt (i+1) j)"
    3.15 +by pat_completeness auto
    3.16 +termination by (relation "measure (\<lambda> (i, j). nat (j-i+1))") auto
    3.17 +
    3.18 +lemma iupt_set: "set (iupt i j) = {i..j}"
    3.19 +  by (induct rule: iupt.induct) (simp add: simp_from_to)
    3.20 +
    3.21 +(* Periodicity of dvd *)
    3.22 +
    3.23 +  (*********************************************************************************)
    3.24 +  (****                            SHADOW SYNTAX AND SEMANTICS                  ****)
    3.25 +  (*********************************************************************************)
    3.26 +
    3.27 +datatype num = C int | Bound nat | CN nat int num | Neg num | Add num num| Sub num num 
    3.28 +  | Mul int num
    3.29 +
    3.30 +  (* A size for num to make inductive proofs simpler*)
    3.31 +primrec num_size :: "num \<Rightarrow> nat" where
    3.32 +  "num_size (C c) = 1"
    3.33 +| "num_size (Bound n) = 1"
    3.34 +| "num_size (Neg a) = 1 + num_size a"
    3.35 +| "num_size (Add a b) = 1 + num_size a + num_size b"
    3.36 +| "num_size (Sub a b) = 3 + num_size a + num_size b"
    3.37 +| "num_size (CN n c a) = 4 + num_size a"
    3.38 +| "num_size (Mul c a) = 1 + num_size a"
    3.39 +
    3.40 +primrec Inum :: "int list \<Rightarrow> num \<Rightarrow> int" where
    3.41 +  "Inum bs (C c) = c"
    3.42 +| "Inum bs (Bound n) = bs!n"
    3.43 +| "Inum bs (CN n c a) = c * (bs!n) + (Inum bs a)"
    3.44 +| "Inum bs (Neg a) = -(Inum bs a)"
    3.45 +| "Inum bs (Add a b) = Inum bs a + Inum bs b"
    3.46 +| "Inum bs (Sub a b) = Inum bs a - Inum bs b"
    3.47 +| "Inum bs (Mul c a) = c* Inum bs a"
    3.48 +
    3.49 +datatype fm  = 
    3.50 +  T| F| Lt num| Le num| Gt num| Ge num| Eq num| NEq num| Dvd int num| NDvd int num|
    3.51 +  NOT fm| And fm fm|  Or fm fm| Imp fm fm| Iff fm fm| E fm| A fm 
    3.52 +  | Closed nat | NClosed nat
    3.53 +
    3.54 +
    3.55 +  (* A size for fm *)
    3.56 +consts fmsize :: "fm \<Rightarrow> nat"
    3.57 +recdef fmsize "measure size"
    3.58 +  "fmsize (NOT p) = 1 + fmsize p"
    3.59 +  "fmsize (And p q) = 1 + fmsize p + fmsize q"
    3.60 +  "fmsize (Or p q) = 1 + fmsize p + fmsize q"
    3.61 +  "fmsize (Imp p q) = 3 + fmsize p + fmsize q"
    3.62 +  "fmsize (Iff p q) = 3 + 2*(fmsize p + fmsize q)"
    3.63 +  "fmsize (E p) = 1 + fmsize p"
    3.64 +  "fmsize (A p) = 4+ fmsize p"
    3.65 +  "fmsize (Dvd i t) = 2"
    3.66 +  "fmsize (NDvd i t) = 2"
    3.67 +  "fmsize p = 1"
    3.68 +  (* several lemmas about fmsize *)
    3.69 +lemma fmsize_pos: "fmsize p > 0"	
    3.70 +by (induct p rule: fmsize.induct) simp_all
    3.71 +
    3.72 +  (* Semantics of formulae (fm) *)
    3.73 +consts Ifm ::"bool list \<Rightarrow> int list \<Rightarrow> fm \<Rightarrow> bool"
    3.74 +primrec
    3.75 +  "Ifm bbs bs T = True"
    3.76 +  "Ifm bbs bs F = False"
    3.77 +  "Ifm bbs bs (Lt a) = (Inum bs a < 0)"
    3.78 +  "Ifm bbs bs (Gt a) = (Inum bs a > 0)"
    3.79 +  "Ifm bbs bs (Le a) = (Inum bs a \<le> 0)"
    3.80 +  "Ifm bbs bs (Ge a) = (Inum bs a \<ge> 0)"
    3.81 +  "Ifm bbs bs (Eq a) = (Inum bs a = 0)"
    3.82 +  "Ifm bbs bs (NEq a) = (Inum bs a \<noteq> 0)"
    3.83 +  "Ifm bbs bs (Dvd i b) = (i dvd Inum bs b)"
    3.84 +  "Ifm bbs bs (NDvd i b) = (\<not>(i dvd Inum bs b))"
    3.85 +  "Ifm bbs bs (NOT p) = (\<not> (Ifm bbs bs p))"
    3.86 +  "Ifm bbs bs (And p q) = (Ifm bbs bs p \<and> Ifm bbs bs q)"
    3.87 +  "Ifm bbs bs (Or p q) = (Ifm bbs bs p \<or> Ifm bbs bs q)"
    3.88 +  "Ifm bbs bs (Imp p q) = ((Ifm bbs bs p) \<longrightarrow> (Ifm bbs bs q))"
    3.89 +  "Ifm bbs bs (Iff p q) = (Ifm bbs bs p = Ifm bbs bs q)"
    3.90 +  "Ifm bbs bs (E p) = (\<exists> x. Ifm bbs (x#bs) p)"
    3.91 +  "Ifm bbs bs (A p) = (\<forall> x. Ifm bbs (x#bs) p)"
    3.92 +  "Ifm bbs bs (Closed n) = bbs!n"
    3.93 +  "Ifm bbs bs (NClosed n) = (\<not> bbs!n)"
    3.94 +
    3.95 +consts prep :: "fm \<Rightarrow> fm"
    3.96 +recdef prep "measure fmsize"
    3.97 +  "prep (E T) = T"
    3.98 +  "prep (E F) = F"
    3.99 +  "prep (E (Or p q)) = Or (prep (E p)) (prep (E q))"
   3.100 +  "prep (E (Imp p q)) = Or (prep (E (NOT p))) (prep (E q))"
   3.101 +  "prep (E (Iff p q)) = Or (prep (E (And p q))) (prep (E (And (NOT p) (NOT q))))" 
   3.102 +  "prep (E (NOT (And p q))) = Or (prep (E (NOT p))) (prep (E(NOT q)))"
   3.103 +  "prep (E (NOT (Imp p q))) = prep (E (And p (NOT q)))"
   3.104 +  "prep (E (NOT (Iff p q))) = Or (prep (E (And p (NOT q)))) (prep (E(And (NOT p) q)))"
   3.105 +  "prep (E p) = E (prep p)"
   3.106 +  "prep (A (And p q)) = And (prep (A p)) (prep (A q))"
   3.107 +  "prep (A p) = prep (NOT (E (NOT p)))"
   3.108 +  "prep (NOT (NOT p)) = prep p"
   3.109 +  "prep (NOT (And p q)) = Or (prep (NOT p)) (prep (NOT q))"
   3.110 +  "prep (NOT (A p)) = prep (E (NOT p))"
   3.111 +  "prep (NOT (Or p q)) = And (prep (NOT p)) (prep (NOT q))"
   3.112 +  "prep (NOT (Imp p q)) = And (prep p) (prep (NOT q))"
   3.113 +  "prep (NOT (Iff p q)) = Or (prep (And p (NOT q))) (prep (And (NOT p) q))"
   3.114 +  "prep (NOT p) = NOT (prep p)"
   3.115 +  "prep (Or p q) = Or (prep p) (prep q)"
   3.116 +  "prep (And p q) = And (prep p) (prep q)"
   3.117 +  "prep (Imp p q) = prep (Or (NOT p) q)"
   3.118 +  "prep (Iff p q) = Or (prep (And p q)) (prep (And (NOT p) (NOT q)))"
   3.119 +  "prep p = p"
   3.120 +(hints simp add: fmsize_pos)
   3.121 +lemma prep: "Ifm bbs bs (prep p) = Ifm bbs bs p"
   3.122 +by (induct p arbitrary: bs rule: prep.induct, auto)
   3.123 +
   3.124 +
   3.125 +  (* Quantifier freeness *)
   3.126 +consts qfree:: "fm \<Rightarrow> bool"
   3.127 +recdef qfree "measure size"
   3.128 +  "qfree (E p) = False"
   3.129 +  "qfree (A p) = False"
   3.130 +  "qfree (NOT p) = qfree p" 
   3.131 +  "qfree (And p q) = (qfree p \<and> qfree q)" 
   3.132 +  "qfree (Or  p q) = (qfree p \<and> qfree q)" 
   3.133 +  "qfree (Imp p q) = (qfree p \<and> qfree q)" 
   3.134 +  "qfree (Iff p q) = (qfree p \<and> qfree q)"
   3.135 +  "qfree p = True"
   3.136 +
   3.137 +  (* Boundedness and substitution *)
   3.138 +consts 
   3.139 +  numbound0:: "num \<Rightarrow> bool" (* a num is INDEPENDENT of Bound 0 *)
   3.140 +  bound0:: "fm \<Rightarrow> bool" (* A Formula is independent of Bound 0 *)
   3.141 +  subst0:: "num \<Rightarrow> fm \<Rightarrow> fm" (* substitue a num into a formula for Bound 0 *)
   3.142 +primrec
   3.143 +  "numbound0 (C c) = True"
   3.144 +  "numbound0 (Bound n) = (n>0)"
   3.145 +  "numbound0 (CN n i a) = (n >0 \<and> numbound0 a)"
   3.146 +  "numbound0 (Neg a) = numbound0 a"
   3.147 +  "numbound0 (Add a b) = (numbound0 a \<and> numbound0 b)"
   3.148 +  "numbound0 (Sub a b) = (numbound0 a \<and> numbound0 b)" 
   3.149 +  "numbound0 (Mul i a) = numbound0 a"
   3.150 +
   3.151 +lemma numbound0_I:
   3.152 +  assumes nb: "numbound0 a"
   3.153 +  shows "Inum (b#bs) a = Inum (b'#bs) a"
   3.154 +using nb
   3.155 +by (induct a rule: numbound0.induct) (auto simp add: gr0_conv_Suc)
   3.156 +
   3.157 +primrec
   3.158 +  "bound0 T = True"
   3.159 +  "bound0 F = True"
   3.160 +  "bound0 (Lt a) = numbound0 a"
   3.161 +  "bound0 (Le a) = numbound0 a"
   3.162 +  "bound0 (Gt a) = numbound0 a"
   3.163 +  "bound0 (Ge a) = numbound0 a"
   3.164 +  "bound0 (Eq a) = numbound0 a"
   3.165 +  "bound0 (NEq a) = numbound0 a"
   3.166 +  "bound0 (Dvd i a) = numbound0 a"
   3.167 +  "bound0 (NDvd i a) = numbound0 a"
   3.168 +  "bound0 (NOT p) = bound0 p"
   3.169 +  "bound0 (And p q) = (bound0 p \<and> bound0 q)"
   3.170 +  "bound0 (Or p q) = (bound0 p \<and> bound0 q)"
   3.171 +  "bound0 (Imp p q) = ((bound0 p) \<and> (bound0 q))"
   3.172 +  "bound0 (Iff p q) = (bound0 p \<and> bound0 q)"
   3.173 +  "bound0 (E p) = False"
   3.174 +  "bound0 (A p) = False"
   3.175 +  "bound0 (Closed P) = True"
   3.176 +  "bound0 (NClosed P) = True"
   3.177 +lemma bound0_I:
   3.178 +  assumes bp: "bound0 p"
   3.179 +  shows "Ifm bbs (b#bs) p = Ifm bbs (b'#bs) p"
   3.180 +using bp numbound0_I[where b="b" and bs="bs" and b'="b'"]
   3.181 +by (induct p rule: bound0.induct) (auto simp add: gr0_conv_Suc)
   3.182 +
   3.183 +fun numsubst0:: "num \<Rightarrow> num \<Rightarrow> num" where
   3.184 +  "numsubst0 t (C c) = (C c)"
   3.185 +| "numsubst0 t (Bound n) = (if n=0 then t else Bound n)"
   3.186 +| "numsubst0 t (CN 0 i a) = Add (Mul i t) (numsubst0 t a)"
   3.187 +| "numsubst0 t (CN n i a) = CN n i (numsubst0 t a)"
   3.188 +| "numsubst0 t (Neg a) = Neg (numsubst0 t a)"
   3.189 +| "numsubst0 t (Add a b) = Add (numsubst0 t a) (numsubst0 t b)"
   3.190 +| "numsubst0 t (Sub a b) = Sub (numsubst0 t a) (numsubst0 t b)" 
   3.191 +| "numsubst0 t (Mul i a) = Mul i (numsubst0 t a)"
   3.192 +
   3.193 +lemma numsubst0_I:
   3.194 +  "Inum (b#bs) (numsubst0 a t) = Inum ((Inum (b#bs) a)#bs) t"
   3.195 +by (induct t rule: numsubst0.induct,auto simp:nth_Cons')
   3.196 +
   3.197 +lemma numsubst0_I':
   3.198 +  "numbound0 a \<Longrightarrow> Inum (b#bs) (numsubst0 a t) = Inum ((Inum (b'#bs) a)#bs) t"
   3.199 +by (induct t rule: numsubst0.induct, auto simp: nth_Cons' numbound0_I[where b="b" and b'="b'"])
   3.200 +
   3.201 +primrec
   3.202 +  "subst0 t T = T"
   3.203 +  "subst0 t F = F"
   3.204 +  "subst0 t (Lt a) = Lt (numsubst0 t a)"
   3.205 +  "subst0 t (Le a) = Le (numsubst0 t a)"
   3.206 +  "subst0 t (Gt a) = Gt (numsubst0 t a)"
   3.207 +  "subst0 t (Ge a) = Ge (numsubst0 t a)"
   3.208 +  "subst0 t (Eq a) = Eq (numsubst0 t a)"
   3.209 +  "subst0 t (NEq a) = NEq (numsubst0 t a)"
   3.210 +  "subst0 t (Dvd i a) = Dvd i (numsubst0 t a)"
   3.211 +  "subst0 t (NDvd i a) = NDvd i (numsubst0 t a)"
   3.212 +  "subst0 t (NOT p) = NOT (subst0 t p)"
   3.213 +  "subst0 t (And p q) = And (subst0 t p) (subst0 t q)"
   3.214 +  "subst0 t (Or p q) = Or (subst0 t p) (subst0 t q)"
   3.215 +  "subst0 t (Imp p q) = Imp (subst0 t p) (subst0 t q)"
   3.216 +  "subst0 t (Iff p q) = Iff (subst0 t p) (subst0 t q)"
   3.217 +  "subst0 t (Closed P) = (Closed P)"
   3.218 +  "subst0 t (NClosed P) = (NClosed P)"
   3.219 +
   3.220 +lemma subst0_I: assumes qfp: "qfree p"
   3.221 +  shows "Ifm bbs (b#bs) (subst0 a p) = Ifm bbs ((Inum (b#bs) a)#bs) p"
   3.222 +  using qfp numsubst0_I[where b="b" and bs="bs" and a="a"]
   3.223 +  by (induct p) (simp_all add: gr0_conv_Suc)
   3.224 +
   3.225 +
   3.226 +consts 
   3.227 +  decrnum:: "num \<Rightarrow> num" 
   3.228 +  decr :: "fm \<Rightarrow> fm"
   3.229 +
   3.230 +recdef decrnum "measure size"
   3.231 +  "decrnum (Bound n) = Bound (n - 1)"
   3.232 +  "decrnum (Neg a) = Neg (decrnum a)"
   3.233 +  "decrnum (Add a b) = Add (decrnum a) (decrnum b)"
   3.234 +  "decrnum (Sub a b) = Sub (decrnum a) (decrnum b)"
   3.235 +  "decrnum (Mul c a) = Mul c (decrnum a)"
   3.236 +  "decrnum (CN n i a) = (CN (n - 1) i (decrnum a))"
   3.237 +  "decrnum a = a"
   3.238 +
   3.239 +recdef decr "measure size"
   3.240 +  "decr (Lt a) = Lt (decrnum a)"
   3.241 +  "decr (Le a) = Le (decrnum a)"
   3.242 +  "decr (Gt a) = Gt (decrnum a)"
   3.243 +  "decr (Ge a) = Ge (decrnum a)"
   3.244 +  "decr (Eq a) = Eq (decrnum a)"
   3.245 +  "decr (NEq a) = NEq (decrnum a)"
   3.246 +  "decr (Dvd i a) = Dvd i (decrnum a)"
   3.247 +  "decr (NDvd i a) = NDvd i (decrnum a)"
   3.248 +  "decr (NOT p) = NOT (decr p)" 
   3.249 +  "decr (And p q) = And (decr p) (decr q)"
   3.250 +  "decr (Or p q) = Or (decr p) (decr q)"
   3.251 +  "decr (Imp p q) = Imp (decr p) (decr q)"
   3.252 +  "decr (Iff p q) = Iff (decr p) (decr q)"
   3.253 +  "decr p = p"
   3.254 +
   3.255 +lemma decrnum: assumes nb: "numbound0 t"
   3.256 +  shows "Inum (x#bs) t = Inum bs (decrnum t)"
   3.257 +  using nb by (induct t rule: decrnum.induct, auto simp add: gr0_conv_Suc)
   3.258 +
   3.259 +lemma decr: assumes nb: "bound0 p"
   3.260 +  shows "Ifm bbs (x#bs) p = Ifm bbs bs (decr p)"
   3.261 +  using nb 
   3.262 +  by (induct p rule: decr.induct, simp_all add: gr0_conv_Suc decrnum)
   3.263 +
   3.264 +lemma decr_qf: "bound0 p \<Longrightarrow> qfree (decr p)"
   3.265 +by (induct p, simp_all)
   3.266 +
   3.267 +consts 
   3.268 +  isatom :: "fm \<Rightarrow> bool" (* test for atomicity *)
   3.269 +recdef isatom "measure size"
   3.270 +  "isatom T = True"
   3.271 +  "isatom F = True"
   3.272 +  "isatom (Lt a) = True"
   3.273 +  "isatom (Le a) = True"
   3.274 +  "isatom (Gt a) = True"
   3.275 +  "isatom (Ge a) = True"
   3.276 +  "isatom (Eq a) = True"
   3.277 +  "isatom (NEq a) = True"
   3.278 +  "isatom (Dvd i b) = True"
   3.279 +  "isatom (NDvd i b) = True"
   3.280 +  "isatom (Closed P) = True"
   3.281 +  "isatom (NClosed P) = True"
   3.282 +  "isatom p = False"
   3.283 +
   3.284 +lemma numsubst0_numbound0: assumes nb: "numbound0 t"
   3.285 +  shows "numbound0 (numsubst0 t a)"
   3.286 +using nb apply (induct a rule: numbound0.induct)
   3.287 +apply simp_all
   3.288 +apply (case_tac n, simp_all)
   3.289 +done
   3.290 +
   3.291 +lemma subst0_bound0: assumes qf: "qfree p" and nb: "numbound0 t"
   3.292 +  shows "bound0 (subst0 t p)"
   3.293 +using qf numsubst0_numbound0[OF nb] by (induct p  rule: subst0.induct, auto)
   3.294 +
   3.295 +lemma bound0_qf: "bound0 p \<Longrightarrow> qfree p"
   3.296 +by (induct p, simp_all)
   3.297 +
   3.298 +
   3.299 +constdefs djf:: "('a \<Rightarrow> fm) \<Rightarrow> 'a \<Rightarrow> fm \<Rightarrow> fm"
   3.300 +  "djf f p q \<equiv> (if q=T then T else if q=F then f p else 
   3.301 +  (let fp = f p in case fp of T \<Rightarrow> T | F \<Rightarrow> q | _ \<Rightarrow> Or (f p) q))"
   3.302 +constdefs evaldjf:: "('a \<Rightarrow> fm) \<Rightarrow> 'a list \<Rightarrow> fm"
   3.303 +  "evaldjf f ps \<equiv> foldr (djf f) ps F"
   3.304 +
   3.305 +lemma djf_Or: "Ifm bbs bs (djf f p q) = Ifm bbs bs (Or (f p) q)"
   3.306 +by (cases "q=T", simp add: djf_def,cases "q=F",simp add: djf_def) 
   3.307 +(cases "f p", simp_all add: Let_def djf_def) 
   3.308 +
   3.309 +lemma evaldjf_ex: "Ifm bbs bs (evaldjf f ps) = (\<exists> p \<in> set ps. Ifm bbs bs (f p))"
   3.310 +  by(induct ps, simp_all add: evaldjf_def djf_Or)
   3.311 +
   3.312 +lemma evaldjf_bound0: 
   3.313 +  assumes nb: "\<forall> x\<in> set xs. bound0 (f x)"
   3.314 +  shows "bound0 (evaldjf f xs)"
   3.315 +  using nb by (induct xs, auto simp add: evaldjf_def djf_def Let_def) (case_tac "f a", auto) 
   3.316 +
   3.317 +lemma evaldjf_qf: 
   3.318 +  assumes nb: "\<forall> x\<in> set xs. qfree (f x)"
   3.319 +  shows "qfree (evaldjf f xs)"
   3.320 +  using nb by (induct xs, auto simp add: evaldjf_def djf_def Let_def) (case_tac "f a", auto) 
   3.321 +
   3.322 +consts disjuncts :: "fm \<Rightarrow> fm list"
   3.323 +recdef disjuncts "measure size"
   3.324 +  "disjuncts (Or p q) = (disjuncts p) @ (disjuncts q)"
   3.325 +  "disjuncts F = []"
   3.326 +  "disjuncts p = [p]"
   3.327 +
   3.328 +lemma disjuncts: "(\<exists> q\<in> set (disjuncts p). Ifm bbs bs q) = Ifm bbs bs p"
   3.329 +by(induct p rule: disjuncts.induct, auto)
   3.330 +
   3.331 +lemma disjuncts_nb: "bound0 p \<Longrightarrow> \<forall> q\<in> set (disjuncts p). bound0 q"
   3.332 +proof-
   3.333 +  assume nb: "bound0 p"
   3.334 +  hence "list_all bound0 (disjuncts p)" by (induct p rule:disjuncts.induct,auto)
   3.335 +  thus ?thesis by (simp only: list_all_iff)
   3.336 +qed
   3.337 +
   3.338 +lemma disjuncts_qf: "qfree p \<Longrightarrow> \<forall> q\<in> set (disjuncts p). qfree q"
   3.339 +proof-
   3.340 +  assume qf: "qfree p"
   3.341 +  hence "list_all qfree (disjuncts p)"
   3.342 +    by (induct p rule: disjuncts.induct, auto)
   3.343 +  thus ?thesis by (simp only: list_all_iff)
   3.344 +qed
   3.345 +
   3.346 +constdefs DJ :: "(fm \<Rightarrow> fm) \<Rightarrow> fm \<Rightarrow> fm"
   3.347 +  "DJ f p \<equiv> evaldjf f (disjuncts p)"
   3.348 +
   3.349 +lemma DJ: assumes fdj: "\<forall> p q. f (Or p q) = Or (f p) (f q)"
   3.350 +  and fF: "f F = F"
   3.351 +  shows "Ifm bbs bs (DJ f p) = Ifm bbs bs (f p)"
   3.352 +proof-
   3.353 +  have "Ifm bbs bs (DJ f p) = (\<exists> q \<in> set (disjuncts p). Ifm bbs bs (f q))"
   3.354 +    by (simp add: DJ_def evaldjf_ex) 
   3.355 +  also have "\<dots> = Ifm bbs bs (f p)" using fdj fF by (induct p rule: disjuncts.induct, auto)
   3.356 +  finally show ?thesis .
   3.357 +qed
   3.358 +
   3.359 +lemma DJ_qf: assumes 
   3.360 +  fqf: "\<forall> p. qfree p \<longrightarrow> qfree (f p)"
   3.361 +  shows "\<forall>p. qfree p \<longrightarrow> qfree (DJ f p) "
   3.362 +proof(clarify)
   3.363 +  fix  p assume qf: "qfree p"
   3.364 +  have th: "DJ f p = evaldjf f (disjuncts p)" by (simp add: DJ_def)
   3.365 +  from disjuncts_qf[OF qf] have "\<forall> q\<in> set (disjuncts p). qfree q" .
   3.366 +  with fqf have th':"\<forall> q\<in> set (disjuncts p). qfree (f q)" by blast
   3.367 +  
   3.368 +  from evaldjf_qf[OF th'] th show "qfree (DJ f p)" by simp
   3.369 +qed
   3.370 +
   3.371 +lemma DJ_qe: assumes qe: "\<forall> bs p. qfree p \<longrightarrow> qfree (qe p) \<and> (Ifm bbs bs (qe p) = Ifm bbs bs (E p))"
   3.372 +  shows "\<forall> bs p. qfree p \<longrightarrow> qfree (DJ qe p) \<and> (Ifm bbs bs ((DJ qe p)) = Ifm bbs bs (E p))"
   3.373 +proof(clarify)
   3.374 +  fix p::fm and bs
   3.375 +  assume qf: "qfree p"
   3.376 +  from qe have qth: "\<forall> p. qfree p \<longrightarrow> qfree (qe p)" by blast
   3.377 +  from DJ_qf[OF qth] qf have qfth:"qfree (DJ qe p)" by auto
   3.378 +  have "Ifm bbs bs (DJ qe p) = (\<exists> q\<in> set (disjuncts p). Ifm bbs bs (qe q))"
   3.379 +    by (simp add: DJ_def evaldjf_ex)
   3.380 +  also have "\<dots> = (\<exists> q \<in> set(disjuncts p). Ifm bbs bs (E q))" using qe disjuncts_qf[OF qf] by auto
   3.381 +  also have "\<dots> = Ifm bbs bs (E p)" by (induct p rule: disjuncts.induct, auto)
   3.382 +  finally show "qfree (DJ qe p) \<and> Ifm bbs bs (DJ qe p) = Ifm bbs bs (E p)" using qfth by blast
   3.383 +qed
   3.384 +  (* Simplification *)
   3.385 +
   3.386 +  (* Algebraic simplifications for nums *)
   3.387 +consts bnds:: "num \<Rightarrow> nat list"
   3.388 +  lex_ns:: "nat list \<times> nat list \<Rightarrow> bool"
   3.389 +recdef bnds "measure size"
   3.390 +  "bnds (Bound n) = [n]"
   3.391 +  "bnds (CN n c a) = n#(bnds a)"
   3.392 +  "bnds (Neg a) = bnds a"
   3.393 +  "bnds (Add a b) = (bnds a)@(bnds b)"
   3.394 +  "bnds (Sub a b) = (bnds a)@(bnds b)"
   3.395 +  "bnds (Mul i a) = bnds a"
   3.396 +  "bnds a = []"
   3.397 +recdef lex_ns "measure (\<lambda> (xs,ys). length xs + length ys)"
   3.398 +  "lex_ns ([], ms) = True"
   3.399 +  "lex_ns (ns, []) = False"
   3.400 +  "lex_ns (n#ns, m#ms) = (n<m \<or> ((n = m) \<and> lex_ns (ns,ms))) "
   3.401 +constdefs lex_bnd :: "num \<Rightarrow> num \<Rightarrow> bool"
   3.402 +  "lex_bnd t s \<equiv> lex_ns (bnds t, bnds s)"
   3.403 +
   3.404 +consts
   3.405 +  numadd:: "num \<times> num \<Rightarrow> num"
   3.406 +recdef numadd "measure (\<lambda> (t,s). num_size t + num_size s)"
   3.407 +  "numadd (CN n1 c1 r1 ,CN n2 c2 r2) =
   3.408 +  (if n1=n2 then 
   3.409 +  (let c = c1 + c2
   3.410 +  in (if c=0 then numadd(r1,r2) else CN n1 c (numadd (r1,r2))))
   3.411 +  else if n1 \<le> n2 then CN n1 c1 (numadd (r1,Add (Mul c2 (Bound n2)) r2))
   3.412 +  else CN n2 c2 (numadd (Add (Mul c1 (Bound n1)) r1,r2)))"
   3.413 +  "numadd (CN n1 c1 r1, t) = CN n1 c1 (numadd (r1, t))"  
   3.414 +  "numadd (t,CN n2 c2 r2) = CN n2 c2 (numadd (t,r2))" 
   3.415 +  "numadd (C b1, C b2) = C (b1+b2)"
   3.416 +  "numadd (a,b) = Add a b"
   3.417 +
   3.418 +(*function (sequential)
   3.419 +  numadd :: "num \<Rightarrow> num \<Rightarrow> num"
   3.420 +where
   3.421 +  "numadd (Add (Mul c1 (Bound n1)) r1) (Add (Mul c2 (Bound n2)) r2) =
   3.422 +      (if n1 = n2 then (let c = c1 + c2
   3.423 +      in (if c = 0 then numadd r1 r2 else
   3.424 +        Add (Mul c (Bound n1)) (numadd r1 r2)))
   3.425 +      else if n1 \<le> n2 then
   3.426 +        Add (Mul c1 (Bound n1)) (numadd r1 (Add (Mul c2 (Bound n2)) r2))
   3.427 +      else
   3.428 +        Add (Mul c2 (Bound n2)) (numadd (Add (Mul c1 (Bound n1)) r1) r2))"
   3.429 +  | "numadd (Add (Mul c1 (Bound n1)) r1) t =
   3.430 +      Add (Mul c1 (Bound n1)) (numadd r1 t)"  
   3.431 +  | "numadd t (Add (Mul c2 (Bound n2)) r2) =
   3.432 +      Add (Mul c2 (Bound n2)) (numadd t r2)" 
   3.433 +  | "numadd (C b1) (C b2) = C (b1 + b2)"
   3.434 +  | "numadd a b = Add a b"
   3.435 +apply pat_completeness apply auto*)
   3.436 +  
   3.437 +lemma numadd: "Inum bs (numadd (t,s)) = Inum bs (Add t s)"
   3.438 +apply (induct t s rule: numadd.induct, simp_all add: Let_def)
   3.439 +apply (case_tac "c1+c2 = 0",case_tac "n1 \<le> n2", simp_all)
   3.440 + apply (case_tac "n1 = n2")
   3.441 +  apply(simp_all add: algebra_simps)
   3.442 +apply(simp add: left_distrib[symmetric])
   3.443 +done
   3.444 +
   3.445 +lemma numadd_nb: "\<lbrakk> numbound0 t ; numbound0 s\<rbrakk> \<Longrightarrow> numbound0 (numadd (t,s))"
   3.446 +by (induct t s rule: numadd.induct, auto simp add: Let_def)
   3.447 +
   3.448 +fun
   3.449 +  nummul :: "int \<Rightarrow> num \<Rightarrow> num"
   3.450 +where
   3.451 +  "nummul i (C j) = C (i * j)"
   3.452 +  | "nummul i (CN n c t) = CN n (c*i) (nummul i t)"
   3.453 +  | "nummul i t = Mul i t"
   3.454 +
   3.455 +lemma nummul: "\<And> i. Inum bs (nummul i t) = Inum bs (Mul i t)"
   3.456 +by (induct t rule: nummul.induct, auto simp add: algebra_simps numadd)
   3.457 +
   3.458 +lemma nummul_nb: "\<And> i. numbound0 t \<Longrightarrow> numbound0 (nummul i t)"
   3.459 +by (induct t rule: nummul.induct, auto simp add: numadd_nb)
   3.460 +
   3.461 +constdefs numneg :: "num \<Rightarrow> num"
   3.462 +  "numneg t \<equiv> nummul (- 1) t"
   3.463 +
   3.464 +constdefs numsub :: "num \<Rightarrow> num \<Rightarrow> num"
   3.465 +  "numsub s t \<equiv> (if s = t then C 0 else numadd (s, numneg t))"
   3.466 +
   3.467 +lemma numneg: "Inum bs (numneg t) = Inum bs (Neg t)"
   3.468 +using numneg_def nummul by simp
   3.469 +
   3.470 +lemma numneg_nb: "numbound0 t \<Longrightarrow> numbound0 (numneg t)"
   3.471 +using numneg_def nummul_nb by simp
   3.472 +
   3.473 +lemma numsub: "Inum bs (numsub a b) = Inum bs (Sub a b)"
   3.474 +using numneg numadd numsub_def by simp
   3.475 +
   3.476 +lemma numsub_nb: "\<lbrakk> numbound0 t ; numbound0 s\<rbrakk> \<Longrightarrow> numbound0 (numsub t s)"
   3.477 +using numsub_def numadd_nb numneg_nb by simp
   3.478 +
   3.479 +fun
   3.480 +  simpnum :: "num \<Rightarrow> num"
   3.481 +where
   3.482 +  "simpnum (C j) = C j"
   3.483 +  | "simpnum (Bound n) = CN n 1 (C 0)"
   3.484 +  | "simpnum (Neg t) = numneg (simpnum t)"
   3.485 +  | "simpnum (Add t s) = numadd (simpnum t, simpnum s)"
   3.486 +  | "simpnum (Sub t s) = numsub (simpnum t) (simpnum s)"
   3.487 +  | "simpnum (Mul i t) = (if i = 0 then C 0 else nummul i (simpnum t))"
   3.488 +  | "simpnum t = t"
   3.489 +
   3.490 +lemma simpnum_ci: "Inum bs (simpnum t) = Inum bs t"
   3.491 +by (induct t rule: simpnum.induct, auto simp add: numneg numadd numsub nummul)
   3.492 +
   3.493 +lemma simpnum_numbound0: 
   3.494 +  "numbound0 t \<Longrightarrow> numbound0 (simpnum t)"
   3.495 +by (induct t rule: simpnum.induct, auto simp add: numadd_nb numsub_nb nummul_nb numneg_nb)
   3.496 +
   3.497 +fun
   3.498 +  not :: "fm \<Rightarrow> fm"
   3.499 +where
   3.500 +  "not (NOT p) = p"
   3.501 +  | "not T = F"
   3.502 +  | "not F = T"
   3.503 +  | "not p = NOT p"
   3.504 +lemma not: "Ifm bbs bs (not p) = Ifm bbs bs (NOT p)"
   3.505 +by (cases p) auto
   3.506 +lemma not_qf: "qfree p \<Longrightarrow> qfree (not p)"
   3.507 +by (cases p, auto)
   3.508 +lemma not_bn: "bound0 p \<Longrightarrow> bound0 (not p)"
   3.509 +by (cases p, auto)
   3.510 +
   3.511 +constdefs conj :: "fm \<Rightarrow> fm \<Rightarrow> fm"
   3.512 +  "conj p q \<equiv> (if (p = F \<or> q=F) then F else if p=T then q else if q=T then p else And p q)"
   3.513 +lemma conj: "Ifm bbs bs (conj p q) = Ifm bbs bs (And p q)"
   3.514 +by (cases "p=F \<or> q=F",simp_all add: conj_def) (cases p,simp_all)
   3.515 +
   3.516 +lemma conj_qf: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (conj p q)"
   3.517 +using conj_def by auto 
   3.518 +lemma conj_nb: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (conj p q)"
   3.519 +using conj_def by auto 
   3.520 +
   3.521 +constdefs disj :: "fm \<Rightarrow> fm \<Rightarrow> fm"
   3.522 +  "disj p q \<equiv> (if (p = T \<or> q=T) then T else if p=F then q else if q=F then p else Or p q)"
   3.523 +
   3.524 +lemma disj: "Ifm bbs bs (disj p q) = Ifm bbs bs (Or p q)"
   3.525 +by (cases "p=T \<or> q=T",simp_all add: disj_def) (cases p,simp_all)
   3.526 +lemma disj_qf: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (disj p q)"
   3.527 +using disj_def by auto 
   3.528 +lemma disj_nb: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (disj p q)"
   3.529 +using disj_def by auto 
   3.530 +
   3.531 +constdefs   imp :: "fm \<Rightarrow> fm \<Rightarrow> fm"
   3.532 +  "imp p q \<equiv> (if (p = F \<or> q=T) then T else if p=T then q else if q=F then not p else Imp p q)"
   3.533 +lemma imp: "Ifm bbs bs (imp p q) = Ifm bbs bs (Imp p q)"
   3.534 +by (cases "p=F \<or> q=T",simp_all add: imp_def,cases p) (simp_all add: not)
   3.535 +lemma imp_qf: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (imp p q)"
   3.536 +using imp_def by (cases "p=F \<or> q=T",simp_all add: imp_def,cases p) (simp_all add: not_qf) 
   3.537 +lemma imp_nb: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (imp p q)"
   3.538 +using imp_def by (cases "p=F \<or> q=T",simp_all add: imp_def,cases p) simp_all
   3.539 +
   3.540 +constdefs   iff :: "fm \<Rightarrow> fm \<Rightarrow> fm"
   3.541 +  "iff p q \<equiv> (if (p = q) then T else if (p = not q \<or> not p = q) then F else 
   3.542 +       if p=F then not q else if q=F then not p else if p=T then q else if q=T then p else 
   3.543 +  Iff p q)"
   3.544 +lemma iff: "Ifm bbs bs (iff p q) = Ifm bbs bs (Iff p q)"
   3.545 +  by (unfold iff_def,cases "p=q", simp,cases "p=not q", simp add:not) 
   3.546 +(cases "not p= q", auto simp add:not)
   3.547 +lemma iff_qf: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (iff p q)"
   3.548 +  by (unfold iff_def,cases "p=q", auto simp add: not_qf)
   3.549 +lemma iff_nb: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (iff p q)"
   3.550 +using iff_def by (unfold iff_def,cases "p=q", auto simp add: not_bn)
   3.551 +
   3.552 +function (sequential)
   3.553 +  simpfm :: "fm \<Rightarrow> fm"
   3.554 +where
   3.555 +  "simpfm (And p q) = conj (simpfm p) (simpfm q)"
   3.556 +  | "simpfm (Or p q) = disj (simpfm p) (simpfm q)"
   3.557 +  | "simpfm (Imp p q) = imp (simpfm p) (simpfm q)"
   3.558 +  | "simpfm (Iff p q) = iff (simpfm p) (simpfm q)"
   3.559 +  | "simpfm (NOT p) = not (simpfm p)"
   3.560 +  | "simpfm (Lt a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v < 0) then T else F 
   3.561 +      | _ \<Rightarrow> Lt a')"
   3.562 +  | "simpfm (Le a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v \<le> 0)  then T else F | _ \<Rightarrow> Le a')"
   3.563 +  | "simpfm (Gt a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v > 0)  then T else F | _ \<Rightarrow> Gt a')"
   3.564 +  | "simpfm (Ge a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v \<ge> 0)  then T else F | _ \<Rightarrow> Ge a')"
   3.565 +  | "simpfm (Eq a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v = 0)  then T else F | _ \<Rightarrow> Eq a')"
   3.566 +  | "simpfm (NEq a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v \<noteq> 0)  then T else F | _ \<Rightarrow> NEq a')"
   3.567 +  | "simpfm (Dvd i a) = (if i=0 then simpfm (Eq a)
   3.568 +             else if (abs i = 1) then T
   3.569 +             else let a' = simpnum a in case a' of C v \<Rightarrow> if (i dvd v)  then T else F | _ \<Rightarrow> Dvd i a')"
   3.570 +  | "simpfm (NDvd i a) = (if i=0 then simpfm (NEq a) 
   3.571 +             else if (abs i = 1) then F
   3.572 +             else let a' = simpnum a in case a' of C v \<Rightarrow> if (\<not>(i dvd v)) then T else F | _ \<Rightarrow> NDvd i a')"
   3.573 +  | "simpfm p = p"
   3.574 +by pat_completeness auto
   3.575 +termination by (relation "measure fmsize") auto
   3.576 +
   3.577 +lemma simpfm: "Ifm bbs bs (simpfm p) = Ifm bbs bs p"
   3.578 +proof(induct p rule: simpfm.induct)
   3.579 +  case (6 a) let ?sa = "simpnum a" from simpnum_ci have sa: "Inum bs ?sa = Inum bs a" by simp
   3.580 +  {fix v assume "?sa = C v" hence ?case using sa by simp }
   3.581 +  moreover {assume "\<not> (\<exists> v. ?sa = C v)" hence ?case using sa 
   3.582 +      by (cases ?sa, simp_all add: Let_def)}
   3.583 +  ultimately show ?case by blast
   3.584 +next
   3.585 +  case (7 a)  let ?sa = "simpnum a" 
   3.586 +  from simpnum_ci have sa: "Inum bs ?sa = Inum bs a" by simp
   3.587 +  {fix v assume "?sa = C v" hence ?case using sa by simp }
   3.588 +  moreover {assume "\<not> (\<exists> v. ?sa = C v)" hence ?case using sa 
   3.589 +      by (cases ?sa, simp_all add: Let_def)}
   3.590 +  ultimately show ?case by blast
   3.591 +next
   3.592 +  case (8 a)  let ?sa = "simpnum a" 
   3.593 +  from simpnum_ci have sa: "Inum bs ?sa = Inum bs a" by simp
   3.594 +  {fix v assume "?sa = C v" hence ?case using sa by simp }
   3.595 +  moreover {assume "\<not> (\<exists> v. ?sa = C v)" hence ?case using sa 
   3.596 +      by (cases ?sa, simp_all add: Let_def)}
   3.597 +  ultimately show ?case by blast
   3.598 +next
   3.599 +  case (9 a)  let ?sa = "simpnum a" 
   3.600 +  from simpnum_ci have sa: "Inum bs ?sa = Inum bs a" by simp
   3.601 +  {fix v assume "?sa = C v" hence ?case using sa by simp }
   3.602 +  moreover {assume "\<not> (\<exists> v. ?sa = C v)" hence ?case using sa 
   3.603 +      by (cases ?sa, simp_all add: Let_def)}
   3.604 +  ultimately show ?case by blast
   3.605 +next
   3.606 +  case (10 a)  let ?sa = "simpnum a" 
   3.607 +  from simpnum_ci have sa: "Inum bs ?sa = Inum bs a" by simp
   3.608 +  {fix v assume "?sa = C v" hence ?case using sa by simp }
   3.609 +  moreover {assume "\<not> (\<exists> v. ?sa = C v)" hence ?case using sa 
   3.610 +      by (cases ?sa, simp_all add: Let_def)}
   3.611 +  ultimately show ?case by blast
   3.612 +next
   3.613 +  case (11 a)  let ?sa = "simpnum a" 
   3.614 +  from simpnum_ci have sa: "Inum bs ?sa = Inum bs a" by simp
   3.615 +  {fix v assume "?sa = C v" hence ?case using sa by simp }
   3.616 +  moreover {assume "\<not> (\<exists> v. ?sa = C v)" hence ?case using sa 
   3.617 +      by (cases ?sa, simp_all add: Let_def)}
   3.618 +  ultimately show ?case by blast
   3.619 +next
   3.620 +  case (12 i a)  let ?sa = "simpnum a" from simpnum_ci 
   3.621 +  have sa: "Inum bs ?sa = Inum bs a" by simp
   3.622 +  have "i=0 \<or> abs i = 1 \<or> (i\<noteq>0 \<and> (abs i \<noteq> 1))" by auto
   3.623 +  {assume "i=0" hence ?case using "12.hyps" by (simp add: dvd_def Let_def)}
   3.624 +  moreover 
   3.625 +  {assume i1: "abs i = 1"
   3.626 +      from zdvd_1_left[where m = "Inum bs a"] uminus_dvd_conv[where d="1" and t="Inum bs a"]
   3.627 +      have ?case using i1 apply (cases "i=0", simp_all add: Let_def) 
   3.628 +	by (cases "i > 0", simp_all)}
   3.629 +  moreover   
   3.630 +  {assume inz: "i\<noteq>0" and cond: "abs i \<noteq> 1"
   3.631 +    {fix v assume "?sa = C v" hence ?case using sa[symmetric] inz cond
   3.632 +	by (cases "abs i = 1", auto) }
   3.633 +    moreover {assume "\<not> (\<exists> v. ?sa = C v)" 
   3.634 +      hence "simpfm (Dvd i a) = Dvd i ?sa" using inz cond 
   3.635 +	by (cases ?sa, auto simp add: Let_def)
   3.636 +      hence ?case using sa by simp}
   3.637 +    ultimately have ?case by blast}
   3.638 +  ultimately show ?case by blast
   3.639 +next
   3.640 +  case (13 i a)  let ?sa = "simpnum a" from simpnum_ci 
   3.641 +  have sa: "Inum bs ?sa = Inum bs a" by simp
   3.642 +  have "i=0 \<or> abs i = 1 \<or> (i\<noteq>0 \<and> (abs i \<noteq> 1))" by auto
   3.643 +  {assume "i=0" hence ?case using "13.hyps" by (simp add: dvd_def Let_def)}
   3.644 +  moreover 
   3.645 +  {assume i1: "abs i = 1"
   3.646 +      from zdvd_1_left[where m = "Inum bs a"] uminus_dvd_conv[where d="1" and t="Inum bs a"]
   3.647 +      have ?case using i1 apply (cases "i=0", simp_all add: Let_def)
   3.648 +      apply (cases "i > 0", simp_all) done}
   3.649 +  moreover   
   3.650 +  {assume inz: "i\<noteq>0" and cond: "abs i \<noteq> 1"
   3.651 +    {fix v assume "?sa = C v" hence ?case using sa[symmetric] inz cond
   3.652 +	by (cases "abs i = 1", auto) }
   3.653 +    moreover {assume "\<not> (\<exists> v. ?sa = C v)" 
   3.654 +      hence "simpfm (NDvd i a) = NDvd i ?sa" using inz cond 
   3.655 +	by (cases ?sa, auto simp add: Let_def)
   3.656 +      hence ?case using sa by simp}
   3.657 +    ultimately have ?case by blast}
   3.658 +  ultimately show ?case by blast
   3.659 +qed (induct p rule: simpfm.induct, simp_all add: conj disj imp iff not)
   3.660 +
   3.661 +lemma simpfm_bound0: "bound0 p \<Longrightarrow> bound0 (simpfm p)"
   3.662 +proof(induct p rule: simpfm.induct)
   3.663 +  case (6 a) hence nb: "numbound0 a" by simp
   3.664 +  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
   3.665 +  thus ?case by (cases "simpnum a", auto simp add: Let_def)
   3.666 +next
   3.667 +  case (7 a) hence nb: "numbound0 a" by simp
   3.668 +  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
   3.669 +  thus ?case by (cases "simpnum a", auto simp add: Let_def)
   3.670 +next
   3.671 +  case (8 a) hence nb: "numbound0 a" by simp
   3.672 +  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
   3.673 +  thus ?case by (cases "simpnum a", auto simp add: Let_def)
   3.674 +next
   3.675 +  case (9 a) hence nb: "numbound0 a" by simp
   3.676 +  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
   3.677 +  thus ?case by (cases "simpnum a", auto simp add: Let_def)
   3.678 +next
   3.679 +  case (10 a) hence nb: "numbound0 a" by simp
   3.680 +  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
   3.681 +  thus ?case by (cases "simpnum a", auto simp add: Let_def)
   3.682 +next
   3.683 +  case (11 a) hence nb: "numbound0 a" by simp
   3.684 +  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
   3.685 +  thus ?case by (cases "simpnum a", auto simp add: Let_def)
   3.686 +next
   3.687 +  case (12 i a) hence nb: "numbound0 a" by simp
   3.688 +  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
   3.689 +  thus ?case by (cases "simpnum a", auto simp add: Let_def)
   3.690 +next
   3.691 +  case (13 i a) hence nb: "numbound0 a" by simp
   3.692 +  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
   3.693 +  thus ?case by (cases "simpnum a", auto simp add: Let_def)
   3.694 +qed(auto simp add: disj_def imp_def iff_def conj_def not_bn)
   3.695 +
   3.696 +lemma simpfm_qf: "qfree p \<Longrightarrow> qfree (simpfm p)"
   3.697 +by (induct p rule: simpfm.induct, auto simp add: disj_qf imp_qf iff_qf conj_qf not_qf Let_def)
   3.698 + (case_tac "simpnum a",auto)+
   3.699 +
   3.700 +  (* Generic quantifier elimination *)
   3.701 +consts qelim :: "fm \<Rightarrow> (fm \<Rightarrow> fm) \<Rightarrow> fm"
   3.702 +recdef qelim "measure fmsize"
   3.703 +  "qelim (E p) = (\<lambda> qe. DJ qe (qelim p qe))"
   3.704 +  "qelim (A p) = (\<lambda> qe. not (qe ((qelim (NOT p) qe))))"
   3.705 +  "qelim (NOT p) = (\<lambda> qe. not (qelim p qe))"
   3.706 +  "qelim (And p q) = (\<lambda> qe. conj (qelim p qe) (qelim q qe))" 
   3.707 +  "qelim (Or  p q) = (\<lambda> qe. disj (qelim p qe) (qelim q qe))" 
   3.708 +  "qelim (Imp p q) = (\<lambda> qe. imp (qelim p qe) (qelim q qe))"
   3.709 +  "qelim (Iff p q) = (\<lambda> qe. iff (qelim p qe) (qelim q qe))"
   3.710 +  "qelim p = (\<lambda> y. simpfm p)"
   3.711 +
   3.712 +(*function (sequential)
   3.713 +  qelim :: "(fm \<Rightarrow> fm) \<Rightarrow> fm \<Rightarrow> fm"
   3.714 +where
   3.715 +  "qelim qe (E p) = DJ qe (qelim qe p)"
   3.716 +  | "qelim qe (A p) = not (qe ((qelim qe (NOT p))))"
   3.717 +  | "qelim qe (NOT p) = not (qelim qe p)"
   3.718 +  | "qelim qe (And p q) = conj (qelim qe p) (qelim qe q)" 
   3.719 +  | "qelim qe (Or  p q) = disj (qelim qe p) (qelim qe q)" 
   3.720 +  | "qelim qe (Imp p q) = imp (qelim qe p) (qelim qe q)"
   3.721 +  | "qelim qe (Iff p q) = iff (qelim qe p) (qelim qe q)"
   3.722 +  | "qelim qe p = simpfm p"
   3.723 +by pat_completeness auto
   3.724 +termination by (relation "measure (fmsize o snd)") auto*)
   3.725 +
   3.726 +lemma qelim_ci:
   3.727 +  assumes qe_inv: "\<forall> bs p. qfree p \<longrightarrow> qfree (qe p) \<and> (Ifm bbs bs (qe p) = Ifm bbs bs (E p))"
   3.728 +  shows "\<And> bs. qfree (qelim p qe) \<and> (Ifm bbs bs (qelim p qe) = Ifm bbs bs p)"
   3.729 +using qe_inv DJ_qe[OF qe_inv] 
   3.730 +by(induct p rule: qelim.induct) 
   3.731 +(auto simp add: not disj conj iff imp not_qf disj_qf conj_qf imp_qf iff_qf 
   3.732 +  simpfm simpfm_qf simp del: simpfm.simps)
   3.733 +  (* Linearity for fm where Bound 0 ranges over \<int> *)
   3.734 +
   3.735 +fun
   3.736 +  zsplit0 :: "num \<Rightarrow> int \<times> num" (* splits the bounded from the unbounded part*)
   3.737 +where
   3.738 +  "zsplit0 (C c) = (0,C c)"
   3.739 +  | "zsplit0 (Bound n) = (if n=0 then (1, C 0) else (0,Bound n))"
   3.740 +  | "zsplit0 (CN n i a) = 
   3.741 +      (let (i',a') =  zsplit0 a 
   3.742 +       in if n=0 then (i+i', a') else (i',CN n i a'))"
   3.743 +  | "zsplit0 (Neg a) = (let (i',a') =  zsplit0 a in (-i', Neg a'))"
   3.744 +  | "zsplit0 (Add a b) = (let (ia,a') =  zsplit0 a ; 
   3.745 +                            (ib,b') =  zsplit0 b 
   3.746 +                            in (ia+ib, Add a' b'))"
   3.747 +  | "zsplit0 (Sub a b) = (let (ia,a') =  zsplit0 a ; 
   3.748 +                            (ib,b') =  zsplit0 b 
   3.749 +                            in (ia-ib, Sub a' b'))"
   3.750 +  | "zsplit0 (Mul i a) = (let (i',a') =  zsplit0 a in (i*i', Mul i a'))"
   3.751 +
   3.752 +lemma zsplit0_I:
   3.753 +  shows "\<And> n a. zsplit0 t = (n,a) \<Longrightarrow> (Inum ((x::int) #bs) (CN 0 n a) = Inum (x #bs) t) \<and> numbound0 a"
   3.754 +  (is "\<And> n a. ?S t = (n,a) \<Longrightarrow> (?I x (CN 0 n a) = ?I x t) \<and> ?N a")
   3.755 +proof(induct t rule: zsplit0.induct)
   3.756 +  case (1 c n a) thus ?case by auto 
   3.757 +next
   3.758 +  case (2 m n a) thus ?case by (cases "m=0") auto
   3.759 +next
   3.760 +  case (3 m i a n a')
   3.761 +  let ?j = "fst (zsplit0 a)"
   3.762 +  let ?b = "snd (zsplit0 a)"
   3.763 +  have abj: "zsplit0 a = (?j,?b)" by simp 
   3.764 +  {assume "m\<noteq>0" 
   3.765 +    with prems(1)[OF abj] prems(2) have ?case by (auto simp add: Let_def split_def)}
   3.766 +  moreover
   3.767 +  {assume m0: "m =0"
   3.768 +    from abj have th: "a'=?b \<and> n=i+?j" using prems 
   3.769 +      by (simp add: Let_def split_def)
   3.770 +    from abj prems  have th2: "(?I x (CN 0 ?j ?b) = ?I x a) \<and> ?N ?b" by blast
   3.771 +    from th have "?I x (CN 0 n a') = ?I x (CN 0 (i+?j) ?b)" by simp
   3.772 +    also from th2 have "\<dots> = ?I x (CN 0 i (CN 0 ?j ?b))" by (simp add: left_distrib)
   3.773 +  finally have "?I x (CN 0 n a') = ?I  x (CN 0 i a)" using th2 by simp
   3.774 +  with th2 th have ?case using m0 by blast} 
   3.775 +ultimately show ?case by blast
   3.776 +next
   3.777 +  case (4 t n a)
   3.778 +  let ?nt = "fst (zsplit0 t)"
   3.779 +  let ?at = "snd (zsplit0 t)"
   3.780 +  have abj: "zsplit0 t = (?nt,?at)" by simp hence th: "a=Neg ?at \<and> n=-?nt" using prems 
   3.781 +    by (simp add: Let_def split_def)
   3.782 +  from abj prems  have th2: "(?I x (CN 0 ?nt ?at) = ?I x t) \<and> ?N ?at" by blast
   3.783 +  from th2[simplified] th[simplified] show ?case by simp
   3.784 +next
   3.785 +  case (5 s t n a)
   3.786 +  let ?ns = "fst (zsplit0 s)"
   3.787 +  let ?as = "snd (zsplit0 s)"
   3.788 +  let ?nt = "fst (zsplit0 t)"
   3.789 +  let ?at = "snd (zsplit0 t)"
   3.790 +  have abjs: "zsplit0 s = (?ns,?as)" by simp 
   3.791 +  moreover have abjt:  "zsplit0 t = (?nt,?at)" by simp 
   3.792 +  ultimately have th: "a=Add ?as ?at \<and> n=?ns + ?nt" using prems 
   3.793 +    by (simp add: Let_def split_def)
   3.794 +  from abjs[symmetric] have bluddy: "\<exists> x y. (x,y) = zsplit0 s" by blast
   3.795 +  from prems have "(\<exists> x y. (x,y) = zsplit0 s) \<longrightarrow> (\<forall>xa xb. zsplit0 t = (xa, xb) \<longrightarrow> Inum (x # bs) (CN 0 xa xb) = Inum (x # bs) t \<and> numbound0 xb)" by auto
   3.796 +  with bluddy abjt have th3: "(?I x (CN 0 ?nt ?at) = ?I x t) \<and> ?N ?at" by blast
   3.797 +  from abjs prems  have th2: "(?I x (CN 0 ?ns ?as) = ?I x s) \<and> ?N ?as" by blast
   3.798 +  from th3[simplified] th2[simplified] th[simplified] show ?case 
   3.799 +    by (simp add: left_distrib)
   3.800 +next
   3.801 +  case (6 s t n a)
   3.802 +  let ?ns = "fst (zsplit0 s)"
   3.803 +  let ?as = "snd (zsplit0 s)"
   3.804 +  let ?nt = "fst (zsplit0 t)"
   3.805 +  let ?at = "snd (zsplit0 t)"
   3.806 +  have abjs: "zsplit0 s = (?ns,?as)" by simp 
   3.807 +  moreover have abjt:  "zsplit0 t = (?nt,?at)" by simp 
   3.808 +  ultimately have th: "a=Sub ?as ?at \<and> n=?ns - ?nt" using prems 
   3.809 +    by (simp add: Let_def split_def)
   3.810 +  from abjs[symmetric] have bluddy: "\<exists> x y. (x,y) = zsplit0 s" by blast
   3.811 +  from prems have "(\<exists> x y. (x,y) = zsplit0 s) \<longrightarrow> (\<forall>xa xb. zsplit0 t = (xa, xb) \<longrightarrow> Inum (x # bs) (CN 0 xa xb) = Inum (x # bs) t \<and> numbound0 xb)" by auto
   3.812 +  with bluddy abjt have th3: "(?I x (CN 0 ?nt ?at) = ?I x t) \<and> ?N ?at" by blast
   3.813 +  from abjs prems  have th2: "(?I x (CN 0 ?ns ?as) = ?I x s) \<and> ?N ?as" by blast
   3.814 +  from th3[simplified] th2[simplified] th[simplified] show ?case 
   3.815 +    by (simp add: left_diff_distrib)
   3.816 +next
   3.817 +  case (7 i t n a)
   3.818 +  let ?nt = "fst (zsplit0 t)"
   3.819 +  let ?at = "snd (zsplit0 t)"
   3.820 +  have abj: "zsplit0 t = (?nt,?at)" by simp hence th: "a=Mul i ?at \<and> n=i*?nt" using prems 
   3.821 +    by (simp add: Let_def split_def)
   3.822 +  from abj prems  have th2: "(?I x (CN 0 ?nt ?at) = ?I x t) \<and> ?N ?at" by blast
   3.823 +  hence " ?I x (Mul i t) = i * ?I x (CN 0 ?nt ?at)" by simp
   3.824 +  also have "\<dots> = ?I x (CN 0 (i*?nt) (Mul i ?at))" by (simp add: right_distrib)
   3.825 +  finally show ?case using th th2 by simp
   3.826 +qed
   3.827 +
   3.828 +consts
   3.829 +  iszlfm :: "fm \<Rightarrow> bool"   (* Linearity test for fm *)
   3.830 +recdef iszlfm "measure size"
   3.831 +  "iszlfm (And p q) = (iszlfm p \<and> iszlfm q)" 
   3.832 +  "iszlfm (Or p q) = (iszlfm p \<and> iszlfm q)" 
   3.833 +  "iszlfm (Eq  (CN 0 c e)) = (c>0 \<and> numbound0 e)"
   3.834 +  "iszlfm (NEq (CN 0 c e)) = (c>0 \<and> numbound0 e)"
   3.835 +  "iszlfm (Lt  (CN 0 c e)) = (c>0 \<and> numbound0 e)"
   3.836 +  "iszlfm (Le  (CN 0 c e)) = (c>0 \<and> numbound0 e)"
   3.837 +  "iszlfm (Gt  (CN 0 c e)) = (c>0 \<and> numbound0 e)"
   3.838 +  "iszlfm (Ge  (CN 0 c e)) = ( c>0 \<and> numbound0 e)"
   3.839 +  "iszlfm (Dvd i (CN 0 c e)) = 
   3.840 +                 (c>0 \<and> i>0 \<and> numbound0 e)"
   3.841 +  "iszlfm (NDvd i (CN 0 c e))= 
   3.842 +                 (c>0 \<and> i>0 \<and> numbound0 e)"
   3.843 +  "iszlfm p = (isatom p \<and> (bound0 p))"
   3.844 +
   3.845 +lemma zlin_qfree: "iszlfm p \<Longrightarrow> qfree p"
   3.846 +  by (induct p rule: iszlfm.induct) auto
   3.847 +
   3.848 +consts
   3.849 +  zlfm :: "fm \<Rightarrow> fm"       (* Linearity transformation for fm *)
   3.850 +recdef zlfm "measure fmsize"
   3.851 +  "zlfm (And p q) = And (zlfm p) (zlfm q)"
   3.852 +  "zlfm (Or p q) = Or (zlfm p) (zlfm q)"
   3.853 +  "zlfm (Imp p q) = Or (zlfm (NOT p)) (zlfm q)"
   3.854 +  "zlfm (Iff p q) = Or (And (zlfm p) (zlfm q)) (And (zlfm (NOT p)) (zlfm (NOT q)))"
   3.855 +  "zlfm (Lt a) = (let (c,r) = zsplit0 a in 
   3.856 +     if c=0 then Lt r else 
   3.857 +     if c>0 then (Lt (CN 0 c r)) else (Gt (CN 0 (- c) (Neg r))))"
   3.858 +  "zlfm (Le a) = (let (c,r) = zsplit0 a in 
   3.859 +     if c=0 then Le r else 
   3.860 +     if c>0 then (Le (CN 0 c r)) else (Ge (CN 0 (- c) (Neg r))))"
   3.861 +  "zlfm (Gt a) = (let (c,r) = zsplit0 a in 
   3.862 +     if c=0 then Gt r else 
   3.863 +     if c>0 then (Gt (CN 0 c r)) else (Lt (CN 0 (- c) (Neg r))))"
   3.864 +  "zlfm (Ge a) = (let (c,r) = zsplit0 a in 
   3.865 +     if c=0 then Ge r else 
   3.866 +     if c>0 then (Ge (CN 0 c r)) else (Le (CN 0 (- c) (Neg r))))"
   3.867 +  "zlfm (Eq a) = (let (c,r) = zsplit0 a in 
   3.868 +     if c=0 then Eq r else 
   3.869 +     if c>0 then (Eq (CN 0 c r)) else (Eq (CN 0 (- c) (Neg r))))"
   3.870 +  "zlfm (NEq a) = (let (c,r) = zsplit0 a in 
   3.871 +     if c=0 then NEq r else 
   3.872 +     if c>0 then (NEq (CN 0 c r)) else (NEq (CN 0 (- c) (Neg r))))"
   3.873 +  "zlfm (Dvd i a) = (if i=0 then zlfm (Eq a) 
   3.874 +        else (let (c,r) = zsplit0 a in 
   3.875 +              if c=0 then (Dvd (abs i) r) else 
   3.876 +      if c>0 then (Dvd (abs i) (CN 0 c r))
   3.877 +      else (Dvd (abs i) (CN 0 (- c) (Neg r)))))"
   3.878 +  "zlfm (NDvd i a) = (if i=0 then zlfm (NEq a) 
   3.879 +        else (let (c,r) = zsplit0 a in 
   3.880 +              if c=0 then (NDvd (abs i) r) else 
   3.881 +      if c>0 then (NDvd (abs i) (CN 0 c r))
   3.882 +      else (NDvd (abs i) (CN 0 (- c) (Neg r)))))"
   3.883 +  "zlfm (NOT (And p q)) = Or (zlfm (NOT p)) (zlfm (NOT q))"
   3.884 +  "zlfm (NOT (Or p q)) = And (zlfm (NOT p)) (zlfm (NOT q))"
   3.885 +  "zlfm (NOT (Imp p q)) = And (zlfm p) (zlfm (NOT q))"
   3.886 +  "zlfm (NOT (Iff p q)) = Or (And(zlfm p) (zlfm(NOT q))) (And (zlfm(NOT p)) (zlfm q))"
   3.887 +  "zlfm (NOT (NOT p)) = zlfm p"
   3.888 +  "zlfm (NOT T) = F"
   3.889 +  "zlfm (NOT F) = T"
   3.890 +  "zlfm (NOT (Lt a)) = zlfm (Ge a)"
   3.891 +  "zlfm (NOT (Le a)) = zlfm (Gt a)"
   3.892 +  "zlfm (NOT (Gt a)) = zlfm (Le a)"
   3.893 +  "zlfm (NOT (Ge a)) = zlfm (Lt a)"
   3.894 +  "zlfm (NOT (Eq a)) = zlfm (NEq a)"
   3.895 +  "zlfm (NOT (NEq a)) = zlfm (Eq a)"
   3.896 +  "zlfm (NOT (Dvd i a)) = zlfm (NDvd i a)"
   3.897 +  "zlfm (NOT (NDvd i a)) = zlfm (Dvd i a)"
   3.898 +  "zlfm (NOT (Closed P)) = NClosed P"
   3.899 +  "zlfm (NOT (NClosed P)) = Closed P"
   3.900 +  "zlfm p = p" (hints simp add: fmsize_pos)
   3.901 +
   3.902 +lemma zlfm_I:
   3.903 +  assumes qfp: "qfree p"
   3.904 +  shows "(Ifm bbs (i#bs) (zlfm p) = Ifm bbs (i# bs) p) \<and> iszlfm (zlfm p)"
   3.905 +  (is "(?I (?l p) = ?I p) \<and> ?L (?l p)")
   3.906 +using qfp
   3.907 +proof(induct p rule: zlfm.induct)
   3.908 +  case (5 a) 
   3.909 +  let ?c = "fst (zsplit0 a)"
   3.910 +  let ?r = "snd (zsplit0 a)"
   3.911 +  have spl: "zsplit0 a = (?c,?r)" by simp
   3.912 +  from zsplit0_I[OF spl, where x="i" and bs="bs"] 
   3.913 +  have Ia:"Inum (i # bs) a = Inum (i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto 
   3.914 +  let ?N = "\<lambda> t. Inum (i#bs) t"
   3.915 +  from prems Ia nb  show ?case 
   3.916 +    apply (auto simp add: Let_def split_def algebra_simps) 
   3.917 +    apply (cases "?r",auto)
   3.918 +    apply (case_tac nat, auto)
   3.919 +    done
   3.920 +next
   3.921 +  case (6 a)  
   3.922 +  let ?c = "fst (zsplit0 a)"
   3.923 +  let ?r = "snd (zsplit0 a)"
   3.924 +  have spl: "zsplit0 a = (?c,?r)" by simp
   3.925 +  from zsplit0_I[OF spl, where x="i" and bs="bs"] 
   3.926 +  have Ia:"Inum (i # bs) a = Inum (i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto 
   3.927 +  let ?N = "\<lambda> t. Inum (i#bs) t"
   3.928 +  from prems Ia nb  show ?case 
   3.929 +    apply (auto simp add: Let_def split_def algebra_simps) 
   3.930 +    apply (cases "?r",auto)
   3.931 +    apply (case_tac nat, auto)
   3.932 +    done
   3.933 +next
   3.934 +  case (7 a)  
   3.935 +  let ?c = "fst (zsplit0 a)"
   3.936 +  let ?r = "snd (zsplit0 a)"
   3.937 +  have spl: "zsplit0 a = (?c,?r)" by simp
   3.938 +  from zsplit0_I[OF spl, where x="i" and bs="bs"] 
   3.939 +  have Ia:"Inum (i # bs) a = Inum (i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto 
   3.940 +  let ?N = "\<lambda> t. Inum (i#bs) t"
   3.941 +  from prems Ia nb  show ?case 
   3.942 +    apply (auto simp add: Let_def split_def algebra_simps) 
   3.943 +    apply (cases "?r",auto)
   3.944 +    apply (case_tac nat, auto)
   3.945 +    done
   3.946 +next
   3.947 +  case (8 a)  
   3.948 +  let ?c = "fst (zsplit0 a)"
   3.949 +  let ?r = "snd (zsplit0 a)"
   3.950 +  have spl: "zsplit0 a = (?c,?r)" by simp
   3.951 +  from zsplit0_I[OF spl, where x="i" and bs="bs"] 
   3.952 +  have Ia:"Inum (i # bs) a = Inum (i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto 
   3.953 +  let ?N = "\<lambda> t. Inum (i#bs) t"
   3.954 +  from prems Ia nb  show ?case 
   3.955 +    apply (auto simp add: Let_def split_def algebra_simps) 
   3.956 +    apply (cases "?r",auto)
   3.957 +    apply (case_tac nat, auto)
   3.958 +    done
   3.959 +next
   3.960 +  case (9 a)  
   3.961 +  let ?c = "fst (zsplit0 a)"
   3.962 +  let ?r = "snd (zsplit0 a)"
   3.963 +  have spl: "zsplit0 a = (?c,?r)" by simp
   3.964 +  from zsplit0_I[OF spl, where x="i" and bs="bs"] 
   3.965 +  have Ia:"Inum (i # bs) a = Inum (i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto 
   3.966 +  let ?N = "\<lambda> t. Inum (i#bs) t"
   3.967 +  from prems Ia nb  show ?case 
   3.968 +    apply (auto simp add: Let_def split_def algebra_simps) 
   3.969 +    apply (cases "?r",auto)
   3.970 +    apply (case_tac nat, auto)
   3.971 +    done
   3.972 +next
   3.973 +  case (10 a)  
   3.974 +  let ?c = "fst (zsplit0 a)"
   3.975 +  let ?r = "snd (zsplit0 a)"
   3.976 +  have spl: "zsplit0 a = (?c,?r)" by simp
   3.977 +  from zsplit0_I[OF spl, where x="i" and bs="bs"] 
   3.978 +  have Ia:"Inum (i # bs) a = Inum (i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto 
   3.979 +  let ?N = "\<lambda> t. Inum (i#bs) t"
   3.980 +  from prems Ia nb  show ?case 
   3.981 +    apply (auto simp add: Let_def split_def algebra_simps) 
   3.982 +    apply (cases "?r",auto)
   3.983 +    apply (case_tac nat, auto)
   3.984 +    done
   3.985 +next
   3.986 +  case (11 j a)  
   3.987 +  let ?c = "fst (zsplit0 a)"
   3.988 +  let ?r = "snd (zsplit0 a)"
   3.989 +  have spl: "zsplit0 a = (?c,?r)" by simp
   3.990 +  from zsplit0_I[OF spl, where x="i" and bs="bs"] 
   3.991 +  have Ia:"Inum (i # bs) a = Inum (i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto 
   3.992 +  let ?N = "\<lambda> t. Inum (i#bs) t"
   3.993 +  have "j=0 \<or> (j\<noteq>0 \<and> ?c = 0) \<or> (j\<noteq>0 \<and> ?c >0) \<or> (j\<noteq> 0 \<and> ?c<0)" by arith
   3.994 +  moreover
   3.995 +  {assume "j=0" hence z: "zlfm (Dvd j a) = (zlfm (Eq a))" by (simp add: Let_def) 
   3.996 +    hence ?case using prems by (simp del: zlfm.simps add: zdvd_0_left)}
   3.997 +  moreover
   3.998 +  {assume "?c=0" and "j\<noteq>0" hence ?case 
   3.999 +      using zsplit0_I[OF spl, where x="i" and bs="bs"]
  3.1000 +    apply (auto simp add: Let_def split_def algebra_simps) 
  3.1001 +    apply (cases "?r",auto)
  3.1002 +    apply (case_tac nat, auto)
  3.1003 +    done}
  3.1004 +  moreover
  3.1005 +  {assume cp: "?c > 0" and jnz: "j\<noteq>0" hence l: "?L (?l (Dvd j a))" 
  3.1006 +      by (simp add: nb Let_def split_def)
  3.1007 +    hence ?case using Ia cp jnz by (simp add: Let_def split_def)}
  3.1008 +  moreover
  3.1009 +  {assume cn: "?c < 0" and jnz: "j\<noteq>0" hence l: "?L (?l (Dvd j a))" 
  3.1010 +      by (simp add: nb Let_def split_def)
  3.1011 +    hence ?case using Ia cn jnz zdvd_zminus_iff[where m="abs j" and n="?c*i + ?N ?r" ]
  3.1012 +      by (simp add: Let_def split_def) }
  3.1013 +  ultimately show ?case by blast
  3.1014 +next
  3.1015 +  case (12 j a) 
  3.1016 +  let ?c = "fst (zsplit0 a)"
  3.1017 +  let ?r = "snd (zsplit0 a)"
  3.1018 +  have spl: "zsplit0 a = (?c,?r)" by simp
  3.1019 +  from zsplit0_I[OF spl, where x="i" and bs="bs"] 
  3.1020 +  have Ia:"Inum (i # bs) a = Inum (i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto 
  3.1021 +  let ?N = "\<lambda> t. Inum (i#bs) t"
  3.1022 +  have "j=0 \<or> (j\<noteq>0 \<and> ?c = 0) \<or> (j\<noteq>0 \<and> ?c >0) \<or> (j\<noteq> 0 \<and> ?c<0)" by arith
  3.1023 +  moreover
  3.1024 +  {assume "j=0" hence z: "zlfm (NDvd j a) = (zlfm (NEq a))" by (simp add: Let_def) 
  3.1025 +    hence ?case using prems by (simp del: zlfm.simps add: zdvd_0_left)}
  3.1026 +  moreover
  3.1027 +  {assume "?c=0" and "j\<noteq>0" hence ?case 
  3.1028 +      using zsplit0_I[OF spl, where x="i" and bs="bs"]
  3.1029 +    apply (auto simp add: Let_def split_def algebra_simps) 
  3.1030 +    apply (cases "?r",auto)
  3.1031 +    apply (case_tac nat, auto)
  3.1032 +    done}
  3.1033 +  moreover
  3.1034 +  {assume cp: "?c > 0" and jnz: "j\<noteq>0" hence l: "?L (?l (Dvd j a))" 
  3.1035 +      by (simp add: nb Let_def split_def)
  3.1036 +    hence ?case using Ia cp jnz by (simp add: Let_def split_def) }
  3.1037 +  moreover
  3.1038 +  {assume cn: "?c < 0" and jnz: "j\<noteq>0" hence l: "?L (?l (Dvd j a))" 
  3.1039 +      by (simp add: nb Let_def split_def)
  3.1040 +    hence ?case using Ia cn jnz zdvd_zminus_iff[where m="abs j" and n="?c*i + ?N ?r" ]
  3.1041 +      by (simp add: Let_def split_def)}
  3.1042 +  ultimately show ?case by blast
  3.1043 +qed auto
  3.1044 +
  3.1045 +consts 
  3.1046 +  plusinf:: "fm \<Rightarrow> fm" (* Virtual substitution of +\<infinity>*)
  3.1047 +  minusinf:: "fm \<Rightarrow> fm" (* Virtual substitution of -\<infinity>*)
  3.1048 +  \<delta> :: "fm \<Rightarrow> int" (* Compute lcm {d| N\<^isup>?\<^isup> Dvd c*x+t \<in> p}*)
  3.1049 +  d\<delta> :: "fm \<Rightarrow> int \<Rightarrow> bool" (* checks if a given l divides all the ds above*)
  3.1050 +
  3.1051 +recdef minusinf "measure size"
  3.1052 +  "minusinf (And p q) = And (minusinf p) (minusinf q)" 
  3.1053 +  "minusinf (Or p q) = Or (minusinf p) (minusinf q)" 
  3.1054 +  "minusinf (Eq  (CN 0 c e)) = F"
  3.1055 +  "minusinf (NEq (CN 0 c e)) = T"
  3.1056 +  "minusinf (Lt  (CN 0 c e)) = T"
  3.1057 +  "minusinf (Le  (CN 0 c e)) = T"
  3.1058 +  "minusinf (Gt  (CN 0 c e)) = F"
  3.1059 +  "minusinf (Ge  (CN 0 c e)) = F"
  3.1060 +  "minusinf p = p"
  3.1061 +
  3.1062 +lemma minusinf_qfree: "qfree p \<Longrightarrow> qfree (minusinf p)"
  3.1063 +  by (induct p rule: minusinf.induct, auto)
  3.1064 +
  3.1065 +recdef plusinf "measure size"
  3.1066 +  "plusinf (And p q) = And (plusinf p) (plusinf q)" 
  3.1067 +  "plusinf (Or p q) = Or (plusinf p) (plusinf q)" 
  3.1068 +  "plusinf (Eq  (CN 0 c e)) = F"
  3.1069 +  "plusinf (NEq (CN 0 c e)) = T"
  3.1070 +  "plusinf (Lt  (CN 0 c e)) = F"
  3.1071 +  "plusinf (Le  (CN 0 c e)) = F"
  3.1072 +  "plusinf (Gt  (CN 0 c e)) = T"
  3.1073 +  "plusinf (Ge  (CN 0 c e)) = T"
  3.1074 +  "plusinf p = p"
  3.1075 +
  3.1076 +recdef \<delta> "measure size"
  3.1077 +  "\<delta> (And p q) = zlcm (\<delta> p) (\<delta> q)" 
  3.1078 +  "\<delta> (Or p q) = zlcm (\<delta> p) (\<delta> q)" 
  3.1079 +  "\<delta> (Dvd i (CN 0 c e)) = i"
  3.1080 +  "\<delta> (NDvd i (CN 0 c e)) = i"
  3.1081 +  "\<delta> p = 1"
  3.1082 +
  3.1083 +recdef d\<delta> "measure size"
  3.1084 +  "d\<delta> (And p q) = (\<lambda> d. d\<delta> p d \<and> d\<delta> q d)" 
  3.1085 +  "d\<delta> (Or p q) = (\<lambda> d. d\<delta> p d \<and> d\<delta> q d)" 
  3.1086 +  "d\<delta> (Dvd i (CN 0 c e)) = (\<lambda> d. i dvd d)"
  3.1087 +  "d\<delta> (NDvd i (CN 0 c e)) = (\<lambda> d. i dvd d)"
  3.1088 +  "d\<delta> p = (\<lambda> d. True)"
  3.1089 +
  3.1090 +lemma delta_mono: 
  3.1091 +  assumes lin: "iszlfm p"
  3.1092 +  and d: "d dvd d'"
  3.1093 +  and ad: "d\<delta> p d"
  3.1094 +  shows "d\<delta> p d'"
  3.1095 +  using lin ad d
  3.1096 +proof(induct p rule: iszlfm.induct)
  3.1097 +  case (9 i c e)  thus ?case using d
  3.1098 +    by (simp add: zdvd_trans[where m="i" and n="d" and k="d'"])
  3.1099 +next
  3.1100 +  case (10 i c e) thus ?case using d
  3.1101 +    by (simp add: zdvd_trans[where m="i" and n="d" and k="d'"])
  3.1102 +qed simp_all
  3.1103 +
  3.1104 +lemma \<delta> : assumes lin:"iszlfm p"
  3.1105 +  shows "d\<delta> p (\<delta> p) \<and> \<delta> p >0"
  3.1106 +using lin
  3.1107 +proof (induct p rule: iszlfm.induct)
  3.1108 +  case (1 p q) 
  3.1109 +  let ?d = "\<delta> (And p q)"
  3.1110 +  from prems zlcm_pos have dp: "?d >0" by simp
  3.1111 +  have d1: "\<delta> p dvd \<delta> (And p q)" using prems by simp
  3.1112 +  hence th: "d\<delta> p ?d" using delta_mono prems(3-4) by(simp del:dvd_zlcm_self1)
  3.1113 +  have "\<delta> q dvd \<delta> (And p q)" using prems by simp
  3.1114 +  hence th': "d\<delta> q ?d" using delta_mono prems by(simp del:dvd_zlcm_self2)
  3.1115 +  from th th' dp show ?case by simp
  3.1116 +next
  3.1117 +  case (2 p q)  
  3.1118 +  let ?d = "\<delta> (And p q)"
  3.1119 +  from prems zlcm_pos have dp: "?d >0" by simp
  3.1120 +  have "\<delta> p dvd \<delta> (And p q)" using prems by simp
  3.1121 +  hence th: "d\<delta> p ?d" using delta_mono prems by(simp del:dvd_zlcm_self1)
  3.1122 +  have "\<delta> q dvd \<delta> (And p q)" using prems by simp
  3.1123 +  hence th': "d\<delta> q ?d" using delta_mono prems by(simp del:dvd_zlcm_self2)
  3.1124 +  from th th' dp show ?case by simp
  3.1125 +qed simp_all
  3.1126 +
  3.1127 +
  3.1128 +consts 
  3.1129 +  a\<beta> :: "fm \<Rightarrow> int \<Rightarrow> fm" (* adjusts the coeffitients of a formula *)
  3.1130 +  d\<beta> :: "fm \<Rightarrow> int \<Rightarrow> bool" (* tests if all coeffs c of c divide a given l*)
  3.1131 +  \<zeta>  :: "fm \<Rightarrow> int" (* computes the lcm of all coefficients of x*)
  3.1132 +  \<beta> :: "fm \<Rightarrow> num list"
  3.1133 +  \<alpha> :: "fm \<Rightarrow> num list"
  3.1134 +
  3.1135 +recdef a\<beta> "measure size"
  3.1136 +  "a\<beta> (And p q) = (\<lambda> k. And (a\<beta> p k) (a\<beta> q k))" 
  3.1137 +  "a\<beta> (Or p q) = (\<lambda> k. Or (a\<beta> p k) (a\<beta> q k))" 
  3.1138 +  "a\<beta> (Eq  (CN 0 c e)) = (\<lambda> k. Eq (CN 0 1 (Mul (k div c) e)))"
  3.1139 +  "a\<beta> (NEq (CN 0 c e)) = (\<lambda> k. NEq (CN 0 1 (Mul (k div c) e)))"
  3.1140 +  "a\<beta> (Lt  (CN 0 c e)) = (\<lambda> k. Lt (CN 0 1 (Mul (k div c) e)))"
  3.1141 +  "a\<beta> (Le  (CN 0 c e)) = (\<lambda> k. Le (CN 0 1 (Mul (k div c) e)))"
  3.1142 +  "a\<beta> (Gt  (CN 0 c e)) = (\<lambda> k. Gt (CN 0 1 (Mul (k div c) e)))"
  3.1143 +  "a\<beta> (Ge  (CN 0 c e)) = (\<lambda> k. Ge (CN 0 1 (Mul (k div c) e)))"
  3.1144 +  "a\<beta> (Dvd i (CN 0 c e)) =(\<lambda> k. Dvd ((k div c)*i) (CN 0 1 (Mul (k div c) e)))"
  3.1145 +  "a\<beta> (NDvd i (CN 0 c e))=(\<lambda> k. NDvd ((k div c)*i) (CN 0 1 (Mul (k div c) e)))"
  3.1146 +  "a\<beta> p = (\<lambda> k. p)"
  3.1147 +
  3.1148 +recdef d\<beta> "measure size"
  3.1149 +  "d\<beta> (And p q) = (\<lambda> k. (d\<beta> p k) \<and> (d\<beta> q k))" 
  3.1150 +  "d\<beta> (Or p q) = (\<lambda> k. (d\<beta> p k) \<and> (d\<beta> q k))" 
  3.1151 +  "d\<beta> (Eq  (CN 0 c e)) = (\<lambda> k. c dvd k)"
  3.1152 +  "d\<beta> (NEq (CN 0 c e)) = (\<lambda> k. c dvd k)"
  3.1153 +  "d\<beta> (Lt  (CN 0 c e)) = (\<lambda> k. c dvd k)"
  3.1154 +  "d\<beta> (Le  (CN 0 c e)) = (\<lambda> k. c dvd k)"
  3.1155 +  "d\<beta> (Gt  (CN 0 c e)) = (\<lambda> k. c dvd k)"
  3.1156 +  "d\<beta> (Ge  (CN 0 c e)) = (\<lambda> k. c dvd k)"
  3.1157 +  "d\<beta> (Dvd i (CN 0 c e)) =(\<lambda> k. c dvd k)"
  3.1158 +  "d\<beta> (NDvd i (CN 0 c e))=(\<lambda> k. c dvd k)"
  3.1159 +  "d\<beta> p = (\<lambda> k. True)"
  3.1160 +
  3.1161 +recdef \<zeta> "measure size"
  3.1162 +  "\<zeta> (And p q) = zlcm (\<zeta> p) (\<zeta> q)" 
  3.1163 +  "\<zeta> (Or p q) = zlcm (\<zeta> p) (\<zeta> q)" 
  3.1164 +  "\<zeta> (Eq  (CN 0 c e)) = c"
  3.1165 +  "\<zeta> (NEq (CN 0 c e)) = c"
  3.1166 +  "\<zeta> (Lt  (CN 0 c e)) = c"
  3.1167 +  "\<zeta> (Le  (CN 0 c e)) = c"
  3.1168 +  "\<zeta> (Gt  (CN 0 c e)) = c"
  3.1169 +  "\<zeta> (Ge  (CN 0 c e)) = c"
  3.1170 +  "\<zeta> (Dvd i (CN 0 c e)) = c"
  3.1171 +  "\<zeta> (NDvd i (CN 0 c e))= c"
  3.1172 +  "\<zeta> p = 1"
  3.1173 +
  3.1174 +recdef \<beta> "measure size"
  3.1175 +  "\<beta> (And p q) = (\<beta> p @ \<beta> q)" 
  3.1176 +  "\<beta> (Or p q) = (\<beta> p @ \<beta> q)" 
  3.1177 +  "\<beta> (Eq  (CN 0 c e)) = [Sub (C -1) e]"
  3.1178 +  "\<beta> (NEq (CN 0 c e)) = [Neg e]"
  3.1179 +  "\<beta> (Lt  (CN 0 c e)) = []"
  3.1180 +  "\<beta> (Le  (CN 0 c e)) = []"
  3.1181 +  "\<beta> (Gt  (CN 0 c e)) = [Neg e]"
  3.1182 +  "\<beta> (Ge  (CN 0 c e)) = [Sub (C -1) e]"
  3.1183 +  "\<beta> p = []"
  3.1184 +
  3.1185 +recdef \<alpha> "measure size"
  3.1186 +  "\<alpha> (And p q) = (\<alpha> p @ \<alpha> q)" 
  3.1187 +  "\<alpha> (Or p q) = (\<alpha> p @ \<alpha> q)" 
  3.1188 +  "\<alpha> (Eq  (CN 0 c e)) = [Add (C -1) e]"
  3.1189 +  "\<alpha> (NEq (CN 0 c e)) = [e]"
  3.1190 +  "\<alpha> (Lt  (CN 0 c e)) = [e]"
  3.1191 +  "\<alpha> (Le  (CN 0 c e)) = [Add (C -1) e]"
  3.1192 +  "\<alpha> (Gt  (CN 0 c e)) = []"
  3.1193 +  "\<alpha> (Ge  (CN 0 c e)) = []"
  3.1194 +  "\<alpha> p = []"
  3.1195 +consts mirror :: "fm \<Rightarrow> fm"
  3.1196 +recdef mirror "measure size"
  3.1197 +  "mirror (And p q) = And (mirror p) (mirror q)" 
  3.1198 +  "mirror (Or p q) = Or (mirror p) (mirror q)" 
  3.1199 +  "mirror (Eq  (CN 0 c e)) = Eq (CN 0 c (Neg e))"
  3.1200 +  "mirror (NEq (CN 0 c e)) = NEq (CN 0 c (Neg e))"
  3.1201 +  "mirror (Lt  (CN 0 c e)) = Gt (CN 0 c (Neg e))"
  3.1202 +  "mirror (Le  (CN 0 c e)) = Ge (CN 0 c (Neg e))"
  3.1203 +  "mirror (Gt  (CN 0 c e)) = Lt (CN 0 c (Neg e))"
  3.1204 +  "mirror (Ge  (CN 0 c e)) = Le (CN 0 c (Neg e))"
  3.1205 +  "mirror (Dvd i (CN 0 c e)) = Dvd i (CN 0 c (Neg e))"
  3.1206 +  "mirror (NDvd i (CN 0 c e)) = NDvd i (CN 0 c (Neg e))"
  3.1207 +  "mirror p = p"
  3.1208 +    (* Lemmas for the correctness of \<sigma>\<rho> *)
  3.1209 +lemma dvd1_eq1: "x >0 \<Longrightarrow> (x::int) dvd 1 = (x = 1)"
  3.1210 +by simp
  3.1211 +
  3.1212 +lemma minusinf_inf:
  3.1213 +  assumes linp: "iszlfm p"
  3.1214 +  and u: "d\<beta> p 1"
  3.1215 +  shows "\<exists> (z::int). \<forall> x < z. Ifm bbs (x#bs) (minusinf p) = Ifm bbs (x#bs) p"
  3.1216 +  (is "?P p" is "\<exists> (z::int). \<forall> x < z. ?I x (?M p) = ?I x p")
  3.1217 +using linp u
  3.1218 +proof (induct p rule: minusinf.induct)
  3.1219 +  case (1 p q) thus ?case 
  3.1220 +    by auto (rule_tac x="min z za" in exI,simp)
  3.1221 +next
  3.1222 +  case (2 p q) thus ?case 
  3.1223 +    by auto (rule_tac x="min z za" in exI,simp)
  3.1224 +next
  3.1225 +  case (3 c e) hence c1: "c=1" and nb: "numbound0 e" by simp+
  3.1226 +  fix a
  3.1227 +  from 3 have "\<forall> x<(- Inum (a#bs) e). c*x + Inum (x#bs) e \<noteq> 0"
  3.1228 +  proof(clarsimp)
  3.1229 +    fix x assume "x < (- Inum (a#bs) e)" and"x + Inum (x#bs) e = 0"
  3.1230 +    with numbound0_I[OF nb, where bs="bs" and b="a" and b'="x"]
  3.1231 +    show "False" by simp
  3.1232 +  qed
  3.1233 +  thus ?case by auto
  3.1234 +next
  3.1235 +  case (4 c e) hence c1: "c=1" and nb: "numbound0 e" by simp+
  3.1236 +  fix a
  3.1237 +  from 4 have "\<forall> x<(- Inum (a#bs) e). c*x + Inum (x#bs) e \<noteq> 0"
  3.1238 +  proof(clarsimp)
  3.1239 +    fix x assume "x < (- Inum (a#bs) e)" and"x + Inum (x#bs) e = 0"
  3.1240 +    with numbound0_I[OF nb, where bs="bs" and b="a" and b'="x"]
  3.1241 +    show "False" by simp
  3.1242 +  qed
  3.1243 +  thus ?case by auto
  3.1244 +next
  3.1245 +  case (5 c e) hence c1: "c=1" and nb: "numbound0 e" by simp+
  3.1246 +  fix a
  3.1247 +  from 5 have "\<forall> x<(- Inum (a#bs) e). c*x + Inum (x#bs) e < 0"
  3.1248 +  proof(clarsimp)
  3.1249 +    fix x assume "x < (- Inum (a#bs) e)" 
  3.1250 +    with numbound0_I[OF nb, where bs="bs" and b="a" and b'="x"]
  3.1251 +    show "x + Inum (x#bs) e < 0" by simp
  3.1252 +  qed
  3.1253 +  thus ?case by auto
  3.1254 +next
  3.1255 +  case (6 c e) hence c1: "c=1" and nb: "numbound0 e" by simp+
  3.1256 +  fix a
  3.1257 +  from 6 have "\<forall> x<(- Inum (a#bs) e). c*x + Inum (x#bs) e \<le> 0"
  3.1258 +  proof(clarsimp)
  3.1259 +    fix x assume "x < (- Inum (a#bs) e)" 
  3.1260 +    with numbound0_I[OF nb, where bs="bs" and b="a" and b'="x"]
  3.1261 +    show "x + Inum (x#bs) e \<le> 0" by simp
  3.1262 +  qed
  3.1263 +  thus ?case by auto
  3.1264 +next
  3.1265 +  case (7 c e) hence c1: "c=1" and nb: "numbound0 e" by simp+
  3.1266 +  fix a
  3.1267 +  from 7 have "\<forall> x<(- Inum (a#bs) e). \<not> (c*x + Inum (x#bs) e > 0)"
  3.1268 +  proof(clarsimp)
  3.1269 +    fix x assume "x < (- Inum (a#bs) e)" and"x + Inum (x#bs) e > 0"
  3.1270 +    with numbound0_I[OF nb, where bs="bs" and b="a" and b'="x"]
  3.1271 +    show "False" by simp
  3.1272 +  qed
  3.1273 +  thus ?case by auto
  3.1274 +next
  3.1275 +  case (8 c e) hence c1: "c=1" and nb: "numbound0 e" by simp+
  3.1276 +  fix a
  3.1277 +  from 8 have "\<forall> x<(- Inum (a#bs) e). \<not> (c*x + Inum (x#bs) e \<ge> 0)"
  3.1278 +  proof(clarsimp)
  3.1279 +    fix x assume "x < (- Inum (a#bs) e)" and"x + Inum (x#bs) e \<ge> 0"
  3.1280 +    with numbound0_I[OF nb, where bs="bs" and b="a" and b'="x"]
  3.1281 +    show "False" by simp
  3.1282 +  qed
  3.1283 +  thus ?case by auto
  3.1284 +qed auto
  3.1285 +
  3.1286 +lemma minusinf_repeats:
  3.1287 +  assumes d: "d\<delta> p d" and linp: "iszlfm p"
  3.1288 +  shows "Ifm bbs ((x - k*d)#bs) (minusinf p) = Ifm bbs (x #bs) (minusinf p)"
  3.1289 +using linp d
  3.1290 +proof(induct p rule: iszlfm.induct) 
  3.1291 +  case (9 i c e) hence nbe: "numbound0 e"  and id: "i dvd d" by simp+
  3.1292 +    hence "\<exists> k. d=i*k" by (simp add: dvd_def)
  3.1293 +    then obtain "di" where di_def: "d=i*di" by blast
  3.1294 +    show ?case 
  3.1295 +    proof(simp add: numbound0_I[OF nbe,where bs="bs" and b="x - k * d" and b'="x"] right_diff_distrib, rule iffI)
  3.1296 +      assume 
  3.1297 +	"i dvd c * x - c*(k*d) + Inum (x # bs) e"
  3.1298 +      (is "?ri dvd ?rc*?rx - ?rc*(?rk*?rd) + ?I x e" is "?ri dvd ?rt")
  3.1299 +      hence "\<exists> (l::int). ?rt = i * l" by (simp add: dvd_def)
  3.1300 +      hence "\<exists> (l::int). c*x+ ?I x e = i*l+c*(k * i*di)" 
  3.1301 +	by (simp add: algebra_simps di_def)
  3.1302 +      hence "\<exists> (l::int). c*x+ ?I x e = i*(l + c*k*di)"
  3.1303 +	by (simp add: algebra_simps)
  3.1304 +      hence "\<exists> (l::int). c*x+ ?I x e = i*l" by blast
  3.1305 +      thus "i dvd c*x + Inum (x # bs) e" by (simp add: dvd_def) 
  3.1306 +    next
  3.1307 +      assume 
  3.1308 +	"i dvd c*x + Inum (x # bs) e" (is "?ri dvd ?rc*?rx+?e")
  3.1309 +      hence "\<exists> (l::int). c*x+?e = i*l" by (simp add: dvd_def)
  3.1310 +      hence "\<exists> (l::int). c*x - c*(k*d) +?e = i*l - c*(k*d)" by simp
  3.1311 +      hence "\<exists> (l::int). c*x - c*(k*d) +?e = i*l - c*(k*i*di)" by (simp add: di_def)
  3.1312 +      hence "\<exists> (l::int). c*x - c*(k*d) +?e = i*((l - c*k*di))" by (simp add: algebra_simps)
  3.1313 +      hence "\<exists> (l::int). c*x - c * (k*d) +?e = i*l"
  3.1314 +	by blast
  3.1315 +      thus "i dvd c*x - c*(k*d) + Inum (x # bs) e" by (simp add: dvd_def)
  3.1316 +    qed
  3.1317 +next
  3.1318 +  case (10 i c e)  hence nbe: "numbound0 e"  and id: "i dvd d" by simp+
  3.1319 +    hence "\<exists> k. d=i*k" by (simp add: dvd_def)
  3.1320 +    then obtain "di" where di_def: "d=i*di" by blast
  3.1321 +    show ?case 
  3.1322 +    proof(simp add: numbound0_I[OF nbe,where bs="bs" and b="x - k * d" and b'="x"] right_diff_distrib, rule iffI)
  3.1323 +      assume 
  3.1324 +	"i dvd c * x - c*(k*d) + Inum (x # bs) e"
  3.1325 +      (is "?ri dvd ?rc*?rx - ?rc*(?rk*?rd) + ?I x e" is "?ri dvd ?rt")
  3.1326 +      hence "\<exists> (l::int). ?rt = i * l" by (simp add: dvd_def)
  3.1327 +      hence "\<exists> (l::int). c*x+ ?I x e = i*l+c*(k * i*di)" 
  3.1328 +	by (simp add: algebra_simps di_def)
  3.1329 +      hence "\<exists> (l::int). c*x+ ?I x e = i*(l + c*k*di)"
  3.1330 +	by (simp add: algebra_simps)
  3.1331 +      hence "\<exists> (l::int). c*x+ ?I x e = i*l" by blast
  3.1332 +      thus "i dvd c*x + Inum (x # bs) e" by (simp add: dvd_def) 
  3.1333 +    next
  3.1334 +      assume 
  3.1335 +	"i dvd c*x + Inum (x # bs) e" (is "?ri dvd ?rc*?rx+?e")
  3.1336 +      hence "\<exists> (l::int). c*x+?e = i*l" by (simp add: dvd_def)
  3.1337 +      hence "\<exists> (l::int). c*x - c*(k*d) +?e = i*l - c*(k*d)" by simp
  3.1338 +      hence "\<exists> (l::int). c*x - c*(k*d) +?e = i*l - c*(k*i*di)" by (simp add: di_def)
  3.1339 +      hence "\<exists> (l::int). c*x - c*(k*d) +?e = i*((l - c*k*di))" by (simp add: algebra_simps)
  3.1340 +      hence "\<exists> (l::int). c*x - c * (k*d) +?e = i*l"
  3.1341 +	by blast
  3.1342 +      thus "i dvd c*x - c*(k*d) + Inum (x # bs) e" by (simp add: dvd_def)
  3.1343 +    qed
  3.1344 +qed (auto simp add: gr0_conv_Suc numbound0_I[where bs="bs" and b="x - k*d" and b'="x"])
  3.1345 +
  3.1346 +lemma mirror\<alpha>\<beta>:
  3.1347 +  assumes lp: "iszlfm p"
  3.1348 +  shows "(Inum (i#bs)) ` set (\<alpha> p) = (Inum (i#bs)) ` set (\<beta> (mirror p))"
  3.1349 +using lp
  3.1350 +by (induct p rule: mirror.induct, auto)
  3.1351 +
  3.1352 +lemma mirror: 
  3.1353 +  assumes lp: "iszlfm p"
  3.1354 +  shows "Ifm bbs (x#bs) (mirror p) = Ifm bbs ((- x)#bs) p" 
  3.1355 +using lp
  3.1356 +proof(induct p rule: iszlfm.induct)
  3.1357 +  case (9 j c e) hence nb: "numbound0 e" by simp
  3.1358 +  have "Ifm bbs (x#bs) (mirror (Dvd j (CN 0 c e))) = (j dvd c*x - Inum (x#bs) e)" (is "_ = (j dvd c*x - ?e)") by simp
  3.1359 +    also have "\<dots> = (j dvd (- (c*x - ?e)))"
  3.1360 +    by (simp only: zdvd_zminus_iff)
  3.1361 +  also have "\<dots> = (j dvd (c* (- x)) + ?e)"
  3.1362 +    apply (simp only: minus_mult_right[symmetric] minus_mult_left[symmetric] diff_def zadd_ac zminus_zadd_distrib)
  3.1363 +    by (simp add: algebra_simps)
  3.1364 +  also have "\<dots> = Ifm bbs ((- x)#bs) (Dvd j (CN 0 c e))"
  3.1365 +    using numbound0_I[OF nb, where bs="bs" and b="x" and b'="- x"]
  3.1366 +    by simp
  3.1367 +  finally show ?case .
  3.1368 +next
  3.1369 +    case (10 j c e) hence nb: "numbound0 e" by simp
  3.1370 +  have "Ifm bbs (x#bs) (mirror (Dvd j (CN 0 c e))) = (j dvd c*x - Inum (x#bs) e)" (is "_ = (j dvd c*x - ?e)") by simp
  3.1371 +    also have "\<dots> = (j dvd (- (c*x - ?e)))"
  3.1372 +    by (simp only: zdvd_zminus_iff)
  3.1373 +  also have "\<dots> = (j dvd (c* (- x)) + ?e)"
  3.1374 +    apply (simp only: minus_mult_right[symmetric] minus_mult_left[symmetric] diff_def zadd_ac zminus_zadd_distrib)
  3.1375 +    by (simp add: algebra_simps)
  3.1376 +  also have "\<dots> = Ifm bbs ((- x)#bs) (Dvd j (CN 0 c e))"
  3.1377 +    using numbound0_I[OF nb, where bs="bs" and b="x" and b'="- x"]
  3.1378 +    by simp
  3.1379 +  finally show ?case by simp
  3.1380 +qed (auto simp add: numbound0_I[where bs="bs" and b="x" and b'="- x"] gr0_conv_Suc)
  3.1381 +
  3.1382 +lemma mirror_l: "iszlfm p \<and> d\<beta> p 1 
  3.1383 +  \<Longrightarrow> iszlfm (mirror p) \<and> d\<beta> (mirror p) 1"
  3.1384 +by (induct p rule: mirror.induct, auto)
  3.1385 +
  3.1386 +lemma mirror_\<delta>: "iszlfm p \<Longrightarrow> \<delta> (mirror p) = \<delta> p"
  3.1387 +by (induct p rule: mirror.induct,auto)
  3.1388 +
  3.1389 +lemma \<beta>_numbound0: assumes lp: "iszlfm p"
  3.1390 +  shows "\<forall> b\<in> set (\<beta> p). numbound0 b"
  3.1391 +  using lp by (induct p rule: \<beta>.induct,auto)
  3.1392 +
  3.1393 +lemma d\<beta>_mono: 
  3.1394 +  assumes linp: "iszlfm p"
  3.1395 +  and dr: "d\<beta> p l"
  3.1396 +  and d: "l dvd l'"
  3.1397 +  shows "d\<beta> p l'"
  3.1398 +using dr linp zdvd_trans[where n="l" and k="l'", simplified d]
  3.1399 +by (induct p rule: iszlfm.induct) simp_all
  3.1400 +
  3.1401 +lemma \<alpha>_l: assumes lp: "iszlfm p"
  3.1402 +  shows "\<forall> b\<in> set (\<alpha> p). numbound0 b"
  3.1403 +using lp
  3.1404 +by(induct p rule: \<alpha>.induct, auto)
  3.1405 +
  3.1406 +lemma \<zeta>: 
  3.1407 +  assumes linp: "iszlfm p"
  3.1408 +  shows "\<zeta> p > 0 \<and> d\<beta> p (\<zeta> p)"
  3.1409 +using linp
  3.1410 +proof(induct p rule: iszlfm.induct)
  3.1411 +  case (1 p q)
  3.1412 +  from prems have dl1: "\<zeta> p dvd zlcm (\<zeta> p) (\<zeta> q)" by simp
  3.1413 +  from prems have dl2: "\<zeta> q dvd zlcm (\<zeta> p) (\<zeta> q)"  by simp
  3.1414 +  from prems d\<beta>_mono[where p = "p" and l="\<zeta> p" and l'="zlcm (\<zeta> p) (\<zeta> q)"] 
  3.1415 +    d\<beta>_mono[where p = "q" and l="\<zeta> q" and l'="zlcm (\<zeta> p) (\<zeta> q)"] 
  3.1416 +    dl1 dl2 show ?case by (auto simp add: zlcm_pos)
  3.1417 +next
  3.1418 +  case (2 p q)
  3.1419 +  from prems have dl1: "\<zeta> p dvd zlcm (\<zeta> p) (\<zeta> q)" by simp
  3.1420 +  from prems have dl2: "\<zeta> q dvd zlcm (\<zeta> p) (\<zeta> q)" by simp
  3.1421 +  from prems d\<beta>_mono[where p = "p" and l="\<zeta> p" and l'="zlcm (\<zeta> p) (\<zeta> q)"] 
  3.1422 +    d\<beta>_mono[where p = "q" and l="\<zeta> q" and l'="zlcm (\<zeta> p) (\<zeta> q)"] 
  3.1423 +    dl1 dl2 show ?case by (auto simp add: zlcm_pos)
  3.1424 +qed (auto simp add: zlcm_pos)
  3.1425 +
  3.1426 +lemma a\<beta>: assumes linp: "iszlfm p" and d: "d\<beta> p l" and lp: "l > 0"
  3.1427 +  shows "iszlfm (a\<beta> p l) \<and> d\<beta> (a\<beta> p l) 1 \<and> (Ifm bbs (l*x #bs) (a\<beta> p l) = Ifm bbs (x#bs) p)"
  3.1428 +using linp d
  3.1429 +proof (induct p rule: iszlfm.induct)
  3.1430 +  case (5 c e) hence cp: "c>0" and be: "numbound0 e" and d': "c dvd l" by simp+
  3.1431 +    from lp cp have clel: "c\<le>l" by (simp add: zdvd_imp_le [OF d' lp])
  3.1432 +    from cp have cnz: "c \<noteq> 0" by simp
  3.1433 +    have "c div c\<le> l div c"
  3.1434 +      by (simp add: zdiv_mono1[OF clel cp])
  3.1435 +    then have ldcp:"0 < l div c" 
  3.1436 +      by (simp add: zdiv_self[OF cnz])
  3.1437 +    have "c * (l div c) = c* (l div c) + l mod c" using d' zdvd_iff_zmod_eq_0[where m="c" and n="l"] by simp
  3.1438 +    hence cl:"c * (l div c) =l" using zmod_zdiv_equality[where a="l" and b="c", symmetric] 
  3.1439 +      by simp
  3.1440 +    hence "(l*x + (l div c) * Inum (x # bs) e < 0) =
  3.1441 +          ((c * (l div c)) * x + (l div c) * Inum (x # bs) e < 0)"
  3.1442 +      by simp
  3.1443 +    also have "\<dots> = ((l div c) * (c*x + Inum (x # bs) e) < (l div c) * 0)" by (simp add: algebra_simps)
  3.1444 +    also have "\<dots> = (c*x + Inum (x # bs) e < 0)"
  3.1445 +    using mult_less_0_iff [where a="(l div c)" and b="c*x + Inum (x # bs) e"] ldcp by simp
  3.1446 +  finally show ?case using numbound0_I[OF be,where b="l*x" and b'="x" and bs="bs"] be  by simp
  3.1447 +next
  3.1448 +  case (6 c e) hence cp: "c>0" and be: "numbound0 e" and d': "c dvd l" by simp+
  3.1449 +    from lp cp have clel: "c\<le>l" by (simp add: zdvd_imp_le [OF d' lp])
  3.1450 +    from cp have cnz: "c \<noteq> 0" by simp
  3.1451 +    have "c div c\<le> l div c"
  3.1452 +      by (simp add: zdiv_mono1[OF clel cp])
  3.1453 +    then have ldcp:"0 < l div c" 
  3.1454 +      by (simp add: zdiv_self[OF cnz])
  3.1455 +    have "c * (l div c) = c* (l div c) + l mod c" using d' zdvd_iff_zmod_eq_0[where m="c" and n="l"] by simp
  3.1456 +    hence cl:"c * (l div c) =l" using zmod_zdiv_equality[where a="l" and b="c", symmetric] 
  3.1457 +      by simp
  3.1458 +    hence "(l*x + (l div c) * Inum (x# bs) e \<le> 0) =
  3.1459 +          ((c * (l div c)) * x + (l div c) * Inum (x # bs) e \<le> 0)"
  3.1460 +      by simp
  3.1461 +    also have "\<dots> = ((l div c) * (c * x + Inum (x # bs) e) \<le> ((l div c)) * 0)" by (simp add: algebra_simps)
  3.1462 +    also have "\<dots> = (c*x + Inum (x # bs) e \<le> 0)"
  3.1463 +    using mult_le_0_iff [where a="(l div c)" and b="c*x + Inum (x # bs) e"] ldcp by simp
  3.1464 +  finally show ?case using numbound0_I[OF be,where b="l*x" and b'="x" and bs="bs"]  be by simp
  3.1465 +next
  3.1466 +  case (7 c e) hence cp: "c>0" and be: "numbound0 e" and d': "c dvd l" by simp+
  3.1467 +    from lp cp have clel: "c\<le>l" by (simp add: zdvd_imp_le [OF d' lp])
  3.1468 +    from cp have cnz: "c \<noteq> 0" by simp
  3.1469 +    have "c div c\<le> l div c"
  3.1470 +      by (simp add: zdiv_mono1[OF clel cp])
  3.1471 +    then have ldcp:"0 < l div c" 
  3.1472 +      by (simp add: zdiv_self[OF cnz])
  3.1473 +    have "c * (l div c) = c* (l div c) + l mod c" using d' zdvd_iff_zmod_eq_0[where m="c" and n="l"] by simp
  3.1474 +    hence cl:"c * (l div c) =l" using zmod_zdiv_equality[where a="l" and b="c", symmetric] 
  3.1475 +      by simp
  3.1476 +    hence "(l*x + (l div c)* Inum (x # bs) e > 0) =
  3.1477 +          ((c * (l div c)) * x + (l div c) * Inum (x # bs) e > 0)"
  3.1478 +      by simp
  3.1479 +    also have "\<dots> = ((l div c) * (c * x + Inum (x # bs) e) > ((l div c)) * 0)" by (simp add: algebra_simps)
  3.1480 +    also have "\<dots> = (c * x + Inum (x # bs) e > 0)"
  3.1481 +    using zero_less_mult_iff [where a="(l div c)" and b="c * x + Inum (x # bs) e"] ldcp by simp
  3.1482 +  finally show ?case using numbound0_I[OF be,where b="(l * x)" and b'="x" and bs="bs"]  be  by simp
  3.1483 +next
  3.1484 +  case (8 c e) hence cp: "c>0" and be: "numbound0 e" and d': "c dvd l" by simp+
  3.1485 +    from lp cp have clel: "c\<le>l" by (simp add: zdvd_imp_le [OF d' lp])
  3.1486 +    from cp have cnz: "c \<noteq> 0" by simp
  3.1487 +    have "c div c\<le> l div c"
  3.1488 +      by (simp add: zdiv_mono1[OF clel cp])
  3.1489 +    then have ldcp:"0 < l div c" 
  3.1490 +      by (simp add: zdiv_self[OF cnz])
  3.1491 +    have "c * (l div c) = c* (l div c) + l mod c" using d' zdvd_iff_zmod_eq_0[where m="c" and n="l"] by simp
  3.1492 +    hence cl:"c * (l div c) =l" using zmod_zdiv_equality[where a="l" and b="c", symmetric] 
  3.1493 +      by simp
  3.1494 +    hence "(l*x + (l div c)* Inum (x # bs) e \<ge> 0) =
  3.1495 +          ((c*(l div c))*x + (l div c)* Inum (x # bs) e \<ge> 0)"
  3.1496 +      by simp
  3.1497 +    also have "\<dots> = ((l div c)*(c*x + Inum (x # bs) e) \<ge> ((l div c)) * 0)" 
  3.1498 +      by (simp add: algebra_simps)
  3.1499 +    also have "\<dots> = (c*x + Inum (x # bs) e \<ge> 0)" using ldcp 
  3.1500 +      zero_le_mult_iff [where a="l div c" and b="c*x + Inum (x # bs) e"] by simp
  3.1501 +  finally show ?case using be numbound0_I[OF be,where b="l*x" and b'="x" and bs="bs"]  
  3.1502 +    by simp
  3.1503 +next
  3.1504 +  case (3 c e) hence cp: "c>0" and be: "numbound0 e" and d': "c dvd l" by simp+
  3.1505 +    from lp cp have clel: "c\<le>l" by (simp add: zdvd_imp_le [OF d' lp])
  3.1506 +    from cp have cnz: "c \<noteq> 0" by simp
  3.1507 +    have "c div c\<le> l div c"
  3.1508 +      by (simp add: zdiv_mono1[OF clel cp])
  3.1509 +    then have ldcp:"0 < l div c" 
  3.1510 +      by (simp add: zdiv_self[OF cnz])
  3.1511 +    have "c * (l div c) = c* (l div c) + l mod c" using d' zdvd_iff_zmod_eq_0[where m="c" and n="l"] by simp
  3.1512 +    hence cl:"c * (l div c) =l" using zmod_zdiv_equality[where a="l" and b="c", symmetric] 
  3.1513 +      by simp
  3.1514 +    hence "(l * x + (l div c) * Inum (x # bs) e = 0) =
  3.1515 +          ((c * (l div c)) * x + (l div c) * Inum (x # bs) e = 0)"
  3.1516 +      by simp
  3.1517 +    also have "\<dots> = ((l div c) * (c * x + Inum (x # bs) e) = ((l div c)) * 0)" by (simp add: algebra_simps)
  3.1518 +    also have "\<dots> = (c * x + Inum (x # bs) e = 0)"
  3.1519 +    using mult_eq_0_iff [where a="(l div c)" and b="c * x + Inum (x # bs) e"] ldcp by simp
  3.1520 +  finally show ?case using numbound0_I[OF be,where b="(l * x)" and b'="x" and bs="bs"]  be  by simp
  3.1521 +next
  3.1522 +  case (4 c e) hence cp: "c>0" and be: "numbound0 e" and d': "c dvd l" by simp+
  3.1523 +    from lp cp have clel: "c\<le>l" by (simp add: zdvd_imp_le [OF d' lp])
  3.1524 +    from cp have cnz: "c \<noteq> 0" by simp
  3.1525 +    have "c div c\<le> l div c"
  3.1526 +      by (simp add: zdiv_mono1[OF clel cp])
  3.1527 +    then have ldcp:"0 < l div c" 
  3.1528 +      by (simp add: zdiv_self[OF cnz])
  3.1529 +    have "c * (l div c) = c* (l div c) + l mod c" using d' zdvd_iff_zmod_eq_0[where m="c" and n="l"] by simp
  3.1530 +    hence cl:"c * (l div c) =l" using zmod_zdiv_equality[where a="l" and b="c", symmetric] 
  3.1531 +      by simp
  3.1532 +    hence "(l * x + (l div c) * Inum (x # bs) e \<noteq> 0) =
  3.1533 +          ((c * (l div c)) * x + (l div c) * Inum (x # bs) e \<noteq> 0)"
  3.1534 +      by simp
  3.1535 +    also have "\<dots> = ((l div c) * (c * x + Inum (x # bs) e) \<noteq> ((l div c)) * 0)" by (simp add: algebra_simps)
  3.1536 +    also have "\<dots> = (c * x + Inum (x # bs) e \<noteq> 0)"
  3.1537 +    using zero_le_mult_iff [where a="(l div c)" and b="c * x + Inum (x # bs) e"] ldcp by simp
  3.1538 +  finally show ?case using numbound0_I[OF be,where b="(l * x)" and b'="x" and bs="bs"]  be  by simp
  3.1539 +next
  3.1540 +  case (9 j c e) hence cp: "c>0" and be: "numbound0 e" and jp: "j > 0" and d': "c dvd l" by simp+
  3.1541 +    from lp cp have clel: "c\<le>l" by (simp add: zdvd_imp_le [OF d' lp])
  3.1542 +    from cp have cnz: "c \<noteq> 0" by simp
  3.1543 +    have "c div c\<le> l div c"
  3.1544 +      by (simp add: zdiv_mono1[OF clel cp])
  3.1545 +    then have ldcp:"0 < l div c" 
  3.1546 +      by (simp add: zdiv_self[OF cnz])
  3.1547 +    have "c * (l div c) = c* (l div c) + l mod c" using d' zdvd_iff_zmod_eq_0[where m="c" and n="l"] by simp
  3.1548 +    hence cl:"c * (l div c) =l" using zmod_zdiv_equality[where a="l" and b="c", symmetric] 
  3.1549 +      by simp
  3.1550 +    hence "(\<exists> (k::int). l * x + (l div c) * Inum (x # bs) e = ((l div c) * j) * k) = (\<exists> (k::int). (c * (l div c)) * x + (l div c) * Inum (x # bs) e = ((l div c) * j) * k)"  by simp
  3.1551 +    also have "\<dots> = (\<exists> (k::int). (l div c) * (c * x + Inum (x # bs) e - j * k) = (l div c)*0)" by (simp add: algebra_simps)
  3.1552 +    also fix k have "\<dots> = (\<exists> (k::int). c * x + Inum (x # bs) e - j * k = 0)"
  3.1553 +    using zero_le_mult_iff [where a="(l div c)" and b="c * x + Inum (x # bs) e - j * k"] ldcp by simp
  3.1554 +  also have "\<dots> = (\<exists> (k::int). c * x + Inum (x # bs) e = j * k)" by simp
  3.1555 +  finally show ?case using numbound0_I[OF be,where b="(l * x)" and b'="x" and bs="bs"] be  mult_strict_mono[OF ldcp jp ldcp ] by (simp add: dvd_def)
  3.1556 +next
  3.1557 +  case (10 j c e) hence cp: "c>0" and be: "numbound0 e" and jp: "j > 0" and d': "c dvd l" by simp+
  3.1558 +    from lp cp have clel: "c\<le>l" by (simp add: zdvd_imp_le [OF d' lp])
  3.1559 +    from cp have cnz: "c \<noteq> 0" by simp
  3.1560 +    have "c div c\<le> l div c"
  3.1561 +      by (simp add: zdiv_mono1[OF clel cp])
  3.1562 +    then have ldcp:"0 < l div c" 
  3.1563 +      by (simp add: zdiv_self[OF cnz])
  3.1564 +    have "c * (l div c) = c* (l div c) + l mod c" using d' zdvd_iff_zmod_eq_0[where m="c" and n="l"] by simp
  3.1565 +    hence cl:"c * (l div c) =l" using zmod_zdiv_equality[where a="l" and b="c", symmetric] 
  3.1566 +      by simp
  3.1567 +    hence "(\<exists> (k::int). l * x + (l div c) * Inum (x # bs) e = ((l div c) * j) * k) = (\<exists> (k::int). (c * (l div c)) * x + (l div c) * Inum (x # bs) e = ((l div c) * j) * k)"  by simp
  3.1568 +    also have "\<dots> = (\<exists> (k::int). (l div c) * (c * x + Inum (x # bs) e - j * k) = (l div c)*0)" by (simp add: algebra_simps)
  3.1569 +    also fix k have "\<dots> = (\<exists> (k::int). c * x + Inum (x # bs) e - j * k = 0)"
  3.1570 +    using zero_le_mult_iff [where a="(l div c)" and b="c * x + Inum (x # bs) e - j * k"] ldcp by simp
  3.1571 +  also have "\<dots> = (\<exists> (k::int). c * x + Inum (x # bs) e = j * k)" by simp
  3.1572 +  finally show ?case using numbound0_I[OF be,where b="(l * x)" and b'="x" and bs="bs"] be  mult_strict_mono[OF ldcp jp ldcp ] by (simp add: dvd_def)
  3.1573 +qed (auto simp add: gr0_conv_Suc numbound0_I[where bs="bs" and b="(l * x)" and b'="x"])
  3.1574 +
  3.1575 +lemma a\<beta>_ex: assumes linp: "iszlfm p" and d: "d\<beta> p l" and lp: "l>0"
  3.1576 +  shows "(\<exists> x. l dvd x \<and> Ifm bbs (x #bs) (a\<beta> p l)) = (\<exists> (x::int). Ifm bbs (x#bs) p)"
  3.1577 +  (is "(\<exists> x. l dvd x \<and> ?P x) = (\<exists> x. ?P' x)")
  3.1578 +proof-
  3.1579 +  have "(\<exists> x. l dvd x \<and> ?P x) = (\<exists> (x::int). ?P (l*x))"
  3.1580 +    using unity_coeff_ex[where l="l" and P="?P", simplified] by simp
  3.1581 +  also have "\<dots> = (\<exists> (x::int). ?P' x)" using a\<beta>[OF linp d lp] by simp
  3.1582 +  finally show ?thesis  . 
  3.1583 +qed
  3.1584 +
  3.1585 +lemma \<beta>:
  3.1586 +  assumes lp: "iszlfm p"
  3.1587 +  and u: "d\<beta> p 1"
  3.1588 +  and d: "d\<delta> p d"
  3.1589 +  and dp: "d > 0"
  3.1590 +  and nob: "\<not>(\<exists>(j::int) \<in> {1 .. d}. \<exists> b\<in> (Inum (a#bs)) ` set(\<beta> p). x = b + j)"
  3.1591 +  and p: "Ifm bbs (x#bs) p" (is "?P x")
  3.1592 +  shows "?P (x - d)"
  3.1593 +using lp u d dp nob p
  3.1594 +proof(induct p rule: iszlfm.induct)
  3.1595 +  case (5 c e) hence c1: "c=1" and  bn:"numbound0 e" by simp+
  3.1596 +    with dp p c1 numbound0_I[OF bn,where b="(x-d)" and b'="x" and bs="bs"] prems
  3.1597 +    show ?case by simp
  3.1598 +next
  3.1599 +  case (6 c e)  hence c1: "c=1" and  bn:"numbound0 e" by simp+
  3.1600 +    with dp p c1 numbound0_I[OF bn,where b="(x-d)" and b'="x" and bs="bs"] prems
  3.1601 +    show ?case by simp
  3.1602 +next
  3.1603 +  case (7 c e) hence p: "Ifm bbs (x #bs) (Gt (CN 0 c e))" and c1: "c=1" and bn:"numbound0 e" by simp+
  3.1604 +    let ?e = "Inum (x # bs) e"
  3.1605 +    {assume "(x-d) +?e > 0" hence ?case using c1 
  3.1606 +      numbound0_I[OF bn,where b="(x-d)" and b'="x" and bs="bs"] by simp}
  3.1607 +    moreover
  3.1608 +    {assume H: "\<not> (x-d) + ?e > 0" 
  3.1609 +      let ?v="Neg e"
  3.1610 +      have vb: "?v \<in> set (\<beta> (Gt (CN 0 c e)))" by simp
  3.1611 +      from prems(11)[simplified simp_thms Inum.simps \<beta>.simps set.simps bex_simps numbound0_I[OF bn,where b="a" and b'="x" and bs="bs"]] 
  3.1612 +      have nob: "\<not> (\<exists> j\<in> {1 ..d}. x =  - ?e + j)" by auto 
  3.1613 +      from H p have "x + ?e > 0 \<and> x + ?e \<le> d" by (simp add: c1)
  3.1614 +      hence "x + ?e \<ge> 1 \<and> x + ?e \<le> d"  by simp
  3.1615 +      hence "\<exists> (j::int) \<in> {1 .. d}. j = x + ?e" by simp
  3.1616 +      hence "\<exists> (j::int) \<in> {1 .. d}. x = (- ?e + j)" 
  3.1617 +	by (simp add: algebra_simps)
  3.1618 +      with nob have ?case by auto}
  3.1619 +    ultimately show ?case by blast
  3.1620 +next
  3.1621 +  case (8 c e) hence p: "Ifm bbs (x #bs) (Ge (CN 0 c e))" and c1: "c=1" and bn:"numbound0 e" 
  3.1622 +    by simp+
  3.1623 +    let ?e = "Inum (x # bs) e"
  3.1624 +    {assume "(x-d) +?e \<ge> 0" hence ?case using  c1 
  3.1625 +      numbound0_I[OF bn,where b="(x-d)" and b'="x" and bs="bs"]
  3.1626 +	by simp}
  3.1627 +    moreover
  3.1628 +    {assume H: "\<not> (x-d) + ?e \<ge> 0" 
  3.1629 +      let ?v="Sub (C -1) e"
  3.1630 +      have vb: "?v \<in> set (\<beta> (Ge (CN 0 c e)))" by simp
  3.1631 +      from prems(11)[simplified simp_thms Inum.simps \<beta>.simps set.simps bex_simps numbound0_I[OF bn,where b="a" and b'="x" and bs="bs"]] 
  3.1632 +      have nob: "\<not> (\<exists> j\<in> {1 ..d}. x =  - ?e - 1 + j)" by auto 
  3.1633 +      from H p have "x + ?e \<ge> 0 \<and> x + ?e < d" by (simp add: c1)
  3.1634 +      hence "x + ?e +1 \<ge> 1 \<and> x + ?e + 1 \<le> d"  by simp
  3.1635 +      hence "\<exists> (j::int) \<in> {1 .. d}. j = x + ?e + 1" by simp
  3.1636 +      hence "\<exists> (j::int) \<in> {1 .. d}. x= - ?e - 1 + j" by (simp add: algebra_simps)
  3.1637 +      with nob have ?case by simp }
  3.1638 +    ultimately show ?case by blast
  3.1639 +next
  3.1640 +  case (3 c e) hence p: "Ifm bbs (x #bs) (Eq (CN 0 c e))" (is "?p x") and c1: "c=1" and bn:"numbound0 e" by simp+
  3.1641 +    let ?e = "Inum (x # bs) e"
  3.1642 +    let ?v="(Sub (C -1) e)"
  3.1643 +    have vb: "?v \<in> set (\<beta> (Eq (CN 0 c e)))" by simp
  3.1644 +    from p have "x= - ?e" by (simp add: c1) with prems(11) show ?case using dp
  3.1645 +      by simp (erule ballE[where x="1"],
  3.1646 +	simp_all add:algebra_simps numbound0_I[OF bn,where b="x"and b'="a"and bs="bs"])
  3.1647 +next
  3.1648 +  case (4 c e)hence p: "Ifm bbs (x #bs) (NEq (CN 0 c e))" (is "?p x") and c1: "c=1" and bn:"numbound0 e" by simp+
  3.1649 +    let ?e = "Inum (x # bs) e"
  3.1650 +    let ?v="Neg e"
  3.1651 +    have vb: "?v \<in> set (\<beta> (NEq (CN 0 c e)))" by simp
  3.1652 +    {assume "x - d + Inum (((x -d)) # bs) e \<noteq> 0" 
  3.1653 +      hence ?case by (simp add: c1)}
  3.1654 +    moreover
  3.1655 +    {assume H: "x - d + Inum (((x -d)) # bs) e = 0"
  3.1656 +      hence "x = - Inum (((x -d)) # bs) e + d" by simp
  3.1657 +      hence "x = - Inum (a # bs) e + d"
  3.1658 +	by (simp add: numbound0_I[OF bn,where b="x - d"and b'="a"and bs="bs"])
  3.1659 +       with prems(11) have ?case using dp by simp}
  3.1660 +  ultimately show ?case by blast
  3.1661 +next 
  3.1662 +  case (9 j c e) hence p: "Ifm bbs (x #bs) (Dvd j (CN 0 c e))" (is "?p x") and c1: "c=1" and bn:"numbound0 e" by simp+
  3.1663 +    let ?e = "Inum (x # bs) e"
  3.1664 +    from prems have id: "j dvd d" by simp
  3.1665 +    from c1 have "?p x = (j dvd (x+ ?e))" by simp
  3.1666 +    also have "\<dots> = (j dvd x - d + ?e)" 
  3.1667 +      using zdvd_period[OF id, where x="x" and c="-1" and t="?e"] by simp
  3.1668 +    finally show ?case 
  3.1669 +      using numbound0_I[OF bn,where b="(x-d)" and b'="x" and bs="bs"] c1 p by simp
  3.1670 +next
  3.1671 +  case (10 j c e) hence p: "Ifm bbs (x #bs) (NDvd j (CN 0 c e))" (is "?p x") and c1: "c=1" and bn:"numbound0 e" by simp+
  3.1672 +    let ?e = "Inum (x # bs) e"
  3.1673 +    from prems have id: "j dvd d" by simp
  3.1674 +    from c1 have "?p x = (\<not> j dvd (x+ ?e))" by simp
  3.1675 +    also have "\<dots> = (\<not> j dvd x - d + ?e)" 
  3.1676 +      using zdvd_period[OF id, where x="x" and c="-1" and t="?e"] by simp
  3.1677 +    finally show ?case using numbound0_I[OF bn,where b="(x-d)" and b'="x" and bs="bs"] c1 p by simp
  3.1678 +qed (auto simp add: numbound0_I[where bs="bs" and b="(x - d)" and b'="x"] gr0_conv_Suc)
  3.1679 +
  3.1680 +lemma \<beta>':   
  3.1681 +  assumes lp: "iszlfm p"
  3.1682 +  and u: "d\<beta> p 1"
  3.1683 +  and d: "d\<delta> p d"
  3.1684 +  and dp: "d > 0"
  3.1685 +  shows "\<forall> x. \<not>(\<exists>(j::int) \<in> {1 .. d}. \<exists> b\<in> set(\<beta> p). Ifm bbs ((Inum (a#bs) b + j) #bs) p) \<longrightarrow> Ifm bbs (x#bs) p \<longrightarrow> Ifm bbs ((x - d)#bs) p" (is "\<forall> x. ?b \<longrightarrow> ?P x \<longrightarrow> ?P (x - d)")
  3.1686 +proof(clarify)
  3.1687 +  fix x 
  3.1688 +  assume nb:"?b" and px: "?P x" 
  3.1689 +  hence nb2: "\<not>(\<exists>(j::int) \<in> {1 .. d}. \<exists> b\<in> (Inum (a#bs)) ` set(\<beta> p). x = b + j)"
  3.1690 +    by auto
  3.1691 +  from  \<beta>[OF lp u d dp nb2 px] show "?P (x -d )" .
  3.1692 +qed
  3.1693 +lemma cpmi_eq: "0 < D \<Longrightarrow> (EX z::int. ALL x. x < z --> (P x = P1 x))
  3.1694 +==> ALL x.~(EX (j::int) : {1..D}. EX (b::int) : B. P(b+j)) --> P (x) --> P (x - D) 
  3.1695 +==> (ALL (x::int). ALL (k::int). ((P1 x)= (P1 (x-k*D))))
  3.1696 +==> (EX (x::int). P(x)) = ((EX (j::int) : {1..D} . (P1(j))) | (EX (j::int) : {1..D}. EX (b::int) : B. P (b+j)))"
  3.1697 +apply(rule iffI)
  3.1698 +prefer 2
  3.1699 +apply(drule minusinfinity)
  3.1700 +apply assumption+
  3.1701 +apply(fastsimp)
  3.1702 +apply clarsimp
  3.1703 +apply(subgoal_tac "!!k. 0<=k \<Longrightarrow> !x. P x \<longrightarrow> P (x - k*D)")
  3.1704 +apply(frule_tac x = x and z=z in decr_lemma)
  3.1705 +apply(subgoal_tac "P1(x - (\<bar>x - z\<bar> + 1) * D)")
  3.1706 +prefer 2
  3.1707 +apply(subgoal_tac "0 <= (\<bar>x - z\<bar> + 1)")
  3.1708 +prefer 2 apply arith
  3.1709 + apply fastsimp
  3.1710 +apply(drule (1)  periodic_finite_ex)
  3.1711 +apply blast
  3.1712 +apply(blast dest:decr_mult_lemma)
  3.1713 +done
  3.1714 +
  3.1715 +theorem cp_thm:
  3.1716 +  assumes lp: "iszlfm p"
  3.1717 +  and u: "d\<beta> p 1"
  3.1718 +  and d: "d\<delta> p d"
  3.1719 +  and dp: "d > 0"
  3.1720 +  shows "(\<exists> (x::int). Ifm bbs (x #bs) p) = (\<exists> j\<in> {1.. d}. Ifm bbs (j #bs) (minusinf p) \<or> (\<exists> b \<in> set (\<beta> p). Ifm bbs ((Inum (i#bs) b + j) #bs) p))"
  3.1721 +  (is "(\<exists> (x::int). ?P (x)) = (\<exists> j\<in> ?D. ?M j \<or> (\<exists> b\<in> ?B. ?P (?I b + j)))")
  3.1722 +proof-
  3.1723 +  from minusinf_inf[OF lp u] 
  3.1724 +  have th: "\<exists>(z::int). \<forall>x<z. ?P (x) = ?M x" by blast
  3.1725 +  let ?B' = "{?I b | b. b\<in> ?B}"
  3.1726 +  have BB': "(\<exists>j\<in>?D. \<exists>b\<in> ?B. ?P (?I b +j)) = (\<exists> j \<in> ?D. \<exists> b \<in> ?B'. ?P (b + j))" by auto
  3.1727 +  hence th2: "\<forall> x. \<not> (\<exists> j \<in> ?D. \<exists> b \<in> ?B'. ?P ((b + j))) \<longrightarrow> ?P (x) \<longrightarrow> ?P ((x - d))" 
  3.1728 +    using \<beta>'[OF lp u d dp, where a="i" and bbs = "bbs"] by blast
  3.1729 +  from minusinf_repeats[OF d lp]
  3.1730 +  have th3: "\<forall> x k. ?M x = ?M (x-k*d)" by simp
  3.1731 +  from cpmi_eq[OF dp th th2 th3] BB' show ?thesis by blast
  3.1732 +qed
  3.1733 +
  3.1734 +    (* Implement the right hand sides of Cooper's theorem and Ferrante and Rackoff. *)
  3.1735 +lemma mirror_ex: 
  3.1736 +  assumes lp: "iszlfm p"
  3.1737 +  shows "(\<exists> x. Ifm bbs (x#bs) (mirror p)) = (\<exists> x. Ifm bbs (x#bs) p)"
  3.1738 +  (is "(\<exists> x. ?I x ?mp) = (\<exists> x. ?I x p)")
  3.1739 +proof(auto)
  3.1740 +  fix x assume "?I x ?mp" hence "?I (- x) p" using mirror[OF lp] by blast
  3.1741 +  thus "\<exists> x. ?I x p" by blast
  3.1742 +next
  3.1743 +  fix x assume "?I x p" hence "?I (- x) ?mp" 
  3.1744 +    using mirror[OF lp, where x="- x", symmetric] by auto
  3.1745 +  thus "\<exists> x. ?I x ?mp" by blast
  3.1746 +qed
  3.1747 +
  3.1748 +
  3.1749 +lemma cp_thm': 
  3.1750 +  assumes lp: "iszlfm p"
  3.1751 +  and up: "d\<beta> p 1" and dd: "d\<delta> p d" and dp: "d > 0"
  3.1752 +  shows "(\<exists> x. Ifm bbs (x#bs) p) = ((\<exists> j\<in> {1 .. d}. Ifm bbs (j#bs) (minusinf p)) \<or> (\<exists> j\<in> {1.. d}. \<exists> b\<in> (Inum (i#bs)) ` set (\<beta> p). Ifm bbs ((b+j)#bs) p))"
  3.1753 +  using cp_thm[OF lp up dd dp,where i="i"] by auto
  3.1754 +
  3.1755 +constdefs unit:: "fm \<Rightarrow> fm \<times> num list \<times> int"
  3.1756 +  "unit p \<equiv> (let p' = zlfm p ; l = \<zeta> p' ; q = And (Dvd l (CN 0 1 (C 0))) (a\<beta> p' l); d = \<delta> q;
  3.1757 +             B = remdups (map simpnum (\<beta> q)) ; a = remdups (map simpnum (\<alpha> q))
  3.1758 +             in if length B \<le> length a then (q,B,d) else (mirror q, a,d))"
  3.1759 +
  3.1760 +lemma unit: assumes qf: "qfree p"
  3.1761 +  shows "\<And> q B d. unit p = (q,B,d) \<Longrightarrow> ((\<exists> x. Ifm bbs (x#bs) p) = (\<exists> x. Ifm bbs (x#bs) q)) \<and> (Inum (i#bs)) ` set B = (Inum (i#bs)) ` set (\<beta> q) \<and> d\<beta> q 1 \<and> d\<delta> q d \<and> d >0 \<and> iszlfm q \<and> (\<forall> b\<in> set B. numbound0 b)"
  3.1762 +proof-
  3.1763 +  fix q B d 
  3.1764 +  assume qBd: "unit p = (q,B,d)"
  3.1765 +  let ?thes = "((\<exists> x. Ifm bbs (x#bs) p) = (\<exists> x. Ifm bbs (x#bs) q)) \<and>
  3.1766 +    Inum (i#bs) ` set B = Inum (i#bs) ` set (\<beta> q) \<and>
  3.1767 +    d\<beta> q 1 \<and> d\<delta> q d \<and> 0 < d \<and> iszlfm q \<and> (\<forall> b\<in> set B. numbound0 b)"
  3.1768 +  let ?I = "\<lambda> x p. Ifm bbs (x#bs) p"
  3.1769 +  let ?p' = "zlfm p"
  3.1770 +  let ?l = "\<zeta> ?p'"
  3.1771 +  let ?q = "And (Dvd ?l (CN 0 1 (C 0))) (a\<beta> ?p' ?l)"
  3.1772 +  let ?d = "\<delta> ?q"
  3.1773 +  let ?B = "set (\<beta> ?q)"
  3.1774 +  let ?B'= "remdups (map simpnum (\<beta> ?q))"
  3.1775 +  let ?A = "set (\<alpha> ?q)"
  3.1776 +  let ?A'= "remdups (map simpnum (\<alpha> ?q))"
  3.1777 +  from conjunct1[OF zlfm_I[OF qf, where bs="bs"]] 
  3.1778 +  have pp': "\<forall> i. ?I i ?p' = ?I i p" by auto
  3.1779 +  from conjunct2[OF zlfm_I[OF qf, where bs="bs" and i="i"]]
  3.1780 +  have lp': "iszlfm ?p'" . 
  3.1781 +  from lp' \<zeta>[where p="?p'"] have lp: "?l >0" and dl: "d\<beta> ?p' ?l" by auto
  3.1782 +  from a\<beta>_ex[where p="?p'" and l="?l" and bs="bs", OF lp' dl lp] pp'
  3.1783 +  have pq_ex:"(\<exists> (x::int). ?I x p) = (\<exists> x. ?I x ?q)" by simp 
  3.1784 +  from lp' lp a\<beta>[OF lp' dl lp] have lq:"iszlfm ?q" and uq: "d\<beta> ?q 1"  by auto
  3.1785 +  from \<delta>[OF lq] have dp:"?d >0" and dd: "d\<delta> ?q ?d" by blast+
  3.1786 +  let ?N = "\<lambda> t. Inum (i#bs) t"
  3.1787 +  have "?N ` set ?B' = ((?N o simpnum) ` ?B)" by auto 
  3.1788 +  also have "\<dots> = ?N ` ?B" using simpnum_ci[where bs="i#bs"] by auto
  3.1789 +  finally have BB': "?N ` set ?B' = ?N ` ?B" .
  3.1790 +  have "?N ` set ?A' = ((?N o simpnum) ` ?A)" by auto 
  3.1791 +  also have "\<dots> = ?N ` ?A" using simpnum_ci[where bs="i#bs"] by auto
  3.1792 +  finally have AA': "?N ` set ?A' = ?N ` ?A" .
  3.1793 +  from \<beta>_numbound0[OF lq] have B_nb:"\<forall> b\<in> set ?B'. numbound0 b"
  3.1794 +    by (simp add: simpnum_numbound0)
  3.1795 +  from \<alpha>_l[OF lq] have A_nb: "\<forall> b\<in> set ?A'. numbound0 b"
  3.1796 +    by (simp add: simpnum_numbound0)
  3.1797 +    {assume "length ?B' \<le> length ?A'"
  3.1798 +    hence q:"q=?q" and "B = ?B'" and d:"d = ?d"
  3.1799 +      using qBd by (auto simp add: Let_def unit_def)
  3.1800 +    with BB' B_nb have b: "?N ` (set B) = ?N ` set (\<beta> q)" 
  3.1801 +      and bn: "\<forall>b\<in> set B. numbound0 b" by simp+ 
  3.1802 +  with pq_ex dp uq dd lq q d have ?thes by simp}
  3.1803 +  moreover 
  3.1804 +  {assume "\<not> (length ?B' \<le> length ?A')"
  3.1805 +    hence q:"q=mirror ?q" and "B = ?A'" and d:"d = ?d"
  3.1806 +      using qBd by (auto simp add: Let_def unit_def)
  3.1807 +    with AA' mirror\<alpha>\<beta>[OF lq] A_nb have b:"?N ` (set B) = ?N ` set (\<beta> q)" 
  3.1808 +      and bn: "\<forall>b\<in> set B. numbound0 b" by simp+
  3.1809 +    from mirror_ex[OF lq] pq_ex q 
  3.1810 +    have pqm_eq:"(\<exists> (x::int). ?I x p) = (\<exists> (x::int). ?I x q)" by simp
  3.1811 +    from lq uq q mirror_l[where p="?q"]
  3.1812 +    have lq': "iszlfm q" and uq: "d\<beta> q 1" by auto
  3.1813 +    from \<delta>[OF lq'] mirror_\<delta>[OF lq] q d have dq:"d\<delta> q d " by auto
  3.1814 +    from pqm_eq b bn uq lq' dp dq q dp d have ?thes by simp
  3.1815 +  }
  3.1816 +  ultimately show ?thes by blast
  3.1817 +qed
  3.1818 +    (* Cooper's Algorithm *)
  3.1819 +
  3.1820 +constdefs cooper :: "fm \<Rightarrow> fm"
  3.1821 +  "cooper p \<equiv> 
  3.1822 +  (let (q,B,d) = unit p; js = iupt 1 d;
  3.1823 +       mq = simpfm (minusinf q);
  3.1824 +       md = evaldjf (\<lambda> j. simpfm (subst0 (C j) mq)) js
  3.1825 +   in if md = T then T else
  3.1826 +    (let qd = evaldjf (\<lambda> (b,j). simpfm (subst0 (Add b (C j)) q)) 
  3.1827 +                               [(b,j). b\<leftarrow>B,j\<leftarrow>js]
  3.1828 +     in decr (disj md qd)))"
  3.1829 +lemma cooper: assumes qf: "qfree p"
  3.1830 +  shows "((\<exists> x. Ifm bbs (x#bs) p) = (Ifm bbs bs (cooper p))) \<and> qfree (cooper p)" 
  3.1831 +  (is "(?lhs = ?rhs) \<and> _")
  3.1832 +proof-
  3.1833 +  let ?I = "\<lambda> x p. Ifm bbs (x#bs) p"
  3.1834 +  let ?q = "fst (unit p)"
  3.1835 +  let ?B = "fst (snd(unit p))"
  3.1836 +  let ?d = "snd (snd (unit p))"
  3.1837 +  let ?js = "iupt 1 ?d"
  3.1838 +  let ?mq = "minusinf ?q"
  3.1839 +  let ?smq = "simpfm ?mq"
  3.1840 +  let ?md = "evaldjf (\<lambda> j. simpfm (subst0 (C j) ?smq)) ?js"
  3.1841 +  fix i
  3.1842 +  let ?N = "\<lambda> t. Inum (i#bs) t"
  3.1843 +  let ?Bjs = "[(b,j). b\<leftarrow>?B,j\<leftarrow>?js]"
  3.1844 +  let ?qd = "evaldjf (\<lambda> (b,j). simpfm (subst0 (Add b (C j)) ?q)) ?Bjs"
  3.1845 +  have qbf:"unit p = (?q,?B,?d)" by simp
  3.1846 +  from unit[OF qf qbf] have pq_ex: "(\<exists>(x::int). ?I x p) = (\<exists> (x::int). ?I x ?q)" and 
  3.1847 +    B:"?N ` set ?B = ?N ` set (\<beta> ?q)" and 
  3.1848 +    uq:"d\<beta> ?q 1" and dd: "d\<delta> ?q ?d" and dp: "?d > 0" and 
  3.1849 +    lq: "iszlfm ?q" and 
  3.1850 +    Bn: "\<forall> b\<in> set ?B. numbound0 b" by auto
  3.1851 +  from zlin_qfree[OF lq] have qfq: "qfree ?q" .
  3.1852 +  from simpfm_qf[OF minusinf_qfree[OF qfq]] have qfmq: "qfree ?smq".
  3.1853 +  have jsnb: "\<forall> j \<in> set ?js. numbound0 (C j)" by simp
  3.1854 +  hence "\<forall> j\<in> set ?js. bound0 (subst0 (C j) ?smq)" 
  3.1855 +    by (auto simp only: subst0_bound0[OF qfmq])
  3.1856 +  hence th: "\<forall> j\<in> set ?js. bound0 (simpfm (subst0 (C j) ?smq))"
  3.1857 +    by (auto simp add: simpfm_bound0)
  3.1858 +  from evaldjf_bound0[OF th] have mdb: "bound0 ?md" by simp 
  3.1859 +  from Bn jsnb have "\<forall> (b,j) \<in> set ?Bjs. numbound0 (Add b (C j))"
  3.1860 +    by simp
  3.1861 +  hence "\<forall> (b,j) \<in> set ?Bjs. bound0 (subst0 (Add b (C j)) ?q)"
  3.1862 +    using subst0_bound0[OF qfq] by blast
  3.1863 +  hence "\<forall> (b,j) \<in> set ?Bjs. bound0 (simpfm (subst0 (Add b (C j)) ?q))"
  3.1864 +    using simpfm_bound0  by blast
  3.1865 +  hence th': "\<forall> x \<in> set ?Bjs. bound0 ((\<lambda> (b,j). simpfm (subst0 (Add b (C j)) ?q)) x)"
  3.1866 +    by auto 
  3.1867 +  from evaldjf_bound0 [OF th'] have qdb: "bound0 ?qd" by simp
  3.1868 +  from mdb qdb 
  3.1869 +  have mdqdb: "bound0 (disj ?md ?qd)" by (simp only: disj_def, cases "?md=T \<or> ?qd=T", simp_all)
  3.1870 +  from trans [OF pq_ex cp_thm'[OF lq uq dd dp,where i="i"]] B
  3.1871 +  have "?lhs = (\<exists> j\<in> {1.. ?d}. ?I j ?mq \<or> (\<exists> b\<in> ?N ` set ?B. Ifm bbs ((b+ j)#bs) ?q))" by auto
  3.1872 +  also have "\<dots> = (\<exists> j\<in> {1.. ?d}. ?I j ?mq \<or> (\<exists> b\<in> set ?B. Ifm bbs ((?N b+ j)#bs) ?q))" by simp
  3.1873 +  also have "\<dots> = ((\<exists> j\<in> {1.. ?d}. ?I j ?mq ) \<or> (\<exists> j\<in> {1.. ?d}. \<exists> b\<in> set ?B. Ifm bbs ((?N (Add b (C j)))#bs) ?q))" by (simp only: Inum.simps) blast
  3.1874 +  also have "\<dots> = ((\<exists> j\<in> {1.. ?d}. ?I j ?smq ) \<or> (\<exists> j\<in> {1.. ?d}. \<exists> b\<in> set ?B. Ifm bbs ((?N (Add b (C j)))#bs) ?q))" by (simp add: simpfm) 
  3.1875 +  also have "\<dots> = ((\<exists> j\<in> set ?js. (\<lambda> j. ?I i (simpfm (subst0 (C j) ?smq))) j) \<or> (\<exists> j\<in> set ?js. \<exists> b\<in> set ?B. Ifm bbs ((?N (Add b (C j)))#bs) ?q))"
  3.1876 +    by (simp only: simpfm subst0_I[OF qfmq] iupt_set) auto
  3.1877 +  also have "\<dots> = (?I i (evaldjf (\<lambda> j. simpfm (subst0 (C j) ?smq)) ?js) \<or> (\<exists> j\<in> set ?js. \<exists> b\<in> set ?B. ?I i (subst0 (Add b (C j)) ?q)))" 
  3.1878 +   by (simp only: evaldjf_ex subst0_I[OF qfq])
  3.1879 + also have "\<dots>= (?I i ?md \<or> (\<exists> (b,j) \<in> set ?Bjs. (\<lambda> (b,j). ?I i (simpfm (subst0 (Add b (C j)) ?q))) (b,j)))"
  3.1880 +   by (simp only: simpfm set_concat set_map concat_map_singleton UN_simps) blast
  3.1881 + also have "\<dots> = (?I i ?md \<or> (?I i (evaldjf (\<lambda> (b,j). simpfm (subst0 (Add b (C j)) ?q)) ?Bjs)))"
  3.1882 +   by (simp only: evaldjf_ex[where bs="i#bs" and f="\<lambda> (b,j). simpfm (subst0 (Add b (C j)) ?q)" and ps="?Bjs"]) (auto simp add: split_def)
  3.1883 + finally have mdqd: "?lhs = (?I i ?md \<or> ?I i ?qd)" by simp  
  3.1884 +  also have "\<dots> = (?I i (disj ?md ?qd))" by (simp add: disj)
  3.1885 +  also have "\<dots> = (Ifm bbs bs (decr (disj ?md ?qd)))" by (simp only: decr [OF mdqdb]) 
  3.1886 +  finally have mdqd2: "?lhs = (Ifm bbs bs (decr (disj ?md ?qd)))" . 
  3.1887 +  {assume mdT: "?md = T"
  3.1888 +    hence cT:"cooper p = T" 
  3.1889 +      by (simp only: cooper_def unit_def split_def Let_def if_True) simp
  3.1890 +    from mdT have lhs:"?lhs" using mdqd by simp 
  3.1891 +    from mdT have "?rhs" by (simp add: cooper_def unit_def split_def)
  3.1892 +    with lhs cT have ?thesis by simp }
  3.1893 +  moreover
  3.1894 +  {assume mdT: "?md \<noteq> T" hence "cooper p = decr (disj ?md ?qd)" 
  3.1895 +      by (simp only: cooper_def unit_def split_def Let_def if_False) 
  3.1896 +    with mdqd2 decr_qf[OF mdqdb] have ?thesis by simp }
  3.1897 +  ultimately show ?thesis by blast
  3.1898 +qed
  3.1899 +
  3.1900 +definition pa :: "fm \<Rightarrow> fm" where
  3.1901 +  "pa p = qelim (prep p) cooper"
  3.1902 +
  3.1903 +theorem mirqe: "(Ifm bbs bs (pa p) = Ifm bbs bs p) \<and> qfree (pa p)"
  3.1904 +  using qelim_ci cooper prep by (auto simp add: pa_def)
  3.1905 +
  3.1906 +definition
  3.1907 +  cooper_test :: "unit \<Rightarrow> fm"
  3.1908 +where
  3.1909 +  "cooper_test u = pa (E (A (Imp (Ge (Sub (Bound 0) (Bound 1)))
  3.1910 +    (E (E (Eq (Sub (Add (Mul 3 (Bound 1)) (Mul 5 (Bound 0)))
  3.1911 +      (Bound 2))))))))"
  3.1912 +
  3.1913 +ML {* @{code cooper_test} () *}
  3.1914 +
  3.1915 +(*
  3.1916 +code_reserved SML oo
  3.1917 +export_code pa in SML module_name GeneratedCooper file "~~/src/HOL/Tools/Qelim/raw_generated_cooper.ML"
  3.1918 +*)
  3.1919 +
  3.1920 +oracle linzqe_oracle = {*
  3.1921 +let
  3.1922 +
  3.1923 +fun num_of_term vs (t as Free (xn, xT)) = (case AList.lookup (op =) vs t
  3.1924 +     of NONE => error "Variable not found in the list!"
  3.1925 +      | SOME n => @{code Bound} n)
  3.1926 +  | num_of_term vs @{term "0::int"} = @{code C} 0
  3.1927 +  | num_of_term vs @{term "1::int"} = @{code C} 1
  3.1928 +  | num_of_term vs (@{term "number_of :: int \<Rightarrow> int"} $ t) = @{code C} (HOLogic.dest_numeral t)
  3.1929 +  | num_of_term vs (Bound i) = @{code Bound} i
  3.1930 +  | num_of_term vs (@{term "uminus :: int \<Rightarrow> int"} $ t') = @{code Neg} (num_of_term vs t')
  3.1931 +  | num_of_term vs (@{term "op + :: int \<Rightarrow> int \<Rightarrow> int"} $ t1 $ t2) =
  3.1932 +      @{code Add} (num_of_term vs t1, num_of_term vs t2)
  3.1933 +  | num_of_term vs (@{term "op - :: int \<Rightarrow> int \<Rightarrow> int"} $ t1 $ t2) =
  3.1934 +      @{code Sub} (num_of_term vs t1, num_of_term vs t2)
  3.1935 +  | num_of_term vs (@{term "op * :: int \<Rightarrow> int \<Rightarrow> int"} $ t1 $ t2) =
  3.1936 +      (case try HOLogic.dest_number t1
  3.1937 +       of SOME (_, i) => @{code Mul} (i, num_of_term vs t2)
  3.1938 +        | NONE => (case try HOLogic.dest_number t2
  3.1939 +                of SOME (_, i) => @{code Mul} (i, num_of_term vs t1)
  3.1940 +                 | NONE => error "num_of_term: unsupported multiplication"))
  3.1941 +  | num_of_term vs t = error ("num_of_term: unknown term " ^ Syntax.string_of_term @{context} t);
  3.1942 +
  3.1943 +fun fm_of_term ps vs @{term True} = @{code T}
  3.1944 +  | fm_of_term ps vs @{term False} = @{code F}
  3.1945 +  | fm_of_term ps vs (@{term "op < :: int \<Rightarrow> int \<Rightarrow> bool"} $ t1 $ t2) =
  3.1946 +      @{code Lt} (@{code Sub} (num_of_term vs t1, num_of_term vs t2))
  3.1947 +  | fm_of_term ps vs (@{term "op \<le> :: int \<Rightarrow> int \<Rightarrow> bool"} $ t1 $ t2) =
  3.1948 +      @{code Le} (@{code Sub} (num_of_term vs t1, num_of_term vs t2))
  3.1949 +  | fm_of_term ps vs (@{term "op = :: int \<Rightarrow> int \<Rightarrow> bool"} $ t1 $ t2) =
  3.1950 +      @{code Eq} (@{code Sub} (num_of_term vs t1, num_of_term vs t2)) 
  3.1951 +  | fm_of_term ps vs (@{term "op dvd :: int \<Rightarrow> int \<Rightarrow> bool"} $ t1 $ t2) =
  3.1952 +      (case try HOLogic.dest_number t1
  3.1953 +       of SOME (_, i) => @{code Dvd} (i, num_of_term vs t2)
  3.1954 +        | NONE => error "num_of_term: unsupported dvd")
  3.1955 +  | fm_of_term ps vs (@{term "op = :: bool \<Rightarrow> bool \<Rightarrow> bool"} $ t1 $ t2) =
  3.1956 +      @{code Iff} (fm_of_term ps vs t1, fm_of_term ps vs t2)
  3.1957 +  | fm_of_term ps vs (@{term "op &"} $ t1 $ t2) =
  3.1958 +      @{code And} (fm_of_term ps vs t1, fm_of_term ps vs t2)
  3.1959 +  | fm_of_term ps vs (@{term "op |"} $ t1 $ t2) =
  3.1960 +      @{code Or} (fm_of_term ps vs t1, fm_of_term ps vs t2)
  3.1961 +  | fm_of_term ps vs (@{term "op -->"} $ t1 $ t2) =
  3.1962 +      @{code Imp} (fm_of_term ps vs t1, fm_of_term ps vs t2)
  3.1963 +  | fm_of_term ps vs (@{term "Not"} $ t') =
  3.1964 +      @{code NOT} (fm_of_term ps vs t')
  3.1965 +  | fm_of_term ps vs (Const ("Ex", _) $ Abs (xn, xT, p)) =
  3.1966 +      let
  3.1967 +        val (xn', p') = variant_abs (xn, xT, p);
  3.1968 +        val vs' = (Free (xn', xT), 0) :: map (fn (v, n) => (v, n + 1)) vs;
  3.1969 +      in @{code E} (fm_of_term ps vs' p) end
  3.1970 +  | fm_of_term ps vs (Const ("All", _) $ Abs (xn, xT, p)) =
  3.1971 +      let
  3.1972 +        val (xn', p') = variant_abs (xn, xT, p);
  3.1973 +        val vs' = (Free (xn', xT), 0) :: map (fn (v, n) => (v, n + 1)) vs;
  3.1974 +      in @{code A} (fm_of_term ps vs' p) end
  3.1975 +  | fm_of_term ps vs t = error ("fm_of_term : unknown term " ^ Syntax.string_of_term @{context} t);
  3.1976 +
  3.1977 +fun term_of_num vs (@{code C} i) = HOLogic.mk_number HOLogic.intT i
  3.1978 +  | term_of_num vs (@{code Bound} n) = fst (the (find_first (fn (_, m) => n = m) vs))
  3.1979 +  | term_of_num vs (@{code Neg} t') = @{term "uminus :: int \<Rightarrow> int"} $ term_of_num vs t'
  3.1980 +  | term_of_num vs (@{code Add} (t1, t2)) = @{term "op + :: int \<Rightarrow> int \<Rightarrow> int"} $
  3.1981 +      term_of_num vs t1 $ term_of_num vs t2
  3.1982 +  | term_of_num vs (@{code Sub} (t1, t2)) = @{term "op - :: int \<Rightarrow> int \<Rightarrow> int"} $
  3.1983 +      term_of_num vs t1 $ term_of_num vs t2
  3.1984 +  | term_of_num vs (@{code Mul} (i, t2)) = @{term "op * :: int \<Rightarrow> int \<Rightarrow> int"} $
  3.1985 +      term_of_num vs (@{code C} i) $ term_of_num vs t2
  3.1986 +  | term_of_num vs (@{code CN} (n, i, t)) = term_of_num vs (@{code Add} (@{code Mul} (i, @{code Bound} n), t));
  3.1987 +
  3.1988 +fun term_of_fm ps vs @{code T} = HOLogic.true_const 
  3.1989 +  | term_of_fm ps vs @{code F} = HOLogic.false_const
  3.1990 +  | term_of_fm ps vs (@{code Lt} t) =
  3.1991 +      @{term "op < :: int \<Rightarrow> int \<Rightarrow> bool"} $ term_of_num vs t $ @{term "0::int"}
  3.1992 +  | term_of_fm ps vs (@{code Le} t) =
  3.1993 +      @{term "op \<le> :: int \<Rightarrow> int \<Rightarrow> bool"} $ term_of_num vs t $ @{term "0::int"}
  3.1994 +  | term_of_fm ps vs (@{code Gt} t) =
  3.1995 +      @{term "op < :: int \<Rightarrow> int \<Rightarrow> bool"} $ @{term "0::int"} $ term_of_num vs t
  3.1996 +  | term_of_fm ps vs (@{code Ge} t) =
  3.1997 +      @{term "op \<le> :: int \<Rightarrow> int \<Rightarrow> bool"} $ @{term "0::int"} $ term_of_num vs t
  3.1998 +  | term_of_fm ps vs (@{code Eq} t) =
  3.1999 +      @{term "op = :: int \<Rightarrow> int \<Rightarrow> bool"} $ term_of_num vs t $ @{term "0::int"}
  3.2000 +  | term_of_fm ps vs (@{code NEq} t) =
  3.2001 +      term_of_fm ps vs (@{code NOT} (@{code Eq} t))
  3.2002 +  | term_of_fm ps vs (@{code Dvd} (i, t)) =
  3.2003 +      @{term "op dvd :: int \<Rightarrow> int \<Rightarrow> bool"} $ term_of_num vs (@{code C} i) $ term_of_num vs t
  3.2004 +  | term_of_fm ps vs (@{code NDvd} (i, t)) =
  3.2005 +      term_of_fm ps vs (@{code NOT} (@{code Dvd} (i, t)))
  3.2006 +  | term_of_fm ps vs (@{code NOT} t') =
  3.2007 +      HOLogic.Not $ term_of_fm ps vs t'
  3.2008 +  | term_of_fm ps vs (@{code And} (t1, t2)) =
  3.2009 +      HOLogic.conj $ term_of_fm ps vs t1 $ term_of_fm ps vs t2
  3.2010 +  | term_of_fm ps vs (@{code Or} (t1, t2)) =
  3.2011 +      HOLogic.disj $ term_of_fm ps vs t1 $ term_of_fm ps vs t2
  3.2012 +  | term_of_fm ps vs (@{code Imp} (t1, t2)) =
  3.2013 +      HOLogic.imp $ term_of_fm ps vs t1 $ term_of_fm ps vs t2
  3.2014 +  | term_of_fm ps vs (@{code Iff} (t1, t2)) =
  3.2015 +      @{term "op = :: bool \<Rightarrow> bool \<Rightarrow> bool"} $ term_of_fm ps vs t1 $ term_of_fm ps vs t2
  3.2016 +  | term_of_fm ps vs (@{code Closed} n) = (fst o the) (find_first (fn (_, m) => m = n) ps)
  3.2017 +  | term_of_fm ps vs (@{code NClosed} n) = term_of_fm ps vs (@{code NOT} (@{code Closed} n));
  3.2018 +
  3.2019 +fun term_bools acc t =
  3.2020 +  let
  3.2021 +    val is_op = member (op =) [@{term "op &"}, @{term "op |"}, @{term "op -->"}, @{term "op = :: bool => _"},
  3.2022 +      @{term "op = :: int => _"}, @{term "op < :: int => _"},
  3.2023 +      @{term "op <= :: int => _"}, @{term "Not"}, @{term "All :: (int => _) => _"},
  3.2024 +      @{term "Ex :: (int => _) => _"}, @{term "True"}, @{term "False"}]
  3.2025 +    fun is_ty t = not (fastype_of t = HOLogic.boolT) 
  3.2026 +  in case t
  3.2027 +   of (l as f $ a) $ b => if is_ty t orelse is_op t then term_bools (term_bools acc l)b 
  3.2028 +        else insert (op aconv) t acc
  3.2029 +    | f $ a => if is_ty t orelse is_op t then term_bools (term_bools acc f) a  
  3.2030 +        else insert (op aconv) t acc
  3.2031 +    | Abs p => term_bools acc (snd (variant_abs p))
  3.2032 +    | _ => if is_ty t orelse is_op t then acc else insert (op aconv) t acc
  3.2033 +  end;
  3.2034 +
  3.2035 +in fn ct =>
  3.2036 +  let
  3.2037 +    val thy = Thm.theory_of_cterm ct;
  3.2038 +    val t = Thm.term_of ct;
  3.2039 +    val fs = OldTerm.term_frees t;
  3.2040 +    val bs = term_bools [] t;
  3.2041 +    val vs = fs ~~ (0 upto (length fs - 1))
  3.2042 +    val ps = bs ~~ (0 upto (length bs - 1))
  3.2043 +    val t' = (term_of_fm ps vs o @{code pa} o fm_of_term ps vs) t;
  3.2044 +  in (Thm.cterm_of thy o HOLogic.mk_Trueprop o HOLogic.mk_eq) (t, t') end
  3.2045 +end;
  3.2046 +*}
  3.2047 +
  3.2048 +use "cooper_tac.ML"
  3.2049 +setup "Cooper_Tac.setup"
  3.2050 +
  3.2051 +text {* Tests *}
  3.2052 +
  3.2053 +lemma "\<exists> (j::int). \<forall> x\<ge>j. (\<exists> a b. x = 3*a+5*b)"
  3.2054 +  by cooper
  3.2055 +
  3.2056 +lemma "ALL (x::int) >=8. EX i j. 5*i + 3*j = x"
  3.2057 +  by cooper
  3.2058 +
  3.2059 +theorem "(\<forall>(y::int). 3 dvd y) ==> \<forall>(x::int). b < x --> a \<le> x"
  3.2060 +  by cooper
  3.2061 +
  3.2062 +theorem "!! (y::int) (z::int) (n::int). 3 dvd z ==> 2 dvd (y::int) ==>
  3.2063 +  (\<exists>(x::int).  2*x =  y) & (\<exists>(k::int). 3*k = z)"
  3.2064 +  by cooper
  3.2065 +
  3.2066 +theorem "!! (y::int) (z::int) n. Suc(n::nat) < 6 ==>  3 dvd z ==>
  3.2067 +  2 dvd (y::int) ==> (\<exists>(x::int).  2*x =  y) & (\<exists>(k::int). 3*k = z)"
  3.2068 +  by cooper
  3.2069 +
  3.2070 +theorem "\<forall>(x::nat). \<exists>(y::nat). (0::nat) \<le> 5 --> y = 5 + x "
  3.2071 +  by cooper
  3.2072 +
  3.2073 +lemma "ALL (x::int) >=8. EX i j. 5*i + 3*j = x"
  3.2074 +  by cooper 
  3.2075 +
  3.2076 +lemma "ALL (y::int) (z::int) (n::int). 3 dvd z --> 2 dvd (y::int) --> (EX (x::int).  2*x =  y) & (EX (k::int). 3*k = z)"
  3.2077 +  by cooper
  3.2078 +
  3.2079 +lemma "ALL(x::int) y. x < y --> 2 * x + 1 < 2 * y"
  3.2080 +  by cooper
  3.2081 +
  3.2082 +lemma "ALL(x::int) y. 2 * x + 1 ~= 2 * y"
  3.2083 +  by cooper
  3.2084 +
  3.2085 +lemma "EX(x::int) y. 0 < x  & 0 <= y  & 3 * x - 5 * y = 1"
  3.2086 +  by cooper
  3.2087 +
  3.2088 +lemma "~ (EX(x::int) (y::int) (z::int). 4*x + (-6::int)*y = 1)"
  3.2089 +  by cooper
  3.2090 +
  3.2091 +lemma "ALL(x::int). (2 dvd x) --> (EX(y::int). x = 2*y)"
  3.2092 +  by cooper
  3.2093 +
  3.2094 +lemma "ALL(x::int). (2 dvd x) = (EX(y::int). x = 2*y)"
  3.2095 +  by cooper
  3.2096 +
  3.2097 +lemma "ALL(x::int). ((2 dvd x) = (ALL(y::int). x ~= 2*y + 1))"
  3.2098 +  by cooper
  3.2099 +
  3.2100 +lemma "~ (ALL(x::int). ((2 dvd x) = (ALL(y::int). x ~= 2*y+1) | (EX(q::int) (u::int) i. 3*i + 2*q - u < 17) --> 0 < x | ((~ 3 dvd x) &(x + 8 = 0))))"
  3.2101 +  by cooper
  3.2102 +
  3.2103 +lemma "~ (ALL(i::int). 4 <= i --> (EX x y. 0 <= x & 0 <= y & 3 * x + 5 * y = i))" 
  3.2104 +  by cooper
  3.2105 +
  3.2106 +lemma "EX j. ALL (x::int) >= j. EX i j. 5*i + 3*j = x"
  3.2107 +  by cooper
  3.2108 +
  3.2109 +theorem "(\<forall>(y::int). 3 dvd y) ==> \<forall>(x::int). b < x --> a \<le> x"
  3.2110 +  by cooper
  3.2111 +
  3.2112 +theorem "!! (y::int) (z::int) (n::int). 3 dvd z ==> 2 dvd (y::int) ==>
  3.2113 +  (\<exists>(x::int).  2*x =  y) & (\<exists>(k::int). 3*k = z)"
  3.2114 +  by cooper
  3.2115 +
  3.2116 +theorem "!! (y::int) (z::int) n. Suc(n::nat) < 6 ==>  3 dvd z ==>
  3.2117 +  2 dvd (y::int) ==> (\<exists>(x::int).  2*x =  y) & (\<exists>(k::int). 3*k = z)"
  3.2118 +  by cooper
  3.2119 +
  3.2120 +theorem "\<forall>(x::nat). \<exists>(y::nat). (0::nat) \<le> 5 --> y = 5 + x "
  3.2121 +  by cooper
  3.2122 +
  3.2123 +theorem "\<forall>(x::nat). \<exists>(y::nat). y = 5 + x | x div 6 + 1= 2"
  3.2124 +  by cooper
  3.2125 +
  3.2126 +theorem "\<exists>(x::int). 0 < x"
  3.2127 +  by cooper
  3.2128 +
  3.2129 +theorem "\<forall>(x::int) y. x < y --> 2 * x + 1 < 2 * y"
  3.2130 +  by cooper
  3.2131 + 
  3.2132 +theorem "\<forall>(x::int) y. 2 * x + 1 \<noteq> 2 * y"
  3.2133 +  by cooper
  3.2134 + 
  3.2135 +theorem "\<exists>(x::int) y. 0 < x  & 0 \<le> y  & 3 * x - 5 * y = 1"
  3.2136 +  by cooper
  3.2137 +
  3.2138 +theorem "~ (\<exists>(x::int) (y::int) (z::int). 4*x + (-6::int)*y = 1)"
  3.2139 +  by cooper
  3.2140 +
  3.2141 +theorem "~ (\<exists>(x::int). False)"
  3.2142 +  by cooper
  3.2143 +
  3.2144 +theorem "\<forall>(x::int). (2 dvd x) --> (\<exists>(y::int). x = 2*y)"
  3.2145 +  by cooper 
  3.2146 +
  3.2147 +theorem "\<forall>(x::int). (2 dvd x) --> (\<exists>(y::int). x = 2*y)"
  3.2148 +  by cooper 
  3.2149 +
  3.2150 +theorem "\<forall>(x::int). (2 dvd x) = (\<exists>(y::int). x = 2*y)"
  3.2151 +  by cooper 
  3.2152 +
  3.2153 +theorem "\<forall>(x::int). ((2 dvd x) = (\<forall>(y::int). x \<noteq> 2*y + 1))"
  3.2154 +  by cooper 
  3.2155 +
  3.2156 +theorem "~ (\<forall>(x::int). 
  3.2157 +            ((2 dvd x) = (\<forall>(y::int). x \<noteq> 2*y+1) | 
  3.2158 +             (\<exists>(q::int) (u::int) i. 3*i + 2*q - u < 17)
  3.2159 +             --> 0 < x | ((~ 3 dvd x) &(x + 8 = 0))))"
  3.2160 +  by cooper
  3.2161 + 
  3.2162 +theorem "~ (\<forall>(i::int). 4 \<le> i --> (\<exists>x y. 0 \<le> x & 0 \<le> y & 3 * x + 5 * y = i))"
  3.2163 +  by cooper
  3.2164 +
  3.2165 +theorem "\<forall>(i::int). 8 \<le> i --> (\<exists>x y. 0 \<le> x & 0 \<le> y & 3 * x + 5 * y = i)"
  3.2166 +  by cooper
  3.2167 +
  3.2168 +theorem "\<exists>(j::int). \<forall>i. j \<le> i --> (\<exists>x y. 0 \<le> x & 0 \<le> y & 3 * x + 5 * y = i)"
  3.2169 +  by cooper
  3.2170 +
  3.2171 +theorem "~ (\<forall>j (i::int). j \<le> i --> (\<exists>x y. 0 \<le> x & 0 \<le> y & 3 * x + 5 * y = i))"
  3.2172 +  by cooper
  3.2173 +
  3.2174 +theorem "(\<exists>m::nat. n = 2 * m) --> (n + 1) div 2 = n div 2"
  3.2175 +  by cooper
  3.2176 +
  3.2177 +end
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/HOL/Decision_Procs/Dense_Linear_Order.thy	Fri Feb 06 15:15:32 2009 +0100
     4.3 @@ -0,0 +1,879 @@
     4.4 +(*  Title       : HOL/Dense_Linear_Order.thy
     4.5 +    Author      : Amine Chaieb, TU Muenchen
     4.6 +*)
     4.7 +
     4.8 +header {* Dense linear order without endpoints
     4.9 +  and a quantifier elimination procedure in Ferrante and Rackoff style *}
    4.10 +
    4.11 +theory Dense_Linear_Order
    4.12 +imports Plain Groebner_Basis Main
    4.13 +uses
    4.14 +  "~~/src/HOL/Tools/Qelim/langford_data.ML"
    4.15 +  "~~/src/HOL/Tools/Qelim/ferrante_rackoff_data.ML"
    4.16 +  ("~~/src/HOL/Tools/Qelim/langford.ML")
    4.17 +  ("~~/src/HOL/Tools/Qelim/ferrante_rackoff.ML")
    4.18 +begin
    4.19 +
    4.20 +setup {* Langford_Data.setup #> Ferrante_Rackoff_Data.setup *}
    4.21 +
    4.22 +context linorder
    4.23 +begin
    4.24 +
    4.25 +lemma less_not_permute[noatp]: "\<not> (x < y \<and> y < x)" by (simp add: not_less linear)
    4.26 +
    4.27 +lemma gather_simps[noatp]: 
    4.28 +  shows 
    4.29 +  "(\<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)"
    4.30 +  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)"
    4.31 +  "(\<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))"
    4.32 +  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
    4.33 +
    4.34 +lemma 
    4.35 +  gather_start[noatp]: "(\<exists>x. P x) \<equiv> (\<exists>x. (\<forall>y \<in> {}. y < x) \<and> (\<forall>y\<in> {}. x < y) \<and> P x)" 
    4.36 +  by simp
    4.37 +
    4.38 +text{* Theorems for @{text "\<exists>z. \<forall>x. x < z \<longrightarrow> (P x \<longleftrightarrow> P\<^bsub>-\<infinity>\<^esub>)"}*}
    4.39 +lemma minf_lt[noatp]:  "\<exists>z . \<forall>x. x < z \<longrightarrow> (x < t \<longleftrightarrow> True)" by auto
    4.40 +lemma minf_gt[noatp]: "\<exists>z . \<forall>x. x < z \<longrightarrow>  (t < x \<longleftrightarrow>  False)"
    4.41 +  by (simp add: not_less) (rule exI[where x="t"], auto simp add: less_le)
    4.42 +
    4.43 +lemma minf_le[noatp]: "\<exists>z. \<forall>x. x < z \<longrightarrow> (x \<le> t \<longleftrightarrow> True)" by (auto simp add: less_le)
    4.44 +lemma minf_ge[noatp]: "\<exists>z. \<forall>x. x < z \<longrightarrow> (t \<le> x \<longleftrightarrow> False)"
    4.45 +  by (auto simp add: less_le not_less not_le)
    4.46 +lemma minf_eq[noatp]: "\<exists>z. \<forall>x. x < z \<longrightarrow> (x = t \<longleftrightarrow> False)" by auto
    4.47 +lemma minf_neq[noatp]: "\<exists>z. \<forall>x. x < z \<longrightarrow> (x \<noteq> t \<longleftrightarrow> True)" by auto
    4.48 +lemma minf_P[noatp]: "\<exists>z. \<forall>x. x < z \<longrightarrow> (P \<longleftrightarrow> P)" by blast
    4.49 +
    4.50 +text{* Theorems for @{text "\<exists>z. \<forall>x. x < z \<longrightarrow> (P x \<longleftrightarrow> P\<^bsub>+\<infinity>\<^esub>)"}*}
    4.51 +lemma pinf_gt[noatp]:  "\<exists>z . \<forall>x. z < x \<longrightarrow> (t < x \<longleftrightarrow> True)" by auto
    4.52 +lemma pinf_lt[noatp]: "\<exists>z . \<forall>x. z < x \<longrightarrow>  (x < t \<longleftrightarrow>  False)"
    4.53 +  by (simp add: not_less) (rule exI[where x="t"], auto simp add: less_le)
    4.54 +
    4.55 +lemma pinf_ge[noatp]: "\<exists>z. \<forall>x. z < x \<longrightarrow> (t \<le> x \<longleftrightarrow> True)" by (auto simp add: less_le)
    4.56 +lemma pinf_le[noatp]: "\<exists>z. \<forall>x. z < x \<longrightarrow> (x \<le> t \<longleftrightarrow> False)"
    4.57 +  by (auto simp add: less_le not_less not_le)
    4.58 +lemma pinf_eq[noatp]: "\<exists>z. \<forall>x. z < x \<longrightarrow> (x = t \<longleftrightarrow> False)" by auto
    4.59 +lemma pinf_neq[noatp]: "\<exists>z. \<forall>x. z < x \<longrightarrow> (x \<noteq> t \<longleftrightarrow> True)" by auto
    4.60 +lemma pinf_P[noatp]: "\<exists>z. \<forall>x. z < x \<longrightarrow> (P \<longleftrightarrow> P)" by blast
    4.61 +
    4.62 +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
    4.63 +lemma nmi_gt[noatp]: "t \<in> U \<Longrightarrow> \<forall>x. \<not>False \<and> t < x \<longrightarrow>  (\<exists> u\<in> U. u \<le> x)"
    4.64 +  by (auto simp add: le_less)
    4.65 +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
    4.66 +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
    4.67 +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
    4.68 +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
    4.69 +lemma  nmi_P[noatp]: "\<forall> x. ~P \<and> P \<longrightarrow>  (\<exists> u\<in> U. u \<le> x)" by auto
    4.70 +lemma  nmi_conj[noatp]: "\<lbrakk>\<forall>x. \<not>P1' \<and> P1 x \<longrightarrow>  (\<exists> u\<in> U. u \<le> x) ;
    4.71 +  \<forall>x. \<not>P2' \<and> P2 x \<longrightarrow>  (\<exists> u\<in> U. u \<le> x)\<rbrakk> \<Longrightarrow>
    4.72 +  \<forall>x. \<not>(P1' \<and> P2') \<and> (P1 x \<and> P2 x) \<longrightarrow>  (\<exists> u\<in> U. u \<le> x)" by auto
    4.73 +lemma  nmi_disj[noatp]: "\<lbrakk>\<forall>x. \<not>P1' \<and> P1 x \<longrightarrow>  (\<exists> u\<in> U. u \<le> x) ;
    4.74 +  \<forall>x. \<not>P2' \<and> P2 x \<longrightarrow>  (\<exists> u\<in> U. u \<le> x)\<rbrakk> \<Longrightarrow>
    4.75 +  \<forall>x. \<not>(P1' \<or> P2') \<and> (P1 x \<or> P2 x) \<longrightarrow>  (\<exists> u\<in> U. u \<le> x)" by auto
    4.76 +
    4.77 +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)
    4.78 +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
    4.79 +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
    4.80 +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
    4.81 +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
    4.82 +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
    4.83 +lemma  npi_P[noatp]: "\<forall> x. ~P \<and> P \<longrightarrow>  (\<exists> u\<in> U. x \<le> u)" by auto
    4.84 +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>
    4.85 +  \<Longrightarrow>  \<forall>x. \<not>(P1' \<and> P2') \<and> (P1 x \<and> P2 x) \<longrightarrow>  (\<exists> u\<in> U. x \<le> u)" by auto
    4.86 +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>
    4.87 +  \<Longrightarrow> \<forall>x. \<not>(P1' \<or> P2') \<and> (P1 x \<or> P2 x) \<longrightarrow>  (\<exists> u\<in> U. x \<le> u)" by auto
    4.88 +
    4.89 +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)"
    4.90 +proof(clarsimp)
    4.91 +  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"
    4.92 +    and xu: "x<u"  and px: "x < t" and ly: "l<y" and yu:"y < u"
    4.93 +  from tU noU ly yu have tny: "t\<noteq>y" by auto
    4.94 +  {assume H: "t < y"
    4.95 +    from less_trans[OF lx px] less_trans[OF H yu]
    4.96 +    have "l < t \<and> t < u"  by simp
    4.97 +    with tU noU have "False" by auto}
    4.98 +  hence "\<not> t < y"  by auto hence "y \<le> t" by (simp add: not_less)
    4.99 +  thus "y < t" using tny by (simp add: less_le)
   4.100 +qed
   4.101 +
   4.102 +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)"
   4.103 +proof(clarsimp)
   4.104 +  fix x l u y
   4.105 +  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"
   4.106 +  and px: "t < x" and ly: "l<y" and yu:"y < u"
   4.107 +  from tU noU ly yu have tny: "t\<noteq>y" by auto
   4.108 +  {assume H: "y< t"
   4.109 +    from less_trans[OF ly H] less_trans[OF px xu] have "l < t \<and> t < u" by simp
   4.110 +    with tU noU have "False" by auto}
   4.111 +  hence "\<not> y<t"  by auto hence "t \<le> y" by (auto simp add: not_less)
   4.112 +  thus "t < y" using tny by (simp add:less_le)
   4.113 +qed
   4.114 +
   4.115 +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)"
   4.116 +proof(clarsimp)
   4.117 +  fix x l u y
   4.118 +  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"
   4.119 +  and px: "x \<le> t" and ly: "l<y" and yu:"y < u"
   4.120 +  from tU noU ly yu have tny: "t\<noteq>y" by auto
   4.121 +  {assume H: "t < y"
   4.122 +    from less_le_trans[OF lx px] less_trans[OF H yu]
   4.123 +    have "l < t \<and> t < u" by simp
   4.124 +    with tU noU have "False" by auto}
   4.125 +  hence "\<not> t < y"  by auto thus "y \<le> t" by (simp add: not_less)
   4.126 +qed
   4.127 +
   4.128 +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)"
   4.129 +proof(clarsimp)
   4.130 +  fix x l u y
   4.131 +  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"
   4.132 +  and px: "t \<le> x" and ly: "l<y" and yu:"y < u"
   4.133 +  from tU noU ly yu have tny: "t\<noteq>y" by auto
   4.134 +  {assume H: "y< t"
   4.135 +    from less_trans[OF ly H] le_less_trans[OF px xu]
   4.136 +    have "l < t \<and> t < u" by simp
   4.137 +    with tU noU have "False" by auto}
   4.138 +  hence "\<not> y<t"  by auto thus "t \<le> y" by (simp add: not_less)
   4.139 +qed
   4.140 +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
   4.141 +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
   4.142 +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
   4.143 +
   4.144 +lemma lin_dense_conj[noatp]:
   4.145 +  "\<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
   4.146 +  \<longrightarrow> (\<forall> y. l < y \<and> y < u \<longrightarrow> P1 y) ;
   4.147 +  \<forall>x l u. (\<forall> t. l < t \<and> t< u \<longrightarrow> t \<notin> U) \<and> l< x \<and> x < u \<and> P2 x
   4.148 +  \<longrightarrow> (\<forall> y. l < y \<and> y < u \<longrightarrow> P2 y)\<rbrakk> \<Longrightarrow>
   4.149 +  \<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)
   4.150 +  \<longrightarrow> (\<forall> y. l < y \<and> y < u \<longrightarrow> (P1 y \<and> P2 y))"
   4.151 +  by blast
   4.152 +lemma lin_dense_disj[noatp]:
   4.153 +  "\<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
   4.154 +  \<longrightarrow> (\<forall> y. l < y \<and> y < u \<longrightarrow> P1 y) ;
   4.155 +  \<forall>x l u. (\<forall> t. l < t \<and> t< u \<longrightarrow> t \<notin> U) \<and> l< x \<and> x < u \<and> P2 x
   4.156 +  \<longrightarrow> (\<forall> y. l < y \<and> y < u \<longrightarrow> P2 y)\<rbrakk> \<Longrightarrow>
   4.157 +  \<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)
   4.158 +  \<longrightarrow> (\<forall> y. l < y \<and> y < u \<longrightarrow> (P1 y \<or> P2 y))"
   4.159 +  by blast
   4.160 +
   4.161 +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>
   4.162 +  \<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')"
   4.163 +by auto
   4.164 +
   4.165 +lemma finite_set_intervals[noatp]:
   4.166 +  assumes px: "P x" and lx: "l \<le> x" and xu: "x \<le> u" and linS: "l\<in> S"
   4.167 +  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"
   4.168 +  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"
   4.169 +proof-
   4.170 +  let ?Mx = "{y. y\<in> S \<and> y \<le> x}"
   4.171 +  let ?xM = "{y. y\<in> S \<and> x \<le> y}"
   4.172 +  let ?a = "Max ?Mx"
   4.173 +  let ?b = "Min ?xM"
   4.174 +  have MxS: "?Mx \<subseteq> S" by blast
   4.175 +  hence fMx: "finite ?Mx" using fS finite_subset by auto
   4.176 +  from lx linS have linMx: "l \<in> ?Mx" by blast
   4.177 +  hence Mxne: "?Mx \<noteq> {}" by blast
   4.178 +  have xMS: "?xM \<subseteq> S" by blast
   4.179 +  hence fxM: "finite ?xM" using fS finite_subset by auto
   4.180 +  from xu uinS have linxM: "u \<in> ?xM" by blast
   4.181 +  hence xMne: "?xM \<noteq> {}" by blast
   4.182 +  have ax:"?a \<le> x" using Mxne fMx by auto
   4.183 +  have xb:"x \<le> ?b" using xMne fxM by auto
   4.184 +  have "?a \<in> ?Mx" using Max_in[OF fMx Mxne] by simp hence ainS: "?a \<in> S" using MxS by blast
   4.185 +  have "?b \<in> ?xM" using Min_in[OF fxM xMne] by simp hence binS: "?b \<in> S" using xMS by blast
   4.186 +  have noy:"\<forall> y. ?a < y \<and> y < ?b \<longrightarrow> y \<notin> S"
   4.187 +  proof(clarsimp)
   4.188 +    fix y   assume ay: "?a < y" and yb: "y < ?b" and yS: "y \<in> S"
   4.189 +    from yS have "y\<in> ?Mx \<or> y\<in> ?xM" by (auto simp add: linear)
   4.190 +    moreover {assume "y \<in> ?Mx" hence "y \<le> ?a" using Mxne fMx by auto with ay have "False" by (simp add: not_le[symmetric])}
   4.191 +    moreover {assume "y \<in> ?xM" hence "?b \<le> y" using xMne fxM by auto with yb have "False" by (simp add: not_le[symmetric])}
   4.192 +    ultimately show "False" by blast
   4.193 +  qed
   4.194 +  from ainS binS noy ax xb px show ?thesis by blast
   4.195 +qed
   4.196 +
   4.197 +lemma finite_set_intervals2[noatp]:
   4.198 +  assumes px: "P x" and lx: "l \<le> x" and xu: "x \<le> u" and linS: "l\<in> S"
   4.199 +  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"
   4.200 +  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)"
   4.201 +proof-
   4.202 +  from finite_set_intervals[where P="P", OF px lx xu linS uinS fS lS Su]
   4.203 +  obtain a and b where
   4.204 +    as: "a\<in> S" and bs: "b\<in> S" and noS:"\<forall>y. a < y \<and> y < b \<longrightarrow> y \<notin> S"
   4.205 +    and axb: "a \<le> x \<and> x \<le> b \<and> P x"  by auto
   4.206 +  from axb have "x= a \<or> x= b \<or> (a < x \<and> x < b)" by (auto simp add: le_less)
   4.207 +  thus ?thesis using px as bs noS by blast
   4.208 +qed
   4.209 +
   4.210 +end
   4.211 +
   4.212 +section {* The classical QE after Langford for dense linear orders *}
   4.213 +
   4.214 +context dense_linear_order
   4.215 +begin
   4.216 +
   4.217 +lemma interval_empty_iff:
   4.218 +  "{y. x < y \<and> y < z} = {} \<longleftrightarrow> \<not> x < z"
   4.219 +  by (auto dest: dense)
   4.220 +
   4.221 +lemma dlo_qe_bnds[noatp]: 
   4.222 +  assumes ne: "L \<noteq> {}" and neU: "U \<noteq> {}" and fL: "finite L" and fU: "finite U"
   4.223 +  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)"
   4.224 +proof (simp only: atomize_eq, rule iffI)
   4.225 +  assume H: "\<exists>x. (\<forall>y\<in>L. y < x) \<and> (\<forall>y\<in>U. x < y)"
   4.226 +  then obtain x where xL: "\<forall>y\<in>L. y < x" and xU: "\<forall>y\<in>U. x < y" by blast
   4.227 +  {fix l u assume l: "l \<in> L" and u: "u \<in> U"
   4.228 +    have "l < x" using xL l by blast
   4.229 +    also have "x < u" using xU u by blast
   4.230 +    finally (less_trans) have "l < u" .}
   4.231 +  thus "\<forall>l\<in>L. \<forall>u\<in>U. l < u" by blast
   4.232 +next
   4.233 +  assume H: "\<forall>l\<in>L. \<forall>u\<in>U. l < u"
   4.234 +  let ?ML = "Max L"
   4.235 +  let ?MU = "Min U"  
   4.236 +  from fL ne have th1: "?ML \<in> L" and th1': "\<forall>l\<in>L. l \<le> ?ML" by auto
   4.237 +  from fU neU have th2: "?MU \<in> U" and th2': "\<forall>u\<in>U. ?MU \<le> u" by auto
   4.238 +  from th1 th2 H have "?ML < ?MU" by auto
   4.239 +  with dense obtain w where th3: "?ML < w" and th4: "w < ?MU" by blast
   4.240 +  from th3 th1' have "\<forall>l \<in> L. l < w" by auto
   4.241 +  moreover from th4 th2' have "\<forall>u \<in> U. w < u" by auto
   4.242 +  ultimately show "\<exists>x. (\<forall>y\<in>L. y < x) \<and> (\<forall>y\<in>U. x < y)" by auto
   4.243 +qed
   4.244 +
   4.245 +lemma dlo_qe_noub[noatp]: 
   4.246 +  assumes ne: "L \<noteq> {}" and fL: "finite L"
   4.247 +  shows "(\<exists>x. (\<forall>y \<in> L. y < x) \<and> (\<forall>y \<in> {}. x < y)) \<equiv> True"
   4.248 +proof(simp add: atomize_eq)
   4.249 +  from gt_ex[of "Max L"] obtain M where M: "Max L < M" by blast
   4.250 +  from ne fL have "\<forall>x \<in> L. x \<le> Max L" by simp
   4.251 +  with M have "\<forall>x\<in>L. x < M" by (auto intro: le_less_trans)
   4.252 +  thus "\<exists>x. \<forall>y\<in>L. y < x" by blast
   4.253 +qed
   4.254 +
   4.255 +lemma dlo_qe_nolb[noatp]: 
   4.256 +  assumes ne: "U \<noteq> {}" and fU: "finite U"
   4.257 +  shows "(\<exists>x. (\<forall>y \<in> {}. y < x) \<and> (\<forall>y \<in> U. x < y)) \<equiv> True"
   4.258 +proof(simp add: atomize_eq)
   4.259 +  from lt_ex[of "Min U"] obtain M where M: "M < Min U" by blast
   4.260 +  from ne fU have "\<forall>x \<in> U. Min U \<le> x" by simp
   4.261 +  with M have "\<forall>x\<in>U. M < x" by (auto intro: less_le_trans)
   4.262 +  thus "\<exists>x. \<forall>y\<in>U. x < y" by blast
   4.263 +qed
   4.264 +
   4.265 +lemma exists_neq[noatp]: "\<exists>(x::'a). x \<noteq> t" "\<exists>(x::'a). t \<noteq> x" 
   4.266 +  using gt_ex[of t] by auto
   4.267 +
   4.268 +lemmas dlo_simps[noatp] = order_refl less_irrefl not_less not_le exists_neq 
   4.269 +  le_less neq_iff linear less_not_permute
   4.270 +
   4.271 +lemma axiom[noatp]: "dense_linear_order (op \<le>) (op <)" by (rule dense_linear_order_axioms)
   4.272 +lemma atoms[noatp]:
   4.273 +  shows "TERM (less :: 'a \<Rightarrow> _)"
   4.274 +    and "TERM (less_eq :: 'a \<Rightarrow> _)"
   4.275 +    and "TERM (op = :: 'a \<Rightarrow> _)" .
   4.276 +
   4.277 +declare axiom[langford qe: dlo_qe_bnds dlo_qe_nolb dlo_qe_noub gather: gather_start gather_simps atoms: atoms]
   4.278 +declare dlo_simps[langfordsimp]
   4.279 +
   4.280 +end
   4.281 +
   4.282 +(* FIXME: Move to HOL -- together with the conj_aci_rule in langford.ML *)
   4.283 +lemma dnf[noatp]:
   4.284 +  "(P & (Q | R)) = ((P&Q) | (P&R))" 
   4.285 +  "((Q | R) & P) = ((Q&P) | (R&P))"
   4.286 +  by blast+
   4.287 +
   4.288 +lemmas weak_dnf_simps[noatp] = simp_thms dnf
   4.289 +
   4.290 +lemma nnf_simps[noatp]:
   4.291 +    "(\<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)"
   4.292 +    "(P = Q) = ((P \<and> Q) \<or> (\<not>P \<and> \<not> Q))" "(\<not> \<not>(P)) = P"
   4.293 +  by blast+
   4.294 +
   4.295 +lemma ex_distrib[noatp]: "(\<exists>x. P x \<or> Q x) \<longleftrightarrow> ((\<exists>x. P x) \<or> (\<exists>x. Q x))" by blast
   4.296 +
   4.297 +lemmas dnf_simps[noatp] = weak_dnf_simps nnf_simps ex_distrib
   4.298 +
   4.299 +use "~~/src/HOL/Tools/Qelim/langford.ML"
   4.300 +method_setup dlo = {*
   4.301 +  Method.ctxt_args (Method.SIMPLE_METHOD' o LangfordQE.dlo_tac)
   4.302 +*} "Langford's algorithm for quantifier elimination in dense linear orders"
   4.303 +
   4.304 +
   4.305 +section {* Contructive dense linear orders yield QE for linear arithmetic over ordered Fields -- see @{text "Arith_Tools.thy"} *}
   4.306 +
   4.307 +text {* Linear order without upper bounds *}
   4.308 +
   4.309 +locale linorder_stupid_syntax = linorder
   4.310 +begin
   4.311 +notation
   4.312 +  less_eq  ("op \<sqsubseteq>") and
   4.313 +  less_eq  ("(_/ \<sqsubseteq> _)" [51, 51] 50) and
   4.314 +  less  ("op \<sqsubset>") and
   4.315 +  less  ("(_/ \<sqsubset> _)"  [51, 51] 50)
   4.316 +
   4.317 +end
   4.318 +
   4.319 +locale linorder_no_ub = linorder_stupid_syntax +
   4.320 +  assumes gt_ex: "\<exists>y. less x y"
   4.321 +begin
   4.322 +lemma ge_ex[noatp]: "\<exists>y. x \<sqsubseteq> y" using gt_ex by auto
   4.323 +
   4.324 +text {* Theorems for @{text "\<exists>z. \<forall>x. z \<sqsubset> x \<longrightarrow> (P x \<longleftrightarrow> P\<^bsub>+\<infinity>\<^esub>)"} *}
   4.325 +lemma pinf_conj[noatp]:
   4.326 +  assumes ex1: "\<exists>z1. \<forall>x. z1 \<sqsubset> x \<longrightarrow> (P1 x \<longleftrightarrow> P1')"
   4.327 +  and ex2: "\<exists>z2. \<forall>x. z2 \<sqsubset> x \<longrightarrow> (P2 x \<longleftrightarrow> P2')"
   4.328 +  shows "\<exists>z. \<forall>x. z \<sqsubset>  x \<longrightarrow> ((P1 x \<and> P2 x) \<longleftrightarrow> (P1' \<and> P2'))"
   4.329 +proof-
   4.330 +  from ex1 ex2 obtain z1 and z2 where z1: "\<forall>x. z1 \<sqsubset> x \<longrightarrow> (P1 x \<longleftrightarrow> P1')"
   4.331 +     and z2: "\<forall>x. z2 \<sqsubset> x \<longrightarrow> (P2 x \<longleftrightarrow> P2')" by blast
   4.332 +  from gt_ex obtain z where z:"ord.max less_eq z1 z2 \<sqsubset> z" by blast
   4.333 +  from z have zz1: "z1 \<sqsubset> z" and zz2: "z2 \<sqsubset> z" by simp_all
   4.334 +  {fix x assume H: "z \<sqsubset> x"
   4.335 +    from less_trans[OF zz1 H] less_trans[OF zz2 H]
   4.336 +    have "(P1 x \<and> P2 x) \<longleftrightarrow> (P1' \<and> P2')"  using z1 zz1 z2 zz2 by auto
   4.337 +  }
   4.338 +  thus ?thesis by blast
   4.339 +qed
   4.340 +
   4.341 +lemma pinf_disj[noatp]:
   4.342 +  assumes ex1: "\<exists>z1. \<forall>x. z1 \<sqsubset> x \<longrightarrow> (P1 x \<longleftrightarrow> P1')"
   4.343 +  and ex2: "\<exists>z2. \<forall>x. z2 \<sqsubset> x \<longrightarrow> (P2 x \<longleftrightarrow> P2')"
   4.344 +  shows "\<exists>z. \<forall>x. z \<sqsubset>  x \<longrightarrow> ((P1 x \<or> P2 x) \<longleftrightarrow> (P1' \<or> P2'))"
   4.345 +proof-
   4.346 +  from ex1 ex2 obtain z1 and z2 where z1: "\<forall>x. z1 \<sqsubset> x \<longrightarrow> (P1 x \<longleftrightarrow> P1')"
   4.347 +     and z2: "\<forall>x. z2 \<sqsubset> x \<longrightarrow> (P2 x \<longleftrightarrow> P2')" by blast
   4.348 +  from gt_ex obtain z where z:"ord.max less_eq z1 z2 \<sqsubset> z" by blast
   4.349 +  from z have zz1: "z1 \<sqsubset> z" and zz2: "z2 \<sqsubset> z" by simp_all
   4.350 +  {fix x assume H: "z \<sqsubset> x"
   4.351 +    from less_trans[OF zz1 H] less_trans[OF zz2 H]
   4.352 +    have "(P1 x \<or> P2 x) \<longleftrightarrow> (P1' \<or> P2')"  using z1 zz1 z2 zz2 by auto
   4.353 +  }
   4.354 +  thus ?thesis by blast
   4.355 +qed
   4.356 +
   4.357 +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"
   4.358 +proof-
   4.359 +  from ex obtain z where z: "\<forall>x. z \<sqsubset> x \<longrightarrow> (P x \<longleftrightarrow> P1)" by blast
   4.360 +  from gt_ex obtain x where x: "z \<sqsubset> x" by blast
   4.361 +  from z x p1 show ?thesis by blast
   4.362 +qed
   4.363 +
   4.364 +end
   4.365 +
   4.366 +text {* Linear order without upper bounds *}
   4.367 +
   4.368 +locale linorder_no_lb = linorder_stupid_syntax +
   4.369 +  assumes lt_ex: "\<exists>y. less y x"
   4.370 +begin
   4.371 +lemma le_ex[noatp]: "\<exists>y. y \<sqsubseteq> x" using lt_ex by auto
   4.372 +
   4.373 +
   4.374 +text {* Theorems for @{text "\<exists>z. \<forall>x. x \<sqsubset> z \<longrightarrow> (P x \<longleftrightarrow> P\<^bsub>-\<infinity>\<^esub>)"} *}
   4.375 +lemma minf_conj[noatp]:
   4.376 +  assumes ex1: "\<exists>z1. \<forall>x. x \<sqsubset> z1 \<longrightarrow> (P1 x \<longleftrightarrow> P1')"
   4.377 +  and ex2: "\<exists>z2. \<forall>x. x \<sqsubset> z2 \<longrightarrow> (P2 x \<longleftrightarrow> P2')"
   4.378 +  shows "\<exists>z. \<forall>x. x \<sqsubset>  z \<longrightarrow> ((P1 x \<and> P2 x) \<longleftrightarrow> (P1' \<and> P2'))"
   4.379 +proof-
   4.380 +  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
   4.381 +  from lt_ex obtain z where z:"z \<sqsubset> ord.min less_eq z1 z2" by blast
   4.382 +  from z have zz1: "z \<sqsubset> z1" and zz2: "z \<sqsubset> z2" by simp_all
   4.383 +  {fix x assume H: "x \<sqsubset> z"
   4.384 +    from less_trans[OF H zz1] less_trans[OF H zz2]
   4.385 +    have "(P1 x \<and> P2 x) \<longleftrightarrow> (P1' \<and> P2')"  using z1 zz1 z2 zz2 by auto
   4.386 +  }
   4.387 +  thus ?thesis by blast
   4.388 +qed
   4.389 +
   4.390 +lemma minf_disj[noatp]:
   4.391 +  assumes ex1: "\<exists>z1. \<forall>x. x \<sqsubset> z1 \<longrightarrow> (P1 x \<longleftrightarrow> P1')"
   4.392 +  and ex2: "\<exists>z2. \<forall>x. x \<sqsubset> z2 \<longrightarrow> (P2 x \<longleftrightarrow> P2')"
   4.393 +  shows "\<exists>z. \<forall>x. x \<sqsubset>  z \<longrightarrow> ((P1 x \<or> P2 x) \<longleftrightarrow> (P1' \<or> P2'))"
   4.394 +proof-
   4.395 +  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
   4.396 +  from lt_ex obtain z where z:"z \<sqsubset> ord.min less_eq z1 z2" by blast
   4.397 +  from z have zz1: "z \<sqsubset> z1" and zz2: "z \<sqsubset> z2" by simp_all
   4.398 +  {fix x assume H: "x \<sqsubset> z"
   4.399 +    from less_trans[OF H zz1] less_trans[OF H zz2]
   4.400 +    have "(P1 x \<or> P2 x) \<longleftrightarrow> (P1' \<or> P2')"  using z1 zz1 z2 zz2 by auto
   4.401 +  }
   4.402 +  thus ?thesis by blast
   4.403 +qed
   4.404 +
   4.405 +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"
   4.406 +proof-
   4.407 +  from ex obtain z where z: "\<forall>x. x \<sqsubset> z \<longrightarrow> (P x \<longleftrightarrow> P1)" by blast
   4.408 +  from lt_ex obtain x where x: "x \<sqsubset> z" by blast
   4.409 +  from z x p1 show ?thesis by blast
   4.410 +qed
   4.411 +
   4.412 +end
   4.413 +
   4.414 +
   4.415 +locale constr_dense_linear_order = linorder_no_lb + linorder_no_ub +
   4.416 +  fixes between
   4.417 +  assumes between_less: "less x y \<Longrightarrow> less x (between x y) \<and> less (between x y) y"
   4.418 +     and  between_same: "between x x = x"
   4.419 +
   4.420 +sublocale  constr_dense_linear_order < dense_linear_order 
   4.421 +  apply unfold_locales
   4.422 +  using gt_ex lt_ex between_less
   4.423 +    by (auto, rule_tac x="between x y" in exI, simp)
   4.424 +
   4.425 +context  constr_dense_linear_order
   4.426 +begin
   4.427 +
   4.428 +lemma rinf_U[noatp]:
   4.429 +  assumes fU: "finite U"
   4.430 +  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
   4.431 +  \<longrightarrow> (\<forall> y. l \<sqsubset> y \<and> y \<sqsubset> u \<longrightarrow> P y )"
   4.432 +  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')"
   4.433 +  and nmi: "\<not> MP"  and npi: "\<not> PP"  and ex: "\<exists> x.  P x"
   4.434 +  shows "\<exists> u\<in> U. \<exists> u' \<in> U. P (between u u')"
   4.435 +proof-
   4.436 +  from ex obtain x where px: "P x" by blast
   4.437 +  from px nmi npi nmpiU have "\<exists> u\<in> U. \<exists> u' \<in> U. u \<sqsubseteq> x \<and> x \<sqsubseteq> u'" by auto
   4.438 +  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
   4.439 +  from uU have Une: "U \<noteq> {}" by auto
   4.440 +  term "linorder.Min less_eq"
   4.441 +  let ?l = "linorder.Min less_eq U"
   4.442 +  let ?u = "linorder.Max less_eq U"
   4.443 +  have linM: "?l \<in> U" using fU Une by simp
   4.444 +  have uinM: "?u \<in> U" using fU Une by simp
   4.445 +  have lM: "\<forall> t\<in> U. ?l \<sqsubseteq> t" using Une fU by auto
   4.446 +  have Mu: "\<forall> t\<in> U. t \<sqsubseteq> ?u" using Une fU by auto
   4.447 +  have th:"?l \<sqsubseteq> u" using uU Une lM by auto
   4.448 +  from order_trans[OF th ux] have lx: "?l \<sqsubseteq> x" .
   4.449 +  have th: "u' \<sqsubseteq> ?u" using uU' Une Mu by simp
   4.450 +  from order_trans[OF xu' th] have xu: "x \<sqsubseteq> ?u" .
   4.451 +  from finite_set_intervals2[where P="P",OF px lx xu linM uinM fU lM Mu]
   4.452 +  have "(\<exists> s\<in> U. P s) \<or>
   4.453 +      (\<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)" .
   4.454 +  moreover { fix u assume um: "u\<in>U" and pu: "P u"
   4.455 +    have "between u u = u" by (simp add: between_same)
   4.456 +    with um pu have "P (between u u)" by simp
   4.457 +    with um have ?thesis by blast}
   4.458 +  moreover{
   4.459 +    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"
   4.460 +      then obtain t1 and t2 where t1M: "t1 \<in> U" and t2M: "t2\<in> U"
   4.461 +        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"
   4.462 +        by blast
   4.463 +      from less_trans[OF t1x xt2] have t1t2: "t1 \<sqsubset> t2" .
   4.464 +      let ?u = "between t1 t2"
   4.465 +      from between_less t1t2 have t1lu: "t1 \<sqsubset> ?u" and ut2: "?u \<sqsubset> t2" by auto
   4.466 +      from lin_dense noM t1x xt2 px t1lu ut2 have "P ?u" by blast
   4.467 +      with t1M t2M have ?thesis by blast}
   4.468 +    ultimately show ?thesis by blast
   4.469 +  qed
   4.470 +
   4.471 +theorem fr_eq[noatp]:
   4.472 +  assumes fU: "finite U"
   4.473 +  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
   4.474 +   \<longrightarrow> (\<forall> y. l \<sqsubset> y \<and> y \<sqsubset> u \<longrightarrow> P y )"
   4.475 +  and nmibnd: "\<forall>x. \<not> MP \<and> P x \<longrightarrow> (\<exists> u\<in> U. u \<sqsubseteq> x)"
   4.476 +  and npibnd: "\<forall>x. \<not>PP \<and> P x \<longrightarrow> (\<exists> u\<in> U. x \<sqsubseteq> u)"
   4.477 +  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)"
   4.478 +  shows "(\<exists> x. P x) \<equiv> (MP \<or> PP \<or> (\<exists> u \<in> U. \<exists> u'\<in> U. P (between u u')))"
   4.479 +  (is "_ \<equiv> (_ \<or> _ \<or> ?F)" is "?E \<equiv> ?D")
   4.480 +proof-
   4.481 + {
   4.482 +   assume px: "\<exists> x. P x"
   4.483 +   have "MP \<or> PP \<or> (\<not> MP \<and> \<not> PP)" by blast
   4.484 +   moreover {assume "MP \<or> PP" hence "?D" by blast}
   4.485 +   moreover {assume nmi: "\<not> MP" and npi: "\<not> PP"
   4.486 +     from npmibnd[OF nmibnd npibnd]
   4.487 +     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')" .
   4.488 +     from rinf_U[OF fU lin_dense nmpiU nmi npi px] have "?D" by blast}
   4.489 +   ultimately have "?D" by blast}
   4.490 + moreover
   4.491 + { assume "?D"
   4.492 +   moreover {assume m:"MP" from minf_ex[OF mi m] have "?E" .}
   4.493 +   moreover {assume p: "PP" from pinf_ex[OF pi p] have "?E" . }
   4.494 +   moreover {assume f:"?F" hence "?E" by blast}
   4.495 +   ultimately have "?E" by blast}
   4.496 + ultimately have "?E = ?D" by blast thus "?E \<equiv> ?D" by simp
   4.497 +qed
   4.498 +
   4.499 +lemmas minf_thms[noatp] = minf_conj minf_disj minf_eq minf_neq minf_lt minf_le minf_gt minf_ge minf_P
   4.500 +lemmas pinf_thms[noatp] = pinf_conj pinf_disj pinf_eq pinf_neq pinf_lt pinf_le pinf_gt pinf_ge pinf_P
   4.501 +
   4.502 +lemmas nmi_thms[noatp] = nmi_conj nmi_disj nmi_eq nmi_neq nmi_lt nmi_le nmi_gt nmi_ge nmi_P
   4.503 +lemmas npi_thms[noatp] = npi_conj npi_disj npi_eq npi_neq npi_lt npi_le npi_gt npi_ge npi_P
   4.504 +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
   4.505 +
   4.506 +lemma ferrack_axiom[noatp]: "constr_dense_linear_order less_eq less between"
   4.507 +  by (rule constr_dense_linear_order_axioms)
   4.508 +lemma atoms[noatp]:
   4.509 +  shows "TERM (less :: 'a \<Rightarrow> _)"
   4.510 +    and "TERM (less_eq :: 'a \<Rightarrow> _)"
   4.511 +    and "TERM (op = :: 'a \<Rightarrow> _)" .
   4.512 +
   4.513 +declare ferrack_axiom [ferrack minf: minf_thms pinf: pinf_thms
   4.514 +    nmi: nmi_thms npi: npi_thms lindense:
   4.515 +    lin_dense_thms qe: fr_eq atoms: atoms]
   4.516 +
   4.517 +declaration {*
   4.518 +let
   4.519 +fun simps phi = map (Morphism.thm phi) [@{thm "not_less"}, @{thm "not_le"}]
   4.520 +fun generic_whatis phi =
   4.521 + let
   4.522 +  val [lt, le] = map (Morphism.term phi) [@{term "op \<sqsubset>"}, @{term "op \<sqsubseteq>"}]
   4.523 +  fun h x t =
   4.524 +   case term_of t of
   4.525 +     Const("op =", _)$y$z => if term_of x aconv y then Ferrante_Rackoff_Data.Eq
   4.526 +                            else Ferrante_Rackoff_Data.Nox
   4.527 +   | @{term "Not"}$(Const("op =", _)$y$z) => if term_of x aconv y then Ferrante_Rackoff_Data.NEq
   4.528 +                            else Ferrante_Rackoff_Data.Nox
   4.529 +   | b$y$z => if Term.could_unify (b, lt) then
   4.530 +                 if term_of x aconv y then Ferrante_Rackoff_Data.Lt
   4.531 +                 else if term_of x aconv z then Ferrante_Rackoff_Data.Gt
   4.532 +                 else Ferrante_Rackoff_Data.Nox
   4.533 +             else if Term.could_unify (b, le) then
   4.534 +                 if term_of x aconv y then Ferrante_Rackoff_Data.Le
   4.535 +                 else if term_of x aconv z then Ferrante_Rackoff_Data.Ge
   4.536 +                 else Ferrante_Rackoff_Data.Nox
   4.537 +             else Ferrante_Rackoff_Data.Nox
   4.538 +   | _ => Ferrante_Rackoff_Data.Nox
   4.539 + in h end
   4.540 + fun ss phi = HOL_ss addsimps (simps phi)
   4.541 +in
   4.542 + Ferrante_Rackoff_Data.funs  @{thm "ferrack_axiom"}
   4.543 +  {isolate_conv = K (K (K Thm.reflexive)), whatis = generic_whatis, simpset = ss}
   4.544 +end
   4.545 +*}
   4.546 +
   4.547 +end
   4.548 +
   4.549 +use "~~/src/HOL/Tools/Qelim/ferrante_rackoff.ML"
   4.550 +
   4.551 +method_setup ferrack = {*
   4.552 +  Method.ctxt_args (Method.SIMPLE_METHOD' o FerranteRackoff.dlo_tac)
   4.553 +*} "Ferrante and Rackoff's algorithm for quantifier elimination in dense linear orders"
   4.554 +
   4.555 +subsection {* Ferrante and Rackoff algorithm over ordered fields *}
   4.556 +
   4.557 +lemma neg_prod_lt:"(c\<Colon>'a\<Colon>ordered_field) < 0 \<Longrightarrow> ((c*x < 0) == (x > 0))"
   4.558 +proof-
   4.559 +  assume H: "c < 0"
   4.560 +  have "c*x < 0 = (0/c < x)" by (simp only: neg_divide_less_eq[OF H] algebra_simps)
   4.561 +  also have "\<dots> = (0 < x)" by simp
   4.562 +  finally show  "(c*x < 0) == (x > 0)" by simp
   4.563 +qed
   4.564 +
   4.565 +lemma pos_prod_lt:"(c\<Colon>'a\<Colon>ordered_field) > 0 \<Longrightarrow> ((c*x < 0) == (x < 0))"
   4.566 +proof-
   4.567 +  assume H: "c > 0"
   4.568 +  hence "c*x < 0 = (0/c > x)" by (simp only: pos_less_divide_eq[OF H] algebra_simps)
   4.569 +  also have "\<dots> = (0 > x)" by simp
   4.570 +  finally show  "(c*x < 0) == (x < 0)" by simp
   4.571 +qed
   4.572 +
   4.573 +lemma neg_prod_sum_lt: "(c\<Colon>'a\<Colon>ordered_field) < 0 \<Longrightarrow> ((c*x + t< 0) == (x > (- 1/c)*t))"
   4.574 +proof-
   4.575 +  assume H: "c < 0"
   4.576 +  have "c*x + t< 0 = (c*x < -t)" by (subst less_iff_diff_less_0 [of "c*x" "-t"], simp)
   4.577 +  also have "\<dots> = (-t/c < x)" by (simp only: neg_divide_less_eq[OF H] algebra_simps)
   4.578 +  also have "\<dots> = ((- 1/c)*t < x)" by simp
   4.579 +  finally show  "(c*x + t < 0) == (x > (- 1/c)*t)" by simp
   4.580 +qed
   4.581 +
   4.582 +lemma pos_prod_sum_lt:"(c\<Colon>'a\<Colon>ordered_field) > 0 \<Longrightarrow> ((c*x + t < 0) == (x < (- 1/c)*t))"
   4.583 +proof-
   4.584 +  assume H: "c > 0"
   4.585 +  have "c*x + t< 0 = (c*x < -t)"  by (subst less_iff_diff_less_0 [of "c*x" "-t"], simp)
   4.586 +  also have "\<dots> = (-t/c > x)" by (simp only: pos_less_divide_eq[OF H] algebra_simps)
   4.587 +  also have "\<dots> = ((- 1/c)*t > x)" by simp
   4.588 +  finally show  "(c*x + t < 0) == (x < (- 1/c)*t)" by simp
   4.589 +qed
   4.590 +
   4.591 +lemma sum_lt:"((x::'a::pordered_ab_group_add) + t < 0) == (x < - t)"
   4.592 +  using less_diff_eq[where a= x and b=t and c=0] by simp
   4.593 +
   4.594 +lemma neg_prod_le:"(c\<Colon>'a\<Colon>ordered_field) < 0 \<Longrightarrow> ((c*x <= 0) == (x >= 0))"
   4.595 +proof-
   4.596 +  assume H: "c < 0"
   4.597 +  have "c*x <= 0 = (0/c <= x)" by (simp only: neg_divide_le_eq[OF H] algebra_simps)
   4.598 +  also have "\<dots> = (0 <= x)" by simp
   4.599 +  finally show  "(c*x <= 0) == (x >= 0)" by simp
   4.600 +qed
   4.601 +
   4.602 +lemma pos_prod_le:"(c\<Colon>'a\<Colon>ordered_field) > 0 \<Longrightarrow> ((c*x <= 0) == (x <= 0))"
   4.603 +proof-
   4.604 +  assume H: "c > 0"
   4.605 +  hence "c*x <= 0 = (0/c >= x)" by (simp only: pos_le_divide_eq[OF H] algebra_simps)
   4.606 +  also have "\<dots> = (0 >= x)" by simp
   4.607 +  finally show  "(c*x <= 0) == (x <= 0)" by simp
   4.608 +qed
   4.609 +
   4.610 +lemma neg_prod_sum_le: "(c\<Colon>'a\<Colon>ordered_field) < 0 \<Longrightarrow> ((c*x + t <= 0) == (x >= (- 1/c)*t))"
   4.611 +proof-
   4.612 +  assume H: "c < 0"
   4.613 +  have "c*x + t <= 0 = (c*x <= -t)"  by (subst le_iff_diff_le_0 [of "c*x" "-t"], simp)
   4.614 +  also have "\<dots> = (-t/c <= x)" by (simp only: neg_divide_le_eq[OF H] algebra_simps)
   4.615 +  also have "\<dots> = ((- 1/c)*t <= x)" by simp
   4.616 +  finally show  "(c*x + t <= 0) == (x >= (- 1/c)*t)" by simp
   4.617 +qed
   4.618 +
   4.619 +lemma pos_prod_sum_le:"(c\<Colon>'a\<Colon>ordered_field) > 0 \<Longrightarrow> ((c*x + t <= 0) == (x <= (- 1/c)*t))"
   4.620 +proof-
   4.621 +  assume H: "c > 0"
   4.622 +  have "c*x + t <= 0 = (c*x <= -t)" by (subst le_iff_diff_le_0 [of "c*x" "-t"], simp)
   4.623 +  also have "\<dots> = (-t/c >= x)" by (simp only: pos_le_divide_eq[OF H] algebra_simps)
   4.624 +  also have "\<dots> = ((- 1/c)*t >= x)" by simp
   4.625 +  finally show  "(c*x + t <= 0) == (x <= (- 1/c)*t)" by simp
   4.626 +qed
   4.627 +
   4.628 +lemma sum_le:"((x::'a::pordered_ab_group_add) + t <= 0) == (x <= - t)"
   4.629 +  using le_diff_eq[where a= x and b=t and c=0] by simp
   4.630 +
   4.631 +lemma nz_prod_eq:"(c\<Colon>'a\<Colon>ordered_field) \<noteq> 0 \<Longrightarrow> ((c*x = 0) == (x = 0))" by simp
   4.632 +lemma nz_prod_sum_eq: "(c\<Colon>'a\<Colon>ordered_field) \<noteq> 0 \<Longrightarrow> ((c*x + t = 0) == (x = (- 1/c)*t))"
   4.633 +proof-
   4.634 +  assume H: "c \<noteq> 0"
   4.635 +  have "c*x + t = 0 = (c*x = -t)" by (subst eq_iff_diff_eq_0 [of "c*x" "-t"], simp)
   4.636 +  also have "\<dots> = (x = -t/c)" by (simp only: nonzero_eq_divide_eq[OF H] algebra_simps)
   4.637 +  finally show  "(c*x + t = 0) == (x = (- 1/c)*t)" by simp
   4.638 +qed
   4.639 +lemma sum_eq:"((x::'a::pordered_ab_group_add) + t = 0) == (x = - t)"
   4.640 +  using eq_diff_eq[where a= x and b=t and c=0] by simp
   4.641 +
   4.642 +
   4.643 +interpretation class_ordered_field_dense_linear_order!: constr_dense_linear_order
   4.644 + "op <=" "op <"
   4.645 +   "\<lambda> x y. 1/2 * ((x::'a::{ordered_field,recpower,number_ring}) + y)"
   4.646 +proof (unfold_locales, dlo, dlo, auto)
   4.647 +  fix x y::'a assume lt: "x < y"
   4.648 +  from  less_half_sum[OF lt] show "x < (x + y) /2" by simp
   4.649 +next
   4.650 +  fix x y::'a assume lt: "x < y"
   4.651 +  from  gt_half_sum[OF lt] show "(x + y) /2 < y" by simp
   4.652 +qed
   4.653 +
   4.654 +declaration{*
   4.655 +let
   4.656 +fun earlier [] x y = false
   4.657 +        | earlier (h::t) x y =
   4.658 +    if h aconvc y then false else if h aconvc x then true else earlier t x y;
   4.659 +
   4.660 +fun dest_frac ct = case term_of ct of
   4.661 +   Const (@{const_name "HOL.divide"},_) $ a $ b=>
   4.662 +    Rat.rat_of_quotient (snd (HOLogic.dest_number a), snd (HOLogic.dest_number b))
   4.663 + | t => Rat.rat_of_int (snd (HOLogic.dest_number t))
   4.664 +
   4.665 +fun mk_frac phi cT x =
   4.666 + let val (a, b) = Rat.quotient_of_rat x
   4.667 + in if b = 1 then Numeral.mk_cnumber cT a
   4.668 +    else Thm.capply
   4.669 +         (Thm.capply (Drule.cterm_rule (instantiate' [SOME cT] []) @{cpat "op /"})
   4.670 +                     (Numeral.mk_cnumber cT a))
   4.671 +         (Numeral.mk_cnumber cT b)
   4.672 + end
   4.673 +
   4.674 +fun whatis x ct = case term_of ct of
   4.675 +  Const(@{const_name "HOL.plus"}, _)$(Const(@{const_name "HOL.times"},_)$_$y)$_ =>
   4.676 +     if y aconv term_of x then ("c*x+t",[(funpow 2 Thm.dest_arg1) ct, Thm.dest_arg ct])
   4.677 +     else ("Nox",[])
   4.678 +| Const(@{const_name "HOL.plus"}, _)$y$_ =>
   4.679 +     if y aconv term_of x then ("x+t",[Thm.dest_arg ct])
   4.680 +     else ("Nox",[])
   4.681 +| Const(@{const_name "HOL.times"}, _)$_$y =>
   4.682 +     if y aconv term_of x then ("c*x",[Thm.dest_arg1 ct])
   4.683 +     else ("Nox",[])
   4.684 +| t => if t aconv term_of x then ("x",[]) else ("Nox",[]);
   4.685 +
   4.686 +fun xnormalize_conv ctxt [] ct = reflexive ct
   4.687 +| xnormalize_conv ctxt (vs as (x::_)) ct =
   4.688 +   case term_of ct of
   4.689 +   Const(@{const_name HOL.less},_)$_$Const(@{const_name "HOL.zero"},_) =>
   4.690 +    (case whatis x (Thm.dest_arg1 ct) of
   4.691 +    ("c*x+t",[c,t]) =>
   4.692 +       let
   4.693 +        val cr = dest_frac c
   4.694 +        val clt = Thm.dest_fun2 ct
   4.695 +        val cz = Thm.dest_arg ct
   4.696 +        val neg = cr </ Rat.zero
   4.697 +        val cthp = Simplifier.rewrite (local_simpset_of ctxt)
   4.698 +               (Thm.capply @{cterm "Trueprop"}
   4.699 +                  (if neg then Thm.capply (Thm.capply clt c) cz
   4.700 +                    else Thm.capply (Thm.capply clt cz) c))
   4.701 +        val cth = equal_elim (symmetric cthp) TrueI
   4.702 +        val th = implies_elim (instantiate' [SOME (ctyp_of_term x)] (map SOME [c,x,t])
   4.703 +             (if neg then @{thm neg_prod_sum_lt} else @{thm pos_prod_sum_lt})) cth
   4.704 +        val rth = Conv.fconv_rule (Conv.arg_conv (Conv.binop_conv
   4.705 +                   (Normalizer.semiring_normalize_ord_conv ctxt (earlier vs)))) th
   4.706 +      in rth end
   4.707 +    | ("x+t",[t]) =>
   4.708 +       let
   4.709 +        val T = ctyp_of_term x
   4.710 +        val th = instantiate' [SOME T] [SOME x, SOME t] @{thm "sum_lt"}
   4.711 +        val rth = Conv.fconv_rule (Conv.arg_conv (Conv.binop_conv
   4.712 +              (Normalizer.semiring_normalize_ord_conv ctxt (earlier vs)))) th
   4.713 +       in  rth end
   4.714 +    | ("c*x",[c]) =>
   4.715 +       let
   4.716 +        val cr = dest_frac c
   4.717 +        val clt = Thm.dest_fun2 ct
   4.718 +        val cz = Thm.dest_arg ct
   4.719 +        val neg = cr </ Rat.zero
   4.720 +        val cthp = Simplifier.rewrite (local_simpset_of ctxt)
   4.721 +               (Thm.capply @{cterm "Trueprop"}
   4.722 +                  (if neg then Thm.capply (Thm.capply clt c) cz
   4.723 +                    else Thm.capply (Thm.capply clt cz) c))
   4.724 +        val cth = equal_elim (symmetric cthp) TrueI
   4.725 +        val th = implies_elim (instantiate' [SOME (ctyp_of_term x)] (map SOME [c,x])
   4.726 +             (if neg then @{thm neg_prod_lt} else @{thm pos_prod_lt})) cth
   4.727 +        val rth = th
   4.728 +      in rth end
   4.729 +    | _ => reflexive ct)
   4.730 +
   4.731 +
   4.732 +|  Const(@{const_name HOL.less_eq},_)$_$Const(@{const_name "HOL.zero"},_) =>
   4.733 +   (case whatis x (Thm.dest_arg1 ct) of
   4.734 +    ("c*x+t",[c,t]) =>
   4.735 +       let
   4.736 +        val T = ctyp_of_term x
   4.737 +        val cr = dest_frac c
   4.738 +        val clt = Drule.cterm_rule (instantiate' [SOME T] []) @{cpat "op <"}
   4.739 +        val cz = Thm.dest_arg ct
   4.740 +        val neg = cr </ Rat.zero
   4.741 +        val cthp = Simplifier.rewrite (local_simpset_of ctxt)
   4.742 +               (Thm.capply @{cterm "Trueprop"}
   4.743 +                  (if neg then Thm.capply (Thm.capply clt c) cz
   4.744 +                    else Thm.capply (Thm.capply clt cz) c))
   4.745 +        val cth = equal_elim (symmetric cthp) TrueI
   4.746 +        val th = implies_elim (instantiate' [SOME T] (map SOME [c,x,t])
   4.747 +             (if neg then @{thm neg_prod_sum_le} else @{thm pos_prod_sum_le})) cth
   4.748 +        val rth = Conv.fconv_rule (Conv.arg_conv (Conv.binop_conv
   4.749 +                   (Normalizer.semiring_normalize_ord_conv ctxt (earlier vs)))) th
   4.750 +      in rth end
   4.751 +    | ("x+t",[t]) =>
   4.752 +       let
   4.753 +        val T = ctyp_of_term x
   4.754 +        val th = instantiate' [SOME T] [SOME x, SOME t] @{thm "sum_le"}
   4.755 +        val rth = Conv.fconv_rule (Conv.arg_conv (Conv.binop_conv
   4.756 +              (Normalizer.semiring_normalize_ord_conv ctxt (earlier vs)))) th
   4.757 +       in  rth end
   4.758 +    | ("c*x",[c]) =>
   4.759 +       let
   4.760 +        val T = ctyp_of_term x
   4.761 +        val cr = dest_frac c
   4.762 +        val clt = Drule.cterm_rule (instantiate' [SOME T] []) @{cpat "op <"}
   4.763 +        val cz = Thm.dest_arg ct
   4.764 +        val neg = cr </ Rat.zero
   4.765 +        val cthp = Simplifier.rewrite (local_simpset_of ctxt)
   4.766 +               (Thm.capply @{cterm "Trueprop"}
   4.767 +                  (if neg then Thm.capply (Thm.capply clt c) cz
   4.768 +                    else Thm.capply (Thm.capply clt cz) c))
   4.769 +        val cth = equal_elim (symmetric cthp) TrueI
   4.770 +        val th = implies_elim (instantiate' [SOME (ctyp_of_term x)] (map SOME [c,x])
   4.771 +             (if neg then @{thm neg_prod_le} else @{thm pos_prod_le})) cth
   4.772 +        val rth = th
   4.773 +      in rth end
   4.774 +    | _ => reflexive ct)
   4.775 +
   4.776 +|  Const("op =",_)$_$Const(@{const_name "HOL.zero"},_) =>
   4.777 +   (case whatis x (Thm.dest_arg1 ct) of
   4.778 +    ("c*x+t",[c,t]) =>
   4.779 +       let
   4.780 +        val T = ctyp_of_term x
   4.781 +        val cr = dest_frac c
   4.782 +        val ceq = Thm.dest_fun2 ct
   4.783 +        val cz = Thm.dest_arg ct
   4.784 +        val cthp = Simplifier.rewrite (local_simpset_of ctxt)
   4.785 +            (Thm.capply @{cterm "Trueprop"}
   4.786 +             (Thm.capply @{cterm "Not"} (Thm.capply (Thm.capply ceq c) cz)))
   4.787 +        val cth = equal_elim (symmetric cthp) TrueI
   4.788 +        val th = implies_elim
   4.789 +                 (instantiate' [SOME T] (map SOME [c,x,t]) @{thm nz_prod_sum_eq}) cth
   4.790 +        val rth = Conv.fconv_rule (Conv.arg_conv (Conv.binop_conv
   4.791 +                   (Normalizer.semiring_normalize_ord_conv ctxt (earlier vs)))) th
   4.792 +      in rth end
   4.793 +    | ("x+t",[t]) =>
   4.794 +       let
   4.795 +        val T = ctyp_of_term x
   4.796 +        val th = instantiate' [SOME T] [SOME x, SOME t] @{thm "sum_eq"}
   4.797 +        val rth = Conv.fconv_rule (Conv.arg_conv (Conv.binop_conv
   4.798 +              (Normalizer.semiring_normalize_ord_conv ctxt (earlier vs)))) th
   4.799 +       in  rth end
   4.800 +    | ("c*x",[c]) =>
   4.801 +       let
   4.802 +        val T = ctyp_of_term x
   4.803 +        val cr = dest_frac c
   4.804 +        val ceq = Thm.dest_fun2 ct
   4.805 +        val cz = Thm.dest_arg ct
   4.806 +        val cthp = Simplifier.rewrite (local_simpset_of ctxt)
   4.807 +            (Thm.capply @{cterm "Trueprop"}
   4.808 +             (Thm.capply @{cterm "Not"} (Thm.capply (Thm.capply ceq c) cz)))
   4.809 +        val cth = equal_elim (symmetric cthp) TrueI
   4.810 +        val rth = implies_elim
   4.811 +                 (instantiate' [SOME T] (map SOME [c,x]) @{thm nz_prod_eq}) cth
   4.812 +      in rth end
   4.813 +    | _ => reflexive ct);
   4.814 +
   4.815 +local
   4.816 +  val less_iff_diff_less_0 = mk_meta_eq @{thm "less_iff_diff_less_0"}
   4.817 +  val le_iff_diff_le_0 = mk_meta_eq @{thm "le_iff_diff_le_0"}
   4.818 +  val eq_iff_diff_eq_0 = mk_meta_eq @{thm "eq_iff_diff_eq_0"}
   4.819 +in
   4.820 +fun field_isolate_conv phi ctxt vs ct = case term_of ct of
   4.821 +  Const(@{const_name HOL.less},_)$a$b =>
   4.822 +   let val (ca,cb) = Thm.dest_binop ct
   4.823 +       val T = ctyp_of_term ca
   4.824 +       val th = instantiate' [SOME T] [SOME ca, SOME cb] less_iff_diff_less_0
   4.825 +       val nth = Conv.fconv_rule
   4.826 +         (Conv.arg_conv (Conv.arg1_conv
   4.827 +              (Normalizer.semiring_normalize_ord_conv @{context} (earlier vs)))) th
   4.828 +       val rth = transitive nth (xnormalize_conv ctxt vs (Thm.rhs_of nth))
   4.829 +   in rth end
   4.830 +| Const(@{const_name HOL.less_eq},_)$a$b =>
   4.831 +   let val (ca,cb) = Thm.dest_binop ct
   4.832 +       val T = ctyp_of_term ca
   4.833 +       val th = instantiate' [SOME T] [SOME ca, SOME cb] le_iff_diff_le_0
   4.834 +       val nth = Conv.fconv_rule
   4.835 +         (Conv.arg_conv (Conv.arg1_conv
   4.836 +              (Normalizer.semiring_normalize_ord_conv @{context} (earlier vs)))) th
   4.837 +       val rth = transitive nth (xnormalize_conv ctxt vs (Thm.rhs_of nth))
   4.838 +   in rth end
   4.839 +
   4.840 +| Const("op =",_)$a$b =>
   4.841 +   let val (ca,cb) = Thm.dest_binop ct
   4.842 +       val T = ctyp_of_term ca
   4.843 +       val th = instantiate' [SOME T] [SOME ca, SOME cb] eq_iff_diff_eq_0
   4.844 +       val nth = Conv.fconv_rule
   4.845 +         (Conv.arg_conv (Conv.arg1_conv
   4.846 +              (Normalizer.semiring_normalize_ord_conv @{context} (earlier vs)))) th
   4.847 +       val rth = transitive nth (xnormalize_conv ctxt vs (Thm.rhs_of nth))
   4.848 +   in rth end
   4.849 +| @{term "Not"} $(Const("op =",_)$a$b) => Conv.arg_conv (field_isolate_conv phi ctxt vs) ct
   4.850 +| _ => reflexive ct
   4.851 +end;
   4.852 +
   4.853 +fun classfield_whatis phi =
   4.854 + let
   4.855 +  fun h x t =
   4.856 +   case term_of t of
   4.857 +     Const("op =", _)$y$z => if term_of x aconv y then Ferrante_Rackoff_Data.Eq
   4.858 +                            else Ferrante_Rackoff_Data.Nox
   4.859 +   | @{term "Not"}$(Const("op =", _)$y$z) => if term_of x aconv y then Ferrante_Rackoff_Data.NEq
   4.860 +                            else Ferrante_Rackoff_Data.Nox
   4.861 +   | Const(@{const_name HOL.less},_)$y$z =>
   4.862 +       if term_of x aconv y then Ferrante_Rackoff_Data.Lt
   4.863 +        else if term_of x aconv z then Ferrante_Rackoff_Data.Gt
   4.864 +        else Ferrante_Rackoff_Data.Nox
   4.865 +   | Const (@{const_name HOL.less_eq},_)$y$z =>
   4.866 +         if term_of x aconv y then Ferrante_Rackoff_Data.Le
   4.867 +         else if term_of x aconv z then Ferrante_Rackoff_Data.Ge
   4.868 +         else Ferrante_Rackoff_Data.Nox
   4.869 +   | _ => Ferrante_Rackoff_Data.Nox
   4.870 + in h end;
   4.871 +fun class_field_ss phi =
   4.872 +   HOL_basic_ss addsimps ([@{thm "linorder_not_less"}, @{thm "linorder_not_le"}])
   4.873 +   addsplits [@{thm "abs_split"},@{thm "split_max"}, @{thm "split_min"}]
   4.874 +
   4.875 +in
   4.876 +Ferrante_Rackoff_Data.funs @{thm "class_ordered_field_dense_linear_order.ferrack_axiom"}
   4.877 +  {isolate_conv = field_isolate_conv, whatis = classfield_whatis, simpset = class_field_ss}
   4.878 +end
   4.879 +*}
   4.880 +
   4.881 +
   4.882 +end 
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/src/HOL/Decision_Procs/Ferrack.thy	Fri Feb 06 15:15:32 2009 +0100
     5.3 @@ -0,0 +1,2101 @@
     5.4 +(*  Title:      HOL/Reflection/Ferrack.thy
     5.5 +    Author:     Amine Chaieb
     5.6 +*)
     5.7 +
     5.8 +theory Ferrack
     5.9 +imports Complex_Main Dense_Linear_Order Efficient_Nat
    5.10 +uses ("ferrack_tac.ML")
    5.11 +begin
    5.12 +
    5.13 +section {* Quantifier elimination for @{text "\<real> (0, 1, +, <)"} *}
    5.14 +
    5.15 +  (*********************************************************************************)
    5.16 +  (*          SOME GENERAL STUFF< HAS TO BE MOVED IN SOME LIB                      *)
    5.17 +  (*********************************************************************************)
    5.18 +
    5.19 +consts alluopairs:: "'a list \<Rightarrow> ('a \<times> 'a) list"
    5.20 +primrec
    5.21 +  "alluopairs [] = []"
    5.22 +  "alluopairs (x#xs) = (map (Pair x) (x#xs))@(alluopairs xs)"
    5.23 +
    5.24 +lemma alluopairs_set1: "set (alluopairs xs) \<le> {(x,y). x\<in> set xs \<and> y\<in> set xs}"
    5.25 +by (induct xs, auto)
    5.26 +
    5.27 +lemma alluopairs_set:
    5.28 +  "\<lbrakk>x\<in> set xs ; y \<in> set xs\<rbrakk> \<Longrightarrow> (x,y) \<in> set (alluopairs xs) \<or> (y,x) \<in> set (alluopairs xs) "
    5.29 +by (induct xs, auto)
    5.30 +
    5.31 +lemma alluopairs_ex:
    5.32 +  assumes Pc: "\<forall> x y. P x y = P y x"
    5.33 +  shows "(\<exists> x \<in> set xs. \<exists> y \<in> set xs. P x y) = (\<exists> (x,y) \<in> set (alluopairs xs). P x y)"
    5.34 +proof
    5.35 +  assume "\<exists>x\<in>set xs. \<exists>y\<in>set xs. P x y"
    5.36 +  then obtain x y where x: "x \<in> set xs" and y:"y \<in> set xs" and P: "P x y"  by blast
    5.37 +  from alluopairs_set[OF x y] P Pc show"\<exists>(x, y)\<in>set (alluopairs xs). P x y" 
    5.38 +    by auto
    5.39 +next
    5.40 +  assume "\<exists>(x, y)\<in>set (alluopairs xs). P x y"
    5.41 +  then obtain "x" and "y"  where xy:"(x,y) \<in> set (alluopairs xs)" and P: "P x y" by blast+
    5.42 +  from xy have "x \<in> set xs \<and> y\<in> set xs" using alluopairs_set1 by blast
    5.43 +  with P show "\<exists>x\<in>set xs. \<exists>y\<in>set xs. P x y" by blast
    5.44 +qed
    5.45 +
    5.46 +lemma nth_pos2: "0 < n \<Longrightarrow> (x#xs) ! n = xs ! (n - 1)"
    5.47 +using Nat.gr0_conv_Suc
    5.48 +by clarsimp
    5.49 +
    5.50 +lemma filter_length: "length (List.filter P xs) < Suc (length xs)"
    5.51 +  apply (induct xs, auto) done
    5.52 +
    5.53 +consts remdps:: "'a list \<Rightarrow> 'a list"
    5.54 +
    5.55 +recdef remdps "measure size"
    5.56 +  "remdps [] = []"
    5.57 +  "remdps (x#xs) = (x#(remdps (List.filter (\<lambda> y. y \<noteq> x) xs)))"
    5.58 +(hints simp add: filter_length[rule_format])
    5.59 +
    5.60 +lemma remdps_set[simp]: "set (remdps xs) = set xs"
    5.61 +  by (induct xs rule: remdps.induct, auto)
    5.62 +
    5.63 +
    5.64 +
    5.65 +  (*********************************************************************************)
    5.66 +  (****                            SHADOW SYNTAX AND SEMANTICS                  ****)
    5.67 +  (*********************************************************************************)
    5.68 +
    5.69 +datatype num = C int | Bound nat | CN nat int num | Neg num | Add num num| Sub num num 
    5.70 +  | Mul int num 
    5.71 +
    5.72 +  (* A size for num to make inductive proofs simpler*)
    5.73 +consts num_size :: "num \<Rightarrow> nat" 
    5.74 +primrec 
    5.75 +  "num_size (C c) = 1"
    5.76 +  "num_size (Bound n) = 1"
    5.77 +  "num_size (Neg a) = 1 + num_size a"
    5.78 +  "num_size (Add a b) = 1 + num_size a + num_size b"
    5.79 +  "num_size (Sub a b) = 3 + num_size a + num_size b"
    5.80 +  "num_size (Mul c a) = 1 + num_size a"
    5.81 +  "num_size (CN n c a) = 3 + num_size a "
    5.82 +
    5.83 +  (* Semantics of numeral terms (num) *)
    5.84 +consts Inum :: "real list \<Rightarrow> num \<Rightarrow> real"
    5.85 +primrec
    5.86 +  "Inum bs (C c) = (real c)"
    5.87 +  "Inum bs (Bound n) = bs!n"
    5.88 +  "Inum bs (CN n c a) = (real c) * (bs!n) + (Inum bs a)"
    5.89 +  "Inum bs (Neg a) = -(Inum bs a)"
    5.90 +  "Inum bs (Add a b) = Inum bs a + Inum bs b"
    5.91 +  "Inum bs (Sub a b) = Inum bs a - Inum bs b"
    5.92 +  "Inum bs (Mul c a) = (real c) * Inum bs a"
    5.93 +    (* FORMULAE *)
    5.94 +datatype fm  = 
    5.95 +  T| F| Lt num| Le num| Gt num| Ge num| Eq num| NEq num|
    5.96 +  NOT fm| And fm fm|  Or fm fm| Imp fm fm| Iff fm fm| E fm| A fm
    5.97 +
    5.98 +
    5.99 +  (* A size for fm *)
   5.100 +consts fmsize :: "fm \<Rightarrow> nat"
   5.101 +recdef fmsize "measure size"
   5.102 +  "fmsize (NOT p) = 1 + fmsize p"
   5.103 +  "fmsize (And p q) = 1 + fmsize p + fmsize q"
   5.104 +  "fmsize (Or p q) = 1 + fmsize p + fmsize q"
   5.105 +  "fmsize (Imp p q) = 3 + fmsize p + fmsize q"
   5.106 +  "fmsize (Iff p q) = 3 + 2*(fmsize p + fmsize q)"
   5.107 +  "fmsize (E p) = 1 + fmsize p"
   5.108 +  "fmsize (A p) = 4+ fmsize p"
   5.109 +  "fmsize p = 1"
   5.110 +  (* several lemmas about fmsize *)
   5.111 +lemma fmsize_pos: "fmsize p > 0"
   5.112 +by (induct p rule: fmsize.induct) simp_all
   5.113 +
   5.114 +  (* Semantics of formulae (fm) *)
   5.115 +consts Ifm ::"real list \<Rightarrow> fm \<Rightarrow> bool"
   5.116 +primrec
   5.117 +  "Ifm bs T = True"
   5.118 +  "Ifm bs F = False"
   5.119 +  "Ifm bs (Lt a) = (Inum bs a < 0)"
   5.120 +  "Ifm bs (Gt a) = (Inum bs a > 0)"
   5.121 +  "Ifm bs (Le a) = (Inum bs a \<le> 0)"
   5.122 +  "Ifm bs (Ge a) = (Inum bs a \<ge> 0)"
   5.123 +  "Ifm bs (Eq a) = (Inum bs a = 0)"
   5.124 +  "Ifm bs (NEq a) = (Inum bs a \<noteq> 0)"
   5.125 +  "Ifm bs (NOT p) = (\<not> (Ifm bs p))"
   5.126 +  "Ifm bs (And p q) = (Ifm bs p \<and> Ifm bs q)"
   5.127 +  "Ifm bs (Or p q) = (Ifm bs p \<or> Ifm bs q)"
   5.128 +  "Ifm bs (Imp p q) = ((Ifm bs p) \<longrightarrow> (Ifm bs q))"
   5.129 +  "Ifm bs (Iff p q) = (Ifm bs p = Ifm bs q)"
   5.130 +  "Ifm bs (E p) = (\<exists> x. Ifm (x#bs) p)"
   5.131 +  "Ifm bs (A p) = (\<forall> x. Ifm (x#bs) p)"
   5.132 +
   5.133 +lemma IfmLeSub: "\<lbrakk> Inum bs s = s' ; Inum bs t = t' \<rbrakk> \<Longrightarrow> Ifm bs (Le (Sub s t)) = (s' \<le> t')"
   5.134 +apply simp
   5.135 +done
   5.136 +
   5.137 +lemma IfmLtSub: "\<lbrakk> Inum bs s = s' ; Inum bs t = t' \<rbrakk> \<Longrightarrow> Ifm bs (Lt (Sub s t)) = (s' < t')"
   5.138 +apply simp
   5.139 +done
   5.140 +lemma IfmEqSub: "\<lbrakk> Inum bs s = s' ; Inum bs t = t' \<rbrakk> \<Longrightarrow> Ifm bs (Eq (Sub s t)) = (s' = t')"
   5.141 +apply simp
   5.142 +done
   5.143 +lemma IfmNOT: " (Ifm bs p = P) \<Longrightarrow> (Ifm bs (NOT p) = (\<not>P))"
   5.144 +apply simp
   5.145 +done
   5.146 +lemma IfmAnd: " \<lbrakk> Ifm bs p = P ; Ifm bs q = Q\<rbrakk> \<Longrightarrow> (Ifm bs (And p q) = (P \<and> Q))"
   5.147 +apply simp
   5.148 +done
   5.149 +lemma IfmOr: " \<lbrakk> Ifm bs p = P ; Ifm bs q = Q\<rbrakk> \<Longrightarrow> (Ifm bs (Or p q) = (P \<or> Q))"
   5.150 +apply simp
   5.151 +done
   5.152 +lemma IfmImp: " \<lbrakk> Ifm bs p = P ; Ifm bs q = Q\<rbrakk> \<Longrightarrow> (Ifm bs (Imp p q) = (P \<longrightarrow> Q))"
   5.153 +apply simp
   5.154 +done
   5.155 +lemma IfmIff: " \<lbrakk> Ifm bs p = P ; Ifm bs q = Q\<rbrakk> \<Longrightarrow> (Ifm bs (Iff p q) = (P = Q))"
   5.156 +apply simp
   5.157 +done
   5.158 +
   5.159 +lemma IfmE: " (!! x. Ifm (x#bs) p = P x) \<Longrightarrow> (Ifm bs (E p) = (\<exists>x. P x))"
   5.160 +apply simp
   5.161 +done
   5.162 +lemma IfmA: " (!! x. Ifm (x#bs) p = P x) \<Longrightarrow> (Ifm bs (A p) = (\<forall>x. P x))"
   5.163 +apply simp
   5.164 +done
   5.165 +
   5.166 +consts not:: "fm \<Rightarrow> fm"
   5.167 +recdef not "measure size"
   5.168 +  "not (NOT p) = p"
   5.169 +  "not T = F"
   5.170 +  "not F = T"
   5.171 +  "not p = NOT p"
   5.172 +lemma not[simp]: "Ifm bs (not p) = Ifm bs (NOT p)"
   5.173 +by (cases p) auto
   5.174 +
   5.175 +constdefs conj :: "fm \<Rightarrow> fm \<Rightarrow> fm"
   5.176 +  "conj p q \<equiv> (if (p = F \<or> q=F) then F else if p=T then q else if q=T then p else 
   5.177 +   if p = q then p else And p q)"
   5.178 +lemma conj[simp]: "Ifm bs (conj p q) = Ifm bs (And p q)"
   5.179 +by (cases "p=F \<or> q=F",simp_all add: conj_def) (cases p,simp_all)
   5.180 +
   5.181 +constdefs disj :: "fm \<Rightarrow> fm \<Rightarrow> fm"
   5.182 +  "disj p q \<equiv> (if (p = T \<or> q=T) then T else if p=F then q else if q=F then p 
   5.183 +       else if p=q then p else Or p q)"
   5.184 +
   5.185 +lemma disj[simp]: "Ifm bs (disj p q) = Ifm bs (Or p q)"
   5.186 +by (cases "p=T \<or> q=T",simp_all add: disj_def) (cases p,simp_all)
   5.187 +
   5.188 +constdefs  imp :: "fm \<Rightarrow> fm \<Rightarrow> fm"
   5.189 +  "imp p q \<equiv> (if (p = F \<or> q=T \<or> p=q) then T else if p=T then q else if q=F then not p 
   5.190 +    else Imp p q)"
   5.191 +lemma imp[simp]: "Ifm bs (imp p q) = Ifm bs (Imp p q)"
   5.192 +by (cases "p=F \<or> q=T",simp_all add: imp_def) 
   5.193 +
   5.194 +constdefs   iff :: "fm \<Rightarrow> fm \<Rightarrow> fm"
   5.195 +  "iff p q \<equiv> (if (p = q) then T else if (p = NOT q \<or> NOT p = q) then F else 
   5.196 +       if p=F then not q else if q=F then not p else if p=T then q else if q=T then p else 
   5.197 +  Iff p q)"
   5.198 +lemma iff[simp]: "Ifm bs (iff p q) = Ifm bs (Iff p q)"
   5.199 +  by (unfold iff_def,cases "p=q", simp,cases "p=NOT q", simp) (cases "NOT p= q", auto)
   5.200 +
   5.201 +lemma conj_simps:
   5.202 +  "conj F Q = F"
   5.203 +  "conj P F = F"
   5.204 +  "conj T Q = Q"
   5.205 +  "conj P T = P"
   5.206 +  "conj P P = P"
   5.207 +  "P \<noteq> T \<Longrightarrow> P \<noteq> F \<Longrightarrow> Q \<noteq> T \<Longrightarrow> Q \<noteq> F \<Longrightarrow> P \<noteq> Q \<Longrightarrow> conj P Q = And P Q"
   5.208 +  by (simp_all add: conj_def)
   5.209 +
   5.210 +lemma disj_simps:
   5.211 +  "disj T Q = T"
   5.212 +  "disj P T = T"
   5.213 +  "disj F Q = Q"
   5.214 +  "disj P F = P"
   5.215 +  "disj P P = P"
   5.216 +  "P \<noteq> T \<Longrightarrow> P \<noteq> F \<Longrightarrow> Q \<noteq> T \<Longrightarrow> Q \<noteq> F \<Longrightarrow> P \<noteq> Q \<Longrightarrow> disj P Q = Or P Q"
   5.217 +  by (simp_all add: disj_def)
   5.218 +lemma imp_simps:
   5.219 +  "imp F Q = T"
   5.220 +  "imp P T = T"
   5.221 +  "imp T Q = Q"
   5.222 +  "imp P F = not P"
   5.223 +  "imp P P = T"
   5.224 +  "P \<noteq> T \<Longrightarrow> P \<noteq> F \<Longrightarrow> P \<noteq> Q \<Longrightarrow> Q \<noteq> T \<Longrightarrow> Q \<noteq> F \<Longrightarrow> imp P Q = Imp P Q"
   5.225 +  by (simp_all add: imp_def)
   5.226 +lemma trivNOT: "p \<noteq> NOT p" "NOT p \<noteq> p"
   5.227 +apply (induct p, auto)
   5.228 +done
   5.229 +
   5.230 +lemma iff_simps:
   5.231 +  "iff p p = T"
   5.232 +  "iff p (NOT p) = F"
   5.233 +  "iff (NOT p) p = F"
   5.234 +  "iff p F = not p"
   5.235 +  "iff F p = not p"
   5.236 +  "p \<noteq> NOT T \<Longrightarrow> iff T p = p"
   5.237 +  "p\<noteq> NOT T \<Longrightarrow> iff p T = p"
   5.238 +  "p\<noteq>q \<Longrightarrow> p\<noteq> NOT q \<Longrightarrow> q\<noteq> NOT p \<Longrightarrow> p\<noteq> F \<Longrightarrow> q\<noteq> F \<Longrightarrow> p \<noteq> T \<Longrightarrow> q \<noteq> T \<Longrightarrow> iff p q = Iff p q"
   5.239 +  using trivNOT
   5.240 +  by (simp_all add: iff_def, cases p, auto)
   5.241 +  (* Quantifier freeness *)
   5.242 +consts qfree:: "fm \<Rightarrow> bool"
   5.243 +recdef qfree "measure size"
   5.244 +  "qfree (E p) = False"
   5.245 +  "qfree (A p) = False"
   5.246 +  "qfree (NOT p) = qfree p" 
   5.247 +  "qfree (And p q) = (qfree p \<and> qfree q)" 
   5.248 +  "qfree (Or  p q) = (qfree p \<and> qfree q)" 
   5.249 +  "qfree (Imp p q) = (qfree p \<and> qfree q)" 
   5.250 +  "qfree (Iff p q) = (qfree p \<and> qfree q)"
   5.251 +  "qfree p = True"
   5.252 +
   5.253 +  (* Boundedness and substitution *)
   5.254 +consts 
   5.255 +  numbound0:: "num \<Rightarrow> bool" (* a num is INDEPENDENT of Bound 0 *)
   5.256 +  bound0:: "fm \<Rightarrow> bool" (* A Formula is independent of Bound 0 *)
   5.257 +primrec
   5.258 +  "numbound0 (C c) = True"
   5.259 +  "numbound0 (Bound n) = (n>0)"
   5.260 +  "numbound0 (CN n c a) = (n\<noteq>0 \<and> numbound0 a)"
   5.261 +  "numbound0 (Neg a) = numbound0 a"
   5.262 +  "numbound0 (Add a b) = (numbound0 a \<and> numbound0 b)"
   5.263 +  "numbound0 (Sub a b) = (numbound0 a \<and> numbound0 b)" 
   5.264 +  "numbound0 (Mul i a) = numbound0 a"
   5.265 +lemma numbound0_I:
   5.266 +  assumes nb: "numbound0 a"
   5.267 +  shows "Inum (b#bs) a = Inum (b'#bs) a"
   5.268 +using nb
   5.269 +by (induct a rule: numbound0.induct,auto simp add: nth_pos2)
   5.270 +
   5.271 +primrec
   5.272 +  "bound0 T = True"
   5.273 +  "bound0 F = True"
   5.274 +  "bound0 (Lt a) = numbound0 a"
   5.275 +  "bound0 (Le a) = numbound0 a"
   5.276 +  "bound0 (Gt a) = numbound0 a"
   5.277 +  "bound0 (Ge a) = numbound0 a"
   5.278 +  "bound0 (Eq a) = numbound0 a"
   5.279 +  "bound0 (NEq a) = numbound0 a"
   5.280 +  "bound0 (NOT p) = bound0 p"
   5.281 +  "bound0 (And p q) = (bound0 p \<and> bound0 q)"
   5.282 +  "bound0 (Or p q) = (bound0 p \<and> bound0 q)"
   5.283 +  "bound0 (Imp p q) = ((bound0 p) \<and> (bound0 q))"
   5.284 +  "bound0 (Iff p q) = (bound0 p \<and> bound0 q)"
   5.285 +  "bound0 (E p) = False"
   5.286 +  "bound0 (A p) = False"
   5.287 +
   5.288 +lemma bound0_I:
   5.289 +  assumes bp: "bound0 p"
   5.290 +  shows "Ifm (b#bs) p = Ifm (b'#bs) p"
   5.291 +using bp numbound0_I[where b="b" and bs="bs" and b'="b'"]
   5.292 +by (induct p rule: bound0.induct) (auto simp add: nth_pos2)
   5.293 +
   5.294 +lemma not_qf[simp]: "qfree p \<Longrightarrow> qfree (not p)"
   5.295 +by (cases p, auto)
   5.296 +lemma not_bn[simp]: "bound0 p \<Longrightarrow> bound0 (not p)"
   5.297 +by (cases p, auto)
   5.298 +
   5.299 +
   5.300 +lemma conj_qf[simp]: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (conj p q)"
   5.301 +using conj_def by auto 
   5.302 +lemma conj_nb[simp]: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (conj p q)"
   5.303 +using conj_def by auto 
   5.304 +
   5.305 +lemma disj_qf[simp]: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (disj p q)"
   5.306 +using disj_def by auto 
   5.307 +lemma disj_nb[simp]: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (disj p q)"
   5.308 +using disj_def by auto 
   5.309 +
   5.310 +lemma imp_qf[simp]: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (imp p q)"
   5.311 +using imp_def by (cases "p=F \<or> q=T",simp_all add: imp_def)
   5.312 +lemma imp_nb[simp]: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (imp p q)"
   5.313 +using imp_def by (cases "p=F \<or> q=T \<or> p=q",simp_all add: imp_def)
   5.314 +
   5.315 +lemma iff_qf[simp]: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (iff p q)"
   5.316 +  by (unfold iff_def,cases "p=q", auto)
   5.317 +lemma iff_nb[simp]: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (iff p q)"
   5.318 +using iff_def by (unfold iff_def,cases "p=q", auto)
   5.319 +
   5.320 +consts 
   5.321 +  decrnum:: "num \<Rightarrow> num" 
   5.322 +  decr :: "fm \<Rightarrow> fm"
   5.323 +
   5.324 +recdef decrnum "measure size"
   5.325 +  "decrnum (Bound n) = Bound (n - 1)"
   5.326 +  "decrnum (Neg a) = Neg (decrnum a)"
   5.327 +  "decrnum (Add a b) = Add (decrnum a) (decrnum b)"
   5.328 +  "decrnum (Sub a b) = Sub (decrnum a) (decrnum b)"
   5.329 +  "decrnum (Mul c a) = Mul c (decrnum a)"
   5.330 +  "decrnum (CN n c a) = CN (n - 1) c (decrnum a)"
   5.331 +  "decrnum a = a"
   5.332 +
   5.333 +recdef decr "measure size"
   5.334 +  "decr (Lt a) = Lt (decrnum a)"
   5.335 +  "decr (Le a) = Le (decrnum a)"
   5.336 +  "decr (Gt a) = Gt (decrnum a)"
   5.337 +  "decr (Ge a) = Ge (decrnum a)"
   5.338 +  "decr (Eq a) = Eq (decrnum a)"
   5.339 +  "decr (NEq a) = NEq (decrnum a)"
   5.340 +  "decr (NOT p) = NOT (decr p)" 
   5.341 +  "decr (And p q) = conj (decr p) (decr q)"
   5.342 +  "decr (Or p q) = disj (decr p) (decr q)"
   5.343 +  "decr (Imp p q) = imp (decr p) (decr q)"
   5.344 +  "decr (Iff p q) = iff (decr p) (decr q)"
   5.345 +  "decr p = p"
   5.346 +
   5.347 +lemma decrnum: assumes nb: "numbound0 t"
   5.348 +  shows "Inum (x#bs) t = Inum bs (decrnum t)"
   5.349 +  using nb by (induct t rule: decrnum.induct, simp_all add: nth_pos2)
   5.350 +
   5.351 +lemma decr: assumes nb: "bound0 p"
   5.352 +  shows "Ifm (x#bs) p = Ifm bs (decr p)"
   5.353 +  using nb 
   5.354 +  by (induct p rule: decr.induct, simp_all add: nth_pos2 decrnum)
   5.355 +
   5.356 +lemma decr_qf: "bound0 p \<Longrightarrow> qfree (decr p)"
   5.357 +by (induct p, simp_all)
   5.358 +
   5.359 +consts 
   5.360 +  isatom :: "fm \<Rightarrow> bool" (* test for atomicity *)
   5.361 +recdef isatom "measure size"
   5.362 +  "isatom T = True"
   5.363 +  "isatom F = True"
   5.364 +  "isatom (Lt a) = True"
   5.365 +  "isatom (Le a) = True"
   5.366 +  "isatom (Gt a) = True"
   5.367 +  "isatom (Ge a) = True"
   5.368 +  "isatom (Eq a) = True"
   5.369 +  "isatom (NEq a) = True"
   5.370 +  "isatom p = False"
   5.371 +
   5.372 +lemma bound0_qf: "bound0 p \<Longrightarrow> qfree p"
   5.373 +by (induct p, simp_all)
   5.374 +
   5.375 +constdefs djf:: "('a \<Rightarrow> fm) \<Rightarrow> 'a \<Rightarrow> fm \<Rightarrow> fm"
   5.376 +  "djf f p q \<equiv> (if q=T then T else if q=F then f p else 
   5.377 +  (let fp = f p in case fp of T \<Rightarrow> T | F \<Rightarrow> q | _ \<Rightarrow> Or (f p) q))"
   5.378 +constdefs evaldjf:: "('a \<Rightarrow> fm) \<Rightarrow> 'a list \<Rightarrow> fm"
   5.379 +  "evaldjf f ps \<equiv> foldr (djf f) ps F"
   5.380 +
   5.381 +lemma djf_Or: "Ifm bs (djf f p q) = Ifm bs (Or (f p) q)"
   5.382 +by (cases "q=T", simp add: djf_def,cases "q=F",simp add: djf_def) 
   5.383 +(cases "f p", simp_all add: Let_def djf_def) 
   5.384 +
   5.385 +
   5.386 +lemma djf_simps:
   5.387 +  "djf f p T = T"
   5.388 +  "djf f p F = f p"
   5.389 +  "q\<noteq>T \<Longrightarrow> q\<noteq>F \<Longrightarrow> djf f p q = (let fp = f p in case fp of T \<Rightarrow> T | F \<Rightarrow> q | _ \<Rightarrow> Or (f p) q)"
   5.390 +  by (simp_all add: djf_def)
   5.391 +
   5.392 +lemma evaldjf_ex: "Ifm bs (evaldjf f ps) = (\<exists> p \<in> set ps. Ifm bs (f p))"
   5.393 +  by(induct ps, simp_all add: evaldjf_def djf_Or)
   5.394 +
   5.395 +lemma evaldjf_bound0: 
   5.396 +  assumes nb: "\<forall> x\<in> set xs. bound0 (f x)"
   5.397 +  shows "bound0 (evaldjf f xs)"
   5.398 +  using nb by (induct xs, auto simp add: evaldjf_def djf_def Let_def) (case_tac "f a", auto) 
   5.399 +
   5.400 +lemma evaldjf_qf: 
   5.401 +  assumes nb: "\<forall> x\<in> set xs. qfree (f x)"
   5.402 +  shows "qfree (evaldjf f xs)"
   5.403 +  using nb by (induct xs, auto simp add: evaldjf_def djf_def Let_def) (case_tac "f a", auto) 
   5.404 +
   5.405 +consts disjuncts :: "fm \<Rightarrow> fm list"
   5.406 +recdef disjuncts "measure size"
   5.407 +  "disjuncts (Or p q) = (disjuncts p) @ (disjuncts q)"
   5.408 +  "disjuncts F = []"
   5.409 +  "disjuncts p = [p]"
   5.410 +
   5.411 +lemma disjuncts: "(\<exists> q\<in> set (disjuncts p). Ifm bs q) = Ifm bs p"
   5.412 +by(induct p rule: disjuncts.induct, auto)
   5.413 +
   5.414 +lemma disjuncts_nb: "bound0 p \<Longrightarrow> \<forall> q\<in> set (disjuncts p). bound0 q"
   5.415 +proof-
   5.416 +  assume nb: "bound0 p"
   5.417 +  hence "list_all bound0 (disjuncts p)" by (induct p rule:disjuncts.induct,auto)
   5.418 +  thus ?thesis by (simp only: list_all_iff)
   5.419 +qed
   5.420 +
   5.421 +lemma disjuncts_qf: "qfree p \<Longrightarrow> \<forall> q\<in> set (disjuncts p). qfree q"
   5.422 +proof-
   5.423 +  assume qf: "qfree p"
   5.424 +  hence "list_all qfree (disjuncts p)"
   5.425 +    by (induct p rule: disjuncts.induct, auto)
   5.426 +  thus ?thesis by (simp only: list_all_iff)
   5.427 +qed
   5.428 +
   5.429 +constdefs DJ :: "(fm \<Rightarrow> fm) \<Rightarrow> fm \<Rightarrow> fm"
   5.430 +  "DJ f p \<equiv> evaldjf f (disjuncts p)"
   5.431 +
   5.432 +lemma DJ: assumes fdj: "\<forall> p q. Ifm bs (f (Or p q)) = Ifm bs (Or (f p) (f q))"
   5.433 +  and fF: "f F = F"
   5.434 +  shows "Ifm bs (DJ f p) = Ifm bs (f p)"
   5.435 +proof-
   5.436 +  have "Ifm bs (DJ f p) = (\<exists> q \<in> set (disjuncts p). Ifm bs (f q))"
   5.437 +    by (simp add: DJ_def evaldjf_ex) 
   5.438 +  also have "\<dots> = Ifm bs (f p)" using fdj fF by (induct p rule: disjuncts.induct, auto)
   5.439 +  finally show ?thesis .
   5.440 +qed
   5.441 +
   5.442 +lemma DJ_qf: assumes 
   5.443 +  fqf: "\<forall> p. qfree p \<longrightarrow> qfree (f p)"
   5.444 +  shows "\<forall>p. qfree p \<longrightarrow> qfree (DJ f p) "
   5.445 +proof(clarify)
   5.446 +  fix  p assume qf: "qfree p"
   5.447 +  have th: "DJ f p = evaldjf f (disjuncts p)" by (simp add: DJ_def)
   5.448 +  from disjuncts_qf[OF qf] have "\<forall> q\<in> set (disjuncts p). qfree q" .
   5.449 +  with fqf have th':"\<forall> q\<in> set (disjuncts p). qfree (f q)" by blast
   5.450 +  
   5.451 +  from evaldjf_qf[OF th'] th show "qfree (DJ f p)" by simp
   5.452 +qed
   5.453 +
   5.454 +lemma DJ_qe: assumes qe: "\<forall> bs p. qfree p \<longrightarrow> qfree (qe p) \<and> (Ifm bs (qe p) = Ifm bs (E p))"
   5.455 +  shows "\<forall> bs p. qfree p \<longrightarrow> qfree (DJ qe p) \<and> (Ifm bs ((DJ qe p)) = Ifm bs (E p))"
   5.456 +proof(clarify)
   5.457 +  fix p::fm and bs
   5.458 +  assume qf: "qfree p"
   5.459 +  from qe have qth: "\<forall> p. qfree p \<longrightarrow> qfree (qe p)" by blast
   5.460 +  from DJ_qf[OF qth] qf have qfth:"qfree (DJ qe p)" by auto
   5.461 +  have "Ifm bs (DJ qe p) = (\<exists> q\<in> set (disjuncts p). Ifm bs (qe q))"
   5.462 +    by (simp add: DJ_def evaldjf_ex)
   5.463 +  also have "\<dots> = (\<exists> q \<in> set(disjuncts p). Ifm bs (E q))" using qe disjuncts_qf[OF qf] by auto
   5.464 +  also have "\<dots> = Ifm bs (E p)" by (induct p rule: disjuncts.induct, auto)
   5.465 +  finally show "qfree (DJ qe p) \<and> Ifm bs (DJ qe p) = Ifm bs (E p)" using qfth by blast
   5.466 +qed
   5.467 +  (* Simplification *)
   5.468 +consts 
   5.469 +  numgcd :: "num \<Rightarrow> int"
   5.470 +  numgcdh:: "num \<Rightarrow> int \<Rightarrow> int"
   5.471 +  reducecoeffh:: "num \<Rightarrow> int \<Rightarrow> num"
   5.472 +  reducecoeff :: "num \<Rightarrow> num"
   5.473 +  dvdnumcoeff:: "num \<Rightarrow> int \<Rightarrow> bool"
   5.474 +consts maxcoeff:: "num \<Rightarrow> int"
   5.475 +recdef maxcoeff "measure size"
   5.476 +  "maxcoeff (C i) = abs i"
   5.477 +  "maxcoeff (CN n c t) = max (abs c) (maxcoeff t)"
   5.478 +  "maxcoeff t = 1"
   5.479 +
   5.480 +lemma maxcoeff_pos: "maxcoeff t \<ge> 0"
   5.481 +  by (induct t rule: maxcoeff.induct, auto)
   5.482 +
   5.483 +recdef numgcdh "measure size"
   5.484 +  "numgcdh (C i) = (\<lambda>g. zgcd i g)"
   5.485 +  "numgcdh (CN n c t) = (\<lambda>g. zgcd c (numgcdh t g))"
   5.486 +  "numgcdh t = (\<lambda>g. 1)"
   5.487 +defs numgcd_def [code]: "numgcd t \<equiv> numgcdh t (maxcoeff t)"
   5.488 +
   5.489 +recdef reducecoeffh "measure size"
   5.490 +  "reducecoeffh (C i) = (\<lambda> g. C (i div g))"
   5.491 +  "reducecoeffh (CN n c t) = (\<lambda> g. CN n (c div g) (reducecoeffh t g))"
   5.492 +  "reducecoeffh t = (\<lambda>g. t)"
   5.493 +
   5.494 +defs reducecoeff_def: "reducecoeff t \<equiv> 
   5.495 +  (let g = numgcd t in 
   5.496 +  if g = 0 then C 0 else if g=1 then t else reducecoeffh t g)"
   5.497 +
   5.498 +recdef dvdnumcoeff "measure size"
   5.499 +  "dvdnumcoeff (C i) = (\<lambda> g. g dvd i)"
   5.500 +  "dvdnumcoeff (CN n c t) = (\<lambda> g. g dvd c \<and> (dvdnumcoeff t g))"
   5.501 +  "dvdnumcoeff t = (\<lambda>g. False)"
   5.502 +
   5.503 +lemma dvdnumcoeff_trans: 
   5.504 +  assumes gdg: "g dvd g'" and dgt':"dvdnumcoeff t g'"
   5.505 +  shows "dvdnumcoeff t g"
   5.506 +  using dgt' gdg 
   5.507 +  by (induct t rule: dvdnumcoeff.induct, simp_all add: gdg zdvd_trans[OF gdg])
   5.508 +
   5.509 +declare zdvd_trans [trans add]
   5.510 +
   5.511 +lemma natabs0: "(nat (abs x) = 0) = (x = 0)"
   5.512 +by arith
   5.513 +
   5.514 +lemma numgcd0:
   5.515 +  assumes g0: "numgcd t = 0"
   5.516 +  shows "Inum bs t = 0"
   5.517 +  using g0[simplified numgcd_def] 
   5.518 +  by (induct t rule: numgcdh.induct, auto simp add: zgcd_def gcd_zero natabs0 max_def maxcoeff_pos)
   5.519 +
   5.520 +lemma numgcdh_pos: assumes gp: "g \<ge> 0" shows "numgcdh t g \<ge> 0"
   5.521 +  using gp
   5.522 +  by (induct t rule: numgcdh.induct, auto simp add: zgcd_def)
   5.523 +
   5.524 +lemma numgcd_pos: "numgcd t \<ge>0"
   5.525 +  by (simp add: numgcd_def numgcdh_pos maxcoeff_pos)
   5.526 +
   5.527 +lemma reducecoeffh:
   5.528 +  assumes gt: "dvdnumcoeff t g" and gp: "g > 0" 
   5.529 +  shows "real g *(Inum bs (reducecoeffh t g)) = Inum bs t"
   5.530 +  using gt
   5.531 +proof(induct t rule: reducecoeffh.induct) 
   5.532 +  case (1 i) hence gd: "g dvd i" by simp
   5.533 +  from gp have gnz: "g \<noteq> 0" by simp
   5.534 +  from prems show ?case by (simp add: real_of_int_div[OF gnz gd])
   5.535 +next
   5.536 +  case (2 n c t)  hence gd: "g dvd c" by simp
   5.537 +  from gp have gnz: "g \<noteq> 0" by simp
   5.538 +  from prems show ?case by (simp add: real_of_int_div[OF gnz gd] algebra_simps)
   5.539 +qed (auto simp add: numgcd_def gp)
   5.540 +consts ismaxcoeff:: "num \<Rightarrow> int \<Rightarrow> bool"
   5.541 +recdef ismaxcoeff "measure size"
   5.542 +  "ismaxcoeff (C i) = (\<lambda> x. abs i \<le> x)"
   5.543 +  "ismaxcoeff (CN n c t) = (\<lambda>x. abs c \<le> x \<and> (ismaxcoeff t x))"
   5.544 +  "ismaxcoeff t = (\<lambda>x. True)"
   5.545 +
   5.546 +lemma ismaxcoeff_mono: "ismaxcoeff t c \<Longrightarrow> c \<le> c' \<Longrightarrow> ismaxcoeff t c'"
   5.547 +by (induct t rule: ismaxcoeff.induct, auto)
   5.548 +
   5.549 +lemma maxcoeff_ismaxcoeff: "ismaxcoeff t (maxcoeff t)"
   5.550 +proof (induct t rule: maxcoeff.induct)
   5.551 +  case (2 n c t)
   5.552 +  hence H:"ismaxcoeff t (maxcoeff t)" .
   5.553 +  have thh: "maxcoeff t \<le> max (abs c) (maxcoeff t)" by (simp add: le_maxI2)
   5.554 +  from ismaxcoeff_mono[OF H thh] show ?case by (simp add: le_maxI1)
   5.555 +qed simp_all
   5.556 +
   5.557 +lemma zgcd_gt1: "zgcd i j > 1 \<Longrightarrow> ((abs i > 1 \<and> abs j > 1) \<or> (abs i = 0 \<and> abs j > 1) \<or> (abs i > 1 \<and> abs j = 0))"
   5.558 +  apply (cases "abs i = 0", simp_all add: zgcd_def)
   5.559 +  apply (cases "abs j = 0", simp_all)
   5.560 +  apply (cases "abs i = 1", simp_all)
   5.561 +  apply (cases "abs j = 1", simp_all)
   5.562 +  apply auto
   5.563 +  done
   5.564 +lemma numgcdh0:"numgcdh t m = 0 \<Longrightarrow>  m =0"
   5.565 +  by (induct t rule: numgcdh.induct, auto simp add:zgcd0)
   5.566 +
   5.567 +lemma dvdnumcoeff_aux:
   5.568 +  assumes "ismaxcoeff t m" and mp:"m \<ge> 0" and "numgcdh t m > 1"
   5.569 +  shows "dvdnumcoeff t (numgcdh t m)"
   5.570 +using prems
   5.571 +proof(induct t rule: numgcdh.induct)
   5.572 +  case (2 n c t) 
   5.573 +  let ?g = "numgcdh t m"
   5.574 +  from prems have th:"zgcd c ?g > 1" by simp
   5.575 +  from zgcd_gt1[OF th] numgcdh_pos[OF mp, where t="t"]
   5.576 +  have "(abs c > 1 \<and> ?g > 1) \<or> (abs c = 0 \<and> ?g > 1) \<or> (abs c > 1 \<and> ?g = 0)" by simp
   5.577 +  moreover {assume "abs c > 1" and gp: "?g > 1" with prems
   5.578 +    have th: "dvdnumcoeff t ?g" by simp
   5.579 +    have th': "zgcd c ?g dvd ?g" by (simp add:zgcd_zdvd2)
   5.580 +    from dvdnumcoeff_trans[OF th' th] have ?case by (simp add: zgcd_zdvd1)}
   5.581 +  moreover {assume "abs c = 0 \<and> ?g > 1"
   5.582 +    with prems have th: "dvdnumcoeff t ?g" by simp
   5.583 +    have th': "zgcd c ?g dvd ?g" by (simp add:zgcd_zdvd2)
   5.584 +    from dvdnumcoeff_trans[OF th' th] have ?case by (simp add: zgcd_zdvd1)
   5.585 +    hence ?case by simp }
   5.586 +  moreover {assume "abs c > 1" and g0:"?g = 0" 
   5.587 +    from numgcdh0[OF g0] have "m=0". with prems   have ?case by simp }
   5.588 +  ultimately show ?case by blast
   5.589 +qed(auto simp add: zgcd_zdvd1)
   5.590 +
   5.591 +lemma dvdnumcoeff_aux2:
   5.592 +  assumes "numgcd t > 1" shows "dvdnumcoeff t (numgcd t) \<and> numgcd t > 0"
   5.593 +  using prems 
   5.594 +proof (simp add: numgcd_def)
   5.595 +  let ?mc = "maxcoeff t"
   5.596 +  let ?g = "numgcdh t ?mc"
   5.597 +  have th1: "ismaxcoeff t ?mc" by (rule maxcoeff_ismaxcoeff)
   5.598 +  have th2: "?mc \<ge> 0" by (rule maxcoeff_pos)
   5.599 +  assume H: "numgcdh t ?mc > 1"
   5.600 +  from dvdnumcoeff_aux[OF th1 th2 H]  show "dvdnumcoeff t ?g" .
   5.601 +qed
   5.602 +
   5.603 +lemma reducecoeff: "real (numgcd t) * (Inum bs (reducecoeff t)) = Inum bs t"
   5.604 +proof-
   5.605 +  let ?g = "numgcd t"
   5.606 +  have "?g \<ge> 0"  by (simp add: numgcd_pos)
   5.607 +  hence	"?g = 0 \<or> ?g = 1 \<or> ?g > 1" by auto
   5.608 +  moreover {assume "?g = 0" hence ?thesis by (simp add: numgcd0)} 
   5.609 +  moreover {assume "?g = 1" hence ?thesis by (simp add: reducecoeff_def)} 
   5.610 +  moreover { assume g1:"?g > 1"
   5.611 +    from dvdnumcoeff_aux2[OF g1] have th1:"dvdnumcoeff t ?g" and g0: "?g > 0" by blast+
   5.612 +    from reducecoeffh[OF th1 g0, where bs="bs"] g1 have ?thesis 
   5.613 +      by (simp add: reducecoeff_def Let_def)} 
   5.614 +  ultimately show ?thesis by blast
   5.615 +qed
   5.616 +
   5.617 +lemma reducecoeffh_numbound0: "numbound0 t \<Longrightarrow> numbound0 (reducecoeffh t g)"
   5.618 +by (induct t rule: reducecoeffh.induct, auto)
   5.619 +
   5.620 +lemma reducecoeff_numbound0: "numbound0 t \<Longrightarrow> numbound0 (reducecoeff t)"
   5.621 +using reducecoeffh_numbound0 by (simp add: reducecoeff_def Let_def)
   5.622 +
   5.623 +consts
   5.624 +  simpnum:: "num \<Rightarrow> num"
   5.625 +  numadd:: "num \<times> num \<Rightarrow> num"
   5.626 +  nummul:: "num \<Rightarrow> int \<Rightarrow> num"
   5.627 +recdef numadd "measure (\<lambda> (t,s). size t + size s)"
   5.628 +  "numadd (CN n1 c1 r1,CN n2 c2 r2) =
   5.629 +  (if n1=n2 then 
   5.630 +  (let c = c1 + c2
   5.631 +  in (if c=0 then numadd(r1,r2) else CN n1 c (numadd (r1,r2))))
   5.632 +  else if n1 \<le> n2 then (CN n1 c1 (numadd (r1,CN n2 c2 r2))) 
   5.633 +  else (CN n2 c2 (numadd (CN n1 c1 r1,r2))))"
   5.634 +  "numadd (CN n1 c1 r1,t) = CN n1 c1 (numadd (r1, t))"  
   5.635 +  "numadd (t,CN n2 c2 r2) = CN n2 c2 (numadd (t,r2))" 
   5.636 +  "numadd (C b1, C b2) = C (b1+b2)"
   5.637 +  "numadd (a,b) = Add a b"
   5.638 +
   5.639 +lemma numadd[simp]: "Inum bs (numadd (t,s)) = Inum bs (Add t s)"
   5.640 +apply (induct t s rule: numadd.induct, simp_all add: Let_def)
   5.641 +apply (case_tac "c1+c2 = 0",case_tac "n1 \<le> n2", simp_all)
   5.642 +apply (case_tac "n1 = n2", simp_all add: algebra_simps)
   5.643 +by (simp only: left_distrib[symmetric],simp)
   5.644 +
   5.645 +lemma numadd_nb[simp]: "\<lbrakk> numbound0 t ; numbound0 s\<rbrakk> \<Longrightarrow> numbound0 (numadd (t,s))"
   5.646 +by (induct t s rule: numadd.induct, auto simp add: Let_def)
   5.647 +
   5.648 +recdef nummul "measure size"
   5.649 +  "nummul (C j) = (\<lambda> i. C (i*j))"
   5.650 +  "nummul (CN n c a) = (\<lambda> i. CN n (i*c) (nummul a i))"
   5.651 +  "nummul t = (\<lambda> i. Mul i t)"
   5.652 +
   5.653 +lemma nummul[simp]: "\<And> i. Inum bs (nummul t i) = Inum bs (Mul i t)"
   5.654 +by (induct t rule: nummul.induct, auto simp add: algebra_simps)
   5.655 +
   5.656 +lemma nummul_nb[simp]: "\<And> i. numbound0 t \<Longrightarrow> numbound0 (nummul t i)"
   5.657 +by (induct t rule: nummul.induct, auto )
   5.658 +
   5.659 +constdefs numneg :: "num \<Rightarrow> num"
   5.660 +  "numneg t \<equiv> nummul t (- 1)"
   5.661 +
   5.662 +constdefs numsub :: "num \<Rightarrow> num \<Rightarrow> num"
   5.663 +  "numsub s t \<equiv> (if s = t then C 0 else numadd (s,numneg t))"
   5.664 +
   5.665 +lemma numneg[simp]: "Inum bs (numneg t) = Inum bs (Neg t)"
   5.666 +using numneg_def by simp
   5.667 +
   5.668 +lemma numneg_nb[simp]: "numbound0 t \<Longrightarrow> numbound0 (numneg t)"
   5.669 +using numneg_def by simp
   5.670 +
   5.671 +lemma numsub[simp]: "Inum bs (numsub a b) = Inum bs (Sub a b)"
   5.672 +using numsub_def by simp
   5.673 +
   5.674 +lemma numsub_nb[simp]: "\<lbrakk> numbound0 t ; numbound0 s\<rbrakk> \<Longrightarrow> numbound0 (numsub t s)"
   5.675 +using numsub_def by simp
   5.676 +
   5.677 +recdef simpnum "measure size"
   5.678 +  "simpnum (C j) = C j"
   5.679 +  "simpnum (Bound n) = CN n 1 (C 0)"
   5.680 +  "simpnum (Neg t) = numneg (simpnum t)"
   5.681 +  "simpnum (Add t s) = numadd (simpnum t,simpnum s)"
   5.682 +  "simpnum (Sub t s) = numsub (simpnum t) (simpnum s)"
   5.683 +  "simpnum (Mul i t) = (if i = 0 then (C 0) else nummul (simpnum t) i)"
   5.684 +  "simpnum (CN n c t) = (if c = 0 then simpnum t else numadd (CN n c (C 0),simpnum t))"
   5.685 +
   5.686 +lemma simpnum_ci[simp]: "Inum bs (simpnum t) = Inum bs t"
   5.687 +by (induct t rule: simpnum.induct, auto simp add: numneg numadd numsub nummul)
   5.688 +
   5.689 +lemma simpnum_numbound0[simp]: 
   5.690 +  "numbound0 t \<Longrightarrow> numbound0 (simpnum t)"
   5.691 +by (induct t rule: simpnum.induct, auto)
   5.692 +
   5.693 +consts nozerocoeff:: "num \<Rightarrow> bool"
   5.694 +recdef nozerocoeff "measure size"
   5.695 +  "nozerocoeff (C c) = True"
   5.696 +  "nozerocoeff (CN n c t) = (c\<noteq>0 \<and> nozerocoeff t)"
   5.697 +  "nozerocoeff t = True"
   5.698 +
   5.699 +lemma numadd_nz : "nozerocoeff a \<Longrightarrow> nozerocoeff b \<Longrightarrow> nozerocoeff (numadd (a,b))"
   5.700 +by (induct a b rule: numadd.induct,auto simp add: Let_def)
   5.701 +
   5.702 +lemma nummul_nz : "\<And> i. i\<noteq>0 \<Longrightarrow> nozerocoeff a \<Longrightarrow> nozerocoeff (nummul a i)"
   5.703 +by (induct a rule: nummul.induct,auto simp add: Let_def numadd_nz)
   5.704 +
   5.705 +lemma numneg_nz : "nozerocoeff a \<Longrightarrow> nozerocoeff (numneg a)"
   5.706 +by (simp add: numneg_def nummul_nz)
   5.707 +
   5.708 +lemma numsub_nz: "nozerocoeff a \<Longrightarrow> nozerocoeff b \<Longrightarrow> nozerocoeff (numsub a b)"
   5.709 +by (simp add: numsub_def numneg_nz numadd_nz)
   5.710 +
   5.711 +lemma simpnum_nz: "nozerocoeff (simpnum t)"
   5.712 +by(induct t rule: simpnum.induct, auto simp add: numadd_nz numneg_nz numsub_nz nummul_nz)
   5.713 +
   5.714 +lemma maxcoeff_nz: "nozerocoeff t \<Longrightarrow> maxcoeff t = 0 \<Longrightarrow> t = C 0"
   5.715 +proof (induct t rule: maxcoeff.induct)
   5.716 +  case (2 n c t)
   5.717 +  hence cnz: "c \<noteq>0" and mx: "max (abs c) (maxcoeff t) = 0" by simp+
   5.718 +  have "max (abs c) (maxcoeff t) \<ge> abs c" by (simp add: le_maxI1)
   5.719 +  with cnz have "max (abs c) (maxcoeff t) > 0" by arith
   5.720 +  with prems show ?case by simp
   5.721 +qed auto
   5.722 +
   5.723 +lemma numgcd_nz: assumes nz: "nozerocoeff t" and g0: "numgcd t = 0" shows "t = C 0"
   5.724 +proof-
   5.725 +  from g0 have th:"numgcdh t (maxcoeff t) = 0" by (simp add: numgcd_def)
   5.726 +  from numgcdh0[OF th]  have th:"maxcoeff t = 0" .
   5.727 +  from maxcoeff_nz[OF nz th] show ?thesis .
   5.728 +qed
   5.729 +
   5.730 +constdefs simp_num_pair:: "(num \<times> int) \<Rightarrow> num \<times> int"
   5.731 +  "simp_num_pair \<equiv> (\<lambda> (t,n). (if n = 0 then (C 0, 0) else
   5.732 +   (let t' = simpnum t ; g = numgcd t' in 
   5.733 +      if g > 1 then (let g' = zgcd n g in 
   5.734 +        if g' = 1 then (t',n) 
   5.735 +        else (reducecoeffh t' g', n div g')) 
   5.736 +      else (t',n))))"
   5.737 +
   5.738 +lemma simp_num_pair_ci:
   5.739 +  shows "((\<lambda> (t,n). Inum bs t / real n) (simp_num_pair (t,n))) = ((\<lambda> (t,n). Inum bs t / real n) (t,n))"
   5.740 +  (is "?lhs = ?rhs")
   5.741 +proof-
   5.742 +  let ?t' = "simpnum t"
   5.743 +  let ?g = "numgcd ?t'"
   5.744 +  let ?g' = "zgcd n ?g"
   5.745 +  {assume nz: "n = 0" hence ?thesis by (simp add: Let_def simp_num_pair_def)}
   5.746 +  moreover
   5.747 +  { assume nnz: "n \<noteq> 0"
   5.748 +    {assume "\<not> ?g > 1" hence ?thesis by (simp add: Let_def simp_num_pair_def simpnum_ci)}
   5.749 +    moreover
   5.750 +    {assume g1:"?g>1" hence g0: "?g > 0" by simp
   5.751 +      from zgcd0 g1 nnz have gp0: "?g' \<noteq> 0" by simp
   5.752 +      hence g'p: "?g' > 0" using zgcd_pos[where i="n" and j="numgcd ?t'"] by arith 
   5.753 +      hence "?g'= 1 \<or> ?g' > 1" by arith
   5.754 +      moreover {assume "?g'=1" hence ?thesis by (simp add: Let_def simp_num_pair_def simpnum_ci)}
   5.755 +      moreover {assume g'1:"?g'>1"
   5.756 +	from dvdnumcoeff_aux2[OF g1] have th1:"dvdnumcoeff ?t' ?g" ..
   5.757 +	let ?tt = "reducecoeffh ?t' ?g'"
   5.758 +	let ?t = "Inum bs ?tt"
   5.759 +	have gpdg: "?g' dvd ?g" by (simp add: zgcd_zdvd2)
   5.760 +	have gpdd: "?g' dvd n" by (simp add: zgcd_zdvd1) 
   5.761 +	have gpdgp: "?g' dvd ?g'" by simp
   5.762 +	from reducecoeffh[OF dvdnumcoeff_trans[OF gpdg th1] g'p] 
   5.763 +	have th2:"real ?g' * ?t = Inum bs ?t'" by simp
   5.764 +	from prems have "?lhs = ?t / real (n div ?g')" by (simp add: simp_num_pair_def Let_def)
   5.765 +	also have "\<dots> = (real ?g' * ?t) / (real ?g' * (real (n div ?g')))" by simp
   5.766 +	also have "\<dots> = (Inum bs ?t' / real n)"
   5.767 +	  using real_of_int_div[OF gp0 gpdd] th2 gp0 by simp
   5.768 +	finally have "?lhs = Inum bs t / real n" by (simp add: simpnum_ci)
   5.769 +	then have ?thesis using prems by (simp add: simp_num_pair_def)}
   5.770 +      ultimately have ?thesis by blast}
   5.771 +    ultimately have ?thesis by blast} 
   5.772 +  ultimately show ?thesis by blast
   5.773 +qed
   5.774 +
   5.775 +lemma simp_num_pair_l: assumes tnb: "numbound0 t" and np: "n >0" and tn: "simp_num_pair (t,n) = (t',n')"
   5.776 +  shows "numbound0 t' \<and> n' >0"
   5.777 +proof-
   5.778 +    let ?t' = "simpnum t"
   5.779 +  let ?g = "numgcd ?t'"
   5.780 +  let ?g' = "zgcd n ?g"
   5.781 +  {assume nz: "n = 0" hence ?thesis using prems by (simp add: Let_def simp_num_pair_def)}
   5.782 +  moreover
   5.783 +  { assume nnz: "n \<noteq> 0"
   5.784 +    {assume "\<not> ?g > 1" hence ?thesis  using prems by (auto simp add: Let_def simp_num_pair_def simpnum_numbound0)}
   5.785 +    moreover
   5.786 +    {assume g1:"?g>1" hence g0: "?g > 0" by simp
   5.787 +      from zgcd0 g1 nnz have gp0: "?g' \<noteq> 0" by simp
   5.788 +      hence g'p: "?g' > 0" using zgcd_pos[where i="n" and j="numgcd ?t'"] by arith
   5.789 +      hence "?g'= 1 \<or> ?g' > 1" by arith
   5.790 +      moreover {assume "?g'=1" hence ?thesis using prems 
   5.791 +	  by (auto simp add: Let_def simp_num_pair_def simpnum_numbound0)}
   5.792 +      moreover {assume g'1:"?g'>1"
   5.793 +	have gpdg: "?g' dvd ?g" by (simp add: zgcd_zdvd2)
   5.794 +	have gpdd: "?g' dvd n" by (simp add: zgcd_zdvd1) 
   5.795 +	have gpdgp: "?g' dvd ?g'" by simp
   5.796 +	from zdvd_imp_le[OF gpdd np] have g'n: "?g' \<le> n" .
   5.797 +	from zdiv_mono1[OF g'n g'p, simplified zdiv_self[OF gp0]]
   5.798 +	have "n div ?g' >0" by simp
   5.799 +	hence ?thesis using prems 
   5.800 +	  by(auto simp add: simp_num_pair_def Let_def reducecoeffh_numbound0 simpnum_numbound0)}
   5.801 +      ultimately have ?thesis by blast}
   5.802 +    ultimately have ?thesis by blast} 
   5.803 +  ultimately show ?thesis by blast
   5.804 +qed
   5.805 +
   5.806 +consts simpfm :: "fm \<Rightarrow> fm"
   5.807 +recdef simpfm "measure fmsize"
   5.808 +  "simpfm (And p q) = conj (simpfm p) (simpfm q)"
   5.809 +  "simpfm (Or p q) = disj (simpfm p) (simpfm q)"
   5.810 +  "simpfm (Imp p q) = imp (simpfm p) (simpfm q)"
   5.811 +  "simpfm (Iff p q) = iff (simpfm p) (simpfm q)"
   5.812 +  "simpfm (NOT p) = not (simpfm p)"
   5.813 +  "simpfm (Lt a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v < 0) then T else F 
   5.814 +  | _ \<Rightarrow> Lt a')"
   5.815 +  "simpfm (Le a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v \<le> 0)  then T else F | _ \<Rightarrow> Le a')"
   5.816 +  "simpfm (Gt a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v > 0)  then T else F | _ \<Rightarrow> Gt a')"
   5.817 +  "simpfm (Ge a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v \<ge> 0)  then T else F | _ \<Rightarrow> Ge a')"
   5.818 +  "simpfm (Eq a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v = 0)  then T else F | _ \<Rightarrow> Eq a')"
   5.819 +  "simpfm (NEq a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v \<noteq> 0)  then T else F | _ \<Rightarrow> NEq a')"
   5.820 +  "simpfm p = p"
   5.821 +lemma simpfm: "Ifm bs (simpfm p) = Ifm bs p"
   5.822 +proof(induct p rule: simpfm.induct)
   5.823 +  case (6 a) let ?sa = "simpnum a" from simpnum_ci have sa: "Inum bs ?sa = Inum bs a" by simp
   5.824 +  {fix v assume "?sa = C v" hence ?case using sa by simp }
   5.825 +  moreover {assume "\<not> (\<exists> v. ?sa = C v)" hence ?case using sa 
   5.826 +      by (cases ?sa, simp_all add: Let_def)}
   5.827 +  ultimately show ?case by blast
   5.828 +next
   5.829 +  case (7 a)  let ?sa = "simpnum a" 
   5.830 +  from simpnum_ci have sa: "Inum bs ?sa = Inum bs a" by simp
   5.831 +  {fix v assume "?sa = C v" hence ?case using sa by simp }
   5.832 +  moreover {assume "\<not> (\<exists> v. ?sa = C v)" hence ?case using sa 
   5.833 +      by (cases ?sa, simp_all add: Let_def)}
   5.834 +  ultimately show ?case by blast
   5.835 +next
   5.836 +  case (8 a)  let ?sa = "simpnum a" 
   5.837 +  from simpnum_ci have sa: "Inum bs ?sa = Inum bs a" by simp
   5.838 +  {fix v assume "?sa = C v" hence ?case using sa by simp }
   5.839 +  moreover {assume "\<not> (\<exists> v. ?sa = C v)" hence ?case using sa 
   5.840 +      by (cases ?sa, simp_all add: Let_def)}
   5.841 +  ultimately show ?case by blast
   5.842 +next
   5.843 +  case (9 a)  let ?sa = "simpnum a" 
   5.844 +  from simpnum_ci have sa: "Inum bs ?sa = Inum bs a" by simp
   5.845 +  {fix v assume "?sa = C v" hence ?case using sa by simp }
   5.846 +  moreover {assume "\<not> (\<exists> v. ?sa = C v)" hence ?case using sa 
   5.847 +      by (cases ?sa, simp_all add: Let_def)}
   5.848 +  ultimately show ?case by blast
   5.849 +next
   5.850 +  case (10 a)  let ?sa = "simpnum a" 
   5.851 +  from simpnum_ci have sa: "Inum bs ?sa = Inum bs a" by simp
   5.852 +  {fix v assume "?sa = C v" hence ?case using sa by simp }
   5.853 +  moreover {assume "\<not> (\<exists> v. ?sa = C v)" hence ?case using sa 
   5.854 +      by (cases ?sa, simp_all add: Let_def)}
   5.855 +  ultimately show ?case by blast
   5.856 +next
   5.857 +  case (11 a)  let ?sa = "simpnum a" 
   5.858 +  from simpnum_ci have sa: "Inum bs ?sa = Inum bs a" by simp
   5.859 +  {fix v assume "?sa = C v" hence ?case using sa by simp }
   5.860 +  moreover {assume "\<not> (\<exists> v. ?sa = C v)" hence ?case using sa 
   5.861 +      by (cases ?sa, simp_all add: Let_def)}
   5.862 +  ultimately show ?case by blast
   5.863 +qed (induct p rule: simpfm.induct, simp_all add: conj disj imp iff not)
   5.864 +
   5.865 +
   5.866 +lemma simpfm_bound0: "bound0 p \<Longrightarrow> bound0 (simpfm p)"
   5.867 +proof(induct p rule: simpfm.induct)
   5.868 +  case (6 a) hence nb: "numbound0 a" by simp
   5.869 +  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
   5.870 +  thus ?case by (cases "simpnum a", auto simp add: Let_def)
   5.871 +next
   5.872 +  case (7 a) hence nb: "numbound0 a" by simp
   5.873 +  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
   5.874 +  thus ?case by (cases "simpnum a", auto simp add: Let_def)
   5.875 +next
   5.876 +  case (8 a) hence nb: "numbound0 a" by simp
   5.877 +  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
   5.878 +  thus ?case by (cases "simpnum a", auto simp add: Let_def)
   5.879 +next
   5.880 +  case (9 a) hence nb: "numbound0 a" by simp
   5.881 +  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
   5.882 +  thus ?case by (cases "simpnum a", auto simp add: Let_def)
   5.883 +next
   5.884 +  case (10 a) hence nb: "numbound0 a" by simp
   5.885 +  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
   5.886 +  thus ?case by (cases "simpnum a", auto simp add: Let_def)
   5.887 +next
   5.888 +  case (11 a) hence nb: "numbound0 a" by simp
   5.889 +  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
   5.890 +  thus ?case by (cases "simpnum a", auto simp add: Let_def)
   5.891 +qed(auto simp add: disj_def imp_def iff_def conj_def not_bn)
   5.892 +
   5.893 +lemma simpfm_qf: "qfree p \<Longrightarrow> qfree (simpfm p)"
   5.894 +by (induct p rule: simpfm.induct, auto simp add: disj_qf imp_qf iff_qf conj_qf not_qf Let_def)
   5.895 + (case_tac "simpnum a",auto)+
   5.896 +
   5.897 +consts prep :: "fm \<Rightarrow> fm"
   5.898 +recdef prep "measure fmsize"
   5.899 +  "prep (E T) = T"
   5.900 +  "prep (E F) = F"
   5.901 +  "prep (E (Or p q)) = disj (prep (E p)) (prep (E q))"
   5.902 +  "prep (E (Imp p q)) = disj (prep (E (NOT p))) (prep (E q))"
   5.903 +  "prep (E (Iff p q)) = disj (prep (E (And p q))) (prep (E (And (NOT p) (NOT q))))" 
   5.904 +  "prep (E (NOT (And p q))) = disj (prep (E (NOT p))) (prep (E(NOT q)))"
   5.905 +  "prep (E (NOT (Imp p q))) = prep (E (And p (NOT q)))"
   5.906 +  "prep (E (NOT (Iff p q))) = disj (prep (E (And p (NOT q)))) (prep (E(And (NOT p) q)))"
   5.907 +  "prep (E p) = E (prep p)"
   5.908 +  "prep (A (And p q)) = conj (prep (A p)) (prep (A q))"
   5.909 +  "prep (A p) = prep (NOT (E (NOT p)))"
   5.910 +  "prep (NOT (NOT p)) = prep p"
   5.911 +  "prep (NOT (And p q)) = disj (prep (NOT p)) (prep (NOT q))"
   5.912 +  "prep (NOT (A p)) = prep (E (NOT p))"
   5.913 +  "prep (NOT (Or p q)) = conj (prep (NOT p)) (prep (NOT q))"
   5.914 +  "prep (NOT (Imp p q)) = conj (prep p) (prep (NOT q))"
   5.915 +  "prep (NOT (Iff p q)) = disj (prep (And p (NOT q))) (prep (And (NOT p) q))"
   5.916 +  "prep (NOT p) = not (prep p)"
   5.917 +  "prep (Or p q) = disj (prep p) (prep q)"
   5.918 +  "prep (And p q) = conj (prep p) (prep q)"
   5.919 +  "prep (Imp p q) = prep (Or (NOT p) q)"
   5.920 +  "prep (Iff p q) = disj (prep (And p q)) (prep (And (NOT p) (NOT q)))"
   5.921 +  "prep p = p"
   5.922 +(hints simp add: fmsize_pos)
   5.923 +lemma prep: "\<And> bs. Ifm bs (prep p) = Ifm bs p"
   5.924 +by (induct p rule: prep.induct, auto)
   5.925 +
   5.926 +  (* Generic quantifier elimination *)
   5.927 +consts qelim :: "fm \<Rightarrow> (fm \<Rightarrow> fm) \<Rightarrow> fm"
   5.928 +recdef qelim "measure fmsize"
   5.929 +  "qelim (E p) = (\<lambda> qe. DJ qe (qelim p qe))"
   5.930 +  "qelim (A p) = (\<lambda> qe. not (qe ((qelim (NOT p) qe))))"
   5.931 +  "qelim (NOT p) = (\<lambda> qe. not (qelim p qe))"
   5.932 +  "qelim (And p q) = (\<lambda> qe. conj (qelim p qe) (qelim q qe))" 
   5.933 +  "qelim (Or  p q) = (\<lambda> qe. disj (qelim p qe) (qelim q qe))" 
   5.934 +  "qelim (Imp p q) = (\<lambda> qe. imp (qelim p qe) (qelim q qe))"
   5.935 +  "qelim (Iff p q) = (\<lambda> qe. iff (qelim p qe) (qelim q qe))"
   5.936 +  "qelim p = (\<lambda> y. simpfm p)"
   5.937 +
   5.938 +lemma qelim_ci:
   5.939 +  assumes qe_inv: "\<forall> bs p. qfree p \<longrightarrow> qfree (qe p) \<and> (Ifm bs (qe p) = Ifm bs (E p))"
   5.940 +  shows "\<And> bs. qfree (qelim p qe) \<and> (Ifm bs (qelim p qe) = Ifm bs p)"
   5.941 +using qe_inv DJ_qe[OF qe_inv] 
   5.942 +by(induct p rule: qelim.induct) 
   5.943 +(auto simp add: not disj conj iff imp not_qf disj_qf conj_qf imp_qf iff_qf 
   5.944 +  simpfm simpfm_qf simp del: simpfm.simps)
   5.945 +
   5.946 +consts 
   5.947 +  plusinf:: "fm \<Rightarrow> fm" (* Virtual substitution of +\<infinity>*)
   5.948 +  minusinf:: "fm \<Rightarrow> fm" (* Virtual substitution of -\<infinity>*)
   5.949 +recdef minusinf "measure size"
   5.950 +  "minusinf (And p q) = conj (minusinf p) (minusinf q)" 
   5.951 +  "minusinf (Or p q) = disj (minusinf p) (minusinf q)" 
   5.952 +  "minusinf (Eq  (CN 0 c e)) = F"
   5.953 +  "minusinf (NEq (CN 0 c e)) = T"
   5.954 +  "minusinf (Lt  (CN 0 c e)) = T"
   5.955 +  "minusinf (Le  (CN 0 c e)) = T"
   5.956 +  "minusinf (Gt  (CN 0 c e)) = F"
   5.957 +  "minusinf (Ge  (CN 0 c e)) = F"
   5.958 +  "minusinf p = p"
   5.959 +
   5.960 +recdef plusinf "measure size"
   5.961 +  "plusinf (And p q) = conj (plusinf p) (plusinf q)" 
   5.962 +  "plusinf (Or p q) = disj (plusinf p) (plusinf q)" 
   5.963 +  "plusinf (Eq  (CN 0 c e)) = F"
   5.964 +  "plusinf (NEq (CN 0 c e)) = T"
   5.965 +  "plusinf (Lt  (CN 0 c e)) = F"
   5.966 +  "plusinf (Le  (CN 0 c e)) = F"
   5.967 +  "plusinf (Gt  (CN 0 c e)) = T"
   5.968 +  "plusinf (Ge  (CN 0 c e)) = T"
   5.969 +  "plusinf p = p"
   5.970 +
   5.971 +consts
   5.972 +  isrlfm :: "fm \<Rightarrow> bool"   (* Linearity test for fm *)
   5.973 +recdef isrlfm "measure size"
   5.974 +  "isrlfm (And p q) = (isrlfm p \<and> isrlfm q)" 
   5.975 +  "isrlfm (Or p q) = (isrlfm p \<and> isrlfm q)" 
   5.976 +  "isrlfm (Eq  (CN 0 c e)) = (c>0 \<and> numbound0 e)"
   5.977 +  "isrlfm (NEq (CN 0 c e)) = (c>0 \<and> numbound0 e)"
   5.978 +  "isrlfm (Lt  (CN 0 c e)) = (c>0 \<and> numbound0 e)"
   5.979 +  "isrlfm (Le  (CN 0 c e)) = (c>0 \<and> numbound0 e)"
   5.980 +  "isrlfm (Gt  (CN 0 c e)) = (c>0 \<and> numbound0 e)"
   5.981 +  "isrlfm (Ge  (CN 0 c e)) = (c>0 \<and> numbound0 e)"
   5.982 +  "isrlfm p = (isatom p \<and> (bound0 p))"
   5.983 +
   5.984 +  (* splits the bounded from the unbounded part*)
   5.985 +consts rsplit0 :: "num \<Rightarrow> int \<times> num" 
   5.986 +recdef rsplit0 "measure num_size"
   5.987 +  "rsplit0 (Bound 0) = (1,C 0)"
   5.988 +  "rsplit0 (Add a b) = (let (ca,ta) = rsplit0 a ; (cb,tb) = rsplit0 b 
   5.989 +              in (ca+cb, Add ta tb))"
   5.990 +  "rsplit0 (Sub a b) = rsplit0 (Add a (Neg b))"
   5.991 +  "rsplit0 (Neg a) = (let (c,t) = rsplit0 a in (-c,Neg t))"
   5.992 +  "rsplit0 (Mul c a) = (let (ca,ta) = rsplit0 a in (c*ca,Mul c ta))"
   5.993 +  "rsplit0 (CN 0 c a) = (let (ca,ta) = rsplit0 a in (c+ca,ta))"
   5.994 +  "rsplit0 (CN n c a) = (let (ca,ta) = rsplit0 a in (ca,CN n c ta))"
   5.995 +  "rsplit0 t = (0,t)"
   5.996 +lemma rsplit0: 
   5.997 +  shows "Inum bs ((split (CN 0)) (rsplit0 t)) = Inum bs t \<and> numbound0 (snd (rsplit0 t))"
   5.998 +proof (induct t rule: rsplit0.induct)
   5.999 +  case (2 a b) 
  5.1000 +  let ?sa = "rsplit0 a" let ?sb = "rsplit0 b"
  5.1001 +  let ?ca = "fst ?sa" let ?cb = "fst ?sb"
  5.1002 +  let ?ta = "snd ?sa" let ?tb = "snd ?sb"
  5.1003 +  from prems have nb: "numbound0 (snd(rsplit0 (Add a b)))" 
  5.1004 +    by(cases "rsplit0 a",auto simp add: Let_def split_def)
  5.1005 +  have "Inum bs ((split (CN 0)) (rsplit0 (Add a b))) = 
  5.1006 +    Inum bs ((split (CN 0)) ?sa)+Inum bs ((split (CN 0)) ?sb)"
  5.1007 +    by (simp add: Let_def split_def algebra_simps)