merged
authorblanchet
Mon, 16 Feb 2009 10:15:43 +0100
changeset 29928 66c5cc1e60a9
parent 29925 17d1e32ef867 (diff)
parent 29927 ae8f42c245b2 (current diff)
child 29929 9e903a645d8f
child 29955 61837a9bdd74
merged
--- a/CONTRIBUTORS	Mon Feb 16 10:13:30 2009 +0100
+++ b/CONTRIBUTORS	Mon Feb 16 10:15:43 2009 +0100
@@ -8,6 +8,9 @@
 --------------------------------------
 
 * February 2008: Timothy Bourke, NICTA
+  New find_consts command.
+
+* February 2008: Timothy Bourke, NICTA
   "solves" criterion for find_theorems and auto_solve option
 
 * December 2008: Clemens Ballarin, TUM
--- a/NEWS	Mon Feb 16 10:13:30 2009 +0100
+++ b/NEWS	Mon Feb 16 10:15:43 2009 +0100
@@ -183,15 +183,26 @@
 * The 'axiomatization' command now only works within a global theory
 context.  INCOMPATIBILITY.
 
-* New find_theorems criterion "solves" matching theorems that 
-  directly solve the current goal. Try "find_theorems solves".
+* New find_theorems criterion "solves" matching theorems that
+directly solve the current goal. Try "find_theorems solves".
 
 * Added an auto solve option, which can be enabled through the
-  ProofGeneral Isabelle settings menu (disabled by default).
+ProofGeneral Isabelle settings menu (disabled by default).
  
-  When enabled, find_theorems solves is called whenever a new lemma
-  is stated. Any theorems that could solve the lemma directly are
-  listed underneath the goal.
+When enabled, find_theorems solves is called whenever a new lemma is
+stated. Any theorems that could solve the lemma directly are listed
+underneath the goal.
+
+* New command find_consts searches for constants based on type and name 
+patterns, e.g.
+
+    find_consts "_ => bool"
+
+By default, matching is against subtypes, but it may be restricted to the
+whole type. Searching by name is possible. Multiple queries are conjunctive
+and queries may be negated by prefixing them with a hyphen:
+
+    find_consts strict: "_ => bool" name: "Int" -"int => int"
 
 
 *** Document preparation ***
--- a/doc-src/IsarRef/Thy/Misc.thy	Mon Feb 16 10:13:30 2009 +0100
+++ b/doc-src/IsarRef/Thy/Misc.thy	Mon Feb 16 10:15:43 2009 +0100
@@ -16,6 +16,7 @@
     @{command_def "print_attributes"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
     @{command_def "print_theorems"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
     @{command_def "find_theorems"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
+    @{command_def "find_consts"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
     @{command_def "thm_deps"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
     @{command_def "print_facts"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
     @{command_def "print_binds"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
@@ -25,10 +26,14 @@
     'print\_theory' ( '!'?)
     ;
 
-    'find\_theorems' (('(' (nat)? ('with\_dups')? ')')?) (criterion *)
+    'find\_theorems' (('(' (nat)? ('with\_dups')? ')')?) (thmcriterion *)
     ;
-    criterion: ('-'?) ('name' ':' nameref | 'intro' | 'elim' | 'dest' |
-      'simp' ':' term | term)
+    thmcriterion: ('-'?) ('name' ':' nameref | 'intro' | 'elim' | 'dest' |
+      'solves' | 'simp' ':' term | term)
+    ;
+    'find\_consts' (constcriterion *)
+    ;
+    constcriterion: ('-'?) ('name' ':' nameref | 'strict' ':' type | type)
     ;
     'thm\_deps' thmrefs
     ;
@@ -63,11 +68,13 @@
   contain ``@{text "*"}'' wildcards.  The criteria @{text intro},
   @{text elim}, and @{text dest} select theorems that match the
   current goal as introduction, elimination or destruction rules,
-  respectively.  The criterion @{text "simp: t"} selects all rewrite
-  rules whose left-hand side matches the given term.  The criterion
-  term @{text t} selects all theorems that contain the pattern @{text
-  t} -- as usual, patterns may contain occurrences of the dummy
-  ``@{text _}'', schematic variables, and type constraints.
+  respectively.  The criterion @{text "solves"} returns all rules
+  that would directly solve the current goal.  The criterion
+  @{text "simp: t"} selects all rewrite rules whose left-hand side
+  matches the given term.  The criterion term @{text t} selects all
+  theorems that contain the pattern @{text t} -- as usual, patterns
+  may contain occurrences of the dummy ``@{text _}'', schematic
+  variables, and type constraints.
   
   Criteria can be preceded by ``@{text "-"}'' to select theorems that
   do \emph{not} match. Note that giving the empty list of criteria
@@ -75,7 +82,16 @@
   number of printed facts may be given; the default is 40.  By
   default, duplicates are removed from the search result. Use
   @{text with_dups} to display duplicates.
-  
+
+  \item @{command "find_consts"}~@{text criteria} prints all constants
+  whose type meets all of the given criteria. The criterion @{text
+  "strict: ty"} is met by any type that matches the type pattern
+  @{text ty}.  Patterns may contain both the dummy type ``@{text _}''
+  and sort constraints. The criterion @{text ty} is similar, but it
+  also matches against subtypes. The criterion @{text "name: p"} and
+  the prefix ``@{text "-"}'' function as described for @{command
+  "find_theorems"}.
+
   \item @{command "thm_deps"}~@{text "a\<^sub>1 \<dots> a\<^sub>n"}
   visualizes dependencies of facts, using Isabelle's graph browser
   tool (see also \cite{isabelle-sys}).
--- a/doc-src/IsarRef/Thy/document/Misc.tex	Mon Feb 16 10:13:30 2009 +0100
+++ b/doc-src/IsarRef/Thy/document/Misc.tex	Mon Feb 16 10:15:43 2009 +0100
@@ -36,6 +36,7 @@
     \indexdef{}{command}{print\_attributes}\hypertarget{command.print-attributes}{\hyperlink{command.print-attributes}{\mbox{\isa{\isacommand{print{\isacharunderscore}attributes}}}}}\isa{{\isachardoublequote}\isactrlsup {\isacharasterisk}{\isachardoublequote}} & : & \isa{{\isachardoublequote}context\ {\isasymrightarrow}{\isachardoublequote}} \\
     \indexdef{}{command}{print\_theorems}\hypertarget{command.print-theorems}{\hyperlink{command.print-theorems}{\mbox{\isa{\isacommand{print{\isacharunderscore}theorems}}}}}\isa{{\isachardoublequote}\isactrlsup {\isacharasterisk}{\isachardoublequote}} & : & \isa{{\isachardoublequote}context\ {\isasymrightarrow}{\isachardoublequote}} \\
     \indexdef{}{command}{find\_theorems}\hypertarget{command.find-theorems}{\hyperlink{command.find-theorems}{\mbox{\isa{\isacommand{find{\isacharunderscore}theorems}}}}}\isa{{\isachardoublequote}\isactrlsup {\isacharasterisk}{\isachardoublequote}} & : & \isa{{\isachardoublequote}context\ {\isasymrightarrow}{\isachardoublequote}} \\
+    \indexdef{}{command}{find\_consts}\hypertarget{command.find-consts}{\hyperlink{command.find-consts}{\mbox{\isa{\isacommand{find{\isacharunderscore}consts}}}}}\isa{{\isachardoublequote}\isactrlsup {\isacharasterisk}{\isachardoublequote}} & : & \isa{{\isachardoublequote}context\ {\isasymrightarrow}{\isachardoublequote}} \\
     \indexdef{}{command}{thm\_deps}\hypertarget{command.thm-deps}{\hyperlink{command.thm-deps}{\mbox{\isa{\isacommand{thm{\isacharunderscore}deps}}}}}\isa{{\isachardoublequote}\isactrlsup {\isacharasterisk}{\isachardoublequote}} & : & \isa{{\isachardoublequote}context\ {\isasymrightarrow}{\isachardoublequote}} \\
     \indexdef{}{command}{print\_facts}\hypertarget{command.print-facts}{\hyperlink{command.print-facts}{\mbox{\isa{\isacommand{print{\isacharunderscore}facts}}}}}\isa{{\isachardoublequote}\isactrlsup {\isacharasterisk}{\isachardoublequote}} & : & \isa{{\isachardoublequote}context\ {\isasymrightarrow}{\isachardoublequote}} \\
     \indexdef{}{command}{print\_binds}\hypertarget{command.print-binds}{\hyperlink{command.print-binds}{\mbox{\isa{\isacommand{print{\isacharunderscore}binds}}}}}\isa{{\isachardoublequote}\isactrlsup {\isacharasterisk}{\isachardoublequote}} & : & \isa{{\isachardoublequote}context\ {\isasymrightarrow}{\isachardoublequote}} \\
@@ -45,10 +46,14 @@
     'print\_theory' ( '!'?)
     ;
 
-    'find\_theorems' (('(' (nat)? ('with\_dups')? ')')?) (criterion *)
+    'find\_theorems' (('(' (nat)? ('with\_dups')? ')')?) (thmcriterion *)
     ;
-    criterion: ('-'?) ('name' ':' nameref | 'intro' | 'elim' | 'dest' |
-      'simp' ':' term | term)
+    thmcriterion: ('-'?) ('name' ':' nameref | 'intro' | 'elim' | 'dest' |
+      'solves' | 'simp' ':' term | term)
+    ;
+    'find\_consts' (constcriterion *)
+    ;
+    constcriterion: ('-'?) ('name' ':' nameref | 'strict' ':' type | type)
     ;
     'thm\_deps' thmrefs
     ;
@@ -83,10 +88,13 @@
   contain ``\isa{{\isachardoublequote}{\isacharasterisk}{\isachardoublequote}}'' wildcards.  The criteria \isa{intro},
   \isa{elim}, and \isa{dest} select theorems that match the
   current goal as introduction, elimination or destruction rules,
-  respectively.  The criterion \isa{{\isachardoublequote}simp{\isacharcolon}\ t{\isachardoublequote}} selects all rewrite
-  rules whose left-hand side matches the given term.  The criterion
-  term \isa{t} selects all theorems that contain the pattern \isa{t} -- as usual, patterns may contain occurrences of the dummy
-  ``\isa{{\isacharunderscore}}'', schematic variables, and type constraints.
+  respectively.  The criterion \isa{{\isachardoublequote}solves{\isachardoublequote}} returns all rules
+  that would directly solve the current goal.  The criterion
+  \isa{{\isachardoublequote}simp{\isacharcolon}\ t{\isachardoublequote}} selects all rewrite rules whose left-hand side
+  matches the given term.  The criterion term \isa{t} selects all
+  theorems that contain the pattern \isa{t} -- as usual, patterns
+  may contain occurrences of the dummy ``\isa{{\isacharunderscore}}'', schematic
+  variables, and type constraints.
   
   Criteria can be preceded by ``\isa{{\isachardoublequote}{\isacharminus}{\isachardoublequote}}'' to select theorems that
   do \emph{not} match. Note that giving the empty list of criteria
@@ -94,7 +102,14 @@
   number of printed facts may be given; the default is 40.  By
   default, duplicates are removed from the search result. Use
   \isa{with{\isacharunderscore}dups} to display duplicates.
-  
+
+  \item \hyperlink{command.find-consts}{\mbox{\isa{\isacommand{find{\isacharunderscore}consts}}}}~\isa{criteria} prints all constants
+  whose type meets all of the given criteria. The criterion \isa{{\isachardoublequote}strict{\isacharcolon}\ ty{\isachardoublequote}} is met by any type that matches the type pattern
+  \isa{ty}.  Patterns may contain both the dummy type ``\isa{{\isacharunderscore}}''
+  and sort constraints. The criterion \isa{ty} is similar, but it
+  also matches against subtypes. The criterion \isa{{\isachardoublequote}name{\isacharcolon}\ p{\isachardoublequote}} and
+  the prefix ``\isa{{\isachardoublequote}{\isacharminus}{\isachardoublequote}}'' function as described for \hyperlink{command.find-theorems}{\mbox{\isa{\isacommand{find{\isacharunderscore}theorems}}}}.
+
   \item \hyperlink{command.thm-deps}{\mbox{\isa{\isacommand{thm{\isacharunderscore}deps}}}}~\isa{{\isachardoublequote}a\isactrlsub {\isadigit{1}}\ {\isasymdots}\ a\isactrlsub n{\isachardoublequote}}
   visualizes dependencies of facts, using Isabelle's graph browser
   tool (see also \cite{isabelle-sys}).
--- a/etc/isar-keywords-ZF.el	Mon Feb 16 10:13:30 2009 +0100
+++ b/etc/isar-keywords-ZF.el	Mon Feb 16 10:15:43 2009 +0100
@@ -73,6 +73,7 @@
     "extract_type"
     "finalconsts"
     "finally"
+    "find_consts"
     "find_theorems"
     "fix"
     "from"
@@ -280,6 +281,7 @@
     "disable_pr"
     "display_drafts"
     "enable_pr"
+    "find_consts"
     "find_theorems"
     "full_prf"
     "header"
--- a/etc/isar-keywords.el	Mon Feb 16 10:13:30 2009 +0100
+++ b/etc/isar-keywords.el	Mon Feb 16 10:15:43 2009 +0100
@@ -95,6 +95,7 @@
     "extract_type"
     "finalconsts"
     "finally"
+    "find_consts"
     "find_theorems"
     "fix"
     "fixpat"
@@ -346,6 +347,7 @@
     "display_drafts"
     "enable_pr"
     "export_code"
+    "find_consts"
     "find_theorems"
     "full_prf"
     "header"
--- a/lib/Tools/codegen	Mon Feb 16 10:13:30 2009 +0100
+++ b/lib/Tools/codegen	Mon Feb 16 10:15:43 2009 +0100
@@ -36,5 +36,5 @@
 THY=$(echo $THY | sed -e 's/\\/\\\\"/g; s/"/\\\"/g')
 ISAR="theory Codegen imports \"$THY\" begin export_code $CMD end"
 
-echo "$ISAR" | "$ISABELLE_PROCESS" -I "$IMAGE" || exit 1
-
+echo "$ISAR" | "$ISABELLE_TOOL" tty -l "$IMAGE"
+exit ${PIPESTATUS[1]}
--- a/lib/jedit/isabelle.xml	Mon Feb 16 10:13:30 2009 +0100
+++ b/lib/jedit/isabelle.xml	Mon Feb 16 10:15:43 2009 +0100
@@ -135,6 +135,7 @@
       <KEYWORD4>file</KEYWORD4>
       <OPERATOR>finalconsts</OPERATOR>
       <OPERATOR>finally</OPERATOR>
+      <LABEL>find_consts</LABEL>
       <LABEL>find_theorems</LABEL>
       <KEYWORD2>fix</KEYWORD2>
       <KEYWORD4>fixes</KEYWORD4>
--- a/src/HOL/Auth/TLS.thy	Mon Feb 16 10:13:30 2009 +0100
+++ b/src/HOL/Auth/TLS.thy	Mon Feb 16 10:15:43 2009 +0100
@@ -41,7 +41,7 @@
 
 header{*The TLS Protocol: Transport Layer Security*}
 
-theory TLS imports Public begin
+theory TLS imports Public Nat_Int_Bij begin
 
 constdefs
   certificate      :: "[agent,key] => msg"
--- a/src/HOL/Auth/document/root.tex	Mon Feb 16 10:13:30 2009 +0100
+++ b/src/HOL/Auth/document/root.tex	Mon Feb 16 10:15:43 2009 +0100
@@ -1,5 +1,6 @@
 \documentclass[10pt,a4paper,twoside]{article}
 \usepackage{graphicx}
+\usepackage{amssymb}
 \usepackage[latin1]{inputenc}
 \usepackage{latexsym,theorem}
 \usepackage{isabelle,isabellesym}
--- a/src/HOL/Complex_Main.thy	Mon Feb 16 10:13:30 2009 +0100
+++ b/src/HOL/Complex_Main.thy	Mon Feb 16 10:15:43 2009 +0100
@@ -4,7 +4,7 @@
 imports
   Main
   Real
-  Fundamental_Theorem_Algebra
+  Complex
   Log
   Ln
   Taylor
--- a/src/HOL/Divides.thy	Mon Feb 16 10:13:30 2009 +0100
+++ b/src/HOL/Divides.thy	Mon Feb 16 10:15:43 2009 +0100
@@ -172,6 +172,22 @@
   finally show ?thesis .
 qed
 
+lemma dvd_imp_mod_0: "a dvd b \<Longrightarrow> b mod a = 0"
+by (unfold dvd_def, auto)
+
+lemma dvd_div_mult_self: "a dvd b \<Longrightarrow> (b div a) * a = b"
+by (subst (2) mod_div_equality [of b a, symmetric]) (simp add:dvd_imp_mod_0)
+
+lemma div_dvd_div[simp]:
+  "a dvd b \<Longrightarrow> a dvd c \<Longrightarrow> (b div a dvd c div a) = (b dvd c)"
+apply (cases "a = 0")
+ apply simp
+apply (unfold dvd_def)
+apply auto
+ apply(blast intro:mult_assoc[symmetric])
+apply(fastsimp simp add: mult_assoc)
+done
+
 text {* Addition respects modular equivalence. *}
 
 lemma mod_add_left_eq: "(a + b) mod c = (a mod c + b) mod c"
--- a/src/HOL/Finite_Set.thy	Mon Feb 16 10:13:30 2009 +0100
+++ b/src/HOL/Finite_Set.thy	Mon Feb 16 10:15:43 2009 +0100
@@ -93,6 +93,20 @@
   qed
 qed
 
+text{* A finite choice principle. Does not need the SOME choice operator. *}
+lemma finite_set_choice:
+  "finite A \<Longrightarrow> ALL x:A. (EX y. P x y) \<Longrightarrow> EX f. ALL x:A. P x (f x)"
+proof (induct set: finite)
+  case empty thus ?case by simp
+next
+  case (insert a A)
+  then obtain f b where f: "ALL x:A. P x (f x)" and ab: "P a b" by auto
+  show ?case (is "EX f. ?P f")
+  proof
+    show "?P(%x. if x = a then b else f x)" using f ab by auto
+  qed
+qed
+
 
 text{* Finite sets are the images of initial segments of natural numbers: *}
 
@@ -140,12 +154,14 @@
   "finite A = (\<exists> (n::nat) f. A = f ` {i::nat. i<n})"
 by(blast intro: nat_seg_image_imp_finite dest: finite_imp_nat_seg_image_inj_on)
 
+lemma finite_Collect_less_nat[iff]: "finite{n::nat. n<k}"
+by(fastsimp simp: finite_conv_nat_seg_image)
+
 
 subsubsection{* Finiteness and set theoretic constructions *}
 
 lemma finite_UnI: "finite F ==> finite G ==> finite (F Un G)"
-  -- {* The union of two finite sets is finite. *}
-  by (induct set: finite) simp_all
+by (induct set: finite) simp_all
 
 lemma finite_subset: "A \<subseteq> B ==> finite B ==> finite A"
   -- {* Every subset of a finite set is finite. *}
@@ -174,15 +190,24 @@
   qed
 qed
 
-lemma finite_Collect_subset[simp]: "finite A \<Longrightarrow> finite{x \<in> A. P x}"
-using finite_subset[of "{x \<in> A. P x}" "A"] by blast
-
 lemma finite_Un [iff]: "finite (F Un G) = (finite F & finite G)"
-  by (blast intro: finite_subset [of _ "X Un Y", standard] finite_UnI)
+by (blast intro: finite_subset [of _ "X Un Y", standard] finite_UnI)
+
+lemma finite_Collect_disjI[simp]:
+  "finite{x. P x | Q x} = (finite{x. P x} & finite{x. Q x})"
+by(simp add:Collect_disj_eq)
 
 lemma finite_Int [simp, intro]: "finite F | finite G ==> finite (F Int G)"
   -- {* The converse obviously fails. *}
-  by (blast intro: finite_subset)
+by (blast intro: finite_subset)
+
+lemma finite_Collect_conjI [simp, intro]:
+  "finite{x. P x} | finite{x. Q x} ==> finite{x. P x & Q x}"
+  -- {* The converse obviously fails. *}
+by(simp add:Collect_conj_eq)
+
+lemma finite_Collect_le_nat[iff]: "finite{n::nat. n<=k}"
+by(simp add: le_eq_less_or_eq)
 
 lemma finite_insert [simp]: "finite (insert a A) = finite A"
   apply (subst insert_is_Un)
@@ -227,8 +252,24 @@
   then show ?thesis by simp
 qed
 
-lemma finite_Diff [simp]: "finite B ==> finite (B - Ba)"
-  by (rule Diff_subset [THEN finite_subset])
+lemma finite_Diff [simp]: "finite A ==> finite (A - B)"
+by (rule Diff_subset [THEN finite_subset])
+
+lemma finite_Diff2 [simp]:
+  assumes "finite B" shows "finite (A - B) = finite A"
+proof -
+  have "finite A \<longleftrightarrow> finite((A-B) Un (A Int B))" by(simp add: Un_Diff_Int)
+  also have "\<dots> \<longleftrightarrow> finite(A-B)" using `finite B` by(simp)
+  finally show ?thesis ..
+qed
+
+lemma finite_compl[simp]:
+  "finite(A::'a set) \<Longrightarrow> finite(-A) = finite(UNIV::'a set)"
+by(simp add:Compl_eq_Diff_UNIV)
+
+lemma finite_Collect_not[simp]:
+  "finite{x::'a. P x} \<Longrightarrow> finite{x. ~P x} = finite(UNIV::'a set)"
+by(simp add:Collect_neg_eq)
 
 lemma finite_Diff_insert [iff]: "finite (A - insert a B) = finite (A - B)"
   apply (subst Diff_insert)
@@ -237,9 +278,6 @@
    apply (subst insert_Diff, simp_all)
   done
 
-lemma finite_Diff_singleton [simp]: "finite (A - {a}) = finite A"
-  by simp
-
 
 text {* Image and Inverse Image over Finite Sets *}
 
@@ -307,8 +345,21 @@
   @{prop "finite C ==> ALL A B. (UNION A B) <= C --> finite {x. x:A & B x \<noteq> {}}"}
   by induction. *}
 
-lemma finite_UN [simp]: "finite A ==> finite (UNION A B) = (ALL x:A. finite (B x))"
-  by (blast intro: finite_UN_I finite_subset)
+lemma finite_UN [simp]:
+  "finite A ==> finite (UNION A B) = (ALL x:A. finite (B x))"
+by (blast intro: finite_UN_I finite_subset)
+
+lemma finite_Collect_bex[simp]: "finite A \<Longrightarrow>
+  finite{x. EX y:A. Q x y} = (ALL y:A. finite{x. Q x y})"
+apply(subgoal_tac "{x. EX y:A. Q x y} = UNION A (%y. {x. Q x y})")
+ apply auto
+done
+
+lemma finite_Collect_bounded_ex[simp]: "finite{y. P y} \<Longrightarrow>
+  finite{x. EX y. P y & Q x y} = (ALL y. P y \<longrightarrow> finite{x. Q x y})"
+apply(subgoal_tac "{x. EX y. P y & Q x y} = UNION {y. P y} (%y. {x. Q x y})")
+ apply auto
+done
 
 
 lemma finite_Plus: "[| finite A; finite B |] ==> finite (A <+> B)"
@@ -375,6 +426,9 @@
     by induct (simp_all add: finite_UnI finite_imageI Pow_insert)
 qed
 
+lemma finite_Collect_subsets[simp,intro]: "finite A \<Longrightarrow> finite{B. B \<subseteq> A}"
+by(simp add: Pow_def[symmetric])
+
 
 lemma finite_UnionD: "finite(\<Union>A) \<Longrightarrow> finite A"
 by(blast intro: finite_subset[OF subset_Pow_Union])
@@ -1655,6 +1709,42 @@
 apply (subst divide_inverse, auto)
 done
 
+lemma setprod_dvd_setprod [rule_format]: 
+    "(ALL x : A. f x dvd g x) \<longrightarrow> setprod f A dvd setprod g A"
+  apply (cases "finite A")
+  apply (induct set: finite)
+  apply (auto simp add: dvd_def)
+  apply (rule_tac x = "k * ka" in exI)
+  apply (simp add: algebra_simps)
+done
+
+lemma setprod_dvd_setprod_subset:
+  "finite B \<Longrightarrow> A <= B \<Longrightarrow> setprod f A dvd setprod f B"
+  apply (subgoal_tac "setprod f B = setprod f A * setprod f (B - A)")
+  apply (unfold dvd_def, blast)
+  apply (subst setprod_Un_disjoint [symmetric])
+  apply (auto elim: finite_subset intro: setprod_cong)
+done
+
+lemma setprod_dvd_setprod_subset2:
+  "finite B \<Longrightarrow> A <= B \<Longrightarrow> ALL x : A. (f x::'a::comm_semiring_1) dvd g x \<Longrightarrow> 
+      setprod f A dvd setprod g B"
+  apply (rule dvd_trans)
+  apply (rule setprod_dvd_setprod, erule (1) bspec)
+  apply (erule (1) setprod_dvd_setprod_subset)
+done
+
+lemma dvd_setprod: "finite A \<Longrightarrow> i:A \<Longrightarrow> 
+    (f i ::'a::comm_semiring_1) dvd setprod f A"
+by (induct set: finite) (auto intro: dvd_mult)
+
+lemma dvd_setsum [rule_format]: "(ALL i : A. d dvd f i) \<longrightarrow> 
+    (d::'a::comm_semiring_1) dvd (SUM x : A. f x)"
+  apply (cases "finite A")
+  apply (induct set: finite)
+  apply auto
+done
+
 
 subsection {* Finite cardinality *}
 
@@ -2029,6 +2119,19 @@
   show False by simp (blast dest: Suc_neq_Zero surjD)
 qed
 
+lemma infinite_UNIV_char_0:
+  "\<not> finite (UNIV::'a::semiring_char_0 set)"
+proof
+  assume "finite (UNIV::'a set)"
+  with subset_UNIV have "finite (range of_nat::'a set)"
+    by (rule finite_subset)
+  moreover have "inj (of_nat::nat \<Rightarrow> 'a)"
+    by (simp add: inj_on_def)
+  ultimately have "finite (UNIV::nat set)"
+    by (rule finite_imageD)
+  then show "False"
+    by (simp add: infinite_UNIV_nat)
+qed
 
 subsection{* A fold functional for non-empty sets *}
 
--- a/src/HOL/Fundamental_Theorem_Algebra.thy	Mon Feb 16 10:13:30 2009 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1396 +0,0 @@
-(* Author: Amine Chaieb, TU Muenchen *)
-
-header{*Fundamental Theorem of Algebra*}
-
-theory Fundamental_Theorem_Algebra
-imports Polynomial Complex
-begin
-
-subsection {* Square root of complex numbers *}
-definition csqrt :: "complex \<Rightarrow> complex" where
-"csqrt z = (if Im z = 0 then
-            if 0 \<le> Re z then Complex (sqrt(Re z)) 0
-            else Complex 0 (sqrt(- Re z))
-           else Complex (sqrt((cmod z + Re z) /2))
-                        ((Im z / abs(Im z)) * sqrt((cmod z - Re z) /2)))"
-
-lemma csqrt[algebra]: "csqrt z ^ 2 = z"
-proof-
-  obtain x y where xy: "z = Complex x y" by (cases z)
-  {assume y0: "y = 0"
-    {assume x0: "x \<ge> 0" 
-      then have ?thesis using y0 xy real_sqrt_pow2[OF x0]
-	by (simp add: csqrt_def power2_eq_square)}
-    moreover
-    {assume "\<not> x \<ge> 0" hence x0: "- x \<ge> 0" by arith
-      then have ?thesis using y0 xy real_sqrt_pow2[OF x0] 
-	by (simp add: csqrt_def power2_eq_square) }
-    ultimately have ?thesis by blast}
-  moreover
-  {assume y0: "y\<noteq>0"
-    {fix x y
-      let ?z = "Complex x y"
-      from abs_Re_le_cmod[of ?z] have tha: "abs x \<le> cmod ?z" by auto
-      hence "cmod ?z - x \<ge> 0" "cmod ?z + x \<ge> 0" by arith+ 
-      hence "(sqrt (x * x + y * y) + x) / 2 \<ge> 0" "(sqrt (x * x + y * y) - x) / 2 \<ge> 0" by (simp_all add: power2_eq_square) }
-    note th = this
-    have sq4: "\<And>x::real. x^2 / 4 = (x / 2) ^ 2" 
-      by (simp add: power2_eq_square) 
-    from th[of x y]
-    have sq4': "sqrt (((sqrt (x * x + y * y) + x)^2 / 4)) = (sqrt (x * x + y * y) + x) / 2" "sqrt (((sqrt (x * x + y * y) - x)^2 / 4)) = (sqrt (x * x + y * y) - x) / 2" unfolding sq4 by simp_all
-    then have th1: "sqrt ((sqrt (x * x + y * y) + x) * (sqrt (x * x + y * y) + x) / 4) - sqrt ((sqrt (x * x + y * y) - x) * (sqrt (x * x + y * y) - x) / 4) = x"
-      unfolding power2_eq_square by simp 
-    have "sqrt 4 = sqrt (2^2)" by simp 
-    hence sqrt4: "sqrt 4 = 2" by (simp only: real_sqrt_abs)
-    have th2: "2 *(y * sqrt ((sqrt (x * x + y * y) - x) * (sqrt (x * x + y * y) + x) / 4)) / \<bar>y\<bar> = y"
-      using iffD2[OF real_sqrt_pow2_iff sum_power2_ge_zero[of x y]] y0
-      unfolding power2_eq_square 
-      by (simp add: algebra_simps real_sqrt_divide sqrt4)
-     from y0 xy have ?thesis  apply (simp add: csqrt_def power2_eq_square)
-       apply (simp add: real_sqrt_sum_squares_mult_ge_zero[of x y] real_sqrt_pow2[OF th(1)[of x y], unfolded power2_eq_square] real_sqrt_pow2[OF th(2)[of x y], unfolded power2_eq_square] real_sqrt_mult[symmetric])
-      using th1 th2  ..}
-  ultimately show ?thesis by blast
-qed
-
-
-subsection{* More lemmas about module of complex numbers *}
-
-lemma complex_of_real_power: "complex_of_real x ^ n = complex_of_real (x^n)"
-  by (rule of_real_power [symmetric])
-
-lemma real_down2: "(0::real) < d1 \<Longrightarrow> 0 < d2 ==> EX e. 0 < e & e < d1 & e < d2"
-  apply (rule exI[where x = "min d1 d2 / 2"])
-  by (simp add: field_simps min_def)
-
-text{* The triangle inequality for cmod *}
-lemma complex_mod_triangle_sub: "cmod w \<le> cmod (w + z) + norm z"
-  using complex_mod_triangle_ineq2[of "w + z" "-z"] by auto
-
-subsection{* Basic lemmas about complex polynomials *}
-
-lemma poly_bound_exists:
-  shows "\<exists>m. m > 0 \<and> (\<forall>z. cmod z <= r \<longrightarrow> cmod (poly p z) \<le> m)"
-proof(induct p)
-  case 0 thus ?case by (rule exI[where x=1], simp) 
-next
-  case (pCons c cs)
-  from pCons.hyps obtain m where m: "\<forall>z. cmod z \<le> r \<longrightarrow> cmod (poly cs z) \<le> m"
-    by blast
-  let ?k = " 1 + cmod c + \<bar>r * m\<bar>"
-  have kp: "?k > 0" using abs_ge_zero[of "r*m"] norm_ge_zero[of c] by arith
-  {fix z
-    assume H: "cmod z \<le> r"
-    from m H have th: "cmod (poly cs z) \<le> m" by blast
-    from H have rp: "r \<ge> 0" using norm_ge_zero[of z] by arith
-    have "cmod (poly (pCons c cs) z) \<le> cmod c + cmod (z* poly cs z)"
-      using norm_triangle_ineq[of c "z* poly cs z"] by simp
-    also have "\<dots> \<le> cmod c + r*m" using mult_mono[OF H th rp norm_ge_zero[of "poly cs z"]] by (simp add: norm_mult)
-    also have "\<dots> \<le> ?k" by simp
-    finally have "cmod (poly (pCons c cs) z) \<le> ?k" .}
-  with kp show ?case by blast
-qed
-
-
-text{* Offsetting the variable in a polynomial gives another of same degree *}
-
-definition
-  "offset_poly p h = poly_rec 0 (\<lambda>a p q. smult h q + pCons a q) p"
-
-lemma offset_poly_0: "offset_poly 0 h = 0"
-  unfolding offset_poly_def by (simp add: poly_rec_0)
-
-lemma offset_poly_pCons:
-  "offset_poly (pCons a p) h =
-    smult h (offset_poly p h) + pCons a (offset_poly p h)"
-  unfolding offset_poly_def by (simp add: poly_rec_pCons)
-
-lemma offset_poly_single: "offset_poly [:a:] h = [:a:]"
-by (simp add: offset_poly_pCons offset_poly_0)
-
-lemma poly_offset_poly: "poly (offset_poly p h) x = poly p (h + x)"
-apply (induct p)
-apply (simp add: offset_poly_0)
-apply (simp add: offset_poly_pCons algebra_simps)
-done
-
-lemma offset_poly_eq_0_lemma: "smult c p + pCons a p = 0 \<Longrightarrow> p = 0"
-by (induct p arbitrary: a, simp, force)
-
-lemma offset_poly_eq_0_iff: "offset_poly p h = 0 \<longleftrightarrow> p = 0"
-apply (safe intro!: offset_poly_0)
-apply (induct p, simp)
-apply (simp add: offset_poly_pCons)
-apply (frule offset_poly_eq_0_lemma, simp)
-done
-
-lemma degree_offset_poly: "degree (offset_poly p h) = degree p"
-apply (induct p)
-apply (simp add: offset_poly_0)
-apply (case_tac "p = 0")
-apply (simp add: offset_poly_0 offset_poly_pCons)
-apply (simp add: offset_poly_pCons)
-apply (subst degree_add_eq_right)
-apply (rule le_less_trans [OF degree_smult_le])
-apply (simp add: offset_poly_eq_0_iff)
-apply (simp add: offset_poly_eq_0_iff)
-done
-
-definition
-  "psize p = (if p = 0 then 0 else Suc (degree p))"
-
-lemma psize_eq_0_iff [simp]: "psize p = 0 \<longleftrightarrow> p = 0"
-  unfolding psize_def by simp
-
-lemma poly_offset: "\<exists> q. psize q = psize p \<and> (\<forall>x. poly q (x::complex) = poly p (a + x))"
-proof (intro exI conjI)
-  show "psize (offset_poly p a) = psize p"
-    unfolding psize_def
-    by (simp add: offset_poly_eq_0_iff degree_offset_poly)
-  show "\<forall>x. poly (offset_poly p a) x = poly p (a + x)"
-    by (simp add: poly_offset_poly)
-qed
-
-text{* An alternative useful formulation of completeness of the reals *}
-lemma real_sup_exists: assumes ex: "\<exists>x. P x" and bz: "\<exists>z. \<forall>x. P x \<longrightarrow> x < z"
-  shows "\<exists>(s::real). \<forall>y. (\<exists>x. P x \<and> y < x) \<longleftrightarrow> y < s"
-proof-
-  from ex bz obtain x Y where x: "P x" and Y: "\<And>x. P x \<Longrightarrow> x < Y"  by blast
-  from ex have thx:"\<exists>x. x \<in> Collect P" by blast
-  from bz have thY: "\<exists>Y. isUb UNIV (Collect P) Y" 
-    by(auto simp add: isUb_def isLub_def setge_def setle_def leastP_def Ball_def order_le_less)
-  from reals_complete[OF thx thY] obtain L where L: "isLub UNIV (Collect P) L"
-    by blast
-  from Y[OF x] have xY: "x < Y" .
-  from L have L': "\<forall>x. P x \<longrightarrow> x \<le> L" by (auto simp add: isUb_def isLub_def setge_def setle_def leastP_def Ball_def)  
-  from Y have Y': "\<forall>x. P x \<longrightarrow> x \<le> Y" 
-    apply (clarsimp, atomize (full)) by auto 
-  from L Y' have "L \<le> Y" by (auto simp add: isUb_def isLub_def setge_def setle_def leastP_def Ball_def)
-  {fix y
-    {fix z assume z: "P z" "y < z"
-      from L' z have "y < L" by auto }
-    moreover
-    {assume yL: "y < L" "\<forall>z. P z \<longrightarrow> \<not> y < z"
-      hence nox: "\<forall>z. P z \<longrightarrow> y \<ge> z" by auto
-      from nox L have "y \<ge> L" by (auto simp add: isUb_def isLub_def setge_def setle_def leastP_def Ball_def) 
-      with yL(1) have False  by arith}
-    ultimately have "(\<exists>x. P x \<and> y < x) \<longleftrightarrow> y < L" by blast}
-  thus ?thesis by blast
-qed
-
-
-subsection{* Some theorems about Sequences*}
-text{* Given a binary function @{text "f:: nat \<Rightarrow> 'a \<Rightarrow> 'a"}, its values are uniquely determined by a function g *}
-
-lemma num_Axiom: "EX! g. g 0 = e \<and> (\<forall>n. g (Suc n) = f n (g n))"
-  unfolding Ex1_def
-  apply (rule_tac x="nat_rec e f" in exI)
-  apply (rule conjI)+
-apply (rule def_nat_rec_0, simp)
-apply (rule allI, rule def_nat_rec_Suc, simp)
-apply (rule allI, rule impI, rule ext)
-apply (erule conjE)
-apply (induct_tac x)
-apply (simp add: nat_rec_0)
-apply (erule_tac x="n" in allE)
-apply (simp)
-done
-
- text{* An equivalent formulation of monotony -- Not used here, but might be useful *}
-lemma mono_Suc: "mono f = (\<forall>n. (f n :: 'a :: order) \<le> f (Suc n))"
-unfolding mono_def
-proof auto
-  fix A B :: nat
-  assume H: "\<forall>n. f n \<le> f (Suc n)" "A \<le> B"
-  hence "\<exists>k. B = A + k" apply -  apply (thin_tac "\<forall>n. f n \<le> f (Suc n)") 
-    by presburger
-  then obtain k where k: "B = A + k" by blast
-  {fix a k
-    have "f a \<le> f (a + k)"
-    proof (induct k)
-      case 0 thus ?case by simp
-    next
-      case (Suc k)
-      from Suc.hyps H(1)[rule_format, of "a + k"] show ?case by simp
-    qed}
-  with k show "f A \<le> f B" by blast
-qed
-
-text{* for any sequence, there is a mootonic subsequence *}
-lemma seq_monosub: "\<exists>f. subseq f \<and> monoseq (\<lambda> n. (s (f n)))"
-proof-
-  {assume H: "\<forall>n. \<exists>p >n. \<forall> m\<ge>p. s m \<le> s p"
-    let ?P = "\<lambda> p n. p > n \<and> (\<forall>m \<ge> p. s m \<le> s p)"
-    from num_Axiom[of "SOME p. ?P p 0" "\<lambda>p n. SOME p. ?P p n"]
-    obtain f where f: "f 0 = (SOME p. ?P p 0)" "\<forall>n. f (Suc n) = (SOME p. ?P p (f n))" by blast
-    have "?P (f 0) 0"  unfolding f(1) some_eq_ex[of "\<lambda>p. ?P p 0"]
-      using H apply - 
-      apply (erule allE[where x=0], erule exE, rule_tac x="p" in exI) 
-      unfolding order_le_less by blast 
-    hence f0: "f 0 > 0" "\<forall>m \<ge> f 0. s m \<le> s (f 0)" by blast+
-    {fix n
-      have "?P (f (Suc n)) (f n)" 
-	unfolding f(2)[rule_format, of n] some_eq_ex[of "\<lambda>p. ?P p (f n)"]
-	using H apply - 
-      apply (erule allE[where x="f n"], erule exE, rule_tac x="p" in exI) 
-      unfolding order_le_less by blast 
-    hence "f (Suc n) > f n" "\<forall>m \<ge> f (Suc n). s m \<le> s (f (Suc n))" by blast+}
-  note fSuc = this
-    {fix p q assume pq: "p \<ge> f q"
-      have "s p \<le> s(f(q))"  using f0(2)[rule_format, of p] pq fSuc
-	by (cases q, simp_all) }
-    note pqth = this
-    {fix q
-      have "f (Suc q) > f q" apply (induct q) 
-	using f0(1) fSuc(1)[of 0] apply simp by (rule fSuc(1))}
-    note fss = this
-    from fss have th1: "subseq f" unfolding subseq_Suc_iff ..
-    {fix a b 
-      have "f a \<le> f (a + b)"
-      proof(induct b)
-	case 0 thus ?case by simp
-      next
-	case (Suc b)
-	from fSuc(1)[of "a + b"] Suc.hyps show ?case by simp
-      qed}
-    note fmon0 = this
-    have "monoseq (\<lambda>n. s (f n))" 
-    proof-
-      {fix n
-	have "s (f n) \<ge> s (f (Suc n))" 
-	proof(cases n)
-	  case 0
-	  assume n0: "n = 0"
-	  from fSuc(1)[of 0] have th0: "f 0 \<le> f (Suc 0)" by simp
-	  from f0(2)[rule_format, OF th0] show ?thesis  using n0 by simp
-	next
-	  case (Suc m)
-	  assume m: "n = Suc m"
-	  from fSuc(1)[of n] m have th0: "f (Suc m) \<le> f (Suc (Suc m))" by simp
-	  from m fSuc(2)[rule_format, OF th0] show ?thesis by simp 
-	qed}
-      thus "monoseq (\<lambda>n. s (f n))" unfolding monoseq_Suc by blast 
-    qed
-    with th1 have ?thesis by blast}
-  moreover
-  {fix N assume N: "\<forall>p >N. \<exists> m\<ge>p. s m > s p"
-    {fix p assume p: "p \<ge> Suc N" 
-      hence pN: "p > N" by arith with N obtain m where m: "m \<ge> p" "s m > s p" by blast
-      have "m \<noteq> p" using m(2) by auto 
-      with m have "\<exists>m>p. s p < s m" by - (rule exI[where x=m], auto)}
-    note th0 = this
-    let ?P = "\<lambda>m x. m > x \<and> s x < s m"
-    from num_Axiom[of "SOME x. ?P x (Suc N)" "\<lambda>m x. SOME y. ?P y x"]
-    obtain f where f: "f 0 = (SOME x. ?P x (Suc N))" 
-      "\<forall>n. f (Suc n) = (SOME m. ?P m (f n))" by blast
-    have "?P (f 0) (Suc N)"  unfolding f(1) some_eq_ex[of "\<lambda>p. ?P p (Suc N)"]
-      using N apply - 
-      apply (erule allE[where x="Suc N"], clarsimp)
-      apply (rule_tac x="m" in exI)
-      apply auto
-      apply (subgoal_tac "Suc N \<noteq> m")
-      apply simp
-      apply (rule ccontr, simp)
-      done
-    hence f0: "f 0 > Suc N" "s (Suc N) < s (f 0)" by blast+
-    {fix n
-      have "f n > N \<and> ?P (f (Suc n)) (f n)"
-	unfolding f(2)[rule_format, of n] some_eq_ex[of "\<lambda>p. ?P p (f n)"]
-      proof (induct n)
-	case 0 thus ?case
-	  using f0 N apply auto 
-	  apply (erule allE[where x="f 0"], clarsimp) 
-	  apply (rule_tac x="m" in exI, simp)
-	  by (subgoal_tac "f 0 \<noteq> m", auto)
-      next
-	case (Suc n)
-	from Suc.hyps have Nfn: "N < f n" by blast
-	from Suc.hyps obtain m where m: "m > f n" "s (f n) < s m" by blast
-	with Nfn have mN: "m > N" by arith
-	note key = Suc.hyps[unfolded some_eq_ex[of "\<lambda>p. ?P p (f n)", symmetric] f(2)[rule_format, of n, symmetric]]
-	
-	from key have th0: "f (Suc n) > N" by simp
-	from N[rule_format, OF th0]
-	obtain m' where m': "m' \<ge> f (Suc n)" "s (f (Suc n)) < s m'" by blast
-	have "m' \<noteq> f (Suc (n))" apply (rule ccontr) using m'(2) by auto
-	hence "m' > f (Suc n)" using m'(1) by simp
-	with key m'(2) show ?case by auto
-      qed}
-    note fSuc = this
-    {fix n
-      have "f n \<ge> Suc N \<and> f(Suc n) > f n \<and> s(f n) < s(f(Suc n))" using fSuc[of n] by auto 
-      hence "f n \<ge> Suc N" "f(Suc n) > f n" "s(f n) < s(f(Suc n))" by blast+}
-    note thf = this
-    have sqf: "subseq f" unfolding subseq_Suc_iff using thf by simp
-    have "monoseq (\<lambda>n. s (f n))"  unfolding monoseq_Suc using thf
-      apply -
-      apply (rule disjI1)
-      apply auto
-      apply (rule order_less_imp_le)
-      apply blast
-      done
-    then have ?thesis  using sqf by blast}
-  ultimately show ?thesis unfolding linorder_not_less[symmetric] by blast
-qed
-
-lemma seq_suble: assumes sf: "subseq f" shows "n \<le> f n"
-proof(induct n)
-  case 0 thus ?case by simp
-next
-  case (Suc n)
-  from sf[unfolded subseq_Suc_iff, rule_format, of n] Suc.hyps
-  have "n < f (Suc n)" by arith 
-  thus ?case by arith
-qed
-
-subsection {* Fundamental theorem of algebra *}
-lemma  unimodular_reduce_norm:
-  assumes md: "cmod z = 1"
-  shows "cmod (z + 1) < 1 \<or> cmod (z - 1) < 1 \<or> cmod (z + ii) < 1 \<or> cmod (z - ii) < 1"
-proof-
-  obtain x y where z: "z = Complex x y " by (cases z, auto)
-  from md z have xy: "x^2 + y^2 = 1" by (simp add: cmod_def)
-  {assume C: "cmod (z + 1) \<ge> 1" "cmod (z - 1) \<ge> 1" "cmod (z + ii) \<ge> 1" "cmod (z - ii) \<ge> 1"
-    from C z xy have "2*x \<le> 1" "2*x \<ge> -1" "2*y \<le> 1" "2*y \<ge> -1"
-      by (simp_all add: cmod_def power2_eq_square algebra_simps)
-    hence "abs (2*x) \<le> 1" "abs (2*y) \<le> 1" by simp_all
-    hence "(abs (2 * x))^2 <= 1^2" "(abs (2 * y)) ^2 <= 1^2"
-      by - (rule power_mono, simp, simp)+
-    hence th0: "4*x^2 \<le> 1" "4*y^2 \<le> 1" 
-      by (simp_all  add: power2_abs power_mult_distrib)
-    from add_mono[OF th0] xy have False by simp }
-  thus ?thesis unfolding linorder_not_le[symmetric] by blast
-qed
-
-text{* Hence we can always reduce modulus of @{text "1 + b z^n"} if nonzero *}
-lemma reduce_poly_simple:
- assumes b: "b \<noteq> 0" and n: "n\<noteq>0"
-  shows "\<exists>z. cmod (1 + b * z^n) < 1"
-using n
-proof(induct n rule: nat_less_induct)
-  fix n
-  assume IH: "\<forall>m<n. m \<noteq> 0 \<longrightarrow> (\<exists>z. cmod (1 + b * z ^ m) < 1)" and n: "n \<noteq> 0"
-  let ?P = "\<lambda>z n. cmod (1 + b * z ^ n) < 1"
-  {assume e: "even n"
-    hence "\<exists>m. n = 2*m" by presburger
-    then obtain m where m: "n = 2*m" by blast
-    from n m have "m\<noteq>0" "m < n" by presburger+
-    with IH[rule_format, of m] obtain z where z: "?P z m" by blast
-    from z have "?P (csqrt z) n" by (simp add: m power_mult csqrt)
-    hence "\<exists>z. ?P z n" ..}
-  moreover
-  {assume o: "odd n"
-    from b have b': "b^2 \<noteq> 0" unfolding power2_eq_square by simp
-    have "Im (inverse b) * (Im (inverse b) * \<bar>Im b * Im b + Re b * Re b\<bar>) +
-    Re (inverse b) * (Re (inverse b) * \<bar>Im b * Im b + Re b * Re b\<bar>) = 
-    ((Re (inverse b))^2 + (Im (inverse b))^2) * \<bar>Im b * Im b + Re b * Re b\<bar>" by algebra
-    also have "\<dots> = cmod (inverse b) ^2 * cmod b ^ 2" 
-      apply (simp add: cmod_def) using realpow_two_le_add_order[of "Re b" "Im b"]
-      by (simp add: power2_eq_square)
-    finally 
-    have th0: "Im (inverse b) * (Im (inverse b) * \<bar>Im b * Im b + Re b * Re b\<bar>) +
-    Re (inverse b) * (Re (inverse b) * \<bar>Im b * Im b + Re b * Re b\<bar>) =
-    1" 
-      apply (simp add: power2_eq_square norm_mult[symmetric] norm_inverse[symmetric])
-      using right_inverse[OF b']
-      by (simp add: power2_eq_square[symmetric] power_inverse[symmetric] algebra_simps)
-    have th0: "cmod (complex_of_real (cmod b) / b) = 1"
-      apply (simp add: complex_Re_mult cmod_def power2_eq_square Re_complex_of_real Im_complex_of_real divide_inverse algebra_simps )
-      by (simp add: real_sqrt_mult[symmetric] th0)        
-    from o have "\<exists>m. n = Suc (2*m)" by presburger+
-    then obtain m where m: "n = Suc (2*m)" by blast
-    from unimodular_reduce_norm[OF th0] o
-    have "\<exists>v. cmod (complex_of_real (cmod b) / b + v^n) < 1"
-      apply (cases "cmod (complex_of_real (cmod b) / b + 1) < 1", rule_tac x="1" in exI, simp)
-      apply (cases "cmod (complex_of_real (cmod b) / b - 1) < 1", rule_tac x="-1" in exI, simp add: diff_def)
-      apply (cases "cmod (complex_of_real (cmod b) / b + ii) < 1")
-      apply (cases "even m", rule_tac x="ii" in exI, simp add: m power_mult)
-      apply (rule_tac x="- ii" in exI, simp add: m power_mult)
-      apply (cases "even m", rule_tac x="- ii" in exI, simp add: m power_mult diff_def)
-      apply (rule_tac x="ii" in exI, simp add: m power_mult diff_def)
-      done
-    then obtain v where v: "cmod (complex_of_real (cmod b) / b + v^n) < 1" by blast
-    let ?w = "v / complex_of_real (root n (cmod b))"
-    from odd_real_root_pow[OF o, of "cmod b"]
-    have th1: "?w ^ n = v^n / complex_of_real (cmod b)" 
-      by (simp add: power_divide complex_of_real_power)
-    have th2:"cmod (complex_of_real (cmod b) / b) = 1" using b by (simp add: norm_divide)
-    hence th3: "cmod (complex_of_real (cmod b) / b) \<ge> 0" by simp
-    have th4: "cmod (complex_of_real (cmod b) / b) *
-   cmod (1 + b * (v ^ n / complex_of_real (cmod b)))
-   < cmod (complex_of_real (cmod b) / b) * 1"
-      apply (simp only: norm_mult[symmetric] right_distrib)
-      using b v by (simp add: th2)
-
-    from mult_less_imp_less_left[OF th4 th3]
-    have "?P ?w n" unfolding th1 . 
-    hence "\<exists>z. ?P z n" .. }
-  ultimately show "\<exists>z. ?P z n" by blast
-qed
-
-
-text{* Bolzano-Weierstrass type property for closed disc in complex plane. *}
-
-lemma metric_bound_lemma: "cmod (x - y) <= \<bar>Re x - Re y\<bar> + \<bar>Im x - Im y\<bar>"
-  using real_sqrt_sum_squares_triangle_ineq[of "Re x - Re y" 0 0 "Im x - Im y" ]
-  unfolding cmod_def by simp
-
-lemma bolzano_weierstrass_complex_disc:
-  assumes r: "\<forall>n. cmod (s n) \<le> r"
-  shows "\<exists>f z. subseq f \<and> (\<forall>e >0. \<exists>N. \<forall>n \<ge> N. cmod (s (f n) - z) < e)"
-proof-
-  from seq_monosub[of "Re o s"] 
-  obtain f g where f: "subseq f" "monoseq (\<lambda>n. Re (s (f n)))" 
-    unfolding o_def by blast
-  from seq_monosub[of "Im o s o f"] 
-  obtain g where g: "subseq g" "monoseq (\<lambda>n. Im (s(f(g n))))" unfolding o_def by blast  
-  let ?h = "f o g"
-  from r[rule_format, of 0] have rp: "r \<ge> 0" using norm_ge_zero[of "s 0"] by arith 
-  have th:"\<forall>n. r + 1 \<ge> \<bar> Re (s n)\<bar>" 
-  proof
-    fix n
-    from abs_Re_le_cmod[of "s n"] r[rule_format, of n]  show "\<bar>Re (s n)\<bar> \<le> r + 1" by arith
-  qed
-  have conv1: "convergent (\<lambda>n. Re (s ( f n)))"
-    apply (rule Bseq_monoseq_convergent)
-    apply (simp add: Bseq_def)
-    apply (rule exI[where x= "r + 1"])
-    using th rp apply simp
-    using f(2) .
-  have th:"\<forall>n. r + 1 \<ge> \<bar> Im (s n)\<bar>" 
-  proof
-    fix n
-    from abs_Im_le_cmod[of "s n"] r[rule_format, of n]  show "\<bar>Im (s n)\<bar> \<le> r + 1" by arith
-  qed
-
-  have conv2: "convergent (\<lambda>n. Im (s (f (g n))))"
-    apply (rule Bseq_monoseq_convergent)
-    apply (simp add: Bseq_def)
-    apply (rule exI[where x= "r + 1"])
-    using th rp apply simp
-    using g(2) .
-
-  from conv1[unfolded convergent_def] obtain x where "LIMSEQ (\<lambda>n. Re (s (f n))) x" 
-    by blast 
-  hence  x: "\<forall>r>0. \<exists>n0. \<forall>n\<ge>n0. \<bar> Re (s (f n)) - x \<bar> < r" 
-    unfolding LIMSEQ_def real_norm_def .
-
-  from conv2[unfolded convergent_def] obtain y where "LIMSEQ (\<lambda>n. Im (s (f (g n)))) y" 
-    by blast 
-  hence  y: "\<forall>r>0. \<exists>n0. \<forall>n\<ge>n0. \<bar> Im (s (f (g n))) - y \<bar> < r" 
-    unfolding LIMSEQ_def real_norm_def .
-  let ?w = "Complex x y"
-  from f(1) g(1) have hs: "subseq ?h" unfolding subseq_def by auto 
-  {fix e assume ep: "e > (0::real)"
-    hence e2: "e/2 > 0" by simp
-    from x[rule_format, OF e2] y[rule_format, OF e2]
-    obtain N1 N2 where N1: "\<forall>n\<ge>N1. \<bar>Re (s (f n)) - x\<bar> < e / 2" and N2: "\<forall>n\<ge>N2. \<bar>Im (s (f (g n))) - y\<bar> < e / 2" by blast
-    {fix n assume nN12: "n \<ge> N1 + N2"
-      hence nN1: "g n \<ge> N1" and nN2: "n \<ge> N2" using seq_suble[OF g(1), of n] by arith+
-      from add_strict_mono[OF N1[rule_format, OF nN1] N2[rule_format, OF nN2]]
-      have "cmod (s (?h n) - ?w) < e" 
-	using metric_bound_lemma[of "s (f (g n))" ?w] by simp }
-    hence "\<exists>N. \<forall>n\<ge>N. cmod (s (?h n) - ?w) < e" by blast }
-  with hs show ?thesis  by blast  
-qed
-
-text{* Polynomial is continuous. *}
-
-lemma poly_cont:
-  assumes ep: "e > 0" 
-  shows "\<exists>d >0. \<forall>w. 0 < cmod (w - z) \<and> cmod (w - z) < d \<longrightarrow> cmod (poly p w - poly p z) < e"
-proof-
-  obtain q where q: "degree q = degree p" "\<And>x. poly q x = poly p (z + x)"
-  proof
-    show "degree (offset_poly p z) = degree p"
-      by (rule degree_offset_poly)
-    show "\<And>x. poly (offset_poly p z) x = poly p (z + x)"
-      by (rule poly_offset_poly)
-  qed
-  {fix w
-    note q(2)[of "w - z", simplified]}
-  note th = this
-  show ?thesis unfolding th[symmetric]
-  proof(induct q)
-    case 0 thus ?case  using ep by auto
-  next
-    case (pCons c cs)
-    from poly_bound_exists[of 1 "cs"] 
-    obtain m where m: "m > 0" "\<And>z. cmod z \<le> 1 \<Longrightarrow> cmod (poly cs z) \<le> m" by blast
-    from ep m(1) have em0: "e/m > 0" by (simp add: field_simps)
-    have one0: "1 > (0::real)"  by arith
-    from real_lbound_gt_zero[OF one0 em0] 
-    obtain d where d: "d >0" "d < 1" "d < e / m" by blast
-    from d(1,3) m(1) have dm: "d*m > 0" "d*m < e" 
-      by (simp_all add: field_simps real_mult_order)
-    show ?case 
-      proof(rule ex_forward[OF real_lbound_gt_zero[OF one0 em0]], clarsimp simp add: norm_mult)
-	fix d w
-	assume H: "d > 0" "d < 1" "d < e/m" "w\<noteq>z" "cmod (w-z) < d"
-	hence d1: "cmod (w-z) \<le> 1" "d \<ge> 0" by simp_all
-	from H(3) m(1) have dme: "d*m < e" by (simp add: field_simps)
-	from H have th: "cmod (w-z) \<le> d" by simp 
-	from mult_mono[OF th m(2)[OF d1(1)] d1(2) norm_ge_zero] dme
-	show "cmod (w - z) * cmod (poly cs (w - z)) < e" by simp
-      qed  
-    qed
-qed
-
-text{* Hence a polynomial attains minimum on a closed disc 
-  in the complex plane. *}
-lemma  poly_minimum_modulus_disc:
-  "\<exists>z. \<forall>w. cmod w \<le> r \<longrightarrow> cmod (poly p z) \<le> cmod (poly p w)"
-proof-
-  {assume "\<not> r \<ge> 0" hence ?thesis unfolding linorder_not_le
-      apply -
-      apply (rule exI[where x=0]) 
-      apply auto
-      apply (subgoal_tac "cmod w < 0")
-      apply simp
-      apply arith
-      done }
-  moreover
-  {assume rp: "r \<ge> 0"
-    from rp have "cmod 0 \<le> r \<and> cmod (poly p 0) = - (- cmod (poly p 0))" by simp 
-    hence mth1: "\<exists>x z. cmod z \<le> r \<and> cmod (poly p z) = - x"  by blast
-    {fix x z
-      assume H: "cmod z \<le> r" "cmod (poly p z) = - x" "\<not>x < 1"
-      hence "- x < 0 " by arith
-      with H(2) norm_ge_zero[of "poly p z"]  have False by simp }
-    then have mth2: "\<exists>z. \<forall>x. (\<exists>z. cmod z \<le> r \<and> cmod (poly p z) = - x) \<longrightarrow> x < z" by blast
-    from real_sup_exists[OF mth1 mth2] obtain s where 
-      s: "\<forall>y. (\<exists>x. (\<exists>z. cmod z \<le> r \<and> cmod (poly p z) = - x) \<and> y < x) \<longleftrightarrow>(y < s)" by blast
-    let ?m = "-s"
-    {fix y
-      from s[rule_format, of "-y"] have 
-    "(\<exists>z x. cmod z \<le> r \<and> -(- cmod (poly p z)) < y) \<longleftrightarrow> ?m < y" 
-	unfolding minus_less_iff[of y ] equation_minus_iff by blast }
-    note s1 = this[unfolded minus_minus]
-    from s1[of ?m] have s1m: "\<And>z x. cmod z \<le> r \<Longrightarrow> cmod (poly p z) \<ge> ?m" 
-      by auto
-    {fix n::nat
-      from s1[rule_format, of "?m + 1/real (Suc n)"] 
-      have "\<exists>z. cmod z \<le> r \<and> cmod (poly p z) < - s + 1 / real (Suc n)"
-	by simp}
-    hence th: "\<forall>n. \<exists>z. cmod z \<le> r \<and> cmod (poly p z) < - s + 1 / real (Suc n)" ..
-    from choice[OF th] obtain g where 
-      g: "\<forall>n. cmod (g n) \<le> r" "\<forall>n. cmod (poly p (g n)) <?m+1 /real(Suc n)" 
-      by blast
-    from bolzano_weierstrass_complex_disc[OF g(1)] 
-    obtain f z where fz: "subseq f" "\<forall>e>0. \<exists>N. \<forall>n\<ge>N. cmod (g (f n) - z) < e"
-      by blast    
-    {fix w 
-      assume wr: "cmod w \<le> r"
-      let ?e = "\<bar>cmod (poly p z) - ?m\<bar>"
-      {assume e: "?e > 0"
-	hence e2: "?e/2 > 0" by simp
-	from poly_cont[OF e2, of z p] obtain d where
-	  d: "d>0" "\<forall>w. 0<cmod (w - z)\<and> cmod(w - z) < d \<longrightarrow> cmod(poly p w - poly p z) < ?e/2" by blast
-	{fix w assume w: "cmod (w - z) < d"
-	  have "cmod(poly p w - poly p z) < ?e / 2"
-	    using d(2)[rule_format, of w] w e by (cases "w=z", simp_all)}
-	note th1 = this
-	
-	from fz(2)[rule_format, OF d(1)] obtain N1 where 
-	  N1: "\<forall>n\<ge>N1. cmod (g (f n) - z) < d" by blast
-	from reals_Archimedean2[of "2/?e"] obtain N2::nat where
-	  N2: "2/?e < real N2" by blast
-	have th2: "cmod(poly p (g(f(N1 + N2))) - poly p z) < ?e/2"
-	  using N1[rule_format, of "N1 + N2"] th1 by simp
-	{fix a b e2 m :: real
-	have "a < e2 \<Longrightarrow> abs(b - m) < e2 \<Longrightarrow> 2 * e2 <= abs(b - m) + a
-          ==> False" by arith}
-      note th0 = this
-      have ath: 
-	"\<And>m x e. m <= x \<Longrightarrow>  x < m + e ==> abs(x - m::real) < e" by arith
-      from s1m[OF g(1)[rule_format]]
-      have th31: "?m \<le> cmod(poly p (g (f (N1 + N2))))" .
-      from seq_suble[OF fz(1), of "N1+N2"]
-      have th00: "real (Suc (N1+N2)) \<le> real (Suc (f (N1+N2)))" by simp
-      have th000: "0 \<le> (1::real)" "(1::real) \<le> 1" "real (Suc (N1+N2)) > 0"  
-	using N2 by auto
-      from frac_le[OF th000 th00] have th00: "?m +1 / real (Suc (f (N1 + N2))) \<le> ?m + 1 / real (Suc (N1 + N2))" by simp
-      from g(2)[rule_format, of "f (N1 + N2)"]
-      have th01:"cmod (poly p (g (f (N1 + N2)))) < - s + 1 / real (Suc (f (N1 + N2)))" .
-      from order_less_le_trans[OF th01 th00]
-      have th32: "cmod(poly p (g (f (N1 + N2)))) < ?m + (1/ real(Suc (N1 + N2)))" .
-      from N2 have "2/?e < real (Suc (N1 + N2))" by arith
-      with e2 less_imp_inverse_less[of "2/?e" "real (Suc (N1 + N2))"]
-      have "?e/2 > 1/ real (Suc (N1 + N2))" by (simp add: inverse_eq_divide)
-      with ath[OF th31 th32]
-      have thc1:"\<bar>cmod(poly p (g (f (N1 + N2)))) - ?m\<bar>< ?e/2" by arith  
-      have ath2: "\<And>(a::real) b c m. \<bar>a - b\<bar> <= c ==> \<bar>b - m\<bar> <= \<bar>a - m\<bar> + c" 
-	by arith
-      have th22: "\<bar>cmod (poly p (g (f (N1 + N2)))) - cmod (poly p z)\<bar>
-\<le> cmod (poly p (g (f (N1 + N2))) - poly p z)" 
-	by (simp add: norm_triangle_ineq3)
-      from ath2[OF th22, of ?m]
-      have thc2: "2*(?e/2) \<le> \<bar>cmod(poly p (g (f (N1 + N2)))) - ?m\<bar> + cmod (poly p (g (f (N1 + N2))) - poly p z)" by simp
-      from th0[OF th2 thc1 thc2] have False .}
-      hence "?e = 0" by auto
-      then have "cmod (poly p z) = ?m" by simp  
-      with s1m[OF wr]
-      have "cmod (poly p z) \<le> cmod (poly p w)" by simp }
-    hence ?thesis by blast}
-  ultimately show ?thesis by blast
-qed
-
-lemma "(rcis (sqrt (abs r)) (a/2)) ^ 2 = rcis (abs r) a"
-  unfolding power2_eq_square
-  apply (simp add: rcis_mult)
-  apply (simp add: power2_eq_square[symmetric])
-  done
-
-lemma cispi: "cis pi = -1" 
-  unfolding cis_def
-  by simp
-
-lemma "(rcis (sqrt (abs r)) ((pi + a)/2)) ^ 2 = rcis (- abs r) a"
-  unfolding power2_eq_square
-  apply (simp add: rcis_mult add_divide_distrib)
-  apply (simp add: power2_eq_square[symmetric] rcis_def cispi cis_mult[symmetric])
-  done
-
-text {* Nonzero polynomial in z goes to infinity as z does. *}
-
-lemma poly_infinity:
-  assumes ex: "p \<noteq> 0"
-  shows "\<exists>r. \<forall>z. r \<le> cmod z \<longrightarrow> d \<le> cmod (poly (pCons a p) z)"
-using ex
-proof(induct p arbitrary: a d)
-  case (pCons c cs a d) 
-  {assume H: "cs \<noteq> 0"
-    with pCons.hyps obtain r where r: "\<forall>z. r \<le> cmod z \<longrightarrow> d + cmod a \<le> cmod (poly (pCons c cs) z)" by blast
-    let ?r = "1 + \<bar>r\<bar>"
-    {fix z assume h: "1 + \<bar>r\<bar> \<le> cmod z"
-      have r0: "r \<le> cmod z" using h by arith
-      from r[rule_format, OF r0]
-      have th0: "d + cmod a \<le> 1 * cmod(poly (pCons c cs) z)" by arith
-      from h have z1: "cmod z \<ge> 1" by arith
-      from order_trans[OF th0 mult_right_mono[OF z1 norm_ge_zero[of "poly (pCons c cs) z"]]]
-      have th1: "d \<le> cmod(z * poly (pCons c cs) z) - cmod a"
-	unfolding norm_mult by (simp add: algebra_simps)
-      from complex_mod_triangle_sub[of "z * poly (pCons c cs) z" a]
-      have th2: "cmod(z * poly (pCons c cs) z) - cmod a \<le> cmod (poly (pCons a (pCons c cs)) z)" 
-	by (simp add: diff_le_eq algebra_simps) 
-      from th1 th2 have "d \<le> cmod (poly (pCons a (pCons c cs)) z)"  by arith}
-    hence ?case by blast}
-  moreover
-  {assume cs0: "\<not> (cs \<noteq> 0)"
-    with pCons.prems have c0: "c \<noteq> 0" by simp
-    from cs0 have cs0': "cs = 0" by simp
-    {fix z
-      assume h: "(\<bar>d\<bar> + cmod a) / cmod c \<le> cmod z"
-      from c0 have "cmod c > 0" by simp
-      from h c0 have th0: "\<bar>d\<bar> + cmod a \<le> cmod (z*c)" 
-	by (simp add: field_simps norm_mult)
-      have ath: "\<And>mzh mazh ma. mzh <= mazh + ma ==> abs(d) + ma <= mzh ==> d <= mazh" by arith
-      from complex_mod_triangle_sub[of "z*c" a ]
-      have th1: "cmod (z * c) \<le> cmod (a + z * c) + cmod a"
-	by (simp add: algebra_simps)
-      from ath[OF th1 th0] have "d \<le> cmod (poly (pCons a (pCons c cs)) z)" 
-        using cs0' by simp}
-    then have ?case  by blast}
-  ultimately show ?case by blast
-qed simp
-
-text {* Hence polynomial's modulus attains its minimum somewhere. *}
-lemma poly_minimum_modulus:
-  "\<exists>z.\<forall>w. cmod (poly p z) \<le> cmod (poly p w)"
-proof(induct p)
-  case (pCons c cs) 
-  {assume cs0: "cs \<noteq> 0"
-    from poly_infinity[OF cs0, of "cmod (poly (pCons c cs) 0)" c]
-    obtain r where r: "\<And>z. r \<le> cmod z \<Longrightarrow> cmod (poly (pCons c cs) 0) \<le> cmod (poly (pCons c cs) z)" by blast
-    have ath: "\<And>z r. r \<le> cmod z \<or> cmod z \<le> \<bar>r\<bar>" by arith
-    from poly_minimum_modulus_disc[of "\<bar>r\<bar>" "pCons c cs"] 
-    obtain v where v: "\<And>w. cmod w \<le> \<bar>r\<bar> \<Longrightarrow> cmod (poly (pCons c cs) v) \<le> cmod (poly (pCons c cs) w)" by blast
-    {fix z assume z: "r \<le> cmod z"
-      from v[of 0] r[OF z] 
-      have "cmod (poly (pCons c cs) v) \<le> cmod (poly (pCons c cs) z)"
-	by simp }
-    note v0 = this
-    from v0 v ath[of r] have ?case by blast}
-  moreover
-  {assume cs0: "\<not> (cs \<noteq> 0)"
-    hence th:"cs = 0" by simp
-    from th pCons.hyps have ?case by simp}
-  ultimately show ?case by blast
-qed simp
-
-text{* Constant function (non-syntactic characterization). *}
-definition "constant f = (\<forall>x y. f x = f y)"
-
-lemma nonconstant_length: "\<not> (constant (poly p)) \<Longrightarrow> psize p \<ge> 2"
-  unfolding constant_def psize_def
-  apply (induct p, auto)
-  done
- 
-lemma poly_replicate_append:
-  "poly (monom 1 n * p) (x::'a::{recpower, comm_ring_1}) = x^n * poly p x"
-  by (simp add: poly_monom)
-
-text {* Decomposition of polynomial, skipping zero coefficients 
-  after the first.  *}
-
-lemma poly_decompose_lemma:
- assumes nz: "\<not>(\<forall>z. z\<noteq>0 \<longrightarrow> poly p z = (0::'a::{recpower,idom}))"
-  shows "\<exists>k a q. a\<noteq>0 \<and> Suc (psize q + k) = psize p \<and> 
-                 (\<forall>z. poly p z = z^k * poly (pCons a q) z)"
-unfolding psize_def
-using nz
-proof(induct p)
-  case 0 thus ?case by simp
-next
-  case (pCons c cs)
-  {assume c0: "c = 0"
-    from pCons.hyps pCons.prems c0 have ?case apply auto
-      apply (rule_tac x="k+1" in exI)
-      apply (rule_tac x="a" in exI, clarsimp)
-      apply (rule_tac x="q" in exI)
-      by (auto simp add: power_Suc)}
-  moreover
-  {assume c0: "c\<noteq>0"
-    hence ?case apply-
-      apply (rule exI[where x=0])
-      apply (rule exI[where x=c], clarsimp)
-      apply (rule exI[where x=cs])
-      apply auto
-      done}
-  ultimately show ?case by blast
-qed
-
-lemma poly_decompose:
-  assumes nc: "~constant(poly p)"
-  shows "\<exists>k a q. a\<noteq>(0::'a::{recpower,idom}) \<and> k\<noteq>0 \<and>
-               psize q + k + 1 = psize p \<and> 
-              (\<forall>z. poly p z = poly p 0 + z^k * poly (pCons a q) z)"
-using nc 
-proof(induct p)
-  case 0 thus ?case by (simp add: constant_def)
-next
-  case (pCons c cs)
-  {assume C:"\<forall>z. z \<noteq> 0 \<longrightarrow> poly cs z = 0"
-    {fix x y
-      from C have "poly (pCons c cs) x = poly (pCons c cs) y" by (cases "x=0", auto)}
-    with pCons.prems have False by (auto simp add: constant_def)}
-  hence th: "\<not> (\<forall>z. z \<noteq> 0 \<longrightarrow> poly cs z = 0)" ..
-  from poly_decompose_lemma[OF th] 
-  show ?case 
-    apply clarsimp
-    apply (rule_tac x="k+1" in exI)
-    apply (rule_tac x="a" in exI)
-    apply simp
-    apply (rule_tac x="q" in exI)
-    apply (auto simp add: power_Suc)
-    apply (auto simp add: psize_def split: if_splits)
-    done
-qed
-
-text{* Fundamental theorem of algebral *}
-
-lemma fundamental_theorem_of_algebra:
-  assumes nc: "~constant(poly p)"
-  shows "\<exists>z::complex. poly p z = 0"
-using nc
-proof(induct n\<equiv> "psize p" arbitrary: p rule: nat_less_induct)
-  fix n fix p :: "complex poly"
-  let ?p = "poly p"
-  assume H: "\<forall>m<n. \<forall>p. \<not> constant (poly p) \<longrightarrow> m = psize p \<longrightarrow> (\<exists>(z::complex). poly p z = 0)" and nc: "\<not> constant ?p" and n: "n = psize p"
-  let ?ths = "\<exists>z. ?p z = 0"
-
-  from nonconstant_length[OF nc] have n2: "n\<ge> 2" by (simp add: n)
-  from poly_minimum_modulus obtain c where 
-    c: "\<forall>w. cmod (?p c) \<le> cmod (?p w)" by blast
-  {assume pc: "?p c = 0" hence ?ths by blast}
-  moreover
-  {assume pc0: "?p c \<noteq> 0"
-    from poly_offset[of p c] obtain q where
-      q: "psize q = psize p" "\<forall>x. poly q x = ?p (c+x)" by blast
-    {assume h: "constant (poly q)"
-      from q(2) have th: "\<forall>x. poly q (x - c) = ?p x" by auto
-      {fix x y
-	from th have "?p x = poly q (x - c)" by auto 
-	also have "\<dots> = poly q (y - c)" 
-	  using h unfolding constant_def by blast
-	also have "\<dots> = ?p y" using th by auto
-	finally have "?p x = ?p y" .}
-      with nc have False unfolding constant_def by blast }
-    hence qnc: "\<not> constant (poly q)" by blast
-    from q(2) have pqc0: "?p c = poly q 0" by simp
-    from c pqc0 have cq0: "\<forall>w. cmod (poly q 0) \<le> cmod (?p w)" by simp 
-    let ?a0 = "poly q 0"
-    from pc0 pqc0 have a00: "?a0 \<noteq> 0" by simp 
-    from a00 
-    have qr: "\<forall>z. poly q z = poly (smult (inverse ?a0) q) z * ?a0"
-      by simp
-    let ?r = "smult (inverse ?a0) q"
-    have lgqr: "psize q = psize ?r"
-      using a00 unfolding psize_def degree_def
-      by (simp add: expand_poly_eq)
-    {assume h: "\<And>x y. poly ?r x = poly ?r y"
-      {fix x y
-	from qr[rule_format, of x] 
-	have "poly q x = poly ?r x * ?a0" by auto
-	also have "\<dots> = poly ?r y * ?a0" using h by simp
-	also have "\<dots> = poly q y" using qr[rule_format, of y] by simp
-	finally have "poly q x = poly q y" .} 
-      with qnc have False unfolding constant_def by blast}
-    hence rnc: "\<not> constant (poly ?r)" unfolding constant_def by blast
-    from qr[rule_format, of 0] a00  have r01: "poly ?r 0 = 1" by auto
-    {fix w 
-      have "cmod (poly ?r w) < 1 \<longleftrightarrow> cmod (poly q w / ?a0) < 1"
-	using qr[rule_format, of w] a00 by (simp add: divide_inverse mult_ac)
-      also have "\<dots> \<longleftrightarrow> cmod (poly q w) < cmod ?a0"
-	using a00 unfolding norm_divide by (simp add: field_simps)
-      finally have "cmod (poly ?r w) < 1 \<longleftrightarrow> cmod (poly q w) < cmod ?a0" .}
-    note mrmq_eq = this
-    from poly_decompose[OF rnc] obtain k a s where 
-      kas: "a\<noteq>0" "k\<noteq>0" "psize s + k + 1 = psize ?r" 
-      "\<forall>z. poly ?r z = poly ?r 0 + z^k* poly (pCons a s) z" by blast
-    {assume "k + 1 = n"
-      with kas(3) lgqr[symmetric] q(1) n[symmetric] have s0:"s=0" by auto
-      {fix w
-	have "cmod (poly ?r w) = cmod (1 + a * w ^ k)" 
-	  using kas(4)[rule_format, of w] s0 r01 by (simp add: algebra_simps)}
-      note hth = this [symmetric]
-	from reduce_poly_simple[OF kas(1,2)] 
-      have "\<exists>w. cmod (poly ?r w) < 1" unfolding hth by blast}
-    moreover
-    {assume kn: "k+1 \<noteq> n"
-      from kn kas(3) q(1) n[symmetric] lgqr have k1n: "k + 1 < n" by simp
-      have th01: "\<not> constant (poly (pCons 1 (monom a (k - 1))))" 
-	unfolding constant_def poly_pCons poly_monom
-	using kas(1) apply simp 
-	by (rule exI[where x=0], rule exI[where x=1], simp)
-      from kas(1) kas(2) have th02: "k+1 = psize (pCons 1 (monom a (k - 1)))"
-	by (simp add: psize_def degree_monom_eq)
-      from H[rule_format, OF k1n th01 th02]
-      obtain w where w: "1 + w^k * a = 0"
-	unfolding poly_pCons poly_monom
-	using kas(2) by (cases k, auto simp add: algebra_simps)
-      from poly_bound_exists[of "cmod w" s] obtain m where 
-	m: "m > 0" "\<forall>z. cmod z \<le> cmod w \<longrightarrow> cmod (poly s z) \<le> m" by blast
-      have w0: "w\<noteq>0" using kas(2) w by (auto simp add: power_0_left)
-      from w have "(1 + w ^ k * a) - 1 = 0 - 1" by simp
-      then have wm1: "w^k * a = - 1" by simp
-      have inv0: "0 < inverse (cmod w ^ (k + 1) * m)" 
-	using norm_ge_zero[of w] w0 m(1)
-	  by (simp add: inverse_eq_divide zero_less_mult_iff)
-      with real_down2[OF zero_less_one] obtain t where
-	t: "t > 0" "t < 1" "t < inverse (cmod w ^ (k + 1) * m)" by blast
-      let ?ct = "complex_of_real t"
-      let ?w = "?ct * w"
-      have "1 + ?w^k * (a + ?w * poly s ?w) = 1 + ?ct^k * (w^k * a) + ?w^k * ?w * poly s ?w" using kas(1) by (simp add: algebra_simps power_mult_distrib)
-      also have "\<dots> = complex_of_real (1 - t^k) + ?w^k * ?w * poly s ?w"
-	unfolding wm1 by (simp)
-      finally have "cmod (1 + ?w^k * (a + ?w * poly s ?w)) = cmod (complex_of_real (1 - t^k) + ?w^k * ?w * poly s ?w)" 
-	apply -
-	apply (rule cong[OF refl[of cmod]])
-	apply assumption
-	done
-      with norm_triangle_ineq[of "complex_of_real (1 - t^k)" "?w^k * ?w * poly s ?w"] 
-      have th11: "cmod (1 + ?w^k * (a + ?w * poly s ?w)) \<le> \<bar>1 - t^k\<bar> + cmod (?w^k * ?w * poly s ?w)" unfolding norm_of_real by simp 
-      have ath: "\<And>x (t::real). 0\<le> x \<Longrightarrow> x < t \<Longrightarrow> t\<le>1 \<Longrightarrow> \<bar>1 - t\<bar> + x < 1" by arith
-      have "t *cmod w \<le> 1 * cmod w" apply (rule mult_mono) using t(1,2) by auto
-      then have tw: "cmod ?w \<le> cmod w" using t(1) by (simp add: norm_mult) 
-      from t inv0 have "t* (cmod w ^ (k + 1) * m) < 1"
-	by (simp add: inverse_eq_divide field_simps)
-      with zero_less_power[OF t(1), of k] 
-      have th30: "t^k * (t* (cmod w ^ (k + 1) * m)) < t^k * 1" 
-	apply - apply (rule mult_strict_left_mono) by simp_all
-      have "cmod (?w^k * ?w * poly s ?w) = t^k * (t* (cmod w ^ (k+1) * cmod (poly s ?w)))"  using w0 t(1)
-	by (simp add: algebra_simps power_mult_distrib norm_of_real norm_power norm_mult)
-      then have "cmod (?w^k * ?w * poly s ?w) \<le> t^k * (t* (cmod w ^ (k + 1) * m))"
-	using t(1,2) m(2)[rule_format, OF tw] w0
-	apply (simp only: )
-	apply auto
-	apply (rule mult_mono, simp_all add: norm_ge_zero)+
-	apply (simp add: zero_le_mult_iff zero_le_power)
-	done
-      with th30 have th120: "cmod (?w^k * ?w * poly s ?w) < t^k" by simp 
-      from power_strict_mono[OF t(2), of k] t(1) kas(2) have th121: "t^k \<le> 1" 
-	by auto
-      from ath[OF norm_ge_zero[of "?w^k * ?w * poly s ?w"] th120 th121]
-      have th12: "\<bar>1 - t^k\<bar> + cmod (?w^k * ?w * poly s ?w) < 1" . 
-      from th11 th12
-      have "cmod (1 + ?w^k * (a + ?w * poly s ?w)) < 1"  by arith 
-      then have "cmod (poly ?r ?w) < 1" 
-	unfolding kas(4)[rule_format, of ?w] r01 by simp 
-      then have "\<exists>w. cmod (poly ?r w) < 1" by blast}
-    ultimately have cr0_contr: "\<exists>w. cmod (poly ?r w) < 1" by blast
-    from cr0_contr cq0 q(2)
-    have ?ths unfolding mrmq_eq not_less[symmetric] by auto}
-  ultimately show ?ths by blast
-qed
-
-text {* Alternative version with a syntactic notion of constant polynomial. *}
-
-lemma fundamental_theorem_of_algebra_alt:
-  assumes nc: "~(\<exists>a l. a\<noteq> 0 \<and> l = 0 \<and> p = pCons a l)"
-  shows "\<exists>z. poly p z = (0::complex)"
-using nc
-proof(induct p)
-  case (pCons c cs)
-  {assume "c=0" hence ?case by auto}
-  moreover
-  {assume c0: "c\<noteq>0"
-    {assume nc: "constant (poly (pCons c cs))"
-      from nc[unfolded constant_def, rule_format, of 0] 
-      have "\<forall>w. w \<noteq> 0 \<longrightarrow> poly cs w = 0" by auto 
-      hence "cs = 0"
-	proof(induct cs)
-	  case (pCons d ds)
-	  {assume "d=0" hence ?case using pCons.prems pCons.hyps by simp}
-	  moreover
-	  {assume d0: "d\<noteq>0"
-	    from poly_bound_exists[of 1 ds] obtain m where 
-	      m: "m > 0" "\<forall>z. \<forall>z. cmod z \<le> 1 \<longrightarrow> cmod (poly ds z) \<le> m" by blast
-	    have dm: "cmod d / m > 0" using d0 m(1) by (simp add: field_simps)
-	    from real_down2[OF dm zero_less_one] obtain x where 
-	      x: "x > 0" "x < cmod d / m" "x < 1" by blast
-	    let ?x = "complex_of_real x"
-	    from x have cx: "?x \<noteq> 0"  "cmod ?x \<le> 1" by simp_all
-	    from pCons.prems[rule_format, OF cx(1)]
-	    have cth: "cmod (?x*poly ds ?x) = cmod d" by (simp add: eq_diff_eq[symmetric])
-	    from m(2)[rule_format, OF cx(2)] x(1)
-	    have th0: "cmod (?x*poly ds ?x) \<le> x*m"
-	      by (simp add: norm_mult)
-	    from x(2) m(1) have "x*m < cmod d" by (simp add: field_simps)
-	    with th0 have "cmod (?x*poly ds ?x) \<noteq> cmod d" by auto
-	    with cth  have ?case by blast}
-	  ultimately show ?case by blast 
-	qed simp}
-      then have nc: "\<not> constant (poly (pCons c cs))" using pCons.prems c0 
-	by blast
-      from fundamental_theorem_of_algebra[OF nc] have ?case .}
-  ultimately show ?case by blast  
-qed simp
-
-subsection {* Order of polynomial roots *}
-
-definition
-  order :: "'a::{idom,recpower} \<Rightarrow> 'a poly \<Rightarrow> nat"
-where
-  [code del]:
-  "order a p = (LEAST n. \<not> [:-a, 1:] ^ Suc n dvd p)"
-
-lemma degree_power_le: "degree (p ^ n) \<le> degree p * n"
-by (induct n, simp, auto intro: order_trans degree_mult_le)
-
-lemma coeff_linear_power:
-  fixes a :: "'a::{comm_semiring_1,recpower}"
-  shows "coeff ([:a, 1:] ^ n) n = 1"
-apply (induct n, simp_all)
-apply (subst coeff_eq_0)
-apply (auto intro: le_less_trans degree_power_le)
-done
-
-lemma degree_linear_power:
-  fixes a :: "'a::{comm_semiring_1,recpower}"
-  shows "degree ([:a, 1:] ^ n) = n"
-apply (rule order_antisym)
-apply (rule ord_le_eq_trans [OF degree_power_le], simp)
-apply (rule le_degree, simp add: coeff_linear_power)
-done
-
-lemma order_1: "[:-a, 1:] ^ order a p dvd p"
-apply (cases "p = 0", simp)
-apply (cases "order a p", simp)
-apply (subgoal_tac "nat < (LEAST n. \<not> [:-a, 1:] ^ Suc n dvd p)")
-apply (drule not_less_Least, simp)
-apply (fold order_def, simp)
-done
-
-lemma order_2: "p \<noteq> 0 \<Longrightarrow> \<not> [:-a, 1:] ^ Suc (order a p) dvd p"
-unfolding order_def
-apply (rule LeastI_ex)
-apply (rule_tac x="degree p" in exI)
-apply (rule notI)
-apply (drule (1) dvd_imp_degree_le)
-apply (simp only: degree_linear_power)
-done
-
-lemma order:
-  "p \<noteq> 0 \<Longrightarrow> [:-a, 1:] ^ order a p dvd p \<and> \<not> [:-a, 1:] ^ Suc (order a p) dvd p"
-by (rule conjI [OF order_1 order_2])
-
-lemma order_degree:
-  assumes p: "p \<noteq> 0"
-  shows "order a p \<le> degree p"
-proof -
-  have "order a p = degree ([:-a, 1:] ^ order a p)"
-    by (simp only: degree_linear_power)
-  also have "\<dots> \<le> degree p"
-    using order_1 p by (rule dvd_imp_degree_le)
-  finally show ?thesis .
-qed
-
-lemma order_root: "poly p a = 0 \<longleftrightarrow> p = 0 \<or> order a p \<noteq> 0"
-apply (cases "p = 0", simp_all)
-apply (rule iffI)
-apply (rule ccontr, simp)
-apply (frule order_2 [where a=a], simp)
-apply (simp add: poly_eq_0_iff_dvd)
-apply (simp add: poly_eq_0_iff_dvd)
-apply (simp only: order_def)
-apply (drule not_less_Least, simp)
-done
-
-lemma UNIV_nat_infinite:
-  "\<not> finite (UNIV :: nat set)" (is "\<not> finite ?U")
-proof
-  assume "finite ?U"
-  moreover have "Suc (Max ?U) \<in> ?U" ..
-  ultimately have "Suc (Max ?U) \<le> Max ?U" by (rule Max_ge)
-  then show "False" by simp
-qed
-
-lemma UNIV_char_0_infinite:
-  "\<not> finite (UNIV::'a::semiring_char_0 set)"
-proof
-  assume "finite (UNIV::'a set)"
-  with subset_UNIV have "finite (range of_nat::'a set)"
-    by (rule finite_subset)
-  moreover have "inj (of_nat::nat \<Rightarrow> 'a)"
-    by (simp add: inj_on_def)
-  ultimately have "finite (UNIV::nat set)"
-    by (rule finite_imageD)
-  then show "False"
-    by (simp add: UNIV_nat_infinite)
-qed
-
-lemma poly_zero:
-  fixes p :: "'a::{idom,ring_char_0} poly"
-  shows "poly p = poly 0 \<longleftrightarrow> p = 0"
-apply (cases "p = 0", simp_all)
-apply (drule poly_roots_finite)
-apply (auto simp add: UNIV_char_0_infinite)
-done
-
-lemma poly_eq_iff:
-  fixes p q :: "'a::{idom,ring_char_0} poly"
-  shows "poly p = poly q \<longleftrightarrow> p = q"
-  using poly_zero [of "p - q"]
-  by (simp add: expand_fun_eq)
-
-
-subsection{* Nullstellenstatz, degrees and divisibility of polynomials *}
-
-lemma nullstellensatz_lemma:
-  fixes p :: "complex poly"
-  assumes "\<forall>x. poly p x = 0 \<longrightarrow> poly q x = 0"
-  and "degree p = n" and "n \<noteq> 0"
-  shows "p dvd (q ^ n)"
-using prems
-proof(induct n arbitrary: p q rule: nat_less_induct)
-  fix n::nat fix p q :: "complex poly"
-  assume IH: "\<forall>m<n. \<forall>p q.
-                 (\<forall>x. poly p x = (0::complex) \<longrightarrow> poly q x = 0) \<longrightarrow>
-                 degree p = m \<longrightarrow> m \<noteq> 0 \<longrightarrow> p dvd (q ^ m)"
-    and pq0: "\<forall>x. poly p x = 0 \<longrightarrow> poly q x = 0" 
-    and dpn: "degree p = n" and n0: "n \<noteq> 0"
-  from dpn n0 have pne: "p \<noteq> 0" by auto
-  let ?ths = "p dvd (q ^ n)"
-  {fix a assume a: "poly p a = 0"
-    {assume oa: "order a p \<noteq> 0"
-      let ?op = "order a p"
-      from pne have ap: "([:- a, 1:] ^ ?op) dvd p" 
-	"\<not> [:- a, 1:] ^ (Suc ?op) dvd p" using order by blast+ 
-      note oop = order_degree[OF pne, unfolded dpn]
-      {assume q0: "q = 0"
-	hence ?ths using n0
-          by (simp add: power_0_left)}
-      moreover
-      {assume q0: "q \<noteq> 0"
-	from pq0[rule_format, OF a, unfolded poly_eq_0_iff_dvd]
-	obtain r where r: "q = [:- a, 1:] * r" by (rule dvdE)
-	from ap(1) obtain s where
-	  s: "p = [:- a, 1:] ^ ?op * s" by (rule dvdE)
-	have sne: "s \<noteq> 0"
-	  using s pne by auto
-	{assume ds0: "degree s = 0"
-	  from ds0 have "\<exists>k. s = [:k:]"
-            by (cases s, simp split: if_splits)
-	  then obtain k where kpn: "s = [:k:]" by blast
-          from sne kpn have k: "k \<noteq> 0" by simp
-	  let ?w = "([:1/k:] * ([:-a,1:] ^ (n - ?op))) * (r ^ n)"
-          from k oop [of a] have "q ^ n = p * ?w"
-            apply -
-            apply (subst r, subst s, subst kpn)
-            apply (subst power_mult_distrib, simp)
-            apply (subst power_add [symmetric], simp)
-            done
-	  hence ?ths unfolding dvd_def by blast}
-	moreover
-	{assume ds0: "degree s \<noteq> 0"
-	  from ds0 sne dpn s oa
-	    have dsn: "degree s < n" apply auto
-              apply (erule ssubst)
-              apply (simp add: degree_mult_eq degree_linear_power)
-              done
-	    {fix x assume h: "poly s x = 0"
-	      {assume xa: "x = a"
-		from h[unfolded xa poly_eq_0_iff_dvd] obtain u where
-		  u: "s = [:- a, 1:] * u" by (rule dvdE)
-		have "p = [:- a, 1:] ^ (Suc ?op) * u"
-                  by (subst s, subst u, simp only: power_Suc mult_ac)
-		with ap(2)[unfolded dvd_def] have False by blast}
-	      note xa = this
-	      from h have "poly p x = 0" by (subst s, simp)
-	      with pq0 have "poly q x = 0" by blast
-	      with r xa have "poly r x = 0"
-                by (auto simp add: uminus_add_conv_diff)}
-	    note impth = this
-	    from IH[rule_format, OF dsn, of s r] impth ds0
-	    have "s dvd (r ^ (degree s))" by blast
-	    then obtain u where u: "r ^ (degree s) = s * u" ..
-	    hence u': "\<And>x. poly s x * poly u x = poly r x ^ degree s"
-              by (simp only: poly_mult[symmetric] poly_power[symmetric])
-	    let ?w = "(u * ([:-a,1:] ^ (n - ?op))) * (r ^ (n - degree s))"
-	    from oop[of a] dsn have "q ^ n = p * ?w"
-              apply -
-              apply (subst s, subst r)
-              apply (simp only: power_mult_distrib)
-              apply (subst mult_assoc [where b=s])
-              apply (subst mult_assoc [where a=u])
-              apply (subst mult_assoc [where b=u, symmetric])
-              apply (subst u [symmetric])
-              apply (simp add: mult_ac power_add [symmetric])
-              done
-	    hence ?ths unfolding dvd_def by blast}
-      ultimately have ?ths by blast }
-      ultimately have ?ths by blast}
-    then have ?ths using a order_root pne by blast}
-  moreover
-  {assume exa: "\<not> (\<exists>a. poly p a = 0)"
-    from fundamental_theorem_of_algebra_alt[of p] exa obtain c where
-      ccs: "c\<noteq>0" "p = pCons c 0" by blast
-    
-    then have pp: "\<And>x. poly p x =  c" by simp
-    let ?w = "[:1/c:] * (q ^ n)"
-    from ccs
-    have "(q ^ n) = (p * ?w) "
-      by (simp add: smult_smult)
-    hence ?ths unfolding dvd_def by blast}
-  ultimately show ?ths by blast
-qed
-
-lemma nullstellensatz_univariate:
-  "(\<forall>x. poly p x = (0::complex) \<longrightarrow> poly q x = 0) \<longleftrightarrow> 
-    p dvd (q ^ (degree p)) \<or> (p = 0 \<and> q = 0)"
-proof-
-  {assume pe: "p = 0"
-    hence eq: "(\<forall>x. poly p x = (0::complex) \<longrightarrow> poly q x = 0) \<longleftrightarrow> q = 0"
-      apply auto
-      apply (rule poly_zero [THEN iffD1])
-      by (rule ext, simp)
-    {assume "p dvd (q ^ (degree p))"
-      then obtain r where r: "q ^ (degree p) = p * r" ..
-      from r pe have False by simp}
-    with eq pe have ?thesis by blast}
-  moreover
-  {assume pe: "p \<noteq> 0"
-    {assume dp: "degree p = 0"
-      then obtain k where k: "p = [:k:]" "k\<noteq>0" using pe
-        by (cases p, simp split: if_splits)
-      hence th1: "\<forall>x. poly p x \<noteq> 0" by simp
-      from k dp have "q ^ (degree p) = p * [:1/k:]"
-        by (simp add: one_poly_def)
-      hence th2: "p dvd (q ^ (degree p))" ..
-      from th1 th2 pe have ?thesis by blast}
-    moreover
-    {assume dp: "degree p \<noteq> 0"
-      then obtain n where n: "degree p = Suc n " by (cases "degree p", auto)
-      {assume "p dvd (q ^ (Suc n))"
-	then obtain u where u: "q ^ (Suc n) = p * u" ..
-	{fix x assume h: "poly p x = 0" "poly q x \<noteq> 0"
-	  hence "poly (q ^ (Suc n)) x \<noteq> 0" by simp
-	  hence False using u h(1) by (simp only: poly_mult) simp}}
-	with n nullstellensatz_lemma[of p q "degree p"] dp 
-	have ?thesis by auto}
-    ultimately have ?thesis by blast}
-  ultimately show ?thesis by blast
-qed
-
-text{* Useful lemma *}
-
-lemma constant_degree:
-  fixes p :: "'a::{idom,ring_char_0} poly"
-  shows "constant (poly p) \<longleftrightarrow> degree p = 0" (is "?lhs = ?rhs")
-proof
-  assume l: ?lhs
-  from l[unfolded constant_def, rule_format, of _ "0"]
-  have th: "poly p = poly [:poly p 0:]" apply - by (rule ext, simp)
-  then have "p = [:poly p 0:]" by (simp add: poly_eq_iff)
-  then have "degree p = degree [:poly p 0:]" by simp
-  then show ?rhs by simp
-next
-  assume r: ?rhs
-  then obtain k where "p = [:k:]"
-    by (cases p, simp split: if_splits)
-  then show ?lhs unfolding constant_def by auto
-qed
-
-lemma divides_degree: assumes pq: "p dvd (q:: complex poly)"
-  shows "degree p \<le> degree q \<or> q = 0"
-apply (cases "q = 0", simp_all)
-apply (erule dvd_imp_degree_le [OF pq])
-done
-
-(* Arithmetic operations on multivariate polynomials.                        *)
-
-lemma mpoly_base_conv: 
-  "(0::complex) \<equiv> poly 0 x" "c \<equiv> poly [:c:] x" "x \<equiv> poly [:0,1:] x" by simp_all
-
-lemma mpoly_norm_conv: 
-  "poly [:0:] (x::complex) \<equiv> poly 0 x" "poly [:poly 0 y:] x \<equiv> poly 0 x" by simp_all
-
-lemma mpoly_sub_conv: 
-  "poly p (x::complex) - poly q x \<equiv> poly p x + -1 * poly q x"
-  by (simp add: diff_def)
-
-lemma poly_pad_rule: "poly p x = 0 ==> poly (pCons 0 p) x = (0::complex)" by simp
-
-lemma poly_cancel_eq_conv: "p = (0::complex) \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> (q = 0) \<equiv> (a * q - b * p = 0)" apply (atomize (full)) by auto
-
-lemma resolve_eq_raw:  "poly 0 x \<equiv> 0" "poly [:c:] x \<equiv> (c::complex)" by auto
-lemma  resolve_eq_then: "(P \<Longrightarrow> (Q \<equiv> Q1)) \<Longrightarrow> (\<not>P \<Longrightarrow> (Q \<equiv> Q2))
-  \<Longrightarrow> Q \<equiv> P \<and> Q1 \<or> \<not>P\<and> Q2" apply (atomize (full)) by blast 
-
-lemma poly_divides_pad_rule: 
-  fixes p q :: "complex poly"
-  assumes pq: "p dvd q"
-  shows "p dvd (pCons (0::complex) q)"
-proof-
-  have "pCons 0 q = q * [:0,1:]" by simp
-  then have "q dvd (pCons 0 q)" ..
-  with pq show ?thesis by (rule dvd_trans)
-qed
-
-lemma poly_divides_pad_const_rule: 
-  fixes p q :: "complex poly"
-  assumes pq: "p dvd q"
-  shows "p dvd (smult a q)"
-proof-
-  have "smult a q = q * [:a:]" by simp
-  then have "q dvd smult a q" ..
-  with pq show ?thesis by (rule dvd_trans)
-qed
-
-
-lemma poly_divides_conv0:  
-  fixes p :: "complex poly"
-  assumes lgpq: "degree q < degree p" and lq:"p \<noteq> 0"
-  shows "p dvd q \<equiv> q = 0" (is "?lhs \<equiv> ?rhs")
-proof-
-  {assume r: ?rhs 
-    hence "q = p * 0" by simp
-    hence ?lhs ..}
-  moreover
-  {assume l: ?lhs
-    {assume q0: "q = 0"
-      hence ?rhs by simp}
-    moreover
-    {assume q0: "q \<noteq> 0"
-      from l q0 have "degree p \<le> degree q"
-        by (rule dvd_imp_degree_le)
-      with lgpq have ?rhs by simp }
-    ultimately have ?rhs by blast }
-  ultimately show "?lhs \<equiv> ?rhs" by - (atomize (full), blast) 
-qed
-
-lemma poly_divides_conv1: 
-  assumes a0: "a\<noteq> (0::complex)" and pp': "(p::complex poly) dvd p'"
-  and qrp': "smult a q - p' \<equiv> r"
-  shows "p dvd q \<equiv> p dvd (r::complex poly)" (is "?lhs \<equiv> ?rhs")
-proof-
-  {
-  from pp' obtain t where t: "p' = p * t" ..
-  {assume l: ?lhs
-    then obtain u where u: "q = p * u" ..
-     have "r = p * (smult a u - t)"
-       using u qrp' [symmetric] t by (simp add: algebra_simps mult_smult_right)
-     then have ?rhs ..}
-  moreover
-  {assume r: ?rhs
-    then obtain u where u: "r = p * u" ..
-    from u [symmetric] t qrp' [symmetric] a0
-    have "q = p * smult (1/a) (u + t)"
-      by (simp add: algebra_simps mult_smult_right smult_smult)
-    hence ?lhs ..}
-  ultimately have "?lhs = ?rhs" by blast }
-thus "?lhs \<equiv> ?rhs"  by - (atomize(full), blast) 
-qed
-
-lemma basic_cqe_conv1:
-  "(\<exists>x. poly p x = 0 \<and> poly 0 x \<noteq> 0) \<equiv> False"
-  "(\<exists>x. poly 0 x \<noteq> 0) \<equiv> False"
-  "(\<exists>x. poly [:c:] x \<noteq> 0) \<equiv> c\<noteq>0"
-  "(\<exists>x. poly 0 x = 0) \<equiv> True"
-  "(\<exists>x. poly [:c:] x = 0) \<equiv> c = 0" by simp_all
-
-lemma basic_cqe_conv2: 
-  assumes l:"p \<noteq> 0" 
-  shows "(\<exists>x. poly (pCons a (pCons b p)) x = (0::complex)) \<equiv> True"
-proof-
-  {fix h t
-    assume h: "h\<noteq>0" "t=0"  "pCons a (pCons b p) = pCons h t"
-    with l have False by simp}
-  hence th: "\<not> (\<exists> h t. h\<noteq>0 \<and> t=0 \<and> pCons a (pCons b p) = pCons h t)"
-    by blast
-  from fundamental_theorem_of_algebra_alt[OF th] 
-  show "(\<exists>x. poly (pCons a (pCons b p)) x = (0::complex)) \<equiv> True" by auto
-qed
-
-lemma  basic_cqe_conv_2b: "(\<exists>x. poly p x \<noteq> (0::complex)) \<equiv> (p \<noteq> 0)"
-proof-
-  have "p = 0 \<longleftrightarrow> poly p = poly 0"
-    by (simp add: poly_zero)
-  also have "\<dots> \<longleftrightarrow> (\<not> (\<exists>x. poly p x \<noteq> 0))" by (auto intro: ext)
-  finally show "(\<exists>x. poly p x \<noteq> (0::complex)) \<equiv> p \<noteq> 0"
-    by - (atomize (full), blast)
-qed
-
-lemma basic_cqe_conv3:
-  fixes p q :: "complex poly"
-  assumes l: "p \<noteq> 0" 
-  shows "(\<exists>x. poly (pCons a p) x = 0 \<and> poly q x \<noteq> 0) \<equiv> \<not> ((pCons a p) dvd (q ^ (psize p)))"
-proof-
-  from l have dp:"degree (pCons a p) = psize p" by (simp add: psize_def)
-  from nullstellensatz_univariate[of "pCons a p" q] l
-  show "(\<exists>x. poly (pCons a p) x = 0 \<and> poly q x \<noteq> 0) \<equiv> \<not> ((pCons a p) dvd (q ^ (psize p)))"
-    unfolding dp
-    by - (atomize (full), auto)
-qed
-
-lemma basic_cqe_conv4:
-  fixes p q :: "complex poly"
-  assumes h: "\<And>x. poly (q ^ n) x \<equiv> poly r x"
-  shows "p dvd (q ^ n) \<equiv> p dvd r"
-proof-
-  from h have "poly (q ^ n) = poly r" by (auto intro: ext)
-  then have "(q ^ n) = r" by (simp add: poly_eq_iff)
-  thus "p dvd (q ^ n) \<equiv> p dvd r" by simp
-qed
-
-lemma pmult_Cons_Cons: "(pCons (a::complex) (pCons b p) * q = (smult a q) + (pCons 0 (pCons b p * q)))"
-  by simp
-
-lemma elim_neg_conv: "- z \<equiv> (-1) * (z::complex)" by simp
-lemma eqT_intr: "PROP P \<Longrightarrow> (True \<Longrightarrow> PROP P )" "PROP P \<Longrightarrow> True" by blast+
-lemma negate_negate_rule: "Trueprop P \<equiv> \<not> P \<equiv> False" by (atomize (full), auto)
-
-lemma complex_entire: "(z::complex) \<noteq> 0 \<and> w \<noteq> 0 \<equiv> z*w \<noteq> 0" by simp
-lemma resolve_eq_ne: "(P \<equiv> True) \<equiv> (\<not>P \<equiv> False)" "(P \<equiv> False) \<equiv> (\<not>P \<equiv> True)" 
-  by (atomize (full)) simp_all
-lemma cqe_conv1: "poly 0 x = 0 \<longleftrightarrow> True"  by simp
-lemma cqe_conv2: "(p \<Longrightarrow> (q \<equiv> r)) \<equiv> ((p \<and> q) \<equiv> (p \<and> r))"  (is "?l \<equiv> ?r")
-proof
-  assume "p \<Longrightarrow> q \<equiv> r" thus "p \<and> q \<equiv> p \<and> r" apply - apply (atomize (full)) by blast
-next
-  assume "p \<and> q \<equiv> p \<and> r" "p"
-  thus "q \<equiv> r" apply - apply (atomize (full)) apply blast done
-qed
-lemma poly_const_conv: "poly [:c:] (x::complex) = y \<longleftrightarrow> c = y" by simp
-
-end
--- a/src/HOL/IsaMakefile	Mon Feb 16 10:13:30 2009 +0100
+++ b/src/HOL/IsaMakefile	Mon Feb 16 10:15:43 2009 +0100
@@ -216,7 +216,6 @@
   Main.thy \
   Map.thy \
   NatBin.thy \
-  Nat_Int_Bij.thy \
   Presburger.thy \
   Recdef.thy \
   Relation_Power.thy \
@@ -270,7 +269,6 @@
 $(OUT)/HOL: ROOT.ML $(MAIN_DEPENDENCIES) \
   Complex_Main.thy \
   Complex.thy \
-  Fundamental_Theorem_Algebra.thy \
   Deriv.thy \
   Fact.thy \
   FrechetDeriv.thy \
@@ -317,8 +315,9 @@
   Library/Executable_Set.thy Library/Infinite_Set.thy			\
   Library/FuncSet.thy Library/Permutations.thy Library/Determinants.thy\
   Library/Finite_Cartesian_Product.thy \
+  Library/Fundamental_Theorem_Algebra.thy \
   Library/Library.thy Library/List_Prefix.thy Library/State_Monad.thy	\
-  Library/Multiset.thy Library/Permutation.thy	\
+  Library/Nat_Int_Bij.thy Library/Multiset.thy Library/Permutation.thy	\
   Library/Primes.thy Library/Pocklington.thy Library/Quotient.thy	\
   Library/Quicksort.thy Library/Nat_Infinity.thy Library/Word.thy	\
   Library/README.html Library/Continuity.thy Library/Order_Relation.thy \
--- a/src/HOL/Library/Binomial.thy	Mon Feb 16 10:13:30 2009 +0100
+++ b/src/HOL/Library/Binomial.thy	Mon Feb 16 10:15:43 2009 +0100
@@ -106,6 +106,21 @@
   apply (erule rev_mp, subst card_Diff_singleton)
   apply (auto intro: finite_subset)
   done
+(*
+lemma "finite(UN y. {x. P x y})"
+apply simp
+lemma Collect_ex_eq
+
+lemma "{x. EX y. P x y} = (UN y. {x. P x y})"
+apply blast
+*)
+
+lemma finite_bex_subset[simp]:
+  "finite B \<Longrightarrow> (!!A. A<=B \<Longrightarrow> finite{x. P x A}) \<Longrightarrow> finite{x. EX A<=B. P x A}"
+apply(subgoal_tac "{x. EX A<=B. P x A} = (UN A:Pow B. {x. P x A})")
+ apply simp
+apply blast
+done
 
 text{*There are as many subsets of @{term A} having cardinality @{term k}
  as there are sets obtained from the former by inserting a fixed element
@@ -114,14 +129,10 @@
    "[|finite A; x \<notin> A|] ==>
     card {B. EX C. C <= A & card(C) = k & B = insert x C} =
     card {B. B <= A & card(B) = k}"
-  apply (rule_tac f = "%s. s - {x}" and g = "insert x" in card_bij_eq)
-       apply (auto elim!: equalityE simp add: inj_on_def)
-    apply (subst Diff_insert0, auto)
-   txt {* finiteness of the two sets *}
-   apply (rule_tac [2] B = "Pow (A)" in finite_subset)
-   apply (rule_tac B = "Pow (insert x A)" in finite_subset)
-   apply fast+
-  done
+apply (rule_tac f = "%s. s - {x}" and g = "insert x" in card_bij_eq)
+     apply (auto elim!: equalityE simp add: inj_on_def)
+apply (subst Diff_insert0, auto)
+done
 
 text {*
   Main theorem: combinatorial statement about number of subsets of a set.
@@ -182,7 +193,7 @@
   finally show ?case by simp
 qed
 
-section{* Pochhammer's symbol : generalized raising factorial*}
+subsection{* Pochhammer's symbol : generalized raising factorial*}
 
 definition "pochhammer (a::'a::comm_semiring_1) n = (if n = 0 then 1 else setprod (\<lambda>n. a + of_nat n) {0 .. n - 1})"
 
@@ -285,7 +296,7 @@
     pochhammer_of_nat_eq_0_lemma'[of k n, where ?'a = 'a]
   by (auto simp add: not_le[symmetric])
 
-section{* Generalized binomial coefficients *}
+subsection{* Generalized binomial coefficients *}
 
 definition gbinomial :: "'a::{field, recpower,ring_char_0} \<Rightarrow> nat \<Rightarrow> 'a" (infixl "gchoose" 65)
   where "a gchoose n = (if n = 0 then 1 else (setprod (\<lambda>i. a - of_nat i) {0 .. n - 1}) / of_nat (fact n))"
--- a/src/HOL/Library/Countable.thy	Mon Feb 16 10:13:30 2009 +0100
+++ b/src/HOL/Library/Countable.thy	Mon Feb 16 10:15:43 2009 +0100
@@ -5,7 +5,12 @@
 header {* Encoding (almost) everything into natural numbers *}
 
 theory Countable
-imports Plain "~~/src/HOL/List" "~~/src/HOL/Hilbert_Choice"
+imports
+  Plain
+  "~~/src/HOL/List"
+  "~~/src/HOL/Hilbert_Choice"
+  "~~/src/HOL/Nat_Int_Bij"
+  "~~/src/HOL/Rational"
 begin
 
 subsection {* The class of countable types *}
@@ -34,6 +39,9 @@
 lemma inj_to_nat [simp]: "inj to_nat"
   by (rule exE_some [OF ex_inj]) (simp add: to_nat_def)
 
+lemma surj_from_nat [simp]: "surj from_nat"
+  unfolding from_nat_def by (simp add: inj_imp_surj_inv)
+
 lemma to_nat_split [simp]: "to_nat x = to_nat y \<longleftrightarrow> x = y"
   using injD [OF inj_to_nat] by auto
 
@@ -193,4 +201,55 @@
   qed
 qed
 
+
+subsection {* The Rationals are Countably Infinite *}
+
+definition nat_to_rat_surj :: "nat \<Rightarrow> rat" where
+"nat_to_rat_surj n = (let (a,b) = nat_to_nat2 n
+                      in Fract (nat_to_int_bij a) (nat_to_int_bij b))"
+
+lemma surj_nat_to_rat_surj: "surj nat_to_rat_surj"
+unfolding surj_def
+proof
+  fix r::rat
+  show "\<exists>n. r = nat_to_rat_surj n"
+  proof(cases r)
+    fix i j assume [simp]: "r = Fract i j" and "j \<noteq> 0"
+    have "r = (let m = inv nat_to_int_bij i; n = inv nat_to_int_bij j
+               in nat_to_rat_surj(nat2_to_nat (m,n)))"
+      using nat2_to_nat_inj surj_f_inv_f[OF surj_nat_to_int_bij]
+      by(simp add:Let_def nat_to_rat_surj_def nat_to_nat2_def)
+    thus "\<exists>n. r = nat_to_rat_surj n" by(auto simp:Let_def)
+  qed
+qed
+
+lemma Rats_eq_range_nat_to_rat_surj: "\<rat> = range nat_to_rat_surj"
+by (simp add: Rats_def surj_nat_to_rat_surj surj_range)
+
+context field_char_0
+begin
+
+lemma Rats_eq_range_of_rat_o_nat_to_rat_surj:
+  "\<rat> = range (of_rat o nat_to_rat_surj)"
+using surj_nat_to_rat_surj
+by (auto simp: Rats_def image_def surj_def)
+   (blast intro: arg_cong[where f = of_rat])
+
+lemma surj_of_rat_nat_to_rat_surj:
+  "r\<in>\<rat> \<Longrightarrow> \<exists>n. r = of_rat(nat_to_rat_surj n)"
+by(simp add: Rats_eq_range_of_rat_o_nat_to_rat_surj image_def)
+
 end
+
+instance rat :: countable
+proof
+  show "\<exists>to_nat::rat \<Rightarrow> nat. inj to_nat"
+  proof
+    have "surj nat_to_rat_surj"
+      by (rule surj_nat_to_rat_surj)
+    then show "inj (inv nat_to_rat_surj)"
+      by (rule surj_imp_inj_inv)
+  qed
+qed
+
+end
--- a/src/HOL/Library/Euclidean_Space.thy	Mon Feb 16 10:13:30 2009 +0100
+++ b/src/HOL/Library/Euclidean_Space.thy	Mon Feb 16 10:15:43 2009 +0100
@@ -40,7 +40,7 @@
 lemma setsum_3: "setsum f {1::nat..3} = f 1 + f 2 + f 3" 
   by (simp add: nat_number  atLeastAtMostSuc_conv add_commute)
 
-section{* Basic componentwise operations on vectors. *}
+subsection{* Basic componentwise operations on vectors. *}
 
 instantiation "^" :: (plus,type) plus
 begin
@@ -84,7 +84,7 @@
 instance by (intro_classes)
 end
 
-text{* Also the scalar-vector multiplication. FIXME: We should unify this with the scalar multiplication in real_vector *}
+text{* Also the scalar-vector multiplication. FIXME: We should unify this with the scalar multiplication in @{text real_vector} *}
 
 definition vector_scalar_mult:: "'a::times \<Rightarrow> 'a ^'n \<Rightarrow> 'a ^ 'n" (infixr "*s" 75)
   where "c *s x = (\<chi> i. c * (x$i))"
@@ -106,7 +106,7 @@
 lemma dot_3[simp]: "(x::'a::{comm_monoid_add, times}^3) \<bullet> y = (x$1) * (y$1) + (x$2) * (y$2) + (x$3) * (y$3)"
   by (simp add: dot_def dimindex_def nat_number)
 
-section {* A naive proof procedure to lift really trivial arithmetic stuff from the basis of the vector space. *}
+subsection {* A naive proof procedure to lift really trivial arithmetic stuff from the basis of the vector space. *}
 
 lemmas Cart_lambda_beta' = Cart_lambda_beta[rule_format]
 method_setup vector = {*
@@ -236,6 +236,7 @@
   apply (intro_classes) by (vector ring_simps)+
 
 instance "^" :: (comm_semiring_0,type) comm_semiring_0 by (intro_classes) 
+instance "^" :: (cancel_comm_monoid_add, type) cancel_comm_monoid_add ..
 instance "^" :: (semiring_0_cancel,type) semiring_0_cancel by (intro_classes) 
 instance "^" :: (comm_semiring_0_cancel,type) comm_semiring_0_cancel by (intro_classes) 
 instance "^" :: (ring,type) ring by (intro_classes) 
@@ -456,7 +457,7 @@
     ultimately show ?thesis using alb by metis
 qed
 
-text{* One immediately useful corollary is the existence of square roots! --- Should help to get rid of all the development of square-root for reals as a special case real ^1 *}
+text{* One immediately useful corollary is the existence of square roots! --- Should help to get rid of all the development of square-root for reals as a special case @{typ "real^1"} *}
 
 lemma square_bound_lemma: "(x::real) < (1 + x) * (1 + x)"
 proof-
@@ -1737,7 +1738,7 @@
   using u
   by (simp add: vector_eq_rdot[symmetric] adjoint_clauses[OF lf])
 
-text{* Matrix notation. NB: an MxN matrix is of type 'a^'n^'m, not 'a^'m^'n *}
+text{* Matrix notation. NB: an MxN matrix is of type @{typ "'a^'n^'m"}, not @{typ "'a^'m^'n"} *}
 
 consts generic_mult :: "'a \<Rightarrow> 'b \<Rightarrow> 'c" (infixr "\<star>" 75)
 
--- a/src/HOL/Library/Finite_Cartesian_Product.thy	Mon Feb 16 10:13:30 2009 +0100
+++ b/src/HOL/Library/Finite_Cartesian_Product.thy	Mon Feb 16 10:15:43 2009 +0100
@@ -33,7 +33,7 @@
 by (simp add: dimindex_def hassize_def)
 
 
-section{* An indexing type parametrized by base type. *}
+subsection{* An indexing type parametrized by base type. *}
 
 typedef 'a finite_image = "{1 .. DIM('a)}"
   using dimindex_ge_1 by auto
--- a/src/HOL/Library/Formal_Power_Series.thy	Mon Feb 16 10:13:30 2009 +0100
+++ b/src/HOL/Library/Formal_Power_Series.thy	Mon Feb 16 10:15:43 2009 +0100
@@ -9,440 +9,387 @@
   imports Main Fact Parity
 begin
 
-section {* The type of formal power series*}
+subsection {* The type of formal power series*}
+
+typedef (open) 'a fps = "{f :: nat \<Rightarrow> 'a. True}"
+  morphisms fps_nth Abs_fps
+  by simp
+
+notation fps_nth (infixl "$" 75)
 
-typedef 'a fps = "{f :: nat \<Rightarrow> 'a. True}"
-  by simp
+lemma expand_fps_eq: "p = q \<longleftrightarrow> (\<forall>n. p $ n = q $ n)"
+  by (simp add: fps_nth_inject [symmetric] expand_fun_eq)
+
+lemma fps_ext: "(\<And>n. p $ n = q $ n) \<Longrightarrow> p = q"
+  by (simp add: expand_fps_eq)
+
+lemma fps_nth_Abs_fps [simp]: "Abs_fps f $ n = f n"
+  by (simp add: Abs_fps_inverse)
 
 text{* Definition of the basic elements 0 and 1 and the basic operations of addition, negation and multiplication *}
 
 instantiation fps :: (zero)  zero
 begin
 
-definition fps_zero_def: "(0 :: 'a fps) \<equiv> Abs_fps (\<lambda>(n::nat). 0)"
+definition fps_zero_def:
+  "0 = Abs_fps (\<lambda>n. 0)"
+
 instance ..
 end
 
+lemma fps_zero_nth [simp]: "0 $ n = 0"
+  unfolding fps_zero_def by simp
+
 instantiation fps :: ("{one,zero}")  one
 begin
 
-definition fps_one_def: "(1 :: 'a fps) \<equiv> Abs_fps (\<lambda>(n::nat). if n = 0 then 1 else 0)"
+definition fps_one_def:
+  "1 = Abs_fps (\<lambda>n. if n = 0 then 1 else 0)"
+
 instance ..
 end
 
+lemma fps_one_nth [simp]: "1 $ n = (if n = 0 then 1 else 0)" 
+  unfolding fps_one_def by simp
+
 instantiation fps :: (plus)  plus
 begin
 
-definition fps_plus_def: "op + \<equiv> (\<lambda>(f::'a fps) (g:: 'a fps). Abs_fps (\<lambda>(n::nat). Rep_fps (f) n + Rep_fps (g) n))"
+definition fps_plus_def:
+  "op + = (\<lambda>f g. Abs_fps (\<lambda>n. f $ n + g $ n))"
+
 instance ..
 end
 
-instantiation fps :: (minus)  minus
+lemma fps_add_nth [simp]: "(f + g) $ n = f $ n + g $ n"
+  unfolding fps_plus_def by simp
+
+instantiation fps :: (minus) minus
 begin
 
-definition fps_minus_def: "op - \<equiv> (\<lambda>(f::'a fps) (g:: 'a fps). Abs_fps (\<lambda>(n::nat). Rep_fps (f) n - Rep_fps (g) n))"
+definition fps_minus_def:
+  "op - = (\<lambda>f g. Abs_fps (\<lambda>n. f $ n - g $ n))"
+
 instance ..
 end
 
-instantiation fps :: (uminus)  uminus
+lemma fps_sub_nth [simp]: "(f - g) $ n = f $ n - g $ n"
+  unfolding fps_minus_def by simp
+
+instantiation fps :: (uminus) uminus
 begin
 
-definition fps_uminus_def: "uminus \<equiv> (\<lambda>(f::'a fps). Abs_fps (\<lambda>(n::nat). - Rep_fps (f) n))"
+definition fps_uminus_def:
+  "uminus = (\<lambda>f. Abs_fps (\<lambda>n. - (f $ n)))"
+
 instance ..
 end
 
+lemma fps_neg_nth [simp]: "(- f) $ n = - (f $ n)"
+  unfolding fps_uminus_def by simp
+
 instantiation fps :: ("{comm_monoid_add, times}")  times
 begin
 
-definition fps_times_def: 
-  "op * \<equiv> (\<lambda>(f::'a fps) (g:: 'a fps). Abs_fps (\<lambda>(n::nat). setsum (\<lambda>i. Rep_fps (f) i  * Rep_fps (g) (n - i)) {0.. n}))"
+definition fps_times_def:
+  "op * = (\<lambda>f g. Abs_fps (\<lambda>n. \<Sum>i=0..n. f $ i * g $ (n - i)))"
+
 instance ..
 end
 
-text{* Some useful theorems to get rid of Abs and Rep *}
+lemma fps_mult_nth: "(f * g) $ n = (\<Sum>i=0..n. f$i * g$(n - i))"
+  unfolding fps_times_def by simp
 
-lemma mem_fps_set_trivial[intro, simp]: "f \<in> fps" unfolding fps_def by blast
-lemma Rep_fps_Abs_fps[simp]: "Rep_fps (Abs_fps f) = f" 
-  by (blast intro: Abs_fps_inverse) 
-lemma Abs_fps_Rep_fps[simp]: "Abs_fps (Rep_fps f) = f" 
-  by (blast intro: Rep_fps_inverse) 
-lemma Abs_fps_eq[simp]: "Abs_fps f = Abs_fps g \<longleftrightarrow> f = g"
-proof-
-  {assume "f = g" hence "Abs_fps f = Abs_fps g" by simp}
-  moreover
-  {assume a: "Abs_fps f = Abs_fps g"
-    from a have "Rep_fps (Abs_fps f) = Rep_fps (Abs_fps g)" by simp
-    hence "f = g" by simp}
-  ultimately show ?thesis by blast
-qed
-
-lemma Rep_fps_eq[simp]: "Rep_fps f = Rep_fps g \<longleftrightarrow> f = g"
-proof-
-  {assume "Rep_fps f = Rep_fps g" 
-    hence "Abs_fps (Rep_fps f) = Abs_fps (Rep_fps g)" by simp hence "f=g" by simp}
-  moreover
-  {assume "f = g" hence "Rep_fps f = Rep_fps g" by simp}
-  ultimately show ?thesis by blast
-qed
-
-declare atLeastAtMost_iff[presburger] 
+declare atLeastAtMost_iff[presburger]
 declare Bex_def[presburger]
 declare Ball_def[presburger]
 
+lemma mult_delta_left:
+  fixes x y :: "'a::mult_zero"
+  shows "(if b then x else 0) * y = (if b then x * y else 0)"
+  by simp
+
+lemma mult_delta_right:
+  fixes x y :: "'a::mult_zero"
+  shows "x * (if b then y else 0) = (if b then x * y else 0)"
+  by simp
+
 lemma cond_value_iff: "f (if b then x else y) = (if b then f x else f y)"
   by auto
 lemma cond_application_beta: "(if b then f else g) x = (if b then f x else g x)"
   by auto
 
-section{* Formal power series form a commutative ring with unity, if the range of sequences 
+subsection{* Formal power series form a commutative ring with unity, if the range of sequences 
   they represent is a commutative ring with unity*}
 
-instantiation fps :: (semigroup_add) semigroup_add
-begin
-
-instance
+instance fps :: (semigroup_add) semigroup_add
 proof
   fix a b c :: "'a fps" show "a + b + c = a + (b + c)"
-    by (auto simp add: fps_plus_def expand_fun_eq add_assoc)
+    by (simp add: fps_ext add_assoc)
 qed
 
-end
-
-instantiation fps :: (ab_semigroup_add) ab_semigroup_add
-begin
+instance fps :: (ab_semigroup_add) ab_semigroup_add
+proof
+  fix a b :: "'a fps" show "a + b = b + a"
+    by (simp add: fps_ext add_commute)
+qed
 
-instance by (intro_classes, simp add: fps_plus_def expand_fun_eq add_commute)
-end
+lemma fps_mult_assoc_lemma:
+  fixes k :: nat and f :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> 'a::comm_monoid_add"
+  shows "(\<Sum>j=0..k. \<Sum>i=0..j. f i (j - i) (n - j)) =
+         (\<Sum>j=0..k. \<Sum>i=0..k - j. f j i (n - j - i))"
+proof (induct k)
+  case 0 show ?case by simp
+next
+  case (Suc k) thus ?case
+    by (simp add: Suc_diff_le setsum_addf add_assoc
+             cong: strong_setsum_cong)
+qed
 
-instantiation fps :: (semiring_1) semigroup_mult
-begin
-
-instance
+instance fps :: (semiring_0) semigroup_mult
 proof
   fix a b c :: "'a fps"
-  let ?a = "Rep_fps a"
-  let ?b = "Rep_fps b"
-  let ?c = "Rep_fps c"
-  let ?x = "\<lambda> i k. if k \<le> i then (1::'a) else 0" 
-  show "a*b*c = a* (b * c)"
-  proof(auto simp add: fps_times_def setsum_right_distrib setsum_left_distrib, rule ext)
-    fix n::nat
-    let ?r = "\<lambda>i. n - i"
-    have i: "inj_on ?r {0..n}" by (auto simp add: inj_on_def)
-    have ri: "{0 .. n} = ?r ` {0..n}" apply (auto simp add: image_iff)
-      by presburger
-    let ?f = "\<lambda>i j. ?a j * ?b (i - j) * ?c (n -i)"
-    let ?g = "\<lambda>i j. ?a i * (?b j * ?c (n - (i + j)))"
-    have "setsum (\<lambda>i. setsum (?f i) {0..i}) {0..n} 
-      = setsum (\<lambda>i. setsum (\<lambda>j. ?f i j * ?x i j) {0..i}) {0..n}"
-      by (rule setsum_cong2)+ auto
-    also have "\<dots> = setsum (\<lambda>i. setsum (\<lambda>j. ?f i j * ?x i j) {0..n}) {0..n}"
-    proof(rule setsum_cong2)
-      fix i assume i: "i \<in> {0..n}"
-      have eq: "{0 .. n} = {0 ..i} \<union> {i+1 .. n}" using i by auto
-      have d: "{0 ..i} \<inter> {i+1 .. n} = {}" using i by auto
-      have f: "finite {0..i}" "finite {i+1 ..n}" by auto
-      have s0: "setsum (\<lambda>j. ?f i j * ?x i j) {i+1 ..n} = 0" by simp
-      show "setsum (\<lambda>j. ?f i j * ?x i j) {0..i} = setsum (\<lambda>j. ?f i j * ?x i j) {0..n}"
-	unfolding eq setsum_Un_disjoint[OF f d] s0
-	by simp
-    qed
-    also have "\<dots> = setsum (\<lambda>i. setsum (\<lambda>j. ?f j i * ?x j i) {0 .. n}) {0 .. n}"
-      by (rule setsum_commute)
-    also have "\<dots> = setsum (\<lambda>i. setsum (\<lambda>j. ?f j i * ?x j i) {i .. n}) {0 .. n}"
-      apply(rule setsum_cong2)
-      apply (rule setsum_mono_zero_right)
-      apply auto
-      done
-    also have "\<dots> = setsum (\<lambda>i. setsum (?g i) {0..n - i}) {0..n}"
-      apply (rule setsum_cong2)
-      apply (rule_tac f="\<lambda>i. i + x" in setsum_reindex_cong)
-      apply (simp add: inj_on_def)
-      apply (rule set_ext)
-      apply (presburger add: image_iff)
-      by (simp add: add_ac mult_assoc)
-    finally  show "setsum (\<lambda>i. setsum (\<lambda>j. ?a j * ?b (i - j) * ?c (n -i)) {0..i}) {0..n} 
-      = setsum (\<lambda>i. setsum (\<lambda>j. ?a i * (?b j * ?c (n - (i + j)))) {0..n - i}) {0..n}".
+  show "(a * b) * c = a * (b * c)"
+  proof (rule fps_ext)
+    fix n :: nat
+    have "(\<Sum>j=0..n. \<Sum>i=0..j. a$i * b$(j - i) * c$(n - j)) =
+          (\<Sum>j=0..n. \<Sum>i=0..n - j. a$j * b$i * c$(n - j - i))"
+      by (rule fps_mult_assoc_lemma)
+    thus "((a * b) * c) $ n = (a * (b * c)) $ n"
+      by (simp add: fps_mult_nth setsum_right_distrib
+                    setsum_left_distrib mult_assoc)
+  qed
+qed
+
+lemma fps_mult_commute_lemma:
+  fixes n :: nat and f :: "nat \<Rightarrow> nat \<Rightarrow> 'a::comm_monoid_add"
+  shows "(\<Sum>i=0..n. f i (n - i)) = (\<Sum>i=0..n. f (n - i) i)"
+proof (rule setsum_reindex_cong)
+  show "inj_on (\<lambda>i. n - i) {0..n}"
+    by (rule inj_onI) simp
+  show "{0..n} = (\<lambda>i. n - i) ` {0..n}"
+    by (auto, rule_tac x="n - x" in image_eqI, simp_all)
+next
+  fix i assume "i \<in> {0..n}"
+  hence "n - (n - i) = i" by simp
+  thus "f (n - i) i = f (n - i) (n - (n - i))" by simp
+qed
+
+instance fps :: (comm_semiring_0) ab_semigroup_mult
+proof
+  fix a b :: "'a fps"
+  show "a * b = b * a"
+  proof (rule fps_ext)
+    fix n :: nat
+    have "(\<Sum>i=0..n. a$i * b$(n - i)) = (\<Sum>i=0..n. a$(n - i) * b$i)"
+      by (rule fps_mult_commute_lemma)
+    thus "(a * b) $ n = (b * a) $ n"
+      by (simp add: fps_mult_nth mult_commute)
   qed
 qed
 
-end
-
-instantiation fps :: (comm_semiring_1) ab_semigroup_mult
-begin
-
-instance
-proof
-  fix a b :: "'a fps"
-  show "a*b = b*a"
-  apply(auto simp add: fps_times_def setsum_right_distrib setsum_left_distrib, rule ext)
-  apply (rule_tac f = "\<lambda>i. n - i" in setsum_reindex_cong)
-  apply (simp add: inj_on_def)
-  apply presburger
-  apply (rule set_ext)
-  apply (presburger add: image_iff)
-  by (simp add: mult_commute)
-qed
-end
-
-instantiation fps :: (monoid_add) monoid_add
-begin
-
-instance
+instance fps :: (monoid_add) monoid_add
 proof
   fix a :: "'a fps" show "0 + a = a "
-    by (auto simp add: fps_plus_def fps_zero_def intro: ext)
+    by (simp add: fps_ext)
 next
   fix a :: "'a fps" show "a + 0 = a "
-    by (auto simp add: fps_plus_def fps_zero_def intro: ext)
+    by (simp add: fps_ext)
 qed
 
-end
-instantiation fps :: (comm_monoid_add) comm_monoid_add
-begin
-
-instance
+instance fps :: (comm_monoid_add) comm_monoid_add
 proof
   fix a :: "'a fps" show "0 + a = a "
-    by (auto simp add: fps_plus_def fps_zero_def intro: ext)
+    by (simp add: fps_ext)
 qed
 
-end
-
-instantiation fps :: (semiring_1) monoid_mult
-begin
-
-instance
+instance fps :: (semiring_1) monoid_mult
 proof
   fix a :: "'a fps" show "1 * a = a"
-    apply (auto simp add: fps_one_def fps_times_def)
-    apply (subst (2) Abs_fps_Rep_fps[of a, symmetric])
-    unfolding Abs_fps_eq
-    apply (rule ext)
-    by (simp add: cond_value_iff cond_application_beta setsum_delta cong del: if_weak_cong)
+    by (simp add: fps_ext fps_mult_nth mult_delta_left setsum_delta)
 next
-  fix a :: "'a fps" show "a*1 = a"
-    apply (auto simp add: fps_one_def fps_times_def)
-    apply (subst (2) Abs_fps_Rep_fps[of a, symmetric])
-    unfolding Abs_fps_eq
-    apply (rule ext)
-    by (simp add: cond_value_iff cond_application_beta setsum_delta' cong del: if_weak_cong)
+  fix a :: "'a fps" show "a * 1 = a"
+    by (simp add: fps_ext fps_mult_nth mult_delta_right setsum_delta')
 qed
-end
-
-instantiation fps :: (cancel_semigroup_add) cancel_semigroup_add
-begin
 
-instance by (intro_classes) (auto simp add: fps_plus_def expand_fun_eq Rep_fps_eq[symmetric])
-end
+instance fps :: (cancel_semigroup_add) cancel_semigroup_add
+proof
+  fix a b c :: "'a fps"
+  assume "a + b = a + c" then show "b = c"
+    by (simp add: expand_fps_eq)
+next
+  fix a b c :: "'a fps"
+  assume "b + a = c + a" then show "b = c"
+    by (simp add: expand_fps_eq)
+qed
 
-instantiation fps :: (group_add) group_add
-begin
+instance fps :: (cancel_ab_semigroup_add) cancel_ab_semigroup_add
+proof
+  fix a b c :: "'a fps"
+  assume "a + b = a + c" then show "b = c"
+    by (simp add: expand_fps_eq)
+qed
 
-instance
+instance fps :: (cancel_comm_monoid_add) cancel_comm_monoid_add ..
+
+instance fps :: (group_add) group_add
 proof
   fix a :: "'a fps" show "- a + a = 0"
-    by (auto simp add: fps_plus_def fps_uminus_def fps_zero_def intro: ext)
+    by (simp add: fps_ext)
 next
   fix a b :: "'a fps" show "a - b = a + - b"
-    by (auto simp add: fps_plus_def fps_uminus_def fps_zero_def 
-      fps_minus_def expand_fun_eq diff_minus)
+    by (simp add: fps_ext diff_minus)
 qed
-end
-
-context comm_ring_1
-begin
-subclass group_add proof qed
-end
 
-instantiation fps :: (zero_neq_one) zero_neq_one
-begin
-instance by (intro_classes, auto simp add: zero_neq_one 
-  fps_one_def fps_zero_def expand_fun_eq)
-end
+instance fps :: (ab_group_add) ab_group_add
+proof
+  fix a :: "'a fps"
+  show "- a + a = 0"
+    by (simp add: fps_ext)
+next
+  fix a b :: "'a fps"
+  show "a - b = a + - b"
+    by (simp add: fps_ext)
+qed
 
-instantiation fps :: (semiring_1) semiring
-begin
+instance fps :: (zero_neq_one) zero_neq_one
+  by default (simp add: expand_fps_eq)
 
-instance
+instance fps :: (semiring_0) semiring
 proof
   fix a b c :: "'a fps"
-  show "(a + b) * c = a * c + b*c"
-    apply (auto simp add: fps_plus_def fps_times_def, rule ext)
-    unfolding setsum_addf[symmetric]
-    apply (simp add: ring_simps)
-    done
+  show "(a + b) * c = a * c + b * c"
+    by (simp add: expand_fps_eq fps_mult_nth left_distrib setsum_addf)
 next
   fix a b c :: "'a fps"
-  show "a * (b + c) = a * b + a*c"
-    apply (auto simp add: fps_plus_def fps_times_def, rule ext)
-    unfolding setsum_addf[symmetric]
-    apply (simp add: ring_simps)
-    done
-qed
-end
-
-instantiation fps :: (semiring_1) semiring_0
-begin
-
-instance
-proof
-  fix a:: "'a fps" show "0 * a = 0" by (simp add: fps_zero_def fps_times_def)
-next
-  fix a:: "'a fps" show "a*0 = 0" by (simp add: fps_zero_def fps_times_def)
+  show "a * (b + c) = a * b + a * c"
+    by (simp add: expand_fps_eq fps_mult_nth right_distrib setsum_addf)
 qed
-end
-  
-section {* Selection of the nth power of the implicit variable in the infinite sum*}
-
-definition fps_nth:: "'a fps \<Rightarrow> nat \<Rightarrow> 'a" (infixl "$" 75)
-  where "f $ n = Rep_fps f n"
 
-lemma fps_nth_Abs_fps[simp]: "Abs_fps a $ n = a n" by (simp add: fps_nth_def)
-lemma fps_zero_nth[simp]: "0 $ n = 0" by (simp add: fps_zero_def)
-lemma fps_one_nth[simp]: "1 $ n = (if n = 0 then 1 else 0)" 
-  by (simp add: fps_one_def)
-lemma fps_add_nth[simp]: "(f + g) $ n = f$n + g$n" by (simp add: fps_plus_def fps_nth_def)
-lemma fps_mult_nth: "(f * g) $ n = setsum (\<lambda>i. f$i * g$(n - i)) {0..n}"
-  by (simp add: fps_times_def fps_nth_def)
-lemma fps_neg_nth[simp]: "(- f) $n = - (f $n)" by (simp add: fps_nth_def fps_uminus_def)
-lemma fps_sub_nth[simp]: "(f - g)$n = f$n - g$n" by (simp add: fps_nth_def fps_minus_def)
+instance fps :: (semiring_0) semiring_0
+proof
+  fix a:: "'a fps" show "0 * a = 0"
+    by (simp add: fps_ext fps_mult_nth)
+next
+  fix a:: "'a fps" show "a * 0 = 0"
+    by (simp add: fps_ext fps_mult_nth)
+qed
+
+instance fps :: (semiring_0_cancel) semiring_0_cancel ..
+
+subsection {* Selection of the nth power of the implicit variable in the infinite sum*}
 
 lemma fps_nonzero_nth: "f \<noteq> 0 \<longleftrightarrow> (\<exists> n. f $n \<noteq> 0)"
-proof-
-  {assume "f \<noteq> 0"
-    hence "Rep_fps f \<noteq> Rep_fps 0" by simp 
-    hence "\<exists>n. f $n \<noteq> 0" by (simp add: expand_fun_eq fps_zero_def fps_nth_def )}
-  moreover
-  {assume "\<exists>n. f$n \<noteq> 0" and "f = 0" 
-    then have False by (simp add: expand_fun_eq fps_zero_def fps_nth_def )}
-  ultimately show ?thesis by blast
-qed
+  by (simp add: expand_fps_eq)
 
-lemma fps_nonzero_nth_minimal: "f \<noteq> 0 \<longleftrightarrow> (\<exists> n. f $n \<noteq> 0 \<and> (\<forall>m <n. f $m = 0))"
-proof-
-  let ?S = "{n. f$n \<noteq> 0}"
-  {assume "\<exists>n. f$n \<noteq> 0 \<and> (\<forall>m <n. f $m = 0)" and "f = 0" 
-    then have False by (simp add: expand_fun_eq fps_zero_def fps_nth_def )}
-  moreover
-  {assume f0: "f \<noteq> 0"
-    from f0 fps_nonzero_nth have ex: "\<exists>n. f$n \<noteq> 0" by blast
-    hence Se: "?S\<noteq> {}" by blast
-    from ex obtain n where n: "f$n \<noteq> 0" by blast
-    from n have nS: "n \<in> ?S" by blast
-        let ?U = "?S \<inter> {0..n}"
-    have fU: "finite ?U" by auto
-    from n have Ue: "?U \<noteq> {}" by auto
-    let ?m = "Min ?U" 
-    have mU: "?m \<in> ?U" using Min_in[OF fU Ue] .
-    hence mn: "?m \<le> n" by simp
-    from mU have mf: "f $ ?m \<noteq> 0" by blast
-    {fix m assume m: "m < ?m" and f: "f $m \<noteq> 0"
-      from m mn have mn': "m < n" by arith
-      with f have mU': "m \<in> ?U" by simp
-      from Min_le[OF fU mU'] m have False by arith}
-    hence "\<forall>m <?m. f$m = 0" by blast
-    with mf have "\<exists> n. f $n \<noteq> 0 \<and> (\<forall>m <n. f $m = 0)" by blast}
-  ultimately show ?thesis by blast
+lemma fps_nonzero_nth_minimal:
+  "f \<noteq> 0 \<longleftrightarrow> (\<exists>n. f $ n \<noteq> 0 \<and> (\<forall>m<n. f $ m = 0))"
+proof
+  let ?n = "LEAST n. f $ n \<noteq> 0"
+  assume "f \<noteq> 0"
+  then have "\<exists>n. f $ n \<noteq> 0"
+    by (simp add: fps_nonzero_nth)
+  then have "f $ ?n \<noteq> 0"
+    by (rule LeastI_ex)
+  moreover have "\<forall>m<?n. f $ m = 0"
+    by (auto dest: not_less_Least)
+  ultimately have "f $ ?n \<noteq> 0 \<and> (\<forall>m<?n. f $ m = 0)" ..
+  then show "\<exists>n. f $ n \<noteq> 0 \<and> (\<forall>m<n. f $ m = 0)" ..
+next
+  assume "\<exists>n. f $ n \<noteq> 0 \<and> (\<forall>m<n. f $ m = 0)"
+  then show "f \<noteq> 0" by (auto simp add: expand_fps_eq)
 qed
 
 lemma fps_eq_iff: "f = g \<longleftrightarrow> (\<forall>n. f $ n = g $n)"
-  by (auto simp add: fps_nth_def Rep_fps_eq[unfolded expand_fun_eq])
+  by (rule expand_fps_eq)
 
 lemma fps_setsum_nth: "(setsum f S) $ n = setsum (\<lambda>k. (f k) $ n) S" 
-proof-
-  {assume "\<not> finite S" hence ?thesis by simp}
-  moreover
-  {assume fS: "finite S"
-    have ?thesis by(induct rule: finite_induct[OF fS]) auto}
-  ultimately show ?thesis by blast
+proof (cases "finite S")
+  assume "\<not> finite S" then show ?thesis by simp
+next
+  assume "finite S"
+  then show ?thesis by (induct set: finite) auto
 qed
 
-section{* Injection of the basic ring elements and multiplication by scalars *}
+subsection{* Injection of the basic ring elements and multiplication by scalars *}
+
+definition
+  "fps_const c = Abs_fps (\<lambda>n. if n = 0 then c else 0)"
+
+lemma fps_nth_fps_const [simp]: "fps_const c $ n = (if n = 0 then c else 0)"
+  unfolding fps_const_def by simp
+
+lemma fps_const_0_eq_0 [simp]: "fps_const 0 = 0"
+  by (simp add: fps_ext)
 
-definition "fps_const c = Abs_fps (\<lambda>n. if n = 0 then c else 0)"
-lemma fps_const_0_eq_0[simp]: "fps_const 0 = 0" by (simp add: fps_const_def fps_eq_iff)
-lemma fps_const_1_eq_1[simp]: "fps_const 1 = 1" by (simp add: fps_const_def fps_eq_iff)
-lemma fps_const_neg[simp]: "- (fps_const (c::'a::ring)) = fps_const (- c)"
-  by (simp add: fps_uminus_def fps_const_def fps_eq_iff)
-lemma fps_const_add[simp]: "fps_const (c::'a\<Colon>monoid_add) + fps_const d = fps_const (c + d)"
-  by (simp add: fps_plus_def fps_const_def fps_eq_iff)
+lemma fps_const_1_eq_1 [simp]: "fps_const 1 = 1"
+  by (simp add: fps_ext)
+
+lemma fps_const_neg [simp]: "- (fps_const (c::'a::ring)) = fps_const (- c)"
+  by (simp add: fps_ext)
+
+lemma fps_const_add [simp]: "fps_const (c::'a\<Colon>monoid_add) + fps_const d = fps_const (c + d)"
+  by (simp add: fps_ext)
+
 lemma fps_const_mult[simp]: "fps_const (c::'a\<Colon>ring) * fps_const d = fps_const (c * d)"
-  by (auto simp add: fps_times_def fps_const_def fps_eq_iff intro: setsum_0')
+  by (simp add: fps_eq_iff fps_mult_nth setsum_0')
 
 lemma fps_const_add_left: "fps_const (c::'a\<Colon>monoid_add) + f = Abs_fps (\<lambda>n. if n = 0 then c + f$0 else f$n)"
-  unfolding fps_eq_iff fps_add_nth by (simp add: fps_const_def)
+  by (simp add: fps_ext)
+
 lemma fps_const_add_right: "f + fps_const (c::'a\<Colon>monoid_add) = Abs_fps (\<lambda>n. if n = 0 then f$0 + c else f$n)"
-  unfolding fps_eq_iff fps_add_nth by (simp add: fps_const_def)
+  by (simp add: fps_ext)
 
 lemma fps_const_mult_left: "fps_const (c::'a\<Colon>semiring_0) * f = Abs_fps (\<lambda>n. c * f$n)"
-  unfolding fps_eq_iff fps_mult_nth 
-  by (simp add: fps_const_def cond_application_beta cond_value_iff setsum_delta cong del: if_weak_cong)
-lemma fps_const_mult_right: "f * fps_const (c::'a\<Colon>semiring_0) = Abs_fps (\<lambda>n. f$n * c)"
-  unfolding fps_eq_iff fps_mult_nth 
-  by (simp add: fps_const_def cond_application_beta cond_value_iff setsum_delta' cong del: if_weak_cong)
+  unfolding fps_eq_iff fps_mult_nth
+  by (simp add: fps_const_def mult_delta_left setsum_delta)
 
-lemma fps_const_nth[simp]: "(fps_const c) $n = (if n = 0 then c else 0)"
-  by (simp add: fps_const_def)
+lemma fps_const_mult_right: "f * fps_const (c::'a\<Colon>semiring_0) = Abs_fps (\<lambda>n. f$n * c)"
+  unfolding fps_eq_iff fps_mult_nth
+  by (simp add: fps_const_def mult_delta_right setsum_delta')
 
-lemma fps_mult_left_const_nth[simp]: "(fps_const (c::'a::semiring_1) * f)$n = c* f$n"
-  by (simp add: fps_mult_nth fps_const_nth cond_application_beta cond_value_iff setsum_delta cong del: if_weak_cong)
-
-lemma fps_mult_right_const_nth[simp]: "(f * fps_const (c::'a::semiring_1))$n = f$n * c"
-  by (simp add: fps_mult_nth fps_const_nth cond_application_beta cond_value_iff setsum_delta' cong del: if_weak_cong)
+lemma fps_mult_left_const_nth [simp]: "(fps_const (c::'a::semiring_1) * f)$n = c* f$n"
+  by (simp add: fps_mult_nth mult_delta_left setsum_delta)
 
-section {* Formal power series form an integral domain*}
+lemma fps_mult_right_const_nth [simp]: "(f * fps_const (c::'a::semiring_1))$n = f$n * c"
+  by (simp add: fps_mult_nth mult_delta_right setsum_delta')
 
-instantiation fps :: (ring_1) ring_1
-begin
+subsection {* Formal power series form an integral domain*}
 
-instance by (intro_classes, auto simp add: diff_minus left_distrib)
-end
+instance fps :: (ring) ring ..
 
-instantiation fps :: (comm_ring_1) comm_ring_1
-begin
+instance fps :: (ring_1) ring_1
+  by (intro_classes, auto simp add: diff_minus left_distrib)
 
-instance by (intro_classes, auto simp add: diff_minus left_distrib)
-end
-instantiation fps :: ("{ring_no_zero_divisors, comm_ring_1}") ring_no_zero_divisors
-begin
+instance fps :: (comm_ring_1) comm_ring_1
+  by (intro_classes, auto simp add: diff_minus left_distrib)
 
-instance 
+instance fps :: (ring_no_zero_divisors) ring_no_zero_divisors
 proof
   fix a b :: "'a fps"
   assume a0: "a \<noteq> 0" and b0: "b \<noteq> 0"
   then obtain i j where i: "a$i\<noteq>0" "\<forall>k<i. a$k=0"
     and j: "b$j \<noteq>0" "\<forall>k<j. b$k =0" unfolding fps_nonzero_nth_minimal
     by blast+
-  have eq: "({0..i+j} -{i}) \<union> {i} = {0..i+j}" by auto
-  have d: "({0..i+j} -{i}) \<inter> {i} = {}" by auto
-  have f: "finite ({0..i+j} -{i})" "finite {i}" by auto
-  have th0: "setsum (\<lambda>k. a$k * b$(i+j - k)) ({0..i+j} -{i}) = 0"
-    apply (rule setsum_0')
-    apply auto
-    apply (case_tac "aa < i")
-    using i
-    apply auto
-    apply (subgoal_tac "b $ (i+j - aa) = 0")
-    apply blast
-    apply (rule j(2)[rule_format])
-    by arith
-  have "(a*b) $ (i+j) =  setsum (\<lambda>k. a$k * b$(i+j - k)) {0..i+j}"
+  have "(a * b) $ (i+j) = (\<Sum>k=0..i+j. a$k * b$(i+j-k))"
     by (rule fps_mult_nth)
-  hence "(a*b) $ (i+j) = a$i * b$j"
-    unfolding setsum_Un_disjoint[OF f d, unfolded eq] th0 by simp
-  with i j have "(a*b) $ (i+j) \<noteq> 0" by simp
+  also have "\<dots> = (a$i * b$(i+j-i)) + (\<Sum>k\<in>{0..i+j}-{i}. a$k * b$(i+j-k))"
+    by (rule setsum_diff1') simp_all
+  also have "(\<Sum>k\<in>{0..i+j}-{i}. a$k * b$(i+j-k)) = 0"
+    proof (rule setsum_0' [rule_format])
+      fix k assume "k \<in> {0..i+j} - {i}"
+      then have "k < i \<or> i+j-k < j" by auto
+      then show "a$k * b$(i+j-k) = 0" using i j by auto
+    qed
+  also have "a$i * b$(i+j-i) + 0 = a$i * b$j" by simp
+  also have "a$i * b$j \<noteq> 0" using i j by simp
+  finally have "(a*b) $ (i+j) \<noteq> 0" .
   then show "a*b \<noteq> 0" unfolding fps_nonzero_nth by blast
 qed
-end
-
-instantiation fps :: (idom) idom
-begin
 
-instance ..
-end
+instance fps :: (idom) idom ..
 
-section{* Inverses of formal power series *}
+subsection{* Inverses of formal power series *}
 
 declare setsum_cong[fundef_cong]
 
@@ -456,24 +403,20 @@
 
 definition fps_inverse_def: 
   "inverse f = (if f$0 = 0 then 0 else Abs_fps (natfun_inverse f))"
-definition fps_divide_def: "divide \<equiv> (\<lambda>(f::'a fps) g. f * inverse g)"
+definition fps_divide_def: "divide = (\<lambda>(f::'a fps) g. f * inverse g)"
 instance ..
 end
 
 lemma fps_inverse_zero[simp]: 
   "inverse (0 :: 'a::{comm_monoid_add,inverse, times, uminus} fps) = 0"
-  by (simp add: fps_zero_def fps_inverse_def)
+  by (simp add: fps_ext fps_inverse_def)
 
 lemma fps_inverse_one[simp]: "inverse (1 :: 'a::{division_ring,zero_neq_one} fps) = 1"
-  apply (auto simp add: fps_one_def fps_inverse_def expand_fun_eq)
-  by (case_tac x, auto)
+  apply (auto simp add: expand_fps_eq fps_inverse_def)
+  by (case_tac n, auto)
 
-instantiation fps :: ("{comm_monoid_add,inverse, times, uminus}")  division_by_zero
-begin
-instance
-  apply (intro_classes)
-  by (rule fps_inverse_zero)
-end
+instance fps :: ("{comm_monoid_add,inverse, times, uminus}")  division_by_zero
+  by default (rule fps_inverse_zero)
 
 lemma inverse_mult_eq_1[intro]: assumes f0: "f$0 \<noteq> (0::'a::field)"
   shows "inverse f * f = 1"
@@ -482,14 +425,14 @@
   from f0 have ifn: "\<And>n. inverse f $ n = natfun_inverse f n" 
     by (simp add: fps_inverse_def)
   from f0 have th0: "(inverse f * f) $ 0 = 1"
-    by (simp add: fps_inverse_def fps_one_def fps_mult_nth)
+    by (simp add: fps_mult_nth fps_inverse_def)
   {fix n::nat assume np: "n >0 "
     from np have eq: "{0..n} = {0} \<union> {1 .. n}" by auto
     have d: "{0} \<inter> {1 .. n} = {}" by auto
     have f: "finite {0::nat}" "finite {1..n}" by auto
     from f0 np have th0: "- (inverse f$n) = 
       (setsum (\<lambda>i. f$i * natfun_inverse f (n - i)) {1..n}) / (f$0)"
-      by (cases n, simp_all add: divide_inverse fps_inverse_def fps_nth_def ring_simps)
+      by (cases n, simp, simp add: divide_inverse fps_inverse_def)
     from th0[symmetric, unfolded nonzero_divide_eq_eq[OF f0]]
     have th1: "setsum (\<lambda>i. f$i * natfun_inverse f (n - i)) {1..n} = 
       - (f$0) * (inverse f)$n" 
@@ -506,8 +449,7 @@
 qed
 
 lemma fps_inverse_0_iff[simp]: "(inverse f)$0 = (0::'a::division_ring) \<longleftrightarrow> f$0 = 0"
-  apply (simp add: fps_inverse_def)
-  by (metis fps_nth_def fps_nth_def inverse_zero_imp_zero)
+  by (simp add: fps_inverse_def nonzero_imp_inverse_nonzero)
 
 lemma fps_inverse_eq_0_iff[simp]: "inverse f = (0:: ('a::field) fps) \<longleftrightarrow> f $0 = 0"
 proof-
@@ -533,15 +475,14 @@
   from inverse_mult_eq_1[OF f0] fg
   have th0: "inverse f * f = g * f" by (simp add: mult_ac)
   then show ?thesis using f0  unfolding mult_cancel_right
-    unfolding Rep_fps_eq[of f 0, symmetric]
-    by (auto simp add: fps_zero_def expand_fun_eq fps_nth_def)
+    by (auto simp add: expand_fps_eq)
 qed
 
 lemma fps_inverse_gp: "inverse (Abs_fps(\<lambda>n. (1::'a::field))) 
   = Abs_fps (\<lambda>n. if n= 0 then 1 else if n=1 then - 1 else 0)"
   apply (rule fps_inverse_unique)
   apply simp
-  apply (simp add: fps_eq_iff fps_nth_def fps_times_def fps_one_def)
+  apply (simp add: fps_eq_iff fps_mult_nth)
 proof(clarsimp)
   fix n::nat assume n: "n > 0"
   let ?f = "\<lambda>i. if n = i then (1\<Colon>'a) else if n - i = 1 then - 1 else 0"
@@ -561,7 +502,7 @@
     by(simp add: setsum_delta)
 qed
 
-section{* Formal Derivatives, and the McLauren theorem around 0*}
+subsection{* Formal Derivatives, and the MacLaurin theorem around 0*}
 
 definition "fps_deriv f = Abs_fps (\<lambda>n. of_nat (n + 1) * f $ (n + 1))"
 
@@ -625,7 +566,7 @@
 qed
 
 lemma fps_deriv_neg[simp]: "fps_deriv (- (f:: ('a:: comm_ring_1) fps)) = - (fps_deriv f)"
-  by (simp add: fps_uminus_def fps_eq_iff fps_deriv_def fps_nth_def)
+  by (simp add: fps_eq_iff fps_deriv_def)
 lemma fps_deriv_add[simp]: "fps_deriv ((f:: ('a::comm_ring_1) fps) + g) = fps_deriv f + fps_deriv g"
   using fps_deriv_linear[of 1 f 1 g] by simp
 
@@ -633,7 +574,7 @@
   unfolding diff_minus by simp 
 
 lemma fps_deriv_const[simp]: "fps_deriv (fps_const c) = 0"
-  by (simp add: fps_deriv_def fps_const_def fps_zero_def)
+  by (simp add: fps_ext fps_deriv_def fps_const_def)
 
 lemma fps_deriv_mult_const_left[simp]: "fps_deriv (fps_const (c::'a::comm_ring_1) * f) = fps_const c * fps_deriv f"
   by simp
@@ -730,7 +671,7 @@
 lemma fps_deriv_maclauren_0: "(fps_nth_deriv k (f:: ('a::comm_semiring_1) fps)) $ 0 = of_nat (fact k) * f$(k)"
   by (induct k arbitrary: f) (auto simp add: ring_simps of_nat_mult)
 
-section {* Powers*}
+subsection {* Powers*}
 
 instantiation fps :: (semiring_1) power
 begin
@@ -750,29 +691,8 @@
   by (simp_all add: fps_power_def)
 end
 
-lemma eq_neg_iff_add_eq_0: "(a::'a::ring) = -b \<longleftrightarrow> a + b = 0"
-proof-
-  {assume "a = -b" hence "b + a = b + -b" by simp
-    hence "a + b = 0" by (simp add: ring_simps)}
-  moreover
-  {assume "a + b = 0" hence "a + b - b = -b" by simp
-    hence "a = -b" by simp}
-  ultimately show ?thesis by blast
-qed
-
-lemma fps_sqrare_eq_iff: "(a:: 'a::idom fps)^ 2 = b^2  \<longleftrightarrow> (a = b \<or> a = -b)"
-proof-
-  {assume "a = b \<or> a = -b" hence "a^2 = b^2" by auto}
-  moreover
-  {assume "a^2 = b^2 "
-    hence "a^2 - b^2 = 0" by simp
-    hence "(a-b) * (a+b) = 0" by (simp add: power2_eq_square ring_simps)
-    hence "a = b \<or> a = -b" by (simp add: eq_neg_iff_add_eq_0)}
-  ultimately show ?thesis by blast
-qed
-
 lemma fps_power_zeroth_eq_one: "a$0 =1 \<Longrightarrow> a^n $ 0 = (1::'a::semiring_1)"
-  by (induct n, auto simp add: fps_power_def fps_times_def fps_nth_def fps_one_def)
+  by (induct n, auto simp add: fps_power_def expand_fps_eq fps_mult_nth)
 
 lemma fps_power_first_eq: "(a:: 'a::comm_ring_1 fps)$0 =1 \<Longrightarrow> a^n $ 1 = of_nat n * a$1"
 proof(induct n)
@@ -945,7 +865,7 @@
   using fps_inverse_deriv[OF a0]
   by (simp add: fps_divide_def ring_simps power2_eq_square fps_inverse_mult inverse_mult_eq_1'[OF a0])
   
-section{* The eXtractor series X*}
+subsection{* The eXtractor series X*}
 
 lemma minus_one_power_iff: "(- (1::'a :: {recpower, comm_ring_1})) ^ n = (if even n then 1 else - 1)"
   by (induct n, auto)
@@ -954,16 +874,15 @@
 
 lemma fps_inverse_gp': "inverse (Abs_fps(\<lambda>n. (1::'a::field))) 
   = 1 - X"
-  by (simp add: fps_inverse_gp fps_eq_iff X_def fps_minus_def fps_one_def)
+  by (simp add: fps_inverse_gp fps_eq_iff X_def)
 
 lemma X_mult_nth[simp]: "(X * (f :: ('a::semiring_1) fps)) $n = (if n = 0 then 0 else f $ (n - 1))"
 proof-
   {assume n: "n \<noteq> 0"
     have fN: "finite {0 .. n}" by simp
     have "(X * f) $n = (\<Sum>i = 0..n. X $ i * f $ (n - i))" by (simp add: fps_mult_nth)
-    also have "\<dots> = f $ (n - 1)" 
-      using n by (simp add: X_def cond_value_iff cond_application_beta setsum_delta[OF fN] 
-	del: One_nat_def cong del:  if_weak_cong)
+    also have "\<dots> = f $ (n - 1)"
+      using n by (simp add: X_def mult_delta_left setsum_delta [OF fN])
   finally have ?thesis using n by simp }
   moreover
   {assume n: "n=0" hence ?thesis by (simp add: fps_mult_nth X_def)}
@@ -1015,7 +934,7 @@
 qed
 
   
-section{* Integration *}
+subsection{* Integration *}
 definition "fps_integral a a0 = Abs_fps (\<lambda>n. if n = 0 then a0 else (a$(n - 1) / of_nat n))"
 
 lemma fps_deriv_fps_integral: "fps_deriv (fps_integral a (a0 :: 'a :: {field, ring_char_0})) = a"
@@ -1029,31 +948,27 @@
     unfolding fps_deriv_eq_iff by auto
 qed
   
-section {* Composition of FPSs *}
+subsection {* Composition of FPSs *}
 definition fps_compose :: "('a::semiring_1) fps \<Rightarrow> 'a fps \<Rightarrow> 'a fps" (infixl "oo" 55) where
   fps_compose_def: "a oo b = Abs_fps (\<lambda>n. setsum (\<lambda>i. a$i * (b^i$n)) {0..n})"
 
 lemma fps_compose_nth: "(a oo b)$n = setsum (\<lambda>i. a$i * (b^i$n)) {0..n}" by (simp add: fps_compose_def)
 
 lemma fps_compose_X[simp]: "a oo X = (a :: ('a :: comm_ring_1) fps)"
-  by (auto simp add: fps_compose_def X_power_iff fps_eq_iff cond_application_beta cond_value_iff setsum_delta' cong del: if_weak_cong)
+  by (simp add: fps_ext fps_compose_def mult_delta_right setsum_delta')
  
 lemma fps_const_compose[simp]: 
   "fps_const (a::'a::{comm_ring_1}) oo b = fps_const (a)"
-  apply (auto simp add: fps_eq_iff fps_compose_nth fps_mult_nth
-  cond_application_beta cond_value_iff cong del: if_weak_cong)
-  by (simp add: setsum_delta )
+  by (simp add: fps_eq_iff fps_compose_nth mult_delta_left setsum_delta)
 
 lemma X_fps_compose_startby0[simp]: "a$0 = 0 \<Longrightarrow> X oo a = (a :: ('a :: comm_ring_1) fps)"
-  apply (auto simp add: fps_compose_def fps_eq_iff cond_application_beta cond_value_iff setsum_delta cong del: if_weak_cong)
-  apply (simp add: power_Suc)
-  apply (subgoal_tac "n = 0")
-  by simp_all
+  by (simp add: fps_eq_iff fps_compose_def mult_delta_left setsum_delta
+                power_Suc not_le)
 
 
-section {* Rules from Herbert Wilf's Generatingfunctionology*}
+subsection {* Rules from Herbert Wilf's Generatingfunctionology*}
 
-subsection {* Rule 1 *}
+subsubsection {* Rule 1 *}
   (* {a_{n+k}}_0^infty Corresponds to (f - setsum (\<lambda>i. a_i * x^i))/x^h, for h>0*)
 
 lemma fps_power_mult_eq_shift: 
@@ -1083,7 +998,7 @@
   then show ?thesis by (simp add: fps_eq_iff)
 qed
 
-subsection{* Rule 2*}
+subsubsection{* Rule 2*}
 
   (* We can not reach the form of Wilf, but still near to it using rewrite rules*)
   (* If f reprents {a_n} and P is a polynomial, then 
@@ -1108,8 +1023,8 @@
 lemma fps_mult_XD_shift: "(XD ^k) (a:: ('a::{comm_ring_1, recpower, ring_char_0}) fps) = Abs_fps (\<lambda>n. (of_nat n ^ k) * a$n)"
 by (induct k arbitrary: a) (simp_all add: power_Suc XD_def fps_eq_iff ring_simps del: One_nat_def)
 
-subsection{* Rule 3 is trivial and is given by @{text fps_times_def}*}
-subsection{* Rule 5 --- summation and "division" by (1 - X)*}
+subsubsection{* Rule 3 is trivial and is given by @{text fps_times_def}*}
+subsubsection{* Rule 5 --- summation and "division" by (1 - X)*}
 
 lemma fps_divide_X_minus1_setsum_lemma:
   "a = ((1::('a::comm_ring_1) fps) - X) * Abs_fps (\<lambda>n. setsum (\<lambda>i. a $ i) {0..n})"
@@ -1157,7 +1072,7 @@
   finally show ?thesis by (simp add: inverse_mult_eq_1[OF th0])
 qed
 
-subsection{* Rule 4 in its more general form: generalizes Rule 3 for an arbitrary 
+subsubsection{* Rule 4 in its more general form: generalizes Rule 3 for an arbitrary 
   finite product of FPS, also the relvant instance of powers of a FPS*}
 
 definition "natpermute n k = {l:: nat list. length l = k \<and> foldl op + 0 l = n}"
@@ -1447,7 +1362,7 @@
 qed
 
 
-section {* Radicals *}
+subsection {* Radicals *}
 
 declare setprod_cong[fundef_cong]
 function radical :: "(nat \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> nat \<Rightarrow> ('a::{field, recpower}) fps \<Rightarrow> nat \<Rightarrow> 'a" where
@@ -1684,7 +1599,7 @@
 	then have "a$n = ?r $n"
 	  apply (simp del: of_nat_Suc)
 	  unfolding fps_radical_def n1
-	  by (simp add: field_simps n1 fps_nth_def th00 del: of_nat_Suc)}
+	  by (simp add: field_simps n1 th00 del: of_nat_Suc)}
 	ultimately show "a$n = ?r $ n" by (cases n, auto)
       qed}
     then have "a = ?r" by (simp add: fps_eq_iff)}
@@ -1832,7 +1747,7 @@
 ultimately show ?thesis by blast
 qed
 
-section{* Derivative of composition *}
+subsection{* Derivative of composition *}
 
 lemma fps_compose_deriv: 
   fixes a:: "('a::idom) fps"
@@ -1850,8 +1765,8 @@
     unfolding fps_mult_nth ..
   also have "\<dots> = setsum (\<lambda>i. of_nat i * a$i * (setsum (\<lambda>j. (b^ (i - 1))$j * (fps_deriv b)$(n - j)) {0..n})) {1.. Suc n}"
     apply (rule setsum_mono_zero_right)
-    by (auto simp add: cond_value_iff cond_application_beta setsum_delta 
-      not_le cong del: if_weak_cong)
+    apply (auto simp add: mult_delta_left setsum_delta not_le)
+    done
   also have "\<dots> = setsum (\<lambda>i. of_nat (i + 1) * a$(i+1) * (setsum (\<lambda>j. (b^ i)$j * of_nat (n - j + 1) * b$(n - j + 1)) {0..n})) {0.. n}"
     unfolding fps_deriv_nth
     apply (rule setsum_reindex_cong[where f="Suc"])
@@ -1898,7 +1813,7 @@
   ultimately show ?thesis by (cases n, auto)
 qed
 
-section{* Finite FPS (i.e. polynomials) and X *}
+subsection{* Finite FPS (i.e. polynomials) and X *}
 lemma fps_poly_sum_X:
   assumes z: "\<forall>i > n. a$i = (0::'a::comm_ring_1)" 
   shows "a = setsum (\<lambda>i. fps_const (a$i) * X^i) {0..n}" (is "a = ?r")
@@ -1906,12 +1821,12 @@
   {fix i
     have "a$i = ?r$i" 
       unfolding fps_setsum_nth fps_mult_left_const_nth X_power_nth
-      apply (simp add: cond_application_beta cond_value_iff setsum_delta' cong del: if_weak_cong)
-      using z by auto}
+      by (simp add: mult_delta_right setsum_delta' z)
+  }
   then show ?thesis unfolding fps_eq_iff by blast
 qed
 
-section{* Compositional inverses *}
+subsection{* Compositional inverses *}
 
 
 fun compinv :: "'a fps \<Rightarrow> nat \<Rightarrow> 'a::{recpower,field}" where
@@ -1935,7 +1850,7 @@
 	have "?i $ n = setsum (\<lambda>i. (fps_inv a $ i) * (a^i)$n) {0 .. n1} + fps_inv a $ Suc n1 * (a $ 1)^ Suc n1"
 	  by (simp add: fps_compose_nth n1 startsby_zero_power_nth_same[OF a0])
 	also have "\<dots> = setsum (\<lambda>i. (fps_inv a $ i) * (a^i)$n) {0 .. n1} + (X$ Suc n1 - setsum (\<lambda>i. (fps_inv a $ i) * (a^i)$n) {0 .. n1})"
-	  using a0 a1 n1 by (simp add: fps_inv_def fps_nth_def)
+	  using a0 a1 n1 by (simp add: fps_inv_def)
 	also have "\<dots> = X$n" using n1 by simp 
 	finally have "?i $ n = X$n" .}
       ultimately show "?i $ n = X$n" by (cases n, auto)
@@ -1965,7 +1880,7 @@
 	have "?i $ n = setsum (\<lambda>i. (fps_ginv b a $ i) * (a^i)$n) {0 .. n1} + fps_ginv b a $ Suc n1 * (a $ 1)^ Suc n1"
 	  by (simp add: fps_compose_nth n1 startsby_zero_power_nth_same[OF a0])
 	also have "\<dots> = setsum (\<lambda>i. (fps_ginv b a $ i) * (a^i)$n) {0 .. n1} + (b$ Suc n1 - setsum (\<lambda>i. (fps_ginv b a $ i) * (a^i)$n) {0 .. n1})"
-	  using a0 a1 n1 by (simp add: fps_ginv_def fps_nth_def)
+	  using a0 a1 n1 by (simp add: fps_ginv_def)
 	also have "\<dots> = b$n" using n1 by simp 
 	finally have "?i $ n = b$n" .}
       ultimately show "?i $ n = b$n" by (cases n, auto)
@@ -1982,19 +1897,16 @@
   done
 
 lemma fps_compose_1[simp]: "1 oo a = 1"
-  apply (auto simp add: fps_eq_iff fps_compose_nth fps_power_def cond_value_iff cond_application_beta cong del: if_weak_cong)
-  apply (simp add: setsum_delta)
-  done
+  by (simp add: fps_eq_iff fps_compose_nth fps_power_def mult_delta_left setsum_delta)
 
 lemma fps_compose_0[simp]: "0 oo a = 0"
-  by (auto simp add: fps_eq_iff fps_compose_nth fps_power_def cond_value_iff cond_application_beta cong del: if_weak_cong)
+  by (simp add: fps_eq_iff fps_compose_nth)
 
 lemma fps_pow_0: "fps_pow n 0 = (if n = 0 then 1 else 0)"
   by (induct n, simp_all)
 
 lemma fps_compose_0_right[simp]: "a oo 0 = fps_const (a$0)"
-  apply (auto simp add: fps_eq_iff fps_compose_nth fps_power_def cond_value_iff cond_application_beta cong del: if_weak_cong)
-  by (case_tac n, auto simp add: fps_pow_0 intro: setsum_0')
+  by (auto simp add: fps_eq_iff fps_compose_nth fps_power_def fps_pow_0 setsum_0')
 
 lemma fps_compose_add_distrib: "(a + b) oo c = (a oo c) + (b oo c)"
   by (simp add: fps_eq_iff fps_compose_nth  ring_simps setsum_addf)
@@ -2092,7 +2004,7 @@
   let ?f = "%s. UNION {(0::nat)..s} (%i. {(i,s - i)})"
   have th0: "?KM = UNION {0..n} ?f"
     apply (simp add: expand_set_eq)
-    apply arith
+    apply arith (* FIXME: VERY slow! *)
     done
   show "?l = ?r "
     unfolding th0
@@ -2177,8 +2089,8 @@
 	  by (simp add: fps_compose_nth)}
       moreover
       {assume kn: "k \<le> n"
-	hence "?l$n = ?r$n" apply (simp only: fps_compose_nth X_power_nth)
-	  by (simp add: cond_value_iff cond_application_beta setsum_delta cong del: if_weak_cong)}
+	hence "?l$n = ?r$n"
+          by (simp add: fps_compose_nth mult_delta_left setsum_delta)}
       moreover have "k >n \<or> k\<le> n"  by arith
       ultimately have "?l$n = ?r$n"  by blast}
     then have ?thesis unfolding fps_eq_iff by blast}
@@ -2217,9 +2129,9 @@
   show "?dia = inverse ?d" by simp
 qed
 
-section{* Elementary series *}
+subsection{* Elementary series *}
 
-subsection{* Exponential series *}
+subsubsection{* Exponential series *}
 definition "E x = Abs_fps (\<lambda>n. x^n / of_nat (fact n))"   
 
 lemma E_deriv[simp]: "fps_deriv (E a) = fps_const (a::'a::{field, recpower, ring_char_0}) * E a" (is "?l = ?r")
@@ -2293,8 +2205,7 @@
   unfolding diff_minus fps_compose_uminus fps_compose_add_distrib ..
 
 lemma X_fps_compose:"X oo a = Abs_fps (\<lambda>n. if n = 0 then (0::'a::comm_ring_1) else a$n)"
-  apply (simp add: fps_eq_iff fps_compose_nth)
-  by (simp add: cond_value_iff cond_application_beta setsum_delta power_Suc cong del: if_weak_cong)
+  by (simp add: fps_eq_iff fps_compose_nth mult_delta_left setsum_delta power_Suc)
 
 lemma X_compose_E[simp]: "X oo E (a::'a::{field, recpower}) = E a - 1"
   by (simp add: fps_eq_iff X_fps_compose)
@@ -2332,7 +2243,7 @@
 lemma E_power_mult: "(E (c::'a::{field,recpower,ring_char_0}))^n = E (of_nat n * c)"
   by (induct n, auto simp add: ring_simps E_add_mult power_Suc)
 
-subsection{* Logarithmic series *}  
+subsubsection{* Logarithmic series *}  
 definition "(L::'a::{field, ring_char_0,recpower} fps) 
   = Abs_fps (\<lambda>n. (- 1) ^ Suc n / of_nat n)"
 
@@ -2366,7 +2277,7 @@
     by (simp add: L_nth fps_inv_def)
 qed
 
-subsection{* Formal trigonometric functions  *}
+subsubsection{* Formal trigonometric functions  *}
 
 definition "fps_sin (c::'a::{field, recpower, ring_char_0}) = 
   Abs_fps (\<lambda>n. if even n then 0 else (- 1) ^((n - 1) div 2) * c^n /(of_nat (fact n)))"
@@ -2399,7 +2310,7 @@
   (is "?lhs = ?rhs")
 proof-
   have th0: "\<And>n. - ((- 1::'a) ^ n) = (- 1)^Suc n" by (simp add: power_Suc)
-  have th1: "\<And>n. odd n\<Longrightarrow> Suc ((n - 1) div 2) = Suc n div 2" by presburger
+  have th1: "\<And>n. odd n\<Longrightarrow> Suc ((n - 1) div 2) = Suc n div 2" by presburger (* FIXME: VERY slow! *)
   {fix n::nat
     {assume en: "odd n"
       from en have n0: "n \<noteq>0 " by presburger
@@ -2414,10 +2325,10 @@
       also have "\<dots> = (- ((- 1)^((n - 1) div 2))) * c^Suc n / of_nat (fact n)"
 	unfolding th0 unfolding th1[OF en] by simp
       finally have "?lhs $n = ?rhs$n" using en 
-	by (simp add: fps_sin_def fps_uminus_def ring_simps power_Suc)}
+	by (simp add: fps_sin_def ring_simps power_Suc)}
     then have "?lhs $ n = ?rhs $ n" 
       by (cases "even n", simp_all add: fps_deriv_def fps_sin_def 
-	fps_cos_def fps_uminus_def) }
+	fps_cos_def) }
   then show ?thesis by (auto simp add: fps_eq_iff)
 qed
 
@@ -2430,7 +2341,7 @@
   then have "?lhs = fps_const (?lhs $ 0)"
     unfolding fps_deriv_eq_0_iff .
   also have "\<dots> = 1"
-    by (auto simp add: fps_eq_iff fps_power_def nat_number fps_mult_nth fps_cos_def fps_sin_def)
+    by (auto simp add: fps_eq_iff fps_power_def numeral_2_eq_2 fps_mult_nth fps_cos_def fps_sin_def)
   finally show ?thesis .
 qed
 
@@ -2445,4 +2356,5 @@
     unfolding right_distrib[symmetric]
     by simp
 qed
-end
\ No newline at end of file
+
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Library/Fundamental_Theorem_Algebra.thy	Mon Feb 16 10:15:43 2009 +0100
@@ -0,0 +1,1353 @@
+(* Author: Amine Chaieb, TU Muenchen *)
+
+header{*Fundamental Theorem of Algebra*}
+
+theory Fundamental_Theorem_Algebra
+imports Polynomial Complex
+begin
+
+subsection {* Square root of complex numbers *}
+definition csqrt :: "complex \<Rightarrow> complex" where
+"csqrt z = (if Im z = 0 then
+            if 0 \<le> Re z then Complex (sqrt(Re z)) 0
+            else Complex 0 (sqrt(- Re z))
+           else Complex (sqrt((cmod z + Re z) /2))
+                        ((Im z / abs(Im z)) * sqrt((cmod z - Re z) /2)))"
+
+lemma csqrt[algebra]: "csqrt z ^ 2 = z"
+proof-
+  obtain x y where xy: "z = Complex x y" by (cases z)
+  {assume y0: "y = 0"
+    {assume x0: "x \<ge> 0" 
+      then have ?thesis using y0 xy real_sqrt_pow2[OF x0]
+	by (simp add: csqrt_def power2_eq_square)}
+    moreover
+    {assume "\<not> x \<ge> 0" hence x0: "- x \<ge> 0" by arith
+      then have ?thesis using y0 xy real_sqrt_pow2[OF x0] 
+	by (simp add: csqrt_def power2_eq_square) }
+    ultimately have ?thesis by blast}
+  moreover
+  {assume y0: "y\<noteq>0"
+    {fix x y
+      let ?z = "Complex x y"
+      from abs_Re_le_cmod[of ?z] have tha: "abs x \<le> cmod ?z" by auto
+      hence "cmod ?z - x \<ge> 0" "cmod ?z + x \<ge> 0" by arith+ 
+      hence "(sqrt (x * x + y * y) + x) / 2 \<ge> 0" "(sqrt (x * x + y * y) - x) / 2 \<ge> 0" by (simp_all add: power2_eq_square) }
+    note th = this
+    have sq4: "\<And>x::real. x^2 / 4 = (x / 2) ^ 2" 
+      by (simp add: power2_eq_square) 
+    from th[of x y]
+    have sq4': "sqrt (((sqrt (x * x + y * y) + x)^2 / 4)) = (sqrt (x * x + y * y) + x) / 2" "sqrt (((sqrt (x * x + y * y) - x)^2 / 4)) = (sqrt (x * x + y * y) - x) / 2" unfolding sq4 by simp_all
+    then have th1: "sqrt ((sqrt (x * x + y * y) + x) * (sqrt (x * x + y * y) + x) / 4) - sqrt ((sqrt (x * x + y * y) - x) * (sqrt (x * x + y * y) - x) / 4) = x"
+      unfolding power2_eq_square by simp 
+    have "sqrt 4 = sqrt (2^2)" by simp 
+    hence sqrt4: "sqrt 4 = 2" by (simp only: real_sqrt_abs)
+    have th2: "2 *(y * sqrt ((sqrt (x * x + y * y) - x) * (sqrt (x * x + y * y) + x) / 4)) / \<bar>y\<bar> = y"
+      using iffD2[OF real_sqrt_pow2_iff sum_power2_ge_zero[of x y]] y0
+      unfolding power2_eq_square 
+      by (simp add: algebra_simps real_sqrt_divide sqrt4)
+     from y0 xy have ?thesis  apply (simp add: csqrt_def power2_eq_square)
+       apply (simp add: real_sqrt_sum_squares_mult_ge_zero[of x y] real_sqrt_pow2[OF th(1)[of x y], unfolded power2_eq_square] real_sqrt_pow2[OF th(2)[of x y], unfolded power2_eq_square] real_sqrt_mult[symmetric])
+      using th1 th2  ..}
+  ultimately show ?thesis by blast
+qed
+
+
+subsection{* More lemmas about module of complex numbers *}
+
+lemma complex_of_real_power: "complex_of_real x ^ n = complex_of_real (x^n)"
+  by (rule of_real_power [symmetric])
+
+lemma real_down2: "(0::real) < d1 \<Longrightarrow> 0 < d2 ==> EX e. 0 < e & e < d1 & e < d2"
+  apply (rule exI[where x = "min d1 d2 / 2"])
+  by (simp add: field_simps min_def)
+
+text{* The triangle inequality for cmod *}
+lemma complex_mod_triangle_sub: "cmod w \<le> cmod (w + z) + norm z"
+  using complex_mod_triangle_ineq2[of "w + z" "-z"] by auto
+
+subsection{* Basic lemmas about complex polynomials *}
+
+lemma poly_bound_exists:
+  shows "\<exists>m. m > 0 \<and> (\<forall>z. cmod z <= r \<longrightarrow> cmod (poly p z) \<le> m)"
+proof(induct p)
+  case 0 thus ?case by (rule exI[where x=1], simp) 
+next
+  case (pCons c cs)
+  from pCons.hyps obtain m where m: "\<forall>z. cmod z \<le> r \<longrightarrow> cmod (poly cs z) \<le> m"
+    by blast
+  let ?k = " 1 + cmod c + \<bar>r * m\<bar>"
+  have kp: "?k > 0" using abs_ge_zero[of "r*m"] norm_ge_zero[of c] by arith
+  {fix z
+    assume H: "cmod z \<le> r"
+    from m H have th: "cmod (poly cs z) \<le> m" by blast
+    from H have rp: "r \<ge> 0" using norm_ge_zero[of z] by arith
+    have "cmod (poly (pCons c cs) z) \<le> cmod c + cmod (z* poly cs z)"
+      using norm_triangle_ineq[of c "z* poly cs z"] by simp
+    also have "\<dots> \<le> cmod c + r*m" using mult_mono[OF H th rp norm_ge_zero[of "poly cs z"]] by (simp add: norm_mult)
+    also have "\<dots> \<le> ?k" by simp
+    finally have "cmod (poly (pCons c cs) z) \<le> ?k" .}
+  with kp show ?case by blast
+qed
+
+
+text{* Offsetting the variable in a polynomial gives another of same degree *}
+
+definition
+  "offset_poly p h = poly_rec 0 (\<lambda>a p q. smult h q + pCons a q) p"
+
+lemma offset_poly_0: "offset_poly 0 h = 0"
+  unfolding offset_poly_def by (simp add: poly_rec_0)
+
+lemma offset_poly_pCons:
+  "offset_poly (pCons a p) h =
+    smult h (offset_poly p h) + pCons a (offset_poly p h)"
+  unfolding offset_poly_def by (simp add: poly_rec_pCons)
+
+lemma offset_poly_single: "offset_poly [:a:] h = [:a:]"
+by (simp add: offset_poly_pCons offset_poly_0)
+
+lemma poly_offset_poly: "poly (offset_poly p h) x = poly p (h + x)"
+apply (induct p)
+apply (simp add: offset_poly_0)
+apply (simp add: offset_poly_pCons algebra_simps)
+done
+
+lemma offset_poly_eq_0_lemma: "smult c p + pCons a p = 0 \<Longrightarrow> p = 0"
+by (induct p arbitrary: a, simp, force)
+
+lemma offset_poly_eq_0_iff: "offset_poly p h = 0 \<longleftrightarrow> p = 0"
+apply (safe intro!: offset_poly_0)
+apply (induct p, simp)
+apply (simp add: offset_poly_pCons)
+apply (frule offset_poly_eq_0_lemma, simp)
+done
+
+lemma degree_offset_poly: "degree (offset_poly p h) = degree p"
+apply (induct p)
+apply (simp add: offset_poly_0)
+apply (case_tac "p = 0")
+apply (simp add: offset_poly_0 offset_poly_pCons)
+apply (simp add: offset_poly_pCons)
+apply (subst degree_add_eq_right)
+apply (rule le_less_trans [OF degree_smult_le])
+apply (simp add: offset_poly_eq_0_iff)
+apply (simp add: offset_poly_eq_0_iff)
+done
+
+definition
+  "psize p = (if p = 0 then 0 else Suc (degree p))"
+
+lemma psize_eq_0_iff [simp]: "psize p = 0 \<longleftrightarrow> p = 0"
+  unfolding psize_def by simp
+
+lemma poly_offset: "\<exists> q. psize q = psize p \<and> (\<forall>x. poly q (x::complex) = poly p (a + x))"
+proof (intro exI conjI)
+  show "psize (offset_poly p a) = psize p"
+    unfolding psize_def
+    by (simp add: offset_poly_eq_0_iff degree_offset_poly)
+  show "\<forall>x. poly (offset_poly p a) x = poly p (a + x)"
+    by (simp add: poly_offset_poly)
+qed
+
+text{* An alternative useful formulation of completeness of the reals *}
+lemma real_sup_exists: assumes ex: "\<exists>x. P x" and bz: "\<exists>z. \<forall>x. P x \<longrightarrow> x < z"
+  shows "\<exists>(s::real). \<forall>y. (\<exists>x. P x \<and> y < x) \<longleftrightarrow> y < s"
+proof-
+  from ex bz obtain x Y where x: "P x" and Y: "\<And>x. P x \<Longrightarrow> x < Y"  by blast
+  from ex have thx:"\<exists>x. x \<in> Collect P" by blast
+  from bz have thY: "\<exists>Y. isUb UNIV (Collect P) Y" 
+    by(auto simp add: isUb_def isLub_def setge_def setle_def leastP_def Ball_def order_le_less)
+  from reals_complete[OF thx thY] obtain L where L: "isLub UNIV (Collect P) L"
+    by blast
+  from Y[OF x] have xY: "x < Y" .
+  from L have L': "\<forall>x. P x \<longrightarrow> x \<le> L" by (auto simp add: isUb_def isLub_def setge_def setle_def leastP_def Ball_def)  
+  from Y have Y': "\<forall>x. P x \<longrightarrow> x \<le> Y" 
+    apply (clarsimp, atomize (full)) by auto 
+  from L Y' have "L \<le> Y" by (auto simp add: isUb_def isLub_def setge_def setle_def leastP_def Ball_def)
+  {fix y
+    {fix z assume z: "P z" "y < z"
+      from L' z have "y < L" by auto }
+    moreover
+    {assume yL: "y < L" "\<forall>z. P z \<longrightarrow> \<not> y < z"
+      hence nox: "\<forall>z. P z \<longrightarrow> y \<ge> z" by auto
+      from nox L have "y \<ge> L" by (auto simp add: isUb_def isLub_def setge_def setle_def leastP_def Ball_def) 
+      with yL(1) have False  by arith}
+    ultimately have "(\<exists>x. P x \<and> y < x) \<longleftrightarrow> y < L" by blast}
+  thus ?thesis by blast
+qed
+
+
+subsection{* Some theorems about Sequences*}
+text{* Given a binary function @{text "f:: nat \<Rightarrow> 'a \<Rightarrow> 'a"}, its values are uniquely determined by a function g *}
+
+lemma num_Axiom: "EX! g. g 0 = e \<and> (\<forall>n. g (Suc n) = f n (g n))"
+  unfolding Ex1_def
+  apply (rule_tac x="nat_rec e f" in exI)
+  apply (rule conjI)+
+apply (rule def_nat_rec_0, simp)
+apply (rule allI, rule def_nat_rec_Suc, simp)
+apply (rule allI, rule impI, rule ext)
+apply (erule conjE)
+apply (induct_tac x)
+apply (simp add: nat_rec_0)
+apply (erule_tac x="n" in allE)
+apply (simp)
+done
+
+text{* for any sequence, there is a mootonic subsequence *}
+lemma seq_monosub: "\<exists>f. subseq f \<and> monoseq (\<lambda> n. (s (f n)))"
+proof-
+  {assume H: "\<forall>n. \<exists>p >n. \<forall> m\<ge>p. s m \<le> s p"
+    let ?P = "\<lambda> p n. p > n \<and> (\<forall>m \<ge> p. s m \<le> s p)"
+    from num_Axiom[of "SOME p. ?P p 0" "\<lambda>p n. SOME p. ?P p n"]
+    obtain f where f: "f 0 = (SOME p. ?P p 0)" "\<forall>n. f (Suc n) = (SOME p. ?P p (f n))" by blast
+    have "?P (f 0) 0"  unfolding f(1) some_eq_ex[of "\<lambda>p. ?P p 0"]
+      using H apply - 
+      apply (erule allE[where x=0], erule exE, rule_tac x="p" in exI) 
+      unfolding order_le_less by blast 
+    hence f0: "f 0 > 0" "\<forall>m \<ge> f 0. s m \<le> s (f 0)" by blast+
+    {fix n
+      have "?P (f (Suc n)) (f n)" 
+	unfolding f(2)[rule_format, of n] some_eq_ex[of "\<lambda>p. ?P p (f n)"]
+	using H apply - 
+      apply (erule allE[where x="f n"], erule exE, rule_tac x="p" in exI) 
+      unfolding order_le_less by blast 
+    hence "f (Suc n) > f n" "\<forall>m \<ge> f (Suc n). s m \<le> s (f (Suc n))" by blast+}
+  note fSuc = this
+    {fix p q assume pq: "p \<ge> f q"
+      have "s p \<le> s(f(q))"  using f0(2)[rule_format, of p] pq fSuc
+	by (cases q, simp_all) }
+    note pqth = this
+    {fix q
+      have "f (Suc q) > f q" apply (induct q) 
+	using f0(1) fSuc(1)[of 0] apply simp by (rule fSuc(1))}
+    note fss = this
+    from fss have th1: "subseq f" unfolding subseq_Suc_iff ..
+    {fix a b 
+      have "f a \<le> f (a + b)"
+      proof(induct b)
+	case 0 thus ?case by simp
+      next
+	case (Suc b)
+	from fSuc(1)[of "a + b"] Suc.hyps show ?case by simp
+      qed}
+    note fmon0 = this
+    have "monoseq (\<lambda>n. s (f n))" 
+    proof-
+      {fix n
+	have "s (f n) \<ge> s (f (Suc n))" 
+	proof(cases n)
+	  case 0
+	  assume n0: "n = 0"
+	  from fSuc(1)[of 0] have th0: "f 0 \<le> f (Suc 0)" by simp
+	  from f0(2)[rule_format, OF th0] show ?thesis  using n0 by simp
+	next
+	  case (Suc m)
+	  assume m: "n = Suc m"
+	  from fSuc(1)[of n] m have th0: "f (Suc m) \<le> f (Suc (Suc m))" by simp
+	  from m fSuc(2)[rule_format, OF th0] show ?thesis by simp 
+	qed}
+      thus "monoseq (\<lambda>n. s (f n))" unfolding monoseq_Suc by blast 
+    qed
+    with th1 have ?thesis by blast}
+  moreover
+  {fix N assume N: "\<forall>p >N. \<exists> m\<ge>p. s m > s p"
+    {fix p assume p: "p \<ge> Suc N" 
+      hence pN: "p > N" by arith with N obtain m where m: "m \<ge> p" "s m > s p" by blast
+      have "m \<noteq> p" using m(2) by auto 
+      with m have "\<exists>m>p. s p < s m" by - (rule exI[where x=m], auto)}
+    note th0 = this
+    let ?P = "\<lambda>m x. m > x \<and> s x < s m"
+    from num_Axiom[of "SOME x. ?P x (Suc N)" "\<lambda>m x. SOME y. ?P y x"]
+    obtain f where f: "f 0 = (SOME x. ?P x (Suc N))" 
+      "\<forall>n. f (Suc n) = (SOME m. ?P m (f n))" by blast
+    have "?P (f 0) (Suc N)"  unfolding f(1) some_eq_ex[of "\<lambda>p. ?P p (Suc N)"]
+      using N apply - 
+      apply (erule allE[where x="Suc N"], clarsimp)
+      apply (rule_tac x="m" in exI)
+      apply auto
+      apply (subgoal_tac "Suc N \<noteq> m")
+      apply simp
+      apply (rule ccontr, simp)
+      done
+    hence f0: "f 0 > Suc N" "s (Suc N) < s (f 0)" by blast+
+    {fix n
+      have "f n > N \<and> ?P (f (Suc n)) (f n)"
+	unfolding f(2)[rule_format, of n] some_eq_ex[of "\<lambda>p. ?P p (f n)"]
+      proof (induct n)
+	case 0 thus ?case
+	  using f0 N apply auto 
+	  apply (erule allE[where x="f 0"], clarsimp) 
+	  apply (rule_tac x="m" in exI, simp)
+	  by (subgoal_tac "f 0 \<noteq> m", auto)
+      next
+	case (Suc n)
+	from Suc.hyps have Nfn: "N < f n" by blast
+	from Suc.hyps obtain m where m: "m > f n" "s (f n) < s m" by blast
+	with Nfn have mN: "m > N" by arith
+	note key = Suc.hyps[unfolded some_eq_ex[of "\<lambda>p. ?P p (f n)", symmetric] f(2)[rule_format, of n, symmetric]]
+	
+	from key have th0: "f (Suc n) > N" by simp
+	from N[rule_format, OF th0]
+	obtain m' where m': "m' \<ge> f (Suc n)" "s (f (Suc n)) < s m'" by blast
+	have "m' \<noteq> f (Suc (n))" apply (rule ccontr) using m'(2) by auto
+	hence "m' > f (Suc n)" using m'(1) by simp
+	with key m'(2) show ?case by auto
+      qed}
+    note fSuc = this
+    {fix n
+      have "f n \<ge> Suc N \<and> f(Suc n) > f n \<and> s(f n) < s(f(Suc n))" using fSuc[of n] by auto 
+      hence "f n \<ge> Suc N" "f(Suc n) > f n" "s(f n) < s(f(Suc n))" by blast+}
+    note thf = this
+    have sqf: "subseq f" unfolding subseq_Suc_iff using thf by simp
+    have "monoseq (\<lambda>n. s (f n))"  unfolding monoseq_Suc using thf
+      apply -
+      apply (rule disjI1)
+      apply auto
+      apply (rule order_less_imp_le)
+      apply blast
+      done
+    then have ?thesis  using sqf by blast}
+  ultimately show ?thesis unfolding linorder_not_less[symmetric] by blast
+qed
+
+lemma seq_suble: assumes sf: "subseq f" shows "n \<le> f n"
+proof(induct n)
+  case 0 thus ?case by simp
+next
+  case (Suc n)
+  from sf[unfolded subseq_Suc_iff, rule_format, of n] Suc.hyps
+  have "n < f (Suc n)" by arith 
+  thus ?case by arith
+qed
+
+subsection {* Fundamental theorem of algebra *}
+lemma  unimodular_reduce_norm:
+  assumes md: "cmod z = 1"
+  shows "cmod (z + 1) < 1 \<or> cmod (z - 1) < 1 \<or> cmod (z + ii) < 1 \<or> cmod (z - ii) < 1"
+proof-
+  obtain x y where z: "z = Complex x y " by (cases z, auto)
+  from md z have xy: "x^2 + y^2 = 1" by (simp add: cmod_def)
+  {assume C: "cmod (z + 1) \<ge> 1" "cmod (z - 1) \<ge> 1" "cmod (z + ii) \<ge> 1" "cmod (z - ii) \<ge> 1"
+    from C z xy have "2*x \<le> 1" "2*x \<ge> -1" "2*y \<le> 1" "2*y \<ge> -1"
+      by (simp_all add: cmod_def power2_eq_square algebra_simps)
+    hence "abs (2*x) \<le> 1" "abs (2*y) \<le> 1" by simp_all
+    hence "(abs (2 * x))^2 <= 1^2" "(abs (2 * y)) ^2 <= 1^2"
+      by - (rule power_mono, simp, simp)+
+    hence th0: "4*x^2 \<le> 1" "4*y^2 \<le> 1" 
+      by (simp_all  add: power2_abs power_mult_distrib)
+    from add_mono[OF th0] xy have False by simp }
+  thus ?thesis unfolding linorder_not_le[symmetric] by blast
+qed
+
+text{* Hence we can always reduce modulus of @{text "1 + b z^n"} if nonzero *}
+lemma reduce_poly_simple:
+ assumes b: "b \<noteq> 0" and n: "n\<noteq>0"
+  shows "\<exists>z. cmod (1 + b * z^n) < 1"
+using n
+proof(induct n rule: nat_less_induct)
+  fix n
+  assume IH: "\<forall>m<n. m \<noteq> 0 \<longrightarrow> (\<exists>z. cmod (1 + b * z ^ m) < 1)" and n: "n \<noteq> 0"
+  let ?P = "\<lambda>z n. cmod (1 + b * z ^ n) < 1"
+  {assume e: "even n"
+    hence "\<exists>m. n = 2*m" by presburger
+    then obtain m where m: "n = 2*m" by blast
+    from n m have "m\<noteq>0" "m < n" by presburger+
+    with IH[rule_format, of m] obtain z where z: "?P z m" by blast
+    from z have "?P (csqrt z) n" by (simp add: m power_mult csqrt)
+    hence "\<exists>z. ?P z n" ..}
+  moreover
+  {assume o: "odd n"
+    from b have b': "b^2 \<noteq> 0" unfolding power2_eq_square by simp
+    have "Im (inverse b) * (Im (inverse b) * \<bar>Im b * Im b + Re b * Re b\<bar>) +
+    Re (inverse b) * (Re (inverse b) * \<bar>Im b * Im b + Re b * Re b\<bar>) = 
+    ((Re (inverse b))^2 + (Im (inverse b))^2) * \<bar>Im b * Im b + Re b * Re b\<bar>" by algebra
+    also have "\<dots> = cmod (inverse b) ^2 * cmod b ^ 2" 
+      apply (simp add: cmod_def) using realpow_two_le_add_order[of "Re b" "Im b"]
+      by (simp add: power2_eq_square)
+    finally 
+    have th0: "Im (inverse b) * (Im (inverse b) * \<bar>Im b * Im b + Re b * Re b\<bar>) +
+    Re (inverse b) * (Re (inverse b) * \<bar>Im b * Im b + Re b * Re b\<bar>) =
+    1" 
+      apply (simp add: power2_eq_square norm_mult[symmetric] norm_inverse[symmetric])
+      using right_inverse[OF b']
+      by (simp add: power2_eq_square[symmetric] power_inverse[symmetric] algebra_simps)
+    have th0: "cmod (complex_of_real (cmod b) / b) = 1"
+      apply (simp add: complex_Re_mult cmod_def power2_eq_square Re_complex_of_real Im_complex_of_real divide_inverse algebra_simps )
+      by (simp add: real_sqrt_mult[symmetric] th0)        
+    from o have "\<exists>m. n = Suc (2*m)" by presburger+
+    then obtain m where m: "n = Suc (2*m)" by blast
+    from unimodular_reduce_norm[OF th0] o
+    have "\<exists>v. cmod (complex_of_real (cmod b) / b + v^n) < 1"
+      apply (cases "cmod (complex_of_real (cmod b) / b + 1) < 1", rule_tac x="1" in exI, simp)
+      apply (cases "cmod (complex_of_real (cmod b) / b - 1) < 1", rule_tac x="-1" in exI, simp add: diff_def)
+      apply (cases "cmod (complex_of_real (cmod b) / b + ii) < 1")
+      apply (cases "even m", rule_tac x="ii" in exI, simp add: m power_mult)
+      apply (rule_tac x="- ii" in exI, simp add: m power_mult)
+      apply (cases "even m", rule_tac x="- ii" in exI, simp add: m power_mult diff_def)
+      apply (rule_tac x="ii" in exI, simp add: m power_mult diff_def)
+      done
+    then obtain v where v: "cmod (complex_of_real (cmod b) / b + v^n) < 1" by blast
+    let ?w = "v / complex_of_real (root n (cmod b))"
+    from odd_real_root_pow[OF o, of "cmod b"]
+    have th1: "?w ^ n = v^n / complex_of_real (cmod b)" 
+      by (simp add: power_divide complex_of_real_power)
+    have th2:"cmod (complex_of_real (cmod b) / b) = 1" using b by (simp add: norm_divide)
+    hence th3: "cmod (complex_of_real (cmod b) / b) \<ge> 0" by simp
+    have th4: "cmod (complex_of_real (cmod b) / b) *
+   cmod (1 + b * (v ^ n / complex_of_real (cmod b)))
+   < cmod (complex_of_real (cmod b) / b) * 1"
+      apply (simp only: norm_mult[symmetric] right_distrib)
+      using b v by (simp add: th2)
+
+    from mult_less_imp_less_left[OF th4 th3]
+    have "?P ?w n" unfolding th1 . 
+    hence "\<exists>z. ?P z n" .. }
+  ultimately show "\<exists>z. ?P z n" by blast
+qed
+
+
+text{* Bolzano-Weierstrass type property for closed disc in complex plane. *}
+
+lemma metric_bound_lemma: "cmod (x - y) <= \<bar>Re x - Re y\<bar> + \<bar>Im x - Im y\<bar>"
+  using real_sqrt_sum_squares_triangle_ineq[of "Re x - Re y" 0 0 "Im x - Im y" ]
+  unfolding cmod_def by simp
+
+lemma bolzano_weierstrass_complex_disc:
+  assumes r: "\<forall>n. cmod (s n) \<le> r"
+  shows "\<exists>f z. subseq f \<and> (\<forall>e >0. \<exists>N. \<forall>n \<ge> N. cmod (s (f n) - z) < e)"
+proof-
+  from seq_monosub[of "Re o s"] 
+  obtain f g where f: "subseq f" "monoseq (\<lambda>n. Re (s (f n)))" 
+    unfolding o_def by blast
+  from seq_monosub[of "Im o s o f"] 
+  obtain g where g: "subseq g" "monoseq (\<lambda>n. Im (s(f(g n))))" unfolding o_def by blast  
+  let ?h = "f o g"
+  from r[rule_format, of 0] have rp: "r \<ge> 0" using norm_ge_zero[of "s 0"] by arith 
+  have th:"\<forall>n. r + 1 \<ge> \<bar> Re (s n)\<bar>" 
+  proof
+    fix n
+    from abs_Re_le_cmod[of "s n"] r[rule_format, of n]  show "\<bar>Re (s n)\<bar> \<le> r + 1" by arith
+  qed
+  have conv1: "convergent (\<lambda>n. Re (s ( f n)))"
+    apply (rule Bseq_monoseq_convergent)
+    apply (simp add: Bseq_def)
+    apply (rule exI[where x= "r + 1"])
+    using th rp apply simp
+    using f(2) .
+  have th:"\<forall>n. r + 1 \<ge> \<bar> Im (s n)\<bar>" 
+  proof
+    fix n
+    from abs_Im_le_cmod[of "s n"] r[rule_format, of n]  show "\<bar>Im (s n)\<bar> \<le> r + 1" by arith
+  qed
+
+  have conv2: "convergent (\<lambda>n. Im (s (f (g n))))"
+    apply (rule Bseq_monoseq_convergent)
+    apply (simp add: Bseq_def)
+    apply (rule exI[where x= "r + 1"])
+    using th rp apply simp
+    using g(2) .
+
+  from conv1[unfolded convergent_def] obtain x where "LIMSEQ (\<lambda>n. Re (s (f n))) x" 
+    by blast 
+  hence  x: "\<forall>r>0. \<exists>n0. \<forall>n\<ge>n0. \<bar> Re (s (f n)) - x \<bar> < r" 
+    unfolding LIMSEQ_def real_norm_def .
+
+  from conv2[unfolded convergent_def] obtain y where "LIMSEQ (\<lambda>n. Im (s (f (g n)))) y" 
+    by blast 
+  hence  y: "\<forall>r>0. \<exists>n0. \<forall>n\<ge>n0. \<bar> Im (s (f (g n))) - y \<bar> < r" 
+    unfolding LIMSEQ_def real_norm_def .
+  let ?w = "Complex x y"
+  from f(1) g(1) have hs: "subseq ?h" unfolding subseq_def by auto 
+  {fix e assume ep: "e > (0::real)"
+    hence e2: "e/2 > 0" by simp
+    from x[rule_format, OF e2] y[rule_format, OF e2]
+    obtain N1 N2 where N1: "\<forall>n\<ge>N1. \<bar>Re (s (f n)) - x\<bar> < e / 2" and N2: "\<forall>n\<ge>N2. \<bar>Im (s (f (g n))) - y\<bar> < e / 2" by blast
+    {fix n assume nN12: "n \<ge> N1 + N2"
+      hence nN1: "g n \<ge> N1" and nN2: "n \<ge> N2" using seq_suble[OF g(1), of n] by arith+
+      from add_strict_mono[OF N1[rule_format, OF nN1] N2[rule_format, OF nN2]]
+      have "cmod (s (?h n) - ?w) < e" 
+	using metric_bound_lemma[of "s (f (g n))" ?w] by simp }
+    hence "\<exists>N. \<forall>n\<ge>N. cmod (s (?h n) - ?w) < e" by blast }
+  with hs show ?thesis  by blast  
+qed
+
+text{* Polynomial is continuous. *}
+
+lemma poly_cont:
+  assumes ep: "e > 0" 
+  shows "\<exists>d >0. \<forall>w. 0 < cmod (w - z) \<and> cmod (w - z) < d \<longrightarrow> cmod (poly p w - poly p z) < e"
+proof-
+  obtain q where q: "degree q = degree p" "\<And>x. poly q x = poly p (z + x)"
+  proof
+    show "degree (offset_poly p z) = degree p"
+      by (rule degree_offset_poly)
+    show "\<And>x. poly (offset_poly p z) x = poly p (z + x)"
+      by (rule poly_offset_poly)
+  qed
+  {fix w
+    note q(2)[of "w - z", simplified]}
+  note th = this
+  show ?thesis unfolding th[symmetric]
+  proof(induct q)
+    case 0 thus ?case  using ep by auto
+  next
+    case (pCons c cs)
+    from poly_bound_exists[of 1 "cs"] 
+    obtain m where m: "m > 0" "\<And>z. cmod z \<le> 1 \<Longrightarrow> cmod (poly cs z) \<le> m" by blast
+    from ep m(1) have em0: "e/m > 0" by (simp add: field_simps)
+    have one0: "1 > (0::real)"  by arith
+    from real_lbound_gt_zero[OF one0 em0] 
+    obtain d where d: "d >0" "d < 1" "d < e / m" by blast
+    from d(1,3) m(1) have dm: "d*m > 0" "d*m < e" 
+      by (simp_all add: field_simps real_mult_order)
+    show ?case 
+      proof(rule ex_forward[OF real_lbound_gt_zero[OF one0 em0]], clarsimp simp add: norm_mult)
+	fix d w
+	assume H: "d > 0" "d < 1" "d < e/m" "w\<noteq>z" "cmod (w-z) < d"
+	hence d1: "cmod (w-z) \<le> 1" "d \<ge> 0" by simp_all
+	from H(3) m(1) have dme: "d*m < e" by (simp add: field_simps)
+	from H have th: "cmod (w-z) \<le> d" by simp 
+	from mult_mono[OF th m(2)[OF d1(1)] d1(2) norm_ge_zero] dme
+	show "cmod (w - z) * cmod (poly cs (w - z)) < e" by simp
+      qed  
+    qed
+qed
+
+text{* Hence a polynomial attains minimum on a closed disc 
+  in the complex plane. *}
+lemma  poly_minimum_modulus_disc:
+  "\<exists>z. \<forall>w. cmod w \<le> r \<longrightarrow> cmod (poly p z) \<le> cmod (poly p w)"
+proof-
+  {assume "\<not> r \<ge> 0" hence ?thesis unfolding linorder_not_le
+      apply -
+      apply (rule exI[where x=0]) 
+      apply auto
+      apply (subgoal_tac "cmod w < 0")
+      apply simp
+      apply arith
+      done }
+  moreover
+  {assume rp: "r \<ge> 0"
+    from rp have "cmod 0 \<le> r \<and> cmod (poly p 0) = - (- cmod (poly p 0))" by simp 
+    hence mth1: "\<exists>x z. cmod z \<le> r \<and> cmod (poly p z) = - x"  by blast
+    {fix x z
+      assume H: "cmod z \<le> r" "cmod (poly p z) = - x" "\<not>x < 1"
+      hence "- x < 0 " by arith
+      with H(2) norm_ge_zero[of "poly p z"]  have False by simp }
+    then have mth2: "\<exists>z. \<forall>x. (\<exists>z. cmod z \<le> r \<and> cmod (poly p z) = - x) \<longrightarrow> x < z" by blast
+    from real_sup_exists[OF mth1 mth2] obtain s where 
+      s: "\<forall>y. (\<exists>x. (\<exists>z. cmod z \<le> r \<and> cmod (poly p z) = - x) \<and> y < x) \<longleftrightarrow>(y < s)" by blast
+    let ?m = "-s"
+    {fix y
+      from s[rule_format, of "-y"] have 
+    "(\<exists>z x. cmod z \<le> r \<and> -(- cmod (poly p z)) < y) \<longleftrightarrow> ?m < y" 
+	unfolding minus_less_iff[of y ] equation_minus_iff by blast }
+    note s1 = this[unfolded minus_minus]
+    from s1[of ?m] have s1m: "\<And>z x. cmod z \<le> r \<Longrightarrow> cmod (poly p z) \<ge> ?m" 
+      by auto
+    {fix n::nat
+      from s1[rule_format, of "?m + 1/real (Suc n)"] 
+      have "\<exists>z. cmod z \<le> r \<and> cmod (poly p z) < - s + 1 / real (Suc n)"
+	by simp}
+    hence th: "\<forall>n. \<exists>z. cmod z \<le> r \<and> cmod (poly p z) < - s + 1 / real (Suc n)" ..
+    from choice[OF th] obtain g where 
+      g: "\<forall>n. cmod (g n) \<le> r" "\<forall>n. cmod (poly p (g n)) <?m+1 /real(Suc n)" 
+      by blast
+    from bolzano_weierstrass_complex_disc[OF g(1)] 
+    obtain f z where fz: "subseq f" "\<forall>e>0. \<exists>N. \<forall>n\<ge>N. cmod (g (f n) - z) < e"
+      by blast    
+    {fix w 
+      assume wr: "cmod w \<le> r"
+      let ?e = "\<bar>cmod (poly p z) - ?m\<bar>"
+      {assume e: "?e > 0"
+	hence e2: "?e/2 > 0" by simp
+	from poly_cont[OF e2, of z p] obtain d where
+	  d: "d>0" "\<forall>w. 0<cmod (w - z)\<and> cmod(w - z) < d \<longrightarrow> cmod(poly p w - poly p z) < ?e/2" by blast
+	{fix w assume w: "cmod (w - z) < d"
+	  have "cmod(poly p w - poly p z) < ?e / 2"
+	    using d(2)[rule_format, of w] w e by (cases "w=z", simp_all)}
+	note th1 = this
+	
+	from fz(2)[rule_format, OF d(1)] obtain N1 where 
+	  N1: "\<forall>n\<ge>N1. cmod (g (f n) - z) < d" by blast
+	from reals_Archimedean2[of "2/?e"] obtain N2::nat where
+	  N2: "2/?e < real N2" by blast
+	have th2: "cmod(poly p (g(f(N1 + N2))) - poly p z) < ?e/2"
+	  using N1[rule_format, of "N1 + N2"] th1 by simp
+	{fix a b e2 m :: real
+	have "a < e2 \<Longrightarrow> abs(b - m) < e2 \<Longrightarrow> 2 * e2 <= abs(b - m) + a
+          ==> False" by arith}
+      note th0 = this
+      have ath: 
+	"\<And>m x e. m <= x \<Longrightarrow>  x < m + e ==> abs(x - m::real) < e" by arith
+      from s1m[OF g(1)[rule_format]]
+      have th31: "?m \<le> cmod(poly p (g (f (N1 + N2))))" .
+      from seq_suble[OF fz(1), of "N1+N2"]
+      have th00: "real (Suc (N1+N2)) \<le> real (Suc (f (N1+N2)))" by simp
+      have th000: "0 \<le> (1::real)" "(1::real) \<le> 1" "real (Suc (N1+N2)) > 0"  
+	using N2 by auto
+      from frac_le[OF th000 th00] have th00: "?m +1 / real (Suc (f (N1 + N2))) \<le> ?m + 1 / real (Suc (N1 + N2))" by simp
+      from g(2)[rule_format, of "f (N1 + N2)"]
+      have th01:"cmod (poly p (g (f (N1 + N2)))) < - s + 1 / real (Suc (f (N1 + N2)))" .
+      from order_less_le_trans[OF th01 th00]
+      have th32: "cmod(poly p (g (f (N1 + N2)))) < ?m + (1/ real(Suc (N1 + N2)))" .
+      from N2 have "2/?e < real (Suc (N1 + N2))" by arith
+      with e2 less_imp_inverse_less[of "2/?e" "real (Suc (N1 + N2))"]
+      have "?e/2 > 1/ real (Suc (N1 + N2))" by (simp add: inverse_eq_divide)
+      with ath[OF th31 th32]
+      have thc1:"\<bar>cmod(poly p (g (f (N1 + N2)))) - ?m\<bar>< ?e/2" by arith  
+      have ath2: "\<And>(a::real) b c m. \<bar>a - b\<bar> <= c ==> \<bar>b - m\<bar> <= \<bar>a - m\<bar> + c" 
+	by arith
+      have th22: "\<bar>cmod (poly p (g (f (N1 + N2)))) - cmod (poly p z)\<bar>
+\<le> cmod (poly p (g (f (N1 + N2))) - poly p z)" 
+	by (simp add: norm_triangle_ineq3)
+      from ath2[OF th22, of ?m]
+      have thc2: "2*(?e/2) \<le> \<bar>cmod(poly p (g (f (N1 + N2)))) - ?m\<bar> + cmod (poly p (g (f (N1 + N2))) - poly p z)" by simp
+      from th0[OF th2 thc1 thc2] have False .}
+      hence "?e = 0" by auto
+      then have "cmod (poly p z) = ?m" by simp  
+      with s1m[OF wr]
+      have "cmod (poly p z) \<le> cmod (poly p w)" by simp }
+    hence ?thesis by blast}
+  ultimately show ?thesis by blast
+qed
+
+lemma "(rcis (sqrt (abs r)) (a/2)) ^ 2 = rcis (abs r) a"
+  unfolding power2_eq_square
+  apply (simp add: rcis_mult)
+  apply (simp add: power2_eq_square[symmetric])
+  done
+
+lemma cispi: "cis pi = -1" 
+  unfolding cis_def
+  by simp
+
+lemma "(rcis (sqrt (abs r)) ((pi + a)/2)) ^ 2 = rcis (- abs r) a"
+  unfolding power2_eq_square
+  apply (simp add: rcis_mult add_divide_distrib)
+  apply (simp add: power2_eq_square[symmetric] rcis_def cispi cis_mult[symmetric])
+  done
+
+text {* Nonzero polynomial in z goes to infinity as z does. *}
+
+lemma poly_infinity:
+  assumes ex: "p \<noteq> 0"
+  shows "\<exists>r. \<forall>z. r \<le> cmod z \<longrightarrow> d \<le> cmod (poly (pCons a p) z)"
+using ex
+proof(induct p arbitrary: a d)
+  case (pCons c cs a d) 
+  {assume H: "cs \<noteq> 0"
+    with pCons.hyps obtain r where r: "\<forall>z. r \<le> cmod z \<longrightarrow> d + cmod a \<le> cmod (poly (pCons c cs) z)" by blast
+    let ?r = "1 + \<bar>r\<bar>"
+    {fix z assume h: "1 + \<bar>r\<bar> \<le> cmod z"
+      have r0: "r \<le> cmod z" using h by arith
+      from r[rule_format, OF r0]
+      have th0: "d + cmod a \<le> 1 * cmod(poly (pCons c cs) z)" by arith
+      from h have z1: "cmod z \<ge> 1" by arith
+      from order_trans[OF th0 mult_right_mono[OF z1 norm_ge_zero[of "poly (pCons c cs) z"]]]
+      have th1: "d \<le> cmod(z * poly (pCons c cs) z) - cmod a"
+	unfolding norm_mult by (simp add: algebra_simps)
+      from complex_mod_triangle_sub[of "z * poly (pCons c cs) z" a]
+      have th2: "cmod(z * poly (pCons c cs) z) - cmod a \<le> cmod (poly (pCons a (pCons c cs)) z)" 
+	by (simp add: diff_le_eq algebra_simps) 
+      from th1 th2 have "d \<le> cmod (poly (pCons a (pCons c cs)) z)"  by arith}
+    hence ?case by blast}
+  moreover
+  {assume cs0: "\<not> (cs \<noteq> 0)"
+    with pCons.prems have c0: "c \<noteq> 0" by simp
+    from cs0 have cs0': "cs = 0" by simp
+    {fix z
+      assume h: "(\<bar>d\<bar> + cmod a) / cmod c \<le> cmod z"
+      from c0 have "cmod c > 0" by simp
+      from h c0 have th0: "\<bar>d\<bar> + cmod a \<le> cmod (z*c)" 
+	by (simp add: field_simps norm_mult)
+      have ath: "\<And>mzh mazh ma. mzh <= mazh + ma ==> abs(d) + ma <= mzh ==> d <= mazh" by arith
+      from complex_mod_triangle_sub[of "z*c" a ]
+      have th1: "cmod (z * c) \<le> cmod (a + z * c) + cmod a"
+	by (simp add: algebra_simps)
+      from ath[OF th1 th0] have "d \<le> cmod (poly (pCons a (pCons c cs)) z)" 
+        using cs0' by simp}
+    then have ?case  by blast}
+  ultimately show ?case by blast
+qed simp
+
+text {* Hence polynomial's modulus attains its minimum somewhere. *}
+lemma poly_minimum_modulus:
+  "\<exists>z.\<forall>w. cmod (poly p z) \<le> cmod (poly p w)"
+proof(induct p)
+  case (pCons c cs) 
+  {assume cs0: "cs \<noteq> 0"
+    from poly_infinity[OF cs0, of "cmod (poly (pCons c cs) 0)" c]
+    obtain r where r: "\<And>z. r \<le> cmod z \<Longrightarrow> cmod (poly (pCons c cs) 0) \<le> cmod (poly (pCons c cs) z)" by blast
+    have ath: "\<And>z r. r \<le> cmod z \<or> cmod z \<le> \<bar>r\<bar>" by arith
+    from poly_minimum_modulus_disc[of "\<bar>r\<bar>" "pCons c cs"] 
+    obtain v where v: "\<And>w. cmod w \<le> \<bar>r\<bar> \<Longrightarrow> cmod (poly (pCons c cs) v) \<le> cmod (poly (pCons c cs) w)" by blast
+    {fix z assume z: "r \<le> cmod z"
+      from v[of 0] r[OF z] 
+      have "cmod (poly (pCons c cs) v) \<le> cmod (poly (pCons c cs) z)"
+	by simp }
+    note v0 = this
+    from v0 v ath[of r] have ?case by blast}
+  moreover
+  {assume cs0: "\<not> (cs \<noteq> 0)"
+    hence th:"cs = 0" by simp
+    from th pCons.hyps have ?case by simp}
+  ultimately show ?case by blast
+qed simp
+
+text{* Constant function (non-syntactic characterization). *}
+definition "constant f = (\<forall>x y. f x = f y)"
+
+lemma nonconstant_length: "\<not> (constant (poly p)) \<Longrightarrow> psize p \<ge> 2"
+  unfolding constant_def psize_def
+  apply (induct p, auto)
+  done
+ 
+lemma poly_replicate_append:
+  "poly (monom 1 n * p) (x::'a::{recpower, comm_ring_1}) = x^n * poly p x"
+  by (simp add: poly_monom)
+
+text {* Decomposition of polynomial, skipping zero coefficients 
+  after the first.  *}
+
+lemma poly_decompose_lemma:
+ assumes nz: "\<not>(\<forall>z. z\<noteq>0 \<longrightarrow> poly p z = (0::'a::{recpower,idom}))"
+  shows "\<exists>k a q. a\<noteq>0 \<and> Suc (psize q + k) = psize p \<and> 
+                 (\<forall>z. poly p z = z^k * poly (pCons a q) z)"
+unfolding psize_def
+using nz
+proof(induct p)
+  case 0 thus ?case by simp
+next
+  case (pCons c cs)
+  {assume c0: "c = 0"
+    from pCons.hyps pCons.prems c0 have ?case apply auto
+      apply (rule_tac x="k+1" in exI)
+      apply (rule_tac x="a" in exI, clarsimp)
+      apply (rule_tac x="q" in exI)
+      by (auto simp add: power_Suc)}
+  moreover
+  {assume c0: "c\<noteq>0"
+    hence ?case apply-
+      apply (rule exI[where x=0])
+      apply (rule exI[where x=c], clarsimp)
+      apply (rule exI[where x=cs])
+      apply auto
+      done}
+  ultimately show ?case by blast
+qed
+
+lemma poly_decompose:
+  assumes nc: "~constant(poly p)"
+  shows "\<exists>k a q. a\<noteq>(0::'a::{recpower,idom}) \<and> k\<noteq>0 \<and>
+               psize q + k + 1 = psize p \<and> 
+              (\<forall>z. poly p z = poly p 0 + z^k * poly (pCons a q) z)"
+using nc 
+proof(induct p)
+  case 0 thus ?case by (simp add: constant_def)
+next
+  case (pCons c cs)
+  {assume C:"\<forall>z. z \<noteq> 0 \<longrightarrow> poly cs z = 0"
+    {fix x y
+      from C have "poly (pCons c cs) x = poly (pCons c cs) y" by (cases "x=0", auto)}
+    with pCons.prems have False by (auto simp add: constant_def)}
+  hence th: "\<not> (\<forall>z. z \<noteq> 0 \<longrightarrow> poly cs z = 0)" ..
+  from poly_decompose_lemma[OF th] 
+  show ?case 
+    apply clarsimp
+    apply (rule_tac x="k+1" in exI)
+    apply (rule_tac x="a" in exI)
+    apply simp
+    apply (rule_tac x="q" in exI)
+    apply (auto simp add: power_Suc)
+    apply (auto simp add: psize_def split: if_splits)
+    done
+qed
+
+text{* Fundamental theorem of algebral *}
+
+lemma fundamental_theorem_of_algebra:
+  assumes nc: "~constant(poly p)"
+  shows "\<exists>z::complex. poly p z = 0"
+using nc
+proof(induct n\<equiv> "psize p" arbitrary: p rule: nat_less_induct)
+  fix n fix p :: "complex poly"
+  let ?p = "poly p"
+  assume H: "\<forall>m<n. \<forall>p. \<not> constant (poly p) \<longrightarrow> m = psize p \<longrightarrow> (\<exists>(z::complex). poly p z = 0)" and nc: "\<not> constant ?p" and n: "n = psize p"
+  let ?ths = "\<exists>z. ?p z = 0"
+
+  from nonconstant_length[OF nc] have n2: "n\<ge> 2" by (simp add: n)
+  from poly_minimum_modulus obtain c where 
+    c: "\<forall>w. cmod (?p c) \<le> cmod (?p w)" by blast
+  {assume pc: "?p c = 0" hence ?ths by blast}
+  moreover
+  {assume pc0: "?p c \<noteq> 0"
+    from poly_offset[of p c] obtain q where
+      q: "psize q = psize p" "\<forall>x. poly q x = ?p (c+x)" by blast
+    {assume h: "constant (poly q)"
+      from q(2) have th: "\<forall>x. poly q (x - c) = ?p x" by auto
+      {fix x y
+	from th have "?p x = poly q (x - c)" by auto 
+	also have "\<dots> = poly q (y - c)" 
+	  using h unfolding constant_def by blast
+	also have "\<dots> = ?p y" using th by auto
+	finally have "?p x = ?p y" .}
+      with nc have False unfolding constant_def by blast }
+    hence qnc: "\<not> constant (poly q)" by blast
+    from q(2) have pqc0: "?p c = poly q 0" by simp
+    from c pqc0 have cq0: "\<forall>w. cmod (poly q 0) \<le> cmod (?p w)" by simp 
+    let ?a0 = "poly q 0"
+    from pc0 pqc0 have a00: "?a0 \<noteq> 0" by simp 
+    from a00 
+    have qr: "\<forall>z. poly q z = poly (smult (inverse ?a0) q) z * ?a0"
+      by simp
+    let ?r = "smult (inverse ?a0) q"
+    have lgqr: "psize q = psize ?r"
+      using a00 unfolding psize_def degree_def
+      by (simp add: expand_poly_eq)
+    {assume h: "\<And>x y. poly ?r x = poly ?r y"
+      {fix x y
+	from qr[rule_format, of x] 
+	have "poly q x = poly ?r x * ?a0" by auto
+	also have "\<dots> = poly ?r y * ?a0" using h by simp
+	also have "\<dots> = poly q y" using qr[rule_format, of y] by simp
+	finally have "poly q x = poly q y" .} 
+      with qnc have False unfolding constant_def by blast}
+    hence rnc: "\<not> constant (poly ?r)" unfolding constant_def by blast
+    from qr[rule_format, of 0] a00  have r01: "poly ?r 0 = 1" by auto
+    {fix w 
+      have "cmod (poly ?r w) < 1 \<longleftrightarrow> cmod (poly q w / ?a0) < 1"
+	using qr[rule_format, of w] a00 by (simp add: divide_inverse mult_ac)
+      also have "\<dots> \<longleftrightarrow> cmod (poly q w) < cmod ?a0"
+	using a00 unfolding norm_divide by (simp add: field_simps)
+      finally have "cmod (poly ?r w) < 1 \<longleftrightarrow> cmod (poly q w) < cmod ?a0" .}
+    note mrmq_eq = this
+    from poly_decompose[OF rnc] obtain k a s where 
+      kas: "a\<noteq>0" "k\<noteq>0" "psize s + k + 1 = psize ?r" 
+      "\<forall>z. poly ?r z = poly ?r 0 + z^k* poly (pCons a s) z" by blast
+    {assume "k + 1 = n"
+      with kas(3) lgqr[symmetric] q(1) n[symmetric] have s0:"s=0" by auto
+      {fix w
+	have "cmod (poly ?r w) = cmod (1 + a * w ^ k)" 
+	  using kas(4)[rule_format, of w] s0 r01 by (simp add: algebra_simps)}
+      note hth = this [symmetric]
+	from reduce_poly_simple[OF kas(1,2)] 
+      have "\<exists>w. cmod (poly ?r w) < 1" unfolding hth by blast}
+    moreover
+    {assume kn: "k+1 \<noteq> n"
+      from kn kas(3) q(1) n[symmetric] lgqr have k1n: "k + 1 < n" by simp
+      have th01: "\<not> constant (poly (pCons 1 (monom a (k - 1))))" 
+	unfolding constant_def poly_pCons poly_monom
+	using kas(1) apply simp 
+	by (rule exI[where x=0], rule exI[where x=1], simp)
+      from kas(1) kas(2) have th02: "k+1 = psize (pCons 1 (monom a (k - 1)))"
+	by (simp add: psize_def degree_monom_eq)
+      from H[rule_format, OF k1n th01 th02]
+      obtain w where w: "1 + w^k * a = 0"
+	unfolding poly_pCons poly_monom
+	using kas(2) by (cases k, auto simp add: algebra_simps)
+      from poly_bound_exists[of "cmod w" s] obtain m where 
+	m: "m > 0" "\<forall>z. cmod z \<le> cmod w \<longrightarrow> cmod (poly s z) \<le> m" by blast
+      have w0: "w\<noteq>0" using kas(2) w by (auto simp add: power_0_left)
+      from w have "(1 + w ^ k * a) - 1 = 0 - 1" by simp
+      then have wm1: "w^k * a = - 1" by simp
+      have inv0: "0 < inverse (cmod w ^ (k + 1) * m)" 
+	using norm_ge_zero[of w] w0 m(1)
+	  by (simp add: inverse_eq_divide zero_less_mult_iff)
+      with real_down2[OF zero_less_one] obtain t where
+	t: "t > 0" "t < 1" "t < inverse (cmod w ^ (k + 1) * m)" by blast
+      let ?ct = "complex_of_real t"
+      let ?w = "?ct * w"
+      have "1 + ?w^k * (a + ?w * poly s ?w) = 1 + ?ct^k * (w^k * a) + ?w^k * ?w * poly s ?w" using kas(1) by (simp add: algebra_simps power_mult_distrib)
+      also have "\<dots> = complex_of_real (1 - t^k) + ?w^k * ?w * poly s ?w"
+	unfolding wm1 by (simp)
+      finally have "cmod (1 + ?w^k * (a + ?w * poly s ?w)) = cmod (complex_of_real (1 - t^k) + ?w^k * ?w * poly s ?w)" 
+	apply -
+	apply (rule cong[OF refl[of cmod]])
+	apply assumption
+	done
+      with norm_triangle_ineq[of "complex_of_real (1 - t^k)" "?w^k * ?w * poly s ?w"] 
+      have th11: "cmod (1 + ?w^k * (a + ?w * poly s ?w)) \<le> \<bar>1 - t^k\<bar> + cmod (?w^k * ?w * poly s ?w)" unfolding norm_of_real by simp 
+      have ath: "\<And>x (t::real). 0\<le> x \<Longrightarrow> x < t \<Longrightarrow> t\<le>1 \<Longrightarrow> \<bar>1 - t\<bar> + x < 1" by arith
+      have "t *cmod w \<le> 1 * cmod w" apply (rule mult_mono) using t(1,2) by auto
+      then have tw: "cmod ?w \<le> cmod w" using t(1) by (simp add: norm_mult) 
+      from t inv0 have "t* (cmod w ^ (k + 1) * m) < 1"
+	by (simp add: inverse_eq_divide field_simps)
+      with zero_less_power[OF t(1), of k] 
+      have th30: "t^k * (t* (cmod w ^ (k + 1) * m)) < t^k * 1" 
+	apply - apply (rule mult_strict_left_mono) by simp_all
+      have "cmod (?w^k * ?w * poly s ?w) = t^k * (t* (cmod w ^ (k+1) * cmod (poly s ?w)))"  using w0 t(1)
+	by (simp add: algebra_simps power_mult_distrib norm_of_real norm_power norm_mult)
+      then have "cmod (?w^k * ?w * poly s ?w) \<le> t^k * (t* (cmod w ^ (k + 1) * m))"
+	using t(1,2) m(2)[rule_format, OF tw] w0
+	apply (simp only: )
+	apply auto
+	apply (rule mult_mono, simp_all add: norm_ge_zero)+
+	apply (simp add: zero_le_mult_iff zero_le_power)
+	done
+      with th30 have th120: "cmod (?w^k * ?w * poly s ?w) < t^k" by simp 
+      from power_strict_mono[OF t(2), of k] t(1) kas(2) have th121: "t^k \<le> 1" 
+	by auto
+      from ath[OF norm_ge_zero[of "?w^k * ?w * poly s ?w"] th120 th121]
+      have th12: "\<bar>1 - t^k\<bar> + cmod (?w^k * ?w * poly s ?w) < 1" . 
+      from th11 th12
+      have "cmod (1 + ?w^k * (a + ?w * poly s ?w)) < 1"  by arith 
+      then have "cmod (poly ?r ?w) < 1" 
+	unfolding kas(4)[rule_format, of ?w] r01 by simp 
+      then have "\<exists>w. cmod (poly ?r w) < 1" by blast}
+    ultimately have cr0_contr: "\<exists>w. cmod (poly ?r w) < 1" by blast
+    from cr0_contr cq0 q(2)
+    have ?ths unfolding mrmq_eq not_less[symmetric] by auto}
+  ultimately show ?ths by blast
+qed
+
+text {* Alternative version with a syntactic notion of constant polynomial. *}
+
+lemma fundamental_theorem_of_algebra_alt:
+  assumes nc: "~(\<exists>a l. a\<noteq> 0 \<and> l = 0 \<and> p = pCons a l)"
+  shows "\<exists>z. poly p z = (0::complex)"
+using nc
+proof(induct p)
+  case (pCons c cs)
+  {assume "c=0" hence ?case by auto}
+  moreover
+  {assume c0: "c\<noteq>0"
+    {assume nc: "constant (poly (pCons c cs))"
+      from nc[unfolded constant_def, rule_format, of 0] 
+      have "\<forall>w. w \<noteq> 0 \<longrightarrow> poly cs w = 0" by auto 
+      hence "cs = 0"
+	proof(induct cs)
+	  case (pCons d ds)
+	  {assume "d=0" hence ?case using pCons.prems pCons.hyps by simp}
+	  moreover
+	  {assume d0: "d\<noteq>0"
+	    from poly_bound_exists[of 1 ds] obtain m where 
+	      m: "m > 0" "\<forall>z. \<forall>z. cmod z \<le> 1 \<longrightarrow> cmod (poly ds z) \<le> m" by blast
+	    have dm: "cmod d / m > 0" using d0 m(1) by (simp add: field_simps)
+	    from real_down2[OF dm zero_less_one] obtain x where 
+	      x: "x > 0" "x < cmod d / m" "x < 1" by blast
+	    let ?x = "complex_of_real x"
+	    from x have cx: "?x \<noteq> 0"  "cmod ?x \<le> 1" by simp_all
+	    from pCons.prems[rule_format, OF cx(1)]
+	    have cth: "cmod (?x*poly ds ?x) = cmod d" by (simp add: eq_diff_eq[symmetric])
+	    from m(2)[rule_format, OF cx(2)] x(1)
+	    have th0: "cmod (?x*poly ds ?x) \<le> x*m"
+	      by (simp add: norm_mult)
+	    from x(2) m(1) have "x*m < cmod d" by (simp add: field_simps)
+	    with th0 have "cmod (?x*poly ds ?x) \<noteq> cmod d" by auto
+	    with cth  have ?case by blast}
+	  ultimately show ?case by blast 
+	qed simp}
+      then have nc: "\<not> constant (poly (pCons c cs))" using pCons.prems c0 
+	by blast
+      from fundamental_theorem_of_algebra[OF nc] have ?case .}
+  ultimately show ?case by blast  
+qed simp
+
+subsection {* Order of polynomial roots *}
+
+definition
+  order :: "'a::{idom,recpower} \<Rightarrow> 'a poly \<Rightarrow> nat"
+where
+  [code del]:
+  "order a p = (LEAST n. \<not> [:-a, 1:] ^ Suc n dvd p)"
+
+lemma degree_power_le: "degree (p ^ n) \<le> degree p * n"
+by (induct n, simp, auto intro: order_trans degree_mult_le)
+
+lemma coeff_linear_power:
+  fixes a :: "'a::{comm_semiring_1,recpower}"
+  shows "coeff ([:a, 1:] ^ n) n = 1"
+apply (induct n, simp_all)
+apply (subst coeff_eq_0)
+apply (auto intro: le_less_trans degree_power_le)
+done
+
+lemma degree_linear_power:
+  fixes a :: "'a::{comm_semiring_1,recpower}"
+  shows "degree ([:a, 1:] ^ n) = n"
+apply (rule order_antisym)
+apply (rule ord_le_eq_trans [OF degree_power_le], simp)
+apply (rule le_degree, simp add: coeff_linear_power)
+done
+
+lemma order_1: "[:-a, 1:] ^ order a p dvd p"
+apply (cases "p = 0", simp)
+apply (cases "order a p", simp)
+apply (subgoal_tac "nat < (LEAST n. \<not> [:-a, 1:] ^ Suc n dvd p)")
+apply (drule not_less_Least, simp)
+apply (fold order_def, simp)
+done
+
+lemma order_2: "p \<noteq> 0 \<Longrightarrow> \<not> [:-a, 1:] ^ Suc (order a p) dvd p"
+unfolding order_def
+apply (rule LeastI_ex)
+apply (rule_tac x="degree p" in exI)
+apply (rule notI)
+apply (drule (1) dvd_imp_degree_le)
+apply (simp only: degree_linear_power)
+done
+
+lemma order:
+  "p \<noteq> 0 \<Longrightarrow> [:-a, 1:] ^ order a p dvd p \<and> \<not> [:-a, 1:] ^ Suc (order a p) dvd p"
+by (rule conjI [OF order_1 order_2])
+
+lemma order_degree:
+  assumes p: "p \<noteq> 0"
+  shows "order a p \<le> degree p"
+proof -
+  have "order a p = degree ([:-a, 1:] ^ order a p)"
+    by (simp only: degree_linear_power)
+  also have "\<dots> \<le> degree p"
+    using order_1 p by (rule dvd_imp_degree_le)
+  finally show ?thesis .
+qed
+
+lemma order_root: "poly p a = 0 \<longleftrightarrow> p = 0 \<or> order a p \<noteq> 0"
+apply (cases "p = 0", simp_all)
+apply (rule iffI)
+apply (rule ccontr, simp)
+apply (frule order_2 [where a=a], simp)
+apply (simp add: poly_eq_0_iff_dvd)
+apply (simp add: poly_eq_0_iff_dvd)
+apply (simp only: order_def)
+apply (drule not_less_Least, simp)
+done
+
+lemma poly_zero:
+  fixes p :: "'a::{idom,ring_char_0} poly"
+  shows "poly p = poly 0 \<longleftrightarrow> p = 0"
+apply (cases "p = 0", simp_all)
+apply (drule poly_roots_finite)
+apply (auto simp add: infinite_UNIV_char_0)
+done
+
+lemma poly_eq_iff:
+  fixes p q :: "'a::{idom,ring_char_0} poly"
+  shows "poly p = poly q \<longleftrightarrow> p = q"
+  using poly_zero [of "p - q"]
+  by (simp add: expand_fun_eq)
+
+
+subsection{* Nullstellenstatz, degrees and divisibility of polynomials *}
+
+lemma nullstellensatz_lemma:
+  fixes p :: "complex poly"
+  assumes "\<forall>x. poly p x = 0 \<longrightarrow> poly q x = 0"
+  and "degree p = n" and "n \<noteq> 0"
+  shows "p dvd (q ^ n)"
+using prems
+proof(induct n arbitrary: p q rule: nat_less_induct)
+  fix n::nat fix p q :: "complex poly"
+  assume IH: "\<forall>m<n. \<forall>p q.
+                 (\<forall>x. poly p x = (0::complex) \<longrightarrow> poly q x = 0) \<longrightarrow>
+                 degree p = m \<longrightarrow> m \<noteq> 0 \<longrightarrow> p dvd (q ^ m)"
+    and pq0: "\<forall>x. poly p x = 0 \<longrightarrow> poly q x = 0" 
+    and dpn: "degree p = n" and n0: "n \<noteq> 0"
+  from dpn n0 have pne: "p \<noteq> 0" by auto
+  let ?ths = "p dvd (q ^ n)"
+  {fix a assume a: "poly p a = 0"
+    {assume oa: "order a p \<noteq> 0"
+      let ?op = "order a p"
+      from pne have ap: "([:- a, 1:] ^ ?op) dvd p" 
+	"\<not> [:- a, 1:] ^ (Suc ?op) dvd p" using order by blast+ 
+      note oop = order_degree[OF pne, unfolded dpn]
+      {assume q0: "q = 0"
+	hence ?ths using n0
+          by (simp add: power_0_left)}
+      moreover
+      {assume q0: "q \<noteq> 0"
+	from pq0[rule_format, OF a, unfolded poly_eq_0_iff_dvd]
+	obtain r where r: "q = [:- a, 1:] * r" by (rule dvdE)
+	from ap(1) obtain s where
+	  s: "p = [:- a, 1:] ^ ?op * s" by (rule dvdE)
+	have sne: "s \<noteq> 0"
+	  using s pne by auto
+	{assume ds0: "degree s = 0"
+	  from ds0 have "\<exists>k. s = [:k:]"
+            by (cases s, simp split: if_splits)
+	  then obtain k where kpn: "s = [:k:]" by blast
+          from sne kpn have k: "k \<noteq> 0" by simp
+	  let ?w = "([:1/k:] * ([:-a,1:] ^ (n - ?op))) * (r ^ n)"
+          from k oop [of a] have "q ^ n = p * ?w"
+            apply -
+            apply (subst r, subst s, subst kpn)
+            apply (subst power_mult_distrib, simp)
+            apply (subst power_add [symmetric], simp)
+            done
+	  hence ?ths unfolding dvd_def by blast}
+	moreover
+	{assume ds0: "degree s \<noteq> 0"
+	  from ds0 sne dpn s oa
+	    have dsn: "degree s < n" apply auto
+              apply (erule ssubst)
+              apply (simp add: degree_mult_eq degree_linear_power)
+              done
+	    {fix x assume h: "poly s x = 0"
+	      {assume xa: "x = a"
+		from h[unfolded xa poly_eq_0_iff_dvd] obtain u where
+		  u: "s = [:- a, 1:] * u" by (rule dvdE)
+		have "p = [:- a, 1:] ^ (Suc ?op) * u"
+                  by (subst s, subst u, simp only: power_Suc mult_ac)
+		with ap(2)[unfolded dvd_def] have False by blast}
+	      note xa = this
+	      from h have "poly p x = 0" by (subst s, simp)
+	      with pq0 have "poly q x = 0" by blast
+	      with r xa have "poly r x = 0"
+                by (auto simp add: uminus_add_conv_diff)}
+	    note impth = this
+	    from IH[rule_format, OF dsn, of s r] impth ds0
+	    have "s dvd (r ^ (degree s))" by blast
+	    then obtain u where u: "r ^ (degree s) = s * u" ..
+	    hence u': "\<And>x. poly s x * poly u x = poly r x ^ degree s"
+              by (simp only: poly_mult[symmetric] poly_power[symmetric])
+	    let ?w = "(u * ([:-a,1:] ^ (n - ?op))) * (r ^ (n - degree s))"
+	    from oop[of a] dsn have "q ^ n = p * ?w"
+              apply -
+              apply (subst s, subst r)
+              apply (simp only: power_mult_distrib)
+              apply (subst mult_assoc [where b=s])
+              apply (subst mult_assoc [where a=u])
+              apply (subst mult_assoc [where b=u, symmetric])
+              apply (subst u [symmetric])
+              apply (simp add: mult_ac power_add [symmetric])
+              done
+	    hence ?ths unfolding dvd_def by blast}
+      ultimately have ?ths by blast }
+      ultimately have ?ths by blast}
+    then have ?ths using a order_root pne by blast}
+  moreover
+  {assume exa: "\<not> (\<exists>a. poly p a = 0)"
+    from fundamental_theorem_of_algebra_alt[of p] exa obtain c where
+      ccs: "c\<noteq>0" "p = pCons c 0" by blast
+    
+    then have pp: "\<And>x. poly p x =  c" by simp
+    let ?w = "[:1/c:] * (q ^ n)"
+    from ccs
+    have "(q ^ n) = (p * ?w) "
+      by (simp add: smult_smult)
+    hence ?ths unfolding dvd_def by blast}
+  ultimately show ?ths by blast
+qed
+
+lemma nullstellensatz_univariate:
+  "(\<forall>x. poly p x = (0::complex) \<longrightarrow> poly q x = 0) \<longleftrightarrow> 
+    p dvd (q ^ (degree p)) \<or> (p = 0 \<and> q = 0)"
+proof-
+  {assume pe: "p = 0"
+    hence eq: "(\<forall>x. poly p x = (0::complex) \<longrightarrow> poly q x = 0) \<longleftrightarrow> q = 0"
+      apply auto
+      apply (rule poly_zero [THEN iffD1])
+      by (rule ext, simp)
+    {assume "p dvd (q ^ (degree p))"
+      then obtain r where r: "q ^ (degree p) = p * r" ..
+      from r pe have False by simp}
+    with eq pe have ?thesis by blast}
+  moreover
+  {assume pe: "p \<noteq> 0"
+    {assume dp: "degree p = 0"
+      then obtain k where k: "p = [:k:]" "k\<noteq>0" using pe
+        by (cases p, simp split: if_splits)
+      hence th1: "\<forall>x. poly p x \<noteq> 0" by simp
+      from k dp have "q ^ (degree p) = p * [:1/k:]"
+        by (simp add: one_poly_def)
+      hence th2: "p dvd (q ^ (degree p))" ..
+      from th1 th2 pe have ?thesis by blast}
+    moreover
+    {assume dp: "degree p \<noteq> 0"
+      then obtain n where n: "degree p = Suc n " by (cases "degree p", auto)
+      {assume "p dvd (q ^ (Suc n))"
+	then obtain u where u: "q ^ (Suc n) = p * u" ..
+	{fix x assume h: "poly p x = 0" "poly q x \<noteq> 0"
+	  hence "poly (q ^ (Suc n)) x \<noteq> 0" by simp
+	  hence False using u h(1) by (simp only: poly_mult) simp}}
+	with n nullstellensatz_lemma[of p q "degree p"] dp 
+	have ?thesis by auto}
+    ultimately have ?thesis by blast}
+  ultimately show ?thesis by blast
+qed
+
+text{* Useful lemma *}
+
+lemma constant_degree:
+  fixes p :: "'a::{idom,ring_char_0} poly"
+  shows "constant (poly p) \<longleftrightarrow> degree p = 0" (is "?lhs = ?rhs")
+proof
+  assume l: ?lhs
+  from l[unfolded constant_def, rule_format, of _ "0"]
+  have th: "poly p = poly [:poly p 0:]" apply - by (rule ext, simp)
+  then have "p = [:poly p 0:]" by (simp add: poly_eq_iff)
+  then have "degree p = degree [:poly p 0:]" by simp
+  then show ?rhs by simp
+next
+  assume r: ?rhs
+  then obtain k where "p = [:k:]"
+    by (cases p, simp split: if_splits)
+  then show ?lhs unfolding constant_def by auto
+qed
+
+lemma divides_degree: assumes pq: "p dvd (q:: complex poly)"
+  shows "degree p \<le> degree q \<or> q = 0"
+apply (cases "q = 0", simp_all)
+apply (erule dvd_imp_degree_le [OF pq])
+done
+
+(* Arithmetic operations on multivariate polynomials.                        *)
+
+lemma mpoly_base_conv: 
+  "(0::complex) \<equiv> poly 0 x" "c \<equiv> poly [:c:] x" "x \<equiv> poly [:0,1:] x" by simp_all
+
+lemma mpoly_norm_conv: 
+  "poly [:0:] (x::complex) \<equiv> poly 0 x" "poly [:poly 0 y:] x \<equiv> poly 0 x" by simp_all
+
+lemma mpoly_sub_conv: 
+  "poly p (x::complex) - poly q x \<equiv> poly p x + -1 * poly q x"
+  by (simp add: diff_def)
+
+lemma poly_pad_rule: "poly p x = 0 ==> poly (pCons 0 p) x = (0::complex)" by simp
+
+lemma poly_cancel_eq_conv: "p = (0::complex) \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> (q = 0) \<equiv> (a * q - b * p = 0)" apply (atomize (full)) by auto
+
+lemma resolve_eq_raw:  "poly 0 x \<equiv> 0" "poly [:c:] x \<equiv> (c::complex)" by auto
+lemma  resolve_eq_then: "(P \<Longrightarrow> (Q \<equiv> Q1)) \<Longrightarrow> (\<not>P \<Longrightarrow> (Q \<equiv> Q2))
+  \<Longrightarrow> Q \<equiv> P \<and> Q1 \<or> \<not>P\<and> Q2" apply (atomize (full)) by blast 
+
+lemma poly_divides_pad_rule: 
+  fixes p q :: "complex poly"
+  assumes pq: "p dvd q"
+  shows "p dvd (pCons (0::complex) q)"
+proof-
+  have "pCons 0 q = q * [:0,1:]" by simp
+  then have "q dvd (pCons 0 q)" ..
+  with pq show ?thesis by (rule dvd_trans)
+qed
+
+lemma poly_divides_pad_const_rule: 
+  fixes p q :: "complex poly"
+  assumes pq: "p dvd q"
+  shows "p dvd (smult a q)"
+proof-
+  have "smult a q = q * [:a:]" by simp
+  then have "q dvd smult a q" ..
+  with pq show ?thesis by (rule dvd_trans)
+qed
+
+
+lemma poly_divides_conv0:  
+  fixes p :: "complex poly"
+  assumes lgpq: "degree q < degree p" and lq:"p \<noteq> 0"
+  shows "p dvd q \<equiv> q = 0" (is "?lhs \<equiv> ?rhs")
+proof-
+  {assume r: ?rhs 
+    hence "q = p * 0" by simp
+    hence ?lhs ..}
+  moreover
+  {assume l: ?lhs
+    {assume q0: "q = 0"
+      hence ?rhs by simp}
+    moreover
+    {assume q0: "q \<noteq> 0"
+      from l q0 have "degree p \<le> degree q"
+        by (rule dvd_imp_degree_le)
+      with lgpq have ?rhs by simp }
+    ultimately have ?rhs by blast }
+  ultimately show "?lhs \<equiv> ?rhs" by - (atomize (full), blast) 
+qed
+
+lemma poly_divides_conv1: 
+  assumes a0: "a\<noteq> (0::complex)" and pp': "(p::complex poly) dvd p'"
+  and qrp': "smult a q - p' \<equiv> r"
+  shows "p dvd q \<equiv> p dvd (r::complex poly)" (is "?lhs \<equiv> ?rhs")
+proof-
+  {
+  from pp' obtain t where t: "p' = p * t" ..
+  {assume l: ?lhs
+    then obtain u where u: "q = p * u" ..
+     have "r = p * (smult a u - t)"
+       using u qrp' [symmetric] t by (simp add: algebra_simps mult_smult_right)
+     then have ?rhs ..}
+  moreover
+  {assume r: ?rhs
+    then obtain u where u: "r = p * u" ..
+    from u [symmetric] t qrp' [symmetric] a0
+    have "q = p * smult (1/a) (u + t)"
+      by (simp add: algebra_simps mult_smult_right smult_smult)
+    hence ?lhs ..}
+  ultimately have "?lhs = ?rhs" by blast }
+thus "?lhs \<equiv> ?rhs"  by - (atomize(full), blast) 
+qed
+
+lemma basic_cqe_conv1:
+  "(\<exists>x. poly p x = 0 \<and> poly 0 x \<noteq> 0) \<equiv> False"
+  "(\<exists>x. poly 0 x \<noteq> 0) \<equiv> False"
+  "(\<exists>x. poly [:c:] x \<noteq> 0) \<equiv> c\<noteq>0"
+  "(\<exists>x. poly 0 x = 0) \<equiv> True"
+  "(\<exists>x. poly [:c:] x = 0) \<equiv> c = 0" by simp_all
+
+lemma basic_cqe_conv2: 
+  assumes l:"p \<noteq> 0" 
+  shows "(\<exists>x. poly (pCons a (pCons b p)) x = (0::complex)) \<equiv> True"
+proof-
+  {fix h t
+    assume h: "h\<noteq>0" "t=0"  "pCons a (pCons b p) = pCons h t"
+    with l have False by simp}
+  hence th: "\<not> (\<exists> h t. h\<noteq>0 \<and> t=0 \<and> pCons a (pCons b p) = pCons h t)"
+    by blast
+  from fundamental_theorem_of_algebra_alt[OF th] 
+  show "(\<exists>x. poly (pCons a (pCons b p)) x = (0::complex)) \<equiv> True" by auto
+qed
+
+lemma  basic_cqe_conv_2b: "(\<exists>x. poly p x \<noteq> (0::complex)) \<equiv> (p \<noteq> 0)"
+proof-
+  have "p = 0 \<longleftrightarrow> poly p = poly 0"
+    by (simp add: poly_zero)
+  also have "\<dots> \<longleftrightarrow> (\<not> (\<exists>x. poly p x \<noteq> 0))" by (auto intro: ext)
+  finally show "(\<exists>x. poly p x \<noteq> (0::complex)) \<equiv> p \<noteq> 0"
+    by - (atomize (full), blast)
+qed
+
+lemma basic_cqe_conv3:
+  fixes p q :: "complex poly"
+  assumes l: "p \<noteq> 0" 
+  shows "(\<exists>x. poly (pCons a p) x = 0 \<and> poly q x \<noteq> 0) \<equiv> \<not> ((pCons a p) dvd (q ^ (psize p)))"
+proof-
+  from l have dp:"degree (pCons a p) = psize p" by (simp add: psize_def)
+  from nullstellensatz_univariate[of "pCons a p" q] l
+  show "(\<exists>x. poly (pCons a p) x = 0 \<and> poly q x \<noteq> 0) \<equiv> \<not> ((pCons a p) dvd (q ^ (psize p)))"
+    unfolding dp
+    by - (atomize (full), auto)
+qed
+
+lemma basic_cqe_conv4:
+  fixes p q :: "complex poly"
+  assumes h: "\<And>x. poly (q ^ n) x \<equiv> poly r x"
+  shows "p dvd (q ^ n) \<equiv> p dvd r"
+proof-
+  from h have "poly (q ^ n) = poly r" by (auto intro: ext)
+  then have "(q ^ n) = r" by (simp add: poly_eq_iff)
+  thus "p dvd (q ^ n) \<equiv> p dvd r" by simp
+qed
+
+lemma pmult_Cons_Cons: "(pCons (a::complex) (pCons b p) * q = (smult a q) + (pCons 0 (pCons b p * q)))"
+  by simp
+
+lemma elim_neg_conv: "- z \<equiv> (-1) * (z::complex)" by simp
+lemma eqT_intr: "PROP P \<Longrightarrow> (True \<Longrightarrow> PROP P )" "PROP P \<Longrightarrow> True" by blast+
+lemma negate_negate_rule: "Trueprop P \<equiv> \<not> P \<equiv> False" by (atomize (full), auto)
+
+lemma complex_entire: "(z::complex) \<noteq> 0 \<and> w \<noteq> 0 \<equiv> z*w \<noteq> 0" by simp
+lemma resolve_eq_ne: "(P \<equiv> True) \<equiv> (\<not>P \<equiv> False)" "(P \<equiv> False) \<equiv> (\<not>P \<equiv> True)" 
+  by (atomize (full)) simp_all
+lemma cqe_conv1: "poly 0 x = 0 \<longleftrightarrow> True"  by simp
+lemma cqe_conv2: "(p \<Longrightarrow> (q \<equiv> r)) \<equiv> ((p \<and> q) \<equiv> (p \<and> r))"  (is "?l \<equiv> ?r")
+proof
+  assume "p \<Longrightarrow> q \<equiv> r" thus "p \<and> q \<equiv> p \<and> r" apply - apply (atomize (full)) by blast
+next
+  assume "p \<and> q \<equiv> p \<and> r" "p"
+  thus "q \<equiv> r" apply - apply (atomize (full)) apply blast done
+qed
+lemma poly_const_conv: "poly [:c:] (x::complex) = y \<longleftrightarrow> c = y" by simp
+
+end
--- a/src/HOL/Library/Infinite_Set.thy	Mon Feb 16 10:13:30 2009 +0100
+++ b/src/HOL/Library/Infinite_Set.thy	Mon Feb 16 10:15:43 2009 +0100
@@ -461,10 +461,11 @@
   by simp
 
 lemma enumerate_in_set: "infinite S \<Longrightarrow> enumerate S n : S"
-  apply (induct n arbitrary: S)
-   apply (fastsimp intro: LeastI dest!: infinite_imp_nonempty)
-  apply (fastsimp iff: finite_Diff_singleton)
-  done
+apply (induct n arbitrary: S)
+ apply (fastsimp intro: LeastI dest!: infinite_imp_nonempty)
+apply simp
+apply (metis Collect_def Collect_mem_eq DiffE infinite_remove)
+done
 
 declare enumerate_0 [simp del] enumerate_Suc [simp del]
 
--- a/src/HOL/Library/Library.thy	Mon Feb 16 10:13:30 2009 +0100
+++ b/src/HOL/Library/Library.thy	Mon Feb 16 10:15:43 2009 +0100
@@ -23,6 +23,7 @@
   Float
   Formal_Power_Series
   FuncSet
+  Fundamental_Theorem_Algebra
   Infinite_Set
   ListVector
   Mapping
--- a/src/HOL/Library/Multiset.thy	Mon Feb 16 10:13:30 2009 +0100
+++ b/src/HOL/Library/Multiset.thy	Mon Feb 16 10:15:43 2009 +0100
@@ -88,10 +88,8 @@
 
 lemma union_preserves_multiset:
   "M \<in> multiset ==> N \<in> multiset ==> (\<lambda>a. M a + N a) \<in> multiset"
-apply (simp add: multiset_def)
-apply (drule (1) finite_UnI)
-apply (simp del: finite_Un add: Un_def)
-done
+by (simp add: multiset_def)
+
 
 lemma diff_preserves_multiset:
   "M \<in> multiset ==> (\<lambda>a. M a - N a) \<in> multiset"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Library/Nat_Int_Bij.thy	Mon Feb 16 10:15:43 2009 +0100
@@ -0,0 +1,170 @@
+(*  Title:      HOL/Nat_Int_Bij.thy
+    ID:         $Id$
+    Author:     Stefan Richter, Tobias Nipkow
+*)
+
+header{* Bijections $\mathbb{N}\to\mathbb{N}^2$ and $\mathbb{N}\to\mathbb{Z}$*}
+
+theory Nat_Int_Bij
+imports Hilbert_Choice Presburger
+begin
+
+subsection{*  A bijection between @{text "\<nat>"} and @{text "\<nat>\<twosuperior>"} *}
+
+text{* Definition and proofs are from \cite[page 85]{Oberschelp:1993}. *}
+
+definition nat2_to_nat:: "(nat * nat) \<Rightarrow> nat" where
+"nat2_to_nat pair = (let (n,m) = pair in (n+m) * Suc (n+m) div 2 + n)"
+definition nat_to_nat2::  "nat \<Rightarrow> (nat * nat)" where
+"nat_to_nat2 = inv nat2_to_nat"
+
+lemma dvd2_a_x_suc_a: "2 dvd a * (Suc a)"
+proof (cases "2 dvd a")
+  case True
+  then show ?thesis by (rule dvd_mult2)
+next
+  case False
+  then have "Suc (a mod 2) = 2" by (simp add: dvd_eq_mod_eq_0)
+  then have "Suc a mod 2 = 0" by (simp add: mod_Suc)
+  then have "2 dvd Suc a" by (simp only:dvd_eq_mod_eq_0)
+  then show ?thesis by (rule dvd_mult)
+qed
+
+lemma
+  assumes eq: "nat2_to_nat (u,v) = nat2_to_nat (x,y)"
+  shows nat2_to_nat_help: "u+v \<le> x+y"
+proof (rule classical)
+  assume "\<not> ?thesis"
+  then have contrapos: "x+y < u+v"
+    by simp
+  have "nat2_to_nat (x,y) < (x+y) * Suc (x+y) div 2 + Suc (x + y)"
+    by (unfold nat2_to_nat_def) (simp add: Let_def)
+  also have "\<dots> = (x+y)*Suc(x+y) div 2 + 2 * Suc(x+y) div 2"
+    by (simp only: div_mult_self1_is_m)
+  also have "\<dots> = (x+y)*Suc(x+y) div 2 + 2 * Suc(x+y) div 2
+    + ((x+y)*Suc(x+y) mod 2 + 2 * Suc(x+y) mod 2) div 2"
+  proof -
+    have "2 dvd (x+y)*Suc(x+y)"
+      by (rule dvd2_a_x_suc_a)
+    then have "(x+y)*Suc(x+y) mod 2 = 0"
+      by (simp only: dvd_eq_mod_eq_0)
+    also
+    have "2 * Suc(x+y) mod 2 = 0"
+      by (rule mod_mult_self1_is_0)
+    ultimately have
+      "((x+y)*Suc(x+y) mod 2 + 2 * Suc(x+y) mod 2) div 2 = 0"
+      by simp
+    then show ?thesis
+      by simp
+  qed
+  also have "\<dots> = ((x+y)*Suc(x+y) + 2*Suc(x+y)) div 2"
+    by (rule div_add1_eq [symmetric])
+  also have "\<dots> = ((x+y+2)*Suc(x+y)) div 2"
+    by (simp only: add_mult_distrib [symmetric])
+  also from contrapos have "\<dots> \<le> ((Suc(u+v))*(u+v)) div 2"
+    by (simp only: mult_le_mono div_le_mono)
+  also have "\<dots> \<le> nat2_to_nat (u,v)"
+    by (unfold nat2_to_nat_def) (simp add: Let_def)
+  finally show ?thesis
+    by (simp only: eq)
+qed
+
+theorem nat2_to_nat_inj: "inj nat2_to_nat"
+proof -
+  {
+    fix u v x y
+    assume eq1: "nat2_to_nat (u,v) = nat2_to_nat (x,y)"
+    then have "u+v \<le> x+y" by (rule nat2_to_nat_help)
+    also from eq1 [symmetric] have "x+y \<le> u+v"
+      by (rule nat2_to_nat_help)
+    finally have eq2: "u+v = x+y" .
+    with eq1 have ux: "u=x"
+      by (simp add: nat2_to_nat_def Let_def)
+    with eq2 have vy: "v=y" by simp
+    with ux have "(u,v) = (x,y)" by simp
+  }
+  then have "\<And>x y. nat2_to_nat x = nat2_to_nat y \<Longrightarrow> x=y" by fast
+  then show ?thesis unfolding inj_on_def by simp
+qed
+
+lemma nat_to_nat2_surj: "surj nat_to_nat2"
+by (simp only: nat_to_nat2_def nat2_to_nat_inj inj_imp_surj_inv)
+
+
+lemma gauss_sum_nat_upto: "2 * (\<Sum>i\<le>n::nat. i) = n * (n + 1)"
+using gauss_sum[where 'a = nat]
+by (simp add:atLeast0AtMost setsum_shift_lb_Suc0_0 numeral_2_eq_2)
+
+lemma nat2_to_nat_surj: "surj nat2_to_nat"
+proof (unfold surj_def)
+  {
+    fix z::nat 
+    def r \<equiv> "Max {r. (\<Sum>i\<le>r. i) \<le> z}" 
+    def x \<equiv> "z - (\<Sum>i\<le>r. i)"
+
+    hence "finite  {r. (\<Sum>i\<le>r. i) \<le> z}"
+      by (simp add: lessThan_Suc_atMost[symmetric] lessThan_Suc finite_less_ub)
+    also have "0 \<in> {r. (\<Sum>i\<le>r. i) \<le> z}"  by simp
+    hence "{r::nat. (\<Sum>i\<le>r. i) \<le> z} \<noteq> {}"  by fast
+    ultimately have a: "r \<in> {r. (\<Sum>i\<le>r. i) \<le> z} \<and> (\<forall>s \<in> {r. (\<Sum>i\<le>r. i) \<le> z}. s \<le> r)"
+      by (simp add: r_def del:mem_Collect_eq)
+    {
+      assume "r<x"
+      hence "r+1\<le>x"  by simp
+      hence "(\<Sum>i\<le>r. i)+(r+1)\<le>z"  using x_def by arith
+      hence "(r+1) \<in>  {r. (\<Sum>i\<le>r. i) \<le> z}"  by simp
+      with a have "(r+1)\<le>r"  by simp
+    }
+    hence b: "x\<le>r"  by force
+    
+    def y \<equiv> "r-x"
+    have "2*z=2*(\<Sum>i\<le>r. i)+2*x"  using x_def a by simp arith
+    also have "\<dots> = r * (r+1) + 2*x"   using gauss_sum_nat_upto by simp
+    also have "\<dots> = (x+y)*(x+y+1)+2*x" using y_def b by simp
+    also { have "2 dvd ((x+y)*(x+y+1))"	using dvd2_a_x_suc_a by simp }
+    hence "\<dots> = 2 * nat2_to_nat(x,y)"
+      using nat2_to_nat_def by (simp add: Let_def dvd_mult_div_cancel)
+    finally have "z=nat2_to_nat (x, y)"  by simp
+  }
+  thus "\<forall>y. \<exists>x. y = nat2_to_nat x"  by fast
+qed
+
+
+subsection{*  A bijection between @{text "\<nat>"} and @{text "\<int>"} *}
+
+definition nat_to_int_bij :: "nat \<Rightarrow> int" where
+"nat_to_int_bij n = (if 2 dvd n then int(n div 2) else -int(Suc n div 2))"
+
+definition int_to_nat_bij :: "int \<Rightarrow> nat" where
+"int_to_nat_bij i = (if 0<=i then 2*nat(i) else 2*nat(-i) - 1)"
+
+lemma  i2n_n2i_id: "int_to_nat_bij (nat_to_int_bij n) = n"
+by (simp add: int_to_nat_bij_def nat_to_int_bij_def) presburger
+
+lemma n2i_i2n_id: "nat_to_int_bij(int_to_nat_bij i) = i"
+proof -
+  have "ALL m n::nat. m>0 \<longrightarrow> 2 * m - Suc 0 \<noteq> 2 * n" by presburger
+  thus ?thesis
+    by(simp add: nat_to_int_bij_def int_to_nat_bij_def, simp add:dvd_def)
+qed
+
+lemma inv_nat_to_int_bij: "inv nat_to_int_bij = int_to_nat_bij"
+by (simp add: i2n_n2i_id inv_equality n2i_i2n_id)
+
+lemma inv_int_to_nat_bij: "inv int_to_nat_bij = nat_to_int_bij"
+by (simp add: i2n_n2i_id inv_equality n2i_i2n_id)
+
+lemma surj_nat_to_int_bij: "surj nat_to_int_bij"
+by (blast intro: n2i_i2n_id surjI)
+
+lemma surj_int_to_nat_bij: "surj int_to_nat_bij"
+by (blast intro: i2n_n2i_id surjI)
+
+lemma inj_nat_to_int_bij: "inj nat_to_int_bij"
+by(simp add:inv_int_to_nat_bij[symmetric] surj_int_to_nat_bij surj_imp_inj_inv)
+
+lemma inj_int_to_nat_bij: "inj int_to_nat_bij"
+by(simp add:inv_nat_to_int_bij[symmetric] surj_nat_to_int_bij surj_imp_inj_inv)
+
+
+end
--- a/src/HOL/Library/Univ_Poly.thy	Mon Feb 16 10:13:30 2009 +0100
+++ b/src/HOL/Library/Univ_Poly.thy	Mon Feb 16 10:15:43 2009 +0100
@@ -344,26 +344,6 @@
 apply (erule_tac x="x" in allE, clarsimp)
 by (case_tac "n=length p", auto simp add: order_le_less)
 
-lemma UNIV_nat_infinite: "\<not> finite (UNIV :: nat set)"
-  unfolding finite_conv_nat_seg_image
-proof(auto simp add: expand_set_eq image_iff)
-  fix n::nat and f:: "nat \<Rightarrow> nat"
-  let ?N = "{i. i < n}"
-  let ?fN = "f ` ?N"
-  let ?y = "Max ?fN + 1"
-  from nat_seg_image_imp_finite[of "?fN" "f" n] 
-  have thfN: "finite ?fN" by simp
-  {assume "n =0" hence "\<exists>x. \<forall>xa<n. x \<noteq> f xa" by auto}
-  moreover
-  {assume nz: "n \<noteq> 0"
-    hence thne: "?fN \<noteq> {}" by (auto simp add: neq0_conv)
-    have "\<forall>x\<in> ?fN. Max ?fN \<ge> x" using nz Max_ge_iff[OF thfN thne] by auto
-    hence "\<forall>x\<in> ?fN. ?y > x" by auto
-    hence "?y \<notin> ?fN" by auto
-    hence "\<exists>x. \<forall>xa<n. x \<noteq> f xa" by auto }
-  ultimately show "\<exists>x. \<forall>xa<n. x \<noteq> f xa" by blast
-qed
-
 lemma (in ring_char_0) UNIV_ring_char_0_infinte: 
   "\<not> (finite (UNIV:: 'a set))" 
 proof
@@ -374,7 +354,7 @@
     then show "finite (of_nat ` UNIV :: 'a set)" using F by (rule finite_subset)
     show "inj (of_nat :: nat \<Rightarrow> 'a)" by (simp add: inj_on_def)
   qed
-  with UNIV_nat_infinite show False ..
+  with infinite_UNIV_nat show False ..
 qed
 
 lemma (in idom_char_0) poly_roots_finite: "(poly p \<noteq> poly []) = 
--- a/src/HOL/Lim.thy	Mon Feb 16 10:13:30 2009 +0100
+++ b/src/HOL/Lim.thy	Mon Feb 16 10:15:43 2009 +0100
@@ -452,6 +452,11 @@
   shows "\<lbrakk>f -- a --> L; L \<noteq> 0\<rbrakk> \<Longrightarrow> (\<lambda>x. inverse (f x)) -- a --> inverse L"
 by (rule LIM_inverse_fun [THEN LIM_compose])
 
+lemma LIM_sgn:
+  "\<lbrakk>f -- a --> l; l \<noteq> 0\<rbrakk> \<Longrightarrow> (\<lambda>x. sgn (f x)) -- a --> sgn l"
+unfolding sgn_div_norm
+by (simp add: LIM_scaleR LIM_inverse LIM_norm)
+
 
 subsection {* Continuity *}
 
@@ -529,6 +534,10 @@
   shows "isCont f a \<Longrightarrow> isCont (\<lambda>x. f x ^ n) a"
   unfolding isCont_def by (rule LIM_power)
 
+lemma isCont_sgn:
+  "\<lbrakk>isCont f a; f a \<noteq> 0\<rbrakk> \<Longrightarrow> isCont (\<lambda>x. sgn (f x)) a"
+  unfolding isCont_def by (rule LIM_sgn)
+
 lemma isCont_abs [simp]: "isCont abs (a::real)"
 by (rule isCont_rabs [OF isCont_ident])
 
--- a/src/HOL/Main.thy	Mon Feb 16 10:13:30 2009 +0100
+++ b/src/HOL/Main.thy	Mon Feb 16 10:15:43 2009 +0100
@@ -1,7 +1,7 @@
 header {* Main HOL *}
 
 theory Main
-imports Plain Code_Eval Map Nat_Int_Bij Recdef SAT
+imports Plain Code_Eval Map Recdef SAT
 begin
 
 text {*
--- a/src/HOL/NSA/HyperNat.thy	Mon Feb 16 10:13:30 2009 +0100
+++ b/src/HOL/NSA/HyperNat.thy	Mon Feb 16 10:15:43 2009 +0100
@@ -286,11 +286,10 @@
 by (simp add: HNatInfinite_def)
 
 lemma lemma_unbounded_set [simp]: "{n::nat. m < n} \<in> FreeUltrafilterNat"
-apply (insert finite_atMost [of m]) 
-apply (simp add: atMost_def)
+apply (insert finite_atMost [of m])
 apply (drule FreeUltrafilterNat.finite)
 apply (drule FreeUltrafilterNat.not_memD)
-apply (simp add: Collect_neg_eq [symmetric] linorder_not_le)
+apply (simp add: Collect_neg_eq [symmetric] linorder_not_le atMost_def)
 done
 
 lemma Compl_Collect_le: "- {n::nat. N \<le> n} = {n. n < N}"
--- a/src/HOL/NSA/StarDef.thy	Mon Feb 16 10:13:30 2009 +0100
+++ b/src/HOL/NSA/StarDef.thy	Mon Feb 16 10:15:43 2009 +0100
@@ -844,6 +844,8 @@
 instance star :: (cancel_ab_semigroup_add) cancel_ab_semigroup_add
 by (intro_classes, transfer, rule add_imp_eq)
 
+instance star :: (cancel_comm_monoid_add) cancel_comm_monoid_add ..
+
 instance star :: (ab_group_add) ab_group_add
 apply (intro_classes)
 apply (transfer, rule left_minus)
--- a/src/HOL/Nat.thy	Mon Feb 16 10:13:30 2009 +0100
+++ b/src/HOL/Nat.thy	Mon Feb 16 10:15:43 2009 +0100
@@ -1367,6 +1367,9 @@
 
 end
 
+lemma mono_iff_le_Suc: "mono f = (\<forall>n. f n \<le> f (Suc n))"
+unfolding mono_def
+by (auto intro:lift_Suc_mono_le[of f])
 
 lemma mono_nat_linear_lb:
   "(!!m n::nat. m<n \<Longrightarrow> f m < f n) \<Longrightarrow> f(m)+k \<le> f(m+k)"
--- a/src/HOL/Nat_Int_Bij.thy	Mon Feb 16 10:13:30 2009 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,170 +0,0 @@
-(*  Title:      HOL/Nat_Int_Bij.thy
-    ID:         $Id$
-    Author:     Stefan Richter, Tobias Nipkow
-*)
-
-header{* Bijections $\mathbb{N}\to\mathbb{N}^2$ and $\mathbb{N}\to\mathbb{Z}$*}
-
-theory Nat_Int_Bij
-imports Hilbert_Choice Presburger
-begin
-
-subsection{*  A bijection between @{text "\<nat>"} and @{text "\<nat>\<twosuperior>"} *}
-
-text{* Definition and proofs are from \cite[page 85]{Oberschelp:1993}. *}
-
-definition nat2_to_nat:: "(nat * nat) \<Rightarrow> nat" where
-"nat2_to_nat pair = (let (n,m) = pair in (n+m) * Suc (n+m) div 2 + n)"
-definition nat_to_nat2::  "nat \<Rightarrow> (nat * nat)" where
-"nat_to_nat2 = inv nat2_to_nat"
-
-lemma dvd2_a_x_suc_a: "2 dvd a * (Suc a)"
-proof (cases "2 dvd a")
-  case True
-  then show ?thesis by (rule dvd_mult2)
-next
-  case False
-  then have "Suc (a mod 2) = 2" by (simp add: dvd_eq_mod_eq_0)
-  then have "Suc a mod 2 = 0" by (simp add: mod_Suc)
-  then have "2 dvd Suc a" by (simp only:dvd_eq_mod_eq_0)
-  then show ?thesis by (rule dvd_mult)
-qed
-
-lemma
-  assumes eq: "nat2_to_nat (u,v) = nat2_to_nat (x,y)"
-  shows nat2_to_nat_help: "u+v \<le> x+y"
-proof (rule classical)
-  assume "\<not> ?thesis"
-  then have contrapos: "x+y < u+v"
-    by simp
-  have "nat2_to_nat (x,y) < (x+y) * Suc (x+y) div 2 + Suc (x + y)"
-    by (unfold nat2_to_nat_def) (simp add: Let_def)
-  also have "\<dots> = (x+y)*Suc(x+y) div 2 + 2 * Suc(x+y) div 2"
-    by (simp only: div_mult_self1_is_m)
-  also have "\<dots> = (x+y)*Suc(x+y) div 2 + 2 * Suc(x+y) div 2
-    + ((x+y)*Suc(x+y) mod 2 + 2 * Suc(x+y) mod 2) div 2"
-  proof -
-    have "2 dvd (x+y)*Suc(x+y)"
-      by (rule dvd2_a_x_suc_a)
-    then have "(x+y)*Suc(x+y) mod 2 = 0"
-      by (simp only: dvd_eq_mod_eq_0)
-    also
-    have "2 * Suc(x+y) mod 2 = 0"
-      by (rule mod_mult_self1_is_0)
-    ultimately have
-      "((x+y)*Suc(x+y) mod 2 + 2 * Suc(x+y) mod 2) div 2 = 0"
-      by simp
-    then show ?thesis
-      by simp
-  qed
-  also have "\<dots> = ((x+y)*Suc(x+y) + 2*Suc(x+y)) div 2"
-    by (rule div_add1_eq [symmetric])
-  also have "\<dots> = ((x+y+2)*Suc(x+y)) div 2"
-    by (simp only: add_mult_distrib [symmetric])
-  also from contrapos have "\<dots> \<le> ((Suc(u+v))*(u+v)) div 2"
-    by (simp only: mult_le_mono div_le_mono)
-  also have "\<dots> \<le> nat2_to_nat (u,v)"
-    by (unfold nat2_to_nat_def) (simp add: Let_def)
-  finally show ?thesis
-    by (simp only: eq)
-qed
-
-theorem nat2_to_nat_inj: "inj nat2_to_nat"
-proof -
-  {
-    fix u v x y
-    assume eq1: "nat2_to_nat (u,v) = nat2_to_nat (x,y)"
-    then have "u+v \<le> x+y" by (rule nat2_to_nat_help)
-    also from eq1 [symmetric] have "x+y \<le> u+v"
-      by (rule nat2_to_nat_help)
-    finally have eq2: "u+v = x+y" .
-    with eq1 have ux: "u=x"
-      by (simp add: nat2_to_nat_def Let_def)
-    with eq2 have vy: "v=y" by simp
-    with ux have "(u,v) = (x,y)" by simp
-  }
-  then have "\<And>x y. nat2_to_nat x = nat2_to_nat y \<Longrightarrow> x=y" by fast
-  then show ?thesis unfolding inj_on_def by simp
-qed
-
-lemma nat_to_nat2_surj: "surj nat_to_nat2"
-by (simp only: nat_to_nat2_def nat2_to_nat_inj inj_imp_surj_inv)
-
-
-lemma gauss_sum_nat_upto: "2 * (\<Sum>i\<le>n::nat. i) = n * (n + 1)"
-using gauss_sum[where 'a = nat]
-by (simp add:atLeast0AtMost setsum_shift_lb_Suc0_0 numeral_2_eq_2)
-
-lemma nat2_to_nat_surj: "surj nat2_to_nat"
-proof (unfold surj_def)
-  {
-    fix z::nat 
-    def r \<equiv> "Max {r. (\<Sum>i\<le>r. i) \<le> z}" 
-    def x \<equiv> "z - (\<Sum>i\<le>r. i)"
-
-    hence "finite  {r. (\<Sum>i\<le>r. i) \<le> z}"
-      by (simp add: lessThan_Suc_atMost[symmetric] lessThan_Suc finite_less_ub)
-    also have "0 \<in> {r. (\<Sum>i\<le>r. i) \<le> z}"  by simp
-    hence "{r::nat. (\<Sum>i\<le>r. i) \<le> z} \<noteq> {}"  by fast
-    ultimately have a: "r \<in> {r. (\<Sum>i\<le>r. i) \<le> z} \<and> (\<forall>s \<in> {r. (\<Sum>i\<le>r. i) \<le> z}. s \<le> r)"
-      by (simp add: r_def del:mem_Collect_eq)
-    {
-      assume "r<x"
-      hence "r+1\<le>x"  by simp
-      hence "(\<Sum>i\<le>r. i)+(r+1)\<le>z"  using x_def by arith
-      hence "(r+1) \<in>  {r. (\<Sum>i\<le>r. i) \<le> z}"  by simp
-      with a have "(r+1)\<le>r"  by simp
-    }
-    hence b: "x\<le>r"  by force
-    
-    def y \<equiv> "r-x"
-    have "2*z=2*(\<Sum>i\<le>r. i)+2*x"  using x_def a by simp arith
-    also have "\<dots> = r * (r+1) + 2*x"   using gauss_sum_nat_upto by simp
-    also have "\<dots> = (x+y)*(x+y+1)+2*x" using y_def b by simp
-    also { have "2 dvd ((x+y)*(x+y+1))"	using dvd2_a_x_suc_a by simp }
-    hence "\<dots> = 2 * nat2_to_nat(x,y)"
-      using nat2_to_nat_def by (simp add: Let_def dvd_mult_div_cancel)
-    finally have "z=nat2_to_nat (x, y)"  by simp
-  }
-  thus "\<forall>y. \<exists>x. y = nat2_to_nat x"  by fast
-qed
-
-
-subsection{*  A bijection between @{text "\<nat>"} and @{text "\<int>"} *}
-
-definition nat_to_int_bij :: "nat \<Rightarrow> int" where
-"nat_to_int_bij n = (if 2 dvd n then int(n div 2) else -int(Suc n div 2))"
-
-definition int_to_nat_bij :: "int \<Rightarrow> nat" where
-"int_to_nat_bij i = (if 0<=i then 2*nat(i) else 2*nat(-i) - 1)"
-
-lemma  i2n_n2i_id: "int_to_nat_bij (nat_to_int_bij n) = n"
-by (simp add: int_to_nat_bij_def nat_to_int_bij_def) presburger
-
-lemma n2i_i2n_id: "nat_to_int_bij(int_to_nat_bij i) = i"
-proof -
-  have "ALL m n::nat. m>0 \<longrightarrow> 2 * m - Suc 0 \<noteq> 2 * n" by presburger
-  thus ?thesis
-    by(simp add: nat_to_int_bij_def int_to_nat_bij_def, simp add:dvd_def)
-qed
-
-lemma inv_nat_to_int_bij: "inv nat_to_int_bij = int_to_nat_bij"
-by (simp add: i2n_n2i_id inv_equality n2i_i2n_id)
-
-lemma inv_int_to_nat_bij: "inv int_to_nat_bij = nat_to_int_bij"
-by (simp add: i2n_n2i_id inv_equality n2i_i2n_id)
-
-lemma surj_nat_to_int_bij: "surj nat_to_int_bij"
-by (blast intro: n2i_i2n_id surjI)
-
-lemma surj_int_to_nat_bij: "surj int_to_nat_bij"
-by (blast intro: i2n_n2i_id surjI)
-
-lemma inj_nat_to_int_bij: "inj nat_to_int_bij"
-by(simp add:inv_int_to_nat_bij[symmetric] surj_int_to_nat_bij surj_imp_inj_inv)
-
-lemma inj_int_to_nat_bij: "inj int_to_nat_bij"
-by(simp add:inv_nat_to_int_bij[symmetric] surj_nat_to_int_bij surj_imp_inj_inv)
-
-
-end
--- a/src/HOL/Nominal/Nominal.thy	Mon Feb 16 10:13:30 2009 +0100
+++ b/src/HOL/Nominal/Nominal.thy	Mon Feb 16 10:15:43 2009 +0100
@@ -558,12 +558,7 @@
   fixes x :: "'x"
   assumes at: "at TYPE('x)"
   shows "supp x = {x}"
-proof (simp add: supp_def Collect_conj_eq Collect_imp_eq at_calc[OF at], auto)
-  assume f: "finite {b::'x. b \<noteq> x}"
-  have a1: "{b::'x. b \<noteq> x} = UNIV-{x}" by force
-  have a2: "infinite (UNIV::'x set)" by (rule at4[OF at])
-  from f a1 a2 show False by force
-qed
+by(auto simp: supp_def Collect_conj_eq Collect_imp_eq at_calc[OF at] at4[OF at])
 
 lemma at_fresh:
   fixes a :: "'x"
@@ -1791,8 +1786,8 @@
     by (simp add: finite_infinite[OF f3,OF at4[OF at], simplified])
   hence "infinite ({c. [(a,c)]\<bullet>x = x \<and> [(b,c)]\<bullet>x = x}-{a,b})" 
     by (force dest: Diff_infinite_finite)
-  hence "({c. [(a,c)]\<bullet>x = x \<and> [(b,c)]\<bullet>x = x}-{a,b}) \<noteq> {}" 
-    by (auto iff del: finite_Diff_insert Diff_eq_empty_iff)
+  hence "({c. [(a,c)]\<bullet>x = x \<and> [(b,c)]\<bullet>x = x}-{a,b}) \<noteq> {}"
+    by (metis Collect_def finite_set set_empty2)
   hence "\<exists>c. c\<in>({c. [(a,c)]\<bullet>x = x \<and> [(b,c)]\<bullet>x = x}-{a,b})" by (force)
   then obtain c 
     where eq1: "[(a,c)]\<bullet>x = x" 
--- a/src/HOL/NumberTheory/IntPrimes.thy	Mon Feb 16 10:13:30 2009 +0100
+++ b/src/HOL/NumberTheory/IntPrimes.thy	Mon Feb 16 10:15:43 2009 +0100
@@ -144,12 +144,8 @@
   done
 
 lemma zcong_trans:
-    "[a = b] (mod m) ==> [b = c] (mod m) ==> [a = c] (mod m)"
-  unfolding zcong_def
-  apply (auto elim!: dvdE simp add: algebra_simps)
-  unfolding left_distrib [symmetric]
-  apply (rule dvd_mult dvd_refl)+
-  done
+  "[a = b] (mod m) ==> [b = c] (mod m) ==> [a = c] (mod m)"
+unfolding zcong_def by (auto elim!: dvdE simp add: algebra_simps)
 
 lemma zcong_zmult:
     "[a = b] (mod m) ==> [c = d] (mod m) ==> [a * c = b * d] (mod m)"
--- a/src/HOL/OrderedGroup.thy	Mon Feb 16 10:13:30 2009 +0100
+++ b/src/HOL/OrderedGroup.thy	Mon Feb 16 10:15:43 2009 +0100
@@ -147,6 +147,9 @@
 
 end
 
+class cancel_comm_monoid_add = cancel_ab_semigroup_add + comm_monoid_add
+
+
 subsection {* Groups *}
 
 class group_add = minus + uminus + monoid_add +
@@ -251,6 +254,16 @@
 
 declare diff_minus[symmetric, algebra_simps]
 
+lemma eq_neg_iff_add_eq_0: "a = - b \<longleftrightarrow> a + b = 0"
+proof
+  assume "a = - b" then show "a + b = 0" by simp
+next
+  assume "a + b = 0"
+  moreover have "a + (b + - b) = (a + b) + - b"
+    by (simp only: add_assoc)
+  ultimately show "a = - b" by simp
+qed
+
 end
 
 class ab_group_add = minus + uminus + comm_monoid_add +
@@ -261,7 +274,7 @@
 subclass group_add
   proof qed (simp_all add: ab_left_minus ab_diff_minus)
 
-subclass cancel_ab_semigroup_add
+subclass cancel_comm_monoid_add
 proof
   fix a b c :: 'a
   assume "a + b = a + c"
@@ -478,6 +491,26 @@
   then show ?thesis by simp
 qed
 
+lemma add_nonneg_eq_0_iff:
+  assumes x: "0 \<le> x" and y: "0 \<le> y"
+  shows "x + y = 0 \<longleftrightarrow> x = 0 \<and> y = 0"
+proof (intro iffI conjI)
+  have "x = x + 0" by simp
+  also have "x + 0 \<le> x + y" using y by (rule add_left_mono)
+  also assume "x + y = 0"
+  also have "0 \<le> x" using x .
+  finally show "x = 0" .
+next
+  have "y = 0 + y" by simp
+  also have "0 + y \<le> x + y" using x by (rule add_right_mono)
+  also assume "x + y = 0"
+  also have "0 \<le> y" using y .
+  finally show "y = 0" .
+next
+  assume "x = 0 \<and> y = 0"
+  then show "x + y = 0" by simp
+qed
+
 end
 
 class pordered_ab_group_add =
--- a/src/HOL/Polynomial.thy	Mon Feb 16 10:13:30 2009 +0100
+++ b/src/HOL/Polynomial.thy	Mon Feb 16 10:15:43 2009 +0100
@@ -293,8 +293,7 @@
 
 end
 
-instance poly ::
-  ("{cancel_ab_semigroup_add,comm_monoid_add}") cancel_ab_semigroup_add
+instance poly :: (cancel_comm_monoid_add) cancel_comm_monoid_add
 proof
   fix p q r :: "'a poly"
   assume "p + q = p + r" thus "q = r"
--- a/src/HOL/Rational.thy	Mon Feb 16 10:13:30 2009 +0100
+++ b/src/HOL/Rational.thy	Mon Feb 16 10:15:43 2009 +0100
@@ -5,7 +5,7 @@
 header {* Rational numbers *}
 
 theory Rational
-imports Nat_Int_Bij GCD
+imports GCD
 uses ("Tools/rat_arith.ML")
 begin
 
@@ -790,46 +790,6 @@
   by (rule Rats_cases) auto
 
 
-subsection {* The Rationals are Countably Infinite *}
-
-definition nat_to_rat_surj :: "nat \<Rightarrow> rat" where
-"nat_to_rat_surj n = (let (a,b) = nat_to_nat2 n
-                      in Fract (nat_to_int_bij a) (nat_to_int_bij b))"
-
-lemma surj_nat_to_rat_surj: "surj nat_to_rat_surj"
-unfolding surj_def
-proof
-  fix r::rat
-  show "\<exists>n. r = nat_to_rat_surj n"
-  proof(cases r)
-    fix i j assume [simp]: "r = Fract i j" and "j \<noteq> 0"
-    have "r = (let m = inv nat_to_int_bij i; n = inv nat_to_int_bij j
-               in nat_to_rat_surj(nat2_to_nat (m,n)))"
-      using nat2_to_nat_inj surj_f_inv_f[OF surj_nat_to_int_bij]
-      by(simp add:Let_def nat_to_rat_surj_def nat_to_nat2_def)
-    thus "\<exists>n. r = nat_to_rat_surj n" by(auto simp:Let_def)
-  qed
-qed
-
-lemma Rats_eq_range_nat_to_rat_surj: "\<rat> = range nat_to_rat_surj"
-by (simp add: Rats_def surj_nat_to_rat_surj surj_range)
-
-context field_char_0
-begin
-
-lemma Rats_eq_range_of_rat_o_nat_to_rat_surj:
-  "\<rat> = range (of_rat o nat_to_rat_surj)"
-using surj_nat_to_rat_surj
-by (auto simp: Rats_def image_def surj_def)
-   (blast intro: arg_cong[where f = of_rat])
-
-lemma surj_of_rat_nat_to_rat_surj:
-  "r\<in>\<rat> \<Longrightarrow> \<exists>n. r = of_rat(nat_to_rat_surj n)"
-by(simp add: Rats_eq_range_of_rat_o_nat_to_rat_surj image_def)
-
-end
-
-
 subsection {* Implementation of rational numbers as pairs of integers *}
 
 lemma Fract_norm: "Fract (a div zgcd a b) (b div zgcd a b) = Fract a b"
@@ -841,7 +801,7 @@
   then have "?c \<noteq> 0" by simp
   then have "Fract ?c ?c = Fract 1 1" by (simp add: eq_rat)
   moreover have "Fract (a div ?c * ?c + a mod ?c) (b div ?c * ?c + b mod ?c) = Fract a b"
-   by (simp add: semiring_div_class.mod_div_equality)
+    by (simp add: semiring_div_class.mod_div_equality)
   moreover have "a mod ?c = 0" by (simp add: dvd_eq_mod_eq_0 [symmetric])
   moreover have "b mod ?c = 0" by (simp add: dvd_eq_mod_eq_0 [symmetric])
   ultimately show ?thesis
@@ -1016,4 +976,4 @@
   | rat_of_int i = (i, 1);
 *}
 
-end
\ No newline at end of file
+end
--- a/src/HOL/Reflection/Approximation.thy	Mon Feb 16 10:13:30 2009 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,2507 +0,0 @@
-(* Title:     HOL/Reflection/Approximation.thy
- * Author:    Johannes Hölzl <hoelzl@in.tum.de> 2008 / 2009
- *)
-header {* Prove unequations about real numbers by computation *}
-theory Approximation
-imports Complex_Main Float Reflection Efficient_Nat
-begin
-
-section "Horner Scheme"
-
-subsection {* Define auxiliary helper @{text horner} function *}
-
-fun horner :: "(nat \<Rightarrow> nat) \<Rightarrow> (nat \<Rightarrow> nat \<Rightarrow> nat) \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> real \<Rightarrow> real" where
-"horner F G 0 i k x       = 0" |
-"horner F G (Suc n) i k x = 1 / real k - x * horner F G n (F i) (G i k) x"
-
-lemma horner_schema': fixes x :: real  and a :: "nat \<Rightarrow> real"
-  shows "a 0 - x * (\<Sum> i=0..<n. (-1)^i * a (Suc i) * x^i) = (\<Sum> i=0..<Suc n. (-1)^i * a i * x^i)"
-proof -
-  have shift_pow: "\<And>i. - (x * ((-1)^i * a (Suc i) * x ^ i)) = (-1)^(Suc i) * a (Suc i) * x ^ (Suc i)" by auto
-  show ?thesis unfolding setsum_right_distrib shift_pow real_diff_def setsum_negf[symmetric] setsum_head_upt_Suc[OF zero_less_Suc]
-    setsum_reindex[OF inj_Suc, unfolded comp_def, symmetric, of "\<lambda> n. (-1)^n  *a n * x^n"] by auto
-qed
-
-lemma horner_schema: fixes f :: "nat \<Rightarrow> nat" and G :: "nat \<Rightarrow> nat \<Rightarrow> nat" and F :: "nat \<Rightarrow> nat"
-  assumes f_Suc: "\<And>n. f (Suc n) = G ((F^n) s) (f n)"
-  shows "horner F G n ((F^j') s) (f j') x = (\<Sum> j = 0..< n. -1^j * (1 / real (f (j' + j))) * x^j)"
-proof (induct n arbitrary: i k j')
-  case (Suc n)
-
-  show ?case unfolding horner.simps Suc[where j'="Suc j'", unfolded funpow.simps comp_def f_Suc]
-    using horner_schema'[of "\<lambda> j. 1 / real (f (j' + j))"] by auto
-qed auto
-
-lemma horner_bounds':
-  assumes "0 \<le> Ifloat x" and f_Suc: "\<And>n. f (Suc n) = G ((F^n) s) (f n)"
-  and lb_0: "\<And> i k x. lb 0 i k x = 0"
-  and lb_Suc: "\<And> n i k x. lb (Suc n) i k x = lapprox_rat prec 1 (int k) - x * (ub n (F i) (G i k) x)"
-  and ub_0: "\<And> i k x. ub 0 i k x = 0"
-  and ub_Suc: "\<And> n i k x. ub (Suc n) i k x = rapprox_rat prec 1 (int k) - x * (lb n (F i) (G i k) x)"
-  shows "Ifloat (lb n ((F^j') s) (f j') x) \<le> horner F G n ((F^j') s) (f j') (Ifloat x) \<and> 
-         horner F G n ((F^j') s) (f j') (Ifloat x) \<le> Ifloat (ub n ((F^j') s) (f j') x)"
-  (is "?lb n j' \<le> ?horner n j' \<and> ?horner n j' \<le> ?ub n j'")
-proof (induct n arbitrary: j')
-  case 0 thus ?case unfolding lb_0 ub_0 horner.simps by auto
-next
-  case (Suc n)
-  have "?lb (Suc n) j' \<le> ?horner (Suc n) j'" unfolding lb_Suc ub_Suc horner.simps Ifloat_sub diff_def
-  proof (rule add_mono)
-    show "Ifloat (lapprox_rat prec 1 (int (f j'))) \<le> 1 / real (f j')" using lapprox_rat[of prec 1  "int (f j')"] by auto
-    from Suc[where j'="Suc j'", unfolded funpow.simps comp_def f_Suc, THEN conjunct2] `0 \<le> Ifloat x`
-    show "- Ifloat (x * ub n (F ((F ^ j') s)) (G ((F ^ j') s) (f j')) x) \<le> - (Ifloat x * horner F G n (F ((F ^ j') s)) (G ((F ^ j') s) (f j')) (Ifloat x))"
-      unfolding Ifloat_mult neg_le_iff_le by (rule mult_left_mono)
-  qed
-  moreover have "?horner (Suc n) j' \<le> ?ub (Suc n) j'" unfolding ub_Suc ub_Suc horner.simps Ifloat_sub diff_def
-  proof (rule add_mono)
-    show "1 / real (f j') \<le> Ifloat (rapprox_rat prec 1 (int (f j')))" using rapprox_rat[of 1 "int (f j')" prec] by auto
-    from Suc[where j'="Suc j'", unfolded funpow.simps comp_def f_Suc, THEN conjunct1] `0 \<le> Ifloat x`
-    show "- (Ifloat x * horner F G n (F ((F ^ j') s)) (G ((F ^ j') s) (f j')) (Ifloat x)) \<le> 
-          - Ifloat (x * lb n (F ((F ^ j') s)) (G ((F ^ j') s) (f j')) x)"
-      unfolding Ifloat_mult neg_le_iff_le by (rule mult_left_mono)
-  qed
-  ultimately show ?case by blast
-qed
-
-subsection "Theorems for floating point functions implementing the horner scheme"
-
-text {*
-
-Here @{term_type "f :: nat \<Rightarrow> nat"} is the sequence defining the Taylor series, the coefficients are
-all alternating and reciprocs. We use @{term G} and @{term F} to describe the computation of @{term f}.
-
-*}
-
-lemma horner_bounds: fixes F :: "nat \<Rightarrow> nat" and G :: "nat \<Rightarrow> nat \<Rightarrow> nat"
-  assumes "0 \<le> Ifloat x" and f_Suc: "\<And>n. f (Suc n) = G ((F^n) s) (f n)"
-  and lb_0: "\<And> i k x. lb 0 i k x = 0"
-  and lb_Suc: "\<And> n i k x. lb (Suc n) i k x = lapprox_rat prec 1 (int k) - x * (ub n (F i) (G i k) x)"
-  and ub_0: "\<And> i k x. ub 0 i k x = 0"
-  and ub_Suc: "\<And> n i k x. ub (Suc n) i k x = rapprox_rat prec 1 (int k) - x * (lb n (F i) (G i k) x)"
-  shows "Ifloat (lb n ((F^j') s) (f j') x) \<le> (\<Sum>j=0..<n. -1^j * (1 / real (f (j' + j))) * (Ifloat x)^j)" (is "?lb") and 
-        "(\<Sum>j=0..<n. -1^j * (1 / real (f (j' + j))) * (Ifloat x)^j) \<le> Ifloat (ub n ((F^j') s) (f j') x)" (is "?ub")
-proof -
-  have "?lb  \<and> ?ub" 
-    using horner_bounds'[where lb=lb, OF `0 \<le> Ifloat x` f_Suc lb_0 lb_Suc ub_0 ub_Suc]
-    unfolding horner_schema[where f=f, OF f_Suc] .
-  thus "?lb" and "?ub" by auto
-qed
-
-lemma horner_bounds_nonpos: fixes F :: "nat \<Rightarrow> nat" and G :: "nat \<Rightarrow> nat \<Rightarrow> nat"
-  assumes "Ifloat x \<le> 0" and f_Suc: "\<And>n. f (Suc n) = G ((F^n) s) (f n)"
-  and lb_0: "\<And> i k x. lb 0 i k x = 0"
-  and lb_Suc: "\<And> n i k x. lb (Suc n) i k x = lapprox_rat prec 1 (int k) + x * (ub n (F i) (G i k) x)"
-  and ub_0: "\<And> i k x. ub 0 i k x = 0"
-  and ub_Suc: "\<And> n i k x. ub (Suc n) i k x = rapprox_rat prec 1 (int k) + x * (lb n (F i) (G i k) x)"
-  shows "Ifloat (lb n ((F^j') s) (f j') x) \<le> (\<Sum>j=0..<n. (1 / real (f (j' + j))) * (Ifloat x)^j)" (is "?lb") and 
-        "(\<Sum>j=0..<n. (1 / real (f (j' + j))) * (Ifloat x)^j) \<le> Ifloat (ub n ((F^j') s) (f j') x)" (is "?ub")
-proof -
-  { fix x y z :: float have "x - y * z = x + - y * z"
-      by (cases x, cases y, cases z, simp add: plus_float.simps minus_float.simps uminus_float.simps times_float.simps algebra_simps)
-  } note diff_mult_minus = this
-
-  { fix x :: float have "- (- x) = x" by (cases x, auto simp add: uminus_float.simps) } note minus_minus = this
-
-  have move_minus: "Ifloat (-x) = -1 * Ifloat x" by auto
-
-  have sum_eq: "(\<Sum>j=0..<n. (1 / real (f (j' + j))) * (Ifloat x)^j) = 
-    (\<Sum>j = 0..<n. -1 ^ j * (1 / real (f (j' + j))) * Ifloat (- x) ^ j)"
-  proof (rule setsum_cong, simp)
-    fix j assume "j \<in> {0 ..< n}"
-    show "1 / real (f (j' + j)) * Ifloat x ^ j = -1 ^ j * (1 / real (f (j' + j))) * Ifloat (- x) ^ j"
-      unfolding move_minus power_mult_distrib real_mult_assoc[symmetric]
-      unfolding real_mult_commute unfolding real_mult_assoc[of "-1^j", symmetric] power_mult_distrib[symmetric]
-      by auto
-  qed
-
-  have "0 \<le> Ifloat (-x)" using assms by auto
-  from horner_bounds[where G=G and F=F and f=f and s=s and prec=prec
-    and lb="\<lambda> n i k x. lb n i k (-x)" and ub="\<lambda> n i k x. ub n i k (-x)", unfolded lb_Suc ub_Suc diff_mult_minus,
-    OF this f_Suc lb_0 refl ub_0 refl]
-  show "?lb" and "?ub" unfolding minus_minus sum_eq
-    by auto
-qed
-
-subsection {* Selectors for next even or odd number *}
-
-text {*
-
-The horner scheme computes alternating series. To get the upper and lower bounds we need to
-guarantee to access a even or odd member. To do this we use @{term get_odd} and @{term get_even}.
-
-*}
-
-definition get_odd :: "nat \<Rightarrow> nat" where
-  "get_odd n = (if odd n then n else (Suc n))"
-
-definition get_even :: "nat \<Rightarrow> nat" where
-  "get_even n = (if even n then n else (Suc n))"
-
-lemma get_odd[simp]: "odd (get_odd n)" unfolding get_odd_def by (cases "odd n", auto)
-lemma get_even[simp]: "even (get_even n)" unfolding get_even_def by (cases "even n", auto)
-lemma get_odd_ex: "\<exists> k. Suc k = get_odd n \<and> odd (Suc k)"
-proof (cases "odd n")
-  case True hence "0 < n" by (rule odd_pos)
-  from gr0_implies_Suc[OF this] obtain k where "Suc k = n" by auto 
-  thus ?thesis unfolding get_odd_def if_P[OF True] using True[unfolded `Suc k = n`[symmetric]] by blast
-next
-  case False hence "odd (Suc n)" by auto
-  thus ?thesis unfolding get_odd_def if_not_P[OF False] by blast
-qed
-
-lemma get_even_double: "\<exists>i. get_even n = 2 * i" using get_even[unfolded even_mult_two_ex] .
-lemma get_odd_double: "\<exists>i. get_odd n = 2 * i + 1" using get_odd[unfolded odd_Suc_mult_two_ex] by auto
-
-section "Power function"
-
-definition float_power_bnds :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float * float" where
-"float_power_bnds n l u = (if odd n \<or> 0 < l then (l ^ n, u ^ n)
-                      else if u < 0         then (u ^ n, l ^ n)
-                                            else (0, (max (-l) u) ^ n))"
-
-lemma float_power_bnds: assumes "(l1, u1) = float_power_bnds n l u" and "x \<in> {Ifloat l .. Ifloat u}"
-  shows "x^n \<in> {Ifloat l1..Ifloat u1}"
-proof (cases "even n")
-  case True 
-  show ?thesis
-  proof (cases "0 < l")
-    case True hence "odd n \<or> 0 < l" and "0 \<le> Ifloat l" unfolding less_float_def by auto
-    have u1: "u1 = u ^ n" and l1: "l1 = l ^ n" using assms unfolding float_power_bnds_def if_P[OF `odd n \<or> 0 < l`] by auto
-    have "Ifloat l^n \<le> x^n" and "x^n \<le> Ifloat u^n " using `0 \<le> Ifloat l` and assms unfolding atLeastAtMost_iff using power_mono[of "Ifloat l" x] power_mono[of x "Ifloat u"] by auto
-    thus ?thesis using assms `0 < l` unfolding atLeastAtMost_iff l1 u1 float_power less_float_def by auto
-  next
-    case False hence P: "\<not> (odd n \<or> 0 < l)" using `even n` by auto
-    show ?thesis
-    proof (cases "u < 0")
-      case True hence "0 \<le> - Ifloat u" and "- Ifloat u \<le> - x" and "0 \<le> - x" and "-x \<le> - Ifloat l" using assms unfolding less_float_def by auto
-      hence "Ifloat u^n \<le> x^n" and "x^n \<le> Ifloat l^n" using power_mono[of  "-x" "-Ifloat l" n] power_mono[of "-Ifloat u" "-x" n] 
-	unfolding power_minus_even[OF `even n`] by auto
-      moreover have u1: "u1 = l ^ n" and l1: "l1 = u ^ n" using assms unfolding float_power_bnds_def if_not_P[OF P] if_P[OF True] by auto
-      ultimately show ?thesis using float_power by auto
-    next
-      case False 
-      have "\<bar>x\<bar> \<le> Ifloat (max (-l) u)"
-      proof (cases "-l \<le> u")
-	case True thus ?thesis unfolding max_def if_P[OF True] using assms unfolding le_float_def by auto
-      next
-	case False thus ?thesis unfolding max_def if_not_P[OF False] using assms unfolding le_float_def by auto
-      qed
-      hence x_abs: "\<bar>x\<bar> \<le> \<bar>Ifloat (max (-l) u)\<bar>" by auto
-      have u1: "u1 = (max (-l) u) ^ n" and l1: "l1 = 0" using assms unfolding float_power_bnds_def if_not_P[OF P] if_not_P[OF False] by auto
-      show ?thesis unfolding atLeastAtMost_iff l1 u1 float_power using zero_le_even_power[OF `even n`] power_mono_even[OF `even n` x_abs] by auto
-    qed
-  qed
-next
-  case False hence "odd n \<or> 0 < l" by auto
-  have u1: "u1 = u ^ n" and l1: "l1 = l ^ n" using assms unfolding float_power_bnds_def if_P[OF `odd n \<or> 0 < l`] by auto
-  have "Ifloat l^n \<le> x^n" and "x^n \<le> Ifloat u^n " using assms unfolding atLeastAtMost_iff using power_mono_odd[OF False] by auto
-  thus ?thesis unfolding atLeastAtMost_iff l1 u1 float_power less_float_def by auto
-qed
-
-lemma bnds_power: "\<forall> x l u. (l1, u1) = float_power_bnds n l u \<and> x \<in> {Ifloat l .. Ifloat u} \<longrightarrow> Ifloat l1 \<le> x^n \<and> x^n \<le> Ifloat u1"
-  using float_power_bnds by auto
-
-section "Square root"
-
-text {*
-
-The square root computation is implemented as newton iteration. As first first step we use the
-nearest power of two greater than the square root.
-
-*}
-
-fun sqrt_iteration :: "nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float" where
-"sqrt_iteration prec 0 (Float m e) = Float 1 ((e + bitlen m) div 2 + 1)" |
-"sqrt_iteration prec (Suc m) x = (let y = sqrt_iteration prec m x 
-                                  in Float 1 -1 * (y + float_divr prec x y))"
-
-definition ub_sqrt :: "nat \<Rightarrow> float \<Rightarrow> float option" where 
-"ub_sqrt prec x = (if 0 < x then Some (sqrt_iteration prec prec x) else if x < 0 then None else Some 0)"
-
-definition lb_sqrt :: "nat \<Rightarrow> float \<Rightarrow> float option" where
-"lb_sqrt prec x = (if 0 < x then Some (float_divl prec x (sqrt_iteration prec prec x)) else if x < 0 then None else Some 0)"
-
-lemma sqrt_ub_pos_pos_1:
-  assumes "sqrt x < b" and "0 < b" and "0 < x"
-  shows "sqrt x < (b + x / b)/2"
-proof -
-  from assms have "0 < (b - sqrt x) ^ 2 " by simp
-  also have "\<dots> = b ^ 2 - 2 * b * sqrt x + (sqrt x) ^ 2" by algebra
-  also have "\<dots> = b ^ 2 - 2 * b * sqrt x + x" using assms by (simp add: real_sqrt_pow2)
-  finally have "0 < b ^ 2 - 2 * b * sqrt x + x" by assumption
-  hence "0 < b / 2 - sqrt x + x / (2 * b)" using assms
-    by (simp add: field_simps power2_eq_square)
-  thus ?thesis by (simp add: field_simps)
-qed
-
-lemma sqrt_iteration_bound: assumes "0 < Ifloat x"
-  shows "sqrt (Ifloat x) < Ifloat (sqrt_iteration prec n x)"
-proof (induct n)
-  case 0
-  show ?case
-  proof (cases x)
-    case (Float m e)
-    hence "0 < m" using float_pos_m_pos[unfolded less_float_def] assms by auto
-    hence "0 < sqrt (real m)" by auto
-
-    have int_nat_bl: "int (nat (bitlen m)) = bitlen m" using bitlen_ge0 by auto
-
-    have "Ifloat x = (real m / 2^nat (bitlen m)) * pow2 (e + int (nat (bitlen m)))"
-      unfolding pow2_add pow2_int Float Ifloat.simps by auto
-    also have "\<dots> < 1 * pow2 (e + int (nat (bitlen m)))"
-    proof (rule mult_strict_right_mono, auto)
-      show "real m < 2^nat (bitlen m)" using bitlen_bounds[OF `0 < m`, THEN conjunct2] 
-	unfolding real_of_int_less_iff[of m, symmetric] by auto
-    qed
-    finally have "sqrt (Ifloat x) < sqrt (pow2 (e + bitlen m))" unfolding int_nat_bl by auto
-    also have "\<dots> \<le> pow2 ((e + bitlen m) div 2 + 1)"
-    proof -
-      let ?E = "e + bitlen m"
-      have E_mod_pow: "pow2 (?E mod 2) < 4"
-      proof (cases "?E mod 2 = 1")
-	case True thus ?thesis by auto
-      next
-	case False 
-	have "0 \<le> ?E mod 2" by auto 
-	have "?E mod 2 < 2" by auto
-	from this[THEN zless_imp_add1_zle]
-	have "?E mod 2 \<le> 0" using False by auto
-	from xt1(5)[OF `0 \<le> ?E mod 2` this]
-	show ?thesis by auto
-      qed
-      hence "sqrt (pow2 (?E mod 2)) < sqrt (2 * 2)" by auto
-      hence E_mod_pow: "sqrt (pow2 (?E mod 2)) < 2" unfolding real_sqrt_abs2 by auto
-
-      have E_eq: "pow2 ?E = pow2 (?E div 2 + ?E div 2 + ?E mod 2)" by auto
-      have "sqrt (pow2 ?E) = sqrt (pow2 (?E div 2) * pow2 (?E div 2) * pow2 (?E mod 2))"
-	unfolding E_eq unfolding pow2_add ..
-      also have "\<dots> = pow2 (?E div 2) * sqrt (pow2 (?E mod 2))"
-	unfolding real_sqrt_mult[of _ "pow2 (?E mod 2)"] real_sqrt_abs2 by auto
-      also have "\<dots> < pow2 (?E div 2) * 2" 
-	by (rule mult_strict_left_mono, auto intro: E_mod_pow)
-      also have "\<dots> = pow2 (?E div 2 + 1)" unfolding zadd_commute[of _ 1] pow2_add1 by auto
-      finally show ?thesis by auto
-    qed
-    finally show ?thesis 
-      unfolding Float sqrt_iteration.simps Ifloat.simps by auto
-  qed
-next
-  case (Suc n)
-  let ?b = "sqrt_iteration prec n x"
-  have "0 < sqrt (Ifloat x)" using `0 < Ifloat x` by auto
-  also have "\<dots> < Ifloat ?b" using Suc .
-  finally have "sqrt (Ifloat x) < (Ifloat ?b + Ifloat x / Ifloat ?b)/2" using sqrt_ub_pos_pos_1[OF Suc _ `0 < Ifloat x`] by auto
-  also have "\<dots> \<le> (Ifloat ?b + Ifloat (float_divr prec x ?b))/2" by (rule divide_right_mono, auto simp add: float_divr)
-  also have "\<dots> = Ifloat (Float 1 -1) * (Ifloat ?b + Ifloat (float_divr prec x ?b))" by auto
-  finally show ?case unfolding sqrt_iteration.simps Let_def Ifloat_mult Ifloat_add right_distrib .
-qed
-
-lemma sqrt_iteration_lower_bound: assumes "0 < Ifloat x"
-  shows "0 < Ifloat (sqrt_iteration prec n x)" (is "0 < ?sqrt")
-proof -
-  have "0 < sqrt (Ifloat x)" using assms by auto
-  also have "\<dots> < ?sqrt" using sqrt_iteration_bound[OF assms] .
-  finally show ?thesis .
-qed
-
-lemma lb_sqrt_lower_bound: assumes "0 \<le> Ifloat x"
-  shows "0 \<le> Ifloat (the (lb_sqrt prec x))"
-proof (cases "0 < x")
-  case True hence "0 < Ifloat x" and "0 \<le> x" using `0 \<le> Ifloat x` unfolding less_float_def le_float_def by auto
-  hence "0 < sqrt_iteration prec prec x" unfolding less_float_def using sqrt_iteration_lower_bound by auto 
-  hence "0 \<le> Ifloat (float_divl prec x (sqrt_iteration prec prec x))" using float_divl_lower_bound[OF `0 \<le> x`] unfolding le_float_def by auto
-  thus ?thesis unfolding lb_sqrt_def using True by auto
-next
-  case False with `0 \<le> Ifloat x` have "Ifloat x = 0" unfolding less_float_def by auto
-  thus ?thesis unfolding lb_sqrt_def less_float_def by auto
-qed
-
-lemma lb_sqrt_upper_bound: assumes "0 \<le> Ifloat x"
-  shows "Ifloat (the (lb_sqrt prec x)) \<le> sqrt (Ifloat x)"
-proof (cases "0 < x")
-  case True hence "0 < Ifloat x" and "0 \<le> Ifloat x" unfolding less_float_def by auto
-  hence sqrt_gt0: "0 < sqrt (Ifloat x)" by auto
-  hence sqrt_ub: "sqrt (Ifloat x) < Ifloat (sqrt_iteration prec prec x)" using sqrt_iteration_bound by auto
-  
-  have "Ifloat (float_divl prec x (sqrt_iteration prec prec x)) \<le> Ifloat x / Ifloat (sqrt_iteration prec prec x)" by (rule float_divl)
-  also have "\<dots> < Ifloat x / sqrt (Ifloat x)" 
-    by (rule divide_strict_left_mono[OF sqrt_ub `0 < Ifloat x` mult_pos_pos[OF order_less_trans[OF sqrt_gt0 sqrt_ub] sqrt_gt0]])
-  also have "\<dots> = sqrt (Ifloat x)" unfolding inverse_eq_iff_eq[of _ "sqrt (Ifloat x)", symmetric] sqrt_divide_self_eq[OF `0 \<le> Ifloat x`, symmetric] by auto
-  finally show ?thesis unfolding lb_sqrt_def if_P[OF `0 < x`] by auto
-next
-  case False with `0 \<le> Ifloat x`
-  have "\<not> x < 0" unfolding less_float_def le_float_def by auto
-  show ?thesis unfolding lb_sqrt_def if_not_P[OF False] if_not_P[OF `\<not> x < 0`] using assms by auto
-qed
-
-lemma lb_sqrt: assumes "Some y = lb_sqrt prec x"
-  shows "Ifloat y \<le> sqrt (Ifloat x)" and "0 \<le> Ifloat x"
-proof -
-  show "0 \<le> Ifloat x"
-  proof (rule ccontr)
-    assume "\<not> 0 \<le> Ifloat x"
-    hence "lb_sqrt prec x = None" unfolding lb_sqrt_def less_float_def by auto
-    thus False using assms by auto
-  qed
-  from lb_sqrt_upper_bound[OF this, of prec]
-  show "Ifloat y \<le> sqrt (Ifloat x)" unfolding assms[symmetric] by auto
-qed
-
-lemma ub_sqrt_lower_bound: assumes "0 \<le> Ifloat x"
-  shows "sqrt (Ifloat x) \<le> Ifloat (the (ub_sqrt prec x))"
-proof (cases "0 < x")
-  case True hence "0 < Ifloat x" unfolding less_float_def by auto
-  hence "0 < sqrt (Ifloat x)" by auto
-  hence "sqrt (Ifloat x) < Ifloat (sqrt_iteration prec prec x)" using sqrt_iteration_bound by auto
-  thus ?thesis unfolding ub_sqrt_def if_P[OF `0 < x`] by auto
-next
-  case False with `0 \<le> Ifloat x`
-  have "Ifloat x = 0" unfolding less_float_def le_float_def by auto
-  thus ?thesis unfolding ub_sqrt_def less_float_def le_float_def by auto
-qed
-
-lemma ub_sqrt: assumes "Some y = ub_sqrt prec x"
-  shows "sqrt (Ifloat x) \<le> Ifloat y" and "0 \<le> Ifloat x"
-proof -
-  show "0 \<le> Ifloat x"
-  proof (rule ccontr)
-    assume "\<not> 0 \<le> Ifloat x"
-    hence "ub_sqrt prec x = None" unfolding ub_sqrt_def less_float_def by auto
-    thus False using assms by auto
-  qed
-  from ub_sqrt_lower_bound[OF this, of prec]
-  show "sqrt (Ifloat x) \<le> Ifloat y" unfolding assms[symmetric] by auto
-qed
-
-lemma bnds_sqrt: "\<forall> x lx ux. (Some l, Some u) = (lb_sqrt prec lx, ub_sqrt prec ux) \<and> x \<in> {Ifloat lx .. Ifloat ux} \<longrightarrow> Ifloat l \<le> sqrt x \<and> sqrt x \<le> Ifloat u"
-proof (rule allI, rule allI, rule allI, rule impI)
-  fix x lx ux
-  assume "(Some l, Some u) = (lb_sqrt prec lx, ub_sqrt prec ux) \<and> x \<in> {Ifloat lx .. Ifloat ux}"
-  hence l: "Some l = lb_sqrt prec lx " and u: "Some u = ub_sqrt prec ux" and x: "x \<in> {Ifloat lx .. Ifloat ux}" by auto
-  
-  have "Ifloat lx \<le> x" and "x \<le> Ifloat ux" using x by auto
-
-  from lb_sqrt(1)[OF l] real_sqrt_le_mono[OF `Ifloat lx \<le> x`]
-  have "Ifloat l \<le> sqrt x" by (rule order_trans)
-  moreover
-  from real_sqrt_le_mono[OF `x \<le> Ifloat ux`] ub_sqrt(1)[OF u]
-  have "sqrt x \<le> Ifloat u" by (rule order_trans)
-  ultimately show "Ifloat l \<le> sqrt x \<and> sqrt x \<le> Ifloat u" ..
-qed
-
-section "Arcus tangens and \<pi>"
-
-subsection "Compute arcus tangens series"
-
-text {*
-
-As first step we implement the computation of the arcus tangens series. This is only valid in the range
-@{term "{-1 :: real .. 1}"}. This is used to compute \<pi> and then the entire arcus tangens.
-
-*}
-
-fun ub_arctan_horner :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float"
-and lb_arctan_horner :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float" where
-  "ub_arctan_horner prec 0 k x = 0"
-| "ub_arctan_horner prec (Suc n) k x = 
-    (rapprox_rat prec 1 (int k)) - x * (lb_arctan_horner prec n (k + 2) x)"
-| "lb_arctan_horner prec 0 k x = 0"
-| "lb_arctan_horner prec (Suc n) k x = 
-    (lapprox_rat prec 1 (int k)) - x * (ub_arctan_horner prec n (k + 2) x)"
-
-lemma arctan_0_1_bounds': assumes "0 \<le> Ifloat x" "Ifloat x \<le> 1" and "even n"
-  shows "arctan (Ifloat x) \<in> {Ifloat (x * lb_arctan_horner prec n 1 (x * x)) .. Ifloat (x * ub_arctan_horner prec (Suc n) 1 (x * x))}"
-proof -
-  let "?c i" = "-1^i * (1 / real (i * 2 + 1) * Ifloat x ^ (i * 2 + 1))"
-  let "?S n" = "\<Sum> i=0..<n. ?c i"
-
-  have "0 \<le> Ifloat (x * x)" by auto
-  from `even n` obtain m where "2 * m = n" unfolding even_mult_two_ex by auto
-  
-  have "arctan (Ifloat x) \<in> { ?S n .. ?S (Suc n) }"
-  proof (cases "Ifloat x = 0")
-    case False
-    hence "0 < Ifloat x" using `0 \<le> Ifloat x` by auto
-    hence prem: "0 < 1 / real (0 * 2 + (1::nat)) * Ifloat x ^ (0 * 2 + 1)" by auto 
-
-    have "\<bar> Ifloat x \<bar> \<le> 1"  using `0 \<le> Ifloat x` `Ifloat x \<le> 1` by auto
-    from mp[OF summable_Leibniz(2)[OF zeroseq_arctan_series[OF this] monoseq_arctan_series[OF this]] prem, THEN spec, of m, unfolded `2 * m = n`]
-    show ?thesis unfolding arctan_series[OF `\<bar> Ifloat x \<bar> \<le> 1`] Suc_plus1  .
-  qed auto
-  note arctan_bounds = this[unfolded atLeastAtMost_iff]
-
-  have F: "\<And>n. 2 * Suc n + 1 = 2 * n + 1 + 2" by auto
-
-  note bounds = horner_bounds[where s=1 and f="\<lambda>i. 2 * i + 1" and j'=0 
-    and lb="\<lambda>n i k x. lb_arctan_horner prec n k x"
-    and ub="\<lambda>n i k x. ub_arctan_horner prec n k x", 
-    OF `0 \<le> Ifloat (x*x)` F lb_arctan_horner.simps ub_arctan_horner.simps]
-
-  { have "Ifloat (x * lb_arctan_horner prec n 1 (x*x)) \<le> ?S n"
-      using bounds(1) `0 \<le> Ifloat x`
-      unfolding Ifloat_mult power_add power_one_right real_mult_assoc[symmetric] setsum_left_distrib[symmetric]
-      unfolding real_mult_commute mult_commute[of _ "2::nat"] power_mult power2_eq_square[of "Ifloat x"]
-      by (auto intro!: mult_left_mono)
-    also have "\<dots> \<le> arctan (Ifloat x)" using arctan_bounds ..
-    finally have "Ifloat (x * lb_arctan_horner prec n 1 (x*x)) \<le> arctan (Ifloat x)" . }
-  moreover
-  { have "arctan (Ifloat x) \<le> ?S (Suc n)" using arctan_bounds ..
-    also have "\<dots> \<le> Ifloat (x * ub_arctan_horner prec (Suc n) 1 (x*x))"
-      using bounds(2)[of "Suc n"] `0 \<le> Ifloat x`
-      unfolding Ifloat_mult power_add power_one_right real_mult_assoc[symmetric] setsum_left_distrib[symmetric]
-      unfolding real_mult_commute mult_commute[of _ "2::nat"] power_mult power2_eq_square[of "Ifloat x"]
-      by (auto intro!: mult_left_mono)
-    finally have "arctan (Ifloat x) \<le> Ifloat (x * ub_arctan_horner prec (Suc n) 1 (x*x))" . }
-  ultimately show ?thesis by auto
-qed
-
-lemma arctan_0_1_bounds: assumes "0 \<le> Ifloat x" "Ifloat x \<le> 1"
-  shows "arctan (Ifloat x) \<in> {Ifloat (x * lb_arctan_horner prec (get_even n) 1 (x * x)) .. Ifloat (x * ub_arctan_horner prec (get_odd n) 1 (x * x))}"
-proof (cases "even n")
-  case True
-  obtain n' where "Suc n' = get_odd n" and "odd (Suc n')" using get_odd_ex by auto
-  hence "even n'" unfolding even_nat_Suc by auto
-  have "arctan (Ifloat x) \<le> Ifloat (x * ub_arctan_horner prec (get_odd n) 1 (x * x))"
-    unfolding `Suc n' = get_odd n`[symmetric] using arctan_0_1_bounds'[OF `0 \<le> Ifloat x` `Ifloat x \<le> 1` `even n'`] by auto
-  moreover
-  have "Ifloat (x * lb_arctan_horner prec (get_even n) 1 (x * x)) \<le> arctan (Ifloat x)"
-    unfolding get_even_def if_P[OF True] using arctan_0_1_bounds'[OF `0 \<le> Ifloat x` `Ifloat x \<le> 1` `even n`] by auto
-  ultimately show ?thesis by auto
-next
-  case False hence "0 < n" by (rule odd_pos)
-  from gr0_implies_Suc[OF this] obtain n' where "n = Suc n'" ..
-  from False[unfolded this even_nat_Suc]
-  have "even n'" and "even (Suc (Suc n'))" by auto
-  have "get_odd n = Suc n'" unfolding get_odd_def if_P[OF False] using `n = Suc n'` .
-
-  have "arctan (Ifloat x) \<le> Ifloat (x * ub_arctan_horner prec (get_odd n) 1 (x * x))"
-    unfolding `get_odd n = Suc n'` using arctan_0_1_bounds'[OF `0 \<le> Ifloat x` `Ifloat x \<le> 1` `even n'`] by auto
-  moreover
-  have "Ifloat (x * lb_arctan_horner prec (get_even n) 1 (x * x)) \<le> arctan (Ifloat x)"
-    unfolding get_even_def if_not_P[OF False] unfolding `n = Suc n'` using arctan_0_1_bounds'[OF `0 \<le> Ifloat x` `Ifloat x \<le> 1` `even (Suc (Suc n'))`] by auto
-  ultimately show ?thesis by auto
-qed
-
-subsection "Compute \<pi>"
-
-definition ub_pi :: "nat \<Rightarrow> float" where
-  "ub_pi prec = (let A = rapprox_rat prec 1 5 ; 
-                     B = lapprox_rat prec 1 239
-                 in ((Float 1 2) * ((Float 1 2) * A * (ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (A * A)) - 
-                                                  B * (lb_arctan_horner prec (get_even (prec div 14 + 1)) 1 (B * B)))))"
-
-definition lb_pi :: "nat \<Rightarrow> float" where
-  "lb_pi prec = (let A = lapprox_rat prec 1 5 ; 
-                     B = rapprox_rat prec 1 239
-                 in ((Float 1 2) * ((Float 1 2) * A * (lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (A * A)) - 
-                                                  B * (ub_arctan_horner prec (get_odd (prec div 14 + 1)) 1 (B * B)))))"
-
-lemma pi_boundaries: "pi \<in> {Ifloat (lb_pi n) .. Ifloat (ub_pi n)}"
-proof -
-  have machin_pi: "pi = 4 * (4 * arctan (1 / 5) - arctan (1 / 239))" unfolding machin[symmetric] by auto
-
-  { fix prec n :: nat fix k :: int assume "1 < k" hence "0 \<le> k" and "0 < k" and "1 \<le> k" by auto
-    let ?k = "rapprox_rat prec 1 k"
-    have "1 div k = 0" using div_pos_pos_trivial[OF _ `1 < k`] by auto
-      
-    have "0 \<le> Ifloat ?k" by (rule order_trans[OF _ rapprox_rat], auto simp add: `0 \<le> k`)
-    have "Ifloat ?k \<le> 1" unfolding rapprox_rat.simps(2)[OF zero_le_one `0 < k`]
-      by (rule rapprox_posrat_le1, auto simp add: `0 < k` `1 \<le> k`)
-
-    have "1 / real k \<le> Ifloat ?k" using rapprox_rat[where x=1 and y=k] by auto
-    hence "arctan (1 / real k) \<le> arctan (Ifloat ?k)" by (rule arctan_monotone')
-    also have "\<dots> \<le> Ifloat (?k * ub_arctan_horner prec (get_odd n) 1 (?k * ?k))"
-      using arctan_0_1_bounds[OF `0 \<le> Ifloat ?k` `Ifloat ?k \<le> 1`] by auto
-    finally have "arctan (1 / (real k)) \<le> Ifloat (?k * ub_arctan_horner prec (get_odd n) 1 (?k * ?k))" .
-  } note ub_arctan = this
-
-  { fix prec n :: nat fix k :: int assume "1 < k" hence "0 \<le> k" and "0 < k" by auto
-    let ?k = "lapprox_rat prec 1 k"
-    have "1 div k = 0" using div_pos_pos_trivial[OF _ `1 < k`] by auto
-    have "1 / real k \<le> 1" using `1 < k` by auto
-
-    have "\<And>n. 0 \<le> Ifloat ?k" using lapprox_rat_bottom[where x=1 and y=k, OF zero_le_one `0 < k`] by (auto simp add: `1 div k = 0`)
-    have "\<And>n. Ifloat ?k \<le> 1" using lapprox_rat by (rule order_trans, auto simp add: `1 / real k \<le> 1`)
-
-    have "Ifloat ?k \<le> 1 / real k" using lapprox_rat[where x=1 and y=k] by auto
-
-    have "Ifloat (?k * lb_arctan_horner prec (get_even n) 1 (?k * ?k)) \<le> arctan (Ifloat ?k)"
-      using arctan_0_1_bounds[OF `0 \<le> Ifloat ?k` `Ifloat ?k \<le> 1`] by auto
-    also have "\<dots> \<le> arctan (1 / real k)" using `Ifloat ?k \<le> 1 / real k` by (rule arctan_monotone')
-    finally have "Ifloat (?k * lb_arctan_horner prec (get_even n) 1 (?k * ?k)) \<le> arctan (1 / (real k))" .
-  } note lb_arctan = this
-
-  have "pi \<le> Ifloat (ub_pi n)"
-    unfolding ub_pi_def machin_pi Let_def Ifloat_mult Ifloat_sub unfolding Float_num
-    using lb_arctan[of 239] ub_arctan[of 5]
-    by (auto intro!: mult_left_mono add_mono simp add: diff_minus simp del: lapprox_rat.simps rapprox_rat.simps)
-  moreover
-  have "Ifloat (lb_pi n) \<le> pi"
-    unfolding lb_pi_def machin_pi Let_def Ifloat_mult Ifloat_sub Float_num
-    using lb_arctan[of 5] ub_arctan[of 239]
-    by (auto intro!: mult_left_mono add_mono simp add: diff_minus simp del: lapprox_rat.simps rapprox_rat.simps)
-  ultimately show ?thesis by auto
-qed
-
-subsection "Compute arcus tangens in the entire domain"
-
-function lb_arctan :: "nat \<Rightarrow> float \<Rightarrow> float" and ub_arctan :: "nat \<Rightarrow> float \<Rightarrow> float" where 
-  "lb_arctan prec x = (let ub_horner = \<lambda> x. x * ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (x * x) ;
-                           lb_horner = \<lambda> x. x * lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (x * x)
-    in (if x < 0          then - ub_arctan prec (-x) else
-        if x \<le> Float 1 -1 then lb_horner x else
-        if x \<le> Float 1 1  then Float 1 1 * lb_horner (float_divl prec x (1 + the (ub_sqrt prec (1 + x * x))))
-                          else (let inv = float_divr prec 1 x 
-                                in if inv > 1 then 0 
-                                              else lb_pi prec * Float 1 -1 - ub_horner inv)))"
-
-| "ub_arctan prec x = (let lb_horner = \<lambda> x. x * lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (x * x) ;
-                           ub_horner = \<lambda> x. x * ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (x * x)
-    in (if x < 0          then - lb_arctan prec (-x) else
-        if x \<le> Float 1 -1 then ub_horner x else
-        if x \<le> Float 1 1  then let y = float_divr prec x (1 + the (lb_sqrt prec (1 + x * x)))
-                               in if y > 1 then ub_pi prec * Float 1 -1 
-                                           else Float 1 1 * ub_horner y 
-                          else ub_pi prec * Float 1 -1 - lb_horner (float_divl prec 1 x)))"
-by pat_completeness auto
-termination by (relation "measure (\<lambda> v. let (prec, x) = sum_case id id v in (if x < 0 then 1 else 0))", auto simp add: less_float_def)
-
-declare ub_arctan_horner.simps[simp del]
-declare lb_arctan_horner.simps[simp del]
-
-lemma lb_arctan_bound': assumes "0 \<le> Ifloat x"
-  shows "Ifloat (lb_arctan prec x) \<le> arctan (Ifloat x)"
-proof -
-  have "\<not> x < 0" and "0 \<le> x" unfolding less_float_def le_float_def using `0 \<le> Ifloat x` by auto
-  let "?ub_horner x" = "x * ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (x * x)"
-    and "?lb_horner x" = "x * lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (x * x)"
-
-  show ?thesis
-  proof (cases "x \<le> Float 1 -1")
-    case True hence "Ifloat x \<le> 1" unfolding le_float_def Float_num by auto
-    show ?thesis unfolding lb_arctan.simps Let_def if_not_P[OF `\<not> x < 0`] if_P[OF True]
-      using arctan_0_1_bounds[OF `0 \<le> Ifloat x` `Ifloat x \<le> 1`] by auto
-  next
-    case False hence "0 < Ifloat x" unfolding le_float_def Float_num by auto
-    let ?R = "1 + sqrt (1 + Ifloat x * Ifloat x)"
-    let ?fR = "1 + the (ub_sqrt prec (1 + x * x))"
-    let ?DIV = "float_divl prec x ?fR"
-    
-    have sqr_ge0: "0 \<le> 1 + Ifloat x * Ifloat x" using sum_power2_ge_zero[of 1 "Ifloat x", unfolded numeral_2_eq_2] by auto
-    hence divisor_gt0: "0 < ?R" by (auto intro: add_pos_nonneg)
-
-    have "sqrt (Ifloat (1 + x * x)) \<le> Ifloat (the (ub_sqrt prec (1 + x * x)))" by (rule ub_sqrt_lower_bound, auto simp add: sqr_ge0)
-    hence "?R \<le> Ifloat ?fR" by auto
-    hence "0 < ?fR" and "0 < Ifloat ?fR" unfolding less_float_def using `0 < ?R` by auto
-
-    have monotone: "Ifloat (float_divl prec x ?fR) \<le> Ifloat x / ?R"
-    proof -
-      have "Ifloat ?DIV \<le> Ifloat x / Ifloat ?fR" by (rule float_divl)
-      also have "\<dots> \<le> Ifloat x / ?R" by (rule divide_left_mono[OF `?R \<le> Ifloat ?fR` `0 \<le> Ifloat x` mult_pos_pos[OF order_less_le_trans[OF divisor_gt0 `?R \<le> Ifloat ?fR`] divisor_gt0]])
-      finally show ?thesis .
-    qed
-
-    show ?thesis
-    proof (cases "x \<le> Float 1 1")
-      case True
-      
-      have "Ifloat x \<le> sqrt (Ifloat (1 + x * x))" using real_sqrt_sum_squares_ge2[where x=1, unfolded numeral_2_eq_2] by auto
-      also have "\<dots> \<le> Ifloat (the (ub_sqrt prec (1 + x * x)))" by (rule ub_sqrt_lower_bound, auto simp add: sqr_ge0)
-      finally have "Ifloat x \<le> Ifloat ?fR" by auto
-      moreover have "Ifloat ?DIV \<le> Ifloat x / Ifloat ?fR" by (rule float_divl)
-      ultimately have "Ifloat ?DIV \<le> 1" unfolding divide_le_eq_1_pos[OF `0 < Ifloat ?fR`, symmetric] by auto
-
-      have "0 \<le> Ifloat ?DIV" using float_divl_lower_bound[OF `0 \<le> x` `0 < ?fR`] unfolding le_float_def by auto
-
-      have "Ifloat (Float 1 1 * ?lb_horner ?DIV) \<le> 2 * arctan (Ifloat (float_divl prec x ?fR))" unfolding Ifloat_mult[of "Float 1 1"] Float_num
-	using arctan_0_1_bounds[OF `0 \<le> Ifloat ?DIV` `Ifloat ?DIV \<le> 1`] by auto
-      also have "\<dots> \<le> 2 * arctan (Ifloat x / ?R)"
-	using arctan_monotone'[OF monotone] by (auto intro!: mult_left_mono)
-      also have "2 * arctan (Ifloat x / ?R) = arctan (Ifloat x)" using arctan_half[symmetric] unfolding numeral_2_eq_2 power_Suc2 realpow_0 real_mult_1 . 
-      finally show ?thesis unfolding lb_arctan.simps Let_def if_not_P[OF `\<not> x < 0`] if_not_P[OF `\<not> x \<le> Float 1 -1`] if_P[OF True] .
-    next
-      case False
-      hence "2 < Ifloat x" unfolding le_float_def Float_num by auto
-      hence "1 \<le> Ifloat x" by auto
-
-      let "?invx" = "float_divr prec 1 x"
-      have "0 \<le> arctan (Ifloat x)" using arctan_monotone'[OF `0 \<le> Ifloat x`] using arctan_tan[of 0, unfolded tan_zero] by auto
-
-      show ?thesis
-      proof (cases "1 < ?invx")
-	case True
-	show ?thesis unfolding lb_arctan.simps Let_def if_not_P[OF `\<not> x < 0`] if_not_P[OF `\<not> x \<le> Float 1 -1`] if_not_P[OF False] if_P[OF True] 
-	  using `0 \<le> arctan (Ifloat x)` by auto
-      next
-	case False
-	hence "Ifloat ?invx \<le> 1" unfolding less_float_def by auto
-	have "0 \<le> Ifloat ?invx" by (rule order_trans[OF _ float_divr], auto simp add: `0 \<le> Ifloat x`)
-
-	have "1 / Ifloat x \<noteq> 0" and "0 < 1 / Ifloat x" using `0 < Ifloat x` by auto
-	
-	have "arctan (1 / Ifloat x) \<le> arctan (Ifloat ?invx)" unfolding Ifloat_1[symmetric] by (rule arctan_monotone', rule float_divr)
-	also have "\<dots> \<le> Ifloat (?ub_horner ?invx)" using arctan_0_1_bounds[OF `0 \<le> Ifloat ?invx` `Ifloat ?invx \<le> 1`] by auto
-	finally have "pi / 2 - Ifloat (?ub_horner ?invx) \<le> arctan (Ifloat x)" 
-	  using `0 \<le> arctan (Ifloat x)` arctan_inverse[OF `1 / Ifloat x \<noteq> 0`] 
-	  unfolding real_sgn_pos[OF `0 < 1 / Ifloat x`] le_diff_eq by auto
-	moreover
-	have "Ifloat (lb_pi prec * Float 1 -1) \<le> pi / 2" unfolding Ifloat_mult Float_num times_divide_eq_right real_mult_1 using pi_boundaries by auto
-	ultimately
-	show ?thesis unfolding lb_arctan.simps Let_def if_not_P[OF `\<not> x < 0`] if_not_P[OF `\<not> x \<le> Float 1 -1`] if_not_P[OF `\<not> x \<le> Float 1 1`] if_not_P[OF False]
-	  by auto
-      qed
-    qed
-  qed
-qed
-
-lemma ub_arctan_bound': assumes "0 \<le> Ifloat x"
-  shows "arctan (Ifloat x) \<le> Ifloat (ub_arctan prec x)"
-proof -
-  have "\<not> x < 0" and "0 \<le> x" unfolding less_float_def le_float_def using `0 \<le> Ifloat x` by auto
-
-  let "?ub_horner x" = "x * ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (x * x)"
-    and "?lb_horner x" = "x * lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (x * x)"
-
-  show ?thesis
-  proof (cases "x \<le> Float 1 -1")
-    case True hence "Ifloat x \<le> 1" unfolding le_float_def Float_num by auto
-    show ?thesis unfolding ub_arctan.simps Let_def if_not_P[OF `\<not> x < 0`] if_P[OF True]
-      using arctan_0_1_bounds[OF `0 \<le> Ifloat x` `Ifloat x \<le> 1`] by auto
-  next
-    case False hence "0 < Ifloat x" unfolding le_float_def Float_num by auto
-    let ?R = "1 + sqrt (1 + Ifloat x * Ifloat x)"
-    let ?fR = "1 + the (lb_sqrt prec (1 + x * x))"
-    let ?DIV = "float_divr prec x ?fR"
-    
-    have sqr_ge0: "0 \<le> 1 + Ifloat x * Ifloat x" using sum_power2_ge_zero[of 1 "Ifloat x", unfolded numeral_2_eq_2] by auto
-    hence "0 \<le> Ifloat (1 + x*x)" by auto
-    
-    hence divisor_gt0: "0 < ?R" by (auto intro: add_pos_nonneg)
-
-    have "Ifloat (the (lb_sqrt prec (1 + x * x))) \<le> sqrt (Ifloat (1 + x * x))" by (rule lb_sqrt_upper_bound, auto simp add: sqr_ge0)
-    hence "Ifloat ?fR \<le> ?R" by auto
-    have "0 < Ifloat ?fR" unfolding Ifloat_add Ifloat_1 by (rule order_less_le_trans[OF zero_less_one], auto simp add: lb_sqrt_lower_bound[OF `0 \<le> Ifloat (1 + x*x)`])
-
-    have monotone: "Ifloat x / ?R \<le> Ifloat (float_divr prec x ?fR)"
-    proof -
-      from divide_left_mono[OF `Ifloat ?fR \<le> ?R` `0 \<le> Ifloat x` mult_pos_pos[OF divisor_gt0 `0 < Ifloat ?fR`]]
-      have "Ifloat x / ?R \<le> Ifloat x / Ifloat ?fR" .
-      also have "\<dots> \<le> Ifloat ?DIV" by (rule float_divr)
-      finally show ?thesis .
-    qed
-
-    show ?thesis
-    proof (cases "x \<le> Float 1 1")
-      case True
-      show ?thesis
-      proof (cases "?DIV > 1")
-	case True
-	have "pi / 2 \<le> Ifloat (ub_pi prec * Float 1 -1)" unfolding Ifloat_mult Float_num times_divide_eq_right real_mult_1 using pi_boundaries by auto
-	from order_less_le_trans[OF arctan_ubound this, THEN less_imp_le]
-	show ?thesis unfolding ub_arctan.simps Let_def if_not_P[OF `\<not> x < 0`] if_not_P[OF `\<not> x \<le> Float 1 -1`] if_P[OF `x \<le> Float 1 1`] if_P[OF True] .
-      next
-	case False
-	hence "Ifloat ?DIV \<le> 1" unfolding less_float_def by auto
-      
-	have "0 \<le> Ifloat x / ?R" using `0 \<le> Ifloat x` `0 < ?R` unfolding real_0_le_divide_iff by auto
-	hence "0 \<le> Ifloat ?DIV" using monotone by (rule order_trans)
-
-	have "arctan (Ifloat x) = 2 * arctan (Ifloat x / ?R)" using arctan_half unfolding numeral_2_eq_2 power_Suc2 realpow_0 real_mult_1 .
-	also have "\<dots> \<le> 2 * arctan (Ifloat ?DIV)"
-	  using arctan_monotone'[OF monotone] by (auto intro!: mult_left_mono)
-	also have "\<dots> \<le> Ifloat (Float 1 1 * ?ub_horner ?DIV)" unfolding Ifloat_mult[of "Float 1 1"] Float_num
-	  using arctan_0_1_bounds[OF `0 \<le> Ifloat ?DIV` `Ifloat ?DIV \<le> 1`] by auto
-	finally show ?thesis unfolding ub_arctan.simps Let_def if_not_P[OF `\<not> x < 0`] if_not_P[OF `\<not> x \<le> Float 1 -1`] if_P[OF `x \<le> Float 1 1`] if_not_P[OF False] .
-      qed
-    next
-      case False
-      hence "2 < Ifloat x" unfolding le_float_def Float_num by auto
-      hence "1 \<le> Ifloat x" by auto
-      hence "0 < Ifloat x" by auto
-      hence "0 < x" unfolding less_float_def by auto
-
-      let "?invx" = "float_divl prec 1 x"
-      have "0 \<le> arctan (Ifloat x)" using arctan_monotone'[OF `0 \<le> Ifloat x`] using arctan_tan[of 0, unfolded tan_zero] by auto
-
-      have "Ifloat ?invx \<le> 1" unfolding less_float_def by (rule order_trans[OF float_divl], auto simp add: `1 \<le> Ifloat x` divide_le_eq_1_pos[OF `0 < Ifloat x`])
-      have "0 \<le> Ifloat ?invx" unfolding Ifloat_0[symmetric] by (rule float_divl_lower_bound[unfolded le_float_def], auto simp add: `0 < x`)
-	
-      have "1 / Ifloat x \<noteq> 0" and "0 < 1 / Ifloat x" using `0 < Ifloat x` by auto
-      
-      have "Ifloat (?lb_horner ?invx) \<le> arctan (Ifloat ?invx)" using arctan_0_1_bounds[OF `0 \<le> Ifloat ?invx` `Ifloat ?invx \<le> 1`] by auto
-      also have "\<dots> \<le> arctan (1 / Ifloat x)" unfolding Ifloat_1[symmetric] by (rule arctan_monotone', rule float_divl)
-      finally have "arctan (Ifloat x) \<le> pi / 2 - Ifloat (?lb_horner ?invx)"
-	using `0 \<le> arctan (Ifloat x)` arctan_inverse[OF `1 / Ifloat x \<noteq> 0`] 
-	unfolding real_sgn_pos[OF `0 < 1 / Ifloat x`] le_diff_eq by auto
-      moreover
-      have "pi / 2 \<le> Ifloat (ub_pi prec * Float 1 -1)" unfolding Ifloat_mult Float_num times_divide_eq_right mult_1_right using pi_boundaries by auto
-      ultimately
-      show ?thesis unfolding ub_arctan.simps Let_def if_not_P[OF `\<not> x < 0`] if_not_P[OF `\<not> x \<le> Float 1 -1`] if_not_P[OF `\<not> x \<le> Float 1 1`] if_not_P[OF False]
-	by auto
-    qed
-  qed
-qed
-
-lemma arctan_boundaries:
-  "arctan (Ifloat x) \<in> {Ifloat (lb_arctan prec x) .. Ifloat (ub_arctan prec x)}"
-proof (cases "0 \<le> x")
-  case True hence "0 \<le> Ifloat x" unfolding le_float_def by auto
-  show ?thesis using ub_arctan_bound'[OF `0 \<le> Ifloat x`] lb_arctan_bound'[OF `0 \<le> Ifloat x`] unfolding atLeastAtMost_iff by auto
-next
-  let ?mx = "-x"
-  case False hence "x < 0" and "0 \<le> Ifloat ?mx" unfolding le_float_def less_float_def by auto
-  hence bounds: "Ifloat (lb_arctan prec ?mx) \<le> arctan (Ifloat ?mx) \<and> arctan (Ifloat ?mx) \<le> Ifloat (ub_arctan prec ?mx)"
-    using ub_arctan_bound'[OF `0 \<le> Ifloat ?mx`] lb_arctan_bound'[OF `0 \<le> Ifloat ?mx`] by auto
-  show ?thesis unfolding Ifloat_minus arctan_minus lb_arctan.simps[where x=x] ub_arctan.simps[where x=x] Let_def if_P[OF `x < 0`]
-    unfolding atLeastAtMost_iff using bounds[unfolded Ifloat_minus arctan_minus] by auto
-qed
-
-lemma bnds_arctan: "\<forall> x lx ux. (l, u) = (lb_arctan prec lx, ub_arctan prec ux) \<and> x \<in> {Ifloat lx .. Ifloat ux} \<longrightarrow> Ifloat l \<le> arctan x \<and> arctan x \<le> Ifloat u"
-proof (rule allI, rule allI, rule allI, rule impI)
-  fix x lx ux
-  assume "(l, u) = (lb_arctan prec lx, ub_arctan prec ux) \<and> x \<in> {Ifloat lx .. Ifloat ux}"
-  hence l: "lb_arctan prec lx = l " and u: "ub_arctan prec ux = u" and x: "x \<in> {Ifloat lx .. Ifloat ux}" by auto
-
-  { from arctan_boundaries[of lx prec, unfolded l]
-    have "Ifloat l \<le> arctan (Ifloat lx)" by (auto simp del: lb_arctan.simps)
-    also have "\<dots> \<le> arctan x" using x by (auto intro: arctan_monotone')
-    finally have "Ifloat l \<le> arctan x" .
-  } moreover
-  { have "arctan x \<le> arctan (Ifloat ux)" using x by (auto intro: arctan_monotone')
-    also have "\<dots> \<le> Ifloat u" using arctan_boundaries[of ux prec, unfolded u] by (auto simp del: ub_arctan.simps)
-    finally have "arctan x \<le> Ifloat u" .
-  } ultimately show "Ifloat l \<le> arctan x \<and> arctan x \<le> Ifloat u" ..
-qed
-
-section "Sinus and Cosinus"
-
-subsection "Compute the cosinus and sinus series"
-
-fun ub_sin_cos_aux :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float"
-and lb_sin_cos_aux :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float" where
-  "ub_sin_cos_aux prec 0 i k x = 0"
-| "ub_sin_cos_aux prec (Suc n) i k x = 
-    (rapprox_rat prec 1 (int k)) - x * (lb_sin_cos_aux prec n (i + 2) (k * i * (i + 1)) x)"
-| "lb_sin_cos_aux prec 0 i k x = 0"
-| "lb_sin_cos_aux prec (Suc n) i k x = 
-    (lapprox_rat prec 1 (int k)) - x * (ub_sin_cos_aux prec n (i + 2) (k * i * (i + 1)) x)"
-
-lemma cos_aux:
-  shows "Ifloat (lb_sin_cos_aux prec n 1 1 (x * x)) \<le> (\<Sum> i=0..<n. -1^i * (1/real (fact (2 * i))) * (Ifloat x)^(2 * i))" (is "?lb")
-  and "(\<Sum> i=0..<n. -1^i * (1/real (fact (2 * i))) * (Ifloat x)^(2 * i)) \<le> Ifloat (ub_sin_cos_aux prec n 1 1 (x * x))" (is "?ub")
-proof -
-  have "0 \<le> Ifloat (x * x)" unfolding Ifloat_mult by auto
-  let "?f n" = "fact (2 * n)"
-
-  { fix n 
-    have F: "\<And>m. ((\<lambda>i. i + 2) ^ n) m = m + 2 * n" by (induct n arbitrary: m, auto)
-    have "?f (Suc n) = ?f n * ((\<lambda>i. i + 2) ^ n) 1 * (((\<lambda>i. i + 2) ^ n) 1 + 1)"
-      unfolding F by auto } note f_eq = this
-    
-  from horner_bounds[where lb="lb_sin_cos_aux prec" and ub="ub_sin_cos_aux prec" and j'=0, 
-    OF `0 \<le> Ifloat (x * x)` f_eq lb_sin_cos_aux.simps ub_sin_cos_aux.simps]
-  show "?lb" and "?ub" by (auto simp add: power_mult power2_eq_square[of "Ifloat x"])
-qed
-
-lemma cos_boundaries: assumes "0 \<le> Ifloat x" and "Ifloat x \<le> pi / 2"
-  shows "cos (Ifloat x) \<in> {Ifloat (lb_sin_cos_aux prec (get_even n) 1 1 (x * x)) .. Ifloat (ub_sin_cos_aux prec (get_odd n) 1 1 (x * x))}"
-proof (cases "Ifloat x = 0")
-  case False hence "Ifloat x \<noteq> 0" by auto
-  hence "0 < x" and "0 < Ifloat x" using `0 \<le> Ifloat x` unfolding less_float_def by auto
-  have "0 < x * x" using `0 < x` unfolding less_float_def Ifloat_mult Ifloat_0
-    using mult_pos_pos[where a="Ifloat x" and b="Ifloat x"] by auto
-
-  { fix x n have "(\<Sum> i=0..<n. -1^i * (1/real (fact (2 * i))) * x^(2 * i))
-    = (\<Sum> i = 0 ..< 2 * n. (if even(i) then (-1 ^ (i div 2))/(real (fact i)) else 0) * x ^ i)" (is "?sum = ?ifsum")
-  proof -
-    have "?sum = ?sum + (\<Sum> j = 0 ..< n. 0)" by auto
-    also have "\<dots> = 
-      (\<Sum> j = 0 ..< n. -1 ^ ((2 * j) div 2) / (real (fact (2 * j))) * x ^(2 * j)) + (\<Sum> j = 0 ..< n. 0)" by auto
-    also have "\<dots> = (\<Sum> i = 0 ..< 2 * n. if even i then -1 ^ (i div 2) / (real (fact i)) * x ^ i else 0)"
-      unfolding sum_split_even_odd ..
-    also have "\<dots> = (\<Sum> i = 0 ..< 2 * n. (if even i then -1 ^ (i div 2) / (real (fact i)) else 0) * x ^ i)"
-      by (rule setsum_cong2) auto
-    finally show ?thesis by assumption
-  qed } note morph_to_if_power = this
-
-
-  { fix n :: nat assume "0 < n"
-    hence "0 < 2 * n" by auto
-    obtain t where "0 < t" and "t < Ifloat x" and
-      cos_eq: "cos (Ifloat x) = (\<Sum> i = 0 ..< 2 * n. (if even(i) then (-1 ^ (i div 2))/(real (fact i)) else 0) * (Ifloat x) ^ i) 
-      + (cos (t + 1/2 * real (2 * n) * pi) / real (fact (2*n))) * (Ifloat x)^(2*n)" 
-      (is "_ = ?SUM + ?rest / ?fact * ?pow")
-      using Maclaurin_cos_expansion2[OF `0 < Ifloat x` `0 < 2 * n`] by auto
-
-    have "cos t * -1^n = cos t * cos (real n * pi) + sin t * sin (real n * pi)" by auto
-    also have "\<dots> = cos (t + real n * pi)"  using cos_add by auto
-    also have "\<dots> = ?rest" by auto
-    finally have "cos t * -1^n = ?rest" .
-    moreover
-    have "t \<le> pi / 2" using `t < Ifloat x` and `Ifloat x \<le> pi / 2` by auto
-    hence "0 \<le> cos t" using `0 < t` and cos_ge_zero by auto
-    ultimately have even: "even n \<Longrightarrow> 0 \<le> ?rest" and odd: "odd n \<Longrightarrow> 0 \<le> - ?rest " by auto
-
-    have "0 < ?fact" by auto
-    have "0 < ?pow" using `0 < Ifloat x` by auto
-
-    {
-      assume "even n"
-      have "Ifloat (lb_sin_cos_aux prec n 1 1 (x * x)) \<le> ?SUM"
-	unfolding morph_to_if_power[symmetric] using cos_aux by auto 
-      also have "\<dots> \<le> cos (Ifloat x)"
-      proof -
-	from even[OF `even n`] `0 < ?fact` `0 < ?pow`
-	have "0 \<le> (?rest / ?fact) * ?pow" by (metis mult_nonneg_nonneg divide_nonneg_pos less_imp_le)
-	thus ?thesis unfolding cos_eq by auto
-      qed
-      finally have "Ifloat (lb_sin_cos_aux prec n 1 1 (x * x)) \<le> cos (Ifloat x)" .
-    } note lb = this
-
-    {
-      assume "odd n"
-      have "cos (Ifloat x) \<le> ?SUM"
-      proof -
-	from `0 < ?fact` and `0 < ?pow` and odd[OF `odd n`]
-	have "0 \<le> (- ?rest) / ?fact * ?pow"
-	  by (metis mult_nonneg_nonneg divide_nonneg_pos less_imp_le)
-	thus ?thesis unfolding cos_eq by auto
-      qed
-      also have "\<dots> \<le> Ifloat (ub_sin_cos_aux prec n 1 1 (x * x))"
-	unfolding morph_to_if_power[symmetric] using cos_aux by auto
-      finally have "cos (Ifloat x) \<le> Ifloat (ub_sin_cos_aux prec n 1 1 (x * x))" .
-    } note ub = this and lb
-  } note ub = this(1) and lb = this(2)
-
-  have "cos (Ifloat x) \<le> Ifloat (ub_sin_cos_aux prec (get_odd n) 1 1 (x * x))" using ub[OF odd_pos[OF get_odd] get_odd] .
-  moreover have "Ifloat (lb_sin_cos_aux prec (get_even n) 1 1 (x * x)) \<le> cos (Ifloat x)" 
-  proof (cases "0 < get_even n")
-    case True show ?thesis using lb[OF True get_even] .
-  next
-    case False
-    hence "get_even n = 0" by auto
-    have "- (pi / 2) \<le> Ifloat x" by (rule order_trans[OF _ `0 < Ifloat x`[THEN less_imp_le]], auto)
-    with `Ifloat x \<le> pi / 2`
-    show ?thesis unfolding `get_even n = 0` lb_sin_cos_aux.simps Ifloat_minus Ifloat_0 using cos_ge_zero by auto
-  qed
-  ultimately show ?thesis by auto
-next
-  case True
-  show ?thesis
-  proof (cases "n = 0")
-    case True 
-    thus ?thesis unfolding `n = 0` get_even_def get_odd_def using `Ifloat x = 0` lapprox_rat[where x="-1" and y=1] by auto
-  next
-    case False with not0_implies_Suc obtain m where "n = Suc m" by blast
-    thus ?thesis unfolding `n = Suc m` get_even_def get_odd_def using `Ifloat x = 0` rapprox_rat[where x=1 and y=1] lapprox_rat[where x=1 and y=1] by (cases "even (Suc m)", auto)
-  qed
-qed
-
-lemma sin_aux: assumes "0 \<le> Ifloat x"
-  shows "Ifloat (x * lb_sin_cos_aux prec n 2 1 (x * x)) \<le> (\<Sum> i=0..<n. -1^i * (1/real (fact (2 * i + 1))) * (Ifloat x)^(2 * i + 1))" (is "?lb")
-  and "(\<Sum> i=0..<n. -1^i * (1/real (fact (2 * i + 1))) * (Ifloat x)^(2 * i + 1)) \<le> Ifloat (x * ub_sin_cos_aux prec n 2 1 (x * x))" (is "?ub")
-proof -
-  have "0 \<le> Ifloat (x * x)" unfolding Ifloat_mult by auto
-  let "?f n" = "fact (2 * n + 1)"
-
-  { fix n 
-    have F: "\<And>m. ((\<lambda>i. i + 2) ^ n) m = m + 2 * n" by (induct n arbitrary: m, auto)
-    have "?f (Suc n) = ?f n * ((\<lambda>i. i + 2) ^ n) 2 * (((\<lambda>i. i + 2) ^ n) 2 + 1)"
-      unfolding F by auto } note f_eq = this
-    
-  from horner_bounds[where lb="lb_sin_cos_aux prec" and ub="ub_sin_cos_aux prec" and j'=0,
-    OF `0 \<le> Ifloat (x * x)` f_eq lb_sin_cos_aux.simps ub_sin_cos_aux.simps]
-  show "?lb" and "?ub" using `0 \<le> Ifloat x` unfolding Ifloat_mult
-    unfolding power_add power_one_right real_mult_assoc[symmetric] setsum_left_distrib[symmetric]
-    unfolding real_mult_commute
-    by (auto intro!: mult_left_mono simp add: power_mult power2_eq_square[of "Ifloat x"])
-qed
-
-lemma sin_boundaries: assumes "0 \<le> Ifloat x" and "Ifloat x \<le> pi / 2"
-  shows "sin (Ifloat x) \<in> {Ifloat (x * lb_sin_cos_aux prec (get_even n) 2 1 (x * x)) .. Ifloat (x * ub_sin_cos_aux prec (get_odd n) 2 1 (x * x))}"
-proof (cases "Ifloat x = 0")
-  case False hence "Ifloat x \<noteq> 0" by auto
-  hence "0 < x" and "0 < Ifloat x" using `0 \<le> Ifloat x` unfolding less_float_def by auto
-  have "0 < x * x" using `0 < x` unfolding less_float_def Ifloat_mult Ifloat_0
-    using mult_pos_pos[where a="Ifloat x" and b="Ifloat x"] by auto
-
-  { fix x n have "(\<Sum> j = 0 ..< n. -1 ^ (((2 * j + 1) - Suc 0) div 2) / (real (fact (2 * j + 1))) * x ^(2 * j + 1))
-    = (\<Sum> i = 0 ..< 2 * n. (if even(i) then 0 else (-1 ^ ((i - Suc 0) div 2))/(real (fact i))) * x ^ i)" (is "?SUM = _")
-    proof -
-      have pow: "!!i. x ^ (2 * i + 1) = x * x ^ (2 * i)" by auto
-      have "?SUM = (\<Sum> j = 0 ..< n. 0) + ?SUM" by auto
-      also have "\<dots> = (\<Sum> i = 0 ..< 2 * n. if even i then 0 else -1 ^ ((i - Suc 0) div 2) / (real (fact i)) * x ^ i)"
-	unfolding sum_split_even_odd ..
-      also have "\<dots> = (\<Sum> i = 0 ..< 2 * n. (if even i then 0 else -1 ^ ((i - Suc 0) div 2) / (real (fact i))) * x ^ i)"
-	by (rule setsum_cong2) auto
-      finally show ?thesis by assumption
-    qed } note setsum_morph = this
-
-  { fix n :: nat assume "0 < n"
-    hence "0 < 2 * n + 1" by auto
-    obtain t where "0 < t" and "t < Ifloat x" and
-      sin_eq: "sin (Ifloat x) = (\<Sum> i = 0 ..< 2 * n + 1. (if even(i) then 0 else (-1 ^ ((i - Suc 0) div 2))/(real (fact i))) * (Ifloat x) ^ i) 
-      + (sin (t + 1/2 * real (2 * n + 1) * pi) / real (fact (2*n + 1))) * (Ifloat x)^(2*n + 1)" 
-      (is "_ = ?SUM + ?rest / ?fact * ?pow")
-      using Maclaurin_sin_expansion3[OF `0 < 2 * n + 1` `0 < Ifloat x`] by auto
-
-    have "?rest = cos t * -1^n" unfolding sin_add cos_add real_of_nat_add left_distrib right_distrib by auto
-    moreover
-    have "t \<le> pi / 2" using `t < Ifloat x` and `Ifloat x \<le> pi / 2` by auto
-    hence "0 \<le> cos t" using `0 < t` and cos_ge_zero by auto
-    ultimately have even: "even n \<Longrightarrow> 0 \<le> ?rest" and odd: "odd n \<Longrightarrow> 0 \<le> - ?rest " by auto
-
-    have "0 < ?fact" by (rule real_of_nat_fact_gt_zero)
-    have "0 < ?pow" using `0 < Ifloat x` by (rule zero_less_power)
-
-    {
-      assume "even n"
-      have "Ifloat (x * lb_sin_cos_aux prec n 2 1 (x * x)) \<le> 
-            (\<Sum> i = 0 ..< 2 * n. (if even(i) then 0 else (-1 ^ ((i - Suc 0) div 2))/(real (fact i))) * (Ifloat x) ^ i)"
-	using sin_aux[OF `0 \<le> Ifloat x`] unfolding setsum_morph[symmetric] by auto
-      also have "\<dots> \<le> ?SUM" by auto
-      also have "\<dots> \<le> sin (Ifloat x)"
-      proof -
-	from even[OF `even n`] `0 < ?fact` `0 < ?pow`
-	have "0 \<le> (?rest / ?fact) * ?pow" by (metis mult_nonneg_nonneg divide_nonneg_pos less_imp_le)
-	thus ?thesis unfolding sin_eq by auto
-      qed
-      finally have "Ifloat (x * lb_sin_cos_aux prec n 2 1 (x * x)) \<le> sin (Ifloat x)" .
-    } note lb = this
-
-    {
-      assume "odd n"
-      have "sin (Ifloat x) \<le> ?SUM"
-      proof -
-	from `0 < ?fact` and `0 < ?pow` and odd[OF `odd n`]
-	have "0 \<le> (- ?rest) / ?fact * ?pow"
-	  by (metis mult_nonneg_nonneg divide_nonneg_pos less_imp_le)
-	thus ?thesis unfolding sin_eq by auto
-      qed
-      also have "\<dots> \<le> (\<Sum> i = 0 ..< 2 * n. (if even(i) then 0 else (-1 ^ ((i - Suc 0) div 2))/(real (fact i))) * (Ifloat x) ^ i)"
-	 by auto
-      also have "\<dots> \<le> Ifloat (x * ub_sin_cos_aux prec n 2 1 (x * x))" 
-	using sin_aux[OF `0 \<le> Ifloat x`] unfolding setsum_morph[symmetric] by auto
-      finally have "sin (Ifloat x) \<le> Ifloat (x * ub_sin_cos_aux prec n 2 1 (x * x))" .
-    } note ub = this and lb
-  } note ub = this(1) and lb = this(2)
-
-  have "sin (Ifloat x) \<le> Ifloat (x * ub_sin_cos_aux prec (get_odd n) 2 1 (x * x))" using ub[OF odd_pos[OF get_odd] get_odd] .
-  moreover have "Ifloat (x * lb_sin_cos_aux prec (get_even n) 2 1 (x * x)) \<le> sin (Ifloat x)" 
-  proof (cases "0 < get_even n")
-    case True show ?thesis using lb[OF True get_even] .
-  next
-    case False
-    hence "get_even n = 0" by auto
-    with `Ifloat x \<le> pi / 2` `0 \<le> Ifloat x`
-    show ?thesis unfolding `get_even n = 0` ub_sin_cos_aux.simps Ifloat_minus Ifloat_0 using sin_ge_zero by auto
-  qed
-  ultimately show ?thesis by auto
-next
-  case True
-  show ?thesis
-  proof (cases "n = 0")
-    case True 
-    thus ?thesis unfolding `n = 0` get_even_def get_odd_def using `Ifloat x = 0` lapprox_rat[where x="-1" and y=1] by auto
-  next
-    case False with not0_implies_Suc obtain m where "n = Suc m" by blast
-    thus ?thesis unfolding `n = Suc m` get_even_def get_odd_def using `Ifloat x = 0` rapprox_rat[where x=1 and y=1] lapprox_rat[where x=1 and y=1] by (cases "even (Suc m)", auto)
-  qed
-qed
-
-subsection "Compute the cosinus in the entire domain"
-
-definition lb_cos :: "nat \<Rightarrow> float \<Rightarrow> float" where
-"lb_cos prec x = (let
-    horner = \<lambda> x. lb_sin_cos_aux prec (get_even (prec div 4 + 1)) 1 1 (x * x) ;
-    half = \<lambda> x. if x < 0 then - 1 else Float 1 1 * x * x - 1
-  in if x < Float 1 -1 then horner x
-else if x < 1          then half (horner (x * Float 1 -1))
-                       else half (half (horner (x * Float 1 -2))))"
-
-definition ub_cos :: "nat \<Rightarrow> float \<Rightarrow> float" where
-"ub_cos prec x = (let
-    horner = \<lambda> x. ub_sin_cos_aux prec (get_odd (prec div 4 + 1)) 1 1 (x * x) ;
-    half = \<lambda> x. Float 1 1 * x * x - 1
-  in if x < Float 1 -1 then horner x
-else if x < 1          then half (horner (x * Float 1 -1))
-                       else half (half (horner (x * Float 1 -2))))"
-
-definition bnds_cos :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float * float" where
-"bnds_cos prec lx ux = (let  lpi = lb_pi prec
-  in   if lx < -lpi \<or> ux > lpi   then (Float -1 0, Float 1 0)
-  else if ux \<le> 0                 then (lb_cos prec (-lx), ub_cos prec (-ux))
-  else if 0 \<le> lx                 then (lb_cos prec ux, ub_cos prec lx)
-                                 else (min (lb_cos prec (-lx)) (lb_cos prec ux), Float 1 0))"
-
-lemma lb_cos: assumes "0 \<le> Ifloat x" and "Ifloat x \<le> pi" 
-  shows "cos (Ifloat x) \<in> {Ifloat (lb_cos prec x) .. Ifloat (ub_cos prec x)}" (is "?cos x \<in> { Ifloat (?lb x) .. Ifloat (?ub x) }")
-proof -
-  { fix x :: real
-    have "cos x = cos (x / 2 + x / 2)" by auto
-    also have "\<dots> = cos (x / 2) * cos (x / 2) + sin (x / 2) * sin (x / 2) - sin (x / 2) * sin (x / 2) + cos (x / 2) * cos (x / 2) - 1"
-      unfolding cos_add by auto
-    also have "\<dots> = 2 * cos (x / 2) * cos (x / 2) - 1" by algebra
-    finally have "cos x = 2 * cos (x / 2) * cos (x / 2) - 1" .
-  } note x_half = this[symmetric]
-
-  have "\<not> x < 0" using `0 \<le> Ifloat x` unfolding less_float_def by auto
-  let "?ub_horner x" = "ub_sin_cos_aux prec (get_odd (prec div 4 + 1)) 1 1 (x * x)"
-  let "?lb_horner x" = "lb_sin_cos_aux prec (get_even (prec div 4 + 1)) 1 1 (x * x)"
-  let "?ub_half x" = "Float 1 1 * x * x - 1"
-  let "?lb_half x" = "if x < 0 then - 1 else Float 1 1 * x * x - 1"
-
-  show ?thesis
-  proof (cases "x < Float 1 -1")
-    case True hence "Ifloat x \<le> pi / 2" unfolding less_float_def using pi_ge_two by auto
-    show ?thesis unfolding lb_cos_def[where x=x] ub_cos_def[where x=x] if_not_P[OF `\<not> x < 0`] if_P[OF `x < Float 1 -1`] Let_def
-      using cos_boundaries[OF `0 \<le> Ifloat x` `Ifloat x \<le> pi / 2`] .
-  next
-    case False
-    
-    { fix y x :: float let ?x2 = "Ifloat (x * Float 1 -1)"
-      assume "Ifloat y \<le> cos ?x2" and "-pi \<le> Ifloat x" and "Ifloat x \<le> pi"
-      hence "- (pi / 2) \<le> ?x2" and "?x2 \<le> pi / 2" using pi_ge_two unfolding Ifloat_mult Float_num by auto
-      hence "0 \<le> cos ?x2" by (rule cos_ge_zero)
-      
-      have "Ifloat (?lb_half y) \<le> cos (Ifloat x)"
-      proof (cases "y < 0")
-	case True show ?thesis using cos_ge_minus_one unfolding if_P[OF True] by auto
-      next
-	case False
-	hence "0 \<le> Ifloat y" unfolding less_float_def by auto
-	from mult_mono[OF `Ifloat y \<le> cos ?x2` `Ifloat y \<le> cos ?x2` `0 \<le> cos ?x2` this]
-	have "Ifloat y * Ifloat y \<le> cos ?x2 * cos ?x2" .
-	hence "2 * Ifloat y * Ifloat y \<le> 2 * cos ?x2 * cos ?x2" by auto
-	hence "2 * Ifloat y * Ifloat y - 1 \<le> 2 * cos (Ifloat x / 2) * cos (Ifloat x / 2) - 1" unfolding Float_num Ifloat_mult by auto
-	thus ?thesis unfolding if_not_P[OF False] x_half Float_num Ifloat_mult Ifloat_sub by auto
-      qed
-    } note lb_half = this
-    
-    { fix y x :: float let ?x2 = "Ifloat (x * Float 1 -1)"
-      assume ub: "cos ?x2 \<le> Ifloat y" and "- pi \<le> Ifloat x" and "Ifloat x \<le> pi"
-      hence "- (pi / 2) \<le> ?x2" and "?x2 \<le> pi / 2" using pi_ge_two unfolding Ifloat_mult Float_num by auto
-      hence "0 \<le> cos ?x2" by (rule cos_ge_zero)
-      
-      have "cos (Ifloat x) \<le> Ifloat (?ub_half y)"
-      proof -
-	have "0 \<le> Ifloat y" using `0 \<le> cos ?x2` ub by (rule order_trans)
-	from mult_mono[OF ub ub this `0 \<le> cos ?x2`]
-	have "cos ?x2 * cos ?x2 \<le> Ifloat y * Ifloat y" .
-	hence "2 * cos ?x2 * cos ?x2 \<le> 2 * Ifloat y * Ifloat y" by auto
-	hence "2 * cos (Ifloat x / 2) * cos (Ifloat x / 2) - 1 \<le> 2 * Ifloat y * Ifloat y - 1" unfolding Float_num Ifloat_mult by auto
-	thus ?thesis unfolding x_half Ifloat_mult Float_num Ifloat_sub by auto
-      qed
-    } note ub_half = this
-    
-    let ?x2 = "x * Float 1 -1"
-    let ?x4 = "x * Float 1 -1 * Float 1 -1"
-    
-    have "-pi \<le> Ifloat x" using pi_ge_zero[THEN le_imp_neg_le, unfolded minus_zero] `0 \<le> Ifloat x` by (rule order_trans)
-    
-    show ?thesis
-    proof (cases "x < 1")
-      case True hence "Ifloat x \<le> 1" unfolding less_float_def by auto
-      have "0 \<le> Ifloat ?x2" and "Ifloat ?x2 \<le> pi / 2" using pi_ge_two `0 \<le> Ifloat x` unfolding Ifloat_mult Float_num using assms by auto
-      from cos_boundaries[OF this]
-      have lb: "Ifloat (?lb_horner ?x2) \<le> ?cos ?x2" and ub: "?cos ?x2 \<le> Ifloat (?ub_horner ?x2)" by auto
-      
-      have "Ifloat (?lb x) \<le> ?cos x"
-      proof -
-	from lb_half[OF lb `-pi \<le> Ifloat x` `Ifloat x \<le> pi`]
-	show ?thesis unfolding lb_cos_def[where x=x] Let_def using `\<not> x < 0` `\<not> x < Float 1 -1` `x < 1` by auto
-      qed
-      moreover have "?cos x \<le> Ifloat (?ub x)"
-      proof -
-	from ub_half[OF ub `-pi \<le> Ifloat x` `Ifloat x \<le> pi`]
-	show ?thesis unfolding ub_cos_def[where x=x] Let_def using `\<not> x < 0` `\<not> x < Float 1 -1` `x < 1` by auto 
-      qed
-      ultimately show ?thesis by auto
-    next
-      case False
-      have "0 \<le> Ifloat ?x4" and "Ifloat ?x4 \<le> pi / 2" using pi_ge_two `0 \<le> Ifloat x` `Ifloat x \<le> pi` unfolding Ifloat_mult Float_num by auto
-      from cos_boundaries[OF this]
-      have lb: "Ifloat (?lb_horner ?x4) \<le> ?cos ?x4" and ub: "?cos ?x4 \<le> Ifloat (?ub_horner ?x4)" by auto
-      
-      have eq_4: "?x2 * Float 1 -1 = x * Float 1 -2" by (cases x, auto simp add: times_float.simps)
-      
-      have "Ifloat (?lb x) \<le> ?cos x"
-      proof -
-	have "-pi \<le> Ifloat ?x2" and "Ifloat ?x2 \<le> pi" unfolding Ifloat_mult Float_num using pi_ge_two `0 \<le> Ifloat x` `Ifloat x \<le> pi` by auto
-	from lb_half[OF lb_half[OF lb this] `-pi \<le> Ifloat x` `Ifloat x \<le> pi`, unfolded eq_4]
-	show ?thesis unfolding lb_cos_def[where x=x] if_not_P[OF `\<not> x < 0`] if_not_P[OF `\<not> x < Float 1 -1`] if_not_P[OF `\<not> x < 1`] Let_def .
-      qed
-      moreover have "?cos x \<le> Ifloat (?ub x)"
-      proof -
-	have "-pi \<le> Ifloat ?x2" and "Ifloat ?x2 \<le> pi" unfolding Ifloat_mult Float_num using pi_ge_two `0 \<le> Ifloat x` `Ifloat x \<le> pi` by auto
-	from ub_half[OF ub_half[OF ub this] `-pi \<le> Ifloat x` `Ifloat x \<le> pi`, unfolded eq_4]
-	show ?thesis unfolding ub_cos_def[where x=x] if_not_P[OF `\<not> x < 0`] if_not_P[OF `\<not> x < Float 1 -1`] if_not_P[OF `\<not> x < 1`] Let_def .
-      qed
-      ultimately show ?thesis by auto
-    qed
-  qed
-qed
-
-lemma lb_cos_minus: assumes "-pi \<le> Ifloat x" and "Ifloat x \<le> 0" 
-  shows "cos (Ifloat (-x)) \<in> {Ifloat (lb_cos prec (-x)) .. Ifloat (ub_cos prec (-x))}"
-proof -
-  have "0 \<le> Ifloat (-x)" and "Ifloat (-x) \<le> pi" using `-pi \<le> Ifloat x` `Ifloat x \<le> 0` by auto
-  from lb_cos[OF this] show ?thesis .
-qed
-
-lemma bnds_cos: "\<forall> x lx ux. (l, u) = bnds_cos prec lx ux \<and> x \<in> {Ifloat lx .. Ifloat ux} \<longrightarrow> Ifloat l \<le> cos x \<and> cos x \<le> Ifloat u"
-proof (rule allI, rule allI, rule allI, rule impI)
-  fix x lx ux
-  assume "(l, u) = bnds_cos prec lx ux \<and> x \<in> {Ifloat lx .. Ifloat ux}"
-  hence bnds: "(l, u) = bnds_cos prec lx ux" and x: "x \<in> {Ifloat lx .. Ifloat ux}" by auto
-
-  let ?lpi = "lb_pi prec"  
-  have [intro!]: "Ifloat lx \<le> Ifloat ux" using x by auto
-  hence "lx \<le> ux" unfolding le_float_def .
-
-  show "Ifloat l \<le> cos x \<and> cos x \<le> Ifloat u"
-  proof (cases "lx < -?lpi \<or> ux > ?lpi")
-    case True
-    show ?thesis using bnds unfolding bnds_cos_def if_P[OF True] Let_def using cos_le_one cos_ge_minus_one by auto
-  next
-    case False note not_out = this
-    hence lpi_lx: "- Ifloat ?lpi \<le> Ifloat lx" and lpi_ux: "Ifloat ux \<le> Ifloat ?lpi" unfolding le_float_def less_float_def by auto
-
-    from pi_boundaries[unfolded atLeastAtMost_iff, THEN conjunct1, THEN le_imp_neg_le] lpi_lx
-    have "- pi \<le> Ifloat lx" by (rule order_trans)
-    hence "- pi \<le> x" and "- pi \<le> Ifloat ux" and "x \<le> Ifloat ux" using x by auto
-    
-    from lpi_ux pi_boundaries[unfolded atLeastAtMost_iff, THEN conjunct1]
-    have "Ifloat ux \<le> pi" by (rule order_trans)
-    hence "x \<le> pi" and "Ifloat lx \<le> pi" and "Ifloat lx \<le> x" using x by auto
-
-    note lb_cos_minus_bottom = lb_cos_minus[unfolded atLeastAtMost_iff, THEN conjunct1]
-    note lb_cos_minus_top = lb_cos_minus[unfolded atLeastAtMost_iff, THEN conjunct2]
-    note lb_cos_bottom = lb_cos[unfolded atLeastAtMost_iff, THEN conjunct1]
-    note lb_cos_top = lb_cos[unfolded atLeastAtMost_iff, THEN conjunct2]
-
-    show ?thesis
-    proof (cases "ux \<le> 0")
-      case True hence "Ifloat ux \<le> 0" unfolding le_float_def by auto
-      hence "x \<le> 0" and "Ifloat lx \<le> 0" using x by auto
-      
-      { have "Ifloat (lb_cos prec (-lx)) \<le> cos (Ifloat (-lx))" using lb_cos_minus_bottom[OF `-pi \<le> Ifloat lx` `Ifloat lx \<le> 0`] .
-	also have "\<dots> \<le> cos x" unfolding Ifloat_minus cos_minus using cos_monotone_minus_pi_0'[OF `- pi \<le> Ifloat lx` `Ifloat lx \<le> x` `x \<le> 0`] .
-	finally have "Ifloat (lb_cos prec (-lx)) \<le> cos x" . }
-      moreover
-      { have "cos x \<le> cos (Ifloat (-ux))" unfolding Ifloat_minus cos_minus using cos_monotone_minus_pi_0'[OF `- pi \<le> x` `x \<le> Ifloat ux` `Ifloat ux \<le> 0`] .
-	also have "\<dots> \<le> Ifloat (ub_cos prec (-ux))" using lb_cos_minus_top[OF `-pi \<le> Ifloat ux` `Ifloat ux \<le> 0`] .
-	finally have "cos x \<le> Ifloat (ub_cos prec (-ux))" . }
-      ultimately show ?thesis using bnds unfolding bnds_cos_def Let_def if_not_P[OF not_out] if_P[OF True] by auto
-    next
-      case False note not_ux = this
-      
-      show ?thesis
-      proof (cases "0 \<le> lx")
-	case True hence "0 \<le> Ifloat lx" unfolding le_float_def by auto
-	hence "0 \<le> x" and "0 \<le> Ifloat ux" using x by auto
-      
-	{ have "Ifloat (lb_cos prec ux) \<le> cos (Ifloat ux)" using lb_cos_bottom[OF `0 \<le> Ifloat ux` `Ifloat ux \<le> pi`] .
-	  also have "\<dots> \<le> cos x" using cos_monotone_0_pi'[OF `0 \<le> x` `x \<le> Ifloat ux` `Ifloat ux \<le> pi`] .
-	  finally have "Ifloat (lb_cos prec ux) \<le> cos x" . }
-	moreover
-	{ have "cos x \<le> cos (Ifloat lx)" using cos_monotone_0_pi'[OF `0 \<le> Ifloat lx` `Ifloat lx \<le> x` `x \<le> pi`] .
-	  also have "\<dots> \<le> Ifloat (ub_cos prec lx)" using lb_cos_top[OF `0 \<le> Ifloat lx` `Ifloat lx \<le> pi`] .
-	  finally have "cos x \<le> Ifloat (ub_cos prec lx)" . }
-	ultimately show ?thesis using bnds unfolding bnds_cos_def Let_def if_not_P[OF not_out] if_not_P[OF not_ux] if_P[OF True] by auto
-      next
-	case False with not_ux
-	have "Ifloat lx \<le> 0" and "0 \<le> Ifloat ux" unfolding le_float_def by auto
-
-	have "Ifloat (min (lb_cos prec (-lx)) (lb_cos prec ux)) \<le> cos x"
-	proof (cases "x \<le> 0")
-	  case True
-	  have "Ifloat (lb_cos prec (-lx)) \<le> cos (Ifloat (-lx))" using lb_cos_minus_bottom[OF `-pi \<le> Ifloat lx` `Ifloat lx \<le> 0`] .
-	  also have "\<dots> \<le> cos x" unfolding Ifloat_minus cos_minus using cos_monotone_minus_pi_0'[OF `- pi \<le> Ifloat lx` `Ifloat lx \<le> x` `x \<le> 0`] .
-	  finally show ?thesis unfolding Ifloat_min by auto
-	next
-	  case False hence "0 \<le> x" by auto
-	  have "Ifloat (lb_cos prec ux) \<le> cos (Ifloat ux)" using lb_cos_bottom[OF `0 \<le> Ifloat ux` `Ifloat ux \<le> pi`] .
-	  also have "\<dots> \<le> cos x" using cos_monotone_0_pi'[OF `0 \<le> x` `x \<le> Ifloat ux` `Ifloat ux \<le> pi`] .
-	  finally show ?thesis unfolding Ifloat_min by auto
-	qed
-	moreover have "cos x \<le> Ifloat (Float 1 0)" by auto
-	ultimately show ?thesis using bnds unfolding bnds_cos_def Let_def if_not_P[OF not_out] if_not_P[OF not_ux] if_not_P[OF False] by auto
-      qed
-    qed
-  qed
-qed
-
-subsection "Compute the sinus in the entire domain"
-
-function lb_sin :: "nat \<Rightarrow> float \<Rightarrow> float" and ub_sin :: "nat \<Rightarrow> float \<Rightarrow> float" where
-"lb_sin prec x = (let sqr_diff = \<lambda> x. if x > 1 then 0 else 1 - x * x 
-  in if x < 0           then - ub_sin prec (- x)
-else if x \<le> Float 1 -1  then x * lb_sin_cos_aux prec (get_even (prec div 4 + 1)) 2 1 (x * x)
-                        else the (lb_sqrt prec (sqr_diff (ub_cos prec x))))" |
-
-"ub_sin prec x = (let sqr_diff = \<lambda> x. if x < 0 then 1 else 1 - x * x
-  in if x < 0           then - lb_sin prec (- x)
-else if x \<le> Float 1 -1  then x * ub_sin_cos_aux prec (get_odd (prec div 4 + 1)) 2 1 (x * x)
-                        else the (ub_sqrt prec (sqr_diff (lb_cos prec x))))"
-by pat_completeness auto
-termination by (relation "measure (\<lambda> v. let (prec, x) = sum_case id id v in (if x < 0 then 1 else 0))", auto simp add: less_float_def)
-
-definition bnds_sin :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float * float" where
-"bnds_sin prec lx ux = (let 
-    lpi = lb_pi prec ;
-    half_pi = lpi * Float 1 -1
-  in if lx \<le> - half_pi \<or> half_pi \<le> ux then (Float -1 0, Float 1 0)
-                                       else (lb_sin prec lx, ub_sin prec ux))"
-
-lemma lb_sin: assumes "- (pi / 2) \<le> Ifloat x" and "Ifloat x \<le> pi / 2"
-  shows "sin (Ifloat x) \<in> { Ifloat (lb_sin prec x) .. Ifloat (ub_sin prec x) }" (is "?sin x \<in> { ?lb x .. ?ub x}")
-proof -
-  { fix x :: float assume "0 \<le> Ifloat x" and "Ifloat x \<le> pi / 2"
-    hence "\<not> (x < 0)" and "- (pi / 2) \<le> Ifloat x" unfolding less_float_def using pi_ge_two by auto
-
-    have "Ifloat x \<le> pi" using `Ifloat x \<le> pi / 2` using pi_ge_two by auto
-
-    have "?sin x \<in> { ?lb x .. ?ub x}"
-    proof (cases "x \<le> Float 1 -1")
-      case True from sin_boundaries[OF `0 \<le> Ifloat x` `Ifloat x \<le> pi / 2`]
-      show ?thesis unfolding lb_sin.simps[of prec x] ub_sin.simps[of prec x] if_not_P[OF `\<not> (x < 0)`] if_P[OF True] Let_def .
-    next
-      case False
-      have "0 \<le> cos (Ifloat x)" using cos_ge_zero[OF _ `Ifloat x \<le> pi /2`] `0 \<le> Ifloat x` pi_ge_two by auto
-      have "0 \<le> sin (Ifloat x)" using `0 \<le> Ifloat x` and `Ifloat x \<le> pi / 2` using sin_ge_zero by auto
-      
-      have "?sin x \<le> ?ub x"
-      proof (cases "lb_cos prec x < 0")
-	case True
-	have "?sin x \<le> 1" using sin_le_one .
-	also have "\<dots> \<le> Ifloat (the (ub_sqrt prec 1))" using ub_sqrt_lower_bound[where prec=prec and x=1] unfolding Ifloat_1 by auto
-	finally show ?thesis unfolding ub_sin.simps if_not_P[OF `\<not> (x < 0)`] if_not_P[OF `\<not> x \<le> Float 1 -1`] if_P[OF True] Let_def .
-      next
-	case False hence "0 \<le> Ifloat (lb_cos prec x)" unfolding less_float_def by auto
-	
-	have "sin (Ifloat x) = sqrt (1 - cos (Ifloat x) ^ 2)" unfolding sin_squared_eq[symmetric] real_sqrt_abs using `0 \<le> sin (Ifloat x)` by auto
-	also have "\<dots> \<le> sqrt (Ifloat (1 - lb_cos prec x * lb_cos prec x))" 
-	proof (rule real_sqrt_le_mono)
-	  have "Ifloat (lb_cos prec x * lb_cos prec x) \<le> cos (Ifloat x) ^ 2" unfolding numeral_2_eq_2 power_Suc2 realpow_0 Ifloat_mult
-	    using `0 \<le> Ifloat (lb_cos prec x)` lb_cos[OF `0 \<le> Ifloat x` `Ifloat x \<le> pi`] `0 \<le> cos (Ifloat x)` by(auto intro!: mult_mono)
-	  thus "1 - cos (Ifloat x) ^ 2 \<le> Ifloat (1 - lb_cos prec x * lb_cos prec x)" unfolding Ifloat_sub Ifloat_1 by auto
-	qed
-	also have "\<dots> \<le> Ifloat (the (ub_sqrt prec (1 - lb_cos prec x * lb_cos prec x)))"
-	proof (rule ub_sqrt_lower_bound)
-	  have "Ifloat (lb_cos prec x) \<le> cos (Ifloat x)" using lb_cos[OF `0 \<le> Ifloat x` `Ifloat x \<le> pi`] by auto
-	  from mult_mono[OF order_trans[OF this cos_le_one] order_trans[OF this cos_le_one]]
-	  have "Ifloat (lb_cos prec x) * Ifloat (lb_cos prec x) \<le> 1" using `0 \<le> Ifloat (lb_cos prec x)` by auto
-	  thus "0 \<le> Ifloat (1 - lb_cos prec x * lb_cos prec x)" by auto
-	qed
-	finally show ?thesis unfolding ub_sin.simps if_not_P[OF `\<not> (x < 0)`] if_not_P[OF `\<not> x \<le> Float 1 -1`] if_not_P[OF False] Let_def .
-      qed
-      moreover
-      have "?lb x \<le> ?sin x"
-      proof (cases "1 < ub_cos prec x")
-	case True
-	show ?thesis unfolding lb_sin.simps if_not_P[OF `\<not> (x < 0)`] if_not_P[OF `\<not> x \<le> Float 1 -1`] if_P[OF True] Let_def 
-	  by (rule order_trans[OF _ sin_ge_zero[OF `0 \<le> Ifloat x` `Ifloat x \<le> pi`]]) 
-        (auto simp add: lb_sqrt_upper_bound[where prec=prec and x=0, unfolded Ifloat_0 real_sqrt_zero])
-      next
-	case False hence "Ifloat (ub_cos prec x) \<le> 1" unfolding less_float_def by auto
-	have "0 \<le> Ifloat (ub_cos prec x)" using order_trans[OF `0 \<le> cos (Ifloat x)`] lb_cos `0 \<le> Ifloat x` `Ifloat x \<le> pi` by auto
-	
-	have "Ifloat (the (lb_sqrt prec (1 - ub_cos prec x * ub_cos prec x))) \<le> sqrt (Ifloat (1 - ub_cos prec x * ub_cos prec x))"
-	proof (rule lb_sqrt_upper_bound)
-	  from mult_mono[OF `Ifloat (ub_cos prec x) \<le> 1` `Ifloat (ub_cos prec x) \<le> 1`] `0 \<le> Ifloat (ub_cos prec x)`
-	  have "Ifloat (ub_cos prec x) * Ifloat (ub_cos prec x) \<le> 1" by auto
-	  thus "0 \<le> Ifloat (1 - ub_cos prec x * ub_cos prec x)" by auto
-	qed
-	also have "\<dots> \<le> sqrt (1 - cos (Ifloat x) ^ 2)"
-	proof (rule real_sqrt_le_mono)
-	  have "cos (Ifloat x) ^ 2 \<le> Ifloat (ub_cos prec x * ub_cos prec x)" unfolding numeral_2_eq_2 power_Suc2 realpow_0 Ifloat_mult
-	    using `0 \<le> Ifloat (ub_cos prec x)` lb_cos[OF `0 \<le> Ifloat x` `Ifloat x \<le> pi`] `0 \<le> cos (Ifloat x)` by(auto intro!: mult_mono)
-	  thus "Ifloat (1 - ub_cos prec x * ub_cos prec x) \<le> 1 - cos (Ifloat x) ^ 2" unfolding Ifloat_sub Ifloat_1 by auto
-	qed
-	also have "\<dots> = sin (Ifloat x)" unfolding sin_squared_eq[symmetric] real_sqrt_abs using `0 \<le> sin (Ifloat x)` by auto
-	finally show ?thesis unfolding lb_sin.simps if_not_P[OF `\<not> (x < 0)`] if_not_P[OF `\<not> x \<le> Float 1 -1`] if_not_P[OF False] Let_def .
-      qed
-      ultimately show ?thesis by auto
-    qed
-  } note for_pos = this
-
-  show ?thesis
-  proof (cases "x < 0")
-    case True 
-    hence "0 \<le> Ifloat (-x)" and "Ifloat (- x) \<le> pi / 2" using `-(pi/2) \<le> Ifloat x` unfolding less_float_def by auto
-    from for_pos[OF this]
-    show ?thesis unfolding Ifloat_minus sin_minus lb_sin.simps[of prec x] ub_sin.simps[of prec x] if_P[OF True] Let_def atLeastAtMost_iff by auto
-  next
-    case False hence "0 \<le> Ifloat x" unfolding less_float_def by auto
-    from for_pos[OF this `Ifloat x \<le> pi /2`]
-    show ?thesis .
-  qed
-qed
-
-lemma bnds_sin: "\<forall> x lx ux. (l, u) = bnds_sin prec lx ux \<and> x \<in> {Ifloat lx .. Ifloat ux} \<longrightarrow> Ifloat l \<le> sin x \<and> sin x \<le> Ifloat u"
-proof (rule allI, rule allI, rule allI, rule impI)
-  fix x lx ux
-  assume "(l, u) = bnds_sin prec lx ux \<and> x \<in> {Ifloat lx .. Ifloat ux}"
-  hence bnds: "(l, u) = bnds_sin prec lx ux" and x: "x \<in> {Ifloat lx .. Ifloat ux}" by auto
-  show "Ifloat l \<le> sin x \<and> sin x \<le> Ifloat u"
-  proof (cases "lx \<le> - (lb_pi prec * Float 1 -1) \<or> lb_pi prec * Float 1 -1 \<le> ux")
-    case True show ?thesis using bnds unfolding bnds_sin_def if_P[OF True] Let_def by auto
-  next
-    case False
-    hence "- lb_pi prec * Float 1 -1 \<le> lx" and "ux \<le> lb_pi prec * Float 1 -1" unfolding le_float_def by auto
-    moreover have "Ifloat (lb_pi prec * Float 1 -1) \<le> pi / 2" unfolding Ifloat_mult using pi_boundaries by auto
-    ultimately have "- (pi / 2) \<le> Ifloat lx" and "Ifloat ux \<le> pi / 2" and "Ifloat lx \<le> Ifloat ux" unfolding le_float_def using x by auto
-    hence "- (pi / 2) \<le> Ifloat ux" and "Ifloat lx \<le> pi / 2" by auto
-    
-    have "- (pi / 2) \<le> x""x \<le> pi / 2" using `Ifloat ux \<le> pi / 2` `- (pi /2) \<le> Ifloat lx` x by auto
-    
-    { have "Ifloat (lb_sin prec lx) \<le> sin (Ifloat lx)" using lb_sin[OF `- (pi / 2) \<le> Ifloat lx` `Ifloat lx \<le> pi / 2`] unfolding atLeastAtMost_iff by auto
-      also have "\<dots> \<le> sin x" using sin_monotone_2pi' `- (pi / 2) \<le> Ifloat lx` x `x \<le> pi / 2` by auto
-      finally have "Ifloat (lb_sin prec lx) \<le> sin x" . }
-    moreover
-    { have "sin x \<le> sin (Ifloat ux)" using sin_monotone_2pi' `- (pi / 2) \<le> x` x `Ifloat ux \<le> pi / 2` by auto
-      also have "\<dots> \<le> Ifloat (ub_sin prec ux)" using lb_sin[OF `- (pi / 2) \<le> Ifloat ux` `Ifloat ux \<le> pi / 2`] unfolding atLeastAtMost_iff by auto
-      finally have "sin x \<le> Ifloat (ub_sin prec ux)" . }
-    ultimately
-    show ?thesis using bnds unfolding bnds_sin_def if_not_P[OF False] Let_def by auto
-  qed
-qed
-
-section "Exponential function"
-
-subsection "Compute the series of the exponential function"
-
-fun ub_exp_horner :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float" and lb_exp_horner :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float" where
-"ub_exp_horner prec 0 i k x       = 0" |
-"ub_exp_horner prec (Suc n) i k x = rapprox_rat prec 1 (int k) + x * lb_exp_horner prec n (i + 1) (k * i) x" |
-"lb_exp_horner prec 0 i k x       = 0" |
-"lb_exp_horner prec (Suc n) i k x = lapprox_rat prec 1 (int k) + x * ub_exp_horner prec n (i + 1) (k * i) x"
-
-lemma bnds_exp_horner: assumes "Ifloat x \<le> 0"
-  shows "exp (Ifloat x) \<in> { Ifloat (lb_exp_horner prec (get_even n) 1 1 x) .. Ifloat (ub_exp_horner prec (get_odd n) 1 1 x) }"
-proof -
-  { fix n
-    have F: "\<And> m. ((\<lambda>i. i + 1) ^ n) m = n + m" by (induct n, auto)
-    have "fact (Suc n) = fact n * ((\<lambda>i. i + 1) ^ n) 1" unfolding F by auto } note f_eq = this
-    
-  note bounds = horner_bounds_nonpos[where f="fact" and lb="lb_exp_horner prec" and ub="ub_exp_horner prec" and j'=0 and s=1,
-    OF assms f_eq lb_exp_horner.simps ub_exp_horner.simps]
-
-  { have "Ifloat (lb_exp_horner prec (get_even n) 1 1 x) \<le> (\<Sum>j = 0..<get_even n. 1 / real (fact j) * Ifloat x ^ j)"
-      using bounds(1) by auto
-    also have "\<dots> \<le> exp (Ifloat x)"
-    proof -
-      obtain t where "\<bar>t\<bar> \<le> \<bar>Ifloat x\<bar>" and "exp (Ifloat x) = (\<Sum>m = 0..<get_even n. (Ifloat x) ^ m / real (fact m)) + exp t / real (fact (get_even n)) * (Ifloat x) ^ (get_even n)"
-	using Maclaurin_exp_le by blast
-      moreover have "0 \<le> exp t / real (fact (get_even n)) * (Ifloat x) ^ (get_even n)"
-	by (auto intro!: mult_nonneg_nonneg divide_nonneg_pos simp add: get_even zero_le_even_power exp_gt_zero)
-      ultimately show ?thesis
-	using get_odd exp_gt_zero by (auto intro!: pordered_cancel_semiring_class.mult_nonneg_nonneg)
-    qed
-    finally have "Ifloat (lb_exp_horner prec (get_even n) 1 1 x) \<le> exp (Ifloat x)" .
-  } moreover
-  { 
-    have x_less_zero: "Ifloat x ^ get_odd n \<le> 0"
-    proof (cases "Ifloat x = 0")
-      case True
-      have "(get_odd n) \<noteq> 0" using get_odd[THEN odd_pos] by auto
-      thus ?thesis unfolding True power_0_left by auto
-    next
-      case False hence "Ifloat x < 0" using `Ifloat x \<le> 0` by auto
-      show ?thesis by (rule less_imp_le, auto simp add: power_less_zero_eq get_odd `Ifloat x < 0`)
-    qed
-
-    obtain t where "\<bar>t\<bar> \<le> \<bar>Ifloat x\<bar>" and "exp (Ifloat x) = (\<Sum>m = 0..<get_odd n. (Ifloat x) ^ m / real (fact m)) + exp t / real (fact (get_odd n)) * (Ifloat x) ^ (get_odd n)"
-      using Maclaurin_exp_le by blast
-    moreover have "exp t / real (fact (get_odd n)) * (Ifloat x) ^ (get_odd n) \<le> 0"
-      by (auto intro!: mult_nonneg_nonpos divide_nonpos_pos simp add: x_less_zero exp_gt_zero)
-    ultimately have "exp (Ifloat x) \<le> (\<Sum>j = 0..<get_odd n. 1 / real (fact j) * Ifloat x ^ j)"
-      using get_odd exp_gt_zero by (auto intro!: pordered_cancel_semiring_class.mult_nonneg_nonneg)
-    also have "\<dots> \<le> Ifloat (ub_exp_horner prec (get_odd n) 1 1 x)"
-      using bounds(2) by auto
-    finally have "exp (Ifloat x) \<le> Ifloat (ub_exp_horner prec (get_odd n) 1 1 x)" .
-  } ultimately show ?thesis by auto
-qed
-
-subsection "Compute the exponential function on the entire domain"
-
-function ub_exp :: "nat \<Rightarrow> float \<Rightarrow> float" and lb_exp :: "nat \<Rightarrow> float \<Rightarrow> float" where
-"lb_exp prec x = (if 0 < x then float_divl prec 1 (ub_exp prec (-x))
-             else let 
-                horner = (\<lambda> x. let  y = lb_exp_horner prec (get_even (prec + 2)) 1 1 x  in if y \<le> 0 then Float 1 -2 else y)
-             in if x < - 1 then (case floor_fl x of (Float m e) \<Rightarrow> (horner (float_divl prec x (- Float m e))) ^ (nat (-m) * 2 ^ nat e))
-                           else horner x)" |
-"ub_exp prec x = (if 0 < x    then float_divr prec 1 (lb_exp prec (-x))
-             else if x < - 1  then (case floor_fl x of (Float m e) \<Rightarrow> 
-                                    (ub_exp_horner prec (get_odd (prec + 2)) 1 1 (float_divr prec x (- Float m e))) ^ (nat (-m) * 2 ^ nat e))
-                              else ub_exp_horner prec (get_odd (prec + 2)) 1 1 x)"
-by pat_completeness auto
-termination by (relation "measure (\<lambda> v. let (prec, x) = sum_case id id v in (if 0 < x then 1 else 0))", auto simp add: less_float_def)
-
-lemma exp_m1_ge_quarter: "(1 / 4 :: real) \<le> exp (- 1)"
-proof -
-  have eq4: "4 = Suc (Suc (Suc (Suc 0)))" by auto
-
-  have "1 / 4 = Ifloat (Float 1 -2)" unfolding Float_num by auto
-  also have "\<dots> \<le> Ifloat (lb_exp_horner 1 (get_even 4) 1 1 (- 1))"
-    unfolding get_even_def eq4 
-    by (auto simp add: lapprox_posrat_def rapprox_posrat_def normfloat.simps)
-  also have "\<dots> \<le> exp (Ifloat (- 1))" using bnds_exp_horner[where x="- 1"] by auto
-  finally show ?thesis unfolding Ifloat_minus Ifloat_1 . 
-qed
-
-lemma lb_exp_pos: assumes "\<not> 0 < x" shows "0 < lb_exp prec x"
-proof -
-  let "?lb_horner x" = "lb_exp_horner prec (get_even (prec + 2)) 1 1 x"
-  let "?horner x" = "let  y = ?lb_horner x  in if y \<le> 0 then Float 1 -2 else y"
-  have pos_horner: "\<And> x. 0 < ?horner x" unfolding Let_def by (cases "?lb_horner x \<le> 0", auto simp add: le_float_def less_float_def)
-  moreover { fix x :: float fix num :: nat
-    have "0 < Ifloat (?horner x) ^ num" using `0 < ?horner x`[unfolded less_float_def Ifloat_0] by (rule zero_less_power)
-    also have "\<dots> = Ifloat ((?horner x) ^ num)" using float_power by auto
-    finally have "0 < Ifloat ((?horner x) ^ num)" .
-  }
-  ultimately show ?thesis
-    unfolding lb_exp.simps if_not_P[OF `\<not> 0 < x`] Let_def by (cases "floor_fl x", cases "x < - 1", auto simp add: le_float_def less_float_def normfloat) 
-qed
-
-lemma exp_boundaries': assumes "x \<le> 0"
-  shows "exp (Ifloat x) \<in> { Ifloat (lb_exp prec x) .. Ifloat (ub_exp prec x)}"
-proof -
-  let "?lb_exp_horner x" = "lb_exp_horner prec (get_even (prec + 2)) 1 1 x"
-  let "?ub_exp_horner x" = "ub_exp_horner prec (get_odd (prec + 2)) 1 1 x"
-
-  have "Ifloat x \<le> 0" and "\<not> x > 0" using `x \<le> 0` unfolding le_float_def less_float_def by auto
-  show ?thesis
-  proof (cases "x < - 1")
-    case False hence "- 1 \<le> Ifloat x" unfolding less_float_def by auto
-    show ?thesis
-    proof (cases "?lb_exp_horner x \<le> 0")
-      from `\<not> x < - 1` have "- 1 \<le> Ifloat x" unfolding less_float_def by auto
-      hence "exp (- 1) \<le> exp (Ifloat x)" unfolding exp_le_cancel_iff .
-      from order_trans[OF exp_m1_ge_quarter this]
-      have "Ifloat (Float 1 -2) \<le> exp (Ifloat x)" unfolding Float_num .
-      moreover case True
-      ultimately show ?thesis using bnds_exp_horner `Ifloat x \<le> 0` `\<not> x > 0` `\<not> x < - 1` by auto
-    next
-      case False thus ?thesis using bnds_exp_horner `Ifloat x \<le> 0` `\<not> x > 0` `\<not> x < - 1` by (auto simp add: Let_def)
-    qed
-  next
-    case True
-    
-    obtain m e where Float_floor: "floor_fl x = Float m e" by (cases "floor_fl x", auto)
-    let ?num = "nat (- m) * 2 ^ nat e"
-    
-    have "Ifloat (floor_fl x) < - 1" using floor_fl `x < - 1` unfolding le_float_def less_float_def Ifloat_minus Ifloat_1 by (rule order_le_less_trans)
-    hence "Ifloat (floor_fl x) < 0" unfolding Float_floor Ifloat.simps using zero_less_pow2[of xe] by auto
-    hence "m < 0"
-      unfolding less_float_def Ifloat_0 Float_floor Ifloat.simps
-      unfolding pos_prod_lt[OF zero_less_pow2[of e], unfolded real_mult_commute] by auto
-    hence "1 \<le> - m" by auto
-    hence "0 < nat (- m)" by auto
-    moreover
-    have "0 \<le> e" using floor_pos_exp Float_floor[symmetric] by auto
-    hence "(0::nat) < 2 ^ nat e" by auto
-    ultimately have "0 < ?num"  by auto
-    hence "real ?num \<noteq> 0" by auto
-    have e_nat: "int (nat e) = e" using `0 \<le> e` by auto
-    have num_eq: "real ?num = Ifloat (- floor_fl x)" using `0 < nat (- m)`
-      unfolding Float_floor Ifloat_minus Ifloat.simps real_of_nat_mult pow2_int[of "nat e", unfolded e_nat] realpow_real_of_nat[symmetric] by auto
-    have "0 < - floor_fl x" using `0 < ?num`[unfolded real_of_nat_less_iff[symmetric]] unfolding less_float_def num_eq[symmetric] Ifloat_0 real_of_nat_zero .
-    hence "Ifloat (floor_fl x) < 0" unfolding less_float_def by auto
-    
-    have "exp (Ifloat x) \<le> Ifloat (ub_exp prec x)"
-    proof -
-      have div_less_zero: "Ifloat (float_divr prec x (- floor_fl x)) \<le> 0" 
-	using float_divr_nonpos_pos_upper_bound[OF `x \<le> 0` `0 < - floor_fl x`] unfolding le_float_def Ifloat_0 .
-      
-      have "exp (Ifloat x) = exp (real ?num * (Ifloat x / real ?num))" using `real ?num \<noteq> 0` by auto
-      also have "\<dots> = exp (Ifloat x / real ?num) ^ ?num" unfolding exp_real_of_nat_mult ..
-      also have "\<dots> \<le> exp (Ifloat (float_divr prec x (- floor_fl x))) ^ ?num" unfolding num_eq
-	by (rule power_mono, rule exp_le_cancel_iff[THEN iffD2], rule float_divr) auto
-      also have "\<dots> \<le> Ifloat ((?ub_exp_horner (float_divr prec x (- floor_fl x))) ^ ?num)" unfolding float_power
-	by (rule power_mono, rule bnds_exp_horner[OF div_less_zero, unfolded atLeastAtMost_iff, THEN conjunct2], auto)
-      finally show ?thesis unfolding ub_exp.simps if_not_P[OF `\<not> 0 < x`] if_P[OF `x < - 1`] float.cases Float_floor Let_def .
-    qed
-    moreover 
-    have "Ifloat (lb_exp prec x) \<le> exp (Ifloat x)"
-    proof -
-      let ?divl = "float_divl prec x (- Float m e)"
-      let ?horner = "?lb_exp_horner ?divl"
-      
-      show ?thesis
-      proof (cases "?horner \<le> 0")
-	case False hence "0 \<le> Ifloat ?horner" unfolding le_float_def by auto
-	
-	have div_less_zero: "Ifloat (float_divl prec x (- floor_fl x)) \<le> 0"
-	  using `Ifloat (floor_fl x) < 0` `Ifloat x \<le> 0` by (auto intro!: order_trans[OF float_divl] divide_nonpos_neg)
-	
-	have "Ifloat ((?lb_exp_horner (float_divl prec x (- floor_fl x))) ^ ?num) \<le>  
-          exp (Ifloat (float_divl prec x (- floor_fl x))) ^ ?num" unfolding float_power 
-	  using `0 \<le> Ifloat ?horner`[unfolded Float_floor[symmetric]] bnds_exp_horner[OF div_less_zero, unfolded atLeastAtMost_iff, THEN conjunct1] by (auto intro!: power_mono)
-	also have "\<dots> \<le> exp (Ifloat x / real ?num) ^ ?num" unfolding num_eq
-	  using float_divl by (auto intro!: power_mono simp del: Ifloat_minus)
-	also have "\<dots> = exp (real ?num * (Ifloat x / real ?num))" unfolding exp_real_of_nat_mult ..
-	also have "\<dots> = exp (Ifloat x)" using `real ?num \<noteq> 0` by auto
-	finally show ?thesis
-	  unfolding lb_exp.simps if_not_P[OF `\<not> 0 < x`] if_P[OF `x < - 1`] float.cases Float_floor Let_def if_not_P[OF False] by auto
-      next
-	case True
-	have "Ifloat (floor_fl x) \<noteq> 0" and "Ifloat (floor_fl x) \<le> 0" using `Ifloat (floor_fl x) < 0` by auto
-	from divide_right_mono_neg[OF floor_fl[of x] `Ifloat (floor_fl x) \<le> 0`, unfolded divide_self[OF `Ifloat (floor_fl x) \<noteq> 0`]]
-	have "- 1 \<le> Ifloat x / Ifloat (- floor_fl x)" unfolding Ifloat_minus by auto
-	from order_trans[OF exp_m1_ge_quarter this[unfolded exp_le_cancel_iff[where x="- 1", symmetric]]]
-	have "Ifloat (Float 1 -2) \<le> exp (Ifloat x / Ifloat (- floor_fl x))" unfolding Float_num .
-	hence "Ifloat (Float 1 -2) ^ ?num \<le> exp (Ifloat x / Ifloat (- floor_fl x)) ^ ?num"
-	  by (auto intro!: power_mono simp add: Float_num)
-	also have "\<dots> = exp (Ifloat x)" unfolding num_eq exp_real_of_nat_mult[symmetric] using `Ifloat (floor_fl x) \<noteq> 0` by auto
-	finally show ?thesis
-	  unfolding lb_exp.simps if_not_P[OF `\<not> 0 < x`] if_P[OF `x < - 1`] float.cases Float_floor Let_def if_P[OF True] float_power .
-      qed
-    qed
-    ultimately show ?thesis by auto
-  qed
-qed
-
-lemma exp_boundaries: "exp (Ifloat x) \<in> { Ifloat (lb_exp prec x) .. Ifloat (ub_exp prec x)}"
-proof -
-  show ?thesis
-  proof (cases "0 < x")
-    case False hence "x \<le> 0" unfolding less_float_def le_float_def by auto 
-    from exp_boundaries'[OF this] show ?thesis .
-  next
-    case True hence "-x \<le> 0" unfolding less_float_def le_float_def by auto
-    
-    have "Ifloat (lb_exp prec x) \<le> exp (Ifloat x)"
-    proof -
-      from exp_boundaries'[OF `-x \<le> 0`]
-      have ub_exp: "exp (- Ifloat x) \<le> Ifloat (ub_exp prec (-x))" unfolding atLeastAtMost_iff Ifloat_minus by auto
-      
-      have "Ifloat (float_divl prec 1 (ub_exp prec (-x))) \<le> Ifloat 1 / Ifloat (ub_exp prec (-x))" using float_divl .
-      also have "Ifloat 1 / Ifloat (ub_exp prec (-x)) \<le> exp (Ifloat x)"
-	using ub_exp[unfolded inverse_le_iff_le[OF order_less_le_trans[OF exp_gt_zero ub_exp] exp_gt_zero, symmetric]]
-	unfolding exp_minus nonzero_inverse_inverse_eq[OF exp_not_eq_zero] inverse_eq_divide by auto
-      finally show ?thesis unfolding lb_exp.simps if_P[OF True] .
-    qed
-    moreover
-    have "exp (Ifloat x) \<le> Ifloat (ub_exp prec x)"
-    proof -
-      have "\<not> 0 < -x" using `0 < x` unfolding less_float_def by auto
-      
-      from exp_boundaries'[OF `-x \<le> 0`]
-      have lb_exp: "Ifloat (lb_exp prec (-x)) \<le> exp (- Ifloat x)" unfolding atLeastAtMost_iff Ifloat_minus by auto
-      
-      have "exp (Ifloat x) \<le> Ifloat 1 / Ifloat (lb_exp prec (-x))"
-	using lb_exp[unfolded inverse_le_iff_le[OF exp_gt_zero lb_exp_pos[OF `\<not> 0 < -x`, unfolded less_float_def Ifloat_0], symmetric]]
-	unfolding exp_minus nonzero_inverse_inverse_eq[OF exp_not_eq_zero] inverse_eq_divide Ifloat_1 by auto
-      also have "\<dots> \<le> Ifloat (float_divr prec 1 (lb_exp prec (-x)))" using float_divr .
-      finally show ?thesis unfolding ub_exp.simps if_P[OF True] .
-    qed
-    ultimately show ?thesis by auto
-  qed
-qed
-
-lemma bnds_exp: "\<forall> x lx ux. (l, u) = (lb_exp prec lx, ub_exp prec ux) \<and> x \<in> {Ifloat lx .. Ifloat ux} \<longrightarrow> Ifloat l \<le> exp x \<and> exp x \<le> Ifloat u"
-proof (rule allI, rule allI, rule allI, rule impI)
-  fix x lx ux
-  assume "(l, u) = (lb_exp prec lx, ub_exp prec ux) \<and> x \<in> {Ifloat lx .. Ifloat ux}"
-  hence l: "lb_exp prec lx = l " and u: "ub_exp prec ux = u" and x: "x \<in> {Ifloat lx .. Ifloat ux}" by auto
-
-  { from exp_boundaries[of lx prec, unfolded l]
-    have "Ifloat l \<le> exp (Ifloat lx)" by (auto simp del: lb_exp.simps)
-    also have "\<dots> \<le> exp x" using x by auto
-    finally have "Ifloat l \<le> exp x" .
-  } moreover
-  { have "exp x \<le> exp (Ifloat ux)" using x by auto
-    also have "\<dots> \<le> Ifloat u" using exp_boundaries[of ux prec, unfolded u] by (auto simp del: ub_exp.simps)
-    finally have "exp x \<le> Ifloat u" .
-  } ultimately show "Ifloat l \<le> exp x \<and> exp x \<le> Ifloat u" ..
-qed
-
-section "Logarithm"
-
-subsection "Compute the logarithm series"
-
-fun ub_ln_horner :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float" 
-and lb_ln_horner :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float" where
-"ub_ln_horner prec 0 i x       = 0" |
-"ub_ln_horner prec (Suc n) i x = rapprox_rat prec 1 (int i) - x * lb_ln_horner prec n (Suc i) x" |
-"lb_ln_horner prec 0 i x       = 0" |
-"lb_ln_horner prec (Suc n) i x = lapprox_rat prec 1 (int i) - x * ub_ln_horner prec n (Suc i) x"
-
-lemma ln_bounds:
-  assumes "0 \<le> x" and "x < 1"
-  shows "(\<Sum>i=0..<2*n. -1^i * (1 / real (i + 1)) * x^(Suc i)) \<le> ln (x + 1)" (is "?lb")
-  and "ln (x + 1) \<le> (\<Sum>i=0..<2*n + 1. -1^i * (1 / real (i + 1)) * x^(Suc i))" (is "?ub")
-proof -
-  let "?a n" = "(1/real (n +1)) * x^(Suc n)"
-
-  have ln_eq: "(\<Sum> i. -1^i * ?a i) = ln (x + 1)"
-    using ln_series[of "x + 1"] `0 \<le> x` `x < 1` by auto
-
-  have "norm x < 1" using assms by auto
-  have "?a ----> 0" unfolding Suc_plus1[symmetric] inverse_eq_divide[symmetric] 
-    using LIMSEQ_mult[OF LIMSEQ_inverse_real_of_nat LIMSEQ_Suc[OF LIMSEQ_power_zero[OF `norm x < 1`]]] by auto
-  { fix n have "0 \<le> ?a n" by (rule mult_nonneg_nonneg, auto intro!: mult_nonneg_nonneg simp add: `0 \<le> x`) }
-  { fix n have "?a (Suc n) \<le> ?a n" unfolding inverse_eq_divide[symmetric]
-    proof (rule mult_mono)
-      show "0 \<le> x ^ Suc (Suc n)" by (auto intro!: mult_nonneg_nonneg simp add: `0 \<le> x`)
-      have "x ^ Suc (Suc n) \<le> x ^ Suc n * 1" unfolding power_Suc2 real_mult_assoc[symmetric] 
-	by (rule mult_left_mono, fact less_imp_le[OF `x < 1`], auto intro!: mult_nonneg_nonneg simp add: `0 \<le> x`)
-      thus "x ^ Suc (Suc n) \<le> x ^ Suc n" by auto
-    qed auto }
-  from summable_Leibniz'(2,4)[OF `?a ----> 0` `\<And>n. 0 \<le> ?a n`, OF `\<And>n. ?a (Suc n) \<le> ?a n`, unfolded ln_eq]
-  show "?lb" and "?ub" by auto
-qed
-
-lemma ln_float_bounds: 
-  assumes "0 \<le> Ifloat x" and "Ifloat x < 1"
-  shows "Ifloat (x * lb_ln_horner prec (get_even n) 1 x) \<le> ln (Ifloat x + 1)" (is "?lb \<le> ?ln")
-  and "ln (Ifloat x + 1) \<le> Ifloat (x * ub_ln_horner prec (get_odd n) 1 x)" (is "?ln \<le> ?ub")
-proof -
-  obtain ev where ev: "get_even n = 2 * ev" using get_even_double ..
-  obtain od where od: "get_odd n = 2 * od + 1" using get_odd_double ..
-
-  let "?s n" = "-1^n * (1 / real (1 + n)) * (Ifloat x)^(Suc n)"
-
-  have "?lb \<le> setsum ?s {0 ..< 2 * ev}" unfolding power_Suc2 real_mult_assoc[symmetric] Ifloat_mult setsum_left_distrib[symmetric] unfolding real_mult_commute[of "Ifloat x"] ev
-    using horner_bounds(1)[where G="\<lambda> i k. Suc k" and F="\<lambda>x. x" and f="\<lambda>x. x" and lb="\<lambda>n i k x. lb_ln_horner prec n k x" and ub="\<lambda>n i k x. ub_ln_horner prec n k x" and j'=1 and n="2*ev",
-      OF `0 \<le> Ifloat x` refl lb_ln_horner.simps ub_ln_horner.simps] `0 \<le> Ifloat x`
-    by (rule mult_right_mono)
-  also have "\<dots> \<le> ?ln" using ln_bounds(1)[OF `0 \<le> Ifloat x` `Ifloat x < 1`] by auto
-  finally show "?lb \<le> ?ln" . 
-
-  have "?ln \<le> setsum ?s {0 ..< 2 * od + 1}" using ln_bounds(2)[OF `0 \<le> Ifloat x` `Ifloat x < 1`] by auto
-  also have "\<dots> \<le> ?ub" unfolding power_Suc2 real_mult_assoc[symmetric] Ifloat_mult setsum_left_distrib[symmetric] unfolding real_mult_commute[of "Ifloat x"] od
-    using horner_bounds(2)[where G="\<lambda> i k. Suc k" and F="\<lambda>x. x" and f="\<lambda>x. x" and lb="\<lambda>n i k x. lb_ln_horner prec n k x" and ub="\<lambda>n i k x. ub_ln_horner prec n k x" and j'=1 and n="2*od+1",
-      OF `0 \<le> Ifloat x` refl lb_ln_horner.simps ub_ln_horner.simps] `0 \<le> Ifloat x`
-    by (rule mult_right_mono)
-  finally show "?ln \<le> ?ub" . 
-qed
-
-lemma ln_add: assumes "0 < x" and "0 < y" shows "ln (x + y) = ln x + ln (1 + y / x)"
-proof -
-  have "x \<noteq> 0" using assms by auto
-  have "x + y = x * (1 + y / x)" unfolding right_distrib times_divide_eq_right nonzero_mult_divide_cancel_left[OF `x \<noteq> 0`] by auto
-  moreover 
-  have "0 < y / x" using assms divide_pos_pos by auto
-  hence "0 < 1 + y / x" by auto
-  ultimately show ?thesis using ln_mult assms by auto
-qed
-
-subsection "Compute the logarithm of 2"
-
-definition ub_ln2 where "ub_ln2 prec = (let third = rapprox_rat (max prec 1) 1 3 
-                                        in (Float 1 -1 * ub_ln_horner prec (get_odd prec) 1 (Float 1 -1)) + 
-                                           (third * ub_ln_horner prec (get_odd prec) 1 third))"
-definition lb_ln2 where "lb_ln2 prec = (let third = lapprox_rat prec 1 3 
-                                        in (Float 1 -1 * lb_ln_horner prec (get_even prec) 1 (Float 1 -1)) + 
-                                           (third * lb_ln_horner prec (get_even prec) 1 third))"
-
-lemma ub_ln2: "ln 2 \<le> Ifloat (ub_ln2 prec)" (is "?ub_ln2")
-  and lb_ln2: "Ifloat (lb_ln2 prec) \<le> ln 2" (is "?lb_ln2")
-proof -
-  let ?uthird = "rapprox_rat (max prec 1) 1 3"
-  let ?lthird = "lapprox_rat prec 1 3"
-
-  have ln2_sum: "ln 2 = ln (1/2 + 1) + ln (1 / 3 + 1)"
-    using ln_add[of "3 / 2" "1 / 2"] by auto
-  have lb3: "Ifloat ?lthird \<le> 1 / 3" using lapprox_rat[of prec 1 3] by auto
-  hence lb3_ub: "Ifloat ?lthird < 1" by auto
-  have lb3_lb: "0 \<le> Ifloat ?lthird" using lapprox_rat_bottom[of 1 3] by auto
-  have ub3: "1 / 3 \<le> Ifloat ?uthird" using rapprox_rat[of 1 3] by auto
-  hence ub3_lb: "0 \<le> Ifloat ?uthird" by auto
-
-  have lb2: "0 \<le> Ifloat (Float 1 -1)" and ub2: "Ifloat (Float 1 -1) < 1" unfolding Float_num by auto
-
-  have "0 \<le> (1::int)" and "0 < (3::int)" by auto
-  have ub3_ub: "Ifloat ?uthird < 1" unfolding rapprox_rat.simps(2)[OF `0 \<le> 1` `0 < 3`]
-    by (rule rapprox_posrat_less1, auto)
-
-  have third_gt0: "(0 :: real) < 1 / 3 + 1" by auto
-  have uthird_gt0: "0 < Ifloat ?uthird + 1" using ub3_lb by auto
-  have lthird_gt0: "0 < Ifloat ?lthird + 1" using lb3_lb by auto
-
-  show ?ub_ln2 unfolding ub_ln2_def Let_def Ifloat_add ln2_sum Float_num(4)[symmetric]
-  proof (rule add_mono, fact ln_float_bounds(2)[OF lb2 ub2])
-    have "ln (1 / 3 + 1) \<le> ln (Ifloat ?uthird + 1)" unfolding ln_le_cancel_iff[OF third_gt0 uthird_gt0] using ub3 by auto
-    also have "\<dots> \<le> Ifloat (?uthird * ub_ln_horner prec (get_odd prec) 1 ?uthird)"
-      using ln_float_bounds(2)[OF ub3_lb ub3_ub] .
-    finally show "ln (1 / 3 + 1) \<le> Ifloat (?uthird * ub_ln_horner prec (get_odd prec) 1 ?uthird)" .
-  qed
-  show ?lb_ln2 unfolding lb_ln2_def Let_def Ifloat_add ln2_sum Float_num(4)[symmetric]
-  proof (rule add_mono, fact ln_float_bounds(1)[OF lb2 ub2])
-    have "Ifloat (?lthird * lb_ln_horner prec (get_even prec) 1 ?lthird) \<le> ln (Ifloat ?lthird + 1)"
-      using ln_float_bounds(1)[OF lb3_lb lb3_ub] .
-    also have "\<dots> \<le> ln (1 / 3 + 1)" unfolding ln_le_cancel_iff[OF lthird_gt0 third_gt0] using lb3 by auto
-    finally show "Ifloat (?lthird * lb_ln_horner prec (get_even prec) 1 ?lthird) \<le> ln (1 / 3 + 1)" .
-  qed
-qed
-
-subsection "Compute the logarithm in the entire domain"
-
-function ub_ln :: "nat \<Rightarrow> float \<Rightarrow> float option" and lb_ln :: "nat \<Rightarrow> float \<Rightarrow> float option" where
-"ub_ln prec x = (if x \<le> 0         then None
-            else if x < 1         then Some (- the (lb_ln prec (float_divl (max prec 1) 1 x)))
-            else let horner = \<lambda>x. (x - 1) * ub_ln_horner prec (get_odd prec) 1 (x - 1) in
-                 if x < Float 1 1 then Some (horner x)
-                                  else let l = bitlen (mantissa x) - 1 in 
-                                       Some (ub_ln2 prec * (Float (scale x + l) 0) + horner (Float (mantissa x) (- l))))" |
-"lb_ln prec x = (if x \<le> 0         then None
-            else if x < 1         then Some (- the (ub_ln prec (float_divr prec 1 x)))
-            else let horner = \<lambda>x. (x - 1) * lb_ln_horner prec (get_even prec) 1 (x - 1) in
-                 if x < Float 1 1 then Some (horner x)
-                                  else let l = bitlen (mantissa x) - 1 in 
-                                       Some (lb_ln2 prec * (Float (scale x + l) 0) + horner (Float (mantissa x) (- l))))"
-by pat_completeness auto
-
-termination proof (relation "measure (\<lambda> v. let (prec, x) = sum_case id id v in (if x < 1 then 1 else 0))", auto)
-  fix prec x assume "\<not> x \<le> 0" and "x < 1" and "float_divl (max prec (Suc 0)) 1 x < 1"
-  hence "0 < x" and "0 < max prec (Suc 0)" unfolding less_float_def le_float_def by auto
-  from float_divl_pos_less1_bound[OF `0 < x` `x < 1` `0 < max prec (Suc 0)`]
-  show False using `float_divl (max prec (Suc 0)) 1 x < 1` unfolding less_float_def le_float_def by auto
-next
-  fix prec x assume "\<not> x \<le> 0" and "x < 1" and "float_divr prec 1 x < 1"
-  hence "0 < x" unfolding less_float_def le_float_def by auto
-  from float_divr_pos_less1_lower_bound[OF `0 < x` `x < 1`, of prec]
-  show False using `float_divr prec 1 x < 1` unfolding less_float_def le_float_def by auto
-qed
-
-lemma ln_shifted_float: assumes "0 < m" shows "ln (Ifloat (Float m e)) = ln 2 * real (e + (bitlen m - 1)) + ln (Ifloat (Float m (- (bitlen m - 1))))"
-proof -
-  let ?B = "2^nat (bitlen m - 1)"
-  have "0 < real m" and "\<And>X. (0 :: real) < 2^X" and "0 < (2 :: real)" and "m \<noteq> 0" using assms by auto
-  hence "0 \<le> bitlen m - 1" using bitlen_ge1[OF `m \<noteq> 0`] by auto
-  show ?thesis 
-  proof (cases "0 \<le> e")
-    case True
-    show ?thesis unfolding normalized_float[OF `m \<noteq> 0`]
-      unfolding ln_div[OF `0 < real m` `0 < ?B`] real_of_int_add ln_realpow[OF `0 < 2`] 
-      unfolding Ifloat_ge0_exp[OF True] ln_mult[OF `0 < real m` `0 < 2^nat e`] 
-      ln_realpow[OF `0 < 2`] algebra_simps using `0 \<le> bitlen m - 1` True by auto
-  next
-    case False hence "0 < -e" by auto
-    hence pow_gt0: "(0::real) < 2^nat (-e)" by auto
-    hence inv_gt0: "(0::real) < inverse (2^nat (-e))" by auto
-    show ?thesis unfolding normalized_float[OF `m \<noteq> 0`]
-      unfolding ln_div[OF `0 < real m` `0 < ?B`] real_of_int_add ln_realpow[OF `0 < 2`] 
-      unfolding Ifloat_nge0_exp[OF False] ln_mult[OF `0 < real m` inv_gt0] ln_inverse[OF pow_gt0]
-      ln_realpow[OF `0 < 2`] algebra_simps using `0 \<le> bitlen m - 1` False by auto
-  qed
-qed
-
-lemma ub_ln_lb_ln_bounds': assumes "1 \<le> x"
-  shows "Ifloat (the (lb_ln prec x)) \<le> ln (Ifloat x) \<and> ln (Ifloat x) \<le> Ifloat (the (ub_ln prec x))"
-  (is "?lb \<le> ?ln \<and> ?ln \<le> ?ub")
-proof (cases "x < Float 1 1")
-  case True hence "Ifloat (x - 1) < 1" unfolding less_float_def Float_num by auto
-  have "\<not> x \<le> 0" and "\<not> x < 1" using `1 \<le> x` unfolding less_float_def le_float_def by auto
-  hence "0 \<le> Ifloat (x - 1)" using `1 \<le> x` unfolding less_float_def Float_num by auto
-  show ?thesis unfolding lb_ln.simps unfolding ub_ln.simps Let_def
-    using ln_float_bounds[OF `0 \<le> Ifloat (x - 1)` `Ifloat (x - 1) < 1`] `\<not> x \<le> 0` `\<not> x < 1` True by auto
-next
-  case False
-  have "\<not> x \<le> 0" and "\<not> x < 1" "0 < x" using `1 \<le> x` unfolding less_float_def le_float_def by auto
-  show ?thesis
-  proof (cases x)
-    case (Float m e)
-    let ?s = "Float (e + (bitlen m - 1)) 0"
-    let ?x = "Float m (- (bitlen m - 1))"
-
-    have "0 < m" and "m \<noteq> 0" using float_pos_m_pos `0 < x` Float by auto
-
-    {
-      have "Ifloat (lb_ln2 prec * ?s) \<le> ln 2 * real (e + (bitlen m - 1))" (is "?lb2 \<le> _")
-	unfolding Ifloat_mult Ifloat_ge0_exp[OF order_refl] nat_0 realpow_0 mult_1_right
-	using lb_ln2[of prec]
-      proof (rule mult_right_mono)
-	have "1 \<le> Float m e" using `1 \<le> x` Float unfolding le_float_def by auto
-	from float_gt1_scale[OF this]
-	show "0 \<le> real (e + (bitlen m - 1))" by auto
-      qed
-      moreover
-      from bitlen_div[OF `0 < m`, unfolded normalized_float[OF `m \<noteq> 0`, symmetric]]
-      have "0 \<le> Ifloat (?x - 1)" and "Ifloat (?x - 1) < 1" by auto
-      from ln_float_bounds(1)[OF this]
-      have "Ifloat ((?x - 1) * lb_ln_horner prec (get_even prec) 1 (?x - 1)) \<le> ln (Ifloat ?x)" (is "?lb_horner \<le> _") by auto
-      ultimately have "?lb2 + ?lb_horner \<le> ln (Ifloat x)"
-	unfolding Float ln_shifted_float[OF `0 < m`, of e] by auto
-    } 
-    moreover
-    {
-      from bitlen_div[OF `0 < m`, unfolded normalized_float[OF `m \<noteq> 0`, symmetric]]
-      have "0 \<le> Ifloat (?x - 1)" and "Ifloat (?x - 1) < 1" by auto
-      from ln_float_bounds(2)[OF this]
-      have "ln (Ifloat ?x) \<le> Ifloat ((?x - 1) * ub_ln_horner prec (get_odd prec) 1 (?x - 1))" (is "_ \<le> ?ub_horner") by auto
-      moreover
-      have "ln 2 * real (e + (bitlen m - 1)) \<le> Ifloat (ub_ln2 prec * ?s)" (is "_ \<le> ?ub2")
-	unfolding Ifloat_mult Ifloat_ge0_exp[OF order_refl] nat_0 realpow_0 mult_1_right
-	using ub_ln2[of prec] 
-      proof (rule mult_right_mono)
-	have "1 \<le> Float m e" using `1 \<le> x` Float unfolding le_float_def by auto
-	from float_gt1_scale[OF this]
-	show "0 \<le> real (e + (bitlen m - 1))" by auto
-      qed
-      ultimately have "ln (Ifloat x) \<le> ?ub2 + ?ub_horner"
-	unfolding Float ln_shifted_float[OF `0 < m`, of e] by auto
-    }
-    ultimately show ?thesis unfolding lb_ln.simps unfolding ub_ln.simps
-      unfolding if_not_P[OF `\<not> x \<le> 0`] if_not_P[OF `\<not> x < 1`] if_not_P[OF False] Let_def
-      unfolding scale.simps[of m e, unfolded Float[symmetric]] mantissa.simps[of m e, unfolded Float[symmetric]] Ifloat_add by auto
-  qed
-qed
-
-lemma ub_ln_lb_ln_bounds: assumes "0 < x"
-  shows "Ifloat (the (lb_ln prec x)) \<le> ln (Ifloat x) \<and> ln (Ifloat x) \<le> Ifloat (the (ub_ln prec x))"
-  (is "?lb \<le> ?ln \<and> ?ln \<le> ?ub")
-proof (cases "x < 1")
-  case False hence "1 \<le> x" unfolding less_float_def le_float_def by auto
-  show ?thesis using ub_ln_lb_ln_bounds'[OF `1 \<le> x`] .
-next
-  case True have "\<not> x \<le> 0" using `0 < x` unfolding less_float_def le_float_def by auto
-
-  have "0 < Ifloat x" and "Ifloat x \<noteq> 0" using `0 < x` unfolding less_float_def by auto
-  hence A: "0 < 1 / Ifloat x" by auto
-
-  {
-    let ?divl = "float_divl (max prec 1) 1 x"
-    have A': "1 \<le> ?divl" using float_divl_pos_less1_bound[OF `0 < x` `x < 1`] unfolding le_float_def less_float_def by auto
-    hence B: "0 < Ifloat ?divl" unfolding le_float_def by auto
-    
-    have "ln (Ifloat ?divl) \<le> ln (1 / Ifloat x)" unfolding ln_le_cancel_iff[OF B A] using float_divl[of _ 1 x] by auto
-    hence "ln (Ifloat x) \<le> - ln (Ifloat ?divl)" unfolding nonzero_inverse_eq_divide[OF `Ifloat x \<noteq> 0`, symmetric] ln_inverse[OF `0 < Ifloat x`] by auto
-    from this ub_ln_lb_ln_bounds'[OF A', THEN conjunct1, THEN le_imp_neg_le] 
-    have "?ln \<le> Ifloat (- the (lb_ln prec ?divl))" unfolding Ifloat_minus by (rule order_trans)
-  } moreover
-  {
-    let ?divr = "float_divr prec 1 x"
-    have A': "1 \<le> ?divr" using float_divr_pos_less1_lower_bound[OF `0 < x` `x < 1`] unfolding le_float_def less_float_def by auto
-    hence B: "0 < Ifloat ?divr" unfolding le_float_def by auto
-    
-    have "ln (1 / Ifloat x) \<le> ln (Ifloat ?divr)" unfolding ln_le_cancel_iff[OF A B] using float_divr[of 1 x] by auto
-    hence "- ln (Ifloat ?divr) \<le> ln (Ifloat x)" unfolding nonzero_inverse_eq_divide[OF `Ifloat x \<noteq> 0`, symmetric] ln_inverse[OF `0 < Ifloat x`] by auto
-    from ub_ln_lb_ln_bounds'[OF A', THEN conjunct2, THEN le_imp_neg_le] this
-    have "Ifloat (- the (ub_ln prec ?divr)) \<le> ?ln" unfolding Ifloat_minus by (rule order_trans)
-  }
-  ultimately show ?thesis unfolding lb_ln.simps[where x=x]  ub_ln.simps[where x=x]
-    unfolding if_not_P[OF `\<not> x \<le> 0`] if_P[OF True] by auto
-qed
-
-lemma lb_ln: assumes "Some y = lb_ln prec x"
-  shows "Ifloat y \<le> ln (Ifloat x)" and "0 < Ifloat x"
-proof -
-  have "0 < x"
-  proof (rule ccontr)
-    assume "\<not> 0 < x" hence "x \<le> 0" unfolding le_float_def less_float_def by auto
-    thus False using assms by auto
-  qed
-  thus "0 < Ifloat x" unfolding less_float_def by auto
-  have "Ifloat (the (lb_ln prec x)) \<le> ln (Ifloat x)" using ub_ln_lb_ln_bounds[OF `0 < x`] ..
-  thus "Ifloat y \<le> ln (Ifloat x)" unfolding assms[symmetric] by auto
-qed
-
-lemma ub_ln: assumes "Some y = ub_ln prec x"
-  shows "ln (Ifloat x) \<le> Ifloat y" and "0 < Ifloat x"
-proof -
-  have "0 < x"
-  proof (rule ccontr)
-    assume "\<not> 0 < x" hence "x \<le> 0" unfolding le_float_def less_float_def by auto
-    thus False using assms by auto
-  qed
-  thus "0 < Ifloat x" unfolding less_float_def by auto
-  have "ln (Ifloat x) \<le> Ifloat (the (ub_ln prec x))" using ub_ln_lb_ln_bounds[OF `0 < x`] ..
-  thus "ln (Ifloat x) \<le> Ifloat y" unfolding assms[symmetric] by auto
-qed
-
-lemma bnds_ln: "\<forall> x lx ux. (Some l, Some u) = (lb_ln prec lx, ub_ln prec ux) \<and> x \<in> {Ifloat lx .. Ifloat ux} \<longrightarrow> Ifloat l \<le> ln x \<and> ln x \<le> Ifloat u"
-proof (rule allI, rule allI, rule allI, rule impI)
-  fix x lx ux
-  assume "(Some l, Some u) = (lb_ln prec lx, ub_ln prec ux) \<and> x \<in> {Ifloat lx .. Ifloat ux}"
-  hence l: "Some l = lb_ln prec lx " and u: "Some u = ub_ln prec ux" and x: "x \<in> {Ifloat lx .. Ifloat ux}" by auto
-
-  have "ln (Ifloat ux) \<le> Ifloat u" and "0 < Ifloat ux" using ub_ln u by auto
-  have "Ifloat l \<le> ln (Ifloat lx)" and "0 < Ifloat lx" and "0 < x" using lb_ln[OF l] x by auto
-
-  from ln_le_cancel_iff[OF `0 < Ifloat lx` `0 < x`] `Ifloat l \<le> ln (Ifloat lx)` 
-  have "Ifloat l \<le> ln x" using x unfolding atLeastAtMost_iff by auto
-  moreover
-  from ln_le_cancel_iff[OF `0 < x` `0 < Ifloat ux`] `ln (Ifloat ux) \<le> Ifloat u` 
-  have "ln x \<le> Ifloat u" using x unfolding atLeastAtMost_iff by auto
-  ultimately show "Ifloat l \<le> ln x \<and> ln x \<le> Ifloat u" ..
-qed
-
-
-section "Implement floatarith"
-
-subsection "Define syntax and semantics"
-
-datatype floatarith
-  = Add floatarith floatarith
-  | Minus floatarith
-  | Mult floatarith floatarith
-  | Inverse floatarith
-  | Sin floatarith
-  | Cos floatarith
-  | Arctan floatarith
-  | Abs floatarith
-  | Max floatarith floatarith
-  | Min floatarith floatarith
-  | Pi
-  | Sqrt floatarith
-  | Exp floatarith
-  | Ln floatarith
-  | Power floatarith nat
-  | Atom nat
-  | Num float
-
-fun Ifloatarith :: "floatarith \<Rightarrow> real list \<Rightarrow> real"
-where
-"Ifloatarith (Add a b) vs   = (Ifloatarith a vs) + (Ifloatarith b vs)" |
-"Ifloatarith (Minus a) vs    = - (Ifloatarith a vs)" |
-"Ifloatarith (Mult a b) vs   = (Ifloatarith a vs) * (Ifloatarith b vs)" |
-"Ifloatarith (Inverse a) vs  = inverse (Ifloatarith a vs)" |
-"Ifloatarith (Sin a) vs      = sin (Ifloatarith a vs)" |
-"Ifloatarith (Cos a) vs      = cos (Ifloatarith a vs)" |
-"Ifloatarith (Arctan a) vs   = arctan (Ifloatarith a vs)" |
-"Ifloatarith (Min a b) vs    = min (Ifloatarith a vs) (Ifloatarith b vs)" |
-"Ifloatarith (Max a b) vs    = max (Ifloatarith a vs) (Ifloatarith b vs)" |
-"Ifloatarith (Abs a) vs      = abs (Ifloatarith a vs)" |
-"Ifloatarith Pi vs           = pi" |
-"Ifloatarith (Sqrt a) vs     = sqrt (Ifloatarith a vs)" |
-"Ifloatarith (Exp a) vs      = exp (Ifloatarith a vs)" |
-"Ifloatarith (Ln a) vs       = ln (Ifloatarith a vs)" |
-"Ifloatarith (Power a n) vs  = (Ifloatarith a vs)^n" |
-"Ifloatarith (Num f) vs      = Ifloat f" |
-"Ifloatarith (Atom n) vs     = vs ! n"
-
-subsection "Implement approximation function"
-
-fun lift_bin :: "(float * float) option \<Rightarrow> (float * float) option \<Rightarrow> (float \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float \<Rightarrow> (float option * float option)) \<Rightarrow> (float * float) option" where
-"lift_bin (Some (l1, u1)) (Some (l2, u2)) f = (case (f l1 u1 l2 u2) of (Some l, Some u) \<Rightarrow> Some (l, u)
-                                                                     | t \<Rightarrow> None)" |
-"lift_bin a b f = None"
-
-fun lift_bin' :: "(float * float) option \<Rightarrow> (float * float) option \<Rightarrow> (float \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float \<Rightarrow> (float * float)) \<Rightarrow> (float * float) option" where
-"lift_bin' (Some (l1, u1)) (Some (l2, u2)) f = Some (f l1 u1 l2 u2)" |
-"lift_bin' a b f = None"
-
-fun lift_un :: "(float * float) option \<Rightarrow> (float \<Rightarrow> float \<Rightarrow> ((float option) * (float option))) \<Rightarrow> (float * float) option" where
-"lift_un (Some (l1, u1)) f = (case (f l1 u1) of (Some l, Some u) \<Rightarrow> Some (l, u)
-                                             | t \<Rightarrow> None)" |
-"lift_un b f = None"
-
-fun lift_un' :: "(float * float) option \<Rightarrow> (float \<Rightarrow> float \<Rightarrow> (float * float)) \<Rightarrow> (float * float) option" where
-"lift_un' (Some (l1, u1)) f = Some (f l1 u1)" |
-"lift_un' b f = None"
-
-fun bounded_by :: "real list \<Rightarrow> (float * float) list \<Rightarrow> bool " where
-bounded_by_Cons: "bounded_by (v#vs) ((l, u)#bs) = ((Ifloat l \<le> v \<and> v \<le> Ifloat u) \<and> bounded_by vs bs)" |
-bounded_by_Nil: "bounded_by [] [] = True" |
-"bounded_by _ _ = False"
-
-lemma bounded_by: assumes "bounded_by vs bs" and "i < length bs"
-  shows "Ifloat (fst (bs ! i)) \<le> vs ! i \<and> vs ! i \<le> Ifloat (snd (bs ! i))"
-  using `bounded_by vs bs` and `i < length bs`
-proof (induct arbitrary: i rule: bounded_by.induct)
-  fix v :: real and vs :: "real list" and l u :: float and bs :: "(float * float) list" and i :: nat
-  assume hyp: "\<And>i. \<lbrakk>bounded_by vs bs; i < length bs\<rbrakk> \<Longrightarrow> Ifloat (fst (bs ! i)) \<le> vs ! i \<and> vs ! i \<le> Ifloat (snd (bs ! i))"
-  assume bounded: "bounded_by (v # vs) ((l, u) # bs)" and length: "i < length ((l, u) # bs)"
-  show "Ifloat (fst (((l, u) # bs) ! i)) \<le> (v # vs) ! i \<and> (v # vs) ! i \<le> Ifloat (snd (((l, u) # bs) ! i))"
-  proof (cases i)
-    case 0
-    show ?thesis using bounded unfolding 0 nth_Cons_0 fst_conv snd_conv bounded_by.simps ..
-  next
-    case (Suc i) with length have "i < length bs" by auto
-    show ?thesis unfolding Suc nth_Cons_Suc bounded_by.simps
-      using hyp[OF bounded[unfolded bounded_by.simps, THEN conjunct2] `i < length bs`] .
-  qed
-qed auto
-
-fun approx approx' :: "nat \<Rightarrow> floatarith \<Rightarrow> (float * float) list \<Rightarrow> (float * float) option" where
-"approx' prec a bs          = (case (approx prec a bs) of Some (l, u) \<Rightarrow> Some (round_down prec l, round_up prec u) | None \<Rightarrow> None)" |
-"approx prec (Add a b) bs  = lift_bin' (approx' prec a bs) (approx' prec b bs) (\<lambda> l1 u1 l2 u2. (l1 + l2, u1 + u2))" | 
-"approx prec (Minus a) bs   = lift_un' (approx' prec a bs) (\<lambda> l u. (-u, -l))" |
-"approx prec (Mult a b) bs  = lift_bin' (approx' prec a bs) (approx' prec b bs)
-                                    (\<lambda> a1 a2 b1 b2. (float_nprt a1 * float_pprt b2 + float_nprt a2 * float_nprt b2 + float_pprt a1 * float_pprt b1 + float_pprt a2 * float_nprt b1, 
-                                                     float_pprt a2 * float_pprt b2 + float_pprt a1 * float_nprt b2 + float_nprt a2 * float_pprt b1 + float_nprt a1 * float_nprt b1))" |
-"approx prec (Inverse a) bs = lift_un (approx' prec a bs) (\<lambda> l u. if (0 < l \<or> u < 0) then (Some (float_divl prec 1 u), Some (float_divr prec 1 l)) else (None, None))" |
-"approx prec (Sin a) bs     = lift_un' (approx' prec a bs) (bnds_sin prec)" |
-"approx prec (Cos a) bs     = lift_un' (approx' prec a bs) (bnds_cos prec)" |
-"approx prec Pi bs          = Some (lb_pi prec, ub_pi prec)" |
-"approx prec (Min a b) bs   = lift_bin' (approx' prec a bs) (approx' prec b bs) (\<lambda> l1 u1 l2 u2. (min l1 l2, min u1 u2))" |
-"approx prec (Max a b) bs   = lift_bin' (approx' prec a bs) (approx' prec b bs) (\<lambda> l1 u1 l2 u2. (max l1 l2, max u1 u2))" |
-"approx prec (Abs a) bs     = lift_un' (approx' prec a bs) (\<lambda>l u. (if l < 0 \<and> 0 < u then 0 else min \<bar>l\<bar> \<bar>u\<bar>, max \<bar>l\<bar> \<bar>u\<bar>))" |
-"approx prec (Arctan a) bs  = lift_un' (approx' prec a bs) (\<lambda> l u. (lb_arctan prec l, ub_arctan prec u))" |
-"approx prec (Sqrt a) bs    = lift_un (approx' prec a bs) (\<lambda> l u. (lb_sqrt prec l, ub_sqrt prec u))" |
-"approx prec (Exp a) bs     = lift_un' (approx' prec a bs) (\<lambda> l u. (lb_exp prec l, ub_exp prec u))" |
-"approx prec (Ln a) bs      = lift_un (approx' prec a bs) (\<lambda> l u. (lb_ln prec l, ub_ln prec u))" |
-"approx prec (Power a n) bs = lift_un' (approx' prec a bs) (float_power_bnds n)" |
-"approx prec (Num f) bs     = Some (f, f)" |
-"approx prec (Atom i) bs    = (if i < length bs then Some (bs ! i) else None)"
-
-lemma lift_bin'_ex:
-  assumes lift_bin'_Some: "Some (l, u) = lift_bin' a b f"
-  shows "\<exists> l1 u1 l2 u2. Some (l1, u1) = a \<and> Some (l2, u2) = b"
-proof (cases a)
-  case None hence "None = lift_bin' a b f" unfolding None lift_bin'.simps ..
-  thus ?thesis using lift_bin'_Some by auto
-next
-  case (Some a')
-  show ?thesis
-  proof (cases b)
-    case None hence "None = lift_bin' a b f" unfolding None lift_bin'.simps ..
-    thus ?thesis using lift_bin'_Some by auto
-  next
-    case (Some b')
-    obtain la ua where a': "a' = (la, ua)" by (cases a', auto)
-    obtain lb ub where b': "b' = (lb, ub)" by (cases b', auto)
-    thus ?thesis unfolding `a = Some a'` `b = Some b'` a' b' by auto
-  qed
-qed
-
-lemma lift_bin'_f:
-  assumes lift_bin'_Some: "Some (l, u) = lift_bin' (g a) (g b) f"
-  and Pa: "\<And>l u. Some (l, u) = g a \<Longrightarrow> P l u a" and Pb: "\<And>l u. Some (l, u) = g b \<Longrightarrow> P l u b"
-  shows "\<exists> l1 u1 l2 u2. P l1 u1 a \<and> P l2 u2 b \<and> l = fst (f l1 u1 l2 u2) \<and> u = snd (f l1 u1 l2 u2)"
-proof -
-  obtain l1 u1 l2 u2
-    where Sa: "Some (l1, u1) = g a" and Sb: "Some (l2, u2) = g b" using lift_bin'_ex[OF assms(1)] by auto
-  have lu: "(l, u) = f l1 u1 l2 u2" using lift_bin'_Some[unfolded Sa[symmetric] Sb[symmetric] lift_bin'.simps] by auto 
-  have "l = fst (f l1 u1 l2 u2)" and "u = snd (f l1 u1 l2 u2)" unfolding lu[symmetric] by auto
-  thus ?thesis using Pa[OF Sa] Pb[OF Sb] by auto 
-qed
-
-lemma approx_approx':
-  assumes Pa: "\<And>l u. Some (l, u) = approx prec a vs \<Longrightarrow> Ifloat l \<le> Ifloatarith a xs \<and> Ifloatarith a xs \<le> Ifloat u"
-  and approx': "Some (l, u) = approx' prec a vs"
-  shows "Ifloat l \<le> Ifloatarith a xs \<and> Ifloatarith a xs \<le> Ifloat u"
-proof -
-  obtain l' u' where S: "Some (l', u') = approx prec a vs"
-    using approx' unfolding approx'.simps by (cases "approx prec a vs", auto)
-  have l': "l = round_down prec l'" and u': "u = round_up prec u'"
-    using approx' unfolding approx'.simps S[symmetric] by auto
-  show ?thesis unfolding l' u' 
-    using order_trans[OF Pa[OF S, THEN conjunct2] round_up[of u']]
-    using order_trans[OF round_down[of _ l'] Pa[OF S, THEN conjunct1]] by auto
-qed
-
-lemma lift_bin':
-  assumes lift_bin'_Some: "Some (l, u) = lift_bin' (approx' prec a bs) (approx' prec b bs) f"
-  and Pa: "\<And>l u. Some (l, u) = approx prec a bs \<Longrightarrow> Ifloat l \<le> Ifloatarith a xs \<and> Ifloatarith a xs \<le> Ifloat u" (is "\<And>l u. _ = ?g a \<Longrightarrow> ?P l u a")
-  and Pb: "\<And>l u. Some (l, u) = approx prec b bs \<Longrightarrow> Ifloat l \<le> Ifloatarith b xs \<and> Ifloatarith b xs \<le> Ifloat u"
-  shows "\<exists> l1 u1 l2 u2. (Ifloat l1 \<le> Ifloatarith a xs \<and> Ifloatarith a xs \<le> Ifloat u1) \<and> 
-                        (Ifloat l2 \<le> Ifloatarith b xs \<and> Ifloatarith b xs \<le> Ifloat u2) \<and> 
-                        l = fst (f l1 u1 l2 u2) \<and> u = snd (f l1 u1 l2 u2)"
-proof -
-  { fix l u assume "Some (l, u) = approx' prec a bs"
-    with approx_approx'[of prec a bs, OF _ this] Pa
-    have "Ifloat l \<le> Ifloatarith a xs \<and> Ifloatarith a xs \<le> Ifloat u" by auto } note Pa = this
-  { fix l u assume "Some (l, u) = approx' prec b bs"
-    with approx_approx'[of prec b bs, OF _ this] Pb
-    have "Ifloat l \<le> Ifloatarith b xs \<and> Ifloatarith b xs \<le> Ifloat u" by auto } note Pb = this
-
-  from lift_bin'_f[where g="\<lambda>a. approx' prec a bs" and P = ?P, OF lift_bin'_Some, OF Pa Pb]
-  show ?thesis by auto
-qed
-
-lemma lift_un'_ex:
-  assumes lift_un'_Some: "Some (l, u) = lift_un' a f"
-  shows "\<exists> l u. Some (l, u) = a"
-proof (cases a)
-  case None hence "None = lift_un' a f" unfolding None lift_un'.simps ..
-  thus ?thesis using lift_un'_Some by auto
-next
-  case (Some a')
-  obtain la ua where a': "a' = (la, ua)" by (cases a', auto)
-  thus ?thesis unfolding `a = Some a'` a' by auto
-qed
-
-lemma lift_un'_f:
-  assumes lift_un'_Some: "Some (l, u) = lift_un' (g a) f"
-  and Pa: "\<And>l u. Some (l, u) = g a \<Longrightarrow> P l u a"
-  shows "\<exists> l1 u1. P l1 u1 a \<and> l = fst (f l1 u1) \<and> u = snd (f l1 u1)"
-proof -
-  obtain l1 u1 where Sa: "Some (l1, u1) = g a" using lift_un'_ex[OF assms(1)] by auto
-  have lu: "(l, u) = f l1 u1" using lift_un'_Some[unfolded Sa[symmetric] lift_un'.simps] by auto
-  have "l = fst (f l1 u1)" and "u = snd (f l1 u1)" unfolding lu[symmetric] by auto
-  thus ?thesis using Pa[OF Sa] by auto
-qed
-
-lemma lift_un':
-  assumes lift_un'_Some: "Some (l, u) = lift_un' (approx' prec a bs) f"
-  and Pa: "\<And>l u. Some (l, u) = approx prec a bs \<Longrightarrow> Ifloat l \<le> Ifloatarith a xs \<and> Ifloatarith a xs \<le> Ifloat u" (is "\<And>l u. _ = ?g a \<Longrightarrow> ?P l u a")
-  shows "\<exists> l1 u1. (Ifloat l1 \<le> Ifloatarith a xs \<and> Ifloatarith a xs \<le> Ifloat u1) \<and> 
-                        l = fst (f l1 u1) \<and> u = snd (f l1 u1)"
-proof -
-  { fix l u assume "Some (l, u) = approx' prec a bs"
-    with approx_approx'[of prec a bs, OF _ this] Pa
-    have "Ifloat l \<le> Ifloatarith a xs \<and> Ifloatarith a xs \<le> Ifloat u" by auto } note Pa = this
-  from lift_un'_f[where g="\<lambda>a. approx' prec a bs" and P = ?P, OF lift_un'_Some, OF Pa]
-  show ?thesis by auto
-qed
-
-lemma lift_un'_bnds:
-  assumes bnds: "\<forall> x lx ux. (l, u) = f lx ux \<and> x \<in> { Ifloat lx .. Ifloat ux } \<longrightarrow> Ifloat l \<le> f' x \<and> f' x \<le> Ifloat u"
-  and lift_un'_Some: "Some (l, u) = lift_un' (approx' prec a bs) f"
-  and Pa: "\<And>l u. Some (l, u) = approx prec a bs \<Longrightarrow> Ifloat l \<le> Ifloatarith a xs \<and> Ifloatarith a xs \<le> Ifloat u"
-  shows "Ifloat l \<le> f' (Ifloatarith a xs) \<and> f' (Ifloatarith a xs) \<le> Ifloat u"
-proof -
-  from lift_un'[OF lift_un'_Some Pa]
-  obtain l1 u1 where "Ifloat l1 \<le> Ifloatarith a xs" and "Ifloatarith a xs \<le> Ifloat u1" and "l = fst (f l1 u1)" and "u = snd (f l1 u1)" by blast
-  hence "(l, u) = f l1 u1" and "Ifloatarith a xs \<in> {Ifloat l1 .. Ifloat u1}" by auto
-  thus ?thesis using bnds by auto
-qed
-
-lemma lift_un_ex:
-  assumes lift_un_Some: "Some (l, u) = lift_un a f"
-  shows "\<exists> l u. Some (l, u) = a"
-proof (cases a)
-  case None hence "None = lift_un a f" unfolding None lift_un.simps ..
-  thus ?thesis using lift_un_Some by auto
-next
-  case (Some a')
-  obtain la ua where a': "a' = (la, ua)" by (cases a', auto)
-  thus ?thesis unfolding `a = Some a'` a' by auto
-qed
-
-lemma lift_un_f:
-  assumes lift_un_Some: "Some (l, u) = lift_un (g a) f"
-  and Pa: "\<And>l u. Some (l, u) = g a \<Longrightarrow> P l u a"
-  shows "\<exists> l1 u1. P l1 u1 a \<and> Some l = fst (f l1 u1) \<and> Some u = snd (f l1 u1)"
-proof -
-  obtain l1 u1 where Sa: "Some (l1, u1) = g a" using lift_un_ex[OF assms(1)] by auto
-  have "fst (f l1 u1) \<noteq> None \<and> snd (f l1 u1) \<noteq> None"
-  proof (rule ccontr)
-    assume "\<not> (fst (f l1 u1) \<noteq> None \<and> snd (f l1 u1) \<noteq> None)"
-    hence or: "fst (f l1 u1) = None \<or> snd (f l1 u1) = None" by auto
-    hence "lift_un (g a) f = None" 
-    proof (cases "fst (f l1 u1) = None")
-      case True
-      then obtain b where b: "f l1 u1 = (None, b)" by (cases "f l1 u1", auto)
-      thus ?thesis unfolding Sa[symmetric] lift_un.simps b by auto
-    next
-      case False hence "snd (f l1 u1) = None" using or by auto
-      with False obtain b where b: "f l1 u1 = (Some b, None)" by (cases "f l1 u1", auto)
-      thus ?thesis unfolding Sa[symmetric] lift_un.simps b by auto
-    qed
-    thus False using lift_un_Some by auto
-  qed
-  then obtain a' b' where f: "f l1 u1 = (Some a', Some b')" by (cases "f l1 u1", auto)
-  from lift_un_Some[unfolded Sa[symmetric] lift_un.simps f]
-  have "Some l = fst (f l1 u1)" and "Some u = snd (f l1 u1)" unfolding f by auto
-  thus ?thesis unfolding Sa[symmetric] lift_un.simps using Pa[OF Sa] by auto
-qed
-
-lemma lift_un:
-  assumes lift_un_Some: "Some (l, u) = lift_un (approx' prec a bs) f"
-  and Pa: "\<And>l u. Some (l, u) = approx prec a bs \<Longrightarrow> Ifloat l \<le> Ifloatarith a xs \<and> Ifloatarith a xs \<le> Ifloat u" (is "\<And>l u. _ = ?g a \<Longrightarrow> ?P l u a")
-  shows "\<exists> l1 u1. (Ifloat l1 \<le> Ifloatarith a xs \<and> Ifloatarith a xs \<le> Ifloat u1) \<and> 
-                  Some l = fst (f l1 u1) \<and> Some u = snd (f l1 u1)"
-proof -
-  { fix l u assume "Some (l, u) = approx' prec a bs"
-    with approx_approx'[of prec a bs, OF _ this] Pa
-    have "Ifloat l \<le> Ifloatarith a xs \<and> Ifloatarith a xs \<le> Ifloat u" by auto } note Pa = this
-  from lift_un_f[where g="\<lambda>a. approx' prec a bs" and P = ?P, OF lift_un_Some, OF Pa]
-  show ?thesis by auto
-qed
-
-lemma lift_un_bnds:
-  assumes bnds: "\<forall> x lx ux. (Some l, Some u) = f lx ux \<and> x \<in> { Ifloat lx .. Ifloat ux } \<longrightarrow> Ifloat l \<le> f' x \<and> f' x \<le> Ifloat u"
-  and lift_un_Some: "Some (l, u) = lift_un (approx' prec a bs) f"
-  and Pa: "\<And>l u. Some (l, u) = approx prec a bs \<Longrightarrow> Ifloat l \<le> Ifloatarith a xs \<and> Ifloatarith a xs \<le> Ifloat u"
-  shows "Ifloat l \<le> f' (Ifloatarith a xs) \<and> f' (Ifloatarith a xs) \<le> Ifloat u"
-proof -
-  from lift_un[OF lift_un_Some Pa]
-  obtain l1 u1 where "Ifloat l1 \<le> Ifloatarith a xs" and "Ifloatarith a xs \<le> Ifloat u1" and "Some l = fst (f l1 u1)" and "Some u = snd (f l1 u1)" by blast
-  hence "(Some l, Some u) = f l1 u1" and "Ifloatarith a xs \<in> {Ifloat l1 .. Ifloat u1}" by auto
-  thus ?thesis using bnds by auto
-qed
-
-lemma approx:
-  assumes "bounded_by xs vs"
-  and "Some (l, u) = approx prec arith vs" (is "_ = ?g arith")
-  shows "Ifloat l \<le> Ifloatarith arith xs \<and> Ifloatarith arith xs \<le> Ifloat u" (is "?P l u arith")
-  using `Some (l, u) = approx prec arith vs` 
-proof (induct arith arbitrary: l u x)
-  case (Add a b)
-  from lift_bin'[OF Add.prems[unfolded approx.simps]] Add.hyps
-  obtain l1 u1 l2 u2 where "l = l1 + l2" and "u = u1 + u2"
-    "Ifloat l1 \<le> Ifloatarith a xs" and "Ifloatarith a xs \<le> Ifloat u1"
-    "Ifloat l2 \<le> Ifloatarith b xs" and "Ifloatarith b xs \<le> Ifloat u2" unfolding fst_conv snd_conv by blast
-  thus ?case unfolding Ifloatarith.simps by auto
-next
-  case (Minus a)
-  from lift_un'[OF Minus.prems[unfolded approx.simps]] Minus.hyps
-  obtain l1 u1 where "l = -u1" and "u = -l1"
-    "Ifloat l1 \<le> Ifloatarith a xs" and "Ifloatarith a xs \<le> Ifloat u1" unfolding fst_conv snd_conv by blast
-  thus ?case unfolding Ifloatarith.simps using Ifloat_minus by auto
-next
-  case (Mult a b)
-  from lift_bin'[OF Mult.prems[unfolded approx.simps]] Mult.hyps
-  obtain l1 u1 l2 u2 
-    where l: "l = float_nprt l1 * float_pprt u2 + float_nprt u1 * float_nprt u2 + float_pprt l1 * float_pprt l2 + float_pprt u1 * float_nprt l2"
-    and u: "u = float_pprt u1 * float_pprt u2 + float_pprt l1 * float_nprt u2 + float_nprt u1 * float_pprt l2 + float_nprt l1 * float_nprt l2"
-    and "Ifloat l1 \<le> Ifloatarith a xs" and "Ifloatarith a xs \<le> Ifloat u1"
-    and "Ifloat l2 \<le> Ifloatarith b xs" and "Ifloatarith b xs \<le> Ifloat u2" unfolding fst_conv snd_conv by blast
-  thus ?case unfolding Ifloatarith.simps l u Ifloat_add Ifloat_mult Ifloat_nprt Ifloat_pprt 
-    using mult_le_prts mult_ge_prts by auto
-next
-  case (Inverse a)
-  from lift_un[OF Inverse.prems[unfolded approx.simps], unfolded if_distrib[of fst] if_distrib[of snd] fst_conv snd_conv] Inverse.hyps
-  obtain l1 u1 where l': "Some l = (if 0 < l1 \<or> u1 < 0 then Some (float_divl prec 1 u1) else None)" 
-    and u': "Some u = (if 0 < l1 \<or> u1 < 0 then Some (float_divr prec 1 l1) else None)"
-    and l1: "Ifloat l1 \<le> Ifloatarith a xs" and u1: "Ifloatarith a xs \<le> Ifloat u1" by blast
-  have either: "0 < l1 \<or> u1 < 0" proof (rule ccontr) assume P: "\<not> (0 < l1 \<or> u1 < 0)" show False using l' unfolding if_not_P[OF P] by auto qed
-  moreover have l1_le_u1: "Ifloat l1 \<le> Ifloat u1" using l1 u1 by auto
-  ultimately have "Ifloat l1 \<noteq> 0" and "Ifloat u1 \<noteq> 0" unfolding less_float_def by auto
-
-  have inv: "inverse (Ifloat u1) \<le> inverse (Ifloatarith a xs)
-           \<and> inverse (Ifloatarith a xs) \<le> inverse (Ifloat l1)"
-  proof (cases "0 < l1")
-    case True hence "0 < Ifloat u1" and "0 < Ifloat l1" "0 < Ifloatarith a xs" 
-      unfolding less_float_def using l1_le_u1 l1 by auto
-    show ?thesis
-      unfolding inverse_le_iff_le[OF `0 < Ifloat u1` `0 < Ifloatarith a xs`]
-	inverse_le_iff_le[OF `0 < Ifloatarith a xs` `0 < Ifloat l1`]
-      using l1 u1 by auto
-  next
-    case False hence "u1 < 0" using either by blast
-    hence "Ifloat u1 < 0" and "Ifloat l1 < 0" "Ifloatarith a xs < 0" 
-      unfolding less_float_def using l1_le_u1 u1 by auto
-    show ?thesis
-      unfolding inverse_le_iff_le_neg[OF `Ifloat u1 < 0` `Ifloatarith a xs < 0`]
-	inverse_le_iff_le_neg[OF `Ifloatarith a xs < 0` `Ifloat l1 < 0`]
-      using l1 u1 by auto
-  qed
-    
-  from l' have "l = float_divl prec 1 u1" by (cases "0 < l1 \<or> u1 < 0", auto)
-  hence "Ifloat l \<le> inverse (Ifloat u1)" unfolding nonzero_inverse_eq_divide[OF `Ifloat u1 \<noteq> 0`] using float_divl[of prec 1 u1] by auto
-  also have "\<dots> \<le> inverse (Ifloatarith a xs)" using inv by auto
-  finally have "Ifloat l \<le> inverse (Ifloatarith a xs)" .
-  moreover
-  from u' have "u = float_divr prec 1 l1" by (cases "0 < l1 \<or> u1 < 0", auto)
-  hence "inverse (Ifloat l1) \<le> Ifloat u" unfolding nonzero_inverse_eq_divide[OF `Ifloat l1 \<noteq> 0`] using float_divr[of 1 l1 prec] by auto
-  hence "inverse (Ifloatarith a xs) \<le> Ifloat u" by (rule order_trans[OF inv[THEN conjunct2]])
-  ultimately show ?case unfolding Ifloatarith.simps using l1 u1 by auto
-next
-  case (Abs x)
-  from lift_un'[OF Abs.prems[unfolded approx.simps], unfolded fst_conv snd_conv] Abs.hyps
-  obtain l1 u1 where l': "l = (if l1 < 0 \<and> 0 < u1 then 0 else min \<bar>l1\<bar> \<bar>u1\<bar>)" and u': "u = max \<bar>l1\<bar> \<bar>u1\<bar>"
-    and l1: "Ifloat l1 \<le> Ifloatarith x xs" and u1: "Ifloatarith x xs \<le> Ifloat u1" by blast
-  thus ?case unfolding l' u' by (cases "l1 < 0 \<and> 0 < u1", auto simp add: Ifloat_min Ifloat_max Ifloat_abs less_float_def)
-next
-  case (Min a b)
-  from lift_bin'[OF Min.prems[unfolded approx.simps], unfolded fst_conv snd_conv] Min.hyps
-  obtain l1 u1 l2 u2 where l': "l = min l1 l2" and u': "u = min u1 u2"
-    and l1: "Ifloat l1 \<le> Ifloatarith a xs" and u1: "Ifloatarith a xs \<le> Ifloat u1"
-    and l1: "Ifloat l2 \<le> Ifloatarith b xs" and u1: "Ifloatarith b xs \<le> Ifloat u2" by blast
-  thus ?case unfolding l' u' by (auto simp add: Ifloat_min)
-next
-  case (Max a b)
-  from lift_bin'[OF Max.prems[unfolded approx.simps], unfolded fst_conv snd_conv] Max.hyps
-  obtain l1 u1 l2 u2 where l': "l = max l1 l2" and u': "u = max u1 u2"
-    and l1: "Ifloat l1 \<le> Ifloatarith a xs" and u1: "Ifloatarith a xs \<le> Ifloat u1"
-    and l1: "Ifloat l2 \<le> Ifloatarith b xs" and u1: "Ifloatarith b xs \<le> Ifloat u2" by blast
-  thus ?case unfolding l' u' by (auto simp add: Ifloat_max)
-next case (Sin a) with lift_un'_bnds[OF bnds_sin] show ?case by auto
-next case (Cos a) with lift_un'_bnds[OF bnds_cos] show ?case by auto
-next case (Arctan a) with lift_un'_bnds[OF bnds_arctan] show ?case by auto
-next case Pi with pi_boundaries show ?case by auto
-next case (Sqrt a) with lift_un_bnds[OF bnds_sqrt] show ?case by auto
-next case (Exp a) with lift_un'_bnds[OF bnds_exp] show ?case by auto
-next case (Ln a) with lift_un_bnds[OF bnds_ln] show ?case by auto
-next case (Power a n) with lift_un'_bnds[OF bnds_power] show ?case by auto
-next case (Num f) thus ?case by auto
-next
-  case (Atom n) 
-  show ?case
-  proof (cases "n < length vs")
-    case True
-    with Atom have "vs ! n = (l, u)" by auto
-    thus ?thesis using bounded_by[OF assms(1) True] by auto
-  next
-    case False thus ?thesis using Atom by auto
-  qed
-qed
-
-datatype ApproxEq = Less floatarith floatarith 
-                  | LessEqual floatarith floatarith 
-
-fun uneq :: "ApproxEq \<Rightarrow> real list \<Rightarrow> bool" where 
-"uneq (Less a b) vs                   = (Ifloatarith a vs < Ifloatarith b vs)" |
-"uneq (LessEqual a b) vs              = (Ifloatarith a vs \<le> Ifloatarith b vs)"
-
-fun uneq' :: "nat \<Rightarrow> ApproxEq \<Rightarrow> (float * float) list \<Rightarrow> bool" where 
-"uneq' prec (Less a b) bs = (case (approx prec a bs, approx prec b bs) of (Some (l, u), Some (l', u')) \<Rightarrow> u < l' | _ \<Rightarrow> False)" |
-"uneq' prec (LessEqual a b) bs = (case (approx prec a bs, approx prec b bs) of (Some (l, u), Some (l', u')) \<Rightarrow> u \<le> l' | _ \<Rightarrow> False)"
-
-lemma uneq_approx: fixes m :: nat assumes "bounded_by vs bs" and "uneq' prec eq bs"
-  shows "uneq eq vs"
-proof (cases eq)
-  case (Less a b)
-  show ?thesis
-  proof (cases "\<exists> u l u' l'. approx prec a bs = Some (l, u) \<and> 
-                             approx prec b bs = Some (l', u')")
-    case True
-    then obtain l u l' u' where a_approx: "approx prec a bs = Some (l, u)"
-      and b_approx: "approx prec b bs = Some (l', u') " by auto
-    with `uneq' prec eq bs` have "Ifloat u < Ifloat l'"
-      unfolding Less uneq'.simps less_float_def by auto
-    moreover from a_approx[symmetric] and b_approx[symmetric] and `bounded_by vs bs`
-    have "Ifloatarith a vs \<le> Ifloat u" and "Ifloat l' \<le> Ifloatarith b vs"
-      using approx by auto
-    ultimately show ?thesis unfolding uneq.simps Less by auto
-  next
-    case False
-    hence "approx prec a bs = None \<or> approx prec b bs = None"
-      unfolding not_Some_eq[symmetric] by auto
-    hence "\<not> uneq' prec eq bs" unfolding Less uneq'.simps 
-      by (cases "approx prec a bs = None", auto)
-    thus ?thesis using assms by auto
-  qed
-next
-  case (LessEqual a b)
-  show ?thesis
-  proof (cases "\<exists> u l u' l'. approx prec a bs = Some (l, u) \<and> 
-                             approx prec b bs = Some (l', u')")
-    case True
-    then obtain l u l' u' where a_approx: "approx prec a bs = Some (l, u)"
-      and b_approx: "approx prec b bs = Some (l', u') " by auto
-    with `uneq' prec eq bs` have "Ifloat u \<le> Ifloat l'"
-      unfolding LessEqual uneq'.simps le_float_def by auto
-    moreover from a_approx[symmetric] and b_approx[symmetric] and `bounded_by vs bs`
-    have "Ifloatarith a vs \<le> Ifloat u" and "Ifloat l' \<le> Ifloatarith b vs"
-      using approx by auto
-    ultimately show ?thesis unfolding uneq.simps LessEqual by auto
-  next
-    case False
-    hence "approx prec a bs = None \<or> approx prec b bs = None"
-      unfolding not_Some_eq[symmetric] by auto
-    hence "\<not> uneq' prec eq bs" unfolding LessEqual uneq'.simps 
-      by (cases "approx prec a bs = None", auto)
-    thus ?thesis using assms by auto
-  qed
-qed
-
-lemma Ifloatarith_divide: "Ifloatarith (Mult a (Inverse b)) vs = (Ifloatarith a vs) / (Ifloatarith b vs)"
-  unfolding real_divide_def Ifloatarith.simps ..
-
-lemma Ifloatarith_diff: "Ifloatarith (Add a (Minus b)) vs = (Ifloatarith a vs) - (Ifloatarith b vs)"
-  unfolding real_diff_def Ifloatarith.simps ..
-
-lemma Ifloatarith_tan: "Ifloatarith (Mult (Sin a) (Inverse (Cos a))) vs = tan (Ifloatarith a vs)"
-  unfolding tan_def Ifloatarith.simps real_divide_def ..
-
-lemma Ifloatarith_powr: "Ifloatarith (Exp (Mult b (Ln a))) vs = (Ifloatarith a vs) powr (Ifloatarith b vs)"
-  unfolding powr_def Ifloatarith.simps ..
-
-lemma Ifloatarith_log: "Ifloatarith ((Mult (Ln x) (Inverse (Ln b)))) vs = log (Ifloatarith b vs) (Ifloatarith x vs)"
-  unfolding log_def Ifloatarith.simps real_divide_def ..
-
-lemma Ifloatarith_num: shows "Ifloatarith (Num (Float 0 0)) vs = 0" and "Ifloatarith (Num (Float 1 0)) vs = 1" and "Ifloatarith (Num (Float (number_of a) 0)) vs = number_of a" by auto
-
-subsection {* Implement proof method \texttt{approximation} *}
-
-lemma bounded_divl: assumes "Ifloat a / Ifloat b \<le> x" shows "Ifloat (float_divl p a b) \<le> x" by (rule order_trans[OF _ assms], rule float_divl)
-lemma bounded_divr: assumes "x \<le> Ifloat a / Ifloat b" shows "x \<le> Ifloat (float_divr p a b)" by (rule order_trans[OF assms _], rule float_divr)
-lemma bounded_num: shows "Ifloat (Float 5 1) = 10" and "Ifloat (Float 0 0) = 0" and "Ifloat (Float 1 0) = 1" and "Ifloat (Float (number_of n) 0) = (number_of n)"
-                     and "0 * pow2 e = Ifloat (Float 0 e)" and "1 * pow2 e = Ifloat (Float 1 e)" and "number_of m * pow2 e = Ifloat (Float (number_of m) e)"
-  by (auto simp add: Ifloat.simps pow2_def)
-
-lemmas bounded_by_equations = bounded_by_Cons bounded_by_Nil float_power bounded_divl bounded_divr bounded_num HOL.simp_thms
-lemmas uneq_equations = uneq.simps Ifloatarith.simps Ifloatarith_num Ifloatarith_divide Ifloatarith_diff Ifloatarith_tan Ifloatarith_powr Ifloatarith_log
-
-lemma "x div (0::int) = 0" by auto -- "What happens in the zero case for div"
-lemma "x mod (0::int) = x" by auto -- "What happens in the zero case for mod"
-
-text {* The following equations must hold for div & mod 
-        -- see "The Definition of Standard ML" by R. Milner, M. Tofte and R. Harper (pg. 79) *}
-lemma "d * (i div d) + i mod d = (i::int)" by auto
-lemma "0 < (d :: int) \<Longrightarrow> 0 \<le> i mod d \<and> i mod d < d" by auto
-lemma "(d :: int) < 0 \<Longrightarrow> d < i mod d \<and> i mod d \<le> 0" by auto
-
-code_const "op div :: int \<Rightarrow> int \<Rightarrow> int" (SML "(fn i => fn d => if d = 0 then 0 else i div d)")
-code_const "op mod :: int \<Rightarrow> int \<Rightarrow> int" (SML "(fn i => fn d => if d = 0 then i else i mod d)")
-code_const "divmod :: int \<Rightarrow> int \<Rightarrow> (int * int)" (SML "(fn i => fn d => if d = 0 then (0, i) else IntInf.divMod (i, d))")
-
-ML {*
-  val uneq_equations = PureThy.get_thms @{theory} "uneq_equations";
-  val bounded_by_equations = PureThy.get_thms @{theory} "bounded_by_equations";
-  val bounded_by_simpset = (HOL_basic_ss addsimps bounded_by_equations)
-
-  fun reify_uneq ctxt i = (fn st =>
-    let
-      val to = HOLogic.dest_Trueprop (Logic.strip_imp_concl (List.nth (prems_of st, i - 1)))
-    in (Reflection.genreify_tac ctxt uneq_equations (SOME to) i) st
-    end)
-
-  fun rule_uneq ctxt prec i thm = let
-    fun conv_num typ = HOLogic.dest_number #> snd #> HOLogic.mk_number typ
-    val to_natc = conv_num @{typ "nat"} #> Thm.cterm_of (ProofContext.theory_of ctxt)
-    val to_nat = conv_num @{typ "nat"}
-    val to_int = conv_num @{typ "int"}
-
-    val prec' = to_nat prec
-
-    fun bot_float (Const (@{const_name "times"}, _) $ mantisse $ (Const (@{const_name "pow2"}, _) $ exp))
-                   = @{term "Float"} $ to_int mantisse $ to_int exp
-      | bot_float (Const (@{const_name "divide"}, _) $ mantisse $ (Const (@{const_name "power"}, _) $ ten $ exp))
-                   = @{term "float_divl"} $ prec' $ (@{term "Float"} $ to_int mantisse $ @{term "0 :: int"}) $ (@{term "power_float (Float 5 1)"} $ to_nat exp)
-      | bot_float (Const (@{const_name "divide"}, _) $ mantisse $ ten)
-                   = @{term "float_divl"} $ prec' $ (@{term "Float"} $ to_int mantisse $ @{term "0 :: int"}) $ @{term "Float 5 1"}
-      | bot_float mantisse = @{term "Float"} $ to_int mantisse $ @{term "0 :: int"}
-
-    fun top_float (Const (@{const_name "times"}, _) $ mantisse $ (Const (@{const_name "pow2"}, _) $ exp))
-                   = @{term "Float"} $ to_int mantisse $ to_int exp
-      | top_float (Const (@{const_name "divide"}, _) $ mantisse $ (Const (@{const_name "power"}, _) $ ten $ exp))
-                   = @{term "float_divr"} $ prec' $ (@{term "Float"} $ to_int mantisse $ @{term "0 :: int"}) $ (@{term "power_float (Float 5 1)"} $ to_nat exp)
-      | top_float (Const (@{const_name "divide"}, _) $ mantisse $ ten)
-                   = @{term "float_divr"} $ prec' $ (@{term "Float"} $ to_int mantisse $ @{term "0 :: int"}) $ @{term "Float 5 1"}
-      | top_float mantisse = @{term "Float"} $ to_int mantisse $ @{term "0 :: int"}
-
-    val goal' : term = List.nth (prems_of thm, i - 1)
-
-    fun lift_bnd (t as (Const (@{const_name "op &"}, _) $ 
-                        (Const (@{const_name "less_eq"}, _) $ 
-                         bottom $ (Free (name, _))) $ 
-                        (Const (@{const_name "less_eq"}, _) $ _ $ top)))
-         = ((name, HOLogic.mk_prod (bot_float bottom, top_float top))
-            handle TERM (txt, ts) => raise TERM ("Premisse needs format '<num> <= <var> & <var> <= <num>', but found " ^
-                                  (Syntax.string_of_term ctxt t), [t]))
-      | lift_bnd t = raise TERM ("Premisse needs format '<num> <= <var> & <var> <= <num>', but found " ^
-                                 (Syntax.string_of_term ctxt t), [t])
-    val bound_eqs = map (HOLogic.dest_Trueprop #> lift_bnd)  (Logic.strip_imp_prems goal')
-
-    fun lift_var (Free (varname, _)) = (case AList.lookup (op =) bound_eqs varname of
-                                          SOME bound => bound
-                                        | NONE => raise TERM ("No bound equations found for " ^ varname, []))
-      | lift_var t = raise TERM ("Can not convert expression " ^ 
-                                 (Syntax.string_of_term ctxt t), [t])
-
-    val _ $ vs = HOLogic.dest_Trueprop (Logic.strip_imp_concl goal')
-
-    val bs = (HOLogic.dest_list #> map lift_var #> HOLogic.mk_list @{typ "float * float"}) vs
-    val map = [(@{cpat "?prec::nat"}, to_natc prec),
-               (@{cpat "?bs::(float * float) list"}, Thm.cterm_of (ProofContext.theory_of ctxt) bs)]
-  in rtac (Thm.instantiate ([], map) @{thm "uneq_approx"}) i thm end
-
-  val eval_tac = CSUBGOAL (fn (ct, i) => rtac (eval_oracle ct) i)
-
-  fun gen_eval_tac conv ctxt = CONVERSION (Conv.params_conv (~1) (K (Conv.concl_conv (~1) conv)) ctxt)
-                               THEN' rtac TrueI
-
-*}
-
-method_setup approximation = {* fn src => 
-  Method.syntax Args.term src #>
-  (fn (prec, ctxt) => let
-   in Method.SIMPLE_METHOD' (fn i =>
-     (DETERM (reify_uneq ctxt i)
-      THEN rule_uneq ctxt prec i
-      THEN Simplifier.asm_full_simp_tac bounded_by_simpset i 
-      THEN (TRY (filter_prems_tac (fn t => false) i))
-      THEN (gen_eval_tac eval_oracle ctxt) i))
-   end)
-*} "real number approximation"
-
-end
--- a/src/HOL/Reflection/Cooper.thy	Mon Feb 16 10:13:30 2009 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,2174 +0,0 @@
-(*  Title:      HOL/Reflection/Cooper.thy
-    Author:     Amine Chaieb
-*)
-
-theory Cooper
-imports Complex_Main Efficient_Nat
-uses ("cooper_tac.ML")
-begin
-
-function iupt :: "int \<Rightarrow> int \<Rightarrow> int list" where
-  "iupt i j = (if j < i then [] else i # iupt (i+1) j)"
-by pat_completeness auto
-termination by (relation "measure (\<lambda> (i, j). nat (j-i+1))") auto
-
-lemma iupt_set: "set (iupt i j) = {i..j}"
-  by (induct rule: iupt.induct) (simp add: simp_from_to)
-
-(* Periodicity of dvd *)
-
-  (*********************************************************************************)
-  (****                            SHADOW SYNTAX AND SEMANTICS                  ****)
-  (*********************************************************************************)
-
-datatype num = C int | Bound nat | CN nat int num | Neg num | Add num num| Sub num num 
-  | Mul int num
-
-  (* A size for num to make inductive proofs simpler*)
-primrec num_size :: "num \<Rightarrow> nat" where
-  "num_size (C c) = 1"
-| "num_size (Bound n) = 1"
-| "num_size (Neg a) = 1 + num_size a"
-| "num_size (Add a b) = 1 + num_size a + num_size b"
-| "num_size (Sub a b) = 3 + num_size a + num_size b"
-| "num_size (CN n c a) = 4 + num_size a"
-| "num_size (Mul c a) = 1 + num_size a"
-
-primrec Inum :: "int list \<Rightarrow> num \<Rightarrow> int" where
-  "Inum bs (C c) = c"
-| "Inum bs (Bound n) = bs!n"
-| "Inum bs (CN n c a) = c * (bs!n) + (Inum bs a)"
-| "Inum bs (Neg a) = -(Inum bs a)"
-| "Inum bs (Add a b) = Inum bs a + Inum bs b"
-| "Inum bs (Sub a b) = Inum bs a - Inum bs b"
-| "Inum bs (Mul c a) = c* Inum bs a"
-
-datatype fm  = 
-  T| F| Lt num| Le num| Gt num| Ge num| Eq num| NEq num| Dvd int num| NDvd int num|
-  NOT fm| And fm fm|  Or fm fm| Imp fm fm| Iff fm fm| E fm| A fm 
-  | Closed nat | NClosed nat
-
-
-  (* A size for fm *)
-consts fmsize :: "fm \<Rightarrow> nat"
-recdef fmsize "measure size"
-  "fmsize (NOT p) = 1 + fmsize p"
-  "fmsize (And p q) = 1 + fmsize p + fmsize q"
-  "fmsize (Or p q) = 1 + fmsize p + fmsize q"
-  "fmsize (Imp p q) = 3 + fmsize p + fmsize q"
-  "fmsize (Iff p q) = 3 + 2*(fmsize p + fmsize q)"
-  "fmsize (E p) = 1 + fmsize p"
-  "fmsize (A p) = 4+ fmsize p"
-  "fmsize (Dvd i t) = 2"
-  "fmsize (NDvd i t) = 2"
-  "fmsize p = 1"
-  (* several lemmas about fmsize *)
-lemma fmsize_pos: "fmsize p > 0"	
-by (induct p rule: fmsize.induct) simp_all
-
-  (* Semantics of formulae (fm) *)
-consts Ifm ::"bool list \<Rightarrow> int list \<Rightarrow> fm \<Rightarrow> bool"
-primrec
-  "Ifm bbs bs T = True"
-  "Ifm bbs bs F = False"
-  "Ifm bbs bs (Lt a) = (Inum bs a < 0)"
-  "Ifm bbs bs (Gt a) = (Inum bs a > 0)"
-  "Ifm bbs bs (Le a) = (Inum bs a \<le> 0)"
-  "Ifm bbs bs (Ge a) = (Inum bs a \<ge> 0)"
-  "Ifm bbs bs (Eq a) = (Inum bs a = 0)"
-  "Ifm bbs bs (NEq a) = (Inum bs a \<noteq> 0)"
-  "Ifm bbs bs (Dvd i b) = (i dvd Inum bs b)"
-  "Ifm bbs bs (NDvd i b) = (\<not>(i dvd Inum bs b))"
-  "Ifm bbs bs (NOT p) = (\<not> (Ifm bbs bs p))"
-  "Ifm bbs bs (And p q) = (Ifm bbs bs p \<and> Ifm bbs bs q)"
-  "Ifm bbs bs (Or p q) = (Ifm bbs bs p \<or> Ifm bbs bs q)"
-  "Ifm bbs bs (Imp p q) = ((Ifm bbs bs p) \<longrightarrow> (Ifm bbs bs q))"
-  "Ifm bbs bs (Iff p q) = (Ifm bbs bs p = Ifm bbs bs q)"
-  "Ifm bbs bs (E p) = (\<exists> x. Ifm bbs (x#bs) p)"
-  "Ifm bbs bs (A p) = (\<forall> x. Ifm bbs (x#bs) p)"
-  "Ifm bbs bs (Closed n) = bbs!n"
-  "Ifm bbs bs (NClosed n) = (\<not> bbs!n)"
-
-consts prep :: "fm \<Rightarrow> fm"
-recdef prep "measure fmsize"
-  "prep (E T) = T"
-  "prep (E F) = F"
-  "prep (E (Or p q)) = Or (prep (E p)) (prep (E q))"
-  "prep (E (Imp p q)) = Or (prep (E (NOT p))) (prep (E q))"
-  "prep (E (Iff p q)) = Or (prep (E (And p q))) (prep (E (And (NOT p) (NOT q))))" 
-  "prep (E (NOT (And p q))) = Or (prep (E (NOT p))) (prep (E(NOT q)))"
-  "prep (E (NOT (Imp p q))) = prep (E (And p (NOT q)))"
-  "prep (E (NOT (Iff p q))) = Or (prep (E (And p (NOT q)))) (prep (E(And (NOT p) q)))"
-  "prep (E p) = E (prep p)"
-  "prep (A (And p q)) = And (prep (A p)) (prep (A q))"
-  "prep (A p) = prep (NOT (E (NOT p)))"
-  "prep (NOT (NOT p)) = prep p"
-  "prep (NOT (And p q)) = Or (prep (NOT p)) (prep (NOT q))"
-  "prep (NOT (A p)) = prep (E (NOT p))"
-  "prep (NOT (Or p q)) = And (prep (NOT p)) (prep (NOT q))"
-  "prep (NOT (Imp p q)) = And (prep p) (prep (NOT q))"
-  "prep (NOT (Iff p q)) = Or (prep (And p (NOT q))) (prep (And (NOT p) q))"
-  "prep (NOT p) = NOT (prep p)"
-  "prep (Or p q) = Or (prep p) (prep q)"
-  "prep (And p q) = And (prep p) (prep q)"
-  "prep (Imp p q) = prep (Or (NOT p) q)"
-  "prep (Iff p q) = Or (prep (And p q)) (prep (And (NOT p) (NOT q)))"
-  "prep p = p"
-(hints simp add: fmsize_pos)
-lemma prep: "Ifm bbs bs (prep p) = Ifm bbs bs p"
-by (induct p arbitrary: bs rule: prep.induct, auto)
-
-
-  (* Quantifier freeness *)
-consts qfree:: "fm \<Rightarrow> bool"
-recdef qfree "measure size"
-  "qfree (E p) = False"
-  "qfree (A p) = False"
-  "qfree (NOT p) = qfree p" 
-  "qfree (And p q) = (qfree p \<and> qfree q)" 
-  "qfree (Or  p q) = (qfree p \<and> qfree q)" 
-  "qfree (Imp p q) = (qfree p \<and> qfree q)" 
-  "qfree (Iff p q) = (qfree p \<and> qfree q)"
-  "qfree p = True"
-
-  (* Boundedness and substitution *)
-consts 
-  numbound0:: "num \<Rightarrow> bool" (* a num is INDEPENDENT of Bound 0 *)
-  bound0:: "fm \<Rightarrow> bool" (* A Formula is independent of Bound 0 *)
-  subst0:: "num \<Rightarrow> fm \<Rightarrow> fm" (* substitue a num into a formula for Bound 0 *)
-primrec
-  "numbound0 (C c) = True"
-  "numbound0 (Bound n) = (n>0)"
-  "numbound0 (CN n i a) = (n >0 \<and> numbound0 a)"
-  "numbound0 (Neg a) = numbound0 a"
-  "numbound0 (Add a b) = (numbound0 a \<and> numbound0 b)"
-  "numbound0 (Sub a b) = (numbound0 a \<and> numbound0 b)" 
-  "numbound0 (Mul i a) = numbound0 a"
-
-lemma numbound0_I:
-  assumes nb: "numbound0 a"
-  shows "Inum (b#bs) a = Inum (b'#bs) a"
-using nb
-by (induct a rule: numbound0.induct) (auto simp add: gr0_conv_Suc)
-
-primrec
-  "bound0 T = True"
-  "bound0 F = True"
-  "bound0 (Lt a) = numbound0 a"
-  "bound0 (Le a) = numbound0 a"
-  "bound0 (Gt a) = numbound0 a"
-  "bound0 (Ge a) = numbound0 a"
-  "bound0 (Eq a) = numbound0 a"
-  "bound0 (NEq a) = numbound0 a"
-  "bound0 (Dvd i a) = numbound0 a"
-  "bound0 (NDvd i a) = numbound0 a"
-  "bound0 (NOT p) = bound0 p"
-  "bound0 (And p q) = (bound0 p \<and> bound0 q)"
-  "bound0 (Or p q) = (bound0 p \<and> bound0 q)"
-  "bound0 (Imp p q) = ((bound0 p) \<and> (bound0 q))"
-  "bound0 (Iff p q) = (bound0 p \<and> bound0 q)"
-  "bound0 (E p) = False"
-  "bound0 (A p) = False"
-  "bound0 (Closed P) = True"
-  "bound0 (NClosed P) = True"
-lemma bound0_I:
-  assumes bp: "bound0 p"
-  shows "Ifm bbs (b#bs) p = Ifm bbs (b'#bs) p"
-using bp numbound0_I[where b="b" and bs="bs" and b'="b'"]
-by (induct p rule: bound0.induct) (auto simp add: gr0_conv_Suc)
-
-fun numsubst0:: "num \<Rightarrow> num \<Rightarrow> num" where
-  "numsubst0 t (C c) = (C c)"
-| "numsubst0 t (Bound n) = (if n=0 then t else Bound n)"
-| "numsubst0 t (CN 0 i a) = Add (Mul i t) (numsubst0 t a)"
-| "numsubst0 t (CN n i a) = CN n i (numsubst0 t a)"
-| "numsubst0 t (Neg a) = Neg (numsubst0 t a)"
-| "numsubst0 t (Add a b) = Add (numsubst0 t a) (numsubst0 t b)"
-| "numsubst0 t (Sub a b) = Sub (numsubst0 t a) (numsubst0 t b)" 
-| "numsubst0 t (Mul i a) = Mul i (numsubst0 t a)"
-
-lemma numsubst0_I:
-  "Inum (b#bs) (numsubst0 a t) = Inum ((Inum (b#bs) a)#bs) t"
-by (induct t rule: numsubst0.induct,auto simp:nth_Cons')
-
-lemma numsubst0_I':
-  "numbound0 a \<Longrightarrow> Inum (b#bs) (numsubst0 a t) = Inum ((Inum (b'#bs) a)#bs) t"
-by (induct t rule: numsubst0.induct, auto simp: nth_Cons' numbound0_I[where b="b" and b'="b'"])
-
-primrec
-  "subst0 t T = T"
-  "subst0 t F = F"
-  "subst0 t (Lt a) = Lt (numsubst0 t a)"
-  "subst0 t (Le a) = Le (numsubst0 t a)"
-  "subst0 t (Gt a) = Gt (numsubst0 t a)"
-  "subst0 t (Ge a) = Ge (numsubst0 t a)"
-  "subst0 t (Eq a) = Eq (numsubst0 t a)"
-  "subst0 t (NEq a) = NEq (numsubst0 t a)"
-  "subst0 t (Dvd i a) = Dvd i (numsubst0 t a)"
-  "subst0 t (NDvd i a) = NDvd i (numsubst0 t a)"
-  "subst0 t (NOT p) = NOT (subst0 t p)"
-  "subst0 t (And p q) = And (subst0 t p) (subst0 t q)"
-  "subst0 t (Or p q) = Or (subst0 t p) (subst0 t q)"
-  "subst0 t (Imp p q) = Imp (subst0 t p) (subst0 t q)"
-  "subst0 t (Iff p q) = Iff (subst0 t p) (subst0 t q)"
-  "subst0 t (Closed P) = (Closed P)"
-  "subst0 t (NClosed P) = (NClosed P)"
-
-lemma subst0_I: assumes qfp: "qfree p"
-  shows "Ifm bbs (b#bs) (subst0 a p) = Ifm bbs ((Inum (b#bs) a)#bs) p"
-  using qfp numsubst0_I[where b="b" and bs="bs" and a="a"]
-  by (induct p) (simp_all add: gr0_conv_Suc)
-
-
-consts 
-  decrnum:: "num \<Rightarrow> num" 
-  decr :: "fm \<Rightarrow> fm"
-
-recdef decrnum "measure size"
-  "decrnum (Bound n) = Bound (n - 1)"
-  "decrnum (Neg a) = Neg (decrnum a)"
-  "decrnum (Add a b) = Add (decrnum a) (decrnum b)"
-  "decrnum (Sub a b) = Sub (decrnum a) (decrnum b)"
-  "decrnum (Mul c a) = Mul c (decrnum a)"
-  "decrnum (CN n i a) = (CN (n - 1) i (decrnum a))"
-  "decrnum a = a"
-
-recdef decr "measure size"
-  "decr (Lt a) = Lt (decrnum a)"
-  "decr (Le a) = Le (decrnum a)"
-  "decr (Gt a) = Gt (decrnum a)"
-  "decr (Ge a) = Ge (decrnum a)"
-  "decr (Eq a) = Eq (decrnum a)"
-  "decr (NEq a) = NEq (decrnum a)"
-  "decr (Dvd i a) = Dvd i (decrnum a)"
-  "decr (NDvd i a) = NDvd i (decrnum a)"
-  "decr (NOT p) = NOT (decr p)" 
-  "decr (And p q) = And (decr p) (decr q)"
-  "decr (Or p q) = Or (decr p) (decr q)"
-  "decr (Imp p q) = Imp (decr p) (decr q)"
-  "decr (Iff p q) = Iff (decr p) (decr q)"
-  "decr p = p"
-
-lemma decrnum: assumes nb: "numbound0 t"
-  shows "Inum (x#bs) t = Inum bs (decrnum t)"
-  using nb by (induct t rule: decrnum.induct, auto simp add: gr0_conv_Suc)
-
-lemma decr: assumes nb: "bound0 p"
-  shows "Ifm bbs (x#bs) p = Ifm bbs bs (decr p)"
-  using nb 
-  by (induct p rule: decr.induct, simp_all add: gr0_conv_Suc decrnum)
-
-lemma decr_qf: "bound0 p \<Longrightarrow> qfree (decr p)"
-by (induct p, simp_all)
-
-consts 
-  isatom :: "fm \<Rightarrow> bool" (* test for atomicity *)
-recdef isatom "measure size"
-  "isatom T = True"
-  "isatom F = True"
-  "isatom (Lt a) = True"
-  "isatom (Le a) = True"
-  "isatom (Gt a) = True"
-  "isatom (Ge a) = True"
-  "isatom (Eq a) = True"
-  "isatom (NEq a) = True"
-  "isatom (Dvd i b) = True"
-  "isatom (NDvd i b) = True"
-  "isatom (Closed P) = True"
-  "isatom (NClosed P) = True"
-  "isatom p = False"
-
-lemma numsubst0_numbound0: assumes nb: "numbound0 t"
-  shows "numbound0 (numsubst0 t a)"
-using nb apply (induct a rule: numbound0.induct)
-apply simp_all
-apply (case_tac n, simp_all)
-done
-
-lemma subst0_bound0: assumes qf: "qfree p" and nb: "numbound0 t"
-  shows "bound0 (subst0 t p)"
-using qf numsubst0_numbound0[OF nb] by (induct p  rule: subst0.induct, auto)
-
-lemma bound0_qf: "bound0 p \<Longrightarrow> qfree p"
-by (induct p, simp_all)
-
-
-constdefs djf:: "('a \<Rightarrow> fm) \<Rightarrow> 'a \<Rightarrow> fm \<Rightarrow> fm"
-  "djf f p q \<equiv> (if q=T then T else if q=F then f p else 
-  (let fp = f p in case fp of T \<Rightarrow> T | F \<Rightarrow> q | _ \<Rightarrow> Or (f p) q))"
-constdefs evaldjf:: "('a \<Rightarrow> fm) \<Rightarrow> 'a list \<Rightarrow> fm"
-  "evaldjf f ps \<equiv> foldr (djf f) ps F"
-
-lemma djf_Or: "Ifm bbs bs (djf f p q) = Ifm bbs bs (Or (f p) q)"
-by (cases "q=T", simp add: djf_def,cases "q=F",simp add: djf_def) 
-(cases "f p", simp_all add: Let_def djf_def) 
-
-lemma evaldjf_ex: "Ifm bbs bs (evaldjf f ps) = (\<exists> p \<in> set ps. Ifm bbs bs (f p))"
-  by(induct ps, simp_all add: evaldjf_def djf_Or)
-
-lemma evaldjf_bound0: 
-  assumes nb: "\<forall> x\<in> set xs. bound0 (f x)"
-  shows "bound0 (evaldjf f xs)"
-  using nb by (induct xs, auto simp add: evaldjf_def djf_def Let_def) (case_tac "f a", auto) 
-
-lemma evaldjf_qf: 
-  assumes nb: "\<forall> x\<in> set xs. qfree (f x)"
-  shows "qfree (evaldjf f xs)"
-  using nb by (induct xs, auto simp add: evaldjf_def djf_def Let_def) (case_tac "f a", auto) 
-
-consts disjuncts :: "fm \<Rightarrow> fm list"
-recdef disjuncts "measure size"
-  "disjuncts (Or p q) = (disjuncts p) @ (disjuncts q)"
-  "disjuncts F = []"
-  "disjuncts p = [p]"
-
-lemma disjuncts: "(\<exists> q\<in> set (disjuncts p). Ifm bbs bs q) = Ifm bbs bs p"
-by(induct p rule: disjuncts.induct, auto)
-
-lemma disjuncts_nb: "bound0 p \<Longrightarrow> \<forall> q\<in> set (disjuncts p). bound0 q"
-proof-
-  assume nb: "bound0 p"
-  hence "list_all bound0 (disjuncts p)" by (induct p rule:disjuncts.induct,auto)
-  thus ?thesis by (simp only: list_all_iff)
-qed
-
-lemma disjuncts_qf: "qfree p \<Longrightarrow> \<forall> q\<in> set (disjuncts p). qfree q"
-proof-
-  assume qf: "qfree p"
-  hence "list_all qfree (disjuncts p)"
-    by (induct p rule: disjuncts.induct, auto)
-  thus ?thesis by (simp only: list_all_iff)
-qed
-
-constdefs DJ :: "(fm \<Rightarrow> fm) \<Rightarrow> fm \<Rightarrow> fm"
-  "DJ f p \<equiv> evaldjf f (disjuncts p)"
-
-lemma DJ: assumes fdj: "\<forall> p q. f (Or p q) = Or (f p) (f q)"
-  and fF: "f F = F"
-  shows "Ifm bbs bs (DJ f p) = Ifm bbs bs (f p)"
-proof-
-  have "Ifm bbs bs (DJ f p) = (\<exists> q \<in> set (disjuncts p). Ifm bbs bs (f q))"
-    by (simp add: DJ_def evaldjf_ex) 
-  also have "\<dots> = Ifm bbs bs (f p)" using fdj fF by (induct p rule: disjuncts.induct, auto)
-  finally show ?thesis .
-qed
-
-lemma DJ_qf: assumes 
-  fqf: "\<forall> p. qfree p \<longrightarrow> qfree (f p)"
-  shows "\<forall>p. qfree p \<longrightarrow> qfree (DJ f p) "
-proof(clarify)
-  fix  p assume qf: "qfree p"
-  have th: "DJ f p = evaldjf f (disjuncts p)" by (simp add: DJ_def)
-  from disjuncts_qf[OF qf] have "\<forall> q\<in> set (disjuncts p). qfree q" .
-  with fqf have th':"\<forall> q\<in> set (disjuncts p). qfree (f q)" by blast
-  
-  from evaldjf_qf[OF th'] th show "qfree (DJ f p)" by simp
-qed
-
-lemma DJ_qe: assumes qe: "\<forall> bs p. qfree p \<longrightarrow> qfree (qe p) \<and> (Ifm bbs bs (qe p) = Ifm bbs bs (E p))"
-  shows "\<forall> bs p. qfree p \<longrightarrow> qfree (DJ qe p) \<and> (Ifm bbs bs ((DJ qe p)) = Ifm bbs bs (E p))"
-proof(clarify)
-  fix p::fm and bs
-  assume qf: "qfree p"
-  from qe have qth: "\<forall> p. qfree p \<longrightarrow> qfree (qe p)" by blast
-  from DJ_qf[OF qth] qf have qfth:"qfree (DJ qe p)" by auto
-  have "Ifm bbs bs (DJ qe p) = (\<exists> q\<in> set (disjuncts p). Ifm bbs bs (qe q))"
-    by (simp add: DJ_def evaldjf_ex)
-  also have "\<dots> = (\<exists> q \<in> set(disjuncts p). Ifm bbs bs (E q))" using qe disjuncts_qf[OF qf] by auto
-  also have "\<dots> = Ifm bbs bs (E p)" by (induct p rule: disjuncts.induct, auto)
-  finally show "qfree (DJ qe p) \<and> Ifm bbs bs (DJ qe p) = Ifm bbs bs (E p)" using qfth by blast
-qed
-  (* Simplification *)
-
-  (* Algebraic simplifications for nums *)
-consts bnds:: "num \<Rightarrow> nat list"
-  lex_ns:: "nat list \<times> nat list \<Rightarrow> bool"
-recdef bnds "measure size"
-  "bnds (Bound n) = [n]"
-  "bnds (CN n c a) = n#(bnds a)"
-  "bnds (Neg a) = bnds a"
-  "bnds (Add a b) = (bnds a)@(bnds b)"
-  "bnds (Sub a b) = (bnds a)@(bnds b)"
-  "bnds (Mul i a) = bnds a"
-  "bnds a = []"
-recdef lex_ns "measure (\<lambda> (xs,ys). length xs + length ys)"
-  "lex_ns ([], ms) = True"
-  "lex_ns (ns, []) = False"
-  "lex_ns (n#ns, m#ms) = (n<m \<or> ((n = m) \<and> lex_ns (ns,ms))) "
-constdefs lex_bnd :: "num \<Rightarrow> num \<Rightarrow> bool"
-  "lex_bnd t s \<equiv> lex_ns (bnds t, bnds s)"
-
-consts
-  numadd:: "num \<times> num \<Rightarrow> num"
-recdef numadd "measure (\<lambda> (t,s). num_size t + num_size s)"
-  "numadd (CN n1 c1 r1 ,CN n2 c2 r2) =
-  (if n1=n2 then 
-  (let c = c1 + c2
-  in (if c=0 then numadd(r1,r2) else CN n1 c (numadd (r1,r2))))
-  else if n1 \<le> n2 then CN n1 c1 (numadd (r1,Add (Mul c2 (Bound n2)) r2))
-  else CN n2 c2 (numadd (Add (Mul c1 (Bound n1)) r1,r2)))"
-  "numadd (CN n1 c1 r1, t) = CN n1 c1 (numadd (r1, t))"  
-  "numadd (t,CN n2 c2 r2) = CN n2 c2 (numadd (t,r2))" 
-  "numadd (C b1, C b2) = C (b1+b2)"
-  "numadd (a,b) = Add a b"
-
-(*function (sequential)
-  numadd :: "num \<Rightarrow> num \<Rightarrow> num"
-where
-  "numadd (Add (Mul c1 (Bound n1)) r1) (Add (Mul c2 (Bound n2)) r2) =
-      (if n1 = n2 then (let c = c1 + c2
-      in (if c = 0 then numadd r1 r2 else
-        Add (Mul c (Bound n1)) (numadd r1 r2)))
-      else if n1 \<le> n2 then
-        Add (Mul c1 (Bound n1)) (numadd r1 (Add (Mul c2 (Bound n2)) r2))
-      else
-        Add (Mul c2 (Bound n2)) (numadd (Add (Mul c1 (Bound n1)) r1) r2))"
-  | "numadd (Add (Mul c1 (Bound n1)) r1) t =
-      Add (Mul c1 (Bound n1)) (numadd r1 t)"  
-  | "numadd t (Add (Mul c2 (Bound n2)) r2) =
-      Add (Mul c2 (Bound n2)) (numadd t r2)" 
-  | "numadd (C b1) (C b2) = C (b1 + b2)"
-  | "numadd a b = Add a b"
-apply pat_completeness apply auto*)
-  
-lemma numadd: "Inum bs (numadd (t,s)) = Inum bs (Add t s)"
-apply (induct t s rule: numadd.induct, simp_all add: Let_def)
-apply (case_tac "c1+c2 = 0",case_tac "n1 \<le> n2", simp_all)
- apply (case_tac "n1 = n2")
-  apply(simp_all add: algebra_simps)
-apply(simp add: left_distrib[symmetric])
-done
-
-lemma numadd_nb: "\<lbrakk> numbound0 t ; numbound0 s\<rbrakk> \<Longrightarrow> numbound0 (numadd (t,s))"
-by (induct t s rule: numadd.induct, auto simp add: Let_def)
-
-fun
-  nummul :: "int \<Rightarrow> num \<Rightarrow> num"
-where
-  "nummul i (C j) = C (i * j)"
-  | "nummul i (CN n c t) = CN n (c*i) (nummul i t)"
-  | "nummul i t = Mul i t"
-
-lemma nummul: "\<And> i. Inum bs (nummul i t) = Inum bs (Mul i t)"
-by (induct t rule: nummul.induct, auto simp add: algebra_simps numadd)
-
-lemma nummul_nb: "\<And> i. numbound0 t \<Longrightarrow> numbound0 (nummul i t)"
-by (induct t rule: nummul.induct, auto simp add: numadd_nb)
-
-constdefs numneg :: "num \<Rightarrow> num"
-  "numneg t \<equiv> nummul (- 1) t"
-
-constdefs numsub :: "num \<Rightarrow> num \<Rightarrow> num"
-  "numsub s t \<equiv> (if s = t then C 0 else numadd (s, numneg t))"
-
-lemma numneg: "Inum bs (numneg t) = Inum bs (Neg t)"
-using numneg_def nummul by simp
-
-lemma numneg_nb: "numbound0 t \<Longrightarrow> numbound0 (numneg t)"
-using numneg_def nummul_nb by simp
-
-lemma numsub: "Inum bs (numsub a b) = Inum bs (Sub a b)"
-using numneg numadd numsub_def by simp
-
-lemma numsub_nb: "\<lbrakk> numbound0 t ; numbound0 s\<rbrakk> \<Longrightarrow> numbound0 (numsub t s)"
-using numsub_def numadd_nb numneg_nb by simp
-
-fun
-  simpnum :: "num \<Rightarrow> num"
-where
-  "simpnum (C j) = C j"
-  | "simpnum (Bound n) = CN n 1 (C 0)"
-  | "simpnum (Neg t) = numneg (simpnum t)"
-  | "simpnum (Add t s) = numadd (simpnum t, simpnum s)"
-  | "simpnum (Sub t s) = numsub (simpnum t) (simpnum s)"
-  | "simpnum (Mul i t) = (if i = 0 then C 0 else nummul i (simpnum t))"
-  | "simpnum t = t"
-
-lemma simpnum_ci: "Inum bs (simpnum t) = Inum bs t"
-by (induct t rule: simpnum.induct, auto simp add: numneg numadd numsub nummul)
-
-lemma simpnum_numbound0: 
-  "numbound0 t \<Longrightarrow> numbound0 (simpnum t)"
-by (induct t rule: simpnum.induct, auto simp add: numadd_nb numsub_nb nummul_nb numneg_nb)
-
-fun
-  not :: "fm \<Rightarrow> fm"
-where
-  "not (NOT p) = p"
-  | "not T = F"
-  | "not F = T"
-  | "not p = NOT p"
-lemma not: "Ifm bbs bs (not p) = Ifm bbs bs (NOT p)"
-by (cases p) auto
-lemma not_qf: "qfree p \<Longrightarrow> qfree (not p)"
-by (cases p, auto)
-lemma not_bn: "bound0 p \<Longrightarrow> bound0 (not p)"
-by (cases p, auto)
-
-constdefs conj :: "fm \<Rightarrow> fm \<Rightarrow> fm"
-  "conj p q \<equiv> (if (p = F \<or> q=F) then F else if p=T then q else if q=T then p else And p q)"
-lemma conj: "Ifm bbs bs (conj p q) = Ifm bbs bs (And p q)"
-by (cases "p=F \<or> q=F",simp_all add: conj_def) (cases p,simp_all)
-
-lemma conj_qf: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (conj p q)"
-using conj_def by auto 
-lemma conj_nb: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (conj p q)"
-using conj_def by auto 
-
-constdefs disj :: "fm \<Rightarrow> fm \<Rightarrow> fm"
-  "disj p q \<equiv> (if (p = T \<or> q=T) then T else if p=F then q else if q=F then p else Or p q)"
-
-lemma disj: "Ifm bbs bs (disj p q) = Ifm bbs bs (Or p q)"
-by (cases "p=T \<or> q=T",simp_all add: disj_def) (cases p,simp_all)
-lemma disj_qf: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (disj p q)"
-using disj_def by auto 
-lemma disj_nb: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (disj p q)"
-using disj_def by auto 
-
-constdefs   imp :: "fm \<Rightarrow> fm \<Rightarrow> fm"
-  "imp p q \<equiv> (if (p = F \<or> q=T) then T else if p=T then q else if q=F then not p else Imp p q)"
-lemma imp: "Ifm bbs bs (imp p q) = Ifm bbs bs (Imp p q)"
-by (cases "p=F \<or> q=T",simp_all add: imp_def,cases p) (simp_all add: not)
-lemma imp_qf: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (imp p q)"
-using imp_def by (cases "p=F \<or> q=T",simp_all add: imp_def,cases p) (simp_all add: not_qf) 
-lemma imp_nb: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (imp p q)"
-using imp_def by (cases "p=F \<or> q=T",simp_all add: imp_def,cases p) simp_all
-
-constdefs   iff :: "fm \<Rightarrow> fm \<Rightarrow> fm"
-  "iff p q \<equiv> (if (p = q) then T else if (p = not q \<or> not p = q) then F else 
-       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 
-  Iff p q)"
-lemma iff: "Ifm bbs bs (iff p q) = Ifm bbs bs (Iff p q)"
-  by (unfold iff_def,cases "p=q", simp,cases "p=not q", simp add:not) 
-(cases "not p= q", auto simp add:not)
-lemma iff_qf: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (iff p q)"
-  by (unfold iff_def,cases "p=q", auto simp add: not_qf)
-lemma iff_nb: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (iff p q)"
-using iff_def by (unfold iff_def,cases "p=q", auto simp add: not_bn)
-
-function (sequential)
-  simpfm :: "fm \<Rightarrow> fm"
-where
-  "simpfm (And p q) = conj (simpfm p) (simpfm q)"
-  | "simpfm (Or p q) = disj (simpfm p) (simpfm q)"
-  | "simpfm (Imp p q) = imp (simpfm p) (simpfm q)"
-  | "simpfm (Iff p q) = iff (simpfm p) (simpfm q)"
-  | "simpfm (NOT p) = not (simpfm p)"
-  | "simpfm (Lt a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v < 0) then T else F 
-      | _ \<Rightarrow> Lt a')"
-  | "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')"
-  | "simpfm (Gt a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v > 0)  then T else F | _ \<Rightarrow> Gt a')"
-  | "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')"
-  | "simpfm (Eq a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v = 0)  then T else F | _ \<Rightarrow> Eq a')"
-  | "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')"
-  | "simpfm (Dvd i a) = (if i=0 then simpfm (Eq a)
-             else if (abs i = 1) then T
-             else let a' = simpnum a in case a' of C v \<Rightarrow> if (i dvd v)  then T else F | _ \<Rightarrow> Dvd i a')"
-  | "simpfm (NDvd i a) = (if i=0 then simpfm (NEq a) 
-             else if (abs i = 1) then F
-             else let a' = simpnum a in case a' of C v \<Rightarrow> if (\<not>(i dvd v)) then T else F | _ \<Rightarrow> NDvd i a')"
-  | "simpfm p = p"
-by pat_completeness auto
-termination by (relation "measure fmsize") auto
-
-lemma simpfm: "Ifm bbs bs (simpfm p) = Ifm bbs bs p"
-proof(induct p rule: simpfm.induct)
-  case (6 a) let ?sa = "simpnum a" from simpnum_ci have sa: "Inum bs ?sa = Inum bs a" by simp
-  {fix v assume "?sa = C v" hence ?case using sa by simp }
-  moreover {assume "\<not> (\<exists> v. ?sa = C v)" hence ?case using sa 
-      by (cases ?sa, simp_all add: Let_def)}
-  ultimately show ?case by blast
-next
-  case (7 a)  let ?sa = "simpnum a" 
-  from simpnum_ci have sa: "Inum bs ?sa = Inum bs a" by simp
-  {fix v assume "?sa = C v" hence ?case using sa by simp }
-  moreover {assume "\<not> (\<exists> v. ?sa = C v)" hence ?case using sa 
-      by (cases ?sa, simp_all add: Let_def)}
-  ultimately show ?case by blast
-next
-  case (8 a)  let ?sa = "simpnum a" 
-  from simpnum_ci have sa: "Inum bs ?sa = Inum bs a" by simp
-  {fix v assume "?sa = C v" hence ?case using sa by simp }
-  moreover {assume "\<not> (\<exists> v. ?sa = C v)" hence ?case using sa 
-      by (cases ?sa, simp_all add: Let_def)}
-  ultimately show ?case by blast
-next
-  case (9 a)  let ?sa = "simpnum a" 
-  from simpnum_ci have sa: "Inum bs ?sa = Inum bs a" by simp
-  {fix v assume "?sa = C v" hence ?case using sa by simp }
-  moreover {assume "\<not> (\<exists> v. ?sa = C v)" hence ?case using sa 
-      by (cases ?sa, simp_all add: Let_def)}
-  ultimately show ?case by blast
-next
-  case (10 a)  let ?sa = "simpnum a" 
-  from simpnum_ci have sa: "Inum bs ?sa = Inum bs a" by simp
-  {fix v assume "?sa = C v" hence ?case using sa by simp }
-  moreover {assume "\<not> (\<exists> v. ?sa = C v)" hence ?case using sa 
-      by (cases ?sa, simp_all add: Let_def)}
-  ultimately show ?case by blast
-next
-  case (11 a)  let ?sa = "simpnum a" 
-  from simpnum_ci have sa: "Inum bs ?sa = Inum bs a" by simp
-  {fix v assume "?sa = C v" hence ?case using sa by simp }
-  moreover {assume "\<not> (\<exists> v. ?sa = C v)" hence ?case using sa 
-      by (cases ?sa, simp_all add: Let_def)}
-  ultimately show ?case by blast
-next
-  case (12 i a)  let ?sa = "simpnum a" from simpnum_ci 
-  have sa: "Inum bs ?sa = Inum bs a" by simp
-  have "i=0 \<or> abs i = 1 \<or> (i\<noteq>0 \<and> (abs i \<noteq> 1))" by auto
-  {assume "i=0" hence ?case using "12.hyps" by (simp add: dvd_def Let_def)}
-  moreover 
-  {assume i1: "abs i = 1"
-      from zdvd_1_left[where m = "Inum bs a"] uminus_dvd_conv[where d="1" and t="Inum bs a"]
-      have ?case using i1 apply (cases "i=0", simp_all add: Let_def) 
-	by (cases "i > 0", simp_all)}
-  moreover   
-  {assume inz: "i\<noteq>0" and cond: "abs i \<noteq> 1"
-    {fix v assume "?sa = C v" hence ?case using sa[symmetric] inz cond
-	by (cases "abs i = 1", auto) }
-    moreover {assume "\<not> (\<exists> v. ?sa = C v)" 
-      hence "simpfm (Dvd i a) = Dvd i ?sa" using inz cond 
-	by (cases ?sa, auto simp add: Let_def)
-      hence ?case using sa by simp}
-    ultimately have ?case by blast}
-  ultimately show ?case by blast
-next
-  case (13 i a)  let ?sa = "simpnum a" from simpnum_ci 
-  have sa: "Inum bs ?sa = Inum bs a" by simp
-  have "i=0 \<or> abs i = 1 \<or> (i\<noteq>0 \<and> (abs i \<noteq> 1))" by auto
-  {assume "i=0" hence ?case using "13.hyps" by (simp add: dvd_def Let_def)}
-  moreover 
-  {assume i1: "abs i = 1"
-      from zdvd_1_left[where m = "Inum bs a"] uminus_dvd_conv[where d="1" and t="Inum bs a"]
-      have ?case using i1 apply (cases "i=0", simp_all add: Let_def)
-      apply (cases "i > 0", simp_all) done}
-  moreover   
-  {assume inz: "i\<noteq>0" and cond: "abs i \<noteq> 1"
-    {fix v assume "?sa = C v" hence ?case using sa[symmetric] inz cond
-	by (cases "abs i = 1", auto) }
-    moreover {assume "\<not> (\<exists> v. ?sa = C v)" 
-      hence "simpfm (NDvd i a) = NDvd i ?sa" using inz cond 
-	by (cases ?sa, auto simp add: Let_def)
-      hence ?case using sa by simp}
-    ultimately have ?case by blast}
-  ultimately show ?case by blast
-qed (induct p rule: simpfm.induct, simp_all add: conj disj imp iff not)
-
-lemma simpfm_bound0: "bound0 p \<Longrightarrow> bound0 (simpfm p)"
-proof(induct p rule: simpfm.induct)
-  case (6 a) hence nb: "numbound0 a" by simp
-  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
-  thus ?case by (cases "simpnum a", auto simp add: Let_def)
-next
-  case (7 a) hence nb: "numbound0 a" by simp
-  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
-  thus ?case by (cases "simpnum a", auto simp add: Let_def)
-next
-  case (8 a) hence nb: "numbound0 a" by simp
-  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
-  thus ?case by (cases "simpnum a", auto simp add: Let_def)
-next
-  case (9 a) hence nb: "numbound0 a" by simp
-  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
-  thus ?case by (cases "simpnum a", auto simp add: Let_def)
-next
-  case (10 a) hence nb: "numbound0 a" by simp
-  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
-  thus ?case by (cases "simpnum a", auto simp add: Let_def)
-next
-  case (11 a) hence nb: "numbound0 a" by simp
-  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
-  thus ?case by (cases "simpnum a", auto simp add: Let_def)
-next
-  case (12 i a) hence nb: "numbound0 a" by simp
-  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
-  thus ?case by (cases "simpnum a", auto simp add: Let_def)
-next
-  case (13 i a) hence nb: "numbound0 a" by simp
-  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
-  thus ?case by (cases "simpnum a", auto simp add: Let_def)
-qed(auto simp add: disj_def imp_def iff_def conj_def not_bn)
-
-lemma simpfm_qf: "qfree p \<Longrightarrow> qfree (simpfm p)"
-by (induct p rule: simpfm.induct, auto simp add: disj_qf imp_qf iff_qf conj_qf not_qf Let_def)
- (case_tac "simpnum a",auto)+
-
-  (* Generic quantifier elimination *)
-consts qelim :: "fm \<Rightarrow> (fm \<Rightarrow> fm) \<Rightarrow> fm"
-recdef qelim "measure fmsize"
-  "qelim (E p) = (\<lambda> qe. DJ qe (qelim p qe))"
-  "qelim (A p) = (\<lambda> qe. not (qe ((qelim (NOT p) qe))))"
-  "qelim (NOT p) = (\<lambda> qe. not (qelim p qe))"
-  "qelim (And p q) = (\<lambda> qe. conj (qelim p qe) (qelim q qe))" 
-  "qelim (Or  p q) = (\<lambda> qe. disj (qelim p qe) (qelim q qe))" 
-  "qelim (Imp p q) = (\<lambda> qe. imp (qelim p qe) (qelim q qe))"
-  "qelim (Iff p q) = (\<lambda> qe. iff (qelim p qe) (qelim q qe))"
-  "qelim p = (\<lambda> y. simpfm p)"
-
-(*function (sequential)
-  qelim :: "(fm \<Rightarrow> fm) \<Rightarrow> fm \<Rightarrow> fm"
-where
-  "qelim qe (E p) = DJ qe (qelim qe p)"
-  | "qelim qe (A p) = not (qe ((qelim qe (NOT p))))"
-  | "qelim qe (NOT p) = not (qelim qe p)"
-  | "qelim qe (And p q) = conj (qelim qe p) (qelim qe q)" 
-  | "qelim qe (Or  p q) = disj (qelim qe p) (qelim qe q)" 
-  | "qelim qe (Imp p q) = imp (qelim qe p) (qelim qe q)"
-  | "qelim qe (Iff p q) = iff (qelim qe p) (qelim qe q)"
-  | "qelim qe p = simpfm p"
-by pat_completeness auto
-termination by (relation "measure (fmsize o snd)") auto*)
-
-lemma qelim_ci:
-  assumes qe_inv: "\<forall> bs p. qfree p \<longrightarrow> qfree (qe p) \<and> (Ifm bbs bs (qe p) = Ifm bbs bs (E p))"
-  shows "\<And> bs. qfree (qelim p qe) \<and> (Ifm bbs bs (qelim p qe) = Ifm bbs bs p)"
-using qe_inv DJ_qe[OF qe_inv] 
-by(induct p rule: qelim.induct) 
-(auto simp add: not disj conj iff imp not_qf disj_qf conj_qf imp_qf iff_qf 
-  simpfm simpfm_qf simp del: simpfm.simps)
-  (* Linearity for fm where Bound 0 ranges over \<int> *)
-
-fun
-  zsplit0 :: "num \<Rightarrow> int \<times> num" (* splits the bounded from the unbounded part*)
-where
-  "zsplit0 (C c) = (0,C c)"
-  | "zsplit0 (Bound n) = (if n=0 then (1, C 0) else (0,Bound n))"
-  | "zsplit0 (CN n i a) = 
-      (let (i',a') =  zsplit0 a 
-       in if n=0 then (i+i', a') else (i',CN n i a'))"
-  | "zsplit0 (Neg a) = (let (i',a') =  zsplit0 a in (-i', Neg a'))"
-  | "zsplit0 (Add a b) = (let (ia,a') =  zsplit0 a ; 
-                            (ib,b') =  zsplit0 b 
-                            in (ia+ib, Add a' b'))"
-  | "zsplit0 (Sub a b) = (let (ia,a') =  zsplit0 a ; 
-                            (ib,b') =  zsplit0 b 
-                            in (ia-ib, Sub a' b'))"
-  | "zsplit0 (Mul i a) = (let (i',a') =  zsplit0 a in (i*i', Mul i a'))"
-
-lemma zsplit0_I:
-  shows "\<And> n a. zsplit0 t = (n,a) \<Longrightarrow> (Inum ((x::int) #bs) (CN 0 n a) = Inum (x #bs) t) \<and> numbound0 a"
-  (is "\<And> n a. ?S t = (n,a) \<Longrightarrow> (?I x (CN 0 n a) = ?I x t) \<and> ?N a")
-proof(induct t rule: zsplit0.induct)
-  case (1 c n a) thus ?case by auto 
-next
-  case (2 m n a) thus ?case by (cases "m=0") auto
-next
-  case (3 m i a n a')
-  let ?j = "fst (zsplit0 a)"
-  let ?b = "snd (zsplit0 a)"
-  have abj: "zsplit0 a = (?j,?b)" by simp 
-  {assume "m\<noteq>0" 
-    with prems(1)[OF abj] prems(2) have ?case by (auto simp add: Let_def split_def)}
-  moreover
-  {assume m0: "m =0"
-    from abj have th: "a'=?b \<and> n=i+?j" using prems 
-      by (simp add: Let_def split_def)
-    from abj prems  have th2: "(?I x (CN 0 ?j ?b) = ?I x a) \<and> ?N ?b" by blast
-    from th have "?I x (CN 0 n a') = ?I x (CN 0 (i+?j) ?b)" by simp
-    also from th2 have "\<dots> = ?I x (CN 0 i (CN 0 ?j ?b))" by (simp add: left_distrib)
-  finally have "?I x (CN 0 n a') = ?I  x (CN 0 i a)" using th2 by simp
-  with th2 th have ?case using m0 by blast} 
-ultimately show ?case by blast
-next
-  case (4 t n a)
-  let ?nt = "fst (zsplit0 t)"
-  let ?at = "snd (zsplit0 t)"
-  have abj: "zsplit0 t = (?nt,?at)" by simp hence th: "a=Neg ?at \<and> n=-?nt" using prems 
-    by (simp add: Let_def split_def)
-  from abj prems  have th2: "(?I x (CN 0 ?nt ?at) = ?I x t) \<and> ?N ?at" by blast
-  from th2[simplified] th[simplified] show ?case by simp
-next
-  case (5 s t n a)
-  let ?ns = "fst (zsplit0 s)"
-  let ?as = "snd (zsplit0 s)"
-  let ?nt = "fst (zsplit0 t)"
-  let ?at = "snd (zsplit0 t)"
-  have abjs: "zsplit0 s = (?ns,?as)" by simp 
-  moreover have abjt:  "zsplit0 t = (?nt,?at)" by simp 
-  ultimately have th: "a=Add ?as ?at \<and> n=?ns + ?nt" using prems 
-    by (simp add: Let_def split_def)
-  from abjs[symmetric] have bluddy: "\<exists> x y. (x,y) = zsplit0 s" by blast
-  from prems have "(\<exists> x y. (x,y) = zsplit0 s) \<longrightarrow> (\<forall>xa xb. zsplit0 t = (xa, xb) \<longrightarrow> Inum (x # bs) (CN 0 xa xb) = Inum (x # bs) t \<and> numbound0 xb)" by auto
-  with bluddy abjt have th3: "(?I x (CN 0 ?nt ?at) = ?I x t) \<and> ?N ?at" by blast
-  from abjs prems  have th2: "(?I x (CN 0 ?ns ?as) = ?I x s) \<and> ?N ?as" by blast
-  from th3[simplified] th2[simplified] th[simplified] show ?case 
-    by (simp add: left_distrib)
-next
-  case (6 s t n a)
-  let ?ns = "fst (zsplit0 s)"
-  let ?as = "snd (zsplit0 s)"
-  let ?nt = "fst (zsplit0 t)"
-  let ?at = "snd (zsplit0 t)"
-  have abjs: "zsplit0 s = (?ns,?as)" by simp 
-  moreover have abjt:  "zsplit0 t = (?nt,?at)" by simp 
-  ultimately have th: "a=Sub ?as ?at \<and> n=?ns - ?nt" using prems 
-    by (simp add: Let_def split_def)
-  from abjs[symmetric] have bluddy: "\<exists> x y. (x,y) = zsplit0 s" by blast
-  from prems have "(\<exists> x y. (x,y) = zsplit0 s) \<longrightarrow> (\<forall>xa xb. zsplit0 t = (xa, xb) \<longrightarrow> Inum (x # bs) (CN 0 xa xb) = Inum (x # bs) t \<and> numbound0 xb)" by auto
-  with bluddy abjt have th3: "(?I x (CN 0 ?nt ?at) = ?I x t) \<and> ?N ?at" by blast
-  from abjs prems  have th2: "(?I x (CN 0 ?ns ?as) = ?I x s) \<and> ?N ?as" by blast
-  from th3[simplified] th2[simplified] th[simplified] show ?case 
-    by (simp add: left_diff_distrib)
-next
-  case (7 i t n a)
-  let ?nt = "fst (zsplit0 t)"
-  let ?at = "snd (zsplit0 t)"
-  have abj: "zsplit0 t = (?nt,?at)" by simp hence th: "a=Mul i ?at \<and> n=i*?nt" using prems 
-    by (simp add: Let_def split_def)
-  from abj prems  have th2: "(?I x (CN 0 ?nt ?at) = ?I x t) \<and> ?N ?at" by blast
-  hence " ?I x (Mul i t) = i * ?I x (CN 0 ?nt ?at)" by simp
-  also have "\<dots> = ?I x (CN 0 (i*?nt) (Mul i ?at))" by (simp add: right_distrib)
-  finally show ?case using th th2 by simp
-qed
-
-consts
-  iszlfm :: "fm \<Rightarrow> bool"   (* Linearity test for fm *)
-recdef iszlfm "measure size"
-  "iszlfm (And p q) = (iszlfm p \<and> iszlfm q)" 
-  "iszlfm (Or p q) = (iszlfm p \<and> iszlfm q)" 
-  "iszlfm (Eq  (CN 0 c e)) = (c>0 \<and> numbound0 e)"
-  "iszlfm (NEq (CN 0 c e)) = (c>0 \<and> numbound0 e)"
-  "iszlfm (Lt  (CN 0 c e)) = (c>0 \<and> numbound0 e)"
-  "iszlfm (Le  (CN 0 c e)) = (c>0 \<and> numbound0 e)"
-  "iszlfm (Gt  (CN 0 c e)) = (c>0 \<and> numbound0 e)"
-  "iszlfm (Ge  (CN 0 c e)) = ( c>0 \<and> numbound0 e)"
-  "iszlfm (Dvd i (CN 0 c e)) = 
-                 (c>0 \<and> i>0 \<and> numbound0 e)"
-  "iszlfm (NDvd i (CN 0 c e))= 
-                 (c>0 \<and> i>0 \<and> numbound0 e)"
-  "iszlfm p = (isatom p \<and> (bound0 p))"
-
-lemma zlin_qfree: "iszlfm p \<Longrightarrow> qfree p"
-  by (induct p rule: iszlfm.induct) auto
-
-consts
-  zlfm :: "fm \<Rightarrow> fm"       (* Linearity transformation for fm *)
-recdef zlfm "measure fmsize"
-  "zlfm (And p q) = And (zlfm p) (zlfm q)"
-  "zlfm (Or p q) = Or (zlfm p) (zlfm q)"
-  "zlfm (Imp p q) = Or (zlfm (NOT p)) (zlfm q)"
-  "zlfm (Iff p q) = Or (And (zlfm p) (zlfm q)) (And (zlfm (NOT p)) (zlfm (NOT q)))"
-  "zlfm (Lt a) = (let (c,r) = zsplit0 a in 
-     if c=0 then Lt r else 
-     if c>0 then (Lt (CN 0 c r)) else (Gt (CN 0 (- c) (Neg r))))"
-  "zlfm (Le a) = (let (c,r) = zsplit0 a in 
-     if c=0 then Le r else 
-     if c>0 then (Le (CN 0 c r)) else (Ge (CN 0 (- c) (Neg r))))"
-  "zlfm (Gt a) = (let (c,r) = zsplit0 a in 
-     if c=0 then Gt r else 
-     if c>0 then (Gt (CN 0 c r)) else (Lt (CN 0 (- c) (Neg r))))"
-  "zlfm (Ge a) = (let (c,r) = zsplit0 a in 
-     if c=0 then Ge r else 
-     if c>0 then (Ge (CN 0 c r)) else (Le (CN 0 (- c) (Neg r))))"
-  "zlfm (Eq a) = (let (c,r) = zsplit0 a in 
-     if c=0 then Eq r else 
-     if c>0 then (Eq (CN 0 c r)) else (Eq (CN 0 (- c) (Neg r))))"
-  "zlfm (NEq a) = (let (c,r) = zsplit0 a in 
-     if c=0 then NEq r else 
-     if c>0 then (NEq (CN 0 c r)) else (NEq (CN 0 (- c) (Neg r))))"
-  "zlfm (Dvd i a) = (if i=0 then zlfm (Eq a) 
-        else (let (c,r) = zsplit0 a in 
-              if c=0 then (Dvd (abs i) r) else 
-      if c>0 then (Dvd (abs i) (CN 0 c r))
-      else (Dvd (abs i) (CN 0 (- c) (Neg r)))))"
-  "zlfm (NDvd i a) = (if i=0 then zlfm (NEq a) 
-        else (let (c,r) = zsplit0 a in 
-              if c=0 then (NDvd (abs i) r) else 
-      if c>0 then (NDvd (abs i) (CN 0 c r))
-      else (NDvd (abs i) (CN 0 (- c) (Neg r)))))"
-  "zlfm (NOT (And p q)) = Or (zlfm (NOT p)) (zlfm (NOT q))"
-  "zlfm (NOT (Or p q)) = And (zlfm (NOT p)) (zlfm (NOT q))"
-  "zlfm (NOT (Imp p q)) = And (zlfm p) (zlfm (NOT q))"
-  "zlfm (NOT (Iff p q)) = Or (And(zlfm p) (zlfm(NOT q))) (And (zlfm(NOT p)) (zlfm q))"
-  "zlfm (NOT (NOT p)) = zlfm p"
-  "zlfm (NOT T) = F"
-  "zlfm (NOT F) = T"
-  "zlfm (NOT (Lt a)) = zlfm (Ge a)"
-  "zlfm (NOT (Le a)) = zlfm (Gt a)"
-  "zlfm (NOT (Gt a)) = zlfm (Le a)"
-  "zlfm (NOT (Ge a)) = zlfm (Lt a)"
-  "zlfm (NOT (Eq a)) = zlfm (NEq a)"
-  "zlfm (NOT (NEq a)) = zlfm (Eq a)"
-  "zlfm (NOT (Dvd i a)) = zlfm (NDvd i a)"
-  "zlfm (NOT (NDvd i a)) = zlfm (Dvd i a)"
-  "zlfm (NOT (Closed P)) = NClosed P"
-  "zlfm (NOT (NClosed P)) = Closed P"
-  "zlfm p = p" (hints simp add: fmsize_pos)
-
-lemma zlfm_I:
-  assumes qfp: "qfree p"
-  shows "(Ifm bbs (i#bs) (zlfm p) = Ifm bbs (i# bs) p) \<and> iszlfm (zlfm p)"
-  (is "(?I (?l p) = ?I p) \<and> ?L (?l p)")
-using qfp
-proof(induct p rule: zlfm.induct)
-  case (5 a) 
-  let ?c = "fst (zsplit0 a)"
-  let ?r = "snd (zsplit0 a)"
-  have spl: "zsplit0 a = (?c,?r)" by simp
-  from zsplit0_I[OF spl, where x="i" and bs="bs"] 
-  have Ia:"Inum (i # bs) a = Inum (i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto 
-  let ?N = "\<lambda> t. Inum (i#bs) t"
-  from prems Ia nb  show ?case 
-    apply (auto simp add: Let_def split_def algebra_simps) 
-    apply (cases "?r",auto)
-    apply (case_tac nat, auto)
-    done
-next
-  case (6 a)  
-  let ?c = "fst (zsplit0 a)"
-  let ?r = "snd (zsplit0 a)"
-  have spl: "zsplit0 a = (?c,?r)" by simp
-  from zsplit0_I[OF spl, where x="i" and bs="bs"] 
-  have Ia:"Inum (i # bs) a = Inum (i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto 
-  let ?N = "\<lambda> t. Inum (i#bs) t"
-  from prems Ia nb  show ?case 
-    apply (auto simp add: Let_def split_def algebra_simps) 
-    apply (cases "?r",auto)
-    apply (case_tac nat, auto)
-    done
-next
-  case (7 a)  
-  let ?c = "fst (zsplit0 a)"
-  let ?r = "snd (zsplit0 a)"
-  have spl: "zsplit0 a = (?c,?r)" by simp
-  from zsplit0_I[OF spl, where x="i" and bs="bs"] 
-  have Ia:"Inum (i # bs) a = Inum (i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto 
-  let ?N = "\<lambda> t. Inum (i#bs) t"
-  from prems Ia nb  show ?case 
-    apply (auto simp add: Let_def split_def algebra_simps) 
-    apply (cases "?r",auto)
-    apply (case_tac nat, auto)
-    done
-next
-  case (8 a)  
-  let ?c = "fst (zsplit0 a)"
-  let ?r = "snd (zsplit0 a)"
-  have spl: "zsplit0 a = (?c,?r)" by simp
-  from zsplit0_I[OF spl, where x="i" and bs="bs"] 
-  have Ia:"Inum (i # bs) a = Inum (i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto 
-  let ?N = "\<lambda> t. Inum (i#bs) t"
-  from prems Ia nb  show ?case 
-    apply (auto simp add: Let_def split_def algebra_simps) 
-    apply (cases "?r",auto)
-    apply (case_tac nat, auto)
-    done
-next
-  case (9 a)  
-  let ?c = "fst (zsplit0 a)"
-  let ?r = "snd (zsplit0 a)"
-  have spl: "zsplit0 a = (?c,?r)" by simp
-  from zsplit0_I[OF spl, where x="i" and bs="bs"] 
-  have Ia:"Inum (i # bs) a = Inum (i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto 
-  let ?N = "\<lambda> t. Inum (i#bs) t"
-  from prems Ia nb  show ?case 
-    apply (auto simp add: Let_def split_def algebra_simps) 
-    apply (cases "?r",auto)
-    apply (case_tac nat, auto)
-    done
-next
-  case (10 a)  
-  let ?c = "fst (zsplit0 a)"
-  let ?r = "snd (zsplit0 a)"
-  have spl: "zsplit0 a = (?c,?r)" by simp
-  from zsplit0_I[OF spl, where x="i" and bs="bs"] 
-  have Ia:"Inum (i # bs) a = Inum (i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto 
-  let ?N = "\<lambda> t. Inum (i#bs) t"
-  from prems Ia nb  show ?case 
-    apply (auto simp add: Let_def split_def algebra_simps) 
-    apply (cases "?r",auto)
-    apply (case_tac nat, auto)
-    done
-next
-  case (11 j a)  
-  let ?c = "fst (zsplit0 a)"
-  let ?r = "snd (zsplit0 a)"
-  have spl: "zsplit0 a = (?c,?r)" by simp
-  from zsplit0_I[OF spl, where x="i" and bs="bs"] 
-  have Ia:"Inum (i # bs) a = Inum (i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto 
-  let ?N = "\<lambda> t. Inum (i#bs) t"
-  have "j=0 \<or> (j\<noteq>0 \<and> ?c = 0) \<or> (j\<noteq>0 \<and> ?c >0) \<or> (j\<noteq> 0 \<and> ?c<0)" by arith
-  moreover
-  {assume "j=0" hence z: "zlfm (Dvd j a) = (zlfm (Eq a))" by (simp add: Let_def) 
-    hence ?case using prems by (simp del: zlfm.simps add: zdvd_0_left)}
-  moreover
-  {assume "?c=0" and "j\<noteq>0" hence ?case 
-      using zsplit0_I[OF spl, where x="i" and bs="bs"]
-    apply (auto simp add: Let_def split_def algebra_simps) 
-    apply (cases "?r",auto)
-    apply (case_tac nat, auto)
-    done}
-  moreover
-  {assume cp: "?c > 0" and jnz: "j\<noteq>0" hence l: "?L (?l (Dvd j a))" 
-      by (simp add: nb Let_def split_def)
-    hence ?case using Ia cp jnz by (simp add: Let_def split_def)}
-  moreover
-  {assume cn: "?c < 0" and jnz: "j\<noteq>0" hence l: "?L (?l (Dvd j a))" 
-      by (simp add: nb Let_def split_def)
-    hence ?case using Ia cn jnz zdvd_zminus_iff[where m="abs j" and n="?c*i + ?N ?r" ]
-      by (simp add: Let_def split_def) }
-  ultimately show ?case by blast
-next
-  case (12 j a) 
-  let ?c = "fst (zsplit0 a)"
-  let ?r = "snd (zsplit0 a)"
-  have spl: "zsplit0 a = (?c,?r)" by simp
-  from zsplit0_I[OF spl, where x="i" and bs="bs"] 
-  have Ia:"Inum (i # bs) a = Inum (i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto 
-  let ?N = "\<lambda> t. Inum (i#bs) t"
-  have "j=0 \<or> (j\<noteq>0 \<and> ?c = 0) \<or> (j\<noteq>0 \<and> ?c >0) \<or> (j\<noteq> 0 \<and> ?c<0)" by arith
-  moreover
-  {assume "j=0" hence z: "zlfm (NDvd j a) = (zlfm (NEq a))" by (simp add: Let_def) 
-    hence ?case using prems by (simp del: zlfm.simps add: zdvd_0_left)}
-  moreover
-  {assume "?c=0" and "j\<noteq>0" hence ?case 
-      using zsplit0_I[OF spl, where x="i" and bs="bs"]
-    apply (auto simp add: Let_def split_def algebra_simps) 
-    apply (cases "?r",auto)
-    apply (case_tac nat, auto)
-    done}
-  moreover
-  {assume cp: "?c > 0" and jnz: "j\<noteq>0" hence l: "?L (?l (Dvd j a))" 
-      by (simp add: nb Let_def split_def)
-    hence ?case using Ia cp jnz by (simp add: Let_def split_def) }
-  moreover
-  {assume cn: "?c < 0" and jnz: "j\<noteq>0" hence l: "?L (?l (Dvd j a))" 
-      by (simp add: nb Let_def split_def)
-    hence ?case using Ia cn jnz zdvd_zminus_iff[where m="abs j" and n="?c*i + ?N ?r" ]
-      by (simp add: Let_def split_def)}
-  ultimately show ?case by blast
-qed auto
-
-consts 
-  plusinf:: "fm \<Rightarrow> fm" (* Virtual substitution of +\<infinity>*)
-  minusinf:: "fm \<Rightarrow> fm" (* Virtual substitution of -\<infinity>*)
-  \<delta> :: "fm \<Rightarrow> int" (* Compute lcm {d| N\<^isup>?\<^isup> Dvd c*x+t \<in> p}*)
-  d\<delta> :: "fm \<Rightarrow> int \<Rightarrow> bool" (* checks if a given l divides all the ds above*)
-
-recdef minusinf "measure size"
-  "minusinf (And p q) = And (minusinf p) (minusinf q)" 
-  "minusinf (Or p q) = Or (minusinf p) (minusinf q)" 
-  "minusinf (Eq  (CN 0 c e)) = F"
-  "minusinf (NEq (CN 0 c e)) = T"
-  "minusinf (Lt  (CN 0 c e)) = T"
-  "minusinf (Le  (CN 0 c e)) = T"
-  "minusinf (Gt  (CN 0 c e)) = F"
-  "minusinf (Ge  (CN 0 c e)) = F"
-  "minusinf p = p"
-
-lemma minusinf_qfree: "qfree p \<Longrightarrow> qfree (minusinf p)"
-  by (induct p rule: minusinf.induct, auto)
-
-recdef plusinf "measure size"
-  "plusinf (And p q) = And (plusinf p) (plusinf q)" 
-  "plusinf (Or p q) = Or (plusinf p) (plusinf q)" 
-  "plusinf (Eq  (CN 0 c e)) = F"
-  "plusinf (NEq (CN 0 c e)) = T"
-  "plusinf (Lt  (CN 0 c e)) = F"
-  "plusinf (Le  (CN 0 c e)) = F"
-  "plusinf (Gt  (CN 0 c e)) = T"
-  "plusinf (Ge  (CN 0 c e)) = T"
-  "plusinf p = p"
-
-recdef \<delta> "measure size"
-  "\<delta> (And p q) = zlcm (\<delta> p) (\<delta> q)" 
-  "\<delta> (Or p q) = zlcm (\<delta> p) (\<delta> q)" 
-  "\<delta> (Dvd i (CN 0 c e)) = i"
-  "\<delta> (NDvd i (CN 0 c e)) = i"
-  "\<delta> p = 1"
-
-recdef d\<delta> "measure size"
-  "d\<delta> (And p q) = (\<lambda> d. d\<delta> p d \<and> d\<delta> q d)" 
-  "d\<delta> (Or p q) = (\<lambda> d. d\<delta> p d \<and> d\<delta> q d)" 
-  "d\<delta> (Dvd i (CN 0 c e)) = (\<lambda> d. i dvd d)"
-  "d\<delta> (NDvd i (CN 0 c e)) = (\<lambda> d. i dvd d)"
-  "d\<delta> p = (\<lambda> d. True)"
-
-lemma delta_mono: 
-  assumes lin: "iszlfm p"
-  and d: "d dvd d'"
-  and ad: "d\<delta> p d"
-  shows "d\<delta> p d'"
-  using lin ad d
-proof(induct p rule: iszlfm.induct)
-  case (9 i c e)  thus ?case using d
-    by (simp add: zdvd_trans[where m="i" and n="d" and k="d'"])
-next
-  case (10 i c e) thus ?case using d
-    by (simp add: zdvd_trans[where m="i" and n="d" and k="d'"])
-qed simp_all
-
-lemma \<delta> : assumes lin:"iszlfm p"
-  shows "d\<delta> p (\<delta> p) \<and> \<delta> p >0"
-using lin
-proof (induct p rule: iszlfm.induct)
-  case (1 p q) 
-  let ?d = "\<delta> (And p q)"
-  from prems zlcm_pos have dp: "?d >0" by simp
-  have d1: "\<delta> p dvd \<delta> (And p q)" using prems by simp
-  hence th: "d\<delta> p ?d" using delta_mono prems(3-4) by(simp del:dvd_zlcm_self1)
-  have "\<delta> q dvd \<delta> (And p q)" using prems by simp
-  hence th': "d\<delta> q ?d" using delta_mono prems by(simp del:dvd_zlcm_self2)
-  from th th' dp show ?case by simp
-next
-  case (2 p q)  
-  let ?d = "\<delta> (And p q)"
-  from prems zlcm_pos have dp: "?d >0" by simp
-  have "\<delta> p dvd \<delta> (And p q)" using prems by simp
-  hence th: "d\<delta> p ?d" using delta_mono prems by(simp del:dvd_zlcm_self1)
-  have "\<delta> q dvd \<delta> (And p q)" using prems by simp
-  hence th': "d\<delta> q ?d" using delta_mono prems by(simp del:dvd_zlcm_self2)
-  from th th' dp show ?case by simp
-qed simp_all
-
-
-consts 
-  a\<beta> :: "fm \<Rightarrow> int \<Rightarrow> fm" (* adjusts the coeffitients of a formula *)
-  d\<beta> :: "fm \<Rightarrow> int \<Rightarrow> bool" (* tests if all coeffs c of c divide a given l*)
-  \<zeta>  :: "fm \<Rightarrow> int" (* computes the lcm of all coefficients of x*)
-  \<beta> :: "fm \<Rightarrow> num list"
-  \<alpha> :: "fm \<Rightarrow> num list"
-
-recdef a\<beta> "measure size"
-  "a\<beta> (And p q) = (\<lambda> k. And (a\<beta> p k) (a\<beta> q k))" 
-  "a\<beta> (Or p q) = (\<lambda> k. Or (a\<beta> p k) (a\<beta> q k))" 
-  "a\<beta> (Eq  (CN 0 c e)) = (\<lambda> k. Eq (CN 0 1 (Mul (k div c) e)))"
-  "a\<beta> (NEq (CN 0 c e)) = (\<lambda> k. NEq (CN 0 1 (Mul (k div c) e)))"
-  "a\<beta> (Lt  (CN 0 c e)) = (\<lambda> k. Lt (CN 0 1 (Mul (k div c) e)))"
-  "a\<beta> (Le  (CN 0 c e)) = (\<lambda> k. Le (CN 0 1 (Mul (k div c) e)))"
-  "a\<beta> (Gt  (CN 0 c e)) = (\<lambda> k. Gt (CN 0 1 (Mul (k div c) e)))"
-  "a\<beta> (Ge  (CN 0 c e)) = (\<lambda> k. Ge (CN 0 1 (Mul (k div c) e)))"
-  "a\<beta> (Dvd i (CN 0 c e)) =(\<lambda> k. Dvd ((k div c)*i) (CN 0 1 (Mul (k div c) e)))"
-  "a\<beta> (NDvd i (CN 0 c e))=(\<lambda> k. NDvd ((k div c)*i) (CN 0 1 (Mul (k div c) e)))"
-  "a\<beta> p = (\<lambda> k. p)"
-
-recdef d\<beta> "measure size"
-  "d\<beta> (And p q) = (\<lambda> k. (d\<beta> p k) \<and> (d\<beta> q k))" 
-  "d\<beta> (Or p q) = (\<lambda> k. (d\<beta> p k) \<and> (d\<beta> q k))" 
-  "d\<beta> (Eq  (CN 0 c e)) = (\<lambda> k. c dvd k)"
-  "d\<beta> (NEq (CN 0 c e)) = (\<lambda> k. c dvd k)"
-  "d\<beta> (Lt  (CN 0 c e)) = (\<lambda> k. c dvd k)"
-  "d\<beta> (Le  (CN 0 c e)) = (\<lambda> k. c dvd k)"
-  "d\<beta> (Gt  (CN 0 c e)) = (\<lambda> k. c dvd k)"
-  "d\<beta> (Ge  (CN 0 c e)) = (\<lambda> k. c dvd k)"
-  "d\<beta> (Dvd i (CN 0 c e)) =(\<lambda> k. c dvd k)"
-  "d\<beta> (NDvd i (CN 0 c e))=(\<lambda> k. c dvd k)"
-  "d\<beta> p = (\<lambda> k. True)"
-
-recdef \<zeta> "measure size"
-  "\<zeta> (And p q) = zlcm (\<zeta> p) (\<zeta> q)" 
-  "\<zeta> (Or p q) = zlcm (\<zeta> p) (\<zeta> q)" 
-  "\<zeta> (Eq  (CN 0 c e)) = c"
-  "\<zeta> (NEq (CN 0 c e)) = c"
-  "\<zeta> (Lt  (CN 0 c e)) = c"
-  "\<zeta> (Le  (CN 0 c e)) = c"
-  "\<zeta> (Gt  (CN 0 c e)) = c"
-  "\<zeta> (Ge  (CN 0 c e)) = c"
-  "\<zeta> (Dvd i (CN 0 c e)) = c"
-  "\<zeta> (NDvd i (CN 0 c e))= c"
-  "\<zeta> p = 1"
-
-recdef \<beta> "measure size"
-  "\<beta> (And p q) = (\<beta> p @ \<beta> q)" 
-  "\<beta> (Or p q) = (\<beta> p @ \<beta> q)" 
-  "\<beta> (Eq  (CN 0 c e)) = [Sub (C -1) e]"
-  "\<beta> (NEq (CN 0 c e)) = [Neg e]"
-  "\<beta> (Lt  (CN 0 c e)) = []"
-  "\<beta> (Le  (CN 0 c e)) = []"
-  "\<beta> (Gt  (CN 0 c e)) = [Neg e]"
-  "\<beta> (Ge  (CN 0 c e)) = [Sub (C -1) e]"
-  "\<beta> p = []"
-
-recdef \<alpha> "measure size"
-  "\<alpha> (And p q) = (\<alpha> p @ \<alpha> q)" 
-  "\<alpha> (Or p q) = (\<alpha> p @ \<alpha> q)" 
-  "\<alpha> (Eq  (CN 0 c e)) = [Add (C -1) e]"
-  "\<alpha> (NEq (CN 0 c e)) = [e]"
-  "\<alpha> (Lt  (CN 0 c e)) = [e]"
-  "\<alpha> (Le  (CN 0 c e)) = [Add (C -1) e]"
-  "\<alpha> (Gt  (CN 0 c e)) = []"
-  "\<alpha> (Ge  (CN 0 c e)) = []"
-  "\<alpha> p = []"
-consts mirror :: "fm \<Rightarrow> fm"
-recdef mirror "measure size"
-  "mirror (And p q) = And (mirror p) (mirror q)" 
-  "mirror (Or p q) = Or (mirror p) (mirror q)" 
-  "mirror (Eq  (CN 0 c e)) = Eq (CN 0 c (Neg e))"
-  "mirror (NEq (CN 0 c e)) = NEq (CN 0 c (Neg e))"
-  "mirror (Lt  (CN 0 c e)) = Gt (CN 0 c (Neg e))"
-  "mirror (Le  (CN 0 c e)) = Ge (CN 0 c (Neg e))"
-  "mirror (Gt  (CN 0 c e)) = Lt (CN 0 c (Neg e))"
-  "mirror (Ge  (CN 0 c e)) = Le (CN 0 c (Neg e))"
-  "mirror (Dvd i (CN 0 c e)) = Dvd i (CN 0 c (Neg e))"
-  "mirror (NDvd i (CN 0 c e)) = NDvd i (CN 0 c (Neg e))"
-  "mirror p = p"
-    (* Lemmas for the correctness of \<sigma>\<rho> *)
-lemma dvd1_eq1: "x >0 \<Longrightarrow> (x::int) dvd 1 = (x = 1)"
-by simp
-
-lemma minusinf_inf:
-  assumes linp: "iszlfm p"
-  and u: "d\<beta> p 1"
-  shows "\<exists> (z::int). \<forall> x < z. Ifm bbs (x#bs) (minusinf p) = Ifm bbs (x#bs) p"
-  (is "?P p" is "\<exists> (z::int). \<forall> x < z. ?I x (?M p) = ?I x p")
-using linp u
-proof (induct p rule: minusinf.induct)
-  case (1 p q) thus ?case 
-    by auto (rule_tac x="min z za" in exI,simp)
-next
-  case (2 p q) thus ?case 
-    by auto (rule_tac x="min z za" in exI,simp)
-next
-  case (3 c e) hence c1: "c=1" and nb: "numbound0 e" by simp+
-  fix a
-  from 3 have "\<forall> x<(- Inum (a#bs) e). c*x + Inum (x#bs) e \<noteq> 0"
-  proof(clarsimp)
-    fix x assume "x < (- Inum (a#bs) e)" and"x + Inum (x#bs) e = 0"
-    with numbound0_I[OF nb, where bs="bs" and b="a" and b'="x"]
-    show "False" by simp
-  qed
-  thus ?case by auto
-next
-  case (4 c e) hence c1: "c=1" and nb: "numbound0 e" by simp+
-  fix a
-  from 4 have "\<forall> x<(- Inum (a#bs) e). c*x + Inum (x#bs) e \<noteq> 0"
-  proof(clarsimp)
-    fix x assume "x < (- Inum (a#bs) e)" and"x + Inum (x#bs) e = 0"
-    with numbound0_I[OF nb, where bs="bs" and b="a" and b'="x"]
-    show "False" by simp
-  qed
-  thus ?case by auto
-next
-  case (5 c e) hence c1: "c=1" and nb: "numbound0 e" by simp+
-  fix a
-  from 5 have "\<forall> x<(- Inum (a#bs) e). c*x + Inum (x#bs) e < 0"
-  proof(clarsimp)
-    fix x assume "x < (- Inum (a#bs) e)" 
-    with numbound0_I[OF nb, where bs="bs" and b="a" and b'="x"]
-    show "x + Inum (x#bs) e < 0" by simp
-  qed
-  thus ?case by auto
-next
-  case (6 c e) hence c1: "c=1" and nb: "numbound0 e" by simp+
-  fix a
-  from 6 have "\<forall> x<(- Inum (a#bs) e). c*x + Inum (x#bs) e \<le> 0"
-  proof(clarsimp)
-    fix x assume "x < (- Inum (a#bs) e)" 
-    with numbound0_I[OF nb, where bs="bs" and b="a" and b'="x"]
-    show "x + Inum (x#bs) e \<le> 0" by simp
-  qed
-  thus ?case by auto
-next
-  case (7 c e) hence c1: "c=1" and nb: "numbound0 e" by simp+
-  fix a
-  from 7 have "\<forall> x<(- Inum (a#bs) e). \<not> (c*x + Inum (x#bs) e > 0)"
-  proof(clarsimp)
-    fix x assume "x < (- Inum (a#bs) e)" and"x + Inum (x#bs) e > 0"
-    with numbound0_I[OF nb, where bs="bs" and b="a" and b'="x"]
-    show "False" by simp
-  qed
-  thus ?case by auto
-next
-  case (8 c e) hence c1: "c=1" and nb: "numbound0 e" by simp+
-  fix a
-  from 8 have "\<forall> x<(- Inum (a#bs) e). \<not> (c*x + Inum (x#bs) e \<ge> 0)"
-  proof(clarsimp)
-    fix x assume "x < (- Inum (a#bs) e)" and"x + Inum (x#bs) e \<ge> 0"
-    with numbound0_I[OF nb, where bs="bs" and b="a" and b'="x"]
-    show "False" by simp
-  qed
-  thus ?case by auto
-qed auto
-
-lemma minusinf_repeats:
-  assumes d: "d\<delta> p d" and linp: "iszlfm p"
-  shows "Ifm bbs ((x - k*d)#bs) (minusinf p) = Ifm bbs (x #bs) (minusinf p)"
-using linp d
-proof(induct p rule: iszlfm.induct) 
-  case (9 i c e) hence nbe: "numbound0 e"  and id: "i dvd d" by simp+
-    hence "\<exists> k. d=i*k" by (simp add: dvd_def)
-    then obtain "di" where di_def: "d=i*di" by blast
-    show ?case 
-    proof(simp add: numbound0_I[OF nbe,where bs="bs" and b="x - k * d" and b'="x"] right_diff_distrib, rule iffI)
-      assume 
-	"i dvd c * x - c*(k*d) + Inum (x # bs) e"
-      (is "?ri dvd ?rc*?rx - ?rc*(?rk*?rd) + ?I x e" is "?ri dvd ?rt")
-      hence "\<exists> (l::int). ?rt = i * l" by (simp add: dvd_def)
-      hence "\<exists> (l::int). c*x+ ?I x e = i*l+c*(k * i*di)" 
-	by (simp add: algebra_simps di_def)
-      hence "\<exists> (l::int). c*x+ ?I x e = i*(l + c*k*di)"
-	by (simp add: algebra_simps)
-      hence "\<exists> (l::int). c*x+ ?I x e = i*l" by blast
-      thus "i dvd c*x + Inum (x # bs) e" by (simp add: dvd_def) 
-    next
-      assume 
-	"i dvd c*x + Inum (x # bs) e" (is "?ri dvd ?rc*?rx+?e")
-      hence "\<exists> (l::int). c*x+?e = i*l" by (simp add: dvd_def)
-      hence "\<exists> (l::int). c*x - c*(k*d) +?e = i*l - c*(k*d)" by simp
-      hence "\<exists> (l::int). c*x - c*(k*d) +?e = i*l - c*(k*i*di)" by (simp add: di_def)
-      hence "\<exists> (l::int). c*x - c*(k*d) +?e = i*((l - c*k*di))" by (simp add: algebra_simps)
-      hence "\<exists> (l::int). c*x - c * (k*d) +?e = i*l"
-	by blast
-      thus "i dvd c*x - c*(k*d) + Inum (x # bs) e" by (simp add: dvd_def)
-    qed
-next
-  case (10 i c e)  hence nbe: "numbound0 e"  and id: "i dvd d" by simp+
-    hence "\<exists> k. d=i*k" by (simp add: dvd_def)
-    then obtain "di" where di_def: "d=i*di" by blast
-    show ?case 
-    proof(simp add: numbound0_I[OF nbe,where bs="bs" and b="x - k * d" and b'="x"] right_diff_distrib, rule iffI)
-      assume 
-	"i dvd c * x - c*(k*d) + Inum (x # bs) e"
-      (is "?ri dvd ?rc*?rx - ?rc*(?rk*?rd) + ?I x e" is "?ri dvd ?rt")
-      hence "\<exists> (l::int). ?rt = i * l" by (simp add: dvd_def)
-      hence "\<exists> (l::int). c*x+ ?I x e = i*l+c*(k * i*di)" 
-	by (simp add: algebra_simps di_def)
-      hence "\<exists> (l::int). c*x+ ?I x e = i*(l + c*k*di)"
-	by (simp add: algebra_simps)
-      hence "\<exists> (l::int). c*x+ ?I x e = i*l" by blast
-      thus "i dvd c*x + Inum (x # bs) e" by (simp add: dvd_def) 
-    next
-      assume 
-	"i dvd c*x + Inum (x # bs) e" (is "?ri dvd ?rc*?rx+?e")
-      hence "\<exists> (l::int). c*x+?e = i*l" by (simp add: dvd_def)
-      hence "\<exists> (l::int). c*x - c*(k*d) +?e = i*l - c*(k*d)" by simp
-      hence "\<exists> (l::int). c*x - c*(k*d) +?e = i*l - c*(k*i*di)" by (simp add: di_def)
-      hence "\<exists> (l::int). c*x - c*(k*d) +?e = i*((l - c*k*di))" by (simp add: algebra_simps)
-      hence "\<exists> (l::int). c*x - c * (k*d) +?e = i*l"
-	by blast
-      thus "i dvd c*x - c*(k*d) + Inum (x # bs) e" by (simp add: dvd_def)
-    qed
-qed (auto simp add: gr0_conv_Suc numbound0_I[where bs="bs" and b="x - k*d" and b'="x"])
-
-lemma mirror\<alpha>\<beta>:
-  assumes lp: "iszlfm p"
-  shows "(Inum (i#bs)) ` set (\<alpha> p) = (Inum (i#bs)) ` set (\<beta> (mirror p))"
-using lp
-by (induct p rule: mirror.induct, auto)
-
-lemma mirror: 
-  assumes lp: "iszlfm p"
-  shows "Ifm bbs (x#bs) (mirror p) = Ifm bbs ((- x)#bs) p" 
-using lp
-proof(induct p rule: iszlfm.induct)
-  case (9 j c e) hence nb: "numbound0 e" by simp
-  have "Ifm bbs (x#bs) (mirror (Dvd j (CN 0 c e))) = (j dvd c*x - Inum (x#bs) e)" (is "_ = (j dvd c*x - ?e)") by simp
-    also have "\<dots> = (j dvd (- (c*x - ?e)))"
-    by (simp only: zdvd_zminus_iff)
-  also have "\<dots> = (j dvd (c* (- x)) + ?e)"
-    apply (simp only: minus_mult_right[symmetric] minus_mult_left[symmetric] diff_def zadd_ac zminus_zadd_distrib)
-    by (simp add: algebra_simps)
-  also have "\<dots> = Ifm bbs ((- x)#bs) (Dvd j (CN 0 c e))"
-    using numbound0_I[OF nb, where bs="bs" and b="x" and b'="- x"]
-    by simp
-  finally show ?case .
-next
-    case (10 j c e) hence nb: "numbound0 e" by simp
-  have "Ifm bbs (x#bs) (mirror (Dvd j (CN 0 c e))) = (j dvd c*x - Inum (x#bs) e)" (is "_ = (j dvd c*x - ?e)") by simp
-    also have "\<dots> = (j dvd (- (c*x - ?e)))"
-    by (simp only: zdvd_zminus_iff)
-  also have "\<dots> = (j dvd (c* (- x)) + ?e)"
-    apply (simp only: minus_mult_right[symmetric] minus_mult_left[symmetric] diff_def zadd_ac zminus_zadd_distrib)
-    by (simp add: algebra_simps)
-  also have "\<dots> = Ifm bbs ((- x)#bs) (Dvd j (CN 0 c e))"
-    using numbound0_I[OF nb, where bs="bs" and b="x" and b'="- x"]
-    by simp
-  finally show ?case by simp
-qed (auto simp add: numbound0_I[where bs="bs" and b="x" and b'="- x"] gr0_conv_Suc)
-
-lemma mirror_l: "iszlfm p \<and> d\<beta> p 1 
-  \<Longrightarrow> iszlfm (mirror p) \<and> d\<beta> (mirror p) 1"
-by (induct p rule: mirror.induct, auto)
-
-lemma mirror_\<delta>: "iszlfm p \<Longrightarrow> \<delta> (mirror p) = \<delta> p"
-by (induct p rule: mirror.induct,auto)
-
-lemma \<beta>_numbound0: assumes lp: "iszlfm p"
-  shows "\<forall> b\<in> set (\<beta> p). numbound0 b"
-  using lp by (induct p rule: \<beta>.induct,auto)
-
-lemma d\<beta>_mono: 
-  assumes linp: "iszlfm p"
-  and dr: "d\<beta> p l"
-  and d: "l dvd l'"
-  shows "d\<beta> p l'"
-using dr linp zdvd_trans[where n="l" and k="l'", simplified d]
-by (induct p rule: iszlfm.induct) simp_all
-
-lemma \<alpha>_l: assumes lp: "iszlfm p"
-  shows "\<forall> b\<in> set (\<alpha> p). numbound0 b"
-using lp
-by(induct p rule: \<alpha>.induct, auto)
-
-lemma \<zeta>: 
-  assumes linp: "iszlfm p"
-  shows "\<zeta> p > 0 \<and> d\<beta> p (\<zeta> p)"
-using linp
-proof(induct p rule: iszlfm.induct)
-  case (1 p q)
-  from prems have dl1: "\<zeta> p dvd zlcm (\<zeta> p) (\<zeta> q)" by simp
-  from prems have dl2: "\<zeta> q dvd zlcm (\<zeta> p) (\<zeta> q)"  by simp
-  from prems d\<beta>_mono[where p = "p" and l="\<zeta> p" and l'="zlcm (\<zeta> p) (\<zeta> q)"] 
-    d\<beta>_mono[where p = "q" and l="\<zeta> q" and l'="zlcm (\<zeta> p) (\<zeta> q)"] 
-    dl1 dl2 show ?case by (auto simp add: zlcm_pos)
-next
-  case (2 p q)
-  from prems have dl1: "\<zeta> p dvd zlcm (\<zeta> p) (\<zeta> q)" by simp
-  from prems have dl2: "\<zeta> q dvd zlcm (\<zeta> p) (\<zeta> q)" by simp
-  from prems d\<beta>_mono[where p = "p" and l="\<zeta> p" and l'="zlcm (\<zeta> p) (\<zeta> q)"] 
-    d\<beta>_mono[where p = "q" and l="\<zeta> q" and l'="zlcm (\<zeta> p) (\<zeta> q)"] 
-    dl1 dl2 show ?case by (auto simp add: zlcm_pos)
-qed (auto simp add: zlcm_pos)
-
-lemma a\<beta>: assumes linp: "iszlfm p" and d: "d\<beta> p l" and lp: "l > 0"
-  shows "iszlfm (a\<beta> p l) \<and> d\<beta> (a\<beta> p l) 1 \<and> (Ifm bbs (l*x #bs) (a\<beta> p l) = Ifm bbs (x#bs) p)"
-using linp d
-proof (induct p rule: iszlfm.induct)
-  case (5 c e) hence cp: "c>0" and be: "numbound0 e" and d': "c dvd l" by simp+
-    from lp cp have clel: "c\<le>l" by (simp add: zdvd_imp_le [OF d' lp])
-    from cp have cnz: "c \<noteq> 0" by simp
-    have "c div c\<le> l div c"
-      by (simp add: zdiv_mono1[OF clel cp])
-    then have ldcp:"0 < l div c" 
-      by (simp add: zdiv_self[OF cnz])
-    have "c * (l div c) = c* (l div c) + l mod c" using d' zdvd_iff_zmod_eq_0[where m="c" and n="l"] by simp
-    hence cl:"c * (l div c) =l" using zmod_zdiv_equality[where a="l" and b="c", symmetric] 
-      by simp
-    hence "(l*x + (l div c) * Inum (x # bs) e < 0) =
-          ((c * (l div c)) * x + (l div c) * Inum (x # bs) e < 0)"
-      by simp
-    also have "\<dots> = ((l div c) * (c*x + Inum (x # bs) e) < (l div c) * 0)" by (simp add: algebra_simps)
-    also have "\<dots> = (c*x + Inum (x # bs) e < 0)"
-    using mult_less_0_iff [where a="(l div c)" and b="c*x + Inum (x # bs) e"] ldcp by simp
-  finally show ?case using numbound0_I[OF be,where b="l*x" and b'="x" and bs="bs"] be  by simp
-next
-  case (6 c e) hence cp: "c>0" and be: "numbound0 e" and d': "c dvd l" by simp+
-    from lp cp have clel: "c\<le>l" by (simp add: zdvd_imp_le [OF d' lp])
-    from cp have cnz: "c \<noteq> 0" by simp
-    have "c div c\<le> l div c"
-      by (simp add: zdiv_mono1[OF clel cp])
-    then have ldcp:"0 < l div c" 
-      by (simp add: zdiv_self[OF cnz])
-    have "c * (l div c) = c* (l div c) + l mod c" using d' zdvd_iff_zmod_eq_0[where m="c" and n="l"] by simp
-    hence cl:"c * (l div c) =l" using zmod_zdiv_equality[where a="l" and b="c", symmetric] 
-      by simp
-    hence "(l*x + (l div c) * Inum (x# bs) e \<le> 0) =
-          ((c * (l div c)) * x + (l div c) * Inum (x # bs) e \<le> 0)"
-      by simp
-    also have "\<dots> = ((l div c) * (c * x + Inum (x # bs) e) \<le> ((l div c)) * 0)" by (simp add: algebra_simps)
-    also have "\<dots> = (c*x + Inum (x # bs) e \<le> 0)"
-    using mult_le_0_iff [where a="(l div c)" and b="c*x + Inum (x # bs) e"] ldcp by simp
-  finally show ?case using numbound0_I[OF be,where b="l*x" and b'="x" and bs="bs"]  be by simp
-next
-  case (7 c e) hence cp: "c>0" and be: "numbound0 e" and d': "c dvd l" by simp+
-    from lp cp have clel: "c\<le>l" by (simp add: zdvd_imp_le [OF d' lp])
-    from cp have cnz: "c \<noteq> 0" by simp
-    have "c div c\<le> l div c"
-      by (simp add: zdiv_mono1[OF clel cp])
-    then have ldcp:"0 < l div c" 
-      by (simp add: zdiv_self[OF cnz])
-    have "c * (l div c) = c* (l div c) + l mod c" using d' zdvd_iff_zmod_eq_0[where m="c" and n="l"] by simp
-    hence cl:"c * (l div c) =l" using zmod_zdiv_equality[where a="l" and b="c", symmetric] 
-      by simp
-    hence "(l*x + (l div c)* Inum (x # bs) e > 0) =
-          ((c * (l div c)) * x + (l div c) * Inum (x # bs) e > 0)"
-      by simp
-    also have "\<dots> = ((l div c) * (c * x + Inum (x # bs) e) > ((l div c)) * 0)" by (simp add: algebra_simps)
-    also have "\<dots> = (c * x + Inum (x # bs) e > 0)"
-    using zero_less_mult_iff [where a="(l div c)" and b="c * x + Inum (x # bs) e"] ldcp by simp
-  finally show ?case using numbound0_I[OF be,where b="(l * x)" and b'="x" and bs="bs"]  be  by simp
-next
-  case (8 c e) hence cp: "c>0" and be: "numbound0 e" and d': "c dvd l" by simp+
-    from lp cp have clel: "c\<le>l" by (simp add: zdvd_imp_le [OF d' lp])
-    from cp have cnz: "c \<noteq> 0" by simp
-    have "c div c\<le> l div c"
-      by (simp add: zdiv_mono1[OF clel cp])
-    then have ldcp:"0 < l div c" 
-      by (simp add: zdiv_self[OF cnz])
-    have "c * (l div c) = c* (l div c) + l mod c" using d' zdvd_iff_zmod_eq_0[where m="c" and n="l"] by simp
-    hence cl:"c * (l div c) =l" using zmod_zdiv_equality[where a="l" and b="c", symmetric] 
-      by simp
-    hence "(l*x + (l div c)* Inum (x # bs) e \<ge> 0) =
-          ((c*(l div c))*x + (l div c)* Inum (x # bs) e \<ge> 0)"
-      by simp
-    also have "\<dots> = ((l div c)*(c*x + Inum (x # bs) e) \<ge> ((l div c)) * 0)" 
-      by (simp add: algebra_simps)
-    also have "\<dots> = (c*x + Inum (x # bs) e \<ge> 0)" using ldcp 
-      zero_le_mult_iff [where a="l div c" and b="c*x + Inum (x # bs) e"] by simp
-  finally show ?case using be numbound0_I[OF be,where b="l*x" and b'="x" and bs="bs"]  
-    by simp
-next
-  case (3 c e) hence cp: "c>0" and be: "numbound0 e" and d': "c dvd l" by simp+
-    from lp cp have clel: "c\<le>l" by (simp add: zdvd_imp_le [OF d' lp])
-    from cp have cnz: "c \<noteq> 0" by simp
-    have "c div c\<le> l div c"
-      by (simp add: zdiv_mono1[OF clel cp])
-    then have ldcp:"0 < l div c" 
-      by (simp add: zdiv_self[OF cnz])
-    have "c * (l div c) = c* (l div c) + l mod c" using d' zdvd_iff_zmod_eq_0[where m="c" and n="l"] by simp
-    hence cl:"c * (l div c) =l" using zmod_zdiv_equality[where a="l" and b="c", symmetric] 
-      by simp
-    hence "(l * x + (l div c) * Inum (x # bs) e = 0) =
-          ((c * (l div c)) * x + (l div c) * Inum (x # bs) e = 0)"
-      by simp
-    also have "\<dots> = ((l div c) * (c * x + Inum (x # bs) e) = ((l div c)) * 0)" by (simp add: algebra_simps)
-    also have "\<dots> = (c * x + Inum (x # bs) e = 0)"
-    using mult_eq_0_iff [where a="(l div c)" and b="c * x + Inum (x # bs) e"] ldcp by simp
-  finally show ?case using numbound0_I[OF be,where b="(l * x)" and b'="x" and bs="bs"]  be  by simp
-next
-  case (4 c e) hence cp: "c>0" and be: "numbound0 e" and d': "c dvd l" by simp+
-    from lp cp have clel: "c\<le>l" by (simp add: zdvd_imp_le [OF d' lp])
-    from cp have cnz: "c \<noteq> 0" by simp
-    have "c div c\<le> l div c"
-      by (simp add: zdiv_mono1[OF clel cp])
-    then have ldcp:"0 < l div c" 
-      by (simp add: zdiv_self[OF cnz])
-    have "c * (l div c) = c* (l div c) + l mod c" using d' zdvd_iff_zmod_eq_0[where m="c" and n="l"] by simp
-    hence cl:"c * (l div c) =l" using zmod_zdiv_equality[where a="l" and b="c", symmetric] 
-      by simp
-    hence "(l * x + (l div c) * Inum (x # bs) e \<noteq> 0) =
-          ((c * (l div c)) * x + (l div c) * Inum (x # bs) e \<noteq> 0)"
-      by simp
-    also have "\<dots> = ((l div c) * (c * x + Inum (x # bs) e) \<noteq> ((l div c)) * 0)" by (simp add: algebra_simps)
-    also have "\<dots> = (c * x + Inum (x # bs) e \<noteq> 0)"
-    using zero_le_mult_iff [where a="(l div c)" and b="c * x + Inum (x # bs) e"] ldcp by simp
-  finally show ?case using numbound0_I[OF be,where b="(l * x)" and b'="x" and bs="bs"]  be  by simp
-next
-  case (9 j c e) hence cp: "c>0" and be: "numbound0 e" and jp: "j > 0" and d': "c dvd l" by simp+
-    from lp cp have clel: "c\<le>l" by (simp add: zdvd_imp_le [OF d' lp])
-    from cp have cnz: "c \<noteq> 0" by simp
-    have "c div c\<le> l div c"
-      by (simp add: zdiv_mono1[OF clel cp])
-    then have ldcp:"0 < l div c" 
-      by (simp add: zdiv_self[OF cnz])
-    have "c * (l div c) = c* (l div c) + l mod c" using d' zdvd_iff_zmod_eq_0[where m="c" and n="l"] by simp
-    hence cl:"c * (l div c) =l" using zmod_zdiv_equality[where a="l" and b="c", symmetric] 
-      by simp
-    hence "(\<exists> (k::int). l * x + (l div c) * Inum (x # bs) e = ((l div c) * j) * k) = (\<exists> (k::int). (c * (l div c)) * x + (l div c) * Inum (x # bs) e = ((l div c) * j) * k)"  by simp
-    also have "\<dots> = (\<exists> (k::int). (l div c) * (c * x + Inum (x # bs) e - j * k) = (l div c)*0)" by (simp add: algebra_simps)
-    also fix k have "\<dots> = (\<exists> (k::int). c * x + Inum (x # bs) e - j * k = 0)"
-    using zero_le_mult_iff [where a="(l div c)" and b="c * x + Inum (x # bs) e - j * k"] ldcp by simp
-  also have "\<dots> = (\<exists> (k::int). c * x + Inum (x # bs) e = j * k)" by simp
-  finally show ?case using numbound0_I[OF be,where b="(l * x)" and b'="x" and bs="bs"] be  mult_strict_mono[OF ldcp jp ldcp ] by (simp add: dvd_def)
-next
-  case (10 j c e) hence cp: "c>0" and be: "numbound0 e" and jp: "j > 0" and d': "c dvd l" by simp+
-    from lp cp have clel: "c\<le>l" by (simp add: zdvd_imp_le [OF d' lp])
-    from cp have cnz: "c \<noteq> 0" by simp
-    have "c div c\<le> l div c"
-      by (simp add: zdiv_mono1[OF clel cp])
-    then have ldcp:"0 < l div c" 
-      by (simp add: zdiv_self[OF cnz])
-    have "c * (l div c) = c* (l div c) + l mod c" using d' zdvd_iff_zmod_eq_0[where m="c" and n="l"] by simp
-    hence cl:"c * (l div c) =l" using zmod_zdiv_equality[where a="l" and b="c", symmetric] 
-      by simp
-    hence "(\<exists> (k::int). l * x + (l div c) * Inum (x # bs) e = ((l div c) * j) * k) = (\<exists> (k::int). (c * (l div c)) * x + (l div c) * Inum (x # bs) e = ((l div c) * j) * k)"  by simp
-    also have "\<dots> = (\<exists> (k::int). (l div c) * (c * x + Inum (x # bs) e - j * k) = (l div c)*0)" by (simp add: algebra_simps)
-    also fix k have "\<dots> = (\<exists> (k::int). c * x + Inum (x # bs) e - j * k = 0)"
-    using zero_le_mult_iff [where a="(l div c)" and b="c * x + Inum (x # bs) e - j * k"] ldcp by simp
-  also have "\<dots> = (\<exists> (k::int). c * x + Inum (x # bs) e = j * k)" by simp
-  finally show ?case using numbound0_I[OF be,where b="(l * x)" and b'="x" and bs="bs"] be  mult_strict_mono[OF ldcp jp ldcp ] by (simp add: dvd_def)
-qed (auto simp add: gr0_conv_Suc numbound0_I[where bs="bs" and b="(l * x)" and b'="x"])
-
-lemma a\<beta>_ex: assumes linp: "iszlfm p" and d: "d\<beta> p l" and lp: "l>0"
-  shows "(\<exists> x. l dvd x \<and> Ifm bbs (x #bs) (a\<beta> p l)) = (\<exists> (x::int). Ifm bbs (x#bs) p)"
-  (is "(\<exists> x. l dvd x \<and> ?P x) = (\<exists> x. ?P' x)")
-proof-
-  have "(\<exists> x. l dvd x \<and> ?P x) = (\<exists> (x::int). ?P (l*x))"
-    using unity_coeff_ex[where l="l" and P="?P", simplified] by simp
-  also have "\<dots> = (\<exists> (x::int). ?P' x)" using a\<beta>[OF linp d lp] by simp
-  finally show ?thesis  . 
-qed
-
-lemma \<beta>:
-  assumes lp: "iszlfm p"
-  and u: "d\<beta> p 1"
-  and d: "d\<delta> p d"
-  and dp: "d > 0"
-  and nob: "\<not>(\<exists>(j::int) \<in> {1 .. d}. \<exists> b\<in> (Inum (a#bs)) ` set(\<beta> p). x = b + j)"
-  and p: "Ifm bbs (x#bs) p" (is "?P x")
-  shows "?P (x - d)"
-using lp u d dp nob p
-proof(induct p rule: iszlfm.induct)
-  case (5 c e) hence c1: "c=1" and  bn:"numbound0 e" by simp+
-    with dp p c1 numbound0_I[OF bn,where b="(x-d)" and b'="x" and bs="bs"] prems
-    show ?case by simp
-next
-  case (6 c e)  hence c1: "c=1" and  bn:"numbound0 e" by simp+
-    with dp p c1 numbound0_I[OF bn,where b="(x-d)" and b'="x" and bs="bs"] prems
-    show ?case by simp
-next
-  case (7 c e) hence p: "Ifm bbs (x #bs) (Gt (CN 0 c e))" and c1: "c=1" and bn:"numbound0 e" by simp+
-    let ?e = "Inum (x # bs) e"
-    {assume "(x-d) +?e > 0" hence ?case using c1 
-      numbound0_I[OF bn,where b="(x-d)" and b'="x" and bs="bs"] by simp}
-    moreover
-    {assume H: "\<not> (x-d) + ?e > 0" 
-      let ?v="Neg e"
-      have vb: "?v \<in> set (\<beta> (Gt (CN 0 c e)))" by simp
-      from prems(11)[simplified simp_thms Inum.simps \<beta>.simps set.simps bex_simps numbound0_I[OF bn,where b="a" and b'="x" and bs="bs"]] 
-      have nob: "\<not> (\<exists> j\<in> {1 ..d}. x =  - ?e + j)" by auto 
-      from H p have "x + ?e > 0 \<and> x + ?e \<le> d" by (simp add: c1)
-      hence "x + ?e \<ge> 1 \<and> x + ?e \<le> d"  by simp
-      hence "\<exists> (j::int) \<in> {1 .. d}. j = x + ?e" by simp
-      hence "\<exists> (j::int) \<in> {1 .. d}. x = (- ?e + j)" 
-	by (simp add: algebra_simps)
-      with nob have ?case by auto}
-    ultimately show ?case by blast
-next
-  case (8 c e) hence p: "Ifm bbs (x #bs) (Ge (CN 0 c e))" and c1: "c=1" and bn:"numbound0 e" 
-    by simp+
-    let ?e = "Inum (x # bs) e"
-    {assume "(x-d) +?e \<ge> 0" hence ?case using  c1 
-      numbound0_I[OF bn,where b="(x-d)" and b'="x" and bs="bs"]
-	by simp}
-    moreover
-    {assume H: "\<not> (x-d) + ?e \<ge> 0" 
-      let ?v="Sub (C -1) e"
-      have vb: "?v \<in> set (\<beta> (Ge (CN 0 c e)))" by simp
-      from prems(11)[simplified simp_thms Inum.simps \<beta>.simps set.simps bex_simps numbound0_I[OF bn,where b="a" and b'="x" and bs="bs"]] 
-      have nob: "\<not> (\<exists> j\<in> {1 ..d}. x =  - ?e - 1 + j)" by auto 
-      from H p have "x + ?e \<ge> 0 \<and> x + ?e < d" by (simp add: c1)
-      hence "x + ?e +1 \<ge> 1 \<and> x + ?e + 1 \<le> d"  by simp
-      hence "\<exists> (j::int) \<in> {1 .. d}. j = x + ?e + 1" by simp
-      hence "\<exists> (j::int) \<in> {1 .. d}. x= - ?e - 1 + j" by (simp add: algebra_simps)
-      with nob have ?case by simp }
-    ultimately show ?case by blast
-next
-  case (3 c e) hence p: "Ifm bbs (x #bs) (Eq (CN 0 c e))" (is "?p x") and c1: "c=1" and bn:"numbound0 e" by simp+
-    let ?e = "Inum (x # bs) e"
-    let ?v="(Sub (C -1) e)"
-    have vb: "?v \<in> set (\<beta> (Eq (CN 0 c e)))" by simp
-    from p have "x= - ?e" by (simp add: c1) with prems(11) show ?case using dp
-      by simp (erule ballE[where x="1"],
-	simp_all add:algebra_simps numbound0_I[OF bn,where b="x"and b'="a"and bs="bs"])
-next
-  case (4 c e)hence p: "Ifm bbs (x #bs) (NEq (CN 0 c e))" (is "?p x") and c1: "c=1" and bn:"numbound0 e" by simp+
-    let ?e = "Inum (x # bs) e"
-    let ?v="Neg e"
-    have vb: "?v \<in> set (\<beta> (NEq (CN 0 c e)))" by simp
-    {assume "x - d + Inum (((x -d)) # bs) e \<noteq> 0" 
-      hence ?case by (simp add: c1)}
-    moreover
-    {assume H: "x - d + Inum (((x -d)) # bs) e = 0"
-      hence "x = - Inum (((x -d)) # bs) e + d" by simp
-      hence "x = - Inum (a # bs) e + d"
-	by (simp add: numbound0_I[OF bn,where b="x - d"and b'="a"and bs="bs"])
-       with prems(11) have ?case using dp by simp}
-  ultimately show ?case by blast
-next 
-  case (9 j c e) hence p: "Ifm bbs (x #bs) (Dvd j (CN 0 c e))" (is "?p x") and c1: "c=1" and bn:"numbound0 e" by simp+
-    let ?e = "Inum (x # bs) e"
-    from prems have id: "j dvd d" by simp
-    from c1 have "?p x = (j dvd (x+ ?e))" by simp
-    also have "\<dots> = (j dvd x - d + ?e)" 
-      using zdvd_period[OF id, where x="x" and c="-1" and t="?e"] by simp
-    finally show ?case 
-      using numbound0_I[OF bn,where b="(x-d)" and b'="x" and bs="bs"] c1 p by simp
-next
-  case (10 j c e) hence p: "Ifm bbs (x #bs) (NDvd j (CN 0 c e))" (is "?p x") and c1: "c=1" and bn:"numbound0 e" by simp+
-    let ?e = "Inum (x # bs) e"
-    from prems have id: "j dvd d" by simp
-    from c1 have "?p x = (\<not> j dvd (x+ ?e))" by simp
-    also have "\<dots> = (\<not> j dvd x - d + ?e)" 
-      using zdvd_period[OF id, where x="x" and c="-1" and t="?e"] by simp
-    finally show ?case using numbound0_I[OF bn,where b="(x-d)" and b'="x" and bs="bs"] c1 p by simp
-qed (auto simp add: numbound0_I[where bs="bs" and b="(x - d)" and b'="x"] gr0_conv_Suc)
-
-lemma \<beta>':   
-  assumes lp: "iszlfm p"
-  and u: "d\<beta> p 1"
-  and d: "d\<delta> p d"
-  and dp: "d > 0"
-  shows "\<forall> x. \<not>(\<exists>(j::int) \<in> {1 .. d}. \<exists> b\<in> set(\<beta> p). Ifm bbs ((Inum (a#bs) b + j) #bs) p) \<longrightarrow> Ifm bbs (x#bs) p \<longrightarrow> Ifm bbs ((x - d)#bs) p" (is "\<forall> x. ?b \<longrightarrow> ?P x \<longrightarrow> ?P (x - d)")
-proof(clarify)
-  fix x 
-  assume nb:"?b" and px: "?P x" 
-  hence nb2: "\<not>(\<exists>(j::int) \<in> {1 .. d}. \<exists> b\<in> (Inum (a#bs)) ` set(\<beta> p). x = b + j)"
-    by auto
-  from  \<beta>[OF lp u d dp nb2 px] show "?P (x -d )" .
-qed
-lemma cpmi_eq: "0 < D \<Longrightarrow> (EX z::int. ALL x. x < z --> (P x = P1 x))
-==> ALL x.~(EX (j::int) : {1..D}. EX (b::int) : B. P(b+j)) --> P (x) --> P (x - D) 
-==> (ALL (x::int). ALL (k::int). ((P1 x)= (P1 (x-k*D))))
-==> (EX (x::int). P(x)) = ((EX (j::int) : {1..D} . (P1(j))) | (EX (j::int) : {1..D}. EX (b::int) : B. P (b+j)))"
-apply(rule iffI)
-prefer 2
-apply(drule minusinfinity)
-apply assumption+
-apply(fastsimp)
-apply clarsimp
-apply(subgoal_tac "!!k. 0<=k \<Longrightarrow> !x. P x \<longrightarrow> P (x - k*D)")
-apply(frule_tac x = x and z=z in decr_lemma)
-apply(subgoal_tac "P1(x - (\<bar>x - z\<bar> + 1) * D)")
-prefer 2
-apply(subgoal_tac "0 <= (\<bar>x - z\<bar> + 1)")
-prefer 2 apply arith
- apply fastsimp
-apply(drule (1)  periodic_finite_ex)
-apply blast
-apply(blast dest:decr_mult_lemma)
-done
-
-theorem cp_thm:
-  assumes lp: "iszlfm p"
-  and u: "d\<beta> p 1"
-  and d: "d\<delta> p d"
-  and dp: "d > 0"
-  shows "(\<exists> (x::int). Ifm bbs (x #bs) p) = (\<exists> j\<in> {1.. d}. Ifm bbs (j #bs) (minusinf p) \<or> (\<exists> b \<in> set (\<beta> p). Ifm bbs ((Inum (i#bs) b + j) #bs) p))"
-  (is "(\<exists> (x::int). ?P (x)) = (\<exists> j\<in> ?D. ?M j \<or> (\<exists> b\<in> ?B. ?P (?I b + j)))")
-proof-
-  from minusinf_inf[OF lp u] 
-  have th: "\<exists>(z::int). \<forall>x<z. ?P (x) = ?M x" by blast
-  let ?B' = "{?I b | b. b\<in> ?B}"
-  have BB': "(\<exists>j\<in>?D. \<exists>b\<in> ?B. ?P (?I b +j)) = (\<exists> j \<in> ?D. \<exists> b \<in> ?B'. ?P (b + j))" by auto
-  hence th2: "\<forall> x. \<not> (\<exists> j \<in> ?D. \<exists> b \<in> ?B'. ?P ((b + j))) \<longrightarrow> ?P (x) \<longrightarrow> ?P ((x - d))" 
-    using \<beta>'[OF lp u d dp, where a="i" and bbs = "bbs"] by blast
-  from minusinf_repeats[OF d lp]
-  have th3: "\<forall> x k. ?M x = ?M (x-k*d)" by simp
-  from cpmi_eq[OF dp th th2 th3] BB' show ?thesis by blast
-qed
-
-    (* Implement the right hand sides of Cooper's theorem and Ferrante and Rackoff. *)
-lemma mirror_ex: 
-  assumes lp: "iszlfm p"
-  shows "(\<exists> x. Ifm bbs (x#bs) (mirror p)) = (\<exists> x. Ifm bbs (x#bs) p)"
-  (is "(\<exists> x. ?I x ?mp) = (\<exists> x. ?I x p)")
-proof(auto)
-  fix x assume "?I x ?mp" hence "?I (- x) p" using mirror[OF lp] by blast
-  thus "\<exists> x. ?I x p" by blast
-next
-  fix x assume "?I x p" hence "?I (- x) ?mp" 
-    using mirror[OF lp, where x="- x", symmetric] by auto
-  thus "\<exists> x. ?I x ?mp" by blast
-qed
-
-
-lemma cp_thm': 
-  assumes lp: "iszlfm p"
-  and up: "d\<beta> p 1" and dd: "d\<delta> p d" and dp: "d > 0"
-  shows "(\<exists> x. Ifm bbs (x#bs) p) = ((\<exists> j\<in> {1 .. d}. Ifm bbs (j#bs) (minusinf p)) \<or> (\<exists> j\<in> {1.. d}. \<exists> b\<in> (Inum (i#bs)) ` set (\<beta> p). Ifm bbs ((b+j)#bs) p))"
-  using cp_thm[OF lp up dd dp,where i="i"] by auto
-
-constdefs unit:: "fm \<Rightarrow> fm \<times> num list \<times> int"
-  "unit p \<equiv> (let p' = zlfm p ; l = \<zeta> p' ; q = And (Dvd l (CN 0 1 (C 0))) (a\<beta> p' l); d = \<delta> q;
-             B = remdups (map simpnum (\<beta> q)) ; a = remdups (map simpnum (\<alpha> q))
-             in if length B \<le> length a then (q,B,d) else (mirror q, a,d))"
-
-lemma unit: assumes qf: "qfree p"
-  shows "\<And> q B d. unit p = (q,B,d) \<Longrightarrow> ((\<exists> x. Ifm bbs (x#bs) p) = (\<exists> x. Ifm bbs (x#bs) q)) \<and> (Inum (i#bs)) ` set B = (Inum (i#bs)) ` set (\<beta> q) \<and> d\<beta> q 1 \<and> d\<delta> q d \<and> d >0 \<and> iszlfm q \<and> (\<forall> b\<in> set B. numbound0 b)"
-proof-
-  fix q B d 
-  assume qBd: "unit p = (q,B,d)"
-  let ?thes = "((\<exists> x. Ifm bbs (x#bs) p) = (\<exists> x. Ifm bbs (x#bs) q)) \<and>
-    Inum (i#bs) ` set B = Inum (i#bs) ` set (\<beta> q) \<and>
-    d\<beta> q 1 \<and> d\<delta> q d \<and> 0 < d \<and> iszlfm q \<and> (\<forall> b\<in> set B. numbound0 b)"
-  let ?I = "\<lambda> x p. Ifm bbs (x#bs) p"
-  let ?p' = "zlfm p"
-  let ?l = "\<zeta> ?p'"
-  let ?q = "And (Dvd ?l (CN 0 1 (C 0))) (a\<beta> ?p' ?l)"
-  let ?d = "\<delta> ?q"
-  let ?B = "set (\<beta> ?q)"
-  let ?B'= "remdups (map simpnum (\<beta> ?q))"
-  let ?A = "set (\<alpha> ?q)"
-  let ?A'= "remdups (map simpnum (\<alpha> ?q))"
-  from conjunct1[OF zlfm_I[OF qf, where bs="bs"]] 
-  have pp': "\<forall> i. ?I i ?p' = ?I i p" by auto
-  from conjunct2[OF zlfm_I[OF qf, where bs="bs" and i="i"]]
-  have lp': "iszlfm ?p'" . 
-  from lp' \<zeta>[where p="?p'"] have lp: "?l >0" and dl: "d\<beta> ?p' ?l" by auto
-  from a\<beta>_ex[where p="?p'" and l="?l" and bs="bs", OF lp' dl lp] pp'
-  have pq_ex:"(\<exists> (x::int). ?I x p) = (\<exists> x. ?I x ?q)" by simp 
-  from lp' lp a\<beta>[OF lp' dl lp] have lq:"iszlfm ?q" and uq: "d\<beta> ?q 1"  by auto
-  from \<delta>[OF lq] have dp:"?d >0" and dd: "d\<delta> ?q ?d" by blast+
-  let ?N = "\<lambda> t. Inum (i#bs) t"
-  have "?N ` set ?B' = ((?N o simpnum) ` ?B)" by auto 
-  also have "\<dots> = ?N ` ?B" using simpnum_ci[where bs="i#bs"] by auto
-  finally have BB': "?N ` set ?B' = ?N ` ?B" .
-  have "?N ` set ?A' = ((?N o simpnum) ` ?A)" by auto 
-  also have "\<dots> = ?N ` ?A" using simpnum_ci[where bs="i#bs"] by auto
-  finally have AA': "?N ` set ?A' = ?N ` ?A" .
-  from \<beta>_numbound0[OF lq] have B_nb:"\<forall> b\<in> set ?B'. numbound0 b"
-    by (simp add: simpnum_numbound0)
-  from \<alpha>_l[OF lq] have A_nb: "\<forall> b\<in> set ?A'. numbound0 b"
-    by (simp add: simpnum_numbound0)
-    {assume "length ?B' \<le> length ?A'"
-    hence q:"q=?q" and "B = ?B'" and d:"d = ?d"
-      using qBd by (auto simp add: Let_def unit_def)
-    with BB' B_nb have b: "?N ` (set B) = ?N ` set (\<beta> q)" 
-      and bn: "\<forall>b\<in> set B. numbound0 b" by simp+ 
-  with pq_ex dp uq dd lq q d have ?thes by simp}
-  moreover 
-  {assume "\<not> (length ?B' \<le> length ?A')"
-    hence q:"q=mirror ?q" and "B = ?A'" and d:"d = ?d"
-      using qBd by (auto simp add: Let_def unit_def)
-    with AA' mirror\<alpha>\<beta>[OF lq] A_nb have b:"?N ` (set B) = ?N ` set (\<beta> q)" 
-      and bn: "\<forall>b\<in> set B. numbound0 b" by simp+
-    from mirror_ex[OF lq] pq_ex q 
-    have pqm_eq:"(\<exists> (x::int). ?I x p) = (\<exists> (x::int). ?I x q)" by simp
-    from lq uq q mirror_l[where p="?q"]
-    have lq': "iszlfm q" and uq: "d\<beta> q 1" by auto
-    from \<delta>[OF lq'] mirror_\<delta>[OF lq] q d have dq:"d\<delta> q d " by auto
-    from pqm_eq b bn uq lq' dp dq q dp d have ?thes by simp
-  }
-  ultimately show ?thes by blast
-qed
-    (* Cooper's Algorithm *)
-
-constdefs cooper :: "fm \<Rightarrow> fm"
-  "cooper p \<equiv> 
-  (let (q,B,d) = unit p; js = iupt 1 d;
-       mq = simpfm (minusinf q);
-       md = evaldjf (\<lambda> j. simpfm (subst0 (C j) mq)) js
-   in if md = T then T else
-    (let qd = evaldjf (\<lambda> (b,j). simpfm (subst0 (Add b (C j)) q)) 
-                               [(b,j). b\<leftarrow>B,j\<leftarrow>js]
-     in decr (disj md qd)))"
-lemma cooper: assumes qf: "qfree p"
-  shows "((\<exists> x. Ifm bbs (x#bs) p) = (Ifm bbs bs (cooper p))) \<and> qfree (cooper p)" 
-  (is "(?lhs = ?rhs) \<and> _")
-proof-
-  let ?I = "\<lambda> x p. Ifm bbs (x#bs) p"
-  let ?q = "fst (unit p)"
-  let ?B = "fst (snd(unit p))"
-  let ?d = "snd (snd (unit p))"
-  let ?js = "iupt 1 ?d"
-  let ?mq = "minusinf ?q"
-  let ?smq = "simpfm ?mq"
-  let ?md = "evaldjf (\<lambda> j. simpfm (subst0 (C j) ?smq)) ?js"
-  fix i
-  let ?N = "\<lambda> t. Inum (i#bs) t"
-  let ?Bjs = "[(b,j). b\<leftarrow>?B,j\<leftarrow>?js]"
-  let ?qd = "evaldjf (\<lambda> (b,j). simpfm (subst0 (Add b (C j)) ?q)) ?Bjs"
-  have qbf:"unit p = (?q,?B,?d)" by simp
-  from unit[OF qf qbf] have pq_ex: "(\<exists>(x::int). ?I x p) = (\<exists> (x::int). ?I x ?q)" and 
-    B:"?N ` set ?B = ?N ` set (\<beta> ?q)" and 
-    uq:"d\<beta> ?q 1" and dd: "d\<delta> ?q ?d" and dp: "?d > 0" and 
-    lq: "iszlfm ?q" and 
-    Bn: "\<forall> b\<in> set ?B. numbound0 b" by auto
-  from zlin_qfree[OF lq] have qfq: "qfree ?q" .
-  from simpfm_qf[OF minusinf_qfree[OF qfq]] have qfmq: "qfree ?smq".
-  have jsnb: "\<forall> j \<in> set ?js. numbound0 (C j)" by simp
-  hence "\<forall> j\<in> set ?js. bound0 (subst0 (C j) ?smq)" 
-    by (auto simp only: subst0_bound0[OF qfmq])
-  hence th: "\<forall> j\<in> set ?js. bound0 (simpfm (subst0 (C j) ?smq))"
-    by (auto simp add: simpfm_bound0)
-  from evaldjf_bound0[OF th] have mdb: "bound0 ?md" by simp 
-  from Bn jsnb have "\<forall> (b,j) \<in> set ?Bjs. numbound0 (Add b (C j))"
-    by simp
-  hence "\<forall> (b,j) \<in> set ?Bjs. bound0 (subst0 (Add b (C j)) ?q)"
-    using subst0_bound0[OF qfq] by blast
-  hence "\<forall> (b,j) \<in> set ?Bjs. bound0 (simpfm (subst0 (Add b (C j)) ?q))"
-    using simpfm_bound0  by blast
-  hence th': "\<forall> x \<in> set ?Bjs. bound0 ((\<lambda> (b,j). simpfm (subst0 (Add b (C j)) ?q)) x)"
-    by auto 
-  from evaldjf_bound0 [OF th'] have qdb: "bound0 ?qd" by simp
-  from mdb qdb 
-  have mdqdb: "bound0 (disj ?md ?qd)" by (simp only: disj_def, cases "?md=T \<or> ?qd=T", simp_all)
-  from trans [OF pq_ex cp_thm'[OF lq uq dd dp,where i="i"]] B
-  have "?lhs = (\<exists> j\<in> {1.. ?d}. ?I j ?mq \<or> (\<exists> b\<in> ?N ` set ?B. Ifm bbs ((b+ j)#bs) ?q))" by auto
-  also have "\<dots> = (\<exists> j\<in> {1.. ?d}. ?I j ?mq \<or> (\<exists> b\<in> set ?B. Ifm bbs ((?N b+ j)#bs) ?q))" by simp
-  also have "\<dots> = ((\<exists> j\<in> {1.. ?d}. ?I j ?mq ) \<or> (\<exists> j\<in> {1.. ?d}. \<exists> b\<in> set ?B. Ifm bbs ((?N (Add b (C j)))#bs) ?q))" by (simp only: Inum.simps) blast
-  also have "\<dots> = ((\<exists> j\<in> {1.. ?d}. ?I j ?smq ) \<or> (\<exists> j\<in> {1.. ?d}. \<exists> b\<in> set ?B. Ifm bbs ((?N (Add b (C j)))#bs) ?q))" by (simp add: simpfm) 
-  also have "\<dots> = ((\<exists> j\<in> set ?js. (\<lambda> j. ?I i (simpfm (subst0 (C j) ?smq))) j) \<or> (\<exists> j\<in> set ?js. \<exists> b\<in> set ?B. Ifm bbs ((?N (Add b (C j)))#bs) ?q))"
-    by (simp only: simpfm subst0_I[OF qfmq] iupt_set) auto
-  also have "\<dots> = (?I i (evaldjf (\<lambda> j. simpfm (subst0 (C j) ?smq)) ?js) \<or> (\<exists> j\<in> set ?js. \<exists> b\<in> set ?B. ?I i (subst0 (Add b (C j)) ?q)))" 
-   by (simp only: evaldjf_ex subst0_I[OF qfq])
- also have "\<dots>= (?I i ?md \<or> (\<exists> (b,j) \<in> set ?Bjs. (\<lambda> (b,j). ?I i (simpfm (subst0 (Add b (C j)) ?q))) (b,j)))"
-   by (simp only: simpfm set_concat set_map concat_map_singleton UN_simps) blast
- also have "\<dots> = (?I i ?md \<or> (?I i (evaldjf (\<lambda> (b,j). simpfm (subst0 (Add b (C j)) ?q)) ?Bjs)))"
-   by (simp only: evaldjf_ex[where bs="i#bs" and f="\<lambda> (b,j). simpfm (subst0 (Add b (C j)) ?q)" and ps="?Bjs"]) (auto simp add: split_def)
- finally have mdqd: "?lhs = (?I i ?md \<or> ?I i ?qd)" by simp  
-  also have "\<dots> = (?I i (disj ?md ?qd))" by (simp add: disj)
-  also have "\<dots> = (Ifm bbs bs (decr (disj ?md ?qd)))" by (simp only: decr [OF mdqdb]) 
-  finally have mdqd2: "?lhs = (Ifm bbs bs (decr (disj ?md ?qd)))" . 
-  {assume mdT: "?md = T"
-    hence cT:"cooper p = T" 
-      by (simp only: cooper_def unit_def split_def Let_def if_True) simp
-    from mdT have lhs:"?lhs" using mdqd by simp 
-    from mdT have "?rhs" by (simp add: cooper_def unit_def split_def)
-    with lhs cT have ?thesis by simp }
-  moreover
-  {assume mdT: "?md \<noteq> T" hence "cooper p = decr (disj ?md ?qd)" 
-      by (simp only: cooper_def unit_def split_def Let_def if_False) 
-    with mdqd2 decr_qf[OF mdqdb] have ?thesis by simp }
-  ultimately show ?thesis by blast
-qed
-
-definition pa :: "fm \<Rightarrow> fm" where
-  "pa p = qelim (prep p) cooper"
-
-theorem mirqe: "(Ifm bbs bs (pa p) = Ifm bbs bs p) \<and> qfree (pa p)"
-  using qelim_ci cooper prep by (auto simp add: pa_def)
-
-definition
-  cooper_test :: "unit \<Rightarrow> fm"
-where
-  "cooper_test u = pa (E (A (Imp (Ge (Sub (Bound 0) (Bound 1)))
-    (E (E (Eq (Sub (Add (Mul 3 (Bound 1)) (Mul 5 (Bound 0)))
-      (Bound 2))))))))"
-
-ML {* @{code cooper_test} () *}
-
-(*
-code_reserved SML oo
-export_code pa in SML module_name GeneratedCooper file "~~/src/HOL/Tools/Qelim/raw_generated_cooper.ML"
-*)
-
-oracle linzqe_oracle = {*
-let
-
-fun num_of_term vs (t as Free (xn, xT)) = (case AList.lookup (op =) vs t
-     of NONE => error "Variable not found in the list!"
-      | SOME n => @{code Bound} n)
-  | num_of_term vs @{term "0::int"} = @{code C} 0
-  | num_of_term vs @{term "1::int"} = @{code C} 1
-  | num_of_term vs (@{term "number_of :: int \<Rightarrow> int"} $ t) = @{code C} (HOLogic.dest_numeral t)
-  | num_of_term vs (Bound i) = @{code Bound} i
-  | num_of_term vs (@{term "uminus :: int \<Rightarrow> int"} $ t') = @{code Neg} (num_of_term vs t')
-  | num_of_term vs (@{term "op + :: int \<Rightarrow> int \<Rightarrow> int"} $ t1 $ t2) =
-      @{code Add} (num_of_term vs t1, num_of_term vs t2)
-  | num_of_term vs (@{term "op - :: int \<Rightarrow> int \<Rightarrow> int"} $ t1 $ t2) =
-      @{code Sub} (num_of_term vs t1, num_of_term vs t2)
-  | num_of_term vs (@{term "op * :: int \<Rightarrow> int \<Rightarrow> int"} $ t1 $ t2) =
-      (case try HOLogic.dest_number t1
-       of SOME (_, i) => @{code Mul} (i, num_of_term vs t2)
-        | NONE => (case try HOLogic.dest_number t2
-                of SOME (_, i) => @{code Mul} (i, num_of_term vs t1)
-                 | NONE => error "num_of_term: unsupported multiplication"))
-  | num_of_term vs t = error ("num_of_term: unknown term " ^ Syntax.string_of_term @{context} t);
-
-fun fm_of_term ps vs @{term True} = @{code T}
-  | fm_of_term ps vs @{term False} = @{code F}
-  | fm_of_term ps vs (@{term "op < :: int \<Rightarrow> int \<Rightarrow> bool"} $ t1 $ t2) =
-      @{code Lt} (@{code Sub} (num_of_term vs t1, num_of_term vs t2))
-  | fm_of_term ps vs (@{term "op \<le> :: int \<Rightarrow> int \<Rightarrow> bool"} $ t1 $ t2) =
-      @{code Le} (@{code Sub} (num_of_term vs t1, num_of_term vs t2))
-  | fm_of_term ps vs (@{term "op = :: int \<Rightarrow> int \<Rightarrow> bool"} $ t1 $ t2) =
-      @{code Eq} (@{code Sub} (num_of_term vs t1, num_of_term vs t2)) 
-  | fm_of_term ps vs (@{term "op dvd :: int \<Rightarrow> int \<Rightarrow> bool"} $ t1 $ t2) =
-      (case try HOLogic.dest_number t1
-       of SOME (_, i) => @{code Dvd} (i, num_of_term vs t2)
-        | NONE => error "num_of_term: unsupported dvd")
-  | fm_of_term ps vs (@{term "op = :: bool \<Rightarrow> bool \<Rightarrow> bool"} $ t1 $ t2) =
-      @{code Iff} (fm_of_term ps vs t1, fm_of_term ps vs t2)
-  | fm_of_term ps vs (@{term "op &"} $ t1 $ t2) =
-      @{code And} (fm_of_term ps vs t1, fm_of_term ps vs t2)
-  | fm_of_term ps vs (@{term "op |"} $ t1 $ t2) =
-      @{code Or} (fm_of_term ps vs t1, fm_of_term ps vs t2)
-  | fm_of_term ps vs (@{term "op -->"} $ t1 $ t2) =
-      @{code Imp} (fm_of_term ps vs t1, fm_of_term ps vs t2)
-  | fm_of_term ps vs (@{term "Not"} $ t') =
-      @{code NOT} (fm_of_term ps vs t')
-  | fm_of_term ps vs (Const ("Ex", _) $ Abs (xn, xT, p)) =
-      let
-        val (xn', p') = variant_abs (xn, xT, p);
-        val vs' = (Free (xn', xT), 0) :: map (fn (v, n) => (v, n + 1)) vs;
-      in @{code E} (fm_of_term ps vs' p) end
-  | fm_of_term ps vs (Const ("All", _) $ Abs (xn, xT, p)) =
-      let
-        val (xn', p') = variant_abs (xn, xT, p);
-        val vs' = (Free (xn', xT), 0) :: map (fn (v, n) => (v, n + 1)) vs;
-      in @{code A} (fm_of_term ps vs' p) end
-  | fm_of_term ps vs t = error ("fm_of_term : unknown term " ^ Syntax.string_of_term @{context} t);
-
-fun term_of_num vs (@{code C} i) = HOLogic.mk_number HOLogic.intT i
-  | term_of_num vs (@{code Bound} n) = fst (the (find_first (fn (_, m) => n = m) vs))
-  | term_of_num vs (@{code Neg} t') = @{term "uminus :: int \<Rightarrow> int"} $ term_of_num vs t'
-  | term_of_num vs (@{code Add} (t1, t2)) = @{term "op + :: int \<Rightarrow> int \<Rightarrow> int"} $
-      term_of_num vs t1 $ term_of_num vs t2
-  | term_of_num vs (@{code Sub} (t1, t2)) = @{term "op - :: int \<Rightarrow> int \<Rightarrow> int"} $
-      term_of_num vs t1 $ term_of_num vs t2
-  | term_of_num vs (@{code Mul} (i, t2)) = @{term "op * :: int \<Rightarrow> int \<Rightarrow> int"} $
-      term_of_num vs (@{code C} i) $ term_of_num vs t2
-  | term_of_num vs (@{code CN} (n, i, t)) = term_of_num vs (@{code Add} (@{code Mul} (i, @{code Bound} n), t));
-
-fun term_of_fm ps vs @{code T} = HOLogic.true_const 
-  | term_of_fm ps vs @{code F} = HOLogic.false_const
-  | term_of_fm ps vs (@{code Lt} t) =
-      @{term "op < :: int \<Rightarrow> int \<Rightarrow> bool"} $ term_of_num vs t $ @{term "0::int"}
-  | term_of_fm ps vs (@{code Le} t) =
-      @{term "op \<le> :: int \<Rightarrow> int \<Rightarrow> bool"} $ term_of_num vs t $ @{term "0::int"}
-  | term_of_fm ps vs (@{code Gt} t) =
-      @{term "op < :: int \<Rightarrow> int \<Rightarrow> bool"} $ @{term "0::int"} $ term_of_num vs t
-  | term_of_fm ps vs (@{code Ge} t) =
-      @{term "op \<le> :: int \<Rightarrow> int \<Rightarrow> bool"} $ @{term "0::int"} $ term_of_num vs t
-  | term_of_fm ps vs (@{code Eq} t) =
-      @{term "op = :: int \<Rightarrow> int \<Rightarrow> bool"} $ term_of_num vs t $ @{term "0::int"}
-  | term_of_fm ps vs (@{code NEq} t) =
-      term_of_fm ps vs (@{code NOT} (@{code Eq} t))
-  | term_of_fm ps vs (@{code Dvd} (i, t)) =
-      @{term "op dvd :: int \<Rightarrow> int \<Rightarrow> bool"} $ term_of_num vs (@{code C} i) $ term_of_num vs t
-  | term_of_fm ps vs (@{code NDvd} (i, t)) =
-      term_of_fm ps vs (@{code NOT} (@{code Dvd} (i, t)))
-  | term_of_fm ps vs (@{code NOT} t') =
-      HOLogic.Not $ term_of_fm ps vs t'
-  | term_of_fm ps vs (@{code And} (t1, t2)) =
-      HOLogic.conj $ term_of_fm ps vs t1 $ term_of_fm ps vs t2
-  | term_of_fm ps vs (@{code Or} (t1, t2)) =
-      HOLogic.disj $ term_of_fm ps vs t1 $ term_of_fm ps vs t2
-  | term_of_fm ps vs (@{code Imp} (t1, t2)) =
-      HOLogic.imp $ term_of_fm ps vs t1 $ term_of_fm ps vs t2
-  | term_of_fm ps vs (@{code Iff} (t1, t2)) =
-      @{term "op = :: bool \<Rightarrow> bool \<Rightarrow> bool"} $ term_of_fm ps vs t1 $ term_of_fm ps vs t2
-  | term_of_fm ps vs (@{code Closed} n) = (fst o the) (find_first (fn (_, m) => m = n) ps)
-  | term_of_fm ps vs (@{code NClosed} n) = term_of_fm ps vs (@{code NOT} (@{code Closed} n));
-
-fun term_bools acc t =
-  let
-    val is_op = member (op =) [@{term "op &"}, @{term "op |"}, @{term "op -->"}, @{term "op = :: bool => _"},
-      @{term "op = :: int => _"}, @{term "op < :: int => _"},
-      @{term "op <= :: int => _"}, @{term "Not"}, @{term "All :: (int => _) => _"},
-      @{term "Ex :: (int => _) => _"}, @{term "True"}, @{term "False"}]
-    fun is_ty t = not (fastype_of t = HOLogic.boolT) 
-  in case t
-   of (l as f $ a) $ b => if is_ty t orelse is_op t then term_bools (term_bools acc l)b 
-        else insert (op aconv) t acc
-    | f $ a => if is_ty t orelse is_op t then term_bools (term_bools acc f) a  
-        else insert (op aconv) t acc
-    | Abs p => term_bools acc (snd (variant_abs p))
-    | _ => if is_ty t orelse is_op t then acc else insert (op aconv) t acc
-  end;
-
-in fn ct =>
-  let
-    val thy = Thm.theory_of_cterm ct;
-    val t = Thm.term_of ct;
-    val fs = OldTerm.term_frees t;
-    val bs = term_bools [] t;
-    val vs = fs ~~ (0 upto (length fs - 1))
-    val ps = bs ~~ (0 upto (length bs - 1))
-    val t' = (term_of_fm ps vs o @{code pa} o fm_of_term ps vs) t;
-  in (Thm.cterm_of thy o HOLogic.mk_Trueprop o HOLogic.mk_eq) (t, t') end
-end;
-*}
-
-use "cooper_tac.ML"
-setup "Cooper_Tac.setup"
-
-text {* Tests *}
-
-lemma "\<exists> (j::int). \<forall> x\<ge>j. (\<exists> a b. x = 3*a+5*b)"
-  by cooper
-
-lemma "ALL (x::int) >=8. EX i j. 5*i + 3*j = x"
-  by cooper
-
-theorem "(\<forall>(y::int). 3 dvd y) ==> \<forall>(x::int). b < x --> a \<le> x"
-  by cooper
-
-theorem "!! (y::int) (z::int) (n::int). 3 dvd z ==> 2 dvd (y::int) ==>
-  (\<exists>(x::int).  2*x =  y) & (\<exists>(k::int). 3*k = z)"
-  by cooper
-
-theorem "!! (y::int) (z::int) n. Suc(n::nat) < 6 ==>  3 dvd z ==>
-  2 dvd (y::int) ==> (\<exists>(x::int).  2*x =  y) & (\<exists>(k::int). 3*k = z)"
-  by cooper
-
-theorem "\<forall>(x::nat). \<exists>(y::nat). (0::nat) \<le> 5 --> y = 5 + x "
-  by cooper
-
-lemma "ALL (x::int) >=8. EX i j. 5*i + 3*j = x"
-  by cooper 
-
-lemma "ALL (y::int) (z::int) (n::int). 3 dvd z --> 2 dvd (y::int) --> (EX (x::int).  2*x =  y) & (EX (k::int). 3*k = z)"
-  by cooper
-
-lemma "ALL(x::int) y. x < y --> 2 * x + 1 < 2 * y"
-  by cooper
-
-lemma "ALL(x::int) y. 2 * x + 1 ~= 2 * y"
-  by cooper
-
-lemma "EX(x::int) y. 0 < x  & 0 <= y  & 3 * x - 5 * y = 1"
-  by cooper
-
-lemma "~ (EX(x::int) (y::int) (z::int). 4*x + (-6::int)*y = 1)"
-  by cooper
-
-lemma "ALL(x::int). (2 dvd x) --> (EX(y::int). x = 2*y)"
-  by cooper
-
-lemma "ALL(x::int). (2 dvd x) = (EX(y::int). x = 2*y)"
-  by cooper
-
-lemma "ALL(x::int). ((2 dvd x) = (ALL(y::int). x ~= 2*y + 1))"
-  by cooper
-
-lemma "~ (ALL(x::int). ((2 dvd x) = (ALL(y::int). x ~= 2*y+1) | (EX(q::int) (u::int) i. 3*i + 2*q - u < 17) --> 0 < x | ((~ 3 dvd x) &(x + 8 = 0))))"
-  by cooper
-
-lemma "~ (ALL(i::int). 4 <= i --> (EX x y. 0 <= x & 0 <= y & 3 * x + 5 * y = i))" 
-  by cooper
-
-lemma "EX j. ALL (x::int) >= j. EX i j. 5*i + 3*j = x"
-  by cooper
-
-theorem "(\<forall>(y::int). 3 dvd y) ==> \<forall>(x::int). b < x --> a \<le> x"
-  by cooper
-
-theorem "!! (y::int) (z::int) (n::int). 3 dvd z ==> 2 dvd (y::int) ==>
-  (\<exists>(x::int).  2*x =  y) & (\<exists>(k::int). 3*k = z)"
-  by cooper
-
-theorem "!! (y::int) (z::int) n. Suc(n::nat) < 6 ==>  3 dvd z ==>
-  2 dvd (y::int) ==> (\<exists>(x::int).  2*x =  y) & (\<exists>(k::int). 3*k = z)"
-  by cooper
-
-theorem "\<forall>(x::nat). \<exists>(y::nat). (0::nat) \<le> 5 --> y = 5 + x "
-  by cooper
-
-theorem "\<forall>(x::nat). \<exists>(y::nat). y = 5 + x | x div 6 + 1= 2"
-  by cooper
-
-theorem "\<exists>(x::int). 0 < x"
-  by cooper
-
-theorem "\<forall>(x::int) y. x < y --> 2 * x + 1 < 2 * y"
-  by cooper
- 
-theorem "\<forall>(x::int) y. 2 * x + 1 \<noteq> 2 * y"
-  by cooper
- 
-theorem "\<exists>(x::int) y. 0 < x  & 0 \<le> y  & 3 * x - 5 * y = 1"
-  by cooper
-
-theorem "~ (\<exists>(x::int) (y::int) (z::int). 4*x + (-6::int)*y = 1)"
-  by cooper
-
-theorem "~ (\<exists>(x::int). False)"
-  by cooper
-
-theorem "\<forall>(x::int). (2 dvd x) --> (\<exists>(y::int). x = 2*y)"
-  by cooper 
-
-theorem "\<forall>(x::int). (2 dvd x) --> (\<exists>(y::int). x = 2*y)"
-  by cooper 
-
-theorem "\<forall>(x::int). (2 dvd x) = (\<exists>(y::int). x = 2*y)"
-  by cooper 
-
-theorem "\<forall>(x::int). ((2 dvd x) = (\<forall>(y::int). x \<noteq> 2*y + 1))"
-  by cooper 
-
-theorem "~ (\<forall>(x::int). 
-            ((2 dvd x) = (\<forall>(y::int). x \<noteq> 2*y+1) | 
-             (\<exists>(q::int) (u::int) i. 3*i + 2*q - u < 17)
-             --> 0 < x | ((~ 3 dvd x) &(x + 8 = 0))))"
-  by cooper
- 
-theorem "~ (\<forall>(i::int). 4 \<le> i --> (\<exists>x y. 0 \<le> x & 0 \<le> y & 3 * x + 5 * y = i))"
-  by cooper
-
-theorem "\<forall>(i::int). 8 \<le> i --> (\<exists>x y. 0 \<le> x & 0 \<le> y & 3 * x + 5 * y = i)"
-  by cooper
-
-theorem "\<exists>(j::int). \<forall>i. j \<le> i --> (\<exists>x y. 0 \<le> x & 0 \<le> y & 3 * x + 5 * y = i)"
-  by cooper
-
-theorem "~ (\<forall>j (i::int). j \<le> i --> (\<exists>x y. 0 \<le> x & 0 \<le> y & 3 * x + 5 * y = i))"
-  by cooper
-
-theorem "(\<exists>m::nat. n = 2 * m) --> (n + 1) div 2 = n div 2"
-  by cooper
-
-end
--- a/src/HOL/Reflection/Ferrack.thy	Mon Feb 16 10:13:30 2009 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,2101 +0,0 @@
-(*  Title:      HOL/Reflection/Ferrack.thy
-    Author:     Amine Chaieb
-*)
-
-theory Ferrack
-imports Complex_Main Dense_Linear_Order Efficient_Nat
-uses ("ferrack_tac.ML")
-begin
-
-section {* Quantifier elimination for @{text "\<real> (0, 1, +, <)"} *}
-
-  (*********************************************************************************)
-  (*          SOME GENERAL STUFF< HAS TO BE MOVED IN SOME LIB                      *)
-  (*********************************************************************************)
-
-consts alluopairs:: "'a list \<Rightarrow> ('a \<times> 'a) list"
-primrec
-  "alluopairs [] = []"
-  "alluopairs (x#xs) = (map (Pair x) (x#xs))@(alluopairs xs)"
-
-lemma alluopairs_set1: "set (alluopairs xs) \<le> {(x,y). x\<in> set xs \<and> y\<in> set xs}"
-by (induct xs, auto)
-
-lemma alluopairs_set:
-  "\<lbrakk>x\<in> set xs ; y \<in> set xs\<rbrakk> \<Longrightarrow> (x,y) \<in> set (alluopairs xs) \<or> (y,x) \<in> set (alluopairs xs) "
-by (induct xs, auto)
-
-lemma alluopairs_ex:
-  assumes Pc: "\<forall> x y. P x y = P y x"
-  shows "(\<exists> x \<in> set xs. \<exists> y \<in> set xs. P x y) = (\<exists> (x,y) \<in> set (alluopairs xs). P x y)"
-proof
-  assume "\<exists>x\<in>set xs. \<exists>y\<in>set xs. P x y"
-  then obtain x y where x: "x \<in> set xs" and y:"y \<in> set xs" and P: "P x y"  by blast
-  from alluopairs_set[OF x y] P Pc show"\<exists>(x, y)\<in>set (alluopairs xs). P x y" 
-    by auto
-next
-  assume "\<exists>(x, y)\<in>set (alluopairs xs). P x y"
-  then obtain "x" and "y"  where xy:"(x,y) \<in> set (alluopairs xs)" and P: "P x y" by blast+
-  from xy have "x \<in> set xs \<and> y\<in> set xs" using alluopairs_set1 by blast
-  with P show "\<exists>x\<in>set xs. \<exists>y\<in>set xs. P x y" by blast
-qed
-
-lemma nth_pos2: "0 < n \<Longrightarrow> (x#xs) ! n = xs ! (n - 1)"
-using Nat.gr0_conv_Suc
-by clarsimp
-
-lemma filter_length: "length (List.filter P xs) < Suc (length xs)"
-  apply (induct xs, auto) done
-
-consts remdps:: "'a list \<Rightarrow> 'a list"
-
-recdef remdps "measure size"
-  "remdps [] = []"
-  "remdps (x#xs) = (x#(remdps (List.filter (\<lambda> y. y \<noteq> x) xs)))"
-(hints simp add: filter_length[rule_format])
-
-lemma remdps_set[simp]: "set (remdps xs) = set xs"
-  by (induct xs rule: remdps.induct, auto)
-
-
-
-  (*********************************************************************************)
-  (****                            SHADOW SYNTAX AND SEMANTICS                  ****)
-  (*********************************************************************************)
-
-datatype num = C int | Bound nat | CN nat int num | Neg num | Add num num| Sub num num 
-  | Mul int num 
-
-  (* A size for num to make inductive proofs simpler*)
-consts num_size :: "num \<Rightarrow> nat" 
-primrec 
-  "num_size (C c) = 1"
-  "num_size (Bound n) = 1"
-  "num_size (Neg a) = 1 + num_size a"
-  "num_size (Add a b) = 1 + num_size a + num_size b"
-  "num_size (Sub a b) = 3 + num_size a + num_size b"
-  "num_size (Mul c a) = 1 + num_size a"
-  "num_size (CN n c a) = 3 + num_size a "
-
-  (* Semantics of numeral terms (num) *)
-consts Inum :: "real list \<Rightarrow> num \<Rightarrow> real"
-primrec
-  "Inum bs (C c) = (real c)"
-  "Inum bs (Bound n) = bs!n"
-  "Inum bs (CN n c a) = (real c) * (bs!n) + (Inum bs a)"
-  "Inum bs (Neg a) = -(Inum bs a)"
-  "Inum bs (Add a b) = Inum bs a + Inum bs b"
-  "Inum bs (Sub a b) = Inum bs a - Inum bs b"
-  "Inum bs (Mul c a) = (real c) * Inum bs a"
-    (* FORMULAE *)
-datatype fm  = 
-  T| F| Lt num| Le num| Gt num| Ge num| Eq num| NEq num|
-  NOT fm| And fm fm|  Or fm fm| Imp fm fm| Iff fm fm| E fm| A fm
-
-
-  (* A size for fm *)
-consts fmsize :: "fm \<Rightarrow> nat"
-recdef fmsize "measure size"
-  "fmsize (NOT p) = 1 + fmsize p"
-  "fmsize (And p q) = 1 + fmsize p + fmsize q"
-  "fmsize (Or p q) = 1 + fmsize p + fmsize q"
-  "fmsize (Imp p q) = 3 + fmsize p + fmsize q"
-  "fmsize (Iff p q) = 3 + 2*(fmsize p + fmsize q)"
-  "fmsize (E p) = 1 + fmsize p"
-  "fmsize (A p) = 4+ fmsize p"
-  "fmsize p = 1"
-  (* several lemmas about fmsize *)
-lemma fmsize_pos: "fmsize p > 0"
-by (induct p rule: fmsize.induct) simp_all
-
-  (* Semantics of formulae (fm) *)
-consts Ifm ::"real list \<Rightarrow> fm \<Rightarrow> bool"
-primrec
-  "Ifm bs T = True"
-  "Ifm bs F = False"
-  "Ifm bs (Lt a) = (Inum bs a < 0)"
-  "Ifm bs (Gt a) = (Inum bs a > 0)"
-  "Ifm bs (Le a) = (Inum bs a \<le> 0)"
-  "Ifm bs (Ge a) = (Inum bs a \<ge> 0)"
-  "Ifm bs (Eq a) = (Inum bs a = 0)"
-  "Ifm bs (NEq a) = (Inum bs a \<noteq> 0)"
-  "Ifm bs (NOT p) = (\<not> (Ifm bs p))"
-  "Ifm bs (And p q) = (Ifm bs p \<and> Ifm bs q)"
-  "Ifm bs (Or p q) = (Ifm bs p \<or> Ifm bs q)"
-  "Ifm bs (Imp p q) = ((Ifm bs p) \<longrightarrow> (Ifm bs q))"
-  "Ifm bs (Iff p q) = (Ifm bs p = Ifm bs q)"
-  "Ifm bs (E p) = (\<exists> x. Ifm (x#bs) p)"
-  "Ifm bs (A p) = (\<forall> x. Ifm (x#bs) p)"
-
-lemma IfmLeSub: "\<lbrakk> Inum bs s = s' ; Inum bs t = t' \<rbrakk> \<Longrightarrow> Ifm bs (Le (Sub s t)) = (s' \<le> t')"
-apply simp
-done
-
-lemma IfmLtSub: "\<lbrakk> Inum bs s = s' ; Inum bs t = t' \<rbrakk> \<Longrightarrow> Ifm bs (Lt (Sub s t)) = (s' < t')"
-apply simp
-done
-lemma IfmEqSub: "\<lbrakk> Inum bs s = s' ; Inum bs t = t' \<rbrakk> \<Longrightarrow> Ifm bs (Eq (Sub s t)) = (s' = t')"
-apply simp
-done
-lemma IfmNOT: " (Ifm bs p = P) \<Longrightarrow> (Ifm bs (NOT p) = (\<not>P))"
-apply simp
-done
-lemma IfmAnd: " \<lbrakk> Ifm bs p = P ; Ifm bs q = Q\<rbrakk> \<Longrightarrow> (Ifm bs (And p q) = (P \<and> Q))"
-apply simp
-done
-lemma IfmOr: " \<lbrakk> Ifm bs p = P ; Ifm bs q = Q\<rbrakk> \<Longrightarrow> (Ifm bs (Or p q) = (P \<or> Q))"
-apply simp
-done
-lemma IfmImp: " \<lbrakk> Ifm bs p = P ; Ifm bs q = Q\<rbrakk> \<Longrightarrow> (Ifm bs (Imp p q) = (P \<longrightarrow> Q))"
-apply simp
-done
-lemma IfmIff: " \<lbrakk> Ifm bs p = P ; Ifm bs q = Q\<rbrakk> \<Longrightarrow> (Ifm bs (Iff p q) = (P = Q))"
-apply simp
-done
-
-lemma IfmE: " (!! x. Ifm (x#bs) p = P x) \<Longrightarrow> (Ifm bs (E p) = (\<exists>x. P x))"
-apply simp
-done
-lemma IfmA: " (!! x. Ifm (x#bs) p = P x) \<Longrightarrow> (Ifm bs (A p) = (\<forall>x. P x))"
-apply simp
-done
-
-consts not:: "fm \<Rightarrow> fm"
-recdef not "measure size"
-  "not (NOT p) = p"
-  "not T = F"
-  "not F = T"
-  "not p = NOT p"
-lemma not[simp]: "Ifm bs (not p) = Ifm bs (NOT p)"
-by (cases p) auto
-
-constdefs conj :: "fm \<Rightarrow> fm \<Rightarrow> fm"
-  "conj p q \<equiv> (if (p = F \<or> q=F) then F else if p=T then q else if q=T then p else 
-   if p = q then p else And p q)"
-lemma conj[simp]: "Ifm bs (conj p q) = Ifm bs (And p q)"
-by (cases "p=F \<or> q=F",simp_all add: conj_def) (cases p,simp_all)
-
-constdefs disj :: "fm \<Rightarrow> fm \<Rightarrow> fm"
-  "disj p q \<equiv> (if (p = T \<or> q=T) then T else if p=F then q else if q=F then p 
-       else if p=q then p else Or p q)"
-
-lemma disj[simp]: "Ifm bs (disj p q) = Ifm bs (Or p q)"
-by (cases "p=T \<or> q=T",simp_all add: disj_def) (cases p,simp_all)
-
-constdefs  imp :: "fm \<Rightarrow> fm \<Rightarrow> fm"
-  "imp p q \<equiv> (if (p = F \<or> q=T \<or> p=q) then T else if p=T then q else if q=F then not p 
-    else Imp p q)"
-lemma imp[simp]: "Ifm bs (imp p q) = Ifm bs (Imp p q)"
-by (cases "p=F \<or> q=T",simp_all add: imp_def) 
-
-constdefs   iff :: "fm \<Rightarrow> fm \<Rightarrow> fm"
-  "iff p q \<equiv> (if (p = q) then T else if (p = NOT q \<or> NOT p = q) then F else 
-       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 
-  Iff p q)"
-lemma iff[simp]: "Ifm bs (iff p q) = Ifm bs (Iff p q)"
-  by (unfold iff_def,cases "p=q", simp,cases "p=NOT q", simp) (cases "NOT p= q", auto)
-
-lemma conj_simps:
-  "conj F Q = F"
-  "conj P F = F"
-  "conj T Q = Q"
-  "conj P T = P"
-  "conj P P = P"
-  "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"
-  by (simp_all add: conj_def)
-
-lemma disj_simps:
-  "disj T Q = T"
-  "disj P T = T"
-  "disj F Q = Q"
-  "disj P F = P"
-  "disj P P = P"
-  "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"
-  by (simp_all add: disj_def)
-lemma imp_simps:
-  "imp F Q = T"
-  "imp P T = T"
-  "imp T Q = Q"
-  "imp P F = not P"
-  "imp P P = T"
-  "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"
-  by (simp_all add: imp_def)
-lemma trivNOT: "p \<noteq> NOT p" "NOT p \<noteq> p"
-apply (induct p, auto)
-done
-
-lemma iff_simps:
-  "iff p p = T"
-  "iff p (NOT p) = F"
-  "iff (NOT p) p = F"
-  "iff p F = not p"
-  "iff F p = not p"
-  "p \<noteq> NOT T \<Longrightarrow> iff T p = p"
-  "p\<noteq> NOT T \<Longrightarrow> iff p T = p"
-  "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"
-  using trivNOT
-  by (simp_all add: iff_def, cases p, auto)
-  (* Quantifier freeness *)
-consts qfree:: "fm \<Rightarrow> bool"
-recdef qfree "measure size"
-  "qfree (E p) = False"
-  "qfree (A p) = False"
-  "qfree (NOT p) = qfree p" 
-  "qfree (And p q) = (qfree p \<and> qfree q)" 
-  "qfree (Or  p q) = (qfree p \<and> qfree q)" 
-  "qfree (Imp p q) = (qfree p \<and> qfree q)" 
-  "qfree (Iff p q) = (qfree p \<and> qfree q)"
-  "qfree p = True"
-
-  (* Boundedness and substitution *)
-consts 
-  numbound0:: "num \<Rightarrow> bool" (* a num is INDEPENDENT of Bound 0 *)
-  bound0:: "fm \<Rightarrow> bool" (* A Formula is independent of Bound 0 *)
-primrec
-  "numbound0 (C c) = True"
-  "numbound0 (Bound n) = (n>0)"
-  "numbound0 (CN n c a) = (n\<noteq>0 \<and> numbound0 a)"
-  "numbound0 (Neg a) = numbound0 a"
-  "numbound0 (Add a b) = (numbound0 a \<and> numbound0 b)"
-  "numbound0 (Sub a b) = (numbound0 a \<and> numbound0 b)" 
-  "numbound0 (Mul i a) = numbound0 a"
-lemma numbound0_I:
-  assumes nb: "numbound0 a"
-  shows "Inum (b#bs) a = Inum (b'#bs) a"
-using nb
-by (induct a rule: numbound0.induct,auto simp add: nth_pos2)
-
-primrec
-  "bound0 T = True"
-  "bound0 F = True"
-  "bound0 (Lt a) = numbound0 a"
-  "bound0 (Le a) = numbound0 a"
-  "bound0 (Gt a) = numbound0 a"
-  "bound0 (Ge a) = numbound0 a"
-  "bound0 (Eq a) = numbound0 a"
-  "bound0 (NEq a) = numbound0 a"
-  "bound0 (NOT p) = bound0 p"
-  "bound0 (And p q) = (bound0 p \<and> bound0 q)"
-  "bound0 (Or p q) = (bound0 p \<and> bound0 q)"
-  "bound0 (Imp p q) = ((bound0 p) \<and> (bound0 q))"
-  "bound0 (Iff p q) = (bound0 p \<and> bound0 q)"
-  "bound0 (E p) = False"
-  "bound0 (A p) = False"
-
-lemma bound0_I:
-  assumes bp: "bound0 p"
-  shows "Ifm (b#bs) p = Ifm (b'#bs) p"
-using bp numbound0_I[where b="b" and bs="bs" and b'="b'"]
-by (induct p rule: bound0.induct) (auto simp add: nth_pos2)
-
-lemma not_qf[simp]: "qfree p \<Longrightarrow> qfree (not p)"
-by (cases p, auto)
-lemma not_bn[simp]: "bound0 p \<Longrightarrow> bound0 (not p)"
-by (cases p, auto)
-
-
-lemma conj_qf[simp]: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (conj p q)"
-using conj_def by auto 
-lemma conj_nb[simp]: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (conj p q)"
-using conj_def by auto 
-
-lemma disj_qf[simp]: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (disj p q)"
-using disj_def by auto 
-lemma disj_nb[simp]: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (disj p q)"
-using disj_def by auto 
-
-lemma imp_qf[simp]: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (imp p q)"
-using imp_def by (cases "p=F \<or> q=T",simp_all add: imp_def)
-lemma imp_nb[simp]: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (imp p q)"
-using imp_def by (cases "p=F \<or> q=T \<or> p=q",simp_all add: imp_def)
-
-lemma iff_qf[simp]: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (iff p q)"
-  by (unfold iff_def,cases "p=q", auto)
-lemma iff_nb[simp]: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (iff p q)"
-using iff_def by (unfold iff_def,cases "p=q", auto)
-
-consts 
-  decrnum:: "num \<Rightarrow> num" 
-  decr :: "fm \<Rightarrow> fm"
-
-recdef decrnum "measure size"
-  "decrnum (Bound n) = Bound (n - 1)"
-  "decrnum (Neg a) = Neg (decrnum a)"
-  "decrnum (Add a b) = Add (decrnum a) (decrnum b)"
-  "decrnum (Sub a b) = Sub (decrnum a) (decrnum b)"
-  "decrnum (Mul c a) = Mul c (decrnum a)"
-  "decrnum (CN n c a) = CN (n - 1) c (decrnum a)"
-  "decrnum a = a"
-
-recdef decr "measure size"
-  "decr (Lt a) = Lt (decrnum a)"
-  "decr (Le a) = Le (decrnum a)"
-  "decr (Gt a) = Gt (decrnum a)"
-  "decr (Ge a) = Ge (decrnum a)"
-  "decr (Eq a) = Eq (decrnum a)"
-  "decr (NEq a) = NEq (decrnum a)"
-  "decr (NOT p) = NOT (decr p)" 
-  "decr (And p q) = conj (decr p) (decr q)"
-  "decr (Or p q) = disj (decr p) (decr q)"
-  "decr (Imp p q) = imp (decr p) (decr q)"
-  "decr (Iff p q) = iff (decr p) (decr q)"
-  "decr p = p"
-
-lemma decrnum: assumes nb: "numbound0 t"
-  shows "Inum (x#bs) t = Inum bs (decrnum t)"
-  using nb by (induct t rule: decrnum.induct, simp_all add: nth_pos2)
-
-lemma decr: assumes nb: "bound0 p"
-  shows "Ifm (x#bs) p = Ifm bs (decr p)"
-  using nb 
-  by (induct p rule: decr.induct, simp_all add: nth_pos2 decrnum)
-
-lemma decr_qf: "bound0 p \<Longrightarrow> qfree (decr p)"
-by (induct p, simp_all)
-
-consts 
-  isatom :: "fm \<Rightarrow> bool" (* test for atomicity *)
-recdef isatom "measure size"
-  "isatom T = True"
-  "isatom F = True"
-  "isatom (Lt a) = True"
-  "isatom (Le a) = True"
-  "isatom (Gt a) = True"
-  "isatom (Ge a) = True"
-  "isatom (Eq a) = True"
-  "isatom (NEq a) = True"
-  "isatom p = False"
-
-lemma bound0_qf: "bound0 p \<Longrightarrow> qfree p"
-by (induct p, simp_all)
-
-constdefs djf:: "('a \<Rightarrow> fm) \<Rightarrow> 'a \<Rightarrow> fm \<Rightarrow> fm"
-  "djf f p q \<equiv> (if q=T then T else if q=F then f p else 
-  (let fp = f p in case fp of T \<Rightarrow> T | F \<Rightarrow> q | _ \<Rightarrow> Or (f p) q))"
-constdefs evaldjf:: "('a \<Rightarrow> fm) \<Rightarrow> 'a list \<Rightarrow> fm"
-  "evaldjf f ps \<equiv> foldr (djf f) ps F"
-
-lemma djf_Or: "Ifm bs (djf f p q) = Ifm bs (Or (f p) q)"
-by (cases "q=T", simp add: djf_def,cases "q=F",simp add: djf_def) 
-(cases "f p", simp_all add: Let_def djf_def) 
-
-
-lemma djf_simps:
-  "djf f p T = T"
-  "djf f p F = f p"
-  "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)"
-  by (simp_all add: djf_def)
-
-lemma evaldjf_ex: "Ifm bs (evaldjf f ps) = (\<exists> p \<in> set ps. Ifm bs (f p))"
-  by(induct ps, simp_all add: evaldjf_def djf_Or)
-
-lemma evaldjf_bound0: 
-  assumes nb: "\<forall> x\<in> set xs. bound0 (f x)"
-  shows "bound0 (evaldjf f xs)"
-  using nb by (induct xs, auto simp add: evaldjf_def djf_def Let_def) (case_tac "f a", auto) 
-
-lemma evaldjf_qf: 
-  assumes nb: "\<forall> x\<in> set xs. qfree (f x)"
-  shows "qfree (evaldjf f xs)"
-  using nb by (induct xs, auto simp add: evaldjf_def djf_def Let_def) (case_tac "f a", auto) 
-
-consts disjuncts :: "fm \<Rightarrow> fm list"
-recdef disjuncts "measure size"
-  "disjuncts (Or p q) = (disjuncts p) @ (disjuncts q)"
-  "disjuncts F = []"
-  "disjuncts p = [p]"
-
-lemma disjuncts: "(\<exists> q\<in> set (disjuncts p). Ifm bs q) = Ifm bs p"
-by(induct p rule: disjuncts.induct, auto)
-
-lemma disjuncts_nb: "bound0 p \<Longrightarrow> \<forall> q\<in> set (disjuncts p). bound0 q"
-proof-
-  assume nb: "bound0 p"
-  hence "list_all bound0 (disjuncts p)" by (induct p rule:disjuncts.induct,auto)
-  thus ?thesis by (simp only: list_all_iff)
-qed
-
-lemma disjuncts_qf: "qfree p \<Longrightarrow> \<forall> q\<in> set (disjuncts p). qfree q"
-proof-
-  assume qf: "qfree p"
-  hence "list_all qfree (disjuncts p)"
-    by (induct p rule: disjuncts.induct, auto)
-  thus ?thesis by (simp only: list_all_iff)
-qed
-
-constdefs DJ :: "(fm \<Rightarrow> fm) \<Rightarrow> fm \<Rightarrow> fm"
-  "DJ f p \<equiv> evaldjf f (disjuncts p)"
-
-lemma DJ: assumes fdj: "\<forall> p q. Ifm bs (f (Or p q)) = Ifm bs (Or (f p) (f q))"
-  and fF: "f F = F"
-  shows "Ifm bs (DJ f p) = Ifm bs (f p)"
-proof-
-  have "Ifm bs (DJ f p) = (\<exists> q \<in> set (disjuncts p). Ifm bs (f q))"
-    by (simp add: DJ_def evaldjf_ex) 
-  also have "\<dots> = Ifm bs (f p)" using fdj fF by (induct p rule: disjuncts.induct, auto)
-  finally show ?thesis .
-qed
-
-lemma DJ_qf: assumes 
-  fqf: "\<forall> p. qfree p \<longrightarrow> qfree (f p)"
-  shows "\<forall>p. qfree p \<longrightarrow> qfree (DJ f p) "
-proof(clarify)
-  fix  p assume qf: "qfree p"
-  have th: "DJ f p = evaldjf f (disjuncts p)" by (simp add: DJ_def)
-  from disjuncts_qf[OF qf] have "\<forall> q\<in> set (disjuncts p). qfree q" .
-  with fqf have th':"\<forall> q\<in> set (disjuncts p). qfree (f q)" by blast
-  
-  from evaldjf_qf[OF th'] th show "qfree (DJ f p)" by simp
-qed
-
-lemma DJ_qe: assumes qe: "\<forall> bs p. qfree p \<longrightarrow> qfree (qe p) \<and> (Ifm bs (qe p) = Ifm bs (E p))"
-  shows "\<forall> bs p. qfree p \<longrightarrow> qfree (DJ qe p) \<and> (Ifm bs ((DJ qe p)) = Ifm bs (E p))"
-proof(clarify)
-  fix p::fm and bs
-  assume qf: "qfree p"
-  from qe have qth: "\<forall> p. qfree p \<longrightarrow> qfree (qe p)" by blast
-  from DJ_qf[OF qth] qf have qfth:"qfree (DJ qe p)" by auto
-  have "Ifm bs (DJ qe p) = (\<exists> q\<in> set (disjuncts p). Ifm bs (qe q))"
-    by (simp add: DJ_def evaldjf_ex)
-  also have "\<dots> = (\<exists> q \<in> set(disjuncts p). Ifm bs (E q))" using qe disjuncts_qf[OF qf] by auto
-  also have "\<dots> = Ifm bs (E p)" by (induct p rule: disjuncts.induct, auto)
-  finally show "qfree (DJ qe p) \<and> Ifm bs (DJ qe p) = Ifm bs (E p)" using qfth by blast
-qed
-  (* Simplification *)
-consts 
-  numgcd :: "num \<Rightarrow> int"
-  numgcdh:: "num \<Rightarrow> int \<Rightarrow> int"
-  reducecoeffh:: "num \<Rightarrow> int \<Rightarrow> num"
-  reducecoeff :: "num \<Rightarrow> num"
-  dvdnumcoeff:: "num \<Rightarrow> int \<Rightarrow> bool"
-consts maxcoeff:: "num \<Rightarrow> int"
-recdef maxcoeff "measure size"
-  "maxcoeff (C i) = abs i"
-  "maxcoeff (CN n c t) = max (abs c) (maxcoeff t)"
-  "maxcoeff t = 1"
-
-lemma maxcoeff_pos: "maxcoeff t \<ge> 0"
-  by (induct t rule: maxcoeff.induct, auto)
-
-recdef numgcdh "measure size"
-  "numgcdh (C i) = (\<lambda>g. zgcd i g)"
-  "numgcdh (CN n c t) = (\<lambda>g. zgcd c (numgcdh t g))"
-  "numgcdh t = (\<lambda>g. 1)"
-defs numgcd_def [code]: "numgcd t \<equiv> numgcdh t (maxcoeff t)"
-
-recdef reducecoeffh "measure size"
-  "reducecoeffh (C i) = (\<lambda> g. C (i div g))"
-  "reducecoeffh (CN n c t) = (\<lambda> g. CN n (c div g) (reducecoeffh t g))"
-  "reducecoeffh t = (\<lambda>g. t)"
-
-defs reducecoeff_def: "reducecoeff t \<equiv> 
-  (let g = numgcd t in 
-  if g = 0 then C 0 else if g=1 then t else reducecoeffh t g)"
-
-recdef dvdnumcoeff "measure size"
-  "dvdnumcoeff (C i) = (\<lambda> g. g dvd i)"
-  "dvdnumcoeff (CN n c t) = (\<lambda> g. g dvd c \<and> (dvdnumcoeff t g))"
-  "dvdnumcoeff t = (\<lambda>g. False)"
-
-lemma dvdnumcoeff_trans: 
-  assumes gdg: "g dvd g'" and dgt':"dvdnumcoeff t g'"
-  shows "dvdnumcoeff t g"
-  using dgt' gdg 
-  by (induct t rule: dvdnumcoeff.induct, simp_all add: gdg zdvd_trans[OF gdg])
-
-declare zdvd_trans [trans add]
-
-lemma natabs0: "(nat (abs x) = 0) = (x = 0)"
-by arith
-
-lemma numgcd0:
-  assumes g0: "numgcd t = 0"
-  shows "Inum bs t = 0"
-  using g0[simplified numgcd_def] 
-  by (induct t rule: numgcdh.induct, auto simp add: zgcd_def gcd_zero natabs0 max_def maxcoeff_pos)
-
-lemma numgcdh_pos: assumes gp: "g \<ge> 0" shows "numgcdh t g \<ge> 0"
-  using gp
-  by (induct t rule: numgcdh.induct, auto simp add: zgcd_def)
-
-lemma numgcd_pos: "numgcd t \<ge>0"
-  by (simp add: numgcd_def numgcdh_pos maxcoeff_pos)
-
-lemma reducecoeffh:
-  assumes gt: "dvdnumcoeff t g" and gp: "g > 0" 
-  shows "real g *(Inum bs (reducecoeffh t g)) = Inum bs t"
-  using gt
-proof(induct t rule: reducecoeffh.induct) 
-  case (1 i) hence gd: "g dvd i" by simp
-  from gp have gnz: "g \<noteq> 0" by simp
-  from prems show ?case by (simp add: real_of_int_div[OF gnz gd])
-next
-  case (2 n c t)  hence gd: "g dvd c" by simp
-  from gp have gnz: "g \<noteq> 0" by simp
-  from prems show ?case by (simp add: real_of_int_div[OF gnz gd] algebra_simps)
-qed (auto simp add: numgcd_def gp)
-consts ismaxcoeff:: "num \<Rightarrow> int \<Rightarrow> bool"
-recdef ismaxcoeff "measure size"
-  "ismaxcoeff (C i) = (\<lambda> x. abs i \<le> x)"
-  "ismaxcoeff (CN n c t) = (\<lambda>x. abs c \<le> x \<and> (ismaxcoeff t x))"
-  "ismaxcoeff t = (\<lambda>x. True)"
-
-lemma ismaxcoeff_mono: "ismaxcoeff t c \<Longrightarrow> c \<le> c' \<Longrightarrow> ismaxcoeff t c'"
-by (induct t rule: ismaxcoeff.induct, auto)
-
-lemma maxcoeff_ismaxcoeff: "ismaxcoeff t (maxcoeff t)"
-proof (induct t rule: maxcoeff.induct)
-  case (2 n c t)
-  hence H:"ismaxcoeff t (maxcoeff t)" .
-  have thh: "maxcoeff t \<le> max (abs c) (maxcoeff t)" by (simp add: le_maxI2)
-  from ismaxcoeff_mono[OF H thh] show ?case by (simp add: le_maxI1)
-qed simp_all
-
-lemma zgcd_gt1: "zgcd i j > 1 \<Longrightarrow> ((abs i > 1 \<and> abs j > 1) \<or> (abs i = 0 \<and> abs j > 1) \<or> (abs i > 1 \<and> abs j = 0))"
-  apply (cases "abs i = 0", simp_all add: zgcd_def)
-  apply (cases "abs j = 0", simp_all)
-  apply (cases "abs i = 1", simp_all)
-  apply (cases "abs j = 1", simp_all)
-  apply auto
-  done
-lemma numgcdh0:"numgcdh t m = 0 \<Longrightarrow>  m =0"
-  by (induct t rule: numgcdh.induct, auto simp add:zgcd0)
-
-lemma dvdnumcoeff_aux:
-  assumes "ismaxcoeff t m" and mp:"m \<ge> 0" and "numgcdh t m > 1"
-  shows "dvdnumcoeff t (numgcdh t m)"
-using prems
-proof(induct t rule: numgcdh.induct)
-  case (2 n c t) 
-  let ?g = "numgcdh t m"
-  from prems have th:"zgcd c ?g > 1" by simp
-  from zgcd_gt1[OF th] numgcdh_pos[OF mp, where t="t"]
-  have "(abs c > 1 \<and> ?g > 1) \<or> (abs c = 0 \<and> ?g > 1) \<or> (abs c > 1 \<and> ?g = 0)" by simp
-  moreover {assume "abs c > 1" and gp: "?g > 1" with prems
-    have th: "dvdnumcoeff t ?g" by simp
-    have th': "zgcd c ?g dvd ?g" by (simp add:zgcd_zdvd2)
-    from dvdnumcoeff_trans[OF th' th] have ?case by (simp add: zgcd_zdvd1)}
-  moreover {assume "abs c = 0 \<and> ?g > 1"
-    with prems have th: "dvdnumcoeff t ?g" by simp
-    have th': "zgcd c ?g dvd ?g" by (simp add:zgcd_zdvd2)
-    from dvdnumcoeff_trans[OF th' th] have ?case by (simp add: zgcd_zdvd1)
-    hence ?case by simp }
-  moreover {assume "abs c > 1" and g0:"?g = 0" 
-    from numgcdh0[OF g0] have "m=0". with prems   have ?case by simp }
-  ultimately show ?case by blast
-qed(auto simp add: zgcd_zdvd1)
-
-lemma dvdnumcoeff_aux2:
-  assumes "numgcd t > 1" shows "dvdnumcoeff t (numgcd t) \<and> numgcd t > 0"
-  using prems 
-proof (simp add: numgcd_def)
-  let ?mc = "maxcoeff t"
-  let ?g = "numgcdh t ?mc"
-  have th1: "ismaxcoeff t ?mc" by (rule maxcoeff_ismaxcoeff)
-  have th2: "?mc \<ge> 0" by (rule maxcoeff_pos)
-  assume H: "numgcdh t ?mc > 1"
-  from dvdnumcoeff_aux[OF th1 th2 H]  show "dvdnumcoeff t ?g" .
-qed
-
-lemma reducecoeff: "real (numgcd t) * (Inum bs (reducecoeff t)) = Inum bs t"
-proof-
-  let ?g = "numgcd t"
-  have "?g \<ge> 0"  by (simp add: numgcd_pos)
-  hence	"?g = 0 \<or> ?g = 1 \<or> ?g > 1" by auto
-  moreover {assume "?g = 0" hence ?thesis by (simp add: numgcd0)} 
-  moreover {assume "?g = 1" hence ?thesis by (simp add: reducecoeff_def)} 
-  moreover { assume g1:"?g > 1"
-    from dvdnumcoeff_aux2[OF g1] have th1:"dvdnumcoeff t ?g" and g0: "?g > 0" by blast+
-    from reducecoeffh[OF th1 g0, where bs="bs"] g1 have ?thesis 
-      by (simp add: reducecoeff_def Let_def)} 
-  ultimately show ?thesis by blast
-qed
-
-lemma reducecoeffh_numbound0: "numbound0 t \<Longrightarrow> numbound0 (reducecoeffh t g)"
-by (induct t rule: reducecoeffh.induct, auto)
-
-lemma reducecoeff_numbound0: "numbound0 t \<Longrightarrow> numbound0 (reducecoeff t)"
-using reducecoeffh_numbound0 by (simp add: reducecoeff_def Let_def)
-
-consts
-  simpnum:: "num \<Rightarrow> num"
-  numadd:: "num \<times> num \<Rightarrow> num"
-  nummul:: "num \<Rightarrow> int \<Rightarrow> num"
-recdef numadd "measure (\<lambda> (t,s). size t + size s)"
-  "numadd (CN n1 c1 r1,CN n2 c2 r2) =
-  (if n1=n2 then 
-  (let c = c1 + c2
-  in (if c=0 then numadd(r1,r2) else CN n1 c (numadd (r1,r2))))
-  else if n1 \<le> n2 then (CN n1 c1 (numadd (r1,CN n2 c2 r2))) 
-  else (CN n2 c2 (numadd (CN n1 c1 r1,r2))))"
-  "numadd (CN n1 c1 r1,t) = CN n1 c1 (numadd (r1, t))"  
-  "numadd (t,CN n2 c2 r2) = CN n2 c2 (numadd (t,r2))" 
-  "numadd (C b1, C b2) = C (b1+b2)"
-  "numadd (a,b) = Add a b"
-
-lemma numadd[simp]: "Inum bs (numadd (t,s)) = Inum bs (Add t s)"
-apply (induct t s rule: numadd.induct, simp_all add: Let_def)
-apply (case_tac "c1+c2 = 0",case_tac "n1 \<le> n2", simp_all)
-apply (case_tac "n1 = n2", simp_all add: algebra_simps)
-by (simp only: left_distrib[symmetric],simp)
-
-lemma numadd_nb[simp]: "\<lbrakk> numbound0 t ; numbound0 s\<rbrakk> \<Longrightarrow> numbound0 (numadd (t,s))"
-by (induct t s rule: numadd.induct, auto simp add: Let_def)
-
-recdef nummul "measure size"
-  "nummul (C j) = (\<lambda> i. C (i*j))"
-  "nummul (CN n c a) = (\<lambda> i. CN n (i*c) (nummul a i))"
-  "nummul t = (\<lambda> i. Mul i t)"
-
-lemma nummul[simp]: "\<And> i. Inum bs (nummul t i) = Inum bs (Mul i t)"
-by (induct t rule: nummul.induct, auto simp add: algebra_simps)
-
-lemma nummul_nb[simp]: "\<And> i. numbound0 t \<Longrightarrow> numbound0 (nummul t i)"
-by (induct t rule: nummul.induct, auto )
-
-constdefs numneg :: "num \<Rightarrow> num"
-  "numneg t \<equiv> nummul t (- 1)"
-
-constdefs numsub :: "num \<Rightarrow> num \<Rightarrow> num"
-  "numsub s t \<equiv> (if s = t then C 0 else numadd (s,numneg t))"
-
-lemma numneg[simp]: "Inum bs (numneg t) = Inum bs (Neg t)"
-using numneg_def by simp
-
-lemma numneg_nb[simp]: "numbound0 t \<Longrightarrow> numbound0 (numneg t)"
-using numneg_def by simp
-
-lemma numsub[simp]: "Inum bs (numsub a b) = Inum bs (Sub a b)"
-using numsub_def by simp
-
-lemma numsub_nb[simp]: "\<lbrakk> numbound0 t ; numbound0 s\<rbrakk> \<Longrightarrow> numbound0 (numsub t s)"
-using numsub_def by simp
-
-recdef simpnum "measure size"
-  "simpnum (C j) = C j"
-  "simpnum (Bound n) = CN n 1 (C 0)"
-  "simpnum (Neg t) = numneg (simpnum t)"
-  "simpnum (Add t s) = numadd (simpnum t,simpnum s)"
-  "simpnum (Sub t s) = numsub (simpnum t) (simpnum s)"
-  "simpnum (Mul i t) = (if i = 0 then (C 0) else nummul (simpnum t) i)"
-  "simpnum (CN n c t) = (if c = 0 then simpnum t else numadd (CN n c (C 0),simpnum t))"
-
-lemma simpnum_ci[simp]: "Inum bs (simpnum t) = Inum bs t"
-by (induct t rule: simpnum.induct, auto simp add: numneg numadd numsub nummul)
-
-lemma simpnum_numbound0[simp]: 
-  "numbound0 t \<Longrightarrow> numbound0 (simpnum t)"
-by (induct t rule: simpnum.induct, auto)
-
-consts nozerocoeff:: "num \<Rightarrow> bool"
-recdef nozerocoeff "measure size"
-  "nozerocoeff (C c) = True"
-  "nozerocoeff (CN n c t) = (c\<noteq>0 \<and> nozerocoeff t)"
-  "nozerocoeff t = True"
-
-lemma numadd_nz : "nozerocoeff a \<Longrightarrow> nozerocoeff b \<Longrightarrow> nozerocoeff (numadd (a,b))"
-by (induct a b rule: numadd.induct,auto simp add: Let_def)
-
-lemma nummul_nz : "\<And> i. i\<noteq>0 \<Longrightarrow> nozerocoeff a \<Longrightarrow> nozerocoeff (nummul a i)"
-by (induct a rule: nummul.induct,auto simp add: Let_def numadd_nz)
-
-lemma numneg_nz : "nozerocoeff a \<Longrightarrow> nozerocoeff (numneg a)"
-by (simp add: numneg_def nummul_nz)
-
-lemma numsub_nz: "nozerocoeff a \<Longrightarrow> nozerocoeff b \<Longrightarrow> nozerocoeff (numsub a b)"
-by (simp add: numsub_def numneg_nz numadd_nz)
-
-lemma simpnum_nz: "nozerocoeff (simpnum t)"
-by(induct t rule: simpnum.induct, auto simp add: numadd_nz numneg_nz numsub_nz nummul_nz)
-
-lemma maxcoeff_nz: "nozerocoeff t \<Longrightarrow> maxcoeff t = 0 \<Longrightarrow> t = C 0"
-proof (induct t rule: maxcoeff.induct)
-  case (2 n c t)
-  hence cnz: "c \<noteq>0" and mx: "max (abs c) (maxcoeff t) = 0" by simp+
-  have "max (abs c) (maxcoeff t) \<ge> abs c" by (simp add: le_maxI1)
-  with cnz have "max (abs c) (maxcoeff t) > 0" by arith
-  with prems show ?case by simp
-qed auto
-
-lemma numgcd_nz: assumes nz: "nozerocoeff t" and g0: "numgcd t = 0" shows "t = C 0"
-proof-
-  from g0 have th:"numgcdh t (maxcoeff t) = 0" by (simp add: numgcd_def)
-  from numgcdh0[OF th]  have th:"maxcoeff t = 0" .
-  from maxcoeff_nz[OF nz th] show ?thesis .
-qed
-
-constdefs simp_num_pair:: "(num \<times> int) \<Rightarrow> num \<times> int"
-  "simp_num_pair \<equiv> (\<lambda> (t,n). (if n = 0 then (C 0, 0) else
-   (let t' = simpnum t ; g = numgcd t' in 
-      if g > 1 then (let g' = zgcd n g in 
-        if g' = 1 then (t',n) 
-        else (reducecoeffh t' g', n div g')) 
-      else (t',n))))"
-
-lemma simp_num_pair_ci:
-  shows "((\<lambda> (t,n). Inum bs t / real n) (simp_num_pair (t,n))) = ((\<lambda> (t,n). Inum bs t / real n) (t,n))"
-  (is "?lhs = ?rhs")
-proof-
-  let ?t' = "simpnum t"
-  let ?g = "numgcd ?t'"
-  let ?g' = "zgcd n ?g"
-  {assume nz: "n = 0" hence ?thesis by (simp add: Let_def simp_num_pair_def)}
-  moreover
-  { assume nnz: "n \<noteq> 0"
-    {assume "\<not> ?g > 1" hence ?thesis by (simp add: Let_def simp_num_pair_def simpnum_ci)}
-    moreover
-    {assume g1:"?g>1" hence g0: "?g > 0" by simp
-      from zgcd0 g1 nnz have gp0: "?g' \<noteq> 0" by simp
-      hence g'p: "?g' > 0" using zgcd_pos[where i="n" and j="numgcd ?t'"] by arith 
-      hence "?g'= 1 \<or> ?g' > 1" by arith
-      moreover {assume "?g'=1" hence ?thesis by (simp add: Let_def simp_num_pair_def simpnum_ci)}
-      moreover {assume g'1:"?g'>1"
-	from dvdnumcoeff_aux2[OF g1] have th1:"dvdnumcoeff ?t' ?g" ..
-	let ?tt = "reducecoeffh ?t' ?g'"
-	let ?t = "Inum bs ?tt"
-	have gpdg: "?g' dvd ?g" by (simp add: zgcd_zdvd2)
-	have gpdd: "?g' dvd n" by (simp add: zgcd_zdvd1) 
-	have gpdgp: "?g' dvd ?g'" by simp
-	from reducecoeffh[OF dvdnumcoeff_trans[OF gpdg th1] g'p] 
-	have th2:"real ?g' * ?t = Inum bs ?t'" by simp
-	from prems have "?lhs = ?t / real (n div ?g')" by (simp add: simp_num_pair_def Let_def)
-	also have "\<dots> = (real ?g' * ?t) / (real ?g' * (real (n div ?g')))" by simp
-	also have "\<dots> = (Inum bs ?t' / real n)"
-	  using real_of_int_div[OF gp0 gpdd] th2 gp0 by simp
-	finally have "?lhs = Inum bs t / real n" by (simp add: simpnum_ci)
-	then have ?thesis using prems by (simp add: simp_num_pair_def)}
-      ultimately have ?thesis by blast}
-    ultimately have ?thesis by blast} 
-  ultimately show ?thesis by blast
-qed
-
-lemma simp_num_pair_l: assumes tnb: "numbound0 t" and np: "n >0" and tn: "simp_num_pair (t,n) = (t',n')"
-  shows "numbound0 t' \<and> n' >0"
-proof-
-    let ?t' = "simpnum t"
-  let ?g = "numgcd ?t'"
-  let ?g' = "zgcd n ?g"
-  {assume nz: "n = 0" hence ?thesis using prems by (simp add: Let_def simp_num_pair_def)}
-  moreover
-  { assume nnz: "n \<noteq> 0"
-    {assume "\<not> ?g > 1" hence ?thesis  using prems by (auto simp add: Let_def simp_num_pair_def simpnum_numbound0)}
-    moreover
-    {assume g1:"?g>1" hence g0: "?g > 0" by simp
-      from zgcd0 g1 nnz have gp0: "?g' \<noteq> 0" by simp
-      hence g'p: "?g' > 0" using zgcd_pos[where i="n" and j="numgcd ?t'"] by arith
-      hence "?g'= 1 \<or> ?g' > 1" by arith
-      moreover {assume "?g'=1" hence ?thesis using prems 
-	  by (auto simp add: Let_def simp_num_pair_def simpnum_numbound0)}
-      moreover {assume g'1:"?g'>1"
-	have gpdg: "?g' dvd ?g" by (simp add: zgcd_zdvd2)
-	have gpdd: "?g' dvd n" by (simp add: zgcd_zdvd1) 
-	have gpdgp: "?g' dvd ?g'" by simp
-	from zdvd_imp_le[OF gpdd np] have g'n: "?g' \<le> n" .
-	from zdiv_mono1[OF g'n g'p, simplified zdiv_self[OF gp0]]
-	have "n div ?g' >0" by simp
-	hence ?thesis using prems 
-	  by(auto simp add: simp_num_pair_def Let_def reducecoeffh_numbound0 simpnum_numbound0)}
-      ultimately have ?thesis by blast}
-    ultimately have ?thesis by blast} 
-  ultimately show ?thesis by blast
-qed
-
-consts simpfm :: "fm \<Rightarrow> fm"
-recdef simpfm "measure fmsize"
-  "simpfm (And p q) = conj (simpfm p) (simpfm q)"
-  "simpfm (Or p q) = disj (simpfm p) (simpfm q)"
-  "simpfm (Imp p q) = imp (simpfm p) (simpfm q)"
-  "simpfm (Iff p q) = iff (simpfm p) (simpfm q)"
-  "simpfm (NOT p) = not (simpfm p)"
-  "simpfm (Lt a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v < 0) then T else F 
-  | _ \<Rightarrow> Lt a')"
-  "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')"
-  "simpfm (Gt a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v > 0)  then T else F | _ \<Rightarrow> Gt a')"
-  "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')"
-  "simpfm (Eq a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v = 0)  then T else F | _ \<Rightarrow> Eq a')"
-  "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')"
-  "simpfm p = p"
-lemma simpfm: "Ifm bs (simpfm p) = Ifm bs p"
-proof(induct p rule: simpfm.induct)
-  case (6 a) let ?sa = "simpnum a" from simpnum_ci have sa: "Inum bs ?sa = Inum bs a" by simp
-  {fix v assume "?sa = C v" hence ?case using sa by simp }
-  moreover {assume "\<not> (\<exists> v. ?sa = C v)" hence ?case using sa 
-      by (cases ?sa, simp_all add: Let_def)}
-  ultimately show ?case by blast
-next
-  case (7 a)  let ?sa = "simpnum a" 
-  from simpnum_ci have sa: "Inum bs ?sa = Inum bs a" by simp
-  {fix v assume "?sa = C v" hence ?case using sa by simp }
-  moreover {assume "\<not> (\<exists> v. ?sa = C v)" hence ?case using sa 
-      by (cases ?sa, simp_all add: Let_def)}
-  ultimately show ?case by blast
-next
-  case (8 a)  let ?sa = "simpnum a" 
-  from simpnum_ci have sa: "Inum bs ?sa = Inum bs a" by simp
-  {fix v assume "?sa = C v" hence ?case using sa by simp }
-  moreover {assume "\<not> (\<exists> v. ?sa = C v)" hence ?case using sa 
-      by (cases ?sa, simp_all add: Let_def)}
-  ultimately show ?case by blast
-next
-  case (9 a)  let ?sa = "simpnum a" 
-  from simpnum_ci have sa: "Inum bs ?sa = Inum bs a" by simp
-  {fix v assume "?sa = C v" hence ?case using sa by simp }
-  moreover {assume "\<not> (\<exists> v. ?sa = C v)" hence ?case using sa 
-      by (cases ?sa, simp_all add: Let_def)}
-  ultimately show ?case by blast
-next
-  case (10 a)  let ?sa = "simpnum a" 
-  from simpnum_ci have sa: "Inum bs ?sa = Inum bs a" by simp
-  {fix v assume "?sa = C v" hence ?case using sa by simp }
-  moreover {assume "\<not> (\<exists> v. ?sa = C v)" hence ?case using sa 
-      by (cases ?sa, simp_all add: Let_def)}
-  ultimately show ?case by blast
-next
-  case (11 a)  let ?sa = "simpnum a" 
-  from simpnum_ci have sa: "Inum bs ?sa = Inum bs a" by simp
-  {fix v assume "?sa = C v" hence ?case using sa by simp }
-  moreover {assume "\<not> (\<exists> v. ?sa = C v)" hence ?case using sa 
-      by (cases ?sa, simp_all add: Let_def)}
-  ultimately show ?case by blast
-qed (induct p rule: simpfm.induct, simp_all add: conj disj imp iff not)
-
-
-lemma simpfm_bound0: "bound0 p \<Longrightarrow> bound0 (simpfm p)"
-proof(induct p rule: simpfm.induct)
-  case (6 a) hence nb: "numbound0 a" by simp
-  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
-  thus ?case by (cases "simpnum a", auto simp add: Let_def)
-next
-  case (7 a) hence nb: "numbound0 a" by simp
-  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
-  thus ?case by (cases "simpnum a", auto simp add: Let_def)
-next
-  case (8 a) hence nb: "numbound0 a" by simp
-  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
-  thus ?case by (cases "simpnum a", auto simp add: Let_def)
-next
-  case (9 a) hence nb: "numbound0 a" by simp
-  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
-  thus ?case by (cases "simpnum a", auto simp add: Let_def)
-next
-  case (10 a) hence nb: "numbound0 a" by simp
-  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
-  thus ?case by (cases "simpnum a", auto simp add: Let_def)
-next
-  case (11 a) hence nb: "numbound0 a" by simp
-  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
-  thus ?case by (cases "simpnum a", auto simp add: Let_def)
-qed(auto simp add: disj_def imp_def iff_def conj_def not_bn)
-
-lemma simpfm_qf: "qfree p \<Longrightarrow> qfree (simpfm p)"
-by (induct p rule: simpfm.induct, auto simp add: disj_qf imp_qf iff_qf conj_qf not_qf Let_def)
- (case_tac "simpnum a",auto)+
-
-consts prep :: "fm \<Rightarrow> fm"
-recdef prep "measure fmsize"
-  "prep (E T) = T"
-  "prep (E F) = F"
-  "prep (E (Or p q)) = disj (prep (E p)) (prep (E q))"
-  "prep (E (Imp p q)) = disj (prep (E (NOT p))) (prep (E q))"
-  "prep (E (Iff p q)) = disj (prep (E (And p q))) (prep (E (And (NOT p) (NOT q))))" 
-  "prep (E (NOT (And p q))) = disj (prep (E (NOT p))) (prep (E(NOT q)))"
-  "prep (E (NOT (Imp p q))) = prep (E (And p (NOT q)))"
-  "prep (E (NOT (Iff p q))) = disj (prep (E (And p (NOT q)))) (prep (E(And (NOT p) q)))"
-  "prep (E p) = E (prep p)"
-  "prep (A (And p q)) = conj (prep (A p)) (prep (A q))"
-  "prep (A p) = prep (NOT (E (NOT p)))"
-  "prep (NOT (NOT p)) = prep p"
-  "prep (NOT (And p q)) = disj (prep (NOT p)) (prep (NOT q))"
-  "prep (NOT (A p)) = prep (E (NOT p))"
-  "prep (NOT (Or p q)) = conj (prep (NOT p)) (prep (NOT q))"
-  "prep (NOT (Imp p q)) = conj (prep p) (prep (NOT q))"
-  "prep (NOT (Iff p q)) = disj (prep (And p (NOT q))) (prep (And (NOT p) q))"
-  "prep (NOT p) = not (prep p)"
-  "prep (Or p q) = disj (prep p) (prep q)"
-  "prep (And p q) = conj (prep p) (prep q)"
-  "prep (Imp p q) = prep (Or (NOT p) q)"
-  "prep (Iff p q) = disj (prep (And p q)) (prep (And (NOT p) (NOT q)))"
-  "prep p = p"
-(hints simp add: fmsize_pos)
-lemma prep: "\<And> bs. Ifm bs (prep p) = Ifm bs p"
-by (induct p rule: prep.induct, auto)
-
-  (* Generic quantifier elimination *)
-consts qelim :: "fm \<Rightarrow> (fm \<Rightarrow> fm) \<Rightarrow> fm"
-recdef qelim "measure fmsize"
-  "qelim (E p) = (\<lambda> qe. DJ qe (qelim p qe))"
-  "qelim (A p) = (\<lambda> qe. not (qe ((qelim (NOT p) qe))))"
-  "qelim (NOT p) = (\<lambda> qe. not (qelim p qe))"
-  "qelim (And p q) = (\<lambda> qe. conj (qelim p qe) (qelim q qe))" 
-  "qelim (Or  p q) = (\<lambda> qe. disj (qelim p qe) (qelim q qe))" 
-  "qelim (Imp p q) = (\<lambda> qe. imp (qelim p qe) (qelim q qe))"
-  "qelim (Iff p q) = (\<lambda> qe. iff (qelim p qe) (qelim q qe))"
-  "qelim p = (\<lambda> y. simpfm p)"
-
-lemma qelim_ci:
-  assumes qe_inv: "\<forall> bs p. qfree p \<longrightarrow> qfree (qe p) \<and> (Ifm bs (qe p) = Ifm bs (E p))"
-  shows "\<And> bs. qfree (qelim p qe) \<and> (Ifm bs (qelim p qe) = Ifm bs p)"
-using qe_inv DJ_qe[OF qe_inv] 
-by(induct p rule: qelim.induct) 
-(auto simp add: not disj conj iff imp not_qf disj_qf conj_qf imp_qf iff_qf 
-  simpfm simpfm_qf simp del: simpfm.simps)
-
-consts 
-  plusinf:: "fm \<Rightarrow> fm" (* Virtual substitution of +\<infinity>*)
-  minusinf:: "fm \<Rightarrow> fm" (* Virtual substitution of -\<infinity>*)
-recdef minusinf "measure size"
-  "minusinf (And p q) = conj (minusinf p) (minusinf q)" 
-  "minusinf (Or p q) = disj (minusinf p) (minusinf q)" 
-  "minusinf (Eq  (CN 0 c e)) = F"
-  "minusinf (NEq (CN 0 c e)) = T"
-  "minusinf (Lt  (CN 0 c e)) = T"
-  "minusinf (Le  (CN 0 c e)) = T"
-  "minusinf (Gt  (CN 0 c e)) = F"
-  "minusinf (Ge  (CN 0 c e)) = F"
-  "minusinf p = p"
-
-recdef plusinf "measure size"
-  "plusinf (And p q) = conj (plusinf p) (plusinf q)" 
-  "plusinf (Or p q) = disj (plusinf p) (plusinf q)" 
-  "plusinf (Eq  (CN 0 c e)) = F"
-  "plusinf (NEq (CN 0 c e)) = T"
-  "plusinf (Lt  (CN 0 c e)) = F"
-  "plusinf (Le  (CN 0 c e)) = F"
-  "plusinf (Gt  (CN 0 c e)) = T"
-  "plusinf (Ge  (CN 0 c e)) = T"
-  "plusinf p = p"
-
-consts
-  isrlfm :: "fm \<Rightarrow> bool"   (* Linearity test for fm *)
-recdef isrlfm "measure size"
-  "isrlfm (And p q) = (isrlfm p \<and> isrlfm q)" 
-  "isrlfm (Or p q) = (isrlfm p \<and> isrlfm q)" 
-  "isrlfm (Eq  (CN 0 c e)) = (c>0 \<and> numbound0 e)"
-  "isrlfm (NEq (CN 0 c e)) = (c>0 \<and> numbound0 e)"
-  "isrlfm (Lt  (CN 0 c e)) = (c>0 \<and> numbound0 e)"
-  "isrlfm (Le  (CN 0 c e)) = (c>0 \<and> numbound0 e)"
-  "isrlfm (Gt  (CN 0 c e)) = (c>0 \<and> numbound0 e)"
-  "isrlfm (Ge  (CN 0 c e)) = (c>0 \<and> numbound0 e)"
-  "isrlfm p = (isatom p \<and> (bound0 p))"
-
-  (* splits the bounded from the unbounded part*)
-consts rsplit0 :: "num \<Rightarrow> int \<times> num" 
-recdef rsplit0 "measure num_size"
-  "rsplit0 (Bound 0) = (1,C 0)"
-  "rsplit0 (Add a b) = (let (ca,ta) = rsplit0 a ; (cb,tb) = rsplit0 b 
-              in (ca+cb, Add ta tb))"
-  "rsplit0 (Sub a b) = rsplit0 (Add a (Neg b))"
-  "rsplit0 (Neg a) = (let (c,t) = rsplit0 a in (-c,Neg t))"
-  "rsplit0 (Mul c a) = (let (ca,ta) = rsplit0 a in (c*ca,Mul c ta))"
-  "rsplit0 (CN 0 c a) = (let (ca,ta) = rsplit0 a in (c+ca,ta))"
-  "rsplit0 (CN n c a) = (let (ca,ta) = rsplit0 a in (ca,CN n c ta))"
-  "rsplit0 t = (0,t)"
-lemma rsplit0: 
-  shows "Inum bs ((split (CN 0)) (rsplit0 t)) = Inum bs t \<and> numbound0 (snd (rsplit0 t))"
-proof (induct t rule: rsplit0.induct)
-  case (2 a b) 
-  let ?sa = "rsplit0 a" let ?sb = "rsplit0 b"
-  let ?ca = "fst ?sa" let ?cb = "fst ?sb"
-  let ?ta = "snd ?sa" let ?tb = "snd ?sb"
-  from prems have nb: "numbound0 (snd(rsplit0 (Add a b)))" 
-    by(cases "rsplit0 a",auto simp add: Let_def split_def)
-  have "Inum bs ((split (CN 0)) (rsplit0 (Add a b))) = 
-    Inum bs ((split (CN 0)) ?sa)+Inum bs ((split (CN 0)) ?sb)"
-    by (simp add: Let_def split_def algebra_simps)
-  also have "\<dots> = Inum bs a + Inum bs b" using prems by (cases "rsplit0 a", simp_all)
-  finally show ?case using nb by simp 
-qed(auto simp add: Let_def split_def algebra_simps , simp add: right_distrib[symmetric])
-
-    (* Linearize a formula*)
-definition
-  lt :: "int \<Rightarrow> num \<Rightarrow> fm"
-where
-  "lt c t = (if c = 0 then (Lt t) else if c > 0 then (Lt (CN 0 c t)) 
-    else (Gt (CN 0 (-c) (Neg t))))"
-
-definition
-  le :: "int \<Rightarrow> num \<Rightarrow> fm"
-where
-  "le c t = (if c = 0 then (Le t) else if c > 0 then (Le (CN 0 c t)) 
-    else (Ge (CN 0 (-c) (Neg t))))"
-
-definition
-  gt :: "int \<Rightarrow> num \<Rightarrow> fm"
-where
-  "gt c t = (if c = 0 then (Gt t) else if c > 0 then (Gt (CN 0 c t)) 
-    else (Lt (CN 0 (-c) (Neg t))))"
-
-definition
-  ge :: "int \<Rightarrow> num \<Rightarrow> fm"
-where
-  "ge c t = (if c = 0 then (Ge t) else if c > 0 then (Ge (CN 0 c t)) 
-    else (Le (CN 0 (-c) (Neg t))))"
-
-definition
-  eq :: "int \<Rightarrow> num \<Rightarrow> fm"
-where
-  "eq c t = (if c = 0 then (Eq t) else if c > 0 then (Eq (CN 0 c t)) 
-    else (Eq (CN 0 (-c) (Neg t))))"
-
-definition
-  neq :: "int \<Rightarrow> num \<Rightarrow> fm"
-where
-  "neq c t = (if c = 0 then (NEq t) else if c > 0 then (NEq (CN 0 c t)) 
-    else (NEq (CN 0 (-c) (Neg t))))"
-
-lemma lt: "numnoabs t \<Longrightarrow> Ifm bs (split lt (rsplit0 t)) = Ifm bs (Lt t) \<and> isrlfm (split lt (rsplit0 t))"
-using rsplit0[where bs = "bs" and t="t"]
-by (auto simp add: lt_def split_def,cases "snd(rsplit0 t)",auto,case_tac "nat",auto)
-
-lemma le: "numnoabs t \<Longrightarrow> Ifm bs (split le (rsplit0 t)) = Ifm bs (Le t) \<and> isrlfm (split le (rsplit0 t))"
-using rsplit0[where bs = "bs" and t="t"]
-by (auto simp add: le_def split_def) (cases "snd(rsplit0 t)",auto,case_tac "nat",auto)
-
-lemma gt: "numnoabs t \<Longrightarrow> Ifm bs (split gt (rsplit0 t)) = Ifm bs (Gt t) \<and> isrlfm (split gt (rsplit0 t))"
-using rsplit0[where bs = "bs" and t="t"]
-by (auto simp add: gt_def split_def) (cases "snd(rsplit0 t)",auto,case_tac "nat",auto)
-
-lemma ge: "numnoabs t \<Longrightarrow> Ifm bs (split ge (rsplit0 t)) = Ifm bs (Ge t) \<and> isrlfm (split ge (rsplit0 t))"
-using rsplit0[where bs = "bs" and t="t"]
-by (auto simp add: ge_def split_def) (cases "snd(rsplit0 t)",auto,case_tac "nat",auto)
-
-lemma eq: "numnoabs t \<Longrightarrow> Ifm bs (split eq (rsplit0 t)) = Ifm bs (Eq t) \<and> isrlfm (split eq (rsplit0 t))"
-using rsplit0[where bs = "bs" and t="t"]
-by (auto simp add: eq_def split_def) (cases "snd(rsplit0 t)",auto,case_tac "nat",auto)
-
-lemma neq: "numnoabs t \<Longrightarrow> Ifm bs (split neq (rsplit0 t)) = Ifm bs (NEq t) \<and> isrlfm (split neq (rsplit0 t))"
-using rsplit0[where bs = "bs" and t="t"]
-by (auto simp add: neq_def split_def) (cases "snd(rsplit0 t)",auto,case_tac "nat",auto)
-
-lemma conj_lin: "isrlfm p \<Longrightarrow> isrlfm q \<Longrightarrow> isrlfm (conj p q)"
-by (auto simp add: conj_def)
-lemma disj_lin: "isrlfm p \<Longrightarrow> isrlfm q \<Longrightarrow> isrlfm (disj p q)"
-by (auto simp add: disj_def)
-
-consts rlfm :: "fm \<Rightarrow> fm"
-recdef rlfm "measure fmsize"
-  "rlfm (And p q) = conj (rlfm p) (rlfm q)"
-  "rlfm (Or p q) = disj (rlfm p) (rlfm q)"
-  "rlfm (Imp p q) = disj (rlfm (NOT p)) (rlfm q)"
-  "rlfm (Iff p q) = disj (conj (rlfm p) (rlfm q)) (conj (rlfm (NOT p)) (rlfm (NOT q)))"
-  "rlfm (Lt a) = split lt (rsplit0 a)"
-  "rlfm (Le a) = split le (rsplit0 a)"
-  "rlfm (Gt a) = split gt (rsplit0 a)"
-  "rlfm (Ge a) = split ge (rsplit0 a)"
-  "rlfm (Eq a) = split eq (rsplit0 a)"
-  "rlfm (NEq a) = split neq (rsplit0 a)"
-  "rlfm (NOT (And p q)) = disj (rlfm (NOT p)) (rlfm (NOT q))"
-  "rlfm (NOT (Or p q)) = conj (rlfm (NOT p)) (rlfm (NOT q))"
-  "rlfm (NOT (Imp p q)) = conj (rlfm p) (rlfm (NOT q))"
-  "rlfm (NOT (Iff p q)) = disj (conj(rlfm p) (rlfm(NOT q))) (conj(rlfm(NOT p)) (rlfm q))"
-  "rlfm (NOT (NOT p)) = rlfm p"
-  "rlfm (NOT T) = F"
-  "rlfm (NOT F) = T"
-  "rlfm (NOT (Lt a)) = rlfm (Ge a)"
-  "rlfm (NOT (Le a)) = rlfm (Gt a)"
-  "rlfm (NOT (Gt a)) = rlfm (Le a)"
-  "rlfm (NOT (Ge a)) = rlfm (Lt a)"
-  "rlfm (NOT (Eq a)) = rlfm (NEq a)"
-  "rlfm (NOT (NEq a)) = rlfm (Eq a)"
-  "rlfm p = p" (hints simp add: fmsize_pos)
-
-lemma rlfm_I:
-  assumes qfp: "qfree p"
-  shows "(Ifm bs (rlfm p) = Ifm bs p) \<and> isrlfm (rlfm p)"
-  using qfp 
-by (induct p rule: rlfm.induct, auto simp add: lt le gt ge eq neq conj disj conj_lin disj_lin)
-
-    (* Operations needed for Ferrante and Rackoff *)
-lemma rminusinf_inf:
-  assumes lp: "isrlfm p"
-  shows "\<exists> z. \<forall> x < z. Ifm (x#bs) (minusinf p) = Ifm (x#bs) p" (is "\<exists> z. \<forall> x. ?P z x p")
-using lp
-proof (induct p rule: minusinf.induct)
-  case (1 p q) thus ?case by (auto,rule_tac x= "min z za" in exI) auto 
-next
-  case (2 p q) thus ?case by (auto,rule_tac x= "min z za" in exI) auto
-next
-  case (3 c e) 
-  from prems have nb: "numbound0 e" by simp
-  from prems have cp: "real c > 0" by simp
-  fix a
-  let ?e="Inum (a#bs) e"
-  let ?z = "(- ?e) / real c"
-  {fix x
-    assume xz: "x < ?z"
-    hence "(real c * x < - ?e)" 
-      by (simp only: pos_less_divide_eq[OF cp, where a="x" and b="- ?e"] mult_ac) 
-    hence "real c * x + ?e < 0" by arith
-    hence "real c * x + ?e \<noteq> 0" by simp
-    with xz have "?P ?z x (Eq (CN 0 c e))"
-      using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp  }
-  hence "\<forall> x < ?z. ?P ?z x (Eq (CN 0 c e))" by simp
-  thus ?case by blast
-next
-  case (4 c e)   
-  from prems have nb: "numbound0 e" by simp
-  from prems have cp: "real c > 0" by simp
-  fix a
-  let ?e="Inum (a#bs) e"
-  let ?z = "(- ?e) / real c"
-  {fix x
-    assume xz: "x < ?z"
-    hence "(real c * x < - ?e)" 
-      by (simp only: pos_less_divide_eq[OF cp, where a="x" and b="- ?e"] mult_ac) 
-    hence "real c * x + ?e < 0" by arith
-    hence "real c * x + ?e \<noteq> 0" by simp
-    with xz have "?P ?z x (NEq (CN 0 c e))"
-      using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp }
-  hence "\<forall> x < ?z. ?P ?z x (NEq (CN 0 c e))" by simp
-  thus ?case by blast
-next
-  case (5 c e) 
-    from prems have nb: "numbound0 e" by simp
-  from prems have cp: "real c > 0" by simp
-  fix a
-  let ?e="Inum (a#bs) e"
-  let ?z = "(- ?e) / real c"
-  {fix x
-    assume xz: "x < ?z"
-    hence "(real c * x < - ?e)" 
-      by (simp only: pos_less_divide_eq[OF cp, where a="x" and b="- ?e"] mult_ac) 
-    hence "real c * x + ?e < 0" by arith
-    with xz have "?P ?z x (Lt (CN 0 c e))"
-      using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"]  by simp }
-  hence "\<forall> x < ?z. ?P ?z x (Lt (CN 0 c e))" by simp
-  thus ?case by blast
-next
-  case (6 c e)  
-    from prems have nb: "numbound0 e" by simp
-  from prems have cp: "real c > 0" by simp
-  fix a
-  let ?e="Inum (a#bs) e"
-  let ?z = "(- ?e) / real c"
-  {fix x
-    assume xz: "x < ?z"
-    hence "(real c * x < - ?e)" 
-      by (simp only: pos_less_divide_eq[OF cp, where a="x" and b="- ?e"] mult_ac) 
-    hence "real c * x + ?e < 0" by arith
-    with xz have "?P ?z x (Le (CN 0 c e))"
-      using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp }
-  hence "\<forall> x < ?z. ?P ?z x (Le (CN 0 c e))" by simp
-  thus ?case by blast
-next
-  case (7 c e)  
-    from prems have nb: "numbound0 e" by simp
-  from prems have cp: "real c > 0" by simp
-  fix a
-  let ?e="Inum (a#bs) e"
-  let ?z = "(- ?e) / real c"
-  {fix x
-    assume xz: "x < ?z"
-    hence "(real c * x < - ?e)" 
-      by (simp only: pos_less_divide_eq[OF cp, where a="x" and b="- ?e"] mult_ac) 
-    hence "real c * x + ?e < 0" by arith
-    with xz have "?P ?z x (Gt (CN 0 c e))"
-      using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp }
-  hence "\<forall> x < ?z. ?P ?z x (Gt (CN 0 c e))" by simp
-  thus ?case by blast
-next
-  case (8 c e)  
-    from prems have nb: "numbound0 e" by simp
-  from prems have cp: "real c > 0" by simp
-  fix a
-  let ?e="Inum (a#bs) e"
-  let ?z = "(- ?e) / real c"
-  {fix x
-    assume xz: "x < ?z"
-    hence "(real c * x < - ?e)" 
-      by (simp only: pos_less_divide_eq[OF cp, where a="x" and b="- ?e"] mult_ac) 
-    hence "real c * x + ?e < 0" by arith
-    with xz have "?P ?z x (Ge (CN 0 c e))"
-      using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp }
-  hence "\<forall> x < ?z. ?P ?z x (Ge (CN 0 c e))" by simp
-  thus ?case by blast
-qed simp_all
-
-lemma rplusinf_inf:
-  assumes lp: "isrlfm p"
-  shows "\<exists> z. \<forall> x > z. Ifm (x#bs) (plusinf p) = Ifm (x#bs) p" (is "\<exists> z. \<forall> x. ?P z x p")
-using lp
-proof (induct p rule: isrlfm.induct)
-  case (1 p q) thus ?case by (auto,rule_tac x= "max z za" in exI) auto
-next
-  case (2 p q) thus ?case by (auto,rule_tac x= "max z za" in exI) auto
-next
-  case (3 c e) 
-  from prems have nb: "numbound0 e" by simp
-  from prems have cp: "real c > 0" by simp
-  fix a
-  let ?e="Inum (a#bs) e"
-  let ?z = "(- ?e) / real c"
-  {fix x
-    assume xz: "x > ?z"
-    with mult_strict_right_mono [OF xz cp] cp
-    have "(real c * x > - ?e)" by (simp add: mult_ac)
-    hence "real c * x + ?e > 0" by arith
-    hence "real c * x + ?e \<noteq> 0" by simp
-    with xz have "?P ?z x (Eq (CN 0 c e))"
-      using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp }
-  hence "\<forall> x > ?z. ?P ?z x (Eq (CN 0 c e))" by simp
-  thus ?case by blast
-next
-  case (4 c e) 
-  from prems have nb: "numbound0 e" by simp
-  from prems have cp: "real c > 0" by simp
-  fix a
-  let ?e="Inum (a#bs) e"
-  let ?z = "(- ?e) / real c"
-  {fix x
-    assume xz: "x > ?z"
-    with mult_strict_right_mono [OF xz cp] cp
-    have "(real c * x > - ?e)" by (simp add: mult_ac)
-    hence "real c * x + ?e > 0" by arith
-    hence "real c * x + ?e \<noteq> 0" by simp
-    with xz have "?P ?z x (NEq (CN 0 c e))"
-      using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp }
-  hence "\<forall> x > ?z. ?P ?z x (NEq (CN 0 c e))" by simp
-  thus ?case by blast
-next
-  case (5 c e) 
-  from prems have nb: "numbound0 e" by simp
-  from prems have cp: "real c > 0" by simp
-  fix a
-  let ?e="Inum (a#bs) e"
-  let ?z = "(- ?e) / real c"
-  {fix x
-    assume xz: "x > ?z"
-    with mult_strict_right_mono [OF xz cp] cp
-    have "(real c * x > - ?e)" by (simp add: mult_ac)
-    hence "real c * x + ?e > 0" by arith
-    with xz have "?P ?z x (Lt (CN 0 c e))"
-      using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp }
-  hence "\<forall> x > ?z. ?P ?z x (Lt (CN 0 c e))" by simp
-  thus ?case by blast
-next
-  case (6 c e) 
-  from prems have nb: "numbound0 e" by simp
-  from prems have cp: "real c > 0" by simp
-  fix a
-  let ?e="Inum (a#bs) e"
-  let ?z = "(- ?e) / real c"
-  {fix x
-    assume xz: "x > ?z"
-    with mult_strict_right_mono [OF xz cp] cp
-    have "(real c * x > - ?e)" by (simp add: mult_ac)
-    hence "real c * x + ?e > 0" by arith
-    with xz have "?P ?z x (Le (CN 0 c e))"
-      using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp }
-  hence "\<forall> x > ?z. ?P ?z x (Le (CN 0 c e))" by simp
-  thus ?case by blast
-next
-  case (7 c e) 
-  from prems have nb: "numbound0 e" by simp
-  from prems have cp: "real c > 0" by simp
-  fix a
-  let ?e="Inum (a#bs) e"
-  let ?z = "(- ?e) / real c"
-  {fix x
-    assume xz: "x > ?z"
-    with mult_strict_right_mono [OF xz cp] cp
-    have "(real c * x > - ?e)" by (simp add: mult_ac)
-    hence "real c * x + ?e > 0" by arith
-    with xz have "?P ?z x (Gt (CN 0 c e))"
-      using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp }
-  hence "\<forall> x > ?z. ?P ?z x (Gt (CN 0 c e))" by simp
-  thus ?case by blast
-next
-  case (8 c e) 
-  from prems have nb: "numbound0 e" by simp
-  from prems have cp: "real c > 0" by simp
-  fix a
-  let ?e="Inum (a#bs) e"
-  let ?z = "(- ?e) / real c"
-  {fix x
-    assume xz: "x > ?z"
-    with mult_strict_right_mono [OF xz cp] cp
-    have "(real c * x > - ?e)" by (simp add: mult_ac)
-    hence "real c * x + ?e > 0" by arith
-    with xz have "?P ?z x (Ge (CN 0 c e))"
-      using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"]   by simp }
-  hence "\<forall> x > ?z. ?P ?z x (Ge (CN 0 c e))" by simp
-  thus ?case by blast
-qed simp_all
-
-lemma rminusinf_bound0:
-  assumes lp: "isrlfm p"
-  shows "bound0 (minusinf p)"
-  using lp
-  by (induct p rule: minusinf.induct) simp_all
-
-lemma rplusinf_bound0:
-  assumes lp: "isrlfm p"
-  shows "bound0 (plusinf p)"
-  using lp
-  by (induct p rule: plusinf.induct) simp_all
-
-lemma rminusinf_ex:
-  assumes lp: "isrlfm p"
-  and ex: "Ifm (a#bs) (minusinf p)"
-  shows "\<exists> x. Ifm (x#bs) p"
-proof-
-  from bound0_I [OF rminusinf_bound0[OF lp], where b="a" and bs ="bs"] ex
-  have th: "\<forall> x. Ifm (x#bs) (minusinf p)" by auto
-  from rminusinf_inf[OF lp, where bs="bs"] 
-  obtain z where z_def: "\<forall>x<z. Ifm (x # bs) (minusinf p) = Ifm (x # bs) p" by blast
-  from th have "Ifm ((z - 1)#bs) (minusinf p)" by simp
-  moreover have "z - 1 < z" by simp
-  ultimately show ?thesis using z_def by auto
-qed
-
-lemma rplusinf_ex:
-  assumes lp: "isrlfm p"
-  and ex: "Ifm (a#bs) (plusinf p)"
-  shows "\<exists> x. Ifm (x#bs) p"
-proof-
-  from bound0_I [OF rplusinf_bound0[OF lp], where b="a" and bs ="bs"] ex
-  have th: "\<forall> x. Ifm (x#bs) (plusinf p)" by auto
-  from rplusinf_inf[OF lp, where bs="bs"] 
-  obtain z where z_def: "\<forall>x>z. Ifm (x # bs) (plusinf p) = Ifm (x # bs) p" by blast
-  from th have "Ifm ((z + 1)#bs) (plusinf p)" by simp
-  moreover have "z + 1 > z" by simp
-  ultimately show ?thesis using z_def by auto
-qed
-
-consts 
-  uset:: "fm \<Rightarrow> (num \<times> int) list"
-  usubst :: "fm \<Rightarrow> (num \<times> int) \<Rightarrow> fm "
-recdef uset "measure size"
-  "uset (And p q) = (uset p @ uset q)" 
-  "uset (Or p q) = (uset p @ uset q)" 
-  "uset (Eq  (CN 0 c e)) = [(Neg e,c)]"
-  "uset (NEq (CN 0 c e)) = [(Neg e,c)]"
-  "uset (Lt  (CN 0 c e)) = [(Neg e,c)]"
-  "uset (Le  (CN 0 c e)) = [(Neg e,c)]"
-  "uset (Gt  (CN 0 c e)) = [(Neg e,c)]"
-  "uset (Ge  (CN 0 c e)) = [(Neg e,c)]"
-  "uset p = []"
-recdef usubst "measure size"
-  "usubst (And p q) = (\<lambda> (t,n). And (usubst p (t,n)) (usubst q (t,n)))"
-  "usubst (Or p q) = (\<lambda> (t,n). Or (usubst p (t,n)) (usubst q (t,n)))"
-  "usubst (Eq (CN 0 c e)) = (\<lambda> (t,n). Eq (Add (Mul c t) (Mul n e)))"
-  "usubst (NEq (CN 0 c e)) = (\<lambda> (t,n). NEq (Add (Mul c t) (Mul n e)))"
-  "usubst (Lt (CN 0 c e)) = (\<lambda> (t,n). Lt (Add (Mul c t) (Mul n e)))"
-  "usubst (Le (CN 0 c e)) = (\<lambda> (t,n). Le (Add (Mul c t) (Mul n e)))"
-  "usubst (Gt (CN 0 c e)) = (\<lambda> (t,n). Gt (Add (Mul c t) (Mul n e)))"
-  "usubst (Ge (CN 0 c e)) = (\<lambda> (t,n). Ge (Add (Mul c t) (Mul n e)))"
-  "usubst p = (\<lambda> (t,n). p)"
-
-lemma usubst_I: assumes lp: "isrlfm p"
-  and np: "real n > 0" and nbt: "numbound0 t"
-  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> _")
-  using lp
-proof(induct p rule: usubst.induct)
-  case (5 c e) from prems have cp: "c >0" and nb: "numbound0 e" by simp+
-  have "?I ?u (Lt (CN 0 c e)) = (real c *(?t/?n) + (?N x e) < 0)"
-    using numbound0_I[OF nb, where bs="bs" and b="?u" and b'="x"] by simp
-  also have "\<dots> = (?n*(real c *(?t/?n)) + ?n*(?N x e) < 0)"
-    by (simp only: pos_less_divide_eq[OF np, where a="real c *(?t/?n) + (?N x e)" 
-      and b="0", simplified divide_zero_left]) (simp only: algebra_simps)
-  also have "\<dots> = (real c *?t + ?n* (?N x e) < 0)"
-    using np by simp 
-  finally show ?case using nbt nb by (simp add: algebra_simps)
-next
-  case (6 c e) from prems have cp: "c >0" and nb: "numbound0 e" by simp+
-  have "?I ?u (Le (CN 0 c e)) = (real c *(?t/?n) + (?N x e) \<le> 0)"
-    using numbound0_I[OF nb, where bs="bs" and b="?u" and b'="x"] by simp
-  also have "\<dots> = (?n*(real c *(?t/?n)) + ?n*(?N x e) \<le> 0)"
-    by (simp only: pos_le_divide_eq[OF np, where a="real c *(?t/?n) + (?N x e)" 
-      and b="0", simplified divide_zero_left]) (simp only: algebra_simps)
-  also have "\<dots> = (real c *?t + ?n* (?N x e) \<le> 0)"
-    using np by simp 
-  finally show ?case using nbt nb by (simp add: algebra_simps)
-next
-  case (7 c e) from prems have cp: "c >0" and nb: "numbound0 e" by simp+
-  have "?I ?u (Gt (CN 0 c e)) = (real c *(?t/?n) + (?N x e) > 0)"
-    using numbound0_I[OF nb, where bs="bs" and b="?u" and b'="x"] by simp
-  also have "\<dots> = (?n*(real c *(?t/?n)) + ?n*(?N x e) > 0)"
-    by (simp only: pos_divide_less_eq[OF np, where a="real c *(?t/?n) + (?N x e)" 
-      and b="0", simplified divide_zero_left]) (simp only: algebra_simps)
-  also have "\<dots> = (real c *?t + ?n* (?N x e) > 0)"
-    using np by simp 
-  finally show ?case using nbt nb by (simp add: algebra_simps)
-next
-  case (8 c e) from prems have cp: "c >0" and nb: "numbound0 e" by simp+
-  have "?I ?u (Ge (CN 0 c e)) = (real c *(?t/?n) + (?N x e) \<ge> 0)"
-    using numbound0_I[OF nb, where bs="bs" and b="?u" and b'="x"] by simp
-  also have "\<dots> = (?n*(real c *(?t/?n)) + ?n*(?N x e) \<ge> 0)"
-    by (simp only: pos_divide_le_eq[OF np, where a="real c *(?t/?n) + (?N x e)" 
-      and b="0", simplified divide_zero_left]) (simp only: algebra_simps)
-  also have "\<dots> = (real c *?t + ?n* (?N x e) \<ge> 0)"
-    using np by simp 
-  finally show ?case using nbt nb by (simp add: algebra_simps)
-next
-  case (3 c e) from prems have cp: "c >0" and nb: "numbound0 e" by simp+
-  from np have np: "real n \<noteq> 0" by simp
-  have "?I ?u (Eq (CN 0 c e)) = (real c *(?t/?n) + (?N x e) = 0)"
-    using numbound0_I[OF nb, where bs="bs" and b="?u" and b'="x"] by simp
-  also have "\<dots> = (?n*(real c *(?t/?n)) + ?n*(?N x e) = 0)"
-    by (simp only: nonzero_eq_divide_eq[OF np, where a="real c *(?t/?n) + (?N x e)" 
-      and b="0", simplified divide_zero_left]) (simp only: algebra_simps)
-  also have "\<dots> = (real c *?t + ?n* (?N x e) = 0)"
-    using np by simp 
-  finally show ?case using nbt nb by (simp add: algebra_simps)
-next
-  case (4 c e) from prems have cp: "c >0" and nb: "numbound0 e" by simp+
-  from np have np: "real n \<noteq> 0" by simp
-  have "?I ?u (NEq (CN 0 c e)) = (real c *(?t/?n) + (?N x e) \<noteq> 0)"
-    using numbound0_I[OF nb, where bs="bs" and b="?u" and b'="x"] by simp
-  also have "\<dots> = (?n*(real c *(?t/?n)) + ?n*(?N x e) \<noteq> 0)"
-    by (simp only: nonzero_eq_divide_eq[OF np, where a="real c *(?t/?n) + (?N x e)" 
-      and b="0", simplified divide_zero_left]) (simp only: algebra_simps)
-  also have "\<dots> = (real c *?t + ?n* (?N x e) \<noteq> 0)"
-    using np by simp 
-  finally show ?case using nbt nb by (simp add: algebra_simps)
-qed(simp_all add: nbt numbound0_I[where bs ="bs" and b="(Inum (x#bs) t)/ real n" and b'="x"] nth_pos2)
-
-lemma uset_l:
-  assumes lp: "isrlfm p"
-  shows "\<forall> (t,k) \<in> set (uset p). numbound0 t \<and> k >0"
-using lp
-by(induct p rule: uset.induct,auto)
-
-lemma rminusinf_uset:
-  assumes lp: "isrlfm p"
-  and nmi: "\<not> (Ifm (a#bs) (minusinf p))" (is "\<not> (Ifm (a#bs) (?M p))")
-  and ex: "Ifm (x#bs) p" (is "?I x p")
-  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")
-proof-
-  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")
-    using lp nmi ex
-    by (induct p rule: minusinf.induct, auto simp add:numbound0_I[where bs="bs" and b="a" and b'="x"] nth_pos2)
-  then obtain s m where smU: "(s,m) \<in> set (uset p)" and mx: "real m * x \<ge> ?N a s" by blast
-  from uset_l[OF lp] smU have mp: "real m > 0" by auto
-  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" 
-    by (auto simp add: mult_commute)
-  thus ?thesis using smU by auto
-qed
-
-lemma rplusinf_uset:
-  assumes lp: "isrlfm p"
-  and nmi: "\<not> (Ifm (a#bs) (plusinf p))" (is "\<not> (Ifm (a#bs) (?M p))")
-  and ex: "Ifm (x#bs) p" (is "?I x p")
-  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")
-proof-
-  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")
-    using lp nmi ex
-    by (induct p rule: minusinf.induct, auto simp add:numbound0_I[where bs="bs" and b="a" and b'="x"] nth_pos2)
-  then obtain s m where smU: "(s,m) \<in> set (uset p)" and mx: "real m * x \<le> ?N a s" by blast
-  from uset_l[OF lp] smU have mp: "real m > 0" by auto
-  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" 
-    by (auto simp add: mult_commute)
-  thus ?thesis using smU by auto
-qed
-
-lemma lin_dense: 
-  assumes lp: "isrlfm p"
-  and noS: "\<forall> t. l < t \<and> t< u \<longrightarrow> t \<notin> (\<lambda> (t,n). Inum (x#bs) t / real n) ` set (uset p)" 
-  (is "\<forall> t. _ \<and> _ \<longrightarrow> t \<notin> (\<lambda> (t,n). ?N x t / real n ) ` (?U p)")
-  and lx: "l < x" and xu:"x < u" and px:" Ifm (x#bs) p"
-  and ly: "l < y" and yu: "y < u"
-  shows "Ifm (y#bs) p"
-using lp px noS
-proof (induct p rule: isrlfm.induct)
-  case (5 c e) hence cp: "real c > 0" and nb: "numbound0 e" by simp+
-    from prems have "x * real c + ?N x e < 0" by (simp add: algebra_simps)
-    hence pxc: "x < (- ?N x e) / real c" 
-      by (simp only: pos_less_divide_eq[OF cp, where a="x" and b="-?N x e"])
-    from prems have noSc:"\<forall> t. l < t \<and> t < u \<longrightarrow> t \<noteq> (- ?N x e) / real c" by auto
-    with ly yu have yne: "y \<noteq> - ?N x e / real c" by auto
-    hence "y < (- ?N x e) / real c \<or> y > (-?N x e) / real c" by auto
-    moreover {assume y: "y < (-?N x e)/ real c"
-      hence "y * real c < - ?N x e"
-	by (simp add: pos_less_divide_eq[OF cp, where a="y" and b="-?N x e", symmetric])
-      hence "real c * y + ?N x e < 0" by (simp add: algebra_simps)
-      hence ?case using numbound0_I[OF nb, where bs="bs" and b="x" and b'="y"] by simp}
-    moreover {assume y: "y > (- ?N x e) / real c" 
-      with yu have eu: "u > (- ?N x e) / real c" by auto
-      with noSc ly yu have "(- ?N x e) / real c \<le> l" by (cases "(- ?N x e) / real c > l", auto)
-      with lx pxc have "False" by auto
-      hence ?case by simp }
-    ultimately show ?case by blast
-next
-  case (6 c e) hence cp: "real c > 0" and nb: "numbound0 e" by simp +
-    from prems have "x * real c + ?N x e \<le> 0" by (simp add: algebra_simps)
-    hence pxc: "x \<le> (- ?N x e) / real c" 
-      by (simp only: pos_le_divide_eq[OF cp, where a="x" and b="-?N x e"])
-    from prems have noSc:"\<forall> t. l < t \<and> t < u \<longrightarrow> t \<noteq> (- ?N x e) / real c" by auto
-    with ly yu have yne: "y \<noteq> - ?N x e / real c" by auto
-    hence "y < (- ?N x e) / real c \<or> y > (-?N x e) / real c" by auto
-    moreover {assume y: "y < (-?N x e)/ real c"
-      hence "y * real c < - ?N x e"
-	by (simp add: pos_less_divide_eq[OF cp, where a="y" and b="-?N x e", symmetric])
-      hence "real c * y + ?N x e < 0" by (simp add: algebra_simps)
-      hence ?case using numbound0_I[OF nb, where bs="bs" and b="x" and b'="y"] by simp}
-    moreover {assume y: "y > (- ?N x e) / real c" 
-      with yu have eu: "u > (- ?N x e) / real c" by auto
-      with noSc ly yu have "(- ?N x e) / real c \<le> l" by (cases "(- ?N x e) / real c > l", auto)
-      with lx pxc have "False" by auto
-      hence ?case by simp }
-    ultimately show ?case by blast
-next
-  case (7 c e) hence cp: "real c > 0" and nb: "numbound0 e" by simp+
-    from prems have "x * real c + ?N x e > 0" by (simp add: algebra_simps)
-    hence pxc: "x > (- ?N x e) / real c" 
-      by (simp only: pos_divide_less_eq[OF cp, where a="x" and b="-?N x e"])
-    from prems have noSc:"\<forall> t. l < t \<and> t < u \<longrightarrow> t \<noteq> (- ?N x e) / real c" by auto
-    with ly yu have yne: "y \<noteq> - ?N x e / real c" by auto
-    hence "y < (- ?N x e) / real c \<or> y > (-?N x e) / real c" by auto
-    moreover {assume y: "y > (-?N x e)/ real c"
-      hence "y * real c > - ?N x e"
-	by (simp add: pos_divide_less_eq[OF cp, where a="y" and b="-?N x e", symmetric])
-      hence "real c * y + ?N x e > 0" by (simp add: algebra_simps)
-      hence ?case using numbound0_I[OF nb, where bs="bs" and b="x" and b'="y"] by simp}
-    moreover {assume y: "y < (- ?N x e) / real c" 
-      with ly have eu: "l < (- ?N x e) / real c" by auto
-      with noSc ly yu have "(- ?N x e) / real c \<ge> u" by (cases "(- ?N x e) / real c > l", auto)
-      with xu pxc have "False" by auto
-      hence ?case by simp }
-    ultimately show ?case by blast
-next
-  case (8 c e) hence cp: "real c > 0" and nb: "numbound0 e" by simp+
-    from prems have "x * real c + ?N x e \<ge> 0" by (simp add: algebra_simps)
-    hence pxc: "x \<ge> (- ?N x e) / real c" 
-      by (simp only: pos_divide_le_eq[OF cp, where a="x" and b="-?N x e"])
-    from prems have noSc:"\<forall> t. l < t \<and> t < u \<longrightarrow> t \<noteq> (- ?N x e) / real c" by auto
-    with ly yu have yne: "y \<noteq> - ?N x e / real c" by auto
-    hence "y < (- ?N x e) / real c \<or> y > (-?N x e) / real c" by auto
-    moreover {assume y: "y > (-?N x e)/ real c"
-      hence "y * real c > - ?N x e"
-	by (simp add: pos_divide_less_eq[OF cp, where a="y" and b="-?N x e", symmetric])
-      hence "real c * y + ?N x e > 0" by (simp add: algebra_simps)
-      hence ?case using numbound0_I[OF nb, where bs="bs" and b="x" and b'="y"] by simp}
-    moreover {assume y: "y < (- ?N x e) / real c" 
-      with ly have eu: "l < (- ?N x e) / real c" by auto
-      with noSc ly yu have "(- ?N x e) / real c \<ge> u" by (cases "(- ?N x e) / real c > l", auto)
-      with xu pxc have "False" by auto
-      hence ?case by simp }
-    ultimately show ?case by blast
-next
-  case (3 c e) hence cp: "real c > 0" and nb: "numbound0 e" by simp+
-    from cp have cnz: "real c \<noteq> 0" by simp
-    from prems have "x * real c + ?N x e = 0" by (simp add: algebra_simps)
-    hence pxc: "x = (- ?N x e) / real c" 
-      by (simp only: nonzero_eq_divide_eq[OF cnz, where a="x" and b="-?N x e"])
-    from prems have noSc:"\<forall> t. l < t \<and> t < u \<longrightarrow> t \<noteq> (- ?N x e) / real c" by auto
-    with lx xu have yne: "x \<noteq> - ?N x e / real c" by auto
-    with pxc show ?case by simp
-next
-  case (4 c e) hence cp: "real c > 0" and nb: "numbound0 e" by simp+
-    from cp have cnz: "real c \<noteq> 0" by simp
-    from prems have noSc:"\<forall> t. l < t \<and> t < u \<longrightarrow> t \<noteq> (- ?N x e) / real c" by auto
-    with ly yu have yne: "y \<noteq> - ?N x e / real c" by auto
-    hence "y* real c \<noteq> -?N x e"      
-      by (simp only: nonzero_eq_divide_eq[OF cnz, where a="y" and b="-?N x e"]) simp
-    hence "y* real c + ?N x e \<noteq> 0" by (simp add: algebra_simps)
-    thus ?case using numbound0_I[OF nb, where bs="bs" and b="x" and b'="y"] 
-      by (simp add: algebra_simps)
-qed (auto simp add: nth_pos2 numbound0_I[where bs="bs" and b="y" and b'="x"])
-
-lemma finite_set_intervals:
-  assumes px: "P (x::real)" 
-  and lx: "l \<le> x" and xu: "x \<le> u"
-  and linS: "l\<in> S" and uinS: "u \<in> S"
-  and fS:"finite S" and lS: "\<forall> x\<in> S. l \<le> x" and Su: "\<forall> x\<in> S. x \<le> u"
-  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"
-proof-
-  let ?Mx = "{y. y\<in> S \<and> y \<le> x}"
-  let ?xM = "{y. y\<in> S \<and> x \<le> y}"
-  let ?a = "Max ?Mx"
-  let ?b = "Min ?xM"
-  have MxS: "?Mx \<subseteq> S" by blast
-  hence fMx: "finite ?Mx" using fS finite_subset by auto
-  from lx linS have linMx: "l \<in> ?Mx" by blast
-  hence Mxne: "?Mx \<noteq> {}" by blast
-  have xMS: "?xM \<subseteq> S" by blast
-  hence fxM: "finite ?xM" using fS finite_subset by auto
-  from xu uinS have linxM: "u \<in> ?xM" by blast
-  hence xMne: "?xM \<noteq> {}" by blast
-  have ax:"?a \<le> x" using Mxne fMx by auto
-  have xb:"x \<le> ?b" using xMne fxM by auto
-  have "?a \<in> ?Mx" using Max_in[OF fMx Mxne] by simp hence ainS: "?a \<in> S" using MxS by blast
-  have "?b \<in> ?xM" using Min_in[OF fxM xMne] by simp hence binS: "?b \<in> S" using xMS by blast
-  have noy:"\<forall> y. ?a < y \<and> y < ?b \<longrightarrow> y \<notin> S"
-  proof(clarsimp)
-    fix y
-    assume ay: "?a < y" and yb: "y < ?b" and yS: "y \<in> S"
-    from yS have "y\<in> ?Mx \<or> y\<in> ?xM" by auto
-    moreover {assume "y \<in> ?Mx" hence "y \<le> ?a" using Mxne fMx by auto with ay have "False" by simp}
-    moreover {assume "y \<in> ?xM" hence "y \<ge> ?b" using xMne fxM by auto with yb have "False" by simp}
-    ultimately show "False" by blast
-  qed
-  from ainS binS noy ax xb px show ?thesis by blast
-qed
-
-lemma finite_set_intervals2:
-  assumes px: "P (x::real)" 
-  and lx: "l \<le> x" and xu: "x \<le> u"
-  and linS: "l\<in> S" and uinS: "u \<in> S"
-  and fS:"finite S" and lS: "\<forall> x\<in> S. l \<le> x" and Su: "\<forall> x\<in> S. x \<le> u"
-  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)"
-proof-
-  from finite_set_intervals[where P="P", OF px lx xu linS uinS fS lS Su]
-  obtain a and b where 
-    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
-  from axb have "x= a \<or> x= b \<or> (a < x \<and> x < b)" by auto
-  thus ?thesis using px as bs noS by blast 
-qed
-
-lemma rinf_uset:
-  assumes lp: "isrlfm p"
-  and nmi: "\<not> (Ifm (x#bs) (minusinf p))" (is "\<not> (Ifm (x#bs) (?M p))")
-  and npi: "\<not> (Ifm (x#bs) (plusinf p))" (is "\<not> (Ifm (x#bs) (?P p))")
-  and ex: "\<exists> x.  Ifm (x#bs) p" (is "\<exists> x. ?I x p")
-  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" 
-proof-
-  let ?N = "\<lambda> x t. Inum (x#bs) t"
-  let ?U = "set (uset p)"
-  from ex obtain a where pa: "?I a p" by blast
-  from bound0_I[OF rminusinf_bound0[OF lp], where bs="bs" and b="x" and b'="a"] nmi
-  have nmi': "\<not> (?I a (?M p))" by simp
-  from bound0_I[OF rplusinf_bound0[OF lp], where bs="bs" and b="x" and b'="a"] npi
-  have npi': "\<not> (?I a (?P p))" by simp
-  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"
-  proof-
-    let ?M = "(\<lambda> (t,c). ?N a t / real c) ` ?U"
-    have fM: "finite ?M" by auto
-    from rminusinf_uset[OF lp nmi pa] rplusinf_uset[OF lp npi pa] 
-    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
-    then obtain "t" "n" "s" "m" where 
-      tnU: "(t,n) \<in> ?U" and smU: "(s,m) \<in> ?U" 
-      and xs1: "a \<le> ?N x s / real m" and tx1: "a \<ge> ?N x t / real n" by blast
-    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
-    from tnU have Mne: "?M \<noteq> {}" by auto
-    hence Une: "?U \<noteq> {}" by simp
-    let ?l = "Min ?M"
-    let ?u = "Max ?M"
-    have linM: "?l \<in> ?M" using fM Mne by simp
-    have uinM: "?u \<in> ?M" using fM Mne by simp
-    have tnM: "?N a t / real n \<in> ?M" using tnU by auto
-    have smM: "?N a s / real m \<in> ?M" using smU by auto 
-    have lM: "\<forall> t\<in> ?M. ?l \<le> t" using Mne fM by auto
-    have Mu: "\<forall> t\<in> ?M. t \<le> ?u" using Mne fM by auto
-    have "?l \<le> ?N a t / real n" using tnM Mne by simp hence lx: "?l \<le> a" using tx by simp
-    have "?N a s / real m \<le> ?u" using smM Mne by simp hence xu: "a \<le> ?u" using xs by simp
-    from finite_set_intervals2[where P="\<lambda> x. ?I x p",OF pa lx xu linM uinM fM lM Mu]
-    have "(\<exists> s\<in> ?M. ?I s p) \<or> 
-      (\<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)" .
-    moreover { fix u assume um: "u\<in> ?M" and pu: "?I u p"
-      hence "\<exists> (tu,nu) \<in> ?U. u = ?N a tu / real nu" by auto
-      then obtain "tu" "nu" where tuU: "(tu,nu) \<in> ?U" and tuu:"u= ?N a tu / real nu" by blast
-      have "(u + u) / 2 = u" by auto with pu tuu 
-      have "?I (((?N a tu / real nu) + (?N a tu / real nu)) / 2) p" by simp
-      with tuU have ?thesis by blast}
-    moreover{
-      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"
-      then obtain t1 and t2 where t1M: "t1 \<in> ?M" and t2M: "t2\<in> ?M" 
-	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"
-	by blast
-      from t1M have "\<exists> (t1u,t1n) \<in> ?U. t1 = ?N a t1u / real t1n" by auto
-      then obtain "t1u" "t1n" where t1uU: "(t1u,t1n) \<in> ?U" and t1u: "t1 = ?N a t1u / real t1n" by blast
-      from t2M have "\<exists> (t2u,t2n) \<in> ?U. t2 = ?N a t2u / real t2n" by auto
-      then obtain "t2u" "t2n" where t2uU: "(t2u,t2n) \<in> ?U" and t2u: "t2 = ?N a t2u / real t2n" by blast
-      from t1x xt2 have t1t2: "t1 < t2" by simp
-      let ?u = "(t1 + t2) / 2"
-      from less_half_sum[OF t1t2] gt_half_sum[OF t1t2] have t1lu: "t1 < ?u" and ut2: "?u < t2" by auto
-      from lin_dense[OF lp noM t1x xt2 px t1lu ut2] have "?I ?u p" .
-      with t1uU t2uU t1u t2u have ?thesis by blast}
-    ultimately show ?thesis by blast
-  qed
-  then obtain "l" "n" "s"  "m" where lnU: "(l,n) \<in> ?U" and smU:"(s,m) \<in> ?U" 
-    and pu: "?I ((?N a l / real n + ?N a s / real m) / 2) p" by blast
-  from lnU smU uset_l[OF lp] have nbl: "numbound0 l" and nbs: "numbound0 s" by auto
-  from numbound0_I[OF nbl, where bs="bs" and b="a" and b'="x"] 
-    numbound0_I[OF nbs, where bs="bs" and b="a" and b'="x"] pu
-  have "?I ((?N x l / real n + ?N x s / real m) / 2) p" by simp
-  with lnU smU
-  show ?thesis by auto
-qed
-    (* The Ferrante - Rackoff Theorem *)
-
-theorem fr_eq: 
-  assumes lp: "isrlfm p"
-  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))"
-  (is "(\<exists> x. ?I x p) = (?M \<or> ?P \<or> ?F)" is "?E = ?D")
-proof
-  assume px: "\<exists> x. ?I x p"
-  have "?M \<or> ?P \<or> (\<not> ?M \<and> \<not> ?P)" by blast
-  moreover {assume "?M \<or> ?P" hence "?D" by blast}
-  moreover {assume nmi: "\<not> ?M" and npi: "\<not> ?P"
-    from rinf_uset[OF lp nmi npi] have "?F" using px by blast hence "?D" by blast}
-  ultimately show "?D" by blast
-next
-  assume "?D" 
-  moreover {assume m:"?M" from rminusinf_ex[OF lp m] have "?E" .}
-  moreover {assume p: "?P" from rplusinf_ex[OF lp p] have "?E" . }
-  moreover {assume f:"?F" hence "?E" by blast}
-  ultimately show "?E" by blast
-qed
-
-
-lemma fr_equsubst: 
-  assumes lp: "isrlfm p"
-  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))))"
-  (is "(\<exists> x. ?I x p) = (?M \<or> ?P \<or> ?F)" is "?E = ?D")
-proof
-  assume px: "\<exists> x. ?I x p"
-  have "?M \<or> ?P \<or> (\<not> ?M \<and> \<not> ?P)" by blast
-  moreover {assume "?M \<or> ?P" hence "?D" by blast}
-  moreover {assume nmi: "\<not> ?M" and npi: "\<not> ?P"
-    let ?f ="\<lambda> (t,n). Inum (x#bs) t / real n"
-    let ?N = "\<lambda> t. Inum (x#bs) t"
-    {fix t n s m assume "(t,n)\<in> set (uset p)" and "(s,m) \<in> set (uset p)"
-      with uset_l[OF lp] have tnb: "numbound0 t" and np:"real n > 0" and snb: "numbound0 s" and mp:"real m > 0"
-	by auto
-      let ?st = "Add (Mul m t) (Mul n s)"
-      from mult_pos_pos[OF np mp] have mnp: "real (2*n*m) > 0" 
-	by (simp add: mult_commute)
-      from tnb snb have st_nb: "numbound0 ?st" by simp
-      have st: "(?N t / real n + ?N s / real m)/2 = ?N ?st / real (2*n*m)"
-	using mnp mp np by (simp add: algebra_simps add_divide_distrib)
-      from usubst_I[OF lp mnp st_nb, where x="x" and bs="bs"] 
-      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])}
-    with rinf_uset[OF lp nmi npi px] have "?F" by blast hence "?D" by blast}
-  ultimately show "?D" by blast
-next
-  assume "?D" 
-  moreover {assume m:"?M" from rminusinf_ex[OF lp m] have "?E" .}
-  moreover {assume p: "?P" from rplusinf_ex[OF lp p] have "?E" . }
-  moreover {fix t k s l assume "(t,k) \<in> set (uset p)" and "(s,l) \<in> set (uset p)" 
-    and px:"?I x (usubst p (Add (Mul l t) (Mul k s), 2*k*l))"
-    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
-    let ?st = "Add (Mul l t) (Mul k s)"
-    from mult_pos_pos[OF np mp] have mnp: "real (2*k*l) > 0" 
-      by (simp add: mult_commute)
-    from tnb snb have st_nb: "numbound0 ?st" by simp
-    from usubst_I[OF lp mnp st_nb, where bs="bs"] px have "?E" by auto}
-  ultimately show "?E" by blast
-qed
-
-
-    (* Implement the right hand side of Ferrante and Rackoff's Theorem. *)
-constdefs ferrack:: "fm \<Rightarrow> fm"
-  "ferrack p \<equiv> (let p' = rlfm (simpfm p); mp = minusinf p'; pp = plusinf p'
-                in if (mp = T \<or> pp = T) then T else 
-                   (let U = remdps(map simp_num_pair 
-                     (map (\<lambda> ((t,n),(s,m)). (Add (Mul m t) (Mul n s) , 2*n*m))
-                           (alluopairs (uset p')))) 
-                    in decr (disj mp (disj pp (evaldjf (simpfm o (usubst p')) U)))))"
-
-lemma uset_cong_aux:
-  assumes Ul: "\<forall> (t,n) \<in> set U. numbound0 t \<and> n >0"
-  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))"
-  (is "?lhs = ?rhs")
-proof(auto)
-  fix t n s m
-  assume "((t,n),(s,m)) \<in> set (alluopairs U)"
-  hence th: "((t,n),(s,m)) \<in> (set U \<times> set U)"
-    using alluopairs_set1[where xs="U"] by blast
-  let ?N = "\<lambda> t. Inum (x#bs) t"
-  let ?st= "Add (Mul m t) (Mul n s)"
-  from Ul th have mnz: "m \<noteq> 0" by auto
-  from Ul th have  nnz: "n \<noteq> 0" by auto  
-  have st: "(?N t / real n + ?N s / real m)/2 = ?N ?st / real (2*n*m)"
-   using mnz nnz by (simp add: algebra_simps add_divide_distrib)
- 
-  thus "(real m *  Inum (x # bs) t + real n * Inum (x # bs) s) /
-       (2 * real n * real m)
-       \<in> (\<lambda>((t, n), s, m).
-             (Inum (x # bs) t / real n + Inum (x # bs) s / real m) / 2) `
-         (set U \<times> set U)"using mnz nnz th  
-    apply (auto simp add: th add_divide_distrib algebra_simps split_def image_def)
-    by (rule_tac x="(s,m)" in bexI,simp_all) 
-  (rule_tac x="(t,n)" in bexI,simp_all)
-next
-  fix t n s m
-  assume tnU: "(t,n) \<in> set U" and smU:"(s,m) \<in> set U" 
-  let ?N = "\<lambda> t. Inum (x#bs) t"
-  let ?st= "Add (Mul m t) (Mul n s)"
-  from Ul smU have mnz: "m \<noteq> 0" by auto
-  from Ul tnU have  nnz: "n \<noteq> 0" by auto  
-  have st: "(?N t / real n + ?N s / real m)/2 = ?N ?st / real (2*n*m)"
-   using mnz nnz by (simp add: algebra_simps add_divide_distrib)
- 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"
- have Pc:"\<forall> a b. ?P a b = ?P b a"
-   by auto
- from Ul alluopairs_set1 have Up:"\<forall> ((t,n),(s,m)) \<in> set (alluopairs U). n \<noteq> 0 \<and> m \<noteq> 0" by blast
- from alluopairs_ex[OF Pc, where xs="U"] tnU smU
- have th':"\<exists> ((t',n'),(s',m')) \<in> set (alluopairs U). ?P (t',n') (s',m')"
-   by blast
- then obtain t' n' s' m' where ts'_U: "((t',n'),(s',m')) \<in> set (alluopairs U)" 
-   and Pts': "?P (t',n') (s',m')" by blast
- from ts'_U Up have mnz': "m' \<noteq> 0" and nnz': "n'\<noteq> 0" by auto
- let ?st' = "Add (Mul m' t') (Mul n' s')"
-   have st': "(?N t' / real n' + ?N s' / real m')/2 = ?N ?st' / real (2*n'*m')"
-   using mnz' nnz' by (simp add: algebra_simps add_divide_distrib)
- from Pts' have 
-   "(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
- 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')
- finally show "(Inum (x # bs) t / real n + Inum (x # bs) s / real m) / 2
-          \<in> (\<lambda>(t, n). Inum (x # bs) t / real n) `
-            (\<lambda>((t, n), s, m). (Add (Mul m t) (Mul n s), 2 * n * m)) `
-            set (alluopairs U)"
-   using ts'_U by blast
-qed
-
-lemma uset_cong:
-  assumes lp: "isrlfm p"
-  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)")
-  and U: "\<forall> (t,n) \<in> U. numbound0 t \<and> n > 0"
-  and U': "\<forall> (t,n) \<in> U'. numbound0 t \<and> n > 0"
-  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)))"
-  (is "?lhs = ?rhs")
-proof
-  assume ?lhs
-  then obtain t n s m where tnU: "(t,n) \<in> U" and smU:"(s,m) \<in> U" and 
-    Pst: "Ifm (x#bs) (usubst p (Add (Mul m t) (Mul n s),2*n*m))" by blast
-  let ?N = "\<lambda> t. Inum (x#bs) t"
-  from tnU smU U have tnb: "numbound0 t" and np: "n > 0" 
-    and snb: "numbound0 s" and mp:"m > 0"  by auto
-  let ?st= "Add (Mul m t) (Mul n s)"
-  from mult_pos_pos[OF np mp] have mnp: "real (2*n*m) > 0" 
-      by (simp add: mult_commute real_of_int_mult[symmetric] del: real_of_int_mult)
-    from tnb snb have stnb: "numbound0 ?st" by simp
-  have st: "(?N t / real n + ?N s / real m)/2 = ?N ?st / real (2*n*m)"
-   using mp np by (simp add: algebra_simps add_divide_distrib)
-  from tnU smU UU' have "?g ((t,n),(s,m)) \<in> ?f ` U'" by blast
-  hence "\<exists> (t',n') \<in> U'. ?g ((t,n),(s,m)) = ?f (t',n')"
-    by auto (rule_tac x="(a,b)" in bexI, auto)
-  then obtain t' n' where tnU': "(t',n') \<in> U'" and th: "?g ((t,n),(s,m)) = ?f (t',n')" by blast
-  from U' tnU' have tnb': "numbound0 t'" and np': "real n' > 0" by auto
-  from usubst_I[OF lp mnp stnb, where bs="bs" and x="x"] Pst 
-  have Pst2: "Ifm (Inum (x # bs) (Add (Mul m t) (Mul n s)) / real (2 * n * m) # bs) p" by simp
-  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]]
-  have "Ifm (x # bs) (usubst p (t', n')) " by (simp only: st) 
-  then show ?rhs using tnU' by auto 
-next
-  assume ?rhs
-  then obtain t' n' where tnU': "(t',n') \<in> U'" and Pt': "Ifm (x # bs) (usubst p (t', n'))" 
-    by blast
-  from tnU' UU' have "?f (t',n') \<in> ?g ` (U\<times>U)" by blast
-  hence "\<exists> ((t,n),(s,m)) \<in> (U\<times>U). ?f (t',n') = ?g ((t,n),(s,m))" 
-    by auto (rule_tac x="(a,b)" in bexI, auto)
-  then obtain t n s m where tnU: "(t,n) \<in> U" and smU:"(s,m) \<in> U" and 
-    th: "?f (t',n') = ?g((t,n),(s,m)) "by blast
-    let ?N = "\<lambda> t. Inum (x#bs) t"
-  from tnU smU U have tnb: "numbound0 t" and np: "n > 0" 
-    and snb: "numbound0 s" and mp:"m > 0"  by auto
-  let ?st= "Add (Mul m t) (Mul n s)"
-  from mult_pos_pos[OF np mp] have mnp: "real (2*n*m) > 0" 
-      by (simp add: mult_commute real_of_int_mult[symmetric] del: real_of_int_mult)
-    from tnb snb have stnb: "numbound0 ?st" by simp
-  have st: "(?N t / real n + ?N s / real m)/2 = ?N ?st / real (2*n*m)"
-   using mp np by (simp add: algebra_simps add_divide_distrib)
-  from U' tnU' have tnb': "numbound0 t'" and np': "real n' > 0" by auto
-  from usubst_I[OF lp np' tnb', where bs="bs" and x="x",simplified th[simplified split_def fst_conv snd_conv] st] Pt'
-  have Pst2: "Ifm (Inum (x # bs) (Add (Mul m t) (Mul n s)) / real (2 * n * m) # bs) p" by simp
-  with usubst_I[OF lp mnp stnb, where x="x" and bs="bs"] tnU smU show ?lhs by blast
-qed
-
-lemma ferrack: 
-  assumes qf: "qfree p"
-  shows "qfree (ferrack p) \<and> ((Ifm bs (ferrack p)) = (\<exists> x. Ifm (x#bs) p))"
-  (is "_ \<and> (?rhs = ?lhs)")
-proof-
-  let ?I = "\<lambda> x p. Ifm (x#bs) p"
-  fix x
-  let ?N = "\<lambda> t. Inum (x#bs) t"
-  let ?q = "rlfm (simpfm p)" 
-  let ?U = "uset ?q"
-  let ?Up = "alluopairs ?U"
-  let ?g = "\<lambda> ((t,n),(s,m)). (Add (Mul m t) (Mul n s) , 2*n*m)"
-  let ?S = "map ?g ?Up"
-  let ?SS = "map simp_num_pair ?S"
-  let ?Y = "remdps ?SS"
-  let ?f= "(\<lambda> (t,n). ?N t / real n)"
-  let ?h = "\<lambda> ((t,n),(s,m)). (?N t/real n + ?N s/ real m) /2"
-  let ?F = "\<lambda> p. \<exists> a \<in> set (uset p). \<exists> b \<in> set (uset p). ?I x (usubst p (?g(a,b)))"
-  let ?ep = "evaldjf (simpfm o (usubst ?q)) ?Y"
-  from rlfm_I[OF simpfm_qf[OF qf]] have lq: "isrlfm ?q" by blast
-  from alluopairs_set1[where xs="?U"] have UpU: "set ?Up \<le> (set ?U \<times> set ?U)" by simp
-  from uset_l[OF lq] have U_l: "\<forall> (t,n) \<in> set ?U. numbound0 t \<and> n > 0" .
-  from U_l UpU 
-  have "\<forall> ((t,n),(s,m)) \<in> set ?Up. numbound0 t \<and> n> 0 \<and> numbound0 s \<and> m > 0" by auto
-  hence Snb: "\<forall> (t,n) \<in> set ?S. numbound0 t \<and> n > 0 "
-    by (auto simp add: mult_pos_pos)
-  have Y_l: "\<forall> (t,n) \<in> set ?Y. numbound0 t \<and> n > 0" 
-  proof-
-    { fix t n assume tnY: "(t,n) \<in> set ?Y" 
-      hence "(t,n) \<in> set ?SS" by simp
-      hence "\<exists> (t',n') \<in> set ?S. simp_num_pair (t',n') = (t,n)"
-	by (auto simp add: split_def) (rule_tac x="((aa,ba),(ab,bb))" in bexI, simp_all)
-      then obtain t' n' where tn'S: "(t',n') \<in> set ?S" and tns: "simp_num_pair (t',n') = (t,n)" by blast
-      from tn'S Snb have tnb: "numbound0 t'" and np: "n' > 0" by auto
-      from simp_num_pair_l[OF tnb np tns]
-      have "numbound0 t \<and> n > 0" . }
-    thus ?thesis by blast
-  qed
-
-  have YU: "(?f ` set ?Y) = (?h ` (set ?U \<times> set ?U))"
-  proof-
-     from simp_num_pair_ci[where bs="x#bs"] have 
-    "\<forall>x. (?f o simp_num_pair) x = ?f x" by auto
-     hence th: "?f o simp_num_pair = ?f" using ext by blast
-    have "(?f ` set ?Y) = ((?f o simp_num_pair) ` set ?S)" by (simp add: image_compose)
-    also have "\<dots> = (?f ` set ?S)" by (simp add: th)
-    also have "\<dots> = ((?f o ?g) ` set ?Up)" 
-      by (simp only: set_map o_def image_compose[symmetric])
-    also have "\<dots> = (?h ` (set ?U \<times> set ?U))"
-      using uset_cong_aux[OF U_l, where x="x" and bs="bs", simplified set_map image_compose[symmetric]] by blast
-    finally show ?thesis .
-  qed
-  have "\<forall> (t,n) \<in> set ?Y. bound0 (simpfm (usubst ?q (t,n)))"
-  proof-
-    { fix t n assume tnY: "(t,n) \<in> set ?Y"
-      with Y_l have tnb: "numbound0 t" and np: "real n > 0" by auto
-      from usubst_I[OF lq np tnb]
-    have "bound0 (usubst ?q (t,n))"  by simp hence "bound0 (simpfm (usubst ?q (t,n)))" 
-      using simpfm_bound0 by simp}
-    thus ?thesis by blast
-  qed
-  hence ep_nb: "bound0 ?ep"  using evaldjf_bound0[where xs="?Y" and f="simpfm o (usubst ?q)"] by auto
-  let ?mp = "minusinf ?q"
-  let ?pp = "plusinf ?q"
-  let ?M = "?I x ?mp"
-  let ?P = "?I x ?pp"
-  let ?res = "disj ?mp (disj ?pp ?ep)"
-  from rminusinf_bound0[OF lq] rplusinf_bound0[OF lq] ep_nb
-  have nbth: "bound0 ?res" by auto
-
-  from conjunct1[OF rlfm_I[OF simpfm_qf[OF qf]]] simpfm  
-
-  have th: "?lhs = (\<exists> x. ?I x ?q)" by auto 
-  from th fr_equsubst[OF lq, where bs="bs" and x="x"] have lhfr: "?lhs = (?M \<or> ?P \<or> ?F ?q)"
-    by (simp only: split_def fst_conv snd_conv)
-  also have "\<dots> = (?M \<or> ?P \<or> (\<exists> (t,n) \<in> set ?Y. ?I x (simpfm (usubst ?q (t,n)))))" 
-    using uset_cong[OF lq YU U_l Y_l]  by (simp only: split_def fst_conv snd_conv simpfm) 
-  also have "\<dots> = (Ifm (x#bs) ?res)"
-    using evaldjf_ex[where ps="?Y" and bs = "x#bs" and f="simpfm o (usubst ?q)",symmetric]
-    by (simp add: split_def pair_collapse)
-  finally have lheq: "?lhs =  (Ifm bs (decr ?res))" using decr[OF nbth] by blast
-  hence lr: "?lhs = ?rhs" apply (unfold ferrack_def Let_def)
-    by (cases "?mp = T \<or> ?pp = T", auto) (simp add: disj_def)+
-  from decr_qf[OF nbth] have "qfree (ferrack p)" by (auto simp add: Let_def ferrack_def)
-  with lr show ?thesis by blast
-qed
-
-definition linrqe:: "fm \<Rightarrow> fm" where
-  "linrqe p = qelim (prep p) ferrack"
-
-theorem linrqe: "Ifm bs (linrqe p) = Ifm bs p \<and> qfree (linrqe p)"
-using ferrack qelim_ci prep
-unfolding linrqe_def by auto
-
-definition ferrack_test :: "unit \<Rightarrow> fm" where
-  "ferrack_test u = linrqe (A (A (Imp (Lt (Sub (Bound 1) (Bound 0)))
-    (E (Eq (Sub (Add (Bound 0) (Bound 2)) (Bound 1)))))))"
-
-ML {* @{code ferrack_test} () *}
-
-oracle linr_oracle = {*
-let
-
-fun num_of_term vs (t as Free (xn, xT)) = (case AList.lookup (op =) vs t
-     of NONE => error "Variable not found in the list!"
-      | SOME n => @{code Bound} n)
-  | num_of_term vs @{term "real (0::int)"} = @{code C} 0
-  | num_of_term vs @{term "real (1::int)"} = @{code C} 1
-  | num_of_term vs @{term "0::real"} = @{code C} 0
-  | num_of_term vs @{term "1::real"} = @{code C} 1
-  | num_of_term vs (Bound i) = @{code Bound} i
-  | num_of_term vs (@{term "uminus :: real \<Rightarrow> real"} $ t') = @{code Neg} (num_of_term vs t')
-  | num_of_term vs (@{term "op + :: real \<Rightarrow> real \<Rightarrow> real"} $ t1 $ t2) = @{code Add} (num_of_term vs t1, num_of_term vs t2)
-  | num_of_term vs (@{term "op - :: real \<Rightarrow> real \<Rightarrow> real"} $ t1 $ t2) = @{code Sub} (num_of_term vs t1, num_of_term vs t2)
-  | num_of_term vs (@{term "op * :: real \<Rightarrow> real \<Rightarrow> real"} $ t1 $ t2) = (case (num_of_term vs t1)
-     of @{code C} i => @{code Mul} (i, num_of_term vs t2)
-      | _ => error "num_of_term: unsupported Multiplication")
-  | num_of_term vs (@{term "real :: int \<Rightarrow> real"} $ (@{term "number_of :: int \<Rightarrow> int"} $ t')) = @{code C} (HOLogic.dest_numeral t')
-  | num_of_term vs (@{term "number_of :: int \<Rightarrow> real"} $ t') = @{code C} (HOLogic.dest_numeral t')
-  | num_of_term vs t = error ("num_of_term: unknown term " ^ Syntax.string_of_term @{context} t);
-
-fun fm_of_term vs @{term True} = @{code T}
-  | fm_of_term vs @{term False} = @{code F}
-  | fm_of_term vs (@{term "op < :: real \<Rightarrow> real \<Rightarrow> bool"} $ t1 $ t2) = @{code Lt} (@{code Sub} (num_of_term vs t1, num_of_term vs t2))
-  | fm_of_term vs (@{term "op \<le> :: real \<Rightarrow> real \<Rightarrow> bool"} $ t1 $ t2) = @{code Le} (@{code Sub} (num_of_term vs t1, num_of_term vs t2))
-  | fm_of_term vs (@{term "op = :: real \<Rightarrow> real \<Rightarrow> bool"} $ t1 $ t2) = @{code Eq} (@{code Sub} (num_of_term vs t1, num_of_term vs t2)) 
-  | fm_of_term vs (@{term "op \<longleftrightarrow> :: bool \<Rightarrow> bool \<Rightarrow> bool"} $ t1 $ t2) = @{code Iff} (fm_of_term vs t1, fm_of_term vs t2)
-  | fm_of_term vs (@{term "op &"} $ t1 $ t2) = @{code And} (fm_of_term vs t1, fm_of_term vs t2)
-  | fm_of_term vs (@{term "op |"} $ t1 $ t2) = @{code Or} (fm_of_term vs t1, fm_of_term vs t2)
-  | fm_of_term vs (@{term "op -->"} $ t1 $ t2) = @{code Imp} (fm_of_term vs t1, fm_of_term vs t2)
-  | fm_of_term vs (@{term "Not"} $ t') = @{code NOT} (fm_of_term vs t')
-  | fm_of_term vs (Const ("Ex", _) $ Abs (xn, xT, p)) =
-      @{code E} (fm_of_term (map (fn (v, n) => (v, n + 1)) vs) p)
-  | fm_of_term vs (Const ("All", _) $ Abs (xn, xT, p)) =
-      @{code A} (fm_of_term (map (fn (v, n) => (v, n + 1)) vs) p)
-  | fm_of_term vs t = error ("fm_of_term : unknown term " ^ Syntax.string_of_term @{context} t);
-
-fun term_of_num vs (@{code C} i) = @{term "real :: int \<Rightarrow> real"} $ HOLogic.mk_number HOLogic.intT i
-  | term_of_num vs (@{code Bound} n) = fst (the (find_first (fn (_, m) => n = m) vs))
-  | term_of_num vs (@{code Neg} t') = @{term "uminus :: real \<Rightarrow> real"} $ term_of_num vs t'
-  | term_of_num vs (@{code Add} (t1, t2)) = @{term "op + :: real \<Rightarrow> real \<Rightarrow> real"} $
-      term_of_num vs t1 $ term_of_num vs t2
-  | term_of_num vs (@{code Sub} (t1, t2)) = @{term "op - :: real \<Rightarrow> real \<Rightarrow> real"} $
-      term_of_num vs t1 $ term_of_num vs t2
-  | term_of_num vs (@{code Mul} (i, t2)) = @{term "op * :: real \<Rightarrow> real \<Rightarrow> real"} $
-      term_of_num vs (@{code C} i) $ term_of_num vs t2
-  | term_of_num vs (@{code CN} (n, i, t)) = term_of_num vs (@{code Add} (@{code Mul} (i, @{code Bound} n), t));
-
-fun term_of_fm vs @{code T} = HOLogic.true_const 
-  | term_of_fm vs @{code F} = HOLogic.false_const
-  | term_of_fm vs (@{code Lt} t) = @{term "op < :: real \<Rightarrow> real \<Rightarrow> bool"} $
-      term_of_num vs t $ @{term "0::real"}
-  | term_of_fm vs (@{code Le} t) = @{term "op \<le> :: real \<Rightarrow> real \<Rightarrow> bool"} $
-      term_of_num vs t $ @{term "0::real"}
-  | term_of_fm vs (@{code Gt} t) = @{term "op < :: real \<Rightarrow> real \<Rightarrow> bool"} $
-      @{term "0::real"} $ term_of_num vs t
-  | term_of_fm vs (@{code Ge} t) = @{term "op \<le> :: real \<Rightarrow> real \<Rightarrow> bool"} $
-      @{term "0::real"} $ term_of_num vs t
-  | term_of_fm vs (@{code Eq} t) = @{term "op = :: real \<Rightarrow> real \<Rightarrow> bool"} $
-      term_of_num vs t $ @{term "0::real"}
-  | term_of_fm vs (@{code NEq} t) = term_of_fm vs (@{code NOT} (@{code Eq} t))
-  | term_of_fm vs (@{code NOT} t') = HOLogic.Not $ term_of_fm vs t'
-  | term_of_fm vs (@{code And} (t1, t2)) = HOLogic.conj $ term_of_fm vs t1 $ term_of_fm vs t2
-  | term_of_fm vs (@{code Or} (t1, t2)) = HOLogic.disj $ term_of_fm vs t1 $ term_of_fm vs t2
-  | term_of_fm vs (@{code Imp}  (t1, t2)) = HOLogic.imp $ term_of_fm vs t1 $ term_of_fm vs t2
-  | term_of_fm vs (@{code Iff} (t1, t2)) = @{term "op \<longleftrightarrow> :: bool \<Rightarrow> bool \<Rightarrow> bool"} $
-      term_of_fm vs t1 $ term_of_fm vs t2
-  | term_of_fm vs _ = error "If this is raised, Isabelle/HOL or generate_code is inconsistent.";
-
-in fn ct =>
-  let 
-    val thy = Thm.theory_of_cterm ct;
-    val t = Thm.term_of ct;
-    val fs = OldTerm.term_frees t;
-    val vs = fs ~~ (0 upto (length fs - 1));
-    val res = HOLogic.mk_Trueprop (HOLogic.mk_eq (t, term_of_fm vs (@{code linrqe} (fm_of_term vs t))));
-  in Thm.cterm_of thy res end
-end;
-*}
-
-use "ferrack_tac.ML"
-setup Ferrack_Tac.setup
-
-lemma
-  fixes x :: real
-  shows "2 * x \<le> 2 * x \<and> 2 * x \<le> 2 * x + 1"
-apply rferrack
-done
-
-lemma
-  fixes x :: real
-  shows "\<exists>y \<le> x. x = y + 1"
-apply rferrack
-done
-
-lemma
-  fixes x :: real
-  shows "\<not> (\<exists>z. x + z = x + z + 1)"
-apply rferrack
-done
-
-end
--- a/src/HOL/Reflection/MIR.thy	Mon Feb 16 10:13:30 2009 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,5933 +0,0 @@
-(*  Title:      HOL/Reflection/MIR.thy
-    Author:     Amine Chaieb
-*)
-
-theory MIR
-imports Complex_Main Dense_Linear_Order Efficient_Nat
-uses ("mir_tac.ML")
-begin
-
-section {* Quantifier elimination for @{text "\<real> (0, 1, +, floor, <)"} *}
-
-declare real_of_int_floor_cancel [simp del]
-
-primrec alluopairs:: "'a list \<Rightarrow> ('a \<times> 'a) list" where 
-  "alluopairs [] = []"
-| "alluopairs (x#xs) = (map (Pair x) (x#xs))@(alluopairs xs)"
-
-lemma alluopairs_set1: "set (alluopairs xs) \<le> {(x,y). x\<in> set xs \<and> y\<in> set xs}"
-by (induct xs, auto)
-
-lemma alluopairs_set:
-  "\<lbrakk>x\<in> set xs ; y \<in> set xs\<rbrakk> \<Longrightarrow> (x,y) \<in> set (alluopairs xs) \<or> (y,x) \<in> set (alluopairs xs) "
-by (induct xs, auto)
-
-lemma alluopairs_ex:
-  assumes Pc: "\<forall> x y. P x y = P y x"
-  shows "(\<exists> x \<in> set xs. \<exists> y \<in> set xs. P x y) = (\<exists> (x,y) \<in> set (alluopairs xs). P x y)"
-proof
-  assume "\<exists>x\<in>set xs. \<exists>y\<in>set xs. P x y"
-  then obtain x y where x: "x \<in> set xs" and y:"y \<in> set xs" and P: "P x y"  by blast
-  from alluopairs_set[OF x y] P Pc show"\<exists>(x, y)\<in>set (alluopairs xs). P x y" 
-    by auto
-next
-  assume "\<exists>(x, y)\<in>set (alluopairs xs). P x y"
-  then obtain "x" and "y"  where xy:"(x,y) \<in> set (alluopairs xs)" and P: "P x y" by blast+
-  from xy have "x \<in> set xs \<and> y\<in> set xs" using alluopairs_set1 by blast
-  with P show "\<exists>x\<in>set xs. \<exists>y\<in>set xs. P x y" by blast
-qed
-
-  (* generate a list from i to j*)
-consts iupt :: "int \<times> int \<Rightarrow> int list"
-recdef iupt "measure (\<lambda> (i,j). nat (j-i +1))" 
-  "iupt (i,j) = (if j <i then [] else (i# iupt(i+1, j)))"
-
-lemma iupt_set: "set (iupt(i,j)) = {i .. j}"
-proof(induct rule: iupt.induct)
-  case (1 a b)
-  show ?case
-    using prems by (simp add: simp_from_to)
-qed
-
-lemma nth_pos2: "0 < n \<Longrightarrow> (x#xs) ! n = xs ! (n - 1)"
-using Nat.gr0_conv_Suc
-by clarsimp
-
-
-lemma myl: "\<forall> (a::'a::{pordered_ab_group_add}) (b::'a). (a \<le> b) = (0 \<le> b - a)" 
-proof(clarify)
-  fix x y ::"'a"
-  have "(x \<le> y) = (x - y \<le> 0)" by (simp only: le_iff_diff_le_0[where a="x" and b="y"])
-  also have "\<dots> = (- (y - x) \<le> 0)" by simp
-  also have "\<dots> = (0 \<le> y - x)" by (simp only: neg_le_0_iff_le[where a="y-x"])
-  finally show "(x \<le> y) = (0 \<le> y - x)" .
-qed
-
-lemma myless: "\<forall> (a::'a::{pordered_ab_group_add}) (b::'a). (a < b) = (0 < b - a)" 
-proof(clarify)
-  fix x y ::"'a"
-  have "(x < y) = (x - y < 0)" by (simp only: less_iff_diff_less_0[where a="x" and b="y"])
-  also have "\<dots> = (- (y - x) < 0)" by simp
-  also have "\<dots> = (0 < y - x)" by (simp only: neg_less_0_iff_less[where a="y-x"])
-  finally show "(x < y) = (0 < y - x)" .
-qed
-
-lemma myeq: "\<forall> (a::'a::{pordered_ab_group_add}) (b::'a). (a = b) = (0 = b - a)"
-  by auto
-
-  (* Maybe should be added to the library \<dots> *)
-lemma floor_int_eq: "(real n\<le> x \<and> x < real (n+1)) = (floor x = n)"
-proof( auto)
-  assume lb: "real n \<le> x"
-    and ub: "x < real n + 1"
-  have "real (floor x) \<le> x" by simp 
-  hence "real (floor x) < real (n + 1) " using ub by arith
-  hence "floor x < n+1" by simp
-  moreover from lb have "n \<le> floor x" using floor_mono2[where x="real n" and y="x"] 
-    by simp ultimately show "floor x = n" by simp
-qed
-
-(* Periodicity of dvd *)
-lemma dvd_period:
-  assumes advdd: "(a::int) dvd d"
-  shows "(a dvd (x + t)) = (a dvd ((x+ c*d) + t))"
-  using advdd  
-proof-
-  {fix x k
-    from inf_period(3)[OF advdd, rule_format, where x=x and k="-k"]  
-    have " ((a::int) dvd (x + t)) = (a dvd (x+k*d + t))" by simp}
-  hence "\<forall>x.\<forall>k. ((a::int) dvd (x + t)) = (a dvd (x+k*d + t))"  by simp
-  then show ?thesis by simp
-qed
-
-  (* The Divisibility relation between reals *)	
-definition
-  rdvd:: "real \<Rightarrow> real \<Rightarrow> bool" (infixl "rdvd" 50)
-where
-  rdvd_def: "x rdvd y \<longleftrightarrow> (\<exists>k\<Colon>int. y = x * real k)"
-
-lemma int_rdvd_real: 
-  shows "real (i::int) rdvd x = (i dvd (floor x) \<and> real (floor x) = x)" (is "?l = ?r")
-proof
-  assume "?l" 
-  hence th: "\<exists> k. x=real (i*k)" by (simp add: rdvd_def)
-  hence th': "real (floor x) = x" by (auto simp del: real_of_int_mult)
-  with th have "\<exists> k. real (floor x) = real (i*k)" by simp
-  hence "\<exists> k. floor x = i*k" by (simp only: real_of_int_inject)
-  thus ?r  using th' by (simp add: dvd_def) 
-next
-  assume "?r" hence "(i\<Colon>int) dvd \<lfloor>x\<Colon>real\<rfloor>" ..
-  hence "\<exists> k. real (floor x) = real (i*k)" 
-    by (simp only: real_of_int_inject) (simp add: dvd_def)
-  thus ?l using prems by (simp add: rdvd_def)
-qed
-
-lemma int_rdvd_iff: "(real (i::int) rdvd real t) = (i dvd t)"
-by (auto simp add: rdvd_def dvd_def) (rule_tac x="k" in exI, simp only :real_of_int_mult[symmetric])
-
-
-lemma rdvd_abs1: 
-  "(abs (real d) rdvd t) = (real (d ::int) rdvd t)"
-proof
-  assume d: "real d rdvd t"
-  from d int_rdvd_real have d2: "d dvd (floor t)" and ti: "real (floor t) = t" by auto
-
-  from iffD2[OF zdvd_abs1] d2 have "(abs d) dvd (floor t)" by blast
-  with ti int_rdvd_real[symmetric] have "real (abs d) rdvd t" by blast 
-  thus "abs (real d) rdvd t" by simp
-next
-  assume "abs (real d) rdvd t" hence "real (abs d) rdvd t" by simp
-  with int_rdvd_real[where i="abs d" and x="t"] have d2: "abs d dvd floor t" and ti: "real (floor t) =t" by auto
-  from iffD1[OF zdvd_abs1] d2 have "d dvd floor t" by blast
-  with ti int_rdvd_real[symmetric] show "real d rdvd t" by blast
-qed
-
-lemma rdvd_minus: "(real (d::int) rdvd t) = (real d rdvd -t)"
-  apply (auto simp add: rdvd_def)
-  apply (rule_tac x="-k" in exI, simp) 
-  apply (rule_tac x="-k" in exI, simp)
-done
-
-lemma rdvd_left_0_eq: "(0 rdvd t) = (t=0)"
-by (auto simp add: rdvd_def)
-
-lemma rdvd_mult: 
-  assumes knz: "k\<noteq>0"
-  shows "(real (n::int) * real (k::int) rdvd x * real k) = (real n rdvd x)"
-using knz by (simp add:rdvd_def)
-
-lemma rdvd_trans: assumes mn:"m rdvd n" and  nk:"n rdvd k" 
-  shows "m rdvd k"
-proof-
-  from rdvd_def mn obtain c where nmc:"n = m * real (c::int)" by auto
-  from rdvd_def nk obtain c' where nkc:"k = n * real (c'::int)" by auto
-  hence "k = m * real (c * c')" using nmc by simp
-  thus ?thesis using rdvd_def by blast
-qed
-
-  (*********************************************************************************)
-  (****                            SHADOW SYNTAX AND SEMANTICS                  ****)
-  (*********************************************************************************)
-
-datatype num = C int | Bound nat | CN nat int num | Neg num | Add num num| Sub num num 
-  | Mul int num | Floor num| CF int num num
-
-  (* A size for num to make inductive proofs simpler*)
-primrec num_size :: "num \<Rightarrow> nat" where
- "num_size (C c) = 1"
-| "num_size (Bound n) = 1"
-| "num_size (Neg a) = 1 + num_size a"
-| "num_size (Add a b) = 1 + num_size a + num_size b"
-| "num_size (Sub a b) = 3 + num_size a + num_size b"
-| "num_size (CN n c a) = 4 + num_size a "
-| "num_size (CF c a b) = 4 + num_size a + num_size b"
-| "num_size (Mul c a) = 1 + num_size a"
-| "num_size (Floor a) = 1 + num_size a"
-
-  (* Semantics of numeral terms (num) *)
-primrec Inum :: "real list \<Rightarrow> num \<Rightarrow> real" where
-  "Inum bs (C c) = (real c)"
-| "Inum bs (Bound n) = bs!n"
-| "Inum bs (CN n c a) = (real c) * (bs!n) + (Inum bs a)"
-| "Inum bs (Neg a) = -(Inum bs a)"
-| "Inum bs (Add a b) = Inum bs a + Inum bs b"
-| "Inum bs (Sub a b) = Inum bs a - Inum bs b"
-| "Inum bs (Mul c a) = (real c) * Inum bs a"
-| "Inum bs (Floor a) = real (floor (Inum bs a))"
-| "Inum bs (CF c a b) = real c * real (floor (Inum bs a)) + Inum bs b"
-definition "isint t bs \<equiv> real (floor (Inum bs t)) = Inum bs t"
-
-lemma isint_iff: "isint n bs = (real (floor (Inum bs n)) = Inum bs n)"
-by (simp add: isint_def)
-
-lemma isint_Floor: "isint (Floor n) bs"
-  by (simp add: isint_iff)
-
-lemma isint_Mul: "isint e bs \<Longrightarrow> isint (Mul c e) bs"
-proof-
-  let ?e = "Inum bs e"
-  let ?fe = "floor ?e"
-  assume be: "isint e bs" hence efe:"real ?fe = ?e" by (simp add: isint_iff)
-  have "real ((floor (Inum bs (Mul c e)))) = real (floor (real (c * ?fe)))" using efe by simp
-  also have "\<dots> = real (c* ?fe)" by (simp only: floor_real_of_int) 
-  also have "\<dots> = real c * ?e" using efe by simp
-  finally show ?thesis using isint_iff by simp
-qed
-
-lemma isint_neg: "isint e bs \<Longrightarrow> isint (Neg e) bs"
-proof-
-  let ?I = "\<lambda> t. Inum bs t"
-  assume ie: "isint e bs"
-  hence th: "real (floor (?I e)) = ?I e" by (simp add: isint_def)  
-  have "real (floor (?I (Neg e))) = real (floor (- (real (floor (?I e)))))" by (simp add: th)
-  also have "\<dots> = - real (floor (?I e))" by(simp add: floor_minus_real_of_int) 
-  finally show "isint (Neg e) bs" by (simp add: isint_def th)
-qed
-
-lemma isint_sub: 
-  assumes ie: "isint e bs" shows "isint (Sub (C c) e) bs"
-proof-
-  let ?I = "\<lambda> t. Inum bs t"
-  from ie have th: "real (floor (?I e)) = ?I e" by (simp add: isint_def)  
-  have "real (floor (?I (Sub (C c) e))) = real (floor ((real (c -floor (?I e)))))" by (simp add: th)
-  also have "\<dots> = real (c- floor (?I e))" by(simp add: floor_minus_real_of_int) 
-  finally show "isint (Sub (C c) e) bs" by (simp add: isint_def th)
-qed
-
-lemma isint_add: assumes
-  ai:"isint a bs" and bi: "isint b bs" shows "isint (Add a b) bs"
-proof-
-  let ?a = "Inum bs a"
-  let ?b = "Inum bs b"
-  from ai bi isint_iff have "real (floor (?a + ?b)) = real (floor (real (floor ?a) + real (floor ?b)))" by simp
-  also have "\<dots> = real (floor ?a) + real (floor ?b)" by simp
-  also have "\<dots> = ?a + ?b" using ai bi isint_iff by simp
-  finally show "isint (Add a b) bs" by (simp add: isint_iff)
-qed
-
-lemma isint_c: "isint (C j) bs"
-  by (simp add: isint_iff)
-
-
-    (* FORMULAE *)
-datatype fm  = 
-  T| F| Lt num| Le num| Gt num| Ge num| Eq num| NEq num| Dvd int num| NDvd int num|
-  NOT fm| And fm fm|  Or fm fm| Imp fm fm| Iff fm fm| E fm| A fm
-
-
-  (* A size for fm *)
-fun fmsize :: "fm \<Rightarrow> nat" where
- "fmsize (NOT p) = 1 + fmsize p"
-| "fmsize (And p q) = 1 + fmsize p + fmsize q"
-| "fmsize (Or p q) = 1 + fmsize p + fmsize q"
-| "fmsize (Imp p q) = 3 + fmsize p + fmsize q"
-| "fmsize (Iff p q) = 3 + 2*(fmsize p + fmsize q)"
-| "fmsize (E p) = 1 + fmsize p"
-| "fmsize (A p) = 4+ fmsize p"
-| "fmsize (Dvd i t) = 2"
-| "fmsize (NDvd i t) = 2"
-| "fmsize p = 1"
-  (* several lemmas about fmsize *)
-lemma fmsize_pos: "fmsize p > 0"	
-by (induct p rule: fmsize.induct) simp_all
-
-  (* Semantics of formulae (fm) *)
-primrec Ifm ::"real list \<Rightarrow> fm \<Rightarrow> bool" where
-  "Ifm bs T = True"
-| "Ifm bs F = False"
-| "Ifm bs (Lt a) = (Inum bs a < 0)"
-| "Ifm bs (Gt a) = (Inum bs a > 0)"
-| "Ifm bs (Le a) = (Inum bs a \<le> 0)"
-| "Ifm bs (Ge a) = (Inum bs a \<ge> 0)"
-| "Ifm bs (Eq a) = (Inum bs a = 0)"
-| "Ifm bs (NEq a) = (Inum bs a \<noteq> 0)"
-| "Ifm bs (Dvd i b) = (real i rdvd Inum bs b)"
-| "Ifm bs (NDvd i b) = (\<not>(real i rdvd Inum bs b))"
-| "Ifm bs (NOT p) = (\<not> (Ifm bs p))"
-| "Ifm bs (And p q) = (Ifm bs p \<and> Ifm bs q)"
-| "Ifm bs (Or p q) = (Ifm bs p \<or> Ifm bs q)"
-| "Ifm bs (Imp p q) = ((Ifm bs p) \<longrightarrow> (Ifm bs q))"
-| "Ifm bs (Iff p q) = (Ifm bs p = Ifm bs q)"
-| "Ifm bs (E p) = (\<exists> x. Ifm (x#bs) p)"
-| "Ifm bs (A p) = (\<forall> x. Ifm (x#bs) p)"
-
-consts prep :: "fm \<Rightarrow> fm"
-recdef prep "measure fmsize"
-  "prep (E T) = T"
-  "prep (E F) = F"
-  "prep (E (Or p q)) = Or (prep (E p)) (prep (E q))"
-  "prep (E (Imp p q)) = Or (prep (E (NOT p))) (prep (E q))"
-  "prep (E (Iff p q)) = Or (prep (E (And p q))) (prep (E (And (NOT p) (NOT q))))" 
-  "prep (E (NOT (And p q))) = Or (prep (E (NOT p))) (prep (E(NOT q)))"
-  "prep (E (NOT (Imp p q))) = prep (E (And p (NOT q)))"
-  "prep (E (NOT (Iff p q))) = Or (prep (E (And p (NOT q)))) (prep (E(And (NOT p) q)))"
-  "prep (E p) = E (prep p)"
-  "prep (A (And p q)) = And (prep (A p)) (prep (A q))"
-  "prep (A p) = prep (NOT (E (NOT p)))"
-  "prep (NOT (NOT p)) = prep p"
-  "prep (NOT (And p q)) = Or (prep (NOT p)) (prep (NOT q))"
-  "prep (NOT (A p)) = prep (E (NOT p))"
-  "prep (NOT (Or p q)) = And (prep (NOT p)) (prep (NOT q))"
-  "prep (NOT (Imp p q)) = And (prep p) (prep (NOT q))"
-  "prep (NOT (Iff p q)) = Or (prep (And p (NOT q))) (prep (And (NOT p) q))"
-  "prep (NOT p) = NOT (prep p)"
-  "prep (Or p q) = Or (prep p) (prep q)"
-  "prep (And p q) = And (prep p) (prep q)"
-  "prep (Imp p q) = prep (Or (NOT p) q)"
-  "prep (Iff p q) = Or (prep (And p q)) (prep (And (NOT p) (NOT q)))"
-  "prep p = p"
-(hints simp add: fmsize_pos)
-lemma prep: "\<And> bs. Ifm bs (prep p) = Ifm bs p"
-by (induct p rule: prep.induct, auto)
-
-
-  (* Quantifier freeness *)
-fun qfree:: "fm \<Rightarrow> bool" where
-  "qfree (E p) = False"
-  | "qfree (A p) = False"
-  | "qfree (NOT p) = qfree p" 
-  | "qfree (And p q) = (qfree p \<and> qfree q)" 
-  | "qfree (Or  p q) = (qfree p \<and> qfree q)" 
-  | "qfree (Imp p q) = (qfree p \<and> qfree q)" 
-  | "qfree (Iff p q) = (qfree p \<and> qfree q)"
-  | "qfree p = True"
-
-  (* Boundedness and substitution *)
-primrec numbound0 :: "num \<Rightarrow> bool" (* a num is INDEPENDENT of Bound 0 *) where
-  "numbound0 (C c) = True"
-  | "numbound0 (Bound n) = (n>0)"
-  | "numbound0 (CN n i a) = (n > 0 \<and> numbound0 a)"
-  | "numbound0 (Neg a) = numbound0 a"
-  | "numbound0 (Add a b) = (numbound0 a \<and> numbound0 b)"
-  | "numbound0 (Sub a b) = (numbound0 a \<and> numbound0 b)" 
-  | "numbound0 (Mul i a) = numbound0 a"
-  | "numbound0 (Floor a) = numbound0 a"
-  | "numbound0 (CF c a b) = (numbound0 a \<and> numbound0 b)" 
-
-lemma numbound0_I:
-  assumes nb: "numbound0 a"
-  shows "Inum (b#bs) a = Inum (b'#bs) a"
-  using nb by (induct a) (auto simp add: nth_pos2)
-
-lemma numbound0_gen: 
-  assumes nb: "numbound0 t" and ti: "isint t (x#bs)"
-  shows "\<forall> y. isint t (y#bs)"
-using nb ti 
-proof(clarify)
-  fix y
-  from numbound0_I[OF nb, where bs="bs" and b="y" and b'="x"] ti[simplified isint_def]
-  show "isint t (y#bs)"
-    by (simp add: isint_def)
-qed
-
-primrec bound0:: "fm \<Rightarrow> bool" (* A Formula is independent of Bound 0 *) where
-  "bound0 T = True"
-  | "bound0 F = True"
-  | "bound0 (Lt a) = numbound0 a"
-  | "bound0 (Le a) = numbound0 a"
-  | "bound0 (Gt a) = numbound0 a"
-  | "bound0 (Ge a) = numbound0 a"
-  | "bound0 (Eq a) = numbound0 a"
-  | "bound0 (NEq a) = numbound0 a"
-  | "bound0 (Dvd i a) = numbound0 a"
-  | "bound0 (NDvd i a) = numbound0 a"
-  | "bound0 (NOT p) = bound0 p"
-  | "bound0 (And p q) = (bound0 p \<and> bound0 q)"
-  | "bound0 (Or p q) = (bound0 p \<and> bound0 q)"
-  | "bound0 (Imp p q) = ((bound0 p) \<and> (bound0 q))"
-  | "bound0 (Iff p q) = (bound0 p \<and> bound0 q)"
-  | "bound0 (E p) = False"
-  | "bound0 (A p) = False"
-
-lemma bound0_I:
-  assumes bp: "bound0 p"
-  shows "Ifm (b#bs) p = Ifm (b'#bs) p"
- using bp numbound0_I [where b="b" and bs="bs" and b'="b'"]
-  by (induct p) (auto simp add: nth_pos2)
-
-primrec numsubst0:: "num \<Rightarrow> num \<Rightarrow> num" (* substitute a num into a num for Bound 0 *) where
-  "numsubst0 t (C c) = (C c)"
-  | "numsubst0 t (Bound n) = (if n=0 then t else Bound n)"
-  | "numsubst0 t (CN n i a) = (if n=0 then Add (Mul i t) (numsubst0 t a) else CN n i (numsubst0 t a))"
-  | "numsubst0 t (CF i a b) = CF i (numsubst0 t a) (numsubst0 t b)"
-  | "numsubst0 t (Neg a) = Neg (numsubst0 t a)"
-  | "numsubst0 t (Add a b) = Add (numsubst0 t a) (numsubst0 t b)"
-  | "numsubst0 t (Sub a b) = Sub (numsubst0 t a) (numsubst0 t b)" 
-  | "numsubst0 t (Mul i a) = Mul i (numsubst0 t a)"
-  | "numsubst0 t (Floor a) = Floor (numsubst0 t a)"
-
-lemma numsubst0_I:
-  shows "Inum (b#bs) (numsubst0 a t) = Inum ((Inum (b#bs) a)#bs) t"
-  by (induct t) (simp_all add: nth_pos2)
-
-lemma numsubst0_I':
-  assumes nb: "numbound0 a"
-  shows "Inum (b#bs) (numsubst0 a t) = Inum ((Inum (b'#bs) a)#bs) t"
-  by (induct t) (simp_all add: nth_pos2 numbound0_I[OF nb, where b="b" and b'="b'"])
-
-primrec subst0:: "num \<Rightarrow> fm \<Rightarrow> fm" (* substitue a num into a formula for Bound 0 *) where
-  "subst0 t T = T"
-  | "subst0 t F = F"
-  | "subst0 t (Lt a) = Lt (numsubst0 t a)"
-  | "subst0 t (Le a) = Le (numsubst0 t a)"
-  | "subst0 t (Gt a) = Gt (numsubst0 t a)"
-  | "subst0 t (Ge a) = Ge (numsubst0 t a)"
-  | "subst0 t (Eq a) = Eq (numsubst0 t a)"
-  | "subst0 t (NEq a) = NEq (numsubst0 t a)"
-  | "subst0 t (Dvd i a) = Dvd i (numsubst0 t a)"
-  | "subst0 t (NDvd i a) = NDvd i (numsubst0 t a)"
-  | "subst0 t (NOT p) = NOT (subst0 t p)"
-  | "subst0 t (And p q) = And (subst0 t p) (subst0 t q)"
-  | "subst0 t (Or p q) = Or (subst0 t p) (subst0 t q)"
-  | "subst0 t (Imp p q) = Imp (subst0 t p) (subst0 t q)"
-  | "subst0 t (Iff p q) = Iff (subst0 t p) (subst0 t q)"
-
-lemma subst0_I: assumes qfp: "qfree p"
-  shows "Ifm (b#bs) (subst0 a p) = Ifm ((Inum (b#bs) a)#bs) p"
-  using qfp numsubst0_I[where b="b" and bs="bs" and a="a"]
-  by (induct p) (simp_all add: nth_pos2 )
-
-consts
-  decrnum:: "num \<Rightarrow> num" 
-  decr :: "fm \<Rightarrow> fm"
-
-recdef decrnum "measure size"
-  "decrnum (Bound n) = Bound (n - 1)"
-  "decrnum (Neg a) = Neg (decrnum a)"
-  "decrnum (Add a b) = Add (decrnum a) (decrnum b)"
-  "decrnum (Sub a b) = Sub (decrnum a) (decrnum b)"
-  "decrnum (Mul c a) = Mul c (decrnum a)"
-  "decrnum (Floor a) = Floor (decrnum a)"
-  "decrnum (CN n c a) = CN (n - 1) c (decrnum a)"
-  "decrnum (CF c a b) = CF c (decrnum a) (decrnum b)"
-  "decrnum a = a"
-
-recdef decr "measure size"
-  "decr (Lt a) = Lt (decrnum a)"
-  "decr (Le a) = Le (decrnum a)"
-  "decr (Gt a) = Gt (decrnum a)"
-  "decr (Ge a) = Ge (decrnum a)"
-  "decr (Eq a) = Eq (decrnum a)"
-  "decr (NEq a) = NEq (decrnum a)"
-  "decr (Dvd i a) = Dvd i (decrnum a)"
-  "decr (NDvd i a) = NDvd i (decrnum a)"
-  "decr (NOT p) = NOT (decr p)" 
-  "decr (And p q) = And (decr p) (decr q)"
-  "decr (Or p q) = Or (decr p) (decr q)"
-  "decr (Imp p q) = Imp (decr p) (decr q)"
-  "decr (Iff p q) = Iff (decr p) (decr q)"
-  "decr p = p"
-
-lemma decrnum: assumes nb: "numbound0 t"
-  shows "Inum (x#bs) t = Inum bs (decrnum t)"
-  using nb by (induct t rule: decrnum.induct, simp_all add: nth_pos2)
-
-lemma decr: assumes nb: "bound0 p"
-  shows "Ifm (x#bs) p = Ifm bs (decr p)"
-  using nb 
-  by (induct p rule: decr.induct, simp_all add: nth_pos2 decrnum)
-
-lemma decr_qf: "bound0 p \<Longrightarrow> qfree (decr p)"
-by (induct p, simp_all)
-
-consts 
-  isatom :: "fm \<Rightarrow> bool" (* test for atomicity *)
-recdef isatom "measure size"
-  "isatom T = True"
-  "isatom F = True"
-  "isatom (Lt a) = True"
-  "isatom (Le a) = True"
-  "isatom (Gt a) = True"
-  "isatom (Ge a) = True"
-  "isatom (Eq a) = True"
-  "isatom (NEq a) = True"
-  "isatom (Dvd i b) = True"
-  "isatom (NDvd i b) = True"
-  "isatom p = False"
-
-lemma numsubst0_numbound0: assumes nb: "numbound0 t"
-  shows "numbound0 (numsubst0 t a)"
-using nb by (induct a, auto)
-
-lemma subst0_bound0: assumes qf: "qfree p" and nb: "numbound0 t"
-  shows "bound0 (subst0 t p)"
-using qf numsubst0_numbound0[OF nb] by (induct p, auto)
-
-lemma bound0_qf: "bound0 p \<Longrightarrow> qfree p"
-by (induct p, simp_all)
-
-
-definition djf:: "('a \<Rightarrow> fm) \<Rightarrow> 'a \<Rightarrow> fm \<Rightarrow> fm" where
-  "djf f p q = (if q=T then T else if q=F then f p else 
-  (let fp = f p in case fp of T \<Rightarrow> T | F \<Rightarrow> q | _ \<Rightarrow> Or fp q))"
-
-definition evaldjf:: "('a \<Rightarrow> fm) \<Rightarrow> 'a list \<Rightarrow> fm" where
-  "evaldjf f ps = foldr (djf f) ps F"
-
-lemma djf_Or: "Ifm bs (djf f p q) = Ifm bs (Or (f p) q)"
-by (cases "q=T", simp add: djf_def,cases "q=F",simp add: djf_def) 
-(cases "f p", simp_all add: Let_def djf_def) 
-
-lemma evaldjf_ex: "Ifm bs (evaldjf f ps) = (\<exists> p \<in> set ps. Ifm bs (f p))"
-  by(induct ps, simp_all add: evaldjf_def djf_Or)
-
-lemma evaldjf_bound0: 
-  assumes nb: "\<forall> x\<in> set xs. bound0 (f x)"
-  shows "bound0 (evaldjf f xs)"
-  using nb by (induct xs, auto simp add: evaldjf_def djf_def Let_def) (case_tac "f a", auto) 
-
-lemma evaldjf_qf: 
-  assumes nb: "\<forall> x\<in> set xs. qfree (f x)"
-  shows "qfree (evaldjf f xs)"
-  using nb by (induct xs, auto simp add: evaldjf_def djf_def Let_def) (case_tac "f a", auto) 
-
-consts 
-  disjuncts :: "fm \<Rightarrow> fm list" 
-  conjuncts :: "fm \<Rightarrow> fm list"
-recdef disjuncts "measure size"
-  "disjuncts (Or p q) = (disjuncts p) @ (disjuncts q)"
-  "disjuncts F = []"
-  "disjuncts p = [p]"
-
-recdef conjuncts "measure size"
-  "conjuncts (And p q) = (conjuncts p) @ (conjuncts q)"
-  "conjuncts T = []"
-  "conjuncts p = [p]"
-lemma disjuncts: "(\<exists> q\<in> set (disjuncts p). Ifm bs q) = Ifm bs p"
-by(induct p rule: disjuncts.induct, auto)
-lemma conjuncts: "(\<forall> q\<in> set (conjuncts p). Ifm bs q) = Ifm bs p"
-by(induct p rule: conjuncts.induct, auto)
-
-lemma disjuncts_nb: "bound0 p \<Longrightarrow> \<forall> q\<in> set (disjuncts p). bound0 q"
-proof-
-  assume nb: "bound0 p"
-  hence "list_all bound0 (disjuncts p)" by (induct p rule:disjuncts.induct,auto)
-  thus ?thesis by (simp only: list_all_iff)
-qed
-lemma conjuncts_nb: "bound0 p \<Longrightarrow> \<forall> q\<in> set (conjuncts p). bound0 q"
-proof-
-  assume nb: "bound0 p"
-  hence "list_all bound0 (conjuncts p)" by (induct p rule:conjuncts.induct,auto)
-  thus ?thesis by (simp only: list_all_iff)
-qed
-
-lemma disjuncts_qf: "qfree p \<Longrightarrow> \<forall> q\<in> set (disjuncts p). qfree q"
-proof-
-  assume qf: "qfree p"
-  hence "list_all qfree (disjuncts p)"
-    by (induct p rule: disjuncts.induct, auto)
-  thus ?thesis by (simp only: list_all_iff)
-qed
-lemma conjuncts_qf: "qfree p \<Longrightarrow> \<forall> q\<in> set (conjuncts p). qfree q"
-proof-
-  assume qf: "qfree p"
-  hence "list_all qfree (conjuncts p)"
-    by (induct p rule: conjuncts.induct, auto)
-  thus ?thesis by (simp only: list_all_iff)
-qed
-
-constdefs DJ :: "(fm \<Rightarrow> fm) \<Rightarrow> fm \<Rightarrow> fm"
-  "DJ f p \<equiv> evaldjf f (disjuncts p)"
-
-lemma DJ: assumes fdj: "\<forall> p q. f (Or p q) = Or (f p) (f q)"
-  and fF: "f F = F"
-  shows "Ifm bs (DJ f p) = Ifm bs (f p)"
-proof-
-  have "Ifm bs (DJ f p) = (\<exists> q \<in> set (disjuncts p). Ifm bs (f q))"
-    by (simp add: DJ_def evaldjf_ex) 
-  also have "\<dots> = Ifm bs (f p)" using fdj fF by (induct p rule: disjuncts.induct, auto)
-  finally show ?thesis .
-qed
-
-lemma DJ_qf: assumes 
-  fqf: "\<forall> p. qfree p \<longrightarrow> qfree (f p)"
-  shows "\<forall>p. qfree p \<longrightarrow> qfree (DJ f p) "
-proof(clarify)
-  fix  p assume qf: "qfree p"
-  have th: "DJ f p = evaldjf f (disjuncts p)" by (simp add: DJ_def)
-  from disjuncts_qf[OF qf] have "\<forall> q\<in> set (disjuncts p). qfree q" .
-  with fqf have th':"\<forall> q\<in> set (disjuncts p). qfree (f q)" by blast
-  
-  from evaldjf_qf[OF th'] th show "qfree (DJ f p)" by simp
-qed
-
-lemma DJ_qe: assumes qe: "\<forall> bs p. qfree p \<longrightarrow> qfree (qe p) \<and> (Ifm bs (qe p) = Ifm bs (E p))"
-  shows "\<forall> bs p. qfree p \<longrightarrow> qfree (DJ qe p) \<and> (Ifm bs ((DJ qe p)) = Ifm bs (E p))"
-proof(clarify)
-  fix p::fm and bs
-  assume qf: "qfree p"
-  from qe have qth: "\<forall> p. qfree p \<longrightarrow> qfree (qe p)" by blast
-  from DJ_qf[OF qth] qf have qfth:"qfree (DJ qe p)" by auto
-  have "Ifm bs (DJ qe p) = (\<exists> q\<in> set (disjuncts p). Ifm bs (qe q))"
-    by (simp add: DJ_def evaldjf_ex)
-  also have "\<dots> = (\<exists> q \<in> set(disjuncts p). Ifm bs (E q))" using qe disjuncts_qf[OF qf] by auto
-  also have "\<dots> = Ifm bs (E p)" by (induct p rule: disjuncts.induct, auto)
-  finally show "qfree (DJ qe p) \<and> Ifm bs (DJ qe p) = Ifm bs (E p)" using qfth by blast
-qed
-  (* Simplification *)
-
-  (* Algebraic simplifications for nums *)
-consts bnds:: "num \<Rightarrow> nat list"
-  lex_ns:: "nat list \<times> nat list \<Rightarrow> bool"
-recdef bnds "measure size"
-  "bnds (Bound n) = [n]"
-  "bnds (CN n c a) = n#(bnds a)"
-  "bnds (Neg a) = bnds a"
-  "bnds (Add a b) = (bnds a)@(bnds b)"
-  "bnds (Sub a b) = (bnds a)@(bnds b)"
-  "bnds (Mul i a) = bnds a"
-  "bnds (Floor a) = bnds a"
-  "bnds (CF c a b) = (bnds a)@(bnds b)"
-  "bnds a = []"
-recdef lex_ns "measure (\<lambda> (xs,ys). length xs + length ys)"
-  "lex_ns ([], ms) = True"
-  "lex_ns (ns, []) = False"
-  "lex_ns (n#ns, m#ms) = (n<m \<or> ((n = m) \<and> lex_ns (ns,ms))) "
-constdefs lex_bnd :: "num \<Rightarrow> num \<Rightarrow> bool"
-  "lex_bnd t s \<equiv> lex_ns (bnds t, bnds s)"
-
-consts 
-  numgcdh:: "num \<Rightarrow> int \<Rightarrow> int"
-  reducecoeffh:: "num \<Rightarrow> int \<Rightarrow> num"
-  dvdnumcoeff:: "num \<Rightarrow> int \<Rightarrow> bool"
-consts maxcoeff:: "num \<Rightarrow> int"
-recdef maxcoeff "measure size"
-  "maxcoeff (C i) = abs i"
-  "maxcoeff (CN n c t) = max (abs c) (maxcoeff t)"
-  "maxcoeff (CF c t s) = max (abs c) (maxcoeff s)"
-  "maxcoeff t = 1"
-
-lemma maxcoeff_pos: "maxcoeff t \<ge> 0"
-  apply (induct t rule: maxcoeff.induct, auto) 
-  done
-
-recdef numgcdh "measure size"
-  "numgcdh (C i) = (\<lambda>g. zgcd i g)"
-  "numgcdh (CN n c t) = (\<lambda>g. zgcd c (numgcdh t g))"
-  "numgcdh (CF c s t) = (\<lambda>g. zgcd c (numgcdh t g))"
-  "numgcdh t = (\<lambda>g. 1)"
-
-definition
-  numgcd :: "num \<Rightarrow> int"
-where
-  numgcd_def: "numgcd t = numgcdh t (maxcoeff t)"
-
-recdef reducecoeffh "measure size"
-  "reducecoeffh (C i) = (\<lambda> g. C (i div g))"
-  "reducecoeffh (CN n c t) = (\<lambda> g. CN n (c div g) (reducecoeffh t g))"
-  "reducecoeffh (CF c s t) = (\<lambda> g. CF (c div g)  s (reducecoeffh t g))"
-  "reducecoeffh t = (\<lambda>g. t)"
-
-definition
-  reducecoeff :: "num \<Rightarrow> num"
-where
-  reducecoeff_def: "reducecoeff t =
-  (let g = numgcd t in 
-  if g = 0 then C 0 else if g=1 then t else reducecoeffh t g)"
-
-recdef dvdnumcoeff "measure size"
-  "dvdnumcoeff (C i) = (\<lambda> g. g dvd i)"
-  "dvdnumcoeff (CN n c t) = (\<lambda> g. g dvd c \<and> (dvdnumcoeff t g))"
-  "dvdnumcoeff (CF c s t) = (\<lambda> g. g dvd c \<and> (dvdnumcoeff t g))"
-  "dvdnumcoeff t = (\<lambda>g. False)"
-
-lemma dvdnumcoeff_trans: 
-  assumes gdg: "g dvd g'" and dgt':"dvdnumcoeff t g'"
-  shows "dvdnumcoeff t g"
-  using dgt' gdg 
-  by (induct t rule: dvdnumcoeff.induct, simp_all add: gdg zdvd_trans[OF gdg])
-
-declare zdvd_trans [trans add]
-
-lemma natabs0: "(nat (abs x) = 0) = (x = 0)"
-by arith
-
-lemma numgcd0:
-  assumes g0: "numgcd t = 0"
-  shows "Inum bs t = 0"
-proof-
-  have "\<And>x. numgcdh t x= 0 \<Longrightarrow> Inum bs t = 0"
-    by (induct t rule: numgcdh.induct, auto simp add: zgcd_def gcd_zero natabs0 max_def maxcoeff_pos)
-  thus ?thesis using g0[simplified numgcd_def] by blast
-qed
-
-lemma numgcdh_pos: assumes gp: "g \<ge> 0" shows "numgcdh t g \<ge> 0"
-  using gp
-  by (induct t rule: numgcdh.induct, auto simp add: zgcd_def)
-
-lemma numgcd_pos: "numgcd t \<ge>0"
-  by (simp add: numgcd_def numgcdh_pos maxcoeff_pos)
-
-lemma reducecoeffh:
-  assumes gt: "dvdnumcoeff t g" and gp: "g > 0" 
-  shows "real g *(Inum bs (reducecoeffh t g)) = Inum bs t"
-  using gt
-proof(induct t rule: reducecoeffh.induct) 
-  case (1 i) hence gd: "g dvd i" by simp
-  from gp have gnz: "g \<noteq> 0" by simp
-  from prems show ?case by (simp add: real_of_int_div[OF gnz gd])
-next
-  case (2 n c t)  hence gd: "g dvd c" by simp
-  from gp have gnz: "g \<noteq> 0" by simp
-  from prems show ?case by (simp add: real_of_int_div[OF gnz gd] algebra_simps)
-next
-  case (3 c s t)  hence gd: "g dvd c" by simp
-  from gp have gnz: "g \<noteq> 0" by simp
-  from prems show ?case by (simp add: real_of_int_div[OF gnz gd] algebra_simps) 
-qed (auto simp add: numgcd_def gp)
-consts ismaxcoeff:: "num \<Rightarrow> int \<Rightarrow> bool"
-recdef ismaxcoeff "measure size"
-  "ismaxcoeff (C i) = (\<lambda> x. abs i \<le> x)"
-  "ismaxcoeff (CN n c t) = (\<lambda>x. abs c \<le> x \<and> (ismaxcoeff t x))"
-  "ismaxcoeff (CF c s t) = (\<lambda>x. abs c \<le> x \<and> (ismaxcoeff t x))"
-  "ismaxcoeff t = (\<lambda>x. True)"
-
-lemma ismaxcoeff_mono: "ismaxcoeff t c \<Longrightarrow> c \<le> c' \<Longrightarrow> ismaxcoeff t c'"
-by (induct t rule: ismaxcoeff.induct, auto)
-
-lemma maxcoeff_ismaxcoeff: "ismaxcoeff t (maxcoeff t)"
-proof (induct t rule: maxcoeff.induct)
-  case (2 n c t)
-  hence H:"ismaxcoeff t (maxcoeff t)" .
-  have thh: "maxcoeff t \<le> max (abs c) (maxcoeff t)" by (simp add: le_maxI2)
-  from ismaxcoeff_mono[OF H thh] show ?case by (simp add: le_maxI1)
-next
-  case (3 c t s) 
-  hence H1:"ismaxcoeff s (maxcoeff s)" by auto
-  have thh1: "maxcoeff s \<le> max \<bar>c\<bar> (maxcoeff s)" by (simp add: max_def)
-  from ismaxcoeff_mono[OF H1 thh1] show ?case by (simp add: le_maxI1)
-qed simp_all
-
-lemma zgcd_gt1: "zgcd i j > 1 \<Longrightarrow> ((abs i > 1 \<and> abs j > 1) \<or> (abs i = 0 \<and> abs j > 1) \<or> (abs i > 1 \<and> abs j = 0))"
-  apply (unfold zgcd_def)
-  apply (cases "i = 0", simp_all)
-  apply (cases "j = 0", simp_all)
-  apply (cases "abs i = 1", simp_all)
-  apply (cases "abs j = 1", simp_all)
-  apply auto
-  done
-lemma numgcdh0:"numgcdh t m = 0 \<Longrightarrow>  m =0"
-  by (induct t rule: numgcdh.induct, auto simp add:zgcd0)
-
-lemma dvdnumcoeff_aux:
-  assumes "ismaxcoeff t m" and mp:"m \<ge> 0" and "numgcdh t m > 1"
-  shows "dvdnumcoeff t (numgcdh t m)"
-using prems
-proof(induct t rule: numgcdh.induct)
-  case (2 n c t) 
-  let ?g = "numgcdh t m"
-  from prems have th:"zgcd c ?g > 1" by simp
-  from zgcd_gt1[OF th] numgcdh_pos[OF mp, where t="t"]
-  have "(abs c > 1 \<and> ?g > 1) \<or> (abs c = 0 \<and> ?g > 1) \<or> (abs c > 1 \<and> ?g = 0)" by simp
-  moreover {assume "abs c > 1" and gp: "?g > 1" with prems
-    have th: "dvdnumcoeff t ?g" by simp
-    have th': "zgcd c ?g dvd ?g" by (simp add:zgcd_zdvd2)
-    from dvdnumcoeff_trans[OF th' th] have ?case by (simp add: zgcd_zdvd1)}
-  moreover {assume "abs c = 0 \<and> ?g > 1"
-    with prems have th: "dvdnumcoeff t ?g" by simp
-    have th': "zgcd c ?g dvd ?g" by (simp add:zgcd_zdvd2)
-    from dvdnumcoeff_trans[OF th' th] have ?case by (simp add: zgcd_zdvd1)
-    hence ?case by simp }
-  moreover {assume "abs c > 1" and g0:"?g = 0" 
-    from numgcdh0[OF g0] have "m=0". with prems   have ?case by simp }
-  ultimately show ?case by blast
-next
-  case (3 c s t) 
-  let ?g = "numgcdh t m"
-  from prems have th:"zgcd c ?g > 1" by simp
-  from zgcd_gt1[OF th] numgcdh_pos[OF mp, where t="t"]
-  have "(abs c > 1 \<and> ?g > 1) \<or> (abs c = 0 \<and> ?g > 1) \<or> (abs c > 1 \<and> ?g = 0)" by simp
-  moreover {assume "abs c > 1" and gp: "?g > 1" with prems
-    have th: "dvdnumcoeff t ?g" by simp
-    have th': "zgcd c ?g dvd ?g" by (simp add:zgcd_zdvd2)
-    from dvdnumcoeff_trans[OF th' th] have ?case by (simp add: zgcd_zdvd1)}
-  moreover {assume "abs c = 0 \<and> ?g > 1"
-    with prems have th: "dvdnumcoeff t ?g" by simp
-    have th': "zgcd c ?g dvd ?g" by (simp add:zgcd_zdvd2)
-    from dvdnumcoeff_trans[OF th' th] have ?case by (simp add: zgcd_zdvd1)
-    hence ?case by simp }
-  moreover {assume "abs c > 1" and g0:"?g = 0" 
-    from numgcdh0[OF g0] have "m=0". with prems   have ?case by simp }
-  ultimately show ?case by blast
-qed(auto simp add: zgcd_zdvd1)
-
-lemma dvdnumcoeff_aux2:
-  assumes "numgcd t > 1" shows "dvdnumcoeff t (numgcd t) \<and> numgcd t > 0"
-  using prems 
-proof (simp add: numgcd_def)
-  let ?mc = "maxcoeff t"
-  let ?g = "numgcdh t ?mc"
-  have th1: "ismaxcoeff t ?mc" by (rule maxcoeff_ismaxcoeff)
-  have th2: "?mc \<ge> 0" by (rule maxcoeff_pos)
-  assume H: "numgcdh t ?mc > 1"
-  from dvdnumcoeff_aux[OF th1 th2 H]  show "dvdnumcoeff t ?g" .
-qed
-
-lemma reducecoeff: "real (numgcd t) * (Inum bs (reducecoeff t)) = Inum bs t"
-proof-
-  let ?g = "numgcd t"
-  have "?g \<ge> 0"  by (simp add: numgcd_pos)
-  hence	"?g = 0 \<or> ?g = 1 \<or> ?g > 1" by auto
-  moreover {assume "?g = 0" hence ?thesis by (simp add: numgcd0)} 
-  moreover {assume "?g = 1" hence ?thesis by (simp add: reducecoeff_def)} 
-  moreover { assume g1:"?g > 1"
-    from dvdnumcoeff_aux2[OF g1] have th1:"dvdnumcoeff t ?g" and g0: "?g > 0" by blast+
-    from reducecoeffh[OF th1 g0, where bs="bs"] g1 have ?thesis 
-      by (simp add: reducecoeff_def Let_def)} 
-  ultimately show ?thesis by blast
-qed
-
-lemma reducecoeffh_numbound0: "numbound0 t \<Longrightarrow> numbound0 (reducecoeffh t g)"
-by (induct t rule: reducecoeffh.induct, auto)
-
-lemma reducecoeff_numbound0: "numbound0 t \<Longrightarrow> numbound0 (reducecoeff t)"
-using reducecoeffh_numbound0 by (simp add: reducecoeff_def Let_def)
-
-consts
-  simpnum:: "num \<Rightarrow> num"
-  numadd:: "num \<times> num \<Rightarrow> num"
-  nummul:: "num \<Rightarrow> int \<Rightarrow> num"
-
-recdef numadd "measure (\<lambda> (t,s). size t + size s)"
-  "numadd (CN n1 c1 r1,CN n2 c2 r2) =
-  (if n1=n2 then 
-  (let c = c1 + c2
-  in (if c=0 then numadd(r1,r2) else CN n1 c (numadd (r1,r2))))
-  else if n1 \<le> n2 then CN n1 c1 (numadd (r1,CN n2 c2 r2))
-  else (CN n2 c2 (numadd (CN n1 c1 r1,r2))))"
-  "numadd (CN n1 c1 r1,t) = CN n1 c1 (numadd (r1, t))"  
-  "numadd (t,CN n2 c2 r2) = CN n2 c2 (numadd (t,r2))" 
-  "numadd (CF c1 t1 r1,CF c2 t2 r2) = 
-   (if t1 = t2 then 
-    (let c=c1+c2; s= numadd(r1,r2) in (if c=0 then s else CF c t1 s))
-   else if lex_bnd t1 t2 then CF c1 t1 (numadd(r1,CF c2 t2 r2))
-   else CF c2 t2 (numadd(CF c1 t1 r1,r2)))"
-  "numadd (CF c1 t1 r1,C c) = CF c1 t1 (numadd (r1, C c))"
-  "numadd (C c,CF c1 t1 r1) = CF c1 t1 (numadd (r1, C c))"
-  "numadd (C b1, C b2) = C (b1+b2)"
-  "numadd (a,b) = Add a b"
-
-lemma numadd[simp]: "Inum bs (numadd (t,s)) = Inum bs (Add t s)"
-apply (induct t s rule: numadd.induct, simp_all add: Let_def)
- apply (case_tac "c1+c2 = 0",case_tac "n1 \<le> n2", simp_all)
-  apply (case_tac "n1 = n2", simp_all add: algebra_simps)
-  apply (simp only: left_distrib[symmetric])
- apply simp
-apply (case_tac "lex_bnd t1 t2", simp_all)
- apply (case_tac "c1+c2 = 0")
-  by (case_tac "t1 = t2", simp_all add: algebra_simps left_distrib[symmetric] real_of_int_mult[symmetric] real_of_int_add[symmetric]del: real_of_int_mult real_of_int_add left_distrib)
-
-lemma numadd_nb[simp]: "\<lbrakk> numbound0 t ; numbound0 s\<rbrakk> \<Longrightarrow> numbound0 (numadd (t,s))"
-by (induct t s rule: numadd.induct, auto simp add: Let_def)
-
-recdef nummul "measure size"
-  "nummul (C j) = (\<lambda> i. C (i*j))"
-  "nummul (CN n c t) = (\<lambda> i. CN n (c*i) (nummul t i))"
-  "nummul (CF c t s) = (\<lambda> i. CF (c*i) t (nummul s i))"
-  "nummul (Mul c t) = (\<lambda> i. nummul t (i*c))"
-  "nummul t = (\<lambda> i. Mul i t)"
-
-lemma nummul[simp]: "\<And> i. Inum bs (nummul t i) = Inum bs (Mul i t)"
-by (induct t rule: nummul.induct, auto simp add: algebra_simps)
-
-lemma nummul_nb[simp]: "\<And> i. numbound0 t \<Longrightarrow> numbound0 (nummul t i)"
-by (induct t rule: nummul.induct, auto)
-
-constdefs numneg :: "num \<Rightarrow> num"
-  "numneg t \<equiv> nummul t (- 1)"
-
-constdefs numsub :: "num \<Rightarrow> num \<Rightarrow> num"
-  "numsub s t \<equiv> (if s = t then C 0 else numadd (s,numneg t))"
-
-lemma numneg[simp]: "Inum bs (numneg t) = Inum bs (Neg t)"
-using numneg_def nummul by simp
-
-lemma numneg_nb[simp]: "numbound0 t \<Longrightarrow> numbound0 (numneg t)"
-using numneg_def by simp
-
-lemma numsub[simp]: "Inum bs (numsub a b) = Inum bs (Sub a b)"
-using numsub_def by simp
-
-lemma numsub_nb[simp]: "\<lbrakk> numbound0 t ; numbound0 s\<rbrakk> \<Longrightarrow> numbound0 (numsub t s)"
-using numsub_def by simp
-
-lemma isint_CF: assumes si: "isint s bs" shows "isint (CF c t s) bs"
-proof-
-  have cti: "isint (Mul c (Floor t)) bs" by (simp add: isint_Mul isint_Floor)
-  
-  have "?thesis = isint (Add (Mul c (Floor t)) s) bs" by (simp add: isint_def)
-  also have "\<dots>" by (simp add: isint_add cti si)
-  finally show ?thesis .
-qed
-
-consts split_int:: "num \<Rightarrow> num\<times>num"
-recdef split_int "measure num_size"
-  "split_int (C c) = (C 0, C c)"
-  "split_int (CN n c b) = 
-     (let (bv,bi) = split_int b 
-       in (CN n c bv, bi))"
-  "split_int (CF c a b) = 
-     (let (bv,bi) = split_int b 
-       in (bv, CF c a bi))"
-  "split_int a = (a,C 0)"
-
-lemma split_int:"\<And> tv ti. split_int t = (tv,ti) \<Longrightarrow> (Inum bs (Add tv ti) = Inum bs t) \<and> isint ti bs"
-proof (induct t rule: split_int.induct)
-  case (2 c n b tv ti)
-  let ?bv = "fst (split_int b)"
-  let ?bi = "snd (split_int b)"
-  have "split_int b = (?bv,?bi)" by simp
-  with prems(1) have b:"Inum bs (Add ?bv ?bi) = Inum bs b" and bii: "isint ?bi bs" by blast+
-  from prems(2) have tibi: "ti = ?bi" by (simp add: Let_def split_def)
-  from prems(2) b[symmetric] bii show ?case by (auto simp add: Let_def split_def)
-next
-  case (3 c a b tv ti) 
-  let ?bv = "fst (split_int b)"
-  let ?bi = "snd (split_int b)"
-  have "split_int b = (?bv,?bi)" by simp
-  with prems(1) have b:"Inum bs (Add ?bv ?bi) = Inum bs b" and bii: "isint ?bi bs" by blast+
-  from prems(2) have tibi: "ti = CF c a ?bi" by (simp add: Let_def split_def)
-  from prems(2) b[symmetric] bii show ?case by (auto simp add: Let_def split_def isint_Floor isint_add isint_Mul isint_CF)
-qed (auto simp add: Let_def isint_iff isint_Floor isint_add isint_Mul split_def algebra_simps)
-
-lemma split_int_nb: "numbound0 t \<Longrightarrow> numbound0 (fst (split_int t)) \<and> numbound0 (snd (split_int t)) "
-by (induct t rule: split_int.induct, auto simp add: Let_def split_def)
-
-definition
-  numfloor:: "num \<Rightarrow> num"
-where
-  numfloor_def: "numfloor t = (let (tv,ti) = split_int t in 
-  (case tv of C i \<Rightarrow> numadd (tv,ti) 
-  | _ \<Rightarrow> numadd(CF 1 tv (C 0),ti)))"
-
-lemma numfloor[simp]: "Inum bs (numfloor t) = Inum bs (Floor t)" (is "?n t = ?N (Floor t)")
-proof-
-  let ?tv = "fst (split_int t)"
-  let ?ti = "snd (split_int t)"
-  have tvti:"split_int t = (?tv,?ti)" by simp
-  {assume H: "\<forall> v. ?tv \<noteq> C v"
-    hence th1: "?n t = ?N (Add (Floor ?tv) ?ti)" 
-      by (cases ?tv, auto simp add: numfloor_def Let_def split_def numadd)
-    from split_int[OF tvti] have "?N (Floor t) = ?N (Floor(Add ?tv ?ti))" and tii:"isint ?ti bs" by simp+
-    hence "?N (Floor t) = real (floor (?N (Add ?tv ?ti)))" by simp 
-    also have "\<dots> = real (floor (?N ?tv) + (floor (?N ?ti)))"
-      by (simp,subst tii[simplified isint_iff, symmetric]) simp
-    also have "\<dots> = ?N (Add (Floor ?tv) ?ti)" by (simp add: tii[simplified isint_iff])
-    finally have ?thesis using th1 by simp}
-  moreover {fix v assume H:"?tv = C v" 
-    from split_int[OF tvti] have "?N (Floor t) = ?N (Floor(Add ?tv ?ti))" and tii:"isint ?ti bs" by simp+
-    hence "?N (Floor t) = real (floor (?N (Add ?tv ?ti)))" by simp 
-    also have "\<dots> = real (floor (?N ?tv) + (floor (?N ?ti)))"
-      by (simp,subst tii[simplified isint_iff, symmetric]) simp
-    also have "\<dots> = ?N (Add (Floor ?tv) ?ti)" by (simp add: tii[simplified isint_iff])
-    finally have ?thesis by (simp add: H numfloor_def Let_def split_def numadd) }
-  ultimately show ?thesis by auto
-qed
-
-lemma numfloor_nb[simp]: "numbound0 t \<Longrightarrow> numbound0 (numfloor t)"
-  using split_int_nb[where t="t"]
-  by (cases "fst(split_int t)" , auto simp add: numfloor_def Let_def split_def  numadd_nb)
-
-recdef simpnum "measure num_size"
-  "simpnum (C j) = C j"
-  "simpnum (Bound n) = CN n 1 (C 0)"
-  "simpnum (Neg t) = numneg (simpnum t)"
-  "simpnum (Add t s) = numadd (simpnum t,simpnum s)"
-  "simpnum (Sub t s) = numsub (simpnum t) (simpnum s)"
-  "simpnum (Mul i t) = (if i = 0 then (C 0) else nummul (simpnum t) i)"
-  "simpnum (Floor t) = numfloor (simpnum t)"
-  "simpnum (CN n c t) = (if c=0 then simpnum t else CN n c (simpnum t))"
-  "simpnum (CF c t s) = simpnum(Add (Mul c (Floor t)) s)"
-
-lemma simpnum_ci[simp]: "Inum bs (simpnum t) = Inum bs t"
-by (induct t rule: simpnum.induct, auto)
-
-lemma simpnum_numbound0[simp]: 
-  "numbound0 t \<Longrightarrow> numbound0 (simpnum t)"
-by (induct t rule: simpnum.induct, auto)
-
-consts nozerocoeff:: "num \<Rightarrow> bool"
-recdef nozerocoeff "measure size"
-  "nozerocoeff (C c) = True"
-  "nozerocoeff (CN n c t) = (c\<noteq>0 \<and> nozerocoeff t)"
-  "nozerocoeff (CF c s t) = (c \<noteq> 0 \<and> nozerocoeff t)"
-  "nozerocoeff (Mul c t) = (c\<noteq>0 \<and> nozerocoeff t)"
-  "nozerocoeff t = True"
-
-lemma numadd_nz : "nozerocoeff a \<Longrightarrow> nozerocoeff b \<Longrightarrow> nozerocoeff (numadd (a,b))"
-by (induct a b rule: numadd.induct,auto simp add: Let_def)
-
-lemma nummul_nz : "\<And> i. i\<noteq>0 \<Longrightarrow> nozerocoeff a \<Longrightarrow> nozerocoeff (nummul a i)"
-  by (induct a rule: nummul.induct,auto simp add: Let_def numadd_nz)
-
-lemma numneg_nz : "nozerocoeff a \<Longrightarrow> nozerocoeff (numneg a)"
-by (simp add: numneg_def nummul_nz)
-
-lemma numsub_nz: "nozerocoeff a \<Longrightarrow> nozerocoeff b \<Longrightarrow> nozerocoeff (numsub a b)"
-by (simp add: numsub_def numneg_nz numadd_nz)
-
-lemma split_int_nz: "nozerocoeff t \<Longrightarrow> nozerocoeff (fst (split_int t)) \<and> nozerocoeff (snd (split_int t))"
-by (induct t rule: split_int.induct,auto simp add: Let_def split_def)
-
-lemma numfloor_nz: "nozerocoeff t \<Longrightarrow> nozerocoeff (numfloor t)"
-by (simp add: numfloor_def Let_def split_def)
-(cases "fst (split_int t)", simp_all add: split_int_nz numadd_nz)
-
-lemma simpnum_nz: "nozerocoeff (simpnum t)"
-by(induct t rule: simpnum.induct, auto simp add: numadd_nz numneg_nz numsub_nz nummul_nz numfloor_nz)
-
-lemma maxcoeff_nz: "nozerocoeff t \<Longrightarrow> maxcoeff t = 0 \<Longrightarrow> t = C 0"
-proof (induct t rule: maxcoeff.induct)
-  case (2 n c t)
-  hence cnz: "c \<noteq>0" and mx: "max (abs c) (maxcoeff t) = 0" by simp+
-  have "max (abs c) (maxcoeff t) \<ge> abs c" by (simp add: le_maxI1)
-  with cnz have "max (abs c) (maxcoeff t) > 0" by arith
-  with prems show ?case by simp
-next
-  case (3 c s t) 
-  hence cnz: "c \<noteq>0" and mx: "max (abs c) (maxcoeff t) = 0" by simp+
-  have "max (abs c) (maxcoeff t) \<ge> abs c" by (simp add: le_maxI1)
-  with cnz have "max (abs c) (maxcoeff t) > 0" by arith
-  with prems show ?case by simp
-qed auto
-
-lemma numgcd_nz: assumes nz: "nozerocoeff t" and g0: "numgcd t = 0" shows "t = C 0"
-proof-
-  from g0 have th:"numgcdh t (maxcoeff t) = 0" by (simp add: numgcd_def)
-  from numgcdh0[OF th]  have th:"maxcoeff t = 0" .
-  from maxcoeff_nz[OF nz th] show ?thesis .
-qed
-
-constdefs simp_num_pair:: "(num \<times> int) \<Rightarrow> num \<times> int"
-  "simp_num_pair \<equiv> (\<lambda> (t,n). (if n = 0 then (C 0, 0) else
-   (let t' = simpnum t ; g = numgcd t' in 
-      if g > 1 then (let g' = zgcd n g in 
-        if g' = 1 then (t',n) 
-        else (reducecoeffh t' g', n div g')) 
-      else (t',n))))"
-
-lemma simp_num_pair_ci:
-  shows "((\<lambda> (t,n). Inum bs t / real n) (simp_num_pair (t,n))) = ((\<lambda> (t,n). Inum bs t / real n) (t,n))"
-  (is "?lhs = ?rhs")
-proof-
-  let ?t' = "simpnum t"
-  let ?g = "numgcd ?t'"
-  let ?g' = "zgcd n ?g"
-  {assume nz: "n = 0" hence ?thesis by (simp add: Let_def simp_num_pair_def)}
-  moreover
-  { assume nnz: "n \<noteq> 0"
-    {assume "\<not> ?g > 1" hence ?thesis by (simp add: Let_def simp_num_pair_def)}
-    moreover
-    {assume g1:"?g>1" hence g0: "?g > 0" by simp
-      from zgcd0 g1 nnz have gp0: "?g' \<noteq> 0" by simp
-      hence g'p: "?g' > 0" using zgcd_pos[where i="n" and j="numgcd ?t'"] by arith
-      hence "?g'= 1 \<or> ?g' > 1" by arith
-      moreover {assume "?g'=1" hence ?thesis by (simp add: Let_def simp_num_pair_def)}
-      moreover {assume g'1:"?g'>1"
-	from dvdnumcoeff_aux2[OF g1] have th1:"dvdnumcoeff ?t' ?g" ..
-	let ?tt = "reducecoeffh ?t' ?g'"
-	let ?t = "Inum bs ?tt"
-	have gpdg: "?g' dvd ?g" by (simp add: zgcd_zdvd2)
-	have gpdd: "?g' dvd n" by (simp add: zgcd_zdvd1) 
-	have gpdgp: "?g' dvd ?g'" by simp
-	from reducecoeffh[OF dvdnumcoeff_trans[OF gpdg th1] g'p] 
-	have th2:"real ?g' * ?t = Inum bs ?t'" by simp
-	from prems have "?lhs = ?t / real (n div ?g')" by (simp add: simp_num_pair_def Let_def)
-	also have "\<dots> = (real ?g' * ?t) / (real ?g' * (real (n div ?g')))" by simp
-	also have "\<dots> = (Inum bs ?t' / real n)"
-	  using real_of_int_div[OF gp0 gpdd] th2 gp0 by simp
-	finally have "?lhs = Inum bs t / real n" by simp
-	then have ?thesis using prems by (simp add: simp_num_pair_def)}
-      ultimately have ?thesis by blast}
-    ultimately have ?thesis by blast} 
-  ultimately show ?thesis by blast
-qed
-
-lemma simp_num_pair_l: assumes tnb: "numbound0 t" and np: "n >0" and tn: "simp_num_pair (t,n) = (t',n')"
-  shows "numbound0 t' \<and> n' >0"
-proof-
-    let ?t' = "simpnum t"
-  let ?g = "numgcd ?t'"
-  let ?g' = "zgcd n ?g"
-  {assume nz: "n = 0" hence ?thesis using prems by (simp add: Let_def simp_num_pair_def)}
-  moreover
-  { assume nnz: "n \<noteq> 0"
-    {assume "\<not> ?g > 1" hence ?thesis  using prems by (auto simp add: Let_def simp_num_pair_def)}
-    moreover
-    {assume g1:"?g>1" hence g0: "?g > 0" by simp
-      from zgcd0 g1 nnz have gp0: "?g' \<noteq> 0" by simp
-      hence g'p: "?g' > 0" using zgcd_pos[where i="n" and j="numgcd ?t'"] by arith
-      hence "?g'= 1 \<or> ?g' > 1" by arith
-      moreover {assume "?g'=1" hence ?thesis using prems 
-	  by (auto simp add: Let_def simp_num_pair_def)}
-      moreover {assume g'1:"?g'>1"
-	have gpdg: "?g' dvd ?g" by (simp add: zgcd_zdvd2)
-	have gpdd: "?g' dvd n" by (simp add: zgcd_zdvd1) 
-	have gpdgp: "?g' dvd ?g'" by simp
-	from zdvd_imp_le[OF gpdd np] have g'n: "?g' \<le> n" .
-	from zdiv_mono1[OF g'n g'p, simplified zdiv_self[OF gp0]]
-	have "n div ?g' >0" by simp
-	hence ?thesis using prems 
-	  by(auto simp add: simp_num_pair_def Let_def reducecoeffh_numbound0)}
-      ultimately have ?thesis by blast}
-    ultimately have ?thesis by blast} 
-  ultimately show ?thesis by blast
-qed
-
-consts not:: "fm \<Rightarrow> fm"
-recdef not "measure size"
-  "not (NOT p) = p"
-  "not T = F"
-  "not F = T"
-  "not (Lt t) = Ge t"
-  "not (Le t) = Gt t"
-  "not (Gt t) = Le t"
-  "not (Ge t) = Lt t"
-  "not (Eq t) = NEq t"
-  "not (NEq t) = Eq t"
-  "not (Dvd i t) = NDvd i t"
-  "not (NDvd i t) = Dvd i t"
-  "not (And p q) = Or (not p) (not q)"
-  "not (Or p q) = And (not p) (not q)"
-  "not p = NOT p"
-lemma not[simp]: "Ifm bs (not p) = Ifm bs (NOT p)"
-by (induct p) auto
-lemma not_qf[simp]: "qfree p \<Longrightarrow> qfree (not p)"
-by (induct p, auto)
-lemma not_nb[simp]: "bound0 p \<Longrightarrow> bound0 (not p)"
-by (induct p, auto)
-
-constdefs conj :: "fm \<Rightarrow> fm \<Rightarrow> fm"
-  "conj p q \<equiv> (if (p = F \<or> q=F) then F else if p=T then q else if q=T then p else 
-   if p = q then p else And p q)"
-lemma conj[simp]: "Ifm bs (conj p q) = Ifm bs (And p q)"
-by (cases "p=F \<or> q=F",simp_all add: conj_def) (cases p,simp_all)
-
-lemma conj_qf[simp]: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (conj p q)"
-using conj_def by auto 
-lemma conj_nb[simp]: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (conj p q)"
-using conj_def by auto 
-
-constdefs disj :: "fm \<Rightarrow> fm \<Rightarrow> fm"
-  "disj p q \<equiv> (if (p = T \<or> q=T) then T else if p=F then q else if q=F then p 
-       else if p=q then p else Or p q)"
-
-lemma disj[simp]: "Ifm bs (disj p q) = Ifm bs (Or p q)"
-by (cases "p=T \<or> q=T",simp_all add: disj_def) (cases p,simp_all)
-lemma disj_qf[simp]: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (disj p q)"
-using disj_def by auto 
-lemma disj_nb[simp]: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (disj p q)"
-using disj_def by auto 
-
-constdefs   imp :: "fm \<Rightarrow> fm \<Rightarrow> fm"
-  "imp p q \<equiv> (if (p = F \<or> q=T \<or> p=q) then T else if p=T then q else if q=F then not p 
-    else Imp p q)"
-lemma imp[simp]: "Ifm bs (imp p q) = Ifm bs (Imp p q)"
-by (cases "p=F \<or> q=T",simp_all add: imp_def)
-lemma imp_qf[simp]: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (imp p q)"
-using imp_def by (cases "p=F \<or> q=T",simp_all add: imp_def)
-lemma imp_nb[simp]: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (imp p q)"
-using imp_def by (cases "p=F \<or> q=T \<or> p=q",simp_all add: imp_def) 
-
-constdefs   iff :: "fm \<Rightarrow> fm \<Rightarrow> fm"
-  "iff p q \<equiv> (if (p = q) then T else if (p = not q \<or> not p = q) then F else 
-       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 
-  Iff p q)"
-lemma iff[simp]: "Ifm bs (iff p q) = Ifm bs (Iff p q)"
-  by (unfold iff_def,cases "p=q", simp,cases "p=not q", simp add:not) 
-(cases "not p= q", auto simp add:not)
-lemma iff_qf[simp]: "\<lbrakk>qfree p ; qfree q\<rbrakk> \<Longrightarrow> qfree (iff p q)"
-  by (unfold iff_def,cases "p=q", auto)
-lemma iff_nb[simp]: "\<lbrakk>bound0 p ; bound0 q\<rbrakk> \<Longrightarrow> bound0 (iff p q)"
-using iff_def by (unfold iff_def,cases "p=q", auto)
-
-consts check_int:: "num \<Rightarrow> bool"
-recdef check_int "measure size"
-  "check_int (C i) = True"
-  "check_int (Floor t) = True"
-  "check_int (Mul i t) = check_int t"
-  "check_int (Add t s) = (check_int t \<and> check_int s)"
-  "check_int (Neg t) = check_int t"
-  "check_int (CF c t s) = check_int s"
-  "check_int t = False"
-lemma check_int: "check_int t \<Longrightarrow> isint t bs"
-by (induct t, auto simp add: isint_add isint_Floor isint_Mul isint_neg isint_c isint_CF)
-
-lemma rdvd_left1_int: "real \<lfloor>t\<rfloor> = t \<Longrightarrow> 1 rdvd t"
-  by (simp add: rdvd_def,rule_tac x="\<lfloor>t\<rfloor>" in exI) simp
-
-lemma rdvd_reduce: 
-  assumes gd:"g dvd d" and gc:"g dvd c" and gp: "g > 0"
-  shows "real (d::int) rdvd real (c::int)*t = (real (d div g) rdvd real (c div g)*t)"
-proof
-  assume d: "real d rdvd real c * t"
-  from d rdvd_def obtain k where k_def: "real c * t = real d* real (k::int)" by auto
-  from gd dvd_def obtain kd where kd_def: "d = g * kd" by auto
-  from gc dvd_def obtain kc where kc_def: "c = g * kc" by auto
-  from k_def kd_def kc_def have "real g * real kc * t = real g * real kd * real k" by simp
-  hence "real kc * t = real kd * real k" using gp by simp
-  hence th:"real kd rdvd real kc * t" using rdvd_def by blast
-  from kd_def gp have th':"kd = d div g" by simp
-  from kc_def gp have "kc = c div g" by simp
-  with th th' show "real (d div g) rdvd real (c div g) * t" by simp
-next
-  assume d: "real (d div g) rdvd real (c div g) * t"
-  from gp have gnz: "g \<noteq> 0" by simp
-  thus "real d rdvd real c * t" using d rdvd_mult[OF gnz, where n="d div g" and x="real (c div g) * t"] real_of_int_div[OF gnz gd] real_of_int_div[OF gnz gc] by simp
-qed
-
-constdefs simpdvd:: "int \<Rightarrow> num \<Rightarrow> (int \<times> num)"
-  "simpdvd d t \<equiv> 
-   (let g = numgcd t in 
-      if g > 1 then (let g' = zgcd d g in 
-        if g' = 1 then (d, t) 
-        else (d div g',reducecoeffh t g')) 
-      else (d, t))"
-lemma simpdvd: 
-  assumes tnz: "nozerocoeff t" and dnz: "d \<noteq> 0"
-  shows "Ifm bs (Dvd (fst (simpdvd d t)) (snd (simpdvd d t))) = Ifm bs (Dvd d t)"
-proof-
-  let ?g = "numgcd t"
-  let ?g' = "zgcd d ?g"
-  {assume "\<not> ?g > 1" hence ?thesis by (simp add: Let_def simpdvd_def)}
-  moreover
-  {assume g1:"?g>1" hence g0: "?g > 0" by simp
-    from zgcd0 g1 dnz have gp0: "?g' \<noteq> 0" by simp
-    hence g'p: "?g' > 0" using zgcd_pos[where i="d" and j="numgcd t"] by arith
-    hence "?g'= 1 \<or> ?g' > 1" by arith
-    moreover {assume "?g'=1" hence ?thesis by (simp add: Let_def simpdvd_def)}
-    moreover {assume g'1:"?g'>1"
-      from dvdnumcoeff_aux2[OF g1] have th1:"dvdnumcoeff t ?g" ..
-      let ?tt = "reducecoeffh t ?g'"
-      let ?t = "Inum bs ?tt"
-      have gpdg: "?g' dvd ?g" by (simp add: zgcd_zdvd2)
-      have gpdd: "?g' dvd d" by (simp add: zgcd_zdvd1) 
-      have gpdgp: "?g' dvd ?g'" by simp
-      from reducecoeffh[OF dvdnumcoeff_trans[OF gpdg th1] g'p] 
-      have th2:"real ?g' * ?t = Inum bs t" by simp
-      from prems have "Ifm bs (Dvd (fst (simpdvd d t)) (snd(simpdvd d t))) = Ifm bs (Dvd (d div ?g') ?tt)"
-	by (simp add: simpdvd_def Let_def)
-      also have "\<dots> = (real d rdvd (Inum bs t))"
-	using rdvd_reduce[OF gpdd gpdgp g'p, where t="?t", simplified zdiv_self[OF gp0]] 
-	  th2[symmetric] by simp
-      finally have ?thesis by simp  }
-    ultimately have ?thesis by blast
-  }
-  ultimately show ?thesis by blast
-qed
-
-consts simpfm :: "fm \<Rightarrow> fm"
-recdef simpfm "measure fmsize"
-  "simpfm (And p q) = conj (simpfm p) (simpfm q)"
-  "simpfm (Or p q) = disj (simpfm p) (simpfm q)"
-  "simpfm (Imp p q) = imp (simpfm p) (simpfm q)"
-  "simpfm (Iff p q) = iff (simpfm p) (simpfm q)"
-  "simpfm (NOT p) = not (simpfm p)"
-  "simpfm (Lt a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v < 0) then T else F 
-  | _ \<Rightarrow> Lt (reducecoeff a'))"
-  "simpfm (Le a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v \<le> 0)  then T else F | _ \<Rightarrow> Le (reducecoeff a'))"
-  "simpfm (Gt a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v > 0)  then T else F | _ \<Rightarrow> Gt (reducecoeff a'))"
-  "simpfm (Ge a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v \<ge> 0)  then T else F | _ \<Rightarrow> Ge (reducecoeff a'))"
-  "simpfm (Eq a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v = 0)  then T else F | _ \<Rightarrow> Eq (reducecoeff a'))"
-  "simpfm (NEq a) = (let a' = simpnum a in case a' of C v \<Rightarrow> if (v \<noteq> 0)  then T else F | _ \<Rightarrow> NEq (reducecoeff a'))"
-  "simpfm (Dvd i a) = (if i=0 then simpfm (Eq a)
-             else if (abs i = 1) \<and> check_int a then T
-             else let a' = simpnum a in case a' of C v \<Rightarrow> if (i dvd v)  then T else F | _ \<Rightarrow> (let (d,t) = simpdvd i a' in Dvd d t))"
-  "simpfm (NDvd i a) = (if i=0 then simpfm (NEq a) 
-             else if (abs i = 1) \<and> check_int a then F
-             else let a' = simpnum a in case a' of C v \<Rightarrow> if (\<not>(i dvd v)) then T else F | _ \<Rightarrow> (let (d,t) = simpdvd i a' in NDvd d t))"
-  "simpfm p = p"
-
-lemma simpfm[simp]: "Ifm bs (simpfm p) = Ifm bs p"
-proof(induct p rule: simpfm.induct)
-  case (6 a) let ?sa = "simpnum a" have sa: "Inum bs ?sa = Inum bs a" by simp
-  {fix v assume "?sa = C v" hence ?case using sa by simp }
-  moreover {assume H:"\<not> (\<exists> v. ?sa = C v)"
-    let ?g = "numgcd ?sa"
-    let ?rsa = "reducecoeff ?sa"
-    let ?r = "Inum bs ?rsa"
-    have sa_nz: "nozerocoeff ?sa" by (rule simpnum_nz)
-    {assume gz: "?g=0" from numgcd_nz[OF sa_nz gz] H have "False" by auto}
-    with numgcd_pos[where t="?sa"] have "?g > 0" by (cases "?g=0", auto)
-    hence gp: "real ?g > 0" by simp
-    have "Inum bs ?sa = real ?g* ?r" by (simp add: reducecoeff)
-    with sa have "Inum bs a < 0 = (real ?g * ?r < real ?g * 0)" by simp
-    also have "\<dots> = (?r < 0)" using gp
-      by (simp only: mult_less_cancel_left) simp
-    finally have ?case using H by (cases "?sa" , simp_all add: Let_def)}
-  ultimately show ?case by blast
-next
-  case (7 a)  let ?sa = "simpnum a" have sa: "Inum bs ?sa = Inum bs a" by simp
-  {fix v assume "?sa = C v" hence ?case using sa by simp }
-  moreover {assume H:"\<not> (\<exists> v. ?sa = C v)"
-    let ?g = "numgcd ?sa"
-    let ?rsa = "reducecoeff ?sa"
-    let ?r = "Inum bs ?rsa"
-    have sa_nz: "nozerocoeff ?sa" by (rule simpnum_nz)
-    {assume gz: "?g=0" from numgcd_nz[OF sa_nz gz] H have "False" by auto}
-    with numgcd_pos[where t="?sa"] have "?g > 0" by (cases "?g=0", auto)
-    hence gp: "real ?g > 0" by simp
-    have "Inum bs ?sa = real ?g* ?r" by (simp add: reducecoeff)
-    with sa have "Inum bs a \<le> 0 = (real ?g * ?r \<le> real ?g * 0)" by simp
-    also have "\<dots> = (?r \<le> 0)" using gp
-      by (simp only: mult_le_cancel_left) simp
-    finally have ?case using H by (cases "?sa" , simp_all add: Let_def)}
-  ultimately show ?case by blast
-next
-  case (8 a)  let ?sa = "simpnum a" have sa: "Inum bs ?sa = Inum bs a" by simp
-  {fix v assume "?sa = C v" hence ?case using sa by simp }
-  moreover {assume H:"\<not> (\<exists> v. ?sa = C v)"
-    let ?g = "numgcd ?sa"
-    let ?rsa = "reducecoeff ?sa"
-    let ?r = "Inum bs ?rsa"
-    have sa_nz: "nozerocoeff ?sa" by (rule simpnum_nz)
-    {assume gz: "?g=0" from numgcd_nz[OF sa_nz gz] H have "False" by auto}
-    with numgcd_pos[where t="?sa"] have "?g > 0" by (cases "?g=0", auto)
-    hence gp: "real ?g > 0" by simp
-    have "Inum bs ?sa = real ?g* ?r" by (simp add: reducecoeff)
-    with sa have "Inum bs a > 0 = (real ?g * ?r > real ?g * 0)" by simp
-    also have "\<dots> = (?r > 0)" using gp
-      by (simp only: mult_less_cancel_left) simp
-    finally have ?case using H by (cases "?sa" , simp_all add: Let_def)}
-  ultimately show ?case by blast
-next
-  case (9 a)  let ?sa = "simpnum a" have sa: "Inum bs ?sa = Inum bs a" by simp
-  {fix v assume "?sa = C v" hence ?case using sa by simp }
-  moreover {assume H:"\<not> (\<exists> v. ?sa = C v)"
-    let ?g = "numgcd ?sa"
-    let ?rsa = "reducecoeff ?sa"
-    let ?r = "Inum bs ?rsa"
-    have sa_nz: "nozerocoeff ?sa" by (rule simpnum_nz)
-    {assume gz: "?g=0" from numgcd_nz[OF sa_nz gz] H have "False" by auto}
-    with numgcd_pos[where t="?sa"] have "?g > 0" by (cases "?g=0", auto)
-    hence gp: "real ?g > 0" by simp
-    have "Inum bs ?sa = real ?g* ?r" by (simp add: reducecoeff)
-    with sa have "Inum bs a \<ge> 0 = (real ?g * ?r \<ge> real ?g * 0)" by simp
-    also have "\<dots> = (?r \<ge> 0)" using gp
-      by (simp only: mult_le_cancel_left) simp
-    finally have ?case using H by (cases "?sa" , simp_all add: Let_def)}
-  ultimately show ?case by blast
-next
-  case (10 a)  let ?sa = "simpnum a" have sa: "Inum bs ?sa = Inum bs a" by simp
-  {fix v assume "?sa = C v" hence ?case using sa by simp }
-  moreover {assume H:"\<not> (\<exists> v. ?sa = C v)"
-    let ?g = "numgcd ?sa"
-    let ?rsa = "reducecoeff ?sa"
-    let ?r = "Inum bs ?rsa"
-    have sa_nz: "nozerocoeff ?sa" by (rule simpnum_nz)
-    {assume gz: "?g=0" from numgcd_nz[OF sa_nz gz] H have "False" by auto}
-    with numgcd_pos[where t="?sa"] have "?g > 0" by (cases "?g=0", auto)
-    hence gp: "real ?g > 0" by simp
-    have "Inum bs ?sa = real ?g* ?r" by (simp add: reducecoeff)
-    with sa have "Inum bs a = 0 = (real ?g * ?r = 0)" by simp
-    also have "\<dots> = (?r = 0)" using gp
-      by (simp add: mult_eq_0_iff)
-    finally have ?case using H by (cases "?sa" , simp_all add: Let_def)}
-  ultimately show ?case by blast
-next
-  case (11 a)  let ?sa = "simpnum a" have sa: "Inum bs ?sa = Inum bs a" by simp
-  {fix v assume "?sa = C v" hence ?case using sa by simp }
-  moreover {assume H:"\<not> (\<exists> v. ?sa = C v)"
-    let ?g = "numgcd ?sa"
-    let ?rsa = "reducecoeff ?sa"
-    let ?r = "Inum bs ?rsa"
-    have sa_nz: "nozerocoeff ?sa" by (rule simpnum_nz)
-    {assume gz: "?g=0" from numgcd_nz[OF sa_nz gz] H have "False" by auto}
-    with numgcd_pos[where t="?sa"] have "?g > 0" by (cases "?g=0", auto)
-    hence gp: "real ?g > 0" by simp
-    have "Inum bs ?sa = real ?g* ?r" by (simp add: reducecoeff)
-    with sa have "Inum bs a \<noteq> 0 = (real ?g * ?r \<noteq> 0)" by simp
-    also have "\<dots> = (?r \<noteq> 0)" using gp
-      by (simp add: mult_eq_0_iff)
-    finally have ?case using H by (cases "?sa" , simp_all add: Let_def)}
-  ultimately show ?case by blast
-next
-  case (12 i a)  let ?sa = "simpnum a"   have sa: "Inum bs ?sa = Inum bs a" by simp
-  have "i=0 \<or> (abs i = 1 \<and> check_int a) \<or> (i\<noteq>0 \<and> ((abs i \<noteq> 1) \<or> (\<not> check_int a)))" by auto
-  {assume "i=0" hence ?case using "12.hyps" by (simp add: rdvd_left_0_eq Let_def)}
-  moreover 
-  {assume ai1: "abs i = 1" and ai: "check_int a" 
-    hence "i=1 \<or> i= - 1" by arith
-    moreover {assume i1: "i = 1" 
-      from rdvd_left1_int[OF check_int[OF ai, simplified isint_iff]] 
-      have ?case using i1 ai by simp }
-    moreover {assume i1: "i = - 1" 
-      from rdvd_left1_int[OF check_int[OF ai, simplified isint_iff]] 
-	rdvd_abs1[where d="- 1" and t="Inum bs a"]
-      have ?case using i1 ai by simp }
-    ultimately have ?case by blast}
-  moreover   
-  {assume inz: "i\<noteq>0" and cond: "(abs i \<noteq> 1) \<or> (\<not> check_int a)"
-    {fix v assume "?sa = C v" hence ?case using sa[symmetric] inz cond
-	by (cases "abs i = 1", auto simp add: int_rdvd_iff) }
-    moreover {assume H:"\<not> (\<exists> v. ?sa = C v)" 
-      hence th: "simpfm (Dvd i a) = Dvd (fst (simpdvd i ?sa)) (snd (simpdvd i ?sa))" using inz cond by (cases ?sa, auto simp add: Let_def split_def)
-      from simpnum_nz have nz:"nozerocoeff ?sa" by simp
-      from simpdvd [OF nz inz] th have ?case using sa by simp}
-    ultimately have ?case by blast}
-  ultimately show ?case by blast
-next
-  case (13 i a)  let ?sa = "simpnum a"   have sa: "Inum bs ?sa = Inum bs a" by simp
-  have "i=0 \<or> (abs i = 1 \<and> check_int a) \<or> (i\<noteq>0 \<and> ((abs i \<noteq> 1) \<or> (\<not> check_int a)))" by auto
-  {assume "i=0" hence ?case using "13.hyps" by (simp add: rdvd_left_0_eq Let_def)}
-  moreover 
-  {assume ai1: "abs i = 1" and ai: "check_int a" 
-    hence "i=1 \<or> i= - 1" by arith
-    moreover {assume i1: "i = 1" 
-      from rdvd_left1_int[OF check_int[OF ai, simplified isint_iff]] 
-      have ?case using i1 ai by simp }
-    moreover {assume i1: "i = - 1" 
-      from rdvd_left1_int[OF check_int[OF ai, simplified isint_iff]] 
-	rdvd_abs1[where d="- 1" and t="Inum bs a"]
-      have ?case using i1 ai by simp }
-    ultimately have ?case by blast}
-  moreover   
-  {assume inz: "i\<noteq>0" and cond: "(abs i \<noteq> 1) \<or> (\<not> check_int a)"
-    {fix v assume "?sa = C v" hence ?case using sa[symmetric] inz cond
-	by (cases "abs i = 1", auto simp add: int_rdvd_iff) }
-    moreover {assume H:"\<not> (\<exists> v. ?sa = C v)" 
-      hence th: "simpfm (NDvd i a) = NDvd (fst (simpdvd i ?sa)) (snd (simpdvd i ?sa))" using inz cond 
-	by (cases ?sa, auto simp add: Let_def split_def)
-      from simpnum_nz have nz:"nozerocoeff ?sa" by simp
-      from simpdvd [OF nz inz] th have ?case using sa by simp}
-    ultimately have ?case by blast}
-  ultimately show ?case by blast
-qed (induct p rule: simpfm.induct, simp_all)
-
-lemma simpdvd_numbound0: "numbound0 t \<Longrightarrow> numbound0 (snd (simpdvd d t))"
-  by (simp add: simpdvd_def Let_def split_def reducecoeffh_numbound0)
-
-lemma simpfm_bound0[simp]: "bound0 p \<Longrightarrow> bound0 (simpfm p)"
-proof(induct p rule: simpfm.induct)
-  case (6 a) hence nb: "numbound0 a" by simp
-  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
-  thus ?case by (cases "simpnum a", auto simp add: Let_def reducecoeff_numbound0)
-next
-  case (7 a) hence nb: "numbound0 a" by simp
-  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
-  thus ?case by (cases "simpnum a", auto simp add: Let_def reducecoeff_numbound0)
-next
-  case (8 a) hence nb: "numbound0 a" by simp
-  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
-  thus ?case by (cases "simpnum a", auto simp add: Let_def reducecoeff_numbound0)
-next
-  case (9 a) hence nb: "numbound0 a" by simp
-  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
-  thus ?case by (cases "simpnum a", auto simp add: Let_def reducecoeff_numbound0)
-next
-  case (10 a) hence nb: "numbound0 a" by simp
-  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
-  thus ?case by (cases "simpnum a", auto simp add: Let_def reducecoeff_numbound0)
-next
-  case (11 a) hence nb: "numbound0 a" by simp
-  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
-  thus ?case by (cases "simpnum a", auto simp add: Let_def reducecoeff_numbound0)
-next
-  case (12 i a) hence nb: "numbound0 a" by simp
-  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
-  thus ?case by (cases "simpnum a", auto simp add: Let_def reducecoeff_numbound0 simpdvd_numbound0 split_def)
-next
-  case (13 i a) hence nb: "numbound0 a" by simp
-  hence "numbound0 (simpnum a)" by (simp only: simpnum_numbound0[OF nb])
-  thus ?case by (cases "simpnum a", auto simp add: Let_def reducecoeff_numbound0 simpdvd_numbound0 split_def)
-qed(auto simp add: disj_def imp_def iff_def conj_def)
-
-lemma simpfm_qf[simp]: "qfree p \<Longrightarrow> qfree (simpfm p)"
-by (induct p rule: simpfm.induct, auto simp add: Let_def)
-(case_tac "simpnum a",auto simp add: split_def Let_def)+
-
-
-  (* Generic quantifier elimination *)
-
-constdefs list_conj :: "fm list \<Rightarrow> fm"
-  "list_conj ps \<equiv> foldr conj ps T"
-lemma list_conj: "Ifm bs (list_conj ps) = (\<forall>p\<in> set ps. Ifm bs p)"
-  by (induct ps, auto simp add: list_conj_def)
-lemma list_conj_qf: " \<forall>p\<in> set ps. qfree p \<Longrightarrow> qfree (list_conj ps)"
-  by (induct ps, auto simp add: list_conj_def)
-lemma list_conj_nb: " \<forall>p\<in> set ps. bound0 p \<Longrightarrow> bound0 (list_conj ps)"
-  by (induct ps, auto simp add: list_conj_def)
-constdefs CJNB:: "(fm \<Rightarrow> fm) \<Rightarrow> fm \<Rightarrow> fm"
-  "CJNB f p \<equiv> (let cjs = conjuncts p ; (yes,no) = List.partition bound0 cjs
-                   in conj (decr (list_conj yes)) (f (list_conj no)))"
-
-lemma CJNB_qe: 
-  assumes qe: "\<forall> bs p. qfree p \<longrightarrow> qfree (qe p) \<and> (Ifm bs (qe p) = Ifm bs (E p))"
-  shows "\<forall> bs p. qfree p \<longrightarrow> qfree (CJNB qe p) \<and> (Ifm bs ((CJNB qe p)) = Ifm bs (E p))"
-proof(clarify)
-  fix bs p
-  assume qfp: "qfree p"
-  let ?cjs = "conjuncts p"
-  let ?yes = "fst (List.partition bound0 ?cjs)"
-  let ?no = "snd (List.partition bound0 ?cjs)"
-  let ?cno = "list_conj ?no"
-  let ?cyes = "list_conj ?yes"
-  have part: "List.partition bound0 ?cjs = (?yes,?no)" by simp
-  from partition_P[OF part] have "\<forall> q\<in> set ?yes. bound0 q" by blast 
-  hence yes_nb: "bound0 ?cyes" by (simp add: list_conj_nb) 
-  hence yes_qf: "qfree (decr ?cyes )" by (simp add: decr_qf)
-  from conjuncts_qf[OF qfp] partition_set[OF part] 
-  have " \<forall>q\<in> set ?no. qfree q" by auto
-  hence no_qf: "qfree ?cno"by (simp add: list_conj_qf)
-  with qe have cno_qf:"qfree (qe ?cno )" 
-    and noE: "Ifm bs (qe ?cno) = Ifm bs (E ?cno)" by blast+
-  from cno_qf yes_qf have qf: "qfree (CJNB qe p)" 
-    by (simp add: CJNB_def Let_def conj_qf split_def)
-  {fix bs
-    from conjuncts have "Ifm bs p = (\<forall>q\<in> set ?cjs. Ifm bs q)" by blast
-    also have "\<dots> = ((\<forall>q\<in> set ?yes. Ifm bs q) \<and> (\<forall>q\<in> set ?no. Ifm bs q))"
-      using partition_set[OF part] by auto
-    finally have "Ifm bs p = ((Ifm bs ?cyes) \<and> (Ifm bs ?cno))" using list_conj by simp}
-  hence "Ifm bs (E p) = (\<exists>x. (Ifm (x#bs) ?cyes) \<and> (Ifm (x#bs) ?cno))" by simp
-  also fix y have "\<dots> = (\<exists>x. (Ifm (y#bs) ?cyes) \<and> (Ifm (x#bs) ?cno))"
-    using bound0_I[OF yes_nb, where bs="bs" and b'="y"] by blast
-  also have "\<dots> = (Ifm bs (decr ?cyes) \<and> Ifm bs (E ?cno))"
-    by (auto simp add: decr[OF yes_nb])
-  also have "\<dots> = (Ifm bs (conj (decr ?cyes) (qe ?cno)))"
-    using qe[rule_format, OF no_qf] by auto
-  finally have "Ifm bs (E p) = Ifm bs (CJNB qe p)" 
-    by (simp add: Let_def CJNB_def split_def)
-  with qf show "qfree (CJNB qe p) \<and> Ifm bs (CJNB qe p) = Ifm bs (E p)" by blast
-qed
-
-consts qelim :: "fm \<Rightarrow> (fm \<Rightarrow> fm) \<Rightarrow> fm"
-recdef qelim "measure fmsize"
-  "qelim (E p) = (\<lambda> qe. DJ (CJNB qe) (qelim p qe))"
-  "qelim (A p) = (\<lambda> qe. not (qe ((qelim (NOT p) qe))))"
-  "qelim (NOT p) = (\<lambda> qe. not (qelim p qe))"
-  "qelim (And p q) = (\<lambda> qe. conj (qelim p qe) (qelim q qe))" 
-  "qelim (Or  p q) = (\<lambda> qe. disj (qelim p qe) (qelim q qe))" 
-  "qelim (Imp p q) = (\<lambda> qe. disj (qelim (NOT p) qe) (qelim q qe))"
-  "qelim (Iff p q) = (\<lambda> qe. iff (qelim p qe) (qelim q qe))"
-  "qelim p = (\<lambda> y. simpfm p)"
-
-lemma qelim_ci:
-  assumes qe_inv: "\<forall> bs p. qfree p \<longrightarrow> qfree (qe p) \<and> (Ifm bs (qe p) = Ifm bs (E p))"
-  shows "\<And> bs. qfree (qelim p qe) \<and> (Ifm bs (qelim p qe) = Ifm bs p)"
-using qe_inv DJ_qe[OF CJNB_qe[OF qe_inv]] 
-by(induct p rule: qelim.induct) 
-(auto simp del: simpfm.simps)
-
-
-text {* The @{text "\<int>"} Part *}
-text{* Linearity for fm where Bound 0 ranges over @{text "\<int>"} *}
-consts
-  zsplit0 :: "num \<Rightarrow> int \<times> num" (* splits the bounded from the unbounded part*)
-recdef zsplit0 "measure num_size"
-  "zsplit0 (C c) = (0,C c)"
-  "zsplit0 (Bound n) = (if n=0 then (1, C 0) else (0,Bound n))"
-  "zsplit0 (CN n c a) = zsplit0 (Add (Mul c (Bound n)) a)"
-  "zsplit0 (CF c a b) = zsplit0 (Add (Mul c (Floor a)) b)"
-  "zsplit0 (Neg a) = (let (i',a') =  zsplit0 a in (-i', Neg a'))"
-  "zsplit0 (Add a b) = (let (ia,a') =  zsplit0 a ; 
-                            (ib,b') =  zsplit0 b 
-                            in (ia+ib, Add a' b'))"
-  "zsplit0 (Sub a b) = (let (ia,a') =  zsplit0 a ; 
-                            (ib,b') =  zsplit0 b 
-                            in (ia-ib, Sub a' b'))"
-  "zsplit0 (Mul i a) = (let (i',a') =  zsplit0 a in (i*i', Mul i a'))"
-  "zsplit0 (Floor a) = (let (i',a') =  zsplit0 a in (i',Floor a'))"
-(hints simp add: Let_def)
-
-lemma zsplit0_I:
-  shows "\<And> n a. zsplit0 t = (n,a) \<Longrightarrow> (Inum ((real (x::int)) #bs) (CN 0 n a) = Inum (real x #bs) t) \<and> numbound0 a"
-  (is "\<And> n a. ?S t = (n,a) \<Longrightarrow> (?I x (CN 0 n a) = ?I x t) \<and> ?N a")
-proof(induct t rule: zsplit0.induct)
-  case (1 c n a) thus ?case by auto 
-next
-  case (2 m n a) thus ?case by (cases "m=0") auto
-next
-  case (3 n i a n a') thus ?case by auto
-next 
-  case (4 c a b n a') thus ?case by auto
-next
-  case (5 t n a)
-  let ?nt = "fst (zsplit0 t)"
-  let ?at = "snd (zsplit0 t)"
-  have abj: "zsplit0 t = (?nt,?at)" by simp hence th: "a=Neg ?at \<and> n=-?nt" using prems 
-    by (simp add: Let_def split_def)
-  from abj prems  have th2: "(?I x (CN 0 ?nt ?at) = ?I x t) \<and> ?N ?at" by blast
-  from th2[simplified] th[simplified] show ?case by simp
-next
-  case (6 s t n a)
-  let ?ns = "fst (zsplit0 s)"
-  let ?as = "snd (zsplit0 s)"
-  let ?nt = "fst (zsplit0 t)"
-  let ?at = "snd (zsplit0 t)"
-  have abjs: "zsplit0 s = (?ns,?as)" by simp 
-  moreover have abjt:  "zsplit0 t = (?nt,?at)" by simp 
-  ultimately have th: "a=Add ?as ?at \<and> n=?ns + ?nt" using prems 
-    by (simp add: Let_def split_def)
-  from abjs[symmetric] have bluddy: "\<exists> x y. (x,y) = zsplit0 s" by blast
-  from prems have "(\<exists> x y. (x,y) = zsplit0 s) \<longrightarrow> (\<forall>xa xb. zsplit0 t = (xa, xb) \<longrightarrow> Inum (real x # bs) (CN 0 xa xb) = Inum (real x # bs) t \<and> numbound0 xb)" by simp
-  with bluddy abjt have th3: "(?I x (CN 0 ?nt ?at) = ?I x t) \<and> ?N ?at" by blast
-  from abjs prems  have th2: "(?I x (CN 0 ?ns ?as) = ?I x s) \<and> ?N ?as" by blast
-  from th3[simplified] th2[simplified] th[simplified] show ?case 
-    by (simp add: left_distrib)
-next
-  case (7 s t n a)
-  let ?ns = "fst (zsplit0 s)"
-  let ?as = "snd (zsplit0 s)"
-  let ?nt = "fst (zsplit0 t)"
-  let ?at = "snd (zsplit0 t)"
-  have abjs: "zsplit0 s = (?ns,?as)" by simp 
-  moreover have abjt:  "zsplit0 t = (?nt,?at)" by simp 
-  ultimately have th: "a=Sub ?as ?at \<and> n=?ns - ?nt" using prems 
-    by (simp add: Let_def split_def)
-  from abjs[symmetric] have bluddy: "\<exists> x y. (x,y) = zsplit0 s" by blast
-  from prems have "(\<exists> x y. (x,y) = zsplit0 s) \<longrightarrow> (\<forall>xa xb. zsplit0 t = (xa, xb) \<longrightarrow> Inum (real x # bs) (CN 0 xa xb) = Inum (real x # bs) t \<and> numbound0 xb)" by simp
-  with bluddy abjt have th3: "(?I x (CN 0 ?nt ?at) = ?I x t) \<and> ?N ?at" by blast
-  from abjs prems  have th2: "(?I x (CN 0 ?ns ?as) = ?I x s) \<and> ?N ?as" by blast
-  from th3[simplified] th2[simplified] th[simplified] show ?case 
-    by (simp add: left_diff_distrib)
-next
-  case (8 i t n a)
-  let ?nt = "fst (zsplit0 t)"
-  let ?at = "snd (zsplit0 t)"
-  have abj: "zsplit0 t = (?nt,?at)" by simp hence th: "a=Mul i ?at \<and> n=i*?nt" using prems 
-    by (simp add: Let_def split_def)
-  from abj prems  have th2: "(?I x (CN 0 ?nt ?at) = ?I x t) \<and> ?N ?at" by blast
-  hence " ?I x (Mul i t) = (real i) * ?I x (CN 0 ?nt ?at)" by simp
-  also have "\<dots> = ?I x (CN 0 (i*?nt) (Mul i ?at))" by (simp add: right_distrib)
-  finally show ?case using th th2 by simp
-next
-  case (9 t n a)
-  let ?nt = "fst (zsplit0 t)"
-  let ?at = "snd (zsplit0 t)"
-  have abj: "zsplit0 t = (?nt,?at)" by simp hence th: "a= Floor ?at \<and> n=?nt" using prems 
-    by (simp add: Let_def split_def)
-  from abj prems  have th2: "(?I x (CN 0 ?nt ?at) = ?I x t) \<and> ?N ?at" by blast
-  hence na: "?N a" using th by simp
-  have th': "(real ?nt)*(real x) = real (?nt * x)" by simp
-  have "?I x (Floor t) = ?I x (Floor (CN 0 ?nt ?at))" using th2 by simp
-  also have "\<dots> = real (floor ((real ?nt)* real(x) + ?I x ?at))" by simp
-  also have "\<dots> = real (floor (?I x ?at + real (?nt* x)))" by (simp add: add_ac)
-  also have "\<dots> = real (floor (?I x ?at) + (?nt* x))" 
-    using floor_add[where x="?I x ?at" and a="?nt* x"] by simp 
-  also have "\<dots> = real (?nt)*(real x) + real (floor (?I x ?at))" by (simp add: add_ac)
-  finally have "?I x (Floor t) = ?I x (CN 0 n a)" using th by simp
-  with na show ?case by simp
-qed
-
-consts
-  iszlfm :: "fm \<Rightarrow> real list \<Rightarrow> bool"   (* Linearity test for fm *)
-  zlfm :: "fm \<Rightarrow> fm"       (* Linearity transformation for fm *)
-recdef iszlfm "measure size"
-  "iszlfm (And p q) = (\<lambda> bs. iszlfm p bs \<and> iszlfm q bs)" 
-  "iszlfm (Or p q) = (\<lambda> bs. iszlfm p bs \<and> iszlfm q bs)" 
-  "iszlfm (Eq  (CN 0 c e)) = (\<lambda> bs. c>0 \<and> numbound0 e \<and> isint e bs)"
-  "iszlfm (NEq (CN 0 c e)) = (\<lambda> bs. c>0 \<and> numbound0 e \<and> isint e bs)"
-  "iszlfm (Lt  (CN 0 c e)) = (\<lambda> bs. c>0 \<and> numbound0 e \<and> isint e bs)"
-  "iszlfm (Le  (CN 0 c e)) = (\<lambda> bs. c>0 \<and> numbound0 e \<and> isint e bs)"
-  "iszlfm (Gt  (CN 0 c e)) = (\<lambda> bs. c>0 \<and> numbound0 e \<and> isint e bs)"
-  "iszlfm (Ge  (CN 0 c e)) = (\<lambda> bs. c>0 \<and> numbound0 e \<and> isint e bs)"
-  "iszlfm (Dvd i (CN 0 c e)) = 
-                 (\<lambda> bs. c>0 \<and> i>0 \<and> numbound0 e \<and> isint e bs)"
-  "iszlfm (NDvd i (CN 0 c e))= 
-                 (\<lambda> bs. c>0 \<and> i>0 \<and> numbound0 e \<and> isint e bs)"
-  "iszlfm p = (\<lambda> bs. isatom p \<and> (bound0 p))"
-
-lemma zlin_qfree: "iszlfm p bs \<Longrightarrow> qfree p"
-  by (induct p rule: iszlfm.induct) auto
-
-lemma iszlfm_gen:
-  assumes lp: "iszlfm p (x#bs)"
-  shows "\<forall> y. iszlfm p (y#bs)"
-proof
-  fix y
-  show "iszlfm p (y#bs)"
-    using lp
-  by(induct p rule: iszlfm.induct, simp_all add: numbound0_gen[rule_format, where x="x" and y="y"])
-qed
-
-lemma conj_zl[simp]: "iszlfm p bs \<Longrightarrow> iszlfm q bs \<Longrightarrow> iszlfm (conj p q) bs"
-  using conj_def by (cases p,auto)
-lemma disj_zl[simp]: "iszlfm p bs \<Longrightarrow> iszlfm q bs \<Longrightarrow> iszlfm (disj p q) bs"
-  using disj_def by (cases p,auto)
-lemma not_zl[simp]: "iszlfm p bs \<Longrightarrow> iszlfm (not p) bs"
-  by (induct p rule:iszlfm.induct ,auto)
-
-recdef zlfm "measure fmsize"
-  "zlfm (And p q) = conj (zlfm p) (zlfm q)"
-  "zlfm (Or p q) = disj (zlfm p) (zlfm q)"
-  "zlfm (Imp p q) = disj (zlfm (NOT p)) (zlfm q)"
-  "zlfm (Iff p q) = disj (conj (zlfm p) (zlfm q)) (conj (zlfm (NOT p)) (zlfm (NOT q)))"
-  "zlfm (Lt a) = (let (c,r) = zsplit0 a in 
-     if c=0 then Lt r else 
-     if c>0 then Or (Lt (CN 0 c (Neg (Floor (Neg r))))) (And (Eq (CN 0 c (Neg (Floor (Neg r))))) (Lt (Add (Floor (Neg r)) r))) 
-     else Or (Gt (CN 0 (-c) (Floor(Neg r)))) (And (Eq(CN 0 (-c) (Floor(Neg r)))) (Lt (Add (Floor (Neg r)) r))))"
-  "zlfm (Le a) = (let (c,r) = zsplit0 a in 
-     if c=0 then Le r else 
-     if c>0 then Or (Le (CN 0 c (Neg (Floor (Neg r))))) (And (Eq (CN 0 c (Neg (Floor (Neg r))))) (Lt (Add (Floor (Neg r)) r))) 
-     else Or (Ge (CN 0 (-c) (Floor(Neg r)))) (And (Eq(CN 0 (-c) (Floor(Neg r)))) (Lt (Add (Floor (Neg r)) r))))"
-  "zlfm (Gt a) = (let (c,r) = zsplit0 a in 
-     if c=0 then Gt r else 
-     if c>0 then Or (Gt (CN 0 c (Floor r))) (And (Eq (CN 0 c (Floor r))) (Lt (Sub (Floor r) r))) 
-     else Or (Lt (CN 0 (-c) (Neg (Floor r)))) (And (Eq(CN 0 (-c) (Neg (Floor r)))) (Lt (Sub (Floor r) r))))"
-  "zlfm (Ge a) = (let (c,r) = zsplit0 a in 
-     if c=0 then Ge r else 
-     if c>0 then Or (Ge (CN 0 c (Floor r))) (And (Eq (CN 0 c (Floor r))) (Lt (Sub (Floor r) r))) 
-     else Or (Le (CN 0 (-c) (Neg (Floor r)))) (And (Eq(CN 0 (-c) (Neg (Floor r)))) (Lt (Sub (Floor r) r))))"
-  "zlfm (Eq a) = (let (c,r) = zsplit0 a in 
-              if c=0 then Eq r else 
-      if c>0 then (And (Eq (CN 0 c (Neg (Floor (Neg r))))) (Eq (Add (Floor (Neg r)) r)))
-      else (And (Eq (CN 0 (-c) (Floor (Neg r)))) (Eq (Add (Floor (Neg r)) r))))"
-  "zlfm (NEq a) = (let (c,r) = zsplit0 a in 
-              if c=0 then NEq r else 
-      if c>0 then (Or (NEq (CN 0 c (Neg (Floor (Neg r))))) (NEq (Add (Floor (Neg r)) r)))
-      else (Or (NEq (CN 0 (-c) (Floor (Neg r)))) (NEq (Add (Floor (Neg r)) r))))"
-  "zlfm (Dvd i a) = (if i=0 then zlfm (Eq a) 
-  else (let (c,r) = zsplit0 a in 
-              if c=0 then Dvd (abs i) r else 
-      if c>0 then And (Eq (Sub (Floor r) r)) (Dvd (abs i) (CN 0 c (Floor r))) 
-      else And (Eq (Sub (Floor r) r)) (Dvd (abs i) (CN 0 (-c) (Neg (Floor r))))))"
-  "zlfm (NDvd i a) = (if i=0 then zlfm (NEq a) 
-  else (let (c,r) = zsplit0 a in 
-              if c=0 then NDvd (abs i) r else 
-      if c>0 then Or (NEq (Sub (Floor r) r)) (NDvd (abs i) (CN 0 c (Floor r))) 
-      else Or (NEq (Sub (Floor r) r)) (NDvd (abs i) (CN 0 (-c) (Neg (Floor r))))))"
-  "zlfm (NOT (And p q)) = disj (zlfm (NOT p)) (zlfm (NOT q))"
-  "zlfm (NOT (Or p q)) = conj (zlfm (NOT p)) (zlfm (NOT q))"
-  "zlfm (NOT (Imp p q)) = conj (zlfm p) (zlfm (NOT q))"
-  "zlfm (NOT (Iff p q)) = disj (conj(zlfm p) (zlfm(NOT q))) (conj (zlfm(NOT p)) (zlfm q))"
-  "zlfm (NOT (NOT p)) = zlfm p"
-  "zlfm (NOT T) = F"
-  "zlfm (NOT F) = T"
-  "zlfm (NOT (Lt a)) = zlfm (Ge a)"
-  "zlfm (NOT (Le a)) = zlfm (Gt a)"
-  "zlfm (NOT (Gt a)) = zlfm (Le a)"
-  "zlfm (NOT (Ge a)) = zlfm (Lt a)"
-  "zlfm (NOT (Eq a)) = zlfm (NEq a)"
-  "zlfm (NOT (NEq a)) = zlfm (Eq a)"
-  "zlfm (NOT (Dvd i a)) = zlfm (NDvd i a)"
-  "zlfm (NOT (NDvd i a)) = zlfm (Dvd i a)"
-  "zlfm p = p" (hints simp add: fmsize_pos)
-
-lemma split_int_less_real: 
-  "(real (a::int) < b) = (a < floor b \<or> (a = floor b \<and> real (floor b) < b))"
-proof( auto)
-  assume alb: "real a < b" and agb: "\<not> a < floor b"
-  from agb have "floor b \<le> a" by simp hence th: "b < real a + 1" by (simp only: floor_le_eq)
-  from floor_eq[OF alb th] show "a= floor b" by simp 
-next
-  assume alb: "a < floor b"
-  hence "real a < real (floor b)" by simp
-  moreover have "real (floor b) \<le> b" by simp ultimately show  "real a < b" by arith 
-qed
-
-lemma split_int_less_real': 
-  "(real (a::int) + b < 0) = (real a - real (floor(-b)) < 0 \<or> (real a - real (floor (-b)) = 0 \<and> real (floor (-b)) + b < 0))"
-proof- 
-  have "(real a + b <0) = (real a < -b)" by arith
-  with split_int_less_real[where a="a" and b="-b"] show ?thesis by arith  
-qed
-
-lemma split_int_gt_real': 
-  "(real (a::int) + b > 0) = (real a + real (floor b) > 0 \<or> (real a + real (floor b) = 0 \<and> real (floor b) - b < 0))"
-proof- 
-  have th: "(real a + b >0) = (real (-a) + (-b)< 0)" by arith
-  show ?thesis using myless[rule_format, where b="real (floor b)"] 
-    by (simp only:th split_int_less_real'[where a="-a" and b="-b"]) 
-    (simp add: algebra_simps diff_def[symmetric],arith)
-qed
-
-lemma split_int_le_real: 
-  "(real (a::int) \<le> b) = (a \<le> floor b \<or> (a = floor b \<and> real (floor b) < b))"
-proof( auto)
-  assume alb: "real a \<le> b" and agb: "\<not> a \<le> floor b"
-  from alb have "floor (real a) \<le> floor b " by (simp only: floor_mono2) 
-  hence "a \<le> floor b" by simp with agb show "False" by simp
-next
-  assume alb: "a \<le> floor b"
-  hence "real a \<le> real (floor b)" by (simp only: floor_mono2)
-  also have "\<dots>\<le> b" by simp  finally show  "real a \<le> b" . 
-qed
-
-lemma split_int_le_real': 
-  "(real (a::int) + b \<le> 0) = (real a - real (floor(-b)) \<le> 0 \<or> (real a - real (floor (-b)) = 0 \<and> real (floor (-b)) + b < 0))"
-proof- 
-  have "(real a + b \<le>0) = (real a \<le> -b)" by arith
-  with split_int_le_real[where a="a" and b="-b"] show ?thesis by arith  
-qed
-
-lemma split_int_ge_real': 
-  "(real (a::int) + b \<ge> 0) = (real a + real (floor b) \<ge> 0 \<or> (real a + real (floor b) = 0 \<and> real (floor b) - b < 0))"
-proof- 
-  have th: "(real a + b \<ge>0) = (real (-a) + (-b) \<le> 0)" by arith
-  show ?thesis by (simp only: th split_int_le_real'[where a="-a" and b="-b"])
-    (simp add: algebra_simps diff_def[symmetric],arith)
-qed
-
-lemma split_int_eq_real: "(real (a::int) = b) = ( a = floor b \<and> b = real (floor b))" (is "?l = ?r")
-by auto
-
-lemma split_int_eq_real': "(real (a::int) + b = 0) = ( a - floor (-b) = 0 \<and> real (floor (-b)) + b = 0)" (is "?l = ?r")
-proof-
-  have "?l = (real a = -b)" by arith
-  with split_int_eq_real[where a="a" and b="-b"] show ?thesis by simp arith
-qed
-
-lemma zlfm_I:
-  assumes qfp: "qfree p"
-  shows "(Ifm (real i #bs) (zlfm p) = Ifm (real i# bs) p) \<and> iszlfm (zlfm p) (real (i::int) #bs)"
-  (is "(?I (?l p) = ?I p) \<and> ?L (?l p)")
-using qfp
-proof(induct p rule: zlfm.induct)
-  case (5 a) 
-  let ?c = "fst (zsplit0 a)"
-  let ?r = "snd (zsplit0 a)"
-  have spl: "zsplit0 a = (?c,?r)" by simp
-  from zsplit0_I[OF spl, where x="i" and bs="bs"] 
-  have Ia:"Inum (real i # bs) a = Inum (real i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto 
-  let ?N = "\<lambda> t. Inum (real i#bs) t"
-  have "?c = 0 \<or> (?c >0 \<and> ?c\<noteq>0) \<or> (?c<0 \<and> ?c\<noteq>0)" by arith
-  moreover
-  {assume "?c=0" hence ?case using zsplit0_I[OF spl, where x="i" and bs="bs"] 
-      by (cases "?r", simp_all add: Let_def split_def,case_tac "nat", simp_all)}
-  moreover
-  {assume cp: "?c > 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (Lt a))" 
-      by (simp add: nb Let_def split_def isint_Floor isint_neg)
-    have "?I (Lt a) = (real (?c * i) + (?N ?r) < 0)" using Ia by (simp add: Let_def split_def)
-    also have "\<dots> = (?I (?l (Lt a)))" apply (simp only: split_int_less_real'[where a="?c*i" and b="?N ?r"]) by (simp add: Ia cp cnz Let_def split_def diff_def)
-    finally have ?case using l by simp}
-  moreover
-  {assume cn: "?c < 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (Lt a))" 
-      by (simp add: nb Let_def split_def isint_Floor isint_neg)
-    have "?I (Lt a) = (real (?c * i) + (?N ?r) < 0)" using Ia by (simp add: Let_def split_def)
-    also from cn cnz have "\<dots> = (?I (?l (Lt a)))" by (simp only: split_int_less_real'[where a="?c*i" and b="?N ?r"]) (simp add: Ia Let_def split_def diff_def[symmetric] add_ac, arith)
-    finally have ?case using l by simp}
-  ultimately show ?case by blast
-next
-  case (6 a)
-  let ?c = "fst (zsplit0 a)"
-  let ?r = "snd (zsplit0 a)"
-  have spl: "zsplit0 a = (?c,?r)" by simp
-  from zsplit0_I[OF spl, where x="i" and bs="bs"] 
-  have Ia:"Inum (real i # bs) a = Inum (real i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto 
-  let ?N = "\<lambda> t. Inum (real i#bs) t"
-  have "?c = 0 \<or> (?c >0 \<and> ?c\<noteq>0) \<or> (?c<0 \<and> ?c\<noteq>0)" by arith
-  moreover
-  {assume "?c=0" hence ?case using zsplit0_I[OF spl, where x="i" and bs="bs"] 
-      by (cases "?r", simp_all add: Let_def split_def, case_tac "nat",simp_all)}
-  moreover
-  {assume cp: "?c > 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (Le a))" 
-      by (simp add: nb Let_def split_def isint_Floor isint_neg)
-    have "?I (Le a) = (real (?c * i) + (?N ?r) \<le> 0)" using Ia by (simp add: Let_def split_def)
-    also have "\<dots> = (?I (?l (Le a)))" by (simp only: split_int_le_real'[where a="?c*i" and b="?N ?r"]) (simp add: Ia cp cnz Let_def split_def diff_def)
-    finally have ?case using l by simp}
-  moreover
-  {assume cn: "?c < 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (Le a))" 
-      by (simp add: nb Let_def split_def isint_Floor isint_neg)
-    have "?I (Le a) = (real (?c * i) + (?N ?r) \<le> 0)" using Ia by (simp add: Let_def split_def)
-    also from cn cnz have "\<dots> = (?I (?l (Le a)))" by (simp only: split_int_le_real'[where a="?c*i" and b="?N ?r"]) (simp add: Ia Let_def split_def diff_def[symmetric] add_ac ,arith)
-    finally have ?case using l by simp}
-  ultimately show ?case by blast
-next
-  case (7 a) 
-  let ?c = "fst (zsplit0 a)"
-  let ?r = "snd (zsplit0 a)"
-  have spl: "zsplit0 a = (?c,?r)" by simp
-  from zsplit0_I[OF spl, where x="i" and bs="bs"] 
-  have Ia:"Inum (real i # bs) a = Inum (real i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto 
-  let ?N = "\<lambda> t. Inum (real i#bs) t"
-  have "?c = 0 \<or> (?c >0 \<and> ?c\<noteq>0) \<or> (?c<0 \<and> ?c\<noteq>0)" by arith
-  moreover
-  {assume "?c=0" hence ?case using zsplit0_I[OF spl, where x="i" and bs="bs"] 
-      by (cases "?r", simp_all add: Let_def split_def, case_tac "nat", simp_all)}
-  moreover
-  {assume cp: "?c > 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (Gt a))" 
-      by (simp add: nb Let_def split_def isint_Floor isint_neg)
-    have "?I (Gt a) = (real (?c * i) + (?N ?r) > 0)" using Ia by (simp add: Let_def split_def)
-    also have "\<dots> = (?I (?l (Gt a)))" by (simp only: split_int_gt_real'[where a="?c*i" and b="?N ?r"]) (simp add: Ia cp cnz Let_def split_def diff_def)
-    finally have ?case using l by simp}
-  moreover
-  {assume cn: "?c < 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (Gt a))" 
-      by (simp add: nb Let_def split_def isint_Floor isint_neg)
-    have "?I (Gt a) = (real (?c * i) + (?N ?r) > 0)" using Ia by (simp add: Let_def split_def)
-    also from cn cnz have "\<dots> = (?I (?l (Gt a)))" by (simp only: split_int_gt_real'[where a="?c*i" and b="?N ?r"]) (simp add: Ia Let_def split_def diff_def[symmetric] add_ac, arith)
-    finally have ?case using l by simp}
-  ultimately show ?case by blast
-next
-  case (8 a)
-   let ?c = "fst (zsplit0 a)"
-  let ?r = "snd (zsplit0 a)"
-  have spl: "zsplit0 a = (?c,?r)" by simp
-  from zsplit0_I[OF spl, where x="i" and bs="bs"] 
-  have Ia:"Inum (real i # bs) a = Inum (real i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto 
-  let ?N = "\<lambda> t. Inum (real i#bs) t"
-  have "?c = 0 \<or> (?c >0 \<and> ?c\<noteq>0) \<or> (?c<0 \<and> ?c\<noteq>0)" by arith
-  moreover
-  {assume "?c=0" hence ?case using zsplit0_I[OF spl, where x="i" and bs="bs"] 
-      by (cases "?r", simp_all add: Let_def split_def, case_tac "nat", simp_all)}
-  moreover
-  {assume cp: "?c > 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (Ge a))" 
-      by (simp add: nb Let_def split_def isint_Floor isint_neg)
-    have "?I (Ge a) = (real (?c * i) + (?N ?r) \<ge> 0)" using Ia by (simp add: Let_def split_def)
-    also have "\<dots> = (?I (?l (Ge a)))" by (simp only: split_int_ge_real'[where a="?c*i" and b="?N ?r"]) (simp add: Ia cp cnz Let_def split_def diff_def)
-    finally have ?case using l by simp}
-  moreover
-  {assume cn: "?c < 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (Ge a))" 
-      by (simp add: nb Let_def split_def isint_Floor isint_neg)
-    have "?I (Ge a) = (real (?c * i) + (?N ?r) \<ge> 0)" using Ia by (simp add: Let_def split_def)
-    also from cn cnz have "\<dots> = (?I (?l (Ge a)))" by (simp only: split_int_ge_real'[where a="?c*i" and b="?N ?r"]) (simp add: Ia Let_def split_def diff_def[symmetric] add_ac, arith)
-    finally have ?case using l by simp}
-  ultimately show ?case by blast
-next
-  case (9 a)
-  let ?c = "fst (zsplit0 a)"
-  let ?r = "snd (zsplit0 a)"
-  have spl: "zsplit0 a = (?c,?r)" by simp
-  from zsplit0_I[OF spl, where x="i" and bs="bs"] 
-  have Ia:"Inum (real i # bs) a = Inum (real i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto 
-  let ?N = "\<lambda> t. Inum (real i#bs) t"
-  have "?c = 0 \<or> (?c >0 \<and> ?c\<noteq>0) \<or> (?c<0 \<and> ?c\<noteq>0)" by arith
-  moreover
-  {assume "?c=0" hence ?case using zsplit0_I[OF spl, where x="i" and bs="bs"] 
-      by (cases "?r", simp_all add: Let_def split_def, case_tac "nat", simp_all)}
-  moreover
-  {assume cp: "?c > 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (Eq a))" 
-      by (simp add: nb Let_def split_def isint_Floor isint_neg)
-    have "?I (Eq a) = (real (?c * i) + (?N ?r) = 0)" using Ia by (simp add: Let_def split_def)
-    also have "\<dots> = (?I (?l (Eq a)))" using cp cnz  by (simp only: split_int_eq_real'[where a="?c*i" and b="?N ?r"]) (simp add: Let_def split_def Ia real_of_int_mult[symmetric] del: real_of_int_mult)
-    finally have ?case using l by simp}
-  moreover
-  {assume cn: "?c < 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (Eq a))" 
-      by (simp add: nb Let_def split_def isint_Floor isint_neg)
-    have "?I (Eq a) = (real (?c * i) + (?N ?r) = 0)" using Ia by (simp add: Let_def split_def)
-    also from cn cnz have "\<dots> = (?I (?l (Eq a)))" by (simp only: split_int_eq_real'[where a="?c*i" and b="?N ?r"]) (simp add: Let_def split_def Ia real_of_int_mult[symmetric] del: real_of_int_mult,arith)
-    finally have ?case using l by simp}
-  ultimately show ?case by blast
-next
-  case (10 a)
-  let ?c = "fst (zsplit0 a)"
-  let ?r = "snd (zsplit0 a)"
-  have spl: "zsplit0 a = (?c,?r)" by simp
-  from zsplit0_I[OF spl, where x="i" and bs="bs"] 
-  have Ia:"Inum (real i # bs) a = Inum (real i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto 
-  let ?N = "\<lambda> t. Inum (real i#bs) t"
-  have "?c = 0 \<or> (?c >0 \<and> ?c\<noteq>0) \<or> (?c<0 \<and> ?c\<noteq>0)" by arith
-  moreover
-  {assume "?c=0" hence ?case using zsplit0_I[OF spl, where x="i" and bs="bs"] 
-      by (cases "?r", simp_all add: Let_def split_def, case_tac "nat", simp_all)}
-  moreover
-  {assume cp: "?c > 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (NEq a))" 
-      by (simp add: nb Let_def split_def isint_Floor isint_neg)
-    have "?I (NEq a) = (real (?c * i) + (?N ?r) \<noteq> 0)" using Ia by (simp add: Let_def split_def)
-    also have "\<dots> = (?I (?l (NEq a)))" using cp cnz  by (simp only: split_int_eq_real'[where a="?c*i" and b="?N ?r"]) (simp add: Let_def split_def Ia real_of_int_mult[symmetric] del: real_of_int_mult)
-    finally have ?case using l by simp}
-  moreover
-  {assume cn: "?c < 0" and cnz: "?c\<noteq>0" hence l: "?L (?l (NEq a))" 
-      by (simp add: nb Let_def split_def isint_Floor isint_neg)
-    have "?I (NEq a) = (real (?c * i) + (?N ?r) \<noteq> 0)" using Ia by (simp add: Let_def split_def)
-    also from cn cnz have "\<dots> = (?I (?l (NEq a)))" by (simp only: split_int_eq_real'[where a="?c*i" and b="?N ?r"]) (simp add: Let_def split_def Ia real_of_int_mult[symmetric] del: real_of_int_mult,arith)
-    finally have ?case using l by simp}
-  ultimately show ?case by blast
-next
-  case (11 j a)
-  let ?c = "fst (zsplit0 a)"
-  let ?r = "snd (zsplit0 a)"
-  have spl: "zsplit0 a = (?c,?r)" by simp
-  from zsplit0_I[OF spl, where x="i" and bs="bs"] 
-  have Ia:"Inum (real i # bs) a = Inum (real i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto 
-  let ?N = "\<lambda> t. Inum (real i#bs) t"
-  have "j=0 \<or> (j\<noteq>0 \<and> ?c = 0) \<or> (j\<noteq>0 \<and> ?c >0 \<and> ?c\<noteq>0) \<or> (j\<noteq> 0 \<and> ?c<0 \<and> ?c\<noteq>0)" by arith
-  moreover
-  {assume "j=0" hence z: "zlfm (Dvd j a) = (zlfm (Eq a))" by (simp add: Let_def) 
-    hence ?case using prems by (simp del: zlfm.simps add: rdvd_left_0_eq)}
-  moreover
-  {assume "?c=0" and "j\<noteq>0" hence ?case 
-      using zsplit0_I[OF spl, where x="i" and bs="bs"] rdvd_abs1[where d="j"]
-      by (cases "?r", simp_all add: Let_def split_def, case_tac "nat", simp_all)}
-  moreover
-  {assume cp: "?c > 0" and cnz: "?c\<noteq>0" and jnz: "j\<noteq>0" hence l: "?L (?l (Dvd j a))" 
-      by (simp add: nb Let_def split_def isint_Floor isint_neg)
-    have "?I (Dvd j a) = (real j rdvd (real (?c * i) + (?N ?r)))" 
-      using Ia by (simp add: Let_def split_def)
-    also have "\<dots> = (real (abs j) rdvd real (?c*i) + (?N ?r))" 
-      by (simp only: rdvd_abs1[where d="j" and t="real (?c*i) + ?N ?r", symmetric]) simp
-    also have "\<dots> = ((abs j) dvd (floor ((?N ?r) + real (?c*i))) \<and> 
-       (real (floor ((?N ?r) + real (?c*i))) = (real (?c*i) + (?N ?r))))" 
-      by(simp only: int_rdvd_real[where i="abs j" and x="real (?c*i) + (?N ?r)"]) (simp only: add_ac)
-    also have "\<dots> = (?I (?l (Dvd j a)))" using cp cnz jnz  
-      by (simp add: Let_def split_def int_rdvd_iff[symmetric]  
-	del: real_of_int_mult) (auto simp add: add_ac)
-    finally have ?case using l jnz  by simp }
-  moreover
-  {assume cn: "?c < 0" and cnz: "?c\<noteq>0" and jnz: "j\<noteq>0" hence l: "?L (?l (Dvd j a))" 
-      by (simp add: nb Let_def split_def isint_Floor isint_neg)
-    have "?I (Dvd j a) = (real j rdvd (real (?c * i) + (?N ?r)))" 
-      using Ia by (simp add: Let_def split_def)
-    also have "\<dots> = (real (abs j) rdvd real (?c*i) + (?N ?r))" 
-      by (simp only: rdvd_abs1[where d="j" and t="real (?c*i) + ?N ?r", symmetric]) simp
-    also have "\<dots> = ((abs j) dvd (floor ((?N ?r) + real (?c*i))) \<and> 
-       (real (floor ((?N ?r) + real (?c*i))) = (real (?c*i) + (?N ?r))))" 
-      by(simp only: int_rdvd_real[where i="abs j" and x="real (?c*i) + (?N ?r)"]) (simp only: add_ac)
-    also have "\<dots> = (?I (?l (Dvd j a)))" using cn cnz jnz
-      using rdvd_minus [where d="abs j" and t="real (?c*i + floor (?N ?r))", simplified, symmetric]
-      by (simp add: Let_def split_def int_rdvd_iff[symmetric]  
-	del: real_of_int_mult) (auto simp add: add_ac)
-    finally have ?case using l jnz by blast }
-  ultimately show ?case by blast
-next
-  case (12 j a)
-  let ?c = "fst (zsplit0 a)"
-  let ?r = "snd (zsplit0 a)"
-  have spl: "zsplit0 a = (?c,?r)" by simp
-  from zsplit0_I[OF spl, where x="i" and bs="bs"] 
-  have Ia:"Inum (real i # bs) a = Inum (real i #bs) (CN 0 ?c ?r)" and nb: "numbound0 ?r" by auto 
-  let ?N = "\<lambda> t. Inum (real i#bs) t"
-  have "j=0 \<or> (j\<noteq>0 \<and> ?c = 0) \<or> (j\<noteq>0 \<and> ?c >0 \<and> ?c\<noteq>0) \<or> (j\<noteq> 0 \<and> ?c<0 \<and> ?c\<noteq>0)" by arith
-  moreover
-  {assume "j=0" hence z: "zlfm (NDvd j a) = (zlfm (NEq a))" by (simp add: Let_def) 
-    hence ?case using prems by (simp del: zlfm.simps add: rdvd_left_0_eq)}
-  moreover
-  {assume "?c=0" and "j\<noteq>0" hence ?case 
-      using zsplit0_I[OF spl, where x="i" and bs="bs"] rdvd_abs1[where d="j"]
-      by (cases "?r", simp_all add: Let_def split_def, case_tac "nat", simp_all)}
-  moreover
-  {assume cp: "?c > 0" and cnz: "?c\<noteq>0" and jnz: "j\<noteq>0" hence l: "?L (?l (NDvd j a))" 
-      by (simp add: nb Let_def split_def isint_Floor isint_neg)
-    have "?I (NDvd j a) = (\<not> (real j rdvd (real (?c * i) + (?N ?r))))" 
-      using Ia by (simp add: Let_def split_def)
-    also have "\<dots> = (\<not> (real (abs j) rdvd real (?c*i) + (?N ?r)))" 
-      by (simp only: rdvd_abs1[where d="j" and t="real (?c*i) + ?N ?r", symmetric]) simp
-    also have "\<dots> = (\<not> ((abs j) dvd (floor ((?N ?r) + real (?c*i))) \<and> 
-       (real (floor ((?N ?r) + real (?c*i))) = (real (?c*i) + (?N ?r)))))" 
-      by(simp only: int_rdvd_real[where i="abs j" and x="real (?c*i) + (?N ?r)"]) (simp only: add_ac)
-    also have "\<dots> = (?I (?l (NDvd j a)))" using cp cnz jnz  
-      by (simp add: Let_def split_def int_rdvd_iff[symmetric]  
-	del: real_of_int_mult) (auto simp add: add_ac)
-    finally have ?case using l jnz  by simp }
-  moreover
-  {assume cn: "?c < 0" and cnz: "?c\<noteq>0" and jnz: "j\<noteq>0" hence l: "?L (?l (NDvd j a))" 
-      by (simp add: nb Let_def split_def isint_Floor isint_neg)
-    have "?I (NDvd j a) = (\<not> (real j rdvd (real (?c * i) + (?N ?r))))" 
-      using Ia by (simp add: Let_def split_def)
-    also have "\<dots> = (\<not> (real (abs j) rdvd real (?c*i) + (?N ?r)))" 
-      by (simp only: rdvd_abs1[where d="j" and t="real (?c*i) + ?N ?r", symmetric]) simp
-    also have "\<dots> = (\<not> ((abs j) dvd (floor ((?N ?r) + real (?c*i))) \<and> 
-       (real (floor ((?N ?r) + real (?c*i))) = (real (?c*i) + (?N ?r)))))" 
-      by(simp only: int_rdvd_real[where i="abs j" and x="real (?c*i) + (?N ?r)"]) (simp only: add_ac)
-    also have "\<dots> = (?I (?l (NDvd j a)))" using cn cnz jnz
-      using rdvd_minus [where d="abs j" and t="real (?c*i + floor (?N ?r))", simplified, symmetric]
-      by (simp add: Let_def split_def int_rdvd_iff[symmetric]  
-	del: real_of_int_mult) (auto simp add: add_ac)
-    finally have ?case using l jnz by blast }
-  ultimately show ?case by blast
-qed auto
-
-text{* plusinf : Virtual substitution of @{text "+\<infinity>"}
-       minusinf: Virtual substitution of @{text "-\<infinity>"}
-       @{text "\<delta>"} Compute lcm @{text "d| Dvd d  c*x+t \<in> p"}
-       @{text "d\<delta>"} checks if a given l divides all the ds above*}
-
-consts 
-  plusinf:: "fm \<Rightarrow> fm" 
-  minusinf:: "fm \<Rightarrow> fm"
-  \<delta> :: "fm \<Rightarrow> int" 
-  d\<delta> :: "fm \<Rightarrow> int \<Rightarrow> bool"
-
-recdef minusinf "measure size"
-  "minusinf (And p q) = conj (minusinf p) (minusinf q)" 
-  "minusinf (Or p q) = disj (minusinf p) (minusinf q)" 
-  "minusinf (Eq  (CN 0 c e)) = F"
-  "minusinf (NEq (CN 0 c e)) = T"
-  "minusinf (Lt  (CN 0 c e)) = T"
-  "minusinf (Le  (CN 0 c e)) = T"
-  "minusinf (Gt  (CN 0 c e)) = F"
-  "minusinf (Ge  (CN 0 c e)) = F"
-  "minusinf p = p"
-
-lemma minusinf_qfree: "qfree p \<Longrightarrow> qfree (minusinf p)"
-  by (induct p rule: minusinf.induct, auto)
-
-recdef plusinf "measure size"
-  "plusinf (And p q) = conj (plusinf p) (plusinf q)" 
-  "plusinf (Or p q) = disj (plusinf p) (plusinf q)" 
-  "plusinf (Eq  (CN 0 c e)) = F"
-  "plusinf (NEq (CN 0 c e)) = T"
-  "plusinf (Lt  (CN 0 c e)) = F"
-  "plusinf (Le  (CN 0 c e)) = F"
-  "plusinf (Gt  (CN 0 c e)) = T"
-  "plusinf (Ge  (CN 0 c e)) = T"
-  "plusinf p = p"
-
-recdef \<delta> "measure size"
-  "\<delta> (And p q) = zlcm (\<delta> p) (\<delta> q)" 
-  "\<delta> (Or p q) = zlcm (\<delta> p) (\<delta> q)" 
-  "\<delta> (Dvd i (CN 0 c e)) = i"
-  "\<delta> (NDvd i (CN 0 c e)) = i"
-  "\<delta> p = 1"
-
-recdef d\<delta> "measure size"
-  "d\<delta> (And p q) = (\<lambda> d. d\<delta> p d \<and> d\<delta> q d)" 
-  "d\<delta> (Or p q) = (\<lambda> d. d\<delta> p d \<and> d\<delta> q d)" 
-  "d\<delta> (Dvd i (CN 0 c e)) = (\<lambda> d. i dvd d)"
-  "d\<delta> (NDvd i (CN 0 c e)) = (\<lambda> d. i dvd d)"
-  "d\<delta> p = (\<lambda> d. True)"
-
-lemma delta_mono: 
-  assumes lin: "iszlfm p bs"
-  and d: "d dvd d'"
-  and ad: "d\<delta> p d"
-  shows "d\<delta> p d'"
-  using lin ad d
-proof(induct p rule: iszlfm.induct)
-  case (9 i c e)  thus ?case using d
-    by (simp add: zdvd_trans[where m="i" and n="d" and k="d'"])
-next
-  case (10 i c e) thus ?case using d
-    by (simp add: zdvd_trans[where m="i" and n="d" and k="d'"])
-qed simp_all
-
-lemma \<delta> : assumes lin:"iszlfm p bs"
-  shows "d\<delta> p (\<delta> p) \<and> \<delta> p >0"
-using lin
-proof (induct p rule: iszlfm.induct)
-  case (1 p q) 
-  let ?d = "\<delta> (And p q)"
-  from prems zlcm_pos have dp: "?d >0" by simp
-  have d1: "\<delta> p dvd \<delta> (And p q)" using prems by simp 
-   hence th: "d\<delta> p ?d" 
-     using delta_mono prems by (auto simp del: dvd_zlcm_self1)
-  have "\<delta> q dvd \<delta> (And p q)" using prems  by simp 
-  hence th': "d\<delta> q ?d" using delta_mono prems by (auto simp del: dvd_zlcm_self2)
-  from th th' dp show ?case by simp 
-next
-  case (2 p q)  
-  let ?d = "\<delta> (And p q)"
-  from prems zlcm_pos have dp: "?d >0" by simp
-  have "\<delta> p dvd \<delta> (And p q)" using prems by simp hence th: "d\<delta> p ?d" using delta_mono prems 
-    by (auto simp del: dvd_zlcm_self1)
-  have "\<delta> q dvd \<delta> (And p q)" using prems by simp hence th': "d\<delta> q ?d" using delta_mono prems by (auto simp del: dvd_zlcm_self2)
-  from th th' dp show ?case by simp 
-qed simp_all
-
-
-lemma minusinf_inf:
-  assumes linp: "iszlfm p (a # bs)"
-  shows "\<exists> (z::int). \<forall> x < z. Ifm ((real x)#bs) (minusinf p) = Ifm ((real x)#bs) p"
-  (is "?P p" is "\<exists> (z::int). \<forall> x < z. ?I x (?M p) = ?I x p")
-using linp
-proof (induct p rule: minusinf.induct)
-  case (1 f g)
-  from prems have "?P f" by simp
-  then obtain z1 where z1_def: "\<forall> x < z1. ?I x (?M f) = ?I x f" by blast
-  from prems have "?P g" by simp
-  then obtain z2 where z2_def: "\<forall> x < z2. ?I x (?M g) = ?I x g" by blast
-  let ?z = "min z1 z2"
-  from z1_def z2_def have "\<forall> x < ?z. ?I x (?M (And f g)) = ?I x (And f g)" by simp
-  thus ?case by blast
-next
-  case (2 f g)   from prems have "?P f" by simp
-  then obtain z1 where z1_def: "\<forall> x < z1. ?I x (?M f) = ?I x f" by blast
-  from prems have "?P g" by simp
-  then obtain z2 where z2_def: "\<forall> x < z2. ?I x (?M g) = ?I x g" by blast
-  let ?z = "min z1 z2"
-  from z1_def z2_def have "\<forall> x < ?z. ?I x (?M (Or f g)) = ?I x (Or f g)" by simp
-  thus ?case by blast
-next
-  case (3 c e) 
-  from prems have "c > 0" by simp hence rcpos: "real c > 0" by simp
-  from prems have nbe: "numbound0 e" by simp
-  fix y
-  have "\<forall> x < (floor (- (Inum (y#bs) e) / (real c))). ?I x (?M (Eq (CN 0 c e))) = ?I x (Eq (CN 0 c e))"
-  proof (simp add: less_floor_eq , rule allI, rule impI) 
-    fix x
-    assume A: "real x + (1\<Colon>real) \<le> - (Inum (y # bs) e / real c)"
-    hence th1:"real x < - (Inum (y # bs) e / real c)" by simp
-    with rcpos  have "(real c)*(real  x) < (real c)*(- (Inum (y # bs) e / real c))"
-      by (simp only:  real_mult_less_mono2[OF rcpos th1])
-    hence "real c * real x + Inum (y # bs) e \<noteq> 0"using rcpos  by simp
-    thus "real c * real x + Inum (real x # bs) e \<noteq> 0" 
-      using numbound0_I[OF nbe, where b="y" and bs="bs" and b'="real x"]  by simp
-  qed
-  thus ?case by blast
-next
-  case (4 c e) 
-  from prems have "c > 0" by simp hence rcpos: "real c > 0" by simp
-  from prems have nbe: "numbound0 e" by simp
-  fix y
-  have "\<forall> x < (floor (- (Inum (y#bs) e) / (real c))). ?I x (?M (NEq (CN 0 c e))) = ?I x (NEq (CN 0 c e))"
-  proof (simp add: less_floor_eq , rule allI, rule impI) 
-    fix x
-    assume A: "real x + (1\<Colon>real) \<le> - (Inum (y # bs) e / real c)"
-    hence th1:"real x < - (Inum (y # bs) e / real c)" by simp
-    with rcpos  have "(real c)*(real  x) < (real c)*(- (Inum (y # bs) e / real c))"
-      by (simp only:  real_mult_less_mono2[OF rcpos th1])
-    hence "real c * real x + Inum (y # bs) e \<noteq> 0"using rcpos  by simp
-    thus "real c * real x + Inum (real x # bs) e \<noteq> 0" 
-      using numbound0_I[OF nbe, where b="y" and bs="bs" and b'="real x"]  by simp
-  qed
-  thus ?case by blast
-next
-  case (5 c e) 
-  from prems have "c > 0" by simp hence rcpos: "real c > 0" by simp
-  from prems have nbe: "numbound0 e" by simp
-  fix y
-  have "\<forall> x < (floor (- (Inum (y#bs) e) / (real c))). ?I x (?M (Lt (CN 0 c e))) = ?I x (Lt (CN 0 c e))"
-  proof (simp add: less_floor_eq , rule allI, rule impI) 
-    fix x
-    assume A: "real x + (1\<Colon>real) \<le> - (Inum (y # bs) e / real c)"
-    hence th1:"real x < - (Inum (y # bs) e / real c)" by simp
-    with rcpos  have "(real c)*(real  x) < (real c)*(- (Inum (y # bs) e / real c))"
-      by (simp only:  real_mult_less_mono2[OF rcpos th1])
-    thus "real c * real x + Inum (real x # bs) e < 0" 
-      using numbound0_I[OF nbe, where b="y" and bs="bs" and b'="real x"] rcpos by simp
-  qed
-  thus ?case by blast
-next
-  case (6 c e) 
-  from prems have "c > 0" by simp hence rcpos: "real c > 0" by simp
-  from prems have nbe: "numbound0 e" by simp
-  fix y
-  have "\<forall> x < (floor (- (Inum (y#bs) e) / (real c))). ?I x (?M (Le (CN 0 c e))) = ?I x (Le (CN 0 c e))"
-  proof (simp add: less_floor_eq , rule allI, rule impI) 
-    fix x
-    assume A: "real x + (1\<Colon>real) \<le> - (Inum (y # bs) e / real c)"
-    hence th1:"real x < - (Inum (y # bs) e / real c)" by simp
-    with rcpos  have "(real c)*(real  x) < (real c)*(- (Inum (y # bs) e / real c))"
-      by (simp only:  real_mult_less_mono2[OF rcpos th1])
-    thus "real c * real x + Inum (real x # bs) e \<le> 0" 
-      using numbound0_I[OF nbe, where b="y" and bs="bs" and b'="real x"] rcpos by simp
-  qed
-  thus ?case by blast
-next
-  case (7 c e) 
-  from prems have "c > 0" by simp hence rcpos: "real c > 0" by simp
-  from prems have nbe: "numbound0 e" by simp
-  fix y
-  have "\<forall> x < (floor (- (Inum (y#bs) e) / (real c))). ?I x (?M (Gt (CN 0 c e))) = ?I x (Gt (CN 0 c e))"
-  proof (simp add: less_floor_eq , rule allI, rule impI) 
-    fix x
-    assume A: "real x + (1\<Colon>real) \<le> - (Inum (y # bs) e / real c)"
-    hence th1:"real x < - (Inum (y # bs) e / real c)" by simp
-    with rcpos  have "(real c)*(real  x) < (real c)*(- (Inum (y # bs) e / real c))"
-      by (simp only:  real_mult_less_mono2[OF rcpos th1])
-    thus "\<not> (real c * real x + Inum (real x # bs) e>0)" 
-      using numbound0_I[OF nbe, where b="y" and bs="bs" and b'="real x"] rcpos by simp
-  qed
-  thus ?case by blast
-next
-  case (8 c e) 
-  from prems have "c > 0" by simp hence rcpos: "real c > 0" by simp
-  from prems have nbe: "numbound0 e" by simp
-  fix y
-  have "\<forall> x < (floor (- (Inum (y#bs) e) / (real c))). ?I x (?M (Ge (CN 0 c e))) = ?I x (Ge (CN 0 c e))"
-  proof (simp add: less_floor_eq , rule allI, rule impI) 
-    fix x
-    assume A: "real x + (1\<Colon>real) \<le> - (Inum (y # bs) e / real c)"
-    hence th1:"real x < - (Inum (y # bs) e / real c)" by simp
-    with rcpos  have "(real c)*(real  x) < (real c)*(- (Inum (y # bs) e / real c))"
-      by (simp only:  real_mult_less_mono2[OF rcpos th1])
-    thus "\<not> real c * real x + Inum (real x # bs) e \<ge> 0" 
-      using numbound0_I[OF nbe, where b="y" and bs="bs" and b'="real x"] rcpos by simp
-  qed
-  thus ?case by blast
-qed simp_all
-
-lemma minusinf_repeats:
-  assumes d: "d\<delta> p d" and linp: "iszlfm p (a # bs)"
-  shows "Ifm ((real(x - k*d))#bs) (minusinf p) = Ifm (real x #bs) (minusinf p)"
-using linp d
-proof(induct p rule: iszlfm.induct) 
-  case (9 i c e) hence nbe: "numbound0 e"  and id: "i dvd d" by simp+
-    hence "\<exists> k. d=i*k" by (simp add: dvd_def)
-    then obtain "di" where di_def: "d=i*di" by blast
-    show ?case 
-    proof(simp add: numbound0_I[OF nbe,where bs="bs" and b="real x - real k * real d" and b'="real x"] right_diff_distrib, rule iffI)
-      assume 
-	"real i rdvd real c * real x - real c * (real k * real d) + Inum (real x # bs) e"
-      (is "?ri rdvd ?rc*?rx - ?rc*(?rk*?rd) + ?I x e" is "?ri rdvd ?rt")
-      hence "\<exists> (l::int). ?rt = ?ri * (real l)" by (simp add: rdvd_def)
-      hence "\<exists> (l::int). ?rc*?rx+ ?I x e = ?ri*(real l)+?rc*(?rk * (real i) * (real di))" 
-	by (simp add: algebra_simps di_def)
-      hence "\<exists> (l::int). ?rc*?rx+ ?I x e = ?ri*(real (l + c*k*di))"
-	by (simp add: algebra_simps)
-      hence "\<exists> (l::int). ?rc*?rx+ ?I x e = ?ri* (real l)" by blast
-      thus "real i rdvd real c * real x + Inum (real x # bs) e" using rdvd_def by simp
-    next
-      assume 
-	"real i rdvd real c * real x + Inum (real x # bs) e" (is "?ri rdvd ?rc*?rx+?e")
-      hence "\<exists> (l::int). ?rc*?rx+?e = ?ri * (real l)" by (simp add: rdvd_def)
-      hence "\<exists> (l::int). ?rc*?rx - real c * (real k * real d) +?e = ?ri * (real l) - real c * (real k * real d)" by simp
-      hence "\<exists> (l::int). ?rc*?rx - real c * (real k * real d) +?e = ?ri * (real l) - real c * (real k * real i * real di)" by (simp add: di_def)
-      hence "\<exists> (l::int). ?rc*?rx - real c * (real k * real d) +?e = ?ri * (real (l - c*k*di))" by (simp add: algebra_simps)
-      hence "\<exists> (l::int). ?rc*?rx - real c * (real k * real d) +?e = ?ri * (real l)"
-	by blast
-      thus "real i rdvd real c * real x - real c * (real k * real d) + Inum (real x # bs) e" using rdvd_def by simp
-    qed
-next
-  case (10 i c e) hence nbe: "numbound0 e"  and id: "i dvd d" by simp+
-    hence "\<exists> k. d=i*k" by (simp add: dvd_def)
-    then obtain "di" where di_def: "d=i*di" by blast
-    show ?case 
-    proof(simp add: numbound0_I[OF nbe,where bs="bs" and b="real x - real k * real d" and b'="real x"] right_diff_distrib, rule iffI)
-      assume 
-	"real i rdvd real c * real x - real c * (real k * real d) + Inum (real x # bs) e"
-      (is "?ri rdvd ?rc*?rx - ?rc*(?rk*?rd) + ?I x e" is "?ri rdvd ?rt")
-      hence "\<exists> (l::int). ?rt = ?ri * (real l)" by (simp add: rdvd_def)
-      hence "\<exists> (l::int). ?rc*?rx+ ?I x e = ?ri*(real l)+?rc*(?rk * (real i) * (real di))" 
-	by (simp add: algebra_simps di_def)
-      hence "\<exists> (l::int). ?rc*?rx+ ?I x e = ?ri*(real (l + c*k*di))"
-	by (simp add: algebra_simps)
-      hence "\<exists> (l::int). ?rc*?rx+ ?I x e = ?ri* (real l)" by blast
-      thus "real i rdvd real c * real x + Inum (real x # bs) e" using rdvd_def by simp
-    next
-      assume 
-	"real i rdvd real c * real x + Inum (real x # bs) e" (is "?ri rdvd ?rc*?rx+?e")
-      hence "\<exists> (l::int). ?rc*?rx+?e = ?ri * (real l)" by (simp add: rdvd_def)
-      hence "\<exists> (l::int). ?rc*?rx - real c * (real k * real d) +?e = ?ri * (real l) - real c * (real k * real d)" by simp
-      hence "\<exists> (l::int). ?rc*?rx - real c * (real k * real d) +?e = ?ri * (real l) - real c * (real k * real i * real di)" by (simp add: di_def)
-      hence "\<exists> (l::int). ?rc*?rx - real c * (real k * real d) +?e = ?ri * (real (l - c*k*di))" by (simp add: algebra_simps)
-      hence "\<exists> (l::int). ?rc*?rx - real c * (real k * real d) +?e = ?ri * (real l)"
-	by blast
-      thus "real i rdvd real c * real x - real c * (real k * real d) + Inum (real x # bs) e" using rdvd_def by simp
-    qed
-qed (auto simp add: nth_pos2 numbound0_I[where bs="bs" and b="real(x - k*d)" and b'="real x"] simp del: real_of_int_mult real_of_int_diff)
-
-lemma minusinf_ex:
-  assumes lin: "iszlfm p (real (a::int) #bs)"
-  and exmi: "\<exists> (x::int). Ifm (real x#bs) (minusinf p)" (is "\<exists> x. ?P1 x")
-  shows "\<exists> (x::int). Ifm (real x#bs) p" (is "\<exists> x. ?P x")
-proof-
-  let ?d = "\<delta> p"
-  from \<delta> [OF lin] have dpos: "?d >0" by simp
-  from \<delta> [OF lin] have alld: "d\<delta> p ?d" by simp
-  from minusinf_repeats[OF alld lin] have th1:"\<forall> x k. ?P1 x = ?P1 (x - (k * ?d))" by simp
-  from minusinf_inf[OF lin] have th2:"\<exists> z. \<forall> x. x<z \<longrightarrow> (?P x = ?P1 x)" by blast
-  from minusinfinity [OF dpos th1 th2] exmi show ?thesis by blast
-qed
-
-lemma minusinf_bex:
-  assumes lin: "iszlfm p (real (a::int) #bs)"
-  shows "(\<exists> (x::int). Ifm (real x#bs) (minusinf p)) = 
-         (\<exists> (x::int)\<in> {1..\<delta> p}. Ifm (real x#bs) (minusinf p))"
-  (is "(\<exists> x. ?P x) = _")
-proof-
-  let ?d = "\<delta> p"
-  from \<delta> [OF lin] have dpos: "?d >0" by simp
-  from \<delta> [OF lin] have alld: "d\<delta> p ?d" by simp
-  from minusinf_repeats[OF alld lin] have th1:"\<forall> x k. ?P x = ?P (x - (k * ?d))" by simp
-  from periodic_finite_ex[OF dpos th1] show ?thesis by blast
-qed
-
-lemma dvd1_eq1: "x >0 \<Longrightarrow> (x::int) dvd 1 = (x = 1)" by auto
-
-consts 
-  a\<beta> :: "fm \<Rightarrow> int \<Rightarrow> fm" (* adjusts the coeffitients of a formula *)
-  d\<beta> :: "fm \<Rightarrow> int \<Rightarrow> bool" (* tests if all coeffs c of c divide a given l*)
-  \<zeta>  :: "fm \<Rightarrow> int" (* computes the lcm of all coefficients of x*)
-  \<beta> :: "fm \<Rightarrow> num list"
-  \<alpha> :: "fm \<Rightarrow> num list"
-
-recdef a\<beta> "measure size"
-  "a\<beta> (And p q) = (\<lambda> k. And (a\<beta> p k) (a\<beta> q k))" 
-  "a\<beta> (Or p q) = (\<lambda> k. Or (a\<beta> p k) (a\<beta> q k))" 
-  "a\<beta> (Eq  (CN 0 c e)) = (\<lambda> k. Eq (CN 0 1 (Mul (k div c) e)))"
-  "a\<beta> (NEq (CN 0 c e)) = (\<lambda> k. NEq (CN 0 1 (Mul (k div c) e)))"
-  "a\<beta> (Lt  (CN 0 c e)) = (\<lambda> k. Lt (CN 0 1 (Mul (k div c) e)))"
-  "a\<beta> (Le  (CN 0 c e)) = (\<lambda> k. Le (CN 0 1 (Mul (k div c) e)))"
-  "a\<beta> (Gt  (CN 0 c e)) = (\<lambda> k. Gt (CN 0 1 (Mul (k div c) e)))"
-  "a\<beta> (Ge  (CN 0 c e)) = (\<lambda> k. Ge (CN 0 1 (Mul (k div c) e)))"
-  "a\<beta> (Dvd i (CN 0 c e)) =(\<lambda> k. Dvd ((k div c)*i) (CN 0 1 (Mul (k div c) e)))"
-  "a\<beta> (NDvd i (CN 0 c e))=(\<lambda> k. NDvd ((k div c)*i) (CN 0 1 (Mul (k div c) e)))"
-  "a\<beta> p = (\<lambda> k. p)"
-
-recdef d\<beta> "measure size"
-  "d\<beta> (And p q) = (\<lambda> k. (d\<beta> p k) \<and> (d\<beta> q k))" 
-  "d\<beta> (Or p q) = (\<lambda> k. (d\<beta> p k) \<and> (d\<beta> q k))" 
-  "d\<beta> (Eq  (CN 0 c e)) = (\<lambda> k. c dvd k)"
-  "d\<beta> (NEq (CN 0 c e)) = (\<lambda> k. c dvd k)"
-  "d\<beta> (Lt  (CN 0 c e)) = (\<lambda> k. c dvd k)"
-  "d\<beta> (Le  (CN 0 c e)) = (\<lambda> k. c dvd k)"
-  "d\<beta> (Gt  (CN 0 c e)) = (\<lambda> k. c dvd k)"
-  "d\<beta> (Ge  (CN 0 c e)) = (\<lambda> k. c dvd k)"
-  "d\<beta> (Dvd i (CN 0 c e)) =(\<lambda> k. c dvd k)"
-  "d\<beta> (NDvd i (CN 0 c e))=(\<lambda> k. c dvd k)"
-  "d\<beta> p = (\<lambda> k. True)"
-
-recdef \<zeta> "measure size"
-  "\<zeta> (And p q) = zlcm (\<zeta> p) (\<zeta> q)" 
-  "\<zeta> (Or p q) = zlcm (\<zeta> p) (\<zeta> q)" 
-  "\<zeta> (Eq  (CN 0 c e)) = c"
-  "\<zeta> (NEq (CN 0 c e)) = c"
-  "\<zeta> (Lt  (CN 0 c e)) = c"
-  "\<zeta> (Le  (CN 0 c e)) = c"
-  "\<zeta> (Gt  (CN 0 c e)) = c"
-  "\<zeta> (Ge  (CN 0 c e)) = c"
-  "\<zeta> (Dvd i (CN 0 c e)) = c"
-  "\<zeta> (NDvd i (CN 0 c e))= c"
-  "\<zeta> p = 1"
-
-recdef \<beta> "measure size"
-  "\<beta> (And p q) = (\<beta> p @ \<beta> q)" 
-  "\<beta> (Or p q) = (\<beta> p @ \<beta> q)" 
-  "\<beta> (Eq  (CN 0 c e)) = [Sub (C -1) e]"
-  "\<beta> (NEq (CN 0 c e)) = [Neg e]"
-  "\<beta> (Lt  (CN 0 c e)) = []"
-  "\<beta> (Le  (CN 0 c e)) = []"
-  "\<beta> (Gt  (CN 0 c e)) = [Neg e]"
-  "\<beta> (Ge  (CN 0 c e)) = [Sub (C -1) e]"
-  "\<beta> p = []"
-
-recdef \<alpha> "measure size"
-  "\<alpha> (And p q) = (\<alpha> p @ \<alpha> q)" 
-  "\<alpha> (Or p q) = (\<alpha> p @ \<alpha> q)" 
-  "\<alpha> (Eq  (CN 0 c e)) = [Add (C -1) e]"
-  "\<alpha> (NEq (CN 0 c e)) = [e]"
-  "\<alpha> (Lt  (CN 0 c e)) = [e]"
-  "\<alpha> (Le  (CN 0 c e)) = [Add (C -1) e]"
-  "\<alpha> (Gt  (CN 0 c e)) = []"
-  "\<alpha> (Ge  (CN 0 c e)) = []"
-  "\<alpha> p = []"
-consts mirror :: "fm \<Rightarrow> fm"
-recdef mirror "measure size"
-  "mirror (And p q) = And (mirror p) (mirror q)" 
-  "mirror (Or p q) = Or (mirror p) (mirror q)" 
-  "mirror (Eq  (CN 0 c e)) = Eq (CN 0 c (Neg e))"
-  "mirror (NEq (CN 0 c e)) = NEq (CN 0 c (Neg e))"
-  "mirror (Lt  (CN 0 c e)) = Gt (CN 0 c (Neg e))"
-  "mirror (Le  (CN 0 c e)) = Ge (CN 0 c (Neg e))"
-  "mirror (Gt  (CN 0 c e)) = Lt (CN 0 c (Neg e))"
-  "mirror (Ge  (CN 0 c e)) = Le (CN 0 c (Neg e))"
-  "mirror (Dvd i (CN 0 c e)) = Dvd i (CN 0 c (Neg e))"
-  "mirror (NDvd i (CN 0 c e)) = NDvd i (CN 0 c (Neg e))"
-  "mirror p = p"
-
-lemma mirror\<alpha>\<beta>:
-  assumes lp: "iszlfm p (a#bs)"
-  shows "(Inum (real (i::int)#bs)) ` set (\<alpha> p) = (Inum (real i#bs)) ` set (\<beta> (mirror p))"
-using lp
-by (induct p rule: mirror.induct, auto)
-
-lemma mirror: 
-  assumes lp: "iszlfm p (a#bs)"
-  shows "Ifm (real (x::int)#bs) (mirror p) = Ifm (real (- x)#bs) p" 
-using lp
-proof(induct p rule: iszlfm.induct)
-  case (9 j c e)
-  have th: "(real j rdvd real c * real x - Inum (real x # bs) e) =
-       (real j rdvd - (real c * real x - Inum (real x # bs) e))"
-    by (simp only: rdvd_minus[symmetric])
-  from prems th show  ?case
-    by (simp add: algebra_simps
-      numbound0_I[where bs="bs" and b'="real x" and b="- real x"])
-next
-    case (10 j c e)
-  have th: "(real j rdvd real c * real x - Inum (real x # bs) e) =
-       (real j rdvd - (real c * real x - Inum (real x # bs) e))"
-    by (simp only: rdvd_minus[symmetric])
-  from prems th show  ?case
-    by (simp add: algebra_simps
-      numbound0_I[where bs="bs" and b'="real x" and b="- real x"])
-qed (auto simp add: numbound0_I[where bs="bs" and b="real x" and b'="- real x"] nth_pos2)
-
-lemma mirror_l: "iszlfm p (a#bs) \<Longrightarrow> iszlfm (mirror p) (a#bs)"
-by (induct p rule: mirror.induct, auto simp add: isint_neg)
-
-lemma mirror_d\<beta>: "iszlfm p (a#bs) \<and> d\<beta> p 1 
-  \<Longrightarrow> iszlfm (mirror p) (a#bs) \<and> d\<beta> (mirror p) 1"
-by (induct p rule: mirror.induct, auto simp add: isint_neg)
-
-lemma mirror_\<delta>: "iszlfm p (a#bs) \<Longrightarrow> \<delta> (mirror p) = \<delta> p"
-by (induct p rule: mirror.induct,auto)
-
-
-lemma mirror_ex: 
-  assumes lp: "iszlfm p (real (i::int)#bs)"
-  shows "(\<exists> (x::int). Ifm (real x#bs) (mirror p)) = (\<exists> (x::int). Ifm (real x#bs) p)"
-  (is "(\<exists> x. ?I x ?mp) = (\<exists> x. ?I x p)")
-proof(auto)
-  fix x assume "?I x ?mp" hence "?I (- x) p" using mirror[OF lp] by blast
-  thus "\<exists> x. ?I x p" by blast
-next
-  fix x assume "?I x p" hence "?I (- x) ?mp" 
-    using mirror[OF lp, where x="- x", symmetric] by auto
-  thus "\<exists> x. ?I x ?mp" by blast
-qed
-
-lemma \<beta>_numbound0: assumes lp: "iszlfm p bs"
-  shows "\<forall> b\<in> set (\<beta> p). numbound0 b"
-  using lp by (induct p rule: \<beta>.induct,auto)
-
-lemma d\<beta>_mono: 
-  assumes linp: "iszlfm p (a #bs)"
-  and dr: "d\<beta> p l"
-  and d: "l dvd l'"
-  shows "d\<beta> p l'"
-using dr linp zdvd_trans[where n="l" and k="l'", simplified d]
-by (induct p rule: iszlfm.induct) simp_all
-
-lemma \<alpha>_l: assumes lp: "iszlfm p (a#bs)"
-  shows "\<forall> b\<in> set (\<alpha> p). numbound0 b \<and> isint b (a#bs)"
-using lp
-by(induct p rule: \<alpha>.induct, auto simp add: isint_add isint_c)
-
-lemma \<zeta>: 
-  assumes linp: "iszlfm p (a #bs)"
-  shows "\<zeta> p > 0 \<and> d\<beta> p (\<zeta> p)"
-using linp
-proof(induct p rule: iszlfm.induct)
-  case (1 p q)
-  from prems have dl1: "\<zeta> p dvd zlcm (\<zeta> p) (\<zeta> q)" by simp
-  from prems have dl2: "\<zeta> q dvd zlcm (\<zeta> p) (\<zeta> q)" by simp
-  from prems d\<beta>_mono[where p = "p" and l="\<zeta> p" and l'="zlcm (\<zeta> p) (\<zeta> q)"] 
-    d\<beta>_mono[where p = "q" and l="\<zeta> q" and l'="zlcm (\<zeta> p) (\<zeta> q)"] 
-    dl1 dl2 show ?case by (auto simp add: zlcm_pos)
-next
-  case (2 p q)
-  from prems have dl1: "\<zeta> p dvd zlcm (\<zeta> p) (\<zeta> q)" by simp
-  from prems have dl2: "\<zeta> q dvd zlcm (\<zeta> p) (\<zeta> q)" by simp
-  from prems d\<beta>_mono[where p = "p" and l="\<zeta> p" and l'="zlcm (\<zeta> p) (\<zeta> q)"] 
-    d\<beta>_mono[where p = "q" and l="\<zeta> q" and l'="zlcm (\<zeta> p) (\<zeta> q)"] 
-    dl1 dl2 show ?case by (auto simp add: zlcm_pos)
-qed (auto simp add: zlcm_pos)
-
-lemma a\<beta>: assumes linp: "iszlfm p (a #bs)" and d: "d\<beta> p l" and lp: "l > 0"
-  shows "iszlfm (a\<beta> p l) (a #bs) \<and> d\<beta> (a\<beta> p l) 1 \<and> (Ifm (real (l * x) #bs) (a\<beta> p l) = Ifm ((real x)#bs) p)"
-using linp d
-proof (induct p rule: iszlfm.induct)
-  case (5 c e) hence cp: "c>0" and be: "numbound0 e" and ei:"isint e (a#bs)" and d': "c dvd l" by simp+
-    from lp cp have clel: "c\<le>l" by (simp add: zdvd_imp_le [OF d' lp])
-    from cp have cnz: "c \<noteq> 0" by simp
-    have "c div c\<le> l div c"
-      by (simp add: zdiv_mono1[OF clel cp])
-    then have ldcp:"0 < l div c" 
-      by (simp add: zdiv_self[OF cnz])
-    have "c * (l div c) = c* (l div c) + l mod c" using d' zdvd_iff_zmod_eq_0[where m="c" and n="l"] by simp
-    hence cl:"c * (l div c) =l" using zmod_zdiv_equality[where a="l" and b="c", symmetric] 
-      by simp
-    hence "(real l * real x + real (l div c) * Inum (real x # bs) e < (0\<Colon>real)) =
-          (real (c * (l div c)) * real x + real (l div c) * Inum (real x # bs) e < 0)"
-      by simp
-    also have "\<dots> = (real (l div c) * (real c * real x + Inum (real x # bs) e) < (real (l div c)) * 0)" by (simp add: algebra_simps)
-    also have "\<dots> = (real c * real x + Inum (real x # bs) e < 0)"
-    using mult_less_0_iff [where a="real (l div c)" and b="real c * real x + Inum (real x # bs) e"] ldcp by simp
-  finally show ?case using numbound0_I[OF be,where b="real (l * x)" and b'="real x" and bs="bs"] be  isint_Mul[OF ei] by simp
-next
-  case (6 c e) hence cp: "c>0" and be: "numbound0 e" and ei:"isint e (a#bs)" and d': "c dvd l" by simp+
-    from lp cp have clel: "c\<le>l" by (simp add: zdvd_imp_le [OF d' lp])
-    from cp have cnz: "c \<noteq> 0" by simp
-    have "c div c\<le> l div c"
-      by (simp add: zdiv_mono1[OF clel cp])
-    then have ldcp:"0 < l div c" 
-      by (simp add: zdiv_self[OF cnz])
-    have "c * (l div c) = c* (l div c) + l mod c" using d' zdvd_iff_zmod_eq_0[where m="c" and n="l"] by simp
-    hence cl:"c * (l div c) =l" using zmod_zdiv_equality[where a="l" and b="c", symmetric] 
-      by simp
-    hence "(real l * real x + real (l div c) * Inum (real x # bs) e \<le> (0\<Colon>real)) =
-          (real (c * (l div c)) * real x + real (l div c) * Inum (real x # bs) e \<le> 0)"
-      by simp
-    also have "\<dots> = (real (l div c) * (real c * real x + Inum (real x # bs) e) \<le> (real (l div c)) * 0)" by (simp add: algebra_simps)
-    also have "\<dots> = (real c * real x + Inum (real x # bs) e \<le> 0)"
-    using mult_le_0_iff [where a="real (l div c)" and b="real c * real x + Inum (real x # bs) e"] ldcp by simp
-  finally show ?case using numbound0_I[OF be,where b="real (l * x)" and b'="real x" and bs="bs"]  be  isint_Mul[OF ei] by simp
-next
-  case (7 c e) hence cp: "c>0" and be: "numbound0 e" and ei:"isint e (a#bs)" and d': "c dvd l" by simp+
-    from lp cp have clel: "c\<le>l" by (simp add: zdvd_imp_le [OF d' lp])
-    from cp have cnz: "c \<noteq> 0" by simp
-    have "c div c\<le> l div c"
-      by (simp add: zdiv_mono1[OF clel cp])
-    then have ldcp:"0 < l div c" 
-      by (simp add: zdiv_self[OF cnz])
-    have "c * (l div c) = c* (l div c) + l mod c" using d' zdvd_iff_zmod_eq_0[where m="c" and n="l"] by simp
-    hence cl:"c * (l div c) =l" using zmod_zdiv_equality[where a="l" and b="c", symmetric] 
-      by simp
-    hence "(real l * real x + real (l div c) * Inum (real x # bs) e > (0\<Colon>real)) =
-          (real (c * (l div c)) * real x + real (l div c) * Inum (real x # bs) e > 0)"
-      by simp
-    also have "\<dots> = (real (l div c) * (real c * real x + Inum (real x # bs) e) > (real (l div c)) * 0)" by (simp add: algebra_simps)
-    also have "\<dots> = (real c * real x + Inum (real x # bs) e > 0)"
-    using zero_less_mult_iff [where a="real (l div c)" and b="real c * real x + Inum (real x # bs) e"] ldcp by simp
-  finally show ?case using numbound0_I[OF be,where b="real (l * x)" and b'="real x" and bs="bs"]  be  isint_Mul[OF ei] by simp
-next
-  case (8 c e) hence cp: "c>0" and be: "numbound0 e"  and ei:"isint e (a#bs)" and d': "c dvd l" by simp+
-    from lp cp have clel: "c\<le>l" by (simp add: zdvd_imp_le [OF d' lp])
-    from cp have cnz: "c \<noteq> 0" by simp
-    have "c div c\<le> l div c"
-      by (simp add: zdiv_mono1[OF clel cp])
-    then have ldcp:"0 < l div c" 
-      by (simp add: zdiv_self[OF cnz])
-    have "c * (l div c) = c* (l div c) + l mod c" using d' zdvd_iff_zmod_eq_0[where m="c" and n="l"] by simp
-    hence cl:"c * (l div c) =l" using zmod_zdiv_equality[where a="l" and b="c", symmetric] 
-      by simp
-    hence "(real l * real x + real (l div c) * Inum (real x # bs) e \<ge> (0\<Colon>real)) =
-          (real (c * (l div c)) * real x + real (l div c) * Inum (real x # bs) e \<ge> 0)"
-      by simp
-    also have "\<dots> = (real (l div c) * (real c * real x + Inum (real x # bs) e) \<ge> (real (l div c)) * 0)" by (simp add: algebra_simps)
-    also have "\<dots> = (real c * real x + Inum (real x # bs) e \<ge> 0)"
-    using zero_le_mult_iff [where a="real (l div c)" and b="real c * real x + Inum (real x # bs) e"] ldcp by simp
-  finally show ?case using numbound0_I[OF be,where b="real (l * x)" and b'="real x" and bs="bs"]  be  isint_Mul[OF ei] by simp
-next
-  case (3 c e) hence cp: "c>0" and be: "numbound0 e" and ei:"isint e (a#bs)" and d': "c dvd l" by simp+
-    from lp cp have clel: "c\<le>l" by (simp add: zdvd_imp_le [OF d' lp])
-    from cp have cnz: "c \<noteq> 0" by simp
-    have "c div c\<le> l div c"
-      by (simp add: zdiv_mono1[OF clel cp])
-    then have ldcp:"0 < l div c" 
-      by (simp add: zdiv_self[OF cnz])
-    have "c * (l div c) = c* (l div c) + l mod c" using d' zdvd_iff_zmod_eq_0[where m="c" and n="l"] by simp
-    hence cl:"c * (l div c) =l" using zmod_zdiv_equality[where a="l" and b="c", symmetric] 
-      by simp
-    hence "(real l * real x + real (l div c) * Inum (real x # bs) e = (0\<Colon>real)) =
-          (real (c * (l div c)) * real x + real (l div c) * Inum (real x # bs) e = 0)"
-      by simp
-    also have "\<dots> = (real (l div c) * (real c * real x + Inum (real x # bs) e) = (real (l div c)) * 0)" by (simp add: algebra_simps)
-    also have "\<dots> = (real c * real x + Inum (real x # bs) e = 0)"
-    using mult_eq_0_iff [where a="real (l div c)" and b="real c * real x + Inum (real x # bs) e"] ldcp by simp
-  finally show ?case using numbound0_I[OF be,where b="real (l * x)" and b'="real x" and bs="bs"]  be  isint_Mul[OF ei] by simp
-next
-  case (4 c e) hence cp: "c>0" and be: "numbound0 e" and ei:"isint e (a#bs)" and d': "c dvd l" by simp+
-    from lp cp have clel: "c\<le>l" by (simp add: zdvd_imp_le [OF d' lp])
-    from cp have cnz: "c \<noteq> 0" by simp
-    have "c div c\<le> l div c"
-      by (simp add: zdiv_mono1[OF clel cp])
-    then have ldcp:"0 < l div c" 
-      by (simp add: zdiv_self[OF cnz])
-    have "c * (l div c) = c* (l div c) + l mod c" using d' zdvd_iff_zmod_eq_0[where m="c" and n="l"] by simp
-    hence cl:"c * (l div c) =l" using zmod_zdiv_equality[where a="l" and b="c", symmetric] 
-      by simp
-    hence "(real l * real x + real (l div c) * Inum (real x # bs) e \<noteq> (0\<Colon>real)) =
-          (real (c * (l div c)) * real x + real (l div c) * Inum (real x # bs) e \<noteq> 0)"
-      by simp
-    also have "\<dots> = (real (l div c) * (real c * real x + Inum (real x # bs) e) \<noteq> (real (l div c)) * 0)" by (simp add: algebra_simps)
-    also have "\<dots> = (real c * real x + Inum (real x # bs) e \<noteq> 0)"
-    using zero_le_mult_iff [where a="real (l div c)" and b="real c * real x + Inum (real x # bs) e"] ldcp by simp
-  finally show ?case using numbound0_I[OF be,where b="real (l * x)" and b'="real x" and bs="bs"]  be  isint_Mul[OF ei] by simp
-next
-  case (9 j c e) hence cp: "c>0" and be: "numbound0 e" and ei:"isint e (a#bs)" and jp: "j > 0" and d': "c dvd l" by simp+
-    from lp cp have clel: "c\<le>l" by (simp add: zdvd_imp_le [OF d' lp])
-    from cp have cnz: "c \<noteq> 0" by simp
-    have "c div c\<le> l div c"
-      by (simp add: zdiv_mono1[OF clel cp])
-    then have ldcp:"0 < l div c" 
-      by (simp add: zdiv_self[OF cnz])
-    have "c * (l div c) = c* (l div c) + l mod c" using d' zdvd_iff_zmod_eq_0[where m="c" and n="l"] by simp
-    hence cl:"c * (l div c) =l" using zmod_zdiv_equality[where a="l" and b="c", symmetric] 
-      by simp
-    hence "(\<exists> (k::int). real l * real x + real (l div c) * Inum (real x # bs) e = (real (l div c) * real j) * real k) = (\<exists> (k::int). real (c * (l div c)) * real x + real (l div c) * Inum (real x # bs) e = (real (l div c) * real j) * real k)"  by simp
-    also have "\<dots> = (\<exists> (k::int). real (l div c) * (real c * real x + Inum (real x # bs) e - real j * real k) = real (l div c)*0)" by (simp add: algebra_simps)
-    also fix k have "\<dots> = (\<exists> (k::int). real c * real x + Inum (real x # bs) e - real j * real k = 0)"
-    using zero_le_mult_iff [where a="real (l div c)" and b="real c * real x + Inum (real x # bs) e - real j * real k"] ldcp by simp
-  also have "\<dots> = (\<exists> (k::int). real c * real x + Inum (real x # bs) e = real j * real k)" by simp
-  finally show ?case using numbound0_I[OF be,where b="real (l * x)" and b'="real x" and bs="bs"] rdvd_def  be  isint_Mul[OF ei] mult_strict_mono[OF ldcp jp ldcp ] by simp 
-next
-  case (10 j c e) hence cp: "c>0" and be: "numbound0 e" and ei:"isint e (a#bs)" and jp: "j > 0" and d': "c dvd l" by simp+
-    from lp cp have clel: "c\<le>l" by (simp add: zdvd_imp_le [OF d' lp])
-    from cp have cnz: "c \<noteq> 0" by simp
-    have "c div c\<le> l div c"
-      by (simp add: zdiv_mono1[OF clel cp])
-    then have ldcp:"0 < l div c" 
-      by (simp add: zdiv_self[OF cnz])
-    have "c * (l div c) = c* (l div c) + l mod c" using d' zdvd_iff_zmod_eq_0[where m="c" and n="l"] by simp
-    hence cl:"c * (l div c) =l" using zmod_zdiv_equality[where a="l" and b="c", symmetric] 
-      by simp
-    hence "(\<exists> (k::int). real l * real x + real (l div c) * Inum (real x # bs) e = (real (l div c) * real j) * real k) = (\<exists> (k::int). real (c * (l div c)) * real x + real (l div c) * Inum (real x # bs) e = (real (l div c) * real j) * real k)"  by simp
-    also have "\<dots> = (\<exists> (k::int). real (l div c) * (real c * real x + Inum (real x # bs) e - real j * real k) = real (l div c)*0)" by (simp add: algebra_simps)
-    also fix k have "\<dots> = (\<exists> (k::int). real c * real x + Inum (real x # bs) e - real j * real k = 0)"
-    using zero_le_mult_iff [where a="real (l div c)" and b="real c * real x + Inum (real x # bs) e - real j * real k"] ldcp by simp
-  also have "\<dots> = (\<exists> (k::int). real c * real x + Inum (real x # bs) e = real j * real k)" by simp
-  finally show ?case using numbound0_I[OF be,where b="real (l * x)" and b'="real x" and bs="bs"] rdvd_def  be  isint_Mul[OF ei]  mult_strict_mono[OF ldcp jp ldcp ] by simp
-qed (simp_all add: nth_pos2 numbound0_I[where bs="bs" and b="real (l * x)" and b'="real x"] isint_Mul del: real_of_int_mult)
-
-lemma a\<beta>_ex: assumes linp: "iszlfm p (a#bs)" and d: "d\<beta> p l" and lp: "l>0"
-  shows "(\<exists> x. l dvd x \<and> Ifm (real x #bs) (a\<beta> p l)) = (\<exists> (x::int). Ifm (real x#bs) p)"
-  (is "(\<exists> x. l dvd x \<and> ?P x) = (\<exists> x. ?P' x)")
-proof-
-  have "(\<exists> x. l dvd x \<and> ?P x) = (\<exists> (x::int). ?P (l*x))"
-    using unity_coeff_ex[where l="l" and P="?P", simplified] by simp
-  also have "\<dots> = (\<exists> (x::int). ?P' x)" using a\<beta>[OF linp d lp] by simp
-  finally show ?thesis  . 
-qed
-
-lemma \<beta>:
-  assumes lp: "iszlfm p (a#bs)"
-  and u: "d\<beta> p 1"
-  and d: "d\<delta> p d"
-  and dp: "d > 0"
-  and nob: "\<not>(\<exists>(j::int) \<in> {1 .. d}. \<exists> b\<in> (Inum (a#bs)) ` set(\<beta> p). real x = b + real j)"
-  and p: "Ifm (real x#bs) p" (is "?P x")
-  shows "?P (x - d)"
-using lp u d dp nob p
-proof(induct p rule: iszlfm.induct)
-  case (5 c e) hence c1: "c=1" and  bn:"numbound0 e" using dvd1_eq1[where x="c"] by simp+
-    with dp p c1 numbound0_I[OF bn,where b="real (x-d)" and b'="real x" and bs="bs"] prems
-    show ?case by (simp del: real_of_int_minus)
-next
-  case (6 c e)  hence c1: "c=1" and  bn:"numbound0 e" using dvd1_eq1[where x="c"] by simp+
-    with dp p c1 numbound0_I[OF bn,where b="real (x-d)" and b'="real x" and bs="bs"] prems
-    show ?case by (simp del: real_of_int_minus)
-next
-  case (7 c e) hence p: "Ifm (real x #bs) (Gt (CN 0 c e))" and c1: "c=1" and bn:"numbound0 e" and ie1:"isint e (a#bs)" using dvd1_eq1[where x="c"] by simp+
-    let ?e = "Inum (real x # bs) e"
-    from ie1 have ie: "real (floor ?e) = ?e" using isint_iff[where n="e" and bs="a#bs"]
-      numbound0_I[OF bn,where b="a" and b'="real x" and bs="bs"]
-      by (simp add: isint_iff)
-    {assume "real (x-d) +?e > 0" hence ?case using c1 
-      numbound0_I[OF bn,where b="real (x-d)" and b'="real x" and bs="bs"]
-	by (simp del: real_of_int_minus)}
-    moreover
-    {assume H: "\<not> real (x-d) + ?e > 0" 
-      let ?v="Neg e"
-      have vb: "?v \<in> set (\<beta> (Gt (CN 0 c e)))" by simp
-      from prems(11)[simplified simp_thms Inum.simps \<beta>.simps set.simps bex_simps numbound0_I[OF bn,where b="a" and b'="real x" and bs="bs"]] 
-      have nob: "\<not> (\<exists> j\<in> {1 ..d}. real x =  - ?e + real j)" by auto 
-      from H p have "real x + ?e > 0 \<and> real x + ?e \<le> real d" by (simp add: c1)
-      hence "real (x + floor ?e) > real (0::int) \<and> real (x + floor ?e) \<le> real d"
-	using ie by simp
-      hence "x + floor ?e \<ge> 1 \<and> x + floor ?e \<le> d"  by simp
-      hence "\<exists> (j::int) \<in> {1 .. d}. j = x + floor ?e" by simp
-      hence "\<exists> (j::int) \<in> {1 .. d}. real x = real (- floor ?e + j)" 
-	by (simp only: real_of_int_inject) (simp add: algebra_simps)
-      hence "\<exists> (j::int) \<in> {1 .. d}. real x = - ?e + real j" 
-	by (simp add: ie[simplified isint_iff])
-      with nob have ?case by auto}
-    ultimately show ?case by blast
-next
-  case (8 c e) hence p: "Ifm (real x #bs) (Ge (CN 0 c e))" and c1: "c=1" and bn:"numbound0 e" 
-    and ie1:"isint e (a #bs)" using dvd1_eq1[where x="c"] by simp+
-    let ?e = "Inum (real x # bs) e"
-    from ie1 have ie: "real (floor ?e) = ?e" using numbound0_I[OF bn,where b="real x" and b'="a" and bs="bs"] isint_iff[where n="e" and bs="(real x)#bs"]
-      by (simp add: isint_iff)
-    {assume "real (x-d) +?e \<ge> 0" hence ?case using  c1 
-      numbound0_I[OF bn,where b="real (x-d)" and b'="real x" and bs="bs"]
-	by (simp del: real_of_int_minus)}
-    moreover
-    {assume H: "\<not> real (x-d) + ?e \<ge> 0" 
-      let ?v="Sub (C -1) e"
-      have vb: "?v \<in> set (\<beta> (Ge (CN 0 c e)))" by simp
-      from prems(11)[simplified simp_thms Inum.simps \<beta>.simps set.simps bex_simps numbound0_I[OF bn,where b="a" and b'="real x" and bs="bs"]] 
-      have nob: "\<not> (\<exists> j\<in> {1 ..d}. real x =  - ?e - 1 + real j)" by auto 
-      from H p have "real x + ?e \<ge> 0 \<and> real x + ?e < real d" by (simp add: c1)
-      hence "real (x + floor ?e) \<ge> real (0::int) \<and> real (x + floor ?e) < real d"
-	using ie by simp
-      hence "x + floor ?e +1 \<ge> 1 \<and> x + floor ?e + 1 \<le> d"  by simp
-      hence "\<exists> (j::int) \<in> {1 .. d}. j = x + floor ?e + 1" by simp
-      hence "\<exists> (j::int) \<in> {1 .. d}. x= - floor ?e - 1 + j" by (simp add: algebra_simps)
-      hence "\<exists> (j::int) \<in> {1 .. d}. real x= real (- floor ?e - 1 + j)" 
-	by (simp only: real_of_int_inject)
-      hence "\<exists> (j::int) \<in> {1 .. d}. real x= - ?e - 1 + real j" 
-	by (simp add: ie[simplified isint_iff])
-      with nob have ?case by simp }
-    ultimately show ?case by blast
-next
-  case (3 c e) hence p: "Ifm (real x #bs) (Eq (CN 0 c e))" (is "?p x") and c1: "c=1" 
-    and bn:"numbound0 e" and ie1: "isint e (a #bs)" using dvd1_eq1[where x="c"] by simp+
-    let ?e = "Inum (real x # bs) e"
-    let ?v="(Sub (C -1) e)"
-    have vb: "?v \<in> set (\<beta> (Eq (CN 0 c e)))" by simp
-    from p have "real x= - ?e" by (simp add: c1) with prems(11) show ?case using dp
-      by simp (erule ballE[where x="1"],
-	simp_all add:algebra_simps numbound0_I[OF bn,where b="real x"and b'="a"and bs="bs"])
-next
-  case (4 c e)hence p: "Ifm (real x #bs) (NEq (CN 0 c e))" (is "?p x") and c1: "c=1" 
-    and bn:"numbound0 e" and ie1: "isint e (a #bs)" using dvd1_eq1[where x="c"] by simp+
-    let ?e = "Inum (real x # bs) e"
-    let ?v="Neg e"
-    have vb: "?v \<in> set (\<beta> (NEq (CN 0 c e)))" by simp
-    {assume "real x - real d + Inum ((real (x -d)) # bs) e \<noteq> 0" 
-      hence ?case by (simp add: c1)}
-    moreover
-    {assume H: "real x - real d + Inum ((real (x -d)) # bs) e = 0"
-      hence "real x = - Inum ((real (x -d)) # bs) e + real d" by simp
-      hence "real x = - Inum (a # bs) e + real d"
-	by (simp add: numbound0_I[OF bn,where b="real x - real d"and b'="a"and bs="bs"])
-       with prems(11) have ?case using dp by simp}
-  ultimately show ?case by blast
-next 
-  case (9 j c e) hence p: "Ifm (real x #bs) (Dvd j (CN 0 c e))" (is "?p x") and c1: "c=1" 
-    and bn:"numbound0 e" using dvd1_eq1[where x="c"] by simp+
-    let ?e = "Inum (real x # bs) e"
-    from prems have "isint e (a #bs)"  by simp 
-    hence ie: "real (floor ?e) = ?e" using isint_iff[where n="e" and bs="(real x)#bs"] numbound0_I[OF bn,where b="real x" and b'="a" and bs="bs"]
-      by (simp add: isint_iff)
-    from prems have id: "j dvd d" by simp
-    from c1 ie[symmetric] have "?p x = (real j rdvd real (x+ floor ?e))" by simp
-    also have "\<dots> = (j dvd x + floor ?e)" 
-      using int_rdvd_real[where i="j" and x="real (x+ floor ?e)"] by simp
-    also have "\<dots> = (j dvd x - d + floor ?e)" 
-      using dvd_period[OF id, where x="x" and c="-1" and t="floor ?e"] by simp
-    also have "\<dots> = (real j rdvd real (x - d + floor ?e))" 
-      using int_rdvd_real[where i="j" and x="real (x-d + floor ?e)",symmetric, simplified]
-      ie by simp
-    also have "\<dots> = (real j rdvd real x - real d + ?e)" 
-      using ie by simp
-    finally show ?case 
-      using numbound0_I[OF bn,where b="real (x-d)" and b'="real x" and bs="bs"] c1 p by simp
-next
-  case (10 j c e) hence p: "Ifm (real x #bs) (NDvd j (CN 0 c e))" (is "?p x") and c1: "c=1" and bn:"numbound0 e" using dvd1_eq1[where x="c"] by simp+
-    let ?e = "Inum (real x # bs) e"
-    from prems have "isint e (a#bs)"  by simp 
-    hence ie: "real (floor ?e) = ?e" using numbound0_I[OF bn,where b="real x" and b'="a" and bs="bs"] isint_iff[where n="e" and bs="(real x)#bs"]
-      by (simp add: isint_iff)
-    from prems have id: "j dvd d" by simp
-    from c1 ie[symmetric] have "?p x = (\<not> real j rdvd real (x+ floor ?e))" by simp
-    also have "\<dots> = (\<not> j dvd x + floor ?e)" 
-      using int_rdvd_real[where i="j" and x="real (x+ floor ?e)"] by simp
-    also have "\<dots> = (\<not> j dvd x - d + floor ?e)" 
-      using dvd_period[OF id, where x="x" and c="-1" and t="floor ?e"] by simp
-    also have "\<dots> = (\<not> real j rdvd real (x - d + floor ?e))" 
-      using int_rdvd_real[where i="j" and x="real (x-d + floor ?e)",symmetric, simplified]
-      ie by simp
-    also have "\<dots> = (\<not> real j rdvd real x - real d + ?e)" 
-      using ie by simp
-    finally show ?case using numbound0_I[OF bn,where b="real (x-d)" and b'="real x" and bs="bs"] c1 p by simp
-qed (auto simp add: numbound0_I[where bs="bs" and b="real (x - d)" and b'="real x"] nth_pos2 simp del: real_of_int_diff)
-
-lemma \<beta>':   
-  assumes lp: "iszlfm p (a #bs)"
-  and u: "d\<beta> p 1"
-  and d: "d\<delta> p d"
-  and dp: "d > 0"
-  shows "\<forall> x. \<not>(\<exists>(j::int) \<in> {1 .. d}. \<exists> b\<in> set(\<beta> p). Ifm ((Inum (a#bs) b + real j) #bs) p) \<longrightarrow> Ifm (real x#bs) p \<longrightarrow> Ifm (real (x - d)#bs) p" (is "\<forall> x. ?b \<longrightarrow> ?P x \<longrightarrow> ?P (x - d)")
-proof(clarify)
-  fix x 
-  assume nb:"?b" and px: "?P x" 
-  hence nb2: "\<not>(\<exists>(j::int) \<in> {1 .. d}. \<exists> b\<in> (Inum (a#bs)) ` set(\<beta> p). real x = b + real j)"
-    by auto
-  from  \<beta>[OF lp u d dp nb2 px] show "?P (x -d )" .
-qed
-
-lemma \<beta>_int: assumes lp: "iszlfm p bs"
-  shows "\<forall> b\<in> set (\<beta> p). isint b bs"
-using lp by (induct p rule: iszlfm.induct) (auto simp add: isint_neg isint_sub)
-
-lemma cpmi_eq: "0 < D \<Longrightarrow> (EX z::int. ALL x. x < z --> (P x = P1 x))
-==> ALL x.~(EX (j::int) : {1..D}. EX (b::int) : B. P(b+j)) --> P (x) --> P (x - D) 
-==> (ALL (x::int). ALL (k::int). ((P1 x)= (P1 (x-k*D))))
-==> (EX (x::int). P(x)) = ((EX (j::int) : {1..D} . (P1(j))) | (EX (j::int) : {1..D}. EX (b::int) : B. P (b+j)))"
-apply(rule iffI)
-prefer 2
-apply(drule minusinfinity)
-apply assumption+
-apply(fastsimp)
-apply clarsimp
-apply(subgoal_tac "!!k. 0<=k \<Longrightarrow> !x. P x \<longrightarrow> P (x - k*D)")
-apply(frule_tac x = x and z=z in decr_lemma)
-apply(subgoal_tac "P1(x - (\<bar>x - z\<bar> + 1) * D)")
-prefer 2
-apply(subgoal_tac "0 <= (\<bar>x - z\<bar> + 1)")
-prefer 2 apply arith
- apply fastsimp
-apply(drule (1)  periodic_finite_ex)
-apply blast
-apply(blast dest:decr_mult_lemma)
-done
-
-
-theorem cp_thm:
-  assumes lp: "iszlfm p (a #bs)"
-  and u: "d\<beta> p 1"
-  and d: "d\<delta> p d"
-  and dp: "d > 0"
-  shows "(\<exists> (x::int). Ifm (real x #bs) p) = (\<exists> j\<in> {1.. d}. Ifm (real j #bs) (minusinf p) \<or> (\<exists> b \<in> set (\<beta> p). Ifm ((Inum (a#bs) b + real j) #bs) p))"
-  (is "(\<exists> (x::int). ?P (real x)) = (\<exists> j\<in> ?D. ?M j \<or> (\<exists> b\<in> ?B. ?P (?I b + real j)))")
-proof-
-  from minusinf_inf[OF lp] 
-  have th: "\<exists>(z::int). \<forall>x<z. ?P (real x) = ?M x" by blast
-  let ?B' = "{floor (?I b) | b. b\<in> ?B}"
-  from \<beta>_int[OF lp] isint_iff[where bs="a # bs"] have B: "\<forall> b\<in> ?B. real (floor (?I b)) = ?I b" by simp
-  from B[rule_format] 
-  have "(\<exists>j\<in>?D. \<exists>b\<in> ?B. ?P (?I b + real j)) = (\<exists>j\<in>?D. \<exists>b\<in> ?B. ?P (real (floor (?I b)) + real j))" 
-    by simp
-  also have "\<dots> = (\<exists>j\<in>?D. \<exists>b\<in> ?B. ?P (real (floor (?I b) + j)))" by simp
-  also have"\<dots> = (\<exists> j \<in> ?D. \<exists> b \<in> ?B'. ?P (real (b + j)))"  by blast
-  finally have BB': 
-    "(\<exists>j\<in>?D. \<exists>b\<in> ?B. ?P (?I b + real j)) = (\<exists> j \<in> ?D. \<exists> b \<in> ?B'. ?P (real (b + j)))" 
-    by blast 
-  hence th2: "\<forall> x. \<not> (\<exists> j \<in> ?D. \<exists> b \<in> ?B'. ?P (real (b + j))) \<longrightarrow> ?P (real x) \<longrightarrow> ?P (real (x - d))" using \<beta>'[OF lp u d dp] by blast
-  from minusinf_repeats[OF d lp]
-  have th3: "\<forall> x k. ?M x = ?M (x-k*d)" by simp
-  from cpmi_eq[OF dp th th2 th3] BB' show ?thesis by blast
-qed
-
-    (* Reddy and Loveland *)
-
-
-consts 
-  \<rho> :: "fm \<Rightarrow> (num \<times> int) list" (* Compute the Reddy and Loveland Bset*)
-  \<sigma>\<rho>:: "fm \<Rightarrow> num \<times> int \<Rightarrow> fm" (* Performs the modified substitution of Reddy and Loveland*)
-  \<alpha>\<rho> :: "fm \<Rightarrow> (num\<times>int) list"
-  a\<rho> :: "fm \<Rightarrow> int \<Rightarrow> fm"
-recdef \<rho> "measure size"
-  "\<rho> (And p q) = (\<rho> p @ \<rho> q)" 
-  "\<rho> (Or p q) = (\<rho> p @ \<rho> q)" 
-  "\<rho> (Eq  (CN 0 c e)) = [(Sub (C -1) e,c)]"
-  "\<rho> (NEq (CN 0 c e)) = [(Neg e,c)]"
-  "\<rho> (Lt  (CN 0 c e)) = []"
-  "\<rho> (Le  (CN 0 c e)) = []"
-  "\<rho> (Gt  (CN 0 c e)) = [(Neg e, c)]"
-  "\<rho> (Ge  (CN 0 c e)) = [(Sub (C (-1)) e, c)]"
-  "\<rho> p = []"
-
-recdef \<sigma>\<rho> "measure size"
-  "\<sigma>\<rho> (And p q) = (\<lambda> (t,k). And (\<sigma>\<rho> p (t,k)) (\<sigma>\<rho> q (t,k)))" 
-  "\<sigma>\<rho> (Or p q) = (\<lambda> (t,k). Or (\<sigma>\<rho> p (t,k)) (\<sigma>\<rho> q (t,k)))" 
-  "\<sigma>\<rho> (Eq  (CN 0 c e)) = (\<lambda> (t,k). if k dvd c then (Eq (Add (Mul (c div k) t) e)) 
-                                            else (Eq (Add (Mul c t) (Mul k e))))"
-  "\<sigma>\<rho> (NEq (CN 0 c e)) = (\<lambda> (t,k). if k dvd c then (NEq (Add (Mul (c div k) t) e)) 
-                                            else (NEq (Add (Mul c t) (Mul k e))))"
-  "\<sigma>\<rho> (Lt  (CN 0 c e)) = (\<lambda> (t,k). if k dvd c then (Lt (Add (Mul (c div k) t) e)) 
-                                            else (Lt (Add (Mul c t) (Mul k e))))"
-  "\<sigma>\<rho> (Le  (CN 0 c e)) = (\<lambda> (t,k). if k dvd c then (Le (Add (Mul (c div k) t) e)) 
-                                            else (Le (Add (Mul c t) (Mul k e))))"
-  "\<sigma>\<rho> (Gt  (CN 0 c e)) = (\<lambda> (t,k). if k dvd c then (Gt (Add (Mul (c div k) t) e)) 
-                                            else (Gt (Add (Mul c t) (Mul k e))))"
-  "\<sigma>\<rho> (Ge  (CN 0 c e)) = (\<lambda> (t,k). if k dvd c then (Ge (Add (Mul (c div k) t) e)) 
-                                            else (Ge (Add (Mul c t) (Mul k e))))"
-  "\<sigma>\<rho> (Dvd i (CN 0 c e)) =(\<lambda> (t,k). if k dvd c then (Dvd i (Add (Mul (c div k) t) e)) 
-                                            else (Dvd (i*k) (Add (Mul c t) (Mul k e))))"
-  "\<sigma>\<rho> (NDvd i (CN 0 c e))=(\<lambda> (t,k). if k dvd c then (NDvd i (Add (Mul (c div k) t) e)) 
-                                            else (NDvd (i*k) (Add (Mul c t) (Mul k e))))"
-  "\<sigma>\<rho> p = (\<lambda> (t,k). p)"
-
-recdef \<alpha>\<rho> "measure size"
-  "\<alpha>\<rho> (And p q) = (\<alpha>\<rho> p @ \<alpha>\<rho> q)" 
-  "\<alpha>\<rho> (Or p q) = (\<alpha>\<rho> p @ \<alpha>\<rho> q)" 
-  "\<alpha>\<rho> (Eq  (CN 0 c e)) = [(Add (C -1) e,c)]"
-  "\<alpha>\<rho> (NEq (CN 0 c e)) = [(e,c)]"
-  "\<alpha>\<rho> (Lt  (CN 0 c e)) = [(e,c)]"
-  "\<alpha>\<rho> (Le  (CN 0 c e)) = [(Add (C -1) e,c)]"
-  "\<alpha>\<rho> p = []"
-
-    (* Simulates normal substituion by modifying the formula see correctness theorem *)
-
-recdef a\<rho> "measure size"
-  "a\<rho> (And p q) = (\<lambda> k. And (a\<rho> p k) (a\<rho> q k))" 
-  "a\<rho> (Or p q) = (\<lambda> k. Or (a\<rho> p k) (a\<rho> q k))" 
-  "a\<rho> (Eq (CN 0 c e)) = (\<lambda> k. if k dvd c then (Eq (CN 0 (c div k) e)) 
-                                           else (Eq (CN 0 c (Mul k e))))"
-  "a\<rho> (NEq (CN 0 c e)) = (\<lambda> k. if k dvd c then (NEq (CN 0 (c div k) e)) 
-                                           else (NEq (CN 0 c (Mul k e))))"
-  "a\<rho> (Lt (CN 0 c e)) = (\<lambda> k. if k dvd c then (Lt (CN 0 (c div k) e)) 
-                                           else (Lt (CN 0 c (Mul k e))))"
-  "a\<rho> (Le (CN 0 c e)) = (\<lambda> k. if k dvd c then (Le (CN 0 (c div k) e)) 
-                                           else (Le (CN 0 c (Mul k e))))"
-  "a\<rho> (Gt (CN 0 c e)) = (\<lambda> k. if k dvd c then (Gt (CN 0 (c div k) e)) 
-                                           else (Gt (CN 0 c (Mul k e))))"
-  "a\<rho> (Ge (CN 0 c e)) = (\<lambda> k. if k dvd c then (Ge (CN 0 (c div k) e)) 
-                                            else (Ge (CN 0 c (Mul k e))))"
-  "a\<rho> (Dvd i (CN 0 c e)) = (\<lambda> k. if k dvd c then (Dvd i (CN 0 (c div k) e)) 
-                                            else (Dvd (i*k) (CN 0 c (Mul k e))))"
-  "a\<rho> (NDvd i (CN 0 c e)) = (\<lambda> k. if k dvd c then (NDvd i (CN 0 (c div k) e)) 
-                                            else (NDvd (i*k) (CN 0 c (Mul k e))))"
-  "a\<rho> p = (\<lambda> k. p)"
-
-constdefs \<sigma> :: "fm \<Rightarrow> int \<Rightarrow> num \<Rightarrow> fm"
-  "\<sigma> p k t \<equiv> And (Dvd k t) (\<sigma>\<rho> p (t,k))"
-
-lemma \<sigma>\<rho>:
-  assumes linp: "iszlfm p (real (x::int)#bs)"
-  and kpos: "real k > 0"
-  and tnb: "numbound0 t"
-  and tint: "isint t (real x#bs)"
-  and kdt: "k dvd floor (Inum (b'#bs) t)"
-  shows "Ifm (real x#bs) (\<sigma>\<rho> p (t,k)) = 
-  (Ifm ((real ((floor (Inum (b'#bs) t)) div k))#bs) p)" 
-  (is "?I (real x) (?s p) = (?I (real ((floor (?N b' t)) div k)) p)" is "_ = (?I ?tk p)")
-using linp kpos tnb
-proof(induct p rule: \<sigma>\<rho>.induct)
-  case (3 c e) 
-  from prems have cp: "c > 0" and nb: "numbound0 e" by auto
-    {assume kdc: "k dvd c" 
-      from kpos have knz: "k\<noteq>0" by simp
-      from tint have ti: "real (floor (?N (real x) t)) = ?N (real x) t" using isint_def by simp
-      from prems have  ?case using real_of_int_div[OF knz kdc] real_of_int_div[OF knz kdt]
-	numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
-      numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"] by (simp add: ti) } 
-    moreover 
-    {assume "\<not> k dvd c"
-      from kpos have knz: "k\<noteq>0" by simp hence knz': "real k \<noteq> 0" by simp
-      from tint have ti: "real (floor (?N (real x) t)) = ?N (real x) t" using isint_def by simp
-      from prems have "?I (real x) (?s (Eq (CN 0 c e))) = ((real c * (?N (real x) t / real k) + ?N (real x) e)* real k = 0)"
-	using real_of_int_div[OF knz kdt]
-	  numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
-	  numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"] by (simp add: ti algebra_simps)
-      also have "\<dots> = (?I ?tk (Eq (CN 0 c e)))" using nonzero_eq_divide_eq[OF knz', where a="real c * (?N (real x) t / real k) + ?N (real x) e" and b="0", symmetric] real_of_int_div[OF knz kdt] numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
-	  numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"]
-	by (simp add: ti)
-      finally have ?case . }
-    ultimately show ?case by blast 
-next
-  case (4 c e)  
-  from prems have cp: "c > 0" and nb: "numbound0 e" by auto
-    {assume kdc: "k dvd c" 
-      from kpos have knz: "k\<noteq>0" by simp
-      from tint have ti: "real (floor (?N (real x) t)) = ?N (real x) t" using isint_def by simp
-      from prems have  ?case using real_of_int_div[OF knz kdc] real_of_int_div[OF knz kdt]
-	numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
-      numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"] by (simp add: ti) } 
-    moreover 
-    {assume "\<not> k dvd c"
-      from kpos have knz: "k\<noteq>0" by simp hence knz': "real k \<noteq> 0" by simp
-      from tint have ti: "real (floor (?N (real x) t)) = ?N (real x) t" using isint_def by simp
-      from prems have "?I (real x) (?s (NEq (CN 0 c e))) = ((real c * (?N (real x) t / real k) + ?N (real x) e)* real k \<noteq> 0)"
-	using real_of_int_div[OF knz kdt]
-	  numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
-	  numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"] by (simp add: ti algebra_simps)
-      also have "\<dots> = (?I ?tk (NEq (CN 0 c e)))" using nonzero_eq_divide_eq[OF knz', where a="real c * (?N (real x) t / real k) + ?N (real x) e" and b="0", symmetric] real_of_int_div[OF knz kdt] numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
-	  numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"]
-	by (simp add: ti)
-      finally have ?case . }
-    ultimately show ?case by blast 
-next
-  case (5 c e) 
-  from prems have cp: "c > 0" and nb: "numbound0 e" by auto
-    {assume kdc: "k dvd c" 
-      from kpos have knz: "k\<noteq>0" by simp
-      from tint have ti: "real (floor (?N (real x) t)) = ?N (real x) t" using isint_def by simp
-      from prems have  ?case using real_of_int_div[OF knz kdc] real_of_int_div[OF knz kdt]
-	numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
-      numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"] by (simp add: ti) } 
-    moreover 
-    {assume "\<not> k dvd c"
-      from kpos have knz: "k\<noteq>0" by simp
-      from tint have ti: "real (floor (?N (real x) t)) = ?N (real x) t" using isint_def by simp
-      from prems have "?I (real x) (?s (Lt (CN 0 c e))) = ((real c * (?N (real x) t / real k) + ?N (real x) e)* real k < 0)"
-	using real_of_int_div[OF knz kdt]
-	  numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
-	  numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"] by (simp add: ti algebra_simps)
-      also have "\<dots> = (?I ?tk (Lt (CN 0 c e)))" using pos_less_divide_eq[OF kpos, where a="real c * (?N (real x) t / real k) + ?N (real x) e" and b="0", symmetric] real_of_int_div[OF knz kdt] numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
-	  numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"]
-	by (simp add: ti)
-      finally have ?case . }
-    ultimately show ?case by blast 
-next
-  case (6 c e)  
-  from prems have cp: "c > 0" and nb: "numbound0 e" by auto
-    {assume kdc: "k dvd c" 
-      from kpos have knz: "k\<noteq>0" by simp
-      from tint have ti: "real (floor (?N (real x) t)) = ?N (real x) t" using isint_def by simp
-      from prems have  ?case using real_of_int_div[OF knz kdc] real_of_int_div[OF knz kdt]
-	numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
-      numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"] by (simp add: ti) } 
-    moreover 
-    {assume "\<not> k dvd c"
-      from kpos have knz: "k\<noteq>0" by simp
-      from tint have ti: "real (floor (?N (real x) t)) = ?N (real x) t" using isint_def by simp
-      from prems have "?I (real x) (?s (Le (CN 0 c e))) = ((real c * (?N (real x) t / real k) + ?N (real x) e)* real k \<le> 0)"
-	using real_of_int_div[OF knz kdt]
-	  numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
-	  numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"] by (simp add: ti algebra_simps)
-      also have "\<dots> = (?I ?tk (Le (CN 0 c e)))" using pos_le_divide_eq[OF kpos, where a="real c * (?N (real x) t / real k) + ?N (real x) e" and b="0", symmetric] real_of_int_div[OF knz kdt] numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
-	  numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"]
-	by (simp add: ti)
-      finally have ?case . }
-    ultimately show ?case by blast 
-next
-  case (7 c e) 
-  from prems have cp: "c > 0" and nb: "numbound0 e" by auto
-    {assume kdc: "k dvd c" 
-      from kpos have knz: "k\<noteq>0" by simp
-      from tint have ti: "real (floor (?N (real x) t)) = ?N (real x) t" using isint_def by simp
-      from prems have  ?case using real_of_int_div[OF knz kdc] real_of_int_div[OF knz kdt]
-	numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
-      numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"] by (simp add: ti) } 
-    moreover 
-    {assume "\<not> k dvd c"
-      from kpos have knz: "k\<noteq>0" by simp
-      from tint have ti: "real (floor (?N (real x) t)) = ?N (real x) t" using isint_def by simp
-      from prems have "?I (real x) (?s (Gt (CN 0 c e))) = ((real c * (?N (real x) t / real k) + ?N (real x) e)* real k > 0)"
-	using real_of_int_div[OF knz kdt]
-	  numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
-	  numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"] by (simp add: ti algebra_simps)
-      also have "\<dots> = (?I ?tk (Gt (CN 0 c e)))" using pos_divide_less_eq[OF kpos, where a="real c * (?N (real x) t / real k) + ?N (real x) e" and b="0", symmetric] real_of_int_div[OF knz kdt] numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
-	  numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"]
-	by (simp add: ti)
-      finally have ?case . }
-    ultimately show ?case by blast 
-next
-  case (8 c e)  
-  from prems have cp: "c > 0" and nb: "numbound0 e" by auto
-    {assume kdc: "k dvd c" 
-      from kpos have knz: "k\<noteq>0" by simp
-      from tint have ti: "real (floor (?N (real x) t)) = ?N (real x) t" using isint_def by simp
-      from prems have  ?case using real_of_int_div[OF knz kdc] real_of_int_div[OF knz kdt]
-	numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
-      numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"] by (simp add: ti) } 
-    moreover 
-    {assume "\<not> k dvd c"
-      from kpos have knz: "k\<noteq>0" by simp
-      from tint have ti: "real (floor (?N (real x) t)) = ?N (real x) t" using isint_def by simp
-      from prems have "?I (real x) (?s (Ge (CN 0 c e))) = ((real c * (?N (real x) t / real k) + ?N (real x) e)* real k \<ge> 0)"
-	using real_of_int_div[OF knz kdt]
-	  numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
-	  numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"] by (simp add: ti algebra_simps)
-      also have "\<dots> = (?I ?tk (Ge (CN 0 c e)))" using pos_divide_le_eq[OF kpos, where a="real c * (?N (real x) t / real k) + ?N (real x) e" and b="0", symmetric] real_of_int_div[OF knz kdt] numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
-	  numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"]
-	by (simp add: ti)
-      finally have ?case . }
-    ultimately show ?case by blast 
-next
-  case (9 i c e)   from prems have cp: "c > 0" and nb: "numbound0 e" by auto
-    {assume kdc: "k dvd c" 
-      from kpos have knz: "k\<noteq>0" by simp
-      from tint have ti: "real (floor (?N (real x) t)) = ?N (real x) t" using isint_def by simp
-      from prems have  ?case using real_of_int_div[OF knz kdc] real_of_int_div[OF knz kdt]
-	numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
-      numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"] by (simp add: ti) } 
-    moreover 
-    {assume "\<not> k dvd c"
-      from kpos have knz: "k\<noteq>0" by simp hence knz': "real k \<noteq> 0" by simp
-      from tint have ti: "real (floor (?N (real x) t)) = ?N (real x) t" using isint_def by simp
-      from prems have "?I (real x) (?s (Dvd i (CN 0 c e))) = (real i * real k rdvd (real c * (?N (real x) t / real k) + ?N (real x) e)* real k)"
-	using real_of_int_div[OF knz kdt]
-	  numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
-	  numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"] by (simp add: ti algebra_simps)
-      also have "\<dots> = (?I ?tk (Dvd i (CN 0 c e)))" using rdvd_mult[OF knz, where n="i"] real_of_int_div[OF knz kdt] numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
-	  numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"]
-	by (simp add: ti)
-      finally have ?case . }
-    ultimately show ?case by blast 
-next
-  case (10 i c e)    from prems have cp: "c > 0" and nb: "numbound0 e" by auto
-    {assume kdc: "k dvd c" 
-      from kpos have knz: "k\<noteq>0" by simp
-      from tint have ti: "real (floor (?N (real x) t)) = ?N (real x) t" using isint_def by simp
-      from prems have  ?case using real_of_int_div[OF knz kdc] real_of_int_div[OF knz kdt]
-	numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
-      numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"] by (simp add: ti) } 
-    moreover 
-    {assume "\<not> k dvd c"
-      from kpos have knz: "k\<noteq>0" by simp hence knz': "real k \<noteq> 0" by simp
-      from tint have ti: "real (floor (?N (real x) t)) = ?N (real x) t" using isint_def by simp
-      from prems have "?I (real x) (?s (NDvd i (CN 0 c e))) = (\<not> (real i * real k rdvd (real c * (?N (real x) t / real k) + ?N (real x) e)* real k))"
-	using real_of_int_div[OF knz kdt]
-	  numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
-	  numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"] by (simp add: ti algebra_simps)
-      also have "\<dots> = (?I ?tk (NDvd i (CN 0 c e)))" using rdvd_mult[OF knz, where n="i"] real_of_int_div[OF knz kdt] numbound0_I[OF tnb, where bs="bs" and b="b'" and b'="real x"]
-	  numbound0_I[OF nb, where bs="bs" and b="?tk" and b'="real x"]
-	by (simp add: ti)
-      finally have ?case . }
-    ultimately show ?case by blast 
-qed (simp_all add: nth_pos2 bound0_I[where bs="bs" and b="real ((floor (?N b' t)) div k)" and b'="real x"] numbound0_I[where bs="bs" and b="real ((floor (?N b' t)) div k)" and b'="real x"])
-
-
-lemma a\<rho>: 
-  assumes lp: "iszlfm p (real (x::int)#bs)" and kp: "real k > 0" 
-  shows "Ifm (real (x*k)#bs) (a\<rho> p k) = Ifm (real x#bs) p" (is "?I (x*k) (?f p k) = ?I x p")
-using lp bound0_I[where bs="bs" and b="real (x*k)" and b'="real x"] numbound0_I[where bs="bs" and b="real (x*k)" and b'="real x"]
-proof(induct p rule: a\<rho>.induct)
-  case (3 c e)  
-  from prems have cp: "c > 0" and nb: "numbound0 e" by auto
-  from kp have knz: "k\<noteq>0" by simp hence knz': "real k \<noteq> 0" by simp
-    {assume kdc: "k dvd c" from prems have  ?case using real_of_int_div[OF knz kdc] by simp } 
-    moreover 
-    {assume nkdc: "\<not> k dvd c" hence ?case using numbound0_I[OF nb, where bs="bs" and b="real (x*k)" and b'="real x"] nonzero_eq_divide_eq[OF knz', where b="0" and a="real c * real x + Inum (real x # bs) e", symmetric] by (simp add: algebra_simps)}
-    ultimately show ?case by blast 
-next
-  case (4 c e)   
-  from prems have cp: "c > 0" and nb: "numbound0 e" by auto
-  from kp have knz: "k\<noteq>0" by simp hence knz': "real k \<noteq> 0" by simp
-    {assume kdc: "k dvd c" from prems have  ?case using real_of_int_div[OF knz kdc] by simp } 
-    moreover 
-    {assume nkdc: "\<not> k dvd c" hence ?case using numbound0_I[OF nb, where bs="bs" and b="real (x*k)" and b'="real x"] nonzero_eq_divide_eq[OF knz', where b="0" and a="real c * real x + Inum (real x # bs) e", symmetric] by (simp add: algebra_simps)}
-    ultimately show ?case by blast 
-next
-  case (5 c e)   
-  from prems have cp: "c > 0" and nb: "numbound0 e" by auto
-  from kp have knz: "k\<noteq>0" by simp hence knz': "real k \<noteq> 0" by simp
-    {assume kdc: "k dvd c" from prems have  ?case using real_of_int_div[OF knz kdc] by simp } 
-    moreover 
-    {assume nkdc: "\<not> k dvd c" hence ?case using numbound0_I[OF nb, where bs="bs" and b="real (x*k)" and b'="real x"] pos_less_divide_eq[OF kp, where b="0" and a="real c * real x + Inum (real x # bs) e", symmetric] by (simp add: algebra_simps)}
-    ultimately show ?case by blast 
-next
-  case (6 c e)    
-  from prems have cp: "c > 0" and nb: "numbound0 e" by auto
-  from kp have knz: "k\<noteq>0" by simp hence knz': "real k \<noteq> 0" by simp
-    {assume kdc: "k dvd c" from prems have  ?case using real_of_int_div[OF knz kdc] by simp } 
-    moreover 
-    {assume nkdc: "\<not> k dvd c" hence ?case using numbound0_I[OF nb, where bs="bs" and b="real (x*k)" and b'="real x"] pos_le_divide_eq[OF kp, where b="0" and a="real c * real x + Inum (real x # bs) e", symmetric] by (simp add: algebra_simps)}
-    ultimately show ?case by blast 
-next
-  case (7 c e)    
-  from prems have cp: "c > 0" and nb: "numbound0 e" by auto
-  from kp have knz: "k\<noteq>0" by simp hence knz': "real k \<noteq> 0" by simp
-    {assume kdc: "k dvd c" from prems have  ?case using real_of_int_div[OF knz kdc] by simp } 
-    moreover 
-    {assume nkdc: "\<not> k dvd c" hence ?case using numbound0_I[OF nb, where bs="bs" and b="real (x*k)" and b'="real x"] pos_divide_less_eq[OF kp, where b="0" and a="real c * real x + Inum (real x # bs) e", symmetric] by (simp add: algebra_simps)}
-    ultimately show ?case by blast 
-next
-  case (8 c e)    
-  from prems have cp: "c > 0" and nb: "numbound0 e" by auto
-  from kp have knz: "k\<noteq>0" by simp hence knz': "real k \<noteq> 0" by simp
-    {assume kdc: "k dvd c" from prems have  ?case using real_of_int_div[OF knz kdc] by simp } 
-    moreover 
-    {assume nkdc: "\<not> k dvd c" hence ?case using numbound0_I[OF nb, where bs="bs" and b="real (x*k)" and b'="real x"] pos_divide_le_eq[OF kp, where b="0" and a="real c * real x + Inum (real x # bs) e", symmetric] by (simp add: algebra_simps)}
-    ultimately show ?case by blast 
-next
-  case (9 i c e)
-  from prems have cp: "c > 0" and nb: "numbound0 e" by auto
-  from kp have knz: "k\<noteq>0" by simp hence knz': "real k \<noteq> 0" by simp
-  {assume kdc: "k dvd c" from prems have  ?case using real_of_int_div[OF knz kdc] by simp } 
-  moreover 
-  {assume "\<not> k dvd c"
-    hence "Ifm (real (x*k)#bs) (a\<rho> (Dvd i (CN 0 c e)) k) = 
-      (real i * real k rdvd (real c * real x + Inum (real x#bs) e) * real k)" 
-      using numbound0_I[OF nb, where bs="bs" and b="real (x*k)" and b'="real x"] 
-      by (simp add: algebra_simps)
-    also have "\<dots> = (Ifm (real x#bs) (Dvd i (CN 0 c e)))" by (simp add: rdvd_mult[OF knz, where n="i"])
-    finally have ?case . }
-  ultimately show ?case by blast 
-next
-  case (10 i c e) 
-  from prems have cp: "c > 0" and nb: "numbound0 e" by auto
-  from kp have knz: "k\<noteq>0" by simp hence knz': "real k \<noteq> 0" by simp
-  {assume kdc: "k dvd c" from prems have  ?case using real_of_int_div[OF knz kdc] by simp } 
-  moreover 
-  {assume "\<not> k dvd c"
-    hence "Ifm (real (x*k)#bs) (a\<rho> (NDvd i (CN 0 c e)) k) = 
-      (\<not> (real i * real k rdvd (real c * real x + Inum (real x#bs) e) * real k))" 
-      using numbound0_I[OF nb, where bs="bs" and b="real (x*k)" and b'="real x"] 
-      by (simp add: algebra_simps)
-    also have "\<dots> = (Ifm (real x#bs) (NDvd i (CN 0 c e)))" by (simp add: rdvd_mult[OF knz, where n="i"])
-    finally have ?case . }
-  ultimately show ?case by blast 
-qed (simp_all add: nth_pos2)
-
-lemma a\<rho>_ex: 
-  assumes lp: "iszlfm p (real (x::int)#bs)" and kp: "k > 0"
-  shows "(\<exists> (x::int). real k rdvd real x \<and> Ifm (real x#bs) (a\<rho> p k)) = 
-  (\<exists> (x::int). Ifm (real x#bs) p)" (is "(\<exists> x. ?D x \<and> ?P' x) = (\<exists> x. ?P x)")
-proof-
-  have "(\<exists> x. ?D x \<and> ?P' x) = (\<exists> x. k dvd x \<and> ?P' x)" using int_rdvd_iff by simp
-  also have "\<dots> = (\<exists>x. ?P' (x*k))" using unity_coeff_ex[where P="?P'" and l="k", simplified]
-    by (simp add: algebra_simps)
-  also have "\<dots> = (\<exists> x. ?P x)" using a\<rho> iszlfm_gen[OF lp] kp by auto
-  finally show ?thesis .
-qed
-
-lemma \<sigma>\<rho>': assumes lp: "iszlfm p (real (x::int)#bs)" and kp: "k > 0" and nb: "numbound0 t"
-  shows "Ifm (real x#bs) (\<sigma>\<rho> p (t,k)) = Ifm ((Inum (real x#bs) t)#bs) (a\<rho> p k)"
-using lp 
-by(induct p rule: \<sigma>\<rho>.induct, simp_all add: 
-  numbound0_I[OF nb, where bs="bs" and b="Inum (real x#bs) t" and b'="real x"] 
-  numbound0_I[where bs="bs" and b="Inum (real x#bs) t" and b'="real x"] 
-  bound0_I[where bs="bs" and b="Inum (real x#bs) t" and b'="real x"] nth_pos2 cong: imp_cong)
-
-lemma \<sigma>\<rho>_nb: assumes lp:"iszlfm p (a#bs)" and nb: "numbound0 t"
-  shows "bound0 (\<sigma>\<rho> p (t,k))"
-  using lp
-  by (induct p rule: iszlfm.induct, auto simp add: nb)
-
-lemma \<rho>_l:
-  assumes lp: "iszlfm p (real (i::int)#bs)"
-  shows "\<forall> (b,k) \<in> set (\<rho> p). k >0 \<and> numbound0 b \<and> isint b (real i#bs)"
-using lp by (induct p rule: \<rho>.induct, auto simp add: isint_sub isint_neg)
-
-lemma \<alpha>\<rho>_l:
-  assumes lp: "iszlfm p (real (i::int)#bs)"
-  shows "\<forall> (b,k) \<in> set (\<alpha>\<rho> p). k >0 \<and> numbound0 b \<and> isint b (real i#bs)"
-using lp isint_add [OF isint_c[where j="- 1"],where bs="real i#bs"]
- by (induct p rule: \<alpha>\<rho>.induct, auto)
-
-lemma zminusinf_\<rho>:
-  assumes lp: "iszlfm p (real (i::int)#bs)"
-  and nmi: "\<not> (Ifm (real i#bs) (minusinf p))" (is "\<not> (Ifm (real i#bs) (?M p))")
-  and ex: "Ifm (real i#bs) p" (is "?I i p")
-  shows "\<exists> (e,c) \<in> set (\<rho> p). real (c*i) > Inum (real i#bs) e" (is "\<exists> (e,c) \<in> ?R p. real (c*i) > ?N i e")
-  using lp nmi ex
-by (induct p rule: minusinf.induct, auto)
-
-
-lemma \<sigma>_And: "Ifm bs (\<sigma> (And p q) k t)  = Ifm bs (And (\<sigma> p k t) (\<sigma> q k t))"
-using \<sigma>_def by auto
-lemma \<sigma>_Or: "Ifm bs (\<sigma> (Or p q) k t)  = Ifm bs (Or (\<sigma> p k t) (\<sigma> q k t))"
-using \<sigma>_def by auto
-
-lemma \<rho>: assumes lp: "iszlfm p (real (i::int) #bs)"
-  and pi: "Ifm (real i#bs) p"
-  and d: "d\<delta> p d"
-  and dp: "d > 0"
-  and nob: "\<forall>(e,c) \<in> set (\<rho> p). \<forall> j\<in> {1 .. c*d}. real (c*i) \<noteq> Inum (real i#bs) e + real j"
-  (is "\<forall>(e,c) \<in> set (\<rho> p). \<forall> j\<in> {1 .. c*d}. _ \<noteq> ?N i e + _")
-  shows "Ifm (real(i - d)#bs) p"
-  using lp pi d nob
-proof(induct p rule: iszlfm.induct)
-  case (3 c e) hence cp: "c >0" and nb: "numbound0 e" and ei: "isint e (real i#bs)"
-    and pi: "real (c*i) = - 1 -  ?N i e + real (1::int)" and nob: "\<forall> j\<in> {1 .. c*d}. real (c*i) \<noteq> -1 - ?N i e + real j"
-    by simp+
-  from mult_strict_left_mono[OF dp cp]  have one:"1 \<in> {1 .. c*d}" by auto
-  from nob[rule_format, where j="1", OF one] pi show ?case by simp
-next
-  case (4 c e)  
-  hence cp: "c >0" and nb: "numbound0 e" and ei: "isint e (real i#bs)"
-    and nob: "\<forall> j\<in> {1 .. c*d}. real (c*i) \<noteq> - ?N i e + real j"
-    by simp+
-  {assume "real (c*i) \<noteq> - ?N i e + real (c*d)"
-    with numbound0_I[OF nb, where bs="bs" and b="real i - real d" and b'="real i"]
-    have ?case by (simp add: algebra_simps)}
-  moreover
-  {assume pi: "real (c*i) = - ?N i e + real (c*d)"
-    from mult_strict_left_mono[OF dp cp] have d: "(c*d) \<in> {1 .. c*d}" by simp
-    from nob[rule_format, where j="c*d", OF d] pi have ?case by simp }
-  ultimately show ?case by blast
-next
-  case (5 c e) hence cp: "c > 0" by simp
-  from prems mult_strict_left_mono[OF dp cp, simplified real_of_int_less_iff[symmetric] 
-    real_of_int_mult]
-  show ?case using prems dp 
-    by (simp add: add: numbound0_I[where bs="bs" and b="real i - real d" and b'="real i"] 
-      algebra_simps)
-next
-  case (6 c e)  hence cp: "c > 0" by simp
-  from prems mult_strict_left_mono[OF dp cp, simplified real_of_int_less_iff[symmetric] 
-    real_of_int_mult]
-  show ?case using prems dp 
-    by (simp add: add: numbound0_I[where bs="bs" and b="real i - real d" and b'="real i"] 
-      algebra_simps)
-next
-  case (7 c e) hence cp: "c >0" and nb: "numbound0 e" and ei: "isint e (real i#bs)"
-    and nob: "\<forall> j\<in> {1 .. c*d}. real (c*i) \<noteq> - ?N i e + real j"
-    and pi: "real (c*i) + ?N i e > 0" and cp': "real c >0"
-    by simp+
-  let ?fe = "floor (?N i e)"
-  from pi cp have th:"(real i +?N i e / real c)*real c > 0" by (simp add: algebra_simps)
-  from pi ei[simplified isint_iff] have "real (c*i + ?fe) > real (0::int)" by simp
-  hence pi': "c*i + ?fe > 0" by (simp only: real_of_int_less_iff[symmetric])
-  have "real (c*i) + ?N i e > real (c*d) \<or> real (c*i) + ?N i e \<le> real (c*d)" by auto
-  moreover
-  {assume "real (c*i) + ?N i e > real (c*d)" hence ?case
-      by (simp add: algebra_simps 
-	numbound0_I[OF nb,where bs="bs" and b="real i - real d" and b'="real i"])} 
-  moreover 
-  {assume H:"real (c*i) + ?N i e \<le> real (c*d)"
-    with ei[simplified isint_iff] have "real (c*i + ?fe) \<le> real (c*d)" by simp
-    hence pid: "c*i + ?fe \<le> c*d" by (simp only: real_of_int_le_iff)
-    with pi' have "\<exists> j1\<in> {1 .. c*d}. c*i + ?fe = j1" by auto
-    hence "\<exists> j1\<in> {1 .. c*d}. real (c*i) = - ?N i e + real j1" 
-      by (simp only: diff_def[symmetric] real_of_int_mult real_of_int_add real_of_int_inject[symmetric] ei[simplified isint_iff] algebra_simps)
-    with nob  have ?case by blast }
-  ultimately show ?case by blast
-next
-  case (8 c e)  hence cp: "c >0" and nb: "numbound0 e" and ei: "isint e (real i#bs)"
-    and nob: "\<forall> j\<in> {1 .. c*d}. real (c*i) \<noteq> - 1 - ?N i e + real j"
-    and pi: "real (c*i) + ?N i e \<ge> 0" and cp': "real c >0"
-    by simp+
-  let ?fe = "floor (?N i e)"
-  from pi cp have th:"(real i +?N i e / real c)*real c \<ge> 0" by (simp add: algebra_simps)
-  from pi ei[simplified isint_iff] have "real (c*i + ?fe) \<ge> real (0::int)" by simp
-  hence pi': "c*i + 1 + ?fe \<ge> 1" by (simp only: real_of_int_le_iff[symmetric])
-  have "real (c*i) + ?N i e \<ge> real (c*d) \<or> real (c*i) + ?N i e < real (c*d)" by auto
-  moreover
-  {assume "real (c*i) + ?N i e \<ge> real (c*d)" hence ?case
-      by (simp add: algebra_simps 
-	numbound0_I[OF nb,where bs="bs" and b="real i - real d" and b'="real i"])} 
-  moreover 
-  {assume H:"real (c*i) + ?N i e < real (c*d)"
-    with ei[simplified isint_iff] have "real (c*i + ?fe) < real (c*d)" by simp
-    hence pid: "c*i + 1 + ?fe \<le> c*d" by (simp only: real_of_int_le_iff)
-    with pi' have "\<exists> j1\<in> {1 .. c*d}. c*i + 1+ ?fe = j1" by auto
-    hence "\<exists> j1\<in> {1 .. c*d}. real (c*i) + 1= - ?N i e + real j1"
-      by (simp only: diff_def[symmetric] real_of_int_mult real_of_int_add real_of_int_inject[symmetric] ei[simplified isint_iff] algebra_simps real_of_one) 
-    hence "\<exists> j1\<in> {1 .. c*d}. real (c*i) = (- ?N i e + real j1) - 1"
-      by (simp only: algebra_simps diff_def[symmetric])
-        hence "\<exists> j1\<in> {1 .. c*d}. real (c*i) = - 1 - ?N i e + real j1"
-	  by (simp only: add_ac diff_def)
-    with nob  have ?case by blast }
-  ultimately show ?case by blast
-next
-  case (9 j c e)  hence p: "real j rdvd real (c*i) + ?N i e" (is "?p x") and cp: "c > 0" and bn:"numbound0 e"  by simp+
-    let ?e = "Inum (real i # bs) e"
-    from prems have "isint e (real i #bs)"  by simp 
-    hence ie: "real (floor ?e) = ?e" using isint_iff[where n="e" and bs="(real i)#bs"] numbound0_I[OF bn,where b="real i" and b'="real i" and bs="bs"]
-      by (simp add: isint_iff)
-    from prems have id: "j dvd d" by simp
-    from ie[symmetric] have "?p i = (real j rdvd real (c*i+ floor ?e))" by simp
-    also have "\<dots> = (j dvd c*i + floor ?e)" 
-      using int_rdvd_iff [where i="j" and t="c*i+ floor ?e"] by simp
-    also have "\<dots> = (j dvd c*i - c*d + floor ?e)" 
-      using dvd_period[OF id, where x="c*i" and c="-c" and t="floor ?e"] by simp
-    also have "\<dots> = (real j rdvd real (c*i - c*d + floor ?e))" 
-      using int_rdvd_iff[where i="j" and t="(c*i - c*d + floor ?e)",symmetric, simplified]
-      ie by simp
-    also have "\<dots> = (real j rdvd real (c*(i - d)) + ?e)" 
-      using ie by (simp add:algebra_simps)
-    finally show ?case 
-      using numbound0_I[OF bn,where b="real i - real d" and b'="real i" and bs="bs"] p 
-      by (simp add: algebra_simps)
-next
-  case (10 j c e)   hence p: "\<not> (real j rdvd real (c*i) + ?N i e)" (is "?p x") and cp: "c > 0" and bn:"numbound0 e"  by simp+
-    let ?e = "Inum (real i # bs) e"
-    from prems have "isint e (real i #bs)"  by simp 
-    hence ie: "real (floor ?e) = ?e" using isint_iff[where n="e" and bs="(real i)#bs"] numbound0_I[OF bn,where b="real i" and b'="real i" and bs="bs"]
-      by (simp add: isint_iff)
-    from prems have id: "j dvd d" by simp
-    from ie[symmetric] have "?p i = (\<not> (real j rdvd real (c*i+ floor ?e)))" by simp
-    also have "\<dots> = Not (j dvd c*i + floor ?e)" 
-      using int_rdvd_iff [where i="j" and t="c*i+ floor ?e"] by simp
-    also have "\<dots> = Not (j dvd c*i - c*d + floor ?e)" 
-      using dvd_period[OF id, where x="c*i" and c="-c" and t="floor ?e"] by simp
-    also have "\<dots> = Not (real j rdvd real (c*i - c*d + floor ?e))" 
-      using int_rdvd_iff[where i="j" and t="(c*i - c*d + floor ?e)",symmetric, simplified]
-      ie by simp
-    also have "\<dots> = Not (real j rdvd real (c*(i - d)) + ?e)" 
-      using ie by (simp add:algebra_simps)
-    finally show ?case 
-      using numbound0_I[OF bn,where b="real i - real d" and b'="real i" and bs="bs"] p 
-      by (simp add: algebra_simps)
-qed(auto simp add: numbound0_I[where bs="bs" and b="real i - real d" and b'="real i"] nth_pos2)
-
-lemma \<sigma>_nb: assumes lp: "iszlfm p (a#bs)" and nb: "numbound0 t"
-  shows "bound0 (\<sigma> p k t)"
-  using \<sigma>\<rho>_nb[OF lp nb] nb by (simp add: \<sigma>_def)
-  
-lemma \<rho>':   assumes lp: "iszlfm p (a #bs)"
-  and d: "d\<delta> p d"
-  and dp: "d > 0"
-  shows "\<forall> x. \<not>(\<exists> (e,c) \<in> set(\<rho> p). \<exists>(j::int) \<in> {1 .. c*d}. Ifm (a #bs) (\<sigma> p c (Add e (C j)))) \<longrightarrow> Ifm (real x#bs) p \<longrightarrow> Ifm (real (x - d)#bs) p" (is "\<forall> x. ?b x \<longrightarrow> ?P x \<longrightarrow> ?P (x - d)")
-proof(clarify)
-  fix x 
-  assume nob1:"?b x" and px: "?P x" 
-  from iszlfm_gen[OF lp, rule_format, where y="real x"] have lp': "iszlfm p (real x#bs)".
-  have nob: "\<forall>(e, c)\<in>set (\<rho> p). \<forall>j\<in>{1..c * d}. real (c * x) \<noteq> Inum (real x # bs) e + real j" 
-  proof(clarify)
-    fix e c j assume ecR: "(e,c) \<in> set (\<rho> p)" and jD: "j\<in> {1 .. c*d}"
-      and cx: "real (c*x) = Inum (real x#bs) e + real j"
-    let ?e = "Inum (real x#bs) e"
-    let ?fe = "floor ?e"
-    from \<rho>_l[OF lp'] ecR have ei:"isint e (real x#bs)" and cp:"c>0" and nb:"numbound0 e"
-      by auto
-    from numbound0_gen [OF nb ei, rule_format,where y="a"] have "isint e (a#bs)" .
-    from cx ei[simplified isint_iff] have "real (c*x) = real (?fe + j)" by simp
-    hence cx: "c*x = ?fe + j" by (simp only: real_of_int_inject)
-    hence cdej:"c dvd ?fe + j" by (simp add: dvd_def) (rule_tac x="x" in exI, simp)
-    hence "real c rdvd real (?fe + j)" by (simp only: int_rdvd_iff)
-    hence rcdej: "real c rdvd ?e + real j" by (simp add: ei[simplified isint_iff])
-    from cx have "(c*x) div c = (?fe + j) div c" by simp
-    with cp have "x = (?fe + j) div c" by simp
-    with px have th: "?P ((?fe + j) div c)" by auto
-    from cp have cp': "real c > 0" by simp
-    from cdej have cdej': "c dvd floor (Inum (real x#bs) (Add e (C j)))" by simp
-    from nb have nb': "numbound0 (Add e (C j))" by simp
-    have ji: "isint (C j) (real x#bs)" by (simp add: isint_def)
-    from isint_add[OF ei ji] have ei':"isint (Add e (C j)) (real x#bs)" .
-    from th \<sigma>\<rho>[where b'="real x", OF lp' cp' nb' ei' cdej',symmetric]
-    have "Ifm (real x#bs) (\<sigma>\<rho> p (Add e (C j), c))" by simp
-    with rcdej have th: "Ifm (real x#bs) (\<sigma> p c (Add e (C j)))" by (simp add: \<sigma>_def)
-    from th bound0_I[OF \<sigma>_nb[OF lp nb', where k="c"],where bs="bs" and b="real x" and b'="a"]
-    have "Ifm (a#bs) (\<sigma> p c (Add e (C j)))" by blast
-      with ecR jD nob1    show "False" by blast
-  qed
-  from \<rho>[OF lp' px d dp nob] show "?P (x -d )" . 
-qed
-
-
-lemma rl_thm: 
-  assumes lp: "iszlfm p (real (i::int)#bs)"
-  shows "(\<exists> (x::int). Ifm (real x#bs) p) = ((\<exists> j\<in> {1 .. \<delta> p}. Ifm (real j#bs) (minusinf p)) \<or> (\<exists> (e,c) \<in> set (\<rho> p). \<exists> j\<in> {1 .. c*(\<delta> p)}. Ifm (a#bs) (\<sigma> p c (Add e (C j)))))"
-  (is "(\<exists>(x::int). ?P x) = ((\<exists> j\<in> {1.. \<delta> p}. ?MP j)\<or>(\<exists> (e,c) \<in> ?R. \<exists> j\<in> _. ?SP c e j))" 
-    is "?lhs = (?MD \<or> ?RD)"  is "?lhs = ?rhs")
-proof-
-  let ?d= "\<delta> p"
-  from \<delta>[OF lp] have d:"d\<delta> p ?d" and dp: "?d > 0" by auto
-  { assume H:"?MD" hence th:"\<exists> (x::int). ?MP x" by blast
-    from H minusinf_ex[OF lp th] have ?thesis  by blast}
-  moreover
-  { fix e c j assume exR:"(e,c) \<in> ?R" and jD:"j\<in> {1 .. c*?d}" and spx:"?SP c e j"
-    from exR \<rho>_l[OF lp] have nb: "numbound0 e" and ei:"isint e (real i#bs)" and cp: "c > 0"
-      by auto
-    have "isint (C j) (real i#bs)" by (simp add: isint_iff)
-    with isint_add[OF numbound0_gen[OF nb ei,rule_format, where y="real i"]]
-    have eji:"isint (Add e (C j)) (real i#bs)" by simp
-    from nb have nb': "numbound0 (Add e (C j))" by simp
-    from spx bound0_I[OF \<sigma>_nb[OF lp nb', where k="c"], where bs="bs" and b="a" and b'="real i"]
-    have spx': "Ifm (real i # bs) (\<sigma> p c (Add e (C j)))" by blast
-    from spx' have rcdej:"real c rdvd (Inum (real i#bs) (Add e (C j)))" 
-      and sr:"Ifm (real i#bs) (\<sigma>\<rho> p (Add e (C j),c))" by (simp add: \<sigma>_def)+
-    from rcdej eji[simplified isint_iff] 
-    have "real c rdvd real (floor (Inum (real i#bs) (Add e (C j))))" by simp
-    hence cdej:"c dvd floor (Inum (real i#bs) (Add e (C j)))" by (simp only: int_rdvd_iff)
-    from cp have cp': "real c > 0" by simp
-    from \<sigma>\<rho>[OF lp cp' nb' eji cdej] spx' have "?P (\<lfloor>Inum (real i # bs) (Add e (C j))\<rfloor> div c)"
-      by (simp add: \<sigma>_def)
-    hence ?lhs by blast
-    with exR jD spx have ?thesis by blast}
-  moreover
-  { fix x assume px: "?P x" and nob: "\<not> ?RD"
-    from iszlfm_gen [OF lp,rule_format, where y="a"] have lp':"iszlfm p (a#bs)" .
-    from \<rho>'[OF lp' d dp, rule_format, OF nob] have th:"\<forall> x. ?P x \<longrightarrow> ?P (x - ?d)" by blast
-    from minusinf_inf[OF lp] obtain z where z:"\<forall> x<z. ?MP x = ?P x" by blast
-    have zp: "abs (x - z) + 1 \<ge> 0" by arith
-    from decr_lemma[OF dp,where x="x" and z="z"] 
-      decr_mult_lemma[OF dp th zp, rule_format, OF px] z have th:"\<exists> x. ?MP x" by auto
-    with minusinf_bex[OF lp] px nob have ?thesis by blast}
-  ultimately show ?thesis by blast
-qed
-
-lemma mirror_\<alpha>\<rho>:   assumes lp: "iszlfm p (a#bs)"
-  shows "(\<lambda> (t,k). (Inum (a#bs) t, k)) ` set (\<alpha>\<rho> p) = (\<lambda> (t,k). (Inum (a#bs) t,k)) ` set (\<rho> (mirror p))"
-using lp
-by (induct p rule: mirror.induct, simp_all add: split_def image_Un )
-  
-text {* The @{text "\<real>"} part*}
-
-text{* Linearity for fm where Bound 0 ranges over @{text "\<real>"}*}
-consts
-  isrlfm :: "fm \<Rightarrow> bool"   (* Linearity test for fm *)
-recdef isrlfm "measure size"
-  "isrlfm (And p q) = (isrlfm p \<and> isrlfm q)" 
-  "isrlfm (Or p q) = (isrlfm p \<and> isrlfm q)" 
-  "isrlfm (Eq  (CN 0 c e)) = (c>0 \<and> numbound0 e)"
-  "isrlfm (NEq (CN 0 c e)) = (c>0 \<and> numbound0 e)"
-  "isrlfm (Lt  (CN 0 c e)) = (c>0 \<and> numbound0 e)"
-  "isrlfm (Le  (CN 0 c e)) = (c>0 \<and> numbound0 e)"
-  "isrlfm (Gt  (CN 0 c e)) = (c>0 \<and> numbound0 e)"
-  "isrlfm (Ge  (CN 0 c e)) = (c>0 \<and> numbound0 e)"
-  "isrlfm p = (isatom p \<and> (bound0 p))"
-
-constdefs fp :: "fm \<Rightarrow> int \<Rightarrow> num \<Rightarrow> int \<Rightarrow> fm"
-  "fp p n s j \<equiv> (if n > 0 then 
-            (And p (And (Ge (CN 0 n (Sub s (Add (Floor s) (C j)))))
-                        (Lt (CN 0 n (Sub s (Add (Floor s) (C (j+1))))))))
-            else 
-            (And p (And (Le (CN 0 (-n) (Add (Neg s) (Add (Floor s) (C j))))) 
-                        (Gt (CN 0 (-n) (Add (Neg s) (Add (Floor s) (C (j + 1)))))))))"
-
-  (* splits the bounded from the unbounded part*)
-consts rsplit0 :: "num \<Rightarrow> (fm \<times> int \<times> num) list" 
-recdef rsplit0 "measure num_size"
-  "rsplit0 (Bound 0) = [(T,1,C 0)]"
-  "rsplit0 (Add a b) = (let acs = rsplit0 a ; bcs = rsplit0 b 
-              in map (\<lambda> ((p,n,t),(q,m,s)). (And p q, n+m, Add t s)) [(a,b). a\<leftarrow>acs,b\<leftarrow>bcs])"
-  "rsplit0 (Sub a b) = rsplit0 (Add a (Neg b))"
-  "rsplit0 (Neg a) = map (\<lambda> (p,n,s). (p,-n,Neg s)) (rsplit0 a)"
-  "rsplit0 (Floor a) = foldl (op @) [] (map 
-      (\<lambda> (p,n,s). if n=0 then [(p,0,Floor s)]
-          else (map (\<lambda> j. (fp p n s j, 0, Add (Floor s) (C j))) (if n > 0 then iupt (0,n) else iupt(n,0))))
-       (rsplit0 a))"
-  "rsplit0 (CN 0 c a) = map (\<lambda> (p,n,s). (p,n+c,s)) (rsplit0 a)"
-  "rsplit0 (CN m c a) = map (\<lambda> (p,n,s). (p,n,CN m c s)) (rsplit0 a)"
-  "rsplit0 (CF c t s) = rsplit0 (Add (Mul c (Floor t)) s)"
-  "rsplit0 (Mul c a) = map (\<lambda> (p,n,s). (p,c*n,Mul c s)) (rsplit0 a)"
-  "rsplit0 t = [(T,0,t)]"
-
-lemma not_rl[simp]: "isrlfm p \<Longrightarrow> isrlfm (not p)"
-  by (induct p rule: isrlfm.induct, auto)
-lemma conj_rl[simp]: "isrlfm p \<Longrightarrow> isrlfm q \<Longrightarrow> isrlfm (conj p q)"
-  using conj_def by (cases p, auto)
-lemma disj_rl[simp]: "isrlfm p \<Longrightarrow> isrlfm q \<Longrightarrow> isrlfm (disj p q)"
-  using disj_def by (cases p, auto)
-
-
-lemma rsplit0_cs:
-  shows "\<forall> (p,n,s) \<in> set (rsplit0 t). 
-  (Ifm (x#bs) p \<longrightarrow>  (Inum (x#bs) t = Inum (x#bs) (CN 0 n s))) \<and> numbound0 s \<and> isrlfm p" 
-  (is "\<forall> (p,n,s) \<in> ?SS t. (?I p \<longrightarrow> ?N t = ?N (CN 0 n s)) \<and> _ \<and> _ ")
-proof(induct t rule: rsplit0.induct)
-  case (5 a) 
-  let ?p = "\<lambda> (p,n,s) j. fp p n s j"
-  let ?f = "(\<lambda> (p,n,s) j. (?p (p,n,s) j, (0::int),Add (Floor s) (C j)))"
-  let ?J = "\<lambda> n. if n>0 then iupt (0,n) else iupt (n,0)"
-  let ?ff=" (\<lambda> (p,n,s). if n= 0 then [(p,0,Floor s)] else map (?f (p,n,s)) (?J n))"
-  have int_cases: "\<forall> (i::int). i= 0 \<or> i < 0 \<or> i > 0" by arith
-  have U1: "(UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n=0} (\<lambda> (p,n,s). set (?ff (p,n,s)))) = 
-    (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n=0} (\<lambda> (p,n,s). set [(p,0,Floor s)]))" by auto
-  have U2': "\<forall> (p,n,s) \<in> {(p,n,s). (p,n,s) \<in> ?SS a \<and> n>0}. 
-    ?ff (p,n,s) = map (?f(p,n,s)) (iupt(0,n))" by auto
-  hence U2: "(UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n>0} (\<lambda> (p,n,s). set (?ff (p,n,s)))) = 
-    (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n>0} (\<lambda> (p,n,s). 
-    set (map (?f(p,n,s)) (iupt(0,n)))))"
-  proof-
-    fix M :: "('a\<times>'b\<times>'c) set" and f :: "('a\<times>'b\<times>'c) \<Rightarrow> 'd list" and g
-    assume "\<forall> (a,b,c) \<in> M. f (a,b,c) = g a b c"
-    thus "(UNION M (\<lambda> (a,b,c). set (f (a,b,c)))) = (UNION M (\<lambda> (a,b,c). set (g a b c)))"
-      by (auto simp add: split_def)
-  qed
-  have U3': "\<forall> (p,n,s) \<in> {(p,n,s). (p,n,s) \<in> ?SS a \<and> n<0}. ?ff (p,n,s) = map (?f(p,n,s)) (iupt(n,0))"
-    by auto
-  hence U3: "(UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n<0} (\<lambda> (p,n,s). set (?ff (p,n,s)))) = 
-    (UNION {(p,n,s). (p,n,s)\<in> ?SS a\<and>n<0} (\<lambda>(p,n,s). set (map (?f(p,n,s)) (iupt(n,0)))))"
-      proof-
-    fix M :: "('a\<times>'b\<times>'c) set" and f :: "('a\<times>'b\<times>'c) \<Rightarrow> 'd list" and g
-    assume "\<forall> (a,b,c) \<in> M. f (a,b,c) = g a b c"
-    thus "(UNION M (\<lambda> (a,b,c). set (f (a,b,c)))) = (UNION M (\<lambda> (a,b,c). set (g a b c)))"
-      by (auto simp add: split_def)
-  qed
-  have "?SS (Floor a) = UNION (?SS a) (\<lambda>x. set (?ff x))" 
-    by (auto simp add: foldl_conv_concat)
-  also have "\<dots> = UNION (?SS a) (\<lambda> (p,n,s). set (?ff (p,n,s)))" by auto
-  also have "\<dots> = 
-    ((UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n=0} (\<lambda> (p,n,s). set (?ff (p,n,s)))) Un 
-    (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n>0} (\<lambda> (p,n,s). set (?ff (p,n,s)))) Un 
-    (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n<0} (\<lambda> (p,n,s). set (?ff (p,n,s)))))" 
-    using int_cases[rule_format] by blast
-  also have "\<dots> =  
-    ((UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n=0} (\<lambda> (p,n,s). set [(p,0,Floor s)])) Un 
-   (UNION {(p,n,s). (p,n,s)\<in> ?SS a\<and>n>0} (\<lambda>(p,n,s). set(map(?f(p,n,s)) (iupt(0,n))))) Un 
-   (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n<0} (\<lambda> (p,n,s). 
-    set (map (?f(p,n,s)) (iupt(n,0))))))" by (simp only: U1 U2 U3)
-  also have "\<dots> =  
-    ((UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n=0} (\<lambda> (p,n,s). {(p,0,Floor s)})) Un 
-    (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n>0} (\<lambda> (p,n,s). (?f(p,n,s)) ` {0 .. n})) Un 
-    (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n<0} (\<lambda> (p,n,s). (?f(p,n,s)) ` {n .. 0})))"
-    by (simp only: set_map iupt_set set.simps)
-  also have "\<dots> =   
-    ((UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n=0} (\<lambda> (p,n,s). {(p,0,Floor s)})) Un 
-    (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n>0} (\<lambda> (p,n,s). {?f(p,n,s) j| j. j\<in> {0 .. n}})) Un 
-    (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n<0} (\<lambda> (p,n,s).  {?f(p,n,s) j| j. j\<in> {n .. 0}})))" by blast
-  finally 
-  have FS: "?SS (Floor a) =   
-    ((UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n=0} (\<lambda> (p,n,s). {(p,0,Floor s)})) Un 
-    (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n>0} (\<lambda> (p,n,s). {?f(p,n,s) j| j. j\<in> {0 .. n}})) Un 
-    (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n<0} (\<lambda> (p,n,s).  {?f(p,n,s) j| j. j\<in> {n .. 0}})))"    by blast
-  show ?case
-    proof(simp only: FS, clarsimp simp del: Ifm.simps Inum.simps, -)
-      fix p n s
-      let ?ths = "(?I p \<longrightarrow> (?N (Floor a) = ?N (CN 0 n s))) \<and> numbound0 s \<and> isrlfm p"
-      assume "(\<exists>ba. (p, 0, ba) \<in> set (rsplit0 a) \<and> n = 0 \<and> s = Floor ba) \<or>
-       (\<exists>ab ac ba.
-           (ab, ac, ba) \<in> set (rsplit0 a) \<and>
-           0 < ac \<and>
-           (\<exists>j. p = fp ab ac ba j \<and>
-                n = 0 \<and> s = Add (Floor ba) (C j) \<and> 0 \<le> j \<and> j \<le> ac)) \<or>
-       (\<exists>ab ac ba.
-           (ab, ac, ba) \<in> set (rsplit0 a) \<and>
-           ac < 0 \<and>
-           (\<exists>j. p = fp ab ac ba j \<and>
-                n = 0 \<and> s = Add (Floor ba) (C j) \<and> ac \<le> j \<and> j \<le> 0))"
-      moreover 
-      {fix s'
-	assume "(p, 0, s') \<in> ?SS a" and "n = 0" and "s = Floor s'"
-	hence ?ths using prems by auto}
-      moreover
-      {	fix p' n' s' j
-	assume pns: "(p', n', s') \<in> ?SS a" 
-	  and np: "0 < n'" 
-	  and p_def: "p = ?p (p',n',s') j" 
-	  and n0: "n = 0" 
-	  and s_def: "s = (Add (Floor s') (C j))" 
-	  and jp: "0 \<le> j" and jn: "j \<le> n'"
-	from prems pns have H:"(Ifm ((x\<Colon>real) # (bs\<Colon>real list)) p' \<longrightarrow>
-          Inum (x # bs) a = Inum (x # bs) (CN 0 n' s')) \<and>
-          numbound0 s' \<and> isrlfm p'" by blast
-	hence nb: "numbound0 s'" by simp
-	from H have nf: "isrlfm (?p (p',n',s') j)" using fp_def np by (simp add: numsub_nb)
-	let ?nxs = "CN 0 n' s'"
-	let ?l = "floor (?N s') + j"
-	from H 
-	have "?I (?p (p',n',s') j) \<longrightarrow> 
-	  (((?N ?nxs \<ge> real ?l) \<and> (?N ?nxs < real (?l + 1))) \<and> (?N a = ?N ?nxs ))" 
-	  by (simp add: fp_def np algebra_simps numsub numadd numfloor)
-	also have "\<dots> \<longrightarrow> ((floor (?N ?nxs) = ?l) \<and> (?N a = ?N ?nxs ))"
-	  using floor_int_eq[where x="?N ?nxs" and n="?l"] by simp
-	moreover
-	have "\<dots> \<longrightarrow> (?N (Floor a) = ?N ((Add (Floor s') (C j))))" by simp
-	ultimately have "?I (?p (p',n',s') j) \<longrightarrow> (?N (Floor a) = ?N ((Add (Floor s') (C j))))"
-	  by blast
-	with s_def n0 p_def nb nf have ?ths by auto}
-      moreover
-      {fix p' n' s' j
-	assume pns: "(p', n', s') \<in> ?SS a" 
-	  and np: "n' < 0" 
-	  and p_def: "p = ?p (p',n',s') j" 
-	  and n0: "n = 0" 
-	  and s_def: "s = (Add (Floor s') (C j))" 
-	  and jp: "n' \<le> j" and jn: "j \<le> 0"
-	from prems pns have H:"(Ifm ((x\<Colon>real) # (bs\<Colon>real list)) p' \<longrightarrow>
-          Inum (x # bs) a = Inum (x # bs) (CN 0 n' s')) \<and>
-          numbound0 s' \<and> isrlfm p'" by blast
-	hence nb: "numbound0 s'" by simp
-	from H have nf: "isrlfm (?p (p',n',s') j)" using fp_def np by (simp add: numneg_nb)
-	let ?nxs = "CN 0 n' s'"
-	let ?l = "floor (?N s') + j"
-	from H 
-	have "?I (?p (p',n',s') j) \<longrightarrow> 
-	  (((?N ?nxs \<ge> real ?l) \<and> (?N ?nxs < real (?l + 1))) \<and> (?N a = ?N ?nxs ))" 
-	  by (simp add: np fp_def algebra_simps numneg numfloor numadd numsub)
-	also have "\<dots> \<longrightarrow> ((floor (?N ?nxs) = ?l) \<and> (?N a = ?N ?nxs ))"
-	  using floor_int_eq[where x="?N ?nxs" and n="?l"] by simp
-	moreover
-	have "\<dots> \<longrightarrow> (?N (Floor a) = ?N ((Add (Floor s') (C j))))"  by simp
-	ultimately have "?I (?p (p',n',s') j) \<longrightarrow> (?N (Floor a) = ?N ((Add (Floor s') (C j))))"
-	  by blast
-	with s_def n0 p_def nb nf have ?ths by auto}
-      ultimately show ?ths by auto
-    qed
-next
-  case (3 a b) then show ?case
-  apply auto
-  apply (erule_tac x = "(aa, aaa, ba)" in ballE) apply simp_all
-  apply (erule_tac x = "(ab, ac, baa)" in ballE) apply simp_all
-  done
-qed (auto simp add: Let_def split_def algebra_simps conj_rl)
-
-lemma real_in_int_intervals: 
-  assumes xb: "real m \<le> x \<and> x < real ((n::int) + 1)"
-  shows "\<exists> j\<in> {m.. n}. real j \<le> x \<and> x < real (j+1)" (is "\<exists> j\<in> ?N. ?P j")
-by (rule bexI[where P="?P" and x="floor x" and A="?N"]) 
-(auto simp add: floor_less_eq[where x="x" and a="n+1", simplified] xb[simplified] floor_mono2[where x="real m" and y="x", OF conjunct1[OF xb], simplified floor_real_of_int[where n="m"]])
-
-lemma rsplit0_complete:
-  assumes xp:"0 \<le> x" and x1:"x < 1"
-  shows "\<exists> (p,n,s) \<in> set (rsplit0 t). Ifm (x#bs) p" (is "\<exists> (p,n,s) \<in> ?SS t. ?I p")
-proof(induct t rule: rsplit0.induct)
-  case (2 a b) 
-  from prems have "\<exists> (pa,na,sa) \<in> ?SS a. ?I pa" by auto
-  then obtain "pa" "na" "sa" where pa: "(pa,na,sa)\<in> ?SS a \<and> ?I pa" by blast
-  from prems have "\<exists> (pb,nb,sb) \<in> ?SS b. ?I pb" by auto
-  then obtain "pb" "nb" "sb" where pb: "(pb,nb,sb)\<in> ?SS b \<and> ?I pb" by blast
-  from pa pb have th: "((pa,na,sa),(pb,nb,sb)) \<in> set[(x,y). x\<leftarrow>rsplit0 a, y\<leftarrow>rsplit0 b]"
-    by (auto)
-  let ?f="(\<lambda> ((p,n,t),(q,m,s)). (And p q, n+m, Add t s))"
-  from imageI[OF th, where f="?f"] have "?f ((pa,na,sa),(pb,nb,sb)) \<in> ?SS (Add a b)"
-    by (simp add: Let_def)
-  hence "(And pa pb, na +nb, Add sa sb) \<in> ?SS (Add a b)" by simp
-  moreover from pa pb have "?I (And pa pb)" by simp
-  ultimately show ?case by blast
-next
-  case (5 a) 
-  let ?p = "\<lambda> (p,n,s) j. fp p n s j"
-  let ?f = "(\<lambda> (p,n,s) j. (?p (p,n,s) j, (0::int),(Add (Floor s) (C j))))"
-  let ?J = "\<lambda> n. if n>0 then iupt (0,n) else iupt (n,0)"
-  let ?ff=" (\<lambda> (p,n,s). if n= 0 then [(p,0,Floor s)] else map (?f (p,n,s)) (?J n))"
-  have int_cases: "\<forall> (i::int). i= 0 \<or> i < 0 \<or> i > 0" by arith
-  have U1: "(UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n=0} (\<lambda> (p,n,s). set (?ff (p,n,s)))) = (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n=0} (\<lambda> (p,n,s). set [(p,0,Floor s)]))" by auto
-  have U2': "\<forall> (p,n,s) \<in> {(p,n,s). (p,n,s) \<in> ?SS a \<and> n>0}. ?ff (p,n,s) = map (?f(p,n,s)) (iupt(0,n))"
-    by auto
-  hence U2: "(UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n>0} (\<lambda> (p,n,s). set (?ff (p,n,s)))) = (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n>0} (\<lambda> (p,n,s). set (map (?f(p,n,s)) (iupt(0,n)))))"
-  proof-
-    fix M :: "('a\<times>'b\<times>'c) set" and f :: "('a\<times>'b\<times>'c) \<Rightarrow> 'd list" and g
-    assume "\<forall> (a,b,c) \<in> M. f (a,b,c) = g a b c"
-    thus "(UNION M (\<lambda> (a,b,c). set (f (a,b,c)))) = (UNION M (\<lambda> (a,b,c). set (g a b c)))"
-      by (auto simp add: split_def)
-  qed
-  have U3': "\<forall> (p,n,s) \<in> {(p,n,s). (p,n,s) \<in> ?SS a \<and> n<0}. ?ff (p,n,s) = map (?f(p,n,s)) (iupt(n,0))"
-    by auto
-  hence U3: "(UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n<0} (\<lambda> (p,n,s). set (?ff (p,n,s)))) = (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n<0} (\<lambda> (p,n,s). set (map (?f(p,n,s)) (iupt(n,0)))))"
-  proof-
-    fix M :: "('a\<times>'b\<times>'c) set" and f :: "('a\<times>'b\<times>'c) \<Rightarrow> 'd list" and g
-    assume "\<forall> (a,b,c) \<in> M. f (a,b,c) = g a b c"
-    thus "(UNION M (\<lambda> (a,b,c). set (f (a,b,c)))) = (UNION M (\<lambda> (a,b,c). set (g a b c)))"
-      by (auto simp add: split_def)
-  qed
-
-  have "?SS (Floor a) = UNION (?SS a) (\<lambda>x. set (?ff x))" by (auto simp add: foldl_conv_concat) 
-  also have "\<dots> = UNION (?SS a) (\<lambda> (p,n,s). set (?ff (p,n,s)))" by auto
-  also have "\<dots> = 
-    ((UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n=0} (\<lambda> (p,n,s). set (?ff (p,n,s)))) Un 
-    (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n>0} (\<lambda> (p,n,s). set (?ff (p,n,s)))) Un 
-    (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n<0} (\<lambda> (p,n,s). set (?ff (p,n,s)))))" 
-    using int_cases[rule_format] by blast
-  also have "\<dots> =  
-    ((UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n=0} (\<lambda> (p,n,s). set [(p,0,Floor s)])) Un 
-    (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n>0} (\<lambda> (p,n,s). set (map (?f(p,n,s)) (iupt(0,n))))) Un 
-    (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n<0} (\<lambda> (p,n,s). set (map (?f(p,n,s)) (iupt(n,0))))))" by (simp only: U1 U2 U3)
-  also have "\<dots> =  
-    ((UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n=0} (\<lambda> (p,n,s). {(p,0,Floor s)})) Un 
-    (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n>0} (\<lambda> (p,n,s). (?f(p,n,s)) ` {0 .. n})) Un 
-    (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n<0} (\<lambda> (p,n,s). (?f(p,n,s)) ` {n .. 0})))"
-    by (simp only: set_map iupt_set set.simps)
-  also have "\<dots> =   
-    ((UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n=0} (\<lambda> (p,n,s). {(p,0,Floor s)})) Un 
-    (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n>0} (\<lambda> (p,n,s). {?f(p,n,s) j| j. j\<in> {0 .. n}})) Un 
-    (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n<0} (\<lambda> (p,n,s).  {?f(p,n,s) j| j. j\<in> {n .. 0}})))" by blast
-  finally 
-  have FS: "?SS (Floor a) =   
-    ((UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n=0} (\<lambda> (p,n,s). {(p,0,Floor s)})) Un 
-    (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n>0} (\<lambda> (p,n,s). {?f(p,n,s) j| j. j\<in> {0 .. n}})) Un 
-    (UNION {(p,n,s). (p,n,s) \<in> ?SS a \<and> n<0} (\<lambda> (p,n,s).  {?f(p,n,s) j| j. j\<in> {n .. 0}})))"    by blast
-  from prems have "\<exists> (p,n,s) \<in> ?SS a. ?I p" by auto
-  then obtain "p" "n" "s" where pns: "(p,n,s) \<in> ?SS a \<and> ?I p" by blast
-  let ?N = "\<lambda> t. Inum (x#bs) t"
-  from rsplit0_cs[rule_format] pns have ans:"(?N a = ?N (CN 0 n s)) \<and> numbound0 s \<and> isrlfm p"
-    by auto
-  
-  have "n=0 \<or> n >0 \<or> n <0" by arith
-  moreover {assume "n=0" hence ?case using pns by (simp only: FS) auto }
-  moreover
-  {
-    assume np: "n > 0"
-    from real_of_int_floor_le[where r="?N s"] have "?N (Floor s) \<le> ?N s" by simp
-    also from mult_left_mono[OF xp] np have "?N s \<le> real n * x + ?N s" by simp
-    finally have "?N (Floor s) \<le> real n * x + ?N s" .
-    moreover
-    {from mult_strict_left_mono[OF x1] np 
-      have "real n *x + ?N s < real n + ?N s" by simp
-      also from real_of_int_floor_add_one_gt[where r="?N s"] 
-      have "\<dots> < real n + ?N (Floor s) + 1" by simp
-      finally have "real n *x + ?N s < ?N (Floor s) + real (n+1)" by simp}
-    ultimately have "?N (Floor s) \<le> real n *x + ?N s\<and> real n *x + ?N s < ?N (Floor s) + real (n+1)" by simp
-    hence th: "0 \<le> real n *x + ?N s - ?N (Floor s) \<and> real n *x + ?N s - ?N (Floor s) < real (n+1)" by simp
-    from real_in_int_intervals th have  "\<exists> j\<in> {0 .. n}. real j \<le> real n *x + ?N s - ?N (Floor s)\<and> real n *x + ?N s - ?N (Floor s) < real (j+1)" by simp
-    
-    hence "\<exists> j\<in> {0 .. n}. 0 \<le> real n *x + ?N s - ?N (Floor s) - real j \<and> real n *x + ?N s - ?N (Floor s) - real (j+1) < 0"
-      by(simp only: myl[rule_format, where b="real n * x + Inum (x # bs) s - Inum (x # bs) (Floor s)"] less_iff_diff_less_0[where a="real n *x + ?N s - ?N (Floor s)"]) 
-    hence "\<exists> j\<in> {0.. n}. ?I (?p (p,n,s) j)"
-      using pns by (simp add: fp_def np algebra_simps numsub numadd)
-    then obtain "j" where j_def: "j\<in> {0 .. n} \<and> ?I (?p (p,n,s) j)" by blast
-    hence "\<exists>x \<in> {?p (p,n,s) j |j. 0\<le> j \<and> j \<le> n }. ?I x" by auto
-    hence ?case using pns 
-      by (simp only: FS,simp add: bex_Un) 
-    (rule disjI2, rule disjI1,rule exI [where x="p"],
-      rule exI [where x="n"],rule exI [where x="s"],simp_all add: np)
-  }
-  moreover
-  { assume nn: "n < 0" hence np: "-n >0" by simp
-    from real_of_int_floor_le[where r="?N s"] have "?N (Floor s) + 1 > ?N s" by simp
-    moreover from mult_left_mono_neg[OF xp] nn have "?N s \<ge> real n * x + ?N s" by simp
-    ultimately have "?N (Floor s) + 1 > real n * x + ?N s" by arith 
-    moreover
-    {from mult_strict_left_mono_neg[OF x1, where c="real n"] nn
-      have "real n *x + ?N s \<ge> real n + ?N s" by simp 
-      moreover from real_of_int_floor_le[where r="?N s"]  have "real n + ?N s \<ge> real n + ?N (Floor s)" by simp
-      ultimately have "real n *x + ?N s \<ge> ?N (Floor s) + real n" 
-	by (simp only: algebra_simps)}
-    ultimately have "?N (Floor s) + real n \<le> real n *x + ?N s\<and> real n *x + ?N s < ?N (Floor s) + real (1::int)" by simp
-    hence th: "real n \<le> real n *x + ?N s - ?N (Floor s) \<and> real n *x + ?N s - ?N (Floor s) < real (1::int)" by simp
-    have th1: "\<forall> (a::real). (- a > 0) = (a < 0)" by auto
-    have th2: "\<forall> (a::real). (0 \<ge> - a) = (a \<ge> 0)" by auto
-    from real_in_int_intervals th  have  "\<exists> j\<in> {n .. 0}. real j \<le> real n *x + ?N s - ?N (Floor s)\<and> real n *x + ?N s - ?N (Floor s) < real (j+1)" by simp
-    
-    hence "\<exists> j\<in> {n .. 0}. 0 \<le> real n *x + ?N s - ?N (Floor s) - real j \<and> real n *x + ?N s - ?N (Floor s) - real (j+1) < 0"
-      by(simp only: myl[rule_format, where b="real n * x + Inum (x # bs) s - Inum (x # bs) (Floor s)"] less_iff_diff_less_0[where a="real n *x + ?N s - ?N (Floor s)"]) 
-    hence "\<exists> j\<in> {n .. 0}. 0 \<ge> - (real n *x + ?N s - ?N (Floor s) - real j) \<and> - (real n *x + ?N s - ?N (Floor s) - real (j+1)) > 0" by (simp only: th1[rule_format] th2[rule_format])
-    hence "\<exists> j\<in> {n.. 0}. ?I (?p (p,n,s) j)"
-      using pns by (simp add: fp_def nn diff_def add_ac mult_ac numfloor numadd numneg
-	del: diff_less_0_iff_less diff_le_0_iff_le) 
-    then obtain "j" where j_def: "j\<in> {n .. 0} \<and> ?I (?p (p,n,s) j)" by blast
-    hence "\<exists>x \<in> {?p (p,n,s) j |j. n\<le> j \<and> j \<le> 0 }. ?I x" by auto
-    hence ?case using pns 
-      by (simp only: FS,simp add: bex_Un)
-    (rule disjI2, rule disjI2,rule exI [where x="p"],
-      rule exI [where x="n"],rule exI [where x="s"],simp_all add: nn)
-  }
-  ultimately show ?case by blast
-qed (auto simp add: Let_def split_def)
-
-    (* Linearize a formula where Bound 0 ranges over [0,1) *)
-
-constdefs rsplit :: "(int \<Rightarrow> num \<Rightarrow> fm) \<Rightarrow> num \<Rightarrow> fm"
-  "rsplit f a \<equiv> foldr disj (map (\<lambda> (\<phi>, n, s). conj \<phi> (f n s)) (rsplit0 a)) F"
-
-lemma foldr_disj_map: "Ifm bs (foldr disj (map f xs) F) = (\<exists> x \<in> set xs. Ifm bs (f x))"
-by(induct xs, simp_all)
-
-lemma foldr_conj_map: "Ifm bs (foldr conj (map f xs) T) = (\<forall> x \<in> set xs. Ifm bs (f x))"
-by(induct xs, simp_all)
-
-lemma foldr_disj_map_rlfm: 
-  assumes lf: "\<forall> n s. numbound0 s \<longrightarrow> isrlfm (f n s)"
-  and \<phi>: "\<forall> (\<phi>,n,s) \<in> set xs. numbound0 s \<and> isrlfm \<phi>"
-  shows "isrlfm (foldr disj (map (\<lambda> (\<phi>, n, s). conj \<phi> (f n s)) xs) F)"
-using lf \<phi> by (induct xs, auto)
-
-lemma rsplit_ex: "Ifm bs (rsplit f a) = (\<exists> (\<phi>,n,s) \<in> set (rsplit0 a). Ifm bs (conj \<phi> (f n s)))"
-using foldr_disj_map[where xs="rsplit0 a"] rsplit_def by (simp add: split_def)
-
-lemma rsplit_l: assumes lf: "\<forall> n s. numbound0 s \<longrightarrow> isrlfm (f n s)"
-  shows "isrlfm (rsplit f a)"
-proof-
-  from rsplit0_cs[where t="a"] have th: "\<forall> (\<phi>,n,s) \<in> set (rsplit0 a). numbound0 s \<and> isrlfm \<phi>" by blast
-  from foldr_disj_map_rlfm[OF lf th] rsplit_def show ?thesis by simp
-qed
-
-lemma rsplit: 
-  assumes xp: "x \<ge> 0" and x1: "x < 1"
-  and f: "\<forall> a n s. Inum (x#bs) a = Inum (x#bs) (CN 0 n s) \<and> numbound0 s \<longrightarrow> (Ifm (x#bs) (f n s) = Ifm (x#bs) (g a))"
-  shows "Ifm (x#bs) (rsplit f a) = Ifm (x#bs) (g a)"
-proof(auto)
-  let ?I = "\<lambda>x p. Ifm (x#bs) p"
-  let ?N = "\<lambda> x t. Inum (x#bs) t"
-  assume "?I x (rsplit f a)"
-  hence "\<exists> (\<phi>,n,s) \<in> set (rsplit0 a). ?I x (And \<phi> (f n s))" using rsplit_ex by simp
-  then obtain "\<phi>" "n" "s" where fnsS:"(\<phi>,n,s) \<in> set (rsplit0 a)" and "?I x (And \<phi> (f n s))" by blast
-  hence \<phi>: "?I x \<phi>" and fns: "?I x (f n s)" by auto
-  from rsplit0_cs[where t="a" and bs="bs" and x="x", rule_format, OF fnsS] \<phi> 
-  have th: "(?N x a = ?N x (CN 0 n s)) \<and> numbound0 s" by auto
-  from f[rule_format, OF th] fns show "?I x (g a)" by simp
-next
-  let ?I = "\<lambda>x p. Ifm (x#bs) p"
-  let ?N = "\<lambda> x t. Inum (x#bs) t"
-  assume ga: "?I x (g a)"
-  from rsplit0_complete[OF xp x1, where bs="bs" and t="a"] 
-  obtain "\<phi>" "n" "s" where fnsS:"(\<phi>,n,s) \<in> set (rsplit0 a)" and fx: "?I x \<phi>" by blast
-  from rsplit0_cs[where t="a" and x="x" and bs="bs"] fnsS fx
-  have ans: "?N x a = ?N x (CN 0 n s)" and nb: "numbound0 s" by auto
-  with ga f have "?I x (f n s)" by auto
-  with rsplit_ex fnsS fx show "?I x (rsplit f a)" by auto
-qed
-
-definition lt :: "int \<Rightarrow> num \<Rightarrow> fm" where
-  lt_def: "lt c t = (if c = 0 then (Lt t) else if c > 0 then (Lt (CN 0 c t)) 
-                        else (Gt (CN 0 (-c) (Neg t))))"
-
-definition  le :: "int \<Rightarrow> num \<Rightarrow> fm" where
-  le_def: "le c t = (if c = 0 then (Le t) else if c > 0 then (Le (CN 0 c t)) 
-                        else (Ge (CN 0 (-c) (Neg t))))"
-
-definition  gt :: "int \<Rightarrow> num \<Rightarrow> fm" where
-  gt_def: "gt c t = (if c = 0 then (Gt t) else if c > 0 then (Gt (CN 0 c t)) 
-                        else (Lt (CN 0 (-c) (Neg t))))"
-
-definition  ge :: "int \<Rightarrow> num \<Rightarrow> fm" where
-  ge_def: "ge c t = (if c = 0 then (Ge t) else if c > 0 then (Ge (CN 0 c t)) 
-                        else (Le (CN 0 (-c) (Neg t))))"
-
-definition  eq :: "int \<Rightarrow> num \<Rightarrow> fm" where
-  eq_def: "eq c t = (if c = 0 then (Eq t) else if c > 0 then (Eq (CN 0 c t)) 
-                        else (Eq (CN 0 (-c) (Neg t))))"
-
-definition neq :: "int \<Rightarrow> num \<Rightarrow> fm" where
-  neq_def: "neq c t = (if c = 0 then (NEq t) else if c > 0 then (NEq (CN 0 c t)) 
-                        else (NEq (CN 0 (-c) (Neg t))))"
-
-lemma lt_mono: "\<forall> a n s. Inum (x#bs) a = Inum (x#bs) (CN 0 n s) \<and> numbound0 s \<longrightarrow> Ifm (x#bs) (lt n s) = Ifm (x#bs) (Lt a)"
-  (is "\<forall> a n s . ?N a = ?N (CN 0 n s) \<and> _\<longrightarrow> ?I (lt n s) = ?I (Lt a)")
-proof(clarify)
-  fix a n s
-  assume H: "?N a = ?N (CN 0 n s)"
-  show "?I (lt n s) = ?I (Lt a)" using H by (cases "n=0", (simp add: lt_def))
-  (cases "n > 0", simp_all add: lt_def algebra_simps myless[rule_format, where b="0"])
-qed
-
-lemma lt_l: "isrlfm (rsplit lt a)"
-  by (rule rsplit_l[where f="lt" and a="a"], auto simp add: lt_def,
-    case_tac s, simp_all, case_tac "nat", simp_all)
-
-lemma le_mono: "\<forall> a n s. Inum (x#bs) a = Inum (x#bs) (CN 0 n s) \<and> numbound0 s \<longrightarrow> Ifm (x#bs) (le n s) = Ifm (x#bs) (Le a)" (is "\<forall> a n s. ?N a = ?N (CN 0 n s) \<and> _ \<longrightarrow> ?I (le n s) = ?I (Le a)")
-proof(clarify)
-  fix a n s
-  assume H: "?N a = ?N (CN 0 n s)"
-  show "?I (le n s) = ?I (Le a)" using H by (cases "n=0", (simp add: le_def))
-  (cases "n > 0", simp_all add: le_def algebra_simps myl[rule_format, where b="0"])
-qed
-
-lemma le_l: "isrlfm (rsplit le a)"
-  by (rule rsplit_l[where f="le" and a="a"], auto simp add: le_def) 
-(case_tac s, simp_all, case_tac "nat",simp_all)
-
-lemma gt_mono: "\<forall> a n s. Inum (x#bs) a = Inum (x#bs) (CN 0 n s) \<and> numbound0 s \<longrightarrow> Ifm (x#bs) (gt n s) = Ifm (x#bs) (Gt a)" (is "\<forall> a n s. ?N a = ?N (CN 0 n s) \<and> _ \<longrightarrow> ?I (gt n s) = ?I (Gt a)")
-proof(clarify)
-  fix a n s
-  assume H: "?N a = ?N (CN 0 n s)"
-  show "?I (gt n s) = ?I (Gt a)" using H by (cases "n=0", (simp add: gt_def))
-  (cases "n > 0", simp_all add: gt_def algebra_simps myless[rule_format, where b="0"])
-qed
-lemma gt_l: "isrlfm (rsplit gt a)"
-  by (rule rsplit_l[where f="gt" and a="a"], auto simp add: gt_def) 
-(case_tac s, simp_all, case_tac "nat", simp_all)
-
-lemma ge_mono: "\<forall> a n s. Inum (x#bs) a = Inum (x#bs) (CN 0 n s) \<and> numbound0 s \<longrightarrow> Ifm (x#bs) (ge n s) = Ifm (x#bs) (Ge a)" (is "\<forall> a n s . ?N a = ?N (CN 0 n s) \<and> _ \<longrightarrow> ?I (ge n s) = ?I (Ge a)")
-proof(clarify)
-  fix a n s 
-  assume H: "?N a = ?N (CN 0 n s)"
-  show "?I (ge n s) = ?I (Ge a)" using H by (cases "n=0", (simp add: ge_def))
-  (cases "n > 0", simp_all add: ge_def algebra_simps myl[rule_format, where b="0"])
-qed
-lemma ge_l: "isrlfm (rsplit ge a)"
-  by (rule rsplit_l[where f="ge" and a="a"], auto simp add: ge_def) 
-(case_tac s, simp_all, case_tac "nat", simp_all)
-
-lemma eq_mono: "\<forall> a n s. Inum (x#bs) a = Inum (x#bs) (CN 0 n s) \<and> numbound0 s \<longrightarrow> Ifm (x#bs) (eq n s) = Ifm (x#bs) (Eq a)" (is "\<forall> a n s. ?N a = ?N (CN 0 n s) \<and> _ \<longrightarrow> ?I (eq n s) = ?I (Eq a)")
-proof(clarify)
-  fix a n s 
-  assume H: "?N a = ?N (CN 0 n s)"
-  show "?I (eq n s) = ?I (Eq a)" using H by (auto simp add: eq_def algebra_simps)
-qed
-lemma eq_l: "isrlfm (rsplit eq a)"
-  by (rule rsplit_l[where f="eq" and a="a"], auto simp add: eq_def) 
-(case_tac s, simp_all, case_tac"nat", simp_all)
-
-lemma neq_mono: "\<forall> a n s. Inum (x#bs) a = Inum (x#bs) (CN 0 n s) \<and> numbound0 s \<longrightarrow> Ifm (x#bs) (neq n s) = Ifm (x#bs) (NEq a)" (is "\<forall> a n s. ?N a = ?N (CN 0 n s) \<and> _ \<longrightarrow> ?I (neq n s) = ?I (NEq a)")
-proof(clarify)
-  fix a n s bs
-  assume H: "?N a = ?N (CN 0 n s)"
-  show "?I (neq n s) = ?I (NEq a)" using H by (auto simp add: neq_def algebra_simps)
-qed
-
-lemma neq_l: "isrlfm (rsplit neq a)"
-  by (rule rsplit_l[where f="neq" and a="a"], auto simp add: neq_def) 
-(case_tac s, simp_all, case_tac"nat", simp_all)
-
-lemma small_le: 
-  assumes u0:"0 \<le> u" and u1: "u < 1"
-  shows "(-u \<le> real (n::int)) = (0 \<le> n)"
-using u0 u1  by auto
-
-lemma small_lt: 
-  assumes u0:"0 \<le> u" and u1: "u < 1"
-  shows "(real (n::int) < real (m::int) - u) = (n < m)"
-using u0 u1  by auto
-
-lemma rdvd01_cs: 
-  assumes up: "u \<ge> 0" and u1: "u<1" and np: "real n > 0"
-  shows "(real (i::int) rdvd real (n::int) * u - s) = (\<exists> j\<in> {0 .. n - 1}. real n * u = s - real (floor s) + real j \<and> real i rdvd real (j - floor s))" (is "?lhs = ?rhs")
-proof-
-  let ?ss = "s - real (floor s)"
-  from real_of_int_floor_add_one_gt[where r="s", simplified myless[rule_format,where a="s"]] 
-    real_of_int_floor_le[where r="s"]  have ss0:"?ss \<ge> 0" and ss1:"?ss < 1" 
-    by (auto simp add: myl[rule_format, where b="s", symmetric] myless[rule_format, where a="?ss"])
-  from np have n0: "real n \<ge> 0" by simp
-  from mult_left_mono[OF up n0] mult_strict_left_mono[OF u1 np] 
-  have nu0:"real n * u - s \<ge> -s" and nun:"real n * u -s < real n - s" by auto  
-  from int_rdvd_real[where i="i" and x="real (n::int) * u - s"] 
-  have "real i rdvd real n * u - s = 
-    (i dvd floor (real n * u -s) \<and> (real (floor (real n * u - s)) = real n * u - s ))" 
-    (is "_ = (?DE)" is "_ = (?D \<and> ?E)") by simp
-  also have "\<dots> = (?DE \<and> real(floor (real n * u - s) + floor s)\<ge> -?ss 
-    \<and> real(floor (real n * u - s) + floor s)< real n - ?ss)" (is "_=(?DE \<and>real ?a \<ge> _ \<and> real ?a < _)")
-    using nu0 nun  by auto
-  also have "\<dots> = (?DE \<and> ?a \<ge> 0 \<and> ?a < n)" by(simp only: small_le[OF ss0 ss1] small_lt[OF ss0 ss1])
-  also have "\<dots> = (?DE \<and> (\<exists> j\<in> {0 .. (n - 1)}. ?a = j))" by simp
-  also have "\<dots> = (?DE \<and> (\<exists> j\<in> {0 .. (n - 1)}. real (\<lfloor>real n * u - s\<rfloor>) = real j - real \<lfloor>s\<rfloor> ))"
-    by (simp only: algebra_simps real_of_int_diff[symmetric] real_of_int_inject del: real_of_int_diff)
-  also have "\<dots> = ((\<exists> j\<in> {0 .. (n - 1)}. real n * u - s = real j - real \<lfloor>s\<rfloor> \<and> real i rdvd real n * u - s))" using int_rdvd_iff[where i="i" and t="\<lfloor>real n * u - s\<rfloor>"]
-    by (auto cong: conj_cong)
-  also have "\<dots> = ?rhs" by(simp cong: conj_cong) (simp add: algebra_simps )
-  finally show ?thesis .
-qed
-
-definition
-  DVDJ:: "int \<Rightarrow> int \<Rightarrow> num \<Rightarrow> fm"
-where
-  DVDJ_def: "DVDJ i n s = (foldr disj (map (\<lambda> j. conj (Eq (CN 0 n (Add s (Sub (Floor (Neg s)) (C j))))) (Dvd i (Sub (C j) (Floor (Neg s))))) (iupt(0,n - 1))) F)"
-
-definition
-  NDVDJ:: "int \<Rightarrow> int \<Rightarrow> num \<Rightarrow> fm"
-where
-  NDVDJ_def: "NDVDJ i n s = (foldr conj (map (\<lambda> j. disj (NEq (CN 0 n (Add s (Sub (Floor (Neg s)) (C j))))) (NDvd i (Sub (C j) (Floor (Neg s))))) (iupt(0,n - 1))) T)"
-
-lemma DVDJ_DVD: 
-  assumes xp:"x\<ge> 0" and x1: "x < 1" and np:"real n > 0"
-  shows "Ifm (x#bs) (DVDJ i n s) = Ifm (x#bs) (Dvd i (CN 0 n s))"
-proof-
-  let ?f = "\<lambda> j. conj (Eq(CN 0 n (Add s (Sub(Floor (Neg s)) (C j))))) (Dvd i (Sub (C j) (Floor (Neg s))))"
-  let ?s= "Inum (x#bs) s"
-  from foldr_disj_map[where xs="iupt(0,n - 1)" and bs="x#bs" and f="?f"]
-  have "Ifm (x#bs) (DVDJ i n s) = (\<exists> j\<in> {0 .. (n - 1)}. Ifm (x#bs) (?f j))" 
-    by (simp add: iupt_set np DVDJ_def del: iupt.simps)
-  also have "\<dots> = (\<exists> j\<in> {0 .. (n - 1)}. real n * x = (- ?s) - real (floor (- ?s)) + real j \<and> real i rdvd real (j - floor (- ?s)))" by (simp add: algebra_simps diff_def[symmetric])
-  also from rdvd01_cs[OF xp x1 np, where i="i" and s="-?s"] 
-  have "\<dots> = (real i rdvd real n * x - (-?s))" by simp
-  finally show ?thesis by simp
-qed
-
-lemma NDVDJ_NDVD: 
-  assumes xp:"x\<ge> 0" and x1: "x < 1" and np:"real n > 0"
-  shows "Ifm (x#bs) (NDVDJ i n s) = Ifm (x#bs) (NDvd i (CN 0 n s))"
-proof-
-  let ?f = "\<lambda> j. disj(NEq(CN 0 n (Add s (Sub (Floor (Neg s)) (C j))))) (NDvd i (Sub (C j) (Floor(Neg s))))"
-  let ?s= "Inum (x#bs) s"
-  from foldr_conj_map[where xs="iupt(0,n - 1)" and bs="x#bs" and f="?f"]
-  have "Ifm (x#bs) (NDVDJ i n s) = (\<forall> j\<in> {0 .. (n - 1)}. Ifm (x#bs) (?f j))" 
-    by (simp add: iupt_set np NDVDJ_def del: iupt.simps)
-  also have "\<dots> = (\<not> (\<exists> j\<in> {0 .. (n - 1)}. real n * x = (- ?s) - real (floor (- ?s)) + real j \<and> real i rdvd real (j - floor (- ?s))))" by (simp add: algebra_simps diff_def[symmetric])
-  also from rdvd01_cs[OF xp x1 np, where i="i" and s="-?s"] 
-  have "\<dots> = (\<not> (real i rdvd real n * x - (-?s)))" by simp
-  finally show ?thesis by simp
-qed  
-
-lemma foldr_disj_map_rlfm2: 
-  assumes lf: "\<forall> n . isrlfm (f n)"
-  shows "isrlfm (foldr disj (map f xs) F)"
-using lf by (induct xs, auto)
-lemma foldr_And_map_rlfm2: 
-  assumes lf: "\<forall> n . isrlfm (f n)"
-  shows "isrlfm (foldr conj (map f xs) T)"
-using lf by (induct xs, auto)
-
-lemma DVDJ_l: assumes ip: "i >0" and np: "n>0" and nb: "numbound0 s"
-  shows "isrlfm (DVDJ i n s)"
-proof-
-  let ?f="\<lambda>j. conj (Eq (CN 0 n (Add s (Sub (Floor (Neg s)) (C j)))))
-                         (Dvd i (Sub (C j) (Floor (Neg s))))"
-  have th: "\<forall> j. isrlfm (?f j)" using nb np by auto
-  from DVDJ_def foldr_disj_map_rlfm2[OF th] show ?thesis by simp 
-qed
-
-lemma NDVDJ_l: assumes ip: "i >0" and np: "n>0" and nb: "numbound0 s"
-  shows "isrlfm (NDVDJ i n s)"
-proof-
-  let ?f="\<lambda>j. disj (NEq (CN 0 n (Add s (Sub (Floor (Neg s)) (C j)))))
-                      (NDvd i (Sub (C j) (Floor (Neg s))))"
-  have th: "\<forall> j. isrlfm (?f j)" using nb np by auto
-  from NDVDJ_def foldr_And_map_rlfm2[OF th] show ?thesis by auto
-qed
-
-definition DVD :: "int \<Rightarrow> int \<Rightarrow> num \<Rightarrow> fm" where
-  DVD_def: "DVD i c t =
-  (if i=0 then eq c t else 
-  if c = 0 then (Dvd i t) else if c >0 then DVDJ (abs i) c t else DVDJ (abs i) (-c) (Neg t))"
-
-definition  NDVD :: "int \<Rightarrow> int \<Rightarrow> num \<Rightarrow> fm" where
-  "NDVD i c t =
-  (if i=0 then neq c t else 
-  if c = 0 then (NDvd i t) else if c >0 then NDVDJ (abs i) c t else NDVDJ (abs i) (-c) (Neg t))"
-
-lemma DVD_mono: 
-  assumes xp: "0\<le> x" and x1: "x < 1" 
-  shows "\<forall> a n s. Inum (x#bs) a = Inum (x#bs) (CN 0 n s) \<and> numbound0 s \<longrightarrow> Ifm (x#bs) (DVD i n s) = Ifm (x#bs) (Dvd i a)"
-  (is "\<forall> a n s. ?N a = ?N (CN 0 n s) \<and> _ \<longrightarrow> ?I (DVD i n s) = ?I (Dvd i a)")
-proof(clarify)
-  fix a n s 
-  assume H: "?N a = ?N (CN 0 n s)" and nb: "numbound0 s"
-  let ?th = "?I (DVD i n s) = ?I (Dvd i a)"
-  have "i=0 \<or> (i\<noteq>0 \<and> n=0) \<or> (i\<noteq>0 \<and> n < 0) \<or> (i\<noteq>0 \<and> n > 0)" by arith
-  moreover {assume iz: "i=0" hence ?th using eq_mono[rule_format, OF conjI[OF H nb]] 
-      by (simp add: DVD_def rdvd_left_0_eq)}
-  moreover {assume inz: "i\<noteq>0" and "n=0" hence ?th by (simp add: H DVD_def) } 
-  moreover {assume inz: "i\<noteq>0" and "n<0" hence ?th 
-      by (simp add: DVD_def H DVDJ_DVD[OF xp x1] rdvd_abs1 
-	rdvd_minus[where d="i" and t="real n * x + Inum (x # bs) s"]) } 
-  moreover {assume inz: "i\<noteq>0" and "n>0" hence ?th by (simp add:DVD_def H DVDJ_DVD[OF xp x1] rdvd_abs1)}
-  ultimately show ?th by blast
-qed
-
-lemma NDVD_mono:   assumes xp: "0\<le> x" and x1: "x < 1" 
-  shows "\<forall> a n s. Inum (x#bs) a = Inum (x#bs) (CN 0 n s) \<and> numbound0 s \<longrightarrow> Ifm (x#bs) (NDVD i n s) = Ifm (x#bs) (NDvd i a)"
-  (is "\<forall> a n s. ?N a = ?N (CN 0 n s) \<and> _ \<longrightarrow> ?I (NDVD i n s) = ?I (NDvd i a)")
-proof(clarify)
-  fix a n s 
-  assume H: "?N a = ?N (CN 0 n s)" and nb: "numbound0 s"
-  let ?th = "?I (NDVD i n s) = ?I (NDvd i a)"
-  have "i=0 \<or> (i\<noteq>0 \<and> n=0) \<or> (i\<noteq>0 \<and> n < 0) \<or> (i\<noteq>0 \<and> n > 0)" by arith
-  moreover {assume iz: "i=0" hence ?th using neq_mono[rule_format, OF conjI[OF H nb]] 
-      by (simp add: NDVD_def rdvd_left_0_eq)}
-  moreover {assume inz: "i\<noteq>0" and "n=0" hence ?th by (simp add: H NDVD_def) } 
-  moreover {assume inz: "i\<noteq>0" and "n<0" hence ?th 
-      by (simp add: NDVD_def H NDVDJ_NDVD[OF xp x1] rdvd_abs1 
-	rdvd_minus[where d="i" and t="real n * x + Inum (x # bs) s"]) } 
-  moreover {assume inz: "i\<noteq>0" and "n>0" hence ?th 
-      by (simp add:NDVD_def H NDVDJ_NDVD[OF xp x1] rdvd_abs1)}
-  ultimately show ?th by blast
-qed
-
-lemma DVD_l: "isrlfm (rsplit (DVD i) a)"
-  by (rule rsplit_l[where f="DVD i" and a="a"], auto simp add: DVD_def eq_def DVDJ_l) 
-(case_tac s, simp_all, case_tac "nat", simp_all)
-
-lemma NDVD_l: "isrlfm (rsplit (NDVD i) a)"
-  by (rule rsplit_l[where f="NDVD i" and a="a"], auto simp add: NDVD_def neq_def NDVDJ_l) 
-(case_tac s, simp_all, case_tac "nat", simp_all)
-
-consts rlfm :: "fm \<Rightarrow> fm"
-recdef rlfm "measure fmsize"
-  "rlfm (And p q) = conj (rlfm p) (rlfm q)"
-  "rlfm (Or p q) = disj (rlfm p) (rlfm q)"
-  "rlfm (Imp p q) = disj (rlfm (NOT p)) (rlfm q)"
-  "rlfm (Iff p q) = disj (conj(rlfm p) (rlfm q)) (conj(rlfm (NOT p)) (rlfm (NOT q)))"
-  "rlfm (Lt a) = rsplit lt a"
-  "rlfm (Le a) = rsplit le a"
-  "rlfm (Gt a) = rsplit gt a"
-  "rlfm (Ge a) = rsplit ge a"
-  "rlfm (Eq a) = rsplit eq a"
-  "rlfm (NEq a) = rsplit neq a"
-  "rlfm (Dvd i a) = rsplit (\<lambda> t. DVD i t) a"
-  "rlfm (NDvd i a) = rsplit (\<lambda> t. NDVD i t) a"
-  "rlfm (NOT (And p q)) = disj (rlfm (NOT p)) (rlfm (NOT q))"
-  "rlfm (NOT (Or p q)) = conj (rlfm (NOT p)) (rlfm (NOT q))"
-  "rlfm (NOT (Imp p q)) = conj (rlfm p) (rlfm (NOT q))"
-  "rlfm (NOT (Iff p q)) = disj (conj(rlfm p) (rlfm(NOT q))) (conj(rlfm(NOT p)) (rlfm q))"
-  "rlfm (NOT (NOT p)) = rlfm p"
-  "rlfm (NOT T) = F"
-  "rlfm (NOT F) = T"
-  "rlfm (NOT (Lt a)) = simpfm (rlfm (Ge a))"
-  "rlfm (NOT (Le a)) = simpfm (rlfm (Gt a))"
-  "rlfm (NOT (Gt a)) = simpfm (rlfm (Le a))"
-  "rlfm (NOT (Ge a)) = simpfm (rlfm (Lt a))"
-  "rlfm (NOT (Eq a)) = simpfm (rlfm (NEq a))"
-  "rlfm (NOT (NEq a)) = simpfm (rlfm (Eq a))"
-  "rlfm (NOT (Dvd i a)) = simpfm (rlfm (NDvd i a))"
-  "rlfm (NOT (NDvd i a)) = simpfm (rlfm (Dvd i a))"
-  "rlfm p = p" (hints simp add: fmsize_pos)
-
-lemma bound0at_l : "\<lbrakk>isatom p ; bound0 p\<rbrakk> \<Longrightarrow> isrlfm p"
-  by (induct p rule: isrlfm.induct, auto)
-lemma zgcd_le1: assumes ip: "0 < i" shows "zgcd i j \<le> i"
-proof-
-  from zgcd_zdvd1 have th: "zgcd i j dvd i" by blast
-  from zdvd_imp_le[OF th ip] show ?thesis .
-qed
-
-
-lemma simpfm_rl: "isrlfm p \<Longrightarrow> isrlfm (simpfm p)"
-proof (induct p)
-  case (Lt a) 
-  hence "bound0 (Lt a) \<or> (\<exists> c e. a = CN 0 c e \<and> c > 0 \<and> numbound0 e)"
-    by (cases a,simp_all, case_tac "nat", simp_all)
-  moreover
-  {assume "bound0 (Lt a)" hence bn:"bound0 (simpfm (Lt a))"  
-      using simpfm_bound0 by blast
-    have "isatom (simpfm (Lt a))" by (cases "simpnum a", auto simp add: Let_def)
-    with bn bound0at_l have ?case by blast}
-  moreover 
-  {fix c e assume "a = CN 0 c e" and "c>0" and "numbound0 e"
-    {
-      assume cn1:"numgcd (CN 0 c (simpnum e)) \<noteq> 1" and cnz:"numgcd (CN 0 c (simpnum e)) \<noteq> 0"
-      with numgcd_pos[where t="CN 0 c (simpnum e)"]
-      have th1:"numgcd (CN 0 c (simpnum e)) > 0" by simp
-      from prems have th:"numgcd (CN 0 c (simpnum e)) \<le> c" 
-	by (simp add: numgcd_def zgcd_le1)
-      from prems have th': "c\<noteq>0" by auto
-      from prems have cp: "c \<ge> 0" by simp
-      from zdiv_mono2[OF cp th1 th, simplified zdiv_self[OF th']]
-	have "0 < c div numgcd (CN 0 c (simpnum e))" by simp
-    }
-    with prems have ?case
-      by (simp add: Let_def reducecoeff_def reducecoeffh_numbound0)}
-  ultimately show ?case by blast
-next
-  case (Le a)   
-  hence "bound0 (Le a) \<or> (\<exists> c e. a = CN 0 c e \<and> c > 0 \<and> numbound0 e)"
-    by (cases a,simp_all, case_tac "nat", simp_all)
-  moreover
-  {assume "bound0 (Le a)" hence bn:"bound0 (simpfm (Le a))"  
-      using simpfm_bound0 by blast
-    have "isatom (simpfm (Le a))" by (cases "simpnum a", auto simp add: Let_def)
-    with bn bound0at_l have ?case by blast}
-  moreover 
-  {fix c e assume "a = CN 0 c e" and "c>0" and "numbound0 e"
-    {
-      assume cn1:"numgcd (CN 0 c (simpnum e)) \<noteq> 1" and cnz:"numgcd (CN 0 c (simpnum e)) \<noteq> 0"
-      with numgcd_pos[where t="CN 0 c (simpnum e)"]
-      have th1:"numgcd (CN 0 c (simpnum e)) > 0" by simp
-      from prems have th:"numgcd (CN 0 c (simpnum e)) \<le> c" 
-	by (simp add: numgcd_def zgcd_le1)
-      from prems have th': "c\<noteq>0" by auto
-      from prems have cp: "c \<ge> 0" by simp
-      from zdiv_mono2[OF cp th1 th, simplified zdiv_self[OF th']]
-	have "0 < c div numgcd (CN 0 c (simpnum e))" by simp
-    }
-    with prems have ?case
-      by (simp add: Let_def reducecoeff_def simpnum_numbound0 reducecoeffh_numbound0)}
-  ultimately show ?case by blast
-next
-  case (Gt a)   
-  hence "bound0 (Gt a) \<or> (\<exists> c e. a = CN 0 c e \<and> c > 0 \<and> numbound0 e)"
-    by (cases a,simp_all, case_tac "nat", simp_all)
-  moreover
-  {assume "bound0 (Gt a)" hence bn:"bound0 (simpfm (Gt a))"  
-      using simpfm_bound0 by blast
-    have "isatom (simpfm (Gt a))" by (cases "simpnum a", auto simp add: Let_def)
-    with bn bound0at_l have ?case by blast}
-  moreover 
-  {fix c e assume "a = CN 0 c e" and "c>0" and "numbound0 e"
-    {
-      assume cn1:"numgcd (CN 0 c (simpnum e)) \<noteq> 1" and cnz:"numgcd (CN 0 c (simpnum e)) \<noteq> 0"
-      with numgcd_pos[where t="CN 0 c (simpnum e)"]
-      have th1:"numgcd (CN 0 c (simpnum e)) > 0" by simp
-      from prems have th:"numgcd (CN 0 c (simpnum e)) \<le> c" 
-	by (simp add: numgcd_def zgcd_le1)
-      from prems have th': "c\<noteq>0" by auto
-      from prems have cp: "c \<ge> 0" by simp
-      from zdiv_mono2[OF cp th1 th, simplified zdiv_self[OF th']]
-	have "0 < c div numgcd (CN 0 c (simpnum e))" by simp
-    }
-    with prems have ?case
-      by (simp add: Let_def reducecoeff_def simpnum_numbound0 reducecoeffh_numbound0)}
-  ultimately show ?case by blast
-next
-  case (Ge a)   
-  hence "bound0 (Ge a) \<or> (\<exists> c e. a = CN 0 c e \<and> c > 0 \<and> numbound0 e)"
-    by (cases a,simp_all, case_tac "nat", simp_all)
-  moreover
-  {assume "bound0 (Ge a)" hence bn:"bound0 (simpfm (Ge a))"  
-      using simpfm_bound0 by blast
-    have "isatom (simpfm (Ge a))" by (cases "simpnum a", auto simp add: Let_def)
-    with bn bound0at_l have ?case by blast}
-  moreover 
-  {fix c e assume "a = CN 0 c e" and "c>0" and "numbound0 e"
-    {
-      assume cn1:"numgcd (CN 0 c (simpnum e)) \<noteq> 1" and cnz:"numgcd (CN 0 c (simpnum e)) \<noteq> 0"
-      with numgcd_pos[where t="CN 0 c (simpnum e)"]
-      have th1:"numgcd (CN 0 c (simpnum e)) > 0" by simp
-      from prems have th:"numgcd (CN 0 c (simpnum e)) \<le> c" 
-	by (simp add: numgcd_def zgcd_le1)
-      from prems have th': "c\<noteq>0" by auto
-      from prems have cp: "c \<ge> 0" by simp
-      from zdiv_mono2[OF cp th1 th, simplified zdiv_self[OF th']]
-	have "0 < c div numgcd (CN 0 c (simpnum e))" by simp
-    }
-    with prems have ?case
-      by (simp add: Let_def reducecoeff_def simpnum_numbound0 reducecoeffh_numbound0)}
-  ultimately show ?case by blast
-next
-  case (Eq a)   
-  hence "bound0 (Eq a) \<or> (\<exists> c e. a = CN 0 c e \<and> c > 0 \<and> numbound0 e)"
-    by (cases a,simp_all, case_tac "nat", simp_all)
-  moreover
-  {assume "bound0 (Eq a)" hence bn:"bound0 (simpfm (Eq a))"  
-      using simpfm_bound0 by blast
-    have "isatom (simpfm (Eq a))" by (cases "simpnum a", auto simp add: Let_def)
-    with bn bound0at_l have ?case by blast}
-  moreover 
-  {fix c e assume "a = CN 0 c e" and "c>0" and "numbound0 e"
-    {
-      assume cn1:"numgcd (CN 0 c (simpnum e)) \<noteq> 1" and cnz:"numgcd (CN 0 c (simpnum e)) \<noteq> 0"
-      with numgcd_pos[where t="CN 0 c (simpnum e)"]
-      have th1:"numgcd (CN 0 c (simpnum e)) > 0" by simp
-      from prems have th:"numgcd (CN 0 c (simpnum e)) \<le> c" 
-	by (simp add: numgcd_def zgcd_le1)
-      from prems have th': "c\<noteq>0" by auto
-      from prems have cp: "c \<ge> 0" by simp
-      from zdiv_mono2[OF cp th1 th, simplified zdiv_self[OF th']]
-	have "0 < c div numgcd (CN 0 c (simpnum e))" by simp
-    }
-    with prems have ?case
-      by (simp add: Let_def reducecoeff_def simpnum_numbound0 reducecoeffh_numbound0)}
-  ultimately show ?case by blast
-next
-  case (NEq a)  
-  hence "bound0 (NEq a) \<or> (\<exists> c e. a = CN 0 c e \<and> c > 0 \<and> numbound0 e)"
-    by (cases a,simp_all, case_tac "nat", simp_all)
-  moreover
-  {assume "bound0 (NEq a)" hence bn:"bound0 (simpfm (NEq a))"  
-      using simpfm_bound0 by blast
-    have "isatom (simpfm (NEq a))" by (cases "simpnum a", auto simp add: Let_def)
-    with bn bound0at_l have ?case by blast}
-  moreover 
-  {fix c e assume "a = CN 0 c e" and "c>0" and "numbound0 e"
-    {
-      assume cn1:"numgcd (CN 0 c (simpnum e)) \<noteq> 1" and cnz:"numgcd (CN 0 c (simpnum e)) \<noteq> 0"
-      with numgcd_pos[where t="CN 0 c (simpnum e)"]
-      have th1:"numgcd (CN 0 c (simpnum e)) > 0" by simp
-      from prems have th:"numgcd (CN 0 c (simpnum e)) \<le> c" 
-	by (simp add: numgcd_def zgcd_le1)
-      from prems have th': "c\<noteq>0" by auto
-      from prems have cp: "c \<ge> 0" by simp
-      from zdiv_mono2[OF cp th1 th, simplified zdiv_self[OF th']]
-	have "0 < c div numgcd (CN 0 c (simpnum e))" by simp
-    }
-    with prems have ?case
-      by (simp add: Let_def reducecoeff_def simpnum_numbound0 reducecoeffh_numbound0)}
-  ultimately show ?case by blast
-next
-  case (Dvd i a) hence "bound0 (Dvd i a)" by auto hence bn:"bound0 (simpfm (Dvd i a))"  
-    using simpfm_bound0 by blast
-  have "isatom (simpfm (Dvd i a))" by (cases "simpnum a", auto simp add: Let_def split_def)
-  with bn bound0at_l show ?case by blast
-next
-  case (NDvd i a)  hence "bound0 (NDvd i a)" by auto hence bn:"bound0 (simpfm (NDvd i a))"  
-    using simpfm_bound0 by blast
-  have "isatom (simpfm (NDvd i a))" by (cases "simpnum a", auto simp add: Let_def split_def)
-  with bn bound0at_l show ?case by blast
-qed(auto simp add: conj_def imp_def disj_def iff_def Let_def simpfm_bound0 numadd_nb numneg_nb)
-
-lemma rlfm_I:
-  assumes qfp: "qfree p"
-  and xp: "0 \<le> x" and x1: "x < 1"
-  shows "(Ifm (x#bs) (rlfm p) = Ifm (x# bs) p) \<and> isrlfm (rlfm p)"
-  using qfp 
-by (induct p rule: rlfm.induct) 
-(auto simp add: rsplit[OF xp x1 lt_mono] lt_l rsplit[OF xp x1 le_mono] le_l rsplit[OF xp x1 gt_mono] gt_l
-               rsplit[OF xp x1 ge_mono] ge_l rsplit[OF xp x1 eq_mono] eq_l rsplit[OF xp x1 neq_mono] neq_l
-               rsplit[OF xp x1 DVD_mono[OF xp x1]] DVD_l rsplit[OF xp x1 NDVD_mono[OF xp x1]] NDVD_l simpfm_rl)
-lemma rlfm_l:
-  assumes qfp: "qfree p"
-  shows "isrlfm (rlfm p)"
-  using qfp lt_l gt_l ge_l le_l eq_l neq_l DVD_l NDVD_l 
-by (induct p rule: rlfm.induct,auto simp add: simpfm_rl)
-
-    (* Operations needed for Ferrante and Rackoff *)
-lemma rminusinf_inf:
-  assumes lp: "isrlfm p"
-  shows "\<exists> z. \<forall> x < z. Ifm (x#bs) (minusinf p) = Ifm (x#bs) p" (is "\<exists> z. \<forall> x. ?P z x p")
-using lp
-proof (induct p rule: minusinf.induct)
-  case (1 p q) thus ?case by (auto,rule_tac x= "min z za" in exI) auto
-next
-  case (2 p q) thus ?case by (auto,rule_tac x= "min z za" in exI) auto
-next
-  case (3 c e) 
-  from prems have nb: "numbound0 e" by simp
-  from prems have cp: "real c > 0" by simp
-  fix a
-  let ?e="Inum (a#bs) e"
-  let ?z = "(- ?e) / real c"
-  {fix x
-    assume xz: "x < ?z"
-    hence "(real c * x < - ?e)" 
-      by (simp only: pos_less_divide_eq[OF cp, where a="x" and b="- ?e"] mult_ac) 
-    hence "real c * x + ?e < 0" by arith
-    hence "real c * x + ?e \<noteq> 0" by simp
-    with xz have "?P ?z x (Eq (CN 0 c e))"
-      using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp  }
-  hence "\<forall> x < ?z. ?P ?z x (Eq (CN 0 c e))" by simp
-  thus ?case by blast
-next
-  case (4 c e)   
-  from prems have nb: "numbound0 e" by simp
-  from prems have cp: "real c > 0" by simp
-  fix a
-  let ?e="Inum (a#bs) e"
-  let ?z = "(- ?e) / real c"
-  {fix x
-    assume xz: "x < ?z"
-    hence "(real c * x < - ?e)" 
-      by (simp only: pos_less_divide_eq[OF cp, where a="x" and b="- ?e"] mult_ac) 
-    hence "real c * x + ?e < 0" by arith
-    hence "real c * x + ?e \<noteq> 0" by simp
-    with xz have "?P ?z x (NEq (CN 0 c e))"
-      using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp }
-  hence "\<forall> x < ?z. ?P ?z x (NEq (CN 0 c e))" by simp
-  thus ?case by blast
-next
-  case (5 c e) 
-    from prems have nb: "numbound0 e" by simp
-  from prems have cp: "real c > 0" by simp
-  fix a
-  let ?e="Inum (a#bs) e"
-  let ?z = "(- ?e) / real c"
-  {fix x
-    assume xz: "x < ?z"
-    hence "(real c * x < - ?e)" 
-      by (simp only: pos_less_divide_eq[OF cp, where a="x" and b="- ?e"] mult_ac) 
-    hence "real c * x + ?e < 0" by arith
-    with xz have "?P ?z x (Lt (CN 0 c e))"
-      using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"]  by simp }
-  hence "\<forall> x < ?z. ?P ?z x (Lt (CN 0 c e))" by simp
-  thus ?case by blast
-next
-  case (6 c e)  
-    from prems have nb: "numbound0 e" by simp
-  from prems have cp: "real c > 0" by simp
-  fix a
-  let ?e="Inum (a#bs) e"
-  let ?z = "(- ?e) / real c"
-  {fix x
-    assume xz: "x < ?z"
-    hence "(real c * x < - ?e)" 
-      by (simp only: pos_less_divide_eq[OF cp, where a="x" and b="- ?e"] mult_ac) 
-    hence "real c * x + ?e < 0" by arith
-    with xz have "?P ?z x (Le (CN 0 c e))"
-      using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp }
-  hence "\<forall> x < ?z. ?P ?z x (Le (CN 0 c e))" by simp
-  thus ?case by blast
-next
-  case (7 c e)  
-    from prems have nb: "numbound0 e" by simp
-  from prems have cp: "real c > 0" by simp
-  fix a
-  let ?e="Inum (a#bs) e"
-  let ?z = "(- ?e) / real c"
-  {fix x
-    assume xz: "x < ?z"
-    hence "(real c * x < - ?e)" 
-      by (simp only: pos_less_divide_eq[OF cp, where a="x" and b="- ?e"] mult_ac) 
-    hence "real c * x + ?e < 0" by arith
-    with xz have "?P ?z x (Gt (CN 0 c e))"
-      using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp }
-  hence "\<forall> x < ?z. ?P ?z x (Gt (CN 0 c e))" by simp
-  thus ?case by blast
-next
-  case (8 c e)  
-    from prems have nb: "numbound0 e" by simp
-  from prems have cp: "real c > 0" by simp
-  fix a
-  let ?e="Inum (a#bs) e"
-  let ?z = "(- ?e) / real c"
-  {fix x
-    assume xz: "x < ?z"
-    hence "(real c * x < - ?e)" 
-      by (simp only: pos_less_divide_eq[OF cp, where a="x" and b="- ?e"] mult_ac) 
-    hence "real c * x + ?e < 0" by arith
-    with xz have "?P ?z x (Ge (CN 0 c e))"
-      using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp }
-  hence "\<forall> x < ?z. ?P ?z x (Ge (CN 0 c e))" by simp
-  thus ?case by blast
-qed simp_all
-
-lemma rplusinf_inf:
-  assumes lp: "isrlfm p"
-  shows "\<exists> z. \<forall> x > z. Ifm (x#bs) (plusinf p) = Ifm (x#bs) p" (is "\<exists> z. \<forall> x. ?P z x p")
-using lp
-proof (induct p rule: isrlfm.induct)
-  case (1 p q) thus ?case by (auto,rule_tac x= "max z za" in exI) auto
-next
-  case (2 p q) thus ?case by (auto,rule_tac x= "max z za" in exI) auto
-next
-  case (3 c e) 
-  from prems have nb: "numbound0 e" by simp
-  from prems have cp: "real c > 0" by simp
-  fix a
-  let ?e="Inum (a#bs) e"
-  let ?z = "(- ?e) / real c"
-  {fix x
-    assume xz: "x > ?z"
-    with mult_strict_right_mono [OF xz cp] cp
-    have "(real c * x > - ?e)" by (simp add: mult_ac)
-    hence "real c * x + ?e > 0" by arith
-    hence "real c * x + ?e \<noteq> 0" by simp
-    with xz have "?P ?z x (Eq (CN 0 c e))"
-      using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp }
-  hence "\<forall> x > ?z. ?P ?z x (Eq (CN 0 c e))" by simp
-  thus ?case by blast
-next
-  case (4 c e) 
-  from prems have nb: "numbound0 e" by simp
-  from prems have cp: "real c > 0" by simp
-  fix a
-  let ?e="Inum (a#bs) e"
-  let ?z = "(- ?e) / real c"
-  {fix x
-    assume xz: "x > ?z"
-    with mult_strict_right_mono [OF xz cp] cp
-    have "(real c * x > - ?e)" by (simp add: mult_ac)
-    hence "real c * x + ?e > 0" by arith
-    hence "real c * x + ?e \<noteq> 0" by simp
-    with xz have "?P ?z x (NEq (CN 0 c e))"
-      using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp }
-  hence "\<forall> x > ?z. ?P ?z x (NEq (CN 0 c e))" by simp
-  thus ?case by blast
-next
-  case (5 c e) 
-  from prems have nb: "numbound0 e" by simp
-  from prems have cp: "real c > 0" by simp
-  fix a
-  let ?e="Inum (a#bs) e"
-  let ?z = "(- ?e) / real c"
-  {fix x
-    assume xz: "x > ?z"
-    with mult_strict_right_mono [OF xz cp] cp
-    have "(real c * x > - ?e)" by (simp add: mult_ac)
-    hence "real c * x + ?e > 0" by arith
-    with xz have "?P ?z x (Lt (CN 0 c e))"
-      using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp }
-  hence "\<forall> x > ?z. ?P ?z x (Lt (CN 0 c e))" by simp
-  thus ?case by blast
-next
-  case (6 c e) 
-  from prems have nb: "numbound0 e" by simp
-  from prems have cp: "real c > 0" by simp
-  fix a
-  let ?e="Inum (a#bs) e"
-  let ?z = "(- ?e) / real c"
-  {fix x
-    assume xz: "x > ?z"
-    with mult_strict_right_mono [OF xz cp] cp
-    have "(real c * x > - ?e)" by (simp add: mult_ac)
-    hence "real c * x + ?e > 0" by arith
-    with xz have "?P ?z x (Le (CN 0 c e))"
-      using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp }
-  hence "\<forall> x > ?z. ?P ?z x (Le (CN 0 c e))" by simp
-  thus ?case by blast
-next
-  case (7 c e) 
-  from prems have nb: "numbound0 e" by simp
-  from prems have cp: "real c > 0" by simp
-  fix a
-  let ?e="Inum (a#bs) e"
-  let ?z = "(- ?e) / real c"
-  {fix x
-    assume xz: "x > ?z"
-    with mult_strict_right_mono [OF xz cp] cp
-    have "(real c * x > - ?e)" by (simp add: mult_ac)
-    hence "real c * x + ?e > 0" by arith
-    with xz have "?P ?z x (Gt (CN 0 c e))"
-      using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"] by simp }
-  hence "\<forall> x > ?z. ?P ?z x (Gt (CN 0 c e))" by simp
-  thus ?case by blast
-next
-  case (8 c e) 
-  from prems have nb: "numbound0 e" by simp
-  from prems have cp: "real c > 0" by simp
-  fix a
-  let ?e="Inum (a#bs) e"
-  let ?z = "(- ?e) / real c"
-  {fix x
-    assume xz: "x > ?z"
-    with mult_strict_right_mono [OF xz cp] cp
-    have "(real c * x > - ?e)" by (simp add: mult_ac)
-    hence "real c * x + ?e > 0" by arith
-    with xz have "?P ?z x (Ge (CN 0 c e))"
-      using numbound0_I[OF nb, where b="x" and bs="bs" and b'="a"]   by simp }
-  hence "\<forall> x > ?z. ?P ?z x (Ge (CN 0 c e))" by simp
-  thus ?case by blast
-qed simp_all
-
-lemma rminusinf_bound0:
-  assumes lp: "isrlfm p"
-  shows "bound0 (minusinf p)"
-  using lp
-  by (induct p rule: minusinf.induct) simp_all
-
-lemma rplusinf_bound0:
-  assumes lp: "isrlfm p"
-  shows "bound0 (plusinf p)"
-  using lp
-  by (induct p rule: plusinf.induct) simp_all
-
-lemma rminusinf_ex:
-  assumes lp: "isrlfm p"
-  and ex: "Ifm (a#bs) (minusinf p)"
-  shows "\<exists> x. Ifm (x#bs) p"
-proof-
-  from bound0_I [OF rminusinf_bound0[OF lp], where b="a" and bs ="bs"] ex
-  have th: "\<forall> x. Ifm (x#bs) (minusinf p)" by auto
-  from rminusinf_inf[OF lp, where bs="bs"] 
-  obtain z where z_def: "\<forall>x<z. Ifm (x # bs) (minusinf p) = Ifm (x # bs) p" by blast
-  from th have "Ifm ((z - 1)#bs) (minusinf p)" by simp
-  moreover have "z - 1 < z" by simp
-  ultimately show ?thesis using z_def by auto
-qed
-
-lemma rplusinf_ex:
-  assumes lp: "isrlfm p"
-  and ex: "Ifm (a#bs) (plusinf p)"
-  shows "\<exists> x. Ifm (x#bs) p"
-proof-
-  from bound0_I [OF rplusinf_bound0[OF lp], where b="a" and bs ="bs"] ex
-  have th: "\<forall> x. Ifm (x#bs) (plusinf p)" by auto
-  from rplusinf_inf[OF lp, where bs="bs"] 
-  obtain z where z_def: "\<forall>x>z. Ifm (x # bs) (plusinf p) = Ifm (x # bs) p" by blast
-  from th have "Ifm ((z + 1)#bs) (plusinf p)" by simp
-  moreover have "z + 1 > z" by simp
-  ultimately show ?thesis using z_def by auto
-qed
-
-consts 
-  \<Upsilon>:: "fm \<Rightarrow> (num \<times> int) list"
-  \<upsilon> :: "fm \<Rightarrow> (num \<times> int) \<Rightarrow> fm "
-recdef \<Upsilon> "measure size"
-  "\<Upsilon> (And p q) = (\<Upsilon> p @ \<Upsilon> q)" 
-  "\<Upsilon> (Or p q) = (\<Upsilon> p @ \<Upsilon> q)" 
-  "\<Upsilon> (Eq  (CN 0 c e)) = [(Neg e,c)]"
-  "\<Upsilon> (NEq (CN 0 c e)) = [(Neg e,c)]"
-  "\<Upsilon> (Lt  (CN 0 c e)) = [(Neg e,c)]"
-  "\<Upsilon> (Le  (CN 0 c e)) = [(Neg e,c)]"
-  "\<Upsilon> (Gt  (CN 0 c e)) = [(Neg e,c)]"
-  "\<Upsilon> (Ge  (CN 0 c e)) = [(Neg e,c)]"
-  "\<Upsilon> p = []"
-
-recdef \<upsilon> "measure size"
-  "\<upsilon> (And p q) = (\<lambda> (t,n). And (\<upsilon> p (t,n)) (\<upsilon> q (t,n)))"
-  "\<upsilon> (Or p q) = (\<lambda> (t,n). Or (\<upsilon> p (t,n)) (\<upsilon> q (t,n)))"
-  "\<upsilon> (Eq (CN 0 c e)) = (\<lambda> (t,n). Eq (Add (Mul c t) (Mul n e)))"
-  "\<upsilon> (NEq (CN 0 c e)) = (\<lambda> (t,n). NEq (Add (Mul c t) (Mul n e)))"
-  "\<upsilon> (Lt (CN 0 c e)) = (\<lambda> (t,n). Lt (Add (Mul c t) (Mul n e)))"
-  "\<upsilon> (Le (CN 0 c e)) = (\<lambda> (t,n). Le (Add (Mul c t) (Mul n e)))"
-  "\<upsilon> (Gt (CN 0 c e)) = (\<lambda> (t,n). Gt (Add (Mul c t) (Mul n e)))"
-  "\<upsilon> (Ge (CN 0 c e)) = (\<lambda> (t,n). Ge (Add (Mul c t) (Mul n e)))"
-  "\<upsilon> p = (\<lambda> (t,n). p)"
-
-lemma \<upsilon>_I: assumes lp: "isrlfm p"
-  and np: "real n > 0" and nbt: "numbound0 t"
-  shows "(Ifm (x#bs) (\<upsilon> p (t,n)) = Ifm (((Inum (x#bs) t)/(real n))#bs) p) \<and> bound0 (\<upsilon> p (t,n))" (is "(?I x (\<upsilon> p (t,n)) = ?I ?u p) \<and> ?B p" is "(_ = ?I (?t/?n) p) \<and> _" is "(_ = ?I (?N x t /_) p) \<and> _")
-  using lp
-proof(induct p rule: \<upsilon>.induct)
-  case (5 c e) from prems have cp: "c >0" and nb: "numbound0 e" by simp+
-  have "?I ?u (Lt (CN 0 c e)) = (real c *(?t/?n) + (?N x e) < 0)"
-    using numbound0_I[OF nb, where bs="bs" and b="?u" and b'="x"] by simp
-  also have "\<dots> = (?n*(real c *(?t/?n)) + ?n*(?N x e) < 0)"
-    by (simp only: pos_less_divide_eq[OF np, where a="real c *(?t/?n) + (?N x e)" 
-      and b="0", simplified divide_zero_left]) (simp only: algebra_simps)
-  also have "\<dots> = (real c *?t + ?n* (?N x e) < 0)"
-    using np by simp 
-  finally show ?case using nbt nb by (simp add: algebra_simps)
-next
-  case (6 c e) from prems have cp: "c >0" and nb: "numbound0 e" by simp+
-  have "?I ?u (Le (CN 0 c e)) = (real c *(?t/?n) + (?N x e) \<le> 0)"
-    using numbound0_I[OF nb, where bs="bs" and b="?u" and b'="x"] by simp
-  also have "\<dots> = (?n*(real c *(?t/?n)) + ?n*(?N x e) \<le> 0)"
-    by (simp only: pos_le_divide_eq[OF np, where a="real c *(?t/?n) + (?N x e)" 
-      and b="0", simplified divide_zero_left]) (simp only: algebra_simps)
-  also have "\<dots> = (real c *?t + ?n* (?N x e) \<le> 0)"
-    using np by simp 
-  finally show ?case using nbt nb by (simp add: algebra_simps)
-next
-  case (7 c e) from prems have cp: "c >0" and nb: "numbound0 e" by simp+
-  have "?I ?u (Gt (CN 0 c e)) = (real c *(?t/?n) + (?N x e) > 0)"
-    using numbound0_I[OF nb, where bs="bs" and b="?u" and b'="x"] by simp
-  also have "\<dots> = (?n*(real c *(?t/?n)) + ?n*(?N x e) > 0)"
-    by (simp only: pos_divide_less_eq[OF np, where a="real c *(?t/?n) + (?N x e)" 
-      and b="0", simplified divide_zero_left]) (simp only: algebra_simps)
-  also have "\<dots> = (real c *?t + ?n* (?N x e) > 0)"
-    using np by simp 
-  finally show ?case using nbt nb by (simp add: algebra_simps)
-next
-  case (8 c e) from prems have cp: "c >0" and nb: "numbound0 e" by simp+
-  have "?I ?u (Ge (CN 0 c e)) = (real c *(?t/?n) + (?N x e) \<ge> 0)"
-    using numbound0_I[OF nb, where bs="bs" and b="?u" and b'="x"] by simp
-  also have "\<dots> = (?n*(real c *(?t/?n)) + ?n*(?N x e) \<ge> 0)"
-    by (simp only: pos_divide_le_eq[OF np, where a="real c *(?t/?n) + (?N x e)" 
-      and b="0", simplified divide_zero_left]) (simp only: algebra_simps)
-  also have "\<dots> = (real c *?t + ?n* (?N x e) \<ge> 0)"
-    using np by simp 
-  finally show ?case using nbt nb by (simp add: algebra_simps)
-next
-  case (3 c e) from prems have cp: "c >0" and nb: "numbound0 e" by simp+
-  from np have np: "real n \<noteq> 0" by simp
-  have "?I ?u (Eq (CN 0 c e)) = (real c *(?t/?n) + (?N x e) = 0)"
-    using numbound0_I[OF nb, where bs="bs" and b="?u" and b'="x"] by simp
-  also have "\<dots> = (?n*(real c *(?t/?n)) + ?n*(?N x e) = 0)"
-    by (simp only: nonzero_eq_divide_eq[OF np, where a="real c *(?t/?n) + (?N x e)" 
-      and b="0", simplified divide_zero_left]) (simp only: algebra_simps)
-  also have "\<dots> = (real c *?t + ?n* (?N x e) = 0)"
-    using np by simp 
-  finally show ?case using nbt nb by (simp add: algebra_simps)
-next
-  case (4 c e) from prems have cp: "c >0" and nb: "numbound0 e" by simp+
-  from np have np: "real n \<noteq> 0" by simp
-  have "?I ?u (NEq (CN 0 c e)) = (real c *(?t/?n) + (?N x e) \<noteq> 0)"
-    using numbound0_I[OF nb, where bs="bs" and b="?u" and b'="x"] by simp
-  also have "\<dots> = (?n*(real c *(?t/?n)) + ?n*(?N x e) \<noteq> 0)"
-    by (simp only: nonzero_eq_divide_eq[OF np, where a="real c *(?t/?n) + (?N x e)" 
-      and b="0", simplified divide_zero_left]) (simp only: algebra_simps)
-  also have "\<dots> = (real c *?t + ?n* (?N x e) \<noteq> 0)"
-    using np by simp 
-  finally show ?case using nbt nb by (simp add: algebra_simps)
-qed(simp_all add: nbt numbound0_I[where bs ="bs" and b="(Inum (x#bs) t)/ real n" and b'="x"] nth_pos2)
-
-lemma \<Upsilon>_l:
-  assumes lp: "isrlfm p"
-  shows "\<forall> (t,k) \<in> set (\<Upsilon> p). numbound0 t \<and> k >0"
-using lp
-by(induct p rule: \<Upsilon>.induct)  auto
-
-lemma rminusinf_\<Upsilon>:
-  assumes lp: "isrlfm p"
-  and nmi: "\<not> (Ifm (a#bs) (minusinf p))" (is "\<not> (Ifm (a#bs) (?M p))")
-  and ex: "Ifm (x#bs) p" (is "?I x p")
-  shows "\<exists> (s,m) \<in> set (\<Upsilon> p). x \<ge> Inum (a#bs) s / real m" (is "\<exists> (s,m) \<in> ?U p. x \<ge> ?N a s / real m")
-proof-
-  have "\<exists> (s,m) \<in> set (\<Upsilon> p). real m * x \<ge> Inum (a#bs) s " (is "\<exists> (s,m) \<in> ?U p. real m *x \<ge> ?N a s")
-    using lp nmi ex
-    by (induct p rule: minusinf.induct, auto simp add:numbound0_I[where bs="bs" and b="a" and b'="x"] nth_pos2)
-  then obtain s m where smU: "(s,m) \<in> set (\<Upsilon> p)" and mx: "real m * x \<ge> ?N a s" by blast
-  from \<Upsilon>_l[OF lp] smU have mp: "real m > 0" by auto
-  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" 
-    by (auto simp add: mult_commute)
-  thus ?thesis using smU by auto
-qed
-
-lemma rplusinf_\<Upsilon>:
-  assumes lp: "isrlfm p"
-  and nmi: "\<not> (Ifm (a#bs) (plusinf p))" (is "\<not> (Ifm (a#bs) (?M p))")
-  and ex: "Ifm (x#bs) p" (is "?I x p")
-  shows "\<exists> (s,m) \<in> set (\<Upsilon> p). x \<le> Inum (a#bs) s / real m" (is "\<exists> (s,m) \<in> ?U p. x \<le> ?N a s / real m")
-proof-
-  have "\<exists> (s,m) \<in> set (\<Upsilon> p). real m * x \<le> Inum (a#bs) s " (is "\<exists> (s,m) \<in> ?U p. real m *x \<le> ?N a s")
-    using lp nmi ex
-    by (induct p rule: minusinf.induct, auto simp add:numbound0_I[where bs="bs" and b="a" and b'="x"] nth_pos2)
-  then obtain s m where smU: "(s,m) \<in> set (\<Upsilon> p)" and mx: "real m * x \<le> ?N a s" by blast
-  from \<Upsilon>_l[OF lp] smU have mp: "real m > 0" by auto
-  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" 
-    by (auto simp add: mult_commute)
-  thus ?thesis using smU by auto
-qed
-
-lemma lin_dense: 
-  assumes lp: "isrlfm p"
-  and noS: "\<forall> t. l < t \<and> t< u \<longrightarrow> t \<notin> (\<lambda> (t,n). Inum (x#bs) t / real n) ` set (\<Upsilon> p)" 
-  (is "\<forall> t. _ \<and> _ \<longrightarrow> t \<notin> (\<lambda> (t,n). ?N x t / real n ) ` (?U p)")
-  and lx: "l < x" and xu:"x < u" and px:" Ifm (x#bs) p"
-  and ly: "l < y" and yu: "y < u"
-  shows "Ifm (y#bs) p"
-using lp px noS
-proof (induct p rule: isrlfm.induct)
-  case (5 c e) hence cp: "real c > 0" and nb: "numbound0 e" by simp+
-    from prems have "x * real c + ?N x e < 0" by (simp add: algebra_simps)
-    hence pxc: "x < (- ?N x e) / real c" 
-      by (simp only: pos_less_divide_eq[OF cp, where a="x" and b="-?N x e"])
-    from prems have noSc:"\<forall> t. l < t \<and> t < u \<longrightarrow> t \<noteq> (- ?N x e) / real c" by auto
-    with ly yu have yne: "y \<noteq> - ?N x e / real c" by auto
-    hence "y < (- ?N x e) / real c \<or> y > (-?N x e) / real c" by auto
-    moreover {assume y: "y < (-?N x e)/ real c"
-      hence "y * real c < - ?N x e"
-	by (simp add: pos_less_divide_eq[OF cp, where a="y" and b="-?N x e", symmetric])
-      hence "real c * y + ?N x e < 0" by (simp add: algebra_simps)
-      hence ?case using numbound0_I[OF nb, where bs="bs" and b="x" and b'="y"] by simp}
-    moreover {assume y: "y > (- ?N x e) / real c" 
-      with yu have eu: "u > (- ?N x e) / real c" by auto
-      with noSc ly yu have "(- ?N x e) / real c \<le> l" by (cases "(- ?N x e) / real c > l", auto)
-      with lx pxc have "False" by auto
-      hence ?case by simp }
-    ultimately show ?case by blast
-next
-  case (6 c e) hence cp: "real c > 0" and nb: "numbound0 e" by simp +
-    from prems have "x * real c + ?N x e \<le> 0" by (simp add: algebra_simps)
-    hence pxc: "x \<le> (- ?N x e) / real c" 
-      by (simp only: pos_le_divide_eq[OF cp, where a="x" and b="-?N x e"])
-    from prems have noSc:"\<forall> t. l < t \<and> t < u \<longrightarrow> t \<noteq> (- ?N x e) / real c" by auto
-    with ly yu have yne: "y \<noteq> - ?N x e / real c" by auto
-    hence "y < (- ?N x e) / real c \<or> y > (-?N x e) / real c" by auto
-    moreover {assume y: "y < (-?N x e)/ real c"
-      hence "y * real c < - ?N x e"
-	by (simp add: pos_less_divide_eq[OF cp, where a="y" and b="-?N x e", symmetric])
-      hence "real c * y + ?N x e < 0" by (simp add: algebra_simps)
-      hence ?case using numbound0_I[OF nb, where bs="bs" and b="x" and b'="y"] by simp}
-    moreover {assume y: "y > (- ?N x e) / real c" 
-      with yu have eu: "u > (- ?N x e) / real c" by auto
-      with noSc ly yu have "(- ?N x e) / real c \<le> l" by (cases "(- ?N x e) / real c > l", auto)
-      with lx pxc have "False" by auto
-      hence ?case by simp }
-    ultimately show ?case by blast
-next
-  case (7 c e) hence cp: "real c > 0" and nb: "numbound0 e" by simp+
-    from prems have "x * real c + ?N x e > 0" by (simp add: algebra_simps)
-    hence pxc: "x > (- ?N x e) / real c" 
-      by (simp only: pos_divide_less_eq[OF cp, where a="x" and b="-?N x e"])
-    from prems have noSc:"\<forall> t. l < t \<and> t < u \<longrightarrow> t \<noteq> (- ?N x e) / real c" by auto
-    with ly yu have yne: "y \<noteq> - ?N x e / real c" by auto
-    hence "y < (- ?N x e) / real c \<or> y > (-?N x e) / real c" by auto
-    moreover {assume y: "y > (-?N x e)/ real c"
-      hence "y * real c > - ?N x e"
-	by (simp add: pos_divide_less_eq[OF cp, where a="y" and b="-?N x e", symmetric])
-      hence "real c * y + ?N x e > 0" by (simp add: algebra_simps)
-      hence ?case using numbound0_I[OF nb, where bs="bs" and b="x" and b'="y"] by simp}
-    moreover {assume y: "y < (- ?N x e) / real c" 
-      with ly have eu: "l < (- ?N x e) / real c" by auto
-      with noSc ly yu have "(- ?N x e) / real c \<ge> u" by (cases "(- ?N x e) / real c > l", auto)
-      with xu pxc have "False" by auto
-      hence ?case by simp }
-    ultimately show ?case by blast
-next
-  case (8 c e) hence cp: "real c > 0" and nb: "numbound0 e" by simp+
-    from prems have "x * real c + ?N x e \<ge> 0" by (simp add: algebra_simps)
-    hence pxc: "x \<ge> (- ?N x e) / real c" 
-      by (simp only: pos_divide_le_eq[OF cp, where a="x" and b="-?N x e"])
-    from prems have noSc:"\<forall> t. l < t \<and> t < u \<longrightarrow> t \<noteq> (- ?N x e) / real c" by auto
-    with ly yu have yne: "y \<noteq> - ?N x e / real c" by auto
-    hence "y < (- ?N x e) / real c \<or> y > (-?N x e) / real c" by auto
-    moreover {assume y: "y > (-?N x e)/ real c"
-      hence "y * real c > - ?N x e"
-	by (simp add: pos_divide_less_eq[OF cp, where a="y" and b="-?N x e", symmetric])
-      hence "real c * y + ?N x e > 0" by (simp add: algebra_simps)
-      hence ?case using numbound0_I[OF nb, where bs="bs" and b="x" and b'="y"] by simp}
-    moreover {assume y: "y < (- ?N x e) / real c" 
-      with ly have eu: "l < (- ?N x e) / real c" by auto
-      with noSc ly yu have "(- ?N x e) / real c \<ge> u" by (cases "(- ?N x e) / real c > l", auto)
-      with xu pxc have "False" by auto
-      hence ?case by simp }
-    ultimately show ?case by blast
-next
-  case (3 c e) hence cp: "real c > 0" and nb: "numbound0 e" by simp+
-    from cp have cnz: "real c \<noteq> 0" by simp
-    from prems have "x * real c + ?N x e = 0" by (simp add: algebra_simps)
-    hence pxc: "x = (- ?N x e) / real c" 
-      by (simp only: nonzero_eq_divide_eq[OF cnz, where a="x" and b="-?N x e"])
-    from prems have noSc:"\<forall> t. l < t \<and> t < u \<longrightarrow> t \<noteq> (- ?N x e) / real c" by auto
-    with lx xu have yne: "x \<noteq> - ?N x e / real c" by auto
-    with pxc show ?case by simp
-next
-  case (4 c e) hence cp: "real c > 0" and nb: "numbound0 e" by simp+
-    from cp have cnz: "real c \<noteq> 0" by simp
-    from prems have noSc:"\<forall> t. l < t \<and> t < u \<longrightarrow> t \<noteq> (- ?N x e) / real c" by auto
-    with ly yu have yne: "y \<noteq> - ?N x e / real c" by auto
-    hence "y* real c \<noteq> -?N x e"      
-      by (simp only: nonzero_eq_divide_eq[OF cnz, where a="y" and b="-?N x e"]) simp
-    hence "y* real c + ?N x e \<noteq> 0" by (simp add: algebra_simps)
-    thus ?case using numbound0_I[OF nb, where bs="bs" and b="x" and b'="y"] 
-      by (simp add: algebra_simps)
-qed (auto simp add: nth_pos2 numbound0_I[where bs="bs" and b="y" and b'="x"])
-
-lemma finite_set_intervals:
-  assumes px: "P (x::real)" 
-  and lx: "l \<le> x" and xu: "x \<le> u"
-  and linS: "l\<in> S" and uinS: "u \<in> S"
-  and fS:"finite S" and lS: "\<forall> x\<in> S. l \<le> x" and Su: "\<forall> x\<in> S. x \<le> u"
-  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"
-proof-
-  let ?Mx = "{y. y\<in> S \<and> y \<le> x}"
-  let ?xM = "{y. y\<in> S \<and> x \<le> y}"
-  let ?a = "Max ?Mx"
-  let ?b = "Min ?xM"
-  have MxS: "?Mx \<subseteq> S" by blast
-  hence fMx: "finite ?Mx" using fS finite_subset by auto
-  from lx linS have linMx: "l \<in> ?Mx" by blast
-  hence Mxne: "?Mx \<noteq> {}" by blast
-  have xMS: "?xM \<subseteq> S" by blast
-  hence fxM: "finite ?xM" using fS finite_subset by auto
-  from xu uinS have linxM: "u \<in> ?xM" by blast
-  hence xMne: "?xM \<noteq> {}" by blast
-  have ax:"?a \<le> x" using Mxne fMx by auto
-  have xb:"x \<le> ?b" using xMne fxM by auto
-  have "?a \<in> ?Mx" using Max_in[OF fMx Mxne] by simp hence ainS: "?a \<in> S" using MxS by blast
-  have "?b \<in> ?xM" using Min_in[OF fxM xMne] by simp hence binS: "?b \<in> S" using xMS by blast
-  have noy:"\<forall> y. ?a < y \<and> y < ?b \<longrightarrow> y \<notin> S"
-  proof(clarsimp)
-    fix y
-    assume ay: "?a < y" and yb: "y < ?b" and yS: "y \<in> S"
-    from yS have "y\<in> ?Mx \<or> y\<in> ?xM" by auto
-    moreover {assume "y \<in> ?Mx" hence "y \<le> ?a" using Mxne fMx by auto with ay have "False" by simp}
-    moreover {assume "y \<in> ?xM" hence "y \<ge> ?b" using xMne fxM by auto with yb have "False" by simp}
-    ultimately show "False" by blast
-  qed
-  from ainS binS noy ax xb px show ?thesis by blast
-qed
-
-lemma finite_set_intervals2:
-  assumes px: "P (x::real)" 
-  and lx: "l \<le> x" and xu: "x \<le> u"
-  and linS: "l\<in> S" and uinS: "u \<in> S"
-  and fS:"finite S" and lS: "\<forall> x\<in> S. l \<le> x" and Su: "\<forall> x\<in> S. x \<le> u"
-  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)"
-proof-
-  from finite_set_intervals[where P="P", OF px lx xu linS uinS fS lS Su]
-  obtain a and b where 
-    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
-  from axb have "x= a \<or> x= b \<or> (a < x \<and> x < b)" by auto
-  thus ?thesis using px as bs noS by blast 
-qed
-
-lemma rinf_\<Upsilon>:
-  assumes lp: "isrlfm p"
-  and nmi: "\<not> (Ifm (x#bs) (minusinf p))" (is "\<not> (Ifm (x#bs) (?M p))")
-  and npi: "\<not> (Ifm (x#bs) (plusinf p))" (is "\<not> (Ifm (x#bs) (?P p))")
-  and ex: "\<exists> x.  Ifm (x#bs) p" (is "\<exists> x. ?I x p")
-  shows "\<exists> (l,n) \<in> set (\<Upsilon> p). \<exists> (s,m) \<in> set (\<Upsilon> p). ?I ((Inum (x#bs) l / real n + Inum (x#bs) s / real m) / 2) p" 
-proof-
-  let ?N = "\<lambda> x t. Inum (x#bs) t"
-  let ?U = "set (\<Upsilon> p)"
-  from ex obtain a where pa: "?I a p" by blast
-  from bound0_I[OF rminusinf_bound0[OF lp], where bs="bs" and b="x" and b'="a"] nmi
-  have nmi': "\<not> (?I a (?M p))" by simp
-  from bound0_I[OF rplusinf_bound0[OF lp], where bs="bs" and b="x" and b'="a"] npi
-  have npi': "\<not> (?I a (?P p))" by simp
-  have "\<exists> (l,n) \<in> set (\<Upsilon> p). \<exists> (s,m) \<in> set (\<Upsilon> p). ?I ((?N a l/real n + ?N a s /real m) / 2) p"
-  proof-
-    let ?M = "(\<lambda> (t,c). ?N a t / real c) ` ?U"
-    have fM: "finite ?M" by auto
-    from rminusinf_\<Upsilon>[OF lp nmi pa] rplusinf_\<Upsilon>[OF lp npi pa] 
-    have "\<exists> (l,n) \<in> set (\<Upsilon> p). \<exists> (s,m) \<in> set (\<Upsilon> p). a \<le> ?N x l / real n \<and> a \<ge> ?N x s / real m" by blast
-    then obtain "t" "n" "s" "m" where 
-      tnU: "(t,n) \<in> ?U" and smU: "(s,m) \<in> ?U" 
-      and xs1: "a \<le> ?N x s / real m" and tx1: "a \<ge> ?N x t / real n" by blast
-    from \<Upsilon>_l[OF lp] tnU smU numbound0_I[where bs="bs" and b="x" and b'="a"] xs1 tx1 have xs: "a \<le> ?N a s / real m" and tx: "a \<ge> ?N a t / real n" by auto
-    from tnU have Mne: "?M \<noteq> {}" by auto
-    hence Une: "?U \<noteq> {}" by simp
-    let ?l = "Min ?M"
-    let ?u = "Max ?M"
-    have linM: "?l \<in> ?M" using fM Mne by simp
-    have uinM: "?u \<in> ?M" using fM Mne by simp
-    have tnM: "?N a t / real n \<in> ?M" using tnU by auto
-    have smM: "?N a s / real m \<in> ?M" using smU by auto 
-    have lM: "\<forall> t\<in> ?M. ?l \<le> t" using Mne fM by auto
-    have Mu: "\<forall> t\<in> ?M. t \<le> ?u" using Mne fM by auto
-    have "?l \<le> ?N a t / real n" using tnM Mne by simp hence lx: "?l \<le> a" using tx by simp
-    have "?N a s / real m \<le> ?u" using smM Mne by simp hence xu: "a \<le> ?u" using xs by simp
-    from finite_set_intervals2[where P="\<lambda> x. ?I x p",OF pa lx xu linM uinM fM lM Mu]
-    have "(\<exists> s\<in> ?M. ?I s p) \<or> 
-      (\<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)" .
-    moreover { fix u assume um: "u\<in> ?M" and pu: "?I u p"
-      hence "\<exists> (tu,nu) \<in> ?U. u = ?N a tu / real nu" by auto
-      then obtain "tu" "nu" where tuU: "(tu,nu) \<in> ?U" and tuu:"u= ?N a tu / real nu" by blast
-      have "(u + u) / 2 = u" by auto with pu tuu 
-      have "?I (((?N a tu / real nu) + (?N a tu / real nu)) / 2) p" by simp
-      with tuU have ?thesis by blast}
-    moreover{
-      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"
-      then obtain t1 and t2 where t1M: "t1 \<in> ?M" and t2M: "t2\<in> ?M" 
-	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"
-	by blast
-      from t1M have "\<exists> (t1u,t1n) \<in> ?U. t1 = ?N a t1u / real t1n" by auto
-      then obtain "t1u" "t1n" where t1uU: "(t1u,t1n) \<in> ?U" and t1u: "t1 = ?N a t1u / real t1n" by blast
-      from t2M have "\<exists> (t2u,t2n) \<in> ?U. t2 = ?N a t2u / real t2n" by auto
-      then obtain "t2u" "t2n" where t2uU: "(t2u,t2n) \<in> ?U" and t2u: "t2 = ?N a t2u / real t2n" by blast
-      from t1x xt2 have t1t2: "t1 < t2" by simp
-      let ?u = "(t1 + t2) / 2"
-      from less_half_sum[OF t1t2] gt_half_sum[OF t1t2] have t1lu: "t1 < ?u" and ut2: "?u < t2" by auto
-      from lin_dense[OF lp noM t1x xt2 px t1lu ut2] have "?I ?u p" .
-      with t1uU t2uU t1u t2u have ?thesis by blast}
-    ultimately show ?thesis by blast
-  qed
-  then obtain "l" "n" "s"  "m" where lnU: "(l,n) \<in> ?U" and smU:"(s,m) \<in> ?U" 
-    and pu: "?I ((?N a l / real n + ?N a s / real m) / 2) p" by blast
-  from lnU smU \<Upsilon>_l[OF lp] have nbl: "numbound0 l" and nbs: "numbound0 s" by auto
-  from numbound0_I[OF nbl, where bs="bs" and b="a" and b'="x"] 
-    numbound0_I[OF nbs, where bs="bs" and b="a" and b'="x"] pu
-  have "?I ((?N x l / real n + ?N x s / real m) / 2) p" by simp
-  with lnU smU
-  show ?thesis by auto
-qed
-    (* The Ferrante - Rackoff Theorem *)
-
-theorem fr_eq: 
-  assumes lp: "isrlfm p"
-  shows "(\<exists> x. Ifm (x#bs) p) = ((Ifm (x#bs) (minusinf p)) \<or> (Ifm (x#bs) (plusinf p)) \<or> (\<exists> (t,n) \<in> set (\<Upsilon> p). \<exists> (s,m) \<in> set (\<Upsilon> p). Ifm ((((Inum (x#bs) t)/  real n + (Inum (x#bs) s) / real m) /2)#bs) p))"
-  (is "(\<exists> x. ?I x p) = (?M \<or> ?P \<or> ?F)" is "?E = ?D")
-proof
-  assume px: "\<exists> x. ?I x p"
-  have "?M \<or> ?P \<or> (\<not> ?M \<and> \<not> ?P)" by blast
-  moreover {assume "?M \<or> ?P" hence "?D" by blast}
-  moreover {assume nmi: "\<not> ?M" and npi: "\<not> ?P"
-    from rinf_\<Upsilon>[OF lp nmi npi] have "?F" using px by blast hence "?D" by blast}
-  ultimately show "?D" by blast
-next
-  assume "?D" 
-  moreover {assume m:"?M" from rminusinf_ex[OF lp m] have "?E" .}
-  moreover {assume p: "?P" from rplusinf_ex[OF lp p] have "?E" . }
-  moreover {assume f:"?F" hence "?E" by blast}
-  ultimately show "?E" by blast
-qed
-
-
-lemma fr_eq\<upsilon>: 
-  assumes lp: "isrlfm p"
-  shows "(\<exists> x. Ifm (x#bs) p) = ((Ifm (x#bs) (minusinf p)) \<or> (Ifm (x#bs) (plusinf p)) \<or> (\<exists> (t,k) \<in> set (\<Upsilon> p). \<exists> (s,l) \<in> set (\<Upsilon> p). Ifm (x#bs) (\<upsilon> p (Add(Mul l t) (Mul k s) , 2*k*l))))"
-  (is "(\<exists> x. ?I x p) = (?M \<or> ?P \<or> ?F)" is "?E = ?D")
-proof
-  assume px: "\<exists> x. ?I x p"
-  have "?M \<or> ?P \<or> (\<not> ?M \<and> \<not> ?P)" by blast
-  moreover {assume "?M \<or> ?P" hence "?D" by blast}
-  moreover {assume nmi: "\<not> ?M" and npi: "\<not> ?P"
-    let ?f ="\<lambda> (t,n). Inum (x#bs) t / real n"
-    let ?N = "\<lambda> t. Inum (x#bs) t"
-    {fix t n s m assume "(t,n)\<in> set (\<Upsilon> p)" and "(s,m) \<in> set (\<Upsilon> p)"
-      with \<Upsilon>_l[OF lp] have tnb: "numbound0 t" and np:"real n > 0" and snb: "numbound0 s" and mp:"real m > 0"
-	by auto
-      let ?st = "Add (Mul m t) (Mul n s)"
-      from mult_pos_pos[OF np mp] have mnp: "real (2*n*m) > 0" 
-	by (simp add: mult_commute)
-      from tnb snb have st_nb: "numbound0 ?st" by simp
-      have st: "(?N t / real n + ?N s / real m)/2 = ?N ?st / real (2*n*m)"
-	using mnp mp np by (simp add: algebra_simps add_divide_distrib)
-      from \<upsilon>_I[OF lp mnp st_nb, where x="x" and bs="bs"] 
-      have "?I x (\<upsilon> p (?st,2*n*m)) = ?I ((?N t / real n + ?N s / real m) /2) p" by (simp only: st[symmetric])}
-    with rinf_\<Upsilon>[OF lp nmi npi px] have "?F" by blast hence "?D" by blast}
-  ultimately show "?D" by blast
-next
-  assume "?D" 
-  moreover {assume m:"?M" from rminusinf_ex[OF lp m] have "?E" .}
-  moreover {assume p: "?P" from rplusinf_ex[OF lp p] have "?E" . }
-  moreover {fix t k s l assume "(t,k) \<in> set (\<Upsilon> p)" and "(s,l) \<in> set (\<Upsilon> p)" 
-    and px:"?I x (\<upsilon> p (Add (Mul l t) (Mul k s), 2*k*l))"
-    with \<Upsilon>_l[OF lp] have tnb: "numbound0 t" and np:"real k > 0" and snb: "numbound0 s" and mp:"real l > 0" by auto
-    let ?st = "Add (Mul l t) (Mul k s)"
-    from mult_pos_pos[OF np mp] have mnp: "real (2*k*l) > 0" 
-      by (simp add: mult_commute)
-    from tnb snb have st_nb: "numbound0 ?st" by simp
-    from \<upsilon>_I[OF lp mnp st_nb, where bs="bs"] px have "?E" by auto}
-  ultimately show "?E" by blast
-qed
-
-text{* The overall Part *}
-
-lemma real_ex_int_real01:
-  shows "(\<exists> (x::real). P x) = (\<exists> (i::int) (u::real). 0\<le> u \<and> u< 1 \<and> P (real i + u))"
-proof(auto)
-  fix x
-  assume Px: "P x"
-  let ?i = "floor x"
-  let ?u = "x - real ?i"
-  have "x = real ?i + ?u" by simp
-  hence "P (real ?i + ?u)" using Px by simp
-  moreover have "real ?i \<le> x" using real_of_int_floor_le by simp hence "0 \<le> ?u" by arith
-  moreover have "?u < 1" using real_of_int_floor_add_one_gt[where r="x"] by arith 
-  ultimately show "(\<exists> (i::int) (u::real). 0\<le> u \<and> u< 1 \<and> P (real i + u))" by blast
-qed
-
-consts exsplitnum :: "num \<Rightarrow> num"
-  exsplit :: "fm \<Rightarrow> fm"
-recdef exsplitnum "measure size"
-  "exsplitnum (C c) = (C c)"
-  "exsplitnum (Bound 0) = Add (Bound 0) (Bound 1)"
-  "exsplitnum (Bound n) = Bound (n+1)"
-  "exsplitnum (Neg a) = Neg (exsplitnum a)"
-  "exsplitnum (Add a b) = Add (exsplitnum a) (exsplitnum b) "
-  "exsplitnum (Sub a b) = Sub (exsplitnum a) (exsplitnum b) "
-  "exsplitnum (Mul c a) = Mul c (exsplitnum a)"
-  "exsplitnum (Floor a) = Floor (exsplitnum a)"
-  "exsplitnum (CN 0 c a) = CN 0 c (Add (Mul c (Bound 1)) (exsplitnum a))"
-  "exsplitnum (CN n c a) = CN (n+1) c (exsplitnum a)"
-  "exsplitnum (CF c s t) = CF c (exsplitnum s) (exsplitnum t)"
-
-recdef exsplit "measure size"
-  "exsplit (Lt a) = Lt (exsplitnum a)"
-  "exsplit (Le a) = Le (exsplitnum a)"
-  "exsplit (Gt a) = Gt (exsplitnum a)"
-  "exsplit (Ge a) = Ge (exsplitnum a)"
-  "exsplit (Eq a) = Eq (exsplitnum a)"
-  "exsplit (NEq a) = NEq (exsplitnum a)"
-  "exsplit (Dvd i a) = Dvd i (exsplitnum a)"
-  "exsplit (NDvd i a) = NDvd i (exsplitnum a)"
-  "exsplit (And p q) = And (exsplit p) (exsplit q)"
-  "exsplit (Or p q) = Or (exsplit p) (exsplit q)"
-  "exsplit (Imp p q) = Imp (exsplit p) (exsplit q)"
-  "exsplit (Iff p q) = Iff (exsplit p) (exsplit q)"
-  "exsplit (NOT p) = NOT (exsplit p)"
-  "exsplit p = p"
-
-lemma exsplitnum: 
-  "Inum (x#y#bs) (exsplitnum t) = Inum ((x+y) #bs) t"
-  by(induct t rule: exsplitnum.induct) (simp_all add: algebra_simps)
-
-lemma exsplit: 
-  assumes qfp: "qfree p"
-  shows "Ifm (x#y#bs) (exsplit p) = Ifm ((x+y)#bs) p"
-using qfp exsplitnum[where x="x" and y="y" and bs="bs"]
-by(induct p rule: exsplit.induct) simp_all
-
-lemma splitex:
-  assumes qf: "qfree p"
-  shows "(Ifm bs (E p)) = (\<exists> (i::int). Ifm (real i#bs) (E (And (And (Ge(CN 0 1 (C 0))) (Lt (CN 0 1 (C (- 1))))) (exsplit p))))" (is "?lhs = ?rhs")
-proof-
-  have "?rhs = (\<exists> (i::int). \<exists> x. 0\<le> x \<and> x < 1 \<and> Ifm (x#(real i)#bs) (exsplit p))"
-    by (simp add: myless[rule_format, where b="1"] myless[rule_format, where b="0"] add_ac diff_def)
-  also have "\<dots> = (\<exists> (i::int). \<exists> x. 0\<le> x \<and> x < 1 \<and> Ifm ((real i + x) #bs) p)"
-    by (simp only: exsplit[OF qf] add_ac)
-  also have "\<dots> = (\<exists> x. Ifm (x#bs) p)" 
-    by (simp only: real_ex_int_real01[where P="\<lambda> x. Ifm (x#bs) p"])
-  finally show ?thesis by simp
-qed
-
-    (* Implement the right hand sides of Cooper's theorem and Ferrante and Rackoff. *)
-
-constdefs ferrack01:: "fm \<Rightarrow> fm"
-  "ferrack01 p \<equiv> (let p' = rlfm(And (And (Ge(CN 0 1 (C 0))) (Lt (CN 0 1 (C (- 1))))) p);
-                    U = remdups(map simp_num_pair 
-                     (map (\<lambda> ((t,n),(s,m)). (Add (Mul m t) (Mul n s) , 2*n*m))
-                           (alluopairs (\<Upsilon> p')))) 
-  in decr (evaldjf (\<upsilon> p') U ))"
-
-lemma fr_eq_01: 
-  assumes qf: "qfree p"
-  shows "(\<exists> x. Ifm (x#bs) (And (And (Ge(CN 0 1 (C 0))) (Lt (CN 0 1 (C (- 1))))) p)) = (\<exists> (t,n) \<in> set (\<Upsilon> (rlfm (And (And (Ge(CN 0 1 (C 0))) (Lt (CN 0 1 (C (- 1))))) p))). \<exists> (s,m) \<in> set (\<Upsilon> (rlfm (And (And (Ge(CN 0 1 (C 0))) (Lt (CN 0 1 (C (- 1))))) p))). Ifm (x#bs) (\<upsilon> (rlfm (And (And (Ge(CN 0 1 (C 0))) (Lt (CN 0 1 (C (- 1))))) p)) (Add (Mul m t) (Mul n s), 2*n*m)))"
-  (is "(\<exists> x. ?I x ?q) = ?F")
-proof-
-  let ?rq = "rlfm ?q"
-  let ?M = "?I x (minusinf ?rq)"
-  let ?P = "?I x (plusinf ?rq)"
-  have MF: "?M = False"
-    apply (simp add: Let_def reducecoeff_def numgcd_def zgcd_def rsplit_def ge_def lt_def conj_def disj_def)
-    by (cases "rlfm p = And (Ge (CN 0 1 (C 0))) (Lt (CN 0 1 (C -1)))", simp_all)
-  have PF: "?P = False" apply (simp add: Let_def reducecoeff_def numgcd_def zgcd_def rsplit_def ge_def lt_def conj_def disj_def)
-    by (cases "rlfm p = And (Ge (CN 0 1 (C 0))) (Lt (CN 0 1 (C -1)))", simp_all)
-  have "(\<exists> x. ?I x ?q ) = 
-    ((?I x (minusinf ?rq)) \<or> (?I x (plusinf ?rq )) \<or> (\<exists> (t,n) \<in> set (\<Upsilon> ?rq). \<exists> (s,m) \<in> set (\<Upsilon> ?rq ). ?I x (\<upsilon> ?rq (Add (Mul m t) (Mul n s), 2*n*m))))"
-    (is "(\<exists> x. ?I x ?q) = (?M \<or> ?P \<or> ?F)" is "?E = ?D")
-  proof
-    assume "\<exists> x. ?I x ?q"  
-    then obtain x where qx: "?I x ?q" by blast
-    hence xp: "0\<le> x" and x1: "x< 1" and px: "?I x p" 
-      by (auto simp add: rsplit_def lt_def ge_def rlfm_I[OF qf])
-    from qx have "?I x ?rq " 
-      by (simp add: rsplit_def lt_def ge_def rlfm_I[OF qf xp x1])
-    hence lqx: "?I x ?rq " using simpfm[where p="?rq" and bs="x#bs"] by auto
-    from qf have qfq:"isrlfm ?rq"  
-      by (auto simp add: rsplit_def lt_def ge_def rlfm_I[OF qf xp x1])
-    with lqx fr_eq\<upsilon>[OF qfq] show "?M \<or> ?P \<or> ?F" by blast
-  next
-    assume D: "?D"
-    let ?U = "set (\<Upsilon> ?rq )"
-    from MF PF D have "?F" by auto
-    then obtain t n s m where aU:"(t,n) \<in> ?U" and bU:"(s,m)\<in> ?U" and rqx: "?I x (\<upsilon> ?rq (Add (Mul m t) (Mul n s), 2*n*m))" by blast
-    from qf have lrq:"isrlfm ?rq"using rlfm_l[OF qf] 
-      by (auto simp add: rsplit_def lt_def ge_def)
-    from aU bU \<Upsilon>_l[OF lrq] have tnb: "numbound0 t" and np:"real n > 0" and snb: "numbound0 s" and mp:"real m > 0" by (auto simp add: split_def)
-    let ?st = "Add (Mul m t) (Mul n s)"
-    from tnb snb have stnb: "numbound0 ?st" by simp
-    from mult_pos_pos[OF np mp] have mnp: "real (2*n*m) > 0" 
-      by (simp add: mult_commute)
-    from conjunct1[OF \<upsilon>_I[OF lrq mnp stnb, where bs="bs" and x="x"], symmetric] rqx
-    have "\<exists> x. ?I x ?rq" by auto
-    thus "?E" 
-      using rlfm_I[OF qf] by (auto simp add: rsplit_def lt_def ge_def)
-  qed
-  with MF PF show ?thesis by blast
-qed
-
-lemma \<Upsilon>_cong_aux:
-  assumes Ul: "\<forall> (t,n) \<in> set U. numbound0 t \<and> n >0"
-  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))"
-  (is "?lhs = ?rhs")
-proof(auto)
-  fix t n s m
-  assume "((t,n),(s,m)) \<in> set (alluopairs U)"
-  hence th: "((t,n),(s,m)) \<in> (set U \<times> set U)"
-    using alluopairs_set1[where xs="U"] by blast
-  let ?N = "\<lambda> t. Inum (x#bs) t"
-  let ?st= "Add (Mul m t) (Mul n s)"
-  from Ul th have mnz: "m \<noteq> 0" by auto
-  from Ul th have  nnz: "n \<noteq> 0" by auto  
-  have st: "(?N t / real n + ?N s / real m)/2 = ?N ?st / real (2*n*m)"
-   using mnz nnz by (simp add: algebra_simps add_divide_distrib)
- 
-  thus "(real m *  Inum (x # bs) t + real n * Inum (x # bs) s) /
-       (2 * real n * real m)
-       \<in> (\<lambda>((t, n), s, m).
-             (Inum (x # bs) t / real n + Inum (x # bs) s / real m) / 2) `
-         (set U \<times> set U)"using mnz nnz th  
-    apply (auto simp add: th add_divide_distrib algebra_simps split_def image_def)
-    by (rule_tac x="(s,m)" in bexI,simp_all) 
-  (rule_tac x="(t,n)" in bexI,simp_all)
-next
-  fix t n s m
-  assume tnU: "(t,n) \<in> set U" and smU:"(s,m) \<in> set U" 
-  let ?N = "\<lambda> t. Inum (x#bs) t"
-  let ?st= "Add (Mul m t) (Mul n s)"
-  from Ul smU have mnz: "m \<noteq> 0" by auto
-  from Ul tnU have  nnz: "n \<noteq> 0" by auto  
-  have st: "(?N t / real n + ?N s / real m)/2 = ?N ?st / real (2*n*m)"
-   using mnz nnz by (simp add: algebra_simps add_divide_distrib)
- 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"
- have Pc:"\<forall> a b. ?P a b = ?P b a"
-   by auto
- from Ul alluopairs_set1 have Up:"\<forall> ((t,n),(s,m)) \<in> set (alluopairs U). n \<noteq> 0 \<and> m \<noteq> 0" by blast
- from alluopairs_ex[OF Pc, where xs="U"] tnU smU
- have th':"\<exists> ((t',n'),(s',m')) \<in> set (alluopairs U). ?P (t',n') (s',m')"
-   by blast
- then obtain t' n' s' m' where ts'_U: "((t',n'),(s',m')) \<in> set (alluopairs U)" 
-   and Pts': "?P (t',n') (s',m')" by blast
- from ts'_U Up have mnz': "m' \<noteq> 0" and nnz': "n'\<noteq> 0" by auto
- let ?st' = "Add (Mul m' t') (Mul n' s')"
-   have st': "(?N t' / real n' + ?N s' / real m')/2 = ?N ?st' / real (2*n'*m')"
-   using mnz' nnz' by (simp add: algebra_simps add_divide_distrib)
- from Pts' have 
-   "(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
- 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')
- finally show "(Inum (x # bs) t / real n + Inum (x # bs) s / real m) / 2
-          \<in> (\<lambda>(t, n). Inum (x # bs) t / real n) `
-            (\<lambda>((t, n), s, m). (Add (Mul m t) (Mul n s), 2 * n * m)) `
-            set (alluopairs U)"
-   using ts'_U by blast
-qed
-
-lemma \<Upsilon>_cong:
-  assumes lp: "isrlfm p"
-  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)")
-  and U: "\<forall> (t,n) \<in> U. numbound0 t \<and> n > 0"
-  and U': "\<forall> (t,n) \<in> U'. numbound0 t \<and> n > 0"
-  shows "(\<exists> (t,n) \<in> U. \<exists> (s,m) \<in> U. Ifm (x#bs) (\<upsilon> p (Add (Mul m t) (Mul n s),2*n*m))) = (\<exists> (t,n) \<in> U'. Ifm (x#bs) (\<upsilon> p (t,n)))"
-  (is "?lhs = ?rhs")
-proof
-  assume ?lhs
-  then obtain t n s m where tnU: "(t,n) \<in> U" and smU:"(s,m) \<in> U" and 
-    Pst: "Ifm (x#bs) (\<upsilon> p (Add (Mul m t) (Mul n s),2*n*m))" by blast
-  let ?N = "\<lambda> t. Inum (x#bs) t"
-  from tnU smU U have tnb: "numbound0 t" and np: "n > 0" 
-    and snb: "numbound0 s" and mp:"m > 0"  by auto
-  let ?st= "Add (Mul m t) (Mul n s)"
-  from mult_pos_pos[OF np mp] have mnp: "real (2*n*m) > 0" 
-      by (simp add: mult_commute real_of_int_mult[symmetric] del: real_of_int_mult)
-    from tnb snb have stnb: "numbound0 ?st" by simp
-  have st: "(?N t / real n + ?N s / real m)/2 = ?N ?st / real (2*n*m)"
-   using mp np by (simp add: algebra_simps add_divide_distrib)
-  from tnU smU UU' have "?g ((t,n),(s,m)) \<in> ?f ` U'" by blast
-  hence "\<exists> (t',n') \<in> U'. ?g ((t,n),(s,m)) = ?f (t',n')"
-    by auto (rule_tac x="(a,b)" in bexI, auto)
-  then obtain t' n' where tnU': "(t',n') \<in> U'" and th: "?g ((t,n),(s,m)) = ?f (t',n')" by blast
-  from U' tnU' have tnb': "numbound0 t'" and np': "real n' > 0" by auto
-  from \<upsilon>_I[OF lp mnp stnb, where bs="bs" and x="x"] Pst 
-  have Pst2: "Ifm (Inum (x # bs) (Add (Mul m t) (Mul n s)) / real (2 * n * m) # bs) p" by simp
-  from conjunct1[OF \<upsilon>_I[OF lp np' tnb', where bs="bs" and x="x"], symmetric] th[simplified split_def fst_conv snd_conv,symmetric] Pst2[simplified st[symmetric]]
-  have "Ifm (x # bs) (\<upsilon> p (t', n')) " by (simp only: st) 
-  then show ?rhs using tnU' by auto 
-next
-  assume ?rhs
-  then obtain t' n' where tnU': "(t',n') \<in> U'" and Pt': "Ifm (x # bs) (\<upsilon> p (t', n'))" 
-    by blast
-  from tnU' UU' have "?f (t',n') \<in> ?g ` (U\<times>U)" by blast
-  hence "\<exists> ((t,n),(s,m)) \<in> (U\<times>U). ?f (t',n') = ?g ((t,n),(s,m))" 
-    by auto (rule_tac x="(a,b)" in bexI, auto)
-  then obtain t n s m where tnU: "(t,n) \<in> U" and smU:"(s,m) \<in> U" and 
-    th: "?f (t',n') = ?g((t,n),(s,m)) "by blast
-    let ?N = "\<lambda> t. Inum (x#bs) t"
-  from tnU smU U have tnb: "numbound0 t" and np: "n > 0" 
-    and snb: "numbound0 s" and mp:"m > 0"  by auto
-  let ?st= "Add (Mul m t) (Mul n s)"
-  from mult_pos_pos[OF np mp] have mnp: "real (2*n*m) > 0" 
-      by (simp add: mult_commute real_of_int_mult[symmetric] del: real_of_int_mult)
-    from tnb snb have stnb: "numbound0 ?st" by simp
-  have st: "(?N t / real n + ?N s / real m)/2 = ?N ?st / real (2*n*m)"
-   using mp np by (simp add: algebra_simps add_divide_distrib)
-  from U' tnU' have tnb': "numbound0 t'" and np': "real n' > 0" by auto
-  from \<upsilon>_I[OF lp np' tnb', where bs="bs" and x="x",simplified th[simplified split_def fst_conv snd_conv] st] Pt'
-  have Pst2: "Ifm (Inum (x # bs) (Add (Mul m t) (Mul n s)) / real (2 * n * m) # bs) p" by simp
-  with \<upsilon>_I[OF lp mnp stnb, where x="x" and bs="bs"] tnU smU show ?lhs by blast
-qed
-  
-lemma ferrack01: 
-  assumes qf: "qfree p"
-  shows "((\<exists> x. Ifm (x#bs) (And (And (Ge(CN 0 1 (C 0))) (Lt (CN 0 1 (C (- 1))))) p)) = (Ifm bs (ferrack01 p))) \<and> qfree (ferrack01 p)" (is "(?lhs = ?rhs) \<and> _")
-proof-
-  let ?I = "\<lambda> x p. Ifm (x#bs) p"
-  fix x
-  let ?N = "\<lambda> t. Inum (x#bs) t"
-  let ?q = "rlfm (And (And (Ge(CN 0 1 (C 0))) (Lt (CN 0 1 (C (- 1))))) p)"
-  let ?U = "\<Upsilon> ?q"
-  let ?Up = "alluopairs ?U"
-  let ?g = "\<lambda> ((t,n),(s,m)). (Add (Mul m t) (Mul n s) , 2*n*m)"
-  let ?S = "map ?g ?Up"
-  let ?SS = "map simp_num_pair ?S"
-  let ?Y = "remdups ?SS"
-  let ?f= "(\<lambda> (t,n). ?N t / real n)"
-  let ?h = "\<lambda> ((t,n),(s,m)). (?N t/real n + ?N s/ real m) /2"
-  let ?F = "\<lambda> p. \<exists> a \<in> set (\<Upsilon> p). \<exists> b \<in> set (\<Upsilon> p). ?I x (\<upsilon> p (?g(a,b)))"
-  let ?ep = "evaldjf (\<upsilon> ?q) ?Y"
-  from rlfm_l[OF qf] have lq: "isrlfm ?q" 
-    by (simp add: rsplit_def lt_def ge_def conj_def disj_def Let_def reducecoeff_def numgcd_def zgcd_def)
-  from alluopairs_set1[where xs="?U"] have UpU: "set ?Up \<le> (set ?U \<times> set ?U)" by simp
-  from \<Upsilon>_l[OF lq] have U_l: "\<forall> (t,n) \<in> set ?U. numbound0 t \<and> n > 0" .
-  from U_l UpU 
-  have Up_: "\<forall> ((t,n),(s,m)) \<in> set ?Up. numbound0 t \<and> n> 0 \<and> numbound0 s \<and> m > 0" by auto
-  hence Snb: "\<forall> (t,n) \<in> set ?S. numbound0 t \<and> n > 0 "
-    by (auto simp add: mult_pos_pos)
-  have Y_l: "\<forall> (t,n) \<in> set ?Y. numbound0 t \<and> n > 0" 
-  proof-
-    { fix t n assume tnY: "(t,n) \<in> set ?Y" 
-      hence "(t,n) \<in> set ?SS" by simp
-      hence "\<exists> (t',n') \<in> set ?S. simp_num_pair (t',n') = (t,n)"
-	by (auto simp add: split_def) (rule_tac x="((aa,ba),(ab,bb))" in bexI, simp_all)
-      then obtain t' n' where tn'S: "(t',n') \<in> set ?S" and tns: "simp_num_pair (t',n') = (t,n)" by blast
-      from tn'S Snb have tnb: "numbound0 t'" and np: "n' > 0" by auto
-      from simp_num_pair_l[OF tnb np tns]
-      have "numbound0 t \<and> n > 0" . }
-    thus ?thesis by blast
-  qed
-
-  have YU: "(?f ` set ?Y) = (?h ` (set ?U \<times> set ?U))"
-  proof-
-     from simp_num_pair_ci[where bs="x#bs"] have 
-    "\<forall>x. (?f o simp_num_pair) x = ?f x" by auto
-     hence th: "?f o simp_num_pair = ?f" using ext by blast
-    have "(?f ` set ?Y) = ((?f o simp_num_pair) ` set ?S)" by (simp add: image_compose)
-    also have "\<dots> = (?f ` set ?S)" by (simp add: th)
-    also have "\<dots> = ((?f o ?g) ` set ?Up)" 
-      by (simp only: set_map o_def image_compose[symmetric])
-    also have "\<dots> = (?h ` (set ?U \<times> set ?U))"
-      using \<Upsilon>_cong_aux[OF U_l, where x="x" and bs="bs", simplified set_map image_compose[symmetric]] by blast
-    finally show ?thesis .
-  qed
-  have "\<forall> (t,n) \<in> set ?Y. bound0 (\<upsilon> ?q (t,n))"
-  proof-
-    { fix t n assume tnY: "(t,n) \<in> set ?Y"
-      with Y_l have tnb: "numbound0 t" and np: "real n > 0" by auto
-      from \<upsilon>_I[OF lq np tnb]
-    have "bound0 (\<upsilon> ?q (t,n))"  by simp}
-    thus ?thesis by blast
-  qed
-  hence ep_nb: "bound0 ?ep"  using evaldjf_bound0[where xs="?Y" and f="\<upsilon> ?q"]
-    by auto
-
-  from fr_eq_01[OF qf, where bs="bs" and x="x"] have "?lhs = ?F ?q"
-    by (simp only: split_def fst_conv snd_conv)
-  also have "\<dots> = (\<exists> (t,n) \<in> set ?Y. ?I x (\<upsilon> ?q (t,n)))" using \<Upsilon>_cong[OF lq YU U_l Y_l]
-    by (simp only: split_def fst_conv snd_conv) 
-  also have "\<dots> = (Ifm (x#bs) ?ep)" 
-    using evaldjf_ex[where ps="?Y" and bs = "x#bs" and f="\<upsilon> ?q",symmetric]
-    by (simp only: split_def pair_collapse)
-  also have "\<dots> = (Ifm bs (decr ?ep))" using decr[OF ep_nb] by blast
-  finally have lr: "?lhs = ?rhs" by (simp only: ferrack01_def Let_def)
-  from decr_qf[OF ep_nb] have "qfree (ferrack01 p)" by (simp only: Let_def ferrack01_def)
-  with lr show ?thesis by blast
-qed
-
-lemma cp_thm': 
-  assumes lp: "iszlfm p (real (i::int)#bs)"
-  and up: "d\<beta> p 1" and dd: "d\<delta> p d" and dp: "d > 0"
-  shows "(\<exists> (x::int). Ifm (real x#bs) p) = ((\<exists> j\<in> {1 .. d}. Ifm (real j#bs) (minusinf p)) \<or> (\<exists> j\<in> {1.. d}. \<exists> b\<in> (Inum (real i#bs)) ` set (\<beta> p). Ifm ((b+real j)#bs) p))"
-  using cp_thm[OF lp up dd dp] by auto
-
-constdefs unit:: "fm \<Rightarrow> fm \<times> num list \<times> int"
-  "unit p \<equiv> (let p' = zlfm p ; l = \<zeta> p' ; q = And (Dvd l (CN 0 1 (C 0))) (a\<beta> p' l); d = \<delta> q;
-             B = remdups (map simpnum (\<beta> q)) ; a = remdups (map simpnum (\<alpha> q))
-             in if length B \<le> length a then (q,B,d) else (mirror q, a,d))"
-
-lemma unit: assumes qf: "qfree p"
-  shows "\<And> q B d. unit p = (q,B,d) \<Longrightarrow> ((\<exists> (x::int). Ifm (real x#bs) p) = (\<exists> (x::int). Ifm (real x#bs) q)) \<and> (Inum (real i#bs)) ` set B = (Inum (real i#bs)) ` set (\<beta> q) \<and> d\<beta> q 1 \<and> d\<delta> q d \<and> d >0 \<and> iszlfm q (real (i::int)#bs) \<and> (\<forall> b\<in> set B. numbound0 b)"
-proof-
-  fix q B d 
-  assume qBd: "unit p = (q,B,d)"
-  let ?thes = "((\<exists> (x::int). Ifm (real x#bs) p) = (\<exists> (x::int). Ifm (real x#bs) q)) \<and>
-    Inum (real i#bs) ` set B = Inum (real i#bs) ` set (\<beta> q) \<and>
-    d\<beta> q 1 \<and> d\<delta> q d \<and> 0 < d \<and> iszlfm q (real i # bs) \<and> (\<forall> b\<in> set B. numbound0 b)"
-  let ?I = "\<lambda> (x::int) p. Ifm (real x#bs) p"
-  let ?p' = "zlfm p"
-  let ?l = "\<zeta> ?p'"
-  let ?q = "And (Dvd ?l (CN 0 1 (C 0))) (a\<beta> ?p' ?l)"
-  let ?d = "\<delta> ?q"
-  let ?B = "set (\<beta> ?q)"
-  let ?B'= "remdups (map simpnum (\<beta> ?q))"
-  let ?A = "set (\<alpha> ?q)"
-  let ?A'= "remdups (map simpnum (\<alpha> ?q))"
-  from conjunct1[OF zlfm_I[OF qf, where bs="bs"]] 
-  have pp': "\<forall> i. ?I i ?p' = ?I i p" by auto
-  from iszlfm_gen[OF conjunct2[OF zlfm_I[OF qf, where bs="bs" and i="i"]]]
-  have lp': "\<forall> (i::int). iszlfm ?p' (real i#bs)" by simp 
-  hence lp'': "iszlfm ?p' (real (i::int)#bs)" by simp
-  from lp' \<zeta>[where p="?p'" and bs="bs"] have lp: "?l >0" and dl: "d\<beta> ?p' ?l" by auto
-  from a\<beta>_ex[where p="?p'" and l="?l" and bs="bs", OF lp'' dl lp] pp'
-  have pq_ex:"(\<exists> (x::int). ?I x p) = (\<exists> x. ?I x ?q)" by (simp add: int_rdvd_iff) 
-  from lp'' lp a\<beta>[OF lp'' dl lp] have lq:"iszlfm ?q (real i#bs)" and uq: "d\<beta> ?q 1" 
-    by (auto simp add: isint_def)
-  from \<delta>[OF lq] have dp:"?d >0" and dd: "d\<delta> ?q ?d" by blast+
-  let ?N = "\<lambda> t. Inum (real (i::int)#bs) t"
-  have "?N ` set ?B' = ((?N o simpnum) ` ?B)" by (simp add:image_compose) 
-  also have "\<dots> = ?N ` ?B" using simpnum_ci[where bs="real i #bs"] by auto
-  finally have BB': "?N ` set ?B' = ?N ` ?B" .
-  have "?N ` set ?A' = ((?N o simpnum) ` ?A)" by (simp add:image_compose) 
-  also have "\<dots> = ?N ` ?A" using simpnum_ci[where bs="real i #bs"] by auto
-  finally have AA': "?N ` set ?A' = ?N ` ?A" .
-  from \<beta>_numbound0[OF lq] have B_nb:"\<forall> b\<in> set ?B'. numbound0 b"
-    by (simp add: simpnum_numbound0)
-  from \<alpha>_l[OF lq] have A_nb: "\<forall> b\<in> set ?A'. numbound0 b"
-    by (simp add: simpnum_numbound0)
-    {assume "length ?B' \<le> length ?A'"
-    hence q:"q=?q" and "B = ?B'" and d:"d = ?d"
-      using qBd by (auto simp add: Let_def unit_def)
-    with BB' B_nb have b: "?N ` (set B) = ?N ` set (\<beta> q)" 
-      and bn: "\<forall>b\<in> set B. numbound0 b" by simp+ 
-  with pq_ex dp uq dd lq q d have ?thes by simp}
-  moreover 
-  {assume "\<not> (length ?B' \<le> length ?A')"
-    hence q:"q=mirror ?q" and "B = ?A'" and d:"d = ?d"
-      using qBd by (auto simp add: Let_def unit_def)
-    with AA' mirror\<alpha>\<beta>[OF lq] A_nb have b:"?N ` (set B) = ?N ` set (\<beta> q)" 
-      and bn: "\<forall>b\<in> set B. numbound0 b" by simp+
-    from mirror_ex[OF lq] pq_ex q 
-    have pqm_eq:"(\<exists> (x::int). ?I x p) = (\<exists> (x::int). ?I x q)" by simp
-    from lq uq q mirror_d\<beta> [where p="?q" and bs="bs" and a="real i"]
-    have lq': "iszlfm q (real i#bs)" and uq: "d\<beta> q 1" by auto
-    from \<delta>[OF lq'] mirror_\<delta>[OF lq] q d have dq:"d\<delta> q d " by auto
-    from pqm_eq b bn uq lq' dp dq q dp d have ?thes by simp
-  }
-  ultimately show ?thes by blast
-qed
-    (* Cooper's Algorithm *)
-
-constdefs cooper :: "fm \<Rightarrow> fm"
-  "cooper p \<equiv> 
-  (let (q,B,d) = unit p; js = iupt (1,d);
-       mq = simpfm (minusinf q);
-       md = evaldjf (\<lambda> j. simpfm (subst0 (C j) mq)) js
-   in if md = T then T else
-    (let qd = evaldjf (\<lambda> t. simpfm (subst0 t q)) 
-                               (remdups (map (\<lambda> (b,j). simpnum (Add b (C j))) 
-                                            [(b,j). b\<leftarrow>B,j\<leftarrow>js]))
-     in decr (disj md qd)))"
-lemma cooper: assumes qf: "qfree p"
-  shows "((\<exists> (x::int). Ifm (real x#bs) p) = (Ifm bs (cooper p))) \<and> qfree (cooper p)" 
-  (is "(?lhs = ?rhs) \<and> _")
-proof-
-
-  let ?I = "\<lambda> (x::int) p. Ifm (real x#bs) p"
-  let ?q = "fst (unit p)"
-  let ?B = "fst (snd(unit p))"
-  let ?d = "snd (snd (unit p))"
-  let ?js = "iupt (1,?d)"
-  let ?mq = "minusinf ?q"
-  let ?smq = "simpfm ?mq"
-  let ?md = "evaldjf (\<lambda> j. simpfm (subst0 (C j) ?smq)) ?js"
-  fix i
-  let ?N = "\<lambda> t. Inum (real (i::int)#bs) t"
-  let ?bjs = "[(b,j). b\<leftarrow>?B,j\<leftarrow>?js]"
-  let ?sbjs = "map (\<lambda> (b,j). simpnum (Add b (C j))) ?bjs"
-  let ?qd = "evaldjf (\<lambda> t. simpfm (subst0 t ?q)) (remdups ?sbjs)"
-  have qbf:"unit p = (?q,?B,?d)" by simp
-  from unit[OF qf qbf] have pq_ex: "(\<exists>(x::int). ?I x p) = (\<exists> (x::int). ?I x ?q)" and 
-    B:"?N ` set ?B = ?N ` set (\<beta> ?q)" and 
-    uq:"d\<beta> ?q 1" and dd: "d\<delta> ?q ?d" and dp: "?d > 0" and 
-    lq: "iszlfm ?q (real i#bs)" and 
-    Bn: "\<forall> b\<in> set ?B. numbound0 b" by auto
-  from zlin_qfree[OF lq] have qfq: "qfree ?q" .
-  from simpfm_qf[OF minusinf_qfree[OF qfq]] have qfmq: "qfree ?smq".
-  have jsnb: "\<forall> j \<in> set ?js. numbound0 (C j)" by simp
-  hence "\<forall> j\<in> set ?js. bound0 (subst0 (C j) ?smq)" 
-    by (auto simp only: subst0_bound0[OF qfmq])
-  hence th: "\<forall> j\<in> set ?js. bound0 (simpfm (subst0 (C j) ?smq))"
-    by (auto simp add: simpfm_bound0)
-  from evaldjf_bound0[OF th] have mdb: "bound0 ?md" by simp 
-  from Bn jsnb have "\<forall> (b,j) \<in> set ?bjs. numbound0 (Add b (C j))"
-    by simp
-  hence "\<forall> (b,j) \<in> set ?bjs. numbound0 (simpnum (Add b (C j)))"
-    using simpnum_numbound0 by blast
-  hence "\<forall> t \<in> set ?sbjs. numbound0 t" by simp
-  hence "\<forall> t \<in> set (remdups ?sbjs). bound0 (subst0 t ?q)"
-    using subst0_bound0[OF qfq] by auto 
-  hence th': "\<forall> t \<in> set (remdups ?sbjs). bound0 (simpfm (subst0 t ?q))"
-    using simpfm_bound0 by blast
-  from evaldjf_bound0 [OF th'] have qdb: "bound0 ?qd" by simp
-  from mdb qdb 
-  have mdqdb: "bound0 (disj ?md ?qd)" by (simp only: disj_def, cases "?md=T \<or> ?qd=T", simp_all)
-  from trans [OF pq_ex cp_thm'[OF lq uq dd dp]] B
-  have "?lhs = (\<exists> j\<in> {1.. ?d}. ?I j ?mq \<or> (\<exists> b\<in> ?N ` set ?B. Ifm ((b+ real j)#bs) ?q))" by auto
-  also have "\<dots> = ((\<exists> j\<in> set ?js. ?I j ?smq) \<or> (\<exists> (b,j) \<in> (?N ` set ?B \<times> set ?js). Ifm ((b+ real j)#bs) ?q))" apply (simp only: iupt_set simpfm) by auto
-  also have "\<dots>= ((\<exists> j\<in> set ?js. ?I j ?smq) \<or> (\<exists> t \<in> (\<lambda> (b,j). ?N (Add b (C j))) ` set ?bjs. Ifm (t #bs) ?q))" by simp
-  also have "\<dots>= ((\<exists> j\<in> set ?js. ?I j ?smq) \<or> (\<exists> t \<in> (\<lambda> (b,j). ?N (simpnum (Add b (C j)))) ` set ?bjs. Ifm (t #bs) ?q))" by (simp only: simpnum_ci)
-  also  have "\<dots>= ((\<exists> j\<in> set ?js. ?I j ?smq) \<or> (\<exists> t \<in> set ?sbjs. Ifm (?N t #bs) ?q))" 
-    by (auto simp add: split_def) 
-  also have "\<dots> = ((\<exists> j\<in> set ?js. (\<lambda> j. ?I i (simpfm (subst0 (C j) ?smq))) j) \<or> (\<exists> t \<in> set (remdups ?sbjs). (\<lambda> t. ?I i (simpfm (subst0 t ?q))) t))" by (simp only: simpfm subst0_I[OF qfq] simpfm Inum.simps subst0_I[OF qfmq] set_remdups)
-  also have "\<dots> = ((?I i (evaldjf (\<lambda> j. simpfm (subst0 (C j) ?smq)) ?js)) \<or> (?I i (evaldjf (\<lambda> t. simpfm (subst0 t ?q)) (remdups ?sbjs))))" by (simp only: evaldjf_ex)
-  finally have mdqd: "?lhs = (?I i (disj ?md ?qd))" by (simp add: disj) 
-  hence mdqd2: "?lhs = (Ifm bs (decr (disj ?md ?qd)))" using decr [OF mdqdb] by simp
-  {assume mdT: "?md = T"
-    hence cT:"cooper p = T" 
-      by (simp only: cooper_def unit_def split_def Let_def if_True) simp
-    from mdT mdqd have lhs:"?lhs" by (auto simp add: disj)
-    from mdT have "?rhs" by (simp add: cooper_def unit_def split_def)
-    with lhs cT have ?thesis by simp }
-  moreover
-  {assume mdT: "?md \<noteq> T" hence "cooper p = decr (disj ?md ?qd)" 
-      by (simp only: cooper_def unit_def split_def Let_def if_False) 
-    with mdqd2 decr_qf[OF mdqdb] have ?thesis by simp }
-  ultimately show ?thesis by blast
-qed
-
-lemma DJcooper: 
-  assumes qf: "qfree p"
-  shows "((\<exists> (x::int). Ifm (real x#bs) p) = (Ifm bs (DJ cooper p))) \<and> qfree (DJ cooper p)"
-proof-
-  from cooper have cqf: "\<forall> p. qfree p \<longrightarrow> qfree (cooper p)" by  blast
-  from DJ_qf[OF cqf] qf have thqf:"qfree (DJ cooper p)" by blast
-  have "Ifm bs (DJ cooper p) = (\<exists> q\<in> set (disjuncts p). Ifm bs (cooper q))" 
-     by (simp add: DJ_def evaldjf_ex)
-  also have "\<dots> = (\<exists> q \<in> set(disjuncts p). \<exists> (x::int). Ifm (real x#bs)  q)" 
-    using cooper disjuncts_qf[OF qf] by blast
-  also have "\<dots> = (\<exists> (x::int). Ifm (real x#bs) p)" by (induct p rule: disjuncts.induct, auto)
-  finally show ?thesis using thqf by blast
-qed
-
-    (* Redy and Loveland *)
-
-lemma \<sigma>\<rho>_cong: assumes lp: "iszlfm p (a#bs)" and tt': "Inum (a#bs) t = Inum (a#bs) t'"
-  shows "Ifm (a#bs) (\<sigma>\<rho> p (t,c)) = Ifm (a#bs) (\<sigma>\<rho> p (t',c))"
-  using lp 
-  by (induct p rule: iszlfm.induct, auto simp add: tt')
-
-lemma \<sigma>_cong: assumes lp: "iszlfm p (a#bs)" and tt': "Inum (a#bs) t = Inum (a#bs) t'"
-  shows "Ifm (a#bs) (\<sigma> p c t) = Ifm (a#bs) (\<sigma> p c t')"
-  by (simp add: \<sigma>_def tt' \<sigma>\<rho>_cong[OF lp tt'])
-
-lemma \<rho>_cong: assumes lp: "iszlfm p (a#bs)" 
-  and RR: "(\<lambda>(b,k). (Inum (a#bs) b,k)) ` R =  (\<lambda>(b,k). (Inum (a#bs) b,k)) ` set (\<rho> p)"
-  shows "(\<exists> (e,c) \<in> R. \<exists> j\<in> {1.. c*(\<delta> p)}. Ifm (a#bs) (\<sigma> p c (Add e (C j)))) = (\<exists> (e,c) \<in> set (\<rho> p). \<exists> j\<in> {1.. c*(\<delta> p)}. Ifm (a#bs) (\<sigma> p c (Add e (C j))))"
-  (is "?lhs = ?rhs")
-proof
-  let ?d = "\<delta> p"
-  assume ?lhs then obtain e c j where ecR: "(e,c) \<in> R" and jD:"j \<in> {1 .. c*?d}" 
-    and px: "Ifm (a#bs) (\<sigma> p c (Add e (C j)))" (is "?sp c e j") by blast
-  from ecR have "(Inum (a#bs) e,c) \<in> (\<lambda>(b,k). (Inum (a#bs) b,k)) ` R" by auto
-  hence "(Inum (a#bs) e,c) \<in> (\<lambda>(b,k). (Inum (a#bs) b,k)) ` set (\<rho> p)" using RR by simp
-  hence "\<exists> (e',c') \<in> set (\<rho> p). Inum (a#bs) e = Inum (a#bs) e' \<and> c = c'" by auto
-  then obtain e' c' where ecRo:"(e',c') \<in> set (\<rho> p)" and ee':"Inum (a#bs) e = Inum (a#bs) e'"
-    and cc':"c = c'" by blast
-  from ee' have tt': "Inum (a#bs) (Add e (C j)) = Inum (a#bs) (Add e' (C j))" by simp
-  
-  from \<sigma>_cong[OF lp tt', where c="c"] px have px':"?sp c e' j" by simp
-  from ecRo jD px' cc'  show ?rhs apply auto 
-    by (rule_tac x="(e', c')" in bexI,simp_all)
-  (rule_tac x="j" in bexI, simp_all add: cc'[symmetric])
-next
-  let ?d = "\<delta> p"
-  assume ?rhs then obtain e c j where ecR: "(e,c) \<in> set (\<rho> p)" and jD:"j \<in> {1 .. c*?d}" 
-    and px: "Ifm (a#bs) (\<sigma> p c (Add e (C j)))" (is "?sp c e j") by blast
-  from ecR have "(Inum (a#bs) e,c) \<in> (\<lambda>(b,k). (Inum (a#bs) b,k)) ` set (\<rho> p)" by auto
-  hence "(Inum (a#bs) e,c) \<in> (\<lambda>(b,k). (Inum (a#bs) b,k)) ` R" using RR by simp
-  hence "\<exists> (e',c') \<in> R. Inum (a#bs) e = Inum (a#bs) e' \<and> c = c'" by auto
-  then obtain e' c' where ecRo:"(e',c') \<in> R" and ee':"Inum (a#bs) e = Inum (a#bs) e'"
-    and cc':"c = c'" by blast
-  from ee' have tt': "Inum (a#bs) (Add e (C j)) = Inum (a#bs) (Add e' (C j))" by simp
-  from \<sigma>_cong[OF lp tt', where c="c"] px have px':"?sp c e' j" by simp
-  from ecRo jD px' cc'  show ?lhs apply auto 
-    by (rule_tac x="(e', c')" in bexI,simp_all)
-  (rule_tac x="j" in bexI, simp_all add: cc'[symmetric])
-qed
-
-lemma rl_thm': 
-  assumes lp: "iszlfm p (real (i::int)#bs)" 
-  and R: "(\<lambda>(b,k). (Inum (a#bs) b,k)) ` R =  (\<lambda>(b,k). (Inum (a#bs) b,k)) ` set (\<rho> p)"
-  shows "(\<exists> (x::int). Ifm (real x#bs) p) = ((\<exists> j\<in> {1 .. \<delta> p}. Ifm (real j#bs) (minusinf p)) \<or> (\<exists> (e,c) \<in> R. \<exists> j\<in> {1.. c*(\<delta> p)}. Ifm (a#bs) (\<sigma> p c (Add e (C j)))))"
-  using rl_thm[OF lp] \<rho>_cong[OF iszlfm_gen[OF lp, rule_format, where y="a"] R] by simp 
-
-constdefs chooset:: "fm \<Rightarrow> fm \<times> ((num\<times>int) list) \<times> int"
-  "chooset p \<equiv> (let q = zlfm p ; d = \<delta> q;
-             B = remdups (map (\<lambda> (t,k). (simpnum t,k)) (\<rho> q)) ; 
-             a = remdups (map (\<lambda> (t,k). (simpnum t,k)) (\<alpha>\<rho> q))
-             in if length B \<le> length a then (q,B,d) else (mirror q, a,d))"
-
-lemma chooset: assumes qf: "qfree p"
-  shows "\<And> q B d. chooset p = (q,B,d) \<Longrightarrow> ((\<exists> (x::int). Ifm (real x#bs) p) = (\<exists> (x::int). Ifm (real x#bs) q)) \<and> ((\<lambda>(t,k). (Inum (real i#bs) t,k)) ` set B = (\<lambda>(t,k). (Inum (real i#bs) t,k)) ` set (\<rho> q)) \<and> (\<delta> q = d) \<and> d >0 \<and> iszlfm q (real (i::int)#bs) \<and> (\<forall> (e,c)\<in> set B. numbound0 e \<and> c>0)"
-proof-
-  fix q B d 
-  assume qBd: "chooset p = (q,B,d)"
-  let ?thes = "((\<exists> (x::int). Ifm (real x#bs) p) = (\<exists> (x::int). Ifm (real x#bs) q)) \<and> ((\<lambda>(t,k). (Inum (real i#bs) t,k)) ` set B = (\<lambda>(t,k). (Inum (real i#bs) t,k)) ` set (\<rho> q)) \<and> (\<delta> q = d) \<and> d >0 \<and> iszlfm q (real (i::int)#bs) \<and> (\<forall> (e,c)\<in> set B. numbound0 e \<and> c>0)" 
-  let ?I = "\<lambda> (x::int) p. Ifm (real x#bs) p"
-  let ?q = "zlfm p"
-  let ?d = "\<delta> ?q"
-  let ?B = "set (\<rho> ?q)"
-  let ?f = "\<lambda> (t,k). (simpnum t,k)"
-  let ?B'= "remdups (map ?f (\<rho> ?q))"
-  let ?A = "set (\<alpha>\<rho> ?q)"
-  let ?A'= "remdups (map ?f (\<alpha>\<rho> ?q))"
-  from conjunct1[OF zlfm_I[OF qf, where bs="bs"]] 
-  have pp': "\<forall> i. ?I i ?q = ?I i p" by auto
-  hence pq_ex:"(\<exists> (x::int). ?I x p) = (\<exists> x. ?I x ?q)" by simp 
-  from iszlfm_gen[OF conjunct2[OF zlfm_I[OF qf, where bs="bs" and i="i"]], rule_format, where y="real i"]
-  have lq: "iszlfm ?q (real (i::int)#bs)" . 
-  from \<delta>[OF lq] have dp:"?d >0" by blast
-  let ?N = "\<lambda> (t,c). (Inum (real (i::int)#bs) t,c)"
-  have "?N ` set ?B' = ((?N o ?f) ` ?B)" by (simp add: split_def image_compose)
-  also have "\<dots> = ?N ` ?B"
-    by(simp add: split_def image_compose simpnum_ci[where bs="real i #bs"] image_def)
-  finally have BB': "?N ` set ?B' = ?N ` ?B" .
-  have "?N ` set ?A' = ((?N o ?f) ` ?A)" by (simp add: split_def image_compose) 
-  also have "\<dots> = ?N ` ?A" using simpnum_ci[where bs="real i #bs"]
-    by(simp add: split_def image_compose simpnum_ci[where bs="real i #bs"] image_def) 
-  finally have AA': "?N ` set ?A' = ?N ` ?A" .
-  from \<rho>_l[OF lq] have B_nb:"\<forall> (e,c)\<in> set ?B'. numbound0 e \<and> c > 0"
-    by (simp add: simpnum_numbound0 split_def)
-  from \<alpha>\<rho>_l[OF lq] have A_nb: "\<forall> (e,c)\<in> set ?A'. numbound0 e \<and> c > 0"
-    by (simp add: simpnum_numbound0 split_def)
-    {assume "length ?B' \<le> length ?A'"
-    hence q:"q=?q" and "B = ?B'" and d:"d = ?d"
-      using qBd by (auto simp add: Let_def chooset_def)
-    with BB' B_nb have b: "?N ` (set B) = ?N ` set (\<rho> q)" 
-      and bn: "\<forall>(e,c)\<in> set B. numbound0 e \<and> c > 0" by auto
-  with pq_ex dp lq q d have ?thes by simp}
-  moreover 
-  {assume "\<not> (length ?B' \<le> length ?A')"
-    hence q:"q=mirror ?q" and "B = ?A'" and d:"d = ?d"
-      using qBd by (auto simp add: Let_def chooset_def)
-    with AA' mirror_\<alpha>\<rho>[OF lq] A_nb have b:"?N ` (set B) = ?N ` set (\<rho> q)" 
-      and bn: "\<forall>(e,c)\<in> set B. numbound0 e \<and> c > 0" by auto 
-    from mirror_ex[OF lq] pq_ex q 
-    have pqm_eq:"(\<exists> (x::int). ?I x p) = (\<exists> (x::int). ?I x q)" by simp
-    from lq q mirror_l [where p="?q" and bs="bs" and a="real i"]
-    have lq': "iszlfm q (real i#bs)" by auto
-    from mirror_\<delta>[OF lq] pqm_eq b bn lq' dp q dp d have ?thes by simp
-  }
-  ultimately show ?thes by blast
-qed
-
-constdefs stage:: "fm \<Rightarrow> int \<Rightarrow> (num \<times> int) \<Rightarrow> fm"
-  "stage p d \<equiv> (\<lambda> (e,c). evaldjf (\<lambda> j. simpfm (\<sigma> p c (Add e (C j)))) (iupt (1,c*d)))"
-lemma stage:
-  shows "Ifm bs (stage p d (e,c)) = (\<exists> j\<in>{1 .. c*d}. Ifm bs (\<sigma> p c (Add e (C j))))"
-  by (unfold stage_def split_def ,simp only: evaldjf_ex iupt_set simpfm) simp
-
-lemma stage_nb: assumes lp: "iszlfm p (a#bs)" and cp: "c >0" and nb:"numbound0 e"
-  shows "bound0 (stage p d (e,c))"
-proof-
-  let ?f = "\<lambda> j. simpfm (\<sigma> p c (Add e (C j)))"
-  have th: "\<forall> j\<in> set (iupt(1,c*d)). bound0 (?f j)"
-  proof
-    fix j
-    from nb have nb':"numbound0 (Add e (C j))" by simp
-    from simpfm_bound0[OF \<sigma>_nb[OF lp nb', where k="c"]]
-    show "bound0 (simpfm (\<sigma> p c (Add e (C j))))" .
-  qed
-  from evaldjf_bound0[OF th] show ?thesis by (unfold stage_def split_def) simp
-qed
-
-constdefs redlove:: "fm \<Rightarrow> fm"
-  "redlove p \<equiv> 
-  (let (q,B,d) = chooset p;
-       mq = simpfm (minusinf q);
-       md = evaldjf (\<lambda> j. simpfm (subst0 (C j) mq)) (iupt (1,d))
-   in if md = T then T else
-    (let qd = evaldjf (stage q d) B
-     in decr (disj md qd)))"
-
-lemma redlove: assumes qf: "qfree p"
-  shows "((\<exists> (x::int). Ifm (real x#bs) p) = (Ifm bs (redlove p))) \<and> qfree (redlove p)" 
-  (is "(?lhs = ?rhs) \<and> _")
-proof-
-
-  let ?I = "\<lambda> (x::int) p. Ifm (real x#bs) p"
-  let ?q = "fst (chooset p)"
-  let ?B = "fst (snd(chooset p))"
-  let ?d = "snd (snd (chooset p))"
-  let ?js = "iupt (1,?d)"
-  let ?mq = "minusinf ?q"
-  let ?smq = "simpfm ?mq"
-  let ?md = "evaldjf (\<lambda> j. simpfm (subst0 (C j) ?smq)) ?js"
-  fix i
-  let ?N = "\<lambda> (t,k). (Inum (real (i::int)#bs) t,k)"
-  let ?qd = "evaldjf (stage ?q ?d) ?B"
-  have qbf:"chooset p = (?q,?B,?d)" by simp
-  from chooset[OF qf qbf] have pq_ex: "(\<exists>(x::int). ?I x p) = (\<exists> (x::int). ?I x ?q)" and 
-    B:"?N ` set ?B = ?N ` set (\<rho> ?q)" and dd: "\<delta> ?q = ?d" and dp: "?d > 0" and 
-    lq: "iszlfm ?q (real i#bs)" and 
-    Bn: "\<forall> (e,c)\<in> set ?B. numbound0 e \<and> c > 0" by auto
-  from zlin_qfree[OF lq] have qfq: "qfree ?q" .
-  from simpfm_qf[OF minusinf_qfree[OF qfq]] have qfmq: "qfree ?smq".
-  have jsnb: "\<forall> j \<in> set ?js. numbound0 (C j)" by simp
-  hence "\<forall> j\<in> set ?js. bound0 (subst0 (C j) ?smq)" 
-    by (auto simp only: subst0_bound0[OF qfmq])
-  hence th: "\<forall> j\<in> set ?js. bound0 (simpfm (subst0 (C j) ?smq))"
-    by (auto simp add: simpfm_bound0)
-  from evaldjf_bound0[OF th] have mdb: "bound0 ?md" by simp 
-  from Bn stage_nb[OF lq] have th:"\<forall> x \<in> set ?B. bound0 (stage ?q ?d x)" by auto
-  from evaldjf_bound0[OF th]  have qdb: "bound0 ?qd" .
-  from mdb qdb 
-  have mdqdb: "bound0 (disj ?md ?qd)" by (simp only: disj_def, cases "?md=T \<or> ?qd=T", simp_all)
-  from trans [OF pq_ex rl_thm'[OF lq B]] dd
-  have "?lhs = ((\<exists> j\<in> {1.. ?d}. ?I j ?mq) \<or> (\<exists> (e,c)\<in> set ?B. \<exists> j\<in> {1 .. c*?d}. Ifm (real i#bs) (\<sigma> ?q c (Add e (C j)))))" by auto
-  also have "\<dots> = ((\<exists> j\<in> {1.. ?d}. ?I j ?smq) \<or> (\<exists> (e,c)\<in> set ?B. ?I i (stage ?q ?d (e,c) )))" 
-    by (simp add: simpfm stage split_def)
-  also have "\<dots> = ((\<exists> j\<in> {1 .. ?d}. ?I i (subst0 (C j) ?smq))  \<or> ?I i ?qd)"
-    by (simp add: evaldjf_ex subst0_I[OF qfmq])
-  finally have mdqd:"?lhs = (?I i ?md \<or> ?I i ?qd)" by (simp only: evaldjf_ex iupt_set simpfm) 
-  also have "\<dots> = (?I i (disj ?md ?qd))" by (simp add: disj)
-  also have "\<dots> = (Ifm bs (decr (disj ?md ?qd)))" by (simp only: decr [OF mdqdb]) 
-  finally have mdqd2: "?lhs = (Ifm bs (decr (disj ?md ?qd)))" . 
-  {assume mdT: "?md = T"
-    hence cT:"redlove p = T" by (simp add: redlove_def Let_def chooset_def split_def)
-    from mdT have lhs:"?lhs" using mdqd by simp 
-    from mdT have "?rhs" by (simp add: redlove_def chooset_def split_def)
-    with lhs cT have ?thesis by simp }
-  moreover
-  {assume mdT: "?md \<noteq> T" hence "redlove p = decr (disj ?md ?qd)" 
-      by (simp add: redlove_def chooset_def split_def Let_def)
-    with mdqd2 decr_qf[OF mdqdb] have ?thesis by simp }
-  ultimately show ?thesis by blast
-qed
-
-lemma DJredlove: 
-  assumes qf: "qfree p"
-  shows "((\<exists> (x::int). Ifm (real x#bs) p) = (Ifm bs (DJ redlove p))) \<and> qfree (DJ redlove p)"
-proof-
-  from redlove have cqf: "\<forall> p. qfree p \<longrightarrow> qfree (redlove p)" by  blast
-  from DJ_qf[OF cqf] qf have thqf:"qfree (DJ redlove p)" by blast
-  have "Ifm bs (DJ redlove p) = (\<exists> q\<in> set (disjuncts p). Ifm bs (redlove q))" 
-     by (simp add: DJ_def evaldjf_ex)
-  also have "\<dots> = (\<exists> q \<in> set(disjuncts p). \<exists> (x::int). Ifm (real x#bs)  q)" 
-    using redlove disjuncts_qf[OF qf] by blast
-  also have "\<dots> = (\<exists> (x::int). Ifm (real x#bs) p)" by (induct p rule: disjuncts.induct, auto)
-  finally show ?thesis using thqf by blast
-qed
-
-
-lemma exsplit_qf: assumes qf: "qfree p"
-  shows "qfree (exsplit p)"
-using qf by (induct p rule: exsplit.induct, auto)
-
-definition mircfr :: "fm \<Rightarrow> fm" where
-  "mircfr = DJ cooper o ferrack01 o simpfm o exsplit"
-
-definition mirlfr :: "fm \<Rightarrow> fm" where
-  "mirlfr = DJ redlove o ferrack01 o simpfm o exsplit"
-
-lemma mircfr: "\<forall> bs p. qfree p \<longrightarrow> qfree (mircfr p) \<and> Ifm bs (mircfr p) = Ifm bs (E p)"
-proof(clarsimp simp del: Ifm.simps)
-  fix bs p
-  assume qf: "qfree p"
-  show "qfree (mircfr p)\<and>(Ifm bs (mircfr p) = Ifm bs (E p))" (is "_ \<and> (?lhs = ?rhs)")
-  proof-
-    let ?es = "(And (And (Ge (CN 0 1 (C 0))) (Lt (CN 0 1 (C (- 1))))) (simpfm (exsplit p)))"
-    have "?rhs = (\<exists> (i::int). \<exists> x. Ifm (x#real i#bs) ?es)" 
-      using splitex[OF qf] by simp
-    with ferrack01[OF simpfm_qf[OF exsplit_qf[OF qf]]] have th1: "?rhs = (\<exists> (i::int). Ifm (real i#bs) (ferrack01 (simpfm (exsplit p))))" and qf':"qfree (ferrack01 (simpfm (exsplit p)))" by simp+
-    with DJcooper[OF qf'] show ?thesis by (simp add: mircfr_def)
-  qed
-qed
-  
-lemma mirlfr: "\<forall> bs p. qfree p \<longrightarrow> qfree(mirlfr p) \<and> Ifm bs (mirlfr p) = Ifm bs (E p)"
-proof(clarsimp simp del: Ifm.simps)
-  fix bs p
-  assume qf: "qfree p"
-  show "qfree (mirlfr p)\<and>(Ifm bs (mirlfr p) = Ifm bs (E p))" (is "_ \<and> (?lhs = ?rhs)")
-  proof-
-    let ?es = "(And (And (Ge (CN 0 1 (C 0))) (Lt (CN 0 1 (C (- 1))))) (simpfm (exsplit p)))"
-    have "?rhs = (\<exists> (i::int). \<exists> x. Ifm (x#real i#bs) ?es)" 
-      using splitex[OF qf] by simp
-    with ferrack01[OF simpfm_qf[OF exsplit_qf[OF qf]]] have th1: "?rhs = (\<exists> (i::int). Ifm (real i#bs) (ferrack01 (simpfm (exsplit p))))" and qf':"qfree (ferrack01 (simpfm (exsplit p)))" by simp+
-    with DJredlove[OF qf'] show ?thesis by (simp add: mirlfr_def)
-  qed
-qed
-  
-definition mircfrqe:: "fm \<Rightarrow> fm" where
-  "mircfrqe p = qelim (prep p) mircfr"
-
-definition mirlfrqe:: "fm \<Rightarrow> fm" where
-  "mirlfrqe p = qelim (prep p) mirlfr"
-
-theorem mircfrqe: "(Ifm bs (mircfrqe p) = Ifm bs p) \<and> qfree (mircfrqe p)"
-  using qelim_ci[OF mircfr] prep by (auto simp add: mircfrqe_def)
-
-theorem mirlfrqe: "(Ifm bs (mirlfrqe p) = Ifm bs p) \<and> qfree (mirlfrqe p)"
-  using qelim_ci[OF mirlfr] prep by (auto simp add: mirlfrqe_def)
-
-definition
-  "test1 (u\<Colon>unit) = mircfrqe (A (And (Le (Sub (Floor (Bound 0)) (Bound 0))) (Le (Add (Bound 0) (Floor (Neg (Bound 0)))))))"
-
-definition
-  "test2 (u\<Colon>unit) = mircfrqe (A (Iff (Eq (Add (Floor (Bound 0)) (Floor (Neg (Bound 0))))) (Eq (Sub (Floor (Bound 0)) (Bound 0)))))"
-
-definition
-  "test3 (u\<Colon>unit) = mirlfrqe (A (And (Le (Sub (Floor (Bound 0)) (Bound 0))) (Le (Add (Bound 0) (Floor (Neg (Bound 0)))))))"
-
-definition
-  "test4 (u\<Colon>unit) = mirlfrqe (A (Iff (Eq (Add (Floor (Bound 0)) (Floor (Neg (Bound 0))))) (Eq (Sub (Floor (Bound 0)) (Bound 0)))))"
-
-definition
-  "test5 (u\<Colon>unit) = mircfrqe (A(E(And (Ge(Sub (Bound 1) (Bound 0))) (Eq (Add (Floor (Bound 1)) (Floor (Neg(Bound 0))))))))"
-
-ML {* @{code test1} () *}
-ML {* @{code test2} () *}
-ML {* @{code test3} () *}
-ML {* @{code test4} () *}
-ML {* @{code test5} () *}
-
-(*export_code mircfrqe mirlfrqe
-  in SML module_name Mir file "raw_mir.ML"*)
-
-oracle mirfr_oracle = {* fn (proofs, ct) =>
-let
-
-fun num_of_term vs (t as Free (xn, xT)) = (case AList.lookup (op =) vs t
-     of NONE => error "Variable not found in the list!"
-      | SOME n => @{code Bound} n)
-  | num_of_term vs @{term "real (0::int)"} = @{code C} 0
-  | num_of_term vs @{term "real (1::int)"} = @{code C} 1
-  | num_of_term vs @{term "0::real"} = @{code C} 0
-  | num_of_term vs @{term "1::real"} = @{code C} 1
-  | num_of_term vs (Bound i) = @{code Bound} i
-  | num_of_term vs (@{term "uminus :: real \<Rightarrow> real"} $ t') = @{code Neg} (num_of_term vs t')
-  | num_of_term vs (@{term "op + :: real \<Rightarrow> real \<Rightarrow> real"} $ t1 $ t2) =
-      @{code Add} (num_of_term vs t1, num_of_term vs t2)
-  | num_of_term vs (@{term "op - :: real \<Rightarrow> real \<Rightarrow> real"} $ t1 $ t2) =
-      @{code Sub} (num_of_term vs t1, num_of_term vs t2)
-  | num_of_term vs (@{term "op * :: real \<Rightarrow> real \<Rightarrow> real"} $ t1 $ t2) =
-      (case (num_of_term vs t1)
-       of @{code C} i => @{code Mul} (i, num_of_term vs t2)
-        | _ => error "num_of_term: unsupported Multiplication")
-  | num_of_term vs (@{term "real :: int \<Rightarrow> real"} $ (@{term "number_of :: int \<Rightarrow> int"} $ t')) =
-      @{code C} (HOLogic.dest_numeral t')
-  | num_of_term vs (@{term "real :: int \<Rightarrow> real"} $ (@{term "floor :: real \<Rightarrow> int"} $ t')) =
-      @{code Floor} (num_of_term vs t')
-  | num_of_term vs (@{term "real :: int \<Rightarrow> real"} $ (@{term "ceiling :: real \<Rightarrow> int"} $ t')) =
-      @{code Neg} (@{code Floor} (@{code Neg} (num_of_term vs t')))
-  | num_of_term vs (@{term "number_of :: int \<Rightarrow> real"} $ t') =
-      @{code C} (HOLogic.dest_numeral t')
-  | num_of_term vs t = error ("num_of_term: unknown term " ^ Syntax.string_of_term @{context} t);
-
-fun fm_of_term vs @{term True} = @{code T}
-  | fm_of_term vs @{term False} = @{code F}
-  | fm_of_term vs (@{term "op < :: real \<Rightarrow> real \<Rightarrow> bool"} $ t1 $ t2) =
-      @{code Lt} (@{code Sub} (num_of_term vs t1, num_of_term vs t2))
-  | fm_of_term vs (@{term "op \<le> :: real \<Rightarrow> real \<Rightarrow> bool"} $ t1 $ t2) =
-      @{code Le} (@{code Sub} (num_of_term vs t1, num_of_term vs t2))
-  | fm_of_term vs (@{term "op = :: real \<Rightarrow> real \<Rightarrow> bool"} $ t1 $ t2) =
-      @{code Eq} (@{code Sub} (num_of_term vs t1, num_of_term vs t2)) 
-  | fm_of_term vs (@{term "op rdvd"} $ (@{term "real :: int \<Rightarrow> real"} $ (@{term "number_of :: int \<Rightarrow> int"} $ t1)) $ t2) =
-      @{code Dvd} (HOLogic.dest_numeral t1, num_of_term vs t2)
-  | fm_of_term vs (@{term "op = :: bool \<Rightarrow> bool \<Rightarrow> bool"} $ t1 $ t2) =
-      @{code Iff} (fm_of_term vs t1, fm_of_term vs t2)
-  | fm_of_term vs (@{term "op &"} $ t1 $ t2) =
-      @{code And} (fm_of_term vs t1, fm_of_term vs t2)
-  | fm_of_term vs (@{term "op |"} $ t1 $ t2) =
-      @{code Or} (fm_of_term vs t1, fm_of_term vs t2)
-  | fm_of_term vs (@{term "op -->"} $ t1 $ t2) =
-      @{code Imp} (fm_of_term vs t1, fm_of_term vs t2)
-  | fm_of_term vs (@{term "Not"} $ t') =
-      @{code NOT} (fm_of_term vs t')
-  | fm_of_term vs (Const ("Ex", _) $ Abs (xn, xT, p)) =
-      @{code E} (fm_of_term (map (fn (v, n) => (v, n + 1)) vs) p)
-  | fm_of_term vs (Const ("All", _) $ Abs (xn, xT, p)) =
-      @{code A} (fm_of_term (map (fn (v, n) => (v, n + 1)) vs) p)
-  | fm_of_term vs t = error ("fm_of_term : unknown term " ^ Syntax.string_of_term @{context} t);
-
-fun term_of_num vs (@{code C} i) = @{term "real :: int \<Rightarrow> real"} $ HOLogic.mk_number HOLogic.intT i
-  | term_of_num vs (@{code Bound} n) = fst (the (find_first (fn (_, m) => n = m) vs))
-  | term_of_num vs (@{code Neg} (@{code Floor} (@{code Neg} t'))) =
-      @{term "real :: int \<Rightarrow> real"} $ (@{term "ceiling :: real \<Rightarrow> int"} $ term_of_num vs t')
-  | term_of_num vs (@{code Neg} t') = @{term "uminus :: real \<Rightarrow> real"} $ term_of_num vs t'
-  | term_of_num vs (@{code Add} (t1, t2)) = @{term "op + :: real \<Rightarrow> real \<Rightarrow> real"} $
-      term_of_num vs t1 $ term_of_num vs t2
-  | term_of_num vs (@{code Sub} (t1, t2)) = @{term "op - :: real \<Rightarrow> real \<Rightarrow> real"} $
-      term_of_num vs t1 $ term_of_num vs t2
-  | term_of_num vs (@{code Mul} (i, t2)) = @{term "op * :: real \<Rightarrow> real \<Rightarrow> real"} $
-      term_of_num vs (@{code C} i) $ term_of_num vs t2
-  | term_of_num vs (@{code Floor} t) = @{term "real :: int \<Rightarrow> real"} $ (@{term "floor :: real \<Rightarrow> int"} $ term_of_num vs t)
-  | term_of_num vs (@{code CN} (n, i, t)) = term_of_num vs (@{code Add} (@{code Mul} (i, @{code Bound} n), t))
-  | term_of_num vs (@{code CF} (c, t, s)) = term_of_num vs (@{code Add} (@{code Mul} (c, @{code Floor} t), s));
-
-fun term_of_fm vs @{code T} = HOLogic.true_const 
-  | term_of_fm vs @{code F} = HOLogic.false_const
-  | term_of_fm vs (@{code Lt} t) =
-      @{term "op < :: real \<Rightarrow> real \<Rightarrow> bool"} $ term_of_num vs t $ @{term "0::real"}
-  | term_of_fm vs (@{code Le} t) =
-      @{term "op \<le> :: real \<Rightarrow> real \<Rightarrow> bool"} $ term_of_num vs t $ @{term "0::real"}
-  | term_of_fm vs (@{code Gt} t) =
-      @{term "op < :: real \<Rightarrow> real \<Rightarrow> bool"} $ @{term "0::real"} $ term_of_num vs t
-  | term_of_fm vs (@{code Ge} t) =
-      @{term "op \<le> :: real \<Rightarrow> real \<Rightarrow> bool"} $ @{term "0::real"} $ term_of_num vs t
-  | term_of_fm vs (@{code Eq} t) =
-      @{term "op = :: real \<Rightarrow> real \<Rightarrow> bool"} $ term_of_num vs t $ @{term "0::real"}
-  | term_of_fm vs (@{code NEq} t) =
-      term_of_fm vs (@{code NOT} (@{code Eq} t))
-  | term_of_fm vs (@{code Dvd} (i, t)) =
-      @{term "op rdvd"} $ term_of_num vs (@{code C} i) $ term_of_num vs t
-  | term_of_fm vs (@{code NDvd} (i, t)) =
-      term_of_fm vs (@{code NOT} (@{code Dvd} (i, t)))
-  | term_of_fm vs (@{code NOT} t') =
-      HOLogic.Not $ term_of_fm vs t'
-  | term_of_fm vs (@{code And} (t1, t2)) =
-      HOLogic.conj $ term_of_fm vs t1 $ term_of_fm vs t2
-  | term_of_fm vs (@{code Or} (t1, t2)) =
-      HOLogic.disj $ term_of_fm vs t1 $ term_of_fm vs t2
-  | term_of_fm vs (@{code Imp}  (t1, t2)) =
-      HOLogic.imp $ term_of_fm vs t1 $ term_of_fm vs t2
-  | term_of_fm vs (@{code Iff} (t1, t2)) =
-      @{term "op = :: bool \<Rightarrow> bool \<Rightarrow> bool"} $ term_of_fm vs t1 $ term_of_fm vs t2;
-
-in
-  let 
-    val thy = Thm.theory_of_cterm ct;
-    val t = Thm.term_of ct;
-    val fs = OldTerm.term_frees t;
-    val vs = fs ~~ (0 upto (length fs - 1));
-    val qe = if proofs then @{code mirlfrqe} else @{code mircfrqe};
-    val t' = (term_of_fm vs o qe o fm_of_term vs) t;
-  in (cterm_of thy o HOLogic.mk_Trueprop o HOLogic.mk_eq) (t, t') end
-end;
-*}
-
-use "mir_tac.ML"
-setup "Mir_Tac.setup"
-
-lemma "ALL (x::real). (\<lfloor>x\<rfloor> = \<lceil>x\<rceil> = (x = real \<lfloor>x\<rfloor>))"
-apply mir
-done
-
-lemma "ALL (x::real). real (2::int)*x - (real (1::int)) < real \<lfloor>x\<rfloor> + real \<lceil>x\<rceil> \<and> real \<lfloor>x\<rfloor> + real \<lceil>x\<rceil>  \<le> real (2::int)*x + (real (1::int))"
-apply mir
-done
-
-lemma "ALL (x::real). 2*\<lfloor>x\<rfloor> \<le> \<lfloor>2*x\<rfloor> \<and> \<lfloor>2*x\<rfloor> \<le> 2*\<lfloor>x+1\<rfloor>"
-apply mir 
-done
-
-lemma "ALL (x::real). \<exists>y \<le> x. (\<lfloor>x\<rfloor> = \<lceil>y\<rceil>)"
-apply mir
-done
-
-lemma "ALL x y. \<lfloor>x\<rfloor> = \<lfloor>y\<rfloor> \<longrightarrow> 0 \<le> abs (y - x) \<and> abs (y - x) \<le> 1"
-apply mir
-done
-
-end
--- a/src/HOL/Reflection/ROOT.ML	Mon Feb 16 10:13:30 2009 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,2 +0,0 @@
-
-use_thys ["Cooper", "Ferrack", "MIR", "Approximation"];
--- a/src/HOL/Reflection/cooper_tac.ML	Mon Feb 16 10:13:30 2009 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,139 +0,0 @@
-(*  Title:      HOL/Reflection/cooper_tac.ML
-    Author:     Amine Chaieb, TU Muenchen
-*)
-
-structure Cooper_Tac =
-struct
-
-val trace = ref false;
-fun trace_msg s = if !trace then tracing s else ();
-
-val cooper_ss = @{simpset};
-
-val nT = HOLogic.natT;
-val binarith = @{thms normalize_bin_simps};
-val comp_arith = binarith @ simp_thms
-
-val zdvd_int = @{thm zdvd_int};
-val zdiff_int_split = @{thm zdiff_int_split};
-val all_nat = @{thm all_nat};
-val ex_nat = @{thm ex_nat};
-val number_of1 = @{thm number_of1};
-val number_of2 = @{thm number_of2};
-val split_zdiv = @{thm split_zdiv};
-val split_zmod = @{thm split_zmod};
-val mod_div_equality' = @{thm mod_div_equality'};
-val split_div' = @{thm split_div'};
-val Suc_plus1 = @{thm Suc_plus1};
-val imp_le_cong = @{thm imp_le_cong};
-val conj_le_cong = @{thm conj_le_cong};
-val nat_mod_add_eq = @{thm mod_add1_eq} RS sym;
-val nat_mod_add_left_eq = @{thm mod_add_left_eq} RS sym;
-val nat_mod_add_right_eq = @{thm mod_add_right_eq} RS sym;
-val int_mod_add_eq = @{thm zmod_zadd1_eq} RS sym;
-val int_mod_add_left_eq = @{thm zmod_zadd_left_eq} RS sym;
-val int_mod_add_right_eq = @{thm zmod_zadd_right_eq} RS sym;
-val nat_div_add_eq = @{thm div_add1_eq} RS sym;
-val int_div_add_eq = @{thm zdiv_zadd1_eq} RS sym;
-
-fun prepare_for_linz q fm = 
-  let
-    val ps = Logic.strip_params fm
-    val hs = map HOLogic.dest_Trueprop (Logic.strip_assums_hyp fm)
-    val c = HOLogic.dest_Trueprop (Logic.strip_assums_concl fm)
-    fun mk_all ((s, T), (P,n)) =
-      if 0 mem loose_bnos P then
-        (HOLogic.all_const T $ Abs (s, T, P), n)
-      else (incr_boundvars ~1 P, n-1)
-    fun mk_all2 (v, t) = HOLogic.all_const (fastype_of v) $ lambda v t;
-    val rhs = hs
-    val np = length ps
-    val (fm',np) =  foldr (fn ((x, T), (fm,n)) => mk_all ((x, T), (fm,n)))
-      (foldr HOLogic.mk_imp c rhs, np) ps
-    val (vs, _) = List.partition (fn t => q orelse (type_of t) = nT)
-      (OldTerm.term_frees fm' @ OldTerm.term_vars fm');
-    val fm2 = foldr mk_all2 fm' vs
-  in (fm2, np + length vs, length rhs) end;
-
-(*Object quantifier to meta --*)
-fun spec_step n th = if (n=0) then th else (spec_step (n-1) th) RS spec ;
-
-(* object implication to meta---*)
-fun mp_step n th = if (n=0) then th else (mp_step (n-1) th) RS mp;
-
-
-fun linz_tac ctxt q i = ObjectLogic.atomize_prems_tac i THEN (fn st =>
-  let
-    val g = List.nth (prems_of st, i - 1)
-    val thy = ProofContext.theory_of ctxt
-    (* Transform the term*)
-    val (t,np,nh) = prepare_for_linz q g
-    (* Some simpsets for dealing with mod div abs and nat*)
-    val mod_div_simpset = HOL_basic_ss 
-			addsimps [refl,nat_mod_add_eq, nat_mod_add_left_eq, 
-				  nat_mod_add_right_eq, int_mod_add_eq, 
-				  int_mod_add_right_eq, int_mod_add_left_eq,
-				  nat_div_add_eq, int_div_add_eq,
-				  @{thm mod_self}, @{thm "zmod_self"},
-				  @{thm mod_by_0}, @{thm div_by_0},
-				  @{thm "zdiv_zero"}, @{thm "zmod_zero"}, @{thm "div_0"}, @{thm "mod_0"},
-				  @{thm "zdiv_1"}, @{thm "zmod_1"}, @{thm "div_1"}, @{thm "mod_1"},
-				  Suc_plus1]
-			addsimps @{thms add_ac}
-			addsimprocs [cancel_div_mod_proc]
-    val simpset0 = HOL_basic_ss
-      addsimps [mod_div_equality', Suc_plus1]
-      addsimps comp_arith
-      addsplits [split_zdiv, split_zmod, split_div', @{thm "split_min"}, @{thm "split_max"}]
-    (* Simp rules for changing (n::int) to int n *)
-    val simpset1 = HOL_basic_ss
-      addsimps [nat_number_of_def, zdvd_int] @ map (fn r => r RS sym)
-        [@{thm int_int_eq}, @{thm zle_int}, @{thm zless_int}, @{thm zadd_int}, @{thm zmult_int}]
-      addsplits [zdiff_int_split]
-    (*simp rules for elimination of int n*)
-
-    val simpset2 = HOL_basic_ss
-      addsimps [@{thm nat_0_le}, @{thm all_nat}, @{thm ex_nat}, @{thm number_of1}, @{thm number_of2}, @{thm int_0}, @{thm int_1}]
-      addcongs [@{thm conj_le_cong}, @{thm imp_le_cong}]
-    (* simp rules for elimination of abs *)
-    val simpset3 = HOL_basic_ss addsplits [@{thm abs_split}]
-    val ct = cterm_of thy (HOLogic.mk_Trueprop t)
-    (* Theorem for the nat --> int transformation *)
-    val pre_thm = Seq.hd (EVERY
-      [simp_tac mod_div_simpset 1, simp_tac simpset0 1,
-       TRY (simp_tac simpset1 1), TRY (simp_tac simpset2 1),
-       TRY (simp_tac simpset3 1), TRY (simp_tac cooper_ss 1)]
-      (trivial ct))
-    fun assm_tac i = REPEAT_DETERM_N nh (assume_tac i)
-    (* The result of the quantifier elimination *)
-    val (th, tac) = case (prop_of pre_thm) of
-        Const ("==>", _) $ (Const ("Trueprop", _) $ t1) $ _ =>
-    let val pth = linzqe_oracle (cterm_of thy (Pattern.eta_long [] t1))
-    in 
-          ((pth RS iffD2) RS pre_thm,
-            assm_tac (i + 1) THEN (if q then I else TRY) (rtac TrueI i))
-    end
-      | _ => (pre_thm, assm_tac i)
-  in (rtac (((mp_step nh) o (spec_step np)) th) i 
-      THEN tac) st
-  end handle Subscript => no_tac st);
-
-fun linz_args meth =
- let val parse_flag = 
-         Args.$$$ "no_quantify" >> (K (K false));
- in
-   Method.simple_args 
-  (Scan.optional (Args.$$$ "(" |-- Scan.repeat1 parse_flag --| Args.$$$ ")") [] >>
-    curry (Library.foldl op |>) true)
-    (fn q => fn ctxt => meth ctxt q 1)
-  end;
-
-fun linz_method ctxt q i = Method.METHOD (fn facts =>
-  Method.insert_tac facts 1 THEN linz_tac ctxt q i);
-
-val setup =
-  Method.add_method ("cooper",
-     linz_args linz_method,
-     "decision procedure for linear integer arithmetic");
-
-end
--- a/src/HOL/Reflection/ferrack_tac.ML	Mon Feb 16 10:13:30 2009 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,113 +0,0 @@
-(*  Title:      HOL/Reflection/ferrack_tac.ML
-    Author:     Amine Chaieb, TU Muenchen
-*)
-
-structure Ferrack_Tac =
-struct
-
-val trace = ref false;
-fun trace_msg s = if !trace then tracing s else ();
-
-val ferrack_ss = let val ths = [@{thm real_of_int_inject}, @{thm real_of_int_less_iff}, 
-				@{thm real_of_int_le_iff}]
-	     in @{simpset} delsimps ths addsimps (map (fn th => th RS sym) ths)
-	     end;
-
-val binarith =
-  @{thms normalize_bin_simps} @ @{thms pred_bin_simps} @ @{thms succ_bin_simps} @
-  @{thms add_bin_simps} @ @{thms minus_bin_simps} @  @{thms mult_bin_simps};
-val comp_arith = binarith @ simp_thms
-
-val zdvd_int = @{thm zdvd_int};
-val zdiff_int_split = @{thm zdiff_int_split};
-val all_nat = @{thm all_nat};
-val ex_nat = @{thm ex_nat};
-val number_of1 = @{thm number_of1};
-val number_of2 = @{thm number_of2};
-val split_zdiv = @{thm split_zdiv};
-val split_zmod = @{thm split_zmod};
-val mod_div_equality' = @{thm mod_div_equality'};
-val split_div' = @{thm split_div'};
-val Suc_plus1 = @{thm Suc_plus1};
-val imp_le_cong = @{thm imp_le_cong};
-val conj_le_cong = @{thm conj_le_cong};
-val nat_mod_add_eq = @{thm mod_add1_eq} RS sym;
-val nat_mod_add_left_eq = @{thm mod_add_left_eq} RS sym;
-val nat_mod_add_right_eq = @{thm mod_add_right_eq} RS sym;
-val int_mod_add_eq = @{thm zmod_zadd1_eq} RS sym;
-val int_mod_add_left_eq = @{thm zmod_zadd_left_eq} RS sym;
-val int_mod_add_right_eq = @{thm zmod_zadd_right_eq} RS sym;
-val nat_div_add_eq = @{thm div_add1_eq} RS sym;
-val int_div_add_eq = @{thm zdiv_zadd1_eq} RS sym;
-val ZDIVISION_BY_ZERO_MOD = @{thm DIVISION_BY_ZERO} RS conjunct2;
-val ZDIVISION_BY_ZERO_DIV = @{thm DIVISION_BY_ZERO} RS conjunct1;
-
-fun prepare_for_linr sg q fm = 
-  let
-    val ps = Logic.strip_params fm
-    val hs = map HOLogic.dest_Trueprop (Logic.strip_assums_hyp fm)
-    val c = HOLogic.dest_Trueprop (Logic.strip_assums_concl fm)
-    fun mk_all ((s, T), (P,n)) =
-      if 0 mem loose_bnos P then
-        (HOLogic.all_const T $ Abs (s, T, P), n)
-      else (incr_boundvars ~1 P, n-1)
-    fun mk_all2 (v, t) = HOLogic.all_const (fastype_of v) $ lambda v t;
-      val rhs = hs
-(*    val (rhs,irhs) = List.partition (relevant (rev ps)) hs *)
-    val np = length ps
-    val (fm',np) =  foldr (fn ((x, T), (fm,n)) => mk_all ((x, T), (fm,n)))
-      (foldr HOLogic.mk_imp c rhs, np) ps
-    val (vs, _) = List.partition (fn t => q orelse (type_of t) = HOLogic.natT)
-      (OldTerm.term_frees fm' @ OldTerm.term_vars fm');
-    val fm2 = foldr mk_all2 fm' vs
-  in (fm2, np + length vs, length rhs) end;
-
-(*Object quantifier to meta --*)
-fun spec_step n th = if (n=0) then th else (spec_step (n-1) th) RS spec ;
-
-(* object implication to meta---*)
-fun mp_step n th = if (n=0) then th else (mp_step (n-1) th) RS mp;
-
-
-fun linr_tac ctxt q i = 
-    (ObjectLogic.atomize_prems_tac i) 
-	THEN (REPEAT_DETERM (split_tac [@{thm split_min}, @{thm split_max}, @{thm abs_split}] i))
-	THEN (fn st =>
-  let
-    val g = List.nth (prems_of st, i - 1)
-    val thy = ProofContext.theory_of ctxt
-    (* Transform the term*)
-    val (t,np,nh) = prepare_for_linr thy q g
-    (* Some simpsets for dealing with mod div abs and nat*)
-    val simpset0 = Simplifier.theory_context thy HOL_basic_ss addsimps comp_arith
-    val ct = cterm_of thy (HOLogic.mk_Trueprop t)
-    (* Theorem for the nat --> int transformation *)
-   val pre_thm = Seq.hd (EVERY
-      [simp_tac simpset0 1,
-       TRY (simp_tac (Simplifier.theory_context thy ferrack_ss) 1)]
-      (trivial ct))
-    fun assm_tac i = REPEAT_DETERM_N nh (assume_tac i)
-    (* The result of the quantifier elimination *)
-    val (th, tac) = case (prop_of pre_thm) of
-        Const ("==>", _) $ (Const ("Trueprop", _) $ t1) $ _ =>
-    let val pth = linr_oracle (cterm_of thy (Pattern.eta_long [] t1))
-    in 
-          (trace_msg ("calling procedure with term:\n" ^
-             Syntax.string_of_term ctxt t1);
-           ((pth RS iffD2) RS pre_thm,
-            assm_tac (i + 1) THEN (if q then I else TRY) (rtac TrueI i)))
-    end
-      | _ => (pre_thm, assm_tac i)
-  in (rtac (((mp_step nh) o (spec_step np)) th) i 
-      THEN tac) st
-  end handle Subscript => no_tac st);
-
-fun linr_meth src =
-  Method.syntax (Args.mode "no_quantify") src
-  #> (fn (q, ctxt) => Method.SIMPLE_METHOD' (linr_tac ctxt (not q)));
-
-val setup =
-  Method.add_method ("rferrack", linr_meth,
-     "decision procedure for linear real arithmetic");
-
-end
--- a/src/HOL/Reflection/mir_tac.ML	Mon Feb 16 10:13:30 2009 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,168 +0,0 @@
-(*  Title:      HOL/Reflection/mir_tac.ML
-    Author:     Amine Chaieb, TU Muenchen
-*)
-
-structure Mir_Tac =
-struct
-
-val trace = ref false;
-fun trace_msg s = if !trace then tracing s else ();
-
-val mir_ss = 
-let val ths = map thm ["real_of_int_inject", "real_of_int_less_iff", "real_of_int_le_iff"]
-in @{simpset} delsimps ths addsimps (map (fn th => th RS sym) ths)
-end;
-
-val nT = HOLogic.natT;
-  val nat_arith = map thm ["add_nat_number_of", "diff_nat_number_of", 
-                       "mult_nat_number_of", "eq_nat_number_of", "less_nat_number_of"];
-
-  val comp_arith = (map thm ["Let_def", "if_False", "if_True", "add_0", 
-                 "add_Suc", "add_number_of_left", "mult_number_of_left", 
-                 "Suc_eq_add_numeral_1"])@
-                 (map (fn s => thm s RS sym) ["numeral_1_eq_1", "numeral_0_eq_0"])
-                 @ @{thms arith_simps} @ nat_arith @ @{thms rel_simps} 
-  val ths = [@{thm "mult_numeral_1"}, @{thm "mult_numeral_1_right"}, 
-             @{thm "real_of_nat_number_of"},
-             @{thm "real_of_nat_Suc"}, @{thm "real_of_nat_one"}, @{thm "real_of_one"},
-             @{thm "real_of_int_zero"}, @{thm "real_of_nat_zero"},
-             @{thm "Ring_and_Field.divide_zero"}, 
-             @{thm "divide_divide_eq_left"}, @{thm "times_divide_eq_right"}, 
-             @{thm "times_divide_eq_left"}, @{thm "divide_divide_eq_right"},
-             @{thm "diff_def"}, @{thm "minus_divide_left"}]
-val comp_ths = ths @ comp_arith @ simp_thms 
-
-
-val zdvd_int = @{thm "zdvd_int"};
-val zdiff_int_split = @{thm "zdiff_int_split"};
-val all_nat = @{thm "all_nat"};
-val ex_nat = @{thm "ex_nat"};
-val number_of1 = @{thm "number_of1"};
-val number_of2 = @{thm "number_of2"};
-val split_zdiv = @{thm "split_zdiv"};
-val split_zmod = @{thm "split_zmod"};
-val mod_div_equality' = @{thm "mod_div_equality'"};
-val split_div' = @{thm "split_div'"};
-val Suc_plus1 = @{thm "Suc_plus1"};
-val imp_le_cong = @{thm "imp_le_cong"};
-val conj_le_cong = @{thm "conj_le_cong"};
-val nat_mod_add_eq = @{thm "mod_add1_eq"} RS sym;
-val nat_mod_add_left_eq = @{thm "mod_add_left_eq"} RS sym;
-val nat_mod_add_right_eq = @{thm "mod_add_right_eq"} RS sym;
-val int_mod_add_eq = @{thm "zmod_zadd1_eq"} RS sym;
-val int_mod_add_left_eq = @{thm "zmod_zadd_left_eq"} RS sym;
-val int_mod_add_right_eq = @{thm "zmod_zadd_right_eq"} RS sym;
-val nat_div_add_eq = @{thm "div_add1_eq"} RS sym;
-val int_div_add_eq = @{thm "zdiv_zadd1_eq"} RS sym;
-val ZDIVISION_BY_ZERO_MOD = @{thm "DIVISION_BY_ZERO"} RS conjunct2;
-val ZDIVISION_BY_ZERO_DIV = @{thm "DIVISION_BY_ZERO"} RS conjunct1;
-
-fun prepare_for_mir thy q fm = 
-  let
-    val ps = Logic.strip_params fm
-    val hs = map HOLogic.dest_Trueprop (Logic.strip_assums_hyp fm)
-    val c = HOLogic.dest_Trueprop (Logic.strip_assums_concl fm)
-    fun mk_all ((s, T), (P,n)) =
-      if 0 mem loose_bnos P then
-        (HOLogic.all_const T $ Abs (s, T, P), n)
-      else (incr_boundvars ~1 P, n-1)
-    fun mk_all2 (v, t) = HOLogic.all_const (fastype_of v) $ lambda v t;
-      val rhs = hs
-(*    val (rhs,irhs) = List.partition (relevant (rev ps)) hs *)
-    val np = length ps
-    val (fm',np) =  foldr (fn ((x, T), (fm,n)) => mk_all ((x, T), (fm,n)))
-      (foldr HOLogic.mk_imp c rhs, np) ps
-    val (vs, _) = List.partition (fn t => q orelse (type_of t) = nT)
-      (OldTerm.term_frees fm' @ OldTerm.term_vars fm');
-    val fm2 = foldr mk_all2 fm' vs
-  in (fm2, np + length vs, length rhs) end;
-
-(*Object quantifier to meta --*)
-fun spec_step n th = if (n=0) then th else (spec_step (n-1) th) RS spec ;
-
-(* object implication to meta---*)
-fun mp_step n th = if (n=0) then th else (mp_step (n-1) th) RS mp;
-
-
-fun mir_tac ctxt q i = 
-    (ObjectLogic.atomize_prems_tac i)
-        THEN (simp_tac (HOL_basic_ss addsimps [@{thm "abs_ge_zero"}] addsimps simp_thms) i)
-        THEN (REPEAT_DETERM (split_tac [@{thm "split_min"}, @{thm "split_max"},@{thm "abs_split"}] i))
-        THEN (fn st =>
-  let
-    val g = List.nth (prems_of st, i - 1)
-    val thy = ProofContext.theory_of ctxt
-    (* Transform the term*)
-    val (t,np,nh) = prepare_for_mir thy q g
-    (* Some simpsets for dealing with mod div abs and nat*)
-    val mod_div_simpset = HOL_basic_ss 
-                        addsimps [refl,nat_mod_add_eq, 
-                                  @{thm "mod_self"}, @{thm "zmod_self"},
-                                  @{thm "zdiv_zero"},@{thm "zmod_zero"},@{thm "div_0"}, @{thm "mod_0"},
-                                  @{thm "zdiv_1"}, @{thm "zmod_1"}, @{thm "div_1"}, @{thm "mod_1"},
-                                  @{thm "Suc_plus1"}]
-                        addsimps @{thms add_ac}
-                        addsimprocs [cancel_div_mod_proc]
-    val simpset0 = HOL_basic_ss
-      addsimps [mod_div_equality', Suc_plus1]
-      addsimps comp_ths
-      addsplits [@{thm "split_zdiv"}, @{thm "split_zmod"}, @{thm "split_div'"}, @{thm "split_min"}, @{thm "split_max"}]
-    (* Simp rules for changing (n::int) to int n *)
-    val simpset1 = HOL_basic_ss
-      addsimps [@{thm "nat_number_of_def"}, @{thm "zdvd_int"}] @ map (fn r => r RS sym)
-        [@{thm "int_int_eq"}, @{thm "zle_int"}, @{thm "zless_int"}, @{thm "zadd_int"}, 
-         @{thm "zmult_int"}]
-      addsplits [@{thm "zdiff_int_split"}]
-    (*simp rules for elimination of int n*)
-
-    val simpset2 = HOL_basic_ss
-      addsimps [@{thm "nat_0_le"}, @{thm "all_nat"}, @{thm "ex_nat"}, @{thm "number_of1"}, 
-                @{thm "number_of2"}, @{thm "int_0"}, @{thm "int_1"}]
-      addcongs [@{thm "conj_le_cong"}, @{thm "imp_le_cong"}]
-    (* simp rules for elimination of abs *)
-    val ct = cterm_of thy (HOLogic.mk_Trueprop t)
-    (* Theorem for the nat --> int transformation *)
-    val pre_thm = Seq.hd (EVERY
-      [simp_tac mod_div_simpset 1, simp_tac simpset0 1,
-       TRY (simp_tac simpset1 1), TRY (simp_tac simpset2 1), TRY (simp_tac mir_ss 1)]
-      (trivial ct))
-    fun assm_tac i = REPEAT_DETERM_N nh (assume_tac i)
-    (* The result of the quantifier elimination *)
-    val (th, tac) = case (prop_of pre_thm) of
-        Const ("==>", _) $ (Const ("Trueprop", _) $ t1) $ _ =>
-    let val pth =
-          (* If quick_and_dirty then run without proof generation as oracle*)
-             if !quick_and_dirty
-             then mirfr_oracle (false, cterm_of thy (Pattern.eta_long [] t1))
-             else mirfr_oracle (true, cterm_of thy (Pattern.eta_long [] t1))
-    in 
-          (trace_msg ("calling procedure with term:\n" ^
-             Syntax.string_of_term ctxt t1);
-           ((pth RS iffD2) RS pre_thm,
-            assm_tac (i + 1) THEN (if q then I else TRY) (rtac TrueI i)))
-    end
-      | _ => (pre_thm, assm_tac i)
-  in (rtac (((mp_step nh) o (spec_step np)) th) i 
-      THEN tac) st
-  end handle Subscript => no_tac st);
-
-fun mir_args meth =
- let val parse_flag = 
-         Args.$$$ "no_quantify" >> (K (K false));
- in
-   Method.simple_args 
-  (Scan.optional (Args.$$$ "(" |-- Scan.repeat1 parse_flag --| Args.$$$ ")") [] >>
-    curry (Library.foldl op |>) true)
-    (fn q => fn ctxt => meth ctxt q 1)
-  end;
-
-fun mir_method ctxt q i = Method.METHOD (fn facts =>
-  Method.insert_tac facts 1 THEN mir_tac ctxt q i);
-
-val setup =
-  Method.add_method ("mir",
-     mir_args mir_method,
-     "decision procedure for MIR arithmetic");
-
-
-end
--- a/src/HOL/Ring_and_Field.thy	Mon Feb 16 10:13:30 2009 +0100
+++ b/src/HOL/Ring_and_Field.thy	Mon Feb 16 10:15:43 2009 +0100
@@ -41,7 +41,7 @@
 
 class semiring_0 = semiring + comm_monoid_add + mult_zero
 
-class semiring_0_cancel = semiring + comm_monoid_add + cancel_ab_semigroup_add
+class semiring_0_cancel = semiring + cancel_comm_monoid_add
 begin
 
 subclass semiring_0
@@ -80,7 +80,7 @@
 
 end
 
-class comm_semiring_0_cancel = comm_semiring + comm_monoid_add + cancel_ab_semigroup_add
+class comm_semiring_0_cancel = comm_semiring + cancel_comm_monoid_add
 begin
 
 subclass semiring_0_cancel ..
@@ -122,7 +122,7 @@
 
 subclass semiring_1 ..
 
-lemma dvd_refl: "a dvd a"
+lemma dvd_refl[simp]: "a dvd a"
 proof
   show "a = a * 1" by simp
 qed
@@ -182,24 +182,23 @@
 lemma dvd_0_left: "0 dvd a \<Longrightarrow> a = 0"
 by simp
 
-lemma dvd_add:
-  assumes ab: "a dvd b"
-    and ac: "a dvd c"
-    shows "a dvd (b + c)"
+lemma dvd_add[simp]:
+  assumes "a dvd b" and "a dvd c" shows "a dvd (b + c)"
 proof -
-  from ab obtain b' where "b = a * b'" ..
-  moreover from ac obtain c' where "c = a * c'" ..
+  from `a dvd b` obtain b' where "b = a * b'" ..
+  moreover from `a dvd c` obtain c' where "c = a * c'" ..
   ultimately have "b + c = a * (b' + c')" by (simp add: right_distrib)
   then show ?thesis ..
 qed
 
 end
 
+
 class no_zero_divisors = zero + times +
   assumes no_zero_divisors: "a \<noteq> 0 \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> a * b \<noteq> 0"
 
-class semiring_1_cancel = semiring + comm_monoid_add + zero_neq_one
-  + cancel_ab_semigroup_add + monoid_mult
+class semiring_1_cancel = semiring + cancel_comm_monoid_add
+  + zero_neq_one + monoid_mult
 begin
 
 subclass semiring_0_cancel ..
@@ -208,8 +207,8 @@
 
 end
 
-class comm_semiring_1_cancel = comm_semiring + comm_monoid_add + comm_monoid_mult
-  + zero_neq_one + cancel_ab_semigroup_add
+class comm_semiring_1_cancel = comm_semiring + cancel_comm_monoid_add
+  + zero_neq_one + comm_monoid_mult
 begin
 
 subclass semiring_1_cancel ..
@@ -374,6 +373,18 @@
 
 subclass ring_1_no_zero_divisors ..
 
+lemma square_eq_iff: "a * a = b * b \<longleftrightarrow> (a = b \<or> a = - b)"
+proof
+  assume "a * a = b * b"
+  then have "(a - b) * (a + b) = 0"
+    by (simp add: algebra_simps)
+  then show "a = b \<or> a = - b"
+    by (simp add: right_minus_eq eq_neg_iff_add_eq_0)
+next
+  assume "a = b \<or> a = - b"
+  then show "a * a = b * b" by auto
+qed
+
 end
 
 class division_ring = ring_1 + inverse +
@@ -543,7 +554,7 @@
 end
 
 class pordered_cancel_semiring = mult_mono + pordered_ab_semigroup_add
-  + semiring + comm_monoid_add + cancel_ab_semigroup_add
+  + semiring + cancel_comm_monoid_add
 begin
 
 subclass semiring_0_cancel ..
--- a/src/HOL/SET-Protocol/MessageSET.thy	Mon Feb 16 10:13:30 2009 +0100
+++ b/src/HOL/SET-Protocol/MessageSET.thy	Mon Feb 16 10:15:43 2009 +0100
@@ -6,7 +6,7 @@
 header{*The Message Theory, Modified for SET*}
 
 theory MessageSET
-imports Main
+imports Main Nat_Int_Bij
 begin
 
 subsection{*General Lemmas*}
--- a/src/HOL/SET-Protocol/ROOT.ML	Mon Feb 16 10:13:30 2009 +0100
+++ b/src/HOL/SET-Protocol/ROOT.ML	Mon Feb 16 10:15:43 2009 +0100
@@ -6,4 +6,5 @@
 Root file for the SET protocol proofs.
 *)
 
+no_document use_thy "Nat_Int_Bij";
 use_thys ["Cardholder_Registration", "Merchant_Registration", "Purchase"];
--- a/src/HOL/Set.thy	Mon Feb 16 10:13:30 2009 +0100
+++ b/src/HOL/Set.thy	Mon Feb 16 10:15:43 2009 +0100
@@ -787,6 +787,9 @@
 
 lemma set_diff_eq: "A - B = {x. x : A & ~ x : B}" by blast
 
+lemma Compl_eq_Diff_UNIV: "-A = (UNIV - A)"
+by blast
+
 
 subsubsection {* Augmenting a set -- insert *}
 
--- a/src/HOL/SetInterval.thy	Mon Feb 16 10:13:30 2009 +0100
+++ b/src/HOL/SetInterval.thy	Mon Feb 16 10:15:43 2009 +0100
@@ -568,7 +568,6 @@
 apply auto
 apply (case_tac xa)
 apply auto
-apply (auto simp add: finite_M_bounded_by_nat)
 done
 
 lemma card_less_Suc:
--- a/src/HOL/Tools/datatype_package.ML	Mon Feb 16 10:13:30 2009 +0100
+++ b/src/HOL/Tools/datatype_package.ML	Mon Feb 16 10:15:43 2009 +0100
@@ -632,9 +632,8 @@
 local
 
 val sym_datatype = Pretty.str "\\isacommand{datatype}";
-val sym_binder = Pretty.str "{\\isacharequal}";
-val sym_of = Pretty.str "of";
-val sym_sep = Pretty.str "{\\isacharbar}";
+val sym_binder = Pretty.str "\\ {\\isacharequal}";
+val sym_sep = Pretty.str "{\\isacharbar}\\ ";
 
 in
 
@@ -660,17 +659,19 @@
       | pretty_constr (co, [ty']) =
           (Pretty.block o Pretty.breaks)
             [Syntax.pretty_term ctxt (Const (co, ty' --> ty)),
-              sym_of, Syntax.pretty_typ ctxt ty']
+              Syntax.pretty_typ ctxt ty']
       | pretty_constr (co, tys) =
           (Pretty.block o Pretty.breaks)
             (Syntax.pretty_term ctxt (Const (co, tys ---> ty)) ::
-              sym_of :: map pretty_typ_br tys);
-  in (Pretty.block o Pretty.breaks) (
-    sym_datatype
-    :: Syntax.pretty_typ ctxt ty
-    :: sym_binder
-    :: separate sym_sep (map pretty_constr cos)
-  ) end
+              map pretty_typ_br tys);
+  in
+    Pretty.block
+      (sym_datatype :: Pretty.brk 1 ::
+       Syntax.pretty_typ ctxt ty ::
+       sym_binder :: Pretty.brk 1 ::
+       flat (separate [Pretty.brk 1, sym_sep]
+         (map (single o pretty_constr) cos)))
+  end
 
 end;
 
--- a/src/HOL/Tools/function_package/fundef_common.ML	Mon Feb 16 10:13:30 2009 +0100
+++ b/src/HOL/Tools/function_package/fundef_common.ML	Mon Feb 16 10:15:43 2009 +0100
@@ -254,8 +254,8 @@
                          ^ " occur" ^ plural "s" "" rvs ^ " on right hand side only:")
                                     
             val _ = forall (not o Term.exists_subterm 
-                             (fn Free (n, _) => n mem fnames | _ => false)) gs 
-                    orelse input_error "Recursive Calls not allowed in premises"
+                             (fn Free (n, _) => n mem fnames | _ => false)) (gs @ args)
+                    orelse input_error "Defined function may not occur in premises or arguments"
 
             val freeargs = map (fn t => subst_bounds (rev (map Free qs), t)) args
             val funvars = filter (fn q => exists (exists_subterm (fn (Free q') $ _ => q = q' | _ => false)) freeargs) qs
--- a/src/HOL/ex/Sublist.thy	Mon Feb 16 10:13:30 2009 +0100
+++ b/src/HOL/ex/Sublist.thy	Mon Feb 16 10:15:43 2009 +0100
@@ -64,8 +64,6 @@
       apply (simp add: sublist_Cons)
       apply auto
       apply (auto simp add: nat.split)
-      apply (simp add: card_less)
-      apply (simp add: card_less)
       apply (simp add: card_less_Suc[symmetric])
       apply (simp add: card_less_Suc2)
       done
--- a/src/HOLCF/ROOT.ML	Mon Feb 16 10:13:30 2009 +0100
+++ b/src/HOLCF/ROOT.ML	Mon Feb 16 10:15:43 2009 +0100
@@ -5,4 +5,6 @@
 HOLCF -- a semantic extension of HOL by the LCF logic.
 *)
 
+no_document use_thy "Nat_Int_Bij";
+
 use_thy "HOLCF";
--- a/src/Pure/General/seq.ML	Mon Feb 16 10:13:30 2009 +0100
+++ b/src/Pure/General/seq.ML	Mon Feb 16 10:15:43 2009 +0100
@@ -89,17 +89,17 @@
 (*the list of the first n elements, paired with rest of sequence;
   if length of list is less than n, then sequence had less than n elements*)
 fun chop n xq =
-  if n <= (0: int) then ([], xq)
+  if n <= (0 : int) then ([], xq)
   else
     (case pull xq of
       NONE => ([], xq)
     | SOME (x, xq') => apfst (Basics.cons x) (chop (n - 1) xq'));
 
-(* truncate the sequence after n elements *)
-fun take n s = let
-    fun f 0 _  () = NONE
-      | f n ss () = Option.map (apsnd (make o f (n - 1))) (pull ss);
-  in make (f n s) end;
+(*truncate the sequence after n elements*)
+fun take n xq =
+  if n <= (0 : int) then empty
+  else make (fn () =>
+    (Option.map o apsnd) (take (n - 1)) (pull xq));
 
 (*conversion from sequence to list*)
 fun list_of xq =
--- a/src/Pure/IsaMakefile	Mon Feb 16 10:13:30 2009 +0100
+++ b/src/Pure/IsaMakefile	Mon Feb 16 10:15:43 2009 +0100
@@ -38,10 +38,10 @@
   Isar/attrib.ML Isar/auto_bind.ML Isar/calculation.ML Isar/class.ML	\
   Isar/class_target.ML Isar/code.ML Isar/code_unit.ML			\
   Isar/constdefs.ML Isar/context_rules.ML Isar/element.ML		\
-  Isar/expression.ML Isar/find_theorems.ML Isar/isar.ML			\
-  Isar/isar_document.ML Isar/isar_cmd.ML Isar/isar_syn.ML		\
+  Isar/expression.ML Isar/find_theorems.ML Isar/find_consts.ML          \
+  Isar/isar.ML Isar/isar_document.ML Isar/isar_cmd.ML Isar/isar_syn.ML	\
   Isar/local_defs.ML Isar/local_syntax.ML Isar/local_theory.ML		\
-  Isar/locale.ML Isar/method.ML Isar/net_rules.ML	\
+  Isar/locale.ML Isar/method.ML Isar/net_rules.ML			\
   Isar/object_logic.ML Isar/obtain.ML Isar/outer_keyword.ML		\
   Isar/outer_lex.ML Isar/outer_parse.ML Isar/outer_syntax.ML		\
   Isar/overloading.ML Isar/proof.ML Isar/proof_context.ML		\
--- a/src/Pure/Isar/ROOT.ML	Mon Feb 16 10:13:30 2009 +0100
+++ b/src/Pure/Isar/ROOT.ML	Mon Feb 16 10:15:43 2009 +0100
@@ -90,5 +90,6 @@
 use "rule_insts.ML";
 use "../Thy/thm_deps.ML";
 use "find_theorems.ML";
+use "find_consts.ML";
 use "isar_cmd.ML";
 use "isar_syn.ML";
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/Pure/Isar/find_consts.ML	Mon Feb 16 10:15:43 2009 +0100
@@ -0,0 +1,120 @@
+(*  Title:      find_consts.ML
+    Author:     Timothy Bourke and Gerwin Klein, NICTA
+
+  Hoogle-like (http://www-users.cs.york.ac.uk/~ndm/hoogle) searching by type
+  over constants, but matching is not fuzzy
+*)
+
+signature FIND_CONSTS =
+sig
+  datatype criterion = Strict of string
+                     | Loose of string
+                     | Name of string
+
+  val default_criteria : (bool * criterion) list ref
+
+  val find_consts : Proof.context -> (bool * criterion) list -> unit
+end;
+
+structure FindConsts : FIND_CONSTS =
+struct
+
+datatype criterion = Strict of string
+                   | Loose of string
+                   | Name of string;
+
+val default_criteria = ref [(false, Name ".sko_")];
+
+fun add_tye (_, (_, t)) n = size_of_typ t + n;
+
+fun matches_subtype thy typat = let
+    val p = can (fn ty => Sign.typ_match thy (typat, ty) Vartab.empty);
+
+    fun fs [] = false
+      | fs (t::ts) = f t orelse fs ts
+
+    and f (t as Type (_, ars)) = p t orelse fs ars
+      | f t = p t;
+  in f end;
+
+fun check_const p (nm, (ty, _)) = if p (nm, ty)
+                                  then SOME (size_of_typ ty)
+                                  else NONE;
+
+fun opt_not f (c as (_, (ty, _))) = if is_some (f c)
+                                    then NONE else SOME (size_of_typ ty);
+
+fun filter_const (_, NONE) = NONE
+  | filter_const (f, (SOME (c, r))) = Option.map
+                                        (pair c o ((curry Int.min) r)) (f c);
+
+fun pretty_criterion (b, c) =
+  let
+    fun prfx s = if b then s else "-" ^ s;
+  in
+    (case c of
+      Strict pat => Pretty.str (prfx "strict: " ^ quote pat)
+    | Loose pat => Pretty.str (prfx (quote pat))
+    | Name name => Pretty.str (prfx "name: " ^ quote name))
+  end;
+
+fun pretty_const ctxt (nm, ty) = let
+    val ty' = Logic.unvarifyT ty;
+  in
+    Pretty.block [Pretty.quote (Pretty.str nm), Pretty.fbrk,
+                  Pretty.str "::", Pretty.brk 1,
+                  Pretty.quote (Syntax.pretty_typ ctxt ty')]
+  end;
+
+fun find_consts ctxt raw_criteria = let
+    val start = start_timing ();
+
+    val thy = ProofContext.theory_of ctxt;
+    val low_ranking = 10000;
+
+    fun make_pattern crit = ProofContext.read_term_pattern ctxt ("_::" ^ crit)
+                            |> type_of;
+
+    fun make_match (Strict arg) =
+          let val qty = make_pattern arg; in
+            fn (_, (ty, _)) => let
+                val tye = Sign.typ_match thy (qty, ty) Vartab.empty;
+                val sub_size = Vartab.fold add_tye tye 0;
+              in SOME sub_size end handle MATCH => NONE
+          end
+
+      | make_match (Loose arg) =
+          check_const (matches_subtype thy (make_pattern arg) o snd)
+      
+      | make_match (Name arg) = check_const (match_string arg o fst);
+
+    fun make_criterion (b, crit) = (if b then I else opt_not) (make_match crit);
+    val criteria = map make_criterion ((!default_criteria) @ raw_criteria);
+
+    val (_, consts) = (#constants o Consts.dest o Sign.consts_of) thy;
+    fun eval_entry c = foldl filter_const (SOME (c, low_ranking)) criteria;
+
+    val matches = Symtab.fold (cons o eval_entry) consts []
+                  |> map_filter I
+                  |> sort (rev_order o int_ord o pairself snd)
+                  |> map ((apsnd fst) o fst);
+
+    val end_msg = " in " ^
+                  (List.nth (String.tokens Char.isSpace (end_timing start), 3))
+                  ^ " secs"
+  in
+    Pretty.big_list "searched for:" (map pretty_criterion raw_criteria)
+      :: Pretty.str ""
+      :: (Pretty.str o concat)
+           (if null matches
+            then ["nothing found", end_msg]
+            else ["found ", (string_of_int o length) matches,
+                  " constants", end_msg, ":"])
+      :: Pretty.str ""
+      :: map (pretty_const ctxt) matches
+    |> Pretty.chunks
+    |> Pretty.writeln
+  end handle ERROR s => Output.error_msg s
+
+end;
+
--- a/src/Pure/Isar/find_theorems.ML	Mon Feb 16 10:13:30 2009 +0100
+++ b/src/Pure/Isar/find_theorems.ML	Mon Feb 16 10:15:43 2009 +0100
@@ -103,21 +103,10 @@
 
 (* filter_name *)
 
-fun match_string pat str =
-  let
-    fun match [] _ = true
-      | match (p :: ps) s =
-          size p <= size s andalso
-            (case try (unprefix p) s of
-              SOME s' => match ps s'
-            | NONE => match (p :: ps) (String.substring (s, 1, size s - 1)));
-  in match (space_explode "*" pat) str end;
-
 fun filter_name str_pat (thmref, _) =
   if match_string str_pat (Facts.name_of_ref thmref)
   then SOME (0, 0) else NONE;
 
-
 (* filter intro/elim/dest/solves rules *)
 
 fun filter_dest ctxt goal (_, thm) =
--- a/src/Pure/Isar/isar_cmd.ML	Mon Feb 16 10:13:30 2009 +0100
+++ b/src/Pure/Isar/isar_cmd.ML	Mon Feb 16 10:15:43 2009 +0100
@@ -64,6 +64,8 @@
   val thm_deps: (Facts.ref * Attrib.src list) list -> Toplevel.transition -> Toplevel.transition
   val find_theorems: (int option * bool) * (bool * string FindTheorems.criterion) list
     -> Toplevel.transition -> Toplevel.transition
+  val find_consts: (bool * FindConsts.criterion) list ->
+                   Toplevel.transition -> Toplevel.transition
   val unused_thms: (string list * string list option) option ->
     Toplevel.transition -> Toplevel.transition
   val print_binds: Toplevel.transition -> Toplevel.transition
@@ -432,6 +434,12 @@
     |> map pretty_thm |> Pretty.chunks |> Pretty.writeln
   end);
 
+(* retrieve constants *)
+
+fun find_consts spec =
+  Toplevel.unknown_theory o Toplevel.keep (fn state =>
+  let val ctxt = (Proof.context_of o Toplevel.enter_proof_body) state
+  in FindConsts.find_consts ctxt spec end);
 
 (* print proof context contents *)
 
--- a/src/Pure/Isar/isar_syn.ML	Mon Feb 16 10:13:30 2009 +0100
+++ b/src/Pure/Isar/isar_syn.ML	Mon Feb 16 10:15:43 2009 +0100
@@ -878,6 +878,22 @@
 
 end;
 
+local
+
+val criterion =
+  P.reserved "strict" |-- P.!!! (P.$$$ ":" |-- P.xname) >> FindConsts.Strict ||
+  P.reserved "name" |-- P.!!! (P.$$$ ":" |-- P.xname) >> FindConsts.Name ||
+  P.xname >> FindConsts.Loose;
+
+in
+
+val _ =
+  OuterSyntax.improper_command "find_consts" "search constants by type pattern"
+    K.diag (Scan.repeat (((Scan.option P.minus >> is_none) -- criterion))
+            >> (Toplevel.no_timing oo IsarCmd.find_consts));
+
+end;
+
 val _ =
   OuterSyntax.improper_command "print_binds" "print term bindings of proof context" K.diag
     (Scan.succeed (Toplevel.no_timing o IsarCmd.print_binds));
--- a/src/Pure/library.ML	Mon Feb 16 10:13:30 2009 +0100
+++ b/src/Pure/library.ML	Mon Feb 16 10:15:43 2009 +0100
@@ -159,6 +159,7 @@
   val replicate_string: int -> string -> string
   val translate_string: (string -> string) -> string -> string
   val multiply: 'a list -> 'a list list -> 'a list list
+  val match_string: string -> string -> bool
 
   (*lists as sets -- see also Pure/General/ord_list.ML*)
   val member: ('b * 'a -> bool) -> 'a list -> 'b -> bool
@@ -554,7 +555,6 @@
 fun multiply [] _ = []
   | multiply (x :: xs) yss = map (cons x) yss @ multiply xs yss;
 
-
 (* direct product *)
 
 fun map_product f _ [] = []
@@ -787,7 +787,16 @@
       if k mod 2 = 0 then replicate_string (k div 2) (a ^ a)
       else replicate_string (k div 2) (a ^ a) ^ a;
 
-
+(*crude matching of str against simple glob pat*)
+fun match_string pat str =
+  let
+    fun match [] _ = true
+      | match (p :: ps) s =
+          size p <= size s andalso
+            (case try (unprefix p) s of
+              SOME s' => match ps s'
+            | NONE => match (p :: ps) (String.substring (s, 1, size s - 1)));
+  in match (space_explode "*" pat) str end;
 
 (** lists as sets -- see also Pure/General/ord_list.ML **)
 
--- a/src/Pure/term.ML	Mon Feb 16 10:13:30 2009 +0100
+++ b/src/Pure/term.ML	Mon Feb 16 10:15:43 2009 +0100
@@ -64,6 +64,7 @@
   val strip_comb: term -> term * term list
   val head_of: term -> term
   val size_of_term: term -> int
+  val size_of_typ: typ -> int
   val map_atyps: (typ -> typ) -> typ -> typ
   val map_aterms: (term -> term) -> term -> term
   val map_type_tvar: (indexname * sort -> typ) -> typ -> typ
@@ -391,6 +392,13 @@
       | add_size (_, n) = n + 1;
   in add_size (tm, 0) end;
 
+(*number of tfrees, tvars, and constructors in a type*)
+fun size_of_typ ty =
+  let
+    fun add_size (Type (_, ars), n) = foldl add_size (n + 1) ars
+      | add_size (_, n) = n + 1;
+  in add_size (ty, 0) end;
+
 fun map_atyps f (Type (a, Ts)) = Type (a, map (map_atyps f) Ts)
   | map_atyps f T = f T;