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