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