src/HOL/Decision_Procs/Parametric_Ferrante_Rackoff.thy
author krauss
Wed Feb 23 10:48:57 2011 +0100 (2011-02-23)
changeset 41821 c118ae98dfbf
parent 41816 7a55699805dc
child 41822 27afef7d6c37
permissions -rw-r--r--
recdef -> fun
     1 (*  Title:      HOL/Decision_Procs/Parametric_Ferrante_Rackoff.thy
     2     Author:     Amine Chaieb
     3 *)
     4 
     5 header{* A formalization of Ferrante and Rackoff's procedure with polynomial parameters, see Paper in CALCULEMUS 2008 *}
     6 
     7 theory Parametric_Ferrante_Rackoff
     8 imports
     9   Reflected_Multivariate_Polynomial
    10   Dense_Linear_Order
    11   "~~/src/HOL/Library/Efficient_Nat"
    12 begin
    13 
    14 subsection {* Terms *}
    15 
    16 datatype tm = CP poly | Bound nat | Add tm tm | Mul poly tm 
    17   | Neg tm | Sub tm tm | CNP nat poly tm
    18   (* A size for poly to make inductive proofs simpler*)
    19 
    20 primrec tmsize :: "tm \<Rightarrow> nat" where
    21   "tmsize (CP c) = polysize c"
    22 | "tmsize (Bound n) = 1"
    23 | "tmsize (Neg a) = 1 + tmsize a"
    24 | "tmsize (Add a b) = 1 + tmsize a + tmsize b"
    25 | "tmsize (Sub a b) = 3 + tmsize a + tmsize b"
    26 | "tmsize (Mul c a) = 1 + polysize c + tmsize a"
    27 | "tmsize (CNP n c a) = 3 + polysize c + tmsize a "
    28 
    29   (* Semantics of terms tm *)
    30 primrec Itm :: "'a::{field_char_0, field_inverse_zero} list \<Rightarrow> 'a list \<Rightarrow> tm \<Rightarrow> 'a" where
    31   "Itm vs bs (CP c) = (Ipoly vs c)"
    32 | "Itm vs bs (Bound n) = bs!n"
    33 | "Itm vs bs (Neg a) = -(Itm vs bs a)"
    34 | "Itm vs bs (Add a b) = Itm vs bs a + Itm vs bs b"
    35 | "Itm vs bs (Sub a b) = Itm vs bs a - Itm vs bs b"
    36 | "Itm vs bs (Mul c a) = (Ipoly vs c) * Itm vs bs a"
    37 | "Itm vs bs (CNP n c t) = (Ipoly vs c)*(bs!n) + Itm vs bs t"   
    38 
    39 
    40 fun allpolys:: "(poly \<Rightarrow> bool) \<Rightarrow> tm \<Rightarrow> bool"  where
    41   "allpolys P (CP c) = P c"
    42 | "allpolys P (CNP n c p) = (P c \<and> allpolys P p)"
    43 | "allpolys P (Mul c p) = (P c \<and> allpolys P p)"
    44 | "allpolys P (Neg p) = allpolys P p"
    45 | "allpolys P (Add p q) = (allpolys P p \<and> allpolys P q)"
    46 | "allpolys P (Sub p q) = (allpolys P p \<and> allpolys P q)"
    47 | "allpolys P p = True"
    48 
    49 primrec tmboundslt:: "nat \<Rightarrow> tm \<Rightarrow> bool" where
    50   "tmboundslt n (CP c) = True"
    51 | "tmboundslt n (Bound m) = (m < n)"
    52 | "tmboundslt n (CNP m c a) = (m < n \<and> tmboundslt n a)"
    53 | "tmboundslt n (Neg a) = tmboundslt n a"
    54 | "tmboundslt n (Add a b) = (tmboundslt n a \<and> tmboundslt n b)"
    55 | "tmboundslt n (Sub a b) = (tmboundslt n a \<and> tmboundslt n b)" 
    56 | "tmboundslt n (Mul i a) = tmboundslt n a"
    57 
    58 primrec tmbound0:: "tm \<Rightarrow> bool" (* a tm is INDEPENDENT of Bound 0 *) where
    59   "tmbound0 (CP c) = True"
    60 | "tmbound0 (Bound n) = (n>0)"
    61 | "tmbound0 (CNP n c a) = (n\<noteq>0 \<and> tmbound0 a)"
    62 | "tmbound0 (Neg a) = tmbound0 a"
    63 | "tmbound0 (Add a b) = (tmbound0 a \<and> tmbound0 b)"
    64 | "tmbound0 (Sub a b) = (tmbound0 a \<and> tmbound0 b)" 
    65 | "tmbound0 (Mul i a) = tmbound0 a"
    66 lemma tmbound0_I:
    67   assumes nb: "tmbound0 a"
    68   shows "Itm vs (b#bs) a = Itm vs (b'#bs) a"
    69 using nb
    70 by (induct a rule: tm.induct,auto simp add: nth_pos2)
    71 
    72 primrec tmbound:: "nat \<Rightarrow> tm \<Rightarrow> bool" (* a tm is INDEPENDENT of Bound n *) where
    73   "tmbound n (CP c) = True"
    74 | "tmbound n (Bound m) = (n \<noteq> m)"
    75 | "tmbound n (CNP m c a) = (n\<noteq>m \<and> tmbound n a)"
    76 | "tmbound n (Neg a) = tmbound n a"
    77 | "tmbound n (Add a b) = (tmbound n a \<and> tmbound n b)"
    78 | "tmbound n (Sub a b) = (tmbound n a \<and> tmbound n b)" 
    79 | "tmbound n (Mul i a) = tmbound n a"
    80 lemma tmbound0_tmbound_iff: "tmbound 0 t = tmbound0 t" by (induct t, auto)
    81 
    82 lemma tmbound_I: 
    83   assumes bnd: "tmboundslt (length bs) t" and nb: "tmbound n t" and le: "n \<le> length bs"
    84   shows "Itm vs (bs[n:=x]) t = Itm vs bs t"
    85   using nb le bnd
    86   by (induct t rule: tm.induct , auto)
    87 
    88 fun decrtm0:: "tm \<Rightarrow> tm" where
    89   "decrtm0 (Bound n) = Bound (n - 1)"
    90 | "decrtm0 (Neg a) = Neg (decrtm0 a)"
    91 | "decrtm0 (Add a b) = Add (decrtm0 a) (decrtm0 b)"
    92 | "decrtm0 (Sub a b) = Sub (decrtm0 a) (decrtm0 b)"
    93 | "decrtm0 (Mul c a) = Mul c (decrtm0 a)"
    94 | "decrtm0 (CNP n c a) = CNP (n - 1) c (decrtm0 a)"
    95 | "decrtm0 a = a"
    96 
    97 fun incrtm0:: "tm \<Rightarrow> tm" where
    98   "incrtm0 (Bound n) = Bound (n + 1)"
    99 | "incrtm0 (Neg a) = Neg (incrtm0 a)"
   100 | "incrtm0 (Add a b) = Add (incrtm0 a) (incrtm0 b)"
   101 | "incrtm0 (Sub a b) = Sub (incrtm0 a) (incrtm0 b)"
   102 | "incrtm0 (Mul c a) = Mul c (incrtm0 a)"
   103 | "incrtm0 (CNP n c a) = CNP (n + 1) c (incrtm0 a)"
   104 | "incrtm0 a = a"
   105 
   106 lemma decrtm0: assumes nb: "tmbound0 t"
   107   shows "Itm vs (x#bs) t = Itm vs bs (decrtm0 t)"
   108   using nb by (induct t rule: decrtm0.induct, simp_all add: nth_pos2)
   109 
   110 lemma incrtm0: "Itm vs (x#bs) (incrtm0 t) = Itm vs bs t"
   111   by (induct t rule: decrtm0.induct, simp_all add: nth_pos2)
   112 
   113 primrec decrtm:: "nat \<Rightarrow> tm \<Rightarrow> tm" where
   114   "decrtm m (CP c) = (CP c)"
   115 | "decrtm m (Bound n) = (if n < m then Bound n else Bound (n - 1))"
   116 | "decrtm m (Neg a) = Neg (decrtm m a)"
   117 | "decrtm m (Add a b) = Add (decrtm m a) (decrtm m b)"
   118 | "decrtm m (Sub a b) = Sub (decrtm m a) (decrtm m b)"
   119 | "decrtm m (Mul c a) = Mul c (decrtm m a)"
   120 | "decrtm m (CNP n c a) = (if n < m then CNP n c (decrtm m a) else CNP (n - 1) c (decrtm m a))"
   121 
   122 primrec removen:: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list" where
   123   "removen n [] = []"
   124 | "removen n (x#xs) = (if n=0 then xs else (x#(removen (n - 1) xs)))"
   125 
   126 lemma removen_same: "n \<ge> length xs \<Longrightarrow> removen n xs = xs"
   127   by (induct xs arbitrary: n, auto)
   128 
   129 lemma nth_length_exceeds: "n \<ge> length xs \<Longrightarrow> xs!n = []!(n - length xs)"
   130   by (induct xs arbitrary: n, auto)
   131 
   132 lemma removen_length: "length (removen n xs) = (if n \<ge> length xs then length xs else length xs - 1)"
   133   by (induct xs arbitrary: n, auto)
   134 lemma removen_nth: "(removen n xs)!m = (if n \<ge> length xs then xs!m 
   135   else if m < n then xs!m else if m \<le> length xs then xs!(Suc m) else []!(m - (length xs - 1)))"
   136 proof(induct xs arbitrary: n m)
   137   case Nil thus ?case by simp
   138 next
   139   case (Cons x xs n m)
   140   {assume nxs: "n \<ge> length (x#xs)" hence ?case using removen_same[OF nxs] by simp}
   141   moreover
   142   {assume nxs: "\<not> (n \<ge> length (x#xs))" 
   143     {assume mln: "m < n" hence ?case using Cons by (cases m, auto)}
   144     moreover
   145     {assume mln: "\<not> (m < n)" 
   146       {assume mxs: "m \<le> length (x#xs)" hence ?case using Cons by (cases m, auto)}
   147       moreover
   148       {assume mxs: "\<not> (m \<le> length (x#xs))" 
   149         have th: "length (removen n (x#xs)) = length xs" 
   150           using removen_length[where n="n" and xs="x#xs"] nxs by simp
   151         with mxs have mxs':"m \<ge> length (removen n (x#xs))" by auto
   152         hence "(removen n (x#xs))!m = [] ! (m - length xs)" 
   153           using th nth_length_exceeds[OF mxs'] by auto
   154         hence th: "(removen n (x#xs))!m = [] ! (m - (length (x#xs) - 1))" 
   155           by auto
   156         hence ?case using nxs mln mxs by auto }
   157       ultimately have ?case by blast
   158     }
   159     ultimately have ?case by blast
   160   } ultimately show ?case by blast
   161 qed
   162 
   163 lemma decrtm: assumes bnd: "tmboundslt (length bs) t" and nb: "tmbound m t" 
   164   and nle: "m \<le> length bs" 
   165   shows "Itm vs (removen m bs) (decrtm m t) = Itm vs bs t"
   166   using bnd nb nle by (induct t rule: tm.induct) (auto simp add: removen_nth)
   167 
   168 primrec tmsubst0:: "tm \<Rightarrow> tm \<Rightarrow> tm" where
   169   "tmsubst0 t (CP c) = CP c"
   170 | "tmsubst0 t (Bound n) = (if n=0 then t else Bound n)"
   171 | "tmsubst0 t (CNP n c a) = (if n=0 then Add (Mul c t) (tmsubst0 t a) else CNP n c (tmsubst0 t a))"
   172 | "tmsubst0 t (Neg a) = Neg (tmsubst0 t a)"
   173 | "tmsubst0 t (Add a b) = Add (tmsubst0 t a) (tmsubst0 t b)"
   174 | "tmsubst0 t (Sub a b) = Sub (tmsubst0 t a) (tmsubst0 t b)" 
   175 | "tmsubst0 t (Mul i a) = Mul i (tmsubst0 t a)"
   176 lemma tmsubst0:
   177   shows "Itm vs (x#bs) (tmsubst0 t a) = Itm vs ((Itm vs (x#bs) t)#bs) a"
   178   by (induct a rule: tm.induct) (auto simp add: nth_pos2)
   179 
   180 lemma tmsubst0_nb: "tmbound0 t \<Longrightarrow> tmbound0 (tmsubst0 t a)"
   181   by (induct a rule: tm.induct) (auto simp add: nth_pos2)
   182 
   183 primrec tmsubst:: "nat \<Rightarrow> tm \<Rightarrow> tm \<Rightarrow> tm" where
   184   "tmsubst n t (CP c) = CP c"
   185 | "tmsubst n t (Bound m) = (if n=m then t else Bound m)"
   186 | "tmsubst n t (CNP m c a) = (if n=m then Add (Mul c t) (tmsubst n t a) 
   187              else CNP m c (tmsubst n t a))"
   188 | "tmsubst n t (Neg a) = Neg (tmsubst n t a)"
   189 | "tmsubst n t (Add a b) = Add (tmsubst n t a) (tmsubst n t b)"
   190 | "tmsubst n t (Sub a b) = Sub (tmsubst n t a) (tmsubst n t b)" 
   191 | "tmsubst n t (Mul i a) = Mul i (tmsubst n t a)"
   192 
   193 lemma tmsubst: assumes nb: "tmboundslt (length bs) a" and nlt: "n \<le> length bs"
   194   shows "Itm vs bs (tmsubst n t a) = Itm vs (bs[n:= Itm vs bs t]) a"
   195 using nb nlt
   196 by (induct a rule: tm.induct,auto simp add: nth_pos2)
   197 
   198 lemma tmsubst_nb0: assumes tnb: "tmbound0 t"
   199 shows "tmbound0 (tmsubst 0 t a)"
   200 using tnb
   201 by (induct a rule: tm.induct, auto)
   202 
   203 lemma tmsubst_nb: assumes tnb: "tmbound m t"
   204 shows "tmbound m (tmsubst m t a)"
   205 using tnb
   206 by (induct a rule: tm.induct, auto)
   207 lemma incrtm0_tmbound: "tmbound n t \<Longrightarrow> tmbound (Suc n) (incrtm0 t)"
   208   by (induct t, auto)
   209   (* Simplification *)
   210 
   211 consts
   212   tmadd:: "tm \<times> tm \<Rightarrow> tm"
   213 recdef tmadd "measure (\<lambda> (t,s). size t + size s)"
   214   "tmadd (CNP n1 c1 r1,CNP n2 c2 r2) =
   215   (if n1=n2 then 
   216   (let c = c1 +\<^sub>p c2
   217   in if c = 0\<^sub>p then tmadd(r1,r2) else CNP n1 c (tmadd (r1,r2)))
   218   else if n1 \<le> n2 then (CNP n1 c1 (tmadd (r1,CNP n2 c2 r2))) 
   219   else (CNP n2 c2 (tmadd (CNP n1 c1 r1,r2))))"
   220   "tmadd (CNP n1 c1 r1,t) = CNP n1 c1 (tmadd (r1, t))"  
   221   "tmadd (t,CNP n2 c2 r2) = CNP n2 c2 (tmadd (t,r2))" 
   222   "tmadd (CP b1, CP b2) = CP (b1 +\<^sub>p b2)"
   223   "tmadd (a,b) = Add a b"
   224 
   225 lemma tmadd[simp]: "Itm vs bs (tmadd (t,s)) = Itm vs bs (Add t s)"
   226 apply (induct t s rule: tmadd.induct, simp_all add: Let_def)
   227 apply (case_tac "c1 +\<^sub>p c2 = 0\<^sub>p",case_tac "n1 \<le> n2", simp_all)
   228 apply (case_tac "n1 = n2", simp_all add: field_simps)
   229 apply (simp only: right_distrib[symmetric]) 
   230 by (auto simp del: polyadd simp add: polyadd[symmetric])
   231 
   232 lemma tmadd_nb0[simp]: "\<lbrakk> tmbound0 t ; tmbound0 s\<rbrakk> \<Longrightarrow> tmbound0 (tmadd (t,s))"
   233 by (induct t s rule: tmadd.induct, auto simp add: Let_def)
   234 
   235 lemma tmadd_nb[simp]: "\<lbrakk> tmbound n t ; tmbound n s\<rbrakk> \<Longrightarrow> tmbound n (tmadd (t,s))"
   236 by (induct t s rule: tmadd.induct, auto simp add: Let_def)
   237 lemma tmadd_blt[simp]: "\<lbrakk>tmboundslt n t ; tmboundslt n s\<rbrakk> \<Longrightarrow> tmboundslt n (tmadd (t,s))"
   238 by (induct t s rule: tmadd.induct, auto simp add: Let_def)
   239 
   240 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)
   241 
   242 fun tmmul:: "tm \<Rightarrow> poly \<Rightarrow> tm" where
   243   "tmmul (CP j) = (\<lambda> i. CP (i *\<^sub>p j))"
   244 | "tmmul (CNP n c a) = (\<lambda> i. CNP n (i *\<^sub>p c) (tmmul a i))"
   245 | "tmmul t = (\<lambda> i. Mul i t)"
   246 
   247 lemma tmmul[simp]: "Itm vs bs (tmmul t i) = Itm vs bs (Mul i t)"
   248 by (induct t arbitrary: i rule: tmmul.induct, simp_all add: field_simps)
   249 
   250 lemma tmmul_nb0[simp]: "tmbound0 t \<Longrightarrow> tmbound0 (tmmul t i)"
   251 by (induct t arbitrary: i rule: tmmul.induct, auto )
   252 
   253 lemma tmmul_nb[simp]: "tmbound n t \<Longrightarrow> tmbound n (tmmul t i)"
   254 by (induct t arbitrary: n rule: tmmul.induct, auto )
   255 lemma tmmul_blt[simp]: "tmboundslt n t \<Longrightarrow> tmboundslt n (tmmul t i)"
   256 by (induct t arbitrary: i rule: tmmul.induct, auto simp add: Let_def)
   257 
   258 lemma tmmul_allpolys_npoly[simp]: 
   259   assumes "SORT_CONSTRAINT('a::{field_char_0, field_inverse_zero})"
   260   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)
   261 
   262 definition tmneg :: "tm \<Rightarrow> tm" where
   263   "tmneg t \<equiv> tmmul t (C (- 1,1))"
   264 
   265 definition tmsub :: "tm \<Rightarrow> tm \<Rightarrow> tm" where
   266   "tmsub s t \<equiv> (if s = t then CP 0\<^sub>p else tmadd (s,tmneg t))"
   267 
   268 lemma tmneg[simp]: "Itm vs bs (tmneg t) = Itm vs bs (Neg t)"
   269 using tmneg_def[of t] 
   270 apply simp
   271 apply (subst number_of_Min)
   272 apply (simp only: of_int_minus)
   273 apply simp
   274 done
   275 
   276 lemma tmneg_nb0[simp]: "tmbound0 t \<Longrightarrow> tmbound0 (tmneg t)"
   277 using tmneg_def by simp
   278 
   279 lemma tmneg_nb[simp]: "tmbound n t \<Longrightarrow> tmbound n (tmneg t)"
   280 using tmneg_def by simp
   281 lemma tmneg_blt[simp]: "tmboundslt n t \<Longrightarrow> tmboundslt n (tmneg t)"
   282 using tmneg_def by simp
   283 lemma [simp]: "isnpoly (C (-1,1))" unfolding isnpoly_def by simp
   284 lemma tmneg_allpolys_npoly[simp]: 
   285   assumes "SORT_CONSTRAINT('a::{field_char_0, field_inverse_zero})"
   286   shows "allpolys isnpoly t \<Longrightarrow> allpolys isnpoly (tmneg t)" 
   287   unfolding tmneg_def by auto
   288 
   289 lemma tmsub[simp]: "Itm vs bs (tmsub a b) = Itm vs bs (Sub a b)"
   290 using tmsub_def by simp
   291 
   292 lemma tmsub_nb0[simp]: "\<lbrakk> tmbound0 t ; tmbound0 s\<rbrakk> \<Longrightarrow> tmbound0 (tmsub t s)"
   293 using tmsub_def by simp
   294 lemma tmsub_nb[simp]: "\<lbrakk> tmbound n t ; tmbound n s\<rbrakk> \<Longrightarrow> tmbound n (tmsub t s)"
   295 using tmsub_def by simp
   296 lemma tmsub_blt[simp]: "\<lbrakk>tmboundslt n t ; tmboundslt n s\<rbrakk> \<Longrightarrow> tmboundslt n (tmsub t s )"
   297 using tmsub_def by simp
   298 lemma tmsub_allpolys_npoly[simp]: 
   299   assumes "SORT_CONSTRAINT('a::{field_char_0, field_inverse_zero})"
   300   shows "allpolys isnpoly t \<Longrightarrow> allpolys isnpoly s \<Longrightarrow> allpolys isnpoly (tmsub t s)" 
   301   unfolding tmsub_def by (simp add: isnpoly_def)
   302 
   303 fun simptm:: "tm \<Rightarrow> tm" where
   304   "simptm (CP j) = CP (polynate j)"
   305 | "simptm (Bound n) = CNP n 1\<^sub>p (CP 0\<^sub>p)"
   306 | "simptm (Neg t) = tmneg (simptm t)"
   307 | "simptm (Add t s) = tmadd (simptm t,simptm s)"
   308 | "simptm (Sub t s) = tmsub (simptm t) (simptm s)"
   309 | "simptm (Mul i t) = (let i' = polynate i in if i' = 0\<^sub>p then CP 0\<^sub>p else tmmul (simptm t) i')"
   310 | "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))"
   311 
   312 lemma polynate_stupid: 
   313   assumes "SORT_CONSTRAINT('a::{field_char_0, field_inverse_zero})"
   314   shows "polynate t = 0\<^sub>p \<Longrightarrow> Ipoly bs t = (0::'a::{field_char_0, field_inverse_zero})" 
   315 apply (subst polynate[symmetric])
   316 apply simp
   317 done
   318 
   319 lemma simptm_ci[simp]: "Itm vs bs (simptm t) = Itm vs bs t"
   320 by (induct t rule: simptm.induct, auto simp add: tmneg tmadd tmsub tmmul Let_def polynate_stupid) 
   321 
   322 lemma simptm_tmbound0[simp]: 
   323   "tmbound0 t \<Longrightarrow> tmbound0 (simptm t)"
   324 by (induct t rule: simptm.induct, auto simp add: Let_def)
   325 
   326 lemma simptm_nb[simp]: "tmbound n t \<Longrightarrow> tmbound n (simptm t)"
   327 by (induct t rule: simptm.induct, auto simp add: Let_def)
   328 lemma simptm_nlt[simp]: "tmboundslt n t \<Longrightarrow> tmboundslt n (simptm t)"
   329 by (induct t rule: simptm.induct, auto simp add: Let_def)
   330 
   331 lemma [simp]: "isnpoly 0\<^sub>p" and [simp]: "isnpoly (C(1,1))" 
   332   by (simp_all add: isnpoly_def)
   333 lemma simptm_allpolys_npoly[simp]: 
   334   assumes "SORT_CONSTRAINT('a::{field_char_0, field_inverse_zero})"
   335   shows "allpolys isnpoly (simptm p)"
   336   by (induct p rule: simptm.induct, auto simp add: Let_def)
   337 
   338 consts split0 :: "tm \<Rightarrow> (poly \<times> tm)"
   339 recdef split0 "measure tmsize"
   340   "split0 (Bound 0) = (1\<^sub>p, CP 0\<^sub>p)"
   341   "split0 (CNP 0 c t) = (let (c',t') = split0 t in (c +\<^sub>p c',t'))"
   342   "split0 (Neg t) = (let (c,t') = split0 t in (~\<^sub>p c,Neg t'))"
   343   "split0 (CNP n c t) = (let (c',t') = split0 t in (c',CNP n c t'))"
   344   "split0 (Add s t) = (let (c1,s') = split0 s ; (c2,t') = split0 t in (c1 +\<^sub>p c2, Add s' t'))"
   345   "split0 (Sub s t) = (let (c1,s') = split0 s ; (c2,t') = split0 t in (c1 -\<^sub>p c2, Sub s' t'))"
   346   "split0 (Mul c t) = (let (c',t') = split0 t in (c *\<^sub>p c', Mul c t'))"
   347   "split0 t = (0\<^sub>p, t)"
   348 
   349 lemma split0_stupid[simp]: "\<exists>x y. (x,y) = split0 p"
   350   apply (rule exI[where x="fst (split0 p)"])
   351   apply (rule exI[where x="snd (split0 p)"])
   352   by simp
   353 
   354 lemma split0:
   355   "tmbound 0 (snd (split0 t)) \<and> (Itm vs bs (CNP 0 (fst (split0 t)) (snd (split0 t))) = Itm vs bs t)"
   356   apply (induct t rule: split0.induct)
   357   apply simp
   358   apply (simp add: Let_def split_def field_simps)
   359   apply (simp add: Let_def split_def field_simps)
   360   apply (simp add: Let_def split_def field_simps)
   361   apply (simp add: Let_def split_def field_simps)
   362   apply (simp add: Let_def split_def field_simps)
   363   apply (simp add: Let_def split_def mult_assoc right_distrib[symmetric])
   364   apply (simp add: Let_def split_def field_simps)
   365   apply (simp add: Let_def split_def field_simps)
   366   done
   367 
   368 lemma split0_ci: "split0 t = (c',t') \<Longrightarrow> Itm vs bs t = Itm vs bs (CNP 0 c' t')"
   369 proof-
   370   fix c' t'
   371   assume "split0 t = (c', t')" hence "c' = fst (split0 t)" and "t' = snd (split0 t)" by auto
   372   with split0[where t="t" and bs="bs"] show "Itm vs bs t = Itm vs bs (CNP 0 c' t')" by simp
   373 qed
   374 
   375 lemma split0_nb0: 
   376   assumes "SORT_CONSTRAINT('a::{field_char_0, field_inverse_zero})"
   377   shows "split0 t = (c',t') \<Longrightarrow>  tmbound 0 t'"
   378 proof-
   379   fix c' t'
   380   assume "split0 t = (c', t')" hence "c' = fst (split0 t)" and "t' = snd (split0 t)" by auto
   381   with conjunct1[OF split0[where t="t"]] show "tmbound 0 t'" by simp
   382 qed
   383 
   384 lemma split0_nb0'[simp]:   assumes "SORT_CONSTRAINT('a::{field_char_0, field_inverse_zero})"
   385   shows "tmbound0 (snd (split0 t))"
   386   using split0_nb0[of t "fst (split0 t)" "snd (split0 t)"] by (simp add: tmbound0_tmbound_iff)
   387 
   388 
   389 lemma split0_nb: assumes nb:"tmbound n t" shows "tmbound n (snd (split0 t))"
   390   using nb by (induct t rule: split0.induct, auto simp add: Let_def split_def split0_stupid)
   391 
   392 lemma split0_blt: assumes nb:"tmboundslt n t" shows "tmboundslt n (snd (split0 t))"
   393   using nb by (induct t rule: split0.induct, auto simp add: Let_def split_def split0_stupid)
   394 
   395 lemma tmbound_split0: "tmbound 0 t \<Longrightarrow> Ipoly vs (fst(split0 t)) = 0"
   396  by (induct t rule: split0.induct, auto simp add: Let_def split_def split0_stupid)
   397 
   398 lemma tmboundslt_split0: "tmboundslt n t \<Longrightarrow> Ipoly vs (fst(split0 t)) = 0 \<or> n > 0"
   399 by (induct t rule: split0.induct, auto simp add: Let_def split_def split0_stupid)
   400 
   401 lemma tmboundslt0_split0: "tmboundslt 0 t \<Longrightarrow> Ipoly vs (fst(split0 t)) = 0"
   402  by (induct t rule: split0.induct, auto simp add: Let_def split_def split0_stupid)
   403 
   404 lemma allpolys_split0: "allpolys isnpoly p \<Longrightarrow> allpolys isnpoly (snd (split0 p))"
   405 by (induct p rule: split0.induct, auto simp  add: isnpoly_def Let_def split_def split0_stupid)
   406 
   407 lemma isnpoly_fst_split0:   assumes "SORT_CONSTRAINT('a::{field_char_0, field_inverse_zero})"
   408   shows 
   409   "allpolys isnpoly p \<Longrightarrow> isnpoly (fst (split0 p))"
   410   by (induct p rule: split0.induct, 
   411     auto simp  add: polyadd_norm polysub_norm polyneg_norm polymul_norm 
   412     Let_def split_def split0_stupid)
   413 
   414 subsection{* Formulae *}
   415 
   416 datatype fm  =  T| F| Le tm | Lt tm | Eq tm | NEq tm|
   417   NOT fm| And fm fm|  Or fm fm| Imp fm fm| Iff fm fm| E fm| A fm
   418 
   419 
   420   (* A size for fm *)
   421 consts fmsize :: "fm \<Rightarrow> nat"
   422 recdef fmsize "measure size"
   423   "fmsize (NOT p) = 1 + fmsize p"
   424   "fmsize (And p q) = 1 + fmsize p + fmsize q"
   425   "fmsize (Or p q) = 1 + fmsize p + fmsize q"
   426   "fmsize (Imp p q) = 3 + fmsize p + fmsize q"
   427   "fmsize (Iff p q) = 3 + 2*(fmsize p + fmsize q)"
   428   "fmsize (E p) = 1 + fmsize p"
   429   "fmsize (A p) = 4+ fmsize p"
   430   "fmsize p = 1"
   431   (* several lemmas about fmsize *)
   432 lemma fmsize_pos: "fmsize p > 0"        
   433 by (induct p rule: fmsize.induct) simp_all
   434 
   435   (* Semantics of formulae (fm) *)
   436 primrec Ifm ::"'a::{linordered_field_inverse_zero} list \<Rightarrow> 'a list \<Rightarrow> fm \<Rightarrow> bool" where
   437   "Ifm vs bs T = True"
   438 | "Ifm vs bs F = False"
   439 | "Ifm vs bs (Lt a) = (Itm vs bs a < 0)"
   440 | "Ifm vs bs (Le a) = (Itm vs bs a \<le> 0)"
   441 | "Ifm vs bs (Eq a) = (Itm vs bs a = 0)"
   442 | "Ifm vs bs (NEq a) = (Itm vs bs a \<noteq> 0)"
   443 | "Ifm vs bs (NOT p) = (\<not> (Ifm vs bs p))"
   444 | "Ifm vs bs (And p q) = (Ifm vs bs p \<and> Ifm vs bs q)"
   445 | "Ifm vs bs (Or p q) = (Ifm vs bs p \<or> Ifm vs bs q)"
   446 | "Ifm vs bs (Imp p q) = ((Ifm vs bs p) \<longrightarrow> (Ifm vs bs q))"
   447 | "Ifm vs bs (Iff p q) = (Ifm vs bs p = Ifm vs bs q)"
   448 | "Ifm vs bs (E p) = (\<exists> x. Ifm vs (x#bs) p)"
   449 | "Ifm vs bs (A p) = (\<forall> x. Ifm vs (x#bs) p)"
   450 
   451 consts not:: "fm \<Rightarrow> fm"
   452 recdef not "measure size"
   453   "not (NOT (NOT p)) = not p"
   454   "not (NOT p) = p"
   455   "not T = F"
   456   "not F = T"
   457   "not (Lt t) = Le (tmneg t)"
   458   "not (Le t) = Lt (tmneg t)"
   459   "not (Eq t) = NEq t"
   460   "not (NEq t) = Eq t"
   461   "not p = NOT p"
   462 lemma not[simp]: "Ifm vs bs (not p) = Ifm vs bs (NOT p)"
   463 by (induct p rule: not.induct) auto
   464 
   465 definition conj :: "fm \<Rightarrow> fm \<Rightarrow> fm" where
   466   "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 
   467    if p = q then p else And p q)"
   468 lemma conj[simp]: "Ifm vs bs (conj p q) = Ifm vs bs (And p q)"
   469 by (cases "p=F \<or> q=F",simp_all add: conj_def) (cases p,simp_all)
   470 
   471 definition disj :: "fm \<Rightarrow> fm \<Rightarrow> fm" where
   472   "disj p q \<equiv> (if (p = T \<or> q=T) then T else if p=F then q else if q=F then p 
   473        else if p=q then p else Or p q)"
   474 
   475 lemma disj[simp]: "Ifm vs bs (disj p q) = Ifm vs bs (Or p q)"
   476 by (cases "p=T \<or> q=T",simp_all add: disj_def) (cases p,simp_all)
   477 
   478 definition imp :: "fm \<Rightarrow> fm \<Rightarrow> fm" where
   479   "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 
   480     else Imp p q)"
   481 lemma imp[simp]: "Ifm vs bs (imp p q) = Ifm vs bs (Imp p q)"
   482 by (cases "p=F \<or> q=T",simp_all add: imp_def) 
   483 
   484 definition iff :: "fm \<Rightarrow> fm \<Rightarrow> fm" where
   485   "iff p q \<equiv> (if (p = q) then T else if (p = NOT q \<or> NOT p = q) then F else 
   486        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 
   487   Iff p q)"
   488 lemma iff[simp]: "Ifm vs bs (iff p q) = Ifm vs bs (Iff p q)"
   489   by (unfold iff_def,cases "p=q", simp,cases "p=NOT q", simp) (cases "NOT p= q", auto)
   490   (* Quantifier freeness *)
   491 consts qfree:: "fm \<Rightarrow> bool"
   492 recdef qfree "measure size"
   493   "qfree (E p) = False"
   494   "qfree (A p) = False"
   495   "qfree (NOT p) = qfree p" 
   496   "qfree (And p q) = (qfree p \<and> qfree q)" 
   497   "qfree (Or  p q) = (qfree p \<and> qfree q)" 
   498   "qfree (Imp p q) = (qfree p \<and> qfree q)" 
   499   "qfree (Iff p q) = (qfree p \<and> qfree q)"
   500   "qfree p = True"
   501 
   502   (* Boundedness and substitution *)
   503 
   504 primrec boundslt :: "nat \<Rightarrow> fm \<Rightarrow> bool" where
   505   "boundslt n T = True"
   506 | "boundslt n F = True"
   507 | "boundslt n (Lt t) = (tmboundslt n t)"
   508 | "boundslt n (Le t) = (tmboundslt n t)"
   509 | "boundslt n (Eq t) = (tmboundslt n t)"
   510 | "boundslt n (NEq t) = (tmboundslt n t)"
   511 | "boundslt n (NOT p) = boundslt n p"
   512 | "boundslt n (And p q) = (boundslt n p \<and> boundslt n q)"
   513 | "boundslt n (Or p q) = (boundslt n p \<and> boundslt n q)"
   514 | "boundslt n (Imp p q) = ((boundslt n p) \<and> (boundslt n q))"
   515 | "boundslt n (Iff p q) = (boundslt n p \<and> boundslt n q)"
   516 | "boundslt n (E p) = boundslt (Suc n) p"
   517 | "boundslt n (A p) = boundslt (Suc n) p"
   518 
   519 consts 
   520   bound0:: "fm \<Rightarrow> bool" (* A Formula is independent of Bound 0 *)
   521   decr0 :: "fm \<Rightarrow> fm"
   522 recdef bound0 "measure size"
   523   "bound0 T = True"
   524   "bound0 F = True"
   525   "bound0 (Lt a) = tmbound0 a"
   526   "bound0 (Le a) = tmbound0 a"
   527   "bound0 (Eq a) = tmbound0 a"
   528   "bound0 (NEq a) = tmbound0 a"
   529   "bound0 (NOT p) = bound0 p"
   530   "bound0 (And p q) = (bound0 p \<and> bound0 q)"
   531   "bound0 (Or p q) = (bound0 p \<and> bound0 q)"
   532   "bound0 (Imp p q) = ((bound0 p) \<and> (bound0 q))"
   533   "bound0 (Iff p q) = (bound0 p \<and> bound0 q)"
   534   "bound0 p = False"
   535 lemma bound0_I:
   536   assumes bp: "bound0 p"
   537   shows "Ifm vs (b#bs) p = Ifm vs (b'#bs) p"
   538 using bp tmbound0_I[where b="b" and bs="bs" and b'="b'"]
   539 by (induct p rule: bound0.induct,auto simp add: nth_pos2)
   540 
   541 primrec bound:: "nat \<Rightarrow> fm \<Rightarrow> bool" (* A Formula is independent of Bound n *) where
   542   "bound m T = True"
   543 | "bound m F = True"
   544 | "bound m (Lt t) = tmbound m t"
   545 | "bound m (Le t) = tmbound m t"
   546 | "bound m (Eq t) = tmbound m t"
   547 | "bound m (NEq t) = tmbound m t"
   548 | "bound m (NOT p) = bound m p"
   549 | "bound m (And p q) = (bound m p \<and> bound m q)"
   550 | "bound m (Or p q) = (bound m p \<and> bound m q)"
   551 | "bound m (Imp p q) = ((bound m p) \<and> (bound m q))"
   552 | "bound m (Iff p q) = (bound m p \<and> bound m q)"
   553 | "bound m (E p) = bound (Suc m) p"
   554 | "bound m (A p) = bound (Suc m) p"
   555 
   556 lemma bound_I:
   557   assumes bnd: "boundslt (length bs) p" and nb: "bound n p" and le: "n \<le> length bs"
   558   shows "Ifm vs (bs[n:=x]) p = Ifm vs bs p"
   559   using bnd nb le tmbound_I[where bs=bs and vs = vs]
   560 proof(induct p arbitrary: bs n rule: fm.induct)
   561   case (E p bs n) 
   562   {fix y
   563     from E have bnd: "boundslt (length (y#bs)) p" 
   564       and nb: "bound (Suc n) p" and le: "Suc n \<le> length (y#bs)" by simp+
   565     from E.hyps[OF bnd nb le tmbound_I] have "Ifm vs ((y#bs)[Suc n:=x]) p = Ifm vs (y#bs) p" .   }
   566   thus ?case by simp 
   567 next
   568   case (A p bs n) {fix y
   569     from A have bnd: "boundslt (length (y#bs)) p" 
   570       and nb: "bound (Suc n) p" and le: "Suc n \<le> length (y#bs)" by simp+
   571     from A.hyps[OF bnd nb le tmbound_I] have "Ifm vs ((y#bs)[Suc n:=x]) p = Ifm vs (y#bs) p" .   }
   572   thus ?case by simp 
   573 qed auto
   574 
   575 recdef decr0 "measure size"
   576   "decr0 (Lt a) = Lt (decrtm0 a)"
   577   "decr0 (Le a) = Le (decrtm0 a)"
   578   "decr0 (Eq a) = Eq (decrtm0 a)"
   579   "decr0 (NEq a) = NEq (decrtm0 a)"
   580   "decr0 (NOT p) = NOT (decr0 p)" 
   581   "decr0 (And p q) = conj (decr0 p) (decr0 q)"
   582   "decr0 (Or p q) = disj (decr0 p) (decr0 q)"
   583   "decr0 (Imp p q) = imp (decr0 p) (decr0 q)"
   584   "decr0 (Iff p q) = iff (decr0 p) (decr0 q)"
   585   "decr0 p = p"
   586 
   587 lemma decr0: assumes nb: "bound0 p"
   588   shows "Ifm vs (x#bs) p = Ifm vs bs (decr0 p)"
   589   using nb 
   590   by (induct p rule: decr0.induct, simp_all add: decrtm0)
   591 
   592 primrec decr :: "nat \<Rightarrow> fm \<Rightarrow> fm" where
   593   "decr m T = T"
   594 | "decr m F = F"
   595 | "decr m (Lt t) = (Lt (decrtm m t))"
   596 | "decr m (Le t) = (Le (decrtm m t))"
   597 | "decr m (Eq t) = (Eq (decrtm m t))"
   598 | "decr m (NEq t) = (NEq (decrtm m t))"
   599 | "decr m (NOT p) = NOT (decr m p)" 
   600 | "decr m (And p q) = conj (decr m p) (decr m q)"
   601 | "decr m (Or p q) = disj (decr m p) (decr m q)"
   602 | "decr m (Imp p q) = imp (decr m p) (decr m q)"
   603 | "decr m (Iff p q) = iff (decr m p) (decr m q)"
   604 | "decr m (E p) = E (decr (Suc m) p)"
   605 | "decr m (A p) = A (decr (Suc m) p)"
   606 
   607 lemma decr: assumes  bnd: "boundslt (length bs) p" and nb: "bound m p" 
   608   and nle: "m < length bs" 
   609   shows "Ifm vs (removen m bs) (decr m p) = Ifm vs bs p"
   610   using bnd nb nle
   611 proof(induct p arbitrary: bs m rule: fm.induct)
   612   case (E p bs m) 
   613   {fix x
   614     from E have bnd: "boundslt (length (x#bs)) p" and nb: "bound (Suc m) p" 
   615   and nle: "Suc m < length (x#bs)" by auto
   616     from E(1)[OF bnd nb nle] have "Ifm vs (removen (Suc m) (x#bs)) (decr (Suc m) p) = Ifm vs (x#bs) p".
   617   } thus ?case by auto 
   618 next
   619   case (A p bs m)  
   620   {fix x
   621     from A have bnd: "boundslt (length (x#bs)) p" and nb: "bound (Suc m) p" 
   622   and nle: "Suc m < length (x#bs)" by auto
   623     from A(1)[OF bnd nb nle] have "Ifm vs (removen (Suc m) (x#bs)) (decr (Suc m) p) = Ifm vs (x#bs) p".
   624   } thus ?case by auto
   625 qed (auto simp add: decrtm removen_nth)
   626 
   627 primrec subst0:: "tm \<Rightarrow> fm \<Rightarrow> fm" where
   628   "subst0 t T = T"
   629 | "subst0 t F = F"
   630 | "subst0 t (Lt a) = Lt (tmsubst0 t a)"
   631 | "subst0 t (Le a) = Le (tmsubst0 t a)"
   632 | "subst0 t (Eq a) = Eq (tmsubst0 t a)"
   633 | "subst0 t (NEq a) = NEq (tmsubst0 t a)"
   634 | "subst0 t (NOT p) = NOT (subst0 t p)"
   635 | "subst0 t (And p q) = And (subst0 t p) (subst0 t q)"
   636 | "subst0 t (Or p q) = Or (subst0 t p) (subst0 t q)"
   637 | "subst0 t (Imp p q) = Imp (subst0 t p)  (subst0 t q)"
   638 | "subst0 t (Iff p q) = Iff (subst0 t p) (subst0 t q)"
   639 | "subst0 t (E p) = E p"
   640 | "subst0 t (A p) = A p"
   641 
   642 lemma subst0: assumes qf: "qfree p"
   643   shows "Ifm vs (x#bs) (subst0 t p) = Ifm vs ((Itm vs (x#bs) t)#bs) p"
   644 using qf tmsubst0[where x="x" and bs="bs" and t="t"]
   645 by (induct p rule: fm.induct, auto)
   646 
   647 lemma subst0_nb:
   648   assumes bp: "tmbound0 t" and qf: "qfree p"
   649   shows "bound0 (subst0 t p)"
   650 using qf tmsubst0_nb[OF bp] bp
   651 by (induct p rule: fm.induct, auto)
   652 
   653 primrec subst:: "nat \<Rightarrow> tm \<Rightarrow> fm \<Rightarrow> fm" where
   654   "subst n t T = T"
   655 | "subst n t F = F"
   656 | "subst n t (Lt a) = Lt (tmsubst n t a)"
   657 | "subst n t (Le a) = Le (tmsubst n t a)"
   658 | "subst n t (Eq a) = Eq (tmsubst n t a)"
   659 | "subst n t (NEq a) = NEq (tmsubst n t a)"
   660 | "subst n t (NOT p) = NOT (subst n t p)"
   661 | "subst n t (And p q) = And (subst n t p) (subst n t q)"
   662 | "subst n t (Or p q) = Or (subst n t p) (subst n t q)"
   663 | "subst n t (Imp p q) = Imp (subst n t p)  (subst n t q)"
   664 | "subst n t (Iff p q) = Iff (subst n t p) (subst n t q)"
   665 | "subst n t (E p) = E (subst (Suc n) (incrtm0 t) p)"
   666 | "subst n t (A p) = A (subst (Suc n) (incrtm0 t) p)"
   667 
   668 lemma subst: assumes nb: "boundslt (length bs) p" and nlm: "n \<le> length bs"
   669   shows "Ifm vs bs (subst n t p) = Ifm vs (bs[n:= Itm vs bs t]) p"
   670   using nb nlm
   671 proof (induct p arbitrary: bs n t rule: fm.induct)
   672   case (E p bs n) 
   673   {fix x 
   674     from E have bn: "boundslt (length (x#bs)) p" by simp 
   675     from E have nlm: "Suc n \<le> length (x#bs)" by simp
   676     from E(1)[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 
   677     hence "Ifm vs (x#bs) (subst (Suc n) (incrtm0 t) p) = Ifm vs (x#bs[n:= Itm vs bs t]) p"
   678     by (simp add: incrtm0[where x="x" and bs="bs" and t="t"]) }  
   679 thus ?case by simp 
   680 next
   681   case (A p bs n)   
   682   {fix x 
   683     from A have bn: "boundslt (length (x#bs)) p" by simp 
   684     from A have nlm: "Suc n \<le> length (x#bs)" by simp
   685     from A(1)[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 
   686     hence "Ifm vs (x#bs) (subst (Suc n) (incrtm0 t) p) = Ifm vs (x#bs[n:= Itm vs bs t]) p"
   687     by (simp add: incrtm0[where x="x" and bs="bs" and t="t"]) }  
   688 thus ?case by simp 
   689 qed(auto simp add: tmsubst)
   690 
   691 lemma subst_nb: assumes tnb: "tmbound m t"
   692 shows "bound m (subst m t p)"
   693 using tnb tmsubst_nb incrtm0_tmbound
   694 by (induct p arbitrary: m t rule: fm.induct, auto)
   695 
   696 lemma not_qf[simp]: "qfree p \<Longrightarrow> qfree (not p)"
   697 by (induct p rule: not.induct, auto)
   698 lemma not_bn0[simp]: "bound0 p \<Longrightarrow> bound0 (not p)"
   699 by (induct p rule: not.induct, auto)
   700 lemma not_nb[simp]: "bound n p \<Longrightarrow> bound n (not p)"
   701 by (induct p rule: not.induct, auto)
   702 lemma not_blt[simp]: "boundslt n p \<Longrightarrow> boundslt n (not p)"
   703  by (induct p rule: not.induct, auto)
   704 
   705 lemma conj_qf[simp]: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (conj p q)"
   706 using conj_def by auto 
   707 lemma conj_nb0[simp]: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (conj p q)"
   708 using conj_def by auto 
   709 lemma conj_nb[simp]: "\<lbrakk>bound n p ; bound n q\<rbrakk> \<Longrightarrow> bound n (conj p q)"
   710 using conj_def by auto 
   711 lemma conj_blt[simp]: "boundslt n p \<Longrightarrow> boundslt n q \<Longrightarrow> boundslt n (conj p q)"
   712 using conj_def by auto 
   713 
   714 lemma disj_qf[simp]: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (disj p q)"
   715 using disj_def by auto 
   716 lemma disj_nb0[simp]: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (disj p q)"
   717 using disj_def by auto 
   718 lemma disj_nb[simp]: "\<lbrakk>bound n p ; bound n q\<rbrakk> \<Longrightarrow> bound n (disj p q)"
   719 using disj_def by auto 
   720 lemma disj_blt[simp]: "boundslt n p \<Longrightarrow> boundslt n q \<Longrightarrow> boundslt n (disj p q)"
   721 using disj_def by auto 
   722 
   723 lemma imp_qf[simp]: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (imp p q)"
   724 using imp_def by (cases "p=F \<or> q=T",simp_all add: imp_def)
   725 lemma imp_nb0[simp]: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (imp p q)"
   726 using imp_def by (cases "p=F \<or> q=T \<or> p=q",simp_all add: imp_def)
   727 lemma imp_nb[simp]: "\<lbrakk>bound n p ; bound n q\<rbrakk> \<Longrightarrow> bound n (imp p q)"
   728 using imp_def by (cases "p=F \<or> q=T \<or> p=q",simp_all add: imp_def)
   729 lemma imp_blt[simp]: "boundslt n p \<Longrightarrow> boundslt n q \<Longrightarrow> boundslt n (imp p q)"
   730 using imp_def by auto 
   731 
   732 lemma iff_qf[simp]: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (iff p q)"
   733   by (unfold iff_def,cases "p=q", auto)
   734 lemma iff_nb0[simp]: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (iff p q)"
   735 using iff_def by (unfold iff_def,cases "p=q", auto)
   736 lemma iff_nb[simp]: "\<lbrakk>bound n p ; bound n q\<rbrakk> \<Longrightarrow> bound n (iff p q)"
   737 using iff_def by (unfold iff_def,cases "p=q", auto)
   738 lemma iff_blt[simp]: "boundslt n p \<Longrightarrow> boundslt n q \<Longrightarrow> boundslt n (iff p q)"
   739 using iff_def by auto 
   740 lemma decr0_qf: "bound0 p \<Longrightarrow> qfree (decr0 p)"
   741 by (induct p, simp_all)
   742 
   743 consts 
   744   isatom :: "fm \<Rightarrow> bool" (* test for atomicity *)
   745 recdef isatom "measure size"
   746   "isatom T = True"
   747   "isatom F = True"
   748   "isatom (Lt a) = True"
   749   "isatom (Le a) = True"
   750   "isatom (Eq a) = True"
   751   "isatom (NEq a) = True"
   752   "isatom p = False"
   753 
   754 lemma bound0_qf: "bound0 p \<Longrightarrow> qfree p"
   755 by (induct p, simp_all)
   756 
   757 definition djf :: "('a \<Rightarrow> fm) \<Rightarrow> 'a \<Rightarrow> fm \<Rightarrow> fm" where
   758   "djf f p q \<equiv> (if q=T then T else if q=F then f p else 
   759   (let fp = f p in case fp of T \<Rightarrow> T | F \<Rightarrow> q | _ \<Rightarrow> Or (f p) q))"
   760 definition evaldjf :: "('a \<Rightarrow> fm) \<Rightarrow> 'a list \<Rightarrow> fm" where
   761   "evaldjf f ps \<equiv> foldr (djf f) ps F"
   762 
   763 lemma djf_Or: "Ifm vs bs (djf f p q) = Ifm vs bs (Or (f p) q)"
   764 by (cases "q=T", simp add: djf_def,cases "q=F",simp add: djf_def) 
   765 (cases "f p", simp_all add: Let_def djf_def) 
   766 
   767 lemma evaldjf_ex: "Ifm vs bs (evaldjf f ps) = (\<exists> p \<in> set ps. Ifm vs bs (f p))"
   768   by(induct ps, simp_all add: evaldjf_def djf_Or)
   769 
   770 lemma evaldjf_bound0: 
   771   assumes nb: "\<forall> x\<in> set xs. bound0 (f x)"
   772   shows "bound0 (evaldjf f xs)"
   773   using nb by (induct xs, auto simp add: evaldjf_def djf_def Let_def) (case_tac "f a", auto) 
   774 
   775 lemma evaldjf_qf: 
   776   assumes nb: "\<forall> x\<in> set xs. qfree (f x)"
   777   shows "qfree (evaldjf f xs)"
   778   using nb by (induct xs, auto simp add: evaldjf_def djf_def Let_def) (case_tac "f a", auto) 
   779 
   780 consts disjuncts :: "fm \<Rightarrow> fm list"
   781 recdef disjuncts "measure size"
   782   "disjuncts (Or p q) = (disjuncts p) @ (disjuncts q)"
   783   "disjuncts F = []"
   784   "disjuncts p = [p]"
   785 
   786 lemma disjuncts: "(\<exists> q\<in> set (disjuncts p). Ifm vs bs q) = Ifm vs bs p"
   787 by(induct p rule: disjuncts.induct, auto)
   788 
   789 lemma disjuncts_nb: "bound0 p \<Longrightarrow> \<forall> q\<in> set (disjuncts p). bound0 q"
   790 proof-
   791   assume nb: "bound0 p"
   792   hence "list_all bound0 (disjuncts p)" by (induct p rule:disjuncts.induct,auto)
   793   thus ?thesis by (simp only: list_all_iff)
   794 qed
   795 
   796 lemma disjuncts_qf: "qfree p \<Longrightarrow> \<forall> q\<in> set (disjuncts p). qfree q"
   797 proof-
   798   assume qf: "qfree p"
   799   hence "list_all qfree (disjuncts p)"
   800     by (induct p rule: disjuncts.induct, auto)
   801   thus ?thesis by (simp only: list_all_iff)
   802 qed
   803 
   804 definition DJ :: "(fm \<Rightarrow> fm) \<Rightarrow> fm \<Rightarrow> fm" where
   805   "DJ f p \<equiv> evaldjf f (disjuncts p)"
   806 
   807 lemma DJ: assumes fdj: "\<forall> p q. Ifm vs bs (f (Or p q)) = Ifm vs bs (Or (f p) (f q))"
   808   and fF: "f F = F"
   809   shows "Ifm vs bs (DJ f p) = Ifm vs bs (f p)"
   810 proof-
   811   have "Ifm vs bs (DJ f p) = (\<exists> q \<in> set (disjuncts p). Ifm vs bs (f q))"
   812     by (simp add: DJ_def evaldjf_ex) 
   813   also have "\<dots> = Ifm vs bs (f p)" using fdj fF by (induct p rule: disjuncts.induct, auto)
   814   finally show ?thesis .
   815 qed
   816 
   817 lemma DJ_qf: assumes 
   818   fqf: "\<forall> p. qfree p \<longrightarrow> qfree (f p)"
   819   shows "\<forall>p. qfree p \<longrightarrow> qfree (DJ f p) "
   820 proof(clarify)
   821   fix  p assume qf: "qfree p"
   822   have th: "DJ f p = evaldjf f (disjuncts p)" by (simp add: DJ_def)
   823   from disjuncts_qf[OF qf] have "\<forall> q\<in> set (disjuncts p). qfree q" .
   824   with fqf have th':"\<forall> q\<in> set (disjuncts p). qfree (f q)" by blast
   825   
   826   from evaldjf_qf[OF th'] th show "qfree (DJ f p)" by simp
   827 qed
   828 
   829 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))"
   830   shows "\<forall> bs p. qfree p \<longrightarrow> qfree (DJ qe p) \<and> (Ifm vs bs ((DJ qe p)) = Ifm vs bs (E p))"
   831 proof(clarify)
   832   fix p::fm and bs
   833   assume qf: "qfree p"
   834   from qe have qth: "\<forall> p. qfree p \<longrightarrow> qfree (qe p)" by blast
   835   from DJ_qf[OF qth] qf have qfth:"qfree (DJ qe p)" by auto
   836   have "Ifm vs bs (DJ qe p) = (\<exists> q\<in> set (disjuncts p). Ifm vs bs (qe q))"
   837     by (simp add: DJ_def evaldjf_ex)
   838   also have "\<dots> = (\<exists> q \<in> set(disjuncts p). Ifm vs bs (E q))" using qe disjuncts_qf[OF qf] by auto
   839   also have "\<dots> = Ifm vs bs (E p)" by (induct p rule: disjuncts.induct, auto)
   840   finally show "qfree (DJ qe p) \<and> Ifm vs bs (DJ qe p) = Ifm vs bs (E p)" using qfth by blast
   841 qed
   842 
   843 consts conjuncts :: "fm \<Rightarrow> fm list"
   844 
   845 recdef conjuncts "measure size"
   846   "conjuncts (And p q) = (conjuncts p) @ (conjuncts q)"
   847   "conjuncts T = []"
   848   "conjuncts p = [p]"
   849 
   850 definition list_conj :: "fm list \<Rightarrow> fm" where
   851   "list_conj ps \<equiv> foldr conj ps T"
   852 
   853 definition CJNB :: "(fm \<Rightarrow> fm) \<Rightarrow> fm \<Rightarrow> fm" where
   854   "CJNB f p \<equiv> (let cjs = conjuncts p ; (yes,no) = partition bound0 cjs
   855                    in conj (decr0 (list_conj yes)) (f (list_conj no)))"
   856 
   857 lemma conjuncts_qf: "qfree p \<Longrightarrow> \<forall> q\<in> set (conjuncts p). qfree q"
   858 proof-
   859   assume qf: "qfree p"
   860   hence "list_all qfree (conjuncts p)"
   861     by (induct p rule: conjuncts.induct, auto)
   862   thus ?thesis by (simp only: list_all_iff)
   863 qed
   864 
   865 lemma conjuncts: "(\<forall> q\<in> set (conjuncts p). Ifm vs bs q) = Ifm vs bs p"
   866 by(induct p rule: conjuncts.induct, auto)
   867 
   868 lemma conjuncts_nb: "bound0 p \<Longrightarrow> \<forall> q\<in> set (conjuncts p). bound0 q"
   869 proof-
   870   assume nb: "bound0 p"
   871   hence "list_all bound0 (conjuncts p)" by (induct p rule:conjuncts.induct,auto)
   872   thus ?thesis by (simp only: list_all_iff)
   873 qed
   874 
   875 fun islin :: "fm \<Rightarrow> bool" where
   876   "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)"
   877 | "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)"
   878 | "islin (Eq (CNP 0 c s)) = (isnpoly c \<and> c \<noteq> 0\<^sub>p \<and> tmbound0 s \<and> allpolys isnpoly s)"
   879 | "islin (NEq (CNP 0 c s)) = (isnpoly c \<and> c \<noteq> 0\<^sub>p \<and> tmbound0 s \<and> allpolys isnpoly s)"
   880 | "islin (Lt (CNP 0 c s)) = (isnpoly c \<and> c \<noteq> 0\<^sub>p \<and> tmbound0 s \<and> allpolys isnpoly s)"
   881 | "islin (Le (CNP 0 c s)) = (isnpoly c \<and> c \<noteq> 0\<^sub>p \<and> tmbound0 s \<and> allpolys isnpoly s)"
   882 | "islin (NOT p) = False"
   883 | "islin (Imp p q) = False"
   884 | "islin (Iff p q) = False"
   885 | "islin p = bound0 p"
   886 
   887 lemma islin_stupid: assumes nb: "tmbound0 p"
   888   shows "islin (Lt p)" and "islin (Le p)" and "islin (Eq p)" and "islin (NEq p)"
   889   using nb by (cases p, auto, case_tac nat, auto)+
   890 
   891 definition "lt p = (case p of CP (C c) \<Rightarrow> if 0>\<^sub>N c then T else F| _ \<Rightarrow> Lt p)"
   892 definition "le p = (case p of CP (C c) \<Rightarrow> if 0\<ge>\<^sub>N c then T else F | _ \<Rightarrow> Le p)"
   893 definition eq where "eq p = (case p of CP (C c) \<Rightarrow> if c = 0\<^sub>N then T else F | _ \<Rightarrow> Eq p)"
   894 definition "neq p = not (eq p)"
   895 
   896 lemma lt: "allpolys isnpoly p \<Longrightarrow> Ifm vs bs (lt p) = Ifm vs bs (Lt p)"
   897   apply(simp add: lt_def)
   898   apply(cases p, simp_all)
   899   apply (case_tac poly, simp_all add: isnpoly_def)
   900   done
   901 
   902 lemma le: "allpolys isnpoly p \<Longrightarrow> Ifm vs bs (le p) = Ifm vs bs (Le p)"
   903   apply(simp add: le_def)
   904   apply(cases p, simp_all)
   905   apply (case_tac poly, simp_all add: isnpoly_def)
   906   done
   907 
   908 lemma eq: "allpolys isnpoly p \<Longrightarrow> Ifm vs bs (eq p) = Ifm vs bs (Eq p)"
   909   apply(simp add: eq_def)
   910   apply(cases p, simp_all)
   911   apply (case_tac poly, simp_all add: isnpoly_def)
   912   done
   913 
   914 lemma neq: "allpolys isnpoly p \<Longrightarrow> Ifm vs bs (neq p) = Ifm vs bs (NEq p)"
   915   by(simp add: neq_def eq)
   916 
   917 lemma lt_lin: "tmbound0 p \<Longrightarrow> islin (lt p)"
   918   apply (simp add: lt_def)
   919   apply (cases p, simp_all)
   920   apply (case_tac poly, simp_all)
   921   apply (case_tac nat, simp_all)
   922   done
   923 
   924 lemma le_lin: "tmbound0 p \<Longrightarrow> islin (le p)"
   925   apply (simp add: le_def)
   926   apply (cases p, simp_all)
   927   apply (case_tac poly, simp_all)
   928   apply (case_tac nat, simp_all)
   929   done
   930 
   931 lemma eq_lin: "tmbound0 p \<Longrightarrow> islin (eq p)"
   932   apply (simp add: eq_def)
   933   apply (cases p, simp_all)
   934   apply (case_tac poly, simp_all)
   935   apply (case_tac nat, simp_all)
   936   done
   937 
   938 lemma neq_lin: "tmbound0 p \<Longrightarrow> islin (neq p)"
   939   apply (simp add: neq_def eq_def)
   940   apply (cases p, simp_all)
   941   apply (case_tac poly, simp_all)
   942   apply (case_tac nat, simp_all)
   943   done
   944 
   945 definition "simplt t = (let (c,s) = split0 (simptm t) in if c= 0\<^sub>p then lt s else Lt (CNP 0 c s))"
   946 definition "simple t = (let (c,s) = split0 (simptm t) in if c= 0\<^sub>p then le s else Le (CNP 0 c s))"
   947 definition "simpeq t = (let (c,s) = split0 (simptm t) in if c= 0\<^sub>p then eq s else Eq (CNP 0 c s))"
   948 definition "simpneq t = (let (c,s) = split0 (simptm t) in if c= 0\<^sub>p then neq s else NEq (CNP 0 c s))"
   949 
   950 lemma simplt_islin[simp]:   assumes "SORT_CONSTRAINT('a::{field_char_0, field_inverse_zero})"
   951   shows "islin (simplt t)"
   952   unfolding simplt_def 
   953   using split0_nb0'
   954 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])
   955   
   956 lemma simple_islin[simp]:   assumes "SORT_CONSTRAINT('a::{field_char_0, field_inverse_zero})"
   957   shows "islin (simple t)"
   958   unfolding simple_def 
   959   using split0_nb0'
   960 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)
   961 lemma simpeq_islin[simp]:   assumes "SORT_CONSTRAINT('a::{field_char_0, field_inverse_zero})"
   962   shows "islin (simpeq t)"
   963   unfolding simpeq_def 
   964   using split0_nb0'
   965 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)
   966 
   967 lemma simpneq_islin[simp]:   assumes "SORT_CONSTRAINT('a::{field_char_0, field_inverse_zero})"
   968   shows "islin (simpneq t)"
   969   unfolding simpneq_def 
   970   using split0_nb0'
   971 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)
   972 
   973 lemma really_stupid: "\<not> (\<forall>c1 s'. (c1, s') \<noteq> split0 s)"
   974   by (cases "split0 s", auto)
   975 lemma split0_npoly:   assumes "SORT_CONSTRAINT('a::{field_char_0, field_inverse_zero})"
   976   and n: "allpolys isnpoly t"
   977   shows "isnpoly (fst (split0 t))" and "allpolys isnpoly (snd (split0 t))"
   978   using n
   979   by (induct t rule: split0.induct, auto simp add: Let_def split_def polyadd_norm polymul_norm polyneg_norm polysub_norm really_stupid)
   980 lemma simplt[simp]:
   981   shows "Ifm vs bs (simplt t) = Ifm vs bs (Lt t)"
   982 proof-
   983   have n: "allpolys isnpoly (simptm t)" by simp
   984   let ?t = "simptm t"
   985   {assume "fst (split0 ?t) = 0\<^sub>p" hence ?thesis
   986       using split0[of "simptm t" vs bs] lt[OF split0_npoly(2)[OF n], of vs bs]
   987       by (simp add: simplt_def Let_def split_def lt)}
   988   moreover
   989   {assume "fst (split0 ?t) \<noteq> 0\<^sub>p"
   990     hence ?thesis using  split0[of "simptm t" vs bs] by (simp add: simplt_def Let_def split_def)
   991   }
   992   ultimately show ?thesis by blast
   993 qed
   994 
   995 lemma simple[simp]:
   996   shows "Ifm vs bs (simple t) = Ifm vs bs (Le t)"
   997 proof-
   998   have n: "allpolys isnpoly (simptm t)" by simp
   999   let ?t = "simptm t"
  1000   {assume "fst (split0 ?t) = 0\<^sub>p" hence ?thesis
  1001       using split0[of "simptm t" vs bs] le[OF split0_npoly(2)[OF n], of vs bs]
  1002       by (simp add: simple_def Let_def split_def le)}
  1003   moreover
  1004   {assume "fst (split0 ?t) \<noteq> 0\<^sub>p"
  1005     hence ?thesis using  split0[of "simptm t" vs bs] by (simp add: simple_def Let_def split_def)
  1006   }
  1007   ultimately show ?thesis by blast
  1008 qed
  1009 
  1010 lemma simpeq[simp]:
  1011   shows "Ifm vs bs (simpeq t) = Ifm vs bs (Eq t)"
  1012 proof-
  1013   have n: "allpolys isnpoly (simptm t)" by simp
  1014   let ?t = "simptm t"
  1015   {assume "fst (split0 ?t) = 0\<^sub>p" hence ?thesis
  1016       using split0[of "simptm t" vs bs] eq[OF split0_npoly(2)[OF n], of vs bs]
  1017       by (simp add: simpeq_def Let_def split_def)}
  1018   moreover
  1019   {assume "fst (split0 ?t) \<noteq> 0\<^sub>p"
  1020     hence ?thesis using  split0[of "simptm t" vs bs] by (simp add: simpeq_def Let_def split_def)
  1021   }
  1022   ultimately show ?thesis by blast
  1023 qed
  1024 
  1025 lemma simpneq[simp]:
  1026   shows "Ifm vs bs (simpneq t) = Ifm vs bs (NEq t)"
  1027 proof-
  1028   have n: "allpolys isnpoly (simptm t)" by simp
  1029   let ?t = "simptm t"
  1030   {assume "fst (split0 ?t) = 0\<^sub>p" hence ?thesis
  1031       using split0[of "simptm t" vs bs] neq[OF split0_npoly(2)[OF n], of vs bs]
  1032       by (simp add: simpneq_def Let_def split_def )}
  1033   moreover
  1034   {assume "fst (split0 ?t) \<noteq> 0\<^sub>p"
  1035     hence ?thesis using  split0[of "simptm t" vs bs] by (simp add: simpneq_def Let_def split_def)
  1036   }
  1037   ultimately show ?thesis by blast
  1038 qed
  1039 
  1040 lemma lt_nb: "tmbound0 t \<Longrightarrow> bound0 (lt t)"
  1041   apply (simp add: lt_def)
  1042   apply (cases t, auto)
  1043   apply (case_tac poly, auto)
  1044   done
  1045 
  1046 lemma le_nb: "tmbound0 t \<Longrightarrow> bound0 (le t)"
  1047   apply (simp add: le_def)
  1048   apply (cases t, auto)
  1049   apply (case_tac poly, auto)
  1050   done
  1051 
  1052 lemma eq_nb: "tmbound0 t \<Longrightarrow> bound0 (eq t)"
  1053   apply (simp add: eq_def)
  1054   apply (cases t, auto)
  1055   apply (case_tac poly, auto)
  1056   done
  1057 
  1058 lemma neq_nb: "tmbound0 t \<Longrightarrow> bound0 (neq t)"
  1059   apply (simp add: neq_def eq_def)
  1060   apply (cases t, auto)
  1061   apply (case_tac poly, auto)
  1062   done
  1063 
  1064 lemma simplt_nb[simp]:   assumes "SORT_CONSTRAINT('a::{field_char_0, field_inverse_zero})"
  1065   shows "tmbound0 t \<Longrightarrow> bound0 (simplt t)"
  1066   using split0 [of "simptm t" vs bs]
  1067 proof(simp add: simplt_def Let_def split_def)
  1068   assume nb: "tmbound0 t"
  1069   hence nb': "tmbound0 (simptm t)" by simp
  1070   let ?c = "fst (split0 (simptm t))"
  1071   from tmbound_split0[OF nb'[unfolded tmbound0_tmbound_iff[symmetric]]]
  1072   have th: "\<forall>bs. Ipoly bs ?c = Ipoly bs 0\<^sub>p" by auto
  1073   from isnpoly_fst_split0[OF simptm_allpolys_npoly[of t]]
  1074   have ths: "isnpolyh ?c 0" "isnpolyh 0\<^sub>p 0" by (simp_all add: isnpoly_def)
  1075   from iffD1[OF isnpolyh_unique[OF ths] th]
  1076   have "fst (split0 (simptm t)) = 0\<^sub>p" . 
  1077   thus "(fst (split0 (simptm t)) = 0\<^sub>p \<longrightarrow> bound0 (lt (snd (split0 (simptm t))))) \<and>
  1078        fst (split0 (simptm t)) = 0\<^sub>p" by (simp add: simplt_def Let_def split_def lt_nb)
  1079 qed
  1080 
  1081 lemma simple_nb[simp]:   assumes "SORT_CONSTRAINT('a::{field_char_0, field_inverse_zero})"
  1082   shows "tmbound0 t \<Longrightarrow> bound0 (simple t)"
  1083   using split0 [of "simptm t" vs bs]
  1084 proof(simp add: simple_def Let_def split_def)
  1085   assume nb: "tmbound0 t"
  1086   hence nb': "tmbound0 (simptm t)" by simp
  1087   let ?c = "fst (split0 (simptm t))"
  1088   from tmbound_split0[OF nb'[unfolded tmbound0_tmbound_iff[symmetric]]]
  1089   have th: "\<forall>bs. Ipoly bs ?c = Ipoly bs 0\<^sub>p" by auto
  1090   from isnpoly_fst_split0[OF simptm_allpolys_npoly[of t]]
  1091   have ths: "isnpolyh ?c 0" "isnpolyh 0\<^sub>p 0" by (simp_all add: isnpoly_def)
  1092   from iffD1[OF isnpolyh_unique[OF ths] th]
  1093   have "fst (split0 (simptm t)) = 0\<^sub>p" . 
  1094   thus "(fst (split0 (simptm t)) = 0\<^sub>p \<longrightarrow> bound0 (le (snd (split0 (simptm t))))) \<and>
  1095        fst (split0 (simptm t)) = 0\<^sub>p" by (simp add: simplt_def Let_def split_def le_nb)
  1096 qed
  1097 
  1098 lemma simpeq_nb[simp]:   assumes "SORT_CONSTRAINT('a::{field_char_0, field_inverse_zero})"
  1099   shows "tmbound0 t \<Longrightarrow> bound0 (simpeq t)"
  1100   using split0 [of "simptm t" vs bs]
  1101 proof(simp add: simpeq_def Let_def split_def)
  1102   assume nb: "tmbound0 t"
  1103   hence nb': "tmbound0 (simptm t)" by simp
  1104   let ?c = "fst (split0 (simptm t))"
  1105   from tmbound_split0[OF nb'[unfolded tmbound0_tmbound_iff[symmetric]]]
  1106   have th: "\<forall>bs. Ipoly bs ?c = Ipoly bs 0\<^sub>p" by auto
  1107   from isnpoly_fst_split0[OF simptm_allpolys_npoly[of t]]
  1108   have ths: "isnpolyh ?c 0" "isnpolyh 0\<^sub>p 0" by (simp_all add: isnpoly_def)
  1109   from iffD1[OF isnpolyh_unique[OF ths] th]
  1110   have "fst (split0 (simptm t)) = 0\<^sub>p" . 
  1111   thus "(fst (split0 (simptm t)) = 0\<^sub>p \<longrightarrow> bound0 (eq (snd (split0 (simptm t))))) \<and>
  1112        fst (split0 (simptm t)) = 0\<^sub>p" by (simp add: simpeq_def Let_def split_def eq_nb)
  1113 qed
  1114 
  1115 lemma simpneq_nb[simp]:   assumes "SORT_CONSTRAINT('a::{field_char_0, field_inverse_zero})"
  1116   shows "tmbound0 t \<Longrightarrow> bound0 (simpneq t)"
  1117   using split0 [of "simptm t" vs bs]
  1118 proof(simp add: simpneq_def Let_def split_def)
  1119   assume nb: "tmbound0 t"
  1120   hence nb': "tmbound0 (simptm t)" by simp
  1121   let ?c = "fst (split0 (simptm t))"
  1122   from tmbound_split0[OF nb'[unfolded tmbound0_tmbound_iff[symmetric]]]
  1123   have th: "\<forall>bs. Ipoly bs ?c = Ipoly bs 0\<^sub>p" by auto
  1124   from isnpoly_fst_split0[OF simptm_allpolys_npoly[of t]]
  1125   have ths: "isnpolyh ?c 0" "isnpolyh 0\<^sub>p 0" by (simp_all add: isnpoly_def)
  1126   from iffD1[OF isnpolyh_unique[OF ths] th]
  1127   have "fst (split0 (simptm t)) = 0\<^sub>p" . 
  1128   thus "(fst (split0 (simptm t)) = 0\<^sub>p \<longrightarrow> bound0 (neq (snd (split0 (simptm t))))) \<and>
  1129        fst (split0 (simptm t)) = 0\<^sub>p" by (simp add: simpneq_def Let_def split_def neq_nb)
  1130 qed
  1131 
  1132 consts conjs   :: "fm \<Rightarrow> fm list"
  1133 recdef conjs "measure size"
  1134   "conjs (And p q) = (conjs p)@(conjs q)"
  1135   "conjs T = []"
  1136   "conjs p = [p]"
  1137 lemma conjs_ci: "(\<forall> q \<in> set (conjs p). Ifm vs bs q) = Ifm vs bs p"
  1138 by (induct p rule: conjs.induct, auto)
  1139 definition list_disj :: "fm list \<Rightarrow> fm" where
  1140   "list_disj ps \<equiv> foldr disj ps F"
  1141 
  1142 lemma list_conj: "Ifm vs bs (list_conj ps) = (\<forall>p\<in> set ps. Ifm vs bs p)"
  1143   by (induct ps, auto simp add: list_conj_def)
  1144 lemma list_conj_qf: " \<forall>p\<in> set ps. qfree p \<Longrightarrow> qfree (list_conj ps)"
  1145   by (induct ps, auto simp add: list_conj_def conj_qf)
  1146 lemma list_disj: "Ifm vs bs (list_disj ps) = (\<exists>p\<in> set ps. Ifm vs bs p)"
  1147   by (induct ps, auto simp add: list_disj_def)
  1148 
  1149 lemma conj_boundslt: "boundslt n p \<Longrightarrow> boundslt n q \<Longrightarrow> boundslt n (conj p q)"
  1150   unfolding conj_def by auto
  1151 
  1152 lemma conjs_nb: "bound n p \<Longrightarrow> \<forall>q\<in> set (conjs p). bound n q"
  1153   apply (induct p rule: conjs.induct) 
  1154   apply (unfold conjs.simps)
  1155   apply (unfold set_append)
  1156   apply (unfold ball_Un)
  1157   apply (unfold bound.simps)
  1158   apply auto
  1159   done
  1160 
  1161 lemma conjs_boundslt: "boundslt n p \<Longrightarrow> \<forall>q\<in> set (conjs p). boundslt n q"
  1162   apply (induct p rule: conjs.induct) 
  1163   apply (unfold conjs.simps)
  1164   apply (unfold set_append)
  1165   apply (unfold ball_Un)
  1166   apply (unfold boundslt.simps)
  1167   apply blast
  1168 by simp_all
  1169 
  1170 lemma list_conj_boundslt: " \<forall>p\<in> set ps. boundslt n p \<Longrightarrow> boundslt n (list_conj ps)"
  1171   unfolding list_conj_def
  1172   by (induct ps, auto simp add: conj_boundslt)
  1173 
  1174 lemma list_conj_nb: assumes bnd: "\<forall>p\<in> set ps. bound n p"
  1175   shows "bound n (list_conj ps)"
  1176   using bnd
  1177   unfolding list_conj_def
  1178   by (induct ps, auto simp add: conj_nb)
  1179 
  1180 lemma list_conj_nb': "\<forall>p\<in>set ps. bound0 p \<Longrightarrow> bound0 (list_conj ps)"
  1181 unfolding list_conj_def by (induct ps , auto)
  1182 
  1183 lemma CJNB_qe: 
  1184   assumes qe: "\<forall> bs p. qfree p \<longrightarrow> qfree (qe p) \<and> (Ifm vs bs (qe p) = Ifm vs bs (E p))"
  1185   shows "\<forall> bs p. qfree p \<longrightarrow> qfree (CJNB qe p) \<and> (Ifm vs bs ((CJNB qe p)) = Ifm vs bs (E p))"
  1186 proof(clarify)
  1187   fix bs p
  1188   assume qfp: "qfree p"
  1189   let ?cjs = "conjuncts p"
  1190   let ?yes = "fst (partition bound0 ?cjs)"
  1191   let ?no = "snd (partition bound0 ?cjs)"
  1192   let ?cno = "list_conj ?no"
  1193   let ?cyes = "list_conj ?yes"
  1194   have part: "partition bound0 ?cjs = (?yes,?no)" by simp
  1195   from partition_P[OF part] have "\<forall> q\<in> set ?yes. bound0 q" by blast 
  1196   hence yes_nb: "bound0 ?cyes" by (simp add: list_conj_nb') 
  1197   hence yes_qf: "qfree (decr0 ?cyes )" by (simp add: decr0_qf)
  1198   from conjuncts_qf[OF qfp] partition_set[OF part] 
  1199   have " \<forall>q\<in> set ?no. qfree q" by auto
  1200   hence no_qf: "qfree ?cno"by (simp add: list_conj_qf)
  1201   with qe have cno_qf:"qfree (qe ?cno )" 
  1202     and noE: "Ifm vs bs (qe ?cno) = Ifm vs bs (E ?cno)" by blast+
  1203   from cno_qf yes_qf have qf: "qfree (CJNB qe p)" 
  1204     by (simp add: CJNB_def Let_def conj_qf split_def)
  1205   {fix bs
  1206     from conjuncts have "Ifm vs bs p = (\<forall>q\<in> set ?cjs. Ifm vs bs q)" by blast
  1207     also have "\<dots> = ((\<forall>q\<in> set ?yes. Ifm vs bs q) \<and> (\<forall>q\<in> set ?no. Ifm vs bs q))"
  1208       using partition_set[OF part] by auto
  1209     finally have "Ifm vs bs p = ((Ifm vs bs ?cyes) \<and> (Ifm vs bs ?cno))" using list_conj[of vs bs] by simp}
  1210   hence "Ifm vs bs (E p) = (\<exists>x. (Ifm vs (x#bs) ?cyes) \<and> (Ifm vs (x#bs) ?cno))" by simp
  1211   also have "\<dots> = (\<exists>x. (Ifm vs (y#bs) ?cyes) \<and> (Ifm vs (x#bs) ?cno))"
  1212     using bound0_I[OF yes_nb, where bs="bs" and b'="y"] by blast
  1213   also have "\<dots> = (Ifm vs bs (decr0 ?cyes) \<and> Ifm vs bs (E ?cno))"
  1214     by (auto simp add: decr0[OF yes_nb] simp del: partition_filter_conv)
  1215   also have "\<dots> = (Ifm vs bs (conj (decr0 ?cyes) (qe ?cno)))"
  1216     using qe[rule_format, OF no_qf] by auto
  1217   finally have "Ifm vs bs (E p) = Ifm vs bs (CJNB qe p)" 
  1218     by (simp add: Let_def CJNB_def split_def)
  1219   with qf show "qfree (CJNB qe p) \<and> Ifm vs bs (CJNB qe p) = Ifm vs bs (E p)" by blast
  1220 qed
  1221 
  1222 consts simpfm :: "fm \<Rightarrow> fm"
  1223 recdef simpfm "measure fmsize"
  1224   "simpfm (Lt t) = simplt (simptm t)"
  1225   "simpfm (Le t) = simple (simptm t)"
  1226   "simpfm (Eq t) = simpeq(simptm t)"
  1227   "simpfm (NEq t) = simpneq(simptm t)"
  1228   "simpfm (And p q) = conj (simpfm p) (simpfm q)"
  1229   "simpfm (Or p q) = disj (simpfm p) (simpfm q)"
  1230   "simpfm (Imp p q) = disj (simpfm (NOT p)) (simpfm q)"
  1231   "simpfm (Iff p q) = disj (conj (simpfm p) (simpfm q)) (conj (simpfm (NOT p)) (simpfm (NOT q)))"
  1232   "simpfm (NOT (And p q)) = disj (simpfm (NOT p)) (simpfm (NOT q))"
  1233   "simpfm (NOT (Or p q)) = conj (simpfm (NOT p)) (simpfm (NOT q))"
  1234   "simpfm (NOT (Imp p q)) = conj (simpfm p) (simpfm (NOT q))"
  1235   "simpfm (NOT (Iff p q)) = disj (conj (simpfm p) (simpfm (NOT q))) (conj (simpfm (NOT p)) (simpfm q))"
  1236   "simpfm (NOT (Eq t)) = simpneq t"
  1237   "simpfm (NOT (NEq t)) = simpeq t"
  1238   "simpfm (NOT (Le t)) = simplt (Neg t)"
  1239   "simpfm (NOT (Lt t)) = simple (Neg t)"
  1240   "simpfm (NOT (NOT p)) = simpfm p"
  1241   "simpfm (NOT T) = F"
  1242   "simpfm (NOT F) = T"
  1243   "simpfm p = p"
  1244 
  1245 lemma simpfm[simp]: "Ifm vs bs (simpfm p) = Ifm vs bs p"
  1246 by(induct p arbitrary: bs rule: simpfm.induct, auto)
  1247 
  1248 lemma simpfm_bound0:   assumes "SORT_CONSTRAINT('a::{field_char_0, field_inverse_zero})"
  1249   shows "bound0 p \<Longrightarrow> bound0 (simpfm p)"
  1250 by (induct p rule: simpfm.induct, auto)
  1251 
  1252 lemma lt_qf[simp]: "qfree (lt t)"
  1253   apply (cases t, auto simp add: lt_def)
  1254   by (case_tac poly, auto)
  1255 
  1256 lemma le_qf[simp]: "qfree (le t)"
  1257   apply (cases t, auto simp add: le_def)
  1258   by (case_tac poly, auto)
  1259 
  1260 lemma eq_qf[simp]: "qfree (eq t)"
  1261   apply (cases t, auto simp add: eq_def)
  1262   by (case_tac poly, auto)
  1263 
  1264 lemma neq_qf[simp]: "qfree (neq t)" by (simp add: neq_def)
  1265 
  1266 lemma simplt_qf[simp]: "qfree (simplt t)" by (simp add: simplt_def Let_def split_def)
  1267 lemma simple_qf[simp]: "qfree (simple t)" by (simp add: simple_def Let_def split_def)
  1268 lemma simpeq_qf[simp]: "qfree (simpeq t)" by (simp add: simpeq_def Let_def split_def)
  1269 lemma simpneq_qf[simp]: "qfree (simpneq t)" by (simp add: simpneq_def Let_def split_def)
  1270 
  1271 lemma simpfm_qf[simp]: "qfree p \<Longrightarrow> qfree (simpfm p)"
  1272 by (induct p rule: simpfm.induct, auto simp add: disj_qf imp_qf iff_qf conj_qf not_qf Let_def)
  1273 
  1274 lemma disj_lin: "islin p \<Longrightarrow> islin q \<Longrightarrow> islin (disj p q)" by (simp add: disj_def)
  1275 lemma conj_lin: "islin p \<Longrightarrow> islin q \<Longrightarrow> islin (conj p q)" by (simp add: conj_def)
  1276 
  1277 lemma   assumes "SORT_CONSTRAINT('a::{field_char_0, field_inverse_zero})"
  1278   shows "qfree p \<Longrightarrow> islin (simpfm p)" 
  1279   apply (induct p rule: simpfm.induct)
  1280   apply (simp_all add: conj_lin disj_lin)
  1281   done
  1282 
  1283 consts prep :: "fm \<Rightarrow> fm"
  1284 recdef prep "measure fmsize"
  1285   "prep (E T) = T"
  1286   "prep (E F) = F"
  1287   "prep (E (Or p q)) = disj (prep (E p)) (prep (E q))"
  1288   "prep (E (Imp p q)) = disj (prep (E (NOT p))) (prep (E q))"
  1289   "prep (E (Iff p q)) = disj (prep (E (And p q))) (prep (E (And (NOT p) (NOT q))))" 
  1290   "prep (E (NOT (And p q))) = disj (prep (E (NOT p))) (prep (E(NOT q)))"
  1291   "prep (E (NOT (Imp p q))) = prep (E (And p (NOT q)))"
  1292   "prep (E (NOT (Iff p q))) = disj (prep (E (And p (NOT q)))) (prep (E(And (NOT p) q)))"
  1293   "prep (E p) = E (prep p)"
  1294   "prep (A (And p q)) = conj (prep (A p)) (prep (A q))"
  1295   "prep (A p) = prep (NOT (E (NOT p)))"
  1296   "prep (NOT (NOT p)) = prep p"
  1297   "prep (NOT (And p q)) = disj (prep (NOT p)) (prep (NOT q))"
  1298   "prep (NOT (A p)) = prep (E (NOT p))"
  1299   "prep (NOT (Or p q)) = conj (prep (NOT p)) (prep (NOT q))"
  1300   "prep (NOT (Imp p q)) = conj (prep p) (prep (NOT q))"
  1301   "prep (NOT (Iff p q)) = disj (prep (And p (NOT q))) (prep (And (NOT p) q))"
  1302   "prep (NOT p) = not (prep p)"
  1303   "prep (Or p q) = disj (prep p) (prep q)"
  1304   "prep (And p q) = conj (prep p) (prep q)"
  1305   "prep (Imp p q) = prep (Or (NOT p) q)"
  1306   "prep (Iff p q) = disj (prep (And p q)) (prep (And (NOT p) (NOT q)))"
  1307   "prep p = p"
  1308 (hints simp add: fmsize_pos)
  1309 lemma prep: "Ifm vs bs (prep p) = Ifm vs bs p"
  1310 by (induct p arbitrary: bs rule: prep.induct, auto)
  1311 
  1312 
  1313 
  1314   (* Generic quantifier elimination *)
  1315 consts qelim :: "fm \<Rightarrow> (fm \<Rightarrow> fm) \<Rightarrow> fm"
  1316 recdef qelim "measure fmsize"
  1317   "qelim (E p) = (\<lambda> qe. DJ (CJNB qe) (qelim p qe))"
  1318   "qelim (A p) = (\<lambda> qe. not (qe ((qelim (NOT p) qe))))"
  1319   "qelim (NOT p) = (\<lambda> qe. not (qelim p qe))"
  1320   "qelim (And p q) = (\<lambda> qe. conj (qelim p qe) (qelim q qe))" 
  1321   "qelim (Or  p q) = (\<lambda> qe. disj (qelim p qe) (qelim q qe))" 
  1322   "qelim (Imp p q) = (\<lambda> qe. imp (qelim p qe) (qelim q qe))"
  1323   "qelim (Iff p q) = (\<lambda> qe. iff (qelim p qe) (qelim q qe))"
  1324   "qelim p = (\<lambda> y. simpfm p)"
  1325 
  1326 
  1327 lemma qelim:
  1328   assumes qe_inv: "\<forall> bs p. qfree p \<longrightarrow> qfree (qe p) \<and> (Ifm vs bs (qe p) = Ifm vs bs (E p))"
  1329   shows "\<And> bs. qfree (qelim p qe) \<and> (Ifm vs bs (qelim p qe) = Ifm vs bs p)"
  1330 using qe_inv DJ_qe[OF CJNB_qe[OF qe_inv]]
  1331 by (induct p rule: qelim.induct) auto
  1332 
  1333 subsection{* Core Procedure *}
  1334 
  1335 consts 
  1336   plusinf:: "fm \<Rightarrow> fm" (* Virtual substitution of +\<infinity>*)
  1337   minusinf:: "fm \<Rightarrow> fm" (* Virtual substitution of -\<infinity>*)
  1338 recdef minusinf "measure size"
  1339   "minusinf (And p q) = conj (minusinf p) (minusinf q)" 
  1340   "minusinf (Or p q) = disj (minusinf p) (minusinf q)" 
  1341   "minusinf (Eq  (CNP 0 c e)) = conj (eq (CP c)) (eq e)"
  1342   "minusinf (NEq (CNP 0 c e)) = disj (not (eq e)) (not (eq (CP c)))"
  1343   "minusinf (Lt  (CNP 0 c e)) = disj (conj (eq (CP c)) (lt e)) (lt (CP (~\<^sub>p c)))"
  1344   "minusinf (Le  (CNP 0 c e)) = disj (conj (eq (CP c)) (le e)) (lt (CP (~\<^sub>p c)))"
  1345   "minusinf p = p"
  1346 
  1347 recdef plusinf "measure size"
  1348   "plusinf (And p q) = conj (plusinf p) (plusinf q)" 
  1349   "plusinf (Or p q) = disj (plusinf p) (plusinf q)" 
  1350   "plusinf (Eq  (CNP 0 c e)) = conj (eq (CP c)) (eq e)"
  1351   "plusinf (NEq (CNP 0 c e)) = disj (not (eq e)) (not (eq (CP c)))"
  1352   "plusinf (Lt  (CNP 0 c e)) = disj (conj (eq (CP c)) (lt e)) (lt (CP c))"
  1353   "plusinf (Le  (CNP 0 c e)) = disj (conj (eq (CP c)) (le e)) (lt (CP c))"
  1354   "plusinf p = p"
  1355 
  1356 lemma minusinf_inf: assumes lp:"islin p"
  1357   shows "\<exists>z. \<forall>x < z. Ifm vs (x#bs) (minusinf p) \<longleftrightarrow> Ifm vs (x#bs) p"
  1358   using lp
  1359 proof (induct p rule: minusinf.induct)
  1360   case 1 thus ?case by (auto,rule_tac x="min z za" in exI, auto)
  1361 next
  1362   case 2 thus ?case by (auto,rule_tac x="min z za" in exI, auto)
  1363 next
  1364   case (3 c e) hence nbe: "tmbound0 e" by simp
  1365   from 3 have nc: "allpolys isnpoly (CP c)" "allpolys isnpoly e" by simp_all
  1366   note eqs = eq[OF nc(1), where ?'a = 'a] eq[OF nc(2), where ?'a = 'a]
  1367   let ?c = "Ipoly vs c"
  1368   let ?e = "Itm vs (y#bs) e"
  1369   have "?c=0 \<or> ?c > 0 \<or> ?c < 0" by arith
  1370   moreover {assume "?c = 0" hence ?case 
  1371       using eq[OF nc(2), of vs] eq[OF nc(1), of vs] by auto}
  1372   moreover {assume cp: "?c > 0"
  1373     {fix x assume xz: "x < -?e / ?c" hence "?c * x < - ?e"
  1374         using pos_less_divide_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
  1375       hence "?c * x + ?e < 0" by simp
  1376       hence "Ifm vs (x#bs) (Eq (CNP 0 c e)) = Ifm vs (x#bs) (minusinf (Eq (CNP 0 c e)))"
  1377         using eqs tmbound0_I[OF nbe, where b="y" and b'="x" and vs=vs and bs=bs] by auto} hence ?case by auto}
  1378   moreover {assume cp: "?c < 0"
  1379     {fix x assume xz: "x < -?e / ?c" hence "?c * x > - ?e"
  1380         using neg_less_divide_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
  1381       hence "?c * x + ?e > 0" by simp
  1382       hence "Ifm vs (x#bs) (Eq (CNP 0 c e)) = Ifm vs (x#bs) (minusinf (Eq (CNP 0 c e)))"
  1383         using tmbound0_I[OF nbe, where b="y" and b'="x"] eqs by auto} hence ?case by auto}
  1384   ultimately show ?case by blast
  1385 next
  1386   case (4 c e)  hence nbe: "tmbound0 e" by simp
  1387   from 4 have nc: "allpolys isnpoly (CP c)" "allpolys isnpoly e" by simp_all
  1388   note eqs = eq[OF nc(1), where ?'a = 'a] eq[OF nc(2), where ?'a = 'a]
  1389   let ?c = "Ipoly vs c"
  1390   let ?e = "Itm vs (y#bs) e"
  1391   have "?c=0 \<or> ?c > 0 \<or> ?c < 0" by arith
  1392   moreover {assume "?c = 0" hence ?case using eqs by auto}
  1393   moreover {assume cp: "?c > 0"
  1394     {fix x assume xz: "x < -?e / ?c" hence "?c * x < - ?e"
  1395         using pos_less_divide_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
  1396       hence "?c * x + ?e < 0" by simp
  1397       hence "Ifm vs (x#bs) (NEq (CNP 0 c e)) = Ifm vs (x#bs) (minusinf (NEq (CNP 0 c e)))"
  1398         using eqs tmbound0_I[OF nbe, where b="y" and b'="x"] by auto} hence ?case by auto}
  1399   moreover {assume cp: "?c < 0"
  1400     {fix x assume xz: "x < -?e / ?c" hence "?c * x > - ?e"
  1401         using neg_less_divide_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
  1402       hence "?c * x + ?e > 0" by simp
  1403       hence "Ifm vs (x#bs) (NEq (CNP 0 c e)) = Ifm vs (x#bs) (minusinf (NEq (CNP 0 c e)))"
  1404         using eqs tmbound0_I[OF nbe, where b="y" and b'="x"] by auto} hence ?case by auto}
  1405   ultimately show ?case by blast
  1406 next
  1407   case (5 c e)  hence nbe: "tmbound0 e" by simp
  1408   from 5 have nc: "allpolys isnpoly (CP c)" "allpolys isnpoly e" by simp_all
  1409   hence nc': "allpolys isnpoly (CP (~\<^sub>p c))" by (simp add: polyneg_norm)
  1410   note eqs = lt[OF nc', where ?'a = 'a] eq [OF nc(1), where ?'a = 'a] lt[OF nc(2), where ?'a = 'a]
  1411   let ?c = "Ipoly vs c"
  1412   let ?e = "Itm vs (y#bs) e"
  1413   have "?c=0 \<or> ?c > 0 \<or> ?c < 0" by arith
  1414   moreover {assume "?c = 0" hence ?case using eqs by auto}
  1415   moreover {assume cp: "?c > 0"
  1416     {fix x assume xz: "x < -?e / ?c" hence "?c * x < - ?e"
  1417         using pos_less_divide_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
  1418       hence "?c * x + ?e < 0" by simp
  1419       hence "Ifm vs (x#bs) (Lt (CNP 0 c e)) = Ifm vs (x#bs) (minusinf (Lt (CNP 0 c e)))"
  1420         using tmbound0_I[OF nbe, where b="y" and b'="x"] cp eqs by auto} hence ?case by auto}
  1421   moreover {assume cp: "?c < 0"
  1422     {fix x assume xz: "x < -?e / ?c" hence "?c * x > - ?e"
  1423         using neg_less_divide_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
  1424       hence "?c * x + ?e > 0" by simp
  1425       hence "Ifm vs (x#bs) (Lt (CNP 0 c e)) = Ifm vs (x#bs) (minusinf (Lt (CNP 0 c e)))"
  1426         using eqs tmbound0_I[OF nbe, where b="y" and b'="x"] cp by auto} hence ?case by auto}
  1427   ultimately show ?case by blast
  1428 next
  1429   case (6 c e)  hence nbe: "tmbound0 e" by simp
  1430   from 6 have nc: "allpolys isnpoly (CP c)" "allpolys isnpoly e" by simp_all
  1431   hence nc': "allpolys isnpoly (CP (~\<^sub>p c))" by (simp add: polyneg_norm)
  1432   note eqs = lt[OF nc', where ?'a = 'a] eq [OF nc(1), where ?'a = 'a] le[OF nc(2), where ?'a = 'a]
  1433   let ?c = "Ipoly vs c"
  1434   let ?e = "Itm vs (y#bs) e"
  1435   have "?c=0 \<or> ?c > 0 \<or> ?c < 0" by arith
  1436   moreover {assume "?c = 0" hence ?case using eqs by auto}
  1437   moreover {assume cp: "?c > 0"
  1438     {fix x assume xz: "x < -?e / ?c" hence "?c * x < - ?e"
  1439         using pos_less_divide_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
  1440       hence "?c * x + ?e < 0" by simp
  1441       hence "Ifm vs (x#bs) (Le (CNP 0 c e)) = Ifm vs (x#bs) (minusinf (Le (CNP 0 c e)))"
  1442         using tmbound0_I[OF nbe, where b="y" and b'="x"] cp eqs by auto} hence ?case by auto}
  1443   moreover {assume cp: "?c < 0"
  1444     {fix x assume xz: "x < -?e / ?c" hence "?c * x > - ?e"
  1445         using neg_less_divide_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
  1446       hence "?c * x + ?e > 0" by simp
  1447       hence "Ifm vs (x#bs) (Le (CNP 0 c e)) = Ifm vs (x#bs) (minusinf (Le (CNP 0 c e)))"
  1448         using tmbound0_I[OF nbe, where b="y" and b'="x"] cp eqs by auto} hence ?case by auto}
  1449   ultimately show ?case by blast
  1450 qed (auto)
  1451 
  1452 lemma plusinf_inf: assumes lp:"islin p"
  1453   shows "\<exists>z. \<forall>x > z. Ifm vs (x#bs) (plusinf p) \<longleftrightarrow> Ifm vs (x#bs) p"
  1454   using lp
  1455 proof (induct p rule: plusinf.induct)
  1456   case 1 thus ?case by (auto,rule_tac x="max z za" in exI, auto)
  1457 next
  1458   case 2 thus ?case by (auto,rule_tac x="max z za" in exI, auto)
  1459 next
  1460   case (3 c e) hence nbe: "tmbound0 e" by simp
  1461   from 3 have nc: "allpolys isnpoly (CP c)" "allpolys isnpoly e" by simp_all
  1462   note eqs = eq[OF nc(1), where ?'a = 'a] eq[OF nc(2), where ?'a = 'a]
  1463   let ?c = "Ipoly vs c"
  1464   let ?e = "Itm vs (y#bs) e"
  1465   have "?c=0 \<or> ?c > 0 \<or> ?c < 0" by arith
  1466   moreover {assume "?c = 0" hence ?case 
  1467       using eq[OF nc(2), of vs] eq[OF nc(1), of vs] by auto}
  1468   moreover {assume cp: "?c > 0"
  1469     {fix x assume xz: "x > -?e / ?c" hence "?c * x > - ?e" 
  1470         using pos_divide_less_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
  1471       hence "?c * x + ?e > 0" by simp
  1472       hence "Ifm vs (x#bs) (Eq (CNP 0 c e)) = Ifm vs (x#bs) (plusinf (Eq (CNP 0 c e)))"
  1473         using eqs tmbound0_I[OF nbe, where b="y" and b'="x" and vs=vs and bs=bs] by auto} hence ?case by auto}
  1474   moreover {assume cp: "?c < 0"
  1475     {fix x assume xz: "x > -?e / ?c" hence "?c * x < - ?e"
  1476         using neg_divide_less_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
  1477       hence "?c * x + ?e < 0" by simp
  1478       hence "Ifm vs (x#bs) (Eq (CNP 0 c e)) = Ifm vs (x#bs) (plusinf (Eq (CNP 0 c e)))"
  1479         using tmbound0_I[OF nbe, where b="y" and b'="x"] eqs by auto} hence ?case by auto}
  1480   ultimately show ?case by blast
  1481 next
  1482   case (4 c e) hence nbe: "tmbound0 e" by simp
  1483   from 4 have nc: "allpolys isnpoly (CP c)" "allpolys isnpoly e" by simp_all
  1484   note eqs = eq[OF nc(1), where ?'a = 'a] eq[OF nc(2), where ?'a = 'a]
  1485   let ?c = "Ipoly vs c"
  1486   let ?e = "Itm vs (y#bs) e"
  1487   have "?c=0 \<or> ?c > 0 \<or> ?c < 0" by arith
  1488   moreover {assume "?c = 0" hence ?case using eqs by auto}
  1489   moreover {assume cp: "?c > 0"
  1490     {fix x assume xz: "x > -?e / ?c" hence "?c * x > - ?e"
  1491         using pos_divide_less_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
  1492       hence "?c * x + ?e > 0" by simp
  1493       hence "Ifm vs (x#bs) (NEq (CNP 0 c e)) = Ifm vs (x#bs) (plusinf (NEq (CNP 0 c e)))"
  1494         using eqs tmbound0_I[OF nbe, where b="y" and b'="x"] by auto} hence ?case by auto}
  1495   moreover {assume cp: "?c < 0"
  1496     {fix x assume xz: "x > -?e / ?c" hence "?c * x < - ?e"
  1497         using neg_divide_less_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
  1498       hence "?c * x + ?e < 0" by simp
  1499       hence "Ifm vs (x#bs) (NEq (CNP 0 c e)) = Ifm vs (x#bs) (plusinf (NEq (CNP 0 c e)))"
  1500         using eqs tmbound0_I[OF nbe, where b="y" and b'="x"] by auto} hence ?case by auto}
  1501   ultimately show ?case by blast
  1502 next
  1503   case (5 c e) hence nbe: "tmbound0 e" by simp
  1504   from 5 have nc: "allpolys isnpoly (CP c)" "allpolys isnpoly e" by simp_all
  1505   hence nc': "allpolys isnpoly (CP (~\<^sub>p c))" by (simp add: polyneg_norm)
  1506   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]
  1507   let ?c = "Ipoly vs c"
  1508   let ?e = "Itm vs (y#bs) e"
  1509   have "?c=0 \<or> ?c > 0 \<or> ?c < 0" by arith
  1510   moreover {assume "?c = 0" hence ?case using eqs by auto}
  1511   moreover {assume cp: "?c > 0"
  1512     {fix x assume xz: "x > -?e / ?c" hence "?c * x > - ?e"
  1513         using pos_divide_less_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
  1514       hence "?c * x + ?e > 0" by simp
  1515       hence "Ifm vs (x#bs) (Lt (CNP 0 c e)) = Ifm vs (x#bs) (plusinf (Lt (CNP 0 c e)))"
  1516         using tmbound0_I[OF nbe, where b="y" and b'="x"] cp eqs by auto} hence ?case by auto}
  1517   moreover {assume cp: "?c < 0"
  1518     {fix x assume xz: "x > -?e / ?c" hence "?c * x < - ?e"
  1519         using neg_divide_less_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
  1520       hence "?c * x + ?e < 0" by simp
  1521       hence "Ifm vs (x#bs) (Lt (CNP 0 c e)) = Ifm vs (x#bs) (plusinf (Lt (CNP 0 c e)))"
  1522         using eqs tmbound0_I[OF nbe, where b="y" and b'="x"] cp by auto} hence ?case by auto}
  1523   ultimately show ?case by blast
  1524 next
  1525   case (6 c e)  hence nbe: "tmbound0 e" by simp
  1526   from 6 have nc: "allpolys isnpoly (CP c)" "allpolys isnpoly e" by simp_all
  1527   hence nc': "allpolys isnpoly (CP (~\<^sub>p c))" by (simp add: polyneg_norm)
  1528   note eqs = lt[OF nc(1), where ?'a = 'a] eq [OF nc(1), where ?'a = 'a] le[OF nc(2), where ?'a = 'a]
  1529   let ?c = "Ipoly vs c"
  1530   let ?e = "Itm vs (y#bs) e"
  1531   have "?c=0 \<or> ?c > 0 \<or> ?c < 0" by arith
  1532   moreover {assume "?c = 0" hence ?case using eqs by auto}
  1533   moreover {assume cp: "?c > 0"
  1534     {fix x assume xz: "x > -?e / ?c" hence "?c * x > - ?e"
  1535         using pos_divide_less_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
  1536       hence "?c * x + ?e > 0" by simp
  1537       hence "Ifm vs (x#bs) (Le (CNP 0 c e)) = Ifm vs (x#bs) (plusinf (Le (CNP 0 c e)))"
  1538         using tmbound0_I[OF nbe, where b="y" and b'="x"] cp eqs by auto} hence ?case by auto}
  1539   moreover {assume cp: "?c < 0"
  1540     {fix x assume xz: "x > -?e / ?c" hence "?c * x < - ?e"
  1541         using neg_divide_less_eq[OF cp, where a="x" and b="-?e"] by (simp add: mult_commute)
  1542       hence "?c * x + ?e < 0" by simp
  1543       hence "Ifm vs (x#bs) (Le (CNP 0 c e)) = Ifm vs (x#bs) (plusinf (Le (CNP 0 c e)))"
  1544         using tmbound0_I[OF nbe, where b="y" and b'="x"] cp eqs by auto} hence ?case by auto}
  1545   ultimately show ?case by blast
  1546 qed (auto)
  1547 
  1548 lemma minusinf_nb: "islin p \<Longrightarrow> bound0 (minusinf p)" 
  1549   by (induct p rule: minusinf.induct, auto simp add: eq_nb lt_nb le_nb)
  1550 lemma plusinf_nb: "islin p \<Longrightarrow> bound0 (plusinf p)" 
  1551   by (induct p rule: minusinf.induct, auto simp add: eq_nb lt_nb le_nb)
  1552 
  1553 lemma minusinf_ex: assumes lp: "islin p" and ex: "Ifm vs (x#bs) (minusinf p)"
  1554   shows "\<exists>x. Ifm vs (x#bs) p"
  1555 proof-
  1556   from bound0_I [OF minusinf_nb[OF lp], where b="a" and bs ="bs"] ex
  1557   have th: "\<forall> x. Ifm vs (x#bs) (minusinf p)" by auto
  1558   from minusinf_inf[OF lp, where bs="bs"] 
  1559   obtain z where z_def: "\<forall>x<z. Ifm vs (x # bs) (minusinf p) = Ifm vs (x # bs) p" by blast
  1560   from th have "Ifm vs ((z - 1)#bs) (minusinf p)" by simp
  1561   moreover have "z - 1 < z" by simp
  1562   ultimately show ?thesis using z_def by auto
  1563 qed
  1564 
  1565 lemma plusinf_ex: assumes lp: "islin p" and ex: "Ifm vs (x#bs) (plusinf p)"
  1566   shows "\<exists>x. Ifm vs (x#bs) p"
  1567 proof-
  1568   from bound0_I [OF plusinf_nb[OF lp], where b="a" and bs ="bs"] ex
  1569   have th: "\<forall> x. Ifm vs (x#bs) (plusinf p)" by auto
  1570   from plusinf_inf[OF lp, where bs="bs"] 
  1571   obtain z where z_def: "\<forall>x>z. Ifm vs (x # bs) (plusinf p) = Ifm vs (x # bs) p" by blast
  1572   from th have "Ifm vs ((z + 1)#bs) (plusinf p)" by simp
  1573   moreover have "z + 1 > z" by simp
  1574   ultimately show ?thesis using z_def by auto
  1575 qed
  1576 
  1577 fun uset :: "fm \<Rightarrow> (poly \<times> tm) list" where
  1578   "uset (And p q) = uset p @ uset q"
  1579 | "uset (Or p q) = uset p @ uset q"
  1580 | "uset (Eq (CNP 0 a e))  = [(a,e)]"
  1581 | "uset (Le (CNP 0 a e))  = [(a,e)]"
  1582 | "uset (Lt (CNP 0 a e))  = [(a,e)]"
  1583 | "uset (NEq (CNP 0 a e)) = [(a,e)]"
  1584 | "uset p = []"
  1585 
  1586 lemma uset_l:
  1587   assumes lp: "islin p"
  1588   shows "\<forall> (c,s) \<in> set (uset p). isnpoly c \<and> c \<noteq> 0\<^sub>p \<and> tmbound0 s \<and> allpolys isnpoly s"
  1589 using lp by(induct p rule: uset.induct,auto)
  1590 
  1591 lemma minusinf_uset0:
  1592   assumes lp: "islin p"
  1593   and nmi: "\<not> (Ifm vs (x#bs) (minusinf p))"
  1594   and ex: "Ifm vs (x#bs) p" (is "?I x p")
  1595   shows "\<exists> (c,s) \<in> set (uset p). x \<ge> - Itm vs (x#bs) s / Ipoly vs c" 
  1596 proof-
  1597   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)" 
  1598     using lp nmi ex
  1599     apply (induct p rule: minusinf.induct, auto simp add: eq le lt nth_pos2 polyneg_norm)
  1600     apply (auto simp add: linorder_not_less order_le_less)
  1601     done 
  1602   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
  1603   hence "x \<ge> (- Itm vs (x#bs) s) / Ipoly vs c"
  1604     using divide_le_eq[of "- Itm vs (x#bs) s" "Ipoly vs c" x]
  1605     by (auto simp add: mult_commute del: divide_minus_left)
  1606   thus ?thesis using csU by auto
  1607 qed
  1608 
  1609 lemma minusinf_uset:
  1610   assumes lp: "islin p"
  1611   and nmi: "\<not> (Ifm vs (a#bs) (minusinf p))"
  1612   and ex: "Ifm vs (x#bs) p" (is "?I x p")
  1613   shows "\<exists> (c,s) \<in> set (uset p). x \<ge> - Itm vs (a#bs) s / Ipoly vs c" 
  1614 proof-
  1615   from nmi have nmi': "\<not> (Ifm vs (x#bs) (minusinf p))" 
  1616     by (simp add: bound0_I[OF minusinf_nb[OF lp], where b=x and b'=a])
  1617   from minusinf_uset0[OF lp nmi' ex] 
  1618   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
  1619   from uset_l[OF lp, rule_format, OF csU] have nb: "tmbound0 s" by simp
  1620   from th tmbound0_I[OF nb, of vs x bs a] csU show ?thesis by auto
  1621 qed
  1622 
  1623 
  1624 lemma plusinf_uset0:
  1625   assumes lp: "islin p"
  1626   and nmi: "\<not> (Ifm vs (x#bs) (plusinf p))"
  1627   and ex: "Ifm vs (x#bs) p" (is "?I x p")
  1628   shows "\<exists> (c,s) \<in> set (uset p). x \<le> - Itm vs (x#bs) s / Ipoly vs c" 
  1629 proof-
  1630   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)" 
  1631     using lp nmi ex
  1632     apply (induct p rule: minusinf.induct, auto simp add: eq le lt nth_pos2 polyneg_norm)
  1633     apply (auto simp add: linorder_not_less order_le_less)
  1634     done 
  1635   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
  1636   hence "x \<le> (- Itm vs (x#bs) s) / Ipoly vs c"
  1637     using le_divide_eq[of x "- Itm vs (x#bs) s" "Ipoly vs c"]
  1638     by (auto simp add: mult_commute del: divide_minus_left)
  1639   thus ?thesis using csU by auto
  1640 qed
  1641 
  1642 lemma plusinf_uset:
  1643   assumes lp: "islin p"
  1644   and nmi: "\<not> (Ifm vs (a#bs) (plusinf p))"
  1645   and ex: "Ifm vs (x#bs) p" (is "?I x p")
  1646   shows "\<exists> (c,s) \<in> set (uset p). x \<le> - Itm vs (a#bs) s / Ipoly vs c" 
  1647 proof-
  1648   from nmi have nmi': "\<not> (Ifm vs (x#bs) (plusinf p))" 
  1649     by (simp add: bound0_I[OF plusinf_nb[OF lp], where b=x and b'=a])
  1650   from plusinf_uset0[OF lp nmi' ex] 
  1651   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
  1652   from uset_l[OF lp, rule_format, OF csU] have nb: "tmbound0 s" by simp
  1653   from th tmbound0_I[OF nb, of vs x bs a] csU show ?thesis by auto
  1654 qed
  1655 
  1656 lemma lin_dense: 
  1657   assumes lp: "islin p"
  1658   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)" 
  1659   (is "\<forall> t. _ \<and> _ \<longrightarrow> t \<notin> (\<lambda> (c,t). - ?Nt x t / ?N c) ` ?U p")
  1660   and lx: "l < x" and xu:"x < u" and px:" Ifm vs (x#bs) p"
  1661   and ly: "l < y" and yu: "y < u"
  1662   shows "Ifm vs (y#bs) p"
  1663 using lp px noS
  1664 proof (induct p rule: islin.induct) 
  1665   case (5 c s)
  1666   from "5.prems" 
  1667   have lin: "isnpoly c" "c \<noteq> 0\<^sub>p" "tmbound0 s" "allpolys isnpoly s"
  1668     and px: "Ifm vs (x # bs) (Lt (CNP 0 c s))"
  1669     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
  1670   from ly yu noS have yne: "y \<noteq> - ?Nt x s / \<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup>" by simp
  1671   hence ycs: "y < - ?Nt x s / ?N c \<or> y > -?Nt x s / ?N c" by auto
  1672   have ccs: "?N c = 0 \<or> ?N c < 0 \<or> ?N c > 0" by dlo
  1673   moreover
  1674   {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"])}
  1675   moreover
  1676   {assume c: "?N c > 0"
  1677       from px pos_less_divide_eq[OF c, where a="x" and b="-?Nt x s"]  
  1678       have px': "x < - ?Nt x s / ?N c" 
  1679         by (auto simp add: not_less field_simps) 
  1680     {assume y: "y < - ?Nt x s / ?N c" 
  1681       hence "y * ?N c < - ?Nt x s"
  1682         by (simp add: pos_less_divide_eq[OF c, where a="y" and b="-?Nt x s", symmetric])
  1683       hence "?N c * y + ?Nt x s < 0" by (simp add: field_simps)
  1684       hence ?case using tmbound0_I[OF lin(3), where bs="bs" and b="x" and b'="y"] by simp}
  1685     moreover
  1686     {assume y: "y > -?Nt x s / ?N c" 
  1687       with yu have eu: "u > - ?Nt x s / ?N c" by auto
  1688       with noS ly yu have th: "- ?Nt x s / ?N c \<le> l" by (cases "- ?Nt x s / ?N c > l", auto)
  1689       with lx px' have "False" by simp  hence ?case by simp }
  1690     ultimately have ?case using ycs by blast
  1691   }
  1692   moreover
  1693   {assume c: "?N c < 0"
  1694       from px neg_divide_less_eq[OF c, where a="x" and b="-?Nt x s"]  
  1695       have px': "x > - ?Nt x s / ?N c" 
  1696         by (auto simp add: not_less field_simps) 
  1697     {assume y: "y > - ?Nt x s / ?N c" 
  1698       hence "y * ?N c < - ?Nt x s"
  1699         by (simp add: neg_divide_less_eq[OF c, where a="y" and b="-?Nt x s", symmetric])
  1700       hence "?N c * y + ?Nt x s < 0" by (simp add: field_simps)
  1701       hence ?case using tmbound0_I[OF lin(3), where bs="bs" and b="x" and b'="y"] by simp}
  1702     moreover
  1703     {assume y: "y < -?Nt x s / ?N c" 
  1704       with ly have eu: "l < - ?Nt x s / ?N c" by auto
  1705       with noS ly yu have th: "- ?Nt x s / ?N c \<ge> u" by (cases "- ?Nt x s / ?N c < u", auto)
  1706       with xu px' have "False" by simp  hence ?case by simp }
  1707     ultimately have ?case using ycs by blast
  1708   }
  1709   ultimately show ?case by blast
  1710 next
  1711   case (6 c s)
  1712   from "6.prems" 
  1713   have lin: "isnpoly c" "c \<noteq> 0\<^sub>p" "tmbound0 s" "allpolys isnpoly s"
  1714     and px: "Ifm vs (x # bs) (Le (CNP 0 c s))"
  1715     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
  1716   from ly yu noS have yne: "y \<noteq> - ?Nt x s / \<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup>" by simp
  1717   hence ycs: "y < - ?Nt x s / ?N c \<or> y > -?Nt x s / ?N c" by auto
  1718   have ccs: "?N c = 0 \<or> ?N c < 0 \<or> ?N c > 0" by dlo
  1719   moreover
  1720   {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"])}
  1721   moreover
  1722   {assume c: "?N c > 0"
  1723       from px pos_le_divide_eq[OF c, where a="x" and b="-?Nt x s"]  
  1724       have px': "x <= - ?Nt x s / ?N c" by (simp add: not_less field_simps) 
  1725     {assume y: "y < - ?Nt x s / ?N c" 
  1726       hence "y * ?N c < - ?Nt x s"
  1727         by (simp add: pos_less_divide_eq[OF c, where a="y" and b="-?Nt x s", symmetric])
  1728       hence "?N c * y + ?Nt x s < 0" by (simp add: field_simps)
  1729       hence ?case using tmbound0_I[OF lin(3), where bs="bs" and b="x" and b'="y"] by simp}
  1730     moreover
  1731     {assume y: "y > -?Nt x s / ?N c" 
  1732       with yu have eu: "u > - ?Nt x s / ?N c" by auto
  1733       with noS ly yu have th: "- ?Nt x s / ?N c \<le> l" by (cases "- ?Nt x s / ?N c > l", auto)
  1734       with lx px' have "False" by simp  hence ?case by simp }
  1735     ultimately have ?case using ycs by blast
  1736   }
  1737   moreover
  1738   {assume c: "?N c < 0"
  1739       from px neg_divide_le_eq[OF c, where a="x" and b="-?Nt x s"]  
  1740       have px': "x >= - ?Nt x s / ?N c" by (simp add: field_simps) 
  1741     {assume y: "y > - ?Nt x s / ?N c" 
  1742       hence "y * ?N c < - ?Nt x s"
  1743         by (simp add: neg_divide_less_eq[OF c, where a="y" and b="-?Nt x s", symmetric])
  1744       hence "?N c * y + ?Nt x s < 0" by (simp add: field_simps)
  1745       hence ?case using tmbound0_I[OF lin(3), where bs="bs" and b="x" and b'="y"] by simp}
  1746     moreover
  1747     {assume y: "y < -?Nt x s / ?N c" 
  1748       with ly have eu: "l < - ?Nt x s / ?N c" by auto
  1749       with noS ly yu have th: "- ?Nt x s / ?N c \<ge> u" by (cases "- ?Nt x s / ?N c < u", auto)
  1750       with xu px' have "False" by simp  hence ?case by simp }
  1751     ultimately have ?case using ycs by blast
  1752   }
  1753   ultimately show ?case by blast
  1754 next
  1755     case (3 c s)
  1756   from "3.prems" 
  1757   have lin: "isnpoly c" "c \<noteq> 0\<^sub>p" "tmbound0 s" "allpolys isnpoly s"
  1758     and px: "Ifm vs (x # bs) (Eq (CNP 0 c s))"
  1759     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
  1760   from ly yu noS have yne: "y \<noteq> - ?Nt x s / \<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup>" by simp
  1761   hence ycs: "y < - ?Nt x s / ?N c \<or> y > -?Nt x s / ?N c" by auto
  1762   have ccs: "?N c = 0 \<or> ?N c < 0 \<or> ?N c > 0" by dlo
  1763   moreover
  1764   {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"])}
  1765   moreover
  1766   {assume c: "?N c > 0" hence cnz: "?N c \<noteq> 0" by simp
  1767     from px eq_divide_eq[of "x" "-?Nt x s" "?N c"]  cnz
  1768     have px': "x = - ?Nt x s / ?N c" by (simp add: field_simps)
  1769     {assume y: "y < -?Nt x s / ?N c" 
  1770       with ly have eu: "l < - ?Nt x s / ?N c" by auto
  1771       with noS ly yu have th: "- ?Nt x s / ?N c \<ge> u" by (cases "- ?Nt x s / ?N c < u", auto)
  1772       with xu px' have "False" by simp  hence ?case by simp }
  1773     moreover
  1774     {assume y: "y > -?Nt x s / ?N c" 
  1775       with yu have eu: "u > - ?Nt x s / ?N c" by auto
  1776       with noS ly yu have th: "- ?Nt x s / ?N c \<le> l" by (cases "- ?Nt x s / ?N c > l", auto)
  1777       with lx px' have "False" by simp  hence ?case by simp }
  1778     ultimately have ?case using ycs by blast
  1779   }
  1780   moreover
  1781   {assume c: "?N c < 0" hence cnz: "?N c \<noteq> 0" by simp
  1782     from px eq_divide_eq[of "x" "-?Nt x s" "?N c"]  cnz
  1783     have px': "x = - ?Nt x s / ?N c" by (simp add: field_simps)
  1784     {assume y: "y < -?Nt x s / ?N c" 
  1785       with ly have eu: "l < - ?Nt x s / ?N c" by auto
  1786       with noS ly yu have th: "- ?Nt x s / ?N c \<ge> u" by (cases "- ?Nt x s / ?N c < u", auto)
  1787       with xu px' have "False" by simp  hence ?case by simp }
  1788     moreover
  1789     {assume y: "y > -?Nt x s / ?N c" 
  1790       with yu have eu: "u > - ?Nt x s / ?N c" by auto
  1791       with noS ly yu have th: "- ?Nt x s / ?N c \<le> l" by (cases "- ?Nt x s / ?N c > l", auto)
  1792       with lx px' have "False" by simp  hence ?case by simp }
  1793     ultimately have ?case using ycs by blast
  1794   }
  1795   ultimately show ?case by blast
  1796 next
  1797     case (4 c s)
  1798   from "4.prems" 
  1799   have lin: "isnpoly c" "c \<noteq> 0\<^sub>p" "tmbound0 s" "allpolys isnpoly s"
  1800     and px: "Ifm vs (x # bs) (NEq (CNP 0 c s))"
  1801     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
  1802   from ly yu noS have yne: "y \<noteq> - ?Nt x s / \<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup>" by simp
  1803   hence ycs: "y < - ?Nt x s / ?N c \<or> y > -?Nt x s / ?N c" by auto
  1804   have ccs: "?N c = 0 \<or> ?N c \<noteq> 0" by dlo
  1805   moreover
  1806   {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"])}
  1807   moreover
  1808   {assume c: "?N c \<noteq> 0"
  1809     from yne c eq_divide_eq[of "y" "- ?Nt x s" "?N c"] have ?case
  1810       by (simp add: field_simps tmbound0_I[OF lin(3), of vs x bs y] sum_eq[symmetric]) }
  1811   ultimately show ?case by blast
  1812 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"])
  1813 
  1814 lemma one_plus_one_pos[simp]: "(1::'a::{linordered_field}) + 1 > 0"
  1815 proof-
  1816   have op: "(1::'a) > 0" by simp
  1817   from add_pos_pos[OF op op] show ?thesis . 
  1818 qed
  1819 
  1820 lemma one_plus_one_nonzero[simp]: "(1::'a::{linordered_field}) + 1 \<noteq> 0" 
  1821   using one_plus_one_pos[where ?'a = 'a] by (simp add: less_le) 
  1822 
  1823 lemma half_sum_eq: "(u + u) / (1+1) = (u::'a::{linordered_field})" 
  1824 proof-
  1825   have "(u + u) = (1 + 1) * u" by (simp add: field_simps)
  1826   hence "(u + u) / (1+1) = (1 + 1)*u / (1 + 1)" by simp
  1827   with nonzero_mult_divide_cancel_left[OF one_plus_one_nonzero, of u] show ?thesis by simp
  1828 qed
  1829 
  1830 lemma inf_uset:
  1831   assumes lp: "islin p"
  1832   and nmi: "\<not> (Ifm vs (x#bs) (minusinf p))" (is "\<not> (Ifm vs (x#bs) (?M p))")
  1833   and npi: "\<not> (Ifm vs (x#bs) (plusinf p))" (is "\<not> (Ifm vs (x#bs) (?P p))")
  1834   and ex: "\<exists> x.  Ifm vs (x#bs) p" (is "\<exists> x. ?I x p")
  1835   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" 
  1836 proof-
  1837   let ?Nt = "\<lambda> x t. Itm vs (x#bs) t"
  1838   let ?N = "Ipoly vs"
  1839   let ?U = "set (uset p)"
  1840   from ex obtain a where pa: "?I a p" by blast
  1841   from bound0_I[OF minusinf_nb[OF lp], where bs="bs" and b="x" and b'="a"] nmi
  1842   have nmi': "\<not> (?I a (?M p))" by simp
  1843   from bound0_I[OF plusinf_nb[OF lp], where bs="bs" and b="x" and b'="a"] npi
  1844   have npi': "\<not> (?I a (?P p))" by simp
  1845   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"
  1846   proof-
  1847     let ?M = "(\<lambda> (c,t). - ?Nt a t / ?N c) ` ?U"
  1848     have fM: "finite ?M" by auto
  1849     from minusinf_uset[OF lp nmi pa] plusinf_uset[OF lp npi pa] 
  1850     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
  1851     then obtain "c" "t" "d" "s" where 
  1852       ctU: "(c,t) \<in> ?U" and dsU: "(d,s) \<in> ?U" 
  1853       and xs1: "a \<le> - ?Nt x s / ?N d" and tx1: "a \<ge> - ?Nt x t / ?N c" by blast
  1854     from uset_l[OF lp] ctU dsU tmbound0_I[where bs="bs" and b="x" and b'="a"] xs1 tx1 
  1855     have xs: "a \<le> - ?Nt a s / ?N d" and tx: "a \<ge> - ?Nt a t / ?N c" by auto
  1856     from ctU have Mne: "?M \<noteq> {}" by auto
  1857     hence Une: "?U \<noteq> {}" by simp
  1858     let ?l = "Min ?M"
  1859     let ?u = "Max ?M"
  1860     have linM: "?l \<in> ?M" using fM Mne by simp
  1861     have uinM: "?u \<in> ?M" using fM Mne by simp
  1862     have ctM: "- ?Nt a t / ?N c \<in> ?M" using ctU by auto
  1863     have dsM: "- ?Nt a s / ?N d \<in> ?M" using dsU by auto 
  1864     have lM: "\<forall> t\<in> ?M. ?l \<le> t" using Mne fM by auto
  1865     have Mu: "\<forall> t\<in> ?M. t \<le> ?u" using Mne fM by auto
  1866     have "?l \<le> - ?Nt a t / ?N c" using ctM Mne by simp hence lx: "?l \<le> a" using tx by simp
  1867     have "- ?Nt a s / ?N d \<le> ?u" using dsM Mne by simp hence xu: "a \<le> ?u" using xs by simp
  1868     from finite_set_intervals2[where P="\<lambda> x. ?I x p",OF pa lx xu linM uinM fM lM Mu]
  1869     have "(\<exists> s\<in> ?M. ?I s p) \<or> 
  1870       (\<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)" .
  1871     moreover {fix u assume um: "u\<in> ?M" and pu: "?I u p"
  1872       hence "\<exists> (nu,tu) \<in> ?U. u = - ?Nt a tu / ?N nu" by auto
  1873       then obtain "tu" "nu" where tuU: "(nu,tu) \<in> ?U" and tuu:"u= - ?Nt a tu / ?N nu" by blast
  1874       from half_sum_eq[of u] pu tuu 
  1875       have "?I (((- ?Nt a tu / ?N nu) + (- ?Nt a tu / ?N nu)) / (1 + 1)) p" by simp
  1876       with tuU have ?thesis by blast}
  1877     moreover{
  1878       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"
  1879       then obtain t1 and t2 where t1M: "t1 \<in> ?M" and t2M: "t2\<in> ?M" 
  1880         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"
  1881         by blast
  1882       from t1M have "\<exists> (t1n,t1u) \<in> ?U. t1 = - ?Nt a t1u / ?N t1n" by auto
  1883       then obtain "t1u" "t1n" where t1uU: "(t1n,t1u) \<in> ?U" and t1u: "t1 = - ?Nt a t1u / ?N t1n" by blast
  1884       from t2M have "\<exists> (t2n,t2u) \<in> ?U. t2 = - ?Nt a t2u / ?N t2n" by auto
  1885       then obtain "t2u" "t2n" where t2uU: "(t2n,t2u) \<in> ?U" and t2u: "t2 = - ?Nt a t2u / ?N t2n" by blast
  1886       from t1x xt2 have t1t2: "t1 < t2" by simp
  1887       let ?u = "(t1 + t2) / (1 + 1)"
  1888       from less_half_sum[OF t1t2] gt_half_sum[OF t1t2] have t1lu: "t1 < ?u" and ut2: "?u < t2" by auto
  1889       from lin_dense[OF lp noM t1x xt2 px t1lu ut2] have "?I ?u p" .
  1890       with t1uU t2uU t1u t2u have ?thesis by blast}
  1891     ultimately show ?thesis by blast
  1892   qed
  1893   then obtain "l" "n" "s"  "m" where lnU: "(n,l) \<in> ?U" and smU:"(m,s) \<in> ?U" 
  1894     and pu: "?I ((- ?Nt a l / ?N n + - ?Nt a s / ?N m) / (1 + 1)) p" by blast
  1895   from lnU smU uset_l[OF lp] have nbl: "tmbound0 l" and nbs: "tmbound0 s" by auto
  1896   from tmbound0_I[OF nbl, where bs="bs" and b="a" and b'="x"] 
  1897     tmbound0_I[OF nbs, where bs="bs" and b="a" and b'="x"] pu
  1898   have "?I ((- ?Nt x l / ?N n + - ?Nt x s / ?N m) / (1 + 1)) p" by simp
  1899   with lnU smU
  1900   show ?thesis by auto
  1901 qed
  1902 
  1903     (* The Ferrante - Rackoff Theorem *)
  1904 
  1905 theorem fr_eq: 
  1906   assumes lp: "islin p"
  1907   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))"
  1908   (is "(\<exists> x. ?I x p) = (?M \<or> ?P \<or> ?F)" is "?E = ?D")
  1909 proof
  1910   assume px: "\<exists> x. ?I x p"
  1911   have "?M \<or> ?P \<or> (\<not> ?M \<and> \<not> ?P)" by blast
  1912   moreover {assume "?M \<or> ?P" hence "?D" by blast}
  1913   moreover {assume nmi: "\<not> ?M" and npi: "\<not> ?P"
  1914     from inf_uset[OF lp nmi npi] have "?F" using px by blast hence "?D" by blast}
  1915   ultimately show "?D" by blast
  1916 next
  1917   assume "?D" 
  1918   moreover {assume m:"?M" from minusinf_ex[OF lp m] have "?E" .}
  1919   moreover {assume p: "?P" from plusinf_ex[OF lp p] have "?E" . }
  1920   moreover {assume f:"?F" hence "?E" by blast}
  1921   ultimately show "?E" by blast
  1922 qed
  1923 
  1924 section{* First implementation : Naive by encoding all case splits locally *}
  1925 definition "msubsteq c t d s a r = 
  1926   evaldjf (split conj) 
  1927   [(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)))),
  1928    (conj (Eq (CP c)) (NEq (CP d)) , Eq (Add (Mul (~\<^sub>p a) s) (Mul (2\<^sub>p *\<^sub>p d) r))),
  1929    (conj (NEq (CP c)) (Eq (CP d)) , Eq (Add (Mul (~\<^sub>p a) t) (Mul (2\<^sub>p *\<^sub>p c) r))),
  1930    (conj (Eq (CP c)) (Eq (CP d)) , Eq r)]"
  1931 
  1932 lemma msubsteq_nb: assumes lp: "islin (Eq (CNP 0 a r))" and t: "tmbound0 t" and s: "tmbound0 s"
  1933   shows "bound0 (msubsteq c t d s a r)"
  1934 proof-
  1935   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)))),
  1936    (conj (Eq (CP c)) (NEq (CP d)) , Eq (Add (Mul (~\<^sub>p a) s) (Mul (2\<^sub>p *\<^sub>p d) r))),
  1937    (conj (NEq (CP c)) (Eq (CP d)) , Eq (Add (Mul (~\<^sub>p a) t) (Mul (2\<^sub>p *\<^sub>p c) r))),
  1938    (conj (Eq (CP c)) (Eq (CP d)) , Eq r)]. bound0 (split conj x)"
  1939     using lp by (simp add: Let_def t s )
  1940   from evaldjf_bound0[OF th] show ?thesis by (simp add: msubsteq_def)
  1941 qed
  1942 
  1943 lemma msubsteq: assumes lp: "islin (Eq (CNP 0 a r))"
  1944   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")
  1945 proof-
  1946   let ?Nt = "\<lambda>(x::'a) t. Itm vs (x#bs) t"
  1947   let ?N = "\<lambda>p. Ipoly vs p"
  1948   let ?c = "?N c"
  1949   let ?d = "?N d"
  1950   let ?t = "?Nt x t"
  1951   let ?s = "?Nt x s"
  1952   let ?a = "?N a"
  1953   let ?r = "?Nt x r"
  1954   from lp have lin:"isnpoly a" "a \<noteq> 0\<^sub>p" "tmbound0 r" "allpolys isnpoly r" by simp_all
  1955   note r= tmbound0_I[OF lin(3), of vs _ bs x]
  1956   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
  1957   moreover
  1958   {assume c: "?c = 0" and d: "?d=0"
  1959     hence ?thesis  by (simp add: r[of 0] msubsteq_def Let_def evaldjf_ex)}
  1960   moreover 
  1961   {assume c: "?c = 0" and d: "?d\<noteq>0"
  1962     from c have th: "(- ?t / ?c + - ?s / ?d)/(1 + 1) = -?s / ((1 + 1)*?d)" by simp
  1963     have "?rhs = Ifm vs (-?s / ((1 + 1)*?d) # bs) (Eq (CNP 0 a r))" by (simp only: th)
  1964     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>))"])
  1965     also have "\<dots> \<longleftrightarrow> (1 + 1)*?d * (?a * (-?s / ((1 + 1)*?d)) + ?r) = 0" 
  1966       using d mult_cancel_left[of "(1 + 1)*?d" "(?a * (-?s / ((1 + 1)*?d)) + ?r)" 0] by simp
  1967     also have "\<dots> \<longleftrightarrow> (- ?a * ?s) * ((1 + 1)*?d / ((1 + 1)*?d)) + (1 + 1)*?d*?r= 0"
  1968       by (simp add: field_simps right_distrib[of "(1 + 1)*?d"] del: right_distrib)
  1969     
  1970     also have "\<dots> \<longleftrightarrow> - (?a * ?s) + (1 + 1)*?d*?r = 0" using d by simp 
  1971     finally have ?thesis using c d 
  1972       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)
  1973       apply (simp only: one_add_one_is_two[symmetric] of_int_add)
  1974       apply simp
  1975       done}
  1976   moreover
  1977   {assume c: "?c \<noteq> 0" and d: "?d=0"
  1978     from d have th: "(- ?t / ?c + - ?s / ?d)/(1 + 1) = -?t / ((1 + 1)*?c)" by simp
  1979     have "?rhs = Ifm vs (-?t / ((1 + 1)*?c) # bs) (Eq (CNP 0 a r))" by (simp only: th)
  1980     also have "\<dots> \<longleftrightarrow> ?a * (-?t / ((1 + 1)*?c)) + ?r = 0" by (simp add: r[of "- (?t/ ((1 + 1)* ?c))"])
  1981     also have "\<dots> \<longleftrightarrow> (1 + 1)*?c * (?a * (-?t / ((1 + 1)*?c)) + ?r) = 0" 
  1982       using c mult_cancel_left[of "(1 + 1)*?c" "(?a * (-?t / ((1 + 1)*?c)) + ?r)" 0] by simp
  1983     also have "\<dots> \<longleftrightarrow> (?a * -?t)* ((1 + 1)*?c) / ((1 + 1)*?c) + (1 + 1)*?c*?r= 0"
  1984       by (simp add: field_simps right_distrib[of "(1 + 1)*?c"] del: right_distrib)
  1985     also have "\<dots> \<longleftrightarrow> - (?a * ?t) + (1 + 1)*?c*?r = 0" using c by simp 
  1986     finally have ?thesis using c d 
  1987       apply (simp add: r[of "- (?t/ ((1 + 1)*?c))"] msubsteq_def Let_def evaldjf_ex del: one_add_one_is_two)
  1988       apply (simp only: one_add_one_is_two[symmetric] of_int_add)
  1989       apply simp
  1990       done }
  1991   moreover
  1992   {assume c: "?c \<noteq> 0" and d: "?d\<noteq>0" hence dc: "?c * ?d *(1 + 1) \<noteq> 0" by simp
  1993     from add_frac_eq[OF c d, of "- ?t" "- ?s"]
  1994     have th: "(- ?t / ?c + - ?s / ?d)/(1 + 1) = - (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d)" 
  1995       by (simp add: field_simps)
  1996     have "?rhs \<longleftrightarrow> Ifm vs (- (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d) # bs) (Eq (CNP 0 a r))" by (simp only: th)
  1997     also have "\<dots> \<longleftrightarrow> ?a * (- (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d)) + ?r = 0" 
  1998       by (simp add: r[of "(- (?d * ?t) + - (?c *?s)) / ((1 + 1) * ?c * ?d)"])
  1999     also have "\<dots> \<longleftrightarrow> ((1 + 1) * ?c * ?d) * (?a * (- (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d)) + ?r) =0 "
  2000       using c d mult_cancel_left[of "(1 + 1) * ?c * ?d" "?a * (- (?d * ?t + ?c* ?s)/ ((1 + 1)*?c*?d)) + ?r" 0] by simp
  2001     also have "\<dots> \<longleftrightarrow> ?a * (- (?d * ?t + ?c* ?s )) + (1 + 1)*?c*?d*?r =0" 
  2002       using nonzero_mult_divide_cancel_left [OF dc] c d
  2003       by (simp add: algebra_simps diff_divide_distrib del: left_distrib)
  2004     finally  have ?thesis using c d 
  2005       apply (simp add: r[of "(- (?d * ?t) + - (?c *?s)) / ((1 + 1) * ?c * ?d)"] msubsteq_def Let_def evaldjf_ex field_simps)
  2006       apply (simp only: one_add_one_is_two[symmetric] of_int_add)
  2007       apply (simp add: field_simps)
  2008       done }
  2009   ultimately show ?thesis by blast
  2010 qed
  2011 
  2012 
  2013 definition "msubstneq c t d s a r = 
  2014   evaldjf (split conj) 
  2015   [(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)))),
  2016    (conj (Eq (CP c)) (NEq (CP d)) , NEq (Add (Mul (~\<^sub>p a) s) (Mul (2\<^sub>p *\<^sub>p d) r))),
  2017    (conj (NEq (CP c)) (Eq (CP d)) , NEq (Add (Mul (~\<^sub>p a) t) (Mul (2\<^sub>p *\<^sub>p c) r))),
  2018    (conj (Eq (CP c)) (Eq (CP d)) , NEq r)]"
  2019 
  2020 lemma msubstneq_nb: assumes lp: "islin (NEq (CNP 0 a r))" and t: "tmbound0 t" and s: "tmbound0 s"
  2021   shows "bound0 (msubstneq c t d s a r)"
  2022 proof-
  2023   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)))), 
  2024     (conj (Eq (CP c)) (NEq (CP d)) , NEq (Add (Mul (~\<^sub>p a) s) (Mul (2\<^sub>p *\<^sub>p d) r))),
  2025     (conj (NEq (CP c)) (Eq (CP d)) , NEq (Add (Mul (~\<^sub>p a) t) (Mul (2\<^sub>p *\<^sub>p c) r))),
  2026     (conj (Eq (CP c)) (Eq (CP d)) , NEq r)]. bound0 (split conj x)"
  2027     using lp by (simp add: Let_def t s )
  2028   from evaldjf_bound0[OF th] show ?thesis by (simp add: msubstneq_def)
  2029 qed
  2030 
  2031 lemma msubstneq: assumes lp: "islin (Eq (CNP 0 a r))"
  2032   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")
  2033 proof-
  2034   let ?Nt = "\<lambda>(x::'a) t. Itm vs (x#bs) t"
  2035   let ?N = "\<lambda>p. Ipoly vs p"
  2036   let ?c = "?N c"
  2037   let ?d = "?N d"
  2038   let ?t = "?Nt x t"
  2039   let ?s = "?Nt x s"
  2040   let ?a = "?N a"
  2041   let ?r = "?Nt x r"
  2042   from lp have lin:"isnpoly a" "a \<noteq> 0\<^sub>p" "tmbound0 r" "allpolys isnpoly r" by simp_all
  2043   note r= tmbound0_I[OF lin(3), of vs _ bs x]
  2044   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
  2045   moreover
  2046   {assume c: "?c = 0" and d: "?d=0"
  2047     hence ?thesis  by (simp add: r[of 0] msubstneq_def Let_def evaldjf_ex)}
  2048   moreover 
  2049   {assume c: "?c = 0" and d: "?d\<noteq>0"
  2050     from c have th: "(- ?t / ?c + - ?s / ?d)/(1 + 1) = -?s / ((1 + 1)*?d)" by simp
  2051     have "?rhs = Ifm vs (-?s / ((1 + 1)*?d) # bs) (NEq (CNP 0 a r))" by (simp only: th)
  2052     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>))"])
  2053     also have "\<dots> \<longleftrightarrow> (1 + 1)*?d * (?a * (-?s / ((1 + 1)*?d)) + ?r) \<noteq> 0" 
  2054       using d mult_cancel_left[of "(1 + 1)*?d" "(?a * (-?s / ((1 + 1)*?d)) + ?r)" 0] by simp
  2055     also have "\<dots> \<longleftrightarrow> (- ?a * ?s) * ((1 + 1)*?d / ((1 + 1)*?d)) + (1 + 1)*?d*?r\<noteq> 0"
  2056       by (simp add: field_simps right_distrib[of "(1 + 1)*?d"] del: right_distrib)
  2057     
  2058     also have "\<dots> \<longleftrightarrow> - (?a * ?s) + (1 + 1)*?d*?r \<noteq> 0" using d by simp 
  2059     finally have ?thesis using c d 
  2060       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)
  2061       apply (simp only: one_add_one_is_two[symmetric] of_int_add)
  2062       apply simp
  2063       done}
  2064   moreover
  2065   {assume c: "?c \<noteq> 0" and d: "?d=0"
  2066     from d have th: "(- ?t / ?c + - ?s / ?d)/(1 + 1) = -?t / ((1 + 1)*?c)" by simp
  2067     have "?rhs = Ifm vs (-?t / ((1 + 1)*?c) # bs) (NEq (CNP 0 a r))" by (simp only: th)
  2068     also have "\<dots> \<longleftrightarrow> ?a * (-?t / ((1 + 1)*?c)) + ?r \<noteq> 0" by (simp add: r[of "- (?t/ ((1 + 1)* ?c))"])
  2069     also have "\<dots> \<longleftrightarrow> (1 + 1)*?c * (?a * (-?t / ((1 + 1)*?c)) + ?r) \<noteq> 0" 
  2070       using c mult_cancel_left[of "(1 + 1)*?c" "(?a * (-?t / ((1 + 1)*?c)) + ?r)" 0] by simp
  2071     also have "\<dots> \<longleftrightarrow> (?a * -?t)* ((1 + 1)*?c) / ((1 + 1)*?c) + (1 + 1)*?c*?r \<noteq> 0"
  2072       by (simp add: field_simps right_distrib[of "(1 + 1)*?c"] del: right_distrib)
  2073     also have "\<dots> \<longleftrightarrow> - (?a * ?t) + (1 + 1)*?c*?r \<noteq> 0" using c by simp 
  2074     finally have ?thesis using c d 
  2075       apply (simp add: r[of "- (?t/ ((1 + 1)*?c))"] msubstneq_def Let_def evaldjf_ex del: one_add_one_is_two)
  2076       apply (simp only: one_add_one_is_two[symmetric] of_int_add)
  2077       apply simp
  2078       done }
  2079   moreover
  2080   {assume c: "?c \<noteq> 0" and d: "?d\<noteq>0" hence dc: "?c * ?d *(1 + 1) \<noteq> 0" by simp
  2081     from add_frac_eq[OF c d, of "- ?t" "- ?s"]
  2082     have th: "(- ?t / ?c + - ?s / ?d)/(1 + 1) = - (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d)" 
  2083       by (simp add: field_simps)
  2084     have "?rhs \<longleftrightarrow> Ifm vs (- (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d) # bs) (NEq (CNP 0 a r))" by (simp only: th)
  2085     also have "\<dots> \<longleftrightarrow> ?a * (- (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d)) + ?r \<noteq> 0" 
  2086       by (simp add: r[of "(- (?d * ?t) + - (?c *?s)) / ((1 + 1) * ?c * ?d)"])
  2087     also have "\<dots> \<longleftrightarrow> ((1 + 1) * ?c * ?d) * (?a * (- (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d)) + ?r) \<noteq> 0 "
  2088       using c d mult_cancel_left[of "(1 + 1) * ?c * ?d" "?a * (- (?d * ?t + ?c* ?s)/ ((1 + 1)*?c*?d)) + ?r" 0] by simp
  2089     also have "\<dots> \<longleftrightarrow> ?a * (- (?d * ?t + ?c* ?s )) + (1 + 1)*?c*?d*?r \<noteq> 0" 
  2090       using nonzero_mult_divide_cancel_left[OF dc] c d
  2091       by (simp add: algebra_simps diff_divide_distrib del: left_distrib)
  2092     finally  have ?thesis using c d 
  2093       apply (simp add: r[of "(- (?d * ?t) + - (?c *?s)) / ((1 + 1) * ?c * ?d)"] msubstneq_def Let_def evaldjf_ex field_simps)
  2094       apply (simp only: one_add_one_is_two[symmetric] of_int_add)
  2095       apply (simp add: field_simps)
  2096       done }
  2097   ultimately show ?thesis by blast
  2098 qed
  2099 
  2100 definition "msubstlt c t d s a r = 
  2101   evaldjf (split conj) 
  2102   [(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)))),
  2103   (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)))),
  2104    (conj (lt (CP (~\<^sub>p c))) (Eq (CP d)) , Lt (Add (Mul (~\<^sub>p a) t) (Mul (2\<^sub>p *\<^sub>p c) r))),
  2105    (conj (lt (CP c)) (Eq (CP d)) , Lt (Sub (Mul a t) (Mul (2\<^sub>p *\<^sub>p c) r))),
  2106    (conj (lt (CP (~\<^sub>p d))) (Eq (CP c)) , Lt (Add (Mul (~\<^sub>p a) s) (Mul (2\<^sub>p *\<^sub>p d) r))),
  2107    (conj (lt (CP d)) (Eq (CP c)) , Lt (Sub (Mul a s) (Mul (2\<^sub>p *\<^sub>p d) r))),
  2108    (conj (Eq (CP c)) (Eq (CP d)) , Lt r)]"
  2109 
  2110 lemma msubstlt_nb: assumes lp: "islin (Lt (CNP 0 a r))" and t: "tmbound0 t" and s: "tmbound0 s"
  2111   shows "bound0 (msubstlt c t d s a r)"
  2112 proof-
  2113   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)))),
  2114   (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)))),
  2115    (conj (lt (CP (~\<^sub>p c))) (Eq (CP d)) , Lt (Add (Mul (~\<^sub>p a) t) (Mul (2\<^sub>p *\<^sub>p c) r))),
  2116    (conj (lt (CP c)) (Eq (CP d)) , Lt (Sub (Mul a t) (Mul (2\<^sub>p *\<^sub>p c) r))),
  2117    (conj (lt (CP (~\<^sub>p d))) (Eq (CP c)) , Lt (Add (Mul (~\<^sub>p a) s) (Mul (2\<^sub>p *\<^sub>p d) r))),
  2118    (conj (lt (CP d)) (Eq (CP c)) , Lt (Sub (Mul a s) (Mul (2\<^sub>p *\<^sub>p d) r))),
  2119    (conj (Eq (CP c)) (Eq (CP d)) , Lt r)]. bound0 (split conj x)"
  2120     using lp by (simp add: Let_def t s lt_nb )
  2121   from evaldjf_bound0[OF th] show ?thesis by (simp add: msubstlt_def)
  2122 qed
  2123 
  2124 
  2125 lemma msubstlt: assumes nc: "isnpoly c" and nd: "isnpoly d" and lp: "islin (Lt (CNP 0 a r))" 
  2126   shows "Ifm vs (x#bs) (msubstlt c t d s a r) \<longleftrightarrow> 
  2127   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")
  2128 proof-
  2129   let ?Nt = "\<lambda>x t. Itm vs (x#bs) t"
  2130   let ?N = "\<lambda>p. Ipoly vs p"
  2131   let ?c = "?N c"
  2132   let ?d = "?N d"
  2133   let ?t = "?Nt x t"
  2134   let ?s = "?Nt x s"
  2135   let ?a = "?N a"
  2136   let ?r = "?Nt x r"
  2137   from lp have lin:"isnpoly a" "a \<noteq> 0\<^sub>p" "tmbound0 r" "allpolys isnpoly r" by simp_all
  2138   note r= tmbound0_I[OF lin(3), of vs _ bs x]
  2139   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
  2140   moreover
  2141   {assume c: "?c=0" and d: "?d=0"
  2142     hence ?thesis  using nc nd by (simp add: polyneg_norm lt r[of 0] msubstlt_def Let_def evaldjf_ex)}
  2143   moreover
  2144   {assume dc: "?c*?d > 0" 
  2145     from mult_pos_pos[OF one_plus_one_pos dc] have dc': "(1 + 1)*?c *?d > 0" by simp
  2146     hence c:"?c \<noteq> 0" and d: "?d\<noteq> 0" by auto
  2147     from dc' have dc'': "\<not> (1 + 1)*?c *?d < 0" by simp
  2148     from add_frac_eq[OF c d, of "- ?t" "- ?s"]
  2149     have th: "(- ?t / ?c + - ?s / ?d)/(1 + 1) = - (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d)" 
  2150       by (simp add: field_simps)
  2151     have "?rhs \<longleftrightarrow> Ifm vs (- (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d) # bs) (Lt (CNP 0 a r))" by (simp only: th)
  2152     also have "\<dots> \<longleftrightarrow> ?a * (- (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d)) + ?r < 0" 
  2153       by (simp add: r[of "(- (?d * ?t) + - (?c *?s)) / ((1 + 1) * ?c * ?d)"])
  2154     also have "\<dots> \<longleftrightarrow> ((1 + 1) * ?c * ?d) * (?a * (- (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d)) + ?r) < 0"
  2155       
  2156       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
  2157     also have "\<dots> \<longleftrightarrow> ?a * (- (?d * ?t + ?c* ?s )) + (1 + 1)*?c*?d*?r < 0" 
  2158       using nonzero_mult_divide_cancel_left[of "(1 + 1)*?c*?d"] c d
  2159       by (simp add: algebra_simps diff_divide_distrib del: left_distrib)
  2160     finally  have ?thesis using dc c d  nc nd dc'
  2161       apply (simp add: r[of "(- (?d * ?t) + - (?c *?s)) / ((1 + 1) * ?c * ?d)"] msubstlt_def Let_def evaldjf_ex field_simps lt polyneg_norm polymul_norm) 
  2162     apply (simp only: one_add_one_is_two[symmetric] of_int_add)
  2163     by (simp add: field_simps order_less_not_sym[OF dc])}
  2164   moreover
  2165   {assume dc: "?c*?d < 0" 
  2166 
  2167     from dc one_plus_one_pos[where ?'a='a] have dc': "(1 + 1)*?c *?d < 0"
  2168       by (simp add: mult_less_0_iff field_simps) 
  2169     hence c:"?c \<noteq> 0" and d: "?d\<noteq> 0" by auto
  2170     from add_frac_eq[OF c d, of "- ?t" "- ?s"]
  2171     have th: "(- ?t / ?c + - ?s / ?d)/(1 + 1) = - (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d)" 
  2172       by (simp add: field_simps)
  2173     have "?rhs \<longleftrightarrow> Ifm vs (- (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d) # bs) (Lt (CNP 0 a r))" by (simp only: th)
  2174     also have "\<dots> \<longleftrightarrow> ?a * (- (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d)) + ?r < 0" 
  2175       by (simp add: r[of "(- (?d * ?t) + - (?c *?s)) / ((1 + 1) * ?c * ?d)"])
  2176 
  2177     also have "\<dots> \<longleftrightarrow> ((1 + 1) * ?c * ?d) * (?a * (- (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d)) + ?r) > 0"
  2178       
  2179       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
  2180     also have "\<dots> \<longleftrightarrow> ?a * ((?d * ?t + ?c* ?s )) - (1 + 1)*?c*?d*?r < 0" 
  2181       using nonzero_mult_divide_cancel_left[of "(1 + 1)*?c*?d"] c d
  2182       by (simp add: algebra_simps diff_divide_distrib del: left_distrib)
  2183     finally  have ?thesis using dc c d  nc nd
  2184       apply (simp add: r[of "(- (?d * ?t) + - (?c *?s)) / ((1 + 1) * ?c * ?d)"] msubstlt_def Let_def evaldjf_ex field_simps lt polyneg_norm polymul_norm) 
  2185       apply (simp only: one_add_one_is_two[symmetric] of_int_add)
  2186       by (simp add: field_simps order_less_not_sym[OF dc]) }
  2187   moreover
  2188   {assume c: "?c > 0" and d: "?d=0"  
  2189     from c have c'': "(1 + 1)*?c > 0" by (simp add: zero_less_mult_iff)
  2190     from c have c': "(1 + 1)*?c \<noteq> 0" by simp
  2191     from d have th: "(- ?t / ?c + - ?s / ?d)/(1 + 1) = - ?t / ((1 + 1)*?c)"  by (simp add: field_simps)
  2192     have "?rhs \<longleftrightarrow> Ifm vs (- ?t / ((1 + 1)*?c) # bs) (Lt (CNP 0 a r))" by (simp only: th)
  2193     also have "\<dots> \<longleftrightarrow> ?a* (- ?t / ((1 + 1)*?c))+ ?r < 0" by (simp add: r[of "- (?t / ((1 + 1)*?c))"])
  2194     also have "\<dots> \<longleftrightarrow> (1 + 1)*?c * (?a* (- ?t / ((1 + 1)*?c))+ ?r) < 0"
  2195       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
  2196     also have "\<dots> \<longleftrightarrow> - ?a*?t+  (1 + 1)*?c *?r < 0" 
  2197       using nonzero_mult_divide_cancel_left[OF c'] c
  2198       by (simp add: algebra_simps diff_divide_distrib less_le del: left_distrib)
  2199     finally have ?thesis using c d nc nd 
  2200       apply(simp add: r[of "- (?t / ((1 + 1)*?c))"] msubstlt_def Let_def evaldjf_ex field_simps lt polyneg_norm polymul_norm)
  2201       apply (simp only: one_add_one_is_two[symmetric] of_int_add)
  2202       using c order_less_not_sym[OF c] less_imp_neq[OF c]
  2203       by (simp add: field_simps )  }
  2204   moreover
  2205   {assume c: "?c < 0" and d: "?d=0"  hence c': "(1 + 1)*?c \<noteq> 0" by simp
  2206     from c have c'': "(1 + 1)*?c < 0" by (simp add: mult_less_0_iff)
  2207     from d have th: "(- ?t / ?c + - ?s / ?d)/(1 + 1) = - ?t / ((1 + 1)*?c)"  by (simp add: field_simps)
  2208     have "?rhs \<longleftrightarrow> Ifm vs (- ?t / ((1 + 1)*?c) # bs) (Lt (CNP 0 a r))" by (simp only: th)
  2209     also have "\<dots> \<longleftrightarrow> ?a* (- ?t / ((1 + 1)*?c))+ ?r < 0" by (simp add: r[of "- (?t / ((1 + 1)*?c))"])
  2210     also have "\<dots> \<longleftrightarrow> (1 + 1)*?c * (?a* (- ?t / ((1 + 1)*?c))+ ?r) > 0"
  2211       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
  2212     also have "\<dots> \<longleftrightarrow> ?a*?t -  (1 + 1)*?c *?r < 0" 
  2213       using nonzero_mult_divide_cancel_left[OF c'] c order_less_not_sym[OF c''] less_imp_neq[OF c''] c''
  2214         by (simp add: algebra_simps diff_divide_distrib del:  left_distrib)
  2215     finally have ?thesis using c d nc nd 
  2216       apply(simp add: r[of "- (?t / ((1 + 1)*?c))"] msubstlt_def Let_def evaldjf_ex field_simps lt polyneg_norm polymul_norm)
  2217       apply (simp only: one_add_one_is_two[symmetric] of_int_add)
  2218       using c order_less_not_sym[OF c] less_imp_neq[OF c]
  2219       by (simp add: field_simps )    }
  2220   moreover
  2221   moreover
  2222   {assume c: "?c = 0" and d: "?d>0"  
  2223     from d have d'': "(1 + 1)*?d > 0" by (simp add: zero_less_mult_iff)
  2224     from d have d': "(1 + 1)*?d \<noteq> 0" by simp
  2225     from c have th: "(- ?t / ?c + - ?s / ?d)/(1 + 1) = - ?s / ((1 + 1)*?d)"  by (simp add: field_simps)
  2226     have "?rhs \<longleftrightarrow> Ifm vs (- ?s / ((1 + 1)*?d) # bs) (Lt (CNP 0 a r))" by (simp only: th)
  2227     also have "\<dots> \<longleftrightarrow> ?a* (- ?s / ((1 + 1)*?d))+ ?r < 0" by (simp add: r[of "- (?s / ((1 + 1)*?d))"])
  2228     also have "\<dots> \<longleftrightarrow> (1 + 1)*?d * (?a* (- ?s / ((1 + 1)*?d))+ ?r) < 0"
  2229       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
  2230     also have "\<dots> \<longleftrightarrow> - ?a*?s+  (1 + 1)*?d *?r < 0" 
  2231       using nonzero_mult_divide_cancel_left[OF d'] d
  2232       by (simp add: algebra_simps diff_divide_distrib less_le del: left_distrib)
  2233     finally have ?thesis using c d nc nd 
  2234       apply(simp add: r[of "- (?s / ((1 + 1)*?d))"] msubstlt_def Let_def evaldjf_ex field_simps lt polyneg_norm polymul_norm)
  2235       apply (simp only: one_add_one_is_two[symmetric] of_int_add)
  2236       using d order_less_not_sym[OF d] less_imp_neq[OF d]
  2237       by (simp add: field_simps)  }
  2238   moreover
  2239   {assume c: "?c = 0" and d: "?d<0"  hence d': "(1 + 1)*?d \<noteq> 0" by simp
  2240     from d have d'': "(1 + 1)*?d < 0" by (simp add: mult_less_0_iff)
  2241     from c have th: "(- ?t / ?c + - ?s / ?d)/(1 + 1) = - ?s / ((1 + 1)*?d)"  by (simp add: field_simps)
  2242     have "?rhs \<longleftrightarrow> Ifm vs (- ?s / ((1 + 1)*?d) # bs) (Lt (CNP 0 a r))" by (simp only: th)
  2243     also have "\<dots> \<longleftrightarrow> ?a* (- ?s / ((1 + 1)*?d))+ ?r < 0" by (simp add: r[of "- (?s / ((1 + 1)*?d))"])
  2244     also have "\<dots> \<longleftrightarrow> (1 + 1)*?d * (?a* (- ?s / ((1 + 1)*?d))+ ?r) > 0"
  2245       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
  2246     also have "\<dots> \<longleftrightarrow> ?a*?s -  (1 + 1)*?d *?r < 0" 
  2247       using nonzero_mult_divide_cancel_left[OF d'] d order_less_not_sym[OF d''] less_imp_neq[OF d''] d''
  2248         by (simp add: algebra_simps diff_divide_distrib del:  left_distrib)
  2249     finally have ?thesis using c d nc nd 
  2250       apply(simp add: r[of "- (?s / ((1 + 1)*?d))"] msubstlt_def Let_def evaldjf_ex field_simps lt polyneg_norm polymul_norm)
  2251       apply (simp only: one_add_one_is_two[symmetric] of_int_add)
  2252       using d order_less_not_sym[OF d] less_imp_neq[OF d]
  2253       by (simp add: field_simps )    }
  2254 ultimately show ?thesis by blast
  2255 qed
  2256 
  2257 definition "msubstle c t d s a r = 
  2258   evaldjf (split conj) 
  2259   [(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)))),
  2260   (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)))),
  2261    (conj (lt (CP (~\<^sub>p c))) (Eq (CP d)) , Le (Add (Mul (~\<^sub>p a) t) (Mul (2\<^sub>p *\<^sub>p c) r))),
  2262    (conj (lt (CP c)) (Eq (CP d)) , Le (Sub (Mul a t) (Mul (2\<^sub>p *\<^sub>p c) r))),
  2263    (conj (lt (CP (~\<^sub>p d))) (Eq (CP c)) , Le (Add (Mul (~\<^sub>p a) s) (Mul (2\<^sub>p *\<^sub>p d) r))),
  2264    (conj (lt (CP d)) (Eq (CP c)) , Le (Sub (Mul a s) (Mul (2\<^sub>p *\<^sub>p d) r))),
  2265    (conj (Eq (CP c)) (Eq (CP d)) , Le r)]"
  2266 
  2267 lemma msubstle_nb: assumes lp: "islin (Le (CNP 0 a r))" and t: "tmbound0 t" and s: "tmbound0 s"
  2268   shows "bound0 (msubstle c t d s a r)"
  2269 proof-
  2270   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)))),
  2271   (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)))),
  2272    (conj (lt (CP (~\<^sub>p c))) (Eq (CP d)) , Le (Add (Mul (~\<^sub>p a) t) (Mul (2\<^sub>p *\<^sub>p c) r))),
  2273    (conj (lt (CP c)) (Eq (CP d)) , Le (Sub (Mul a t) (Mul (2\<^sub>p *\<^sub>p c) r))),
  2274    (conj (lt (CP (~\<^sub>p d))) (Eq (CP c)) , Le (Add (Mul (~\<^sub>p a) s) (Mul (2\<^sub>p *\<^sub>p d) r))),
  2275    (conj (lt (CP d)) (Eq (CP c)) , Le (Sub (Mul a s) (Mul (2\<^sub>p *\<^sub>p d) r))),
  2276    (conj (Eq (CP c)) (Eq (CP d)) , Le r)]. bound0 (split conj x)"
  2277     using lp by (simp add: Let_def t s lt_nb )
  2278   from evaldjf_bound0[OF th] show ?thesis by (simp add: msubstle_def)
  2279 qed
  2280 
  2281 lemma msubstle: assumes nc: "isnpoly c" and nd: "isnpoly d" and lp: "islin (Le (CNP 0 a r))" 
  2282   shows "Ifm vs (x#bs) (msubstle c t d s a r) \<longleftrightarrow> 
  2283   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")
  2284 proof-
  2285   let ?Nt = "\<lambda>x t. Itm vs (x#bs) t"
  2286   let ?N = "\<lambda>p. Ipoly vs p"
  2287   let ?c = "?N c"
  2288   let ?d = "?N d"
  2289   let ?t = "?Nt x t"
  2290   let ?s = "?Nt x s"
  2291   let ?a = "?N a"
  2292   let ?r = "?Nt x r"
  2293   from lp have lin:"isnpoly a" "a \<noteq> 0\<^sub>p" "tmbound0 r" "allpolys isnpoly r" by simp_all
  2294   note r= tmbound0_I[OF lin(3), of vs _ bs x]
  2295   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
  2296   moreover
  2297   {assume c: "?c=0" and d: "?d=0"
  2298     hence ?thesis  using nc nd by (simp add: polyneg_norm polymul_norm lt r[of 0] msubstle_def Let_def evaldjf_ex)}
  2299   moreover
  2300   {assume dc: "?c*?d > 0" 
  2301     from mult_pos_pos[OF one_plus_one_pos dc] have dc': "(1 + 1)*?c *?d > 0" by simp
  2302     hence c:"?c \<noteq> 0" and d: "?d\<noteq> 0" by auto
  2303     from dc' have dc'': "\<not> (1 + 1)*?c *?d < 0" by simp
  2304     from add_frac_eq[OF c d, of "- ?t" "- ?s"]
  2305     have th: "(- ?t / ?c + - ?s / ?d)/(1 + 1) = - (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d)" 
  2306       by (simp add: field_simps)
  2307     have "?rhs \<longleftrightarrow> Ifm vs (- (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d) # bs) (Le (CNP 0 a r))" by (simp only: th)
  2308     also have "\<dots> \<longleftrightarrow> ?a * (- (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d)) + ?r <= 0" 
  2309       by (simp add: r[of "(- (?d * ?t) + - (?c *?s)) / ((1 + 1) * ?c * ?d)"])
  2310     also have "\<dots> \<longleftrightarrow> ((1 + 1) * ?c * ?d) * (?a * (- (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d)) + ?r) <= 0"
  2311       
  2312       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
  2313     also have "\<dots> \<longleftrightarrow> ?a * (- (?d * ?t + ?c* ?s )) + (1 + 1)*?c*?d*?r <= 0" 
  2314       using nonzero_mult_divide_cancel_left[of "(1 + 1)*?c*?d"] c d
  2315       by (simp add: algebra_simps diff_divide_distrib del: left_distrib)
  2316     finally  have ?thesis using dc c d  nc nd dc'
  2317       apply (simp add: r[of "(- (?d * ?t) + - (?c *?s)) / ((1 + 1) * ?c * ?d)"] msubstle_def Let_def evaldjf_ex field_simps lt polyneg_norm polymul_norm) 
  2318     apply (simp only: one_add_one_is_two[symmetric] of_int_add)
  2319     by (simp add: field_simps order_less_not_sym[OF dc])}
  2320   moreover
  2321   {assume dc: "?c*?d < 0" 
  2322 
  2323     from dc one_plus_one_pos[where ?'a='a] have dc': "(1 + 1)*?c *?d < 0"
  2324       by (simp add: mult_less_0_iff field_simps add_neg_neg add_pos_pos)
  2325     hence c:"?c \<noteq> 0" and d: "?d\<noteq> 0" by auto
  2326     from add_frac_eq[OF c d, of "- ?t" "- ?s"]
  2327     have th: "(- ?t / ?c + - ?s / ?d)/(1 + 1) = - (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d)" 
  2328       by (simp add: field_simps)
  2329     have "?rhs \<longleftrightarrow> Ifm vs (- (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d) # bs) (Le (CNP 0 a r))" by (simp only: th)
  2330     also have "\<dots> \<longleftrightarrow> ?a * (- (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d)) + ?r <= 0" 
  2331       by (simp add: r[of "(- (?d * ?t) + - (?c *?s)) / ((1 + 1) * ?c * ?d)"])
  2332 
  2333     also have "\<dots> \<longleftrightarrow> ((1 + 1) * ?c * ?d) * (?a * (- (?d * ?t + ?c* ?s )/ ((1 + 1)*?c*?d)) + ?r) >= 0"
  2334       
  2335       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
  2336     also have "\<dots> \<longleftrightarrow> ?a * ((?d * ?t + ?c* ?s )) - (1 + 1)*?c*?d*?r <= 0" 
  2337       using nonzero_mult_divide_cancel_left[of "(1 + 1)*?c*?d"] c d
  2338       by (simp add: algebra_simps diff_divide_distrib del: left_distrib)
  2339     finally  have ?thesis using dc c d  nc nd
  2340       apply (simp add: r[of "(- (?d * ?t) + - (?c *?s)) / ((1 + 1) * ?c * ?d)"] msubstle_def Let_def evaldjf_ex field_simps lt polyneg_norm polymul_norm) 
  2341       apply (simp only: one_add_one_is_two[symmetric] of_int_add)
  2342       by (simp add: field_simps order_less_not_sym[OF dc]) }
  2343   moreover
  2344   {assume c: "?c > 0" and d: "?d=0"  
  2345     from c have c'': "(1 + 1)*?c > 0" by (simp add: zero_less_mult_iff)
  2346     from c have c': "(1 + 1)*?c \<noteq> 0" by simp
  2347     from d have th: "(- ?t / ?c + - ?s / ?d)/(1 + 1) = - ?t / ((1 + 1)*?c)"  by (simp add: field_simps)
  2348     have "?rhs \<longleftrightarrow> Ifm vs (- ?t / ((1 + 1)*?c) # bs) (Le (CNP 0 a r))" by (simp only: th)
  2349     also have "\<dots> \<longleftrightarrow> ?a* (- ?t / ((1 + 1)*?c))+ ?r <= 0" by (simp add: r[of "- (?t / ((1 + 1)*?c))"])
  2350     also have "\<dots> \<longleftrightarrow> (1 + 1)*?c * (?a* (- ?t / ((1 + 1)*?c))+ ?r) <= 0"
  2351       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
  2352     also have "\<dots> \<longleftrightarrow> - ?a*?t+  (1 + 1)*?c *?r <= 0" 
  2353       using nonzero_mult_divide_cancel_left[OF c'] c
  2354       by (simp add: algebra_simps diff_divide_distrib less_le del: left_distrib)
  2355     finally have ?thesis using c d nc nd 
  2356       apply(simp add: r[of "- (?t / ((1 + 1)*?c))"] msubstle_def Let_def evaldjf_ex field_simps lt polyneg_norm polymul_norm)
  2357       apply (simp only: one_add_one_is_two[symmetric] of_int_add)
  2358       using c order_less_not_sym[OF c] less_imp_neq[OF c]
  2359       by (simp add: field_simps )  }
  2360   moreover
  2361   {assume c: "?c < 0" and d: "?d=0"  hence c': "(1 + 1)*?c \<noteq> 0" by simp
  2362     from c have c'': "(1 + 1)*?c < 0" by (simp add: mult_less_0_iff)
  2363     from d have th: "(- ?t / ?c + - ?s / ?d)/(1 + 1) = - ?t / ((1 + 1)*?c)"  by (simp add: field_simps)
  2364     have "?rhs \<longleftrightarrow> Ifm vs (- ?t / ((1 + 1)*?c) # bs) (Le (CNP 0 a r))" by (simp only: th)
  2365     also have "\<dots> \<longleftrightarrow> ?a* (- ?t / ((1 + 1)*?c))+ ?r <= 0" by (simp add: r[of "- (?t / ((1 + 1)*?c))"])
  2366     also have "\<dots> \<longleftrightarrow> (1 + 1)*?c * (?a* (- ?t / ((1 + 1)*?c))+ ?r) >= 0"
  2367       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
  2368     also have "\<dots> \<longleftrightarrow> ?a*?t -  (1 + 1)*?c *?r <= 0" 
  2369       using nonzero_mult_divide_cancel_left[OF c'] c order_less_not_sym[OF c''] less_imp_neq[OF c''] c''
  2370         by (simp add: algebra_simps diff_divide_distrib del:  left_distrib)
  2371     finally have ?thesis using c d nc nd 
  2372       apply(simp add: r[of "- (?t / ((1 + 1)*?c))"] msubstle_def Let_def evaldjf_ex field_simps lt polyneg_norm polymul_norm)
  2373       apply (simp only: one_add_one_is_two[symmetric] of_int_add)
  2374       using c order_less_not_sym[OF c] less_imp_neq[OF c]
  2375       by (simp add: field_simps )    }
  2376   moreover
  2377   moreover
  2378   {assume c: "?c = 0" and d: "?d>0"  
  2379     from d have d'': "(1 + 1)*?d > 0" by (simp add: zero_less_mult_iff)
  2380     from d have d': "(1 + 1)*?d \<noteq> 0" by simp
  2381     from c have th: "(- ?t / ?c + - ?s / ?d)/(1 + 1) = - ?s / ((1 + 1)*?d)"  by (simp add: field_simps)
  2382     have "?rhs \<longleftrightarrow> Ifm vs (- ?s / ((1 + 1)*?d) # bs) (Le (CNP 0 a r))" by (simp only: th)
  2383     also have "\<dots> \<longleftrightarrow> ?a* (- ?s / ((1 + 1)*?d))+ ?r <= 0" by (simp add: r[of "- (?s / ((1 + 1)*?d))"])
  2384     also have "\<dots> \<longleftrightarrow> (1 + 1)*?d * (?a* (- ?s / ((1 + 1)*?d))+ ?r) <= 0"
  2385       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
  2386     also have "\<dots> \<longleftrightarrow> - ?a*?s+  (1 + 1)*?d *?r <= 0" 
  2387       using nonzero_mult_divide_cancel_left[OF d'] d
  2388       by (simp add: algebra_simps diff_divide_distrib less_le del: left_distrib)
  2389     finally have ?thesis using c d nc nd 
  2390       apply(simp add: r[of "- (?s / ((1 + 1)*?d))"] msubstle_def Let_def evaldjf_ex field_simps lt polyneg_norm polymul_norm)
  2391       apply (simp only: one_add_one_is_two[symmetric] of_int_add)
  2392       using d order_less_not_sym[OF d] less_imp_neq[OF d]
  2393       by (simp add: field_simps )  }
  2394   moreover
  2395   {assume c: "?c = 0" and d: "?d<0"  hence d': "(1 + 1)*?d \<noteq> 0" by simp
  2396     from d have d'': "(1 + 1)*?d < 0" by (simp add: mult_less_0_iff)
  2397     from c have th: "(- ?t / ?c + - ?s / ?d)/(1 + 1) = - ?s / ((1 + 1)*?d)"  by (simp add: field_simps)
  2398     have "?rhs \<longleftrightarrow> Ifm vs (- ?s / ((1 + 1)*?d) # bs) (Le (CNP 0 a r))" by (simp only: th)
  2399     also have "\<dots> \<longleftrightarrow> ?a* (- ?s / ((1 + 1)*?d))+ ?r <= 0" by (simp add: r[of "- (?s / ((1 + 1)*?d))"])
  2400     also have "\<dots> \<longleftrightarrow> (1 + 1)*?d * (?a* (- ?s / ((1 + 1)*?d))+ ?r) >= 0"
  2401       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
  2402     also have "\<dots> \<longleftrightarrow> ?a*?s -  (1 + 1)*?d *?r <= 0" 
  2403       using nonzero_mult_divide_cancel_left[OF d'] d order_less_not_sym[OF d''] less_imp_neq[OF d''] d''
  2404         by (simp add: algebra_simps diff_divide_distrib del:  left_distrib)
  2405     finally have ?thesis using c d nc nd 
  2406       apply(simp add: r[of "- (?s / ((1 + 1)*?d))"] msubstle_def Let_def evaldjf_ex field_simps lt polyneg_norm polymul_norm)
  2407       apply (simp only: one_add_one_is_two[symmetric] of_int_add)
  2408       using d order_less_not_sym[OF d] less_imp_neq[OF d]
  2409       by (simp add: field_simps )    }
  2410 ultimately show ?thesis by blast
  2411 qed
  2412 
  2413 
  2414 fun msubst :: "fm \<Rightarrow> (poly \<times> tm) \<times> (poly \<times> tm) \<Rightarrow> fm" where
  2415   "msubst (And p q) ((c,t), (d,s)) = conj (msubst p ((c,t),(d,s))) (msubst q ((c,t),(d,s)))"
  2416 | "msubst (Or p q) ((c,t), (d,s)) = disj (msubst p ((c,t),(d,s))) (msubst q ((c,t), (d,s)))"
  2417 | "msubst (Eq (CNP 0 a r)) ((c,t),(d,s)) = msubsteq c t d s a r"
  2418 | "msubst (NEq (CNP 0 a r)) ((c,t),(d,s)) = msubstneq c t d s a r"
  2419 | "msubst (Lt (CNP 0 a r)) ((c,t),(d,s)) = msubstlt c t d s a r"
  2420 | "msubst (Le (CNP 0 a r)) ((c,t),(d,s)) = msubstle c t d s a r"
  2421 | "msubst p ((c,t),(d,s)) = p"
  2422 
  2423 lemma msubst_I: assumes lp: "islin p" and nc: "isnpoly c" and nd: "isnpoly d"
  2424   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"
  2425   using lp
  2426 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])
  2427 
  2428 lemma msubst_nb: assumes lp: "islin p" and t: "tmbound0 t" and s: "tmbound0 s"
  2429   shows "bound0 (msubst p ((c,t),(d,s)))"
  2430   using lp t s
  2431   by (induct p rule: islin.induct, auto simp add: msubsteq_nb msubstneq_nb msubstlt_nb msubstle_nb)
  2432 
  2433 lemma fr_eq_msubst: 
  2434   assumes lp: "islin p"
  2435   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)))))"
  2436   (is "(\<exists> x. ?I x p) = (?M \<or> ?P \<or> ?F)" is "?E = ?D")
  2437 proof-
  2438 from uset_l[OF lp] have th: "\<forall>(c, s)\<in>set (uset p). isnpoly c \<and> tmbound0 s" by blast
  2439 {fix c t d s assume ctU: "(c,t) \<in>set (uset p)" and dsU: "(d,s) \<in>set (uset p)" 
  2440   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"
  2441   from th[rule_format, OF ctU] th[rule_format, OF dsU] have norm:"isnpoly c" "isnpoly d" by simp_all
  2442   from msubst_I[OF lp norm, of vs x bs t s] pts
  2443   have "Ifm vs (x # bs) (msubst p ((c, t), d, s))" ..}
  2444 moreover
  2445 {fix c t d s assume ctU: "(c,t) \<in>set (uset p)" and dsU: "(d,s) \<in>set (uset p)" 
  2446   and pts: "Ifm vs (x # bs) (msubst p ((c, t), d, s))"
  2447   from th[rule_format, OF ctU] th[rule_format, OF dsU] have norm:"isnpoly c" "isnpoly d" by simp_all
  2448   from msubst_I[OF lp norm, of vs x bs t s] pts
  2449   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" ..}
  2450 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
  2451 from fr_eq[OF lp, of vs bs x, simplified th'] show ?thesis .
  2452 qed 
  2453 
  2454 text {* Rest of the implementation *}
  2455 
  2456 primrec alluopairs:: "'a list \<Rightarrow> ('a \<times> 'a) list" where
  2457   "alluopairs [] = []"
  2458 | "alluopairs (x#xs) = (map (Pair x) (x#xs))@(alluopairs xs)"
  2459 
  2460 lemma alluopairs_set1: "set (alluopairs xs) \<le> {(x,y). x\<in> set xs \<and> y\<in> set xs}"
  2461 by (induct xs, auto)
  2462 
  2463 lemma alluopairs_set:
  2464   "\<lbrakk>x\<in> set xs ; y \<in> set xs\<rbrakk> \<Longrightarrow> (x,y) \<in> set (alluopairs xs) \<or> (y,x) \<in> set (alluopairs xs) "
  2465 by (induct xs, auto)
  2466 
  2467 lemma alluopairs_ex:
  2468   assumes Pc: "\<forall> x \<in> set xs. \<forall>y\<in> set xs. P x y = P y x"
  2469   shows "(\<exists> x \<in> set xs. \<exists> y \<in> set xs. P x y) = (\<exists> (x,y) \<in> set (alluopairs xs). P x y)"
  2470 proof
  2471   assume "\<exists>x\<in>set xs. \<exists>y\<in>set xs. P x y"
  2472   then obtain x y where x: "x \<in> set xs" and y:"y \<in> set xs" and P: "P x y"  by blast
  2473   from alluopairs_set[OF x y] P Pc x y show"\<exists>(x, y)\<in>set (alluopairs xs). P x y" 
  2474     by auto
  2475 next
  2476   assume "\<exists>(x, y)\<in>set (alluopairs xs). P x y"
  2477   then obtain "x" and "y"  where xy:"(x,y) \<in> set (alluopairs xs)" and P: "P x y" by blast+
  2478   from xy have "x \<in> set xs \<and> y\<in> set xs" using alluopairs_set1 by blast
  2479   with P show "\<exists>x\<in>set xs. \<exists>y\<in>set xs. P x y" by blast
  2480 qed
  2481 
  2482 lemma nth_pos2: "0 < n \<Longrightarrow> (x#xs) ! n = xs ! (n - 1)"
  2483 using Nat.gr0_conv_Suc
  2484 by clarsimp
  2485 
  2486 lemma filter_length: "length (List.filter P xs) < Suc (length xs)"
  2487   apply (induct xs, auto) done
  2488 
  2489 consts remdps:: "'a list \<Rightarrow> 'a list"
  2490 
  2491 recdef remdps "measure size"
  2492   "remdps [] = []"
  2493   "remdps (x#xs) = (x#(remdps (List.filter (\<lambda> y. y \<noteq> x) xs)))"
  2494 (hints simp add: filter_length[rule_format])
  2495 
  2496 lemma remdps_set[simp]: "set (remdps xs) = set xs"
  2497   by (induct xs rule: remdps.induct, auto)
  2498 
  2499 lemma simpfm_lin:   assumes "SORT_CONSTRAINT('a::{field_char_0, field_inverse_zero})"
  2500   shows "qfree p \<Longrightarrow> islin (simpfm p)"
  2501   by (induct p rule: simpfm.induct, auto simp add: conj_lin disj_lin)
  2502 
  2503 definition 
  2504   "ferrack p \<equiv> let q = simpfm p ; mp = minusinf q ; pp = plusinf q
  2505   in if (mp = T \<or> pp = T) then T 
  2506      else (let U = alluopairs (remdps (uset  q))
  2507            in decr0 (disj mp (disj pp (evaldjf (simpfm o (msubst q)) U ))))"
  2508 
  2509 lemma ferrack: 
  2510   assumes qf: "qfree p"
  2511   shows "qfree (ferrack p) \<and> ((Ifm vs bs (ferrack p)) = (Ifm vs bs (E p)))"
  2512   (is "_ \<and> (?rhs = ?lhs)")
  2513 proof-
  2514   let ?I = "\<lambda> x p. Ifm vs (x#bs) p"
  2515   let ?N = "\<lambda> t. Ipoly vs t"
  2516   let ?Nt = "\<lambda>x t. Itm vs (x#bs) t"
  2517   let ?q = "simpfm p" 
  2518   let ?U = "remdps(uset ?q)"
  2519   let ?Up = "alluopairs ?U"
  2520   let ?mp = "minusinf ?q"
  2521   let ?pp = "plusinf ?q"
  2522   let ?I = "\<lambda>p. Ifm vs (x#bs) p"
  2523   from simpfm_lin[OF qf] simpfm_qf[OF qf] have lq: "islin ?q" and q_qf: "qfree ?q" .
  2524   from minusinf_nb[OF lq] plusinf_nb[OF lq] have mp_nb: "bound0 ?mp" and pp_nb: "bound0 ?pp" .
  2525   from bound0_qf[OF mp_nb] bound0_qf[OF pp_nb] have mp_qf: "qfree ?mp" and pp_qf: "qfree ?pp" .
  2526   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"
  2527     by simp
  2528   {fix c t d s assume ctU: "(c,t) \<in> set ?U" and dsU: "(d,s) \<in> set ?U"
  2529     from U_l ctU dsU have norm: "isnpoly c" "isnpoly d" by auto
  2530     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]
  2531     have "?I (msubst ?q ((c,t),(d,s))) = ?I (msubst ?q ((d,s),(c,t)))" by (simp add: field_simps)}
  2532   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
  2533   {fix x assume xUp: "x \<in> set ?Up" 
  2534     then  obtain c t d s where ctU: "(c,t) \<in> set ?U" and dsU: "(d,s) \<in> set ?U" 
  2535       and x: "x = ((c,t),(d,s))" using alluopairs_set1[of ?U] by auto  
  2536     from U_l[rule_format, OF ctU] U_l[rule_format, OF dsU] 
  2537     have nbs: "tmbound0 t" "tmbound0 s" by simp_all
  2538     from simpfm_bound0[OF msubst_nb[OF lq nbs, of c d]] 
  2539     have "bound0 ((simpfm o (msubst (simpfm p))) x)" using x by simp}
  2540   with evaldjf_bound0[of ?Up "(simpfm o (msubst (simpfm p)))"]
  2541   have "bound0 (evaldjf (simpfm o (msubst (simpfm p))) ?Up)" by blast
  2542   with mp_nb pp_nb 
  2543   have th1: "bound0 (disj ?mp (disj ?pp (evaldjf (simpfm o (msubst ?q)) ?Up )))" by (simp add: disj_nb)
  2544   from decr0_qf[OF th1] have thqf: "qfree (ferrack p)" by (simp add: ferrack_def Let_def)
  2545   have "?lhs \<longleftrightarrow> (\<exists>x. Ifm vs (x#bs) ?q)" by simp
  2546   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
  2547   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
  2548   also have "\<dots> \<longleftrightarrow> ?I ?mp \<or> ?I ?pp \<or> ?I (evaldjf (simpfm o (msubst ?q)) ?Up)" 
  2549     by (simp add: evaldjf_ex)
  2550   also have "\<dots> \<longleftrightarrow> ?I (disj ?mp (disj ?pp (evaldjf (simpfm o (msubst ?q)) ?Up)))" by simp
  2551   also have "\<dots> \<longleftrightarrow> ?rhs" using decr0[OF th1, of vs x bs]
  2552     apply (simp add: ferrack_def Let_def)
  2553     by (cases "?mp = T \<or> ?pp = T", auto)
  2554   finally show ?thesis using thqf by blast
  2555 qed
  2556 
  2557 definition "frpar p = simpfm (qelim p ferrack)"
  2558 lemma frpar: "qfree (frpar p) \<and> (Ifm vs bs (frpar p) \<longleftrightarrow> Ifm vs bs p)"
  2559 proof-
  2560   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
  2561   from qelim[OF th, of p bs] show ?thesis  unfolding frpar_def by auto
  2562 qed
  2563 
  2564 
  2565 section{* Second implemenation: Case splits not local *}
  2566 
  2567 lemma fr_eq2:  assumes lp: "islin p"
  2568   shows "(\<exists> x. Ifm vs (x#bs) p) \<longleftrightarrow> 
  2569    ((Ifm vs (x#bs) (minusinf p)) \<or> (Ifm vs (x#bs) (plusinf p)) \<or> 
  2570     (Ifm vs (0#bs) p) \<or> 
  2571     (\<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> 
  2572     (\<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))"
  2573   (is "(\<exists> x. ?I x p) = (?M \<or> ?P \<or> ?Z \<or> ?U \<or> ?F)" is "?E = ?D")
  2574 proof
  2575   assume px: "\<exists> x. ?I x p"
  2576   have "?M \<or> ?P \<or> (\<not> ?M \<and> \<not> ?P)" by blast
  2577   moreover {assume "?M \<or> ?P" hence "?D" by blast}
  2578   moreover {assume nmi: "\<not> ?M" and npi: "\<not> ?P"
  2579     from inf_uset[OF lp nmi npi, OF px] 
  2580     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"
  2581       by auto
  2582     let ?c = "\<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup>"
  2583     let ?d = "\<lparr>d\<rparr>\<^sub>p\<^bsup>vs\<^esup>"
  2584     let ?s = "Itm vs (x # bs) s"
  2585     let ?t = "Itm vs (x # bs) t"
  2586     have eq2: "\<And>(x::'a). x + x = (1 + 1) * x"
  2587       by  (simp add: field_simps)
  2588     {assume "?c = 0 \<and> ?d = 0"
  2589       with ct have ?D by simp}
  2590     moreover
  2591     {assume z: "?c = 0" "?d \<noteq> 0"
  2592       from z have ?D using ct by auto}
  2593     moreover
  2594     {assume z: "?c \<noteq> 0" "?d = 0"
  2595       with ct have ?D by auto }
  2596     moreover
  2597     {assume z: "?c \<noteq> 0" "?d \<noteq> 0"
  2598       from z have ?F using ct
  2599         apply - apply (rule bexI[where x = "(c,t)"], simp_all)
  2600         by (rule bexI[where x = "(d,s)"], simp_all)
  2601       hence ?D by blast}
  2602     ultimately have ?D by auto}
  2603   ultimately show "?D" by blast
  2604 next
  2605   assume "?D" 
  2606   moreover {assume m:"?M" from minusinf_ex[OF lp m] have "?E" .}
  2607   moreover {assume p: "?P" from plusinf_ex[OF lp p] have "?E" . }
  2608   moreover {assume f:"?F" hence "?E" by blast}
  2609   ultimately show "?E" by blast
  2610 qed
  2611 
  2612 definition "msubsteq2 c t a b = Eq (Add (Mul a t) (Mul c b))"
  2613 definition "msubstltpos c t a b = Lt (Add (Mul a t) (Mul c b))"
  2614 definition "msubstlepos c t a b = Le (Add (Mul a t) (Mul c b))"
  2615 definition "msubstltneg c t a b = Lt (Neg (Add (Mul a t) (Mul c b)))"
  2616 definition "msubstleneg c t a b = Le (Neg (Add (Mul a t) (Mul c b)))"
  2617 
  2618 lemma msubsteq2: 
  2619   assumes nz: "Ipoly vs c \<noteq> 0" and l: "islin (Eq (CNP 0 a b))"
  2620   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")
  2621   using nz l tmbound0_I[of b vs x bs "Itm vs (x # bs) t / \<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup>" , symmetric]
  2622   by (simp add: msubsteq2_def field_simps)
  2623 
  2624 lemma msubstltpos: 
  2625   assumes nz: "Ipoly vs c > 0" and l: "islin (Lt (CNP 0 a b))"
  2626   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")
  2627   using nz l tmbound0_I[of b vs x bs "Itm vs (x # bs) t / \<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup>" , symmetric]
  2628   by (simp add: msubstltpos_def field_simps)
  2629 
  2630 lemma msubstlepos: 
  2631   assumes nz: "Ipoly vs c > 0" and l: "islin (Le (CNP 0 a b))"
  2632   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")
  2633   using nz l tmbound0_I[of b vs x bs "Itm vs (x # bs) t / \<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup>" , symmetric]
  2634   by (simp add: msubstlepos_def field_simps)
  2635 
  2636 lemma msubstltneg: 
  2637   assumes nz: "Ipoly vs c < 0" and l: "islin (Lt (CNP 0 a b))"
  2638   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")
  2639   using nz l tmbound0_I[of b vs x bs "Itm vs (x # bs) t / \<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup>" , symmetric]
  2640   by (simp add: msubstltneg_def field_simps del: minus_add_distrib)
  2641 
  2642 lemma msubstleneg: 
  2643   assumes nz: "Ipoly vs c < 0" and l: "islin (Le (CNP 0 a b))"
  2644   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")
  2645   using nz l tmbound0_I[of b vs x bs "Itm vs (x # bs) t / \<lparr>c\<rparr>\<^sub>p\<^bsup>vs\<^esup>" , symmetric]
  2646   by (simp add: msubstleneg_def field_simps del: minus_add_distrib)
  2647 
  2648 fun msubstpos :: "fm \<Rightarrow> poly \<Rightarrow> tm \<Rightarrow> fm" where
  2649   "msubstpos (And p q) c t = And (msubstpos p c t) (msubstpos q c t)"
  2650 | "msubstpos (Or p q) c t = Or (msubstpos p c t) (msubstpos q c t)"
  2651 | "msubstpos (Eq (CNP 0 a r)) c t = msubsteq2 c t a r"
  2652 | "msubstpos (NEq (CNP 0 a r)) c t = NOT (msubsteq2 c t a r)"
  2653 | "msubstpos (Lt (CNP 0 a r)) c t = msubstltpos c t a r"
  2654 | "msubstpos (Le (CNP 0 a r)) c t = msubstlepos c t a r"
  2655 | "msubstpos p c t = p"
  2656     
  2657 lemma msubstpos_I: 
  2658   assumes lp: "islin p" and pos: "Ipoly vs c > 0"
  2659   shows "Ifm vs (x#bs) (msubstpos p c t) = Ifm vs (Itm vs (x#bs) t /  Ipoly vs c #bs) p"
  2660   using lp pos
  2661   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)
  2662 
  2663 fun msubstneg :: "fm \<Rightarrow> poly \<Rightarrow> tm \<Rightarrow> fm" where
  2664   "msubstneg (And p q) c t = And (msubstneg p c t) (msubstneg q c t)"
  2665 | "msubstneg (Or p q) c t = Or (msubstneg p c t) (msubstneg q c t)"
  2666 | "msubstneg (Eq (CNP 0 a r)) c t = msubsteq2 c t a r"
  2667 | "msubstneg (NEq (CNP 0 a r)) c t = NOT (msubsteq2 c t a r)"
  2668 | "msubstneg (Lt (CNP 0 a r)) c t = msubstltneg c t a r"
  2669 | "msubstneg (Le (CNP 0 a r)) c t = msubstleneg c t a r"
  2670 | "msubstneg p c t = p"
  2671 
  2672 lemma msubstneg_I: 
  2673   assumes lp: "islin p" and pos: "Ipoly vs c < 0"
  2674   shows "Ifm vs (x#bs) (msubstneg p c t) = Ifm vs (Itm vs (x#bs) t /  Ipoly vs c #bs) p"
  2675   using lp pos
  2676   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)
  2677 
  2678 
  2679 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)))"
  2680 
  2681 lemma msubst2: assumes lp: "islin p" and nc: "isnpoly c" and nz: "Ipoly vs c \<noteq> 0"
  2682   shows "Ifm vs (x#bs) (msubst2 p c t) = Ifm vs (Itm vs (x#bs) t /  Ipoly vs c #bs) p"
  2683 proof-
  2684   let ?c = "Ipoly vs c"
  2685   from nc have anc: "allpolys isnpoly (CP c)" "allpolys isnpoly (CP (~\<^sub>p c))" 
  2686     by (simp_all add: polyneg_norm)
  2687   from nz have "?c > 0 \<or> ?c < 0" by arith
  2688   moreover
  2689   {assume c: "?c < 0"
  2690     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"]
  2691     have ?thesis by (auto simp add: msubst2_def)}
  2692   moreover
  2693   {assume c: "?c > 0"
  2694     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"]
  2695     have ?thesis by (auto simp add: msubst2_def)}
  2696   ultimately show ?thesis by blast
  2697 qed
  2698 
  2699 term msubsteq2
  2700 lemma msubsteq2_nb: "tmbound0 t \<Longrightarrow> islin (Eq (CNP 0 a r)) \<Longrightarrow> bound0 (msubsteq2 c t a r)"
  2701   by (simp add: msubsteq2_def)
  2702 
  2703 lemma msubstltpos_nb: "tmbound0 t \<Longrightarrow> islin (Lt (CNP 0 a r)) \<Longrightarrow> bound0 (msubstltpos c t a r)"
  2704   by (simp add: msubstltpos_def)
  2705 lemma msubstltneg_nb: "tmbound0 t \<Longrightarrow> islin (Lt (CNP 0 a r)) \<Longrightarrow> bound0 (msubstltneg c t a r)"
  2706   by (simp add: msubstltneg_def)
  2707 
  2708 lemma msubstlepos_nb: "tmbound0 t \<Longrightarrow> islin (Le (CNP 0 a r)) \<Longrightarrow> bound0 (msubstlepos c t a r)"
  2709   by (simp add: msubstlepos_def)
  2710 lemma msubstleneg_nb: "tmbound0 t \<Longrightarrow> islin (Le (CNP 0 a r)) \<Longrightarrow> bound0 (msubstleneg c t a r)"
  2711   by (simp add: msubstleneg_def)
  2712 
  2713 lemma msubstpos_nb: assumes lp: "islin p" and tnb: "tmbound0 t"
  2714   shows "bound0 (msubstpos p c t)"
  2715 using lp tnb
  2716 by (induct p c t rule: msubstpos.induct, auto simp add: msubsteq2_nb msubstltpos_nb msubstlepos_nb)
  2717 
  2718 lemma msubstneg_nb: assumes "SORT_CONSTRAINT('a::{field_char_0, field_inverse_zero})" and lp: "islin p" and tnb: "tmbound0 t"
  2719   shows "bound0 (msubstneg p c t)"
  2720 using lp tnb
  2721 by (induct p c t rule: msubstneg.induct, auto simp add: msubsteq2_nb msubstltneg_nb msubstleneg_nb)
  2722 
  2723 lemma msubst2_nb: assumes "SORT_CONSTRAINT('a::{field_char_0, field_inverse_zero})" and lp: "islin p" and tnb: "tmbound0 t"
  2724   shows "bound0 (msubst2 p c t)"
  2725 using lp tnb
  2726 by (simp add: msubst2_def msubstneg_nb msubstpos_nb conj_nb disj_nb lt_nb simpfm_bound0)
  2727     
  2728 lemma of_int2: "of_int 2 = 1 + 1"
  2729 proof-
  2730   have "(2::int) = 1 + 1" by simp
  2731   hence "of_int 2 = of_int (1 + 1)" by simp
  2732   thus ?thesis unfolding of_int_add by simp
  2733 qed
  2734 
  2735 lemma of_int_minus2: "of_int (-2) = - (1 + 1)"
  2736 proof-
  2737   have th: "(-2::int) = - 2" by simp
  2738   show ?thesis unfolding th by (simp only: of_int_minus of_int2)
  2739 qed
  2740 
  2741 
  2742 lemma islin_qf: "islin p \<Longrightarrow> qfree p"
  2743   by (induct p rule: islin.induct, auto simp add: bound0_qf)
  2744 lemma fr_eq_msubst2: 
  2745   assumes lp: "islin p"
  2746   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)))))"
  2747   (is "(\<exists> x. ?I x p) = (?M \<or> ?P \<or> ?Pz \<or> ?PU \<or> ?F)" is "?E = ?D")
  2748 proof-
  2749   from uset_l[OF lp] have th: "\<forall>(c, s)\<in>set (uset p). isnpoly c \<and> tmbound0 s" by blast
  2750   let ?I = "\<lambda>p. Ifm vs (x#bs) p"
  2751   have n2: "isnpoly (C (-2,1))" by (simp add: isnpoly_def)
  2752   note eq0 = subst0[OF islin_qf[OF lp], of vs x bs "CP 0\<^sub>p", simplified]
  2753   
  2754   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)"
  2755   proof-
  2756     {fix n t assume H: "(n, t)\<in>set (uset p)" "?I(msubst2 p (n *\<^sub>p C (-2, 1)) t)"
  2757       from H(1) th have "isnpoly n" by blast
  2758       hence nn: "isnpoly (n *\<^sub>p (C (-2,1)))" by (simp_all add: polymul_norm n2)
  2759       have nn': "allpolys isnpoly (CP (~\<^sub>p (n *\<^sub>p C (-2, 1))))"
  2760         by (simp add: polyneg_norm nn)
  2761       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 
  2762         by (auto simp add: msubst2_def lt zero_less_mult_iff mult_less_0_iff)
  2763       from msubst2[OF lp nn nn2(1), of x bs t]
  2764       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"
  2765         using H(2) nn2 by (simp add: of_int_minus2 del: minus_add_distrib)}
  2766     moreover
  2767     {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"
  2768       from H(1) th have "isnpoly n" by blast
  2769       hence nn: "isnpoly (n *\<^sub>p (C (-2,1)))" "\<lparr>n *\<^sub>p(C (-2,1)) \<rparr>\<^sub>p\<^bsup>vs\<^esup> \<noteq> 0"
  2770         using H(2) by (simp_all add: polymul_norm n2)
  2771       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)}
  2772     ultimately show ?thesis by blast
  2773   qed
  2774   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).
  2775      \<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)" 
  2776   proof-
  2777     {fix c t d s assume H: "(c,t) \<in> set (uset p)" "(d,s) \<in> set (uset p)" 
  2778      "Ifm vs (x#bs) (msubst2 p (C (-2, 1) *\<^sub>p c*\<^sub>p d) (Add (Mul d t) (Mul c s)))"
  2779       from H(1,2) th have "isnpoly c" "isnpoly d" by blast+
  2780       hence nn: "isnpoly (C (-2, 1) *\<^sub>p c*\<^sub>p d)" 
  2781         by (simp_all add: polymul_norm n2)
  2782       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)))"
  2783         by (simp_all add: polyneg_norm nn)
  2784       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"
  2785         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)
  2786       from msubst2[OF lp nn nn'(1), of x bs ] H(3) nn'
  2787       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" 
  2788         apply (simp add: add_divide_distrib of_int_minus2 del: minus_add_distrib)
  2789         by (simp add: mult_commute)}
  2790     moreover
  2791     {fix c t d s assume H: "(c,t) \<in> set (uset p)" "(d,s) \<in> set (uset p)" 
  2792       "\<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"
  2793      from H(1,2) th have "isnpoly c" "isnpoly d" by blast+
  2794       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"
  2795         using H(3,4) by (simp_all add: polymul_norm n2)
  2796       from msubst2[OF lp nn, of x bs ] H(3,4,5) 
  2797       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)}
  2798     ultimately show ?thesis by blast
  2799   qed
  2800   from fr_eq2[OF lp, of vs bs x] show ?thesis
  2801     unfolding eq0 eq1 eq2 by blast  
  2802 qed
  2803 
  2804 definition 
  2805 "ferrack2 p \<equiv> let q = simpfm p ; mp = minusinf q ; pp = plusinf q
  2806  in if (mp = T \<or> pp = T) then T 
  2807   else (let U = remdps (uset  q)
  2808     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, 
  2809    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)]))"
  2810 
  2811 definition "frpar2 p = simpfm (qelim (prep p) ferrack2)"
  2812 
  2813 lemma ferrack2: assumes qf: "qfree p"
  2814   shows "qfree (ferrack2 p) \<and> ((Ifm vs bs (ferrack2 p)) = (Ifm vs bs (E p)))"
  2815   (is "_ \<and> (?rhs = ?lhs)")
  2816 proof-
  2817   let ?J = "\<lambda> x p. Ifm vs (x#bs) p"
  2818   let ?N = "\<lambda> t. Ipoly vs t"
  2819   let ?Nt = "\<lambda>x t. Itm vs (x#bs) t"
  2820   let ?q = "simpfm p" 
  2821   let ?qz = "subst0 (CP 0\<^sub>p) ?q"
  2822   let ?U = "remdps(uset ?q)"
  2823   let ?Up = "alluopairs ?U"
  2824   let ?mp = "minusinf ?q"
  2825   let ?pp = "plusinf ?q"
  2826   let ?I = "\<lambda>p. Ifm vs (x#bs) p"
  2827   from simpfm_lin[OF qf] simpfm_qf[OF qf] have lq: "islin ?q" and q_qf: "qfree ?q" .
  2828   from minusinf_nb[OF lq] plusinf_nb[OF lq] have mp_nb: "bound0 ?mp" and pp_nb: "bound0 ?pp" .
  2829   from bound0_qf[OF mp_nb] bound0_qf[OF pp_nb] have mp_qf: "qfree ?mp" and pp_qf: "qfree ?pp" .
  2830   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"
  2831     by simp
  2832   have bnd0: "\<forall>x \<in> set ?U. bound0 ((\<lambda>(c,t). msubst2 ?q (c *\<^sub>p C (-2, 1)) t) x)" 
  2833   proof-
  2834     {fix c t assume ct: "(c,t) \<in> set ?U"
  2835       hence tnb: "tmbound0 t" using U_l by blast
  2836       from msubst2_nb[OF lq tnb]
  2837       have "bound0 ((\<lambda>(c,t). msubst2 ?q (c *\<^sub>p C (-2, 1)) t) (c,t))" by simp}
  2838     thus ?thesis by auto
  2839   qed
  2840   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)" 
  2841   proof-
  2842     {fix b a d c assume badc: "((b,a),(d,c)) \<in> set ?Up"
  2843       from badc U_l alluopairs_set1[of ?U] 
  2844       have nb: "tmbound0 (Add (Mul d a) (Mul b c))" by auto
  2845       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}
  2846     thus ?thesis by auto
  2847   qed
  2848   have stupid: "bound0 F" by simp
  2849   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, 
  2850    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)]"
  2851   from subst0_nb[of "CP 0\<^sub>p" ?q] q_qf evaldjf_bound0[OF bnd1] evaldjf_bound0[OF bnd0] mp_nb pp_nb stupid
  2852   have nb: "bound0 ?R "
  2853     by (simp add: list_disj_def disj_nb0 simpfm_bound0)
  2854   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))"
  2855 
  2856   {fix b a d c assume baU: "(b,a) \<in> set ?U" and dcU: "(d,c) \<in> set ?U"
  2857     from U_l baU dcU have norm: "isnpoly b" "isnpoly d" "isnpoly (C (-2, 1))" 
  2858       by auto (simp add: isnpoly_def)
  2859     have norm2: "isnpoly (C (-2, 1) *\<^sub>p b*\<^sub>p d)" "isnpoly (C (-2, 1) *\<^sub>p d*\<^sub>p b)"
  2860       using norm by (simp_all add: polymul_norm)
  2861     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)))"
  2862       by (simp_all add: polyneg_norm norm2)
  2863     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")
  2864     proof
  2865       assume H: ?lhs
  2866       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" 
  2867         by (auto simp add: msubst2_def lt[OF stupid(3)] lt[OF stupid(1)] mult_less_0_iff zero_less_mult_iff)
  2868       from msubst2[OF lq norm2(1) z(1), of x bs] 
  2869         msubst2[OF lq norm2(2) z(2), of x bs] H 
  2870       show ?rhs by (simp add: field_simps)
  2871     next
  2872       assume H: ?rhs
  2873       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" 
  2874         by (auto simp add: msubst2_def lt[OF stupid(4)] lt[OF stupid(2)] mult_less_0_iff zero_less_mult_iff)
  2875       from msubst2[OF lq norm2(1) z(1), of x bs] 
  2876         msubst2[OF lq norm2(2) z(2), of x bs] H 
  2877       show ?lhs by (simp add: field_simps)
  2878     qed}
  2879   hence th0: "\<forall>x \<in> set ?U. \<forall>y \<in> set ?U. ?I (?s (x, y)) \<longleftrightarrow> ?I (?s (y, x))"
  2880     by clarsimp
  2881 
  2882   have "?lhs \<longleftrightarrow> (\<exists>x. Ifm vs (x#bs) ?q)" by simp
  2883   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))))"
  2884     using fr_eq_msubst2[OF lq, of vs bs x] by simp
  2885   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)))"
  2886     by (simp add: split_def)
  2887   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)))"
  2888     using alluopairs_ex[OF th0] by simp 
  2889   also have "\<dots> \<longleftrightarrow> ?I ?R" 
  2890     by (simp add: list_disj_def evaldjf_ex split_def)
  2891   also have "\<dots> \<longleftrightarrow> ?rhs"
  2892     unfolding ferrack2_def
  2893     apply (cases "?mp = T") 
  2894     apply (simp add: list_disj_def)
  2895     apply (cases "?pp = T") 
  2896     apply (simp add: list_disj_def)
  2897     by (simp_all add: Let_def decr0[OF nb])
  2898   finally show ?thesis using decr0_qf[OF nb]  
  2899     by (simp  add: ferrack2_def Let_def)
  2900 qed
  2901 
  2902 lemma frpar2: "qfree (frpar2 p) \<and> (Ifm vs bs (frpar2 p) \<longleftrightarrow> Ifm vs bs p)"
  2903 proof-
  2904   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
  2905   from qelim[OF th, of "prep p" bs] 
  2906 show ?thesis  unfolding frpar2_def by (auto simp add: prep)
  2907 qed
  2908 
  2909 ML {* 
  2910 structure ReflectedFRPar = 
  2911 struct
  2912 
  2913 val bT = HOLogic.boolT;
  2914 fun num rT x = HOLogic.mk_number rT x;
  2915 fun rrelT rT = [rT,rT] ---> rT;
  2916 fun rrT rT = [rT, rT] ---> bT;
  2917 fun divt rT = Const(@{const_name Rings.divide},rrelT rT);
  2918 fun timest rT = Const(@{const_name Groups.times},rrelT rT);
  2919 fun plust rT = Const(@{const_name Groups.plus},rrelT rT);
  2920 fun minust rT = Const(@{const_name Groups.minus},rrelT rT);
  2921 fun uminust rT = Const(@{const_name Groups.uminus}, rT --> rT);
  2922 fun powt rT = Const(@{const_name "power"}, [rT,@{typ "nat"}] ---> rT);
  2923 val brT = [bT, bT] ---> bT;
  2924 val nott = @{term "Not"};
  2925 val conjt = @{term HOL.conj};
  2926 val disjt = @{term HOL.disj};
  2927 val impt = @{term HOL.implies};
  2928 val ifft = @{term "op = :: bool => _"}
  2929 fun llt rT = Const(@{const_name Orderings.less},rrT rT);
  2930 fun lle rT = Const(@{const_name Orderings.less},rrT rT);
  2931 fun eqt rT = Const(@{const_name HOL.eq},rrT rT);
  2932 fun rz rT = Const(@{const_name Groups.zero},rT);
  2933 
  2934 fun dest_nat t = case t of
  2935   Const (@{const_name Suc}, _) $ t' => 1 + dest_nat t'
  2936 | _ => (snd o HOLogic.dest_number) t;
  2937 
  2938 fun num_of_term m t = 
  2939  case t of
  2940    Const(@{const_name Groups.uminus},_)$t => @{code poly.Neg} (num_of_term m t)
  2941  | Const(@{const_name Groups.plus},_)$a$b => @{code poly.Add} (num_of_term m a, num_of_term m b)
  2942  | Const(@{const_name Groups.minus},_)$a$b => @{code poly.Sub} (num_of_term m a, num_of_term m b)
  2943  | Const(@{const_name Groups.times},_)$a$b => @{code poly.Mul} (num_of_term m a, num_of_term m b)
  2944  | Const(@{const_name Power.power},_)$a$n => @{code poly.Pw} (num_of_term m a, dest_nat n)
  2945  | Const(@{const_name Rings.divide},_)$a$b => @{code poly.C} (HOLogic.dest_number a |> snd, HOLogic.dest_number b |> snd)
  2946  | _ => (@{code poly.C} (HOLogic.dest_number t |> snd,1) 
  2947          handle TERM _ => @{code poly.Bound} (AList.lookup (op aconv) m t |> the));
  2948 
  2949 fun tm_of_term m m' t = 
  2950  case t of
  2951    Const(@{const_name Groups.uminus},_)$t => @{code Neg} (tm_of_term m m' t)
  2952  | Const(@{const_name Groups.plus},_)$a$b => @{code Add} (tm_of_term m m' a, tm_of_term m m' b)
  2953  | Const(@{const_name Groups.minus},_)$a$b => @{code Sub} (tm_of_term m m' a, tm_of_term m m' b)
  2954  | Const(@{const_name Groups.times},_)$a$b => @{code Mul} (num_of_term m' a, tm_of_term m m' b)
  2955  | _ => (@{code CP} (num_of_term m' t) 
  2956          handle TERM _ => @{code Bound} (AList.lookup (op aconv) m t |> the)
  2957               | Option => @{code Bound} (AList.lookup (op aconv) m t |> the));
  2958 
  2959 fun term_of_num T m t = 
  2960  case t of
  2961   @{code poly.C} (a,b) => (if b = 1 then num T a else if b=0 then (rz T) 
  2962                                         else (divt T) $ num T a $ num T b)
  2963 | @{code poly.Bound} i => AList.lookup (op = : int*int -> bool) m i |> the
  2964 | @{code poly.Add} (a,b) => (plust T)$(term_of_num T m a)$(term_of_num T m b)
  2965 | @{code poly.Mul} (a,b) => (timest T)$(term_of_num T m a)$(term_of_num T m b)
  2966 | @{code poly.Sub} (a,b) => (minust T)$(term_of_num T m a)$(term_of_num T m b)
  2967 | @{code poly.Neg} a => (uminust T)$(term_of_num T m a)
  2968 | @{code poly.Pw} (a,n) => (powt T)$(term_of_num T m t)$(HOLogic.mk_number HOLogic.natT n)
  2969 | @{code poly.CN} (c,n,p) => term_of_num T m (@{code poly.Add} (c, @{code poly.Mul} (@{code poly.Bound} n, p)))
  2970 | _ => error "term_of_num: Unknown term";
  2971 
  2972 fun term_of_tm T m m' t = 
  2973  case t of
  2974   @{code CP} p => term_of_num T m' p
  2975 | @{code Bound} i => AList.lookup (op = : int*int -> bool) m i |> the
  2976 | @{code Add} (a,b) => (plust T)$(term_of_tm T m m' a)$(term_of_tm T m m' b)
  2977 | @{code Mul} (a,b) => (timest T)$(term_of_num T m' a)$(term_of_tm T m m' b)
  2978 | @{code Sub} (a,b) => (minust T)$(term_of_tm T m m' a)$(term_of_tm T m m' b)
  2979 | @{code Neg} a => (uminust T)$(term_of_tm T m m' a)
  2980 | @{code CNP} (n,c,p) => term_of_tm T m m' (@{code Add}
  2981      (@{code Mul} (c, @{code Bound} n), p))
  2982 | _ => error "term_of_tm: Unknown term";
  2983 
  2984 fun fm_of_term m m' fm = 
  2985  case fm of
  2986     Const(@{const_name True},_) => @{code T}
  2987   | Const(@{const_name False},_) => @{code F}
  2988   | Const(@{const_name Not},_)$p => @{code NOT} (fm_of_term m m' p)
  2989   | Const(@{const_name HOL.conj},_)$p$q => @{code And} (fm_of_term m m' p, fm_of_term m m' q)
  2990   | Const(@{const_name HOL.disj},_)$p$q => @{code Or} (fm_of_term m m' p, fm_of_term m m' q)
  2991   | Const(@{const_name HOL.implies},_)$p$q => @{code Imp} (fm_of_term m m' p, fm_of_term m m' q)
  2992   | Const(@{const_name HOL.eq},ty)$p$q => 
  2993        if domain_type ty = bT then @{code Iff} (fm_of_term m m' p, fm_of_term m m' q)
  2994        else @{code Eq} (@{code Sub} (tm_of_term m m' p, tm_of_term m m' q))
  2995   | Const(@{const_name Orderings.less},_)$p$q => 
  2996         @{code Lt} (@{code Sub} (tm_of_term m m' p, tm_of_term m m' q))
  2997   | Const(@{const_name Orderings.less_eq},_)$p$q => 
  2998         @{code Le} (@{code Sub} (tm_of_term m m' p, tm_of_term m m' q))
  2999   | Const(@{const_name Ex},_)$Abs(xn,xT,p) => 
  3000      let val (xn', p') =  variant_abs (xn,xT,p)
  3001          val x = Free(xn',xT)
  3002          fun incr i = i + 1
  3003          val m0 = (x,0):: (map (apsnd incr) m)
  3004       in @{code E} (fm_of_term m0 m' p') end
  3005   | Const(@{const_name All},_)$Abs(xn,xT,p) => 
  3006      let val (xn', p') =  variant_abs (xn,xT,p)
  3007          val x = Free(xn',xT)
  3008          fun incr i = i + 1
  3009          val m0 = (x,0):: (map (apsnd incr) m)
  3010       in @{code A} (fm_of_term m0 m' p') end
  3011   | _ => error "fm_of_term";
  3012 
  3013 
  3014 fun term_of_fm T m m' t = 
  3015   case t of
  3016     @{code T} => Const(@{const_name True},bT)
  3017   | @{code F} => Const(@{const_name False},bT)
  3018   | @{code NOT} p => nott $ (term_of_fm T m m' p)
  3019   | @{code And} (p,q) => conjt $ (term_of_fm T m m' p) $ (term_of_fm T m m' q)
  3020   | @{code Or} (p,q) => disjt $ (term_of_fm T m m' p) $ (term_of_fm T m m' q)
  3021   | @{code Imp} (p,q) => impt $ (term_of_fm T m m' p) $ (term_of_fm T m m' q)
  3022   | @{code Iff} (p,q) => ifft $ (term_of_fm T m m' p) $ (term_of_fm T m m' q)
  3023   | @{code Lt} p => (llt T) $ (term_of_tm T m m' p) $ (rz T)
  3024   | @{code Le} p => (lle T) $ (term_of_tm T m m' p) $ (rz T)
  3025   | @{code Eq} p => (eqt T) $ (term_of_tm T m m' p) $ (rz T)
  3026   | @{code NEq} p => nott $ ((eqt T) $ (term_of_tm T m m' p) $ (rz T))
  3027   | _ => error "term_of_fm: quantifiers!!!!???";
  3028 
  3029 fun frpar_oracle (T,m, m', fm) = 
  3030  let 
  3031    val t = HOLogic.dest_Trueprop fm
  3032    val im = 0 upto (length m - 1)
  3033    val im' = 0 upto (length m' - 1)   
  3034  in HOLogic.mk_Trueprop (HOLogic.mk_eq(t, term_of_fm T (im ~~ m) (im' ~~ m')  
  3035                                                      (@{code frpar} (fm_of_term (m ~~ im) (m' ~~ im') t))))
  3036  end;
  3037 
  3038 fun frpar_oracle2 (T,m, m', fm) = 
  3039  let 
  3040    val t = HOLogic.dest_Trueprop fm
  3041    val im = 0 upto (length m - 1)
  3042    val im' = 0 upto (length m' - 1)   
  3043  in HOLogic.mk_Trueprop (HOLogic.mk_eq(t, term_of_fm T (im ~~ m) (im' ~~ m')  
  3044                                                      (@{code frpar2} (fm_of_term (m ~~ im) (m' ~~ im') t))))
  3045  end;
  3046 
  3047 end;
  3048 
  3049 
  3050 *}
  3051 
  3052 oracle frpar_oracle = {* fn (ty, ts, ts', ct) => 
  3053  let 
  3054   val thy = Thm.theory_of_cterm ct
  3055  in cterm_of thy (ReflectedFRPar.frpar_oracle (ty,ts, ts', term_of ct))
  3056  end *}
  3057 
  3058 oracle frpar_oracle2 = {* fn (ty, ts, ts', ct) => 
  3059  let 
  3060   val thy = Thm.theory_of_cterm ct
  3061  in cterm_of thy (ReflectedFRPar.frpar_oracle2 (ty,ts, ts', term_of ct))
  3062  end *}
  3063 
  3064 ML{* 
  3065 structure FRParTac = 
  3066 struct
  3067 
  3068 fun frpar_tac T ps ctxt i = 
  3069  Object_Logic.full_atomize_tac i
  3070  THEN (fn st =>
  3071   let
  3072     val g = List.nth (cprems_of st, i - 1)
  3073     val thy = ProofContext.theory_of ctxt
  3074     val fs = subtract (op aconv) (map Free (Term.add_frees (term_of g) [])) ps
  3075     val th = frpar_oracle (T, fs,ps, (* Pattern.eta_long [] *)g)
  3076   in rtac (th RS iffD2) i st end);
  3077 
  3078 fun frpar2_tac T ps ctxt i = 
  3079  Object_Logic.full_atomize_tac i
  3080  THEN (fn st =>
  3081   let
  3082     val g = List.nth (cprems_of st, i - 1)
  3083     val thy = ProofContext.theory_of ctxt
  3084     val fs = subtract (op aconv) (map Free (Term.add_frees (term_of g) [])) ps
  3085     val th = frpar_oracle2 (T, fs,ps, (* Pattern.eta_long [] *)g)
  3086   in rtac (th RS iffD2) i st end);
  3087 
  3088 end;
  3089 
  3090 *}
  3091 
  3092 method_setup frpar = {*
  3093 let
  3094  fun keyword k = Scan.lift (Args.$$$ k -- Args.colon) >> K ()
  3095  fun simple_keyword k = Scan.lift (Args.$$$ k) >> K ()
  3096  val parsN = "pars"
  3097  val typN = "type"
  3098  val any_keyword = keyword parsN || keyword typN
  3099  val thms = Scan.repeat (Scan.unless any_keyword Attrib.multi_thm) >> flat
  3100  val cterms = thms >> map Drule.dest_term;
  3101  val terms = Scan.repeat (Scan.unless any_keyword Args.term)
  3102  val typ = Scan.unless any_keyword Args.typ
  3103 in
  3104  (keyword typN |-- typ) -- (keyword parsN |-- terms) >>
  3105   (fn (T,ps) => fn ctxt => SIMPLE_METHOD' (FRParTac.frpar_tac T ps ctxt))
  3106 end
  3107 *} "Parametric QE for linear Arithmetic over fields, Version 1"
  3108 
  3109 method_setup frpar2 = {*
  3110 let
  3111  fun keyword k = Scan.lift (Args.$$$ k -- Args.colon) >> K ()
  3112  fun simple_keyword k = Scan.lift (Args.$$$ k) >> K ()
  3113  val parsN = "pars"
  3114  val typN = "type"
  3115  val any_keyword = keyword parsN || keyword typN
  3116  val thms = Scan.repeat (Scan.unless any_keyword Attrib.multi_thm) >> flat
  3117  val cterms = thms >> map Drule.dest_term;
  3118  val terms = Scan.repeat (Scan.unless any_keyword Args.term)
  3119  val typ = Scan.unless any_keyword Args.typ
  3120 in
  3121  (keyword typN |-- typ) -- (keyword parsN |-- terms) >>
  3122   (fn (T,ps) => fn ctxt => SIMPLE_METHOD' (FRParTac.frpar2_tac T ps ctxt))
  3123 end
  3124 *} "Parametric QE for linear Arithmetic over fields, Version 2"
  3125 
  3126 
  3127 lemma "\<exists>(x::'a::{linordered_field_inverse_zero, number_ring}). y \<noteq> -1 \<longrightarrow> (y + 1)*x < 0"
  3128   apply (frpar type: "'a::{linordered_field_inverse_zero, number_ring}" pars: "y::'a::{linordered_field_inverse_zero, number_ring}")
  3129   apply (simp add: field_simps)
  3130   apply (rule spec[where x=y])
  3131   apply (frpar type: "'a::{linordered_field_inverse_zero, number_ring}" pars: "z::'a::{linordered_field_inverse_zero, number_ring}")
  3132   by simp
  3133 
  3134 text{* Collins/Jones Problem *}
  3135 (*
  3136 lemma "\<exists>(r::'a::{linordered_field_inverse_zero, 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"
  3137 proof-
  3138   have "(\<exists>(r::'a::{linordered_field_inverse_zero, 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::{linordered_field_inverse_zero, 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")
  3139 by (simp add: field_simps)
  3140 have "?rhs"
  3141 
  3142   apply (frpar type: "'a::{linordered_field_inverse_zero, number_ring}" pars: "a::'a::{linordered_field_inverse_zero, number_ring}" "b::'a::{linordered_field_inverse_zero, number_ring}")
  3143   apply (simp add: field_simps)
  3144 oops
  3145 *)
  3146 (*
  3147 lemma "ALL (x::'a::{linordered_field_inverse_zero, number_ring}) y. (1 - t)*x \<le> (1+t)*y \<and> (1 - t)*y \<le> (1+t)*x --> 0 \<le> y"
  3148 apply (frpar type: "'a::{linordered_field_inverse_zero, number_ring}" pars: "t::'a::{linordered_field_inverse_zero, number_ring}")
  3149 oops
  3150 *)
  3151 
  3152 lemma "\<exists>(x::'a::{linordered_field_inverse_zero, number_ring}). y \<noteq> -1 \<longrightarrow> (y + 1)*x < 0"
  3153   apply (frpar2 type: "'a::{linordered_field_inverse_zero, number_ring}" pars: "y::'a::{linordered_field_inverse_zero, number_ring}")
  3154   apply (simp add: field_simps)
  3155   apply (rule spec[where x=y])
  3156   apply (frpar2 type: "'a::{linordered_field_inverse_zero, number_ring}" pars: "z::'a::{linordered_field_inverse_zero, number_ring}")
  3157   by simp
  3158 
  3159 text{* Collins/Jones Problem *}
  3160 
  3161 (*
  3162 lemma "\<exists>(r::'a::{linordered_field_inverse_zero, 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"
  3163 proof-
  3164   have "(\<exists>(r::'a::{linordered_field_inverse_zero, 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::{linordered_field_inverse_zero, 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")
  3165 by (simp add: field_simps)
  3166 have "?rhs"
  3167   apply (frpar2 type: "'a::{linordered_field_inverse_zero, number_ring}" pars: "a::'a::{linordered_field_inverse_zero, number_ring}" "b::'a::{linordered_field_inverse_zero, number_ring}")
  3168   apply simp
  3169 oops
  3170 *)
  3171 
  3172 (*
  3173 lemma "ALL (x::'a::{linordered_field_inverse_zero, number_ring}) y. (1 - t)*x \<le> (1+t)*y \<and> (1 - t)*y \<le> (1+t)*x --> 0 \<le> y"
  3174 apply (frpar2 type: "'a::{linordered_field_inverse_zero, number_ring}" pars: "t::'a::{linordered_field_inverse_zero, number_ring}")
  3175 apply (simp add: field_simps linorder_neq_iff[symmetric])
  3176 apply ferrack
  3177 oops
  3178 *)
  3179 end