established session HOL-Reflection
authorhaftmann
Tue Feb 03 16:50:41 2009 +0100 (2009-02-03)
changeset 297881b80ebe713a4
parent 29787 23bf900a21db
child 29789 b4534c3e68f6
established session HOL-Reflection
NEWS
src/HOL/IsaMakefile
src/HOL/Reflection/Cooper.thy
src/HOL/Reflection/MIR.thy
src/HOL/Reflection/cooper_tac.ML
src/HOL/Reflection/ferrack_tac.ML
src/HOL/Reflection/mir_tac.ML
src/HOL/ex/MIR.thy
src/HOL/ex/ROOT.ML
src/HOL/ex/ReflectedFerrack.thy
src/HOL/ex/Reflected_Presburger.thy
src/HOL/ex/coopertac.ML
src/HOL/ex/linrtac.ML
src/HOL/ex/mirtac.ML
     1.1 --- a/NEWS	Tue Feb 03 16:50:40 2009 +0100
     1.2 +++ b/NEWS	Tue Feb 03 16:50:41 2009 +0100
     1.3 @@ -193,7 +193,8 @@
     1.4  
     1.5  *** HOL ***
     1.6  
     1.7 -* Theory "Reflection" now resides in HOL/Library.
     1.8 +* Theory "Reflection" now resides in HOL/Library.  Common reflection examples
     1.9 +(Cooper, MIR, Ferrack) now in distinct session directory HOL/Reflection.
    1.10  
    1.11  * Entry point to Word library now simply named "Word".  INCOMPATIBILITY.
    1.12  
     2.1 --- a/src/HOL/IsaMakefile	Tue Feb 03 16:50:40 2009 +0100
     2.2 +++ b/src/HOL/IsaMakefile	Tue Feb 03 16:50:41 2009 +0100
     2.3 @@ -36,6 +36,7 @@
     2.4    HOL-Nominal-Examples \
     2.5    HOL-NumberTheory \
     2.6    HOL-Prolog \
     2.7 +  HOL-Reflection \
     2.8    HOL-SET-Protocol \
     2.9    HOL-SizeChange \
    2.10    HOL-Statespace \
    2.11 @@ -678,6 +679,21 @@
    2.12  	@$(ISABELLE_TOOL) usedir $(OUT)/HOL Prolog
    2.13  
    2.14  
    2.15 +## HOL-Reflection
    2.16 +
    2.17 +HOL-Reflection: HOL $(LOG)/HOL-Reflection.gz
    2.18 +
    2.19 +$(LOG)/HOL-Reflection.gz: $(OUT)/HOL \
    2.20 +  Reflection/Cooper.thy \
    2.21 +  Reflection/cooper_tac.ML \
    2.22 +  Reflection/Ferrack.thy \
    2.23 +  Reflection/ferrack_tac.ML \
    2.24 +  Reflection/MIR.thy \
    2.25 +  Reflection/mir_tac.ML \
    2.26 +  Reflection/ROOT.ML
    2.27 +	@$(ISABELLE_TOOL) usedir $(OUT)/HOL Reflection
    2.28 +
    2.29 +
    2.30  ## HOL-W0
    2.31  
    2.32  HOL-W0: HOL $(LOG)/HOL-W0.gz
    2.33 @@ -812,7 +828,6 @@
    2.34    ex/Numeral.thy ex/PER.thy ex/PresburgerEx.thy ex/Primrec.thy		\
    2.35    ex/Quickcheck_Examples.thy	\
    2.36    ex/ReflectionEx.thy ex/ROOT.ML ex/Recdefs.thy ex/Records.thy		\
    2.37 -  ex/Reflected_Presburger.thy ex/coopertac.ML				\
    2.38    ex/Refute_Examples.thy ex/SAT_Examples.thy ex/SVC_Oracle.thy		\
    2.39    ex/Subarray.thy ex/Sublist.thy                                        \
    2.40    ex/Sudoku.thy ex/Tarski.thy ex/Termination.thy ex/Term_Of_Syntax.thy	\
    2.41 @@ -822,10 +837,7 @@
    2.42    ex/ImperativeQuicksort.thy	\
    2.43    ex/BigO_Complex.thy			\
    2.44    ex/Arithmetic_Series_Complex.thy ex/HarmonicSeries.thy	\
    2.45 -  ex/Sqrt.thy							\
    2.46 -  ex/Sqrt_Script.thy ex/MIR.thy ex/mirtac.ML	\
    2.47 -  ex/ReflectedFerrack.thy					\
    2.48 -  ex/linrtac.ML
    2.49 +  ex/Sqrt.thy ex/Sqrt_Script.thy
    2.50  	@$(ISABELLE_TOOL) usedir $(OUT)/HOL ex
    2.51  
    2.52  
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/HOL/Reflection/Cooper.thy	Tue Feb 03 16:50:41 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/Reflection/MIR.thy	Tue Feb 03 16:50:41 2009 +0100
     4.3 @@ -0,0 +1,5933 @@
     4.4 +(*  Title:      HOL/Reflection/MIR.thy
     4.5 +    Author:     Amine Chaieb
     4.6 +*)
     4.7 +
     4.8 +theory MIR
     4.9 +imports Complex_Main Efficient_Nat
    4.10 +uses ("mir_tac.ML")
    4.11 +begin
    4.12 +
    4.13 +section {* Quantifier elimination for @{text "\<real> (0, 1, +, floor, <)"} *}
    4.14 +
    4.15 +declare real_of_int_floor_cancel [simp del]
    4.16 +
    4.17 +primrec alluopairs:: "'a list \<Rightarrow> ('a \<times> 'a) list" where 
    4.18 +  "alluopairs [] = []"
    4.19 +| "alluopairs (x#xs) = (map (Pair x) (x#xs))@(alluopairs xs)"
    4.20 +
    4.21 +lemma alluopairs_set1: "set (alluopairs xs) \<le> {(x,y). x\<in> set xs \<and> y\<in> set xs}"
    4.22 +by (induct xs, auto)
    4.23 +
    4.24 +lemma alluopairs_set:
    4.25 +  "\<lbrakk>x\<in> set xs ; y \<in> set xs\<rbrakk> \<Longrightarrow> (x,y) \<in> set (alluopairs xs) \<or> (y,x) \<in> set (alluopairs xs) "
    4.26 +by (induct xs, auto)
    4.27 +
    4.28 +lemma alluopairs_ex:
    4.29 +  assumes Pc: "\<forall> x y. P x y = P y x"
    4.30 +  shows "(\<exists> x \<in> set xs. \<exists> y \<in> set xs. P x y) = (\<exists> (x,y) \<in> set (alluopairs xs). P x y)"
    4.31 +proof
    4.32 +  assume "\<exists>x\<in>set xs. \<exists>y\<in>set xs. P x y"
    4.33 +  then obtain x y where x: "x \<in> set xs" and y:"y \<in> set xs" and P: "P x y"  by blast
    4.34 +  from alluopairs_set[OF x y] P Pc show"\<exists>(x, y)\<in>set (alluopairs xs). P x y" 
    4.35 +    by auto
    4.36 +next
    4.37 +  assume "\<exists>(x, y)\<in>set (alluopairs xs). P x y"
    4.38 +  then obtain "x" and "y"  where xy:"(x,y) \<in> set (alluopairs xs)" and P: "P x y" by blast+
    4.39 +  from xy have "x \<in> set xs \<and> y\<in> set xs" using alluopairs_set1 by blast
    4.40 +  with P show "\<exists>x\<in>set xs. \<exists>y\<in>set xs. P x y" by blast
    4.41 +qed
    4.42 +
    4.43 +  (* generate a list from i to j*)
    4.44 +consts iupt :: "int \<times> int \<Rightarrow> int list"
    4.45 +recdef iupt "measure (\<lambda> (i,j). nat (j-i +1))" 
    4.46 +  "iupt (i,j) = (if j <i then [] else (i# iupt(i+1, j)))"
    4.47 +
    4.48 +lemma iupt_set: "set (iupt(i,j)) = {i .. j}"
    4.49 +proof(induct rule: iupt.induct)
    4.50 +  case (1 a b)
    4.51 +  show ?case
    4.52 +    using prems by (simp add: simp_from_to)
    4.53 +qed
    4.54 +
    4.55 +lemma nth_pos2: "0 < n \<Longrightarrow> (x#xs) ! n = xs ! (n - 1)"
    4.56 +using Nat.gr0_conv_Suc
    4.57 +by clarsimp
    4.58 +
    4.59 +
    4.60 +lemma myl: "\<forall> (a::'a::{pordered_ab_group_add}) (b::'a). (a \<le> b) = (0 \<le> b - a)" 
    4.61 +proof(clarify)
    4.62 +  fix x y ::"'a"
    4.63 +  have "(x \<le> y) = (x - y \<le> 0)" by (simp only: le_iff_diff_le_0[where a="x" and b="y"])
    4.64 +  also have "\<dots> = (- (y - x) \<le> 0)" by simp
    4.65 +  also have "\<dots> = (0 \<le> y - x)" by (simp only: neg_le_0_iff_le[where a="y-x"])
    4.66 +  finally show "(x \<le> y) = (0 \<le> y - x)" .
    4.67 +qed
    4.68 +
    4.69 +lemma myless: "\<forall> (a::'a::{pordered_ab_group_add}) (b::'a). (a < b) = (0 < b - a)" 
    4.70 +proof(clarify)
    4.71 +  fix x y ::"'a"
    4.72 +  have "(x < y) = (x - y < 0)" by (simp only: less_iff_diff_less_0[where a="x" and b="y"])
    4.73 +  also have "\<dots> = (- (y - x) < 0)" by simp
    4.74 +  also have "\<dots> = (0 < y - x)" by (simp only: neg_less_0_iff_less[where a="y-x"])
    4.75 +  finally show "(x < y) = (0 < y - x)" .
    4.76 +qed
    4.77 +
    4.78 +lemma myeq: "\<forall> (a::'a::{pordered_ab_group_add}) (b::'a). (a = b) = (0 = b - a)"
    4.79 +  by auto
    4.80 +
    4.81 +  (* Maybe should be added to the library \<dots> *)
    4.82 +lemma floor_int_eq: "(real n\<le> x \<and> x < real (n+1)) = (floor x = n)"
    4.83 +proof( auto)
    4.84 +  assume lb: "real n \<le> x"
    4.85 +    and ub: "x < real n + 1"
    4.86 +  have "real (floor x) \<le> x" by simp 
    4.87 +  hence "real (floor x) < real (n + 1) " using ub by arith
    4.88 +  hence "floor x < n+1" by simp
    4.89 +  moreover from lb have "n \<le> floor x" using floor_mono2[where x="real n" and y="x"] 
    4.90 +    by simp ultimately show "floor x = n" by simp
    4.91 +qed
    4.92 +
    4.93 +(* Periodicity of dvd *)
    4.94 +lemma dvd_period:
    4.95 +  assumes advdd: "(a::int) dvd d"
    4.96 +  shows "(a dvd (x + t)) = (a dvd ((x+ c*d) + t))"
    4.97 +  using advdd  
    4.98 +proof-
    4.99 +  {fix x k
   4.100 +    from inf_period(3)[OF advdd, rule_format, where x=x and k="-k"]  
   4.101 +    have " ((a::int) dvd (x + t)) = (a dvd (x+k*d + t))" by simp}
   4.102 +  hence "\<forall>x.\<forall>k. ((a::int) dvd (x + t)) = (a dvd (x+k*d + t))"  by simp
   4.103 +  then show ?thesis by simp
   4.104 +qed
   4.105 +
   4.106 +  (* The Divisibility relation between reals *)	
   4.107 +definition
   4.108 +  rdvd:: "real \<Rightarrow> real \<Rightarrow> bool" (infixl "rdvd" 50)
   4.109 +where
   4.110 +  rdvd_def: "x rdvd y \<longleftrightarrow> (\<exists>k\<Colon>int. y = x * real k)"
   4.111 +
   4.112 +lemma int_rdvd_real: 
   4.113 +  shows "real (i::int) rdvd x = (i dvd (floor x) \<and> real (floor x) = x)" (is "?l = ?r")
   4.114 +proof
   4.115 +  assume "?l" 
   4.116 +  hence th: "\<exists> k. x=real (i*k)" by (simp add: rdvd_def)
   4.117 +  hence th': "real (floor x) = x" by (auto simp del: real_of_int_mult)
   4.118 +  with th have "\<exists> k. real (floor x) = real (i*k)" by simp
   4.119 +  hence "\<exists> k. floor x = i*k" by (simp only: real_of_int_inject)
   4.120 +  thus ?r  using th' by (simp add: dvd_def) 
   4.121 +next
   4.122 +  assume "?r" hence "(i\<Colon>int) dvd \<lfloor>x\<Colon>real\<rfloor>" ..
   4.123 +  hence "\<exists> k. real (floor x) = real (i*k)" 
   4.124 +    by (simp only: real_of_int_inject) (simp add: dvd_def)
   4.125 +  thus ?l using prems by (simp add: rdvd_def)
   4.126 +qed
   4.127 +
   4.128 +lemma int_rdvd_iff: "(real (i::int) rdvd real t) = (i dvd t)"
   4.129 +by (auto simp add: rdvd_def dvd_def) (rule_tac x="k" in exI, simp only :real_of_int_mult[symmetric])
   4.130 +
   4.131 +
   4.132 +lemma rdvd_abs1: 
   4.133 +  "(abs (real d) rdvd t) = (real (d ::int) rdvd t)"
   4.134 +proof
   4.135 +  assume d: "real d rdvd t"
   4.136 +  from d int_rdvd_real have d2: "d dvd (floor t)" and ti: "real (floor t) = t" by auto
   4.137 +
   4.138 +  from iffD2[OF zdvd_abs1] d2 have "(abs d) dvd (floor t)" by blast
   4.139 +  with ti int_rdvd_real[symmetric] have "real (abs d) rdvd t" by blast 
   4.140 +  thus "abs (real d) rdvd t" by simp
   4.141 +next
   4.142 +  assume "abs (real d) rdvd t" hence "real (abs d) rdvd t" by simp
   4.143 +  with int_rdvd_real[where i="abs d" and x="t"] have d2: "abs d dvd floor t" and ti: "real (floor t) =t" by auto
   4.144 +  from iffD1[OF zdvd_abs1] d2 have "d dvd floor t" by blast
   4.145 +  with ti int_rdvd_real[symmetric] show "real d rdvd t" by blast
   4.146 +qed
   4.147 +
   4.148 +lemma rdvd_minus: "(real (d::int) rdvd t) = (real d rdvd -t)"
   4.149 +  apply (auto simp add: rdvd_def)
   4.150 +  apply (rule_tac x="-k" in exI, simp) 
   4.151 +  apply (rule_tac x="-k" in exI, simp)
   4.152 +done
   4.153 +
   4.154 +lemma rdvd_left_0_eq: "(0 rdvd t) = (t=0)"
   4.155 +by (auto simp add: rdvd_def)
   4.156 +
   4.157 +lemma rdvd_mult: 
   4.158 +  assumes knz: "k\<noteq>0"
   4.159 +  shows "(real (n::int) * real (k::int) rdvd x * real k) = (real n rdvd x)"
   4.160 +using knz by (simp add:rdvd_def)
   4.161 +
   4.162 +lemma rdvd_trans: assumes mn:"m rdvd n" and  nk:"n rdvd k" 
   4.163 +  shows "m rdvd k"
   4.164 +proof-
   4.165 +  from rdvd_def mn obtain c where nmc:"n = m * real (c::int)" by auto
   4.166 +  from rdvd_def nk obtain c' where nkc:"k = n * real (c'::int)" by auto
   4.167 +  hence "k = m * real (c * c')" using nmc by simp
   4.168 +  thus ?thesis using rdvd_def by blast
   4.169 +qed
   4.170 +
   4.171 +  (*********************************************************************************)
   4.172 +  (****                            SHADOW SYNTAX AND SEMANTICS                  ****)
   4.173 +  (*********************************************************************************)
   4.174 +
   4.175 +datatype num = C int | Bound nat | CN nat int num | Neg num | Add num num| Sub num num 
   4.176 +  | Mul int num | Floor num| CF int num num
   4.177 +
   4.178 +  (* A size for num to make inductive proofs simpler*)
   4.179 +primrec num_size :: "num \<Rightarrow> nat" where
   4.180 + "num_size (C c) = 1"
   4.181 +| "num_size (Bound n) = 1"
   4.182 +| "num_size (Neg a) = 1 + num_size a"
   4.183 +| "num_size (Add a b) = 1 + num_size a + num_size b"
   4.184 +| "num_size (Sub a b) = 3 + num_size a + num_size b"
   4.185 +| "num_size (CN n c a) = 4 + num_size a "
   4.186 +| "num_size (CF c a b) = 4 + num_size a + num_size b"
   4.187 +| "num_size (Mul c a) = 1 + num_size a"
   4.188 +| "num_size (Floor a) = 1 + num_size a"
   4.189 +
   4.190 +  (* Semantics of numeral terms (num) *)
   4.191 +primrec Inum :: "real list \<Rightarrow> num \<Rightarrow> real" where
   4.192 +  "Inum bs (C c) = (real c)"
   4.193 +| "Inum bs (Bound n) = bs!n"
   4.194 +| "Inum bs (CN n c a) = (real c) * (bs!n) + (Inum bs a)"
   4.195 +| "Inum bs (Neg a) = -(Inum bs a)"
   4.196 +| "Inum bs (Add a b) = Inum bs a + Inum bs b"
   4.197 +| "Inum bs (Sub a b) = Inum bs a - Inum bs b"
   4.198 +| "Inum bs (Mul c a) = (real c) * Inum bs a"
   4.199 +| "Inum bs (Floor a) = real (floor (Inum bs a))"
   4.200 +| "Inum bs (CF c a b) = real c * real (floor (Inum bs a)) + Inum bs b"
   4.201 +definition "isint t bs \<equiv> real (floor (Inum bs t)) = Inum bs t"
   4.202 +
   4.203 +lemma isint_iff: "isint n bs = (real (floor (Inum bs n)) = Inum bs n)"
   4.204 +by (simp add: isint_def)
   4.205 +
   4.206 +lemma isint_Floor: "isint (Floor n) bs"
   4.207 +  by (simp add: isint_iff)
   4.208 +
   4.209 +lemma isint_Mul: "isint e bs \<Longrightarrow> isint (Mul c e) bs"
   4.210 +proof-
   4.211 +  let ?e = "Inum bs e"
   4.212 +  let ?fe = "floor ?e"
   4.213 +  assume be: "isint e bs" hence efe:"real ?fe = ?e" by (simp add: isint_iff)
   4.214 +  have "real ((floor (Inum bs (Mul c e)))) = real (floor (real (c * ?fe)))" using efe by simp
   4.215 +  also have "\<dots> = real (c* ?fe)" by (simp only: floor_real_of_int) 
   4.216 +  also have "\<dots> = real c * ?e" using efe by simp
   4.217 +  finally show ?thesis using isint_iff by simp
   4.218 +qed
   4.219 +
   4.220 +lemma isint_neg: "isint e bs \<Longrightarrow> isint (Neg e) bs"
   4.221 +proof-
   4.222 +  let ?I = "\<lambda> t. Inum bs t"
   4.223 +  assume ie: "isint e bs"
   4.224 +  hence th: "real (floor (?I e)) = ?I e" by (simp add: isint_def)  
   4.225 +  have "real (floor (?I (Neg e))) = real (floor (- (real (floor (?I e)))))" by (simp add: th)
   4.226 +  also have "\<dots> = - real (floor (?I e))" by(simp add: floor_minus_real_of_int) 
   4.227 +  finally show "isint (Neg e) bs" by (simp add: isint_def th)
   4.228 +qed
   4.229 +
   4.230 +lemma isint_sub: 
   4.231 +  assumes ie: "isint e bs" shows "isint (Sub (C c) e) bs"
   4.232 +proof-
   4.233 +  let ?I = "\<lambda> t. Inum bs t"
   4.234 +  from ie have th: "real (floor (?I e)) = ?I e" by (simp add: isint_def)  
   4.235 +  have "real (floor (?I (Sub (C c) e))) = real (floor ((real (c -floor (?I e)))))" by (simp add: th)
   4.236 +  also have "\<dots> = real (c- floor (?I e))" by(simp add: floor_minus_real_of_int) 
   4.237 +  finally show "isint (Sub (C c) e) bs" by (simp add: isint_def th)
   4.238 +qed
   4.239 +
   4.240 +lemma isint_add: assumes
   4.241 +  ai:"isint a bs" and bi: "isint b bs" shows "isint (Add a b) bs"
   4.242 +proof-
   4.243 +  let ?a = "Inum bs a"
   4.244 +  let ?b = "Inum bs b"
   4.245 +  from ai bi isint_iff have "real (floor (?a + ?b)) = real (floor (real (floor ?a) + real (floor ?b)))" by simp
   4.246 +  also have "\<dots> = real (floor ?a) + real (floor ?b)" by simp
   4.247 +  also have "\<dots> = ?a + ?b" using ai bi isint_iff by simp
   4.248 +  finally show "isint (Add a b) bs" by (simp add: isint_iff)
   4.249 +qed
   4.250 +
   4.251 +lemma isint_c: "isint (C j) bs"
   4.252 +  by (simp add: isint_iff)
   4.253 +
   4.254 +
   4.255 +    (* FORMULAE *)
   4.256 +datatype fm  = 
   4.257 +  T| F| Lt num| Le num| Gt num| Ge num| Eq num| NEq num| Dvd int num| NDvd int num|
   4.258 +  NOT fm| And fm fm|  Or fm fm| Imp fm fm| Iff fm fm| E fm| A fm
   4.259 +
   4.260 +
   4.261 +  (* A size for fm *)
   4.262 +fun fmsize :: "fm \<Rightarrow> nat" where
   4.263 + "fmsize (NOT p) = 1 + fmsize p"
   4.264 +| "fmsize (And p q) = 1 + fmsize p + fmsize q"
   4.265 +| "fmsize (Or p q) = 1 + fmsize p + fmsize q"
   4.266 +| "fmsize (Imp p q) = 3 + fmsize p + fmsize q"
   4.267 +| "fmsize (Iff p q) = 3 + 2*(fmsize p + fmsize q)"
   4.268 +| "fmsize (E p) = 1 + fmsize p"
   4.269 +| "fmsize (A p) = 4+ fmsize p"
   4.270 +| "fmsize (Dvd i t) = 2"
   4.271 +| "fmsize (NDvd i t) = 2"
   4.272 +| "fmsize p = 1"
   4.273 +  (* several lemmas about fmsize *)
   4.274 +lemma fmsize_pos: "fmsize p > 0"	
   4.275 +by (induct p rule: fmsize.induct) simp_all
   4.276 +
   4.277 +  (* Semantics of formulae (fm) *)
   4.278 +primrec Ifm ::"real list \<Rightarrow> fm \<Rightarrow> bool" where
   4.279 +  "Ifm bs T = True"
   4.280 +| "Ifm bs F = False"
   4.281 +| "Ifm bs (Lt a) = (Inum bs a < 0)"
   4.282 +| "Ifm bs (Gt a) = (Inum bs a > 0)"
   4.283 +| "Ifm bs (Le a) = (Inum bs a \<le> 0)"
   4.284 +| "Ifm bs (Ge a) = (Inum bs a \<ge> 0)"
   4.285 +| "Ifm bs (Eq a) = (Inum bs a = 0)"
   4.286 +| "Ifm bs (NEq a) = (Inum bs a \<noteq> 0)"
   4.287 +| "Ifm bs (Dvd i b) = (real i rdvd Inum bs b)"
   4.288 +| "Ifm bs (NDvd i b) = (\<not>(real i rdvd Inum bs b))"
   4.289 +| "Ifm bs (NOT p) = (\<not> (Ifm bs p))"
   4.290 +| "Ifm bs (And p q) = (Ifm bs p \<and> Ifm bs q)"
   4.291 +| "Ifm bs (Or p q) = (Ifm bs p \<or> Ifm bs q)"
   4.292 +| "Ifm bs (Imp p q) = ((Ifm bs p) \<longrightarrow> (Ifm bs q))"
   4.293 +| "Ifm bs (Iff p q) = (Ifm bs p = Ifm bs q)"
   4.294 +| "Ifm bs (E p) = (\<exists> x. Ifm (x#bs) p)"
   4.295 +| "Ifm bs (A p) = (\<forall> x. Ifm (x#bs) p)"
   4.296 +
   4.297 +consts prep :: "fm \<Rightarrow> fm"
   4.298 +recdef prep "measure fmsize"
   4.299 +  "prep (E T) = T"
   4.300 +  "prep (E F) = F"
   4.301 +  "prep (E (Or p q)) = Or (prep (E p)) (prep (E q))"
   4.302 +  "prep (E (Imp p q)) = Or (prep (E (NOT p))) (prep (E q))"
   4.303 +  "prep (E (Iff p q)) = Or (prep (E (And p q))) (prep (E (And (NOT p) (NOT q))))" 
   4.304 +  "prep (E (NOT (And p q))) = Or (prep (E (NOT p))) (prep (E(NOT q)))"
   4.305 +  "prep (E (NOT (Imp p q))) = prep (E (And p (NOT q)))"
   4.306 +  "prep (E (NOT (Iff p q))) = Or (prep (E (And p (NOT q)))) (prep (E(And (NOT p) q)))"
   4.307 +  "prep (E p) = E (prep p)"
   4.308 +  "prep (A (And p q)) = And (prep (A p)) (prep (A q))"
   4.309 +  "prep (A p) = prep (NOT (E (NOT p)))"
   4.310 +  "prep (NOT (NOT p)) = prep p"
   4.311 +  "prep (NOT (And p q)) = Or (prep (NOT p)) (prep (NOT q))"
   4.312 +  "prep (NOT (A p)) = prep (E (NOT p))"
   4.313 +  "prep (NOT (Or p q)) = And (prep (NOT p)) (prep (NOT q))"
   4.314 +  "prep (NOT (Imp p q)) = And (prep p) (prep (NOT q))"
   4.315 +  "prep (NOT (Iff p q)) = Or (prep (And p (NOT q))) (prep (And (NOT p) q))"
   4.316 +  "prep (NOT p) = NOT (prep p)"
   4.317 +  "prep (Or p q) = Or (prep p) (prep q)"
   4.318 +  "prep (And p q) = And (prep p) (prep q)"
   4.319 +  "prep (Imp p q) = prep (Or (NOT p) q)"
   4.320 +  "prep (Iff p q) = Or (prep (And p q)) (prep (And (NOT p) (NOT q)))"
   4.321 +  "prep p = p"
   4.322 +(hints simp add: fmsize_pos)
   4.323 +lemma prep: "\<And> bs. Ifm bs (prep p) = Ifm bs p"
   4.324 +by (induct p rule: prep.induct, auto)
   4.325 +
   4.326 +
   4.327 +  (* Quantifier freeness *)
   4.328 +fun qfree:: "fm \<Rightarrow> bool" where
   4.329 +  "qfree (E p) = False"
   4.330 +  | "qfree (A p) = False"
   4.331 +  | "qfree (NOT p) = qfree p" 
   4.332 +  | "qfree (And p q) = (qfree p \<and> qfree q)" 
   4.333 +  | "qfree (Or  p q) = (qfree p \<and> qfree q)" 
   4.334 +  | "qfree (Imp p q) = (qfree p \<and> qfree q)" 
   4.335 +  | "qfree (Iff p q) = (qfree p \<and> qfree q)"
   4.336 +  | "qfree p = True"
   4.337 +
   4.338 +  (* Boundedness and substitution *)
   4.339 +primrec numbound0 :: "num \<Rightarrow> bool" (* a num is INDEPENDENT of Bound 0 *) where
   4.340 +  "numbound0 (C c) = True"
   4.341 +  | "numbound0 (Bound n) = (n>0)"
   4.342 +  | "numbound0 (CN n i a) = (n > 0 \<and> numbound0 a)"
   4.343 +  | "numbound0 (Neg a) = numbound0 a"
   4.344 +  | "numbound0 (Add a b) = (numbound0 a \<and> numbound0 b)"
   4.345 +  | "numbound0 (Sub a b) = (numbound0 a \<and> numbound0 b)" 
   4.346 +  | "numbound0 (Mul i a) = numbound0 a"
   4.347 +  | "numbound0 (Floor a) = numbound0 a"
   4.348 +  | "numbound0 (CF c a b) = (numbound0 a \<and> numbound0 b)" 
   4.349 +
   4.350 +lemma numbound0_I:
   4.351 +  assumes nb: "numbound0 a"
   4.352 +  shows "Inum (b#bs) a = Inum (b'#bs) a"
   4.353 +  using nb by (induct a) (auto simp add: nth_pos2)
   4.354 +
   4.355 +lemma numbound0_gen: 
   4.356 +  assumes nb: "numbound0 t" and ti: "isint t (x#bs)"
   4.357 +  shows "\<forall> y. isint t (y#bs)"
   4.358 +using nb ti 
   4.359 +proof(clarify)
   4.360 +  fix y
   4.361 +  from numbound0_I[OF nb, where bs="bs" and b="y" and b'="x"] ti[simplified isint_def]
   4.362 +  show "isint t (y#bs)"
   4.363 +    by (simp add: isint_def)
   4.364 +qed
   4.365 +
   4.366 +primrec bound0:: "fm \<Rightarrow> bool" (* A Formula is independent of Bound 0 *) where
   4.367 +  "bound0 T = True"
   4.368 +  | "bound0 F = True"
   4.369 +  | "bound0 (Lt a) = numbound0 a"
   4.370 +  | "bound0 (Le a) = numbound0 a"
   4.371 +  | "bound0 (Gt a) = numbound0 a"
   4.372 +  | "bound0 (Ge a) = numbound0 a"
   4.373 +  | "bound0 (Eq a) = numbound0 a"
   4.374 +  | "bound0 (NEq a) = numbound0 a"
   4.375 +  | "bound0 (Dvd i a) = numbound0 a"
   4.376 +  | "bound0 (NDvd i a) = numbound0 a"
   4.377 +  | "bound0 (NOT p) = bound0 p"
   4.378 +  | "bound0 (And p q) = (bound0 p \<and> bound0 q)"
   4.379 +  | "bound0 (Or p q) = (bound0 p \<and> bound0 q)"
   4.380 +  | "bound0 (Imp p q) = ((bound0 p) \<and> (bound0 q))"
   4.381 +  | "bound0 (Iff p q) = (bound0 p \<and> bound0 q)"
   4.382 +  | "bound0 (E p) = False"
   4.383 +  | "bound0 (A p) = False"
   4.384 +
   4.385 +lemma bound0_I:
   4.386 +  assumes bp: "bound0 p"
   4.387 +  shows "Ifm (b#bs) p = Ifm (b'#bs) p"
   4.388 + using bp numbound0_I [where b="b" and bs="bs" and b'="b'"]
   4.389 +  by (induct p) (auto simp add: nth_pos2)
   4.390 +
   4.391 +primrec numsubst0:: "num \<Rightarrow> num \<Rightarrow> num" (* substitute a num into a num for Bound 0 *) where
   4.392 +  "numsubst0 t (C c) = (C c)"
   4.393 +  | "numsubst0 t (Bound n) = (if n=0 then t else Bound n)"
   4.394 +  | "numsubst0 t (CN n i a) = (if n=0 then Add (Mul i t) (numsubst0 t a) else CN n i (numsubst0 t a))"
   4.395 +  | "numsubst0 t (CF i a b) = CF i (numsubst0 t a) (numsubst0 t b)"
   4.396 +  | "numsubst0 t (Neg a) = Neg (numsubst0 t a)"
   4.397 +  | "numsubst0 t (Add a b) = Add (numsubst0 t a) (numsubst0 t b)"
   4.398 +  | "numsubst0 t (Sub a b) = Sub (numsubst0 t a) (numsubst0 t b)" 
   4.399 +  | "numsubst0 t (Mul i a) = Mul i (numsubst0 t a)"
   4.400 +  | "numsubst0 t (Floor a) = Floor (numsubst0 t a)"
   4.401 +
   4.402 +lemma numsubst0_I:
   4.403 +  shows "Inum (b#bs) (numsubst0 a t) = Inum ((Inum (b#bs) a)#bs) t"
   4.404 +  by (induct t) (simp_all add: nth_pos2)
   4.405 +
   4.406 +lemma numsubst0_I':
   4.407 +  assumes nb: "numbound0 a"
   4.408 +  shows "Inum (b#bs) (numsubst0 a t) = Inum ((Inum (b'#bs) a)#bs) t"
   4.409 +  by (induct t) (simp_all add: nth_pos2 numbound0_I[OF nb, where b="b" and b'="b'"])
   4.410 +
   4.411 +primrec subst0:: "num \<Rightarrow> fm \<Rightarrow> fm" (* substitue a num into a formula for Bound 0 *) where
   4.412 +  "subst0 t T = T"
   4.413 +  | "subst0 t F = F"
   4.414 +  | "subst0 t (Lt a) = Lt (numsubst0 t a)"
   4.415 +  | "subst0 t (Le a) = Le (numsubst0 t a)"
   4.416 +  | "subst0 t (Gt a) = Gt (numsubst0 t a)"
   4.417 +  | "subst0 t (Ge a) = Ge (numsubst0 t a)"
   4.418 +  | "subst0 t (Eq a) = Eq (numsubst0 t a)"
   4.419 +  | "subst0 t (NEq a) = NEq (numsubst0 t a)"
   4.420 +  | "subst0 t (Dvd i a) = Dvd i (numsubst0 t a)"
   4.421 +  | "subst0 t (NDvd i a) = NDvd i (numsubst0 t a)"
   4.422 +  | "subst0 t (NOT p) = NOT (subst0 t p)"
   4.423 +  | "subst0 t (And p q) = And (subst0 t p) (subst0 t q)"
   4.424 +  | "subst0 t (Or p q) = Or (subst0 t p) (subst0 t q)"
   4.425 +  | "subst0 t (Imp p q) = Imp (subst0 t p) (subst0 t q)"
   4.426 +  | "subst0 t (Iff p q) = Iff (subst0 t p) (subst0 t q)"
   4.427 +
   4.428 +lemma subst0_I: assumes qfp: "qfree p"
   4.429 +  shows "Ifm (b#bs) (subst0 a p) = Ifm ((Inum (b#bs) a)#bs) p"
   4.430 +  using qfp numsubst0_I[where b="b" and bs="bs" and a="a"]
   4.431 +  by (induct p) (simp_all add: nth_pos2 )
   4.432 +
   4.433 +consts
   4.434 +  decrnum:: "num \<Rightarrow> num" 
   4.435 +  decr :: "fm \<Rightarrow> fm"
   4.436 +
   4.437 +recdef decrnum "measure size"
   4.438 +  "decrnum (Bound n) = Bound (n - 1)"
   4.439 +  "decrnum (Neg a) = Neg (decrnum a)"
   4.440 +  "decrnum (Add a b) = Add (decrnum a) (decrnum b)"
   4.441 +  "decrnum (Sub a b) = Sub (decrnum a) (decrnum b)"
   4.442 +  "decrnum (Mul c a) = Mul c (decrnum a)"
   4.443 +  "decrnum (Floor a) = Floor (decrnum a)"
   4.444 +  "decrnum (CN n c a) = CN (n - 1) c (decrnum a)"
   4.445 +  "decrnum (CF c a b) = CF c (decrnum a) (decrnum b)"
   4.446 +  "decrnum a = a"
   4.447 +
   4.448 +recdef decr "measure size"
   4.449 +  "decr (Lt a) = Lt (decrnum a)"
   4.450 +  "decr (Le a) = Le (decrnum a)"
   4.451 +  "decr (Gt a) = Gt (decrnum a)"
   4.452 +  "decr (Ge a) = Ge (decrnum a)"
   4.453 +  "decr (Eq a) = Eq (decrnum a)"
   4.454 +  "decr (NEq a) = NEq (decrnum a)"
   4.455 +  "decr (Dvd i a) = Dvd i (decrnum a)"
   4.456 +  "decr (NDvd i a) = NDvd i (decrnum a)"
   4.457 +  "decr (NOT p) = NOT (decr p)" 
   4.458 +  "decr (And p q) = And (decr p) (decr q)"
   4.459 +  "decr (Or p q) = Or (decr p) (decr q)"
   4.460 +  "decr (Imp p q) = Imp (decr p) (decr q)"
   4.461 +  "decr (Iff p q) = Iff (decr p) (decr q)"
   4.462 +  "decr p = p"
   4.463 +
   4.464 +lemma decrnum: assumes nb: "numbound0 t"
   4.465 +  shows "Inum (x#bs) t = Inum bs (decrnum t)"
   4.466 +  using nb by (induct t rule: decrnum.induct, simp_all add: nth_pos2)
   4.467 +
   4.468 +lemma decr: assumes nb: "bound0 p"
   4.469 +  shows "Ifm (x#bs) p = Ifm bs (decr p)"
   4.470 +  using nb 
   4.471 +  by (induct p rule: decr.induct, simp_all add: nth_pos2 decrnum)
   4.472 +
   4.473 +lemma decr_qf: "bound0 p \<Longrightarrow> qfree (decr p)"
   4.474 +by (induct p, simp_all)
   4.475 +
   4.476 +consts 
   4.477 +  isatom :: "fm \<Rightarrow> bool" (* test for atomicity *)
   4.478 +recdef isatom "measure size"
   4.479 +  "isatom T = True"
   4.480 +  "isatom F = True"
   4.481 +  "isatom (Lt a) = True"
   4.482 +  "isatom (Le a) = True"
   4.483 +  "isatom (Gt a) = True"
   4.484 +  "isatom (Ge a) = True"
   4.485 +  "isatom (Eq a) = True"
   4.486 +  "isatom (NEq a) = True"
   4.487 +  "isatom (Dvd i b) = True"
   4.488 +  "isatom (NDvd i b) = True"
   4.489 +  "isatom p = False"
   4.490 +
   4.491 +lemma numsubst0_numbound0: assumes nb: "numbound0 t"
   4.492 +  shows "numbound0 (numsubst0 t a)"
   4.493 +using nb by (induct a, auto)
   4.494 +
   4.495 +lemma subst0_bound0: assumes qf: "qfree p" and nb: "numbound0 t"
   4.496 +  shows "bound0 (subst0 t p)"
   4.497 +using qf numsubst0_numbound0[OF nb] by (induct p, auto)
   4.498 +
   4.499 +lemma bound0_qf: "bound0 p \<Longrightarrow> qfree p"
   4.500 +by (induct p, simp_all)
   4.501 +
   4.502 +
   4.503 +definition djf:: "('a \<Rightarrow> fm) \<Rightarrow> 'a \<Rightarrow> fm \<Rightarrow> fm" where
   4.504 +  "djf f p q = (if q=T then T else if q=F then f p else 
   4.505 +  (let fp = f p in case fp of T \<Rightarrow> T | F \<Rightarrow> q | _ \<Rightarrow> Or fp q))"
   4.506 +
   4.507 +definition evaldjf:: "('a \<Rightarrow> fm) \<Rightarrow> 'a list \<Rightarrow> fm" where
   4.508 +  "evaldjf f ps = foldr (djf f) ps F"
   4.509 +
   4.510 +lemma djf_Or: "Ifm bs (djf f p q) = Ifm bs (Or (f p) q)"
   4.511 +by (cases "q=T", simp add: djf_def,cases "q=F",simp add: djf_def) 
   4.512 +(cases "f p", simp_all add: Let_def djf_def) 
   4.513 +
   4.514 +lemma evaldjf_ex: "Ifm bs (evaldjf f ps) = (\<exists> p \<in> set ps. Ifm bs (f p))"
   4.515 +  by(induct ps, simp_all add: evaldjf_def djf_Or)
   4.516 +
   4.517 +lemma evaldjf_bound0: 
   4.518 +  assumes nb: "\<forall> x\<in> set xs. bound0 (f x)"
   4.519 +  shows "bound0 (evaldjf f xs)"
   4.520 +  using nb by (induct xs, auto simp add: evaldjf_def djf_def Let_def) (case_tac "f a", auto) 
   4.521 +
   4.522 +lemma evaldjf_qf: 
   4.523 +  assumes nb: "\<forall> x\<in> set xs. qfree (f x)"
   4.524 +  shows "qfree (evaldjf f xs)"
   4.525 +  using nb by (induct xs, auto simp add: evaldjf_def djf_def Let_def) (case_tac "f a", auto) 
   4.526 +
   4.527 +consts 
   4.528 +  disjuncts :: "fm \<Rightarrow> fm list" 
   4.529 +  conjuncts :: "fm \<Rightarrow> fm list"
   4.530 +recdef disjuncts "measure size"
   4.531 +  "disjuncts (Or p q) = (disjuncts p) @ (disjuncts q)"
   4.532 +  "disjuncts F = []"
   4.533 +  "disjuncts p = [p]"
   4.534 +
   4.535 +recdef conjuncts "measure size"
   4.536 +  "conjuncts (And p q) = (conjuncts p) @ (conjuncts q)"
   4.537 +  "conjuncts T = []"
   4.538 +  "conjuncts p = [p]"
   4.539 +lemma disjuncts: "(\<exists> q\<in> set (disjuncts p). Ifm bs q) = Ifm bs p"
   4.540 +by(induct p rule: disjuncts.induct, auto)
   4.541 +lemma conjuncts: "(\<forall> q\<in> set (conjuncts p). Ifm bs q) = Ifm bs p"
   4.542 +by(induct p rule: conjuncts.induct, auto)
   4.543 +
   4.544 +lemma disjuncts_nb: "bound0 p \<Longrightarrow> \<forall> q\<in> set (disjuncts p). bound0 q"
   4.545 +proof-
   4.546 +  assume nb: "bound0 p"
   4.547 +  hence "list_all bound0 (disjuncts p)" by (induct p rule:disjuncts.induct,auto)
   4.548 +  thus ?thesis by (simp only: list_all_iff)
   4.549 +qed
   4.550 +lemma conjuncts_nb: "bound0 p \<Longrightarrow> \<forall> q\<in> set (conjuncts p). bound0 q"
   4.551 +proof-
   4.552 +  assume nb: "bound0 p"
   4.553 +  hence "list_all bound0 (conjuncts p)" by (induct p rule:conjuncts.induct,auto)
   4.554 +  thus ?thesis by (simp only: list_all_iff)
   4.555 +qed
   4.556 +
   4.557 +lemma disjuncts_qf: "qfree p \<Longrightarrow> \<forall> q\<in> set (disjuncts p). qfree q"
   4.558 +proof-
   4.559 +  assume qf: "qfree p"
   4.560 +  hence "list_all qfree (disjuncts p)"
   4.561 +    by (induct p rule: disjuncts.induct, auto)
   4.562 +  thus ?thesis by (simp only: list_all_iff)
   4.563 +qed
   4.564 +lemma conjuncts_qf: "qfree p \<Longrightarrow> \<forall> q\<in> set (conjuncts p). qfree q"
   4.565 +proof-
   4.566 +  assume qf: "qfree p"
   4.567 +  hence "list_all qfree (conjuncts p)"
   4.568 +    by (induct p rule: conjuncts.induct, auto)
   4.569 +  thus ?thesis by (simp only: list_all_iff)
   4.570 +qed
   4.571 +
   4.572 +constdefs DJ :: "(fm \<Rightarrow> fm) \<Rightarrow> fm \<Rightarrow> fm"
   4.573 +  "DJ f p \<equiv> evaldjf f (disjuncts p)"
   4.574 +
   4.575 +lemma DJ: assumes fdj: "\<forall> p q. f (Or p q) = Or (f p) (f q)"
   4.576 +  and fF: "f F = F"
   4.577 +  shows "Ifm bs (DJ f p) = Ifm bs (f p)"
   4.578 +proof-
   4.579 +  have "Ifm bs (DJ f p) = (\<exists> q \<in> set (disjuncts p). Ifm bs (f q))"
   4.580 +    by (simp add: DJ_def evaldjf_ex) 
   4.581 +  also have "\<dots> = Ifm bs (f p)" using fdj fF by (induct p rule: disjuncts.induct, auto)
   4.582 +  finally show ?thesis .
   4.583 +qed
   4.584 +
   4.585 +lemma DJ_qf: assumes 
   4.586 +  fqf: "\<forall> p. qfree p \<longrightarrow> qfree (f p)"
   4.587 +  shows "\<forall>p. qfree p \<longrightarrow> qfree (DJ f p) "
   4.588 +proof(clarify)
   4.589 +  fix  p assume qf: "qfree p"
   4.590 +  have th: "DJ f p = evaldjf f (disjuncts p)" by (simp add: DJ_def)
   4.591 +  from disjuncts_qf[OF qf] have "\<forall> q\<in> set (disjuncts p). qfree q" .
   4.592 +  with fqf have th':"\<forall> q\<in> set (disjuncts p). qfree (f q)" by blast
   4.593 +  
   4.594 +  from evaldjf_qf[OF th'] th show "qfree (DJ f p)" by simp
   4.595 +qed
   4.596 +
   4.597 +lemma DJ_qe: assumes qe: "\<forall> bs p. qfree p \<longrightarrow> qfree (qe p) \<and> (Ifm bs (qe p) = Ifm bs (E p))"
   4.598 +  shows "\<forall> bs p. qfree p \<longrightarrow> qfree (DJ qe p) \<and> (Ifm bs ((DJ qe p)) = Ifm bs (E p))"
   4.599 +proof(clarify)
   4.600 +  fix p::fm and bs
   4.601 +  assume qf: "qfree p"
   4.602 +  from qe have qth: "\<forall> p. qfree p \<longrightarrow> qfree (qe p)" by blast
   4.603 +  from DJ_qf[OF qth] qf have qfth:"qfree (DJ qe p)" by auto
   4.604 +  have "Ifm bs (DJ qe p) = (\<exists> q\<in> set (disjuncts p). Ifm bs (qe q))"
   4.605 +    by (simp add: DJ_def evaldjf_ex)
   4.606 +  also have "\<dots> = (\<exists> q \<in> set(disjuncts p). Ifm bs (E q))" using qe disjuncts_qf[OF qf] by auto
   4.607 +  also have "\<dots> = Ifm bs (E p)" by (induct p rule: disjuncts.induct, auto)
   4.608 +  finally show "qfree (DJ qe p) \<and> Ifm bs (DJ qe p) = Ifm bs (E p)" using qfth by blast
   4.609 +qed
   4.610 +  (* Simplification *)
   4.611 +
   4.612 +  (* Algebraic simplifications for nums *)
   4.613 +consts bnds:: "num \<Rightarrow> nat list"
   4.614 +  lex_ns:: "nat list \<times> nat list \<Rightarrow> bool"
   4.615 +recdef bnds "measure size"
   4.616 +  "bnds (Bound n) = [n]"
   4.617 +  "bnds (CN n c a) = n#(bnds a)"
   4.618 +  "bnds (Neg a) = bnds a"
   4.619 +  "bnds (Add a b) = (bnds a)@(bnds b)"
   4.620 +  "bnds (Sub a b) = (bnds a)@(bnds b)"
   4.621 +  "bnds (Mul i a) = bnds a"
   4.622 +  "bnds (Floor a) = bnds a"
   4.623 +  "bnds (CF c a b) = (bnds a)@(bnds b)"
   4.624 +  "bnds a = []"
   4.625 +recdef lex_ns "measure (\<lambda> (xs,ys). length xs + length ys)"
   4.626 +  "lex_ns ([], ms) = True"
   4.627 +  "lex_ns (ns, []) = False"
   4.628 +  "lex_ns (n#ns, m#ms) = (n<m \<or> ((n = m) \<and> lex_ns (ns,ms))) "
   4.629 +constdefs lex_bnd :: "num \<Rightarrow> num \<Rightarrow> bool"
   4.630 +  "lex_bnd t s \<equiv> lex_ns (bnds t, bnds s)"
   4.631 +
   4.632 +consts 
   4.633 +  numgcdh:: "num \<Rightarrow> int \<Rightarrow> int"
   4.634 +  reducecoeffh:: "num \<Rightarrow> int \<Rightarrow> num"
   4.635 +  dvdnumcoeff:: "num \<Rightarrow> int \<Rightarrow> bool"
   4.636 +consts maxcoeff:: "num \<Rightarrow> int"
   4.637 +recdef maxcoeff "measure size"
   4.638 +  "maxcoeff (C i) = abs i"
   4.639 +  "maxcoeff (CN n c t) = max (abs c) (maxcoeff t)"
   4.640 +  "maxcoeff (CF c t s) = max (abs c) (maxcoeff s)"
   4.641 +  "maxcoeff t = 1"
   4.642 +
   4.643 +lemma maxcoeff_pos: "maxcoeff t \<ge> 0"
   4.644 +  apply (induct t rule: maxcoeff.induct, auto) 
   4.645 +  done
   4.646 +
   4.647 +recdef numgcdh "measure size"
   4.648 +  "numgcdh (C i) = (\<lambda>g. zgcd i g)"
   4.649 +  "numgcdh (CN n c t) = (\<lambda>g. zgcd c (numgcdh t g))"
   4.650 +  "numgcdh (CF c s t) = (\<lambda>g. zgcd c (numgcdh t g))"
   4.651 +  "numgcdh t = (\<lambda>g. 1)"
   4.652 +
   4.653 +definition
   4.654 +  numgcd :: "num \<Rightarrow> int"
   4.655 +where
   4.656 +  numgcd_def: "numgcd t = numgcdh t (maxcoeff t)"
   4.657 +
   4.658 +recdef reducecoeffh "measure size"
   4.659 +  "reducecoeffh (C i) = (\<lambda> g. C (i div g))"
   4.660 +  "reducecoeffh (CN n c t) = (\<lambda> g. CN n (c div g) (reducecoeffh t g))"
   4.661 +  "reducecoeffh (CF c s t) = (\<lambda> g. CF (c div g)  s (reducecoeffh t g))"
   4.662 +  "reducecoeffh t = (\<lambda>g. t)"
   4.663 +
   4.664 +definition
   4.665 +  reducecoeff :: "num \<Rightarrow> num"
   4.666 +where
   4.667 +  reducecoeff_def: "reducecoeff t =
   4.668 +  (let g = numgcd t in 
   4.669 +  if g = 0 then C 0 else if g=1 then t else reducecoeffh t g)"
   4.670 +
   4.671 +recdef dvdnumcoeff "measure size"
   4.672 +  "dvdnumcoeff (C i) = (\<lambda> g. g dvd i)"
   4.673 +  "dvdnumcoeff (CN n c t) = (\<lambda> g. g dvd c \<and> (dvdnumcoeff t g))"
   4.674 +  "dvdnumcoeff (CF c s t) = (\<lambda> g. g dvd c \<and> (dvdnumcoeff t g))"
   4.675 +  "dvdnumcoeff t = (\<lambda>g. False)"
   4.676 +
   4.677 +lemma dvdnumcoeff_trans: 
   4.678 +  assumes gdg: "g dvd g'" and dgt':"dvdnumcoeff t g'"
   4.679 +  shows "dvdnumcoeff t g"
   4.680 +  using dgt' gdg 
   4.681 +  by (induct t rule: dvdnumcoeff.induct, simp_all add: gdg zdvd_trans[OF gdg])
   4.682 +
   4.683 +declare zdvd_trans [trans add]
   4.684 +
   4.685 +lemma natabs0: "(nat (abs x) = 0) = (x = 0)"
   4.686 +by arith
   4.687 +
   4.688 +lemma numgcd0:
   4.689 +  assumes g0: "numgcd t = 0"
   4.690 +  shows "Inum bs t = 0"
   4.691 +proof-
   4.692 +  have "\<And>x. numgcdh t x= 0 \<Longrightarrow> Inum bs t = 0"
   4.693 +    by (induct t rule: numgcdh.induct, auto simp add: zgcd_def gcd_zero natabs0 max_def maxcoeff_pos)
   4.694 +  thus ?thesis using g0[simplified numgcd_def] by blast
   4.695 +qed
   4.696 +
   4.697 +lemma numgcdh_pos: assumes gp: "g \<ge> 0" shows "numgcdh t g \<ge> 0"
   4.698 +  using gp
   4.699 +  by (induct t rule: numgcdh.induct, auto simp add: zgcd_def)
   4.700 +
   4.701 +lemma numgcd_pos: "numgcd t \<ge>0"
   4.702 +  by (simp add: numgcd_def numgcdh_pos maxcoeff_pos)
   4.703 +
   4.704 +lemma reducecoeffh:
   4.705 +  assumes gt: "dvdnumcoeff t g" and gp: "g > 0" 
   4.706 +  shows "real g *(Inum bs (reducecoeffh t g)) = Inum bs t"
   4.707 +  using gt
   4.708 +proof(induct t rule: reducecoeffh.induct) 
   4.709 +  case (1 i) hence gd: "g dvd i" by simp
   4.710 +  from gp have gnz: "g \<noteq> 0" by simp
   4.711 +  from prems show ?case by (simp add: real_of_int_div[OF gnz gd])
   4.712 +next
   4.713 +  case (2 n c t)  hence gd: "g dvd c" by simp
   4.714 +  from gp have gnz: "g \<noteq> 0" by simp
   4.715 +  from prems show ?case by (simp add: real_of_int_div[OF gnz gd] algebra_simps)
   4.716 +next
   4.717 +  case (3 c s t)  hence gd: "g dvd c" by simp
   4.718 +  from gp have gnz: "g \<noteq> 0" by simp
   4.719 +  from prems show ?case by (simp add: real_of_int_div[OF gnz gd] algebra_simps) 
   4.720 +qed (auto simp add: numgcd_def gp)
   4.721 +consts ismaxcoeff:: "num \<Rightarrow> int \<Rightarrow> bool"
   4.722 +recdef ismaxcoeff "measure size"
   4.723 +  "ismaxcoeff (C i) = (\<lambda> x. abs i \<le> x)"
   4.724 +  "ismaxcoeff (CN n c t) = (\<lambda>x. abs c \<le> x \<and> (ismaxcoeff t x))"
   4.725 +  "ismaxcoeff (CF c s t) = (\<lambda>x. abs c \<le> x \<and> (ismaxcoeff t x))"
   4.726 +  "ismaxcoeff t = (\<lambda>x. True)"
   4.727 +
   4.728 +lemma ismaxcoeff_mono: "ismaxcoeff t c \<Longrightarrow> c \<le> c' \<Longrightarrow> ismaxcoeff t c'"
   4.729 +by (induct t rule: ismaxcoeff.induct, auto)
   4.730 +
   4.731 +lemma maxcoeff_ismaxcoeff: "ismaxcoeff t (maxcoeff t)"
   4.732 +proof (induct t rule: maxcoeff.induct)
   4.733 +  case (2 n c t)
   4.734 +  hence H:"ismaxcoeff t (maxcoeff t)" .
   4.735 +  have thh: "maxcoeff t \<le> max (abs c) (maxcoeff t)" by (simp add: le_maxI2)
   4.736 +  from ismaxcoeff_mono[OF H thh] show ?case by (simp add: le_maxI1)
   4.737 +next
   4.738 +  case (3 c t s) 
   4.739 +  hence H1:"ismaxcoeff s (maxcoeff s)" by auto
   4.740 +  have thh1: "maxcoeff s \<le> max \<bar>c\<bar> (maxcoeff s)" by (simp add: max_def)
   4.741 +  from ismaxcoeff_mono[OF H1 thh1] show ?case by (simp add: le_maxI1)
   4.742 +qed simp_all
   4.743 +
   4.744 +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))"
   4.745 +  apply (unfold zgcd_def)
   4.746 +  apply (cases "i = 0", simp_all)
   4.747 +  apply (cases "j = 0", simp_all)
   4.748 +  apply (cases "abs i = 1", simp_all)
   4.749 +  apply (cases "abs j = 1", simp_all)
   4.750 +  apply auto
   4.751 +  done
   4.752 +lemma numgcdh0:"numgcdh t m = 0 \<Longrightarrow>  m =0"
   4.753 +  by (induct t rule: numgcdh.induct, auto simp add:zgcd0)
   4.754 +
   4.755 +lemma dvdnumcoeff_aux:
   4.756 +  assumes "ismaxcoeff t m" and mp:"m \<ge> 0" and "numgcdh t m > 1"
   4.757 +  shows "dvdnumcoeff t (numgcdh t m)"
   4.758 +using prems
   4.759 +proof(induct t rule: numgcdh.induct)
   4.760 +  case (2 n c t) 
   4.761 +  let ?g = "numgcdh t m"
   4.762 +  from prems have th:"zgcd c ?g > 1" by simp
   4.763 +  from zgcd_gt1[OF th] numgcdh_pos[OF mp, where t="t"]
   4.764 +  have "(abs c > 1 \<and> ?g > 1) \<or> (abs c = 0 \<and> ?g > 1) \<or> (abs c > 1 \<and> ?g = 0)" by simp
   4.765 +  moreover {assume "abs c > 1" and gp: "?g > 1" with prems
   4.766 +    have th: "dvdnumcoeff t ?g" by simp
   4.767 +    have th': "zgcd c ?g dvd ?g" by (simp add:zgcd_zdvd2)
   4.768 +    from dvdnumcoeff_trans[OF th' th] have ?case by (simp add: zgcd_zdvd1)}
   4.769 +  moreover {assume "abs c = 0 \<and> ?g > 1"
   4.770 +    with prems have th: "dvdnumcoeff t ?g" by simp
   4.771 +    have th': "zgcd c ?g dvd ?g" by (simp add:zgcd_zdvd2)
   4.772 +    from dvdnumcoeff_trans[OF th' th] have ?case by (simp add: zgcd_zdvd1)
   4.773 +    hence ?case by simp }
   4.774 +  moreover {assume "abs c > 1" and g0:"?g = 0" 
   4.775 +    from numgcdh0[OF g0] have "m=0". with prems   have ?case by simp }
   4.776 +  ultimately show ?case by blast
   4.777 +next
   4.778 +  case (3 c s t) 
   4.779 +  let ?g = "numgcdh t m"
   4.780 +  from prems have th:"zgcd c ?g > 1" by simp
   4.781 +  from zgcd_gt1[OF th] numgcdh_pos[OF mp, where t="t"]
   4.782 +  have "(abs c > 1 \<and> ?g > 1) \<or> (abs c = 0 \<and> ?g > 1) \<or> (abs c > 1 \<and> ?g = 0)" by simp
   4.783 +  moreover {assume "abs c > 1" and gp: "?g > 1" with prems
   4.784 +    have th: "dvdnumcoeff t ?g" by simp
   4.785 +    have th': "zgcd c ?g dvd ?g" by (simp add:zgcd_zdvd2)
   4.786 +    from dvdnumcoeff_trans[OF th' th] have ?case by (simp add: zgcd_zdvd1)}
   4.787 +  moreover {assume "abs c = 0 \<and> ?g > 1"
   4.788 +    with prems have th: "dvdnumcoeff t ?g" by simp
   4.789 +    have th': "zgcd c ?g dvd ?g" by (simp add:zgcd_zdvd2)
   4.790 +    from dvdnumcoeff_trans[OF th' th] have ?case by (simp add: zgcd_zdvd1)
   4.791 +    hence ?case by simp }
   4.792 +  moreover {assume "abs c > 1" and g0:"?g = 0" 
   4.793 +    from numgcdh0[OF g0] have "m=0". with prems   have ?case by simp }
   4.794 +  ultimately show ?case by blast
   4.795 +qed(auto simp add: zgcd_zdvd1)
   4.796 +
   4.797 +lemma dvdnumcoeff_aux2:
   4.798 +  assumes "numgcd t > 1" shows "dvdnumcoeff t (numgcd t) \<and> numgcd t > 0"
   4.799 +  using prems 
   4.800 +proof (simp add: numgcd_def)
   4.801 +  let ?mc = "maxcoeff t"
   4.802 +  let ?g = "numgcdh t ?mc"
   4.803 +  have th1: "ismaxcoeff t ?mc" by (rule maxcoeff_ismaxcoeff)
   4.804 +  have th2: "?mc \<ge> 0" by (rule maxcoeff_pos)
   4.805 +  assume H: "numgcdh t ?mc > 1"
   4.806 +  from dvdnumcoeff_aux[OF th1 th2 H]  show "dvdnumcoeff t ?g" .
   4.807 +qed
   4.808 +
   4.809 +lemma reducecoeff: "real (numgcd t) * (Inum bs (reducecoeff t)) = Inum bs t"
   4.810 +proof-
   4.811 +  let ?g = "numgcd t"
   4.812 +  have "?g \<ge> 0"  by (simp add: numgcd_pos)
   4.813 +  hence	"?g = 0 \<or> ?g = 1 \<or> ?g > 1" by auto
   4.814 +  moreover {assume "?g = 0" hence ?thesis by (simp add: numgcd0)} 
   4.815 +  moreover {assume "?g = 1" hence ?thesis by (simp add: reducecoeff_def)} 
   4.816 +  moreover { assume g1:"?g > 1"
   4.817 +    from dvdnumcoeff_aux2[OF g1] have th1:"dvdnumcoeff t ?g" and g0: "?g > 0" by blast+
   4.818 +    from reducecoeffh[OF th1 g0, where bs="bs"] g1 have ?thesis 
   4.819 +      by (simp add: reducecoeff_def Let_def)} 
   4.820 +  ultimately show ?thesis by blast
   4.821 +qed
   4.822 +
   4.823 +lemma reducecoeffh_numbound0: "numbound0 t \<Longrightarrow> numbound0 (reducecoeffh t g)"
   4.824 +by (induct t rule: reducecoeffh.induct, auto)
   4.825 +
   4.826 +lemma reducecoeff_numbound0: "numbound0 t \<Longrightarrow> numbound0 (reducecoeff t)"
   4.827 +using reducecoeffh_numbound0 by (simp add: reducecoeff_def Let_def)
   4.828 +
   4.829 +consts
   4.830 +  simpnum:: "num \<Rightarrow> num"
   4.831 +  numadd:: "num \<times> num \<Rightarrow> num"
   4.832 +  nummul:: "num \<Rightarrow> int \<Rightarrow> num"
   4.833 +
   4.834 +recdef numadd "measure (\<lambda> (t,s). size t + size s)"
   4.835 +  "numadd (CN n1 c1 r1,CN n2 c2 r2) =
   4.836 +  (if n1=n2 then 
   4.837 +  (let c = c1 + c2
   4.838 +  in (if c=0 then numadd(r1,r2) else CN n1 c (numadd (r1,r2))))
   4.839 +  else if n1 \<le> n2 then CN n1 c1 (numadd (r1,CN n2 c2 r2))
   4.840 +  else (CN n2 c2 (numadd (CN n1 c1 r1,r2))))"
   4.841 +  "numadd (CN n1 c1 r1,t) = CN n1 c1 (numadd (r1, t))"  
   4.842 +  "numadd (t,CN n2 c2 r2) = CN n2 c2 (numadd (t,r2))" 
   4.843 +  "numadd (CF c1 t1 r1,CF c2 t2 r2) = 
   4.844 +   (if t1 = t2 then 
   4.845 +    (let c=c1+c2; s= numadd(r1,r2) in (if c=0 then s else CF c t1 s))
   4.846 +   else if lex_bnd t1 t2 then CF c1 t1 (numadd(r1,CF c2 t2 r2))
   4.847 +   else CF c2 t2 (numadd(CF c1 t1 r1,r2)))"
   4.848 +  "numadd (CF c1 t1 r1,C c) = CF c1 t1 (numadd (r1, C c))"
   4.849 +  "numadd (C c,CF c1 t1 r1) = CF c1 t1 (numadd (r1, C c))"
   4.850 +  "numadd (C b1, C b2) = C (b1+b2)"
   4.851 +  "numadd (a,b) = Add a b"
   4.852 +
   4.853 +lemma numadd[simp]: "Inum bs (numadd (t,s)) = Inum bs (Add t s)"
   4.854 +apply (induct t s rule: numadd.induct, simp_all add: Let_def)
   4.855 + apply (case_tac "c1+c2 = 0",case_tac "n1 \<le> n2", simp_all)
   4.856 +  apply (case_tac "n1 = n2", simp_all add: algebra_simps)
   4.857 +  apply (simp only: left_distrib[symmetric])
   4.858 + apply simp
   4.859 +apply (case_tac "lex_bnd t1 t2", simp_all)
   4.860 + apply (case_tac "c1+c2 = 0")
   4.861 +  by (case_tac "t1 = t2", simp_all add: algebra_simps left_distrib[symmetric] real_of_int_mult[symmetric] real_of_int_add[symmetric]del: real_of_int_mult real_of_int_add left_distrib)
   4.862 +
   4.863 +lemma numadd_nb[simp]: "\<lbrakk> numbound0 t ; numbound0 s\<rbrakk> \<Longrightarrow> numbound0 (numadd (t,s))"
   4.864 +by (induct t s rule: numadd.induct, auto simp add: Let_def)
   4.865 +
   4.866 +recdef nummul "measure size"
   4.867 +  "nummul (C j) = (\<lambda> i. C (i*j))"
   4.868 +  "nummul (CN n c t) = (\<lambda> i. CN n (c*i) (nummul t i))"
   4.869 +  "nummul (CF c t s) = (\<lambda> i. CF (c*i) t (nummul s i))"
   4.870 +  "nummul (Mul c t) = (\<lambda> i. nummul t (i*c))"
   4.871 +  "nummul t = (\<lambda> i. Mul i t)"
   4.872 +
   4.873 +lemma nummul[simp]: "\<And> i. Inum bs (nummul t i) = Inum bs (Mul i t)"
   4.874 +by (induct t rule: nummul.induct, auto simp add: algebra_simps)
   4.875 +
   4.876 +lemma nummul_nb[simp]: "\<And> i. numbound0 t \<Longrightarrow> numbound0 (nummul t i)"
   4.877 +by (induct t rule: nummul.induct, auto)
   4.878 +
   4.879 +constdefs numneg :: "num \<Rightarrow> num"
   4.880 +  "numneg t \<equiv> nummul t (- 1)"
   4.881 +
   4.882 +constdefs numsub :: "num \<Rightarrow> num \<Rightarrow> num"
   4.883 +  "numsub s t \<equiv> (if s = t then C 0 else numadd (s,numneg t))"
   4.884 +
   4.885 +lemma numneg[simp]: "Inum bs (numneg t) = Inum bs (Neg t)"
   4.886 +using numneg_def nummul by simp
   4.887 +
   4.888 +lemma numneg_nb[simp]: "numbound0 t \<Longrightarrow> numbound0 (numneg t)"
   4.889 +using numneg_def by simp
   4.890 +
   4.891 +lemma numsub[simp]: "Inum bs (numsub a b) = Inum bs (Sub a b)"
   4.892 +using numsub_def by simp
   4.893 +
   4.894 +lemma numsub_nb[simp]: "\<lbrakk> numbound0 t ; numbound0 s\<rbrakk> \<Longrightarrow> numbound0 (numsub t s)"
   4.895 +using numsub_def by simp
   4.896 +
   4.897 +lemma isint_CF: assumes si: "isint s bs" shows "isint (CF c t s) bs"
   4.898 +proof-
   4.899 +  have cti: "isint (Mul c (Floor t)) bs" by (simp add: isint_Mul isint_Floor)
   4.900 +  
   4.901 +  have "?thesis = isint (Add (Mul c (Floor t)) s) bs" by (simp add: isint_def)
   4.902 +  also have "\<dots>" by (simp add: isint_add cti si)
   4.903 +  finally show ?thesis .
   4.904 +qed
   4.905 +
   4.906 +consts split_int:: "num \<Rightarrow> num\<times>num"
   4.907 +recdef split_int "measure num_size"
   4.908 +  "split_int (C c) = (C 0, C c)"
   4.909 +  "split_int (CN n c b) = 
   4.910 +     (let (bv,bi) = split_int b 
   4.911 +       in (CN n c bv, bi))"
   4.912 +  "split_int (CF c a b) = 
   4.913 +     (let (bv,bi) = split_int b 
   4.914 +       in (bv, CF c a bi))"
   4.915 +  "split_int a = (a,C 0)"
   4.916 +
   4.917 +lemma split_int:"\<And> tv ti. split_int t = (tv,ti) \<Longrightarrow> (Inum bs (Add tv ti) = Inum bs t) \<and> isint ti bs"
   4.918 +proof (induct t rule: split_int.induct)
   4.919 +  case (2 c n b tv ti)
   4.920 +  let ?bv = "fst (split_int b)"
   4.921 +  let ?bi = "snd (split_int b)"
   4.922 +  have "split_int b = (?bv,?bi)" by simp
   4.923 +  with prems(1) have b:"Inum bs (Add ?bv ?bi) = Inum bs b" and bii: "isint ?bi bs" by blast+
   4.924 +  from prems(2) have tibi: "ti = ?bi" by (simp add: Let_def split_def)
   4.925 +  from prems(2) b[symmetric] bii show ?case by (auto simp add: Let_def split_def)
   4.926 +next
   4.927 +  case (3 c a b tv ti) 
   4.928 +  let ?bv = "fst (split_int b)"
   4.929 +  let ?bi = "snd (split_int b)"
   4.930 +  have "split_int b = (?bv,?bi)" by simp
   4.931 +  with prems(1) have b:"Inum bs (Add ?bv ?bi) = Inum bs b" and bii: "isint ?bi bs" by blast+
   4.932 +  from prems(2) have tibi: "ti = CF c a ?bi" by (simp add: Let_def split_def)
   4.933 +  from prems(2) b[symmetric] bii show ?case by (auto simp add: Let_def split_def isint_Floor isint_add isint_Mul isint_CF)
   4.934 +qed (auto simp add: Let_def isint_iff isint_Floor isint_add isint_Mul split_def algebra_simps)
   4.935 +
   4.936 +lemma split_int_nb: "numbound0 t \<Longrightarrow> numbound0 (fst (split_int t)) \<and> numbound0 (snd (split_int t)) "
   4.937 +by (induct t rule: split_int.induct, auto simp add: Let_def split_def)
   4.938 +
   4.939 +definition
   4.940 +  numfloor:: "num \<Rightarrow> num"
   4.941 +where
   4.942 +  numfloor_def: "numfloor t = (let (tv,ti) = split_int t in 
   4.943 +  (case tv of C i \<Rightarrow> numadd (tv,ti) 
   4.944 +  | _ \<Rightarrow> numadd(CF 1 tv (C 0),ti)))"
   4.945 +
   4.946 +lemma numfloor[simp]: "Inum bs (numfloor t) = Inum bs (Floor t)" (is "?n t = ?N (Floor t)")
   4.947 +proof-
   4.948 +  let ?tv = "fst (split_int t)"
   4.949 +  let ?ti = "snd (split_int t)"
   4.950 +  have tvti:"split_int t = (?tv,?ti)" by simp
   4.951 +  {assume H: "\<forall> v. ?tv \<noteq> C v"
   4.952 +    hence th1: "?n t = ?N (Add (Floor ?tv) ?ti)" 
   4.953 +      by (cases ?tv, auto simp add: numfloor_def Let_def split_def numadd)
   4.954 +    from split_int[OF tvti] have "?N (Floor t) = ?N (Floor(Add ?tv ?ti))" and tii:"isint ?ti bs" by simp+
   4.955 +    hence "?N (Floor t) = real (floor (?N (Add ?tv ?ti)))" by simp 
   4.956 +    also have "\<dots> = real (floor (?N ?tv) + (floor (?N ?ti)))"
   4.957 +      by (simp,subst tii[simplified isint_iff, symmetric]) simp
   4.958 +    also have "\<dots> = ?N (Add (Floor ?tv) ?ti)" by (simp add: tii[simplified isint_iff])
   4.959 +    finally have ?thesis using th1 by simp}
   4.960 +  moreover {fix v assume H:"?tv = C v" 
   4.961 +    from split_int[OF tvti] have "?N (Floor t) = ?N (Floor(Add ?tv ?ti))" and tii:"isint ?ti bs" by simp+
   4.962 +    hence "?N (Floor t) = real (floor (?N (Add ?tv ?ti)))" by simp 
   4.963 +    also have "\<dots> = real (floor (?N ?tv) + (floor (?N ?ti)))"
   4.964 +      by (simp,subst tii[simplified isint_iff, symmetric]) simp
   4.965 +    also have "\<dots> = ?N (Add (Floor ?tv) ?ti)" by (simp add: tii[simplified isint_iff])
   4.966 +    finally have ?thesis by (simp add: H numfloor_def Let_def split_def numadd) }
   4.967 +  ultimately show ?thesis by auto
   4.968 +qed
   4.969 +
   4.970 +lemma numfloor_nb[simp]: "numbound0 t \<Longrightarrow> numbound0 (numfloor t)"
   4.971 +  using split_int_nb[where t="t"]
   4.972 +  by (cases "fst(split_int t)" , auto simp add: numfloor_def Let_def split_def  numadd_nb)
   4.973 +
   4.974 +recdef simpnum "measure num_size"
   4.975 +  "simpnum (C j) = C j"
   4.976 +  "simpnum (Bound n) = CN n 1 (C 0)"
   4.977 +  "simpnum (Neg t) = numneg (simpnum t)"
   4.978 +  "simpnum (Add t s) = numadd (simpnum t,simpnum s)"
   4.979 +  "simpnum (Sub t s) = numsub (simpnum t) (simpnum s)"
   4.980 +  "simpnum (Mul i t) = (if i = 0 then (C 0) else nummul (simpnum t) i)"
   4.981 +  "simpnum (Floor t) = numfloor (simpnum t)"
   4.982 +  "simpnum (CN n c t) = (if c=0 then simpnum t else CN n c (simpnum t))"
   4.983 +  "simpnum (CF c t s) = simpnum(Add (Mul c (Floor t)) s)"
   4.984 +
   4.985 +lemma simpnum_ci[simp]: "Inum bs (simpnum t) = Inum bs t"
   4.986 +by (induct t rule: simpnum.induct, auto)
   4.987 +
   4.988 +lemma simpnum_numbound0[simp]: 
   4.989 +  "numbound0 t \<Longrightarrow> numbound0 (simpnum t)"
   4.990 +by (induct t rule: simpnum.induct, auto)
   4.991 +
   4.992 +consts nozerocoeff:: "num \<Rightarrow> bool"
   4.993 +recdef nozerocoeff "measure size"
   4.994 +  "nozerocoeff (C c) = True"
   4.995 +  "nozerocoeff (CN n c t) = (c\<noteq>0 \<and> nozerocoeff t)"
   4.996 +  "nozerocoeff (CF c s t) = (c \<noteq> 0 \<and> nozerocoeff t)"
   4.997 +  "nozerocoeff (Mul c t) = (c\<noteq>0 \<and> nozerocoeff t)"
   4.998 +  "nozerocoeff t = True"
   4.999 +
  4.1000 +lemma numadd_nz : "nozerocoeff a \<Longrightarrow> nozerocoeff b \<Longrightarrow> nozerocoeff (numadd (a,b))"
  4.1001 +by (induct a b rule: numadd.induct,auto simp add: Let_def)
  4.1002 +
  4.1003 +lemma nummul_nz : "\<And> i. i\<noteq>0 \<Longrightarrow> nozerocoeff a \<Longrightarrow> nozerocoeff (nummul a i)"
  4.1004 +  by (induct a rule: nummul.induct,auto simp add: Let_def numadd_nz)
  4.1005 +
  4.1006 +lemma numneg_nz : "nozerocoeff a \<Longrightarrow> nozerocoeff (numneg a)"
  4.1007 +by (simp add: numneg_def nummul_nz)
  4.1008 +
  4.1009 +lemma numsub_nz: "nozerocoeff a \<Longrightarrow> nozerocoeff b \<Longrightarrow> nozerocoeff (numsub a b)"
  4.1010 +by (simp add: numsub_def numneg_nz numadd_nz)
  4.1011 +
  4.1012 +lemma split_int_nz: "nozerocoeff t \<Longrightarrow> nozerocoeff (fst (split_int t)) \<and> nozerocoeff (snd (split_int t))"
  4.1013 +by (induct t rule: split_int.induct,auto simp add: Let_def split_def)
  4.1014 +
  4.1015 +lemma numfloor_nz: "nozerocoeff t \<Longrightarrow> nozerocoeff (numfloor t)"
  4.1016 +by (simp add: numfloor_def Let_def split_def)
  4.1017 +(cases "fst (split_int t)", simp_all add: split_int_nz numadd_nz)
  4.1018 +
  4.1019 +lemma simpnum_nz: "nozerocoeff (simpnum t)"
  4.1020 +by(induct t rule: simpnum.induct, auto simp add: numadd_nz numneg_nz numsub_nz nummul_nz numfloor_nz)
  4.1021 +
  4.1022 +lemma maxcoeff_nz: "nozerocoeff t \<Longrightarrow> maxcoeff t = 0 \<Longrightarrow> t = C 0"
  4.1023 +proof (induct t rule: maxcoeff.induct)
  4.1024 +  case (2 n c t)
  4.1025 +  hence cnz: "c \<noteq>0" and mx: "max (abs c) (maxcoeff t) = 0" by simp+
  4.1026 +  have "max (abs c) (maxcoeff t) \<ge> abs c" by (simp add: le_maxI1)
  4.1027 +  with cnz have "max (abs c) (maxcoeff t) > 0" by arith
  4.1028 +  with prems show ?case by simp
  4.1029 +next
  4.1030 +  case (3 c s t) 
  4.1031 +  hence cnz: "c \<noteq>0" and mx: "max (abs c) (maxcoeff t) = 0" by simp+
  4.1032 +  have "max (abs c) (maxcoeff t) \<ge> abs c" by (simp add: le_maxI1)
  4.1033 +  with cnz have "max (abs c) (maxcoeff t) > 0" by arith
  4.1034 +  with prems show ?case by simp
  4.1035 +qed auto
  4.1036 +
  4.1037 +lemma numgcd_nz: assumes nz: "nozerocoeff t" and g0: "numgcd t = 0" shows "t = C 0"
  4.1038 +proof-
  4.1039 +  from g0 have th:"numgcdh t (maxcoeff t) = 0" by (simp add: numgcd_def)
  4.1040 +  from numgcdh0[OF th]  have th:"maxcoeff t = 0" .
  4.1041 +  from maxcoeff_nz[OF nz th] show ?thesis .
  4.1042 +qed
  4.1043 +
  4.1044 +constdefs simp_num_pair:: "(num \<times> int) \<Rightarrow> num \<times> int"
  4.1045 +  "simp_num_pair \<equiv> (\<lambda> (t,n). (if n = 0 then (C 0, 0) else
  4.1046 +   (let t' = simpnum t ; g = numgcd t' in 
  4.1047 +      if g > 1 then (let g' = zgcd n g in 
  4.1048 +        if g' = 1 then (t',n) 
  4.1049 +        else (reducecoeffh t' g', n div g')) 
  4.1050 +      else (t',n))))"
  4.1051 +
  4.1052 +lemma simp_num_pair_ci:
  4.1053 +  shows "((\<lambda> (t,n). Inum bs t / real n) (simp_num_pair (t,n))) = ((\<lambda> (t,n). Inum bs t / real n) (t,n))"
  4.1054 +  (is "?lhs = ?rhs")
  4.1055 +proof-
  4.1056 +  let ?t' = "simpnum t"
  4.1057 +  let ?g = "numgcd ?t'"
  4.1058 +  let ?g' = "zgcd n ?g"
  4.1059 +  {assume nz: "n = 0" hence ?thesis by (simp add: Let_def simp_num_pair_def)}
  4.1060 +  moreover
  4.1061 +  { assume nnz: "n \<noteq> 0"
  4.1062 +    {assume "\<not> ?g > 1" hence ?thesis by (simp add: Let_def simp_num_pair_def)}
  4.1063 +    moreover
  4.1064 +    {assume g1:"?g>1" hence g0: "?g > 0" by simp
  4.1065 +      from zgcd0 g1 nnz have gp0: "?g' \<noteq> 0" by simp
  4.1066 +      hence g'p: "?g' > 0" using zgcd_pos[where i="n" and j="numgcd ?t'"] by arith
  4.1067 +      hence "?g'= 1 \<or> ?g' > 1" by arith
  4.1068 +      moreover {assume "?g'=1" hence ?thesis by (simp add: Let_def simp_num_pair_def)}
  4.1069 +      moreover {assume g'1:"?g'>1"
  4.1070 +	from dvdnumcoeff_aux2[OF g1] have th1:"dvdnumcoeff ?t' ?g" ..
  4.1071 +	let ?tt = "reducecoeffh ?t' ?g'"
  4.1072 +	let ?t = "Inum bs ?tt"
  4.1073 +	have gpdg: "?g' dvd ?g" by (simp add: zgcd_zdvd2)
  4.1074 +	have gpdd: "?g' dvd n" by (simp add: zgcd_zdvd1) 
  4.1075 +	have gpdgp: "?g' dvd ?g'" by simp
  4.1076 +	from reducecoeffh[OF dvdnumcoeff_trans[OF gpdg th1] g'p] 
  4.1077 +	have th2:"real ?g' * ?t = Inum bs ?t'" by simp
  4.1078 +	from prems have "?lhs = ?t / real (n div ?g')" by (simp add: simp_num_pair_def Let_def)
  4.1079 +	also have "\<dots> = (real ?g' * ?t) / (real ?g' * (real (n div ?g')))" by simp
  4.1080 +	also have "\<dots> = (Inum bs ?t' / real n)"
  4.1081 +	  using real_of_int_div[OF gp0 gpdd] th2 gp0 by simp
  4.1082 +	finally have "?lhs = Inum bs t / real n" by simp
  4.1083 +	then have ?thesis using prems by (simp add: simp_num_pair_def)}
  4.1084 +      ultimately have ?thesis by blast}
  4.1085 +    ultimately have ?thesis by blast} 
  4.1086 +  ultimately show ?thesis by blast
  4.1087 +qed
  4.1088 +
  4.1089 +lemma simp_num_pair_l: assumes tnb: "numbound0 t" and np: "n >0" and tn: "simp_num_pair (t,n) = (t',n')"
  4.1090 +  shows "numbound0 t' \<and> n' >0"
  4.1091 +proof-
  4.1092 +    let ?t' = "simpnum t"
  4.1093 +  let ?g = "numgcd ?t'"
  4.1094 +  let ?g' = "zgcd n ?g"
  4.1095 +  {assume nz: "n = 0" hence ?thesis using prems by (simp add: Let_def simp_num_pair_def)}
  4.1096 +  moreover
  4.1097 +  { assume nnz: "n \<noteq> 0"
  4.1098 +    {assume "\<not> ?g > 1" hence ?thesis  using prems by (auto simp add: Let_def simp_num_pair_def)}
  4.1099 +    moreover
  4.1100 +    {assume g1:"?g>1" hence g0: "?g > 0" by simp
  4.1101 +      from zgcd0 g1 nnz have gp0: "?g' \<noteq> 0" by simp
  4.1102 +      hence g'p: "?g' > 0" using zgcd_pos[where i="n" and j="numgcd ?t'"] by arith
  4.1103 +      hence "?g'= 1 \<or> ?g' > 1" by arith
  4.1104 +      moreover {assume "?g'=1" hence ?thesis using prems 
  4.1105 +	  by (auto simp add: Let_def simp_num_pair_def)}
  4.1106 +      moreover {assume g'1:"?g'>1"
  4.1107 +	have gpdg: "?g' dvd ?g" by (simp add: zgcd_zdvd2)
  4.1108 +	have gpdd: "?g' dvd n" by (simp add: zgcd_zdvd1) 
  4.1109 +	have gpdgp: "?g' dvd ?g'" by simp
  4.1110 +	from zdvd_imp_le[OF gpdd np] have g'n: "?g' \<le> n" .
  4.1111 +	from zdiv_mono1[OF g'n g'p, simplified zdiv_self[OF gp0]]
  4.1112 +	have "n div ?g' >0" by simp
  4.1113 +	hence ?thesis using prems 
  4.1114 +	  by(auto simp add: simp_num_pair_def Let_def reducecoeffh_numbound0)}
  4.1115 +      ultimately have ?thesis by blast}
  4.1116 +    ultimately have ?thesis by blast} 
  4.1117 +  ultimately show ?thesis by blast
  4.1118 +qed
  4.1119 +
  4.1120 +consts not:: "fm \<Rightarrow> fm"
  4.1121 +recdef not "measure size"
  4.1122 +  "not (NOT p) = p"
  4.1123 +  "not T = F"
  4.1124 +  "not F = T"
  4.1125 +  "not (Lt t) = Ge t"
  4.1126 +  "not (Le t) = Gt t"
  4.1127 +  "not (Gt t) = Le t"
  4.1128 +  "not (Ge t) = Lt t"
  4.1129 +  "not (Eq t) = NEq t"
  4.1130 +  "not (NEq t) = Eq t"
  4.1131 +  "not (Dvd i t) = NDvd i t"
  4.1132 +  "not (NDvd i t) = Dvd i t"
  4.1133 +  "not (And p q) = Or (not p) (not q)"
  4.1134 +  "not (Or p q) = And (not p) (not q)"
  4.1135 +  "not p = NOT p"
  4.1136 +lemma not[simp]: "Ifm bs (not p) = Ifm bs (NOT p)"
  4.1137 +by (induct p) auto
  4.1138 +lemma not_qf[simp]: "qfree p \<Longrightarrow> qfree (not p)"
  4.1139 +by (induct p, auto)
  4.1140 +lemma not_nb[simp]: "bound0 p \<Longrightarrow> bound0 (not p)"
  4.1141 +by (induct p, auto)
  4.1142 +
  4.1143 +constdefs conj :: "fm \<Rightarrow> fm \<Rightarrow> fm"
  4.1144 +  "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 
  4.1145 +   if p = q then p else And p q)"
  4.1146 +lemma conj[simp]: "Ifm bs (conj p q) = Ifm bs (And p q)"
  4.1147 +by (cases "p=F \<or> q=F",simp_all add: conj_def) (cases p,simp_all)
  4.1148 +
  4.1149 +lemma conj_qf[simp]: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (conj p q)"
  4.1150 +using conj_def by auto 
  4.1151 +lemma conj_nb[simp]: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (conj p q)"
  4.1152 +using conj_def by auto 
  4.1153 +
  4.1154 +constdefs disj :: "fm \<Rightarrow> fm \<Rightarrow> fm"
  4.1155 +  "disj p q \<equiv> (if (p = T \<or> q=T) then T else if p=F then q else if q=F then p 
  4.1156 +       else if p=q then p else Or p q)"
  4.1157 +
  4.1158 +lemma disj[simp]: "Ifm bs (disj p q) = Ifm bs (Or p q)"
  4.1159 +by (cases "p=T \<or> q=T",simp_all add: disj_def) (cases p,simp_all)
  4.1160 +lemma disj_qf[simp]: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (disj p q)"
  4.1161 +using disj_def by auto 
  4.1162 +lemma disj_nb[simp]: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (disj p q)"
  4.1163 +using disj_def by auto 
  4.1164 +
  4.1165 +constdefs   imp :: "fm \<Rightarrow> fm \<Rightarrow> fm"
  4.1166 +  "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 
  4.1167 +    else Imp p q)"
  4.1168 +lemma imp[simp]: "Ifm bs (imp p q) = Ifm bs (Imp p q)"
  4.1169 +by (cases "p=F \<or> q=T",simp_all add: imp_def)
  4.1170 +lemma imp_qf[simp]: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (imp p q)"
  4.1171 +using imp_def by (cases "p=F \<or> q=T",simp_all add: imp_def)
  4.1172 +lemma imp_nb[simp]: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (imp p q)"
  4.1173 +using imp_def by (cases "p=F \<or> q=T \<or> p=q",simp_all add: imp_def) 
  4.1174 +
  4.1175 +constdefs   iff :: "fm \<Rightarrow> fm \<Rightarrow> fm"
  4.1176 +  "iff p q \<equiv> (if (p = q) then T else if (p = not q \<or> not p = q) then F else 
  4.1177 +       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 
  4.1178 +  Iff p q)"
  4.1179 +lemma iff[simp]: "Ifm bs (iff p q) = Ifm bs (Iff p q)"
  4.1180 +  by (unfold iff_def,cases "p=q", simp,cases "p=not q", simp add:not) 
  4.1181 +(cases "not p= q", auto simp add:not)
  4.1182 +lemma iff_qf[simp]: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (iff p q)"
  4.1183 +  by (unfold iff_def,cases "p=q", auto)
  4.1184 +lemma iff_nb[simp]: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (iff p q)"
  4.1185 +using iff_def by (unfold iff_def,cases "p=q", auto)
  4.1186 +
  4.1187 +consts check_int:: "num \<Rightarrow> bool"
  4.1188 +recdef check_int "measure size"
  4.1189 +  "check_int (C i) = True"
  4.1190 +  "check_int (Floor t) = True"
  4.1191 +  "check_int (Mul i t) = check_int t"
  4.1192 +  "check_int (Add t s) = (check_int t \<and> check_int s)"
  4.1193 +  "check_int (Neg t) = check_int t"
  4.1194 +  "check_int (CF c t s) = check_int s"
  4.1195 +  "check_int t = False"
  4.1196 +lemma check_int: "check_int t \<Longrightarrow> isint t bs"
  4.1197 +by (induct t, auto simp add: isint_add isint_Floor isint_Mul isint_neg isint_c isint_CF)
  4.1198 +
  4.1199 +lemma rdvd_left1_int: "real \<lfloor>t\<rfloor> = t \<Longrightarrow> 1 rdvd t"
  4.1200 +  by (simp add: rdvd_def,rule_tac x="\<lfloor>t\<rfloor>" in exI) simp
  4.1201 +
  4.1202 +lemma rdvd_reduce: 
  4.1203 +  assumes gd:"g dvd d" and gc:"g dvd c" and gp: "g > 0"
  4.1204 +  shows "real (d::int) rdvd real (c::int)*t = (real (d div g) rdvd real (c div g)*t)"
  4.1205 +proof
  4.1206 +  assume d: "real d rdvd real c * t"
  4.1207 +  from d rdvd_def obtain k where k_def: "real c * t = real d* real (k::int)" by auto
  4.1208 +  from gd dvd_def obtain kd where kd_def: "d = g * kd" by auto
  4.1209 +  from gc dvd_def obtain kc where kc_def: "c = g * kc" by auto
  4.1210 +  from k_def kd_def kc_def have "real g * real kc * t = real g * real kd * real k" by simp
  4.1211 +  hence "real kc * t = real kd * real k" using gp by simp
  4.1212 +  hence th:"real kd rdvd real kc * t" using rdvd_def by blast
  4.1213 +  from kd_def gp have th':"kd = d div g" by simp
  4.1214 +  from kc_def gp have "kc = c div g" by simp
  4.1215 +  with th th' show "real (d div g) rdvd real (c div g) * t" by simp
  4.1216 +next
  4.1217 +  assume d: "real (d div g) rdvd real (c div g) * t"
  4.1218 +  from gp have gnz: "g \<noteq> 0" by simp
  4.1219 +  thus "real d rdvd real c * t" using d rdvd_mult[OF gnz, where n="d div g" and x="real (c div g) * t"] real_of_int_div[OF gnz gd] real_of_int_div[OF gnz gc] by simp
  4.1220 +qed
  4.1221 +
  4.1222 +constdefs simpdvd:: "int \<Rightarrow> num \<Rightarrow> (int \<times> num)"
  4.1223 +  "simpdvd d t \<equiv> 
  4.1224 +   (let g = numgcd t in 
  4.1225 +      if g > 1 then (let g' = zgcd d g in 
  4.1226 +        if g' = 1 then (d, t) 
  4.1227 +        else (d div g',reducecoeffh t g')) 
  4.1228 +      else (d, t))"
  4.1229 +lemma simpdvd: 
  4.1230 +  assumes tnz: "nozerocoeff t" and dnz: "d \<noteq> 0"
  4.1231 +  shows "Ifm bs (Dvd (fst (simpdvd d t)) (snd (simpdvd d t))) = Ifm bs (Dvd d t)"
  4.1232 +proof-
  4.1233 +  let ?g = "numgcd t"
  4.1234 +  let ?g' = "zgcd d ?g"
  4.1235 +  {assume "\<not> ?g > 1" hence ?thesis by (simp add: Let_def simpdvd_def)}
  4.1236 +  moreover
  4.1237 +  {assume g1:"?g>1" hence g0: "?g > 0" by simp
  4.1238 +    from zgcd0 g1 dnz have gp0: "?g' \<noteq> 0" by simp
  4.1239 +    hence g'p: "?g' > 0" using zgcd_pos[where i="d" and j="numgcd t"] by arith
  4.1240 +    hence "?g'= 1 \<or> ?g' > 1" by arith
  4.1241 +    moreover {assume "?g'=1" hence ?thesis by (simp add: Let_def simpdvd_def)}
  4.1242 +    moreover {assume g'1:"?g'>1"
  4.1243 +      from dvdnumcoeff_aux2[OF g1] have th1:"dvdnumcoeff t ?g" ..
  4.1244 +      let ?tt = "reducecoeffh t ?g'"
  4.1245 +      let ?t = "Inum bs ?tt"
  4.1246 +      have gpdg: "?g' dvd ?g" by (simp add: zgcd_zdvd2)
  4.1247 +      have gpdd: "?g' dvd d" by (simp add: zgcd_zdvd1) 
  4.1248 +      have gpdgp: "?g' dvd ?g'" by simp
  4.1249 +      from reducecoeffh[OF dvdnumcoeff_trans[OF gpdg th1] g'p] 
  4.1250 +      have th2:"real ?g' * ?t = Inum bs t" by simp
  4.1251 +      from prems have "Ifm bs (Dvd (fst (simpdvd d t)) (snd(simpdvd d t))) = Ifm bs (Dvd (d div ?g') ?tt)"
  4.1252 +	by (simp add: simpdvd_def Let_def)
  4.1253 +      also have "\<dots> = (real d rdvd (Inum bs t))"
  4.1254 +	using rdvd_reduce[OF gpdd gpdgp g'p, where t="?t", simplified zdiv_self[OF gp0]] 
  4.1255 +	  th2[symmetric] by simp
  4.1256 +      finally have ?thesis by simp  }
  4.1257 +    ultimately have ?thesis by blast
  4.1258 +  }
  4.1259 +  ultimately show ?thesis by blast
  4.1260 +qed
  4.1261 +
  4.1262 +consts simpfm :: "fm \<Rightarrow> fm"
  4.1263 +recdef simpfm "measure fmsize"
  4.1264 +  "simpfm (And p q) = conj (simpfm p) (simpfm q)"
  4.1265 +  "simpfm (Or p q) = disj (simpfm p) (simpfm q)"
  4.1266 +  "simpfm (Imp p q) = imp (simpfm p) (simpfm q)"
  4.1267 +  "simpfm (Iff p q) = iff (simpfm p) (simpfm q)"
  4.1268 +  "simpfm (NOT p) = not (simpfm p)"
  4.1269 +  "simpfm (Lt a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v < 0) then T else F 
  4.1270 +  | _ \<Rightarrow> Lt (reducecoeff a'))"
  4.1271 +  "simpfm (Le a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v \<le> 0)  then T else F | _ \<Rightarrow> Le (reducecoeff a'))"
  4.1272 +  "simpfm (Gt a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v > 0)  then T else F | _ \<Rightarrow> Gt (reducecoeff a'))"
  4.1273 +  "simpfm (Ge a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v \<ge> 0)  then T else F | _ \<Rightarrow> Ge (reducecoeff a'))"
  4.1274 +  "simpfm (Eq a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v = 0)  then T else F | _ \<Rightarrow> Eq (reducecoeff a'))"
  4.1275 +  "simpfm (NEq a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v \<noteq> 0)  then T else F | _ \<Rightarrow> NEq (reducecoeff a'))"
  4.1276 +  "simpfm (Dvd i a) = (if i=0 then simpfm (Eq a)
  4.1277 +             else if (abs i = 1) \<and> check_int a then T
  4.1278 +             else let a' = simpnum a in case a' of C v \<Rightarrow> if (i dvd v)  then T else F | _ \<Rightarrow> (let (d,t) = simpdvd i a' in Dvd d t))"
  4.1279 +  "simpfm (NDvd i a) = (if i=0 then simpfm (NEq a) 
  4.1280 +             else if (abs i = 1) \<and> check_int a then F
  4.1281 +             else let a' = simpnum a in case a' of C v \<Rightarrow> if (\<not>(i dvd v)) then T else F | _ \<Rightarrow> (let (d,t) = simpdvd i a' in NDvd d t))"
  4.1282 +  "simpfm p = p"
  4.1283 +
  4.1284 +lemma simpfm[simp]: "Ifm bs (simpfm p) = Ifm bs p"
  4.1285 +proof(induct p rule: simpfm.induct)
  4.1286 +  case (6 a) let ?sa = "simpnum a" have sa: "Inum bs ?sa = Inum bs a" by simp
  4.1287 +  {fix v assume "?sa = C v" hence ?case using sa by simp }
  4.1288 +  moreover {assume H:"\<not> (\<exists> v. ?sa = C v)"
  4.1289 +    let ?g = "numgcd ?sa"
  4.1290 +    let ?rsa = "reducecoeff ?sa"
  4.1291 +    let ?r = "Inum bs ?rsa"
  4.1292 +    have sa_nz: "nozerocoeff ?sa" by (rule simpnum_nz)
  4.1293 +    {assume gz: "?g=0" from numgcd_nz[OF sa_nz gz] H have "False" by auto}
  4.1294 +    with numgcd_pos[where t="?sa"] have "?g > 0" by (cases "?g=0", auto)
  4.1295 +    hence gp: "real ?g > 0" by simp
  4.1296 +    have "Inum bs ?sa = real ?g* ?r" by (simp add: reducecoeff)
  4.1297 +    with sa have "Inum bs a < 0 = (real ?g * ?r < real ?g * 0)" by simp
  4.1298 +    also have "\<dots> = (?r < 0)" using gp
  4.1299 +      by (simp only: mult_less_cancel_left) simp
  4.1300 +    finally have ?case using H by (cases "?sa" , simp_all add: Let_def)}
  4.1301 +  ultimately show ?case by blast
  4.1302 +next
  4.1303 +  case (7 a)  let ?sa = "simpnum a" have sa: "Inum bs ?sa = Inum bs a" by simp
  4.1304 +  {fix v assume "?sa = C v" hence ?case using sa by simp }
  4.1305 +  moreover {assume H:"\<not> (\<exists> v. ?sa = C v)"
  4.1306 +    let ?g = "numgcd ?sa"
  4.1307 +    let ?rsa = "reducecoeff ?sa"
  4.1308 +    let ?r = "Inum bs ?rsa"
  4.1309 +    have sa_nz: "nozerocoeff ?sa" by (rule simpnum_nz)
  4.1310 +    {assume gz: "?g=0" from numgcd_nz[OF sa_nz gz] H have "False" by auto}
  4.1311 +    with numgcd_pos[where t="?sa"] have "?g > 0" by (cases "?g=0", auto)
  4.1312 +    hence gp: "real ?g > 0" by simp
  4.1313 +    have "Inum bs ?sa = real ?g* ?r" by (simp add: reducecoeff)
  4.1314 +    with sa have "Inum bs a \<le> 0 = (real ?g * ?r \<le> real ?g * 0)" by simp
  4.1315 +    also have "\<dots> = (?r \<le> 0)" using gp
  4.1316 +      by (simp only: mult_le_cancel_left) simp
  4.1317 +    finally have ?case using H by (cases "?sa" , simp_all add: Let_def)}
  4.1318 +  ultimately show ?case by blast
  4.1319 +next
  4.1320 +  case (8 a)  let ?sa = "simpnum a" have sa: "Inum bs ?sa = Inum bs a" by simp
  4.1321 +  {fix v assume "?sa = C v" hence ?case using sa by simp }
  4.1322 +  moreover {assume H:"\<not> (\<exists> v. ?sa = C v)"
  4.1323 +    let ?g = "numgcd ?sa"
  4.1324 +    let ?rsa = "reducecoeff ?sa"
  4.1325 +    let ?r = "Inum bs ?rsa"
  4.1326 +    have sa_nz: "nozerocoeff ?sa" by (rule simpnum_nz)
  4.1327 +    {assume gz: "?g=0" from numgcd_nz[OF sa_nz gz] H have "False" by auto}
  4.1328 +    with numgcd_pos[where t="?sa"] have "?g > 0" by (cases "?g=0", auto)
  4.1329 +    hence gp: "real ?g > 0" by simp
  4.1330 +    have "Inum bs ?sa = real ?g* ?r" by (simp add: reducecoeff)
  4.1331 +    with sa have "Inum bs a > 0 = (real ?g * ?r > real ?g * 0)" by simp
  4.1332 +    also have "\<dots> = (?r > 0)" using gp
  4.1333 +      by (simp only: mult_less_cancel_left) simp
  4.1334 +    finally have ?case using H by (cases "?sa" , simp_all add: Let_def)}
  4.1335 +  ultimately show ?case by blast
  4.1336 +next
  4.1337 +  case (9 a)  let ?sa = "simpnum a" have sa: "Inum bs ?sa = Inum bs a" by simp
  4.1338 +  {fix v assume "?sa = C v" hence ?case using sa by simp }
  4.1339 +  moreover {assume H:"\<not> (\<exists> v. ?sa = C v)"
  4.1340 +    let ?g = "numgcd ?sa"
  4.1341 +    let ?rsa = "reducecoeff ?sa"
  4.1342 +    let ?r = "Inum bs ?rsa"
  4.1343 +    have sa_nz: "nozerocoeff ?sa" by (rule simpnum_nz)
  4.1344 +    {assume gz: "?g=0" from numgcd_nz[OF sa_nz gz] H have "False" by auto}
  4.1345 +    with numgcd_pos[where t="?sa"] have "?g > 0" by (cases "?g=0", auto)
  4.1346 +    hence gp: "real ?g > 0" by simp
  4.1347 +    have "Inum bs ?sa = real ?g* ?r" by (simp add: reducecoeff)
  4.1348 +    with sa have "Inum bs a \<ge> 0 = (real ?g * ?r \<ge> real ?g * 0)" by simp
  4.1349 +    also have "\<dots> = (?r \<ge> 0)" using gp
  4.1350 +      by (simp only: mult_le_cancel_left) simp
  4.1351 +    finally have ?case using H by (cases "?sa" , simp_all add: Let_def)}
  4.1352 +  ultimately show ?case by blast
  4.1353 +next
  4.1354 +  case (10 a)  let ?sa = "simpnum a" have sa: "Inum bs ?sa = Inum bs a" by simp
  4.1355 +  {fix v assume "?sa = C v" hence ?case using sa by simp }
  4.1356 +  moreover {assume H:"\<not> (\<exists> v. ?sa = C v)"
  4.1357 +    let ?g = "numgcd ?sa"
  4.1358 +    let ?rsa = "reducecoeff ?sa"
  4.1359 +    let ?r = "Inum bs ?rsa"
  4.1360 +    have sa_nz: "nozerocoeff ?sa" by (rule simpnum_nz)
  4.1361 +    {assume gz: "?g=0" from numgcd_nz[OF sa_nz gz] H have "False" by auto}
  4.1362 +    with numgcd_pos[where t="?sa"] have "?g > 0" by (cases "?g=0", auto)
  4.1363 +    hence gp: "real ?g > 0" by simp
  4.1364 +    have "Inum bs ?sa = real ?g* ?r" by (simp add: reducecoeff)
  4.1365 +    with sa have "Inum bs a = 0 = (real ?g * ?r = 0)" by simp
  4.1366 +    also have "\<dots> = (?r = 0)" using gp
  4.1367 +      by (simp add: mult_eq_0_iff)
  4.1368 +    finally have ?case using H by (cases "?sa" , simp_all add: Let_def)}
  4.1369 +  ultimately show ?case by blast
  4.1370 +next
  4.1371 +  case (11 a)  let ?sa = "simpnum a" have sa: "Inum bs ?sa = Inum bs a" by simp
  4.1372 +  {fix v assume "?sa = C v" hence ?case using sa by simp }
  4.1373 +  moreover {assume H:"\<not> (\<exists> v. ?sa = C v)"
  4.1374 +    let ?g = "numgcd ?sa"
  4.1375 +    let ?rsa = "reducecoeff ?sa"
  4.1376 +    let ?r = "Inum bs ?rsa"
  4.1377 +    have sa_nz: "nozerocoeff ?sa" by (rule simpnum_nz)
  4.1378 +    {assume gz: "?g=0" from numgcd_nz[OF sa_nz gz] H have "False" by auto}
  4.1379 +    with numgcd_pos[where t="?sa"] have "?g > 0" by (cases "?g=0", auto)
  4.1380 +    hence gp: "real ?g > 0" by simp
  4.1381 +    have "Inum bs ?sa = real ?g* ?r" by (simp add: reducecoeff)
  4.1382 +    with sa have "Inum bs a \<noteq> 0 = (real ?g * ?r \<noteq> 0)" by simp
  4.1383 +    also have "\<dots> = (?r \<noteq> 0)" using gp
  4.1384 +      by (simp add: mult_eq_0_iff)
  4.1385 +    finally have ?case using H by (cases "?sa" , simp_all add: Let_def)}
  4.1386 +  ultimately show ?case by blast
  4.1387 +next
  4.1388 +  case (12 i a)  let ?sa = "simpnum a"   have sa: "Inum bs ?sa = Inum bs a" by simp
  4.1389 +  have "i=0 \<or> (abs i = 1 \<and> check_int a) \<or> (i\<noteq>0 \<and> ((abs i \<noteq> 1) \<or> (\<not> check_int a)))" by auto
  4.1390 +  {assume "i=0" hence ?case using "12.hyps" by (simp add: rdvd_left_0_eq Let_def)}
  4.1391 +  moreover 
  4.1392 +  {assume ai1: "abs i = 1" and ai: "check_int a" 
  4.1393 +    hence "i=1 \<or> i= - 1" by arith
  4.1394 +    moreover {assume i1: "i = 1" 
  4.1395 +      from rdvd_left1_int[OF check_int[OF ai, simplified isint_iff]] 
  4.1396 +      have ?case using i1 ai by simp }
  4.1397 +    moreover {assume i1: "i = - 1" 
  4.1398 +      from rdvd_left1_int[OF check_int[OF ai, simplified isint_iff]] 
  4.1399 +	rdvd_abs1[where d="- 1" and t="Inum bs a"]
  4.1400 +      have ?case using i1 ai by simp }
  4.1401 +    ultimately have ?case by blast}
  4.1402 +  moreover   
  4.1403 +  {assume inz: "i\<noteq>0" and cond: "(abs i \<noteq> 1) \<or> (\<not> check_int a)"
  4.1404 +    {fix v assume "?sa = C v" hence ?case using sa[symmetric] inz cond
  4.1405 +	by (cases "abs i = 1", auto simp add: int_rdvd_iff) }
  4.1406 +    moreover {assume H:"\<not> (\<exists> v. ?sa = C v)" 
  4.1407 +      hence th: "simpfm (Dvd i a) = Dvd (fst (simpdvd i ?sa)) (snd (simpdvd i ?sa))" using inz cond by (cases ?sa, auto simp add: Let_def split_def)
  4.1408 +      from simpnum_nz have nz:"nozerocoeff ?sa" by simp
  4.1409 +      from simpdvd [OF nz inz] th have ?case using sa by simp}
  4.1410 +    ultimately have ?case by blast}
  4.1411 +  ultimately show ?case by blast
  4.1412 +next
  4.1413 +  case (13 i a)  let ?sa = "simpnum a"   have sa: "Inum bs ?sa = Inum bs a" by simp
  4.1414 +  have "i=0 \<or> (abs i = 1 \<and> check_int a) \<or> (i\<noteq>0 \<and> ((abs i \<noteq> 1) \<or> (\<not> check_int a)))" by auto
  4.1415 +  {assume "i=0" hence ?case using "13.hyps" by (simp add: rdvd_left_0_eq Let_def)}
  4.1416 +  moreover 
  4.1417 +  {assume ai1: "abs i = 1" and ai: "check_int a" 
  4.1418 +    hence "i=1 \<or> i= - 1" by arith
  4.1419 +    moreover {assume i1: "i = 1" 
  4.1420 +      from rdvd_left1_int[OF check_int[OF ai, simplified isint_iff]] 
  4.1421 +      have ?case using i1 ai by simp }
  4.1422 +    moreover {assume i1: "i = - 1" 
  4.1423 +      from rdvd_left1_int[OF check_int[OF ai, simplified isint_iff]] 
  4.1424 +	rdvd_abs1[where d="- 1" and t="Inum bs a"]
  4.1425 +      have ?case using i1 ai by simp }
  4.1426 +    ultimately have ?case by blast}
  4.1427 +  moreover   
  4.1428 +  {assume inz: "i\<noteq>0" and cond: "(abs i \<noteq> 1) \<or> (\<not> check_int a)"
  4.1429 +    {fix v assume "?sa = C v" hence ?case using sa[symmetric] inz cond
  4.1430 +	by (cases "abs i = 1", auto simp add: int_rdvd_iff) }
  4.1431 +    moreover {assume H:"\<not> (\<exists> v. ?sa = C v)" 
  4.1432 +      hence th: "simpfm (NDvd i a) = NDvd (fst (simpdvd i ?sa)) (snd (simpdvd i ?sa))" using inz cond 
  4.1433 +	by (cases ?sa, auto simp add: Let_def split_def)
  4.1434 +      from simpnum_nz have nz:"nozerocoeff ?sa" by simp
  4.1435 +      from simpdvd [OF nz inz] th have ?case using sa by simp}
  4.1436 +    ultimately have ?case by blast}
  4.1437 +  ultimately show ?case by blast
  4.1438 +qed (induct p rule: simpfm.induct, simp_all)
  4.1439 +
  4.1440 +lemma simpdvd_numbound0: "numbound0 t \<Longrightarrow> numbound0 (snd (simpdvd d t))"
  4.1441 +  by (simp add: simpdvd_def Let_def split_def reducecoeffh_numbound0)
  4.1442 +
  4.1443 +lemma simpfm_bound0[simp]: "bound0 p \<Longrightarrow> bound0 (simpfm p)"
  4.1444 +proof(induct p rule: simpfm.induct)
  4.1445 +  case (6 a) hence nb: "numbound0 a" by simp
  4.1446 +  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
  4.1447 +  thus ?case by (cases "simpnum a", auto simp add: Let_def reducecoeff_numbound0)
  4.1448 +next
  4.1449 +  case (7 a) hence nb: "numbound0 a" by simp
  4.1450 +  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
  4.1451 +  thus ?case by (cases "simpnum a", auto simp add: Let_def reducecoeff_numbound0)
  4.1452 +next
  4.1453 +  case (8 a) hence nb: "numbound0 a" by simp
  4.1454 +  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
  4.1455 +  thus ?case by (cases "simpnum a", auto simp add: Let_def reducecoeff_numbound0)
  4.1456 +next
  4.1457 +  case (9 a) hence nb: "numbound0 a" by simp
  4.1458 +  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
  4.1459 +  thus ?case by (cases "simpnum a", auto simp add: Let_def reducecoeff_numbound0)
  4.1460 +next
  4.1461 +  case (10 a) hence nb: "numbound0 a" by simp
  4.1462 +  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
  4.1463 +  thus ?case by (cases "simpnum a", auto simp add: Let_def reducecoeff_numbound0)
  4.1464 +next
  4.1465 +  case (11 a) hence nb: "numbound0 a" by simp
  4.1466 +  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
  4.1467 +  thus ?case by (cases "simpnum a", auto simp add: Let_def reducecoeff_numbound0)
  4.1468 +next
  4.1469 +  case (12 i a) hence nb: "numbound0 a" by simp
  4.1470 +  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
  4.1471 +  thus ?case by (cases "simpnum a", auto simp add: Let_def reducecoeff_numbound0 simpdvd_numbound0 split_def)
  4.1472 +next
  4.1473 +  case (13 i a) hence nb: "numbound0 a" by simp
  4.1474 +  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
  4.1475 +  thus ?case by (cases "simpnum a", auto simp add: Let_def reducecoeff_numbound0 simpdvd_numbound0 split_def)
  4.1476 +qed(auto simp add: disj_def imp_def iff_def conj_def)
  4.1477 +
  4.1478 +lemma simpfm_qf[simp]: "qfree p \<Longrightarrow> qfree (simpfm p)"
  4.1479 +by (induct p rule: simpfm.induct, auto simp add: Let_def)
  4.1480 +(case_tac "simpnum a",auto simp add: split_def Let_def)+
  4.1481 +
  4.1482 +
  4.1483 +  (* Generic quantifier elimination *)
  4.1484 +
  4.1485 +constdefs list_conj :: "fm list \<Rightarrow> fm"
  4.1486 +  "list_conj ps \<equiv> foldr conj ps T"
  4.1487 +lemma list_conj: "Ifm bs (list_conj ps) = (\<forall>p\<in> set ps. Ifm bs p)"
  4.1488 +  by (induct ps, auto simp add: list_conj_def)
  4.1489 +lemma list_conj_qf: " \<forall>p\<in> set ps. qfree p \<Longrightarrow> qfree (list_conj ps)"
  4.1490 +  by (induct ps, auto simp add: list_conj_def)
  4.1491 +lemma list_conj_nb: " \<forall>p\<in> set ps. bound0 p \<Longrightarrow> bound0 (list_conj ps)"
  4.1492 +  by (induct ps, auto simp add: list_conj_def)
  4.1493 +constdefs CJNB:: "(fm \<Rightarrow> fm) \<Rightarrow> fm \<Rightarrow> fm"
  4.1494 +  "CJNB f p \<equiv> (let cjs = conjuncts p ; (yes,no) = List.partition bound0 cjs
  4.1495 +                   in conj (decr (list_conj yes)) (f (list_conj no)))"
  4.1496 +
  4.1497 +lemma CJNB_qe: 
  4.1498 +  assumes qe: "\<forall> bs p. qfree p \<longrightarrow> qfree (qe p) \<and> (Ifm bs (qe p) = Ifm bs (E p))"
  4.1499 +  shows "\<forall> bs p. qfree p \<longrightarrow> qfree (CJNB qe p) \<and> (Ifm bs ((CJNB qe p)) = Ifm bs (E p))"
  4.1500 +proof(clarify)
  4.1501 +  fix bs p
  4.1502 +  assume qfp: "qfree p"
  4.1503 +  let ?cjs = "conjuncts p"
  4.1504 +  let ?yes = "fst (List.partition bound0 ?cjs)"
  4.1505 +  let ?no = "snd (List.partition bound0 ?cjs)"
  4.1506 +  let ?cno = "list_conj ?no"
  4.1507 +  let ?cyes = "list_conj ?yes"
  4.1508 +  have part: "List.partition bound0 ?cjs = (?yes,?no)" by simp
  4.1509 +  from partition_P[OF part] have "\<forall> q\<in> set ?yes. bound0 q" by blast 
  4.1510 +  hence yes_nb: "bound0 ?cyes" by (simp add: list_conj_nb) 
  4.1511 +  hence yes_qf: "qfree (decr ?cyes )" by (simp add: decr_qf)
  4.1512 +  from conjuncts_qf[OF qfp] partition_set[OF part] 
  4.1513 +  have " \<forall>q\<in> set ?no. qfree q" by auto
  4.1514 +  hence no_qf: "qfree ?cno"by (simp add: list_conj_qf)
  4.1515 +  with qe have cno_qf:"qfree (qe ?cno )" 
  4.1516 +    and noE: "Ifm bs (qe ?cno) = Ifm bs (E ?cno)" by blast+
  4.1517 +  from cno_qf yes_qf have qf: "qfree (CJNB qe p)" 
  4.1518 +    by (simp add: CJNB_def Let_def conj_qf split_def)
  4.1519 +  {fix bs
  4.1520 +    from conjuncts have "Ifm bs p = (\<forall>q\<in> set ?cjs. Ifm bs q)" by blast
  4.1521 +    also have "\<dots> = ((\<forall>q\<in> set ?yes. Ifm bs q) \<and> (\<forall>q\<in> set ?no. Ifm bs q))"
  4.1522 +      using partition_set[OF part] by auto
  4.1523 +    finally have "Ifm bs p = ((Ifm bs ?cyes) \<and> (Ifm bs ?cno))" using list_conj by simp}
  4.1524 +  hence "Ifm bs (E p) = (\<exists>x. (Ifm (x#bs) ?cyes) \<and> (Ifm (x#bs) ?cno))" by simp
  4.1525 +  also fix y have "\<dots> = (\<exists>x. (Ifm (y#bs) ?cyes) \<and> (Ifm (x#bs) ?cno))"
  4.1526 +    using bound0_I[OF yes_nb, where bs="bs" and b'="y"] by blast
  4.1527 +  also have "\<dots> = (Ifm bs (decr ?cyes) \<and> Ifm bs (E ?cno))"
  4.1528 +    by (auto simp add: decr[OF yes_nb])
  4.1529 +  also have "\<dots> = (Ifm bs (conj (decr ?cyes) (qe ?cno)))"
  4.1530 +    using qe[rule_format, OF no_qf] by auto
  4.1531 +  finally have "Ifm bs (E p) = Ifm bs (CJNB qe p)" 
  4.1532 +    by (simp add: Let_def CJNB_def split_def)
  4.1533 +  with qf show "qfree (CJNB qe p) \<and> Ifm bs (CJNB qe p) = Ifm bs (E p)" by blast
  4.1534 +qed
  4.1535 +
  4.1536 +consts qelim :: "fm \<Rightarrow> (fm \<Rightarrow> fm) \<Rightarrow> fm"
  4.1537 +recdef qelim "measure fmsize"
  4.1538 +  "qelim (E p) = (\<lambda> qe. DJ (CJNB qe) (qelim p qe))"
  4.1539 +  "qelim (A p) = (\<lambda> qe. not (qe ((qelim (NOT p) qe))))"
  4.1540 +  "qelim (NOT p) = (\<lambda> qe. not (qelim p qe))"
  4.1541 +  "qelim (And p q) = (\<lambda> qe. conj (qelim p qe) (qelim q qe))" 
  4.1542 +  "qelim (Or  p q) = (\<lambda> qe. disj (qelim p qe) (qelim q qe))" 
  4.1543 +  "qelim (Imp p q) = (\<lambda> qe. disj (qelim (NOT p) qe) (qelim q qe))"
  4.1544 +  "qelim (Iff p q) = (\<lambda> qe. iff (qelim p qe) (qelim q qe))"
  4.1545 +  "qelim p = (\<lambda> y. simpfm p)"
  4.1546 +
  4.1547 +lemma qelim_ci:
  4.1548 +  assumes qe_inv: "\<forall> bs p. qfree p \<longrightarrow> qfree (qe p) \<and> (Ifm bs (qe p) = Ifm bs (E p))"
  4.1549 +  shows "\<And> bs. qfree (qelim p qe) \<and> (Ifm bs (qelim p qe) = Ifm bs p)"
  4.1550 +using qe_inv DJ_qe[OF CJNB_qe[OF qe_inv]] 
  4.1551 +by(induct p rule: qelim.induct) 
  4.1552 +(auto simp del: simpfm.simps)
  4.1553 +
  4.1554 +
  4.1555 +text {* The @{text "\<int>"} Part *}
  4.1556 +text{* Linearity for fm where Bound 0 ranges over @{text "\<int>"} *}
  4.1557 +consts
  4.1558 +  zsplit0 :: "num \<Rightarrow> int \<times> num" (* splits the bounded from the unbounded part*)
  4.1559 +recdef zsplit0 "measure num_size"
  4.1560 +  "zsplit0 (C c) = (0,C c)"
  4.1561 +  "zsplit0 (Bound n) = (if n=0 then (1, C 0) else (0,Bound n))"
  4.1562 +  "zsplit0 (CN n c a) = zsplit0 (Add (Mul c (Bound n)) a)"
  4.1563 +  "zsplit0 (CF c a b) = zsplit0 (Add (Mul c (Floor a)) b)"
  4.1564 +  "zsplit0 (Neg a) = (let (i',a') =  zsplit0 a in (-i', Neg a'))"
  4.1565 +  "zsplit0 (Add a b) = (let (ia,a') =  zsplit0 a ; 
  4.1566 +                            (ib,b') =  zsplit0 b 
  4.1567 +                            in (ia+ib, Add a' b'))"
  4.1568 +  "zsplit0 (Sub a b) = (let (ia,a') =  zsplit0 a ; 
  4.1569 +                            (ib,b') =  zsplit0 b 
  4.1570 +                            in (ia-ib, Sub a' b'))"
  4.1571 +  "zsplit0 (Mul i a) = (let (i',a') =  zsplit0 a in (i*i', Mul i a'))"
  4.1572 +  "zsplit0 (Floor a) = (let (i',a') =  zsplit0 a in (i',Floor a'))"
  4.1573 +(hints simp add: Let_def)
  4.1574 +
  4.1575 +lemma zsplit0_I:
  4.1576 +  shows "\<And> n a. zsplit0 t = (n,a) \<Longrightarrow> (Inum ((real (x::int)) #bs) (CN 0 n a) = Inum (real x #bs) t) \<and> numbound0 a"
  4.1577 +  (is "\<And> n a. ?S t = (n,a) \<Longrightarrow> (?I x (CN 0 n a) = ?I x t) \<and> ?N a")
  4.1578 +proof(induct t rule: zsplit0.induct)
  4.1579 +  case (1 c n a) thus ?case by auto 
  4.1580 +next
  4.1581 +  case (2 m n a) thus ?case by (cases "m=0") auto
  4.1582 +next
  4.1583 +  case (3 n i a n a') thus ?case by auto
  4.1584 +next 
  4.1585 +  case (4 c a b n a') thus ?case by auto
  4.1586 +next
  4.1587 +  case (5 t n a)
  4.1588 +  let ?nt = "fst (zsplit0 t)"
  4.1589 +  let ?at = "snd (zsplit0 t)"
  4.1590 +  have abj: "zsplit0 t = (?nt,?at)" by simp hence th: "a=Neg ?at \<and> n=-?nt" using prems 
  4.1591 +    by (simp add: Let_def split_def)
  4.1592 +  from abj prems  have th2: "(?I x (CN 0 ?nt ?at) = ?I x t) \<and> ?N ?at" by blast
  4.1593 +  from th2[simplified] th[simplified] show ?case by simp
  4.1594 +next
  4.1595 +  case (6 s t n a)
  4.1596 +  let ?ns = "fst (zsplit0 s)"
  4.1597 +  let ?as = "snd (zsplit0 s)"
  4.1598 +  let ?nt = "fst (zsplit0 t)"
  4.1599 +  let ?at = "snd (zsplit0 t)"
  4.1600 +  have abjs: "zsplit0 s = (?ns,?as)" by simp 
  4.1601 +  moreover have abjt:  "zsplit0 t = (?nt,?at)" by simp 
  4.1602 +  ultimately have th: "a=Add ?as ?at \<and> n=?ns + ?nt" using prems 
  4.1603 +    by (simp add: Let_def split_def)
  4.1604 +  from abjs[symmetric] have bluddy: "\<exists> x y. (x,y) = zsplit0 s" by blast
  4.1605 +  from prems have "(\<exists> x y. (x,y) = zsplit0 s) \<longrightarrow> (\<forall>xa xb. zsplit0 t = (xa, xb) \<longrightarrow> Inum (real x # bs) (CN 0 xa xb) = Inum (real x # bs) t \<and> numbound0 xb)" by simp
  4.1606 +  with bluddy abjt have th3: "(?I x (CN 0 ?nt ?at) = ?I x t) \<and> ?N ?at" by blast
  4.1607 +  from abjs prems  have th2: "(?I x (CN 0 ?ns ?as) = ?I x s) \<and> ?N ?as" by blast
  4.1608 +  from th3[simplified] th2[simplified] th[simplified] show ?case 
  4.1609 +    by (simp add: left_distrib)
  4.1610 +next
  4.1611 +  case (7 s t n a)
  4.1612 +  let ?ns = "fst (zsplit0 s)"
  4.1613 +  let ?as = "snd (zsplit0 s)"
  4.1614 +  let ?nt = "fst (zsplit0 t)"
  4.1615 +  let ?at = "snd (zsplit0 t)"
  4.1616 +  have abjs: "zsplit0 s = (?ns,?as)" by simp 
  4.1617 +  moreover have abjt:  "zsplit0 t = (?nt,?at)" by simp 
  4.1618 +  ultimately have th: "a=Sub ?as ?at \<and> n=?ns - ?nt" using prems 
  4.1619 +    by (simp add: Let_def split_def)
  4.1620 +  from abjs[symmetric] have bluddy: "\<exists> x y. (x,y) = zsplit0 s" by blast
  4.1621 +  from prems have "(\<exists> x y. (x,y) = zsplit0 s) \<longrightarrow> (\<forall>xa xb. zsplit0 t = (xa, xb) \<longrightarrow> Inum (real x # bs) (CN 0 xa xb) = Inum (real x # bs) t \<and> numbound0 xb)" by simp
  4.1622 +  with bluddy abjt have th3: "(?I x (CN 0 ?nt ?at) = ?I x t) \<and> ?N ?at" by blast
  4.1623 +  from abjs prems  have th2: "(?I x (CN 0 ?ns ?as) = ?I x s) \<and> ?N ?as" by blast
  4.1624 +  from th3[simplified] th2[simplified] th[simplified] show ?case 
  4.1625 +    by (simp add: left_diff_distrib)
  4.1626 +next
  4.1627 +  case (8 i t n a)
  4.1628 +  let ?nt = "fst (zsplit0 t)"
  4.1629 +  let ?at = "snd (zsplit0 t)"
  4.1630 +  have abj: "zsplit0 t = (?nt,?at)" by simp hence th: "a=Mul i ?at \<and> n=i*?nt" using prems 
  4.1631 +    by (simp add: Let_def split_def)
  4.1632 +  from abj prems  have th2: "(?I x (CN 0 ?nt ?at) = ?I x t) \<and> ?N ?at" by blast
  4.1633 +  hence " ?I x (Mul i t) = (real i) * ?I x (CN 0 ?nt ?at)" by simp
  4.1634 +  also have "\<dots> = ?I x (CN 0 (i*?nt) (Mul i ?at))" by (simp add: right_distrib)
  4.1635 +  finally show ?case using th th2 by simp
  4.1636 +next
  4.1637 +  case (9 t n a)
  4.1638 +  let ?nt = "fst (zsplit0 t)"
  4.1639 +  let ?at = "snd (zsplit0 t)"
  4.1640 +  have abj: "zsplit0 t = (?nt,?at)" by simp hence th: "a= Floor ?at \<and> n=?nt" using prems 
  4.1641 +    by (simp add: Let_def split_def)
  4.1642 +  from abj prems  have th2: "(?I x (CN 0 ?nt ?at) = ?I x t) \<and> ?N ?at" by blast
  4.1643 +  hence na: "?N a" using th by simp
  4.1644 +  have th': "(real ?nt)*(real x) = real (?nt * x)" by simp
  4.1645 +  have "?I x (Floor t) = ?I x (Floor (CN 0 ?nt ?at))" using th2 by simp
  4.1646 +  also have "\<dots> = real (floor ((real ?nt)* real(x) + ?I x ?at))" by simp
  4.1647 +  also have "\<dots> = real (floor (?I x ?at + real (?nt* x)))" by (simp add: add_ac)
  4.1648 +  also have "\<dots> = real (floor (?I x ?at) + (?nt* x))" 
  4.1649 +    using floor_add[where x="?I x ?at" and a="?nt* x"] by simp 
  4.1650 +  also have "\<dots> = real (?nt)*(real x) + real (floor (?I x ?at))" by (simp add: add_ac)
  4.1651 +  finally have "?I x (Floor t) = ?I x (CN 0 n a)" using th by simp
  4.1652 +  with na show ?case by simp
  4.1653 +qed
  4.1654 +
  4.1655 +consts
  4.1656 +  iszlfm :: "fm \<Rightarrow> real list \<Rightarrow> bool"   (* Linearity test for fm *)
  4.1657 +  zlfm :: "fm \<Rightarrow> fm"       (* Linearity transformation for fm *)
  4.1658 +recdef iszlfm "measure size"
  4.1659 +  "iszlfm (And p q) = (\<lambda> bs. iszlfm p bs \<and> iszlfm q bs)" 
  4.1660 +  "iszlfm (Or p q) = (\<lambda> bs. iszlfm p bs \<and> iszlfm q bs)" 
  4.1661 +  "iszlfm (Eq  (CN 0 c e)) = (\<lambda> bs. c>0 \<and> numbound0 e \<and> isint e bs)"
  4.1662 +  "iszlfm (NEq (CN 0 c e)) = (\<lambda> bs. c>0 \<and> numbound0 e \<and> isint e bs)"
  4.1663 +  "iszlfm (Lt  (CN 0 c e)) = (\<lambda> bs. c>0 \<and> numbound0 e \<and> isint e bs)"
  4.1664 +  "iszlfm (Le  (CN 0 c e)) = (\<lambda> bs. c>0 \<and> numbound0 e \<and> isint e bs)"
  4.1665 +  "iszlfm (Gt  (CN 0 c e)) = (\<lambda> bs. c>0 \<and> numbound0 e \<and> isint e bs)"
  4.1666 +  "iszlfm (Ge  (CN 0 c e)) = (\<lambda> bs. c>0 \<and> numbound0 e \<and> isint e bs)"
  4.1667 +  "iszlfm (Dvd i (CN 0 c e)) = 
  4.1668 +                 (\<lambda> bs. c>0 \<and> i>0 \<and> numbound0 e \<and> isint e bs)"
  4.1669 +  "iszlfm (NDvd i (CN 0 c e))= 
  4.1670 +                 (\<lambda> bs. c>0 \<and> i>0 \<and> numbound0 e \<and> isint e bs)"
  4.1671 +  "iszlfm p = (\<lambda> bs. isatom p \<and> (bound0 p))"
  4.1672 +
  4.1673 +lemma zlin_qfree: "iszlfm p bs \<Longrightarrow> qfree p"
  4.1674 +  by (induct p rule: iszlfm.induct) auto
  4.1675 +
  4.1676 +lemma iszlfm_gen:
  4.1677 +  assumes lp: "iszlfm p (x#bs)"
  4.1678 +  shows "\<forall> y. iszlfm p (y#bs)"
  4.1679 +proof
  4.1680 +  fix y
  4.1681 +  show "iszlfm p (y#bs)"
  4.1682 +    using lp
  4.1683 +  by(induct p rule: iszlfm.induct, simp_all add: numbound0_gen[rule_format, where x="x" and y="y"])
  4.1684 +qed
  4.1685 +
  4.1686 +lemma conj_zl[simp]: "iszlfm p bs \<Longrightarrow> iszlfm q bs \<Longrightarrow> iszlfm (conj p q) bs"
  4.1687 +  using conj_def by (cases p,auto)
  4.1688 +lemma disj_zl[simp]: "iszlfm p bs \<Longrightarrow> iszlfm q bs \<Longrightarrow> iszlfm (disj p q) bs"
  4.1689 +  using disj_def by (cases p,auto)
  4.1690 +lemma not_zl[simp]: "iszlfm p bs \<Longrightarrow> iszlfm (not p) bs"
  4.1691 +  by (induct p rule:iszlfm.induct ,auto)
  4.1692 +
  4.1693 +recdef zlfm "measure fmsize"
  4.1694 +  "zlfm (And p q) = conj (zlfm p) (zlfm q)"
  4.1695 +  "zlfm (Or p q) = disj (zlfm p) (zlfm q)"
  4.1696 +  "zlfm (Imp p q) = disj (zlfm (NOT p)) (zlfm q)"
  4.1697 +  "zlfm (Iff p q) = disj (conj (zlfm p) (zlfm q)) (conj (zlfm (NOT p)) (zlfm (NOT q)))"
  4.1698 +  "zlfm (Lt a) = (let (c,r) = zsplit0 a in 
  4.1699 +     if c=0 then Lt r else 
  4.1700 +     if c>0 then Or (Lt (CN 0 c (Neg (Floor (Neg r))))) (And (Eq (CN 0 c (Neg (Floor (Neg r))))) (Lt (Add (Floor (Neg r)) r))) 
  4.1701 +     else Or (Gt (CN 0 (-c) (Floor(Neg r)))) (And (Eq(CN 0 (-c) (Floor(Neg r)))) (Lt (Add (Floor (Neg r)) r))))"
  4.1702 +  "zlfm (Le a) = (let (c,r) = zsplit0 a in 
  4.1703 +     if c=0 then Le r else 
  4.1704 +     if c>0 then Or (Le (CN 0 c (Neg (Floor (Neg r))))) (And (Eq (CN 0 c (Neg (Floor (Neg r))))) (Lt (Add (Floor (Neg r)) r))) 
  4.1705 +     else Or (Ge (CN 0 (-c) (Floor(Neg r)))) (And (Eq(CN 0 (-c) (Floor(Neg r)))) (Lt (Add (Floor (Neg r)) r))))"
  4.1706 +  "zlfm (Gt a) = (let (c,r) = zsplit0 a in 
  4.1707 +     if c=0 then Gt r else 
  4.1708 +     if c>0 then Or (Gt (CN 0 c (Floor r))) (And (Eq (CN 0 c (Floor r))) (Lt (Sub (Floor r) r))) 
  4.1709 +     else Or (Lt (CN 0 (-c) (Neg (Floor r)))) (And (Eq(CN 0 (-c) (Neg (Floor r)))) (Lt (Sub (Floor r) r))))"
  4.1710 +  "zlfm (Ge a) = (let (c,r) = zsplit0 a in 
  4.1711 +     if c=0 then Ge r else 
  4.1712 +     if c>0 then Or (Ge (CN 0 c (Floor r))) (And (Eq (CN 0 c (Floor r))) (Lt (Sub (Floor r) r))) 
  4.1713 +     else Or (Le (CN 0 (-c) (Neg (Floor r)))) (And (Eq(CN 0 (-c) (Neg (Floor r)))) (Lt (Sub (Floor r) r))))"
  4.1714 +  "zlfm (Eq a) = (let (c,r) = zsplit0 a in 
  4.1715 +              if c=0 then Eq r else 
  4.1716 +      if c>0 then (And (Eq (CN 0 c (Neg (Floor (Neg r))))) (Eq (Add (Floor (Neg r)) r)))
  4.1717 +      else (And (Eq (CN 0 (-c) (Floor (Neg r)))) (Eq (Add (Floor (Neg r)) r))))"
  4.1718 +  "zlfm (NEq a) = (let (c,r) = zsplit0 a in 
  4.1719 +              if c=0 then NEq r else 
  4.1720 +      if c>0 then (Or (NEq (CN 0 c (Neg (Floor (Neg r))))) (NEq (Add (Floor (Neg r)) r)))
  4.1721 +      else (Or (NEq (CN 0 (-c) (Floor (Neg r)))) (NEq (Add (Floor (Neg r)) r))))"
  4.1722 +  "zlfm (Dvd i a) = (if i=0 then zlfm (Eq a) 
  4.1723 +  else (let (c,r) = zsplit0 a in 
  4.1724 +              if c=0 then Dvd (abs i) r else 
  4.1725 +      if c>0 then And (Eq (Sub (Floor r) r)) (Dvd (abs i) (CN 0 c (Floor r))) 
  4.1726 +      else And (Eq (Sub (Floor r) r)) (Dvd (abs i) (CN 0 (-c) (Neg (Floor r))))))"
  4.1727 +  "zlfm (NDvd i a) = (if i=0 then zlfm (NEq a) 
  4.1728 +  else (let (c,r) = zsplit0 a in 
  4.1729 +              if c=0 then NDvd (abs i) r else 
  4.1730 +      if c>0 then Or (NEq (Sub (Floor r) r)) (NDvd (abs i) (CN 0 c (Floor r))) 
  4.1731 +      else Or (NEq (Sub (Floor r) r)) (NDvd (abs i) (CN 0 (-c) (Neg (Floor r))))))"
  4.1732 +  "zlfm (NOT (And p q)) = disj (zlfm (NOT p)) (zlfm (NOT q))"
  4.1733 +  "zlfm (NOT (Or p q)) = conj (zlfm (NOT p)) (zlfm (NOT q))"
  4.1734 +  "zlfm (NOT (Imp p q)) = conj (zlfm p) (zlfm (NOT q))"
  4.1735 +  "zlfm (NOT (Iff p q)) = disj (conj(zlfm p) (zlfm(NOT q))) (conj (zlfm(NOT p)) (zlfm q))"
  4.1736 +  "zlfm (NOT (NOT p)) = zlfm p"
  4.1737 +  "zlfm (NOT T) = F"
  4.1738 +  "zlfm (NOT F) = T"
  4.1739 +  "zlfm (NOT (Lt a)) = zlfm (Ge a)"
  4.1740 +  "zlfm (NOT (Le a)) = zlfm (Gt a)"
  4.1741 +  "zlfm (NOT (Gt a)) = zlfm (Le a)"
  4.1742 +  "zlfm (NOT (Ge a)) = zlfm (Lt a)"
  4.1743 +  "zlfm (NOT (Eq a)) = zlfm (NEq a)"
  4.1744 +  "zlfm (NOT (NEq a)) = zlfm (Eq a)"
  4.1745 +  "zlfm (NOT (Dvd i a)) = zlfm (NDvd i a)"
  4.1746 +  "zlfm (NOT (NDvd i a)) = zlfm (Dvd i a)"
  4.1747 +  "zlfm p = p" (hints simp add: fmsize_pos)
  4.1748 +
  4.1749 +lemma split_int_less_real: 
  4.1750 +  "(real (a::int) < b) = (a < floor b \<or> (a = floor b \<and> real (floor b) < b))"
  4.1751 +proof( auto)
  4.1752 +  assume alb: "real a < b" and agb: "\<not> a < floor b"
  4.1753 +  from agb have "floor b \<le> a" by simp hence th: "b < real a + 1" by (simp only: floor_le_eq)
  4.1754 +  from floor_eq[OF alb th] show "a= floor b" by simp 
  4.1755 +next
  4.1756 +  assume alb: "a < floor b"
  4.1757 +  hence "real a < real (floor b)" by simp
  4.1758 +  moreover have "real (floor b) \<le> b" by simp ultimately show  "real a < b" by arith 
  4.1759 +qed
  4.1760 +
  4.1761 +lemma split_int_less_real': 
  4.1762 +  "(real (a::int) + b < 0) = (real a - real (floor(-b)) < 0 \<or> (real a - real (floor (-b)) = 0 \<and> real (floor (-b)) + b < 0))"
  4.1763 +proof- 
  4.1764 +  have "(real a + b <0) = (real a < -b)" by arith
  4.1765 +  with split_int_less_real[where a="a" and b="-b"] show ?thesis by arith  
  4.1766 +qed
  4.1767 +
  4.1768 +lemma split_int_gt_real': 
  4.1769 +  "(real (a::int) + b > 0) = (real a + real (floor b) > 0 \<or> (real a + real (floor b) = 0 \<and> real (floor b) - b < 0))"
  4.1770 +proof- 
  4.1771 +  have th: "(real a + b >0) = (real (-a) + (-b)< 0)" by arith
  4.1772 +  show ?thesis using myless[rule_format, where b="real (floor b)"] 
  4.1773 +    by (simp only:th split_int_less_real'[where a="-a" and b="-b"]) 
  4.1774 +    (simp add: algebra_simps diff_def[symmetric],arith)
  4.1775 +qed
  4.1776 +
  4.1777 +lemma split_int_le_real: 
  4.1778 +  "(real (a::int) \<le> b) = (a \<le> floor b \<or> (a = floor b \<and> real (floor b) < b))"
  4.1779 +proof( auto)
  4.1780 +  assume alb: "real a \<le> b" and agb: "\<not> a \<le> floor b"
  4.1781 +  from alb have "floor (real a) \<le> floor b " by (simp only: floor_mono2) 
  4.1782 +  hence "a \<le> floor b" by simp with agb show "False" by simp
  4.1783 +next
  4.1784 +  assume alb: "a \<le> floor b"
  4.1785 +  hence "real a \<le> real (floor b)" by (simp only: floor_mono2)
  4.1786 +  also have "\<dots>\<le> b" by simp  finally show  "real a \<le> b" . 
  4.1787 +qed
  4.1788 +
  4.1789 +lemma split_int_le_real': 
  4.1790 +  "(real (a::int) + b \<le> 0) = (real a - real (floor(-b)) \<le> 0 \<or> (real a - real (floor (-b)) = 0 \<and> real (floor (-b)) + b < 0))"
  4.1791 +proof- 
  4.1792 +  have "(real a + b \<le>0) = (real a \<le> -b)" by arith
  4.1793 +  with split_int_le_real[where a="a" and b="-b"] show ?thesis by arith  
  4.1794 +qed
  4.1795 +
  4.1796 +lemma split_int_ge_real': 
  4.1797 +  "(real (a::int) + b \<ge> 0) = (real a + real (floor b) \<ge> 0 \<or> (real a + real (floor b) = 0 \<and> real (floor b) - b < 0))"
  4.1798 +proof- 
  4.1799 +  have th: "(real a + b \<ge>0) = (real (-a) + (-b) \<le> 0)" by arith
  4.1800 +  show ?thesis by (simp only: th split_int_le_real'[where a="-a" and b="-b"])
  4.1801 +    (simp add: algebra_simps diff_def[symmetric],arith)
  4.1802 +qed
  4.1803 +
  4.1804 +lemma split_int_eq_real: "(real (a::int) = b) = ( a = floor b \<and> b = real (floor b))" (is "?l = ?r")
  4.1805 +by auto
  4.1806 +
  4.1807 +lemma split_int_eq_real': "(real (a::int) + b = 0) = ( a - floor (-b) = 0 \<and> real (floor (-b)) + b = 0)" (is "?l = ?r")
  4.1808 +proof-
  4.1809 +  have "?l = (real a = -b)" by arith
  4.1810 +  with split_int_eq_real[where a="a" and b="-b"] show ?thesis by simp arith
  4.1811 +qed
  4.1812 +
  4.1813 +lemma zlfm_I:
  4.1814 +  assumes qfp: "qfree p"
  4.1815 +  shows "(Ifm (real i #bs) (zlfm p) = Ifm (real i# bs) p) \<and> iszlfm (zlfm p) (real (i::int) #bs)"
  4.1816 +  (is "(?I (?l p) = ?I p) \<and> ?L (?l p)")
  4.1817 +using qfp
  4.1818 +proof(induct p rule: zlfm.induct)
  4.1819 +  case (5 a) 
  4.1820 +  let ?c = "fst (zsplit0 a)"
  4.1821 +  let ?r = "snd (zsplit0 a)"
  4.1822 +  have spl: "zsplit0 a = (?c,?r)" by simp
  4.1823 +  from zsplit0_I[OF spl, where x="i" and bs="bs"] 
  4.1824 +  have Ia:"Inum (real i # bs) a = Inum (real i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto 
  4.1825 +  let ?N = "\<lambda> t. Inum (real i#bs) t"
  4.1826 +  have "?c = 0 \<or> (?c >0 \<and> ?c\<noteq>0) \<or> (?c<0 \<and> ?c\<noteq>0)" by arith
  4.1827 +  moreover
  4.1828 +  {assume "?c=0" hence ?case using zsplit0_I[OF spl, where x="i" and bs="bs"] 
  4.1829 +      by (cases "?r", simp_all add: Let_def split_def,case_tac "nat", simp_all)}
  4.1830 +  moreover
  4.1831 +  {assume cp: "?c > 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (Lt a))" 
  4.1832 +      by (simp add: nb Let_def split_def isint_Floor isint_neg)
  4.1833 +    have "?I (Lt a) = (real (?c * i) + (?N ?r) < 0)" using Ia by (simp add: Let_def split_def)
  4.1834 +    also have "\<dots> = (?I (?l (Lt a)))" apply (simp only: split_int_less_real'[where a="?c*i" and b="?N ?r"]) by (simp add: Ia cp cnz Let_def split_def diff_def)
  4.1835 +    finally have ?case using l by simp}
  4.1836 +  moreover
  4.1837 +  {assume cn: "?c < 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (Lt a))" 
  4.1838 +      by (simp add: nb Let_def split_def isint_Floor isint_neg)
  4.1839 +    have "?I (Lt a) = (real (?c * i) + (?N ?r) < 0)" using Ia by (simp add: Let_def split_def)
  4.1840 +    also from cn cnz have "\<dots> = (?I (?l (Lt a)))" by (simp only: split_int_less_real'[where a="?c*i" and b="?N ?r"]) (simp add: Ia Let_def split_def diff_def[symmetric] add_ac, arith)
  4.1841 +    finally have ?case using l by simp}
  4.1842 +  ultimately show ?case by blast
  4.1843 +next
  4.1844 +  case (6 a)
  4.1845 +  let ?c = "fst (zsplit0 a)"
  4.1846 +  let ?r = "snd (zsplit0 a)"
  4.1847 +  have spl: "zsplit0 a = (?c,?r)" by simp
  4.1848 +  from zsplit0_I[OF spl, where x="i" and bs="bs"] 
  4.1849 +  have Ia:"Inum (real i # bs) a = Inum (real i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto 
  4.1850 +  let ?N = "\<lambda> t. Inum (real i#bs) t"
  4.1851 +  have "?c = 0 \<or> (?c >0 \<and> ?c\<noteq>0) \<or> (?c<0 \<and> ?c\<noteq>0)" by arith
  4.1852 +  moreover
  4.1853 +  {assume "?c=0" hence ?case using zsplit0_I[OF spl, where x="i" and bs="bs"] 
  4.1854 +      by (cases "?r", simp_all add: Let_def split_def, case_tac "nat",simp_all)}
  4.1855 +  moreover
  4.1856 +  {assume cp: "?c > 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (Le a))" 
  4.1857 +      by (simp add: nb Let_def split_def isint_Floor isint_neg)
  4.1858 +    have "?I (Le a) = (real (?c * i) + (?N ?r) \<le> 0)" using Ia by (simp add: Let_def split_def)
  4.1859 +    also have "\<dots> = (?I (?l (Le a)))" by (simp only: split_int_le_real'[where a="?c*i" and b="?N ?r"]) (simp add: Ia cp cnz Let_def split_def diff_def)
  4.1860 +    finally have ?case using l by simp}
  4.1861 +  moreover
  4.1862 +  {assume cn: "?c < 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (Le a))" 
  4.1863 +      by (simp add: nb Let_def split_def isint_Floor isint_neg)
  4.1864 +    have "?I (Le a) = (real (?c * i) + (?N ?r) \<le> 0)" using Ia by (simp add: Let_def split_def)
  4.1865 +    also from cn cnz have "\<dots> = (?I (?l (Le a)))" by (simp only: split_int_le_real'[where a="?c*i" and b="?N ?r"]) (simp add: Ia Let_def split_def diff_def[symmetric] add_ac ,arith)
  4.1866 +    finally have ?case using l by simp}
  4.1867 +  ultimately show ?case by blast
  4.1868 +next
  4.1869 +  case (7 a) 
  4.1870 +  let ?c = "fst (zsplit0 a)"
  4.1871 +  let ?r = "snd (zsplit0 a)"
  4.1872 +  have spl: "zsplit0 a = (?c,?r)" by simp
  4.1873 +  from zsplit0_I[OF spl, where x="i" and bs="bs"] 
  4.1874 +  have Ia:"Inum (real i # bs) a = Inum (real i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto 
  4.1875 +  let ?N = "\<lambda> t. Inum (real i#bs) t"
  4.1876 +  have "?c = 0 \<or> (?c >0 \<and> ?c\<noteq>0) \<or> (?c<0 \<and> ?c\<noteq>0)" by arith
  4.1877 +  moreover
  4.1878 +  {assume "?c=0" hence ?case using zsplit0_I[OF spl, where x="i" and bs="bs"] 
  4.1879 +      by (cases "?r", simp_all add: Let_def split_def, case_tac "nat", simp_all)}
  4.1880 +  moreover
  4.1881 +  {assume cp: "?c > 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (Gt a))" 
  4.1882 +      by (simp add: nb Let_def split_def isint_Floor isint_neg)
  4.1883 +    have "?I (Gt a) = (real (?c * i) + (?N ?r) > 0)" using Ia by (simp add: Let_def split_def)
  4.1884 +    also have "\<dots> = (?I (?l (Gt a)))" by (simp only: split_int_gt_real'[where a="?c*i" and b="?N ?r"]) (simp add: Ia cp cnz Let_def split_def diff_def)
  4.1885 +    finally have ?case using l by simp}
  4.1886 +  moreover
  4.1887 +  {assume cn: "?c < 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (Gt a))" 
  4.1888 +      by (simp add: nb Let_def split_def isint_Floor isint_neg)
  4.1889 +    have "?I (Gt a) = (real (?c * i) + (?N ?r) > 0)" using Ia by (simp add: Let_def split_def)
  4.1890 +    also from cn cnz have "\<dots> = (?I (?l (Gt a)))" by (simp only: split_int_gt_real'[where a="?c*i" and b="?N ?r"]) (simp add: Ia Let_def split_def diff_def[symmetric] add_ac, arith)
  4.1891 +    finally have ?case using l by simp}
  4.1892 +  ultimately show ?case by blast
  4.1893 +next
  4.1894 +  case (8 a)
  4.1895 +   let ?c = "fst (zsplit0 a)"
  4.1896 +  let ?r = "snd (zsplit0 a)"
  4.1897 +  have spl: "zsplit0 a = (?c,?r)" by simp
  4.1898 +  from zsplit0_I[OF spl, where x="i" and bs="bs"] 
  4.1899 +  have Ia:"Inum (real i # bs) a = Inum (real i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto 
  4.1900 +  let ?N = "\<lambda> t. Inum (real i#bs) t"
  4.1901 +  have "?c = 0 \<or> (?c >0 \<and> ?c\<noteq>0) \<or> (?c<0 \<and> ?c\<noteq>0)" by arith
  4.1902 +  moreover
  4.1903 +  {assume "?c=0" hence ?case using zsplit0_I[OF spl, where x="i" and bs="bs"] 
  4.1904 +      by (cases "?r", simp_all add: Let_def split_def, case_tac "nat", simp_all)}
  4.1905 +  moreover
  4.1906 +  {assume cp: "?c > 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (Ge a))" 
  4.1907 +      by (simp add: nb Let_def split_def isint_Floor isint_neg)
  4.1908 +    have "?I (Ge a) = (real (?c * i) + (?N ?r) \<ge> 0)" using Ia by (simp add: Let_def split_def)
  4.1909 +    also have "\<dots> = (?I (?l (Ge a)))" by (simp only: split_int_ge_real'[where a="?c*i" and b="?N ?r"]) (simp add: Ia cp cnz Let_def split_def diff_def)
  4.1910 +    finally have ?case using l by simp}
  4.1911 +  moreover
  4.1912 +  {assume cn: "?c < 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (Ge a))" 
  4.1913 +      by (simp add: nb Let_def split_def isint_Floor isint_neg)
  4.1914 +    have "?I (Ge a) = (real (?c * i) + (?N ?r) \<ge> 0)" using Ia by (simp add: Let_def split_def)
  4.1915 +    also from cn cnz have "\<dots> = (?I (?l (Ge a)))" by (simp only: split_int_ge_real'[where a="?c*i" and b="?N ?r"]) (simp add: Ia Let_def split_def diff_def[symmetric] add_ac, arith)
  4.1916 +    finally have ?case using l by simp}
  4.1917 +  ultimately show ?case by blast
  4.1918 +next
  4.1919 +  case (9 a)
  4.1920 +  let ?c = "fst (zsplit0 a)"
  4.1921 +  let ?r = "snd (zsplit0 a)"
  4.1922 +  have spl: "zsplit0 a = (?c,?r)" by simp
  4.1923 +  from zsplit0_I[OF spl, where x="i" and bs="bs"] 
  4.1924 +  have Ia:"Inum (real i # bs) a = Inum (real i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto 
  4.1925 +  let ?N = "\<lambda> t. Inum (real i#bs) t"
  4.1926 +  have "?c = 0 \<or> (?c >0 \<and> ?c\<noteq>0) \<or> (?c<0 \<and> ?c\<noteq>0)" by arith
  4.1927 +  moreover
  4.1928 +  {assume "?c=0" hence ?case using zsplit0_I[OF spl, where x="i" and bs="bs"] 
  4.1929 +      by (cases "?r", simp_all add: Let_def split_def, case_tac "nat", simp_all)}
  4.1930 +  moreover
  4.1931 +  {assume cp: "?c > 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (Eq a))" 
  4.1932 +      by (simp add: nb Let_def split_def isint_Floor isint_neg)
  4.1933 +    have "?I (Eq a) = (real (?c * i) + (?N ?r) = 0)" using Ia by (simp add: Let_def split_def)
  4.1934 +    also have "\<dots> = (?I (?l (Eq a)))" using cp cnz  by (simp only: split_int_eq_real'[where a="?c*i" and b="?N ?r"]) (simp add: Let_def split_def Ia real_of_int_mult[symmetric] del: real_of_int_mult)
  4.1935 +    finally have ?case using l by simp}
  4.1936 +  moreover
  4.1937 +  {assume cn: "?c < 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (Eq a))" 
  4.1938 +      by (simp add: nb Let_def split_def isint_Floor isint_neg)
  4.1939 +    have "?I (Eq a) = (real (?c * i) + (?N ?r) = 0)" using Ia by (simp add: Let_def split_def)
  4.1940 +    also from cn cnz have "\<dots> = (?I (?l (Eq a)))" by (simp only: split_int_eq_real'[where a="?c*i" and b="?N ?r"]) (simp add: Let_def split_def Ia real_of_int_mult[symmetric] del: real_of_int_mult,arith)
  4.1941 +    finally have ?case using l by simp}
  4.1942 +  ultimately show ?case by blast
  4.1943 +next
  4.1944 +  case (10 a)
  4.1945 +  let ?c = "fst (zsplit0 a)"
  4.1946 +  let ?r = "snd (zsplit0 a)"
  4.1947 +  have spl: "zsplit0 a = (?c,?r)" by simp
  4.1948 +  from zsplit0_I[OF spl, where x="i" and bs="bs"] 
  4.1949 +  have Ia:"Inum (real i # bs) a = Inum (real i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto 
  4.1950 +  let ?N = "\<lambda> t. Inum (real i#bs) t"
  4.1951 +  have "?c = 0 \<or> (?c >0 \<and> ?c\<noteq>0) \<or> (?c<0 \<and> ?c\<noteq>0)" by arith
  4.1952 +  moreover
  4.1953 +  {assume "?c=0" hence ?case using zsplit0_I[OF spl, where x="i" and bs="bs"] 
  4.1954 +      by (cases "?r", simp_all add: Let_def split_def, case_tac "nat", simp_all)}
  4.1955 +  moreover
  4.1956 +  {assume cp: "?c > 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (NEq a))" 
  4.1957 +      by (simp add: nb Let_def split_def isint_Floor isint_neg)
  4.1958 +    have "?I (NEq a) = (real (?c * i) + (?N ?r) \<noteq> 0)" using Ia by (simp add: Let_def split_def)
  4.1959 +    also have "\<dots> = (?I (?l (NEq a)))" using cp cnz  by (simp only: split_int_eq_real'[where a="?c*i" and b="?N ?r"]) (simp add: Let_def split_def Ia real_of_int_mult[symmetric] del: real_of_int_mult)
  4.1960 +    finally have ?case using l by simp}
  4.1961 +  moreover
  4.1962 +  {assume cn: "?c < 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (NEq a))" 
  4.1963 +      by (simp add: nb Let_def split_def isint_Floor isint_neg)
  4.1964 +    have "?I (NEq a) = (real (?c * i) + (?N ?r) \<noteq> 0)" using Ia by (simp add: Let_def split_def)
  4.1965 +    also from cn cnz have "\<dots> = (?I (?l (NEq a)))" by (simp only: split_int_eq_real'[where a="?c*i" and b="?N ?r"]) (simp add: Let_def split_def Ia real_of_int_mult[symmetric] del: real_of_int_mult,arith)
  4.1966 +    finally have ?case using l by simp}
  4.1967 +  ultimately show ?case by blast
  4.1968 +next
  4.1969 +  case (11 j a)
  4.1970 +  let ?c = "fst (zsplit0 a)"
  4.1971 +  let ?r = "snd (zsplit0 a)"
  4.1972 +  have spl: "zsplit0 a = (?c,?r)" by simp
  4.1973 +  from zsplit0_I[OF spl, where x="i" and bs="bs"] 
  4.1974 +  have Ia:"Inum (real i # bs) a = Inum (real i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto 
  4.1975 +  let ?N = "\<lambda> t. Inum (real i#bs) t"
  4.1976 +  have "j=0 \<or> (j\<noteq>0 \<and> ?c = 0) \<or> (j\<noteq>0 \<and> ?c >0 \<and> ?c\<noteq>0) \<or> (j\<noteq> 0 \<and> ?c<0 \<and> ?c\<noteq>0)" by arith
  4.1977 +  moreover
  4.1978 +  {assume "j=0" hence z: "zlfm (Dvd j a) = (zlfm (Eq a))" by (simp add: Let_def) 
  4.1979 +    hence ?case using prems by (simp del: zlfm.simps add: rdvd_left_0_eq)}
  4.1980 +  moreover
  4.1981 +  {assume "?c=0" and "j\<noteq>0" hence ?case 
  4.1982 +      using zsplit0_I[OF spl, where x="i" and bs="bs"] rdvd_abs1[where d="j"]
  4.1983 +      by (cases "?r", simp_all add: Let_def split_def, case_tac "nat", simp_all)}
  4.1984 +  moreover
  4.1985 +  {assume cp: "?c > 0" and cnz: "?c\<noteq>0" and jnz: "j\<noteq>0" hence l: "?L (?l (Dvd j a))" 
  4.1986 +      by (simp add: nb Let_def split_def isint_Floor isint_neg)
  4.1987 +    have "?I (Dvd j a) = (real j rdvd (real (?c * i) + (?N ?r)))" 
  4.1988 +      using Ia by (simp add: Let_def split_def)
  4.1989 +    also have "\<dots> = (real (abs j) rdvd real (?c*i) + (?N ?r))" 
  4.1990 +      by (simp only: rdvd_abs1[where d="j" and t="real (?c*i) + ?N ?r", symmetric]) simp
  4.1991 +    also have "\<dots> = ((abs j) dvd (floor ((?N ?r) + real (?c*i))) \<and> 
  4.1992 +       (real (floor ((?N ?r) + real (?c*i))) = (real (?c*i) + (?N ?r))))" 
  4.1993 +      by(simp only: int_rdvd_real[where i="abs j" and x="real (?c*i) + (?N ?r)"]) (simp only: add_ac)
  4.1994 +    also have "\<dots> = (?I (?l (Dvd j a)))" using cp cnz jnz  
  4.1995 +      by (simp add: Let_def split_def int_rdvd_iff[symmetric]  
  4.1996 +	del: real_of_int_mult) (auto simp add: add_ac)
  4.1997 +    finally have ?case using l jnz  by simp }
  4.1998 +  moreover
  4.1999 +  {assume cn: "?c < 0" and cnz: "?c\<noteq>0" and jnz: "j\<noteq>0" hence l: "?L (?l (Dvd j a))" 
  4.2000 +      by (simp add: nb Let_def split_def isint_Floor isint_neg)
  4.2001 +    have "?I (Dvd j a) = (real j rdvd (real (?c * i) + (?N ?r)))" 
  4.2002 +      using Ia by (simp add: Let_def split_def)
  4.2003 +    also have "\<dots> = (real (abs j) rdvd real (?c*i) + (?N ?r))" 
  4.2004 +      by (simp only: rdvd_abs1[where d="j" and t="real (?c*i) + ?N ?r", symmetric]) simp
  4.2005 +    also have "\<dots> = ((abs j) dvd (floor ((?N ?r) + real (?c*i))) \<and> 
  4.2006 +       (real (floor ((?N ?r) + real (?c*i))) = (real (?c*i) + (?N ?r))))" 
  4.2007 +      by(simp only: int_rdvd_real[where i="abs j" and x="real (?c*i) + (?N ?r)"]) (simp only: add_ac)
  4.2008 +    also have "\<dots> = (?I (?l (Dvd j a)))" using cn cnz jnz
  4.2009 +      using rdvd_minus [where d="abs j" and t="real (?c*i + floor (?N ?r))", simplified, symmetric]
  4.2010 +      by (simp add: Let_def split_def int_rdvd_iff[symmetric]  
  4.2011 +	del: real_of_int_mult) (auto simp add: add_ac)
  4.2012 +    finally have ?case using l jnz by blast }
  4.2013 +  ultimately show ?case by blast
  4.2014 +next
  4.2015 +  case (12 j a)
  4.2016 +  let ?c = "fst (zsplit0 a)"
  4.2017 +  let ?r = "snd (zsplit0 a)"
  4.2018 +  have spl: "zsplit0 a = (?c,?r)" by simp
  4.2019 +  from zsplit0_I[OF spl, where x="i" and bs="bs"] 
  4.2020 +  have Ia:"Inum (real i # bs) a = Inum (real i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto 
  4.2021 +  let ?N = "\<lambda> t. Inum (real i#bs) t"
  4.2022 +  have "j=0 \<or> (j\<noteq>0 \<and> ?c = 0) \<or> (j\<noteq>0 \<and> ?c >0 \<and> ?c\<noteq>0) \<or> (j\<noteq> 0 \<and> ?c<0 \<and> ?c\<noteq>0)" by arith
  4.2023 +  moreover
  4.2024 +  {assume "j=0" hence z: "zlfm (NDvd j a) = (zlfm (NEq a))" by (simp add: Let_def) 
  4.2025 +    hence ?case using prems by (simp del: zlfm.simps add: rdvd_left_0_eq)}
  4.2026 +  moreover
  4.2027 +  {assume "?c=0" and "j\<noteq>0" hence ?case 
  4.2028 +      using zsplit0_I[OF spl, where x="i" and bs="bs"] rdvd_abs1[where d="j"]
  4.2029 +      by (cases "?r", simp_all add: Let_def split_def, case_tac "nat", simp_all)}
  4.2030 +  moreover
  4.2031 +  {assume cp: "?c > 0" and cnz: "?c\<noteq>0" and jnz: "j\<noteq>0" hence l: "?L (?l (NDvd j a))" 
  4.2032 +      by (simp add: nb Let_def split_def isint_Floor isint_neg)
  4.2033 +    have "?I (NDvd j a) = (\<not> (real j rdvd (real (?c * i) + (?N ?r))))" 
  4.2034 +      using Ia by (simp add: Let_def split_def)
  4.2035 +    also have "\<dots> = (\<not> (real (abs j) rdvd real (?c*i) + (?N ?r)))" 
  4.2036 +      by (simp only: rdvd_abs1[where d="j" and t="real (?c*i) + ?N ?r", symmetric]) simp
  4.2037 +    also have "\<dots> = (\<not> ((abs j) dvd (floor ((?N ?r) + real (?c*i))) \<and> 
  4.2038 +       (real (floor ((?N ?r) + real (?c*i))) = (real (?c*i) + (?N ?r)))))" 
  4.2039 +      by(simp only: int_rdvd_real[where i="abs j" and x="real (?c*i) + (?N ?r)"]) (simp only: add_ac)
  4.2040 +    also have "\<dots> = (?I (?l (NDvd j a)))" using cp cnz jnz  
  4.2041 +      by (simp add: Let_def split_def int_rdvd_iff[symmetric]  
  4.2042 +	del: real_of_int_mult) (auto simp add: add_ac)
  4.2043 +    finally have ?case using l jnz  by simp }
  4.2044 +  moreover
  4.2045 +  {assume cn: "?c < 0" and cnz: "?c\<noteq>0" and jnz: "j\<noteq>0" hence l: "?L (?l (NDvd j a))" 
  4.2046 +      by (simp add: nb Let_def split_def isint_Floor isint_neg)
  4.2047 +    have "?I (NDvd j a) = (\<not> (real j rdvd (real (?c * i) + (?N ?r))))" 
  4.2048 +      using Ia by (simp add: Let_def split_def)
  4.2049 +    also have "\<dots> = (\<not> (real (abs j) rdvd real (?c*i) + (?N ?r)))" 
  4.2050 +      by (simp only: rdvd_abs1[where d="j" and t="real (?c*i) + ?N ?r", symmetric]) simp
  4.2051 +    also have "\<dots> = (\<not> ((abs j) dvd (floor ((?N ?r) + real (?c*i))) \<and> 
  4.2052 +       (real (floor ((?N ?r) + real (?c*i))) = (real (?c*i) + (?N ?r)))))" 
  4.2053 +      by(simp only: int_rdvd_real[where i="abs j" and x="real (?c*i) + (?N ?r)"]) (simp only: add_ac)
  4.2054 +    also have "\<dots> = (?I (?l (NDvd j a)))" using cn cnz jnz
  4.2055 +      using rdvd_minus [where d="abs j" and t="real (?c*i + floor (?N ?r))", simplified, symmetric]
  4.2056 +      by (simp add: Let_def split_def int_rdvd_iff[symmetric]  
  4.2057 +	del: real_of_int_mult) (auto simp add: add_ac)
  4.2058 +    finally have ?case using l jnz by blast }
  4.2059 +  ultimately show ?case by blast
  4.2060 +qed auto
  4.2061 +
  4.2062 +text{* plusinf : Virtual substitution of @{text "+\<infinity>"}
  4.2063 +       minusinf: Virtual substitution of @{text "-\<infinity>"}
  4.2064 +       @{text "\<delta>"} Compute lcm @{text "d| Dvd d  c*x+t \<in> p"}
  4.2065 +       @{text "d\<delta>"} checks if a given l divides all the ds above*}
  4.2066 +
  4.2067 +consts 
  4.2068 +  plusinf:: "fm \<Rightarrow> fm" 
  4.2069 +  minusinf:: "fm \<Rightarrow> fm"
  4.2070 +  \<delta> :: "fm \<Rightarrow> int" 
  4.2071 +  d\<delta> :: "fm \<Rightarrow> int \<Rightarrow> bool"
  4.2072 +
  4.2073 +recdef minusinf "measure size"
  4.2074 +  "minusinf (And p q) = conj (minusinf p) (minusinf q)" 
  4.2075 +  "minusinf (Or p q) = disj (minusinf p) (minusinf q)" 
  4.2076 +  "minusinf (Eq  (CN 0 c e)) = F"
  4.2077 +  "minusinf (NEq (CN 0 c e)) = T"
  4.2078 +  "minusinf (Lt  (CN 0 c e)) = T"
  4.2079 +  "minusinf (Le  (CN 0 c e)) = T"
  4.2080 +  "minusinf (Gt  (CN 0 c e)) = F"
  4.2081 +  "minusinf (Ge  (CN 0 c e)) = F"
  4.2082 +  "minusinf p = p"
  4.2083 +
  4.2084 +lemma minusinf_qfree: "qfree p \<Longrightarrow> qfree (minusinf p)"
  4.2085 +  by (induct p rule: minusinf.induct, auto)
  4.2086 +
  4.2087 +recdef plusinf "measure size"
  4.2088 +  "plusinf (And p q) = conj (plusinf p) (plusinf q)" 
  4.2089 +  "plusinf (Or p q) = disj (plusinf p) (plusinf q)" 
  4.2090 +  "plusinf (Eq  (CN 0 c e)) = F"
  4.2091 +  "plusinf (NEq (CN 0 c e)) = T"
  4.2092 +  "plusinf (Lt  (CN 0 c e)) = F"
  4.2093 +  "plusinf (Le  (CN 0 c e)) = F"
  4.2094 +  "plusinf (Gt  (CN 0 c e)) = T"
  4.2095 +  "plusinf (Ge  (CN 0 c e)) = T"
  4.2096 +  "plusinf p = p"
  4.2097 +
  4.2098 +recdef \<delta> "measure size"
  4.2099 +  "\<delta> (And p q) = zlcm (\<delta> p) (\<delta> q)" 
  4.2100 +  "\<delta> (Or p q) = zlcm (\<delta> p) (\<delta> q)" 
  4.2101 +  "\<delta> (Dvd i (CN 0 c e)) = i"
  4.2102 +  "\<delta> (NDvd i (CN 0 c e)) = i"
  4.2103 +  "\<delta> p = 1"
  4.2104 +
  4.2105 +recdef d\<delta> "measure size"
  4.2106 +  "d\<delta> (And p q) = (\<lambda> d. d\<delta> p d \<and> d\<delta> q d)" 
  4.2107 +  "d\<delta> (Or p q) = (\<lambda> d. d\<delta> p d \<and> d\<delta> q d)" 
  4.2108 +  "d\<delta> (Dvd i (CN 0 c e)) = (\<lambda> d. i dvd d)"
  4.2109 +  "d\<delta> (NDvd i (CN 0 c e)) = (\<lambda> d. i dvd d)"
  4.2110 +  "d\<delta> p = (\<lambda> d. True)"
  4.2111 +
  4.2112 +lemma delta_mono: 
  4.2113 +  assumes lin: "iszlfm p bs"
  4.2114 +  and d: "d dvd d'"
  4.2115 +  and ad: "d\<delta> p d"
  4.2116 +  shows "d\<delta> p d'"
  4.2117 +  using lin ad d
  4.2118 +proof(induct p rule: iszlfm.induct)
  4.2119 +  case (9 i c e)  thus ?case using d
  4.2120 +    by (simp add: zdvd_trans[where m="i" and n="d" and k="d'"])
  4.2121 +next
  4.2122 +  case (10 i c e) thus ?case using d
  4.2123 +    by (simp add: zdvd_trans[where m="i" and n="d" and k="d'"])
  4.2124 +qed simp_all
  4.2125 +
  4.2126 +lemma \<delta> : assumes lin:"iszlfm p bs"
  4.2127 +  shows "d\<delta> p (\<delta> p) \<and> \<delta> p >0"
  4.2128 +using lin
  4.2129 +proof (induct p rule: iszlfm.induct)
  4.2130 +  case (1 p q) 
  4.2131 +  let ?d = "\<delta> (And p q)"
  4.2132 +  from prems zlcm_pos have dp: "?d >0" by simp
  4.2133 +  have d1: "\<delta> p dvd \<delta> (And p q)" using prems by simp 
  4.2134 +   hence th: "d\<delta> p ?d" 
  4.2135 +     using delta_mono prems by (auto simp del: dvd_zlcm_self1)
  4.2136 +  have "\<delta> q dvd \<delta> (And p q)" using prems  by simp 
  4.2137 +  hence th': "d\<delta> q ?d" using delta_mono prems by (auto simp del: dvd_zlcm_self2)
  4.2138 +  from th th' dp show ?case by simp 
  4.2139 +next
  4.2140 +  case (2 p q)  
  4.2141 +  let ?d = "\<delta> (And p q)"
  4.2142 +  from prems zlcm_pos have dp: "?d >0" by simp
  4.2143 +  have "\<delta> p dvd \<delta> (And p q)" using prems by simp hence th: "d\<delta> p ?d" using delta_mono prems 
  4.2144 +    by (auto simp del: dvd_zlcm_self1)
  4.2145 +  have "\<delta> q dvd \<delta> (And p q)" using prems by simp hence th': "d\<delta> q ?d" using delta_mono prems by (auto simp del: dvd_zlcm_self2)
  4.2146 +  from th th' dp show ?case by simp 
  4.2147 +qed simp_all
  4.2148 +
  4.2149 +
  4.2150 +lemma minusinf_inf:
  4.2151 +  assumes linp: "iszlfm p (a # bs)"
  4.2152 +  shows "\<exists> (z::int). \<forall> x < z. Ifm ((real x)#bs) (minusinf p) = Ifm ((real x)#bs) p"
  4.2153 +  (is "?P p" is "\<exists> (z::int). \<forall> x < z. ?I x (?M p) = ?I x p")
  4.2154 +using linp
  4.2155 +proof (induct p rule: minusinf.induct)
  4.2156 +  case (1 f g)
  4.2157 +  from prems have "?P f" by simp
  4.2158 +  then obtain z1 where z1_def: "\<forall> x < z1. ?I x (?M f) = ?I x f" by blast
  4.2159 +  from prems have "?P g" by simp
  4.2160 +  then obtain z2 where z2_def: "\<forall> x < z2. ?I x (?M g) = ?I x g" by blast
  4.2161 +  let ?z = "min z1 z2"
  4.2162 +  from z1_def z2_def have "\<forall> x < ?z. ?I x (?M (And f g)) = ?I x (And f g)" by simp
  4.2163 +  thus ?case by blast
  4.2164 +next
  4.2165 +  case (2 f g)   from prems have "?P f" by simp
  4.2166 +  then obtain z1 where z1_def: "\<forall> x < z1. ?I x (?M f) = ?I x f" by blast
  4.2167 +  from prems have "?P g" by simp
  4.2168 +  then obtain z2 where z2_def: "\<forall> x < z2. ?I x (?M g) = ?I x g" by blast
  4.2169 +  let ?z = "min z1 z2"
  4.2170 +  from z1_def z2_def have "\<forall> x < ?z. ?I x (?M (Or f g)) = ?I x (Or f g)" by simp
  4.2171 +  thus ?case by blast
  4.2172 +next
  4.2173 +  case (3 c e) 
  4.2174 +  from prems have "c > 0" by simp hence rcpos: "real c > 0" by simp
  4.2175 +  from prems have nbe: "numbound0 e" by simp
  4.2176 +  fix y
  4.2177 +  have "\<forall> x < (floor (- (Inum (y#bs) e) / (real c))). ?I x (?M (Eq (CN 0 c e))) = ?I x (Eq (CN 0 c e))"
  4.2178 +  proof (simp add: less_floor_eq , rule allI, rule impI) 
  4.2179 +    fix x
  4.2180 +    assume A: "real x + (1\<Colon>real) \<le> - (Inum (y # bs) e / real c)"
  4.2181 +    hence th1:"real x < - (Inum (y # bs) e / real c)" by simp
  4.2182 +    with rcpos  have "(real c)*(real  x) < (real c)*(- (Inum (y # bs) e / real c))"
  4.2183 +      by (simp only:  real_mult_less_mono2[OF rcpos th1])
  4.2184 +    hence "real c * real x + Inum (y # bs) e \<noteq> 0"using rcpos  by simp
  4.2185 +    thus "real c * real x + Inum (real x # bs) e \<noteq> 0" 
  4.2186 +      using numbound0_I[OF nbe, where b="y" and bs="bs" and b'="real x"]  by simp
  4.2187 +  qed
  4.2188 +  thus ?case by blast
  4.2189 +next
  4.2190 +  case (4 c e) 
  4.2191 +  from prems have "c > 0" by simp hence rcpos: "real c > 0" by simp
  4.2192 +  from prems have nbe: "numbound0 e" by simp
  4.2193 +  fix y
  4.2194 +  have "\<forall> x < (floor (- (Inum (y#bs) e) / (real c))). ?I x (?M (NEq (CN 0 c e))) = ?I x (NEq (CN 0 c e))"
  4.2195 +  proof (simp add: less_floor_eq , rule allI, rule impI) 
  4.2196 +    fix x
  4.2197 +    assume A: "real x + (1\<Colon>real) \<le> - (Inum (y # bs) e / real c)"
  4.2198 +    hence th1:"real x < - (Inum (y # bs) e / real c)" by simp
  4.2199 +    with rcpos  have "(real c)*(real  x) < (real c)*(- (Inum (y # bs) e / real c))"
  4.2200 +      by (simp only:  real_mult_less_mono2[OF rcpos th1])
  4.2201 +    hence "real c * real x + Inum (y # bs) e \<noteq> 0"using rcpos  by simp
  4.2202 +    thus "real c * real x + Inum (real x # bs) e \<noteq> 0" 
  4.2203 +      using numbound0_I[OF nbe, where b="y" and bs="bs" and b'="real x"]  by simp
  4.2204 +  qed
  4.2205 +  thus ?case by blast
  4.2206 +next
  4.2207 +  case (5 c e) 
  4.2208 +  from prems have "c > 0" by simp hence rcpos: "real c > 0" by simp
  4.2209 +  from prems have nbe: "numbound0 e" by simp
  4.2210 +  fix y
  4.2211 +  have "\<forall> x < (floor (- (Inum (y#bs) e) / (real c))). ?I x (?M (Lt (CN 0 c e))) = ?I x (Lt (CN 0 c e))"
  4.2212 +  proof (simp add: less_floor_eq , rule allI, rule impI) 
  4.2213 +    fix x
  4.2214 +    assume A: "real x + (1\<Colon>real) \<le> - (Inum (y # bs) e / real c)"
  4.2215 +    hence th1:"real x < - (Inum (y # bs) e / real c)" by simp
  4.2216 +    with rcpos  have "(real c)*(real  x) < (real c)*(- (Inum (y # bs) e / real c))"
  4.2217 +      by (simp only:  real_mult_less_mono2[OF rcpos th1])
  4.2218 +    thus "real c * real x + Inum (real x # bs) e < 0" 
  4.2219 +      using numbound0_I[OF nbe, where b="y" and bs="bs" and b'="real x"] rcpos by simp
  4.2220 +  qed
  4.2221 +  thus ?case by blast
  4.2222 +next
  4.2223 +  case (6 c e) 
  4.2224 +  from prems have "c > 0" by simp hence rcpos: "real c > 0" by simp
  4.2225 +  from prems have nbe: "numbound0 e" by simp
  4.2226 +  fix y
  4.2227 +  have "\<forall> x < (floor (- (Inum (y#bs) e) / (real c))). ?I x (?M (Le (CN 0 c e))) = ?I x (Le (CN 0 c e))"
  4.2228 +  proof (simp add: less_floor_eq , rule allI, rule impI) 
  4.2229 +    fix x
  4.2230 +    assume A: "real x + (1\<Colon>real) \<le> - (Inum (y # bs) e / real c)"
  4.2231 +    hence th1:"real x < - (Inum (y # bs) e / real c)" by simp
  4.2232 +    with rcpos  have "(real c)*(real  x) < (real c)*(- (Inum (y # bs) e / real c))"
  4.2233 +      by (simp only:  real_mult_less_mono2[OF rcpos th1])
  4.2234 +    thus "real c * real x + Inum (real x # bs) e \<le> 0" 
  4.2235 +      using numbound0_I[OF nbe, where b="y" and bs="bs" and b'="real x"] rcpos by simp
  4.2236 +  qed
  4.2237 +  thus ?case by blast
  4.2238 +next
  4.2239 +  case (7 c e) 
  4.2240 +  from prems have "c > 0" by simp hence rcpos: "real c > 0" by simp
  4.2241 +  from prems have nbe: "numbound0 e" by simp
  4.2242 +  fix y
  4.2243 +  have "\<forall> x < (floor (- (Inum (y#bs) e) / (real c))). ?I x (?M (Gt (CN 0 c e))) = ?I x (Gt (CN 0 c e))"
  4.2244 +  proof (simp add: less_floor_eq , rule allI, rule impI) 
  4.2245 +    fix x
  4.2246 +    assume A: "real x + (1\<Colon>real) \<le> - (Inum (y # bs) e / real c)"
  4.2247 +    hence th1:"real x < - (Inum (y # bs) e / real c)" by simp
  4.2248 +    with rcpos  have "(real c)*(real  x) < (real c)*(- (Inum (y # bs) e / real c))"
  4.2249 +      by (simp only:  real_mult_less_mono2[OF rcpos th1])
  4.2250 +    thus "\<not> (real c * real x + Inum (real x # bs) e>0)" 
  4.2251 +      using numbound0_I[OF nbe, where b="y" and bs="bs" and b'="real x"] rcpos by simp
  4.2252 +  qed
  4.2253 +  thus ?case by blast
  4.2254 +next
  4.2255 +  case (8 c e) 
  4.2256 +  from prems have "c > 0" by simp hence rcpos: "real c > 0" by simp
  4.2257 +  from prems have nbe: "numbound0 e" by simp
  4.2258 +  fix y
  4.2259 +  have "\<forall> x < (floor (- (Inum (y#bs) e) / (real c))). ?I x (?M (Ge (CN 0 c e))) = ?I x (Ge (CN 0 c e))"
  4.2260 +  proof (simp add: less_floor_eq , rule allI, rule impI) 
  4.2261 +    fix x
  4.2262 +    assume A: "real x + (1\<Colon>real) \<le> - (Inum (y # bs) e / real c)"
  4.2263 +    hence th1:"real x < - (Inum (y # bs) e / real c)" by simp
  4.2264 +    with rcpos  have "(real c)*(real  x) < (real c)*(- (Inum (y # bs) e / real c))"
  4.2265 +      by (simp only:  real_mult_less_mono2[OF rcpos th1])
  4.2266 +    thus "\<not> real c * real x + Inum (real x # bs) e \<ge> 0" 
  4.2267 +      using numbound0_I[OF nbe, where b="y" and bs="bs" and b'="real x"] rcpos by simp
  4.2268 +  qed
  4.2269 +  thus ?case by blast
  4.2270 +qed simp_all
  4.2271 +
  4.2272 +lemma minusinf_repeats:
  4.2273 +  assumes d: "d\<delta> p d" and linp: "iszlfm p (a # bs)"
  4.2274 +  shows "Ifm ((real(x - k*d))#bs) (minusinf p) = Ifm (real x #bs) (minusinf p)"
  4.2275 +using linp d
  4.2276 +proof(induct p rule: iszlfm.induct) 
  4.2277 +  case (9 i c e) hence nbe: "numbound0 e"  and id: "i dvd d" by simp+
  4.2278 +    hence "\<exists> k. d=i*k" by (simp add: dvd_def)
  4.2279 +    then obtain "di" where di_def: "d=i*di" by blast
  4.2280 +    show ?case 
  4.2281 +    proof(simp add: numbound0_I[OF nbe,where bs="bs" and b="real x - real k * real d" and b'="real x"] right_diff_distrib, rule iffI)
  4.2282 +      assume 
  4.2283 +	"real i rdvd real c * real x - real c * (real k * real d) + Inum (real x # bs) e"
  4.2284 +      (is "?ri rdvd ?rc*?rx - ?rc*(?rk*?rd) + ?I x e" is "?ri rdvd ?rt")
  4.2285 +      hence "\<exists> (l::int). ?rt = ?ri * (real l)" by (simp add: rdvd_def)
  4.2286 +      hence "\<exists> (l::int). ?rc*?rx+ ?I x e = ?ri*(real l)+?rc*(?rk * (real i) * (real di))" 
  4.2287 +	by (simp add: algebra_simps di_def)
  4.2288 +      hence "\<exists> (l::int). ?rc*?rx+ ?I x e = ?ri*(real (l + c*k*di))"
  4.2289 +	by (simp add: algebra_simps)
  4.2290 +      hence "\<exists> (l::int). ?rc*?rx+ ?I x e = ?ri* (real l)" by blast
  4.2291 +      thus "real i rdvd real c * real x + Inum (real x # bs) e" using rdvd_def by simp
  4.2292 +    next
  4.2293 +      assume 
  4.2294 +	"real i rdvd real c * real x + Inum (real x # bs) e" (is "?ri rdvd ?rc*?rx+?e")
  4.2295 +      hence "\<exists> (l::int). ?rc*?rx+?e = ?ri * (real l)" by (simp add: rdvd_def)
  4.2296 +      hence "\<exists> (l::int). ?rc*?rx - real c * (real k * real d) +?e = ?ri * (real l) - real c * (real k * real d)" by simp
  4.2297 +      hence "\<exists> (l::int). ?rc*?rx - real c * (real k * real d) +?e = ?ri * (real l) - real c * (real k * real i * real di)" by (simp add: di_def)
  4.2298 +      hence "\<exists> (l::int). ?rc*?rx - real c * (real k * real d) +?e = ?ri * (real (l - c*k*di))" by (simp add: algebra_simps)
  4.2299 +      hence "\<exists> (l::int). ?rc*?rx - real c * (real k * real d) +?e = ?ri * (real l)"
  4.2300 +	by blast
  4.2301 +      thus "real i rdvd real c * real x - real c * (real k * real d) + Inum (real x # bs) e" using rdvd_def by simp
  4.2302 +    qed
  4.2303 +next
  4.2304 +  case (10 i c e) hence nbe: "numbound0 e"  and id: "i dvd d" by simp+
  4.2305 +    hence "\<exists> k. d=i*k" by (simp add: dvd_def)
  4.2306 +    then obtain "di" where di_def: "d=i*di" by blast
  4.2307 +    show ?case 
  4.2308 +    proof(simp add: numbound0_I[OF nbe,where bs="bs" and b="real x - real k * real d" and b'="real x"] right_diff_distrib, rule iffI)
  4.2309 +      assume 
  4.2310 +	"real i rdvd real c * real x - real c * (real k * real d) + Inum (real x # bs) e"
  4.2311 +      (is "?ri rdvd ?rc*?rx - ?rc*(?rk*?rd) + ?I x e" is "?ri rdvd ?rt")
  4.2312 +      hence "\<exists> (l::int). ?rt = ?ri * (real l)" by (simp add: rdvd_def)
  4.2313 +      hence "\<exists> (l::int). ?rc*?rx+ ?I x e = ?ri*(real l)+?rc*(?rk * (real i) * (real di))" 
  4.2314 +	by (simp add: algebra_simps di_def)
  4.2315 +      hence "\<exists> (l::int). ?rc*?rx+ ?I x e = ?ri*(real (l + c*k*di))"
  4.2316 +	by (simp add: algebra_simps)
  4.2317 +      hence "\<exists> (l::int). ?rc*?rx+ ?I x e = ?ri* (real l)" by blast
  4.2318 +      thus "real i rdvd real c * real x + Inum (real x # bs) e" using rdvd_def by simp
  4.2319 +    next
  4.2320 +      assume 
  4.2321 +	"real i rdvd real c * real x + Inum (real x # bs) e" (is "?ri rdvd ?rc*?rx+?e")
  4.2322 +      hence "\<exists> (l::int). ?rc*?rx+?e = ?ri * (real l)" by (simp add: rdvd_def)
  4.2323 +      hence "\<exists> (l::int). ?rc*?rx - real c * (real k * real d) +?e = ?ri * (real l) - real c * (real k * real d)" by simp
  4.2324 +      hence "\<exists> (l::int). ?rc*?rx - real c * (real k * real d) +?e = ?ri * (real l) - real c * (real k * real i * real di)" by (simp add: di_def)
  4.2325 +      hence "\<exists> (l::int). ?rc*?rx - real c * (real k * real d) +?e = ?ri * (real (l - c*k*di))" by (simp add: algebra_simps)
  4.2326 +      hence "\<exists> (l::int). ?rc*?rx - real c * (real k * real d) +?e = ?ri * (real l)"
  4.2327 +	by blast
  4.2328 +      thus "real i rdvd real c * real x - real c * (real k * real d) + Inum (real x # bs) e" using rdvd_def by simp
  4.2329 +    qed
  4.2330 +qed (auto simp add: nth_pos2 numbound0_I[where bs="bs" and b="real(x - k*d)" and b'="real x"] simp del: real_of_int_mult real_of_int_diff)
  4.2331 +
  4.2332 +lemma minusinf_ex:
  4.2333 +  assumes lin: "iszlfm p (real (a::int) #bs)"
  4.2334 +  and exmi: "\<exists> (x::int). Ifm (real x#bs) (minusinf p)" (is "\<exists> x. ?P1 x")
  4.2335 +  shows "\<exists> (x::int). Ifm (real x#bs) p" (is "\<exists> x. ?P x")
  4.2336 +proof-
  4.2337 +  let ?d = "\<delta> p"
  4.2338 +  from \<delta> [OF lin] have dpos: "?d >0" by simp
  4.2339 +  from \<delta> [OF lin] have alld: "d\<delta> p ?d" by simp
  4.2340 +  from minusinf_repeats[OF alld lin] have th1:"\<forall> x k. ?P1 x = ?P1 (x - (k * ?d))" by simp
  4.2341 +  from minusinf_inf[OF lin] have th2:"\<exists> z. \<forall> x. x<z \<longrightarrow> (?P x = ?P1 x)" by blast
  4.2342 +  from minusinfinity [OF dpos th1 th2] exmi show ?thesis by blast
  4.2343 +qed
  4.2344 +
  4.2345 +lemma minusinf_bex:
  4.2346 +  assumes lin: "iszlfm p (real (a::int) #bs)"
  4.2347 +  shows "(\<exists> (x::int). Ifm (real x#bs) (minusinf p)) = 
  4.2348 +         (\<exists> (x::int)\<in> {1..\<delta> p}. Ifm (real x#bs) (minusinf p))"
  4.2349 +  (is "(\<exists> x. ?P x) = _")
  4.2350 +proof-
  4.2351 +  let ?d = "\<delta> p"
  4.2352 +  from \<delta> [OF lin] have dpos: "?d >0" by simp
  4.2353 +  from \<delta> [OF lin] have alld: "d\<delta> p ?d" by simp
  4.2354 +  from minusinf_repeats[OF alld lin] have th1:"\<forall> x k. ?P x = ?P (x - (k * ?d))" by simp
  4.2355 +  from periodic_finite_ex[OF dpos th1] show ?thesis by blast
  4.2356 +qed
  4.2357 +
  4.2358 +lemma dvd1_eq1: "x >0 \<Longrightarrow> (x::int) dvd 1 = (x = 1)" by auto
  4.2359 +
  4.2360 +consts 
  4.2361 +  a\<beta> :: "fm \<Rightarrow> int \<Rightarrow> fm" (* adjusts the coeffitients of a formula *)
  4.2362 +  d\<beta> :: "fm \<Rightarrow> int \<Rightarrow> bool" (* tests if all coeffs c of c divide a given l*)
  4.2363 +  \<zeta>  :: "fm \<Rightarrow> int" (* computes the lcm of all coefficients of x*)
  4.2364 +  \<beta> :: "fm \<Rightarrow> num list"
  4.2365 +  \<alpha> :: "fm \<Rightarrow> num list"
  4.2366 +
  4.2367 +recdef a\<beta> "measure size"
  4.2368 +  "a\<beta> (And p q) = (\<lambda> k. And (a\<beta> p k) (a\<beta> q k))" 
  4.2369 +  "a\<beta> (Or p q) = (\<lambda> k. Or (a\<beta> p k) (a\<beta> q k))" 
  4.2370 +  "a\<beta> (Eq  (CN 0 c e)) = (\<lambda> k. Eq (CN 0 1 (Mul (k div c) e)))"
  4.2371 +  "a\<beta> (NEq (CN 0 c e)) = (\<lambda> k. NEq (CN 0 1 (Mul (k div c) e)))"
  4.2372 +  "a\<beta> (Lt  (CN 0 c e)) = (\<lambda> k. Lt (CN 0 1 (Mul (k div c) e)))"
  4.2373 +  "a\<beta> (Le  (CN 0 c e)) = (\<lambda> k. Le (CN 0 1 (Mul (k div c) e)))"
  4.2374 +  "a\<beta> (Gt  (CN 0 c e)) = (\<lambda> k. Gt (CN 0 1 (Mul (k div c) e)))"
  4.2375 +  "a\<beta> (Ge  (CN 0 c e)) = (\<lambda> k. Ge (CN 0 1 (Mul (k div c) e)))"
  4.2376 +  "a\<beta> (Dvd i (CN 0 c e)) =(\<lambda> k. Dvd ((k div c)*i) (CN 0 1 (Mul (k div c) e)))"
  4.2377 +  "a\<beta> (NDvd i (CN 0 c e))=(\<lambda> k. NDvd ((k div c)*i) (CN 0 1 (Mul (k div c) e)))"
  4.2378 +  "a\<beta> p = (\<lambda> k. p)"
  4.2379 +
  4.2380 +recdef d\<beta> "measure size"
  4.2381 +  "d\<beta> (And p q) = (\<lambda> k. (d\<beta> p k) \<and> (d\<beta> q k))" 
  4.2382 +  "d\<beta> (Or p q) = (\<lambda> k. (d\<beta> p k) \<and> (d\<beta> q k))" 
  4.2383 +  "d\<beta> (Eq  (CN 0 c e)) = (\<lambda> k. c dvd k)"
  4.2384 +  "d\<beta> (NEq (CN 0 c e)) = (\<lambda> k. c dvd k)"
  4.2385 +  "d\<beta> (Lt  (CN 0 c e)) = (\<lambda> k. c dvd k)"
  4.2386 +  "d\<beta> (Le  (CN 0 c e)) = (\<lambda> k. c dvd k)"
  4.2387 +  "d\<beta> (Gt  (CN 0 c e)) = (\<lambda> k. c dvd k)"
  4.2388 +  "d\<beta> (Ge  (CN 0 c e)) = (\<lambda> k. c dvd k)"
  4.2389 +  "d\<beta> (Dvd i (CN 0 c e)) =(\<lambda> k. c dvd k)"
  4.2390 +  "d\<beta> (NDvd i (CN 0 c e))=(\<lambda> k. c dvd k)"
  4.2391 +  "d\<beta> p = (\<lambda> k. True)"
  4.2392 +
  4.2393 +recdef \<zeta> "measure size"
  4.2394 +  "\<zeta> (And p q) = zlcm (\<zeta> p) (\<zeta> q)" 
  4.2395 +  "\<zeta> (Or p q) = zlcm (\<zeta> p) (\<zeta> q)" 
  4.2396 +  "\<zeta> (Eq  (CN 0 c e)) = c"
  4.2397 +  "\<zeta> (NEq (CN 0 c e)) = c"
  4.2398 +  "\<zeta> (Lt  (CN 0 c e)) = c"
  4.2399 +  "\<zeta> (Le  (CN 0 c e)) = c"
  4.2400 +  "\<zeta> (Gt  (CN 0 c e)) = c"
  4.2401 +  "\<zeta> (Ge  (CN 0 c e)) = c"
  4.2402 +  "\<zeta> (Dvd i (CN 0 c e)) = c"
  4.2403 +  "\<zeta> (NDvd i (CN 0 c e))= c"
  4.2404 +  "\<zeta> p = 1"
  4.2405 +
  4.2406 +recdef \<beta> "measure size"
  4.2407 +  "\<beta> (And p q) = (\<beta> p @ \<beta> q)" 
  4.2408 +  "\<beta> (Or p q) = (\<beta> p @ \<beta> q)" 
  4.2409 +  "\<beta> (Eq  (CN 0 c e)) = [Sub (C -1) e]"
  4.2410 +  "\<beta> (NEq (CN 0 c e)) = [Neg e]"
  4.2411 +  "\<beta> (Lt  (CN 0 c e)) = []"
  4.2412 +  "\<beta> (Le  (CN 0 c e)) = []"
  4.2413 +  "\<beta> (Gt  (CN 0 c e)) = [Neg e]"
  4.2414 +  "\<beta> (Ge  (CN 0 c e)) = [Sub (C -1) e]"
  4.2415 +  "\<beta> p = []"
  4.2416 +
  4.2417 +recdef \<alpha> "measure size"
  4.2418 +  "\<alpha> (And p q) = (\<alpha> p @ \<alpha> q)" 
  4.2419 +  "\<alpha> (Or p q) = (\<alpha> p @ \<alpha> q)" 
  4.2420 +  "\<alpha> (Eq  (CN 0 c e)) = [Add (C -1) e]"
  4.2421 +  "\<alpha> (NEq (CN 0 c e)) = [e]"
  4.2422 +  "\<alpha> (Lt  (CN 0 c e)) = [e]"
  4.2423 +  "\<alpha> (Le  (CN 0 c e)) = [Add (C -1) e]"
  4.2424 +  "\<alpha> (Gt  (CN 0 c e)) = []"
  4.2425 +  "\<alpha> (Ge  (CN 0 c e)) = []"
  4.2426 +  "\<alpha> p = []"
  4.2427 +consts mirror :: "fm \<Rightarrow> fm"
  4.2428 +recdef mirror "measure size"
  4.2429 +  "mirror (And p q) = And (mirror p) (mirror q)" 
  4.2430 +  "mirror (Or p q) = Or (mirror p) (mirror q)" 
  4.2431 +  "mirror (Eq  (CN 0 c e)) = Eq (CN 0 c (Neg e))"
  4.2432 +  "mirror (NEq (CN 0 c e)) = NEq (CN 0 c (Neg e))"
  4.2433 +  "mirror (Lt  (CN 0 c e)) = Gt (CN 0 c (Neg e))"
  4.2434 +  "mirror (Le  (CN 0 c e)) = Ge (CN 0 c (Neg e))"
  4.2435 +  "mirror (Gt  (CN 0 c e)) = Lt (CN 0 c (Neg e))"
  4.2436 +  "mirror (Ge  (CN 0 c e)) = Le (CN 0 c (Neg e))"
  4.2437 +  "mirror (Dvd i (CN 0 c e)) = Dvd i (CN 0 c (Neg e))"
  4.2438 +  "mirror (NDvd i (CN 0 c e)) = NDvd i (CN 0 c (Neg e))"
  4.2439 +  "mirror p = p"
  4.2440 +
  4.2441 +lemma mirror\<alpha>\<beta>:
  4.2442 +  assumes lp: "iszlfm p (a#bs)"
  4.2443 +  shows "(Inum (real (i::int)#bs)) ` set (\<alpha> p) = (Inum (real i#bs)) ` set (\<beta> (mirror p))"
  4.2444 +using lp
  4.2445 +by (induct p rule: mirror.induct, auto)
  4.2446 +
  4.2447 +lemma mirror: 
  4.2448 +  assumes lp: "iszlfm p (a#bs)"
  4.2449 +  shows "Ifm (real (x::int)#bs) (mirror p) = Ifm (real (- x)#bs) p" 
  4.2450 +using lp
  4.2451 +proof(induct p rule: iszlfm.induct)
  4.2452 +  case (9 j c e)
  4.2453 +  have th: "(real j rdvd real c * real x - Inum (real x # bs) e) =
  4.2454 +       (real j rdvd - (real c * real x - Inum (real x # bs) e))"
  4.2455 +    by (simp only: rdvd_minus[symmetric])
  4.2456 +  from prems th show  ?case
  4.2457 +    by (simp add: algebra_simps
  4.2458 +      numbound0_I[where bs="bs" and b'="real x" and b="- real x"])
  4.2459 +next
  4.2460 +    case (10 j c e)
  4.2461 +  have th: "(real j rdvd real c * real x - Inum (real x # bs) e) =
  4.2462 +       (real j rdvd - (real c * real x - Inum (real x # bs) e))"
  4.2463 +    by (simp only: rdvd_minus[symmetric])
  4.2464 +  from prems th show  ?case
  4.2465 +    by (simp add: algebra_simps
  4.2466 +      numbound0_I[where bs="bs" and b'="real x" and b="- real x"])
  4.2467 +qed (auto simp add: numbound0_I[where bs="bs" and b="real x" and b'="- real x"] nth_pos2)
  4.2468 +
  4.2469 +lemma mirror_l: "iszlfm p (a#bs) \<Longrightarrow> iszlfm (mirror p) (a#bs)"
  4.2470 +by (induct p rule: mirror.induct, auto simp add: isint_neg)
  4.2471 +
  4.2472 +lemma mirror_d\<beta>: "iszlfm p (a#bs) \<and> d\<beta> p 1 
  4.2473 +  \<Longrightarrow> iszlfm (mirror p) (a#bs) \<and> d\<beta> (mirror p) 1"
  4.2474 +by (induct p rule: mirror.induct, auto simp add: isint_neg)
  4.2475 +
  4.2476 +lemma mirror_\<delta>: "iszlfm p (a#bs) \<Longrightarrow> \<delta> (mirror p) = \<delta> p"
  4.2477 +by (induct p rule: mirror.induct,auto)
  4.2478 +
  4.2479 +
  4.2480 +lemma mirror_ex: 
  4.2481 +  assumes lp: "iszlfm p (real (i::int)#bs)"
  4.2482 +  shows "(\<exists> (x::int). Ifm (real x#bs) (mirror p)) = (\<exists> (x::int). Ifm (real x#bs) p)"
  4.2483 +  (is "(\<exists> x. ?I x ?mp) = (\<exists> x. ?I x p)")
  4.2484 +proof(auto)
  4.2485 +  fix x assume "?I x ?mp" hence "?I (- x) p" using mirror[OF lp] by blast
  4.2486 +  thus "\<exists> x. ?I x p" by blast
  4.2487 +next
  4.2488 +  fix x assume "?I x p" hence "?I (- x) ?mp" 
  4.2489 +    using mirror[OF lp, where x="- x", symmetric] by auto
  4.2490 +  thus "\<exists> x. ?I x ?mp" by blast
  4.2491 +qed
  4.2492 +
  4.2493 +lemma \<beta>_numbound0: assumes lp: "iszlfm p bs"
  4.2494 +  shows "\<forall> b\<in> set (\<beta> p). numbound0 b"
  4.2495 +  using lp by (induct p rule: \<beta>.induct,auto)
  4.2496 +
  4.2497 +lemma d\<beta>_mono: 
  4.2498 +  assumes linp: "iszlfm p (a #bs)"
  4.2499 +  and dr: "d\<beta> p l"
  4.2500 +  and d: "l dvd l'"
  4.2501 +  shows "d\<beta> p l'"
  4.2502 +using dr linp zdvd_trans[where n="l" and k="l'", simplified d]
  4.2503 +by (induct p rule: iszlfm.induct) simp_all
  4.2504 +
  4.2505 +lemma \<alpha>_l: assumes lp: "iszlfm p (a#bs)"
  4.2506 +  shows "\<forall> b\<in> set (\<alpha> p). numbound0 b \<and> isint b (a#bs)"
  4.2507 +using lp
  4.2508 +by(induct p rule: \<alpha>.induct, auto simp add: isint_add isint_c)
  4.2509 +
  4.2510 +lemma \<zeta>: 
  4.2511 +  assumes linp: "iszlfm p (a #bs)"
  4.2512 +  shows "\<zeta> p > 0 \<and> d\<beta> p (\<zeta> p)"
  4.2513 +using linp
  4.2514 +proof(induct p rule: iszlfm.induct)
  4.2515 +  case (1 p q)
  4.2516 +  from prems have dl1: "\<zeta> p dvd zlcm (\<zeta> p) (\<zeta> q)" by simp
  4.2517 +  from prems have dl2: "\<zeta> q dvd zlcm (\<zeta> p) (\<zeta> q)" by simp
  4.2518 +  from prems d\<beta>_mono[where p = "p" and l="\<zeta> p" and l'="zlcm (\<zeta> p) (\<zeta> q)"] 
  4.2519 +    d\<beta>_mono[where p = "q" and l="\<zeta> q" and l'="zlcm (\<zeta> p) (\<zeta> q)"] 
  4.2520 +    dl1 dl2 show ?case by (auto simp add: zlcm_pos)
  4.2521 +next
  4.2522 +  case (2 p q)
  4.2523 +  from prems have dl1: "\<zeta> p dvd zlcm (\<zeta> p) (\<zeta> q)" by simp
  4.2524 +  from prems have dl2: "\<zeta> q dvd zlcm (\<zeta> p) (\<zeta> q)" by simp
  4.2525 +  from prems d\<beta>_mono[where p = "p" and l="\<zeta> p" and l'="zlcm (\<zeta> p) (\<zeta> q)"] 
  4.2526 +    d\<beta>_mono[where p = "q" and l="\<zeta> q" and l'="zlcm (\<zeta> p) (\<zeta> q)"] 
  4.2527 +    dl1 dl2 show ?case by (auto simp add: zlcm_pos)
  4.2528 +qed (auto simp add: zlcm_pos)
  4.2529 +
  4.2530 +lemma a\<beta>: assumes linp: "iszlfm p (a #bs)" and d: "d\<beta> p l" and lp: "l > 0"
  4.2531 +  shows "iszlfm (a\<beta> p l) (a #bs) \<and> d\<beta> (a\<beta> p l) 1 \<and> (Ifm (real (l * x) #bs) (a\<beta> p l) = Ifm ((real x)#bs) p)"
  4.2532 +using linp d
  4.2533 +proof (induct p rule: iszlfm.induct)
  4.2534 +  case (5 c e) hence cp: "c>0" and be: "numbound0 e" and ei:"isint e (a#bs)" and d': "c dvd l" by simp+
  4.2535 +    from lp cp have clel: "c\<le>l" by (simp add: zdvd_imp_le [OF d' lp])
  4.2536 +    from cp have cnz: "c \<noteq> 0" by simp
  4.2537 +    have "c div c\<le> l div c"
  4.2538 +      by (simp add: zdiv_mono1[OF clel cp])
  4.2539 +    then have ldcp:"0 < l div c" 
  4.2540 +      by (simp add: zdiv_self[OF cnz])
  4.2541 +    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
  4.2542 +    hence cl:"c * (l div c) =l" using zmod_zdiv_equality[where a="l" and b="c", symmetric] 
  4.2543 +      by simp
  4.2544 +    hence "(real l * real x + real (l div c) * Inum (real x # bs) e < (0\<Colon>real)) =
  4.2545 +          (real (c * (l div c)) * real x + real (l div c) * Inum (real x # bs) e < 0)"
  4.2546 +      by simp
  4.2547 +    also have "\<dots> = (real (l div c) * (real c * real x + Inum (real x # bs) e) < (real (l div c)) * 0)" by (simp add: algebra_simps)
  4.2548 +    also have "\<dots> = (real c * real x + Inum (real x # bs) e < 0)"
  4.2549 +    using mult_less_0_iff [where a="real (l div c)" and b="real c * real x + Inum (real x # bs) e"] ldcp by simp
  4.2550 +  finally show ?case using numbound0_I[OF be,where b="real (l * x)" and b'="real x" and bs="bs"] be  isint_Mul[OF ei] by simp
  4.2551 +next
  4.2552 +  case (6 c e) hence cp: "c>0" and be: "numbound0 e" and ei:"isint e (a#bs)" and d': "c dvd l" by simp+
  4.2553 +    from lp cp have clel: "c\<le>l" by (simp add: zdvd_imp_le [OF d' lp])
  4.2554 +    from cp have cnz: "c \<noteq> 0" by simp
  4.2555 +    have "c div c\<le> l div c"
  4.2556 +      by (simp add: zdiv_mono1[OF clel cp])
  4.2557 +    then have ldcp:"0 < l div c" 
  4.2558 +      by (simp add: zdiv_self[OF cnz])
  4.2559 +    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
  4.2560 +    hence cl:"c * (l div c) =l" using zmod_zdiv_equality[where a="l" and b="c", symmetric] 
  4.2561 +      by simp
  4.2562 +    hence "(real l * real x + real (l div c) * Inum (real x # bs) e \<le> (0\<Colon>real)) =
  4.2563 +          (real (c * (l div c)) * real x + real (l div c) * Inum (real x # bs) e \<le> 0)"
  4.2564 +      by simp
  4.2565 +    also have "\<dots> = (real (l div c) * (real c * real x + Inum (real x # bs) e) \<le> (real (l div c)) * 0)" by (simp add: algebra_simps)
  4.2566 +    also have "\<dots> = (real c * real x + Inum (real x # bs) e \<le> 0)"
  4.2567 +    using mult_le_0_iff [where a="real (l div c)" and b="real c * real x + Inum (real x # bs) e"] ldcp by simp
  4.2568 +  finally show ?case using numbound0_I[OF be,where b="real (l * x)" and b'="real x" and bs="bs"]  be  isint_Mul[OF ei] by simp
  4.2569 +next
  4.2570 +  case (7 c e) hence cp: "c>0" and be: "numbound0 e" and ei:"isint e (a#bs)" and d': "c dvd l" by simp+
  4.2571 +    from lp cp have clel: "c\<le>l" by (simp add: zdvd_imp_le [OF d' lp])
  4.2572 +    from cp have cnz: "c \<noteq> 0" by simp
  4.2573 +    have "c div c\<le> l div c"
  4.2574 +      by (simp add: zdiv_mono1[OF clel cp])
  4.2575 +    then have ldcp:"0 < l div c" 
  4.2576 +      by (simp add: zdiv_self[OF cnz])
  4.2577 +    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
  4.2578 +    hence cl:"c * (l div c) =l" using zmod_zdiv_equality[where a="l" and b="c", symmetric] 
  4.2579 +      by simp
  4.2580 +    hence "(real l * real x + real (l div c) * Inum (real x # bs) e > (0\<Colon>real)) =
  4.2581 +          (real (c * (l div c)) * real x + real (l div c) * Inum (real x # bs) e > 0)"
  4.2582 +      by simp
  4.2583 +    also have "\<dots> = (real (l div c) * (real c * real x + Inum (real x # bs) e) > (real (l div c)) * 0)" by (simp add: algebra_simps)
  4.2584 +    also have "\<dots> = (real c * real x + Inum (real x # bs) e > 0)"
  4.2585 +    using zero_less_mult_iff [where a="real (l div c)" and b="real c * real x + Inum (real x # bs) e"] ldcp by simp
  4.2586 +  finally show ?case using numbound0_I[OF be,where b="real (l * x)" and b'="real x" and bs="bs"]  be  isint_Mul[OF ei] by simp
  4.2587 +next
  4.2588 +  case (8 c e) hence cp: "c>0" and be: "numbound0 e"  and ei:"isint e (a#bs)" and d': "c dvd l" by simp+
  4.2589 +    from lp cp have clel: "c\<le>l" by (simp add: zdvd_imp_le [OF d' lp])
  4.2590 +    from cp have cnz: "c \<noteq> 0" by simp
  4.2591 +    have "c div c\<le> l div c"
  4.2592 +      by (simp add: zdiv_mono1[OF clel cp])
  4.2593 +    then have ldcp:"0 < l div c" 
  4.2594 +      by (simp add: zdiv_self[OF cnz])
  4.2595 +    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
  4.2596 +    hence cl:"c * (l div c) =l" using zmod_zdiv_equality[where a="l" and b="c", symmetric] 
  4.2597 +      by simp
  4.2598 +    hence "(real l * real x + real (l div c) * Inum (real x # bs) e \<ge> (0\<Colon>real)) =
  4.2599 +          (real (c * (l div c)) * real x + real (l div c) * Inum (real x # bs) e \<ge> 0)"
  4.2600 +      by simp
  4.2601 +    also have "\<dots> = (real (l div c) * (real c * real x + Inum (real x # bs) e) \<ge> (real (l div c)) * 0)" by (simp add: algebra_simps)
  4.2602 +    also have "\<dots> = (real c * real x + Inum (real x # bs) e \<ge> 0)"
  4.2603 +    using zero_le_mult_iff [where a="real (l div c)" and b="real c * real x + Inum (real x # bs) e"] ldcp by simp
  4.2604 +  finally show ?case using numbound0_I[OF be,where b="real (l * x)" and b'="real x" and bs="bs"]  be  isint_Mul[OF ei] by simp
  4.2605 +next
  4.2606 +  case (3 c e) hence cp: "c>0" and be: "numbound0 e" and ei:"isint e (a#bs)" and d': "c dvd l" by simp+
  4.2607 +    from lp cp have clel: "c\<le>l" by (simp add: zdvd_imp_le [OF d' lp])
  4.2608 +    from cp have cnz: "c \<noteq> 0" by simp
  4.2609 +    have "c div c\<le> l div c"
  4.2610 +      by (simp add: zdiv_mono1[OF clel cp])
  4.2611 +    then have ldcp:"0 < l div c" 
  4.2612 +      by (simp add: zdiv_self[OF cnz])
  4.2613 +    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
  4.2614 +    hence cl:"c * (l div c) =l" using zmod_zdiv_equality[where a="l" and b="c", symmetric] 
  4.2615 +      by simp
  4.2616 +    hence "(real l * real x + real (l div c) * Inum (real x # bs) e = (0\<Colon>real)) =
  4.2617 +          (real (c * (l div c)) * real x + real (l div c) * Inum (real x # bs) e = 0)"
  4.2618 +      by simp
  4.2619 +    also have "\<dots> = (real (l div c) * (real c * real x + Inum (real x # bs) e) = (real (l div c)) * 0)" by (simp add: algebra_simps)
  4.2620 +    also have "\<dots> = (real c * real x + Inum (real x # bs) e = 0)"
  4.2621 +    using mult_eq_0_iff [where a="real (l div c)" and b="real c * real x + Inum (real x # bs) e"] ldcp by simp
  4.2622 +  finally show ?case using numbound0_I[OF be,where b="real (l * x)" and b'="real x" and bs="bs"]  be  isint_Mul[OF ei] by simp
  4.2623 +next
  4.2624 +  case (4 c e) hence cp: "c>0" and be: "numbound0 e" and ei:"isint e (a#bs)" and d': "c dvd l" by simp+
  4.2625 +    from lp cp have clel: "c\<le>l" by (simp add: zdvd_imp_le [OF d' lp])
  4.2626 +    from cp have cnz: "c \<noteq> 0" by simp
  4.2627 +    have "c div c\<le> l div c"
  4.2628 +      by (simp add: zdiv_mono1[OF clel cp])
  4.2629 +    then have ldcp:"0 < l div c" 
  4.2630 +      by (simp add: zdiv_self[OF cnz])
  4.2631 +    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
  4.2632 +    hence cl:"c * (l div c) =l" using zmod_zdiv_equality[where a="l" and b="c", symmetric] 
  4.2633 +      by simp
  4.2634 +    hence "(real l * real x + real (l div c) * Inum (real x # bs) e \<noteq> (0\<Colon>real)) =
  4.2635 +          (real (c * (l div c)) * real x + real (l div c) * Inum (real x # bs) e \<noteq> 0)"
  4.2636 +      by simp
  4.2637 +    also have "\<dots> = (real (l div c) * (real c * real x + Inum (real x # bs) e) \<noteq> (real (l div c)) * 0)" by (simp add: algebra_simps)
  4.2638 +    also have "\<dots> = (real c * real x + Inum (real x # bs) e \<noteq> 0)"
  4.2639 +    using zero_le_mult_iff [where a="real (l div c)" and b="real c * real x + Inum (real x # bs) e"] ldcp by simp
  4.2640 +  finally show ?case using numbound0_I[OF be,where b="real (l * x)" and b'="real x" and bs="bs"]  be  isint_Mul[OF ei] by simp
  4.2641 +next
  4.2642 +  case (9 j c e) hence cp: "c>0" and be: "numbound0 e" and ei:"isint e (a#bs)" and jp: "j > 0" and d': "c dvd l" by simp+
  4.2643 +    from lp cp have clel: "c\<le>l" by (simp add: zdvd_imp_le [OF d' lp])
  4.2644 +    from cp have cnz: "c \<noteq> 0" by simp
  4.2645 +    have "c div c\<le> l div c"
  4.2646 +      by (simp add: zdiv_mono1[OF clel cp])
  4.2647 +    then have ldcp:"0 < l div c" 
  4.2648 +      by (simp add: zdiv_self[OF cnz])
  4.2649 +    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
  4.2650 +    hence cl:"c * (l div c) =l" using zmod_zdiv_equality[where a="l" and b="c", symmetric] 
  4.2651 +      by simp
  4.2652 +    hence "(\<exists> (k::int). real l * real x + real (l div c) * Inum (real x # bs) e = (real (l div c) * real j) * real k) = (\<exists> (k::int). real (c * (l div c)) * real x + real (l div c) * Inum (real x # bs) e = (real (l div c) * real j) * real k)"  by simp
  4.2653 +    also have "\<dots> = (\<exists> (k::int). real (l div c) * (real c * real x + Inum (real x # bs) e - real j * real k) = real (l div c)*0)" by (simp add: algebra_simps)
  4.2654 +    also fix k have "\<dots> = (\<exists> (k::int). real c * real x + Inum (real x # bs) e - real j * real k = 0)"
  4.2655 +    using zero_le_mult_iff [where a="real (l div c)" and b="real c * real x + Inum (real x # bs) e - real j * real k"] ldcp by simp
  4.2656 +  also have "\<dots> = (\<exists> (k::int). real c * real x + Inum (real x # bs) e = real j * real k)" by simp
  4.2657 +  finally show ?case using numbound0_I[OF be,where b="real (l * x)" and b'="real x" and bs="bs"] rdvd_def  be  isint_Mul[OF ei] mult_strict_mono[OF ldcp jp ldcp ] by simp 
  4.2658 +next
  4.2659 +  case (10 j c e) hence cp: "c>0" and be: "numbound0 e" and ei:"isint e (a#bs)" and jp: "j > 0" and d': "c dvd l" by simp+
  4.2660 +    from lp cp have clel: "c\<le>l" by (simp add: zdvd_imp_le [OF d' lp])
  4.2661 +    from cp have cnz: "c \<noteq> 0" by simp
  4.2662 +    have "c div c\<le> l div c"
  4.2663 +      by (simp add: zdiv_mono1[OF clel cp])
  4.2664 +    then have ldcp:"0 < l div c" 
  4.2665 +      by (simp add: zdiv_self[OF cnz])
  4.2666 +    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
  4.2667 +    hence cl:"c * (l div c) =l" using zmod_zdiv_equality[where a="l" and b="c", symmetric] 
  4.2668 +      by simp
  4.2669 +    hence "(\<exists> (k::int). real l * real x + real (l div c) * Inum (real x # bs) e = (real (l div c) * real j) * real k) = (\<exists> (k::int). real (c * (l div c)) * real x + real (l div c) * Inum (real x # bs) e = (real (l div c) * real j) * real k)"  by simp
  4.2670 +    also have "\<dots> = (\<exists> (k::int). real (l div c) * (real c * real x + Inum (real x # bs) e - real j * real k) = real (l div c)*0)" by (simp add: algebra_simps)
  4.2671 +    also fix k have "\<dots> = (\<exists> (k::int). real c * real x + Inum (real x # bs) e - real j * real k = 0)"
  4.2672 +    using zero_le_mult_iff [where a="real (l div c)" and b="real c * real x + Inum (real x # bs) e - real j * real k"] ldcp by simp
  4.2673 +  also have "\<dots> = (\<exists> (k::int). real c * real x + Inum (real x # bs) e = real j * real k)" by simp
  4.2674 +  finally show ?case using numbound0_I[OF be,where b="real (l * x)" and b'="real x" and bs="bs"] rdvd_def  be  isint_Mul[OF ei]  mult_strict_mono[OF ldcp jp ldcp ] by simp
  4.2675 +qed (simp_all add: nth_pos2 numbound0_I[where bs="bs" and b="real (l * x)" and b'="real x"] isint_Mul del: real_of_int_mult)
  4.2676 +
  4.2677 +lemma a\<beta>_ex: assumes linp: "iszlfm p (a#bs)" and d: "d\<beta> p l" and lp: "l>0"
  4.2678 +  shows "(\<exists> x. l dvd x \<and> Ifm (real x #bs) (a\<beta> p l)) = (\<exists> (x::int). Ifm (real x#bs) p)"
  4.2679 +  (is "(\<exists> x. l dvd x \<and> ?P x) = (\<exists> x. ?P' x)")
  4.2680 +proof-
  4.2681 +  have "(\<exists> x. l dvd x \<and> ?P x) = (\<exists> (x::int). ?P (l*x))"
  4.2682 +    using unity_coeff_ex[where l="l" and P="?P", simplified] by simp
  4.2683 +  also have "\<dots> = (\<exists> (x::int). ?P' x)" using a\<beta>[OF linp d lp] by simp
  4.2684 +  finally show ?thesis  . 
  4.2685 +qed
  4.2686 +
  4.2687 +lemma \<beta>:
  4.2688 +  assumes lp: "iszlfm p (a#bs)"
  4.2689 +  and u: "d\<beta> p 1"
  4.2690 +  and d: "d\<delta> p d"
  4.2691 +  and dp: "d > 0"
  4.2692 +  and nob: "\<not>(\<exists>(j::int) \<in> {1 .. d}. \<exists> b\<in> (Inum (a#bs)) ` set(\<beta> p). real x = b + real j)"
  4.2693 +  and p: "Ifm (real x#bs) p" (is "?P x")
  4.2694 +  shows "?P (x - d)"
  4.2695 +using lp u d dp nob p
  4.2696 +proof(induct p rule: iszlfm.induct)
  4.2697 +  case (5 c e) hence c1: "c=1" and  bn:"numbound0 e" using dvd1_eq1[where x="c"] by simp+
  4.2698 +    with dp p c1 numbound0_I[OF bn,where b="real (x-d)" and b'="real x" and bs="bs"] prems
  4.2699 +    show ?case by (simp del: real_of_int_minus)
  4.2700 +next
  4.2701 +  case (6 c e)  hence c1: "c=1" and  bn:"numbound0 e" using dvd1_eq1[where x="c"] by simp+
  4.2702 +    with dp p c1 numbound0_I[OF bn,where b="real (x-d)" and b'="real x" and bs="bs"] prems
  4.2703 +    show ?case by (simp del: real_of_int_minus)
  4.2704 +next
  4.2705 +  case (7 c e) hence p: "Ifm (real x #bs) (Gt (CN 0 c e))" and c1: "c=1" and bn:"numbound0 e" and ie1:"isint e (a#bs)" using dvd1_eq1[where x="c"] by simp+
  4.2706 +    let ?e = "Inum (real x # bs) e"
  4.2707 +    from ie1 have ie: "real (floor ?e) = ?e" using isint_iff[where n="e" and bs="a#bs"]
  4.2708 +      numbound0_I[OF bn,where b="a" and b'="real x" and bs="bs"]
  4.2709 +      by (simp add: isint_iff)
  4.2710 +    {assume "real (x-d) +?e > 0" hence ?case using c1 
  4.2711 +      numbound0_I[OF bn,where b="real (x-d)" and b'="real x" and bs="bs"]
  4.2712 +	by (simp del: real_of_int_minus)}
  4.2713 +    moreover
  4.2714 +    {assume H: "\<not> real (x-d) + ?e > 0" 
  4.2715 +      let ?v="Neg e"
  4.2716 +      have vb: "?v \<in> set (\<beta> (Gt (CN 0 c e)))" by simp
  4.2717 +      from prems(11)[simplified simp_thms Inum.simps \<beta>.simps set.simps bex_simps numbound0_I[OF bn,where b="a" and b'="real x" and bs="bs"]] 
  4.2718 +      have nob: "\<not> (\<exists> j\<in> {1 ..d}. real x =  - ?e + real j)" by auto 
  4.2719 +      from H p have "real x + ?e > 0 \<and> real x + ?e \<le> real d" by (simp add: c1)
  4.2720 +      hence "real (x + floor ?e) > real (0::int) \<and> real (x + floor ?e) \<le> real d"
  4.2721 +	using ie by simp
  4.2722 +      hence "x + floor ?e \<ge> 1 \<and> x + floor ?e \<le> d"  by simp
  4.2723 +      hence "\<exists> (j::int) \<in> {1 .. d}. j = x + floor ?e" by simp
  4.2724 +      hence "\<exists> (j::int) \<in> {1 .. d}. real x = real (- floor ?e + j)" 
  4.2725 +	by (simp only: real_of_int_inject) (simp add: algebra_simps)
  4.2726 +      hence "\<exists> (j::int) \<in> {1 .. d}. real x = - ?e + real j" 
  4.2727 +	by (simp add: ie[simplified isint_iff])
  4.2728 +      with nob have ?case by auto}
  4.2729 +    ultimately show ?case by blast
  4.2730 +next
  4.2731 +  case (8 c e) hence p: "Ifm (real x #bs) (Ge (CN 0 c e))" and c1: "c=1" and bn:"numbound0 e" 
  4.2732 +    and ie1:"isint e (a #bs)" using dvd1_eq1[where x="c"] by simp+
  4.2733 +    let ?e = "Inum (real x # bs) e"
  4.2734 +    from ie1 have ie: "real (floor ?e) = ?e" using numbound0_I[OF bn,where b="real x" and b'="a" and bs="bs"] isint_iff[where n="e" and bs="(real x)#bs"]
  4.2735 +      by (simp add: isint_iff)
  4.2736 +    {assume "real (x-d) +?e \<ge> 0" hence ?case using  c1 
  4.2737 +      numbound0_I[OF bn,where b="real (x-d)" and b'="real x" and bs="bs"]
  4.2738 +	by (simp del: real_of_int_minus)}
  4.2739 +    moreover
  4.2740 +    {assume H: "\<not> real (x-d) + ?e \<ge> 0" 
  4.2741 +      let ?v="Sub (C -1) e"
  4.2742 +      have vb: "?v \<in> set (\<beta> (Ge (CN 0 c e)))" by simp
  4.2743 +      from prems(11)[simplified simp_thms Inum.simps \<beta>.simps set.simps bex_simps numbound0_I[OF bn,where b="a" and b'="real x" and bs="bs"]] 
  4.2744 +      have nob: "\<not> (\<exists> j\<in> {1 ..d}. real x =  - ?e - 1 + real j)" by auto 
  4.2745 +      from H p have "real x + ?e \<ge> 0 \<and> real x + ?e < real d" by (simp add: c1)
  4.2746 +      hence "real (x + floor ?e) \<ge> real (0::int) \<and> real (x + floor ?e) < real d"
  4.2747 +	using ie by simp
  4.2748 +      hence "x + floor ?e +1 \<ge> 1 \<and> x + floor ?e + 1 \<le> d"  by simp
  4.2749 +      hence "\<exists> (j::int) \<in> {1 .. d}. j = x + floor ?e + 1" by simp
  4.2750 +      hence "\<exists> (j::int) \<in> {1 .. d}. x= - floor ?e - 1 + j" by (simp add: algebra_simps)
  4.2751 +      hence "\<exists> (j::int) \<in> {1 .. d}. real x= real (- floor ?e - 1 + j)" 
  4.2752 +	by (simp only: real_of_int_inject)
  4.2753 +      hence "\<exists> (j::int) \<in> {1 .. d}. real x= - ?e - 1 + real j" 
  4.2754 +	by (simp add: ie[simplified isint_iff])
  4.2755 +      with nob have ?case by simp }
  4.2756 +    ultimately show ?case by blast
  4.2757 +next
  4.2758 +  case (3 c e) hence p: "Ifm (real x #bs) (Eq (CN 0 c e))" (is "?p x") and c1: "c=1" 
  4.2759 +    and bn:"numbound0 e" and ie1: "isint e (a #bs)" using dvd1_eq1[where x="c"] by simp+
  4.2760 +    let ?e = "Inum (real x # bs) e"
  4.2761 +    let ?v="(Sub (C -1) e)"
  4.2762 +    have vb: "?v \<in> set (\<beta> (Eq (CN 0 c e)))" by simp
  4.2763 +    from p have "real x= - ?e" by (simp add: c1) with prems(11) show ?case using dp
  4.2764 +      by simp (erule ballE[where x="1"],
  4.2765 +	simp_all add:algebra_simps numbound0_I[OF bn,where b="real x"and b'="a"and bs="bs"])
  4.2766 +next
  4.2767 +  case (4 c e)hence p: "Ifm (real x #bs) (NEq (CN 0 c e))" (is "?p x") and c1: "c=1" 
  4.2768 +    and bn:"numbound0 e" and ie1: "isint e (a #bs)" using dvd1_eq1[where x="c"] by simp+
  4.2769 +    let ?e = "Inum (real x # bs) e"
  4.2770 +    let ?v="Neg e"
  4.2771 +    have vb: "?v \<in> set (\<beta> (NEq (CN 0 c e)))" by simp
  4.2772 +    {assume "real x - real d + Inum ((real (x -d)) # bs) e \<noteq> 0" 
  4.2773 +      hence ?case by (simp add: c1)}
  4.2774 +    moreover
  4.2775 +    {assume H: "real x - real d + Inum ((real (x -d)) # bs) e = 0"
  4.2776 +      hence "real x = - Inum ((real (x -d)) # bs) e + real d" by simp
  4.2777 +      hence "real x = - Inum (a # bs) e + real d"
  4.2778 +	by (simp add: numbound0_I[OF bn,where b="real x - real d"and b'="a"and bs="bs"])
  4.2779 +       with prems(11) have ?case using dp by simp}
  4.2780 +  ultimately show ?case by blast
  4.2781 +next 
  4.2782 +  case (9 j c e) hence p: "Ifm (real x #bs) (Dvd j (CN 0 c e))" (is "?p x") and c1: "c=1" 
  4.2783 +    and bn:"numbound0 e" using dvd1_eq1[where x="c"] by simp+
  4.2784 +    let ?e = "Inum (real x # bs) e"
  4.2785 +    from prems have "isint e (a #bs)"  by simp 
  4.2786 +    hence ie: "real (floor ?e) = ?e" using isint_iff[where n="e" and bs="(real x)#bs"] numbound0_I[OF bn,where b="real x" and b'="a" and bs="bs"]
  4.2787 +      by (simp add: isint_iff)
  4.2788 +    from prems have id: "j dvd d" by simp
  4.2789 +    from c1 ie[symmetric] have "?p x = (real j rdvd real (x+ floor ?e))" by simp
  4.2790 +    also have "\<dots> = (j dvd x + floor ?e)" 
  4.2791 +      using int_rdvd_real[where i="j" and x="real (x+ floor ?e)"] by simp
  4.2792 +    also have "\<dots> = (j dvd x - d + floor ?e)" 
  4.2793 +      using dvd_period[OF id, where x="x" and c="-1" and t="floor ?e"] by simp
  4.2794 +    also have "\<dots> = (real j rdvd real (x - d + floor ?e))" 
  4.2795 +      using int_rdvd_real[where i="j" and x="real (x-d + floor ?e)",symmetric, simplified]
  4.2796 +      ie by simp
  4.2797 +    also have "\<dots> = (real j rdvd real x - real d + ?e)" 
  4.2798 +      using ie by simp
  4.2799 +    finally show ?case 
  4.2800 +      using numbound0_I[OF bn,where b="real (x-d)" and b'="real x" and bs="bs"] c1 p by simp
  4.2801 +next
  4.2802 +  case (10 j c e) hence p: "Ifm (real x #bs) (NDvd j (CN 0 c e))" (is "?p x") and c1: "c=1" and bn:"numbound0 e" using dvd1_eq1[where x="c"] by simp+
  4.2803 +    let ?e = "Inum (real x # bs) e"
  4.2804 +    from prems have "isint e (a#bs)"  by simp 
  4.2805 +    hence ie: "real (floor ?e) = ?e" using numbound0_I[OF bn,where b="real x" and b'="a" and bs="bs"] isint_iff[where n="e" and bs="(real x)#bs"]
  4.2806 +      by (simp add: isint_iff)
  4.2807 +    from prems have id: "j dvd d" by simp
  4.2808 +    from c1 ie[symmetric] have "?p x = (\<not> real j rdvd real (x+ floor ?e))" by simp
  4.2809 +    also have "\<dots> = (\<not> j dvd x + floor ?e)" 
  4.2810 +      using int_rdvd_real[where i="j" and x="real (x+ floor ?e)"] by simp
  4.2811 +    also have "\<dots> = (\<not> j dvd x - d + floor ?e)" 
  4.2812 +      using dvd_period[OF id, where x="x" and c="-1" and t="floor ?e"] by simp
  4.2813 +    also have "\<dots> = (\<not> real j rdvd real (x - d + floor ?e))" 
  4.2814 +      using int_rdvd_real[where i="j" and x="real (x-d + floor ?e)",symmetric, simplified]
  4.2815 +      ie by simp
  4.2816 +    also have "\<dots> = (\<not> real j rdvd real x - real d + ?e)" 
  4.2817 +      using ie by simp
  4.2818 +    finally show ?case using numbound0_I[OF bn,where b="real (x-d)" and b'="real x" and bs="bs"] c1 p by simp
  4.2819 +qed (auto simp add: numbound0_I[where bs="bs" and b="real (x - d)" and b'="real x"] nth_pos2 simp del: real_of_int_diff)
  4.2820 +
  4.2821 +lemma \<beta>':   
  4.2822 +  assumes lp: "iszlfm p (a #bs)"
  4.2823 +  and u: "d\<beta> p 1"
  4.2824 +  and d: "d\<delta> p d"
  4.2825 +  and dp: "d > 0"
  4.2826 +  shows "\<forall> x. \<not>(\<exists>(j::int) \<in> {1 .. d}. \<exists> b\<in> set(\<beta> p). Ifm ((Inum (a#bs) b + real j) #bs) p) \<longrightarrow> Ifm (real x#bs) p \<longrightarrow> Ifm (real (x - d)#bs) p" (is "\<forall> x. ?b \<longrightarrow> ?P x \<longrightarrow> ?P (x - d)")
  4.2827 +proof(clarify)
  4.2828 +  fix x 
  4.2829 +  assume nb:"?b" and px: "?P x" 
  4.2830 +  hence nb2: "\<not>(\<exists>(j::int) \<in> {1 .. d}. \<exists> b\<in> (Inum (a#bs)) ` set(\<beta> p). real x = b + real j)"
  4.2831 +    by auto
  4.2832 +  from  \<beta>[OF lp u d dp nb2 px] show "?P (x -d )" .
  4.2833 +qed
  4.2834 +
  4.2835 +lemma \<beta>_int: assumes lp: "iszlfm p bs"
  4.2836 +  shows "\<forall> b\<in> set (\<beta> p). isint b bs"
  4.2837 +using lp by (induct p rule: iszlfm.induct) (auto simp add: isint_neg isint_sub)
  4.2838 +
  4.2839 +lemma cpmi_eq: "0 < D \<Longrightarrow> (EX z::int. ALL x. x < z --> (P x = P1 x))
  4.2840 +==> ALL x.~(EX (j::int) : {1..D}. EX (b::int) : B. P(b+j)) --> P (x) --> P (x - D) 
  4.2841 +==> (ALL (x::int). ALL (k::int). ((P1 x)= (P1 (x-k*D))))
  4.2842 +==> (EX (x::int). P(x)) = ((EX (j::int) : {1..D} . (P1(j))) | (EX (j::int) : {1..D}. EX (b::int) : B. P (b+j)))"
  4.2843 +apply(rule iffI)
  4.2844 +prefer 2
  4.2845 +apply(drule minusinfinity)
  4.2846 +apply assumption+
  4.2847 +apply(fastsimp)
  4.2848 +apply clarsimp
  4.2849 +apply(subgoal_tac "!!k. 0<=k \<Longrightarrow> !x. P x \<longrightarrow> P (x - k*D)")
  4.2850 +apply(frule_tac x = x and z=z in decr_lemma)
  4.2851 +apply(subgoal_tac "P1(x - (\<bar>x - z\<bar> + 1) * D)")
  4.2852 +prefer 2
  4.2853 +apply(subgoal_tac "0 <= (\<bar>x - z\<bar> + 1)")
  4.2854 +prefer 2 apply arith
  4.2855 + apply fastsimp
  4.2856 +apply(drule (1)  periodic_finite_ex)
  4.2857 +apply blast
  4.2858 +apply(blast dest:decr_mult_lemma)
  4.2859 +done
  4.2860 +
  4.2861 +
  4.2862 +theorem cp_thm:
  4.2863 +  assumes lp: "iszlfm p (a #bs)"
  4.2864 +  and u: "d\<beta> p 1"
  4.2865 +  and d: "d\<delta> p d"
  4.2866 +  and dp: "d > 0"
  4.2867 +  shows "(\<exists> (x::int). Ifm (real x #bs) p) = (\<exists> j\<in> {1.. d}. Ifm (real j #bs) (minusinf p) \<or> (\<exists> b \<in> set (\<beta> p). Ifm ((Inum (a#bs) b + real j) #bs) p))"
  4.2868 +  (is "(\<exists> (x::int). ?P (real x)) = (\<exists> j\<in> ?D. ?M j \<or> (\<exists> b\<in> ?B. ?P (?I b + real j)))")
  4.2869 +proof-
  4.2870 +  from minusinf_inf[OF lp] 
  4.2871 +  have th: "\<exists>(z::int). \<forall>x<z. ?P (real x) = ?M x" by blast
  4.2872 +  let ?B' = "{floor (?I b) | b. b\<in> ?B}"
  4.2873 +  from \<beta>_int[OF lp] isint_iff[where bs="a # bs"] have B: "\<forall> b\<in> ?B. real (floor (?I b)) = ?I b" by simp
  4.2874 +  from B[rule_format] 
  4.2875 +  have "(\<exists>j\<in>?D. \<exists>b\<in> ?B. ?P (?I b + real j)) = (\<exists>j\<in>?D. \<exists>b\<in> ?B. ?P (real (floor (?I b)) + real j))" 
  4.2876 +    by simp
  4.2877 +  also have "\<dots> = (\<exists>j\<in>?D. \<exists>b\<in> ?B. ?P (real (floor (?I b) + j)))" by simp
  4.2878 +  also have"\<dots> = (\<exists> j \<in> ?D. \<exists> b \<in> ?B'. ?P (real (b + j)))"  by blast
  4.2879 +  finally have BB': 
  4.2880 +    "(\<exists>j\<in>?D. \<exists>b\<in> ?B. ?P (?I b + real j)) = (\<exists> j \<in> ?D. \<exists> b \<in> ?B'. ?P (real (b + j)))" 
  4.2881 +    by blast 
  4.2882 +  hence th2: "\<forall> x. \<not> (\<exists> j \<in> ?D. \<exists> b \<in> ?B'. ?P (real (b + j))) \<longrightarrow> ?P (real x) \<longrightarrow> ?P (real (x - d))" using \<beta>'[OF lp u d dp] by blast
  4.2883 +  from minusinf_repeats[OF d lp]
  4.2884 +  have th3: "\<forall> x k. ?M x = ?M (x-k*d)" by simp
  4.2885 +  from cpmi_eq[OF dp th th2 th3] BB' show ?thesis by blast
  4.2886 +qed
  4.2887 +
  4.2888 +    (* Reddy and Loveland *)
  4.2889 +
  4.2890 +
  4.2891 +consts 
  4.2892 +  \<rho> :: "fm \<Rightarrow> (num \<times> int) list" (* Compute the Reddy and Loveland Bset*)
  4.2893 +  \<sigma>\<rho>:: "fm \<Rightarrow> num \<times> int \<Rightarrow> fm" (* Performs the modified substitution of Reddy and Loveland*)
  4.2894 +  \<alpha>\<rho> :: "fm \<Rightarrow> (num\<times>int) list"
  4.2895 +  a\<rho> :: "fm \<Rightarrow> int \<Rightarrow> fm"
  4.2896 +recdef \<rho> "measure size"
  4.2897 +  "\<rho> (And p q) = (\<rho> p @ \<rho> q)" 
  4.2898 +  "\<rho> (Or p q) = (\<rho> p @ \<rho> q)" 
  4.2899 +  "\<rho> (Eq  (CN 0 c e)) = [(Sub (C -1) e,c)]"
  4.2900 +  "\<rho> (NEq (CN 0 c e)) = [(Neg e,c)]"
  4.2901 +  "\<rho> (Lt  (CN 0 c e)) = []"
  4.2902 +  "\<rho> (Le  (CN 0 c e)) = []"
  4.2903 +  "\<rho> (Gt  (CN 0 c e)) = [(Neg e, c)]"
  4.2904 +  "\<rho> (Ge  (CN 0 c e)) = [(Sub (C (-1)) e, c)]"
  4.2905 +  "\<rho> p = []"
  4.2906 +
  4.2907 +recdef \<sigma>\<rho> "measure size"
  4.2908 +  "\<sigma>\<rho> (And p q) = (\<lambda> (t,k). And (\<sigma>\<rho> p (t,k)) (\<sigma>\<rho> q (t,k)))" 
  4.2909 +  "\<sigma>\<rho> (Or p q) = (\<lambda> (t,k). Or (\<sigma>\<rho> p (t,k)) (\<sigma>\<rho> q (t,k)))" 
  4.2910 +  "\<sigma>\<rho> (Eq  (CN 0 c e)) = (\<lambda> (t,k). if k dvd c then (Eq (Add (Mul (c div k) t) e)) 
  4.2911 +                                            else (Eq (Add (Mul c t) (Mul k e))))"
  4.2912 +  "\<sigma>\<rho> (NEq (CN 0 c e)) = (\<lambda> (t,k). if k dvd c then (NEq (Add (Mul (c div k) t) e)) 
  4.2913 +                                            else (NEq (Add (Mul c t) (Mul k e))))"
  4.2914 +  "\<sigma>\<rho> (Lt  (CN 0 c e)) = (\<lambda> (t,k). if k dvd c then (Lt (Add (Mul (c div k) t) e)) 
  4.2915 +                                            else (Lt (Add (Mul c t) (Mul k e))))"
  4.2916 +  "\<sigma>\<rho> (Le  (CN 0 c e)) = (\<lambda> (t,k). if k dvd c then (Le (Add (Mul (c div k) t) e)) 
  4.2917 +                                            else (Le (Add (Mul c t) (Mul k e))))"
  4.2918 +  "\<sigma>\<rho> (Gt  (CN 0 c e)) = (\<lambda> (t,k). if k dvd c then (Gt (Add (Mul (c div k) t) e)) 
  4.2919 +                                            else (Gt (Add (Mul c t) (Mul k e))))"
  4.2920 +  "\<sigma>\<rho> (Ge  (CN 0 c e)) = (\<lambda> (t,k). if k dvd c then (Ge (Add (Mul (c div k) t) e)) 
  4.2921 +                                            else (Ge (Add (Mul c t) (Mul k e))))"
  4.2922 +  "\<sigma>\<rho> (Dvd i (CN 0 c e)) =(\<lambda> (t,k). if k dvd c then (Dvd i (Add (Mul (c div k) t) e)) 
  4.2923 +                                            else (Dvd (i*k) (Add (Mul c t) (Mul k e))))"
  4.2924 +  "\<sigma>\<rho> (NDvd i (CN 0 c e))=(\<lambda> (t,k). if k dvd c then (NDvd i (Add (Mul (c div k) t) e)) 
  4.2925 +                                            else (NDvd (i*k) (Add (Mul c t) (Mul k e))))"
  4.2926 +  "\<sigma>\<rho> p = (\<lambda> (t,k). p)"
  4.2927 +
  4.2928 +recdef \<alpha>\<rho> "measure size"
  4.2929 +  "\<alpha>\<rho> (And p q) = (\<alpha>\<rho> p @ \<alpha>\<rho> q)" 
  4.2930 +  "\<alpha>\<rho> (Or p q) = (\<alpha>\<rho> p @ \<alpha>\<rho> q)" 
  4.2931 +  "\<alpha>\<rho> (Eq  (CN 0 c e)) = [(Add (C -1) e,c)]"
  4.2932 +  "\<alpha>\<rho> (NEq (CN 0 c e)) = [(e,c)]"
  4.2933 +  "\<alpha>\<rho> (Lt  (CN 0 c e)) = [(e,c)]"
  4.2934 +  "\<alpha>\<rho> (Le  (CN 0 c e)) = [(Add (C -1) e,c)]"
  4.2935 +  "\<alpha>\<rho> p = []"
  4.2936 +
  4.2937 +    (* Simulates normal substituion by modifying the formula see correctness theorem *)
  4.2938 +
  4.2939 +recdef a\<rho> "measure size"
  4.2940 +  "a\<rho> (And p q) = (\<lambda> k. And (a\<rho> p k) (a\<rho> q k))" 
  4.2941 +  "a\<rho> (Or p q) = (\<lambda> k. Or (a\<rho> p k) (a\<rho> q k))" 
  4.2942 +  "a\<rho> (Eq (CN 0 c e)) = (\<lambda> k. if k dvd c then (Eq (CN 0 (c div k) e)) 
  4.2943 +                                           else (Eq (CN 0 c (Mul k e))))"
  4.2944 +  "a\<rho> (NEq (CN 0 c e)) = (\<lambda> k. if k dvd c then (NEq (CN 0 (c div k) e)) 
  4.2945 +                                           else (NEq (CN 0 c (Mul k e))))"
  4.2946 +  "a\<rho> (Lt (CN 0 c e)) = (\<lambda> k. if k dvd c then (Lt (CN 0 (c div k) e)) 
  4.2947 +                                           else (Lt (CN 0 c (Mul k e))))"
  4.2948 +  "a\<rho> (Le (CN 0 c e)) = (\<lambda> k. if k dvd c then (Le (CN 0 (c div k) e)) 
  4.2949 +                                           else (Le (CN 0 c (Mul k e))))"
  4.2950 +  "a\<rho> (Gt (CN 0 c e)) = (\<lambda> k. if k dvd c then (Gt (CN 0 (c div k) e)) 
  4.2951 +                                           else (Gt (CN 0 c (Mul k e))))"
  4.2952 +  "a\<rho> (Ge (CN 0 c e)) = (\<lambda> k. if k dvd c then (Ge (CN 0 (c div k) e)) 
  4.2953 +                                            else (Ge (CN 0 c (Mul k e))))"
  4.2954 +  "a\<rho> (Dvd i (CN 0 c e)) = (\<lambda> k. if k dvd c then (Dvd i (CN 0 (c div k) e)) 
  4.2955 +                                            else (Dvd (i*k) (CN 0 c (Mul k e))))"
  4.2956 +  "a\<rho> (NDvd i (CN 0 c e)) = (\<lambda> k. if k dvd c then (NDvd i (CN 0 (c div k) e)) 
  4.2957 +                                            else (NDvd (i*k) (CN 0 c (Mul k e))))"
  4.2958 +  "a\<rho> p = (\<lambda> k. p)"
  4.2959 +
  4.2960 +constdefs \<sigma> :: "fm \<Rightarrow> int \<Rightarrow> num \<Rightarrow> fm"
  4.2961 +  "\<sigma> p k t \<equiv> And (Dvd k t) (\<sigma>\<rho> p (t,k))"
  4.2962 +
  4.2963 +lemma \<sigma>\<rho>:
  4.2964 +  assumes linp: "iszlfm p (real (x::int)#bs)"
  4.2965 +  and kpos: "real k > 0"
  4.2966 +  and tnb: "numbound0 t"
  4.2967 +  and tint: "isint t (real x#bs)"
  4.2968 +  and kdt: "k dvd floor (Inum (b'#bs) t)"
  4.2969 +  shows "Ifm (real x#bs) (\<sigma>\<rho> p (t,k)) = 
  4.2970 +  (Ifm ((real ((floor (Inum (b'#bs) t)) div k))#bs) p)" 
  4.2971 +  (is "?I (real x) (?s p) = (?I (real ((floor (?N b' t)) div k)) p)" is "_ = (?I ?tk p)")
  4.2972 +using linp kpos tnb
  4.2973 +proof(induct p rule: \<sigma>\<rho>.induct)
  4.2974 +  case (3 c e) 
  4.2975 +  from prems have cp: "c > 0" and nb: "numbound0 e" by auto
  4.2976 +    {assume kdc: "k dvd c" 
  4.2977 +      from kpos have knz: "k\<noteq>0" by simp
  4.2978 +      from tint have ti: "real (floor (?N (real x) t)) = ?N (real x) t" using isint_def by simp
  4.2979 +      from prems have  ?case using real_of_int_div[OF knz kdc] real_of_int_div[OF knz kdt]
  4.2980 +	numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
  4.2981 +      numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"] by (simp add: ti) } 
  4.2982 +    moreover 
  4.2983 +    {assume "\<not> k dvd c"
  4.2984 +      from kpos have knz: "k\<noteq>0" by simp hence knz': "real k \<noteq> 0" by simp
  4.2985 +      from tint have ti: "real (floor (?N (real x) t)) = ?N (real x) t" using isint_def by simp
  4.2986 +      from prems have "?I (real x) (?s (Eq (CN 0 c e))) = ((real c * (?N (real x) t / real k) + ?N (real x) e)* real k = 0)"
  4.2987 +	using real_of_int_div[OF knz kdt]
  4.2988 +	  numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
  4.2989 +	  numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"] by (simp add: ti algebra_simps)
  4.2990 +      also have "\<dots> = (?I ?tk (Eq (CN 0 c e)))" using nonzero_eq_divide_eq[OF knz', where a="real c * (?N (real x) t / real k) + ?N (real x) e" and b="0", symmetric] real_of_int_div[OF knz kdt] numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
  4.2991 +	  numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"]
  4.2992 +	by (simp add: ti)
  4.2993 +      finally have ?case . }
  4.2994 +    ultimately show ?case by blast 
  4.2995 +next
  4.2996 +  case (4 c e)  
  4.2997 +  from prems have cp: "c > 0" and nb: "numbound0 e" by auto
  4.2998 +    {assume kdc: "k dvd c" 
  4.2999 +      from kpos have knz: "k\<noteq>0" by simp
  4.3000 +      from tint have ti: "real (floor (?N (real x) t)) = ?N (real x) t" using isint_def by simp
  4.3001 +      from prems have  ?case using real_of_int_div[OF knz kdc] real_of_int_div[OF knz kdt]
  4.3002 +	numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
  4.3003 +      numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"] by (simp add: ti) } 
  4.3004 +    moreover 
  4.3005 +    {assume "\<not> k dvd c"
  4.3006 +      from kpos have knz: "k\<noteq>0" by simp hence knz': "real k \<noteq> 0" by simp
  4.3007 +      from tint have ti: "real (floor (?N (real x) t)) = ?N (real x) t" using isint_def by simp
  4.3008 +      from prems have "?I (real x) (?s (NEq (CN 0 c e))) = ((real c * (?N (real x) t / real k) + ?N (real x) e)* real k \<noteq> 0)"
  4.3009 +	using real_of_int_div[OF knz kdt]
  4.3010 +	  numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
  4.3011 +	  numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"] by (simp add: ti algebra_simps)
  4.3012 +      also have "\<dots> = (?I ?tk (NEq (CN 0 c e)))" using nonzero_eq_divide_eq[OF knz', where a="real c * (?N (real x) t / real k) + ?N (real x) e" and b="0", symmetric] real_of_int_div[OF knz kdt] numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
  4.3013 +	  numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"]
  4.3014 +	by (simp add: ti)
  4.3015 +      finally have ?case . }
  4.3016 +    ultimately show ?case by blast 
  4.3017 +next
  4.3018 +  case (5 c e) 
  4.3019 +  from prems have cp: "c > 0" and nb: "numbound0 e" by auto
  4.3020 +    {assume kdc: "k dvd c" 
  4.3021 +      from kpos have knz: "k\<noteq>0" by simp
  4.3022 +      from tint have ti: "real (floor (?N (real x) t)) = ?N (real x) t" using isint_def by simp
  4.3023 +      from prems have  ?case using real_of_int_div[OF knz kdc] real_of_int_div[OF knz kdt]
  4.3024 +	numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
  4.3025 +      numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"] by (simp add: ti) } 
  4.3026 +    moreover 
  4.3027 +    {assume "\<not> k dvd c"
  4.3028 +      from kpos have knz: "k\<noteq>0" by simp
  4.3029 +      from tint have ti: "real (floor (?N (real x) t)) = ?N (real x) t" using isint_def by simp
  4.3030 +      from prems have "?I (real x) (?s (Lt (CN 0 c e))) = ((real c * (?N (real x) t / real k) + ?N (real x) e)* real k < 0)"
  4.3031 +	using real_of_int_div[OF knz kdt]
  4.3032 +	  numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
  4.3033 +	  numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"] by (simp add: ti algebra_simps)
  4.3034 +      also have "\<dots> = (?I ?tk (Lt (CN 0 c e)))" using pos_less_divide_eq[OF kpos, where a="real c * (?N (real x) t / real k) + ?N (real x) e" and b="0", symmetric] real_of_int_div[OF knz kdt] numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
  4.3035 +	  numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"]
  4.3036 +	by (simp add: ti)
  4.3037 +      finally have ?case . }
  4.3038 +    ultimately show ?case by blast 
  4.3039 +next
  4.3040 +  case (6 c e)  
  4.3041 +  from prems have cp: "c > 0" and nb: "numbound0 e" by auto
  4.3042 +    {assume kdc: "k dvd c" 
  4.3043 +      from kpos have knz: "k\<noteq>0" by simp
  4.3044 +      from tint have ti: "real (floor (?N (real x) t)) = ?N (real x) t" using isint_def by simp
  4.3045 +      from prems have  ?case using real_of_int_div[OF knz kdc] real_of_int_div[OF knz kdt]
  4.3046 +	numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
  4.3047 +      numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"] by (simp add: ti) } 
  4.3048 +    moreover 
  4.3049 +    {assume "\<not> k dvd c"
  4.3050 +      from kpos have knz: "k\<noteq>0" by simp
  4.3051 +      from tint have ti: "real (floor (?N (real x) t)) = ?N (real x) t" using isint_def by simp
  4.3052 +      from prems have "?I (real x) (?s (Le (CN 0 c e))) = ((real c * (?N (real x) t / real k) + ?N (real x) e)* real k \<le> 0)"
  4.3053 +	using real_of_int_div[OF knz kdt]
  4.3054 +	  numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
  4.3055 +	  numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"] by (simp add: ti algebra_simps)
  4.3056 +      also have "\<dots> = (?I ?tk (Le (CN 0 c e)))" using pos_le_divide_eq[OF kpos, where a="real c * (?N (real x) t / real k) + ?N (real x) e" and b="0", symmetric] real_of_int_div[OF knz kdt] numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
  4.3057 +	  numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"]
  4.3058 +	by (simp add: ti)
  4.3059 +      finally have ?case . }
  4.3060 +    ultimately show ?case by blast 
  4.3061 +next
  4.3062 +  case (7 c e) 
  4.3063 +  from prems have cp: "c > 0" and nb: "numbound0 e" by auto
  4.3064 +    {assume kdc: "k dvd c" 
  4.3065 +      from kpos have knz: "k\<noteq>0" by simp
  4.3066 +      from tint have ti: "real (floor (?N (real x) t)) = ?N (real x) t" using isint_def by simp
  4.3067 +      from prems have  ?case using real_of_int_div[OF knz kdc] real_of_int_div[OF knz kdt]
  4.3068 +	numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
  4.3069 +      numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"] by (simp add: ti) } 
  4.3070 +    moreover 
  4.3071 +    {assume "\<not> k dvd c"
  4.3072 +      from kpos have knz: "k\<noteq>0" by simp
  4.3073 +      from tint have ti: "real (floor (?N (real x) t)) = ?N (real x) t" using isint_def by simp
  4.3074 +      from prems have "?I (real x) (?s (Gt (CN 0 c e))) = ((real c * (?N (real x) t / real k) + ?N (real x) e)* real k > 0)"
  4.3075 +	using real_of_int_div[OF knz kdt]
  4.3076 +	  numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
  4.3077 +	  numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"] by (simp add: ti algebra_simps)
  4.3078 +      also have "\<dots> = (?I ?tk (Gt (CN 0 c e)))" using pos_divide_less_eq[OF kpos, where a="real c * (?N (real x) t / real k) + ?N (real x) e" and b="0", symmetric] real_of_int_div[OF knz kdt] numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
  4.3079 +	  numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"]
  4.3080 +	by (simp add: ti)
  4.3081 +      finally have ?case . }
  4.3082 +    ultimately show ?case by blast 
  4.3083 +next
  4.3084 +  case (8 c e)  
  4.3085 +  from prems have cp: "c > 0" and nb: "numbound0 e" by auto
  4.3086 +    {assume kdc: "k dvd c" 
  4.3087 +      from kpos have knz: "k\<noteq>0" by simp
  4.3088 +      from tint have ti: "real (floor (?N (real x) t)) = ?N (real x) t" using isint_def by simp
  4.3089 +      from prems have  ?case using real_of_int_div[OF knz kdc] real_of_int_div[OF knz kdt]
  4.3090 +	numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
  4.3091 +      numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"] by (simp add: ti) } 
  4.3092 +    moreover 
  4.3093 +    {assume "\<not> k dvd c"
  4.3094 +      from kpos have knz: "k\<noteq>0" by simp
  4.3095 +      from tint have ti: "real (floor (?N (real x) t)) = ?N (real x) t" using isint_def by simp
  4.3096 +      from prems have "?I (real x) (?s (Ge (CN 0 c e))) = ((real c * (?N (real x) t / real k) + ?N (real x) e)* real k \<ge> 0)"
  4.3097 +	using real_of_int_div[OF knz kdt]
  4.3098 +	  numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
  4.3099 +	  numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"] by (simp add: ti algebra_simps)
  4.3100 +      also have "\<dots> = (?I ?tk (Ge (CN 0 c e)))" using pos_divide_le_eq[OF kpos, where a="real c * (?N (real x) t / real k) + ?N (real x) e" and b="0", symmetric] real_of_int_div[OF knz kdt] numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
  4.3101 +	  numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"]
  4.3102 +	by (simp add: ti)
  4.3103 +      finally have ?case . }
  4.3104 +    ultimately show ?case by blast 
  4.3105 +next
  4.3106 +  case (9 i c e)   from prems have cp: "c > 0" and nb: "numbound0 e" by auto
  4.3107 +    {assume kdc: "k dvd c" 
  4.3108 +      from kpos have knz: "k\<noteq>0" by simp
  4.3109 +      from tint have ti: "real (floor (?N (real x) t)) = ?N (real x) t" using isint_def by simp
  4.3110 +      from prems have  ?case using real_of_int_div[OF knz kdc] real_of_int_div[OF knz kdt]
  4.3111 +	numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
  4.3112 +      numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"] by (simp add: ti) } 
  4.3113 +    moreover 
  4.3114 +    {assume "\<not> k dvd c"
  4.3115 +      from kpos have knz: "k\<noteq>0" by simp hence knz': "real k \<noteq> 0" by simp
  4.3116 +      from tint have ti: "real (floor (?N (real x) t)) = ?N (real x) t" using isint_def by simp
  4.3117 +      from prems have "?I (real x) (?s (Dvd i (CN 0 c e))) = (real i * real k rdvd (real c * (?N (real x) t / real k) + ?N (real x) e)* real k)"
  4.3118 +	using real_of_int_div[OF knz kdt]
  4.3119 +	  numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
  4.3120 +	  numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"] by (simp add: ti algebra_simps)
  4.3121 +      also have "\<dots> = (?I ?tk (Dvd i (CN 0 c e)))" using rdvd_mult[OF knz, where n="i"] real_of_int_div[OF knz kdt] numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
  4.3122 +	  numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"]
  4.3123 +	by (simp add: ti)
  4.3124 +      finally have ?case . }
  4.3125 +    ultimately show ?case by blast 
  4.3126 +next
  4.3127 +  case (10 i c e)    from prems have cp: "c > 0" and nb: "numbound0 e" by auto
  4.3128 +    {assume kdc: "k dvd c" 
  4.3129 +      from kpos have knz: "k\<noteq>0" by simp
  4.3130 +      from tint have ti: "real (floor (?N (real x) t)) = ?N (real x) t" using isint_def by simp
  4.3131 +      from prems have  ?case using real_of_int_div[OF knz kdc] real_of_int_div[OF knz kdt]
  4.3132 +	numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
  4.3133 +      numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"] by (simp add: ti) } 
  4.3134 +    moreover 
  4.3135 +    {assume "\<not> k dvd c"
  4.3136 +      from kpos have knz: "k\<noteq>0" by simp hence knz': "real k \<noteq> 0" by simp
  4.3137 +      from tint have ti: "real (floor (?N (real x) t)) = ?N (real x) t" using isint_def by simp
  4.3138 +      from prems have "?I (real x) (?s (NDvd i (CN 0 c e))) = (\<not> (real i * real k rdvd (real c * (?N (real x) t / real k) + ?N (real x) e)* real k))"
  4.3139 +	using real_of_int_div[OF knz kdt]
  4.3140 +	  numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
  4.3141 +	  numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"] by (simp add: ti algebra_simps)
  4.3142 +      also have "\<dots> = (?I ?tk (NDvd i (CN 0 c e)))" using rdvd_mult[OF knz, where n="i"] real_of_int_div[OF knz kdt] numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
  4.3143 +	  numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"]
  4.3144 +	by (simp add: ti)
  4.3145 +      finally have ?case . }
  4.3146 +    ultimately show ?case by blast 
  4.3147 +qed (simp_all add: nth_pos2 bound0_I[where bs="bs" and b="real ((floor (?N b' t)) div k)" and b'="real x"] numbound0_I[where bs="bs" and b="real ((floor (?N b' t)) div k)" and b'="real x"])
  4.3148 +
  4.3149 +
  4.3150 +lemma a\<rho>: 
  4.3151 +  assumes lp: "iszlfm p (real (x::int)#bs)" and kp: "real k > 0" 
  4.3152 +  shows "Ifm (real (x*k)#bs) (a\<rho> p k) = Ifm (real x#bs) p" (is "?I (x*k) (?f p k) = ?I x p")
  4.3153 +using lp bound0_I[where bs="bs" and b="real (x*k)" and b'="real x"] numbound0_I[where bs="bs" and b="real (x*k)" and b'="real x"]
  4.3154 +proof(induct p rule: a\<rho>.induct)
  4.3155 +  case (3 c e)  
  4.3156 +  from prems have cp: "c > 0" and nb: "numbound0 e" by auto
  4.3157 +  from kp have knz: "k\<noteq>0" by simp hence knz': "real k \<noteq> 0" by simp
  4.3158 +    {assume kdc: "k dvd c" from prems have  ?case using real_of_int_div[OF knz kdc] by simp } 
  4.3159 +    moreover 
  4.3160 +    {assume nkdc: "\<not> k dvd c" hence ?case using numbound0_I[OF nb, where bs="bs" and b="real (x*k)" and b'="real x"] nonzero_eq_divide_eq[OF knz', where b="0" and a="real c * real x + Inum (real x # bs) e", symmetric] by (simp add: algebra_simps)}
  4.3161 +    ultimately show ?case by blast 
  4.3162 +next
  4.3163 +  case (4 c e)   
  4.3164 +  from prems have cp: "c > 0" and nb: "numbound0 e" by auto
  4.3165 +  from kp have knz: "k\<noteq>0" by simp hence knz': "real k \<noteq> 0" by simp
  4.3166 +    {assume kdc: "k dvd c" from prems have  ?case using real_of_int_div[OF knz kdc] by simp } 
  4.3167 +    moreover 
  4.3168 +    {assume nkdc: "\<not> k dvd c" hence ?case using numbound0_I[OF nb, where bs="bs" and b="real (x*k)" and b'="real x"] nonzero_eq_divide_eq[OF knz', where b="0" and a="real c * real x + Inum (real x # bs) e", symmetric] by (simp add: algebra_simps)}
  4.3169 +    ultimately show ?case by blast 
  4.3170 +next
  4.3171 +  case (5 c e)   
  4.3172 +  from prems have cp: "c > 0" and nb: "numbound0 e" by auto
  4.3173 +  from kp have knz: "k\<noteq>0" by simp hence knz': "real k \<noteq> 0" by simp
  4.3174 +    {assume kdc: "k dvd c" from prems have  ?case using real_of_int_div[OF knz kdc] by simp } 
  4.3175 +    moreover 
  4.3176 +    {assume nkdc: "\<not> k dvd c" hence ?case using numbound0_I[OF nb, where bs="bs" and b="real (x*k)" and b'="real x"] pos_less_divide_eq[OF kp, where b="0" and a="real c * real x + Inum (real x # bs) e", symmetric] by (simp add: algebra_simps)}
  4.3177 +    ultimately show ?case by blast 
  4.3178 +next
  4.3179 +  case (6 c e)    
  4.3180 +  from prems have cp: "c > 0" and nb: "numbound0 e" by auto
  4.3181 +  from kp have knz: "k\<noteq>0" by simp hence knz': "real k \<noteq> 0" by simp
  4.3182 +    {assume kdc: "k dvd c" from prems have  ?case using real_of_int_div[OF knz kdc] by simp } 
  4.3183 +    moreover 
  4.3184 +    {assume nkdc: "\<not> k dvd c" hence ?case using numbound0_I[OF nb, where bs="bs" and b="real (x*k)" and b'="real x"] pos_le_divide_eq[OF kp, where b="0" and a="real c * real x + Inum (real x # bs) e", symmetric] by (simp add: algebra_simps)}
  4.3185 +    ultimately show ?case by blast 
  4.3186 +next
  4.3187 +  case (7 c e)    
  4.3188 +  from prems have cp: "c > 0" and nb: "numbound0 e" by auto
  4.3189 +  from kp have knz: "k\<noteq>0" by simp hence knz': "real k \<noteq> 0" by simp
  4.3190 +    {assume kdc: "k dvd c" from prems have  ?case using real_of_int_div[OF knz kdc] by simp } 
  4.3191 +    moreover 
  4.3192 +    {assume nkdc: "\<not> k dvd c" hence ?case using numbound0_I[OF nb, where bs="bs" and b="real (x*k)" and b'="real x"] pos_divide_less_eq[OF kp, where b="0" and a="real c * real x + Inum (real x # bs) e", symmetric] by (simp add: algebra_simps)}
  4.3193 +    ultimately show ?case by blast 
  4.3194 +next
  4.3195 +  case (8 c e)    
  4.3196 +  from prems have cp: "c > 0" and nb: "numbound0 e" by auto
  4.3197 +  from kp have knz: "k\<noteq>0" by simp hence knz': "real k \<noteq> 0" by simp
  4.3198 +    {assume kdc: "k dvd c" from prems have  ?case using real_of_int_div[OF knz kdc] by simp } 
  4.3199 +    moreover 
  4.3200 +    {assume nkdc: "\<not> k dvd c" hence ?case using numbound0_I[OF nb, where bs="bs" and b="real (x*k)" and b'="real x"] pos_divide_le_eq[OF kp, where b="0" and a="real c * real x + Inum (real x # bs) e", symmetric] by (simp add: algebra_simps)}
  4.3201 +    ultimately show ?case by blast 
  4.3202 +next
  4.3203 +  case (9 i c e)
  4.3204 +  from prems have cp: "c > 0" and nb: "numbound0 e" by auto
  4.3205 +  from kp have knz: "k\<noteq>0" by simp hence knz': "real k \<noteq> 0" by simp
  4.3206 +  {assume kdc: "k dvd c" from prems have  ?case using real_of_int_div[OF knz kdc] by simp } 
  4.3207 +  moreover 
  4.3208 +  {assume "\<not> k dvd c"
  4.3209 +    hence "Ifm (real (x*k)#bs) (a\<rho> (Dvd i (CN 0 c e)) k) = 
  4.3210 +      (real i * real k rdvd (real c * real x + Inum (real x#bs) e) * real k)" 
  4.3211 +      using numbound0_I[OF nb, where bs="bs" and b="real (x*k)" and b'="real x"] 
  4.3212 +      by (simp add: algebra_simps)
  4.3213 +    also have "\<dots> = (Ifm (real x#bs) (Dvd i (CN 0 c e)))" by (simp add: rdvd_mult[OF knz, where n="i"])
  4.3214 +    finally have ?case . }
  4.3215 +  ultimately show ?case by blast 
  4.3216 +next
  4.3217 +  case (10 i c e) 
  4.3218 +  from prems have cp: "c > 0" and nb: "numbound0 e" by auto
  4.3219 +  from kp have knz: "k\<noteq>0" by simp hence knz': "real k \<noteq> 0" by simp
  4.3220 +  {assume kdc: "k dvd c" from prems have  ?case using real_of_int_div[OF knz kdc] by simp } 
  4.3221 +  moreover 
  4.3222 +  {assume "\<not> k dvd c"
  4.3223 +    hence "Ifm (real (x*k)#bs) (a\<rho> (NDvd i (CN 0 c e)) k) = 
  4.3224 +      (\<not> (real i * real k rdvd (real c * real x + Inum (real x#bs) e) * real k))" 
  4.3225 +      using numbound0_I[OF nb, where bs="bs" and b="real (x*k)" and b'="real x"] 
  4.3226 +      by (simp add: algebra_simps)
  4.3227 +    also have "\<dots> = (Ifm (real x#bs) (NDvd i (CN 0 c e)))" by (simp add: rdvd_mult[OF knz, where n="i"])
  4.3228 +    finally have ?case . }
  4.3229 +  ultimately show ?case by blast 
  4.3230 +qed (simp_all add: nth_pos2)
  4.3231 +
  4.3232 +lemma a\<rho>_ex: 
  4.3233 +  assumes lp: "iszlfm p (real (x::int)#bs)" and kp: "k > 0"
  4.3234 +  shows "(\<exists> (x::int). real k rdvd real x \<and> Ifm (real x#bs) (a\<rho> p k)) = 
  4.3235 +  (\<exists> (x::int). Ifm (real x#bs) p)" (is "(\<exists> x. ?D x \<and> ?P' x) = (\<exists> x. ?P x)")
  4.3236 +proof-
  4.3237 +  have "(\<exists> x. ?D x \<and> ?P' x) = (\<exists> x. k dvd x \<and> ?P' x)" using int_rdvd_iff by simp
  4.3238 +  also have "\<dots> = (\<exists>x. ?P' (x*k))" using unity_coeff_ex[where P="?P'" and l="k", simplified]
  4.3239 +    by (simp add: algebra_simps)
  4.3240 +  also have "\<dots> = (\<exists> x. ?P x)" using a\<rho> iszlfm_gen[OF lp] kp by auto
  4.3241 +  finally show ?thesis .
  4.3242 +qed
  4.3243 +
  4.3244 +lemma \<sigma>\<rho>': assumes lp: "iszlfm p (real (x::int)#bs)" and kp: "k > 0" and nb: "numbound0 t"
  4.3245 +  shows "Ifm (real x#bs) (\<sigma>\<rho> p (t,k)) = Ifm ((Inum (real x#bs) t)#bs) (a\<rho> p k)"
  4.3246 +using lp 
  4.3247 +by(induct p rule: \<sigma>\<rho>.induct, simp_all add: 
  4.3248 +  numbound0_I[OF nb, where bs="bs" and b="Inum (real x#bs) t" and b'="real x"] 
  4.3249 +  numbound0_I[where bs="bs" and b="Inum (real x#bs) t" and b'="real x"] 
  4.3250 +  bound0_I[where bs="bs" and b="Inum (real x#bs) t" and b'="real x"] nth_pos2 cong: imp_cong)
  4.3251 +
  4.3252 +lemma \<sigma>\<rho>_nb: assumes lp:"iszlfm p (a#bs)" and nb: "numbound0 t"
  4.3253 +  shows "bound0 (\<sigma>\<rho> p (t,k))"
  4.3254 +  using lp
  4.3255 +  by (induct p rule: iszlfm.induct, auto simp add: nb)
  4.3256 +
  4.3257 +lemma \<rho>_l:
  4.3258 +  assumes lp: "iszlfm p (real (i::int)#bs)"
  4.3259 +  shows "\<forall> (b,k) \<in> set (\<rho> p). k >0 \<and> numbound0 b \<and> isint b (real i#bs)"
  4.3260 +using lp by (induct p rule: \<rho>.induct, auto simp add: isint_sub isint_neg)
  4.3261 +
  4.3262 +lemma \<alpha>\<rho>_l:
  4.3263 +  assumes lp: "iszlfm p (real (i::int)#bs)"
  4.3264 +  shows "\<forall> (b,k) \<in> set (\<alpha>\<rho> p). k >0 \<and> numbound0 b \<and> isint b (real i#bs)"
  4.3265 +using lp isint_add [OF isint_c[where j="- 1"],where bs="real i#bs"]
  4.3266 + by (induct p rule: \<alpha>\<rho>.induct, auto)
  4.3267 +
  4.3268 +lemma zminusinf_\<rho>:
  4.3269 +  assumes lp: "iszlfm p (real (i::int)#bs)"
  4.3270 +  and nmi: "\<not> (Ifm (real i#bs) (minusinf p))" (is "\<not> (Ifm (real i#bs) (?M p))")
  4.3271 +  and ex: "Ifm (real i#bs) p" (is "?I i p")
  4.3272 +  shows "\<exists> (e,c) \<in> set (\<rho> p). real (c*i) > Inum (real i#bs) e" (is "\<exists> (e,c) \<in> ?R p. real (c*i) > ?N i e")
  4.3273 +  using lp nmi ex
  4.3274 +by (induct p rule: minusinf.induct, auto)
  4.3275 +
  4.3276 +
  4.3277 +lemma \<sigma>_And: "Ifm bs (\<sigma> (And p q) k t)  = Ifm bs (And (\<sigma> p k t) (\<sigma> q k t))"
  4.3278 +using \<sigma>_def by auto
  4.3279 +lemma \<sigma>_Or: "Ifm bs (\<sigma> (Or p q) k t)  = Ifm bs (Or (\<sigma> p k t) (\<sigma> q k t))"
  4.3280 +using \<sigma>_def by auto
  4.3281 +
  4.3282 +lemma \<rho>: assumes lp: "iszlfm p (real (i::int) #bs)"
  4.3283 +  and pi: "Ifm (real i#bs) p"
  4.3284 +  and d: "d\<delta> p d"
  4.3285 +  and dp: "d > 0"
  4.3286 +  and nob: "\<forall>(e,c) \<in> set (\<rho> p). \<forall> j\<in> {1 .. c*d}. real (c*i) \<noteq> Inum (real i#bs) e + real j"
  4.3287 +  (is "\<forall>(e,c) \<in> set (\<rho> p). \<forall> j\<in> {1 .. c*d}. _ \<noteq> ?N i e + _")
  4.3288 +  shows "Ifm (real(i - d)#bs) p"
  4.3289 +  using lp pi d nob
  4.3290 +proof(induct p rule: iszlfm.induct)
  4.3291 +  case (3 c e) hence cp: "c >0" and nb: "numbound0 e" and ei: "isint e (real i#bs)"
  4.3292 +    and pi: "real (c*i) = - 1 -  ?N i e + real (1::int)" and nob: "\<forall> j\<in> {1 .. c*d}. real (c*i) \<noteq> -1 - ?N i e + real j"
  4.3293 +    by simp+
  4.3294 +  from mult_strict_left_mono[OF dp cp]  have one:"1 \<in> {1 .. c*d}" by auto
  4.3295 +  from nob[rule_format, where j="1", OF one] pi show ?case by simp
  4.3296 +next
  4.3297 +  case (4 c e)  
  4.3298 +  hence cp: "c >0" and nb: "numbound0 e" and ei: "isint e (real i#bs)"
  4.3299 +    and nob: "\<forall> j\<in> {1 .. c*d}. real (c*i) \<noteq> - ?N i e + real j"
  4.3300 +    by simp+
  4.3301 +  {assume "real (c*i) \<noteq> - ?N i e + real (c*d)"
  4.3302 +    with numbound0_I[OF nb, where bs="bs" and b="real i - real d" and b'="real i"]
  4.3303 +    have ?case by (simp add: algebra_simps)}
  4.3304 +  moreover
  4.3305 +  {assume pi: "real (c*i) = - ?N i e + real (c*d)"
  4.3306 +    from mult_strict_left_mono[OF dp cp] have d: "(c*d) \<in> {1 .. c*d}" by simp
  4.3307 +    from nob[rule_format, where j="c*d", OF d] pi have ?case by simp }
  4.3308 +  ultimately show ?case by blast
  4.3309 +next
  4.3310 +  case (5 c e) hence cp: "c > 0" by simp
  4.3311 +  from prems mult_strict_left_mono[OF dp cp, simplified real_of_int_less_iff[symmetric] 
  4.3312 +    real_of_int_mult]
  4.3313 +  show ?case using prems dp 
  4.3314 +    by (simp add: add: numbound0_I[where bs="bs" and b="real i - real d" and b'="real i"] 
  4.3315 +      algebra_simps)
  4.3316 +next
  4.3317 +  case (6 c e)  hence cp: "c > 0" by simp
  4.3318 +  from prems mult_strict_left_mono[OF dp cp, simplified real_of_int_less_iff[symmetric] 
  4.3319 +    real_of_int_mult]
  4.3320 +  show ?case using prems dp 
  4.3321 +    by (simp add: add: numbound0_I[where bs="bs" and b="real i - real d" and b'="real i"] 
  4.3322 +      algebra_simps)
  4.3323 +next
  4.3324 +  case (7 c e) hence cp: "c >0" and nb: "numbound0 e" and ei: "isint e (real i#bs)"
  4.3325 +    and nob: "\<forall> j\<in> {1 .. c*d}. real (c*i) \<noteq> - ?N i e + real j"
  4.3326 +    and pi: "real (c*i) + ?N i e > 0" and cp': "real c >0"
  4.3327 +    by simp+
  4.3328 +  let ?fe = "floor (?N i e)"
  4.3329 +  from pi cp have th:"(real i +?N i e / real c)*real c > 0" by (simp add: algebra_simps)
  4.3330 +  from pi ei[simplified isint_iff] have "real (c*i + ?fe) > real (0::int)" by simp
  4.3331 +  hence pi': "c*i + ?fe > 0" by (simp only: real_of_int_less_iff[symmetric])
  4.3332 +  have "real (c*i) + ?N i e > real (c*d) \<or> real (c*i) + ?N i e \<le> real (c*d)" by auto
  4.3333 +  moreover
  4.3334 +  {assume "real (c*i) + ?N i e > real (c*d)" hence ?case
  4.3335 +      by (simp add: algebra_simps 
  4.3336 +	numbound0_I[OF nb,where bs="bs" and b="real i - real d" and b'="real i"])} 
  4.3337 +  moreover 
  4.3338 +  {assume H:"real (c*i) + ?N i e \<le> real (c*d)"
  4.3339 +    with ei[simplified isint_iff] have "real (c*i + ?fe) \<le> real (c*d)" by simp
  4.3340 +    hence pid: "c*i + ?fe \<le> c*d" by (simp only: real_of_int_le_iff)
  4.3341 +    with pi' have "\<exists> j1\<in> {1 .. c*d}. c*i + ?fe = j1" by auto
  4.3342 +    hence "\<exists> j1\<in> {1 .. c*d}. real (c*i) = - ?N i e + real j1" 
  4.3343 +      by (simp only: diff_def[symmetric] real_of_int_mult real_of_int_add real_of_int_inject[symmetric] ei[simplified isint_iff] algebra_simps)
  4.3344 +    with nob  have ?case by blast }
  4.3345 +  ultimately show ?case by blast
  4.3346 +next
  4.3347 +  case (8 c e)  hence cp: "c >0" and nb: "numbound0 e" and ei: "isint e (real i#bs)"
  4.3348 +    and nob: "\<forall> j\<in> {1 .. c*d}. real (c*i) \<noteq> - 1 - ?N i e + real j"
  4.3349 +    and pi: "real (c*i) + ?N i e \<ge> 0" and cp': "real c >0"
  4.3350 +    by simp+
  4.3351 +  let ?fe = "floor (?N i e)"
  4.3352 +  from pi cp have th:"(real i +?N i e / real c)*real c \<ge> 0" by (simp add: algebra_simps)
  4.3353 +  from pi ei[simplified isint_iff] have "real (c*i + ?fe) \<ge> real (0::int)" by simp
  4.3354 +  hence pi': "c*i + 1 + ?fe \<ge> 1" by (simp only: real_of_int_le_iff[symmetric])
  4.3355 +  have "real (c*i) + ?N i e \<ge> real (c*d) \<or> real (c*i) + ?N i e < real (c*d)" by auto
  4.3356 +  moreover
  4.3357 +  {assume "real (c*i) + ?N i e \<ge> real (c*d)" hence ?case
  4.3358 +      by (simp add: algebra_simps 
  4.3359 +	numbound0_I[OF nb,where bs="bs" and b="real i - real d" and b'="real i"])} 
  4.3360 +  moreover 
  4.3361 +  {assume H:"real (c*i) + ?N i e < real (c*d)"
  4.3362 +    with ei[simplified isint_iff] have "real (c*i + ?fe) < real (c*d)" by simp
  4.3363 +    hence pid: "c*i + 1 + ?fe \<le> c*d" by (simp only: real_of_int_le_iff)
  4.3364 +    with pi' have "\<exists> j1\<in> {1 .. c*d}. c*i + 1+ ?fe = j1" by auto
  4.3365 +    hence "\<exists> j1\<in> {1 .. c*d}. real (c*i) + 1= - ?N i e + real j1"
  4.3366 +      by (simp only: diff_def[symmetric] real_of_int_mult real_of_int_add real_of_int_inject[symmetric] ei[simplified isint_iff] algebra_simps real_of_one) 
  4.3367 +    hence "\<exists> j1\<in> {1 .. c*d}. real (c*i) = (- ?N i e + real j1) - 1"
  4.3368 +      by (simp only: algebra_simps diff_def[symmetric])
  4.3369 +        hence "\<exists> j1\<in> {1 .. c*d}. real (c*i) = - 1 - ?N i e + real j1"
  4.3370 +	  by (simp only: add_ac diff_def)
  4.3371 +    with nob  have ?case by blast }
  4.3372 +  ultimately show ?case by blast
  4.3373 +next
  4.3374 +  case (9 j c e)  hence p: "real j rdvd real (c*i) + ?N i e" (is "?p x") and cp: "c > 0" and bn:"numbound0 e"  by simp+
  4.3375 +    let ?e = "Inum (real i # bs) e"
  4.3376 +    from prems have "isint e (real i #bs)"  by simp 
  4.3377 +    hence ie: "real (floor ?e) = ?e" using isint_iff[where n="e" and bs="(real i)#bs"] numbound0_I[OF bn,where b="real i" and b'="real i" and bs="bs"]
  4.3378 +      by (simp add: isint_iff)
  4.3379 +    from prems have id: "j dvd d" by simp
  4.3380 +    from ie[symmetric] have "?p i = (real j rdvd real (c*i+ floor ?e))" by simp
  4.3381 +    also have "\<dots> = (j dvd c*i + floor ?e)" 
  4.3382 +      using int_rdvd_iff [where i="j" and t="c*i+ floor ?e"] by simp
  4.3383 +    also have "\<dots> = (j dvd c*i - c*d + floor ?e)" 
  4.3384 +      using dvd_period[OF id, where x="c*i" and c="-c" and t="floor ?e"] by simp
  4.3385 +    also have "\<dots> = (real j rdvd real (c*i - c*d + floor ?e))" 
  4.3386 +      using int_rdvd_iff[where i="j" and t="(c*i - c*d + floor ?e)",symmetric, simplified]
  4.3387 +      ie by simp
  4.3388 +    also have "\<dots> = (real j rdvd real (c*(i - d)) + ?e)" 
  4.3389 +      using ie by (simp add:algebra_simps)
  4.3390 +    finally show ?case 
  4.3391 +      using numbound0_I[OF bn,where b="real i - real d" and b'="real i" and bs="bs"] p 
  4.3392 +      by (simp add: algebra_simps)
  4.3393 +next
  4.3394 +  case (10 j c e)   hence p: "\<not> (real j rdvd real (c*i) + ?N i e)" (is "?p x") and cp: "c > 0" and bn:"numbound0 e"  by simp+
  4.3395 +    let ?e = "Inum (real i # bs) e"
  4.3396 +    from prems have "isint e (real i #bs)"  by simp 
  4.3397 +    hence ie: "real (floor ?e) = ?e" using isint_iff[where n="e" and bs="(real i)#bs"] numbound0_I[OF bn,where b="real i" and b'="real i" and bs="bs"]
  4.3398 +      by (simp add: isint_iff)
  4.3399 +    from prems have id: "j dvd d" by simp
  4.3400 +    from ie[symmetric] have "?p i = (\<not> (real j rdvd real (c*i+ floor ?e)))" by simp
  4.3401 +    also have "\<dots> = Not (j dvd c*i + floor ?e)" 
  4.3402 +      using int_rdvd_iff [where i="j" and t="c*i+ floor ?e"] by simp
  4.3403 +    also have "\<dots> = Not (j dvd c*i - c*d + floor ?e)" 
  4.3404 +      using dvd_period[OF id, where x="c*i" and c="-c" and t="floor ?e"] by simp
  4.3405 +    also have "\<dots> = Not (real j rdvd real (c*i - c*d + floor ?e))" 
  4.3406 +      using int_rdvd_iff[where i="j" and t="(c*i - c*d + floor ?e)",symmetric, simplified]
  4.3407 +      ie by simp
  4.3408 +    also have "\<dots> = Not (real j rdvd real (c*(i - d)) + ?e)" 
  4.3409 +      using ie by (simp add:algebra_simps)
  4.3410 +    finally show ?case 
  4.3411 +      using numbound0_I[OF bn,where b="real i - real d" and b'="real i" and bs="bs"] p 
  4.3412 +      by (simp add: algebra_simps)
  4.3413 +qed(auto simp add: numbound0_I[where bs="bs" and b="real i - real d" and b'="real i"] nth_pos2)
  4.3414 +
  4.3415 +lemma \<sigma>_nb: assumes lp: "iszlfm p (a#bs)" and nb: "numbound0 t"
  4.3416 +  shows "bound0 (\<sigma> p k t)"
  4.3417 +  using \<sigma>\<rho>_nb[OF lp nb] nb by (simp add: \<sigma>_def)
  4.3418 +  
  4.3419 +lemma \<rho>':   assumes lp: "iszlfm p (a #bs)"
  4.3420 +  and d: "d\<delta> p d"
  4.3421 +  and dp: "d > 0"
  4.3422 +  shows "\<forall> x. \<not>(\<exists> (e,c) \<in> set(\<rho> p). \<exists>(j::int) \<in> {1 .. c*d}. Ifm (a #bs) (\<sigma> p c (Add e (C j)))) \<longrightarrow> Ifm (real x#bs) p \<longrightarrow> Ifm (real (x - d)#bs) p" (is "\<forall> x. ?b x \<longrightarrow> ?P x \<longrightarrow> ?P (x - d)")
  4.3423 +proof(clarify)
  4.3424 +  fix x 
  4.3425 +  assume nob1:"?b x" and px: "?P x" 
  4.3426 +  from iszlfm_gen[OF lp, rule_format, where y="real x"] have lp': "iszlfm p (real x#bs)".
  4.3427 +  have nob: "\<forall>(e, c)\<in>set (\<rho> p). \<forall>j\<in>{1..c * d}. real (c * x) \<noteq> Inum (real x # bs) e + real j" 
  4.3428 +  proof(clarify)
  4.3429 +    fix e c j assume ecR: "(e,c) \<in> set (\<rho> p)" and jD: "j\<in> {1 .. c*d}"
  4.3430 +      and cx: "real (c*x) = Inum (real x#bs) e + real j"
  4.3431 +    let ?e = "Inum (real x#bs) e"
  4.3432 +    let ?fe = "floor ?e"
  4.3433 +    from \<rho>_l[OF lp'] ecR have ei:"isint e (real x#bs)" and cp:"c>0" and nb:"numbound0 e"
  4.3434 +      by auto
  4.3435 +    from numbound0_gen [OF nb ei, rule_format,where y="a"] have "isint e (a#bs)" .
  4.3436 +    from cx ei[simplified isint_iff] have "real (c*x) = real (?fe + j)" by simp
  4.3437 +    hence cx: "c*x = ?fe + j" by (simp only: real_of_int_inject)
  4.3438 +    hence cdej:"c dvd ?fe + j" by (simp add: dvd_def) (rule_tac x="x" in exI, simp)
  4.3439 +    hence "real c rdvd real (?fe + j)" by (simp only: int_rdvd_iff)
  4.3440 +    hence rcdej: "real c rdvd ?e + real j" by (simp add: ei[simplified isint_iff])
  4.3441 +    from cx have "(c*x) div c = (?fe + j) div c" by simp
  4.3442 +    with cp have "x = (?fe + j) div c" by simp
  4.3443 +    with px have th: "?P ((?fe + j) div c)" by auto
  4.3444 +    from cp have cp': "real c > 0" by simp
  4.3445 +    from cdej have cdej': "c dvd floor (Inum (real x#bs) (Add e (C j)))" by simp
  4.3446 +    from nb have nb': "numbound0 (Add e (C j))" by simp
  4.3447 +    have ji: "isint (C j) (real x#bs)" by (simp add: isint_def)
  4.3448 +    from isint_add[OF ei ji] have ei':"isint (Add e (C j)) (real x#bs)" .
  4.3449 +    from th \<sigma>\<rho>[where b'="real x", OF lp' cp' nb' ei' cdej',symmetric]
  4.3450 +    have "Ifm (real x#bs) (\<sigma>\<rho> p (Add e (C j), c))" by simp
  4.3451 +    with rcdej have th: "Ifm (real x#bs) (\<sigma> p c (Add e (C j)))" by (simp add: \<sigma>_def)
  4.3452 +    from th bound0_I[OF \<sigma>_nb[OF lp nb', where k="c"],where bs="bs" and b="real x" and b'="a"]
  4.3453 +    have "Ifm (a#bs) (\<sigma> p c (Add e (C j)))" by blast
  4.3454 +      with ecR jD nob1    show "False" by blast
  4.3455 +  qed
  4.3456 +  from \<rho>[OF lp' px d dp nob] show "?P (x -d )" . 
  4.3457 +qed
  4.3458 +
  4.3459 +
  4.3460 +lemma rl_thm: 
  4.3461 +  assumes lp: "iszlfm p (real (i::int)#bs)"
  4.3462 +  shows "(\<exists> (x::int). Ifm (real x#bs) p) = ((\<exists> j\<in> {1 .. \<delta> p}. Ifm (real j#bs) (minusinf p)) \<or> (\<exists> (e,c) \<in> set (\<rho> p). \<exists> j\<in> {1 .. c*(\<delta> p)}. Ifm (a#bs) (\<sigma> p c (Add e (C j)))))"
  4.3463 +  (is "(\<exists>(x::int). ?P x) = ((\<exists> j\<in> {1.. \<delta> p}. ?MP j)\<or>(\<exists> (e,c) \<in> ?R. \<exists> j\<in> _. ?SP c e j))" 
  4.3464 +    is "?lhs = (?MD \<or> ?RD)"  is "?lhs = ?rhs")
  4.3465 +proof-
  4.3466 +  let ?d= "\<delta> p"
  4.3467 +  from \<delta>[OF lp] have d:"d\<delta> p ?d" and dp: "?d > 0" by auto
  4.3468 +  { assume H:"?MD" hence th:"\<exists> (x::int). ?MP x" by blast
  4.3469 +    from H minusinf_ex[OF lp th] have ?thesis  by blast}
  4.3470 +  moreover
  4.3471 +  { fix e c j assume exR:"(e,c) \<in> ?R" and jD:"j\<in> {1 .. c*?d}" and spx:"?SP c e j"
  4.3472 +    from exR \<rho>_l[OF lp] have nb: "numbound0 e" and ei:"isint e (real i#bs)" and cp: "c > 0"
  4.3473 +      by auto
  4.3474 +    have "isint (C j) (real i#bs)" by (simp add: isint_iff)
  4.3475 +    with isint_add[OF numbound0_gen[OF nb ei,rule_format, where y="real i"]]
  4.3476 +    have eji:"isint (Add e (C j)) (real i#bs)" by simp
  4.3477 +    from nb have nb': "numbound0 (Add e (C j))" by simp
  4.3478 +    from spx bound0_I[OF \<sigma>_nb[OF lp nb', where k="c"], where bs="bs" and b="a" and b'="real i"]
  4.3479 +    have spx': "Ifm (real i # bs) (\<sigma> p c (Add e (C j)))" by blast
  4.3480 +    from spx' have rcdej:"real c rdvd (Inum (real i#bs) (Add e (C j)))" 
  4.3481 +      and sr:"Ifm (real i#bs) (\<sigma>\<rho> p (Add e (C j),c))" by (simp add: \<sigma>_def)+
  4.3482 +    from rcdej eji[simplified isint_iff] 
  4.3483 +    have "real c rdvd real (floor (Inum (real i#bs) (Add e (C j))))" by simp
  4.3484 +    hence cdej:"c dvd floor (Inum (real i#bs) (Add e (C j)))" by (simp only: int_rdvd_iff)
  4.3485 +    from cp have cp': "real c > 0" by simp
  4.3486 +    from \<sigma>\<rho>[OF lp cp' nb' eji cdej] spx' have "?P (\<lfloor>Inum (real i # bs) (Add e (C j))\<rfloor> div c)"
  4.3487 +      by (simp add: \<sigma>_def)
  4.3488 +    hence ?lhs by blast
  4.3489 +    with exR jD spx have ?thesis by blast}
  4.3490 +  moreover
  4.3491 +  { fix x assume px: "?P x" and nob: "\<not> ?RD"
  4.3492 +    from iszlfm_gen [OF lp,rule_format, where y="a"] have lp':"iszlfm p (a#bs)" .
  4.3493 +    from \<rho>'[OF lp' d dp, rule_format, OF nob] have th:"\<forall> x. ?P x \<longrightarrow> ?P (x - ?d)" by blast
  4.3494 +    from minusinf_inf[OF lp] obtain z where z:"\<forall> x<z. ?MP x = ?P x" by blast
  4.3495 +    have zp: "abs (x - z) + 1 \<ge> 0" by arith
  4.3496 +    from decr_lemma[OF dp,where x="x" and z="z"] 
  4.3497 +      decr_mult_lemma[OF dp th zp, rule_format, OF px] z have th:"\<exists> x. ?MP x" by auto
  4.3498 +    with minusinf_bex[OF lp] px nob have ?thesis by blast}
  4.3499 +  ultimately show ?thesis by blast
  4.3500 +qed
  4.3501 +
  4.3502 +lemma mirror_\<alpha>\<rho>:   assumes lp: "iszlfm p (a#bs)"
  4.3503 +  shows "(\<lambda> (t,k). (Inum (a#bs) t, k)) ` set (\<alpha>\<rho> p) = (\<lambda> (t,k). (Inum (a#bs) t,k)) ` set (\<rho> (mirror p))"
  4.3504 +using lp
  4.3505 +by (induct p rule: mirror.induct, simp_all add: split_def image_Un )
  4.3506 +  
  4.3507 +text {* The @{text "\<real>"} part*}
  4.3508 +
  4.3509 +text{* Linearity for fm where Bound 0 ranges over @{text "\<real>"}*}
  4.3510 +consts
  4.3511 +  isrlfm :: "fm \<Rightarrow> bool"   (* Linearity test for fm *)
  4.3512 +recdef isrlfm "measure size"
  4.3513 +  "isrlfm (And p q) = (isrlfm p \<and> isrlfm q)" 
  4.3514 +  "isrlfm (Or p q) = (isrlfm p \<and> isrlfm q)" 
  4.3515 +  "isrlfm (Eq  (CN 0 c e)) = (c>0 \<and> numbound0 e)"
  4.3516 +  "isrlfm (NEq (CN 0 c e)) = (c>0 \<and> numbound0 e)"
  4.3517 +  "isrlfm (Lt  (CN 0 c e)) = (c>0 \<and> numbound0 e)"
  4.3518 +  "isrlfm (Le  (CN 0 c e)) = (c>0 \<and> numbound0 e)"
  4.3519 +  "isrlfm (Gt  (CN 0 c e)) = (c>0 \<and> numbound0 e)"
  4.3520 +  "isrlfm (Ge  (CN 0 c e)) = (c>0 \<and> numbound0 e)"
  4.3521 +  "isrlfm p = (isatom p \<and> (bound0 p))"
  4.3522 +
  4.3523 +constdefs fp :: "fm \<Rightarrow> int \<Rightarrow> num \<Rightarrow> int \<Rightarrow> fm"
  4.3524 +  "fp p n s j \<equiv> (if n > 0 then 
  4.3525 +            (And p (And (Ge (CN 0 n (Sub s (Add (Floor s) (C j)))))
  4.3526 +                        (Lt (CN 0 n (Sub s (Add (Floor s) (C (j+1))))))))
  4.3527 +            else 
  4.3528 +            (And p (And (Le (CN 0 (-n) (Add (Neg s) (Add (Floor s) (C j))))) 
  4.3529 +                        (Gt (CN 0 (-n) (Add (Neg s) (Add (Floor s) (C (j + 1)))))))))"
  4.3530 +
  4.3531 +  (* splits the bounded from the unbounded part*)
  4.3532 +consts rsplit0 :: "num \<Rightarrow> (fm \<times> int \<times> num) list" 
  4.3533 +recdef rsplit0 "measure num_size"
  4.3534 +  "rsplit0 (Bound 0) = [(T,1,C 0)]"
  4.3535 +  "rsplit0 (Add a b) = (let acs = rsplit0 a ; bcs = rsplit0 b 
  4.3536 +              in map (\<lambda> ((p,n,t),(q,m,s)). (And p q, n+m, Add t s)) [(a,b). a\<leftarrow>acs,b\<leftarrow>bcs])"
  4.3537 +  "rsplit0 (Sub a b) = rsplit0 (Add a (Neg b))"
  4.3538 +  "rsplit0 (Neg a) = map (\<lambda> (p,n,s). (p,-n,Neg s)) (rsplit0 a)"
  4.3539 +  "rsplit0 (Floor a) = foldl (op @) [] (map 
  4.3540 +      (\<lambda> (p,n,s). if n=0 then [(p,0,Floor s)]
  4.3541 +          else (map (\<lambda> j. (fp p n s j, 0, Add (Floor s) (C j))) (if n > 0 then iupt (0,n) else iupt(n,0))))
  4.3542 +       (rsplit0 a))"
  4.3543 +  "rsplit0 (CN 0 c a) = map (\<lambda> (p,n,s). (p,n+c,s)) (rsplit0 a)"
  4.3544 +  "rsplit0 (CN m c a) = map (\<lambda> (p,n,s). (p,n,CN m c s)) (rsplit0 a)"
  4.3545 +  "rsplit0 (CF c t s) = rsplit0 (Add (Mul c (Floor t)) s)"
  4.3546 +  "rsplit0 (Mul c a) = map (\<lambda> (p,n,s). (p,c*n,Mul c s)) (rsplit0 a)"
  4.3547 +  "rsplit0 t = [(T,0,t)]"
  4.3548 +
  4.3549 +lemma not_rl[simp]: "isrlfm p \<Longrightarrow> isrlfm (not p)"
  4.3550 +  by (induct p rule: isrlfm.induct, auto)
  4.3551 +lemma conj_rl[simp]: "isrlfm p \<Longrightarrow> isrlfm q \<Longrightarrow> isrlfm (conj p q)"
  4.3552 +  using conj_def by (cases p, auto)
  4.3553 +lemma disj_rl[simp]: "isrlfm p \<Longrightarrow> isrlfm q \<Longrightarrow> isrlfm (disj p q)"
  4.3554 +  using disj_def by (cases p, auto)
  4.3555 +
  4.3556 +
  4.3557 +lemma rsplit0_cs:
  4.3558 +  shows "\<forall> (p,n,s) \<in> set (rsplit0 t). 
  4.3559 +  (Ifm (x#bs) p \<longrightarrow>  (Inum (x#bs) t = Inum (x#bs) (CN 0 n s))) \<and> numbound0 s \<and> isrlfm p" 
  4.3560 +  (is "\<forall> (p,n,s) \<in> ?SS t. (?I p \<longrightarrow> ?N t = ?N (CN 0 n s)) \<and> _ \<and> _ ")
  4.3561 +proof(induct t rule: rsplit0.induct)
  4.3562 +  case (5 a) 
  4.3563 +  let ?p = "\<lambda> (p,n,s) j. fp p n s j"
  4.3564 +  let ?f = "(\<lambda> (p,n,s) j. (?p (p,n,s) j, (0::int),Add (Floor s) (C j)))"
  4.3565 +  let ?J = "\<lambda> n. if n>0 then iupt (0,n) else iupt (n,0)"
  4.3566 +  let ?ff=" (\<lambda> (p,n,s). if n= 0 then [(p,0,Floor s)] else map (?f (p,n,s)) (?J n))"
  4.3567 +  have int_cases: "\<forall> (i::int). i= 0 \<or> i < 0 \<or> i > 0" by arith
  4.3568 +  have U1: "(UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n=0} (\<lambda> (p,n,s). set (?ff (p,n,s)))) = 
  4.3569 +    (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n=0} (\<lambda> (p,n,s). set [(p,0,Floor s)]))" by auto
  4.3570 +  have U2': "\<forall> (p,n,s) \<in> {(p,n,s). (p,n,s) \<in> ?SS a \<and> n>0}. 
  4.3571 +    ?ff (p,n,s) = map (?f(p,n,s)) (iupt(0,n))" by auto
  4.3572 +  hence U2: "(UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n>0} (\<lambda> (p,n,s). set (?ff (p,n,s)))) = 
  4.3573 +    (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n>0} (\<lambda> (p,n,s). 
  4.3574 +    set (map (?f(p,n,s)) (iupt(0,n)))))"
  4.3575 +  proof-
  4.3576 +    fix M :: "('a\<times>'b\<times>'c) set" and f :: "('a\<times>'b\<times>'c) \<Rightarrow> 'd list" and g
  4.3577 +    assume "\<forall> (a,b,c) \<in> M. f (a,b,c) = g a b c"
  4.3578 +    thus "(UNION M (\<lambda> (a,b,c). set (f (a,b,c)))) = (UNION M (\<lambda> (a,b,c). set (g a b c)))"
  4.3579 +      by (auto simp add: split_def)
  4.3580 +  qed
  4.3581 +  have U3': "\<forall> (p,n,s) \<in> {(p,n,s). (p,n,s) \<in> ?SS a \<and> n<0}. ?ff (p,n,s) = map (?f(p,n,s)) (iupt(n,0))"
  4.3582 +    by auto
  4.3583 +  hence U3: "(UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n<0} (\<lambda> (p,n,s). set (?ff (p,n,s)))) = 
  4.3584 +    (UNION {(p,n,s). (p,n,s)\<in> ?SS a\<and>n<0} (\<lambda>(p,n,s). set (map (?f(p,n,s)) (iupt(n,0)))))"
  4.3585 +      proof-
  4.3586 +    fix M :: "('a\<times>'b\<times>'c) set" and f :: "('a\<times>'b\<times>'c) \<Rightarrow> 'd list" and g
  4.3587 +    assume "\<forall> (a,b,c) \<in> M. f (a,b,c) = g a b c"
  4.3588 +    thus "(UNION M (\<lambda> (a,b,c). set (f (a,b,c)))) = (UNION M (\<lambda> (a,b,c). set (g a b c)))"
  4.3589 +      by (auto simp add: split_def)
  4.3590 +  qed
  4.3591 +  have "?SS (Floor a) = UNION (?SS a) (\<lambda>x. set (?ff x))" 
  4.3592 +    by (auto simp add: foldl_conv_concat)
  4.3593 +  also have "\<dots> = UNION (?SS a) (\<lambda> (p,n,s). set (?ff (p,n,s)))" by auto
  4.3594 +  also have "\<dots> = 
  4.3595 +    ((UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n=0} (\<lambda> (p,n,s). set (?ff (p,n,s)))) Un 
  4.3596 +    (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n>0} (\<lambda> (p,n,s). set (?ff (p,n,s)))) Un 
  4.3597 +    (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n<0} (\<lambda> (p,n,s). set (?ff (p,n,s)))))" 
  4.3598 +    using int_cases[rule_format] by blast
  4.3599 +  also have "\<dots> =  
  4.3600 +    ((UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n=0} (\<lambda> (p,n,s). set [(p,0,Floor s)])) Un 
  4.3601 +   (UNION {(p,n,s). (p,n,s)\<in> ?SS a\<and>n>0} (\<lambda>(p,n,s). set(map(?f(p,n,s)) (iupt(0,n))))) Un 
  4.3602 +   (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n<0} (\<lambda> (p,n,s). 
  4.3603 +    set (map (?f(p,n,s)) (iupt(n,0))))))" by (simp only: U1 U2 U3)
  4.3604 +  also have "\<dots> =  
  4.3605 +    ((UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n=0} (\<lambda> (p,n,s). {(p,0,Floor s)})) Un 
  4.3606 +    (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n>0} (\<lambda> (p,n,s). (?f(p,n,s)) ` {0 .. n})) Un 
  4.3607 +    (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n<0} (\<lambda> (p,n,s). (?f(p,n,s)) ` {n .. 0})))"
  4.3608 +    by (simp only: set_map iupt_set set.simps)
  4.3609 +  also have "\<dots> =   
  4.3610 +    ((UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n=0} (\<lambda> (p,n,s). {(p,0,Floor s)})) Un 
  4.3611 +    (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n>0} (\<lambda> (p,n,s). {?f(p,n,s) j| j. j\<in> {0 .. n}})) Un 
  4.3612 +    (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n<0} (\<lambda> (p,n,s).  {?f(p,n,s) j| j. j\<in> {n .. 0}})))" by blast
  4.3613 +  finally 
  4.3614 +  have FS: "?SS (Floor a) =   
  4.3615 +    ((UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n=0} (\<lambda> (p,n,s). {(p,0,Floor s)})) Un 
  4.3616 +    (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n>0} (\<lambda> (p,n,s). {?f(p,n,s) j| j. j\<in> {0 .. n}})) Un 
  4.3617 +    (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n<0} (\<lambda> (p,n,s).  {?f(p,n,s) j| j. j\<in> {n .. 0}})))"    by blast
  4.3618 +  show ?case
  4.3619 +    proof(simp only: FS, clarsimp simp del: Ifm.simps Inum.simps, -)
  4.3620 +      fix p n s
  4.3621 +      let ?ths = "(?I p \<longrightarrow> (?N (Floor a) = ?N (CN 0 n s))) \<and> numbound0 s \<and> isrlfm p"
  4.3622 +      assume "(\<exists>ba. (p, 0, ba) \<in> set (rsplit0 a) \<and> n = 0 \<and> s = Floor ba) \<or>
  4.3623 +       (\<exists>ab ac ba.
  4.3624 +           (ab, ac, ba) \<in> set (rsplit0 a) \<and>
  4.3625 +           0 < ac \<and>
  4.3626 +           (\<exists>j. p = fp ab ac ba j \<and>
  4.3627 +                n = 0 \<and> s = Add (Floor ba) (C j) \<and> 0 \<le> j \<and> j \<le> ac)) \<or>
  4.3628 +       (\<exists>ab ac ba.
  4.3629 +           (ab, ac, ba) \<in> set (rsplit0 a) \<and>
  4.3630 +           ac < 0 \<and>
  4.3631 +           (\<exists>j. p = fp ab ac ba j \<and>
  4.3632 +                n = 0 \<and> s = Add (Floor ba) (C j) \<and> ac \<le> j \<and> j \<le> 0))"
  4.3633 +      moreover 
  4.3634 +      {fix s'
  4.3635 +	assume "(p, 0, s') \<in> ?SS a" and "n = 0" and "s = Floor s'"
  4.3636 +	hence ?ths using prems by auto}
  4.3637 +      moreover
  4.3638 +      {	fix p' n' s' j
  4.3639 +	assume pns: "(p', n', s') \<in> ?SS a" 
  4.3640 +	  and np: "0 < n'" 
  4.3641 +	  and p_def: "p = ?p (p',n',s') j" 
  4.3642 +	  and n0: "n = 0" 
  4.3643 +	  and s_def: "s = (Add (Floor s') (C j))" 
  4.3644 +	  and jp: "0 \<le> j" and jn: "j \<le> n'"
  4.3645 +	from prems pns have H:"(Ifm ((x\<Colon>real) # (bs\<Colon>real list)) p' \<longrightarrow>
  4.3646 +          Inum (x # bs) a = Inum (x # bs) (CN 0 n' s')) \<and>
  4.3647 +          numbound0 s' \<and> isrlfm p'" by blast
  4.3648 +	hence nb: "numbound0 s'" by simp
  4.3649 +	from H have nf: "isrlfm (?p (p',n',s') j)" using fp_def np by (simp add: numsub_nb)
  4.3650 +	let ?nxs = "CN 0 n' s'"
  4.3651 +	let ?l = "floor (?N s') + j"
  4.3652 +	from H 
  4.3653 +	have "?I (?p (p',n',s') j) \<longrightarrow> 
  4.3654 +	  (((?N ?nxs \<ge> real ?l) \<and> (?N ?nxs < real (?l + 1))) \<and> (?N a = ?N ?nxs ))" 
  4.3655 +	  by (simp add: fp_def np algebra_simps numsub numadd numfloor)
  4.3656 +	also have "\<dots> \<longrightarrow> ((floor (?N ?nxs) = ?l) \<and> (?N a = ?N ?nxs ))"
  4.3657 +	  using floor_int_eq[where x="?N ?nxs" and n="?l"] by simp
  4.3658 +	moreover
  4.3659 +	have "\<dots> \<longrightarrow> (?N (Floor a) = ?N ((Add (Floor s') (C j))))" by simp
  4.3660 +	ultimately have "?I (?p (p',n',s') j) \<longrightarrow> (?N (Floor a) = ?N ((Add (Floor s') (C j))))"
  4.3661 +	  by blast
  4.3662 +	with s_def n0 p_def nb nf have ?ths by auto}
  4.3663 +      moreover
  4.3664 +      {fix p' n' s' j
  4.3665 +	assume pns: "(p', n', s') \<in> ?SS a" 
  4.3666 +	  and np: "n' < 0" 
  4.3667 +	  and p_def: "p = ?p (p',n',s') j" 
  4.3668 +	  and n0: "n = 0" 
  4.3669 +	  and s_def: "s = (Add (Floor s') (C j))" 
  4.3670 +	  and jp: "n' \<le> j" and jn: "j \<le> 0"
  4.3671 +	from prems pns have H:"(Ifm ((x\<Colon>real) # (bs\<Colon>real list)) p' \<longrightarrow>
  4.3672 +          Inum (x # bs) a = Inum (x # bs) (CN 0 n' s')) \<and>
  4.3673 +          numbound0 s' \<and> isrlfm p'" by blast
  4.3674 +	hence nb: "numbound0 s'" by simp
  4.3675 +	from H have nf: "isrlfm (?p (p',n',s') j)" using fp_def np by (simp add: numneg_nb)
  4.3676 +	let ?nxs = "CN 0 n' s'"
  4.3677 +	let ?l = "floor (?N s') + j"
  4.3678 +	from H 
  4.3679 +	have "?I (?p (p',n',s') j) \<longrightarrow> 
  4.3680 +	  (((?N ?nxs \<ge> real ?l) \<and> (?N ?nxs < real (?l + 1))) \<and> (?N a = ?N ?nxs ))" 
  4.3681 +	  by (simp add: np fp_def algebra_simps numneg numfloor numadd numsub)
  4.3682 +	also have "\<dots> \<longrightarrow> ((floor (?N ?nxs) = ?l) \<and> (?N a = ?N ?nxs ))"
  4.3683 +	  using floor_int_eq[where x="?N ?nxs" and n="?l"] by simp
  4.3684 +	moreover
  4.3685 +	have "\<dots> \<longrightarrow> (?N (Floor a) = ?N ((Add (Floor s') (C j))))"  by simp
  4.3686 +	ultimately have "?I (?p (p',n',s') j) \<longrightarrow> (?N (Floor a) = ?N ((Add (Floor s') (C j))))"
  4.3687 +	  by blast
  4.3688 +	with s_def n0 p_def nb nf have ?ths by auto}
  4.3689 +      ultimately show ?ths by auto
  4.3690 +    qed
  4.3691 +next
  4.3692 +  case (3 a b) then show ?case
  4.3693 +  apply auto
  4.3694 +  apply (erule_tac x = "(aa, aaa, ba)" in ballE) apply simp_all
  4.3695 +  apply (erule_tac x = "(ab, ac, baa)" in ballE) apply simp_all
  4.3696 +  done
  4.3697 +qed (auto simp add: Let_def split_def algebra_simps conj_rl)
  4.3698 +
  4.3699 +lemma real_in_int_intervals: 
  4.3700 +  assumes xb: "real m \<le> x \<and> x < real ((n::int) + 1)"
  4.3701 +  shows "\<exists> j\<in> {m.. n}. real j \<le> x \<and> x < real (j+1)" (is "\<exists> j\<in> ?N. ?P j")
  4.3702 +by (rule bexI[where P="?P" and x="floor x" and A="?N"]) 
  4.3703 +(auto simp add: floor_less_eq[where x="x" and a="n+1", simplified] xb[simplified] floor_mono2[where x="real m" and y="x", OF conjunct1[OF xb], simplified floor_real_of_int[where n="m"]])
  4.3704 +
  4.3705 +lemma rsplit0_complete:
  4.3706 +  assumes xp:"0 \<le> x" and x1:"x < 1"
  4.3707 +  shows "\<exists> (p,n,s) \<in> set (rsplit0 t). Ifm (x#bs) p" (is "\<exists> (p,n,s) \<in> ?SS t. ?I p")
  4.3708 +proof(induct t rule: rsplit0.induct)
  4.3709 +  case (2 a b) 
  4.3710 +  from prems have "\<exists> (pa,na,sa) \<in> ?SS a. ?I pa" by auto
  4.3711 +  then obtain "pa" "na" "sa" where pa: "(pa,na,sa)\<in> ?SS a \<and> ?I pa" by blast
  4.3712 +  from prems have "\<exists> (pb,nb,sb) \<in> ?SS b. ?I pb" by auto
  4.3713 +  then obtain "pb" "nb" "sb" where pb: "(pb,nb,sb)\<in> ?SS b \<and> ?I pb" by blast
  4.3714 +  from pa pb have th: "((pa,na,sa),(pb,nb,sb)) \<in> set[(x,y). x\<leftarrow>rsplit0 a, y\<leftarrow>rsplit0 b]"
  4.3715 +    by (auto)
  4.3716 +  let ?f="(\<lambda> ((p,n,t),(q,m,s)). (And p q, n+m, Add t s))"
  4.3717 +  from imageI[OF th, where f="?f"] have "?f ((pa,na,sa),(pb,nb,sb)) \<in> ?SS (Add a b)"
  4.3718 +    by (simp add: Let_def)
  4.3719 +  hence "(And pa pb, na +nb, Add sa sb) \<in> ?SS (Add a b)" by simp
  4.3720 +  moreover from pa pb have "?I (And pa pb)" by simp
  4.3721 +  ultimately show ?case by blast
  4.3722 +next
  4.3723 +  case (5 a) 
  4.3724 +  let ?p = "\<lambda> (p,n,s) j. fp p n s j"
  4.3725 +  let ?f = "(\<lambda> (p,n,s) j. (?p (p,n,s) j, (0::int),(Add (Floor s) (C j))))"
  4.3726 +  let ?J = "\<lambda> n. if n>0 then iupt (0,n) else iupt (n,0)"
  4.3727 +  let ?ff=" (\<lambda> (p,n,s). if n= 0 then [(p,0,Floor s)] else map (?f (p,n,s)) (?J n))"
  4.3728 +  have int_cases: "\<forall> (i::int). i= 0 \<or> i < 0 \<or> i > 0" by arith
  4.3729 +  have U1: "(UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n=0} (\<lambda> (p,n,s). set (?ff (p,n,s)))) = (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n=0} (\<lambda> (p,n,s). set [(p,0,Floor s)]))" by auto
  4.3730 +  have U2': "\<forall> (p,n,s) \<in> {(p,n,s). (p,n,s) \<in> ?SS a \<and> n>0}. ?ff (p,n,s) = map (?f(p,n,s)) (iupt(0,n))"
  4.3731 +    by auto
  4.3732 +  hence U2: "(UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n>0} (\<lambda> (p,n,s). set (?ff (p,n,s)))) = (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n>0} (\<lambda> (p,n,s). set (map (?f(p,n,s)) (iupt(0,n)))))"
  4.3733 +  proof-
  4.3734 +    fix M :: "('a\<times>'b\<times>'c) set" and f :: "('a\<times>'b\<times>'c) \<Rightarrow> 'd list" and g
  4.3735 +    assume "\<forall> (a,b,c) \<in> M. f (a,b,c) = g a b c"
  4.3736 +    thus "(UNION M (\<lambda> (a,b,c). set (f (a,b,c)))) = (UNION M (\<lambda> (a,b,c). set (g a b c)))"
  4.3737 +      by (auto simp add: split_def)
  4.3738 +  qed
  4.3739 +  have U3': "\<forall> (p,n,s) \<in> {(p,n,s). (p,n,s) \<in> ?SS a \<and> n<0}. ?ff (p,n,s) = map (?f(p,n,s)) (iupt(n,0))"
  4.3740 +    by auto
  4.3741 +  hence U3: "(UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n<0} (\<lambda> (p,n,s). set (?ff (p,n,s)))) = (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n<0} (\<lambda> (p,n,s). set (map (?f(p,n,s)) (iupt(n,0)))))"
  4.3742 +  proof-
  4.3743 +    fix M :: "('a\<times>'b\<times>'c) set" and f :: "('a\<times>'b\<times>'c) \<Rightarrow> 'd list" and g
  4.3744 +    assume "\<forall> (a,b,c) \<in> M. f (a,b,c) = g a b c"
  4.3745 +    thus "(UNION M (\<lambda> (a,b,c). set (f (a,b,c)))) = (UNION M (\<lambda> (a,b,c). set (g a b c)))"
  4.3746 +      by (auto simp add: split_def)
  4.3747 +  qed
  4.3748 +
  4.3749 +  have "?SS (Floor a) = UNION (?SS a) (\<lambda>x. set (?ff x))" by (auto simp add: foldl_conv_concat) 
  4.3750 +  also have "\<dots> = UNION (?SS a) (\<lambda> (p,n,s). set (?ff (p,n,s)))" by auto
  4.3751 +  also have "\<dots> = 
  4.3752 +    ((UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n=0} (\<lambda> (p,n,s). set (?ff (p,n,s)))) Un 
  4.3753 +    (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n>0} (\<lambda> (p,n,s). set (?ff (p,n,s)))) Un 
  4.3754 +    (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n<0} (\<lambda> (p,n,s). set (?ff (p,n,s)))))" 
  4.3755 +    using int_cases[rule_format] by blast
  4.3756 +  also have "\<dots> =  
  4.3757 +    ((UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n=0} (\<lambda> (p,n,s). set [(p,0,Floor s)])) Un 
  4.3758 +    (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n>0} (\<lambda> (p,n,s). set (map (?f(p,n,s)) (iupt(0,n))))) Un 
  4.3759 +    (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n<0} (\<lambda> (p,n,s). set (map (?f(p,n,s)) (iupt(n,0))))))" by (simp only: U1 U2 U3)
  4.3760 +  also have "\<dots> =  
  4.3761 +    ((UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n=0} (\<lambda> (p,n,s). {(p,0,Floor s)})) Un 
  4.3762 +    (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n>0} (\<lambda> (p,n,s). (?f(p,n,s)) ` {0 .. n})) Un 
  4.3763 +    (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n<0} (\<lambda> (p,n,s). (?f(p,n,s)) ` {n .. 0})))"
  4.3764 +    by (simp only: set_map iupt_set set.simps)
  4.3765 +  also have "\<dots> =   
  4.3766 +    ((UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n=0} (\<lambda> (p,n,s). {(p,0,Floor s)})) Un 
  4.3767 +    (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n>0} (\<lambda> (p,n,s). {?f(p,n,s) j| j. j\<in> {0 .. n}})) Un 
  4.3768 +    (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n<0} (\<lambda> (p,n,s).  {?f(p,n,s) j| j. j\<in> {n .. 0}})))" by blast
  4.3769 +  finally 
  4.3770 +  have FS: "?SS (Floor a) =   
  4.3771 +    ((UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n=0} (\<lambda> (p,n,s). {(p,0,Floor s)})) Un 
  4.3772 +    (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n>0} (\<lambda> (p,n,s). {?f(p,n,s) j| j. j\<in> {0 .. n}})) Un 
  4.3773 +    (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n<0} (\<lambda> (p,n,s).  {?f(p,n,s) j| j. j\<in> {n .. 0}})))"    by blast
  4.3774 +  from prems have "\<exists> (p,n,s) \<in> ?SS a. ?I p" by auto
  4.3775 +  then obtain "p" "n" "s" where pns: "(p,n,s) \<in> ?SS a \<and> ?I p" by blast
  4.3776 +  let ?N = "\<lambda> t. Inum (x#bs) t"
  4.3777 +  from rsplit0_cs[rule_format] pns have ans:"(?N a = ?N (CN 0 n s)) \<and> numbound0 s \<and> isrlfm p"
  4.3778 +    by auto
  4.3779 +  
  4.3780 +  have "n=0 \<or> n >0 \<or> n <0" by arith
  4.3781 +  moreover {assume "n=0" hence ?case using pns by (simp only: FS) auto }
  4.3782 +  moreover
  4.3783 +  {
  4.3784 +    assume np: "n > 0"
  4.3785 +    from real_of_int_floor_le[where r="?N s"] have "?N (Floor s) \<le> ?N s" by simp
  4.3786 +    also from mult_left_mono[OF xp] np have "?N s \<le> real n * x + ?N s" by simp
  4.3787 +    finally have "?N (Floor s) \<le> real n * x + ?N s" .
  4.3788 +    moreover
  4.3789 +    {from mult_strict_left_mono[OF x1] np 
  4.3790 +      have "real n *x + ?N s < real n + ?N s" by simp
  4.3791 +      also from real_of_int_floor_add_one_gt[where r="?N s"] 
  4.3792 +      have "\<dots> < real n + ?N (Floor s) + 1" by simp
  4.3793 +      finally have "real n *x + ?N s < ?N (Floor s) + real (n+1)" by simp}
  4.3794 +    ultimately have "?N (Floor s) \<le> real n *x + ?N s\<and> real n *x + ?N s < ?N (Floor s) + real (n+1)" by simp
  4.3795 +    hence th: "0 \<le> real n *x + ?N s - ?N (Floor s) \<and> real n *x + ?N s - ?N (Floor s) < real (n+1)" by simp
  4.3796 +    from real_in_int_intervals th have  "\<exists> j\<in> {0 .. n}. real j \<le> real n *x + ?N s - ?N (Floor s)\<and> real n *x + ?N s - ?N (Floor s) < real (j+1)" by simp
  4.3797 +    
  4.3798 +    hence "\<exists> j\<in> {0 .. n}. 0 \<le> real n *x + ?N s - ?N (Floor s) - real j \<and> real n *x + ?N s - ?N (Floor s) - real (j+1) < 0"
  4.3799 +      by(simp only: myl[rule_format, where b="real n * x + Inum (x # bs) s - Inum (x # bs) (Floor s)"] less_iff_diff_less_0[where a="real n *x + ?N s - ?N (Floor s)"]) 
  4.3800 +    hence "\<exists> j\<in> {0.. n}. ?I (?p (p,n,s) j)"
  4.3801 +      using pns by (simp add: fp_def np algebra_simps numsub numadd)
  4.3802 +    then obtain "j" where j_def: "j\<in> {0 .. n} \<and> ?I (?p (p,n,s) j)" by blast
  4.3803 +    hence "\<exists>x \<in> {?p (p,n,s) j |j. 0\<le> j \<and> j \<le> n }. ?I x" by auto
  4.3804 +    hence ?case using pns 
  4.3805 +      by (simp only: FS,simp add: bex_Un) 
  4.3806 +    (rule disjI2, rule disjI1,rule exI [where x="p"],
  4.3807 +      rule exI [where x="n"],rule exI [where x="s"],simp_all add: np)
  4.3808 +  }
  4.3809 +  moreover
  4.3810 +  { assume nn: "n < 0" hence np: "-n >0" by simp
  4.3811 +    from real_of_int_floor_le[where r="?N s"] have "?N (Floor s) + 1 > ?N s" by simp
  4.3812 +    moreover from mult_left_mono_neg[OF xp] nn have "?N s \<ge> real n * x + ?N s" by simp
  4.3813 +    ultimately have "?N (Floor s) + 1 > real n * x + ?N s" by arith 
  4.3814 +    moreover
  4.3815 +    {from mult_strict_left_mono_neg[OF x1, where c="real n"] nn
  4.3816 +      have "real n *x + ?N s \<ge> real n + ?N s" by simp 
  4.3817 +      moreover from real_of_int_floor_le[where r="?N s"]  have "real n + ?N s \<ge> real n + ?N (Floor s)" by simp
  4.3818 +      ultimately have "real n *x + ?N s \<ge> ?N (Floor s) + real n" 
  4.3819 +	by (simp only: algebra_simps)}
  4.3820 +    ultimately have "?N (Floor s) + real n \<le> real n *x + ?N s\<and> real n *x + ?N s < ?N (Floor s) + real (1::int)" by simp
  4.3821 +    hence th: "real n \<le> real n *x + ?N s - ?N (Floor s) \<and> real n *x + ?N s - ?N (Floor s) < real (1::int)" by simp
  4.3822 +    have th1: "\<forall> (a::real). (- a > 0) = (a < 0)" by auto
  4.3823 +    have th2: "\<forall> (a::real). (0 \<ge> - a) = (a \<ge> 0)" by auto
  4.3824 +    from real_in_int_intervals th  have  "\<exists> j\<in> {n .. 0}. real j \<le> real n *x + ?N s - ?N (Floor s)\<and> real n *x + ?N s - ?N (Floor s) < real (j+1)" by simp
  4.3825 +    
  4.3826 +    hence "\<exists> j\<in> {n .. 0}. 0 \<le> real n *x + ?N s - ?N (Floor s) - real j \<and> real n *x + ?N s - ?N (Floor s) - real (j+1) < 0"
  4.3827 +      by(simp only: myl[rule_format, where b="real n * x + Inum (x # bs) s - Inum (x # bs) (Floor s)"] less_iff_diff_less_0[where a="real n *x + ?N s - ?N (Floor s)"]) 
  4.3828 +    hence "\<exists> j\<in> {n .. 0}. 0 \<ge> - (real n *x + ?N s - ?N (Floor s) - real j) \<and> - (real n *x + ?N s - ?N (Floor s) - real (j+1)) > 0" by (simp only: th1[rule_format] th2[rule_format])
  4.3829 +    hence "\<exists> j\<in> {n.. 0}. ?I (?p (p,n,s) j)"
  4.3830 +      using pns by (simp add: fp_def nn diff_def add_ac mult_ac numfloor numadd numneg
  4.3831 +	del: diff_less_0_iff_less diff_le_0_iff_le) 
  4.3832 +    then obtain "j" where j_def: "j\<in> {n .. 0} \<and> ?I (?p (p,n,s) j)" by blast
  4.3833 +    hence "\<exists>x \<in> {?p (p,n,s) j |j. n\<le> j \<and> j \<le> 0 }. ?I x" by auto
  4.3834 +    hence ?case using pns 
  4.3835 +      by (simp only: FS,simp add: bex_Un)
  4.3836 +    (rule disjI2, rule disjI2,rule exI [where x="p"],
  4.3837 +      rule exI [where x="n"],rule exI [where x="s"],simp_all add: nn)
  4.3838 +  }
  4.3839 +  ultimately show ?case by blast
  4.3840 +qed (auto simp add: Let_def split_def)
  4.3841 +
  4.3842 +    (* Linearize a formula where Bound 0 ranges over [0,1) *)
  4.3843 +
  4.3844 +constdefs rsplit :: "(int \<Rightarrow> num \<Rightarrow> fm) \<Rightarrow> num \<Rightarrow> fm"
  4.3845 +  "rsplit f a \<equiv> foldr disj (map (\<lambda> (\<phi>, n, s). conj \<phi> (f n s)) (rsplit0 a)) F"
  4.3846 +
  4.3847 +lemma foldr_disj_map: "Ifm bs (foldr disj (map f xs) F) = (\<exists> x \<in> set xs. Ifm bs (f x))"
  4.3848 +by(induct xs, simp_all)
  4.3849 +
  4.3850 +lemma foldr_conj_map: "Ifm bs (foldr conj (map f xs) T) = (\<forall> x \<in> set xs. Ifm bs (f x))"
  4.3851 +by(induct xs, simp_all)
  4.3852 +
  4.3853 +lemma foldr_disj_map_rlfm: 
  4.3854 +  assumes lf: "\<forall> n s. numbound0 s \<longrightarrow> isrlfm (f n s)"
  4.3855 +  and \<phi>: "\<forall> (\<phi>,n,s) \<in> set xs. numbound0 s \<and> isrlfm \<phi>"
  4.3856 +  shows "isrlfm (foldr disj (map (\<lambda> (\<phi>, n, s). conj \<phi> (f n s)) xs) F)"
  4.3857 +using lf \<phi> by (induct xs, auto)
  4.3858 +
  4.3859 +lemma rsplit_ex: "Ifm bs (rsplit f a) = (\<exists> (\<phi>,n,s) \<in> set (rsplit0 a). Ifm bs (conj \<phi> (f n s)))"
  4.3860 +using foldr_disj_map[where xs="rsplit0 a"] rsplit_def by (simp add: split_def)
  4.3861 +
  4.3862 +lemma rsplit_l: assumes lf: "\<forall> n s. numbound0 s \<longrightarrow> isrlfm (f n s)"
  4.3863 +  shows "isrlfm (rsplit f a)"
  4.3864 +proof-
  4.3865 +  from rsplit0_cs[where t="a"] have th: "\<forall> (\<phi>,n,s) \<in> set (rsplit0 a). numbound0 s \<and> isrlfm \<phi>" by blast
  4.3866 +  from foldr_disj_map_rlfm[OF lf th] rsplit_def show ?thesis by simp
  4.3867 +qed
  4.3868 +
  4.3869 +lemma rsplit: 
  4.3870 +  assumes xp: "x \<ge> 0" and x1: "x < 1"
  4.3871 +  and f: "\<forall> a n s. Inum (x#bs) a = Inum (x#bs) (CN 0 n s) \<and> numbound0 s \<longrightarrow> (Ifm (x#bs) (f n s) = Ifm (x#bs) (g a))"
  4.3872 +  shows "Ifm (x#bs) (rsplit f a) = Ifm (x#bs) (g a)"
  4.3873 +proof(auto)
  4.3874 +  let ?I = "\<lambda>x p. Ifm (x#bs) p"
  4.3875 +  let ?N = "\<lambda> x t. Inum (x#bs) t"
  4.3876 +  assume "?I x (rsplit f a)"
  4.3877 +  hence "\<exists> (\<phi>,n,s) \<in> set (rsplit0 a). ?I x (And \<phi> (f n s))" using rsplit_ex by simp
  4.3878 +  then obtain "\<phi>" "n" "s" where fnsS:"(\<phi>,n,s) \<in> set (rsplit0 a)" and "?I x (And \<phi> (f n s))" by blast
  4.3879 +  hence \<phi>: "?I x \<phi>" and fns: "?I x (f n s)" by auto
  4.3880 +  from rsplit0_cs[where t="a" and bs="bs" and x="x", rule_format, OF fnsS] \<phi> 
  4.3881 +  have th: "(?N x a = ?N x (CN 0 n s)) \<and> numbound0 s" by auto
  4.3882 +  from f[rule_format, OF th] fns show "?I x (g a)" by simp
  4.3883 +next
  4.3884 +  let ?I = "\<lambda>x p. Ifm (x#bs) p"
  4.3885 +  let ?N = "\<lambda> x t. Inum (x#bs) t"
  4.3886 +  assume ga: "?I x (g a)"
  4.3887 +  from rsplit0_complete[OF xp x1, where bs="bs" and t="a"] 
  4.3888 +  obtain "\<phi>" "n" "s" where fnsS:"(\<phi>,n,s) \<in> set (rsplit0 a)" and fx: "?I x \<phi>" by blast
  4.3889 +  from rsplit0_cs[where t="a" and x="x" and bs="bs"] fnsS fx
  4.3890 +  have ans: "?N x a = ?N x (CN 0 n s)" and nb: "numbound0 s" by auto
  4.3891 +  with ga f have "?I x (f n s)" by auto
  4.3892 +  with rsplit_ex fnsS fx show "?I x (rsplit f a)" by auto
  4.3893 +qed
  4.3894 +
  4.3895 +definition lt :: "int \<Rightarrow> num \<Rightarrow> fm" where
  4.3896 +  lt_def: "lt c t = (if c = 0 then (Lt t) else if c > 0 then (Lt (CN 0 c t)) 
  4.3897 +                        else (Gt (CN 0 (-c) (Neg t))))"
  4.3898 +
  4.3899 +definition  le :: "int \<Rightarrow> num \<Rightarrow> fm" where
  4.3900 +  le_def: "le c t = (if c = 0 then (Le t) else if c > 0 then (Le (CN 0 c t)) 
  4.3901 +                        else (Ge (CN 0 (-c) (Neg t))))"
  4.3902 +
  4.3903 +definition  gt :: "int \<Rightarrow> num \<Rightarrow> fm" where
  4.3904 +  gt_def: "gt c t = (if c = 0 then (Gt t) else if c > 0 then (Gt (CN 0 c t)) 
  4.3905 +                        else (Lt (CN 0 (-c) (Neg t))))"
  4.3906 +
  4.3907 +definition  ge :: "int \<Rightarrow> num \<Rightarrow> fm" where
  4.3908 +  ge_def: "ge c t = (if c = 0 then (Ge t) else if c > 0 then (Ge (CN 0 c t)) 
  4.3909 +                        else (Le (CN 0 (-c) (Neg t))))"
  4.3910 +
  4.3911 +definition  eq :: "int \<Rightarrow> num \<Rightarrow> fm" where
  4.3912 +  eq_def: "eq c t = (if c = 0 then (Eq t) else if c > 0 then (Eq (CN 0 c t)) 
  4.3913 +                        else (Eq (CN 0 (-c) (Neg t))))"
  4.3914 +
  4.3915 +definition neq :: "int \<Rightarrow> num \<Rightarrow> fm" where
  4.3916 +  neq_def: "neq c t = (if c = 0 then (NEq t) else if c > 0 then (NEq (CN 0 c t)) 
  4.3917 +                        else (NEq (CN 0 (-c) (Neg t))))"
  4.3918 +
  4.3919 +lemma lt_mono: "\<forall> a n s. Inum (x#bs) a = Inum (x#bs) (CN 0 n s) \<and> numbound0 s \<longrightarrow> Ifm (x#bs) (lt n s) = Ifm (x#bs) (Lt a)"
  4.3920 +  (is "\<forall> a n s . ?N a = ?N (CN 0 n s) \<and> _\<longrightarrow> ?I (lt n s) = ?I (Lt a)")
  4.3921 +proof(clarify)
  4.3922 +  fix a n s
  4.3923 +  assume H: "?N a = ?N (CN 0 n s)"
  4.3924 +  show "?I (lt n s) = ?I (Lt a)" using H by (cases "n=0", (simp add: lt_def))
  4.3925 +  (cases "n > 0", simp_all add: lt_def algebra_simps myless[rule_format, where b="0"])
  4.3926 +qed
  4.3927 +
  4.3928 +lemma lt_l: "isrlfm (rsplit lt a)"
  4.3929 +  by (rule rsplit_l[where f="lt" and a="a"], auto simp add: lt_def,
  4.3930 +    case_tac s, simp_all, case_tac "nat", simp_all)
  4.3931 +
  4.3932 +lemma le_mono: "\<forall> a n s. Inum (x#bs) a = Inum (x#bs) (CN 0 n s) \<and> numbound0 s \<longrightarrow> Ifm (x#bs) (le n s) = Ifm (x#bs) (Le a)" (is "\<forall> a n s. ?N a = ?N (CN 0 n s) \<and> _ \<longrightarrow> ?I (le n s) = ?I (Le a)")
  4.3933 +proof(clarify)
  4.3934 +  fix a n s
  4.3935 +  assume H: "?N a = ?N (CN 0 n s)"
  4.3936 +  show "?I (le n s) = ?I (Le a)" using H by (cases "n=0", (simp add: le_def))
  4.3937 +  (cases "n > 0", simp_all add: le_def algebra_simps myl[rule_format, where b="0"])
  4.3938 +qed
  4.3939 +
  4.3940 +lemma le_l: "isrlfm (rsplit le a)"
  4.3941 +  by (rule rsplit_l[where f="le" and a="a"], auto simp add: le_def) 
  4.3942 +(case_tac s, simp_all, case_tac "nat",simp_all)
  4.3943 +
  4.3944 +lemma gt_mono: "\<forall> a n s. Inum (x#bs) a = Inum (x#bs) (CN 0 n s) \<and> numbound0 s \<longrightarrow> Ifm (x#bs) (gt n s) = Ifm (x#bs) (Gt a)" (is "\<forall> a n s. ?N a = ?N (CN 0 n s) \<and> _ \<longrightarrow> ?I (gt n s) = ?I (Gt a)")
  4.3945 +proof(clarify)
  4.3946 +  fix a n s
  4.3947 +  assume H: "?N a = ?N (CN 0 n s)"
  4.3948 +  show "?I (gt n s) = ?I (Gt a)" using H by (cases "n=0", (simp add: gt_def))
  4.3949 +  (cases "n > 0", simp_all add: gt_def algebra_simps myless[rule_format, where b="0"])
  4.3950 +qed
  4.3951 +lemma gt_l: "isrlfm (rsplit gt a)"
  4.3952 +  by (rule rsplit_l[where f="gt" and a="a"], auto simp add: gt_def) 
  4.3953 +(case_tac s, simp_all, case_tac "nat", simp_all)
  4.3954 +
  4.3955 +lemma ge_mono: "\<forall> a n s. Inum (x#bs) a = Inum (x#bs) (CN 0 n s) \<and> numbound0 s \<longrightarrow> Ifm (x#bs) (ge n s) = Ifm (x#bs) (Ge a)" (is "\<forall> a n s . ?N a = ?N (CN 0 n s) \<and> _ \<longrightarrow> ?I (ge n s) = ?I (Ge a)")
  4.3956 +proof(clarify)
  4.3957 +  fix a n s 
  4.3958 +  assume H: "?N a = ?N (CN 0 n s)"
  4.3959 +  show "?I (ge n s) = ?I (Ge a)" using H by (cases "n=0", (simp add: ge_def))
  4.3960 +  (cases "n > 0", simp_all add: ge_def algebra_simps myl[rule_format, where b="0"])
  4.3961 +qed
  4.3962 +lemma ge_l: "isrlfm (rsplit ge a)"
  4.3963 +  by (rule rsplit_l[where f="ge" and a="a"], auto simp add: ge_def) 
  4.3964 +(case_tac s, simp_all, case_tac "nat", simp_all)
  4.3965 +
  4.3966 +lemma eq_mono: "\<forall> a n s. Inum (x#bs) a = Inum (x#bs) (CN 0 n s) \<and> numbound0 s \<longrightarrow> Ifm (x#bs) (eq n s) = Ifm (x#bs) (Eq a)" (is "\<forall> a n s. ?N a = ?N (CN 0 n s) \<and> _ \<longrightarrow> ?I (eq n s) = ?I (Eq a)")
  4.3967 +proof(clarify)
  4.3968 +  fix a n s 
  4.3969 +  assume H: "?N a = ?N (CN 0 n s)"
  4.3970 +  show "?I (eq n s) = ?I (Eq a)" using H by (auto simp add: eq_def algebra_simps)
  4.3971 +qed
  4.3972 +lemma eq_l: "isrlfm (rsplit eq a)"
  4.3973 +  by (rule rsplit_l[where f="eq" and a="a"], auto simp add: eq_def) 
  4.3974 +(case_tac s, simp_all, case_tac"nat", simp_all)
  4.3975 +
  4.3976 +lemma neq_mono: "\<forall> a n s. Inum (x#bs) a = Inum (x#bs) (CN 0 n s) \<and> numbound0 s \<longrightarrow> Ifm (x#bs) (neq n s) = Ifm (x#bs) (NEq a)" (is "\<forall> a n s. ?N a = ?N (CN 0 n s) \<and> _ \<longrightarrow> ?I (neq n s) = ?I (NEq a)")
  4.3977 +proof(clarify)
  4.3978 +  fix a n s bs
  4.3979 +  assume H: "?N a = ?N (CN 0 n s)"
  4.3980 +  show "?I (neq n s) = ?I (NEq a)" using H by (auto simp add: neq_def algebra_simps)
  4.3981 +qed
  4.3982 +
  4.3983 +lemma neq_l: "isrlfm (rsplit neq a)"
  4.3984 +  by (rule rsplit_l[where f="neq" and a="a"], auto simp add: neq_def) 
  4.3985 +(case_tac s, simp_all, case_tac"nat", simp_all)
  4.3986 +
  4.3987 +lemma small_le: 
  4.3988 +  assumes u0:"0 \<le> u" and u1: "u < 1"
  4.3989 +  shows "(-u \<le> real (n::int)) = (0 \<le> n)"
  4.3990 +using u0 u1  by auto
  4.3991 +
  4.3992 +lemma small_lt: 
  4.3993 +  assumes u0:"0 \<le> u" and u1: "u < 1"
  4.3994 +  shows "(real (n::int) < real (m::int) - u) = (n < m)"
  4.3995 +using u0 u1  by auto
  4.3996 +
  4.3997 +lemma rdvd01_cs: 
  4.3998 +  assumes up: "u \<ge> 0" and u1: "u<1" and np: "real n > 0"
  4.3999 +  shows "(real (i::int) rdvd real (n::int) * u - s) = (\<exists> j\<in> {0 .. n - 1}. real n * u = s - real (floor s) + real j \<and> real i rdvd real (j - floor s))" (is "?lhs = ?rhs")
  4.4000 +proof-
  4.4001 +  let ?ss = "s - real (floor s)"
  4.4002 +  from real_of_int_floor_add_one_gt[where r="s", simplified myless[rule_format,where a="s"]] 
  4.4003 +    real_of_int_floor_le[where r="s"]  have ss0:"?ss \<ge> 0" and ss1:"?ss < 1" 
  4.4004 +    by (auto simp add: myl[rule_format, where b="s", symmetric] myless[rule_format, where a="?ss"])
  4.4005 +  from np have n0: "real n \<ge> 0" by simp
  4.4006 +  from mult_left_mono[OF up n0] mult_strict_left_mono[OF u1 np] 
  4.4007 +  have nu0:"real n * u - s \<ge> -s" and nun:"real n * u -s < real n - s" by auto  
  4.4008 +  from int_rdvd_real[where i="i" and x="real (n::int) * u - s"] 
  4.4009 +  have "real i rdvd real n * u - s = 
  4.4010 +    (i dvd floor (real n * u -s) \<and> (real (floor (real n * u - s)) = real n * u - s ))" 
  4.4011 +    (is "_ = (?DE)" is "_ = (?D \<and> ?E)") by simp
  4.4012 +  also have "\<dots> = (?DE \<and> real(floor (real n * u - s) + floor s)\<ge> -?ss 
  4.4013 +    \<and> real(floor (real n * u - s) + floor s)< real n - ?ss)" (is "_=(?DE \<and>real ?a \<ge> _ \<and> real ?a < _)")
  4.4014 +    using nu0 nun  by auto
  4.4015 +  also have "\<dots> = (?DE \<and> ?a \<ge> 0 \<and> ?a < n)" by(simp only: small_le[OF ss0 ss1] small_lt[OF ss0 ss1])
  4.4016 +  also have "\<dots> = (?DE \<and> (\<exists> j\<in> {0 .. (n - 1)}. ?a = j))" by simp
  4.4017 +  also have "\<dots> = (?DE \<and> (\<exists> j\<in> {0 .. (n - 1)}. real (\<lfloor>real n * u - s\<rfloor>) = real j - real \<lfloor>s\<rfloor> ))"
  4.4018 +    by (simp only: algebra_simps real_of_int_diff[symmetric] real_of_int_inject del: real_of_int_diff)
  4.4019 +  also have "\<dots> = ((\<exists> j\<in> {0 .. (n - 1)}. real n * u - s = real j - real \<lfloor>s\<rfloor> \<and> real i rdvd real n * u - s))" using int_rdvd_iff[where i="i" and t="\<lfloor>real n * u - s\<rfloor>"]
  4.4020 +    by (auto cong: conj_cong)
  4.4021 +  also have "\<dots> = ?rhs" by(simp cong: conj_cong) (simp add: algebra_simps )
  4.4022 +  finally show ?thesis .
  4.4023 +qed
  4.4024 +
  4.4025 +definition
  4.4026 +  DVDJ:: "int \<Rightarrow> int \<Rightarrow> num \<Rightarrow> fm"
  4.4027 +where
  4.4028 +  DVDJ_def: "DVDJ i n s = (foldr disj (map (\<lambda> j. conj (Eq (CN 0 n (Add s (Sub (Floor (Neg s)) (C j))))) (Dvd i (Sub (C j) (Floor (Neg s))))) (iupt(0,n - 1))) F)"
  4.4029 +
  4.4030 +definition
  4.4031 +  NDVDJ:: "int \<Rightarrow> int \<Rightarrow> num \<Rightarrow> fm"
  4.4032 +where
  4.4033 +  NDVDJ_def: "NDVDJ i n s = (foldr conj (map (\<lambda> j. disj (NEq (CN 0 n (Add s (Sub (Floor (Neg s)) (C j))))) (NDvd i (Sub (C j) (Floor (Neg s))))) (iupt(0,n - 1))) T)"
  4.4034 +
  4.4035 +lemma DVDJ_DVD: 
  4.4036 +  assumes xp:"x\<ge> 0" and x1: "x < 1" and np:"real n > 0"
  4.4037 +  shows "Ifm (x#bs) (DVDJ i n s) = Ifm (x#bs) (Dvd i (CN 0 n s))"
  4.4038 +proof-
  4.4039 +  let ?f = "\<lambda> j. conj (Eq(CN 0 n (Add s (Sub(Floor (Neg s)) (C j))))) (Dvd i (Sub (C j) (Floor (Neg s))))"
  4.4040 +  let ?s= "Inum (x#bs) s"
  4.4041 +  from foldr_disj_map[where xs="iupt(0,n - 1)" and bs="x#bs" and f="?f"]
  4.4042 +  have "Ifm (x#bs) (DVDJ i n s) = (\<exists> j\<in> {0 .. (n - 1)}. Ifm (x#bs) (?f j))" 
  4.4043 +    by (simp add: iupt_set np DVDJ_def del: iupt.simps)
  4.4044 +  also have "\<dots> = (\<exists> j\<in> {0 .. (n - 1)}. real n * x = (- ?s) - real (floor (- ?s)) + real j \<and> real i rdvd real (j - floor (- ?s)))" by (simp add: algebra_simps diff_def[symmetric])
  4.4045 +  also from rdvd01_cs[OF xp x1 np, where i="i" and s="-?s"] 
  4.4046 +  have "\<dots> = (real i rdvd real n * x - (-?s))" by simp
  4.4047 +  finally show ?thesis by simp
  4.4048 +qed
  4.4049 +
  4.4050 +lemma NDVDJ_NDVD: 
  4.4051 +  assumes xp:"x\<ge> 0" and x1: "x < 1" and np:"real n > 0"
  4.4052 +  shows "Ifm (x#bs) (NDVDJ i n s) = Ifm (x#bs) (NDvd i (CN 0 n s))"
  4.4053 +proof-
  4.4054 +  let ?f = "\<lambda> j. disj(NEq(CN 0 n (Add s (Sub (Floor (Neg s)) (C j))))) (NDvd i (Sub (C j) (Floor(Neg s))))"
  4.4055 +  let ?s= "Inum (x#bs) s"
  4.4056 +  from foldr_conj_map[where xs="iupt(0,n - 1)" and bs="x#bs" and f="?f"]
  4.4057 +  have "Ifm (x#bs) (NDVDJ i n s) = (\<forall> j\<in> {0 .. (n - 1)}. Ifm (x#bs) (?f j))" 
  4.4058 +    by (simp add: iupt_set np NDVDJ_def del: iupt.simps)
  4.4059 +  also have "\<dots> = (\<not> (\<exists> j\<in> {0 .. (n - 1)}. real n * x = (- ?s) - real (floor (- ?s)) + real j \<and> real i rdvd real (j - floor (- ?s))))" by (simp add: algebra_simps diff_def[symmetric])
  4.4060 +  also from rdvd01_cs[OF xp x1 np, where i="i" and s="-?s"] 
  4.4061 +  have "\<dots> = (\<not> (real i rdvd real n * x - (-?s)))" by simp
  4.4062 +  finally show ?thesis by simp
  4.4063 +qed  
  4.4064 +
  4.4065 +lemma foldr_disj_map_rlfm2: 
  4.4066 +  assumes lf: "\<forall> n . isrlfm (f n)"
  4.4067 +  shows "isrlfm (foldr disj (map f xs) F)"
  4.4068 +using lf by (induct xs, auto)
  4.4069 +lemma foldr_And_map_rlfm2: 
  4.4070 +  assumes lf: "\<forall> n . isrlfm (f n)"
  4.4071 +  shows "isrlfm (foldr conj (map f xs) T)"
  4.4072 +using lf by (induct xs, auto)
  4.4073 +
  4.4074 +lemma DVDJ_l: assumes ip: "i >0" and np: "n>0" and nb: "numbound0 s"
  4.4075 +  shows "isrlfm (DVDJ i n s)"
  4.4076 +proof-
  4.4077 +  let ?f="\<lambda>j. conj (Eq (CN 0 n (Add s (Sub (Floor (Neg s)) (C j)))))
  4.4078 +                         (Dvd i (Sub (C j) (Floor (Neg s))))"
  4.4079 +  have th: "\<forall> j. isrlfm (?f j)" using nb np by auto
  4.4080 +  from DVDJ_def foldr_disj_map_rlfm2[OF th] show ?thesis by simp 
  4.4081 +qed
  4.4082 +
  4.4083 +lemma NDVDJ_l: assumes ip: "i >0" and np: "n>0" and nb: "numbound0 s"
  4.4084 +  shows "isrlfm (NDVDJ i n s)"
  4.4085 +proof-
  4.4086 +  let ?f="\<lambda>j. disj (NEq (CN 0 n (Add s (Sub (Floor (Neg s)) (C j)))))
  4.4087 +                      (NDvd i (Sub (C j) (Floor (Neg s))))"
  4.4088 +  have th: "\<forall> j. isrlfm (?f j)" using nb np by auto
  4.4089 +  from NDVDJ_def foldr_And_map_rlfm2[OF th] show ?thesis by auto
  4.4090 +qed
  4.4091 +
  4.4092 +definition DVD :: "int \<Rightarrow> int \<Rightarrow> num \<Rightarrow> fm" where
  4.4093 +  DVD_def: "DVD i c t =
  4.4094 +  (if i=0 then eq c t else 
  4.4095 +  if c = 0 then (Dvd i t) else if c >0 then DVDJ (abs i) c t else DVDJ (abs i) (-c) (Neg t))"
  4.4096 +
  4.4097 +definition  NDVD :: "int \<Rightarrow> int \<Rightarrow> num \<Rightarrow> fm" where
  4.4098 +  "NDVD i c t =
  4.4099 +  (if i=0 then neq c t else 
  4.4100 +  if c = 0 then (NDvd i t) else if c >0 then NDVDJ (abs i) c t else NDVDJ (abs i) (-c) (Neg t))"
  4.4101 +
  4.4102 +lemma DVD_mono: 
  4.4103 +  assumes xp: "0\<le> x" and x1: "x < 1" 
  4.4104 +  shows "\<forall> a n s. Inum (x#bs) a = Inum (x#bs) (CN 0 n s) \<and> numbound0 s \<longrightarrow> Ifm (x#bs) (DVD i n s) = Ifm (x#bs) (Dvd i a)"
  4.4105 +  (is "\<forall> a n s. ?N a = ?N (CN 0 n s) \<and> _ \<longrightarrow> ?I (DVD i n s) = ?I (Dvd i a)")
  4.4106 +proof(clarify)
  4.4107 +  fix a n s 
  4.4108 +  assume H: "?N a = ?N (CN 0 n s)" and nb: "numbound0 s"
  4.4109 +  let ?th = "?I (DVD i n s) = ?I (Dvd i a)"
  4.4110 +  have "i=0 \<or> (i\<noteq>0 \<and> n=0) \<or> (i\<noteq>0 \<and> n < 0) \<or> (i\<noteq>0 \<and> n > 0)" by arith
  4.4111 +  moreover {assume iz: "i=0" hence ?th using eq_mono[rule_format, OF conjI[OF H nb]] 
  4.4112 +      by (simp add: DVD_def rdvd_left_0_eq)}
  4.4113 +  moreover {assume inz: "i\<noteq>0" and "n=0" hence ?th by (simp add: H DVD_def) } 
  4.4114 +  moreover {assume inz: "i\<noteq>0" and "n<0" hence ?th 
  4.4115 +      by (simp add: DVD_def H DVDJ_DVD[OF xp x1] rdvd_abs1 
  4.4116 +	rdvd_minus[where d="i" and t="real n * x + Inum (x # bs) s"]) } 
  4.4117 +  moreover {assume inz: "i\<noteq>0" and "n>0" hence ?th by (simp add:DVD_def H DVDJ_DVD[OF xp x1] rdvd_abs1)}
  4.4118 +  ultimately show ?th by blast
  4.4119 +qed
  4.4120 +
  4.4121 +lemma NDVD_mono:   assumes xp: "0\<le> x" and x1: "x < 1" 
  4.4122 +  shows "\<forall> a n s. Inum (x#bs) a = Inum (x#bs) (CN 0 n s) \<and> numbound0 s \<longrightarrow> Ifm (x#bs) (NDVD i n s) = Ifm (x#bs) (NDvd i a)"
  4.4123 +  (is "\<forall> a n s. ?N a = ?N (CN 0 n s) \<and> _ \<longrightarrow> ?I (NDVD i n s) = ?I (NDvd i a)")
  4.4124 +proof(clarify)
  4.4125 +  fix a n s 
  4.4126 +  assume H: "?N a = ?N (CN 0 n s)" and nb: "numbound0 s"
  4.4127 +  let ?th = "?I (NDVD i n s) = ?I (NDvd i a)"
  4.4128 +  have "i=0 \<or> (i\<noteq>0 \<and> n=0) \<or> (i\<noteq>0 \<and> n < 0) \<or> (i\<noteq>0 \<and> n > 0)" by arith
  4.4129 +  moreover {assume iz: "i=0" hence ?th using neq_mono[rule_format, OF conjI[OF H nb]] 
  4.4130 +      by (simp add: NDVD_def rdvd_left_0_eq)}
  4.4131 +  moreover {assume inz: "i\<noteq>0" and "n=0" hence ?th by (simp add: H NDVD_def) } 
  4.4132 +  moreover {assume inz: "i\<noteq>0" and "n<0" hence ?th 
  4.4133 +      by (simp add: NDVD_def H NDVDJ_NDVD[OF xp x1] rdvd_abs1 
  4.4134 +	rdvd_minus[where d="i" and t="real n * x + Inum (x # bs) s"]) } 
  4.4135 +  moreover {assume inz: "i\<noteq>0" and "n>0" hence ?th 
  4.4136 +      by (simp add:NDVD_def H NDVDJ_NDVD[OF xp x1] rdvd_abs1)}
  4.4137 +  ultimately show ?th by blast
  4.4138 +qed
  4.4139 +
  4.4140 +lemma DVD_l: "isrlfm (rsplit (DVD i) a)"
  4.4141 +  by (rule rsplit_l[where f="DVD i" and a="a"], auto simp add: DVD_def eq_def DVDJ_l) 
  4.4142 +(case_tac s, simp_all, case_tac "nat", simp_all)
  4.4143 +
  4.4144 +lemma NDVD_l: "isrlfm (rsplit (NDVD i) a)"
  4.4145 +  by (rule rsplit_l[where f="NDVD i" and a="a"], auto simp add: NDVD_def neq_def NDVDJ_l) 
  4.4146 +(case_tac s, simp_all, case_tac "nat", simp_all)
  4.4147 +
  4.4148 +consts rlfm :: "fm \<Rightarrow> fm"
  4.4149 +recdef rlfm "measure fmsize"
  4.4150 +  "rlfm (And p q) = conj (rlfm p) (rlfm q)"
  4.4151 +  "rlfm (Or p q) = disj (rlfm p) (rlfm q)"
  4.4152 +  "rlfm (Imp p q) = disj (rlfm (NOT p)) (rlfm q)"
  4.4153 +  "rlfm (Iff p q) = disj (conj(rlfm p) (rlfm q)) (conj(rlfm (NOT p)) (rlfm (NOT q)))"
  4.4154 +  "rlfm (Lt a) = rsplit lt a"
  4.4155 +  "rlfm (Le a) = rsplit le a"
  4.4156 +  "rlfm (Gt a) = rsplit gt a"
  4.4157 +  "rlfm (Ge a) = rsplit ge a"
  4.4158 +  "rlfm (Eq a) = rsplit eq a"
  4.4159 +  "rlfm (NEq a) = rsplit neq a"
  4.4160 +  "rlfm (Dvd i a) = rsplit (\<lambda> t. DVD i t) a"
  4.4161 +  "rlfm (NDvd i a) = rsplit (\<lambda> t. NDVD i t) a"
  4.4162 +  "rlfm (NOT (And p q)) = disj (rlfm (NOT p)) (rlfm (NOT q))"
  4.4163 +  "rlfm (NOT (Or p q)) = conj (rlfm (NOT p)) (rlfm (NOT q))"
  4.4164 +  "rlfm (NOT (Imp p q)) = conj (rlfm p) (rlfm (NOT q))"
  4.4165 +  "rlfm (NOT (Iff p q)) = disj (conj(rlfm p) (rlfm(NOT q))) (conj(rlfm(NOT p)) (rlfm q))"
  4.4166 +  "rlfm (NOT (NOT p)) = rlfm p"
  4.4167 +  "rlfm (NOT T) = F"
  4.4168 +  "rlfm (NOT F) = T"
  4.4169 +  "rlfm (NOT (Lt a)) = simpfm (rlfm (Ge a))"
  4.4170 +  "rlfm (NOT (Le a)) = simpfm (rlfm (Gt a))"
  4.4171 +  "rlfm (NOT (Gt a)) = simpfm (rlfm (Le a))"
  4.4172 +  "rlfm (NOT (Ge a)) = simpfm (rlfm (Lt a))"
  4.4173 +  "rlfm (NOT (Eq a)) = simpfm (rlfm (NEq a))"
  4.4174 +  "rlfm (NOT (NEq a)) = simpfm (rlfm (Eq a))"
  4.4175 +  "rlfm (NOT (Dvd i a)) = simpfm (rlfm (NDvd i a))"
  4.4176 +  "rlfm (NOT (NDvd i a)) = simpfm (rlfm (Dvd i a))"
  4.4177 +  "rlfm p = p" (hints simp add: fmsize_pos)
  4.4178 +
  4.4179 +lemma bound0at_l : "\<lbrakk>isatom p ; bound0 p\<rbrakk> \<Longrightarrow> isrlfm p"
  4.4180 +  by (induct p rule: isrlfm.induct, auto)
  4.4181 +lemma zgcd_le1: assumes ip: "0 < i" shows "zgcd i j \<le> i"
  4.4182 +proof-
  4.4183 +  from zgcd_zdvd1 have th: "zgcd i j dvd i" by blast
  4.4184 +  from zdvd_imp_le[OF th ip] show ?thesis .
  4.4185 +qed
  4.4186 +
  4.4187 +
  4.4188 +lemma simpfm_rl: "isrlfm p \<Longrightarrow> isrlfm (simpfm p)"
  4.4189 +proof (induct p)
  4.4190 +  case (Lt a) 
  4.4191 +  hence "bound0 (Lt a) \<or> (\<exists> c e. a = CN 0 c e \<and> c > 0 \<and> numbound0 e)"
  4.4192 +    by (cases a,simp_all, case_tac "nat", simp_all)
  4.4193 +  moreover
  4.4194 +  {assume "bound0 (Lt a)" hence bn:"bound0 (simpfm (Lt a))"  
  4.4195 +      using simpfm_bound0 by blast
  4.4196 +    have "isatom (simpfm (Lt a))" by (cases "simpnum a", auto simp add: Let_def)
  4.4197 +    with bn bound0at_l have ?case by blast}
  4.4198 +  moreover 
  4.4199 +  {fix c e assume "a = CN 0 c e" and "c>0" and "numbound0 e"
  4.4200 +    {
  4.4201 +      assume cn1:"numgcd (CN 0 c (simpnum e)) \<noteq> 1" and cnz:"numgcd (CN 0 c (simpnum e)) \<noteq> 0"
  4.4202 +      with numgcd_pos[where t="CN 0 c (simpnum e)"]
  4.4203 +      have th1:"numgcd (CN 0 c (simpnum e)) > 0" by simp
  4.4204 +      from prems have th:"numgcd (CN 0 c (simpnum e)) \<le> c" 
  4.4205 +	by (simp add: numgcd_def zgcd_le1)
  4.4206 +      from prems have th': "c\<noteq>0" by auto
  4.4207 +      from prems have cp: "c \<ge> 0" by simp
  4.4208 +      from zdiv_mono2[OF cp th1 th, simplified zdiv_self[OF th']]
  4.4209 +	have "0 < c div numgcd (CN 0 c (simpnum e))" by simp
  4.4210 +    }
  4.4211 +    with prems have ?case
  4.4212 +      by (simp add: Let_def reducecoeff_def reducecoeffh_numbound0)}
  4.4213 +  ultimately show ?case by blast
  4.4214 +next
  4.4215 +  case (Le a)   
  4.4216 +  hence "bound0 (Le a) \<or> (\<exists> c e. a = CN 0 c e \<and> c > 0 \<and> numbound0 e)"
  4.4217 +    by (cases a,simp_all, case_tac "nat", simp_all)
  4.4218 +  moreover
  4.4219 +  {assume "bound0 (Le a)" hence bn:"bound0 (simpfm (Le a))"  
  4.4220 +      using simpfm_bound0 by blast
  4.4221 +    have "isatom (simpfm (Le a))" by (cases "simpnum a", auto simp add: Let_def)
  4.4222 +    with bn bound0at_l have ?case by blast}
  4.4223 +  moreover 
  4.4224 +  {fix c e assume "a = CN 0 c e" and "c>0" and "numbound0 e"
  4.4225 +    {
  4.4226 +      assume cn1:"numgcd (CN 0 c (simpnum e)) \<noteq> 1" and cnz:"numgcd (CN 0 c (simpnum e)) \<noteq> 0"
  4.4227 +      with numgcd_pos[where t="CN 0 c (simpnum e)"]
  4.4228 +      have th1:"numgcd (CN 0 c (simpnum e)) > 0" by simp
  4.4229 +      from prems have th:"numgcd (CN 0 c (simpnum e)) \<le> c" 
  4.4230 +	by (simp add: numgcd_def zgcd_le1)
  4.4231 +      from prems have th': "c\<noteq>0" by auto
  4.4232 +      from prems have cp: "c \<ge> 0" by simp
  4.4233 +      from zdiv_mono2[OF cp th1 th, simplified zdiv_self[OF th']]
  4.4234 +	have "0 < c div numgcd (CN 0 c (simpnum e))" by simp
  4.4235 +    }
  4.4236 +    with prems have ?case
  4.4237 +      by (simp add: Let_def reducecoeff_def simpnum_numbound0 reducecoeffh_numbound0)}
  4.4238 +  ultimately show ?case by blast
  4.4239 +next
  4.4240 +  case (Gt a)   
  4.4241 +  hence "bound0 (Gt a) \<or> (\<exists> c e. a = CN 0 c e \<and> c > 0 \<and> numbound0 e)"
  4.4242 +    by (cases a,simp_all, case_tac "nat", simp_all)
  4.4243 +  moreover
  4.4244 +  {assume "bound0 (Gt a)" hence bn:"bound0 (simpfm (Gt a))"  
  4.4245 +      using simpfm_bound0 by blast
  4.4246 +    have "isatom (simpfm (Gt a))" by (cases "simpnum a", auto simp add: Let_def)
  4.4247 +    with bn bound0at_l have ?case by blast}
  4.4248 +  moreover 
  4.4249 +  {fix c e assume "a = CN 0 c e" and "c>0" and "numbound0 e"
  4.4250 +    {
  4.4251 +      assume cn1:"numgcd (CN 0 c (simpnum e)) \<noteq> 1" and cnz:"numgcd (CN 0 c (simpnum e)) \<noteq> 0"
  4.4252 +      with numgcd_pos[where t="CN 0 c (simpnum e)"]
  4.4253 +      have th1:"numgcd (CN 0 c (simpnum e)) > 0" by simp
  4.4254 +      from prems have th:"numgcd (CN 0 c (simpnum e)) \<le> c" 
  4.4255 +	by (simp add: numgcd_def zgcd_le1)
  4.4256 +      from prems have th': "c\<noteq>0" by auto
  4.4257 +      from prems have cp: "c \<ge> 0" by simp
  4.4258 +      from zdiv_mono2[OF cp th1 th, simplified zdiv_self[OF th']]
  4.4259 +	have "0 < c div numgcd (CN 0 c (simpnum e))" by simp
  4.4260 +    }
  4.4261 +    with prems have ?case
  4.4262 +      by (simp add: Let_def reducecoeff_def simpnum_numbound0 reducecoeffh_numbound0)}
  4.4263 +  ultimately show ?case by blast
  4.4264 +next
  4.4265 +  case (Ge a)   
  4.4266 +  hence "bound0 (Ge a) \<or> (\<exists> c e. a = CN 0 c e \<and> c > 0 \<and> numbound0 e)"
  4.4267 +    by (cases a,simp_all, case_tac "nat", simp_all)
  4.4268 +  moreover
  4.4269 +  {assume "bound0 (Ge a)" hence bn:"bound0 (simpfm (Ge a))"  
  4.4270 +      using simpfm_bound0 by blast
  4.4271 +    have "isatom (simpfm (Ge a))" by (cases "simpnum a", auto simp add: Let_def)
  4.4272 +    with bn bound0at_l have ?case by blast}
  4.4273 +  moreover 
  4.4274 +  {fix c e assume "a = CN 0 c e" and "c>0" and "numbound0 e"
  4.4275 +    {
  4.4276 +      assume cn1:"numgcd (CN 0 c (simpnum e)) \<noteq> 1" and cnz:"numgcd (CN 0 c (simpnum e)) \<noteq> 0"
  4.4277 +      with numgcd_pos[where t="CN 0 c (simpnum e)"]
  4.4278 +      have th1:"numgcd (CN 0 c (simpnum e)) > 0" by simp
  4.4279 +      from prems have th:"numgcd (CN 0 c (simpnum e)) \<le> c" 
  4.4280 +	by (simp add: numgcd_def zgcd_le1)
  4.4281 +      from prems have th': "c\<noteq>0" by auto
  4.4282 +      from prems have cp: "c \<ge> 0" by simp
  4.4283 +      from zdiv_mono2[OF cp th1 th, simplified zdiv_self[OF th']]
  4.4284 +	have "0 < c div numgcd (CN 0 c (simpnum e))" by simp
  4.4285 +    }
  4.4286 +    with prems have ?case
  4.4287 +      by (simp add: Let_def reducecoeff_def simpnum_numbound0 reducecoeffh_numbound0)}
  4.4288 +  ultimately show ?case by blast
  4.4289 +next
  4.4290 +  case (Eq a)   
  4.4291 +  hence "bound0 (Eq a) \<or> (\<exists> c e. a = CN 0 c e \<and> c > 0 \<and> numbound0 e)"
  4.4292 +    by (cases a,simp_all, case_tac "nat", simp_all)
  4.4293 +  moreover
  4.4294 +  {assume "bound0 (Eq a)" hence bn:"bound0 (simpfm (Eq a))"  
  4.4295 +      using simpfm_bound0 by blast
  4.4296 +    have "isatom (simpfm (Eq a))" by (cases "simpnum a", auto simp add: Let_def)
  4.4297 +    with bn bound0at_l have ?case by blast}
  4.4298 +  moreover 
  4.4299 +  {fix c e assume "a = CN 0 c e" and "c>0" and "numbound0 e"
  4.4300 +    {
  4.4301 +      assume cn1:"numgcd (CN 0 c (simpnum e)) \<noteq> 1" and cnz:"numgcd (CN 0 c (simpnum e)) \<noteq> 0"
  4.4302 +      with numgcd_pos[where t="CN 0 c (simpnum e)"]
  4.4303 +      have th1:"numgcd (CN 0 c (simpnum e)) > 0" by simp
  4.4304 +      from prems have th:"numgcd (CN 0 c (simpnum e)) \<le> c" 
  4.4305 +	by (simp add: numgcd_def zgcd_le1)
  4.4306 +      from prems have th': "c\<noteq>0" by auto
  4.4307 +      from prems have cp: "c \<ge> 0" by simp
  4.4308 +      from zdiv_mono2[OF cp th1 th, simplified zdiv_self[OF th']]
  4.4309 +	have "0 < c div numgcd (CN 0 c (simpnum e))" by simp
  4.4310 +    }
  4.4311 +    with prems have ?case
  4.4312 +      by (simp add: Let_def reducecoeff_def simpnum_numbound0 reducecoeffh_numbound0)}
  4.4313 +  ultimately show ?case by blast
  4.4314 +next
  4.4315 +  case (NEq a)  
  4.4316 +  hence "bound0 (NEq a) \<or> (\<exists> c e. a = CN 0 c e \<and> c > 0 \<and> numbound0 e)"
  4.4317 +    by (cases a,simp_all, case_tac "nat", simp_all)
  4.4318 +  moreover
  4.4319 +  {assume "bound0 (NEq a)" hence bn:"bound0 (simpfm (NEq a))"  
  4.4320 +      using simpfm_bound0 by blast
  4.4321 +    have "isatom (simpfm (NEq a))" by (cases "simpnum a", auto simp add: Let_def)
  4.4322 +    with bn bound0at_l have ?case by blast}
  4.4323 +  moreover 
  4.4324 +  {fix c e assume "a = CN 0 c e" and "c>0" and "numbound0 e"
  4.4325 +    {
  4.4326 +      assume cn1:"numgcd (CN 0 c (simpnum e)) \<noteq> 1" and cnz:"numgcd (CN 0 c (simpnum e)) \<noteq> 0"
  4.4327 +      with numgcd_pos[where t="CN 0 c (simpnum e)"]
  4.4328 +      have th1:"numgcd (CN 0 c (simpnum e)) > 0" by simp
  4.4329 +      from prems have th:"numgcd (CN 0 c (simpnum e)) \<le> c" 
  4.4330 +	by (simp add: numgcd_def zgcd_le1)
  4.4331 +      from prems have th': "c\<noteq>0" by auto
  4.4332 +      from prems have cp: "c \<ge> 0" by simp
  4.4333 +      from zdiv_mono2[OF cp th1 th, simplified zdiv_self[OF th']]
  4.4334 +	have "0 < c div numgcd (CN 0 c (simpnum e))" by simp
  4.4335 +    }
  4.4336 +    with prems have ?case
  4.4337 +      by (simp add: Let_def reducecoeff_def simpnum_numbound0 reducecoeffh_numbound0)}
  4.4338 +  ultimately show ?case by blast
  4.4339 +next
  4.4340 +  case (Dvd i a) hence "bound0 (Dvd i a)" by auto hence bn:"bound0 (simpfm (Dvd i a))"  
  4.4341 +    using simpfm_bound0 by blast
  4.4342 +  have "isatom (simpfm (Dvd i a))" by (cases "simpnum a", auto simp add: Let_def split_def)
  4.4343 +  with bn bound0at_l show ?case by blast
  4.4344 +next
  4.4345 +  case (NDvd i a)  hence "bound0 (NDvd i a)" by auto hence bn:"bound0 (simpfm (NDvd i a))"  
  4.4346 +    using simpfm_bound0 by blast
  4.4347 +  have "isatom (simpfm (NDvd i a))" by (cases "simpnum a", auto simp add: Let_def split_def)
  4.4348 +  with bn bound0at_l show ?case by blast
  4.4349 +qed(auto simp add: conj_def imp_def disj_def iff_def Let_def simpfm_bound0 numadd_nb numneg_nb)
  4.4350 +
  4.4351 +lemma rlfm_I:
  4.4352 +  assumes qfp: "qfree p"
  4.4353 +  and xp: "0 \<le> x" and x1: "x < 1"
  4.4354 +  shows "(Ifm (x#bs) (rlfm p) = Ifm (x# bs) p) \<and> isrlfm (rlfm p)"
  4.4355 +  using qfp 
  4.4356 +by (induct p rule: rlfm.induct) 
  4.4357 +(auto simp add: rsplit[OF xp x1 lt_mono] lt_l rsplit[OF xp x1 le_mono] le_l rsplit[OF xp x1 gt_mono] gt_l
  4.4358 +               rsplit[OF xp x1 ge_mono] ge_l rsplit[OF xp x1 eq_mono] eq_l rsplit[OF xp x1 neq_mono] neq_l
  4.4359 +               rsplit[OF xp x1 DVD_mono[OF xp x1]] DVD_l rsplit[OF xp x1 NDVD_mono[OF xp x1]] NDVD_l simpfm_rl)
  4.4360 +lemma rlfm_l:
  4.4361 +  assumes qfp: "qfree p"
  4.4362 +  shows "isrlfm (rlfm p)"
  4.4363 +  using qfp lt_l gt_l ge_l le_l eq_l neq_l DVD_l NDVD_l 
  4.4364 +by (induct p rule: rlfm.induct,auto simp add: simpfm_rl)
  4.4365 +
  4.4366 +    (* Operations needed for Ferrante and Rackoff *)
  4.4367 +lemma rminusinf_inf:
  4.4368 +  assumes lp: "isrlfm p"
  4.4369 +  shows "\<exists> z. \<forall> x < z. Ifm (x#bs) (minusinf p) = Ifm (x#bs) p" (is "\<exists> z. \<forall> x. ?P z x p")
  4.4370 +using lp
  4.4371 +proof (induct p rule: minusinf.induct)
  4.4372 +  case (1 p q) thus ?case by (auto,rule_tac x= "min z za" in exI) auto
  4.4373 +next
  4.4374 +  case (2 p q) thus ?case by (auto,rule_tac x= "min z za" in exI) auto
  4.4375 +next
  4.4376 +  case (3 c e) 
  4.4377 +  from prems have nb: "numbound0 e" by simp
  4.4378 +  from prems have cp: "real c > 0" by simp
  4.4379 +  fix a
  4.4380 +  let ?e="Inum (a#bs) e"
  4.4381 +  let ?z = "(- ?e) / real c"
  4.4382 +  {fix x
  4.4383 +    assume xz: "x < ?z"
  4.4384 +    hence "(real c * x < - ?e)" 
  4.4385 +      by (simp only: pos_less_divide_eq[OF cp, where a="x" and b="- ?e"] mult_ac) 
  4.4386 +    hence "real c * x + ?e < 0" by arith
  4.4387 +    hence "real c * x + ?e \<noteq> 0" by simp
  4.4388 +    with xz have "?P ?z x (Eq (CN 0 c e))"
  4.4389 +      using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp  }
  4.4390 +  hence "\<forall> x < ?z. ?P ?z x (Eq (CN 0 c e))" by simp
  4.4391 +  thus ?case by blast
  4.4392 +next