src/HOL/Decision_Procs/Parametric_Ferrante_Rackoff.thy
changeset 33152 241cfaed158f
child 33212 f3c8acbff503
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Decision_Procs/Parametric_Ferrante_Rackoff.thy	Sun Oct 25 08:57:35 2009 +0100
     1.3 @@ -0,0 +1,3227 @@
     1.4 +(*  Title:      HOL/Decision_Procs/Parametric_Ferrante_Rackoff.thy
     1.5 +    Author:     Amine Chaieb
     1.6 +*)
     1.7 +
     1.8 +header{* A formalization of Ferrante and Rackoff's procedure with polynomial parameters, see Paper in CALCULEMUS 2008 *}
     1.9 +
    1.10 +theory Parametric_Ferrante_Rackoff
    1.11 +imports Reflected_Multivariate_Polynomial 
    1.12 +  "~~/src/HOL/Decision_Procs/Dense_Linear_Order"
    1.13 +begin
    1.14 +
    1.15 +
    1.16 +subsection {* Terms *}
    1.17 +
    1.18 +datatype tm = CP poly | Bound nat | Add tm tm | Mul poly tm 
    1.19 +  | Neg tm | Sub tm tm | CNP nat poly tm
    1.20 +  (* A size for poly to make inductive proofs simpler*)
    1.21 +
    1.22 +consts tmsize :: "tm \<Rightarrow> nat"
    1.23 +primrec 
    1.24 +  "tmsize (CP c) = polysize c"
    1.25 +  "tmsize (Bound n) = 1"
    1.26 +  "tmsize (Neg a) = 1 + tmsize a"
    1.27 +  "tmsize (Add a b) = 1 + tmsize a + tmsize b"
    1.28 +  "tmsize (Sub a b) = 3 + tmsize a + tmsize b"
    1.29 +  "tmsize (Mul c a) = 1 + polysize c + tmsize a"
    1.30 +  "tmsize (CNP n c a) = 3 + polysize c + tmsize a "
    1.31 +
    1.32 +  (* Semantics of terms tm *)
    1.33 +consts Itm :: "'a::{ring_char_0,division_by_zero,field} list \<Rightarrow> 'a list \<Rightarrow> tm \<Rightarrow> 'a"
    1.34 +primrec
    1.35 +  "Itm vs bs (CP c) = (Ipoly vs c)"
    1.36 +  "Itm vs bs (Bound n) = bs!n"
    1.37 +  "Itm vs bs (Neg a) = -(Itm vs bs a)"
    1.38 +  "Itm vs bs (Add a b) = Itm vs bs a + Itm vs bs b"
    1.39 +  "Itm vs bs (Sub a b) = Itm vs bs a - Itm vs bs b"
    1.40 +  "Itm vs bs (Mul c a) = (Ipoly vs c) * Itm vs bs a"
    1.41 +  "Itm vs bs (CNP n c t) = (Ipoly vs c)*(bs!n) + Itm vs bs t"	
    1.42 +
    1.43 +
    1.44 +fun allpolys:: "(poly \<Rightarrow> bool) \<Rightarrow> tm \<Rightarrow> bool"  where
    1.45 +  "allpolys P (CP c) = P c"
    1.46 +| "allpolys P (CNP n c p) = (P c \<and> allpolys P p)"
    1.47 +| "allpolys P (Mul c p) = (P c \<and> allpolys P p)"
    1.48 +| "allpolys P (Neg p) = allpolys P p"
    1.49 +| "allpolys P (Add p q) = (allpolys P p \<and> allpolys P q)"
    1.50 +| "allpolys P (Sub p q) = (allpolys P p \<and> allpolys P q)"
    1.51 +| "allpolys P p = True"
    1.52 +
    1.53 +consts 
    1.54 +  tmboundslt:: "nat \<Rightarrow> tm \<Rightarrow> bool"
    1.55 +  tmbound0:: "tm \<Rightarrow> bool" (* a tm is INDEPENDENT of Bound 0 *)
    1.56 +  tmbound:: "nat \<Rightarrow> tm \<Rightarrow> bool" (* a tm is INDEPENDENT of Bound n *)
    1.57 +  incrtm0:: "tm \<Rightarrow> tm"
    1.58 +  incrtm:: "nat \<Rightarrow> tm \<Rightarrow> tm"
    1.59 +  decrtm0:: "tm \<Rightarrow> tm" 
    1.60 +  decrtm:: "nat \<Rightarrow> tm \<Rightarrow> tm" 
    1.61 +primrec
    1.62 +  "tmboundslt n (CP c) = True"
    1.63 +  "tmboundslt n (Bound m) = (m < n)"
    1.64 +  "tmboundslt n (CNP m c a) = (m < n \<and> tmboundslt n a)"
    1.65 +  "tmboundslt n (Neg a) = tmboundslt n a"
    1.66 +  "tmboundslt n (Add a b) = (tmboundslt n a \<and> tmboundslt n b)"
    1.67 +  "tmboundslt n (Sub a b) = (tmboundslt n a \<and> tmboundslt n b)" 
    1.68 +  "tmboundslt n (Mul i a) = tmboundslt n a"
    1.69 +primrec
    1.70 +  "tmbound0 (CP c) = True"
    1.71 +  "tmbound0 (Bound n) = (n>0)"
    1.72 +  "tmbound0 (CNP n c a) = (n\<noteq>0 \<and> tmbound0 a)"
    1.73 +  "tmbound0 (Neg a) = tmbound0 a"
    1.74 +  "tmbound0 (Add a b) = (tmbound0 a \<and> tmbound0 b)"
    1.75 +  "tmbound0 (Sub a b) = (tmbound0 a \<and> tmbound0 b)" 
    1.76 +  "tmbound0 (Mul i a) = tmbound0 a"
    1.77 +lemma tmbound0_I:
    1.78 +  assumes nb: "tmbound0 a"
    1.79 +  shows "Itm vs (b#bs) a = Itm vs (b'#bs) a"
    1.80 +using nb
    1.81 +by (induct a rule: tmbound0.induct,auto simp add: nth_pos2)
    1.82 +
    1.83 +primrec
    1.84 +  "tmbound n (CP c) = True"
    1.85 +  "tmbound n (Bound m) = (n \<noteq> m)"
    1.86 +  "tmbound n (CNP m c a) = (n\<noteq>m \<and> tmbound n a)"
    1.87 +  "tmbound n (Neg a) = tmbound n a"
    1.88 +  "tmbound n (Add a b) = (tmbound n a \<and> tmbound n b)"
    1.89 +  "tmbound n (Sub a b) = (tmbound n a \<and> tmbound n b)" 
    1.90 +  "tmbound n (Mul i a) = tmbound n a"
    1.91 +lemma tmbound0_tmbound_iff: "tmbound 0 t = tmbound0 t" by (induct t, auto)
    1.92 +
    1.93 +lemma tmbound_I: 
    1.94 +  assumes bnd: "tmboundslt (length bs) t" and nb: "tmbound n t" and le: "n \<le> length bs"
    1.95 +  shows "Itm vs (bs[n:=x]) t = Itm vs bs t"
    1.96 +  using nb le bnd
    1.97 +  by (induct t rule: tmbound.induct , auto)
    1.98 +
    1.99 +recdef decrtm0 "measure size"
   1.100 +  "decrtm0 (Bound n) = Bound (n - 1)"
   1.101 +  "decrtm0 (Neg a) = Neg (decrtm0 a)"
   1.102 +  "decrtm0 (Add a b) = Add (decrtm0 a) (decrtm0 b)"
   1.103 +  "decrtm0 (Sub a b) = Sub (decrtm0 a) (decrtm0 b)"
   1.104 +  "decrtm0 (Mul c a) = Mul c (decrtm0 a)"
   1.105 +  "decrtm0 (CNP n c a) = CNP (n - 1) c (decrtm0 a)"
   1.106 +  "decrtm0 a = a"
   1.107 +recdef incrtm0 "measure size"
   1.108 +  "incrtm0 (Bound n) = Bound (n + 1)"
   1.109 +  "incrtm0 (Neg a) = Neg (incrtm0 a)"
   1.110 +  "incrtm0 (Add a b) = Add (incrtm0 a) (incrtm0 b)"
   1.111 +  "incrtm0 (Sub a b) = Sub (incrtm0 a) (incrtm0 b)"
   1.112 +  "incrtm0 (Mul c a) = Mul c (incrtm0 a)"
   1.113 +  "incrtm0 (CNP n c a) = CNP (n + 1) c (incrtm0 a)"
   1.114 +  "incrtm0 a = a"
   1.115 +lemma decrtm0: assumes nb: "tmbound0 t"
   1.116 +  shows "Itm vs (x#bs) t = Itm vs bs (decrtm0 t)"
   1.117 +  using nb by (induct t rule: decrtm0.induct, simp_all add: nth_pos2)
   1.118 +lemma incrtm0: "Itm vs (x#bs) (incrtm0 t) = Itm vs bs t"
   1.119 +  by (induct t rule: decrtm0.induct, simp_all add: nth_pos2)
   1.120 +
   1.121 +primrec
   1.122 +  "decrtm m (CP c) = (CP c)"
   1.123 +  "decrtm m (Bound n) = (if n < m then Bound n else Bound (n - 1))"
   1.124 +  "decrtm m (Neg a) = Neg (decrtm m a)"
   1.125 +  "decrtm m (Add a b) = Add (decrtm m a) (decrtm m b)"
   1.126 +  "decrtm m (Sub a b) = Sub (decrtm m a) (decrtm m b)"
   1.127 +  "decrtm m (Mul c a) = Mul c (decrtm m a)"
   1.128 +  "decrtm m (CNP n c a) = (if n < m then CNP n c (decrtm m a) else CNP (n - 1) c (decrtm m a))"
   1.129 +
   1.130 +consts removen:: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list"
   1.131 +primrec
   1.132 +  "removen n [] = []"
   1.133 +  "removen n (x#xs) = (if n=0 then xs else (x#(removen (n - 1) xs)))"
   1.134 +
   1.135 +lemma removen_same: "n \<ge> length xs \<Longrightarrow> removen n xs = xs"
   1.136 +  by (induct xs arbitrary: n, auto)
   1.137 +
   1.138 +lemma nth_length_exceeds: "n \<ge> length xs \<Longrightarrow> xs!n = []!(n - length xs)"
   1.139 +  by (induct xs arbitrary: n, auto)
   1.140 +
   1.141 +lemma removen_length: "length (removen n xs) = (if n \<ge> length xs then length xs else length xs - 1)"
   1.142 +  by (induct xs arbitrary: n, auto)
   1.143 +lemma removen_nth: "(removen n xs)!m = (if n \<ge> length xs then xs!m 
   1.144 +  else if m < n then xs!m else if m \<le> length xs then xs!(Suc m) else []!(m - (length xs - 1)))"
   1.145 +proof(induct xs arbitrary: n m)
   1.146 +  case Nil thus ?case by simp
   1.147 +next
   1.148 +  case (Cons x xs n m)
   1.149 +  {assume nxs: "n \<ge> length (x#xs)" hence ?case using removen_same[OF nxs] by simp}
   1.150 +  moreover
   1.151 +  {assume nxs: "\<not> (n \<ge> length (x#xs))" 
   1.152 +    {assume mln: "m < n" hence ?case using prems by (cases m, auto)}
   1.153 +    moreover
   1.154 +    {assume mln: "\<not> (m < n)" 
   1.155 +      
   1.156 +      {assume mxs: "m \<le> length (x#xs)" hence ?case using prems by (cases m, auto)}
   1.157 +      moreover
   1.158 +      {assume mxs: "\<not> (m \<le> length (x#xs))" 
   1.159 +	have th: "length (removen n (x#xs)) = length xs" 
   1.160 +	  using removen_length[where n="n" and xs="x#xs"] nxs by simp
   1.161 +	with mxs have mxs':"m \<ge> length (removen n (x#xs))" by auto
   1.162 +	hence "(removen n (x#xs))!m = [] ! (m - length xs)" 
   1.163 +	  using th nth_length_exceeds[OF mxs'] by auto
   1.164 +	hence th: "(removen n (x#xs))!m = [] ! (m - (length (x#xs) - 1))" 
   1.165 +	  by auto
   1.166 +	hence ?case using nxs mln mxs by auto }
   1.167 +      ultimately have ?case by blast
   1.168 +    }
   1.169 +    ultimately have ?case by blast
   1.170 +    
   1.171 +  }      ultimately show ?case by blast
   1.172 +qed
   1.173 +
   1.174 +lemma decrtm: assumes bnd: "tmboundslt (length bs) t" and nb: "tmbound m t" 
   1.175 +  and nle: "m \<le> length bs" 
   1.176 +  shows "Itm vs (removen m bs) (decrtm m t) = Itm vs bs t"
   1.177 +  using bnd nb nle
   1.178 +  by (induct t rule: decrtm.induct, auto simp add: removen_nth)
   1.179 +
   1.180 +consts tmsubst0:: "tm \<Rightarrow> tm \<Rightarrow> tm"
   1.181 +primrec
   1.182 +  "tmsubst0 t (CP c) = CP c"
   1.183 +  "tmsubst0 t (Bound n) = (if n=0 then t else Bound n)"
   1.184 +  "tmsubst0 t (CNP n c a) = (if n=0 then Add (Mul c t) (tmsubst0 t a) else CNP n c (tmsubst0 t a))"
   1.185 +  "tmsubst0 t (Neg a) = Neg (tmsubst0 t a)"
   1.186 +  "tmsubst0 t (Add a b) = Add (tmsubst0 t a) (tmsubst0 t b)"
   1.187 +  "tmsubst0 t (Sub a b) = Sub (tmsubst0 t a) (tmsubst0 t b)" 
   1.188 +  "tmsubst0 t (Mul i a) = Mul i (tmsubst0 t a)"
   1.189 +lemma tmsubst0:
   1.190 +  shows "Itm vs (x#bs) (tmsubst0 t a) = Itm vs ((Itm vs (x#bs) t)#bs) a"
   1.191 +by (induct a rule: tmsubst0.induct,auto simp add: nth_pos2)
   1.192 +
   1.193 +lemma tmsubst0_nb: "tmbound0 t \<Longrightarrow> tmbound0 (tmsubst0 t a)"
   1.194 +by (induct a rule: tmsubst0.induct,auto simp add: nth_pos2)
   1.195 +
   1.196 +consts tmsubst:: "nat \<Rightarrow> tm \<Rightarrow> tm \<Rightarrow> tm" 
   1.197 +
   1.198 +primrec
   1.199 +  "tmsubst n t (CP c) = CP c"
   1.200 +  "tmsubst n t (Bound m) = (if n=m then t else Bound m)"
   1.201 +  "tmsubst n t (CNP m c a) = (if n=m then Add (Mul c t) (tmsubst n t a) 
   1.202 +             else CNP m c (tmsubst n t a))"
   1.203 +  "tmsubst n t (Neg a) = Neg (tmsubst n t a)"
   1.204 +  "tmsubst n t (Add a b) = Add (tmsubst n t a) (tmsubst n t b)"
   1.205 +  "tmsubst n t (Sub a b) = Sub (tmsubst n t a) (tmsubst n t b)" 
   1.206 +  "tmsubst n t (Mul i a) = Mul i (tmsubst n t a)"
   1.207 +
   1.208 +lemma tmsubst: assumes nb: "tmboundslt (length bs) a" and nlt: "n \<le> length bs"
   1.209 +  shows "Itm vs bs (tmsubst n t a) = Itm vs (bs[n:= Itm vs bs t]) a"
   1.210 +using nb nlt
   1.211 +by (induct a rule: tmsubst0.induct,auto simp add: nth_pos2)
   1.212 +
   1.213 +lemma tmsubst_nb0: assumes tnb: "tmbound0 t"
   1.214 +shows "tmbound0 (tmsubst 0 t a)"
   1.215 +using tnb
   1.216 +by (induct a rule: tmsubst.induct, auto)
   1.217 +
   1.218 +lemma tmsubst_nb: assumes tnb: "tmbound m t"
   1.219 +shows "tmbound m (tmsubst m t a)"
   1.220 +using tnb
   1.221 +by (induct a rule: tmsubst.induct, auto)
   1.222 +lemma incrtm0_tmbound: "tmbound n t \<Longrightarrow> tmbound (Suc n) (incrtm0 t)"
   1.223 +  by (induct t, auto)
   1.224 +  (* Simplification *)
   1.225 +
   1.226 +consts
   1.227 +  simptm:: "tm \<Rightarrow> tm"
   1.228 +  tmadd:: "tm \<times> tm \<Rightarrow> tm"
   1.229 +  tmmul:: "tm \<Rightarrow> poly \<Rightarrow> tm"
   1.230 +recdef tmadd "measure (\<lambda> (t,s). size t + size s)"
   1.231 +  "tmadd (CNP n1 c1 r1,CNP n2 c2 r2) =
   1.232 +  (if n1=n2 then 
   1.233 +  (let c = c1 +\<^sub>p c2
   1.234 +  in if c = 0\<^sub>p then tmadd(r1,r2) else CNP n1 c (tmadd (r1,r2)))
   1.235 +  else if n1 \<le> n2 then (CNP n1 c1 (tmadd (r1,CNP n2 c2 r2))) 
   1.236 +  else (CNP n2 c2 (tmadd (CNP n1 c1 r1,r2))))"
   1.237 +  "tmadd (CNP n1 c1 r1,t) = CNP n1 c1 (tmadd (r1, t))"  
   1.238 +  "tmadd (t,CNP n2 c2 r2) = CNP n2 c2 (tmadd (t,r2))" 
   1.239 +  "tmadd (CP b1, CP b2) = CP (b1 +\<^sub>p b2)"
   1.240 +  "tmadd (a,b) = Add a b"
   1.241 +
   1.242 +lemma tmadd[simp]: "Itm vs bs (tmadd (t,s)) = Itm vs bs (Add t s)"
   1.243 +apply (induct t s rule: tmadd.induct, simp_all add: Let_def)
   1.244 +apply (case_tac "c1 +\<^sub>p c2 = 0\<^sub>p",case_tac "n1 \<le> n2", simp_all)
   1.245 +apply (case_tac "n1 = n2", simp_all add: ring_simps)
   1.246 +apply (simp only: right_distrib[symmetric]) 
   1.247 +by (auto simp del: polyadd simp add: polyadd[symmetric])
   1.248 +
   1.249 +lemma tmadd_nb0[simp]: "\<lbrakk> tmbound0 t ; tmbound0 s\<rbrakk> \<Longrightarrow> tmbound0 (tmadd (t,s))"
   1.250 +by (induct t s rule: tmadd.induct, auto simp add: Let_def)
   1.251 +
   1.252 +lemma tmadd_nb[simp]: "\<lbrakk> tmbound n t ; tmbound n s\<rbrakk> \<Longrightarrow> tmbound n (tmadd (t,s))"
   1.253 +by (induct t s rule: tmadd.induct, auto simp add: Let_def)
   1.254 +lemma tmadd_blt[simp]: "\<lbrakk>tmboundslt n t ; tmboundslt n s\<rbrakk> \<Longrightarrow> tmboundslt n (tmadd (t,s))"
   1.255 +by (induct t s rule: tmadd.induct, auto simp add: Let_def)
   1.256 +
   1.257 +lemma tmadd_allpolys_npoly[simp]: "allpolys isnpoly t \<Longrightarrow> allpolys isnpoly s \<Longrightarrow> allpolys isnpoly (tmadd(t,s))" by (induct t s rule: tmadd.induct, simp_all add: Let_def polyadd_norm)
   1.258 +
   1.259 +recdef tmmul "measure size"
   1.260 +  "tmmul (CP j) = (\<lambda> i. CP (i *\<^sub>p j))"
   1.261 +  "tmmul (CNP n c a) = (\<lambda> i. CNP n (i *\<^sub>p c) (tmmul a i))"
   1.262 +  "tmmul t = (\<lambda> i. Mul i t)"
   1.263 +
   1.264 +lemma tmmul[simp]: "Itm vs bs (tmmul t i) = Itm vs bs (Mul i t)"
   1.265 +by (induct t arbitrary: i rule: tmmul.induct, simp_all add: ring_simps)
   1.266 +
   1.267 +lemma tmmul_nb0[simp]: "tmbound0 t \<Longrightarrow> tmbound0 (tmmul t i)"
   1.268 +by (induct t arbitrary: i rule: tmmul.induct, auto )
   1.269 +
   1.270 +lemma tmmul_nb[simp]: "tmbound n t \<Longrightarrow> tmbound n (tmmul t i)"
   1.271 +by (induct t arbitrary: n rule: tmmul.induct, auto )
   1.272 +lemma tmmul_blt[simp]: "tmboundslt n t \<Longrightarrow> tmboundslt n (tmmul t i)"
   1.273 +by (induct t arbitrary: i rule: tmmul.induct, auto simp add: Let_def)
   1.274 +
   1.275 +lemma tmmul_allpolys_npoly[simp]: 
   1.276 +  assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero, field})"
   1.277 +  shows "allpolys isnpoly t \<Longrightarrow> isnpoly c \<Longrightarrow> allpolys isnpoly (tmmul t c)" by (induct t rule: tmmul.induct, simp_all add: Let_def polymul_norm)
   1.278 +
   1.279 +constdefs tmneg :: "tm \<Rightarrow> tm"
   1.280 +  "tmneg t \<equiv> tmmul t (C (- 1,1))"
   1.281 +
   1.282 +constdefs tmsub :: "tm \<Rightarrow> tm \<Rightarrow> tm"
   1.283 +  "tmsub s t \<equiv> (if s = t then CP 0\<^sub>p else tmadd (s,tmneg t))"
   1.284 +
   1.285 +lemma tmneg[simp]: "Itm vs bs (tmneg t) = Itm vs bs (Neg t)"
   1.286 +using tmneg_def[of t] 
   1.287 +apply simp
   1.288 +apply (subst number_of_Min)
   1.289 +apply (simp only: of_int_minus)
   1.290 +apply simp
   1.291 +done
   1.292 +
   1.293 +lemma tmneg_nb0[simp]: "tmbound0 t \<Longrightarrow> tmbound0 (tmneg t)"
   1.294 +using tmneg_def by simp
   1.295 +
   1.296 +lemma tmneg_nb[simp]: "tmbound n t \<Longrightarrow> tmbound n (tmneg t)"
   1.297 +using tmneg_def by simp
   1.298 +lemma tmneg_blt[simp]: "tmboundslt n t \<Longrightarrow> tmboundslt n (tmneg t)"
   1.299 +using tmneg_def by simp
   1.300 +lemma [simp]: "isnpoly (C (-1,1))" unfolding isnpoly_def by simp
   1.301 +lemma tmneg_allpolys_npoly[simp]: 
   1.302 +  assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero, field})"
   1.303 +  shows "allpolys isnpoly t \<Longrightarrow> allpolys isnpoly (tmneg t)" 
   1.304 +  unfolding tmneg_def by auto
   1.305 +
   1.306 +lemma tmsub[simp]: "Itm vs bs (tmsub a b) = Itm vs bs (Sub a b)"
   1.307 +using tmsub_def by simp
   1.308 +
   1.309 +lemma tmsub_nb0[simp]: "\<lbrakk> tmbound0 t ; tmbound0 s\<rbrakk> \<Longrightarrow> tmbound0 (tmsub t s)"
   1.310 +using tmsub_def by simp
   1.311 +lemma tmsub_nb[simp]: "\<lbrakk> tmbound n t ; tmbound n s\<rbrakk> \<Longrightarrow> tmbound n (tmsub t s)"
   1.312 +using tmsub_def by simp
   1.313 +lemma tmsub_blt[simp]: "\<lbrakk>tmboundslt n t ; tmboundslt n s\<rbrakk> \<Longrightarrow> tmboundslt n (tmsub t s )"
   1.314 +using tmsub_def by simp
   1.315 +lemma tmsub_allpolys_npoly[simp]: 
   1.316 +  assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero, field})"
   1.317 +  shows "allpolys isnpoly t \<Longrightarrow> allpolys isnpoly s \<Longrightarrow> allpolys isnpoly (tmsub t s)" 
   1.318 +  unfolding tmsub_def by (simp add: isnpoly_def)
   1.319 +
   1.320 +recdef simptm "measure size"
   1.321 +  "simptm (CP j) = CP (polynate j)"
   1.322 +  "simptm (Bound n) = CNP n 1\<^sub>p (CP 0\<^sub>p)"
   1.323 +  "simptm (Neg t) = tmneg (simptm t)"
   1.324 +  "simptm (Add t s) = tmadd (simptm t,simptm s)"
   1.325 +  "simptm (Sub t s) = tmsub (simptm t) (simptm s)"
   1.326 +  "simptm (Mul i t) = (let i' = polynate i in if i' = 0\<^sub>p then CP 0\<^sub>p else tmmul (simptm t) i')"
   1.327 +  "simptm (CNP n c t) = (let c' = polynate c in if c' = 0\<^sub>p then simptm t else tmadd (CNP n c' (CP 0\<^sub>p ), simptm t))"
   1.328 +
   1.329 +lemma polynate_stupid: 
   1.330 +  assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero, field})"
   1.331 +  shows "polynate t = 0\<^sub>p \<Longrightarrow> Ipoly bs t = (0::'a::{ring_char_0,division_by_zero, field})" 
   1.332 +apply (subst polynate[symmetric])
   1.333 +apply simp
   1.334 +done
   1.335 +
   1.336 +lemma simptm_ci[simp]: "Itm vs bs (simptm t) = Itm vs bs t"
   1.337 +by (induct t rule: simptm.induct, auto simp add: tmneg tmadd tmsub tmmul Let_def polynate_stupid) 
   1.338 +
   1.339 +lemma simptm_tmbound0[simp]: 
   1.340 +  "tmbound0 t \<Longrightarrow> tmbound0 (simptm t)"
   1.341 +by (induct t rule: simptm.induct, auto simp add: Let_def)
   1.342 +
   1.343 +lemma simptm_nb[simp]: "tmbound n t \<Longrightarrow> tmbound n (simptm t)"
   1.344 +by (induct t rule: simptm.induct, auto simp add: Let_def)
   1.345 +lemma simptm_nlt[simp]: "tmboundslt n t \<Longrightarrow> tmboundslt n (simptm t)"
   1.346 +by (induct t rule: simptm.induct, auto simp add: Let_def)
   1.347 +
   1.348 +lemma [simp]: "isnpoly 0\<^sub>p" and [simp]: "isnpoly (C(1,1))" 
   1.349 +  by (simp_all add: isnpoly_def)
   1.350 +lemma simptm_allpolys_npoly[simp]: 
   1.351 +  assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero, field})"
   1.352 +  shows "allpolys isnpoly (simptm p)"
   1.353 +  by (induct p rule: simptm.induct, auto simp add: Let_def)
   1.354 +
   1.355 +consts split0 :: "tm \<Rightarrow> (poly \<times> tm)"
   1.356 +recdef split0 "measure tmsize"
   1.357 +  "split0 (Bound 0) = (1\<^sub>p, CP 0\<^sub>p)"
   1.358 +  "split0 (CNP 0 c t) = (let (c',t') = split0 t in (c +\<^sub>p c',t'))"
   1.359 +  "split0 (Neg t) = (let (c,t') = split0 t in (~\<^sub>p c,Neg t'))"
   1.360 +  "split0 (CNP n c t) = (let (c',t') = split0 t in (c',CNP n c t'))"
   1.361 +  "split0 (Add s t) = (let (c1,s') = split0 s ; (c2,t') = split0 t in (c1 +\<^sub>p c2, Add s' t'))"
   1.362 +  "split0 (Sub s t) = (let (c1,s') = split0 s ; (c2,t') = split0 t in (c1 -\<^sub>p c2, Sub s' t'))"
   1.363 +  "split0 (Mul c t) = (let (c',t') = split0 t in (c *\<^sub>p c', Mul c t'))"
   1.364 +  "split0 t = (0\<^sub>p, t)"
   1.365 +
   1.366 +lemma split0_stupid[simp]: "\<exists>x y. (x,y) = split0 p"
   1.367 +  apply (rule exI[where x="fst (split0 p)"])
   1.368 +  apply (rule exI[where x="snd (split0 p)"])
   1.369 +  by simp
   1.370 +
   1.371 +lemma split0:
   1.372 +  "tmbound 0 (snd (split0 t)) \<and> (Itm vs bs (CNP 0 (fst (split0 t)) (snd (split0 t))) = Itm vs bs t)"
   1.373 +  apply (induct t rule: split0.induct)
   1.374 +  apply simp
   1.375 +  apply (simp add: Let_def split_def ring_simps)
   1.376 +  apply (simp add: Let_def split_def ring_simps)
   1.377 +  apply (simp add: Let_def split_def ring_simps)
   1.378 +  apply (simp add: Let_def split_def ring_simps)
   1.379 +  apply (simp add: Let_def split_def ring_simps)
   1.380 +  apply (simp add: Let_def split_def mult_assoc right_distrib[symmetric])
   1.381 +  apply (simp add: Let_def split_def ring_simps)
   1.382 +  apply (simp add: Let_def split_def ring_simps)
   1.383 +  done
   1.384 +
   1.385 +lemma split0_ci: "split0 t = (c',t') \<Longrightarrow> Itm vs bs t = Itm vs bs (CNP 0 c' t')"
   1.386 +proof-
   1.387 +  fix c' t'
   1.388 +  assume "split0 t = (c', t')" hence "c' = fst (split0 t)" and "t' = snd (split0 t)" by auto
   1.389 +  with split0[where t="t" and bs="bs"] show "Itm vs bs t = Itm vs bs (CNP 0 c' t')" by simp
   1.390 +qed
   1.391 +
   1.392 +lemma split0_nb0: 
   1.393 +  assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero, field})"
   1.394 +  shows "split0 t = (c',t') \<Longrightarrow>  tmbound 0 t'"
   1.395 +proof-
   1.396 +  fix c' t'
   1.397 +  assume "split0 t = (c', t')" hence "c' = fst (split0 t)" and "t' = snd (split0 t)" by auto
   1.398 +  with conjunct1[OF split0[where t="t"]] show "tmbound 0 t'" by simp
   1.399 +qed
   1.400 +
   1.401 +lemma split0_nb0'[simp]:   assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero, field})"
   1.402 +  shows "tmbound0 (snd (split0 t))"
   1.403 +  using split0_nb0[of t "fst (split0 t)" "snd (split0 t)"] by (simp add: tmbound0_tmbound_iff)
   1.404 +
   1.405 +
   1.406 +lemma split0_nb: assumes nb:"tmbound n t" shows "tmbound n (snd (split0 t))"
   1.407 +  using nb by (induct t rule: split0.induct, auto simp add: Let_def split_def split0_stupid)
   1.408 +
   1.409 +lemma split0_blt: assumes nb:"tmboundslt n t" shows "tmboundslt n (snd (split0 t))"
   1.410 +  using nb by (induct t rule: split0.induct, auto simp add: Let_def split_def split0_stupid)
   1.411 +
   1.412 +lemma tmbound_split0: "tmbound 0 t \<Longrightarrow> Ipoly vs (fst(split0 t)) = 0"
   1.413 + by (induct t rule: split0.induct, auto simp add: Let_def split_def split0_stupid)
   1.414 +
   1.415 +lemma tmboundslt_split0: "tmboundslt n t \<Longrightarrow> Ipoly vs (fst(split0 t)) = 0 \<or> n > 0"
   1.416 +by (induct t rule: split0.induct, auto simp add: Let_def split_def split0_stupid)
   1.417 +
   1.418 +lemma tmboundslt0_split0: "tmboundslt 0 t \<Longrightarrow> Ipoly vs (fst(split0 t)) = 0"
   1.419 + by (induct t rule: split0.induct, auto simp add: Let_def split_def split0_stupid)
   1.420 +
   1.421 +lemma allpolys_split0: "allpolys isnpoly p \<Longrightarrow> allpolys isnpoly (snd (split0 p))"
   1.422 +by (induct p rule: split0.induct, auto simp  add: isnpoly_def Let_def split_def split0_stupid)
   1.423 +
   1.424 +lemma isnpoly_fst_split0:   assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero, field})"
   1.425 +  shows 
   1.426 +  "allpolys isnpoly p \<Longrightarrow> isnpoly (fst (split0 p))"
   1.427 +  by (induct p rule: split0.induct, 
   1.428 +    auto simp  add: polyadd_norm polysub_norm polyneg_norm polymul_norm 
   1.429 +    Let_def split_def split0_stupid)
   1.430 +
   1.431 +subsection{* Formulae *}
   1.432 +
   1.433 +datatype fm  =  T| F| Le tm | Lt tm | Eq tm | NEq tm|
   1.434 +  NOT fm| And fm fm|  Or fm fm| Imp fm fm| Iff fm fm| E fm| A fm
   1.435 +
   1.436 +
   1.437 +  (* A size for fm *)
   1.438 +consts fmsize :: "fm \<Rightarrow> nat"
   1.439 +recdef fmsize "measure size"
   1.440 +  "fmsize (NOT p) = 1 + fmsize p"
   1.441 +  "fmsize (And p q) = 1 + fmsize p + fmsize q"
   1.442 +  "fmsize (Or p q) = 1 + fmsize p + fmsize q"
   1.443 +  "fmsize (Imp p q) = 3 + fmsize p + fmsize q"
   1.444 +  "fmsize (Iff p q) = 3 + 2*(fmsize p + fmsize q)"
   1.445 +  "fmsize (E p) = 1 + fmsize p"
   1.446 +  "fmsize (A p) = 4+ fmsize p"
   1.447 +  "fmsize p = 1"
   1.448 +  (* several lemmas about fmsize *)
   1.449 +lemma fmsize_pos: "fmsize p > 0"	
   1.450 +by (induct p rule: fmsize.induct) simp_all
   1.451 +
   1.452 +  (* Semantics of formulae (fm) *)
   1.453 +consts Ifm ::"'a::{division_by_zero,ordered_field} list \<Rightarrow> 'a list \<Rightarrow> fm \<Rightarrow> bool"
   1.454 +primrec
   1.455 +  "Ifm vs bs T = True"
   1.456 +  "Ifm vs bs F = False"
   1.457 +  "Ifm vs bs (Lt a) = (Itm vs bs a < 0)"
   1.458 +  "Ifm vs bs (Le a) = (Itm vs bs a \<le> 0)"
   1.459 +  "Ifm vs bs (Eq a) = (Itm vs bs a = 0)"
   1.460 +  "Ifm vs bs (NEq a) = (Itm vs bs a \<noteq> 0)"
   1.461 +  "Ifm vs bs (NOT p) = (\<not> (Ifm vs bs p))"
   1.462 +  "Ifm vs bs (And p q) = (Ifm vs bs p \<and> Ifm vs bs q)"
   1.463 +  "Ifm vs bs (Or p q) = (Ifm vs bs p \<or> Ifm vs bs q)"
   1.464 +  "Ifm vs bs (Imp p q) = ((Ifm vs bs p) \<longrightarrow> (Ifm vs bs q))"
   1.465 +  "Ifm vs bs (Iff p q) = (Ifm vs bs p = Ifm vs bs q)"
   1.466 +  "Ifm vs bs (E p) = (\<exists> x. Ifm vs (x#bs) p)"
   1.467 +  "Ifm vs bs (A p) = (\<forall> x. Ifm vs (x#bs) p)"
   1.468 +
   1.469 +consts not:: "fm \<Rightarrow> fm"
   1.470 +recdef not "measure size"
   1.471 +  "not (NOT (NOT p)) = not p"
   1.472 +  "not (NOT p) = p"
   1.473 +  "not T = F"
   1.474 +  "not F = T"
   1.475 +  "not (Lt t) = Le (tmneg t)"
   1.476 +  "not (Le t) = Lt (tmneg t)"
   1.477 +  "not (Eq t) = NEq t"
   1.478 +  "not (NEq t) = Eq t"
   1.479 +  "not p = NOT p"
   1.480 +lemma not[simp]: "Ifm vs bs (not p) = Ifm vs bs (NOT p)"
   1.481 +by (induct p rule: not.induct) auto
   1.482 +
   1.483 +constdefs conj :: "fm \<Rightarrow> fm \<Rightarrow> fm"
   1.484 +  "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 
   1.485 +   if p = q then p else And p q)"
   1.486 +lemma conj[simp]: "Ifm vs bs (conj p q) = Ifm vs bs (And p q)"
   1.487 +by (cases "p=F \<or> q=F",simp_all add: conj_def) (cases p,simp_all)
   1.488 +
   1.489 +constdefs disj :: "fm \<Rightarrow> fm \<Rightarrow> fm"
   1.490 +  "disj p q \<equiv> (if (p = T \<or> q=T) then T else if p=F then q else if q=F then p 
   1.491 +       else if p=q then p else Or p q)"
   1.492 +
   1.493 +lemma disj[simp]: "Ifm vs bs (disj p q) = Ifm vs bs (Or p q)"
   1.494 +by (cases "p=T \<or> q=T",simp_all add: disj_def) (cases p,simp_all)
   1.495 +
   1.496 +constdefs  imp :: "fm \<Rightarrow> fm \<Rightarrow> fm"
   1.497 +  "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 
   1.498 +    else Imp p q)"
   1.499 +lemma imp[simp]: "Ifm vs bs (imp p q) = Ifm vs bs (Imp p q)"
   1.500 +by (cases "p=F \<or> q=T",simp_all add: imp_def) 
   1.501 +
   1.502 +constdefs   iff :: "fm \<Rightarrow> fm \<Rightarrow> fm"
   1.503 +  "iff p q \<equiv> (if (p = q) then T else if (p = NOT q \<or> NOT p = q) then F else 
   1.504 +       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 
   1.505 +  Iff p q)"
   1.506 +lemma iff[simp]: "Ifm vs bs (iff p q) = Ifm vs bs (Iff p q)"
   1.507 +  by (unfold iff_def,cases "p=q", simp,cases "p=NOT q", simp) (cases "NOT p= q", auto)
   1.508 +  (* Quantifier freeness *)
   1.509 +consts qfree:: "fm \<Rightarrow> bool"
   1.510 +recdef qfree "measure size"
   1.511 +  "qfree (E p) = False"
   1.512 +  "qfree (A p) = False"
   1.513 +  "qfree (NOT p) = qfree p" 
   1.514 +  "qfree (And p q) = (qfree p \<and> qfree q)" 
   1.515 +  "qfree (Or  p q) = (qfree p \<and> qfree q)" 
   1.516 +  "qfree (Imp p q) = (qfree p \<and> qfree q)" 
   1.517 +  "qfree (Iff p q) = (qfree p \<and> qfree q)"
   1.518 +  "qfree p = True"
   1.519 +
   1.520 +  (* Boundedness and substitution *)
   1.521 +
   1.522 +consts boundslt :: "nat \<Rightarrow> fm \<Rightarrow> bool"
   1.523 +primrec
   1.524 +  "boundslt n T = True"
   1.525 +  "boundslt n F = True"
   1.526 +  "boundslt n (Lt t) = (tmboundslt n t)"
   1.527 +  "boundslt n (Le t) = (tmboundslt n t)"
   1.528 +  "boundslt n (Eq t) = (tmboundslt n t)"
   1.529 +  "boundslt n (NEq t) = (tmboundslt n t)"
   1.530 +  "boundslt n (NOT p) = boundslt n p"
   1.531 +  "boundslt n (And p q) = (boundslt n p \<and> boundslt n q)"
   1.532 +  "boundslt n (Or p q) = (boundslt n p \<and> boundslt n q)"
   1.533 +  "boundslt n (Imp p q) = ((boundslt n p) \<and> (boundslt n q))"
   1.534 +  "boundslt n (Iff p q) = (boundslt n p \<and> boundslt n q)"
   1.535 +  "boundslt n (E p) = boundslt (Suc n) p"
   1.536 +  "boundslt n (A p) = boundslt (Suc n) p"
   1.537 +
   1.538 +consts 
   1.539 +  bound0:: "fm \<Rightarrow> bool" (* A Formula is independent of Bound 0 *)
   1.540 +  bound:: "nat \<Rightarrow> fm \<Rightarrow> bool" (* A Formula is independent of Bound n *)
   1.541 +  decr0 :: "fm \<Rightarrow> fm"
   1.542 +  decr :: "nat \<Rightarrow> fm \<Rightarrow> fm"
   1.543 +recdef bound0 "measure size"
   1.544 +  "bound0 T = True"
   1.545 +  "bound0 F = True"
   1.546 +  "bound0 (Lt a) = tmbound0 a"
   1.547 +  "bound0 (Le a) = tmbound0 a"
   1.548 +  "bound0 (Eq a) = tmbound0 a"
   1.549 +  "bound0 (NEq a) = tmbound0 a"
   1.550 +  "bound0 (NOT p) = bound0 p"
   1.551 +  "bound0 (And p q) = (bound0 p \<and> bound0 q)"
   1.552 +  "bound0 (Or p q) = (bound0 p \<and> bound0 q)"
   1.553 +  "bound0 (Imp p q) = ((bound0 p) \<and> (bound0 q))"
   1.554 +  "bound0 (Iff p q) = (bound0 p \<and> bound0 q)"
   1.555 +  "bound0 p = False"
   1.556 +lemma bound0_I:
   1.557 +  assumes bp: "bound0 p"
   1.558 +  shows "Ifm vs (b#bs) p = Ifm vs (b'#bs) p"
   1.559 +using bp tmbound0_I[where b="b" and bs="bs" and b'="b'"]
   1.560 +by (induct p rule: bound0.induct,auto simp add: nth_pos2)
   1.561 +
   1.562 +primrec
   1.563 +  "bound m T = True"
   1.564 +  "bound m F = True"
   1.565 +  "bound m (Lt t) = tmbound m t"
   1.566 +  "bound m (Le t) = tmbound m t"
   1.567 +  "bound m (Eq t) = tmbound m t"
   1.568 +  "bound m (NEq t) = tmbound m t"
   1.569 +  "bound m (NOT p) = bound m p"
   1.570 +  "bound m (And p q) = (bound m p \<and> bound m q)"
   1.571 +  "bound m (Or p q) = (bound m p \<and> bound m q)"
   1.572 +  "bound m (Imp p q) = ((bound m p) \<and> (bound m q))"
   1.573 +  "bound m (Iff p q) = (bound m p \<and> bound m q)"
   1.574 +  "bound m (E p) = bound (Suc m) p"
   1.575 +  "bound m (A p) = bound (Suc m) p"
   1.576 +
   1.577 +lemma bound_I:
   1.578 +  assumes bnd: "boundslt (length bs) p" and nb: "bound n p" and le: "n \<le> length bs"
   1.579 +  shows "Ifm vs (bs[n:=x]) p = Ifm vs bs p"
   1.580 +  using bnd nb le tmbound_I[where bs=bs and vs = vs]
   1.581 +proof(induct p arbitrary: bs n rule: bound.induct)
   1.582 +  case (E p bs n) 
   1.583 +  {fix y
   1.584 +    from prems have bnd: "boundslt (length (y#bs)) p" 
   1.585 +      and nb: "bound (Suc n) p" and le: "Suc n \<le> length (y#bs)" by simp+
   1.586 +    from E.hyps[OF bnd nb le tmbound_I] have "Ifm vs ((y#bs)[Suc n:=x]) p = Ifm vs (y#bs) p" .   }
   1.587 +  thus ?case by simp 
   1.588 +next
   1.589 +  case (A p bs n) {fix y
   1.590 +    from prems have bnd: "boundslt (length (y#bs)) p" 
   1.591 +      and nb: "bound (Suc n) p" and le: "Suc n \<le> length (y#bs)" by simp+
   1.592 +    from A.hyps[OF bnd nb le tmbound_I] have "Ifm vs ((y#bs)[Suc n:=x]) p = Ifm vs (y#bs) p" .   }
   1.593 +  thus ?case by simp 
   1.594 +qed auto
   1.595 +
   1.596 +recdef decr0 "measure size"
   1.597 +  "decr0 (Lt a) = Lt (decrtm0 a)"
   1.598 +  "decr0 (Le a) = Le (decrtm0 a)"
   1.599 +  "decr0 (Eq a) = Eq (decrtm0 a)"
   1.600 +  "decr0 (NEq a) = NEq (decrtm0 a)"
   1.601 +  "decr0 (NOT p) = NOT (decr0 p)" 
   1.602 +  "decr0 (And p q) = conj (decr0 p) (decr0 q)"
   1.603 +  "decr0 (Or p q) = disj (decr0 p) (decr0 q)"
   1.604 +  "decr0 (Imp p q) = imp (decr0 p) (decr0 q)"
   1.605 +  "decr0 (Iff p q) = iff (decr0 p) (decr0 q)"
   1.606 +  "decr0 p = p"
   1.607 +
   1.608 +lemma decr0: assumes nb: "bound0 p"
   1.609 +  shows "Ifm vs (x#bs) p = Ifm vs bs (decr0 p)"
   1.610 +  using nb 
   1.611 +  by (induct p rule: decr0.induct, simp_all add: decrtm0)
   1.612 +
   1.613 +primrec
   1.614 +  "decr m T = T"
   1.615 +  "decr m F = F"
   1.616 +  "decr m (Lt t) = (Lt (decrtm m t))"
   1.617 +  "decr m (Le t) = (Le (decrtm m t))"
   1.618 +  "decr m (Eq t) = (Eq (decrtm m t))"
   1.619 +  "decr m (NEq t) = (NEq (decrtm m t))"
   1.620 +  "decr m (NOT p) = NOT (decr m p)" 
   1.621 +  "decr m (And p q) = conj (decr m p) (decr m q)"
   1.622 +  "decr m (Or p q) = disj (decr m p) (decr m q)"
   1.623 +  "decr m (Imp p q) = imp (decr m p) (decr m q)"
   1.624 +  "decr m (Iff p q) = iff (decr m p) (decr m q)"
   1.625 +  "decr m (E p) = E (decr (Suc m) p)"
   1.626 +  "decr m (A p) = A (decr (Suc m) p)"
   1.627 +
   1.628 +lemma decr: assumes  bnd: "boundslt (length bs) p" and nb: "bound m p" 
   1.629 +  and nle: "m < length bs" 
   1.630 +  shows "Ifm vs (removen m bs) (decr m p) = Ifm vs bs p"
   1.631 +  using bnd nb nle
   1.632 +proof(induct p arbitrary: bs m rule: decr.induct)
   1.633 +  case (E p bs m) 
   1.634 +  {fix x
   1.635 +    from prems have bnd: "boundslt (length (x#bs)) p" and nb: "bound (Suc m) p" 
   1.636 +  and nle: "Suc m < length (x#bs)" by auto
   1.637 +    from prems(4)[OF bnd nb nle] have "Ifm vs (removen (Suc m) (x#bs)) (decr (Suc m) p) = Ifm vs (x#bs) p".
   1.638 +  } thus ?case by auto 
   1.639 +next
   1.640 +  case (A p bs m)  
   1.641 +  {fix x
   1.642 +    from prems have bnd: "boundslt (length (x#bs)) p" and nb: "bound (Suc m) p" 
   1.643 +  and nle: "Suc m < length (x#bs)" by auto
   1.644 +    from prems(4)[OF bnd nb nle] have "Ifm vs (removen (Suc m) (x#bs)) (decr (Suc m) p) = Ifm vs (x#bs) p".
   1.645 +  } thus ?case by auto
   1.646 +qed (auto simp add: decrtm removen_nth)
   1.647 +
   1.648 +consts
   1.649 +  subst0:: "tm \<Rightarrow> fm \<Rightarrow> fm"
   1.650 +
   1.651 +primrec
   1.652 +  "subst0 t T = T"
   1.653 +  "subst0 t F = F"
   1.654 +  "subst0 t (Lt a) = Lt (tmsubst0 t a)"
   1.655 +  "subst0 t (Le a) = Le (tmsubst0 t a)"
   1.656 +  "subst0 t (Eq a) = Eq (tmsubst0 t a)"
   1.657 +  "subst0 t (NEq a) = NEq (tmsubst0 t a)"
   1.658 +  "subst0 t (NOT p) = NOT (subst0 t p)"
   1.659 +  "subst0 t (And p q) = And (subst0 t p) (subst0 t q)"
   1.660 +  "subst0 t (Or p q) = Or (subst0 t p) (subst0 t q)"
   1.661 +  "subst0 t (Imp p q) = Imp (subst0 t p)  (subst0 t q)"
   1.662 +  "subst0 t (Iff p q) = Iff (subst0 t p) (subst0 t q)"
   1.663 +  "subst0 t (E p) = E p"
   1.664 +  "subst0 t (A p) = A p"
   1.665 +
   1.666 +lemma subst0: assumes qf: "qfree p"
   1.667 +  shows "Ifm vs (x#bs) (subst0 t p) = Ifm vs ((Itm vs (x#bs) t)#bs) p"
   1.668 +using qf tmsubst0[where x="x" and bs="bs" and t="t"]
   1.669 +by (induct p rule: subst0.induct, auto)
   1.670 +
   1.671 +lemma subst0_nb:
   1.672 +  assumes bp: "tmbound0 t" and qf: "qfree p"
   1.673 +  shows "bound0 (subst0 t p)"
   1.674 +using qf tmsubst0_nb[OF bp] bp
   1.675 +by (induct p rule: subst0.induct, auto)
   1.676 +
   1.677 +consts   subst:: "nat \<Rightarrow> tm \<Rightarrow> fm \<Rightarrow> fm" 
   1.678 +primrec
   1.679 +  "subst n t T = T"
   1.680 +  "subst n t F = F"
   1.681 +  "subst n t (Lt a) = Lt (tmsubst n t a)"
   1.682 +  "subst n t (Le a) = Le (tmsubst n t a)"
   1.683 +  "subst n t (Eq a) = Eq (tmsubst n t a)"
   1.684 +  "subst n t (NEq a) = NEq (tmsubst n t a)"
   1.685 +  "subst n t (NOT p) = NOT (subst n t p)"
   1.686 +  "subst n t (And p q) = And (subst n t p) (subst n t q)"
   1.687 +  "subst n t (Or p q) = Or (subst n t p) (subst n t q)"
   1.688 +  "subst n t (Imp p q) = Imp (subst n t p)  (subst n t q)"
   1.689 +  "subst n t (Iff p q) = Iff (subst n t p) (subst n t q)"
   1.690 +  "subst n t (E p) = E (subst (Suc n) (incrtm0 t) p)"
   1.691 +  "subst n t (A p) = A (subst (Suc n) (incrtm0 t) p)"
   1.692 +
   1.693 +lemma subst: assumes nb: "boundslt (length bs) p" and nlm: "n \<le> length bs"
   1.694 +  shows "Ifm vs bs (subst n t p) = Ifm vs (bs[n:= Itm vs bs t]) p"
   1.695 +  using nb nlm
   1.696 +proof (induct p arbitrary: bs n t rule: subst0.induct)
   1.697 +  case (E p bs n) 
   1.698 +  {fix x 
   1.699 +    from prems have bn: "boundslt (length (x#bs)) p" by simp 
   1.700 +      from prems have nlm: "Suc n \<le> length (x#bs)" by simp
   1.701 +    from prems(3)[OF bn nlm] have "Ifm vs (x#bs) (subst (Suc n) (incrtm0 t) p) = Ifm vs ((x#bs)[Suc n:= Itm vs (x#bs) (incrtm0 t)]) p" by simp 
   1.702 +    hence "Ifm vs (x#bs) (subst (Suc n) (incrtm0 t) p) = Ifm vs (x#bs[n:= Itm vs bs t]) p"
   1.703 +    by (simp add: incrtm0[where x="x" and bs="bs" and t="t"]) }  
   1.704 +thus ?case by simp 
   1.705 +next
   1.706 +  case (A p bs n)   
   1.707 +  {fix x 
   1.708 +    from prems have bn: "boundslt (length (x#bs)) p" by simp 
   1.709 +      from prems have nlm: "Suc n \<le> length (x#bs)" by simp
   1.710 +    from prems(3)[OF bn nlm] have "Ifm vs (x#bs) (subst (Suc n) (incrtm0 t) p) = Ifm vs ((x#bs)[Suc n:= Itm vs (x#bs) (incrtm0 t)]) p" by simp 
   1.711 +    hence "Ifm vs (x#bs) (subst (Suc n) (incrtm0 t) p) = Ifm vs (x#bs[n:= Itm vs bs t]) p"
   1.712 +    by (simp add: incrtm0[where x="x" and bs="bs" and t="t"]) }  
   1.713 +thus ?case by simp 
   1.714 +qed(auto simp add: tmsubst)
   1.715 +
   1.716 +lemma subst_nb: assumes tnb: "tmbound m t"
   1.717 +shows "bound m (subst m t p)"
   1.718 +using tnb tmsubst_nb incrtm0_tmbound
   1.719 +by (induct p arbitrary: m t rule: subst.induct, auto)
   1.720 +
   1.721 +lemma not_qf[simp]: "qfree p \<Longrightarrow> qfree (not p)"
   1.722 +by (induct p rule: not.induct, auto)
   1.723 +lemma not_bn0[simp]: "bound0 p \<Longrightarrow> bound0 (not p)"
   1.724 +by (induct p rule: not.induct, auto)
   1.725 +lemma not_nb[simp]: "bound n p \<Longrightarrow> bound n (not p)"
   1.726 +by (induct p rule: not.induct, auto)
   1.727 +lemma not_blt[simp]: "boundslt n p \<Longrightarrow> boundslt n (not p)"
   1.728 + by (induct p rule: not.induct, auto)
   1.729 +
   1.730 +lemma conj_qf[simp]: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (conj p q)"
   1.731 +using conj_def by auto 
   1.732 +lemma conj_nb0[simp]: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (conj p q)"
   1.733 +using conj_def by auto 
   1.734 +lemma conj_nb[simp]: "\<lbrakk>bound n p ; bound n q\<rbrakk> \<Longrightarrow> bound n (conj p q)"
   1.735 +using conj_def by auto 
   1.736 +lemma conj_blt[simp]: "boundslt n p \<Longrightarrow> boundslt n q \<Longrightarrow> boundslt n (conj p q)"
   1.737 +using conj_def by auto 
   1.738 +
   1.739 +lemma disj_qf[simp]: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (disj p q)"
   1.740 +using disj_def by auto 
   1.741 +lemma disj_nb0[simp]: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (disj p q)"
   1.742 +using disj_def by auto 
   1.743 +lemma disj_nb[simp]: "\<lbrakk>bound n p ; bound n q\<rbrakk> \<Longrightarrow> bound n (disj p q)"
   1.744 +using disj_def by auto 
   1.745 +lemma disj_blt[simp]: "boundslt n p \<Longrightarrow> boundslt n q \<Longrightarrow> boundslt n (disj p q)"
   1.746 +using disj_def by auto 
   1.747 +
   1.748 +lemma imp_qf[simp]: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (imp p q)"
   1.749 +using imp_def by (cases "p=F \<or> q=T",simp_all add: imp_def)
   1.750 +lemma imp_nb0[simp]: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (imp p q)"
   1.751 +using imp_def by (cases "p=F \<or> q=T \<or> p=q",simp_all add: imp_def)
   1.752 +lemma imp_nb[simp]: "\<lbrakk>bound n p ; bound n q\<rbrakk> \<Longrightarrow> bound n (imp p q)"
   1.753 +using imp_def by (cases "p=F \<or> q=T \<or> p=q",simp_all add: imp_def)
   1.754 +lemma imp_blt[simp]: "boundslt n p \<Longrightarrow> boundslt n q \<Longrightarrow> boundslt n (imp p q)"
   1.755 +using imp_def by auto 
   1.756 +
   1.757 +lemma iff_qf[simp]: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (iff p q)"
   1.758 +  by (unfold iff_def,cases "p=q", auto)
   1.759 +lemma iff_nb0[simp]: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (iff p q)"
   1.760 +using iff_def by (unfold iff_def,cases "p=q", auto)
   1.761 +lemma iff_nb[simp]: "\<lbrakk>bound n p ; bound n q\<rbrakk> \<Longrightarrow> bound n (iff p q)"
   1.762 +using iff_def by (unfold iff_def,cases "p=q", auto)
   1.763 +lemma iff_blt[simp]: "boundslt n p \<Longrightarrow> boundslt n q \<Longrightarrow> boundslt n (iff p q)"
   1.764 +using iff_def by auto 
   1.765 +lemma decr0_qf: "bound0 p \<Longrightarrow> qfree (decr0 p)"
   1.766 +by (induct p, simp_all)
   1.767 +
   1.768 +consts 
   1.769 +  isatom :: "fm \<Rightarrow> bool" (* test for atomicity *)
   1.770 +recdef isatom "measure size"
   1.771 +  "isatom T = True"
   1.772 +  "isatom F = True"
   1.773 +  "isatom (Lt a) = True"
   1.774 +  "isatom (Le a) = True"
   1.775 +  "isatom (Eq a) = True"
   1.776 +  "isatom (NEq a) = True"
   1.777 +  "isatom p = False"
   1.778 +
   1.779 +lemma bound0_qf: "bound0 p \<Longrightarrow> qfree p"
   1.780 +by (induct p, simp_all)
   1.781 +
   1.782 +constdefs djf:: "('a \<Rightarrow> fm) \<Rightarrow> 'a \<Rightarrow> fm \<Rightarrow> fm"
   1.783 +  "djf f p q \<equiv> (if q=T then T else if q=F then f p else 
   1.784 +  (let fp = f p in case fp of T \<Rightarrow> T | F \<Rightarrow> q | _ \<Rightarrow> Or (f p) q))"
   1.785 +constdefs evaldjf:: "('a \<Rightarrow> fm) \<Rightarrow> 'a list \<Rightarrow> fm"
   1.786 +  "evaldjf f ps \<equiv> foldr (djf f) ps F"
   1.787 +
   1.788 +lemma djf_Or: "Ifm vs bs (djf f p q) = Ifm vs bs (Or (f p) q)"
   1.789 +by (cases "q=T", simp add: djf_def,cases "q=F",simp add: djf_def) 
   1.790 +(cases "f p", simp_all add: Let_def djf_def) 
   1.791 +
   1.792 +lemma evaldjf_ex: "Ifm vs bs (evaldjf f ps) = (\<exists> p \<in> set ps. Ifm vs bs (f p))"
   1.793 +  by(induct ps, simp_all add: evaldjf_def djf_Or)
   1.794 +
   1.795 +lemma evaldjf_bound0: 
   1.796 +  assumes nb: "\<forall> x\<in> set xs. bound0 (f x)"
   1.797 +  shows "bound0 (evaldjf f xs)"
   1.798 +  using nb by (induct xs, auto simp add: evaldjf_def djf_def Let_def) (case_tac "f a", auto) 
   1.799 +
   1.800 +lemma evaldjf_qf: 
   1.801 +  assumes nb: "\<forall> x\<in> set xs. qfree (f x)"
   1.802 +  shows "qfree (evaldjf f xs)"
   1.803 +  using nb by (induct xs, auto simp add: evaldjf_def djf_def Let_def) (case_tac "f a", auto) 
   1.804 +
   1.805 +consts disjuncts :: "fm \<Rightarrow> fm list"
   1.806 +recdef disjuncts "measure size"
   1.807 +  "disjuncts (Or p q) = (disjuncts p) @ (disjuncts q)"
   1.808 +  "disjuncts F = []"
   1.809 +  "disjuncts p = [p]"
   1.810 +
   1.811 +lemma disjuncts: "(\<exists> q\<in> set (disjuncts p). Ifm vs bs q) = Ifm vs bs p"
   1.812 +by(induct p rule: disjuncts.induct, auto)
   1.813 +
   1.814 +lemma disjuncts_nb: "bound0 p \<Longrightarrow> \<forall> q\<in> set (disjuncts p). bound0 q"
   1.815 +proof-
   1.816 +  assume nb: "bound0 p"
   1.817 +  hence "list_all bound0 (disjuncts p)" by (induct p rule:disjuncts.induct,auto)
   1.818 +  thus ?thesis by (simp only: list_all_iff)
   1.819 +qed
   1.820 +
   1.821 +lemma disjuncts_qf: "qfree p \<Longrightarrow> \<forall> q\<in> set (disjuncts p). qfree q"
   1.822 +proof-
   1.823 +  assume qf: "qfree p"
   1.824 +  hence "list_all qfree (disjuncts p)"
   1.825 +    by (induct p rule: disjuncts.induct, auto)
   1.826 +  thus ?thesis by (simp only: list_all_iff)
   1.827 +qed
   1.828 +
   1.829 +constdefs DJ :: "(fm \<Rightarrow> fm) \<Rightarrow> fm \<Rightarrow> fm"
   1.830 +  "DJ f p \<equiv> evaldjf f (disjuncts p)"
   1.831 +
   1.832 +lemma DJ: assumes fdj: "\<forall> p q. Ifm vs bs (f (Or p q)) = Ifm vs bs (Or (f p) (f q))"
   1.833 +  and fF: "f F = F"
   1.834 +  shows "Ifm vs bs (DJ f p) = Ifm vs bs (f p)"
   1.835 +proof-
   1.836 +  have "Ifm vs bs (DJ f p) = (\<exists> q \<in> set (disjuncts p). Ifm vs bs (f q))"
   1.837 +    by (simp add: DJ_def evaldjf_ex) 
   1.838 +  also have "\<dots> = Ifm vs bs (f p)" using fdj fF by (induct p rule: disjuncts.induct, auto)
   1.839 +  finally show ?thesis .
   1.840 +qed
   1.841 +
   1.842 +lemma DJ_qf: assumes 
   1.843 +  fqf: "\<forall> p. qfree p \<longrightarrow> qfree (f p)"
   1.844 +  shows "\<forall>p. qfree p \<longrightarrow> qfree (DJ f p) "
   1.845 +proof(clarify)
   1.846 +  fix  p assume qf: "qfree p"
   1.847 +  have th: "DJ f p = evaldjf f (disjuncts p)" by (simp add: DJ_def)
   1.848 +  from disjuncts_qf[OF qf] have "\<forall> q\<in> set (disjuncts p). qfree q" .
   1.849 +  with fqf have th':"\<forall> q\<in> set (disjuncts p). qfree (f q)" by blast
   1.850 +  
   1.851 +  from evaldjf_qf[OF th'] th show "qfree (DJ f p)" by simp
   1.852 +qed
   1.853 +
   1.854 +lemma DJ_qe: assumes qe: "\<forall> bs p. qfree p \<longrightarrow> qfree (qe p) \<and> (Ifm vs bs (qe p) = Ifm vs bs (E p))"
   1.855 +  shows "\<forall> bs p. qfree p \<longrightarrow> qfree (DJ qe p) \<and> (Ifm vs bs ((DJ qe p)) = Ifm vs bs (E p))"
   1.856 +proof(clarify)
   1.857 +  fix p::fm and bs
   1.858 +  assume qf: "qfree p"
   1.859 +  from qe have qth: "\<forall> p. qfree p \<longrightarrow> qfree (qe p)" by blast
   1.860 +  from DJ_qf[OF qth] qf have qfth:"qfree (DJ qe p)" by auto
   1.861 +  have "Ifm vs bs (DJ qe p) = (\<exists> q\<in> set (disjuncts p). Ifm vs bs (qe q))"
   1.862 +    by (simp add: DJ_def evaldjf_ex)
   1.863 +  also have "\<dots> = (\<exists> q \<in> set(disjuncts p). Ifm vs bs (E q))" using qe disjuncts_qf[OF qf] by auto
   1.864 +  also have "\<dots> = Ifm vs bs (E p)" by (induct p rule: disjuncts.induct, auto)
   1.865 +  finally show "qfree (DJ qe p) \<and> Ifm vs bs (DJ qe p) = Ifm vs bs (E p)" using qfth by blast
   1.866 +qed
   1.867 +
   1.868 +consts conjuncts :: "fm \<Rightarrow> fm list"
   1.869 +
   1.870 +recdef conjuncts "measure size"
   1.871 +  "conjuncts (And p q) = (conjuncts p) @ (conjuncts q)"
   1.872 +  "conjuncts T = []"
   1.873 +  "conjuncts p = [p]"
   1.874 +
   1.875 +constdefs list_conj :: "fm list \<Rightarrow> fm"
   1.876 +  "list_conj ps \<equiv> foldr conj ps T"
   1.877 +
   1.878 +constdefs CJNB:: "(fm \<Rightarrow> fm) \<Rightarrow> fm \<Rightarrow> fm"
   1.879 +  "CJNB f p \<equiv> (let cjs = conjuncts p ; (yes,no) = partition bound0 cjs
   1.880 +                   in conj (decr0 (list_conj yes)) (f (list_conj no)))"
   1.881 +
   1.882 +lemma conjuncts_qf: "qfree p \<Longrightarrow> \<forall> q\<in> set (conjuncts p). qfree q"
   1.883 +proof-
   1.884 +  assume qf: "qfree p"
   1.885 +  hence "list_all qfree (conjuncts p)"
   1.886 +    by (induct p rule: conjuncts.induct, auto)
   1.887 +  thus ?thesis by (simp only: list_all_iff)
   1.888 +qed
   1.889 +
   1.890 +lemma conjuncts: "(\<forall> q\<in> set (conjuncts p). Ifm vs bs q) = Ifm vs bs p"
   1.891 +by(induct p rule: conjuncts.induct, auto)
   1.892 +
   1.893 +lemma conjuncts_nb: "bound0 p \<Longrightarrow> \<forall> q\<in> set (conjuncts p). bound0 q"
   1.894 +proof-
   1.895 +  assume nb: "bound0 p"
   1.896 +  hence "list_all bound0 (conjuncts p)" by (induct p rule:conjuncts.induct,auto)
   1.897 +  thus ?thesis by (simp only: list_all_iff)
   1.898 +qed
   1.899 +
   1.900 +fun islin :: "fm \<Rightarrow> bool" where
   1.901 +  "islin (And p q) = (islin p \<and> islin q \<and> p \<noteq> T \<and> p \<noteq> F \<and> q \<noteq> T \<and> q \<noteq> F)"
   1.902 +| "islin (Or p q) = (islin p \<and> islin q \<and> p \<noteq> T \<and> p \<noteq> F \<and> q \<noteq> T \<and> q \<noteq> F)"
   1.903 +| "islin (Eq (CNP 0 c s)) = (isnpoly c \<and> c \<noteq> 0\<^sub>p \<and> tmbound0 s \<and> allpolys isnpoly s)"
   1.904 +| "islin (NEq (CNP 0 c s)) = (isnpoly c \<and> c \<noteq> 0\<^sub>p \<and> tmbound0 s \<and> allpolys isnpoly s)"
   1.905 +| "islin (Lt (CNP 0 c s)) = (isnpoly c \<and> c \<noteq> 0\<^sub>p \<and> tmbound0 s \<and> allpolys isnpoly s)"
   1.906 +| "islin (Le (CNP 0 c s)) = (isnpoly c \<and> c \<noteq> 0\<^sub>p \<and> tmbound0 s \<and> allpolys isnpoly s)"
   1.907 +| "islin (NOT p) = False"
   1.908 +| "islin (Imp p q) = False"
   1.909 +| "islin (Iff p q) = False"
   1.910 +| "islin p = bound0 p"
   1.911 +
   1.912 +lemma islin_stupid: assumes nb: "tmbound0 p"
   1.913 +  shows "islin (Lt p)" and "islin (Le p)" and "islin (Eq p)" and "islin (NEq p)"
   1.914 +  using nb by (cases p, auto, case_tac nat, auto)+
   1.915 +
   1.916 +definition "lt p = (case p of CP (C c) \<Rightarrow> if 0>\<^sub>N c then T else F| _ \<Rightarrow> Lt p)"
   1.917 +definition "le p = (case p of CP (C c) \<Rightarrow> if 0\<ge>\<^sub>N c then T else F | _ \<Rightarrow> Le p)"
   1.918 +definition "eq p = (case p of CP (C c) \<Rightarrow> if c = 0\<^sub>N then T else F | _ \<Rightarrow> Eq p)"
   1.919 +definition "neq p = not (eq p)"
   1.920 +
   1.921 +lemma lt: "allpolys isnpoly p \<Longrightarrow> Ifm vs bs (lt p) = Ifm vs bs (Lt p)"
   1.922 +  apply(simp add: lt_def)
   1.923 +  apply(cases p, simp_all)
   1.924 +  apply (case_tac poly, simp_all add: isnpoly_def)
   1.925 +  done
   1.926 +
   1.927 +lemma le: "allpolys isnpoly p \<Longrightarrow> Ifm vs bs (le p) = Ifm vs bs (Le p)"
   1.928 +  apply(simp add: le_def)
   1.929 +  apply(cases p, simp_all)
   1.930 +  apply (case_tac poly, simp_all add: isnpoly_def)
   1.931 +  done
   1.932 +
   1.933 +lemma eq: "allpolys isnpoly p \<Longrightarrow> Ifm vs bs (eq p) = Ifm vs bs (Eq p)"
   1.934 +  apply(simp add: eq_def)
   1.935 +  apply(cases p, simp_all)
   1.936 +  apply (case_tac poly, simp_all add: isnpoly_def)
   1.937 +  done
   1.938 +
   1.939 +lemma neq: "allpolys isnpoly p \<Longrightarrow> Ifm vs bs (neq p) = Ifm vs bs (NEq p)"
   1.940 +  by(simp add: neq_def eq)
   1.941 +
   1.942 +lemma lt_lin: "tmbound0 p \<Longrightarrow> islin (lt p)"
   1.943 +  apply (simp add: lt_def)
   1.944 +  apply (cases p, simp_all)
   1.945 +  apply (case_tac poly, simp_all)
   1.946 +  apply (case_tac nat, simp_all)
   1.947 +  done
   1.948 +
   1.949 +lemma le_lin: "tmbound0 p \<Longrightarrow> islin (le p)"
   1.950 +  apply (simp add: le_def)
   1.951 +  apply (cases p, simp_all)
   1.952 +  apply (case_tac poly, simp_all)
   1.953 +  apply (case_tac nat, simp_all)
   1.954 +  done
   1.955 +
   1.956 +lemma eq_lin: "tmbound0 p \<Longrightarrow> islin (eq p)"
   1.957 +  apply (simp add: eq_def)
   1.958 +  apply (cases p, simp_all)
   1.959 +  apply (case_tac poly, simp_all)
   1.960 +  apply (case_tac nat, simp_all)
   1.961 +  done
   1.962 +
   1.963 +lemma neq_lin: "tmbound0 p \<Longrightarrow> islin (neq p)"
   1.964 +  apply (simp add: neq_def eq_def)
   1.965 +  apply (cases p, simp_all)
   1.966 +  apply (case_tac poly, simp_all)
   1.967 +  apply (case_tac nat, simp_all)
   1.968 +  done
   1.969 +
   1.970 +definition "simplt t = (let (c,s) = split0 (simptm t) in if c= 0\<^sub>p then lt s else Lt (CNP 0 c s))"
   1.971 +definition "simple t = (let (c,s) = split0 (simptm t) in if c= 0\<^sub>p then le s else Le (CNP 0 c s))"
   1.972 +definition "simpeq t = (let (c,s) = split0 (simptm t) in if c= 0\<^sub>p then eq s else Eq (CNP 0 c s))"
   1.973 +definition "simpneq t = (let (c,s) = split0 (simptm t) in if c= 0\<^sub>p then neq s else NEq (CNP 0 c s))"
   1.974 +
   1.975 +lemma simplt_islin[simp]:   assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
   1.976 +  shows "islin (simplt t)"
   1.977 +  unfolding simplt_def 
   1.978 +  using split0_nb0'
   1.979 +by (auto simp add: lt_lin Let_def split_def isnpoly_fst_split0[OF simptm_allpolys_npoly] islin_stupid allpolys_split0[OF simptm_allpolys_npoly])
   1.980 +  
   1.981 +lemma simple_islin[simp]:   assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
   1.982 +  shows "islin (simple t)"
   1.983 +  unfolding simple_def 
   1.984 +  using split0_nb0'
   1.985 +by (auto simp add: Let_def split_def isnpoly_fst_split0[OF simptm_allpolys_npoly] islin_stupid allpolys_split0[OF simptm_allpolys_npoly] le_lin)
   1.986 +lemma simpeq_islin[simp]:   assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
   1.987 +  shows "islin (simpeq t)"
   1.988 +  unfolding simpeq_def 
   1.989 +  using split0_nb0'
   1.990 +by (auto simp add: Let_def split_def isnpoly_fst_split0[OF simptm_allpolys_npoly] islin_stupid allpolys_split0[OF simptm_allpolys_npoly] eq_lin)
   1.991 +
   1.992 +lemma simpneq_islin[simp]:   assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
   1.993 +  shows "islin (simpneq t)"
   1.994 +  unfolding simpneq_def 
   1.995 +  using split0_nb0'
   1.996 +by (auto simp add: Let_def split_def isnpoly_fst_split0[OF simptm_allpolys_npoly] islin_stupid allpolys_split0[OF simptm_allpolys_npoly] neq_lin)
   1.997 +
   1.998 +lemma really_stupid: "\<not> (\<forall>c1 s'. (c1, s') \<noteq> split0 s)"
   1.999 +  by (cases "split0 s", auto)
  1.1000 +lemma split0_npoly:   assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
  1.1001 +  and n: "allpolys isnpoly t"
  1.1002 +  shows "isnpoly (fst (split0 t))" and "allpolys isnpoly (snd (split0 t))"
  1.1003 +  using n
  1.1004 +  by (induct t rule: split0.induct, auto simp add: Let_def split_def polyadd_norm polymul_norm polyneg_norm polysub_norm really_stupid)
  1.1005 +lemma simplt[simp]:
  1.1006 +  shows "Ifm vs bs (simplt t) = Ifm vs bs (Lt t)"
  1.1007 +proof-
  1.1008 +  have n: "allpolys isnpoly (simptm t)" by simp
  1.1009 +  let ?t = "simptm t"
  1.1010 +  {assume "fst (split0 ?t) = 0\<^sub>p" hence ?thesis
  1.1011 +      using split0[of "simptm t" vs bs] lt[OF split0_npoly(2)[OF n], of vs bs]
  1.1012 +      by (simp add: simplt_def Let_def split_def lt)}
  1.1013 +  moreover
  1.1014 +  {assume "fst (split0 ?t) \<noteq> 0\<^sub>p"
  1.1015 +    hence ?thesis using  split0[of "simptm t" vs bs] by (simp add: simplt_def Let_def split_def)
  1.1016 +  }
  1.1017 +  ultimately show ?thesis by blast
  1.1018 +qed
  1.1019 +
  1.1020 +lemma simple[simp]:
  1.1021 +  shows "Ifm vs bs (simple t) = Ifm vs bs (Le t)"
  1.1022 +proof-
  1.1023 +  have n: "allpolys isnpoly (simptm t)" by simp
  1.1024 +  let ?t = "simptm t"
  1.1025 +  {assume "fst (split0 ?t) = 0\<^sub>p" hence ?thesis
  1.1026 +      using split0[of "simptm t" vs bs] le[OF split0_npoly(2)[OF n], of vs bs]
  1.1027 +      by (simp add: simple_def Let_def split_def le)}
  1.1028 +  moreover
  1.1029 +  {assume "fst (split0 ?t) \<noteq> 0\<^sub>p"
  1.1030 +    hence ?thesis using  split0[of "simptm t" vs bs] by (simp add: simple_def Let_def split_def)
  1.1031 +  }
  1.1032 +  ultimately show ?thesis by blast
  1.1033 +qed
  1.1034 +
  1.1035 +lemma simpeq[simp]:
  1.1036 +  shows "Ifm vs bs (simpeq t) = Ifm vs bs (Eq t)"
  1.1037 +proof-
  1.1038 +  have n: "allpolys isnpoly (simptm t)" by simp
  1.1039 +  let ?t = "simptm t"
  1.1040 +  {assume "fst (split0 ?t) = 0\<^sub>p" hence ?thesis
  1.1041 +      using split0[of "simptm t" vs bs] eq[OF split0_npoly(2)[OF n], of vs bs]
  1.1042 +      by (simp add: simpeq_def Let_def split_def)}
  1.1043 +  moreover
  1.1044 +  {assume "fst (split0 ?t) \<noteq> 0\<^sub>p"
  1.1045 +    hence ?thesis using  split0[of "simptm t" vs bs] by (simp add: simpeq_def Let_def split_def)
  1.1046 +  }
  1.1047 +  ultimately show ?thesis by blast
  1.1048 +qed
  1.1049 +
  1.1050 +lemma simpneq[simp]:
  1.1051 +  shows "Ifm vs bs (simpneq t) = Ifm vs bs (NEq t)"
  1.1052 +proof-
  1.1053 +  have n: "allpolys isnpoly (simptm t)" by simp
  1.1054 +  let ?t = "simptm t"
  1.1055 +  {assume "fst (split0 ?t) = 0\<^sub>p" hence ?thesis
  1.1056 +      using split0[of "simptm t" vs bs] neq[OF split0_npoly(2)[OF n], of vs bs]
  1.1057 +      by (simp add: simpneq_def Let_def split_def )}
  1.1058 +  moreover
  1.1059 +  {assume "fst (split0 ?t) \<noteq> 0\<^sub>p"
  1.1060 +    hence ?thesis using  split0[of "simptm t" vs bs] by (simp add: simpneq_def Let_def split_def)
  1.1061 +  }
  1.1062 +  ultimately show ?thesis by blast
  1.1063 +qed
  1.1064 +
  1.1065 +lemma lt_nb: "tmbound0 t \<Longrightarrow> bound0 (lt t)"
  1.1066 +  apply (simp add: lt_def)
  1.1067 +  apply (cases t, auto)
  1.1068 +  apply (case_tac poly, auto)
  1.1069 +  done
  1.1070 +
  1.1071 +lemma le_nb: "tmbound0 t \<Longrightarrow> bound0 (le t)"
  1.1072 +  apply (simp add: le_def)
  1.1073 +  apply (cases t, auto)
  1.1074 +  apply (case_tac poly, auto)
  1.1075 +  done
  1.1076 +
  1.1077 +lemma eq_nb: "tmbound0 t \<Longrightarrow> bound0 (eq t)"
  1.1078 +  apply (simp add: eq_def)
  1.1079 +  apply (cases t, auto)
  1.1080 +  apply (case_tac poly, auto)
  1.1081 +  done
  1.1082 +
  1.1083 +lemma neq_nb: "tmbound0 t \<Longrightarrow> bound0 (neq t)"
  1.1084 +  apply (simp add: neq_def eq_def)
  1.1085 +  apply (cases t, auto)
  1.1086 +  apply (case_tac poly, auto)
  1.1087 +  done
  1.1088 +
  1.1089 +lemma simplt_nb[simp]:   assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
  1.1090 +  shows "tmbound0 t \<Longrightarrow> bound0 (simplt t)"
  1.1091 +  using split0 [of "simptm t" vs bs]
  1.1092 +proof(simp add: simplt_def Let_def split_def)
  1.1093 +  assume nb: "tmbound0 t"
  1.1094 +  hence nb': "tmbound0 (simptm t)" by simp
  1.1095 +  let ?c = "fst (split0 (simptm t))"
  1.1096 +  from tmbound_split0[OF nb'[unfolded tmbound0_tmbound_iff[symmetric]]]
  1.1097 +  have th: "\<forall>bs. Ipoly bs ?c = Ipoly bs 0\<^sub>p" by auto
  1.1098 +  from isnpoly_fst_split0[OF simptm_allpolys_npoly[of t]]
  1.1099 +  have ths: "isnpolyh ?c 0" "isnpolyh 0\<^sub>p 0" by (simp_all add: isnpoly_def)
  1.1100 +  from iffD1[OF isnpolyh_unique[OF ths] th]
  1.1101 +  have "fst (split0 (simptm t)) = 0\<^sub>p" . 
  1.1102 +  thus "(fst (split0 (simptm t)) = 0\<^sub>p \<longrightarrow> bound0 (lt (snd (split0 (simptm t))))) \<and>
  1.1103 +       fst (split0 (simptm t)) = 0\<^sub>p" by (simp add: simplt_def Let_def split_def lt_nb)
  1.1104 +qed
  1.1105 +
  1.1106 +lemma simple_nb[simp]:   assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
  1.1107 +  shows "tmbound0 t \<Longrightarrow> bound0 (simple t)"
  1.1108 +  using split0 [of "simptm t" vs bs]
  1.1109 +proof(simp add: simple_def Let_def split_def)
  1.1110 +  assume nb: "tmbound0 t"
  1.1111 +  hence nb': "tmbound0 (simptm t)" by simp
  1.1112 +  let ?c = "fst (split0 (simptm t))"
  1.1113 +  from tmbound_split0[OF nb'[unfolded tmbound0_tmbound_iff[symmetric]]]
  1.1114 +  have th: "\<forall>bs. Ipoly bs ?c = Ipoly bs 0\<^sub>p" by auto
  1.1115 +  from isnpoly_fst_split0[OF simptm_allpolys_npoly[of t]]
  1.1116 +  have ths: "isnpolyh ?c 0" "isnpolyh 0\<^sub>p 0" by (simp_all add: isnpoly_def)
  1.1117 +  from iffD1[OF isnpolyh_unique[OF ths] th]
  1.1118 +  have "fst (split0 (simptm t)) = 0\<^sub>p" . 
  1.1119 +  thus "(fst (split0 (simptm t)) = 0\<^sub>p \<longrightarrow> bound0 (le (snd (split0 (simptm t))))) \<and>
  1.1120 +       fst (split0 (simptm t)) = 0\<^sub>p" by (simp add: simplt_def Let_def split_def le_nb)
  1.1121 +qed
  1.1122 +
  1.1123 +lemma simpeq_nb[simp]:   assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
  1.1124 +  shows "tmbound0 t \<Longrightarrow> bound0 (simpeq t)"
  1.1125 +  using split0 [of "simptm t" vs bs]
  1.1126 +proof(simp add: simpeq_def Let_def split_def)
  1.1127 +  assume nb: "tmbound0 t"
  1.1128 +  hence nb': "tmbound0 (simptm t)" by simp
  1.1129 +  let ?c = "fst (split0 (simptm t))"
  1.1130 +  from tmbound_split0[OF nb'[unfolded tmbound0_tmbound_iff[symmetric]]]
  1.1131 +  have th: "\<forall>bs. Ipoly bs ?c = Ipoly bs 0\<^sub>p" by auto
  1.1132 +  from isnpoly_fst_split0[OF simptm_allpolys_npoly[of t]]
  1.1133 +  have ths: "isnpolyh ?c 0" "isnpolyh 0\<^sub>p 0" by (simp_all add: isnpoly_def)
  1.1134 +  from iffD1[OF isnpolyh_unique[OF ths] th]
  1.1135 +  have "fst (split0 (simptm t)) = 0\<^sub>p" . 
  1.1136 +  thus "(fst (split0 (simptm t)) = 0\<^sub>p \<longrightarrow> bound0 (eq (snd (split0 (simptm t))))) \<and>
  1.1137 +       fst (split0 (simptm t)) = 0\<^sub>p" by (simp add: simpeq_def Let_def split_def eq_nb)
  1.1138 +qed
  1.1139 +
  1.1140 +lemma simpneq_nb[simp]:   assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
  1.1141 +  shows "tmbound0 t \<Longrightarrow> bound0 (simpneq t)"
  1.1142 +  using split0 [of "simptm t" vs bs]
  1.1143 +proof(simp add: simpneq_def Let_def split_def)
  1.1144 +  assume nb: "tmbound0 t"
  1.1145 +  hence nb': "tmbound0 (simptm t)" by simp
  1.1146 +  let ?c = "fst (split0 (simptm t))"
  1.1147 +  from tmbound_split0[OF nb'[unfolded tmbound0_tmbound_iff[symmetric]]]
  1.1148 +  have th: "\<forall>bs. Ipoly bs ?c = Ipoly bs 0\<^sub>p" by auto
  1.1149 +  from isnpoly_fst_split0[OF simptm_allpolys_npoly[of t]]
  1.1150 +  have ths: "isnpolyh ?c 0" "isnpolyh 0\<^sub>p 0" by (simp_all add: isnpoly_def)
  1.1151 +  from iffD1[OF isnpolyh_unique[OF ths] th]
  1.1152 +  have "fst (split0 (simptm t)) = 0\<^sub>p" . 
  1.1153 +  thus "(fst (split0 (simptm t)) = 0\<^sub>p \<longrightarrow> bound0 (neq (snd (split0 (simptm t))))) \<and>
  1.1154 +       fst (split0 (simptm t)) = 0\<^sub>p" by (simp add: simpneq_def Let_def split_def neq_nb)
  1.1155 +qed
  1.1156 +
  1.1157 +consts conjs   :: "fm \<Rightarrow> fm list"
  1.1158 +recdef conjs "measure size"
  1.1159 +  "conjs (And p q) = (conjs p)@(conjs q)"
  1.1160 +  "conjs T = []"
  1.1161 +  "conjs p = [p]"
  1.1162 +lemma conjs_ci: "(\<forall> q \<in> set (conjs p). Ifm vs bs q) = Ifm vs bs p"
  1.1163 +by (induct p rule: conjs.induct, auto)
  1.1164 +constdefs list_disj :: "fm list \<Rightarrow> fm"
  1.1165 +  "list_disj ps \<equiv> foldr disj ps F"
  1.1166 +
  1.1167 +lemma list_conj: "Ifm vs bs (list_conj ps) = (\<forall>p\<in> set ps. Ifm vs bs p)"
  1.1168 +  by (induct ps, auto simp add: list_conj_def)
  1.1169 +lemma list_conj_qf: " \<forall>p\<in> set ps. qfree p \<Longrightarrow> qfree (list_conj ps)"
  1.1170 +  by (induct ps, auto simp add: list_conj_def conj_qf)
  1.1171 +lemma list_disj: "Ifm vs bs (list_disj ps) = (\<exists>p\<in> set ps. Ifm vs bs p)"
  1.1172 +  by (induct ps, auto simp add: list_disj_def)
  1.1173 +
  1.1174 +lemma conj_boundslt: "boundslt n p \<Longrightarrow> boundslt n q \<Longrightarrow> boundslt n (conj p q)"
  1.1175 +  unfolding conj_def by auto
  1.1176 +
  1.1177 +lemma conjs_nb: "bound n p \<Longrightarrow> \<forall>q\<in> set (conjs p). bound n q"
  1.1178 +  apply (induct p rule: conjs.induct) 
  1.1179 +  apply (unfold conjs.simps)
  1.1180 +  apply (unfold set_append)
  1.1181 +  apply (unfold ball_Un)
  1.1182 +  apply (unfold bound.simps)
  1.1183 +  apply auto
  1.1184 +  done
  1.1185 +
  1.1186 +lemma conjs_boundslt: "boundslt n p \<Longrightarrow> \<forall>q\<in> set (conjs p). boundslt n q"
  1.1187 +  apply (induct p rule: conjs.induct) 
  1.1188 +  apply (unfold conjs.simps)
  1.1189 +  apply (unfold set_append)
  1.1190 +  apply (unfold ball_Un)
  1.1191 +  apply (unfold boundslt.simps)
  1.1192 +  apply blast
  1.1193 +by simp_all
  1.1194 +
  1.1195 +lemma list_conj_boundslt: " \<forall>p\<in> set ps. boundslt n p \<Longrightarrow> boundslt n (list_conj ps)"
  1.1196 +  unfolding list_conj_def
  1.1197 +  by (induct ps, auto simp add: conj_boundslt)
  1.1198 +
  1.1199 +lemma list_conj_nb: assumes bnd: "\<forall>p\<in> set ps. bound n p"
  1.1200 +  shows "bound n (list_conj ps)"
  1.1201 +  using bnd
  1.1202 +  unfolding list_conj_def
  1.1203 +  by (induct ps, auto simp add: conj_nb)
  1.1204 +
  1.1205 +lemma list_conj_nb': "\<forall>p\<in>set ps. bound0 p \<Longrightarrow> bound0 (list_conj ps)"
  1.1206 +unfolding list_conj_def by (induct ps , auto)
  1.1207 +
  1.1208 +lemma CJNB_qe: 
  1.1209 +  assumes qe: "\<forall> bs p. qfree p \<longrightarrow> qfree (qe p) \<and> (Ifm vs bs (qe p) = Ifm vs bs (E p))"
  1.1210 +  shows "\<forall> bs p. qfree p \<longrightarrow> qfree (CJNB qe p) \<and> (Ifm vs bs ((CJNB qe p)) = Ifm vs bs (E p))"
  1.1211 +proof(clarify)
  1.1212 +  fix bs p
  1.1213 +  assume qfp: "qfree p"
  1.1214 +  let ?cjs = "conjuncts p"
  1.1215 +  let ?yes = "fst (partition bound0 ?cjs)"
  1.1216 +  let ?no = "snd (partition bound0 ?cjs)"
  1.1217 +  let ?cno = "list_conj ?no"
  1.1218 +  let ?cyes = "list_conj ?yes"
  1.1219 +  have part: "partition bound0 ?cjs = (?yes,?no)" by simp
  1.1220 +  from partition_P[OF part] have "\<forall> q\<in> set ?yes. bound0 q" by blast 
  1.1221 +  hence yes_nb: "bound0 ?cyes" by (simp add: list_conj_nb') 
  1.1222 +  hence yes_qf: "qfree (decr0 ?cyes )" by (simp add: decr0_qf)
  1.1223 +  from conjuncts_qf[OF qfp] partition_set[OF part] 
  1.1224 +  have " \<forall>q\<in> set ?no. qfree q" by auto
  1.1225 +  hence no_qf: "qfree ?cno"by (simp add: list_conj_qf)
  1.1226 +  with qe have cno_qf:"qfree (qe ?cno )" 
  1.1227 +    and noE: "Ifm vs bs (qe ?cno) = Ifm vs bs (E ?cno)" by blast+
  1.1228 +  from cno_qf yes_qf have qf: "qfree (CJNB qe p)" 
  1.1229 +    by (simp add: CJNB_def Let_def conj_qf split_def)
  1.1230 +  {fix bs
  1.1231 +    from conjuncts have "Ifm vs bs p = (\<forall>q\<in> set ?cjs. Ifm vs bs q)" by blast
  1.1232 +    also have "\<dots> = ((\<forall>q\<in> set ?yes. Ifm vs bs q) \<and> (\<forall>q\<in> set ?no. Ifm vs bs q))"
  1.1233 +      using partition_set[OF part] by auto
  1.1234 +    finally have "Ifm vs bs p = ((Ifm vs bs ?cyes) \<and> (Ifm vs bs ?cno))" using list_conj[of vs bs] by simp}
  1.1235 +  hence "Ifm vs bs (E p) = (\<exists>x. (Ifm vs (x#bs) ?cyes) \<and> (Ifm vs (x#bs) ?cno))" by simp
  1.1236 +  also have "\<dots> = (\<exists>x. (Ifm vs (y#bs) ?cyes) \<and> (Ifm vs (x#bs) ?cno))"
  1.1237 +    using bound0_I[OF yes_nb, where bs="bs" and b'="y"] by blast
  1.1238 +  also have "\<dots> = (Ifm vs bs (decr0 ?cyes) \<and> Ifm vs bs (E ?cno))"
  1.1239 +    by (auto simp add: decr0[OF yes_nb])
  1.1240 +  also have "\<dots> = (Ifm vs bs (conj (decr0 ?cyes) (qe ?cno)))"
  1.1241 +    using qe[rule_format, OF no_qf] by auto
  1.1242 +  finally have "Ifm vs bs (E p) = Ifm vs bs (CJNB qe p)" 
  1.1243 +    by (simp add: Let_def CJNB_def split_def)
  1.1244 +  with qf show "qfree (CJNB qe p) \<and> Ifm vs bs (CJNB qe p) = Ifm vs bs (E p)" by blast
  1.1245 +qed
  1.1246 +
  1.1247 +consts simpfm :: "fm \<Rightarrow> fm"
  1.1248 +recdef simpfm "measure fmsize"
  1.1249 +  "simpfm (Lt t) = simplt (simptm t)"
  1.1250 +  "simpfm (Le t) = simple (simptm t)"
  1.1251 +  "simpfm (Eq t) = simpeq(simptm t)"
  1.1252 +  "simpfm (NEq t) = simpneq(simptm t)"
  1.1253 +  "simpfm (And p q) = conj (simpfm p) (simpfm q)"
  1.1254 +  "simpfm (Or p q) = disj (simpfm p) (simpfm q)"
  1.1255 +  "simpfm (Imp p q) = disj (simpfm (NOT p)) (simpfm q)"
  1.1256 +  "simpfm (Iff p q) = disj (conj (simpfm p) (simpfm q)) (conj (simpfm (NOT p)) (simpfm (NOT q)))"
  1.1257 +  "simpfm (NOT (And p q)) = disj (simpfm (NOT p)) (simpfm (NOT q))"
  1.1258 +  "simpfm (NOT (Or p q)) = conj (simpfm (NOT p)) (simpfm (NOT q))"
  1.1259 +  "simpfm (NOT (Imp p q)) = conj (simpfm p) (simpfm (NOT q))"
  1.1260 +  "simpfm (NOT (Iff p q)) = disj (conj (simpfm p) (simpfm (NOT q))) (conj (simpfm (NOT p)) (simpfm q))"
  1.1261 +  "simpfm (NOT (Eq t)) = simpneq t"
  1.1262 +  "simpfm (NOT (NEq t)) = simpeq t"
  1.1263 +  "simpfm (NOT (Le t)) = simplt (Neg t)"
  1.1264 +  "simpfm (NOT (Lt t)) = simple (Neg t)"
  1.1265 +  "simpfm (NOT (NOT p)) = simpfm p"
  1.1266 +  "simpfm (NOT T) = F"
  1.1267 +  "simpfm (NOT F) = T"
  1.1268 +  "simpfm p = p"
  1.1269 +
  1.1270 +lemma simpfm[simp]: "Ifm vs bs (simpfm p) = Ifm vs bs p"
  1.1271 +by(induct p arbitrary: bs rule: simpfm.induct, auto)
  1.1272 +
  1.1273 +lemma simpfm_bound0:   assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
  1.1274 +  shows "bound0 p \<Longrightarrow> bound0 (simpfm p)"
  1.1275 +by (induct p rule: simpfm.induct, auto)
  1.1276 +
  1.1277 +lemma lt_qf[simp]: "qfree (lt t)"
  1.1278 +  apply (cases t, auto simp add: lt_def)
  1.1279 +  by (case_tac poly, auto)
  1.1280 +
  1.1281 +lemma le_qf[simp]: "qfree (le t)"
  1.1282 +  apply (cases t, auto simp add: le_def)
  1.1283 +  by (case_tac poly, auto)
  1.1284 +
  1.1285 +lemma eq_qf[simp]: "qfree (eq t)"
  1.1286 +  apply (cases t, auto simp add: eq_def)
  1.1287 +  by (case_tac poly, auto)
  1.1288 +
  1.1289 +lemma neq_qf[simp]: "qfree (neq t)" by (simp add: neq_def)
  1.1290 +
  1.1291 +lemma simplt_qf[simp]: "qfree (simplt t)" by (simp add: simplt_def Let_def split_def)
  1.1292 +lemma simple_qf[simp]: "qfree (simple t)" by (simp add: simple_def Let_def split_def)
  1.1293 +lemma simpeq_qf[simp]: "qfree (simpeq t)" by (simp add: simpeq_def Let_def split_def)
  1.1294 +lemma simpneq_qf[simp]: "qfree (simpneq t)" by (simp add: simpneq_def Let_def split_def)
  1.1295 +
  1.1296 +lemma simpfm_qf[simp]: "qfree p \<Longrightarrow> qfree (simpfm p)"
  1.1297 +by (induct p rule: simpfm.induct, auto simp add: disj_qf imp_qf iff_qf conj_qf not_qf Let_def)
  1.1298 +
  1.1299 +lemma disj_lin: "islin p \<Longrightarrow> islin q \<Longrightarrow> islin (disj p q)" by (simp add: disj_def)
  1.1300 +lemma conj_lin: "islin p \<Longrightarrow> islin q \<Longrightarrow> islin (conj p q)" by (simp add: conj_def)
  1.1301 +
  1.1302 +lemma   assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
  1.1303 +  shows "qfree p \<Longrightarrow> islin (simpfm p)" 
  1.1304 +  apply (induct p rule: simpfm.induct)
  1.1305 +  apply (simp_all add: conj_lin disj_lin)
  1.1306 +  done
  1.1307 +
  1.1308 +consts prep :: "fm \<Rightarrow> fm"
  1.1309 +recdef prep "measure fmsize"
  1.1310 +  "prep (E T) = T"
  1.1311 +  "prep (E F) = F"
  1.1312 +  "prep (E (Or p q)) = disj (prep (E p)) (prep (E q))"
  1.1313 +  "prep (E (Imp p q)) = disj (prep (E (NOT p))) (prep (E q))"
  1.1314 +  "prep (E (Iff p q)) = disj (prep (E (And p q))) (prep (E (And (NOT p) (NOT q))))" 
  1.1315 +  "prep (E (NOT (And p q))) = disj (prep (E (NOT p))) (prep (E(NOT q)))"
  1.1316 +  "prep (E (NOT (Imp p q))) = prep (E (And p (NOT q)))"
  1.1317 +  "prep (E (NOT (Iff p q))) = disj (prep (E (And p (NOT q)))) (prep (E(And (NOT p) q)))"
  1.1318 +  "prep (E p) = E (prep p)"
  1.1319 +  "prep (A (And p q)) = conj (prep (A p)) (prep (A q))"
  1.1320 +  "prep (A p) = prep (NOT (E (NOT p)))"
  1.1321 +  "prep (NOT (NOT p)) = prep p"
  1.1322 +  "prep (NOT (And p q)) = disj (prep (NOT p)) (prep (NOT q))"
  1.1323 +  "prep (NOT (A p)) = prep (E (NOT p))"
  1.1324 +  "prep (NOT (Or p q)) = conj (prep (NOT p)) (prep (NOT q))"
  1.1325 +  "prep (NOT (Imp p q)) = conj (prep p) (prep (NOT q))"
  1.1326 +  "prep (NOT (Iff p q)) = disj (prep (And p (NOT q))) (prep (And (NOT p) q))"
  1.1327 +  "prep (NOT p) = not (prep p)"
  1.1328 +  "prep (Or p q) = disj (prep p) (prep q)"
  1.1329 +  "prep (And p q) = conj (prep p) (prep q)"
  1.1330 +  "prep (Imp p q) = prep (Or (NOT p) q)"
  1.1331 +  "prep (Iff p q) = disj (prep (And p q)) (prep (And (NOT p) (NOT q)))"
  1.1332 +  "prep p = p"
  1.1333 +(hints simp add: fmsize_pos)
  1.1334 +lemma prep: "Ifm vs bs (prep p) = Ifm vs bs p"
  1.1335 +by (induct p arbitrary: bs rule: prep.induct, auto)
  1.1336 +
  1.1337 +
  1.1338 +
  1.1339 +  (* Generic quantifier elimination *)
  1.1340 +consts qelim :: "fm \<Rightarrow> (fm \<Rightarrow> fm) \<Rightarrow> fm"
  1.1341 +recdef qelim "measure fmsize"
  1.1342 +  "qelim (E p) = (\<lambda> qe. DJ (CJNB qe) (qelim p qe))"
  1.1343 +  "qelim (A p) = (\<lambda> qe. not (qe ((qelim (NOT p) qe))))"
  1.1344 +  "qelim (NOT p) = (\<lambda> qe. not (qelim p qe))"
  1.1345 +  "qelim (And p q) = (\<lambda> qe. conj (qelim p qe) (qelim q qe))" 
  1.1346 +  "qelim (Or  p q) = (\<lambda> qe. disj (qelim p qe) (qelim q qe))" 
  1.1347 +  "qelim (Imp p q) = (\<lambda> qe. imp (qelim p qe) (qelim q qe))"
  1.1348 +  "qelim (Iff p q) = (\<lambda> qe. iff (qelim p qe) (qelim q qe))"
  1.1349 +  "qelim p = (\<lambda> y. simpfm p)"
  1.1350 +
  1.1351 +
  1.1352 +lemma qelim:
  1.1353 +  assumes qe_inv: "\<forall> bs p. qfree p \<longrightarrow> qfree (qe p) \<and> (Ifm vs bs (qe p) = Ifm vs bs (E p))"
  1.1354 +  shows "\<And> bs. qfree (qelim p qe) \<and> (Ifm vs bs (qelim p qe) = Ifm vs bs p)"
  1.1355 +using qe_inv DJ_qe[OF CJNB_qe[OF qe_inv]]
  1.1356 +by (induct p rule: qelim.induct) auto
  1.1357 +
  1.1358 +subsection{* Core Procedure *}
  1.1359 +
  1.1360 +consts 
  1.1361 +  plusinf:: "fm \<Rightarrow> fm" (* Virtual substitution of +\<infinity>*)
  1.1362 +  minusinf:: "fm \<Rightarrow> fm" (* Virtual substitution of -\<infinity>*)
  1.1363 +recdef minusinf "measure size"
  1.1364 +  "minusinf (And p q) = conj (minusinf p) (minusinf q)" 
  1.1365 +  "minusinf (Or p q) = disj (minusinf p) (minusinf q)" 
  1.1366 +  "minusinf (Eq  (CNP 0 c e)) = conj (eq (CP c)) (eq e)"
  1.1367 +  "minusinf (NEq (CNP 0 c e)) = disj (not (eq e)) (not (eq (CP c)))"
  1.1368 +  "minusinf (Lt  (CNP 0 c e)) = disj (conj (eq (CP c)) (lt e)) (lt (CP (~\<^sub>p c)))"
  1.1369 +  "minusinf (Le  (CNP 0 c e)) = disj (conj (eq (CP c)) (le e)) (lt (CP (~\<^sub>p c)))"
  1.1370 +  "minusinf p = p"
  1.1371 +
  1.1372 +recdef plusinf "measure size"
  1.1373 +  "plusinf (And p q) = conj (plusinf p) (plusinf q)" 
  1.1374 +  "plusinf (Or p q) = disj (plusinf p) (plusinf q)" 
  1.1375 +  "plusinf (Eq  (CNP 0 c e)) = conj (eq (CP c)) (eq e)"
  1.1376 +  "plusinf (NEq (CNP 0 c e)) = disj (not (eq e)) (not (eq (CP c)))"
  1.1377 +  "plusinf (Lt  (CNP 0 c e)) = disj (conj (eq (CP c)) (lt e)) (lt (CP c))"
  1.1378 +  "plusinf (Le  (CNP 0 c e)) = disj (conj (eq (CP c)) (le e)) (lt (CP c))"
  1.1379 +  "plusinf p = p"
  1.1380 +
  1.1381 +lemma minusinf_inf: assumes lp:"islin p"
  1.1382 +  shows "\<exists>z. \<forall>x < z. Ifm vs (x#bs) (minusinf p) \<longleftrightarrow> Ifm vs (x#bs) p"
  1.1383 +  using lp
  1.1384 +proof (induct p rule: minusinf.induct)
  1.1385 +  case 1 thus ?case by (auto,rule_tac x="min z za" in exI, auto)
  1.1386 +next
  1.1387 +  case 2 thus ?case by (auto,rule_tac x="min z za" in exI, auto)
  1.1388 +next
  1.1389 +  case (3 c e) hence nbe: "tmbound0 e" by simp
  1.1390 +  from prems have nc: "allpolys isnpoly (CP c)" "allpolys isnpoly e" by simp_all
  1.1391 +  note eqs = eq[OF nc(1), where ?'a = 'a] eq[OF nc(2), where ?'a = 'a]
  1.1392 +  let ?c = "Ipoly vs c"
  1.1393 +  let ?e = "Itm vs (y#bs) e"
  1.1394 +  have "?c=0 \<or> ?c > 0 \<or> ?c < 0" by arith
  1.1395 +  moreover {assume "?c = 0" hence ?case 
  1.1396 +      using eq[OF nc(2), of vs] eq[OF nc(1), of vs] by auto}
  1.1397 +  moreover {assume cp: "?c > 0"
  1.1398 +    {fix x assume xz: "x < -?e / ?c" hence "?c * x < - ?e"
  1.1399 +	using pos_less_divide_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
  1.1400 +      hence "?c * x + ?e < 0" by simp
  1.1401 +      hence "Ifm vs (x#bs) (Eq (CNP 0 c e)) = Ifm vs (x#bs) (minusinf (Eq (CNP 0 c e)))"
  1.1402 +	using eqs tmbound0_I[OF nbe, where b="y" and b'="x" and vs=vs and bs=bs] by auto} hence ?case by auto}
  1.1403 +  moreover {assume cp: "?c < 0"
  1.1404 +    {fix x assume xz: "x < -?e / ?c" hence "?c * x > - ?e"
  1.1405 +	using neg_less_divide_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
  1.1406 +      hence "?c * x + ?e > 0" by simp
  1.1407 +      hence "Ifm vs (x#bs) (Eq (CNP 0 c e)) = Ifm vs (x#bs) (minusinf (Eq (CNP 0 c e)))"
  1.1408 +	using tmbound0_I[OF nbe, where b="y" and b'="x"] eqs by auto} hence ?case by auto}
  1.1409 +  ultimately show ?case by blast
  1.1410 +next
  1.1411 +  case (4 c e)  hence nbe: "tmbound0 e" by simp
  1.1412 +  from prems have nc: "allpolys isnpoly (CP c)" "allpolys isnpoly e" by simp_all
  1.1413 +  note eqs = eq[OF nc(1), where ?'a = 'a] eq[OF nc(2), where ?'a = 'a]
  1.1414 +  let ?c = "Ipoly vs c"
  1.1415 +  let ?e = "Itm vs (y#bs) e"
  1.1416 +  have "?c=0 \<or> ?c > 0 \<or> ?c < 0" by arith
  1.1417 +  moreover {assume "?c = 0" hence ?case using eqs by auto}
  1.1418 +  moreover {assume cp: "?c > 0"
  1.1419 +    {fix x assume xz: "x < -?e / ?c" hence "?c * x < - ?e"
  1.1420 +	using pos_less_divide_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
  1.1421 +      hence "?c * x + ?e < 0" by simp
  1.1422 +      hence "Ifm vs (x#bs) (NEq (CNP 0 c e)) = Ifm vs (x#bs) (minusinf (NEq (CNP 0 c e)))"
  1.1423 +	using eqs tmbound0_I[OF nbe, where b="y" and b'="x"] by auto} hence ?case by auto}
  1.1424 +  moreover {assume cp: "?c < 0"
  1.1425 +    {fix x assume xz: "x < -?e / ?c" hence "?c * x > - ?e"
  1.1426 +	using neg_less_divide_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
  1.1427 +      hence "?c * x + ?e > 0" by simp
  1.1428 +      hence "Ifm vs (x#bs) (NEq (CNP 0 c e)) = Ifm vs (x#bs) (minusinf (NEq (CNP 0 c e)))"
  1.1429 +	using eqs tmbound0_I[OF nbe, where b="y" and b'="x"] by auto} hence ?case by auto}
  1.1430 +  ultimately show ?case by blast
  1.1431 +next
  1.1432 +  case (5 c e)  hence nbe: "tmbound0 e" by simp
  1.1433 +  from prems have nc: "allpolys isnpoly (CP c)" "allpolys isnpoly e" by simp_all
  1.1434 +  hence nc': "allpolys isnpoly (CP (~\<^sub>p c))" by (simp add: polyneg_norm)
  1.1435 +  note eqs = lt[OF nc', where ?'a = 'a] eq [OF nc(1), where ?'a = 'a] lt[OF nc(2), where ?'a = 'a]
  1.1436 +  let ?c = "Ipoly vs c"
  1.1437 +  let ?e = "Itm vs (y#bs) e"
  1.1438 +  have "?c=0 \<or> ?c > 0 \<or> ?c < 0" by arith
  1.1439 +  moreover {assume "?c = 0" hence ?case using eqs by auto}
  1.1440 +  moreover {assume cp: "?c > 0"
  1.1441 +    {fix x assume xz: "x < -?e / ?c" hence "?c * x < - ?e"
  1.1442 +	using pos_less_divide_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
  1.1443 +      hence "?c * x + ?e < 0" by simp
  1.1444 +      hence "Ifm vs (x#bs) (Lt (CNP 0 c e)) = Ifm vs (x#bs) (minusinf (Lt (CNP 0 c e)))"
  1.1445 +	using tmbound0_I[OF nbe, where b="y" and b'="x"] cp eqs by auto} hence ?case by auto}
  1.1446 +  moreover {assume cp: "?c < 0"
  1.1447 +    {fix x assume xz: "x < -?e / ?c" hence "?c * x > - ?e"
  1.1448 +	using neg_less_divide_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
  1.1449 +      hence "?c * x + ?e > 0" by simp
  1.1450 +      hence "Ifm vs (x#bs) (Lt (CNP 0 c e)) = Ifm vs (x#bs) (minusinf (Lt (CNP 0 c e)))"
  1.1451 +	using eqs tmbound0_I[OF nbe, where b="y" and b'="x"] cp by auto} hence ?case by auto}
  1.1452 +  ultimately show ?case by blast
  1.1453 +next
  1.1454 +  case (6 c e)  hence nbe: "tmbound0 e" by simp
  1.1455 +  from prems have nc: "allpolys isnpoly (CP c)" "allpolys isnpoly e" by simp_all
  1.1456 +  hence nc': "allpolys isnpoly (CP (~\<^sub>p c))" by (simp add: polyneg_norm)
  1.1457 +  note eqs = lt[OF nc', where ?'a = 'a] eq [OF nc(1), where ?'a = 'a] le[OF nc(2), where ?'a = 'a]
  1.1458 +  let ?c = "Ipoly vs c"
  1.1459 +  let ?e = "Itm vs (y#bs) e"
  1.1460 +  have "?c=0 \<or> ?c > 0 \<or> ?c < 0" by arith
  1.1461 +  moreover {assume "?c = 0" hence ?case using eqs by auto}
  1.1462 +  moreover {assume cp: "?c > 0"
  1.1463 +    {fix x assume xz: "x < -?e / ?c" hence "?c * x < - ?e"
  1.1464 +	using pos_less_divide_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
  1.1465 +      hence "?c * x + ?e < 0" by simp
  1.1466 +      hence "Ifm vs (x#bs) (Le (CNP 0 c e)) = Ifm vs (x#bs) (minusinf (Le (CNP 0 c e)))"
  1.1467 +	using tmbound0_I[OF nbe, where b="y" and b'="x"] cp eqs by auto} hence ?case by auto}
  1.1468 +  moreover {assume cp: "?c < 0"
  1.1469 +    {fix x assume xz: "x < -?e / ?c" hence "?c * x > - ?e"
  1.1470 +	using neg_less_divide_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
  1.1471 +      hence "?c * x + ?e > 0" by simp
  1.1472 +      hence "Ifm vs (x#bs) (Le (CNP 0 c e)) = Ifm vs (x#bs) (minusinf (Le (CNP 0 c e)))"
  1.1473 +	using tmbound0_I[OF nbe, where b="y" and b'="x"] cp eqs by auto} hence ?case by auto}
  1.1474 +  ultimately show ?case by blast
  1.1475 +qed (auto)
  1.1476 +
  1.1477 +lemma plusinf_inf: assumes lp:"islin p"
  1.1478 +  shows "\<exists>z. \<forall>x > z. Ifm vs (x#bs) (plusinf p) \<longleftrightarrow> Ifm vs (x#bs) p"
  1.1479 +  using lp
  1.1480 +proof (induct p rule: plusinf.induct)
  1.1481 +  case 1 thus ?case by (auto,rule_tac x="max z za" in exI, auto)
  1.1482 +next
  1.1483 +  case 2 thus ?case by (auto,rule_tac x="max z za" in exI, auto)
  1.1484 +next
  1.1485 +  case (3 c e) hence nbe: "tmbound0 e" by simp
  1.1486 +  from prems have nc: "allpolys isnpoly (CP c)" "allpolys isnpoly e" by simp_all
  1.1487 +  note eqs = eq[OF nc(1), where ?'a = 'a] eq[OF nc(2), where ?'a = 'a]
  1.1488 +  let ?c = "Ipoly vs c"
  1.1489 +  let ?e = "Itm vs (y#bs) e"
  1.1490 +  have "?c=0 \<or> ?c > 0 \<or> ?c < 0" by arith
  1.1491 +  moreover {assume "?c = 0" hence ?case 
  1.1492 +      using eq[OF nc(2), of vs] eq[OF nc(1), of vs] by auto}
  1.1493 +  moreover {assume cp: "?c > 0"
  1.1494 +    {fix x assume xz: "x > -?e / ?c" hence "?c * x > - ?e" 
  1.1495 +	using pos_divide_less_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
  1.1496 +      hence "?c * x + ?e > 0" by simp
  1.1497 +      hence "Ifm vs (x#bs) (Eq (CNP 0 c e)) = Ifm vs (x#bs) (plusinf (Eq (CNP 0 c e)))"
  1.1498 +	using eqs tmbound0_I[OF nbe, where b="y" and b'="x" and vs=vs and bs=bs] by auto} hence ?case by auto}
  1.1499 +  moreover {assume cp: "?c < 0"
  1.1500 +    {fix x assume xz: "x > -?e / ?c" hence "?c * x < - ?e"
  1.1501 +	using neg_divide_less_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
  1.1502 +      hence "?c * x + ?e < 0" by simp
  1.1503 +      hence "Ifm vs (x#bs) (Eq (CNP 0 c e)) = Ifm vs (x#bs) (plusinf (Eq (CNP 0 c e)))"
  1.1504 +	using tmbound0_I[OF nbe, where b="y" and b'="x"] eqs by auto} hence ?case by auto}
  1.1505 +  ultimately show ?case by blast
  1.1506 +next
  1.1507 +  case (4 c e)  hence nbe: "tmbound0 e" by simp
  1.1508 +  from prems have nc: "allpolys isnpoly (CP c)" "allpolys isnpoly e" by simp_all
  1.1509 +  note eqs = eq[OF nc(1), where ?'a = 'a] eq[OF nc(2), where ?'a = 'a]
  1.1510 +  let ?c = "Ipoly vs c"
  1.1511 +  let ?e = "Itm vs (y#bs) e"
  1.1512 +  have "?c=0 \<or> ?c > 0 \<or> ?c < 0" by arith
  1.1513 +  moreover {assume "?c = 0" hence ?case using eqs by auto}
  1.1514 +  moreover {assume cp: "?c > 0"
  1.1515 +    {fix x assume xz: "x > -?e / ?c" hence "?c * x > - ?e"
  1.1516 +	using pos_divide_less_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
  1.1517 +      hence "?c * x + ?e > 0" by simp
  1.1518 +      hence "Ifm vs (x#bs) (NEq (CNP 0 c e)) = Ifm vs (x#bs) (plusinf (NEq (CNP 0 c e)))"
  1.1519 +	using eqs tmbound0_I[OF nbe, where b="y" and b'="x"] by auto} hence ?case by auto}
  1.1520 +  moreover {assume cp: "?c < 0"
  1.1521 +    {fix x assume xz: "x > -?e / ?c" hence "?c * x < - ?e"
  1.1522 +	using neg_divide_less_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
  1.1523 +      hence "?c * x + ?e < 0" by simp
  1.1524 +      hence "Ifm vs (x#bs) (NEq (CNP 0 c e)) = Ifm vs (x#bs) (plusinf (NEq (CNP 0 c e)))"
  1.1525 +	using eqs tmbound0_I[OF nbe, where b="y" and b'="x"] by auto} hence ?case by auto}
  1.1526 +  ultimately show ?case by blast
  1.1527 +next
  1.1528 +  case (5 c e)  hence nbe: "tmbound0 e" by simp
  1.1529 +  from prems have nc: "allpolys isnpoly (CP c)" "allpolys isnpoly e" by simp_all
  1.1530 +  hence nc': "allpolys isnpoly (CP (~\<^sub>p c))" by (simp add: polyneg_norm)
  1.1531 +  note eqs = lt[OF nc(1), where ?'a = 'a] lt[OF nc', where ?'a = 'a] eq [OF nc(1), where ?'a = 'a] lt[OF nc(2), where ?'a = 'a]
  1.1532 +  let ?c = "Ipoly vs c"
  1.1533 +  let ?e = "Itm vs (y#bs) e"
  1.1534 +  have "?c=0 \<or> ?c > 0 \<or> ?c < 0" by arith
  1.1535 +  moreover {assume "?c = 0" hence ?case using eqs by auto}
  1.1536 +  moreover {assume cp: "?c > 0"
  1.1537 +    {fix x assume xz: "x > -?e / ?c" hence "?c * x > - ?e"
  1.1538 +	using pos_divide_less_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
  1.1539 +      hence "?c * x + ?e > 0" by simp
  1.1540 +      hence "Ifm vs (x#bs) (Lt (CNP 0 c e)) = Ifm vs (x#bs) (plusinf (Lt (CNP 0 c e)))"
  1.1541 +	using tmbound0_I[OF nbe, where b="y" and b'="x"] cp eqs by auto} hence ?case by auto}
  1.1542 +  moreover {assume cp: "?c < 0"
  1.1543 +    {fix x assume xz: "x > -?e / ?c" hence "?c * x < - ?e"
  1.1544 +	using neg_divide_less_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
  1.1545 +      hence "?c * x + ?e < 0" by simp
  1.1546 +      hence "Ifm vs (x#bs) (Lt (CNP 0 c e)) = Ifm vs (x#bs) (plusinf (Lt (CNP 0 c e)))"
  1.1547 +	using eqs tmbound0_I[OF nbe, where b="y" and b'="x"] cp by auto} hence ?case by auto}
  1.1548 +  ultimately show ?case by blast
  1.1549 +next
  1.1550 +  case (6 c e)  hence nbe: "tmbound0 e" by simp
  1.1551 +  from prems have nc: "allpolys isnpoly (CP c)" "allpolys isnpoly e" by simp_all
  1.1552 +  hence nc': "allpolys isnpoly (CP (~\<^sub>p c))" by (simp add: polyneg_norm)
  1.1553 +  note eqs = lt[OF nc(1), where ?'a = 'a] eq [OF nc(1), where ?'a = 'a] le[OF nc(2), where ?'a = 'a]
  1.1554 +  let ?c = "Ipoly vs c"
  1.1555 +  let ?e = "Itm vs (y#bs) e"
  1.1556 +  have "?c=0 \<or> ?c > 0 \<or> ?c < 0" by arith
  1.1557 +  moreover {assume "?c = 0" hence ?case using eqs by auto}
  1.1558 +  moreover {assume cp: "?c > 0"
  1.1559 +    {fix x assume xz: "x > -?e / ?c" hence "?c * x > - ?e"
  1.1560 +	using pos_divide_less_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
  1.1561 +      hence "?c * x + ?e > 0" by simp
  1.1562 +      hence "Ifm vs (x#bs) (Le (CNP 0 c e)) = Ifm vs (x#bs) (plusinf (Le (CNP 0 c e)))"
  1.1563 +	using tmbound0_I[OF nbe, where b="y" and b'="x"] cp eqs by auto} hence ?case by auto}
  1.1564 +  moreover {assume cp: "?c < 0"
  1.1565 +    {fix x assume xz: "x > -?e / ?c" hence "?c * x < - ?e"
  1.1566 +	using neg_divide_less_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
  1.1567 +      hence "?c * x + ?e < 0" by simp
  1.1568 +      hence "Ifm vs (x#bs) (Le (CNP 0 c e)) = Ifm vs (x#bs) (plusinf (Le (CNP 0 c e)))"
  1.1569 +	using tmbound0_I[OF nbe, where b="y" and b'="x"] cp eqs by auto} hence ?case by auto}
  1.1570 +  ultimately show ?case by blast
  1.1571 +qed (auto)
  1.1572 +
  1.1573 +lemma minusinf_nb: "islin p \<Longrightarrow> bound0 (minusinf p)" 
  1.1574 +  by (induct p rule: minusinf.induct, auto simp add: eq_nb lt_nb le_nb)
  1.1575 +lemma plusinf_nb: "islin p \<Longrightarrow> bound0 (plusinf p)" 
  1.1576 +  by (induct p rule: minusinf.induct, auto simp add: eq_nb lt_nb le_nb)
  1.1577 +
  1.1578 +lemma minusinf_ex: assumes lp: "islin p" and ex: "Ifm vs (x#bs) (minusinf p)"
  1.1579 +  shows "\<exists>x. Ifm vs (x#bs) p"
  1.1580 +proof-
  1.1581 +  from bound0_I [OF minusinf_nb[OF lp], where b="a" and bs ="bs"] ex
  1.1582 +  have th: "\<forall> x. Ifm vs (x#bs) (minusinf p)" by auto
  1.1583 +  from minusinf_inf[OF lp, where bs="bs"] 
  1.1584 +  obtain z where z_def: "\<forall>x<z. Ifm vs (x # bs) (minusinf p) = Ifm vs (x # bs) p" by blast
  1.1585 +  from th have "Ifm vs ((z - 1)#bs) (minusinf p)" by simp
  1.1586 +  moreover have "z - 1 < z" by simp
  1.1587 +  ultimately show ?thesis using z_def by auto
  1.1588 +qed
  1.1589 +
  1.1590 +lemma plusinf_ex: assumes lp: "islin p" and ex: "Ifm vs (x#bs) (plusinf p)"
  1.1591 +  shows "\<exists>x. Ifm vs (x#bs) p"
  1.1592 +proof-
  1.1593 +  from bound0_I [OF plusinf_nb[OF lp], where b="a" and bs ="bs"] ex
  1.1594 +  have th: "\<forall> x. Ifm vs (x#bs) (plusinf p)" by auto
  1.1595 +  from plusinf_inf[OF lp, where bs="bs"] 
  1.1596 +  obtain z where z_def: "\<forall>x>z. Ifm vs (x # bs) (plusinf p) = Ifm vs (x # bs) p" by blast
  1.1597 +  from th have "Ifm vs ((z + 1)#bs) (plusinf p)" by simp
  1.1598 +  moreover have "z + 1 > z" by simp
  1.1599 +  ultimately show ?thesis using z_def by auto
  1.1600 +qed
  1.1601 +
  1.1602 +fun uset :: "fm \<Rightarrow> (poly \<times> tm) list" where
  1.1603 +  "uset (And p q) = uset p @ uset q"
  1.1604 +| "uset (Or p q) = uset p @ uset q"
  1.1605 +| "uset (Eq (CNP 0 a e))  = [(a,e)]"
  1.1606 +| "uset (Le (CNP 0 a e))  = [(a,e)]"
  1.1607 +| "uset (Lt (CNP 0 a e))  = [(a,e)]"
  1.1608 +| "uset (NEq (CNP 0 a e)) = [(a,e)]"
  1.1609 +| "uset p = []"
  1.1610 +
  1.1611 +lemma uset_l:
  1.1612 +  assumes lp: "islin p"
  1.1613 +  shows "\<forall> (c,s) \<in> set (uset p). isnpoly c \<and> c \<noteq> 0\<^sub>p \<and> tmbound0 s \<and> allpolys isnpoly s"
  1.1614 +using lp by(induct p rule: uset.induct,auto)
  1.1615 +
  1.1616 +lemma minusinf_uset0:
  1.1617 +  assumes lp: "islin p"
  1.1618 +  and nmi: "\<not> (Ifm vs (x#bs) (minusinf p))"
  1.1619 +  and ex: "Ifm vs (x#bs) p" (is "?I x p")
  1.1620 +  shows "\<exists> (c,s) \<in> set (uset p). x \<ge> - Itm vs (x#bs) s / Ipoly vs c" 
  1.1621 +proof-
  1.1622 +  have "\<exists> (c,s) \<in> set (uset p). (Ipoly vs c < 0 \<and> Ipoly vs c * x \<le> - Itm vs (x#bs) s) \<or>  (Ipoly vs c > 0 \<and> Ipoly vs c * x \<ge> - Itm vs (x#bs) s)" 
  1.1623 +    using lp nmi ex
  1.1624 +    apply (induct p rule: minusinf.induct, auto simp add: eq le lt nth_pos2 polyneg_norm)
  1.1625 +    apply (auto simp add: linorder_not_less order_le_less)
  1.1626 +    done 
  1.1627 +  then obtain c s where csU: "(c,s) \<in> set (uset p)" and x: "(Ipoly vs c < 0 \<and> Ipoly vs c * x \<le> - Itm vs (x#bs) s) \<or>  (Ipoly vs c > 0 \<and> Ipoly vs c * x \<ge> - Itm vs (x#bs) s)" by blast
  1.1628 +  hence "x \<ge> (- Itm vs (x#bs) s) / Ipoly vs c"
  1.1629 +    using divide_le_eq[of "- Itm vs (x#bs) s" "Ipoly vs c" x]
  1.1630 +    by (auto simp add: mult_commute del: divide_minus_left)
  1.1631 +  thus ?thesis using csU by auto
  1.1632 +qed
  1.1633 +
  1.1634 +lemma minusinf_uset:
  1.1635 +  assumes lp: "islin p"
  1.1636 +  and nmi: "\<not> (Ifm vs (a#bs) (minusinf p))"
  1.1637 +  and ex: "Ifm vs (x#bs) p" (is "?I x p")
  1.1638 +  shows "\<exists> (c,s) \<in> set (uset p). x \<ge> - Itm vs (a#bs) s / Ipoly vs c" 
  1.1639 +proof-
  1.1640 +  from nmi have nmi': "\<not> (Ifm vs (x#bs) (minusinf p))" 
  1.1641 +    by (simp add: bound0_I[OF minusinf_nb[OF lp], where b=x and b'=a])
  1.1642 +  from minusinf_uset0[OF lp nmi' ex] 
  1.1643 +  obtain c s where csU: "(c,s) \<in> set (uset p)" and th: "x \<ge> - Itm vs (x#bs) s / Ipoly vs c" by blast
  1.1644 +  from uset_l[OF lp, rule_format, OF csU] have nb: "tmbound0 s" by simp
  1.1645 +  from th tmbound0_I[OF nb, of vs x bs a] csU show ?thesis by auto
  1.1646 +qed
  1.1647 +
  1.1648 +
  1.1649 +lemma plusinf_uset0:
  1.1650 +  assumes lp: "islin p"
  1.1651 +  and nmi: "\<not> (Ifm vs (x#bs) (plusinf p))"
  1.1652 +  and ex: "Ifm vs (x#bs) p" (is "?I x p")
  1.1653 +  shows "\<exists> (c,s) \<in> set (uset p). x \<le> - Itm vs (x#bs) s / Ipoly vs c" 
  1.1654 +proof-
  1.1655 +  have "\<exists> (c,s) \<in> set (uset p). (Ipoly vs c < 0 \<and> Ipoly vs c * x \<ge> - Itm vs (x#bs) s) \<or>  (Ipoly vs c > 0 \<and> Ipoly vs c * x \<le> - Itm vs (x#bs) s)" 
  1.1656 +    using lp nmi ex
  1.1657 +    apply (induct p rule: minusinf.induct, auto simp add: eq le lt nth_pos2 polyneg_norm)
  1.1658 +    apply (auto simp add: linorder_not_less order_le_less)
  1.1659 +    done 
  1.1660 +  then obtain c s where csU: "(c,s) \<in> set (uset p)" and x: "(Ipoly vs c < 0 \<and> Ipoly vs c * x \<ge> - Itm vs (x#bs) s) \<or>  (Ipoly vs c > 0 \<and> Ipoly vs c * x \<le> - Itm vs (x#bs) s)" by blast
  1.1661 +  hence "x \<le> (- Itm vs (x#bs) s) / Ipoly vs c"
  1.1662 +    using le_divide_eq[of x "- Itm vs (x#bs) s" "Ipoly vs c"]
  1.1663 +    by (auto simp add: mult_commute del: divide_minus_left)
  1.1664 +  thus ?thesis using csU by auto
  1.1665 +qed
  1.1666 +
  1.1667 +lemma plusinf_uset:
  1.1668 +  assumes lp: "islin p"
  1.1669 +  and nmi: "\<not> (Ifm vs (a#bs) (plusinf p))"
  1.1670 +  and ex: "Ifm vs (x#bs) p" (is "?I x p")
  1.1671 +  shows "\<exists> (c,s) \<in> set (uset p). x \<le> - Itm vs (a#bs) s / Ipoly vs c" 
  1.1672 +proof-
  1.1673 +  from nmi have nmi': "\<not> (Ifm vs (x#bs) (plusinf p))" 
  1.1674 +    by (simp add: bound0_I[OF plusinf_nb[OF lp], where b=x and b'=a])
  1.1675 +  from plusinf_uset0[OF lp nmi' ex] 
  1.1676 +  obtain c s where csU: "(c,s) \<in> set (uset p)" and th: "x \<le> - Itm vs (x#bs) s / Ipoly vs c" by blast
  1.1677 +  from uset_l[OF lp, rule_format, OF csU] have nb: "tmbound0 s" by simp
  1.1678 +  from th tmbound0_I[OF nb, of vs x bs a] csU show ?thesis by auto
  1.1679 +qed
  1.1680 +
  1.1681 +lemma lin_dense: 
  1.1682 +  assumes lp: "islin p"
  1.1683 +  and noS: "\<forall> t. l < t \<and> t< u \<longrightarrow> t \<notin> (\<lambda> (c,t). - Itm vs (x#bs) t / Ipoly vs c) ` set (uset p)" 
  1.1684 +  (is "\<forall> t. _ \<and> _ \<longrightarrow> t \<notin> (\<lambda> (c,t). - ?Nt x t / ?N c) ` ?U p")
  1.1685 +  and lx: "l < x" and xu:"x < u" and px:" Ifm vs (x#bs) p"
  1.1686 +  and ly: "l < y" and yu: "y < u"
  1.1687 +  shows "Ifm vs (y#bs) p"
  1.1688 +using lp px noS
  1.1689 +proof (induct p rule: islin.induct) 
  1.1690 +  case (5 c s)
  1.1691 +  from "5.prems" 
  1.1692 +  have lin: "isnpoly c" "c \<noteq> 0\<^sub>p" "tmbound0 s" "allpolys isnpoly s"
  1.1693 +    and px: "Ifm vs (x # bs) (Lt (CNP 0 c s))"
  1.1694 +    and noS: "\<forall>t. l < t \<and> t < u \<longrightarrow> t \<noteq> - Itm vs (x # bs) s / \<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup>" by simp_all
  1.1695 +  from ly yu noS have yne: "y \<noteq> - ?Nt x s / \<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup>" by simp
  1.1696 +  hence ycs: "y < - ?Nt x s / ?N c \<or> y > -?Nt x s / ?N c" by auto
  1.1697 +  have ccs: "?N c = 0 \<or> ?N c < 0 \<or> ?N c > 0" by dlo
  1.1698 +  moreover
  1.1699 +  {assume "?N c = 0" hence ?case using px by (simp add: tmbound0_I[OF lin(3), where bs="bs" and b="x" and b'="y"])}
  1.1700 +  moreover
  1.1701 +  {assume c: "?N c > 0"
  1.1702 +      from px pos_less_divide_eq[OF c, where a="x" and b="-?Nt x s"]  
  1.1703 +      have px': "x < - ?Nt x s / ?N c" 
  1.1704 +	by (auto simp add: not_less ring_simps) 
  1.1705 +    {assume y: "y < - ?Nt x s / ?N c" 
  1.1706 +      hence "y * ?N c < - ?Nt x s"
  1.1707 +	by (simp add: pos_less_divide_eq[OF c, where a="y" and b="-?Nt x s", symmetric])
  1.1708 +      hence "?N c * y + ?Nt x s < 0" by (simp add: ring_simps)
  1.1709 +      hence ?case using tmbound0_I[OF lin(3), where bs="bs" and b="x" and b'="y"] by simp}
  1.1710 +    moreover
  1.1711 +    {assume y: "y > -?Nt x s / ?N c" 
  1.1712 +      with yu have eu: "u > - ?Nt x s / ?N c" by auto
  1.1713 +      with noS ly yu have th: "- ?Nt x s / ?N c \<le> l" by (cases "- ?Nt x s / ?N c > l", auto)
  1.1714 +      with lx px' have "False" by simp  hence ?case by simp }
  1.1715 +    ultimately have ?case using ycs by blast
  1.1716 +  }
  1.1717 +  moreover
  1.1718 +  {assume c: "?N c < 0"
  1.1719 +      from px neg_divide_less_eq[OF c, where a="x" and b="-?Nt x s"]  
  1.1720 +      have px': "x > - ?Nt x s / ?N c" 
  1.1721 +	by (auto simp add: not_less ring_simps) 
  1.1722 +    {assume y: "y > - ?Nt x s / ?N c" 
  1.1723 +      hence "y * ?N c < - ?Nt x s"
  1.1724 +	by (simp add: neg_divide_less_eq[OF c, where a="y" and b="-?Nt x s", symmetric])
  1.1725 +      hence "?N c * y + ?Nt x s < 0" by (simp add: ring_simps)
  1.1726 +      hence ?case using tmbound0_I[OF lin(3), where bs="bs" and b="x" and b'="y"] by simp}
  1.1727 +    moreover
  1.1728 +    {assume y: "y < -?Nt x s / ?N c" 
  1.1729 +      with ly have eu: "l < - ?Nt x s / ?N c" by auto
  1.1730 +      with noS ly yu have th: "- ?Nt x s / ?N c \<ge> u" by (cases "- ?Nt x s / ?N c < u", auto)
  1.1731 +      with xu px' have "False" by simp  hence ?case by simp }
  1.1732 +    ultimately have ?case using ycs by blast
  1.1733 +  }
  1.1734 +  ultimately show ?case by blast
  1.1735 +next
  1.1736 +  case (6 c s)
  1.1737 +  from "6.prems" 
  1.1738 +  have lin: "isnpoly c" "c \<noteq> 0\<^sub>p" "tmbound0 s" "allpolys isnpoly s"
  1.1739 +    and px: "Ifm vs (x # bs) (Le (CNP 0 c s))"
  1.1740 +    and noS: "\<forall>t. l < t \<and> t < u \<longrightarrow> t \<noteq> - Itm vs (x # bs) s / \<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup>" by simp_all
  1.1741 +  from ly yu noS have yne: "y \<noteq> - ?Nt x s / \<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup>" by simp
  1.1742 +  hence ycs: "y < - ?Nt x s / ?N c \<or> y > -?Nt x s / ?N c" by auto
  1.1743 +  have ccs: "?N c = 0 \<or> ?N c < 0 \<or> ?N c > 0" by dlo
  1.1744 +  moreover
  1.1745 +  {assume "?N c = 0" hence ?case using px by (simp add: tmbound0_I[OF lin(3), where bs="bs" and b="x" and b'="y"])}
  1.1746 +  moreover
  1.1747 +  {assume c: "?N c > 0"
  1.1748 +      from px pos_le_divide_eq[OF c, where a="x" and b="-?Nt x s"]  
  1.1749 +      have px': "x <= - ?Nt x s / ?N c" by (simp add: not_less ring_simps) 
  1.1750 +    {assume y: "y < - ?Nt x s / ?N c" 
  1.1751 +      hence "y * ?N c < - ?Nt x s"
  1.1752 +	by (simp add: pos_less_divide_eq[OF c, where a="y" and b="-?Nt x s", symmetric])
  1.1753 +      hence "?N c * y + ?Nt x s < 0" by (simp add: ring_simps)
  1.1754 +      hence ?case using tmbound0_I[OF lin(3), where bs="bs" and b="x" and b'="y"] by simp}
  1.1755 +    moreover
  1.1756 +    {assume y: "y > -?Nt x s / ?N c" 
  1.1757 +      with yu have eu: "u > - ?Nt x s / ?N c" by auto
  1.1758 +      with noS ly yu have th: "- ?Nt x s / ?N c \<le> l" by (cases "- ?Nt x s / ?N c > l", auto)
  1.1759 +      with lx px' have "False" by simp  hence ?case by simp }
  1.1760 +    ultimately have ?case using ycs by blast
  1.1761 +  }
  1.1762 +  moreover
  1.1763 +  {assume c: "?N c < 0"
  1.1764 +      from px neg_divide_le_eq[OF c, where a="x" and b="-?Nt x s"]  
  1.1765 +      have px': "x >= - ?Nt x s / ?N c" by (simp add: ring_simps) 
  1.1766 +    {assume y: "y > - ?Nt x s / ?N c" 
  1.1767 +      hence "y * ?N c < - ?Nt x s"
  1.1768 +	by (simp add: neg_divide_less_eq[OF c, where a="y" and b="-?Nt x s", symmetric])
  1.1769 +      hence "?N c * y + ?Nt x s < 0" by (simp add: ring_simps)
  1.1770 +      hence ?case using tmbound0_I[OF lin(3), where bs="bs" and b="x" and b'="y"] by simp}
  1.1771 +    moreover
  1.1772 +    {assume y: "y < -?Nt x s / ?N c" 
  1.1773 +      with ly have eu: "l < - ?Nt x s / ?N c" by auto
  1.1774 +      with noS ly yu have th: "- ?Nt x s / ?N c \<ge> u" by (cases "- ?Nt x s / ?N c < u", auto)
  1.1775 +      with xu px' have "False" by simp  hence ?case by simp }
  1.1776 +    ultimately have ?case using ycs by blast
  1.1777 +  }
  1.1778 +  ultimately show ?case by blast
  1.1779 +next
  1.1780 +    case (3 c s)
  1.1781 +  from "3.prems" 
  1.1782 +  have lin: "isnpoly c" "c \<noteq> 0\<^sub>p" "tmbound0 s" "allpolys isnpoly s"
  1.1783 +    and px: "Ifm vs (x # bs) (Eq (CNP 0 c s))"
  1.1784 +    and noS: "\<forall>t. l < t \<and> t < u \<longrightarrow> t \<noteq> - Itm vs (x # bs) s / \<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup>" by simp_all
  1.1785 +  from ly yu noS have yne: "y \<noteq> - ?Nt x s / \<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup>" by simp
  1.1786 +  hence ycs: "y < - ?Nt x s / ?N c \<or> y > -?Nt x s / ?N c" by auto
  1.1787 +  have ccs: "?N c = 0 \<or> ?N c < 0 \<or> ?N c > 0" by dlo
  1.1788 +  moreover
  1.1789 +  {assume "?N c = 0" hence ?case using px by (simp add: tmbound0_I[OF lin(3), where bs="bs" and b="x" and b'="y"])}
  1.1790 +  moreover
  1.1791 +  {assume c: "?N c > 0" hence cnz: "?N c \<noteq> 0" by simp
  1.1792 +    from px eq_divide_eq[of "x" "-?Nt x s" "?N c"]  cnz
  1.1793 +    have px': "x = - ?Nt x s / ?N c" by (simp add: ring_simps)
  1.1794 +    {assume y: "y < -?Nt x s / ?N c" 
  1.1795 +      with ly have eu: "l < - ?Nt x s / ?N c" by auto
  1.1796 +      with noS ly yu have th: "- ?Nt x s / ?N c \<ge> u" by (cases "- ?Nt x s / ?N c < u", auto)
  1.1797 +      with xu px' have "False" by simp  hence ?case by simp }
  1.1798 +    moreover
  1.1799 +    {assume y: "y > -?Nt x s / ?N c" 
  1.1800 +      with yu have eu: "u > - ?Nt x s / ?N c" by auto
  1.1801 +      with noS ly yu have th: "- ?Nt x s / ?N c \<le> l" by (cases "- ?Nt x s / ?N c > l", auto)
  1.1802 +      with lx px' have "False" by simp  hence ?case by simp }
  1.1803 +    ultimately have ?case using ycs by blast
  1.1804 +  }
  1.1805 +  moreover
  1.1806 +  {assume c: "?N c < 0" hence cnz: "?N c \<noteq> 0" by simp
  1.1807 +    from px eq_divide_eq[of "x" "-?Nt x s" "?N c"]  cnz
  1.1808 +    have px': "x = - ?Nt x s / ?N c" by (simp add: ring_simps)
  1.1809 +    {assume y: "y < -?Nt x s / ?N c" 
  1.1810 +      with ly have eu: "l < - ?Nt x s / ?N c" by auto
  1.1811 +      with noS ly yu have th: "- ?Nt x s / ?N c \<ge> u" by (cases "- ?Nt x s / ?N c < u", auto)
  1.1812 +      with xu px' have "False" by simp  hence ?case by simp }
  1.1813 +    moreover
  1.1814 +    {assume y: "y > -?Nt x s / ?N c" 
  1.1815 +      with yu have eu: "u > - ?Nt x s / ?N c" by auto
  1.1816 +      with noS ly yu have th: "- ?Nt x s / ?N c \<le> l" by (cases "- ?Nt x s / ?N c > l", auto)
  1.1817 +      with lx px' have "False" by simp  hence ?case by simp }
  1.1818 +    ultimately have ?case using ycs by blast
  1.1819 +  }
  1.1820 +  ultimately show ?case by blast
  1.1821 +next
  1.1822 +    case (4 c s)
  1.1823 +  from "4.prems" 
  1.1824 +  have lin: "isnpoly c" "c \<noteq> 0\<^sub>p" "tmbound0 s" "allpolys isnpoly s"
  1.1825 +    and px: "Ifm vs (x # bs) (NEq (CNP 0 c s))"
  1.1826 +    and noS: "\<forall>t. l < t \<and> t < u \<longrightarrow> t \<noteq> - Itm vs (x # bs) s / \<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup>" by simp_all
  1.1827 +  from ly yu noS have yne: "y \<noteq> - ?Nt x s / \<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup>" by simp
  1.1828 +  hence ycs: "y < - ?Nt x s / ?N c \<or> y > -?Nt x s / ?N c" by auto
  1.1829 +  have ccs: "?N c = 0 \<or> ?N c \<noteq> 0" by dlo
  1.1830 +  moreover
  1.1831 +  {assume "?N c = 0" hence ?case using px by (simp add: tmbound0_I[OF lin(3), where bs="bs" and b="x" and b'="y"])}
  1.1832 +  moreover
  1.1833 +  {assume c: "?N c \<noteq> 0"
  1.1834 +    from yne c eq_divide_eq[of "y" "- ?Nt x s" "?N c"] have ?case
  1.1835 +      by (simp add: ring_simps tmbound0_I[OF lin(3), of vs x bs y] sum_eq[symmetric]) }
  1.1836 +  ultimately show ?case by blast
  1.1837 +qed (auto simp add: nth_pos2 tmbound0_I[where vs=vs and bs="bs" and b="y" and b'="x"] bound0_I[where vs=vs and bs="bs" and b="y" and b'="x"])
  1.1838 +
  1.1839 +lemma one_plus_one_pos[simp]: "(1::'a::{ordered_field}) + 1 > 0"
  1.1840 +proof-
  1.1841 +  have op: "(1::'a) > 0" by simp
  1.1842 +  from add_pos_pos[OF op op] show ?thesis . 
  1.1843 +qed
  1.1844 +
  1.1845 +lemma one_plus_one_nonzero[simp]: "(1::'a::{ordered_field}) + 1 \<noteq> 0" 
  1.1846 +  using one_plus_one_pos[where ?'a = 'a] by (simp add: less_le) 
  1.1847 +
  1.1848 +lemma half_sum_eq: "(u + u) / (1+1) = (u::'a::{ordered_field})" 
  1.1849 +proof-
  1.1850 +  have "(u + u) = (1 + 1) * u" by (simp add: ring_simps)
  1.1851 +  hence "(u + u) / (1+1) = (1 + 1)*u / (1 + 1)" by simp
  1.1852 +  with nonzero_mult_divide_cancel_left[OF one_plus_one_nonzero, of u] show ?thesis by simp
  1.1853 +qed
  1.1854 +
  1.1855 +lemma inf_uset:
  1.1856 +  assumes lp: "islin p"
  1.1857 +  and nmi: "\<not> (Ifm vs (x#bs) (minusinf p))" (is "\<not> (Ifm vs (x#bs) (?M p))")
  1.1858 +  and npi: "\<not> (Ifm vs (x#bs) (plusinf p))" (is "\<not> (Ifm vs (x#bs) (?P p))")
  1.1859 +  and ex: "\<exists> x.  Ifm vs (x#bs) p" (is "\<exists> x. ?I x p")
  1.1860 +  shows "\<exists> (c,t) \<in> set (uset p). \<exists> (d,s) \<in> set (uset p). ?I ((- Itm vs (x#bs) t / Ipoly vs c + - Itm vs (x#bs) s / Ipoly vs d) / (1 + 1)) p" 
  1.1861 +proof-
  1.1862 +  let ?Nt = "\<lambda> x t. Itm vs (x#bs) t"
  1.1863 +  let ?N = "Ipoly vs"
  1.1864 +  let ?U = "set (uset p)"
  1.1865 +  from ex obtain a where pa: "?I a p" by blast
  1.1866 +  from bound0_I[OF minusinf_nb[OF lp], where bs="bs" and b="x" and b'="a"] nmi
  1.1867 +  have nmi': "\<not> (?I a (?M p))" by simp
  1.1868 +  from bound0_I[OF plusinf_nb[OF lp], where bs="bs" and b="x" and b'="a"] npi
  1.1869 +  have npi': "\<not> (?I a (?P p))" by simp
  1.1870 +  have "\<exists> (c,t) \<in> set (uset p). \<exists> (d,s) \<in> set (uset p). ?I ((- ?Nt a t/?N c + - ?Nt a s /?N d) / (1 + 1)) p"
  1.1871 +  proof-
  1.1872 +    let ?M = "(\<lambda> (c,t). - ?Nt a t / ?N c) ` ?U"
  1.1873 +    have fM: "finite ?M" by auto
  1.1874 +    from minusinf_uset[OF lp nmi pa] plusinf_uset[OF lp npi pa] 
  1.1875 +    have "\<exists> (c,t) \<in> set (uset p). \<exists> (d,s) \<in> set (uset p). a \<le> - ?Nt x t / ?N c \<and> a \<ge> - ?Nt x s / ?N d" by blast
  1.1876 +    then obtain "c" "t" "d" "s" where 
  1.1877 +      ctU: "(c,t) \<in> ?U" and dsU: "(d,s) \<in> ?U" 
  1.1878 +      and xs1: "a \<le> - ?Nt x s / ?N d" and tx1: "a \<ge> - ?Nt x t / ?N c" by blast
  1.1879 +    from uset_l[OF lp] ctU dsU tmbound0_I[where bs="bs" and b="x" and b'="a"] xs1 tx1 
  1.1880 +    have xs: "a \<le> - ?Nt a s / ?N d" and tx: "a \<ge> - ?Nt a t / ?N c" by auto
  1.1881 +    from ctU have Mne: "?M \<noteq> {}" by auto
  1.1882 +    hence Une: "?U \<noteq> {}" by simp
  1.1883 +    let ?l = "Min ?M"
  1.1884 +    let ?u = "Max ?M"
  1.1885 +    have linM: "?l \<in> ?M" using fM Mne by simp
  1.1886 +    have uinM: "?u \<in> ?M" using fM Mne by simp
  1.1887 +    have ctM: "- ?Nt a t / ?N c \<in> ?M" using ctU by auto
  1.1888 +    have dsM: "- ?Nt a s / ?N d \<in> ?M" using dsU by auto 
  1.1889 +    have lM: "\<forall> t\<in> ?M. ?l \<le> t" using Mne fM by auto
  1.1890 +    have Mu: "\<forall> t\<in> ?M. t \<le> ?u" using Mne fM by auto
  1.1891 +    have "?l \<le> - ?Nt a t / ?N c" using ctM Mne by simp hence lx: "?l \<le> a" using tx by simp
  1.1892 +    have "- ?Nt a s / ?N d \<le> ?u" using dsM Mne by simp hence xu: "a \<le> ?u" using xs by simp
  1.1893 +    from finite_set_intervals2[where P="\<lambda> x. ?I x p",OF pa lx xu linM uinM fM lM Mu]
  1.1894 +    have "(\<exists> s\<in> ?M. ?I s p) \<or> 
  1.1895 +      (\<exists> t1\<in> ?M. \<exists> t2 \<in> ?M. (\<forall> y. t1 < y \<and> y < t2 \<longrightarrow> y \<notin> ?M) \<and> t1 < a \<and> a < t2 \<and> ?I a p)" .
  1.1896 +    moreover {fix u assume um: "u\<in> ?M" and pu: "?I u p"
  1.1897 +      hence "\<exists> (nu,tu) \<in> ?U. u = - ?Nt a tu / ?N nu" by auto
  1.1898 +      then obtain "tu" "nu" where tuU: "(nu,tu) \<in> ?U" and tuu:"u= - ?Nt a tu / ?N nu" by blast
  1.1899 +      from half_sum_eq[of u] pu tuu 
  1.1900 +      have "?I (((- ?Nt a tu / ?N nu) + (- ?Nt a tu / ?N nu)) / (1 + 1)) p" by simp
  1.1901 +      with tuU have ?thesis by blast}
  1.1902 +    moreover{
  1.1903 +      assume "\<exists> t1\<in> ?M. \<exists> t2 \<in> ?M. (\<forall> y. t1 < y \<and> y < t2 \<longrightarrow> y \<notin> ?M) \<and> t1 < a \<and> a < t2 \<and> ?I a p"
  1.1904 +      then obtain t1 and t2 where t1M: "t1 \<in> ?M" and t2M: "t2\<in> ?M" 
  1.1905 +	and noM: "\<forall> y. t1 < y \<and> y < t2 \<longrightarrow> y \<notin> ?M" and t1x: "t1 < a" and xt2: "a < t2" and px: "?I a p"
  1.1906 +	by blast
  1.1907 +      from t1M have "\<exists> (t1n,t1u) \<in> ?U. t1 = - ?Nt a t1u / ?N t1n" by auto
  1.1908 +      then obtain "t1u" "t1n" where t1uU: "(t1n,t1u) \<in> ?U" and t1u: "t1 = - ?Nt a t1u / ?N t1n" by blast
  1.1909 +      from t2M have "\<exists> (t2n,t2u) \<in> ?U. t2 = - ?Nt a t2u / ?N t2n" by auto
  1.1910 +      then obtain "t2u" "t2n" where t2uU: "(t2n,t2u) \<in> ?U" and t2u: "t2 = - ?Nt a t2u / ?N t2n" by blast
  1.1911 +      from t1x xt2 have t1t2: "t1 < t2" by simp
  1.1912 +      let ?u = "(t1 + t2) / (1 + 1)"
  1.1913 +      from less_half_sum[OF t1t2] gt_half_sum[OF t1t2] have t1lu: "t1 < ?u" and ut2: "?u < t2" by auto
  1.1914 +      from lin_dense[OF lp noM t1x xt2 px t1lu ut2] have "?I ?u p" .
  1.1915 +      with t1uU t2uU t1u t2u have ?thesis by blast}
  1.1916 +    ultimately show ?thesis by blast
  1.1917 +  qed
  1.1918 +  then obtain "l" "n" "s"  "m" where lnU: "(n,l) \<in> ?U" and smU:"(m,s) \<in> ?U" 
  1.1919 +    and pu: "?I ((- ?Nt a l / ?N n + - ?Nt a s / ?N m) / (1 + 1)) p" by blast
  1.1920 +  from lnU smU uset_l[OF lp] have nbl: "tmbound0 l" and nbs: "tmbound0 s" by auto
  1.1921 +  from tmbound0_I[OF nbl, where bs="bs" and b="a" and b'="x"] 
  1.1922 +    tmbound0_I[OF nbs, where bs="bs" and b="a" and b'="x"] pu
  1.1923 +  have "?I ((- ?Nt x l / ?N n + - ?Nt x s / ?N m) / (1 + 1)) p" by simp
  1.1924 +  with lnU smU
  1.1925 +  show ?thesis by auto
  1.1926 +qed
  1.1927 +
  1.1928 +    (* The Ferrante - Rackoff Theorem *)
  1.1929 +
  1.1930 +theorem fr_eq: 
  1.1931 +  assumes lp: "islin p"
  1.1932 +  shows "(\<exists> x. Ifm vs (x#bs) p) = ((Ifm vs (x#bs) (minusinf p)) \<or> (Ifm vs (x#bs) (plusinf p)) \<or> (\<exists> (n,t) \<in> set (uset p). \<exists> (m,s) \<in> set (uset p). Ifm vs (((- Itm vs (x#bs) t /  Ipoly vs n + - Itm vs (x#bs) s / Ipoly vs m) /(1 + 1))#bs) p))"
  1.1933 +  (is "(\<exists> x. ?I x p) = (?M \<or> ?P \<or> ?F)" is "?E = ?D")
  1.1934 +proof
  1.1935 +  assume px: "\<exists> x. ?I x p"
  1.1936 +  have "?M \<or> ?P \<or> (\<not> ?M \<and> \<not> ?P)" by blast
  1.1937 +  moreover {assume "?M \<or> ?P" hence "?D" by blast}
  1.1938 +  moreover {assume nmi: "\<not> ?M" and npi: "\<not> ?P"
  1.1939 +    from inf_uset[OF lp nmi npi] have "?F" using px by blast hence "?D" by blast}
  1.1940 +  ultimately show "?D" by blast
  1.1941 +next
  1.1942 +  assume "?D" 
  1.1943 +  moreover {assume m:"?M" from minusinf_ex[OF lp m] have "?E" .}
  1.1944 +  moreover {assume p: "?P" from plusinf_ex[OF lp p] have "?E" . }
  1.1945 +  moreover {assume f:"?F" hence "?E" by blast}
  1.1946 +  ultimately show "?E" by blast
  1.1947 +qed
  1.1948 +
  1.1949 +section{* First implementation : Naive by encoding all case splits locally *}
  1.1950 +definition "msubsteq c t d s a r = 
  1.1951 +  evaldjf (split conj) 
  1.1952 +  [(let cd = c *\<^sub>p d in (NEq (CP cd), Eq (Add (Mul (~\<^sub>p a) (Add (Mul d t) (Mul c s))) (Mul (2\<^sub>p *\<^sub>p cd) r)))),
  1.1953 +   (conj (Eq (CP c)) (NEq (CP d)) , Eq (Add (Mul (~\<^sub>p a) s) (Mul (2\<^sub>p *\<^sub>p d) r))),
  1.1954 +   (conj (NEq (CP c)) (Eq (CP d)) , Eq (Add (Mul (~\<^sub>p a) t) (Mul (2\<^sub>p *\<^sub>p c) r))),
  1.1955 +   (conj (Eq (CP c)) (Eq (CP d)) , Eq r)]"
  1.1956 +
  1.1957 +lemma msubsteq_nb: assumes lp: "islin (Eq (CNP 0 a r))" and t: "tmbound0 t" and s: "tmbound0 s"
  1.1958 +  shows "bound0 (msubsteq c t d s a r)"
  1.1959 +proof-
  1.1960 +  have th: "\<forall>x\<in> set [(let cd = c *\<^sub>p d in (NEq (CP cd), Eq (Add (Mul (~\<^sub>p a) (Add (Mul d t) (Mul c s))) (Mul (2\<^sub>p *\<^sub>p cd) r)))),
  1.1961 +   (conj (Eq (CP c)) (NEq (CP d)) , Eq (Add (Mul (~\<^sub>p a) s) (Mul (2\<^sub>p *\<^sub>p d) r))),
  1.1962 +   (conj (NEq (CP c)) (Eq (CP d)) , Eq (Add (Mul (~\<^sub>p a) t) (Mul (2\<^sub>p *\<^sub>p c) r))),
  1.1963 +   (conj (Eq (CP c)) (Eq (CP d)) , Eq r)]. bound0 (split conj x)"
  1.1964 +    using lp by (simp add: Let_def t s )
  1.1965 +  from evaldjf_bound0[OF th] show ?thesis by (simp add: msubsteq_def)
  1.1966 +qed
  1.1967 +
  1.1968 +lemma msubsteq: assumes lp: "islin (Eq (CNP 0 a r))"
  1.1969 +  shows "Ifm vs (x#bs) (msubsteq c t d s a r) = Ifm vs (((- Itm vs (x#bs) t /  Ipoly vs c + - Itm vs (x#bs) s / Ipoly vs d) /(1 + 1))#bs) (Eq (CNP 0 a r))" (is "?lhs = ?rhs")
  1.1970 +proof-
  1.1971 +  let ?Nt = "\<lambda>(x::'a) t. Itm vs (x#bs) t"
  1.1972 +  let ?N = "\<lambda>p. Ipoly vs p"
  1.1973 +  let ?c = "?N c"
  1.1974 +  let ?d = "?N d"
  1.1975 +  let ?t = "?Nt x t"
  1.1976 +  let ?s = "?Nt x s"
  1.1977 +  let ?a = "?N a"
  1.1978 +  let ?r = "?Nt x r"
  1.1979 +  from lp have lin:"isnpoly a" "a \<noteq> 0\<^sub>p" "tmbound0 r" "allpolys isnpoly r" by simp_all
  1.1980 +  note r= tmbound0_I[OF lin(3), of vs _ bs x]
  1.1981 +  have cd_cs: "?c * ?d \<noteq> 0 \<or> (?c = 0 \<and> ?d = 0) \<or> (?c = 0 \<and> ?d \<noteq> 0) \<or> (?c \<noteq> 0 \<and> ?d = 0)" by auto
  1.1982 +  moreover
  1.1983 +  {assume c: "?c = 0" and d: "?d=0"
  1.1984 +    hence ?thesis  by (simp add: r[of 0] msubsteq_def Let_def evaldjf_ex)}
  1.1985 +  moreover 
  1.1986 +  {assume c: "?c = 0" and d: "?d\<noteq>0"
  1.1987 +    from c have th: "(- ?t / ?c + - ?s / ?d)/(1 + 1) = -?s / ((1 + 1)*?d)" by simp
  1.1988 +    have "?rhs = Ifm vs (-?s / ((1 + 1)*?d) # bs) (Eq (CNP 0 a r))" by (simp only: th)
  1.1989 +    also have "\<dots> \<longleftrightarrow> ?a * (-?s / ((1 + 1)*?d)) + ?r = 0" by (simp add: r[of "- (Itm vs (x # bs) s / ((1 + 1) * \<lparr>d\<rparr>\<^sub>p\<^bsup>vs\<^esup>))"])
  1.1990 +    also have "\<dots> \<longleftrightarrow> (1 + 1)*?d * (?a * (-?s / ((1 + 1)*?d)) + ?r) = 0" 
  1.1991 +      using d mult_cancel_left[of "(1 + 1)*?d" "(?a * (-?s / ((1 + 1)*?d)) + ?r)" 0] by simp
  1.1992 +    also have "\<dots> \<longleftrightarrow> (- ?a * ?s) * ((1 + 1)*?d / ((1 + 1)*?d)) + (1 + 1)*?d*?r= 0"
  1.1993 +      by (simp add: ring_simps right_distrib[of "(1 + 1)*?d"] del: right_distrib)
  1.1994 +    
  1.1995 +    also have "\<dots> \<longleftrightarrow> - (?a * ?s) + (1 + 1)*?d*?r = 0" using d by simp 
  1.1996 +    finally have ?thesis using c d 
  1.1997 +      apply (simp add: r[of "- (Itm vs (x # bs) s / ((1 + 1) * \<lparr>d\<rparr>\<^sub>p\<^bsup>vs\<^esup>))"] msubsteq_def Let_def evaldjf_ex del: one_add_one_is_two)
  1.1998 +      apply (simp only: one_add_one_is_two[symmetric] of_int_add)
  1.1999 +      apply simp
  1.2000 +      done}
  1.2001 +  moreover
  1.2002 +  {assume c: "?c \<noteq> 0" and d: "?d=0"
  1.2003 +    from d have th: "(- ?t / ?c + - ?s / ?d)/(1 + 1) = -?t / ((1 + 1)*?c)" by simp
  1.2004 +    have "?rhs = Ifm vs (-?t / ((1 + 1)*?c) # bs) (Eq (CNP 0 a r))" by (simp only: th)
  1.2005 +    also have "\<dots> \<longleftrightarrow> ?a * (-?t / ((1 + 1)*?c)) + ?r = 0" by (simp add: r[of "- (?t/ ((1 + 1)* ?c))"])
  1.2006 +    also have "\<dots> \<longleftrightarrow> (1 + 1)*?c * (?a * (-?t / ((1 + 1)*?c)) + ?r) = 0" 
  1.2007 +      using c mult_cancel_left[of "(1 + 1)*?c" "(?a * (-?t / ((1 + 1)*?c)) + ?r)" 0] by simp
  1.2008 +    also have "\<dots> \<longleftrightarrow> (?a * -?t)* ((1 + 1)*?c) / ((1 + 1)*?c) + (1 + 1)*?c*?r= 0"
  1.2009 +      by (simp add: ring_simps right_distrib[of "(1 + 1)*?c"] del: right_distrib)
  1.2010 +    also have "\<dots> \<longleftrightarrow> - (?a * ?t) + (1 + 1)*?c*?r = 0" using c by simp 
  1.2011 +    finally have ?thesis using c d 
  1.2012 +      apply (simp add: r[of "- (?t/ ((1 + 1)*?c))"] msubsteq_def Let_def evaldjf_ex del: one_add_one_is_two)
  1.2013 +      apply (simp only: one_add_one_is_two[symmetric] of_int_add)
  1.2014 +      apply simp
  1.2015 +      done }
  1.2016 +  moreover
  1.2017 +  {assume c: "?c \<noteq> 0" and d: "?d\<noteq>0" hence dc: "?c * ?d *(1 + 1) \<noteq> 0" by simp
  1.2018 +    from add_frac_eq[OF c d, of "- ?t" "- ?s"]
  1.2019 +    have th: "(- ?t / ?c + - ?s / ?d)/(1 + 1) = - (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d)" 
  1.2020 +      by (simp add: ring_simps)
  1.2021 +    have "?rhs \<longleftrightarrow> Ifm vs (- (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d) # bs) (Eq (CNP 0 a r))" by (simp only: th)
  1.2022 +    also have "\<dots> \<longleftrightarrow> ?a * (- (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d)) + ?r = 0" 
  1.2023 +      by (simp add: r[of "(- (?d * ?t) + - (?c *?s)) / ((1 + 1) * ?c * ?d)"])
  1.2024 +    also have "\<dots> \<longleftrightarrow> ((1 + 1) * ?c * ?d) * (?a * (- (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d)) + ?r) =0 "
  1.2025 +      using c d mult_cancel_left[of "(1 + 1) * ?c * ?d" "?a * (- (?d * ?t + ?c* ?s)/ ((1 + 1)*?c*?d)) + ?r" 0] by simp
  1.2026 +    also have "\<dots> \<longleftrightarrow> ?a * (- (?d * ?t + ?c* ?s )) + (1 + 1)*?c*?d*?r =0" 
  1.2027 +      using nonzero_mult_divide_cancel_left[OF dc] c d
  1.2028 +      by (simp add: ring_simps diff_divide_distrib del: left_distrib)
  1.2029 +    finally  have ?thesis using c d 
  1.2030 +      apply (simp add: r[of "(- (?d * ?t) + - (?c *?s)) / ((1 + 1) * ?c * ?d)"] msubsteq_def Let_def evaldjf_ex ring_simps)
  1.2031 +      apply (simp only: one_add_one_is_two[symmetric] of_int_add)
  1.2032 +      apply (simp add: ring_simps)
  1.2033 +      done }
  1.2034 +  ultimately show ?thesis by blast
  1.2035 +qed
  1.2036 +
  1.2037 +
  1.2038 +definition "msubstneq c t d s a r = 
  1.2039 +  evaldjf (split conj) 
  1.2040 +  [(let cd = c *\<^sub>p d in (NEq (CP cd), NEq (Add (Mul (~\<^sub>p a) (Add (Mul d t) (Mul c s))) (Mul (2\<^sub>p *\<^sub>p cd) r)))),
  1.2041 +   (conj (Eq (CP c)) (NEq (CP d)) , NEq (Add (Mul (~\<^sub>p a) s) (Mul (2\<^sub>p *\<^sub>p d) r))),
  1.2042 +   (conj (NEq (CP c)) (Eq (CP d)) , NEq (Add (Mul (~\<^sub>p a) t) (Mul (2\<^sub>p *\<^sub>p c) r))),
  1.2043 +   (conj (Eq (CP c)) (Eq (CP d)) , NEq r)]"
  1.2044 +
  1.2045 +lemma msubstneq_nb: assumes lp: "islin (NEq (CNP 0 a r))" and t: "tmbound0 t" and s: "tmbound0 s"
  1.2046 +  shows "bound0 (msubstneq c t d s a r)"
  1.2047 +proof-
  1.2048 +  have th: "\<forall>x\<in> set [(let cd = c *\<^sub>p d in (NEq (CP cd), NEq (Add (Mul (~\<^sub>p a) (Add (Mul d t) (Mul c s))) (Mul (2\<^sub>p *\<^sub>p cd) r)))), 
  1.2049 +    (conj (Eq (CP c)) (NEq (CP d)) , NEq (Add (Mul (~\<^sub>p a) s) (Mul (2\<^sub>p *\<^sub>p d) r))),
  1.2050 +    (conj (NEq (CP c)) (Eq (CP d)) , NEq (Add (Mul (~\<^sub>p a) t) (Mul (2\<^sub>p *\<^sub>p c) r))),
  1.2051 +    (conj (Eq (CP c)) (Eq (CP d)) , NEq r)]. bound0 (split conj x)"
  1.2052 +    using lp by (simp add: Let_def t s )
  1.2053 +  from evaldjf_bound0[OF th] show ?thesis by (simp add: msubstneq_def)
  1.2054 +qed
  1.2055 +
  1.2056 +lemma msubstneq: assumes lp: "islin (Eq (CNP 0 a r))"
  1.2057 +  shows "Ifm vs (x#bs) (msubstneq c t d s a r) = Ifm vs (((- Itm vs (x#bs) t /  Ipoly vs c + - Itm vs (x#bs) s / Ipoly vs d) /(1 + 1))#bs) (NEq (CNP 0 a r))" (is "?lhs = ?rhs")
  1.2058 +proof-
  1.2059 +  let ?Nt = "\<lambda>(x::'a) t. Itm vs (x#bs) t"
  1.2060 +  let ?N = "\<lambda>p. Ipoly vs p"
  1.2061 +  let ?c = "?N c"
  1.2062 +  let ?d = "?N d"
  1.2063 +  let ?t = "?Nt x t"
  1.2064 +  let ?s = "?Nt x s"
  1.2065 +  let ?a = "?N a"
  1.2066 +  let ?r = "?Nt x r"
  1.2067 +  from lp have lin:"isnpoly a" "a \<noteq> 0\<^sub>p" "tmbound0 r" "allpolys isnpoly r" by simp_all
  1.2068 +  note r= tmbound0_I[OF lin(3), of vs _ bs x]
  1.2069 +  have cd_cs: "?c * ?d \<noteq> 0 \<or> (?c = 0 \<and> ?d = 0) \<or> (?c = 0 \<and> ?d \<noteq> 0) \<or> (?c \<noteq> 0 \<and> ?d = 0)" by auto
  1.2070 +  moreover
  1.2071 +  {assume c: "?c = 0" and d: "?d=0"
  1.2072 +    hence ?thesis  by (simp add: r[of 0] msubstneq_def Let_def evaldjf_ex)}
  1.2073 +  moreover 
  1.2074 +  {assume c: "?c = 0" and d: "?d\<noteq>0"
  1.2075 +    from c have th: "(- ?t / ?c + - ?s / ?d)/(1 + 1) = -?s / ((1 + 1)*?d)" by simp
  1.2076 +    have "?rhs = Ifm vs (-?s / ((1 + 1)*?d) # bs) (NEq (CNP 0 a r))" by (simp only: th)
  1.2077 +    also have "\<dots> \<longleftrightarrow> ?a * (-?s / ((1 + 1)*?d)) + ?r \<noteq> 0" by (simp add: r[of "- (Itm vs (x # bs) s / ((1 + 1) * \<lparr>d\<rparr>\<^sub>p\<^bsup>vs\<^esup>))"])
  1.2078 +    also have "\<dots> \<longleftrightarrow> (1 + 1)*?d * (?a * (-?s / ((1 + 1)*?d)) + ?r) \<noteq> 0" 
  1.2079 +      using d mult_cancel_left[of "(1 + 1)*?d" "(?a * (-?s / ((1 + 1)*?d)) + ?r)" 0] by simp
  1.2080 +    also have "\<dots> \<longleftrightarrow> (- ?a * ?s) * ((1 + 1)*?d / ((1 + 1)*?d)) + (1 + 1)*?d*?r\<noteq> 0"
  1.2081 +      by (simp add: ring_simps right_distrib[of "(1 + 1)*?d"] del: right_distrib)
  1.2082 +    
  1.2083 +    also have "\<dots> \<longleftrightarrow> - (?a * ?s) + (1 + 1)*?d*?r \<noteq> 0" using d by simp 
  1.2084 +    finally have ?thesis using c d 
  1.2085 +      apply (simp add: r[of "- (Itm vs (x # bs) s / ((1 + 1) * \<lparr>d\<rparr>\<^sub>p\<^bsup>vs\<^esup>))"] msubstneq_def Let_def evaldjf_ex del: one_add_one_is_two)
  1.2086 +      apply (simp only: one_add_one_is_two[symmetric] of_int_add)
  1.2087 +      apply simp
  1.2088 +      done}
  1.2089 +  moreover
  1.2090 +  {assume c: "?c \<noteq> 0" and d: "?d=0"
  1.2091 +    from d have th: "(- ?t / ?c + - ?s / ?d)/(1 + 1) = -?t / ((1 + 1)*?c)" by simp
  1.2092 +    have "?rhs = Ifm vs (-?t / ((1 + 1)*?c) # bs) (NEq (CNP 0 a r))" by (simp only: th)
  1.2093 +    also have "\<dots> \<longleftrightarrow> ?a * (-?t / ((1 + 1)*?c)) + ?r \<noteq> 0" by (simp add: r[of "- (?t/ ((1 + 1)* ?c))"])
  1.2094 +    also have "\<dots> \<longleftrightarrow> (1 + 1)*?c * (?a * (-?t / ((1 + 1)*?c)) + ?r) \<noteq> 0" 
  1.2095 +      using c mult_cancel_left[of "(1 + 1)*?c" "(?a * (-?t / ((1 + 1)*?c)) + ?r)" 0] by simp
  1.2096 +    also have "\<dots> \<longleftrightarrow> (?a * -?t)* ((1 + 1)*?c) / ((1 + 1)*?c) + (1 + 1)*?c*?r \<noteq> 0"
  1.2097 +      by (simp add: ring_simps right_distrib[of "(1 + 1)*?c"] del: right_distrib)
  1.2098 +    also have "\<dots> \<longleftrightarrow> - (?a * ?t) + (1 + 1)*?c*?r \<noteq> 0" using c by simp 
  1.2099 +    finally have ?thesis using c d 
  1.2100 +      apply (simp add: r[of "- (?t/ ((1 + 1)*?c))"] msubstneq_def Let_def evaldjf_ex del: one_add_one_is_two)
  1.2101 +      apply (simp only: one_add_one_is_two[symmetric] of_int_add)
  1.2102 +      apply simp
  1.2103 +      done }
  1.2104 +  moreover
  1.2105 +  {assume c: "?c \<noteq> 0" and d: "?d\<noteq>0" hence dc: "?c * ?d *(1 + 1) \<noteq> 0" by simp
  1.2106 +    from add_frac_eq[OF c d, of "- ?t" "- ?s"]
  1.2107 +    have th: "(- ?t / ?c + - ?s / ?d)/(1 + 1) = - (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d)" 
  1.2108 +      by (simp add: ring_simps)
  1.2109 +    have "?rhs \<longleftrightarrow> Ifm vs (- (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d) # bs) (NEq (CNP 0 a r))" by (simp only: th)
  1.2110 +    also have "\<dots> \<longleftrightarrow> ?a * (- (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d)) + ?r \<noteq> 0" 
  1.2111 +      by (simp add: r[of "(- (?d * ?t) + - (?c *?s)) / ((1 + 1) * ?c * ?d)"])
  1.2112 +    also have "\<dots> \<longleftrightarrow> ((1 + 1) * ?c * ?d) * (?a * (- (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d)) + ?r) \<noteq> 0 "
  1.2113 +      using c d mult_cancel_left[of "(1 + 1) * ?c * ?d" "?a * (- (?d * ?t + ?c* ?s)/ ((1 + 1)*?c*?d)) + ?r" 0] by simp
  1.2114 +    also have "\<dots> \<longleftrightarrow> ?a * (- (?d * ?t + ?c* ?s )) + (1 + 1)*?c*?d*?r \<noteq> 0" 
  1.2115 +      using nonzero_mult_divide_cancel_left[OF dc] c d
  1.2116 +      by (simp add: ring_simps diff_divide_distrib del: left_distrib)
  1.2117 +    finally  have ?thesis using c d 
  1.2118 +      apply (simp add: r[of "(- (?d * ?t) + - (?c *?s)) / ((1 + 1) * ?c * ?d)"] msubstneq_def Let_def evaldjf_ex ring_simps)
  1.2119 +      apply (simp only: one_add_one_is_two[symmetric] of_int_add)
  1.2120 +      apply (simp add: ring_simps)
  1.2121 +      done }
  1.2122 +  ultimately show ?thesis by blast
  1.2123 +qed
  1.2124 +
  1.2125 +definition "msubstlt c t d s a r = 
  1.2126 +  evaldjf (split conj) 
  1.2127 +  [(let cd = c *\<^sub>p d in (lt (CP (~\<^sub>p cd)), Lt (Add (Mul (~\<^sub>p a) (Add (Mul d t) (Mul c s))) (Mul (2\<^sub>p *\<^sub>p cd) r)))),
  1.2128 +  (let cd = c *\<^sub>p d in (lt (CP cd), Lt (Sub (Mul a (Add (Mul d t) (Mul c s))) (Mul (2\<^sub>p *\<^sub>p cd) r)))),
  1.2129 +   (conj (lt (CP (~\<^sub>p c))) (Eq (CP d)) , Lt (Add (Mul (~\<^sub>p a) t) (Mul (2\<^sub>p *\<^sub>p c) r))),
  1.2130 +   (conj (lt (CP c)) (Eq (CP d)) , Lt (Sub (Mul a t) (Mul (2\<^sub>p *\<^sub>p c) r))),
  1.2131 +   (conj (lt (CP (~\<^sub>p d))) (Eq (CP c)) , Lt (Add (Mul (~\<^sub>p a) s) (Mul (2\<^sub>p *\<^sub>p d) r))),
  1.2132 +   (conj (lt (CP d)) (Eq (CP c)) , Lt (Sub (Mul a s) (Mul (2\<^sub>p *\<^sub>p d) r))),
  1.2133 +   (conj (Eq (CP c)) (Eq (CP d)) , Lt r)]"
  1.2134 +
  1.2135 +lemma msubstlt_nb: assumes lp: "islin (Lt (CNP 0 a r))" and t: "tmbound0 t" and s: "tmbound0 s"
  1.2136 +  shows "bound0 (msubstlt c t d s a r)"
  1.2137 +proof-
  1.2138 +  have th: "\<forall>x\<in> set [(let cd = c *\<^sub>p d in (lt (CP (~\<^sub>p cd)), Lt (Add (Mul (~\<^sub>p a) (Add (Mul d t) (Mul c s))) (Mul (2\<^sub>p *\<^sub>p cd) r)))),
  1.2139 +  (let cd = c *\<^sub>p d in (lt (CP cd), Lt (Sub (Mul a (Add (Mul d t) (Mul c s))) (Mul (2\<^sub>p *\<^sub>p cd) r)))),
  1.2140 +   (conj (lt (CP (~\<^sub>p c))) (Eq (CP d)) , Lt (Add (Mul (~\<^sub>p a) t) (Mul (2\<^sub>p *\<^sub>p c) r))),
  1.2141 +   (conj (lt (CP c)) (Eq (CP d)) , Lt (Sub (Mul a t) (Mul (2\<^sub>p *\<^sub>p c) r))),
  1.2142 +   (conj (lt (CP (~\<^sub>p d))) (Eq (CP c)) , Lt (Add (Mul (~\<^sub>p a) s) (Mul (2\<^sub>p *\<^sub>p d) r))),
  1.2143 +   (conj (lt (CP d)) (Eq (CP c)) , Lt (Sub (Mul a s) (Mul (2\<^sub>p *\<^sub>p d) r))),
  1.2144 +   (conj (Eq (CP c)) (Eq (CP d)) , Lt r)]. bound0 (split conj x)"
  1.2145 +    using lp by (simp add: Let_def t s lt_nb )
  1.2146 +  from evaldjf_bound0[OF th] show ?thesis by (simp add: msubstlt_def)
  1.2147 +qed
  1.2148 +
  1.2149 +
  1.2150 +lemma msubstlt: assumes nc: "isnpoly c" and nd: "isnpoly d" and lp: "islin (Lt (CNP 0 a r))" 
  1.2151 +  shows "Ifm vs (x#bs) (msubstlt c t d s a r) \<longleftrightarrow> 
  1.2152 +  Ifm vs (((- Itm vs (x#bs) t /  Ipoly vs c + - Itm vs (x#bs) s / Ipoly vs d) /(1 + 1))#bs) (Lt (CNP 0 a r))" (is "?lhs = ?rhs")
  1.2153 +proof-
  1.2154 +  let ?Nt = "\<lambda>x t. Itm vs (x#bs) t"
  1.2155 +  let ?N = "\<lambda>p. Ipoly vs p"
  1.2156 +  let ?c = "?N c"
  1.2157 +  let ?d = "?N d"
  1.2158 +  let ?t = "?Nt x t"
  1.2159 +  let ?s = "?Nt x s"
  1.2160 +  let ?a = "?N a"
  1.2161 +  let ?r = "?Nt x r"
  1.2162 +  from lp have lin:"isnpoly a" "a \<noteq> 0\<^sub>p" "tmbound0 r" "allpolys isnpoly r" by simp_all
  1.2163 +  note r= tmbound0_I[OF lin(3), of vs _ bs x]
  1.2164 +  have cd_cs: "?c * ?d < 0 \<or> ?c * ?d > 0 \<or> (?c = 0 \<and> ?d = 0) \<or> (?c = 0 \<and> ?d < 0) \<or> (?c = 0 \<and> ?d > 0) \<or> (?c < 0 \<and> ?d = 0) \<or> (?c > 0 \<and> ?d = 0)" by auto
  1.2165 +  moreover
  1.2166 +  {assume c: "?c=0" and d: "?d=0"
  1.2167 +    hence ?thesis  using nc nd by (simp add: polyneg_norm lt r[of 0] msubstlt_def Let_def evaldjf_ex)}
  1.2168 +  moreover
  1.2169 +  {assume dc: "?c*?d > 0" 
  1.2170 +    from mult_pos_pos[OF one_plus_one_pos dc] have dc': "(1 + 1)*?c *?d > 0" by simp
  1.2171 +    hence c:"?c \<noteq> 0" and d: "?d\<noteq> 0" by auto
  1.2172 +    from dc' have dc'': "\<not> (1 + 1)*?c *?d < 0" by simp
  1.2173 +    from add_frac_eq[OF c d, of "- ?t" "- ?s"]
  1.2174 +    have th: "(- ?t / ?c + - ?s / ?d)/(1 + 1) = - (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d)" 
  1.2175 +      by (simp add: ring_simps)
  1.2176 +    have "?rhs \<longleftrightarrow> Ifm vs (- (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d) # bs) (Lt (CNP 0 a r))" by (simp only: th)
  1.2177 +    also have "\<dots> \<longleftrightarrow> ?a * (- (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d)) + ?r < 0" 
  1.2178 +      by (simp add: r[of "(- (?d * ?t) + - (?c *?s)) / ((1 + 1) * ?c * ?d)"])
  1.2179 +    also have "\<dots> \<longleftrightarrow> ((1 + 1) * ?c * ?d) * (?a * (- (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d)) + ?r) < 0"
  1.2180 +      
  1.2181 +      using dc' dc'' mult_less_cancel_left_disj[of "(1 + 1) * ?c * ?d" "?a * (- (?d * ?t + ?c* ?s)/ ((1 + 1)*?c*?d)) + ?r" 0] by simp
  1.2182 +    also have "\<dots> \<longleftrightarrow> ?a * (- (?d * ?t + ?c* ?s )) + (1 + 1)*?c*?d*?r < 0" 
  1.2183 +      using nonzero_mult_divide_cancel_left[of "(1 + 1)*?c*?d"] c d
  1.2184 +      by (simp add: ring_simps diff_divide_distrib del: left_distrib)
  1.2185 +    finally  have ?thesis using dc c d  nc nd dc'
  1.2186 +      apply (simp add: r[of "(- (?d * ?t) + - (?c *?s)) / ((1 + 1) * ?c * ?d)"] msubstlt_def Let_def evaldjf_ex ring_simps lt polyneg_norm polymul_norm) 
  1.2187 +    apply (simp only: one_add_one_is_two[symmetric] of_int_add)
  1.2188 +    by (simp add: ring_simps order_less_not_sym[OF dc])}
  1.2189 +  moreover
  1.2190 +  {assume dc: "?c*?d < 0" 
  1.2191 +
  1.2192 +    from dc one_plus_one_pos[where ?'a='a] have dc': "(1 + 1)*?c *?d < 0"
  1.2193 +      apply (simp add: mult_less_0_iff field_simps) 
  1.2194 +      apply (rule add_neg_neg)
  1.2195 +      apply (simp_all add: mult_less_0_iff)
  1.2196 +      done
  1.2197 +    hence c:"?c \<noteq> 0" and d: "?d\<noteq> 0" by auto
  1.2198 +    from add_frac_eq[OF c d, of "- ?t" "- ?s"]
  1.2199 +    have th: "(- ?t / ?c + - ?s / ?d)/(1 + 1) = - (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d)" 
  1.2200 +      by (simp add: ring_simps)
  1.2201 +    have "?rhs \<longleftrightarrow> Ifm vs (- (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d) # bs) (Lt (CNP 0 a r))" by (simp only: th)
  1.2202 +    also have "\<dots> \<longleftrightarrow> ?a * (- (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d)) + ?r < 0" 
  1.2203 +      by (simp add: r[of "(- (?d * ?t) + - (?c *?s)) / ((1 + 1) * ?c * ?d)"])
  1.2204 +
  1.2205 +    also have "\<dots> \<longleftrightarrow> ((1 + 1) * ?c * ?d) * (?a * (- (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d)) + ?r) > 0"
  1.2206 +      
  1.2207 +      using dc' order_less_not_sym[OF dc'] mult_less_cancel_left_disj[of "(1 + 1) * ?c * ?d" 0 "?a * (- (?d * ?t + ?c* ?s)/ ((1 + 1)*?c*?d)) + ?r"] by simp
  1.2208 +    also have "\<dots> \<longleftrightarrow> ?a * ((?d * ?t + ?c* ?s )) - (1 + 1)*?c*?d*?r < 0" 
  1.2209 +      using nonzero_mult_divide_cancel_left[of "(1 + 1)*?c*?d"] c d
  1.2210 +      by (simp add: ring_simps diff_divide_distrib del: left_distrib)
  1.2211 +    finally  have ?thesis using dc c d  nc nd
  1.2212 +      apply (simp add: r[of "(- (?d * ?t) + - (?c *?s)) / ((1 + 1) * ?c * ?d)"] msubstlt_def Let_def evaldjf_ex ring_simps lt polyneg_norm polymul_norm) 
  1.2213 +      apply (simp only: one_add_one_is_two[symmetric] of_int_add)
  1.2214 +      by (simp add: ring_simps order_less_not_sym[OF dc]) }
  1.2215 +  moreover
  1.2216 +  {assume c: "?c > 0" and d: "?d=0"  
  1.2217 +    from c have c'': "(1 + 1)*?c > 0" by (simp add: zero_less_mult_iff)
  1.2218 +    from c have c': "(1 + 1)*?c \<noteq> 0" by simp
  1.2219 +    from d have th: "(- ?t / ?c + - ?s / ?d)/(1 + 1) = - ?t / ((1 + 1)*?c)"  by (simp add: ring_simps)
  1.2220 +    have "?rhs \<longleftrightarrow> Ifm vs (- ?t / ((1 + 1)*?c) # bs) (Lt (CNP 0 a r))" by (simp only: th)
  1.2221 +    also have "\<dots> \<longleftrightarrow> ?a* (- ?t / ((1 + 1)*?c))+ ?r < 0" by (simp add: r[of "- (?t / ((1 + 1)*?c))"])
  1.2222 +    also have "\<dots> \<longleftrightarrow> (1 + 1)*?c * (?a* (- ?t / ((1 + 1)*?c))+ ?r) < 0"
  1.2223 +      using c mult_less_cancel_left_disj[of "(1 + 1) * ?c" "?a* (- ?t / ((1 + 1)*?c))+ ?r" 0] c' c'' order_less_not_sym[OF c''] by simp
  1.2224 +    also have "\<dots> \<longleftrightarrow> - ?a*?t+  (1 + 1)*?c *?r < 0" 
  1.2225 +      using nonzero_mult_divide_cancel_left[OF c'] c
  1.2226 +      by (simp add: ring_simps diff_divide_distrib less_le del: left_distrib)
  1.2227 +    finally have ?thesis using c d nc nd 
  1.2228 +      apply(simp add: r[of "- (?t / ((1 + 1)*?c))"] msubstlt_def Let_def evaldjf_ex ring_simps lt polyneg_norm polymul_norm)
  1.2229 +      apply (simp only: one_add_one_is_two[symmetric] of_int_add)
  1.2230 +      using c order_less_not_sym[OF c] less_imp_neq[OF c]
  1.2231 +      by (simp add: ring_simps )  }
  1.2232 +  moreover
  1.2233 +  {assume c: "?c < 0" and d: "?d=0"  hence c': "(1 + 1)*?c \<noteq> 0" by simp
  1.2234 +    from c have c'': "(1 + 1)*?c < 0" by (simp add: mult_less_0_iff)
  1.2235 +    from d have th: "(- ?t / ?c + - ?s / ?d)/(1 + 1) = - ?t / ((1 + 1)*?c)"  by (simp add: ring_simps)
  1.2236 +    have "?rhs \<longleftrightarrow> Ifm vs (- ?t / ((1 + 1)*?c) # bs) (Lt (CNP 0 a r))" by (simp only: th)
  1.2237 +    also have "\<dots> \<longleftrightarrow> ?a* (- ?t / ((1 + 1)*?c))+ ?r < 0" by (simp add: r[of "- (?t / ((1 + 1)*?c))"])
  1.2238 +    also have "\<dots> \<longleftrightarrow> (1 + 1)*?c * (?a* (- ?t / ((1 + 1)*?c))+ ?r) > 0"
  1.2239 +      using c order_less_not_sym[OF c''] less_imp_neq[OF c''] c'' mult_less_cancel_left_disj[of "(1 + 1) * ?c" 0 "?a* (- ?t / ((1 + 1)*?c))+ ?r"] by simp
  1.2240 +    also have "\<dots> \<longleftrightarrow> ?a*?t -  (1 + 1)*?c *?r < 0" 
  1.2241 +      using nonzero_mult_divide_cancel_left[OF c'] c order_less_not_sym[OF c''] less_imp_neq[OF c''] c''
  1.2242 +	by (simp add: ring_simps diff_divide_distrib del:  left_distrib)
  1.2243 +    finally have ?thesis using c d nc nd 
  1.2244 +      apply(simp add: r[of "- (?t / ((1 + 1)*?c))"] msubstlt_def Let_def evaldjf_ex ring_simps lt polyneg_norm polymul_norm)
  1.2245 +      apply (simp only: one_add_one_is_two[symmetric] of_int_add)
  1.2246 +      using c order_less_not_sym[OF c] less_imp_neq[OF c]
  1.2247 +      by (simp add: ring_simps )    }
  1.2248 +  moreover
  1.2249 +  moreover
  1.2250 +  {assume c: "?c = 0" and d: "?d>0"  
  1.2251 +    from d have d'': "(1 + 1)*?d > 0" by (simp add: zero_less_mult_iff)
  1.2252 +    from d have d': "(1 + 1)*?d \<noteq> 0" by simp
  1.2253 +    from c have th: "(- ?t / ?c + - ?s / ?d)/(1 + 1) = - ?s / ((1 + 1)*?d)"  by (simp add: ring_simps)
  1.2254 +    have "?rhs \<longleftrightarrow> Ifm vs (- ?s / ((1 + 1)*?d) # bs) (Lt (CNP 0 a r))" by (simp only: th)
  1.2255 +    also have "\<dots> \<longleftrightarrow> ?a* (- ?s / ((1 + 1)*?d))+ ?r < 0" by (simp add: r[of "- (?s / ((1 + 1)*?d))"])
  1.2256 +    also have "\<dots> \<longleftrightarrow> (1 + 1)*?d * (?a* (- ?s / ((1 + 1)*?d))+ ?r) < 0"
  1.2257 +      using d mult_less_cancel_left_disj[of "(1 + 1) * ?d" "?a* (- ?s / ((1 + 1)*?d))+ ?r" 0] d' d'' order_less_not_sym[OF d''] by simp
  1.2258 +    also have "\<dots> \<longleftrightarrow> - ?a*?s+  (1 + 1)*?d *?r < 0" 
  1.2259 +      using nonzero_mult_divide_cancel_left[OF d'] d
  1.2260 +      by (simp add: ring_simps diff_divide_distrib less_le del: left_distrib)
  1.2261 +    finally have ?thesis using c d nc nd 
  1.2262 +      apply(simp add: r[of "- (?s / ((1 + 1)*?d))"] msubstlt_def Let_def evaldjf_ex ring_simps lt polyneg_norm polymul_norm)
  1.2263 +      apply (simp only: one_add_one_is_two[symmetric] of_int_add)
  1.2264 +      using d order_less_not_sym[OF d] less_imp_neq[OF d]
  1.2265 +      by (simp add: ring_simps )  }
  1.2266 +  moreover
  1.2267 +  {assume c: "?c = 0" and d: "?d<0"  hence d': "(1 + 1)*?d \<noteq> 0" by simp
  1.2268 +    from d have d'': "(1 + 1)*?d < 0" by (simp add: mult_less_0_iff)
  1.2269 +    from c have th: "(- ?t / ?c + - ?s / ?d)/(1 + 1) = - ?s / ((1 + 1)*?d)"  by (simp add: ring_simps)
  1.2270 +    have "?rhs \<longleftrightarrow> Ifm vs (- ?s / ((1 + 1)*?d) # bs) (Lt (CNP 0 a r))" by (simp only: th)
  1.2271 +    also have "\<dots> \<longleftrightarrow> ?a* (- ?s / ((1 + 1)*?d))+ ?r < 0" by (simp add: r[of "- (?s / ((1 + 1)*?d))"])
  1.2272 +    also have "\<dots> \<longleftrightarrow> (1 + 1)*?d * (?a* (- ?s / ((1 + 1)*?d))+ ?r) > 0"
  1.2273 +      using d order_less_not_sym[OF d''] less_imp_neq[OF d''] d'' mult_less_cancel_left_disj[of "(1 + 1) * ?d" 0 "?a* (- ?s / ((1 + 1)*?d))+ ?r"] by simp
  1.2274 +    also have "\<dots> \<longleftrightarrow> ?a*?s -  (1 + 1)*?d *?r < 0" 
  1.2275 +      using nonzero_mult_divide_cancel_left[OF d'] d order_less_not_sym[OF d''] less_imp_neq[OF d''] d''
  1.2276 +	by (simp add: ring_simps diff_divide_distrib del:  left_distrib)
  1.2277 +    finally have ?thesis using c d nc nd 
  1.2278 +      apply(simp add: r[of "- (?s / ((1 + 1)*?d))"] msubstlt_def Let_def evaldjf_ex ring_simps lt polyneg_norm polymul_norm)
  1.2279 +      apply (simp only: one_add_one_is_two[symmetric] of_int_add)
  1.2280 +      using d order_less_not_sym[OF d] less_imp_neq[OF d]
  1.2281 +      by (simp add: ring_simps )    }
  1.2282 +ultimately show ?thesis by blast
  1.2283 +qed
  1.2284 +
  1.2285 +definition "msubstle c t d s a r = 
  1.2286 +  evaldjf (split conj) 
  1.2287 +  [(let cd = c *\<^sub>p d in (lt (CP (~\<^sub>p cd)), Le (Add (Mul (~\<^sub>p a) (Add (Mul d t) (Mul c s))) (Mul (2\<^sub>p *\<^sub>p cd) r)))),
  1.2288 +  (let cd = c *\<^sub>p d in (lt (CP cd), Le (Sub (Mul a (Add (Mul d t) (Mul c s))) (Mul (2\<^sub>p *\<^sub>p cd) r)))),
  1.2289 +   (conj (lt (CP (~\<^sub>p c))) (Eq (CP d)) , Le (Add (Mul (~\<^sub>p a) t) (Mul (2\<^sub>p *\<^sub>p c) r))),
  1.2290 +   (conj (lt (CP c)) (Eq (CP d)) , Le (Sub (Mul a t) (Mul (2\<^sub>p *\<^sub>p c) r))),
  1.2291 +   (conj (lt (CP (~\<^sub>p d))) (Eq (CP c)) , Le (Add (Mul (~\<^sub>p a) s) (Mul (2\<^sub>p *\<^sub>p d) r))),
  1.2292 +   (conj (lt (CP d)) (Eq (CP c)) , Le (Sub (Mul a s) (Mul (2\<^sub>p *\<^sub>p d) r))),
  1.2293 +   (conj (Eq (CP c)) (Eq (CP d)) , Le r)]"
  1.2294 +
  1.2295 +lemma msubstle_nb: assumes lp: "islin (Le (CNP 0 a r))" and t: "tmbound0 t" and s: "tmbound0 s"
  1.2296 +  shows "bound0 (msubstle c t d s a r)"
  1.2297 +proof-
  1.2298 +  have th: "\<forall>x\<in> set [(let cd = c *\<^sub>p d in (lt (CP (~\<^sub>p cd)), Le (Add (Mul (~\<^sub>p a) (Add (Mul d t) (Mul c s))) (Mul (2\<^sub>p *\<^sub>p cd) r)))),
  1.2299 +  (let cd = c *\<^sub>p d in (lt (CP cd), Le (Sub (Mul a (Add (Mul d t) (Mul c s))) (Mul (2\<^sub>p *\<^sub>p cd) r)))),
  1.2300 +   (conj (lt (CP (~\<^sub>p c))) (Eq (CP d)) , Le (Add (Mul (~\<^sub>p a) t) (Mul (2\<^sub>p *\<^sub>p c) r))),
  1.2301 +   (conj (lt (CP c)) (Eq (CP d)) , Le (Sub (Mul a t) (Mul (2\<^sub>p *\<^sub>p c) r))),
  1.2302 +   (conj (lt (CP (~\<^sub>p d))) (Eq (CP c)) , Le (Add (Mul (~\<^sub>p a) s) (Mul (2\<^sub>p *\<^sub>p d) r))),
  1.2303 +   (conj (lt (CP d)) (Eq (CP c)) , Le (Sub (Mul a s) (Mul (2\<^sub>p *\<^sub>p d) r))),
  1.2304 +   (conj (Eq (CP c)) (Eq (CP d)) , Le r)]. bound0 (split conj x)"
  1.2305 +    using lp by (simp add: Let_def t s lt_nb )
  1.2306 +  from evaldjf_bound0[OF th] show ?thesis by (simp add: msubstle_def)
  1.2307 +qed
  1.2308 +
  1.2309 +lemma msubstle: assumes nc: "isnpoly c" and nd: "isnpoly d" and lp: "islin (Le (CNP 0 a r))" 
  1.2310 +  shows "Ifm vs (x#bs) (msubstle c t d s a r) \<longleftrightarrow> 
  1.2311 +  Ifm vs (((- Itm vs (x#bs) t /  Ipoly vs c + - Itm vs (x#bs) s / Ipoly vs d) /(1 + 1))#bs) (Le (CNP 0 a r))" (is "?lhs = ?rhs")
  1.2312 +proof-
  1.2313 +  let ?Nt = "\<lambda>x t. Itm vs (x#bs) t"
  1.2314 +  let ?N = "\<lambda>p. Ipoly vs p"
  1.2315 +  let ?c = "?N c"
  1.2316 +  let ?d = "?N d"
  1.2317 +  let ?t = "?Nt x t"
  1.2318 +  let ?s = "?Nt x s"
  1.2319 +  let ?a = "?N a"
  1.2320 +  let ?r = "?Nt x r"
  1.2321 +  from lp have lin:"isnpoly a" "a \<noteq> 0\<^sub>p" "tmbound0 r" "allpolys isnpoly r" by simp_all
  1.2322 +  note r= tmbound0_I[OF lin(3), of vs _ bs x]
  1.2323 +  have cd_cs: "?c * ?d < 0 \<or> ?c * ?d > 0 \<or> (?c = 0 \<and> ?d = 0) \<or> (?c = 0 \<and> ?d < 0) \<or> (?c = 0 \<and> ?d > 0) \<or> (?c < 0 \<and> ?d = 0) \<or> (?c > 0 \<and> ?d = 0)" by auto
  1.2324 +  moreover
  1.2325 +  {assume c: "?c=0" and d: "?d=0"
  1.2326 +    hence ?thesis  using nc nd by (simp add: polyneg_norm polymul_norm lt r[of 0] msubstle_def Let_def evaldjf_ex)}
  1.2327 +  moreover
  1.2328 +  {assume dc: "?c*?d > 0" 
  1.2329 +    from mult_pos_pos[OF one_plus_one_pos dc] have dc': "(1 + 1)*?c *?d > 0" by simp
  1.2330 +    hence c:"?c \<noteq> 0" and d: "?d\<noteq> 0" by auto
  1.2331 +    from dc' have dc'': "\<not> (1 + 1)*?c *?d < 0" by simp
  1.2332 +    from add_frac_eq[OF c d, of "- ?t" "- ?s"]
  1.2333 +    have th: "(- ?t / ?c + - ?s / ?d)/(1 + 1) = - (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d)" 
  1.2334 +      by (simp add: ring_simps)
  1.2335 +    have "?rhs \<longleftrightarrow> Ifm vs (- (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d) # bs) (Le (CNP 0 a r))" by (simp only: th)
  1.2336 +    also have "\<dots> \<longleftrightarrow> ?a * (- (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d)) + ?r <= 0" 
  1.2337 +      by (simp add: r[of "(- (?d * ?t) + - (?c *?s)) / ((1 + 1) * ?c * ?d)"])
  1.2338 +    also have "\<dots> \<longleftrightarrow> ((1 + 1) * ?c * ?d) * (?a * (- (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d)) + ?r) <= 0"
  1.2339 +      
  1.2340 +      using dc' dc'' mult_le_cancel_left[of "(1 + 1) * ?c * ?d" "?a * (- (?d * ?t + ?c* ?s)/ ((1 + 1)*?c*?d)) + ?r" 0] by simp
  1.2341 +    also have "\<dots> \<longleftrightarrow> ?a * (- (?d * ?t + ?c* ?s )) + (1 + 1)*?c*?d*?r <= 0" 
  1.2342 +      using nonzero_mult_divide_cancel_left[of "(1 + 1)*?c*?d"] c d
  1.2343 +      by (simp add: ring_simps diff_divide_distrib del: left_distrib)
  1.2344 +    finally  have ?thesis using dc c d  nc nd dc'
  1.2345 +      apply (simp add: r[of "(- (?d * ?t) + - (?c *?s)) / ((1 + 1) * ?c * ?d)"] msubstle_def Let_def evaldjf_ex ring_simps lt polyneg_norm polymul_norm) 
  1.2346 +    apply (simp only: one_add_one_is_two[symmetric] of_int_add)
  1.2347 +    by (simp add: ring_simps order_less_not_sym[OF dc])}
  1.2348 +  moreover
  1.2349 +  {assume dc: "?c*?d < 0" 
  1.2350 +
  1.2351 +    from dc one_plus_one_pos[where ?'a='a] have dc': "(1 + 1)*?c *?d < 0"
  1.2352 +      by (simp add: mult_less_0_iff field_simps add_neg_neg add_pos_pos)
  1.2353 +    hence c:"?c \<noteq> 0" and d: "?d\<noteq> 0" by auto
  1.2354 +    from add_frac_eq[OF c d, of "- ?t" "- ?s"]
  1.2355 +    have th: "(- ?t / ?c + - ?s / ?d)/(1 + 1) = - (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d)" 
  1.2356 +      by (simp add: ring_simps)
  1.2357 +    have "?rhs \<longleftrightarrow> Ifm vs (- (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d) # bs) (Le (CNP 0 a r))" by (simp only: th)
  1.2358 +    also have "\<dots> \<longleftrightarrow> ?a * (- (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d)) + ?r <= 0" 
  1.2359 +      by (simp add: r[of "(- (?d * ?t) + - (?c *?s)) / ((1 + 1) * ?c * ?d)"])
  1.2360 +
  1.2361 +    also have "\<dots> \<longleftrightarrow> ((1 + 1) * ?c * ?d) * (?a * (- (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d)) + ?r) >= 0"
  1.2362 +      
  1.2363 +      using dc' order_less_not_sym[OF dc'] mult_le_cancel_left[of "(1 + 1) * ?c * ?d" 0 "?a * (- (?d * ?t + ?c* ?s)/ ((1 + 1)*?c*?d)) + ?r"] by simp
  1.2364 +    also have "\<dots> \<longleftrightarrow> ?a * ((?d * ?t + ?c* ?s )) - (1 + 1)*?c*?d*?r <= 0" 
  1.2365 +      using nonzero_mult_divide_cancel_left[of "(1 + 1)*?c*?d"] c d
  1.2366 +      by (simp add: ring_simps diff_divide_distrib del: left_distrib)
  1.2367 +    finally  have ?thesis using dc c d  nc nd
  1.2368 +      apply (simp add: r[of "(- (?d * ?t) + - (?c *?s)) / ((1 + 1) * ?c * ?d)"] msubstle_def Let_def evaldjf_ex ring_simps lt polyneg_norm polymul_norm) 
  1.2369 +      apply (simp only: one_add_one_is_two[symmetric] of_int_add)
  1.2370 +      by (simp add: ring_simps order_less_not_sym[OF dc]) }
  1.2371 +  moreover
  1.2372 +  {assume c: "?c > 0" and d: "?d=0"  
  1.2373 +    from c have c'': "(1 + 1)*?c > 0" by (simp add: zero_less_mult_iff)
  1.2374 +    from c have c': "(1 + 1)*?c \<noteq> 0" by simp
  1.2375 +    from d have th: "(- ?t / ?c + - ?s / ?d)/(1 + 1) = - ?t / ((1 + 1)*?c)"  by (simp add: ring_simps)
  1.2376 +    have "?rhs \<longleftrightarrow> Ifm vs (- ?t / ((1 + 1)*?c) # bs) (Le (CNP 0 a r))" by (simp only: th)
  1.2377 +    also have "\<dots> \<longleftrightarrow> ?a* (- ?t / ((1 + 1)*?c))+ ?r <= 0" by (simp add: r[of "- (?t / ((1 + 1)*?c))"])
  1.2378 +    also have "\<dots> \<longleftrightarrow> (1 + 1)*?c * (?a* (- ?t / ((1 + 1)*?c))+ ?r) <= 0"
  1.2379 +      using c mult_le_cancel_left[of "(1 + 1) * ?c" "?a* (- ?t / ((1 + 1)*?c))+ ?r" 0] c' c'' order_less_not_sym[OF c''] by simp
  1.2380 +    also have "\<dots> \<longleftrightarrow> - ?a*?t+  (1 + 1)*?c *?r <= 0" 
  1.2381 +      using nonzero_mult_divide_cancel_left[OF c'] c
  1.2382 +      by (simp add: ring_simps diff_divide_distrib less_le del: left_distrib)
  1.2383 +    finally have ?thesis using c d nc nd 
  1.2384 +      apply(simp add: r[of "- (?t / ((1 + 1)*?c))"] msubstle_def Let_def evaldjf_ex ring_simps lt polyneg_norm polymul_norm)
  1.2385 +      apply (simp only: one_add_one_is_two[symmetric] of_int_add)
  1.2386 +      using c order_less_not_sym[OF c] less_imp_neq[OF c]
  1.2387 +      by (simp add: ring_simps )  }
  1.2388 +  moreover
  1.2389 +  {assume c: "?c < 0" and d: "?d=0"  hence c': "(1 + 1)*?c \<noteq> 0" by simp
  1.2390 +    from c have c'': "(1 + 1)*?c < 0" by (simp add: mult_less_0_iff)
  1.2391 +    from d have th: "(- ?t / ?c + - ?s / ?d)/(1 + 1) = - ?t / ((1 + 1)*?c)"  by (simp add: ring_simps)
  1.2392 +    have "?rhs \<longleftrightarrow> Ifm vs (- ?t / ((1 + 1)*?c) # bs) (Le (CNP 0 a r))" by (simp only: th)
  1.2393 +    also have "\<dots> \<longleftrightarrow> ?a* (- ?t / ((1 + 1)*?c))+ ?r <= 0" by (simp add: r[of "- (?t / ((1 + 1)*?c))"])
  1.2394 +    also have "\<dots> \<longleftrightarrow> (1 + 1)*?c * (?a* (- ?t / ((1 + 1)*?c))+ ?r) >= 0"
  1.2395 +      using c order_less_not_sym[OF c''] less_imp_neq[OF c''] c'' mult_le_cancel_left[of "(1 + 1) * ?c" 0 "?a* (- ?t / ((1 + 1)*?c))+ ?r"] by simp
  1.2396 +    also have "\<dots> \<longleftrightarrow> ?a*?t -  (1 + 1)*?c *?r <= 0" 
  1.2397 +      using nonzero_mult_divide_cancel_left[OF c'] c order_less_not_sym[OF c''] less_imp_neq[OF c''] c''
  1.2398 +	by (simp add: ring_simps diff_divide_distrib del:  left_distrib)
  1.2399 +    finally have ?thesis using c d nc nd 
  1.2400 +      apply(simp add: r[of "- (?t / ((1 + 1)*?c))"] msubstle_def Let_def evaldjf_ex ring_simps lt polyneg_norm polymul_norm)
  1.2401 +      apply (simp only: one_add_one_is_two[symmetric] of_int_add)
  1.2402 +      using c order_less_not_sym[OF c] less_imp_neq[OF c]
  1.2403 +      by (simp add: ring_simps )    }
  1.2404 +  moreover
  1.2405 +  moreover
  1.2406 +  {assume c: "?c = 0" and d: "?d>0"  
  1.2407 +    from d have d'': "(1 + 1)*?d > 0" by (simp add: zero_less_mult_iff)
  1.2408 +    from d have d': "(1 + 1)*?d \<noteq> 0" by simp
  1.2409 +    from c have th: "(- ?t / ?c + - ?s / ?d)/(1 + 1) = - ?s / ((1 + 1)*?d)"  by (simp add: ring_simps)
  1.2410 +    have "?rhs \<longleftrightarrow> Ifm vs (- ?s / ((1 + 1)*?d) # bs) (Le (CNP 0 a r))" by (simp only: th)
  1.2411 +    also have "\<dots> \<longleftrightarrow> ?a* (- ?s / ((1 + 1)*?d))+ ?r <= 0" by (simp add: r[of "- (?s / ((1 + 1)*?d))"])
  1.2412 +    also have "\<dots> \<longleftrightarrow> (1 + 1)*?d * (?a* (- ?s / ((1 + 1)*?d))+ ?r) <= 0"
  1.2413 +      using d mult_le_cancel_left[of "(1 + 1) * ?d" "?a* (- ?s / ((1 + 1)*?d))+ ?r" 0] d' d'' order_less_not_sym[OF d''] by simp
  1.2414 +    also have "\<dots> \<longleftrightarrow> - ?a*?s+  (1 + 1)*?d *?r <= 0" 
  1.2415 +      using nonzero_mult_divide_cancel_left[OF d'] d
  1.2416 +      by (simp add: ring_simps diff_divide_distrib less_le del: left_distrib)
  1.2417 +    finally have ?thesis using c d nc nd 
  1.2418 +      apply(simp add: r[of "- (?s / ((1 + 1)*?d))"] msubstle_def Let_def evaldjf_ex ring_simps lt polyneg_norm polymul_norm)
  1.2419 +      apply (simp only: one_add_one_is_two[symmetric] of_int_add)
  1.2420 +      using d order_less_not_sym[OF d] less_imp_neq[OF d]
  1.2421 +      by (simp add: ring_simps )  }
  1.2422 +  moreover
  1.2423 +  {assume c: "?c = 0" and d: "?d<0"  hence d': "(1 + 1)*?d \<noteq> 0" by simp
  1.2424 +    from d have d'': "(1 + 1)*?d < 0" by (simp add: mult_less_0_iff)
  1.2425 +    from c have th: "(- ?t / ?c + - ?s / ?d)/(1 + 1) = - ?s / ((1 + 1)*?d)"  by (simp add: ring_simps)
  1.2426 +    have "?rhs \<longleftrightarrow> Ifm vs (- ?s / ((1 + 1)*?d) # bs) (Le (CNP 0 a r))" by (simp only: th)
  1.2427 +    also have "\<dots> \<longleftrightarrow> ?a* (- ?s / ((1 + 1)*?d))+ ?r <= 0" by (simp add: r[of "- (?s / ((1 + 1)*?d))"])
  1.2428 +    also have "\<dots> \<longleftrightarrow> (1 + 1)*?d * (?a* (- ?s / ((1 + 1)*?d))+ ?r) >= 0"
  1.2429 +      using d order_less_not_sym[OF d''] less_imp_neq[OF d''] d'' mult_le_cancel_left[of "(1 + 1) * ?d" 0 "?a* (- ?s / ((1 + 1)*?d))+ ?r"] by simp
  1.2430 +    also have "\<dots> \<longleftrightarrow> ?a*?s -  (1 + 1)*?d *?r <= 0" 
  1.2431 +      using nonzero_mult_divide_cancel_left[OF d'] d order_less_not_sym[OF d''] less_imp_neq[OF d''] d''
  1.2432 +	by (simp add: ring_simps diff_divide_distrib del:  left_distrib)
  1.2433 +    finally have ?thesis using c d nc nd 
  1.2434 +      apply(simp add: r[of "- (?s / ((1 + 1)*?d))"] msubstle_def Let_def evaldjf_ex ring_simps lt polyneg_norm polymul_norm)
  1.2435 +      apply (simp only: one_add_one_is_two[symmetric] of_int_add)
  1.2436 +      using d order_less_not_sym[OF d] less_imp_neq[OF d]
  1.2437 +      by (simp add: ring_simps )    }
  1.2438 +ultimately show ?thesis by blast
  1.2439 +qed
  1.2440 +
  1.2441 +
  1.2442 +fun msubst :: "fm \<Rightarrow> (poly \<times> tm) \<times> (poly \<times> tm) \<Rightarrow> fm" where
  1.2443 +  "msubst (And p q) ((c,t), (d,s)) = conj (msubst p ((c,t),(d,s))) (msubst q ((c,t),(d,s)))"
  1.2444 +| "msubst (Or p q) ((c,t), (d,s)) = disj (msubst p ((c,t),(d,s))) (msubst q ((c,t), (d,s)))"
  1.2445 +| "msubst (Eq (CNP 0 a r)) ((c,t),(d,s)) = msubsteq c t d s a r"
  1.2446 +| "msubst (NEq (CNP 0 a r)) ((c,t),(d,s)) = msubstneq c t d s a r"
  1.2447 +| "msubst (Lt (CNP 0 a r)) ((c,t),(d,s)) = msubstlt c t d s a r"
  1.2448 +| "msubst (Le (CNP 0 a r)) ((c,t),(d,s)) = msubstle c t d s a r"
  1.2449 +| "msubst p ((c,t),(d,s)) = p"
  1.2450 +
  1.2451 +lemma msubst_I: assumes lp: "islin p" and nc: "isnpoly c" and nd: "isnpoly d"
  1.2452 +  shows "Ifm vs (x#bs) (msubst p ((c,t),(d,s))) = Ifm vs (((- Itm vs (x#bs) t /  Ipoly vs c + - Itm vs (x#bs) s / Ipoly vs d) /(1 + 1))#bs) p"
  1.2453 +  using lp
  1.2454 +by (induct p rule: islin.induct, auto simp add: tmbound0_I[where b="(- (Itm vs (x # bs) t / \<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup>) + - (Itm vs (x # bs) s / \<lparr>d\<rparr>\<^sub>p\<^bsup>vs\<^esup>)) /(1 + 1)" and b'=x and bs = bs and vs=vs] bound0_I[where b="(- (Itm vs (x # bs) t / \<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup>) + - (Itm vs (x # bs) s / \<lparr>d\<rparr>\<^sub>p\<^bsup>vs\<^esup>)) /(1 + 1)" and b'=x and bs = bs and vs=vs] msubsteq msubstneq msubstlt[OF nc nd] msubstle[OF nc nd])
  1.2455 +
  1.2456 +lemma msubst_nb: assumes lp: "islin p" and t: "tmbound0 t" and s: "tmbound0 s"
  1.2457 +  shows "bound0 (msubst p ((c,t),(d,s)))"
  1.2458 +  using lp t s
  1.2459 +  by (induct p rule: islin.induct, auto simp add: msubsteq_nb msubstneq_nb msubstlt_nb msubstle_nb)
  1.2460 +
  1.2461 +lemma fr_eq_msubst: 
  1.2462 +  assumes lp: "islin p"
  1.2463 +  shows "(\<exists> x. Ifm vs (x#bs) p) = ((Ifm vs (x#bs) (minusinf p)) \<or> (Ifm vs (x#bs) (plusinf p)) \<or> (\<exists> (c,t) \<in> set (uset p). \<exists> (d,s) \<in> set (uset p). Ifm vs (x#bs) (msubst p ((c,t),(d,s)))))"
  1.2464 +  (is "(\<exists> x. ?I x p) = (?M \<or> ?P \<or> ?F)" is "?E = ?D")
  1.2465 +proof-
  1.2466 +from uset_l[OF lp] have th: "\<forall>(c, s)\<in>set (uset p). isnpoly c \<and> tmbound0 s" by blast
  1.2467 +{fix c t d s assume ctU: "(c,t) \<in>set (uset p)" and dsU: "(d,s) \<in>set (uset p)" 
  1.2468 +  and pts: "Ifm vs ((- Itm vs (x # bs) t / \<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup> + - Itm vs (x # bs) s / \<lparr>d\<rparr>\<^sub>p\<^bsup>vs\<^esup>) / (1+1) # bs) p"
  1.2469 +  from th[rule_format, OF ctU] th[rule_format, OF dsU] have norm:"isnpoly c" "isnpoly d" by simp_all
  1.2470 +  from msubst_I[OF lp norm, of vs x bs t s] pts
  1.2471 +  have "Ifm vs (x # bs) (msubst p ((c, t), d, s))" ..}
  1.2472 +moreover
  1.2473 +{fix c t d s assume ctU: "(c,t) \<in>set (uset p)" and dsU: "(d,s) \<in>set (uset p)" 
  1.2474 +  and pts: "Ifm vs (x # bs) (msubst p ((c, t), d, s))"
  1.2475 +  from th[rule_format, OF ctU] th[rule_format, OF dsU] have norm:"isnpoly c" "isnpoly d" by simp_all
  1.2476 +  from msubst_I[OF lp norm, of vs x bs t s] pts
  1.2477 +  have "Ifm vs ((- Itm vs (x # bs) t / \<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup> + - Itm vs (x # bs) s / \<lparr>d\<rparr>\<^sub>p\<^bsup>vs\<^esup>) / (1+1) # bs) p" ..}
  1.2478 +ultimately have th': "(\<exists> (c,t) \<in> set (uset p). \<exists> (d,s) \<in> set (uset p). Ifm vs ((- Itm vs (x # bs) t / \<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup> + - Itm vs (x # bs) s / \<lparr>d\<rparr>\<^sub>p\<^bsup>vs\<^esup>) / (1+1) # bs) p) \<longleftrightarrow> ?F" by blast
  1.2479 +from fr_eq[OF lp, of vs bs x, simplified th'] show ?thesis .
  1.2480 +qed 
  1.2481 +
  1.2482 +text {* Rest of the implementation *}
  1.2483 +
  1.2484 +consts alluopairs:: "'a list \<Rightarrow> ('a \<times> 'a) list"
  1.2485 +primrec
  1.2486 +  "alluopairs [] = []"
  1.2487 +  "alluopairs (x#xs) = (map (Pair x) (x#xs))@(alluopairs xs)"
  1.2488 +
  1.2489 +lemma alluopairs_set1: "set (alluopairs xs) \<le> {(x,y). x\<in> set xs \<and> y\<in> set xs}"
  1.2490 +by (induct xs, auto)
  1.2491 +
  1.2492 +lemma alluopairs_set:
  1.2493 +  "\<lbrakk>x\<in> set xs ; y \<in> set xs\<rbrakk> \<Longrightarrow> (x,y) \<in> set (alluopairs xs) \<or> (y,x) \<in> set (alluopairs xs) "
  1.2494 +by (induct xs, auto)
  1.2495 +
  1.2496 +lemma alluopairs_ex:
  1.2497 +  assumes Pc: "\<forall> x \<in> set xs. \<forall>y\<in> set xs. P x y = P y x"
  1.2498 +  shows "(\<exists> x \<in> set xs. \<exists> y \<in> set xs. P x y) = (\<exists> (x,y) \<in> set (alluopairs xs). P x y)"
  1.2499 +proof
  1.2500 +  assume "\<exists>x\<in>set xs. \<exists>y\<in>set xs. P x y"
  1.2501 +  then obtain x y where x: "x \<in> set xs" and y:"y \<in> set xs" and P: "P x y"  by blast
  1.2502 +  from alluopairs_set[OF x y] P Pc x y show"\<exists>(x, y)\<in>set (alluopairs xs). P x y" 
  1.2503 +    by auto
  1.2504 +next
  1.2505 +  assume "\<exists>(x, y)\<in>set (alluopairs xs). P x y"
  1.2506 +  then obtain "x" and "y"  where xy:"(x,y) \<in> set (alluopairs xs)" and P: "P x y" by blast+
  1.2507 +  from xy have "x \<in> set xs \<and> y\<in> set xs" using alluopairs_set1 by blast
  1.2508 +  with P show "\<exists>x\<in>set xs. \<exists>y\<in>set xs. P x y" by blast
  1.2509 +qed
  1.2510 +
  1.2511 +lemma nth_pos2: "0 < n \<Longrightarrow> (x#xs) ! n = xs ! (n - 1)"
  1.2512 +using Nat.gr0_conv_Suc
  1.2513 +by clarsimp
  1.2514 +
  1.2515 +lemma filter_length: "length (List.filter P xs) < Suc (length xs)"
  1.2516 +  apply (induct xs, auto) done
  1.2517 +
  1.2518 +consts remdps:: "'a list \<Rightarrow> 'a list"
  1.2519 +
  1.2520 +recdef remdps "measure size"
  1.2521 +  "remdps [] = []"
  1.2522 +  "remdps (x#xs) = (x#(remdps (List.filter (\<lambda> y. y \<noteq> x) xs)))"
  1.2523 +(hints simp add: filter_length[rule_format])
  1.2524 +
  1.2525 +lemma remdps_set[simp]: "set (remdps xs) = set xs"
  1.2526 +  by (induct xs rule: remdps.induct, auto)
  1.2527 +
  1.2528 +lemma simpfm_lin:   assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})"
  1.2529 +  shows "qfree p \<Longrightarrow> islin (simpfm p)"
  1.2530 +  by (induct p rule: simpfm.induct, auto simp add: conj_lin disj_lin)
  1.2531 +
  1.2532 +definition 
  1.2533 +  "ferrack p \<equiv> let q = simpfm p ; mp = minusinf q ; pp = plusinf q
  1.2534 +  in if (mp = T \<or> pp = T) then T 
  1.2535 +     else (let U = alluopairs (remdps (uset  q))
  1.2536 +           in decr0 (disj mp (disj pp (evaldjf (simpfm o (msubst q)) U ))))"
  1.2537 +
  1.2538 +lemma ferrack: 
  1.2539 +  assumes qf: "qfree p"
  1.2540 +  shows "qfree (ferrack p) \<and> ((Ifm vs bs (ferrack p)) = (Ifm vs bs (E p)))"
  1.2541 +  (is "_ \<and> (?rhs = ?lhs)")
  1.2542 +proof-
  1.2543 +  let ?I = "\<lambda> x p. Ifm vs (x#bs) p"
  1.2544 +  let ?N = "\<lambda> t. Ipoly vs t"
  1.2545 +  let ?Nt = "\<lambda>x t. Itm vs (x#bs) t"
  1.2546 +  let ?q = "simpfm p" 
  1.2547 +  let ?U = "remdps(uset ?q)"
  1.2548 +  let ?Up = "alluopairs ?U"
  1.2549 +  let ?mp = "minusinf ?q"
  1.2550 +  let ?pp = "plusinf ?q"
  1.2551 +  let ?I = "\<lambda>p. Ifm vs (x#bs) p"
  1.2552 +  from simpfm_lin[OF qf] simpfm_qf[OF qf] have lq: "islin ?q" and q_qf: "qfree ?q" .
  1.2553 +  from minusinf_nb[OF lq] plusinf_nb[OF lq] have mp_nb: "bound0 ?mp" and pp_nb: "bound0 ?pp" .
  1.2554 +  from bound0_qf[OF mp_nb] bound0_qf[OF pp_nb] have mp_qf: "qfree ?mp" and pp_qf: "qfree ?pp" .
  1.2555 +  from uset_l[OF lq] have U_l: "\<forall>(c, s)\<in>set ?U. isnpoly c \<and> c \<noteq> 0\<^sub>p \<and> tmbound0 s \<and> allpolys isnpoly s"
  1.2556 +    by simp
  1.2557 +  {fix c t d s assume ctU: "(c,t) \<in> set ?U" and dsU: "(d,s) \<in> set ?U"
  1.2558 +    from U_l ctU dsU have norm: "isnpoly c" "isnpoly d" by auto
  1.2559 +    from msubst_I[OF lq norm, of vs x bs t s] msubst_I[OF lq norm(2,1), of vs x bs s t]
  1.2560 +    have "?I (msubst ?q ((c,t),(d,s))) = ?I (msubst ?q ((d,s),(c,t)))" by (simp add: ring_simps)}
  1.2561 +  hence th0: "\<forall>x \<in> set ?U. \<forall>y \<in> set ?U. ?I (msubst ?q (x, y)) \<longleftrightarrow> ?I (msubst ?q (y, x))" by clarsimp
  1.2562 +  {fix x assume xUp: "x \<in> set ?Up" 
  1.2563 +    then  obtain c t d s where ctU: "(c,t) \<in> set ?U" and dsU: "(d,s) \<in> set ?U" 
  1.2564 +      and x: "x = ((c,t),(d,s))" using alluopairs_set1[of ?U] by auto  
  1.2565 +    from U_l[rule_format, OF ctU] U_l[rule_format, OF dsU] 
  1.2566 +    have nbs: "tmbound0 t" "tmbound0 s" by simp_all
  1.2567 +    from simpfm_bound0[OF msubst_nb[OF lq nbs, of c d]] 
  1.2568 +    have "bound0 ((simpfm o (msubst (simpfm p))) x)" using x by simp}
  1.2569 +  with evaldjf_bound0[of ?Up "(simpfm o (msubst (simpfm p)))"]
  1.2570 +  have "bound0 (evaldjf (simpfm o (msubst (simpfm p))) ?Up)" by blast
  1.2571 +  with mp_nb pp_nb 
  1.2572 +  have th1: "bound0 (disj ?mp (disj ?pp (evaldjf (simpfm o (msubst ?q)) ?Up )))" by (simp add: disj_nb)
  1.2573 +  from decr0_qf[OF th1] have thqf: "qfree (ferrack p)" by (simp add: ferrack_def Let_def)
  1.2574 +  have "?lhs \<longleftrightarrow> (\<exists>x. Ifm vs (x#bs) ?q)" by simp
  1.2575 +  also have "\<dots> \<longleftrightarrow> ?I ?mp \<or> ?I ?pp \<or> (\<exists>(c, t)\<in>set ?U. \<exists>(d, s)\<in>set ?U. ?I (msubst (simpfm p) ((c, t), d, s)))" using fr_eq_msubst[OF lq, of vs bs x] by simp
  1.2576 +  also have "\<dots> \<longleftrightarrow> ?I ?mp \<or> ?I ?pp \<or> (\<exists> (x,y) \<in> set ?Up. ?I ((simpfm o (msubst ?q)) (x,y)))" using alluopairs_ex[OF th0] by simp
  1.2577 +  also have "\<dots> \<longleftrightarrow> ?I ?mp \<or> ?I ?pp \<or> ?I (evaldjf (simpfm o (msubst ?q)) ?Up)" 
  1.2578 +    by (simp add: evaldjf_ex)
  1.2579 +  also have "\<dots> \<longleftrightarrow> ?I (disj ?mp (disj ?pp (evaldjf (simpfm o (msubst ?q)) ?Up)))" by simp
  1.2580 +  also have "\<dots> \<longleftrightarrow> ?rhs" using decr0[OF th1, of vs x bs]
  1.2581 +    apply (simp add: ferrack_def Let_def)
  1.2582 +    by (cases "?mp = T \<or> ?pp = T", auto)
  1.2583 +  finally show ?thesis using thqf by blast
  1.2584 +qed
  1.2585 +
  1.2586 +definition "frpar p = simpfm (qelim p ferrack)"
  1.2587 +lemma frpar: "qfree (frpar p) \<and> (Ifm vs bs (frpar p) \<longleftrightarrow> Ifm vs bs p)"
  1.2588 +proof-
  1.2589 +  from ferrack have th: "\<forall>bs p. qfree p \<longrightarrow> qfree (ferrack p) \<and> Ifm vs bs (ferrack p) = Ifm vs bs (E p)" by blast
  1.2590 +  from qelim[OF th, of p bs] show ?thesis  unfolding frpar_def by auto
  1.2591 +qed
  1.2592 +
  1.2593 +declare polyadd.simps[code]
  1.2594 +lemma [simp,code]: "polyadd (CN c n p, CN c' n' p') = 
  1.2595 +    (if n < n' then CN (polyadd(c,CN c' n' p')) n p
  1.2596 +     else if n'<n then CN (polyadd(CN c n p, c')) n' p'
  1.2597 +     else (let cc' = polyadd (c,c') ; 
  1.2598 +               pp' = polyadd (p,p')
  1.2599 +           in (if pp' = 0\<^sub>p then cc' else CN cc' n pp')))"
  1.2600 +  by (simp add: Let_def stupid)
  1.2601 +
  1.2602 +
  1.2603 +
  1.2604 +(*
  1.2605 +lemmas [code func] = polysub_def
  1.2606 +lemmas [code func del] = Zero_nat_def
  1.2607 +code_gen  "frpar" in SML to FRParTest
  1.2608 +*)
  1.2609 +
  1.2610 +section{* Second implemenation: Case splits not local *}
  1.2611 +
  1.2612 +lemma fr_eq2:  assumes lp: "islin p"
  1.2613 +  shows "(\<exists> x. Ifm vs (x#bs) p) \<longleftrightarrow> 
  1.2614 +   ((Ifm vs (x#bs) (minusinf p)) \<or> (Ifm vs (x#bs) (plusinf p)) \<or> 
  1.2615 +    (Ifm vs (0#bs) p) \<or> 
  1.2616 +    (\<exists> (n,t) \<in> set (uset p). Ipoly vs n \<noteq> 0 \<and> Ifm vs ((- Itm vs (x#bs) t /  (Ipoly vs n * (1 + 1)))#bs) p) \<or> 
  1.2617 +    (\<exists> (n,t) \<in> set (uset p). \<exists> (m,s) \<in> set (uset p). Ipoly vs n \<noteq> 0 \<and> Ipoly vs m \<noteq> 0 \<and> Ifm vs (((- Itm vs (x#bs) t /  Ipoly vs n + - Itm vs (x#bs) s / Ipoly vs m) /(1 + 1))#bs) p))"
  1.2618 +  (is "(\<exists> x. ?I x p) = (?M \<or> ?P \<or> ?Z \<or> ?U \<or> ?F)" is "?E = ?D")
  1.2619 +proof
  1.2620 +  assume px: "\<exists> x. ?I x p"
  1.2621 +  have "?M \<or> ?P \<or> (\<not> ?M \<and> \<not> ?P)" by blast
  1.2622 +  moreover {assume "?M \<or> ?P" hence "?D" by blast}
  1.2623 +  moreover {assume nmi: "\<not> ?M" and npi: "\<not> ?P"
  1.2624 +    from inf_uset[OF lp nmi npi, OF px] 
  1.2625 +    obtain c t d s where ct: "(c,t) \<in> set (uset p)" "(d,s) \<in> set (uset p)" "?I ((- Itm vs (x # bs) t / \<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup> + - Itm vs (x # bs) s / \<lparr>d\<rparr>\<^sub>p\<^bsup>vs\<^esup>) / ((1\<Colon>'a) + (1\<Colon>'a))) p"
  1.2626 +      by auto
  1.2627 +    let ?c = "\<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup>"
  1.2628 +    let ?d = "\<lparr>d\<rparr>\<^sub>p\<^bsup>vs\<^esup>"
  1.2629 +    let ?s = "Itm vs (x # bs) s"
  1.2630 +    let ?t = "Itm vs (x # bs) t"
  1.2631 +    have eq2: "\<And>(x::'a). x + x = (1 + 1) * x"
  1.2632 +      by  (simp add: ring_simps)
  1.2633 +    {assume "?c = 0 \<and> ?d = 0"
  1.2634 +      with ct have ?D by simp}
  1.2635 +    moreover
  1.2636 +    {assume z: "?c = 0" "?d \<noteq> 0"
  1.2637 +      from z have ?D using ct by auto}
  1.2638 +    moreover
  1.2639 +    {assume z: "?c \<noteq> 0" "?d = 0"
  1.2640 +      with ct have ?D by auto }
  1.2641 +    moreover
  1.2642 +    {assume z: "?c \<noteq> 0" "?d \<noteq> 0"
  1.2643 +      from z have ?F using ct
  1.2644 +	apply - apply (rule bexI[where x = "(c,t)"], simp_all)
  1.2645 +	by (rule bexI[where x = "(d,s)"], simp_all)
  1.2646 +      hence ?D by blast}
  1.2647 +    ultimately have ?D by auto}
  1.2648 +  ultimately show "?D" by blast
  1.2649 +next
  1.2650 +  assume "?D" 
  1.2651 +  moreover {assume m:"?M" from minusinf_ex[OF lp m] have "?E" .}
  1.2652 +  moreover {assume p: "?P" from plusinf_ex[OF lp p] have "?E" . }
  1.2653 +  moreover {assume f:"?F" hence "?E" by blast}
  1.2654 +  ultimately show "?E" by blast
  1.2655 +qed
  1.2656 +
  1.2657 +definition "msubsteq2 c t a b = Eq (Add (Mul a t) (Mul c b))"
  1.2658 +definition "msubstltpos c t a b = Lt (Add (Mul a t) (Mul c b))"
  1.2659 +definition "msubstlepos c t a b = Le (Add (Mul a t) (Mul c b))"
  1.2660 +definition "msubstltneg c t a b = Lt (Neg (Add (Mul a t) (Mul c b)))"
  1.2661 +definition "msubstleneg c t a b = Le (Neg (Add (Mul a t) (Mul c b)))"
  1.2662 +
  1.2663 +lemma msubsteq2: 
  1.2664 +  assumes nz: "Ipoly vs c \<noteq> 0" and l: "islin (Eq (CNP 0 a b))"
  1.2665 +  shows "Ifm vs (x#bs) (msubsteq2 c t a b) = Ifm vs (((Itm vs (x#bs) t /  Ipoly vs c ))#bs) (Eq (CNP 0 a b))" (is "?lhs = ?rhs")
  1.2666 +  using nz l tmbound0_I[of b vs x bs "Itm vs (x # bs) t / \<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup>" , symmetric]
  1.2667 +  by (simp add: msubsteq2_def field_simps)
  1.2668 +
  1.2669 +lemma msubstltpos: 
  1.2670 +  assumes nz: "Ipoly vs c > 0" and l: "islin (Lt (CNP 0 a b))"
  1.2671 +  shows "Ifm vs (x#bs) (msubstltpos c t a b) = Ifm vs (((Itm vs (x#bs) t /  Ipoly vs c ))#bs) (Lt (CNP 0 a b))" (is "?lhs = ?rhs")
  1.2672 +  using nz l tmbound0_I[of b vs x bs "Itm vs (x # bs) t / \<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup>" , symmetric]
  1.2673 +  by (simp add: msubstltpos_def field_simps)
  1.2674 +
  1.2675 +lemma msubstlepos: 
  1.2676 +  assumes nz: "Ipoly vs c > 0" and l: "islin (Le (CNP 0 a b))"
  1.2677 +  shows "Ifm vs (x#bs) (msubstlepos c t a b) = Ifm vs (((Itm vs (x#bs) t /  Ipoly vs c ))#bs) (Le (CNP 0 a b))" (is "?lhs = ?rhs")
  1.2678 +  using nz l tmbound0_I[of b vs x bs "Itm vs (x # bs) t / \<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup>" , symmetric]
  1.2679 +  by (simp add: msubstlepos_def field_simps)
  1.2680 +
  1.2681 +lemma msubstltneg: 
  1.2682 +  assumes nz: "Ipoly vs c < 0" and l: "islin (Lt (CNP 0 a b))"
  1.2683 +  shows "Ifm vs (x#bs) (msubstltneg c t a b) = Ifm vs (((Itm vs (x#bs) t /  Ipoly vs c ))#bs) (Lt (CNP 0 a b))" (is "?lhs = ?rhs")
  1.2684 +  using nz l tmbound0_I[of b vs x bs "Itm vs (x # bs) t / \<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup>" , symmetric]
  1.2685 +  by (simp add: msubstltneg_def field_simps del: minus_add_distrib)
  1.2686 +
  1.2687 +lemma msubstleneg: 
  1.2688 +  assumes nz: "Ipoly vs c < 0" and l: "islin (Le (CNP 0 a b))"
  1.2689 +  shows "Ifm vs (x#bs) (msubstleneg c t a b) = Ifm vs (((Itm vs (x#bs) t /  Ipoly vs c ))#bs) (Le (CNP 0 a b))" (is "?lhs = ?rhs")
  1.2690 +  using nz l tmbound0_I[of b vs x bs "Itm vs (x # bs) t / \<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup>" , symmetric]
  1.2691 +  by (simp add: msubstleneg_def field_simps del: minus_add_distrib)
  1.2692 +
  1.2693 +fun msubstpos :: "fm \<Rightarrow> poly \<Rightarrow> tm \<Rightarrow> fm" where
  1.2694 +  "msubstpos (And p q) c t = And (msubstpos p c t) (msubstpos q c t)"
  1.2695 +| "msubstpos (Or p q) c t = Or (msubstpos p c t) (msubstpos q c t)"
  1.2696 +| "msubstpos (Eq (CNP 0 a r)) c t = msubsteq2 c t a r"
  1.2697 +| "msubstpos (NEq (CNP 0 a r)) c t = NOT (msubsteq2 c t a r)"
  1.2698 +| "msubstpos (Lt (CNP 0 a r)) c t = msubstltpos c t a r"
  1.2699 +| "msubstpos (Le (CNP 0 a r)) c t = msubstlepos c t a r"
  1.2700 +| "msubstpos p c t = p"
  1.2701 +    
  1.2702 +lemma msubstpos_I: 
  1.2703 +  assumes lp: "islin p" and pos: "Ipoly vs c > 0"
  1.2704 +  shows "Ifm vs (x#bs) (msubstpos p c t) = Ifm vs (Itm vs (x#bs) t /  Ipoly vs c #bs) p"
  1.2705 +  using lp pos
  1.2706 +  by (induct p rule: islin.induct, auto simp add: msubsteq2 msubstltpos[OF pos] msubstlepos[OF pos] tmbound0_I[of _ vs "Itm vs (x # bs) t / \<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup>" bs x] bound0_I[of _ vs "Itm vs (x # bs) t / \<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup>" bs x] field_simps)
  1.2707 +
  1.2708 +fun msubstneg :: "fm \<Rightarrow> poly \<Rightarrow> tm \<Rightarrow> fm" where
  1.2709 +  "msubstneg (And p q) c t = And (msubstneg p c t) (msubstneg q c t)"
  1.2710 +| "msubstneg (Or p q) c t = Or (msubstneg p c t) (msubstneg q c t)"
  1.2711 +| "msubstneg (Eq (CNP 0 a r)) c t = msubsteq2 c t a r"
  1.2712 +| "msubstneg (NEq (CNP 0 a r)) c t = NOT (msubsteq2 c t a r)"
  1.2713 +| "msubstneg (Lt (CNP 0 a r)) c t = msubstltneg c t a r"
  1.2714 +| "msubstneg (Le (CNP 0 a r)) c t = msubstleneg c t a r"
  1.2715 +| "msubstneg p c t = p"
  1.2716 +
  1.2717 +lemma msubstneg_I: 
  1.2718 +  assumes lp: "islin p" and pos: "Ipoly vs c < 0"
  1.2719 +  shows "Ifm vs (x#bs) (msubstneg p c t) = Ifm vs (Itm vs (x#bs) t /  Ipoly vs c #bs) p"
  1.2720 +  using lp pos
  1.2721 +  by (induct p rule: islin.induct, auto simp add: msubsteq2 msubstltneg[OF pos] msubstleneg[OF pos] tmbound0_I[of _ vs "Itm vs (x # bs) t / \<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup>" bs x] bound0_I[of _ vs "Itm vs (x # bs) t / \<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup>" bs x] field_simps)
  1.2722 +
  1.2723 +
  1.2724 +definition "msubst2 p c t = disj (conj (lt (CP (polyneg c))) (simpfm (msubstpos p c t))) (conj (lt (CP c)) (simpfm (msubstneg p c t)))"
  1.2725 +
  1.2726 +lemma msubst2: assumes lp: "islin p" and nc: "isnpoly c" and nz: "Ipoly vs c \<noteq> 0"
  1.2727 +  shows "Ifm vs (x#bs) (msubst2 p c t) = Ifm vs (Itm vs (x#bs) t /  Ipoly vs c #bs) p"
  1.2728 +proof-
  1.2729 +  let ?c = "Ipoly vs c"
  1.2730 +  from nc have anc: "allpolys isnpoly (CP c)" "allpolys isnpoly (CP (~\<^sub>p c))" 
  1.2731 +    by (simp_all add: polyneg_norm)
  1.2732 +  from nz have "?c > 0 \<or> ?c < 0" by arith
  1.2733 +  moreover
  1.2734 +  {assume c: "?c < 0"
  1.2735 +    from c msubstneg_I[OF lp c, of x bs t] lt[OF anc(1), of vs "x#bs"] lt[OF anc(2), of vs "x#bs"]
  1.2736 +    have ?thesis by (auto simp add: msubst2_def)}
  1.2737 +  moreover
  1.2738 +  {assume c: "?c > 0"
  1.2739 +    from c msubstpos_I[OF lp c, of x bs t] lt[OF anc(1), of vs "x#bs"] lt[OF anc(2), of vs "x#bs"]
  1.2740 +    have ?thesis by (auto simp add: msubst2_def)}
  1.2741 +  ultimately show ?thesis by blast
  1.2742 +qed
  1.2743 +
  1.2744 +term msubsteq2
  1.2745 +lemma msubsteq2_nb: "tmbound0 t \<Longrightarrow> islin (Eq (CNP 0 a r)) \<Longrightarrow> bound0 (msubsteq2 c t a r)"
  1.2746 +  by (simp add: msubsteq2_def)
  1.2747 +
  1.2748 +lemma msubstltpos_nb: "tmbound0 t \<Longrightarrow> islin (Lt (CNP 0 a r)) \<Longrightarrow> bound0 (msubstltpos c t a r)"
  1.2749 +  by (simp add: msubstltpos_def)
  1.2750 +lemma msubstltneg_nb: "tmbound0 t \<Longrightarrow> islin (Lt (CNP 0 a r)) \<Longrightarrow> bound0 (msubstltneg c t a r)"
  1.2751 +  by (simp add: msubstltneg_def)
  1.2752 +
  1.2753 +lemma msubstlepos_nb: "tmbound0 t \<Longrightarrow> islin (Le (CNP 0 a r)) \<Longrightarrow> bound0 (msubstlepos c t a r)"
  1.2754 +  by (simp add: msubstlepos_def)
  1.2755 +lemma msubstleneg_nb: "tmbound0 t \<Longrightarrow> islin (Le (CNP 0 a r)) \<Longrightarrow> bound0 (msubstleneg c t a r)"
  1.2756 +  by (simp add: msubstleneg_def)
  1.2757 +
  1.2758 +lemma msubstpos_nb: assumes lp: "islin p" and tnb: "tmbound0 t"
  1.2759 +  shows "bound0 (msubstpos p c t)"
  1.2760 +using lp tnb
  1.2761 +by (induct p c t rule: msubstpos.induct, auto simp add: msubsteq2_nb msubstltpos_nb msubstlepos_nb)
  1.2762 +
  1.2763 +lemma msubstneg_nb: assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})" and lp: "islin p" and tnb: "tmbound0 t"
  1.2764 +  shows "bound0 (msubstneg p c t)"
  1.2765 +using lp tnb
  1.2766 +by (induct p c t rule: msubstneg.induct, auto simp add: msubsteq2_nb msubstltneg_nb msubstleneg_nb)
  1.2767 +
  1.2768 +lemma msubst2_nb: assumes "SORT_CONSTRAINT('a::{ring_char_0,division_by_zero,field})" and lp: "islin p" and tnb: "tmbound0 t"
  1.2769 +  shows "bound0 (msubst2 p c t)"
  1.2770 +using lp tnb
  1.2771 +by (simp add: msubst2_def msubstneg_nb msubstpos_nb conj_nb disj_nb lt_nb simpfm_bound0)
  1.2772 +    
  1.2773 +lemma of_int2: "of_int 2 = 1 + 1"
  1.2774 +proof-
  1.2775 +  have "(2::int) = 1 + 1" by simp
  1.2776 +  hence "of_int 2 = of_int (1 + 1)" by simp
  1.2777 +  thus ?thesis unfolding of_int_add by simp
  1.2778 +qed
  1.2779 +
  1.2780 +lemma of_int_minus2: "of_int (-2) = - (1 + 1)"
  1.2781 +proof-
  1.2782 +  have th: "(-2::int) = - 2" by simp
  1.2783 +  show ?thesis unfolding th by (simp only: of_int_minus of_int2)
  1.2784 +qed
  1.2785 +
  1.2786 +
  1.2787 +lemma islin_qf: "islin p \<Longrightarrow> qfree p"
  1.2788 +  by (induct p rule: islin.induct, auto simp add: bound0_qf)
  1.2789 +lemma fr_eq_msubst2: 
  1.2790 +  assumes lp: "islin p"
  1.2791 +  shows "(\<exists> x. Ifm vs (x#bs) p) \<longleftrightarrow> ((Ifm vs (x#bs) (minusinf p)) \<or> (Ifm vs (x#bs) (plusinf p)) \<or> Ifm vs (x#bs) (subst0 (CP 0\<^sub>p) p) \<or> (\<exists>(n, t)\<in>set (uset p). Ifm vs (x# bs) (msubst2 p (n *\<^sub>p (C (-2,1))) t)) \<or> (\<exists> (c,t) \<in> set (uset p). \<exists> (d,s) \<in> set (uset p). Ifm vs (x#bs) (msubst2 p (C (-2, 1) *\<^sub>p c*\<^sub>p d) (Add (Mul d t) (Mul c s)))))"
  1.2792 +  (is "(\<exists> x. ?I x p) = (?M \<or> ?P \<or> ?Pz \<or> ?PU \<or> ?F)" is "?E = ?D")
  1.2793 +proof-
  1.2794 +  from uset_l[OF lp] have th: "\<forall>(c, s)\<in>set (uset p). isnpoly c \<and> tmbound0 s" by blast
  1.2795 +  let ?I = "\<lambda>p. Ifm vs (x#bs) p"
  1.2796 +  have n2: "isnpoly (C (-2,1))" by (simp add: isnpoly_def)
  1.2797 +  note eq0 = subst0[OF islin_qf[OF lp], of vs x bs "CP 0\<^sub>p", simplified]
  1.2798 +  
  1.2799 +  have eq1: "(\<exists>(n, t)\<in>set (uset p). ?I (msubst2 p (n *\<^sub>p (C (-2,1))) t)) \<longleftrightarrow> (\<exists>(n, t)\<in>set (uset p). \<lparr>n\<rparr>\<^sub>p\<^bsup>vs\<^esup> \<noteq> 0 \<and> Ifm vs (- Itm vs (x # bs) t / (\<lparr>n\<rparr>\<^sub>p\<^bsup>vs\<^esup> * (1 + 1)) # bs) p)"
  1.2800 +  proof-
  1.2801 +    {fix n t assume H: "(n, t)\<in>set (uset p)" "?I(msubst2 p (n *\<^sub>p C (-2, 1)) t)"
  1.2802 +      from H(1) th have "isnpoly n" by blast
  1.2803 +      hence nn: "isnpoly (n *\<^sub>p (C (-2,1)))" by (simp_all add: polymul_norm n2)
  1.2804 +      have nn': "allpolys isnpoly (CP (~\<^sub>p (n *\<^sub>p C (-2, 1))))"
  1.2805 +	by (simp add: polyneg_norm nn)
  1.2806 +      hence nn2: "\<lparr>n *\<^sub>p(C (-2,1)) \<rparr>\<^sub>p\<^bsup>vs\<^esup> \<noteq> 0" "\<lparr>n \<rparr>\<^sub>p\<^bsup>vs\<^esup> \<noteq> 0" using H(2) nn' nn 
  1.2807 +	by (auto simp add: msubst2_def lt zero_less_mult_iff mult_less_0_iff)
  1.2808 +      from msubst2[OF lp nn nn2(1), of x bs t]
  1.2809 +      have "\<lparr>n\<rparr>\<^sub>p\<^bsup>vs\<^esup> \<noteq> 0 \<and> Ifm vs (- Itm vs (x # bs) t / (\<lparr>n\<rparr>\<^sub>p\<^bsup>vs\<^esup> * (1 + 1)) # bs) p"
  1.2810 +	using H(2) nn2 by (simp add: of_int_minus2 del: minus_add_distrib)}
  1.2811 +    moreover
  1.2812 +    {fix n t assume H: "(n, t)\<in>set (uset p)" "\<lparr>n\<rparr>\<^sub>p\<^bsup>vs\<^esup> \<noteq> 0" "Ifm vs (- Itm vs (x # bs) t / (\<lparr>n\<rparr>\<^sub>p\<^bsup>vs\<^esup> * (1 + 1)) # bs) p"
  1.2813 +      from H(1) th have "isnpoly n" by blast
  1.2814 +      hence nn: "isnpoly (n *\<^sub>p (C (-2,1)))" "\<lparr>n *\<^sub>p(C (-2,1)) \<rparr>\<^sub>p\<^bsup>vs\<^esup> \<noteq> 0"
  1.2815 +	using H(2) by (simp_all add: polymul_norm n2)
  1.2816 +      from msubst2[OF lp nn, of x bs t] have "?I (msubst2 p (n *\<^sub>p (C (-2,1))) t)" using H(2,3) by (simp add: of_int_minus2 del: minus_add_distrib)}
  1.2817 +    ultimately show ?thesis by blast
  1.2818 +  qed
  1.2819 +  have eq2: "(\<exists> (c,t) \<in> set (uset p). \<exists> (d,s) \<in> set (uset p). Ifm vs (x#bs) (msubst2 p (C (-2, 1) *\<^sub>p c*\<^sub>p d) (Add (Mul d t) (Mul c s)))) \<longleftrightarrow> (\<exists>(n, t)\<in>set (uset p).
  1.2820 +     \<exists>(m, s)\<in>set (uset p). \<lparr>n\<rparr>\<^sub>p\<^bsup>vs\<^esup> \<noteq> 0 \<and> \<lparr>m\<rparr>\<^sub>p\<^bsup>vs\<^esup> \<noteq> 0 \<and> Ifm vs ((- Itm vs (x # bs) t / \<lparr>n\<rparr>\<^sub>p\<^bsup>vs\<^esup> + - Itm vs (x # bs) s / \<lparr>m\<rparr>\<^sub>p\<^bsup>vs\<^esup>) / (1 + 1) # bs) p)" 
  1.2821 +  proof-
  1.2822 +    {fix c t d s assume H: "(c,t) \<in> set (uset p)" "(d,s) \<in> set (uset p)" 
  1.2823 +     "Ifm vs (x#bs) (msubst2 p (C (-2, 1) *\<^sub>p c*\<^sub>p d) (Add (Mul d t) (Mul c s)))"
  1.2824 +      from H(1,2) th have "isnpoly c" "isnpoly d" by blast+
  1.2825 +      hence nn: "isnpoly (C (-2, 1) *\<^sub>p c*\<^sub>p d)" 
  1.2826 +	by (simp_all add: polymul_norm n2)
  1.2827 +      have stupid: "allpolys isnpoly (CP (~\<^sub>p (C (-2, 1) *\<^sub>p c *\<^sub>p d)))" "allpolys isnpoly (CP ((C (-2, 1) *\<^sub>p c *\<^sub>p d)))"
  1.2828 +	by (simp_all add: polyneg_norm nn)
  1.2829 +      have nn': "\<lparr>(C (-2, 1) *\<^sub>p c*\<^sub>p d)\<rparr>\<^sub>p\<^bsup>vs\<^esup> \<noteq> 0" "\<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup> \<noteq> 0" "\<lparr>d\<rparr>\<^sub>p\<^bsup>vs\<^esup> \<noteq> 0"
  1.2830 +	using H(3) by (auto simp add: msubst2_def lt[OF stupid(1)]  lt[OF stupid(2)] zero_less_mult_iff mult_less_0_iff)
  1.2831 +      from msubst2[OF lp nn nn'(1), of x bs ] H(3) nn'
  1.2832 +      have "\<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup> \<noteq> 0 \<and> \<lparr>d\<rparr>\<^sub>p\<^bsup>vs\<^esup> \<noteq> 0 \<and> Ifm vs ((- Itm vs (x # bs) t / \<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup> + - Itm vs (x # bs) s / \<lparr>d\<rparr>\<^sub>p\<^bsup>vs\<^esup>) / (1 + 1) # bs) p" 
  1.2833 +	apply (simp add: add_divide_distrib of_int_minus2 del: minus_add_distrib)
  1.2834 +	by (simp add: mult_commute)}
  1.2835 +    moreover
  1.2836 +    {fix c t d s assume H: "(c,t) \<in> set (uset p)" "(d,s) \<in> set (uset p)" 
  1.2837 +      "\<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup> \<noteq> 0" "\<lparr>d\<rparr>\<^sub>p\<^bsup>vs\<^esup> \<noteq> 0" "Ifm vs ((- Itm vs (x # bs) t / \<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup> + - Itm vs (x # bs) s / \<lparr>d\<rparr>\<^sub>p\<^bsup>vs\<^esup>) / (1 + 1) # bs) p"
  1.2838 +     from H(1,2) th have "isnpoly c" "isnpoly d" by blast+
  1.2839 +      hence nn: "isnpoly (C (-2, 1) *\<^sub>p c*\<^sub>p d)" "\<lparr>(C (-2, 1) *\<^sub>p c*\<^sub>p d)\<rparr>\<^sub>p\<^bsup>vs\<^esup> \<noteq> 0"
  1.2840 +	using H(3,4) by (simp_all add: polymul_norm n2)
  1.2841 +      from msubst2[OF lp nn, of x bs ] H(3,4,5) 
  1.2842 +      have "Ifm vs (x#bs) (msubst2 p (C (-2, 1) *\<^sub>p c*\<^sub>p d) (Add (Mul d t) (Mul c s)))" apply (simp add: add_divide_distrib of_int_minus2 del: minus_add_distrib)by (simp add: mult_commute)}
  1.2843 +    ultimately show ?thesis by blast
  1.2844 +  qed
  1.2845 +  from fr_eq2[OF lp, of vs bs x] show ?thesis
  1.2846 +    unfolding eq0 eq1 eq2 by blast  
  1.2847 +qed
  1.2848 +
  1.2849 +definition 
  1.2850 +"ferrack2 p \<equiv> let q = simpfm p ; mp = minusinf q ; pp = plusinf q
  1.2851 + in if (mp = T \<or> pp = T) then T 
  1.2852 +  else (let U = remdps (uset  q)
  1.2853 +    in decr0 (list_disj [mp, pp, simpfm (subst0 (CP 0\<^sub>p) q), evaldjf (\<lambda>(c,t). msubst2 q (c *\<^sub>p C (-2, 1)) t) U, 
  1.2854 +   evaldjf (\<lambda>((b,a),(d,c)). msubst2 q (C (-2, 1) *\<^sub>p b*\<^sub>p d) (Add (Mul d a) (Mul b c))) (alluopairs U)]))"
  1.2855 +
  1.2856 +definition "frpar2 p = simpfm (qelim (prep p) ferrack2)"
  1.2857 +
  1.2858 +lemma ferrack2: assumes qf: "qfree p"
  1.2859 +  shows "qfree (ferrack2 p) \<and> ((Ifm vs bs (ferrack2 p)) = (Ifm vs bs (E p)))"
  1.2860 +  (is "_ \<and> (?rhs = ?lhs)")
  1.2861 +proof-
  1.2862 +  let ?J = "\<lambda> x p. Ifm vs (x#bs) p"
  1.2863 +  let ?N = "\<lambda> t. Ipoly vs t"
  1.2864 +  let ?Nt = "\<lambda>x t. Itm vs (x#bs) t"
  1.2865 +  let ?q = "simpfm p" 
  1.2866 +  let ?qz = "subst0 (CP 0\<^sub>p) ?q"
  1.2867 +  let ?U = "remdps(uset ?q)"
  1.2868 +  let ?Up = "alluopairs ?U"
  1.2869 +  let ?mp = "minusinf ?q"
  1.2870 +  let ?pp = "plusinf ?q"
  1.2871 +  let ?I = "\<lambda>p. Ifm vs (x#bs) p"
  1.2872 +  from simpfm_lin[OF qf] simpfm_qf[OF qf] have lq: "islin ?q" and q_qf: "qfree ?q" .
  1.2873 +  from minusinf_nb[OF lq] plusinf_nb[OF lq] have mp_nb: "bound0 ?mp" and pp_nb: "bound0 ?pp" .
  1.2874 +  from bound0_qf[OF mp_nb] bound0_qf[OF pp_nb] have mp_qf: "qfree ?mp" and pp_qf: "qfree ?pp" .
  1.2875 +  from uset_l[OF lq] have U_l: "\<forall>(c, s)\<in>set ?U. isnpoly c \<and> c \<noteq> 0\<^sub>p \<and> tmbound0 s \<and> allpolys isnpoly s"
  1.2876 +    by simp
  1.2877 +  have bnd0: "\<forall>x \<in> set ?U. bound0 ((\<lambda>(c,t). msubst2 ?q (c *\<^sub>p C (-2, 1)) t) x)" 
  1.2878 +  proof-
  1.2879 +    {fix c t assume ct: "(c,t) \<in> set ?U"
  1.2880 +      hence tnb: "tmbound0 t" using U_l by blast
  1.2881 +      from msubst2_nb[OF lq tnb]
  1.2882 +      have "bound0 ((\<lambda>(c,t). msubst2 ?q (c *\<^sub>p C (-2, 1)) t) (c,t))" by simp}
  1.2883 +    thus ?thesis by auto
  1.2884 +  qed
  1.2885 +  have bnd1: "\<forall>x \<in> set ?Up. bound0 ((\<lambda>((b,a),(d,c)). msubst2 ?q (C (-2, 1) *\<^sub>p b*\<^sub>p d) (Add (Mul d a) (Mul b c))) x)" 
  1.2886 +  proof-
  1.2887 +    {fix b a d c assume badc: "((b,a),(d,c)) \<in> set ?Up"
  1.2888 +      from badc U_l alluopairs_set1[of ?U] 
  1.2889 +      have nb: "tmbound0 (Add (Mul d a) (Mul b c))" by auto
  1.2890 +      from msubst2_nb[OF lq nb] have "bound0 ((\<lambda>((b,a),(d,c)). msubst2 ?q (C (-2, 1) *\<^sub>p b*\<^sub>p d) (Add (Mul d a) (Mul b c))) ((b,a),(d,c)))" by simp}
  1.2891 +    thus ?thesis by auto
  1.2892 +  qed
  1.2893 +  have stupid: "bound0 F" by simp
  1.2894 +  let ?R = "list_disj [?mp, ?pp, simpfm (subst0 (CP 0\<^sub>p) ?q), evaldjf (\<lambda>(c,t). msubst2 ?q (c *\<^sub>p C (-2, 1)) t) ?U, 
  1.2895 +   evaldjf (\<lambda>((b,a),(d,c)). msubst2 ?q (C (-2, 1) *\<^sub>p b*\<^sub>p d) (Add (Mul d a) (Mul b c))) (alluopairs ?U)]"
  1.2896 +  from subst0_nb[of "CP 0\<^sub>p" ?q] q_qf evaldjf_bound0[OF bnd1] evaldjf_bound0[OF bnd0] mp_nb pp_nb stupid
  1.2897 +  have nb: "bound0 ?R "
  1.2898 +    by (simp add: list_disj_def disj_nb0 simpfm_bound0)
  1.2899 +  let ?s = "\<lambda>((b,a),(d,c)). msubst2 ?q (C (-2, 1) *\<^sub>p b*\<^sub>p d) (Add (Mul d a) (Mul b c))"
  1.2900 +
  1.2901 +  {fix b a d c assume baU: "(b,a) \<in> set ?U" and dcU: "(d,c) \<in> set ?U"
  1.2902 +    from U_l baU dcU have norm: "isnpoly b" "isnpoly d" "isnpoly (C (-2, 1))" 
  1.2903 +      by auto (simp add: isnpoly_def)
  1.2904 +    have norm2: "isnpoly (C (-2, 1) *\<^sub>p b*\<^sub>p d)" "isnpoly (C (-2, 1) *\<^sub>p d*\<^sub>p b)"
  1.2905 +      using norm by (simp_all add: polymul_norm)
  1.2906 +    have stupid: "allpolys isnpoly (CP (C (-2, 1) *\<^sub>p b*\<^sub>p d))" "allpolys isnpoly (CP (C (-2, 1) *\<^sub>p d*\<^sub>p b))" "allpolys isnpoly (CP (~\<^sub>p(C (-2, 1) *\<^sub>p b*\<^sub>p d)))" "allpolys isnpoly (CP (~\<^sub>p(C (-2, 1) *\<^sub>p d*\<^sub>p b)))"
  1.2907 +      by (simp_all add: polyneg_norm norm2)
  1.2908 +    have "?I (msubst2 ?q (C (-2, 1) *\<^sub>p b*\<^sub>p d) (Add (Mul d a) (Mul b c))) = ?I (msubst2 ?q (C (-2, 1) *\<^sub>p d*\<^sub>p b) (Add (Mul b c) (Mul d a)))" (is "?lhs \<longleftrightarrow> ?rhs")
  1.2909 +    proof
  1.2910 +      assume H: ?lhs
  1.2911 +      hence z: "\<lparr>C (-2, 1) *\<^sub>p b *\<^sub>p d\<rparr>\<^sub>p\<^bsup>vs\<^esup> \<noteq> 0" "\<lparr>C (-2, 1) *\<^sub>p d *\<^sub>p b\<rparr>\<^sub>p\<^bsup>vs\<^esup> \<noteq> 0" 
  1.2912 +	by (auto simp add: msubst2_def lt[OF stupid(3)] lt[OF stupid(1)] mult_less_0_iff zero_less_mult_iff)
  1.2913 +      from msubst2[OF lq norm2(1) z(1), of x bs] 
  1.2914 +	msubst2[OF lq norm2(2) z(2), of x bs] H 
  1.2915 +      show ?rhs by (simp add: ring_simps)
  1.2916 +    next
  1.2917 +      assume H: ?rhs
  1.2918 +      hence z: "\<lparr>C (-2, 1) *\<^sub>p b *\<^sub>p d\<rparr>\<^sub>p\<^bsup>vs\<^esup> \<noteq> 0" "\<lparr>C (-2, 1) *\<^sub>p d *\<^sub>p b\<rparr>\<^sub>p\<^bsup>vs\<^esup> \<noteq> 0" 
  1.2919 +	by (auto simp add: msubst2_def lt[OF stupid(4)] lt[OF stupid(2)] mult_less_0_iff zero_less_mult_iff)
  1.2920 +      from msubst2[OF lq norm2(1) z(1), of x bs] 
  1.2921 +	msubst2[OF lq norm2(2) z(2), of x bs] H 
  1.2922 +      show ?lhs by (simp add: ring_simps)
  1.2923 +    qed}
  1.2924 +  hence th0: "\<forall>x \<in> set ?U. \<forall>y \<in> set ?U. ?I (?s (x, y)) \<longleftrightarrow> ?I (?s (y, x))"
  1.2925 +    by clarsimp
  1.2926 +
  1.2927 +  have "?lhs \<longleftrightarrow> (\<exists>x. Ifm vs (x#bs) ?q)" by simp
  1.2928 +  also have "\<dots> \<longleftrightarrow> ?I ?mp \<or> ?I ?pp \<or> ?I (subst0 (CP 0\<^sub>p) ?q) \<or> (\<exists>(n,t) \<in> set ?U. ?I (msubst2 ?q (n *\<^sub>p C (-2, 1)) t)) \<or> (\<exists>(b, a)\<in>set ?U. \<exists>(d, c)\<in>set ?U. ?I (msubst2 ?q (C (-2, 1) *\<^sub>p b*\<^sub>p d) (Add (Mul d a) (Mul b c))))"
  1.2929 +    using fr_eq_msubst2[OF lq, of vs bs x] by simp
  1.2930 +  also have "\<dots> \<longleftrightarrow> ?I ?mp \<or> ?I ?pp \<or> ?I (subst0 (CP 0\<^sub>p) ?q) \<or> (\<exists>(n,t) \<in> set ?U. ?I (msubst2 ?q (n *\<^sub>p C (-2, 1)) t)) \<or> (\<exists> x\<in>set ?U. \<exists> y \<in>set ?U. ?I (?s (x,y)))"
  1.2931 +    by (simp add: split_def)
  1.2932 +  also have "\<dots> \<longleftrightarrow> ?I ?mp \<or> ?I ?pp \<or> ?I (subst0 (CP 0\<^sub>p) ?q) \<or> (\<exists>(n,t) \<in> set ?U. ?I (msubst2 ?q (n *\<^sub>p C (-2, 1)) t)) \<or> (\<exists> (x,y) \<in> set ?Up. ?I (?s (x,y)))"
  1.2933 +    using alluopairs_ex[OF th0] by simp 
  1.2934 +  also have "\<dots> \<longleftrightarrow> ?I ?R" 
  1.2935 +    by (simp add: list_disj_def evaldjf_ex split_def)
  1.2936 +  also have "\<dots> \<longleftrightarrow> ?rhs"
  1.2937 +    unfolding ferrack2_def
  1.2938 +    apply (cases "?mp = T") 
  1.2939 +    apply (simp add: list_disj_def)
  1.2940 +    apply (cases "?pp = T") 
  1.2941 +    apply (simp add: list_disj_def)
  1.2942 +    by (simp_all add: Let_def decr0[OF nb])
  1.2943 +  finally show ?thesis using decr0_qf[OF nb]  
  1.2944 +    by (simp  add: ferrack2_def Let_def)
  1.2945 +qed
  1.2946 +
  1.2947 +lemma frpar2: "qfree (frpar2 p) \<and> (Ifm vs bs (frpar2 p) \<longleftrightarrow> Ifm vs bs p)"
  1.2948 +proof-
  1.2949 +  from ferrack2 have th: "\<forall>bs p. qfree p \<longrightarrow> qfree (ferrack2 p) \<and> Ifm vs bs (ferrack2 p) = Ifm vs bs (E p)" by blast
  1.2950 +  from qelim[OF th, of "prep p" bs] 
  1.2951 +show ?thesis  unfolding frpar2_def by (auto simp add: prep)
  1.2952 +qed
  1.2953 +
  1.2954 +code_module FRPar
  1.2955 +  contains 
  1.2956 +  frpar = "frpar"
  1.2957 +  frpar2 = "frpar2"
  1.2958 +  test = "%x . frpar (E(Lt (Mul 1\<^sub>p (Bound 0))))"
  1.2959 +
  1.2960 +ML{* 
  1.2961 +
  1.2962 +structure ReflectedFRPar = 
  1.2963 +struct
  1.2964 +
  1.2965 +val bT = HOLogic.boolT;
  1.2966 +fun num rT x = HOLogic.mk_number rT x;
  1.2967 +fun rrelT rT = [rT,rT] ---> rT;
  1.2968 +fun rrT rT = [rT, rT] ---> bT;
  1.2969 +fun divt rT = Const(@{const_name "HOL.divide"},rrelT rT);
  1.2970 +fun timest rT = Const(@{const_name "HOL.times"},rrelT rT);
  1.2971 +fun plust rT = Const(@{const_name "HOL.plus"},rrelT rT);
  1.2972 +fun minust rT = Const(@{const_name "HOL.minus"},rrelT rT);
  1.2973 +fun uminust rT = Const(@{const_name "HOL.uminus"}, rT --> rT);
  1.2974 +fun powt rT = Const(@{const_name "power"}, [rT,@{typ "nat"}] ---> rT);
  1.2975 +val brT = [bT, bT] ---> bT;
  1.2976 +val nott = @{term "Not"};
  1.2977 +val conjt = @{term "op &"};
  1.2978 +val disjt = @{term "op |"};
  1.2979 +val impt = @{term "op -->"};
  1.2980 +val ifft = @{term "op = :: bool => _"}
  1.2981 +fun llt rT = Const(@{const_name "HOL.less"},rrT rT);
  1.2982 +fun lle rT = Const(@{const_name "HOL.less"},rrT rT);
  1.2983 +fun eqt rT = Const("op =",rrT rT);
  1.2984 +fun rz rT = Const(@{const_name "HOL.zero"},rT);
  1.2985 +
  1.2986 +fun dest_nat t = case t of
  1.2987 +  Const ("Suc",_)$t' => 1 + dest_nat t'
  1.2988 +| _ => (snd o HOLogic.dest_number) t;
  1.2989 +
  1.2990 +fun num_of_term m t = 
  1.2991 + case t of
  1.2992 +   Const(@{const_name "uminus"},_)$t => FRPar.Neg (num_of_term m t)
  1.2993 + | Const(@{const_name "HOL.plus"},_)$a$b => FRPar.Add (num_of_term m a, num_of_term m b)
  1.2994 + | Const(@{const_name "HOL.minus"},_)$a$b => FRPar.Sub (num_of_term m a, num_of_term m b)
  1.2995 + | Const(@{const_name "HOL.times"},_)$a$b => FRPar.Mul (num_of_term m a, num_of_term m b)
  1.2996 + | Const(@{const_name "power"},_)$a$n => FRPar.Pw (num_of_term m a, dest_nat n)
  1.2997 + | Const(@{const_name "HOL.divide"},_)$a$b => FRPar.C (HOLogic.dest_number a |> snd, HOLogic.dest_number b |> snd)
  1.2998 + | _ => (FRPar.C (HOLogic.dest_number t |> snd,1) 
  1.2999 +         handle TERM _ => FRPar.Bound (AList.lookup (op aconv) m t |> valOf));
  1.3000 +
  1.3001 +fun tm_of_term m m' t = 
  1.3002 + case t of
  1.3003 +   Const(@{const_name "uminus"},_)$t => FRPar.tm_Neg (tm_of_term m m' t)
  1.3004 + | Const(@{const_name "HOL.plus"},_)$a$b => FRPar.tm_Add (tm_of_term m m' a, tm_of_term m m' b)
  1.3005 + | Const(@{const_name "HOL.minus"},_)$a$b => FRPar.tm_Sub (tm_of_term m m' a, tm_of_term m m' b)
  1.3006 + | Const(@{const_name "HOL.times"},_)$a$b => FRPar.tm_Mul (num_of_term m' a, tm_of_term m m' b)
  1.3007 + | _ => (FRPar.CP (num_of_term m' t) 
  1.3008 +         handle TERM _ => FRPar.tm_Bound (AList.lookup (op aconv) m t |> valOf)
  1.3009 +              | Option => FRPar.tm_Bound (AList.lookup (op aconv) m t |> valOf));
  1.3010 +
  1.3011 +fun term_of_num T m t = 
  1.3012 + case t of
  1.3013 +  FRPar.C (a,b) => (if b = 1 then num T a else if b=0 then (rz T) 
  1.3014 +                                        else (divt T) $ num T a $ num T b)
  1.3015 +| FRPar.Bound i => AList.lookup (op = : int*int -> bool) m i |> valOf
  1.3016 +| FRPar.Add(a,b) => (plust T)$(term_of_num T m a)$(term_of_num T m b)
  1.3017 +| FRPar.Mul(a,b) => (timest T)$(term_of_num T m a)$(term_of_num T m b)
  1.3018 +| FRPar.Sub(a,b) => (minust T)$(term_of_num T m a)$(term_of_num T m b)
  1.3019 +| FRPar.Neg a => (uminust T)$(term_of_num T m a)
  1.3020 +| FRPar.Pw(a,n) => (powt T)$(term_of_num T m t)$(HOLogic.mk_number HOLogic.natT n)
  1.3021 +| FRPar.CN(c,n,p) => term_of_num T m (FRPar.Add(c,FRPar.Mul(FRPar.Bound n, p)))
  1.3022 +| _ => error "term_of_num: Unknown term";
  1.3023 +
  1.3024 +fun term_of_tm T m m' t = 
  1.3025 + case t of
  1.3026 +  FRPar.CP p => term_of_num T m' p
  1.3027 +| FRPar.tm_Bound i => AList.lookup (op = : int*int -> bool) m i |> valOf
  1.3028 +| FRPar.tm_Add(a,b) => (plust T)$(term_of_tm T m m' a)$(term_of_tm T m m' b)
  1.3029 +| FRPar.tm_Mul(a,b) => (timest T)$(term_of_num T m' a)$(term_of_tm T m m' b)
  1.3030 +| FRPar.tm_Sub(a,b) => (minust T)$(term_of_tm T m m' a)$(term_of_tm T m m' b)
  1.3031 +| FRPar.tm_Neg a => (uminust T)$(term_of_tm T m m' a)
  1.3032 +| FRPar.CNP(n,c,p) => term_of_tm T m m' (FRPar.tm_Add(FRPar.tm_Mul(c, FRPar.tm_Bound n), p))
  1.3033 +| _ => error "term_of_tm: Unknown term";
  1.3034 +
  1.3035 +fun fm_of_term m m' fm = 
  1.3036 + case fm of
  1.3037 +    Const("True",_) => FRPar.T
  1.3038 +  | Const("False",_) => FRPar.F
  1.3039 +  | Const("Not",_)$p => FRPar.NOT (fm_of_term m m' p)
  1.3040 +  | Const("op &",_)$p$q => FRPar.And(fm_of_term m m' p, fm_of_term m m' q)
  1.3041 +  | Const("op |",_)$p$q => FRPar.Or(fm_of_term m m' p, fm_of_term m m' q)
  1.3042 +  | Const("op -->",_)$p$q => FRPar.Imp(fm_of_term m m' p, fm_of_term m m' q)
  1.3043 +  | Const("op =",ty)$p$q => 
  1.3044 +       if domain_type ty = bT then FRPar.Iff(fm_of_term m m' p, fm_of_term m m' q)
  1.3045 +       else FRPar.Eq (FRPar.tm_Sub(tm_of_term m m' p, tm_of_term m m' q))
  1.3046 +  | Const(@{const_name "HOL.less"},_)$p$q => 
  1.3047 +        FRPar.Lt (FRPar.tm_Sub(tm_of_term m m' p, tm_of_term m m' q))
  1.3048 +  | Const(@{const_name "HOL.less_eq"},_)$p$q => 
  1.3049 +        FRPar.Le (FRPar.tm_Sub(tm_of_term m m' p, tm_of_term m m' q))
  1.3050 +  | Const("Ex",_)$Abs(xn,xT,p) => 
  1.3051 +     let val (xn', p') =  variant_abs (xn,xT,p)
  1.3052 +         val x = Free(xn',xT)
  1.3053 +         fun incr i = i + 1
  1.3054 +         val m0 = (x,0):: (map (apsnd incr) m)
  1.3055 +      in FRPar.E (fm_of_term m0 m' p') end
  1.3056 +  | Const("All",_)$Abs(xn,xT,p) => 
  1.3057 +     let val (xn', p') =  variant_abs (xn,xT,p)
  1.3058 +         val x = Free(xn',xT)
  1.3059 +         fun incr i = i + 1
  1.3060 +         val m0 = (x,0):: (map (apsnd incr) m)
  1.3061 +      in FRPar.A (fm_of_term m0 m' p') end
  1.3062 +  | _ => error "fm_of_term";
  1.3063 +
  1.3064 +
  1.3065 +fun term_of_fm T m m' t = 
  1.3066 +  case t of
  1.3067 +    FRPar.T => Const("True",bT)
  1.3068 +  | FRPar.F => Const("False",bT)
  1.3069 +  | FRPar.NOT p => nott $ (term_of_fm T m m' p)
  1.3070 +  | FRPar.And (p,q) => conjt $ (term_of_fm T m m' p) $ (term_of_fm T m m' q)
  1.3071 +  | FRPar.Or (p,q) => disjt $ (term_of_fm T m m' p) $ (term_of_fm T m m' q)
  1.3072 +  | FRPar.Imp (p,q) => impt $ (term_of_fm T m m' p) $ (term_of_fm T m m' q)
  1.3073 +  | FRPar.Iff (p,q) => ifft $ (term_of_fm T m m' p) $ (term_of_fm T m m' q)
  1.3074 +  | FRPar.Lt p => (llt T) $ (term_of_tm T m m' p) $ (rz T)
  1.3075 +  | FRPar.Le p => (lle T) $ (term_of_tm T m m' p) $ (rz T)
  1.3076 +  | FRPar.Eq p => (eqt T) $ (term_of_tm T m m' p) $ (rz T)
  1.3077 +  | FRPar.NEq p => nott $ ((eqt T) $ (term_of_tm T m m' p) $ (rz T))
  1.3078 +  | _ => error "term_of_fm: quantifiers!!!!???";
  1.3079 +
  1.3080 +fun frpar_oracle (T,m, m', fm) = 
  1.3081 + let 
  1.3082 +   val t = HOLogic.dest_Trueprop fm
  1.3083 +   val im = 0 upto (length m - 1)
  1.3084 +   val im' = 0 upto (length m' - 1)   
  1.3085 + in HOLogic.mk_Trueprop (HOLogic.mk_eq(t, term_of_fm T (im ~~ m) (im' ~~ m')  
  1.3086 +                                                     (FRPar.frpar (fm_of_term (m ~~ im) (m' ~~ im') t))))
  1.3087 + end;
  1.3088 +
  1.3089 +fun frpar_oracle2 (T,m, m', fm) = 
  1.3090 + let 
  1.3091 +   val t = HOLogic.dest_Trueprop fm
  1.3092 +   val im = 0 upto (length m - 1)
  1.3093 +   val im' = 0 upto (length m' - 1)   
  1.3094 + in HOLogic.mk_Trueprop (HOLogic.mk_eq(t, term_of_fm T (im ~~ m) (im' ~~ m')  
  1.3095 +                                                     (FRPar.frpar2 (fm_of_term (m ~~ im) (m' ~~ im') t))))
  1.3096 + end;
  1.3097 +
  1.3098 +end;
  1.3099 +
  1.3100 +
  1.3101 +*}
  1.3102 +
  1.3103 +oracle frpar_oracle = {* fn (ty, ts, ts', ct) => 
  1.3104 + let 
  1.3105 +  val thy = Thm.theory_of_cterm ct
  1.3106 + in cterm_of thy (ReflectedFRPar.frpar_oracle (ty,ts, ts', term_of ct))
  1.3107 + end *}
  1.3108 +
  1.3109 +oracle frpar_oracle2 = {* fn (ty, ts, ts', ct) => 
  1.3110 + let 
  1.3111 +  val thy = Thm.theory_of_cterm ct
  1.3112 + in cterm_of thy (ReflectedFRPar.frpar_oracle2 (ty,ts, ts', term_of ct))
  1.3113 + end *}
  1.3114 +
  1.3115 +ML{* 
  1.3116 +structure FRParTac = 
  1.3117 +struct
  1.3118 +
  1.3119 +fun frpar_tac T ps ctxt i = 
  1.3120 + (ObjectLogic.full_atomize_tac i) 
  1.3121 + THEN (fn st =>
  1.3122 +  let
  1.3123 +    val g = List.nth (cprems_of st, i - 1)
  1.3124 +    val thy = ProofContext.theory_of ctxt
  1.3125 +    val fs = subtract (op aconv) (map Free (Term.add_frees (term_of g) [])) ps
  1.3126 +    val th = frpar_oracle (T, fs,ps, (* Pattern.eta_long [] *)g)
  1.3127 +  in rtac (th RS iffD2) i st end);
  1.3128 +
  1.3129 +fun frpar2_tac T ps ctxt i = 
  1.3130 + (ObjectLogic.full_atomize_tac i) 
  1.3131 + THEN (fn st =>
  1.3132 +  let
  1.3133 +    val g = List.nth (cprems_of st, i - 1)
  1.3134 +    val thy = ProofContext.theory_of ctxt
  1.3135 +    val fs = subtract (op aconv) (map Free (Term.add_frees (term_of g) [])) ps
  1.3136 +    val th = frpar_oracle2 (T, fs,ps, (* Pattern.eta_long [] *)g)
  1.3137 +  in rtac (th RS iffD2) i st end);
  1.3138 +
  1.3139 +end;
  1.3140 +
  1.3141 +*}
  1.3142 +
  1.3143 +method_setup frpar = {*
  1.3144 +let
  1.3145 + fun keyword k = Scan.lift (Args.$$$ k -- Args.colon) >> K ()
  1.3146 + fun simple_keyword k = Scan.lift (Args.$$$ k) >> K ()
  1.3147 + val parsN = "pars"
  1.3148 + val typN = "type"
  1.3149 + val any_keyword = keyword parsN || keyword typN
  1.3150 + val thms = Scan.repeat (Scan.unless any_keyword Attrib.multi_thm) >> flat
  1.3151 + val cterms = thms >> map Drule.dest_term;
  1.3152 + val terms = Scan.repeat (Scan.unless any_keyword Args.term)
  1.3153 + val typ = Scan.unless any_keyword Args.typ
  1.3154 +in
  1.3155 + (keyword typN |-- typ) -- (keyword parsN |-- terms) >>
  1.3156 +  (fn (T,ps) => fn ctxt => SIMPLE_METHOD' (FRParTac.frpar_tac T ps ctxt))
  1.3157 +end
  1.3158 +*} "Parametric QE for linear Arithmetic over fields, Version 1"
  1.3159 +
  1.3160 +method_setup frpar2 = {*
  1.3161 +let
  1.3162 + fun keyword k = Scan.lift (Args.$$$ k -- Args.colon) >> K ()
  1.3163 + fun simple_keyword k = Scan.lift (Args.$$$ k) >> K ()
  1.3164 + val parsN = "pars"
  1.3165 + val typN = "type"
  1.3166 + val any_keyword = keyword parsN || keyword typN
  1.3167 + val thms = Scan.repeat (Scan.unless any_keyword Attrib.multi_thm) >> flat
  1.3168 + val cterms = thms >> map Drule.dest_term;
  1.3169 + val terms = Scan.repeat (Scan.unless any_keyword Args.term)
  1.3170 + val typ = Scan.unless any_keyword Args.typ
  1.3171 +in
  1.3172 + (keyword typN |-- typ) -- (keyword parsN |-- terms) >>
  1.3173 +  (fn (T,ps) => fn ctxt => SIMPLE_METHOD' (FRParTac.frpar2_tac T ps ctxt))
  1.3174 +end
  1.3175 +*} "Parametric QE for linear Arithmetic over fields, Version 2"
  1.3176 +
  1.3177 +
  1.3178 +lemma "\<exists>(x::'a::{division_by_zero,ordered_field,number_ring}). y \<noteq> -1 \<longrightarrow> (y + 1)*x < 0"
  1.3179 +  apply (frpar type: "'a::{division_by_zero,ordered_field,number_ring}" pars: "y::'a::{division_by_zero,ordered_field,number_ring}")
  1.3180 +  apply (simp add: ring_simps)
  1.3181 +  apply (rule spec[where x=y])
  1.3182 +  apply (frpar type: "'a::{division_by_zero,ordered_field,number_ring}" pars: "z::'a::{division_by_zero,ordered_field,number_ring}")
  1.3183 +  by simp
  1.3184 +
  1.3185 +text{* Collins/Jones Problem *}
  1.3186 +(*
  1.3187 +lemma "\<exists>(r::'a::{division_by_zero,ordered_field,number_ring}). 0 < r \<and> r < 1 \<and> 0 < (2 - 3*r) *(a^2 + b^2) + (2*a)*r \<and> (2 - 3*r) *(a^2 + b^2) + 4*a*r - 2*a - r < 0"
  1.3188 +proof-
  1.3189 +  have "(\<exists>(r::'a::{division_by_zero,ordered_field,number_ring}). 0 < r \<and> r < 1 \<and> 0 < (2 - 3*r) *(a^2 + b^2) + (2*a)*r \<and> (2 - 3*r) *(a^2 + b^2) + 4*a*r - 2*a - r < 0) \<longleftrightarrow> (\<exists>(r::'a::{division_by_zero,ordered_field,number_ring}). 0 < r \<and> r < 1 \<and> 0 < 2 *(a^2 + b^2) - (3*(a^2 + b^2)) * r + (2*a)*r \<and> 2*(a^2 + b^2) - (3*(a^2 + b^2) - 4*a + 1)*r - 2*a < 0)" (is "?lhs \<longleftrightarrow> ?rhs")
  1.3190 +by (simp add: ring_simps)
  1.3191 +have "?rhs"
  1.3192 +
  1.3193 +  apply (frpar type: "'a::{division_by_zero,ordered_field,number_ring}" pars: "a::'a::{division_by_zero,ordered_field,number_ring}" "b::'a::{division_by_zero,ordered_field,number_ring}")
  1.3194 +  apply (simp add: ring_simps)
  1.3195 +oops
  1.3196 +*)
  1.3197 +(*
  1.3198 +lemma "ALL (x::'a::{division_by_zero,ordered_field,number_ring}) y. (1 - t)*x \<le> (1+t)*y \<and> (1 - t)*y \<le> (1+t)*x --> 0 \<le> y"
  1.3199 +apply (frpar type: "'a::{division_by_zero,ordered_field,number_ring}" pars: "t::'a::{division_by_zero,ordered_field,number_ring}")
  1.3200 +oops
  1.3201 +*)
  1.3202 +
  1.3203 +lemma "\<exists>(x::'a::{division_by_zero,ordered_field,number_ring}). y \<noteq> -1 \<longrightarrow> (y + 1)*x < 0"
  1.3204 +  apply (frpar2 type: "'a::{division_by_zero,ordered_field,number_ring}" pars: "y::'a::{division_by_zero,ordered_field,number_ring}")
  1.3205 +  apply (simp add: ring_simps)
  1.3206 +  apply (rule spec[where x=y])
  1.3207 +  apply (frpar2 type: "'a::{division_by_zero,ordered_field,number_ring}" pars: "z::'a::{division_by_zero,ordered_field,number_ring}")
  1.3208 +  by simp
  1.3209 +
  1.3210 +text{* Collins/Jones Problem *}
  1.3211 +
  1.3212 +(*
  1.3213 +lemma "\<exists>(r::'a::{division_by_zero,ordered_field,number_ring}). 0 < r \<and> r < 1 \<and> 0 < (2 - 3*r) *(a^2 + b^2) + (2*a)*r \<and> (2 - 3*r) *(a^2 + b^2) + 4*a*r - 2*a - r < 0"
  1.3214 +proof-
  1.3215 +  have "(\<exists>(r::'a::{division_by_zero,ordered_field,number_ring}). 0 < r \<and> r < 1 \<and> 0 < (2 - 3*r) *(a^2 + b^2) + (2*a)*r \<and> (2 - 3*r) *(a^2 + b^2) + 4*a*r - 2*a - r < 0) \<longleftrightarrow> (\<exists>(r::'a::{division_by_zero,ordered_field,number_ring}). 0 < r \<and> r < 1 \<and> 0 < 2 *(a^2 + b^2) - (3*(a^2 + b^2)) * r + (2*a)*r \<and> 2*(a^2 + b^2) - (3*(a^2 + b^2) - 4*a + 1)*r - 2*a < 0)" (is "?lhs \<longleftrightarrow> ?rhs")
  1.3216 +by (simp add: ring_simps)
  1.3217 +have "?rhs"
  1.3218 +  apply (frpar2 type: "'a::{division_by_zero,ordered_field,number_ring}" pars: "a::'a::{division_by_zero,ordered_field,number_ring}" "b::'a::{division_by_zero,ordered_field,number_ring}")
  1.3219 +  apply simp
  1.3220 +oops
  1.3221 +*)
  1.3222 +
  1.3223 +(*
  1.3224 +lemma "ALL (x::'a::{division_by_zero,ordered_field,number_ring}) y. (1 - t)*x \<le> (1+t)*y \<and> (1 - t)*y \<le> (1+t)*x --> 0 \<le> y"
  1.3225 +apply (frpar2 type: "'a::{division_by_zero,ordered_field,number_ring}" pars: "t::'a::{division_by_zero,ordered_field,number_ring}")
  1.3226 +apply (simp add: field_simps linorder_neq_iff[symmetric])
  1.3227 +apply ferrack
  1.3228 +oops
  1.3229 +*)
  1.3230 +end
  1.3231 \ No newline at end of file