src/HOL/Decision_Procs/MIR.thy
author wenzelm
Thu Nov 08 15:49:56 2018 +0100 (7 months ago)
changeset 69266 7cc2d66a92a6
parent 69064 5840724b1d71
child 69313 b021008c5397
permissions -rw-r--r--
proper ML expressions, without trailing semicolons;
     1 (*  Title:      HOL/Decision_Procs/MIR.thy
     2     Author:     Amine Chaieb
     3 *)
     4 
     5 theory MIR
     6 imports Complex_Main Dense_Linear_Order DP_Library
     7   "HOL-Library.Code_Target_Numeral" "HOL-Library.Old_Recdef"
     8 begin
     9 
    10 section \<open>Quantifier elimination for \<open>\<real> (0, 1, +, floor, <)\<close>\<close>
    11 
    12 declare of_int_floor_cancel [simp del]
    13 
    14 lemma myle:
    15   fixes a b :: "'a::{ordered_ab_group_add}"
    16   shows "(a \<le> b) = (0 \<le> b - a)"
    17   by (metis add_0_left add_le_cancel_right diff_add_cancel)
    18 
    19 lemma myless:
    20   fixes a b :: "'a::{ordered_ab_group_add}"
    21   shows "(a < b) = (0 < b - a)"
    22   by (metis le_iff_diff_le_0 less_le_not_le myle)
    23 
    24 (* Periodicity of dvd *)
    25 lemmas dvd_period = zdvd_period
    26 
    27 (* The Divisibility relation between reals *)
    28 definition rdvd:: "real \<Rightarrow> real \<Rightarrow> bool" (infixl "rdvd" 50)
    29   where "x rdvd y \<longleftrightarrow> (\<exists>k::int. y = x * real_of_int k)"
    30 
    31 lemma int_rdvd_real:
    32   "real_of_int (i::int) rdvd x = (i dvd \<lfloor>x\<rfloor> \<and> real_of_int \<lfloor>x\<rfloor> = x)" (is "?l = ?r")
    33 proof
    34   assume "?l"
    35   hence th: "\<exists> k. x=real_of_int (i*k)" by (simp add: rdvd_def)
    36   hence th': "real_of_int \<lfloor>x\<rfloor> = x" by (auto simp del: of_int_mult)
    37   with th have "\<exists> k. real_of_int \<lfloor>x\<rfloor> = real_of_int (i*k)" by simp
    38   hence "\<exists>k. \<lfloor>x\<rfloor> = i*k" by presburger
    39   thus ?r using th' by (simp add: dvd_def)
    40 next
    41   assume "?r" hence "(i::int) dvd \<lfloor>x::real\<rfloor>" ..
    42   hence "\<exists>k. real_of_int \<lfloor>x\<rfloor> = real_of_int (i*k)"
    43     by (metis (no_types) dvd_def)
    44   thus ?l using \<open>?r\<close> by (simp add: rdvd_def)
    45 qed
    46 
    47 lemma int_rdvd_iff: "(real_of_int (i::int) rdvd real_of_int t) = (i dvd t)"
    48   by (auto simp add: rdvd_def dvd_def) (rule_tac x="k" in exI, simp only: of_int_mult[symmetric])
    49 
    50 
    51 lemma rdvd_abs1: "(\<bar>real_of_int d\<bar> rdvd t) = (real_of_int (d ::int) rdvd t)"
    52 proof
    53   assume d: "real_of_int d rdvd t"
    54   from d int_rdvd_real have d2: "d dvd \<lfloor>t\<rfloor>" and ti: "real_of_int \<lfloor>t\<rfloor> = t"
    55     by auto
    56 
    57   from iffD2[OF abs_dvd_iff] d2 have "\<bar>d\<bar> dvd \<lfloor>t\<rfloor>" by blast
    58   with ti int_rdvd_real[symmetric] have "real_of_int \<bar>d\<bar> rdvd t" by blast
    59   thus "\<bar>real_of_int d\<bar> rdvd t" by simp
    60 next
    61   assume "\<bar>real_of_int d\<bar> rdvd t" hence "real_of_int \<bar>d\<bar> rdvd t" by simp
    62   with int_rdvd_real[where i="\<bar>d\<bar>" and x="t"]
    63   have d2: "\<bar>d\<bar> dvd \<lfloor>t\<rfloor>" and ti: "real_of_int \<lfloor>t\<rfloor> = t"
    64     by auto
    65   from iffD1[OF abs_dvd_iff] d2 have "d dvd \<lfloor>t\<rfloor>" by blast
    66   with ti int_rdvd_real[symmetric] show "real_of_int d rdvd t" by blast
    67 qed
    68 
    69 lemma rdvd_minus: "(real_of_int (d::int) rdvd t) = (real_of_int d rdvd -t)"
    70   apply (auto simp add: rdvd_def)
    71   apply (rule_tac x="-k" in exI, simp)
    72   apply (rule_tac x="-k" in exI, simp)
    73   done
    74 
    75 lemma rdvd_left_0_eq: "(0 rdvd t) = (t=0)"
    76   by (auto simp add: rdvd_def)
    77 
    78 lemma rdvd_mult:
    79   assumes knz: "k\<noteq>0"
    80   shows "(real_of_int (n::int) * real_of_int (k::int) rdvd x * real_of_int k) = (real_of_int n rdvd x)"
    81   using knz by (simp add: rdvd_def)
    82 
    83   (*********************************************************************************)
    84   (****                            SHADOW SYNTAX AND SEMANTICS                  ****)
    85   (*********************************************************************************)
    86 
    87 datatype (plugins del: size) num = C int | Bound nat | CN nat int num
    88   | Neg num | Add num num | Sub num num
    89   | Mul int num | Floor num | CF int num num
    90 
    91 instantiation num :: size
    92 begin
    93 
    94 primrec size_num :: "num \<Rightarrow> nat"
    95 where
    96   "size_num (C c) = 1"
    97 | "size_num (Bound n) = 1"
    98 | "size_num (Neg a) = 1 + size_num a"
    99 | "size_num (Add a b) = 1 + size_num a + size_num b"
   100 | "size_num (Sub a b) = 3 + size_num a + size_num b"
   101 | "size_num (CN n c a) = 4 + size_num a "
   102 | "size_num (CF c a b) = 4 + size_num a + size_num b"
   103 | "size_num (Mul c a) = 1 + size_num a"
   104 | "size_num (Floor a) = 1 + size_num a"
   105 
   106 instance ..
   107 
   108 end
   109 
   110   (* Semantics of numeral terms (num) *)
   111 primrec Inum :: "real list \<Rightarrow> num \<Rightarrow> real"
   112 where
   113   "Inum bs (C c) = (real_of_int c)"
   114 | "Inum bs (Bound n) = bs!n"
   115 | "Inum bs (CN n c a) = (real_of_int c) * (bs!n) + (Inum bs a)"
   116 | "Inum bs (Neg a) = -(Inum bs a)"
   117 | "Inum bs (Add a b) = Inum bs a + Inum bs b"
   118 | "Inum bs (Sub a b) = Inum bs a - Inum bs b"
   119 | "Inum bs (Mul c a) = (real_of_int c) * Inum bs a"
   120 | "Inum bs (Floor a) = real_of_int \<lfloor>Inum bs a\<rfloor>"
   121 | "Inum bs (CF c a b) = real_of_int c * real_of_int \<lfloor>Inum bs a\<rfloor> + Inum bs b"
   122 definition "isint t bs \<equiv> real_of_int \<lfloor>Inum bs t\<rfloor> = Inum bs t"
   123 
   124 lemma isint_iff: "isint n bs = (real_of_int \<lfloor>Inum bs n\<rfloor> = Inum bs n)"
   125   by (simp add: isint_def)
   126 
   127 lemma isint_Floor: "isint (Floor n) bs"
   128   by (simp add: isint_iff)
   129 
   130 lemma isint_Mul: "isint e bs \<Longrightarrow> isint (Mul c e) bs"
   131 proof-
   132   let ?e = "Inum bs e"
   133   assume be: "isint e bs" hence efe:"real_of_int \<lfloor>?e\<rfloor> = ?e" by (simp add: isint_iff)
   134   have "real_of_int \<lfloor>Inum bs (Mul c e)\<rfloor> = real_of_int \<lfloor>real_of_int (c * \<lfloor>?e\<rfloor>)\<rfloor>"
   135     using efe by simp
   136   also have "\<dots> = real_of_int (c* \<lfloor>?e\<rfloor>)" by (metis floor_of_int)
   137   also have "\<dots> = real_of_int c * ?e" using efe by simp
   138   finally show ?thesis using isint_iff by simp
   139 qed
   140 
   141 lemma isint_neg: "isint e bs \<Longrightarrow> isint (Neg e) bs"
   142 proof-
   143   let ?I = "\<lambda> t. Inum bs t"
   144   assume ie: "isint e bs"
   145   hence th: "real_of_int \<lfloor>?I e\<rfloor> = ?I e" by (simp add: isint_def)
   146   have "real_of_int \<lfloor>?I (Neg e)\<rfloor> = real_of_int \<lfloor>- (real_of_int \<lfloor>?I e\<rfloor>)\<rfloor>"
   147     by (simp add: th)
   148   also have "\<dots> = - real_of_int \<lfloor>?I e\<rfloor>" by simp
   149   finally show "isint (Neg e) bs" by (simp add: isint_def th)
   150 qed
   151 
   152 lemma isint_sub:
   153   assumes ie: "isint e bs" shows "isint (Sub (C c) e) bs"
   154 proof-
   155   let ?I = "\<lambda> t. Inum bs t"
   156   from ie have th: "real_of_int \<lfloor>?I e\<rfloor> = ?I e" by (simp add: isint_def)
   157   have "real_of_int \<lfloor>?I (Sub (C c) e)\<rfloor> = real_of_int \<lfloor>real_of_int (c - \<lfloor>?I e\<rfloor>)\<rfloor>"
   158     by (simp add: th)
   159   also have "\<dots> = real_of_int (c - \<lfloor>?I e\<rfloor>)" by simp
   160   finally show "isint (Sub (C c) e) bs" by (simp add: isint_def th)
   161 qed
   162 
   163 lemma isint_add:
   164   assumes ai: "isint a bs" and bi: "isint b bs"
   165   shows "isint (Add a b) bs"
   166 proof-
   167   let ?a = "Inum bs a"
   168   let ?b = "Inum bs b"
   169   from ai bi isint_iff have "real_of_int \<lfloor>?a + ?b\<rfloor> = real_of_int \<lfloor>real_of_int \<lfloor>?a\<rfloor> + real_of_int \<lfloor>?b\<rfloor>\<rfloor>"
   170     by simp
   171   also have "\<dots> = real_of_int \<lfloor>?a\<rfloor> + real_of_int \<lfloor>?b\<rfloor>" by simp
   172   also have "\<dots> = ?a + ?b" using ai bi isint_iff by simp
   173   finally show "isint (Add a b) bs" by (simp add: isint_iff)
   174 qed
   175 
   176 lemma isint_c: "isint (C j) bs"
   177   by (simp add: isint_iff)
   178 
   179 
   180     (* FORMULAE *)
   181 datatype (plugins del: size) fm =
   182   T | F | Lt num | Le num | Gt num | Ge num | Eq num | NEq num |
   183   Dvd int num | NDvd int num |
   184   NOT fm | And fm fm |  Or fm fm | Imp fm fm | Iff fm fm | E fm | A fm
   185 
   186 instantiation fm :: size
   187 begin
   188 
   189 primrec size_fm :: "fm \<Rightarrow> nat"
   190 where
   191   "size_fm (NOT p) = 1 + size_fm p"
   192 | "size_fm (And p q) = 1 + size_fm p + size_fm q"
   193 | "size_fm (Or p q) = 1 + size_fm p + size_fm q"
   194 | "size_fm (Imp p q) = 3 + size_fm p + size_fm q"
   195 | "size_fm (Iff p q) = 3 + 2 * (size_fm p + size_fm q)"
   196 | "size_fm (E p) = 1 + size_fm p"
   197 | "size_fm (A p) = 4 + size_fm p"
   198 | "size_fm (Dvd i t) = 2"
   199 | "size_fm (NDvd i t) = 2"
   200 | "size_fm T = 1"
   201 | "size_fm F = 1"
   202 | "size_fm (Lt _) = 1"
   203 | "size_fm (Le _) = 1"
   204 | "size_fm (Gt _) = 1"
   205 | "size_fm (Ge _) = 1"
   206 | "size_fm (Eq _) = 1"
   207 | "size_fm (NEq _) = 1"
   208 
   209 instance ..
   210 
   211 end
   212 
   213 lemma size_fm_pos [simp]: "size p > 0" for p :: fm
   214   by (induct p) simp_all
   215 
   216   (* Semantics of formulae (fm) *)
   217 primrec Ifm ::"real list \<Rightarrow> fm \<Rightarrow> bool"
   218 where
   219   "Ifm bs T \<longleftrightarrow> True"
   220 | "Ifm bs F \<longleftrightarrow> False"
   221 | "Ifm bs (Lt a) \<longleftrightarrow> Inum bs a < 0"
   222 | "Ifm bs (Gt a) \<longleftrightarrow> Inum bs a > 0"
   223 | "Ifm bs (Le a) \<longleftrightarrow> Inum bs a \<le> 0"
   224 | "Ifm bs (Ge a) \<longleftrightarrow> Inum bs a \<ge> 0"
   225 | "Ifm bs (Eq a) \<longleftrightarrow> Inum bs a = 0"
   226 | "Ifm bs (NEq a) \<longleftrightarrow> Inum bs a \<noteq> 0"
   227 | "Ifm bs (Dvd i b) \<longleftrightarrow> real_of_int i rdvd Inum bs b"
   228 | "Ifm bs (NDvd i b) \<longleftrightarrow> \<not> (real_of_int i rdvd Inum bs b)"
   229 | "Ifm bs (NOT p) \<longleftrightarrow> \<not> (Ifm bs p)"
   230 | "Ifm bs (And p q) \<longleftrightarrow> Ifm bs p \<and> Ifm bs q"
   231 | "Ifm bs (Or p q) \<longleftrightarrow> Ifm bs p \<or> Ifm bs q"
   232 | "Ifm bs (Imp p q) \<longleftrightarrow> (Ifm bs p \<longrightarrow> Ifm bs q)"
   233 | "Ifm bs (Iff p q) \<longleftrightarrow> (Ifm bs p \<longleftrightarrow> Ifm bs q)"
   234 | "Ifm bs (E p) \<longleftrightarrow> (\<exists>x. Ifm (x # bs) p)"
   235 | "Ifm bs (A p) \<longleftrightarrow> (\<forall>x. Ifm (x # bs) p)"
   236 
   237 fun prep :: "fm \<Rightarrow> fm"
   238 where
   239   "prep (E T) = T"
   240 | "prep (E F) = F"
   241 | "prep (E (Or p q)) = Or (prep (E p)) (prep (E q))"
   242 | "prep (E (Imp p q)) = Or (prep (E (NOT p))) (prep (E q))"
   243 | "prep (E (Iff p q)) = Or (prep (E (And p q))) (prep (E (And (NOT p) (NOT q))))"
   244 | "prep (E (NOT (And p q))) = Or (prep (E (NOT p))) (prep (E(NOT q)))"
   245 | "prep (E (NOT (Imp p q))) = prep (E (And p (NOT q)))"
   246 | "prep (E (NOT (Iff p q))) = Or (prep (E (And p (NOT q)))) (prep (E(And (NOT p) q)))"
   247 | "prep (E p) = E (prep p)"
   248 | "prep (A (And p q)) = And (prep (A p)) (prep (A q))"
   249 | "prep (A p) = prep (NOT (E (NOT p)))"
   250 | "prep (NOT (NOT p)) = prep p"
   251 | "prep (NOT (And p q)) = Or (prep (NOT p)) (prep (NOT q))"
   252 | "prep (NOT (A p)) = prep (E (NOT p))"
   253 | "prep (NOT (Or p q)) = And (prep (NOT p)) (prep (NOT q))"
   254 | "prep (NOT (Imp p q)) = And (prep p) (prep (NOT q))"
   255 | "prep (NOT (Iff p q)) = Or (prep (And p (NOT q))) (prep (And (NOT p) q))"
   256 | "prep (NOT p) = NOT (prep p)"
   257 | "prep (Or p q) = Or (prep p) (prep q)"
   258 | "prep (And p q) = And (prep p) (prep q)"
   259 | "prep (Imp p q) = prep (Or (NOT p) q)"
   260 | "prep (Iff p q) = Or (prep (And p q)) (prep (And (NOT p) (NOT q)))"
   261 | "prep p = p"
   262 
   263 lemma prep: "\<And> bs. Ifm bs (prep p) = Ifm bs p"
   264   by (induct p rule: prep.induct) auto
   265 
   266 
   267   (* Quantifier freeness *)
   268 fun qfree:: "fm \<Rightarrow> bool"
   269 where
   270   "qfree (E p) = False"
   271 | "qfree (A p) = False"
   272 | "qfree (NOT p) = qfree p"
   273 | "qfree (And p q) = (qfree p \<and> qfree q)"
   274 | "qfree (Or  p q) = (qfree p \<and> qfree q)"
   275 | "qfree (Imp p q) = (qfree p \<and> qfree q)"
   276 | "qfree (Iff p q) = (qfree p \<and> qfree q)"
   277 | "qfree p = True"
   278 
   279   (* Boundedness and substitution *)
   280 primrec numbound0 :: "num \<Rightarrow> bool" (* a num is INDEPENDENT of Bound 0 *)
   281 where
   282   "numbound0 (C c) = True"
   283 | "numbound0 (Bound n) = (n>0)"
   284 | "numbound0 (CN n i a) = (n > 0 \<and> numbound0 a)"
   285 | "numbound0 (Neg a) = numbound0 a"
   286 | "numbound0 (Add a b) = (numbound0 a \<and> numbound0 b)"
   287 | "numbound0 (Sub a b) = (numbound0 a \<and> numbound0 b)"
   288 | "numbound0 (Mul i a) = numbound0 a"
   289 | "numbound0 (Floor a) = numbound0 a"
   290 | "numbound0 (CF c a b) = (numbound0 a \<and> numbound0 b)"
   291 
   292 lemma numbound0_I:
   293   assumes nb: "numbound0 a"
   294   shows "Inum (b#bs) a = Inum (b'#bs) a"
   295   using nb by (induct a) auto
   296 
   297 lemma numbound0_gen:
   298   assumes nb: "numbound0 t" and ti: "isint t (x#bs)"
   299   shows "\<forall> y. isint t (y#bs)"
   300   using nb ti
   301 proof(clarify)
   302   fix y
   303   from numbound0_I[OF nb, where bs="bs" and b="y" and b'="x"] ti[simplified isint_def]
   304   show "isint t (y#bs)"
   305     by (simp add: isint_def)
   306 qed
   307 
   308 primrec bound0:: "fm \<Rightarrow> bool" (* A Formula is independent of Bound 0 *)
   309 where
   310   "bound0 T = True"
   311 | "bound0 F = True"
   312 | "bound0 (Lt a) = numbound0 a"
   313 | "bound0 (Le a) = numbound0 a"
   314 | "bound0 (Gt a) = numbound0 a"
   315 | "bound0 (Ge a) = numbound0 a"
   316 | "bound0 (Eq a) = numbound0 a"
   317 | "bound0 (NEq a) = numbound0 a"
   318 | "bound0 (Dvd i a) = numbound0 a"
   319 | "bound0 (NDvd i a) = numbound0 a"
   320 | "bound0 (NOT p) = bound0 p"
   321 | "bound0 (And p q) = (bound0 p \<and> bound0 q)"
   322 | "bound0 (Or p q) = (bound0 p \<and> bound0 q)"
   323 | "bound0 (Imp p q) = ((bound0 p) \<and> (bound0 q))"
   324 | "bound0 (Iff p q) = (bound0 p \<and> bound0 q)"
   325 | "bound0 (E p) = False"
   326 | "bound0 (A p) = False"
   327 
   328 lemma bound0_I:
   329   assumes bp: "bound0 p"
   330   shows "Ifm (b#bs) p = Ifm (b'#bs) p"
   331   using bp numbound0_I [where b="b" and bs="bs" and b'="b'"]
   332   by (induct p) auto
   333 
   334 primrec numsubst0:: "num \<Rightarrow> num \<Rightarrow> num" (* substitute a num into a num for Bound 0 *)
   335 where
   336   "numsubst0 t (C c) = (C c)"
   337 | "numsubst0 t (Bound n) = (if n=0 then t else Bound n)"
   338 | "numsubst0 t (CN n i a) = (if n=0 then Add (Mul i t) (numsubst0 t a) else CN n i (numsubst0 t a))"
   339 | "numsubst0 t (CF i a b) = CF i (numsubst0 t a) (numsubst0 t b)"
   340 | "numsubst0 t (Neg a) = Neg (numsubst0 t a)"
   341 | "numsubst0 t (Add a b) = Add (numsubst0 t a) (numsubst0 t b)"
   342 | "numsubst0 t (Sub a b) = Sub (numsubst0 t a) (numsubst0 t b)"
   343 | "numsubst0 t (Mul i a) = Mul i (numsubst0 t a)"
   344 | "numsubst0 t (Floor a) = Floor (numsubst0 t a)"
   345 
   346 lemma numsubst0_I:
   347   shows "Inum (b#bs) (numsubst0 a t) = Inum ((Inum (b#bs) a)#bs) t"
   348   by (induct t) simp_all
   349 
   350 primrec subst0:: "num \<Rightarrow> fm \<Rightarrow> fm" (* substitue a num into a formula for Bound 0 *)
   351 where
   352   "subst0 t T = T"
   353 | "subst0 t F = F"
   354 | "subst0 t (Lt a) = Lt (numsubst0 t a)"
   355 | "subst0 t (Le a) = Le (numsubst0 t a)"
   356 | "subst0 t (Gt a) = Gt (numsubst0 t a)"
   357 | "subst0 t (Ge a) = Ge (numsubst0 t a)"
   358 | "subst0 t (Eq a) = Eq (numsubst0 t a)"
   359 | "subst0 t (NEq a) = NEq (numsubst0 t a)"
   360 | "subst0 t (Dvd i a) = Dvd i (numsubst0 t a)"
   361 | "subst0 t (NDvd i a) = NDvd i (numsubst0 t a)"
   362 | "subst0 t (NOT p) = NOT (subst0 t p)"
   363 | "subst0 t (And p q) = And (subst0 t p) (subst0 t q)"
   364 | "subst0 t (Or p q) = Or (subst0 t p) (subst0 t q)"
   365 | "subst0 t (Imp p q) = Imp (subst0 t p) (subst0 t q)"
   366 | "subst0 t (Iff p q) = Iff (subst0 t p) (subst0 t q)"
   367 
   368 lemma subst0_I: assumes qfp: "qfree p"
   369   shows "Ifm (b#bs) (subst0 a p) = Ifm ((Inum (b#bs) a)#bs) p"
   370   using qfp numsubst0_I[where b="b" and bs="bs" and a="a"]
   371   by (induct p) simp_all
   372 
   373 fun decrnum:: "num \<Rightarrow> num"
   374 where
   375   "decrnum (Bound n) = Bound (n - 1)"
   376 | "decrnum (Neg a) = Neg (decrnum a)"
   377 | "decrnum (Add a b) = Add (decrnum a) (decrnum b)"
   378 | "decrnum (Sub a b) = Sub (decrnum a) (decrnum b)"
   379 | "decrnum (Mul c a) = Mul c (decrnum a)"
   380 | "decrnum (Floor a) = Floor (decrnum a)"
   381 | "decrnum (CN n c a) = CN (n - 1) c (decrnum a)"
   382 | "decrnum (CF c a b) = CF c (decrnum a) (decrnum b)"
   383 | "decrnum a = a"
   384 
   385 fun decr :: "fm \<Rightarrow> fm"
   386 where
   387   "decr (Lt a) = Lt (decrnum a)"
   388 | "decr (Le a) = Le (decrnum a)"
   389 | "decr (Gt a) = Gt (decrnum a)"
   390 | "decr (Ge a) = Ge (decrnum a)"
   391 | "decr (Eq a) = Eq (decrnum a)"
   392 | "decr (NEq a) = NEq (decrnum a)"
   393 | "decr (Dvd i a) = Dvd i (decrnum a)"
   394 | "decr (NDvd i a) = NDvd i (decrnum a)"
   395 | "decr (NOT p) = NOT (decr p)"
   396 | "decr (And p q) = And (decr p) (decr q)"
   397 | "decr (Or p q) = Or (decr p) (decr q)"
   398 | "decr (Imp p q) = Imp (decr p) (decr q)"
   399 | "decr (Iff p q) = Iff (decr p) (decr q)"
   400 | "decr p = p"
   401 
   402 lemma decrnum: assumes nb: "numbound0 t"
   403   shows "Inum (x#bs) t = Inum bs (decrnum t)"
   404   using nb by (induct t rule: decrnum.induct) simp_all
   405 
   406 lemma decr: assumes nb: "bound0 p"
   407   shows "Ifm (x#bs) p = Ifm bs (decr p)"
   408   using nb by (induct p rule: decr.induct) (simp_all add: decrnum)
   409 
   410 lemma decr_qf: "bound0 p \<Longrightarrow> qfree (decr p)"
   411   by (induct p) simp_all
   412 
   413 fun isatom :: "fm \<Rightarrow> bool" (* test for atomicity *)
   414 where
   415   "isatom T = True"
   416 | "isatom F = True"
   417 | "isatom (Lt a) = True"
   418 | "isatom (Le a) = True"
   419 | "isatom (Gt a) = True"
   420 | "isatom (Ge a) = True"
   421 | "isatom (Eq a) = True"
   422 | "isatom (NEq a) = True"
   423 | "isatom (Dvd i b) = True"
   424 | "isatom (NDvd i b) = True"
   425 | "isatom p = False"
   426 
   427 lemma numsubst0_numbound0:
   428   assumes nb: "numbound0 t"
   429   shows "numbound0 (numsubst0 t a)"
   430   using nb by (induct a) auto
   431 
   432 lemma subst0_bound0:
   433   assumes qf: "qfree p" and nb: "numbound0 t"
   434   shows "bound0 (subst0 t p)"
   435   using qf numsubst0_numbound0[OF nb] by (induct p) auto
   436 
   437 lemma bound0_qf: "bound0 p \<Longrightarrow> qfree p"
   438   by (induct p) simp_all
   439 
   440 
   441 definition djf:: "('a \<Rightarrow> fm) \<Rightarrow> 'a \<Rightarrow> fm \<Rightarrow> fm" where
   442   "djf f p q = (if q=T then T else if q=F then f p else
   443   (let fp = f p in case fp of T \<Rightarrow> T | F \<Rightarrow> q | _ \<Rightarrow> Or fp q))"
   444 
   445 definition evaldjf:: "('a \<Rightarrow> fm) \<Rightarrow> 'a list \<Rightarrow> fm" where
   446   "evaldjf f ps = foldr (djf f) ps F"
   447 
   448 lemma djf_Or: "Ifm bs (djf f p q) = Ifm bs (Or (f p) q)"
   449   by (cases "q=T", simp add: djf_def,cases "q=F",simp add: djf_def)
   450   (cases "f p", simp_all add: Let_def djf_def)
   451 
   452 lemma evaldjf_ex: "Ifm bs (evaldjf f ps) = (\<exists> p \<in> set ps. Ifm bs (f p))"
   453   by (induct ps) (simp_all add: evaldjf_def djf_Or)
   454 
   455 lemma evaldjf_bound0:
   456   assumes nb: "\<forall> x\<in> set xs. bound0 (f x)"
   457   shows "bound0 (evaldjf f xs)"
   458   using nb
   459   apply (induct xs)
   460   apply (auto simp add: evaldjf_def djf_def Let_def)
   461   apply (case_tac "f a")
   462   apply auto
   463   done
   464 
   465 lemma evaldjf_qf:
   466   assumes nb: "\<forall> x\<in> set xs. qfree (f x)"
   467   shows "qfree (evaldjf f xs)"
   468   using nb
   469   apply (induct xs)
   470   apply (auto simp add: evaldjf_def djf_def Let_def)
   471   apply (case_tac "f a")
   472   apply auto
   473   done
   474 
   475 fun disjuncts :: "fm \<Rightarrow> fm list"
   476 where
   477   "disjuncts (Or p q) = (disjuncts p) @ (disjuncts q)"
   478 | "disjuncts F = []"
   479 | "disjuncts p = [p]"
   480 
   481 fun conjuncts :: "fm \<Rightarrow> fm list"
   482 where
   483   "conjuncts (And p q) = (conjuncts p) @ (conjuncts q)"
   484 | "conjuncts T = []"
   485 | "conjuncts p = [p]"
   486 
   487 lemma conjuncts: "(\<forall> q\<in> set (conjuncts p). Ifm bs q) = Ifm bs p"
   488   by (induct p rule: conjuncts.induct) auto
   489 
   490 lemma disjuncts_qf: "qfree p \<Longrightarrow> \<forall> q\<in> set (disjuncts p). qfree q"
   491 proof -
   492   assume qf: "qfree p"
   493   hence "list_all qfree (disjuncts p)"
   494     by (induct p rule: disjuncts.induct, auto)
   495   thus ?thesis by (simp only: list_all_iff)
   496 qed
   497 
   498 lemma conjuncts_qf: "qfree p \<Longrightarrow> \<forall> q\<in> set (conjuncts p). qfree q"
   499 proof-
   500   assume qf: "qfree p"
   501   hence "list_all qfree (conjuncts p)"
   502     by (induct p rule: conjuncts.induct, auto)
   503   thus ?thesis by (simp only: list_all_iff)
   504 qed
   505 
   506 definition DJ :: "(fm \<Rightarrow> fm) \<Rightarrow> fm \<Rightarrow> fm" where
   507   "DJ f p \<equiv> evaldjf f (disjuncts p)"
   508 
   509 lemma DJ: assumes fdj: "\<forall> p q. f (Or p q) = Or (f p) (f q)"
   510   and fF: "f F = F"
   511   shows "Ifm bs (DJ f p) = Ifm bs (f p)"
   512 proof -
   513   have "Ifm bs (DJ f p) = (\<exists> q \<in> set (disjuncts p). Ifm bs (f q))"
   514     by (simp add: DJ_def evaldjf_ex)
   515   also have "\<dots> = Ifm bs (f p)" using fdj fF by (induct p rule: disjuncts.induct, auto)
   516   finally show ?thesis .
   517 qed
   518 
   519 lemma DJ_qf: assumes
   520   fqf: "\<forall> p. qfree p \<longrightarrow> qfree (f p)"
   521   shows "\<forall>p. qfree p \<longrightarrow> qfree (DJ f p) "
   522 proof(clarify)
   523   fix  p assume qf: "qfree p"
   524   have th: "DJ f p = evaldjf f (disjuncts p)" by (simp add: DJ_def)
   525   from disjuncts_qf[OF qf] have "\<forall> q\<in> set (disjuncts p). qfree q" .
   526   with fqf have th':"\<forall> q\<in> set (disjuncts p). qfree (f q)" by blast
   527 
   528   from evaldjf_qf[OF th'] th show "qfree (DJ f p)" by simp
   529 qed
   530 
   531 lemma DJ_qe: assumes qe: "\<forall> bs p. qfree p \<longrightarrow> qfree (qe p) \<and> (Ifm bs (qe p) = Ifm bs (E p))"
   532   shows "\<forall> bs p. qfree p \<longrightarrow> qfree (DJ qe p) \<and> (Ifm bs ((DJ qe p)) = Ifm bs (E p))"
   533 proof(clarify)
   534   fix p::fm and bs
   535   assume qf: "qfree p"
   536   from qe have qth: "\<forall> p. qfree p \<longrightarrow> qfree (qe p)" by blast
   537   from DJ_qf[OF qth] qf have qfth:"qfree (DJ qe p)" by auto
   538   have "Ifm bs (DJ qe p) = (\<exists> q\<in> set (disjuncts p). Ifm bs (qe q))"
   539     by (simp add: DJ_def evaldjf_ex)
   540   also have "\<dots> = (\<exists> q \<in> set(disjuncts p). Ifm bs (E q))" using qe disjuncts_qf[OF qf] by auto
   541   also have "\<dots> = Ifm bs (E p)" by (induct p rule: disjuncts.induct, auto)
   542   finally show "qfree (DJ qe p) \<and> Ifm bs (DJ qe p) = Ifm bs (E p)" using qfth by blast
   543 qed
   544   (* Simplification *)
   545 
   546   (* Algebraic simplifications for nums *)
   547 fun bnds:: "num \<Rightarrow> nat list"
   548 where
   549   "bnds (Bound n) = [n]"
   550 | "bnds (CN n c a) = n#(bnds a)"
   551 | "bnds (Neg a) = bnds a"
   552 | "bnds (Add a b) = (bnds a)@(bnds b)"
   553 | "bnds (Sub a b) = (bnds a)@(bnds b)"
   554 | "bnds (Mul i a) = bnds a"
   555 | "bnds (Floor a) = bnds a"
   556 | "bnds (CF c a b) = (bnds a)@(bnds b)"
   557 | "bnds a = []"
   558 
   559 fun lex_ns:: "nat list \<Rightarrow> nat list \<Rightarrow> bool"
   560 where
   561   "lex_ns [] ms = True"
   562 | "lex_ns ns [] = False"
   563 | "lex_ns (n#ns) (m#ms) = (n<m \<or> ((n = m) \<and> lex_ns ns ms)) "
   564 definition lex_bnd :: "num \<Rightarrow> num \<Rightarrow> bool" where
   565   "lex_bnd t s \<equiv> lex_ns (bnds t) (bnds s)"
   566 
   567 fun maxcoeff:: "num \<Rightarrow> int"
   568 where
   569   "maxcoeff (C i) = \<bar>i\<bar>"
   570 | "maxcoeff (CN n c t) = max \<bar>c\<bar> (maxcoeff t)"
   571 | "maxcoeff (CF c t s) = max \<bar>c\<bar> (maxcoeff s)"
   572 | "maxcoeff t = 1"
   573 
   574 lemma maxcoeff_pos: "maxcoeff t \<ge> 0"
   575   by (induct t rule: maxcoeff.induct) auto
   576 
   577 fun numgcdh:: "num \<Rightarrow> int \<Rightarrow> int"
   578 where
   579   "numgcdh (C i) = (\<lambda>g. gcd i g)"
   580 | "numgcdh (CN n c t) = (\<lambda>g. gcd c (numgcdh t g))"
   581 | "numgcdh (CF c s t) = (\<lambda>g. gcd c (numgcdh t g))"
   582 | "numgcdh t = (\<lambda>g. 1)"
   583 
   584 definition numgcd :: "num \<Rightarrow> int"
   585   where "numgcd t = numgcdh t (maxcoeff t)"
   586 
   587 fun reducecoeffh:: "num \<Rightarrow> int \<Rightarrow> num"
   588 where
   589   "reducecoeffh (C i) = (\<lambda> g. C (i div g))"
   590 | "reducecoeffh (CN n c t) = (\<lambda> g. CN n (c div g) (reducecoeffh t g))"
   591 | "reducecoeffh (CF c s t) = (\<lambda> g. CF (c div g)  s (reducecoeffh t g))"
   592 | "reducecoeffh t = (\<lambda>g. t)"
   593 
   594 definition reducecoeff :: "num \<Rightarrow> num"
   595 where
   596   "reducecoeff t =
   597     (let g = numgcd t in
   598      if g = 0 then C 0 else if g=1 then t else reducecoeffh t g)"
   599 
   600 fun dvdnumcoeff:: "num \<Rightarrow> int \<Rightarrow> bool"
   601 where
   602   "dvdnumcoeff (C i) = (\<lambda> g. g dvd i)"
   603 | "dvdnumcoeff (CN n c t) = (\<lambda> g. g dvd c \<and> (dvdnumcoeff t g))"
   604 | "dvdnumcoeff (CF c s t) = (\<lambda> g. g dvd c \<and> (dvdnumcoeff t g))"
   605 | "dvdnumcoeff t = (\<lambda>g. False)"
   606 
   607 lemma dvdnumcoeff_trans:
   608   assumes gdg: "g dvd g'" and dgt':"dvdnumcoeff t g'"
   609   shows "dvdnumcoeff t g"
   610   using dgt' gdg
   611   by (induct t rule: dvdnumcoeff.induct) (simp_all add: gdg dvd_trans[OF gdg])
   612 
   613 declare dvd_trans [trans add]
   614 
   615 lemma numgcd0:
   616   assumes g0: "numgcd t = 0"
   617   shows "Inum bs t = 0"
   618 proof-
   619   have "\<And>x. numgcdh t x= 0 \<Longrightarrow> Inum bs t = 0"
   620     by (induct t rule: numgcdh.induct, auto)
   621   thus ?thesis using g0[simplified numgcd_def] by blast
   622 qed
   623 
   624 lemma numgcdh_pos: assumes gp: "g \<ge> 0" shows "numgcdh t g \<ge> 0"
   625   using gp by (induct t rule: numgcdh.induct) auto
   626 
   627 lemma numgcd_pos: "numgcd t \<ge>0"
   628   by (simp add: numgcd_def numgcdh_pos maxcoeff_pos)
   629 
   630 lemma reducecoeffh:
   631   assumes gt: "dvdnumcoeff t g" and gp: "g > 0"
   632   shows "real_of_int g *(Inum bs (reducecoeffh t g)) = Inum bs t"
   633   using gt
   634 proof(induct t rule: reducecoeffh.induct)
   635   case (1 i) hence gd: "g dvd i" by simp
   636   from assms 1 show ?case by (simp add: real_of_int_div[OF gd])
   637 next
   638   case (2 n c t)  hence gd: "g dvd c" by simp
   639   from assms 2 show ?case by (simp add: real_of_int_div[OF gd] algebra_simps)
   640 next
   641   case (3 c s t)  hence gd: "g dvd c" by simp
   642   from assms 3 show ?case by (simp add: real_of_int_div[OF gd] algebra_simps)
   643 qed (auto simp add: numgcd_def gp)
   644 
   645 fun ismaxcoeff:: "num \<Rightarrow> int \<Rightarrow> bool"
   646 where
   647   "ismaxcoeff (C i) = (\<lambda> x. \<bar>i\<bar> \<le> x)"
   648 | "ismaxcoeff (CN n c t) = (\<lambda>x. \<bar>c\<bar> \<le> x \<and> (ismaxcoeff t x))"
   649 | "ismaxcoeff (CF c s t) = (\<lambda>x. \<bar>c\<bar> \<le> x \<and> (ismaxcoeff t x))"
   650 | "ismaxcoeff t = (\<lambda>x. True)"
   651 
   652 lemma ismaxcoeff_mono: "ismaxcoeff t c \<Longrightarrow> c \<le> c' \<Longrightarrow> ismaxcoeff t c'"
   653   by (induct t rule: ismaxcoeff.induct) auto
   654 
   655 lemma maxcoeff_ismaxcoeff: "ismaxcoeff t (maxcoeff t)"
   656 proof (induct t rule: maxcoeff.induct)
   657   case (2 n c t)
   658   hence H:"ismaxcoeff t (maxcoeff t)" .
   659   have thh: "maxcoeff t \<le> max \<bar>c\<bar> (maxcoeff t)" by simp
   660   from ismaxcoeff_mono[OF H thh] show ?case by simp
   661 next
   662   case (3 c t s)
   663   hence H1:"ismaxcoeff s (maxcoeff s)" by auto
   664   have thh1: "maxcoeff s \<le> max \<bar>c\<bar> (maxcoeff s)" by (simp add: max_def)
   665   from ismaxcoeff_mono[OF H1 thh1] show ?case by simp
   666 qed simp_all
   667 
   668 lemma zgcd_gt1:
   669   "\<bar>i\<bar> > 1 \<and> \<bar>j\<bar> > 1 \<or> \<bar>i\<bar> = 0 \<and> \<bar>j\<bar> > 1 \<or> \<bar>i\<bar> > 1 \<and> \<bar>j\<bar> = 0"
   670   if "gcd i j > 1" for i j :: int
   671 proof -
   672   have "\<bar>k\<bar> \<le> 1 \<longleftrightarrow> k = - 1 \<or> k = 0 \<or> k = 1" for k :: int
   673     by auto
   674   with that show ?thesis
   675     by (auto simp add: not_less)
   676 qed
   677 
   678 lemma numgcdh0:"numgcdh t m = 0 \<Longrightarrow>  m =0"
   679   by (induct t rule: numgcdh.induct) auto
   680 
   681 lemma dvdnumcoeff_aux:
   682   assumes "ismaxcoeff t m" and mp:"m \<ge> 0" and "numgcdh t m > 1"
   683   shows "dvdnumcoeff t (numgcdh t m)"
   684 using assms
   685 proof(induct t rule: numgcdh.induct)
   686   case (2 n c t)
   687   let ?g = "numgcdh t m"
   688   from 2 have th:"gcd c ?g > 1" by simp
   689   from zgcd_gt1[OF th] numgcdh_pos[OF mp, where t="t"]
   690   have "(\<bar>c\<bar> > 1 \<and> ?g > 1) \<or> (\<bar>c\<bar> = 0 \<and> ?g > 1) \<or> (\<bar>c\<bar> > 1 \<and> ?g = 0)" by simp
   691   moreover {assume "\<bar>c\<bar> > 1" and gp: "?g > 1" with 2
   692     have th: "dvdnumcoeff t ?g" by simp
   693     have th': "gcd c ?g dvd ?g" by simp
   694     from dvdnumcoeff_trans[OF th' th] have ?case by simp }
   695   moreover {assume "\<bar>c\<bar> = 0 \<and> ?g > 1"
   696     with 2 have th: "dvdnumcoeff t ?g" by simp
   697     have th': "gcd c ?g dvd ?g" by simp
   698     from dvdnumcoeff_trans[OF th' th] have ?case by simp
   699     hence ?case by simp }
   700   moreover {assume "\<bar>c\<bar> > 1" and g0:"?g = 0"
   701     from numgcdh0[OF g0] have "m=0". with 2 g0 have ?case by simp }
   702   ultimately show ?case by blast
   703 next
   704   case (3 c s t)
   705   let ?g = "numgcdh t m"
   706   from 3 have th:"gcd c ?g > 1" by simp
   707   from zgcd_gt1[OF th] numgcdh_pos[OF mp, where t="t"]
   708   have "(\<bar>c\<bar> > 1 \<and> ?g > 1) \<or> (\<bar>c\<bar> = 0 \<and> ?g > 1) \<or> (\<bar>c\<bar> > 1 \<and> ?g = 0)" by simp
   709   moreover {assume "\<bar>c\<bar> > 1" and gp: "?g > 1" with 3
   710     have th: "dvdnumcoeff t ?g" by simp
   711     have th': "gcd c ?g dvd ?g" by simp
   712     from dvdnumcoeff_trans[OF th' th] have ?case by simp }
   713   moreover {assume "\<bar>c\<bar> = 0 \<and> ?g > 1"
   714     with 3 have th: "dvdnumcoeff t ?g" by simp
   715     have th': "gcd c ?g dvd ?g" by simp
   716     from dvdnumcoeff_trans[OF th' th] have ?case by simp
   717     hence ?case by simp }
   718   moreover {assume "\<bar>c\<bar> > 1" and g0:"?g = 0"
   719     from numgcdh0[OF g0] have "m=0". with 3 g0 have ?case by simp }
   720   ultimately show ?case by blast
   721 qed auto
   722 
   723 lemma dvdnumcoeff_aux2:
   724   assumes "numgcd t > 1" shows "dvdnumcoeff t (numgcd t) \<and> numgcd t > 0"
   725   using assms
   726 proof (simp add: numgcd_def)
   727   let ?mc = "maxcoeff t"
   728   let ?g = "numgcdh t ?mc"
   729   have th1: "ismaxcoeff t ?mc" by (rule maxcoeff_ismaxcoeff)
   730   have th2: "?mc \<ge> 0" by (rule maxcoeff_pos)
   731   assume H: "numgcdh t ?mc > 1"
   732   from dvdnumcoeff_aux[OF th1 th2 H] show "dvdnumcoeff t ?g" .
   733 qed
   734 
   735 lemma reducecoeff: "real_of_int (numgcd t) * (Inum bs (reducecoeff t)) = Inum bs t"
   736 proof-
   737   let ?g = "numgcd t"
   738   have "?g \<ge> 0"  by (simp add: numgcd_pos)
   739   hence "?g = 0 \<or> ?g = 1 \<or> ?g > 1" by auto
   740   moreover {assume "?g = 0" hence ?thesis by (simp add: numgcd0)}
   741   moreover {assume "?g = 1" hence ?thesis by (simp add: reducecoeff_def)}
   742   moreover { assume g1:"?g > 1"
   743     from dvdnumcoeff_aux2[OF g1] have th1:"dvdnumcoeff t ?g" and g0: "?g > 0" by blast+
   744     from reducecoeffh[OF th1 g0, where bs="bs"] g1 have ?thesis
   745       by (simp add: reducecoeff_def Let_def)}
   746   ultimately show ?thesis by blast
   747 qed
   748 
   749 lemma reducecoeffh_numbound0: "numbound0 t \<Longrightarrow> numbound0 (reducecoeffh t g)"
   750   by (induct t rule: reducecoeffh.induct) auto
   751 
   752 lemma reducecoeff_numbound0: "numbound0 t \<Longrightarrow> numbound0 (reducecoeff t)"
   753   using reducecoeffh_numbound0 by (simp add: reducecoeff_def Let_def)
   754 
   755 consts numadd:: "num \<times> num \<Rightarrow> num"
   756 recdef numadd "measure (\<lambda>(t, s). size t + size s)"
   757   "numadd (CN n1 c1 r1,CN n2 c2 r2) =
   758   (if n1=n2 then
   759   (let c = c1 + c2
   760   in (if c=0 then numadd(r1,r2) else CN n1 c (numadd (r1,r2))))
   761   else if n1 \<le> n2 then CN n1 c1 (numadd (r1,CN n2 c2 r2))
   762   else (CN n2 c2 (numadd (CN n1 c1 r1,r2))))"
   763   "numadd (CN n1 c1 r1,t) = CN n1 c1 (numadd (r1, t))"
   764   "numadd (t,CN n2 c2 r2) = CN n2 c2 (numadd (t,r2))"
   765   "numadd (CF c1 t1 r1,CF c2 t2 r2) =
   766    (if t1 = t2 then
   767     (let c=c1+c2; s= numadd(r1,r2) in (if c=0 then s else CF c t1 s))
   768    else if lex_bnd t1 t2 then CF c1 t1 (numadd(r1,CF c2 t2 r2))
   769    else CF c2 t2 (numadd(CF c1 t1 r1,r2)))"
   770   "numadd (CF c1 t1 r1,C c) = CF c1 t1 (numadd (r1, C c))"
   771   "numadd (C c,CF c1 t1 r1) = CF c1 t1 (numadd (r1, C c))"
   772   "numadd (C b1, C b2) = C (b1+b2)"
   773   "numadd (a,b) = Add a b"
   774 
   775 lemma numadd [simp]: "Inum bs (numadd (t, s)) = Inum bs (Add t s)"
   776   by (induct t s rule: numadd.induct) (simp_all add: Let_def algebra_simps add_eq_0_iff)
   777 
   778 lemma numadd_nb [simp]: "numbound0 t \<Longrightarrow> numbound0 s \<Longrightarrow> numbound0 (numadd (t, s))"
   779   by (induct t s rule: numadd.induct) (simp_all add: Let_def)
   780 
   781 fun nummul:: "num \<Rightarrow> int \<Rightarrow> num"
   782 where
   783   "nummul (C j) = (\<lambda> i. C (i*j))"
   784 | "nummul (CN n c t) = (\<lambda> i. CN n (c*i) (nummul t i))"
   785 | "nummul (CF c t s) = (\<lambda> i. CF (c*i) t (nummul s i))"
   786 | "nummul (Mul c t) = (\<lambda> i. nummul t (i*c))"
   787 | "nummul t = (\<lambda> i. Mul i t)"
   788 
   789 lemma nummul[simp]: "\<And> i. Inum bs (nummul t i) = Inum bs (Mul i t)"
   790   by (induct t rule: nummul.induct) (auto simp add: algebra_simps)
   791 
   792 lemma nummul_nb[simp]: "\<And> i. numbound0 t \<Longrightarrow> numbound0 (nummul t i)"
   793   by (induct t rule: nummul.induct) auto
   794 
   795 definition numneg :: "num \<Rightarrow> num"
   796   where "numneg t \<equiv> nummul t (- 1)"
   797 
   798 definition numsub :: "num \<Rightarrow> num \<Rightarrow> num"
   799   where "numsub s t \<equiv> (if s = t then C 0 else numadd (s,numneg t))"
   800 
   801 lemma numneg[simp]: "Inum bs (numneg t) = Inum bs (Neg t)"
   802   using numneg_def nummul by simp
   803 
   804 lemma numneg_nb[simp]: "numbound0 t \<Longrightarrow> numbound0 (numneg t)"
   805   using numneg_def by simp
   806 
   807 lemma numsub[simp]: "Inum bs (numsub a b) = Inum bs (Sub a b)"
   808   using numsub_def by simp
   809 
   810 lemma numsub_nb[simp]: "\<lbrakk> numbound0 t ; numbound0 s\<rbrakk> \<Longrightarrow> numbound0 (numsub t s)"
   811   using numsub_def by simp
   812 
   813 lemma isint_CF: assumes si: "isint s bs" shows "isint (CF c t s) bs"
   814 proof-
   815   have cti: "isint (Mul c (Floor t)) bs" by (simp add: isint_Mul isint_Floor)
   816 
   817   have "?thesis = isint (Add (Mul c (Floor t)) s) bs" by (simp add: isint_def)
   818   also have "\<dots>" by (simp add: isint_add cti si)
   819   finally show ?thesis .
   820 qed
   821 
   822 fun split_int:: "num \<Rightarrow> num \<times> num"
   823 where
   824   "split_int (C c) = (C 0, C c)"
   825 | "split_int (CN n c b) =
   826      (let (bv,bi) = split_int b
   827        in (CN n c bv, bi))"
   828 | "split_int (CF c a b) =
   829      (let (bv,bi) = split_int b
   830        in (bv, CF c a bi))"
   831 | "split_int a = (a,C 0)"
   832 
   833 lemma split_int: "\<And>tv ti. split_int t = (tv,ti) \<Longrightarrow> (Inum bs (Add tv ti) = Inum bs t) \<and> isint ti bs"
   834 proof (induct t rule: split_int.induct)
   835   case (2 c n b tv ti)
   836   let ?bv = "fst (split_int b)"
   837   let ?bi = "snd (split_int b)"
   838   have "split_int b = (?bv,?bi)" by simp
   839   with 2(1) have b:"Inum bs (Add ?bv ?bi) = Inum bs b" and bii: "isint ?bi bs" by blast+
   840   from 2(2) have tibi: "ti = ?bi" by (simp add: Let_def split_def)
   841   from 2(2) b[symmetric] bii show ?case by (auto simp add: Let_def split_def)
   842 next
   843   case (3 c a b tv ti)
   844   let ?bv = "fst (split_int b)"
   845   let ?bi = "snd (split_int b)"
   846   have "split_int b = (?bv,?bi)" by simp
   847   with 3(1) have b:"Inum bs (Add ?bv ?bi) = Inum bs b" and bii: "isint ?bi bs" by blast+
   848   from 3(2) have tibi: "ti = CF c a ?bi"
   849     by (simp add: Let_def split_def)
   850   from 3(2) b[symmetric] bii show ?case
   851     by (auto simp add: Let_def split_def isint_Floor isint_add isint_Mul isint_CF)
   852 qed (auto simp add: Let_def isint_iff isint_Floor isint_add isint_Mul split_def algebra_simps)
   853 
   854 lemma split_int_nb: "numbound0 t \<Longrightarrow> numbound0 (fst (split_int t)) \<and> numbound0 (snd (split_int t)) "
   855   by (induct t rule: split_int.induct) (auto simp add: Let_def split_def)
   856 
   857 definition numfloor:: "num \<Rightarrow> num"
   858 where
   859   "numfloor t = (let (tv,ti) = split_int t in
   860   (case tv of C i \<Rightarrow> numadd (tv,ti)
   861   | _ \<Rightarrow> numadd(CF 1 tv (C 0),ti)))"
   862 
   863 lemma numfloor[simp]: "Inum bs (numfloor t) = Inum bs (Floor t)" (is "?n t = ?N (Floor t)")
   864 proof-
   865   let ?tv = "fst (split_int t)"
   866   let ?ti = "snd (split_int t)"
   867   have tvti:"split_int t = (?tv,?ti)" by simp
   868   {assume H: "\<forall> v. ?tv \<noteq> C v"
   869     hence th1: "?n t = ?N (Add (Floor ?tv) ?ti)"
   870       by (cases ?tv) (auto simp add: numfloor_def Let_def split_def)
   871     from split_int[OF tvti] have "?N (Floor t) = ?N (Floor(Add ?tv ?ti))" and tii:"isint ?ti bs" by simp+
   872     hence "?N (Floor t) = real_of_int \<lfloor>?N (Add ?tv ?ti)\<rfloor>" by simp
   873     also have "\<dots> = real_of_int (\<lfloor>?N ?tv\<rfloor> + \<lfloor>?N ?ti\<rfloor>)"
   874       by (simp,subst tii[simplified isint_iff, symmetric]) simp
   875     also have "\<dots> = ?N (Add (Floor ?tv) ?ti)" by (simp add: tii[simplified isint_iff])
   876     finally have ?thesis using th1 by simp}
   877   moreover {fix v assume H:"?tv = C v"
   878     from split_int[OF tvti] have "?N (Floor t) = ?N (Floor(Add ?tv ?ti))" and tii:"isint ?ti bs" by simp+
   879     hence "?N (Floor t) = real_of_int \<lfloor>?N (Add ?tv ?ti)\<rfloor>" by simp
   880     also have "\<dots> = real_of_int (\<lfloor>?N ?tv\<rfloor> + \<lfloor>?N ?ti\<rfloor>)"
   881       by (simp,subst tii[simplified isint_iff, symmetric]) simp
   882     also have "\<dots> = ?N (Add (Floor ?tv) ?ti)" by (simp add: tii[simplified isint_iff])
   883     finally have ?thesis by (simp add: H numfloor_def Let_def split_def) }
   884   ultimately show ?thesis by auto
   885 qed
   886 
   887 lemma numfloor_nb[simp]: "numbound0 t \<Longrightarrow> numbound0 (numfloor t)"
   888   using split_int_nb[where t="t"]
   889   by (cases "fst (split_int t)") (auto simp add: numfloor_def Let_def split_def)
   890 
   891 fun simpnum:: "num \<Rightarrow> num"
   892 where
   893   "simpnum (C j) = C j"
   894 | "simpnum (Bound n) = CN n 1 (C 0)"
   895 | "simpnum (Neg t) = numneg (simpnum t)"
   896 | "simpnum (Add t s) = numadd (simpnum t,simpnum s)"
   897 | "simpnum (Sub t s) = numsub (simpnum t) (simpnum s)"
   898 | "simpnum (Mul i t) = (if i = 0 then (C 0) else nummul (simpnum t) i)"
   899 | "simpnum (Floor t) = numfloor (simpnum t)"
   900 | "simpnum (CN n c t) = (if c=0 then simpnum t else CN n c (simpnum t))"
   901 | "simpnum (CF c t s) = simpnum(Add (Mul c (Floor t)) s)"
   902 
   903 lemma simpnum_ci[simp]: "Inum bs (simpnum t) = Inum bs t"
   904   by (induct t rule: simpnum.induct) auto
   905 
   906 lemma simpnum_numbound0[simp]: "numbound0 t \<Longrightarrow> numbound0 (simpnum t)"
   907   by (induct t rule: simpnum.induct) auto
   908 
   909 fun nozerocoeff:: "num \<Rightarrow> bool"
   910 where
   911   "nozerocoeff (C c) = True"
   912 | "nozerocoeff (CN n c t) = (c\<noteq>0 \<and> nozerocoeff t)"
   913 | "nozerocoeff (CF c s t) = (c \<noteq> 0 \<and> nozerocoeff t)"
   914 | "nozerocoeff (Mul c t) = (c\<noteq>0 \<and> nozerocoeff t)"
   915 | "nozerocoeff t = True"
   916 
   917 lemma numadd_nz : "nozerocoeff a \<Longrightarrow> nozerocoeff b \<Longrightarrow> nozerocoeff (numadd (a,b))"
   918   by (induct a b rule: numadd.induct) (auto simp add: Let_def)
   919 
   920 lemma nummul_nz : "\<And> i. i\<noteq>0 \<Longrightarrow> nozerocoeff a \<Longrightarrow> nozerocoeff (nummul a i)"
   921   by (induct a rule: nummul.induct) (auto simp add: Let_def numadd_nz)
   922 
   923 lemma numneg_nz : "nozerocoeff a \<Longrightarrow> nozerocoeff (numneg a)"
   924   by (simp add: numneg_def nummul_nz)
   925 
   926 lemma numsub_nz: "nozerocoeff a \<Longrightarrow> nozerocoeff b \<Longrightarrow> nozerocoeff (numsub a b)"
   927   by (simp add: numsub_def numneg_nz numadd_nz)
   928 
   929 lemma split_int_nz: "nozerocoeff t \<Longrightarrow> nozerocoeff (fst (split_int t)) \<and> nozerocoeff (snd (split_int t))"
   930   by (induct t rule: split_int.induct) (auto simp add: Let_def split_def)
   931 
   932 lemma numfloor_nz: "nozerocoeff t \<Longrightarrow> nozerocoeff (numfloor t)"
   933   by (simp add: numfloor_def Let_def split_def)
   934     (cases "fst (split_int t)", simp_all add: split_int_nz numadd_nz)
   935 
   936 lemma simpnum_nz: "nozerocoeff (simpnum t)"
   937   by (induct t rule: simpnum.induct)
   938     (auto simp add: numadd_nz numneg_nz numsub_nz nummul_nz numfloor_nz)
   939 
   940 lemma maxcoeff_nz: "nozerocoeff t \<Longrightarrow> maxcoeff t = 0 \<Longrightarrow> t = C 0"
   941 proof (induct t rule: maxcoeff.induct)
   942   case (2 n c t)
   943   hence cnz: "c \<noteq>0" and mx: "max \<bar>c\<bar> (maxcoeff t) = 0" by simp+
   944   have "max \<bar>c\<bar> (maxcoeff t) \<ge> \<bar>c\<bar>" by simp
   945   with cnz have "max \<bar>c\<bar> (maxcoeff t) > 0" by arith
   946   with 2 show ?case by simp
   947 next
   948   case (3 c s t)
   949   hence cnz: "c \<noteq>0" and mx: "max \<bar>c\<bar> (maxcoeff t) = 0" by simp+
   950   have "max \<bar>c\<bar> (maxcoeff t) \<ge> \<bar>c\<bar>" by simp
   951   with cnz have "max \<bar>c\<bar> (maxcoeff t) > 0" by arith
   952   with 3 show ?case by simp
   953 qed auto
   954 
   955 lemma numgcd_nz: assumes nz: "nozerocoeff t" and g0: "numgcd t = 0" shows "t = C 0"
   956 proof-
   957   from g0 have th:"numgcdh t (maxcoeff t) = 0" by (simp add: numgcd_def)
   958   from numgcdh0[OF th]  have th:"maxcoeff t = 0" .
   959   from maxcoeff_nz[OF nz th] show ?thesis .
   960 qed
   961 
   962 definition simp_num_pair :: "(num \<times> int) \<Rightarrow> num \<times> int" where
   963   "simp_num_pair \<equiv> (\<lambda> (t,n). (if n = 0 then (C 0, 0) else
   964    (let t' = simpnum t ; g = numgcd t' in
   965       if g > 1 then (let g' = gcd n g in
   966         if g' = 1 then (t',n)
   967         else (reducecoeffh t' g', n div g'))
   968       else (t',n))))"
   969 
   970 lemma simp_num_pair_ci:
   971   shows "((\<lambda> (t,n). Inum bs t / real_of_int n) (simp_num_pair (t,n))) = ((\<lambda> (t,n). Inum bs t / real_of_int n) (t,n))"
   972   (is "?lhs = ?rhs")
   973 proof-
   974   let ?t' = "simpnum t"
   975   let ?g = "numgcd ?t'"
   976   let ?g' = "gcd n ?g"
   977   {assume nz: "n = 0" hence ?thesis by (simp add: Let_def simp_num_pair_def)}
   978   moreover
   979   { assume nnz: "n \<noteq> 0"
   980     {assume "\<not> ?g > 1" hence ?thesis by (simp add: Let_def simp_num_pair_def)}
   981     moreover
   982     {assume g1:"?g>1" hence g0: "?g > 0" by simp
   983       from g1 nnz have gp0: "?g' \<noteq> 0" by simp
   984       hence g'p: "?g' > 0" using gcd_ge_0_int[where x="n" and y="numgcd ?t'"] by arith
   985       hence "?g'= 1 \<or> ?g' > 1" by arith
   986       moreover {assume "?g'=1" hence ?thesis by (simp add: Let_def simp_num_pair_def)}
   987       moreover {assume g'1:"?g'>1"
   988         from dvdnumcoeff_aux2[OF g1] have th1:"dvdnumcoeff ?t' ?g" ..
   989         let ?tt = "reducecoeffh ?t' ?g'"
   990         let ?t = "Inum bs ?tt"
   991         have gpdg: "?g' dvd ?g" by simp
   992         have gpdd: "?g' dvd n" by simp
   993         have gpdgp: "?g' dvd ?g'" by simp
   994         from reducecoeffh[OF dvdnumcoeff_trans[OF gpdg th1] g'p]
   995         have th2:"real_of_int ?g' * ?t = Inum bs ?t'" by simp
   996         from nnz g1 g'1 have "?lhs = ?t / real_of_int (n div ?g')" by (simp add: simp_num_pair_def Let_def)
   997         also have "\<dots> = (real_of_int ?g' * ?t) / (real_of_int ?g' * (real_of_int (n div ?g')))" by simp
   998         also have "\<dots> = (Inum bs ?t' / real_of_int n)"
   999           using real_of_int_div[OF gpdd] th2 gp0 by simp
  1000         finally have "?lhs = Inum bs t / real_of_int n" by simp
  1001         then have ?thesis using nnz g1 g'1 by (simp add: simp_num_pair_def) }
  1002       ultimately have ?thesis by auto }
  1003     ultimately have ?thesis by blast }
  1004   ultimately show ?thesis by blast
  1005 qed
  1006 
  1007 lemma simp_num_pair_l:
  1008   assumes tnb: "numbound0 t" and np: "n >0" and tn: "simp_num_pair (t,n) = (t',n')"
  1009   shows "numbound0 t' \<and> n' >0"
  1010 proof-
  1011   let ?t' = "simpnum t"
  1012   let ?g = "numgcd ?t'"
  1013   let ?g' = "gcd n ?g"
  1014   { assume nz: "n = 0" hence ?thesis using assms by (simp add: Let_def simp_num_pair_def) }
  1015   moreover
  1016   { assume nnz: "n \<noteq> 0"
  1017     {assume "\<not> ?g > 1" hence ?thesis using assms by (auto simp add: Let_def simp_num_pair_def) }
  1018     moreover
  1019     {assume g1:"?g>1" hence g0: "?g > 0" by simp
  1020       from g1 nnz have gp0: "?g' \<noteq> 0" by simp
  1021       hence g'p: "?g' > 0" using gcd_ge_0_int[where x="n" and y="numgcd ?t'"] by arith
  1022       hence "?g'= 1 \<or> ?g' > 1" by arith
  1023       moreover {assume "?g'=1" hence ?thesis using assms g1 g0
  1024           by (auto simp add: Let_def simp_num_pair_def) }
  1025       moreover {assume g'1:"?g'>1"
  1026         have gpdg: "?g' dvd ?g" by simp
  1027         have gpdd: "?g' dvd n" by simp
  1028         have gpdgp: "?g' dvd ?g'" by simp
  1029         from zdvd_imp_le[OF gpdd np] have g'n: "?g' \<le> n" .
  1030         from zdiv_mono1[OF g'n g'p, simplified div_self[OF gp0]]
  1031         have "n div ?g' >0" by simp
  1032         hence ?thesis using assms g1 g'1
  1033           by(auto simp add: simp_num_pair_def Let_def reducecoeffh_numbound0)}
  1034       ultimately have ?thesis by auto }
  1035     ultimately have ?thesis by blast }
  1036   ultimately show ?thesis by blast
  1037 qed
  1038 
  1039 fun not:: "fm \<Rightarrow> fm"
  1040 where
  1041   "not (NOT p) = p"
  1042 | "not T = F"
  1043 | "not F = T"
  1044 | "not (Lt t) = Ge t"
  1045 | "not (Le t) = Gt t"
  1046 | "not (Gt t) = Le t"
  1047 | "not (Ge t) = Lt t"
  1048 | "not (Eq t) = NEq t"
  1049 | "not (NEq t) = Eq t"
  1050 | "not (Dvd i t) = NDvd i t"
  1051 | "not (NDvd i t) = Dvd i t"
  1052 | "not (And p q) = Or (not p) (not q)"
  1053 | "not (Or p q) = And (not p) (not q)"
  1054 | "not p = NOT p"
  1055 lemma not[simp]: "Ifm bs (not p) = Ifm bs (NOT p)"
  1056   by (induct p) auto
  1057 lemma not_qf[simp]: "qfree p \<Longrightarrow> qfree (not p)"
  1058   by (induct p) auto
  1059 lemma not_nb[simp]: "bound0 p \<Longrightarrow> bound0 (not p)"
  1060   by (induct p) auto
  1061 
  1062 definition conj :: "fm \<Rightarrow> fm \<Rightarrow> fm" where
  1063   "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
  1064    if p = q then p else And p q)"
  1065 lemma conj[simp]: "Ifm bs (conj p q) = Ifm bs (And p q)"
  1066   by (cases "p=F \<or> q=F", simp_all add: conj_def) (cases p, simp_all)
  1067 
  1068 lemma conj_qf[simp]: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (conj p q)"
  1069   using conj_def by auto
  1070 lemma conj_nb[simp]: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (conj p q)"
  1071   using conj_def by auto
  1072 
  1073 definition disj :: "fm \<Rightarrow> fm \<Rightarrow> fm" where
  1074   "disj p q \<equiv> (if (p = T \<or> q=T) then T else if p=F then q else if q=F then p
  1075        else if p=q then p else Or p q)"
  1076 
  1077 lemma disj[simp]: "Ifm bs (disj p q) = Ifm bs (Or p q)"
  1078   by (cases "p=T \<or> q=T",simp_all add: disj_def) (cases p,simp_all)
  1079 lemma disj_qf[simp]: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (disj p q)"
  1080   using disj_def by auto
  1081 lemma disj_nb[simp]: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (disj p q)"
  1082   using disj_def by auto
  1083 
  1084 definition imp :: "fm \<Rightarrow> fm \<Rightarrow> fm" where
  1085   "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
  1086     else Imp p q)"
  1087 lemma imp[simp]: "Ifm bs (imp p q) = Ifm bs (Imp p q)"
  1088   by (cases "p=F \<or> q=T",simp_all add: imp_def)
  1089 lemma imp_qf[simp]: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (imp p q)"
  1090   using imp_def by (cases "p=F \<or> q=T",simp_all add: imp_def)
  1091 
  1092 definition iff :: "fm \<Rightarrow> fm \<Rightarrow> fm" where
  1093   "iff p q \<equiv> (if (p = q) then T else if (p = not q \<or> not p = q) then F else
  1094        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
  1095   Iff p q)"
  1096 
  1097 lemma iff[simp]: "Ifm bs (iff p q) = Ifm bs (Iff p q)"
  1098   by (unfold iff_def,cases "p=q", simp,cases "p=not q", simp)  (cases "not p= q", auto)
  1099 
  1100 lemma iff_qf[simp]: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (iff p q)"
  1101   by (unfold iff_def,cases "p=q", auto)
  1102 
  1103 fun check_int:: "num \<Rightarrow> bool"
  1104 where
  1105   "check_int (C i) = True"
  1106 | "check_int (Floor t) = True"
  1107 | "check_int (Mul i t) = check_int t"
  1108 | "check_int (Add t s) = (check_int t \<and> check_int s)"
  1109 | "check_int (Neg t) = check_int t"
  1110 | "check_int (CF c t s) = check_int s"
  1111 | "check_int t = False"
  1112 lemma check_int: "check_int t \<Longrightarrow> isint t bs"
  1113   by (induct t) (auto simp add: isint_add isint_Floor isint_Mul isint_neg isint_c isint_CF)
  1114 
  1115 lemma rdvd_left1_int: "real_of_int \<lfloor>t\<rfloor> = t \<Longrightarrow> 1 rdvd t"
  1116   by (simp add: rdvd_def,rule_tac x="\<lfloor>t\<rfloor>" in exI) simp
  1117 
  1118 lemma rdvd_reduce:
  1119   assumes gd:"g dvd d" and gc:"g dvd c" and gp: "g > 0"
  1120   shows "real_of_int (d::int) rdvd real_of_int (c::int)*t = (real_of_int (d div g) rdvd real_of_int (c div g)*t)"
  1121 proof
  1122   assume d: "real_of_int d rdvd real_of_int c * t"
  1123   from d rdvd_def obtain k where k_def: "real_of_int c * t = real_of_int d* real_of_int (k::int)" by auto
  1124   from gd dvd_def obtain kd where kd_def: "d = g * kd" by auto
  1125   from gc dvd_def obtain kc where kc_def: "c = g * kc" by auto
  1126   from k_def kd_def kc_def have "real_of_int g * real_of_int kc * t = real_of_int g * real_of_int kd * real_of_int k" by simp
  1127   hence "real_of_int kc * t = real_of_int kd * real_of_int k" using gp by simp
  1128   hence th:"real_of_int kd rdvd real_of_int kc * t" using rdvd_def by blast
  1129   from kd_def gp have th':"kd = d div g" by simp
  1130   from kc_def gp have "kc = c div g" by simp
  1131   with th th' show "real_of_int (d div g) rdvd real_of_int (c div g) * t" by simp
  1132 next
  1133   assume d: "real_of_int (d div g) rdvd real_of_int (c div g) * t"
  1134   from gp have gnz: "g \<noteq> 0" by simp
  1135   thus "real_of_int d rdvd real_of_int c * t" using d rdvd_mult[OF gnz, where n="d div g" and x="real_of_int (c div g) * t"] real_of_int_div[OF gd] real_of_int_div[OF gc] by simp
  1136 qed
  1137 
  1138 definition simpdvd :: "int \<Rightarrow> num \<Rightarrow> (int \<times> num)" where
  1139   "simpdvd d t \<equiv>
  1140    (let g = numgcd t in
  1141       if g > 1 then (let g' = gcd d g in
  1142         if g' = 1 then (d, t)
  1143         else (d div g',reducecoeffh t g'))
  1144       else (d, t))"
  1145 lemma simpdvd:
  1146   assumes tnz: "nozerocoeff t" and dnz: "d \<noteq> 0"
  1147   shows "Ifm bs (Dvd (fst (simpdvd d t)) (snd (simpdvd d t))) = Ifm bs (Dvd d t)"
  1148 proof-
  1149   let ?g = "numgcd t"
  1150   let ?g' = "gcd d ?g"
  1151   {assume "\<not> ?g > 1" hence ?thesis by (simp add: Let_def simpdvd_def)}
  1152   moreover
  1153   {assume g1:"?g>1" hence g0: "?g > 0" by simp
  1154     from g1 dnz have gp0: "?g' \<noteq> 0" by simp
  1155     hence g'p: "?g' > 0" using gcd_ge_0_int[where x="d" and y="numgcd t"] by arith
  1156     hence "?g'= 1 \<or> ?g' > 1" by arith
  1157     moreover {assume "?g'=1" hence ?thesis by (simp add: Let_def simpdvd_def)}
  1158     moreover {assume g'1:"?g'>1"
  1159       from dvdnumcoeff_aux2[OF g1] have th1:"dvdnumcoeff t ?g" ..
  1160       let ?tt = "reducecoeffh t ?g'"
  1161       let ?t = "Inum bs ?tt"
  1162       have gpdg: "?g' dvd ?g" by simp
  1163       have gpdd: "?g' dvd d" by simp
  1164       have gpdgp: "?g' dvd ?g'" by simp
  1165       from reducecoeffh[OF dvdnumcoeff_trans[OF gpdg th1] g'p]
  1166       have th2:"real_of_int ?g' * ?t = Inum bs t" by simp
  1167       from assms g1 g0 g'1
  1168       have "Ifm bs (Dvd (fst (simpdvd d t)) (snd(simpdvd d t))) = Ifm bs (Dvd (d div ?g') ?tt)"
  1169         by (simp add: simpdvd_def Let_def)
  1170       also have "\<dots> = (real_of_int d rdvd (Inum bs t))"
  1171         using rdvd_reduce[OF gpdd gpdgp g'p, where t="?t", simplified div_self[OF gp0]]
  1172           th2[symmetric] by simp
  1173       finally have ?thesis by simp  }
  1174     ultimately have ?thesis by auto
  1175   }
  1176   ultimately show ?thesis by blast
  1177 qed
  1178 
  1179 fun simpfm :: "fm \<Rightarrow> fm"
  1180 where
  1181   "simpfm (And p q) = conj (simpfm p) (simpfm q)"
  1182 | "simpfm (Or p q) = disj (simpfm p) (simpfm q)"
  1183 | "simpfm (Imp p q) = imp (simpfm p) (simpfm q)"
  1184 | "simpfm (Iff p q) = iff (simpfm p) (simpfm q)"
  1185 | "simpfm (NOT p) = not (simpfm p)"
  1186 | "simpfm (Lt a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v < 0) then T else F
  1187     | _ \<Rightarrow> Lt (reducecoeff a'))"
  1188 | "simpfm (Le a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v \<le> 0)  then T else F | _ \<Rightarrow> Le (reducecoeff a'))"
  1189 | "simpfm (Gt a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v > 0)  then T else F | _ \<Rightarrow> Gt (reducecoeff a'))"
  1190 | "simpfm (Ge a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v \<ge> 0)  then T else F | _ \<Rightarrow> Ge (reducecoeff a'))"
  1191 | "simpfm (Eq a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v = 0)  then T else F | _ \<Rightarrow> Eq (reducecoeff a'))"
  1192 | "simpfm (NEq a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v \<noteq> 0)  then T else F | _ \<Rightarrow> NEq (reducecoeff a'))"
  1193 | "simpfm (Dvd i a) = (if i=0 then simpfm (Eq a)
  1194              else if (\<bar>i\<bar> = 1) \<and> check_int a then T
  1195              else let a' = simpnum a in case a' of C v \<Rightarrow> if (i dvd v)  then T else F | _ \<Rightarrow> (let (d,t) = simpdvd i a' in Dvd d t))"
  1196 | "simpfm (NDvd i a) = (if i=0 then simpfm (NEq a)
  1197              else if (\<bar>i\<bar> = 1) \<and> check_int a then F
  1198              else let a' = simpnum a in case a' of C v \<Rightarrow> if (\<not>(i dvd v)) then T else F | _ \<Rightarrow> (let (d,t) = simpdvd i a' in NDvd d t))"
  1199 | "simpfm p = p"
  1200 
  1201 lemma simpfm[simp]: "Ifm bs (simpfm p) = Ifm bs p"
  1202 proof(induct p rule: simpfm.induct)
  1203   case (6 a) let ?sa = "simpnum a" have sa: "Inum bs ?sa = Inum bs a" by simp
  1204   {fix v assume "?sa = C v" hence ?case using sa by simp }
  1205   moreover {assume H:"\<not> (\<exists> v. ?sa = C v)"
  1206     let ?g = "numgcd ?sa"
  1207     let ?rsa = "reducecoeff ?sa"
  1208     let ?r = "Inum bs ?rsa"
  1209     have sa_nz: "nozerocoeff ?sa" by (rule simpnum_nz)
  1210     {assume gz: "?g=0" from numgcd_nz[OF sa_nz gz] H have "False" by auto}
  1211     with numgcd_pos[where t="?sa"] have "?g > 0" by (cases "?g=0", auto)
  1212     hence gp: "real_of_int ?g > 0" by simp
  1213     have "Inum bs ?sa = real_of_int ?g* ?r" by (simp add: reducecoeff)
  1214     with sa have "Inum bs a < 0 = (real_of_int ?g * ?r < real_of_int ?g * 0)" by simp
  1215     also have "\<dots> = (?r < 0)" using gp
  1216       by (simp only: mult_less_cancel_left) simp
  1217     finally have ?case using H by (cases "?sa" , simp_all add: Let_def)}
  1218   ultimately show ?case by blast
  1219 next
  1220   case (7 a)  let ?sa = "simpnum a" have sa: "Inum bs ?sa = Inum bs a" by simp
  1221   {fix v assume "?sa = C v" hence ?case using sa by simp }
  1222   moreover {assume H:"\<not> (\<exists> v. ?sa = C v)"
  1223     let ?g = "numgcd ?sa"
  1224     let ?rsa = "reducecoeff ?sa"
  1225     let ?r = "Inum bs ?rsa"
  1226     have sa_nz: "nozerocoeff ?sa" by (rule simpnum_nz)
  1227     {assume gz: "?g=0" from numgcd_nz[OF sa_nz gz] H have "False" by auto}
  1228     with numgcd_pos[where t="?sa"] have "?g > 0" by (cases "?g=0", auto)
  1229     hence gp: "real_of_int ?g > 0" by simp
  1230     have "Inum bs ?sa = real_of_int ?g* ?r" by (simp add: reducecoeff)
  1231     with sa have "Inum bs a \<le> 0 = (real_of_int ?g * ?r \<le> real_of_int ?g * 0)" by simp
  1232     also have "\<dots> = (?r \<le> 0)" using gp
  1233       by (simp only: mult_le_cancel_left) simp
  1234     finally have ?case using H by (cases "?sa" , simp_all add: Let_def)}
  1235   ultimately show ?case by blast
  1236 next
  1237   case (8 a)  let ?sa = "simpnum a" have sa: "Inum bs ?sa = Inum bs a" by simp
  1238   {fix v assume "?sa = C v" hence ?case using sa by simp }
  1239   moreover {assume H:"\<not> (\<exists> v. ?sa = C v)"
  1240     let ?g = "numgcd ?sa"
  1241     let ?rsa = "reducecoeff ?sa"
  1242     let ?r = "Inum bs ?rsa"
  1243     have sa_nz: "nozerocoeff ?sa" by (rule simpnum_nz)
  1244     {assume gz: "?g=0" from numgcd_nz[OF sa_nz gz] H have "False" by auto}
  1245     with numgcd_pos[where t="?sa"] have "?g > 0" by (cases "?g=0", auto)
  1246     hence gp: "real_of_int ?g > 0" by simp
  1247     have "Inum bs ?sa = real_of_int ?g* ?r" by (simp add: reducecoeff)
  1248     with sa have "Inum bs a > 0 = (real_of_int ?g * ?r > real_of_int ?g * 0)" by simp
  1249     also have "\<dots> = (?r > 0)" using gp
  1250       by (simp only: mult_less_cancel_left) simp
  1251     finally have ?case using H by (cases "?sa" , simp_all add: Let_def)}
  1252   ultimately show ?case by blast
  1253 next
  1254   case (9 a)  let ?sa = "simpnum a" have sa: "Inum bs ?sa = Inum bs a" by simp
  1255   {fix v assume "?sa = C v" hence ?case using sa by simp }
  1256   moreover {assume H:"\<not> (\<exists> v. ?sa = C v)"
  1257     let ?g = "numgcd ?sa"
  1258     let ?rsa = "reducecoeff ?sa"
  1259     let ?r = "Inum bs ?rsa"
  1260     have sa_nz: "nozerocoeff ?sa" by (rule simpnum_nz)
  1261     {assume gz: "?g=0" from numgcd_nz[OF sa_nz gz] H have "False" by auto}
  1262     with numgcd_pos[where t="?sa"] have "?g > 0" by (cases "?g=0", auto)
  1263     hence gp: "real_of_int ?g > 0" by simp
  1264     have "Inum bs ?sa = real_of_int ?g* ?r" by (simp add: reducecoeff)
  1265     with sa have "Inum bs a \<ge> 0 = (real_of_int ?g * ?r \<ge> real_of_int ?g * 0)" by simp
  1266     also have "\<dots> = (?r \<ge> 0)" using gp
  1267       by (simp only: mult_le_cancel_left) simp
  1268     finally have ?case using H by (cases "?sa" , simp_all add: Let_def)}
  1269   ultimately show ?case by blast
  1270 next
  1271   case (10 a)  let ?sa = "simpnum a" have sa: "Inum bs ?sa = Inum bs a" by simp
  1272   {fix v assume "?sa = C v" hence ?case using sa by simp }
  1273   moreover {assume H:"\<not> (\<exists> v. ?sa = C v)"
  1274     let ?g = "numgcd ?sa"
  1275     let ?rsa = "reducecoeff ?sa"
  1276     let ?r = "Inum bs ?rsa"
  1277     have sa_nz: "nozerocoeff ?sa" by (rule simpnum_nz)
  1278     {assume gz: "?g=0" from numgcd_nz[OF sa_nz gz] H have "False" by auto}
  1279     with numgcd_pos[where t="?sa"] have "?g > 0" by (cases "?g=0", auto)
  1280     hence gp: "real_of_int ?g > 0" by simp
  1281     have "Inum bs ?sa = real_of_int ?g* ?r" by (simp add: reducecoeff)
  1282     with sa have "Inum bs a = 0 = (real_of_int ?g * ?r = 0)" by simp
  1283     also have "\<dots> = (?r = 0)" using gp
  1284       by simp
  1285     finally have ?case using H by (cases "?sa" , simp_all add: Let_def)}
  1286   ultimately show ?case by blast
  1287 next
  1288   case (11 a)  let ?sa = "simpnum a" have sa: "Inum bs ?sa = Inum bs a" by simp
  1289   {fix v assume "?sa = C v" hence ?case using sa by simp }
  1290   moreover {assume H:"\<not> (\<exists> v. ?sa = C v)"
  1291     let ?g = "numgcd ?sa"
  1292     let ?rsa = "reducecoeff ?sa"
  1293     let ?r = "Inum bs ?rsa"
  1294     have sa_nz: "nozerocoeff ?sa" by (rule simpnum_nz)
  1295     {assume gz: "?g=0" from numgcd_nz[OF sa_nz gz] H have "False" by auto}
  1296     with numgcd_pos[where t="?sa"] have "?g > 0" by (cases "?g=0", auto)
  1297     hence gp: "real_of_int ?g > 0" by simp
  1298     have "Inum bs ?sa = real_of_int ?g* ?r" by (simp add: reducecoeff)
  1299     with sa have "Inum bs a \<noteq> 0 = (real_of_int ?g * ?r \<noteq> 0)" by simp
  1300     also have "\<dots> = (?r \<noteq> 0)" using gp
  1301       by simp
  1302     finally have ?case using H by (cases "?sa") (simp_all add: Let_def) }
  1303   ultimately show ?case by blast
  1304 next
  1305   case (12 i a)  let ?sa = "simpnum a"   have sa: "Inum bs ?sa = Inum bs a" by simp
  1306   have "i=0 \<or> (\<bar>i\<bar> = 1 \<and> check_int a) \<or> (i\<noteq>0 \<and> ((\<bar>i\<bar> \<noteq> 1) \<or> (\<not> check_int a)))" by auto
  1307   {assume "i=0" hence ?case using "12.hyps" by (simp add: rdvd_left_0_eq Let_def)}
  1308   moreover
  1309   {assume ai1: "\<bar>i\<bar> = 1" and ai: "check_int a"
  1310     hence "i=1 \<or> i= - 1" by arith
  1311     moreover {assume i1: "i = 1"
  1312       from rdvd_left1_int[OF check_int[OF ai, simplified isint_iff]]
  1313       have ?case using i1 ai by simp }
  1314     moreover {assume i1: "i = - 1"
  1315       from rdvd_left1_int[OF check_int[OF ai, simplified isint_iff]]
  1316         rdvd_abs1[where d="- 1" and t="Inum bs a"]
  1317       have ?case using i1 ai by simp }
  1318     ultimately have ?case by blast}
  1319   moreover
  1320   {assume inz: "i\<noteq>0" and cond: "(\<bar>i\<bar> \<noteq> 1) \<or> (\<not> check_int a)"
  1321     {fix v assume "?sa = C v" hence ?case using sa[symmetric] inz cond
  1322         by (cases "\<bar>i\<bar> = 1", auto simp add: int_rdvd_iff) }
  1323     moreover {assume H:"\<not> (\<exists> v. ?sa = C v)"
  1324       hence th: "simpfm (Dvd i a) = Dvd (fst (simpdvd i ?sa)) (snd (simpdvd i ?sa))" using inz cond by (cases ?sa, auto simp add: Let_def split_def)
  1325       from simpnum_nz have nz:"nozerocoeff ?sa" by simp
  1326       from simpdvd [OF nz inz] th have ?case using sa by simp}
  1327     ultimately have ?case by blast}
  1328   ultimately show ?case by blast
  1329 next
  1330   case (13 i a)  let ?sa = "simpnum a"   have sa: "Inum bs ?sa = Inum bs a" by simp
  1331   have "i=0 \<or> (\<bar>i\<bar> = 1 \<and> check_int a) \<or> (i\<noteq>0 \<and> ((\<bar>i\<bar> \<noteq> 1) \<or> (\<not> check_int a)))" by auto
  1332   {assume "i=0" hence ?case using "13.hyps" by (simp add: rdvd_left_0_eq Let_def)}
  1333   moreover
  1334   {assume ai1: "\<bar>i\<bar> = 1" and ai: "check_int a"
  1335     hence "i=1 \<or> i= - 1" by arith
  1336     moreover {assume i1: "i = 1"
  1337       from rdvd_left1_int[OF check_int[OF ai, simplified isint_iff]]
  1338       have ?case using i1 ai by simp }
  1339     moreover {assume i1: "i = - 1"
  1340       from rdvd_left1_int[OF check_int[OF ai, simplified isint_iff]]
  1341         rdvd_abs1[where d="- 1" and t="Inum bs a"]
  1342       have ?case using i1 ai by simp }
  1343     ultimately have ?case by blast}
  1344   moreover
  1345   {assume inz: "i\<noteq>0" and cond: "(\<bar>i\<bar> \<noteq> 1) \<or> (\<not> check_int a)"
  1346     {fix v assume "?sa = C v" hence ?case using sa[symmetric] inz cond
  1347         by (cases "\<bar>i\<bar> = 1", auto simp add: int_rdvd_iff) }
  1348     moreover {assume H:"\<not> (\<exists> v. ?sa = C v)"
  1349       hence th: "simpfm (NDvd i a) = NDvd (fst (simpdvd i ?sa)) (snd (simpdvd i ?sa))" using inz cond
  1350         by (cases ?sa, auto simp add: Let_def split_def)
  1351       from simpnum_nz have nz:"nozerocoeff ?sa" by simp
  1352       from simpdvd [OF nz inz] th have ?case using sa by simp}
  1353     ultimately have ?case by blast}
  1354   ultimately show ?case by blast
  1355 qed (induct p rule: simpfm.induct, simp_all)
  1356 
  1357 lemma simpdvd_numbound0: "numbound0 t \<Longrightarrow> numbound0 (snd (simpdvd d t))"
  1358   by (simp add: simpdvd_def Let_def split_def reducecoeffh_numbound0)
  1359 
  1360 lemma simpfm_bound0[simp]: "bound0 p \<Longrightarrow> bound0 (simpfm p)"
  1361 proof(induct p rule: simpfm.induct)
  1362   case (6 a) hence nb: "numbound0 a" by simp
  1363   hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
  1364   thus ?case by (cases "simpnum a", auto simp add: Let_def reducecoeff_numbound0)
  1365 next
  1366   case (7 a) hence nb: "numbound0 a" by simp
  1367   hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
  1368   thus ?case by (cases "simpnum a", auto simp add: Let_def reducecoeff_numbound0)
  1369 next
  1370   case (8 a) hence nb: "numbound0 a" by simp
  1371   hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
  1372   thus ?case by (cases "simpnum a", auto simp add: Let_def reducecoeff_numbound0)
  1373 next
  1374   case (9 a) hence nb: "numbound0 a" by simp
  1375   hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
  1376   thus ?case by (cases "simpnum a", auto simp add: Let_def reducecoeff_numbound0)
  1377 next
  1378   case (10 a) hence nb: "numbound0 a" by simp
  1379   hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
  1380   thus ?case by (cases "simpnum a", auto simp add: Let_def reducecoeff_numbound0)
  1381 next
  1382   case (11 a) hence nb: "numbound0 a" by simp
  1383   hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
  1384   thus ?case by (cases "simpnum a", auto simp add: Let_def reducecoeff_numbound0)
  1385 next
  1386   case (12 i a) hence nb: "numbound0 a" by simp
  1387   hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
  1388   thus ?case by (cases "simpnum a", auto simp add: Let_def reducecoeff_numbound0 simpdvd_numbound0 split_def)
  1389 next
  1390   case (13 i a) hence nb: "numbound0 a" by simp
  1391   hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
  1392   thus ?case by (cases "simpnum a", auto simp add: Let_def reducecoeff_numbound0 simpdvd_numbound0 split_def)
  1393 qed(auto simp add: disj_def imp_def iff_def conj_def)
  1394 
  1395 lemma simpfm_qf[simp]: "qfree p \<Longrightarrow> qfree (simpfm p)"
  1396 by (induct p rule: simpfm.induct, auto simp add: Let_def)
  1397 (case_tac "simpnum a",auto simp add: split_def Let_def)+
  1398 
  1399 
  1400   (* Generic quantifier elimination *)
  1401 
  1402 definition list_conj :: "fm list \<Rightarrow> fm" where
  1403   "list_conj ps \<equiv> foldr conj ps T"
  1404 lemma list_conj: "Ifm bs (list_conj ps) = (\<forall>p\<in> set ps. Ifm bs p)"
  1405   by (induct ps, auto simp add: list_conj_def)
  1406 lemma list_conj_qf: " \<forall>p\<in> set ps. qfree p \<Longrightarrow> qfree (list_conj ps)"
  1407   by (induct ps, auto simp add: list_conj_def)
  1408 lemma list_conj_nb: " \<forall>p\<in> set ps. bound0 p \<Longrightarrow> bound0 (list_conj ps)"
  1409   by (induct ps, auto simp add: list_conj_def)
  1410 definition CJNB :: "(fm \<Rightarrow> fm) \<Rightarrow> fm \<Rightarrow> fm" where
  1411   "CJNB f p \<equiv> (let cjs = conjuncts p ; (yes,no) = List.partition bound0 cjs
  1412                    in conj (decr (list_conj yes)) (f (list_conj no)))"
  1413 
  1414 lemma CJNB_qe:
  1415   assumes qe: "\<forall> bs p. qfree p \<longrightarrow> qfree (qe p) \<and> (Ifm bs (qe p) = Ifm bs (E p))"
  1416   shows "\<forall> bs p. qfree p \<longrightarrow> qfree (CJNB qe p) \<and> (Ifm bs ((CJNB qe p)) = Ifm bs (E p))"
  1417 proof(clarify)
  1418   fix bs p
  1419   assume qfp: "qfree p"
  1420   let ?cjs = "conjuncts p"
  1421   let ?yes = "fst (List.partition bound0 ?cjs)"
  1422   let ?no = "snd (List.partition bound0 ?cjs)"
  1423   let ?cno = "list_conj ?no"
  1424   let ?cyes = "list_conj ?yes"
  1425   have part: "List.partition bound0 ?cjs = (?yes,?no)" by simp
  1426   from partition_P[OF part] have "\<forall> q\<in> set ?yes. bound0 q" by blast
  1427   hence yes_nb: "bound0 ?cyes" by (simp add: list_conj_nb)
  1428   hence yes_qf: "qfree (decr ?cyes )" by (simp add: decr_qf)
  1429   from conjuncts_qf[OF qfp] partition_set[OF part]
  1430   have " \<forall>q\<in> set ?no. qfree q" by auto
  1431   hence no_qf: "qfree ?cno"by (simp add: list_conj_qf)
  1432   with qe have cno_qf:"qfree (qe ?cno )"
  1433     and noE: "Ifm bs (qe ?cno) = Ifm bs (E ?cno)" by blast+
  1434   from cno_qf yes_qf have qf: "qfree (CJNB qe p)"
  1435     by (simp add: CJNB_def Let_def split_def)
  1436   {fix bs
  1437     from conjuncts have "Ifm bs p = (\<forall>q\<in> set ?cjs. Ifm bs q)" by blast
  1438     also have "\<dots> = ((\<forall>q\<in> set ?yes. Ifm bs q) \<and> (\<forall>q\<in> set ?no. Ifm bs q))"
  1439       using partition_set[OF part] by auto
  1440     finally have "Ifm bs p = ((Ifm bs ?cyes) \<and> (Ifm bs ?cno))" using list_conj by simp}
  1441   hence "Ifm bs (E p) = (\<exists>x. (Ifm (x#bs) ?cyes) \<and> (Ifm (x#bs) ?cno))" by simp
  1442   also fix y have "\<dots> = (\<exists>x. (Ifm (y#bs) ?cyes) \<and> (Ifm (x#bs) ?cno))"
  1443     using bound0_I[OF yes_nb, where bs="bs" and b'="y"] by blast
  1444   also have "\<dots> = (Ifm bs (decr ?cyes) \<and> Ifm bs (E ?cno))"
  1445     by (auto simp add: decr[OF yes_nb] simp del: partition_filter_conv)
  1446   also have "\<dots> = (Ifm bs (conj (decr ?cyes) (qe ?cno)))"
  1447     using qe[rule_format, OF no_qf] by auto
  1448   finally have "Ifm bs (E p) = Ifm bs (CJNB qe p)"
  1449     by (simp add: Let_def CJNB_def split_def)
  1450   with qf show "qfree (CJNB qe p) \<and> Ifm bs (CJNB qe p) = Ifm bs (E p)" by blast
  1451 qed
  1452 
  1453 fun qelim :: "fm \<Rightarrow> (fm \<Rightarrow> fm) \<Rightarrow> fm"
  1454 where
  1455   "qelim (E p) = (\<lambda> qe. DJ (CJNB qe) (qelim p qe))"
  1456 | "qelim (A p) = (\<lambda> qe. not (qe ((qelim (NOT p) qe))))"
  1457 | "qelim (NOT p) = (\<lambda> qe. not (qelim p qe))"
  1458 | "qelim (And p q) = (\<lambda> qe. conj (qelim p qe) (qelim q qe))"
  1459 | "qelim (Or  p q) = (\<lambda> qe. disj (qelim p qe) (qelim q qe))"
  1460 | "qelim (Imp p q) = (\<lambda> qe. disj (qelim (NOT p) qe) (qelim q qe))"
  1461 | "qelim (Iff p q) = (\<lambda> qe. iff (qelim p qe) (qelim q qe))"
  1462 | "qelim p = (\<lambda> y. simpfm p)"
  1463 
  1464 lemma qelim_ci:
  1465   assumes qe_inv: "\<forall> bs p. qfree p \<longrightarrow> qfree (qe p) \<and> (Ifm bs (qe p) = Ifm bs (E p))"
  1466   shows "\<And> bs. qfree (qelim p qe) \<and> (Ifm bs (qelim p qe) = Ifm bs p)"
  1467   using qe_inv DJ_qe[OF CJNB_qe[OF qe_inv]]
  1468   by (induct p rule: qelim.induct) (auto simp del: simpfm.simps)
  1469 
  1470 
  1471 text \<open>The \<open>\<int>\<close> Part\<close>
  1472 text\<open>Linearity for fm where Bound 0 ranges over \<open>\<int>\<close>\<close>
  1473 
  1474 fun zsplit0 :: "num \<Rightarrow> int \<times> num" (* splits the bounded from the unbounded part*)
  1475 where
  1476   "zsplit0 (C c) = (0,C c)"
  1477 | "zsplit0 (Bound n) = (if n=0 then (1, C 0) else (0,Bound n))"
  1478 | "zsplit0 (CN n c a) = zsplit0 (Add (Mul c (Bound n)) a)"
  1479 | "zsplit0 (CF c a b) = zsplit0 (Add (Mul c (Floor a)) b)"
  1480 | "zsplit0 (Neg a) = (let (i',a') =  zsplit0 a in (-i', Neg a'))"
  1481 | "zsplit0 (Add a b) = (let (ia,a') =  zsplit0 a ;
  1482                             (ib,b') =  zsplit0 b
  1483                             in (ia+ib, Add a' b'))"
  1484 | "zsplit0 (Sub a b) = (let (ia,a') =  zsplit0 a ;
  1485                             (ib,b') =  zsplit0 b
  1486                             in (ia-ib, Sub a' b'))"
  1487 | "zsplit0 (Mul i a) = (let (i',a') =  zsplit0 a in (i*i', Mul i a'))"
  1488 | "zsplit0 (Floor a) = (let (i',a') =  zsplit0 a in (i',Floor a'))"
  1489 
  1490 lemma zsplit0_I:
  1491   shows "\<And> n a. zsplit0 t = (n,a) \<Longrightarrow> (Inum ((real_of_int (x::int)) #bs) (CN 0 n a) = Inum (real_of_int x #bs) t) \<and> numbound0 a"
  1492   (is "\<And> n a. ?S t = (n,a) \<Longrightarrow> (?I x (CN 0 n a) = ?I x t) \<and> ?N a")
  1493 proof(induct t rule: zsplit0.induct)
  1494   case (1 c n a) thus ?case by auto
  1495 next
  1496   case (2 m n a) thus ?case by (cases "m=0") auto
  1497 next
  1498   case (3 n i a n a') thus ?case by auto
  1499 next
  1500   case (4 c a b n a') thus ?case by auto
  1501 next
  1502   case (5 t n a)
  1503   let ?nt = "fst (zsplit0 t)"
  1504   let ?at = "snd (zsplit0 t)"
  1505   have abj: "zsplit0 t = (?nt,?at)" by simp hence th: "a=Neg ?at \<and> n=-?nt" using 5
  1506     by (simp add: Let_def split_def)
  1507   from abj 5 have th2: "(?I x (CN 0 ?nt ?at) = ?I x t) \<and> ?N ?at" by blast
  1508   from th2[simplified] th[simplified] show ?case by simp
  1509 next
  1510   case (6 s t n a)
  1511   let ?ns = "fst (zsplit0 s)"
  1512   let ?as = "snd (zsplit0 s)"
  1513   let ?nt = "fst (zsplit0 t)"
  1514   let ?at = "snd (zsplit0 t)"
  1515   have abjs: "zsplit0 s = (?ns,?as)" by simp
  1516   moreover have abjt:  "zsplit0 t = (?nt,?at)" by simp
  1517   ultimately have th: "a=Add ?as ?at \<and> n=?ns + ?nt" using 6
  1518     by (simp add: Let_def split_def)
  1519   from abjs[symmetric] have bluddy: "\<exists> x y. (x,y) = zsplit0 s" by blast
  1520   from 6 have "(\<exists> x y. (x,y) = zsplit0 s) \<longrightarrow> (\<forall>xa xb. zsplit0 t = (xa, xb) \<longrightarrow> Inum (real_of_int x # bs) (CN 0 xa xb) = Inum (real_of_int x # bs) t \<and> numbound0 xb)" by blast (*FIXME*)
  1521   with bluddy abjt have th3: "(?I x (CN 0 ?nt ?at) = ?I x t) \<and> ?N ?at" by blast
  1522   from abjs 6  have th2: "(?I x (CN 0 ?ns ?as) = ?I x s) \<and> ?N ?as" by blast
  1523   from th3[simplified] th2[simplified] th[simplified] show ?case
  1524     by (simp add: distrib_right)
  1525 next
  1526   case (7 s t n a)
  1527   let ?ns = "fst (zsplit0 s)"
  1528   let ?as = "snd (zsplit0 s)"
  1529   let ?nt = "fst (zsplit0 t)"
  1530   let ?at = "snd (zsplit0 t)"
  1531   have abjs: "zsplit0 s = (?ns,?as)" by simp
  1532   moreover have abjt:  "zsplit0 t = (?nt,?at)" by simp
  1533   ultimately have th: "a=Sub ?as ?at \<and> n=?ns - ?nt" using 7
  1534     by (simp add: Let_def split_def)
  1535   from abjs[symmetric] have bluddy: "\<exists> x y. (x,y) = zsplit0 s" by blast
  1536   from 7 have "(\<exists> x y. (x,y) = zsplit0 s) \<longrightarrow> (\<forall>xa xb. zsplit0 t = (xa, xb) \<longrightarrow> Inum (real_of_int x # bs) (CN 0 xa xb) = Inum (real_of_int x # bs) t \<and> numbound0 xb)" by blast (*FIXME*)
  1537   with bluddy abjt have th3: "(?I x (CN 0 ?nt ?at) = ?I x t) \<and> ?N ?at" by blast
  1538   from abjs 7 have th2: "(?I x (CN 0 ?ns ?as) = ?I x s) \<and> ?N ?as" by blast
  1539   from th3[simplified] th2[simplified] th[simplified] show ?case
  1540     by (simp add: left_diff_distrib)
  1541 next
  1542   case (8 i t n a)
  1543   let ?nt = "fst (zsplit0 t)"
  1544   let ?at = "snd (zsplit0 t)"
  1545   have abj: "zsplit0 t = (?nt,?at)" by simp hence th: "a=Mul i ?at \<and> n=i*?nt" using 8
  1546     by (simp add: Let_def split_def)
  1547   from abj 8 have th2: "(?I x (CN 0 ?nt ?at) = ?I x t) \<and> ?N ?at" by blast
  1548   hence "?I x (Mul i t) = (real_of_int i) * ?I x (CN 0 ?nt ?at)" by simp
  1549   also have "\<dots> = ?I x (CN 0 (i*?nt) (Mul i ?at))" by (simp add: distrib_left)
  1550   finally show ?case using th th2 by simp
  1551 next
  1552   case (9 t n a)
  1553   let ?nt = "fst (zsplit0 t)"
  1554   let ?at = "snd (zsplit0 t)"
  1555   have abj: "zsplit0 t = (?nt,?at)" by simp hence th: "a= Floor ?at \<and> n=?nt" using 9
  1556     by (simp add: Let_def split_def)
  1557   from abj 9 have th2: "(?I x (CN 0 ?nt ?at) = ?I x t) \<and> ?N ?at" by blast
  1558   hence na: "?N a" using th by simp
  1559   have th': "(real_of_int ?nt)*(real_of_int x) = real_of_int (?nt * x)" by simp
  1560   have "?I x (Floor t) = ?I x (Floor (CN 0 ?nt ?at))" using th2 by simp
  1561   also have "\<dots> = real_of_int \<lfloor>real_of_int ?nt * real_of_int x + ?I x ?at\<rfloor>" by simp
  1562   also have "\<dots> = real_of_int \<lfloor>?I x ?at + real_of_int (?nt * x)\<rfloor>" by (simp add: ac_simps)
  1563   also have "\<dots> = real_of_int (\<lfloor>?I x ?at\<rfloor> + (?nt * x))"
  1564     by (simp add: of_int_mult[symmetric] del: of_int_mult)
  1565   also have "\<dots> = real_of_int (?nt)*(real_of_int x) + real_of_int \<lfloor>?I x ?at\<rfloor>" by (simp add: ac_simps)
  1566   finally have "?I x (Floor t) = ?I x (CN 0 n a)" using th by simp
  1567   with na show ?case by simp
  1568 qed
  1569 
  1570 fun iszlfm :: "fm \<Rightarrow> real list \<Rightarrow> bool"   (* Linearity test for fm *)
  1571 where
  1572   "iszlfm (And p q) = (\<lambda> bs. iszlfm p bs \<and> iszlfm q bs)"
  1573 | "iszlfm (Or p q) = (\<lambda> bs. iszlfm p bs \<and> iszlfm q bs)"
  1574 | "iszlfm (Eq  (CN 0 c e)) = (\<lambda> bs. c>0 \<and> numbound0 e \<and> isint e bs)"
  1575 | "iszlfm (NEq (CN 0 c e)) = (\<lambda> bs. c>0 \<and> numbound0 e \<and> isint e bs)"
  1576 | "iszlfm (Lt  (CN 0 c e)) = (\<lambda> bs. c>0 \<and> numbound0 e \<and> isint e bs)"
  1577 | "iszlfm (Le  (CN 0 c e)) = (\<lambda> bs. c>0 \<and> numbound0 e \<and> isint e bs)"
  1578 | "iszlfm (Gt  (CN 0 c e)) = (\<lambda> bs. c>0 \<and> numbound0 e \<and> isint e bs)"
  1579 | "iszlfm (Ge  (CN 0 c e)) = (\<lambda> bs. c>0 \<and> numbound0 e \<and> isint e bs)"
  1580 | "iszlfm (Dvd i (CN 0 c e)) =
  1581                  (\<lambda> bs. c>0 \<and> i>0 \<and> numbound0 e \<and> isint e bs)"
  1582 | "iszlfm (NDvd i (CN 0 c e))=
  1583                  (\<lambda> bs. c>0 \<and> i>0 \<and> numbound0 e \<and> isint e bs)"
  1584 | "iszlfm p = (\<lambda> bs. isatom p \<and> (bound0 p))"
  1585 
  1586 lemma zlin_qfree: "iszlfm p bs \<Longrightarrow> qfree p"
  1587   by (induct p rule: iszlfm.induct) auto
  1588 
  1589 lemma iszlfm_gen:
  1590   assumes lp: "iszlfm p (x#bs)"
  1591   shows "\<forall> y. iszlfm p (y#bs)"
  1592 proof
  1593   fix y
  1594   show "iszlfm p (y#bs)"
  1595     using lp
  1596   by(induct p rule: iszlfm.induct, simp_all add: numbound0_gen[rule_format, where x="x" and y="y"])
  1597 qed
  1598 
  1599 lemma conj_zl[simp]: "iszlfm p bs \<Longrightarrow> iszlfm q bs \<Longrightarrow> iszlfm (conj p q) bs"
  1600   using conj_def by (cases p,auto)
  1601 lemma disj_zl[simp]: "iszlfm p bs \<Longrightarrow> iszlfm q bs \<Longrightarrow> iszlfm (disj p q) bs"
  1602   using disj_def by (cases p,auto)
  1603 
  1604 fun zlfm :: "fm \<Rightarrow> fm"       (* Linearity transformation for fm *)
  1605 where
  1606   "zlfm (And p q) = conj (zlfm p) (zlfm q)"
  1607 | "zlfm (Or p q) = disj (zlfm p) (zlfm q)"
  1608 | "zlfm (Imp p q) = disj (zlfm (NOT p)) (zlfm q)"
  1609 | "zlfm (Iff p q) = disj (conj (zlfm p) (zlfm q)) (conj (zlfm (NOT p)) (zlfm (NOT q)))"
  1610 | "zlfm (Lt a) = (let (c,r) = zsplit0 a in
  1611      if c=0 then Lt r else
  1612      if c>0 then Or (Lt (CN 0 c (Neg (Floor (Neg r))))) (And (Eq (CN 0 c (Neg (Floor (Neg r))))) (Lt (Add (Floor (Neg r)) r)))
  1613      else Or (Gt (CN 0 (-c) (Floor(Neg r)))) (And (Eq(CN 0 (-c) (Floor(Neg r)))) (Lt (Add (Floor (Neg r)) r))))"
  1614 | "zlfm (Le a) = (let (c,r) = zsplit0 a in
  1615      if c=0 then Le r else
  1616      if c>0 then Or (Le (CN 0 c (Neg (Floor (Neg r))))) (And (Eq (CN 0 c (Neg (Floor (Neg r))))) (Lt (Add (Floor (Neg r)) r)))
  1617      else Or (Ge (CN 0 (-c) (Floor(Neg r)))) (And (Eq(CN 0 (-c) (Floor(Neg r)))) (Lt (Add (Floor (Neg r)) r))))"
  1618 | "zlfm (Gt a) = (let (c,r) = zsplit0 a in
  1619      if c=0 then Gt r else
  1620      if c>0 then Or (Gt (CN 0 c (Floor r))) (And (Eq (CN 0 c (Floor r))) (Lt (Sub (Floor r) r)))
  1621      else Or (Lt (CN 0 (-c) (Neg (Floor r)))) (And (Eq(CN 0 (-c) (Neg (Floor r)))) (Lt (Sub (Floor r) r))))"
  1622 | "zlfm (Ge a) = (let (c,r) = zsplit0 a in
  1623      if c=0 then Ge r else
  1624      if c>0 then Or (Ge (CN 0 c (Floor r))) (And (Eq (CN 0 c (Floor r))) (Lt (Sub (Floor r) r)))
  1625      else Or (Le (CN 0 (-c) (Neg (Floor r)))) (And (Eq(CN 0 (-c) (Neg (Floor r)))) (Lt (Sub (Floor r) r))))"
  1626 | "zlfm (Eq a) = (let (c,r) = zsplit0 a in
  1627               if c=0 then Eq r else
  1628       if c>0 then (And (Eq (CN 0 c (Neg (Floor (Neg r))))) (Eq (Add (Floor (Neg r)) r)))
  1629       else (And (Eq (CN 0 (-c) (Floor (Neg r)))) (Eq (Add (Floor (Neg r)) r))))"
  1630 | "zlfm (NEq a) = (let (c,r) = zsplit0 a in
  1631               if c=0 then NEq r else
  1632       if c>0 then (Or (NEq (CN 0 c (Neg (Floor (Neg r))))) (NEq (Add (Floor (Neg r)) r)))
  1633       else (Or (NEq (CN 0 (-c) (Floor (Neg r)))) (NEq (Add (Floor (Neg r)) r))))"
  1634 | "zlfm (Dvd i a) = (if i=0 then zlfm (Eq a)
  1635   else (let (c,r) = zsplit0 a in
  1636               if c=0 then Dvd \<bar>i\<bar> r else
  1637       if c>0 then And (Eq (Sub (Floor r) r)) (Dvd \<bar>i\<bar> (CN 0 c (Floor r)))
  1638       else And (Eq (Sub (Floor r) r)) (Dvd \<bar>i\<bar> (CN 0 (-c) (Neg (Floor r))))))"
  1639 | "zlfm (NDvd i a) = (if i=0 then zlfm (NEq a)
  1640   else (let (c,r) = zsplit0 a in
  1641               if c=0 then NDvd \<bar>i\<bar> r else
  1642       if c>0 then Or (NEq (Sub (Floor r) r)) (NDvd \<bar>i\<bar> (CN 0 c (Floor r)))
  1643       else Or (NEq (Sub (Floor r) r)) (NDvd \<bar>i\<bar> (CN 0 (-c) (Neg (Floor r))))))"
  1644 | "zlfm (NOT (And p q)) = disj (zlfm (NOT p)) (zlfm (NOT q))"
  1645 | "zlfm (NOT (Or p q)) = conj (zlfm (NOT p)) (zlfm (NOT q))"
  1646 | "zlfm (NOT (Imp p q)) = conj (zlfm p) (zlfm (NOT q))"
  1647 | "zlfm (NOT (Iff p q)) = disj (conj(zlfm p) (zlfm(NOT q))) (conj (zlfm(NOT p)) (zlfm q))"
  1648 | "zlfm (NOT (NOT p)) = zlfm p"
  1649 | "zlfm (NOT T) = F"
  1650 | "zlfm (NOT F) = T"
  1651 | "zlfm (NOT (Lt a)) = zlfm (Ge a)"
  1652 | "zlfm (NOT (Le a)) = zlfm (Gt a)"
  1653 | "zlfm (NOT (Gt a)) = zlfm (Le a)"
  1654 | "zlfm (NOT (Ge a)) = zlfm (Lt a)"
  1655 | "zlfm (NOT (Eq a)) = zlfm (NEq a)"
  1656 | "zlfm (NOT (NEq a)) = zlfm (Eq a)"
  1657 | "zlfm (NOT (Dvd i a)) = zlfm (NDvd i a)"
  1658 | "zlfm (NOT (NDvd i a)) = zlfm (Dvd i a)"
  1659 | "zlfm p = p"
  1660 
  1661 lemma split_int_less_real:
  1662   "(real_of_int (a::int) < b) = (a < \<lfloor>b\<rfloor> \<or> (a = \<lfloor>b\<rfloor> \<and> real_of_int \<lfloor>b\<rfloor> < b))"
  1663 proof( auto)
  1664   assume alb: "real_of_int a < b" and agb: "\<not> a < \<lfloor>b\<rfloor>"
  1665   from agb have "\<lfloor>b\<rfloor> \<le> a" by simp
  1666   hence th: "b < real_of_int a + 1" by (simp only: floor_le_iff)
  1667   from floor_eq[OF alb th] show "a = \<lfloor>b\<rfloor>" by simp
  1668 next
  1669   assume alb: "a < \<lfloor>b\<rfloor>"
  1670   hence "real_of_int a < real_of_int \<lfloor>b\<rfloor>" by simp
  1671   moreover have "real_of_int \<lfloor>b\<rfloor> \<le> b" by simp
  1672   ultimately show  "real_of_int a < b" by arith
  1673 qed
  1674 
  1675 lemma split_int_less_real':
  1676   "(real_of_int (a::int) + b < 0) = (real_of_int a - real_of_int \<lfloor>- b\<rfloor> < 0 \<or> (real_of_int a - real_of_int \<lfloor>- b\<rfloor> = 0 \<and> real_of_int \<lfloor>- b\<rfloor> + b < 0))"
  1677 proof-
  1678   have "(real_of_int a + b <0) = (real_of_int a < -b)" by arith
  1679   with split_int_less_real[where a="a" and b="-b"] show ?thesis by arith
  1680 qed
  1681 
  1682 lemma split_int_gt_real':
  1683   "(real_of_int (a::int) + b > 0) = (real_of_int a + real_of_int \<lfloor>b\<rfloor> > 0 \<or> (real_of_int a + real_of_int \<lfloor>b\<rfloor> = 0 \<and> real_of_int \<lfloor>b\<rfloor> - b < 0))"
  1684 proof-
  1685   have th: "(real_of_int a + b >0) = (real_of_int (-a) + (-b)< 0)" by arith
  1686   show ?thesis 
  1687     by (simp only:th split_int_less_real'[where a="-a" and b="-b"]) (auto simp add: algebra_simps)
  1688 qed
  1689 
  1690 lemma split_int_le_real:
  1691   "(real_of_int (a::int) \<le> b) = (a \<le> \<lfloor>b\<rfloor> \<or> (a = \<lfloor>b\<rfloor> \<and> real_of_int \<lfloor>b\<rfloor> < b))"
  1692 proof( auto)
  1693   assume alb: "real_of_int a \<le> b" and agb: "\<not> a \<le> \<lfloor>b\<rfloor>"
  1694   from alb have "\<lfloor>real_of_int a\<rfloor> \<le> \<lfloor>b\<rfloor>" by (simp only: floor_mono)
  1695   hence "a \<le> \<lfloor>b\<rfloor>" by simp with agb show "False" by simp
  1696 next
  1697   assume alb: "a \<le> \<lfloor>b\<rfloor>"
  1698   hence "real_of_int a \<le> real_of_int \<lfloor>b\<rfloor>" by (simp only: floor_mono)
  1699   also have "\<dots>\<le> b" by simp  finally show  "real_of_int a \<le> b" .
  1700 qed
  1701 
  1702 lemma split_int_le_real':
  1703   "(real_of_int (a::int) + b \<le> 0) = (real_of_int a - real_of_int \<lfloor>- b\<rfloor> \<le> 0 \<or> (real_of_int a - real_of_int \<lfloor>- b\<rfloor> = 0 \<and> real_of_int \<lfloor>- b\<rfloor> + b < 0))"
  1704 proof-
  1705   have "(real_of_int a + b \<le>0) = (real_of_int a \<le> -b)" by arith
  1706   with split_int_le_real[where a="a" and b="-b"] show ?thesis by arith
  1707 qed
  1708 
  1709 lemma split_int_ge_real':
  1710   "(real_of_int (a::int) + b \<ge> 0) = (real_of_int a + real_of_int \<lfloor>b\<rfloor> \<ge> 0 \<or> (real_of_int a + real_of_int \<lfloor>b\<rfloor> = 0 \<and> real_of_int \<lfloor>b\<rfloor> - b < 0))"
  1711 proof-
  1712   have th: "(real_of_int a + b \<ge>0) = (real_of_int (-a) + (-b) \<le> 0)" by arith
  1713   show ?thesis by (simp only: th split_int_le_real'[where a="-a" and b="-b"])
  1714     (simp add: algebra_simps ,arith)
  1715 qed
  1716 
  1717 lemma split_int_eq_real: "(real_of_int (a::int) = b) = ( a = \<lfloor>b\<rfloor> \<and> b = real_of_int \<lfloor>b\<rfloor>)" (is "?l = ?r")
  1718 by auto
  1719 
  1720 lemma split_int_eq_real': "(real_of_int (a::int) + b = 0) = ( a - \<lfloor>- b\<rfloor> = 0 \<and> real_of_int \<lfloor>- b\<rfloor> + b = 0)" (is "?l = ?r")
  1721 proof-
  1722   have "?l = (real_of_int a = -b)" by arith
  1723   with split_int_eq_real[where a="a" and b="-b"] show ?thesis by simp arith
  1724 qed
  1725 
  1726 lemma zlfm_I:
  1727   assumes qfp: "qfree p"
  1728   shows "(Ifm (real_of_int i #bs) (zlfm p) = Ifm (real_of_int i# bs) p) \<and> iszlfm (zlfm p) (real_of_int (i::int) #bs)"
  1729   (is "(?I (?l p) = ?I p) \<and> ?L (?l p)")
  1730 using qfp
  1731 proof(induct p rule: zlfm.induct)
  1732   case (5 a)
  1733   let ?c = "fst (zsplit0 a)"
  1734   let ?r = "snd (zsplit0 a)"
  1735   have spl: "zsplit0 a = (?c,?r)" by simp
  1736   from zsplit0_I[OF spl, where x="i" and bs="bs"]
  1737   have Ia:"Inum (real_of_int i # bs) a = Inum (real_of_int i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto
  1738   let ?N = "\<lambda> t. Inum (real_of_int i#bs) t"
  1739   have "?c = 0 \<or> (?c >0 \<and> ?c\<noteq>0) \<or> (?c<0 \<and> ?c\<noteq>0)" by arith
  1740   moreover
  1741   {assume "?c=0" hence ?case using zsplit0_I[OF spl, where x="i" and bs="bs"]
  1742       by (cases "?r", simp_all add: Let_def split_def,rename_tac nat a b,case_tac "nat", simp_all)}
  1743   moreover
  1744   {assume cp: "?c > 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (Lt a))"
  1745       by (simp add: nb Let_def split_def isint_Floor isint_neg)
  1746     have "?I (Lt a) = (real_of_int (?c * i) + (?N ?r) < 0)" using Ia by (simp add: Let_def split_def)
  1747     also have "\<dots> = (?I (?l (Lt a)))" apply (simp only: split_int_less_real'[where a="?c*i" and b="?N ?r"]) by (simp add: Ia cp cnz Let_def split_def)
  1748     finally have ?case using l by simp}
  1749   moreover
  1750   {assume cn: "?c < 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (Lt a))"
  1751       by (simp add: nb Let_def split_def isint_Floor isint_neg)
  1752     have "?I (Lt a) = (real_of_int (?c * i) + (?N ?r) < 0)" using Ia by (simp add: Let_def split_def)
  1753     also from cn cnz have "\<dots> = (?I (?l (Lt a)))" by (simp only: split_int_less_real'[where a="?c*i" and b="?N ?r"]) (simp add: Ia Let_def split_def ac_simps, arith)
  1754     finally have ?case using l by simp}
  1755   ultimately show ?case by blast
  1756 next
  1757   case (6 a)
  1758   let ?c = "fst (zsplit0 a)"
  1759   let ?r = "snd (zsplit0 a)"
  1760   have spl: "zsplit0 a = (?c,?r)" by simp
  1761   from zsplit0_I[OF spl, where x="i" and bs="bs"]
  1762   have Ia:"Inum (real_of_int i # bs) a = Inum (real_of_int i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto
  1763   let ?N = "\<lambda> t. Inum (real_of_int i#bs) t"
  1764   have "?c = 0 \<or> (?c >0 \<and> ?c\<noteq>0) \<or> (?c<0 \<and> ?c\<noteq>0)" by arith
  1765   moreover
  1766   {assume "?c=0" hence ?case using zsplit0_I[OF spl, where x="i" and bs="bs"]
  1767       by (cases "?r", simp_all add: Let_def split_def, rename_tac nat a b, case_tac "nat",simp_all)}
  1768   moreover
  1769   {assume cp: "?c > 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (Le a))"
  1770       by (simp add: nb Let_def split_def isint_Floor isint_neg)
  1771     have "?I (Le a) = (real_of_int (?c * i) + (?N ?r) \<le> 0)" using Ia by (simp add: Let_def split_def)
  1772     also have "\<dots> = (?I (?l (Le a)))" by (simp only: split_int_le_real'[where a="?c*i" and b="?N ?r"]) (simp add: Ia cp cnz Let_def split_def)
  1773     finally have ?case using l by simp}
  1774   moreover
  1775   {assume cn: "?c < 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (Le a))"
  1776       by (simp add: nb Let_def split_def isint_Floor isint_neg)
  1777     have "?I (Le a) = (real_of_int (?c * i) + (?N ?r) \<le> 0)" using Ia by (simp add: Let_def split_def)
  1778     also from cn cnz have "\<dots> = (?I (?l (Le a)))" by (simp only: split_int_le_real'[where a="?c*i" and b="?N ?r"]) (simp add: Ia Let_def split_def ac_simps, arith)
  1779     finally have ?case using l by simp}
  1780   ultimately show ?case by blast
  1781 next
  1782   case (7 a)
  1783   let ?c = "fst (zsplit0 a)"
  1784   let ?r = "snd (zsplit0 a)"
  1785   have spl: "zsplit0 a = (?c,?r)" by simp
  1786   from zsplit0_I[OF spl, where x="i" and bs="bs"]
  1787   have Ia:"Inum (real_of_int i # bs) a = Inum (real_of_int i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto
  1788   let ?N = "\<lambda> t. Inum (real_of_int i#bs) t"
  1789   have "?c = 0 \<or> (?c >0 \<and> ?c\<noteq>0) \<or> (?c<0 \<and> ?c\<noteq>0)" by arith
  1790   moreover
  1791   {assume "?c=0" hence ?case using zsplit0_I[OF spl, where x="i" and bs="bs"]
  1792       by (cases "?r", simp_all add: Let_def split_def, rename_tac nat a b, case_tac "nat", simp_all)}
  1793   moreover
  1794   {assume cp: "?c > 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (Gt a))"
  1795       by (simp add: nb Let_def split_def isint_Floor isint_neg)
  1796     have "?I (Gt a) = (real_of_int (?c * i) + (?N ?r) > 0)" using Ia by (simp add: Let_def split_def)
  1797     also have "\<dots> = (?I (?l (Gt a)))" by (simp only: split_int_gt_real'[where a="?c*i" and b="?N ?r"]) (simp add: Ia cp cnz Let_def split_def)
  1798     finally have ?case using l by simp}
  1799   moreover
  1800   {assume cn: "?c < 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (Gt a))"
  1801       by (simp add: nb Let_def split_def isint_Floor isint_neg)
  1802     have "?I (Gt a) = (real_of_int (?c * i) + (?N ?r) > 0)" using Ia by (simp add: Let_def split_def)
  1803     also from cn cnz have "\<dots> = (?I (?l (Gt a)))" by (simp only: split_int_gt_real'[where a="?c*i" and b="?N ?r"]) (simp add: Ia Let_def split_def ac_simps, arith)
  1804     finally have ?case using l by simp}
  1805   ultimately show ?case by blast
  1806 next
  1807   case (8 a)
  1808    let ?c = "fst (zsplit0 a)"
  1809   let ?r = "snd (zsplit0 a)"
  1810   have spl: "zsplit0 a = (?c,?r)" by simp
  1811   from zsplit0_I[OF spl, where x="i" and bs="bs"]
  1812   have Ia:"Inum (real_of_int i # bs) a = Inum (real_of_int i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto
  1813   let ?N = "\<lambda> t. Inum (real_of_int i#bs) t"
  1814   have "?c = 0 \<or> (?c >0 \<and> ?c\<noteq>0) \<or> (?c<0 \<and> ?c\<noteq>0)" by arith
  1815   moreover
  1816   {assume "?c=0" hence ?case using zsplit0_I[OF spl, where x="i" and bs="bs"]
  1817       by (cases "?r", simp_all add: Let_def split_def, rename_tac nat a b, case_tac "nat", simp_all)}
  1818   moreover
  1819   {assume cp: "?c > 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (Ge a))"
  1820       by (simp add: nb Let_def split_def isint_Floor isint_neg)
  1821     have "?I (Ge a) = (real_of_int (?c * i) + (?N ?r) \<ge> 0)" using Ia by (simp add: Let_def split_def)
  1822     also have "\<dots> = (?I (?l (Ge a)))" by (simp only: split_int_ge_real'[where a="?c*i" and b="?N ?r"]) (simp add: Ia cp cnz Let_def split_def)
  1823     finally have ?case using l by simp}
  1824   moreover
  1825   {assume cn: "?c < 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (Ge a))"
  1826       by (simp add: nb Let_def split_def isint_Floor isint_neg)
  1827     have "?I (Ge a) = (real_of_int (?c * i) + (?N ?r) \<ge> 0)" using Ia by (simp add: Let_def split_def)
  1828     also from cn cnz have "\<dots> = (?I (?l (Ge a)))" by (simp only: split_int_ge_real'[where a="?c*i" and b="?N ?r"]) (simp add: Ia Let_def split_def ac_simps, arith)
  1829     finally have ?case using l by simp}
  1830   ultimately show ?case by blast
  1831 next
  1832   case (9 a)
  1833   let ?c = "fst (zsplit0 a)"
  1834   let ?r = "snd (zsplit0 a)"
  1835   have spl: "zsplit0 a = (?c,?r)" by simp
  1836   from zsplit0_I[OF spl, where x="i" and bs="bs"]
  1837   have Ia:"Inum (real_of_int i # bs) a = Inum (real_of_int i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto
  1838   let ?N = "\<lambda> t. Inum (real_of_int i#bs) t"
  1839   have "?c = 0 \<or> (?c >0 \<and> ?c\<noteq>0) \<or> (?c<0 \<and> ?c\<noteq>0)" by arith
  1840   moreover
  1841   {assume "?c=0" hence ?case using zsplit0_I[OF spl, where x="i" and bs="bs"]
  1842       by (cases "?r", simp_all add: Let_def split_def, rename_tac nat a b, case_tac "nat", simp_all)}
  1843   moreover
  1844   {assume cp: "?c > 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (Eq a))"
  1845       by (simp add: nb Let_def split_def isint_Floor isint_neg)
  1846     have "?I (Eq a) = (real_of_int (?c * i) + (?N ?r) = 0)" using Ia by (simp add: Let_def split_def)
  1847     also have "\<dots> = (?I (?l (Eq a)))" using cp cnz  by (simp only: split_int_eq_real'[where a="?c*i" and b="?N ?r"]) (simp add: Let_def split_def Ia of_int_mult[symmetric] del: of_int_mult)
  1848     finally have ?case using l by simp}
  1849   moreover
  1850   {assume cn: "?c < 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (Eq a))"
  1851       by (simp add: nb Let_def split_def isint_Floor isint_neg)
  1852     have "?I (Eq a) = (real_of_int (?c * i) + (?N ?r) = 0)" using Ia by (simp add: Let_def split_def)
  1853     also from cn cnz have "\<dots> = (?I (?l (Eq a)))" by (simp only: split_int_eq_real'[where a="?c*i" and b="?N ?r"]) (simp add: Let_def split_def Ia of_int_mult[symmetric] del: of_int_mult,arith)
  1854     finally have ?case using l by simp}
  1855   ultimately show ?case by blast
  1856 next
  1857   case (10 a)
  1858   let ?c = "fst (zsplit0 a)"
  1859   let ?r = "snd (zsplit0 a)"
  1860   have spl: "zsplit0 a = (?c,?r)" by simp
  1861   from zsplit0_I[OF spl, where x="i" and bs="bs"]
  1862   have Ia:"Inum (real_of_int i # bs) a = Inum (real_of_int i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto
  1863   let ?N = "\<lambda> t. Inum (real_of_int i#bs) t"
  1864   have "?c = 0 \<or> (?c >0 \<and> ?c\<noteq>0) \<or> (?c<0 \<and> ?c\<noteq>0)" by arith
  1865   moreover
  1866   {assume "?c=0" hence ?case using zsplit0_I[OF spl, where x="i" and bs="bs"]
  1867       by (cases "?r", simp_all add: Let_def split_def, rename_tac nat a b, case_tac "nat", simp_all)}
  1868   moreover
  1869   {assume cp: "?c > 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (NEq a))"
  1870       by (simp add: nb Let_def split_def isint_Floor isint_neg)
  1871     have "?I (NEq a) = (real_of_int (?c * i) + (?N ?r) \<noteq> 0)" using Ia by (simp add: Let_def split_def)
  1872     also have "\<dots> = (?I (?l (NEq a)))" using cp cnz  by (simp only: split_int_eq_real'[where a="?c*i" and b="?N ?r"]) (simp add: Let_def split_def Ia of_int_mult[symmetric] del: of_int_mult)
  1873     finally have ?case using l by simp}
  1874   moreover
  1875   {assume cn: "?c < 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (NEq a))"
  1876       by (simp add: nb Let_def split_def isint_Floor isint_neg)
  1877     have "?I (NEq a) = (real_of_int (?c * i) + (?N ?r) \<noteq> 0)" using Ia by (simp add: Let_def split_def)
  1878     also from cn cnz have "\<dots> = (?I (?l (NEq a)))" by (simp only: split_int_eq_real'[where a="?c*i" and b="?N ?r"]) (simp add: Let_def split_def Ia of_int_mult[symmetric] del: of_int_mult,arith)
  1879     finally have ?case using l by simp}
  1880   ultimately show ?case by blast
  1881 next
  1882   case (11 j a)
  1883   let ?c = "fst (zsplit0 a)"
  1884   let ?r = "snd (zsplit0 a)"
  1885   have spl: "zsplit0 a = (?c,?r)" by simp
  1886   from zsplit0_I[OF spl, where x="i" and bs="bs"]
  1887   have Ia:"Inum (real_of_int i # bs) a = Inum (real_of_int i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto
  1888   let ?N = "\<lambda> t. Inum (real_of_int i#bs) t"
  1889   have "j=0 \<or> (j\<noteq>0 \<and> ?c = 0) \<or> (j\<noteq>0 \<and> ?c >0 \<and> ?c\<noteq>0) \<or> (j\<noteq> 0 \<and> ?c<0 \<and> ?c\<noteq>0)" by arith
  1890   moreover
  1891   { assume j: "j=0" hence z: "zlfm (Dvd j a) = (zlfm (Eq a))" by (simp add: Let_def)
  1892     hence ?case using 11 j by (simp del: zlfm.simps add: rdvd_left_0_eq)}
  1893   moreover
  1894   {assume "?c=0" and "j\<noteq>0" hence ?case
  1895       using zsplit0_I[OF spl, where x="i" and bs="bs"] rdvd_abs1[where d="j"]
  1896       by (cases "?r", simp_all add: Let_def split_def, rename_tac nat a b, case_tac "nat", simp_all)}
  1897   moreover
  1898   {assume cp: "?c > 0" and cnz: "?c\<noteq>0" and jnz: "j\<noteq>0" hence l: "?L (?l (Dvd j a))"
  1899       by (simp add: nb Let_def split_def isint_Floor isint_neg)
  1900     have "?I (Dvd j a) = (real_of_int j rdvd (real_of_int (?c * i) + (?N ?r)))"
  1901       using Ia by (simp add: Let_def split_def)
  1902     also have "\<dots> = (real_of_int \<bar>j\<bar> rdvd real_of_int (?c*i) + (?N ?r))"
  1903       by (simp only: rdvd_abs1[where d="j" and t="real_of_int (?c*i) + ?N ?r", symmetric]) simp
  1904     also have "\<dots> = (\<bar>j\<bar> dvd \<lfloor>(?N ?r) + real_of_int (?c*i)\<rfloor> \<and>
  1905        (real_of_int \<lfloor>(?N ?r) + real_of_int (?c*i)\<rfloor> = (real_of_int (?c*i) + (?N ?r))))"
  1906       by(simp only: int_rdvd_real[where i="\<bar>j\<bar>" and x="real_of_int (?c*i) + (?N ?r)"]) (simp only: ac_simps)
  1907     also have "\<dots> = (?I (?l (Dvd j a)))" using cp cnz jnz
  1908       by (simp add: Let_def split_def int_rdvd_iff[symmetric]
  1909         del: of_int_mult) (auto simp add: ac_simps)
  1910     finally have ?case using l jnz  by simp }
  1911   moreover
  1912   {assume cn: "?c < 0" and cnz: "?c\<noteq>0" and jnz: "j\<noteq>0" hence l: "?L (?l (Dvd j a))"
  1913       by (simp add: nb Let_def split_def isint_Floor isint_neg)
  1914     have "?I (Dvd j a) = (real_of_int j rdvd (real_of_int (?c * i) + (?N ?r)))"
  1915       using Ia by (simp add: Let_def split_def)
  1916     also have "\<dots> = (real_of_int \<bar>j\<bar> rdvd real_of_int (?c*i) + (?N ?r))"
  1917       by (simp only: rdvd_abs1[where d="j" and t="real_of_int (?c*i) + ?N ?r", symmetric]) simp
  1918     also have "\<dots> = (\<bar>j\<bar> dvd \<lfloor>(?N ?r) + real_of_int (?c*i)\<rfloor> \<and>
  1919        (real_of_int \<lfloor>(?N ?r) + real_of_int (?c*i)\<rfloor> = (real_of_int (?c*i) + (?N ?r))))"
  1920       by(simp only: int_rdvd_real[where i="\<bar>j\<bar>" and x="real_of_int (?c*i) + (?N ?r)"]) (simp only: ac_simps)
  1921     also have "\<dots> = (?I (?l (Dvd j a)))" using cn cnz jnz
  1922       using rdvd_minus [where d="\<bar>j\<bar>" and t="real_of_int (?c*i + \<lfloor>?N ?r\<rfloor>)", simplified, symmetric]
  1923       by (simp add: Let_def split_def int_rdvd_iff[symmetric]
  1924         del: of_int_mult) (auto simp add: ac_simps)
  1925     finally have ?case using l jnz by blast }
  1926   ultimately show ?case by blast
  1927 next
  1928   case (12 j a)
  1929   let ?c = "fst (zsplit0 a)"
  1930   let ?r = "snd (zsplit0 a)"
  1931   have spl: "zsplit0 a = (?c,?r)" by simp
  1932   from zsplit0_I[OF spl, where x="i" and bs="bs"]
  1933   have Ia:"Inum (real_of_int i # bs) a = Inum (real_of_int i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto
  1934   let ?N = "\<lambda> t. Inum (real_of_int i#bs) t"
  1935   have "j=0 \<or> (j\<noteq>0 \<and> ?c = 0) \<or> (j\<noteq>0 \<and> ?c >0 \<and> ?c\<noteq>0) \<or> (j\<noteq> 0 \<and> ?c<0 \<and> ?c\<noteq>0)" by arith
  1936   moreover
  1937   {assume j: "j=0" hence z: "zlfm (NDvd j a) = (zlfm (NEq a))" by (simp add: Let_def)
  1938     hence ?case using 12 j by (simp del: zlfm.simps add: rdvd_left_0_eq)}
  1939   moreover
  1940   {assume "?c=0" and "j\<noteq>0" hence ?case
  1941       using zsplit0_I[OF spl, where x="i" and bs="bs"] rdvd_abs1[where d="j"]
  1942       by (cases "?r", simp_all add: Let_def split_def, rename_tac nat a b, case_tac "nat", simp_all)}
  1943   moreover
  1944   {assume cp: "?c > 0" and cnz: "?c\<noteq>0" and jnz: "j\<noteq>0" hence l: "?L (?l (NDvd j a))"
  1945       by (simp add: nb Let_def split_def isint_Floor isint_neg)
  1946     have "?I (NDvd j a) = (\<not> (real_of_int j rdvd (real_of_int (?c * i) + (?N ?r))))"
  1947       using Ia by (simp add: Let_def split_def)
  1948     also have "\<dots> = (\<not> (real_of_int \<bar>j\<bar> rdvd real_of_int (?c*i) + (?N ?r)))"
  1949       by (simp only: rdvd_abs1[where d="j" and t="real_of_int (?c*i) + ?N ?r", symmetric]) simp
  1950     also have "\<dots> = (\<not> (\<bar>j\<bar> dvd \<lfloor>(?N ?r) + real_of_int (?c*i)\<rfloor> \<and>
  1951        (real_of_int \<lfloor>(?N ?r) + real_of_int (?c*i)\<rfloor> = (real_of_int (?c*i) + (?N ?r)))))"
  1952       by(simp only: int_rdvd_real[where i="\<bar>j\<bar>" and x="real_of_int (?c*i) + (?N ?r)"]) (simp only: ac_simps)
  1953     also have "\<dots> = (?I (?l (NDvd j a)))" using cp cnz jnz
  1954       by (simp add: Let_def split_def int_rdvd_iff[symmetric]
  1955         del: of_int_mult) (auto simp add: ac_simps)
  1956     finally have ?case using l jnz  by simp }
  1957   moreover
  1958   {assume cn: "?c < 0" and cnz: "?c\<noteq>0" and jnz: "j\<noteq>0" hence l: "?L (?l (NDvd j a))"
  1959       by (simp add: nb Let_def split_def isint_Floor isint_neg)
  1960     have "?I (NDvd j a) = (\<not> (real_of_int j rdvd (real_of_int (?c * i) + (?N ?r))))"
  1961       using Ia by (simp add: Let_def split_def)
  1962     also have "\<dots> = (\<not> (real_of_int \<bar>j\<bar> rdvd real_of_int (?c*i) + (?N ?r)))"
  1963       by (simp only: rdvd_abs1[where d="j" and t="real_of_int (?c*i) + ?N ?r", symmetric]) simp
  1964     also have "\<dots> = (\<not> (\<bar>j\<bar> dvd \<lfloor>(?N ?r) + real_of_int (?c*i)\<rfloor> \<and>
  1965        (real_of_int \<lfloor>(?N ?r) + real_of_int (?c*i)\<rfloor> = (real_of_int (?c*i) + (?N ?r)))))"
  1966       by(simp only: int_rdvd_real[where i="\<bar>j\<bar>" and x="real_of_int (?c*i) + (?N ?r)"]) (simp only: ac_simps)
  1967     also have "\<dots> = (?I (?l (NDvd j a)))" using cn cnz jnz
  1968       using rdvd_minus [where d="\<bar>j\<bar>" and t="real_of_int (?c*i + \<lfloor>?N ?r\<rfloor>)", simplified, symmetric]
  1969       by (simp add: Let_def split_def int_rdvd_iff[symmetric]
  1970         del: of_int_mult) (auto simp add: ac_simps)
  1971     finally have ?case using l jnz by blast }
  1972   ultimately show ?case by blast
  1973 qed auto
  1974 
  1975 text\<open>plusinf : Virtual substitution of \<open>+\<infinity>\<close>
  1976        minusinf: Virtual substitution of \<open>-\<infinity>\<close>
  1977        \<open>\<delta>\<close> Compute lcm \<open>d| Dvd d  c*x+t \<in> p\<close>
  1978        \<open>d_\<delta>\<close> checks if a given l divides all the ds above\<close>
  1979 
  1980 fun minusinf:: "fm \<Rightarrow> fm"
  1981 where
  1982   "minusinf (And p q) = conj (minusinf p) (minusinf q)"
  1983 | "minusinf (Or p q) = disj (minusinf p) (minusinf q)"
  1984 | "minusinf (Eq  (CN 0 c e)) = F"
  1985 | "minusinf (NEq (CN 0 c e)) = T"
  1986 | "minusinf (Lt  (CN 0 c e)) = T"
  1987 | "minusinf (Le  (CN 0 c e)) = T"
  1988 | "minusinf (Gt  (CN 0 c e)) = F"
  1989 | "minusinf (Ge  (CN 0 c e)) = F"
  1990 | "minusinf p = p"
  1991 
  1992 lemma minusinf_qfree: "qfree p \<Longrightarrow> qfree (minusinf p)"
  1993   by (induct p rule: minusinf.induct, auto)
  1994 
  1995 fun plusinf:: "fm \<Rightarrow> fm"
  1996 where
  1997   "plusinf (And p q) = conj (plusinf p) (plusinf q)"
  1998 | "plusinf (Or p q) = disj (plusinf p) (plusinf q)"
  1999 | "plusinf (Eq  (CN 0 c e)) = F"
  2000 | "plusinf (NEq (CN 0 c e)) = T"
  2001 | "plusinf (Lt  (CN 0 c e)) = F"
  2002 | "plusinf (Le  (CN 0 c e)) = F"
  2003 | "plusinf (Gt  (CN 0 c e)) = T"
  2004 | "plusinf (Ge  (CN 0 c e)) = T"
  2005 | "plusinf p = p"
  2006 
  2007 fun \<delta> :: "fm \<Rightarrow> int"
  2008 where
  2009   "\<delta> (And p q) = lcm (\<delta> p) (\<delta> q)"
  2010 | "\<delta> (Or p q) = lcm (\<delta> p) (\<delta> q)"
  2011 | "\<delta> (Dvd i (CN 0 c e)) = i"
  2012 | "\<delta> (NDvd i (CN 0 c e)) = i"
  2013 | "\<delta> p = 1"
  2014 
  2015 fun d_\<delta> :: "fm \<Rightarrow> int \<Rightarrow> bool"
  2016 where
  2017   "d_\<delta> (And p q) = (\<lambda> d. d_\<delta> p d \<and> d_\<delta> q d)"
  2018 | "d_\<delta> (Or p q) = (\<lambda> d. d_\<delta> p d \<and> d_\<delta> q d)"
  2019 | "d_\<delta> (Dvd i (CN 0 c e)) = (\<lambda> d. i dvd d)"
  2020 | "d_\<delta> (NDvd i (CN 0 c e)) = (\<lambda> d. i dvd d)"
  2021 | "d_\<delta> p = (\<lambda> d. True)"
  2022 
  2023 lemma delta_mono:
  2024   assumes lin: "iszlfm p bs"
  2025   and d: "d dvd d'"
  2026   and ad: "d_\<delta> p d"
  2027   shows "d_\<delta> p d'"
  2028   using lin ad d
  2029 proof(induct p rule: iszlfm.induct)
  2030   case (9 i c e)  thus ?case using d
  2031     by (simp add: dvd_trans[of "i" "d" "d'"])
  2032 next
  2033   case (10 i c e) thus ?case using d
  2034     by (simp add: dvd_trans[of "i" "d" "d'"])
  2035 qed simp_all
  2036 
  2037 lemma \<delta> : assumes lin:"iszlfm p bs"
  2038   shows "d_\<delta> p (\<delta> p) \<and> \<delta> p >0"
  2039 using lin
  2040 proof (induct p rule: iszlfm.induct)
  2041   case (1 p q)
  2042   let ?d = "\<delta> (And p q)"
  2043   from 1 lcm_pos_int have dp: "?d >0" by simp
  2044   have d1: "\<delta> p dvd \<delta> (And p q)" using 1 by simp
  2045   hence th: "d_\<delta> p ?d"
  2046     using delta_mono 1 by (simp only: iszlfm.simps) blast
  2047   have "\<delta> q dvd \<delta> (And p q)" using 1 by simp
  2048   hence th': "d_\<delta> q ?d" using delta_mono 1 by (simp only: iszlfm.simps) blast
  2049   from th th' dp show ?case by simp
  2050 next
  2051   case (2 p q)
  2052   let ?d = "\<delta> (And p q)"
  2053   from 2 lcm_pos_int have dp: "?d >0" by simp
  2054   have "\<delta> p dvd \<delta> (And p q)" using 2 by simp
  2055   hence th: "d_\<delta> p ?d" using delta_mono 2 by (simp only: iszlfm.simps) blast
  2056   have "\<delta> q dvd \<delta> (And p q)" using 2 by simp
  2057   hence th': "d_\<delta> q ?d" using delta_mono 2 by (simp only: iszlfm.simps) blast
  2058   from th th' dp show ?case by simp
  2059 qed simp_all
  2060 
  2061 
  2062 lemma minusinf_inf:
  2063   assumes linp: "iszlfm p (a # bs)"
  2064   shows "\<exists> (z::int). \<forall> x < z. Ifm ((real_of_int x)#bs) (minusinf p) = Ifm ((real_of_int x)#bs) p"
  2065   (is "?P p" is "\<exists> (z::int). \<forall> x < z. ?I x (?M p) = ?I x p")
  2066 using linp
  2067 proof (induct p rule: minusinf.induct)
  2068   case (1 f g)
  2069   then have "?P f" by simp
  2070   then obtain z1 where z1_def: "\<forall> x < z1. ?I x (?M f) = ?I x f" by blast
  2071   with 1 have "?P g" by simp
  2072   then obtain z2 where z2_def: "\<forall> x < z2. ?I x (?M g) = ?I x g" by blast
  2073   let ?z = "min z1 z2"
  2074   from z1_def z2_def have "\<forall> x < ?z. ?I x (?M (And f g)) = ?I x (And f g)" by simp
  2075   thus ?case by blast
  2076 next
  2077   case (2 f g)
  2078   then have "?P f" by simp
  2079   then obtain z1 where z1_def: "\<forall> x < z1. ?I x (?M f) = ?I x f" by blast
  2080   with 2 have "?P g" by simp
  2081   then obtain z2 where z2_def: "\<forall> x < z2. ?I x (?M g) = ?I x g" by blast
  2082   let ?z = "min z1 z2"
  2083   from z1_def z2_def have "\<forall> x < ?z. ?I x (?M (Or f g)) = ?I x (Or f g)" by simp
  2084   thus ?case by blast
  2085 next
  2086   case (3 c e)
  2087   then have "c > 0" by simp
  2088   hence rcpos: "real_of_int c > 0" by simp
  2089   from 3 have nbe: "numbound0 e" by simp
  2090   fix y
  2091   have "\<forall> x < \<lfloor>- (Inum (y#bs) e) / (real_of_int c)\<rfloor>. ?I x (?M (Eq (CN 0 c e))) = ?I x (Eq (CN 0 c e))"
  2092   proof (simp add: less_floor_iff , rule allI, rule impI)
  2093     fix x :: int
  2094     assume A: "real_of_int x + 1 \<le> - (Inum (y # bs) e / real_of_int c)"
  2095     hence th1:"real_of_int x < - (Inum (y # bs) e / real_of_int c)" by simp
  2096     with rcpos  have "(real_of_int c)*(real_of_int  x) < (real_of_int c)*(- (Inum (y # bs) e / real_of_int c))"
  2097       by (simp only: mult_strict_left_mono [OF th1 rcpos])
  2098     hence "real_of_int c * real_of_int x + Inum (y # bs) e \<noteq> 0"using rcpos  by simp
  2099     thus "real_of_int c * real_of_int x + Inum (real_of_int x # bs) e \<noteq> 0"
  2100       using numbound0_I[OF nbe, where b="y" and bs="bs" and b'="real_of_int x"]  by simp
  2101   qed
  2102   thus ?case by blast
  2103 next
  2104   case (4 c e)
  2105   then have "c > 0" by simp hence rcpos: "real_of_int c > 0" by simp
  2106   from 4 have nbe: "numbound0 e" by simp
  2107   fix y
  2108   have "\<forall> x < \<lfloor>- (Inum (y#bs) e) / (real_of_int c)\<rfloor>. ?I x (?M (NEq (CN 0 c e))) = ?I x (NEq (CN 0 c e))"
  2109   proof (simp add: less_floor_iff , rule allI, rule impI)
  2110     fix x :: int
  2111     assume A: "real_of_int x + 1 \<le> - (Inum (y # bs) e / real_of_int c)"
  2112     hence th1:"real_of_int x < - (Inum (y # bs) e / real_of_int c)" by simp
  2113     with rcpos  have "(real_of_int c)*(real_of_int x) < (real_of_int c)*(- (Inum (y # bs) e / real_of_int c))"
  2114       by (simp only: mult_strict_left_mono [OF th1 rcpos])
  2115     hence "real_of_int c * real_of_int x + Inum (y # bs) e \<noteq> 0"using rcpos  by simp
  2116     thus "real_of_int c * real_of_int x + Inum (real_of_int x # bs) e \<noteq> 0"
  2117       using numbound0_I[OF nbe, where b="y" and bs="bs" and b'="real_of_int x"]  by simp
  2118   qed
  2119   thus ?case by blast
  2120 next
  2121   case (5 c e)
  2122   then have "c > 0" by simp hence rcpos: "real_of_int c > 0" by simp
  2123   from 5 have nbe: "numbound0 e" by simp
  2124   fix y
  2125   have "\<forall> x < \<lfloor>- (Inum (y#bs) e) / (real_of_int c)\<rfloor>. ?I x (?M (Lt (CN 0 c e))) = ?I x (Lt (CN 0 c e))"
  2126   proof (simp add: less_floor_iff , rule allI, rule impI)
  2127     fix x :: int
  2128     assume A: "real_of_int x + 1 \<le> - (Inum (y # bs) e / real_of_int c)"
  2129     hence th1:"real_of_int x < - (Inum (y # bs) e / real_of_int c)" by simp
  2130     with rcpos  have "(real_of_int c)*(real_of_int x) < (real_of_int c)*(- (Inum (y # bs) e / real_of_int c))"
  2131       by (simp only: mult_strict_left_mono [OF th1 rcpos])
  2132     thus "real_of_int c * real_of_int x + Inum (real_of_int x # bs) e < 0"
  2133       using numbound0_I[OF nbe, where b="y" and bs="bs" and b'="real_of_int x"] rcpos by simp
  2134   qed
  2135   thus ?case by blast
  2136 next
  2137   case (6 c e)
  2138   then have "c > 0" by simp hence rcpos: "real_of_int c > 0" by simp
  2139   from 6 have nbe: "numbound0 e" by simp
  2140   fix y
  2141   have "\<forall> x < \<lfloor>- (Inum (y#bs) e) / (real_of_int c)\<rfloor>. ?I x (?M (Le (CN 0 c e))) = ?I x (Le (CN 0 c e))"
  2142   proof (simp add: less_floor_iff , rule allI, rule impI)
  2143     fix x :: int
  2144     assume A: "real_of_int x + 1 \<le> - (Inum (y # bs) e / real_of_int c)"
  2145     hence th1:"real_of_int x < - (Inum (y # bs) e / real_of_int c)" by simp
  2146     with rcpos  have "(real_of_int c)*(real_of_int x) < (real_of_int c)*(- (Inum (y # bs) e / real_of_int c))"
  2147       by (simp only: mult_strict_left_mono [OF th1 rcpos])
  2148     thus "real_of_int c * real_of_int x + Inum (real_of_int x # bs) e \<le> 0"
  2149       using numbound0_I[OF nbe, where b="y" and bs="bs" and b'="real_of_int x"] rcpos by simp
  2150   qed
  2151   thus ?case by blast
  2152 next
  2153   case (7 c e)
  2154   then have "c > 0" by simp hence rcpos: "real_of_int c > 0" by simp
  2155   from 7 have nbe: "numbound0 e" by simp
  2156   fix y
  2157   have "\<forall> x < \<lfloor>- (Inum (y#bs) e) / (real_of_int c)\<rfloor>. ?I x (?M (Gt (CN 0 c e))) = ?I x (Gt (CN 0 c e))"
  2158   proof (simp add: less_floor_iff , rule allI, rule impI)
  2159     fix x :: int
  2160     assume A: "real_of_int x + 1 \<le> - (Inum (y # bs) e / real_of_int c)"
  2161     hence th1:"real_of_int x < - (Inum (y # bs) e / real_of_int c)" by simp
  2162     with rcpos  have "(real_of_int c)*(real_of_int x) < (real_of_int c)*(- (Inum (y # bs) e / real_of_int c))"
  2163       by (simp only: mult_strict_left_mono [OF th1 rcpos])
  2164     thus "\<not> (real_of_int c * real_of_int x + Inum (real_of_int x # bs) e>0)"
  2165       using numbound0_I[OF nbe, where b="y" and bs="bs" and b'="real_of_int x"] rcpos by simp
  2166   qed
  2167   thus ?case by blast
  2168 next
  2169   case (8 c e)
  2170   then have "c > 0" by simp hence rcpos: "real_of_int c > 0" by simp
  2171   from 8 have nbe: "numbound0 e" by simp
  2172   fix y
  2173   have "\<forall> x < \<lfloor>- (Inum (y#bs) e) / (real_of_int c)\<rfloor>. ?I x (?M (Ge (CN 0 c e))) = ?I x (Ge (CN 0 c e))"
  2174   proof (simp add: less_floor_iff , rule allI, rule impI)
  2175     fix x :: int
  2176     assume A: "real_of_int x + 1 \<le> - (Inum (y # bs) e / real_of_int c)"
  2177     hence th1:"real_of_int x < - (Inum (y # bs) e / real_of_int c)" by simp
  2178     with rcpos  have "(real_of_int c)*(real_of_int x) < (real_of_int c)*(- (Inum (y # bs) e / real_of_int c))"
  2179       by (simp only: mult_strict_left_mono [OF th1 rcpos])
  2180     thus "\<not> real_of_int c * real_of_int x + Inum (real_of_int x # bs) e \<ge> 0"
  2181       using numbound0_I[OF nbe, where b="y" and bs="bs" and b'="real_of_int x"] rcpos by simp
  2182   qed
  2183   thus ?case by blast
  2184 qed simp_all
  2185 
  2186 lemma minusinf_repeats:
  2187   assumes d: "d_\<delta> p d" and linp: "iszlfm p (a # bs)"
  2188   shows "Ifm ((real_of_int(x - k*d))#bs) (minusinf p) = Ifm (real_of_int x #bs) (minusinf p)"
  2189 using linp d
  2190 proof(induct p rule: iszlfm.induct)
  2191   case (9 i c e) hence nbe: "numbound0 e"  and id: "i dvd d" by simp+
  2192     hence "\<exists> k. d=i*k" by (simp add: dvd_def)
  2193     then obtain "di" where di_def: "d=i*di" by blast
  2194     show ?case
  2195     proof(simp add: numbound0_I[OF nbe,where bs="bs" and b="real_of_int x - real_of_int k * real_of_int d" and b'="real_of_int x"] right_diff_distrib, rule iffI)
  2196       assume
  2197         "real_of_int i rdvd real_of_int c * real_of_int x - real_of_int c * (real_of_int k * real_of_int d) + Inum (real_of_int x # bs) e"
  2198       (is "?ri rdvd ?rc*?rx - ?rc*(?rk*?rd) + ?I x e" is "?ri rdvd ?rt")
  2199       hence "\<exists> (l::int). ?rt = ?ri * (real_of_int l)" by (simp add: rdvd_def)
  2200       hence "\<exists> (l::int). ?rc*?rx+ ?I x e = ?ri*(real_of_int l)+?rc*(?rk * (real_of_int i) * (real_of_int di))"
  2201         by (simp add: algebra_simps di_def)
  2202       hence "\<exists> (l::int). ?rc*?rx+ ?I x e = ?ri*(real_of_int (l + c*k*di))"
  2203         by (simp add: algebra_simps)
  2204       hence "\<exists> (l::int). ?rc*?rx+ ?I x e = ?ri* (real_of_int l)" by blast
  2205       thus "real_of_int i rdvd real_of_int c * real_of_int x + Inum (real_of_int x # bs) e" using rdvd_def by simp
  2206     next
  2207       assume
  2208         "real_of_int i rdvd real_of_int c * real_of_int x + Inum (real_of_int x # bs) e" (is "?ri rdvd ?rc*?rx+?e")
  2209       hence "\<exists> (l::int). ?rc*?rx+?e = ?ri * (real_of_int l)" by (simp add: rdvd_def)
  2210       hence "\<exists> (l::int). ?rc*?rx - real_of_int c * (real_of_int k * real_of_int d) +?e = ?ri * (real_of_int l) - real_of_int c * (real_of_int k * real_of_int d)" by simp
  2211       hence "\<exists> (l::int). ?rc*?rx - real_of_int c * (real_of_int k * real_of_int d) +?e = ?ri * (real_of_int l) - real_of_int c * (real_of_int k * real_of_int i * real_of_int di)" by (simp add: di_def)
  2212       hence "\<exists> (l::int). ?rc*?rx - real_of_int c * (real_of_int k * real_of_int d) +?e = ?ri * (real_of_int (l - c*k*di))" by (simp add: algebra_simps)
  2213       hence "\<exists> (l::int). ?rc*?rx - real_of_int c * (real_of_int k * real_of_int d) +?e = ?ri * (real_of_int l)"
  2214         by blast
  2215       thus "real_of_int i rdvd real_of_int c * real_of_int x - real_of_int c * (real_of_int k * real_of_int d) + Inum (real_of_int x # bs) e" using rdvd_def by simp
  2216     qed
  2217 next
  2218   case (10 i c e) hence nbe: "numbound0 e"  and id: "i dvd d" by simp+
  2219     hence "\<exists> k. d=i*k" by (simp add: dvd_def)
  2220     then obtain "di" where di_def: "d=i*di" by blast
  2221     show ?case
  2222     proof(simp add: numbound0_I[OF nbe,where bs="bs" and b="real_of_int x - real_of_int k * real_of_int d" and b'="real_of_int x"] right_diff_distrib, rule iffI)
  2223       assume
  2224         "real_of_int i rdvd real_of_int c * real_of_int x - real_of_int c * (real_of_int k * real_of_int d) + Inum (real_of_int x # bs) e"
  2225       (is "?ri rdvd ?rc*?rx - ?rc*(?rk*?rd) + ?I x e" is "?ri rdvd ?rt")
  2226       hence "\<exists> (l::int). ?rt = ?ri * (real_of_int l)" by (simp add: rdvd_def)
  2227       hence "\<exists> (l::int). ?rc*?rx+ ?I x e = ?ri*(real_of_int l)+?rc*(?rk * (real_of_int i) * (real_of_int di))"
  2228         by (simp add: algebra_simps di_def)
  2229       hence "\<exists> (l::int). ?rc*?rx+ ?I x e = ?ri*(real_of_int (l + c*k*di))"
  2230         by (simp add: algebra_simps)
  2231       hence "\<exists> (l::int). ?rc*?rx+ ?I x e = ?ri* (real_of_int l)" by blast
  2232       thus "real_of_int i rdvd real_of_int c * real_of_int x + Inum (real_of_int x # bs) e" using rdvd_def by simp
  2233     next
  2234       assume
  2235         "real_of_int i rdvd real_of_int c * real_of_int x + Inum (real_of_int x # bs) e" (is "?ri rdvd ?rc*?rx+?e")
  2236       hence "\<exists> (l::int). ?rc*?rx+?e = ?ri * (real_of_int l)"
  2237         by (simp add: rdvd_def)
  2238       hence "\<exists> (l::int). ?rc*?rx - real_of_int c * (real_of_int k * real_of_int d) +?e = ?ri * (real_of_int l) - real_of_int c * (real_of_int k * real_of_int d)"
  2239         by simp
  2240       hence "\<exists> (l::int). ?rc*?rx - real_of_int c * (real_of_int k * real_of_int d) +?e = ?ri * (real_of_int l) - real_of_int c * (real_of_int k * real_of_int i * real_of_int di)"
  2241         by (simp add: di_def)
  2242       hence "\<exists> (l::int). ?rc*?rx - real_of_int c * (real_of_int k * real_of_int d) +?e = ?ri * (real_of_int (l - c*k*di))"
  2243         by (simp add: algebra_simps)
  2244       hence "\<exists> (l::int). ?rc*?rx - real_of_int c * (real_of_int k * real_of_int d) +?e = ?ri * (real_of_int l)"
  2245         by blast
  2246       thus "real_of_int i rdvd real_of_int c * real_of_int x - real_of_int c * (real_of_int k * real_of_int d) + Inum (real_of_int x # bs) e"
  2247         using rdvd_def by simp
  2248     qed
  2249 qed (auto simp add: numbound0_I[where bs="bs" and b="real_of_int(x - k*d)" and b'="real_of_int x"] simp del: of_int_mult of_int_diff)
  2250 
  2251 lemma minusinf_ex:
  2252   assumes lin: "iszlfm p (real_of_int (a::int) #bs)"
  2253   and exmi: "\<exists> (x::int). Ifm (real_of_int x#bs) (minusinf p)" (is "\<exists> x. ?P1 x")
  2254   shows "\<exists> (x::int). Ifm (real_of_int x#bs) p" (is "\<exists> x. ?P x")
  2255 proof-
  2256   let ?d = "\<delta> p"
  2257   from \<delta> [OF lin] have dpos: "?d >0" by simp
  2258   from \<delta> [OF lin] have alld: "d_\<delta> p ?d" by simp
  2259   from minusinf_repeats[OF alld lin] have th1:"\<forall> x k. ?P1 x = ?P1 (x - (k * ?d))" by simp
  2260   from minusinf_inf[OF lin] have th2:"\<exists> z. \<forall> x. x<z \<longrightarrow> (?P x = ?P1 x)" by blast
  2261   from minusinfinity [OF dpos th1 th2] exmi show ?thesis by blast
  2262 qed
  2263 
  2264 lemma minusinf_bex:
  2265   assumes lin: "iszlfm p (real_of_int (a::int) #bs)"
  2266   shows "(\<exists> (x::int). Ifm (real_of_int x#bs) (minusinf p)) =
  2267          (\<exists> (x::int)\<in> {1..\<delta> p}. Ifm (real_of_int x#bs) (minusinf p))"
  2268   (is "(\<exists> x. ?P x) = _")
  2269 proof-
  2270   let ?d = "\<delta> p"
  2271   from \<delta> [OF lin] have dpos: "?d >0" by simp
  2272   from \<delta> [OF lin] have alld: "d_\<delta> p ?d" by simp
  2273   from minusinf_repeats[OF alld lin] have th1:"\<forall> x k. ?P x = ?P (x - (k * ?d))" by simp
  2274   from periodic_finite_ex[OF dpos th1] show ?thesis by blast
  2275 qed
  2276 
  2277 lemma dvd1_eq1: "x > 0 \<Longrightarrow> is_unit x \<longleftrightarrow> x = 1" for x :: int
  2278   by simp
  2279 
  2280 fun a_\<beta> :: "fm \<Rightarrow> int \<Rightarrow> fm" (* adjusts the coefficients of a formula *)
  2281 where
  2282   "a_\<beta> (And p q) = (\<lambda> k. And (a_\<beta> p k) (a_\<beta> q k))"
  2283 | "a_\<beta> (Or p q) = (\<lambda> k. Or (a_\<beta> p k) (a_\<beta> q k))"
  2284 | "a_\<beta> (Eq  (CN 0 c e)) = (\<lambda> k. Eq (CN 0 1 (Mul (k div c) e)))"
  2285 | "a_\<beta> (NEq (CN 0 c e)) = (\<lambda> k. NEq (CN 0 1 (Mul (k div c) e)))"
  2286 | "a_\<beta> (Lt  (CN 0 c e)) = (\<lambda> k. Lt (CN 0 1 (Mul (k div c) e)))"
  2287 | "a_\<beta> (Le  (CN 0 c e)) = (\<lambda> k. Le (CN 0 1 (Mul (k div c) e)))"
  2288 | "a_\<beta> (Gt  (CN 0 c e)) = (\<lambda> k. Gt (CN 0 1 (Mul (k div c) e)))"
  2289 | "a_\<beta> (Ge  (CN 0 c e)) = (\<lambda> k. Ge (CN 0 1 (Mul (k div c) e)))"
  2290 | "a_\<beta> (Dvd i (CN 0 c e)) =(\<lambda> k. Dvd ((k div c)*i) (CN 0 1 (Mul (k div c) e)))"
  2291 | "a_\<beta> (NDvd i (CN 0 c e))=(\<lambda> k. NDvd ((k div c)*i) (CN 0 1 (Mul (k div c) e)))"
  2292 | "a_\<beta> p = (\<lambda> k. p)"
  2293 
  2294 fun d_\<beta> :: "fm \<Rightarrow> int \<Rightarrow> bool" (* tests if all coeffs c of c divide a given l*)
  2295 where
  2296   "d_\<beta> (And p q) = (\<lambda> k. (d_\<beta> p k) \<and> (d_\<beta> q k))"
  2297 | "d_\<beta> (Or p q) = (\<lambda> k. (d_\<beta> p k) \<and> (d_\<beta> q k))"
  2298 | "d_\<beta> (Eq  (CN 0 c e)) = (\<lambda> k. c dvd k)"
  2299 | "d_\<beta> (NEq (CN 0 c e)) = (\<lambda> k. c dvd k)"
  2300 | "d_\<beta> (Lt  (CN 0 c e)) = (\<lambda> k. c dvd k)"
  2301 | "d_\<beta> (Le  (CN 0 c e)) = (\<lambda> k. c dvd k)"
  2302 | "d_\<beta> (Gt  (CN 0 c e)) = (\<lambda> k. c dvd k)"
  2303 | "d_\<beta> (Ge  (CN 0 c e)) = (\<lambda> k. c dvd k)"
  2304 | "d_\<beta> (Dvd i (CN 0 c e)) =(\<lambda> k. c dvd k)"
  2305 | "d_\<beta> (NDvd i (CN 0 c e))=(\<lambda> k. c dvd k)"
  2306 | "d_\<beta> p = (\<lambda> k. True)"
  2307 
  2308 fun \<zeta>  :: "fm \<Rightarrow> int" (* computes the lcm of all coefficients of x*)
  2309 where
  2310   "\<zeta> (And p q) = lcm (\<zeta> p) (\<zeta> q)"
  2311 | "\<zeta> (Or p q) = lcm (\<zeta> p) (\<zeta> q)"
  2312 | "\<zeta> (Eq  (CN 0 c e)) = c"
  2313 | "\<zeta> (NEq (CN 0 c e)) = c"
  2314 | "\<zeta> (Lt  (CN 0 c e)) = c"
  2315 | "\<zeta> (Le  (CN 0 c e)) = c"
  2316 | "\<zeta> (Gt  (CN 0 c e)) = c"
  2317 | "\<zeta> (Ge  (CN 0 c e)) = c"
  2318 | "\<zeta> (Dvd i (CN 0 c e)) = c"
  2319 | "\<zeta> (NDvd i (CN 0 c e))= c"
  2320 | "\<zeta> p = 1"
  2321 
  2322 fun \<beta> :: "fm \<Rightarrow> num list"
  2323 where
  2324   "\<beta> (And p q) = (\<beta> p @ \<beta> q)"
  2325 | "\<beta> (Or p q) = (\<beta> p @ \<beta> q)"
  2326 | "\<beta> (Eq  (CN 0 c e)) = [Sub (C (- 1)) e]"
  2327 | "\<beta> (NEq (CN 0 c e)) = [Neg e]"
  2328 | "\<beta> (Lt  (CN 0 c e)) = []"
  2329 | "\<beta> (Le  (CN 0 c e)) = []"
  2330 | "\<beta> (Gt  (CN 0 c e)) = [Neg e]"
  2331 | "\<beta> (Ge  (CN 0 c e)) = [Sub (C (- 1)) e]"
  2332 | "\<beta> p = []"
  2333 
  2334 fun \<alpha> :: "fm \<Rightarrow> num list"
  2335 where
  2336   "\<alpha> (And p q) = (\<alpha> p @ \<alpha> q)"
  2337 | "\<alpha> (Or p q) = (\<alpha> p @ \<alpha> q)"
  2338 | "\<alpha> (Eq  (CN 0 c e)) = [Add (C (- 1)) e]"
  2339 | "\<alpha> (NEq (CN 0 c e)) = [e]"
  2340 | "\<alpha> (Lt  (CN 0 c e)) = [e]"
  2341 | "\<alpha> (Le  (CN 0 c e)) = [Add (C (- 1)) e]"
  2342 | "\<alpha> (Gt  (CN 0 c e)) = []"
  2343 | "\<alpha> (Ge  (CN 0 c e)) = []"
  2344 | "\<alpha> p = []"
  2345 
  2346 fun mirror :: "fm \<Rightarrow> fm"
  2347 where
  2348   "mirror (And p q) = And (mirror p) (mirror q)"
  2349 | "mirror (Or p q) = Or (mirror p) (mirror q)"
  2350 | "mirror (Eq  (CN 0 c e)) = Eq (CN 0 c (Neg e))"
  2351 | "mirror (NEq (CN 0 c e)) = NEq (CN 0 c (Neg e))"
  2352 | "mirror (Lt  (CN 0 c e)) = Gt (CN 0 c (Neg e))"
  2353 | "mirror (Le  (CN 0 c e)) = Ge (CN 0 c (Neg e))"
  2354 | "mirror (Gt  (CN 0 c e)) = Lt (CN 0 c (Neg e))"
  2355 | "mirror (Ge  (CN 0 c e)) = Le (CN 0 c (Neg e))"
  2356 | "mirror (Dvd i (CN 0 c e)) = Dvd i (CN 0 c (Neg e))"
  2357 | "mirror (NDvd i (CN 0 c e)) = NDvd i (CN 0 c (Neg e))"
  2358 | "mirror p = p"
  2359 
  2360 lemma mirror_\<alpha>_\<beta>:
  2361   assumes lp: "iszlfm p (a#bs)"
  2362   shows "(Inum (real_of_int (i::int)#bs)) ` set (\<alpha> p) = (Inum (real_of_int i#bs)) ` set (\<beta> (mirror p))"
  2363   using lp by (induct p rule: mirror.induct) auto
  2364 
  2365 lemma mirror:
  2366   assumes lp: "iszlfm p (a#bs)"
  2367   shows "Ifm (real_of_int (x::int)#bs) (mirror p) = Ifm (real_of_int (- x)#bs) p"
  2368   using lp
  2369 proof(induct p rule: iszlfm.induct)
  2370   case (9 j c e)
  2371   have th: "(real_of_int j rdvd real_of_int c * real_of_int x - Inum (real_of_int x # bs) e) =
  2372        (real_of_int j rdvd - (real_of_int c * real_of_int x - Inum (real_of_int x # bs) e))"
  2373     by (simp only: rdvd_minus[symmetric])
  2374   from 9 th show ?case
  2375     by (simp add: algebra_simps
  2376       numbound0_I[where bs="bs" and b'="real_of_int x" and b="- real_of_int x"])
  2377 next
  2378   case (10 j c e)
  2379   have th: "(real_of_int j rdvd real_of_int c * real_of_int x - Inum (real_of_int x # bs) e) =
  2380        (real_of_int j rdvd - (real_of_int c * real_of_int x - Inum (real_of_int x # bs) e))"
  2381     by (simp only: rdvd_minus[symmetric])
  2382   from 10 th show  ?case
  2383     by (simp add: algebra_simps
  2384       numbound0_I[where bs="bs" and b'="real_of_int x" and b="- real_of_int x"])
  2385 qed (auto simp add: numbound0_I[where bs="bs" and b="real_of_int x" and b'="- real_of_int x"])
  2386 
  2387 lemma mirror_l: "iszlfm p (a#bs) \<Longrightarrow> iszlfm (mirror p) (a#bs)"
  2388   by (induct p rule: mirror.induct) (auto simp add: isint_neg)
  2389 
  2390 lemma mirror_d_\<beta>: "iszlfm p (a#bs) \<and> d_\<beta> p 1
  2391   \<Longrightarrow> iszlfm (mirror p) (a#bs) \<and> d_\<beta> (mirror p) 1"
  2392   by (induct p rule: mirror.induct) (auto simp add: isint_neg)
  2393 
  2394 lemma mirror_\<delta>: "iszlfm p (a#bs) \<Longrightarrow> \<delta> (mirror p) = \<delta> p"
  2395   by (induct p rule: mirror.induct) auto
  2396 
  2397 
  2398 lemma mirror_ex:
  2399   assumes lp: "iszlfm p (real_of_int (i::int)#bs)"
  2400   shows "(\<exists> (x::int). Ifm (real_of_int x#bs) (mirror p)) = (\<exists> (x::int). Ifm (real_of_int x#bs) p)"
  2401   (is "(\<exists> x. ?I x ?mp) = (\<exists> x. ?I x p)")
  2402 proof(auto)
  2403   fix x assume "?I x ?mp" hence "?I (- x) p" using mirror[OF lp] by blast
  2404   thus "\<exists> x. ?I x p" by blast
  2405 next
  2406   fix x assume "?I x p" hence "?I (- x) ?mp"
  2407     using mirror[OF lp, where x="- x", symmetric] by auto
  2408   thus "\<exists> x. ?I x ?mp" by blast
  2409 qed
  2410 
  2411 lemma \<beta>_numbound0: assumes lp: "iszlfm p bs"
  2412   shows "\<forall> b\<in> set (\<beta> p). numbound0 b"
  2413   using lp by (induct p rule: \<beta>.induct,auto)
  2414 
  2415 lemma d_\<beta>_mono:
  2416   assumes linp: "iszlfm p (a #bs)"
  2417   and dr: "d_\<beta> p l"
  2418   and d: "l dvd l'"
  2419   shows "d_\<beta> p l'"
  2420 using dr linp dvd_trans[of _ "l" "l'", simplified d]
  2421 by (induct p rule: iszlfm.induct) simp_all
  2422 
  2423 lemma \<alpha>_l: assumes lp: "iszlfm p (a#bs)"
  2424   shows "\<forall> b\<in> set (\<alpha> p). numbound0 b \<and> isint b (a#bs)"
  2425 using lp
  2426 by(induct p rule: \<alpha>.induct, auto simp add: isint_add isint_c)
  2427 
  2428 lemma \<zeta>:
  2429   assumes linp: "iszlfm p (a #bs)"
  2430   shows "\<zeta> p > 0 \<and> d_\<beta> p (\<zeta> p)"
  2431 using linp
  2432 proof(induct p rule: iszlfm.induct)
  2433   case (1 p q)
  2434   then  have dl1: "\<zeta> p dvd lcm (\<zeta> p) (\<zeta> q)" by simp
  2435   from 1 have dl2: "\<zeta> q dvd lcm (\<zeta> p) (\<zeta> q)" by simp
  2436   from 1 d_\<beta>_mono[where p = "p" and l="\<zeta> p" and l'="lcm (\<zeta> p) (\<zeta> q)"]
  2437     d_\<beta>_mono[where p = "q" and l="\<zeta> q" and l'="lcm (\<zeta> p) (\<zeta> q)"]
  2438     dl1 dl2 show ?case by (auto simp add: lcm_pos_int)
  2439 next
  2440   case (2 p q)
  2441   then have dl1: "\<zeta> p dvd lcm (\<zeta> p) (\<zeta> q)" by simp
  2442   from 2 have dl2: "\<zeta> q dvd lcm (\<zeta> p) (\<zeta> q)" by simp
  2443   from 2 d_\<beta>_mono[where p = "p" and l="\<zeta> p" and l'="lcm (\<zeta> p) (\<zeta> q)"]
  2444     d_\<beta>_mono[where p = "q" and l="\<zeta> q" and l'="lcm (\<zeta> p) (\<zeta> q)"]
  2445     dl1 dl2 show ?case by (auto simp add: lcm_pos_int)
  2446 qed (auto simp add: lcm_pos_int)
  2447 
  2448 lemma a_\<beta>: assumes linp: "iszlfm p (a #bs)" and d: "d_\<beta> p l" and lp: "l > 0"
  2449   shows "iszlfm (a_\<beta> p l) (a #bs) \<and> d_\<beta> (a_\<beta> p l) 1 \<and> (Ifm (real_of_int (l * x) #bs) (a_\<beta> p l) = Ifm ((real_of_int x)#bs) p)"
  2450 using linp d
  2451 proof (induct p rule: iszlfm.induct)
  2452   case (5 c e) hence cp: "c>0" and be: "numbound0 e" and ei:"isint e (a#bs)" and d': "c dvd l" by simp+
  2453     from lp cp have clel: "c\<le>l" by (simp add: zdvd_imp_le [OF d' lp])
  2454     from cp have cnz: "c \<noteq> 0" by simp
  2455     have "c div c\<le> l div c"
  2456       by (simp add: zdiv_mono1[OF clel cp])
  2457     then have ldcp:"0 < l div c"
  2458       by (simp add: div_self[OF cnz])
  2459     have "c * (l div c) = c* (l div c) + l mod c" using d' dvd_eq_mod_eq_0[of "c" "l"] by simp
  2460     hence cl:"c * (l div c) =l" using mult_div_mod_eq [where a="l" and b="c"]
  2461       by simp
  2462     hence "(real_of_int l * real_of_int x + real_of_int (l div c) * Inum (real_of_int x # bs) e < (0::real)) =
  2463           (real_of_int (c * (l div c)) * real_of_int x + real_of_int (l div c) * Inum (real_of_int x # bs) e < 0)"
  2464       by simp
  2465     also have "\<dots> = (real_of_int (l div c) * (real_of_int c * real_of_int x + Inum (real_of_int x # bs) e) < (real_of_int (l div c)) * 0)" by (simp add: algebra_simps)
  2466     also have "\<dots> = (real_of_int c * real_of_int x + Inum (real_of_int x # bs) e < 0)"
  2467     using mult_less_0_iff [where a="real_of_int (l div c)" and b="real_of_int c * real_of_int x + Inum (real_of_int x # bs) e"] ldcp by simp
  2468   finally show ?case using numbound0_I[OF be,where b="real_of_int (l * x)" and b'="real_of_int x" and bs="bs"] be  isint_Mul[OF ei] by simp
  2469 next
  2470   case (6 c e) hence cp: "c>0" and be: "numbound0 e" and ei:"isint e (a#bs)" and d': "c dvd l" by simp+
  2471     from lp cp have clel: "c\<le>l" by (simp add: zdvd_imp_le [OF d' lp])
  2472     from cp have cnz: "c \<noteq> 0" by simp
  2473     have "c div c\<le> l div c"
  2474       by (simp add: zdiv_mono1[OF clel cp])
  2475     then have ldcp:"0 < l div c"
  2476       by (simp add: div_self[OF cnz])
  2477     have "c * (l div c) = c* (l div c) + l mod c" using d' dvd_eq_mod_eq_0[of "c" "l"] by simp
  2478     hence cl:"c * (l div c) =l" using mult_div_mod_eq [where a="l" and b="c"]
  2479       by simp
  2480     hence "(real_of_int l * real_of_int x + real_of_int (l div c) * Inum (real_of_int x # bs) e \<le> (0::real)) =
  2481           (real_of_int (c * (l div c)) * real_of_int x + real_of_int (l div c) * Inum (real_of_int x # bs) e \<le> 0)"
  2482       by simp
  2483     also have "\<dots> = (real_of_int (l div c) * (real_of_int c * real_of_int x + Inum (real_of_int x # bs) e) \<le> (real_of_int (l div c)) * 0)" by (simp add: algebra_simps)
  2484     also have "\<dots> = (real_of_int c * real_of_int x + Inum (real_of_int x # bs) e \<le> 0)"
  2485     using mult_le_0_iff [where a="real_of_int (l div c)" and b="real_of_int c * real_of_int x + Inum (real_of_int x # bs) e"] ldcp by simp
  2486   finally show ?case using numbound0_I[OF be,where b="real_of_int (l * x)" and b'="real_of_int x" and bs="bs"]  be  isint_Mul[OF ei] by simp
  2487 next
  2488   case (7 c e) hence cp: "c>0" and be: "numbound0 e" and ei:"isint e (a#bs)" and d': "c dvd l" by simp+
  2489     from lp cp have clel: "c\<le>l" by (simp add: zdvd_imp_le [OF d' lp])
  2490     from cp have cnz: "c \<noteq> 0" by simp
  2491     have "c div c\<le> l div c"
  2492       by (simp add: zdiv_mono1[OF clel cp])
  2493     then have ldcp:"0 < l div c"
  2494       by (simp add: div_self[OF cnz])
  2495     have "c * (l div c) = c* (l div c) + l mod c" using d' dvd_eq_mod_eq_0[of "c" "l"] by simp
  2496     hence cl:"c * (l div c) =l" using mult_div_mod_eq [where a="l" and b="c"]
  2497       by simp
  2498     hence "(real_of_int l * real_of_int x + real_of_int (l div c) * Inum (real_of_int x # bs) e > (0::real)) =
  2499           (real_of_int (c * (l div c)) * real_of_int x + real_of_int (l div c) * Inum (real_of_int x # bs) e > 0)"
  2500       by simp
  2501     also have "\<dots> = (real_of_int (l div c) * (real_of_int c * real_of_int x + Inum (real_of_int x # bs) e) > (real_of_int (l div c)) * 0)" by (simp add: algebra_simps)
  2502     also have "\<dots> = (real_of_int c * real_of_int x + Inum (real_of_int x # bs) e > 0)"
  2503     using zero_less_mult_iff [where a="real_of_int (l div c)" and b="real_of_int c * real_of_int x + Inum (real_of_int x # bs) e"] ldcp by simp
  2504   finally show ?case using numbound0_I[OF be,where b="real_of_int (l * x)" and b'="real_of_int x" and bs="bs"]  be  isint_Mul[OF ei] by simp
  2505 next
  2506   case (8 c e) hence cp: "c>0" and be: "numbound0 e"  and ei:"isint e (a#bs)" and d': "c dvd l" by simp+
  2507     from lp cp have clel: "c\<le>l" by (simp add: zdvd_imp_le [OF d' lp])
  2508     from cp have cnz: "c \<noteq> 0" by simp
  2509     have "c div c\<le> l div c"
  2510       by (simp add: zdiv_mono1[OF clel cp])
  2511     then have ldcp:"0 < l div c"
  2512       by (simp add: div_self[OF cnz])
  2513     have "c * (l div c) = c* (l div c) + l mod c" using d' dvd_eq_mod_eq_0[of "c" "l"] by simp
  2514     hence cl:"c * (l div c) =l" using mult_div_mod_eq [where a="l" and b="c"]
  2515       by simp
  2516     hence "(real_of_int l * real_of_int x + real_of_int (l div c) * Inum (real_of_int x # bs) e \<ge> (0::real)) =
  2517           (real_of_int (c * (l div c)) * real_of_int x + real_of_int (l div c) * Inum (real_of_int x # bs) e \<ge> 0)"
  2518       by simp
  2519     also have "\<dots> = (real_of_int (l div c) * (real_of_int c * real_of_int x + Inum (real_of_int x # bs) e) \<ge> (real_of_int (l div c)) * 0)" by (simp add: algebra_simps)
  2520     also have "\<dots> = (real_of_int c * real_of_int x + Inum (real_of_int x # bs) e \<ge> 0)"
  2521     using zero_le_mult_iff [where a="real_of_int (l div c)" and b="real_of_int c * real_of_int x + Inum (real_of_int x # bs) e"] ldcp by simp
  2522   finally show ?case using numbound0_I[OF be,where b="real_of_int (l * x)" and b'="real_of_int x" and bs="bs"]  be  isint_Mul[OF ei] by simp
  2523 next
  2524   case (3 c e) hence cp: "c>0" and be: "numbound0 e" and ei:"isint e (a#bs)" and d': "c dvd l" by simp+
  2525     from lp cp have clel: "c\<le>l" by (simp add: zdvd_imp_le [OF d' lp])
  2526     from cp have cnz: "c \<noteq> 0" by simp
  2527     have "c div c\<le> l div c"
  2528       by (simp add: zdiv_mono1[OF clel cp])
  2529     then have ldcp:"0 < l div c"
  2530       by (simp add: div_self[OF cnz])
  2531     have "c * (l div c) = c* (l div c) + l mod c" using d' dvd_eq_mod_eq_0[of "c" "l"] by simp
  2532     hence cl:"c * (l div c) =l" using mult_div_mod_eq [where a="l" and b="c"]
  2533       by simp
  2534     hence "(real_of_int l * real_of_int x + real_of_int (l div c) * Inum (real_of_int x # bs) e = (0::real)) =
  2535           (real_of_int (c * (l div c)) * real_of_int x + real_of_int (l div c) * Inum (real_of_int x # bs) e = 0)"
  2536       by simp
  2537     also have "\<dots> = (real_of_int (l div c) * (real_of_int c * real_of_int x + Inum (real_of_int x # bs) e) = (real_of_int (l div c)) * 0)" by (simp add: algebra_simps)
  2538     also have "\<dots> = (real_of_int c * real_of_int x + Inum (real_of_int x # bs) e = 0)"
  2539     using mult_eq_0_iff [where a="real_of_int (l div c)" and b="real_of_int c * real_of_int x + Inum (real_of_int x # bs) e"] ldcp by simp
  2540   finally show ?case using numbound0_I[OF be,where b="real_of_int (l * x)" and b'="real_of_int x" and bs="bs"]  be  isint_Mul[OF ei] by simp
  2541 next
  2542   case (4 c e) hence cp: "c>0" and be: "numbound0 e" and ei:"isint e (a#bs)" and d': "c dvd l" by simp+
  2543     from lp cp have clel: "c\<le>l" by (simp add: zdvd_imp_le [OF d' lp])
  2544     from cp have cnz: "c \<noteq> 0" by simp
  2545     have "c div c\<le> l div c"
  2546       by (simp add: zdiv_mono1[OF clel cp])
  2547     then have ldcp:"0 < l div c"
  2548       by (simp add: div_self[OF cnz])
  2549     have "c * (l div c) = c* (l div c) + l mod c" using d' dvd_eq_mod_eq_0[of "c" "l"] by simp
  2550     hence cl:"c * (l div c) =l" using mult_div_mod_eq [where a="l" and b="c"]
  2551       by simp
  2552     hence "(real_of_int l * real_of_int x + real_of_int (l div c) * Inum (real_of_int x # bs) e \<noteq> (0::real)) =
  2553           (real_of_int (c * (l div c)) * real_of_int x + real_of_int (l div c) * Inum (real_of_int x # bs) e \<noteq> 0)"
  2554       by simp
  2555     also have "\<dots> = (real_of_int (l div c) * (real_of_int c * real_of_int x + Inum (real_of_int x # bs) e) \<noteq> (real_of_int (l div c)) * 0)" by (simp add: algebra_simps)
  2556     also have "\<dots> = (real_of_int c * real_of_int x + Inum (real_of_int x # bs) e \<noteq> 0)"
  2557     using zero_le_mult_iff [where a="real_of_int (l div c)" and b="real_of_int c * real_of_int x + Inum (real_of_int x # bs) e"] ldcp by simp
  2558   finally show ?case using numbound0_I[OF be,where b="real_of_int (l * x)" and b'="real_of_int x" and bs="bs"]  be  isint_Mul[OF ei] by simp
  2559 next
  2560   case (9 j c e) hence cp: "c>0" and be: "numbound0 e" and ei:"isint e (a#bs)" and jp: "j > 0" and d': "c dvd l" by simp+
  2561     from lp cp have clel: "c\<le>l" by (simp add: zdvd_imp_le [OF d' lp])
  2562     from cp have cnz: "c \<noteq> 0" by simp
  2563     have "c div c\<le> l div c"
  2564       by (simp add: zdiv_mono1[OF clel cp])
  2565     then have ldcp:"0 < l div c"
  2566       by (simp add: div_self[OF cnz])
  2567     have "c * (l div c) = c* (l div c) + l mod c" using d' dvd_eq_mod_eq_0[of "c" "l"] by simp
  2568     hence cl:"c * (l div c) =l" using mult_div_mod_eq [where a="l" and b="c"]
  2569       by simp
  2570     hence "(\<exists> (k::int). real_of_int l * real_of_int x + real_of_int (l div c) * Inum (real_of_int x # bs) e = (real_of_int (l div c) * real_of_int j) * real_of_int k) = (\<exists> (k::int). real_of_int (c * (l div c)) * real_of_int x + real_of_int (l div c) * Inum (real_of_int x # bs) e = (real_of_int (l div c) * real_of_int j) * real_of_int k)"  by simp
  2571     also have "\<dots> = (\<exists> (k::int). real_of_int (l div c) * (real_of_int c * real_of_int x + Inum (real_of_int x # bs) e - real_of_int j * real_of_int k) = real_of_int (l div c)*0)" by (simp add: algebra_simps)
  2572     also fix k have "\<dots> = (\<exists> (k::int). real_of_int c * real_of_int x + Inum (real_of_int x # bs) e - real_of_int j * real_of_int k = 0)"
  2573     using zero_le_mult_iff [where a="real_of_int (l div c)" and b="real_of_int c * real_of_int x + Inum (real_of_int x # bs) e - real_of_int j * real_of_int k"] ldcp by simp
  2574   also have "\<dots> = (\<exists> (k::int). real_of_int c * real_of_int x + Inum (real_of_int x # bs) e = real_of_int j * real_of_int k)" by simp
  2575   finally show ?case using numbound0_I[OF be,where b="real_of_int (l * x)" and b'="real_of_int x" and bs="bs"] rdvd_def  be  isint_Mul[OF ei] mult_strict_mono[OF ldcp jp ldcp ] by simp
  2576 next
  2577   case (10 j c e) hence cp: "c>0" and be: "numbound0 e" and ei:"isint e (a#bs)" and jp: "j > 0" and d': "c dvd l" by simp+
  2578     from lp cp have clel: "c\<le>l" by (simp add: zdvd_imp_le [OF d' lp])
  2579     from cp have cnz: "c \<noteq> 0" by simp
  2580     have "c div c\<le> l div c"
  2581       by (simp add: zdiv_mono1[OF clel cp])
  2582     then have ldcp:"0 < l div c"
  2583       by (simp add: div_self[OF cnz])
  2584     have "c * (l div c) = c* (l div c) + l mod c" using d' dvd_eq_mod_eq_0[of "c" "l"] by simp
  2585     hence cl:"c * (l div c) =l" using mult_div_mod_eq [where a="l" and b="c"]
  2586       by simp
  2587     hence "(\<exists> (k::int). real_of_int l * real_of_int x + real_of_int (l div c) * Inum (real_of_int x # bs) e = (real_of_int (l div c) * real_of_int j) * real_of_int k) = (\<exists> (k::int). real_of_int (c * (l div c)) * real_of_int x + real_of_int (l div c) * Inum (real_of_int x # bs) e = (real_of_int (l div c) * real_of_int j) * real_of_int k)"  by simp
  2588     also have "\<dots> = (\<exists> (k::int). real_of_int (l div c) * (real_of_int c * real_of_int x + Inum (real_of_int x # bs) e - real_of_int j * real_of_int k) = real_of_int (l div c)*0)" by (simp add: algebra_simps)
  2589     also fix k have "\<dots> = (\<exists> (k::int). real_of_int c * real_of_int x + Inum (real_of_int x # bs) e - real_of_int j * real_of_int k = 0)"
  2590     using zero_le_mult_iff [where a="real_of_int (l div c)" and b="real_of_int c * real_of_int x + Inum (real_of_int x # bs) e - real_of_int j * real_of_int k"] ldcp by simp
  2591   also have "\<dots> = (\<exists> (k::int). real_of_int c * real_of_int x + Inum (real_of_int x # bs) e = real_of_int j * real_of_int k)" by simp
  2592   finally show ?case using numbound0_I[OF be,where b="real_of_int (l * x)" and b'="real_of_int x" and bs="bs"] rdvd_def  be  isint_Mul[OF ei]  mult_strict_mono[OF ldcp jp ldcp ] by simp
  2593 qed (simp_all add: numbound0_I[where bs="bs" and b="real_of_int (l * x)" and b'="real_of_int x"] isint_Mul del: of_int_mult)
  2594 
  2595 lemma a_\<beta>_ex: assumes linp: "iszlfm p (a#bs)" and d: "d_\<beta> p l" and lp: "l>0"
  2596   shows "(\<exists> x. l dvd x \<and> Ifm (real_of_int x #bs) (a_\<beta> p l)) = (\<exists> (x::int). Ifm (real_of_int x#bs) p)"
  2597   (is "(\<exists> x. l dvd x \<and> ?P x) = (\<exists> x. ?P' x)")
  2598 proof-
  2599   have "(\<exists> x. l dvd x \<and> ?P x) = (\<exists> (x::int). ?P (l*x))"
  2600     using unity_coeff_ex[where l="l" and P="?P", simplified] by simp
  2601   also have "\<dots> = (\<exists> (x::int). ?P' x)" using a_\<beta>[OF linp d lp] by simp
  2602   finally show ?thesis  .
  2603 qed
  2604 
  2605 lemma \<beta>:
  2606   assumes lp: "iszlfm p (a#bs)"
  2607   and u: "d_\<beta> p 1"
  2608   and d: "d_\<delta> p d"
  2609   and dp: "d > 0"
  2610   and nob: "\<not>(\<exists>(j::int) \<in> {1 .. d}. \<exists> b\<in> (Inum (a#bs)) ` set(\<beta> p). real_of_int x = b + real_of_int j)"
  2611   and p: "Ifm (real_of_int x#bs) p" (is "?P x")
  2612   shows "?P (x - d)"
  2613 using lp u d dp nob p
  2614 proof(induct p rule: iszlfm.induct)
  2615   case (5 c e) hence c1: "c=1" and  bn:"numbound0 e" using dvd1_eq1[where x="c"] by simp_all
  2616   with dp p c1 numbound0_I[OF bn,where b="real_of_int (x-d)" and b'="real_of_int x" and bs="bs"] 5
  2617   show ?case by (simp del: of_int_minus)
  2618 next
  2619   case (6 c e)  hence c1: "c=1" and  bn:"numbound0 e" using dvd1_eq1[where x="c"] by simp_all
  2620   with dp p c1 numbound0_I[OF bn,where b="real_of_int (x-d)" and b'="real_of_int x" and bs="bs"] 6
  2621   show ?case by (simp del: of_int_minus)
  2622 next
  2623   case (7 c e) hence p: "Ifm (real_of_int x #bs) (Gt (CN 0 c e))" and c1: "c=1"
  2624     and bn:"numbound0 e" and ie1:"isint e (a#bs)" using dvd1_eq1[where x="c"] by simp_all
  2625   let ?e = "Inum (real_of_int x # bs) e"
  2626   from ie1 have ie: "real_of_int \<lfloor>?e\<rfloor> = ?e" using isint_iff[where n="e" and bs="a#bs"]
  2627       numbound0_I[OF bn,where b="a" and b'="real_of_int x" and bs="bs"]
  2628     by (simp add: isint_iff)
  2629     {assume "real_of_int (x-d) +?e > 0" hence ?case using c1
  2630       numbound0_I[OF bn,where b="real_of_int (x-d)" and b'="real_of_int x" and bs="bs"]
  2631         by (simp del: of_int_minus)}
  2632     moreover
  2633     {assume H: "\<not> real_of_int (x-d) + ?e > 0"
  2634       let ?v="Neg e"
  2635       have vb: "?v \<in> set (\<beta> (Gt (CN 0 c e)))" by simp
  2636       from 7(5)[simplified simp_thms Inum.simps \<beta>.simps list.set bex_simps numbound0_I[OF bn,where b="a" and b'="real_of_int x" and bs="bs"]]
  2637       have nob: "\<not> (\<exists> j\<in> {1 ..d}. real_of_int x =  - ?e + real_of_int j)" by auto
  2638       from H p have "real_of_int x + ?e > 0 \<and> real_of_int x + ?e \<le> real_of_int d" by (simp add: c1)
  2639       hence "real_of_int (x + \<lfloor>?e\<rfloor>) > real_of_int (0::int) \<and> real_of_int (x + \<lfloor>?e\<rfloor>) \<le> real_of_int d"
  2640         using ie by simp
  2641       hence "x + \<lfloor>?e\<rfloor> \<ge> 1 \<and> x + \<lfloor>?e\<rfloor> \<le> d"  by simp
  2642       hence "\<exists> (j::int) \<in> {1 .. d}. j = x + \<lfloor>?e\<rfloor>" by simp
  2643       hence "\<exists> (j::int) \<in> {1 .. d}. real_of_int x = real_of_int (- \<lfloor>?e\<rfloor> + j)" by force
  2644       hence "\<exists> (j::int) \<in> {1 .. d}. real_of_int x = - ?e + real_of_int j"
  2645         by (simp add: ie[simplified isint_iff])
  2646       with nob have ?case by auto}
  2647     ultimately show ?case by blast
  2648 next
  2649   case (8 c e) hence p: "Ifm (real_of_int x #bs) (Ge (CN 0 c e))" and c1: "c=1" and bn:"numbound0 e"
  2650     and ie1:"isint e (a #bs)" using dvd1_eq1[where x="c"] by simp+
  2651     let ?e = "Inum (real_of_int x # bs) e"
  2652     from ie1 have ie: "real_of_int \<lfloor>?e\<rfloor> = ?e" using numbound0_I[OF bn,where b="real_of_int x" and b'="a" and bs="bs"] isint_iff[where n="e" and bs="(real_of_int x)#bs"]
  2653       by (simp add: isint_iff)
  2654     {assume "real_of_int (x-d) +?e \<ge> 0" hence ?case using  c1
  2655       numbound0_I[OF bn,where b="real_of_int (x-d)" and b'="real_of_int x" and bs="bs"]
  2656         by (simp del: of_int_minus)}
  2657     moreover
  2658     {assume H: "\<not> real_of_int (x-d) + ?e \<ge> 0"
  2659       let ?v="Sub (C (- 1)) e"
  2660       have vb: "?v \<in> set (\<beta> (Ge (CN 0 c e)))" by simp
  2661       from 8(5)[simplified simp_thms Inum.simps \<beta>.simps list.set bex_simps numbound0_I[OF bn,where b="a" and b'="real_of_int x" and bs="bs"]]
  2662       have nob: "\<not> (\<exists> j\<in> {1 ..d}. real_of_int x =  - ?e - 1 + real_of_int j)" by auto
  2663       from H p have "real_of_int x + ?e \<ge> 0 \<and> real_of_int x + ?e < real_of_int d" by (simp add: c1)
  2664       hence "real_of_int (x + \<lfloor>?e\<rfloor>) \<ge> real_of_int (0::int) \<and> real_of_int (x + \<lfloor>?e\<rfloor>) < real_of_int d"
  2665         using ie by simp
  2666       hence "x + \<lfloor>?e\<rfloor> + 1 \<ge> 1 \<and> x + \<lfloor>?e\<rfloor> + 1 \<le> d" by simp
  2667       hence "\<exists> (j::int) \<in> {1 .. d}. j = x + \<lfloor>?e\<rfloor> + 1" by simp
  2668       hence "\<exists> (j::int) \<in> {1 .. d}. x= - \<lfloor>?e\<rfloor> - 1 + j" by (simp add: algebra_simps)
  2669       hence "\<exists> (j::int) \<in> {1 .. d}. real_of_int x= real_of_int (- \<lfloor>?e\<rfloor> - 1 + j)" by presburger
  2670       hence "\<exists> (j::int) \<in> {1 .. d}. real_of_int x= - ?e - 1 + real_of_int j"
  2671         by (simp add: ie[simplified isint_iff])
  2672       with nob have ?case by simp }
  2673     ultimately show ?case by blast
  2674 next
  2675   case (3 c e) hence p: "Ifm (real_of_int x #bs) (Eq (CN 0 c e))" (is "?p x") and c1: "c=1"
  2676     and bn:"numbound0 e" and ie1: "isint e (a #bs)" using dvd1_eq1[where x="c"] by simp+
  2677     let ?e = "Inum (real_of_int x # bs) e"
  2678     let ?v="(Sub (C (- 1)) e)"
  2679     have vb: "?v \<in> set (\<beta> (Eq (CN 0 c e)))" by simp
  2680     from p have "real_of_int x= - ?e" by (simp add: c1) with 3(5) show ?case using dp
  2681       by simp (erule ballE[where x="1"],
  2682         simp_all add:algebra_simps numbound0_I[OF bn,where b="real_of_int x"and b'="a"and bs="bs"])
  2683 next
  2684   case (4 c e)hence p: "Ifm (real_of_int x #bs) (NEq (CN 0 c e))" (is "?p x") and c1: "c=1"
  2685     and bn:"numbound0 e" and ie1: "isint e (a #bs)" using dvd1_eq1[where x="c"] by simp+
  2686     let ?e = "Inum (real_of_int x # bs) e"
  2687     let ?v="Neg e"
  2688     have vb: "?v \<in> set (\<beta> (NEq (CN 0 c e)))" by simp
  2689     {assume "real_of_int x - real_of_int d + Inum ((real_of_int (x -d)) # bs) e \<noteq> 0"
  2690       hence ?case by (simp add: c1)}
  2691     moreover
  2692     {assume H: "real_of_int x - real_of_int d + Inum ((real_of_int (x -d)) # bs) e = 0"
  2693       hence "real_of_int x = - Inum ((real_of_int (x -d)) # bs) e + real_of_int d" by simp
  2694       hence "real_of_int x = - Inum (a # bs) e + real_of_int d"
  2695         by (simp add: numbound0_I[OF bn,where b="real_of_int x - real_of_int d"and b'="a"and bs="bs"])
  2696        with 4(5) have ?case using dp by simp}
  2697   ultimately show ?case by blast
  2698 next
  2699   case (9 j c e) hence p: "Ifm (real_of_int x #bs) (Dvd j (CN 0 c e))" (is "?p x") and c1: "c=1"
  2700     and bn:"numbound0 e" using dvd1_eq1[where x="c"] by simp+
  2701   let ?e = "Inum (real_of_int x # bs) e"
  2702   from 9 have "isint e (a #bs)"  by simp
  2703   hence ie: "real_of_int \<lfloor>?e\<rfloor> = ?e" using isint_iff[where n="e" and bs="(real_of_int x)#bs"] numbound0_I[OF bn,where b="real_of_int x" and b'="a" and bs="bs"]
  2704     by (simp add: isint_iff)
  2705   from 9 have id: "j dvd d" by simp
  2706   from c1 ie[symmetric] have "?p x = (real_of_int j rdvd real_of_int (x + \<lfloor>?e\<rfloor>))" by simp
  2707   also have "\<dots> = (j dvd x + \<lfloor>?e\<rfloor>)"
  2708     using int_rdvd_real[where i="j" and x="real_of_int (x + \<lfloor>?e\<rfloor>)"] by simp
  2709   also have "\<dots> = (j dvd x - d + \<lfloor>?e\<rfloor>)"
  2710     using dvd_period[OF id, where x="x" and c="-1" and t="\<lfloor>?e\<rfloor>"] by simp
  2711   also have "\<dots> = (real_of_int j rdvd real_of_int (x - d + \<lfloor>?e\<rfloor>))"
  2712     using int_rdvd_real[where i="j" and x="real_of_int (x - d + \<lfloor>?e\<rfloor>)",symmetric, simplified]
  2713       ie by simp
  2714   also have "\<dots> = (real_of_int j rdvd real_of_int x - real_of_int d + ?e)"
  2715     using ie by simp
  2716   finally show ?case
  2717     using numbound0_I[OF bn,where b="real_of_int (x-d)" and b'="real_of_int x" and bs="bs"] c1 p by simp
  2718 next
  2719   case (10 j c e) hence p: "Ifm (real_of_int x #bs) (NDvd j (CN 0 c e))" (is "?p x") and c1: "c=1" and bn:"numbound0 e" using dvd1_eq1[where x="c"] by simp+
  2720   let ?e = "Inum (real_of_int x # bs) e"
  2721   from 10 have "isint e (a#bs)"  by simp
  2722   hence ie: "real_of_int \<lfloor>?e\<rfloor> = ?e" using numbound0_I[OF bn,where b="real_of_int x" and b'="a" and bs="bs"] isint_iff[where n="e" and bs="(real_of_int x)#bs"]
  2723     by (simp add: isint_iff)
  2724   from 10 have id: "j dvd d" by simp
  2725   from c1 ie[symmetric] have "?p x = (\<not> real_of_int j rdvd real_of_int (x + \<lfloor>?e\<rfloor>))" by simp
  2726   also have "\<dots> = (\<not> j dvd x + \<lfloor>?e\<rfloor>)"
  2727     using int_rdvd_real[where i="j" and x="real_of_int (x + \<lfloor>?e\<rfloor>)"] by simp
  2728   also have "\<dots> = (\<not> j dvd x - d + \<lfloor>?e\<rfloor>)"
  2729     using dvd_period[OF id, where x="x" and c="-1" and t="\<lfloor>?e\<rfloor>"] by simp
  2730   also have "\<dots> = (\<not> real_of_int j rdvd real_of_int (x - d + \<lfloor>?e\<rfloor>))"
  2731     using int_rdvd_real[where i="j" and x="real_of_int (x - d + \<lfloor>?e\<rfloor>)",symmetric, simplified]
  2732       ie by simp
  2733   also have "\<dots> = (\<not> real_of_int j rdvd real_of_int x - real_of_int d + ?e)"
  2734     using ie by simp
  2735   finally show ?case
  2736     using numbound0_I[OF bn,where b="real_of_int (x-d)" and b'="real_of_int x" and bs="bs"] c1 p by simp
  2737 qed (auto simp add: numbound0_I[where bs="bs" and b="real_of_int (x - d)" and b'="real_of_int x"]
  2738   simp del: of_int_diff)
  2739 
  2740 lemma \<beta>':
  2741   assumes lp: "iszlfm p (a #bs)"
  2742   and u: "d_\<beta> p 1"
  2743   and d: "d_\<delta> p d"
  2744   and dp: "d > 0"
  2745   shows "\<forall> x. \<not>(\<exists>(j::int) \<in> {1 .. d}. \<exists> b\<in> set(\<beta> p). Ifm ((Inum (a#bs) b + real_of_int j) #bs) p) \<longrightarrow> Ifm (real_of_int x#bs) p \<longrightarrow> Ifm (real_of_int (x - d)#bs) p" (is "\<forall> x. ?b \<longrightarrow> ?P x \<longrightarrow> ?P (x - d)")
  2746 proof(clarify)
  2747   fix x
  2748   assume nb:"?b" and px: "?P x"
  2749   hence nb2: "\<not>(\<exists>(j::int) \<in> {1 .. d}. \<exists> b\<in> (Inum (a#bs)) ` set(\<beta> p). real_of_int x = b + real_of_int j)"
  2750     by auto
  2751   from  \<beta>[OF lp u d dp nb2 px] show "?P (x -d )" .
  2752 qed
  2753 
  2754 lemma \<beta>_int: assumes lp: "iszlfm p bs"
  2755   shows "\<forall> b\<in> set (\<beta> p). isint b bs"
  2756 using lp by (induct p rule: iszlfm.induct) (auto simp add: isint_neg isint_sub)
  2757 
  2758 lemma cpmi_eq: "0 < D \<Longrightarrow> (\<exists>z::int. \<forall>x. x < z \<longrightarrow> (P x = P1 x))
  2759 \<Longrightarrow> \<forall>x. \<not>(\<exists>(j::int) \<in> {1..D}. \<exists>(b::int) \<in> B. P(b+j)) \<longrightarrow> P (x) \<longrightarrow> P (x - D)
  2760 \<Longrightarrow> (\<forall>(x::int). \<forall>(k::int). ((P1 x)= (P1 (x-k*D))))
  2761 \<Longrightarrow> (\<exists>(x::int). P(x)) = ((\<exists>(j::int) \<in> {1..D} . (P1(j))) | (\<exists>(j::int) \<in> {1..D}. \<exists>(b::int) \<in> B. P (b+j)))"
  2762 apply(rule iffI)
  2763 prefer 2
  2764 apply(drule minusinfinity)
  2765 apply assumption+
  2766 apply(fastforce)
  2767 apply clarsimp
  2768 apply(subgoal_tac "\<And>k. 0<=k \<Longrightarrow> \<forall>x. P x \<longrightarrow> P (x - k*D)")
  2769 apply(frule_tac x = x and z=z in decr_lemma)
  2770 apply(subgoal_tac "P1(x - (\<bar>x - z\<bar> + 1) * D)")
  2771 prefer 2
  2772 apply(subgoal_tac "0 <= (\<bar>x - z\<bar> + 1)")
  2773 prefer 2 apply arith
  2774  apply fastforce
  2775 apply(drule (1)  periodic_finite_ex)
  2776 apply blast
  2777 apply(blast dest:decr_mult_lemma)
  2778 done
  2779 
  2780 
  2781 theorem cp_thm:
  2782   assumes lp: "iszlfm p (a #bs)"
  2783   and u: "d_\<beta> p 1"
  2784   and d: "d_\<delta> p d"
  2785   and dp: "d > 0"
  2786   shows "(\<exists> (x::int). Ifm (real_of_int x #bs) p) = (\<exists> j\<in> {1.. d}. Ifm (real_of_int j #bs) (minusinf p) \<or> (\<exists> b \<in> set (\<beta> p). Ifm ((Inum (a#bs) b + real_of_int j) #bs) p))"
  2787   (is "(\<exists> (x::int). ?P (real_of_int x)) = (\<exists> j\<in> ?D. ?M j \<or> (\<exists> b\<in> ?B. ?P (?I b + real_of_int j)))")
  2788 proof-
  2789   from minusinf_inf[OF lp]
  2790   have th: "\<exists>(z::int). \<forall>x<z. ?P (real_of_int x) = ?M x" by blast
  2791   let ?B' = "{\<lfloor>?I b\<rfloor> | b. b\<in> ?B}"
  2792   from \<beta>_int[OF lp] isint_iff[where bs="a # bs"] have B: "\<forall> b\<in> ?B. real_of_int \<lfloor>?I b\<rfloor> = ?I b" by simp
  2793   from B[rule_format]
  2794   have "(\<exists>j\<in>?D. \<exists>b\<in> ?B. ?P (?I b + real_of_int j)) = (\<exists>j\<in>?D. \<exists>b\<in> ?B. ?P (real_of_int \<lfloor>?I b\<rfloor> + real_of_int j))"
  2795     by simp
  2796   also have "\<dots> = (\<exists>j\<in>?D. \<exists>b\<in> ?B. ?P (real_of_int (\<lfloor>?I b\<rfloor> + j)))" by simp
  2797   also have"\<dots> = (\<exists> j \<in> ?D. \<exists> b \<in> ?B'. ?P (real_of_int (b + j)))"  by blast
  2798   finally have BB':
  2799     "(\<exists>j\<in>?D. \<exists>b\<in> ?B. ?P (?I b + real_of_int j)) = (\<exists> j \<in> ?D. \<exists> b \<in> ?B'. ?P (real_of_int (b + j)))"
  2800     by blast
  2801   hence th2: "\<forall> x. \<not> (\<exists> j \<in> ?D. \<exists> b \<in> ?B'. ?P (real_of_int (b + j))) \<longrightarrow> ?P (real_of_int x) \<longrightarrow> ?P (real_of_int (x - d))" using \<beta>'[OF lp u d dp] by blast
  2802   from minusinf_repeats[OF d lp]
  2803   have th3: "\<forall> x k. ?M x = ?M (x-k*d)" by simp
  2804   from cpmi_eq[OF dp th th2 th3] BB' show ?thesis by blast
  2805 qed
  2806 
  2807     (* Reddy and Loveland *)
  2808 
  2809 
  2810 fun \<rho> :: "fm \<Rightarrow> (num \<times> int) list" (* Compute the Reddy and Loveland Bset*)
  2811   where
  2812   "\<rho> (And p q) = (\<rho> p @ \<rho> q)"
  2813 | "\<rho> (Or p q) = (\<rho> p @ \<rho> q)"
  2814 | "\<rho> (Eq  (CN 0 c e)) = [(Sub (C (- 1)) e,c)]"
  2815 | "\<rho> (NEq (CN 0 c e)) = [(Neg e,c)]"
  2816 | "\<rho> (Lt  (CN 0 c e)) = []"
  2817 | "\<rho> (Le  (CN 0 c e)) = []"
  2818 | "\<rho> (Gt  (CN 0 c e)) = [(Neg e, c)]"
  2819 | "\<rho> (Ge  (CN 0 c e)) = [(Sub (C (-1)) e, c)]"
  2820 | "\<rho> p = []"
  2821 
  2822 fun \<sigma>_\<rho>:: "fm \<Rightarrow> num \<times> int \<Rightarrow> fm" (* Performs the modified substitution of Reddy and Loveland*)
  2823 where
  2824   "\<sigma>_\<rho> (And p q) = (\<lambda> (t,k). And (\<sigma>_\<rho> p (t,k)) (\<sigma>_\<rho> q (t,k)))"
  2825 | "\<sigma>_\<rho> (Or p q) = (\<lambda> (t,k). Or (\<sigma>_\<rho> p (t,k)) (\<sigma>_\<rho> q (t,k)))"
  2826 | "\<sigma>_\<rho> (Eq  (CN 0 c e)) = (\<lambda> (t,k). if k dvd c then (Eq (Add (Mul (c div k) t) e))
  2827                                             else (Eq (Add (Mul c t) (Mul k e))))"
  2828 | "\<sigma>_\<rho> (NEq (CN 0 c e)) = (\<lambda> (t,k). if k dvd c then (NEq (Add (Mul (c div k) t) e))
  2829                                             else (NEq (Add (Mul c t) (Mul k e))))"
  2830 | "\<sigma>_\<rho> (Lt  (CN 0 c e)) = (\<lambda> (t,k). if k dvd c then (Lt (Add (Mul (c div k) t) e))
  2831                                             else (Lt (Add (Mul c t) (Mul k e))))"
  2832 | "\<sigma>_\<rho> (Le  (CN 0 c e)) = (\<lambda> (t,k). if k dvd c then (Le (Add (Mul (c div k) t) e))
  2833                                             else (Le (Add (Mul c t) (Mul k e))))"
  2834 | "\<sigma>_\<rho> (Gt  (CN 0 c e)) = (\<lambda> (t,k). if k dvd c then (Gt (Add (Mul (c div k) t) e))
  2835                                             else (Gt (Add (Mul c t) (Mul k e))))"
  2836 | "\<sigma>_\<rho> (Ge  (CN 0 c e)) = (\<lambda> (t,k). if k dvd c then (Ge (Add (Mul (c div k) t) e))
  2837                                             else (Ge (Add (Mul c t) (Mul k e))))"
  2838 | "\<sigma>_\<rho> (Dvd i (CN 0 c e)) =(\<lambda> (t,k). if k dvd c then (Dvd i (Add (Mul (c div k) t) e))
  2839                                             else (Dvd (i*k) (Add (Mul c t) (Mul k e))))"
  2840 | "\<sigma>_\<rho> (NDvd i (CN 0 c e))=(\<lambda> (t,k). if k dvd c then (NDvd i (Add (Mul (c div k) t) e))
  2841                                             else (NDvd (i*k) (Add (Mul c t) (Mul k e))))"
  2842 | "\<sigma>_\<rho> p = (\<lambda> (t,k). p)"
  2843 
  2844 fun \<alpha>_\<rho> :: "fm \<Rightarrow> (num \<times> int) list"
  2845 where
  2846   "\<alpha>_\<rho> (And p q) = (\<alpha>_\<rho> p @ \<alpha>_\<rho> q)"
  2847 | "\<alpha>_\<rho> (Or p q) = (\<alpha>_\<rho> p @ \<alpha>_\<rho> q)"
  2848 | "\<alpha>_\<rho> (Eq  (CN 0 c e)) = [(Add (C (- 1)) e,c)]"
  2849 | "\<alpha>_\<rho> (NEq (CN 0 c e)) = [(e,c)]"
  2850 | "\<alpha>_\<rho> (Lt  (CN 0 c e)) = [(e,c)]"
  2851 | "\<alpha>_\<rho> (Le  (CN 0 c e)) = [(Add (C (- 1)) e,c)]"
  2852 | "\<alpha>_\<rho> p = []"
  2853 
  2854     (* Simulates normal substituion by modifying the formula see correctness theorem *)
  2855 
  2856 definition \<sigma> :: "fm \<Rightarrow> int \<Rightarrow> num \<Rightarrow> fm" where
  2857   "\<sigma> p k t \<equiv> And (Dvd k t) (\<sigma>_\<rho> p (t,k))"
  2858 
  2859 lemma \<sigma>_\<rho>:
  2860   assumes linp: "iszlfm p (real_of_int (x::int)#bs)"
  2861   and kpos: "real_of_int k > 0"
  2862   and tnb: "numbound0 t"
  2863   and tint: "isint t (real_of_int x#bs)"
  2864   and kdt: "k dvd \<lfloor>Inum (b'#bs) t\<rfloor>"
  2865   shows "Ifm (real_of_int x#bs) (\<sigma>_\<rho> p (t,k)) = (Ifm ((real_of_int (\<lfloor>Inum (b'#bs) t\<rfloor> div k))#bs) p)"
  2866   (is "?I (real_of_int x) (?s p) = (?I (real_of_int (\<lfloor>?N b' t\<rfloor> div k)) p)" is "_ = (?I ?tk p)")
  2867 using linp kpos tnb
  2868 proof(induct p rule: \<sigma>_\<rho>.induct)
  2869   case (3 c e)
  2870   from 3 have cp: "c > 0" and nb: "numbound0 e" by auto
  2871   { assume kdc: "k dvd c"
  2872     from tint have ti: "real_of_int \<lfloor>?N (real_of_int x) t\<rfloor> = ?N (real_of_int x) t" using isint_def by simp
  2873     from kdc have ?case using real_of_int_div[OF kdc] real_of_int_div[OF kdt]
  2874       numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real_of_int x"]
  2875       numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real_of_int x"] by (simp add: ti) }
  2876   moreover
  2877   { assume *: "\<not> k dvd c"
  2878     from kpos have knz': "real_of_int k \<noteq> 0" by simp
  2879     from tint have ti: "real_of_int \<lfloor>?N (real_of_int x) t\<rfloor> = ?N (real_of_int x) t"
  2880       using isint_def by simp
  2881     from assms * have "?I (real_of_int x) (?s (Eq (CN 0 c e))) = ((real_of_int c * (?N (real_of_int x) t / real_of_int k) + ?N (real_of_int x) e)* real_of_int k = 0)"
  2882       using real_of_int_div[OF kdt]
  2883         numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real_of_int x"]
  2884         numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real_of_int x"]
  2885       by (simp add: ti algebra_simps)
  2886       also have "\<dots> = (?I ?tk (Eq (CN 0 c e)))"
  2887         using nonzero_eq_divide_eq[OF knz',
  2888             where a="real_of_int c * (?N (real_of_int x) t / real_of_int k) + ?N (real_of_int x) e" and b="0", symmetric]
  2889           real_of_int_div[OF kdt] numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real_of_int x"]
  2890           numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real_of_int x"]
  2891         by (simp add: ti)
  2892       finally have ?case . }
  2893     ultimately show ?case by blast
  2894 next
  2895   case (4 c e)
  2896   then have cp: "c > 0" and nb: "numbound0 e" by auto
  2897   { assume kdc: "k dvd c"
  2898     from tint have ti: "real_of_int \<lfloor>?N (real_of_int x) t\<rfloor> = ?N (real_of_int x) t" using isint_def by simp
  2899     from kdc have  ?case using real_of_int_div[OF kdc] real_of_int_div[OF kdt]
  2900       numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real_of_int x"]
  2901       numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real_of_int x"] by (simp add: ti) }
  2902   moreover
  2903   { assume *: "\<not> k dvd c"
  2904     from kpos have knz': "real_of_int k \<noteq> 0" by simp
  2905     from tint have ti: "real_of_int \<lfloor>?N (real_of_int x) t\<rfloor> = ?N (real_of_int x) t" using isint_def by simp
  2906     from assms * have "?I (real_of_int x) (?s (NEq (CN 0 c e))) = ((real_of_int c * (?N (real_of_int x) t / real_of_int k) + ?N (real_of_int x) e)* real_of_int k \<noteq> 0)"
  2907       using real_of_int_div[OF kdt]
  2908         numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real_of_int x"]
  2909         numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real_of_int x"]
  2910       by (simp add: ti algebra_simps)
  2911     also have "\<dots> = (?I ?tk (NEq (CN 0 c e)))"
  2912       using nonzero_eq_divide_eq[OF knz',
  2913           where a="real_of_int c * (?N (real_of_int x) t / real_of_int k) + ?N (real_of_int x) e" and b="0", symmetric]
  2914         real_of_int_div[OF kdt] numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real_of_int x"]
  2915         numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real_of_int x"]
  2916       by (simp add: ti)
  2917     finally have ?case . }
  2918   ultimately show ?case by blast
  2919 next
  2920   case (5 c e)
  2921   then have cp: "c > 0" and nb: "numbound0 e" by auto
  2922   { assume kdc: "k dvd c"
  2923     from tint have ti: "real_of_int \<lfloor>?N (real_of_int x) t\<rfloor> = ?N (real_of_int x) t" using isint_def by simp
  2924     from kdc have  ?case using real_of_int_div[OF kdc] real_of_int_div[OF kdt]
  2925       numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real_of_int x"]
  2926       numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real_of_int x"] by (simp add: ti) }
  2927   moreover
  2928   { assume *: "\<not> k dvd c"
  2929     from tint have ti: "real_of_int \<lfloor>?N (real_of_int x) t\<rfloor> = ?N (real_of_int x) t" using isint_def by simp
  2930     from assms * have "?I (real_of_int x) (?s (Lt (CN 0 c e))) = ((real_of_int c * (?N (real_of_int x) t / real_of_int k) + ?N (real_of_int x) e)* real_of_int k < 0)"
  2931       using real_of_int_div[OF kdt]
  2932         numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real_of_int x"]
  2933         numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real_of_int x"]
  2934       by (simp add: ti algebra_simps)
  2935     also have "\<dots> = (?I ?tk (Lt (CN 0 c e)))"
  2936       using pos_less_divide_eq[OF kpos,
  2937           where a="real_of_int c * (?N (real_of_int x) t / real_of_int k) + ?N (real_of_int x) e" and b="0", symmetric]
  2938         real_of_int_div[OF kdt] numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real_of_int x"]
  2939         numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real_of_int x"]
  2940       by (simp add: ti)
  2941     finally have ?case . }
  2942   ultimately show ?case by blast
  2943 next
  2944   case (6 c e)
  2945   then have cp: "c > 0" and nb: "numbound0 e" by auto
  2946   { assume kdc: "k dvd c"
  2947     from tint have ti: "real_of_int \<lfloor>?N (real_of_int x) t\<rfloor> = ?N (real_of_int x) t" using isint_def by simp
  2948     from kdc have  ?case using real_of_int_div[OF kdc] real_of_int_div[OF kdt]
  2949       numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real_of_int x"]
  2950       numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real_of_int x"] by (simp add: ti) }
  2951   moreover
  2952   { assume *: "\<not> k dvd c"
  2953     from tint have ti: "real_of_int \<lfloor>?N (real_of_int x) t\<rfloor> = ?N (real_of_int x) t" using isint_def by simp
  2954     from assms * have "?I (real_of_int x) (?s (Le (CN 0 c e))) = ((real_of_int c * (?N (real_of_int x) t / real_of_int k) + ?N (real_of_int x) e)* real_of_int k \<le> 0)"
  2955       using real_of_int_div[OF kdt]
  2956         numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real_of_int x"]
  2957         numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real_of_int x"]
  2958       by (simp add: ti algebra_simps)
  2959     also have "\<dots> = (?I ?tk (Le (CN 0 c e)))"
  2960       using pos_le_divide_eq[OF kpos,
  2961           where a="real_of_int c * (?N (real_of_int x) t / real_of_int k) + ?N (real_of_int x) e" and b="0", symmetric]
  2962         real_of_int_div[OF kdt] numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real_of_int x"]
  2963         numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real_of_int x"]
  2964       by (simp add: ti)
  2965     finally have ?case . }
  2966   ultimately show ?case by blast
  2967 next
  2968   case (7 c e)
  2969   then have cp: "c > 0" and nb: "numbound0 e" by auto
  2970   { assume kdc: "k dvd c"
  2971     from tint have ti: "real_of_int \<lfloor>?N (real_of_int x) t\<rfloor> = ?N (real_of_int x) t" using isint_def by simp
  2972     from kdc have  ?case using real_of_int_div[OF kdc] real_of_int_div[OF kdt]
  2973       numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real_of_int x"]
  2974       numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real_of_int x"] by (simp add: ti) }
  2975   moreover
  2976   { assume *: "\<not> k dvd c"
  2977     from tint have ti: "real_of_int \<lfloor>?N (real_of_int x) t\<rfloor> = ?N (real_of_int x) t" using isint_def by simp
  2978     from assms * have "?I (real_of_int x) (?s (Gt (CN 0 c e))) = ((real_of_int c * (?N (real_of_int x) t / real_of_int k) + ?N (real_of_int x) e)* real_of_int k > 0)"
  2979       using real_of_int_div[OF kdt]
  2980         numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real_of_int x"]
  2981         numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real_of_int x"]
  2982       by (simp add: ti algebra_simps)
  2983     also have "\<dots> = (?I ?tk (Gt (CN 0 c e)))"
  2984       using pos_divide_less_eq[OF kpos,
  2985           where a="real_of_int c * (?N (real_of_int x) t / real_of_int k) + ?N (real_of_int x) e" and b="0", symmetric]
  2986         real_of_int_div[OF kdt] numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real_of_int x"]
  2987         numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real_of_int x"]
  2988       by (simp add: ti)
  2989     finally have ?case . }
  2990   ultimately show ?case by blast
  2991 next
  2992   case (8 c e)
  2993   then have cp: "c > 0" and nb: "numbound0 e" by auto
  2994   { assume kdc: "k dvd c"
  2995     from tint have ti: "real_of_int \<lfloor>?N (real_of_int x) t\<rfloor> = ?N (real_of_int x) t" using isint_def by simp
  2996     from kdc have  ?case using real_of_int_div[OF kdc] real_of_int_div[OF kdt]
  2997       numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real_of_int x"]
  2998       numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real_of_int x"] by (simp add: ti) }
  2999   moreover
  3000   { assume *: "\<not> k dvd c"
  3001     from tint have ti: "real_of_int \<lfloor>?N (real_of_int x) t\<rfloor> = ?N (real_of_int x) t" using isint_def by simp
  3002     from assms * have "?I (real_of_int x) (?s (Ge (CN 0 c e))) = ((real_of_int c * (?N (real_of_int x) t / real_of_int k) + ?N (real_of_int x) e)* real_of_int k \<ge> 0)"
  3003       using real_of_int_div[OF kdt]
  3004         numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real_of_int x"]
  3005         numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real_of_int x"]
  3006       by (simp add: ti algebra_simps)
  3007     also have "\<dots> = (?I ?tk (Ge (CN 0 c e)))"
  3008       using pos_divide_le_eq[OF kpos,
  3009           where a="real_of_int c * (?N (real_of_int x) t / real_of_int k) + ?N (real_of_int x) e" and b="0", symmetric]
  3010         real_of_int_div[OF kdt] numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real_of_int x"]
  3011         numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real_of_int x"]
  3012       by (simp add: ti)
  3013     finally have ?case . }
  3014   ultimately show ?case by blast
  3015 next
  3016   case (9 i c e)
  3017   then have cp: "c > 0" and nb: "numbound0 e" by auto
  3018   { assume kdc: "k dvd c"
  3019     from tint have ti: "real_of_int \<lfloor>?N (real_of_int x) t\<rfloor> = ?N (real_of_int x) t" using isint_def by simp
  3020     from kdc have ?case using real_of_int_div[OF kdc] real_of_int_div[OF kdt]
  3021       numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real_of_int x"]
  3022       numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real_of_int x"] by (simp add: ti) }
  3023   moreover
  3024   { assume *: "\<not> k dvd c"
  3025     from kpos have knz: "k\<noteq>0" by simp hence knz': "real_of_int k \<noteq> 0" by simp
  3026     from tint have ti: "real_of_int \<lfloor>?N (real_of_int x) t\<rfloor> = ?N (real_of_int x) t" using isint_def by simp
  3027     from assms * have "?I (real_of_int x) (?s (Dvd i (CN 0 c e))) = (real_of_int i * real_of_int k rdvd (real_of_int c * (?N (real_of_int x) t / real_of_int k) + ?N (real_of_int x) e)* real_of_int k)"
  3028       using real_of_int_div[OF kdt]
  3029         numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real_of_int x"]
  3030         numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real_of_int x"]
  3031       by (simp add: ti algebra_simps)
  3032     also have "\<dots> = (?I ?tk (Dvd i (CN 0 c e)))"
  3033       using rdvd_mult[OF knz, where n="i"]
  3034         real_of_int_div[OF kdt] numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real_of_int x"]
  3035         numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real_of_int x"]
  3036       by (simp add: ti)
  3037     finally have ?case . }
  3038   ultimately show ?case by blast
  3039 next
  3040   case (10 i c e)
  3041   then have cp: "c > 0" and nb: "numbound0 e" by auto
  3042   { assume kdc: "k dvd c"
  3043     from tint have ti: "real_of_int \<lfloor>?N (real_of_int x) t\<rfloor> = ?N (real_of_int x) t" using isint_def by simp
  3044     from kdc have  ?case using real_of_int_div[OF kdc] real_of_int_div[OF kdt]
  3045       numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real_of_int x"]
  3046       numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real_of_int x"] by (simp add: ti) }
  3047   moreover
  3048   { assume *: "\<not> k dvd c"
  3049     from kpos have knz: "k\<noteq>0" by simp hence knz': "real_of_int k \<noteq> 0" by simp
  3050     from tint have ti: "real_of_int \<lfloor>?N (real_of_int x) t\<rfloor> = ?N (real_of_int x) t" using isint_def by simp
  3051     from assms * have "?I (real_of_int x) (?s (NDvd i (CN 0 c e))) = (\<not> (real_of_int i * real_of_int k rdvd (real_of_int c * (?N (real_of_int x) t / real_of_int k) + ?N (real_of_int x) e)* real_of_int k))"
  3052       using real_of_int_div[OF kdt]
  3053         numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real_of_int x"]
  3054         numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real_of_int x"]
  3055       by (simp add: ti algebra_simps)
  3056     also have "\<dots> = (?I ?tk (NDvd i (CN 0 c e)))"
  3057       using rdvd_mult[OF knz, where n="i"] real_of_int_div[OF kdt]
  3058         numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real_of_int x"]
  3059         numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real_of_int x"]
  3060       by (simp add: ti)
  3061     finally have ?case . }
  3062   ultimately show ?case by blast
  3063 qed (simp_all add: bound0_I[where bs="bs" and b="real_of_int (\<lfloor>?N b' t\<rfloor> div k)" and b'="real_of_int x"]
  3064   numbound0_I[where bs="bs" and b="real_of_int (\<lfloor>?N b' t\<rfloor> div k)" and b'="real_of_int x"])
  3065 
  3066 
  3067 lemma \<sigma>_\<rho>_nb: assumes lp:"iszlfm p (a#bs)" and nb: "numbound0 t"
  3068   shows "bound0 (\<sigma>_\<rho> p (t,k))"
  3069   using lp
  3070   by (induct p rule: iszlfm.induct, auto simp add: nb)
  3071 
  3072 lemma \<rho>_l:
  3073   assumes lp: "iszlfm p (real_of_int (i::int)#bs)"
  3074   shows "\<forall> (b,k) \<in> set (\<rho> p). k >0 \<and> numbound0 b \<and> isint b (real_of_int i#bs)"
  3075 using lp by (induct p rule: \<rho>.induct, auto simp add: isint_sub isint_neg)
  3076 
  3077 lemma \<alpha>_\<rho>_l:
  3078   assumes lp: "iszlfm p (real_of_int (i::int)#bs)"
  3079   shows "\<forall> (b,k) \<in> set (\<alpha>_\<rho> p). k >0 \<and> numbound0 b \<and> isint b (real_of_int i#bs)"
  3080 using lp isint_add [OF isint_c[where j="- 1"],where bs="real_of_int i#bs"]
  3081  by (induct p rule: \<alpha>_\<rho>.induct, auto)
  3082 
  3083 lemma \<rho>: assumes lp: "iszlfm p (real_of_int (i::int) #bs)"
  3084   and pi: "Ifm (real_of_int i#bs) p"
  3085   and d: "d_\<delta> p d"
  3086   and dp: "d > 0"
  3087   and nob: "\<forall>(e,c) \<in> set (\<rho> p). \<forall> j\<in> {1 .. c*d}. real_of_int (c*i) \<noteq> Inum (real_of_int i#bs) e + real_of_int j"
  3088   (is "\<forall>(e,c) \<in> set (\<rho> p). \<forall> j\<in> {1 .. c*d}. _ \<noteq> ?N i e + _")
  3089   shows "Ifm (real_of_int(i - d)#bs) p"
  3090   using lp pi d nob
  3091 proof(induct p rule: iszlfm.induct)
  3092   case (3 c e) hence cp: "c >0" and nb: "numbound0 e" and ei: "isint e (real_of_int i#bs)"
  3093     and pi: "real_of_int (c*i) = - 1 -  ?N i e + real_of_int (1::int)" and nob: "\<forall> j\<in> {1 .. c*d}. real_of_int (c*i) \<noteq> -1 - ?N i e + real_of_int j"
  3094     by simp+
  3095   from mult_strict_left_mono[OF dp cp]  have one:"1 \<in> {1 .. c*d}" by auto
  3096   from nob[rule_format, where j="1", OF one] pi show ?case by simp
  3097 next
  3098   case (4 c e)
  3099   hence cp: "c >0" and nb: "numbound0 e" and ei: "isint e (real_of_int i#bs)"
  3100     and nob: "\<forall> j\<in> {1 .. c*d}. real_of_int (c*i) \<noteq> - ?N i e + real_of_int j"
  3101     by simp+
  3102   {assume "real_of_int (c*i) \<noteq> - ?N i e + real_of_int (c*d)"
  3103     with numbound0_I[OF nb, where bs="bs" and b="real_of_int i - real_of_int d" and b'="real_of_int i"]
  3104     have ?case by (simp add: algebra_simps)}
  3105   moreover
  3106   {assume pi: "real_of_int (c*i) = - ?N i e + real_of_int (c*d)"
  3107     from mult_strict_left_mono[OF dp cp] have d: "(c*d) \<in> {1 .. c*d}" by simp
  3108     from nob[rule_format, where j="c*d", OF d] pi have ?case by simp }
  3109   ultimately show ?case by blast
  3110 next
  3111   case (5 c e) hence cp: "c > 0" by simp
  3112   from 5 mult_strict_left_mono[OF dp cp, simplified of_int_less_iff[symmetric]
  3113     of_int_mult]
  3114   show ?case using 5 dp
  3115     apply (simp add: numbound0_I[where bs="bs" and b="real_of_int i - real_of_int d" and b'="real_of_int i"]
  3116       algebra_simps del: mult_pos_pos)
  3117      by (metis add.right_neutral of_int_0_less_iff of_int_mult pos_add_strict)
  3118 next
  3119   case (6 c e) hence cp: "c > 0" by simp
  3120   from 6 mult_strict_left_mono[OF dp cp, simplified of_int_less_iff[symmetric]
  3121     of_int_mult]
  3122   show ?case using 6 dp
  3123     apply (simp add: numbound0_I[where bs="bs" and b="real_of_int i - real_of_int d" and b'="real_of_int i"]
  3124       algebra_simps del: mult_pos_pos)
  3125       using order_trans by fastforce
  3126 next
  3127   case (7 c e) hence cp: "c >0" and nb: "numbound0 e" and ei: "isint e (real_of_int i#bs)"
  3128     and nob: "\<forall> j\<in> {1 .. c*d}. real_of_int (c*i) \<noteq> - ?N i e + real_of_int j"
  3129     and pi: "real_of_int (c*i) + ?N i e > 0" and cp': "real_of_int c >0"
  3130     by simp+
  3131   let ?fe = "\<lfloor>?N i e\<rfloor>"
  3132   from pi cp have th:"(real_of_int i +?N i e / real_of_int c)*real_of_int c > 0" by (simp add: algebra_simps)
  3133   from pi ei[simplified isint_iff] have "real_of_int (c*i + ?fe) > real_of_int (0::int)" by simp
  3134   hence pi': "c*i + ?fe > 0" by (simp only: of_int_less_iff[symmetric])
  3135   have "real_of_int (c*i) + ?N i e > real_of_int (c*d) \<or> real_of_int (c*i) + ?N i e \<le> real_of_int (c*d)" by auto
  3136   moreover
  3137   {assume "real_of_int (c*i) + ?N i e > real_of_int (c*d)" hence ?case
  3138       by (simp add: algebra_simps
  3139         numbound0_I[OF nb,where bs="bs" and b="real_of_int i - real_of_int d" and b'="real_of_int i"])}
  3140   moreover
  3141   {assume H:"real_of_int (c*i) + ?N i e \<le> real_of_int (c*d)"
  3142     with ei[simplified isint_iff] have "real_of_int (c*i + ?fe) \<le> real_of_int (c*d)" by simp
  3143     hence pid: "c*i + ?fe \<le> c*d" by (simp only: of_int_le_iff)
  3144     with pi' have "\<exists> j1\<in> {1 .. c*d}. c*i + ?fe = j1" by auto
  3145     hence "\<exists> j1\<in> {1 .. c*d}. real_of_int (c*i) = - ?N i e + real_of_int j1"
  3146       unfolding Bex_def using ei[simplified isint_iff] by fastforce
  3147     with nob  have ?case by blast }
  3148   ultimately show ?case by blast
  3149 next
  3150   case (8 c e)  hence cp: "c >0" and nb: "numbound0 e" and ei: "isint e (real_of_int i#bs)"
  3151     and nob: "\<forall> j\<in> {1 .. c*d}. real_of_int (c*i) \<noteq> - 1 - ?N i e + real_of_int j"
  3152     and pi: "real_of_int (c*i) + ?N i e \<ge> 0" and cp': "real_of_int c >0"
  3153     by simp+
  3154   let ?fe = "\<lfloor>?N i e\<rfloor>"
  3155   from pi cp have th:"(real_of_int i +?N i e / real_of_int c)*real_of_int c \<ge> 0" by (simp add: algebra_simps)
  3156   from pi ei[simplified isint_iff] have "real_of_int (c*i + ?fe) \<ge> real_of_int (0::int)" by simp
  3157   hence pi': "c*i + 1 + ?fe \<ge> 1" by (simp only: of_int_le_iff[symmetric])
  3158   have "real_of_int (c*i) + ?N i e \<ge> real_of_int (c*d) \<or> real_of_int (c*i) + ?N i e < real_of_int (c*d)" by auto
  3159   moreover
  3160   {assume "real_of_int (c*i) + ?N i e \<ge> real_of_int (c*d)" hence ?case
  3161       by (simp add: algebra_simps
  3162         numbound0_I[OF nb,where bs="bs" and b="real_of_int i - real_of_int d" and b'="real_of_int i"])}
  3163   moreover
  3164   {assume H:"real_of_int (c*i) + ?N i e < real_of_int (c*d)"
  3165     with ei[simplified isint_iff] have "real_of_int (c*i + ?fe) < real_of_int (c*d)" by simp
  3166     hence pid: "c*i + 1 + ?fe \<le> c*d" by (simp only: of_int_le_iff)
  3167     with pi' have "\<exists> j1\<in> {1 .. c*d}. c*i + 1+ ?fe = j1" by auto
  3168     hence "\<exists> j1\<in> {1 .. c*d}. real_of_int (c*i) + 1= - ?N i e + real_of_int j1"
  3169       unfolding Bex_def using ei[simplified isint_iff] by fastforce
  3170     hence "\<exists> j1\<in> {1 .. c*d}. real_of_int (c*i) = (- ?N i e + real_of_int j1) - 1"
  3171       by (simp only: algebra_simps)
  3172         hence "\<exists> j1\<in> {1 .. c*d}. real_of_int (c*i) = - 1 - ?N i e + real_of_int j1"
  3173           by (simp add: algebra_simps)
  3174     with nob  have ?case by blast }
  3175   ultimately show ?case by blast
  3176 next
  3177   case (9 j c e)  hence p: "real_of_int j rdvd real_of_int (c*i) + ?N i e" (is "?p x") and cp: "c > 0" and bn:"numbound0 e"  by simp+
  3178   let ?e = "Inum (real_of_int i # bs) e"
  3179   from 9 have "isint e (real_of_int i #bs)"  by simp
  3180   hence ie: "real_of_int \<lfloor>?e\<rfloor> = ?e" using isint_iff[where n="e" and bs="(real_of_int i)#bs"] numbound0_I[OF bn,where b="real_of_int i" and b'="real_of_int i" and bs="bs"]
  3181     by (simp add: isint_iff)
  3182   from 9 have id: "j dvd d" by simp
  3183   from ie[symmetric] have "?p i = (real_of_int j rdvd real_of_int (c*i + \<lfloor>?e\<rfloor>))" by simp
  3184   also have "\<dots> = (j dvd c*i + \<lfloor>?e\<rfloor>)"
  3185     using int_rdvd_iff [where i="j" and t="c*i + \<lfloor>?e\<rfloor>"] by simp
  3186   also have "\<dots> = (j dvd c*i - c*d + \<lfloor>?e\<rfloor>)"
  3187     using dvd_period[OF id, where x="c*i" and c="-c" and t="\<lfloor>?e\<rfloor>"] by simp
  3188   also have "\<dots> = (real_of_int j rdvd real_of_int (c*i - c*d + \<lfloor>?e\<rfloor>))"
  3189     using int_rdvd_iff[where i="j" and t="(c*i - c*d + \<lfloor>?e\<rfloor>)",symmetric, simplified]
  3190       ie by simp
  3191   also have "\<dots> = (real_of_int j rdvd real_of_int (c*(i - d)) + ?e)"
  3192     using ie by (simp add:algebra_simps)
  3193   finally show ?case
  3194     using numbound0_I[OF bn,where b="real_of_int i - real_of_int d" and b'="real_of_int i" and bs="bs"] p
  3195     by (simp add: algebra_simps)
  3196 next
  3197   case (10 j c e)
  3198   hence p: "\<not> (real_of_int j rdvd real_of_int (c*i) + ?N i e)" (is "?p x") and cp: "c > 0" and bn:"numbound0 e"
  3199     by simp+
  3200   let ?e = "Inum (real_of_int i # bs) e"
  3201   from 10 have "isint e (real_of_int i #bs)"  by simp
  3202   hence ie: "real_of_int \<lfloor>?e\<rfloor> = ?e"
  3203     using isint_iff[where n="e" and bs="(real_of_int i)#bs"] numbound0_I[OF bn,where b="real_of_int i" and b'="real_of_int i" and bs="bs"]
  3204     by (simp add: isint_iff)
  3205   from 10 have id: "j dvd d" by simp
  3206   from ie[symmetric] have "?p i = (\<not> (real_of_int j rdvd real_of_int (c*i + \<lfloor>?e\<rfloor>)))" by simp
  3207   also have "\<dots> = Not (j dvd c*i + \<lfloor>?e\<rfloor>)"
  3208     using int_rdvd_iff [where i="j" and t="c*i + \<lfloor>?e\<rfloor>"] by simp
  3209   also have "\<dots> = Not (j dvd c*i - c*d + \<lfloor>?e\<rfloor>)"
  3210     using dvd_period[OF id, where x="c*i" and c="-c" and t="\<lfloor>?e\<rfloor>"] by simp
  3211   also have "\<dots> = Not (real_of_int j rdvd real_of_int (c*i - c*d + \<lfloor>?e\<rfloor>))"
  3212     using int_rdvd_iff[where i="j" and t="(c*i - c*d + \<lfloor>?e\<rfloor>)",symmetric, simplified]
  3213       ie by simp
  3214   also have "\<dots> = Not (real_of_int j rdvd real_of_int (c*(i - d)) + ?e)"
  3215     using ie by (simp add:algebra_simps)
  3216   finally show ?case
  3217     using numbound0_I[OF bn,where b="real_of_int i - real_of_int d" and b'="real_of_int i" and bs="bs"] p
  3218     by (simp add: algebra_simps)
  3219 qed (auto simp add: numbound0_I[where bs="bs" and b="real_of_int i - real_of_int d" and b'="real_of_int i"])
  3220 
  3221 lemma \<sigma>_nb: assumes lp: "iszlfm p (a#bs)" and nb: "numbound0 t"
  3222   shows "bound0 (\<sigma> p k t)"
  3223   using \<sigma>_\<rho>_nb[OF lp nb] nb by (simp add: \<sigma>_def)
  3224 
  3225 lemma \<rho>':   assumes lp: "iszlfm p (a #bs)"
  3226   and d: "d_\<delta> p d"
  3227   and dp: "d > 0"
  3228   shows "\<forall> x. \<not>(\<exists> (e,c) \<in> set(\<rho> p). \<exists>(j::int) \<in> {1 .. c*d}. Ifm (a #bs) (\<sigma> p c (Add e (C j)))) \<longrightarrow> Ifm (real_of_int x#bs) p \<longrightarrow> Ifm (real_of_int (x - d)#bs) p" (is "\<forall> x. ?b x \<longrightarrow> ?P x \<longrightarrow> ?P (x - d)")
  3229 proof(clarify)
  3230   fix x
  3231   assume nob1:"?b x" and px: "?P x"
  3232   from iszlfm_gen[OF lp, rule_format, where y="real_of_int x"] have lp': "iszlfm p (real_of_int x#bs)".
  3233   have nob: "\<forall>(e, c)\<in>set (\<rho> p). \<forall>j\<in>{1..c * d}. real_of_int (c * x) \<noteq> Inum (real_of_int x # bs) e + real_of_int j"
  3234   proof(clarify)
  3235     fix e c j assume ecR: "(e,c) \<in> set (\<rho> p)" and jD: "j\<in> {1 .. c*d}"
  3236       and cx: "real_of_int (c*x) = Inum (real_of_int x#bs) e + real_of_int j"
  3237     let ?e = "Inum (real_of_int x#bs) e"
  3238     from \<rho>_l[OF lp'] ecR have ei:"isint e (real_of_int x#bs)" and cp:"c>0" and nb:"numbound0 e"
  3239       by auto
  3240     from numbound0_gen [OF nb ei, rule_format,where y="a"] have "isint e (a#bs)" .
  3241     from cx ei[simplified isint_iff] have "real_of_int (c*x) = real_of_int (\<lfloor>?e\<rfloor> + j)" by simp
  3242     hence cx: "c*x = \<lfloor>?e\<rfloor> + j" by (simp only: of_int_eq_iff)
  3243     hence cdej:"c dvd \<lfloor>?e\<rfloor> + j" by (simp add: dvd_def) (rule_tac x="x" in exI, simp)
  3244     hence "real_of_int c rdvd real_of_int (\<lfloor>?e\<rfloor> + j)" by (simp only: int_rdvd_iff)
  3245     hence rcdej: "real_of_int c rdvd ?e + real_of_int j" by (simp add: ei[simplified isint_iff])
  3246     from cx have "(c*x) div c = (\<lfloor>?e\<rfloor> + j) div c" by simp
  3247     with cp have "x = (\<lfloor>?e\<rfloor> + j) div c" by simp
  3248     with px have th: "?P ((\<lfloor>?e\<rfloor> + j) div c)" by auto
  3249     from cp have cp': "real_of_int c > 0" by simp
  3250     from cdej have cdej': "c dvd \<lfloor>Inum (real_of_int x#bs) (Add e (C j))\<rfloor>" by simp
  3251     from nb have nb': "numbound0 (Add e (C j))" by simp
  3252     have ji: "isint (C j) (real_of_int x#bs)" by (simp add: isint_def)
  3253     from isint_add[OF ei ji] have ei':"isint (Add e (C j)) (real_of_int x#bs)" .
  3254     from th \<sigma>_\<rho>[where b'="real_of_int x", OF lp' cp' nb' ei' cdej',symmetric]
  3255     have "Ifm (real_of_int x#bs) (\<sigma>_\<rho> p (Add e (C j), c))" by simp
  3256     with rcdej have th: "Ifm (real_of_int x#bs) (\<sigma> p c (Add e (C j)))" by (simp add: \<sigma>_def)
  3257     from th bound0_I[OF \<sigma>_nb[OF lp nb', where k="c"],where bs="bs" and b="real_of_int x" and b'="a"]
  3258     have "Ifm (a#bs) (\<sigma> p c (Add e (C j)))" by blast
  3259       with ecR jD nob1    show "False" by blast
  3260   qed
  3261   from \<rho>[OF lp' px d dp nob] show "?P (x -d )" .
  3262 qed
  3263 
  3264 
  3265 lemma rl_thm:
  3266   assumes lp: "iszlfm p (real_of_int (i::int)#bs)"
  3267   shows "(\<exists> (x::int). Ifm (real_of_int x#bs) p) = ((\<exists> j\<in> {1 .. \<delta> p}. Ifm (real_of_int j#bs) (minusinf p)) \<or> (\<exists> (e,c) \<in> set (\<rho> p). \<exists> j\<in> {1 .. c*(\<delta> p)}. Ifm (a#bs) (\<sigma> p c (Add e (C j)))))"
  3268   (is "(\<exists>(x::int). ?P x) = ((\<exists> j\<in> {1.. \<delta> p}. ?MP j)\<or>(\<exists> (e,c) \<in> ?R. \<exists> j\<in> _. ?SP c e j))"
  3269     is "?lhs = (?MD \<or> ?RD)"  is "?lhs = ?rhs")
  3270 proof-
  3271   let ?d= "\<delta> p"
  3272   from \<delta>[OF lp] have d:"d_\<delta> p ?d" and dp: "?d > 0" by auto
  3273   { assume H:"?MD" hence th:"\<exists> (x::int). ?MP x" by blast
  3274     from H minusinf_ex[OF lp th] have ?thesis  by blast}
  3275   moreover
  3276   { fix e c j assume exR:"(e,c) \<in> ?R" and jD:"j\<in> {1 .. c*?d}" and spx:"?SP c e j"
  3277     from exR \<rho>_l[OF lp] have nb: "numbound0 e" and ei:"isint e (real_of_int i#bs)" and cp: "c > 0"
  3278       by auto
  3279     have "isint (C j) (real_of_int i#bs)" by (simp add: isint_iff)
  3280     with isint_add[OF numbound0_gen[OF nb ei,rule_format, where y="real_of_int i"]]
  3281     have eji:"isint (Add e (C j)) (real_of_int i#bs)" by simp
  3282     from nb have nb': "numbound0 (Add e (C j))" by simp
  3283     from spx bound0_I[OF \<sigma>_nb[OF lp nb', where k="c"], where bs="bs" and b="a" and b'="real_of_int i"]
  3284     have spx': "Ifm (real_of_int i # bs) (\<sigma> p c (Add e (C j)))" by blast
  3285     from spx' have rcdej:"real_of_int c rdvd (Inum (real_of_int i#bs) (Add e (C j)))"
  3286       and sr:"Ifm (real_of_int i#bs) (\<sigma>_\<rho> p (Add e (C j),c))" by (simp add: \<sigma>_def)+
  3287     from rcdej eji[simplified isint_iff]
  3288     have "real_of_int c rdvd real_of_int \<lfloor>Inum (real_of_int i#bs) (Add e (C j))\<rfloor>" by simp
  3289     hence cdej:"c dvd \<lfloor>Inum (real_of_int i#bs) (Add e (C j))\<rfloor>" by (simp only: int_rdvd_iff)
  3290     from cp have cp': "real_of_int c > 0" by simp
  3291     from \<sigma>_\<rho>[OF lp cp' nb' eji cdej] spx' have "?P (\<lfloor>Inum (real_of_int i # bs) (Add e (C j))\<rfloor> div c)"
  3292       by (simp add: \<sigma>_def)
  3293     hence ?lhs by blast
  3294     with exR jD spx have ?thesis by blast}
  3295   moreover
  3296   { fix x assume px: "?P x" and nob: "\<not> ?RD"
  3297     from iszlfm_gen [OF lp,rule_format, where y="a"] have lp':"iszlfm p (a#bs)" .
  3298     from \<rho>'[OF lp' d dp, rule_format, OF nob] have th:"\<forall> x. ?P x \<longrightarrow> ?P (x - ?d)" by blast
  3299     from minusinf_inf[OF lp] obtain z where z:"\<forall> x<z. ?MP x = ?P x" by blast
  3300     have zp: "\<bar>x - z\<bar> + 1 \<ge> 0" by arith
  3301     from decr_lemma[OF dp,where x="x" and z="z"]
  3302       decr_mult_lemma[OF dp th zp, rule_format, OF px] z have th:"\<exists> x. ?MP x" by auto
  3303     with minusinf_bex[OF lp] px nob have ?thesis by blast}
  3304   ultimately show ?thesis by blast
  3305 qed
  3306 
  3307 lemma mirror_\<alpha>_\<rho>:   assumes lp: "iszlfm p (a#bs)"
  3308   shows "(\<lambda> (t,k). (Inum (a#bs) t, k)) ` set (\<alpha>_\<rho> p) = (\<lambda> (t,k). (Inum (a#bs) t,k)) ` set (\<rho> (mirror p))"
  3309   using lp
  3310   by (induct p rule: mirror.induct) (simp_all add: split_def image_Un)
  3311 
  3312 text \<open>The \<open>\<real>\<close> part\<close>
  3313 
  3314 text\<open>Linearity for fm where Bound 0 ranges over \<open>\<real>\<close>\<close>
  3315 fun isrlfm :: "fm \<Rightarrow> bool"   (* Linearity test for fm *)
  3316 where
  3317   "isrlfm (And p q) = (isrlfm p \<and> isrlfm q)"
  3318 | "isrlfm (Or p q) = (isrlfm p \<and> isrlfm q)"
  3319 | "isrlfm (Eq  (CN 0 c e)) = (c>0 \<and> numbound0 e)"
  3320 | "isrlfm (NEq (CN 0 c e)) = (c>0 \<and> numbound0 e)"
  3321 | "isrlfm (Lt  (CN 0 c e)) = (c>0 \<and> numbound0 e)"
  3322 | "isrlfm (Le  (CN 0 c e)) = (c>0 \<and> numbound0 e)"
  3323 | "isrlfm (Gt  (CN 0 c e)) = (c>0 \<and> numbound0 e)"
  3324 | "isrlfm (Ge  (CN 0 c e)) = (c>0 \<and> numbound0 e)"
  3325 | "isrlfm p = (isatom p \<and> (bound0 p))"
  3326 
  3327 definition fp :: "fm \<Rightarrow> int \<Rightarrow> num \<Rightarrow> int \<Rightarrow> fm" where
  3328   "fp p n s j \<equiv> (if n > 0 then
  3329             (And p (And (Ge (CN 0 n (Sub s (Add (Floor s) (C j)))))
  3330                         (Lt (CN 0 n (Sub s (Add (Floor s) (C (j+1))))))))
  3331             else
  3332             (And p (And (Le (CN 0 (-n) (Add (Neg s) (Add (Floor s) (C j)))))
  3333                         (Gt (CN 0 (-n) (Add (Neg s) (Add (Floor s) (C (j + 1)))))))))"
  3334 
  3335   (* splits the bounded from the unbounded part*)
  3336 fun rsplit0 :: "num \<Rightarrow> (fm \<times> int \<times> num) list"
  3337 where
  3338   "rsplit0 (Bound 0) = [(T,1,C 0)]"
  3339 | "rsplit0 (Add a b) = (let acs = rsplit0 a ; bcs = rsplit0 b
  3340               in map (\<lambda> ((p,n,t),(q,m,s)). (And p q, n+m, Add t s)) [(a,b). a\<leftarrow>acs,b\<leftarrow>bcs])"
  3341 | "rsplit0 (Sub a b) = rsplit0 (Add a (Neg b))"
  3342 | "rsplit0 (Neg a) = map (\<lambda> (p,n,s). (p,-n,Neg s)) (rsplit0 a)"
  3343 | "rsplit0 (Floor a) = concat (map
  3344       (\<lambda> (p,n,s). if n=0 then [(p,0,Floor s)]
  3345           else (map (\<lambda> j. (fp p n s j, 0, Add (Floor s) (C j))) (if n > 0 then [0 .. n] else [n .. 0])))
  3346        (rsplit0 a))"
  3347 | "rsplit0 (CN 0 c a) = map (\<lambda> (p,n,s). (p,n+c,s)) (rsplit0 a)"
  3348 | "rsplit0 (CN m c a) = map (\<lambda> (p,n,s). (p,n,CN m c s)) (rsplit0 a)"
  3349 | "rsplit0 (CF c t s) = rsplit0 (Add (Mul c (Floor t)) s)"
  3350 | "rsplit0 (Mul c a) = map (\<lambda> (p,n,s). (p,c*n,Mul c s)) (rsplit0 a)"
  3351 | "rsplit0 t = [(T,0,t)]"
  3352 
  3353 lemma conj_rl[simp]: "isrlfm p \<Longrightarrow> isrlfm q \<Longrightarrow> isrlfm (conj p q)"
  3354   using conj_def by (cases p, auto)
  3355 lemma disj_rl[simp]: "isrlfm p \<Longrightarrow> isrlfm q \<Longrightarrow> isrlfm (disj p q)"
  3356   using disj_def by (cases p, auto)
  3357 
  3358 
  3359 lemma rsplit0_cs:
  3360   shows "\<forall> (p,n,s) \<in> set (rsplit0 t).
  3361   (Ifm (x#bs) p \<longrightarrow>  (Inum (x#bs) t = Inum (x#bs) (CN 0 n s))) \<and> numbound0 s \<and> isrlfm p"
  3362   (is "\<forall> (p,n,s) \<in> ?SS t. (?I p \<longrightarrow> ?N t = ?N (CN 0 n s)) \<and> _ \<and> _ ")
  3363 proof(induct t rule: rsplit0.induct)
  3364   case (5 a)
  3365   let ?p = "\<lambda> (p,n,s) j. fp p n s j"
  3366   let ?f = "(\<lambda> (p,n,s) j. (?p (p,n,s) j, (0::int),Add (Floor s) (C j)))"
  3367   let ?J = "\<lambda> n. if n>0 then [0..n] else [n..0]"
  3368   let ?ff=" (\<lambda> (p,n,s). if n= 0 then [(p,0,Floor s)] else map (?f (p,n,s)) (?J n))"
  3369   have int_cases: "\<forall> (i::int). i= 0 \<or> i < 0 \<or> i > 0" by arith
  3370   have U1: "(UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n=0} (\<lambda> (p,n,s). set (?ff (p,n,s)))) =
  3371     (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n=0} (\<lambda> (p,n,s). set [(p,0,Floor s)]))" by auto
  3372   have U2': "\<forall> (p,n,s) \<in> {(p,n,s). (p,n,s) \<in> ?SS a \<and> n>0}.
  3373     ?ff (p,n,s) = map (?f(p,n,s)) [0..n]" by auto
  3374   hence U2: "(UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n>0} (\<lambda> (p,n,s). set (?ff (p,n,s)))) =
  3375     (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n>0} (\<lambda> (p,n,s).
  3376     set (map (?f(p,n,s)) [0..n])))"
  3377   proof-
  3378     fix M :: "('a\<times>'b\<times>'c) set" and f :: "('a\<times>'b\<times>'c) \<Rightarrow> 'd list" and g
  3379     assume "\<forall> (a,b,c) \<in> M. f (a,b,c) = g a b c"
  3380     thus "(UNION M (\<lambda> (a,b,c). set (f (a,b,c)))) = (UNION M (\<lambda> (a,b,c). set (g a b c)))"
  3381       by (auto simp add: split_def)
  3382   qed
  3383   have U3': "\<forall> (p,n,s) \<in> {(p,n,s). (p,n,s) \<in> ?SS a \<and> n<0}. ?ff (p,n,s) = map (?f(p,n,s)) [n..0]"
  3384     by auto
  3385   hence U3: "(UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n<0} (\<lambda> (p,n,s). set (?ff (p,n,s)))) =
  3386     (UNION {(p,n,s). (p,n,s)\<in> ?SS a\<and>n<0} (\<lambda>(p,n,s). set (map (?f(p,n,s)) [n..0])))"
  3387       proof-
  3388     fix M :: "('a\<times>'b\<times>'c) set" and f :: "('a\<times>'b\<times>'c) \<Rightarrow> 'd list" and g
  3389     assume "\<forall> (a,b,c) \<in> M. f (a,b,c) = g a b c"
  3390     thus "(UNION M (\<lambda> (a,b,c). set (f (a,b,c)))) = (UNION M (\<lambda> (a,b,c). set (g a b c)))"
  3391       by (auto simp add: split_def)
  3392   qed
  3393   have "?SS (Floor a) = UNION (?SS a) (\<lambda>x. set (?ff x))"
  3394     by auto
  3395   also have "\<dots> = UNION (?SS a) (\<lambda> (p,n,s). set (?ff (p,n,s)))" by blast
  3396   also have "\<dots> =
  3397     ((UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n=0} (\<lambda> (p,n,s). set (?ff (p,n,s)))) Un
  3398     (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n>0} (\<lambda> (p,n,s). set (?ff (p,n,s)))) Un
  3399     (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n<0} (\<lambda> (p,n,s). set (?ff (p,n,s)))))"
  3400     by (auto split: if_splits)
  3401   also have "\<dots> =
  3402     ((UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n=0} (\<lambda> (p,n,s). set [(p,0,Floor s)])) Un
  3403    (UNION {(p,n,s). (p,n,s)\<in> ?SS a\<and>n>0} (\<lambda>(p,n,s). set(map(?f(p,n,s)) [0..n]))) Un
  3404    (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n<0} (\<lambda> (p,n,s).
  3405     set (map (?f(p,n,s)) [n..0]))))" by (simp only: U1 U2 U3)
  3406   also have "\<dots> =
  3407     ((UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n=0} (\<lambda> (p,n,s). {(p,0,Floor s)})) Un
  3408     (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n>0} (\<lambda> (p,n,s). (?f(p,n,s)) ` {0 .. n})) Un
  3409     (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n<0} (\<lambda> (p,n,s). (?f(p,n,s)) ` {n .. 0})))"
  3410     by (simp only: set_map set_upto list.set)
  3411   also have "\<dots> =
  3412     ((UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n=0} (\<lambda> (p,n,s). {(p,0,Floor s)})) Un
  3413     (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n>0} (\<lambda> (p,n,s). {?f(p,n,s) j| j. j\<in> {0 .. n}})) Un
  3414     (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n<0} (\<lambda> (p,n,s).  {?f(p,n,s) j| j. j\<in> {n .. 0}})))"
  3415     by blast
  3416   finally
  3417   have FS: "?SS (Floor a) =
  3418     ((UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n=0} (\<lambda> (p,n,s). {(p,0,Floor s)})) Un
  3419     (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n>0} (\<lambda> (p,n,s). {?f(p,n,s) j| j. j\<in> {0 .. n}})) Un
  3420     (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n<0} (\<lambda> (p,n,s).  {?f(p,n,s) j| j. j\<in> {n .. 0}})))"
  3421     by blast
  3422   show ?case
  3423   proof(simp only: FS, clarsimp simp del: Ifm.simps Inum.simps, -)
  3424     fix p n s
  3425     let ?ths = "(?I p \<longrightarrow> (?N (Floor a) = ?N (CN 0 n s))) \<and> numbound0 s \<and> isrlfm p"
  3426     assume "(\<exists>ba. (p, 0, ba) \<in> set (rsplit0 a) \<and> n = 0 \<and> s = Floor ba) \<or>
  3427        (\<exists>ab ac ba.
  3428            (ab, ac, ba) \<in> set (rsplit0 a) \<and>
  3429            0 < ac \<and>
  3430            (\<exists>j. p = fp ab ac ba j \<and>
  3431                 n = 0 \<and> s = Add (Floor ba) (C j) \<and> 0 \<le> j \<and> j \<le> ac)) \<or>
  3432        (\<exists>ab ac ba.
  3433            (ab, ac, ba) \<in> set (rsplit0 a) \<and>
  3434            ac < 0 \<and>
  3435            (\<exists>j. p = fp ab ac ba j \<and>
  3436                 n = 0 \<and> s = Add (Floor ba) (C j) \<and> ac \<le> j \<and> j \<le> 0))"
  3437     moreover
  3438     { fix s'
  3439       assume "(p, 0, s') \<in> ?SS a" and "n = 0" and "s = Floor s'"
  3440       hence ?ths using 5(1) by auto }
  3441     moreover
  3442     { fix p' n' s' j
  3443       assume pns: "(p', n', s') \<in> ?SS a"
  3444         and np: "0 < n'"
  3445         and p_def: "p = ?p (p',n',s') j"
  3446         and n0: "n = 0"
  3447         and s_def: "s = (Add (Floor s') (C j))"
  3448         and jp: "0 \<le> j" and jn: "j \<le> n'"
  3449       from 5 pns have H:"(Ifm ((x::real) # (bs::real list)) p' \<longrightarrow>
  3450           Inum (x # bs) a = Inum (x # bs) (CN 0 n' s')) \<and>
  3451           numbound0 s' \<and> isrlfm p'" by blast
  3452       hence nb: "numbound0 s'" by simp
  3453       from H have nf: "isrlfm (?p (p',n',s') j)" using fp_def np by simp
  3454       let ?nxs = "CN 0 n' s'"
  3455       let ?l = "\<lfloor>?N s'\<rfloor> + j"
  3456       from H
  3457       have "?I (?p (p',n',s') j) \<longrightarrow>
  3458           (((?N ?nxs \<ge> real_of_int ?l) \<and> (?N ?nxs < real_of_int (?l + 1))) \<and> (?N a = ?N ?nxs ))"
  3459         by (simp add: fp_def np algebra_simps)
  3460       also have "\<dots> \<longrightarrow> \<lfloor>?N ?nxs\<rfloor> = ?l \<and> ?N a = ?N ?nxs"
  3461         using floor_eq_iff[where x="?N ?nxs" and a="?l"] by simp
  3462       moreover
  3463       have "\<dots> \<longrightarrow> (?N (Floor a) = ?N ((Add (Floor s') (C j))))" by simp
  3464       ultimately have "?I (?p (p',n',s') j) \<longrightarrow> (?N (Floor a) = ?N ((Add (Floor s') (C j))))"
  3465         by blast
  3466       with s_def n0 p_def nb nf have ?ths by auto}
  3467     moreover
  3468     { fix p' n' s' j
  3469       assume pns: "(p', n', s') \<in> ?SS a"
  3470         and np: "n' < 0"
  3471         and p_def: "p = ?p (p',n',s') j"
  3472         and n0: "n = 0"
  3473         and s_def: "s = (Add (Floor s') (C j))"
  3474         and jp: "n' \<le> j" and jn: "j \<le> 0"
  3475       from 5 pns have H:"(Ifm ((x::real) # (bs::real list)) p' \<longrightarrow>
  3476           Inum (x # bs) a = Inum (x # bs) (CN 0 n' s')) \<and>
  3477           numbound0 s' \<and> isrlfm p'" by blast
  3478       hence nb: "numbound0 s'" by simp
  3479       from H have nf: "isrlfm (?p (p',n',s') j)" using fp_def np by simp
  3480       let ?nxs = "CN 0 n' s'"
  3481       let ?l = "\<lfloor>?N s'\<rfloor> + j"
  3482       from H
  3483       have "?I (?p (p',n',s') j) \<longrightarrow>
  3484           (((?N ?nxs \<ge> real_of_int ?l) \<and> (?N ?nxs < real_of_int (?l + 1))) \<and> (?N a = ?N ?nxs ))"
  3485         by (simp add: np fp_def algebra_simps)
  3486       also have "\<dots> \<longrightarrow> \<lfloor>?N ?nxs\<rfloor> = ?l \<and> ?N a = ?N ?nxs"
  3487         using floor_eq_iff[where x="?N ?nxs" and a="?l"] by simp
  3488       moreover
  3489       have "\<dots> \<longrightarrow> (?N (Floor a) = ?N ((Add (Floor s') (C j))))"  by simp
  3490       ultimately have "?I (?p (p',n',s') j) \<longrightarrow> (?N (Floor a) = ?N ((Add (Floor s') (C j))))"
  3491         by blast
  3492       with s_def n0 p_def nb nf have ?ths by auto}
  3493     ultimately show ?ths by fastforce
  3494   qed
  3495 next
  3496   case (3 a b) then show ?case
  3497     by auto
  3498 qed (auto simp add: Let_def split_def algebra_simps)
  3499 
  3500 lemma real_in_int_intervals:
  3501   assumes xb: "real_of_int m \<le> x \<and> x < real_of_int ((n::int) + 1)"
  3502   shows "\<exists> j\<in> {m.. n}. real_of_int j \<le> x \<and> x < real_of_int (j+1)" (is "\<exists> j\<in> ?N. ?P j")
  3503 by (rule bexI[where P="?P" and x="\<lfloor>x\<rfloor>" and A="?N"])
  3504 (auto simp add: floor_less_iff[where x="x" and z="n+1", simplified]
  3505   xb[simplified] floor_mono[where x="real_of_int m" and y="x", OF conjunct1[OF xb], simplified floor_of_int[where z="m"]])
  3506 
  3507 lemma rsplit0_complete:
  3508   assumes xp:"0 \<le> x" and x1:"x < 1"
  3509   shows "\<exists> (p,n,s) \<in> set (rsplit0 t). Ifm (x#bs) p" (is "\<exists> (p,n,s) \<in> ?SS t. ?I p")
  3510 proof(induct t rule: rsplit0.induct)
  3511   case (2 a b)
  3512   then have "\<exists> (pa,na,sa) \<in> ?SS a. ?I pa" by auto
  3513   then obtain "pa" "na" "sa" where pa: "(pa,na,sa)\<in> ?SS a \<and> ?I pa" by blast
  3514   with 2 have "\<exists> (pb,nb,sb) \<in> ?SS b. ?I pb" by blast
  3515   then obtain "pb" "nb" "sb" where pb: "(pb,nb,sb)\<in> ?SS b \<and> ?I pb" by blast
  3516   from pa pb have th: "((pa,na,sa),(pb,nb,sb)) \<in> set[(x,y). x\<leftarrow>rsplit0 a, y\<leftarrow>rsplit0 b]"
  3517     by (auto)
  3518   let ?f="(\<lambda> ((p,n,t),(q,m,s)). (And p q, n+m, Add t s))"
  3519   from imageI[OF th, where f="?f"] have "?f ((pa,na,sa),(pb,nb,sb)) \<in> ?SS (Add a b)"
  3520     by (simp add: Let_def)
  3521   hence "(And pa pb, na +nb, Add sa sb) \<in> ?SS (Add a b)" by simp
  3522   moreover from pa pb have "?I (And pa pb)" by simp
  3523   ultimately show ?case by blast
  3524 next
  3525   case (5 a)
  3526   let ?p = "\<lambda> (p,n,s) j. fp p n s j"
  3527   let ?f = "(\<lambda> (p,n,s) j. (?p (p,n,s) j, (0::int),(Add (Floor s) (C j))))"
  3528   let ?J = "\<lambda> n. if n>0 then [0..n] else [n..0]"
  3529   let ?ff=" (\<lambda> (p,n,s). if n= 0 then [(p,0,Floor s)] else map (?f (p,n,s)) (?J n))"
  3530   have int_cases: "\<forall> (i::int). i= 0 \<or> i < 0 \<or> i > 0" by arith
  3531   have U1: "(UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n=0} (\<lambda> (p,n,s). set (?ff (p,n,s)))) = (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n=0} (\<lambda> (p,n,s). set [(p,0,Floor s)]))" by auto
  3532   have U2': "\<forall> (p,n,s) \<in> {(p,n,s). (p,n,s) \<in> ?SS a \<and> n>0}. ?ff (p,n,s) = map (?f(p,n,s)) [0..n]"
  3533     by auto
  3534   hence U2: "(UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n>0} (\<lambda> (p,n,s). set (?ff (p,n,s)))) = (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n>0} (\<lambda> (p,n,s). set (map (?f(p,n,s)) [0..n])))"
  3535   proof-
  3536     fix M :: "('a\<times>'b\<times>'c) set" and f :: "('a\<times>'b\<times>'c) \<Rightarrow> 'd list" and g
  3537     assume "\<forall> (a,b,c) \<in> M. f (a,b,c) = g a b c"
  3538     thus "(UNION M (\<lambda> (a,b,c). set (f (a,b,c)))) = (UNION M (\<lambda> (a,b,c). set (g a b c)))"
  3539       by (auto simp add: split_def)
  3540   qed
  3541   have U3': "\<forall> (p,n,s) \<in> {(p,n,s). (p,n,s) \<in> ?SS a \<and> n<0}. ?ff (p,n,s) = map (?f(p,n,s)) [n..0]"
  3542     by auto
  3543   hence U3: "(UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n<0} (\<lambda> (p,n,s). set (?ff (p,n,s)))) = (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n<0} (\<lambda> (p,n,s). set (map (?f(p,n,s)) [n..0])))"
  3544   proof-
  3545     fix M :: "('a\<times>'b\<times>'c) set" and f :: "('a\<times>'b\<times>'c) \<Rightarrow> 'd list" and g
  3546     assume "\<forall> (a,b,c) \<in> M. f (a,b,c) = g a b c"
  3547     thus "(UNION M (\<lambda> (a,b,c). set (f (a,b,c)))) = (UNION M (\<lambda> (a,b,c). set (g a b c)))"
  3548       by (auto simp add: split_def)
  3549   qed
  3550 
  3551   have "?SS (Floor a) = UNION (?SS a) (\<lambda>x. set (?ff x))" by auto
  3552   also have "\<dots> = UNION (?SS a) (\<lambda> (p,n,s). set (?ff (p,n,s)))" by blast
  3553   also have "\<dots> =
  3554     ((UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n=0} (\<lambda> (p,n,s). set (?ff (p,n,s)))) Un
  3555     (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n>0} (\<lambda> (p,n,s). set (?ff (p,n,s)))) Un
  3556     (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n<0} (\<lambda> (p,n,s). set (?ff (p,n,s)))))"
  3557     by (auto split: if_splits)
  3558   also have "\<dots> =
  3559     ((UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n=0} (\<lambda> (p,n,s). set [(p,0,Floor s)])) Un
  3560     (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n>0} (\<lambda> (p,n,s). set (map (?f(p,n,s)) [0..n]))) Un
  3561     (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n<0} (\<lambda> (p,n,s). set (map (?f(p,n,s)) [n..0]))))"
  3562     by (simp only: U1 U2 U3)
  3563   also have "\<dots> =
  3564     ((UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n=0} (\<lambda> (p,n,s). {(p,0,Floor s)})) Un
  3565     (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n>0} (\<lambda> (p,n,s). (?f(p,n,s)) ` {0 .. n})) Un
  3566     (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n<0} (\<lambda> (p,n,s). (?f(p,n,s)) ` {n .. 0})))"
  3567     by (simp only: set_map set_upto list.set)
  3568   also have "\<dots> =
  3569     ((UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n=0} (\<lambda> (p,n,s). {(p,0,Floor s)})) Un
  3570     (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n>0} (\<lambda> (p,n,s). {?f(p,n,s) j| j. j\<in> {0 .. n}})) Un
  3571     (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n<0} (\<lambda> (p,n,s).  {?f(p,n,s) j| j. j\<in> {n .. 0}})))"
  3572     by blast
  3573   finally
  3574   have FS: "?SS (Floor a) =
  3575     ((UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n=0} (\<lambda> (p,n,s). {(p,0,Floor s)})) Un
  3576     (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n>0} (\<lambda> (p,n,s). {?f(p,n,s) j| j. j\<in> {0 .. n}})) Un
  3577     (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n<0} (\<lambda> (p,n,s).  {?f(p,n,s) j| j. j\<in> {n .. 0}})))"
  3578     by blast
  3579   from 5 have "\<exists> (p,n,s) \<in> ?SS a. ?I p" by auto
  3580   then obtain "p" "n" "s" where pns: "(p,n,s) \<in> ?SS a \<and> ?I p" by blast
  3581   let ?N = "\<lambda> t. Inum (x#bs) t"
  3582   from rsplit0_cs[rule_format] pns have ans:"(?N a = ?N (CN 0 n s)) \<and> numbound0 s \<and> isrlfm p"
  3583     by auto
  3584 
  3585   have "n=0 \<or> n >0 \<or> n <0" by arith
  3586   moreover {assume "n=0" hence ?case using pns by (simp only: FS) auto }
  3587   moreover
  3588   {
  3589     assume np: "n > 0"
  3590     from of_int_floor_le[of "?N s"] have "?N (Floor s) \<le> ?N s" by simp
  3591     also from mult_left_mono[OF xp] np have "?N s \<le> real_of_int n * x + ?N s" by simp
  3592     finally have "?N (Floor s) \<le> real_of_int n * x + ?N s" .
  3593     moreover
  3594     {from x1 np have "real_of_int n *x + ?N s < real_of_int n + ?N s" by simp
  3595       also from real_of_int_floor_add_one_gt[where r="?N s"]
  3596       have "\<dots> < real_of_int n + ?N (Floor s) + 1" by simp
  3597       finally have "real_of_int n *x + ?N s < ?N (Floor s) + real_of_int (n+1)" by simp}
  3598     ultimately have "?N (Floor s) \<le> real_of_int n *x + ?N s\<and> real_of_int n *x + ?N s < ?N (Floor s) + real_of_int (n+1)" by simp
  3599     hence th: "0 \<le> real_of_int n *x + ?N s - ?N (Floor s) \<and> real_of_int n *x + ?N s - ?N (Floor s) < real_of_int (n+1)" by simp
  3600     from real_in_int_intervals th have  "\<exists> j\<in> {0 .. n}. real_of_int j \<le> real_of_int n *x + ?N s - ?N (Floor s)\<and> real_of_int n *x + ?N s - ?N (Floor s) < real_of_int (j+1)" by simp
  3601 
  3602     hence "\<exists> j\<in> {0 .. n}. 0 \<le> real_of_int n *x + ?N s - ?N (Floor s) - real_of_int j \<and> real_of_int n *x + ?N s - ?N (Floor s) - real_of_int (j+1) < 0"
  3603       by(simp only: myle[of _ "real_of_int n * x + Inum (x # bs) s - Inum (x # bs) (Floor s)"] less_iff_diff_less_0[where a="real_of_int n *x + ?N s - ?N (Floor s)"])
  3604     hence "\<exists> j\<in> {0.. n}. ?I (?p (p,n,s) j)"
  3605       using pns by (simp add: fp_def np algebra_simps)
  3606     then obtain "j" where j_def: "j\<in> {0 .. n} \<and> ?I (?p (p,n,s) j)" by blast
  3607     hence "\<exists>x \<in> {?p (p,n,s) j |j. 0\<le> j \<and> j \<le> n }. ?I x" by auto
  3608     hence ?case using pns
  3609       by (simp only: FS,simp add: bex_Un)
  3610     (rule disjI2, rule disjI1,rule exI [where x="p"],
  3611       rule exI [where x="n"],rule exI [where x="s"],simp_all add: np)
  3612   }
  3613   moreover
  3614   { assume nn: "n < 0" hence np: "-n >0" by simp
  3615     from of_int_floor_le[of "?N s"] have "?N (Floor s) + 1 > ?N s" by simp
  3616     moreover from mult_left_mono_neg[OF xp] nn have "?N s \<ge> real_of_int n * x + ?N s" by simp
  3617     ultimately have "?N (Floor s) + 1 > real_of_int n * x + ?N s" by arith
  3618     moreover
  3619     {from x1 nn have "real_of_int n *x + ?N s \<ge> real_of_int n + ?N s" by simp
  3620       moreover from of_int_floor_le[of "?N s"]  have "real_of_int n + ?N s \<ge> real_of_int n + ?N (Floor s)" by simp
  3621       ultimately have "real_of_int n *x + ?N s \<ge> ?N (Floor s) + real_of_int n"
  3622         by (simp only: algebra_simps)}
  3623     ultimately have "?N (Floor s) + real_of_int n \<le> real_of_int n *x + ?N s\<and> real_of_int n *x + ?N s < ?N (Floor s) + real_of_int (1::int)" by simp
  3624     hence th: "real_of_int n \<le> real_of_int n *x + ?N s - ?N (Floor s) \<and> real_of_int n *x + ?N s - ?N (Floor s) < real_of_int (1::int)" by simp
  3625     have th1: "\<forall> (a::real). (- a > 0) = (a < 0)" by auto
  3626     have th2: "\<forall> (a::real). (0 \<ge> - a) = (a \<ge> 0)" by auto
  3627     from real_in_int_intervals th  have  "\<exists> j\<in> {n .. 0}. real_of_int j \<le> real_of_int n *x + ?N s - ?N (Floor s)\<and> real_of_int n *x + ?N s - ?N (Floor s) < real_of_int (j+1)" by simp
  3628 
  3629     hence "\<exists> j\<in> {n .. 0}. 0 \<le> real_of_int n *x + ?N s - ?N (Floor s) - real_of_int j \<and> real_of_int n *x + ?N s - ?N (Floor s) - real_of_int (j+1) < 0"
  3630       by(simp only: myle[of _ "real_of_int n * x + Inum (x # bs) s - Inum (x # bs) (Floor s)"] less_iff_diff_less_0[where a="real_of_int n *x + ?N s - ?N (Floor s)"])
  3631     hence "\<exists> j\<in> {n .. 0}. 0 \<ge> - (real_of_int n *x + ?N s - ?N (Floor s) - real_of_int j) \<and> - (real_of_int n *x + ?N s - ?N (Floor s) - real_of_int (j+1)) > 0" by (simp only: th1[rule_format] th2[rule_format])
  3632     hence "\<exists> j\<in> {n.. 0}. ?I (?p (p,n,s) j)"
  3633       using pns by (simp add: fp_def nn algebra_simps
  3634         del: diff_less_0_iff_less diff_le_0_iff_le)
  3635     then obtain "j" where j_def: "j\<in> {n .. 0} \<and> ?I (?p (p,n,s) j)" by blast
  3636     hence "\<exists>x \<in> {?p (p,n,s) j |j. n\<le> j \<and> j \<le> 0 }. ?I x" by auto
  3637     hence ?case using pns
  3638       by (simp only: FS,simp add: bex_Un)
  3639     (rule disjI2, rule disjI2,rule exI [where x="p"],
  3640       rule exI [where x="n"],rule exI [where x="s"],simp_all add: nn)
  3641   }
  3642   ultimately show ?case by blast
  3643 qed (auto simp add: Let_def split_def)
  3644 
  3645     (* Linearize a formula where Bound 0 ranges over [0,1) *)
  3646 
  3647 definition rsplit :: "(int \<Rightarrow> num \<Rightarrow> fm) \<Rightarrow> num \<Rightarrow> fm" where
  3648   "rsplit f a \<equiv> foldr disj (map (\<lambda> (\<phi>, n, s). conj \<phi> (f n s)) (rsplit0 a)) F"
  3649 
  3650 lemma foldr_disj_map: "Ifm bs (foldr disj (map f xs) F) = (\<exists> x \<in> set xs. Ifm bs (f x))"
  3651 by(induct xs, simp_all)
  3652 
  3653 lemma foldr_conj_map: "Ifm bs (foldr conj (map f xs) T) = (\<forall> x \<in> set xs. Ifm bs (f x))"
  3654 by(induct xs, simp_all)
  3655 
  3656 lemma foldr_disj_map_rlfm:
  3657   assumes lf: "\<forall> n s. numbound0 s \<longrightarrow> isrlfm (f n s)"
  3658   and \<phi>: "\<forall> (\<phi>,n,s) \<in> set xs. numbound0 s \<and> isrlfm \<phi>"
  3659   shows "isrlfm (foldr disj (map (\<lambda> (\<phi>, n, s). conj \<phi> (f n s)) xs) F)"
  3660 using lf \<phi> by (induct xs, auto)
  3661 
  3662 lemma rsplit_ex: "Ifm bs (rsplit f a) = (\<exists> (\<phi>,n,s) \<in> set (rsplit0 a). Ifm bs (conj \<phi> (f n s)))"
  3663 using foldr_disj_map[where xs="rsplit0 a"] rsplit_def by (simp add: split_def)
  3664 
  3665 lemma rsplit_l: assumes lf: "\<forall> n s. numbound0 s \<longrightarrow> isrlfm (f n s)"
  3666   shows "isrlfm (rsplit f a)"
  3667 proof-
  3668   from rsplit0_cs[where t="a"] have th: "\<forall> (\<phi>,n,s) \<in> set (rsplit0 a). numbound0 s \<and> isrlfm \<phi>" by blast
  3669   from foldr_disj_map_rlfm[OF lf th] rsplit_def show ?thesis by simp
  3670 qed
  3671 
  3672 lemma rsplit:
  3673   assumes xp: "x \<ge> 0" and x1: "x < 1"
  3674   and f: "\<forall> a n s. Inum (x#bs) a = Inum (x#bs) (CN 0 n s) \<and> numbound0 s \<longrightarrow> (Ifm (x#bs) (f n s) = Ifm (x#bs) (g a))"
  3675   shows "Ifm (x#bs) (rsplit f a) = Ifm (x#bs) (g a)"
  3676 proof(auto)
  3677   let ?I = "\<lambda>x p. Ifm (x#bs) p"
  3678   let ?N = "\<lambda> x t. Inum (x#bs) t"
  3679   assume "?I x (rsplit f a)"
  3680   hence "\<exists> (\<phi>,n,s) \<in> set (rsplit0 a). ?I x (And \<phi> (f n s))" using rsplit_ex by simp
  3681   then obtain "\<phi>" "n" "s" where fnsS:"(\<phi>,n,s) \<in> set (rsplit0 a)" and "?I x (And \<phi> (f n s))" by blast
  3682   hence \<phi>: "?I x \<phi>" and fns: "?I x (f n s)" by auto
  3683   from rsplit0_cs[where t="a" and bs="bs" and x="x", rule_format, OF fnsS] \<phi>
  3684   have th: "(?N x a = ?N x (CN 0 n s)) \<and> numbound0 s" by auto
  3685   from f[rule_format, OF th] fns show "?I x (g a)" by simp
  3686 next
  3687   let ?I = "\<lambda>x p. Ifm (x#bs) p"
  3688   let ?N = "\<lambda> x t. Inum (x#bs) t"
  3689   assume ga: "?I x (g a)"
  3690   from rsplit0_complete[OF xp x1, where bs="bs" and t="a"]
  3691   obtain "\<phi>" "n" "s" where fnsS:"(\<phi>,n,s) \<in> set (rsplit0 a)" and fx: "?I x \<phi>" by blast
  3692   from rsplit0_cs[where t="a" and x="x" and bs="bs"] fnsS fx
  3693   have ans: "?N x a = ?N x (CN 0 n s)" and nb: "numbound0 s" by auto
  3694   with ga f have "?I x (f n s)" by auto
  3695   with rsplit_ex fnsS fx show "?I x (rsplit f a)" by auto
  3696 qed
  3697 
  3698 definition lt :: "int \<Rightarrow> num \<Rightarrow> fm" where
  3699   lt_def: "lt c t = (if c = 0 then (Lt t) else if c > 0 then (Lt (CN 0 c t))
  3700                         else (Gt (CN 0 (-c) (Neg t))))"
  3701 
  3702 definition  le :: "int \<Rightarrow> num \<Rightarrow> fm" where
  3703   le_def: "le c t = (if c = 0 then (Le t) else if c > 0 then (Le (CN 0 c t))
  3704                         else (Ge (CN 0 (-c) (Neg t))))"
  3705 
  3706 definition  gt :: "int \<Rightarrow> num \<Rightarrow> fm" where
  3707   gt_def: "gt c t = (if c = 0 then (Gt t) else if c > 0 then (Gt (CN 0 c t))
  3708                         else (Lt (CN 0 (-c) (Neg t))))"
  3709 
  3710 definition  ge :: "int \<Rightarrow> num \<Rightarrow> fm" where
  3711   ge_def: "ge c t = (if c = 0 then (Ge t) else if c > 0 then (Ge (CN 0 c t))
  3712                         else (Le (CN 0 (-c) (Neg t))))"
  3713 
  3714 definition  eq :: "int \<Rightarrow> num \<Rightarrow> fm" where
  3715   eq_def: "eq c t = (if c = 0 then (Eq t) else if c > 0 then (Eq (CN 0 c t))
  3716                         else (Eq (CN 0 (-c) (Neg t))))"
  3717 
  3718 definition neq :: "int \<Rightarrow> num \<Rightarrow> fm" where
  3719   neq_def: "neq c t = (if c = 0 then (NEq t) else if c > 0 then (NEq (CN 0 c t))
  3720                         else (NEq (CN 0 (-c) (Neg t))))"
  3721 
  3722 lemma lt_mono: "\<forall> a n s. Inum (x#bs) a = Inum (x#bs) (CN 0 n s) \<and> numbound0 s \<longrightarrow> Ifm (x#bs) (lt n s) = Ifm (x#bs) (Lt a)"
  3723   (is "\<forall> a n s . ?N a = ?N (CN 0 n s) \<and> _\<longrightarrow> ?I (lt n s) = ?I (Lt a)")
  3724 proof(clarify)
  3725   fix a n s
  3726   assume H: "?N a = ?N (CN 0 n s)"
  3727   show "?I (lt n s) = ?I (Lt a)" using H by (cases "n=0", (simp add: lt_def))
  3728   (cases "n > 0", simp_all add: lt_def algebra_simps myless[of _ "0"])
  3729 qed
  3730 
  3731 lemma lt_l: "isrlfm (rsplit lt a)"
  3732   by (rule rsplit_l[where f="lt" and a="a"], auto simp add: lt_def,
  3733     case_tac s, simp_all, rename_tac nat a b, case_tac "nat", simp_all)
  3734 
  3735 lemma le_mono: "\<forall> a n s. Inum (x#bs) a = Inum (x#bs) (CN 0 n s) \<and> numbound0 s \<longrightarrow> Ifm (x#bs) (le n s) = Ifm (x#bs) (Le a)" (is "\<forall> a n s. ?N a = ?N (CN 0 n s) \<and> _ \<longrightarrow> ?I (le n s) = ?I (Le a)")
  3736 proof(clarify)
  3737   fix a n s
  3738   assume H: "?N a = ?N (CN 0 n s)"
  3739   show "?I (le n s) = ?I (Le a)" using H by (cases "n=0", (simp add: le_def))
  3740   (cases "n > 0", simp_all add: le_def algebra_simps myle[of _ "0"])
  3741 qed
  3742 
  3743 lemma le_l: "isrlfm (rsplit le a)"
  3744   by (rule rsplit_l[where f="le" and a="a"], auto simp add: le_def)
  3745 (case_tac s, simp_all, rename_tac nat a b, case_tac "nat",simp_all)
  3746 
  3747 lemma gt_mono: "\<forall> a n s. Inum (x#bs) a = Inum (x#bs) (CN 0 n s) \<and> numbound0 s \<longrightarrow> Ifm (x#bs) (gt n s) = Ifm (x#bs) (Gt a)" (is "\<forall> a n s. ?N a = ?N (CN 0 n s) \<and> _ \<longrightarrow> ?I (gt n s) = ?I (Gt a)")
  3748 proof(clarify)
  3749   fix a n s
  3750   assume H: "?N a = ?N (CN 0 n s)"
  3751   show "?I (gt n s) = ?I (Gt a)" using H by (cases "n=0", (simp add: gt_def))
  3752   (cases "n > 0", simp_all add: gt_def algebra_simps myless[of _ "0"])
  3753 qed
  3754 lemma gt_l: "isrlfm (rsplit gt a)"
  3755   by (rule rsplit_l[where f="gt" and a="a"], auto simp add: gt_def)
  3756 (case_tac s, simp_all, rename_tac nat a b, case_tac "nat", simp_all)
  3757 
  3758 lemma ge_mono: "\<forall> a n s. Inum (x#bs) a = Inum (x#bs) (CN 0 n s) \<and> numbound0 s \<longrightarrow> Ifm (x#bs) (ge n s) = Ifm (x#bs) (Ge a)" (is "\<forall> a n s . ?N a = ?N (CN 0 n s) \<and> _ \<longrightarrow> ?I (ge n s) = ?I (Ge a)")
  3759 proof(clarify)
  3760   fix a n s
  3761   assume H: "?N a = ?N (CN 0 n s)"
  3762   show "?I (ge n s) = ?I (Ge a)" using H by (cases "n=0", (simp add: ge_def))
  3763   (cases "n > 0", simp_all add: ge_def algebra_simps myle[of _ "0"])
  3764 qed
  3765 lemma ge_l: "isrlfm (rsplit ge a)"
  3766   by (rule rsplit_l[where f="ge" and a="a"], auto simp add: ge_def)
  3767 (case_tac s, simp_all, rename_tac nat a b, case_tac "nat", simp_all)
  3768 
  3769 lemma eq_mono: "\<forall> a n s. Inum (x#bs) a = Inum (x#bs) (CN 0 n s) \<and> numbound0 s \<longrightarrow> Ifm (x#bs) (eq n s) = Ifm (x#bs) (Eq a)" (is "\<forall> a n s. ?N a = ?N (CN 0 n s) \<and> _ \<longrightarrow> ?I (eq n s) = ?I (Eq a)")
  3770 proof(clarify)
  3771   fix a n s
  3772   assume H: "?N a = ?N (CN 0 n s)"
  3773   show "?I (eq n s) = ?I (Eq a)" using H by (auto simp add: eq_def algebra_simps)
  3774 qed
  3775 lemma eq_l: "isrlfm (rsplit eq a)"
  3776   by (rule rsplit_l[where f="eq" and a="a"], auto simp add: eq_def)
  3777 (case_tac s, simp_all, rename_tac nat a b, case_tac"nat", simp_all)
  3778 
  3779 lemma neq_mono: "\<forall> a n s. Inum (x#bs) a = Inum (x#bs) (CN 0 n s) \<and> numbound0 s \<longrightarrow> Ifm (x#bs) (neq n s) = Ifm (x#bs) (NEq a)" (is "\<forall> a n s. ?N a = ?N (CN 0 n s) \<and> _ \<longrightarrow> ?I (neq n s) = ?I (NEq a)")
  3780 proof(clarify)
  3781   fix a n s bs
  3782   assume H: "?N a = ?N (CN 0 n s)"
  3783   show "?I (neq n s) = ?I (NEq a)" using H by (auto simp add: neq_def algebra_simps)
  3784 qed
  3785 
  3786 lemma neq_l: "isrlfm (rsplit neq a)"
  3787   by (rule rsplit_l[where f="neq" and a="a"], auto simp add: neq_def)
  3788 (case_tac s, simp_all, rename_tac nat a b, case_tac"nat", simp_all)
  3789 
  3790 lemma small_le:
  3791   assumes u0:"0 \<le> u" and u1: "u < 1"
  3792   shows "(-u \<le> real_of_int (n::int)) = (0 \<le> n)"
  3793 using u0 u1  by auto
  3794 
  3795 lemma small_lt:
  3796   assumes u0:"0 \<le> u" and u1: "u < 1"
  3797   shows "(real_of_int (n::int) < real_of_int (m::int) - u) = (n < m)"
  3798 using u0 u1  by auto
  3799 
  3800 lemma rdvd01_cs:
  3801   assumes up: "u \<ge> 0" and u1: "u<1" and np: "real_of_int n > 0"
  3802   shows "(real_of_int (i::int) rdvd real_of_int (n::int) * u - s) = (\<exists> j\<in> {0 .. n - 1}. real_of_int n * u = s - real_of_int \<lfloor>s\<rfloor> + real_of_int j \<and> real_of_int i rdvd real_of_int (j - \<lfloor>s\<rfloor>))" (is "?lhs = ?rhs")
  3803 proof-
  3804   let ?ss = "s - real_of_int \<lfloor>s\<rfloor>"
  3805   from real_of_int_floor_add_one_gt[where r="s", simplified myless[of "s"]]
  3806     of_int_floor_le  have ss0:"?ss \<ge> 0" and ss1:"?ss < 1" by (auto simp: floor_less_cancel)
  3807   from np have n0: "real_of_int n \<ge> 0" by simp
  3808   from mult_left_mono[OF up n0] mult_strict_left_mono[OF u1 np]
  3809   have nu0:"real_of_int n * u - s \<ge> -s" and nun:"real_of_int n * u -s < real_of_int n - s" by auto
  3810   from int_rdvd_real[where i="i" and x="real_of_int (n::int) * u - s"]
  3811   have "real_of_int i rdvd real_of_int n * u - s =
  3812     (i dvd \<lfloor>real_of_int n * u - s\<rfloor> \<and> (real_of_int \<lfloor>real_of_int n * u - s\<rfloor> = real_of_int n * u - s ))"
  3813     (is "_ = (?DE)" is "_ = (?D \<and> ?E)") by simp
  3814   also have "\<dots> = (?DE \<and> real_of_int (\<lfloor>real_of_int n * u - s\<rfloor> + \<lfloor>s\<rfloor>) \<ge> -?ss
  3815     \<and> real_of_int (\<lfloor>real_of_int n * u - s\<rfloor> + \<lfloor>s\<rfloor>) < real_of_int n - ?ss)" (is "_=(?DE \<and>real_of_int ?a \<ge> _ \<and> real_of_int ?a < _)")
  3816     using nu0 nun  by auto
  3817   also have "\<dots> = (?DE \<and> ?a \<ge> 0 \<and> ?a < n)" by(simp only: small_le[OF ss0 ss1] small_lt[OF ss0 ss1])
  3818   also have "\<dots> = (?DE \<and> (\<exists> j\<in> {0 .. (n - 1)}. ?a = j))" by simp
  3819   also have "\<dots> = (?DE \<and> (\<exists> j\<in> {0 .. (n - 1)}. real_of_int (\<lfloor>real_of_int n * u - s\<rfloor>) = real_of_int j - real_of_int \<lfloor>s\<rfloor> ))"
  3820     by (simp only: algebra_simps of_int_diff[symmetric] of_int_eq_iff)
  3821   also have "\<dots> = ((\<exists> j\<in> {0 .. (n - 1)}. real_of_int n * u - s = real_of_int j - real_of_int \<lfloor>s\<rfloor> \<and> real_of_int i rdvd real_of_int n * u - s))" using int_rdvd_iff[where i="i" and t="\<lfloor>real_of_int n * u - s\<rfloor>"]
  3822     by (auto cong: conj_cong)
  3823   also have "\<dots> = ?rhs" by(simp cong: conj_cong) (simp add: algebra_simps )
  3824   finally show ?thesis .
  3825 qed
  3826 
  3827 definition
  3828   DVDJ:: "int \<Rightarrow> int \<Rightarrow> num \<Rightarrow> fm"
  3829 where
  3830   DVDJ_def: "DVDJ i n s = (foldr disj (map (\<lambda> j. conj (Eq (CN 0 n (Add s (Sub (Floor (Neg s)) (C j))))) (Dvd i (Sub (C j) (Floor (Neg s))))) [0..n - 1]) F)"
  3831 
  3832 definition
  3833   NDVDJ:: "int \<Rightarrow> int \<Rightarrow> num \<Rightarrow> fm"
  3834 where
  3835   NDVDJ_def: "NDVDJ i n s = (foldr conj (map (\<lambda> j. disj (NEq (CN 0 n (Add s (Sub (Floor (Neg s)) (C j))))) (NDvd i (Sub (C j) (Floor (Neg s))))) [0..n - 1]) T)"
  3836 
  3837 lemma DVDJ_DVD:
  3838   assumes xp:"x\<ge> 0" and x1: "x < 1" and np:"real_of_int n > 0"
  3839   shows "Ifm (x#bs) (DVDJ i n s) = Ifm (x#bs) (Dvd i (CN 0 n s))"
  3840 proof-
  3841   let ?f = "\<lambda> j. conj (Eq(CN 0 n (Add s (Sub(Floor (Neg s)) (C j))))) (Dvd i (Sub (C j) (Floor (Neg s))))"
  3842   let ?s= "Inum (x#bs) s"
  3843   from foldr_disj_map[where xs="[0..n - 1]" and bs="x#bs" and f="?f"]
  3844   have "Ifm (x#bs) (DVDJ i n s) = (\<exists> j\<in> {0 .. (n - 1)}. Ifm (x#bs) (?f j))"
  3845     by (simp add: np DVDJ_def)
  3846   also have "\<dots> = (\<exists> j\<in> {0 .. (n - 1)}. real_of_int n * x = (- ?s) - real_of_int \<lfloor>- ?s\<rfloor> + real_of_int j \<and> real_of_int i rdvd real_of_int (j - \<lfloor>- ?s\<rfloor>))"
  3847     by (simp add: algebra_simps)
  3848   also from rdvd01_cs[OF xp x1 np, where i="i" and s="-?s"]
  3849   have "\<dots> = (real_of_int i rdvd real_of_int n * x - (-?s))" by simp
  3850   finally show ?thesis by simp
  3851 qed
  3852 
  3853 lemma NDVDJ_NDVD:
  3854   assumes xp:"x\<ge> 0" and x1: "x < 1" and np:"real_of_int n > 0"
  3855   shows "Ifm (x#bs) (NDVDJ i n s) = Ifm (x#bs) (NDvd i (CN 0 n s))"
  3856 proof-
  3857   let ?f = "\<lambda> j. disj(NEq(CN 0 n (Add s (Sub (Floor (Neg s)) (C j))))) (NDvd i (Sub (C j) (Floor(Neg s))))"
  3858   let ?s= "Inum (x#bs) s"
  3859   from foldr_conj_map[where xs="[0..n - 1]" and bs="x#bs" and f="?f"]
  3860   have "Ifm (x#bs) (NDVDJ i n s) = (\<forall> j\<in> {0 .. (n - 1)}. Ifm (x#bs) (?f j))"
  3861     by (simp add: np NDVDJ_def)
  3862   also have "\<dots> = (\<not> (\<exists> j\<in> {0 .. (n - 1)}. real_of_int n * x = (- ?s) - real_of_int \<lfloor>- ?s\<rfloor> + real_of_int j \<and> real_of_int i rdvd real_of_int (j - \<lfloor>- ?s\<rfloor>)))"
  3863     by (simp add: algebra_simps)
  3864   also from rdvd01_cs[OF xp x1 np, where i="i" and s="-?s"]
  3865   have "\<dots> = (\<not> (real_of_int i rdvd real_of_int n * x - (-?s)))" by simp
  3866   finally show ?thesis by simp
  3867 qed
  3868 
  3869 lemma foldr_disj_map_rlfm2:
  3870   assumes lf: "\<forall> n . isrlfm (f n)"
  3871   shows "isrlfm (foldr disj (map f xs) F)"
  3872 using lf by (induct xs, auto)
  3873 lemma foldr_And_map_rlfm2:
  3874   assumes lf: "\<forall> n . isrlfm (f n)"
  3875   shows "isrlfm (foldr conj (map f xs) T)"
  3876 using lf by (induct xs, auto)
  3877 
  3878 lemma DVDJ_l: assumes ip: "i >0" and np: "n>0" and nb: "numbound0 s"
  3879   shows "isrlfm (DVDJ i n s)"
  3880 proof-
  3881   let ?f="\<lambda>j. conj (Eq (CN 0 n (Add s (Sub (Floor (Neg s)) (C j)))))
  3882                          (Dvd i (Sub (C j) (Floor (Neg s))))"
  3883   have th: "\<forall> j. isrlfm (?f j)" using nb np by auto
  3884   from DVDJ_def foldr_disj_map_rlfm2[OF th] show ?thesis by simp
  3885 qed
  3886 
  3887 lemma NDVDJ_l: assumes ip: "i >0" and np: "n>0" and nb: "numbound0 s"
  3888   shows "isrlfm (NDVDJ i n s)"
  3889 proof-
  3890   let ?f="\<lambda>j. disj (NEq (CN 0 n (Add s (Sub (Floor (Neg s)) (C j)))))
  3891                       (NDvd i (Sub (C j) (Floor (Neg s))))"
  3892   have th: "\<forall> j. isrlfm (?f j)" using nb np by auto
  3893   from NDVDJ_def foldr_And_map_rlfm2[OF th] show ?thesis by auto
  3894 qed
  3895 
  3896 definition DVD :: "int \<Rightarrow> int \<Rightarrow> num \<Rightarrow> fm" where
  3897   DVD_def: "DVD i c t =
  3898   (if i=0 then eq c t else
  3899   if c = 0 then (Dvd i t) else if c >0 then DVDJ \<bar>i\<bar> c t else DVDJ \<bar>i\<bar> (-c) (Neg t))"
  3900 
  3901 definition  NDVD :: "int \<Rightarrow> int \<Rightarrow> num \<Rightarrow> fm" where
  3902   "NDVD i c t =
  3903   (if i=0 then neq c t else
  3904   if c = 0 then (NDvd i t) else if c >0 then NDVDJ \<bar>i\<bar> c t else NDVDJ \<bar>i\<bar> (-c) (Neg t))"
  3905 
  3906 lemma DVD_mono:
  3907   assumes xp: "0\<le> x" and x1: "x < 1"
  3908   shows "\<forall> a n s. Inum (x#bs) a = Inum (x#bs) (CN 0 n s) \<and> numbound0 s \<longrightarrow> Ifm (x#bs) (DVD i n s) = Ifm (x#bs) (Dvd i a)"
  3909   (is "\<forall> a n s. ?N a = ?N (CN 0 n s) \<and> _ \<longrightarrow> ?I (DVD i n s) = ?I (Dvd i a)")
  3910 proof(clarify)
  3911   fix a n s
  3912   assume H: "?N a = ?N (CN 0 n s)" and nb: "numbound0 s"
  3913   let ?th = "?I (DVD i n s) = ?I (Dvd i a)"
  3914   have "i=0 \<or> (i\<noteq>0 \<and> n=0) \<or> (i\<noteq>0 \<and> n < 0) \<or> (i\<noteq>0 \<and> n > 0)" by arith
  3915   moreover {assume iz: "i=0" hence ?th using eq_mono[rule_format, OF conjI[OF H nb]]
  3916       by (simp add: DVD_def rdvd_left_0_eq)}
  3917   moreover {assume inz: "i\<noteq>0" and "n=0" hence ?th by (simp add: H DVD_def) }
  3918   moreover {assume inz: "i\<noteq>0" and "n<0" hence ?th
  3919       by (simp add: DVD_def H DVDJ_DVD[OF xp x1] rdvd_abs1
  3920         rdvd_minus[where d="i" and t="real_of_int n * x + Inum (x # bs) s"]) }
  3921   moreover {assume inz: "i\<noteq>0" and "n>0" hence ?th by (simp add:DVD_def H DVDJ_DVD[OF xp x1] rdvd_abs1)}
  3922   ultimately show ?th by blast
  3923 qed
  3924 
  3925 lemma NDVD_mono:   assumes xp: "0\<le> x" and x1: "x < 1"
  3926   shows "\<forall> a n s. Inum (x#bs) a = Inum (x#bs) (CN 0 n s) \<and> numbound0 s \<longrightarrow> Ifm (x#bs) (NDVD i n s) = Ifm (x#bs) (NDvd i a)"
  3927   (is "\<forall> a n s. ?N a = ?N (CN 0 n s) \<and> _ \<longrightarrow> ?I (NDVD i n s) = ?I (NDvd i a)")
  3928 proof(clarify)
  3929   fix a n s
  3930   assume H: "?N a = ?N (CN 0 n s)" and nb: "numbound0 s"
  3931   let ?th = "?I (NDVD i n s) = ?I (NDvd i a)"
  3932   have "i=0 \<or> (i\<noteq>0 \<and> n=0) \<or> (i\<noteq>0 \<and> n < 0) \<or> (i\<noteq>0 \<and> n > 0)" by arith
  3933   moreover {assume iz: "i=0" hence ?th using neq_mono[rule_format, OF conjI[OF H nb]]
  3934       by (simp add: NDVD_def rdvd_left_0_eq)}
  3935   moreover {assume inz: "i\<noteq>0" and "n=0" hence ?th by (simp add: H NDVD_def) }
  3936   moreover {assume inz: "i\<noteq>0" and "n<0" hence ?th
  3937       by (simp add: NDVD_def H NDVDJ_NDVD[OF xp x1] rdvd_abs1
  3938         rdvd_minus[where d="i" and t="real_of_int n * x + Inum (x # bs) s"]) }
  3939   moreover {assume inz: "i\<noteq>0" and "n>0" hence ?th
  3940       by (simp add:NDVD_def H NDVDJ_NDVD[OF xp x1] rdvd_abs1)}
  3941   ultimately show ?th by blast
  3942 qed
  3943 
  3944 lemma DVD_l: "isrlfm (rsplit (DVD i) a)"
  3945   by (rule rsplit_l[where f="DVD i" and a="a"], auto simp add: DVD_def eq_def DVDJ_l)
  3946 (case_tac s, simp_all, rename_tac nat a b, case_tac "nat", simp_all)
  3947 
  3948 lemma NDVD_l: "isrlfm (rsplit (NDVD i) a)"
  3949   by (rule rsplit_l[where f="NDVD i" and a="a"], auto simp add: NDVD_def neq_def NDVDJ_l)
  3950 (case_tac s, simp_all, rename_tac nat a b, case_tac "nat", simp_all)
  3951 
  3952 fun rlfm :: "fm \<Rightarrow> fm"
  3953 where
  3954   "rlfm (And p q) = conj (rlfm p) (rlfm q)"
  3955 | "rlfm (Or p q) = disj (rlfm p) (rlfm q)"
  3956 | "rlfm (Imp p q) = disj (rlfm (NOT p)) (rlfm q)"
  3957 | "rlfm (Iff p q) = disj (conj(rlfm p) (rlfm q)) (conj(rlfm (NOT p)) (rlfm (NOT q)))"
  3958 | "rlfm (Lt a) = rsplit lt a"
  3959 | "rlfm (Le a) = rsplit le a"
  3960 | "rlfm (Gt a) = rsplit gt a"
  3961 | "rlfm (Ge a) = rsplit ge a"
  3962 | "rlfm (Eq a) = rsplit eq a"
  3963 | "rlfm (NEq a) = rsplit neq a"
  3964 | "rlfm (Dvd i a) = rsplit (\<lambda> t. DVD i t) a"
  3965 | "rlfm (NDvd i a) = rsplit (\<lambda> t. NDVD i t) a"
  3966 | "rlfm (NOT (And p q)) = disj (rlfm (NOT p)) (rlfm (NOT q))"
  3967 | "rlfm (NOT (Or p q)) = conj (rlfm (NOT p)) (rlfm (NOT q))"
  3968 | "rlfm (NOT (Imp p q)) = conj (rlfm p) (rlfm (NOT q))"
  3969 | "rlfm (NOT (Iff p q)) = disj (conj(rlfm p) (rlfm(NOT q))) (conj(rlfm(NOT p)) (rlfm q))"
  3970 | "rlfm (NOT (NOT p)) = rlfm p"
  3971 | "rlfm (NOT T) = F"
  3972 | "rlfm (NOT F) = T"
  3973 | "rlfm (NOT (Lt a)) = simpfm (rlfm (Ge a))"
  3974 | "rlfm (NOT (Le a)) = simpfm (rlfm (Gt a))"
  3975 | "rlfm (NOT (Gt a)) = simpfm (rlfm (Le a))"
  3976 | "rlfm (NOT (Ge a)) = simpfm (rlfm (Lt a))"
  3977 | "rlfm (NOT (Eq a)) = simpfm (rlfm (NEq a))"
  3978 | "rlfm (NOT (NEq a)) = simpfm (rlfm (Eq a))"
  3979 | "rlfm (NOT (Dvd i a)) = simpfm (rlfm (NDvd i a))"
  3980 | "rlfm (NOT (NDvd i a)) = simpfm (rlfm (Dvd i a))"
  3981 | "rlfm p = p"
  3982 
  3983 lemma bound0at_l : "\<lbrakk>isatom p ; bound0 p\<rbrakk> \<Longrightarrow> isrlfm p"
  3984   by (induct p rule: isrlfm.induct, auto)
  3985 
  3986 lemma simpfm_rl: "isrlfm p \<Longrightarrow> isrlfm (simpfm p)"
  3987 proof (induct p)
  3988   case (Lt a)
  3989   hence "bound0 (Lt a) \<or> (\<exists> c e. a = CN 0 c e \<and> c > 0 \<and> numbound0 e)"
  3990     by (cases a,simp_all, rename_tac nat a b, case_tac "nat", simp_all)
  3991   moreover
  3992   {assume "bound0 (Lt a)" hence bn:"bound0 (simpfm (Lt a))"
  3993       using simpfm_bound0 by blast
  3994     have "isatom (simpfm (Lt a))" by (cases "simpnum a", auto simp add: Let_def)
  3995     with bn bound0at_l have ?case by blast}
  3996   moreover
  3997   { fix c e assume a: "a = CN 0 c e" and "c>0" and "numbound0 e"
  3998     { assume cn1:"numgcd (CN 0 c (simpnum e)) \<noteq> 1" and cnz:"numgcd (CN 0 c (simpnum e)) \<noteq> 0"
  3999       with numgcd_pos[where t="CN 0 c (simpnum e)"]
  4000       have th1:"numgcd (CN 0 c (simpnum e)) > 0" by simp
  4001       from \<open>c > 0\<close> have th:"numgcd (CN 0 c (simpnum e)) \<le> c"
  4002         by (simp add: numgcd_def)
  4003       from \<open>c > 0\<close> have th': "c\<noteq>0" by auto
  4004       from \<open>c > 0\<close> have cp: "c \<ge> 0" by simp
  4005       from zdiv_mono2[OF cp th1 th, simplified div_self[OF th']]
  4006       have "0 < c div numgcd (CN 0 c (simpnum e))" by simp
  4007     }
  4008     with Lt a have ?case
  4009       by (simp add: Let_def reducecoeff_def reducecoeffh_numbound0)}
  4010   ultimately show ?case by blast
  4011 next
  4012   case (Le a)
  4013   hence "bound0 (Le a) \<or> (\<exists> c e. a = CN 0 c e \<and> c > 0 \<and> numbound0 e)"
  4014     by (cases a,simp_all, rename_tac nat a b, case_tac "nat", simp_all)
  4015   moreover
  4016   { assume "bound0 (Le a)" hence bn:"bound0 (simpfm (Le a))"
  4017       using simpfm_bound0 by blast
  4018     have "isatom (simpfm (Le a))" by (cases "simpnum a", auto simp add: Let_def)
  4019     with bn bound0at_l have ?case by blast}
  4020   moreover
  4021   { fix c e assume a: "a = CN 0 c e" and "c>0" and "numbound0 e"
  4022     { assume cn1:"numgcd (CN 0 c (simpnum e)) \<noteq> 1" and cnz:"numgcd (CN 0 c (simpnum e)) \<noteq> 0"
  4023       with numgcd_pos[where t="CN 0 c (simpnum e)"]
  4024       have th1:"numgcd (CN 0 c (simpnum e)) > 0" by simp
  4025       from \<open>c > 0\<close> have th:"numgcd (CN 0 c (simpnum e)) \<le> c"
  4026         by (simp add: numgcd_def)
  4027       from \<open>c > 0\<close> have th': "c\<noteq>0" by auto
  4028       from \<open>c > 0\<close> have cp: "c \<ge> 0" by simp
  4029       from zdiv_mono2[OF cp th1 th, simplified div_self[OF th']]
  4030       have "0 < c div numgcd (CN 0 c (simpnum e))" by simp
  4031     }
  4032     with Le a have ?case
  4033       by (simp add: Let_def reducecoeff_def reducecoeffh_numbound0)}
  4034   ultimately show ?case by blast
  4035 next
  4036   case (Gt a)
  4037   hence "bound0 (Gt a) \<or> (\<exists> c e. a = CN 0 c e \<and> c > 0 \<and> numbound0 e)"
  4038     by (cases a, simp_all, rename_tac nat a b,case_tac "nat", simp_all)
  4039   moreover
  4040   {assume "bound0 (Gt a)" hence bn:"bound0 (simpfm (Gt a))"
  4041       using simpfm_bound0 by blast
  4042     have "isatom (simpfm (Gt a))" by (cases "simpnum a", auto simp add: Let_def)
  4043     with bn bound0at_l have ?case by blast}
  4044   moreover
  4045   { fix c e assume a: "a = CN 0 c e" and "c>0" and "numbound0 e"
  4046     { assume cn1: "numgcd (CN 0 c (simpnum e)) \<noteq> 1" and cnz:"numgcd (CN 0 c (simpnum e)) \<noteq> 0"
  4047       with numgcd_pos[where t="CN 0 c (simpnum e)"]
  4048       have th1:"numgcd (CN 0 c (simpnum e)) > 0" by simp
  4049       from \<open>c > 0\<close> have th:"numgcd (CN 0 c (simpnum e)) \<le> c"
  4050         by (simp add: numgcd_def)
  4051       from \<open>c > 0\<close> have th': "c\<noteq>0" by auto
  4052       from \<open>c > 0\<close> have cp: "c \<ge> 0" by simp
  4053       from zdiv_mono2[OF cp th1 th, simplified div_self[OF th']]
  4054       have "0 < c div numgcd (CN 0 c (simpnum e))" by simp
  4055     }
  4056     with Gt a have ?case
  4057       by (simp add: Let_def reducecoeff_def reducecoeffh_numbound0)}
  4058   ultimately show ?case by blast
  4059 next
  4060   case (Ge a)
  4061   hence "bound0 (Ge a) \<or> (\<exists> c e. a = CN 0 c e \<and> c > 0 \<and> numbound0 e)"
  4062     by (cases a,simp_all, rename_tac nat a b, case_tac "nat", simp_all)
  4063   moreover
  4064   { assume "bound0 (Ge a)" hence bn:"bound0 (simpfm (Ge a))"
  4065       using simpfm_bound0 by blast
  4066     have "isatom (simpfm (Ge a))" by (cases "simpnum a", auto simp add: Let_def)
  4067     with bn bound0at_l have ?case by blast}
  4068   moreover
  4069   { fix c e assume a: "a = CN 0 c e" and "c>0" and "numbound0 e"
  4070     { assume cn1:"numgcd (CN 0 c (simpnum e)) \<noteq> 1" and cnz:"numgcd (CN 0 c (simpnum e)) \<noteq> 0"
  4071       with numgcd_pos[where t="CN 0 c (simpnum e)"]
  4072       have th1:"numgcd (CN 0 c (simpnum e)) > 0" by simp
  4073       from \<open>c > 0\<close> have th:"numgcd (CN 0 c (simpnum e)) \<le> c"
  4074         by (simp add: numgcd_def)
  4075       from \<open>c > 0\<close> have th': "c\<noteq>0" by auto
  4076       from \<open>c > 0\<close> have cp: "c \<ge> 0" by simp
  4077       from zdiv_mono2[OF cp th1 th, simplified div_self[OF th']]
  4078       have "0 < c div numgcd (CN 0 c (simpnum e))" by simp
  4079     }
  4080     with Ge a have ?case
  4081       by (simp add: Let_def reducecoeff_def reducecoeffh_numbound0)}
  4082   ultimately show ?case by blast
  4083 next
  4084   case (Eq a)
  4085   hence "bound0 (Eq a) \<or> (\<exists> c e. a = CN 0 c e \<and> c > 0 \<and> numbound0 e)"
  4086     by (cases a,simp_all, rename_tac nat a b, case_tac "nat", simp_all)
  4087   moreover
  4088   { assume "bound0 (Eq a)" hence bn:"bound0 (simpfm (Eq a))"
  4089       using simpfm_bound0 by blast
  4090     have "isatom (simpfm (Eq a))" by (cases "simpnum a", auto simp add: Let_def)
  4091     with bn bound0at_l have ?case by blast}
  4092   moreover
  4093   { fix c e assume a: "a = CN 0 c e" and "c>0" and "numbound0 e"
  4094     { assume cn1:"numgcd (CN 0 c (simpnum e)) \<noteq> 1" and cnz:"numgcd (CN 0 c (simpnum e)) \<noteq> 0"
  4095       with numgcd_pos[where t="CN 0 c (simpnum e)"]
  4096       have th1:"numgcd (CN 0 c (simpnum e)) > 0" by simp
  4097       from \<open>c > 0\<close> have th:"numgcd (CN 0 c (simpnum e)) \<le> c"
  4098         by (simp add: numgcd_def)
  4099       from \<open>c > 0\<close> have th': "c\<noteq>0" by auto
  4100       from \<open>c > 0\<close> have cp: "c \<ge> 0" by simp
  4101       from zdiv_mono2[OF cp th1 th, simplified div_self[OF th']]
  4102       have "0 < c div numgcd (CN 0 c (simpnum e))" by simp
  4103     }
  4104     with Eq a have ?case
  4105       by (simp add: Let_def reducecoeff_def reducecoeffh_numbound0)}
  4106   ultimately show ?case by blast
  4107 next
  4108   case (NEq a)
  4109   hence "bound0 (NEq a) \<or> (\<exists> c e. a = CN 0 c e \<and> c > 0 \<and> numbound0 e)"
  4110     by (cases a,simp_all, rename_tac nat a b, case_tac "nat", simp_all)
  4111   moreover
  4112   {assume "bound0 (NEq a)" hence bn:"bound0 (simpfm (NEq a))"
  4113       using simpfm_bound0 by blast
  4114     have "isatom (simpfm (NEq a))" by (cases "simpnum a", auto simp add: Let_def)
  4115     with bn bound0at_l have ?case by blast}
  4116   moreover
  4117   { fix c e assume a: "a = CN 0 c e" and "c>0" and "numbound0 e"
  4118     { assume cn1:"numgcd (CN 0 c (simpnum e)) \<noteq> 1" and cnz:"numgcd (CN 0 c (simpnum e)) \<noteq> 0"
  4119       with numgcd_pos[where t="CN 0 c (simpnum e)"]
  4120       have th1:"numgcd (CN 0 c (simpnum e)) > 0" by simp
  4121       from \<open>c > 0\<close> have th:"numgcd (CN 0 c (simpnum e)) \<le> c"
  4122         by (simp add: numgcd_def)
  4123       from \<open>c > 0\<close> have th': "c\<noteq>0" by auto
  4124       from \<open>c > 0\<close> have cp: "c \<ge> 0" by simp
  4125       from zdiv_mono2[OF cp th1 th, simplified div_self[OF th']]
  4126       have "0 < c div numgcd (CN 0 c (simpnum e))" by simp
  4127     }
  4128     with NEq a have ?case
  4129       by (simp add: Let_def reducecoeff_def reducecoeffh_numbound0)}
  4130   ultimately show ?case by blast
  4131 next
  4132   case (Dvd i a) hence "bound0 (Dvd i a)" by auto hence bn:"bound0 (simpfm (Dvd i a))"
  4133     using simpfm_bound0 by blast
  4134   have "isatom (simpfm (Dvd i a))" by (cases "simpnum a", auto simp add: Let_def split_def)
  4135   with bn bound0at_l show ?case by blast
  4136 next
  4137   case (NDvd i a)  hence "bound0 (NDvd i a)" by auto hence bn:"bound0 (simpfm (NDvd i a))"
  4138     using simpfm_bound0 by blast
  4139   have "isatom (simpfm (NDvd i a))" by (cases "simpnum a", auto simp add: Let_def split_def)
  4140   with bn bound0at_l show ?case by blast
  4141 qed(auto simp add: conj_def imp_def disj_def iff_def Let_def)
  4142 
  4143 lemma rlfm_I:
  4144   assumes qfp: "qfree p"
  4145   and xp: "0 \<le> x" and x1: "x < 1"
  4146   shows "(Ifm (x#bs) (rlfm p) = Ifm (x# bs) p) \<and> isrlfm (rlfm p)"
  4147   using qfp
  4148 by (induct p rule: rlfm.induct)
  4149 (auto simp add: rsplit[OF xp x1 lt_mono] lt_l rsplit[OF xp x1 le_mono] le_l rsplit[OF xp x1 gt_mono] gt_l
  4150                rsplit[OF xp x1 ge_mono] ge_l rsplit[OF xp x1 eq_mono] eq_l rsplit[OF xp x1 neq_mono] neq_l
  4151                rsplit[OF xp x1 DVD_mono[OF xp x1]] DVD_l rsplit[OF xp x1 NDVD_mono[OF xp x1]] NDVD_l simpfm_rl)
  4152 lemma rlfm_l:
  4153   assumes qfp: "qfree p"
  4154   shows "isrlfm (rlfm p)"
  4155   using qfp lt_l gt_l ge_l le_l eq_l neq_l DVD_l NDVD_l
  4156 by (induct p rule: rlfm.induct) (auto simp add: simpfm_rl)
  4157 
  4158     (* Operations needed for Ferrante and Rackoff *)
  4159 lemma rminusinf_inf:
  4160   assumes lp: "isrlfm p"
  4161   shows "\<exists> z. \<forall> x < z. Ifm (x#bs) (minusinf p) = Ifm (x#bs) p" (is "\<exists> z. \<forall> x. ?P z x p")
  4162 using lp
  4163 proof (induct p rule: minusinf.induct)
  4164   case (1 p q) thus ?case by (auto,rule_tac x= "min z za" in exI) auto
  4165 next
  4166   case (2 p q) thus ?case by (auto,rule_tac x= "min z za" in exI) auto
  4167 next
  4168   case (3 c e)
  4169   from 3 have nb: "numbound0 e" by simp
  4170   from 3 have cp: "real_of_int c > 0" by simp
  4171   fix a
  4172   let ?e="Inum (a#bs) e"
  4173   let ?z = "(- ?e) / real_of_int c"
  4174   {fix x
  4175     assume xz: "x < ?z"
  4176     hence "(real_of_int c * x < - ?e)"
  4177       by (simp only: pos_less_divide_eq[OF cp, where a="x" and b="- ?e"] ac_simps)
  4178     hence "real_of_int c * x + ?e < 0" by arith
  4179     hence "real_of_int c * x + ?e \<noteq> 0" by simp
  4180     with xz have "?P ?z x (Eq (CN 0 c e))"
  4181       using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp  }
  4182   hence "\<forall> x < ?z. ?P ?z x (Eq (CN 0 c e))" by simp
  4183   thus ?case by blast
  4184 next
  4185   case (4 c e)
  4186   from 4 have nb: "numbound0 e" by simp
  4187   from 4 have cp: "real_of_int c > 0" by simp
  4188   fix a
  4189   let ?e="Inum (a#bs) e"
  4190   let ?z = "(- ?e) / real_of_int c"
  4191   {fix x
  4192     assume xz: "x < ?z"
  4193     hence "(real_of_int c * x < - ?e)"
  4194       by (simp only: pos_less_divide_eq[OF cp, where a="x" and b="- ?e"] ac_simps)
  4195     hence "real_of_int c * x + ?e < 0" by arith
  4196     hence "real_of_int c * x + ?e \<noteq> 0" by simp
  4197     with xz have "?P ?z x (NEq (CN 0 c e))"
  4198       using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp }
  4199   hence "\<forall> x < ?z. ?P ?z x (NEq (CN 0 c e))" by simp
  4200   thus ?case by blast
  4201 next
  4202   case (5 c e)
  4203   from 5 have nb: "numbound0 e" by simp
  4204   from 5 have cp: "real_of_int c > 0" by simp
  4205   fix a
  4206   let ?e="Inum (a#bs) e"
  4207   let ?z = "(- ?e) / real_of_int c"
  4208   {fix x
  4209     assume xz: "x < ?z"
  4210     hence "(real_of_int c * x < - ?e)"
  4211       by (simp only: pos_less_divide_eq[OF cp, where a="x" and b="- ?e"] ac_simps)
  4212     hence "real_of_int c * x + ?e < 0" by arith
  4213     with xz have "?P ?z x (Lt (CN 0 c e))"
  4214       using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"]  by simp }
  4215   hence "\<forall> x < ?z. ?P ?z x (Lt (CN 0 c e))" by simp
  4216   thus ?case by blast
  4217 next
  4218   case (6 c e)
  4219   from 6 have nb: "numbound0 e" by simp
  4220   from 6 have cp: "real_of_int c > 0" by simp
  4221   fix a
  4222   let ?e="Inum (a#bs) e"
  4223   let ?z = "(- ?e) / real_of_int c"
  4224   {fix x
  4225     assume xz: "x < ?z"
  4226     hence "(real_of_int c * x < - ?e)"
  4227       by (simp only: pos_less_divide_eq[OF cp, where a="x" and b="- ?e"] ac_simps)
  4228     hence "real_of_int c * x + ?e < 0" by arith
  4229     with xz have "?P ?z x (Le (CN 0 c e))"
  4230       using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp }
  4231   hence "\<forall> x < ?z. ?P ?z x (Le (CN 0 c e))" by simp
  4232   thus ?case by blast
  4233 next
  4234   case (7 c e)
  4235   from 7 have nb: "numbound0 e" by simp
  4236   from 7 have cp: "real_of_int c > 0" by simp
  4237   fix a
  4238   let ?e="Inum (a#bs) e"
  4239   let ?z = "(- ?e) / real_of_int c"
  4240   {fix x
  4241     assume xz: "x < ?z"
  4242     hence "(real_of_int c * x < - ?e)"
  4243       by (simp only: pos_less_divide_eq[OF cp, where a="x" and b="- ?e"] ac_simps)
  4244     hence "real_of_int c * x + ?e < 0" by arith
  4245     with xz have "?P ?z x (Gt (CN 0 c e))"
  4246       using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp }
  4247   hence "\<forall> x < ?z. ?P ?z x (Gt (CN 0 c e))" by simp
  4248   thus ?case by blast
  4249 next
  4250   case (8 c e)
  4251   from 8 have nb: "numbound0 e" by simp
  4252   from 8 have cp: "real_of_int c > 0" by simp
  4253   fix a
  4254   let ?e="Inum (a#bs) e"
  4255   let ?z = "(- ?e) / real_of_int c"
  4256   {fix x
  4257     assume xz: "x < ?z"
  4258     hence "(real_of_int c * x < - ?e)"
  4259       by (simp only: pos_less_divide_eq[OF cp, where a="x" and b="- ?e"] ac_simps)
  4260     hence "real_of_int c * x + ?e < 0" by arith
  4261     with xz have "?P ?z x (Ge (CN 0 c e))"
  4262       using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp }
  4263   hence "\<forall> x < ?z. ?P ?z x (Ge (CN 0 c e))" by simp
  4264   thus ?case by blast
  4265 qed simp_all
  4266 
  4267 lemma rplusinf_inf:
  4268   assumes lp: "isrlfm p"
  4269   shows "\<exists> z. \<forall> x > z. Ifm (x#bs) (plusinf p) = Ifm (x#bs) p" (is "\<exists> z. \<forall> x. ?P z x p")
  4270 using lp
  4271 proof (induct p rule: isrlfm.induct)
  4272   case (1 p q) thus ?case by (auto,rule_tac x= "max z za" in exI) auto
  4273 next
  4274   case (2 p q) thus ?case by (auto,rule_tac x= "max z za" in exI) auto
  4275 next
  4276   case (3 c e)
  4277   from 3 have nb: "numbound0 e" by simp
  4278   from 3 have cp: "real_of_int c > 0" by simp
  4279   fix a
  4280   let ?e="Inum (a#bs) e"
  4281   let ?z = "(- ?e) / real_of_int c"
  4282   {fix x
  4283     assume xz: "x > ?z"
  4284     with mult_strict_right_mono [OF xz cp] cp
  4285     have "(real_of_int c * x > - ?e)" by (simp add: ac_simps)
  4286     hence "real_of_int c * x + ?e > 0" by arith
  4287     hence "real_of_int c * x + ?e \<noteq> 0" by simp
  4288     with xz have "?P ?z x (Eq (CN 0 c e))"
  4289       using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp }
  4290   hence "\<forall> x > ?z. ?P ?z x (Eq (CN 0 c e))" by simp
  4291   thus ?case by blast
  4292 next
  4293   case (4 c e)
  4294   from 4 have nb: "numbound0 e" by simp
  4295   from 4 have cp: "real_of_int c > 0" by simp
  4296   fix a
  4297   let ?e="Inum (a#bs) e"
  4298   let ?z = "(- ?e) / real_of_int c"
  4299   {fix x
  4300     assume xz: "x > ?z"
  4301     with mult_strict_right_mono [OF xz cp] cp
  4302     have "(real_of_int c * x > - ?e)" by (simp add: ac_simps)
  4303     hence "real_of_int c * x + ?e > 0" by arith
  4304     hence "real_of_int c * x + ?e \<noteq> 0" by simp
  4305     with xz have "?P ?z x (NEq (CN 0 c e))"
  4306       using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp }
  4307   hence "\<forall> x > ?z. ?P ?z x (NEq (CN 0 c e))" by simp
  4308   thus ?case by blast
  4309 next
  4310   case (5 c e)
  4311   from 5 have nb: "numbound0 e" by simp
  4312   from 5 have cp: "real_of_int c > 0" by simp
  4313   fix a
  4314   let ?e="Inum (a#bs) e"
  4315   let ?z = "(- ?e) / real_of_int c"
  4316   {fix x
  4317     assume xz: "x > ?z"
  4318     with mult_strict_right_mono [OF xz cp] cp
  4319     have "(real_of_int c * x > - ?e)" by (simp add: ac_simps)
  4320     hence "real_of_int c * x + ?e > 0" by arith
  4321     with xz have "?P ?z x (Lt (CN 0 c e))"
  4322       using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp }
  4323   hence "\<forall> x > ?z. ?P ?z x (Lt (CN 0 c e))" by simp
  4324   thus ?case by blast
  4325 next
  4326   case (6 c e)
  4327   from 6 have nb: "numbound0 e" by simp
  4328   from 6 have cp: "real_of_int c > 0" by simp
  4329   fix a
  4330   let ?e="Inum (a#bs) e"
  4331   let ?z = "(- ?e) / real_of_int c"
  4332   {fix x
  4333     assume xz: "x > ?z"
  4334     with mult_strict_right_mono [OF xz cp] cp
  4335     have "(real_of_int c * x > - ?e)" by (simp add: ac_simps)
  4336     hence "real_of_int c * x + ?e > 0" by arith
  4337     with xz have "?P ?z x (Le (CN 0 c e))"
  4338       using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp }
  4339   hence "\<forall> x > ?z. ?P ?z x (Le (CN 0 c e))" by simp
  4340   thus ?case by blast
  4341 next
  4342   case (7 c e)
  4343   from 7 have nb: "numbound0 e" by simp
  4344   from 7 have cp: "real_of_int c > 0" by simp
  4345   fix a
  4346   let ?e="Inum (a#bs) e"
  4347   let ?z = "(- ?e) / real_of_int c"
  4348   {fix x
  4349     assume xz: "x > ?z"
  4350     with mult_strict_right_mono [OF xz cp] cp
  4351     have "(real_of_int c * x > - ?e)" by (simp add: ac_simps)
  4352     hence "real_of_int c * x + ?e > 0" by arith
  4353     with xz have "?P ?z x (Gt (CN 0 c e))"
  4354       using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp }
  4355   hence "\<forall> x > ?z. ?P ?z x (Gt (CN 0 c e))" by simp
  4356   thus ?case by blast
  4357 next
  4358   case (8 c e)
  4359   from 8 have nb: "numbound0 e" by simp
  4360   from 8 have cp: "real_of_int c > 0" by simp
  4361   fix a
  4362   let ?e="Inum (a#bs) e"
  4363   let ?z = "(- ?e) / real_of_int c"
  4364   {fix x
  4365     assume xz: "x > ?z"
  4366     with mult_strict_right_mono [OF xz cp] cp
  4367     have "(real_of_int c * x > - ?e)" by (simp add: ac_simps)
  4368     hence "real_of_int c * x + ?e > 0" by arith
  4369     with xz have "?P ?z x (Ge (CN 0 c e))"
  4370       using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"]   by simp }
  4371   hence "\<forall> x > ?z. ?P ?z x (Ge (CN 0 c e))" by simp
  4372   thus ?case by blast
  4373 qed simp_all
  4374 
  4375 lemma rminusinf_bound0:
  4376   assumes lp: "isrlfm p"
  4377   shows "bound0 (minusinf p)"
  4378   using lp
  4379   by (induct p rule: minusinf.induct) simp_all
  4380 
  4381 lemma rplusinf_bound0:
  4382   assumes lp: "isrlfm p"
  4383   shows "bound0 (plusinf p)"
  4384   using lp
  4385   by (induct p rule: plusinf.induct) simp_all
  4386 
  4387 lemma rminusinf_ex:
  4388   assumes lp: "isrlfm p"
  4389   and ex: "Ifm (a#bs) (minusinf p)"
  4390   shows "\<exists> x. Ifm (x#bs) p"
  4391 proof-
  4392   from bound0_I [OF rminusinf_bound0[OF lp], where b="a" and bs ="bs"] ex
  4393   have th: "\<forall> x. Ifm (x#bs) (minusinf p)" by auto
  4394   from rminusinf_inf[OF lp, where bs="bs"]
  4395   obtain z where z_def: "\<forall>x<z. Ifm (x # bs) (minusinf p) = Ifm (x # bs) p" by blast
  4396   from th have "Ifm ((z - 1)#bs) (minusinf p)" by simp
  4397   moreover have "z - 1 < z" by simp
  4398   ultimately show ?thesis using z_def by auto
  4399 qed
  4400 
  4401 lemma rplusinf_ex:
  4402   assumes lp: "isrlfm p"
  4403   and ex: "Ifm (a#bs) (plusinf p)"
  4404   shows "\<exists> x. Ifm (x#bs) p"
  4405 proof-
  4406   from bound0_I [OF rplusinf_bound0[OF lp], where b="a" and bs ="bs"] ex
  4407   have th: "\<forall> x. Ifm (x#bs) (plusinf p)" by auto
  4408   from rplusinf_inf[OF lp, where bs="bs"]
  4409   obtain z where z_def: "\<forall>x>z. Ifm (x # bs) (plusinf p) = Ifm (x # bs) p" by blast
  4410   from th have "Ifm ((z + 1)#bs) (plusinf p)" by simp
  4411   moreover have "z + 1 > z" by simp
  4412   ultimately show ?thesis using z_def by auto
  4413 qed
  4414 
  4415 fun \<Upsilon>:: "fm \<Rightarrow> (num \<times> int) list"
  4416 where
  4417   "\<Upsilon> (And p q) = (\<Upsilon> p @ \<Upsilon> q)"
  4418 | "\<Upsilon> (Or p q) = (\<Upsilon> p @ \<Upsilon> q)"
  4419 | "\<Upsilon> (Eq  (CN 0 c e)) = [(Neg e,c)]"
  4420 | "\<Upsilon> (NEq (CN 0 c e)) = [(Neg e,c)]"
  4421 | "\<Upsilon> (Lt  (CN 0 c e)) = [(Neg e,c)]"
  4422 | "\<Upsilon> (Le  (CN 0 c e)) = [(Neg e,c)]"
  4423 | "\<Upsilon> (Gt  (CN 0 c e)) = [(Neg e,c)]"
  4424 | "\<Upsilon> (Ge  (CN 0 c e)) = [(Neg e,c)]"
  4425 | "\<Upsilon> p = []"
  4426 
  4427 fun \<upsilon> :: "fm \<Rightarrow> num \<times> int \<Rightarrow> fm"
  4428 where
  4429   "\<upsilon> (And p q) = (\<lambda> (t,n). And (\<upsilon> p (t,n)) (\<upsilon> q (t,n)))"
  4430 | "\<upsilon> (Or p q) = (\<lambda> (t,n). Or (\<upsilon> p (t,n)) (\<upsilon> q (t,n)))"
  4431 | "\<upsilon> (Eq (CN 0 c e)) = (\<lambda> (t,n). Eq (Add (Mul c t) (Mul n e)))"
  4432 | "\<upsilon> (NEq (CN 0 c e)) = (\<lambda> (t,n). NEq (Add (Mul c t) (Mul n e)))"
  4433 | "\<upsilon> (Lt (CN 0 c e)) = (\<lambda> (t,n). Lt (Add (Mul c t) (Mul n e)))"
  4434 | "\<upsilon> (Le (CN 0 c e)) = (\<lambda> (t,n). Le (Add (Mul c t) (Mul n e)))"
  4435 | "\<upsilon> (Gt (CN 0 c e)) = (\<lambda> (t,n). Gt (Add (Mul c t) (Mul n e)))"
  4436 | "\<upsilon> (Ge (CN 0 c e)) = (\<lambda> (t,n). Ge (Add (Mul c t) (Mul n e)))"
  4437 | "\<upsilon> p = (\<lambda> (t,n). p)"
  4438 
  4439 lemma \<upsilon>_I: assumes lp: "isrlfm p"
  4440   and np: "real_of_int n > 0" and nbt: "numbound0 t"
  4441   shows "(Ifm (x#bs) (\<upsilon> p (t,n)) = Ifm (((Inum (x#bs) t)/(real_of_int n))#bs) p) \<and> bound0 (\<upsilon> p (t,n))" (is "(?I x (\<upsilon> p (t,n)) = ?I ?u p) \<and> ?B p" is "(_ = ?I (?t/?n) p) \<and> _" is "(_ = ?I (?N x t /_) p) \<and> _")
  4442   using lp
  4443 proof(induct p rule: \<upsilon>.induct)
  4444   case (5 c e)
  4445   from 5 have cp: "c >0" and nb: "numbound0 e" by simp_all
  4446   have "?I ?u (Lt (CN 0 c e)) = (real_of_int c *(?t/?n) + (?N x e) < 0)"
  4447     using numbound0_I[OF nb, where bs="bs" and b="?u" and b'="x"] by simp
  4448   also have "\<dots> = (?n*(real_of_int c *(?t/?n)) + ?n*(?N x e) < 0)"
  4449     by (simp only: pos_less_divide_eq[OF np, where a="real_of_int c *(?t/?n) + (?N x e)"
  4450       and b="0", simplified div_0]) (simp only: algebra_simps)
  4451   also have "\<dots> = (real_of_int c *?t + ?n* (?N x e) < 0)"
  4452     using np by simp
  4453   finally show ?case using nbt nb by (simp add: algebra_simps)
  4454 next
  4455   case (6 c e)
  4456   from 6 have cp: "c >0" and nb: "numbound0 e" by simp_all
  4457   have "?I ?u (Le (CN 0 c e)) = (real_of_int c *(?t/?n) + (?N x e) \<le> 0)"
  4458     using numbound0_I[OF nb, where bs="bs" and b="?u" and b'="x"] by simp
  4459   also have "\<dots> = (?n*(real_of_int c *(?t/?n)) + ?n*(?N x e) \<le> 0)"
  4460     by (simp only: pos_le_divide_eq[OF np, where a="real_of_int c *(?t/?n) + (?N x e)"
  4461       and b="0", simplified div_0]) (simp only: algebra_simps)
  4462   also have "\<dots> = (real_of_int c *?t + ?n* (?N x e) \<le> 0)"
  4463     using np by simp
  4464   finally show ?case using nbt nb by (simp add: algebra_simps)
  4465 next
  4466   case (7 c e)
  4467   from 7 have cp: "c >0" and nb: "numbound0 e" by simp_all
  4468   have "?I ?u (Gt (CN 0 c e)) = (real_of_int c *(?t/?n) + (?N x e) > 0)"
  4469     using numbound0_I[OF nb, where bs="bs" and b="?u" and b'="x"] by simp
  4470   also have "\<dots> = (?n*(real_of_int c *(?t/?n)) + ?n*(?N x e) > 0)"
  4471     by (simp only: pos_divide_less_eq[OF np, where a="real_of_int c *(?t/?n) + (?N x e)"
  4472       and b="0", simplified div_0]) (simp only: algebra_simps)
  4473   also have "\<dots> = (real_of_int c *?t + ?n* (?N x e) > 0)"
  4474     using np by simp
  4475   finally show ?case using nbt nb by (simp add: algebra_simps)
  4476 next
  4477   case (8 c e)
  4478   from 8 have cp: "c >0" and nb: "numbound0 e" by simp_all
  4479   have "?I ?u (Ge (CN 0 c e)) = (real_of_int c *(?t/?n) + (?N x e) \<ge> 0)"
  4480     using numbound0_I[OF nb, where bs="bs" and b="?u" and b'="x"] by simp
  4481   also have "\<dots> = (?n*(real_of_int c *(?t/?n)) + ?n*(?N x e) \<ge> 0)"
  4482     by (simp only: pos_divide_le_eq[OF np, where a="real_of_int c *(?t/?n) + (?N x e)"
  4483       and b="0", simplified div_0]) (simp only: algebra_simps)
  4484   also have "\<dots> = (real_of_int c *?t + ?n* (?N x e) \<ge> 0)"
  4485     using np by simp
  4486   finally show ?case using nbt nb by (simp add: algebra_simps)
  4487 next
  4488   case (3 c e)
  4489   from 3 have cp: "c >0" and nb: "numbound0 e" by simp_all
  4490   from np have np: "real_of_int n \<noteq> 0" by simp
  4491   have "?I ?u (Eq (CN 0 c e)) = (real_of_int c *(?t/?n) + (?N x e) = 0)"
  4492     using numbound0_I[OF nb, where bs="bs" and b="?u" and b'="x"] by simp
  4493   also have "\<dots> = (?n*(real_of_int c *(?t/?n)) + ?n*(?N x e) = 0)"
  4494     by (simp only: nonzero_eq_divide_eq[OF np, where a="real_of_int c *(?t/?n) + (?N x e)"
  4495       and b="0", simplified div_0]) (simp only: algebra_simps)
  4496   also have "\<dots> = (real_of_int c *?t + ?n* (?N x e) = 0)"
  4497     using np by simp
  4498   finally show ?case using nbt nb by (simp add: algebra_simps)
  4499 next
  4500   case (4 c e)
  4501   from 4 have cp: "c >0" and nb: "numbound0 e" by simp_all
  4502   from np have np: "real_of_int n \<noteq> 0" by simp
  4503   have "?I ?u (NEq (CN 0 c e)) = (real_of_int c *(?t/?n) + (?N x e) \<noteq> 0)"
  4504     using numbound0_I[OF nb, where bs="bs" and b="?u" and b'="x"] by simp
  4505   also have "\<dots> = (?n*(real_of_int c *(?t/?n)) + ?n*(?N x e) \<noteq> 0)"
  4506     by (simp only: nonzero_eq_divide_eq[OF np, where a="real_of_int c *(?t/?n) + (?N x e)"
  4507       and b="0", simplified div_0]) (simp only: algebra_simps)
  4508   also have "\<dots> = (real_of_int c *?t + ?n* (?N x e) \<noteq> 0)"
  4509     using np by simp
  4510   finally show ?case using nbt nb by (simp add: algebra_simps)
  4511 qed(simp_all add: nbt numbound0_I[where bs ="bs" and b="(Inum (x#bs) t)/ real_of_int n" and b'="x"])
  4512 
  4513 lemma \<Upsilon>_l:
  4514   assumes lp: "isrlfm p"
  4515   shows "\<forall> (t,k) \<in> set (\<Upsilon> p). numbound0 t \<and> k >0"
  4516 using lp
  4517 by(induct p rule: \<Upsilon>.induct)  auto
  4518 
  4519 lemma rminusinf_\<Upsilon>:
  4520   assumes lp: "isrlfm p"
  4521   and nmi: "\<not> (Ifm (a#bs) (minusinf p))" (is "\<not> (Ifm (a#bs) (?M p))")
  4522   and ex: "Ifm (x#bs) p" (is "?I x p")
  4523   shows "\<exists> (s,m) \<in> set (\<Upsilon> p). x \<ge> Inum (a#bs) s / real_of_int m" (is "\<exists> (s,m) \<in> ?U p. x \<ge> ?N a s / real_of_int m")
  4524 proof-
  4525   have "\<exists> (s,m) \<in> set (\<Upsilon> p). real_of_int m * x \<ge> Inum (a#bs) s " (is "\<exists> (s,m) \<in> ?U p. real_of_int m *x \<ge> ?N a s")
  4526     using lp nmi ex
  4527     by (induct p rule: minusinf.induct, auto simp add:numbound0_I[where bs="bs" and b="a" and b'="x"])
  4528   then obtain s m where smU: "(s,m) \<in> set (\<Upsilon> p)" and mx: "real_of_int m * x \<ge> ?N a s" by blast
  4529   from \<Upsilon>_l[OF lp] smU have mp: "real_of_int m > 0" by auto
  4530   from pos_divide_le_eq[OF mp, where a="x" and b="?N a s", symmetric] mx have "x \<ge> ?N a s / real_of_int m"
  4531     by (auto simp add: mult.commute)
  4532   thus ?thesis using smU by auto
  4533 qed
  4534 
  4535 lemma rplusinf_\<Upsilon>:
  4536   assumes lp: "isrlfm p"
  4537   and nmi: "\<not> (Ifm (a#bs) (plusinf p))" (is "\<not> (Ifm (a#bs) (?M p))")
  4538   and ex: "Ifm (x#bs) p" (is "?I x p")
  4539   shows "\<exists> (s,m) \<in> set (\<Upsilon> p). x \<le> Inum (a#bs) s / real_of_int m" (is "\<exists> (s,m) \<in> ?U p. x \<le> ?N a s / real_of_int m")
  4540 proof-
  4541   have "\<exists> (s,m) \<in> set (\<Upsilon> p). real_of_int m * x \<le> Inum (a#bs) s " (is "\<exists> (s,m) \<in> ?U p. real_of_int m *x \<le> ?N a s")
  4542     using lp nmi ex
  4543     by (induct p rule: minusinf.induct, auto simp add:numbound0_I[where bs="bs" and b="a" and b'="x"])
  4544   then obtain s m where smU: "(s,m) \<in> set (\<Upsilon> p)" and mx: "real_of_int m * x \<le> ?N a s" by blast
  4545   from \<Upsilon>_l[OF lp] smU have mp: "real_of_int m > 0" by auto
  4546   from pos_le_divide_eq[OF mp, where a="x" and b="?N a s", symmetric] mx have "x \<le> ?N a s / real_of_int m"
  4547     by (auto simp add: mult.commute)
  4548   thus ?thesis using smU by auto
  4549 qed
  4550 
  4551 lemma lin_dense:
  4552   assumes lp: "isrlfm p"
  4553   and noS: "\<forall> t. l < t \<and> t< u \<longrightarrow> t \<notin> (\<lambda> (t,n). Inum (x#bs) t / real_of_int n) ` set (\<Upsilon> p)"
  4554   (is "\<forall> t. _ \<and> _ \<longrightarrow> t \<notin> (\<lambda> (t,n). ?N x t / real_of_int n ) ` (?U p)")
  4555   and lx: "l < x" and xu:"x < u" and px:" Ifm (x#bs) p"
  4556   and ly: "l < y" and yu: "y < u"
  4557   shows "Ifm (y#bs) p"
  4558 using lp px noS
  4559 proof (induct p rule: isrlfm.induct)
  4560   case (5 c e) hence cp: "real_of_int c > 0" and nb: "numbound0 e" by simp_all
  4561   from 5 have "x * real_of_int c + ?N x e < 0" by (simp add: algebra_simps)
  4562   hence pxc: "x < (- ?N x e) / real_of_int c"
  4563     by (simp only: pos_less_divide_eq[OF cp, where a="x" and b="-?N x e"])
  4564   from 5 have noSc:"\<forall> t. l < t \<and> t < u \<longrightarrow> t \<noteq> (- ?N x e) / real_of_int c" by auto
  4565   with ly yu have yne: "y \<noteq> - ?N x e / real_of_int c" by auto
  4566   hence "y < (- ?N x e) / real_of_int c \<or> y > (-?N x e) / real_of_int c" by auto
  4567   moreover {assume y: "y < (-?N x e)/ real_of_int c"
  4568     hence "y * real_of_int c < - ?N x e"
  4569       by (simp add: pos_less_divide_eq[OF cp, where a="y" and b="-?N x e", symmetric])
  4570     hence "real_of_int c * y + ?N x e < 0" by (simp add: algebra_simps)
  4571     hence ?case using numbound0_I[OF nb, where bs="bs" and b="x" and b'="y"] by simp}
  4572   moreover {assume y: "y > (- ?N x e) / real_of_int c"
  4573     with yu have eu: "u > (- ?N x e) / real_of_int c" by auto
  4574     with noSc ly yu have "(- ?N x e) / real_of_int c \<le> l" by (cases "(- ?N x e) / real_of_int c > l", auto)
  4575     with lx pxc have "False" by auto
  4576     hence ?case by simp }
  4577   ultimately show ?case by blast
  4578 next
  4579   case (6 c e) hence cp: "real_of_int c > 0" and nb: "numbound0 e" by simp_all
  4580   from 6 have "x * real_of_int c + ?N x e \<le> 0" by (simp add: algebra_simps)
  4581   hence pxc: "x \<le> (- ?N x e) / real_of_int c"
  4582     by (simp only: pos_le_divide_eq[OF cp, where a="x" and b="-?N x e"])
  4583   from 6 have noSc:"\<forall> t. l < t \<and> t < u \<longrightarrow> t \<noteq> (- ?N x e) / real_of_int c" by auto
  4584   with ly yu have yne: "y \<noteq> - ?N x e / real_of_int c" by auto
  4585   hence "y < (- ?N x e) / real_of_int c \<or> y > (-?N x e) / real_of_int c" by auto
  4586   moreover {assume y: "y < (-?N x e)/ real_of_int c"
  4587     hence "y * real_of_int c < - ?N x e"
  4588       by (simp add: pos_less_divide_eq[OF cp, where a="y" and b="-?N x e", symmetric])
  4589     hence "real_of_int c * y + ?N x e < 0" by (simp add: algebra_simps)
  4590     hence ?case using numbound0_I[OF nb, where bs="bs" and b="x" and b'="y"] by simp}
  4591   moreover {assume y: "y > (- ?N x e) / real_of_int c"
  4592     with yu have eu: "u > (- ?N x e) / real_of_int c" by auto
  4593     with noSc ly yu have "(- ?N x e) / real_of_int c \<le> l" by (cases "(- ?N x e) / real_of_int c > l", auto)
  4594     with lx pxc have "False" by auto
  4595     hence ?case by simp }
  4596   ultimately show ?case by blast
  4597 next
  4598   case (7 c e) hence cp: "real_of_int c > 0" and nb: "numbound0 e" by simp_all
  4599   from 7 have "x * real_of_int c + ?N x e > 0" by (simp add: algebra_simps)
  4600   hence pxc: "x > (- ?N x e) / real_of_int c"
  4601     by (simp only: pos_divide_less_eq[OF cp, where a="x" and b="-?N x e"])
  4602   from 7 have noSc:"\<forall> t. l < t \<and> t < u \<longrightarrow> t \<noteq> (- ?N x e) / real_of_int c" by auto
  4603   with ly yu have yne: "y \<noteq> - ?N x e / real_of_int c" by auto
  4604   hence "y < (- ?N x e) / real_of_int c \<or> y > (-?N x e) / real_of_int c" by auto
  4605   moreover {assume y: "y > (-?N x e)/ real_of_int c"
  4606     hence "y * real_of_int c > - ?N x e"
  4607       by (simp add: pos_divide_less_eq[OF cp, where a="y" and b="-?N x e", symmetric])
  4608     hence "real_of_int c * y + ?N x e > 0" by (simp add: algebra_simps)
  4609     hence ?case using numbound0_I[OF nb, where bs="bs" and b="x" and b'="y"] by simp}
  4610   moreover {assume y: "y < (- ?N x e) / real_of_int c"
  4611     with ly have eu: "l < (- ?N x e) / real_of_int c" by auto
  4612     with noSc ly yu have "(- ?N x e) / real_of_int c \<ge> u" by (cases "(- ?N x e) / real_of_int c > l", auto)
  4613     with xu pxc have "False" by auto
  4614     hence ?case by simp }
  4615   ultimately show ?case by blast
  4616 next
  4617   case (8 c e) hence cp: "real_of_int c > 0" and nb: "numbound0 e" by simp_all
  4618   from 8 have "x * real_of_int c + ?N x e \<ge> 0" by (simp add: algebra_simps)
  4619   hence pxc: "x \<ge> (- ?N x e) / real_of_int c"
  4620     by (simp only: pos_divide_le_eq[OF cp, where a="x" and b="-?N x e"])
  4621   from 8 have noSc:"\<forall> t. l < t \<and> t < u \<longrightarrow> t \<noteq> (- ?N x e) / real_of_int c" by auto
  4622   with ly yu have yne: "y \<noteq> - ?N x e / real_of_int c" by auto
  4623   hence "y < (- ?N x e) / real_of_int c \<or> y > (-?N x e) / real_of_int c" by auto
  4624   moreover {assume y: "y > (-?N x e)/ real_of_int c"
  4625     hence "y * real_of_int c > - ?N x e"
  4626       by (simp add: pos_divide_less_eq[OF cp, where a="y" and b="-?N x e", symmetric])
  4627     hence "real_of_int c * y + ?N x e > 0" by (simp add: algebra_simps)
  4628     hence ?case using numbound0_I[OF nb, where bs="bs" and b="x" and b'="y"] by simp}
  4629   moreover {assume y: "y < (- ?N x e) / real_of_int c"
  4630     with ly have eu: "l < (- ?N x e) / real_of_int c" by auto
  4631     with noSc ly yu have "(- ?N x e) / real_of_int c \<ge> u" by (cases "(- ?N x e) / real_of_int c > l", auto)
  4632     with xu pxc have "False" by auto
  4633     hence ?case by simp }
  4634   ultimately show ?case by blast
  4635 next
  4636   case (3 c e) hence cp: "real_of_int c > 0" and nb: "numbound0 e" by simp_all
  4637   from cp have cnz: "real_of_int c \<noteq> 0" by simp
  4638   from 3 have "x * real_of_int c + ?N x e = 0" by (simp add: algebra_simps)
  4639   hence pxc: "x = (- ?N x e) / real_of_int c"
  4640     by (simp only: nonzero_eq_divide_eq[OF cnz, where a="x" and b="-?N x e"])
  4641   from 3 have noSc:"\<forall> t. l < t \<and> t < u \<longrightarrow> t \<noteq> (- ?N x e) / real_of_int c" by auto
  4642   with lx xu have yne: "x \<noteq> - ?N x e / real_of_int c" by auto
  4643   with pxc show ?case by simp
  4644 next
  4645   case (4 c e) hence cp: "real_of_int c > 0" and nb: "numbound0 e" by simp_all
  4646   from cp have cnz: "real_of_int c \<noteq> 0" by simp
  4647   from 4 have noSc:"\<forall> t. l < t \<and> t < u \<longrightarrow> t \<noteq> (- ?N x e) / real_of_int c" by auto
  4648   with ly yu have yne: "y \<noteq> - ?N x e / real_of_int c" by auto
  4649   hence "y* real_of_int c \<noteq> -?N x e"
  4650     by (simp only: nonzero_eq_divide_eq[OF cnz, where a="y" and b="-?N x e"]) simp
  4651   hence "y* real_of_int c + ?N x e \<noteq> 0" by (simp add: algebra_simps)
  4652   thus ?case using numbound0_I[OF nb, where bs="bs" and b="x" and b'="y"]
  4653     by (simp add: algebra_simps)
  4654 qed (auto simp add: numbound0_I[where bs="bs" and b="y" and b'="x"])
  4655 
  4656 lemma rinf_\<Upsilon>:
  4657   assumes lp: "isrlfm p"
  4658   and nmi: "\<not> (Ifm (x#bs) (minusinf p))" (is "\<not> (Ifm (x#bs) (?M p))")
  4659   and npi: "\<not> (Ifm (x#bs) (plusinf p))" (is "\<not> (Ifm (x#bs) (?P p))")
  4660   and ex: "\<exists> x.  Ifm (x#bs) p" (is "\<exists> x. ?I x p")
  4661   shows "\<exists> (l,n) \<in> set (\<Upsilon> p). \<exists> (s,m) \<in> set (\<Upsilon> p).
  4662     ?I ((Inum (x#bs) l / real_of_int n + Inum (x#bs) s / real_of_int m) / 2) p"
  4663 proof-
  4664   let ?N = "\<lambda> x t. Inum (x#bs) t"
  4665   let ?U = "set (\<Upsilon> p)"
  4666   from ex obtain a where pa: "?I a p" by blast
  4667   from bound0_I[OF rminusinf_bound0[OF lp], where bs="bs" and b="x" and b'="a"] nmi
  4668   have nmi': "\<not> (?I a (?M p))" by simp
  4669   from bound0_I[OF rplusinf_bound0[OF lp], where bs="bs" and b="x" and b'="a"] npi
  4670   have npi': "\<not> (?I a (?P p))" by simp
  4671   have "\<exists> (l,n) \<in> set (\<Upsilon> p). \<exists> (s,m) \<in> set (\<Upsilon> p). ?I ((?N a l/real_of_int n + ?N a s /real_of_int m) / 2) p"
  4672   proof-
  4673     let ?M = "(\<lambda> (t,c). ?N a t / real_of_int c) ` ?U"
  4674     have fM: "finite ?M" by auto
  4675     from rminusinf_\<Upsilon>[OF lp nmi pa] rplusinf_\<Upsilon>[OF lp npi pa]
  4676     have "\<exists> (l,n) \<in> set (\<Upsilon> p). \<exists> (s,m) \<in> set (\<Upsilon> p). a \<le> ?N x l / real_of_int n \<and> a \<ge> ?N x s / real_of_int m" by blast
  4677     then obtain "t" "n" "s" "m" where
  4678       tnU: "(t,n) \<in> ?U" and smU: "(s,m) \<in> ?U"
  4679       and xs1: "a \<le> ?N x s / real_of_int m" and tx1: "a \<ge> ?N x t / real_of_int n" by blast
  4680     from \<Upsilon>_l[OF lp] tnU smU numbound0_I[where bs="bs" and b="x" and b'="a"] xs1 tx1 have xs: "a \<le> ?N a s / real_of_int m" and tx: "a \<ge> ?N a t / real_of_int n" by auto
  4681     from tnU have Mne: "?M \<noteq> {}" by auto
  4682     hence Une: "?U \<noteq> {}" by simp
  4683     let ?l = "Min ?M"
  4684     let ?u = "Max ?M"
  4685     have linM: "?l \<in> ?M" using fM Mne by simp
  4686     have uinM: "?u \<in> ?M" using fM Mne by simp
  4687     have tnM: "?N a t / real_of_int n \<in> ?M" using tnU by auto
  4688     have smM: "?N a s / real_of_int m \<in> ?M" using smU by auto
  4689     have lM: "\<forall> t\<in> ?M. ?l \<le> t" using Mne fM by auto
  4690     have Mu: "\<forall> t\<in> ?M. t \<le> ?u" using Mne fM by auto
  4691     have "?l \<le> ?N a t / real_of_int n" using tnM Mne by simp hence lx: "?l \<le> a" using tx by simp
  4692     have "?N a s / real_of_int m \<le> ?u" using smM Mne by simp hence xu: "a \<le> ?u" using xs by simp
  4693     from finite_set_intervals2[where P="\<lambda> x. ?I x p",OF pa lx xu linM uinM fM lM Mu]
  4694     have "(\<exists> s\<in> ?M. ?I s p) \<or>
  4695       (\<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)" .
  4696     moreover { fix u assume um: "u\<in> ?M" and pu: "?I u p"
  4697       hence "\<exists> (tu,nu) \<in> ?U. u = ?N a tu / real_of_int nu" by auto
  4698       then obtain "tu" "nu" where tuU: "(tu,nu) \<in> ?U" and tuu:"u= ?N a tu / real_of_int nu" by blast
  4699       have "(u + u) / 2 = u" by auto with pu tuu
  4700       have "?I (((?N a tu / real_of_int nu) + (?N a tu / real_of_int nu)) / 2) p" by simp
  4701       with tuU have ?thesis by blast}
  4702     moreover{
  4703       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"
  4704       then obtain t1 and t2 where t1M: "t1 \<in> ?M" and t2M: "t2\<in> ?M"
  4705         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"
  4706         by blast
  4707       from t1M have "\<exists> (t1u,t1n) \<in> ?U. t1 = ?N a t1u / real_of_int t1n" by auto
  4708       then obtain "t1u" "t1n" where t1uU: "(t1u,t1n) \<in> ?U" and t1u: "t1 = ?N a t1u / real_of_int t1n" by blast
  4709       from t2M have "\<exists> (t2u,t2n) \<in> ?U. t2 = ?N a t2u / real_of_int t2n" by auto
  4710       then obtain "t2u" "t2n" where t2uU: "(t2u,t2n) \<in> ?U" and t2u: "t2 = ?N a t2u / real_of_int t2n" by blast
  4711       from t1x xt2 have t1t2: "t1 < t2" by simp
  4712       let ?u = "(t1 + t2) / 2"
  4713       from less_half_sum[OF t1t2] gt_half_sum[OF t1t2] have t1lu: "t1 < ?u" and ut2: "?u < t2" by auto
  4714       from lin_dense[OF lp noM t1x xt2 px t1lu ut2] have "?I ?u p" .
  4715       with t1uU t2uU t1u t2u have ?thesis by blast}
  4716     ultimately show ?thesis by blast
  4717   qed
  4718   then obtain "l" "n" "s"  "m" where lnU: "(l,n) \<in> ?U" and smU:"(s,m) \<in> ?U"
  4719     and pu: "?I ((?N a l / real_of_int n + ?N a s / real_of_int m) / 2) p" by blast
  4720   from lnU smU \<Upsilon>_l[OF lp] have nbl: "numbound0 l" and nbs: "numbound0 s" by auto
  4721   from numbound0_I[OF nbl, where bs="bs" and b="a" and b'="x"]
  4722     numbound0_I[OF nbs, where bs="bs" and b="a" and b'="x"] pu
  4723   have "?I ((?N x l / real_of_int n + ?N x s / real_of_int m) / 2) p" by simp
  4724   with lnU smU
  4725   show ?thesis by auto
  4726 qed
  4727     (* The Ferrante - Rackoff Theorem *)
  4728 
  4729 theorem fr_eq:
  4730   assumes lp: "isrlfm p"
  4731   shows "(\<exists> x. Ifm (x#bs) p) = ((Ifm (x#bs) (minusinf p)) \<or> (Ifm (x#bs) (plusinf p)) \<or> (\<exists> (t,n) \<in> set (\<Upsilon> p). \<exists> (s,m) \<in> set (\<Upsilon> p). Ifm ((((Inum (x#bs) t)/  real_of_int n + (Inum (x#bs) s) / real_of_int m) /2)#bs) p))"
  4732   (is "(\<exists> x. ?I x p) = (?M \<or> ?P \<or> ?F)" is "?E = ?D")
  4733 proof
  4734   assume px: "\<exists> x. ?I x p"
  4735   have "?M \<or> ?P \<or> (\<not> ?M \<and> \<not> ?P)" by blast
  4736   moreover {assume "?M \<or> ?P" hence "?D" by blast}
  4737   moreover {assume nmi: "\<not> ?M" and npi: "\<not> ?P"
  4738     from rinf_\<Upsilon>[OF lp nmi npi] have "?F" using px by blast hence "?D" by blast}
  4739   ultimately show "?D" by blast
  4740 next
  4741   assume "?D"
  4742   moreover {assume m:"?M" from rminusinf_ex[OF lp m] have "?E" .}
  4743   moreover {assume p: "?P" from rplusinf_ex[OF lp p] have "?E" . }
  4744   moreover {assume f:"?F" hence "?E" by blast}
  4745   ultimately show "?E" by blast
  4746 qed
  4747 
  4748 
  4749 lemma fr_eq_\<upsilon>:
  4750   assumes lp: "isrlfm p"
  4751   shows "(\<exists> x. Ifm (x#bs) p) = ((Ifm (x#bs) (minusinf p)) \<or> (Ifm (x#bs) (plusinf p)) \<or> (\<exists> (t,k) \<in> set (\<Upsilon> p). \<exists> (s,l) \<in> set (\<Upsilon> p). Ifm (x#bs) (\<upsilon> p (Add(Mul l t) (Mul k s) , 2*k*l))))"
  4752   (is "(\<exists> x. ?I x p) = (?M \<or> ?P \<or> ?F)" is "?E = ?D")
  4753 proof
  4754   assume px: "\<exists> x. ?I x p"
  4755   have "?M \<or> ?P \<or> (\<not> ?M \<and> \<not> ?P)" by blast
  4756   moreover {assume "?M \<or> ?P" hence "?D" by blast}
  4757   moreover {assume nmi: "\<not> ?M" and npi: "\<not> ?P"
  4758     let ?f ="\<lambda> (t,n). Inum (x#bs) t / real_of_int n"
  4759     let ?N = "\<lambda> t. Inum (x#bs) t"
  4760     {fix t n s m assume "(t,n)\<in> set (\<Upsilon> p)" and "(s,m) \<in> set (\<Upsilon> p)"
  4761       with \<Upsilon>_l[OF lp] have tnb: "numbound0 t" and np:"real_of_int n > 0" and snb: "numbound0 s" and mp:"real_of_int m > 0"
  4762         by auto
  4763       let ?st = "Add (Mul m t) (Mul n s)"
  4764       from np mp have mnp: "real_of_int (2*n*m) > 0" by (simp add: mult.commute)
  4765       from tnb snb have st_nb: "numbound0 ?st" by simp
  4766       have st: "(?N t / real_of_int n + ?N s / real_of_int m)/2 = ?N ?st / real_of_int (2*n*m)"
  4767         using mnp mp np by (simp add: algebra_simps add_divide_distrib)
  4768       from \<upsilon>_I[OF lp mnp st_nb, where x="x" and bs="bs"]
  4769       have "?I x (\<upsilon> p (?st,2*n*m)) = ?I ((?N t / real_of_int n + ?N s / real_of_int m) /2) p" by (simp only: st[symmetric])}
  4770     with rinf_\<Upsilon>[OF lp nmi npi px] have "?F" by blast hence "?D" by blast}
  4771   ultimately show "?D" by blast
  4772 next
  4773   assume "?D"
  4774   moreover {assume m:"?M" from rminusinf_ex[OF lp m] have "?E" .}
  4775   moreover {assume p: "?P" from rplusinf_ex[OF lp p] have "?E" . }
  4776   moreover {fix t k s l assume "(t,k) \<in> set (\<Upsilon> p)" and "(s,l) \<in> set (\<Upsilon> p)"
  4777     and px:"?I x (\<upsilon> p (Add (Mul l t) (Mul k s), 2*k*l))"
  4778     with \<Upsilon>_l[OF lp] have tnb: "numbound0 t" and np:"real_of_int k > 0" and snb: "numbound0 s" and mp:"real_of_int l > 0" by auto
  4779     let ?st = "Add (Mul l t) (Mul k s)"
  4780     from np mp have mnp: "real_of_int (2*k*l) > 0" by (simp add: mult.commute)
  4781     from tnb snb have st_nb: "numbound0 ?st" by simp
  4782     from \<upsilon>_I[OF lp mnp st_nb, where bs="bs"] px have "?E" by auto}
  4783   ultimately show "?E" by blast
  4784 qed
  4785 
  4786 text\<open>The overall Part\<close>
  4787 
  4788 lemma real_ex_int_real01:
  4789   shows "(\<exists> (x::real). P x) = (\<exists> (i::int) (u::real). 0\<le> u \<and> u< 1 \<and> P (real_of_int i + u))"
  4790 proof(auto)
  4791   fix x
  4792   assume Px: "P x"
  4793   let ?i = "\<lfloor>x\<rfloor>"
  4794   let ?u = "x - real_of_int ?i"
  4795   have "x = real_of_int ?i + ?u" by simp
  4796   hence "P (real_of_int ?i + ?u)" using Px by simp
  4797   moreover have "real_of_int ?i \<le> x" using of_int_floor_le by simp hence "0 \<le> ?u" by arith
  4798   moreover have "?u < 1" using real_of_int_floor_add_one_gt[where r="x"] by arith
  4799   ultimately show "(\<exists> (i::int) (u::real). 0\<le> u \<and> u< 1 \<and> P (real_of_int i + u))" by blast
  4800 qed
  4801 
  4802 fun exsplitnum :: "num \<Rightarrow> num"
  4803 where
  4804   "exsplitnum (C c) = (C c)"
  4805 | "exsplitnum (Bound 0) = Add (Bound 0) (Bound 1)"
  4806 | "exsplitnum (Bound n) = Bound (n+1)"
  4807 | "exsplitnum (Neg a) = Neg (exsplitnum a)"
  4808 | "exsplitnum (Add a b) = Add (exsplitnum a) (exsplitnum b) "
  4809 | "exsplitnum (Sub a b) = Sub (exsplitnum a) (exsplitnum b) "
  4810 | "exsplitnum (Mul c a) = Mul c (exsplitnum a)"
  4811 | "exsplitnum (Floor a) = Floor (exsplitnum a)"
  4812 | "exsplitnum (CN 0 c a) = CN 0 c (Add (Mul c (Bound 1)) (exsplitnum a))"
  4813 | "exsplitnum (CN n c a) = CN (n+1) c (exsplitnum a)"
  4814 | "exsplitnum (CF c s t) = CF c (exsplitnum s) (exsplitnum t)"
  4815 
  4816 fun exsplit :: "fm \<Rightarrow> fm"
  4817 where
  4818   "exsplit (Lt a) = Lt (exsplitnum a)"
  4819 | "exsplit (Le a) = Le (exsplitnum a)"
  4820 | "exsplit (Gt a) = Gt (exsplitnum a)"
  4821 | "exsplit (Ge a) = Ge (exsplitnum a)"
  4822 | "exsplit (Eq a) = Eq (exsplitnum a)"
  4823 | "exsplit (NEq a) = NEq (exsplitnum a)"
  4824 | "exsplit (Dvd i a) = Dvd i (exsplitnum a)"
  4825 | "exsplit (NDvd i a) = NDvd i (exsplitnum a)"
  4826 | "exsplit (And p q) = And (exsplit p) (exsplit q)"
  4827 | "exsplit (Or p q) = Or (exsplit p) (exsplit q)"
  4828 | "exsplit (Imp p q) = Imp (exsplit p) (exsplit q)"
  4829 | "exsplit (Iff p q) = Iff (exsplit p) (exsplit q)"
  4830 | "exsplit (NOT p) = NOT (exsplit p)"
  4831 | "exsplit p = p"
  4832 
  4833 lemma exsplitnum:
  4834   "Inum (x#y#bs) (exsplitnum t) = Inum ((x+y) #bs) t"
  4835   by(induct t rule: exsplitnum.induct) (simp_all add: algebra_simps)
  4836 
  4837 lemma exsplit:
  4838   assumes qfp: "qfree p"
  4839   shows "Ifm (x#y#bs) (exsplit p) = Ifm ((x+y)#bs) p"
  4840 using qfp exsplitnum[where x="x" and y="y" and bs="bs"]
  4841 by(induct p rule: exsplit.induct) simp_all
  4842 
  4843 lemma splitex:
  4844   assumes qf: "qfree p"
  4845   shows "(Ifm bs (E p)) = (\<exists> (i::int). Ifm (real_of_int i#bs) (E (And (And (Ge(CN 0 1 (C 0))) (Lt (CN 0 1 (C (- 1))))) (exsplit p))))" (is "?lhs = ?rhs")
  4846 proof-
  4847   have "?rhs = (\<exists> (i::int). \<exists> x. 0\<le> x \<and> x < 1 \<and> Ifm (x#(real_of_int i)#bs) (exsplit p))"
  4848     by auto
  4849   also have "\<dots> = (\<exists> (i::int). \<exists> x. 0\<le> x \<and> x < 1 \<and> Ifm ((real_of_int i + x) #bs) p)"
  4850     by (simp only: exsplit[OF qf] ac_simps)
  4851   also have "\<dots> = (\<exists> x. Ifm (x#bs) p)"
  4852     by (simp only: real_ex_int_real01[where P="\<lambda> x. Ifm (x#bs) p"])
  4853   finally show ?thesis by simp
  4854 qed
  4855 
  4856     (* Implement the right hand sides of Cooper's theorem and Ferrante and Rackoff. *)
  4857 
  4858 definition ferrack01 :: "fm \<Rightarrow> fm" where
  4859   "ferrack01 p \<equiv> (let p' = rlfm(And (And (Ge(CN 0 1 (C 0))) (Lt (CN 0 1 (C (- 1))))) p);
  4860                     U = remdups(map simp_num_pair
  4861                      (map (\<lambda> ((t,n),(s,m)). (Add (Mul m t) (Mul n s) , 2*n*m))
  4862                            (alluopairs (\<Upsilon> p'))))
  4863   in decr (evaldjf (\<upsilon> p') U ))"
  4864 
  4865 lemma fr_eq_01:
  4866   assumes qf: "qfree p"
  4867   shows "(\<exists> x. Ifm (x#bs) (And (And (Ge(CN 0 1 (C 0))) (Lt (CN 0 1 (C (- 1))))) p)) = (\<exists> (t,n) \<in> set (\<Upsilon> (rlfm (And (And (Ge(CN 0 1 (C 0))) (Lt (CN 0 1 (C (- 1))))) p))). \<exists> (s,m) \<in> set (\<Upsilon> (rlfm (And (And (Ge(CN 0 1 (C 0))) (Lt (CN 0 1 (C (- 1))))) p))). Ifm (x#bs) (\<upsilon> (rlfm (And (And (Ge(CN 0 1 (C 0))) (Lt (CN 0 1 (C (- 1))))) p)) (Add (Mul m t) (Mul n s), 2*n*m)))"
  4868   (is "(\<exists> x. ?I x ?q) = ?F")
  4869 proof-
  4870   let ?rq = "rlfm ?q"
  4871   let ?M = "?I x (minusinf ?rq)"
  4872   let ?P = "?I x (plusinf ?rq)"
  4873   have MF: "?M = False"
  4874     apply (simp add: Let_def reducecoeff_def numgcd_def rsplit_def ge_def lt_def conj_def disj_def)
  4875     by (cases "rlfm p = And (Ge (CN 0 1 (C 0))) (Lt (CN 0 1 (C (- 1))))", simp_all)
  4876   have PF: "?P = False" apply (simp add: Let_def reducecoeff_def numgcd_def rsplit_def ge_def lt_def conj_def disj_def)
  4877     by (cases "rlfm p = And (Ge (CN 0 1 (C 0))) (Lt (CN 0 1 (C (- 1))))", simp_all)
  4878   have "(\<exists> x. ?I x ?q ) =
  4879     ((?I x (minusinf ?rq)) \<or> (?I x (plusinf ?rq )) \<or> (\<exists> (t,n) \<in> set (\<Upsilon> ?rq). \<exists> (s,m) \<in> set (\<Upsilon> ?rq ). ?I x (\<upsilon> ?rq (Add (Mul m t) (Mul n s), 2*n*m))))"
  4880     (is "(\<exists> x. ?I x ?q) = (?M \<or> ?P \<or> ?F)" is "?E = ?D")
  4881   proof
  4882     assume "\<exists> x. ?I x ?q"
  4883     then obtain x where qx: "?I x ?q" by blast
  4884     hence xp: "0\<le> x" and x1: "x< 1" and px: "?I x p"
  4885       by (auto simp add: rsplit_def lt_def ge_def rlfm_I[OF qf])
  4886     from qx have "?I x ?rq "
  4887       by (simp add: rsplit_def lt_def ge_def rlfm_I[OF qf xp x1])
  4888     hence lqx: "?I x ?rq " using simpfm[where p="?rq" and bs="x#bs"] by auto
  4889     from qf have qfq:"isrlfm ?rq"
  4890       by (auto simp add: rsplit_def lt_def ge_def rlfm_I[OF qf xp x1])
  4891     with lqx fr_eq_\<upsilon>[OF qfq] show "?M \<or> ?P \<or> ?F" by blast
  4892   next
  4893     assume D: "?D"
  4894     let ?U = "set (\<Upsilon> ?rq )"
  4895     from MF PF D have "?F" by auto
  4896     then obtain t n s m where aU:"(t,n) \<in> ?U" and bU:"(s,m)\<in> ?U" and rqx: "?I x (\<upsilon> ?rq (Add (Mul m t) (Mul n s), 2*n*m))" by blast
  4897     from qf have lrq:"isrlfm ?rq"using rlfm_l[OF qf]
  4898       by (auto simp add: rsplit_def lt_def ge_def)
  4899     from aU bU \<Upsilon>_l[OF lrq] have tnb: "numbound0 t" and np:"real_of_int n > 0" and snb: "numbound0 s" and mp:"real_of_int m > 0" by (auto simp add: split_def)
  4900     let ?st = "Add (Mul m t) (Mul n s)"
  4901     from tnb snb have stnb: "numbound0 ?st" by simp
  4902     from np mp have mnp: "real_of_int (2*n*m) > 0" by (simp add: mult.commute)
  4903     from conjunct1[OF \<upsilon>_I[OF lrq mnp stnb, where bs="bs" and x="x"], symmetric] rqx
  4904     have "\<exists> x. ?I x ?rq" by auto
  4905     thus "?E"
  4906       using rlfm_I[OF qf] by (auto simp add: rsplit_def lt_def ge_def)
  4907   qed
  4908   with MF PF show ?thesis by blast
  4909 qed
  4910 
  4911 lemma \<Upsilon>_cong_aux:
  4912   assumes Ul: "\<forall> (t,n) \<in> set U. numbound0 t \<and> n >0"
  4913   shows "((\<lambda> (t,n). Inum (x#bs) t /real_of_int n) ` (set (map (\<lambda> ((t,n),(s,m)). (Add (Mul m t) (Mul n s) , 2*n*m)) (alluopairs U)))) = ((\<lambda> ((t,n),(s,m)). (Inum (x#bs) t /real_of_int n + Inum (x#bs) s /real_of_int m)/2) ` (set U \<times> set U))"
  4914   (is "?lhs = ?rhs")
  4915 proof(auto)
  4916   fix t n s m
  4917   assume "((t,n),(s,m)) \<in> set (alluopairs U)"
  4918   hence th: "((t,n),(s,m)) \<in> (set U \<times> set U)"
  4919     using alluopairs_set1[where xs="U"] by blast
  4920   let ?N = "\<lambda> t. Inum (x#bs) t"
  4921   let ?st= "Add (Mul m t) (Mul n s)"
  4922   from Ul th have mnz: "m \<noteq> 0" by auto
  4923   from Ul th have  nnz: "n \<noteq> 0" by auto
  4924   have st: "(?N t / real_of_int n + ?N s / real_of_int m)/2 = ?N ?st / real_of_int (2*n*m)"
  4925    using mnz nnz by (simp add: algebra_simps add_divide_distrib)
  4926 
  4927   thus "(real_of_int m *  Inum (x # bs) t + real_of_int n * Inum (x # bs) s) /
  4928        (2 * real_of_int n * real_of_int m)
  4929        \<in> (\<lambda>((t, n), s, m).
  4930              (Inum (x # bs) t / real_of_int n + Inum (x # bs) s / real_of_int m) / 2) `
  4931          (set U \<times> set U)"using mnz nnz th
  4932     apply (auto simp add: th add_divide_distrib algebra_simps split_def image_def)
  4933     by (rule_tac x="(s,m)" in bexI,simp_all)
  4934   (rule_tac x="(t,n)" in bexI,simp_all add: mult.commute)
  4935 next
  4936   fix t n s m
  4937   assume tnU: "(t,n) \<in> set U" and smU:"(s,m) \<in> set U"
  4938   let ?N = "\<lambda> t. Inum (x#bs) t"
  4939   let ?st= "Add (Mul m t) (Mul n s)"
  4940   from Ul smU have mnz: "m \<noteq> 0" by auto
  4941   from Ul tnU have  nnz: "n \<noteq> 0" by auto
  4942   have st: "(?N t / real_of_int n + ?N s / real_of_int m)/2 = ?N ?st / real_of_int (2*n*m)"
  4943    using mnz nnz by (simp add: algebra_simps add_divide_distrib)
  4944  let ?P = "\<lambda> (t',n') (s',m'). (Inum (x # bs) t / real_of_int n + Inum (x # bs) s / real_of_int m)/2 = (Inum (x # bs) t' / real_of_int n' + Inum (x # bs) s' / real_of_int m')/2"
  4945  have Pc:"\<forall> a b. ?P a b = ?P b a"
  4946    by auto
  4947  from Ul alluopairs_set1 have Up:"\<forall> ((t,n),(s,m)) \<in> set (alluopairs U). n \<noteq> 0 \<and> m \<noteq> 0" by blast
  4948  from alluopairs_ex[OF Pc, where xs="U"] tnU smU
  4949  have th':"\<exists> ((t',n'),(s',m')) \<in> set (alluopairs U). ?P (t',n') (s',m')"
  4950    by blast
  4951  then obtain t' n' s' m' where ts'_U: "((t',n'),(s',m')) \<in> set (alluopairs U)"
  4952    and Pts': "?P (t',n') (s',m')" by blast
  4953  from ts'_U Up have mnz': "m' \<noteq> 0" and nnz': "n'\<noteq> 0" by auto
  4954  let ?st' = "Add (Mul m' t') (Mul n' s')"
  4955    have st': "(?N t' / real_of_int n' + ?N s' / real_of_int m')/2 = ?N ?st' / real_of_int (2*n'*m')"
  4956    using mnz' nnz' by (simp add: algebra_simps add_divide_distrib)
  4957  from Pts' have
  4958    "(Inum (x # bs) t / real_of_int n + Inum (x # bs) s / real_of_int m)/2 = (Inum (x # bs) t' / real_of_int n' + Inum (x # bs) s' / real_of_int m')/2" by simp
  4959  also have "\<dots> = ((\<lambda>(t, n). Inum (x # bs) t / real_of_int n) ((\<lambda>((t, n), s, m). (Add (Mul m t) (Mul n s), 2 * n * m)) ((t',n'),(s',m'))))" by (simp add: st')
  4960  finally show "(Inum (x # bs) t / real_of_int n + Inum (x # bs) s / real_of_int m) / 2
  4961           \<in> (\<lambda>(t, n). Inum (x # bs) t / real_of_int n) `
  4962             (\<lambda>((t, n), s, m). (Add (Mul m t) (Mul n s), 2 * n * m)) `
  4963             set (alluopairs U)"
  4964    using ts'_U by blast
  4965 qed
  4966 
  4967 lemma \<Upsilon>_cong:
  4968   assumes lp: "isrlfm p"
  4969   and UU': "((\<lambda> (t,n). Inum (x#bs) t /real_of_int n) ` U') = ((\<lambda> ((t,n),(s,m)). (Inum (x#bs) t /real_of_int n + Inum (x#bs) s /real_of_int m)/2) ` (U \<times> U))" (is "?f ` U' = ?g ` (U\<times>U)")
  4970   and U: "\<forall> (t,n) \<in> U. numbound0 t \<and> n > 0"
  4971   and U': "\<forall> (t,n) \<in> U'. numbound0 t \<and> n > 0"
  4972   shows "(\<exists> (t,n) \<in> U. \<exists> (s,m) \<in> U. Ifm (x#bs) (\<upsilon> p (Add (Mul m t) (Mul n s),2*n*m))) = (\<exists> (t,n) \<in> U'. Ifm (x#bs) (\<upsilon> p (t,n)))"
  4973   (is "?lhs = ?rhs")
  4974 proof
  4975   assume ?lhs
  4976   then obtain t n s m where tnU: "(t,n) \<in> U" and smU:"(s,m) \<in> U" and
  4977     Pst: "Ifm (x#bs) (\<upsilon> p (Add (Mul m t) (Mul n s),2*n*m))" by blast
  4978   let ?N = "\<lambda> t. Inum (x#bs) t"
  4979   from tnU smU U have tnb: "numbound0 t" and np: "n > 0"
  4980     and snb: "numbound0 s" and mp:"m > 0"  by auto
  4981   let ?st= "Add (Mul m t) (Mul n s)"
  4982   from np mp have mnp: "real_of_int (2*n*m) > 0"
  4983       by (simp add: mult.commute of_int_mult[symmetric] del: of_int_mult)
  4984     from tnb snb have stnb: "numbound0 ?st" by simp
  4985   have st: "(?N t / real_of_int n + ?N s / real_of_int m)/2 = ?N ?st / real_of_int (2*n*m)"
  4986    using mp np by (simp add: algebra_simps add_divide_distrib)
  4987   from tnU smU UU' have "?g ((t,n),(s,m)) \<in> ?f ` U'" by blast
  4988   hence "\<exists> (t',n') \<in> U'. ?g ((t,n),(s,m)) = ?f (t',n')"
  4989     by auto (rule_tac x="(a,b)" in bexI, auto)
  4990   then obtain t' n' where tnU': "(t',n') \<in> U'" and th: "?g ((t,n),(s,m)) = ?f (t',n')" by blast
  4991   from U' tnU' have tnb': "numbound0 t'" and np': "real_of_int n' > 0" by auto
  4992   from \<upsilon>_I[OF lp mnp stnb, where bs="bs" and x="x"] Pst
  4993   have Pst2: "Ifm (Inum (x # bs) (Add (Mul m t) (Mul n s)) / real_of_int (2 * n * m) # bs) p" by simp
  4994   from conjunct1[OF \<upsilon>_I[OF lp np' tnb', where bs="bs" and x="x"], symmetric] th[simplified split_def fst_conv snd_conv,symmetric] Pst2[simplified st[symmetric]]
  4995   have "Ifm (x # bs) (\<upsilon> p (t', n')) " by (simp only: st)
  4996   then show ?rhs using tnU' by auto
  4997 next
  4998   assume ?rhs
  4999   then obtain t' n' where tnU': "(t',n') \<in> U'" and Pt': "Ifm (x # bs) (\<upsilon> p (t', n'))"
  5000     by blast
  5001   from tnU' UU' have "?f (t',n') \<in> ?g ` (U\<times>U)" by blast
  5002   hence "\<exists> ((t,n),(s,m)) \<in> (U\<times>U). ?f (t',n') = ?g ((t,n),(s,m))"
  5003     by auto (rule_tac x="(a,b)" in bexI, auto)
  5004   then obtain t n s m where tnU: "(t,n) \<in> U" and smU:"(s,m) \<in> U" and
  5005     th: "?f (t',n') = ?g((t,n),(s,m)) "by blast
  5006     let ?N = "\<lambda> t. Inum (x#bs) t"
  5007   from tnU smU U have tnb: "numbound0 t" and np: "n > 0"
  5008     and snb: "numbound0 s" and mp:"m > 0"  by auto
  5009   let ?st= "Add (Mul m t) (Mul n s)"
  5010   from np mp have mnp: "real_of_int (2*n*m) > 0"
  5011       by (simp add: mult.commute of_int_mult[symmetric] del: of_int_mult)
  5012     from tnb snb have stnb: "numbound0 ?st" by simp
  5013   have st: "(?N t / real_of_int n + ?N s / real_of_int m)/2 = ?N ?st / real_of_int (2*n*m)"
  5014    using mp np by (simp add: algebra_simps add_divide_distrib)
  5015   from U' tnU' have tnb': "numbound0 t'" and np': "real_of_int n' > 0" by auto
  5016   from \<upsilon>_I[OF lp np' tnb', where bs="bs" and x="x",simplified th[simplified split_def fst_conv snd_conv] st] Pt'
  5017   have Pst2: "Ifm (Inum (x # bs) (Add (Mul m t) (Mul n s)) / real_of_int (2 * n * m) # bs) p" by simp
  5018   with \<upsilon>_I[OF lp mnp stnb, where x="x" and bs="bs"] tnU smU show ?lhs by blast
  5019 qed
  5020 
  5021 lemma ferrack01:
  5022   assumes qf: "qfree p"
  5023   shows "((\<exists> x. Ifm (x#bs) (And (And (Ge(CN 0 1 (C 0))) (Lt (CN 0 1 (C (- 1))))) p)) = (Ifm bs (ferrack01 p))) \<and> qfree (ferrack01 p)" (is "(?lhs = ?rhs) \<and> _")
  5024 proof-
  5025   let ?I = "\<lambda> x p. Ifm (x#bs) p"
  5026   fix x
  5027   let ?N = "\<lambda> t. Inum (x#bs) t"
  5028   let ?q = "rlfm (And (And (Ge(CN 0 1 (C 0))) (Lt (CN 0 1 (C (- 1))))) p)"
  5029   let ?U = "\<Upsilon> ?q"
  5030   let ?Up = "alluopairs ?U"
  5031   let ?g = "\<lambda> ((t,n),(s,m)). (Add (Mul m t) (Mul n s) , 2*n*m)"
  5032   let ?S = "map ?g ?Up"
  5033   let ?SS = "map simp_num_pair ?S"
  5034   let ?Y = "remdups ?SS"
  5035   let ?f= "(\<lambda> (t,n). ?N t / real_of_int n)"
  5036   let ?h = "\<lambda> ((t,n),(s,m)). (?N t/real_of_int n + ?N s/ real_of_int m) /2"
  5037   let ?F = "\<lambda> p. \<exists> a \<in> set (\<Upsilon> p). \<exists> b \<in> set (\<Upsilon> p). ?I x (\<upsilon> p (?g(a,b)))"
  5038   let ?ep = "evaldjf (\<upsilon> ?q) ?Y"
  5039   from rlfm_l[OF qf] have lq: "isrlfm ?q"
  5040     by (simp add: rsplit_def lt_def ge_def conj_def disj_def Let_def reducecoeff_def numgcd_def)
  5041   from alluopairs_set1[where xs="?U"] have UpU: "set ?Up \<le> (set ?U \<times> set ?U)" by simp
  5042   from \<Upsilon>_l[OF lq] have U_l: "\<forall> (t,n) \<in> set ?U. numbound0 t \<and> n > 0" .
  5043   from U_l UpU
  5044   have "\<forall> ((t,n),(s,m)) \<in> set ?Up. numbound0 t \<and> n> 0 \<and> numbound0 s \<and> m > 0" by auto
  5045   hence Snb: "\<forall> (t,n) \<in> set ?S. numbound0 t \<and> n > 0 "
  5046     by (auto)
  5047   have Y_l: "\<forall> (t,n) \<in> set ?Y. numbound0 t \<and> n > 0"
  5048   proof-
  5049     { fix t n assume tnY: "(t,n) \<in> set ?Y"
  5050       hence "(t,n) \<in> set ?SS" by simp
  5051       hence "\<exists> (t',n') \<in> set ?S. simp_num_pair (t',n') = (t,n)"
  5052         by (auto simp add: split_def simp del: map_map)
  5053            (rule_tac x="((aa,ba),(ab,bb))" in bexI, simp_all)
  5054       then obtain t' n' where tn'S: "(t',n') \<in> set ?S" and tns: "simp_num_pair (t',n') = (t,n)" by blast
  5055       from tn'S Snb have tnb: "numbound0 t'" and np: "n' > 0" by auto
  5056       from simp_num_pair_l[OF tnb np tns]
  5057       have "numbound0 t \<and> n > 0" . }
  5058     thus ?thesis by blast
  5059   qed
  5060 
  5061   have YU: "(?f ` set ?Y) = (?h ` (set ?U \<times> set ?U))"
  5062   proof-
  5063      from simp_num_pair_ci[where bs="x#bs"] have
  5064     "\<forall>x. (?f o simp_num_pair) x = ?f x" by auto
  5065      hence th: "?f o simp_num_pair = ?f" using ext by blast
  5066     have "(?f ` set ?Y) = ((?f o simp_num_pair) ` set ?S)" by (simp add: image_comp comp_assoc)
  5067     also have "\<dots> = (?f ` set ?S)" by (simp add: th)
  5068     also have "\<dots> = ((?f o ?g) ` set ?Up)"
  5069       by (simp only: set_map o_def image_comp)
  5070     also have "\<dots> = (?h ` (set ?U \<times> set ?U))"
  5071       using \<Upsilon>_cong_aux[OF U_l, where x="x" and bs="bs", simplified set_map image_comp] by blast
  5072     finally show ?thesis .
  5073   qed
  5074   have "\<forall> (t,n) \<in> set ?Y. bound0 (\<upsilon> ?q (t,n))"
  5075   proof-
  5076     { fix t n assume tnY: "(t,n) \<in> set ?Y"
  5077       with Y_l have tnb: "numbound0 t" and np: "real_of_int n > 0" by auto
  5078       from \<upsilon>_I[OF lq np tnb]
  5079     have "bound0 (\<upsilon> ?q (t,n))"  by simp}
  5080     thus ?thesis by blast
  5081   qed
  5082   hence ep_nb: "bound0 ?ep"  using evaldjf_bound0[where xs="?Y" and f="\<upsilon> ?q"]
  5083     by auto
  5084 
  5085   from fr_eq_01[OF qf, where bs="bs" and x="x"] have "?lhs = ?F ?q"
  5086     by (simp only: split_def fst_conv snd_conv)
  5087   also have "\<dots> = (\<exists> (t,n) \<in> set ?Y. ?I x (\<upsilon> ?q (t,n)))" using \<Upsilon>_cong[OF lq YU U_l Y_l]
  5088     by (simp only: split_def fst_conv snd_conv)
  5089   also have "\<dots> = (Ifm (x#bs) ?ep)"
  5090     using evaldjf_ex[where ps="?Y" and bs = "x#bs" and f="\<upsilon> ?q",symmetric]
  5091     by (simp only: split_def prod.collapse)
  5092   also have "\<dots> = (Ifm bs (decr ?ep))" using decr[OF ep_nb] by blast
  5093   finally have lr: "?lhs = ?rhs" by (simp only: ferrack01_def Let_def)
  5094   from decr_qf[OF ep_nb] have "qfree (ferrack01 p)" by (simp only: Let_def ferrack01_def)
  5095   with lr show ?thesis by blast
  5096 qed
  5097 
  5098 lemma cp_thm':
  5099   assumes lp: "iszlfm p (real_of_int (i::int)#bs)"
  5100   and up: "d_\<beta> p 1" and dd: "d_\<delta> p d" and dp: "d > 0"
  5101   shows "(\<exists> (x::int). Ifm (real_of_int x#bs) p) = ((\<exists> j\<in> {1 .. d}. Ifm (real_of_int j#bs) (minusinf p)) \<or> (\<exists> j\<in> {1.. d}. \<exists> b\<in> (Inum (real_of_int i#bs)) ` set (\<beta> p). Ifm ((b+real_of_int j)#bs) p))"
  5102   using cp_thm[OF lp up dd dp] by auto
  5103 
  5104 definition unit :: "fm \<Rightarrow> fm \<times> num list \<times> int" where
  5105   "unit p \<equiv> (let p' = zlfm p ; l = \<zeta> p' ; q = And (Dvd l (CN 0 1 (C 0))) (a_\<beta> p' l); d = \<delta> q;
  5106              B = remdups (map simpnum (\<beta> q)) ; a = remdups (map simpnum (\<alpha> q))
  5107              in if length B \<le> length a then (q,B,d) else (mirror q, a,d))"
  5108 
  5109 lemma unit: assumes qf: "qfree p"
  5110   shows "\<And> q B d. unit p = (q,B,d) \<Longrightarrow>
  5111       ((\<exists> (x::int). Ifm (real_of_int x#bs) p) =
  5112        (\<exists> (x::int). Ifm (real_of_int x#bs) q)) \<and>
  5113        (Inum (real_of_int i#bs)) ` set B = (Inum (real_of_int i#bs)) ` set (\<beta> q) \<and>
  5114        d_\<beta> q 1 \<and> d_\<delta> q d \<and> d >0 \<and> iszlfm q (real_of_int (i::int)#bs) \<and> (\<forall> b\<in> set B. numbound0 b)"
  5115 proof-
  5116   fix q B d
  5117   assume qBd: "unit p = (q,B,d)"
  5118   let ?thes = "((\<exists> (x::int). Ifm (real_of_int x#bs) p) = (\<exists> (x::int). Ifm (real_of_int x#bs) q)) \<and>
  5119     Inum (real_of_int i#bs) ` set B = Inum (real_of_int i#bs) ` set (\<beta> q) \<and>
  5120     d_\<beta> q 1 \<and> d_\<delta> q d \<and> 0 < d \<and> iszlfm q (real_of_int i # bs) \<and> (\<forall> b\<in> set B. numbound0 b)"
  5121   let ?I = "\<lambda> (x::int) p. Ifm (real_of_int x#bs) p"
  5122   let ?p' = "zlfm p"
  5123   let ?l = "\<zeta> ?p'"
  5124   let ?q = "And (Dvd ?l (CN 0 1 (C 0))) (a_\<beta> ?p' ?l)"
  5125   let ?d = "\<delta> ?q"
  5126   let ?B = "set (\<beta> ?q)"
  5127   let ?B'= "remdups (map simpnum (\<beta> ?q))"
  5128   let ?A = "set (\<alpha> ?q)"
  5129   let ?A'= "remdups (map simpnum (\<alpha> ?q))"
  5130   from conjunct1[OF zlfm_I[OF qf, where bs="bs"]]
  5131   have pp': "\<forall> i. ?I i ?p' = ?I i p" by auto
  5132   from iszlfm_gen[OF conjunct2[OF zlfm_I[OF qf, where bs="bs" and i="i"]]]
  5133   have lp': "\<forall> (i::int). iszlfm ?p' (real_of_int i#bs)" by simp
  5134   hence lp'': "iszlfm ?p' (real_of_int (i::int)#bs)" by simp
  5135   from lp' \<zeta>[where p="?p'" and bs="bs"] have lp: "?l >0" and dl: "d_\<beta> ?p' ?l" by auto
  5136   from a_\<beta>_ex[where p="?p'" and l="?l" and bs="bs", OF lp'' dl lp] pp'
  5137   have pq_ex:"(\<exists> (x::int). ?I x p) = (\<exists> x. ?I x ?q)" by (simp add: int_rdvd_iff)
  5138   from lp'' lp a_\<beta>[OF lp'' dl lp] have lq:"iszlfm ?q (real_of_int i#bs)" and uq: "d_\<beta> ?q 1"
  5139     by (auto simp add: isint_def)
  5140   from \<delta>[OF lq] have dp:"?d >0" and dd: "d_\<delta> ?q ?d" by blast+
  5141   let ?N = "\<lambda> t. Inum (real_of_int (i::int)#bs) t"
  5142   have "?N ` set ?B' = ((?N o simpnum) ` ?B)" by (simp add:image_comp)
  5143   also have "\<dots> = ?N ` ?B" using simpnum_ci[where bs="real_of_int i #bs"] by auto
  5144   finally have BB': "?N ` set ?B' = ?N ` ?B" .
  5145   have "?N ` set ?A' = ((?N o simpnum) ` ?A)" by (simp add:image_comp)
  5146   also have "\<dots> = ?N ` ?A" using simpnum_ci[where bs="real_of_int i #bs"] by auto
  5147   finally have AA': "?N ` set ?A' = ?N ` ?A" .
  5148   from \<beta>_numbound0[OF lq] have B_nb:"\<forall> b\<in> set ?B'. numbound0 b"
  5149     by simp
  5150   from \<alpha>_l[OF lq] have A_nb: "\<forall> b\<in> set ?A'. numbound0 b"
  5151     by simp
  5152   { assume "length ?B' \<le> length ?A'"
  5153     hence q:"q=?q" and "B = ?B'" and d:"d = ?d"
  5154       using qBd by (auto simp add: Let_def unit_def)
  5155     with BB' B_nb have b: "?N ` (set B) = ?N ` set (\<beta> q)"
  5156       and bn: "\<forall>b\<in> set B. numbound0 b" by simp+
  5157     with pq_ex dp uq dd lq q d have ?thes by simp }
  5158   moreover
  5159   { assume "\<not> (length ?B' \<le> length ?A')"
  5160     hence q:"q=mirror ?q" and "B = ?A'" and d:"d = ?d"
  5161       using qBd by (auto simp add: Let_def unit_def)
  5162     with AA' mirror_\<alpha>_\<beta>[OF lq] A_nb have b:"?N ` (set B) = ?N ` set (\<beta> q)"
  5163       and bn: "\<forall>b\<in> set B. numbound0 b" by simp+
  5164     from mirror_ex[OF lq] pq_ex q
  5165     have pqm_eq:"(\<exists> (x::int). ?I x p) = (\<exists> (x::int). ?I x q)" by simp
  5166     from lq uq q mirror_d_\<beta> [where p="?q" and bs="bs" and a="real_of_int i"]
  5167     have lq': "iszlfm q (real_of_int i#bs)" and uq: "d_\<beta> q 1" by auto
  5168     from \<delta>[OF lq'] mirror_\<delta>[OF lq] q d have dq:"d_\<delta> q d " by auto
  5169     from pqm_eq b bn uq lq' dp dq q dp d have ?thes by simp
  5170   }
  5171   ultimately show ?thes by blast
  5172 qed
  5173     (* Cooper's Algorithm *)
  5174 
  5175 definition cooper :: "fm \<Rightarrow> fm" where
  5176   "cooper p \<equiv>
  5177   (let (q,B,d) = unit p; js = [1..d];
  5178        mq = simpfm (minusinf q);
  5179        md = evaldjf (\<lambda> j. simpfm (subst0 (C j) mq)) js
  5180    in if md = T then T else
  5181     (let qd = evaldjf (\<lambda> t. simpfm (subst0 t q))
  5182                                (remdups (map (\<lambda> (b,j). simpnum (Add b (C j)))
  5183                                             [(b,j). b\<leftarrow>B,j\<leftarrow>js]))
  5184      in decr (disj md qd)))"
  5185 lemma cooper: assumes qf: "qfree p"
  5186   shows "((\<exists> (x::int). Ifm (real_of_int x#bs) p) = (Ifm bs (cooper p))) \<and> qfree (cooper p)"
  5187   (is "(?lhs = ?rhs) \<and> _")
  5188 proof-
  5189 
  5190   let ?I = "\<lambda> (x::int) p. Ifm (real_of_int x#bs) p"
  5191   let ?q = "fst (unit p)"
  5192   let ?B = "fst (snd(unit p))"
  5193   let ?d = "snd (snd (unit p))"
  5194   let ?js = "[1..?d]"
  5195   let ?mq = "minusinf ?q"
  5196   let ?smq = "simpfm ?mq"
  5197   let ?md = "evaldjf (\<lambda> j. simpfm (subst0 (C j) ?smq)) ?js"
  5198   fix i
  5199   let ?N = "\<lambda> t. Inum (real_of_int (i::int)#bs) t"
  5200   let ?bjs = "[(b,j). b\<leftarrow>?B,j\<leftarrow>?js]"
  5201   let ?sbjs = "map (\<lambda> (b,j). simpnum (Add b (C j))) ?bjs"
  5202   let ?qd = "evaldjf (\<lambda> t. simpfm (subst0 t ?q)) (remdups ?sbjs)"
  5203   have qbf:"unit p = (?q,?B,?d)" by simp
  5204   from unit[OF qf qbf] have pq_ex: "(\<exists>(x::int). ?I x p) = (\<exists> (x::int). ?I x ?q)" and
  5205     B:"?N ` set ?B = ?N ` set (\<beta> ?q)" and
  5206     uq:"d_\<beta> ?q 1" and dd: "d_\<delta> ?q ?d" and dp: "?d > 0" and
  5207     lq: "iszlfm ?q (real_of_int i#bs)" and
  5208     Bn: "\<forall> b\<in> set ?B. numbound0 b" by auto
  5209   from zlin_qfree[OF lq] have qfq: "qfree ?q" .
  5210   from simpfm_qf[OF minusinf_qfree[OF qfq]] have qfmq: "qfree ?smq".
  5211   have jsnb: "\<forall> j \<in> set ?js. numbound0 (C j)" by simp
  5212   hence "\<forall> j\<in> set ?js. bound0 (subst0 (C j) ?smq)"
  5213     by (auto simp only: subst0_bound0[OF qfmq])
  5214   hence th: "\<forall> j\<in> set ?js. bound0 (simpfm (subst0 (C j) ?smq))"
  5215     by auto
  5216   from evaldjf_bound0[OF th] have mdb: "bound0 ?md" by simp
  5217   from Bn jsnb have "\<forall> (b,j) \<in> set ?bjs. numbound0 (Add b (C j))"
  5218     by simp
  5219   hence "\<forall> (b,j) \<in> set ?bjs. numbound0 (simpnum (Add b (C j)))"
  5220     using simpnum_numbound0 by blast
  5221   hence "\<forall> t \<in> set ?sbjs. numbound0 t" by simp
  5222   hence "\<forall> t \<in> set (remdups ?sbjs). bound0 (subst0 t ?q)"
  5223     using subst0_bound0[OF qfq] by auto
  5224   hence th': "\<forall> t \<in> set (remdups ?sbjs). bound0 (simpfm (subst0 t ?q))"
  5225     using simpfm_bound0 by blast
  5226   from evaldjf_bound0 [OF th'] have qdb: "bound0 ?qd" by simp
  5227   from mdb qdb
  5228   have mdqdb: "bound0 (disj ?md ?qd)" by (simp only: disj_def, cases "?md=T \<or> ?qd=T", simp_all)
  5229   from trans [OF pq_ex cp_thm'[OF lq uq dd dp]] B
  5230   have "?lhs = (\<exists> j\<in> {1.. ?d}. ?I j ?mq \<or> (\<exists> b\<in> ?N ` set ?B. Ifm ((b+ real_of_int j)#bs) ?q))" by auto
  5231   also have "\<dots> = ((\<exists> j\<in> set ?js. ?I j ?smq) \<or> (\<exists> (b,j) \<in> (?N ` set ?B \<times> set ?js). Ifm ((b+ real_of_int j)#bs) ?q))" by auto
  5232   also have "\<dots>= ((\<exists> j\<in> set ?js. ?I j ?smq) \<or> (\<exists> t \<in> (\<lambda> (b,j). ?N (Add b (C j))) ` set ?bjs. Ifm (t #bs) ?q))" by simp
  5233   also have "\<dots>= ((\<exists> j\<in> set ?js. ?I j ?smq) \<or> (\<exists> t \<in> (\<lambda> (b,j). ?N (simpnum (Add b (C j)))) ` set ?bjs. Ifm (t #bs) ?q))" by (simp only: simpnum_ci)
  5234   also  have "\<dots>= ((\<exists> j\<in> set ?js. ?I j ?smq) \<or> (\<exists> t \<in> set ?sbjs. Ifm (?N t #bs) ?q))"
  5235     by (auto simp add: split_def)
  5236   also have "\<dots> = ((\<exists> j\<in> set ?js. (\<lambda> j. ?I i (simpfm (subst0 (C j) ?smq))) j) \<or> (\<exists> t \<in> set (remdups ?sbjs). (\<lambda> t. ?I i (simpfm (subst0 t ?q))) t))"
  5237     by (simp only: simpfm subst0_I[OF qfq] Inum.simps subst0_I[OF qfmq] set_remdups)
  5238   also have "\<dots> = ((?I i (evaldjf (\<lambda> j. simpfm (subst0 (C j) ?smq)) ?js)) \<or> (?I i (evaldjf (\<lambda> t. simpfm (subst0 t ?q)) (remdups ?sbjs))))" by (simp only: evaldjf_ex)
  5239   finally have mdqd: "?lhs = (?I i (disj ?md ?qd))" by simp
  5240   hence mdqd2: "?lhs = (Ifm bs (decr (disj ?md ?qd)))" using decr [OF mdqdb] by simp
  5241   {assume mdT: "?md = T"
  5242     hence cT:"cooper p = T"
  5243       by (simp only: cooper_def unit_def split_def Let_def if_True) simp
  5244     from mdT mdqd have lhs:"?lhs" by auto
  5245     from mdT have "?rhs" by (simp add: cooper_def unit_def split_def)
  5246     with lhs cT have ?thesis by simp }
  5247   moreover
  5248   {assume mdT: "?md \<noteq> T" hence "cooper p = decr (disj ?md ?qd)"
  5249       by (simp only: cooper_def unit_def split_def Let_def if_False)
  5250     with mdqd2 decr_qf[OF mdqdb] have ?thesis by simp }
  5251   ultimately show ?thesis by blast
  5252 qed
  5253 
  5254 lemma DJcooper:
  5255   assumes qf: "qfree p"
  5256   shows "((\<exists> (x::int). Ifm (real_of_int x#bs) p) = (Ifm bs (DJ cooper p))) \<and> qfree (DJ cooper p)"
  5257 proof-
  5258   from cooper have cqf: "\<forall> p. qfree p \<longrightarrow> qfree (cooper p)" by  blast
  5259   from DJ_qf[OF cqf] qf have thqf:"qfree (DJ cooper p)" by blast
  5260   have "Ifm bs (DJ cooper p) = (\<exists> q\<in> set (disjuncts p). Ifm bs (cooper q))"
  5261      by (simp add: DJ_def evaldjf_ex)
  5262   also have "\<dots> = (\<exists> q \<in> set(disjuncts p). \<exists> (x::int). Ifm (real_of_int x#bs)  q)"
  5263     using cooper disjuncts_qf[OF qf] by blast
  5264   also have "\<dots> = (\<exists> (x::int). Ifm (real_of_int x#bs) p)" by (induct p rule: disjuncts.induct, auto)
  5265   finally show ?thesis using thqf by blast
  5266 qed
  5267 
  5268     (* Redy and Loveland *)
  5269 
  5270 lemma \<sigma>_\<rho>_cong: assumes lp: "iszlfm p (a#bs)" and tt': "Inum (a#bs) t = Inum (a#bs) t'"
  5271   shows "Ifm (a#bs) (\<sigma>_\<rho> p (t,c)) = Ifm (a#bs) (\<sigma>_\<rho> p (t',c))"
  5272   using lp
  5273   by (induct p rule: iszlfm.induct, auto simp add: tt')
  5274 
  5275 lemma \<sigma>_cong: assumes lp: "iszlfm p (a#bs)" and tt': "Inum (a#bs) t = Inum (a#bs) t'"
  5276   shows "Ifm (a#bs) (\<sigma> p c t) = Ifm (a#bs) (\<sigma> p c t')"
  5277   by (simp add: \<sigma>_def tt' \<sigma>_\<rho>_cong[OF lp tt'])
  5278 
  5279 lemma \<rho>_cong: assumes lp: "iszlfm p (a#bs)"
  5280   and RR: "(\<lambda>(b,k). (Inum (a#bs) b,k)) ` R =  (\<lambda>(b,k). (Inum (a#bs) b,k)) ` set (\<rho> p)"
  5281   shows "(\<exists> (e,c) \<in> R. \<exists> j\<in> {1.. c*(\<delta> p)}. Ifm (a#bs) (\<sigma> p c (Add e (C j)))) = (\<exists> (e,c) \<in> set (\<rho> p). \<exists> j\<in> {1.. c*(\<delta> p)}. Ifm (a#bs) (\<sigma> p c (Add e (C j))))"
  5282   (is "?lhs = ?rhs")
  5283 proof
  5284   let ?d = "\<delta> p"
  5285   assume ?lhs then obtain e c j where ecR: "(e,c) \<in> R" and jD:"j \<in> {1 .. c*?d}"
  5286     and px: "Ifm (a#bs) (\<sigma> p c (Add e (C j)))" (is "?sp c e j") by blast
  5287   from ecR have "(Inum (a#bs) e,c) \<in> (\<lambda>(b,k). (Inum (a#bs) b,k)) ` R" by auto
  5288   hence "(Inum (a#bs) e,c) \<in> (\<lambda>(b,k). (Inum (a#bs) b,k)) ` set (\<rho> p)" using RR by simp
  5289   hence "\<exists> (e',c') \<in> set (\<rho> p). Inum (a#bs) e = Inum (a#bs) e' \<and> c = c'" by auto
  5290   then obtain e' c' where ecRo:"(e',c') \<in> set (\<rho> p)" and ee':"Inum (a#bs) e = Inum (a#bs) e'"
  5291     and cc':"c = c'" by blast
  5292   from ee' have tt': "Inum (a#bs) (Add e (C j)) = Inum (a#bs) (Add e' (C j))" by simp
  5293 
  5294   from \<sigma>_cong[OF lp tt', where c="c"] px have px':"?sp c e' j" by simp
  5295   from ecRo jD px' show ?rhs apply (auto simp: cc')
  5296     by (rule_tac x="(e', c')" in bexI,simp_all)
  5297   (rule_tac x="j" in bexI, simp_all add: cc'[symmetric])
  5298 next
  5299   let ?d = "\<delta> p"
  5300   assume ?rhs then obtain e c j where ecR: "(e,c) \<in> set (\<rho> p)" and jD:"j \<in> {1 .. c*?d}"
  5301     and px: "Ifm (a#bs) (\<sigma> p c (Add e (C j)))" (is "?sp c e j") by blast
  5302   from ecR have "(Inum (a#bs) e,c) \<in> (\<lambda>(b,k). (Inum (a#bs) b,k)) ` set (\<rho> p)" by auto
  5303   hence "(Inum (a#bs) e,c) \<in> (\<lambda>(b,k). (Inum (a#bs) b,k)) ` R" using RR by simp
  5304   hence "\<exists> (e',c') \<in> R. Inum (a#bs) e = Inum (a#bs) e' \<and> c = c'" by auto
  5305   then obtain e' c' where ecRo:"(e',c') \<in> R" and ee':"Inum (a#bs) e = Inum (a#bs) e'"
  5306     and cc':"c = c'" by blast
  5307   from ee' have tt': "Inum (a#bs) (Add e (C j)) = Inum (a#bs) (Add e' (C j))" by simp
  5308   from \<sigma>_cong[OF lp tt', where c="c"] px have px':"?sp c e' j" by simp
  5309   from ecRo jD px' show ?lhs apply (auto simp: cc')
  5310     by (rule_tac x="(e', c')" in bexI,simp_all)
  5311   (rule_tac x="j" in bexI, simp_all add: cc'[symmetric])
  5312 qed
  5313 
  5314 lemma rl_thm':
  5315   assumes lp: "iszlfm p (real_of_int (i::int)#bs)"
  5316   and R: "(\<lambda>(b,k). (Inum (a#bs) b,k)) ` R =  (\<lambda>(b,k). (Inum (a#bs) b,k)) ` set (\<rho> p)"
  5317   shows "(\<exists> (x::int). Ifm (real_of_int x#bs) p) = ((\<exists> j\<in> {1 .. \<delta> p}. Ifm (real_of_int j#bs) (minusinf p)) \<or> (\<exists> (e,c) \<in> R. \<exists> j\<in> {1.. c*(\<delta> p)}. Ifm (a#bs) (\<sigma> p c (Add e (C j)))))"
  5318   using rl_thm[OF lp] \<rho>_cong[OF iszlfm_gen[OF lp, rule_format, where y="a"] R] by simp
  5319 
  5320 definition chooset :: "fm \<Rightarrow> fm \<times> ((num\<times>int) list) \<times> int" where
  5321   "chooset p \<equiv> (let q = zlfm p ; d = \<delta> q;
  5322              B = remdups (map (\<lambda> (t,k). (simpnum t,k)) (\<rho> q)) ;
  5323              a = remdups (map (\<lambda> (t,k). (simpnum t,k)) (\<alpha>_\<rho> q))
  5324              in if length B \<le> length a then (q,B,d) else (mirror q, a,d))"
  5325 
  5326 lemma chooset: assumes qf: "qfree p"
  5327   shows "\<And> q B d. chooset p = (q,B,d) \<Longrightarrow>
  5328      ((\<exists> (x::int). Ifm (real_of_int x#bs) p) =
  5329       (\<exists> (x::int). Ifm (real_of_int x#bs) q)) \<and>
  5330       ((\<lambda>(t,k). (Inum (real_of_int i#bs) t,k)) ` set B = (\<lambda>(t,k). (Inum (real_of_int i#bs) t,k)) ` set (\<rho> q)) \<and>
  5331       (\<delta> q = d) \<and> d >0 \<and> iszlfm q (real_of_int (i::int)#bs) \<and> (\<forall> (e,c)\<in> set B. numbound0 e \<and> c>0)"
  5332 proof-
  5333   fix q B d
  5334   assume qBd: "chooset p = (q,B,d)"
  5335   let ?thes = "((\<exists> (x::int). Ifm (real_of_int x#bs) p) =
  5336              (\<exists> (x::int). Ifm (real_of_int x#bs) q)) \<and> ((\<lambda>(t,k). (Inum (real_of_int i#bs) t,k)) ` set B = (\<lambda>(t,k). (Inum (real_of_int i#bs) t,k)) ` set (\<rho> q)) \<and>
  5337              (\<delta> q = d) \<and> d >0 \<and> iszlfm q (real_of_int (i::int)#bs) \<and> (\<forall> (e,c)\<in> set B. numbound0 e \<and> c>0)"
  5338   let ?I = "\<lambda> (x::int) p. Ifm (real_of_int x#bs) p"
  5339   let ?q = "zlfm p"
  5340   let ?d = "\<delta> ?q"
  5341   let ?B = "set (\<rho> ?q)"
  5342   let ?f = "\<lambda> (t,k). (simpnum t,k)"
  5343   let ?B'= "remdups (map ?f (\<rho> ?q))"
  5344   let ?A = "set (\<alpha>_\<rho> ?q)"
  5345   let ?A'= "remdups (map ?f (\<alpha>_\<rho> ?q))"
  5346   from conjunct1[OF zlfm_I[OF qf, where bs="bs"]]
  5347   have pp': "\<forall> i. ?I i ?q = ?I i p" by auto
  5348   hence pq_ex:"(\<exists> (x::int). ?I x p) = (\<exists> x. ?I x ?q)" by simp
  5349   from iszlfm_gen[OF conjunct2[OF zlfm_I[OF qf, where bs="bs" and i="i"]], rule_format, where y="real_of_int i"]
  5350   have lq: "iszlfm ?q (real_of_int (i::int)#bs)" .
  5351   from \<delta>[OF lq] have dp:"?d >0" by blast
  5352   let ?N = "\<lambda> (t,c). (Inum (real_of_int (i::int)#bs) t,c)"
  5353   have "?N ` set ?B' = ((?N o ?f) ` ?B)" by (simp add: split_def image_comp)
  5354   also have "\<dots> = ?N ` ?B"
  5355     by(simp add: split_def image_comp simpnum_ci[where bs="real_of_int i #bs"] image_def)
  5356   finally have BB': "?N ` set ?B' = ?N ` ?B" .
  5357   have "?N ` set ?A' = ((?N o ?f) ` ?A)" by (simp add: split_def image_comp)
  5358   also have "\<dots> = ?N ` ?A" using simpnum_ci[where bs="real_of_int i #bs"]
  5359     by(simp add: split_def image_comp simpnum_ci[where bs="real_of_int i #bs"] image_def)
  5360   finally have AA': "?N ` set ?A' = ?N ` ?A" .
  5361   from \<rho>_l[OF lq] have B_nb:"\<forall> (e,c)\<in> set ?B'. numbound0 e \<and> c > 0"
  5362     by (simp add: split_def)
  5363   from \<alpha>_\<rho>_l[OF lq] have A_nb: "\<forall> (e,c)\<in> set ?A'. numbound0 e \<and> c > 0"
  5364     by (simp add: split_def)
  5365     {assume "length ?B' \<le> length ?A'"
  5366     hence q:"q=?q" and "B = ?B'" and d:"d = ?d"
  5367       using qBd by (auto simp add: Let_def chooset_def)
  5368     with BB' B_nb have b: "?N ` (set B) = ?N ` set (\<rho> q)"
  5369       and bn: "\<forall>(e,c)\<in> set B. numbound0 e \<and> c > 0" by auto
  5370   with pq_ex dp lq q d have ?thes by simp}
  5371   moreover
  5372   {assume "\<not> (length ?B' \<le> length ?A')"
  5373     hence q:"q=mirror ?q" and "B = ?A'" and d:"d = ?d"
  5374       using qBd by (auto simp add: Let_def chooset_def)
  5375     with AA' mirror_\<alpha>_\<rho>[OF lq] A_nb have b:"?N ` (set B) = ?N ` set (\<rho> q)"
  5376       and bn: "\<forall>(e,c)\<in> set B. numbound0 e \<and> c > 0" by auto
  5377     from mirror_ex[OF lq] pq_ex q
  5378     have pqm_eq:"(\<exists> (x::int). ?I x p) = (\<exists> (x::int). ?I x q)" by simp
  5379     from lq q mirror_l [where p="?q" and bs="bs" and a="real_of_int i"]
  5380     have lq': "iszlfm q (real_of_int i#bs)" by auto
  5381     from mirror_\<delta>[OF lq] pqm_eq b bn lq' dp q dp d have ?thes by simp
  5382   }
  5383   ultimately show ?thes by blast
  5384 qed
  5385 
  5386 definition stage :: "fm \<Rightarrow> int \<Rightarrow> (num \<times> int) \<Rightarrow> fm" where
  5387   "stage p d \<equiv> (\<lambda> (e,c). evaldjf (\<lambda> j. simpfm (\<sigma> p c (Add e (C j)))) [1..c*d])"
  5388 lemma stage:
  5389   shows "Ifm bs (stage p d (e,c)) = (\<exists> j\<in>{1 .. c*d}. Ifm bs (\<sigma> p c (Add e (C j))))"
  5390   by (unfold stage_def split_def ,simp only: evaldjf_ex simpfm) simp
  5391 
  5392 lemma stage_nb: assumes lp: "iszlfm p (a#bs)" and cp: "c >0" and nb:"numbound0 e"
  5393   shows "bound0 (stage p d (e,c))"
  5394 proof-
  5395   let ?f = "\<lambda> j. simpfm (\<sigma> p c (Add e (C j)))"
  5396   have th: "\<forall> j\<in> set [1..c*d]. bound0 (?f j)"
  5397   proof
  5398     fix j
  5399     from nb have nb':"numbound0 (Add e (C j))" by simp
  5400     from simpfm_bound0[OF \<sigma>_nb[OF lp nb', where k="c"]]
  5401     show "bound0 (simpfm (\<sigma> p c (Add e (C j))))" .
  5402   qed
  5403   from evaldjf_bound0[OF th] show ?thesis by (unfold stage_def split_def) simp
  5404 qed
  5405 
  5406 definition redlove :: "fm \<Rightarrow> fm" where
  5407   "redlove p \<equiv>
  5408   (let (q,B,d) = chooset p;
  5409        mq = simpfm (minusinf q);
  5410        md = evaldjf (\<lambda> j. simpfm (subst0 (C j) mq)) [1..d]
  5411    in if md = T then T else
  5412     (let qd = evaldjf (stage q d) B
  5413      in decr (disj md qd)))"
  5414 
  5415 lemma redlove: assumes qf: "qfree p"
  5416   shows "((\<exists> (x::int). Ifm (real_of_int x#bs) p) = (Ifm bs (redlove p))) \<and> qfree (redlove p)"
  5417   (is "(?lhs = ?rhs) \<and> _")
  5418 proof-
  5419 
  5420   let ?I = "\<lambda> (x::int) p. Ifm (real_of_int x#bs) p"
  5421   let ?q = "fst (chooset p)"
  5422   let ?B = "fst (snd(chooset p))"
  5423   let ?d = "snd (snd (chooset p))"
  5424   let ?js = "[1..?d]"
  5425   let ?mq = "minusinf ?q"
  5426   let ?smq = "simpfm ?mq"
  5427   let ?md = "evaldjf (\<lambda> j. simpfm (subst0 (C j) ?smq)) ?js"
  5428   fix i
  5429   let ?N = "\<lambda> (t,k). (Inum (real_of_int (i::int)#bs) t,k)"
  5430   let ?qd = "evaldjf (stage ?q ?d) ?B"
  5431   have qbf:"chooset p = (?q,?B,?d)" by simp
  5432   from chooset[OF qf qbf] have pq_ex: "(\<exists>(x::int). ?I x p) = (\<exists> (x::int). ?I x ?q)" and
  5433     B:"?N ` set ?B = ?N ` set (\<rho> ?q)" and dd: "\<delta> ?q = ?d" and dp: "?d > 0" and
  5434     lq: "iszlfm ?q (real_of_int i#bs)" and
  5435     Bn: "\<forall> (e,c)\<in> set ?B. numbound0 e \<and> c > 0" by auto
  5436   from zlin_qfree[OF lq] have qfq: "qfree ?q" .
  5437   from simpfm_qf[OF minusinf_qfree[OF qfq]] have qfmq: "qfree ?smq".
  5438   have jsnb: "\<forall> j \<in> set ?js. numbound0 (C j)" by simp
  5439   hence "\<forall> j\<in> set ?js. bound0 (subst0 (C j) ?smq)"
  5440     by (auto simp only: subst0_bound0[OF qfmq])
  5441   hence th: "\<forall> j\<in> set ?js. bound0 (simpfm (subst0 (C j) ?smq))"
  5442     by auto
  5443   from evaldjf_bound0[OF th] have mdb: "bound0 ?md" by simp
  5444   from Bn stage_nb[OF lq] have th:"\<forall> x \<in> set ?B. bound0 (stage ?q ?d x)" by auto
  5445   from evaldjf_bound0[OF th]  have qdb: "bound0 ?qd" .
  5446   from mdb qdb
  5447   have mdqdb: "bound0 (disj ?md ?qd)" by (simp only: disj_def, cases "?md=T \<or> ?qd=T", simp_all)
  5448   from trans [OF pq_ex rl_thm'[OF lq B]] dd
  5449   have "?lhs = ((\<exists> j\<in> {1.. ?d}. ?I j ?mq) \<or> (\<exists> (e,c)\<in> set ?B. \<exists> j\<in> {1 .. c*?d}. Ifm (real_of_int i#bs) (\<sigma> ?q c (Add e (C j)))))" by auto
  5450   also have "\<dots> = ((\<exists> j\<in> {1.. ?d}. ?I j ?smq) \<or> (\<exists> (e,c)\<in> set ?B. ?I i (stage ?q ?d (e,c) )))"
  5451     by (simp add: stage split_def)
  5452   also have "\<dots> = ((\<exists> j\<in> {1 .. ?d}. ?I i (subst0 (C j) ?smq))  \<or> ?I i ?qd)"
  5453     by (simp add: evaldjf_ex subst0_I[OF qfmq])
  5454   finally have mdqd:"?lhs = (?I i ?md \<or> ?I i ?qd)" by (simp only: evaldjf_ex set_upto simpfm)
  5455   also have "\<dots> = (?I i (disj ?md ?qd))" by simp
  5456   also have "\<dots> = (Ifm bs (decr (disj ?md ?qd)))" by (simp only: decr [OF mdqdb])
  5457   finally have mdqd2: "?lhs = (Ifm bs (decr (disj ?md ?qd)))" .
  5458   {assume mdT: "?md = T"
  5459     hence cT:"redlove p = T" by (simp add: redlove_def Let_def chooset_def split_def)
  5460     from mdT have lhs:"?lhs" using mdqd by simp
  5461     from mdT have "?rhs" by (simp add: redlove_def chooset_def split_def)
  5462     with lhs cT have ?thesis by simp }
  5463   moreover
  5464   {assume mdT: "?md \<noteq> T" hence "redlove p = decr (disj ?md ?qd)"
  5465       by (simp add: redlove_def chooset_def split_def Let_def)
  5466     with mdqd2 decr_qf[OF mdqdb] have ?thesis by simp }
  5467   ultimately show ?thesis by blast
  5468 qed
  5469 
  5470 lemma DJredlove:
  5471   assumes qf: "qfree p"
  5472   shows "((\<exists> (x::int). Ifm (real_of_int x#bs) p) = (Ifm bs (DJ redlove p))) \<and> qfree (DJ redlove p)"
  5473 proof-
  5474   from redlove have cqf: "\<forall> p. qfree p \<longrightarrow> qfree (redlove p)" by  blast
  5475   from DJ_qf[OF cqf] qf have thqf:"qfree (DJ redlove p)" by blast
  5476   have "Ifm bs (DJ redlove p) = (\<exists> q\<in> set (disjuncts p). Ifm bs (redlove q))"
  5477      by (simp add: DJ_def evaldjf_ex)
  5478   also have "\<dots> = (\<exists> q \<in> set(disjuncts p). \<exists> (x::int). Ifm (real_of_int x#bs)  q)"
  5479     using redlove disjuncts_qf[OF qf] by blast
  5480   also have "\<dots> = (\<exists> (x::int). Ifm (real_of_int x#bs) p)" by (induct p rule: disjuncts.induct, auto)
  5481   finally show ?thesis using thqf by blast
  5482 qed
  5483 
  5484 
  5485 lemma exsplit_qf: assumes qf: "qfree p"
  5486   shows "qfree (exsplit p)"
  5487 using qf by (induct p rule: exsplit.induct, auto)
  5488 
  5489 definition mircfr :: "fm \<Rightarrow> fm" where
  5490   "mircfr = DJ cooper o ferrack01 o simpfm o exsplit"
  5491 
  5492 definition mirlfr :: "fm \<Rightarrow> fm" where
  5493   "mirlfr = DJ redlove o ferrack01 o simpfm o exsplit"
  5494 
  5495 lemma mircfr: "\<forall> bs p. qfree p \<longrightarrow> qfree (mircfr p) \<and> Ifm bs (mircfr p) = Ifm bs (E p)"
  5496 proof(clarsimp simp del: Ifm.simps)