src/HOL/Decision_Procs/Ferrack.thy
author hoelzl
Fri Mar 22 10:41:43 2013 +0100 (2013-03-22)
changeset 51474 1e9e68247ad1
parent 51272 9c8d63b4b6be
child 53168 d998de7f0efc
permissions -rw-r--r--
generalize Bfun and Bseq to metric spaces; Bseq is an abbreviation for Bfun
     1 (*  Title:      HOL/Decision_Procs/Ferrack.thy
     2     Author:     Amine Chaieb
     3 *)
     4 
     5 theory Ferrack
     6 imports Complex_Main Dense_Linear_Order DP_Library
     7   "~~/src/HOL/Library/Code_Target_Numeral" "~~/src/HOL/Library/Old_Recdef"
     8 begin
     9 
    10 section {* Quantifier elimination for @{text "\<real> (0, 1, +, <)"} *}
    11 
    12   (*********************************************************************************)
    13   (****                            SHADOW SYNTAX AND SEMANTICS                  ****)
    14   (*********************************************************************************)
    15 
    16 datatype num = C int | Bound nat | CN nat int num | Neg num | Add num num| Sub num num 
    17   | Mul int num 
    18 
    19   (* A size for num to make inductive proofs simpler*)
    20 primrec num_size :: "num \<Rightarrow> nat" where
    21   "num_size (C c) = 1"
    22 | "num_size (Bound n) = 1"
    23 | "num_size (Neg a) = 1 + num_size a"
    24 | "num_size (Add a b) = 1 + num_size a + num_size b"
    25 | "num_size (Sub a b) = 3 + num_size a + num_size b"
    26 | "num_size (Mul c a) = 1 + num_size a"
    27 | "num_size (CN n c a) = 3 + num_size a "
    28 
    29   (* Semantics of numeral terms (num) *)
    30 primrec Inum :: "real list \<Rightarrow> num \<Rightarrow> real" where
    31   "Inum bs (C c) = (real c)"
    32 | "Inum bs (Bound n) = bs!n"
    33 | "Inum bs (CN n c a) = (real c) * (bs!n) + (Inum bs a)"
    34 | "Inum bs (Neg a) = -(Inum bs a)"
    35 | "Inum bs (Add a b) = Inum bs a + Inum bs b"
    36 | "Inum bs (Sub a b) = Inum bs a - Inum bs b"
    37 | "Inum bs (Mul c a) = (real c) * Inum bs a"
    38     (* FORMULAE *)
    39 datatype fm  = 
    40   T| F| Lt num| Le num| Gt num| Ge num| Eq num| NEq num|
    41   NOT fm| And fm fm|  Or fm fm| Imp fm fm| Iff fm fm| E fm| A fm
    42 
    43 
    44   (* A size for fm *)
    45 fun fmsize :: "fm \<Rightarrow> nat" where
    46   "fmsize (NOT p) = 1 + fmsize p"
    47 | "fmsize (And p q) = 1 + fmsize p + fmsize q"
    48 | "fmsize (Or p q) = 1 + fmsize p + fmsize q"
    49 | "fmsize (Imp p q) = 3 + fmsize p + fmsize q"
    50 | "fmsize (Iff p q) = 3 + 2*(fmsize p + fmsize q)"
    51 | "fmsize (E p) = 1 + fmsize p"
    52 | "fmsize (A p) = 4+ fmsize p"
    53 | "fmsize p = 1"
    54   (* several lemmas about fmsize *)
    55 lemma fmsize_pos: "fmsize p > 0"
    56 by (induct p rule: fmsize.induct) simp_all
    57 
    58   (* Semantics of formulae (fm) *)
    59 primrec Ifm ::"real list \<Rightarrow> fm \<Rightarrow> bool" where
    60   "Ifm bs T = True"
    61 | "Ifm bs F = False"
    62 | "Ifm bs (Lt a) = (Inum bs a < 0)"
    63 | "Ifm bs (Gt a) = (Inum bs a > 0)"
    64 | "Ifm bs (Le a) = (Inum bs a \<le> 0)"
    65 | "Ifm bs (Ge a) = (Inum bs a \<ge> 0)"
    66 | "Ifm bs (Eq a) = (Inum bs a = 0)"
    67 | "Ifm bs (NEq a) = (Inum bs a \<noteq> 0)"
    68 | "Ifm bs (NOT p) = (\<not> (Ifm bs p))"
    69 | "Ifm bs (And p q) = (Ifm bs p \<and> Ifm bs q)"
    70 | "Ifm bs (Or p q) = (Ifm bs p \<or> Ifm bs q)"
    71 | "Ifm bs (Imp p q) = ((Ifm bs p) \<longrightarrow> (Ifm bs q))"
    72 | "Ifm bs (Iff p q) = (Ifm bs p = Ifm bs q)"
    73 | "Ifm bs (E p) = (\<exists> x. Ifm (x#bs) p)"
    74 | "Ifm bs (A p) = (\<forall> x. Ifm (x#bs) p)"
    75 
    76 lemma IfmLeSub: "\<lbrakk> Inum bs s = s' ; Inum bs t = t' \<rbrakk> \<Longrightarrow> Ifm bs (Le (Sub s t)) = (s' \<le> t')"
    77 apply simp
    78 done
    79 
    80 lemma IfmLtSub: "\<lbrakk> Inum bs s = s' ; Inum bs t = t' \<rbrakk> \<Longrightarrow> Ifm bs (Lt (Sub s t)) = (s' < t')"
    81 apply simp
    82 done
    83 lemma IfmEqSub: "\<lbrakk> Inum bs s = s' ; Inum bs t = t' \<rbrakk> \<Longrightarrow> Ifm bs (Eq (Sub s t)) = (s' = t')"
    84 apply simp
    85 done
    86 lemma IfmNOT: " (Ifm bs p = P) \<Longrightarrow> (Ifm bs (NOT p) = (\<not>P))"
    87 apply simp
    88 done
    89 lemma IfmAnd: " \<lbrakk> Ifm bs p = P ; Ifm bs q = Q\<rbrakk> \<Longrightarrow> (Ifm bs (And p q) = (P \<and> Q))"
    90 apply simp
    91 done
    92 lemma IfmOr: " \<lbrakk> Ifm bs p = P ; Ifm bs q = Q\<rbrakk> \<Longrightarrow> (Ifm bs (Or p q) = (P \<or> Q))"
    93 apply simp
    94 done
    95 lemma IfmImp: " \<lbrakk> Ifm bs p = P ; Ifm bs q = Q\<rbrakk> \<Longrightarrow> (Ifm bs (Imp p q) = (P \<longrightarrow> Q))"
    96 apply simp
    97 done
    98 lemma IfmIff: " \<lbrakk> Ifm bs p = P ; Ifm bs q = Q\<rbrakk> \<Longrightarrow> (Ifm bs (Iff p q) = (P = Q))"
    99 apply simp
   100 done
   101 
   102 lemma IfmE: " (!! x. Ifm (x#bs) p = P x) \<Longrightarrow> (Ifm bs (E p) = (\<exists>x. P x))"
   103 apply simp
   104 done
   105 lemma IfmA: " (!! x. Ifm (x#bs) p = P x) \<Longrightarrow> (Ifm bs (A p) = (\<forall>x. P x))"
   106 apply simp
   107 done
   108 
   109 fun not:: "fm \<Rightarrow> fm" where
   110   "not (NOT p) = p"
   111 | "not T = F"
   112 | "not F = T"
   113 | "not p = NOT p"
   114 lemma not[simp]: "Ifm bs (not p) = Ifm bs (NOT p)"
   115 by (cases p) auto
   116 
   117 definition conj :: "fm \<Rightarrow> fm \<Rightarrow> fm" where
   118   "conj p q = (if (p = F \<or> q=F) then F else if p=T then q else if q=T then p else 
   119    if p = q then p else And p q)"
   120 lemma conj[simp]: "Ifm bs (conj p q) = Ifm bs (And p q)"
   121 by (cases "p=F \<or> q=F",simp_all add: conj_def) (cases p,simp_all)
   122 
   123 definition disj :: "fm \<Rightarrow> fm \<Rightarrow> fm" where
   124   "disj p q = (if (p = T \<or> q=T) then T else if p=F then q else if q=F then p 
   125        else if p=q then p else Or p q)"
   126 
   127 lemma disj[simp]: "Ifm bs (disj p q) = Ifm bs (Or p q)"
   128 by (cases "p=T \<or> q=T",simp_all add: disj_def) (cases p,simp_all)
   129 
   130 definition imp :: "fm \<Rightarrow> fm \<Rightarrow> fm" where
   131   "imp p q = (if (p = F \<or> q=T \<or> p=q) then T else if p=T then q else if q=F then not p 
   132     else Imp p q)"
   133 lemma imp[simp]: "Ifm bs (imp p q) = Ifm bs (Imp p q)"
   134 by (cases "p=F \<or> q=T",simp_all add: imp_def) 
   135 
   136 definition iff :: "fm \<Rightarrow> fm \<Rightarrow> fm" where
   137   "iff p q = (if (p = q) then T else if (p = NOT q \<or> NOT p = q) then F else 
   138        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 
   139   Iff p q)"
   140 lemma iff[simp]: "Ifm bs (iff p q) = Ifm bs (Iff p q)"
   141   by (unfold iff_def,cases "p=q", simp,cases "p=NOT q", simp) (cases "NOT p= q", auto)
   142 
   143 lemma conj_simps:
   144   "conj F Q = F"
   145   "conj P F = F"
   146   "conj T Q = Q"
   147   "conj P T = P"
   148   "conj P P = P"
   149   "P \<noteq> T \<Longrightarrow> P \<noteq> F \<Longrightarrow> Q \<noteq> T \<Longrightarrow> Q \<noteq> F \<Longrightarrow> P \<noteq> Q \<Longrightarrow> conj P Q = And P Q"
   150   by (simp_all add: conj_def)
   151 
   152 lemma disj_simps:
   153   "disj T Q = T"
   154   "disj P T = T"
   155   "disj F Q = Q"
   156   "disj P F = P"
   157   "disj P P = P"
   158   "P \<noteq> T \<Longrightarrow> P \<noteq> F \<Longrightarrow> Q \<noteq> T \<Longrightarrow> Q \<noteq> F \<Longrightarrow> P \<noteq> Q \<Longrightarrow> disj P Q = Or P Q"
   159   by (simp_all add: disj_def)
   160 lemma imp_simps:
   161   "imp F Q = T"
   162   "imp P T = T"
   163   "imp T Q = Q"
   164   "imp P F = not P"
   165   "imp P P = T"
   166   "P \<noteq> T \<Longrightarrow> P \<noteq> F \<Longrightarrow> P \<noteq> Q \<Longrightarrow> Q \<noteq> T \<Longrightarrow> Q \<noteq> F \<Longrightarrow> imp P Q = Imp P Q"
   167   by (simp_all add: imp_def)
   168 lemma trivNOT: "p \<noteq> NOT p" "NOT p \<noteq> p"
   169 apply (induct p, auto)
   170 done
   171 
   172 lemma iff_simps:
   173   "iff p p = T"
   174   "iff p (NOT p) = F"
   175   "iff (NOT p) p = F"
   176   "iff p F = not p"
   177   "iff F p = not p"
   178   "p \<noteq> NOT T \<Longrightarrow> iff T p = p"
   179   "p\<noteq> NOT T \<Longrightarrow> iff p T = p"
   180   "p\<noteq>q \<Longrightarrow> p\<noteq> NOT q \<Longrightarrow> q\<noteq> NOT p \<Longrightarrow> p\<noteq> F \<Longrightarrow> q\<noteq> F \<Longrightarrow> p \<noteq> T \<Longrightarrow> q \<noteq> T \<Longrightarrow> iff p q = Iff p q"
   181   using trivNOT
   182   by (simp_all add: iff_def, cases p, auto)
   183   (* Quantifier freeness *)
   184 fun qfree:: "fm \<Rightarrow> bool" where
   185   "qfree (E p) = False"
   186 | "qfree (A p) = False"
   187 | "qfree (NOT p) = qfree p" 
   188 | "qfree (And p q) = (qfree p \<and> qfree q)" 
   189 | "qfree (Or  p q) = (qfree p \<and> qfree q)" 
   190 | "qfree (Imp p q) = (qfree p \<and> qfree q)" 
   191 | "qfree (Iff p q) = (qfree p \<and> qfree q)"
   192 | "qfree p = True"
   193 
   194   (* Boundedness and substitution *)
   195 primrec numbound0:: "num \<Rightarrow> bool" (* a num is INDEPENDENT of Bound 0 *) where
   196   "numbound0 (C c) = True"
   197 | "numbound0 (Bound n) = (n>0)"
   198 | "numbound0 (CN n c a) = (n\<noteq>0 \<and> numbound0 a)"
   199 | "numbound0 (Neg a) = numbound0 a"
   200 | "numbound0 (Add a b) = (numbound0 a \<and> numbound0 b)"
   201 | "numbound0 (Sub a b) = (numbound0 a \<and> numbound0 b)" 
   202 | "numbound0 (Mul i a) = numbound0 a"
   203 
   204 lemma numbound0_I:
   205   assumes nb: "numbound0 a"
   206   shows "Inum (b#bs) a = Inum (b'#bs) a"
   207 using nb
   208 by (induct a) simp_all
   209 
   210 primrec bound0:: "fm \<Rightarrow> bool" (* A Formula is independent of Bound 0 *) where
   211   "bound0 T = True"
   212 | "bound0 F = True"
   213 | "bound0 (Lt a) = numbound0 a"
   214 | "bound0 (Le a) = numbound0 a"
   215 | "bound0 (Gt a) = numbound0 a"
   216 | "bound0 (Ge a) = numbound0 a"
   217 | "bound0 (Eq a) = numbound0 a"
   218 | "bound0 (NEq a) = numbound0 a"
   219 | "bound0 (NOT p) = bound0 p"
   220 | "bound0 (And p q) = (bound0 p \<and> bound0 q)"
   221 | "bound0 (Or p q) = (bound0 p \<and> bound0 q)"
   222 | "bound0 (Imp p q) = ((bound0 p) \<and> (bound0 q))"
   223 | "bound0 (Iff p q) = (bound0 p \<and> bound0 q)"
   224 | "bound0 (E p) = False"
   225 | "bound0 (A p) = False"
   226 
   227 lemma bound0_I:
   228   assumes bp: "bound0 p"
   229   shows "Ifm (b#bs) p = Ifm (b'#bs) p"
   230 using bp numbound0_I[where b="b" and bs="bs" and b'="b'"]
   231 by (induct p) auto
   232 
   233 lemma not_qf[simp]: "qfree p \<Longrightarrow> qfree (not p)"
   234 by (cases p, auto)
   235 lemma not_bn[simp]: "bound0 p \<Longrightarrow> bound0 (not p)"
   236 by (cases p, auto)
   237 
   238 
   239 lemma conj_qf[simp]: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (conj p q)"
   240 using conj_def by auto 
   241 lemma conj_nb[simp]: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (conj p q)"
   242 using conj_def by auto 
   243 
   244 lemma disj_qf[simp]: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (disj p q)"
   245 using disj_def by auto 
   246 lemma disj_nb[simp]: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (disj p q)"
   247 using disj_def by auto 
   248 
   249 lemma imp_qf[simp]: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (imp p q)"
   250 using imp_def by (cases "p=F \<or> q=T",simp_all add: imp_def)
   251 lemma imp_nb[simp]: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (imp p q)"
   252 using imp_def by (cases "p=F \<or> q=T \<or> p=q",simp_all add: imp_def)
   253 
   254 lemma iff_qf[simp]: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (iff p q)"
   255   by (unfold iff_def,cases "p=q", auto)
   256 lemma iff_nb[simp]: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (iff p q)"
   257 using iff_def by (unfold iff_def,cases "p=q", auto)
   258 
   259 fun decrnum:: "num \<Rightarrow> num"  where
   260   "decrnum (Bound n) = Bound (n - 1)"
   261 | "decrnum (Neg a) = Neg (decrnum a)"
   262 | "decrnum (Add a b) = Add (decrnum a) (decrnum b)"
   263 | "decrnum (Sub a b) = Sub (decrnum a) (decrnum b)"
   264 | "decrnum (Mul c a) = Mul c (decrnum a)"
   265 | "decrnum (CN n c a) = CN (n - 1) c (decrnum a)"
   266 | "decrnum a = a"
   267 
   268 fun decr :: "fm \<Rightarrow> fm" where
   269   "decr (Lt a) = Lt (decrnum a)"
   270 | "decr (Le a) = Le (decrnum a)"
   271 | "decr (Gt a) = Gt (decrnum a)"
   272 | "decr (Ge a) = Ge (decrnum a)"
   273 | "decr (Eq a) = Eq (decrnum a)"
   274 | "decr (NEq a) = NEq (decrnum a)"
   275 | "decr (NOT p) = NOT (decr p)" 
   276 | "decr (And p q) = conj (decr p) (decr q)"
   277 | "decr (Or p q) = disj (decr p) (decr q)"
   278 | "decr (Imp p q) = imp (decr p) (decr q)"
   279 | "decr (Iff p q) = iff (decr p) (decr q)"
   280 | "decr p = p"
   281 
   282 lemma decrnum: assumes nb: "numbound0 t"
   283   shows "Inum (x#bs) t = Inum bs (decrnum t)"
   284   using nb by (induct t rule: decrnum.induct, simp_all)
   285 
   286 lemma decr: assumes nb: "bound0 p"
   287   shows "Ifm (x#bs) p = Ifm bs (decr p)"
   288   using nb 
   289   by (induct p rule: decr.induct, simp_all add: decrnum)
   290 
   291 lemma decr_qf: "bound0 p \<Longrightarrow> qfree (decr p)"
   292 by (induct p, simp_all)
   293 
   294 fun isatom :: "fm \<Rightarrow> bool" (* test for atomicity *) where
   295   "isatom T = True"
   296 | "isatom F = True"
   297 | "isatom (Lt a) = True"
   298 | "isatom (Le a) = True"
   299 | "isatom (Gt a) = True"
   300 | "isatom (Ge a) = True"
   301 | "isatom (Eq a) = True"
   302 | "isatom (NEq a) = True"
   303 | "isatom p = False"
   304 
   305 lemma bound0_qf: "bound0 p \<Longrightarrow> qfree p"
   306 by (induct p, simp_all)
   307 
   308 definition djf :: "('a \<Rightarrow> fm) \<Rightarrow> 'a \<Rightarrow> fm \<Rightarrow> fm" where
   309   "djf f p q = (if q=T then T else if q=F then f p else 
   310   (let fp = f p in case fp of T \<Rightarrow> T | F \<Rightarrow> q | _ \<Rightarrow> Or (f p) q))"
   311 definition evaldjf :: "('a \<Rightarrow> fm) \<Rightarrow> 'a list \<Rightarrow> fm" where
   312   "evaldjf f ps = foldr (djf f) ps F"
   313 
   314 lemma djf_Or: "Ifm bs (djf f p q) = Ifm bs (Or (f p) q)"
   315 by (cases "q=T", simp add: djf_def,cases "q=F",simp add: djf_def) 
   316 (cases "f p", simp_all add: Let_def djf_def) 
   317 
   318 
   319 lemma djf_simps:
   320   "djf f p T = T"
   321   "djf f p F = f p"
   322   "q\<noteq>T \<Longrightarrow> q\<noteq>F \<Longrightarrow> djf f p q = (let fp = f p in case fp of T \<Rightarrow> T | F \<Rightarrow> q | _ \<Rightarrow> Or (f p) q)"
   323   by (simp_all add: djf_def)
   324 
   325 lemma evaldjf_ex: "Ifm bs (evaldjf f ps) = (\<exists> p \<in> set ps. Ifm bs (f p))"
   326   by(induct ps, simp_all add: evaldjf_def djf_Or)
   327 
   328 lemma evaldjf_bound0: 
   329   assumes nb: "\<forall> x\<in> set xs. bound0 (f x)"
   330   shows "bound0 (evaldjf f xs)"
   331   using nb by (induct xs, auto simp add: evaldjf_def djf_def Let_def) (case_tac "f a", auto) 
   332 
   333 lemma evaldjf_qf: 
   334   assumes nb: "\<forall> x\<in> set xs. qfree (f x)"
   335   shows "qfree (evaldjf f xs)"
   336   using nb by (induct xs, auto simp add: evaldjf_def djf_def Let_def) (case_tac "f a", auto) 
   337 
   338 fun disjuncts :: "fm \<Rightarrow> fm list" where
   339   "disjuncts (Or p q) = disjuncts p @ disjuncts q"
   340 | "disjuncts F = []"
   341 | "disjuncts p = [p]"
   342 
   343 lemma disjuncts: "(\<exists> q\<in> set (disjuncts p). Ifm bs q) = Ifm bs p"
   344 by(induct p rule: disjuncts.induct, auto)
   345 
   346 lemma disjuncts_nb: "bound0 p \<Longrightarrow> \<forall> q\<in> set (disjuncts p). bound0 q"
   347 proof-
   348   assume nb: "bound0 p"
   349   hence "list_all bound0 (disjuncts p)" by (induct p rule:disjuncts.induct,auto)
   350   thus ?thesis by (simp only: list_all_iff)
   351 qed
   352 
   353 lemma disjuncts_qf: "qfree p \<Longrightarrow> \<forall> q\<in> set (disjuncts p). qfree q"
   354 proof-
   355   assume qf: "qfree p"
   356   hence "list_all qfree (disjuncts p)"
   357     by (induct p rule: disjuncts.induct, auto)
   358   thus ?thesis by (simp only: list_all_iff)
   359 qed
   360 
   361 definition DJ :: "(fm \<Rightarrow> fm) \<Rightarrow> fm \<Rightarrow> fm" where
   362   "DJ f p = evaldjf f (disjuncts p)"
   363 
   364 lemma DJ: assumes fdj: "\<forall> p q. Ifm bs (f (Or p q)) = Ifm bs (Or (f p) (f q))"
   365   and fF: "f F = F"
   366   shows "Ifm bs (DJ f p) = Ifm bs (f p)"
   367 proof-
   368   have "Ifm bs (DJ f p) = (\<exists> q \<in> set (disjuncts p). Ifm bs (f q))"
   369     by (simp add: DJ_def evaldjf_ex) 
   370   also have "\<dots> = Ifm bs (f p)" using fdj fF by (induct p rule: disjuncts.induct, auto)
   371   finally show ?thesis .
   372 qed
   373 
   374 lemma DJ_qf: assumes 
   375   fqf: "\<forall> p. qfree p \<longrightarrow> qfree (f p)"
   376   shows "\<forall>p. qfree p \<longrightarrow> qfree (DJ f p) "
   377 proof(clarify)
   378   fix  p assume qf: "qfree p"
   379   have th: "DJ f p = evaldjf f (disjuncts p)" by (simp add: DJ_def)
   380   from disjuncts_qf[OF qf] have "\<forall> q\<in> set (disjuncts p). qfree q" .
   381   with fqf have th':"\<forall> q\<in> set (disjuncts p). qfree (f q)" by blast
   382   
   383   from evaldjf_qf[OF th'] th show "qfree (DJ f p)" by simp
   384 qed
   385 
   386 lemma DJ_qe: assumes qe: "\<forall> bs p. qfree p \<longrightarrow> qfree (qe p) \<and> (Ifm bs (qe p) = Ifm bs (E p))"
   387   shows "\<forall> bs p. qfree p \<longrightarrow> qfree (DJ qe p) \<and> (Ifm bs ((DJ qe p)) = Ifm bs (E p))"
   388 proof(clarify)
   389   fix p::fm and bs
   390   assume qf: "qfree p"
   391   from qe have qth: "\<forall> p. qfree p \<longrightarrow> qfree (qe p)" by blast
   392   from DJ_qf[OF qth] qf have qfth:"qfree (DJ qe p)" by auto
   393   have "Ifm bs (DJ qe p) = (\<exists> q\<in> set (disjuncts p). Ifm bs (qe q))"
   394     by (simp add: DJ_def evaldjf_ex)
   395   also have "\<dots> = (\<exists> q \<in> set(disjuncts p). Ifm bs (E q))" using qe disjuncts_qf[OF qf] by auto
   396   also have "\<dots> = Ifm bs (E p)" by (induct p rule: disjuncts.induct, auto)
   397   finally show "qfree (DJ qe p) \<and> Ifm bs (DJ qe p) = Ifm bs (E p)" using qfth by blast
   398 qed
   399   (* Simplification *)
   400 
   401 fun maxcoeff:: "num \<Rightarrow> int" where
   402   "maxcoeff (C i) = abs i"
   403 | "maxcoeff (CN n c t) = max (abs c) (maxcoeff t)"
   404 | "maxcoeff t = 1"
   405 
   406 lemma maxcoeff_pos: "maxcoeff t \<ge> 0"
   407   by (induct t rule: maxcoeff.induct, auto)
   408 
   409 fun numgcdh:: "num \<Rightarrow> int \<Rightarrow> int" where
   410   "numgcdh (C i) = (\<lambda>g. gcd i g)"
   411 | "numgcdh (CN n c t) = (\<lambda>g. gcd c (numgcdh t g))"
   412 | "numgcdh t = (\<lambda>g. 1)"
   413 
   414 definition numgcd :: "num \<Rightarrow> int" where
   415   "numgcd t = numgcdh t (maxcoeff t)"
   416 
   417 fun reducecoeffh:: "num \<Rightarrow> int \<Rightarrow> num" where
   418   "reducecoeffh (C i) = (\<lambda> g. C (i div g))"
   419 | "reducecoeffh (CN n c t) = (\<lambda> g. CN n (c div g) (reducecoeffh t g))"
   420 | "reducecoeffh t = (\<lambda>g. t)"
   421 
   422 definition reducecoeff :: "num \<Rightarrow> num" where
   423   "reducecoeff t =
   424   (let g = numgcd t in 
   425   if g = 0 then C 0 else if g=1 then t else reducecoeffh t g)"
   426 
   427 fun dvdnumcoeff:: "num \<Rightarrow> int \<Rightarrow> bool" where
   428   "dvdnumcoeff (C i) = (\<lambda> g. g dvd i)"
   429 | "dvdnumcoeff (CN n c t) = (\<lambda> g. g dvd c \<and> (dvdnumcoeff t g))"
   430 | "dvdnumcoeff t = (\<lambda>g. False)"
   431 
   432 lemma dvdnumcoeff_trans: 
   433   assumes gdg: "g dvd g'" and dgt':"dvdnumcoeff t g'"
   434   shows "dvdnumcoeff t g"
   435   using dgt' gdg 
   436   by (induct t rule: dvdnumcoeff.induct, simp_all add: gdg dvd_trans[OF gdg])
   437 
   438 declare dvd_trans [trans add]
   439 
   440 lemma natabs0: "(nat (abs x) = 0) = (x = 0)"
   441 by arith
   442 
   443 lemma numgcd0:
   444   assumes g0: "numgcd t = 0"
   445   shows "Inum bs t = 0"
   446   using g0[simplified numgcd_def] 
   447   by (induct t rule: numgcdh.induct, auto simp add: natabs0 maxcoeff_pos min_max.sup_absorb2)
   448 
   449 lemma numgcdh_pos: assumes gp: "g \<ge> 0" shows "numgcdh t g \<ge> 0"
   450   using gp
   451   by (induct t rule: numgcdh.induct, auto)
   452 
   453 lemma numgcd_pos: "numgcd t \<ge>0"
   454   by (simp add: numgcd_def numgcdh_pos maxcoeff_pos)
   455 
   456 lemma reducecoeffh:
   457   assumes gt: "dvdnumcoeff t g" and gp: "g > 0" 
   458   shows "real g *(Inum bs (reducecoeffh t g)) = Inum bs t"
   459   using gt
   460 proof (induct t rule: reducecoeffh.induct) 
   461   case (1 i)
   462   hence gd: "g dvd i" by simp
   463   with assms show ?case by (simp add: real_of_int_div[OF gd])
   464 next
   465   case (2 n c t)
   466   hence gd: "g dvd c" by simp
   467   from assms 2 show ?case by (simp add: real_of_int_div[OF gd] algebra_simps)
   468 qed (auto simp add: numgcd_def gp)
   469 
   470 fun ismaxcoeff:: "num \<Rightarrow> int \<Rightarrow> bool" where
   471   "ismaxcoeff (C i) = (\<lambda> x. abs i \<le> x)"
   472 | "ismaxcoeff (CN n c t) = (\<lambda>x. abs c \<le> x \<and> (ismaxcoeff t x))"
   473 | "ismaxcoeff t = (\<lambda>x. True)"
   474 
   475 lemma ismaxcoeff_mono: "ismaxcoeff t c \<Longrightarrow> c \<le> c' \<Longrightarrow> ismaxcoeff t c'"
   476   by (induct t rule: ismaxcoeff.induct) auto
   477 
   478 lemma maxcoeff_ismaxcoeff: "ismaxcoeff t (maxcoeff t)"
   479 proof (induct t rule: maxcoeff.induct)
   480   case (2 n c t)
   481   hence H:"ismaxcoeff t (maxcoeff t)" .
   482   have thh: "maxcoeff t \<le> max (abs c) (maxcoeff t)" by simp
   483   from ismaxcoeff_mono[OF H thh] show ?case by simp
   484 qed simp_all
   485 
   486 lemma zgcd_gt1: "gcd i j > (1::int) \<Longrightarrow> ((abs i > 1 \<and> abs j > 1) \<or> (abs i = 0 \<and> abs j > 1) \<or> (abs i > 1 \<and> abs j = 0))"
   487   apply (cases "abs i = 0", simp_all add: gcd_int_def)
   488   apply (cases "abs j = 0", simp_all)
   489   apply (cases "abs i = 1", simp_all)
   490   apply (cases "abs j = 1", simp_all)
   491   apply auto
   492   done
   493 lemma numgcdh0:"numgcdh t m = 0 \<Longrightarrow>  m =0"
   494   by (induct t rule: numgcdh.induct, auto)
   495 
   496 lemma dvdnumcoeff_aux:
   497   assumes "ismaxcoeff t m" and mp:"m \<ge> 0" and "numgcdh t m > 1"
   498   shows "dvdnumcoeff t (numgcdh t m)"
   499 using assms
   500 proof(induct t rule: numgcdh.induct)
   501   case (2 n c t) 
   502   let ?g = "numgcdh t m"
   503   from 2 have th:"gcd c ?g > 1" by simp
   504   from zgcd_gt1[OF th] numgcdh_pos[OF mp, where t="t"]
   505   have "(abs c > 1 \<and> ?g > 1) \<or> (abs c = 0 \<and> ?g > 1) \<or> (abs c > 1 \<and> ?g = 0)" by simp
   506   moreover {assume "abs c > 1" and gp: "?g > 1" with 2
   507     have th: "dvdnumcoeff t ?g" by simp
   508     have th': "gcd c ?g dvd ?g" by simp
   509     from dvdnumcoeff_trans[OF th' th] have ?case by simp }
   510   moreover {assume "abs c = 0 \<and> ?g > 1"
   511     with 2 have th: "dvdnumcoeff t ?g" by simp
   512     have th': "gcd c ?g dvd ?g" by simp
   513     from dvdnumcoeff_trans[OF th' th] have ?case by simp
   514     hence ?case by simp }
   515   moreover {assume "abs c > 1" and g0:"?g = 0" 
   516     from numgcdh0[OF g0] have "m=0". with 2 g0 have ?case by simp }
   517   ultimately show ?case by blast
   518 qed auto
   519 
   520 lemma dvdnumcoeff_aux2:
   521   assumes "numgcd t > 1"
   522   shows "dvdnumcoeff t (numgcd t) \<and> numgcd t > 0"
   523   using assms
   524 proof (simp add: numgcd_def)
   525   let ?mc = "maxcoeff t"
   526   let ?g = "numgcdh t ?mc"
   527   have th1: "ismaxcoeff t ?mc" by (rule maxcoeff_ismaxcoeff)
   528   have th2: "?mc \<ge> 0" by (rule maxcoeff_pos)
   529   assume H: "numgcdh t ?mc > 1"
   530   from dvdnumcoeff_aux[OF th1 th2 H]  show "dvdnumcoeff t ?g" .
   531 qed
   532 
   533 lemma reducecoeff: "real (numgcd t) * (Inum bs (reducecoeff t)) = Inum bs t"
   534 proof-
   535   let ?g = "numgcd t"
   536   have "?g \<ge> 0"  by (simp add: numgcd_pos)
   537   hence "?g = 0 \<or> ?g = 1 \<or> ?g > 1" by auto
   538   moreover {assume "?g = 0" hence ?thesis by (simp add: numgcd0)} 
   539   moreover {assume "?g = 1" hence ?thesis by (simp add: reducecoeff_def)} 
   540   moreover { assume g1:"?g > 1"
   541     from dvdnumcoeff_aux2[OF g1] have th1:"dvdnumcoeff t ?g" and g0: "?g > 0" by blast+
   542     from reducecoeffh[OF th1 g0, where bs="bs"] g1 have ?thesis 
   543       by (simp add: reducecoeff_def Let_def)} 
   544   ultimately show ?thesis by blast
   545 qed
   546 
   547 lemma reducecoeffh_numbound0: "numbound0 t \<Longrightarrow> numbound0 (reducecoeffh t g)"
   548 by (induct t rule: reducecoeffh.induct, auto)
   549 
   550 lemma reducecoeff_numbound0: "numbound0 t \<Longrightarrow> numbound0 (reducecoeff t)"
   551 using reducecoeffh_numbound0 by (simp add: reducecoeff_def Let_def)
   552 
   553 consts
   554   numadd:: "num \<times> num \<Rightarrow> num"
   555 
   556 recdef numadd "measure (\<lambda> (t,s). size t + size s)"
   557   "numadd (CN n1 c1 r1,CN n2 c2 r2) =
   558   (if n1=n2 then 
   559   (let c = c1 + c2
   560   in (if c=0 then numadd(r1,r2) else CN n1 c (numadd (r1,r2))))
   561   else if n1 \<le> n2 then (CN n1 c1 (numadd (r1,CN n2 c2 r2))) 
   562   else (CN n2 c2 (numadd (CN n1 c1 r1,r2))))"
   563   "numadd (CN n1 c1 r1,t) = CN n1 c1 (numadd (r1, t))"  
   564   "numadd (t,CN n2 c2 r2) = CN n2 c2 (numadd (t,r2))" 
   565   "numadd (C b1, C b2) = C (b1+b2)"
   566   "numadd (a,b) = Add a b"
   567 
   568 lemma numadd[simp]: "Inum bs (numadd (t,s)) = Inum bs (Add t s)"
   569 apply (induct t s rule: numadd.induct, simp_all add: Let_def)
   570 apply (case_tac "c1+c2 = 0",case_tac "n1 \<le> n2", simp_all)
   571 apply (case_tac "n1 = n2", simp_all add: algebra_simps)
   572 by (simp only: distrib_right[symmetric],simp)
   573 
   574 lemma numadd_nb[simp]: "\<lbrakk> numbound0 t ; numbound0 s\<rbrakk> \<Longrightarrow> numbound0 (numadd (t,s))"
   575 by (induct t s rule: numadd.induct, auto simp add: Let_def)
   576 
   577 fun nummul:: "num \<Rightarrow> int \<Rightarrow> num" where
   578   "nummul (C j) = (\<lambda> i. C (i*j))"
   579 | "nummul (CN n c a) = (\<lambda> i. CN n (i*c) (nummul a i))"
   580 | "nummul t = (\<lambda> i. Mul i t)"
   581 
   582 lemma nummul[simp]: "\<And> i. Inum bs (nummul t i) = Inum bs (Mul i t)"
   583 by (induct t rule: nummul.induct, auto simp add: algebra_simps)
   584 
   585 lemma nummul_nb[simp]: "\<And> i. numbound0 t \<Longrightarrow> numbound0 (nummul t i)"
   586 by (induct t rule: nummul.induct, auto )
   587 
   588 definition numneg :: "num \<Rightarrow> num" where
   589   "numneg t = nummul t (- 1)"
   590 
   591 definition numsub :: "num \<Rightarrow> num \<Rightarrow> num" where
   592   "numsub s t = (if s = t then C 0 else numadd (s,numneg t))"
   593 
   594 lemma numneg[simp]: "Inum bs (numneg t) = Inum bs (Neg t)"
   595 using numneg_def by simp
   596 
   597 lemma numneg_nb[simp]: "numbound0 t \<Longrightarrow> numbound0 (numneg t)"
   598 using numneg_def by simp
   599 
   600 lemma numsub[simp]: "Inum bs (numsub a b) = Inum bs (Sub a b)"
   601 using numsub_def by simp
   602 
   603 lemma numsub_nb[simp]: "\<lbrakk> numbound0 t ; numbound0 s\<rbrakk> \<Longrightarrow> numbound0 (numsub t s)"
   604 using numsub_def by simp
   605 
   606 primrec simpnum:: "num \<Rightarrow> num" where
   607   "simpnum (C j) = C j"
   608 | "simpnum (Bound n) = CN n 1 (C 0)"
   609 | "simpnum (Neg t) = numneg (simpnum t)"
   610 | "simpnum (Add t s) = numadd (simpnum t,simpnum s)"
   611 | "simpnum (Sub t s) = numsub (simpnum t) (simpnum s)"
   612 | "simpnum (Mul i t) = (if i = 0 then (C 0) else nummul (simpnum t) i)"
   613 | "simpnum (CN n c t) = (if c = 0 then simpnum t else numadd (CN n c (C 0),simpnum t))"
   614 
   615 lemma simpnum_ci[simp]: "Inum bs (simpnum t) = Inum bs t"
   616 by (induct t) simp_all
   617 
   618 lemma simpnum_numbound0[simp]: 
   619   "numbound0 t \<Longrightarrow> numbound0 (simpnum t)"
   620 by (induct t) simp_all
   621 
   622 fun nozerocoeff:: "num \<Rightarrow> bool" where
   623   "nozerocoeff (C c) = True"
   624 | "nozerocoeff (CN n c t) = (c\<noteq>0 \<and> nozerocoeff t)"
   625 | "nozerocoeff t = True"
   626 
   627 lemma numadd_nz : "nozerocoeff a \<Longrightarrow> nozerocoeff b \<Longrightarrow> nozerocoeff (numadd (a,b))"
   628 by (induct a b rule: numadd.induct,auto simp add: Let_def)
   629 
   630 lemma nummul_nz : "\<And> i. i\<noteq>0 \<Longrightarrow> nozerocoeff a \<Longrightarrow> nozerocoeff (nummul a i)"
   631 by (induct a rule: nummul.induct,auto simp add: Let_def numadd_nz)
   632 
   633 lemma numneg_nz : "nozerocoeff a \<Longrightarrow> nozerocoeff (numneg a)"
   634 by (simp add: numneg_def nummul_nz)
   635 
   636 lemma numsub_nz: "nozerocoeff a \<Longrightarrow> nozerocoeff b \<Longrightarrow> nozerocoeff (numsub a b)"
   637 by (simp add: numsub_def numneg_nz numadd_nz)
   638 
   639 lemma simpnum_nz: "nozerocoeff (simpnum t)"
   640 by(induct t) (simp_all add: numadd_nz numneg_nz numsub_nz nummul_nz)
   641 
   642 lemma maxcoeff_nz: "nozerocoeff t \<Longrightarrow> maxcoeff t = 0 \<Longrightarrow> t = C 0"
   643 proof (induct t rule: maxcoeff.induct)
   644   case (2 n c t)
   645   hence cnz: "c \<noteq>0" and mx: "max (abs c) (maxcoeff t) = 0" by simp_all
   646   have "max (abs c) (maxcoeff t) \<ge> abs c" by simp
   647   with cnz have "max (abs c) (maxcoeff t) > 0" by arith
   648   with 2 show ?case by simp
   649 qed auto
   650 
   651 lemma numgcd_nz: assumes nz: "nozerocoeff t" and g0: "numgcd t = 0" shows "t = C 0"
   652 proof-
   653   from g0 have th:"numgcdh t (maxcoeff t) = 0" by (simp add: numgcd_def)
   654   from numgcdh0[OF th]  have th:"maxcoeff t = 0" .
   655   from maxcoeff_nz[OF nz th] show ?thesis .
   656 qed
   657 
   658 definition simp_num_pair :: "(num \<times> int) \<Rightarrow> num \<times> int" where
   659   "simp_num_pair = (\<lambda> (t,n). (if n = 0 then (C 0, 0) else
   660    (let t' = simpnum t ; g = numgcd t' in 
   661       if g > 1 then (let g' = gcd n g in 
   662         if g' = 1 then (t',n) 
   663         else (reducecoeffh t' g', n div g')) 
   664       else (t',n))))"
   665 
   666 lemma simp_num_pair_ci:
   667   shows "((\<lambda> (t,n). Inum bs t / real n) (simp_num_pair (t,n))) = ((\<lambda> (t,n). Inum bs t / real n) (t,n))"
   668   (is "?lhs = ?rhs")
   669 proof-
   670   let ?t' = "simpnum t"
   671   let ?g = "numgcd ?t'"
   672   let ?g' = "gcd n ?g"
   673   {assume nz: "n = 0" hence ?thesis by (simp add: Let_def simp_num_pair_def)}
   674   moreover
   675   { assume nnz: "n \<noteq> 0"
   676     {assume "\<not> ?g > 1" hence ?thesis by (simp add: Let_def simp_num_pair_def) }
   677     moreover
   678     {assume g1:"?g>1" hence g0: "?g > 0" by simp
   679       from g1 nnz have gp0: "?g' \<noteq> 0" by simp
   680       hence g'p: "?g' > 0" using gcd_ge_0_int[where x="n" and y="numgcd ?t'"] by arith 
   681       hence "?g'= 1 \<or> ?g' > 1" by arith
   682       moreover {assume "?g'=1" hence ?thesis by (simp add: Let_def simp_num_pair_def)}
   683       moreover {assume g'1:"?g'>1"
   684         from dvdnumcoeff_aux2[OF g1] have th1:"dvdnumcoeff ?t' ?g" ..
   685         let ?tt = "reducecoeffh ?t' ?g'"
   686         let ?t = "Inum bs ?tt"
   687         have gpdg: "?g' dvd ?g" by simp
   688         have gpdd: "?g' dvd n" by simp 
   689         have gpdgp: "?g' dvd ?g'" by simp
   690         from reducecoeffh[OF dvdnumcoeff_trans[OF gpdg th1] g'p] 
   691         have th2:"real ?g' * ?t = Inum bs ?t'" by simp
   692         from g1 g'1 have "?lhs = ?t / real (n div ?g')" by (simp add: simp_num_pair_def Let_def)
   693         also have "\<dots> = (real ?g' * ?t) / (real ?g' * (real (n div ?g')))" by simp
   694         also have "\<dots> = (Inum bs ?t' / real n)"
   695           using real_of_int_div[OF gpdd] th2 gp0 by simp
   696         finally have "?lhs = Inum bs t / real n" by simp
   697         then have ?thesis by (simp add: simp_num_pair_def) }
   698       ultimately have ?thesis by blast }
   699     ultimately have ?thesis by blast }
   700   ultimately show ?thesis by blast
   701 qed
   702 
   703 lemma simp_num_pair_l: assumes tnb: "numbound0 t" and np: "n >0" and tn: "simp_num_pair (t,n) = (t',n')"
   704   shows "numbound0 t' \<and> n' >0"
   705 proof-
   706   let ?t' = "simpnum t"
   707   let ?g = "numgcd ?t'"
   708   let ?g' = "gcd n ?g"
   709   { assume nz: "n = 0" hence ?thesis using assms by (simp add: Let_def simp_num_pair_def) }
   710   moreover
   711   { assume nnz: "n \<noteq> 0"
   712     { assume "\<not> ?g > 1" hence ?thesis using assms
   713         by (auto simp add: Let_def simp_num_pair_def simpnum_numbound0) }
   714     moreover
   715     { assume g1:"?g>1" hence g0: "?g > 0" by simp
   716       from g1 nnz have gp0: "?g' \<noteq> 0" by simp
   717       hence g'p: "?g' > 0" using gcd_ge_0_int[where x="n" and y="numgcd ?t'"] by arith
   718       hence "?g'= 1 \<or> ?g' > 1" by arith
   719       moreover {
   720         assume "?g' = 1" hence ?thesis using assms g1
   721           by (auto simp add: Let_def simp_num_pair_def simpnum_numbound0) }
   722       moreover {
   723         assume g'1: "?g' > 1"
   724         have gpdg: "?g' dvd ?g" by simp
   725         have gpdd: "?g' dvd n" by simp
   726         have gpdgp: "?g' dvd ?g'" by simp
   727         from zdvd_imp_le[OF gpdd np] have g'n: "?g' \<le> n" .
   728         from zdiv_mono1[OF g'n g'p, simplified div_self[OF gp0]]
   729         have "n div ?g' >0" by simp
   730         hence ?thesis using assms g1 g'1
   731           by(auto simp add: simp_num_pair_def Let_def reducecoeffh_numbound0 simpnum_numbound0) }
   732       ultimately have ?thesis by blast }
   733     ultimately have ?thesis by blast }
   734   ultimately show ?thesis by blast
   735 qed
   736 
   737 fun simpfm :: "fm \<Rightarrow> fm" where
   738   "simpfm (And p q) = conj (simpfm p) (simpfm q)"
   739 | "simpfm (Or p q) = disj (simpfm p) (simpfm q)"
   740 | "simpfm (Imp p q) = imp (simpfm p) (simpfm q)"
   741 | "simpfm (Iff p q) = iff (simpfm p) (simpfm q)"
   742 | "simpfm (NOT p) = not (simpfm p)"
   743 | "simpfm (Lt a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v < 0) then T else F 
   744   | _ \<Rightarrow> Lt a')"
   745 | "simpfm (Le a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v \<le> 0)  then T else F | _ \<Rightarrow> Le a')"
   746 | "simpfm (Gt a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v > 0)  then T else F | _ \<Rightarrow> Gt a')"
   747 | "simpfm (Ge a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v \<ge> 0)  then T else F | _ \<Rightarrow> Ge a')"
   748 | "simpfm (Eq a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v = 0)  then T else F | _ \<Rightarrow> Eq a')"
   749 | "simpfm (NEq a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v \<noteq> 0)  then T else F | _ \<Rightarrow> NEq a')"
   750 | "simpfm p = p"
   751 lemma simpfm: "Ifm bs (simpfm p) = Ifm bs p"
   752 proof(induct p rule: simpfm.induct)
   753   case (6 a) let ?sa = "simpnum a" from simpnum_ci have sa: "Inum bs ?sa = Inum bs a" by simp
   754   {fix v assume "?sa = C v" hence ?case using sa by simp }
   755   moreover {assume "\<not> (\<exists> v. ?sa = C v)" hence ?case using sa 
   756       by (cases ?sa, simp_all add: Let_def)}
   757   ultimately show ?case by blast
   758 next
   759   case (7 a)  let ?sa = "simpnum a" 
   760   from simpnum_ci have sa: "Inum bs ?sa = Inum bs a" by simp
   761   {fix v assume "?sa = C v" hence ?case using sa by simp }
   762   moreover {assume "\<not> (\<exists> v. ?sa = C v)" hence ?case using sa 
   763       by (cases ?sa, simp_all add: Let_def)}
   764   ultimately show ?case by blast
   765 next
   766   case (8 a)  let ?sa = "simpnum a" 
   767   from simpnum_ci have sa: "Inum bs ?sa = Inum bs a" by simp
   768   {fix v assume "?sa = C v" hence ?case using sa by simp }
   769   moreover {assume "\<not> (\<exists> v. ?sa = C v)" hence ?case using sa 
   770       by (cases ?sa, simp_all add: Let_def)}
   771   ultimately show ?case by blast
   772 next
   773   case (9 a)  let ?sa = "simpnum a" 
   774   from simpnum_ci have sa: "Inum bs ?sa = Inum bs a" by simp
   775   {fix v assume "?sa = C v" hence ?case using sa by simp }
   776   moreover {assume "\<not> (\<exists> v. ?sa = C v)" hence ?case using sa 
   777       by (cases ?sa, simp_all add: Let_def)}
   778   ultimately show ?case by blast
   779 next
   780   case (10 a)  let ?sa = "simpnum a" 
   781   from simpnum_ci have sa: "Inum bs ?sa = Inum bs a" by simp
   782   {fix v assume "?sa = C v" hence ?case using sa by simp }
   783   moreover {assume "\<not> (\<exists> v. ?sa = C v)" hence ?case using sa 
   784       by (cases ?sa, simp_all add: Let_def)}
   785   ultimately show ?case by blast
   786 next
   787   case (11 a)  let ?sa = "simpnum a" 
   788   from simpnum_ci have sa: "Inum bs ?sa = Inum bs a" by simp
   789   {fix v assume "?sa = C v" hence ?case using sa by simp }
   790   moreover {assume "\<not> (\<exists> v. ?sa = C v)" hence ?case using sa 
   791       by (cases ?sa, simp_all add: Let_def)}
   792   ultimately show ?case by blast
   793 qed (induct p rule: simpfm.induct, simp_all add: conj disj imp iff not)
   794 
   795 
   796 lemma simpfm_bound0: "bound0 p \<Longrightarrow> bound0 (simpfm p)"
   797 proof(induct p rule: simpfm.induct)
   798   case (6 a) hence nb: "numbound0 a" by simp
   799   hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
   800   thus ?case by (cases "simpnum a") (auto simp add: Let_def)
   801 next
   802   case (7 a) hence nb: "numbound0 a" by simp
   803   hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
   804   thus ?case by (cases "simpnum a") (auto simp add: Let_def)
   805 next
   806   case (8 a) hence nb: "numbound0 a" by simp
   807   hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
   808   thus ?case by (cases "simpnum a") (auto simp add: Let_def)
   809 next
   810   case (9 a) hence nb: "numbound0 a" by simp
   811   hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
   812   thus ?case by (cases "simpnum a") (auto simp add: Let_def)
   813 next
   814   case (10 a) hence nb: "numbound0 a" by simp
   815   hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
   816   thus ?case by (cases "simpnum a") (auto simp add: Let_def)
   817 next
   818   case (11 a) hence nb: "numbound0 a" by simp
   819   hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
   820   thus ?case by (cases "simpnum a") (auto simp add: Let_def)
   821 qed(auto simp add: disj_def imp_def iff_def conj_def not_bn)
   822 
   823 lemma simpfm_qf: "qfree p \<Longrightarrow> qfree (simpfm p)"
   824   apply (induct p rule: simpfm.induct)
   825   apply (auto simp add: Let_def)
   826   apply (case_tac "simpnum a", auto)+
   827   done
   828 
   829 consts prep :: "fm \<Rightarrow> fm"
   830 recdef prep "measure fmsize"
   831   "prep (E T) = T"
   832   "prep (E F) = F"
   833   "prep (E (Or p q)) = disj (prep (E p)) (prep (E q))"
   834   "prep (E (Imp p q)) = disj (prep (E (NOT p))) (prep (E q))"
   835   "prep (E (Iff p q)) = disj (prep (E (And p q))) (prep (E (And (NOT p) (NOT q))))" 
   836   "prep (E (NOT (And p q))) = disj (prep (E (NOT p))) (prep (E(NOT q)))"
   837   "prep (E (NOT (Imp p q))) = prep (E (And p (NOT q)))"
   838   "prep (E (NOT (Iff p q))) = disj (prep (E (And p (NOT q)))) (prep (E(And (NOT p) q)))"
   839   "prep (E p) = E (prep p)"
   840   "prep (A (And p q)) = conj (prep (A p)) (prep (A q))"
   841   "prep (A p) = prep (NOT (E (NOT p)))"
   842   "prep (NOT (NOT p)) = prep p"
   843   "prep (NOT (And p q)) = disj (prep (NOT p)) (prep (NOT q))"
   844   "prep (NOT (A p)) = prep (E (NOT p))"
   845   "prep (NOT (Or p q)) = conj (prep (NOT p)) (prep (NOT q))"
   846   "prep (NOT (Imp p q)) = conj (prep p) (prep (NOT q))"
   847   "prep (NOT (Iff p q)) = disj (prep (And p (NOT q))) (prep (And (NOT p) q))"
   848   "prep (NOT p) = not (prep p)"
   849   "prep (Or p q) = disj (prep p) (prep q)"
   850   "prep (And p q) = conj (prep p) (prep q)"
   851   "prep (Imp p q) = prep (Or (NOT p) q)"
   852   "prep (Iff p q) = disj (prep (And p q)) (prep (And (NOT p) (NOT q)))"
   853   "prep p = p"
   854 (hints simp add: fmsize_pos)
   855 lemma prep: "\<And> bs. Ifm bs (prep p) = Ifm bs p"
   856   by (induct p rule: prep.induct) auto
   857 
   858   (* Generic quantifier elimination *)
   859 function (sequential) qelim :: "fm \<Rightarrow> (fm \<Rightarrow> fm) \<Rightarrow> fm" where
   860   "qelim (E p) = (\<lambda> qe. DJ qe (qelim p qe))"
   861 | "qelim (A p) = (\<lambda> qe. not (qe ((qelim (NOT p) qe))))"
   862 | "qelim (NOT p) = (\<lambda> qe. not (qelim p qe))"
   863 | "qelim (And p q) = (\<lambda> qe. conj (qelim p qe) (qelim q qe))" 
   864 | "qelim (Or  p q) = (\<lambda> qe. disj (qelim p qe) (qelim q qe))" 
   865 | "qelim (Imp p q) = (\<lambda> qe. imp (qelim p qe) (qelim q qe))"
   866 | "qelim (Iff p q) = (\<lambda> qe. iff (qelim p qe) (qelim q qe))"
   867 | "qelim p = (\<lambda> y. simpfm p)"
   868 by pat_completeness auto
   869 termination qelim by (relation "measure fmsize") simp_all
   870 
   871 lemma qelim_ci:
   872   assumes qe_inv: "\<forall> bs p. qfree p \<longrightarrow> qfree (qe p) \<and> (Ifm bs (qe p) = Ifm bs (E p))"
   873   shows "\<And> bs. qfree (qelim p qe) \<and> (Ifm bs (qelim p qe) = Ifm bs p)"
   874 using qe_inv DJ_qe[OF qe_inv] 
   875 by(induct p rule: qelim.induct) 
   876 (auto simp add: not disj conj iff imp not_qf disj_qf conj_qf imp_qf iff_qf 
   877   simpfm simpfm_qf simp del: simpfm.simps)
   878 
   879 fun minusinf:: "fm \<Rightarrow> fm" (* Virtual substitution of -\<infinity>*) where
   880   "minusinf (And p q) = conj (minusinf p) (minusinf q)" 
   881 | "minusinf (Or p q) = disj (minusinf p) (minusinf q)" 
   882 | "minusinf (Eq  (CN 0 c e)) = F"
   883 | "minusinf (NEq (CN 0 c e)) = T"
   884 | "minusinf (Lt  (CN 0 c e)) = T"
   885 | "minusinf (Le  (CN 0 c e)) = T"
   886 | "minusinf (Gt  (CN 0 c e)) = F"
   887 | "minusinf (Ge  (CN 0 c e)) = F"
   888 | "minusinf p = p"
   889 
   890 fun plusinf:: "fm \<Rightarrow> fm" (* Virtual substitution of +\<infinity>*) where
   891   "plusinf (And p q) = conj (plusinf p) (plusinf q)" 
   892 | "plusinf (Or p q) = disj (plusinf p) (plusinf q)" 
   893 | "plusinf (Eq  (CN 0 c e)) = F"
   894 | "plusinf (NEq (CN 0 c e)) = T"
   895 | "plusinf (Lt  (CN 0 c e)) = F"
   896 | "plusinf (Le  (CN 0 c e)) = F"
   897 | "plusinf (Gt  (CN 0 c e)) = T"
   898 | "plusinf (Ge  (CN 0 c e)) = T"
   899 | "plusinf p = p"
   900 
   901 fun isrlfm :: "fm \<Rightarrow> bool"   (* Linearity test for fm *) where
   902   "isrlfm (And p q) = (isrlfm p \<and> isrlfm q)" 
   903 | "isrlfm (Or p q) = (isrlfm p \<and> isrlfm q)" 
   904 | "isrlfm (Eq  (CN 0 c e)) = (c>0 \<and> numbound0 e)"
   905 | "isrlfm (NEq (CN 0 c e)) = (c>0 \<and> numbound0 e)"
   906 | "isrlfm (Lt  (CN 0 c e)) = (c>0 \<and> numbound0 e)"
   907 | "isrlfm (Le  (CN 0 c e)) = (c>0 \<and> numbound0 e)"
   908 | "isrlfm (Gt  (CN 0 c e)) = (c>0 \<and> numbound0 e)"
   909 | "isrlfm (Ge  (CN 0 c e)) = (c>0 \<and> numbound0 e)"
   910 | "isrlfm p = (isatom p \<and> (bound0 p))"
   911 
   912   (* splits the bounded from the unbounded part*)
   913 function (sequential) rsplit0 :: "num \<Rightarrow> int \<times> num" where
   914   "rsplit0 (Bound 0) = (1,C 0)"
   915 | "rsplit0 (Add a b) = (let (ca,ta) = rsplit0 a ; (cb,tb) = rsplit0 b 
   916               in (ca+cb, Add ta tb))"
   917 | "rsplit0 (Sub a b) = rsplit0 (Add a (Neg b))"
   918 | "rsplit0 (Neg a) = (let (c,t) = rsplit0 a in (-c,Neg t))"
   919 | "rsplit0 (Mul c a) = (let (ca,ta) = rsplit0 a in (c*ca,Mul c ta))"
   920 | "rsplit0 (CN 0 c a) = (let (ca,ta) = rsplit0 a in (c+ca,ta))"
   921 | "rsplit0 (CN n c a) = (let (ca,ta) = rsplit0 a in (ca,CN n c ta))"
   922 | "rsplit0 t = (0,t)"
   923 by pat_completeness auto
   924 termination rsplit0 by (relation "measure num_size") simp_all
   925 
   926 lemma rsplit0: 
   927   shows "Inum bs ((split (CN 0)) (rsplit0 t)) = Inum bs t \<and> numbound0 (snd (rsplit0 t))"
   928 proof (induct t rule: rsplit0.induct)
   929   case (2 a b) 
   930   let ?sa = "rsplit0 a" let ?sb = "rsplit0 b"
   931   let ?ca = "fst ?sa" let ?cb = "fst ?sb"
   932   let ?ta = "snd ?sa" let ?tb = "snd ?sb"
   933   from 2 have nb: "numbound0 (snd(rsplit0 (Add a b)))" 
   934     by (cases "rsplit0 a") (auto simp add: Let_def split_def)
   935   have "Inum bs ((split (CN 0)) (rsplit0 (Add a b))) = 
   936     Inum bs ((split (CN 0)) ?sa)+Inum bs ((split (CN 0)) ?sb)"
   937     by (simp add: Let_def split_def algebra_simps)
   938   also have "\<dots> = Inum bs a + Inum bs b" using 2 by (cases "rsplit0 a") auto
   939   finally show ?case using nb by simp 
   940 qed (auto simp add: Let_def split_def algebra_simps, simp add: distrib_left[symmetric])
   941 
   942     (* Linearize a formula*)
   943 definition
   944   lt :: "int \<Rightarrow> num \<Rightarrow> fm"
   945 where
   946   "lt c t = (if c = 0 then (Lt t) else if c > 0 then (Lt (CN 0 c t)) 
   947     else (Gt (CN 0 (-c) (Neg t))))"
   948 
   949 definition
   950   le :: "int \<Rightarrow> num \<Rightarrow> fm"
   951 where
   952   "le c t = (if c = 0 then (Le t) else if c > 0 then (Le (CN 0 c t)) 
   953     else (Ge (CN 0 (-c) (Neg t))))"
   954 
   955 definition
   956   gt :: "int \<Rightarrow> num \<Rightarrow> fm"
   957 where
   958   "gt c t = (if c = 0 then (Gt t) else if c > 0 then (Gt (CN 0 c t)) 
   959     else (Lt (CN 0 (-c) (Neg t))))"
   960 
   961 definition
   962   ge :: "int \<Rightarrow> num \<Rightarrow> fm"
   963 where
   964   "ge c t = (if c = 0 then (Ge t) else if c > 0 then (Ge (CN 0 c t)) 
   965     else (Le (CN 0 (-c) (Neg t))))"
   966 
   967 definition
   968   eq :: "int \<Rightarrow> num \<Rightarrow> fm"
   969 where
   970   "eq c t = (if c = 0 then (Eq t) else if c > 0 then (Eq (CN 0 c t)) 
   971     else (Eq (CN 0 (-c) (Neg t))))"
   972 
   973 definition
   974   neq :: "int \<Rightarrow> num \<Rightarrow> fm"
   975 where
   976   "neq c t = (if c = 0 then (NEq t) else if c > 0 then (NEq (CN 0 c t)) 
   977     else (NEq (CN 0 (-c) (Neg t))))"
   978 
   979 lemma lt: "numnoabs t \<Longrightarrow> Ifm bs (split lt (rsplit0 t)) = Ifm bs (Lt t) \<and> isrlfm (split lt (rsplit0 t))"
   980 using rsplit0[where bs = "bs" and t="t"]
   981 by (auto simp add: lt_def split_def,cases "snd(rsplit0 t)",auto,case_tac "nat",auto)
   982 
   983 lemma le: "numnoabs t \<Longrightarrow> Ifm bs (split le (rsplit0 t)) = Ifm bs (Le t) \<and> isrlfm (split le (rsplit0 t))"
   984 using rsplit0[where bs = "bs" and t="t"]
   985 by (auto simp add: le_def split_def) (cases "snd(rsplit0 t)",auto,case_tac "nat",auto)
   986 
   987 lemma gt: "numnoabs t \<Longrightarrow> Ifm bs (split gt (rsplit0 t)) = Ifm bs (Gt t) \<and> isrlfm (split gt (rsplit0 t))"
   988 using rsplit0[where bs = "bs" and t="t"]
   989 by (auto simp add: gt_def split_def) (cases "snd(rsplit0 t)",auto,case_tac "nat",auto)
   990 
   991 lemma ge: "numnoabs t \<Longrightarrow> Ifm bs (split ge (rsplit0 t)) = Ifm bs (Ge t) \<and> isrlfm (split ge (rsplit0 t))"
   992 using rsplit0[where bs = "bs" and t="t"]
   993 by (auto simp add: ge_def split_def) (cases "snd(rsplit0 t)",auto,case_tac "nat",auto)
   994 
   995 lemma eq: "numnoabs t \<Longrightarrow> Ifm bs (split eq (rsplit0 t)) = Ifm bs (Eq t) \<and> isrlfm (split eq (rsplit0 t))"
   996 using rsplit0[where bs = "bs" and t="t"]
   997 by (auto simp add: eq_def split_def) (cases "snd(rsplit0 t)",auto,case_tac "nat",auto)
   998 
   999 lemma neq: "numnoabs t \<Longrightarrow> Ifm bs (split neq (rsplit0 t)) = Ifm bs (NEq t) \<and> isrlfm (split neq (rsplit0 t))"
  1000 using rsplit0[where bs = "bs" and t="t"]
  1001 by (auto simp add: neq_def split_def) (cases "snd(rsplit0 t)",auto,case_tac "nat",auto)
  1002 
  1003 lemma conj_lin: "isrlfm p \<Longrightarrow> isrlfm q \<Longrightarrow> isrlfm (conj p q)"
  1004 by (auto simp add: conj_def)
  1005 lemma disj_lin: "isrlfm p \<Longrightarrow> isrlfm q \<Longrightarrow> isrlfm (disj p q)"
  1006 by (auto simp add: disj_def)
  1007 
  1008 consts rlfm :: "fm \<Rightarrow> fm"
  1009 recdef rlfm "measure fmsize"
  1010   "rlfm (And p q) = conj (rlfm p) (rlfm q)"
  1011   "rlfm (Or p q) = disj (rlfm p) (rlfm q)"
  1012   "rlfm (Imp p q) = disj (rlfm (NOT p)) (rlfm q)"
  1013   "rlfm (Iff p q) = disj (conj (rlfm p) (rlfm q)) (conj (rlfm (NOT p)) (rlfm (NOT q)))"
  1014   "rlfm (Lt a) = split lt (rsplit0 a)"
  1015   "rlfm (Le a) = split le (rsplit0 a)"
  1016   "rlfm (Gt a) = split gt (rsplit0 a)"
  1017   "rlfm (Ge a) = split ge (rsplit0 a)"
  1018   "rlfm (Eq a) = split eq (rsplit0 a)"
  1019   "rlfm (NEq a) = split neq (rsplit0 a)"
  1020   "rlfm (NOT (And p q)) = disj (rlfm (NOT p)) (rlfm (NOT q))"
  1021   "rlfm (NOT (Or p q)) = conj (rlfm (NOT p)) (rlfm (NOT q))"
  1022   "rlfm (NOT (Imp p q)) = conj (rlfm p) (rlfm (NOT q))"
  1023   "rlfm (NOT (Iff p q)) = disj (conj(rlfm p) (rlfm(NOT q))) (conj(rlfm(NOT p)) (rlfm q))"
  1024   "rlfm (NOT (NOT p)) = rlfm p"
  1025   "rlfm (NOT T) = F"
  1026   "rlfm (NOT F) = T"
  1027   "rlfm (NOT (Lt a)) = rlfm (Ge a)"
  1028   "rlfm (NOT (Le a)) = rlfm (Gt a)"
  1029   "rlfm (NOT (Gt a)) = rlfm (Le a)"
  1030   "rlfm (NOT (Ge a)) = rlfm (Lt a)"
  1031   "rlfm (NOT (Eq a)) = rlfm (NEq a)"
  1032   "rlfm (NOT (NEq a)) = rlfm (Eq a)"
  1033   "rlfm p = p" (hints simp add: fmsize_pos)
  1034 
  1035 lemma rlfm_I:
  1036   assumes qfp: "qfree p"
  1037   shows "(Ifm bs (rlfm p) = Ifm bs p) \<and> isrlfm (rlfm p)"
  1038   using qfp 
  1039 by (induct p rule: rlfm.induct) (auto simp add: lt le gt ge eq neq conj disj conj_lin disj_lin)
  1040 
  1041     (* Operations needed for Ferrante and Rackoff *)
  1042 lemma rminusinf_inf:
  1043   assumes lp: "isrlfm p"
  1044   shows "\<exists> z. \<forall> x < z. Ifm (x#bs) (minusinf p) = Ifm (x#bs) p" (is "\<exists> z. \<forall> x. ?P z x p")
  1045 using lp
  1046 proof (induct p rule: minusinf.induct)
  1047   case (1 p q)
  1048   thus ?case apply auto apply (rule_tac x= "min z za" in exI) apply auto done
  1049 next
  1050   case (2 p q)
  1051   thus ?case apply auto apply (rule_tac x= "min z za" in exI) apply auto done
  1052 next
  1053   case (3 c e) 
  1054   from 3 have nb: "numbound0 e" by simp
  1055   from 3 have cp: "real c > 0" by simp
  1056   fix a
  1057   let ?e="Inum (a#bs) e"
  1058   let ?z = "(- ?e) / real c"
  1059   {fix x
  1060     assume xz: "x < ?z"
  1061     hence "(real c * x < - ?e)" 
  1062       by (simp only: pos_less_divide_eq[OF cp, where a="x" and b="- ?e"] mult_ac) 
  1063     hence "real c * x + ?e < 0" by arith
  1064     hence "real c * x + ?e \<noteq> 0" by simp
  1065     with xz have "?P ?z x (Eq (CN 0 c e))"
  1066       using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp  }
  1067   hence "\<forall> x < ?z. ?P ?z x (Eq (CN 0 c e))" by simp
  1068   thus ?case by blast
  1069 next
  1070   case (4 c e)   
  1071   from 4 have nb: "numbound0 e" by simp
  1072   from 4 have cp: "real c > 0" by simp
  1073   fix a
  1074   let ?e="Inum (a#bs) e"
  1075   let ?z = "(- ?e) / real c"
  1076   {fix x
  1077     assume xz: "x < ?z"
  1078     hence "(real c * x < - ?e)" 
  1079       by (simp only: pos_less_divide_eq[OF cp, where a="x" and b="- ?e"] mult_ac) 
  1080     hence "real c * x + ?e < 0" by arith
  1081     hence "real c * x + ?e \<noteq> 0" by simp
  1082     with xz have "?P ?z x (NEq (CN 0 c e))"
  1083       using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp }
  1084   hence "\<forall> x < ?z. ?P ?z x (NEq (CN 0 c e))" by simp
  1085   thus ?case by blast
  1086 next
  1087   case (5 c e) 
  1088   from 5 have nb: "numbound0 e" by simp
  1089   from 5 have cp: "real c > 0" by simp
  1090   fix a
  1091   let ?e="Inum (a#bs) e"
  1092   let ?z = "(- ?e) / real c"
  1093   {fix x
  1094     assume xz: "x < ?z"
  1095     hence "(real c * x < - ?e)" 
  1096       by (simp only: pos_less_divide_eq[OF cp, where a="x" and b="- ?e"] mult_ac) 
  1097     hence "real c * x + ?e < 0" by arith
  1098     with xz have "?P ?z x (Lt (CN 0 c e))"
  1099       using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"]  by simp }
  1100   hence "\<forall> x < ?z. ?P ?z x (Lt (CN 0 c e))" by simp
  1101   thus ?case by blast
  1102 next
  1103   case (6 c e)  
  1104   from 6 have nb: "numbound0 e" by simp
  1105   from lp 6 have cp: "real c > 0" by simp
  1106   fix a
  1107   let ?e="Inum (a#bs) e"
  1108   let ?z = "(- ?e) / real c"
  1109   {fix x
  1110     assume xz: "x < ?z"
  1111     hence "(real c * x < - ?e)" 
  1112       by (simp only: pos_less_divide_eq[OF cp, where a="x" and b="- ?e"] mult_ac) 
  1113     hence "real c * x + ?e < 0" by arith
  1114     with xz have "?P ?z x (Le (CN 0 c e))"
  1115       using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp }
  1116   hence "\<forall> x < ?z. ?P ?z x (Le (CN 0 c e))" by simp
  1117   thus ?case by blast
  1118 next
  1119   case (7 c e)  
  1120   from 7 have nb: "numbound0 e" by simp
  1121   from 7 have cp: "real c > 0" by simp
  1122   fix a
  1123   let ?e="Inum (a#bs) e"
  1124   let ?z = "(- ?e) / real c"
  1125   {fix x
  1126     assume xz: "x < ?z"
  1127     hence "(real c * x < - ?e)" 
  1128       by (simp only: pos_less_divide_eq[OF cp, where a="x" and b="- ?e"] mult_ac) 
  1129     hence "real c * x + ?e < 0" by arith
  1130     with xz have "?P ?z x (Gt (CN 0 c e))"
  1131       using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp }
  1132   hence "\<forall> x < ?z. ?P ?z x (Gt (CN 0 c e))" by simp
  1133   thus ?case by blast
  1134 next
  1135   case (8 c e)  
  1136   from 8 have nb: "numbound0 e" by simp
  1137   from 8 have cp: "real c > 0" by simp
  1138   fix a
  1139   let ?e="Inum (a#bs) e"
  1140   let ?z = "(- ?e) / real c"
  1141   {fix x
  1142     assume xz: "x < ?z"
  1143     hence "(real c * x < - ?e)" 
  1144       by (simp only: pos_less_divide_eq[OF cp, where a="x" and b="- ?e"] mult_ac) 
  1145     hence "real c * x + ?e < 0" by arith
  1146     with xz have "?P ?z x (Ge (CN 0 c e))"
  1147       using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp }
  1148   hence "\<forall> x < ?z. ?P ?z x (Ge (CN 0 c e))" by simp
  1149   thus ?case by blast
  1150 qed simp_all
  1151 
  1152 lemma rplusinf_inf:
  1153   assumes lp: "isrlfm p"
  1154   shows "\<exists> z. \<forall> x > z. Ifm (x#bs) (plusinf p) = Ifm (x#bs) p" (is "\<exists> z. \<forall> x. ?P z x p")
  1155 using lp
  1156 proof (induct p rule: isrlfm.induct)
  1157   case (1 p q) thus ?case by (auto,rule_tac x= "max z za" in exI) auto
  1158 next
  1159   case (2 p q) thus ?case by (auto,rule_tac x= "max z za" in exI) auto
  1160 next
  1161   case (3 c e) 
  1162   from 3 have nb: "numbound0 e" by simp
  1163   from 3 have cp: "real c > 0" by simp
  1164   fix a
  1165   let ?e="Inum (a#bs) e"
  1166   let ?z = "(- ?e) / real c"
  1167   {fix x
  1168     assume xz: "x > ?z"
  1169     with mult_strict_right_mono [OF xz cp] cp
  1170     have "(real c * x > - ?e)" by (simp add: mult_ac)
  1171     hence "real c * x + ?e > 0" by arith
  1172     hence "real c * x + ?e \<noteq> 0" by simp
  1173     with xz have "?P ?z x (Eq (CN 0 c e))"
  1174       using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp }
  1175   hence "\<forall> x > ?z. ?P ?z x (Eq (CN 0 c e))" by simp
  1176   thus ?case by blast
  1177 next
  1178   case (4 c e) 
  1179   from 4 have nb: "numbound0 e" by simp
  1180   from 4 have cp: "real c > 0" by simp
  1181   fix a
  1182   let ?e="Inum (a#bs) e"
  1183   let ?z = "(- ?e) / real c"
  1184   {fix x
  1185     assume xz: "x > ?z"
  1186     with mult_strict_right_mono [OF xz cp] cp
  1187     have "(real c * x > - ?e)" by (simp add: mult_ac)
  1188     hence "real c * x + ?e > 0" by arith
  1189     hence "real c * x + ?e \<noteq> 0" by simp
  1190     with xz have "?P ?z x (NEq (CN 0 c e))"
  1191       using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp }
  1192   hence "\<forall> x > ?z. ?P ?z x (NEq (CN 0 c e))" by simp
  1193   thus ?case by blast
  1194 next
  1195   case (5 c e) 
  1196   from 5 have nb: "numbound0 e" by simp
  1197   from 5 have cp: "real c > 0" by simp
  1198   fix a
  1199   let ?e="Inum (a#bs) e"
  1200   let ?z = "(- ?e) / real c"
  1201   {fix x
  1202     assume xz: "x > ?z"
  1203     with mult_strict_right_mono [OF xz cp] cp
  1204     have "(real c * x > - ?e)" by (simp add: mult_ac)
  1205     hence "real c * x + ?e > 0" by arith
  1206     with xz have "?P ?z x (Lt (CN 0 c e))"
  1207       using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp }
  1208   hence "\<forall> x > ?z. ?P ?z x (Lt (CN 0 c e))" by simp
  1209   thus ?case by blast
  1210 next
  1211   case (6 c e) 
  1212   from 6 have nb: "numbound0 e" by simp
  1213   from 6 have cp: "real c > 0" by simp
  1214   fix a
  1215   let ?e="Inum (a#bs) e"
  1216   let ?z = "(- ?e) / real c"
  1217   {fix x
  1218     assume xz: "x > ?z"
  1219     with mult_strict_right_mono [OF xz cp] cp
  1220     have "(real c * x > - ?e)" by (simp add: mult_ac)
  1221     hence "real c * x + ?e > 0" by arith
  1222     with xz have "?P ?z x (Le (CN 0 c e))"
  1223       using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp }
  1224   hence "\<forall> x > ?z. ?P ?z x (Le (CN 0 c e))" by simp
  1225   thus ?case by blast
  1226 next
  1227   case (7 c e) 
  1228   from 7 have nb: "numbound0 e" by simp
  1229   from 7 have cp: "real c > 0" by simp
  1230   fix a
  1231   let ?e="Inum (a#bs) e"
  1232   let ?z = "(- ?e) / real c"
  1233   {fix x
  1234     assume xz: "x > ?z"
  1235     with mult_strict_right_mono [OF xz cp] cp
  1236     have "(real c * x > - ?e)" by (simp add: mult_ac)
  1237     hence "real c * x + ?e > 0" by arith
  1238     with xz have "?P ?z x (Gt (CN 0 c e))"
  1239       using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp }
  1240   hence "\<forall> x > ?z. ?P ?z x (Gt (CN 0 c e))" by simp
  1241   thus ?case by blast
  1242 next
  1243   case (8 c e) 
  1244   from 8 have nb: "numbound0 e" by simp
  1245   from 8 have cp: "real c > 0" by simp
  1246   fix a
  1247   let ?e="Inum (a#bs) e"
  1248   let ?z = "(- ?e) / real c"
  1249   {fix x
  1250     assume xz: "x > ?z"
  1251     with mult_strict_right_mono [OF xz cp] cp
  1252     have "(real c * x > - ?e)" by (simp add: mult_ac)
  1253     hence "real c * x + ?e > 0" by arith
  1254     with xz have "?P ?z x (Ge (CN 0 c e))"
  1255       using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"]   by simp }
  1256   hence "\<forall> x > ?z. ?P ?z x (Ge (CN 0 c e))" by simp
  1257   thus ?case by blast
  1258 qed simp_all
  1259 
  1260 lemma rminusinf_bound0:
  1261   assumes lp: "isrlfm p"
  1262   shows "bound0 (minusinf p)"
  1263   using lp
  1264   by (induct p rule: minusinf.induct) simp_all
  1265 
  1266 lemma rplusinf_bound0:
  1267   assumes lp: "isrlfm p"
  1268   shows "bound0 (plusinf p)"
  1269   using lp
  1270   by (induct p rule: plusinf.induct) simp_all
  1271 
  1272 lemma rminusinf_ex:
  1273   assumes lp: "isrlfm p"
  1274   and ex: "Ifm (a#bs) (minusinf p)"
  1275   shows "\<exists> x. Ifm (x#bs) p"
  1276 proof-
  1277   from bound0_I [OF rminusinf_bound0[OF lp], where b="a" and bs ="bs"] ex
  1278   have th: "\<forall> x. Ifm (x#bs) (minusinf p)" by auto
  1279   from rminusinf_inf[OF lp, where bs="bs"] 
  1280   obtain z where z_def: "\<forall>x<z. Ifm (x # bs) (minusinf p) = Ifm (x # bs) p" by blast
  1281   from th have "Ifm ((z - 1)#bs) (minusinf p)" by simp
  1282   moreover have "z - 1 < z" by simp
  1283   ultimately show ?thesis using z_def by auto
  1284 qed
  1285 
  1286 lemma rplusinf_ex:
  1287   assumes lp: "isrlfm p"
  1288   and ex: "Ifm (a#bs) (plusinf p)"
  1289   shows "\<exists> x. Ifm (x#bs) p"
  1290 proof-
  1291   from bound0_I [OF rplusinf_bound0[OF lp], where b="a" and bs ="bs"] ex
  1292   have th: "\<forall> x. Ifm (x#bs) (plusinf p)" by auto
  1293   from rplusinf_inf[OF lp, where bs="bs"] 
  1294   obtain z where z_def: "\<forall>x>z. Ifm (x # bs) (plusinf p) = Ifm (x # bs) p" by blast
  1295   from th have "Ifm ((z + 1)#bs) (plusinf p)" by simp
  1296   moreover have "z + 1 > z" by simp
  1297   ultimately show ?thesis using z_def by auto
  1298 qed
  1299 
  1300 consts 
  1301   uset:: "fm \<Rightarrow> (num \<times> int) list"
  1302   usubst :: "fm \<Rightarrow> (num \<times> int) \<Rightarrow> fm "
  1303 recdef uset "measure size"
  1304   "uset (And p q) = (uset p @ uset q)" 
  1305   "uset (Or p q) = (uset p @ uset q)" 
  1306   "uset (Eq  (CN 0 c e)) = [(Neg e,c)]"
  1307   "uset (NEq (CN 0 c e)) = [(Neg e,c)]"
  1308   "uset (Lt  (CN 0 c e)) = [(Neg e,c)]"
  1309   "uset (Le  (CN 0 c e)) = [(Neg e,c)]"
  1310   "uset (Gt  (CN 0 c e)) = [(Neg e,c)]"
  1311   "uset (Ge  (CN 0 c e)) = [(Neg e,c)]"
  1312   "uset p = []"
  1313 recdef usubst "measure size"
  1314   "usubst (And p q) = (\<lambda> (t,n). And (usubst p (t,n)) (usubst q (t,n)))"
  1315   "usubst (Or p q) = (\<lambda> (t,n). Or (usubst p (t,n)) (usubst q (t,n)))"
  1316   "usubst (Eq (CN 0 c e)) = (\<lambda> (t,n). Eq (Add (Mul c t) (Mul n e)))"
  1317   "usubst (NEq (CN 0 c e)) = (\<lambda> (t,n). NEq (Add (Mul c t) (Mul n e)))"
  1318   "usubst (Lt (CN 0 c e)) = (\<lambda> (t,n). Lt (Add (Mul c t) (Mul n e)))"
  1319   "usubst (Le (CN 0 c e)) = (\<lambda> (t,n). Le (Add (Mul c t) (Mul n e)))"
  1320   "usubst (Gt (CN 0 c e)) = (\<lambda> (t,n). Gt (Add (Mul c t) (Mul n e)))"
  1321   "usubst (Ge (CN 0 c e)) = (\<lambda> (t,n). Ge (Add (Mul c t) (Mul n e)))"
  1322   "usubst p = (\<lambda> (t,n). p)"
  1323 
  1324 lemma usubst_I: assumes lp: "isrlfm p"
  1325   and np: "real n > 0" and nbt: "numbound0 t"
  1326   shows "(Ifm (x#bs) (usubst p (t,n)) = Ifm (((Inum (x#bs) t)/(real n))#bs) p) \<and> bound0 (usubst p (t,n))" (is "(?I x (usubst p (t,n)) = ?I ?u p) \<and> ?B p" is "(_ = ?I (?t/?n) p) \<and> _" is "(_ = ?I (?N x t /_) p) \<and> _")
  1327   using lp
  1328 proof(induct p rule: usubst.induct)
  1329   case (5 c e) with assms have cp: "c >0" and nb: "numbound0 e" by simp_all
  1330   have "?I ?u (Lt (CN 0 c e)) = (real c *(?t/?n) + (?N x e) < 0)"
  1331     using numbound0_I[OF nb, where bs="bs" and b="?u" and b'="x"] by simp
  1332   also have "\<dots> = (?n*(real c *(?t/?n)) + ?n*(?N x e) < 0)"
  1333     by (simp only: pos_less_divide_eq[OF np, where a="real c *(?t/?n) + (?N x e)" 
  1334       and b="0", simplified divide_zero_left]) (simp only: algebra_simps)
  1335   also have "\<dots> = (real c *?t + ?n* (?N x e) < 0)"
  1336     using np by simp 
  1337   finally show ?case using nbt nb by (simp add: algebra_simps)
  1338 next
  1339   case (6 c e) with assms have cp: "c >0" and nb: "numbound0 e" by simp_all
  1340   have "?I ?u (Le (CN 0 c e)) = (real c *(?t/?n) + (?N x e) \<le> 0)"
  1341     using numbound0_I[OF nb, where bs="bs" and b="?u" and b'="x"] by simp
  1342   also have "\<dots> = (?n*(real c *(?t/?n)) + ?n*(?N x e) \<le> 0)"
  1343     by (simp only: pos_le_divide_eq[OF np, where a="real c *(?t/?n) + (?N x e)" 
  1344       and b="0", simplified divide_zero_left]) (simp only: algebra_simps)
  1345   also have "\<dots> = (real c *?t + ?n* (?N x e) \<le> 0)"
  1346     using np by simp 
  1347   finally show ?case using nbt nb by (simp add: algebra_simps)
  1348 next
  1349   case (7 c e) with assms have cp: "c >0" and nb: "numbound0 e" by simp_all
  1350   have "?I ?u (Gt (CN 0 c e)) = (real c *(?t/?n) + (?N x e) > 0)"
  1351     using numbound0_I[OF nb, where bs="bs" and b="?u" and b'="x"] by simp
  1352   also have "\<dots> = (?n*(real c *(?t/?n)) + ?n*(?N x e) > 0)"
  1353     by (simp only: pos_divide_less_eq[OF np, where a="real c *(?t/?n) + (?N x e)" 
  1354       and b="0", simplified divide_zero_left]) (simp only: algebra_simps)
  1355   also have "\<dots> = (real c *?t + ?n* (?N x e) > 0)"
  1356     using np by simp 
  1357   finally show ?case using nbt nb by (simp add: algebra_simps)
  1358 next
  1359   case (8 c e) with assms have cp: "c >0" and nb: "numbound0 e" by simp_all
  1360   have "?I ?u (Ge (CN 0 c e)) = (real c *(?t/?n) + (?N x e) \<ge> 0)"
  1361     using numbound0_I[OF nb, where bs="bs" and b="?u" and b'="x"] by simp
  1362   also have "\<dots> = (?n*(real c *(?t/?n)) + ?n*(?N x e) \<ge> 0)"
  1363     by (simp only: pos_divide_le_eq[OF np, where a="real c *(?t/?n) + (?N x e)" 
  1364       and b="0", simplified divide_zero_left]) (simp only: algebra_simps)
  1365   also have "\<dots> = (real c *?t + ?n* (?N x e) \<ge> 0)"
  1366     using np by simp 
  1367   finally show ?case using nbt nb by (simp add: algebra_simps)
  1368 next
  1369   case (3 c e) with assms have cp: "c >0" and nb: "numbound0 e" by simp_all
  1370   from np have np: "real n \<noteq> 0" by simp
  1371   have "?I ?u (Eq (CN 0 c e)) = (real c *(?t/?n) + (?N x e) = 0)"
  1372     using numbound0_I[OF nb, where bs="bs" and b="?u" and b'="x"] by simp
  1373   also have "\<dots> = (?n*(real c *(?t/?n)) + ?n*(?N x e) = 0)"
  1374     by (simp only: nonzero_eq_divide_eq[OF np, where a="real c *(?t/?n) + (?N x e)" 
  1375       and b="0", simplified divide_zero_left]) (simp only: algebra_simps)
  1376   also have "\<dots> = (real c *?t + ?n* (?N x e) = 0)"
  1377     using np by simp 
  1378   finally show ?case using nbt nb by (simp add: algebra_simps)
  1379 next
  1380   case (4 c e) with assms have cp: "c >0" and nb: "numbound0 e" by simp_all
  1381   from np have np: "real n \<noteq> 0" by simp
  1382   have "?I ?u (NEq (CN 0 c e)) = (real c *(?t/?n) + (?N x e) \<noteq> 0)"
  1383     using numbound0_I[OF nb, where bs="bs" and b="?u" and b'="x"] by simp
  1384   also have "\<dots> = (?n*(real c *(?t/?n)) + ?n*(?N x e) \<noteq> 0)"
  1385     by (simp only: nonzero_eq_divide_eq[OF np, where a="real c *(?t/?n) + (?N x e)" 
  1386       and b="0", simplified divide_zero_left]) (simp only: algebra_simps)
  1387   also have "\<dots> = (real c *?t + ?n* (?N x e) \<noteq> 0)"
  1388     using np by simp 
  1389   finally show ?case using nbt nb by (simp add: algebra_simps)
  1390 qed(simp_all add: nbt numbound0_I[where bs ="bs" and b="(Inum (x#bs) t)/ real n" and b'="x"])
  1391 
  1392 lemma uset_l:
  1393   assumes lp: "isrlfm p"
  1394   shows "\<forall> (t,k) \<in> set (uset p). numbound0 t \<and> k >0"
  1395 using lp
  1396 by(induct p rule: uset.induct,auto)
  1397 
  1398 lemma rminusinf_uset:
  1399   assumes lp: "isrlfm p"
  1400   and nmi: "\<not> (Ifm (a#bs) (minusinf p))" (is "\<not> (Ifm (a#bs) (?M p))")
  1401   and ex: "Ifm (x#bs) p" (is "?I x p")
  1402   shows "\<exists> (s,m) \<in> set (uset p). x \<ge> Inum (a#bs) s / real m" (is "\<exists> (s,m) \<in> ?U p. x \<ge> ?N a s / real m")
  1403 proof-
  1404   have "\<exists> (s,m) \<in> set (uset p). real m * x \<ge> Inum (a#bs) s " (is "\<exists> (s,m) \<in> ?U p. real m *x \<ge> ?N a s")
  1405     using lp nmi ex
  1406     by (induct p rule: minusinf.induct, auto simp add:numbound0_I[where bs="bs" and b="a" and b'="x"])
  1407   then obtain s m where smU: "(s,m) \<in> set (uset p)" and mx: "real m * x \<ge> ?N a s" by blast
  1408   from uset_l[OF lp] smU have mp: "real m > 0" by auto
  1409   from pos_divide_le_eq[OF mp, where a="x" and b="?N a s", symmetric] mx have "x \<ge> ?N a s / real m" 
  1410     by (auto simp add: mult_commute)
  1411   thus ?thesis using smU by auto
  1412 qed
  1413 
  1414 lemma rplusinf_uset:
  1415   assumes lp: "isrlfm p"
  1416   and nmi: "\<not> (Ifm (a#bs) (plusinf p))" (is "\<not> (Ifm (a#bs) (?M p))")
  1417   and ex: "Ifm (x#bs) p" (is "?I x p")
  1418   shows "\<exists> (s,m) \<in> set (uset p). x \<le> Inum (a#bs) s / real m" (is "\<exists> (s,m) \<in> ?U p. x \<le> ?N a s / real m")
  1419 proof-
  1420   have "\<exists> (s,m) \<in> set (uset p). real m * x \<le> Inum (a#bs) s " (is "\<exists> (s,m) \<in> ?U p. real m *x \<le> ?N a s")
  1421     using lp nmi ex
  1422     by (induct p rule: minusinf.induct, auto simp add:numbound0_I[where bs="bs" and b="a" and b'="x"])
  1423   then obtain s m where smU: "(s,m) \<in> set (uset p)" and mx: "real m * x \<le> ?N a s" by blast
  1424   from uset_l[OF lp] smU have mp: "real m > 0" by auto
  1425   from pos_le_divide_eq[OF mp, where a="x" and b="?N a s", symmetric] mx have "x \<le> ?N a s / real m" 
  1426     by (auto simp add: mult_commute)
  1427   thus ?thesis using smU by auto
  1428 qed
  1429 
  1430 lemma lin_dense: 
  1431   assumes lp: "isrlfm p"
  1432   and noS: "\<forall> t. l < t \<and> t< u \<longrightarrow> t \<notin> (\<lambda> (t,n). Inum (x#bs) t / real n) ` set (uset p)" 
  1433   (is "\<forall> t. _ \<and> _ \<longrightarrow> t \<notin> (\<lambda> (t,n). ?N x t / real n ) ` (?U p)")
  1434   and lx: "l < x" and xu:"x < u" and px:" Ifm (x#bs) p"
  1435   and ly: "l < y" and yu: "y < u"
  1436   shows "Ifm (y#bs) p"
  1437 using lp px noS
  1438 proof (induct p rule: isrlfm.induct)
  1439   case (5 c e) hence cp: "real c > 0" and nb: "numbound0 e" by simp+
  1440   from 5 have "x * real c + ?N x e < 0" by (simp add: algebra_simps)
  1441   hence pxc: "x < (- ?N x e) / real c" 
  1442     by (simp only: pos_less_divide_eq[OF cp, where a="x" and b="-?N x e"])
  1443   from 5 have noSc:"\<forall> t. l < t \<and> t < u \<longrightarrow> t \<noteq> (- ?N x e) / real c" by auto
  1444   with ly yu have yne: "y \<noteq> - ?N x e / real c" by auto
  1445   hence "y < (- ?N x e) / real c \<or> y > (-?N x e) / real c" by auto
  1446   moreover {assume y: "y < (-?N x e)/ real c"
  1447     hence "y * real c < - ?N x e"
  1448       by (simp add: pos_less_divide_eq[OF cp, where a="y" and b="-?N x e", symmetric])
  1449     hence "real c * y + ?N x e < 0" by (simp add: algebra_simps)
  1450     hence ?case using numbound0_I[OF nb, where bs="bs" and b="x" and b'="y"] by simp}
  1451   moreover {assume y: "y > (- ?N x e) / real c" 
  1452     with yu have eu: "u > (- ?N x e) / real c" by auto
  1453     with noSc ly yu have "(- ?N x e) / real c \<le> l" by (cases "(- ?N x e) / real c > l", auto)
  1454     with lx pxc have "False" by auto
  1455     hence ?case by simp }
  1456   ultimately show ?case by blast
  1457 next
  1458   case (6 c e) hence cp: "real c > 0" and nb: "numbound0 e" by simp +
  1459   from 6 have "x * real c + ?N x e \<le> 0" by (simp add: algebra_simps)
  1460   hence pxc: "x \<le> (- ?N x e) / real c" 
  1461     by (simp only: pos_le_divide_eq[OF cp, where a="x" and b="-?N x e"])
  1462   from 6 have noSc:"\<forall> t. l < t \<and> t < u \<longrightarrow> t \<noteq> (- ?N x e) / real c" by auto
  1463   with ly yu have yne: "y \<noteq> - ?N x e / real c" by auto
  1464   hence "y < (- ?N x e) / real c \<or> y > (-?N x e) / real c" by auto
  1465   moreover {assume y: "y < (-?N x e)/ real c"
  1466     hence "y * real c < - ?N x e"
  1467       by (simp add: pos_less_divide_eq[OF cp, where a="y" and b="-?N x e", symmetric])
  1468     hence "real c * y + ?N x e < 0" by (simp add: algebra_simps)
  1469     hence ?case using numbound0_I[OF nb, where bs="bs" and b="x" and b'="y"] by simp}
  1470   moreover {assume y: "y > (- ?N x e) / real c" 
  1471     with yu have eu: "u > (- ?N x e) / real c" by auto
  1472     with noSc ly yu have "(- ?N x e) / real c \<le> l" by (cases "(- ?N x e) / real c > l", auto)
  1473     with lx pxc have "False" by auto
  1474     hence ?case by simp }
  1475   ultimately show ?case by blast
  1476 next
  1477   case (7 c e) hence cp: "real c > 0" and nb: "numbound0 e" by simp+
  1478   from 7 have "x * real c + ?N x e > 0" by (simp add: algebra_simps)
  1479   hence pxc: "x > (- ?N x e) / real c" 
  1480     by (simp only: pos_divide_less_eq[OF cp, where a="x" and b="-?N x e"])
  1481   from 7 have noSc: "\<forall> t. l < t \<and> t < u \<longrightarrow> t \<noteq> (- ?N x e) / real c" by auto
  1482   with ly yu have yne: "y \<noteq> - ?N x e / real c" by auto
  1483   hence "y < (- ?N x e) / real c \<or> y > (-?N x e) / real c" by auto
  1484   moreover {assume y: "y > (-?N x e)/ real c"
  1485     hence "y * real c > - ?N x e"
  1486       by (simp add: pos_divide_less_eq[OF cp, where a="y" and b="-?N x e", symmetric])
  1487     hence "real c * y + ?N x e > 0" by (simp add: algebra_simps)
  1488     hence ?case using numbound0_I[OF nb, where bs="bs" and b="x" and b'="y"] by simp}
  1489   moreover {assume y: "y < (- ?N x e) / real c" 
  1490     with ly have eu: "l < (- ?N x e) / real c" by auto
  1491     with noSc ly yu have "(- ?N x e) / real c \<ge> u" by (cases "(- ?N x e) / real c > l", auto)
  1492     with xu pxc have "False" by auto
  1493     hence ?case by simp }
  1494   ultimately show ?case by blast
  1495 next
  1496   case (8 c e) hence cp: "real c > 0" and nb: "numbound0 e" by simp+
  1497   from 8 have "x * real c + ?N x e \<ge> 0" by (simp add: algebra_simps)
  1498   hence pxc: "x \<ge> (- ?N x e) / real c" 
  1499     by (simp only: pos_divide_le_eq[OF cp, where a="x" and b="-?N x e"])
  1500   from 8 have noSc:"\<forall> t. l < t \<and> t < u \<longrightarrow> t \<noteq> (- ?N x e) / real c" by auto
  1501   with ly yu have yne: "y \<noteq> - ?N x e / real c" by auto
  1502   hence "y < (- ?N x e) / real c \<or> y > (-?N x e) / real c" by auto
  1503   moreover {assume y: "y > (-?N x e)/ real c"
  1504     hence "y * real c > - ?N x e"
  1505       by (simp add: pos_divide_less_eq[OF cp, where a="y" and b="-?N x e", symmetric])
  1506     hence "real c * y + ?N x e > 0" by (simp add: algebra_simps)
  1507     hence ?case using numbound0_I[OF nb, where bs="bs" and b="x" and b'="y"] by simp}
  1508   moreover {assume y: "y < (- ?N x e) / real c" 
  1509     with ly have eu: "l < (- ?N x e) / real c" by auto
  1510     with noSc ly yu have "(- ?N x e) / real c \<ge> u" by (cases "(- ?N x e) / real c > l", auto)
  1511     with xu pxc have "False" by auto
  1512     hence ?case by simp }
  1513   ultimately show ?case by blast
  1514 next
  1515   case (3 c e) hence cp: "real c > 0" and nb: "numbound0 e" by simp+
  1516   from cp have cnz: "real c \<noteq> 0" by simp
  1517   from 3 have "x * real c + ?N x e = 0" by (simp add: algebra_simps)
  1518   hence pxc: "x = (- ?N x e) / real c" 
  1519     by (simp only: nonzero_eq_divide_eq[OF cnz, where a="x" and b="-?N x e"])
  1520   from 3 have noSc:"\<forall> t. l < t \<and> t < u \<longrightarrow> t \<noteq> (- ?N x e) / real c" by auto
  1521   with lx xu have yne: "x \<noteq> - ?N x e / real c" by auto
  1522   with pxc show ?case by simp
  1523 next
  1524   case (4 c e) hence cp: "real c > 0" and nb: "numbound0 e" by simp+
  1525   from cp have cnz: "real c \<noteq> 0" by simp
  1526   from 4 have noSc:"\<forall> t. l < t \<and> t < u \<longrightarrow> t \<noteq> (- ?N x e) / real c" by auto
  1527   with ly yu have yne: "y \<noteq> - ?N x e / real c" by auto
  1528   hence "y* real c \<noteq> -?N x e"      
  1529     by (simp only: nonzero_eq_divide_eq[OF cnz, where a="y" and b="-?N x e"]) simp
  1530   hence "y* real c + ?N x e \<noteq> 0" by (simp add: algebra_simps)
  1531   thus ?case using numbound0_I[OF nb, where bs="bs" and b="x" and b'="y"] 
  1532     by (simp add: algebra_simps)
  1533 qed (auto simp add: numbound0_I[where bs="bs" and b="y" and b'="x"])
  1534 
  1535 lemma finite_set_intervals:
  1536   assumes px: "P (x::real)" 
  1537   and lx: "l \<le> x" and xu: "x \<le> u"
  1538   and linS: "l\<in> S" and uinS: "u \<in> S"
  1539   and fS:"finite S" and lS: "\<forall> x\<in> S. l \<le> x" and Su: "\<forall> x\<in> S. x \<le> u"
  1540   shows "\<exists> a \<in> S. \<exists> b \<in> S. (\<forall> y. a < y \<and> y < b \<longrightarrow> y \<notin> S) \<and> a \<le> x \<and> x \<le> b \<and> P x"
  1541 proof-
  1542   let ?Mx = "{y. y\<in> S \<and> y \<le> x}"
  1543   let ?xM = "{y. y\<in> S \<and> x \<le> y}"
  1544   let ?a = "Max ?Mx"
  1545   let ?b = "Min ?xM"
  1546   have MxS: "?Mx \<subseteq> S" by blast
  1547   hence fMx: "finite ?Mx" using fS finite_subset by auto
  1548   from lx linS have linMx: "l \<in> ?Mx" by blast
  1549   hence Mxne: "?Mx \<noteq> {}" by blast
  1550   have xMS: "?xM \<subseteq> S" by blast
  1551   hence fxM: "finite ?xM" using fS finite_subset by auto
  1552   from xu uinS have linxM: "u \<in> ?xM" by blast
  1553   hence xMne: "?xM \<noteq> {}" by blast
  1554   have ax:"?a \<le> x" using Mxne fMx by auto
  1555   have xb:"x \<le> ?b" using xMne fxM by auto
  1556   have "?a \<in> ?Mx" using Max_in[OF fMx Mxne] by simp hence ainS: "?a \<in> S" using MxS by blast
  1557   have "?b \<in> ?xM" using Min_in[OF fxM xMne] by simp hence binS: "?b \<in> S" using xMS by blast
  1558   have noy:"\<forall> y. ?a < y \<and> y < ?b \<longrightarrow> y \<notin> S"
  1559   proof(clarsimp)
  1560     fix y
  1561     assume ay: "?a < y" and yb: "y < ?b" and yS: "y \<in> S"
  1562     from yS have "y\<in> ?Mx \<or> y\<in> ?xM" by auto
  1563     moreover {assume "y \<in> ?Mx" hence "y \<le> ?a" using Mxne fMx by auto with ay have "False" by simp}
  1564     moreover {assume "y \<in> ?xM" hence "y \<ge> ?b" using xMne fxM by auto with yb have "False" by simp}
  1565     ultimately show "False" by blast
  1566   qed
  1567   from ainS binS noy ax xb px show ?thesis by blast
  1568 qed
  1569 
  1570 lemma rinf_uset:
  1571   assumes lp: "isrlfm p"
  1572   and nmi: "\<not> (Ifm (x#bs) (minusinf p))" (is "\<not> (Ifm (x#bs) (?M p))")
  1573   and npi: "\<not> (Ifm (x#bs) (plusinf p))" (is "\<not> (Ifm (x#bs) (?P p))")
  1574   and ex: "\<exists> x.  Ifm (x#bs) p" (is "\<exists> x. ?I x p")
  1575   shows "\<exists> (l,n) \<in> set (uset p). \<exists> (s,m) \<in> set (uset p). ?I ((Inum (x#bs) l / real n + Inum (x#bs) s / real m) / 2) p" 
  1576 proof-
  1577   let ?N = "\<lambda> x t. Inum (x#bs) t"
  1578   let ?U = "set (uset p)"
  1579   from ex obtain a where pa: "?I a p" by blast
  1580   from bound0_I[OF rminusinf_bound0[OF lp], where bs="bs" and b="x" and b'="a"] nmi
  1581   have nmi': "\<not> (?I a (?M p))" by simp
  1582   from bound0_I[OF rplusinf_bound0[OF lp], where bs="bs" and b="x" and b'="a"] npi
  1583   have npi': "\<not> (?I a (?P p))" by simp
  1584   have "\<exists> (l,n) \<in> set (uset p). \<exists> (s,m) \<in> set (uset p). ?I ((?N a l/real n + ?N a s /real m) / 2) p"
  1585   proof-
  1586     let ?M = "(\<lambda> (t,c). ?N a t / real c) ` ?U"
  1587     have fM: "finite ?M" by auto
  1588     from rminusinf_uset[OF lp nmi pa] rplusinf_uset[OF lp npi pa] 
  1589     have "\<exists> (l,n) \<in> set (uset p). \<exists> (s,m) \<in> set (uset p). a \<le> ?N x l / real n \<and> a \<ge> ?N x s / real m" by blast
  1590     then obtain "t" "n" "s" "m" where 
  1591       tnU: "(t,n) \<in> ?U" and smU: "(s,m) \<in> ?U" 
  1592       and xs1: "a \<le> ?N x s / real m" and tx1: "a \<ge> ?N x t / real n" by blast
  1593     from uset_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 m" and tx: "a \<ge> ?N a t / real n" by auto
  1594     from tnU have Mne: "?M \<noteq> {}" by auto
  1595     hence Une: "?U \<noteq> {}" by simp
  1596     let ?l = "Min ?M"
  1597     let ?u = "Max ?M"
  1598     have linM: "?l \<in> ?M" using fM Mne by simp
  1599     have uinM: "?u \<in> ?M" using fM Mne by simp
  1600     have tnM: "?N a t / real n \<in> ?M" using tnU by auto
  1601     have smM: "?N a s / real m \<in> ?M" using smU by auto 
  1602     have lM: "\<forall> t\<in> ?M. ?l \<le> t" using Mne fM by auto
  1603     have Mu: "\<forall> t\<in> ?M. t \<le> ?u" using Mne fM by auto
  1604     have "?l \<le> ?N a t / real n" using tnM Mne by simp hence lx: "?l \<le> a" using tx by simp
  1605     have "?N a s / real m \<le> ?u" using smM Mne by simp hence xu: "a \<le> ?u" using xs by simp
  1606     from finite_set_intervals2[where P="\<lambda> x. ?I x p",OF pa lx xu linM uinM fM lM Mu]
  1607     have "(\<exists> s\<in> ?M. ?I s p) \<or> 
  1608       (\<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)" .
  1609     moreover { fix u assume um: "u\<in> ?M" and pu: "?I u p"
  1610       hence "\<exists> (tu,nu) \<in> ?U. u = ?N a tu / real nu" by auto
  1611       then obtain "tu" "nu" where tuU: "(tu,nu) \<in> ?U" and tuu:"u= ?N a tu / real nu" by blast
  1612       have "(u + u) / 2 = u" by auto with pu tuu 
  1613       have "?I (((?N a tu / real nu) + (?N a tu / real nu)) / 2) p" by simp
  1614       with tuU have ?thesis by blast}
  1615     moreover{
  1616       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"
  1617       then obtain t1 and t2 where t1M: "t1 \<in> ?M" and t2M: "t2\<in> ?M" 
  1618         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"
  1619         by blast
  1620       from t1M have "\<exists> (t1u,t1n) \<in> ?U. t1 = ?N a t1u / real t1n" by auto
  1621       then obtain "t1u" "t1n" where t1uU: "(t1u,t1n) \<in> ?U" and t1u: "t1 = ?N a t1u / real t1n" by blast
  1622       from t2M have "\<exists> (t2u,t2n) \<in> ?U. t2 = ?N a t2u / real t2n" by auto
  1623       then obtain "t2u" "t2n" where t2uU: "(t2u,t2n) \<in> ?U" and t2u: "t2 = ?N a t2u / real t2n" by blast
  1624       from t1x xt2 have t1t2: "t1 < t2" by simp
  1625       let ?u = "(t1 + t2) / 2"
  1626       from less_half_sum[OF t1t2] gt_half_sum[OF t1t2] have t1lu: "t1 < ?u" and ut2: "?u < t2" by auto
  1627       from lin_dense[OF lp noM t1x xt2 px t1lu ut2] have "?I ?u p" .
  1628       with t1uU t2uU t1u t2u have ?thesis by blast}
  1629     ultimately show ?thesis by blast
  1630   qed
  1631   then obtain "l" "n" "s"  "m" where lnU: "(l,n) \<in> ?U" and smU:"(s,m) \<in> ?U" 
  1632     and pu: "?I ((?N a l / real n + ?N a s / real m) / 2) p" by blast
  1633   from lnU smU uset_l[OF lp] have nbl: "numbound0 l" and nbs: "numbound0 s" by auto
  1634   from numbound0_I[OF nbl, where bs="bs" and b="a" and b'="x"] 
  1635     numbound0_I[OF nbs, where bs="bs" and b="a" and b'="x"] pu
  1636   have "?I ((?N x l / real n + ?N x s / real m) / 2) p" by simp
  1637   with lnU smU
  1638   show ?thesis by auto
  1639 qed
  1640     (* The Ferrante - Rackoff Theorem *)
  1641 
  1642 theorem fr_eq: 
  1643   assumes lp: "isrlfm p"
  1644   shows "(\<exists> x. Ifm (x#bs) p) = ((Ifm (x#bs) (minusinf p)) \<or> (Ifm (x#bs) (plusinf p)) \<or> (\<exists> (t,n) \<in> set (uset p). \<exists> (s,m) \<in> set (uset p). Ifm ((((Inum (x#bs) t)/  real n + (Inum (x#bs) s) / real m) /2)#bs) p))"
  1645   (is "(\<exists> x. ?I x p) = (?M \<or> ?P \<or> ?F)" is "?E = ?D")
  1646 proof
  1647   assume px: "\<exists> x. ?I x p"
  1648   have "?M \<or> ?P \<or> (\<not> ?M \<and> \<not> ?P)" by blast
  1649   moreover {assume "?M \<or> ?P" hence "?D" by blast}
  1650   moreover {assume nmi: "\<not> ?M" and npi: "\<not> ?P"
  1651     from rinf_uset[OF lp nmi npi] have "?F" using px by blast hence "?D" by blast}
  1652   ultimately show "?D" by blast
  1653 next
  1654   assume "?D" 
  1655   moreover {assume m:"?M" from rminusinf_ex[OF lp m] have "?E" .}
  1656   moreover {assume p: "?P" from rplusinf_ex[OF lp p] have "?E" . }
  1657   moreover {assume f:"?F" hence "?E" by blast}
  1658   ultimately show "?E" by blast
  1659 qed
  1660 
  1661 
  1662 lemma fr_equsubst: 
  1663   assumes lp: "isrlfm p"
  1664   shows "(\<exists> x. Ifm (x#bs) p) = ((Ifm (x#bs) (minusinf p)) \<or> (Ifm (x#bs) (plusinf p)) \<or> (\<exists> (t,k) \<in> set (uset p). \<exists> (s,l) \<in> set (uset p). Ifm (x#bs) (usubst p (Add(Mul l t) (Mul k s) , 2*k*l))))"
  1665   (is "(\<exists> x. ?I x p) = (?M \<or> ?P \<or> ?F)" is "?E = ?D")
  1666 proof
  1667   assume px: "\<exists> x. ?I x p"
  1668   have "?M \<or> ?P \<or> (\<not> ?M \<and> \<not> ?P)" by blast
  1669   moreover {assume "?M \<or> ?P" hence "?D" by blast}
  1670   moreover {assume nmi: "\<not> ?M" and npi: "\<not> ?P"
  1671     let ?f ="\<lambda> (t,n). Inum (x#bs) t / real n"
  1672     let ?N = "\<lambda> t. Inum (x#bs) t"
  1673     {fix t n s m assume "(t,n)\<in> set (uset p)" and "(s,m) \<in> set (uset p)"
  1674       with uset_l[OF lp] have tnb: "numbound0 t" and np:"real n > 0" and snb: "numbound0 s" and mp:"real m > 0"
  1675         by auto
  1676       let ?st = "Add (Mul m t) (Mul n s)"
  1677       from mult_pos_pos[OF np mp] have mnp: "real (2*n*m) > 0" 
  1678         by (simp add: mult_commute)
  1679       from tnb snb have st_nb: "numbound0 ?st" by simp
  1680       have st: "(?N t / real n + ?N s / real m)/2 = ?N ?st / real (2*n*m)"
  1681         using mnp mp np by (simp add: algebra_simps add_divide_distrib)
  1682       from usubst_I[OF lp mnp st_nb, where x="x" and bs="bs"] 
  1683       have "?I x (usubst p (?st,2*n*m)) = ?I ((?N t / real n + ?N s / real m) /2) p" by (simp only: st[symmetric])}
  1684     with rinf_uset[OF lp nmi npi px] have "?F" by blast hence "?D" by blast}
  1685   ultimately show "?D" by blast
  1686 next
  1687   assume "?D" 
  1688   moreover {assume m:"?M" from rminusinf_ex[OF lp m] have "?E" .}
  1689   moreover {assume p: "?P" from rplusinf_ex[OF lp p] have "?E" . }
  1690   moreover {fix t k s l assume "(t,k) \<in> set (uset p)" and "(s,l) \<in> set (uset p)" 
  1691     and px:"?I x (usubst p (Add (Mul l t) (Mul k s), 2*k*l))"
  1692     with uset_l[OF lp] have tnb: "numbound0 t" and np:"real k > 0" and snb: "numbound0 s" and mp:"real l > 0" by auto
  1693     let ?st = "Add (Mul l t) (Mul k s)"
  1694     from mult_pos_pos[OF np mp] have mnp: "real (2*k*l) > 0" 
  1695       by (simp add: mult_commute)
  1696     from tnb snb have st_nb: "numbound0 ?st" by simp
  1697     from usubst_I[OF lp mnp st_nb, where bs="bs"] px have "?E" by auto}
  1698   ultimately show "?E" by blast
  1699 qed
  1700 
  1701 
  1702     (* Implement the right hand side of Ferrante and Rackoff's Theorem. *)
  1703 definition ferrack :: "fm \<Rightarrow> fm" where
  1704   "ferrack p = (let p' = rlfm (simpfm p); mp = minusinf p'; pp = plusinf p'
  1705                 in if (mp = T \<or> pp = T) then T else 
  1706                    (let U = remdups(map simp_num_pair 
  1707                      (map (\<lambda> ((t,n),(s,m)). (Add (Mul m t) (Mul n s) , 2*n*m))
  1708                            (alluopairs (uset p')))) 
  1709                     in decr (disj mp (disj pp (evaldjf (simpfm o (usubst p')) U)))))"
  1710 
  1711 lemma uset_cong_aux:
  1712   assumes Ul: "\<forall> (t,n) \<in> set U. numbound0 t \<and> n >0"
  1713   shows "((\<lambda> (t,n). Inum (x#bs) t /real 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 n + Inum (x#bs) s /real m)/2) ` (set U \<times> set U))"
  1714   (is "?lhs = ?rhs")
  1715 proof(auto)
  1716   fix t n s m
  1717   assume "((t,n),(s,m)) \<in> set (alluopairs U)"
  1718   hence th: "((t,n),(s,m)) \<in> (set U \<times> set U)"
  1719     using alluopairs_set1[where xs="U"] by blast
  1720   let ?N = "\<lambda> t. Inum (x#bs) t"
  1721   let ?st= "Add (Mul m t) (Mul n s)"
  1722   from Ul th have mnz: "m \<noteq> 0" by auto
  1723   from Ul th have  nnz: "n \<noteq> 0" by auto  
  1724   have st: "(?N t / real n + ?N s / real m)/2 = ?N ?st / real (2*n*m)"
  1725    using mnz nnz by (simp add: algebra_simps add_divide_distrib)
  1726  
  1727   thus "(real m *  Inum (x # bs) t + real n * Inum (x # bs) s) /
  1728        (2 * real n * real m)
  1729        \<in> (\<lambda>((t, n), s, m).
  1730              (Inum (x # bs) t / real n + Inum (x # bs) s / real m) / 2) `
  1731          (set U \<times> set U)"using mnz nnz th  
  1732     apply (auto simp add: th add_divide_distrib algebra_simps split_def image_def)
  1733     by (rule_tac x="(s,m)" in bexI,simp_all) 
  1734   (rule_tac x="(t,n)" in bexI,simp_all add: mult_commute)
  1735 next
  1736   fix t n s m
  1737   assume tnU: "(t,n) \<in> set U" and smU:"(s,m) \<in> set U" 
  1738   let ?N = "\<lambda> t. Inum (x#bs) t"
  1739   let ?st= "Add (Mul m t) (Mul n s)"
  1740   from Ul smU have mnz: "m \<noteq> 0" by auto
  1741   from Ul tnU have  nnz: "n \<noteq> 0" by auto  
  1742   have st: "(?N t / real n + ?N s / real m)/2 = ?N ?st / real (2*n*m)"
  1743    using mnz nnz by (simp add: algebra_simps add_divide_distrib)
  1744  let ?P = "\<lambda> (t',n') (s',m'). (Inum (x # bs) t / real n + Inum (x # bs) s / real m)/2 = (Inum (x # bs) t' / real n' + Inum (x # bs) s' / real m')/2"
  1745  have Pc:"\<forall> a b. ?P a b = ?P b a"
  1746    by auto
  1747  from Ul alluopairs_set1 have Up:"\<forall> ((t,n),(s,m)) \<in> set (alluopairs U). n \<noteq> 0 \<and> m \<noteq> 0" by blast
  1748  from alluopairs_ex[OF Pc, where xs="U"] tnU smU
  1749  have th':"\<exists> ((t',n'),(s',m')) \<in> set (alluopairs U). ?P (t',n') (s',m')"
  1750    by blast
  1751  then obtain t' n' s' m' where ts'_U: "((t',n'),(s',m')) \<in> set (alluopairs U)" 
  1752    and Pts': "?P (t',n') (s',m')" by blast
  1753  from ts'_U Up have mnz': "m' \<noteq> 0" and nnz': "n'\<noteq> 0" by auto
  1754  let ?st' = "Add (Mul m' t') (Mul n' s')"
  1755    have st': "(?N t' / real n' + ?N s' / real m')/2 = ?N ?st' / real (2*n'*m')"
  1756    using mnz' nnz' by (simp add: algebra_simps add_divide_distrib)
  1757  from Pts' have 
  1758    "(Inum (x # bs) t / real n + Inum (x # bs) s / real m)/2 = (Inum (x # bs) t' / real n' + Inum (x # bs) s' / real m')/2" by simp
  1759  also have "\<dots> = ((\<lambda>(t, n). Inum (x # bs) t / real n) ((\<lambda>((t, n), s, m). (Add (Mul m t) (Mul n s), 2 * n * m)) ((t',n'),(s',m'))))" by (simp add: st')
  1760  finally show "(Inum (x # bs) t / real n + Inum (x # bs) s / real m) / 2
  1761           \<in> (\<lambda>(t, n). Inum (x # bs) t / real n) `
  1762             (\<lambda>((t, n), s, m). (Add (Mul m t) (Mul n s), 2 * n * m)) `
  1763             set (alluopairs U)"
  1764    using ts'_U by blast
  1765 qed
  1766 
  1767 lemma uset_cong:
  1768   assumes lp: "isrlfm p"
  1769   and UU': "((\<lambda> (t,n). Inum (x#bs) t /real n) ` U') = ((\<lambda> ((t,n),(s,m)). (Inum (x#bs) t /real n + Inum (x#bs) s /real m)/2) ` (U \<times> U))" (is "?f ` U' = ?g ` (U\<times>U)")
  1770   and U: "\<forall> (t,n) \<in> U. numbound0 t \<and> n > 0"
  1771   and U': "\<forall> (t,n) \<in> U'. numbound0 t \<and> n > 0"
  1772   shows "(\<exists> (t,n) \<in> U. \<exists> (s,m) \<in> U. Ifm (x#bs) (usubst p (Add (Mul m t) (Mul n s),2*n*m))) = (\<exists> (t,n) \<in> U'. Ifm (x#bs) (usubst p (t,n)))"
  1773   (is "?lhs = ?rhs")
  1774 proof
  1775   assume ?lhs
  1776   then obtain t n s m where tnU: "(t,n) \<in> U" and smU:"(s,m) \<in> U" and 
  1777     Pst: "Ifm (x#bs) (usubst p (Add (Mul m t) (Mul n s),2*n*m))" by blast
  1778   let ?N = "\<lambda> t. Inum (x#bs) t"
  1779   from tnU smU U have tnb: "numbound0 t" and np: "n > 0" 
  1780     and snb: "numbound0 s" and mp:"m > 0"  by auto
  1781   let ?st= "Add (Mul m t) (Mul n s)"
  1782   from mult_pos_pos[OF np mp] have mnp: "real (2*n*m) > 0" 
  1783       by (simp add: mult_commute real_of_int_mult[symmetric] del: real_of_int_mult)
  1784     from tnb snb have stnb: "numbound0 ?st" by simp
  1785   have st: "(?N t / real n + ?N s / real m)/2 = ?N ?st / real (2*n*m)"
  1786    using mp np by (simp add: algebra_simps add_divide_distrib)
  1787   from tnU smU UU' have "?g ((t,n),(s,m)) \<in> ?f ` U'" by blast
  1788   hence "\<exists> (t',n') \<in> U'. ?g ((t,n),(s,m)) = ?f (t',n')"
  1789     by auto (rule_tac x="(a,b)" in bexI, auto)
  1790   then obtain t' n' where tnU': "(t',n') \<in> U'" and th: "?g ((t,n),(s,m)) = ?f (t',n')" by blast
  1791   from U' tnU' have tnb': "numbound0 t'" and np': "real n' > 0" by auto
  1792   from usubst_I[OF lp mnp stnb, where bs="bs" and x="x"] Pst 
  1793   have Pst2: "Ifm (Inum (x # bs) (Add (Mul m t) (Mul n s)) / real (2 * n * m) # bs) p" by simp
  1794   from conjunct1[OF usubst_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]]
  1795   have "Ifm (x # bs) (usubst p (t', n')) " by (simp only: st) 
  1796   then show ?rhs using tnU' by auto 
  1797 next
  1798   assume ?rhs
  1799   then obtain t' n' where tnU': "(t',n') \<in> U'" and Pt': "Ifm (x # bs) (usubst p (t', n'))" 
  1800     by blast
  1801   from tnU' UU' have "?f (t',n') \<in> ?g ` (U\<times>U)" by blast
  1802   hence "\<exists> ((t,n),(s,m)) \<in> (U\<times>U). ?f (t',n') = ?g ((t,n),(s,m))" 
  1803     by auto (rule_tac x="(a,b)" in bexI, auto)
  1804   then obtain t n s m where tnU: "(t,n) \<in> U" and smU:"(s,m) \<in> U" and 
  1805     th: "?f (t',n') = ?g((t,n),(s,m)) "by blast
  1806     let ?N = "\<lambda> t. Inum (x#bs) t"
  1807   from tnU smU U have tnb: "numbound0 t" and np: "n > 0" 
  1808     and snb: "numbound0 s" and mp:"m > 0"  by auto
  1809   let ?st= "Add (Mul m t) (Mul n s)"
  1810   from mult_pos_pos[OF np mp] have mnp: "real (2*n*m) > 0" 
  1811       by (simp add: mult_commute real_of_int_mult[symmetric] del: real_of_int_mult)
  1812     from tnb snb have stnb: "numbound0 ?st" by simp
  1813   have st: "(?N t / real n + ?N s / real m)/2 = ?N ?st / real (2*n*m)"
  1814    using mp np by (simp add: algebra_simps add_divide_distrib)
  1815   from U' tnU' have tnb': "numbound0 t'" and np': "real n' > 0" by auto
  1816   from usubst_I[OF lp np' tnb', where bs="bs" and x="x",simplified th[simplified split_def fst_conv snd_conv] st] Pt'
  1817   have Pst2: "Ifm (Inum (x # bs) (Add (Mul m t) (Mul n s)) / real (2 * n * m) # bs) p" by simp
  1818   with usubst_I[OF lp mnp stnb, where x="x" and bs="bs"] tnU smU show ?lhs by blast
  1819 qed
  1820 
  1821 lemma ferrack:
  1822   assumes qf: "qfree p"
  1823   shows "qfree (ferrack p) \<and> ((Ifm bs (ferrack p)) = (\<exists> x. Ifm (x#bs) p))"
  1824   (is "_ \<and> (?rhs = ?lhs)")
  1825 proof-
  1826   let ?I = "\<lambda> x p. Ifm (x#bs) p"
  1827   fix x
  1828   let ?N = "\<lambda> t. Inum (x#bs) t"
  1829   let ?q = "rlfm (simpfm p)" 
  1830   let ?U = "uset ?q"
  1831   let ?Up = "alluopairs ?U"
  1832   let ?g = "\<lambda> ((t,n),(s,m)). (Add (Mul m t) (Mul n s) , 2*n*m)"
  1833   let ?S = "map ?g ?Up"
  1834   let ?SS = "map simp_num_pair ?S"
  1835   let ?Y = "remdups ?SS"
  1836   let ?f= "(\<lambda> (t,n). ?N t / real n)"
  1837   let ?h = "\<lambda> ((t,n),(s,m)). (?N t/real n + ?N s/ real m) /2"
  1838   let ?F = "\<lambda> p. \<exists> a \<in> set (uset p). \<exists> b \<in> set (uset p). ?I x (usubst p (?g(a,b)))"
  1839   let ?ep = "evaldjf (simpfm o (usubst ?q)) ?Y"
  1840   from rlfm_I[OF simpfm_qf[OF qf]] have lq: "isrlfm ?q" by blast
  1841   from alluopairs_set1[where xs="?U"] have UpU: "set ?Up \<le> (set ?U \<times> set ?U)" by simp
  1842   from uset_l[OF lq] have U_l: "\<forall> (t,n) \<in> set ?U. numbound0 t \<and> n > 0" .
  1843   from U_l UpU 
  1844   have "\<forall> ((t,n),(s,m)) \<in> set ?Up. numbound0 t \<and> n> 0 \<and> numbound0 s \<and> m > 0" by auto
  1845   hence Snb: "\<forall> (t,n) \<in> set ?S. numbound0 t \<and> n > 0 "
  1846     by (auto simp add: mult_pos_pos)
  1847   have Y_l: "\<forall> (t,n) \<in> set ?Y. numbound0 t \<and> n > 0" 
  1848   proof-
  1849     { fix t n assume tnY: "(t,n) \<in> set ?Y" 
  1850       hence "(t,n) \<in> set ?SS" by simp
  1851       hence "\<exists> (t',n') \<in> set ?S. simp_num_pair (t',n') = (t,n)"
  1852         by (auto simp add: split_def simp del: map_map)
  1853            (rule_tac x="((aa,ba),(ab,bb))" in bexI, simp_all)
  1854       then obtain t' n' where tn'S: "(t',n') \<in> set ?S" and tns: "simp_num_pair (t',n') = (t,n)" by blast
  1855       from tn'S Snb have tnb: "numbound0 t'" and np: "n' > 0" by auto
  1856       from simp_num_pair_l[OF tnb np tns]
  1857       have "numbound0 t \<and> n > 0" . }
  1858     thus ?thesis by blast
  1859   qed
  1860 
  1861   have YU: "(?f ` set ?Y) = (?h ` (set ?U \<times> set ?U))"
  1862   proof-
  1863      from simp_num_pair_ci[where bs="x#bs"] have 
  1864     "\<forall>x. (?f o simp_num_pair) x = ?f x" by auto
  1865      hence th: "?f o simp_num_pair = ?f" using ext by blast
  1866     have "(?f ` set ?Y) = ((?f o simp_num_pair) ` set ?S)" by (simp add: image_compose)
  1867     also have "\<dots> = (?f ` set ?S)" by (simp add: th)
  1868     also have "\<dots> = ((?f o ?g) ` set ?Up)" 
  1869       by (simp only: set_map o_def image_compose[symmetric])
  1870     also have "\<dots> = (?h ` (set ?U \<times> set ?U))"
  1871       using uset_cong_aux[OF U_l, where x="x" and bs="bs", simplified set_map image_compose[symmetric]] by blast
  1872     finally show ?thesis .
  1873   qed
  1874   have "\<forall> (t,n) \<in> set ?Y. bound0 (simpfm (usubst ?q (t,n)))"
  1875   proof-
  1876     { fix t n assume tnY: "(t,n) \<in> set ?Y"
  1877       with Y_l have tnb: "numbound0 t" and np: "real n > 0" by auto
  1878       from usubst_I[OF lq np tnb]
  1879     have "bound0 (usubst ?q (t,n))"  by simp hence "bound0 (simpfm (usubst ?q (t,n)))" 
  1880       using simpfm_bound0 by simp}
  1881     thus ?thesis by blast
  1882   qed
  1883   hence ep_nb: "bound0 ?ep"  using evaldjf_bound0[where xs="?Y" and f="simpfm o (usubst ?q)"] by auto
  1884   let ?mp = "minusinf ?q"
  1885   let ?pp = "plusinf ?q"
  1886   let ?M = "?I x ?mp"
  1887   let ?P = "?I x ?pp"
  1888   let ?res = "disj ?mp (disj ?pp ?ep)"
  1889   from rminusinf_bound0[OF lq] rplusinf_bound0[OF lq] ep_nb
  1890   have nbth: "bound0 ?res" by auto
  1891 
  1892   from conjunct1[OF rlfm_I[OF simpfm_qf[OF qf]]] simpfm  
  1893 
  1894   have th: "?lhs = (\<exists> x. ?I x ?q)" by auto 
  1895   from th fr_equsubst[OF lq, where bs="bs" and x="x"] have lhfr: "?lhs = (?M \<or> ?P \<or> ?F ?q)"
  1896     by (simp only: split_def fst_conv snd_conv)
  1897   also have "\<dots> = (?M \<or> ?P \<or> (\<exists> (t,n) \<in> set ?Y. ?I x (simpfm (usubst ?q (t,n)))))" 
  1898     using uset_cong[OF lq YU U_l Y_l]  by (simp only: split_def fst_conv snd_conv simpfm) 
  1899   also have "\<dots> = (Ifm (x#bs) ?res)"
  1900     using evaldjf_ex[where ps="?Y" and bs = "x#bs" and f="simpfm o (usubst ?q)",symmetric]
  1901     by (simp add: split_def pair_collapse)
  1902   finally have lheq: "?lhs =  (Ifm bs (decr ?res))" using decr[OF nbth] by blast
  1903   hence lr: "?lhs = ?rhs" apply (unfold ferrack_def Let_def)
  1904     by (cases "?mp = T \<or> ?pp = T", auto) (simp add: disj_def)+
  1905   from decr_qf[OF nbth] have "qfree (ferrack p)" by (auto simp add: Let_def ferrack_def)
  1906   with lr show ?thesis by blast
  1907 qed
  1908 
  1909 definition linrqe:: "fm \<Rightarrow> fm" where
  1910   "linrqe p = qelim (prep p) ferrack"
  1911 
  1912 theorem linrqe: "Ifm bs (linrqe p) = Ifm bs p \<and> qfree (linrqe p)"
  1913 using ferrack qelim_ci prep
  1914 unfolding linrqe_def by auto
  1915 
  1916 definition ferrack_test :: "unit \<Rightarrow> fm" where
  1917   "ferrack_test u = linrqe (A (A (Imp (Lt (Sub (Bound 1) (Bound 0)))
  1918     (E (Eq (Sub (Add (Bound 0) (Bound 2)) (Bound 1)))))))"
  1919 
  1920 ML_val {* @{code ferrack_test} () *}
  1921 
  1922 oracle linr_oracle = {*
  1923 let
  1924 
  1925 val mk_C = @{code C} o @{code int_of_integer};
  1926 val mk_Bound = @{code Bound} o @{code nat_of_integer};
  1927 
  1928 fun num_of_term vs (Free vT) = mk_Bound (find_index (fn vT' => vT = vT') vs)
  1929   | num_of_term vs @{term "real (0::int)"} = mk_C 0
  1930   | num_of_term vs @{term "real (1::int)"} = mk_C 1
  1931   | num_of_term vs @{term "0::real"} = mk_C 0
  1932   | num_of_term vs @{term "1::real"} = mk_C 1
  1933   | num_of_term vs (Bound i) = mk_Bound i
  1934   | num_of_term vs (@{term "uminus :: real \<Rightarrow> real"} $ t') = @{code Neg} (num_of_term vs t')
  1935   | num_of_term vs (@{term "op + :: real \<Rightarrow> real \<Rightarrow> real"} $ t1 $ t2) =
  1936      @{code Add} (num_of_term vs t1, num_of_term vs t2)
  1937   | num_of_term vs (@{term "op - :: real \<Rightarrow> real \<Rightarrow> real"} $ t1 $ t2) =
  1938      @{code Sub} (num_of_term vs t1, num_of_term vs t2)
  1939   | num_of_term vs (@{term "op * :: real \<Rightarrow> real \<Rightarrow> real"} $ t1 $ t2) = (case num_of_term vs t1
  1940      of @{code C} i => @{code Mul} (i, num_of_term vs t2)
  1941       | _ => error "num_of_term: unsupported multiplication")
  1942   | num_of_term vs (@{term "real :: int \<Rightarrow> real"} $ t') =
  1943      (mk_C (snd (HOLogic.dest_number t'))
  1944        handle TERM _ => error ("num_of_term: unknown term"))
  1945   | num_of_term vs t' =
  1946      (mk_C (snd (HOLogic.dest_number t'))
  1947        handle TERM _ => error ("num_of_term: unknown term"));
  1948 
  1949 fun fm_of_term vs @{term True} = @{code T}
  1950   | fm_of_term vs @{term False} = @{code F}
  1951   | fm_of_term vs (@{term "op < :: real \<Rightarrow> real \<Rightarrow> bool"} $ t1 $ t2) =
  1952       @{code Lt} (@{code Sub} (num_of_term vs t1, num_of_term vs t2))
  1953   | fm_of_term vs (@{term "op \<le> :: real \<Rightarrow> real \<Rightarrow> bool"} $ t1 $ t2) =
  1954       @{code Le} (@{code Sub} (num_of_term vs t1, num_of_term vs t2))
  1955   | fm_of_term vs (@{term "op = :: real \<Rightarrow> real \<Rightarrow> bool"} $ t1 $ t2) =
  1956       @{code Eq} (@{code Sub} (num_of_term vs t1, num_of_term vs t2)) 
  1957   | fm_of_term vs (@{term "op \<longleftrightarrow> :: bool \<Rightarrow> bool \<Rightarrow> bool"} $ t1 $ t2) =
  1958       @{code Iff} (fm_of_term vs t1, fm_of_term vs t2)
  1959   | fm_of_term vs (@{term HOL.conj} $ t1 $ t2) = @{code And} (fm_of_term vs t1, fm_of_term vs t2)
  1960   | fm_of_term vs (@{term HOL.disj} $ t1 $ t2) = @{code Or} (fm_of_term vs t1, fm_of_term vs t2)
  1961   | fm_of_term vs (@{term HOL.implies} $ t1 $ t2) = @{code Imp} (fm_of_term vs t1, fm_of_term vs t2)
  1962   | fm_of_term vs (@{term "Not"} $ t') = @{code NOT} (fm_of_term vs t')
  1963   | fm_of_term vs (Const (@{const_name Ex}, _) $ Abs (xn, xT, p)) =
  1964       @{code E} (fm_of_term (("", dummyT) :: vs) p)
  1965   | fm_of_term vs (Const (@{const_name All}, _) $ Abs (xn, xT, p)) =
  1966       @{code A} (fm_of_term (("", dummyT) ::  vs) p)
  1967   | fm_of_term vs t = error ("fm_of_term : unknown term " ^ Syntax.string_of_term @{context} t);
  1968 
  1969 fun term_of_num vs (@{code C} i) = @{term "real :: int \<Rightarrow> real"} $
  1970       HOLogic.mk_number HOLogic.intT (@{code integer_of_int} i)
  1971   | term_of_num vs (@{code Bound} n) = Free (nth vs (@{code integer_of_nat} n))
  1972   | term_of_num vs (@{code Neg} t') = @{term "uminus :: real \<Rightarrow> real"} $ term_of_num vs t'
  1973   | term_of_num vs (@{code Add} (t1, t2)) = @{term "op + :: real \<Rightarrow> real \<Rightarrow> real"} $
  1974       term_of_num vs t1 $ term_of_num vs t2
  1975   | term_of_num vs (@{code Sub} (t1, t2)) = @{term "op - :: real \<Rightarrow> real \<Rightarrow> real"} $
  1976       term_of_num vs t1 $ term_of_num vs t2
  1977   | term_of_num vs (@{code Mul} (i, t2)) = @{term "op * :: real \<Rightarrow> real \<Rightarrow> real"} $
  1978       term_of_num vs (@{code C} i) $ term_of_num vs t2
  1979   | term_of_num vs (@{code CN} (n, i, t)) = term_of_num vs (@{code Add} (@{code Mul} (i, @{code Bound} n), t));
  1980 
  1981 fun term_of_fm vs @{code T} = @{term True} 
  1982   | term_of_fm vs @{code F} = @{term False}
  1983   | term_of_fm vs (@{code Lt} t) = @{term "op < :: real \<Rightarrow> real \<Rightarrow> bool"} $
  1984       term_of_num vs t $ @{term "0::real"}
  1985   | term_of_fm vs (@{code Le} t) = @{term "op \<le> :: real \<Rightarrow> real \<Rightarrow> bool"} $
  1986       term_of_num vs t $ @{term "0::real"}
  1987   | term_of_fm vs (@{code Gt} t) = @{term "op < :: real \<Rightarrow> real \<Rightarrow> bool"} $
  1988       @{term "0::real"} $ term_of_num vs t
  1989   | term_of_fm vs (@{code Ge} t) = @{term "op \<le> :: real \<Rightarrow> real \<Rightarrow> bool"} $
  1990       @{term "0::real"} $ term_of_num vs t
  1991   | term_of_fm vs (@{code Eq} t) = @{term "op = :: real \<Rightarrow> real \<Rightarrow> bool"} $
  1992       term_of_num vs t $ @{term "0::real"}
  1993   | term_of_fm vs (@{code NEq} t) = term_of_fm vs (@{code NOT} (@{code Eq} t))
  1994   | term_of_fm vs (@{code NOT} t') = HOLogic.Not $ term_of_fm vs t'
  1995   | term_of_fm vs (@{code And} (t1, t2)) = HOLogic.conj $ term_of_fm vs t1 $ term_of_fm vs t2
  1996   | term_of_fm vs (@{code Or} (t1, t2)) = HOLogic.disj $ term_of_fm vs t1 $ term_of_fm vs t2
  1997   | term_of_fm vs (@{code Imp}  (t1, t2)) = HOLogic.imp $ term_of_fm vs t1 $ term_of_fm vs t2
  1998   | term_of_fm vs (@{code Iff} (t1, t2)) = @{term "op \<longleftrightarrow> :: bool \<Rightarrow> bool \<Rightarrow> bool"} $
  1999       term_of_fm vs t1 $ term_of_fm vs t2;
  2000 
  2001 in fn (ctxt, t) =>
  2002   let 
  2003     val vs = Term.add_frees t [];
  2004     val t' = (term_of_fm vs o @{code linrqe} o fm_of_term vs) t;
  2005   in (Thm.cterm_of (Proof_Context.theory_of ctxt) o HOLogic.mk_Trueprop o HOLogic.mk_eq) (t, t') end
  2006 end;
  2007 *}
  2008 
  2009 ML_file "ferrack_tac.ML"
  2010 
  2011 method_setup rferrack = {*
  2012   Args.mode "no_quantify" >>
  2013     (fn q => fn ctxt => SIMPLE_METHOD' (Ferrack_Tac.linr_tac ctxt (not q)))
  2014 *} "decision procedure for linear real arithmetic"
  2015 
  2016 
  2017 lemma
  2018   fixes x :: real
  2019   shows "2 * x \<le> 2 * x \<and> 2 * x \<le> 2 * x + 1"
  2020   by rferrack
  2021 
  2022 lemma
  2023   fixes x :: real
  2024   shows "\<exists>y \<le> x. x = y + 1"
  2025   by rferrack
  2026 
  2027 lemma
  2028   fixes x :: real
  2029   shows "\<not> (\<exists>z. x + z = x + z + 1)"
  2030   by rferrack
  2031 
  2032 end
  2033