separation of HOL-Hyperreal from HOL-Real
authorpaulson
Sat, 30 Dec 2000 22:03:47 +0100
changeset 10751 a81ea5d3dd41
parent 10750 a681d3df1a39
child 10752 c4f1bf2acf4c
separation of HOL-Hyperreal from HOL-Real
src/HOL/Hyperreal/HSeries.ML
src/HOL/Hyperreal/HSeries.thy
src/HOL/Hyperreal/HyperArith.thy
src/HOL/Hyperreal/HyperArith0.ML
src/HOL/Hyperreal/HyperArith0.thy
src/HOL/Hyperreal/HyperBin.ML
src/HOL/Hyperreal/HyperBin.thy
src/HOL/Hyperreal/HyperDef.ML
src/HOL/Hyperreal/HyperDef.thy
src/HOL/Hyperreal/HyperNat.ML
src/HOL/Hyperreal/HyperNat.thy
src/HOL/Hyperreal/HyperOrd.ML
src/HOL/Hyperreal/HyperOrd.thy
src/HOL/Hyperreal/HyperPow.ML
src/HOL/Hyperreal/HyperPow.thy
src/HOL/Hyperreal/Hyperreal.thy
src/HOL/Hyperreal/Lim.ML
src/HOL/Hyperreal/Lim.thy
src/HOL/Hyperreal/NSA.ML
src/HOL/Hyperreal/NSA.thy
src/HOL/Hyperreal/NatStar.ML
src/HOL/Hyperreal/NatStar.thy
src/HOL/Hyperreal/ROOT.ML
src/HOL/Hyperreal/SEQ.ML
src/HOL/Hyperreal/SEQ.thy
src/HOL/Hyperreal/Series.ML
src/HOL/Hyperreal/Series.thy
src/HOL/Hyperreal/Star.ML
src/HOL/Hyperreal/Star.thy
src/HOL/Hyperreal/Zorn.ML
src/HOL/Hyperreal/Zorn.thy
src/HOL/Hyperreal/fuf.ML
src/HOL/Hyperreal/hypreal_arith.ML
src/HOL/Hyperreal/hypreal_arith0.ML
src/HOL/IsaMakefile
src/HOL/Real/Hyperreal/Filter.ML
src/HOL/Real/Hyperreal/Filter.thy
src/HOL/Real/Hyperreal/HRealAbs.ML
src/HOL/Real/Hyperreal/HRealAbs.thy
src/HOL/Real/Hyperreal/HyperDef.ML
src/HOL/Real/Hyperreal/HyperDef.thy
src/HOL/Real/Hyperreal/HyperNat.ML
src/HOL/Real/Hyperreal/HyperNat.thy
src/HOL/Real/Hyperreal/HyperOrd.ML
src/HOL/Real/Hyperreal/HyperOrd.thy
src/HOL/Real/Hyperreal/HyperPow.ML
src/HOL/Real/Hyperreal/HyperPow.thy
src/HOL/Real/Hyperreal/Hyperreal.thy
src/HOL/Real/Hyperreal/Lim.ML
src/HOL/Real/Hyperreal/Lim.thy
src/HOL/Real/Hyperreal/NSA.ML
src/HOL/Real/Hyperreal/NSA.thy
src/HOL/Real/Hyperreal/NatStar.ML
src/HOL/Real/Hyperreal/NatStar.thy
src/HOL/Real/Hyperreal/SEQ.ML
src/HOL/Real/Hyperreal/SEQ.thy
src/HOL/Real/Hyperreal/Series.ML
src/HOL/Real/Hyperreal/Series.thy
src/HOL/Real/Hyperreal/Star.ML
src/HOL/Real/Hyperreal/Star.thy
src/HOL/Real/Hyperreal/Zorn.ML
src/HOL/Real/Hyperreal/Zorn.thy
src/HOL/Real/Hyperreal/fuf.ML
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Hyperreal/HSeries.ML	Sat Dec 30 22:03:47 2000 +0100
@@ -0,0 +1,321 @@
+(*  Title       : HSeries.ML
+    Author      : Jacques D. Fleuriot
+    Copyright   : 1998  University of Cambridge
+    Description : Finite summation and infinite series
+                  for hyperreals
+*) 
+
+Goalw [sumhr_def]
+     "sumhr(M,N,f) =  \
+\       Abs_hypreal(UN X:Rep_hypnat(M). UN Y: Rep_hypnat(N). \
+\         hyprel ^^{%n::nat. sumr (X n) (Y n) f})";
+by (Auto_tac);
+qed "sumhr_iff";
+
+Goalw [sumhr_def]
+     "sumhr(Abs_hypnat(hypnatrel^^{%n. M n}), \
+\           Abs_hypnat(hypnatrel^^{%n. N n}), f) = \
+\     Abs_hypreal(hyprel ^^ {%n. sumr (M n) (N n) f})";
+by (res_inst_tac [("f","Abs_hypreal")] arg_cong 1);
+by (Auto_tac THEN Ultra_tac 1);
+qed "sumhr";
+
+(*------------------------------------------------------- 
+  lcp's suggestion: exploit pattern matching 
+  facilities and use as definition instead (to do)
+ -------------------------------------------------------*)
+Goalw [sumhr_def]
+      "sumhr p = (%(M,N,f). Abs_hypreal(UN X:Rep_hypnat(M). \
+\                     UN Y: Rep_hypnat(N). \
+\           hyprel ^^{%n::nat. sumr (X n) (Y n) f})) p";
+by (res_inst_tac [("p","p")] PairE 1);
+by (res_inst_tac [("p","y")] PairE 1);
+by (Auto_tac);
+qed "sumhr_iff2";
+
+(* Theorem corresponding to base case in def of sumr *)
+Goalw [hypnat_zero_def]
+     "sumhr (m,0,f) = #0";
+by (res_inst_tac [("z","m")] eq_Abs_hypnat 1);
+by (auto_tac (claset(), 
+              simpset() addsimps [sumhr, symmetric hypreal_zero_def]));
+qed "sumhr_zero";
+Addsimps [sumhr_zero];
+
+(* Theorem corresponding to recursive case in def of sumr *)
+Goalw [hypnat_one_def]
+     "sumhr(m,n+1hn,f) = (if n + 1hn <= m then #0 \
+\                         else sumhr(m,n,f) + (*fNat* f) n)";
+by (simp_tac (HOL_ss addsimps
+             [zero_eq_numeral_0 RS sym, hypreal_zero_def]) 1); 
+by (res_inst_tac [("z","m")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","n")] eq_Abs_hypnat 1);
+by (auto_tac (claset(),
+     simpset() addsimps [sumhr, hypnat_add,hypnat_le,starfunNat,hypreal_add]));
+by (ALLGOALS(Ultra_tac));
+qed "sumhr_if";
+
+Goalw [hypnat_one_def] "sumhr (n + 1hn, n, f) = #0";
+by (simp_tac (HOL_ss addsimps
+             [zero_eq_numeral_0 RS sym, hypreal_zero_def]) 1); 
+by (res_inst_tac [("z","n")] eq_Abs_hypnat 1);
+by (auto_tac (claset(),
+              simpset() addsimps [sumhr, hypnat_add]));
+qed "sumhr_Suc_zero";
+Addsimps [sumhr_Suc_zero];
+
+Goal "sumhr (n,n,f) = #0";
+by (simp_tac (HOL_ss addsimps
+             [zero_eq_numeral_0 RS sym, hypreal_zero_def]) 1); 
+by (res_inst_tac [("z","n")] eq_Abs_hypnat 1);
+by (auto_tac (claset(), simpset() addsimps [sumhr]));
+qed "sumhr_eq_bounds";
+Addsimps [sumhr_eq_bounds];
+
+Goalw [hypnat_one_def] 
+     "sumhr (m,m + 1hn,f) = (*fNat* f) m";
+by (res_inst_tac [("z","m")] eq_Abs_hypnat 1);
+by (auto_tac (claset(),
+              simpset() addsimps [sumhr, hypnat_add,starfunNat]));
+qed "sumhr_Suc";
+Addsimps [sumhr_Suc];
+
+Goal "sumhr(m+k,k,f) = #0";
+by (simp_tac (HOL_ss addsimps
+             [zero_eq_numeral_0 RS sym, hypreal_zero_def]) 1); 
+by (res_inst_tac [("z","m")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","k")] eq_Abs_hypnat 1);
+by (auto_tac (claset(),
+              simpset() addsimps [sumhr, hypnat_add]));
+qed "sumhr_add_lbound_zero";
+Addsimps [sumhr_add_lbound_zero];
+
+Goal "sumhr (m,n,f) + sumhr(m,n,g) = sumhr(m,n,%i. f i + g i)";
+by (res_inst_tac [("z","m")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","n")] eq_Abs_hypnat 1);
+by (auto_tac (claset(),
+              simpset() addsimps [sumhr, hypreal_add,sumr_add]));
+qed "sumhr_add";
+
+Goalw [hypreal_of_real_def]
+      "hypreal_of_real r * sumhr(m,n,f) = sumhr(m,n,%n. r * f n)";
+by (res_inst_tac [("z","m")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","n")] eq_Abs_hypnat 1);
+by (auto_tac (claset(),
+              simpset() addsimps [sumhr, hypreal_mult,sumr_mult]));
+qed "sumhr_mult";
+
+Goalw [hypnat_zero_def]
+     "n < p ==> sumhr (0,n,f) + sumhr (n,p,f) = sumhr (0,p,f)";
+by (res_inst_tac [("z","n")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","p")] eq_Abs_hypnat 1);
+by (auto_tac (claset() addSEs [FreeUltrafilterNat_subset],
+       simpset() addsimps [sumhr,hypreal_add,hypnat_less, sumr_split_add]));
+qed "sumhr_split_add";
+
+(*FIXME delete*)
+Goal "n < p ==> sumhr (0, p, f) + - sumhr (0, n, f) = sumhr (n,p,f)";
+by (dres_inst_tac [("f1","f")] (sumhr_split_add RS sym) 1);
+by (Asm_simp_tac 1);
+qed "sumhr_split_add_minus";
+
+Goal "n < p ==> sumhr (0, p, f) - sumhr (0, n, f) = sumhr (n,p,f)";
+by (dres_inst_tac [("f1","f")] (sumhr_split_add RS sym) 1);
+by (Asm_simp_tac 1);
+qed "sumhr_split_diff";
+
+Goal "abs(sumhr(m,n,f)) <= sumhr(m,n,%i. abs(f i))";
+by (res_inst_tac [("z","n")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","m")] eq_Abs_hypnat 1);
+by (auto_tac (claset(),
+              simpset() addsimps [sumhr, hypreal_le,hypreal_hrabs,sumr_rabs]));
+qed "sumhr_hrabs";
+
+(* other general version also needed *)
+Goalw [hypnat_of_nat_def]
+     "(ALL r. m <= r & r < n --> f r = g r) --> \
+\     sumhr(hypnat_of_nat m, hypnat_of_nat n, f) = \
+\     sumhr(hypnat_of_nat m, hypnat_of_nat n, g)";
+by (Step_tac 1 THEN dtac sumr_fun_eq 1);
+by (auto_tac (claset(), simpset() addsimps [sumhr]));
+qed "sumhr_fun_hypnat_eq";
+
+Goalw [hypnat_zero_def,hypreal_of_real_def]
+      "sumhr(0,n,%i. r) = hypreal_of_hypnat n*hypreal_of_real r";
+by (res_inst_tac [("z","n")] eq_Abs_hypnat 1);
+by (asm_simp_tac
+    (simpset() addsimps [sumhr, hypreal_of_hypnat,hypreal_mult]) 1);
+qed "sumhr_const";
+
+Goalw [hypnat_zero_def,hypreal_of_real_def]
+     "sumhr(0,n,f) + -(hypreal_of_hypnat n*hypreal_of_real r) = \
+\     sumhr(0,n,%i. f i + -r)";
+by (res_inst_tac [("z","n")] eq_Abs_hypnat 1);
+by (asm_simp_tac (simpset() addsimps [sumhr,
+		    hypreal_of_hypnat,hypreal_mult,hypreal_add,
+		    hypreal_minus,sumr_add RS sym]) 1);
+qed "sumhr_add_mult_const";
+
+Goal "n < m ==> sumhr (m,n,f) = #0";
+by (simp_tac (HOL_ss addsimps
+             [zero_eq_numeral_0 RS sym, hypreal_zero_def]) 1); 
+by (res_inst_tac [("z","m")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","n")] eq_Abs_hypnat 1);
+by (auto_tac (claset() addEs [FreeUltrafilterNat_subset],
+              simpset() addsimps [sumhr,hypnat_less]));
+qed "sumhr_less_bounds_zero";
+Addsimps [sumhr_less_bounds_zero];
+
+Goal "sumhr(m, n, %i. - f i) = - sumhr(m, n, f)";
+by (res_inst_tac [("z","m")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","n")] eq_Abs_hypnat 1);
+by (auto_tac (claset(), simpset() addsimps [sumhr, hypreal_minus,sumr_minus]));
+qed "sumhr_minus";
+
+Goalw [hypnat_of_nat_def]
+     "sumhr(m+hypnat_of_nat k,n+hypnat_of_nat k,f) = sumhr(m,n,%i. f(i + k))";
+by (res_inst_tac [("z","m")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","n")] eq_Abs_hypnat 1);
+by (auto_tac (claset(),
+              simpset() addsimps [sumhr, hypnat_add,sumr_shift_bounds]));
+qed "sumhr_shift_bounds";
+
+(*------------------------------------------------------------------
+      Theorems about NS sums - infinite sums are obtained
+      by summing to some infinite hypernatural (such as whn)
+ -----------------------------------------------------------------*)
+Goalw [hypnat_omega_def,hypnat_zero_def] 
+      "sumhr(0,whn,%i. #1) = hypreal_of_hypnat whn";
+by (auto_tac (claset(),
+              simpset() addsimps [sumhr, hypreal_of_hypnat]));
+qed "sumhr_hypreal_of_hypnat_omega";
+
+Goalw [hypnat_omega_def,hypnat_zero_def,omega_def]  
+     "sumhr(0, whn, %i. #1) = whr - #1";
+by (simp_tac (HOL_ss addsimps
+             [one_eq_numeral_1 RS sym, hypreal_one_def]) 1); 
+by (auto_tac (claset(),
+              simpset() addsimps [sumhr, hypreal_diff, real_of_nat_def]));
+qed "sumhr_hypreal_omega_minus_one";
+
+Goalw [hypnat_zero_def, hypnat_omega_def]
+     "sumhr(0, whn + whn, %i. (-#1) ^ (i+1)) = #0";
+by (simp_tac (HOL_ss addsimps
+             [zero_eq_numeral_0 RS sym, hypreal_zero_def]) 1); 
+by (simp_tac (simpset() addsimps [sumhr,hypnat_add] delsimps [realpow_Suc]) 1);
+qed "sumhr_minus_one_realpow_zero";
+Addsimps [sumhr_minus_one_realpow_zero];
+
+Goalw [hypnat_of_nat_def,hypreal_of_real_def]
+     "(ALL n. m <= Suc n --> f n = r) & m <= na \
+\          ==> sumhr(hypnat_of_nat m,hypnat_of_nat na,f) = \
+\          (hypreal_of_nat (na - m) * hypreal_of_real r)";
+by (auto_tac (claset() addSDs [sumr_interval_const],
+    simpset() addsimps [sumhr,hypreal_of_nat_real_of_nat,
+              hypreal_of_real_def,hypreal_mult]));
+qed "sumhr_interval_const";
+
+Goalw [hypnat_zero_def]
+     "(*fNat* (%n. sumr 0 n f)) N = sumhr(0,N,f)";
+by (res_inst_tac [("z","N")] eq_Abs_hypnat 1);
+by (asm_full_simp_tac (simpset() addsimps [starfunNat,sumhr]) 1);
+qed "starfunNat_sumr";
+
+Goal "sumhr (0, M, f) @= sumhr (0, N, f) \
+\     ==> abs (sumhr (M, N, f)) @= #0";
+by (cut_inst_tac [("x","M"),("y","N")] hypnat_linear 1);
+by (auto_tac (claset(), simpset() addsimps [inf_close_refl]));
+by (dtac (inf_close_sym RS (inf_close_minus_iff RS iffD1)) 1);
+by (auto_tac (claset() addDs [inf_close_hrabs],
+              simpset() addsimps [sumhr_split_add_minus]));
+qed "sumhr_hrabs_inf_close";
+Addsimps [sumhr_hrabs_inf_close];
+
+(*----------------------------------------------------------------
+      infinite sums: Standard and NS theorems
+ ----------------------------------------------------------------*)
+Goalw [sums_def,NSsums_def] "(f sums l) = (f NSsums l)";
+by (simp_tac (simpset() addsimps [LIMSEQ_NSLIMSEQ_iff]) 1);
+qed "sums_NSsums_iff";
+
+Goalw [summable_def,NSsummable_def] 
+      "(summable f) = (NSsummable f)";
+by (simp_tac (simpset() addsimps [sums_NSsums_iff]) 1);
+qed "summable_NSsummable_iff";
+
+Goalw [suminf_def,NSsuminf_def] 
+      "(suminf f) = (NSsuminf f)";
+by (simp_tac (simpset() addsimps [sums_NSsums_iff]) 1);
+qed "suminf_NSsuminf_iff";
+
+Goalw [NSsums_def,NSsummable_def] 
+      "f NSsums l ==> NSsummable f";
+by (Blast_tac 1);
+qed "NSsums_NSsummable";
+
+Goalw [NSsummable_def,NSsuminf_def]
+     "NSsummable f ==> f NSsums (NSsuminf f)";
+by (blast_tac (claset() addIs [someI2]) 1);
+qed "NSsummable_NSsums";
+
+Goal "f NSsums s ==> (s = NSsuminf f)";
+by (asm_full_simp_tac 
+    (simpset() addsimps [suminf_NSsuminf_iff RS sym,sums_NSsums_iff,
+                         sums_unique]) 1);
+qed "NSsums_unique";
+
+Goal "ALL m. n <= Suc m --> f(m) = #0 ==> f NSsums (sumr 0 n f)";
+by (asm_simp_tac (simpset() addsimps [sums_NSsums_iff RS sym, series_zero]) 1);
+qed "NSseries_zero";
+
+Goal "NSsummable f = \
+\     (ALL M: HNatInfinite. ALL N: HNatInfinite. abs (sumhr(M,N,f)) @= #0)";
+by (auto_tac (claset(),
+              simpset() addsimps [summable_NSsummable_iff RS sym,
+                 summable_convergent_sumr_iff, convergent_NSconvergent_iff,
+                 NSCauchy_NSconvergent_iff RS sym, NSCauchy_def,
+                 starfunNat_sumr]));
+by (cut_inst_tac [("x","M"),("y","N")] hypnat_linear 1);
+by (auto_tac (claset(), simpset() addsimps [inf_close_refl]));
+by (rtac ((inf_close_minus_iff RS iffD2) RS inf_close_sym) 1);
+by (rtac (inf_close_minus_iff RS iffD2) 2);
+by (auto_tac (claset() addDs [inf_close_hrabs_zero_cancel],
+              simpset() addsimps [sumhr_split_add_minus]));
+qed "NSsummable_NSCauchy";
+
+(*-------------------------------------------------------------------
+         Terms of a convergent series tend to zero
+ -------------------------------------------------------------------*)
+Goalw [NSLIMSEQ_def] "NSsummable f ==> f ----NS> #0";
+by (auto_tac (claset(), simpset() addsimps [NSsummable_NSCauchy]));
+by (dtac bspec 1 THEN Auto_tac);
+by (dres_inst_tac [("x","N + 1hn")] bspec 1);
+by (auto_tac (claset() addIs [HNatInfinite_add_one,
+                              inf_close_hrabs_zero_cancel],
+              simpset() addsimps [rename_numerals hypreal_of_real_zero]));
+qed "NSsummable_NSLIMSEQ_zero";
+
+(* Easy to prove stsandard case now *)
+Goal "summable f ==> f ----> #0";
+by (auto_tac (claset(),
+        simpset() addsimps [summable_NSsummable_iff,
+                            LIMSEQ_NSLIMSEQ_iff, NSsummable_NSLIMSEQ_zero]));
+qed "summable_LIMSEQ_zero";
+
+(*-------------------------------------------------------------------
+                  NS Comparison test
+ -------------------------------------------------------------------*)
+
+Goal "[| EX N. ALL n. N <= n --> abs(f n) <= g n; \
+\        NSsummable g \
+\     |] ==> NSsummable f";
+by (auto_tac (claset() addIs [summable_comparison_test],
+              simpset() addsimps [summable_NSsummable_iff RS sym]));
+qed "NSsummable_comparison_test";
+
+Goal "[| EX N. ALL n. N <= n --> abs(f n) <= g n; \
+\        NSsummable g \
+\     |] ==> NSsummable (%k. abs (f k))";
+by (rtac NSsummable_comparison_test 1);
+by (auto_tac (claset(), simpset() addsimps [abs_idempotent]));
+qed "NSsummable_rabs_comparison_test";
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Hyperreal/HSeries.thy	Sat Dec 30 22:03:47 2000 +0100
@@ -0,0 +1,30 @@
+(*  Title       : HSeries.thy
+    Author      : Jacques D. Fleuriot
+    Copyright   : 1998  University of Cambridge
+    Description : Finite summation and infinite series
+                  for hyperreals
+*) 
+
+HSeries = Series +
+
+consts 
+   sumhr :: "(hypnat * hypnat * (nat=>real)) => hypreal"
+
+defs
+   sumhr_def
+   "sumhr p
+       == Abs_hypreal(UN X:Rep_hypnat(fst p). 
+              UN Y: Rep_hypnat(fst(snd p)).
+              hyprel ^^{%n::nat. sumr (X n) (Y n) (snd(snd p))})"
+
+constdefs
+   NSsums  :: [nat=>real,real] => bool     (infixr 80)
+   "f NSsums s  == (%n. sumr 0 n f) ----NS> s"
+
+   NSsummable :: (nat=>real) => bool
+   "NSsummable f == (EX s. f NSsums s)"
+
+   NSsuminf   :: (nat=>real) => real
+   "NSsuminf f == (@s. f NSsums s)"
+
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Hyperreal/HyperArith.thy	Sat Dec 30 22:03:47 2000 +0100
@@ -0,0 +1,6 @@
+theory HyperArith = HyperArith0
+files "hypreal_arith.ML":
+
+setup hypreal_arith_setup
+
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Hyperreal/HyperArith0.ML	Sat Dec 30 22:03:47 2000 +0100
@@ -0,0 +1,679 @@
+(*  Title:      HOL/Hyperreal/HyperRealArith0.ML
+    ID:         $Id$
+    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
+    Copyright   1999  University of Cambridge
+
+Assorted facts that need binary literals and the arithmetic decision procedure
+
+Also, common factor cancellation
+*)
+
+Goal "((x * y = #0) = (x = #0 | y = (#0::hypreal)))";
+by Auto_tac;  
+by (cut_inst_tac [("x","x"),("y","y")] hypreal_mult_zero_disj 1);
+by Auto_tac;  
+qed "hypreal_mult_is_0";
+AddIffs [hypreal_mult_is_0];
+
+(** Division and inverse **)
+
+Goal "#0/x = (#0::hypreal)";
+by (simp_tac (simpset() addsimps [hypreal_divide_def]) 1); 
+qed "hypreal_0_divide";
+Addsimps [hypreal_0_divide];
+
+Goal "((#0::hypreal) < inverse x) = (#0 < x)";
+by (case_tac "x=#0" 1);
+by (asm_simp_tac (HOL_ss addsimps [rename_numerals HYPREAL_INVERSE_ZERO]) 1); 
+by (auto_tac (claset() addDs [hypreal_inverse_less_0], 
+              simpset() addsimps [linorder_neq_iff, 
+                                  hypreal_inverse_gt_0]));  
+qed "hypreal_0_less_inverse_iff";
+Addsimps [hypreal_0_less_inverse_iff];
+
+Goal "(inverse x < (#0::hypreal)) = (x < #0)";
+by (case_tac "x=#0" 1);
+by (asm_simp_tac (HOL_ss addsimps [rename_numerals HYPREAL_INVERSE_ZERO]) 1); 
+by (auto_tac (claset() addDs [hypreal_inverse_less_0], 
+              simpset() addsimps [linorder_neq_iff, 
+                                  hypreal_inverse_gt_0]));  
+qed "hypreal_inverse_less_0_iff";
+Addsimps [hypreal_inverse_less_0_iff];
+
+Goal "((#0::hypreal) <= inverse x) = (#0 <= x)";
+by (simp_tac (simpset() addsimps [linorder_not_less RS sym]) 1); 
+qed "hypreal_0_le_inverse_iff";
+Addsimps [hypreal_0_le_inverse_iff];
+
+Goal "(inverse x <= (#0::hypreal)) = (x <= #0)";
+by (simp_tac (simpset() addsimps [linorder_not_less RS sym]) 1); 
+qed "hypreal_inverse_le_0_iff";
+Addsimps [hypreal_inverse_le_0_iff];
+
+Goalw [hypreal_divide_def] "x/(#0::hypreal) = #0";
+by (stac (rename_numerals HYPREAL_INVERSE_ZERO) 1); 
+by (Simp_tac 1); 
+qed "HYPREAL_DIVIDE_ZERO";
+
+Goal "inverse (x::hypreal) = #1/x";
+by (simp_tac (simpset() addsimps [hypreal_divide_def]) 1); 
+qed "hypreal_inverse_eq_divide";
+
+Goal "((#0::hypreal) < x/y) = (#0 < x & #0 < y | x < #0 & y < #0)";
+by (simp_tac (simpset() addsimps [hypreal_divide_def, hypreal_0_less_mult_iff]) 1);
+qed "hypreal_0_less_divide_iff";
+Addsimps [inst "x" "number_of ?w" hypreal_0_less_divide_iff];
+
+Goal "(x/y < (#0::hypreal)) = (#0 < x & y < #0 | x < #0 & #0 < y)";
+by (simp_tac (simpset() addsimps [hypreal_divide_def, hypreal_mult_less_0_iff]) 1);
+qed "hypreal_divide_less_0_iff";
+Addsimps [inst "x" "number_of ?w" hypreal_divide_less_0_iff];
+
+Goal "((#0::hypreal) <= x/y) = ((x <= #0 | #0 <= y) & (#0 <= x | y <= #0))";
+by (simp_tac (simpset() addsimps [hypreal_divide_def, hypreal_0_le_mult_iff]) 1);
+by Auto_tac;  
+qed "hypreal_0_le_divide_iff";
+Addsimps [inst "x" "number_of ?w" hypreal_0_le_divide_iff];
+
+Goal "(x/y <= (#0::hypreal)) = ((x <= #0 | y <= #0) & (#0 <= x | #0 <= y))";
+by (simp_tac (simpset() addsimps [hypreal_divide_def, 
+                                  hypreal_mult_le_0_iff]) 1);
+by Auto_tac;  
+qed "hypreal_divide_le_0_iff";
+Addsimps [inst "x" "number_of ?w" hypreal_divide_le_0_iff];
+
+Goal "(inverse(x::hypreal) = #0) = (x = #0)";
+by (auto_tac (claset(), 
+              simpset() addsimps [rename_numerals HYPREAL_INVERSE_ZERO]));  
+by (rtac ccontr 1); 
+by (blast_tac (claset() addDs [rename_numerals hypreal_inverse_not_zero]) 1); 
+qed "hypreal_inverse_zero_iff";
+Addsimps [hypreal_inverse_zero_iff];
+
+Goal "(x/y = #0) = (x=#0 | y=(#0::hypreal))";
+by (auto_tac (claset(), simpset() addsimps [hypreal_divide_def]));  
+qed "hypreal_divide_eq_0_iff";
+Addsimps [hypreal_divide_eq_0_iff];
+
+Goal "h ~= (#0::hypreal) ==> h/h = #1";
+by (asm_simp_tac 
+    (simpset() addsimps [hypreal_divide_def, hypreal_mult_inverse_left]) 1);
+qed "hypreal_divide_self_eq"; 
+Addsimps [hypreal_divide_self_eq];
+
+
+(**** Factor cancellation theorems for "hypreal" ****)
+
+(** Cancellation laws for k*m < k*n and m*k < n*k, also for <= and =,
+    but not (yet?) for k*m < n*k. **)
+
+bind_thm ("hypreal_mult_minus_right", hypreal_minus_mult_eq2 RS sym);
+
+Goal "(-y < -x) = ((x::hypreal) < y)";
+by (arith_tac 1);
+qed "hypreal_minus_less_minus";
+Addsimps [hypreal_minus_less_minus];
+
+Goal "[| i<j;  k < (0::hypreal) |] ==> j*k < i*k";
+by (rtac (hypreal_minus_less_minus RS iffD1) 1);
+by (auto_tac (claset(), 
+              simpset() delsimps [hypreal_minus_mult_eq2 RS sym]
+                        addsimps [hypreal_minus_mult_eq2,
+                                  hypreal_mult_less_mono1])); 
+qed "hypreal_mult_less_mono1_neg";
+
+Goal "[| i<j;  k < (0::hypreal) |] ==> k*j < k*i";
+by (rtac (hypreal_minus_less_minus RS iffD1) 1);
+by (auto_tac (claset(), 
+              simpset() delsimps [hypreal_minus_mult_eq1 RS sym]
+                        addsimps [hypreal_minus_mult_eq1,
+                                  hypreal_mult_less_mono2]));
+qed "hypreal_mult_less_mono2_neg";
+
+Goal "[| i <= j;  (0::hypreal) <= k |] ==> i*k <= j*k";
+by (auto_tac (claset(), 
+              simpset() addsimps [order_le_less, hypreal_mult_less_mono1]));  
+qed "hypreal_mult_le_mono1";
+
+Goal "[| i <= j;  k <= (0::hypreal) |] ==> j*k <= i*k";
+by (auto_tac (claset(), 
+          simpset() addsimps [order_le_less, hypreal_mult_less_mono1_neg]));  
+qed "hypreal_mult_le_mono1_neg";
+
+Goal "[| i <= j;  (0::hypreal) <= k |] ==> k*i <= k*j";
+by (dtac hypreal_mult_le_mono1 1);
+by (ALLGOALS (asm_full_simp_tac (simpset() addsimps [hypreal_mult_commute])));
+qed "hypreal_mult_le_mono2";
+
+Goal "[| i <= j;  k <= (0::hypreal) |] ==> k*j <= k*i";
+by (dtac hypreal_mult_le_mono1_neg 1);
+by (ALLGOALS (asm_full_simp_tac (simpset() addsimps [hypreal_mult_commute])));
+qed "hypreal_mult_le_mono2_neg";
+
+Goal "[| u <= v;  x <= y;  0 <= v;  (0::hypreal) <= x |] ==> u * x <= v * y";
+by (etac (hypreal_mult_le_mono1 RS order_trans) 1);
+by (assume_tac 1);
+by (etac hypreal_mult_le_mono2 1);
+by (assume_tac 1);
+qed "hypreal_mult_le_mono";
+
+Goal "(m*k < n*k) = (((#0::hypreal) < k & m<n) | (k < #0 & n<m))";
+by (case_tac "k = (0::hypreal)" 1);
+by (auto_tac (claset(), 
+          simpset() addsimps [linorder_neq_iff, 
+                      hypreal_mult_less_mono1, hypreal_mult_less_mono1_neg]));  
+by (auto_tac (claset(), 
+              simpset() addsimps [linorder_not_less,
+				  inst "y1" "m*k" (linorder_not_le RS sym),
+                                  inst "y1" "m" (linorder_not_le RS sym)]));
+by (TRYALL (etac notE));
+by (auto_tac (claset(), 
+              simpset() addsimps [order_less_imp_le, hypreal_mult_le_mono1,
+                                  hypreal_mult_le_mono1_neg]));  
+qed "hypreal_mult_less_cancel2";
+
+Goal "(m*k <= n*k) = (((#0::hypreal) < k --> m<=n) & (k < #0 --> n<=m))";
+by (simp_tac (simpset() addsimps [linorder_not_less RS sym, 
+                                  hypreal_mult_less_cancel2]) 1);
+qed "hypreal_mult_le_cancel2";
+
+Goal "(k*m < k*n) = (((#0::hypreal) < k & m<n) | (k < #0 & n<m))";
+by (simp_tac (simpset() addsimps [inst "z" "k" hypreal_mult_commute, 
+                                  hypreal_mult_less_cancel2]) 1);
+qed "hypreal_mult_less_cancel1";
+
+Goal "!!k::hypreal. (k*m <= k*n) = ((#0 < k --> m<=n) & (k < #0 --> n<=m))";
+by (simp_tac (simpset() addsimps [linorder_not_less RS sym, 
+                                  hypreal_mult_less_cancel1]) 1);
+qed "hypreal_mult_le_cancel1";
+
+Goal "!!k::hypreal. (k*m = k*n) = (k = #0 | m=n)";
+by (case_tac "k=0" 1);
+by (auto_tac (claset(), simpset() addsimps [hypreal_mult_left_cancel]));  
+qed "hypreal_mult_eq_cancel1";
+
+Goal "!!k::hypreal. (m*k = n*k) = (k = #0 | m=n)";
+by (case_tac "k=0" 1);
+by (auto_tac (claset(), simpset() addsimps [hypreal_mult_right_cancel]));  
+qed "hypreal_mult_eq_cancel2";
+
+Goal "!!k::hypreal. k~=#0 ==> (k*m) / (k*n) = (m/n)";
+by (asm_simp_tac
+    (simpset() addsimps [hypreal_divide_def, hypreal_inverse_distrib]) 1); 
+by (subgoal_tac "k * m * (inverse k * inverse n) = \
+\                (k * inverse k) * (m * inverse n)" 1);
+by (asm_full_simp_tac (simpset() addsimps []) 1); 
+by (asm_full_simp_tac (HOL_ss addsimps hypreal_mult_ac) 1); 
+qed "hypreal_mult_div_cancel1";
+
+(*For ExtractCommonTerm*)
+Goal "(k*m) / (k*n) = (if k = (#0::hypreal) then #0 else m/n)";
+by (simp_tac (simpset() addsimps [hypreal_mult_div_cancel1]) 1); 
+qed "hypreal_mult_div_cancel_disj";
+
+
+local
+  open Hyperreal_Numeral_Simprocs
+in
+
+val rel_hypreal_number_of = [eq_hypreal_number_of, less_hypreal_number_of, 
+                          le_hypreal_number_of_eq_not_less];
+
+structure CancelNumeralFactorCommon =
+  struct
+  val mk_coeff		= mk_coeff
+  val dest_coeff	= dest_coeff 1
+  val trans_tac         = trans_tac
+  val norm_tac = ALLGOALS (simp_tac (HOL_ss addsimps mult_plus_1s))
+     THEN ALLGOALS (simp_tac (HOL_ss addsimps bin_simps@hypreal_mult_minus_simps))
+     THEN ALLGOALS
+	  (simp_tac 
+	   (HOL_ss addsimps [eq_hypreal_number_of, mult_hypreal_number_of, 
+                             hypreal_mult_number_of_left]@
+           hypreal_minus_from_mult_simps @ hypreal_mult_ac))
+  val numeral_simp_tac	= 
+         ALLGOALS (simp_tac (HOL_ss addsimps rel_hypreal_number_of@bin_simps))
+  val simplify_meta_eq  = simplify_meta_eq
+  end
+
+structure DivCancelNumeralFactor = CancelNumeralFactorFun
+ (open CancelNumeralFactorCommon
+  val prove_conv = prove_conv "hyprealdiv_cancel_numeral_factor"
+  val mk_bal   = HOLogic.mk_binop "HOL.divide"
+  val dest_bal = HOLogic.dest_bin "HOL.divide" hyprealT
+  val cancel = hypreal_mult_div_cancel1 RS trans
+  val neg_exchanges = false
+)
+
+structure EqCancelNumeralFactor = CancelNumeralFactorFun
+ (open CancelNumeralFactorCommon
+  val prove_conv = prove_conv "hyprealeq_cancel_numeral_factor"
+  val mk_bal   = HOLogic.mk_eq
+  val dest_bal = HOLogic.dest_bin "op =" hyprealT
+  val cancel = hypreal_mult_eq_cancel1 RS trans
+  val neg_exchanges = false
+)
+
+structure LessCancelNumeralFactor = CancelNumeralFactorFun
+ (open CancelNumeralFactorCommon
+  val prove_conv = prove_conv "hyprealless_cancel_numeral_factor"
+  val mk_bal   = HOLogic.mk_binrel "op <"
+  val dest_bal = HOLogic.dest_bin "op <" hyprealT
+  val cancel = hypreal_mult_less_cancel1 RS trans
+  val neg_exchanges = true
+)
+
+structure LeCancelNumeralFactor = CancelNumeralFactorFun
+ (open CancelNumeralFactorCommon
+  val prove_conv = prove_conv "hyprealle_cancel_numeral_factor"
+  val mk_bal   = HOLogic.mk_binrel "op <="
+  val dest_bal = HOLogic.dest_bin "op <=" hyprealT
+  val cancel = hypreal_mult_le_cancel1 RS trans
+  val neg_exchanges = true
+)
+
+val hypreal_cancel_numeral_factors_relations = 
+  map prep_simproc
+   [("hyprealeq_cancel_numeral_factor",
+     prep_pats ["(l::hypreal) * m = n", "(l::hypreal) = m * n"], 
+     EqCancelNumeralFactor.proc),
+    ("hyprealless_cancel_numeral_factor", 
+     prep_pats ["(l::hypreal) * m < n", "(l::hypreal) < m * n"], 
+     LessCancelNumeralFactor.proc),
+    ("hyprealle_cancel_numeral_factor", 
+     prep_pats ["(l::hypreal) * m <= n", "(l::hypreal) <= m * n"], 
+     LeCancelNumeralFactor.proc)];
+
+val hypreal_cancel_numeral_factors_divide = prep_simproc
+	("hyprealdiv_cancel_numeral_factor", 
+	 prep_pats ["((l::hypreal) * m) / n", "(l::hypreal) / (m * n)"], 
+	 DivCancelNumeralFactor.proc);
+
+val hypreal_cancel_numeral_factors = 
+    hypreal_cancel_numeral_factors_relations @ 
+    [hypreal_cancel_numeral_factors_divide];
+
+end;
+
+Addsimprocs hypreal_cancel_numeral_factors;
+
+
+(*examples:
+print_depth 22;
+set timing;
+set trace_simp;
+fun test s = (Goal s; by (Simp_tac 1)); 
+
+test "#0 <= (y::hypreal) * #-2";
+test "#9*x = #12 * (y::hypreal)";
+test "(#9*x) / (#12 * (y::hypreal)) = z";
+test "#9*x < #12 * (y::hypreal)";
+test "#9*x <= #12 * (y::hypreal)";
+
+test "#-99*x = #132 * (y::hypreal)";
+test "(#-99*x) / (#132 * (y::hypreal)) = z";
+test "#-99*x < #132 * (y::hypreal)";
+test "#-99*x <= #132 * (y::hypreal)";
+
+test "#999*x = #-396 * (y::hypreal)";
+test "(#999*x) / (#-396 * (y::hypreal)) = z";
+test "#999*x < #-396 * (y::hypreal)";
+test "#999*x <= #-396 * (y::hypreal)";
+
+test "#-99*x = #-81 * (y::hypreal)";
+test "(#-99*x) / (#-81 * (y::hypreal)) = z";
+test "#-99*x <= #-81 * (y::hypreal)";
+test "#-99*x < #-81 * (y::hypreal)";
+
+test "#-2 * x = #-1 * (y::hypreal)";
+test "#-2 * x = -(y::hypreal)";
+test "(#-2 * x) / (#-1 * (y::hypreal)) = z";
+test "#-2 * x < -(y::hypreal)";
+test "#-2 * x <= #-1 * (y::hypreal)";
+test "-x < #-23 * (y::hypreal)";
+test "-x <= #-23 * (y::hypreal)";
+*)
+
+
+(** Declarations for ExtractCommonTerm **)
+
+local
+  open Hyperreal_Numeral_Simprocs
+in
+
+structure CancelFactorCommon =
+  struct
+  val mk_sum    	= long_mk_prod
+  val dest_sum		= dest_prod
+  val mk_coeff		= mk_coeff
+  val dest_coeff	= dest_coeff
+  val find_first	= find_first []
+  val trans_tac         = trans_tac
+  val norm_tac = ALLGOALS (simp_tac (HOL_ss addsimps mult_1s@hypreal_mult_ac))
+  end;
+
+structure EqCancelFactor = ExtractCommonTermFun
+ (open CancelFactorCommon
+  val prove_conv = prove_conv "hypreal_eq_cancel_factor"
+  val mk_bal   = HOLogic.mk_eq
+  val dest_bal = HOLogic.dest_bin "op =" hyprealT
+  val simplify_meta_eq  = cancel_simplify_meta_eq hypreal_mult_eq_cancel1
+);
+
+
+structure DivideCancelFactor = ExtractCommonTermFun
+ (open CancelFactorCommon
+  val prove_conv = prove_conv "hypreal_divide_cancel_factor"
+  val mk_bal   = HOLogic.mk_binop "HOL.divide"
+  val dest_bal = HOLogic.dest_bin "HOL.divide" hyprealT
+  val simplify_meta_eq  = cancel_simplify_meta_eq hypreal_mult_div_cancel_disj
+);
+
+val hypreal_cancel_factor = 
+  map prep_simproc
+   [("hypreal_eq_cancel_factor",
+     prep_pats ["(l::hypreal) * m = n", "(l::hypreal) = m * n"], 
+     EqCancelFactor.proc),
+    ("hypreal_divide_cancel_factor", 
+     prep_pats ["((l::hypreal) * m) / n", "(l::hypreal) / (m * n)"], 
+     DivideCancelFactor.proc)];
+
+end;
+
+Addsimprocs hypreal_cancel_factor;
+
+
+(*examples:
+print_depth 22;
+set timing;
+set trace_simp;
+fun test s = (Goal s; by (Asm_simp_tac 1)); 
+
+test "x*k = k*(y::hypreal)";
+test "k = k*(y::hypreal)"; 
+test "a*(b*c) = (b::hypreal)";
+test "a*(b*c) = d*(b::hypreal)*(x*a)";
+
+
+test "(x*k) / (k*(y::hypreal)) = (uu::hypreal)";
+test "(k) / (k*(y::hypreal)) = (uu::hypreal)"; 
+test "(a*(b*c)) / ((b::hypreal)) = (uu::hypreal)";
+test "(a*(b*c)) / (d*(b::hypreal)*(x*a)) = (uu::hypreal)";
+
+(*FIXME: what do we do about this?*)
+test "a*(b*c)/(y*z) = d*(b::hypreal)*(x*a)/z";
+*)
+
+
+(*** Simplification of inequalities involving literal divisors ***)
+
+Goal "#0<z ==> ((x::hypreal) <= y/z) = (x*z <= y)";
+by (subgoal_tac "(x*z <= y) = (x*z <= (y/z)*z)" 1);
+by (asm_simp_tac (simpset() addsimps [hypreal_divide_def, hypreal_mult_assoc]) 2); 
+by (etac ssubst 1);
+by (stac hypreal_mult_le_cancel2 1); 
+by (Asm_simp_tac 1); 
+qed "pos_hypreal_le_divide_eq";
+Addsimps [inst "z" "number_of ?w" pos_hypreal_le_divide_eq];
+
+Goal "z<#0 ==> ((x::hypreal) <= y/z) = (y <= x*z)";
+by (subgoal_tac "(y <= x*z) = ((y/z)*z <= x*z)" 1);
+by (asm_simp_tac (simpset() addsimps [hypreal_divide_def, hypreal_mult_assoc]) 2); 
+by (etac ssubst 1);
+by (stac hypreal_mult_le_cancel2 1); 
+by (Asm_simp_tac 1); 
+qed "neg_hypreal_le_divide_eq";
+Addsimps [inst "z" "number_of ?w" neg_hypreal_le_divide_eq];
+
+Goal "#0<z ==> (y/z <= (x::hypreal)) = (y <= x*z)";
+by (subgoal_tac "(y <= x*z) = ((y/z)*z <= x*z)" 1);
+by (asm_simp_tac (simpset() addsimps [hypreal_divide_def, hypreal_mult_assoc]) 2); 
+by (etac ssubst 1);
+by (stac hypreal_mult_le_cancel2 1); 
+by (Asm_simp_tac 1); 
+qed "pos_hypreal_divide_le_eq";
+Addsimps [inst "z" "number_of ?w" pos_hypreal_divide_le_eq];
+
+Goal "z<#0 ==> (y/z <= (x::hypreal)) = (x*z <= y)";
+by (subgoal_tac "(x*z <= y) = (x*z <= (y/z)*z)" 1);
+by (asm_simp_tac (simpset() addsimps [hypreal_divide_def, hypreal_mult_assoc]) 2); 
+by (etac ssubst 1);
+by (stac hypreal_mult_le_cancel2 1); 
+by (Asm_simp_tac 1); 
+qed "neg_hypreal_divide_le_eq";
+Addsimps [inst "z" "number_of ?w" neg_hypreal_divide_le_eq];
+
+Goal "#0<z ==> ((x::hypreal) < y/z) = (x*z < y)";
+by (subgoal_tac "(x*z < y) = (x*z < (y/z)*z)" 1);
+by (asm_simp_tac (simpset() addsimps [hypreal_divide_def, hypreal_mult_assoc]) 2); 
+by (etac ssubst 1);
+by (stac hypreal_mult_less_cancel2 1); 
+by (Asm_simp_tac 1); 
+qed "pos_hypreal_less_divide_eq";
+Addsimps [inst "z" "number_of ?w" pos_hypreal_less_divide_eq];
+
+Goal "z<#0 ==> ((x::hypreal) < y/z) = (y < x*z)";
+by (subgoal_tac "(y < x*z) = ((y/z)*z < x*z)" 1);
+by (asm_simp_tac (simpset() addsimps [hypreal_divide_def, hypreal_mult_assoc]) 2); 
+by (etac ssubst 1);
+by (stac hypreal_mult_less_cancel2 1); 
+by (Asm_simp_tac 1); 
+qed "neg_hypreal_less_divide_eq";
+Addsimps [inst "z" "number_of ?w" neg_hypreal_less_divide_eq];
+
+Goal "#0<z ==> (y/z < (x::hypreal)) = (y < x*z)";
+by (subgoal_tac "(y < x*z) = ((y/z)*z < x*z)" 1);
+by (asm_simp_tac (simpset() addsimps [hypreal_divide_def, hypreal_mult_assoc]) 2); 
+by (etac ssubst 1);
+by (stac hypreal_mult_less_cancel2 1); 
+by (Asm_simp_tac 1); 
+qed "pos_hypreal_divide_less_eq";
+Addsimps [inst "z" "number_of ?w" pos_hypreal_divide_less_eq];
+
+Goal "z<#0 ==> (y/z < (x::hypreal)) = (x*z < y)";
+by (subgoal_tac "(x*z < y) = (x*z < (y/z)*z)" 1);
+by (asm_simp_tac (simpset() addsimps [hypreal_divide_def, hypreal_mult_assoc]) 2); 
+by (etac ssubst 1);
+by (stac hypreal_mult_less_cancel2 1); 
+by (Asm_simp_tac 1); 
+qed "neg_hypreal_divide_less_eq";
+Addsimps [inst "z" "number_of ?w" neg_hypreal_divide_less_eq];
+
+Goal "z~=#0 ==> ((x::hypreal) = y/z) = (x*z = y)";
+by (subgoal_tac "(x*z = y) = (x*z = (y/z)*z)" 1);
+by (asm_simp_tac (simpset() addsimps [hypreal_divide_def, hypreal_mult_assoc]) 2); 
+by (etac ssubst 1);
+by (stac hypreal_mult_eq_cancel2 1); 
+by (Asm_simp_tac 1); 
+qed "hypreal_eq_divide_eq";
+Addsimps [inst "z" "number_of ?w" hypreal_eq_divide_eq];
+
+Goal "z~=#0 ==> (y/z = (x::hypreal)) = (y = x*z)";
+by (subgoal_tac "(y = x*z) = ((y/z)*z = x*z)" 1);
+by (asm_simp_tac (simpset() addsimps [hypreal_divide_def, hypreal_mult_assoc]) 2); 
+by (etac ssubst 1);
+by (stac hypreal_mult_eq_cancel2 1); 
+by (Asm_simp_tac 1); 
+qed "hypreal_divide_eq_eq";
+Addsimps [inst "z" "number_of ?w" hypreal_divide_eq_eq];
+
+Goal "(m/k = n/k) = (k = #0 | m = (n::hypreal))";
+by (case_tac "k=#0" 1);
+by (asm_simp_tac (simpset() addsimps [HYPREAL_DIVIDE_ZERO]) 1); 
+by (asm_simp_tac (simpset() addsimps [hypreal_divide_eq_eq, hypreal_eq_divide_eq, 
+                                      hypreal_mult_eq_cancel2]) 1); 
+qed "hypreal_divide_eq_cancel2";
+
+Goal "(k/m = k/n) = (k = #0 | m = (n::hypreal))";
+by (case_tac "m=#0 | n = #0" 1);
+by (auto_tac (claset(), 
+              simpset() addsimps [HYPREAL_DIVIDE_ZERO, hypreal_divide_eq_eq, 
+                                  hypreal_eq_divide_eq, hypreal_mult_eq_cancel1]));  
+qed "hypreal_divide_eq_cancel1";
+
+Goal "[| #0 < r; #0 < x|] ==> (inverse x < inverse (r::hypreal)) = (r < x)";
+by (auto_tac (claset() addIs [hypreal_inverse_less_swap], simpset()));
+by (res_inst_tac [("t","r")] (hypreal_inverse_inverse RS subst) 1);
+by (res_inst_tac [("t","x")] (hypreal_inverse_inverse RS subst) 1);
+by (auto_tac (claset() addIs [hypreal_inverse_less_swap],
+	      simpset() delsimps [hypreal_inverse_inverse]
+			addsimps [hypreal_inverse_gt_zero]));
+qed "hypreal_inverse_less_iff";
+
+Goal "[| #0 < r; #0 < x|] ==> (inverse x <= inverse r) = (r <= (x::hypreal))";
+by (asm_simp_tac (simpset() addsimps [linorder_not_less RS sym, 
+                                      hypreal_inverse_less_iff]) 1); 
+qed "hypreal_inverse_le_iff";
+
+(** Division by 1, -1 **)
+
+Goal "(x::hypreal)/#1 = x";
+by (simp_tac (simpset() addsimps [hypreal_divide_def]) 1); 
+qed "hypreal_divide_1";
+Addsimps [hypreal_divide_1];
+
+Goal "x/#-1 = -(x::hypreal)";
+by (Simp_tac 1); 
+qed "hypreal_divide_minus1";
+Addsimps [hypreal_divide_minus1];
+
+Goal "#-1/(x::hypreal) = - (#1/x)";
+by (simp_tac (simpset() addsimps [hypreal_divide_def, hypreal_minus_inverse]) 1); 
+qed "hypreal_minus1_divide";
+Addsimps [hypreal_minus1_divide];
+
+Goal "[| (#0::hypreal) < d1; #0 < d2 |] ==> EX e. #0 < e & e < d1 & e < d2";
+by (res_inst_tac [("x","(min d1 d2)/#2")] exI 1); 
+by (asm_simp_tac (simpset() addsimps [min_def]) 1); 
+qed "hypreal_lbound_gt_zero";
+
+
+(*** General rewrites to improve automation, like those for type "int" ***)
+
+(** The next several equations can make the simplifier loop! **)
+
+Goal "(x < - y) = (y < - (x::hypreal))";
+by Auto_tac;  
+qed "hypreal_less_minus"; 
+
+Goal "(- x < y) = (- y < (x::hypreal))";
+by Auto_tac;  
+qed "hypreal_minus_less"; 
+
+Goal "(x <= - y) = (y <= - (x::hypreal))";
+by Auto_tac;  
+qed "hypreal_le_minus"; 
+
+Goal "(- x <= y) = (- y <= (x::hypreal))";
+by Auto_tac;  
+qed "hypreal_minus_le"; 
+
+Goal "(x = - y) = (y = - (x::hypreal))";
+by Auto_tac;
+qed "hypreal_equation_minus";
+
+Goal "(- x = y) = (- (y::hypreal) = x)";
+by Auto_tac;
+qed "hypreal_minus_equation";
+
+Goal "(x + - a = (#0::hypreal)) = (x=a)";
+by (arith_tac 1);
+qed "hypreal_add_minus_iff";
+Addsimps [hypreal_add_minus_iff];
+
+Goal "(-b = -a) = (b = (a::hypreal))";
+by (arith_tac 1);
+qed "hypreal_minus_eq_cancel";
+Addsimps [hypreal_minus_eq_cancel];
+
+Goal "(-s <= -r) = ((r::hypreal) <= s)";
+by (stac hypreal_minus_le 1); 
+by (Simp_tac 1); 
+qed "hypreal_le_minus_iff";
+Addsimps [hypreal_le_minus_iff];          
+
+
+(*Distributive laws for literals*)
+Addsimps (map (inst "w" "number_of ?v")
+	  [hypreal_add_mult_distrib, hypreal_add_mult_distrib2,
+	   hypreal_diff_mult_distrib, hypreal_diff_mult_distrib2]);
+
+Addsimps (map (inst "x" "number_of ?v") 
+	  [hypreal_less_minus, hypreal_le_minus, hypreal_equation_minus]);
+Addsimps (map (inst "y" "number_of ?v") 
+	  [hypreal_minus_less, hypreal_minus_le, hypreal_minus_equation]);
+
+
+(*** Simprules combining x+y and #0 ***)
+
+Goal "(x+y = (#0::hypreal)) = (y = -x)";
+by Auto_tac;  
+qed "hypreal_add_eq_0_iff";
+AddIffs [hypreal_add_eq_0_iff];
+
+Goal "(x+y < (#0::hypreal)) = (y < -x)";
+by Auto_tac;  
+qed "hypreal_add_less_0_iff";
+AddIffs [hypreal_add_less_0_iff];
+
+Goal "((#0::hypreal) < x+y) = (-x < y)";
+by Auto_tac;  
+qed "hypreal_0_less_add_iff";
+AddIffs [hypreal_0_less_add_iff];
+
+Goal "(x+y <= (#0::hypreal)) = (y <= -x)";
+by Auto_tac;  
+qed "hypreal_add_le_0_iff";
+AddIffs [hypreal_add_le_0_iff];
+
+Goal "((#0::hypreal) <= x+y) = (-x <= y)";
+by Auto_tac;  
+qed "hypreal_0_le_add_iff";
+AddIffs [hypreal_0_le_add_iff];
+
+
+(** Simprules combining x-y and #0; see also hypreal_less_iff_diff_less_0 etc
+    in HyperBin
+**)
+
+Goal "((#0::hypreal) < x-y) = (y < x)";
+by Auto_tac;  
+qed "hypreal_0_less_diff_iff";
+AddIffs [hypreal_0_less_diff_iff];
+
+Goal "((#0::hypreal) <= x-y) = (y <= x)";
+by Auto_tac;  
+qed "hypreal_0_le_diff_iff";
+AddIffs [hypreal_0_le_diff_iff];
+
+(*
+FIXME: we should have this, as for type int, but many proofs would break.
+It replaces x+-y by x-y.
+Addsimps [symmetric hypreal_diff_def];
+*)
+
+Goal "-(x-y) = y - (x::hypreal)";
+by (arith_tac 1);
+qed "hypreal_minus_diff_eq";
+Addsimps [hypreal_minus_diff_eq];
+
+
+(*** Density of the Hyperreals ***)
+
+Goal "x < y ==> x < (x+y) / (#2::hypreal)";
+by Auto_tac;
+qed "hypreal_less_half_sum";
+
+Goal "x < y ==> (x+y)/(#2::hypreal) < y";
+by Auto_tac;
+qed "hypreal_gt_half_sum";
+
+Goal "x < y ==> EX r::hypreal. x < r & r < y";
+by (blast_tac (claset() addSIs [hypreal_less_half_sum, hypreal_gt_half_sum]) 1);
+qed "hypreal_dense";
+
+
+(*Replaces "inverse #nn" by #1/#nn *)
+Addsimps [inst "x" "number_of ?w" hypreal_inverse_eq_divide];
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Hyperreal/HyperArith0.thy	Sat Dec 30 22:03:47 2000 +0100
@@ -0,0 +1,6 @@
+theory HyperArith0 = HyperBin
+files "hypreal_arith0.ML":
+
+setup hypreal_arith_setup
+
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Hyperreal/HyperBin.ML	Sat Dec 30 22:03:47 2000 +0100
@@ -0,0 +1,626 @@
+(*  Title:      HOL/Hyperreal/HyperBin.ML
+    ID:         $Id$
+    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
+    Copyright   2000  University of Cambridge
+
+Binary arithmetic for the hypreals (integer literals only).
+*)
+
+(** hypreal_of_real (coercion from int to real) **)
+
+Goal "hypreal_of_real (number_of w) = number_of w";
+by (simp_tac (simpset() addsimps [hypreal_number_of_def]) 1);
+qed "hypreal_number_of";
+Addsimps [hypreal_number_of];
+
+Goalw [hypreal_number_of_def] "(0::hypreal) = #0";
+by (simp_tac (simpset() addsimps [hypreal_of_real_zero RS sym]) 1);
+qed "zero_eq_numeral_0";
+
+Goalw [hypreal_number_of_def] "1hr = #1";
+by (simp_tac (simpset() addsimps [hypreal_of_real_one RS sym]) 1);
+qed "one_eq_numeral_1";
+
+(** Addition **)
+
+Goal "(number_of v :: hypreal) + number_of v' = number_of (bin_add v v')";
+by (simp_tac
+    (HOL_ss addsimps [hypreal_number_of_def, 
+                      hypreal_of_real_add RS sym, add_real_number_of]) 1);
+qed "add_hypreal_number_of";
+Addsimps [add_hypreal_number_of];
+
+
+(** Subtraction **)
+
+Goalw [hypreal_number_of_def]
+     "- (number_of w :: hypreal) = number_of (bin_minus w)";
+by (simp_tac
+    (HOL_ss addsimps [minus_real_number_of, hypreal_of_real_minus RS sym]) 1);
+qed "minus_hypreal_number_of";
+Addsimps [minus_hypreal_number_of];
+
+Goalw [hypreal_number_of_def, hypreal_diff_def]
+     "(number_of v :: hypreal) - number_of w = \
+\     number_of (bin_add v (bin_minus w))";
+by (Simp_tac 1); 
+qed "diff_hypreal_number_of";
+Addsimps [diff_hypreal_number_of];
+
+
+(** Multiplication **)
+
+Goal "(number_of v :: hypreal) * number_of v' = number_of (bin_mult v v')";
+by (simp_tac
+    (HOL_ss addsimps [hypreal_number_of_def, 
+	              hypreal_of_real_mult RS sym, mult_real_number_of]) 1);
+qed "mult_hypreal_number_of";
+Addsimps [mult_hypreal_number_of];
+
+Goal "(#2::hypreal) = #1 + #1";
+by (Simp_tac 1);
+val lemma = result();
+
+(*For specialist use: NOT as default simprules*)
+Goal "#2 * z = (z+z::hypreal)";
+by (simp_tac (simpset ()
+	      addsimps [lemma, hypreal_add_mult_distrib,
+			one_eq_numeral_1 RS sym]) 1);
+qed "hypreal_mult_2";
+
+Goal "z * #2 = (z+z::hypreal)";
+by (stac hypreal_mult_commute 1 THEN rtac hypreal_mult_2 1);
+qed "hypreal_mult_2_right";
+
+
+(*** Comparisons ***)
+
+(** Equals (=) **)
+
+Goal "((number_of v :: hypreal) = number_of v') = \
+\     iszero (number_of (bin_add v (bin_minus v')))";
+by (simp_tac
+    (HOL_ss addsimps [hypreal_number_of_def, 
+	              hypreal_of_real_eq_iff, eq_real_number_of]) 1);
+qed "eq_hypreal_number_of";
+Addsimps [eq_hypreal_number_of];
+
+(** Less-than (<) **)
+
+(*"neg" is used in rewrite rules for binary comparisons*)
+Goal "((number_of v :: hypreal) < number_of v') = \
+\     neg (number_of (bin_add v (bin_minus v')))";
+by (simp_tac
+    (HOL_ss addsimps [hypreal_number_of_def, hypreal_of_real_less_iff, 
+		      less_real_number_of]) 1);
+qed "less_hypreal_number_of";
+Addsimps [less_hypreal_number_of];
+
+
+(** Less-than-or-equals (<=) **)
+
+Goal "(number_of x <= (number_of y::hypreal)) = \
+\     (~ number_of y < (number_of x::hypreal))";
+by (rtac (linorder_not_less RS sym) 1);
+qed "le_hypreal_number_of_eq_not_less"; 
+Addsimps [le_hypreal_number_of_eq_not_less];
+
+(*** New versions of existing theorems involving 0, 1hr ***)
+
+Goal "- #1 = (#-1::hypreal)";
+by (Simp_tac 1);
+qed "minus_numeral_one";
+
+(*Maps 0 to #0 and 1hr to #1 and -1hr to #-1*)
+val hypreal_numeral_ss = 
+    real_numeral_ss addsimps [zero_eq_numeral_0, one_eq_numeral_1, 
+		              minus_numeral_one];
+
+fun rename_numerals th = 
+    asm_full_simplify hypreal_numeral_ss (Thm.transfer (the_context ()) th);
+
+
+(*Now insert some identities previously stated for 0 and 1hr*)
+
+(** HyperDef **)
+
+Addsimps (map rename_numerals
+	  [hypreal_minus_zero, hypreal_minus_zero_iff,
+	   hypreal_add_zero_left, hypreal_add_zero_right, 
+	   hypreal_diff_zero, hypreal_diff_zero_right,
+	   hypreal_mult_0_right, hypreal_mult_0, 
+           hypreal_mult_1_right, hypreal_mult_1,
+	   hypreal_inverse_1, hypreal_minus_zero_less_iff]);
+
+bind_thm ("hypreal_0_less_mult_iff", 
+	  rename_numerals hypreal_zero_less_mult_iff);
+bind_thm ("hypreal_0_le_mult_iff", 
+	  rename_numerals hypreal_zero_le_mult_iff);
+bind_thm ("hypreal_mult_less_0_iff", 
+	  rename_numerals hypreal_mult_less_zero_iff);
+bind_thm ("hypreal_mult_le_0_iff", 
+	  rename_numerals hypreal_mult_le_zero_iff);
+
+bind_thm ("hypreal_inverse_less_0", rename_numerals hypreal_inverse_less_zero);
+bind_thm ("hypreal_inverse_gt_0", rename_numerals hypreal_inverse_gt_zero);
+
+Addsimps [zero_eq_numeral_0,one_eq_numeral_1];
+
+
+(** Simplification of arithmetic when nested to the right **)
+
+Goal "number_of v + (number_of w + z) = (number_of(bin_add v w) + z::hypreal)";
+by Auto_tac; 
+qed "hypreal_add_number_of_left";
+
+Goal "number_of v *(number_of w * z) = (number_of(bin_mult v w) * z::hypreal)";
+by (simp_tac (simpset() addsimps [hypreal_mult_assoc RS sym]) 1);
+qed "hypreal_mult_number_of_left";
+
+Goalw [hypreal_diff_def]
+    "number_of v + (number_of w - c) = number_of(bin_add v w) - (c::hypreal)";
+by (rtac hypreal_add_number_of_left 1);
+qed "hypreal_add_number_of_diff1";
+
+Goal "number_of v + (c - number_of w) = \
+\     number_of (bin_add v (bin_minus w)) + (c::hypreal)";
+by (stac (diff_hypreal_number_of RS sym) 1);
+by Auto_tac;
+qed "hypreal_add_number_of_diff2";
+
+Addsimps [hypreal_add_number_of_left, hypreal_mult_number_of_left,
+	  hypreal_add_number_of_diff1, hypreal_add_number_of_diff2]; 
+
+
+(*"neg" is used in rewrite rules for binary comparisons*)
+Goal "hypreal_of_nat (number_of v :: nat) = \
+\        (if neg (number_of v) then #0 \
+\         else (number_of v :: hypreal))";
+by (simp_tac (simpset() addsimps [hypreal_of_nat_real_of_nat]) 1);
+qed "hypreal_of_nat_number_of";
+Addsimps [hypreal_of_nat_number_of];
+
+
+(**** Simprocs for numeric literals ****)
+
+(** Combining of literal coefficients in sums of products **)
+
+Goal "(x < y) = (x-y < (#0::hypreal))";
+by (simp_tac (simpset() addsimps [hypreal_diff_less_eq]) 1);   
+qed "hypreal_less_iff_diff_less_0";
+
+Goal "(x = y) = (x-y = (#0::hypreal))";
+by (simp_tac (simpset() addsimps [hypreal_diff_eq_eq]) 1);   
+qed "hypreal_eq_iff_diff_eq_0";
+
+Goal "(x <= y) = (x-y <= (#0::hypreal))";
+by (simp_tac (simpset() addsimps [hypreal_diff_le_eq]) 1);   
+qed "hypreal_le_iff_diff_le_0";
+
+
+(** For combine_numerals **)
+
+Goal "i*u + (j*u + k) = (i+j)*u + (k::hypreal)";
+by (asm_simp_tac (simpset() addsimps [hypreal_add_mult_distrib]) 1);
+qed "left_hypreal_add_mult_distrib";
+
+
+(** For cancel_numerals **)
+
+val rel_iff_rel_0_rls = 
+    map (inst "y" "?u+?v")
+      [hypreal_less_iff_diff_less_0, hypreal_eq_iff_diff_eq_0, 
+       hypreal_le_iff_diff_le_0] @
+    map (inst "y" "n")
+      [hypreal_less_iff_diff_less_0, hypreal_eq_iff_diff_eq_0, 
+       hypreal_le_iff_diff_le_0];
+
+Goal "!!i::hypreal. (i*u + m = j*u + n) = ((i-j)*u + m = n)";
+by (asm_simp_tac
+    (simpset() addsimps [hypreal_diff_def, hypreal_add_mult_distrib]@
+	                 hypreal_add_ac@rel_iff_rel_0_rls) 1);
+qed "hypreal_eq_add_iff1";
+
+Goal "!!i::hypreal. (i*u + m = j*u + n) = (m = (j-i)*u + n)";
+by (asm_simp_tac
+    (simpset() addsimps [hypreal_diff_def, hypreal_add_mult_distrib]@
+                         hypreal_add_ac@rel_iff_rel_0_rls) 1);
+qed "hypreal_eq_add_iff2";
+
+Goal "!!i::hypreal. (i*u + m < j*u + n) = ((i-j)*u + m < n)";
+by (asm_simp_tac
+    (simpset() addsimps [hypreal_diff_def, hypreal_add_mult_distrib]@
+                         hypreal_add_ac@rel_iff_rel_0_rls) 1);
+qed "hypreal_less_add_iff1";
+
+Goal "!!i::hypreal. (i*u + m < j*u + n) = (m < (j-i)*u + n)";
+by (asm_simp_tac
+    (simpset() addsimps [hypreal_diff_def, hypreal_add_mult_distrib]@
+                         hypreal_add_ac@rel_iff_rel_0_rls) 1);
+qed "hypreal_less_add_iff2";
+
+Goal "!!i::hypreal. (i*u + m <= j*u + n) = ((i-j)*u + m <= n)";
+by (asm_simp_tac
+    (simpset() addsimps [hypreal_diff_def, hypreal_add_mult_distrib]@
+                         hypreal_add_ac@rel_iff_rel_0_rls) 1);
+qed "hypreal_le_add_iff1";
+
+Goal "!!i::hypreal. (i*u + m <= j*u + n) = (m <= (j-i)*u + n)";
+by (asm_simp_tac
+    (simpset() addsimps [hypreal_diff_def, hypreal_add_mult_distrib]@
+                        hypreal_add_ac@rel_iff_rel_0_rls) 1);
+qed "hypreal_le_add_iff2";
+
+Goal "(z::hypreal) * #-1 = -z";
+by (stac (minus_numeral_one RS sym) 1);
+by (stac (hypreal_minus_mult_eq2 RS sym) 1); 
+by Auto_tac;  
+qed "hypreal_mult_minus_1_right";
+Addsimps [hypreal_mult_minus_1_right];
+
+Goal "#-1 * (z::hypreal) = -z";
+by (simp_tac (simpset() addsimps [hypreal_mult_commute]) 1); 
+qed "hypreal_mult_minus_1";
+Addsimps [hypreal_mult_minus_1];
+
+
+
+structure Hyperreal_Numeral_Simprocs =
+struct
+
+(*Utilities*)
+
+
+val hyprealT = Type("HyperDef.hypreal",[]);
+
+fun mk_numeral n = HOLogic.number_of_const hyprealT $ HOLogic.mk_bin n;
+
+val dest_numeral = Real_Numeral_Simprocs.dest_numeral;
+
+val find_first_numeral = Real_Numeral_Simprocs.find_first_numeral;
+
+val zero = mk_numeral 0;
+val mk_plus = HOLogic.mk_binop "op +";
+
+val uminus_const = Const ("uminus", hyprealT --> hyprealT);
+
+(*Thus mk_sum[t] yields t+#0; longer sums don't have a trailing zero*)
+fun mk_sum []        = zero
+  | mk_sum [t,u]     = mk_plus (t, u)
+  | mk_sum (t :: ts) = mk_plus (t, mk_sum ts);
+
+(*this version ALWAYS includes a trailing zero*)
+fun long_mk_sum []        = zero
+  | long_mk_sum (t :: ts) = mk_plus (t, mk_sum ts);
+
+val dest_plus = HOLogic.dest_bin "op +" hyprealT;
+
+(*decompose additions AND subtractions as a sum*)
+fun dest_summing (pos, Const ("op +", _) $ t $ u, ts) =
+        dest_summing (pos, t, dest_summing (pos, u, ts))
+  | dest_summing (pos, Const ("op -", _) $ t $ u, ts) =
+        dest_summing (pos, t, dest_summing (not pos, u, ts))
+  | dest_summing (pos, t, ts) =
+	if pos then t::ts else uminus_const$t :: ts;
+
+fun dest_sum t = dest_summing (true, t, []);
+
+val mk_diff = HOLogic.mk_binop "op -";
+val dest_diff = HOLogic.dest_bin "op -" hyprealT;
+
+val one = mk_numeral 1;
+val mk_times = HOLogic.mk_binop "op *";
+
+fun mk_prod [] = one
+  | mk_prod [t] = t
+  | mk_prod (t :: ts) = if t = one then mk_prod ts
+                        else mk_times (t, mk_prod ts);
+
+val dest_times = HOLogic.dest_bin "op *" hyprealT;
+
+fun dest_prod t =
+      let val (t,u) = dest_times t 
+      in  dest_prod t @ dest_prod u  end
+      handle TERM _ => [t];
+
+(*DON'T do the obvious simplifications; that would create special cases*) 
+fun mk_coeff (k, ts) = mk_times (mk_numeral k, ts);
+
+(*Express t as a product of (possibly) a numeral with other sorted terms*)
+fun dest_coeff sign (Const ("uminus", _) $ t) = dest_coeff (~sign) t
+  | dest_coeff sign t =
+    let val ts = sort Term.term_ord (dest_prod t)
+	val (n, ts') = find_first_numeral [] ts
+                          handle TERM _ => (1, ts)
+    in (sign*n, mk_prod ts') end;
+
+(*Find first coefficient-term THAT MATCHES u*)
+fun find_first_coeff past u [] = raise TERM("find_first_coeff", []) 
+  | find_first_coeff past u (t::terms) =
+	let val (n,u') = dest_coeff 1 t
+	in  if u aconv u' then (n, rev past @ terms)
+			  else find_first_coeff (t::past) u terms
+	end
+	handle TERM _ => find_first_coeff (t::past) u terms;
+
+
+(*Simplify #1*n and n*#1 to n*)
+val add_0s = map rename_numerals
+                 [hypreal_add_zero_left, hypreal_add_zero_right];
+val mult_plus_1s = map rename_numerals
+                      [hypreal_mult_1, hypreal_mult_1_right];
+val mult_minus_1s = map rename_numerals
+                      [hypreal_mult_minus_1, hypreal_mult_minus_1_right];
+val mult_1s = mult_plus_1s @ mult_minus_1s;
+
+(*To perform binary arithmetic*)
+val bin_simps =
+    [add_hypreal_number_of, hypreal_add_number_of_left, 
+     minus_hypreal_number_of, diff_hypreal_number_of, mult_hypreal_number_of, 
+     hypreal_mult_number_of_left] @ bin_arith_simps @ bin_rel_simps;
+
+(*To evaluate binary negations of coefficients*)
+val hypreal_minus_simps = NCons_simps @
+                   [minus_hypreal_number_of, 
+		    bin_minus_1, bin_minus_0, bin_minus_Pls, bin_minus_Min,
+		    bin_pred_1, bin_pred_0, bin_pred_Pls, bin_pred_Min];
+
+(*To let us treat subtraction as addition*)
+val diff_simps = [hypreal_diff_def, hypreal_minus_add_distrib, 
+                  hypreal_minus_minus];
+
+(*push the unary minus down: - x * y = x * - y *)
+val hypreal_minus_mult_eq_1_to_2 = 
+    [hypreal_minus_mult_eq1 RS sym, hypreal_minus_mult_eq2] MRS trans 
+    |> standard;
+
+(*to extract again any uncancelled minuses*)
+val hypreal_minus_from_mult_simps = 
+    [hypreal_minus_minus, hypreal_minus_mult_eq1 RS sym, 
+     hypreal_minus_mult_eq2 RS sym];
+
+(*combine unary minus with numeric literals, however nested within a product*)
+val hypreal_mult_minus_simps =
+    [hypreal_mult_assoc, hypreal_minus_mult_eq1, hypreal_minus_mult_eq_1_to_2];
+
+(*Apply the given rewrite (if present) just once*)
+fun trans_tac None      = all_tac
+  | trans_tac (Some th) = ALLGOALS (rtac (th RS trans));
+
+fun prove_conv name tacs sg (hyps: thm list) (t,u) =
+  if t aconv u then None
+  else
+  let val ct = cterm_of sg (HOLogic.mk_Trueprop (HOLogic.mk_eq (t, u)))
+  in Some
+     (prove_goalw_cterm [] ct (K tacs)
+      handle ERROR => error 
+	  ("The error(s) above occurred while trying to prove " ^
+	   string_of_cterm ct ^ "\nInternal failure of simproc " ^ name))
+  end;
+
+(*version without the hyps argument*)
+fun prove_conv_nohyps name tacs sg = prove_conv name tacs sg [];
+
+(*Final simplification: cancel + and *  *)
+val simplify_meta_eq = 
+    Int_Numeral_Simprocs.simplify_meta_eq
+         [hypreal_add_zero_left, hypreal_add_zero_right,
+ 	  hypreal_mult_0, hypreal_mult_0_right, hypreal_mult_1, 
+          hypreal_mult_1_right];
+
+val prep_simproc = Real_Numeral_Simprocs.prep_simproc;
+val prep_pats = map Real_Numeral_Simprocs.prep_pat;
+
+structure CancelNumeralsCommon =
+  struct
+  val mk_sum    	= mk_sum
+  val dest_sum		= dest_sum
+  val mk_coeff		= mk_coeff
+  val dest_coeff	= dest_coeff 1
+  val find_first_coeff	= find_first_coeff []
+  val trans_tac         = trans_tac
+  val norm_tac = 
+     ALLGOALS (simp_tac (HOL_ss addsimps add_0s@mult_1s@diff_simps@
+                                         hypreal_minus_simps@hypreal_add_ac))
+     THEN ALLGOALS (simp_tac (HOL_ss addsimps bin_simps@hypreal_mult_minus_simps))
+     THEN ALLGOALS
+              (simp_tac (HOL_ss addsimps hypreal_minus_from_mult_simps@
+                                         hypreal_add_ac@hypreal_mult_ac))
+  val numeral_simp_tac	= ALLGOALS (simp_tac (HOL_ss addsimps add_0s@bin_simps))
+  val simplify_meta_eq  = simplify_meta_eq
+  end;
+
+
+structure EqCancelNumerals = CancelNumeralsFun
+ (open CancelNumeralsCommon
+  val prove_conv = prove_conv "hyprealeq_cancel_numerals"
+  val mk_bal   = HOLogic.mk_eq
+  val dest_bal = HOLogic.dest_bin "op =" hyprealT
+  val bal_add1 = hypreal_eq_add_iff1 RS trans
+  val bal_add2 = hypreal_eq_add_iff2 RS trans
+);
+
+structure LessCancelNumerals = CancelNumeralsFun
+ (open CancelNumeralsCommon
+  val prove_conv = prove_conv "hyprealless_cancel_numerals"
+  val mk_bal   = HOLogic.mk_binrel "op <"
+  val dest_bal = HOLogic.dest_bin "op <" hyprealT
+  val bal_add1 = hypreal_less_add_iff1 RS trans
+  val bal_add2 = hypreal_less_add_iff2 RS trans
+);
+
+structure LeCancelNumerals = CancelNumeralsFun
+ (open CancelNumeralsCommon
+  val prove_conv = prove_conv "hyprealle_cancel_numerals"
+  val mk_bal   = HOLogic.mk_binrel "op <="
+  val dest_bal = HOLogic.dest_bin "op <=" hyprealT
+  val bal_add1 = hypreal_le_add_iff1 RS trans
+  val bal_add2 = hypreal_le_add_iff2 RS trans
+);
+
+val cancel_numerals = 
+  map prep_simproc
+   [("hyprealeq_cancel_numerals",
+     prep_pats ["(l::hypreal) + m = n", "(l::hypreal) = m + n", 
+		"(l::hypreal) - m = n", "(l::hypreal) = m - n", 
+		"(l::hypreal) * m = n", "(l::hypreal) = m * n"], 
+     EqCancelNumerals.proc),
+    ("hyprealless_cancel_numerals", 
+     prep_pats ["(l::hypreal) + m < n", "(l::hypreal) < m + n", 
+		"(l::hypreal) - m < n", "(l::hypreal) < m - n", 
+		"(l::hypreal) * m < n", "(l::hypreal) < m * n"], 
+     LessCancelNumerals.proc),
+    ("hyprealle_cancel_numerals", 
+     prep_pats ["(l::hypreal) + m <= n", "(l::hypreal) <= m + n", 
+		"(l::hypreal) - m <= n", "(l::hypreal) <= m - n", 
+		"(l::hypreal) * m <= n", "(l::hypreal) <= m * n"], 
+     LeCancelNumerals.proc)];
+
+
+structure CombineNumeralsData =
+  struct
+  val add		= op + : int*int -> int 
+  val mk_sum    	= long_mk_sum    (*to work for e.g. #2*x + #3*x *)
+  val dest_sum		= dest_sum
+  val mk_coeff		= mk_coeff
+  val dest_coeff	= dest_coeff 1
+  val left_distrib	= left_hypreal_add_mult_distrib RS trans
+  val prove_conv	= prove_conv_nohyps "hypreal_combine_numerals"
+  val trans_tac          = trans_tac
+  val norm_tac = 
+     ALLGOALS (simp_tac (HOL_ss addsimps add_0s@mult_1s@diff_simps@
+                                         hypreal_minus_simps@hypreal_add_ac))
+     THEN ALLGOALS (simp_tac (HOL_ss addsimps bin_simps@hypreal_mult_minus_simps))
+     THEN ALLGOALS (simp_tac (HOL_ss addsimps hypreal_minus_from_mult_simps@
+                                              hypreal_add_ac@hypreal_mult_ac))
+  val numeral_simp_tac	= ALLGOALS 
+                    (simp_tac (HOL_ss addsimps add_0s@bin_simps))
+  val simplify_meta_eq  = simplify_meta_eq
+  end;
+
+structure CombineNumerals = CombineNumeralsFun(CombineNumeralsData);
+  
+val combine_numerals = 
+    prep_simproc ("hypreal_combine_numerals",
+		  prep_pats ["(i::hypreal) + j", "(i::hypreal) - j"],
+		  CombineNumerals.proc);
+
+
+(** Declarations for ExtractCommonTerm **)
+
+(*this version ALWAYS includes a trailing one*)
+fun long_mk_prod []        = one
+  | long_mk_prod (t :: ts) = mk_times (t, mk_prod ts);
+
+(*Find first term that matches u*)
+fun find_first past u []         = raise TERM("find_first", []) 
+  | find_first past u (t::terms) =
+	if u aconv t then (rev past @ terms)
+        else find_first (t::past) u terms
+	handle TERM _ => find_first (t::past) u terms;
+
+(*Final simplification: cancel + and *  *)
+fun cancel_simplify_meta_eq cancel_th th = 
+    Int_Numeral_Simprocs.simplify_meta_eq 
+        [hypreal_mult_1, hypreal_mult_1_right] 
+        (([th, cancel_th]) MRS trans);
+
+end;
+
+Addsimprocs Hyperreal_Numeral_Simprocs.cancel_numerals;
+Addsimprocs [Hyperreal_Numeral_Simprocs.combine_numerals];
+
+(*The Abel_Cancel simprocs are now obsolete*)
+Delsimprocs [Hyperreal_Cancel.sum_conv, Hyperreal_Cancel.rel_conv];
+
+(*examples:
+print_depth 22;
+set timing;
+set trace_simp;
+fun test s = (Goal s, by (Simp_tac 1)); 
+
+test "l + #2 + #2 + #2 + (l + #2) + (oo + #2) = (uu::hypreal)";
+test "#2*u = (u::hypreal)";
+test "(i + j + #12 + (k::hypreal)) - #15 = y";
+test "(i + j + #12 + (k::hypreal)) - #5 = y";
+
+test "y - b < (b::hypreal)";
+test "y - (#3*b + c) < (b::hypreal) - #2*c";
+
+test "(#2*x - (u*v) + y) - v*#3*u = (w::hypreal)";
+test "(#2*x*u*v + (u*v)*#4 + y) - v*u*#4 = (w::hypreal)";
+test "(#2*x*u*v + (u*v)*#4 + y) - v*u = (w::hypreal)";
+test "u*v - (x*u*v + (u*v)*#4 + y) = (w::hypreal)";
+
+test "(i + j + #12 + (k::hypreal)) = u + #15 + y";
+test "(i + j*#2 + #12 + (k::hypreal)) = j + #5 + y";
+
+test "#2*y + #3*z + #6*w + #2*y + #3*z + #2*u = #2*y' + #3*z' + #6*w' + #2*y' + #3*z' + u + (vv::hypreal)";
+
+test "a + -(b+c) + b = (d::hypreal)";
+test "a + -(b+c) - b = (d::hypreal)";
+
+(*negative numerals*)
+test "(i + j + #-2 + (k::hypreal)) - (u + #5 + y) = zz";
+test "(i + j + #-3 + (k::hypreal)) < u + #5 + y";
+test "(i + j + #3 + (k::hypreal)) < u + #-6 + y";
+test "(i + j + #-12 + (k::hypreal)) - #15 = y";
+test "(i + j + #12 + (k::hypreal)) - #-15 = y";
+test "(i + j + #-12 + (k::hypreal)) - #-15 = y";
+*)
+
+
+(** Constant folding for hypreal plus and times **)
+
+(*We do not need
+    structure Hyperreal_Plus_Assoc = Assoc_Fold (Hyperreal_Plus_Assoc_Data);
+  because combine_numerals does the same thing*)
+
+structure Hyperreal_Times_Assoc_Data : ASSOC_FOLD_DATA =
+struct
+  val ss		= HOL_ss
+  val eq_reflection	= eq_reflection
+  val sg_ref    = Sign.self_ref (Theory.sign_of (the_context ()))
+  val T	     = Hyperreal_Numeral_Simprocs.hyprealT
+  val plus   = Const ("op *", [T,T] ---> T)
+  val add_ac = hypreal_mult_ac
+end;
+
+structure Hyperreal_Times_Assoc = Assoc_Fold (Hyperreal_Times_Assoc_Data);
+
+Addsimprocs [Hyperreal_Times_Assoc.conv];
+
+Addsimps [rename_numerals hypreal_of_real_zero_iff];
+
+(*Simplification of  x-y < 0, etc.*)
+AddIffs [hypreal_less_iff_diff_less_0 RS sym];
+AddIffs [hypreal_eq_iff_diff_eq_0 RS sym];
+AddIffs [hypreal_le_iff_diff_le_0 RS sym];
+
+
+(** number_of related to hypreal_of_real **)
+
+Goal "(number_of w < hypreal_of_real z) = (number_of w < z)";
+by (stac (hypreal_of_real_less_iff RS sym) 1); 
+by (Simp_tac 1); 
+qed "number_of_less_hypreal_of_real_iff";
+Addsimps [number_of_less_hypreal_of_real_iff];
+
+Goal "(number_of w <= hypreal_of_real z) = (number_of w <= z)";
+by (stac (hypreal_of_real_le_iff RS sym) 1); 
+by (Simp_tac 1); 
+qed "number_of_le_hypreal_of_real_iff";
+Addsimps [number_of_le_hypreal_of_real_iff];
+
+Goal "(hypreal_of_real z < number_of w) = (z < number_of w)";
+by (stac (hypreal_of_real_less_iff RS sym) 1); 
+by (Simp_tac 1); 
+qed "hypreal_of_real_less_number_of_iff";
+Addsimps [hypreal_of_real_less_number_of_iff];
+
+Goal "(hypreal_of_real z <= number_of w) = (z <= number_of w)";
+by (stac (hypreal_of_real_le_iff RS sym) 1); 
+by (Simp_tac 1); 
+qed "hypreal_of_real_le_number_of_iff";
+Addsimps [hypreal_of_real_le_number_of_iff];
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Hyperreal/HyperBin.thy	Sat Dec 30 22:03:47 2000 +0100
@@ -0,0 +1,21 @@
+(*  Title:      HOL/HyperBin.thy
+    ID:         $Id$
+    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
+    Copyright   1999  University of Cambridge
+
+Binary arithmetic for the hyperreals
+
+This case is reduced to that for the reals.
+*)
+
+HyperBin = HyperOrd +
+
+instance
+  hypreal :: number 
+
+defs
+  hypreal_number_of_def
+    "number_of v == hypreal_of_real (number_of v)"
+     (*::bin=>hypreal               ::bin=>real*)
+
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Hyperreal/HyperDef.ML	Sat Dec 30 22:03:47 2000 +0100
@@ -0,0 +1,1354 @@
+(*  Title       : HOL/Real/Hyperreal/Hyper.ML
+    ID          : $Id$
+    Author      : Jacques D. Fleuriot
+    Copyright   : 1998  University of Cambridge
+    Description : Ultrapower construction of hyperreals
+*) 
+
+(*------------------------------------------------------------------------
+             Proof that the set of naturals is not finite
+ ------------------------------------------------------------------------*)
+
+(*** based on James' proof that the set of naturals is not finite ***)
+Goal "finite (A::nat set) --> (EX n. ALL m. Suc (n + m) ~: A)";
+by (rtac impI 1);
+by (eres_inst_tac [("F","A")] finite_induct 1);
+by (Blast_tac 1 THEN etac exE 1);
+by (res_inst_tac [("x","n + x")] exI 1);
+by (rtac allI 1 THEN eres_inst_tac [("x","x + m")] allE 1);
+by (auto_tac (claset(), simpset() addsimps add_ac));
+by (auto_tac (claset(),
+	      simpset() addsimps [add_assoc RS sym,
+				  less_add_Suc2 RS less_not_refl2]));
+qed_spec_mp "finite_exhausts";
+
+Goal "finite (A :: nat set) --> (EX n. n ~:A)";
+by (rtac impI 1 THEN dtac finite_exhausts 1);
+by (Blast_tac 1);
+qed_spec_mp "finite_not_covers";
+
+Goal "~ finite(UNIV:: nat set)";
+by (fast_tac (claset() addSDs [finite_exhausts]) 1);
+qed "not_finite_nat";
+
+(*------------------------------------------------------------------------
+   Existence of free ultrafilter over the naturals and proof of various 
+   properties of the FreeUltrafilterNat- an arbitrary free ultrafilter
+ ------------------------------------------------------------------------*)
+
+Goal "EX U. U: FreeUltrafilter (UNIV::nat set)";
+by (rtac (not_finite_nat RS FreeUltrafilter_Ex) 1);
+qed "FreeUltrafilterNat_Ex";
+
+Goalw [FreeUltrafilterNat_def] 
+     "FreeUltrafilterNat: FreeUltrafilter(UNIV:: nat set)";
+by (rtac (FreeUltrafilterNat_Ex RS exE) 1);
+by (rtac someI2 1 THEN ALLGOALS(assume_tac));
+qed "FreeUltrafilterNat_mem";
+Addsimps [FreeUltrafilterNat_mem];
+
+Goalw [FreeUltrafilterNat_def] "finite x ==> x ~: FreeUltrafilterNat";
+by (rtac (FreeUltrafilterNat_Ex RS exE) 1);
+by (rtac someI2 1 THEN assume_tac 1);
+by (blast_tac (claset() addDs [mem_FreeUltrafiltersetD1]) 1);
+qed "FreeUltrafilterNat_finite";
+
+Goal "x: FreeUltrafilterNat ==> ~ finite x";
+by (blast_tac (claset() addDs [FreeUltrafilterNat_finite]) 1);
+qed "FreeUltrafilterNat_not_finite";
+
+Goalw [FreeUltrafilterNat_def] "{} ~: FreeUltrafilterNat";
+by (rtac (FreeUltrafilterNat_Ex RS exE) 1);
+by (rtac someI2 1 THEN assume_tac 1);
+by (blast_tac (claset() addDs [FreeUltrafilter_Ultrafilter,
+			       Ultrafilter_Filter,Filter_empty_not_mem]) 1);
+qed "FreeUltrafilterNat_empty";
+Addsimps [FreeUltrafilterNat_empty];
+
+Goal "[| X: FreeUltrafilterNat;  Y: FreeUltrafilterNat |]  \
+\     ==> X Int Y : FreeUltrafilterNat";
+by (cut_facts_tac [FreeUltrafilterNat_mem] 1);
+by (blast_tac (claset() addDs [FreeUltrafilter_Ultrafilter,
+			       Ultrafilter_Filter,mem_FiltersetD1]) 1);
+qed "FreeUltrafilterNat_Int";
+
+Goal "[| X: FreeUltrafilterNat;  X <= Y |] \
+\     ==> Y : FreeUltrafilterNat";
+by (cut_facts_tac [FreeUltrafilterNat_mem] 1);
+by (blast_tac (claset() addDs [FreeUltrafilter_Ultrafilter,
+			       Ultrafilter_Filter,mem_FiltersetD2]) 1);
+qed "FreeUltrafilterNat_subset";
+
+Goal "X: FreeUltrafilterNat ==> -X ~: FreeUltrafilterNat";
+by (Step_tac 1);
+by (dtac FreeUltrafilterNat_Int 1 THEN assume_tac 1);
+by Auto_tac;
+qed "FreeUltrafilterNat_Compl";
+
+Goal "X~: FreeUltrafilterNat ==> -X : FreeUltrafilterNat";
+by (cut_facts_tac [FreeUltrafilterNat_mem RS (FreeUltrafilter_iff RS iffD1)] 1);
+by (Step_tac 1 THEN dres_inst_tac [("x","X")] bspec 1);
+by (auto_tac (claset(), simpset() addsimps [UNIV_diff_Compl]));
+qed "FreeUltrafilterNat_Compl_mem";
+
+Goal "(X ~: FreeUltrafilterNat) = (-X: FreeUltrafilterNat)";
+by (blast_tac (claset() addDs [FreeUltrafilterNat_Compl,
+			       FreeUltrafilterNat_Compl_mem]) 1);
+qed "FreeUltrafilterNat_Compl_iff1";
+
+Goal "(X: FreeUltrafilterNat) = (-X ~: FreeUltrafilterNat)";
+by (auto_tac (claset(),
+	      simpset() addsimps [FreeUltrafilterNat_Compl_iff1 RS sym]));
+qed "FreeUltrafilterNat_Compl_iff2";
+
+Goal "(UNIV::nat set) : FreeUltrafilterNat";
+by (rtac (FreeUltrafilterNat_mem RS FreeUltrafilter_Ultrafilter RS 
+          Ultrafilter_Filter RS mem_FiltersetD4) 1);
+qed "FreeUltrafilterNat_UNIV";
+Addsimps [FreeUltrafilterNat_UNIV];
+
+Goal "UNIV : FreeUltrafilterNat";
+by Auto_tac;
+qed "FreeUltrafilterNat_Nat_set";
+Addsimps [FreeUltrafilterNat_Nat_set];
+
+Goal "{n. P(n) = P(n)} : FreeUltrafilterNat";
+by (Simp_tac 1);
+qed "FreeUltrafilterNat_Nat_set_refl";
+AddIs [FreeUltrafilterNat_Nat_set_refl];
+
+Goal "{n::nat. P} : FreeUltrafilterNat ==> P";
+by (rtac ccontr 1);
+by (rotate_tac 1 1);
+by (Asm_full_simp_tac 1);
+qed "FreeUltrafilterNat_P";
+
+Goal "{n. P(n)} : FreeUltrafilterNat ==> EX n. P(n)";
+by (rtac ccontr 1 THEN rotate_tac 1 1);
+by (Asm_full_simp_tac 1);
+qed "FreeUltrafilterNat_Ex_P";
+
+Goal "ALL n. P(n) ==> {n. P(n)} : FreeUltrafilterNat";
+by (auto_tac (claset() addIs [FreeUltrafilterNat_Nat_set], simpset()));
+qed "FreeUltrafilterNat_all";
+
+(*-------------------------------------------------------
+     Define and use Ultrafilter tactics
+ -------------------------------------------------------*)
+use "fuf.ML";
+
+(*-------------------------------------------------------
+  Now prove one further property of our free ultrafilter
+ -------------------------------------------------------*)
+Goal "X Un Y: FreeUltrafilterNat \
+\     ==> X: FreeUltrafilterNat | Y: FreeUltrafilterNat";
+by Auto_tac;
+by (Ultra_tac 1);
+qed "FreeUltrafilterNat_Un";
+
+(*-------------------------------------------------------
+   Properties of hyprel
+ -------------------------------------------------------*)
+
+(** Proving that hyprel is an equivalence relation **)
+(** Natural deduction for hyprel **)
+
+Goalw [hyprel_def]
+   "((X,Y): hyprel) = ({n. X n = Y n}: FreeUltrafilterNat)";
+by (Fast_tac 1);
+qed "hyprel_iff";
+
+Goalw [hyprel_def] 
+     "{n. X n = Y n}: FreeUltrafilterNat  ==> (X,Y): hyprel";
+by (Fast_tac 1);
+qed "hyprelI";
+
+Goalw [hyprel_def]
+  "p: hyprel --> (EX X Y. \
+\                 p = (X,Y) & {n. X n = Y n} : FreeUltrafilterNat)";
+by (Fast_tac 1);
+qed "hyprelE_lemma";
+
+val [major,minor] = goal (the_context ())
+  "[| p: hyprel;  \
+\     !!X Y. [| p = (X,Y); {n. X n = Y n}: FreeUltrafilterNat\
+\                    |] ==> Q |] ==> Q";
+by (cut_facts_tac [major RS (hyprelE_lemma RS mp)] 1);
+by (REPEAT (eresolve_tac [asm_rl,exE,conjE,minor] 1));
+qed "hyprelE";
+
+AddSIs [hyprelI];
+AddSEs [hyprelE];
+
+Goalw [hyprel_def] "(x,x): hyprel";
+by (auto_tac (claset(),
+              simpset() addsimps [FreeUltrafilterNat_Nat_set]));
+qed "hyprel_refl";
+
+Goal "{n. X n = Y n} = {n. Y n = X n}";
+by Auto_tac;
+qed "lemma_perm";
+
+Goalw [hyprel_def] "(x,y): hyprel --> (y,x):hyprel";
+by (auto_tac (claset() addIs [lemma_perm RS subst], simpset()));
+qed_spec_mp "hyprel_sym";
+
+Goalw [hyprel_def]
+      "(x,y): hyprel --> (y,z):hyprel --> (x,z):hyprel";
+by Auto_tac;
+by (Ultra_tac 1);
+qed_spec_mp "hyprel_trans";
+
+Goalw [equiv_def, refl_def, sym_def, trans_def] "equiv UNIV hyprel";
+by (auto_tac (claset() addSIs [hyprel_refl] 
+                       addSEs [hyprel_sym,hyprel_trans] 
+                       delrules [hyprelI,hyprelE],
+	      simpset() addsimps [FreeUltrafilterNat_Nat_set]));
+qed "equiv_hyprel";
+
+(* (hyprel ^^ {x} = hyprel ^^ {y}) = ((x,y) : hyprel) *)
+bind_thm ("equiv_hyprel_iff",
+    	  [equiv_hyprel, UNIV_I, UNIV_I] MRS eq_equiv_class_iff);
+
+Goalw  [hypreal_def,hyprel_def,quotient_def] "hyprel^^{x}:hypreal";
+by (Blast_tac 1);
+qed "hyprel_in_hypreal";
+
+Goal "inj_on Abs_hypreal hypreal";
+by (rtac inj_on_inverseI 1);
+by (etac Abs_hypreal_inverse 1);
+qed "inj_on_Abs_hypreal";
+
+Addsimps [equiv_hyprel_iff,inj_on_Abs_hypreal RS inj_on_iff,
+          hyprel_iff, hyprel_in_hypreal, Abs_hypreal_inverse];
+
+Addsimps [equiv_hyprel RS eq_equiv_class_iff];
+bind_thm ("eq_hyprelD", equiv_hyprel RSN (2,eq_equiv_class));
+
+Goal "inj(Rep_hypreal)";
+by (rtac inj_inverseI 1);
+by (rtac Rep_hypreal_inverse 1);
+qed "inj_Rep_hypreal";
+
+Goalw [hyprel_def] "x: hyprel ^^ {x}";
+by (Step_tac 1);
+by (auto_tac (claset() addSIs [FreeUltrafilterNat_Nat_set], simpset()));
+qed "lemma_hyprel_refl";
+
+Addsimps [lemma_hyprel_refl];
+
+Goalw [hypreal_def] "{} ~: hypreal";
+by (auto_tac (claset() addSEs [quotientE], simpset()));
+qed "hypreal_empty_not_mem";
+
+Addsimps [hypreal_empty_not_mem];
+
+Goal "Rep_hypreal x ~= {}";
+by (cut_inst_tac [("x","x")] Rep_hypreal 1);
+by Auto_tac;
+qed "Rep_hypreal_nonempty";
+
+Addsimps [Rep_hypreal_nonempty];
+
+(*------------------------------------------------------------------------
+   hypreal_of_real: the injection from real to hypreal
+ ------------------------------------------------------------------------*)
+
+Goal "inj(hypreal_of_real)";
+by (rtac injI 1);
+by (rewtac hypreal_of_real_def);
+by (dtac (inj_on_Abs_hypreal RS inj_onD) 1);
+by (REPEAT (rtac hyprel_in_hypreal 1));
+by (dtac eq_equiv_class 1);
+by (rtac equiv_hyprel 1);
+by (Fast_tac 1);
+by (rtac ccontr 1 THEN rotate_tac 1 1);
+by Auto_tac;
+qed "inj_hypreal_of_real";
+
+val [prem] = goal (the_context ())
+    "(!!x y. z = Abs_hypreal(hyprel^^{x}) ==> P) ==> P";
+by (res_inst_tac [("x1","z")] 
+    (rewrite_rule [hypreal_def] Rep_hypreal RS quotientE) 1);
+by (dres_inst_tac [("f","Abs_hypreal")] arg_cong 1);
+by (res_inst_tac [("x","x")] prem 1);
+by (asm_full_simp_tac (simpset() addsimps [Rep_hypreal_inverse]) 1);
+qed "eq_Abs_hypreal";
+
+(**** hypreal_minus: additive inverse on hypreal ****)
+
+Goalw [congruent_def]
+  "congruent hyprel (%X. hyprel^^{%n. - (X n)})";
+by Safe_tac;
+by (ALLGOALS Ultra_tac);
+qed "hypreal_minus_congruent";
+
+Goalw [hypreal_minus_def]
+   "- (Abs_hypreal(hyprel^^{%n. X n})) = Abs_hypreal(hyprel ^^ {%n. -(X n)})";
+by (res_inst_tac [("f","Abs_hypreal")] arg_cong 1);
+by (simp_tac (simpset() addsimps 
+      [hyprel_in_hypreal RS Abs_hypreal_inverse,
+       [equiv_hyprel, hypreal_minus_congruent] MRS UN_equiv_class]) 1);
+qed "hypreal_minus";
+
+Goal "- (- z) = (z::hypreal)";
+by (res_inst_tac [("z","z")] eq_Abs_hypreal 1);
+by (asm_simp_tac (simpset() addsimps [hypreal_minus]) 1);
+qed "hypreal_minus_minus";
+
+Addsimps [hypreal_minus_minus];
+
+Goal "inj(%r::hypreal. -r)";
+by (rtac injI 1);
+by (dres_inst_tac [("f","uminus")] arg_cong 1);
+by (asm_full_simp_tac (simpset() addsimps [hypreal_minus_minus]) 1);
+qed "inj_hypreal_minus";
+
+Goalw [hypreal_zero_def] "-0 = (0::hypreal)";
+by (simp_tac (simpset() addsimps [hypreal_minus]) 1);
+qed "hypreal_minus_zero";
+Addsimps [hypreal_minus_zero];
+
+Goal "(-x = 0) = (x = (0::hypreal))"; 
+by (res_inst_tac [("z","x")] eq_Abs_hypreal 1);
+by (auto_tac (claset(),
+       simpset() addsimps [hypreal_zero_def, hypreal_minus, eq_commute] @ 
+                          real_add_ac));
+qed "hypreal_minus_zero_iff";
+
+Addsimps [hypreal_minus_zero_iff];
+
+
+(**** hyperreal addition: hypreal_add  ****)
+
+Goalw [congruent2_def]
+    "congruent2 hyprel (%X Y. hyprel^^{%n. X n + Y n})";
+by Safe_tac;
+by (ALLGOALS(Ultra_tac));
+qed "hypreal_add_congruent2";
+
+Goalw [hypreal_add_def]
+  "Abs_hypreal(hyprel^^{%n. X n}) + Abs_hypreal(hyprel^^{%n. Y n}) = \
+\  Abs_hypreal(hyprel^^{%n. X n + Y n})";
+by (simp_tac (simpset() addsimps 
+         [[equiv_hyprel, hypreal_add_congruent2] MRS UN_equiv_class2]) 1);
+qed "hypreal_add";
+
+Goal "Abs_hypreal(hyprel^^{%n. X n}) - Abs_hypreal(hyprel^^{%n. Y n}) = \
+\     Abs_hypreal(hyprel^^{%n. X n - Y n})";
+by (simp_tac (simpset() addsimps 
+         [hypreal_diff_def, hypreal_minus,hypreal_add]) 1);
+qed "hypreal_diff";
+
+Goal "(z::hypreal) + w = w + z";
+by (res_inst_tac [("z","z")] eq_Abs_hypreal 1);
+by (res_inst_tac [("z","w")] eq_Abs_hypreal 1);
+by (asm_simp_tac (simpset() addsimps (real_add_ac @ [hypreal_add])) 1);
+qed "hypreal_add_commute";
+
+Goal "((z1::hypreal) + z2) + z3 = z1 + (z2 + z3)";
+by (res_inst_tac [("z","z1")] eq_Abs_hypreal 1);
+by (res_inst_tac [("z","z2")] eq_Abs_hypreal 1);
+by (res_inst_tac [("z","z3")] eq_Abs_hypreal 1);
+by (asm_simp_tac (simpset() addsimps [hypreal_add, real_add_assoc]) 1);
+qed "hypreal_add_assoc";
+
+(*For AC rewriting*)
+Goal "(x::hypreal)+(y+z)=y+(x+z)";
+by (rtac (hypreal_add_commute RS trans) 1);
+by (rtac (hypreal_add_assoc RS trans) 1);
+by (rtac (hypreal_add_commute RS arg_cong) 1);
+qed "hypreal_add_left_commute";
+
+(* hypreal addition is an AC operator *)
+bind_thms ("hypreal_add_ac", [hypreal_add_assoc,hypreal_add_commute,
+                      hypreal_add_left_commute]);
+
+Goalw [hypreal_zero_def] "(0::hypreal) + z = z";
+by (res_inst_tac [("z","z")] eq_Abs_hypreal 1);
+by (asm_full_simp_tac (simpset() addsimps 
+    [hypreal_add]) 1);
+qed "hypreal_add_zero_left";
+
+Goal "z + (0::hypreal) = z";
+by (simp_tac (simpset() addsimps 
+    [hypreal_add_zero_left,hypreal_add_commute]) 1);
+qed "hypreal_add_zero_right";
+
+Goalw [hypreal_zero_def] "z + -z = (0::hypreal)";
+by (res_inst_tac [("z","z")] eq_Abs_hypreal 1);
+by (asm_full_simp_tac (simpset() addsimps [hypreal_minus, hypreal_add]) 1);
+qed "hypreal_add_minus";
+
+Goal "-z + z = (0::hypreal)";
+by (simp_tac (simpset() addsimps [hypreal_add_commute, hypreal_add_minus]) 1);
+qed "hypreal_add_minus_left";
+
+Addsimps [hypreal_add_minus,hypreal_add_minus_left,
+          hypreal_add_zero_left,hypreal_add_zero_right];
+
+Goal "EX y. (x::hypreal) + y = 0";
+by (fast_tac (claset() addIs [hypreal_add_minus]) 1);
+qed "hypreal_minus_ex";
+
+Goal "EX! y. (x::hypreal) + y = 0";
+by (auto_tac (claset() addIs [hypreal_add_minus], simpset()));
+by (dres_inst_tac [("f","%x. ya+x")] arg_cong 1);
+by (asm_full_simp_tac (simpset() addsimps [hypreal_add_assoc RS sym]) 1);
+by (asm_full_simp_tac (simpset() addsimps [hypreal_add_commute]) 1);
+qed "hypreal_minus_ex1";
+
+Goal "EX! y. y + (x::hypreal) = 0";
+by (auto_tac (claset() addIs [hypreal_add_minus_left], simpset()));
+by (dres_inst_tac [("f","%x. x+ya")] arg_cong 1);
+by (asm_full_simp_tac (simpset() addsimps [hypreal_add_assoc]) 1);
+by (asm_full_simp_tac (simpset() addsimps [hypreal_add_commute]) 1);
+qed "hypreal_minus_left_ex1";
+
+Goal "x + y = (0::hypreal) ==> x = -y";
+by (cut_inst_tac [("z","y")] hypreal_add_minus_left 1);
+by (res_inst_tac [("x1","y")] (hypreal_minus_left_ex1 RS ex1E) 1);
+by (Blast_tac 1);
+qed "hypreal_add_minus_eq_minus";
+
+Goal "EX y::hypreal. x = -y";
+by (cut_inst_tac [("x","x")] hypreal_minus_ex 1);
+by (etac exE 1 THEN dtac hypreal_add_minus_eq_minus 1);
+by (Fast_tac 1);
+qed "hypreal_as_add_inverse_ex";
+
+Goal "-(x + (y::hypreal)) = -x + -y";
+by (res_inst_tac [("z","x")] eq_Abs_hypreal 1);
+by (res_inst_tac [("z","y")] eq_Abs_hypreal 1);
+by (auto_tac (claset(),
+              simpset() addsimps [hypreal_minus, hypreal_add,
+                                  real_minus_add_distrib]));
+qed "hypreal_minus_add_distrib";
+Addsimps [hypreal_minus_add_distrib];
+
+Goal "-(y + -(x::hypreal)) = x + -y";
+by (simp_tac (simpset() addsimps [hypreal_add_commute]) 1);
+qed "hypreal_minus_distrib1";
+
+Goal "(x + - (y::hypreal)) + (y + - z) = x + -z";
+by (res_inst_tac [("w1","y")] (hypreal_add_commute RS subst) 1);
+by (simp_tac (simpset() addsimps [hypreal_add_left_commute,
+                                  hypreal_add_assoc]) 1);
+by (simp_tac (simpset() addsimps [hypreal_add_commute]) 1);
+qed "hypreal_add_minus_cancel1";
+
+Goal "((x::hypreal) + y = x + z) = (y = z)";
+by (Step_tac 1);
+by (dres_inst_tac [("f","%t.-x + t")] arg_cong 1);
+by (asm_full_simp_tac (simpset() addsimps [hypreal_add_assoc RS sym]) 1);
+qed "hypreal_add_left_cancel";
+
+Goal "z + (x + (y + -z)) = x + (y::hypreal)";
+by (simp_tac (simpset() addsimps hypreal_add_ac) 1);
+qed "hypreal_add_minus_cancel2";
+Addsimps [hypreal_add_minus_cancel2];
+
+Goal "y + -(x + y) = -(x::hypreal)";
+by (Full_simp_tac 1);
+by (rtac (hypreal_add_left_commute RS subst) 1);
+by (Full_simp_tac 1);
+qed "hypreal_add_minus_cancel";
+Addsimps [hypreal_add_minus_cancel];
+
+Goal "y + -(y + x) = -(x::hypreal)";
+by (simp_tac (simpset() addsimps [hypreal_add_assoc RS sym]) 1);
+qed "hypreal_add_minus_cancelc";
+Addsimps [hypreal_add_minus_cancelc];
+
+Goal "(z + -x) + (y + -z) = (y + -(x::hypreal))";
+by (full_simp_tac
+    (simpset() addsimps [hypreal_minus_add_distrib RS sym, 
+                         hypreal_add_left_cancel] @ hypreal_add_ac 
+               delsimps [hypreal_minus_add_distrib]) 1); 
+qed "hypreal_add_minus_cancel3";
+Addsimps [hypreal_add_minus_cancel3];
+
+Goal "(y + (x::hypreal)= z + x) = (y = z)";
+by (simp_tac (simpset() addsimps [hypreal_add_commute,
+                                  hypreal_add_left_cancel]) 1);
+qed "hypreal_add_right_cancel";
+
+Goal "z + (y + -z) = (y::hypreal)";
+by (simp_tac (simpset() addsimps hypreal_add_ac) 1);
+qed "hypreal_add_minus_cancel4";
+Addsimps [hypreal_add_minus_cancel4];
+
+Goal "z + (w + (x + (-z + y))) = w + x + (y::hypreal)";
+by (simp_tac (simpset() addsimps hypreal_add_ac) 1);
+qed "hypreal_add_minus_cancel5";
+Addsimps [hypreal_add_minus_cancel5];
+
+Goal "z + ((- z) + w) = (w::hypreal)";
+by (simp_tac (simpset() addsimps [hypreal_add_assoc RS sym]) 1);
+qed "hypreal_add_minus_cancelA";
+
+Goal "(-z) + (z + w) = (w::hypreal)";
+by (simp_tac (simpset() addsimps [hypreal_add_assoc RS sym]) 1);
+qed "hypreal_minus_add_cancelA";
+
+Addsimps [hypreal_add_minus_cancelA, hypreal_minus_add_cancelA];
+
+(**** hyperreal multiplication: hypreal_mult  ****)
+
+Goalw [congruent2_def]
+    "congruent2 hyprel (%X Y. hyprel^^{%n. X n * Y n})";
+by Safe_tac;
+by (ALLGOALS(Ultra_tac));
+qed "hypreal_mult_congruent2";
+
+Goalw [hypreal_mult_def]
+  "Abs_hypreal(hyprel^^{%n. X n}) * Abs_hypreal(hyprel^^{%n. Y n}) = \
+\  Abs_hypreal(hyprel^^{%n. X n * Y n})";
+by (simp_tac (simpset() addsimps 
+      [[equiv_hyprel, hypreal_mult_congruent2] MRS UN_equiv_class2]) 1);
+qed "hypreal_mult";
+
+Goal "(z::hypreal) * w = w * z";
+by (res_inst_tac [("z","z")] eq_Abs_hypreal 1);
+by (res_inst_tac [("z","w")] eq_Abs_hypreal 1);
+by (asm_simp_tac (simpset() addsimps ([hypreal_mult] @ real_mult_ac)) 1);
+qed "hypreal_mult_commute";
+
+Goal "((z1::hypreal) * z2) * z3 = z1 * (z2 * z3)";
+by (res_inst_tac [("z","z1")] eq_Abs_hypreal 1);
+by (res_inst_tac [("z","z2")] eq_Abs_hypreal 1);
+by (res_inst_tac [("z","z3")] eq_Abs_hypreal 1);
+by (asm_simp_tac (simpset() addsimps [hypreal_mult,real_mult_assoc]) 1);
+qed "hypreal_mult_assoc";
+
+qed_goal "hypreal_mult_left_commute" (the_context ())
+    "(z1::hypreal) * (z2 * z3) = z2 * (z1 * z3)"
+ (fn _ => [rtac (hypreal_mult_commute RS trans) 1, 
+           rtac (hypreal_mult_assoc RS trans) 1,
+           rtac (hypreal_mult_commute RS arg_cong) 1]);
+
+(* hypreal multiplication is an AC operator *)
+bind_thms ("hypreal_mult_ac", [hypreal_mult_assoc, hypreal_mult_commute, 
+                       hypreal_mult_left_commute]);
+
+Goalw [hypreal_one_def] "1hr * z = z";
+by (res_inst_tac [("z","z")] eq_Abs_hypreal 1);
+by (asm_full_simp_tac (simpset() addsimps [hypreal_mult]) 1);
+qed "hypreal_mult_1";
+
+Goal "z * 1hr = z";
+by (simp_tac (simpset() addsimps [hypreal_mult_commute,
+    hypreal_mult_1]) 1);
+qed "hypreal_mult_1_right";
+
+Goalw [hypreal_zero_def] "0 * z = (0::hypreal)";
+by (res_inst_tac [("z","z")] eq_Abs_hypreal 1);
+by (asm_full_simp_tac (simpset() addsimps [hypreal_mult,real_mult_0]) 1);
+qed "hypreal_mult_0";
+
+Goal "z * 0 = (0::hypreal)";
+by (simp_tac (simpset() addsimps [hypreal_mult_commute, hypreal_mult_0]) 1);
+qed "hypreal_mult_0_right";
+
+Addsimps [hypreal_mult_0,hypreal_mult_0_right];
+
+Goal "-(x * y) = -x * (y::hypreal)";
+by (res_inst_tac [("z","x")] eq_Abs_hypreal 1);
+by (res_inst_tac [("z","y")] eq_Abs_hypreal 1);
+by (auto_tac (claset(),
+	      simpset() addsimps [hypreal_minus, hypreal_mult] 
+                                 @ real_mult_ac @ real_add_ac));
+qed "hypreal_minus_mult_eq1";
+
+Goal "-(x * y) = (x::hypreal) * -y";
+by (res_inst_tac [("z","x")] eq_Abs_hypreal 1);
+by (res_inst_tac [("z","y")] eq_Abs_hypreal 1);
+by (auto_tac (claset(), simpset() addsimps [hypreal_minus, hypreal_mult] 
+                                           @ real_mult_ac @ real_add_ac));
+qed "hypreal_minus_mult_eq2";
+
+(*Pull negations out*)
+Addsimps [hypreal_minus_mult_eq2 RS sym, hypreal_minus_mult_eq1 RS sym];
+
+Goal "-x*y = (x::hypreal)*-y";
+by Auto_tac;
+qed "hypreal_minus_mult_commute";
+
+(*-----------------------------------------------------------------------------
+    A few more theorems
+ ----------------------------------------------------------------------------*)
+Goal "(z::hypreal) + v = z' + v' ==> z + (v + w) = z' + (v' + w)";
+by (asm_simp_tac (simpset() addsimps [hypreal_add_assoc RS sym]) 1);
+qed "hypreal_add_assoc_cong";
+
+Goal "(z::hypreal) + (v + w) = v + (z + w)";
+by (REPEAT (ares_tac [hypreal_add_commute RS hypreal_add_assoc_cong] 1));
+qed "hypreal_add_assoc_swap";
+
+Goal "((z1::hypreal) + z2) * w = (z1 * w) + (z2 * w)";
+by (res_inst_tac [("z","z1")] eq_Abs_hypreal 1);
+by (res_inst_tac [("z","z2")] eq_Abs_hypreal 1);
+by (res_inst_tac [("z","w")] eq_Abs_hypreal 1);
+by (asm_simp_tac (simpset() addsimps [hypreal_mult,hypreal_add,
+     real_add_mult_distrib]) 1);
+qed "hypreal_add_mult_distrib";
+
+val hypreal_mult_commute'= read_instantiate [("z","w")] hypreal_mult_commute;
+
+Goal "(w::hypreal) * (z1 + z2) = (w * z1) + (w * z2)";
+by (simp_tac (simpset() addsimps [hypreal_mult_commute',hypreal_add_mult_distrib]) 1);
+qed "hypreal_add_mult_distrib2";
+
+bind_thms ("hypreal_mult_simps", [hypreal_mult_1, hypreal_mult_1_right]);
+Addsimps hypreal_mult_simps;
+
+(* 07/00 *)
+
+Goalw [hypreal_diff_def] "((z1::hypreal) - z2) * w = (z1 * w) - (z2 * w)";
+by (simp_tac (simpset() addsimps [hypreal_add_mult_distrib]) 1);
+qed "hypreal_diff_mult_distrib";
+
+Goal "(w::hypreal) * (z1 - z2) = (w * z1) - (w * z2)";
+by (simp_tac (simpset() addsimps [hypreal_mult_commute', 
+				  hypreal_diff_mult_distrib]) 1);
+qed "hypreal_diff_mult_distrib2";
+
+(*** one and zero are distinct ***)
+Goalw [hypreal_zero_def,hypreal_one_def] "0 ~= 1hr";
+by (auto_tac (claset(), simpset() addsimps [real_zero_not_eq_one]));
+qed "hypreal_zero_not_eq_one";
+
+
+(**** multiplicative inverse on hypreal ****)
+
+Goalw [congruent_def]
+  "congruent hyprel (%X. hyprel^^{%n. if X n = #0 then #0 else inverse(X n)})";
+by (Auto_tac THEN Ultra_tac 1);
+qed "hypreal_inverse_congruent";
+
+Goalw [hypreal_inverse_def]
+      "inverse (Abs_hypreal(hyprel^^{%n. X n})) = \
+\      Abs_hypreal(hyprel ^^ {%n. if X n = #0 then #0 else inverse(X n)})";
+by (res_inst_tac [("f","Abs_hypreal")] arg_cong 1);
+by (simp_tac (simpset() addsimps 
+   [hyprel_in_hypreal RS Abs_hypreal_inverse,
+    [equiv_hyprel, hypreal_inverse_congruent] MRS UN_equiv_class]) 1);
+qed "hypreal_inverse";
+
+Goal "inverse 0 = (0::hypreal)";
+by (simp_tac (simpset() addsimps [hypreal_inverse, hypreal_zero_def]) 1);
+qed "HYPREAL_INVERSE_ZERO";
+
+Goal "a / (0::hypreal) = 0";
+by (simp_tac
+    (simpset() addsimps [hypreal_divide_def, HYPREAL_INVERSE_ZERO]) 1);
+qed "HYPREAL_DIVISION_BY_ZERO";  (*NOT for adding to default simpset*)
+
+fun hypreal_div_undefined_case_tac s i =
+  case_tac s i THEN 
+  asm_simp_tac 
+       (simpset() addsimps [HYPREAL_DIVISION_BY_ZERO, HYPREAL_INVERSE_ZERO]) i;
+
+Goal "inverse (inverse (z::hypreal)) = z";
+by (hypreal_div_undefined_case_tac "z=0" 1);
+by (res_inst_tac [("z","z")] eq_Abs_hypreal 1);
+by (asm_full_simp_tac (simpset() addsimps 
+                       [hypreal_inverse, hypreal_zero_def]) 1);
+qed "hypreal_inverse_inverse";
+Addsimps [hypreal_inverse_inverse];
+
+Goalw [hypreal_one_def] "inverse(1hr) = 1hr";
+by (full_simp_tac (simpset() addsimps [hypreal_inverse,
+                                       real_zero_not_eq_one RS not_sym]) 1);
+qed "hypreal_inverse_1";
+Addsimps [hypreal_inverse_1];
+
+
+(*** existence of inverse ***)
+
+Goalw [hypreal_one_def,hypreal_zero_def] 
+     "x ~= 0 ==> x*inverse(x) = 1hr";
+by (res_inst_tac [("z","x")] eq_Abs_hypreal 1);
+by (rotate_tac 1 1);
+by (asm_full_simp_tac (simpset() addsimps [hypreal_inverse, hypreal_mult]) 1);
+by (dtac FreeUltrafilterNat_Compl_mem 1);
+by (blast_tac (claset() addSIs [real_mult_inv_right,
+    FreeUltrafilterNat_subset]) 1);
+qed "hypreal_mult_inverse";
+
+Goal "x ~= 0 ==> inverse(x)*x = 1hr";
+by (asm_simp_tac (simpset() addsimps [hypreal_mult_inverse,
+				      hypreal_mult_commute]) 1);
+qed "hypreal_mult_inverse_left";
+
+Goal "(c::hypreal) ~= 0 ==> (c*a=c*b) = (a=b)";
+by Auto_tac;
+by (dres_inst_tac [("f","%x. x*inverse c")] arg_cong 1);
+by (asm_full_simp_tac (simpset() addsimps [hypreal_mult_inverse] @ hypreal_mult_ac)  1);
+qed "hypreal_mult_left_cancel";
+    
+Goal "(c::hypreal) ~= 0 ==> (a*c=b*c) = (a=b)";
+by (Step_tac 1);
+by (dres_inst_tac [("f","%x. x*inverse c")] arg_cong 1);
+by (asm_full_simp_tac (simpset() addsimps [hypreal_mult_inverse] @ hypreal_mult_ac)  1);
+qed "hypreal_mult_right_cancel";
+
+Goalw [hypreal_zero_def] "x ~= 0 ==> inverse (x::hypreal) ~= 0";
+by (res_inst_tac [("z","x")] eq_Abs_hypreal 1);
+by (asm_full_simp_tac (simpset() addsimps [hypreal_inverse, hypreal_mult]) 1);
+qed "hypreal_inverse_not_zero";
+
+Addsimps [hypreal_mult_inverse,hypreal_mult_inverse_left];
+
+Goal "[| x ~= 0; y ~= 0 |] ==> x * y ~= (0::hypreal)";
+by (Step_tac 1);
+by (dres_inst_tac [("f","%z. inverse x*z")] arg_cong 1);
+by (asm_full_simp_tac (simpset() addsimps [hypreal_mult_assoc RS sym]) 1);
+qed "hypreal_mult_not_0";
+
+Goal "x*y = (0::hypreal) ==> x = 0 | y = 0";
+by (auto_tac (claset() addIs [ccontr] addDs [hypreal_mult_not_0],
+              simpset()));
+qed "hypreal_mult_zero_disj";
+
+Goal "inverse(-x) = -inverse(x::hypreal)";
+by (hypreal_div_undefined_case_tac "x=0" 1);
+by (rtac (hypreal_mult_right_cancel RS iffD1) 1);
+by (stac hypreal_mult_inverse_left 2);
+by Auto_tac;
+qed "hypreal_minus_inverse";
+
+Goal "inverse(x*y) = inverse(x)*inverse(y::hypreal)";
+by (hypreal_div_undefined_case_tac "x=0" 1);
+by (hypreal_div_undefined_case_tac "y=0" 1);
+by (forw_inst_tac [("y","y")] hypreal_mult_not_0 1 THEN assume_tac 1);
+by (res_inst_tac [("c1","x")] (hypreal_mult_left_cancel RS iffD1) 1);
+by (auto_tac (claset(), simpset() addsimps [hypreal_mult_assoc RS sym]));
+by (res_inst_tac [("c1","y")] (hypreal_mult_left_cancel RS iffD1) 1);
+by (auto_tac (claset(), simpset() addsimps [hypreal_mult_left_commute]));
+by (asm_simp_tac (simpset() addsimps [hypreal_mult_assoc RS sym]) 1);
+qed "hypreal_inverse_distrib";
+
+(*------------------------------------------------------------------
+                   Theorems for ordering 
+ ------------------------------------------------------------------*)
+
+(* prove introduction and elimination rules for hypreal_less *)
+
+Goalw [hypreal_less_def]
+ "P < (Q::hypreal) = (EX X Y. X : Rep_hypreal(P) & \
+\                             Y : Rep_hypreal(Q) & \
+\                             {n. X n < Y n} : FreeUltrafilterNat)";
+by (Fast_tac 1);
+qed "hypreal_less_iff";
+
+Goalw [hypreal_less_def]
+ "[| {n. X n < Y n} : FreeUltrafilterNat; \
+\         X : Rep_hypreal(P); \
+\         Y : Rep_hypreal(Q) |] ==> P < (Q::hypreal)";
+by (Fast_tac 1);
+qed "hypreal_lessI";
+
+
+Goalw [hypreal_less_def]
+     "!! R1. [| R1 < (R2::hypreal); \
+\         !!X Y. {n. X n < Y n} : FreeUltrafilterNat ==> P; \
+\         !!X. X : Rep_hypreal(R1) ==> P; \ 
+\         !!Y. Y : Rep_hypreal(R2) ==> P |] \
+\     ==> P";
+by Auto_tac;
+qed "hypreal_lessE";
+
+Goalw [hypreal_less_def]
+ "R1 < (R2::hypreal) ==> (EX X Y. {n. X n < Y n} : FreeUltrafilterNat & \
+\                                  X : Rep_hypreal(R1) & \
+\                                  Y : Rep_hypreal(R2))";
+by (Fast_tac 1);
+qed "hypreal_lessD";
+
+Goal "~ (R::hypreal) < R";
+by (res_inst_tac [("z","R")] eq_Abs_hypreal 1);
+by (auto_tac (claset(), simpset() addsimps [hypreal_less_def]));
+by (Ultra_tac 1);
+qed "hypreal_less_not_refl";
+
+(*** y < y ==> P ***)
+bind_thm("hypreal_less_irrefl",hypreal_less_not_refl RS notE);
+AddSEs [hypreal_less_irrefl];
+
+Goal "!!(x::hypreal). x < y ==> x ~= y";
+by (auto_tac (claset(), simpset() addsimps [hypreal_less_not_refl]));
+qed "hypreal_not_refl2";
+
+Goal "!!(R1::hypreal). [| R1 < R2; R2 < R3 |] ==> R1 < R3";
+by (res_inst_tac [("z","R1")] eq_Abs_hypreal 1);
+by (res_inst_tac [("z","R2")] eq_Abs_hypreal 1);
+by (res_inst_tac [("z","R3")] eq_Abs_hypreal 1);
+by (auto_tac (claset() addSIs [exI], simpset() addsimps [hypreal_less_def]));
+by (ultra_tac (claset() addIs [order_less_trans], simpset()) 1);
+qed "hypreal_less_trans";
+
+Goal "!! (R1::hypreal). [| R1 < R2; R2 < R1 |] ==> P";
+by (dtac hypreal_less_trans 1 THEN assume_tac 1);
+by (asm_full_simp_tac (simpset() addsimps 
+    [hypreal_less_not_refl]) 1);
+qed "hypreal_less_asym";
+
+(*-------------------------------------------------------
+  TODO: The following theorem should have been proved 
+  first and then used througout the proofs as it probably 
+  makes many of them more straightforward. 
+ -------------------------------------------------------*)
+Goalw [hypreal_less_def]
+      "(Abs_hypreal(hyprel^^{%n. X n}) < \
+\           Abs_hypreal(hyprel^^{%n. Y n})) = \
+\      ({n. X n < Y n} : FreeUltrafilterNat)";
+by (auto_tac (claset() addSIs [lemma_hyprel_refl], simpset()));
+by (Ultra_tac 1);
+qed "hypreal_less";
+
+(*---------------------------------------------------------------------------------
+             Hyperreals as a linearly ordered field
+ ---------------------------------------------------------------------------------*)
+(*** sum order 
+Goalw [hypreal_zero_def] 
+      "[| 0 < x; 0 < y |] ==> (0::hypreal) < x + y";
+by (res_inst_tac [("z","x")] eq_Abs_hypreal 1);
+by (res_inst_tac [("z","y")] eq_Abs_hypreal 1);
+by (auto_tac (claset(),simpset() addsimps
+    [hypreal_less_def,hypreal_add]));
+by (auto_tac (claset() addSIs [exI],simpset() addsimps
+    [hypreal_less_def,hypreal_add]));
+by (ultra_tac (claset() addIs [real_add_order],simpset()) 1);
+qed "hypreal_add_order";
+***)
+
+(*** mult order 
+Goalw [hypreal_zero_def] 
+          "[| 0 < x; 0 < y |] ==> (0::hypreal) < x * y";
+by (res_inst_tac [("z","x")] eq_Abs_hypreal 1);
+by (res_inst_tac [("z","y")] eq_Abs_hypreal 1);
+by (auto_tac (claset() addSIs [exI],simpset() addsimps
+    [hypreal_less_def,hypreal_mult]));
+by (ultra_tac (claset() addIs [rename_numerals real_mult_order],
+	       simpset()) 1);
+qed "hypreal_mult_order";
+****)
+
+
+(*---------------------------------------------------------------------------------
+                         Trichotomy of the hyperreals
+  --------------------------------------------------------------------------------*)
+
+Goalw [hyprel_def] "EX x. x: hyprel ^^ {%n. #0}";
+by (res_inst_tac [("x","%n. #0")] exI 1);
+by (Step_tac 1);
+by (auto_tac (claset() addSIs [FreeUltrafilterNat_Nat_set], simpset()));
+qed "lemma_hyprel_0r_mem";
+
+Goalw [hypreal_zero_def]"0 <  x | x = 0 | x < (0::hypreal)";
+by (res_inst_tac [("z","x")] eq_Abs_hypreal 1);
+by (auto_tac (claset(),simpset() addsimps [hypreal_less_def]));
+by (cut_facts_tac [lemma_hyprel_0r_mem] 1 THEN etac exE 1);
+by (dres_inst_tac [("x","xa")] spec 1);
+by (dres_inst_tac [("x","x")] spec 1);
+by (cut_inst_tac [("x","x")] lemma_hyprel_refl 1);
+by Auto_tac;
+by (dres_inst_tac [("x","x")] spec 1);
+by (dres_inst_tac [("x","xa")] spec 1);
+by Auto_tac;
+by (Ultra_tac 1);
+by (auto_tac (claset() addIs [real_linear_less2],simpset()));
+qed "hypreal_trichotomy";
+
+val prems = Goal "[| (0::hypreal) < x ==> P; \
+\                 x = 0 ==> P; \
+\                 x < 0 ==> P |] ==> P";
+by (cut_inst_tac [("x","x")] hypreal_trichotomy 1);
+by (REPEAT (eresolve_tac (disjE::prems) 1));
+qed "hypreal_trichotomyE";
+
+(*----------------------------------------------------------------------------
+            More properties of <
+ ----------------------------------------------------------------------------*)
+
+Goal "((x::hypreal) < y) = (0 < y + -x)";
+by (res_inst_tac [("z","x")] eq_Abs_hypreal 1);
+by (res_inst_tac [("z","y")] eq_Abs_hypreal 1);
+by (auto_tac (claset(),simpset() addsimps [hypreal_add,
+    hypreal_zero_def,hypreal_minus,hypreal_less]));
+by (ALLGOALS(Ultra_tac));
+qed "hypreal_less_minus_iff"; 
+
+Goal "((x::hypreal) < y) = (x + -y < 0)";
+by (res_inst_tac [("z","x")] eq_Abs_hypreal 1);
+by (res_inst_tac [("z","y")] eq_Abs_hypreal 1);
+by (auto_tac (claset(),simpset() addsimps [hypreal_add,
+    hypreal_zero_def,hypreal_minus,hypreal_less]));
+by (ALLGOALS(Ultra_tac));
+qed "hypreal_less_minus_iff2";
+
+Goal "((x::hypreal) = y) = (x + - y = 0)";
+by Auto_tac;
+by (res_inst_tac [("x1","-y")] (hypreal_add_right_cancel RS iffD1) 1);
+by Auto_tac;
+qed "hypreal_eq_minus_iff"; 
+
+Goal "((x::hypreal) = y) = (0 = y + - x)";
+by Auto_tac;
+by (res_inst_tac [("x1","-x")] (hypreal_add_right_cancel RS iffD1) 1);
+by Auto_tac;
+qed "hypreal_eq_minus_iff2"; 
+
+(* 07/00 *)
+Goal "(0::hypreal) - x = -x";
+by (simp_tac (simpset() addsimps [hypreal_diff_def]) 1);
+qed "hypreal_diff_zero";
+
+Goal "x - (0::hypreal) = x";
+by (simp_tac (simpset() addsimps [hypreal_diff_def]) 1);
+qed "hypreal_diff_zero_right";
+
+Goal "x - x = (0::hypreal)";
+by (simp_tac (simpset() addsimps [hypreal_diff_def]) 1);
+qed "hypreal_diff_self";
+
+Addsimps [hypreal_diff_zero, hypreal_diff_zero_right, hypreal_diff_self];
+
+Goal "(x = y + z) = (x + -z = (y::hypreal))";
+by (auto_tac (claset(),simpset() addsimps [hypreal_add_assoc]));
+qed "hypreal_eq_minus_iff3";
+
+Goal "(x ~= a) = (x + -a ~= (0::hypreal))";
+by (auto_tac (claset() addDs [hypreal_eq_minus_iff RS iffD2],
+              simpset())); 
+qed "hypreal_not_eq_minus_iff";
+
+Goal "(x+y = (0::hypreal)) = (x = -y)";
+by (stac hypreal_eq_minus_iff 1);
+by Auto_tac;
+qed "hypreal_add_eq_0_iff";
+AddIffs [hypreal_add_eq_0_iff];
+
+
+(*** linearity ***)
+
+Goal "(x::hypreal) < y | x = y | y < x";
+by (stac hypreal_eq_minus_iff2 1);
+by (res_inst_tac [("x1","x")] (hypreal_less_minus_iff RS ssubst) 1);
+by (res_inst_tac [("x1","y")] (hypreal_less_minus_iff2 RS ssubst) 1);
+by (rtac hypreal_trichotomyE 1);
+by Auto_tac;
+qed "hypreal_linear";
+
+Goal "((w::hypreal) ~= z) = (w<z | z<w)";
+by (cut_facts_tac [hypreal_linear] 1);
+by (Blast_tac 1);
+qed "hypreal_neq_iff";
+
+Goal "!!(x::hypreal). [| x < y ==> P;  x = y ==> P; \
+\          y < x ==> P |] ==> P";
+by (cut_inst_tac [("x","x"),("y","y")] hypreal_linear 1);
+by Auto_tac;
+qed "hypreal_linear_less2";
+
+(*------------------------------------------------------------------------------
+                            Properties of <=
+ ------------------------------------------------------------------------------*)
+(*------ hypreal le iff reals le a.e ------*)
+
+Goalw [hypreal_le_def,real_le_def]
+      "(Abs_hypreal(hyprel^^{%n. X n}) <= \
+\           Abs_hypreal(hyprel^^{%n. Y n})) = \
+\      ({n. X n <= Y n} : FreeUltrafilterNat)";
+by (auto_tac (claset(),simpset() addsimps [hypreal_less]));
+by (ALLGOALS(Ultra_tac));
+qed "hypreal_le";
+
+(*---------------------------------------------------------*)
+(*---------------------------------------------------------*)
+Goalw [hypreal_le_def] 
+     "~(w < z) ==> z <= (w::hypreal)";
+by (assume_tac 1);
+qed "hypreal_leI";
+
+Goalw [hypreal_le_def] 
+      "z<=w ==> ~(w<(z::hypreal))";
+by (assume_tac 1);
+qed "hypreal_leD";
+
+bind_thm ("hypreal_leE", make_elim hypreal_leD);
+
+Goal "(~(w < z)) = (z <= (w::hypreal))";
+by (fast_tac (claset() addSIs [hypreal_leI,hypreal_leD]) 1);
+qed "hypreal_less_le_iff";
+
+Goalw [hypreal_le_def] "~ z <= w ==> w<(z::hypreal)";
+by (Fast_tac 1);
+qed "not_hypreal_leE";
+
+Goalw [hypreal_le_def] "!!(x::hypreal). x <= y ==> x < y | x = y";
+by (cut_facts_tac [hypreal_linear] 1);
+by (fast_tac (claset() addEs [hypreal_less_irrefl,hypreal_less_asym]) 1);
+qed "hypreal_le_imp_less_or_eq";
+
+Goalw [hypreal_le_def] "z<w | z=w ==> z <=(w::hypreal)";
+by (cut_facts_tac [hypreal_linear] 1);
+by (fast_tac (claset() addEs [hypreal_less_irrefl,hypreal_less_asym]) 1);
+qed "hypreal_less_or_eq_imp_le";
+
+Goal "(x <= (y::hypreal)) = (x < y | x=y)";
+by (REPEAT(ares_tac [iffI, hypreal_less_or_eq_imp_le, hypreal_le_imp_less_or_eq] 1));
+qed "hypreal_le_eq_less_or_eq";
+val hypreal_le_less = hypreal_le_eq_less_or_eq;
+
+Goal "w <= (w::hypreal)";
+by (simp_tac (simpset() addsimps [hypreal_le_eq_less_or_eq]) 1);
+qed "hypreal_le_refl";
+
+(* Axiom 'linorder_linear' of class 'linorder': *)
+Goal "(z::hypreal) <= w | w <= z";
+by (simp_tac (simpset() addsimps [hypreal_le_less]) 1);
+by (cut_facts_tac [hypreal_linear] 1);
+by (Blast_tac 1);
+qed "hypreal_le_linear";
+
+Goal "[| i <= j; j <= k |] ==> i <= (k::hypreal)";
+by (EVERY1 [dtac hypreal_le_imp_less_or_eq, dtac hypreal_le_imp_less_or_eq,
+            rtac hypreal_less_or_eq_imp_le, 
+            fast_tac (claset() addIs [hypreal_less_trans])]);
+qed "hypreal_le_trans";
+
+Goal "[| z <= w; w <= z |] ==> z = (w::hypreal)";
+by (EVERY1 [dtac hypreal_le_imp_less_or_eq, dtac hypreal_le_imp_less_or_eq,
+            fast_tac (claset() addEs [hypreal_less_irrefl,hypreal_less_asym])]);
+qed "hypreal_le_anti_sym";
+
+Goal "[| ~ y < x; y ~= x |] ==> x < (y::hypreal)";
+by (rtac not_hypreal_leE 1);
+by (fast_tac (claset() addDs [hypreal_le_imp_less_or_eq]) 1);
+qed "not_less_not_eq_hypreal_less";
+
+(* Axiom 'order_less_le' of class 'order': *)
+Goal "(w::hypreal) < z = (w <= z & w ~= z)";
+by (simp_tac (simpset() addsimps [hypreal_le_def, hypreal_neq_iff]) 1);
+by (blast_tac (claset() addIs [hypreal_less_asym]) 1);
+qed "hypreal_less_le";
+
+Goal "(0 < -R) = (R < (0::hypreal))";
+by (res_inst_tac [("z","R")] eq_Abs_hypreal 1);
+by (auto_tac (claset(),
+       simpset() addsimps [hypreal_zero_def, hypreal_less,hypreal_minus]));
+qed "hypreal_minus_zero_less_iff";
+Addsimps [hypreal_minus_zero_less_iff];
+
+Goal "(-R < 0) = ((0::hypreal) < R)";
+by (res_inst_tac [("z","R")] eq_Abs_hypreal 1);
+by (auto_tac (claset(),
+         simpset() addsimps [hypreal_zero_def, hypreal_less,hypreal_minus]));
+by (ALLGOALS(Ultra_tac));
+qed "hypreal_minus_zero_less_iff2";
+
+Goalw [hypreal_le_def] "((0::hypreal) <= -r) = (r <= (0::hypreal))";
+by (simp_tac (simpset() addsimps [hypreal_minus_zero_less_iff2]) 1);
+qed "hypreal_minus_zero_le_iff";
+Addsimps [hypreal_minus_zero_le_iff];
+
+(*----------------------------------------------------------
+  hypreal_of_real preserves field and order properties
+ -----------------------------------------------------------*)
+Goalw [hypreal_of_real_def] 
+     "hypreal_of_real (z1 + z2) = hypreal_of_real z1 + hypreal_of_real z2";
+by (simp_tac (simpset() addsimps [hypreal_add, hypreal_add_mult_distrib]) 1);
+qed "hypreal_of_real_add";
+Addsimps [hypreal_of_real_add];
+
+Goalw [hypreal_of_real_def] 
+     "hypreal_of_real (z1 * z2) = hypreal_of_real z1 * hypreal_of_real z2";
+by (simp_tac (simpset() addsimps [hypreal_mult, hypreal_add_mult_distrib2]) 1);
+qed "hypreal_of_real_mult";
+Addsimps [hypreal_of_real_mult];
+
+Goalw [hypreal_less_def,hypreal_of_real_def] 
+     "(hypreal_of_real z1 <  hypreal_of_real z2) = (z1 < z2)";
+by Auto_tac;
+by (res_inst_tac [("x","%n. z1")] exI 2);
+by (Step_tac 1); 
+by (res_inst_tac [("x","%n. z2")] exI 3);
+by Auto_tac;
+by (rtac FreeUltrafilterNat_P 1);
+by (Ultra_tac 1);
+qed "hypreal_of_real_less_iff";
+Addsimps [hypreal_of_real_less_iff];
+
+Goalw [hypreal_le_def,real_le_def] 
+     "(hypreal_of_real z1 <= hypreal_of_real z2) = (z1 <= z2)";
+by Auto_tac;
+qed "hypreal_of_real_le_iff";
+Addsimps [hypreal_of_real_le_iff];
+
+Goal "(hypreal_of_real z1 = hypreal_of_real z2) = (z1 = z2)";
+by (force_tac (claset() addIs [order_antisym, hypreal_of_real_le_iff RS iffD1],
+               simpset()) 1); 
+qed "hypreal_of_real_eq_iff";
+Addsimps [hypreal_of_real_eq_iff];
+
+Goalw [hypreal_of_real_def] "hypreal_of_real (-r) = - hypreal_of_real  r";
+by (auto_tac (claset(),simpset() addsimps [hypreal_minus]));
+qed "hypreal_of_real_minus";
+Addsimps [hypreal_of_real_minus];
+
+(*DON'T insert this or the next one as default simprules.
+  They are used in both orientations and anyway aren't the ones we finally
+  need, which would use binary literals.*)
+Goalw [hypreal_of_real_def,hypreal_one_def] "hypreal_of_real  #1 = 1hr";
+by (Step_tac 1);
+qed "hypreal_of_real_one";
+
+Goalw [hypreal_of_real_def,hypreal_zero_def] "hypreal_of_real #0 = 0";
+by (Step_tac 1);
+qed "hypreal_of_real_zero";
+
+Goal "(hypreal_of_real r = 0) = (r = #0)";
+by (auto_tac (claset() addIs [FreeUltrafilterNat_P],
+    simpset() addsimps [hypreal_of_real_def,
+                        hypreal_zero_def,FreeUltrafilterNat_Nat_set]));
+qed "hypreal_of_real_zero_iff";
+
+Goal "hypreal_of_real (inverse r) = inverse (hypreal_of_real r)";
+by (case_tac "r=#0" 1);
+by (asm_simp_tac (simpset() addsimps [REAL_DIVIDE_ZERO, INVERSE_ZERO, 
+                              HYPREAL_INVERSE_ZERO, hypreal_of_real_zero]) 1);
+by (res_inst_tac [("c1","hypreal_of_real r")] 
+    (hypreal_mult_left_cancel RS iffD1) 1);
+by (stac (hypreal_of_real_mult RS sym) 2); 
+by (auto_tac (claset(), 
+         simpset() addsimps [hypreal_of_real_one, hypreal_of_real_zero_iff]));
+qed "hypreal_of_real_inverse";
+Addsimps [hypreal_of_real_inverse];
+
+Goal "hypreal_of_real (z1 / z2) = hypreal_of_real z1 / hypreal_of_real z2";
+by (simp_tac (simpset() addsimps [hypreal_divide_def, real_divide_def]) 1);
+qed "hypreal_of_real_divide"; 
+Addsimps [hypreal_of_real_divide];
+
+
+(*** Division lemmas ***)
+
+Goal "(0::hypreal)/x = 0";
+by (simp_tac (simpset() addsimps [hypreal_divide_def]) 1); 
+qed "hypreal_zero_divide";
+
+Goal "x/1hr = x";
+by (simp_tac (simpset() addsimps [hypreal_divide_def]) 1); 
+qed "hypreal_divide_one";
+Addsimps [hypreal_zero_divide, hypreal_divide_one];
+
+Goal "(x::hypreal) * (y/z) = (x*y)/z";
+by (simp_tac (simpset() addsimps [hypreal_divide_def, hypreal_mult_assoc]) 1); 
+qed "hypreal_times_divide1_eq";
+
+Goal "(y/z) * (x::hypreal) = (y*x)/z";
+by (simp_tac (simpset() addsimps [hypreal_divide_def]@hypreal_mult_ac) 1); 
+qed "hypreal_times_divide2_eq";
+
+Addsimps [hypreal_times_divide1_eq, hypreal_times_divide2_eq];
+
+Goal "(x::hypreal) / (y/z) = (x*z)/y";
+by (simp_tac (simpset() addsimps [hypreal_divide_def, hypreal_inverse_distrib]@
+                                 hypreal_mult_ac) 1); 
+qed "hypreal_divide_divide1_eq";
+
+Goal "((x::hypreal) / y) / z = x/(y*z)";
+by (simp_tac (simpset() addsimps [hypreal_divide_def, hypreal_inverse_distrib, 
+                                  hypreal_mult_assoc]) 1); 
+qed "hypreal_divide_divide2_eq";
+
+Addsimps [hypreal_divide_divide1_eq, hypreal_divide_divide2_eq];
+
+(** As with multiplication, pull minus signs OUT of the / operator **)
+
+Goal "(-x) / (y::hypreal) = - (x/y)";
+by (simp_tac (simpset() addsimps [hypreal_divide_def]) 1); 
+qed "hypreal_minus_divide_eq";
+Addsimps [hypreal_minus_divide_eq];
+
+Goal "(x / -(y::hypreal)) = - (x/y)";
+by (simp_tac (simpset() addsimps [hypreal_divide_def, hypreal_minus_inverse]) 1); 
+qed "hypreal_divide_minus_eq";
+Addsimps [hypreal_divide_minus_eq];
+
+Goal "(x+y)/(z::hypreal) = x/z + y/z";
+by (simp_tac (simpset() addsimps [hypreal_divide_def, 
+                                  hypreal_add_mult_distrib]) 1); 
+qed "hypreal_add_divide_distrib";
+
+Goal "[|(x::hypreal) ~= 0;  y ~= 0 |]  \
+\     ==> inverse(x) + inverse(y) = (x + y)*inverse(x*y)";
+by (asm_full_simp_tac (simpset() addsimps [hypreal_inverse_distrib,
+                    hypreal_add_mult_distrib,hypreal_mult_assoc RS sym]) 1);
+by (stac hypreal_mult_assoc 1);
+by (rtac (hypreal_mult_left_commute RS subst) 1);
+by (asm_full_simp_tac (simpset() addsimps [hypreal_add_commute]) 1);
+qed "hypreal_inverse_add";
+
+Goal "x = -x ==> x = (0::hypreal)";
+by (res_inst_tac [("z","x")] eq_Abs_hypreal 1);
+by (auto_tac (claset(), simpset() addsimps [hypreal_minus, hypreal_zero_def]));
+by (Ultra_tac 1);
+qed "hypreal_self_eq_minus_self_zero";
+
+Goal "(x + x = 0) = (x = (0::hypreal))";
+by (res_inst_tac [("z","x")] eq_Abs_hypreal 1);
+by (auto_tac (claset(), simpset() addsimps [hypreal_add, hypreal_zero_def]));
+qed "hypreal_add_self_zero_cancel";
+Addsimps [hypreal_add_self_zero_cancel];
+
+Goal "(x + x + y = y) = (x = (0::hypreal))";
+by Auto_tac;
+by (dtac (hypreal_eq_minus_iff RS iffD1) 1);
+by (auto_tac (claset(), 
+     simpset() addsimps [hypreal_add_assoc, hypreal_self_eq_minus_self_zero]));
+qed "hypreal_add_self_zero_cancel2";
+Addsimps [hypreal_add_self_zero_cancel2];
+
+Goal "(x + (x + y) = y) = (x = (0::hypreal))";
+by (simp_tac (simpset() addsimps [hypreal_add_assoc RS sym]) 1);
+qed "hypreal_add_self_zero_cancel2a";
+Addsimps [hypreal_add_self_zero_cancel2a];
+
+Goal "(b = -a) = (-b = (a::hypreal))";
+by Auto_tac;
+qed "hypreal_minus_eq_swap";
+
+Goal "(-b = -a) = (b = (a::hypreal))";
+by (asm_full_simp_tac (simpset() addsimps 
+    [hypreal_minus_eq_swap]) 1);
+qed "hypreal_minus_eq_cancel";
+Addsimps [hypreal_minus_eq_cancel];
+
+Goal "x < x + 1hr";
+by (res_inst_tac [("z","x")] eq_Abs_hypreal 1);
+by (auto_tac (claset(),
+              simpset() addsimps [hypreal_add, hypreal_one_def,hypreal_less]));
+qed "hypreal_less_self_add_one";
+Addsimps [hypreal_less_self_add_one];
+
+(*??DELETE MOST OF THE FOLLOWING??*)
+Goal "((x::hypreal) + x = y + y) = (x = y)";
+by (res_inst_tac [("z","x")] eq_Abs_hypreal 1);
+by (res_inst_tac [("z","y")] eq_Abs_hypreal 1);
+by (auto_tac (claset(),simpset() addsimps [hypreal_add]));
+by (ALLGOALS(Ultra_tac));
+qed "hypreal_add_self_cancel";
+Addsimps [hypreal_add_self_cancel];
+
+Goal "(y = x + - y + x) = (y = (x::hypreal))";
+by Auto_tac;
+by (dres_inst_tac [("x1","y")] 
+    (hypreal_add_right_cancel RS iffD2) 1);
+by (auto_tac (claset(),simpset() addsimps hypreal_add_ac));
+qed "hypreal_add_self_minus_cancel";
+Addsimps [hypreal_add_self_minus_cancel];
+
+Goal "(y = x + (- y + x)) = (y = (x::hypreal))";
+by (asm_full_simp_tac (simpset() addsimps 
+         [hypreal_add_assoc RS sym])1);
+qed "hypreal_add_self_minus_cancel2";
+Addsimps [hypreal_add_self_minus_cancel2];
+
+(* of course, can prove this by "transfer" as well *)
+Goal "z + -x = y + (y + (-x + -z)) = (y = (z::hypreal))";
+by Auto_tac;
+by (dres_inst_tac [("x1","z")] 
+    (hypreal_add_right_cancel RS iffD2) 1);
+by (asm_full_simp_tac (simpset() addsimps 
+    [hypreal_minus_add_distrib RS sym] @ hypreal_add_ac
+    delsimps [hypreal_minus_add_distrib]) 1);
+by (asm_full_simp_tac (simpset() addsimps 
+     [hypreal_add_assoc RS sym,hypreal_add_right_cancel]) 1);
+qed "hypreal_add_self_minus_cancel3";
+Addsimps [hypreal_add_self_minus_cancel3];
+
+Goalw [hypreal_diff_def] "(x<y) = (x-y < (0::hypreal))";
+by (rtac hypreal_less_minus_iff2 1);
+qed "hypreal_less_eq_diff";
+
+(*** Subtraction laws ***)
+
+Goal "x + (y - z) = (x + y) - (z::hypreal)";
+by (simp_tac (simpset() addsimps hypreal_diff_def::hypreal_add_ac) 1);
+qed "hypreal_add_diff_eq";
+
+Goal "(x - y) + z = (x + z) - (y::hypreal)";
+by (simp_tac (simpset() addsimps hypreal_diff_def::hypreal_add_ac) 1);
+qed "hypreal_diff_add_eq";
+
+Goal "(x - y) - z = x - (y + (z::hypreal))";
+by (simp_tac (simpset() addsimps hypreal_diff_def::hypreal_add_ac) 1);
+qed "hypreal_diff_diff_eq";
+
+Goal "x - (y - z) = (x + z) - (y::hypreal)";
+by (simp_tac (simpset() addsimps hypreal_diff_def::hypreal_add_ac) 1);
+qed "hypreal_diff_diff_eq2";
+
+Goal "(x-y < z) = (x < z + (y::hypreal))";
+by (stac hypreal_less_eq_diff 1);
+by (res_inst_tac [("y1", "z")] (hypreal_less_eq_diff RS ssubst) 1);
+by (simp_tac (simpset() addsimps hypreal_diff_def::hypreal_add_ac) 1);
+qed "hypreal_diff_less_eq";
+
+Goal "(x < z-y) = (x + (y::hypreal) < z)";
+by (stac hypreal_less_eq_diff 1);
+by (res_inst_tac [("y1", "z-y")] (hypreal_less_eq_diff RS ssubst) 1);
+by (simp_tac (simpset() addsimps hypreal_diff_def::hypreal_add_ac) 1);
+qed "hypreal_less_diff_eq";
+
+Goalw [hypreal_le_def] "(x-y <= z) = (x <= z + (y::hypreal))";
+by (simp_tac (simpset() addsimps [hypreal_less_diff_eq]) 1);
+qed "hypreal_diff_le_eq";
+
+Goalw [hypreal_le_def] "(x <= z-y) = (x + (y::hypreal) <= z)";
+by (simp_tac (simpset() addsimps [hypreal_diff_less_eq]) 1);
+qed "hypreal_le_diff_eq";
+
+Goalw [hypreal_diff_def] "(x-y = z) = (x = z + (y::hypreal))";
+by (auto_tac (claset(), simpset() addsimps [hypreal_add_assoc]));
+qed "hypreal_diff_eq_eq";
+
+Goalw [hypreal_diff_def] "(x = z-y) = (x + (y::hypreal) = z)";
+by (auto_tac (claset(), simpset() addsimps [hypreal_add_assoc]));
+qed "hypreal_eq_diff_eq";
+
+(*This list of rewrites simplifies (in)equalities by bringing subtractions
+  to the top and then moving negative terms to the other side.  
+  Use with hypreal_add_ac*)
+val hypreal_compare_rls = 
+  [symmetric hypreal_diff_def,
+   hypreal_add_diff_eq, hypreal_diff_add_eq, hypreal_diff_diff_eq, 
+   hypreal_diff_diff_eq2, 
+   hypreal_diff_less_eq, hypreal_less_diff_eq, hypreal_diff_le_eq, 
+   hypreal_le_diff_eq, hypreal_diff_eq_eq, hypreal_eq_diff_eq];
+
+
+(** For the cancellation simproc.
+    The idea is to cancel like terms on opposite sides by subtraction **)
+
+Goal "(x::hypreal) - y = x' - y' ==> (x<y) = (x'<y')";
+by (stac hypreal_less_eq_diff 1);
+by (res_inst_tac [("y1", "y")] (hypreal_less_eq_diff RS ssubst) 1);
+by (Asm_simp_tac 1);
+qed "hypreal_less_eqI";
+
+Goal "(x::hypreal) - y = x' - y' ==> (y<=x) = (y'<=x')";
+by (dtac hypreal_less_eqI 1);
+by (asm_simp_tac (simpset() addsimps [hypreal_le_def]) 1);
+qed "hypreal_le_eqI";
+
+Goal "(x::hypreal) - y = x' - y' ==> (x=y) = (x'=y')";
+by Safe_tac;
+by (ALLGOALS
+    (asm_full_simp_tac
+     (simpset() addsimps [hypreal_eq_diff_eq, hypreal_diff_eq_eq])));
+qed "hypreal_eq_eqI";
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Hyperreal/HyperDef.thy	Sat Dec 30 22:03:47 2000 +0100
@@ -0,0 +1,98 @@
+(*  Title       : HOL/Real/Hyperreal/HyperDef.thy
+    ID          : $Id$
+    Author      : Jacques D. Fleuriot
+    Copyright   : 1998  University of Cambridge
+    Description : Construction of hyperreals using ultrafilters
+*) 
+
+HyperDef = Filter + Real +
+
+consts 
+ 
+    FreeUltrafilterNat   :: nat set set    ("\\<U>")
+
+defs
+
+    FreeUltrafilterNat_def
+    "FreeUltrafilterNat    ==   (@U. U : FreeUltrafilter (UNIV:: nat set))"
+
+
+constdefs
+    hyprel :: "((nat=>real)*(nat=>real)) set"
+    "hyprel == {p. ? X Y. p = ((X::nat=>real),Y) & 
+                   {n::nat. X(n) = Y(n)}: FreeUltrafilterNat}"
+
+typedef hypreal = "UNIV//hyprel"   (Equiv.quotient_def)
+
+instance
+   hypreal  :: {ord, zero, plus, times, minus, inverse}
+
+consts 
+
+  "1hr"       :: hypreal               ("1hr")  
+  "whr"       :: hypreal               ("whr")  
+  "ehr"       :: hypreal               ("ehr")  
+
+
+defs
+
+  hypreal_zero_def
+  "0 == Abs_hypreal(hyprel^^{%n::nat. (#0::real)})"
+
+  hypreal_one_def
+  "1hr == Abs_hypreal(hyprel^^{%n::nat. (#1::real)})"
+
+  (* an infinite number = [<1,2,3,...>] *)
+  omega_def
+  "whr == Abs_hypreal(hyprel^^{%n::nat. real_of_posnat n})"
+    
+  (* an infinitesimal number = [<1,1/2,1/3,...>] *)
+  epsilon_def
+  "ehr == Abs_hypreal(hyprel^^{%n::nat. inverse (real_of_posnat n)})"
+
+  hypreal_minus_def
+  "- P == Abs_hypreal(UN X: Rep_hypreal(P). hyprel^^{%n::nat. - (X n)})"
+
+  hypreal_diff_def 
+  "x - y == x + -(y::hypreal)"
+
+  hypreal_inverse_def
+  "inverse P == Abs_hypreal(UN X: Rep_hypreal(P). 
+                    hyprel^^{%n. if X n = #0 then #0 else inverse (X n)})"
+
+  hypreal_divide_def
+  "P / Q::hypreal == P * inverse Q"
+  
+constdefs
+
+  hypreal_of_real  :: real => hypreal                 
+  "hypreal_of_real r         == Abs_hypreal(hyprel^^{%n::nat. r})"
+  
+  (* n::nat --> (n+1)::hypreal *)
+  hypreal_of_posnat :: nat => hypreal                
+  "hypreal_of_posnat n  == (hypreal_of_real(real_of_preal
+                            (preal_of_prat(prat_of_pnat(pnat_of_nat n)))))"
+
+  hypreal_of_nat :: nat => hypreal                   
+  "hypreal_of_nat n      == hypreal_of_posnat n + -1hr"
+
+defs 
+
+  hypreal_add_def  
+  "P + Q == Abs_hypreal(UN X:Rep_hypreal(P). UN Y:Rep_hypreal(Q).
+                hyprel^^{%n::nat. X n + Y n})"
+
+  hypreal_mult_def  
+  "P * Q == Abs_hypreal(UN X:Rep_hypreal(P). UN Y:Rep_hypreal(Q).
+                hyprel^^{%n::nat. X n * Y n})"
+
+  hypreal_less_def
+  "P < (Q::hypreal) == EX X Y. X: Rep_hypreal(P) & 
+                               Y: Rep_hypreal(Q) & 
+                               {n::nat. X n < Y n} : FreeUltrafilterNat"
+  hypreal_le_def
+  "P <= (Q::hypreal) == ~(Q < P)" 
+
+end
+ 
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Hyperreal/HyperNat.ML	Sat Dec 30 22:03:47 2000 +0100
@@ -0,0 +1,1335 @@
+(*  Title       : HyperNat.ML
+    Author      : Jacques D. Fleuriot
+    Copyright   : 1998  University of Cambridge
+    Description : Explicit construction of hypernaturals using 
+                  ultrafilters
+*) 
+       
+(*------------------------------------------------------------------------
+                       Properties of hypnatrel
+ ------------------------------------------------------------------------*)
+
+(** Proving that hyprel is an equivalence relation       **)
+(** Natural deduction for hypnatrel - similar to hyprel! **)
+
+Goalw [hypnatrel_def]
+     "((X,Y): hypnatrel) = ({n. X n = Y n}: FreeUltrafilterNat)";
+by (Fast_tac 1);
+qed "hypnatrel_iff";
+
+Goalw [hypnatrel_def] 
+     "{n. X n = Y n}: FreeUltrafilterNat ==> (X,Y): hypnatrel";
+by (Fast_tac 1);
+qed "hypnatrelI";
+
+Goalw [hypnatrel_def]
+  "p: hypnatrel --> (EX X Y. \
+\                 p = (X,Y) & {n. X n = Y n} : FreeUltrafilterNat)";
+by (Fast_tac 1);
+qed "hypnatrelE_lemma";
+
+val [major,minor] = Goal
+  "[| p: hypnatrel;  \
+\     !!X Y. [| p = (X,Y); {n. X n = Y n}: FreeUltrafilterNat |] \
+\            ==> Q |] \
+\  ==> Q";
+by (cut_facts_tac [major RS (hypnatrelE_lemma RS mp)] 1);
+by (REPEAT (eresolve_tac [asm_rl,exE,conjE,minor] 1));
+qed "hypnatrelE";
+
+AddSIs [hypnatrelI];
+AddSEs [hypnatrelE];
+
+Goalw [hypnatrel_def] "(x,x): hypnatrel";
+by (Auto_tac);
+qed "hypnatrel_refl";
+
+Goalw [hypnatrel_def] "(x,y): hypnatrel --> (y,x):hypnatrel";
+by (auto_tac (claset() addIs [lemma_perm RS subst], simpset()));
+qed_spec_mp "hypnatrel_sym";
+
+Goalw [hypnatrel_def]
+     "(x,y): hypnatrel --> (y,z):hypnatrel --> (x,z):hypnatrel";
+by (Auto_tac);
+by (Fuf_tac 1);
+qed_spec_mp "hypnatrel_trans";
+
+Goalw [equiv_def, refl_def, sym_def, trans_def]
+     "equiv UNIV hypnatrel";
+by (auto_tac (claset() addSIs [hypnatrel_refl] 
+                       addSEs [hypnatrel_sym,hypnatrel_trans] 
+                       delrules [hypnatrelI,hypnatrelE],
+              simpset()));
+qed "equiv_hypnatrel";
+
+val equiv_hypnatrel_iff =
+    [UNIV_I, UNIV_I] MRS (equiv_hypnatrel RS eq_equiv_class_iff);
+
+Goalw  [hypnat_def,hypnatrel_def,quotient_def] "hypnatrel^^{x}:hypnat";
+by (Blast_tac 1);
+qed "hypnatrel_in_hypnat";
+
+Goal "inj_on Abs_hypnat hypnat";
+by (rtac inj_on_inverseI 1);
+by (etac Abs_hypnat_inverse 1);
+qed "inj_on_Abs_hypnat";
+
+Addsimps [equiv_hypnatrel_iff,inj_on_Abs_hypnat RS inj_on_iff,
+          hypnatrel_iff, hypnatrel_in_hypnat, Abs_hypnat_inverse];
+
+Addsimps [equiv_hypnatrel RS eq_equiv_class_iff];
+val eq_hypnatrelD = equiv_hypnatrel RSN (2,eq_equiv_class);
+
+Goal "inj(Rep_hypnat)";
+by (rtac inj_inverseI 1);
+by (rtac Rep_hypnat_inverse 1);
+qed "inj_Rep_hypnat";
+
+Goalw [hypnatrel_def] "x: hypnatrel ^^ {x}";
+by (Step_tac 1);
+by (Auto_tac);
+qed "lemma_hypnatrel_refl";
+
+Addsimps [lemma_hypnatrel_refl];
+
+Goalw [hypnat_def] "{} ~: hypnat";
+by (auto_tac (claset() addSEs [quotientE],simpset()));
+qed "hypnat_empty_not_mem";
+
+Addsimps [hypnat_empty_not_mem];
+
+Goal "Rep_hypnat x ~= {}";
+by (cut_inst_tac [("x","x")] Rep_hypnat 1);
+by (Auto_tac);
+qed "Rep_hypnat_nonempty";
+
+Addsimps [Rep_hypnat_nonempty];
+
+(*------------------------------------------------------------------------
+   hypnat_of_nat: the injection from nat to hypnat
+ ------------------------------------------------------------------------*)
+Goal "inj(hypnat_of_nat)";
+by (rtac injI 1);
+by (rewtac hypnat_of_nat_def);
+by (dtac (inj_on_Abs_hypnat RS inj_onD) 1);
+by (REPEAT (rtac hypnatrel_in_hypnat 1));
+by (dtac eq_equiv_class 1);
+by (rtac equiv_hypnatrel 1);
+by (Fast_tac 1);
+by (rtac ccontr 1 THEN rotate_tac 1 1);
+by (Auto_tac);
+qed "inj_hypnat_of_nat";
+
+val [prem] = Goal
+    "(!!x. z = Abs_hypnat(hypnatrel^^{x}) ==> P) ==> P";
+by (res_inst_tac [("x1","z")] 
+    (rewrite_rule [hypnat_def] Rep_hypnat RS quotientE) 1);
+by (dres_inst_tac [("f","Abs_hypnat")] arg_cong 1);
+by (res_inst_tac [("x","x")] prem 1);
+by (asm_full_simp_tac (simpset() addsimps [Rep_hypnat_inverse]) 1);
+qed "eq_Abs_hypnat";
+
+(*-----------------------------------------------------------
+   Addition for hyper naturals: hypnat_add 
+ -----------------------------------------------------------*)
+Goalw [congruent2_def]
+     "congruent2 hypnatrel (%X Y. hypnatrel^^{%n. X n + Y n})";
+by Safe_tac;
+by (ALLGOALS(Fuf_tac));
+qed "hypnat_add_congruent2";
+
+Goalw [hypnat_add_def]
+  "Abs_hypnat(hypnatrel^^{%n. X n}) + Abs_hypnat(hypnatrel^^{%n. Y n}) = \
+\  Abs_hypnat(hypnatrel^^{%n. X n + Y n})";
+by (asm_simp_tac
+    (simpset() addsimps [[equiv_hypnatrel, hypnat_add_congruent2] 
+     MRS UN_equiv_class2]) 1);
+qed "hypnat_add";
+
+Goal "(z::hypnat) + w = w + z";
+by (res_inst_tac [("z","z")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","w")] eq_Abs_hypnat 1);
+by (asm_simp_tac (simpset() addsimps (add_ac @ [hypnat_add])) 1);
+qed "hypnat_add_commute";
+
+Goal "((z1::hypnat) + z2) + z3 = z1 + (z2 + z3)";
+by (res_inst_tac [("z","z1")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","z2")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","z3")] eq_Abs_hypnat 1);
+by (asm_simp_tac (simpset() addsimps [hypnat_add,add_assoc]) 1);
+qed "hypnat_add_assoc";
+
+(*For AC rewriting*)
+Goal "(x::hypnat)+(y+z)=y+(x+z)";
+by (rtac (hypnat_add_commute RS trans) 1);
+by (rtac (hypnat_add_assoc RS trans) 1);
+by (rtac (hypnat_add_commute RS arg_cong) 1);
+qed "hypnat_add_left_commute";
+
+(* hypnat addition is an AC operator *)
+val hypnat_add_ac = [hypnat_add_assoc,hypnat_add_commute,
+                      hypnat_add_left_commute];
+
+Goalw [hypnat_zero_def] "(0::hypnat) + z = z";
+by (res_inst_tac [("z","z")] eq_Abs_hypnat 1);
+by (asm_full_simp_tac (simpset() addsimps [hypnat_add]) 1);
+qed "hypnat_add_zero_left";
+
+Goal "z + (0::hypnat) = z";
+by (simp_tac (simpset() addsimps 
+    [hypnat_add_zero_left,hypnat_add_commute]) 1);
+qed "hypnat_add_zero_right";
+
+Addsimps [hypnat_add_zero_left,hypnat_add_zero_right];
+
+(*-----------------------------------------------------------
+   Subtraction for hyper naturals: hypnat_minus
+ -----------------------------------------------------------*)
+Goalw [congruent2_def]
+    "congruent2 hypnatrel (%X Y. hypnatrel^^{%n. X n - Y n})";
+by Safe_tac;
+by (ALLGOALS(Fuf_tac));
+qed "hypnat_minus_congruent2";
+ 
+Goalw [hypnat_minus_def]
+  "Abs_hypnat(hypnatrel^^{%n. X n}) - Abs_hypnat(hypnatrel^^{%n. Y n}) = \
+\  Abs_hypnat(hypnatrel^^{%n. X n - Y n})";
+by (asm_simp_tac
+    (simpset() addsimps [[equiv_hypnatrel, hypnat_minus_congruent2] 
+     MRS UN_equiv_class2]) 1);
+qed "hypnat_minus";
+
+Goalw [hypnat_zero_def] "z - z = (0::hypnat)";
+by (res_inst_tac [("z","z")] eq_Abs_hypnat 1);
+by (asm_full_simp_tac (simpset() addsimps [hypnat_minus]) 1);
+qed "hypnat_minus_zero";
+
+Goalw [hypnat_zero_def] "(0::hypnat) - n = 0";
+by (res_inst_tac [("z","n")] eq_Abs_hypnat 1);
+by (asm_full_simp_tac (simpset() addsimps [hypnat_minus]) 1);
+qed "hypnat_diff_0_eq_0";
+
+Addsimps [hypnat_minus_zero,hypnat_diff_0_eq_0];
+
+Goalw [hypnat_zero_def] "(m+n = (0::hypnat)) = (m=0 & n=0)";
+by (res_inst_tac [("z","n")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","m")] eq_Abs_hypnat 1);
+by (auto_tac (claset() addIs [FreeUltrafilterNat_subset]
+                       addDs [FreeUltrafilterNat_Int],
+              simpset() addsimps [hypnat_add] ));
+qed "hypnat_add_is_0";
+
+AddIffs [hypnat_add_is_0];
+
+Goal "!!i::hypnat. i-j-k = i - (j+k)";
+by (res_inst_tac [("z","i")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","j")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","k")] eq_Abs_hypnat 1);
+by (asm_full_simp_tac (simpset() addsimps 
+    [hypnat_minus,hypnat_add,diff_diff_left]) 1);
+qed "hypnat_diff_diff_left";
+
+Goal "!!i::hypnat. i-j-k = i-k-j";
+by (simp_tac (simpset() addsimps 
+    [hypnat_diff_diff_left, hypnat_add_commute]) 1);
+qed "hypnat_diff_commute";
+
+Goal "!!n::hypnat. (n+m) - n = m";
+by (res_inst_tac [("z","n")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","m")] eq_Abs_hypnat 1);
+by (asm_full_simp_tac (simpset() addsimps [hypnat_minus,hypnat_add]) 1);
+qed "hypnat_diff_add_inverse";
+Addsimps [hypnat_diff_add_inverse];
+
+Goal "!!n::hypnat.(m+n) - n = m";
+by (res_inst_tac [("z","n")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","m")] eq_Abs_hypnat 1);
+by (asm_full_simp_tac (simpset() addsimps [hypnat_minus,hypnat_add]) 1);
+qed "hypnat_diff_add_inverse2";
+Addsimps [hypnat_diff_add_inverse2];
+
+Goal "!!k::hypnat. (k+m) - (k+n) = m - n";
+by (res_inst_tac [("z","n")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","m")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","k")] eq_Abs_hypnat 1);
+by (asm_full_simp_tac (simpset() addsimps [hypnat_minus,hypnat_add]) 1);
+qed "hypnat_diff_cancel";
+Addsimps [hypnat_diff_cancel];
+
+Goal "!!m::hypnat. (m+k) - (n+k) = m - n";
+val hypnat_add_commute_k = read_instantiate [("w","k")] hypnat_add_commute;
+by (asm_simp_tac (simpset() addsimps ([hypnat_add_commute_k])) 1);
+qed "hypnat_diff_cancel2";
+Addsimps [hypnat_diff_cancel2];
+
+Goalw [hypnat_zero_def] "!!n::hypnat. n - (n+m) = (0::hypnat)";
+by (res_inst_tac [("z","n")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","m")] eq_Abs_hypnat 1);
+by (asm_full_simp_tac (simpset() addsimps [hypnat_minus,hypnat_add]) 1);
+qed "hypnat_diff_add_0";
+Addsimps [hypnat_diff_add_0];
+
+(*-----------------------------------------------------------
+   Multiplication for hyper naturals: hypnat_mult
+ -----------------------------------------------------------*)
+Goalw [congruent2_def]
+    "congruent2 hypnatrel (%X Y. hypnatrel^^{%n. X n * Y n})";
+by Safe_tac;
+by (ALLGOALS(Fuf_tac));
+qed "hypnat_mult_congruent2";
+
+Goalw [hypnat_mult_def]
+  "Abs_hypnat(hypnatrel^^{%n. X n}) * Abs_hypnat(hypnatrel^^{%n. Y n}) = \
+\  Abs_hypnat(hypnatrel^^{%n. X n * Y n})";
+by (asm_simp_tac
+    (simpset() addsimps [[equiv_hypnatrel,hypnat_mult_congruent2] MRS
+     UN_equiv_class2]) 1);
+qed "hypnat_mult";
+
+Goal "(z::hypnat) * w = w * z";
+by (res_inst_tac [("z","z")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","w")] eq_Abs_hypnat 1);
+by (asm_simp_tac (simpset() addsimps ([hypnat_mult] @ mult_ac)) 1);
+qed "hypnat_mult_commute";
+
+Goal "((z1::hypnat) * z2) * z3 = z1 * (z2 * z3)";
+by (res_inst_tac [("z","z1")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","z2")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","z3")] eq_Abs_hypnat 1);
+by (asm_simp_tac (simpset() addsimps [hypnat_mult,mult_assoc]) 1);
+qed "hypnat_mult_assoc";
+
+
+Goal "(z1::hypnat) * (z2 * z3) = z2 * (z1 * z3)";
+by (rtac (hypnat_mult_commute RS trans) 1);
+by (rtac (hypnat_mult_assoc RS trans) 1);
+by (rtac (hypnat_mult_commute RS arg_cong) 1);
+qed "hypnat_mult_left_commute";
+
+(* hypnat multiplication is an AC operator *)
+val hypnat_mult_ac = [hypnat_mult_assoc, hypnat_mult_commute, 
+                      hypnat_mult_left_commute];
+
+Goalw [hypnat_one_def] "1hn * z = z";
+by (res_inst_tac [("z","z")] eq_Abs_hypnat 1);
+by (asm_full_simp_tac (simpset() addsimps [hypnat_mult]) 1);
+qed "hypnat_mult_1";
+Addsimps [hypnat_mult_1];
+
+Goal "z * 1hn = z";
+by (simp_tac (simpset() addsimps [hypnat_mult_commute]) 1);
+qed "hypnat_mult_1_right";
+Addsimps [hypnat_mult_1_right];
+
+Goalw [hypnat_zero_def] "(0::hypnat) * z = 0";
+by (res_inst_tac [("z","z")] eq_Abs_hypnat 1);
+by (asm_full_simp_tac (simpset() addsimps [hypnat_mult]) 1);
+qed "hypnat_mult_0";
+Addsimps [hypnat_mult_0];
+
+Goal "z * (0::hypnat) = 0";
+by (simp_tac (simpset() addsimps [hypnat_mult_commute]) 1);
+qed "hypnat_mult_0_right";
+Addsimps [hypnat_mult_0_right];
+
+Goal "!!m::hypnat. (m - n) * k = (m * k) - (n * k)";
+by (res_inst_tac [("z","m")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","n")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","k")] eq_Abs_hypnat 1);
+by (asm_simp_tac (simpset() addsimps [hypnat_mult,
+    hypnat_minus,diff_mult_distrib]) 1);
+qed "hypnat_diff_mult_distrib" ;
+
+Goal "!!m::hypnat. k * (m - n) = (k * m) - (k * n)";
+val hypnat_mult_commute_k = read_instantiate [("z","k")] hypnat_mult_commute;
+by (simp_tac (simpset() addsimps [hypnat_diff_mult_distrib, 
+                                  hypnat_mult_commute_k]) 1);
+qed "hypnat_diff_mult_distrib2" ;
+
+(*--------------------------
+    A Few more theorems
+ -------------------------*)
+
+Goal "(z::hypnat) + v = z' + v' ==> z + (v + w) = z' + (v' + w)";
+by (asm_simp_tac (simpset() addsimps [hypnat_add_assoc RS sym]) 1);
+qed "hypnat_add_assoc_cong";
+
+Goal "(z::hypnat) + (v + w) = v + (z + w)";
+by (REPEAT (ares_tac [hypnat_add_commute RS hypnat_add_assoc_cong] 1));
+qed "hypnat_add_assoc_swap";
+
+Goal "((z1::hypnat) + z2) * w = (z1 * w) + (z2 * w)";
+by (res_inst_tac [("z","z1")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","z2")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","w")] eq_Abs_hypnat 1);
+by (asm_simp_tac (simpset() addsimps [hypnat_mult,hypnat_add,
+                                      add_mult_distrib]) 1);
+qed "hypnat_add_mult_distrib";
+Addsimps [hypnat_add_mult_distrib];
+
+val hypnat_mult_commute'= read_instantiate [("z","w")] hypnat_mult_commute;
+
+Goal "(w::hypnat) * (z1 + z2) = (w * z1) + (w * z2)";
+by (simp_tac (simpset() addsimps [hypnat_mult_commute']) 1);
+qed "hypnat_add_mult_distrib2";
+Addsimps [hypnat_add_mult_distrib2];
+
+(*** (hypnat) one and zero are distinct ***)
+Goalw [hypnat_zero_def,hypnat_one_def] "(0::hypnat) ~= 1hn";
+by (Auto_tac);
+qed "hypnat_zero_not_eq_one";
+Addsimps [hypnat_zero_not_eq_one];
+Addsimps [hypnat_zero_not_eq_one RS not_sym];
+
+Goalw [hypnat_zero_def] "(m*n = (0::hypnat)) = (m=0 | n=0)";
+by (res_inst_tac [("z","m")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","n")] eq_Abs_hypnat 1);
+by (auto_tac (claset() addSDs [FreeUltrafilterNat_Compl_mem],
+    simpset() addsimps [hypnat_mult]));
+by (ALLGOALS(Fuf_tac));
+qed "hypnat_mult_is_0";
+Addsimps [hypnat_mult_is_0];
+
+(*------------------------------------------------------------------
+                   Theorems for ordering 
+ ------------------------------------------------------------------*)
+
+(* prove introduction and elimination rules for hypnat_less *)
+
+Goalw [hypnat_less_def]
+ "P < (Q::hypnat) = (EX X Y. X : Rep_hypnat(P) & \
+\                             Y : Rep_hypnat(Q) & \
+\                             {n. X n < Y n} : FreeUltrafilterNat)";
+by (Fast_tac 1);
+qed "hypnat_less_iff";
+
+Goalw [hypnat_less_def]
+ "!!P. [| {n. X n < Y n} : FreeUltrafilterNat; \
+\         X : Rep_hypnat(P); \
+\         Y : Rep_hypnat(Q) |] ==> P < (Q::hypnat)";
+by (Fast_tac 1);
+qed "hypnat_lessI";
+
+Goalw [hypnat_less_def]
+     "!! R1. [| R1 < (R2::hypnat); \
+\         !!X Y. {n. X n < Y n} : FreeUltrafilterNat ==> P; \
+\         !!X. X : Rep_hypnat(R1) ==> P; \ 
+\         !!Y. Y : Rep_hypnat(R2) ==> P |] \
+\     ==> P";
+by (Auto_tac);
+qed "hypnat_lessE";
+
+Goalw [hypnat_less_def]
+ "!!R1. R1 < (R2::hypnat) ==> (EX X Y. {n. X n < Y n} : FreeUltrafilterNat & \
+\                                  X : Rep_hypnat(R1) & \
+\                                  Y : Rep_hypnat(R2))";
+by (Fast_tac 1);
+qed "hypnat_lessD";
+
+Goal "~ (R::hypnat) < R";
+by (res_inst_tac [("z","R")] eq_Abs_hypnat 1);
+by (auto_tac (claset(),simpset() addsimps [hypnat_less_def]));
+by (Fuf_empty_tac 1);
+qed "hypnat_less_not_refl";
+Addsimps [hypnat_less_not_refl];
+
+bind_thm("hypnat_less_irrefl",hypnat_less_not_refl RS notE);
+
+Goalw [hypnat_less_def,hypnat_zero_def] "~ n<(0::hypnat)";
+by (res_inst_tac [("z","n")] eq_Abs_hypnat 1);
+by (Auto_tac);
+by (Fuf_empty_tac 1);
+qed "hypnat_not_less0";
+AddIffs [hypnat_not_less0];
+
+(* n<(0::hypnat) ==> R *)
+bind_thm ("hypnat_less_zeroE", hypnat_not_less0 RS notE);
+
+Goalw [hypnat_less_def,hypnat_zero_def,hypnat_one_def]
+      "(n<1hn) = (n=0)";
+by (res_inst_tac [("z","n")] eq_Abs_hypnat 1);
+by (auto_tac (claset() addSIs [exI] addEs 
+    [FreeUltrafilterNat_subset],simpset()));
+by (Fuf_tac 1);
+qed "hypnat_less_one";
+AddIffs [hypnat_less_one];
+
+Goal "!!(R1::hypnat). [| R1 < R2; R2 < R3 |] ==> R1 < R3";
+by (res_inst_tac [("z","R1")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","R2")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","R3")] eq_Abs_hypnat 1);
+by (auto_tac (claset(),simpset() addsimps [hypnat_less_def]));
+by (res_inst_tac [("x","X")] exI 1);
+by (Auto_tac);
+by (res_inst_tac [("x","Ya")] exI 1);
+by (Auto_tac);
+by (Fuf_tac 1);
+qed "hypnat_less_trans";
+
+Goal "!! (R1::hypnat). [| R1 < R2; R2 < R1 |] ==> P";
+by (dtac hypnat_less_trans 1 THEN assume_tac 1);
+by (Asm_full_simp_tac 1);
+qed "hypnat_less_asym";
+
+(*----- hypnat less iff less a.e -----*)
+(* See comments in HYPER for corresponding thm *)
+
+Goalw [hypnat_less_def]
+      "(Abs_hypnat(hypnatrel^^{%n. X n}) < \
+\           Abs_hypnat(hypnatrel^^{%n. Y n})) = \
+\      ({n. X n < Y n} : FreeUltrafilterNat)";
+by (auto_tac (claset() addSIs [lemma_hypnatrel_refl],simpset()));
+by (Fuf_tac 1);
+qed "hypnat_less";
+
+Goal "~ m<n --> n+(m-n) = (m::hypnat)";
+by (res_inst_tac [("z","n")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","m")] eq_Abs_hypnat 1);
+by (auto_tac (claset(),simpset() addsimps 
+    [hypnat_minus,hypnat_add,hypnat_less]));
+by (dtac FreeUltrafilterNat_Compl_mem 1);
+by (Fuf_tac 1);
+qed_spec_mp "hypnat_add_diff_inverse";
+
+Goal "n<=m ==> n+(m-n) = (m::hypnat)";
+by (asm_full_simp_tac (simpset() addsimps 
+    [hypnat_add_diff_inverse, hypnat_le_def]) 1);
+qed "hypnat_le_add_diff_inverse";
+
+Goal "n<=m ==> (m-n)+n = (m::hypnat)";
+by (asm_simp_tac (simpset() addsimps [hypnat_le_add_diff_inverse, 
+    hypnat_add_commute]) 1);
+qed "hypnat_le_add_diff_inverse2";
+
+(*---------------------------------------------------------------------------------
+                    Hyper naturals as a linearly ordered field
+ ---------------------------------------------------------------------------------*)
+Goalw [hypnat_zero_def] 
+     "[| (0::hypnat) < x; 0 < y |] ==> 0 < x + y";
+by (res_inst_tac [("z","x")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","y")] eq_Abs_hypnat 1);
+by (auto_tac (claset(),simpset() addsimps
+   [hypnat_less_def,hypnat_add]));
+by (REPEAT(Step_tac 1));
+by (Fuf_tac 1);
+qed "hypnat_add_order";
+
+Goalw [hypnat_zero_def] 
+      "!!(x::hypnat). [| (0::hypnat) < x; 0 < y |] ==> 0 < x * y";
+by (res_inst_tac [("z","x")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","y")] eq_Abs_hypnat 1);
+by (auto_tac (claset(),simpset() addsimps 
+    [hypnat_less_def,hypnat_mult]));
+by (REPEAT(Step_tac 1));
+by (Fuf_tac 1);
+qed "hypnat_mult_order";
+
+(*---------------------------------------------------------------------------------
+                   Trichotomy of the hyper naturals
+  --------------------------------------------------------------------------------*)
+Goalw [hypnatrel_def] "EX x. x: hypnatrel ^^ {%n. 0}";
+by (res_inst_tac [("x","%n. 0")] exI 1);
+by (Step_tac 1);
+by (Auto_tac);
+qed "lemma_hypnatrel_0_mem";
+
+(* linearity is actually proved further down! *)
+Goalw [hypnat_zero_def,
+       hypnat_less_def]"(0::hypnat) <  x | x = 0 | x < 0";
+by (res_inst_tac [("z","x")] eq_Abs_hypnat 1);
+by (Auto_tac );
+by (REPEAT(Step_tac 1));
+by (REPEAT(dtac FreeUltrafilterNat_Compl_mem 1));
+by (Fuf_tac 1);
+qed "hypnat_trichotomy";
+
+Goal "!!x. [| (0::hypnat) < x ==> P; \
+\                 x = 0 ==> P; \
+\                 x < 0 ==> P |] ==> P";
+by (cut_inst_tac [("x","x")] hypnat_trichotomy 1);
+by (Auto_tac);
+qed "hypnat_trichotomyE";
+
+(*------------------------------------------------------------------------------
+            More properties of <
+ ------------------------------------------------------------------------------*)
+Goal "!!(A::hypnat). A < B ==> A + C < B + C";
+by (res_inst_tac [("z","A")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","B")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","C")] eq_Abs_hypnat 1);
+by (auto_tac (claset(),simpset() addsimps 
+    [hypnat_less_def,hypnat_add]));
+by (REPEAT(Step_tac 1));
+by (Fuf_tac 1);
+qed "hypnat_add_less_mono1";
+
+Goal "!!(A::hypnat). A < B ==> C + A < C + B";
+by (auto_tac (claset() addIs [hypnat_add_less_mono1],
+    simpset() addsimps [hypnat_add_commute]));
+qed "hypnat_add_less_mono2";
+
+Goal "!!k l::hypnat. [|i<j;  k<l |] ==> i + k < j + l";
+by (etac (hypnat_add_less_mono1 RS hypnat_less_trans) 1);
+by (simp_tac (simpset() addsimps [hypnat_add_commute]) 1);
+(*j moves to the end because it is free while k, l are bound*)
+by (etac hypnat_add_less_mono1 1);
+qed "hypnat_add_less_mono";
+
+(*---------------------------------------
+        hypnat_minus_less
+ ---------------------------------------*)
+Goalw [hypnat_less_def,hypnat_zero_def] 
+      "((x::hypnat) < y) = ((0::hypnat) < y - x)";
+by (res_inst_tac [("z","x")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","y")] eq_Abs_hypnat 1);
+by (auto_tac (claset(),simpset() addsimps 
+    [hypnat_minus]));
+by (REPEAT(Step_tac 1));
+by (REPEAT(Step_tac 2));
+by (ALLGOALS(fuf_tac (claset() addDs [sym],simpset())));
+
+(*** linearity ***)
+Goalw [hypnat_less_def] "(x::hypnat) < y | x = y | y < x";
+by (res_inst_tac [("z","x")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","y")] eq_Abs_hypnat 1);
+by (Auto_tac );
+by (REPEAT(Step_tac 1));
+by (REPEAT(dtac FreeUltrafilterNat_Compl_mem 1));
+by (Fuf_tac 1);
+qed "hypnat_linear";
+
+Goal
+    "!!(x::hypnat). [| x < y ==> P;  x = y ==> P; \
+\          y < x ==> P |] ==> P";
+by (cut_inst_tac [("x","x"),("y","y")] hypnat_linear 1);
+by (Auto_tac);
+qed "hypnat_linear_less2";
+
+(*------------------------------------------------------------------------------
+                            Properties of <=
+ ------------------------------------------------------------------------------*)
+(*------ hypnat le iff nat le a.e ------*)
+Goalw [hypnat_le_def,le_def]
+      "(Abs_hypnat(hypnatrel^^{%n. X n}) <= \
+\           Abs_hypnat(hypnatrel^^{%n. Y n})) = \
+\      ({n. X n <= Y n} : FreeUltrafilterNat)";
+by (auto_tac (claset() addSDs [FreeUltrafilterNat_Compl_mem],
+    simpset() addsimps [hypnat_less]));
+by (Fuf_tac 1 THEN Fuf_empty_tac 1);
+qed "hypnat_le";
+
+(*---------------------------------------------------------*)
+(*---------------------------------------------------------*)
+Goalw [hypnat_le_def] "!!w. ~(w < z) ==> z <= (w::hypnat)";
+by (assume_tac 1);
+qed "hypnat_leI";
+
+Goalw [hypnat_le_def] "!!w. z<=w ==> ~(w<(z::hypnat))";
+by (assume_tac 1);
+qed "hypnat_leD";
+
+val hypnat_leE = make_elim hypnat_leD;
+
+Goal "!!w. (~(w < z)) = (z <= (w::hypnat))";
+by (fast_tac (claset() addSIs [hypnat_leI,hypnat_leD]) 1);
+qed "hypnat_less_le_iff";
+
+Goalw [hypnat_le_def] "!!z. ~ z <= w ==> w<(z::hypnat)";
+by (Fast_tac 1);
+qed "not_hypnat_leE";
+
+Goalw [hypnat_le_def] "!!z. z < w ==> z <= (w::hypnat)";
+by (fast_tac (claset() addEs [hypnat_less_asym]) 1);
+qed "hypnat_less_imp_le";
+
+Goalw [hypnat_le_def] "!!(x::hypnat). x <= y ==> x < y | x = y";
+by (cut_facts_tac [hypnat_linear] 1);
+by (fast_tac (claset() addEs [hypnat_less_irrefl,hypnat_less_asym]) 1);
+qed "hypnat_le_imp_less_or_eq";
+
+Goalw [hypnat_le_def] "!!z. z<w | z=w ==> z <=(w::hypnat)";
+by (cut_facts_tac [hypnat_linear] 1);
+by (fast_tac (claset() addEs [hypnat_less_irrefl,hypnat_less_asym]) 1);
+qed "hypnat_less_or_eq_imp_le";
+
+Goal "(x <= (y::hypnat)) = (x < y | x=y)";
+by (REPEAT(ares_tac [iffI, hypnat_less_or_eq_imp_le, hypnat_le_imp_less_or_eq] 1));
+qed "hypnat_le_eq_less_or_eq";
+
+Goal "w <= (w::hypnat)";
+by (simp_tac (simpset() addsimps [hypnat_le_eq_less_or_eq]) 1);
+qed "hypnat_le_refl";
+Addsimps [hypnat_le_refl];
+
+val prems = goal thy "!!i. [| i <= j; j < k |] ==> i < (k::hypnat)";
+by (dtac hypnat_le_imp_less_or_eq 1);
+by (fast_tac (claset() addIs [hypnat_less_trans]) 1);
+qed "hypnat_le_less_trans";
+
+Goal "!! (i::hypnat). [| i < j; j <= k |] ==> i < k";
+by (dtac hypnat_le_imp_less_or_eq 1);
+by (fast_tac (claset() addIs [hypnat_less_trans]) 1);
+qed "hypnat_less_le_trans";
+
+Goal "!!i. [| i <= j; j <= k |] ==> i <= (k::hypnat)";
+by (EVERY1 [dtac hypnat_le_imp_less_or_eq, dtac hypnat_le_imp_less_or_eq,
+            rtac hypnat_less_or_eq_imp_le, fast_tac (claset() addIs [hypnat_less_trans])]);
+qed "hypnat_le_trans";
+
+Goal "!!z. [| z <= w; w <= z |] ==> z = (w::hypnat)";
+by (EVERY1 [dtac hypnat_le_imp_less_or_eq, dtac hypnat_le_imp_less_or_eq,
+            fast_tac (claset() addEs [hypnat_less_irrefl,hypnat_less_asym])]);
+qed "hypnat_le_anti_sym";
+
+Goal "!!x. [| ~ y < x; y ~= x |] ==> x < (y::hypnat)";
+by (rtac not_hypnat_leE 1);
+by (fast_tac (claset() addDs [hypnat_le_imp_less_or_eq]) 1);
+qed "not_less_not_eq_hypnat_less";
+
+Goal "!!x. [| (0::hypnat) <= x; 0 <= y |] ==> 0 <= x * y";
+by (REPEAT(dtac hypnat_le_imp_less_or_eq 1));
+by (auto_tac (claset() addIs [hypnat_mult_order,
+    hypnat_less_imp_le],simpset() addsimps [hypnat_le_refl]));
+qed "hypnat_le_mult_order";
+
+Goalw [hypnat_one_def,hypnat_zero_def,hypnat_less_def] 
+      "(0::hypnat) < 1hn";
+by (res_inst_tac [("x","%n. 0")] exI 1);
+by (res_inst_tac [("x","%n. 1")] exI 1);
+by (Auto_tac);
+qed "hypnat_zero_less_one";
+
+Goal "!!x. [| (0::hypnat) <= x; 0 <= y |] ==> 0 <= x + y";
+by (REPEAT(dtac hypnat_le_imp_less_or_eq 1));
+by (auto_tac (claset() addIs [hypnat_add_order,
+    hypnat_less_imp_le],simpset() addsimps [hypnat_le_refl]));
+qed "hypnat_le_add_order";
+
+Goal "!!(q1::hypnat). q1 <= q2  ==> x + q1 <= x + q2";
+by (dtac hypnat_le_imp_less_or_eq 1);
+by (Step_tac 1);
+by (auto_tac (claset() addSIs [hypnat_le_refl,
+    hypnat_less_imp_le,hypnat_add_less_mono1],
+    simpset() addsimps [hypnat_add_commute]));
+qed "hypnat_add_le_mono2";
+
+Goal "!!(q1::hypnat). q1 <= q2  ==> q1 + x <= q2 + x";
+by (auto_tac (claset() addDs [hypnat_add_le_mono2],
+    simpset() addsimps [hypnat_add_commute]));
+qed "hypnat_add_le_mono1";
+
+Goal "!!k l::hypnat. [|i<=j;  k<=l |] ==> i + k <= j + l";
+by (etac (hypnat_add_le_mono1 RS hypnat_le_trans) 1);
+by (simp_tac (simpset() addsimps [hypnat_add_commute]) 1);
+(*j moves to the end because it is free while k, l are bound*)
+by (etac hypnat_add_le_mono1 1);
+qed "hypnat_add_le_mono";
+
+Goalw [hypnat_zero_def]
+     "!!x::hypnat. [| (0::hypnat) < z; x < y |] ==> x * z < y * z";
+by (res_inst_tac [("z","x")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","y")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","z")] eq_Abs_hypnat 1);
+by (auto_tac (claset(),simpset() addsimps 
+    [hypnat_less,hypnat_mult]));
+by (Fuf_tac 1);
+qed "hypnat_mult_less_mono1";
+
+Goal "!!x::hypnat. [| 0 < z; x < y |] ==> z * x < z * y";
+by (auto_tac (claset() addIs [hypnat_mult_less_mono1],
+              simpset() addsimps [hypnat_mult_commute]));
+qed "hypnat_mult_less_mono2";
+
+Addsimps [hypnat_mult_less_mono2,hypnat_mult_less_mono1];
+
+Goal "(x::hypnat) <= n + x";
+by (res_inst_tac [("x","n")] hypnat_trichotomyE 1);
+by (auto_tac (claset() addDs [(hypnat_less_imp_le RS
+    hypnat_add_le_mono1)],simpset() addsimps [hypnat_le_refl]));
+qed "hypnat_add_self_le";
+Addsimps [hypnat_add_self_le];
+
+Goal "(x::hypnat) < x + 1hn";
+by (cut_facts_tac [hypnat_zero_less_one 
+         RS hypnat_add_less_mono2] 1);
+by (Auto_tac);
+qed "hypnat_add_one_self_less";
+Addsimps [hypnat_add_one_self_less];
+
+Goalw [hypnat_le_def] "~ x + 1hn <= x";
+by (Simp_tac 1);
+qed "not_hypnat_add_one_le_self";
+Addsimps [not_hypnat_add_one_le_self];
+
+Goal "((0::hypnat) < n) = (1hn <= n)";
+by (res_inst_tac [("x","n")] hypnat_trichotomyE 1);
+by (auto_tac (claset(),simpset() addsimps [hypnat_le_def]));
+qed "hypnat_gt_zero_iff";
+
+Addsimps  [hypnat_le_add_diff_inverse, hypnat_le_add_diff_inverse2,
+           hypnat_less_imp_le RS hypnat_le_add_diff_inverse2];
+
+Goal "(0 < n) = (EX m. n = m + 1hn)";
+by (Step_tac 1);
+by (res_inst_tac [("x","m")] hypnat_trichotomyE 2);
+by (rtac hypnat_less_trans 2);
+by (res_inst_tac [("x","n - 1hn")] exI 1);
+by (auto_tac (claset(),simpset() addsimps 
+    [hypnat_gt_zero_iff,hypnat_add_commute]));
+qed "hypnat_gt_zero_iff2";
+
+Goalw [hypnat_zero_def] "(0::hypnat) <= n";
+by (res_inst_tac [("z","n")] eq_Abs_hypnat 1);
+by (asm_simp_tac (simpset() addsimps [hypnat_le]) 1);
+qed "hypnat_le_zero";
+Addsimps [hypnat_le_zero];
+
+(*------------------------------------------------------------------
+     hypnat_of_nat: properties embedding of naturals in hypernaturals
+ -----------------------------------------------------------------*)
+    (** hypnat_of_nat preserves field and order properties **)
+
+Goalw [hypnat_of_nat_def] 
+      "hypnat_of_nat ((z1::nat) + z2) = \
+\      hypnat_of_nat z1 + hypnat_of_nat z2";
+by (asm_simp_tac (simpset() addsimps [hypnat_add]) 1);
+qed "hypnat_of_nat_add";
+
+Goalw [hypnat_of_nat_def] 
+      "hypnat_of_nat ((z1::nat) - z2) = \
+\      hypnat_of_nat z1 - hypnat_of_nat z2";
+by (asm_simp_tac (simpset() addsimps [hypnat_minus]) 1);
+qed "hypnat_of_nat_minus";
+
+Goalw [hypnat_of_nat_def] 
+      "hypnat_of_nat (z1 * z2) = hypnat_of_nat z1 * hypnat_of_nat z2";
+by (full_simp_tac (simpset() addsimps [hypnat_mult]) 1);
+qed "hypnat_of_nat_mult";
+
+Goalw [hypnat_less_def,hypnat_of_nat_def] 
+      "(z1 < z2) = (hypnat_of_nat z1 < hypnat_of_nat z2)";
+by (auto_tac (claset() addSIs [exI] addIs 
+   [FreeUltrafilterNat_all],simpset()));
+by (rtac FreeUltrafilterNat_P 1 THEN Fuf_tac 1);
+qed "hypnat_of_nat_less_iff";
+Addsimps [hypnat_of_nat_less_iff RS sym];
+
+Goalw [hypnat_le_def,le_def] 
+      "(z1 <= z2) = (hypnat_of_nat z1 <= hypnat_of_nat z2)";
+by (Auto_tac);
+qed "hypnat_of_nat_le_iff";
+
+Goalw [hypnat_of_nat_def,hypnat_one_def] "hypnat_of_nat  1 = 1hn";
+by (Simp_tac 1);
+qed "hypnat_of_nat_one";
+
+Goalw [hypnat_of_nat_def,hypnat_zero_def] "hypnat_of_nat  0 = 0";
+by (Simp_tac 1);
+qed "hypnat_of_nat_zero";
+
+Goal "(hypnat_of_nat  n = 0) = (n = 0)";
+by (auto_tac (claset() addIs [FreeUltrafilterNat_P],
+    simpset() addsimps [hypnat_of_nat_def,
+    hypnat_zero_def]));
+qed "hypnat_of_nat_zero_iff";
+
+Goal "(hypnat_of_nat  n ~= 0) = (n ~= 0)";
+by (full_simp_tac (simpset() addsimps [hypnat_of_nat_zero_iff]) 1);
+qed "hypnat_of_nat_not_zero_iff";
+
+goalw HyperNat.thy [hypnat_of_nat_def,hypnat_one_def]
+      "hypnat_of_nat (Suc n) = hypnat_of_nat n + 1hn";
+by (auto_tac (claset(),simpset() addsimps [hypnat_add]));
+qed "hypnat_of_nat_Suc";
+
+(*---------------------------------------------------------------------------------
+              Existence of infinite hypernatural number
+ ---------------------------------------------------------------------------------*)
+
+Goal "hypnatrel^^{%n::nat. n} : hypnat";
+by (Auto_tac);
+qed "hypnat_omega";
+
+Goalw [hypnat_omega_def] "Rep_hypnat(whn) : hypnat";
+by (rtac Rep_hypnat 1);
+qed "Rep_hypnat_omega";
+
+(* See Hyper.thy for similar argument*)
+(* existence of infinite number not corresponding to any natural number *)
+(* use assumption that member FreeUltrafilterNat is not finite       *)
+(* a few lemmas first *)
+
+Goalw [hypnat_omega_def,hypnat_of_nat_def]
+      "~ (EX x. hypnat_of_nat x = whn)";
+by (auto_tac (claset() addDs [FreeUltrafilterNat_not_finite], 
+              simpset()));
+qed "not_ex_hypnat_of_nat_eq_omega";
+
+Goal "hypnat_of_nat x ~= whn";
+by (cut_facts_tac [not_ex_hypnat_of_nat_eq_omega] 1);
+by (Auto_tac);
+qed "hypnat_of_nat_not_eq_omega";
+Addsimps [hypnat_of_nat_not_eq_omega RS not_sym];
+
+(*-----------------------------------------------------------
+    Properties of the set SHNat of embedded natural numbers
+              (cf. set SReal in NSA.thy/NSA.ML)
+ ----------------------------------------------------------*)
+
+(* Infinite hypernatural not in embedded SHNat *)
+Goalw [SHNat_def] "whn ~: SHNat";
+by (Auto_tac);
+qed "SHNAT_omega_not_mem";
+Addsimps [SHNAT_omega_not_mem];
+
+(*-----------------------------------------------------------------------
+     Closure laws for members of (embedded) set standard naturals SHNat
+ -----------------------------------------------------------------------*)
+Goalw [SHNat_def] 
+      "!!x. [| x: SHNat; y: SHNat |] ==> x + y: SHNat";
+by (Step_tac 1);
+by (res_inst_tac [("x","N + Na")] exI 1);
+by (simp_tac (simpset() addsimps [hypnat_of_nat_add]) 1);
+qed "SHNat_add";
+
+Goalw [SHNat_def] 
+      "!!x. [| x: SHNat; y: SHNat |] ==> x - y: SHNat";
+by (Step_tac 1);
+by (res_inst_tac [("x","N - Na")] exI 1);
+by (simp_tac (simpset() addsimps [hypnat_of_nat_minus]) 1);
+qed "SHNat_minus";
+
+Goalw [SHNat_def] 
+      "!!x. [| x: SHNat; y: SHNat |] ==> x * y: SHNat";
+by (Step_tac 1);
+by (res_inst_tac [("x","N * Na")] exI 1);
+by (simp_tac (simpset() addsimps [hypnat_of_nat_mult]) 1);
+qed "SHNat_mult";
+
+Goal "!!x. [| x + y : SHNat; y: SHNat |] ==> x: SHNat";
+by (dres_inst_tac [("x","x+y")] SHNat_minus 1);
+by (Auto_tac);
+qed "SHNat_add_cancel";
+
+Goalw [SHNat_def] "hypnat_of_nat x : SHNat";
+by (Blast_tac 1);
+qed "SHNat_hypnat_of_nat";
+Addsimps [SHNat_hypnat_of_nat];
+
+Goal "hypnat_of_nat 1 : SHNat";
+by (Simp_tac 1);
+qed "SHNat_hypnat_of_nat_one";
+
+Goal "hypnat_of_nat 0 : SHNat";
+by (Simp_tac 1);
+qed "SHNat_hypnat_of_nat_zero";
+
+Goal "1hn : SHNat";
+by (simp_tac (simpset() addsimps [SHNat_hypnat_of_nat_one,
+    hypnat_of_nat_one RS sym]) 1);
+qed "SHNat_one";
+
+Goal "0 : SHNat";
+by (simp_tac (simpset() addsimps [SHNat_hypnat_of_nat_zero,
+    hypnat_of_nat_zero RS sym]) 1);
+qed "SHNat_zero";
+
+Addsimps [SHNat_hypnat_of_nat_one,SHNat_hypnat_of_nat_zero,
+          SHNat_one,SHNat_zero];
+
+Goal "1hn + 1hn : SHNat";
+by (rtac ([SHNat_one,SHNat_one] MRS SHNat_add) 1);
+qed "SHNat_two";
+
+Goalw [SHNat_def] "{x. hypnat_of_nat x : SHNat} = (UNIV::nat set)";
+by (Auto_tac);
+qed "SHNat_UNIV_nat";
+
+Goalw [SHNat_def] "(x: SHNat) = (EX y. x = hypnat_of_nat  y)";
+by (Auto_tac);
+qed "SHNat_iff";
+
+Goalw [SHNat_def] "hypnat_of_nat ``(UNIV::nat set) = SHNat";
+by (Auto_tac);
+qed "hypnat_of_nat_image";
+
+Goalw [SHNat_def] "inv hypnat_of_nat ``SHNat = (UNIV::nat set)";
+by (Auto_tac);
+by (rtac (inj_hypnat_of_nat RS inv_f_f RS subst) 1);
+by (Blast_tac 1);
+qed "inv_hypnat_of_nat_image";
+
+Goalw [SHNat_def] 
+      "!!P. [| EX x. x: P; P <= SHNat |] ==> \
+\           EX Q. P = hypnat_of_nat `` Q";
+by (Best_tac 1); 
+qed "SHNat_hypnat_of_nat_image";
+
+Goalw [SHNat_def] 
+      "SHNat = hypnat_of_nat `` (UNIV::nat set)";
+by (Auto_tac);
+qed "SHNat_hypnat_of_nat_iff";
+
+Goalw [SHNat_def] "SHNat <= (UNIV::hypnat set)";
+by (Auto_tac);
+qed "SHNat_subset_UNIV";
+
+Goal "{n. n <= Suc m} = {n. n <= m} Un {n. n = Suc m}";
+by (auto_tac (claset(),simpset() addsimps [le_Suc_eq]));
+qed "leSuc_Un_eq";
+
+Goal "finite {n::nat. n <= m}";
+by (nat_ind_tac "m" 1);
+by (auto_tac (claset(),simpset() addsimps [leSuc_Un_eq]));
+qed "finite_nat_le_segment";
+
+Goal "{n::nat. m < n} : FreeUltrafilterNat";
+by (cut_inst_tac [("m2","m")] (finite_nat_le_segment RS 
+    FreeUltrafilterNat_finite RS FreeUltrafilterNat_Compl_mem) 1);
+by (Fuf_tac 1);
+qed "lemma_unbounded_set";
+Addsimps [lemma_unbounded_set];
+
+Goalw [SHNat_def,hypnat_of_nat_def,
+           hypnat_less_def,hypnat_omega_def] 
+           "ALL n: SHNat. n < whn";
+by (Clarify_tac 1);
+by (auto_tac (claset() addSIs [exI],simpset()));
+qed "hypnat_omega_gt_SHNat";
+
+Goal "hypnat_of_nat n < whn";
+by (cut_facts_tac [hypnat_omega_gt_SHNat] 1);
+by (dres_inst_tac [("x","hypnat_of_nat n")] bspec 1);
+by (Auto_tac);
+qed "hypnat_of_nat_less_whn";
+Addsimps [hypnat_of_nat_less_whn];
+
+Goal "hypnat_of_nat n <= whn";
+by (rtac (hypnat_of_nat_less_whn RS hypnat_less_imp_le) 1);
+qed "hypnat_of_nat_le_whn";
+Addsimps [hypnat_of_nat_le_whn];
+
+Goal "0 < whn";
+by (rtac (hypnat_omega_gt_SHNat RS ballE) 1);
+by (Auto_tac);
+qed "hypnat_zero_less_hypnat_omega";
+Addsimps [hypnat_zero_less_hypnat_omega];
+
+Goal "1hn < whn";
+by (rtac (hypnat_omega_gt_SHNat RS ballE) 1);
+by (Auto_tac);
+qed "hypnat_one_less_hypnat_omega";
+Addsimps [hypnat_one_less_hypnat_omega];
+
+(*--------------------------------------------------------------------------
+     Theorems about infinite hypernatural numbers -- HNatInfinite
+ -------------------------------------------------------------------------*)
+Goalw [HNatInfinite_def,SHNat_def] "whn : HNatInfinite";
+by (Auto_tac);
+qed "HNatInfinite_whn";
+Addsimps [HNatInfinite_whn];
+
+Goalw [HNatInfinite_def] "!!x. x: SHNat ==> x ~: HNatInfinite";
+by (Simp_tac 1);
+qed "SHNat_not_HNatInfinite";
+
+Goalw [HNatInfinite_def] "!!x. x ~: HNatInfinite ==> x: SHNat";
+by (Asm_full_simp_tac 1);
+qed "not_HNatInfinite_SHNat";
+
+Goalw [HNatInfinite_def] "!!x. x ~: SHNat ==> x: HNatInfinite";
+by (Simp_tac 1);
+qed "not_SHNat_HNatInfinite";
+
+Goalw [HNatInfinite_def] "!!x. x: HNatInfinite ==> x ~: SHNat";
+by (Asm_full_simp_tac 1);
+qed "HNatInfinite_not_SHNat";
+
+Goal "(x: SHNat) = (x ~: HNatInfinite)";
+by (blast_tac (claset() addSIs [SHNat_not_HNatInfinite,
+    not_HNatInfinite_SHNat]) 1);
+qed "SHNat_not_HNatInfinite_iff";
+
+Goal "(x ~: SHNat) = (x: HNatInfinite)";
+by (blast_tac (claset() addSIs [not_SHNat_HNatInfinite,
+    HNatInfinite_not_SHNat]) 1);
+qed "not_SHNat_HNatInfinite_iff";
+
+Goal "x : SHNat | x : HNatInfinite";
+by (simp_tac (simpset() addsimps [SHNat_not_HNatInfinite_iff]) 1);
+qed "SHNat_HNatInfinite_disj";
+
+(*-------------------------------------------------------------------
+   Proof of alternative definition for set of Infinite hypernatural 
+   numbers --- HNatInfinite = {N. ALL n: SHNat. n < N}
+ -------------------------------------------------------------------*)
+Goal "!!N (xa::nat=>nat). \
+\         (ALL N. {n. xa n ~= N} : FreeUltrafilterNat) ==> \
+\         ({n. N < xa n} : FreeUltrafilterNat)";
+by (nat_ind_tac "N" 1);
+by (dres_inst_tac [("x","0")] spec 1);
+by (rtac ccontr 1 THEN dtac FreeUltrafilterNat_Compl_mem 1
+    THEN dtac FreeUltrafilterNat_Int 1 THEN assume_tac 1);
+by (Asm_full_simp_tac 1);
+by (dres_inst_tac [("x","Suc N")] spec 1);
+by (fuf_tac (claset() addSDs [Suc_leI],simpset() addsimps 
+    [le_eq_less_or_eq]) 1);
+qed "HNatInfinite_FreeUltrafilterNat_lemma";
+
+(*** alternative definition ***)
+Goalw [HNatInfinite_def,SHNat_def,hypnat_of_nat_def] 
+      "HNatInfinite = {N. ALL n:SHNat. n < N}";
+by (Step_tac 1);
+by (dres_inst_tac [("x","Abs_hypnat \
+\        (hypnatrel ^^ {%n. N})")] bspec 2);
+by (res_inst_tac [("z","x")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","n")] eq_Abs_hypnat 1);
+by (auto_tac (claset(),simpset() addsimps [hypnat_less_iff]));
+by (auto_tac (claset() addSIs [exI] addEs 
+    [HNatInfinite_FreeUltrafilterNat_lemma],
+    simpset() addsimps [FreeUltrafilterNat_Compl_iff1, 
+     CLAIM "- {n. xa n = N} = {n. xa n ~= N}"]));
+qed "HNatInfinite_iff";
+
+(*--------------------------------------------------------------------
+   Alternative definition for HNatInfinite using Free ultrafilter
+ --------------------------------------------------------------------*)
+Goal "!!x. x : HNatInfinite ==> EX X: Rep_hypnat x. \
+\          ALL u. {n. u < X n}:  FreeUltrafilterNat";
+by (auto_tac (claset(),simpset() addsimps [hypnat_less_def,
+    HNatInfinite_iff,SHNat_iff,hypnat_of_nat_def]));
+by (res_inst_tac [("z","x")] eq_Abs_hypnat 1);
+by (EVERY[Auto_tac, rtac bexI 1, 
+    rtac lemma_hypnatrel_refl 2, Step_tac 1]);
+by (dres_inst_tac [("x","hypnat_of_nat u")] bspec 1);
+by (Simp_tac 1);
+by (auto_tac (claset(),
+    simpset() addsimps [hypnat_of_nat_def]));
+by (Fuf_tac 1);
+qed "HNatInfinite_FreeUltrafilterNat";
+
+Goal "!!x. EX X: Rep_hypnat x. \
+\          ALL u. {n. u < X n}:  FreeUltrafilterNat \
+\          ==> x: HNatInfinite";
+by (auto_tac (claset(),simpset() addsimps [hypnat_less_def,
+    HNatInfinite_iff,SHNat_iff,hypnat_of_nat_def]));
+by (rtac exI 1 THEN Auto_tac);
+qed "FreeUltrafilterNat_HNatInfinite";
+
+Goal "!!x. (x : HNatInfinite) = (EX X: Rep_hypnat x. \
+\          ALL u. {n. u < X n}:  FreeUltrafilterNat)";
+by (blast_tac (claset() addIs [HNatInfinite_FreeUltrafilterNat,
+    FreeUltrafilterNat_HNatInfinite]) 1);
+qed "HNatInfinite_FreeUltrafilterNat_iff";
+
+Goal "!!x. x : HNatInfinite ==> 1hn < x";
+by (auto_tac (claset(),simpset() addsimps [HNatInfinite_iff]));
+qed "HNatInfinite_gt_one";
+Addsimps [HNatInfinite_gt_one];
+
+Goal "!!x. 0 ~: HNatInfinite";
+by (auto_tac (claset(),simpset() 
+    addsimps [HNatInfinite_iff]));
+by (dres_inst_tac [("a","1hn")] equals0D 1);
+by (Asm_full_simp_tac 1);
+qed "zero_not_mem_HNatInfinite";
+Addsimps [zero_not_mem_HNatInfinite];
+
+Goal "!!x. x : HNatInfinite ==> x ~= 0";
+by (Auto_tac);
+qed "HNatInfinite_not_eq_zero";
+
+Goal "!!x. x : HNatInfinite ==> 1hn <= x";
+by (blast_tac (claset() addIs [hypnat_less_imp_le,
+         HNatInfinite_gt_one]) 1);
+qed "HNatInfinite_ge_one";
+Addsimps [HNatInfinite_ge_one];
+
+(*--------------------------------------------------
+                   Closure Rules
+ --------------------------------------------------*)
+Goal "!!x. [| x: HNatInfinite; y: HNatInfinite |] \
+\           ==> x + y: HNatInfinite";
+by (auto_tac (claset(),simpset() addsimps [HNatInfinite_iff]));
+by (dtac bspec 1 THEN assume_tac 1);
+by (dtac (SHNat_zero RSN (2,bspec)) 1);
+by (dtac hypnat_add_less_mono 1 THEN assume_tac 1);
+by (Asm_full_simp_tac 1);
+qed "HNatInfinite_add";
+
+Goal "!!x. [| x: HNatInfinite; y: SHNat |] \
+\                       ==> x + y: HNatInfinite";
+by (rtac ccontr 1 THEN dtac not_HNatInfinite_SHNat 1);
+by (dres_inst_tac [("x","x + y")] SHNat_minus 1);
+by (auto_tac (claset(),simpset() addsimps 
+    [SHNat_not_HNatInfinite_iff]));
+qed "HNatInfinite_SHNat_add";
+
+goal HyperNat.thy "!!x. [| x: HNatInfinite; y: SHNat |] \
+\                       ==> x - y: HNatInfinite";
+by (rtac ccontr 1 THEN dtac not_HNatInfinite_SHNat 1);
+by (dres_inst_tac [("x","x - y")] SHNat_add 1);
+by (subgoal_tac "y <= x" 2);
+by (auto_tac (claset() addSDs [hypnat_le_add_diff_inverse2],
+    simpset() addsimps [not_SHNat_HNatInfinite_iff RS sym]));
+by (auto_tac (claset() addSIs [hypnat_less_imp_le],
+    simpset() addsimps [not_SHNat_HNatInfinite_iff,HNatInfinite_iff]));
+qed "HNatInfinite_SHNat_diff";
+
+Goal 
+     "!!x. x: HNatInfinite ==> x + 1hn: HNatInfinite";
+by (auto_tac (claset() addIs [HNatInfinite_SHNat_add],
+              simpset()));
+qed "HNatInfinite_add_one";
+
+Goal 
+     "!!x. x: HNatInfinite ==> x - 1hn: HNatInfinite";
+by (rtac ccontr 1 THEN dtac not_HNatInfinite_SHNat 1);
+by (dres_inst_tac [("x","x - 1hn"),("y","1hn")] SHNat_add 1);
+by (auto_tac (claset(),simpset() addsimps 
+    [not_SHNat_HNatInfinite_iff RS sym]));
+qed "HNatInfinite_minus_one";
+
+Goal "!!x. x : HNatInfinite ==> EX y. x = y + 1hn";
+by (res_inst_tac [("x","x - 1hn")] exI 1);
+by (Auto_tac);
+qed "HNatInfinite_is_Suc";
+
+(*---------------------------------------------------------------
+       HNat : the hypernaturals embedded in the hyperreals
+       Obtained using the NS extension of the naturals
+ --------------------------------------------------------------*)
+ 
+Goalw [HNat_def,starset_def,
+         hypreal_of_posnat_def,hypreal_of_real_def] 
+         "hypreal_of_posnat N : HNat";
+by (Auto_tac);
+by (Ultra_tac 1);
+by (res_inst_tac [("x","Suc N")] exI 1);
+by (dtac sym 1 THEN auto_tac (claset(),simpset() 
+    addsimps [real_of_preal_real_of_nat_Suc]));
+qed "HNat_hypreal_of_posnat";
+Addsimps [HNat_hypreal_of_posnat];
+
+Goalw [HNat_def,starset_def]
+     "[| x: HNat; y: HNat |] ==> x + y: HNat";
+by (res_inst_tac [("z","x")] eq_Abs_hypreal 1);
+by (res_inst_tac [("z","y")] eq_Abs_hypreal 1);
+by (auto_tac (claset() addSDs [bspec] addIs [lemma_hyprel_refl],
+    simpset() addsimps [hypreal_add]));
+by (Ultra_tac 1);
+by (dres_inst_tac [("t","Y xb")] sym 1);
+by (auto_tac (claset(),simpset() addsimps [real_of_nat_add RS sym]));
+qed "HNat_add";
+
+Goalw [HNat_def,starset_def]
+     "[| x: HNat; y: HNat |] ==> x * y: HNat";
+by (res_inst_tac [("z","x")] eq_Abs_hypreal 1);
+by (res_inst_tac [("z","y")] eq_Abs_hypreal 1);
+by (auto_tac (claset() addSDs [bspec] addIs [lemma_hyprel_refl],
+    simpset() addsimps [hypreal_mult]));
+by (Ultra_tac 1);
+by (dres_inst_tac [("t","Y xb")] sym 1);
+by (auto_tac (claset(),simpset() addsimps [real_of_nat_mult RS sym]));
+qed "HNat_mult";
+
+(*---------------------------------------------------------------
+    Embedding of the hypernaturals into the hyperreal
+ --------------------------------------------------------------*)
+(*** A lemma that should have been derived a long time ago! ***)
+Goal "(Ya : hyprel ^^{%n. f(n)}) = \
+\         ({n. f n = Ya n} : FreeUltrafilterNat)";
+by (Auto_tac);
+qed "lemma_hyprel_FUFN";
+
+Goalw [hypreal_of_hypnat_def]
+      "hypreal_of_hypnat (Abs_hypnat(hypnatrel^^{%n. X n})) = \
+\         Abs_hypreal(hyprel ^^ {%n. real_of_nat (X n)})";
+by (res_inst_tac [("f","Abs_hypreal")] arg_cong 1);
+by (auto_tac (claset() addEs [FreeUltrafilterNat_Int RS
+    FreeUltrafilterNat_subset],simpset() addsimps 
+    [lemma_hyprel_FUFN]));
+qed "hypreal_of_hypnat";
+
+Goal "inj(hypreal_of_hypnat)";
+by (rtac injI 1);
+by (res_inst_tac [("z","x")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","y")] eq_Abs_hypnat 1);
+by (auto_tac (claset(),simpset() addsimps 
+    [hypreal_of_hypnat,real_of_nat_eq_cancel]));
+qed "inj_hypreal_of_hypnat";
+
+Goal "(hypreal_of_hypnat n = hypreal_of_hypnat m) = (n = m)";
+by (auto_tac (claset(),simpset() addsimps [inj_hypreal_of_hypnat RS injD]));
+qed "hypreal_of_hypnat_eq_cancel";
+Addsimps [hypreal_of_hypnat_eq_cancel];
+
+Goal "(hypnat_of_nat n = hypnat_of_nat m) = (n = m)";
+by (auto_tac (claset() addDs [inj_hypnat_of_nat RS injD],
+              simpset()));
+qed "hypnat_of_nat_eq_cancel";
+Addsimps [hypnat_of_nat_eq_cancel];
+
+Goalw [hypnat_zero_def] 
+     "hypreal_of_hypnat 0 = #0";
+by (simp_tac (HOL_ss addsimps
+             [zero_eq_numeral_0 RS sym, hypreal_zero_def]) 1); 
+by (simp_tac (simpset() addsimps [hypreal_of_hypnat, real_of_nat_zero]) 1);
+qed "hypreal_of_hypnat_zero";
+
+Goalw [hypnat_one_def] 
+     "hypreal_of_hypnat 1hn = #1";
+by (simp_tac (HOL_ss addsimps
+             [one_eq_numeral_1 RS sym, hypreal_one_def]) 1); 
+by (simp_tac (simpset() addsimps [hypreal_of_hypnat, real_of_nat_one]) 1);
+qed "hypreal_of_hypnat_one";
+
+Goal "hypreal_of_hypnat n1 + hypreal_of_hypnat n2 = hypreal_of_hypnat (n1 + n2)";
+by (res_inst_tac [("z","n1")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","n2")] eq_Abs_hypnat 1);
+by (asm_simp_tac (simpset() addsimps [hypreal_of_hypnat,
+        hypreal_add,hypnat_add,real_of_nat_add]) 1);
+qed "hypreal_of_hypnat_add";
+
+Goal "hypreal_of_hypnat n1 * hypreal_of_hypnat n2 = hypreal_of_hypnat (n1 * n2)";
+by (res_inst_tac [("z","n1")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","n2")] eq_Abs_hypnat 1);
+by (asm_simp_tac (simpset() addsimps [hypreal_of_hypnat,
+        hypreal_mult,hypnat_mult,real_of_nat_mult]) 1);
+qed "hypreal_of_hypnat_mult";
+
+Goal "(hypreal_of_hypnat n < hypreal_of_hypnat m) = (n < m)";
+by (res_inst_tac [("z","n")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","m")] eq_Abs_hypnat 1);
+by (asm_simp_tac (simpset() addsimps 
+    [hypreal_of_hypnat,hypreal_less,hypnat_less]) 1);
+qed "hypreal_of_hypnat_less_iff";
+Addsimps [hypreal_of_hypnat_less_iff];
+
+Goal "(hypreal_of_hypnat N = #0) = (N = 0)";
+by (simp_tac (simpset() addsimps [hypreal_of_hypnat_zero RS sym]) 1);
+qed "hypreal_of_hypnat_eq_zero_iff";
+Addsimps [hypreal_of_hypnat_eq_zero_iff];
+
+Goal "ALL n. N <= n ==> N = (0::hypnat)";
+by (dres_inst_tac [("x","0")] spec 1);
+by (res_inst_tac [("z","N")] eq_Abs_hypnat 1);
+by (auto_tac (claset(),simpset() addsimps [hypnat_le,hypnat_zero_def]));
+qed "hypnat_eq_zero";
+Addsimps [hypnat_eq_zero];
+
+Goal "~ (ALL n. n = (0::hypnat))";
+by Auto_tac;
+by (res_inst_tac [("x","1hn")] exI 1);
+by (Simp_tac 1);
+qed "hypnat_not_all_eq_zero";
+Addsimps [hypnat_not_all_eq_zero];
+
+Goal "n ~= 0 ==> (n <= 1hn) = (n = 1hn)";
+by (auto_tac (claset(),simpset() addsimps [hypnat_le_eq_less_or_eq]));
+qed "hypnat_le_one_eq_one";
+Addsimps [hypnat_le_one_eq_one];
+
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Hyperreal/HyperNat.thy	Sat Dec 30 22:03:47 2000 +0100
@@ -0,0 +1,84 @@
+(*  Title       : HyperNat.thy
+    Author      : Jacques D. Fleuriot
+    Copyright   : 1998  University of Cambridge
+    Description : Explicit construction of hypernaturals using 
+                  ultrafilters
+*) 
+
+HyperNat = Star + 
+
+constdefs
+    hypnatrel :: "((nat=>nat)*(nat=>nat)) set"
+    "hypnatrel == {p. EX X Y. p = ((X::nat=>nat),Y) & 
+                       {n::nat. X(n) = Y(n)} : FreeUltrafilterNat}"
+
+typedef hypnat = "UNIV//hypnatrel"              (Equiv.quotient_def)
+
+instance
+   hypnat  :: {ord,zero,plus,times,minus}
+
+consts
+  "1hn"       :: hypnat               ("1hn")  
+  "whn"       :: hypnat               ("whn")  
+
+defs
+
+  hypnat_zero_def      "0 == Abs_hypnat(hypnatrel^^{%n::nat. 0})"
+  hypnat_one_def       "1hn == Abs_hypnat(hypnatrel^^{%n::nat. 1})"
+
+  (* omega is in fact an infinite hypernatural number = [<1,2,3,...>] *)
+  hypnat_omega_def     "whn == Abs_hypnat(hypnatrel^^{%n::nat. n})"
+ 
+
+constdefs
+
+  (* embedding the naturals in the hypernaturals *)
+  hypnat_of_nat   :: nat => hypnat
+  "hypnat_of_nat m  == Abs_hypnat(hypnatrel^^{%n::nat. m})"
+
+  (* set of naturals embedded in the hypernaturals*)
+  SHNat         :: "hypnat set"
+  "SHNat        == {n. EX N. n = hypnat_of_nat N}"  
+ 
+  (* embedding the naturals in the hyperreals*)
+  SNat         :: "hypreal set"
+  "SNat        == {n. EX N. n = hypreal_of_nat N}"  
+
+  (* hypernaturals as members of the hyperreals; the set is defined as  *)
+  (* the nonstandard extension of set of naturals embedded in the reals *)
+  HNat         :: "hypreal set"
+  "HNat         == *s* {n. EX no. n = real_of_nat no}"
+
+  (* the set of infinite hypernatural numbers *)
+  HNatInfinite :: "hypnat set"
+  "HNatInfinite == {n. n ~: SHNat}"
+
+  (* explicit embedding of the hypernaturals in the hyperreals *)    
+  hypreal_of_hypnat :: hypnat => hypreal
+  "hypreal_of_hypnat N  == Abs_hypreal(UN X: Rep_hypnat(N). 
+                            hyprel^^{%n::nat. real_of_nat (X n)})"
+  
+defs
+  hypnat_add_def  
+  "P + Q == Abs_hypnat(UN X:Rep_hypnat(P). UN Y:Rep_hypnat(Q).
+                hypnatrel^^{%n::nat. X n + Y n})"
+
+  hypnat_mult_def  
+  "P * Q == Abs_hypnat(UN X:Rep_hypnat(P). UN Y:Rep_hypnat(Q).
+                hypnatrel^^{%n::nat. X n * Y n})"
+
+  hypnat_minus_def  
+  "P - Q == Abs_hypnat(UN X:Rep_hypnat(P). UN Y:Rep_hypnat(Q).
+                hypnatrel^^{%n::nat. X n - Y n})"
+
+  hypnat_less_def
+  "P < (Q::hypnat) == EX X Y. X: Rep_hypnat(P) & 
+                               Y: Rep_hypnat(Q) & 
+                               {n::nat. X n < Y n} : FreeUltrafilterNat"
+  hypnat_le_def
+  "P <= (Q::hypnat) == ~(Q < P)" 
+
+end
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Hyperreal/HyperOrd.ML	Sat Dec 30 22:03:47 2000 +0100
@@ -0,0 +1,715 @@
+(*  Title:	 Real/Hyperreal/HyperOrd.ML
+    Author:      Jacques D. Fleuriot
+    Copyright:   1998 University of Cambridge
+                 2000 University of Edinburgh
+    Description: Type "hypreal" is a linear order and also 
+                 satisfies plus_ac0: + is an AC-operator with zero
+*)
+
+(**** The simproc abel_cancel ****)
+
+(*** Two lemmas needed for the simprocs ***)
+
+(*Deletion of other terms in the formula, seeking the -x at the front of z*)
+Goal "((x::hypreal) + (y + z) = y + u) = ((x + z) = u)";
+by (stac hypreal_add_left_commute 1);
+by (rtac hypreal_add_left_cancel 1);
+qed "hypreal_add_cancel_21";
+
+(*A further rule to deal with the case that
+  everything gets cancelled on the right.*)
+Goal "((x::hypreal) + (y + z) = y) = (x = -z)";
+by (stac hypreal_add_left_commute 1);
+by (res_inst_tac [("t", "y")] (hypreal_add_zero_right RS subst) 1
+    THEN stac hypreal_add_left_cancel 1);
+by (simp_tac (simpset() addsimps [hypreal_eq_diff_eq RS sym]) 1);
+qed "hypreal_add_cancel_end";
+
+
+structure Hyperreal_Cancel_Data =
+struct
+  val ss		= HOL_ss
+  val eq_reflection	= eq_reflection
+
+  val sg_ref		= Sign.self_ref (Theory.sign_of (the_context ()))
+  val T			= Type("HyperDef.hypreal",[])
+  val zero		= Const ("0", T)
+  val restrict_to_left  = restrict_to_left
+  val add_cancel_21	= hypreal_add_cancel_21
+  val add_cancel_end	= hypreal_add_cancel_end
+  val add_left_cancel	= hypreal_add_left_cancel
+  val add_assoc		= hypreal_add_assoc
+  val add_commute	= hypreal_add_commute
+  val add_left_commute	= hypreal_add_left_commute
+  val add_0		= hypreal_add_zero_left
+  val add_0_right	= hypreal_add_zero_right
+
+  val eq_diff_eq	= hypreal_eq_diff_eq
+  val eqI_rules		= [hypreal_less_eqI, hypreal_eq_eqI, hypreal_le_eqI]
+  fun dest_eqI th = 
+      #1 (HOLogic.dest_bin "op =" HOLogic.boolT 
+	      (HOLogic.dest_Trueprop (concl_of th)))
+
+  val diff_def		= hypreal_diff_def
+  val minus_add_distrib	= hypreal_minus_add_distrib
+  val minus_minus	= hypreal_minus_minus
+  val minus_0		= hypreal_minus_zero
+  val add_inverses	= [hypreal_add_minus, hypreal_add_minus_left]
+  val cancel_simps	= [hypreal_add_minus_cancelA, hypreal_minus_add_cancelA]
+end;
+
+structure Hyperreal_Cancel = Abel_Cancel (Hyperreal_Cancel_Data);
+
+Addsimprocs [Hyperreal_Cancel.sum_conv, Hyperreal_Cancel.rel_conv];
+
+Goal "- (z - y) = y - (z::hypreal)";
+by (Simp_tac 1);
+qed "hypreal_minus_diff_eq";
+Addsimps [hypreal_minus_diff_eq];
+
+
+Goal "((x::hypreal) < y) = (-y < -x)";
+by (stac hypreal_less_minus_iff 1);
+by (res_inst_tac [("x1","x")] (hypreal_less_minus_iff RS ssubst) 1);
+by (simp_tac (simpset() addsimps [hypreal_add_commute]) 1);
+qed "hypreal_less_swap_iff";
+
+Goalw [hypreal_zero_def] 
+      "((0::hypreal) < x) = (-x < x)";
+by (res_inst_tac [("z","x")] eq_Abs_hypreal 1);
+by (auto_tac (claset(), simpset() addsimps [hypreal_less, hypreal_minus]));
+by (ALLGOALS(Ultra_tac));
+qed "hypreal_gt_zero_iff";
+
+Goal "(A::hypreal) < B ==> A + C < B + C";
+by (res_inst_tac [("z","A")] eq_Abs_hypreal 1);
+by (res_inst_tac [("z","B")] eq_Abs_hypreal 1);
+by (res_inst_tac [("z","C")] eq_Abs_hypreal 1);
+by (auto_tac (claset() addSIs [exI], 
+              simpset() addsimps [hypreal_less_def,hypreal_add]));
+by (Ultra_tac 1);
+qed "hypreal_add_less_mono1";
+
+Goal "!!(A::hypreal). A < B ==> C + A < C + B";
+by (auto_tac (claset() addIs [hypreal_add_less_mono1],
+    simpset() addsimps [hypreal_add_commute]));
+qed "hypreal_add_less_mono2";
+
+Goal "(x < (0::hypreal)) = (x < -x)";
+by (rtac (hypreal_minus_zero_less_iff RS subst) 1);
+by (stac hypreal_gt_zero_iff 1);
+by (Full_simp_tac 1);
+qed "hypreal_lt_zero_iff";
+
+Goalw [hypreal_le_def] "((0::hypreal) <= x) = (-x <= x)";
+by (auto_tac (claset(),simpset() addsimps [hypreal_lt_zero_iff RS sym]));
+qed "hypreal_ge_zero_iff";
+
+Goalw [hypreal_le_def] "(x <= (0::hypreal)) = (x <= -x)";
+by (auto_tac (claset(),simpset() addsimps [hypreal_gt_zero_iff RS sym]));
+qed "hypreal_le_zero_iff";
+
+Goalw [hypreal_zero_def] 
+      "[| 0 < x; 0 < y |] ==> (0::hypreal) < x + y";
+by (res_inst_tac [("z","x")] eq_Abs_hypreal 1);
+by (res_inst_tac [("z","y")] eq_Abs_hypreal 1);
+by (auto_tac (claset(),simpset() addsimps
+    [hypreal_less_def,hypreal_add]));
+by (auto_tac (claset() addSIs [exI],simpset() addsimps
+    [hypreal_less_def,hypreal_add]));
+by (ultra_tac (claset() addIs [real_add_order],simpset()) 1);
+qed "hypreal_add_order";
+
+Goal "[| 0 < x; 0 <= y |] ==> (0::hypreal) < x + y";
+by (auto_tac (claset() addDs [sym, order_le_imp_less_or_eq]
+                       addIs [hypreal_add_order],
+              simpset()));
+qed "hypreal_add_order_le";            
+
+Goalw [hypreal_zero_def] 
+          "[| 0 < x; 0 < y |] ==> (0::hypreal) < x * y";
+by (res_inst_tac [("z","x")] eq_Abs_hypreal 1);
+by (res_inst_tac [("z","y")] eq_Abs_hypreal 1);
+by (auto_tac (claset() addSIs [exI],simpset() addsimps
+    [hypreal_less_def,hypreal_mult]));
+by (ultra_tac (claset() addIs [rename_numerals real_mult_order],
+	       simpset()) 1);
+qed "hypreal_mult_order";
+
+Goal "[| x < 0; y < 0 |] ==> (0::hypreal) < x * y";
+by (REPEAT(dtac (hypreal_minus_zero_less_iff RS iffD2) 1));
+by (dtac hypreal_mult_order 1 THEN assume_tac 1);
+by (Asm_full_simp_tac 1);
+qed "hypreal_mult_less_zero1";
+
+Goal "[| 0 <= x; 0 <= y |] ==> (0::hypreal) <= x * y";
+by (REPEAT(dtac order_le_imp_less_or_eq 1));
+by (auto_tac (claset() addIs [hypreal_mult_order, order_less_imp_le],
+              simpset()));
+qed "hypreal_le_mult_order";
+
+
+Goal "[| x <= 0; y <= 0 |] ==> (0::hypreal) <= x * y";
+by (rtac hypreal_less_or_eq_imp_le 1);
+by (dtac order_le_imp_less_or_eq 1 THEN etac disjE 1);
+by Auto_tac;
+by (dtac order_le_imp_less_or_eq 1);
+by (auto_tac (claset() addDs [hypreal_mult_less_zero1],simpset()));
+qed "hypreal_mult_le_zero1";
+
+Goal "[| 0 <= x; y < 0 |] ==> x * y <= (0::hypreal)";
+by (rtac hypreal_less_or_eq_imp_le 1);
+by (dtac order_le_imp_less_or_eq 1 THEN etac disjE 1);
+by Auto_tac;
+by (dtac (hypreal_minus_zero_less_iff RS iffD2) 1);
+by (rtac (hypreal_minus_zero_less_iff RS subst) 1);
+by (blast_tac (claset() addDs [hypreal_mult_order] 
+    addIs [hypreal_minus_mult_eq2 RS ssubst]) 1);
+qed "hypreal_mult_le_zero";
+
+Goal "[| 0 < x; y < 0 |] ==> x*y < (0::hypreal)";
+by (dtac (hypreal_minus_zero_less_iff RS iffD2) 1);
+by (dtac hypreal_mult_order 1 THEN assume_tac 1);
+by (rtac (hypreal_minus_zero_less_iff RS iffD1) 1);
+by (Asm_full_simp_tac 1);
+qed "hypreal_mult_less_zero";
+
+Goalw [hypreal_one_def,hypreal_zero_def,hypreal_less_def] "0 < 1hr";
+by (res_inst_tac [("x","%n. #0")] exI 1);
+by (res_inst_tac [("x","%n. #1")] exI 1);
+by (auto_tac (claset(),
+        simpset() addsimps [real_zero_less_one, FreeUltrafilterNat_Nat_set]));
+qed "hypreal_zero_less_one";
+
+Goal "1hr < 1hr + 1hr";
+by (rtac (hypreal_less_minus_iff RS iffD2) 1);
+by (full_simp_tac (simpset() addsimps [hypreal_zero_less_one,
+    hypreal_add_assoc]) 1);
+qed "hypreal_one_less_two";
+
+Goal "0 < 1hr + 1hr";
+by (rtac ([hypreal_zero_less_one,
+          hypreal_one_less_two] MRS order_less_trans) 1);
+qed "hypreal_zero_less_two";
+
+Goal "1hr + 1hr ~= 0";
+by (rtac (hypreal_zero_less_two RS hypreal_not_refl2 RS not_sym) 1);
+qed "hypreal_two_not_zero";
+Addsimps [hypreal_two_not_zero];
+
+Goal "[| 0 <= x; 0 <= y |] ==> (0::hypreal) <= x + y";
+by (REPEAT(dtac order_le_imp_less_or_eq 1));
+by (auto_tac (claset() addIs [hypreal_add_order, order_less_imp_le],
+              simpset()));
+qed "hypreal_le_add_order";
+
+(*** Monotonicity results ***)
+
+Goal "(v+z < w+z) = (v < (w::hypreal))";
+by (Simp_tac 1);
+qed "hypreal_add_right_cancel_less";
+
+Goal "(z+v < z+w) = (v < (w::hypreal))";
+by (Simp_tac 1);
+qed "hypreal_add_left_cancel_less";
+
+Addsimps [hypreal_add_right_cancel_less, 
+          hypreal_add_left_cancel_less];
+
+Goal "(v+z <= w+z) = (v <= (w::hypreal))";
+by (Simp_tac 1);
+qed "hypreal_add_right_cancel_le";
+
+Goal "(z+v <= z+w) = (v <= (w::hypreal))";
+by (Simp_tac 1);
+qed "hypreal_add_left_cancel_le";
+
+Addsimps [hypreal_add_right_cancel_le, hypreal_add_left_cancel_le];
+
+Goal  "[| (z1::hypreal) < y1; z2 < y2 |] ==> z1 + z2 < y1 + y2";
+by (dtac (hypreal_less_minus_iff RS iffD1) 1);
+by (dtac (hypreal_less_minus_iff RS iffD1) 1);
+by (dtac hypreal_add_order 1 THEN assume_tac 1);
+by (thin_tac "0 < y2 + - z2" 1);
+by (dres_inst_tac [("C","z1 + z2")] hypreal_add_less_mono1 1);
+by (auto_tac (claset(),simpset() addsimps 
+    [hypreal_minus_add_distrib RS sym] @ hypreal_add_ac
+    delsimps [hypreal_minus_add_distrib]));
+qed "hypreal_add_less_mono";
+
+Goal "(q1::hypreal) <= q2  ==> x + q1 <= x + q2";
+by (dtac order_le_imp_less_or_eq 1);
+by (Step_tac 1);
+by (auto_tac (claset() addSIs [order_less_imp_le,hypreal_add_less_mono1],
+              simpset() addsimps [hypreal_add_commute]));
+qed "hypreal_add_left_le_mono1";
+
+Goal "(q1::hypreal) <= q2  ==> q1 + x <= q2 + x";
+by (auto_tac (claset() addDs [hypreal_add_left_le_mono1],
+              simpset() addsimps [hypreal_add_commute]));
+qed "hypreal_add_le_mono1";
+
+Goal "[|(i::hypreal)<=j;  k<=l |] ==> i + k <= j + l";
+by (etac (hypreal_add_le_mono1 RS order_trans) 1);
+by (Simp_tac 1);
+qed "hypreal_add_le_mono";
+
+Goal "[|(i::hypreal)<j;  k<=l |] ==> i + k < j + l";
+by (auto_tac (claset() addSDs [order_le_imp_less_or_eq] 
+                       addIs [hypreal_add_less_mono1,hypreal_add_less_mono],
+    simpset()));
+qed "hypreal_add_less_le_mono";
+
+Goal "[|(i::hypreal)<=j;  k<l |] ==> i + k < j + l";
+by (auto_tac (claset() addSDs [order_le_imp_less_or_eq] 
+                       addIs [hypreal_add_less_mono2,hypreal_add_less_mono],
+              simpset()));
+qed "hypreal_add_le_less_mono";
+
+Goal "(A::hypreal) + C < B + C ==> A < B";
+by (Full_simp_tac 1);
+qed "hypreal_less_add_right_cancel";
+
+Goal "(C::hypreal) + A < C + B ==> A < B";
+by (Full_simp_tac 1);
+qed "hypreal_less_add_left_cancel";
+
+Goal "[|r < x; (0::hypreal) <= y|] ==> r < x + y";
+by (auto_tac (claset() addDs [hypreal_add_less_le_mono],
+    simpset()));
+qed "hypreal_add_zero_less_le_mono";
+
+Goal "!!(A::hypreal). A + C <= B + C ==> A <= B";
+by (dres_inst_tac [("x","-C")] hypreal_add_le_mono1 1);
+by (asm_full_simp_tac (simpset() addsimps [hypreal_add_assoc]) 1);
+qed "hypreal_le_add_right_cancel";
+
+Goal "!!(A::hypreal). C + A <= C + B ==> A <= B";
+by (dres_inst_tac [("x","-C")] hypreal_add_left_le_mono1 1);
+by (asm_full_simp_tac (simpset() addsimps [hypreal_add_assoc RS sym]) 1);
+qed "hypreal_le_add_left_cancel";
+
+Goal "(0::hypreal) <= x*x";
+by (res_inst_tac [("x","0"),("y","x")] hypreal_linear_less2 1);
+by (auto_tac (claset() addIs [hypreal_mult_order,
+                              hypreal_mult_less_zero1,order_less_imp_le],
+              simpset()));
+qed "hypreal_le_square";
+Addsimps [hypreal_le_square];
+
+Goalw [hypreal_le_def] "- (x*x) <= (0::hypreal)";
+by (auto_tac (claset() addSDs [hypreal_le_square RS order_le_less_trans],
+      simpset() addsimps [hypreal_minus_zero_less_iff]));
+qed "hypreal_less_minus_square";
+Addsimps [hypreal_less_minus_square];
+
+Goal "(0*x<r)=((0::hypreal)<r)";
+by (Simp_tac 1);
+qed "hypreal_mult_0_less";
+
+Goal "[| (0::hypreal) < z; x < y |] ==> x*z < y*z";       
+by (rotate_tac 1 1);
+by (dtac (hypreal_less_minus_iff RS iffD1) 1);
+by (rtac (hypreal_less_minus_iff RS iffD2) 1);
+by (dtac hypreal_mult_order 1 THEN assume_tac 1);
+by (asm_full_simp_tac (simpset() addsimps [hypreal_add_mult_distrib2,
+					   hypreal_mult_commute ]) 1);
+qed "hypreal_mult_less_mono1";
+
+Goal "[| (0::hypreal)<z; x<y |] ==> z*x<z*y";       
+by (asm_simp_tac (simpset() addsimps [hypreal_mult_commute,hypreal_mult_less_mono1]) 1);
+qed "hypreal_mult_less_mono2";
+
+Goal "[| (0::hypreal)<=z; x<y |] ==> x*z<=y*z";
+by (EVERY1 [rtac hypreal_less_or_eq_imp_le, dtac order_le_imp_less_or_eq]);
+by (auto_tac (claset() addIs [hypreal_mult_less_mono1],simpset()));
+qed "hypreal_mult_le_less_mono1";
+
+Goal "[| (0::hypreal)<=z; x<y |] ==> z*x<=z*y";
+by (asm_simp_tac (simpset() addsimps [hypreal_mult_commute,
+				      hypreal_mult_le_less_mono1]) 1);
+qed "hypreal_mult_le_less_mono2";
+
+Goal "[| (0::hypreal)<=z; x<=y |] ==> z*x<=z*y";
+by (dres_inst_tac [("x","x")] order_le_imp_less_or_eq 1);
+by (auto_tac (claset() addIs [hypreal_mult_le_less_mono2], simpset()));
+qed "hypreal_mult_le_le_mono1";
+
+val prem1::prem2::prem3::rest = goal thy
+     "[| (0::hypreal)<y; x<r; y*r<t*s |] ==> y*x<t*s";
+by (rtac ([[prem1,prem2] MRS hypreal_mult_less_mono2, prem3] 
+          MRS order_less_trans) 1);
+qed "hypreal_mult_less_trans";
+
+Goal "[| 0<=y; x<r; y*r<t*s; (0::hypreal)<t*s|] ==> y*x<t*s";
+by (dtac order_le_imp_less_or_eq 1);
+by (fast_tac (HOL_cs addEs [hypreal_mult_0_less RS iffD2,
+                            hypreal_mult_less_trans]) 1);
+qed "hypreal_mult_le_less_trans";
+
+Goal "[| 0 <= y; x <= r; y*r < t*s; (0::hypreal) < t*s|] ==> y*x < t*s";
+by (dres_inst_tac [("x","x")] order_le_imp_less_or_eq 1);
+by (fast_tac (claset() addIs [hypreal_mult_le_less_trans]) 1);
+qed "hypreal_mult_le_le_trans";
+
+Goal "[| u<v;  x<y;  (0::hypreal) < v;  0 < x |] ==> u*x < v* y";
+by (etac (hypreal_mult_less_mono1 RS order_less_trans) 1);
+by (assume_tac 1);
+by (etac hypreal_mult_less_mono2 1);
+by (assume_tac 1);
+qed "hypreal_mult_less_mono";
+
+(*UNUSED at present but possibly more useful than hypreal_mult_less_mono*)
+Goal "[| x < y;  r1 < r2;  (0::hypreal) <= r1;  0 <= x|] ==> r1 * x < r2 * y";
+by (subgoal_tac "0<r2" 1);
+by (blast_tac (claset() addIs [order_le_less_trans]) 2); 
+by (case_tac "x=0" 1);
+by (auto_tac (claset() addSDs [order_le_imp_less_or_eq] 
+	               addIs [hypreal_mult_less_mono, hypreal_mult_order],
+	      simpset()));
+qed "hypreal_mult_less_mono'";
+
+Goal "0 < x ==> 0 < inverse (x::hypreal)";
+by (EVERY1[rtac ccontr, dtac hypreal_leI]);
+by (forward_tac [hypreal_minus_zero_less_iff2 RS iffD2] 1);
+by (forward_tac [hypreal_not_refl2 RS not_sym] 1);
+by (dtac (hypreal_not_refl2 RS not_sym RS hypreal_inverse_not_zero) 1);
+by (EVERY1[dtac order_le_imp_less_or_eq, Step_tac]); 
+by (dtac hypreal_mult_less_zero1 1 THEN assume_tac 1);
+by (auto_tac (claset() addIs [hypreal_zero_less_one RS hypreal_less_asym],
+                 simpset() addsimps [hypreal_minus_zero_less_iff]));
+qed "hypreal_inverse_gt_zero";
+
+Goal "x < 0 ==> inverse (x::hypreal) < 0";
+by (ftac hypreal_not_refl2 1);
+by (dtac (hypreal_minus_zero_less_iff RS iffD2) 1);
+by (rtac (hypreal_minus_zero_less_iff RS iffD1) 1);
+by (stac (hypreal_minus_inverse RS sym) 1);
+by (auto_tac (claset() addIs [hypreal_inverse_gt_zero],  simpset()));
+qed "hypreal_inverse_less_zero";
+
+Goal "[| 0 <= x; 0 <= y |] ==> (x+y = 0) = (x = 0 & y = (0::hypreal))";
+by (auto_tac (claset() addIs [order_antisym], simpset()));
+qed "hypreal_add_nonneg_eq_0_iff";
+
+Goal "(x*y = 0) = (x = 0 | y = (0::hypreal))";
+by Auto_tac;
+by (blast_tac (claset() addDs [hypreal_mult_zero_disj]) 1);
+qed "hypreal_mult_is_0";
+
+Goal "(x*x + y*y = 0) = (x = 0 & y = (0::hypreal))";
+by (simp_tac (HOL_ss addsimps [hypreal_le_square, hypreal_add_nonneg_eq_0_iff, 
+                               hypreal_mult_is_0]) 1);
+qed "hypreal_squares_add_zero_iff";
+Addsimps [hypreal_squares_add_zero_iff];
+
+Goal "(x*x + y*y + z*z = 0) = (x = 0 & y = 0 & z = (0::hypreal))";
+by (simp_tac (HOL_ss addsimps [hypreal_le_square, hypreal_le_add_order, 
+                         hypreal_add_nonneg_eq_0_iff, hypreal_mult_is_0]) 1);
+qed "hypreal_three_squares_add_zero_iff";
+
+Goal "(x::hypreal)*x <= x*x + y*y";
+by (res_inst_tac [("z","x")] eq_Abs_hypreal 1);
+by (res_inst_tac [("z","y")] eq_Abs_hypreal 1);
+by (auto_tac (claset(),
+              simpset() addsimps [hypreal_mult,hypreal_add,hypreal_le]));
+qed "hypreal_self_le_add_pos";
+Addsimps [hypreal_self_le_add_pos];
+
+(*lcp: new lemma unfortunately needed...*)
+Goal "-(x*x) <= (y*y::real)";
+by (rtac order_trans 1);
+by (rtac real_le_square 2);
+by Auto_tac;
+qed "minus_square_le_square";
+
+Goal "(x::hypreal)*x <= x*x + y*y + z*z";
+by (res_inst_tac [("z","x")] eq_Abs_hypreal 1);
+by (res_inst_tac [("z","y")] eq_Abs_hypreal 1);
+by (res_inst_tac [("z","z")] eq_Abs_hypreal 1);
+by (auto_tac (claset(),
+	      simpset() addsimps [hypreal_mult, hypreal_add, hypreal_le,
+				  minus_square_le_square]));
+qed "hypreal_self_le_add_pos2";
+Addsimps [hypreal_self_le_add_pos2];
+
+
+(*----------------------------------------------------------------------------
+             Embedding of the naturals in the hyperreals
+ ----------------------------------------------------------------------------*)
+Goalw [hypreal_of_posnat_def] "hypreal_of_posnat 0 = 1hr";
+by (full_simp_tac (simpset() addsimps 
+    [pnat_one_iff RS sym,real_of_preal_def]) 1);
+by (fold_tac [real_one_def]);
+by (simp_tac (simpset() addsimps [hypreal_of_real_one]) 1);
+qed "hypreal_of_posnat_one";
+
+Goalw [hypreal_of_posnat_def] "hypreal_of_posnat 1 = 1hr + 1hr";
+by (full_simp_tac (simpset() addsimps 
+		   [real_of_preal_def,
+		    rename_numerals (real_one_def RS meta_eq_to_obj_eq),
+		    hypreal_add,hypreal_of_real_def,pnat_two_eq,
+		    hypreal_one_def, real_add,
+		    prat_of_pnat_add RS sym, preal_of_prat_add RS sym] @ 
+		    pnat_add_ac) 1);
+qed "hypreal_of_posnat_two";
+
+(*FIXME: delete this and all posnat*)
+Goalw [hypreal_of_posnat_def]
+     "hypreal_of_posnat n1 + hypreal_of_posnat n2 = \
+\     hypreal_of_posnat (n1 + n2) + 1hr";
+by (full_simp_tac (HOL_ss addsimps [hypreal_of_posnat_one RS sym,
+    hypreal_of_real_add RS sym,hypreal_of_posnat_def,real_of_preal_add RS sym,
+    preal_of_prat_add RS sym,prat_of_pnat_add RS sym,pnat_of_nat_add]) 1);
+qed "hypreal_of_posnat_add";
+
+Goal "hypreal_of_posnat (n + 1) = hypreal_of_posnat n + 1hr";
+by (res_inst_tac [("x1","1hr")] (hypreal_add_right_cancel RS iffD1) 1);
+by (rtac (hypreal_of_posnat_add RS subst) 1);
+by (full_simp_tac (simpset() addsimps [hypreal_of_posnat_two,hypreal_add_assoc]) 1);
+qed "hypreal_of_posnat_add_one";
+
+Goalw [real_of_posnat_def,hypreal_of_posnat_def] 
+      "hypreal_of_posnat n = hypreal_of_real (real_of_posnat n)";
+by (rtac refl 1);
+qed "hypreal_of_real_of_posnat";
+
+Goalw [hypreal_of_posnat_def] 
+      "(n < m) = (hypreal_of_posnat n < hypreal_of_posnat m)";
+by Auto_tac;
+qed "hypreal_of_posnat_less_iff";
+
+Addsimps [hypreal_of_posnat_less_iff RS sym];
+(*---------------------------------------------------------------------------------
+               Existence of infinite hyperreal number
+ ---------------------------------------------------------------------------------*)
+
+Goal "hyprel^^{%n::nat. real_of_posnat n} : hypreal";
+by Auto_tac;
+qed "hypreal_omega";
+
+Goalw [omega_def] "Rep_hypreal(whr) : hypreal";
+by (rtac Rep_hypreal 1);
+qed "Rep_hypreal_omega";
+
+(* existence of infinite number not corresponding to any real number *)
+(* use assumption that member FreeUltrafilterNat is not finite       *)
+(* a few lemmas first *)
+
+Goal "{n::nat. x = real_of_posnat n} = {} | \
+\     (EX y. {n::nat. x = real_of_posnat n} = {y})";
+by (auto_tac (claset() addDs [inj_real_of_posnat RS injD],simpset()));
+qed "lemma_omega_empty_singleton_disj";
+
+Goal "finite {n::nat. x = real_of_posnat n}";
+by (cut_inst_tac [("x","x")] lemma_omega_empty_singleton_disj 1);
+by Auto_tac;
+qed "lemma_finite_omega_set";
+
+Goalw [omega_def,hypreal_of_real_def] 
+      "~ (EX x. hypreal_of_real x = whr)";
+by (auto_tac (claset(),simpset() addsimps [lemma_finite_omega_set 
+    RS FreeUltrafilterNat_finite]));
+qed "not_ex_hypreal_of_real_eq_omega";
+
+Goal "hypreal_of_real x ~= whr";
+by (cut_facts_tac [not_ex_hypreal_of_real_eq_omega] 1);
+by Auto_tac;
+qed "hypreal_of_real_not_eq_omega";
+
+(* existence of infinitesimal number also not *)
+(* corresponding to any real number *)
+
+Goal "{n::nat. x = inverse(real_of_posnat n)} = {} | \
+\     (EX y. {n::nat. x = inverse(real_of_posnat n)} = {y})";
+by (Step_tac 1 THEN Step_tac 1);
+by (auto_tac (claset() addIs [real_of_posnat_inverse_inj],simpset()));
+qed "lemma_epsilon_empty_singleton_disj";
+
+Goal "finite {n::nat. x = inverse(real_of_posnat n)}";
+by (cut_inst_tac [("x","x")] lemma_epsilon_empty_singleton_disj 1);
+by Auto_tac;
+qed "lemma_finite_epsilon_set";
+
+Goalw [epsilon_def,hypreal_of_real_def] 
+      "~ (EX x. hypreal_of_real x = ehr)";
+by (auto_tac (claset(),simpset() addsimps [lemma_finite_epsilon_set 
+    RS FreeUltrafilterNat_finite]));
+qed "not_ex_hypreal_of_real_eq_epsilon";
+
+Goal "hypreal_of_real x ~= ehr";
+by (cut_facts_tac [not_ex_hypreal_of_real_eq_epsilon] 1);
+by Auto_tac;
+qed "hypreal_of_real_not_eq_epsilon";
+
+Goalw [epsilon_def,hypreal_zero_def] "ehr ~= 0";
+by (auto_tac (claset(),
+     simpset() addsimps [rename_numerals real_of_posnat_not_eq_zero]));
+qed "hypreal_epsilon_not_zero";
+
+Addsimps [rename_numerals real_of_posnat_not_eq_zero];
+Goalw [omega_def,hypreal_zero_def] "whr ~= 0";
+by (Simp_tac 1);
+qed "hypreal_omega_not_zero";
+
+Goal "ehr = inverse(whr)";
+by (asm_full_simp_tac (simpset() addsimps 
+                     [hypreal_inverse, omega_def, epsilon_def]) 1);
+qed "hypreal_epsilon_inverse_omega";
+
+(*----------------------------------------------------------------
+     Another embedding of the naturals in the 
+    hyperreals (see hypreal_of_posnat)
+ ----------------------------------------------------------------*)
+Goalw [hypreal_of_nat_def] "hypreal_of_nat 0 = 0";
+by (full_simp_tac (simpset() addsimps [hypreal_of_posnat_one]) 1);
+qed "hypreal_of_nat_zero";
+
+Goalw [hypreal_of_nat_def] "hypreal_of_nat 1 = 1hr";
+by (full_simp_tac (simpset() addsimps [hypreal_of_posnat_two,
+                                       hypreal_add_assoc]) 1);
+qed "hypreal_of_nat_one";
+
+Goalw [hypreal_of_nat_def]
+     "hypreal_of_nat n1 + hypreal_of_nat n2 = hypreal_of_nat (n1 + n2)";
+by (full_simp_tac (simpset() addsimps hypreal_add_ac) 1);
+by (simp_tac (simpset() addsimps [hypreal_of_posnat_add,
+                                  hypreal_add_assoc RS sym]) 1);
+qed "hypreal_of_nat_add";
+
+Goal "hypreal_of_nat 2 = 1hr + 1hr";
+by (simp_tac (simpset() addsimps [hypreal_of_nat_one 
+    RS sym,hypreal_of_nat_add]) 1);
+qed "hypreal_of_nat_two";
+
+Goalw [hypreal_of_nat_def] 
+      "(n < m) = (hypreal_of_nat n < hypreal_of_nat m)";
+by (auto_tac (claset() addIs [hypreal_add_less_mono1],simpset()));
+qed "hypreal_of_nat_less_iff";
+Addsimps [hypreal_of_nat_less_iff RS sym];
+
+(*------------------------------------------------------------*)
+(* naturals embedded in hyperreals                            *)
+(* is a hyperreal c.f. NS extension                           *)
+(*------------------------------------------------------------*)
+
+Goalw [hypreal_of_nat_def,real_of_nat_def] 
+      "hypreal_of_nat  m = Abs_hypreal(hyprel^^{%n. real_of_nat m})";
+by (auto_tac (claset(),simpset() addsimps [hypreal_of_real_def,
+    hypreal_of_real_of_posnat,hypreal_minus,hypreal_one_def,hypreal_add]));
+qed "hypreal_of_nat_iff";
+
+Goal "inj hypreal_of_nat";
+by (rtac injI 1);
+by (auto_tac (claset() addSDs [FreeUltrafilterNat_P],
+        simpset() addsimps [split_if_mem1, hypreal_of_nat_iff,
+        real_add_right_cancel,inj_real_of_nat RS injD]));
+qed "inj_hypreal_of_nat";
+
+Goalw [hypreal_of_nat_def,hypreal_of_real_def,hypreal_of_posnat_def,
+       real_of_posnat_def,hypreal_one_def,real_of_nat_def] 
+       "hypreal_of_nat n = hypreal_of_real (real_of_nat n)";
+by (simp_tac (simpset() addsimps [hypreal_add,hypreal_minus]) 1);
+qed "hypreal_of_nat_real_of_nat";
+
+Goal "hypreal_of_posnat (Suc n) = hypreal_of_posnat n + 1hr";
+by (stac (hypreal_of_posnat_add_one RS sym) 1);
+by (Simp_tac 1);
+qed "hypreal_of_posnat_Suc";
+
+Goalw [hypreal_of_nat_def] 
+      "hypreal_of_nat (Suc n) = hypreal_of_nat n + 1hr";
+by (simp_tac (simpset() addsimps [hypreal_of_posnat_Suc] @ hypreal_add_ac) 1);
+qed "hypreal_of_nat_Suc";
+
+(* this proof is so much simpler than one for reals!! *)
+Goal "[| 0 < r; r < x |] ==> inverse x < inverse (r::hypreal)";
+by (res_inst_tac [("z","x")] eq_Abs_hypreal 1);
+by (res_inst_tac [("z","r")] eq_Abs_hypreal 1);
+by (auto_tac (claset(),simpset() addsimps [hypreal_inverse,
+    hypreal_less,hypreal_zero_def]));
+by (ultra_tac (claset() addIs [real_inverse_less_swap],simpset()) 1);
+qed "hypreal_inverse_less_swap";
+
+Goal "[| 0 < r; 0 < x|] ==> (r < x) = (inverse x < inverse (r::hypreal))";
+by (auto_tac (claset() addIs [hypreal_inverse_less_swap],simpset()));
+by (res_inst_tac [("t","r")] (hypreal_inverse_inverse RS subst) 1);
+by (res_inst_tac [("t","x")] (hypreal_inverse_inverse RS subst) 1);
+by (rtac hypreal_inverse_less_swap 1);
+by (auto_tac (claset(), simpset() addsimps [hypreal_inverse_gt_zero]));
+qed "hypreal_inverse_less_iff";
+
+Goal "[| 0 < z; x < y |] ==> x * inverse z < y * inverse (z::hypreal)";
+by (blast_tac (claset() addSIs [hypreal_mult_less_mono1,
+                                hypreal_inverse_gt_zero]) 1);
+qed "hypreal_mult_inverse_less_mono1";
+
+Goal "[| 0 < z; x < y |] ==> inverse z * x < inverse (z::hypreal) * y";
+by (blast_tac (claset() addSIs [hypreal_mult_less_mono2,
+                                hypreal_inverse_gt_zero]) 1);
+qed "hypreal_mult_inverse_less_mono2";
+
+Goal "[| (0::hypreal) < z; x*z < y*z |] ==> x < y";
+by (forw_inst_tac [("x","x*z")] hypreal_mult_inverse_less_mono1 1);
+by (dtac (hypreal_not_refl2 RS not_sym) 2);
+by (auto_tac (claset() addSDs [hypreal_mult_inverse],
+              simpset() addsimps hypreal_mult_ac));
+qed "hypreal_less_mult_right_cancel";
+
+Goal "[| (0::hypreal) < z; z*x < z*y |] ==> x < y";
+by (auto_tac (claset() addIs [hypreal_less_mult_right_cancel],
+    simpset() addsimps [hypreal_mult_commute]));
+qed "hypreal_less_mult_left_cancel";
+
+Goal "[| 0 < r; (0::hypreal) < ra; r < x; ra < y |] ==> r*ra < x*y";
+by (forw_inst_tac [("y","r")] order_less_trans 1);
+by (dres_inst_tac [("z","ra"),("x","r")] hypreal_mult_less_mono1 2);
+by (dres_inst_tac [("z","x"),("x","ra")] hypreal_mult_less_mono2 3);
+by (auto_tac (claset() addIs [order_less_trans], simpset()));
+qed "hypreal_mult_less_gt_zero"; 
+
+Goal "[| 0 < r; (0::hypreal) < ra; r <= x; ra <= y |] ==> r*ra <= x*y";
+by (REPEAT(dtac order_le_imp_less_or_eq 1));
+by (rtac hypreal_less_or_eq_imp_le 1);
+by (auto_tac (claset() addIs [hypreal_mult_less_mono1,
+                         hypreal_mult_less_mono2,hypreal_mult_less_gt_zero],
+    simpset()));
+qed "hypreal_mult_le_ge_zero"; 
+
+(*----------------------------------------------------------------------------
+     Some convenient biconditionals for products of signs 
+ ----------------------------------------------------------------------------*)
+
+Goal "((0::hypreal) < x*y) = (0 < x & 0 < y | x < 0 & y < 0)";
+ by (auto_tac (claset(), 
+               simpset() addsimps [order_le_less, linorder_not_less, 
+                              hypreal_mult_order, hypreal_mult_less_zero1]));
+by (ALLGOALS (rtac ccontr)); 
+by (auto_tac (claset(), 
+              simpset() addsimps [order_le_less, linorder_not_less]));
+by (ALLGOALS (etac rev_mp)); 
+by (ALLGOALS (dtac hypreal_mult_less_zero THEN' assume_tac));
+by (auto_tac (claset() addDs [order_less_not_sym], 
+              simpset() addsimps [hypreal_mult_commute]));  
+qed "hypreal_zero_less_mult_iff";
+
+Goal "((0::hypreal) <= x*y) = (0 <= x & 0 <= y | x <= 0 & y <= 0)";
+by (auto_tac (claset() addDs [hypreal_mult_zero_disj],
+              simpset() addsimps [order_le_less, linorder_not_less,
+                                  hypreal_zero_less_mult_iff]));
+qed "hypreal_zero_le_mult_iff";
+
+Goal "(x*y < (0::hypreal)) = (0 < x & y < 0 | x < 0 & 0 < y)";
+by (auto_tac (claset(), 
+              simpset() addsimps [hypreal_zero_le_mult_iff, 
+                                  linorder_not_le RS sym]));
+by (auto_tac (claset() addDs [order_less_not_sym],  
+              simpset() addsimps [linorder_not_le]));
+qed "hypreal_mult_less_zero_iff";
+
+Goal "(x*y <= (0::hypreal)) = (0 <= x & y <= 0 | x <= 0 & 0 <= y)";
+by (auto_tac (claset() addDs [order_less_not_sym], 
+              simpset() addsimps [hypreal_zero_less_mult_iff, 
+                                  linorder_not_less RS sym]));
+qed "hypreal_mult_le_zero_iff";
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Hyperreal/HyperOrd.thy	Sat Dec 30 22:03:47 2000 +0100
@@ -0,0 +1,15 @@
+(*  Title:	 Real/Hyperreal/HyperOrd.thy
+    Author:      Jacques D. Fleuriot
+    Copyright:   2000 University of Edinburgh
+    Description: Type "hypreal" is a linear order and also 
+                 satisfies plus_ac0: + is an AC-operator with zero
+*)
+
+HyperOrd = HyperDef +
+
+instance hypreal :: order (hypreal_le_refl,hypreal_le_trans,hypreal_le_anti_sym,hypreal_less_le)
+instance hypreal :: linorder (hypreal_le_linear)
+
+instance hypreal :: plus_ac0(hypreal_add_commute,hypreal_add_assoc,hypreal_add_zero_left)
+
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Hyperreal/HyperPow.ML	Sat Dec 30 22:03:47 2000 +0100
@@ -0,0 +1,521 @@
+(*  Title       : HyperPow.ML
+    Author      : Jacques D. Fleuriot  
+    Copyright   : 1998  University of Cambridge
+    Description : Natural Powers of hyperreals theory
+
+*)
+
+Goal "(#0::hypreal) ^ (Suc n) = 0";
+by (Auto_tac);
+qed "hrealpow_zero";
+Addsimps [hrealpow_zero];
+
+Goal "r ~= (#0::hypreal) --> r ^ n ~= 0";
+by (induct_tac "n" 1);
+by Auto_tac;
+qed_spec_mp "hrealpow_not_zero";
+
+Goal "r ~= (#0::hypreal) --> inverse(r ^ n) = (inverse r) ^ n";
+by (induct_tac "n" 1);
+by (Auto_tac);
+by (forw_inst_tac [("n","n")] hrealpow_not_zero 1);
+by (auto_tac (claset(), simpset() addsimps [hypreal_inverse_distrib]));
+qed_spec_mp "hrealpow_inverse";
+
+Goal "abs (r::hypreal) ^ n = abs (r ^ n)";
+by (induct_tac "n" 1);
+by (auto_tac (claset(), simpset() addsimps [hrabs_mult]));
+qed "hrealpow_hrabs";
+
+Goal "(r::hypreal) ^ (n + m) = (r ^ n) * (r ^ m)";
+by (induct_tac "n" 1);
+by (auto_tac (claset(), simpset() addsimps hypreal_mult_ac));
+qed "hrealpow_add";
+
+Goal "(r::hypreal) ^ 1 = r";
+by (Simp_tac 1);
+qed "hrealpow_one";
+Addsimps [hrealpow_one];
+
+Goal "(r::hypreal) ^ 2 = r * r";
+by (Simp_tac 1);
+qed "hrealpow_two";
+
+Goal "(#0::hypreal) <= r --> #0 <= r ^ n";
+by (induct_tac "n" 1);
+by (auto_tac (claset(), simpset() addsimps [hypreal_0_le_mult_iff]));
+qed_spec_mp "hrealpow_ge_zero";
+
+Goal "(#0::hypreal) < r --> #0 < r ^ n";
+by (induct_tac "n" 1);
+by (auto_tac (claset(), simpset() addsimps [hypreal_0_less_mult_iff]));
+qed_spec_mp "hrealpow_gt_zero";
+
+Goal "x <= y & (#0::hypreal) < x --> x ^ n <= y ^ n";
+by (induct_tac "n" 1);
+by (auto_tac (claset() addSIs [hypreal_mult_le_mono], simpset()));
+by (asm_simp_tac (simpset() addsimps [hrealpow_ge_zero]) 1);
+qed_spec_mp "hrealpow_le";
+
+Goal "x < y & (#0::hypreal) < x & 0 < n --> x ^ n < y ^ n";
+by (induct_tac "n" 1);
+by (auto_tac (claset() addIs [hypreal_mult_less_mono,gr0I],
+              simpset() addsimps [hrealpow_gt_zero]));
+qed "hrealpow_less";
+
+Goal "#1 ^ n = (#1::hypreal)";
+by (induct_tac "n" 1);
+by (Auto_tac);
+qed "hrealpow_eq_one";
+Addsimps [hrealpow_eq_one];
+
+Goal "abs(-(#1 ^ n)) = (#1::hypreal)";
+by Auto_tac;  
+qed "hrabs_minus_hrealpow_one";
+Addsimps [hrabs_minus_hrealpow_one];
+
+Goal "abs(#-1 ^ n) = (#1::hypreal)";
+by (induct_tac "n" 1);
+by Auto_tac;  
+qed "hrabs_hrealpow_minus_one";
+Addsimps [hrabs_hrealpow_minus_one];
+
+Goal "((r::hypreal) * s) ^ n = (r ^ n) * (s ^ n)";
+by (induct_tac "n" 1);
+by (auto_tac (claset(), simpset() addsimps hypreal_mult_ac));
+qed "hrealpow_mult";
+
+Goal "(#0::hypreal) <= r ^ 2";
+by (auto_tac (claset(), simpset() addsimps [hypreal_0_le_mult_iff]));
+qed "hrealpow_two_le";
+Addsimps [hrealpow_two_le];
+
+Goal "(#0::hypreal) <= u ^ 2 + v ^ 2";
+by (simp_tac (HOL_ss addsimps [hrealpow_two_le, 
+                    rename_numerals hypreal_le_add_order]) 1); 
+qed "hrealpow_two_le_add_order";
+Addsimps [hrealpow_two_le_add_order];
+
+Goal "(#0::hypreal) <= u ^ 2 + v ^ 2 + w ^ 2";
+by (simp_tac (HOL_ss addsimps [hrealpow_two_le, 
+                    rename_numerals hypreal_le_add_order]) 1); 
+qed "hrealpow_two_le_add_order2";
+Addsimps [hrealpow_two_le_add_order2];
+
+Goal "(x ^ 2 + y ^ 2 + z ^ 2 = (#0::hypreal)) = (x = #0 & y = #0 & z = #0)";
+by (simp_tac (HOL_ss addsimps
+      [rename_numerals hypreal_three_squares_add_zero_iff, hrealpow_two]) 1);
+qed "hrealpow_three_squares_add_zero_iff";
+Addsimps [hrealpow_three_squares_add_zero_iff];
+
+Goal "abs(x ^ 2) = (x::hypreal) ^ 2";
+by (auto_tac (claset(), 
+     simpset() addsimps [hrabs_def, hypreal_0_le_mult_iff])); 
+qed "hrabs_hrealpow_two";
+Addsimps [hrabs_hrealpow_two];
+
+Goal "abs(x) ^ 2 = (x::hypreal) ^ 2";
+by (simp_tac (simpset() addsimps [hrealpow_hrabs, hrabs_eqI1] 
+                        delsimps [hpowr_Suc]) 1);
+qed "hrealpow_two_hrabs";
+Addsimps [hrealpow_two_hrabs];
+
+Goal "(#1::hypreal) < r ==> #1 < r ^ 2";
+by (auto_tac (claset(), simpset() addsimps [hrealpow_two]));
+by (res_inst_tac [("y","#1*#1")] order_le_less_trans 1); 
+by (rtac hypreal_mult_less_mono 2); 
+by Auto_tac;  
+qed "hrealpow_two_gt_one";
+
+Goal "(#1::hypreal) <= r ==> #1 <= r ^ 2";
+by (etac (order_le_imp_less_or_eq RS disjE) 1);
+by (etac (hrealpow_two_gt_one RS order_less_imp_le) 1);
+by Auto_tac;  
+qed "hrealpow_two_ge_one";
+
+Goal "(#1::hypreal) <= #2 ^ n";
+by (res_inst_tac [("y","#1 ^ n")] order_trans 1);
+by (rtac hrealpow_le 2);
+by (auto_tac (claset() addIs [order_less_imp_le],
+         simpset() addsimps [hypreal_zero_less_one, hypreal_one_less_two]));
+qed "two_hrealpow_ge_one";
+
+Goal "hypreal_of_nat n < #2 ^ n";
+by (induct_tac "n" 1);
+by (auto_tac (claset(),
+        simpset() addsimps [hypreal_of_nat_Suc,hypreal_of_nat_zero,
+          hypreal_zero_less_one,hypreal_add_mult_distrib,hypreal_of_nat_one]));
+by (cut_inst_tac [("n","n")] two_hrealpow_ge_one 1);
+by (arith_tac 1);
+qed "two_hrealpow_gt";
+Addsimps [two_hrealpow_gt,two_hrealpow_ge_one];
+
+Goal "#-1 ^ (2*n) = (#1::hypreal)";
+by (induct_tac "n" 1);
+by (Auto_tac);
+qed "hrealpow_minus_one";
+
+Goal "#-1 ^ (n + n) = (#1::hypreal)";
+by (induct_tac "n" 1);
+by (Auto_tac);
+qed "hrealpow_minus_one2";
+Addsimps [hrealpow_minus_one2];
+
+Goal "(-(x::hypreal)) ^ 2 = x ^ 2";
+by (Auto_tac);
+qed "hrealpow_minus_two";
+Addsimps [hrealpow_minus_two];
+
+Goal "(#0::hypreal) < r & r < #1 --> r ^ Suc n < r ^ n";
+by (induct_tac "n" 1);
+by (auto_tac (claset(),
+              simpset() addsimps [hypreal_mult_less_mono2]));
+qed_spec_mp "hrealpow_Suc_less";
+
+Goal "(#0::hypreal) <= r & r < #1 --> r ^ Suc n <= r ^ n";
+by (induct_tac "n" 1);
+by (auto_tac (claset() addIs [order_less_imp_le]
+                       addSDs [order_le_imp_less_or_eq,hrealpow_Suc_less],
+              simpset() addsimps [hypreal_mult_less_mono2]));
+qed_spec_mp "hrealpow_Suc_le";
+
+Goal "Abs_hypreal(hyprel^^{%n. X n}) ^ m = Abs_hypreal(hyprel^^{%n. (X n) ^ m})";
+by (induct_tac "m" 1);
+by (auto_tac (claset(),
+              simpset() delsimps [one_eq_numeral_1]
+			addsimps [hypreal_one_def, hypreal_mult,
+                                  one_eq_numeral_1 RS sym]));
+qed "hrealpow";
+
+Goal "(x + (y::hypreal)) ^ 2 = \
+\     x ^ 2 + y ^ 2 + (hypreal_of_nat 2)*x*y";
+by (simp_tac (simpset() addsimps [hypreal_add_mult_distrib2,
+               hypreal_add_mult_distrib,hypreal_of_nat_two] 
+                @ hypreal_add_ac @ hypreal_mult_ac) 1);
+qed "hrealpow_sum_square_expand";
+
+(*---------------------------------------------------------------
+   we'll prove the following theorem by going down to the
+   level of the ultrafilter and relying on the analogous
+   property for the real rather than prove it directly 
+   using induction: proof is much simpler this way!
+ ---------------------------------------------------------------*)
+Goal "[|(#0::hypreal) <= x; #0 <= y;x ^ Suc n <= y ^ Suc n |] ==> x <= y";
+by (full_simp_tac
+    (HOL_ss addsimps [zero_eq_numeral_0 RS sym, hypreal_zero_def]) 1);
+by (res_inst_tac [("z","x")] eq_Abs_hypreal 1);
+by (res_inst_tac [("z","y")] eq_Abs_hypreal 1);
+by (auto_tac (claset(),
+              simpset() addsimps [hrealpow,hypreal_le,hypreal_mult]));
+by (ultra_tac (claset() addIs [realpow_increasing], simpset()) 1);
+qed "hrealpow_increasing";
+
+(*By antisymmetry with the above we conclude x=y, replacing the deleted 
+  theorem hrealpow_Suc_cancel_eq*)
+
+Goal "x : HFinite --> x ^ n : HFinite";
+by (induct_tac "n" 1);
+by (auto_tac (claset() addIs [HFinite_mult], simpset()));
+qed_spec_mp "hrealpow_HFinite";
+
+(*---------------------------------------------------------------
+                  Hypernaturals Powers
+ --------------------------------------------------------------*)
+Goalw [congruent_def]
+     "congruent hyprel \
+\    (%X Y. hyprel^^{%n. ((X::nat=>real) n ^ (Y::nat=>nat) n)})";
+by (safe_tac (claset() addSIs [ext]));
+by (ALLGOALS(Fuf_tac));
+qed "hyperpow_congruent";
+
+Goalw [hyperpow_def]
+  "Abs_hypreal(hyprel^^{%n. X n}) pow Abs_hypnat(hypnatrel^^{%n. Y n}) = \
+\  Abs_hypreal(hyprel^^{%n. X n ^ Y n})";
+by (res_inst_tac [("f","Abs_hypreal")] arg_cong 1);
+by (auto_tac (claset() addSIs [lemma_hyprel_refl,bexI],
+    simpset() addsimps [hyprel_in_hypreal RS 
+    Abs_hypreal_inverse,equiv_hyprel,hyperpow_congruent]));
+by (Fuf_tac 1);
+qed "hyperpow";
+
+Goalw [hypnat_one_def] "(#0::hypreal) pow (n + 1hn) = #0";
+by (simp_tac (HOL_ss addsimps [zero_eq_numeral_0 RS sym, hypreal_zero_def]) 1);
+by (res_inst_tac [("z","n")] eq_Abs_hypnat 1);
+by (auto_tac (claset(), simpset() addsimps [hyperpow,hypnat_add]));
+qed "hyperpow_zero";
+Addsimps [hyperpow_zero];
+
+Goal "r ~= (#0::hypreal) --> r pow n ~= #0";
+by (simp_tac (HOL_ss addsimps [zero_eq_numeral_0 RS sym, hypreal_zero_def]) 1);
+by (res_inst_tac [("z","n")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","r")] eq_Abs_hypreal 1);
+by (auto_tac (claset(), simpset() addsimps [hyperpow]));
+by (dtac FreeUltrafilterNat_Compl_mem 1);
+by (fuf_empty_tac (claset() addIs [realpow_not_zero RS notE],
+    simpset()) 1);
+qed_spec_mp "hyperpow_not_zero";
+
+Goal "r ~= (#0::hypreal) --> inverse(r pow n) = (inverse r) pow n";
+by (simp_tac (HOL_ss addsimps [zero_eq_numeral_0 RS sym, hypreal_zero_def]) 1);
+by (res_inst_tac [("z","n")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","r")] eq_Abs_hypreal 1);
+by (auto_tac (claset() addSDs [FreeUltrafilterNat_Compl_mem],
+              simpset() addsimps [hypreal_inverse,hyperpow]));
+by (rtac FreeUltrafilterNat_subset 1);
+by (auto_tac (claset() addDs [realpow_not_zero] 
+                       addIs [realpow_inverse], 
+              simpset()));
+qed "hyperpow_inverse";
+
+Goal "abs r pow n = abs (r pow n)";
+by (res_inst_tac [("z","n")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","r")] eq_Abs_hypreal 1);
+by (auto_tac (claset(),
+              simpset() addsimps [hypreal_hrabs, hyperpow,realpow_abs]));
+qed "hyperpow_hrabs";
+
+Goal "r pow (n + m) = (r pow n) * (r pow m)";
+by (res_inst_tac [("z","n")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","m")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","r")] eq_Abs_hypreal 1);
+by (auto_tac (claset(),
+          simpset() addsimps [hyperpow,hypnat_add, hypreal_mult,realpow_add]));
+qed "hyperpow_add";
+
+Goalw [hypnat_one_def] "r pow 1hn = r";
+by (res_inst_tac [("z","r")] eq_Abs_hypreal 1);
+by (auto_tac (claset(), simpset() addsimps [hyperpow]));
+qed "hyperpow_one";
+Addsimps [hyperpow_one];
+
+Goalw [hypnat_one_def] 
+      "r pow (1hn + 1hn) = r * r";
+by (res_inst_tac [("z","r")] eq_Abs_hypreal 1);
+by (auto_tac (claset(),
+           simpset() addsimps [hyperpow,hypnat_add, hypreal_mult,realpow_two]));
+qed "hyperpow_two";
+
+Goal "(#0::hypreal) < r --> #0 < r pow n";
+by (simp_tac (HOL_ss addsimps [zero_eq_numeral_0 RS sym, hypreal_zero_def]) 1);
+by (res_inst_tac [("z","n")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","r")] eq_Abs_hypreal 1);
+by (auto_tac (claset() addSEs [FreeUltrafilterNat_subset, realpow_gt_zero],
+              simpset() addsimps [hyperpow,hypreal_less, hypreal_le]));
+qed_spec_mp "hyperpow_gt_zero";
+
+Goal "(#0::hypreal) < r --> #0 <= r pow n";
+by (blast_tac (claset() addSIs [hyperpow_gt_zero, order_less_imp_le]) 1);
+qed_spec_mp "hyperpow_ge_zero";
+
+Goal "(#0::hypreal) <= r --> #0 <= r pow n";
+by (simp_tac (HOL_ss addsimps [zero_eq_numeral_0 RS sym, hypreal_zero_def]) 1);
+by (res_inst_tac [("z","n")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","r")] eq_Abs_hypreal 1);
+by (auto_tac (claset() addSEs [FreeUltrafilterNat_subset, realpow_ge_zero2],
+              simpset() addsimps [hyperpow,hypreal_le]));
+qed "hyperpow_ge_zero2";
+
+Goal "(#0::hypreal) < x & x <= y --> x pow n <= y pow n";
+by (full_simp_tac
+    (HOL_ss addsimps [zero_eq_numeral_0 RS sym, hypreal_zero_def]) 1);
+by (res_inst_tac [("z","n")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","x")] eq_Abs_hypreal 1);
+by (res_inst_tac [("z","y")] eq_Abs_hypreal 1);
+by (auto_tac (claset() addIs [realpow_le,
+    (FreeUltrafilterNat_Int RS FreeUltrafilterNat_subset)],
+    simpset() addsimps [hyperpow,hypreal_le,hypreal_less]));
+qed_spec_mp "hyperpow_le";
+
+Goal "#1 pow n = (#1::hypreal)";
+by (simp_tac (HOL_ss addsimps [one_eq_numeral_1 RS sym, hypreal_one_def]) 1);
+by (res_inst_tac [("z","n")] eq_Abs_hypnat 1);
+by (auto_tac (claset(), simpset() addsimps [hyperpow]));
+qed "hyperpow_eq_one";
+Addsimps [hyperpow_eq_one];
+
+Goal "abs(-(#1 pow n)) = (#1::hypreal)";
+by (simp_tac (HOL_ss addsimps [one_eq_numeral_1 RS sym, hypreal_one_def]) 1);
+by (res_inst_tac [("z","n")] eq_Abs_hypnat 1);
+by (auto_tac (claset(), simpset() addsimps [hyperpow,hypreal_hrabs]));
+qed "hrabs_minus_hyperpow_one";
+Addsimps [hrabs_minus_hyperpow_one];
+
+Goal "abs(#-1 pow n) = (#1::hypreal)";
+by (subgoal_tac "abs((-1hr) pow n) = 1hr" 1);
+by (Asm_full_simp_tac 1); 
+by (simp_tac (HOL_ss addsimps [hypreal_one_def]) 1);
+by (res_inst_tac [("z","n")] eq_Abs_hypnat 1);
+by (auto_tac (claset(),
+       simpset() addsimps [hyperpow,hypreal_minus,hypreal_hrabs]));
+qed "hrabs_hyperpow_minus_one";
+Addsimps [hrabs_hyperpow_minus_one];
+
+Goal "(r * s) pow n = (r pow n) * (s pow n)";
+by (res_inst_tac [("z","n")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","r")] eq_Abs_hypreal 1);
+by (res_inst_tac [("z","s")] eq_Abs_hypreal 1);
+by (auto_tac (claset(),
+       simpset() addsimps [hyperpow, hypreal_mult,realpow_mult]));
+qed "hyperpow_mult";
+
+Goal "(#0::hypreal) <= r pow (1hn + 1hn)";
+by (auto_tac (claset(), 
+              simpset() addsimps [hyperpow_two, hypreal_0_le_mult_iff]));
+qed "hyperpow_two_le";
+Addsimps [hyperpow_two_le];
+
+Goal "abs(x pow (1hn + 1hn)) = x pow (1hn + 1hn)";
+by (simp_tac (simpset() addsimps [hrabs_eqI1]) 1);
+qed "hrabs_hyperpow_two";
+Addsimps [hrabs_hyperpow_two];
+
+Goal "abs(x) pow (1hn + 1hn)  = x pow (1hn + 1hn)";
+by (simp_tac (simpset() addsimps [hyperpow_hrabs,hrabs_eqI1]) 1);
+qed "hyperpow_two_hrabs";
+Addsimps [hyperpow_two_hrabs];
+
+(*? very similar to hrealpow_two_gt_one *)
+Goal "(#1::hypreal) < r ==> #1 < r pow (1hn + 1hn)";
+by (auto_tac (claset(), simpset() addsimps [hyperpow_two]));
+by (res_inst_tac [("y","#1*#1")] order_le_less_trans 1); 
+by (rtac hypreal_mult_less_mono 2); 
+by Auto_tac;  
+qed "hyperpow_two_gt_one";
+
+Goal "(#1::hypreal) <= r ==> #1 <= r pow (1hn + 1hn)";
+by (auto_tac (claset() addSDs [order_le_imp_less_or_eq] 
+                       addIs [hyperpow_two_gt_one,order_less_imp_le],
+              simpset()));
+qed "hyperpow_two_ge_one";
+
+Goal "(#1::hypreal) <= #2 pow n";
+by (res_inst_tac [("y","#1 pow n")] order_trans 1);
+by (rtac hyperpow_le 2);
+by (auto_tac (claset() addIs [order_less_imp_le],
+          simpset() addsimps [hypreal_zero_less_one, hypreal_one_less_two]));
+qed "two_hyperpow_ge_one";
+Addsimps [two_hyperpow_ge_one];
+
+Addsimps [simplify (simpset()) realpow_minus_one];
+
+Goal "#-1 pow ((1hn + 1hn)*n) = (#1::hypreal)";
+by (subgoal_tac "(-(1hr)) pow ((1hn + 1hn)*n) = 1hr" 1);
+by (Asm_full_simp_tac 1); 
+by (simp_tac (HOL_ss addsimps [hypreal_one_def]) 1);
+by (res_inst_tac [("z","n")] eq_Abs_hypnat 1);
+by (auto_tac (claset(),
+              simpset() addsimps [hyperpow, hypnat_add,hypreal_minus]));
+qed "hyperpow_minus_one2";
+Addsimps [hyperpow_minus_one2];
+
+Goalw [hypnat_one_def]
+     "(#0::hypreal) < r & r < #1 --> r pow (n + 1hn) < r pow n";
+by (full_simp_tac
+    (HOL_ss addsimps [zero_eq_numeral_0 RS sym, hypreal_zero_def,
+                      one_eq_numeral_1 RS sym, hypreal_one_def]) 1);
+by (res_inst_tac [("z","n")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","r")] eq_Abs_hypreal 1);
+by (auto_tac (claset() addSDs [conjI RS realpow_Suc_less] 
+                  addEs [FreeUltrafilterNat_Int RS FreeUltrafilterNat_subset],
+              simpset() addsimps [hyperpow,hypreal_less,hypnat_add]));
+qed_spec_mp "hyperpow_Suc_less";
+
+Goalw [hypnat_one_def]
+     "#0 <= r & r < (#1::hypreal) --> r pow (n + 1hn) <= r pow n";
+by (full_simp_tac
+    (HOL_ss addsimps [zero_eq_numeral_0 RS sym, hypreal_zero_def,
+                      one_eq_numeral_1 RS sym, hypreal_one_def]) 1);
+by (res_inst_tac [("z","n")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","r")] eq_Abs_hypreal 1);
+by (auto_tac (claset() addSDs [conjI RS realpow_Suc_le] addEs
+    [FreeUltrafilterNat_Int RS FreeUltrafilterNat_subset ],
+    simpset() addsimps [hyperpow,hypreal_le,hypnat_add,
+    hypreal_less]));
+qed_spec_mp "hyperpow_Suc_le";
+
+Goalw [hypnat_one_def]
+     "(#0::hypreal) <= r & r < #1 & n < N --> r pow N <= r pow n";
+by (full_simp_tac
+    (HOL_ss addsimps [zero_eq_numeral_0 RS sym, hypreal_zero_def,
+                      one_eq_numeral_1 RS sym, hypreal_one_def]) 1);
+by (res_inst_tac [("z","n")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","N")] eq_Abs_hypnat 1);
+by (res_inst_tac [("z","r")] eq_Abs_hypreal 1);
+by (auto_tac (claset(),
+        simpset() addsimps [hyperpow, hypreal_le,hypreal_less,hypnat_less]));
+by (etac (FreeUltrafilterNat_Int RS FreeUltrafilterNat_subset) 1);
+by (etac FreeUltrafilterNat_Int 1);
+by (auto_tac (claset() addSDs [conjI RS realpow_less_le],
+    simpset()));
+qed_spec_mp "hyperpow_less_le";
+
+Goal "[| (#0::hypreal) <= r; r < #1 |]  \
+\     ==> ALL N n. n < N --> r pow N <= r pow n";
+by (blast_tac (claset() addSIs [hyperpow_less_le]) 1);
+qed "hyperpow_less_le2";
+
+Goal "[| #0 <= r;  r < (#1::hypreal);  N : HNatInfinite |]  \
+\     ==> ALL n:SHNat. r pow N <= r pow n";
+by (auto_tac (claset() addSIs [hyperpow_less_le],
+              simpset() addsimps [HNatInfinite_iff]));
+qed "hyperpow_SHNat_le";
+
+Goalw [hypreal_of_real_def,hypnat_of_nat_def] 
+      "(hypreal_of_real r) pow (hypnat_of_nat n) = hypreal_of_real (r ^ n)";
+by (auto_tac (claset(), simpset() addsimps [hyperpow]));
+qed "hyperpow_realpow";
+
+Goalw [SReal_def]
+     "(hypreal_of_real r) pow (hypnat_of_nat n) : SReal";
+by (auto_tac (claset(), simpset() addsimps [hyperpow_realpow]));
+qed "hyperpow_SReal";
+Addsimps [hyperpow_SReal];
+
+Goal "N : HNatInfinite ==> (#0::hypreal) pow N = 0";
+by (dtac HNatInfinite_is_Suc 1);
+by (Auto_tac);
+qed "hyperpow_zero_HNatInfinite";
+Addsimps [hyperpow_zero_HNatInfinite];
+
+Goal "[| (#0::hypreal) <= r; r < #1; n <= N |] ==> r pow N <= r pow n";
+by (dres_inst_tac [("y","N")] hypnat_le_imp_less_or_eq 1);
+by (auto_tac (claset() addIs [hyperpow_less_le], simpset()));
+qed "hyperpow_le_le";
+
+Goal "[| (#0::hypreal) < r; r < #1 |] ==> r pow (n + 1hn) <= r";
+by (dres_inst_tac [("n","1hn")] (order_less_imp_le RS hyperpow_le_le) 1);
+by (Auto_tac);
+qed "hyperpow_Suc_le_self";
+
+Goal "[| (#0::hypreal) <= r; r < #1 |] ==> r pow (n + 1hn) <= r";
+by (dres_inst_tac [("n","1hn")] hyperpow_le_le 1);
+by (Auto_tac);
+qed "hyperpow_Suc_le_self2";
+
+Goalw [Infinitesimal_def]
+     "[| x : Infinitesimal; 0 < N |] ==> (abs x) pow N <= abs x";
+by (auto_tac (claset() addSIs [hyperpow_Suc_le_self2],
+    simpset() addsimps [hyperpow_hrabs RS sym,
+                        hypnat_gt_zero_iff2, hrabs_ge_zero,
+                        hypreal_zero_less_one]));
+qed "lemma_Infinitesimal_hyperpow";
+
+Goal "[| x : Infinitesimal; 0 < N |] ==> x pow N : Infinitesimal";
+by (rtac hrabs_le_Infinitesimal 1);
+by (dtac Infinitesimal_hrabs 1);
+by (auto_tac (claset() addSIs [lemma_Infinitesimal_hyperpow],
+    simpset() addsimps [hyperpow_hrabs RS sym]));
+qed "Infinitesimal_hyperpow";
+
+Goalw [hypnat_of_nat_def] 
+     "(x ^ n : Infinitesimal) = (x pow (hypnat_of_nat n) : Infinitesimal)";
+by (res_inst_tac [("z","x")] eq_Abs_hypreal 1);
+by (auto_tac (claset(), simpset() addsimps [hrealpow, hyperpow]));
+qed "hrealpow_hyperpow_Infinitesimal_iff";
+
+Goal "[| x : Infinitesimal; 0 < n |] ==> x ^ n : Infinitesimal";
+by (auto_tac (claset() addSIs [Infinitesimal_hyperpow],
+    simpset() addsimps [hrealpow_hyperpow_Infinitesimal_iff,
+                        hypnat_of_nat_less_iff,hypnat_of_nat_zero] 
+              delsimps [hypnat_of_nat_less_iff RS sym]));
+qed "Infinitesimal_hrealpow";
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Hyperreal/HyperPow.thy	Sat Dec 30 22:03:47 2000 +0100
@@ -0,0 +1,26 @@
+(*  Title       : HyperPow.thy
+    Author      : Jacques D. Fleuriot  
+    Copyright   : 1998  University of Cambridge
+    Description : Powers theory for hyperreals
+*)
+
+HyperPow = HRealAbs + HyperNat + RealPow +  
+
+instance hypreal :: {power}
+
+consts hpowr :: "[hypreal,nat] => hypreal"  
+primrec
+     hpowr_0   "r ^ 0       = 1hr"
+     hpowr_Suc "r ^ (Suc n) = (r::hypreal) * (r ^ n)"
+
+consts
+  "pow"  :: [hypreal,hypnat] => hypreal     (infixr 80)
+
+defs
+
+  (* hypernatural powers of hyperreals *)
+  hyperpow_def
+  "(R::hypreal) pow (N::hypnat) 
+      == Abs_hypreal(UN X:Rep_hypreal(R). UN Y: Rep_hypnat(N).
+             hyprel^^{%n::nat. (X n) ^ (Y n)})"
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Hyperreal/Hyperreal.thy	Sat Dec 30 22:03:47 2000 +0100
@@ -0,0 +1,4 @@
+
+theory Hyperreal = HSeries:
+
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Hyperreal/Lim.ML	Sat Dec 30 22:03:47 2000 +0100
@@ -0,0 +1,2324 @@
+(*  Title       : Lim.ML
+    Author      : Jacques D. Fleuriot
+    Copyright   : 1998  University of Cambridge
+    Description : Theory of limits, continuity and 
+                  differentiation of real=>real functions
+*)
+
+
+fun ARITH_PROVE str = prove_goal thy str 
+                      (fn prems => [cut_facts_tac prems 1,arith_tac 1]);
+
+
+(*---------------------------------------------------------------
+   Theory of limits, continuity and differentiation of 
+   real=>real functions 
+ ----------------------------------------------------------------*)
+
+Goalw [LIM_def] "(%x. k) -- x --> k";
+by Auto_tac;
+qed "LIM_const";
+Addsimps [LIM_const];
+
+(***-----------------------------------------------------------***)
+(***  Some Purely Standard Proofs - Can be used for comparison ***)
+(***-----------------------------------------------------------***)
+ 
+(*--------------- 
+    LIM_add    
+ ---------------*)
+Goalw [LIM_def] 
+     "[| f -- x --> l; g -- x --> m |] \
+\     ==> (%x. f(x) + g(x)) -- x --> (l + m)";
+by (Step_tac 1);
+by (REPEAT(dres_inst_tac [("x","r/#2")] spec 1));
+by (dtac (rename_numerals (real_zero_less_two RS real_inverse_gt_zero 
+          RSN (2,real_mult_less_mono2))) 1);
+by (Asm_full_simp_tac 1);
+by (Clarify_tac 1);
+by (res_inst_tac [("R1.0","s"),("R2.0","sa")] 
+    real_linear_less2 1);
+by (res_inst_tac [("x","s")] exI 1);
+by (res_inst_tac [("x","sa")] exI 2);
+by (res_inst_tac [("x","sa")] exI 3);
+by (Step_tac 1);
+by (REPEAT(dres_inst_tac [("x","xa")] spec 1) 
+    THEN step_tac (claset() addSEs [order_less_trans]) 1);
+by (REPEAT(dres_inst_tac [("x","xa")] spec 2) 
+    THEN step_tac (claset() addSEs [order_less_trans]) 2);
+by (REPEAT(dres_inst_tac [("x","xa")] spec 3) 
+    THEN step_tac (claset() addSEs [order_less_trans]) 3);
+by (ALLGOALS(rtac (abs_sum_triangle_ineq RS order_le_less_trans)));
+by (ALLGOALS(rtac (real_sum_of_halves RS subst)));
+by (auto_tac (claset() addIs [real_add_less_mono],simpset()));
+qed "LIM_add";
+
+Goalw [LIM_def] "f -- a --> L ==> (%x. -f(x)) -- a --> -L";
+by (full_simp_tac (simpset() addsimps [real_minus_add_distrib RS sym] 
+                    delsimps [real_minus_add_distrib, real_minus_minus]) 1);
+qed "LIM_minus";
+
+(*----------------------------------------------
+     LIM_add_minus
+ ----------------------------------------------*)
+Goal "[| f -- x --> l; g -- x --> m |] \
+\     ==> (%x. f(x) + -g(x)) -- x --> (l + -m)";
+by (blast_tac (claset() addDs [LIM_add,LIM_minus]) 1);
+qed "LIM_add_minus";
+
+(*----------------------------------------------
+     LIM_zero
+ ----------------------------------------------*)
+Goal "f -- a --> l ==> (%x. f(x) + -l) -- a --> #0";
+by (res_inst_tac [("z1","l")] (rename_numerals (real_add_minus RS subst)) 1);
+by (rtac LIM_add_minus 1 THEN Auto_tac);
+qed "LIM_zero";
+
+(*--------------------------
+   Limit not zero
+ --------------------------*)
+Goalw [LIM_def] "k ~= #0 ==> ~ ((%x. k) -- x --> #0)";
+by (res_inst_tac [("R1.0","k"),("R2.0","#0")] real_linear_less2 1);
+by (auto_tac (claset(), simpset() addsimps [real_abs_def]));
+by (res_inst_tac [("x","-k")] exI 1);
+by (res_inst_tac [("x","k")] exI 2);
+by Auto_tac;
+by (ALLGOALS(dres_inst_tac [("y","s")] real_dense));
+by Safe_tac;
+by (ALLGOALS(res_inst_tac [("x","r + x")] exI));
+by Auto_tac;  
+qed "LIM_not_zero";
+
+(* [| k ~= #0; (%x. k) -- x --> #0 |] ==> R *)
+bind_thm("LIM_not_zeroE", LIM_not_zero RS notE);
+
+Goal "(%x. k) -- x --> L ==> k = L";
+by (rtac ccontr 1);
+by (dtac LIM_zero 1);
+by (rtac LIM_not_zeroE 1 THEN assume_tac 2);
+by (arith_tac 1);
+qed "LIM_const_eq";
+
+(*------------------------
+     Limit is Unique
+ ------------------------*)
+Goal "[| f -- x --> L; f -- x --> M |] ==> L = M";
+by (dtac LIM_minus 1);
+by (dtac LIM_add 1 THEN assume_tac 1);
+by (auto_tac (claset() addSDs [LIM_const_eq RS sym],  simpset()));
+qed "LIM_unique";
+
+(*-------------
+    LIM_mult_zero
+ -------------*)
+Goalw [LIM_def] "[| f -- x --> #0; g -- x --> #0 |] \
+\         ==> (%x. f(x)*g(x)) -- x --> #0";
+by (Step_tac 1);
+by (dres_inst_tac [("x","#1")] spec 1);
+by (dres_inst_tac [("x","r")] spec 1);
+by (cut_facts_tac [real_zero_less_one] 1);
+by (asm_full_simp_tac (simpset() addsimps 
+    [abs_mult]) 1);
+by (Clarify_tac 1);
+by (res_inst_tac [("R1.0","s"),("R2.0","sa")] 
+    real_linear_less2 1);
+by (res_inst_tac [("x","s")] exI 1);
+by (res_inst_tac [("x","sa")] exI 2);
+by (res_inst_tac [("x","sa")] exI 3);
+by (Step_tac 1);
+by (REPEAT(dres_inst_tac [("x","xa")] spec 1) 
+    THEN step_tac (claset() addSEs [order_less_trans]) 1);
+by (REPEAT(dres_inst_tac [("x","xa")] spec 2) 
+    THEN step_tac (claset() addSEs [order_less_trans]) 2);
+by (REPEAT(dres_inst_tac [("x","xa")] spec 3) 
+    THEN step_tac (claset() addSEs [order_less_trans]) 3);
+by (ALLGOALS(res_inst_tac [("t","r")] (real_mult_1 RS subst)));
+by (ALLGOALS(rtac abs_mult_less2));
+by Auto_tac;
+qed "LIM_mult_zero";
+
+Goalw [LIM_def] "(%x. x) -- a --> a";
+by Auto_tac;
+qed "LIM_self";
+
+(*--------------------------------------------------------------
+   Limits are equal for functions equal except at limit point
+ --------------------------------------------------------------*)
+Goalw [LIM_def] 
+      "[| ALL x. x ~= a --> (f x = g x) |] \
+\           ==> (f -- a --> l) = (g -- a --> l)";
+by (auto_tac (claset(), simpset() addsimps [real_add_minus_iff]));
+qed "LIM_equal";
+
+Goal "[| (%x. f(x) + -g(x)) -- a --> #0; \
+\        g -- a --> l |] \
+\      ==> f -- a --> l";
+by (dtac LIM_add 1 THEN assume_tac 1);
+by (auto_tac (claset(), simpset() addsimps [real_add_assoc]));
+qed "LIM_trans";
+
+(***-------------------------------------------------------------***)
+(***           End of Purely Standard Proofs                     ***)
+(***-------------------------------------------------------------***)
+(*--------------------------------------------------------------
+       Standard and NS definitions of Limit
+ --------------------------------------------------------------*)
+Goalw [LIM_def,NSLIM_def,inf_close_def] 
+      "f -- x --> L ==> f -- x --NS> L";
+by (asm_full_simp_tac
+    (simpset() addsimps [Infinitesimal_FreeUltrafilterNat_iff]) 1);
+by (Step_tac 1);
+by (res_inst_tac [("z","xa")] eq_Abs_hypreal 1);
+by (auto_tac (claset(),
+      simpset() addsimps [real_add_minus_iff, starfun, hypreal_minus, 
+                          hypreal_of_real_def, hypreal_add]));
+by (rtac bexI 1 THEN rtac lemma_hyprel_refl 2 THEN Step_tac 1);
+by (dres_inst_tac [("x","u")] spec 1 THEN Clarify_tac 1);
+by (dres_inst_tac [("x","s")] spec 1 THEN Clarify_tac 1);
+by (subgoal_tac "ALL n::nat. (xa n) ~= x & \
+\                    abs ((xa n) + - x) < s --> abs (f (xa n) + - L) < u" 1);
+by (Blast_tac 2);
+by (dtac FreeUltrafilterNat_all 1);
+by (Ultra_tac 1);
+qed "LIM_NSLIM";
+ 
+(*---------------------------------------------------------------------
+    Limit: NS definition ==> standard definition
+ ---------------------------------------------------------------------*)
+
+Goal "ALL s. #0 < s --> (EX xa.  xa ~= x & \
+\        abs (xa + - x) < s  & r <= abs (f xa + -L)) \
+\     ==> ALL n::nat. EX xa.  xa ~= x & \
+\             abs(xa + -x) < inverse(real_of_posnat n) & r <= abs(f xa + -L)";
+by (Step_tac 1);
+by (cut_inst_tac [("n1","n")]
+    (real_of_posnat_gt_zero RS real_inverse_gt_zero) 1);
+by Auto_tac;
+val lemma_LIM = result();
+
+Goal "ALL s. #0 < s --> (EX xa.  xa ~= x & \
+\        abs (xa + - x) < s  & r <= abs (f xa + -L)) \
+\     ==> EX X. ALL n::nat. X n ~= x & \
+\               abs(X n + -x) < inverse(real_of_posnat n) & r <= abs(f (X n) + -L)";
+by (dtac lemma_LIM 1);
+by (dtac choice 1);
+by (Blast_tac 1);
+val lemma_skolemize_LIM2 = result();
+
+Goal "ALL n. X n ~= x & \
+\         abs (X n + - x) < inverse (real_of_posnat  n) & \
+\         r <= abs (f (X n) + - L) ==> \
+\         ALL n. abs (X n + - x) < inverse (real_of_posnat  n)";
+by (Auto_tac );
+val lemma_simp = result();
+ 
+(*-------------------
+    NSLIM => LIM
+ -------------------*)
+
+Goalw [LIM_def,NSLIM_def,inf_close_def] 
+      "f -- x --NS> L ==> f -- x --> L";
+by (asm_full_simp_tac
+    (simpset() addsimps [Infinitesimal_FreeUltrafilterNat_iff]) 1);
+by (EVERY1[Step_tac, rtac ccontr, Asm_full_simp_tac]);
+by (fold_tac [real_le_def]);
+by (dtac lemma_skolemize_LIM2 1);
+by (Step_tac 1);
+by (dres_inst_tac [("x","Abs_hypreal(hyprel^^{X})")] spec 1);
+by (asm_full_simp_tac
+    (simpset() addsimps [starfun, hypreal_minus, 
+                         hypreal_of_real_def,hypreal_add]) 1);
+by (Step_tac 1);
+by (dtac (lemma_simp RS real_seq_to_hypreal_Infinitesimal) 1);
+by (asm_full_simp_tac
+    (simpset() addsimps 
+       [Infinitesimal_FreeUltrafilterNat_iff,hypreal_of_real_def,
+        hypreal_minus, hypreal_add]) 1);
+by (Blast_tac 1); 
+by (rotate_tac 2 1);
+by (dres_inst_tac [("x","r")] spec 1);
+by (Clarify_tac 1);
+by (dtac FreeUltrafilterNat_all 1);
+by (Ultra_tac 1);
+qed "NSLIM_LIM";
+
+
+(**** Key result ****)
+Goal "(f -- x --> L) = (f -- x --NS> L)";
+by (blast_tac (claset() addIs [LIM_NSLIM,NSLIM_LIM]) 1);
+qed "LIM_NSLIM_iff";
+
+(*-------------------------------------------------------------------*)
+(*   Proving properties of limits using nonstandard definition and   *)
+(*   hence, the properties hold for standard limits as well          *)
+(*-------------------------------------------------------------------*)
+(*------------------------------------------------
+      NSLIM_mult and hence (trivially) LIM_mult
+ ------------------------------------------------*)
+
+Goalw [NSLIM_def]
+     "[| f -- x --NS> l; g -- x --NS> m |] \
+\     ==> (%x. f(x) * g(x)) -- x --NS> (l * m)";
+by (auto_tac (claset() addSIs [inf_close_mult_HFinite],  simpset()));
+qed "NSLIM_mult";
+
+Goal "[| f -- x --> l; g -- x --> m |] \
+\     ==> (%x. f(x) * g(x)) -- x --> (l * m)";
+by (asm_full_simp_tac (simpset() addsimps [LIM_NSLIM_iff, NSLIM_mult]) 1);
+qed "LIM_mult2";
+
+(*----------------------------------------------
+      NSLIM_add and hence (trivially) LIM_add
+      Note the much shorter proof
+ ----------------------------------------------*)
+Goalw [NSLIM_def]
+     "[| f -- x --NS> l; g -- x --NS> m |] \
+\     ==> (%x. f(x) + g(x)) -- x --NS> (l + m)";
+by (auto_tac (claset() addSIs [inf_close_add], simpset()));
+qed "NSLIM_add";
+
+Goal "[| f -- x --> l; g -- x --> m |] \
+\     ==> (%x. f(x) + g(x)) -- x --> (l + m)";
+by (asm_full_simp_tac (simpset() addsimps [LIM_NSLIM_iff, NSLIM_add]) 1);
+qed "LIM_add2";
+
+(*----------------------------------------------
+     NSLIM_const
+ ----------------------------------------------*)
+Goalw [NSLIM_def] "(%x. k) -- x --NS> k";
+by Auto_tac;
+qed "NSLIM_const";
+
+Addsimps [NSLIM_const];
+
+Goal "(%x. k) -- x --> k";
+by (asm_full_simp_tac (simpset() addsimps [LIM_NSLIM_iff]) 1);
+qed "LIM_const2";
+
+(*----------------------------------------------
+     NSLIM_minus
+ ----------------------------------------------*)
+Goalw [NSLIM_def] 
+      "f -- a --NS> L ==> (%x. -f(x)) -- a --NS> -L";
+by Auto_tac;  
+qed "NSLIM_minus";
+
+Goal "f -- a --> L ==> (%x. -f(x)) -- a --> -L";
+by (asm_full_simp_tac (simpset() addsimps [LIM_NSLIM_iff, NSLIM_minus]) 1);
+qed "LIM_minus2";
+
+(*----------------------------------------------
+     NSLIM_add_minus
+ ----------------------------------------------*)
+Goal "[| f -- x --NS> l; g -- x --NS> m |] \
+\     ==> (%x. f(x) + -g(x)) -- x --NS> (l + -m)";
+by (blast_tac (claset() addDs [NSLIM_add,NSLIM_minus]) 1);
+qed "NSLIM_add_minus";
+
+Goal "[| f -- x --> l; g -- x --> m |] \
+\     ==> (%x. f(x) + -g(x)) -- x --> (l + -m)";
+by (asm_full_simp_tac (simpset() addsimps [LIM_NSLIM_iff,
+    NSLIM_add_minus]) 1);
+qed "LIM_add_minus2";
+
+(*-----------------------------
+    NSLIM_inverse
+ -----------------------------*)
+Goalw [NSLIM_def] 
+     "[| f -- a --NS> L;  L ~= #0 |] \
+\     ==> (%x. inverse(f(x))) -- a --NS> (inverse L)";
+by (Clarify_tac 1);
+by (dtac spec 1);
+by (auto_tac (claset(), 
+              simpset() addsimps [hypreal_of_real_inf_close_inverse]));  
+qed "NSLIM_inverse";
+
+Goal "[| f -- a --> L; \
+\        L ~= #0 |] ==> (%x. inverse(f(x))) -- a --> (inverse L)";
+by (asm_full_simp_tac (simpset() addsimps [LIM_NSLIM_iff, NSLIM_inverse]) 1);
+qed "LIM_inverse";
+
+(*------------------------------
+    NSLIM_zero
+ ------------------------------*)
+Goal "f -- a --NS> l ==> (%x. f(x) + -l) -- a --NS> #0";
+by (res_inst_tac [("z1","l")] (rename_numerals (real_add_minus RS subst)) 1);
+by (rtac NSLIM_add_minus 1 THEN Auto_tac);
+qed "NSLIM_zero";
+
+Goal "f -- a --> l ==> (%x. f(x) + -l) -- a --> #0";
+by (asm_full_simp_tac (simpset() addsimps [LIM_NSLIM_iff, NSLIM_zero]) 1);
+qed "LIM_zero2";
+
+Goal "(%x. f(x) - l) -- x --NS> #0 ==> f -- x --NS> l";
+by (dres_inst_tac [("g","%x. l"),("m","l")] NSLIM_add 1);
+by (auto_tac (claset(),simpset() addsimps [real_diff_def, real_add_assoc]));
+qed "NSLIM_zero_cancel";
+
+Goal "(%x. f(x) - l) -- x --> #0 ==> f -- x --> l";
+by (dres_inst_tac [("g","%x. l"),("m","l")] LIM_add 1);
+by (auto_tac (claset(),simpset() addsimps [real_diff_def, real_add_assoc]));
+qed "LIM_zero_cancel";
+
+
+(*--------------------------
+   NSLIM_not_zero
+ --------------------------*)
+Goalw [NSLIM_def] "k ~= #0 ==> ~ ((%x. k) -- x --NS> #0)";
+by Auto_tac;
+by (res_inst_tac [("x","hypreal_of_real x + ehr")] exI 1);
+by (auto_tac (claset() addIs [Infinitesimal_add_inf_close_self RS inf_close_sym],
+              simpset() addsimps [rename_numerals hypreal_epsilon_not_zero]));
+qed "NSLIM_not_zero";
+
+(* [| k ~= #0; (%x. k) -- x --NS> #0 |] ==> R *)
+bind_thm("NSLIM_not_zeroE", NSLIM_not_zero RS notE);
+
+Goal "k ~= #0 ==> ~ ((%x. k) -- x --> #0)";
+by (asm_full_simp_tac (simpset() addsimps [LIM_NSLIM_iff, NSLIM_not_zero]) 1);
+qed "LIM_not_zero2";
+
+(*-------------------------------------
+   NSLIM of constant function
+ -------------------------------------*)
+Goal "(%x. k) -- x --NS> L ==> k = L";
+by (rtac ccontr 1);
+by (dtac NSLIM_zero 1);
+by (rtac NSLIM_not_zeroE 1 THEN assume_tac 2);
+by (arith_tac 1);
+qed "NSLIM_const_eq";
+
+Goal "(%x. k) -- x --> L ==> k = L";
+by (asm_full_simp_tac (simpset() addsimps [LIM_NSLIM_iff,
+    NSLIM_const_eq]) 1);
+qed "LIM_const_eq2";
+
+(*------------------------
+     NS Limit is Unique
+ ------------------------*)
+(* can actually be proved more easily by unfolding def! *)
+Goal "[| f -- x --NS> L; f -- x --NS> M |] ==> L = M";
+by (dtac NSLIM_minus 1);
+by (dtac NSLIM_add 1 THEN assume_tac 1);
+by (auto_tac (claset() addSDs [NSLIM_const_eq RS sym], simpset()));
+qed "NSLIM_unique";
+
+Goal "[| f -- x --> L; f -- x --> M |] ==> L = M";
+by (asm_full_simp_tac (simpset() addsimps [LIM_NSLIM_iff, NSLIM_unique]) 1);
+qed "LIM_unique2";
+
+(*--------------------
+    NSLIM_mult_zero
+ --------------------*)
+Goal "[| f -- x --NS> #0; g -- x --NS> #0 |] \
+\         ==> (%x. f(x)*g(x)) -- x --NS> #0";
+by (dtac NSLIM_mult 1 THEN Auto_tac);
+qed "NSLIM_mult_zero";
+
+(* we can use the corresponding thm LIM_mult2 *)
+(* for standard definition of limit           *)
+
+Goal "[| f -- x --> #0; g -- x --> #0 |] \
+\     ==> (%x. f(x)*g(x)) -- x --> #0";
+by (dtac LIM_mult2 1 THEN Auto_tac);
+qed "LIM_mult_zero2";
+
+(*----------------------------
+    NSLIM_self
+ ----------------------------*)
+Goalw [NSLIM_def] "(%x. x) -- a --NS> a";
+by (auto_tac (claset() addIs [starfun_Idfun_inf_close],simpset()));
+qed "NSLIM_self";
+
+Goal "(%x. x) -- a --> a";
+by (simp_tac (simpset() addsimps [LIM_NSLIM_iff,NSLIM_self]) 1);
+qed "LIM_self2";
+
+(*-----------------------------------------------------------------------------
+   Derivatives and Continuity - NS and Standard properties
+ -----------------------------------------------------------------------------*)
+(*---------------
+    Continuity 
+ ---------------*)
+
+Goalw [isNSCont_def] 
+      "[| isNSCont f a; y @= hypreal_of_real a |] \
+\           ==> (*f* f) y @= hypreal_of_real (f a)";
+by (Blast_tac 1);
+qed "isNSContD";
+
+Goalw [isNSCont_def,NSLIM_def] 
+      "isNSCont f a ==> f -- a --NS> (f a) ";
+by (Blast_tac 1);
+qed "isNSCont_NSLIM";
+
+Goalw [isNSCont_def,NSLIM_def] 
+      "f -- a --NS> (f a) ==> isNSCont f a";
+by Auto_tac;
+by (res_inst_tac [("Q","y = hypreal_of_real a")] 
+    (excluded_middle RS disjE) 1);
+by Auto_tac;
+qed "NSLIM_isNSCont";
+
+(*-----------------------------------------------------
+    NS continuity can be defined using NS Limit in
+    similar fashion to standard def of continuity
+ -----------------------------------------------------*)
+Goal "(isNSCont f a) = (f -- a --NS> (f a))";
+by (blast_tac (claset() addIs [isNSCont_NSLIM,NSLIM_isNSCont]) 1);
+qed "isNSCont_NSLIM_iff";
+
+(*----------------------------------------------
+  Hence, NS continuity can be given
+  in terms of standard limit
+ ---------------------------------------------*)
+Goal "(isNSCont f a) = (f -- a --> (f a))";
+by (asm_full_simp_tac (simpset() addsimps 
+    [LIM_NSLIM_iff,isNSCont_NSLIM_iff]) 1);
+qed "isNSCont_LIM_iff";
+
+(*-----------------------------------------------
+  Moreover, it's trivial now that NS continuity 
+  is equivalent to standard continuity
+ -----------------------------------------------*)
+Goalw [isCont_def] "(isNSCont f a) = (isCont f a)";
+by (rtac isNSCont_LIM_iff 1);
+qed "isNSCont_isCont_iff";
+
+(*----------------------------------------
+  Standard continuity ==> NS continuity 
+ ----------------------------------------*)
+Goal "isCont f a ==> isNSCont f a";
+by (etac (isNSCont_isCont_iff RS iffD2) 1);
+qed "isCont_isNSCont";
+
+(*----------------------------------------
+  NS continuity ==> Standard continuity 
+ ----------------------------------------*)
+Goal "isNSCont f a ==> isCont f a";
+by (etac (isNSCont_isCont_iff RS iffD1) 1);
+qed "isNSCont_isCont";
+
+(*--------------------------------------------------------------------------
+                 Alternative definition of continuity
+ --------------------------------------------------------------------------*)
+(* Prove equivalence between NS limits - *)
+(* seems easier than using standard def  *)
+Goalw [NSLIM_def] "(f -- a --NS> L) = ((%h. f(a + h)) -- #0 --NS> L)";
+by (auto_tac (claset(),simpset() addsimps [hypreal_of_real_zero]));
+by (dres_inst_tac [("x","hypreal_of_real a + x")] spec 1);
+by (dres_inst_tac [("x","-hypreal_of_real a + x")] spec 2);
+by (Step_tac 1);
+by (Asm_full_simp_tac 1);
+by (rtac ((mem_infmal_iff RS iffD2) RS 
+    (Infinitesimal_add_inf_close_self RS inf_close_sym)) 1);
+by (rtac (inf_close_minus_iff2 RS iffD1) 4);
+by (asm_full_simp_tac (simpset() addsimps [hypreal_add_commute]) 3);
+by (res_inst_tac [("z","x")] eq_Abs_hypreal 2);
+by (res_inst_tac [("z","x")] eq_Abs_hypreal 4);
+by (auto_tac (claset(),
+       simpset() addsimps [starfun, hypreal_of_real_def, hypreal_minus,
+              hypreal_add, real_add_assoc, inf_close_refl, hypreal_zero_def]));
+qed "NSLIM_h_iff";
+
+Goal "(f -- a --NS> f a) = ((%h. f(a + h)) -- #0 --NS> f a)";
+by (rtac NSLIM_h_iff 1);
+qed "NSLIM_isCont_iff";
+
+Goal "(f -- a --> f a) = ((%h. f(a + h)) -- #0 --> f(a))";
+by (simp_tac (simpset() addsimps [LIM_NSLIM_iff, NSLIM_isCont_iff]) 1);
+qed "LIM_isCont_iff";
+
+Goalw [isCont_def] "(isCont f x) = ((%h. f(x + h)) -- #0 --> f(x))";
+by (simp_tac (simpset() addsimps [LIM_isCont_iff]) 1);
+qed "isCont_iff";
+
+(*--------------------------------------------------------------------------
+   Immediate application of nonstandard criterion for continuity can offer 
+   very simple proofs of some standard property of continuous functions
+ --------------------------------------------------------------------------*)
+(*------------------------
+     sum continuous
+ ------------------------*)
+Goal "[| isCont f a; isCont g a |] ==> isCont (%x. f(x) + g(x)) a";
+by (auto_tac (claset() addIs [inf_close_add],
+              simpset() addsimps [isNSCont_isCont_iff RS sym, isNSCont_def]));
+qed "isCont_add";
+
+(*------------------------
+     mult continuous
+ ------------------------*)
+Goal "[| isCont f a; isCont g a |] ==> isCont (%x. f(x) * g(x)) a";
+by (auto_tac (claset() addSIs [starfun_mult_HFinite_inf_close],
+              simpset() delsimps [starfun_mult RS sym]
+			addsimps [isNSCont_isCont_iff RS sym, isNSCont_def]));
+qed "isCont_mult";
+
+(*-------------------------------------------
+     composition of continuous functions
+     Note very short straightforard proof!
+ ------------------------------------------*)
+Goal "[| isCont f a; isCont g (f a) |] \
+\     ==> isCont (g o f) a";
+by (auto_tac (claset(),simpset() addsimps [isNSCont_isCont_iff RS sym,
+              isNSCont_def,starfun_o RS sym]));
+qed "isCont_o";
+
+Goal "[| isCont f a; isCont g (f a) |] \
+\     ==> isCont (%x. g (f x)) a";
+by (auto_tac (claset() addDs [isCont_o],simpset() addsimps [o_def]));
+qed "isCont_o2";
+
+Goalw [isNSCont_def] "isNSCont f a ==> isNSCont (%x. - f x) a";
+by Auto_tac; 
+qed "isNSCont_minus";
+
+Goal "isCont f a ==> isCont (%x. - f x) a";
+by (auto_tac (claset(),simpset() addsimps [isNSCont_isCont_iff RS sym,
+              isNSCont_minus]));
+qed "isCont_minus";
+
+Goalw [isCont_def]  
+      "[| isCont f x; f x ~= #0 |] ==> isCont (%x. inverse (f x)) x";
+by (blast_tac (claset() addIs [LIM_inverse]) 1);
+qed "isCont_inverse";
+
+Goal "[| isNSCont f x; f x ~= #0 |] ==> isNSCont (%x. inverse (f x)) x";
+by (auto_tac (claset() addIs [isCont_inverse],simpset() addsimps 
+    [isNSCont_isCont_iff]));
+qed "isNSCont_inverse";
+
+Goalw [real_diff_def] 
+      "[| isCont f a; isCont g a |] ==> isCont (%x. f(x) - g(x)) a";
+by (auto_tac (claset() addIs [isCont_add,isCont_minus],simpset()));
+qed "isCont_diff";
+
+Goalw [isCont_def]  "isCont (%x. k) a";
+by (Simp_tac 1);
+qed "isCont_const";
+Addsimps [isCont_const];
+
+Goalw [isNSCont_def]  "isNSCont (%x. k) a";
+by (Simp_tac 1);
+qed "isNSCont_const";
+Addsimps [isNSCont_const];
+
+Goalw [isNSCont_def]  "isNSCont abs a";
+by (auto_tac (claset() addIs [inf_close_hrabs],
+              simpset() addsimps [hypreal_of_real_hrabs RS sym,
+                                  starfun_rabs_hrabs]));
+qed "isNSCont_rabs";
+Addsimps [isNSCont_rabs];
+
+Goal "isCont abs a";
+by (auto_tac (claset(), simpset() addsimps [isNSCont_isCont_iff RS sym]));
+qed "isCont_rabs";
+Addsimps [isCont_rabs];
+
+(****************************************************************
+(%* Leave as commented until I add topology theory or remove? *%)
+(%*------------------------------------------------------------
+  Elementary topology proof for a characterisation of 
+  continuity now: a function f is continuous if and only 
+  if the inverse image, {x. f(x) : A}, of any open set A 
+  is always an open set
+ ------------------------------------------------------------*%)
+Goal "[| isNSopen A; ALL x. isNSCont f x |] \
+\              ==> isNSopen {x. f x : A}";
+by (auto_tac (claset(),simpset() addsimps [isNSopen_iff1]));
+by (dtac (mem_monad_inf_close RS inf_close_sym) 1);
+by (dres_inst_tac [("x","a")] spec 1);
+by (dtac isNSContD 1 THEN assume_tac 1);
+by (dtac bspec 1 THEN assume_tac 1);
+by (dres_inst_tac [("x","( *f* f) x")] inf_close_mem_monad2 1);
+by (blast_tac (claset() addIs [starfun_mem_starset]) 1);
+qed "isNSCont_isNSopen";
+
+Goalw [isNSCont_def]
+          "ALL A. isNSopen A --> isNSopen {x. f x : A} \
+\              ==> isNSCont f x";
+by (auto_tac (claset() addSIs [(mem_infmal_iff RS iffD1) RS 
+     (inf_close_minus_iff RS iffD2)],simpset() addsimps 
+      [Infinitesimal_def,SReal_iff]));
+by (dres_inst_tac [("x","{z. abs(z + -f(x)) < ya}")] spec 1);
+by (etac (isNSopen_open_interval RSN (2,impE)) 1);
+by (auto_tac (claset(),simpset() addsimps [isNSopen_def,isNSnbhd_def]));
+by (dres_inst_tac [("x","x")] spec 1);
+by (auto_tac (claset() addDs [inf_close_sym RS inf_close_mem_monad],
+    simpset() addsimps [hypreal_of_real_zero RS sym,STAR_starfun_rabs_add_minus]));
+qed "isNSopen_isNSCont";
+
+Goal "(ALL x. isNSCont f x) = \
+\     (ALL A. isNSopen A --> isNSopen {x. f(x) : A})";
+by (blast_tac (claset() addIs [isNSCont_isNSopen,
+    isNSopen_isNSCont]) 1);
+qed "isNSCont_isNSopen_iff";
+
+(%*------- Standard version of same theorem --------*%)
+Goal "(ALL x. isCont f x) = \
+\         (ALL A. isopen A --> isopen {x. f(x) : A})";
+by (auto_tac (claset() addSIs [isNSCont_isNSopen_iff],
+              simpset() addsimps [isNSopen_isopen_iff RS sym,
+              isNSCont_isCont_iff RS sym]));
+qed "isCont_isopen_iff";
+*******************************************************************)
+
+(*-----------------------------------------------------------------
+                        Uniform continuity
+ ------------------------------------------------------------------*)
+Goalw [isNSUCont_def] 
+      "[| isNSUCont f; x @= y|] ==> (*f* f) x @= (*f* f) y";
+by (Blast_tac 1);
+qed "isNSUContD";
+
+Goalw [isUCont_def,isCont_def,LIM_def]
+     "isUCont f ==> EX x. isCont f x";
+by (Force_tac 1);
+qed "isUCont_isCont";
+
+Goalw [isNSUCont_def,isUCont_def,inf_close_def] 
+     "isUCont f ==> isNSUCont f";
+by (asm_full_simp_tac (simpset() addsimps 
+    [Infinitesimal_FreeUltrafilterNat_iff]) 1);
+by (Step_tac 1);
+by (res_inst_tac [("z","x")] eq_Abs_hypreal 1);
+by (res_inst_tac [("z","y")] eq_Abs_hypreal 1);
+by (auto_tac (claset(),simpset() addsimps [starfun,
+    hypreal_minus, hypreal_add]));
+by (rtac bexI 1 THEN rtac lemma_hyprel_refl 2 THEN Step_tac 1);
+by (dres_inst_tac [("x","u")] spec 1 THEN Clarify_tac 1);
+by (dres_inst_tac [("x","s")] spec 1 THEN Clarify_tac 1);
+by (subgoal_tac "ALL n::nat. abs ((xa n) + - (xb n)) < s --> abs (f (xa n) + - f (xb n)) < u" 1);
+by (Blast_tac 2);
+by (thin_tac "ALL x y. abs (x + - y) < s --> abs (f x + - f y) < u" 1);
+by (dtac FreeUltrafilterNat_all 1);
+by (Ultra_tac 1);
+qed "isUCont_isNSUCont";
+
+Goal "ALL s. #0 < s --> (EX z y. abs (z + - y) < s & r <= abs (f z + -f y)) \
+\     ==> ALL n::nat. EX z y.  \
+\              abs(z + -y) < inverse(real_of_posnat n) & \
+\              r <= abs(f z + -f y)";
+by (Step_tac 1);
+by (cut_inst_tac [("n1","n")] (real_of_posnat_gt_zero RS real_inverse_gt_zero) 1);
+by Auto_tac;
+val lemma_LIMu = result();
+
+Goal "ALL s. #0 < s --> (EX z y. abs (z + - y) < s  & r <= abs (f z + -f y)) \
+\     ==> EX X Y. ALL n::nat. \
+\              abs(X n + -(Y n)) < inverse(real_of_posnat n) & \
+\              r <= abs(f (X n) + -f (Y n))";
+by (dtac lemma_LIMu 1);
+by (dtac choice 1);
+by (Step_tac 1);
+by (dtac choice 1);
+by (Blast_tac 1);
+val lemma_skolemize_LIM2u = result();
+
+Goal "ALL n. abs (X n + -Y n) < inverse (real_of_posnat  n) & \
+\         r <= abs (f (X n) + - f(Y n)) ==> \
+\         ALL n. abs (X n + - Y n) < inverse (real_of_posnat  n)";
+by (Auto_tac );
+val lemma_simpu = result();
+
+Goalw [isNSUCont_def,isUCont_def,inf_close_def] 
+     "isNSUCont f ==> isUCont f";
+by (asm_full_simp_tac (simpset() addsimps 
+                       [Infinitesimal_FreeUltrafilterNat_iff]) 1);
+by (EVERY1[Step_tac, rtac ccontr, Asm_full_simp_tac]);
+by (fold_tac [real_le_def]);
+by (dtac lemma_skolemize_LIM2u 1);
+by (Step_tac 1);
+by (dres_inst_tac [("x","Abs_hypreal(hyprel^^{X})")] spec 1);
+by (dres_inst_tac [("x","Abs_hypreal(hyprel^^{Y})")] spec 1);
+by (asm_full_simp_tac
+    (simpset() addsimps [starfun, hypreal_minus,hypreal_add]) 1);
+by Auto_tac;
+by (dtac (lemma_simpu RS real_seq_to_hypreal_Infinitesimal2) 1);
+by (asm_full_simp_tac (simpset() addsimps 
+     [Infinitesimal_FreeUltrafilterNat_iff, hypreal_minus,hypreal_add]) 1);
+by (Blast_tac 1);
+by (rotate_tac 2 1);
+by (dres_inst_tac [("x","r")] spec 1);
+by (Clarify_tac 1);
+by (dtac FreeUltrafilterNat_all 1);
+by (Ultra_tac 1);
+qed "isNSUCont_isUCont";
+
+(*------------------------------------------------------------------
+                         Derivatives
+ ------------------------------------------------------------------*)
+Goalw [deriv_def] 
+      "(DERIV f x :> D) = ((%h. (f(x + h) + - f(x))/h) -- #0 --> D)";
+by (Blast_tac 1);        
+qed "DERIV_iff";
+
+Goalw [deriv_def] 
+      "(DERIV f x :> D) = ((%h. (f(x + h) + - f(x))/h) -- #0 --NS> D)";
+by (simp_tac (simpset() addsimps [LIM_NSLIM_iff]) 1);
+qed "DERIV_NS_iff";
+
+Goalw [deriv_def] 
+      "DERIV f x :> D \
+\      ==> (%h. (f(x + h) + - f(x))/h) -- #0 --> D";
+by (Blast_tac 1);        
+qed "DERIVD";
+
+Goalw [deriv_def] "DERIV f x :> D ==> \
+\          (%h. (f(x + h) + - f(x))/h) -- #0 --NS> D";
+by (asm_full_simp_tac (simpset() addsimps [LIM_NSLIM_iff]) 1);
+qed "NS_DERIVD";
+
+(* Uniqueness *)
+Goalw [deriv_def] 
+      "[| DERIV f x :> D; DERIV f x :> E |] ==> D = E";
+by (blast_tac (claset() addIs [LIM_unique]) 1);
+qed "DERIV_unique";
+
+Goalw [nsderiv_def] 
+     "[| NSDERIV f x :> D; NSDERIV f x :> E |] ==> D = E";
+by (cut_facts_tac [Infinitesimal_epsilon, hypreal_epsilon_not_zero] 1);
+by (auto_tac (claset() addSDs [inst "x" "ehr" bspec] 
+                       addSIs [inj_hypreal_of_real RS injD] 
+                       addDs [inf_close_trans3],
+              simpset()));
+qed "NSDeriv_unique";
+
+(*------------------------------------------------------------------------
+                          Differentiable
+ ------------------------------------------------------------------------*)
+
+Goalw [differentiable_def] 
+      "f differentiable x ==> EX D. DERIV f x :> D";
+by (assume_tac 1);
+qed "differentiableD";
+
+Goalw [differentiable_def] 
+      "DERIV f x :> D ==> f differentiable x";
+by (Blast_tac 1);
+qed "differentiableI";
+
+Goalw [NSdifferentiable_def] 
+      "f NSdifferentiable x ==> EX D. NSDERIV f x :> D";
+by (assume_tac 1);
+qed "NSdifferentiableD";
+
+Goalw [NSdifferentiable_def] 
+      "NSDERIV f x :> D ==> f NSdifferentiable x";
+by (Blast_tac 1);
+qed "NSdifferentiableI";
+
+(*--------------------------------------------------------
+      Alternative definition for differentiability
+ -------------------------------------------------------*)
+
+Goalw [LIM_def] 
+ "((%h. (f(a + h) + - f(a))/h) -- #0 --> D) = \
+\ ((%x. (f(x) + -f(a)) / (x + -a)) -- a --> D)";
+by (Step_tac 1);
+by (ALLGOALS(dtac spec));
+by (Step_tac 1);
+by (Blast_tac 1 THEN Blast_tac 2);
+by (ALLGOALS(res_inst_tac [("x","s")] exI));
+by (Step_tac 1);
+by (dres_inst_tac [("x","x + -a")] spec 1);
+by (dres_inst_tac [("x","x + a")] spec 2);
+by (auto_tac (claset(), simpset() addsimps real_add_ac));
+qed "DERIV_LIM_iff";
+
+Goalw [deriv_def] "(DERIV f x :> D) = \
+\         ((%z. (f(z) + -f(x)) / (z + -x)) -- x --> D)";
+by (simp_tac (simpset() addsimps [DERIV_LIM_iff]) 1);
+qed "DERIV_iff2";
+
+(*--------------------------------------------------------
+  Equivalence of NS and standard defs of differentiation
+ -------------------------------------------------------*)
+(*-------------------------------------------
+   First NSDERIV in terms of NSLIM 
+ -------------------------------------------*)
+
+(*--- first equivalence ---*)
+Goalw [nsderiv_def,NSLIM_def] 
+      "(NSDERIV f x :> D) = ((%h. (f(x + h) + - f(x))/h) -- #0 --NS> D)";
+by (auto_tac (claset(), simpset() addsimps [hypreal_of_real_zero]));
+by (dres_inst_tac [("x","xa")] bspec 1);
+by (rtac ccontr 3);
+by (dres_inst_tac [("x","h")] spec 3);
+by (auto_tac (claset(),
+              simpset() addsimps [mem_infmal_iff, starfun_lambda_cancel]));
+qed "NSDERIV_NSLIM_iff";
+
+(*--- second equivalence ---*)
+Goal "(NSDERIV f x :> D) = \
+\         ((%z. (f(z) + -f(x)) / (z + -x)) -- x --NS> D)";
+by (full_simp_tac (simpset() addsimps 
+     [NSDERIV_NSLIM_iff, DERIV_LIM_iff, LIM_NSLIM_iff RS sym]) 1);
+qed "NSDERIV_NSLIM_iff2";
+
+(* while we're at it! *)
+Goalw [real_diff_def]
+     "(NSDERIV f x :> D) = \
+\     (ALL xa. \
+\       xa ~= hypreal_of_real x & xa @= hypreal_of_real x --> \
+\       (*f* (%z. (f z - f x) / (z - x))) xa @= hypreal_of_real D)";
+by (auto_tac (claset(), simpset() addsimps [NSDERIV_NSLIM_iff2, NSLIM_def]));
+qed "NSDERIV_iff2";
+
+
+Goal "(NSDERIV f x :> D) ==> \
+\    (ALL u. \
+\       u @= hypreal_of_real x --> \
+\       (*f* (%z. f z - f x)) u @= hypreal_of_real D * (u - hypreal_of_real x))";
+by (auto_tac (claset(), simpset() addsimps [NSDERIV_iff2]));
+by (case_tac "u = hypreal_of_real x" 1);
+by (auto_tac (claset(), 
+              simpset() addsimps [hypreal_diff_def, hypreal_of_real_zero]));
+by (dres_inst_tac [("x","u")] spec 1);
+by Auto_tac;
+by (dres_inst_tac [("c","u - hypreal_of_real x"),("b","hypreal_of_real D")]
+     inf_close_mult1 1);
+by (ALLGOALS(dtac (hypreal_not_eq_minus_iff RS iffD1)));
+by (subgoal_tac "(*f* (%z. z - x)) u ~= (0::hypreal)" 2);
+by (rotate_tac ~1 2);
+by (auto_tac (claset(),
+    simpset() addsimps [real_diff_def, hypreal_diff_def, 
+		(inf_close_minus_iff RS iffD1) RS (mem_infmal_iff RS iffD2),  
+			Infinitesimal_subset_HFinite RS subsetD]));
+qed "NSDERIVD5";
+
+Goal "(NSDERIV f x :> D) ==> \
+\     (ALL h: Infinitesimal. \
+\              ((*f* f)(hypreal_of_real x + h) - \
+\                hypreal_of_real (f x))@= (hypreal_of_real D) * h)";
+by (auto_tac (claset(),simpset() addsimps [nsderiv_def]));
+by (case_tac "h = (0::hypreal)" 1);
+by (auto_tac (claset(),simpset() addsimps [hypreal_diff_def]));
+by (dres_inst_tac [("x","h")] bspec 1);
+by (dres_inst_tac [("c","h")] inf_close_mult1 2);
+by (auto_tac (claset() addIs [Infinitesimal_subset_HFinite RS subsetD],
+              simpset() addsimps [hypreal_diff_def]));
+qed "NSDERIVD4";
+
+Goal "(NSDERIV f x :> D) ==> \
+\     (ALL h: Infinitesimal - {0}. \
+\              ((*f* f)(hypreal_of_real x + h) - \
+\                hypreal_of_real (f x))@= (hypreal_of_real D) * h)";
+by (auto_tac (claset(),simpset() addsimps [nsderiv_def]));
+by (rtac ccontr 1 THEN dres_inst_tac [("x","h")] bspec 1);
+by (dres_inst_tac [("c","h")] inf_close_mult1 2);
+by (auto_tac (claset() addIs [Infinitesimal_subset_HFinite RS subsetD],
+              simpset() addsimps [hypreal_mult_assoc, hypreal_diff_def]));
+qed "NSDERIVD3";
+
+(*--------------------------------------------------------------
+          Now equivalence between NSDERIV and DERIV
+ -------------------------------------------------------------*)
+Goalw [deriv_def] "(NSDERIV f x :> D) = (DERIV f x :> D)";
+by (simp_tac (simpset() addsimps [NSDERIV_NSLIM_iff,LIM_NSLIM_iff]) 1);
+qed "NSDERIV_DERIV_iff";
+
+(*---------------------------------------------------
+         Differentiability implies continuity 
+         nice and simple "algebraic" proof
+ --------------------------------------------------*)
+Goalw [nsderiv_def]
+      "NSDERIV f x :> D ==> isNSCont f x";
+by (auto_tac (claset(),simpset() addsimps 
+        [isNSCont_NSLIM_iff,NSLIM_def]));
+by (dtac (inf_close_minus_iff RS iffD1) 1);
+by (dtac (hypreal_not_eq_minus_iff RS iffD1) 1);
+by (dres_inst_tac [("x","-hypreal_of_real x + xa")] bspec 1);
+by (asm_full_simp_tac (simpset() addsimps 
+    [hypreal_add_assoc RS sym]) 2);
+by (auto_tac (claset(),simpset() addsimps 
+    [mem_infmal_iff RS sym,hypreal_add_commute]));
+by (dres_inst_tac [("c","xa + -hypreal_of_real x")] inf_close_mult1 1);
+by (auto_tac (claset() addIs [Infinitesimal_subset_HFinite
+    RS subsetD],simpset() addsimps [hypreal_mult_assoc]));
+by (dres_inst_tac [("x3","D")] (HFinite_hypreal_of_real RSN
+    (2,Infinitesimal_HFinite_mult) RS (mem_infmal_iff RS iffD1)) 1);
+by (blast_tac (claset() addIs [inf_close_trans,
+    hypreal_mult_commute RS subst,
+    (inf_close_minus_iff RS iffD2)]) 1);
+qed "NSDERIV_isNSCont";
+
+(* Now Sandard proof *)
+Goal "DERIV f x :> D ==> isCont f x";
+by (asm_full_simp_tac (simpset() addsimps 
+    [NSDERIV_DERIV_iff RS sym, isNSCont_isCont_iff RS sym,
+     NSDERIV_isNSCont]) 1);
+qed "DERIV_isCont";
+
+(*----------------------------------------------------------------------------
+      Differentiation rules for combinations of functions
+      follow from clear, straightforard, algebraic 
+      manipulations
+ ----------------------------------------------------------------------------*)
+(*-------------------------
+    Constant function
+ ------------------------*)
+
+(* use simple constant nslimit theorem *)
+Goal "(NSDERIV (%x. k) x :> #0)";
+by (simp_tac (simpset() addsimps [NSDERIV_NSLIM_iff]) 1);
+qed "NSDERIV_const";
+Addsimps [NSDERIV_const];
+
+Goal "(DERIV (%x. k) x :> #0)";
+by (simp_tac (simpset() addsimps [NSDERIV_DERIV_iff RS sym]) 1);
+qed "DERIV_const";
+Addsimps [DERIV_const];
+
+(*-----------------------------------------------------
+    Sum of functions- proved easily
+ ----------------------------------------------------*)
+
+
+Goal "[| NSDERIV f x :> Da;  NSDERIV g x :> Db |] \
+\     ==> NSDERIV (%x. f x + g x) x :> Da + Db";
+by (asm_full_simp_tac (simpset() addsimps [NSDERIV_NSLIM_iff,
+           NSLIM_def]) 1 THEN REPEAT(Step_tac 1));
+by (auto_tac (claset(),
+       simpset() addsimps [hypreal_add_divide_distrib]));
+by (dres_inst_tac [("b","hypreal_of_real Da"),
+                   ("d","hypreal_of_real Db")] inf_close_add 1);
+by (auto_tac (claset(), simpset() addsimps hypreal_add_ac));
+qed "NSDERIV_add";
+
+(* Standard theorem *)
+Goal "[| DERIV f x :> Da; DERIV g x :> Db |] \
+\     ==> DERIV (%x. f x + g x) x :> Da + Db";
+by (asm_full_simp_tac (simpset() addsimps [NSDERIV_add,
+                                     NSDERIV_DERIV_iff RS sym]) 1);
+qed "DERIV_add";
+
+(*-----------------------------------------------------
+  Product of functions - Proof is trivial but tedious
+  and long due to rearrangement of terms  
+ ----------------------------------------------------*)
+
+Goal "((a::hypreal)*b) + -(c*d) = (b*(a + -c)) + (c*(b + -d))";
+by (simp_tac (simpset() addsimps [hypreal_add_mult_distrib2]) 1);
+val lemma_nsderiv1 = result();
+
+Goal "[| (x + y) / z = hypreal_of_real D + yb; z ~= 0; \
+\        z : Infinitesimal; yb : Infinitesimal |] \
+\     ==> x + y @= #0";
+by (forw_inst_tac [("c1","z")] (hypreal_mult_right_cancel RS iffD2) 1 
+    THEN assume_tac 1);
+by (thin_tac "(x + y) / z = hypreal_of_real D + yb" 1);
+by (auto_tac (claset() addSIs [Infinitesimal_HFinite_mult2, HFinite_add],
+              simpset() addsimps [hypreal_mult_assoc, mem_infmal_iff RS sym]));
+by (etac (Infinitesimal_subset_HFinite RS subsetD) 1);
+val lemma_nsderiv2 = result();
+
+
+Goal "[| NSDERIV f x :> Da; NSDERIV g x :> Db |] \
+\     ==> NSDERIV (%x. f x * g x) x :> (Da * g(x)) + (Db * f(x))";
+by (asm_full_simp_tac (simpset() addsimps [NSDERIV_NSLIM_iff, NSLIM_def]) 1 
+    THEN REPEAT(Step_tac 1));
+by (auto_tac (claset(),
+       simpset() addsimps [starfun_lambda_cancel, hypreal_of_real_zero,
+              lemma_nsderiv1]));
+by (simp_tac (simpset() addsimps [hypreal_add_divide_distrib]) 1); 
+by (REPEAT(dtac (bex_Infinitesimal_iff2 RS iffD2) 1));
+by (auto_tac (claset(),
+        simpset() delsimps [hypreal_times_divide1_eq]
+		  addsimps [hypreal_times_divide1_eq RS sym]));
+by (dres_inst_tac [("D","Db")] lemma_nsderiv2 1);
+by (dtac (inf_close_minus_iff RS iffD2 RS (bex_Infinitesimal_iff2 RS iffD2)) 4);
+by (auto_tac (claset() addSIs [inf_close_add_mono1],
+      simpset() addsimps [hypreal_add_mult_distrib, hypreal_add_mult_distrib2, 
+			  hypreal_mult_commute, hypreal_add_assoc]));
+by (res_inst_tac [("w1","hypreal_of_real Db * hypreal_of_real (f x)")]
+    (hypreal_add_commute RS subst) 1);
+by (auto_tac (claset() addSIs [Infinitesimal_add_inf_close_self2 RS inf_close_sym,
+			       Infinitesimal_add, Infinitesimal_mult,
+			       Infinitesimal_hypreal_of_real_mult,
+			       Infinitesimal_hypreal_of_real_mult2],
+	      simpset() addsimps [hypreal_add_assoc RS sym]));
+qed "NSDERIV_mult";
+
+Goal "[| DERIV f x :> Da; DERIV g x :> Db |] \
+\     ==> DERIV (%x. f x * g x) x :> (Da * g(x)) + (Db * f(x))";
+by (asm_full_simp_tac (simpset() addsimps [NSDERIV_mult,
+                                           NSDERIV_DERIV_iff RS sym]) 1);
+qed "DERIV_mult";
+
+(*----------------------------
+   Multiplying by a constant
+ ---------------------------*)
+Goal "NSDERIV f x :> D \
+\     ==> NSDERIV (%x. c * f x) x :> c*D";
+by (asm_full_simp_tac 
+    (simpset() addsimps [real_times_divide1_eq RS sym, NSDERIV_NSLIM_iff,
+                         real_minus_mult_eq2, real_add_mult_distrib2 RS sym] 
+             delsimps [real_times_divide1_eq, real_minus_mult_eq2 RS sym]) 1);
+by (etac (NSLIM_const RS NSLIM_mult) 1);
+qed "NSDERIV_cmult";
+
+(* let's do the standard proof though theorem *)
+(* LIM_mult2 follows from a NS proof          *)
+
+Goalw [deriv_def] 
+      "DERIV f x :> D \
+\      ==> DERIV (%x. c * f x) x :> c*D";
+by (asm_full_simp_tac 
+    (simpset() addsimps [real_times_divide1_eq RS sym, NSDERIV_NSLIM_iff,
+                         real_minus_mult_eq2, real_add_mult_distrib2 RS sym] 
+             delsimps [real_times_divide1_eq, real_minus_mult_eq2 RS sym]) 1);
+by (etac (LIM_const RS LIM_mult2) 1);
+qed "DERIV_cmult";
+
+(*--------------------------------
+   Negation of function
+ -------------------------------*)
+Goal "NSDERIV f x :> D ==> NSDERIV (%x. -(f x)) x :> -D";
+by (asm_full_simp_tac (simpset() addsimps [NSDERIV_NSLIM_iff]) 1);
+by (res_inst_tac [("t","f x")] (real_minus_minus RS subst) 1);
+by (asm_simp_tac (simpset() addsimps [real_minus_add_distrib RS sym,
+                                      real_minus_mult_eq1 RS sym] 
+                   delsimps [real_minus_add_distrib, real_minus_minus]) 1);
+by (etac NSLIM_minus 1);
+qed "NSDERIV_minus";
+
+Goal "DERIV f x :> D \
+\     ==> DERIV (%x. -(f x)) x :> -D";
+by (asm_full_simp_tac (simpset() addsimps 
+    [NSDERIV_minus,NSDERIV_DERIV_iff RS sym]) 1);
+qed "DERIV_minus";
+
+(*-------------------------------
+   Subtraction
+ ------------------------------*)
+Goal "[| NSDERIV f x :> Da; NSDERIV g x :> Db |] \
+\     ==> NSDERIV (%x. f x + -g x) x :> Da + -Db";
+by (blast_tac (claset() addDs [NSDERIV_add,NSDERIV_minus]) 1);
+qed "NSDERIV_add_minus";
+
+Goal "[| DERIV f x :> Da; DERIV g x :> Db |] \
+\     ==> DERIV (%x. f x + -g x) x :> Da + -Db";
+by (blast_tac (claset() addDs [DERIV_add,DERIV_minus]) 1);
+qed "DERIV_add_minus";
+
+Goalw [real_diff_def]
+     "[| NSDERIV f x :> Da; NSDERIV g x :> Db |] \
+\     ==> NSDERIV (%x. f x - g x) x :> Da - Db";
+by (blast_tac (claset() addIs [NSDERIV_add_minus]) 1);
+qed "NSDERIV_diff";
+
+Goalw [real_diff_def]
+     "[| DERIV f x :> Da; DERIV g x :> Db |] \
+\      ==> DERIV (%x. f x - g x) x :> Da - Db";
+by (blast_tac (claset() addIs [DERIV_add_minus]) 1);
+qed "DERIV_diff";
+
+(*---------------------------------------------------------------
+                     (NS) Increment
+ ---------------------------------------------------------------*)
+Goalw [increment_def] 
+      "f NSdifferentiable x ==> \
+\     increment f x h = (*f* f) (hypreal_of_real(x) + h) + \
+\     -hypreal_of_real (f x)";
+by (Blast_tac 1);
+qed "incrementI";
+
+Goal "NSDERIV f x :> D ==> \
+\    increment f x h = (*f* f) (hypreal_of_real(x) + h) + \
+\    -hypreal_of_real (f x)";
+by (etac (NSdifferentiableI RS incrementI) 1);
+qed "incrementI2";
+
+(* The Increment theorem -- Keisler p. 65 *)
+Goal "[| NSDERIV f x :> D; h: Infinitesimal; h ~= #0 |] \
+\     ==> EX e: Infinitesimal. increment f x h = hypreal_of_real(D)*h + e*h";
+by (forw_inst_tac [("h","h")] incrementI2 1 THEN rewtac nsderiv_def);
+by (dtac bspec 1 THEN Auto_tac);
+by (dtac (bex_Infinitesimal_iff2 RS iffD2) 1 THEN Step_tac 1);
+by (forw_inst_tac [("b1","hypreal_of_real(D) + y")] 
+    (rename_numerals (hypreal_mult_right_cancel RS iffD2)) 1);
+by (thin_tac "((*f* f) (hypreal_of_real(x) + h) + \
+\   - hypreal_of_real (f x)) / h = hypreal_of_real(D) + y" 2);
+by (assume_tac 1);
+by (asm_full_simp_tac (simpset() addsimps [hypreal_times_divide1_eq RS sym]
+             delsimps [hypreal_times_divide1_eq]) 1);
+by (auto_tac (claset(),
+              simpset() addsimps [hypreal_add_mult_distrib]));
+qed "increment_thm";
+
+Goal "[| NSDERIV f x :> D; h @= #0; h ~= #0 |] \
+\     ==> EX e: Infinitesimal. increment f x h = \
+\             hypreal_of_real(D)*h + e*h";
+by (blast_tac (claset() addSDs [mem_infmal_iff RS iffD2] 
+                        addSIs [increment_thm]) 1);
+qed "increment_thm2";
+
+Goal "[| NSDERIV f x :> D; h @= #0; h ~= #0 |] \
+\     ==> increment f x h @= #0";
+by (dtac increment_thm2 1 THEN auto_tac (claset() addSIs 
+    [Infinitesimal_HFinite_mult2,HFinite_add],simpset() addsimps 
+    [hypreal_add_mult_distrib RS sym,mem_infmal_iff RS sym]));
+by (etac (Infinitesimal_subset_HFinite RS subsetD) 1);
+qed "increment_inf_close_zero";
+
+(*---------------------------------------------------------------
+   Similarly to the above, the chain rule admits an entirely
+   straightforward derivation. Compare this with Harrison's
+   HOL proof of the chain rule, which proved to be trickier and
+   required an alternative characterisation of differentiability- 
+   the so-called Carathedory derivative. Our main problem is
+   manipulation of terms.
+ --------------------------------------------------------------*)
+
+(* lemmas *)
+Goalw [nsderiv_def] 
+      "[| NSDERIV g x :> D; \
+\              (*f* g) (hypreal_of_real(x) + xa) = hypreal_of_real(g x);\
+\              xa : Infinitesimal;\
+\              xa ~= #0 \
+\           |] ==> D = #0";
+by (dtac bspec 1);
+by Auto_tac;
+qed "NSDERIV_zero";
+
+(* can be proved differently using NSLIM_isCont_iff *)
+Goalw [nsderiv_def] 
+     "[| NSDERIV f x :> D;  h: Infinitesimal;  h ~= #0 |]  \
+\     ==> (*f* f) (hypreal_of_real(x) + h) + -hypreal_of_real(f x) @= #0";    
+by (asm_full_simp_tac (simpset() addsimps 
+    [mem_infmal_iff RS sym]) 1);
+by (rtac Infinitesimal_ratio 1);
+by (rtac inf_close_hypreal_of_real_HFinite 3);
+by Auto_tac;
+qed "NSDERIV_inf_close";
+
+(*--------------------------------------------------------------- 
+   from one version of differentiability 
+ 
+                f(x) - f(a)
+              --------------- @= Db
+                  x - a
+ ---------------------------------------------------------------*)
+Goal "[| NSDERIV f (g x) :> Da; \
+\        (*f* g) (hypreal_of_real(x) + xa) ~= hypreal_of_real (g x); \
+\        (*f* g) (hypreal_of_real(x) + xa) @= hypreal_of_real (g x) \
+\     |] ==> ((*f* f) ((*f* g) (hypreal_of_real(x) + xa)) \
+\                  + - hypreal_of_real (f (g x))) \
+\             / ((*f* g) (hypreal_of_real(x) + xa) + - hypreal_of_real (g x)) \
+\            @= hypreal_of_real(Da)";
+by (auto_tac (claset(),
+       simpset() addsimps [NSDERIV_NSLIM_iff2, NSLIM_def]));
+qed "NSDERIVD1";
+
+(*-------------------------------------------------------------- 
+   from other version of differentiability 
+
+                f(x + h) - f(x)
+               ----------------- @= Db
+                       h
+ --------------------------------------------------------------*)
+Goal "[| NSDERIV g x :> Db; xa: Infinitesimal; xa ~= #0 |] \
+\     ==> ((*f* g) (hypreal_of_real(x) + xa) + - hypreal_of_real(g x)) / xa \
+\         @= hypreal_of_real(Db)";
+by (auto_tac (claset(),
+    simpset() addsimps [NSDERIV_NSLIM_iff, NSLIM_def, 
+		hypreal_of_real_zero, mem_infmal_iff, starfun_lambda_cancel]));
+qed "NSDERIVD2";
+
+Goal "(z::hypreal) ~= 0 ==> x*y = (x*inverse(z))*(z*y)";
+by Auto_tac;  
+qed "lemma_chain";
+
+(*------------------------------------------------------
+  This proof uses both definitions of differentiability.
+ ------------------------------------------------------*)
+Goal "[| NSDERIV f (g x) :> Da; NSDERIV g x :> Db |] \
+\     ==> NSDERIV (f o g) x :> Da * Db";
+by (asm_simp_tac (simpset() addsimps [NSDERIV_NSLIM_iff,
+    NSLIM_def,hypreal_of_real_zero,mem_infmal_iff RS sym]) 1 THEN Step_tac 1);
+by (forw_inst_tac [("f","g")] NSDERIV_inf_close 1);
+by (auto_tac (claset(),
+              simpset() addsimps [starfun_lambda_cancel2, starfun_o RS sym]));
+by (case_tac "(*f* g) (hypreal_of_real(x) + xa) = hypreal_of_real (g x)" 1);
+by (dres_inst_tac [("g","g")] NSDERIV_zero 1);
+by (auto_tac (claset(),
+    simpset() addsimps [hypreal_divide_def, hypreal_of_real_zero]));
+by (res_inst_tac [("z1","(*f* g) (hypreal_of_real(x) + xa) + -hypreal_of_real (g x)"),
+    ("y1","inverse xa")] (lemma_chain RS ssubst) 1);
+by (etac (hypreal_not_eq_minus_iff RS iffD1) 1);
+by (rtac inf_close_mult_hypreal_of_real 1);
+by (fold_tac [hypreal_divide_def]);
+by (blast_tac (claset() addIs [NSDERIVD1,
+    inf_close_minus_iff RS iffD2]) 1);
+by (blast_tac (claset() addIs [NSDERIVD2]) 1);
+qed "NSDERIV_chain";
+
+(* standard version *)
+Goal "[| DERIV f (g x) :> Da; \
+\                 DERIV g x :> Db \
+\              |] ==> DERIV (f o g) x :> Da * Db";
+by (asm_full_simp_tac (simpset() addsimps [NSDERIV_DERIV_iff RS sym,
+    NSDERIV_chain]) 1);
+qed "DERIV_chain";
+
+Goal "[| DERIV f (g x) :> Da; DERIV g x :> Db |] \
+\     ==> DERIV (%x. f (g x)) x :> Da * Db";
+by (auto_tac (claset() addDs [DERIV_chain], simpset() addsimps [o_def]));
+qed "DERIV_chain2";
+
+(*------------------------------------------------------------------
+           Differentiation of natural number powers
+ ------------------------------------------------------------------*)
+Goal "NSDERIV (%x. x) x :> #1";
+by (auto_tac (claset(),
+     simpset() addsimps [NSDERIV_NSLIM_iff,
+          NSLIM_def ,starfun_Id, hypreal_of_real_zero,
+           hypreal_of_real_one]));
+qed "NSDERIV_Id";
+Addsimps [NSDERIV_Id];
+
+(*derivative of the identity function*)
+Goal "DERIV (%x. x) x :> #1";
+by (simp_tac (simpset() addsimps [NSDERIV_DERIV_iff RS sym]) 1);
+qed "DERIV_Id";
+Addsimps [DERIV_Id];
+
+bind_thm ("isCont_Id", DERIV_Id RS DERIV_isCont);
+
+(*derivative of linear multiplication*)
+Goal "DERIV (op * c) x :> c";
+by (cut_inst_tac [("c","c"),("x","x")] (DERIV_Id RS DERIV_cmult) 1);
+by (Asm_full_simp_tac 1);
+qed "DERIV_cmult_Id";
+Addsimps [DERIV_cmult_Id];
+
+Goal "NSDERIV (op * c) x :> c";
+by (simp_tac (simpset() addsimps [NSDERIV_DERIV_iff]) 1);
+qed "NSDERIV_cmult_Id";
+Addsimps [NSDERIV_cmult_Id];
+
+Goal "DERIV (%x. x ^ n) x :> real_of_nat n * (x ^ (n - 1))";
+by (induct_tac "n" 1);
+by (dtac (DERIV_Id RS DERIV_mult) 2);
+by (auto_tac (claset(),simpset() addsimps 
+    [real_add_mult_distrib]));
+by (case_tac "0 < n" 1);
+by (dres_inst_tac [("x","x")] realpow_minus_mult 1);
+by (auto_tac (claset(),simpset() addsimps 
+    [real_mult_assoc,real_add_commute]));
+qed "DERIV_pow";
+
+(* NS version *)
+Goal "NSDERIV (%x. x ^ n) x :> real_of_nat n * (x ^ (n - 1))";
+by (simp_tac (simpset() addsimps [NSDERIV_DERIV_iff,DERIV_pow]) 1);
+qed "NSDERIV_pow";
+
+(*---------------------------------------------------------------
+                    Power of -1 
+ ---------------------------------------------------------------*)
+
+(*Can't get rid of x ~= #0 because it isn't continuous at zero*)
+Goalw [nsderiv_def]
+     "x ~= #0 ==> NSDERIV (%x. inverse(x)) x :> (- (inverse x ^ 2))";
+by (rtac ballI 1 THEN Asm_full_simp_tac 1 THEN Step_tac 1);
+by (forward_tac [Infinitesimal_add_not_zero] 1);
+by (asm_full_simp_tac (simpset() addsimps [hypreal_add_commute]) 2); 
+by (auto_tac (claset(),
+     simpset() addsimps [starfun_inverse_inverse, realpow_two] 
+               delsimps [hypreal_minus_mult_eq1 RS sym,
+                         hypreal_minus_mult_eq2 RS sym]));
+by (asm_full_simp_tac
+     (simpset() addsimps [hypreal_inverse_add,
+          hypreal_inverse_distrib RS sym, hypreal_minus_inverse RS sym] 
+          @ hypreal_add_ac @ hypreal_mult_ac 
+       delsimps [hypreal_minus_mult_eq1 RS sym,
+                 hypreal_minus_mult_eq2 RS sym] ) 1);
+by (asm_simp_tac (simpset() addsimps [hypreal_mult_assoc RS sym,
+                                      hypreal_add_mult_distrib2] 
+         delsimps [hypreal_minus_mult_eq1 RS sym, 
+                   hypreal_minus_mult_eq2 RS sym]) 1);
+by (res_inst_tac [("y"," inverse(- hypreal_of_real x * hypreal_of_real x)")] 
+                 inf_close_trans 1);
+by (rtac inverse_add_Infinitesimal_inf_close2 1);
+by (auto_tac (claset() addSDs [hypreal_of_real_HFinite_diff_Infinitesimal], 
+         simpset() addsimps [hypreal_minus_inverse RS sym,
+                             HFinite_minus_iff]));
+by (rtac Infinitesimal_HFinite_mult2 1); 
+by Auto_tac;  
+qed "NSDERIV_inverse";
+
+
+Goal "x ~= #0 ==> DERIV (%x. inverse(x)) x :> (-(inverse x ^ 2))";
+by (asm_simp_tac (simpset() addsimps [NSDERIV_inverse,
+         NSDERIV_DERIV_iff RS sym] delsimps [realpow_Suc]) 1);
+qed "DERIV_inverse";
+
+(*--------------------------------------------------------------
+        Derivative of inverse 
+ -------------------------------------------------------------*)
+Goal "[| DERIV f x :> d; f(x) ~= #0 |] \
+\     ==> DERIV (%x. inverse(f x)) x :> (- (d * inverse(f(x) ^ 2)))";
+by (rtac (real_mult_commute RS subst) 1);
+by (asm_simp_tac (simpset() addsimps [real_minus_mult_eq1,
+    realpow_inverse] delsimps [realpow_Suc, 
+    real_minus_mult_eq1 RS sym]) 1);
+by (fold_goals_tac [o_def]);
+by (blast_tac (claset() addSIs [DERIV_chain,DERIV_inverse]) 1);
+qed "DERIV_inverse_fun";
+
+Goal "[| NSDERIV f x :> d; f(x) ~= #0 |] \
+\     ==> NSDERIV (%x. inverse(f x)) x :> (- (d * inverse(f(x) ^ 2)))";
+by (asm_full_simp_tac (simpset() addsimps [NSDERIV_DERIV_iff,
+            DERIV_inverse_fun] delsimps [realpow_Suc]) 1);
+qed "NSDERIV_inverse_fun";
+
+(*--------------------------------------------------------------
+        Derivative of quotient 
+ -------------------------------------------------------------*)
+Goal "[| DERIV f x :> d; DERIV g x :> e; g(x) ~= #0 |] \
+\      ==> DERIV (%y. f(y) / (g y)) x :> (d*g(x) + -(e*f(x))) / (g(x) ^ 2)";
+by (dres_inst_tac [("f","g")] DERIV_inverse_fun 1);
+by (dtac DERIV_mult 2);
+by (REPEAT(assume_tac 1));
+by (asm_full_simp_tac
+    (simpset() addsimps [real_divide_def, real_add_mult_distrib2,
+                         realpow_inverse,real_minus_mult_eq1] @ real_mult_ac 
+       delsimps [realpow_Suc, real_minus_mult_eq1 RS sym,
+                 real_minus_mult_eq2 RS sym]) 1);
+qed "DERIV_quotient";
+
+Goal "[| NSDERIV f x :> d; DERIV g x :> e; g(x) ~= #0 |] \
+\      ==> NSDERIV (%y. f(y) / (g y)) x :> (d*g(x) \
+\                           + -(e*f(x))) / (g(x) ^ 2)";
+by (asm_full_simp_tac (simpset() addsimps [NSDERIV_DERIV_iff,
+            DERIV_quotient] delsimps [realpow_Suc]) 1);
+qed "NSDERIV_quotient";
+ 
+(* ------------------------------------------------------------------------ *)
+(* Caratheodory formulation of derivative at a point: standard proof        *)
+(* ------------------------------------------------------------------------ *)
+
+Goal "(DERIV f x :> l) = \
+\     (EX g. (ALL z. f z - f x = g z * (z - x)) & isCont g x & g x = l)";
+by (Step_tac 1);
+by (res_inst_tac 
+    [("x","%z. if  z = x then l else (f(z) - f(x)) / (z - x)")] exI 1);
+by (auto_tac (claset(),simpset() addsimps [real_mult_assoc,
+    ARITH_PROVE "z ~= x ==> z - x ~= (#0::real)"]));
+by (auto_tac (claset(),simpset() addsimps [isCont_iff,DERIV_iff]));
+by (ALLGOALS(rtac (LIM_equal RS iffD1)));
+by (auto_tac (claset(),simpset() addsimps [real_diff_def,real_mult_assoc]));
+qed "CARAT_DERIV";
+
+Goal "NSDERIV f x :> l ==> \
+\     EX g. (ALL z. f z - f x = g z * (z - x)) & isNSCont g x & g x = l";
+by (auto_tac (claset(),simpset() addsimps [NSDERIV_DERIV_iff,
+    isNSCont_isCont_iff,CARAT_DERIV]));
+qed "CARAT_NSDERIV";
+
+(* How about a NS proof? *)
+Goal "(ALL z. f z - f x = g z * (z - x)) & isNSCont g x & g x = l \
+\     ==> NSDERIV f x :> l";
+by (auto_tac (claset(), 
+              simpset() delsimprocs real_cancel_factor
+                        addsimps [NSDERIV_iff2]));
+by (auto_tac (claset(),
+              simpset() addsimps [hypreal_mult_assoc]));
+by (asm_full_simp_tac (simpset() addsimps [hypreal_eq_minus_iff3 RS sym,
+                                           hypreal_diff_def]) 1);
+by (asm_full_simp_tac (simpset() addsimps [isNSCont_def]) 1);
+qed "CARAT_DERIVD";
+ 
+
+
+(*--------------------------------------------------------------------------*)
+(* Lemmas about nested intervals and proof by bisection (cf.Harrison)       *)
+(* All considerably tidied by lcp                                           *)
+(*--------------------------------------------------------------------------*)
+
+Goal "(ALL n. (f::nat=>real) n <= f (Suc n)) --> f m <= f(m + no)";
+by (induct_tac "no" 1);
+by (auto_tac (claset() addIs [order_trans], simpset()));
+qed_spec_mp "lemma_f_mono_add";
+
+Goal "[| ALL n. f(n) <= f(Suc n); \
+\        ALL n. g(Suc n) <= g(n); \
+\        ALL n. f(n) <= g(n) |] \
+\     ==> Bseq f";
+by (res_inst_tac [("k","f 0"),("K","g 0")] BseqI2 1 THEN rtac allI 1);
+by (induct_tac "n" 1);
+by (auto_tac (claset() addIs [order_trans], simpset()));
+by (res_inst_tac [("y","g(Suc na)")] order_trans 1);
+by (induct_tac "na" 2);
+by (auto_tac (claset() addIs [order_trans], simpset()));
+qed "f_inc_g_dec_Beq_f";
+
+Goal "[| ALL n. f(n) <= f(Suc n); \
+\        ALL n. g(Suc n) <= g(n); \
+\        ALL n. f(n) <= g(n) |] \
+\     ==> Bseq g";
+by (stac (Bseq_minus_iff RS sym) 1);
+by (res_inst_tac [("g","%x. -(f x)")] f_inc_g_dec_Beq_f 1); 
+by Auto_tac;  
+qed "f_inc_g_dec_Beq_g";
+
+Goal "[| ALL n. f n <= f (Suc n);  convergent f |] ==> f n <= lim f";
+by (rtac real_leI 1);
+by (auto_tac (claset(), 
+      simpset() addsimps [convergent_LIMSEQ_iff, LIMSEQ_iff, monoseq_Suc]));
+by (dtac real_less_sum_gt_zero 1);
+by (dres_inst_tac [("x","f n + - lim f")] spec 1);
+by Safe_tac;
+by (dres_inst_tac [("P","%na. no<=na --> ?Q na"),("x","no + n")] spec 2);
+by Auto_tac;
+by (subgoal_tac "lim f <= f(no + n)" 1);
+by (induct_tac "no" 2);
+by (auto_tac (claset() addIs [order_trans],
+              simpset() addsimps [real_diff_def, real_abs_def]));
+by (dres_inst_tac [("x","f(no + n)"),("no1","no")] 
+    (lemma_f_mono_add RSN (2,order_less_le_trans)) 1);
+by (auto_tac (claset(), simpset() addsimps [add_commute]));
+qed "f_inc_imp_le_lim";
+
+Goal "convergent g ==> lim (%x. - g x) = - (lim g)";
+by (rtac (LIMSEQ_minus RS limI) 1); 
+by (asm_full_simp_tac (simpset() addsimps [convergent_LIMSEQ_iff]) 1); 
+qed "lim_uminus";
+
+Goal "[| ALL n. g(Suc n) <= g(n);  convergent g |] ==> lim g <= g n";
+by (subgoal_tac "- (g n) <= - (lim g)" 1);
+by (cut_inst_tac [("f", "%x. - (g x)")] f_inc_imp_le_lim 2);
+by (auto_tac (claset(), 
+              simpset() addsimps [lim_uminus, convergent_minus_iff RS sym]));  
+qed "g_dec_imp_lim_le";
+
+Goal "[| ALL n. f(n) <= f(Suc n); \
+\        ALL n. g(Suc n) <= g(n); \
+\        ALL n. f(n) <= g(n) |] \
+\     ==> EX l m. l <= m &  ((ALL n. f(n) <= l) & f ----> l) & \
+\                           ((ALL n. m <= g(n)) & g ----> m)";
+by (subgoal_tac "monoseq f & monoseq g" 1);
+by (force_tac (claset(), simpset() addsimps [LIMSEQ_iff,monoseq_Suc]) 2);
+by (subgoal_tac "Bseq f & Bseq g" 1);
+by (blast_tac (claset() addIs [f_inc_g_dec_Beq_f, f_inc_g_dec_Beq_g]) 2); 
+by (auto_tac (claset() addSDs [Bseq_monoseq_convergent],
+              simpset() addsimps [convergent_LIMSEQ_iff]));
+by (res_inst_tac [("x","lim f")] exI 1);
+by (res_inst_tac [("x","lim g")] exI 1);
+by (auto_tac (claset() addIs [LIMSEQ_le], simpset()));
+by (auto_tac (claset(), 
+              simpset() addsimps [f_inc_imp_le_lim, g_dec_imp_lim_le, 
+                                  convergent_LIMSEQ_iff]));  
+qed "lemma_nest";
+
+Goal "[| ALL n. f(n) <= f(Suc n); \
+\        ALL n. g(Suc n) <= g(n); \
+\        ALL n. f(n) <= g(n); \
+\        (%n. f(n) - g(n)) ----> #0 |] \
+\     ==> EX l. ((ALL n. f(n) <= l) & f ----> l) & \
+\               ((ALL n. l <= g(n)) & g ----> l)";
+by (dtac lemma_nest 1 THEN Auto_tac);
+by (subgoal_tac "l = m" 1);
+by (dres_inst_tac [("X","f")] LIMSEQ_diff 2);
+by (auto_tac (claset() addIs [LIMSEQ_unique], simpset()));
+qed "lemma_nest_unique";
+
+
+Goal "a <= b ==> \
+\  ALL n. fst (Bolzano_bisect P a b n) <= snd (Bolzano_bisect P a b n)";
+by (rtac allI 1);
+by (induct_tac "n" 1);
+by (auto_tac (claset(), simpset() addsimps [Let_def, split_def]));  
+qed "Bolzano_bisect_le";
+
+Goal "a <= b ==> \
+\  ALL n. fst(Bolzano_bisect P a b n) <= fst (Bolzano_bisect P a b (Suc n))";
+by (rtac allI 1);
+by (induct_tac "n" 1);
+by (auto_tac (claset(), 
+              simpset() addsimps [Bolzano_bisect_le, Let_def, split_def]));  
+qed "Bolzano_bisect_fst_le_Suc";
+
+Goal "a <= b ==> \
+\  ALL n. snd(Bolzano_bisect P a b (Suc n)) <= snd (Bolzano_bisect P a b n)";
+by (rtac allI 1);
+by (induct_tac "n" 1);
+by (auto_tac (claset(), 
+              simpset() addsimps [Bolzano_bisect_le, Let_def, split_def]));  
+qed "Bolzano_bisect_Suc_le_snd";
+
+Goal "((x::real) = y / (#2 * z)) = (#2 * x = y/z)";
+by Auto_tac;  
+by (dres_inst_tac [("f","%u. (#1/#2)*u")] arg_cong 1); 
+by Auto_tac;  
+qed "eq_divide_2_times_iff";
+
+Goal "a <= b ==> \
+\     snd(Bolzano_bisect P a b n) - fst(Bolzano_bisect P a b n) = \
+\     (b-a) / (#2 ^ n)";
+by (induct_tac "n" 1);
+by (auto_tac (claset(), 
+      simpset() addsimps [eq_divide_2_times_iff, real_add_divide_distrib, 
+                          Let_def, split_def]));
+by (auto_tac (claset(), 
+              simpset() addsimps (real_add_ac@[Bolzano_bisect_le, real_diff_def])));
+qed "Bolzano_bisect_diff";
+
+val Bolzano_nest_unique =
+    [Bolzano_bisect_fst_le_Suc, Bolzano_bisect_Suc_le_snd, Bolzano_bisect_le] 
+    MRS lemma_nest_unique;
+
+(*P_prem is a looping simprule, so it works better if it isn't an assumption*)
+val P_prem::notP_prem::rest =
+Goal "[| !!a b c. [| P(a,b); P(b,c); a <= b; b <= c|] ==> P(a,c); \
+\        ~ P(a,b);  a <= b |] ==> \
+\     ~ P(fst(Bolzano_bisect P a b n), snd(Bolzano_bisect P a b n))";
+by (cut_facts_tac rest 1);
+by (induct_tac "n" 1);
+by (auto_tac (claset(), 
+              simpset() delsimps [surjective_pairing RS sym]
+			addsimps [notP_prem, Let_def, split_def]));  
+by (swap_res_tac [P_prem] 1);
+by (assume_tac 1); 
+by (auto_tac (claset(), simpset() addsimps [Bolzano_bisect_le]));  
+qed "not_P_Bolzano_bisect";
+
+(*Now we re-package P_prem as a formula*)
+Goal "[| ALL a b c. P(a,b) & P(b,c) & a <= b & b <= c --> P(a,c); \
+\        ~ P(a,b);  a <= b |] ==> \
+\     ALL n. ~ P(fst(Bolzano_bisect P a b n), snd(Bolzano_bisect P a b n))";
+by (blast_tac (claset() addSEs [not_P_Bolzano_bisect RSN (2,rev_notE)]) 1); 
+qed "not_P_Bolzano_bisect'";
+
+
+Goal "[| ALL a b c. P(a,b) & P(b,c) & a <= b & b <= c --> P(a,c); \
+\        ALL x. EX d::real. #0 < d & \
+\               (ALL a b. a <= x & x <= b & (b - a) < d --> P(a,b)); \
+\        a <= b |]  \
+\     ==> P(a,b)";
+by (rtac (inst "P1" "P" Bolzano_nest_unique RS exE) 1);
+by (REPEAT (assume_tac 1));
+by (rtac LIMSEQ_minus_cancel 1);
+by (asm_simp_tac (simpset() addsimps [Bolzano_bisect_diff,
+                                      LIMSEQ_divide_realpow_zero]) 1); 
+by (rtac ccontr 1);
+by (dtac not_P_Bolzano_bisect' 1); 
+by (REPEAT (assume_tac 1));
+by (rename_tac "l" 1);
+by (dres_inst_tac [("x","l")] spec 1 THEN Clarify_tac 1);
+by (rewtac LIMSEQ_def);
+by (dres_inst_tac [("P", "%r. #0<r --> ?Q r"), ("x","d/#2")] spec 1);
+by (dres_inst_tac [("P", "%r. #0<r --> ?Q r"), ("x","d/#2")] spec 1);
+by (dtac real_less_half_sum 1);
+by Safe_tac;
+(*linear arithmetic bug if we just use Asm_simp_tac*)
+by (ALLGOALS Asm_full_simp_tac);
+by (dres_inst_tac [("x","fst(Bolzano_bisect P a b (no + noa))")] spec 1);
+by (dres_inst_tac [("x","snd(Bolzano_bisect P a b (no + noa))")] spec 1);
+by Safe_tac;
+by (ALLGOALS Asm_simp_tac);
+by (res_inst_tac [("y","abs(fst(Bolzano_bisect P a b(no + noa)) - l) + \
+\                       abs(snd(Bolzano_bisect P a b(no + noa)) - l)")] 
+    order_le_less_trans 1);
+by (asm_simp_tac (simpset() addsimps [real_abs_def]) 1);  
+by (rtac (real_sum_of_halves RS subst) 1);
+by (rtac real_add_less_mono 1);
+by (ALLGOALS 
+    (asm_full_simp_tac (simpset() addsimps [symmetric real_diff_def])));
+qed "lemma_BOLZANO";
+
+
+Goal "((ALL a b c. (a <= b & b <= c & P(a,b) & P(b,c)) --> P(a,c)) & \
+\      (ALL x. EX d::real. #0 < d & \
+\               (ALL a b. a <= x & x <= b & (b - a) < d --> P(a,b)))) \
+\     --> (ALL a b. a <= b --> P(a,b))";
+by (Clarify_tac 1);
+by (blast_tac (claset() addIs [lemma_BOLZANO]) 1); 
+qed "lemma_BOLZANO2";
+
+
+(*----------------------------------------------------------------------------*)
+(* Intermediate Value Theorem (prove contrapositive by bisection)             *)
+(*----------------------------------------------------------------------------*)
+
+Goal "[| f(a) <= y & y <= f(b); \
+\        a <= b; \
+\        (ALL x. a <= x & x <= b --> isCont f x) |] \
+\     ==> EX x. a <= x & x <= b & f(x) = y";
+by (rtac contrapos_pp 1);
+by (assume_tac 1);
+by (cut_inst_tac
+    [("P","%(u,v). a <= u & u <= v & v <= b --> ~(f(u) <= y & y <= f(v))")] 
+    lemma_BOLZANO2 1);
+by (Step_tac 1);
+by (ALLGOALS(Asm_full_simp_tac));
+by (Blast_tac 2);
+by (asm_full_simp_tac (simpset() addsimps [isCont_iff,LIM_def]) 1);
+by (rtac ccontr 1);
+by (subgoal_tac "a <= x & x <= b" 1);
+by (Asm_full_simp_tac 2);
+by (dres_inst_tac [("P", "%d. #0<d --> ?P d"),("x","#1")] spec 2);
+by (Step_tac 2);
+by (Asm_full_simp_tac 2);
+by (Asm_full_simp_tac 2);
+by (REPEAT(blast_tac (claset() addIs [order_trans]) 2));
+by (REPEAT(dres_inst_tac [("x","x")] spec 1));
+by (Asm_full_simp_tac 1);
+by (dres_inst_tac [("P", "%r. ?P r --> (EX s. #0<s & ?Q r s)"),
+                   ("x","abs(y - f x)")] spec 1);
+by (Step_tac 1);
+by (asm_full_simp_tac (simpset() addsimps []) 1);
+by (dres_inst_tac [("x","s")] spec 1);
+by (Clarify_tac 1);
+by (cut_inst_tac [("R1.0","f x"),("R2.0","y")] real_linear 1);
+by Safe_tac;
+by (dres_inst_tac [("x","ba - x")] spec 1);
+by (ALLGOALS (asm_full_simp_tac (simpset() addsimps [abs_iff])));
+by (dres_inst_tac [("x","aa - x")] spec 1);
+by (case_tac "x <= aa" 1);
+by (ALLGOALS Asm_full_simp_tac);
+by (dres_inst_tac [("z","x"),("w","aa")] real_le_anti_sym 1);
+by (assume_tac 1 THEN Asm_full_simp_tac 1);
+qed "IVT";
+
+
+Goal "[| f(b) <= y & y <= f(a); \
+\        a <= b; \
+\        (ALL x. a <= x & x <= b --> isCont f x) \
+\     |] ==> EX x. a <= x & x <= b & f(x) = y";
+by (subgoal_tac "- f a <= -y & -y <= - f b" 1);
+by (thin_tac "f b <= y & y <= f a" 1);
+by (dres_inst_tac [("f","%x. - f x")] IVT 1);
+by (auto_tac (claset() addIs [isCont_minus],simpset()));
+qed "IVT2";
+
+
+(*HOL style here: object-level formulations*)
+Goal "(f(a) <= y & y <= f(b) & a <= b & \
+\     (ALL x. a <= x & x <= b --> isCont f x)) \
+\     --> (EX x. a <= x & x <= b & f(x) = y)";
+by (blast_tac (claset() addIs [IVT]) 1);
+qed "IVT_objl";
+
+Goal "(f(b) <= y & y <= f(a) & a <= b & \
+\     (ALL x. a <= x & x <= b --> isCont f x)) \
+\     --> (EX x. a <= x & x <= b & f(x) = y)";
+by (blast_tac (claset() addIs [IVT2]) 1);
+qed "IVT2_objl";
+
+(*---------------------------------------------------------------------------*)
+(* By bisection, function continuous on closed interval is bounded above     *)
+(*---------------------------------------------------------------------------*)
+
+Goal "abs (real_of_nat x) = real_of_nat x";
+by (auto_tac (claset() addIs [abs_eqI1],simpset()));
+qed "abs_real_of_nat_cancel";
+Addsimps [abs_real_of_nat_cancel];
+
+Goal "~ abs(x) + 1r < x";
+by (rtac real_leD 1);
+by (auto_tac (claset() addIs [abs_ge_self RS order_trans],simpset()));
+qed "abs_add_one_not_less_self";
+Addsimps [abs_add_one_not_less_self];
+
+
+Goal "[| a <= b; ALL x. a <= x & x <= b --> isCont f x |]\
+\     ==> EX M. ALL x. a <= x & x <= b --> f(x) <= M";
+by (cut_inst_tac [("P","%(u,v). a <= u & u <= v & v <= b --> \
+\                         (EX M. ALL x. u <= x & x <= v --> f x <= M)")] 
+    lemma_BOLZANO2 1);
+by (Step_tac 1);
+by (ALLGOALS(Asm_full_simp_tac));
+by (cut_inst_tac [("x","M"),("y","Ma")] 
+    (CLAIM "x <= y | y <= (x::real)") 1);
+by (Step_tac 1);
+by (res_inst_tac [("x","Ma")] exI 1);
+by (Step_tac 1);
+by (cut_inst_tac [("x","xb"),("y","xa")] 
+    (CLAIM "x <= y | y <= (x::real)") 1);
+by (Step_tac 1);
+by (rtac order_trans 1 THEN assume_tac 2);
+by (Asm_full_simp_tac 1);
+by (Asm_full_simp_tac 1);
+by (res_inst_tac [("x","M")] exI 1);
+by (Step_tac 1);
+by (cut_inst_tac [("x","xb"),("y","xa")] 
+    (CLAIM "x <= y | y <= (x::real)") 1);
+by (Step_tac 1);
+by (Asm_full_simp_tac 1);
+by (rtac order_trans 1 THEN assume_tac 2);
+by (Asm_full_simp_tac 1);
+by (case_tac "a <= x & x <= b" 1);
+by (res_inst_tac [("x","#1")] exI 2);
+by (auto_tac (claset(),
+              simpset() addsimps [LIM_def,isCont_iff]));
+by (dres_inst_tac [("x","x")] spec 1 THEN Auto_tac);
+by (thin_tac "ALL M. EX x. a <= x & x <= b & ~ f x <= M" 1);
+by (dres_inst_tac [("x","#1")] spec 1);
+by Auto_tac;  
+by (res_inst_tac [("x","s")] exI 1 THEN Auto_tac);
+by (res_inst_tac [("x","abs(f x) + #1")] exI 1 THEN Step_tac 1);
+by (dres_inst_tac [("x","xa - x")] spec 1 THEN Auto_tac);
+by (res_inst_tac [("y","abs(f xa)")] order_trans 3);
+by (res_inst_tac [("y","abs(f x) + abs(f(xa) - f(x))")] order_trans 4);
+by (auto_tac (claset() addIs [abs_triangle_ineq RSN (2, order_trans)],
+              simpset() addsimps [real_diff_def,abs_ge_self]));
+by (auto_tac (claset(),
+              simpset() addsimps [real_abs_def] addsplits [split_if_asm]));
+qed "isCont_bounded";
+
+(*----------------------------------------------------------------------------*)
+(* Refine the above to existence of least upper bound                         *)
+(*----------------------------------------------------------------------------*)
+
+Goal "((EX x. x : S) & (EX y. isUb UNIV S (y::real))) --> \
+\     (EX t. isLub UNIV S t)";
+by (blast_tac (claset() addIs [reals_complete]) 1);
+qed "lemma_reals_complete";
+
+Goal "[| a <= b; ALL x. a <= x & x <= b --> isCont f x |] \
+\        ==> EX M. (ALL x. a <= x & x <= b --> f(x) <= M) & \
+\                  (ALL N. N < M --> (EX x. a <= x & x <= b & N < f(x)))";
+by (cut_inst_tac [("S","Collect (%y. EX x. a <= x & x <= b & y = f x)")]
+    lemma_reals_complete 1);
+by Auto_tac;
+by (dtac isCont_bounded 1 THEN assume_tac 1);
+by (auto_tac (claset(),simpset() addsimps [isUb_def,leastP_def,
+    isLub_def,setge_def,setle_def]));
+by (rtac exI 1 THEN Auto_tac);
+by (REPEAT(dtac spec 1) THEN Auto_tac);
+by (dres_inst_tac [("x","x")] spec 1);
+by (auto_tac (claset() addSIs [real_leI],simpset()));
+qed "isCont_has_Ub";
+
+(*----------------------------------------------------------------------------*)
+(* Now show that it attains its upper bound                                   *)
+(*----------------------------------------------------------------------------*)
+
+Goal "[| a <= b; ALL x. a <= x & x <= b --> isCont f x |] \
+\        ==> EX M. (ALL x. a <= x & x <= b --> f(x) <= M) & \
+\                  (EX x. a <= x & x <= b & f(x) = M)";
+by (ftac isCont_has_Ub 1 THEN assume_tac 1);
+by (Clarify_tac 1);
+by (res_inst_tac [("x","M")] exI 1);
+by (Asm_full_simp_tac 1); 
+by (rtac ccontr 1);
+by (subgoal_tac "ALL x. a <= x & x <= b --> f x < M" 1 THEN Step_tac 1);
+by (rtac ccontr 2 THEN dtac real_leI 2);
+by (dres_inst_tac [("z","M")] real_le_anti_sym 2);
+by (REPEAT(Blast_tac 2));
+by (subgoal_tac "ALL x. a <= x & x <= b --> isCont (%x. inverse(M - f x)) x" 1);
+by (Step_tac 1);
+by (EVERY[rtac isCont_inverse 2, rtac isCont_diff 2, rtac notI 4]);
+by (ALLGOALS(asm_full_simp_tac (simpset() addsimps [real_diff_eq_eq])));
+by (Blast_tac 2);
+by (subgoal_tac 
+    "EX k. ALL x. a <= x & x <= b --> (%x. inverse(M - (f x))) x <= k" 1);
+by (rtac isCont_bounded 2);
+by (Step_tac 1);
+by (subgoal_tac "ALL x. a <= x & x <= b --> #0 < inverse(M - f(x))" 1);
+by (Asm_full_simp_tac 1); 
+by (Step_tac 1);
+by (asm_full_simp_tac (simpset() addsimps [real_less_diff_eq]) 2);
+by (subgoal_tac 
+    "ALL x. a <= x & x <= b --> (%x. inverse(M - (f x))) x < (k + #1)" 1);
+by (Step_tac 1);
+by (res_inst_tac [("y","k")] order_le_less_trans 2);
+by (asm_full_simp_tac (simpset() addsimps [real_zero_less_one]) 3);
+by (Asm_full_simp_tac 2); 
+by (subgoal_tac "ALL x. a <= x & x <= b --> \
+\                inverse(k + #1) < inverse((%x. inverse(M - (f x))) x)" 1);
+by (Step_tac 1);
+by (rtac real_inverse_less_swap 2);
+by (ALLGOALS Asm_full_simp_tac);
+by (dres_inst_tac [("P", "%N. N<M --> ?Q N"),
+                   ("x","M - inverse(k + #1)")] spec 1);
+by (Step_tac 1 THEN dtac real_leI 1);
+by (dtac (real_le_diff_eq RS iffD1) 1);
+by (REPEAT(dres_inst_tac [("x","a")] spec 1));
+by (Asm_full_simp_tac 1);
+by (asm_full_simp_tac 
+    (simpset() addsimps [real_inverse_eq_divide, pos_real_divide_le_eq]) 1); 
+by (cut_inst_tac [("x","k"),("y","M-f a")] real_0_less_mult_iff 1);
+by (Asm_full_simp_tac 1); 
+(*last one*)
+by (REPEAT(dres_inst_tac [("x","x")] spec 1));
+by (Asm_full_simp_tac 1); 
+qed "isCont_eq_Ub";
+
+
+(*----------------------------------------------------------------------------*)
+(* Same theorem for lower bound                                               *)
+(*----------------------------------------------------------------------------*)
+
+Goal "[| a <= b; ALL x. a <= x & x <= b --> isCont f x |] \
+\        ==> EX M. (ALL x. a <= x & x <= b --> M <= f(x)) & \
+\                  (EX x. a <= x & x <= b & f(x) = M)";
+by (subgoal_tac "ALL x. a <= x & x <= b --> isCont (%x. -(f x)) x" 1);
+by (blast_tac (claset() addIs [isCont_minus]) 2);
+by (dres_inst_tac [("f","(%x. -(f x))")] isCont_eq_Ub 1);
+by (Step_tac 1);
+by Auto_tac;
+qed "isCont_eq_Lb";
+
+
+(* ------------------------------------------------------------------------- *)
+(* Another version.                                                          *)
+(* ------------------------------------------------------------------------- *)
+
+Goal "[|a <= b; ALL x. a <= x & x <= b --> isCont f x |] \
+\     ==> EX L M. (ALL x. a <= x & x <= b --> L <= f(x) & f(x) <= M) & \
+\         (ALL y. L <= y & y <= M --> (EX x. a <= x & x <= b & (f(x) = y)))";
+by (ftac isCont_eq_Lb 1);
+by (ftac isCont_eq_Ub 2);
+by (REPEAT(assume_tac 1));
+by (Step_tac 1);
+by (res_inst_tac [("x","f x")] exI 1);
+by (res_inst_tac [("x","f xa")] exI 1);
+by (Asm_full_simp_tac 1);
+by (Step_tac 1);
+by (cut_inst_tac [("x","x"),("y","xa")] (CLAIM "x <= y | y <= (x::real)") 1);
+by (Step_tac 1);
+by (cut_inst_tac [("f","f"),("a","x"),("b","xa"),("y","y")] IVT_objl 1);
+by (cut_inst_tac [("f","f"),("a","xa"),("b","x"),("y","y")] IVT2_objl 2);
+by (Step_tac 1);
+by (res_inst_tac [("x","xb")] exI 2);
+by (res_inst_tac [("x","xb")] exI 4);
+by (ALLGOALS(Asm_full_simp_tac));
+qed "isCont_Lb_Ub";
+
+(*----------------------------------------------------------------------------*)
+(* If f'(x) > 0 then x is locally strictly increasing at the right            *)
+(*----------------------------------------------------------------------------*)
+
+Goalw [deriv_def,LIM_def] 
+    "[| DERIV f x :> l;  #0 < l |] ==> \
+\      EX d. #0 < d & (ALL h. #0 < h & h < d --> f(x) < f(x + h))";
+by (dtac spec 1 THEN Auto_tac);
+by (res_inst_tac [("x","s")] exI 1 THEN Auto_tac);
+by (subgoal_tac "#0 < l*h" 1);
+by (asm_full_simp_tac (simpset() addsimps [real_0_less_mult_iff]) 2); 
+by (dres_inst_tac [("x","h")] spec 1);
+by (asm_full_simp_tac
+    (simpset() addsimps [real_abs_def, real_inverse_eq_divide, 
+                 pos_real_le_divide_eq, pos_real_less_divide_eq]
+              addsplits [split_if_asm]) 1); 
+qed "DERIV_left_inc";
+
+Goalw [deriv_def,LIM_def] 
+    "[| DERIV f x :> l;  l < #0 |] ==> \
+\      EX d. #0 < d & (ALL h. #0 < h & h < d --> f(x) < f(x - h))";
+by (dres_inst_tac [("x","-l")] spec 1 THEN Auto_tac);
+by (res_inst_tac [("x","s")] exI 1 THEN Auto_tac);
+by (subgoal_tac "l*h < #0" 1);
+by (asm_full_simp_tac (simpset() addsimps [real_mult_less_0_iff]) 2); 
+by (dres_inst_tac [("x","-h")] spec 1);
+by (asm_full_simp_tac
+    (simpset() addsimps [real_abs_def, real_inverse_eq_divide, 
+                         pos_real_less_divide_eq,
+                         symmetric real_diff_def]
+               addsplits [split_if_asm]) 1);
+by (subgoal_tac "#0 < (f (x - h) - f x)/h" 1);
+by (arith_tac 2);
+by (asm_full_simp_tac
+    (simpset() addsimps [pos_real_less_divide_eq]) 1); 
+qed "DERIV_left_dec";
+
+
+Goal "[| DERIV f x :> l; \
+\        EX d. #0 < d & (ALL y. abs(x - y) < d --> f(y) <= f(x)) |] \
+\     ==> l = #0";
+by (res_inst_tac [("R1.0","l"),("R2.0","#0")] real_linear_less2 1);
+by (Step_tac 1);
+by (dtac DERIV_left_dec 1);
+by (dtac DERIV_left_inc 3);
+by (Step_tac 1);
+by (dres_inst_tac [("d1.0","d"),("d2.0","da")] real_lbound_gt_zero 1);
+by (dres_inst_tac [("d1.0","d"),("d2.0","da")] real_lbound_gt_zero 3);
+by (Step_tac 1);
+by (dres_inst_tac [("x","x - e")] spec 1);
+by (dres_inst_tac [("x","x + e")] spec 2);
+by (auto_tac (claset(), simpset() addsimps [real_abs_def]));
+qed "DERIV_local_max";
+
+(*----------------------------------------------------------------------------*)
+(* Similar theorem for a local minimum                                        *)
+(*----------------------------------------------------------------------------*)
+
+Goal "[| DERIV f x :> l; \
+\        EX d::real. #0 < d & (ALL y. abs(x - y) < d --> f(x) <= f(y)) |] \
+\     ==> l = #0";
+by (dtac (DERIV_minus RS DERIV_local_max) 1); 
+by Auto_tac;  
+qed "DERIV_local_min";
+
+(*----------------------------------------------------------------------------*)
+(* In particular if a function is locally flat                                *)
+(*----------------------------------------------------------------------------*)
+
+Goal "[| DERIV f x :> l; \
+\        EX d. #0 < d & (ALL y. abs(x - y) < d --> f(x) = f(y)) |] \
+\     ==> l = #0";
+by (auto_tac (claset() addSDs [DERIV_local_max],simpset()));
+qed "DERIV_local_const";
+
+(*----------------------------------------------------------------------------*)
+(* Lemma about introducing open ball in open interval                         *)
+(*----------------------------------------------------------------------------*)
+
+Goal "[| a < x;  x < b |] ==> \
+\       EX d::real. #0 < d &  (ALL y. abs(x - y) < d --> a < y & y < b)";
+by (simp_tac (simpset() addsimps [abs_interval_iff]) 1);
+by (cut_inst_tac [("x","x - a"),("y","b - x")] 
+    (CLAIM "x <= y | y <= (x::real)") 1);
+by (Step_tac 1);
+by (res_inst_tac [("x","x - a")] exI 1);
+by (res_inst_tac [("x","b - x")] exI 2);
+by Auto_tac;
+by (auto_tac (claset(),simpset() addsimps [real_less_diff_eq]));
+qed "lemma_interval_lt";
+
+Goal "[| a < x;  x < b |] ==> \
+\       EX d::real. #0 < d &  (ALL y. abs(x - y) < d --> a <= y & y <= b)";
+by (dtac lemma_interval_lt 1);
+by Auto_tac;
+by (auto_tac (claset() addSIs [exI] ,simpset()));
+qed "lemma_interval";
+
+(*-----------------------------------------------------------------------
+            Rolle's Theorem
+   If f is defined and continuous on the finite closed interval [a,b]
+   and differentiable a least on the open interval (a,b), and f(a) = f(b),
+   then x0 : (a,b) such that f'(x0) = #0
+ ----------------------------------------------------------------------*)
+
+Goal "[| a < b; f(a) = f(b); \
+\        ALL x. a <= x & x <= b --> isCont f x; \
+\        ALL x. a < x & x < b --> f differentiable x \
+\     |] ==> EX z. a < z & z < b & DERIV f z :> #0";
+by (ftac (order_less_imp_le RS isCont_eq_Ub) 1);
+by (EVERY1[assume_tac,Step_tac]);
+by (ftac (order_less_imp_le RS isCont_eq_Lb) 1);
+by (EVERY1[assume_tac,Step_tac]);
+by (case_tac "a < x & x < b" 1 THEN etac conjE 1);
+by (Asm_full_simp_tac 2);
+by (forw_inst_tac [("a","a"),("x","x")] lemma_interval 1);
+by (EVERY1[assume_tac,etac exE]);
+by (res_inst_tac [("x","x")] exI 1 THEN Asm_full_simp_tac 1);
+by (subgoal_tac "(EX l. DERIV f x :> l) & \
+\        (EX d. #0 < d & (ALL y. abs(x - y) < d --> f(y) <= f(x)))" 1);
+by (Clarify_tac 1 THEN rtac conjI 2);
+by (blast_tac (claset() addIs [differentiableD]) 2);
+by (Blast_tac 2);
+by (ftac DERIV_local_max 1);
+by (EVERY1[Blast_tac,Blast_tac]);
+by (case_tac "a < xa & xa < b" 1 THEN etac conjE 1);
+by (Asm_full_simp_tac 2);
+by (forw_inst_tac [("a","a"),("x","xa")] lemma_interval 1);
+by (EVERY1[assume_tac,etac exE]);
+by (res_inst_tac [("x","xa")] exI 1 THEN Asm_full_simp_tac 1);
+by (subgoal_tac "(EX l. DERIV f xa :> l) & \
+\        (EX d. #0 < d & (ALL y. abs(xa - y) < d --> f(xa) <= f(y)))" 1);
+by (Clarify_tac 1 THEN rtac conjI 2);
+by (blast_tac (claset() addIs [differentiableD]) 2);
+by (Blast_tac 2);
+by (ftac DERIV_local_min 1);
+by (EVERY1[Blast_tac,Blast_tac]);
+by (subgoal_tac "ALL x. a <= x & x <= b --> f(x) = f(b)" 1);
+by (Clarify_tac 2);
+by (rtac real_le_anti_sym 2);
+by (subgoal_tac "f b = f x" 2);
+by (Asm_full_simp_tac 2);
+by (res_inst_tac [("x1","a"),("y1","x")] (order_le_imp_less_or_eq RS disjE) 2);
+by (assume_tac 2);
+by (dres_inst_tac [("z","x"),("w","b")] real_le_anti_sym 2);
+by (subgoal_tac "f b = f xa" 5);
+by (Asm_full_simp_tac 5);
+by (res_inst_tac [("x1","a"),("y1","xa")] (order_le_imp_less_or_eq RS disjE) 5);
+by (assume_tac 5);
+by (dres_inst_tac [("z","xa"),("w","b")] real_le_anti_sym 5);
+by (REPEAT(Asm_full_simp_tac 2));
+by (dtac real_dense 1 THEN etac exE 1);
+by (res_inst_tac [("x","r")] exI 1 THEN Asm_full_simp_tac 1);
+by (etac conjE 1);
+by (forw_inst_tac [("a","a"),("x","r")] lemma_interval 1);
+by (EVERY1[assume_tac, etac exE]);
+by (subgoal_tac "(EX l. DERIV f r :> l) & \
+\        (EX d. #0 < d & (ALL y. abs(r - y) < d --> f(r) = f(y)))" 1);
+by (Clarify_tac 1 THEN rtac conjI 2);
+by (blast_tac (claset() addIs [differentiableD]) 2);
+by (EVERY1[ftac DERIV_local_const, Blast_tac, Blast_tac]);
+by (res_inst_tac [("x","d")] exI 1);
+by (EVERY1[rtac conjI, Blast_tac, rtac allI, rtac impI]);
+by (res_inst_tac [("s","f b")] trans 1);
+by (blast_tac (claset() addSDs [order_less_imp_le]) 1);
+by (rtac sym 1 THEN Blast_tac 1);
+qed "Rolle";
+
+(*----------------------------------------------------------------------------*)
+(* Mean value theorem                                                         *)
+(*----------------------------------------------------------------------------*)
+
+Goal "f a - (f b - f a)/(b - a) * a = \
+\     f b - (f b - f a)/(b - a) * (b::real)";
+by (case_tac "a = b" 1);
+by (asm_full_simp_tac (simpset() addsimps [REAL_DIVIDE_ZERO]) 1); 
+by (res_inst_tac [("c1","b - a")] (real_mult_left_cancel RS iffD1) 1);
+by (arith_tac 1);
+by (auto_tac (claset(),
+              simpset() addsimps [real_diff_mult_distrib2]));
+by (auto_tac (claset(),
+           simpset() addsimps [real_diff_mult_distrib]));
+qed "lemma_MVT";
+
+Goal "[| a < b; \
+\        ALL x. a <= x & x <= b --> isCont f x; \
+\        ALL x. a < x & x < b --> f differentiable x |] \
+\     ==>  EX l z. a < z & z < b & DERIV f z :> l & \
+\                  (f(b) - f(a) = (b - a) * l)";
+by (dres_inst_tac [("f","%x. f(x) - (((f(b) - f(a)) / (b - a)) * x)")]
+    Rolle 1);
+by (rtac lemma_MVT 1);
+by (Step_tac 1);
+by (rtac isCont_diff 1 THEN Blast_tac 1);
+by (rtac (isCont_const RS isCont_mult) 1);
+by (rtac isCont_Id 1); 
+by (dres_inst_tac [("P", "%x. ?Pre x --> f differentiable x"), 
+                   ("x","x")] spec 1);
+by (asm_full_simp_tac (simpset() addsimps [differentiable_def]) 1);
+by (Step_tac 1);
+by (res_inst_tac [("x","xa - ((f(b) - f(a)) / (b - a))")] exI 1);
+by (rtac DERIV_diff 1 THEN assume_tac 1);
+(*derivative of a linear function is the constant...*)
+by (subgoal_tac "(%x. (f b - f a) * x / (b - a)) = \
+\                op * ((f b - f a) / (b - a))" 1);
+by (rtac ext 2 THEN Simp_tac 2);
+by (Asm_full_simp_tac 1); 
+(*final case*)
+by (res_inst_tac [("x","((f(b) - f(a)) / (b - a))")] exI 1);
+by (res_inst_tac [("x","z")] exI 1);
+by (Step_tac 1);
+by (Asm_full_simp_tac 2); 
+by (subgoal_tac "DERIV (%x. ((f(b) - f(a)) / (b - a)) * x) z :> \
+\                           ((f(b) - f(a)) / (b - a))" 1);
+by (rtac DERIV_cmult_Id 2); 
+by (dtac DERIV_add 1 THEN assume_tac 1);
+by (asm_full_simp_tac (simpset() addsimps [real_add_assoc, real_diff_def]) 1);
+qed "MVT";
+
+(*----------------------------------------------------------------------------*)
+(* Theorem that function is constant if its derivative is 0 over an interval. *)
+(*----------------------------------------------------------------------------*)
+
+Goal "[| a < b; \
+\        ALL x. a <= x & x <= b --> isCont f x; \
+\        ALL x. a < x & x < b --> DERIV f x :> #0 |] \
+\       ==> (f b = f a)";
+by (dtac MVT 1 THEN assume_tac 1);
+by (blast_tac (claset() addIs [differentiableI]) 1);
+by (auto_tac (claset() addSDs [DERIV_unique],simpset() 
+    addsimps [real_diff_eq_eq]));
+qed "DERIV_isconst_end";
+
+Goal "[| a < b; \
+\        ALL x. a <= x & x <= b --> isCont f x; \
+\        ALL x. a < x & x < b --> DERIV f x :> #0 |] \
+\       ==> ALL x. a <= x & x <= b --> f x = f a";
+by (Step_tac 1);
+by (dres_inst_tac [("x","a")] order_le_imp_less_or_eq 1);
+by (Step_tac 1);
+by (dres_inst_tac [("b","x")] DERIV_isconst_end 1);
+by Auto_tac;
+qed "DERIV_isconst1";
+
+Goal "[| a < b; \
+\        ALL x. a <= x & x <= b --> isCont f x; \
+\        ALL x. a < x & x < b --> DERIV f x :> #0; \
+\        a <= x; x <= b |] \
+\       ==> f x = f a";
+by (blast_tac (claset() addDs [DERIV_isconst1]) 1);
+qed "DERIV_isconst2";
+
+Goal "ALL x. DERIV f x :> #0 ==> f(x) = f(y)";
+by (res_inst_tac [("R1.0","x"),("R2.0","y")] real_linear_less2 1);
+by (rtac sym 1);
+by (auto_tac (claset() addIs [DERIV_isCont,DERIV_isconst_end],simpset()));
+qed "DERIV_isconst_all";
+
+Goal "[|a ~= b; ALL x. DERIV f x :> k |] ==> (f(b) - f(a)) = (b - a) * k";
+by (res_inst_tac [("R1.0","a"),("R2.0","b")] real_linear_less2 1);
+by Auto_tac;
+by (ALLGOALS(dres_inst_tac [("f","f")] MVT));
+by (auto_tac (claset() addDs [DERIV_isCont,DERIV_unique],simpset() addsimps 
+    [differentiable_def]));
+by (auto_tac (claset() addDs [DERIV_unique],
+       simpset() addsimps [real_add_mult_distrib, real_diff_def,
+                           real_minus_mult_eq1 RS sym]));
+qed "DERIV_const_ratio_const";
+
+Goal "[|a ~= b; ALL x. DERIV f x :> k |] ==> (f(b) - f(a))/(b - a) = k";
+by (res_inst_tac [("c1","b - a")] (real_mult_right_cancel RS iffD1) 1);
+by (auto_tac (claset() addSDs [DERIV_const_ratio_const], 
+              simpset() addsimps [real_mult_assoc]));
+qed "DERIV_const_ratio_const2";
+
+Goal "((a + b) /#2 - a) = (b - a)/(#2::real)";
+by Auto_tac;  
+qed "real_average_minus_first";
+Addsimps [real_average_minus_first];
+
+Goal "((b + a)/#2 - a) = (b - a)/(#2::real)";
+by Auto_tac;  
+qed "real_average_minus_second";
+Addsimps [real_average_minus_second];
+
+
+(* Gallileo's "trick": average velocity = av. of end velocities *)
+Goal "[|a ~= (b::real); ALL x. DERIV v x :> k|] \
+\     ==> v((a + b)/#2) = (v a + v b)/#2";
+by (res_inst_tac [("R1.0","a"),("R2.0","b")] real_linear_less2 1);
+by Auto_tac;
+by (ftac DERIV_const_ratio_const2 1 THEN assume_tac 1);
+by (ftac DERIV_const_ratio_const2 2 THEN assume_tac 2);
+by (dtac real_less_half_sum 1);
+by (dtac real_gt_half_sum 2); 
+by (ftac (real_not_refl2 RS DERIV_const_ratio_const2) 1 THEN assume_tac 1);
+by (dtac ((real_not_refl2 RS not_sym) RS DERIV_const_ratio_const2) 2
+    THEN assume_tac 2);
+by (ALLGOALS (dres_inst_tac [("f","%u. (b-a)*u")] arg_cong)); 
+by (auto_tac (claset(), simpset() addsimps [real_inverse_eq_divide])); 
+by (asm_full_simp_tac (simpset() addsimps [real_add_commute, eq_commute]) 1);  
+qed "DERIV_const_average";
+
+
+(* ------------------------------------------------------------------------ *)
+(* Dull lemma that an continuous injection on an interval must have a strict*)
+(* maximum at an end point, not in the middle.                              *)
+(* ------------------------------------------------------------------------ *)
+
+Goal "[|#0 < d; ALL z. abs(z - x) <= d --> g(f z) = z; \
+\       ALL z. abs(z - x) <= d --> isCont f z |]  \
+\     ==> ~(ALL z. abs(z - x) <= d --> f(z) <= f(x))";
+by (rtac notI 1);
+by (rotate_tac 3 1);
+by (forw_inst_tac [("x","x - d")] spec 1);
+by (forw_inst_tac [("x","x + d")] spec 1);
+by (Step_tac 1);
+by (cut_inst_tac [("x","f(x - d)"),("y","f(x + d)")] 
+    (ARITH_PROVE "x <= y | y <= (x::real)") 4);
+by (etac disjE 4);
+by (REPEAT(arith_tac 1));
+by (cut_inst_tac [("f","f"),("a","x - d"),("b","x"),("y","f(x + d)")]
+    IVT_objl 1);
+by (Step_tac 1);
+by (arith_tac 1);
+by (asm_full_simp_tac (simpset() addsimps [abs_le_interval_iff]) 1);
+by (dres_inst_tac [("f","g")] arg_cong 1);
+by (rotate_tac 2 1);
+by (forw_inst_tac [("x","xa")] spec 1);
+by (dres_inst_tac [("x","x + d")] spec 1);
+by (asm_full_simp_tac (simpset() addsimps [abs_le_interval_iff]) 1);
+(* 2nd case: similar *)
+by (cut_inst_tac [("f","f"),("a","x"),("b","x + d"),("y","f(x - d)")]
+    IVT2_objl 1);
+by (Step_tac 1);
+by (arith_tac 1);
+by (asm_full_simp_tac (simpset() addsimps [abs_le_interval_iff]) 1);
+by (dres_inst_tac [("f","g")] arg_cong 1);
+by (rotate_tac 2 1);
+by (forw_inst_tac [("x","xa")] spec 1);
+by (dres_inst_tac [("x","x - d")] spec 1);
+by (asm_full_simp_tac (simpset() addsimps [abs_le_interval_iff]) 1);
+qed "lemma_isCont_inj";
+
+(* ------------------------------------------------------------------------ *)
+(* Similar version for lower bound                                          *)
+(* ------------------------------------------------------------------------ *)
+
+Goal "[|#0 < d; ALL z. abs(z - x) <= d --> g(f z) = z; \
+\       ALL z. abs(z - x) <= d --> isCont f z |]  \
+\     ==> ~(ALL z. abs(z - x) <= d --> f(x) <= f(z))";
+by (auto_tac (claset() addSDs [(asm_full_simplify (simpset()) 
+    (read_instantiate [("f","%x. - f x"),("g","%y. g(-y)"),("x","x"),("d","d")]
+     lemma_isCont_inj))],simpset() addsimps [isCont_minus]));
+qed "lemma_isCont_inj2";
+
+(* ------------------------------------------------------------------------ *)
+(* Show there's an interval surrounding f(x) in f[[x - d, x + d]]           *)
+(* Also from John's theory                                                  *)
+(* ------------------------------------------------------------------------ *)
+
+Addsimps [zero_eq_numeral_0,one_eq_numeral_1];
+
+val lemma_le = ARITH_PROVE "#0 <= (d::real) ==> -d <= d";
+
+(* FIXME: awful proof - needs improvement *)
+Goal "[| #0 < d; ALL z. abs(z - x) <= d --> g(f z) = z; \
+\        ALL z. abs(z - x) <= d --> isCont f z |] \
+\      ==> EX e. #0 < e & \
+\                 (ALL y. \
+\                     abs(y - f(x)) <= e --> \
+\                     (EX z. abs(z - x) <= d & (f z = y)))";
+by (ftac order_less_imp_le 1);
+by (dtac (lemma_le RS (asm_full_simplify (simpset()) (read_instantiate 
+    [("f","f"),("a","x - d"),("b","x + d")] isCont_Lb_Ub))) 1);
+by (Step_tac 1);
+by (asm_full_simp_tac (simpset() addsimps [abs_le_interval_iff]) 1);
+by (subgoal_tac "L <= f x & f x <= M" 1);
+by (dres_inst_tac [("P", "%v. ?P v --> ?Q v & ?R v"), ("x","x")] spec 2);
+by (Asm_full_simp_tac 2);
+by (subgoal_tac "L < f x & f x < M" 1);
+by (Step_tac 1);
+by (dres_inst_tac [("x","L")] (ARITH_PROVE "x < y ==> #0 < y - (x::real)") 1);
+by (dres_inst_tac [("x","f x")] (ARITH_PROVE "x < y ==> #0 < y - (x::real)") 1);
+by (dres_inst_tac [("d1.0","f x - L"),("d2.0","M - f x")] 
+    (rename_numerals real_lbound_gt_zero) 1);
+by (Step_tac 1);
+by (res_inst_tac [("x","e")] exI 1);
+by (Step_tac 1);
+by (asm_full_simp_tac (simpset() addsimps [abs_le_interval_iff]) 1);
+by (dres_inst_tac [("P","%v. ?PP v --> (EX xa. ?Q v xa)"),("x","y")] spec 1);
+by (Step_tac 1 THEN REPEAT(arith_tac 1));
+by (res_inst_tac [("x","xa")] exI 1);
+by (arith_tac 1);
+by (ALLGOALS(etac (ARITH_PROVE "[|x <= y; x ~= y |] ==> x < (y::real)")));
+by (ALLGOALS(rotate_tac 3));
+by (dtac lemma_isCont_inj2 1);
+by (assume_tac 2);
+by (dtac lemma_isCont_inj 3);
+by (assume_tac 4);
+by (TRYALL(assume_tac));
+by (Step_tac 1);
+by (ALLGOALS(dres_inst_tac [("x","z")] spec));
+by (ALLGOALS(arith_tac));
+qed "isCont_inj_range";
+
+
+(* ------------------------------------------------------------------------ *)
+(* Continuity of inverse function                                           *)
+(* ------------------------------------------------------------------------ *)
+
+Goal "[| #0 < d; ALL z. abs(z - x) <= d --> g(f(z)) = z; \
+\        ALL z. abs(z - x) <= d --> isCont f z |] \
+\     ==> isCont g (f x)";
+by (simp_tac (simpset() addsimps [isCont_iff,LIM_def]) 1);
+by (Step_tac 1);
+by (dres_inst_tac [("d1.0","r")] (rename_numerals real_lbound_gt_zero) 1);
+by (assume_tac 1 THEN Step_tac 1);
+by (subgoal_tac "ALL z. abs(z - x) <= e --> (g(f z) = z)" 1);
+by (Force_tac 2);
+by (subgoal_tac "ALL z. abs(z - x) <= e --> isCont f z" 1);
+by (Force_tac 2);
+by (dres_inst_tac [("d","e")] isCont_inj_range 1);
+by (assume_tac 2 THEN assume_tac 1);
+by (Step_tac 1);
+by (res_inst_tac [("x","ea")] exI 1);
+by Auto_tac;
+by (rotate_tac 4 1);
+by (dres_inst_tac [("x","f(x) + xa")] spec 1);
+by Auto_tac;
+by (dtac sym 1 THEN Auto_tac);
+by (arith_tac 1);
+qed "isCont_inverse";
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Hyperreal/Lim.thy	Sat Dec 30 22:03:47 2000 +0100
@@ -0,0 +1,79 @@
+(*  Title       : Lim.thy
+    Author      : Jacques D. Fleuriot
+    Copyright   : 1998  University of Cambridge
+    Description : Theory of limits, continuity and 
+                  differentiation of real=>real functions
+*)
+
+Lim = SEQ + RealAbs + 
+
+(*-----------------------------------------------------------------------
+    Limits, continuity and differentiation: standard and NS definitions
+ -----------------------------------------------------------------------*)
+
+constdefs
+  LIM :: [real=>real,real,real] => bool
+				("((_)/ -- (_)/ --> (_))" [60, 0, 60] 60)
+  "f -- a --> L ==
+     ALL r. #0 < r --> 
+	     (EX s. #0 < s & (ALL x. (x ~= a & (abs(x + -a) < s)
+			  --> abs(f x + -L) < r)))"
+
+  NSLIM :: [real=>real,real,real] => bool
+			      ("((_)/ -- (_)/ --NS> (_))" [60, 0, 60] 60)
+  "f -- a --NS> L == (ALL x. (x ~= hypreal_of_real a & 
+		      x @= hypreal_of_real a -->
+		      (*f* f) x @= hypreal_of_real L))"   
+
+  isCont :: [real=>real,real] => bool
+  "isCont f a == (f -- a --> (f a))"        
+
+  (* NS definition dispenses with limit notions *)
+  isNSCont :: [real=>real,real] => bool
+  "isNSCont f a == (ALL y. y @= hypreal_of_real a --> 
+			   (*f* f) y @= hypreal_of_real (f a))"
+
+  (* differentiation: D is derivative of function f at x *)
+  deriv:: [real=>real,real,real] => bool
+			    ("(DERIV (_)/ (_)/ :> (_))" [60, 0, 60] 60)
+  "DERIV f x :> D == ((%h. (f(x + h) + -f(x))/h) -- #0 --> D)"
+
+  nsderiv :: [real=>real,real,real] => bool
+			    ("(NSDERIV (_)/ (_)/ :> (_))" [60, 0, 60] 60)
+  "NSDERIV f x :> D == (ALL h: Infinitesimal - {0}. 
+			((*f* f)(hypreal_of_real x + h) + 
+			 - hypreal_of_real (f x))/h @= hypreal_of_real D)"
+
+  differentiable :: [real=>real,real] => bool   (infixl 60)
+  "f differentiable x == (EX D. DERIV f x :> D)"
+
+  NSdifferentiable :: [real=>real,real] => bool   (infixl 60)
+  "f NSdifferentiable x == (EX D. NSDERIV f x :> D)"
+
+  increment :: [real=>real,real,hypreal] => hypreal
+  "increment f x h == (@inc. f NSdifferentiable x & 
+		       inc = (*f* f)(hypreal_of_real x + h) + -hypreal_of_real (f x))"
+
+  isUCont :: (real=>real) => bool
+  "isUCont f ==  (ALL r. #0 < r --> 
+		      (EX s. #0 < s & (ALL x y. abs(x + -y) < s
+			    --> abs(f x + -f y) < r)))"
+
+  isNSUCont :: (real=>real) => bool
+  "isNSUCont f == (ALL x y. x @= y --> (*f* f) x @= (*f* f) y)"
+
+
+(*Used in the proof of the Bolzano theorem*)
+consts
+  Bolzano_bisect :: "[real*real=>bool, real, real, nat] => (real*real)"
+
+primrec
+  "Bolzano_bisect P a b 0 = (a,b)"
+  "Bolzano_bisect P a b (Suc n) =
+      (let (x,y) = Bolzano_bisect P a b n
+       in if P(x, (x+y)/#2) then ((x+y)/#2, y)
+                            else (x, (x+y)/#2) )"
+  
+
+end
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Hyperreal/NSA.ML	Sat Dec 30 22:03:47 2000 +0100
@@ -0,0 +1,2215 @@
+(*  Title       : NSA.ML
+    Author      : Jacques D. Fleuriot
+    Copyright   : 1998  University of Cambridge
+    Description : Infinite numbers, Infinitesimals,
+                  infinitely close relation  etc.
+*) 
+
+fun CLAIM_SIMP str thms =
+               prove_goal (the_context()) str
+               (fn prems => [cut_facts_tac prems 1,
+                   auto_tac (claset(),simpset() addsimps thms)]);
+fun CLAIM str = CLAIM_SIMP str [];
+
+(*--------------------------------------------------------------------
+     Closure laws for members of (embedded) set standard real SReal
+ --------------------------------------------------------------------*)
+
+Goalw [SReal_def] "[| (x::hypreal): SReal; y: SReal |] ==> x + y: SReal";
+by (Step_tac 1);
+by (res_inst_tac [("x","r + ra")] exI 1);
+by (Simp_tac 1);
+qed "SReal_add";
+
+Goalw [SReal_def] "[| (x::hypreal): SReal; y: SReal |] ==> x * y: SReal";
+by (Step_tac 1);
+by (res_inst_tac [("x","r * ra")] exI 1);
+by (simp_tac (simpset() addsimps [hypreal_of_real_mult]) 1);
+qed "SReal_mult";
+
+Goalw [SReal_def] "(x::hypreal): SReal ==> inverse x : SReal";
+by (blast_tac (claset() addIs [hypreal_of_real_inverse RS sym]) 1); 
+qed "SReal_inverse";
+
+Goal "[| (x::hypreal): SReal;  y: SReal |] ==> x/y: SReal";
+by (asm_simp_tac (simpset() addsimps [SReal_mult,SReal_inverse,
+                                      hypreal_divide_def]) 1); 
+qed "SReal_divide";
+
+Goalw [SReal_def] "(x::hypreal): SReal ==> -x : SReal";
+by (blast_tac (claset() addIs [hypreal_of_real_minus RS sym]) 1); 
+qed "SReal_minus";
+
+Goal "(-x : SReal) = ((x::hypreal): SReal)";
+by Auto_tac;  
+by (etac SReal_minus 2); 
+by (dtac SReal_minus 1); 
+by Auto_tac;  
+qed "SReal_minus_iff";
+Addsimps [SReal_minus_iff]; 
+
+Goal "[| (x::hypreal) + y : SReal; y: SReal |] ==> x: SReal";
+by (dres_inst_tac [("x","y")] SReal_minus 1);
+by (dtac SReal_add 1);
+by (assume_tac 1); 
+by Auto_tac;  
+qed "SReal_add_cancel";
+
+Goalw [SReal_def] "(x::hypreal): SReal ==> abs x : SReal";
+by (auto_tac (claset(), simpset() addsimps [hypreal_of_real_hrabs]));
+qed "SReal_hrabs";
+
+Goalw [SReal_def] "hypreal_of_real x: SReal";
+by (Blast_tac 1);
+qed "SReal_hypreal_of_real";
+Addsimps [SReal_hypreal_of_real];
+
+Goalw [hypreal_number_of_def] "(number_of w ::hypreal) : SReal";
+by (rtac SReal_hypreal_of_real 1);
+qed "SReal_number_of";
+Addsimps [SReal_number_of];
+
+Goalw [hypreal_divide_def] "r : SReal ==> r/(number_of w::hypreal) : SReal";
+by (blast_tac (claset() addSIs [SReal_number_of, SReal_mult, 
+                                SReal_inverse]) 1);
+qed "SReal_divide_number_of";
+
+(* Infinitesimal ehr not in SReal *)
+
+Goalw [SReal_def] "ehr ~: SReal";
+by (auto_tac (claset(),
+              simpset() addsimps [hypreal_of_real_not_eq_epsilon RS not_sym]));
+qed "SReal_epsilon_not_mem";
+
+Goalw [SReal_def] "whr ~: SReal";
+by (auto_tac (claset(),
+              simpset() addsimps [hypreal_of_real_not_eq_omega RS not_sym]));
+qed "SReal_omega_not_mem";
+
+Goalw [SReal_def] "{x. hypreal_of_real x : SReal} = (UNIV::real set)";
+by Auto_tac;
+qed "SReal_UNIV_real";
+
+Goalw [SReal_def] "(x: SReal) = (EX y. x = hypreal_of_real y)";
+by Auto_tac;
+qed "SReal_iff";
+
+Goalw [SReal_def] "hypreal_of_real ``(UNIV::real set) = SReal";
+by Auto_tac;
+qed "hypreal_of_real_image";
+
+Goalw [SReal_def] "inv hypreal_of_real ``SReal = (UNIV::real set)";
+by Auto_tac;
+by (rtac (inj_hypreal_of_real RS inv_f_f RS subst) 1);
+by (Blast_tac 1);
+qed "inv_hypreal_of_real_image";
+
+Goalw [SReal_def] 
+      "[| EX x. x: P; P <= SReal |] ==> EX Q. P = hypreal_of_real `` Q";
+by (Best_tac 1); 
+qed "SReal_hypreal_of_real_image";
+
+Goal "[| (x::hypreal): SReal; y: SReal;  x<y |] ==> EX r: SReal. x<r & r<y";
+by (auto_tac (claset(), simpset() addsimps [SReal_iff]));
+by (dtac real_dense 1 THEN Step_tac 1);
+by (res_inst_tac [("x","hypreal_of_real r")] bexI 1);
+by Auto_tac;
+qed "SReal_dense";
+
+(*------------------------------------------------------------------
+                   Completeness of SReal
+ ------------------------------------------------------------------*)
+Goal "P <= SReal ==> ((EX x:P. y < x) = \ 
+\     (EX X. hypreal_of_real X : P & y < hypreal_of_real X))";
+by (blast_tac (claset() addSDs [SReal_iff RS iffD1]) 1);
+by (flexflex_tac );
+qed "SReal_sup_lemma";
+
+Goal "[| P <= SReal; EX x. x: P; EX y : SReal. ALL x: P. x < y |] \
+\     ==> (EX X. X: {w. hypreal_of_real w : P}) & \
+\         (EX Y. ALL X: {w. hypreal_of_real w : P}. X < Y)";
+by (rtac conjI 1);
+by (fast_tac (claset() addSDs [SReal_iff RS iffD1]) 1);
+by (Auto_tac THEN forward_tac [subsetD] 1 THEN assume_tac 1);
+by (dtac (SReal_iff RS iffD1) 1);
+by (Auto_tac THEN res_inst_tac [("x","ya")] exI 1);
+by Auto_tac;
+qed "SReal_sup_lemma2";
+
+(*------------------------------------------------------
+    lifting of ub and property of lub
+ -------------------------------------------------------*)
+Goalw [isUb_def,setle_def] 
+      "(isUb (SReal) (hypreal_of_real `` Q) (hypreal_of_real Y)) = \
+\      (isUb (UNIV :: real set) Q Y)";
+by Auto_tac;
+qed "hypreal_of_real_isUb_iff";
+
+Goalw [isLub_def,leastP_def] 
+     "isLub SReal (hypreal_of_real `` Q) (hypreal_of_real Y) \
+\     ==> isLub (UNIV :: real set) Q Y";
+by (auto_tac (claset() addIs [hypreal_of_real_isUb_iff RS iffD2],
+              simpset() addsimps [hypreal_of_real_isUb_iff, setge_def]));
+qed "hypreal_of_real_isLub1";
+
+Goalw [isLub_def,leastP_def] 
+      "isLub (UNIV :: real set) Q Y \
+\      ==> isLub SReal (hypreal_of_real `` Q) (hypreal_of_real Y)";
+by (auto_tac (claset(),
+              simpset() addsimps [hypreal_of_real_isUb_iff, setge_def]));
+by (forw_inst_tac [("x2","x")] (isUbD2a RS (SReal_iff RS iffD1) RS exE) 1);
+by (assume_tac 2);
+by (dres_inst_tac [("x","xa")] spec 1);
+by (auto_tac (claset(), simpset() addsimps [hypreal_of_real_isUb_iff]));
+qed "hypreal_of_real_isLub2";
+
+Goal "(isLub SReal (hypreal_of_real `` Q) (hypreal_of_real Y)) = \
+\     (isLub (UNIV :: real set) Q Y)";
+by (blast_tac (claset() addIs [hypreal_of_real_isLub1,
+                               hypreal_of_real_isLub2]) 1);
+qed "hypreal_of_real_isLub_iff";
+
+(* lemmas *)
+Goalw [isUb_def] 
+     "isUb SReal P Y ==> EX Yo. isUb SReal P (hypreal_of_real Yo)";
+by (auto_tac (claset(), simpset() addsimps [SReal_iff]));
+qed "lemma_isUb_hypreal_of_real";
+
+Goalw [isLub_def,leastP_def,isUb_def] 
+     "isLub SReal P Y ==> EX Yo. isLub SReal P (hypreal_of_real Yo)";
+by (auto_tac (claset(), simpset() addsimps [SReal_iff]));
+qed "lemma_isLub_hypreal_of_real";
+
+Goalw [isLub_def,leastP_def,isUb_def] 
+     "EX Yo. isLub SReal P (hypreal_of_real Yo) ==> EX Y. isLub SReal P Y";
+by Auto_tac;
+qed "lemma_isLub_hypreal_of_real2";
+
+Goal "[| P <= SReal;  EX x. x: P;  EX Y. isUb SReal P Y |] \
+\     ==> EX t::hypreal. isLub SReal P t";
+by (forward_tac [SReal_hypreal_of_real_image] 1);
+by (Auto_tac THEN dtac lemma_isUb_hypreal_of_real 1);
+by (auto_tac (claset() addSIs [reals_complete, lemma_isLub_hypreal_of_real2], 
+     simpset() addsimps [hypreal_of_real_isLub_iff,hypreal_of_real_isUb_iff]));
+qed "SReal_complete";
+
+(*--------------------------------------------------------------------
+        Set of finite elements is a subring of the extended reals
+ --------------------------------------------------------------------*)
+Goalw [HFinite_def] "[|x : HFinite; y : HFinite|] ==> (x+y) : HFinite";
+by (blast_tac (claset() addSIs [SReal_add,hrabs_add_less]) 1);
+qed "HFinite_add";
+
+Goalw [HFinite_def] "[|x : HFinite; y : HFinite|] ==> x*y : HFinite";
+by (Asm_full_simp_tac 1); 
+by (blast_tac (claset() addSIs [SReal_mult,hrabs_mult_less]) 1);
+qed "HFinite_mult";
+
+Goalw [HFinite_def] "(-x : HFinite) = (x : HFinite)";
+by (Simp_tac 1);
+qed "HFinite_minus_iff";
+
+Goalw [SReal_def,HFinite_def] "SReal <= HFinite";
+by Auto_tac;
+by (res_inst_tac [("x","#1 + abs(hypreal_of_real r)")] exI 1);
+by (auto_tac (claset(), simpset() addsimps [hypreal_of_real_hrabs]));
+by (res_inst_tac [("x","#1 + abs r")] exI 1);
+by (Simp_tac 1);
+qed "SReal_subset_HFinite";
+
+Goal "hypreal_of_real x : HFinite";
+by (auto_tac (claset() addIs [(SReal_subset_HFinite RS subsetD)],
+              simpset()));
+qed "HFinite_hypreal_of_real";
+
+Addsimps [HFinite_hypreal_of_real];
+
+Goalw [HFinite_def] "x : HFinite ==> EX t: SReal. abs x < t";
+by Auto_tac;
+qed "HFiniteD";
+
+Goalw [HFinite_def] "x : HFinite ==> abs x : HFinite";
+by (auto_tac (claset(), simpset() addsimps [hrabs_idempotent]));
+qed "HFinite_hrabs";
+
+Goalw [HFinite_def,Bex_def] "x ~: HFinite ==> abs x ~: HFinite";
+by (auto_tac (claset(), simpset() addsimps [hrabs_idempotent]));
+qed "not_HFinite_hrabs";
+
+Goal "number_of w : HFinite";
+by (rtac (SReal_number_of RS (SReal_subset_HFinite RS subsetD)) 1);
+qed "HFinite_number_of";
+Addsimps [HFinite_number_of];
+
+Goal "[|x : HFinite; y <= x; #0 <= y |] ==> y: HFinite";
+by (case_tac "x <= #0" 1);
+by (dres_inst_tac [("y","x")] order_trans 1);
+by (dtac hypreal_le_anti_sym 2);
+by (auto_tac (claset() addSDs [not_hypreal_leE], simpset()));
+by (auto_tac (claset() addSIs [bexI] addIs [order_le_less_trans],
+     simpset() addsimps [hrabs_eqI1,hrabs_eqI2,hrabs_minus_eqI1,HFinite_def]));
+qed "HFinite_bounded"; 
+
+(*------------------------------------------------------------------
+       Set of infinitesimals is a subring of the hyperreals   
+ ------------------------------------------------------------------*)
+Goalw [Infinitesimal_def]
+      "x : Infinitesimal ==> ALL r: SReal. #0 < r --> abs x < r";
+by Auto_tac;
+qed "InfinitesimalD";
+
+Goalw [Infinitesimal_def] "#0 : Infinitesimal";
+by (simp_tac (simpset() addsimps [hrabs_zero]) 1);
+qed "Infinitesimal_zero";
+AddIffs [Infinitesimal_zero];
+
+Goal "x/(#2::hypreal) + x/(#2::hypreal) = x";
+by Auto_tac;  
+qed "hypreal_sum_of_halves";
+
+Goal "#0 < r ==> #0 < r/(#2::hypreal)";
+by Auto_tac;  
+qed "hypreal_half_gt_zero";
+
+Goalw [Infinitesimal_def] 
+     "[| x : Infinitesimal; y : Infinitesimal |] ==> (x+y) : Infinitesimal";
+by Auto_tac;
+by (rtac (hypreal_sum_of_halves RS subst) 1);
+by (dtac hypreal_half_gt_zero 1);
+by (blast_tac (claset() addIs [hrabs_add_less, hrabs_add_less, 
+                               SReal_divide_number_of]) 1); 
+qed "Infinitesimal_add";
+
+Goalw [Infinitesimal_def] "(-x:Infinitesimal) = (x:Infinitesimal)";
+by (Full_simp_tac 1);
+qed "Infinitesimal_minus_iff";
+Addsimps [Infinitesimal_minus_iff];
+
+Goal "[| x : Infinitesimal;  y : Infinitesimal |] ==> x-y : Infinitesimal";
+by (asm_simp_tac
+    (simpset() addsimps [hypreal_diff_def, Infinitesimal_add]) 1);
+qed "Infinitesimal_diff";
+
+Goalw [Infinitesimal_def] 
+     "[| x : Infinitesimal; y : Infinitesimal |] ==> (x * y) : Infinitesimal";
+by Auto_tac;
+by (case_tac "y=#0" 1);
+by (cut_inst_tac [("u","abs x"),("v","#1"),("x","abs y"),("y","r")] 
+    hypreal_mult_less_mono 2);
+by Auto_tac;  
+qed "Infinitesimal_mult";
+
+Goal "[| x : Infinitesimal; y : HFinite |] ==> (x * y) : Infinitesimal";
+by (auto_tac (claset() addSDs [HFiniteD],
+              simpset() addsimps [Infinitesimal_def]));
+by (forward_tac [hrabs_less_gt_zero] 1);
+by (dres_inst_tac [("x","r/t")] bspec 1); 
+by (blast_tac (claset() addIs [SReal_divide]) 1);
+by (asm_full_simp_tac (simpset() addsimps [hypreal_0_less_divide_iff]) 1); 
+by (case_tac "x=0 | y=0" 1);
+by (cut_inst_tac [("u","abs x"),("v","r/t"),("x","abs y")] 
+    hypreal_mult_less_mono 2);
+by (auto_tac (claset(), simpset() addsimps [hypreal_0_less_divide_iff]));  
+qed "Infinitesimal_HFinite_mult";
+
+Goal "[| x : Infinitesimal; y : HFinite |] ==> (y * x) : Infinitesimal";
+by (auto_tac (claset() addDs [Infinitesimal_HFinite_mult],
+              simpset() addsimps [hypreal_mult_commute]));
+qed "Infinitesimal_HFinite_mult2";
+
+(*** rather long proof ***)
+Goalw [HInfinite_def,Infinitesimal_def] 
+     "x: HInfinite ==> inverse x: Infinitesimal";
+by Auto_tac;
+by (eres_inst_tac [("x","inverse r")] ballE 1);
+by (rtac (hrabs_inverse RS ssubst) 1);
+by (forw_inst_tac [("x1","r"),("z","abs x")] 
+    (hypreal_inverse_gt_0 RS order_less_trans) 1);
+by (assume_tac 1);
+by (dtac ((hypreal_inverse_inverse RS sym) RS subst) 1);
+by (rtac (hypreal_inverse_less_iff RS iffD1) 1);
+by (auto_tac (claset(), simpset() addsimps [SReal_inverse]));  
+qed "HInfinite_inverse_Infinitesimal";
+
+
+
+Goalw [HInfinite_def] "[|x: HInfinite;y: HInfinite|] ==> (x*y): HInfinite";
+by Auto_tac;
+by (eres_inst_tac [("x","#1")] ballE 1);
+by (eres_inst_tac [("x","r")] ballE 1);
+by (case_tac "y=0" 1); 
+by (cut_inst_tac [("x","#1"),("y","abs x"),
+                  ("u","r"),("v","abs y")] hypreal_mult_less_mono 2);
+by (auto_tac (claset(), simpset() addsimps hypreal_mult_ac));  
+qed "HInfinite_mult";
+
+Goalw [HInfinite_def] 
+      "[|x: HInfinite; #0 <= y; #0 <= x|] ==> (x + y): HInfinite";
+by (auto_tac (claset() addSIs [hypreal_add_zero_less_le_mono],
+              simpset() addsimps [hrabs_eqI1, hypreal_add_commute,
+                                  hypreal_le_add_order]));
+qed "HInfinite_add_ge_zero";
+
+Goal "[|x: HInfinite; #0 <= y; #0 <= x|] ==> (y + x): HInfinite";
+by (auto_tac (claset() addSIs [HInfinite_add_ge_zero],
+              simpset() addsimps [hypreal_add_commute]));
+qed "HInfinite_add_ge_zero2";
+
+Goal "[|x: HInfinite; #0 < y; #0 < x|] ==> (x + y): HInfinite";
+by (blast_tac (claset() addIs [HInfinite_add_ge_zero,
+                    order_less_imp_le]) 1);
+qed "HInfinite_add_gt_zero";
+
+Goalw [HInfinite_def] "(-x: HInfinite) = (x: HInfinite)";
+by Auto_tac;
+qed "HInfinite_minus_iff";
+
+Goal "[|x: HInfinite; y <= #0; x <= #0|] ==> (x + y): HInfinite";
+by (dtac (HInfinite_minus_iff RS iffD2) 1);
+by (rtac (HInfinite_minus_iff RS iffD1) 1);
+by (auto_tac (claset() addIs [HInfinite_add_ge_zero],
+              simpset() addsimps [hypreal_minus_zero_le_iff]));
+qed "HInfinite_add_le_zero";
+
+Goal "[|x: HInfinite; y < #0; x < #0|] ==> (x + y): HInfinite";
+by (blast_tac (claset() addIs [HInfinite_add_le_zero,
+                               order_less_imp_le]) 1);
+qed "HInfinite_add_lt_zero";
+
+Goal "[|a: HFinite; b: HFinite; c: HFinite|] \ 
+\     ==> a*a + b*b + c*c : HFinite";
+by (auto_tac (claset() addIs [HFinite_mult,HFinite_add], simpset()));
+qed "HFinite_sum_squares";
+
+Goal "x ~: Infinitesimal ==> x ~= #0";
+by Auto_tac;
+qed "not_Infinitesimal_not_zero";
+
+Goal "x: HFinite - Infinitesimal ==> x ~= #0";
+by Auto_tac;
+qed "not_Infinitesimal_not_zero2";
+
+Goal "x : Infinitesimal ==> abs x : Infinitesimal";
+by (res_inst_tac [("x1","x")] (hrabs_disj RS disjE) 1);
+by Auto_tac;
+qed "Infinitesimal_hrabs";
+
+Goal "x ~: Infinitesimal ==> abs x ~: Infinitesimal";
+by (res_inst_tac [("x1","x")] (hrabs_disj RS disjE) 1);
+by Auto_tac;
+qed "not_Infinitesimal_hrabs";
+
+Goal "abs x : Infinitesimal ==> x : Infinitesimal";
+by (rtac ccontr 1);
+by (blast_tac (claset() addDs [not_Infinitesimal_hrabs]) 1);
+qed "hrabs_Infinitesimal_Infinitesimal";
+
+Goal "x : HFinite - Infinitesimal ==> abs x : HFinite - Infinitesimal";
+by (fast_tac (set_cs addSEs [HFinite_hrabs,not_Infinitesimal_hrabs]) 1);
+qed "HFinite_diff_Infinitesimal_hrabs";
+
+Goalw [Infinitesimal_def] 
+      "[| e : Infinitesimal; abs x < e |] ==> x : Infinitesimal";
+by (auto_tac (claset() addSDs [bspec], simpset()));
+by (dres_inst_tac [("x","e")] (hrabs_ge_self RS order_le_less_trans) 1);
+by (fast_tac (claset() addIs [order_less_trans]) 1);
+qed "hrabs_less_Infinitesimal";
+
+Goal "[| e : Infinitesimal; abs x <= e |] ==> x : Infinitesimal";
+by (blast_tac (claset() addDs [order_le_imp_less_or_eq] 
+                        addIs [hrabs_Infinitesimal_Infinitesimal,
+                               hrabs_less_Infinitesimal]) 1);
+qed "hrabs_le_Infinitesimal";
+
+Goalw [Infinitesimal_def] 
+      "[| e : Infinitesimal; \
+\         e' : Infinitesimal; \
+\         e' < x ; x < e |] ==> x : Infinitesimal";
+by (auto_tac (claset() addSDs [bspec], simpset()));
+by (dres_inst_tac [("x","e")] (hrabs_ge_self RS order_le_less_trans) 1);
+by (dtac (hrabs_interval_iff RS iffD1) 1);
+by (fast_tac(claset() addIs [order_less_trans,hrabs_interval_iff RS iffD2]) 1);
+qed "Infinitesimal_interval";
+
+Goal "[| e : Infinitesimal; e' : Infinitesimal; \
+\        e' <= x ; x <= e |] ==> x : Infinitesimal";
+by (auto_tac (claset() addIs [Infinitesimal_interval],
+    simpset() addsimps [hypreal_le_eq_less_or_eq]));
+qed "Infinitesimal_interval2";
+
+Goalw [Infinitesimal_def] 
+     "[| x ~: Infinitesimal;  y ~: Infinitesimal|] ==> (x*y) ~:Infinitesimal";
+by (Clarify_tac 1);
+by (asm_full_simp_tac (simpset() addsimps [linorder_not_less]) 1); 
+by (eres_inst_tac [("x","r*ra")] ballE 1);
+by (fast_tac (claset() addIs [SReal_mult]) 2);
+by (auto_tac (claset(), simpset() addsimps [hypreal_0_less_mult_iff]));
+by (cut_inst_tac [("x","ra"),("y","abs y"),
+                  ("u","r"),("v","abs x")] hypreal_mult_le_mono 1);
+by Auto_tac;  
+qed "not_Infinitesimal_mult";
+
+Goal "x*y : Infinitesimal ==> x : Infinitesimal | y : Infinitesimal";
+by (rtac ccontr 1);
+by (dtac (de_Morgan_disj RS iffD1) 1);
+by (fast_tac (claset() addDs [not_Infinitesimal_mult]) 1);
+qed "Infinitesimal_mult_disj";
+
+Goal "x: HFinite-Infinitesimal ==> x ~= #0";
+by (Blast_tac 1);
+qed "HFinite_Infinitesimal_not_zero";
+
+Goal "[| x : HFinite - Infinitesimal; \
+\                  y : HFinite - Infinitesimal \
+\               |] ==> (x*y) : HFinite - Infinitesimal";
+by (Clarify_tac 1);
+by (blast_tac (claset() addDs [HFinite_mult,not_Infinitesimal_mult]) 1);
+qed "HFinite_Infinitesimal_diff_mult";
+
+Goalw [Infinitesimal_def,HFinite_def]  
+      "Infinitesimal <= HFinite";
+by Auto_tac;  
+by (res_inst_tac [("x","#1")] bexI 1); 
+by Auto_tac;  
+qed "Infinitesimal_subset_HFinite";
+
+Goal "x: Infinitesimal ==> x * hypreal_of_real r : Infinitesimal";
+by (etac (HFinite_hypreal_of_real RSN 
+          (2,Infinitesimal_HFinite_mult)) 1);
+qed "Infinitesimal_hypreal_of_real_mult";
+
+Goal "x: Infinitesimal ==> hypreal_of_real r * x: Infinitesimal";
+by (etac (HFinite_hypreal_of_real RSN 
+          (2,Infinitesimal_HFinite_mult2)) 1);
+qed "Infinitesimal_hypreal_of_real_mult2";
+
+(*----------------------------------------------------------------------
+                   Infinitely close relation @=
+ ----------------------------------------------------------------------*)
+
+Goalw [Infinitesimal_def,inf_close_def] 
+        "x:Infinitesimal = (x @= #0)";
+by (Simp_tac 1);
+qed "mem_infmal_iff";
+
+Goalw [inf_close_def]" (x @= y) = (x + -y @= #0)";
+by (Simp_tac 1);
+qed "inf_close_minus_iff";
+
+Goalw [inf_close_def]" (x @= y) = (-y + x @= #0)";
+by (simp_tac (simpset() addsimps [hypreal_add_commute]) 1);
+qed "inf_close_minus_iff2";
+
+Goalw [inf_close_def,Infinitesimal_def]  "x @= x";
+by (Simp_tac 1);
+qed "inf_close_refl";
+AddIffs [inf_close_refl];
+
+Goalw [inf_close_def]  "x @= y ==> y @= x";
+by (rtac (hypreal_minus_distrib1 RS subst) 1);
+by (etac (Infinitesimal_minus_iff RS iffD2) 1);
+qed "inf_close_sym";
+
+Goalw [inf_close_def]  "[| x @= y; y @= z |] ==> x @= z";
+by (dtac Infinitesimal_add 1); 
+by (assume_tac 1); 
+by Auto_tac;  
+qed "inf_close_trans";
+
+Goal "[| r @= x; s @= x |] ==> r @= s";
+by (blast_tac (claset() addIs [inf_close_sym, inf_close_trans]) 1); 
+qed "inf_close_trans2";
+
+Goal "[| x @= r; x @= s|] ==> r @= s";
+by (blast_tac (claset() addIs [inf_close_sym, inf_close_trans]) 1); 
+qed "inf_close_trans3";
+
+Goal "(number_of w @= x) = (x @= number_of w)";
+by (blast_tac (claset() addIs [inf_close_sym]) 1); 
+qed "number_of_inf_close_reorient";
+Addsimps [number_of_inf_close_reorient];
+
+Goal "(x-y : Infinitesimal) = (x @= y)";
+by (auto_tac (claset(),
+              simpset() addsimps [hypreal_diff_def, inf_close_minus_iff RS sym,
+                                  mem_infmal_iff]));
+qed "Infinitesimal_inf_close_minus";
+
+Goalw [monad_def] "(x @= y) = (monad(x)=monad(y))";
+by (auto_tac (claset() addDs [inf_close_sym] 
+                       addSEs [inf_close_trans,equalityCE],
+              simpset()));
+qed "inf_close_monad_iff";
+
+Goal "[| x: Infinitesimal; y: Infinitesimal |] ==> x @= y";
+by (asm_full_simp_tac (simpset() addsimps [mem_infmal_iff]) 1);
+by (blast_tac (claset() addIs [inf_close_trans, inf_close_sym]) 1); 
+qed "Infinitesimal_inf_close";
+
+val prem1::prem2::rest = 
+goalw thy [inf_close_def] "[| a @= b; c @= d |] ==> a+c @= b+d";
+by (rtac (hypreal_minus_add_distrib RS ssubst) 1);
+by (rtac (hypreal_add_assoc RS ssubst) 1);
+by (res_inst_tac [("y1","c")] (hypreal_add_left_commute RS subst) 1);
+by (rtac (hypreal_add_assoc RS subst) 1);
+by (rtac ([prem1,prem2] MRS Infinitesimal_add) 1);
+qed "inf_close_add";
+
+Goal "a @= b ==> -a @= -b";
+by (rtac ((inf_close_minus_iff RS iffD2) RS inf_close_sym) 1);
+by (dtac (inf_close_minus_iff RS iffD1) 1);
+by (simp_tac (simpset() addsimps [hypreal_add_commute]) 1);
+qed "inf_close_minus";
+
+Goal "-a @= -b ==> a @= b";
+by (auto_tac (claset() addDs [inf_close_minus], simpset()));
+qed "inf_close_minus2";
+
+Goal "(-a @= -b) = (a @= b)";
+by (blast_tac (claset() addIs [inf_close_minus,inf_close_minus2]) 1);
+qed "inf_close_minus_cancel";
+
+Addsimps [inf_close_minus_cancel];
+
+Goal "[| a @= b; c @= d |] ==> a + -c @= b + -d";
+by (blast_tac (claset() addSIs [inf_close_add,inf_close_minus]) 1);
+qed "inf_close_add_minus";
+
+Goalw [inf_close_def] "[| a @= b; c: HFinite|] ==> a*c @= b*c"; 
+by (asm_full_simp_tac (simpset() addsimps [Infinitesimal_HFinite_mult,
+    hypreal_minus_mult_eq1,hypreal_add_mult_distrib RS sym] 
+    delsimps [hypreal_minus_mult_eq1 RS sym]) 1);
+qed "inf_close_mult1";
+
+Goal "[|a @= b; c: HFinite|] ==> c*a @= c*b"; 
+by (asm_simp_tac (simpset() addsimps [inf_close_mult1,hypreal_mult_commute]) 1);
+qed "inf_close_mult2";
+
+Goal "[|u @= v*x; x @= y; v: HFinite|] ==> u @= v*y";
+by (fast_tac (claset() addIs [inf_close_mult2,inf_close_trans]) 1);
+qed "inf_close_mult_subst";
+
+Goal "[| u @= x*v; x @= y; v: HFinite |] ==> u @= y*v";
+by (fast_tac (claset() addIs [inf_close_mult1,inf_close_trans]) 1);
+qed "inf_close_mult_subst2";
+
+Goal "[| u @= x*hypreal_of_real v; x @= y |] ==> u @= y*hypreal_of_real v";
+by (auto_tac (claset() addIs [inf_close_mult_subst2], simpset()));
+qed "inf_close_mult_subst_SReal";
+
+Goalw [inf_close_def]  "a = b ==> a @= b";
+by (Asm_simp_tac 1);
+qed "inf_close_eq_imp";
+
+Goal "x: Infinitesimal ==> -x @= x"; 
+by (fast_tac (HOL_cs addIs [Infinitesimal_minus_iff RS iffD2,
+    mem_infmal_iff RS iffD1,inf_close_trans2]) 1);
+qed "Infinitesimal_minus_inf_close";
+
+Goalw [inf_close_def]  "(EX y: Infinitesimal. x + -z = y) = (x @= z)";
+by (Blast_tac 1);
+qed "bex_Infinitesimal_iff";
+
+Goal "(EX y: Infinitesimal. x = z + y) = (x @= z)";
+by (asm_full_simp_tac (simpset() addsimps [bex_Infinitesimal_iff RS sym]) 1);
+by (Force_tac 1);
+qed "bex_Infinitesimal_iff2";
+
+Goal "[| y: Infinitesimal; x + y = z |] ==> x @= z";
+by (rtac (bex_Infinitesimal_iff RS iffD1) 1);
+by (dtac (Infinitesimal_minus_iff RS iffD2) 1);
+by (auto_tac (claset(), simpset() addsimps [hypreal_minus_add_distrib,
+    hypreal_add_assoc RS sym]));
+qed "Infinitesimal_add_inf_close";
+
+Goal "y: Infinitesimal ==> x @= x + y";
+by (rtac (bex_Infinitesimal_iff RS iffD1) 1);
+by (dtac (Infinitesimal_minus_iff RS iffD2) 1);
+by (auto_tac (claset(), simpset() addsimps [hypreal_minus_add_distrib,
+    hypreal_add_assoc RS sym]));
+qed "Infinitesimal_add_inf_close_self";
+
+Goal "y: Infinitesimal ==> x @= y + x";
+by (auto_tac (claset() addDs [Infinitesimal_add_inf_close_self],
+    simpset() addsimps [hypreal_add_commute]));
+qed "Infinitesimal_add_inf_close_self2";
+
+Goal "y: Infinitesimal ==> x @= x + -y";
+by (blast_tac (claset() addSIs [Infinitesimal_add_inf_close_self,
+                                Infinitesimal_minus_iff RS iffD2]) 1);
+qed "Infinitesimal_add_minus_inf_close_self";
+
+Goal "[| y: Infinitesimal; x+y @= z|] ==> x @= z";
+by (dres_inst_tac [("x","x")] (Infinitesimal_add_inf_close_self RS inf_close_sym) 1);
+by (etac (inf_close_trans3 RS inf_close_sym) 1);
+by (assume_tac 1);
+qed "Infinitesimal_add_cancel";
+
+Goal "[| y: Infinitesimal; x @= z + y|] ==> x @= z";
+by (dres_inst_tac [("x","z")] (Infinitesimal_add_inf_close_self2  RS inf_close_sym) 1);
+by (etac (inf_close_trans3 RS inf_close_sym) 1);
+by (asm_full_simp_tac (simpset() addsimps [hypreal_add_commute]) 1);
+by (etac inf_close_sym 1);
+qed "Infinitesimal_add_right_cancel";
+
+Goal "d + b  @= d + c ==> b @= c";
+by (dtac (inf_close_minus_iff RS iffD1) 1);
+by (asm_full_simp_tac (simpset() addsimps 
+    [hypreal_minus_add_distrib,inf_close_minus_iff RS sym] 
+    @ hypreal_add_ac) 1);
+qed "inf_close_add_left_cancel";
+
+Goal "b + d @= c + d ==> b @= c";
+by (rtac inf_close_add_left_cancel 1);
+by (asm_full_simp_tac (simpset() addsimps 
+    [hypreal_add_commute]) 1);
+qed "inf_close_add_right_cancel";
+
+Goal "b @= c ==> d + b @= d + c";
+by (rtac (inf_close_minus_iff RS iffD2) 1);
+by (asm_full_simp_tac (simpset() addsimps 
+    [hypreal_minus_add_distrib,inf_close_minus_iff RS sym] 
+    @ hypreal_add_ac) 1);
+qed "inf_close_add_mono1";
+
+Goal "b @= c ==> b + a @= c + a";
+by (asm_simp_tac (simpset() addsimps 
+    [hypreal_add_commute,inf_close_add_mono1]) 1);
+qed "inf_close_add_mono2";
+
+Goal "(a + b @= a + c) = (b @= c)";
+by (fast_tac (claset() addEs [inf_close_add_left_cancel,
+    inf_close_add_mono1]) 1);
+qed "inf_close_add_left_iff";
+
+Addsimps [inf_close_add_left_iff];
+
+Goal "(b + a @= c + a) = (b @= c)";
+by (simp_tac (simpset() addsimps [hypreal_add_commute]) 1);
+qed "inf_close_add_right_iff";
+
+Addsimps [inf_close_add_right_iff];
+
+Goal "[| x: HFinite; x @= y |] ==> y: HFinite";
+by (dtac (bex_Infinitesimal_iff2 RS iffD2) 1);
+by (Step_tac 1);
+by (dtac (Infinitesimal_subset_HFinite RS subsetD 
+          RS (HFinite_minus_iff RS iffD2)) 1);
+by (dtac HFinite_add 1);
+by (auto_tac (claset(), simpset() addsimps [hypreal_add_assoc]));
+qed "inf_close_HFinite";
+
+Goal "x @= hypreal_of_real D ==> x: HFinite";
+by (rtac (inf_close_sym RSN (2,inf_close_HFinite)) 1);
+by Auto_tac;
+qed "inf_close_hypreal_of_real_HFinite";
+
+Goal "[|a @= b; c @= d; b: HFinite; d: HFinite|] ==> a*c @= b*d";
+by (rtac inf_close_trans 1); 
+by (rtac inf_close_mult2 2); 
+by (rtac inf_close_mult1 1);
+by (blast_tac (claset() addIs [inf_close_HFinite, inf_close_sym]) 2);  
+by Auto_tac;  
+qed "inf_close_mult_HFinite";
+
+Goal "[|a @= hypreal_of_real b; c @= hypreal_of_real d |] \
+\     ==> a*c @= hypreal_of_real b*hypreal_of_real d";
+by (blast_tac (claset() addSIs [inf_close_mult_HFinite,
+            inf_close_hypreal_of_real_HFinite,HFinite_hypreal_of_real]) 1);
+qed "inf_close_mult_hypreal_of_real";
+
+Goal "[| a: SReal; a ~= #0; a*x @= #0 |] ==> x @= #0";
+by (dtac (SReal_inverse RS (SReal_subset_HFinite RS subsetD)) 1);
+by (auto_tac (claset() addDs [inf_close_mult2],