merged
authorboehmes
Wed, 20 Jul 2011 13:27:01 +0200
changeset 43930 cb7914f6e9b3
parent 43929 61d432e51aff (current diff)
parent 43926 3264fbfd87d6 (diff)
child 43931 c92df8144681
merged
src/HOL/Import/ImportRecorder.thy
src/HOL/Import/importrecorder.ML
src/HOL/Import/lazy_seq.ML
src/HOL/Import/mono_scan.ML
src/HOL/Import/mono_seq.ML
src/HOL/Import/scan.ML
src/HOL/Import/seq.ML
src/HOL/Import/xml.ML
src/HOL/Import/xmlconv.ML
src/HOL/IsaMakefile
src/HOL/Library/Extended_Reals.thy
src/HOL/Library/Nat_Infinity.thy
--- a/src/HOL/HOLCF/FOCUS/Buffer_adm.thy	Wed Jul 20 12:23:20 2011 +0200
+++ b/src/HOL/HOLCF/FOCUS/Buffer_adm.thy	Wed Jul 20 13:27:01 2011 +0200
@@ -8,7 +8,7 @@
 imports Buffer Stream_adm
 begin
 
-declare Fin_0 [simp]
+declare enat_0 [simp]
 
 lemma BufAC_Asm_d2: "a\<leadsto>s:BufAC_Asm ==> ? d. a=Md d"
 by (drule BufAC_Asm_unfold [THEN iffD1], auto)
@@ -116,7 +116,7 @@
 done
 
 (*adm_BufAC_Asm,BufAC_Asm_antiton,adm_non_BufAC_Asm,BufAC_Asm_cong*)
-lemma BufAC_Cmt_2stream_monoP: "f:BufEq ==> ? l. !i x s. s:BufAC_Asm --> x << s --> Fin (l i) < #x --> 
+lemma BufAC_Cmt_2stream_monoP: "f:BufEq ==> ? l. !i x s. s:BufAC_Asm --> x << s --> enat (l i) < #x --> 
                      (x,f\<cdot>x):down_iterate BufAC_Cmt_F i --> 
                      (s,f\<cdot>s):down_iterate BufAC_Cmt_F i"
 apply (rule_tac x="%i. 2*i" in exI)
@@ -139,10 +139,10 @@
        \<lbrakk>f \<in> BufEq;
           \<forall>x s. s \<in> BufAC_Asm \<longrightarrow>
                 x \<sqsubseteq> s \<longrightarrow>
-                Fin (2 * i) < #x \<longrightarrow>
+                enat (2 * i) < #x \<longrightarrow>
                 (x, f\<cdot>x) \<in> down_iterate BufAC_Cmt_F i \<longrightarrow>
                 (s, f\<cdot>s) \<in> down_iterate BufAC_Cmt_F i;
-          Md d\<leadsto>\<bullet>\<leadsto>xa \<in> BufAC_Asm; Fin (2 * i) < #ya; f\<cdot>(Md d\<leadsto>\<bullet>\<leadsto>ya) = d\<leadsto>t;
+          Md d\<leadsto>\<bullet>\<leadsto>xa \<in> BufAC_Asm; enat (2 * i) < #ya; f\<cdot>(Md d\<leadsto>\<bullet>\<leadsto>ya) = d\<leadsto>t;
           (ya, t) \<in> down_iterate BufAC_Cmt_F i; ya \<sqsubseteq> xa\<rbrakk>
        \<Longrightarrow> (xa, rt\<cdot>(f\<cdot>(Md d\<leadsto>\<bullet>\<leadsto>xa))) \<in> down_iterate BufAC_Cmt_F i
 *)
@@ -158,11 +158,11 @@
 apply (erule subst)
 (*
  1. \<And>i d xa ya t ff ffa.
-       \<lbrakk>f\<cdot>(Md d\<leadsto>\<bullet>\<leadsto>ya) = d\<leadsto>ffa\<cdot>ya; Fin (2 * i) < #ya;
+       \<lbrakk>f\<cdot>(Md d\<leadsto>\<bullet>\<leadsto>ya) = d\<leadsto>ffa\<cdot>ya; enat (2 * i) < #ya;
           (ya, ffa\<cdot>ya) \<in> down_iterate BufAC_Cmt_F i; ya \<sqsubseteq> xa; f \<in> BufEq;
           \<forall>x s. s \<in> BufAC_Asm \<longrightarrow>
                 x \<sqsubseteq> s \<longrightarrow>
-                Fin (2 * i) < #x \<longrightarrow>
+                enat (2 * i) < #x \<longrightarrow>
                 (x, f\<cdot>x) \<in> down_iterate BufAC_Cmt_F i \<longrightarrow>
                 (s, f\<cdot>s) \<in> down_iterate BufAC_Cmt_F i;
           xa \<in> BufAC_Asm; ff \<in> BufEq; ffa \<in> BufEq\<rbrakk>
--- a/src/HOL/HOLCF/FOCUS/Fstream.thy	Wed Jul 20 12:23:20 2011 +0200
+++ b/src/HOL/HOLCF/FOCUS/Fstream.thy	Wed Jul 20 13:27:01 2011 +0200
@@ -144,13 +144,13 @@
 by (simp add: fscons_def)
 
 lemma slen_fscons_eq:
-        "(Fin (Suc n) < #x) = (? a y. x = a~> y & Fin n < #y)"
+        "(enat (Suc n) < #x) = (? a y. x = a~> y & enat n < #y)"
 apply (simp add: fscons_def2 slen_scons_eq)
 apply (fast dest: not_Undef_is_Def [THEN iffD1] elim: DefE)
 done
 
 lemma slen_fscons_eq_rev:
-        "(#x < Fin (Suc (Suc n))) = (!a y. x ~= a~> y | #y < Fin (Suc n))"
+        "(#x < enat (Suc (Suc n))) = (!a y. x ~= a~> y | #y < enat (Suc n))"
 apply (simp add: fscons_def2 slen_scons_eq_rev)
 apply (tactic {* step_tac (put_claset HOL_cs @{context} addSEs @{thms DefE}) 1 *})
 apply (tactic {* step_tac (put_claset HOL_cs @{context} addSEs @{thms DefE}) 1 *})
@@ -163,7 +163,7 @@
 done
 
 lemma slen_fscons_less_eq:
-        "(#(a~> y) < Fin (Suc (Suc n))) = (#y < Fin (Suc n))"
+        "(#(a~> y) < enat (Suc (Suc n))) = (#y < enat (Suc n))"
 apply (subst slen_fscons_eq_rev)
 apply (fast dest!: fscons_inject [THEN iffD1])
 done
--- a/src/HOL/HOLCF/FOCUS/Fstreams.thy	Wed Jul 20 12:23:20 2011 +0200
+++ b/src/HOL/HOLCF/FOCUS/Fstreams.thy	Wed Jul 20 13:27:01 2011 +0200
@@ -28,7 +28,7 @@
 
 definition
   jth           :: "nat => 'a fstream => 'a" where
-  "jth = (%n s. if Fin n < #s then THE a. i_th n s = Def a else undefined)"
+  "jth = (%n s. if enat n < #s then THE a. i_th n s = Def a else undefined)"
 
 definition
   first         :: "'a fstream => 'a" where
@@ -36,7 +36,7 @@
 
 definition
   last          :: "'a fstream => 'a" where
-  "last = (%s. case #s of Fin n => (if n~=0 then jth (THE k. Suc k = n) s else undefined))"
+  "last = (%s. case #s of enat n => (if n~=0 then jth (THE k. Suc k = n) s else undefined))"
 
 
 abbreviation
@@ -54,25 +54,25 @@
 lemma ft_fsingleton[simp]: "ft$(<a>) = Def a"
 by (simp add: fsingleton_def2)
 
-lemma slen_fsingleton[simp]: "#(<a>) = Fin 1"
-by (simp add: fsingleton_def2 inat_defs)
+lemma slen_fsingleton[simp]: "#(<a>) = enat 1"
+by (simp add: fsingleton_def2 enat_defs)
 
 lemma slen_fstreams[simp]: "#(<a> ooo s) = iSuc (#s)"
 by (simp add: fsingleton_def2)
 
 lemma slen_fstreams2[simp]: "#(s ooo <a>) = iSuc (#s)"
 apply (cases "#s")
-apply (auto simp add: iSuc_Fin)
+apply (auto simp add: iSuc_enat)
 apply (insert slen_sconc [of _ s "Suc 0" "<a>"], auto)
 by (simp add: sconc_def)
 
 lemma j_th_0_fsingleton[simp]:"jth 0 (<a>) = a"
 apply (simp add: fsingleton_def2 jth_def)
-by (simp add: i_th_def Fin_0)
+by (simp add: i_th_def enat_0)
 
 lemma jth_0[simp]: "jth 0 (<a> ooo s) = a"  
 apply (simp add: fsingleton_def2 jth_def)
-by (simp add: i_th_def Fin_0)
+by (simp add: i_th_def enat_0)
 
 lemma first_sconc[simp]: "first (<a> ooo s) = a"
 by (simp add: first_def)
@@ -80,14 +80,14 @@
 lemma first_fsingleton[simp]: "first (<a>) = a"
 by (simp add: first_def)
 
-lemma jth_n[simp]: "Fin n = #s ==> jth n (s ooo <a>) = a"
+lemma jth_n[simp]: "enat n = #s ==> jth n (s ooo <a>) = a"
 apply (simp add: jth_def, auto)
 apply (simp add: i_th_def rt_sconc1)
-by (simp add: inat_defs split: inat.splits)
+by (simp add: enat_defs split: enat.splits)
 
-lemma last_sconc[simp]: "Fin n = #s ==> last (s ooo <a>) = a"
+lemma last_sconc[simp]: "enat n = #s ==> last (s ooo <a>) = a"
 apply (simp add: last_def)
-apply (simp add: inat_defs split:inat.splits)
+apply (simp add: enat_defs split:enat.splits)
 by (drule sym, auto)
 
 lemma last_fsingleton[simp]: "last (<a>) = a"
@@ -97,18 +97,18 @@
 by (simp add: first_def jth_def)
 
 lemma last_UU[simp]:"last UU = undefined"
-by (simp add: last_def jth_def inat_defs)
+by (simp add: last_def jth_def enat_defs)
 
-lemma last_infinite[simp]:"#s = Infty ==> last s = undefined"
+lemma last_infinite[simp]:"#s = \<infinity> ==> last s = undefined"
 by (simp add: last_def)
 
-lemma jth_slen_lemma1:"n <= k & Fin n = #s ==> jth k s = undefined"
-by (simp add: jth_def inat_defs split:inat.splits, auto)
+lemma jth_slen_lemma1:"n <= k & enat n = #s ==> jth k s = undefined"
+by (simp add: jth_def enat_defs split:enat.splits, auto)
 
 lemma jth_UU[simp]:"jth n UU = undefined" 
 by (simp add: jth_def)
 
-lemma ext_last:"[|s ~= UU; Fin (Suc n) = #s|] ==> (stream_take n$s) ooo <(last s)> = s" 
+lemma ext_last:"[|s ~= UU; enat (Suc n) = #s|] ==> (stream_take n$s) ooo <(last s)> = s" 
 apply (simp add: last_def)
 apply (case_tac "#s", auto)
 apply (simp add: fsingleton_def2)
--- a/src/HOL/HOLCF/FOCUS/Stream_adm.thy	Wed Jul 20 12:23:20 2011 +0200
+++ b/src/HOL/HOLCF/FOCUS/Stream_adm.thy	Wed Jul 20 13:27:01 2011 +0200
@@ -10,14 +10,14 @@
 
 definition
   stream_monoP  :: "(('a stream) set \<Rightarrow> ('a stream) set) \<Rightarrow> bool" where
-  "stream_monoP F = (\<exists>Q i. \<forall>P s. Fin i \<le> #s \<longrightarrow>
+  "stream_monoP F = (\<exists>Q i. \<forall>P s. enat i \<le> #s \<longrightarrow>
                     (s \<in> F P) = (stream_take i\<cdot>s \<in> Q \<and> iterate i\<cdot>rt\<cdot>s \<in> P))"
 
 definition
   stream_antiP  :: "(('a stream) set \<Rightarrow> ('a stream) set) \<Rightarrow> bool" where
   "stream_antiP F = (\<forall>P x. \<exists>Q i.
-                (#x  < Fin i \<longrightarrow> (\<forall>y. x \<sqsubseteq> y \<longrightarrow> y \<in> F P \<longrightarrow> x \<in> F P)) \<and>
-                (Fin i <= #x \<longrightarrow> (\<forall>y. x \<sqsubseteq> y \<longrightarrow>
+                (#x  < enat i \<longrightarrow> (\<forall>y. x \<sqsubseteq> y \<longrightarrow> y \<in> F P \<longrightarrow> x \<in> F P)) \<and>
+                (enat i <= #x \<longrightarrow> (\<forall>y. x \<sqsubseteq> y \<longrightarrow>
                 (y \<in> F P) = (stream_take i\<cdot>y \<in> Q \<and> iterate i\<cdot>rt\<cdot>y \<in> P))))"
 
 definition
@@ -57,7 +57,7 @@
 lemma flatstream_adm_lemma:
   assumes 1: "Porder.chain Y"
   assumes 2: "!i. P (Y i)"
-  assumes 3: "(!!Y. [| Porder.chain Y; !i. P (Y i); !k. ? j. Fin k < #((Y j)::'a::flat stream)|]
+  assumes 3: "(!!Y. [| Porder.chain Y; !i. P (Y i); !k. ? j. enat k < #((Y j)::'a::flat stream)|]
   ==> P(LUB i. Y i))"
   shows "P(LUB i. Y i)"
 apply (rule increasing_chain_adm_lemma [of _ P, OF 1 2])
@@ -74,12 +74,12 @@
 apply (   erule spec)
 apply (  assumption)
 apply ( assumption)
-apply (metis inat_ord_code(4) slen_infinite)
+apply (metis enat_ord_code(4) slen_infinite)
 done
 
 (* should be without reference to stream length? *)
 lemma flatstream_admI: "[|(!!Y. [| Porder.chain Y; !i. P (Y i); 
- !k. ? j. Fin k < #((Y j)::'a::flat stream)|] ==> P(LUB i. Y i))|]==> adm P"
+ !k. ? j. enat k < #((Y j)::'a::flat stream)|] ==> P(LUB i. Y i))|]==> adm P"
 apply (unfold adm_def)
 apply (intro strip)
 apply (erule (1) flatstream_adm_lemma)
@@ -87,13 +87,13 @@
 done
 
 
-(* context (theory "Nat_InFinity");*)
-lemma ile_lemma: "Fin (i + j) <= x ==> Fin i <= x"
+(* context (theory "Extended_Nat");*)
+lemma ile_lemma: "enat (i + j) <= x ==> enat i <= x"
   by (rule order_trans) auto
 
 lemma stream_monoP2I:
 "!!X. stream_monoP F ==> !i. ? l. !x y. 
-  Fin l <= #x --> (x::'a::flat stream) << y --> x:down_iterate F i --> y:down_iterate F i"
+  enat l <= #x --> (x::'a::flat stream) << y --> x:down_iterate F i --> y:down_iterate F i"
 apply (unfold stream_monoP_def)
 apply (safe)
 apply (rule_tac x="i*ia" in exI)
@@ -120,7 +120,7 @@
 done
 
 lemma stream_monoP2_gfp_admI: "[| !i. ? l. !x y. 
- Fin l <= #x --> (x::'a::flat stream) << y --> x:down_iterate F i --> y:down_iterate F i;
+ enat l <= #x --> (x::'a::flat stream) << y --> x:down_iterate F i --> y:down_iterate F i;
     down_cont F |] ==> adm (%x. x:gfp F)"
 apply (erule INTER_down_iterate_is_gfp [THEN ssubst]) (* cont *)
 apply (simp (no_asm))
@@ -153,7 +153,7 @@
 apply (intro strip)
 apply (erule allE, erule all_dupE, erule exE, erule exE)
 apply (erule conjE)
-apply (case_tac "#x < Fin i")
+apply (case_tac "#x < enat i")
 apply ( fast)
 apply (unfold linorder_not_less)
 apply (drule (1) mp)
--- a/src/HOL/HOLCF/IsaMakefile	Wed Jul 20 12:23:20 2011 +0200
+++ b/src/HOL/HOLCF/IsaMakefile	Wed Jul 20 13:27:01 2011 +0200
@@ -134,7 +134,7 @@
 HOLCF-ex: HOLCF $(LOG)/HOLCF-ex.gz
 
 $(LOG)/HOLCF-ex.gz: $(OUT)/HOLCF \
-  ../Library/Nat_Infinity.thy \
+  ../Library/Extended_Nat.thy \
   ex/Concurrency_Monad.thy \
   ex/Dagstuhl.thy \
   ex/Dnat.thy \
--- a/src/HOL/HOLCF/Library/Stream.thy	Wed Jul 20 12:23:20 2011 +0200
+++ b/src/HOL/HOLCF/Library/Stream.thy	Wed Jul 20 13:27:01 2011 +0200
@@ -5,7 +5,7 @@
 header {* General Stream domain *}
 
 theory Stream
-imports HOLCF "~~/src/HOL/Library/Nat_Infinity"
+imports HOLCF "~~/src/HOL/Library/Extended_Nat"
 begin
 
 default_sort pcpo
@@ -22,8 +22,8 @@
                                      If p\<cdot>x then x && h\<cdot>p\<cdot>xs else h\<cdot>p\<cdot>xs)"
 
 definition
-  slen :: "'a stream \<Rightarrow> inat"  ("#_" [1000] 1000) where
-  "#s = (if stream_finite s then Fin (LEAST n. stream_take n\<cdot>s = s) else \<infinity>)"
+  slen :: "'a stream \<Rightarrow> enat"  ("#_" [1000] 1000) where
+  "#s = (if stream_finite s then enat (LEAST n. stream_take n\<cdot>s = s) else \<infinity>)"
 
 
 (* concatenation *)
@@ -39,7 +39,7 @@
 definition
   sconc :: "'a stream => 'a stream => 'a stream"  (infixr "ooo" 65) where
   "s1 ooo s2 = (case #s1 of
-                  Fin n \<Rightarrow> (SOME s. (stream_take n$s=s1) & (i_rt n s = s2))
+                  enat n \<Rightarrow> (SOME s. (stream_take n$s=s1) & (i_rt n s = s2))
                | \<infinity>     \<Rightarrow> s1)"
 
 primrec constr_sconc' :: "nat => 'a stream => 'a stream => 'a stream"
@@ -51,7 +51,7 @@
 definition
   constr_sconc  :: "'a stream => 'a stream => 'a stream" where (* constructive *)
   "constr_sconc s1 s2 = (case #s1 of
-                          Fin n \<Rightarrow> constr_sconc' n s1 s2
+                          enat n \<Rightarrow> constr_sconc' n s1 s2
                         | \<infinity>    \<Rightarrow> s1)"
 
 
@@ -327,12 +327,12 @@
 section "slen"
 
 lemma slen_empty [simp]: "#\<bottom> = 0"
-by (simp add: slen_def stream.finite_def zero_inat_def Least_equality)
+by (simp add: slen_def stream.finite_def zero_enat_def Least_equality)
 
 lemma slen_scons [simp]: "x ~= \<bottom> ==> #(x&&xs) = iSuc (#xs)"
 apply (case_tac "stream_finite (x && xs)")
 apply (simp add: slen_def, auto)
-apply (simp add: stream.finite_def, auto simp add: iSuc_Fin)
+apply (simp add: stream.finite_def, auto simp add: iSuc_enat)
 apply (rule Least_Suc2, auto)
 (*apply (drule sym)*)
 (*apply (drule sym scons_eq_UU [THEN iffD1],simp)*)
@@ -340,13 +340,13 @@
 apply (simp add: slen_def, auto)
 by (drule stream_finite_lemma1,auto)
 
-lemma slen_less_1_eq: "(#x < Fin (Suc 0)) = (x = \<bottom>)"
-by (cases x, auto simp add: Fin_0 iSuc_Fin[THEN sym])
+lemma slen_less_1_eq: "(#x < enat (Suc 0)) = (x = \<bottom>)"
+by (cases x, auto simp add: enat_0 iSuc_enat[THEN sym])
 
 lemma slen_empty_eq: "(#x = 0) = (x = \<bottom>)"
 by (cases x, auto)
 
-lemma slen_scons_eq: "(Fin (Suc n) < #x) = (? a y. x = a && y &  a ~= \<bottom> &  Fin n < #y)"
+lemma slen_scons_eq: "(enat (Suc n) < #x) = (? a y. x = a && y &  a ~= \<bottom> &  enat n < #y)"
 apply (auto, case_tac "x=UU",auto)
 apply (drule stream_exhaust_eq [THEN iffD1], auto)
 apply (case_tac "#y") apply simp_all
@@ -359,18 +359,18 @@
 lemma slen_stream_take_finite [simp]: "#(stream_take n$s) ~= \<infinity>"
 by (simp add: slen_def)
 
-lemma slen_scons_eq_rev: "(#x < Fin (Suc (Suc n))) = (!a y. x ~= a && y |  a = \<bottom> |  #y < Fin (Suc n))"
+lemma slen_scons_eq_rev: "(#x < enat (Suc (Suc n))) = (!a y. x ~= a && y |  a = \<bottom> |  #y < enat (Suc n))"
  apply (cases x, auto)
-   apply (simp add: zero_inat_def)
-  apply (case_tac "#stream") apply (simp_all add: iSuc_Fin)
- apply (case_tac "#stream") apply (simp_all add: iSuc_Fin)
+   apply (simp add: zero_enat_def)
+  apply (case_tac "#stream") apply (simp_all add: iSuc_enat)
+ apply (case_tac "#stream") apply (simp_all add: iSuc_enat)
 done
 
 lemma slen_take_lemma4 [rule_format]:
-  "!s. stream_take n$s ~= s --> #(stream_take n$s) = Fin n"
-apply (induct n, auto simp add: Fin_0)
+  "!s. stream_take n$s ~= s --> #(stream_take n$s) = enat n"
+apply (induct n, auto simp add: enat_0)
 apply (case_tac "s=UU", simp)
-by (drule stream_exhaust_eq [THEN iffD1], auto simp add: iSuc_Fin)
+by (drule stream_exhaust_eq [THEN iffD1], auto simp add: iSuc_enat)
 
 (*
 lemma stream_take_idempotent [simp]:
@@ -390,41 +390,41 @@
 by (simp add: chain_def,simp)
 *)
 
-lemma slen_take_eq: "ALL x. (Fin n < #x) = (stream_take n\<cdot>x ~= x)"
+lemma slen_take_eq: "ALL x. (enat n < #x) = (stream_take n\<cdot>x ~= x)"
 apply (induct_tac n, auto)
-apply (simp add: Fin_0, clarsimp)
+apply (simp add: enat_0, clarsimp)
 apply (drule not_sym)
 apply (drule slen_empty_eq [THEN iffD1], simp)
 apply (case_tac "x=UU", simp)
 apply (drule stream_exhaust_eq [THEN iffD1], clarsimp)
 apply (erule_tac x="y" in allE, auto)
-apply (simp_all add: not_less iSuc_Fin)
+apply (simp_all add: not_less iSuc_enat)
 apply (case_tac "#y") apply simp_all
 apply (case_tac "x=UU", simp)
 apply (drule stream_exhaust_eq [THEN iffD1], clarsimp)
 apply (erule_tac x="y" in allE, simp)
 apply (case_tac "#y") by simp_all
 
-lemma slen_take_eq_rev: "(#x <= Fin n) = (stream_take n\<cdot>x = x)"
+lemma slen_take_eq_rev: "(#x <= enat n) = (stream_take n\<cdot>x = x)"
 by (simp add: linorder_not_less [symmetric] slen_take_eq)
 
-lemma slen_take_lemma1: "#x = Fin n ==> stream_take n\<cdot>x = x"
+lemma slen_take_lemma1: "#x = enat n ==> stream_take n\<cdot>x = x"
 by (rule slen_take_eq_rev [THEN iffD1], auto)
 
 lemma slen_rt_mono: "#s2 <= #s1 ==> #(rt$s2) <= #(rt$s1)"
 apply (cases s1)
  by (cases s2, simp+)+
 
-lemma slen_take_lemma5: "#(stream_take n$s) <= Fin n"
+lemma slen_take_lemma5: "#(stream_take n$s) <= enat n"
 apply (case_tac "stream_take n$s = s")
  apply (simp add: slen_take_eq_rev)
 by (simp add: slen_take_lemma4)
 
-lemma slen_take_lemma2: "!x. ~stream_finite x --> #(stream_take i\<cdot>x) = Fin i"
+lemma slen_take_lemma2: "!x. ~stream_finite x --> #(stream_take i\<cdot>x) = enat i"
 apply (simp add: stream.finite_def, auto)
 by (simp add: slen_take_lemma4)
 
-lemma slen_infinite: "stream_finite x = (#x ~= Infty)"
+lemma slen_infinite: "stream_finite x = (#x ~= \<infinity>)"
 by (simp add: slen_def)
 
 lemma slen_mono_lemma: "stream_finite s ==> ALL t. s << t --> #s <= #t"
@@ -443,19 +443,19 @@
 lemma iterate_lemma: "F$(iterate n$F$x) = iterate n$F$(F$x)"
 by (insert iterate_Suc2 [of n F x], auto)
 
-lemma slen_rt_mult [rule_format]: "!x. Fin (i + j) <= #x --> Fin j <= #(iterate i$rt$x)"
+lemma slen_rt_mult [rule_format]: "!x. enat (i + j) <= #x --> enat j <= #(iterate i$rt$x)"
 apply (induct i, auto)
-apply (case_tac "x=UU", auto simp add: zero_inat_def)
+apply (case_tac "x=UU", auto simp add: zero_enat_def)
 apply (drule stream_exhaust_eq [THEN iffD1], auto)
 apply (erule_tac x="y" in allE, auto)
-apply (simp add: not_le) apply (case_tac "#y") apply (simp_all add: iSuc_Fin)
+apply (simp add: not_le) apply (case_tac "#y") apply (simp_all add: iSuc_enat)
 by (simp add: iterate_lemma)
 
 lemma slen_take_lemma3 [rule_format]:
-  "!(x::'a::flat stream) y. Fin n <= #x --> x << y --> stream_take n\<cdot>x = stream_take n\<cdot>y"
+  "!(x::'a::flat stream) y. enat n <= #x --> x << y --> stream_take n\<cdot>x = stream_take n\<cdot>y"
 apply (induct_tac n, auto)
 apply (case_tac "x=UU", auto)
-apply (simp add: zero_inat_def)
+apply (simp add: zero_enat_def)
 apply (simp add: Suc_ile_eq)
 apply (case_tac "y=UU", clarsimp)
 apply (drule stream_exhaust_eq [THEN iffD1], clarsimp)+
@@ -478,7 +478,7 @@
 apply (subgoal_tac "stream_take n$s ~=s")
  apply (insert slen_take_lemma4 [of n s],auto)
 apply (cases s, simp)
-by (simp add: slen_take_lemma4 iSuc_Fin)
+by (simp add: slen_take_lemma4 iSuc_enat)
 
 (* ----------------------------------------------------------------------- *)
 (* theorems about smap                                                     *)
@@ -546,7 +546,7 @@
 lemma i_rt_mono: "x << s ==> i_rt n x  << i_rt n s"
 by (simp add: i_rt_def monofun_rt_mult)
 
-lemma i_rt_ij_lemma: "Fin (i + j) <= #x ==> Fin j <= #(i_rt i x)"
+lemma i_rt_ij_lemma: "enat (i + j) <= #x ==> enat j <= #(i_rt i x)"
 by (simp add: i_rt_def slen_rt_mult)
 
 lemma slen_i_rt_mono: "#s2 <= #s1 ==> #(i_rt n s2) <= #(i_rt n s1)"
@@ -566,14 +566,14 @@
  apply (subgoal_tac "#(i_rt n s)=0")
   apply (case_tac "stream_take n$s = s",simp+)
   apply (insert slen_take_eq [rule_format,of n s],simp)
-  apply (cases "#s") apply (simp_all add: zero_inat_def)
+  apply (cases "#s") apply (simp_all add: zero_enat_def)
   apply (simp add: slen_take_eq)
   apply (cases "#s")
   using i_rt_take_lemma1 [of n s]
-  apply (simp_all add: zero_inat_def)
+  apply (simp_all add: zero_enat_def)
   done
 
-lemma i_rt_lemma_slen: "#s=Fin n ==> i_rt n s = UU"
+lemma i_rt_lemma_slen: "#s=enat n ==> i_rt n s = UU"
 by (simp add: i_rt_slen slen_take_lemma1)
 
 lemma stream_finite_i_rt [simp]: "stream_finite (i_rt n s) = stream_finite s"
@@ -581,29 +581,29 @@
  apply (cases s, auto simp del: i_rt_Suc)
 by (simp add: i_rt_Suc_back stream_finite_rt_eq)+
 
-lemma take_i_rt_len_lemma: "ALL sl x j t. Fin sl = #x & n <= sl &
-                            #(stream_take n$x) = Fin t & #(i_rt n x)= Fin j
-                                              --> Fin (j + t) = #x"
+lemma take_i_rt_len_lemma: "ALL sl x j t. enat sl = #x & n <= sl &
+                            #(stream_take n$x) = enat t & #(i_rt n x)= enat j
+                                              --> enat (j + t) = #x"
 apply (induct n, auto)
- apply (simp add: zero_inat_def)
+ apply (simp add: zero_enat_def)
 apply (case_tac "x=UU",auto)
- apply (simp add: zero_inat_def)
+ apply (simp add: zero_enat_def)
 apply (drule stream_exhaust_eq [THEN iffD1],clarsimp)
-apply (subgoal_tac "EX k. Fin k = #y",clarify)
+apply (subgoal_tac "EX k. enat k = #y",clarify)
  apply (erule_tac x="k" in allE)
  apply (erule_tac x="y" in allE,auto)
  apply (erule_tac x="THE p. Suc p = t" in allE,auto)
-   apply (simp add: iSuc_def split: inat.splits)
-  apply (simp add: iSuc_def split: inat.splits)
+   apply (simp add: iSuc_def split: enat.splits)
+  apply (simp add: iSuc_def split: enat.splits)
   apply (simp only: the_equality)
- apply (simp add: iSuc_def split: inat.splits)
+ apply (simp add: iSuc_def split: enat.splits)
  apply force
-apply (simp add: iSuc_def split: inat.splits)
+apply (simp add: iSuc_def split: enat.splits)
 done
 
 lemma take_i_rt_len:
-"[| Fin sl = #x; n <= sl; #(stream_take n$x) = Fin t; #(i_rt n x) = Fin j |] ==>
-    Fin (j + t) = #x"
+"[| enat sl = #x; n <= sl; #(stream_take n$x) = enat t; #(i_rt n x) = enat j |] ==>
+    enat (j + t) = #x"
 by (blast intro: take_i_rt_len_lemma [rule_format])
 
 
@@ -690,13 +690,13 @@
 (* ----------------------------------------------------------------------- *)
 
 lemma UU_sconc [simp]: " UU ooo s = s "
-by (simp add: sconc_def zero_inat_def)
+by (simp add: sconc_def zero_enat_def)
 
 lemma scons_neq_UU: "a~=UU ==> a && s ~=UU"
 by auto
 
 lemma singleton_sconc [rule_format, simp]: "x~=UU --> (x && UU) ooo y = x && y"
-apply (simp add: sconc_def zero_inat_def iSuc_def split: inat.splits, auto)
+apply (simp add: sconc_def zero_enat_def iSuc_def split: enat.splits, auto)
 apply (rule someI2_ex,auto)
  apply (rule_tac x="x && y" in exI,auto)
 apply (simp add: i_rt_Suc_forw)
@@ -704,21 +704,21 @@
 by (drule stream_exhaust_eq [THEN iffD1],auto)
 
 lemma ex_sconc [rule_format]:
-  "ALL k y. #x = Fin k --> (EX w. stream_take k$w = x & i_rt k w = y)"
+  "ALL k y. #x = enat k --> (EX w. stream_take k$w = x & i_rt k w = y)"
 apply (case_tac "#x")
  apply (rule stream_finite_ind [of x],auto)
   apply (simp add: stream.finite_def)
   apply (drule slen_take_lemma1,blast)
- apply (simp_all add: zero_inat_def iSuc_def split: inat.splits)
+ apply (simp_all add: zero_enat_def iSuc_def split: enat.splits)
 apply (erule_tac x="y" in allE,auto)
 by (rule_tac x="a && w" in exI,auto)
 
-lemma rt_sconc1: "Fin n = #x ==> i_rt n (x ooo y) = y"
-apply (simp add: sconc_def split: inat.splits, arith?,auto)
+lemma rt_sconc1: "enat n = #x ==> i_rt n (x ooo y) = y"
+apply (simp add: sconc_def split: enat.splits, arith?,auto)
 apply (rule someI2_ex,auto)
 by (drule ex_sconc,simp)
 
-lemma sconc_inj2: "\<lbrakk>Fin n = #x; x ooo y = x ooo z\<rbrakk> \<Longrightarrow> y = z"
+lemma sconc_inj2: "\<lbrakk>enat n = #x; x ooo y = x ooo z\<rbrakk> \<Longrightarrow> y = z"
 apply (frule_tac y=y in rt_sconc1)
 by (auto elim: rt_sconc1)
 
@@ -734,7 +734,7 @@
  apply (simp add: i_rt_slen)
 by (simp add: sconc_def)
 
-lemma stream_take_sconc [simp]: "Fin n = #x ==> stream_take n$(x ooo y) = x"
+lemma stream_take_sconc [simp]: "enat n = #x ==> stream_take n$(x ooo y) = x"
 apply (simp add: sconc_def)
 apply (cases "#x")
 apply auto
@@ -743,7 +743,7 @@
 
 lemma scons_sconc [rule_format,simp]: "a~=UU --> (a && x) ooo y = a && x ooo y"
 apply (cases "#x",auto)
- apply (simp add: sconc_def iSuc_Fin)
+ apply (simp add: sconc_def iSuc_enat)
  apply (rule someI2_ex)
   apply (drule ex_sconc, simp)
  apply (rule someI2_ex, auto)
@@ -799,9 +799,9 @@
 by (cases s, auto)
 
 lemma i_th_sconc_lemma [rule_format]:
-  "ALL x y. Fin n < #x --> i_th n (x ooo y) = i_th n x"
+  "ALL x y. enat n < #x --> i_th n (x ooo y) = i_th n x"
 apply (induct_tac n, auto)
-apply (simp add: Fin_0 i_th_def)
+apply (simp add: enat_0 i_th_def)
 apply (simp add: slen_empty_eq ft_sconc)
 apply (simp add: i_th_def)
 apply (case_tac "x=UU",auto)
@@ -849,16 +849,16 @@
 (* ----------------------------------------------------------------------- *)
 
 lemma slen_sconc_finite1:
-  "[| #(x ooo y) = Infty; Fin n = #x |] ==> #y = Infty"
-apply (case_tac "#y ~= Infty",auto)
+  "[| #(x ooo y) = \<infinity>; enat n = #x |] ==> #y = \<infinity>"
+apply (case_tac "#y ~= \<infinity>",auto)
 apply (drule_tac y=y in rt_sconc1)
 apply (insert stream_finite_i_rt [of n "x ooo y"])
 by (simp add: slen_infinite)
 
-lemma slen_sconc_infinite1: "#x=Infty ==> #(x ooo y) = Infty"
+lemma slen_sconc_infinite1: "#x=\<infinity> ==> #(x ooo y) = \<infinity>"
 by (simp add: sconc_def)
 
-lemma slen_sconc_infinite2: "#y=Infty ==> #(x ooo y) = Infty"
+lemma slen_sconc_infinite2: "#y=\<infinity> ==> #(x ooo y) = \<infinity>"
 apply (case_tac "#x")
  apply (simp add: sconc_def)
  apply (rule someI2_ex)
@@ -868,7 +868,7 @@
  apply (fastsimp simp add: slen_infinite,auto)
 by (simp add: sconc_def)
 
-lemma sconc_finite: "(#x~=Infty & #y~=Infty) = (#(x ooo y)~=Infty)"
+lemma sconc_finite: "(#x~=\<infinity> & #y~=\<infinity>) = (#(x ooo y)~=\<infinity>)"
 apply auto
   apply (metis not_Infty_eq slen_sconc_finite1)
  apply (metis not_Infty_eq slen_sconc_infinite1)
@@ -877,7 +877,7 @@
 
 (* ----------------------------------------------------------------------- *)
 
-lemma slen_sconc_mono3: "[| Fin n = #x; Fin k = #(x ooo y) |] ==> n <= k"
+lemma slen_sconc_mono3: "[| enat n = #x; enat k = #(x ooo y) |] ==> n <= k"
 apply (insert slen_mono [of "x" "x ooo y"])
 apply (cases "#x") apply simp_all
 apply (cases "#(x ooo y)") apply simp_all
@@ -887,10 +887,10 @@
    subsection "finite slen"
 (* ----------------------------------------------------------------------- *)
 
-lemma slen_sconc: "[| Fin n = #x; Fin m = #y |] ==> #(x ooo y) = Fin (n + m)"
+lemma slen_sconc: "[| enat n = #x; enat m = #y |] ==> #(x ooo y) = enat (n + m)"
 apply (case_tac "#(x ooo y)")
  apply (frule_tac y=y in rt_sconc1)
- apply (insert take_i_rt_len [of "THE j. Fin j = #(x ooo y)" "x ooo y" n n m],simp)
+ apply (insert take_i_rt_len [of "THE j. enat j = #(x ooo y)" "x ooo y" n n m],simp)
  apply (insert slen_sconc_mono3 [of n x _ y],simp)
 by (insert sconc_finite [of x y],auto)
 
@@ -934,7 +934,7 @@
 
 lemma contlub_sconc_lemma:
   "chain Y ==> (LUB i. x ooo Y i) = (x ooo (LUB i. Y i))"
-apply (case_tac "#x=Infty")
+apply (case_tac "#x=\<infinity>")
  apply (simp add: sconc_def)
 apply (drule finite_lub_sconc,auto simp add: slen_infinite)
 done
@@ -948,7 +948,7 @@
 (* ----------------------------------------------------------------------- *)
 
 lemma constr_sconc_UUs [simp]: "constr_sconc UU s = s"
-by (simp add: constr_sconc_def zero_inat_def)
+by (simp add: constr_sconc_def zero_enat_def)
 
 lemma "x ooo y = constr_sconc x y"
 apply (case_tac "#x")
@@ -956,7 +956,7 @@
   defer 1
   apply (simp add: constr_sconc_def del: scons_sconc)
   apply (case_tac "#s")
-   apply (simp add: iSuc_Fin)
+   apply (simp add: iSuc_enat)
    apply (case_tac "a=UU",auto simp del: scons_sconc)
    apply (simp)
   apply (simp add: sconc_def)
--- a/src/HOL/Import/HOL4Setup.thy	Wed Jul 20 12:23:20 2011 +0200
+++ b/src/HOL/Import/HOL4Setup.thy	Wed Jul 20 13:27:01 2011 +0200
@@ -2,7 +2,7 @@
     Author:     Sebastian Skalberg (TU Muenchen)
 *)
 
-theory HOL4Setup imports MakeEqual ImportRecorder
+theory HOL4Setup imports MakeEqual
   uses ("proof_kernel.ML") ("replay.ML") ("hol4rews.ML") ("import.ML") begin
 
 section {* General Setup *}
--- a/src/HOL/Import/HOLLight/hollight.imp	Wed Jul 20 12:23:20 2011 +0200
+++ b/src/HOL/Import/HOLLight/hollight.imp	Wed Jul 20 13:27:01 2011 +0200
@@ -1273,9 +1273,9 @@
   "NSUM_ADD_GEN" > "HOLLight.hollight.NSUM_ADD_GEN"
   "NSUM_ADD" > "HOLLight.hollight.NSUM_ADD"
   "NSUM_0" > "HOLLight.hollight.NSUM_0"
-  "NOT_UNIV_PSUBSET" > "Complete_Lattice.complete_lattice_class.not_top_less"
+  "NOT_UNIV_PSUBSET" > "Orderings.top_class.not_top_less"
   "NOT_SUC" > "Nat.Suc_not_Zero"
-  "NOT_PSUBSET_EMPTY" > "Complete_Lattice.complete_lattice_class.not_less_bot"
+  "NOT_PSUBSET_EMPTY" > "Orderings.bot_class.not_less_bot"
   "NOT_ODD" > "HOLLight.hollight.NOT_ODD"
   "NOT_LT" > "Orderings.linorder_class.not_less"
   "NOT_LE" > "Orderings.linorder_class.not_le"
@@ -1644,7 +1644,7 @@
   "IMAGE_INJECTIVE_IMAGE_OF_SUBSET" > "HOLLight.hollight.IMAGE_INJECTIVE_IMAGE_OF_SUBSET"
   "IMAGE_IMP_INJECTIVE_GEN" > "HOLLight.hollight.IMAGE_IMP_INJECTIVE_GEN"
   "IMAGE_IMP_INJECTIVE" > "HOLLight.hollight.IMAGE_IMP_INJECTIVE"
-  "IMAGE_ID" > "Fun.image_ident"
+  "IMAGE_ID" > "Set.image_ident"
   "IMAGE_I" > "Fun.image_id"
   "IMAGE_EQ_EMPTY" > "Set.image_is_empty"
   "IMAGE_DIFF_INJ" > "HOLLight.hollight.IMAGE_DIFF_INJ"
--- a/src/HOL/Import/ImportRecorder.thy	Wed Jul 20 12:23:20 2011 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,4 +0,0 @@
-theory ImportRecorder imports Main 
-uses  "seq.ML" "scan.ML" "mono_seq.ML" "mono_scan.ML" "lazy_seq.ML" "xml.ML" "xmlconv.ML" "importrecorder.ML"
-begin
-end
\ No newline at end of file
--- a/src/HOL/Import/import_syntax.ML	Wed Jul 20 12:23:20 2011 +0200
+++ b/src/HOL/Import/import_syntax.ML	Wed Jul 20 13:27:01 2011 +0200
@@ -19,9 +19,6 @@
     val const_moves   : (theory -> theory) parser
     val const_renames : (theory -> theory) parser
 
-    val skip_import_dir : (theory -> theory) parser
-    val skip_import     : (theory -> theory) parser
-
     val setup        : unit -> unit
 end
 
@@ -39,13 +36,6 @@
                                       |> Sign.add_path thyname
                                       |> add_dump (";setup_theory " ^ thyname))
 
-fun do_skip_import_dir s = (ImportRecorder.set_skip_import_dir (SOME s); I: theory -> theory)
-val skip_import_dir = Parse.string >> do_skip_import_dir
-
-val lower = String.map Char.toLower
-fun do_skip_import s = (ImportRecorder.set_skip_import (case lower s of "on" => true | "off" => false | _ => Scan.fail ()); I: theory -> theory)
-val skip_import = Parse.short_ident >> do_skip_import
-
 fun end_import toks =
     Scan.succeed
         (fn thy =>
@@ -54,16 +44,7 @@
                 val segname = get_import_segment thy
                 val (int_thms,facts) = Replay.setup_int_thms thyname thy
                 val thmnames = filter_out (should_ignore thyname thy) facts
-                fun replay thy = 
-                    let
-                        val _ = ImportRecorder.load_history thyname
-                        val thy = Replay.import_thms thyname int_thms thmnames thy
-                            (*handle x => (ImportRecorder.save_history thyname; raise x)*)  (* FIXME avoid handle x ?? *)
-                        val _ = ImportRecorder.save_history thyname
-                        val _ = ImportRecorder.clear_history ()
-                    in
-                        thy
-                    end                                 
+                fun replay thy = Replay.import_thms thyname int_thms thmnames thy
             in
                 thy |> clear_import_thy
                     |> set_segment thyname segname
@@ -225,12 +206,6 @@
    Outer_Syntax.command "end_import"
                        "End HOL4 import"
                        Keyword.thy_decl (end_import >> Toplevel.theory);
-   Outer_Syntax.command "skip_import_dir" 
-                       "Sets caching directory for skipping importing"
-                       Keyword.thy_decl (skip_import_dir >> Toplevel.theory);
-   Outer_Syntax.command "skip_import" 
-                       "Switches skipping importing on or off"
-                       Keyword.thy_decl (skip_import >> Toplevel.theory);                   
    Outer_Syntax.command "setup_theory"
                        "Setup HOL4 theory replaying"
                        Keyword.thy_decl (setup_theory >> Toplevel.theory);
--- a/src/HOL/Import/importrecorder.ML	Wed Jul 20 12:23:20 2011 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,265 +0,0 @@
-signature IMPORT_RECORDER =
-sig
-
-    datatype deltastate = Consts of (string * typ * mixfix) list
-                        | Specification of (string * string * bool) list * term
-                        | Hol_mapping of string * string * string
-                        | Hol_pending of string * string * term
-                        | Hol_const_mapping of string * string * string
-                        | Hol_move of string * string
-                        | Defs of string * term
-                        | Hol_theorem of string * string * term
-                        | Typedef of string option * (string * string list * mixfix) * term * (string * string) option * term 
-                        | Hol_type_mapping of string * string * string
-                        | Indexed_theorem of int * term
-                        | Protect_varname of string * string
-                        | Dump of string
-
-    datatype history = History of history_entry list
-         and history_entry = ThmEntry of string*string*bool*history | DeltaEntry of deltastate list
-
-    val get_history : unit -> history
-    val set_history : history -> unit
-    val clear_history : unit -> unit
-                                      
-    val start_replay_proof : string -> string -> unit
-    val stop_replay_proof : string -> string -> unit
-    val abort_replay_proof : string -> string -> unit
-
-    val add_consts : (string * typ * mixfix) list -> unit
-    val add_specification : (string * string * bool) list -> thm -> unit
-    val add_hol_mapping : string -> string -> string -> unit
-    val add_hol_pending : string -> string -> thm -> unit
-    val add_hol_const_mapping : string -> string -> string -> unit
-    val add_hol_move : string -> string -> unit
-    val add_defs : string -> term -> unit
-    val add_hol_theorem : string -> string -> thm -> unit
-    val add_typedef :  string option -> string * string list * mixfix -> term -> (string * string) option -> thm -> unit
-    val add_hol_type_mapping : string -> string -> string -> unit
-    val add_indexed_theorem : int -> thm -> unit
-    val protect_varname : string -> string -> unit
-    val add_dump : string -> unit
-
-    val set_skip_import_dir : string option -> unit
-    val get_skip_import_dir : unit -> string option
-
-    val set_skip_import : bool -> unit
-    val get_skip_import : unit -> bool
-
-    val save_history : string -> unit
-    val load_history : string -> unit
-end
-
-structure ImportRecorder :> IMPORT_RECORDER  =
-struct
-
-datatype deltastate = Consts of (string * typ * mixfix) list
-                    | Specification of (string * string * bool) list * term
-                    | Hol_mapping of string * string * string
-                    | Hol_pending of string * string * term
-                    | Hol_const_mapping of string * string * string
-                    | Hol_move of string * string
-                    | Defs of string * term
-                    | Hol_theorem of string * string * term
-                    | Typedef of string option * (string * string list * mixfix) * term * (string * string) option * term 
-                    | Hol_type_mapping of string * string * string
-                    | Indexed_theorem of int * term
-                    | Protect_varname of string * string
-                    | Dump of string
-
-datatype history_entry = StartReplay of string*string  
-                       | StopReplay of string*string
-                       | AbortReplay of string*string
-                       | Delta of deltastate list
-
-val history = Unsynchronized.ref ([]:history_entry list)
-val history_dir = Unsynchronized.ref (SOME "")
-val skip_imports = Unsynchronized.ref false
-
-fun set_skip_import b = skip_imports := b
-fun get_skip_import () = !skip_imports
-
-fun clear_history () = history := []
-
-fun add_delta d = history := (case !history of (Delta ds)::hs => (Delta (d::ds))::hs | hs => (Delta [d])::hs)
-fun add_replay r = history := (r :: (!history))
-
-local 
-    open XMLConvOutput
-    val consts = list (triple string typ mixfix)
-    val specification = pair (list (triple string string bool)) term
-    val hol_mapping = triple string string string
-    val hol_pending = triple string string term
-    val hol_const_mapping = triple string string string
-    val hol_move = pair string string
-    val defs = pair string term
-    val hol_theorem = triple string string term
-    val typedef = quintuple (option string) (triple string (list string) mixfix) term (option (pair string string)) term
-    val hol_type_mapping = triple string string string
-    val indexed_theorem = pair int term
-    val entry = pair string string
-
-    fun delta (Consts args) = wrap "consts" consts args
-      | delta (Specification args) = wrap "specification" specification args
-      | delta (Hol_mapping args) = wrap "hol_mapping" hol_mapping args
-      | delta (Hol_pending args) = wrap "hol_pending" hol_pending args
-      | delta (Hol_const_mapping args) = wrap "hol_const_mapping" hol_const_mapping args
-      | delta (Hol_move args) = wrap "hol_move" hol_move args
-      | delta (Defs args) = wrap "defs" defs args
-      | delta (Hol_theorem args) = wrap "hol_theorem" hol_theorem args
-      | delta (Typedef args) = wrap "typedef" typedef args
-      | delta (Hol_type_mapping args) = wrap "hol_type_mapping" hol_type_mapping args
-      | delta (Indexed_theorem args) = wrap "indexed_theorem" indexed_theorem args
-      | delta (Protect_varname args) = wrap "protect_varname" entry args
-      | delta (Dump args) = wrap "dump" string args
-
-    fun history_entry (StartReplay args) = wrap "startreplay" entry args
-      | history_entry (StopReplay args) = wrap "stopreplay" entry args
-      | history_entry (AbortReplay args) = wrap "abortreplay" entry args
-      | history_entry (Delta args) = wrap "delta" (list delta) args
-in
-
-val xml_of_history = list history_entry
-
-end
-
-local 
-    open XMLConvInput
-    val consts = list (triple string typ mixfix)
-    val specification = pair (list (triple string string bool)) term
-    val hol_mapping = triple string string string
-    val hol_pending = triple string string term
-    val hol_const_mapping = triple string string string
-    val hol_move = pair string string
-    val defs = pair string term
-    val hol_theorem = triple string string term
-    val typedef = quintuple (option string) (triple string (list string) mixfix) term (option (pair string string)) term
-    val hol_type_mapping = triple string string string
-    val indexed_theorem = pair int term
-    val entry = pair string string
-
-    fun delta "consts" = unwrap Consts consts
-      | delta "specification" = unwrap Specification specification 
-      | delta "hol_mapping" = unwrap Hol_mapping hol_mapping 
-      | delta "hol_pending" = unwrap Hol_pending hol_pending
-      | delta "hol_const_mapping" = unwrap Hol_const_mapping hol_const_mapping
-      | delta "hol_move" = unwrap Hol_move hol_move
-      | delta "defs" = unwrap Defs defs
-      | delta "hol_theorem" = unwrap Hol_theorem hol_theorem
-      | delta "typedef" = unwrap Typedef typedef
-      | delta "hol_type_mapping" = unwrap Hol_type_mapping hol_type_mapping
-      | delta "indexed_theorem" = unwrap Indexed_theorem indexed_theorem
-      | delta "protect_varname" = unwrap Protect_varname entry
-      | delta "dump" = unwrap Dump string
-      | delta _ = raise ParseException "delta"
-
-    val delta = fn e => delta (name_of_wrap e) e
-
-    fun history_entry "startreplay" = unwrap StartReplay entry 
-      | history_entry "stopreplay" = unwrap StopReplay entry
-      | history_entry "abortreplay" = unwrap AbortReplay entry
-      | history_entry "delta" = unwrap Delta (list delta)
-      | history_entry _ = raise ParseException "history_entry"
-
-    val history_entry = fn e => history_entry (name_of_wrap e) e
-in
-
-val history_of_xml = list history_entry
-
-end
-
-fun start_replay_proof thyname thmname = add_replay (StartReplay (thyname, thmname))
-fun stop_replay_proof thyname thmname = add_replay (StopReplay (thyname, thmname))
-fun abort_replay_proof thyname thmname = add_replay (AbortReplay (thyname, thmname))
-fun add_hol_theorem thyname thmname thm = add_delta (Hol_theorem (thyname, thmname, prop_of thm))
-fun add_hol_mapping thyname thmname isaname = add_delta (Hol_mapping (thyname, thmname, isaname))
-fun add_consts consts = add_delta (Consts consts)
-fun add_typedef thmname_opt typ c absrep_opt th = add_delta (Typedef (thmname_opt, typ, c, absrep_opt, prop_of th))
-fun add_defs thmname eq = add_delta (Defs (thmname, eq)) 
-fun add_hol_const_mapping thyname constname fullcname = add_delta (Hol_const_mapping (thyname, constname, fullcname))
-fun add_hol_move fullname moved_thmname = add_delta (Hol_move (fullname, moved_thmname))
-fun add_hol_type_mapping thyname tycname fulltyname = add_delta (Hol_type_mapping (thyname, tycname, fulltyname))
-fun add_hol_pending thyname thmname th = add_delta (Hol_pending (thyname, thmname, prop_of th))
-fun add_specification names th = add_delta (Specification (names, prop_of th)) 
-fun add_indexed_theorem i th = add_delta (Indexed_theorem (i, prop_of th))
-fun protect_varname s t = add_delta (Protect_varname (s,t))
-fun add_dump s = add_delta (Dump s)
-                            
-fun set_skip_import_dir dir = (history_dir := dir)
-fun get_skip_import_dir () = !history_dir
-
-fun get_filename thyname = Path.implode (Path.append (Path.explode (the (get_skip_import_dir ()))) (Path.explode (thyname^".history")))
-
-fun save_history thyname = 
-    if get_skip_import () then
-        XMLConv.write_to_file xml_of_history (get_filename thyname) (!history)
-    else 
-        ()
-
-fun load_history thyname = 
-    if get_skip_import () then
-        let 
-            val filename = get_filename thyname
-            val _ = writeln "load_history / before"
-            val _ = 
-                if File.exists (Path.explode filename) then                                     
-                    (history := XMLConv.read_from_file history_of_xml (get_filename thyname)) 
-                else
-                    clear_history ()
-            val _ = writeln "load_history / after"
-        in
-            ()
-        end
-    else
-        ()
-    
- 
-datatype history = History of history_entry list
-     and history_entry = ThmEntry of string*string*bool*history | DeltaEntry of deltastate list
-
-exception CONV 
-
-fun conv_histories ((StartReplay (thyname, thmname))::rest) = 
-    let
-        val (hs, rest) = conv_histories rest
-        fun continue thyname' thmname' aborted rest = 
-            if thyname = thyname' andalso thmname = thmname' then
-                let
-                    val (hs', rest) = conv_histories rest
-                in
-                    ((ThmEntry (thyname, thmname, aborted, History hs))::hs', rest)
-                end
-            else
-                raise CONV 
-    in
-        case rest of 
-            (StopReplay (thyname',thmname'))::rest =>
-            continue thyname' thmname' false rest
-          | (AbortReplay (thyname',thmname'))::rest =>
-            continue thyname' thmname' true rest
-          | [] => (hs, [])
-          | _ => raise CONV
-    end
-  | conv_histories ((Delta ds)::rest) = (conv_histories rest) |>> (fn hs => (DeltaEntry (rev ds))::hs)  
-  | conv_histories rest = ([], rest)
-
-fun conv es = 
-    let
-        val (h, rest) = conv_histories (rev es)
-    in
-        case rest of
-            [] => h
-          | _ => raise CONV
-    end 
-
-fun get_history () = History (conv (!history))
-
-fun reconv (History hs) rs = fold reconv_entry hs rs
-and reconv_entry (ThmEntry (thyname, thmname, aborted, h)) rs =
-    ((if aborted then AbortReplay else StopReplay) (thyname, thmname)) :: (reconv h ((StartReplay (thyname, thmname))::rs))
-  | reconv_entry (DeltaEntry ds) rs = (Delta (rev ds))::rs
-    
-fun set_history h = history := reconv h []
-
-
-end
--- a/src/HOL/Import/lazy_seq.ML	Wed Jul 20 12:23:20 2011 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,551 +0,0 @@
-(*  Title:      HOL/Import/lazy_seq.ML
-    Author:     Sebastian Skalberg, TU Muenchen
-
-Alternative version of lazy sequences.
-*)
-
-signature LAZY_SEQ =
-sig
-
-    include EXTENDED_SCANNER_SEQ
-
-    (* From LIST *)
-
-    val fromString : string -> string seq
-    val @ : 'a seq * 'a seq -> 'a seq
-    val hd : 'a seq -> 'a
-    val tl : 'a seq -> 'a seq
-    val last : 'a seq -> 'a
-    val getItem : 'a seq -> ('a * 'a seq) option
-    val nth : 'a seq * int -> 'a
-    val take : 'a seq * int -> 'a seq
-    val drop : 'a seq * int -> 'a seq
-    val rev : 'a seq -> 'a seq
-    val concat : 'a seq seq -> 'a seq
-    val revAppend : 'a seq * 'a seq -> 'a seq
-    val app : ('a -> unit) -> 'a seq -> unit
-    val map : ('a -> 'b) -> 'a seq -> 'b seq
-    val mapPartial : ('a -> 'b option) -> 'a seq -> 'b seq
-    val find : ('a -> bool) -> 'a seq -> 'a option
-    val filter : ('a -> bool) -> 'a seq -> 'a seq
-    val partition : ('a -> bool)
-                      -> 'a seq -> 'a seq * 'a seq
-    val foldl : ('a * 'b -> 'b) -> 'b -> 'a seq -> 'b
-    val foldr : ('a * 'b -> 'b) -> 'b -> 'a seq -> 'b
-    val exists : ('a -> bool) -> 'a seq -> bool
-    val all : ('a -> bool) -> 'a seq -> bool
-    val tabulate : int * (int -> 'a) -> 'a seq
-    val collate : ('a * 'a -> order)
-                    -> 'a seq * 'a seq -> order 
-
-    (* Miscellaneous *)
-
-    val cycle      : ((unit ->'a seq) -> 'a seq) -> 'a seq
-    val iterates   : ('a -> 'a) -> 'a -> 'a seq
-    val of_function: (unit -> 'a option) -> 'a seq
-    val of_string  : string -> char seq
-    val of_instream: TextIO.instream -> char seq
-
-    (* From SEQ *)
-
-    val make: (unit -> ('a * 'a seq) option) -> 'a seq
-    val empty: 'a -> 'a seq
-    val cons: 'a * 'a seq -> 'a seq
-    val single: 'a -> 'a seq
-    val try: ('a -> 'b) -> 'a -> 'b seq
-    val chop: int * 'a seq -> 'a list * 'a seq
-    val list_of: 'a seq -> 'a list
-    val of_list: 'a list -> 'a seq
-    val mapp: ('a -> 'b) -> 'a seq -> 'b seq -> 'b seq
-    val interleave: 'a seq * 'a seq -> 'a seq
-    val print: (int -> 'a -> unit) -> int -> 'a seq -> unit
-    val it_right : ('a * 'b seq -> 'b seq) -> 'a seq * 'b seq -> 'b seq
-    val commute: 'a seq list -> 'a list seq
-    val succeed: 'a -> 'a seq
-    val fail: 'a -> 'b seq
-    val THEN: ('a -> 'b seq) * ('b -> 'c seq) -> 'a -> 'c seq
-    val ORELSE: ('a -> 'b seq) * ('a -> 'b seq) -> 'a -> 'b seq
-    val APPEND: ('a -> 'b seq) * ('a -> 'b seq) -> 'a -> 'b seq
-    val EVERY: ('a -> 'a seq) list -> 'a -> 'a seq
-    val FIRST: ('a -> 'b seq) list -> 'a -> 'b seq
-    val TRY: ('a -> 'a seq) -> 'a -> 'a seq
-    val REPEAT: ('a -> 'a seq) -> 'a -> 'a seq
-    val REPEAT1: ('a -> 'a seq) -> 'a -> 'a seq
-    val INTERVAL: (int -> 'a -> 'a seq) -> int -> int -> 'a -> 'a seq
-    val DETERM: ('a -> 'b seq) -> 'a -> 'b seq
-
-end
-
-structure LazySeq :> LAZY_SEQ =
-struct
-
-datatype 'a seq = Seq of ('a * 'a seq) option lazy
-
-exception Empty
-
-fun getItem (Seq s) = Lazy.force s
-val pull = getItem
-fun make f = Seq (Lazy.lazy f)
-
-fun null s = is_none (getItem s)
-
-local
-    fun F n NONE = n
-      | F n (SOME(_,s)) = F (n+1) (getItem s)
-in
-fun length s = F 0 (getItem s)
-end
-
-fun s1 @ s2 =
-    let
-        fun F NONE = getItem s2
-          | F (SOME(x,s1')) = SOME(x,F' s1')
-        and F' s = make (fn () => F (getItem s))
-    in
-        F' s1
-    end
-
-local
-    fun F NONE = raise Empty
-      | F (SOME arg) = arg
-in
-fun hd s = #1 (F (getItem s))
-fun tl s = #2 (F (getItem s))
-end
-
-local
-    fun F x NONE = x
-      | F _ (SOME(x,s)) = F x (getItem s)
-    fun G NONE = raise Empty
-      | G (SOME(x,s)) = F x (getItem s)
-in
-fun last s = G (getItem s)
-end
-
-local
-    fun F NONE _ = raise Subscript
-      | F (SOME(x,_)) 0 = x
-      | F (SOME(_,s)) n = F (getItem s) (n-1)
-in
-fun nth(s,n) =
-    if n < 0
-    then raise Subscript
-    else F (getItem s) n
-end
-
-local
-    fun F NONE _ = raise Subscript
-      | F (SOME(x,s)) n = SOME(x,F' s (n-1))
-    and F' s 0 = Seq (Lazy.value NONE)
-      | F' s n = make (fn () => F (getItem s) n)
-in
-fun take (s,n) =
-    if n < 0
-    then raise Subscript
-    else F' s n
-end
-
-fun take_at_most s n = 
-    if n <= 0 then [] else
-    case getItem s of 
-        NONE => []
-      | SOME (x,s') => x::(take_at_most s' (n-1))
-
-local
-    fun F s 0 = s
-      | F NONE _ = raise Subscript
-      | F (SOME(_,s)) n = F (getItem s) (n-1)
-in
-fun drop (s,0) = s
-  | drop (s,n) = 
-    if n < 0
-    then raise Subscript
-    else make (fn () => F (getItem s) n)
-end
-
-local
-    fun F s NONE = s
-      | F s (SOME(x,s')) = F (SOME(x, Seq (Lazy.value s))) (getItem s')
-in
-fun rev s = make (fn () => F NONE (getItem s))
-end
-
-local
-    fun F s NONE = getItem s
-      | F s (SOME(x,s')) = F (Seq (Lazy.value (SOME(x,s)))) (getItem s')
-in
-fun revAppend (s1,s2) = make (fn () => F s2 (getItem s1))
-end
-
-local
-    fun F NONE = NONE
-      | F (SOME(s1,s2)) =
-        let
-            fun G NONE = F (getItem s2)
-              | G (SOME(x,s)) = SOME(x,make (fn () => G (getItem s)))
-        in
-            G (getItem s1)
-        end
-in
-fun concat s = make (fn () => F (getItem s))
-end
-
-fun app f =
-    let
-        fun F NONE = ()
-          | F (SOME(x,s)) =
-            (f x;
-             F (getItem s))
-    in
-        F o getItem
-    end
-
-fun map f =
-    let
-        fun F NONE = NONE
-          | F (SOME(x,s)) = SOME(f x,F' s)
-        and F' s = make (fn() => F (getItem s))
-    in
-        F'
-    end
-
-fun mapPartial f =
-    let
-        fun F NONE = NONE
-          | F (SOME(x,s)) =
-            (case f x of
-                 SOME y => SOME(y,F' s)
-               | NONE => F (getItem s))
-        and F' s = make (fn()=> F (getItem s))
-    in
-        F'
-    end
-
-fun find P =
-    let
-        fun F NONE = NONE
-          | F (SOME(x,s)) =
-            if P x
-            then SOME x
-            else F (getItem s)
-    in
-        F o getItem
-    end
-
-(*fun filter p = mapPartial (fn x => if p x then SOME x else NONE)*)
-
-fun filter P =
-    let
-        fun F NONE = NONE
-          | F (SOME(x,s)) =
-            if P x
-            then SOME(x,F' s)
-            else F (getItem s)
-        and F' s = make (fn () => F (getItem s))
-    in
-        F'
-    end
-
-fun partition f s =
-    let
-        val s' = map (fn x => (x,f x)) s
-    in
-        (mapPartial (fn (x,true) => SOME x | _ => NONE) s',
-         mapPartial (fn (x,false) => SOME x | _ => NONE) s')
-    end
-
-fun exists P =
-    let
-        fun F NONE = false
-          | F (SOME(x,s)) = P x orelse F (getItem s)
-    in
-        F o getItem
-    end
-
-fun all P =
-    let
-        fun F NONE = true
-          | F (SOME(x,s)) = P x andalso F (getItem s)
-    in
-        F o getItem
-    end
-
-(*fun tabulate f = map f (iterates (fn x => x + 1) 0)*)
-
-fun tabulate (n,f) =
-    let
-        fun F n = make (fn () => SOME(f n,F (n+1)))
-    in
-        F n
-    end
-
-fun collate c (s1,s2) =
-    let
-        fun F NONE _ = LESS
-          | F _ NONE = GREATER
-          | F (SOME(x,s1)) (SOME(y,s2)) =
-            (case c (x,y) of
-                 LESS => LESS
-               | GREATER => GREATER
-               | EQUAL => F' s1 s2)
-        and F' s1 s2 = F (getItem s1) (getItem s2)
-    in
-        F' s1 s2
-    end
-
-fun empty  _ = Seq (Lazy.value NONE)
-fun single x = Seq(Lazy.value (SOME(x,Seq (Lazy.value NONE))))
-fun cons a = Seq(Lazy.value (SOME a))
-
-fun cycle seqfn =
-    let
-        val knot = Unsynchronized.ref (Seq (Lazy.value NONE))
-    in
-        knot := seqfn (fn () => !knot);
-        !knot
-    end
-
-fun iterates f =
-    let
-        fun F x = make (fn() => SOME(x,F (f x)))
-    in
-        F
-    end
-
-fun interleave (s1,s2) =
-    let
-        fun F NONE = getItem s2
-          | F (SOME(x,s1')) = SOME(x,interleave(s2,s1'))
-    in
-        make (fn()=> F (getItem s1))
-    end
-
-(* val force_all = app ignore *)
-
-local
-    fun F NONE = ()
-      | F (SOME(x,s)) = F (getItem s)
-in
-fun force_all s = F (getItem s)
-end
-
-fun of_function f =
-    let
-        fun F () = case f () of
-                       SOME x => SOME(x,make F)
-                     | NONE => NONE
-    in
-        make F
-    end
-
-local
-    fun F [] = NONE
-      | F (x::xs) = SOME(x,F' xs)
-    and F' xs = make (fn () => F xs)
-in
-fun of_list xs = F' xs
-end
-
-val of_string = of_list o String.explode
-
-fun of_instream is =
-    let
-        val buffer : char list Unsynchronized.ref = Unsynchronized.ref []
-        fun get_input () =
-            case !buffer of
-                (c::cs) => (buffer:=cs;
-                            SOME c)
-              | [] => (case String.explode (TextIO.input is) of
-                           [] => NONE
-                         | (c::cs) => (buffer := cs;
-                                       SOME c))
-    in
-        of_function get_input
-    end
-
-local
-    fun F k NONE = k []
-      | F k (SOME(x,s)) = F (fn xs => k (x::xs)) (getItem s)
-in
-fun list_of s = F (fn x => x) (getItem s)
-end
-
-(* Adapted from seq.ML *)
-
-val succeed = single
-fun fail _ = Seq (Lazy.value NONE)
-
-(* fun op THEN (f, g) x = flat (map g (f x)) *)
-
-fun op THEN (f, g) =
-    let
-        fun F NONE = NONE
-          | F (SOME(x,xs)) =
-            let
-                fun G NONE = F (getItem xs)
-                  | G (SOME(y,ys)) = SOME(y,make (fn () => G (getItem ys)))
-            in
-                G (getItem (g x))
-            end
-    in
-        fn x => make (fn () => F (getItem (f x)))
-    end
-
-fun op ORELSE (f, g) x =
-    make (fn () =>
-             case getItem (f x) of
-                 NONE => getItem (g x)
-               | some => some)
-
-fun op APPEND (f, g) x =
-    let
-        fun copy s =
-            case getItem s of
-                NONE => getItem (g x)
-              | SOME(x,xs) => SOME(x,make (fn () => copy xs))
-    in
-        make (fn () => copy (f x))
-    end
-
-fun EVERY fs = fold_rev (curry op THEN) fs succeed
-fun FIRST fs = fold_rev (curry op ORELSE) fs fail
-
-fun TRY f x =
-    make (fn () =>
-             case getItem (f x) of
-                 NONE => SOME(x,Seq (Lazy.value NONE))
-               | some => some)
-
-fun REPEAT f =
-    let
-        fun rep qs x =
-            case getItem (f x) of
-                NONE => SOME(x, make (fn () => repq qs))
-              | SOME (x', q) => rep (q :: qs) x'
-        and repq [] = NONE
-          | repq (q :: qs) =
-            case getItem q of
-                NONE => repq qs
-              | SOME (x, q) => rep (q :: qs) x
-    in
-     fn x => make (fn () => rep [] x)
-    end
-
-fun REPEAT1 f = op THEN (f, REPEAT f);
-
-fun INTERVAL f i =
-    let
-        fun F j =
-            if i > j
-            then single
-            else op THEN (f j, F (j - 1))
-    in F end
-
-fun DETERM f x =
-    make (fn () =>
-             case getItem (f x) of
-                 NONE => NONE
-               | SOME (x', _) => SOME(x',Seq (Lazy.value NONE)))
-
-(*partial function as procedure*)
-fun try f x =
-    make (fn () =>
-             case Basics.try f x of
-                 SOME y => SOME(y,Seq (Lazy.value NONE))
-               | NONE => NONE)
-
-(*functional to print a sequence, up to "count" elements;
-  the function prelem should print the element number and also the element*)
-fun print prelem count seq =
-    let
-        fun pr k xq =
-            if k > count
-            then ()
-            else
-                case getItem xq of
-                    NONE => ()
-                  | SOME (x, xq') =>
-                    (prelem k x;
-                     writeln "";
-                     pr (k + 1) xq')
-    in
-        pr 1 seq
-    end
-
-(*accumulating a function over a sequence; this is lazy*)
-fun it_right f (xq, yq) =
-    let
-        fun its s =
-            make (fn () =>
-                     case getItem s of
-                         NONE => getItem yq
-                       | SOME (a, s') => getItem(f (a, its s')))
-    in
-        its xq
-    end
-
-(*map over a sequence s1, append the sequence s2*)
-fun mapp f s1 s2 =
-    let
-        fun F NONE = getItem s2
-          | F (SOME(x,s1')) = SOME(f x,F' s1')
-        and F' s = make (fn () => F (getItem s))
-    in
-        F' s1
-    end
-
-(*turn a list of sequences into a sequence of lists*)
-local
-    fun F [] = SOME([],Seq (Lazy.value NONE))
-      | F (xq :: xqs) =
-        case getItem xq of
-            NONE => NONE
-          | SOME (x, xq') =>
-            (case F xqs of
-                 NONE => NONE
-               | SOME (xs, xsq) =>
-                 let
-                     fun G s =
-                         make (fn () =>
-                                  case getItem s of
-                                      NONE => F (xq' :: xqs)
-                                    | SOME(ys,ysq) => SOME(x::ys,G ysq))
-                 in
-                     SOME (x :: xs, G xsq)
-                 end)
-in
-fun commute xqs = make (fn () => F xqs)
-end
-
-(*the list of the first n elements, paired with rest of sequence;
-  if length of list is less than n, then sequence had less than n elements*)
-fun chop (n, xq) =
-    if n <= 0
-    then ([], xq)
-    else
-        case getItem xq of
-            NONE => ([], xq)
-          | SOME (x, xq') => apfst (Basics.cons x) (chop (n - 1, xq'))
-
-fun foldl f e s =
-    let
-        fun F k NONE = k e
-          | F k (SOME(x,s)) = F (fn y => k (f(x,y))) (getItem s)
-    in
-        F (fn x => x) (getItem s)
-    end
-
-fun foldr f e s =
-    let
-        fun F e NONE = e
-          | F e (SOME(x,s)) = F (f(x,e)) (getItem s)
-    in
-        F e (getItem s)
-    end
-
-fun fromString s = of_list (raw_explode s)
-
-end
-
-structure LazyScan = Scanner (structure Seq = LazySeq)
-
--- a/src/HOL/Import/mono_scan.ML	Wed Jul 20 12:23:20 2011 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,237 +0,0 @@
-(*  Title:      HOL/Import/mono_scan.ML
-    Author:     Steven Obua, TU Muenchen
-
-Monomorphic scanner combinators for monomorphic sequences.
-*)
-
-signature MONO_SCANNER =
-sig
-
-    include MONO_SCANNER_SEQ
-
-    exception SyntaxError
-
-    type 'a scanner = seq -> 'a * seq
-
-    val :--      : 'a scanner * ('a -> 'b scanner)
-                   -> ('a*'b) scanner
-    val --       : 'a scanner * 'b scanner -> ('a * 'b) scanner
-    val >>       : 'a scanner * ('a -> 'b) -> 'b scanner
-    val --|      : 'a scanner * 'b scanner -> 'a scanner
-    val |--      : 'a scanner * 'b scanner -> 'b scanner
-    val ^^       : string scanner * string scanner
-                   -> string scanner 
-    val ||       : 'a scanner * 'a scanner -> 'a scanner
-    val one      : (item -> bool) -> item scanner
-    val anyone   : item scanner
-    val succeed  : 'a -> 'a scanner
-    val any      : (item -> bool) -> item list scanner
-    val any1     : (item -> bool) -> item list scanner
-    val optional : 'a scanner -> 'a -> 'a scanner
-    val option   : 'a scanner -> 'a option scanner
-    val repeat   : 'a scanner -> 'a list scanner
-    val repeat1  : 'a scanner -> 'a list scanner
-    val repeat_fixed : int -> 'a scanner -> 'a list scanner  
-    val ahead    : 'a scanner -> 'a scanner
-    val unless   : 'a scanner -> 'b scanner -> 'b scanner
-    val !!       : (seq -> string) -> 'a scanner -> 'a scanner
-
-end
-
-signature STRING_SCANNER =
-sig
-
-    include MONO_SCANNER  where type item = string
-
-    val $$       : item -> item scanner
-    
-    val scan_id : string scanner
-    val scan_nat : int scanner
-
-    val this : item list -> item list scanner
-    val this_string : string -> string scanner                                      
-
-end    
-
-functor MonoScanner (structure Seq : MONO_SCANNER_SEQ) : MONO_SCANNER =
-struct
-
-infix 7 |-- --|
-infix 5 :-- -- ^^
-infix 3 >>
-infix 0 ||
-
-exception SyntaxError
-exception Fail of string
-
-type seq = Seq.seq
-type item = Seq.item
-type 'a scanner = seq -> 'a * seq
-
-val pull = Seq.pull
-
-fun (sc1 :-- sc2) toks =
-    let
-        val (x,toks2) = sc1 toks
-        val (y,toks3) = sc2 x toks2
-    in
-        ((x,y),toks3)
-    end
-
-fun (sc1 -- sc2) toks =
-    let
-        val (x,toks2) = sc1 toks
-        val (y,toks3) = sc2 toks2
-    in
-        ((x,y),toks3)
-    end
-
-fun (sc >> f) toks =
-    let
-        val (x,toks2) = sc toks
-    in
-        (f x,toks2)
-    end
-
-fun (sc1 --| sc2) toks =
-    let
-        val (x,toks2) = sc1 toks
-        val (_,toks3) = sc2 toks2
-    in
-        (x,toks3)
-    end
-
-fun (sc1 |-- sc2) toks =
-    let
-        val (_,toks2) = sc1 toks
-    in
-        sc2 toks2
-    end
-
-fun (sc1 ^^ sc2) toks =
-    let
-        val (x,toks2) = sc1 toks
-        val (y,toks3) = sc2 toks2
-    in
-        (x^y,toks3)
-    end
-
-fun (sc1 || sc2) toks =
-    (sc1 toks)
-    handle SyntaxError => sc2 toks
-
-fun anyone toks = case pull toks of NONE => raise SyntaxError | SOME x => x
-
-fun one p toks = case anyone toks of x as (t, toks) => if p t then x else raise SyntaxError
-
-fun succeed e toks = (e,toks)
-
-fun any p toks =
-    case pull toks of
-        NONE =>  ([],toks)
-      | SOME(x,toks2) => if p x
-                         then
-                             let
-                                 val (xs,toks3) = any p toks2
-                             in
-                                 (x::xs,toks3)
-                             end
-                         else ([],toks)
-
-fun any1 p toks =
-    let
-        val (x,toks2) = one p toks
-        val (xs,toks3) = any p toks2
-    in
-        (x::xs,toks3)
-    end
-
-fun optional sc def =  sc || succeed def
-fun option sc = (sc >> SOME) || succeed NONE
-
-(*
-fun repeat sc =
-    let
-        fun R toks =
-            let
-                val (x,toks2) = sc toks
-                val (xs,toks3) = R toks2
-            in
-                (x::xs,toks3)
-            end
-            handle SyntaxError => ([],toks)
-    in
-        R
-    end
-*)
-
-(* A tail-recursive version of repeat.  It is (ever so) slightly slower
- * than the above, non-tail-recursive version (due to the garbage generation
- * associated with the reversal of the list).  However,  this version will be
- * able to process input where the former version must give up (due to stack
- * overflow).  The slowdown seems to be around the one percent mark.
- *)
-fun repeat sc =
-    let
-        fun R xs toks =
-            case SOME (sc toks) handle SyntaxError => NONE of
-                SOME (x,toks2) => R (x::xs) toks2
-              | NONE => (List.rev xs,toks)
-    in
-        R []
-    end
-
-fun repeat1 sc toks =
-    let
-        val (x,toks2) = sc toks
-        val (xs,toks3) = repeat sc toks2
-    in
-        (x::xs,toks3)
-    end
-
-fun repeat_fixed n sc =
-    let
-        fun R n xs toks =
-            if (n <= 0) then (List.rev xs, toks)
-            else case (sc toks) of (x, toks2) => R (n-1) (x::xs) toks2
-    in
-        R n []
-    end
-
-fun ahead (sc:'a->'b*'a) toks = (#1 (sc toks),toks)
-
-fun unless test sc toks =
-    let
-        val test_failed = (test toks;false) handle SyntaxError => true
-    in
-        if test_failed
-        then sc toks
-        else raise SyntaxError
-    end
-
-fun !! f sc toks = (sc toks
-                    handle SyntaxError => raise Fail (f toks))
-
-end
-
-
-structure StringScanner : STRING_SCANNER =
-struct
-
-structure Scan = MonoScanner(structure Seq = StringScannerSeq)
-open Scan
-
-fun $$ arg = one (fn x => x = arg)
-
-val scan_id = one Symbol.is_letter ^^ (any Symbol.is_letdig >> implode);
-
-val nat_of_list = the o Int.fromString o implode 
-
-val scan_nat = repeat1 (one Symbol.is_digit) >> nat_of_list 
-
-fun this [] = (fn toks => ([], toks))
-  | this (xs' as (x::xs)) = one (fn y => x=y) -- this xs >> K xs'
-
-fun this_string s = this (raw_explode s) >> K s
-
-end
\ No newline at end of file
--- a/src/HOL/Import/mono_seq.ML	Wed Jul 20 12:23:20 2011 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,76 +0,0 @@
-(*  Title:      HOL/Import/mono_seq.ML
-    Author:     Steven Obua, TU Muenchen
-
-Monomorphic sequences.
-*)
-
-(* The trouble is that signature / structures cannot depend on type variable parameters ... *)
-
-signature MONO_SCANNER_SEQ =
-sig
-    type seq
-    type item
-    
-    val pull : seq -> (item * seq) option 
-end
-
-signature MONO_EXTENDED_SCANNER_SEQ =
-sig
-
-  include MONO_SCANNER_SEQ
-
-  val null : seq -> bool
-  val length : seq -> int
-  val take_at_most : seq -> int -> item list
-
-end
-
-functor MonoExtendScannerSeq (structure Seq : MONO_SCANNER_SEQ) : MONO_EXTENDED_SCANNER_SEQ =
-struct
-  
-type seq = Seq.seq
-type item = Seq.item
-val pull = Seq.pull
-
-fun null s = is_none (pull s)
-
-fun take_at_most s n = 
-    if n <= 0 then [] else
-    case pull s of 
-        NONE => []
-      | SOME (x,s') => x::(take_at_most s' (n-1))
-
-fun length' s n = 
-    case pull s of
-        NONE => n
-      | SOME (_, s') => length' s' (n+1)
-
-fun length s = length' s 0
-                
-end  
-
-
-structure StringScannerSeq : 
-          sig 
-              include MONO_EXTENDED_SCANNER_SEQ 
-              val fromString : string -> seq
-          end
-  =
-struct
-
-structure Incubator : MONO_SCANNER_SEQ =
-struct
-
-type seq = string * int * int
-type item = string
-
-fun pull (s, len, i) = if i >= len then NONE else SOME (String.substring (s, i, 1), (s, len, i+1))
-end
-
-structure Extended = MonoExtendScannerSeq (structure Seq = Incubator)
-open Extended
-
-fun fromString s = (s, String.size s, 0)
-
-end
-
--- a/src/HOL/Import/proof_kernel.ML	Wed Jul 20 12:23:20 2011 +0200
+++ b/src/HOL/Import/proof_kernel.ML	Wed Jul 20 13:27:01 2011 +0200
@@ -129,7 +129,7 @@
 fun to_hol_thm th = HOLThm ([], th)
 
 val replay_add_dump = add_dump
-fun add_dump s thy = (ImportRecorder.add_dump s; replay_add_dump s thy)
+fun add_dump s thy = replay_add_dump s thy
 
 datatype proof_info
   = Info of {disk_info: (string * string) option Unsynchronized.ref}
@@ -303,85 +303,14 @@
                              handle PK _ => thyname)
 val get_name : (string * string) list -> string = Lib.assoc "n"
 
-local
-    open LazyScan
-    infix 7 |-- --|
-    infix 5 :-- -- ^^
-    infix 3 >>
-    infix 0 ||
-in
 exception XML of string
 
 datatype xml = Elem of string * (string * string) list * xml list
 datatype XMLtype = XMLty of xml | FullType of hol_type
 datatype XMLterm = XMLtm of xml | FullTerm of term
 
-fun pair x y = (x,y)
-
-fun scan_id toks =
-    let
-        val (x,toks2) = one Char.isAlpha toks
-        val (xs,toks3) = any Char.isAlphaNum toks2
-    in
-        (String.implode (x::xs),toks3)
-    end
-
-fun scan_string str c =
-    let
-        fun F [] toks = (c,toks)
-          | F (c::cs) toks =
-            case LazySeq.getItem toks of
-                SOME(c',toks') =>
-                if c = c'
-                then F cs toks'
-                else raise SyntaxError
-              | NONE => raise SyntaxError
-    in
-        F (String.explode str)
-    end
-
-local
-    val scan_entity =
-        (scan_string "amp;" #"&")
-            || scan_string "quot;" #"\""
-            || scan_string "gt;" #">"
-            || scan_string "lt;" #"<"
-            || scan_string "apos;" #"'"
-in
-fun scan_nonquote toks =
-    case LazySeq.getItem toks of
-        SOME (c,toks') =>
-        (case c of
-             #"\"" => raise SyntaxError
-           | #"&" => scan_entity toks'
-           | c => (c,toks'))
-      | NONE => raise SyntaxError
-end
-
-val scan_string = $$ #"\"" |-- repeat scan_nonquote --| $$ #"\"" >>
-                     String.implode
-
-val scan_attribute = scan_id -- $$ #"=" |-- scan_string
-
-val scan_start_of_tag = $$ #"<" |-- scan_id --
-                           repeat ($$ #" " |-- scan_attribute)
-
-fun scan_end_of_tag toks = ($$ #"/" |-- $$ #">" |-- succeed []) toks
-
-val scan_end_tag = $$ #"<" |-- $$ #"/" |-- scan_id --| $$ #">"
-
-fun scan_children id = $$ #">" |-- repeat scan_tag -- scan_end_tag >>
-                       (fn (chldr,id') => if id = id'
-                                          then chldr
-                                          else raise XML "Tag mismatch")
-and scan_tag toks =
-    let
-        val ((id,atts),toks2) = scan_start_of_tag toks
-        val (chldr,toks3) = (scan_children id || scan_end_of_tag) toks2
-    in
-        (Elem (id,atts,chldr),toks3)
-    end
-end
+fun xml_to_import_xml (XML.Elem ((n, l), ts)) = Elem (n, l, map xml_to_import_xml ts)
+  | xml_to_import_xml (XML.Text _) = raise XML "Incorrect proof file: text";
 
 val type_of = Term.type_of
 
@@ -473,7 +402,6 @@
       let
           val _ = Unsynchronized.inc invented_isavar
           val t = "u_" ^ string_of_int (!invented_isavar)
-          val _ = ImportRecorder.protect_varname s t
           val _ = protected_varnames := Symtab.update (s, t) (!protected_varnames)
       in
           t
@@ -927,7 +855,7 @@
 fun import_proof_concl thyname thmname thy =
     let
         val is = TextIO.openIn(proof_file_name thyname thmname thy)
-        val (proof_xml,_) = scan_tag (LazySeq.of_instream is)
+        val proof_xml = xml_to_import_xml (XML.parse (TextIO.inputAll is))
         val _ = TextIO.closeIn is
     in
         case proof_xml of
@@ -948,7 +876,7 @@
 fun import_proof thyname thmname thy =
     let
         val is = TextIO.openIn(proof_file_name thyname thmname thy)
-        val (proof_xml,_) = scan_tag (LazySeq.of_instream is)
+        val proof_xml = xml_to_import_xml (XML.parse (TextIO.inputAll is))
         val _ = TextIO.closeIn is
     in
         case proof_xml of
@@ -1292,8 +1220,6 @@
                         val hth as HOLThm args = mk_res th
                         val thy' =  thy |> add_hol4_theorem thyname thmname args
                                         |> add_hol4_mapping thyname thmname isaname
-                        val _ = ImportRecorder.add_hol_theorem thyname thmname (snd args)
-                        val _ = ImportRecorder.add_hol_mapping thyname thmname isaname
                     in
                         (thy',SOME hth)
                     end
@@ -1364,7 +1290,6 @@
         val rew = rewrite_hol4_term (concl_of th) thy
         val th  = Thm.equal_elim rew th
         val thy' = add_hol4_pending thyname thmname args thy
-        val _ = ImportRecorder.add_hol_pending thyname thmname (hthm2thm hth')
         val th = disambiguate_frees th
         val th = Object_Logic.rulify th
         val thy2 =
@@ -1920,17 +1845,14 @@
         val csyn = mk_syn thy constname
         val thy1 = case HOL4DefThy.get thy of
                        Replaying _ => thy
-                     | _ => (ImportRecorder.add_consts [(constname, ctype, csyn)];
-                              Sign.add_consts_i [(Binding.name constname,ctype,csyn)] thy)
+                     | _ => Sign.add_consts_i [(Binding.name constname,ctype,csyn)] thy
         val eq = mk_defeq constname rhs' thy1
         val (thms, thy2) = Global_Theory.add_defs false [((Binding.name thmname,eq),[])] thy1
-        val _ = ImportRecorder.add_defs thmname eq
         val def_thm = hd thms
         val thm' = def_thm RS meta_eq_to_obj_eq_thm
         val (thy',th) = (thy2, thm')
         val fullcname = Sign.intern_const thy' constname
         val thy'' = add_hol4_const_mapping thyname constname true fullcname thy'
-        val _ = ImportRecorder.add_hol_const_mapping thyname constname fullcname
         val (linfo,tm24) = disamb_term (mk_teq constname rhs' thy'')
         val rew = rewrite_hol4_term eq thy''
         val crhs = cterm_of thy'' (#2 (Logic.dest_equals (prop_of rew)))
@@ -1958,13 +1880,10 @@
                     | NONE => raise ERR "new_definition" "Bad conclusion"
         val fullname = Sign.full_bname thy22 thmname
         val thy22' = case opt_get_output_thy thy22 of
-                         "" => (ImportRecorder.add_hol_mapping thyname thmname fullname;
-                                add_hol4_mapping thyname thmname fullname thy22)
+                         "" => add_hol4_mapping thyname thmname fullname thy22
                        | output_thy =>
                          let
                              val moved_thmname = output_thy ^ "." ^ thyname ^ "." ^ thmname
-                             val _ = ImportRecorder.add_hol_move fullname moved_thmname
-                             val _ = ImportRecorder.add_hol_mapping thyname thmname moved_thmname
                          in
                              thy22 |> add_hol4_move fullname moved_thmname
                                    |> add_hol4_mapping thyname thmname moved_thmname
@@ -2012,7 +1931,6 @@
                                    acc ^ "\n  " ^ quotename c ^ " :: \"" ^
                                    Syntax.string_of_typ_global thy T ^ "\" " ^ string_of_mixfix csyn) ("consts", consts)
                                val thy' = add_dump str thy
-                               val _ = ImportRecorder.add_consts consts
                            in
                                Sign.add_consts_i (map (fn (c, T, mx) => (Binding.name c, T, mx)) consts) thy'
                            end
@@ -2024,7 +1942,6 @@
             val (thy',res) = Choice_Specification.add_specification NONE
                                  names'
                                  (thy1,th)
-            val _ = ImportRecorder.add_specification names' th
             val res' = Thm.unvarify_global res
             val hth = HOLThm(rens,res')
             val rew = rewrite_hol4_term (concl_of res') thy'
@@ -2092,19 +2009,16 @@
             val ((_, typedef_info), thy') =
               Typedef.add_typedef_global false (SOME (Binding.name thmname))
                 (Binding.name tycname, map (rpair dummyS) tnames, tsyn) c NONE (rtac th2 1) thy
-            val _ = ImportRecorder.add_typedef (SOME thmname) typ c NONE th2
 
             val th3 = (#type_definition (#2 typedef_info)) RS typedef_hol2hol4
 
             val fulltyname = Sign.intern_type thy' tycname
             val thy'' = add_hol4_type_mapping thyname tycname true fulltyname thy'
-            val _ = ImportRecorder.add_hol_type_mapping thyname tycname fulltyname
 
             val (hth' as HOLThm args) = norm_hthm thy'' (HOLThm(rens,th3))
             val _ = if has_ren hth' then warning ("Theorem " ^ thmname ^ " needs variable-disambiguating")
                     else ()
             val thy4 = add_hol4_pending thyname thmname args thy''
-            val _ = ImportRecorder.add_hol_pending thyname thmname (hthm2thm hth')
 
             val rew = rewrite_hol4_term (concl_of td_th) thy4
             val th  = Thm.equal_elim rew (Thm.transfer thy4 td_th)
@@ -2169,7 +2083,6 @@
               Typedef.add_typedef_global false NONE
                 (Binding.name tycname, map (rpair dummyS) tnames, tsyn) c
                 (SOME(Binding.name rep_name,Binding.name abs_name)) (rtac th2 1) thy
-            val _ = ImportRecorder.add_typedef NONE typ c (SOME(rep_name,abs_name)) th2
             val fulltyname = Sign.intern_type thy' tycname
             val aty = Type (fulltyname, map mk_vartype tnames)
             val abs_ty = tT --> aty
@@ -2186,11 +2099,9 @@
               raise ERR "type_introduction" "no term variables expected any more"
             val _ = message ("step 3: thyname="^thyname^", tycname="^tycname^", fulltyname="^fulltyname)
             val thy'' = add_hol4_type_mapping thyname tycname true fulltyname thy'
-            val _ = ImportRecorder.add_hol_type_mapping thyname tycname fulltyname
             val _ = message "step 4"
             val (hth' as HOLThm args) = norm_hthm thy'' (HOLThm(rens,th4))
             val thy4 = add_hol4_pending thyname thmname args thy''
-            val _ = ImportRecorder.add_hol_pending thyname thmname (hthm2thm hth')
 
             val P' = P (* why !? #2 (Logic.dest_equals (concl_of (rewrite_hol4_term P thy4))) *)
             val c   =
--- a/src/HOL/Import/replay.ML	Wed Jul 20 12:23:20 2011 +0200
+++ b/src/HOL/Import/replay.ML	Wed Jul 20 13:27:01 2011 +0200
@@ -6,7 +6,6 @@
 struct
 
 open ProofKernel
-open ImportRecorder
 
 exception REPLAY of string * string
 fun ERR f mesg = REPLAY (f,mesg)
@@ -14,7 +13,6 @@
 
 fun replay_proof int_thms thyname thmname prf thy =
     let
-        val _ = ImportRecorder.start_replay_proof thyname thmname 
         fun rp (PRefl tm) thy = ProofKernel.REFL tm thy
           | rp (PInstT(p,lambda)) thy =
             let
@@ -269,13 +267,8 @@
                   | _ => rp pc thy
             end
     in
-        let
-            val x = rp' prf thy
-            val _ = ImportRecorder.stop_replay_proof thyname thmname
-        in
-            x
-        end
-    end handle x => (ImportRecorder.abort_replay_proof thyname thmname; reraise x)  (* FIXME avoid handle x ?? *)
+        rp' prf thy
+    end
 
 fun setup_int_thms thyname thy =
     let
@@ -316,74 +309,8 @@
         replay_fact (thmname,thy)
     end
 
-fun replay_chached_thm int_thms thyname thmname =
-    let
-        fun th_of thy = Skip_Proof.make_thm thy
-        fun err msg = raise ERR "replay_cached_thm" msg
-        val _ = writeln ("Replaying (from cache) "^thmname^" ...")
-        fun rps [] thy = thy
-          | rps (t::ts) thy = rps ts (rp t thy)
-        and rp (ThmEntry (thyname', thmname', aborted, History history)) thy = rps history thy      
-          | rp (DeltaEntry ds) thy = fold delta ds thy
-        and delta (Specification (names, th)) thy = 
-            fst (Choice_Specification.add_specification NONE names (thy,th_of thy th))
-          | delta (Hol_mapping (thyname, thmname, isaname)) thy = 
-            add_hol4_mapping thyname thmname isaname thy
-          | delta (Hol_pending (thyname, thmname, th)) thy = 
-            add_hol4_pending thyname thmname ([], th_of thy th) thy
-          | delta (Consts cs) thy = Sign.add_consts_i (map (fn (c, T, mx) => (Binding.name c, T, mx)) cs) thy
-          | delta (Hol_const_mapping (thyname, constname, fullcname)) thy = 
-            add_hol4_const_mapping thyname constname true fullcname thy
-          | delta (Hol_move (fullname, moved_thmname)) thy = 
-            add_hol4_move fullname moved_thmname thy
-          | delta (Defs (thmname, eq)) thy =
-            snd (Global_Theory.add_defs false [((Binding.name thmname, eq), [])] thy)
-          | delta (Hol_theorem (thyname, thmname, th)) thy =
-            add_hol4_theorem thyname thmname ([], th_of thy th) thy
-          | delta (Typedef (thmname, (t, vs, mx), c, repabs, th)) thy = 
-            snd (Typedef.add_typedef_global false (Option.map Binding.name thmname)
-              (Binding.name t, map (rpair dummyS) vs, mx) c
-        (Option.map (pairself Binding.name) repabs) (rtac (th_of thy th) 1) thy)
-          | delta (Hol_type_mapping (thyname, tycname, fulltyname)) thy =  
-            add_hol4_type_mapping thyname tycname true fulltyname thy
-          | delta (Indexed_theorem (i, th)) thy = 
-            (Array.update (int_thms,i-1,SOME (ProofKernel.to_hol_thm (th_of thy th))); thy)                   
-          | delta (Protect_varname (s,t)) thy = (ProofKernel.replay_protect_varname s t; thy)
-          | delta (Dump s) thy = ProofKernel.replay_add_dump s thy
-    in
-        rps
-    end
-
 fun import_thms thyname int_thms thmnames thy =
     let
-        fun zip names [] = ([], names)
-          | zip [] _ = ([], [])
-          | zip (thmname::names) ((ThmEntry (entry as (thyname',thmname',aborted,History history)))::ys) = 
-            if thyname = thyname' andalso thmname = thmname' then
-                (if aborted then ([], thmname::names) else 
-                 let
-                     val _ = writeln ("theorem is in-sync: "^thmname)
-                     val (cached,normal) = zip names ys
-                 in
-                     (entry::cached, normal)
-                 end)
-            else
-                let
-                    val _ = writeln ("cached theorems are not in-sync,  expected: "^thmname^", found: "^thmname')
-                    val _ = writeln ("proceeding with next uncached theorem...")
-                in
-                    ([], thmname::names)
-                end
-        (*      raise ERR "import_thms" ("cached theorems are not in-sync, expected: "^thmname^", found: "^thmname')*)
-          | zip (thmname::_) (DeltaEntry _ :: _) = 
-            raise ERR "import_thms" ("expected theorem '"^thmname^"', but found a delta")
-        fun zip' xs (History ys) = 
-            let
-                val _ = writeln ("length of xs: "^(string_of_int (length xs)))
-                val _ = writeln ("length of history: "^(string_of_int (length ys)))
-            in
-                zip xs ys
-            end
         fun replay_fact thmname thy = 
             let
                 val prf = mk_proof PDisk        
@@ -393,10 +320,7 @@
             in
                 p
             end
-        fun replay_cache (_,thmname, _, History history) thy = replay_chached_thm int_thms thyname thmname history thy
-        val (cached, normal) = zip' thmnames (ImportRecorder.get_history ())
-        val _ = ImportRecorder.set_history (History (map ThmEntry cached))
-        val res_thy = fold replay_fact normal (fold replay_cache cached thy)
+        val res_thy = fold replay_fact thmnames thy
     in
         res_thy
     end
--- a/src/HOL/Import/scan.ML	Wed Jul 20 12:23:20 2011 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,219 +0,0 @@
-(*  Title:      HOL/Import/scan.ML
-    Author:     Sebastian Skalberg, TU Muenchen / Steven Obua, TU Muenchen
-
-Scanner combinators for sequences.
-*)
-
-signature SCANNER =
-sig
-
-    include SCANNER_SEQ
-
-    exception SyntaxError
-
-    type ('a,'b) scanner = 'a seq -> 'b * 'a seq
-
-    val :--      : ('a,'b) scanner * ('b -> ('a,'c) scanner)
-                   -> ('a,'b*'c) scanner
-    val --       : ('a,'b) scanner * ('a,'c) scanner -> ('a,'b*'c) scanner
-    val >>       : ('a,'b) scanner * ('b -> 'c) -> ('a,'c) scanner
-    val --|      : ('a,'b) scanner * ('a,'c) scanner -> ('a,'b) scanner
-    val |--      : ('a,'b) scanner * ('a,'c) scanner -> ('a,'c) scanner
-    val ^^       : ('a,string) scanner * ('a,string) scanner
-                   -> ('a,string) scanner 
-    val ||       : ('a,'b) scanner * ('a,'b) scanner -> ('a,'b) scanner
-    val one      : ('a -> bool) -> ('a,'a) scanner
-    val anyone   : ('a,'a) scanner
-    val succeed  : 'b -> ('a,'b) scanner
-    val any      : ('a -> bool) -> ('a,'a list) scanner
-    val any1     : ('a -> bool) -> ('a,'a list) scanner
-    val optional : ('a,'b) scanner -> 'b -> ('a,'b) scanner
-    val option   : ('a,'b) scanner -> ('a,'b option) scanner
-    val repeat   : ('a,'b) scanner -> ('a,'b list) scanner
-    val repeat1  : ('a,'b) scanner -> ('a,'b list) scanner
-    val repeat_fixed : int -> ('a, 'b) scanner -> ('a, 'b list) scanner  
-    val ahead    : ('a,'b) scanner -> ('a,'b) scanner
-    val unless   : ('a, 'b) scanner -> ('a,'c) scanner -> ('a,'c) scanner
-    val $$       : ''a -> (''a,''a) scanner
-    val !!       : ('a seq -> string) -> ('a,'b) scanner -> ('a,'b) scanner
-    
-    val scan_id : (string, string) scanner
-    val scan_nat : (string, int) scanner
-
-    val this : ''a list -> (''a, ''a list) scanner
-    val this_string : string -> (string, string) scanner
-end
-
-functor Scanner (structure Seq : SCANNER_SEQ) : SCANNER =
-struct
-
-infix 7 |-- --|
-infix 5 :-- -- ^^
-infix 3 >>
-infix 0 ||
-
-exception SyntaxError
-exception Fail of string
-
-type 'a seq = 'a Seq.seq
-type ('a,'b) scanner = 'a seq -> 'b * 'a seq
-
-val pull = Seq.pull
-
-fun (sc1 :-- sc2) toks =
-    let
-        val (x,toks2) = sc1 toks
-        val (y,toks3) = sc2 x toks2
-    in
-        ((x,y),toks3)
-    end
-
-fun (sc1 -- sc2) toks =
-    let
-        val (x,toks2) = sc1 toks
-        val (y,toks3) = sc2 toks2
-    in
-        ((x,y),toks3)
-    end
-
-fun (sc >> f) toks =
-    let
-        val (x,toks2) = sc toks
-    in
-        (f x,toks2)
-    end
-
-fun (sc1 --| sc2) toks =
-    let
-        val (x,toks2) = sc1 toks
-        val (_,toks3) = sc2 toks2
-    in
-        (x,toks3)
-    end
-
-fun (sc1 |-- sc2) toks =
-    let
-        val (_,toks2) = sc1 toks
-    in
-        sc2 toks2
-    end
-
-fun (sc1 ^^ sc2) toks =
-    let
-        val (x,toks2) = sc1 toks
-        val (y,toks3) = sc2 toks2
-    in
-        (x^y,toks3)
-    end
-
-fun (sc1 || sc2) toks =
-    (sc1 toks)
-    handle SyntaxError => sc2 toks
-
-fun anyone toks = case pull toks of NONE => raise SyntaxError | SOME x => x
-
-fun one p toks = case anyone toks of x as (t, toks) => if p t then x else raise SyntaxError
-
-fun succeed e toks = (e,toks)
-
-fun any p toks =
-    case pull toks of
-        NONE =>  ([],toks)
-      | SOME(x,toks2) => if p x
-                         then
-                             let
-                                 val (xs,toks3) = any p toks2
-                             in
-                                 (x::xs,toks3)
-                             end
-                         else ([],toks)
-
-fun any1 p toks =
-    let
-        val (x,toks2) = one p toks
-        val (xs,toks3) = any p toks2
-    in
-        (x::xs,toks3)
-    end
-
-fun optional sc def =  sc || succeed def
-fun option sc = (sc >> SOME) || succeed NONE
-
-(*
-fun repeat sc =
-    let
-        fun R toks =
-            let
-                val (x,toks2) = sc toks
-                val (xs,toks3) = R toks2
-            in
-                (x::xs,toks3)
-            end
-            handle SyntaxError => ([],toks)
-    in
-        R
-    end
-*)
-
-(* A tail-recursive version of repeat.  It is (ever so) slightly slower
- * than the above, non-tail-recursive version (due to the garbage generation
- * associated with the reversal of the list).  However,  this version will be
- * able to process input where the former version must give up (due to stack
- * overflow).  The slowdown seems to be around the one percent mark.
- *)
-fun repeat sc =
-    let
-        fun R xs toks =
-            case SOME (sc toks) handle SyntaxError => NONE of
-                SOME (x,toks2) => R (x::xs) toks2
-              | NONE => (List.rev xs,toks)
-    in
-        R []
-    end
-
-fun repeat1 sc toks =
-    let
-        val (x,toks2) = sc toks
-        val (xs,toks3) = repeat sc toks2
-    in
-        (x::xs,toks3)
-    end
-
-fun repeat_fixed n sc =
-    let
-        fun R n xs toks =
-            if (n <= 0) then (List.rev xs, toks)
-            else case (sc toks) of (x, toks2) => R (n-1) (x::xs) toks2
-    in
-        R n []
-    end
-
-fun ahead (sc:'a->'b*'a) toks = (#1 (sc toks),toks)
-
-fun unless test sc toks =
-    let
-        val test_failed = (test toks;false) handle SyntaxError => true
-    in
-        if test_failed
-        then sc toks
-        else raise SyntaxError
-    end
-
-fun $$ arg = one (fn x => x = arg)
-
-fun !! f sc toks = (sc toks
-                    handle SyntaxError => raise Fail (f toks))
-
-val scan_id = one Symbol.is_letter ^^ (any Symbol.is_letdig >> implode);
-
-val nat_of_list = the o Int.fromString o implode 
-
-val scan_nat = repeat1 (one Symbol.is_digit) >> nat_of_list 
-
-fun this [] = (fn toks => ([], toks))
-  | this (xs' as (x::xs)) = one (fn y => x=y) -- this xs >> K xs'
-
-fun this_string s = this (raw_explode s) >> K s
-
-end
-
--- a/src/HOL/Import/seq.ML	Wed Jul 20 12:23:20 2011 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,99 +0,0 @@
-signature SCANNER_SEQ =
-sig
-    type 'a seq
-    
-    val pull : 'a seq -> ('a * 'a seq) option 
-end
-
-signature EXTENDED_SCANNER_SEQ =
-sig
-
-  include SCANNER_SEQ
-
-  val null : 'a seq -> bool
-  val length : 'a seq -> int
-  val take_at_most : 'a seq -> int -> 'a list
-
-end
-
-functor ExtendScannerSeq (structure Seq : SCANNER_SEQ) : EXTENDED_SCANNER_SEQ =
-struct
-  
-type 'a seq = 'a Seq.seq
-
-val pull = Seq.pull
-
-fun null s = is_none (pull s)
-
-fun take_at_most s n = 
-    if n <= 0 then [] else
-    case pull s of 
-        NONE => []
-      | SOME (x,s') => x::(take_at_most s' (n-1))
-
-fun length' s n = 
-    case pull s of
-        NONE => n
-      | SOME (_, s') => length' s' (n+1)
-
-fun length s = length' s 0
-                
-end  
-
-signature VECTOR_SCANNER_SEQ = 
-sig
-    include EXTENDED_SCANNER_SEQ
-
-    val fromString : string -> string seq
-    val fromVector : 'a Vector.vector -> 'a seq
-end
-
-structure VectorScannerSeq :> VECTOR_SCANNER_SEQ =
-struct
-  
-structure Incubator : SCANNER_SEQ =
-struct
-
-type 'a seq = 'a Vector.vector * int * int
-fun pull (v, len, i) = if i >= len then NONE else SOME (Vector.sub (v, i), (v, len, i+1))
-
-end
-
-structure Extended = ExtendScannerSeq (structure Seq = Incubator)
-
-open Extended
-
-fun fromVector v = (v, Vector.length v, 0)
-fun vector_from_string s = Vector.tabulate (String.size s, fn i => String.str (String.sub (s, i)))
-fun fromString s = fromVector (vector_from_string s)
-
-end
-
-signature LIST_SCANNER_SEQ =
-sig
-    include EXTENDED_SCANNER_SEQ
-    
-    val fromString : string -> string seq
-    val fromList : 'a list -> 'a seq
-end
-
-structure ListScannerSeq :> LIST_SCANNER_SEQ =
-struct
-     
-structure Incubator : SCANNER_SEQ =
-struct
-
-type 'a seq = 'a list
-fun pull [] = NONE
-  | pull (x::xs) = SOME (x, xs)
-
-end
-
-structure Extended = ExtendScannerSeq (structure Seq = Incubator)
-
-open Extended
-
-val fromList = I
-val fromString = raw_explode
-
-end
--- a/src/HOL/Import/xml.ML	Wed Jul 20 12:23:20 2011 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,199 +0,0 @@
-(*  Title:      HOL/Import/xml.ML
-
-Yet another version of XML support.
-*)
-
-signature XML =
-sig
-  val header: string
-  val text: string -> string
-  val text_charref: string -> string
-  val cdata: string -> string
-  val element: string -> (string * string) list -> string list -> string
-  
-  datatype tree =
-      Elem of string * (string * string) list * tree list
-    | Text of string
-  
-  val string_of_tree: tree -> string
-  val tree_of_string: string -> tree
-
-  val encoded_string_of_tree : tree -> string
-  val tree_of_encoded_string : string -> tree
-end;
-
-structure XML :> XML =
-struct
-
-(*structure Seq = VectorScannerSeq
-structure Scan = Scanner (structure Seq = Seq)*)
-structure Seq = StringScannerSeq
-structure Scan = StringScanner
-
-
-open Scan
-
-(** string based representation (small scale) **)
-
-val header = "<?xml version=\"1.0\"?>\n";
-
-(* text and character data *)
-
-fun decode "&lt;" = "<"
-  | decode "&gt;" = ">"
-  | decode "&amp;" = "&"
-  | decode "&apos;" = "'"
-  | decode "&quot;" = "\""
-  | decode c = c;
-
-fun encode "<" = "&lt;"
-  | encode ">" = "&gt;"
-  | encode "&" = "&amp;"
-  | encode "'" = "&apos;"
-  | encode "\"" = "&quot;"
-  | encode c = c;
-
-fun encode_charref c = "&#" ^ string_of_int (ord c) ^ ";"
-
-val text = Library.translate_string encode
-
-val text_charref = translate_string encode_charref;
-
-val cdata = enclose "<![CDATA[" "]]>\n"
-
-(* elements *)
-
-fun attribute (a, x) = a ^ " = \"" ^ text x ^ "\"";
-
-fun element name atts cs =
-  let val elem = space_implode " " (name :: map attribute atts) in
-    if null cs then enclose "<" "/>" elem
-    else enclose "<" ">" elem ^ implode cs ^ enclose "</" ">" name
-  end;
-
-(** explicit XML trees **)
-
-datatype tree =
-    Elem of string * (string * string) list * tree list
-  | Text of string;
-
-fun string_of_tree tree =
-  let
-    fun string_of (Elem (name, atts, ts)) buf =
-        let val buf' =
-          buf |> Buffer.add "<"
-          |> fold Buffer.add (separate " " (name :: map attribute atts))
-        in
-          if null ts then
-            buf' |> Buffer.add "/>"
-          else
-            buf' |> Buffer.add ">"
-            |> fold string_of ts
-            |> Buffer.add "</" |> Buffer.add name |> Buffer.add ">"
-        end
-      | string_of (Text s) buf = Buffer.add (text s) buf;
-  in Buffer.content (string_of tree Buffer.empty) end;
-
-(** XML parsing **)
-
-fun beginning n xs = Symbol.beginning n (Seq.take_at_most xs n)
-
-fun err s xs =
-  "XML parsing error: " ^ s ^ "\nfound: " ^ quote (beginning 100 xs) ;
-
-val scan_whspc = Scan.any Symbol.is_blank;
-
-val scan_special = $$ "&" ^^ scan_id ^^ $$ ";" >> decode;
-
-val parse_chars = Scan.repeat1 (Scan.unless ((* scan_whspc -- *)$$ "<")
-  (scan_special || Scan.one Symbol.is_regular)) >> implode;
-
-val parse_cdata = Scan.this_string "<![CDATA[" |--
-  (Scan.repeat (Scan.unless (Scan.this_string "]]>") (Scan.one Symbol.is_regular)) >>
-    implode) --| Scan.this_string "]]>";
-
-val parse_att =
-    scan_id --| scan_whspc --| $$ "=" --| scan_whspc --
-    (($$ "\"" || $$ "'") :-- (fn s => (Scan.repeat (Scan.unless ($$ s)
-    (scan_special || Scan.one Symbol.is_regular)) >> implode) --| $$ s) >> snd);
-
-val parse_comment = Scan.this_string "<!--" --
-  Scan.repeat (Scan.unless (Scan.this_string "-->") (Scan.one Symbol.is_regular)) --
-  Scan.this_string "-->";
-
-val scan_comment_whspc = 
-    (scan_whspc >> K()) --| (Scan.repeat (parse_comment |-- (scan_whspc >> K())));
-
-val parse_pi = Scan.this_string "<?" |--
-  Scan.repeat (Scan.unless (Scan.this_string "?>") (Scan.one Symbol.is_regular)) --|
-  Scan.this_string "?>";
-
-fun parse_content xs =
-  ((Scan.optional ((* scan_whspc |-- *) parse_chars >> (single o Text)) [] --
-    (Scan.repeat ((* scan_whspc |-- *)
-       (   parse_elem >> single
-        || parse_cdata >> (single o Text)
-        || parse_pi >> K []
-        || parse_comment >> K []) --
-       Scan.optional ((* scan_whspc |-- *) parse_chars >> (single o Text)) []
-         >> op @) >> flat) >> op @)(* --| scan_whspc*)) xs
-
-and parse_elem xs =
-  ($$ "<" |-- scan_id --
-    Scan.repeat (scan_whspc |-- parse_att) --| scan_whspc :-- (fn (s, _) =>
-      !! (err "Expected > or />")
-        (Scan.this_string "/>" >> K []
-         || $$ ">" |-- parse_content --|
-            !! (err ("Expected </" ^ s ^ ">"))
-              (Scan.this_string ("</" ^ s) --| scan_whspc --| $$ ">"))) >>
-    (fn ((s, atts), ts) => Elem (s, atts, ts))) xs;
-
-val parse_document =
-  Scan.option (Scan.this_string "<!DOCTYPE" -- scan_whspc |--
-    (Scan.repeat (Scan.unless ($$ ">")
-      (Scan.one Symbol.is_regular)) >> implode) --| $$ ">" --| scan_whspc) --
-  parse_elem;
-
-fun tree_of_string s =
-    let
-        val seq = Seq.fromString s
-        val scanner = !! (err "Malformed element") (scan_whspc |-- parse_elem --| scan_whspc)
-        val (x, toks) = scanner seq
-    in
-        if Seq.null toks then x else error ("Unprocessed input: '"^(beginning 100 toks)^"'")
-    end
-
-(* More efficient saving and loading of xml trees employing a proprietary external format *)
-
-fun write_lstring s buf = buf |> Buffer.add (string_of_int (size s)) |> Buffer.add " " |> Buffer.add s
-fun parse_lstring toks = (scan_nat --| one Symbol.is_blank :-- (fn i => repeat_fixed i (one (K true))) >> snd >> implode) toks
-
-fun write_list w l buf = buf |> Buffer.add (string_of_int (length l)) |> Buffer.add " " |> fold w l     
-fun parse_list sc = scan_nat --| one Symbol.is_blank :-- (fn i => repeat_fixed i sc) >> snd
-
-fun write_tree (Text s) buf = buf |> Buffer.add "T" |> write_lstring s
-  | write_tree (Elem (name, attrs, children)) buf = 
-    buf |> Buffer.add "E" 
-        |> write_lstring name 
-        |> write_list (fn (a,b) => fn buf => buf |> write_lstring a |> write_lstring b) attrs 
-        |> write_list write_tree children
-
-fun parse_tree toks = (one (K true) :-- (fn "T" => parse_lstring >> Text | "E" => parse_elem | _ => raise SyntaxError) >> snd) toks
-and parse_elem toks = (parse_lstring -- parse_list (parse_lstring -- parse_lstring) -- parse_list parse_tree >> (fn ((n, a), c) => Elem (n,a,c))) toks
-
-fun encoded_string_of_tree tree = Buffer.content (write_tree tree Buffer.empty)
-
-fun tree_of_encoded_string s = 
-    let
-        fun print (a,b) = writeln (a^" "^(string_of_int b))
-        val _ = print ("length of encoded string: ", size s)
-        val _ = writeln "Seq.fromString..."
-        val seq = timeit (fn () => Seq.fromString s)
-        val  _ = print ("length of sequence", timeit (fn () => Seq.length seq))
-        val scanner = !! (err "Malformed encoded xml") parse_tree
-        val (x, toks) = scanner seq
-    in
-        if Seq.null toks then x else error ("Unprocessed input: '"^(beginning 100 toks)^"'")
-    end        
-
-end;
--- a/src/HOL/Import/xmlconv.ML	Wed Jul 20 12:23:20 2011 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,303 +0,0 @@
-signature XML_CONV =
-sig
-  type 'a input = XML.tree -> 'a
-  type 'a output = 'a -> XML.tree
-
-  exception ParseException of string
-
-  val xml_of_typ: typ output
-  val typ_of_xml: typ input
-
-  val xml_of_term: term output
-  val term_of_xml : term input
-
-  val xml_of_mixfix: mixfix output
-  val mixfix_of_xml: mixfix input
-  
-  val xml_of_proof: Proofterm.proof output
-
-  val xml_of_bool: bool output
-  val bool_of_xml: bool input
-                  
-  val xml_of_int: int output
-  val int_of_xml: int input
-
-  val xml_of_string: string output
-  val string_of_xml: string input
-
-  val xml_of_list: 'a output -> 'a list output
-  val list_of_xml: 'a input -> 'a list input
-  
-  val xml_of_tuple : 'a output -> 'a list output
-  val tuple_of_xml: 'a input -> int -> 'a list input
-
-  val xml_of_option: 'a output -> 'a option output
-  val option_of_xml: 'a input -> 'a option input
-
-  val xml_of_pair: 'a output -> 'b output -> ('a * 'b) output
-  val pair_of_xml: 'a input -> 'b input -> ('a * 'b) input
-
-  val xml_of_triple: 'a output -> 'b output -> 'c output -> ('a * 'b * 'c) output
-  val triple_of_xml: 'a input -> 'b input -> 'c input -> ('a * 'b * 'c) input
-  
-  val xml_of_unit: unit output
-  val unit_of_xml: unit input
-
-  val xml_of_quadruple: 'a output -> 'b output -> 'c output -> 'd output -> ('a * 'b * 'c * 'd) output
-  val quadruple_of_xml: 'a input -> 'b input -> 'c input -> 'd input -> ('a * 'b * 'c * 'd) input
-
-  val xml_of_quintuple: 'a output -> 'b output -> 'c output -> 'd output -> 'e output -> ('a * 'b * 'c * 'd * 'e) output
-  val quintuple_of_xml: 'a input -> 'b input -> 'c input -> 'd input -> 'e input -> ('a * 'b * 'c * 'd * 'e) input
-
-  val wrap : string -> 'a output -> 'a output
-  val unwrap : ('a -> 'b) -> 'a input -> 'b input
-  val wrap_empty : string output
-  val unwrap_empty : 'a -> 'a input
-  val name_of_wrap : XML.tree -> string
-
-  val write_to_file: 'a output -> string -> 'a -> unit
-  val read_from_file: 'a input -> string -> 'a
-
-  val serialize_to_file : 'a output -> string -> 'a -> unit
-  val deserialize_from_file : 'a input -> string -> 'a
-end;
-
-structure XMLConv : XML_CONV =
-struct
-  
-type 'a input = XML.tree -> 'a
-type 'a output = 'a -> XML.tree
-exception ParseException of string
-
-fun parse_err s = raise ParseException s
-
-fun xml_of_class name = XML.Elem ("class", [("name", name)], []);
-
-fun class_of_xml (XML.Elem ("class", [("name", name)], [])) = name
-  | class_of_xml _ = parse_err "class"
- 
-fun xml_of_typ (TVar ((s, i), S)) = XML.Elem ("TVar",
-      ("name", s) :: (if i=0 then [] else [("index", string_of_int i)]),
-      map xml_of_class S)
-  | xml_of_typ (TFree (s, S)) =
-      XML.Elem ("TFree", [("name", s)], map xml_of_class S)
-  | xml_of_typ (Type (s, Ts)) =
-      XML.Elem ("Type", [("name", s)], map xml_of_typ Ts);
-
-fun intofstr s i = 
-    case Int.fromString i of 
-        NONE => parse_err (s^", invalid index: "^i)
-      | SOME i => i
-
-fun typ_of_xml (XML.Elem ("TVar", [("name", s)], cs)) = TVar ((s,0), map class_of_xml cs)
-  | typ_of_xml (XML.Elem ("TVar", [("name", s), ("index", i)], cs)) = 
-    TVar ((s, intofstr "TVar" i), map class_of_xml cs)
-  | typ_of_xml (XML.Elem ("TFree", [("name", s)], cs)) = TFree (s, map class_of_xml cs)
-  | typ_of_xml (XML.Elem ("Type", [("name", s)], ts)) = Type (s, map typ_of_xml ts)
-  | typ_of_xml _ = parse_err "type"
-
-fun xml_of_term (Bound i) =
-      XML.Elem ("Bound", [("index", string_of_int i)], [])
-  | xml_of_term (Free (s, T)) =
-      XML.Elem ("Free", [("name", s)], [xml_of_typ T])
-  | xml_of_term (Var ((s, i), T)) = XML.Elem ("Var",
-      ("name", s) :: (if i=0 then [] else [("index", string_of_int i)]),
-      [xml_of_typ T])
-  | xml_of_term (Const (s, T)) =
-      XML.Elem ("Const", [("name", s)], [xml_of_typ T])
-  | xml_of_term (t $ u) =
-      XML.Elem ("App", [], [xml_of_term t, xml_of_term u])
-  | xml_of_term (Abs (s, T, t)) =
-      XML.Elem ("Abs", [("vname", s)], [xml_of_typ T, xml_of_term t]);
-
-fun term_of_xml (XML.Elem ("Bound", [("index", i)], [])) = Bound (intofstr "Bound" i)
-  | term_of_xml (XML.Elem ("Free", [("name", s)], [T])) = Free (s, typ_of_xml T)
-  | term_of_xml (XML.Elem ("Var",[("name", s)], [T])) = Var ((s,0), typ_of_xml T)
-  | term_of_xml (XML.Elem ("Var",[("name", s), ("index", i)], [T])) = Var ((s,intofstr "Var" i), typ_of_xml T)
-  | term_of_xml (XML.Elem ("Const", [("name", s)], [T])) = Const (s, typ_of_xml T)
-  | term_of_xml (XML.Elem ("App", [], [t, u])) = (term_of_xml t) $ (term_of_xml u)
-  | term_of_xml (XML.Elem ("Abs", [("vname", s)], [T, t])) = Abs (s, typ_of_xml T, term_of_xml t)
-  | term_of_xml _ = parse_err "term"
-
-fun xml_of_opttypes NONE = []
-  | xml_of_opttypes (SOME Ts) = [XML.Elem ("types", [], map xml_of_typ Ts)];
-
-(* FIXME: the t argument of PThm and PAxm is actually redundant, since *)
-(* it can be looked up in the theorem database. Thus, it could be      *)
-(* omitted from the xml representation.                                *)
-
-fun xml_of_proof (PBound i) =
-      XML.Elem ("PBound", [("index", string_of_int i)], [])
-  | xml_of_proof (Abst (s, optT, prf)) =
-      XML.Elem ("Abst", [("vname", s)],
-        (case optT of NONE => [] | SOME T => [xml_of_typ T]) @
-        [xml_of_proof prf])
-  | xml_of_proof (AbsP (s, optt, prf)) =
-      XML.Elem ("AbsP", [("vname", s)],
-        (case optt of NONE => [] | SOME t => [xml_of_term t]) @
-        [xml_of_proof prf])
-  | xml_of_proof (prf % optt) =
-      XML.Elem ("Appt", [],
-        xml_of_proof prf ::
-        (case optt of NONE => [] | SOME t => [xml_of_term t]))
-  | xml_of_proof (prf %% prf') =
-      XML.Elem ("AppP", [], [xml_of_proof prf, xml_of_proof prf'])
-  | xml_of_proof (Hyp t) = XML.Elem ("Hyp", [], [xml_of_term t])
-  | xml_of_proof (PThm (_, ((s, t, optTs), _))) =
-      XML.Elem ("PThm", [("name", s)], xml_of_term t :: xml_of_opttypes optTs)
-  | xml_of_proof (PAxm (s, t, optTs)) =
-      XML.Elem ("PAxm", [("name", s)], xml_of_term t :: xml_of_opttypes optTs)
-  | xml_of_proof (Oracle (s, t, optTs)) =
-      XML.Elem ("Oracle", [("name", s)], xml_of_term t :: xml_of_opttypes optTs)
-  | xml_of_proof MinProof = XML.Elem ("MinProof", [], []);
-
-fun xml_of_bool b = XML.Elem ("bool", [("value", if b then "true" else "false")], [])
-fun xml_of_int i = XML.Elem ("int", [("value", string_of_int i)], [])
-fun xml_of_string s = XML.Elem ("string", [("value", s)], [])
-fun xml_of_unit () = XML.Elem ("unit", [], [])
-fun xml_of_list output ls = XML.Elem ("list", [], map output ls)
-fun xml_of_tuple output ls = XML.Elem ("tuple", [], map output ls)
-fun xml_of_option output opt = XML.Elem ("option", [], case opt of NONE => [] | SOME x => [output x])
-fun wrap s output x = XML.Elem (s, [], [output x])
-fun wrap_empty s = XML.Elem (s, [], [])
-fun xml_of_pair output1 output2 (x1, x2) = XML.Elem ("pair", [], [output1 x1, output2 x2])
-fun xml_of_triple output1 output2 output3 (x1, x2, x3) = XML.Elem ("triple", [], [output1 x1, output2 x2, output3 x3])
-fun xml_of_quadruple output1 output2 output3 output4 (x1, x2, x3, x4) = 
-    XML.Elem ("quadruple", [], [output1 x1, output2 x2, output3 x3, output4 x4])
-fun xml_of_quintuple output1 output2 output3 output4 output5 (x1, x2, x3, x4, x5) = 
-    XML.Elem ("quintuple", [], [output1 x1, output2 x2, output3 x3, output4 x4, output5 x5])
-                                                                                  
-fun bool_of_xml (XML.Elem ("bool", [("value", "true")], [])) = true
-  | bool_of_xml (XML.Elem ("bool", [("value", "false")], [])) = false
-  | bool_of_xml _ = parse_err "bool"
-
-fun int_of_xml (XML.Elem ("int", [("value", i)], [])) = intofstr "int" i
-  | int_of_xml _ = parse_err "int"
-
-fun unit_of_xml (XML.Elem ("unit", [], [])) = ()
-  | unit_of_xml _ = parse_err "unit"
-
-fun list_of_xml input (XML.Elem ("list", [], ls)) = map input ls
-  | list_of_xml _ _ = parse_err "list"
-
-fun tuple_of_xml input i (XML.Elem ("tuple", [], ls)) = 
-    if i = length ls then map input ls else parse_err "tuple"
-  | tuple_of_xml _ _ _ = parse_err "tuple"
-
-fun option_of_xml input (XML.Elem ("option", [], [])) = NONE
-  | option_of_xml input (XML.Elem ("option", [], [opt])) = SOME (input opt)
-  | option_of_xml _ _ = parse_err "option"
-
-fun pair_of_xml input1 input2 (XML.Elem ("pair", [], [x1, x2])) = (input1 x1, input2 x2)
-  | pair_of_xml _ _ _ = parse_err "pair"
-
-fun triple_of_xml input1 input2 input3 (XML.Elem ("triple", [], [x1, x2, x3])) = (input1 x1, input2 x2, input3 x3)
-  | triple_of_xml _ _ _ _ = parse_err "triple"
-
-fun quadruple_of_xml input1 input2 input3 input4 (XML.Elem ("quadruple", [], [x1, x2, x3, x4])) = 
-    (input1 x1, input2 x2, input3 x3, input4 x4)
-  | quadruple_of_xml _ _ _ _ _ = parse_err "quadruple"
-
-fun quintuple_of_xml input1 input2 input3 input4 input5 (XML.Elem ("quintuple", [], [x1, x2, x3, x4, x5])) = 
-    (input1 x1, input2 x2, input3 x3, input4 x4, input5 x5)
-  | quintuple_of_xml _ _ _ _ _ _ = parse_err "quintuple"
-
-fun unwrap f input (XML.Elem (_, [], [x])) = f (input x)
-  | unwrap _ _ _  = parse_err "unwrap"
-
-fun unwrap_empty x (XML.Elem (_, [], [])) = x 
-  | unwrap_empty _ _ = parse_err "unwrap_unit"
-
-fun name_of_wrap (XML.Elem (name, _, _)) = name
-  | name_of_wrap _ = parse_err "name_of_wrap"
-
-fun string_of_xml (XML.Elem ("string", [("value", s)], [])) = s
-  | string_of_xml _ = parse_err "string"
-
-fun xml_of_mixfix NoSyn = wrap_empty "nosyn"
-  | xml_of_mixfix Structure = wrap_empty "structure"
-  | xml_of_mixfix (Mixfix args) = wrap "mixfix" (xml_of_triple xml_of_string (xml_of_list xml_of_int) xml_of_int) args
-  | xml_of_mixfix (Delimfix s) = wrap "delimfix" xml_of_string s
-  | xml_of_mixfix (Infix args) = wrap "infix" (xml_of_pair xml_of_string xml_of_int) args
-  | xml_of_mixfix (Infixl args) = wrap "infixl" (xml_of_pair xml_of_string xml_of_int) args
-  | xml_of_mixfix (Infixr args) = wrap "infixr" (xml_of_pair xml_of_string xml_of_int) args
-  | xml_of_mixfix (Binder args) = wrap "binder" (xml_of_triple xml_of_string xml_of_int xml_of_int) args
-                                  
-fun mixfix_of_xml e = 
-    (case name_of_wrap e of 
-         "nosyn" => unwrap_empty NoSyn 
-       | "structure" => unwrap_empty Structure 
-       | "mixfix" => unwrap Mixfix (triple_of_xml string_of_xml (list_of_xml int_of_xml) int_of_xml)
-       | "delimfix" => unwrap Delimfix string_of_xml
-       | "infix" => unwrap Infix (pair_of_xml string_of_xml int_of_xml) 
-       | "infixl" => unwrap Infixl (pair_of_xml string_of_xml int_of_xml)  
-       | "infixr" => unwrap Infixr (pair_of_xml string_of_xml int_of_xml)
-       | "binder" => unwrap Binder (triple_of_xml string_of_xml int_of_xml int_of_xml)
-       | _ => parse_err "mixfix"
-    ) e
-
-
-fun to_file f output fname x = File.write (Path.explode fname) (f (output x))
- 
-fun from_file f input fname = 
-    let
-        val _ = writeln "read_from_file enter"
-        val s = File.read (Path.explode fname)
-        val _ = writeln "done: read file"
-        val tree = timeit (fn () => f s)
-        val _ = writeln "done: tree"
-        val x = timeit (fn () => input tree)
-        val _ = writeln "done: input"
-    in
-        x
-    end
-
-fun write_to_file x = to_file XML.string_of_tree x
-fun read_from_file x = timeit (fn () => (writeln "read_from_file"; from_file XML.tree_of_string x))
-
-fun serialize_to_file x = to_file XML.encoded_string_of_tree x
-fun deserialize_from_file x = timeit (fn () => (writeln "deserialize_from_file"; from_file XML.tree_of_encoded_string x))
-
-end;
-
-structure XMLConvOutput =
-struct
-open XMLConv
- 
-val typ = xml_of_typ
-val term = xml_of_term
-val mixfix = xml_of_mixfix
-val proof = xml_of_proof
-val bool = xml_of_bool
-val int = xml_of_int
-val string = xml_of_string
-val unit = xml_of_unit
-val list = xml_of_list
-val pair = xml_of_pair
-val option = xml_of_option
-val triple = xml_of_triple
-val quadruple = xml_of_quadruple
-val quintuple = xml_of_quintuple
-
-end
-
-structure XMLConvInput = 
-struct
-open XMLConv
-
-val typ = typ_of_xml
-val term = term_of_xml
-val mixfix = mixfix_of_xml
-val bool = bool_of_xml
-val int = int_of_xml
-val string = string_of_xml
-val unit = unit_of_xml
-val list = list_of_xml
-val pair = pair_of_xml
-val option = option_of_xml
-val triple = triple_of_xml
-val quadruple = quadruple_of_xml
-val quintuple = quintuple_of_xml
-
-end
-
--- a/src/HOL/IsaMakefile	Wed Jul 20 12:23:20 2011 +0200
+++ b/src/HOL/IsaMakefile	Wed Jul 20 13:27:01 2011 +0200
@@ -443,25 +443,25 @@
   Library/AssocList.thy Library/BigO.thy Library/Binomial.thy		\
   Library/Bit.thy Library/Boolean_Algebra.thy Library/Cardinality.thy	\
   Library/Char_nat.thy Library/Code_Char.thy Library/Code_Char_chr.thy	\
-  Library/Code_Char_ord.thy Library/Code_Integer.thy Library/Code_Natural.thy			\
-  Library/Code_Prolog.thy Tools/Predicate_Compile/code_prolog.ML	\
-  Library/ContNotDenum.thy Library/Continuity.thy Library/Convex.thy	\
-  Library/Countable.thy Library/Diagonalize.thy Library/Dlist.thy	\
-  Library/Dlist_Cset.thy 						\
+  Library/Code_Char_ord.thy Library/Code_Integer.thy			\
+  Library/Code_Natural.thy Library/Code_Prolog.thy			\
+  Tools/Predicate_Compile/code_prolog.ML Library/ContNotDenum.thy	\
+  Library/Continuity.thy Library/Convex.thy Library/Countable.thy	\
+  Library/Diagonalize.thy Library/Dlist.thy Library/Dlist_Cset.thy 	\
   Library/Efficient_Nat.thy Library/Eval_Witness.thy 			\
-  Library/Executable_Set.thy Library/Extended_Reals.thy			\
-  Library/Float.thy Library/Formal_Power_Series.thy			\
-  Library/Fraction_Field.thy Library/FrechetDeriv.thy Library/Cset.thy	\
-  Library/FuncSet.thy Library/Function_Algebras.thy			\
-  Library/Fundamental_Theorem_Algebra.thy Library/Glbs.thy		\
-  Library/Indicator_Function.thy Library/Infinite_Set.thy		\
-  Library/Inner_Product.thy Library/Kleene_Algebra.thy			\
-  Library/LaTeXsugar.thy Library/Lattice_Algebras.thy			\
-  Library/Lattice_Syntax.thy Library/Library.thy Library/List_Cset.thy 	\
-  Library/List_Prefix.thy Library/List_lexord.thy Library/Mapping.thy	\
-  Library/Monad_Syntax.thy Library/More_List.thy Library/More_Set.thy	\
-  Library/Multiset.thy Library/Nat_Bijection.thy			\
-  Library/Nat_Infinity.thy Library/Nested_Environment.thy		\
+  Library/Executable_Set.thy Library/Extended_Real.thy			\
+  Library/Extended_Nat.thy Library/Float.thy				\
+  Library/Formal_Power_Series.thy Library/Fraction_Field.thy		\
+  Library/FrechetDeriv.thy Library/Cset.thy Library/FuncSet.thy		\
+  Library/Function_Algebras.thy Library/Fundamental_Theorem_Algebra.thy	\
+  Library/Glbs.thy Library/Indicator_Function.thy			\
+  Library/Infinite_Set.thy Library/Inner_Product.thy			\
+  Library/Kleene_Algebra.thy Library/LaTeXsugar.thy			\
+  Library/Lattice_Algebras.thy Library/Lattice_Syntax.thy		\
+  Library/Library.thy Library/List_Cset.thy Library/List_Prefix.thy	\
+  Library/List_lexord.thy Library/Mapping.thy Library/Monad_Syntax.thy	\
+  Library/More_List.thy Library/More_Set.thy Library/Multiset.thy	\
+  Library/Nat_Bijection.thy Library/Nested_Environment.thy		\
   Library/Numeral_Type.thy Library/OptionalSugar.thy			\
   Library/Order_Relation.thy Library/Permutation.thy			\
   Library/Permutations.thy Library/Poly_Deriv.thy			\
@@ -480,7 +480,7 @@
   Library/Sum_of_Squares/sos_wrapper.ML					\
   Library/Sum_of_Squares/sum_of_squares.ML				\
   Library/Transitive_Closure_Table.thy Library/Univ_Poly.thy		\
-  Library/While_Combinator.thy Library/Zorn.thy	\
+  Library/While_Combinator.thy Library/Zorn.thy				\
   $(SRC)/Tools/adhoc_overloading.ML Library/positivstellensatz.ML	\
   Library/reflection.ML Library/reify_data.ML				\
   Library/document/root.bib Library/document/root.tex
@@ -554,13 +554,11 @@
 ## HOL-Import
 
 IMPORTER_FILES = \
-  Import/ImportRecorder.thy Import/HOL4Compat.thy \
+  Import/HOL4Compat.thy \
   Import/HOLLightCompat.thy Import/HOL4Setup.thy Import/HOL4Syntax.thy \
   Import/MakeEqual.thy Import/ROOT.ML Import/hol4rews.ML \
-  Import/import.ML Import/importrecorder.ML Import/import_syntax.ML \
-  Import/lazy_seq.ML Import/mono_scan.ML Import/mono_seq.ML \
-  Import/proof_kernel.ML Import/replay.ML Import/scan.ML Import/seq.ML \
-  Import/shuffler.ML Import/xml.ML Import/xmlconv.ML \
+  Import/import.ML Import/import_syntax.ML \
+  Import/proof_kernel.ML Import/replay.ML Import/shuffler.ML \
   Library/ContNotDenum.thy Old_Number_Theory/Primes.thy
 
 HOL-Import: HOL $(LOG)/HOL-Import.gz
@@ -1202,7 +1200,7 @@
   Multivariate_Analysis/Topology_Euclidean_Space.thy			\
   Multivariate_Analysis/document/root.tex				\
   Multivariate_Analysis/normarith.ML Library/Glbs.thy			\
-  Library/Extended_Reals.thy Library/Indicator_Function.thy		\
+  Library/Extended_Real.thy Library/Indicator_Function.thy		\
   Library/Inner_Product.thy Library/Numeral_Type.thy Library/Convex.thy	\
   Library/FrechetDeriv.thy Library/Product_Vector.thy			\
   Library/Product_plus.thy
@@ -1575,7 +1573,7 @@
 HOLCF-Library: HOLCF $(LOG)/HOLCF-Library.gz
 
 $(LOG)/HOLCF-Library.gz: $(OUT)/HOLCF \
-  Library/Nat_Infinity.thy \
+  Library/Extended_Nat.thy \
   HOLCF/Library/Defl_Bifinite.thy \
   HOLCF/Library/Bool_Discrete.thy \
   HOLCF/Library/Char_Discrete.thy \
@@ -1629,7 +1627,7 @@
 HOLCF-FOCUS: HOLCF $(LOG)/HOLCF-FOCUS.gz
 
 $(LOG)/HOLCF-FOCUS.gz: $(OUT)/HOLCF HOLCF/FOCUS/ROOT.ML \
-  Library/Nat_Infinity.thy \
+  Library/Extended_Nat.thy \
   HOLCF/Library/Stream.thy \
   HOLCF/FOCUS/Fstreams.thy \
   HOLCF/FOCUS/Fstream.thy \
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Library/Extended_Nat.thy	Wed Jul 20 13:27:01 2011 +0200
@@ -0,0 +1,573 @@
+(*  Title:      HOL/Library/Extended_Nat.thy
+    Author:     David von Oheimb, TU Muenchen;  Florian Haftmann, TU Muenchen
+    Contributions: David Trachtenherz, TU Muenchen
+*)
+
+header {* Extended natural numbers (i.e. with infinity) *}
+
+theory Extended_Nat
+imports Main
+begin
+
+class infinity =
+  fixes infinity :: "'a"
+
+notation (xsymbols)
+  infinity  ("\<infinity>")
+
+notation (HTML output)
+  infinity  ("\<infinity>")
+
+subsection {* Type definition *}
+
+text {*
+  We extend the standard natural numbers by a special value indicating
+  infinity.
+*}
+
+typedef (open) enat = "UNIV :: nat option set" ..
+ 
+definition enat :: "nat \<Rightarrow> enat" where
+  "enat n = Abs_enat (Some n)"
+ 
+instantiation enat :: infinity
+begin
+  definition "\<infinity> = Abs_enat None"
+  instance proof qed
+end
+ 
+rep_datatype enat "\<infinity> :: enat"
+proof -
+  fix P i assume "\<And>j. P (enat j)" "P \<infinity>"
+  then show "P i"
+  proof induct
+    case (Abs_enat y) then show ?case
+      by (cases y rule: option.exhaust)
+         (auto simp: enat_def infinity_enat_def)
+  qed
+qed (auto simp add: enat_def infinity_enat_def Abs_enat_inject)
+
+declare [[coercion "enat::nat\<Rightarrow>enat"]]
+
+lemma not_Infty_eq[iff]: "(x \<noteq> \<infinity>) = (EX i. x = enat i)"
+by (cases x) auto
+
+lemma not_enat_eq [iff]: "(ALL y. x ~= enat y) = (x = \<infinity>)"
+by (cases x) auto
+
+primrec the_enat :: "enat \<Rightarrow> nat"
+where "the_enat (enat n) = n"
+
+subsection {* Constructors and numbers *}
+
+instantiation enat :: "{zero, one, number}"
+begin
+
+definition
+  "0 = enat 0"
+
+definition
+  [code_unfold]: "1 = enat 1"
+
+definition
+  [code_unfold, code del]: "number_of k = enat (number_of k)"
+
+instance ..
+
+end
+
+definition iSuc :: "enat \<Rightarrow> enat" where
+  "iSuc i = (case i of enat n \<Rightarrow> enat (Suc n) | \<infinity> \<Rightarrow> \<infinity>)"
+
+lemma enat_0: "enat 0 = 0"
+  by (simp add: zero_enat_def)
+
+lemma enat_1: "enat 1 = 1"
+  by (simp add: one_enat_def)
+
+lemma enat_number: "enat (number_of k) = number_of k"
+  by (simp add: number_of_enat_def)
+
+lemma one_iSuc: "1 = iSuc 0"
+  by (simp add: zero_enat_def one_enat_def iSuc_def)
+
+lemma Infty_ne_i0 [simp]: "(\<infinity>::enat) \<noteq> 0"
+  by (simp add: zero_enat_def)
+
+lemma i0_ne_Infty [simp]: "0 \<noteq> (\<infinity>::enat)"
+  by (simp add: zero_enat_def)
+
+lemma zero_enat_eq [simp]:
+  "number_of k = (0\<Colon>enat) \<longleftrightarrow> number_of k = (0\<Colon>nat)"
+  "(0\<Colon>enat) = number_of k \<longleftrightarrow> number_of k = (0\<Colon>nat)"
+  unfolding zero_enat_def number_of_enat_def by simp_all
+
+lemma one_enat_eq [simp]:
+  "number_of k = (1\<Colon>enat) \<longleftrightarrow> number_of k = (1\<Colon>nat)"
+  "(1\<Colon>enat) = number_of k \<longleftrightarrow> number_of k = (1\<Colon>nat)"
+  unfolding one_enat_def number_of_enat_def by simp_all
+
+lemma zero_one_enat_neq [simp]:
+  "\<not> 0 = (1\<Colon>enat)"
+  "\<not> 1 = (0\<Colon>enat)"
+  unfolding zero_enat_def one_enat_def by simp_all
+
+lemma Infty_ne_i1 [simp]: "(\<infinity>::enat) \<noteq> 1"
+  by (simp add: one_enat_def)
+
+lemma i1_ne_Infty [simp]: "1 \<noteq> (\<infinity>::enat)"
+  by (simp add: one_enat_def)
+
+lemma Infty_ne_number [simp]: "(\<infinity>::enat) \<noteq> number_of k"
+  by (simp add: number_of_enat_def)
+
+lemma number_ne_Infty [simp]: "number_of k \<noteq> (\<infinity>::enat)"
+  by (simp add: number_of_enat_def)
+
+lemma iSuc_enat: "iSuc (enat n) = enat (Suc n)"
+  by (simp add: iSuc_def)
+
+lemma iSuc_number_of: "iSuc (number_of k) = enat (Suc (number_of k))"
+  by (simp add: iSuc_enat number_of_enat_def)
+
+lemma iSuc_Infty [simp]: "iSuc \<infinity> = \<infinity>"
+  by (simp add: iSuc_def)
+
+lemma iSuc_ne_0 [simp]: "iSuc n \<noteq> 0"
+  by (simp add: iSuc_def zero_enat_def split: enat.splits)
+
+lemma zero_ne_iSuc [simp]: "0 \<noteq> iSuc n"
+  by (rule iSuc_ne_0 [symmetric])
+
+lemma iSuc_inject [simp]: "iSuc m = iSuc n \<longleftrightarrow> m = n"
+  by (simp add: iSuc_def split: enat.splits)
+
+lemma number_of_enat_inject [simp]:
+  "(number_of k \<Colon> enat) = number_of l \<longleftrightarrow> (number_of k \<Colon> nat) = number_of l"
+  by (simp add: number_of_enat_def)
+
+
+subsection {* Addition *}
+
+instantiation enat :: comm_monoid_add
+begin
+
+definition [nitpick_simp]:
+  "m + n = (case m of \<infinity> \<Rightarrow> \<infinity> | enat m \<Rightarrow> (case n of \<infinity> \<Rightarrow> \<infinity> | enat n \<Rightarrow> enat (m + n)))"
+
+lemma plus_enat_simps [simp, code]:
+  fixes q :: enat
+  shows "enat m + enat n = enat (m + n)"
+    and "\<infinity> + q = \<infinity>"
+    and "q + \<infinity> = \<infinity>"
+  by (simp_all add: plus_enat_def split: enat.splits)
+
+instance proof
+  fix n m q :: enat
+  show "n + m + q = n + (m + q)"
+    by (cases n, auto, cases m, auto, cases q, auto)
+  show "n + m = m + n"
+    by (cases n, auto, cases m, auto)
+  show "0 + n = n"
+    by (cases n) (simp_all add: zero_enat_def)
+qed
+
+end
+
+lemma plus_enat_0 [simp]:
+  "0 + (q\<Colon>enat) = q"
+  "(q\<Colon>enat) + 0 = q"
+  by (simp_all add: plus_enat_def zero_enat_def split: enat.splits)
+
+lemma plus_enat_number [simp]:
+  "(number_of k \<Colon> enat) + number_of l = (if k < Int.Pls then number_of l
+    else if l < Int.Pls then number_of k else number_of (k + l))"
+  unfolding number_of_enat_def plus_enat_simps nat_arith(1) if_distrib [symmetric, of _ enat] ..
+
+lemma iSuc_number [simp]:
+  "iSuc (number_of k) = (if neg (number_of k \<Colon> int) then 1 else number_of (Int.succ k))"
+  unfolding iSuc_number_of
+  unfolding one_enat_def number_of_enat_def Suc_nat_number_of if_distrib [symmetric] ..
+
+lemma iSuc_plus_1:
+  "iSuc n = n + 1"
+  by (cases n) (simp_all add: iSuc_enat one_enat_def)
+  
+lemma plus_1_iSuc:
+  "1 + q = iSuc q"
+  "q + 1 = iSuc q"
+by (simp_all add: iSuc_plus_1 add_ac)
+
+lemma iadd_Suc: "iSuc m + n = iSuc (m + n)"
+by (simp_all add: iSuc_plus_1 add_ac)
+
+lemma iadd_Suc_right: "m + iSuc n = iSuc (m + n)"
+by (simp only: add_commute[of m] iadd_Suc)
+
+lemma iadd_is_0: "(m + n = (0::enat)) = (m = 0 \<and> n = 0)"
+by (cases m, cases n, simp_all add: zero_enat_def)
+
+subsection {* Multiplication *}
+
+instantiation enat :: comm_semiring_1
+begin
+
+definition times_enat_def [nitpick_simp]:
+  "m * n = (case m of \<infinity> \<Rightarrow> if n = 0 then 0 else \<infinity> | enat m \<Rightarrow>
+    (case n of \<infinity> \<Rightarrow> if m = 0 then 0 else \<infinity> | enat n \<Rightarrow> enat (m * n)))"
+
+lemma times_enat_simps [simp, code]:
+  "enat m * enat n = enat (m * n)"
+  "\<infinity> * \<infinity> = (\<infinity>::enat)"
+  "\<infinity> * enat n = (if n = 0 then 0 else \<infinity>)"
+  "enat m * \<infinity> = (if m = 0 then 0 else \<infinity>)"
+  unfolding times_enat_def zero_enat_def
+  by (simp_all split: enat.split)
+
+instance proof
+  fix a b c :: enat
+  show "(a * b) * c = a * (b * c)"
+    unfolding times_enat_def zero_enat_def
+    by (simp split: enat.split)
+  show "a * b = b * a"
+    unfolding times_enat_def zero_enat_def
+    by (simp split: enat.split)
+  show "1 * a = a"
+    unfolding times_enat_def zero_enat_def one_enat_def
+    by (simp split: enat.split)
+  show "(a + b) * c = a * c + b * c"
+    unfolding times_enat_def zero_enat_def
+    by (simp split: enat.split add: left_distrib)
+  show "0 * a = 0"
+    unfolding times_enat_def zero_enat_def
+    by (simp split: enat.split)
+  show "a * 0 = 0"
+    unfolding times_enat_def zero_enat_def
+    by (simp split: enat.split)
+  show "(0::enat) \<noteq> 1"
+    unfolding zero_enat_def one_enat_def
+    by simp
+qed
+
+end
+
+lemma mult_iSuc: "iSuc m * n = n + m * n"
+  unfolding iSuc_plus_1 by (simp add: algebra_simps)
+
+lemma mult_iSuc_right: "m * iSuc n = m + m * n"
+  unfolding iSuc_plus_1 by (simp add: algebra_simps)
+
+lemma of_nat_eq_enat: "of_nat n = enat n"
+  apply (induct n)
+  apply (simp add: enat_0)
+  apply (simp add: plus_1_iSuc iSuc_enat)
+  done
+
+instance enat :: number_semiring
+proof
+  fix n show "number_of (int n) = (of_nat n :: enat)"
+    unfolding number_of_enat_def number_of_int of_nat_id of_nat_eq_enat ..
+qed
+
+instance enat :: semiring_char_0 proof
+  have "inj enat" by (rule injI) simp
+  then show "inj (\<lambda>n. of_nat n :: enat)" by (simp add: of_nat_eq_enat)
+qed
+
+lemma imult_is_0[simp]: "((m::enat) * n = 0) = (m = 0 \<or> n = 0)"
+by(auto simp add: times_enat_def zero_enat_def split: enat.split)
+
+lemma imult_is_Infty: "((a::enat) * b = \<infinity>) = (a = \<infinity> \<and> b \<noteq> 0 \<or> b = \<infinity> \<and> a \<noteq> 0)"
+by(auto simp add: times_enat_def zero_enat_def split: enat.split)
+
+
+subsection {* Subtraction *}
+
+instantiation enat :: minus
+begin
+
+definition diff_enat_def:
+"a - b = (case a of (enat x) \<Rightarrow> (case b of (enat y) \<Rightarrow> enat (x - y) | \<infinity> \<Rightarrow> 0)
+          | \<infinity> \<Rightarrow> \<infinity>)"
+
+instance ..
+
+end
+
+lemma idiff_enat_enat[simp,code]: "enat a - enat b = enat (a - b)"
+by(simp add: diff_enat_def)
+
+lemma idiff_Infty[simp,code]: "\<infinity> - n = (\<infinity>::enat)"
+by(simp add: diff_enat_def)
+
+lemma idiff_Infty_right[simp,code]: "enat a - \<infinity> = 0"
+by(simp add: diff_enat_def)
+
+lemma idiff_0[simp]: "(0::enat) - n = 0"
+by (cases n, simp_all add: zero_enat_def)
+
+lemmas idiff_enat_0[simp] = idiff_0[unfolded zero_enat_def]
+
+lemma idiff_0_right[simp]: "(n::enat) - 0 = n"
+by (cases n) (simp_all add: zero_enat_def)
+
+lemmas idiff_enat_0_right[simp] = idiff_0_right[unfolded zero_enat_def]
+
+lemma idiff_self[simp]: "n \<noteq> \<infinity> \<Longrightarrow> (n::enat) - n = 0"
+by(auto simp: zero_enat_def)
+
+lemma iSuc_minus_iSuc [simp]: "iSuc n - iSuc m = n - m"
+by(simp add: iSuc_def split: enat.split)
+
+lemma iSuc_minus_1 [simp]: "iSuc n - 1 = n"
+by(simp add: one_enat_def iSuc_enat[symmetric] zero_enat_def[symmetric])
+
+(*lemmas idiff_self_eq_0_enat = idiff_self_eq_0[unfolded zero_enat_def]*)
+
+subsection {* Ordering *}
+
+instantiation enat :: linordered_ab_semigroup_add
+begin
+
+definition [nitpick_simp]:
+  "m \<le> n = (case n of enat n1 \<Rightarrow> (case m of enat m1 \<Rightarrow> m1 \<le> n1 | \<infinity> \<Rightarrow> False)
+    | \<infinity> \<Rightarrow> True)"
+
+definition [nitpick_simp]:
+  "m < n = (case m of enat m1 \<Rightarrow> (case n of enat n1 \<Rightarrow> m1 < n1 | \<infinity> \<Rightarrow> True)
+    | \<infinity> \<Rightarrow> False)"
+
+lemma enat_ord_simps [simp]:
+  "enat m \<le> enat n \<longleftrightarrow> m \<le> n"
+  "enat m < enat n \<longleftrightarrow> m < n"
+  "q \<le> (\<infinity>::enat)"
+  "q < (\<infinity>::enat) \<longleftrightarrow> q \<noteq> \<infinity>"
+  "(\<infinity>::enat) \<le> q \<longleftrightarrow> q = \<infinity>"
+  "(\<infinity>::enat) < q \<longleftrightarrow> False"
+  by (simp_all add: less_eq_enat_def less_enat_def split: enat.splits)
+
+lemma enat_ord_code [code]:
+  "enat m \<le> enat n \<longleftrightarrow> m \<le> n"
+  "enat m < enat n \<longleftrightarrow> m < n"
+  "q \<le> (\<infinity>::enat) \<longleftrightarrow> True"
+  "enat m < \<infinity> \<longleftrightarrow> True"
+  "\<infinity> \<le> enat n \<longleftrightarrow> False"
+  "(\<infinity>::enat) < q \<longleftrightarrow> False"
+  by simp_all
+
+instance by default
+  (auto simp add: less_eq_enat_def less_enat_def plus_enat_def split: enat.splits)
+
+end
+
+instance enat :: ordered_comm_semiring
+proof
+  fix a b c :: enat
+  assume "a \<le> b" and "0 \<le> c"
+  thus "c * a \<le> c * b"
+    unfolding times_enat_def less_eq_enat_def zero_enat_def
+    by (simp split: enat.splits)
+qed
+
+lemma enat_ord_number [simp]:
+  "(number_of m \<Colon> enat) \<le> number_of n \<longleftrightarrow> (number_of m \<Colon> nat) \<le> number_of n"
+  "(number_of m \<Colon> enat) < number_of n \<longleftrightarrow> (number_of m \<Colon> nat) < number_of n"
+  by (simp_all add: number_of_enat_def)
+
+lemma i0_lb [simp]: "(0\<Colon>enat) \<le> n"
+  by (simp add: zero_enat_def less_eq_enat_def split: enat.splits)
+
+lemma ile0_eq [simp]: "n \<le> (0\<Colon>enat) \<longleftrightarrow> n = 0"
+by (simp add: zero_enat_def less_eq_enat_def split: enat.splits)
+
+lemma Infty_ileE [elim!]: "\<infinity> \<le> enat m \<Longrightarrow> R"
+  by (simp add: zero_enat_def less_eq_enat_def split: enat.splits)
+
+lemma Infty_ilessE [elim!]: "\<infinity> < enat m \<Longrightarrow> R"
+  by simp
+
+lemma not_iless0 [simp]: "\<not> n < (0\<Colon>enat)"
+  by (simp add: zero_enat_def less_enat_def split: enat.splits)
+
+lemma i0_less [simp]: "(0\<Colon>enat) < n \<longleftrightarrow> n \<noteq> 0"
+by (simp add: zero_enat_def less_enat_def split: enat.splits)
+
+lemma iSuc_ile_mono [simp]: "iSuc n \<le> iSuc m \<longleftrightarrow> n \<le> m"
+  by (simp add: iSuc_def less_eq_enat_def split: enat.splits)
+ 
+lemma iSuc_mono [simp]: "iSuc n < iSuc m \<longleftrightarrow> n < m"
+  by (simp add: iSuc_def less_enat_def split: enat.splits)
+
+lemma ile_iSuc [simp]: "n \<le> iSuc n"
+  by (simp add: iSuc_def less_eq_enat_def split: enat.splits)
+
+lemma not_iSuc_ilei0 [simp]: "\<not> iSuc n \<le> 0"
+  by (simp add: zero_enat_def iSuc_def less_eq_enat_def split: enat.splits)
+
+lemma i0_iless_iSuc [simp]: "0 < iSuc n"
+  by (simp add: zero_enat_def iSuc_def less_enat_def split: enat.splits)
+
+lemma iless_iSuc0[simp]: "(n < iSuc 0) = (n = 0)"
+by (simp add: zero_enat_def iSuc_def less_enat_def split: enat.split)
+
+lemma ileI1: "m < n \<Longrightarrow> iSuc m \<le> n"
+  by (simp add: iSuc_def less_eq_enat_def less_enat_def split: enat.splits)
+
+lemma Suc_ile_eq: "enat (Suc m) \<le> n \<longleftrightarrow> enat m < n"
+  by (cases n) auto
+
+lemma iless_Suc_eq [simp]: "enat m < iSuc n \<longleftrightarrow> enat m \<le> n"
+  by (auto simp add: iSuc_def less_enat_def split: enat.splits)
+
+lemma imult_Infty: "(0::enat) < n \<Longrightarrow> \<infinity> * n = \<infinity>"
+by (simp add: zero_enat_def less_enat_def split: enat.splits)
+
+lemma imult_Infty_right: "(0::enat) < n \<Longrightarrow> n * \<infinity> = \<infinity>"
+by (simp add: zero_enat_def less_enat_def split: enat.splits)
+
+lemma enat_0_less_mult_iff: "(0 < (m::enat) * n) = (0 < m \<and> 0 < n)"
+by (simp only: i0_less imult_is_0, simp)
+
+lemma mono_iSuc: "mono iSuc"
+by(simp add: mono_def)
+
+
+lemma min_enat_simps [simp]:
+  "min (enat m) (enat n) = enat (min m n)"
+  "min q 0 = 0"
+  "min 0 q = 0"
+  "min q (\<infinity>::enat) = q"
+  "min (\<infinity>::enat) q = q"
+  by (auto simp add: min_def)
+
+lemma max_enat_simps [simp]:
+  "max (enat m) (enat n) = enat (max m n)"
+  "max q 0 = q"
+  "max 0 q = q"
+  "max q \<infinity> = (\<infinity>::enat)"
+  "max \<infinity> q = (\<infinity>::enat)"
+  by (simp_all add: max_def)
+
+lemma enat_ile: "n \<le> enat m \<Longrightarrow> \<exists>k. n = enat k"
+  by (cases n) simp_all
+
+lemma enat_iless: "n < enat m \<Longrightarrow> \<exists>k. n = enat k"
+  by (cases n) simp_all
+
+lemma chain_incr: "\<forall>i. \<exists>j. Y i < Y j ==> \<exists>j. enat k < Y j"
+apply (induct_tac k)
+ apply (simp (no_asm) only: enat_0)
+ apply (fast intro: le_less_trans [OF i0_lb])
+apply (erule exE)
+apply (drule spec)
+apply (erule exE)
+apply (drule ileI1)
+apply (rule iSuc_enat [THEN subst])
+apply (rule exI)
+apply (erule (1) le_less_trans)
+done
+
+instantiation enat :: "{bot, top}"
+begin
+
+definition bot_enat :: enat where
+  "bot_enat = 0"
+
+definition top_enat :: enat where
+  "top_enat = \<infinity>"
+
+instance proof
+qed (simp_all add: bot_enat_def top_enat_def)
+
+end
+
+lemma finite_enat_bounded:
+  assumes le_fin: "\<And>y. y \<in> A \<Longrightarrow> y \<le> enat n"
+  shows "finite A"
+proof (rule finite_subset)
+  show "finite (enat ` {..n})" by blast
+
+  have "A \<subseteq> {..enat n}" using le_fin by fastsimp
+  also have "\<dots> \<subseteq> enat ` {..n}"
+    by (rule subsetI) (case_tac x, auto)
+  finally show "A \<subseteq> enat ` {..n}" .
+qed
+
+
+subsection {* Well-ordering *}
+
+lemma less_enatE:
+  "[| n < enat m; !!k. n = enat k ==> k < m ==> P |] ==> P"
+by (induct n) auto
+
+lemma less_InftyE:
+  "[| n < \<infinity>; !!k. n = enat k ==> P |] ==> P"
+by (induct n) auto
+
+lemma enat_less_induct:
+  assumes prem: "!!n. \<forall>m::enat. m < n --> P m ==> P n" shows "P n"
+proof -
+  have P_enat: "!!k. P (enat k)"
+    apply (rule nat_less_induct)
+    apply (rule prem, clarify)
+    apply (erule less_enatE, simp)
+    done
+  show ?thesis
+  proof (induct n)
+    fix nat
+    show "P (enat nat)" by (rule P_enat)
+  next
+    show "P \<infinity>"
+      apply (rule prem, clarify)
+      apply (erule less_InftyE)
+      apply (simp add: P_enat)
+      done
+  qed
+qed
+
+instance enat :: wellorder
+proof
+  fix P and n
+  assume hyp: "(\<And>n\<Colon>enat. (\<And>m\<Colon>enat. m < n \<Longrightarrow> P m) \<Longrightarrow> P n)"
+  show "P n" by (blast intro: enat_less_induct hyp)
+qed
+
+subsection {* Complete Lattice *}
+
+instantiation enat :: complete_lattice
+begin
+
+definition inf_enat :: "enat \<Rightarrow> enat \<Rightarrow> enat" where
+  "inf_enat \<equiv> min"
+
+definition sup_enat :: "enat \<Rightarrow> enat \<Rightarrow> enat" where
+  "sup_enat \<equiv> max"
+
+definition Inf_enat :: "enat set \<Rightarrow> enat" where
+  "Inf_enat A \<equiv> if A = {} then \<infinity> else (LEAST x. x \<in> A)"
+
+definition Sup_enat :: "enat set \<Rightarrow> enat" where
+  "Sup_enat A \<equiv> if A = {} then 0
+    else if finite A then Max A
+                     else \<infinity>"
+instance proof
+  fix x :: "enat" and A :: "enat set"
+  { assume "x \<in> A" then show "Inf A \<le> x"
+      unfolding Inf_enat_def by (auto intro: Least_le) }
+  { assume "\<And>y. y \<in> A \<Longrightarrow> x \<le> y" then show "x \<le> Inf A"
+      unfolding Inf_enat_def
+      by (cases "A = {}") (auto intro: LeastI2_ex) }
+  { assume "x \<in> A" then show "x \<le> Sup A"
+      unfolding Sup_enat_def by (cases "finite A") auto }
+  { assume "\<And>y. y \<in> A \<Longrightarrow> y \<le> x" then show "Sup A \<le> x"
+      unfolding Sup_enat_def using finite_enat_bounded by auto }
+qed (simp_all add: inf_enat_def sup_enat_def)
+end
+
+
+subsection {* Traditional theorem names *}
+
+lemmas enat_defs = zero_enat_def one_enat_def number_of_enat_def iSuc_def
+  plus_enat_def less_eq_enat_def less_enat_def
+
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Library/Extended_Real.thy	Wed Jul 20 13:27:01 2011 +0200
@@ -0,0 +1,2543 @@
+(*  Title:      HOL/Library/Extended_Real.thy
+    Author:     Johannes Hölzl, TU München
+    Author:     Robert Himmelmann, TU München
+    Author:     Armin Heller, TU München
+    Author:     Bogdan Grechuk, University of Edinburgh
+*)
+
+header {* Extended real number line *}
+
+theory Extended_Real
+  imports Complex_Main Extended_Nat
+begin
+
+text {*
+
+For more lemmas about the extended real numbers go to
+  @{text "src/HOL/Multivariate_Analysis/Extended_Real_Limits.thy"}
+
+*}
+
+lemma (in complete_lattice) atLeast_eq_UNIV_iff: "{x..} = UNIV \<longleftrightarrow> x = bot"
+proof
+  assume "{x..} = UNIV"
+  show "x = bot"
+  proof (rule ccontr)
+    assume "x \<noteq> bot" then have "bot \<notin> {x..}" by (simp add: le_less)
+    then show False using `{x..} = UNIV` by simp
+  qed
+qed auto
+
+lemma SUPR_pair:
+  "(SUP i : A. SUP j : B. f i j) = (SUP p : A \<times> B. f (fst p) (snd p))"
+  by (rule antisym) (auto intro!: SUP_leI le_SUPI2)
+
+lemma INFI_pair:
+  "(INF i : A. INF j : B. f i j) = (INF p : A \<times> B. f (fst p) (snd p))"
+  by (rule antisym) (auto intro!: le_INFI INF_leI2)
+
+subsection {* Definition and basic properties *}
+
+datatype ereal = ereal real | PInfty | MInfty
+
+instantiation ereal :: uminus
+begin
+  fun uminus_ereal where
+    "- (ereal r) = ereal (- r)"
+  | "- PInfty = MInfty"
+  | "- MInfty = PInfty"
+  instance ..
+end
+
+instantiation ereal :: infinity
+begin
+  definition "(\<infinity>::ereal) = PInfty"
+  instance ..
+end
+
+definition "ereal_of_enat n = (case n of enat n \<Rightarrow> ereal (real n) | \<infinity> \<Rightarrow> \<infinity>)"
+
+declare [[coercion "ereal :: real \<Rightarrow> ereal"]]
+declare [[coercion "ereal_of_enat :: enat \<Rightarrow> ereal"]]
+declare [[coercion "(\<lambda>n. ereal (of_nat n)) :: nat \<Rightarrow> ereal"]]
+
+lemma ereal_uminus_uminus[simp]:
+  fixes a :: ereal shows "- (- a) = a"
+  by (cases a) simp_all
+
+lemma
+  shows PInfty_eq_infinity[simp]: "PInfty = \<infinity>"
+    and MInfty_eq_minfinity[simp]: "MInfty = - \<infinity>"
+    and MInfty_neq_PInfty[simp]: "\<infinity> \<noteq> - (\<infinity>::ereal)" "- \<infinity> \<noteq> (\<infinity>::ereal)"
+    and MInfty_neq_ereal[simp]: "ereal r \<noteq> - \<infinity>" "- \<infinity> \<noteq> ereal r"
+    and PInfty_neq_ereal[simp]: "ereal r \<noteq> \<infinity>" "\<infinity> \<noteq> ereal r"
+    and PInfty_cases[simp]: "(case \<infinity> of ereal r \<Rightarrow> f r | PInfty \<Rightarrow> y | MInfty \<Rightarrow> z) = y"
+    and MInfty_cases[simp]: "(case - \<infinity> of ereal r \<Rightarrow> f r | PInfty \<Rightarrow> y | MInfty \<Rightarrow> z) = z"
+  by (simp_all add: infinity_ereal_def)
+
+lemma inj_ereal[simp]: "inj_on ereal A"
+  unfolding inj_on_def by auto
+
+lemma ereal_cases[case_names real PInf MInf, cases type: ereal]:
+  assumes "\<And>r. x = ereal r \<Longrightarrow> P"
+  assumes "x = \<infinity> \<Longrightarrow> P"
+  assumes "x = -\<infinity> \<Longrightarrow> P"
+  shows P
+  using assms by (cases x) auto
+
+lemmas ereal2_cases = ereal_cases[case_product ereal_cases]
+lemmas ereal3_cases = ereal2_cases[case_product ereal_cases]
+
+lemma ereal_uminus_eq_iff[simp]:
+  fixes a b :: ereal shows "-a = -b \<longleftrightarrow> a = b"
+  by (cases rule: ereal2_cases[of a b]) simp_all
+
+function of_ereal :: "ereal \<Rightarrow> real" where
+"of_ereal (ereal r) = r" |
+"of_ereal \<infinity> = 0" |
+"of_ereal (-\<infinity>) = 0"
+  by (auto intro: ereal_cases)
+termination proof qed (rule wf_empty)
+
+defs (overloaded)
+  real_of_ereal_def [code_unfold]: "real \<equiv> of_ereal"
+
+lemma real_of_ereal[simp]:
+    "real (- x :: ereal) = - (real x)"
+    "real (ereal r) = r"
+    "real (\<infinity>::ereal) = 0"
+  by (cases x) (simp_all add: real_of_ereal_def)
+
+lemma range_ereal[simp]: "range ereal = UNIV - {\<infinity>, -\<infinity>}"
+proof safe
+  fix x assume "x \<notin> range ereal" "x \<noteq> \<infinity>"
+  then show "x = -\<infinity>" by (cases x) auto
+qed auto
+
+lemma ereal_range_uminus[simp]: "range uminus = (UNIV::ereal set)"
+proof safe
+  fix x :: ereal show "x \<in> range uminus" by (intro image_eqI[of _ _ "-x"]) auto
+qed auto
+
+instantiation ereal :: number
+begin
+definition [simp]: "number_of x = ereal (number_of x)"
+instance proof qed
+end
+
+instantiation ereal :: abs
+begin
+  function abs_ereal where
+    "\<bar>ereal r\<bar> = ereal \<bar>r\<bar>"
+  | "\<bar>-\<infinity>\<bar> = (\<infinity>::ereal)"
+  | "\<bar>\<infinity>\<bar> = (\<infinity>::ereal)"
+  by (auto intro: ereal_cases)
+  termination proof qed (rule wf_empty)
+  instance ..
+end
+
+lemma abs_eq_infinity_cases[elim!]: "\<lbrakk> \<bar>x :: ereal\<bar> = \<infinity> ; x = \<infinity> \<Longrightarrow> P ; x = -\<infinity> \<Longrightarrow> P \<rbrakk> \<Longrightarrow> P"
+  by (cases x) auto
+
+lemma abs_neq_infinity_cases[elim!]: "\<lbrakk> \<bar>x :: ereal\<bar> \<noteq> \<infinity> ; \<And>r. x = ereal r \<Longrightarrow> P \<rbrakk> \<Longrightarrow> P"
+  by (cases x) auto
+
+lemma abs_ereal_uminus[simp]: "\<bar>- x\<bar> = \<bar>x::ereal\<bar>"
+  by (cases x) auto
+
+subsubsection "Addition"
+
+instantiation ereal :: comm_monoid_add
+begin
+
+definition "0 = ereal 0"
+
+function plus_ereal where
+"ereal r + ereal p = ereal (r + p)" |
+"\<infinity> + a = (\<infinity>::ereal)" |
+"a + \<infinity> = (\<infinity>::ereal)" |
+"ereal r + -\<infinity> = - \<infinity>" |
+"-\<infinity> + ereal p = -(\<infinity>::ereal)" |
+"-\<infinity> + -\<infinity> = -(\<infinity>::ereal)"
+proof -
+  case (goal1 P x)
+  moreover then obtain a b where "x = (a, b)" by (cases x) auto
+  ultimately show P
+   by (cases rule: ereal2_cases[of a b]) auto
+qed auto
+termination proof qed (rule wf_empty)
+
+lemma Infty_neq_0[simp]:
+  "(\<infinity>::ereal) \<noteq> 0" "0 \<noteq> (\<infinity>::ereal)"
+  "-(\<infinity>::ereal) \<noteq> 0" "0 \<noteq> -(\<infinity>::ereal)"
+  by (simp_all add: zero_ereal_def)
+
+lemma ereal_eq_0[simp]:
+  "ereal r = 0 \<longleftrightarrow> r = 0"
+  "0 = ereal r \<longleftrightarrow> r = 0"
+  unfolding zero_ereal_def by simp_all
+
+instance
+proof
+  fix a :: ereal show "0 + a = a"
+    by (cases a) (simp_all add: zero_ereal_def)
+  fix b :: ereal show "a + b = b + a"
+    by (cases rule: ereal2_cases[of a b]) simp_all
+  fix c :: ereal show "a + b + c = a + (b + c)"
+    by (cases rule: ereal3_cases[of a b c]) simp_all
+qed
+end
+
+lemma real_of_ereal_0[simp]: "real (0::ereal) = 0"
+  unfolding real_of_ereal_def zero_ereal_def by simp
+
+lemma abs_ereal_zero[simp]: "\<bar>0\<bar> = (0::ereal)"
+  unfolding zero_ereal_def abs_ereal.simps by simp
+
+lemma ereal_uminus_zero[simp]:
+  "- 0 = (0::ereal)"
+  by (simp add: zero_ereal_def)
+
+lemma ereal_uminus_zero_iff[simp]:
+  fixes a :: ereal shows "-a = 0 \<longleftrightarrow> a = 0"
+  by (cases a) simp_all
+
+lemma ereal_plus_eq_PInfty[simp]:
+  fixes a b :: ereal shows "a + b = \<infinity> \<longleftrightarrow> a = \<infinity> \<or> b = \<infinity>"
+  by (cases rule: ereal2_cases[of a b]) auto
+
+lemma ereal_plus_eq_MInfty[simp]:
+  fixes a b :: ereal shows "a + b = -\<infinity> \<longleftrightarrow>
+    (a = -\<infinity> \<or> b = -\<infinity>) \<and> a \<noteq> \<infinity> \<and> b \<noteq> \<infinity>"
+  by (cases rule: ereal2_cases[of a b]) auto
+
+lemma ereal_add_cancel_left:
+  fixes a b :: ereal assumes "a \<noteq> -\<infinity>"
+  shows "a + b = a + c \<longleftrightarrow> (a = \<infinity> \<or> b = c)"
+  using assms by (cases rule: ereal3_cases[of a b c]) auto
+
+lemma ereal_add_cancel_right:
+  fixes a b :: ereal assumes "a \<noteq> -\<infinity>"
+  shows "b + a = c + a \<longleftrightarrow> (a = \<infinity> \<or> b = c)"
+  using assms by (cases rule: ereal3_cases[of a b c]) auto
+
+lemma ereal_real:
+  "ereal (real x) = (if \<bar>x\<bar> = \<infinity> then 0 else x)"
+  by (cases x) simp_all
+
+lemma real_of_ereal_add:
+  fixes a b :: ereal
+  shows "real (a + b) = (if (\<bar>a\<bar> = \<infinity>) \<and> (\<bar>b\<bar> = \<infinity>) \<or> (\<bar>a\<bar> \<noteq> \<infinity>) \<and> (\<bar>b\<bar> \<noteq> \<infinity>) then real a + real b else 0)"
+  by (cases rule: ereal2_cases[of a b]) auto
+
+subsubsection "Linear order on @{typ ereal}"
+
+instantiation ereal :: linorder
+begin
+
+function less_ereal where
+"   ereal x < ereal y     \<longleftrightarrow> x < y" |
+"(\<infinity>::ereal) < a           \<longleftrightarrow> False" |
+"         a < -(\<infinity>::ereal) \<longleftrightarrow> False" |
+"ereal x    < \<infinity>           \<longleftrightarrow> True" |
+"        -\<infinity> < ereal r     \<longleftrightarrow> True" |
+"        -\<infinity> < (\<infinity>::ereal) \<longleftrightarrow> True"
+proof -
+  case (goal1 P x)
+  moreover then obtain a b where "x = (a,b)" by (cases x) auto
+  ultimately show P by (cases rule: ereal2_cases[of a b]) auto
+qed simp_all
+termination by (relation "{}") simp
+
+definition "x \<le> (y::ereal) \<longleftrightarrow> x < y \<or> x = y"
+
+lemma ereal_infty_less[simp]:
+  fixes x :: ereal
+  shows "x < \<infinity> \<longleftrightarrow> (x \<noteq> \<infinity>)"
+    "-\<infinity> < x \<longleftrightarrow> (x \<noteq> -\<infinity>)"
+  by (cases x, simp_all) (cases x, simp_all)
+
+lemma ereal_infty_less_eq[simp]:
+  fixes x :: ereal
+  shows "\<infinity> \<le> x \<longleftrightarrow> x = \<infinity>"
+  "x \<le> -\<infinity> \<longleftrightarrow> x = -\<infinity>"
+  by (auto simp add: less_eq_ereal_def)
+
+lemma ereal_less[simp]:
+  "ereal r < 0 \<longleftrightarrow> (r < 0)"
+  "0 < ereal r \<longleftrightarrow> (0 < r)"
+  "0 < (\<infinity>::ereal)"
+  "-(\<infinity>::ereal) < 0"
+  by (simp_all add: zero_ereal_def)
+
+lemma ereal_less_eq[simp]:
+  "x \<le> (\<infinity>::ereal)"
+  "-(\<infinity>::ereal) \<le> x"
+  "ereal r \<le> ereal p \<longleftrightarrow> r \<le> p"
+  "ereal r \<le> 0 \<longleftrightarrow> r \<le> 0"
+  "0 \<le> ereal r \<longleftrightarrow> 0 \<le> r"
+  by (auto simp add: less_eq_ereal_def zero_ereal_def)
+
+lemma ereal_infty_less_eq2:
+  "a \<le> b \<Longrightarrow> a = \<infinity> \<Longrightarrow> b = (\<infinity>::ereal)"
+  "a \<le> b \<Longrightarrow> b = -\<infinity> \<Longrightarrow> a = -(\<infinity>::ereal)"
+  by simp_all
+
+instance
+proof
+  fix x :: ereal show "x \<le> x"
+    by (cases x) simp_all
+  fix y :: ereal show "x < y \<longleftrightarrow> x \<le> y \<and> \<not> y \<le> x"
+    by (cases rule: ereal2_cases[of x y]) auto
+  show "x \<le> y \<or> y \<le> x "
+    by (cases rule: ereal2_cases[of x y]) auto
+  { assume "x \<le> y" "y \<le> x" then show "x = y"
+    by (cases rule: ereal2_cases[of x y]) auto }
+  { fix z assume "x \<le> y" "y \<le> z" then show "x \<le> z"
+    by (cases rule: ereal3_cases[of x y z]) auto }
+qed
+end
+
+instance ereal :: ordered_ab_semigroup_add
+proof
+  fix a b c :: ereal assume "a \<le> b" then show "c + a \<le> c + b"
+    by (cases rule: ereal3_cases[of a b c]) auto
+qed
+
+lemma real_of_ereal_positive_mono:
+  fixes x y :: ereal shows "\<lbrakk>0 \<le> x; x \<le> y; y \<noteq> \<infinity>\<rbrakk> \<Longrightarrow> real x \<le> real y"
+  by (cases rule: ereal2_cases[of x y]) auto
+
+lemma ereal_MInfty_lessI[intro, simp]:
+  fixes a :: ereal shows "a \<noteq> -\<infinity> \<Longrightarrow> -\<infinity> < a"
+  by (cases a) auto
+
+lemma ereal_less_PInfty[intro, simp]:
+  fixes a :: ereal shows "a \<noteq> \<infinity> \<Longrightarrow> a < \<infinity>"
+  by (cases a) auto
+
+lemma ereal_less_ereal_Ex:
+  fixes a b :: ereal
+  shows "x < ereal r \<longleftrightarrow> x = -\<infinity> \<or> (\<exists>p. p < r \<and> x = ereal p)"
+  by (cases x) auto
+
+lemma less_PInf_Ex_of_nat: "x \<noteq> \<infinity> \<longleftrightarrow> (\<exists>n::nat. x < ereal (real n))"
+proof (cases x)
+  case (real r) then show ?thesis
+    using reals_Archimedean2[of r] by simp
+qed simp_all
+
+lemma ereal_add_mono:
+  fixes a b c d :: ereal assumes "a \<le> b" "c \<le> d" shows "a + c \<le> b + d"
+  using assms
+  apply (cases a)
+  apply (cases rule: ereal3_cases[of b c d], auto)
+  apply (cases rule: ereal3_cases[of b c d], auto)
+  done
+
+lemma ereal_minus_le_minus[simp]:
+  fixes a b :: ereal shows "- a \<le> - b \<longleftrightarrow> b \<le> a"
+  by (cases rule: ereal2_cases[of a b]) auto
+
+lemma ereal_minus_less_minus[simp]:
+  fixes a b :: ereal shows "- a < - b \<longleftrightarrow> b < a"
+  by (cases rule: ereal2_cases[of a b]) auto
+
+lemma ereal_le_real_iff:
+  "x \<le> real y \<longleftrightarrow> ((\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> ereal x \<le> y) \<and> (\<bar>y\<bar> = \<infinity> \<longrightarrow> x \<le> 0))"
+  by (cases y) auto
+
+lemma real_le_ereal_iff:
+  "real y \<le> x \<longleftrightarrow> ((\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> y \<le> ereal x) \<and> (\<bar>y\<bar> = \<infinity> \<longrightarrow> 0 \<le> x))"
+  by (cases y) auto
+
+lemma ereal_less_real_iff:
+  "x < real y \<longleftrightarrow> ((\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> ereal x < y) \<and> (\<bar>y\<bar> = \<infinity> \<longrightarrow> x < 0))"
+  by (cases y) auto
+
+lemma real_less_ereal_iff:
+  "real y < x \<longleftrightarrow> ((\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> y < ereal x) \<and> (\<bar>y\<bar> = \<infinity> \<longrightarrow> 0 < x))"
+  by (cases y) auto
+
+lemma real_of_ereal_pos:
+  fixes x :: ereal shows "0 \<le> x \<Longrightarrow> 0 \<le> real x" by (cases x) auto
+
+lemmas real_of_ereal_ord_simps =
+  ereal_le_real_iff real_le_ereal_iff ereal_less_real_iff real_less_ereal_iff
+
+lemma abs_ereal_ge0[simp]: "0 \<le> x \<Longrightarrow> \<bar>x :: ereal\<bar> = x"
+  by (cases x) auto
+
+lemma abs_ereal_less0[simp]: "x < 0 \<Longrightarrow> \<bar>x :: ereal\<bar> = -x"
+  by (cases x) auto
+
+lemma abs_ereal_pos[simp]: "0 \<le> \<bar>x :: ereal\<bar>"
+  by (cases x) auto
+
+lemma real_of_ereal_le_0[simp]: "real (x :: ereal) \<le> 0 \<longleftrightarrow> (x \<le> 0 \<or> x = \<infinity>)"
+  by (cases x) auto
+
+lemma abs_real_of_ereal[simp]: "\<bar>real (x :: ereal)\<bar> = real \<bar>x\<bar>"
+  by (cases x) auto
+
+lemma zero_less_real_of_ereal:
+  fixes x :: ereal shows "0 < real x \<longleftrightarrow> (0 < x \<and> x \<noteq> \<infinity>)"
+  by (cases x) auto
+
+lemma ereal_0_le_uminus_iff[simp]:
+  fixes a :: ereal shows "0 \<le> -a \<longleftrightarrow> a \<le> 0"
+  by (cases rule: ereal2_cases[of a]) auto
+
+lemma ereal_uminus_le_0_iff[simp]:
+  fixes a :: ereal shows "-a \<le> 0 \<longleftrightarrow> 0 \<le> a"
+  by (cases rule: ereal2_cases[of a]) auto
+
+lemma ereal_dense2: "x < y \<Longrightarrow> \<exists>z. x < ereal z \<and> ereal z < y"
+  using lt_ex gt_ex dense by (cases x y rule: ereal2_cases) auto
+
+lemma ereal_dense:
+  fixes x y :: ereal assumes "x < y"
+  shows "\<exists>z. x < z \<and> z < y"
+  using ereal_dense2[OF `x < y`] by blast
+
+lemma ereal_add_strict_mono:
+  fixes a b c d :: ereal
+  assumes "a = b" "0 \<le> a" "a \<noteq> \<infinity>" "c < d"
+  shows "a + c < b + d"
+  using assms by (cases rule: ereal3_cases[case_product ereal_cases, of a b c d]) auto
+
+lemma ereal_less_add: 
+  fixes a b c :: ereal shows "\<bar>a\<bar> \<noteq> \<infinity> \<Longrightarrow> c < b \<Longrightarrow> a + c < a + b"
+  by (cases rule: ereal2_cases[of b c]) auto
+
+lemma ereal_uminus_eq_reorder: "- a = b \<longleftrightarrow> a = (-b::ereal)" by auto
+
+lemma ereal_uminus_less_reorder: "- a < b \<longleftrightarrow> -b < (a::ereal)"
+  by (subst (3) ereal_uminus_uminus[symmetric]) (simp only: ereal_minus_less_minus)
+
+lemma ereal_uminus_le_reorder: "- a \<le> b \<longleftrightarrow> -b \<le> (a::ereal)"
+  by (subst (3) ereal_uminus_uminus[symmetric]) (simp only: ereal_minus_le_minus)
+
+lemmas ereal_uminus_reorder =
+  ereal_uminus_eq_reorder ereal_uminus_less_reorder ereal_uminus_le_reorder
+
+lemma ereal_bot:
+  fixes x :: ereal assumes "\<And>B. x \<le> ereal B" shows "x = - \<infinity>"
+proof (cases x)
+  case (real r) with assms[of "r - 1"] show ?thesis by auto
+next case PInf with assms[of 0] show ?thesis by auto
+next case MInf then show ?thesis by simp
+qed
+
+lemma ereal_top:
+  fixes x :: ereal assumes "\<And>B. x \<ge> ereal B" shows "x = \<infinity>"
+proof (cases x)
+  case (real r) with assms[of "r + 1"] show ?thesis by auto
+next case MInf with assms[of 0] show ?thesis by auto
+next case PInf then show ?thesis by simp
+qed
+
+lemma
+  shows ereal_max[simp]: "ereal (max x y) = max (ereal x) (ereal y)"
+    and ereal_min[simp]: "ereal (min x y) = min (ereal x) (ereal y)"
+  by (simp_all add: min_def max_def)
+
+lemma ereal_max_0: "max 0 (ereal r) = ereal (max 0 r)"
+  by (auto simp: zero_ereal_def)
+
+lemma
+  fixes f :: "nat \<Rightarrow> ereal"
+  shows incseq_uminus[simp]: "incseq (\<lambda>x. - f x) \<longleftrightarrow> decseq f"
+  and decseq_uminus[simp]: "decseq (\<lambda>x. - f x) \<longleftrightarrow> incseq f"
+  unfolding decseq_def incseq_def by auto
+
+lemma incseq_ereal: "incseq f \<Longrightarrow> incseq (\<lambda>x. ereal (f x))"
+  unfolding incseq_def by auto
+
+lemma ereal_add_nonneg_nonneg:
+  fixes a b :: ereal shows "0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> 0 \<le> a + b"
+  using add_mono[of 0 a 0 b] by simp
+
+lemma image_eqD: "f ` A = B \<Longrightarrow> (\<forall>x\<in>A. f x \<in> B)"
+  by auto
+
+lemma incseq_setsumI:
+  fixes f :: "nat \<Rightarrow> 'a::{comm_monoid_add, ordered_ab_semigroup_add}"
+  assumes "\<And>i. 0 \<le> f i"
+  shows "incseq (\<lambda>i. setsum f {..< i})"
+proof (intro incseq_SucI)
+  fix n have "setsum f {..< n} + 0 \<le> setsum f {..<n} + f n"
+    using assms by (rule add_left_mono)
+  then show "setsum f {..< n} \<le> setsum f {..< Suc n}"
+    by auto
+qed
+
+lemma incseq_setsumI2:
+  fixes f :: "'i \<Rightarrow> nat \<Rightarrow> 'a::{comm_monoid_add, ordered_ab_semigroup_add}"
+  assumes "\<And>n. n \<in> A \<Longrightarrow> incseq (f n)"
+  shows "incseq (\<lambda>i. \<Sum>n\<in>A. f n i)"
+  using assms unfolding incseq_def by (auto intro: setsum_mono)
+
+subsubsection "Multiplication"
+
+instantiation ereal :: "{comm_monoid_mult, sgn}"
+begin
+
+definition "1 = ereal 1"
+
+function sgn_ereal where
+  "sgn (ereal r) = ereal (sgn r)"
+| "sgn (\<infinity>::ereal) = 1"
+| "sgn (-\<infinity>::ereal) = -1"
+by (auto intro: ereal_cases)
+termination proof qed (rule wf_empty)
+
+function times_ereal where
+"ereal r * ereal p = ereal (r * p)" |
+"ereal r * \<infinity> = (if r = 0 then 0 else if r > 0 then \<infinity> else -\<infinity>)" |
+"\<infinity> * ereal r = (if r = 0 then 0 else if r > 0 then \<infinity> else -\<infinity>)" |
+"ereal r * -\<infinity> = (if r = 0 then 0 else if r > 0 then -\<infinity> else \<infinity>)" |
+"-\<infinity> * ereal r = (if r = 0 then 0 else if r > 0 then -\<infinity> else \<infinity>)" |
+"(\<infinity>::ereal) * \<infinity> = \<infinity>" |
+"-(\<infinity>::ereal) * \<infinity> = -\<infinity>" |
+"(\<infinity>::ereal) * -\<infinity> = -\<infinity>" |
+"-(\<infinity>::ereal) * -\<infinity> = \<infinity>"
+proof -
+  case (goal1 P x)
+  moreover then obtain a b where "x = (a, b)" by (cases x) auto
+  ultimately show P by (cases rule: ereal2_cases[of a b]) auto
+qed simp_all
+termination by (relation "{}") simp
+
+instance
+proof
+  fix a :: ereal show "1 * a = a"
+    by (cases a) (simp_all add: one_ereal_def)
+  fix b :: ereal show "a * b = b * a"
+    by (cases rule: ereal2_cases[of a b]) simp_all
+  fix c :: ereal show "a * b * c = a * (b * c)"
+    by (cases rule: ereal3_cases[of a b c])
+       (simp_all add: zero_ereal_def zero_less_mult_iff)
+qed
+end
+
+lemma real_of_ereal_le_1:
+  fixes a :: ereal shows "a \<le> 1 \<Longrightarrow> real a \<le> 1"
+  by (cases a) (auto simp: one_ereal_def)
+
+lemma abs_ereal_one[simp]: "\<bar>1\<bar> = (1::ereal)"
+  unfolding one_ereal_def by simp
+
+lemma ereal_mult_zero[simp]:
+  fixes a :: ereal shows "a * 0 = 0"
+  by (cases a) (simp_all add: zero_ereal_def)
+
+lemma ereal_zero_mult[simp]:
+  fixes a :: ereal shows "0 * a = 0"
+  by (cases a) (simp_all add: zero_ereal_def)
+
+lemma ereal_m1_less_0[simp]:
+  "-(1::ereal) < 0"
+  by (simp add: zero_ereal_def one_ereal_def)
+
+lemma ereal_zero_m1[simp]:
+  "1 \<noteq> (0::ereal)"
+  by (simp add: zero_ereal_def one_ereal_def)
+
+lemma ereal_times_0[simp]:
+  fixes x :: ereal shows "0 * x = 0"
+  by (cases x) (auto simp: zero_ereal_def)
+
+lemma ereal_times[simp]:
+  "1 \<noteq> (\<infinity>::ereal)" "(\<infinity>::ereal) \<noteq> 1"
+  "1 \<noteq> -(\<infinity>::ereal)" "-(\<infinity>::ereal) \<noteq> 1"
+  by (auto simp add: times_ereal_def one_ereal_def)
+
+lemma ereal_plus_1[simp]:
+  "1 + ereal r = ereal (r + 1)" "ereal r + 1 = ereal (r + 1)"
+  "1 + -(\<infinity>::ereal) = -\<infinity>" "-(\<infinity>::ereal) + 1 = -\<infinity>"
+  unfolding one_ereal_def by auto
+
+lemma ereal_zero_times[simp]:
+  fixes a b :: ereal shows "a * b = 0 \<longleftrightarrow> a = 0 \<or> b = 0"
+  by (cases rule: ereal2_cases[of a b]) auto
+
+lemma ereal_mult_eq_PInfty[simp]:
+  shows "a * b = (\<infinity>::ereal) \<longleftrightarrow>
+    (a = \<infinity> \<and> b > 0) \<or> (a > 0 \<and> b = \<infinity>) \<or> (a = -\<infinity> \<and> b < 0) \<or> (a < 0 \<and> b = -\<infinity>)"
+  by (cases rule: ereal2_cases[of a b]) auto
+
+lemma ereal_mult_eq_MInfty[simp]:
+  shows "a * b = -(\<infinity>::ereal) \<longleftrightarrow>
+    (a = \<infinity> \<and> b < 0) \<or> (a < 0 \<and> b = \<infinity>) \<or> (a = -\<infinity> \<and> b > 0) \<or> (a > 0 \<and> b = -\<infinity>)"
+  by (cases rule: ereal2_cases[of a b]) auto
+
+lemma ereal_0_less_1[simp]: "0 < (1::ereal)"
+  by (simp_all add: zero_ereal_def one_ereal_def)
+
+lemma ereal_zero_one[simp]: "0 \<noteq> (1::ereal)"
+  by (simp_all add: zero_ereal_def one_ereal_def)
+
+lemma ereal_mult_minus_left[simp]:
+  fixes a b :: ereal shows "-a * b = - (a * b)"
+  by (cases rule: ereal2_cases[of a b]) auto
+
+lemma ereal_mult_minus_right[simp]:
+  fixes a b :: ereal shows "a * -b = - (a * b)"
+  by (cases rule: ereal2_cases[of a b]) auto
+
+lemma ereal_mult_infty[simp]:
+  "a * (\<infinity>::ereal) = (if a = 0 then 0 else if 0 < a then \<infinity> else - \<infinity>)"
+  by (cases a) auto
+
+lemma ereal_infty_mult[simp]:
+  "(\<infinity>::ereal) * a = (if a = 0 then 0 else if 0 < a then \<infinity> else - \<infinity>)"
+  by (cases a) auto
+
+lemma ereal_mult_strict_right_mono:
+  assumes "a < b" and "0 < c" "c < (\<infinity>::ereal)"
+  shows "a * c < b * c"
+  using assms
+  by (cases rule: ereal3_cases[of a b c])
+     (auto simp: zero_le_mult_iff ereal_less_PInfty)
+
+lemma ereal_mult_strict_left_mono:
+  "\<lbrakk> a < b ; 0 < c ; c < (\<infinity>::ereal)\<rbrakk> \<Longrightarrow> c * a < c * b"
+  using ereal_mult_strict_right_mono by (simp add: mult_commute[of c])
+
+lemma ereal_mult_right_mono:
+  fixes a b c :: ereal shows "\<lbrakk>a \<le> b; 0 \<le> c\<rbrakk> \<Longrightarrow> a*c \<le> b*c"
+  using assms
+  apply (cases "c = 0") apply simp
+  by (cases rule: ereal3_cases[of a b c])
+     (auto simp: zero_le_mult_iff ereal_less_PInfty)
+
+lemma ereal_mult_left_mono:
+  fixes a b c :: ereal shows "\<lbrakk>a \<le> b; 0 \<le> c\<rbrakk> \<Longrightarrow> c * a \<le> c * b"
+  using ereal_mult_right_mono by (simp add: mult_commute[of c])
+
+lemma zero_less_one_ereal[simp]: "0 \<le> (1::ereal)"
+  by (simp add: one_ereal_def zero_ereal_def)
+
+lemma ereal_0_le_mult[simp]: "0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> 0 \<le> a * (b :: ereal)"
+  by (cases rule: ereal2_cases[of a b]) (auto simp: mult_nonneg_nonneg)
+
+lemma ereal_right_distrib:
+  fixes r a b :: ereal shows "0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> r * (a + b) = r * a + r * b"
+  by (cases rule: ereal3_cases[of r a b]) (simp_all add: field_simps)
+
+lemma ereal_left_distrib:
+  fixes r a b :: ereal shows "0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> (a + b) * r = a * r + b * r"
+  by (cases rule: ereal3_cases[of r a b]) (simp_all add: field_simps)
+
+lemma ereal_mult_le_0_iff:
+  fixes a b :: ereal
+  shows "a * b \<le> 0 \<longleftrightarrow> (0 \<le> a \<and> b \<le> 0) \<or> (a \<le> 0 \<and> 0 \<le> b)"
+  by (cases rule: ereal2_cases[of a b]) (simp_all add: mult_le_0_iff)
+
+lemma ereal_zero_le_0_iff:
+  fixes a b :: ereal
+  shows "0 \<le> a * b \<longleftrightarrow> (0 \<le> a \<and> 0 \<le> b) \<or> (a \<le> 0 \<and> b \<le> 0)"
+  by (cases rule: ereal2_cases[of a b]) (simp_all add: zero_le_mult_iff)
+
+lemma ereal_mult_less_0_iff:
+  fixes a b :: ereal
+  shows "a * b < 0 \<longleftrightarrow> (0 < a \<and> b < 0) \<or> (a < 0 \<and> 0 < b)"
+  by (cases rule: ereal2_cases[of a b]) (simp_all add: mult_less_0_iff)
+
+lemma ereal_zero_less_0_iff:
+  fixes a b :: ereal
+  shows "0 < a * b \<longleftrightarrow> (0 < a \<and> 0 < b) \<or> (a < 0 \<and> b < 0)"
+  by (cases rule: ereal2_cases[of a b]) (simp_all add: zero_less_mult_iff)
+
+lemma ereal_distrib:
+  fixes a b c :: ereal
+  assumes "a \<noteq> \<infinity> \<or> b \<noteq> -\<infinity>" "a \<noteq> -\<infinity> \<or> b \<noteq> \<infinity>" "\<bar>c\<bar> \<noteq> \<infinity>"
+  shows "(a + b) * c = a * c + b * c"
+  using assms
+  by (cases rule: ereal3_cases[of a b c]) (simp_all add: field_simps)
+
+lemma ereal_le_epsilon:
+  fixes x y :: ereal
+  assumes "ALL e. 0 < e --> x <= y + e"
+  shows "x <= y"
+proof-
+{ assume a: "EX r. y = ereal r"
+  from this obtain r where r_def: "y = ereal r" by auto
+  { assume "x=(-\<infinity>)" hence ?thesis by auto }
+  moreover
+  { assume "~(x=(-\<infinity>))"
+    from this obtain p where p_def: "x = ereal p"
+    using a assms[rule_format, of 1] by (cases x) auto
+    { fix e have "0 < e --> p <= r + e"
+      using assms[rule_format, of "ereal e"] p_def r_def by auto }
+    hence "p <= r" apply (subst field_le_epsilon) by auto
+    hence ?thesis using r_def p_def by auto
+  } ultimately have ?thesis by blast
+}
+moreover
+{ assume "y=(-\<infinity>) | y=\<infinity>" hence ?thesis
+    using assms[rule_format, of 1] by (cases x) auto
+} ultimately show ?thesis by (cases y) auto
+qed
+
+
+lemma ereal_le_epsilon2:
+  fixes x y :: ereal
+  assumes "ALL e. 0 < e --> x <= y + ereal e"
+  shows "x <= y"
+proof-
+{ fix e :: ereal assume "e>0"
+  { assume "e=\<infinity>" hence "x<=y+e" by auto }
+  moreover
+  { assume "e~=\<infinity>"
+    from this obtain r where "e = ereal r" using `e>0` apply (cases e) by auto
+    hence "x<=y+e" using assms[rule_format, of r] `e>0` by auto
+  } ultimately have "x<=y+e" by blast
+} from this show ?thesis using ereal_le_epsilon by auto
+qed
+
+lemma ereal_le_real:
+  fixes x y :: ereal
+  assumes "ALL z. x <= ereal z --> y <= ereal z"
+  shows "y <= x"
+by (metis assms ereal_bot ereal_cases ereal_infty_less_eq ereal_less_eq linorder_le_cases)
+
+lemma ereal_le_ereal:
+  fixes x y :: ereal
+  assumes "\<And>B. B < x \<Longrightarrow> B <= y"
+  shows "x <= y"
+by (metis assms ereal_dense leD linorder_le_less_linear)
+
+lemma ereal_ge_ereal:
+  fixes x y :: ereal
+  assumes "ALL B. B>x --> B >= y"
+  shows "x >= y"
+by (metis assms ereal_dense leD linorder_le_less_linear)
+
+lemma setprod_ereal_0:
+  fixes f :: "'a \<Rightarrow> ereal"
+  shows "(\<Prod>i\<in>A. f i) = 0 \<longleftrightarrow> (finite A \<and> (\<exists>i\<in>A. f i = 0))"
+proof cases
+  assume "finite A"
+  then show ?thesis by (induct A) auto
+qed auto
+
+lemma setprod_ereal_pos:
+  fixes f :: "'a \<Rightarrow> ereal" assumes pos: "\<And>i. i \<in> I \<Longrightarrow> 0 \<le> f i" shows "0 \<le> (\<Prod>i\<in>I. f i)"
+proof cases
+  assume "finite I" from this pos show ?thesis by induct auto
+qed simp
+
+lemma setprod_PInf:
+  fixes f :: "'a \<Rightarrow> ereal"
+  assumes "\<And>i. i \<in> I \<Longrightarrow> 0 \<le> f i"
+  shows "(\<Prod>i\<in>I. f i) = \<infinity> \<longleftrightarrow> finite I \<and> (\<exists>i\<in>I. f i = \<infinity>) \<and> (\<forall>i\<in>I. f i \<noteq> 0)"
+proof cases
+  assume "finite I" from this assms show ?thesis
+  proof (induct I)
+    case (insert i I)
+    then have pos: "0 \<le> f i" "0 \<le> setprod f I" by (auto intro!: setprod_ereal_pos)
+    from insert have "(\<Prod>j\<in>insert i I. f j) = \<infinity> \<longleftrightarrow> setprod f I * f i = \<infinity>" by auto
+    also have "\<dots> \<longleftrightarrow> (setprod f I = \<infinity> \<or> f i = \<infinity>) \<and> f i \<noteq> 0 \<and> setprod f I \<noteq> 0"
+      using setprod_ereal_pos[of I f] pos
+      by (cases rule: ereal2_cases[of "f i" "setprod f I"]) auto
+    also have "\<dots> \<longleftrightarrow> finite (insert i I) \<and> (\<exists>j\<in>insert i I. f j = \<infinity>) \<and> (\<forall>j\<in>insert i I. f j \<noteq> 0)"
+      using insert by (auto simp: setprod_ereal_0)
+    finally show ?case .
+  qed simp
+qed simp
+
+lemma setprod_ereal: "(\<Prod>i\<in>A. ereal (f i)) = ereal (setprod f A)"
+proof cases
+  assume "finite A" then show ?thesis
+    by induct (auto simp: one_ereal_def)
+qed (simp add: one_ereal_def)
+
+subsubsection {* Power *}
+
+instantiation ereal :: power
+begin
+primrec power_ereal where
+  "power_ereal x 0 = 1" |
+  "power_ereal x (Suc n) = x * x ^ n"
+instance ..
+end
+
+lemma ereal_power[simp]: "(ereal x) ^ n = ereal (x^n)"
+  by (induct n) (auto simp: one_ereal_def)
+
+lemma ereal_power_PInf[simp]: "(\<infinity>::ereal) ^ n = (if n = 0 then 1 else \<infinity>)"
+  by (induct n) (auto simp: one_ereal_def)
+
+lemma ereal_power_uminus[simp]:
+  fixes x :: ereal
+  shows "(- x) ^ n = (if even n then x ^ n else - (x^n))"
+  by (induct n) (auto simp: one_ereal_def)
+
+lemma ereal_power_number_of[simp]:
+  "(number_of num :: ereal) ^ n = ereal (number_of num ^ n)"
+  by (induct n) (auto simp: one_ereal_def)
+
+lemma zero_le_power_ereal[simp]:
+  fixes a :: ereal assumes "0 \<le> a"
+  shows "0 \<le> a ^ n"
+  using assms by (induct n) (auto simp: ereal_zero_le_0_iff)
+
+subsubsection {* Subtraction *}
+
+lemma ereal_minus_minus_image[simp]:
+  fixes S :: "ereal set"
+  shows "uminus ` uminus ` S = S"
+  by (auto simp: image_iff)
+
+lemma ereal_uminus_lessThan[simp]:
+  fixes a :: ereal shows "uminus ` {..<a} = {-a<..}"
+proof (safe intro!: image_eqI)
+  fix x assume "-a < x"
+  then have "- x < - (- a)" by (simp del: ereal_uminus_uminus)
+  then show "- x < a" by simp
+qed auto
+
+lemma ereal_uminus_greaterThan[simp]:
+  "uminus ` {(a::ereal)<..} = {..<-a}"
+  by (metis ereal_uminus_lessThan ereal_uminus_uminus
+            ereal_minus_minus_image)
+
+instantiation ereal :: minus
+begin
+definition "x - y = x + -(y::ereal)"
+instance ..
+end
+
+lemma ereal_minus[simp]:
+  "ereal r - ereal p = ereal (r - p)"
+  "-\<infinity> - ereal r = -\<infinity>"
+  "ereal r - \<infinity> = -\<infinity>"
+  "(\<infinity>::ereal) - x = \<infinity>"
+  "-(\<infinity>::ereal) - \<infinity> = -\<infinity>"
+  "x - -y = x + y"
+  "x - 0 = x"
+  "0 - x = -x"
+  by (simp_all add: minus_ereal_def)
+
+lemma ereal_x_minus_x[simp]:
+  "x - x = (if \<bar>x\<bar> = \<infinity> then \<infinity> else 0::ereal)"
+  by (cases x) simp_all
+
+lemma ereal_eq_minus_iff:
+  fixes x y z :: ereal
+  shows "x = z - y \<longleftrightarrow>
+    (\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> x + y = z) \<and>
+    (y = -\<infinity> \<longrightarrow> x = \<infinity>) \<and>
+    (y = \<infinity> \<longrightarrow> z = \<infinity> \<longrightarrow> x = \<infinity>) \<and>
+    (y = \<infinity> \<longrightarrow> z \<noteq> \<infinity> \<longrightarrow> x = -\<infinity>)"
+  by (cases rule: ereal3_cases[of x y z]) auto
+
+lemma ereal_eq_minus:
+  fixes x y z :: ereal
+  shows "\<bar>y\<bar> \<noteq> \<infinity> \<Longrightarrow> x = z - y \<longleftrightarrow> x + y = z"
+  by (auto simp: ereal_eq_minus_iff)
+
+lemma ereal_less_minus_iff:
+  fixes x y z :: ereal
+  shows "x < z - y \<longleftrightarrow>
+    (y = \<infinity> \<longrightarrow> z = \<infinity> \<and> x \<noteq> \<infinity>) \<and>
+    (y = -\<infinity> \<longrightarrow> x \<noteq> \<infinity>) \<and>
+    (\<bar>y\<bar> \<noteq> \<infinity>\<longrightarrow> x + y < z)"
+  by (cases rule: ereal3_cases[of x y z]) auto
+
+lemma ereal_less_minus:
+  fixes x y z :: ereal
+  shows "\<bar>y\<bar> \<noteq> \<infinity> \<Longrightarrow> x < z - y \<longleftrightarrow> x + y < z"
+  by (auto simp: ereal_less_minus_iff)
+
+lemma ereal_le_minus_iff:
+  fixes x y z :: ereal
+  shows "x \<le> z - y \<longleftrightarrow>
+    (y = \<infinity> \<longrightarrow> z \<noteq> \<infinity> \<longrightarrow> x = -\<infinity>) \<and>
+    (\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> x + y \<le> z)"
+  by (cases rule: ereal3_cases[of x y z]) auto
+
+lemma ereal_le_minus:
+  fixes x y z :: ereal
+  shows "\<bar>y\<bar> \<noteq> \<infinity> \<Longrightarrow> x \<le> z - y \<longleftrightarrow> x + y \<le> z"
+  by (auto simp: ereal_le_minus_iff)
+
+lemma ereal_minus_less_iff:
+  fixes x y z :: ereal
+  shows "x - y < z \<longleftrightarrow>
+    y \<noteq> -\<infinity> \<and> (y = \<infinity> \<longrightarrow> x \<noteq> \<infinity> \<and> z \<noteq> -\<infinity>) \<and>
+    (y \<noteq> \<infinity> \<longrightarrow> x < z + y)"
+  by (cases rule: ereal3_cases[of x y z]) auto
+
+lemma ereal_minus_less:
+  fixes x y z :: ereal
+  shows "\<bar>y\<bar> \<noteq> \<infinity> \<Longrightarrow> x - y < z \<longleftrightarrow> x < z + y"
+  by (auto simp: ereal_minus_less_iff)
+
+lemma ereal_minus_le_iff:
+  fixes x y z :: ereal
+  shows "x - y \<le> z \<longleftrightarrow>
+    (y = -\<infinity> \<longrightarrow> z = \<infinity>) \<and>
+    (y = \<infinity> \<longrightarrow> x = \<infinity> \<longrightarrow> z = \<infinity>) \<and>
+    (\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> x \<le> z + y)"
+  by (cases rule: ereal3_cases[of x y z]) auto
+
+lemma ereal_minus_le:
+  fixes x y z :: ereal
+  shows "\<bar>y\<bar> \<noteq> \<infinity> \<Longrightarrow> x - y \<le> z \<longleftrightarrow> x \<le> z + y"
+  by (auto simp: ereal_minus_le_iff)
+
+lemma ereal_minus_eq_minus_iff:
+  fixes a b c :: ereal
+  shows "a - b = a - c \<longleftrightarrow>
+    b = c \<or> a = \<infinity> \<or> (a = -\<infinity> \<and> b \<noteq> -\<infinity> \<and> c \<noteq> -\<infinity>)"
+  by (cases rule: ereal3_cases[of a b c]) auto
+
+lemma ereal_add_le_add_iff:
+  fixes a b c :: ereal
+  shows "c + a \<le> c + b \<longleftrightarrow>
+    a \<le> b \<or> c = \<infinity> \<or> (c = -\<infinity> \<and> a \<noteq> \<infinity> \<and> b \<noteq> \<infinity>)"
+  by (cases rule: ereal3_cases[of a b c]) (simp_all add: field_simps)
+
+lemma ereal_mult_le_mult_iff:
+  fixes a b c :: ereal
+  shows "\<bar>c\<bar> \<noteq> \<infinity> \<Longrightarrow> c * a \<le> c * b \<longleftrightarrow> (0 < c \<longrightarrow> a \<le> b) \<and> (c < 0 \<longrightarrow> b \<le> a)"
+  by (cases rule: ereal3_cases[of a b c]) (simp_all add: mult_le_cancel_left)
+
+lemma ereal_minus_mono:
+  fixes A B C D :: ereal assumes "A \<le> B" "D \<le> C"
+  shows "A - C \<le> B - D"
+  using assms
+  by (cases rule: ereal3_cases[case_product ereal_cases, of A B C D]) simp_all
+
+lemma real_of_ereal_minus:
+  fixes a b :: ereal
+  shows "real (a - b) = (if \<bar>a\<bar> = \<infinity> \<or> \<bar>b\<bar> = \<infinity> then 0 else real a - real b)"
+  by (cases rule: ereal2_cases[of a b]) auto
+
+lemma ereal_diff_positive:
+  fixes a b :: ereal shows "a \<le> b \<Longrightarrow> 0 \<le> b - a"
+  by (cases rule: ereal2_cases[of a b]) auto
+
+lemma ereal_between:
+  fixes x e :: ereal
+  assumes "\<bar>x\<bar> \<noteq> \<infinity>" "0 < e"
+  shows "x - e < x" "x < x + e"
+using assms apply (cases x, cases e) apply auto
+using assms by (cases x, cases e) auto
+
+subsubsection {* Division *}
+
+instantiation ereal :: inverse
+begin
+
+function inverse_ereal where
+"inverse (ereal r) = (if r = 0 then \<infinity> else ereal (inverse r))" |
+"inverse (\<infinity>::ereal) = 0" |
+"inverse (-\<infinity>::ereal) = 0"
+  by (auto intro: ereal_cases)
+termination by (relation "{}") simp
+
+definition "x / y = x * inverse (y :: ereal)"
+
+instance proof qed
+end
+
+lemma real_of_ereal_inverse[simp]:
+  fixes a :: ereal
+  shows "real (inverse a) = 1 / real a"
+  by (cases a) (auto simp: inverse_eq_divide)
+
+lemma ereal_inverse[simp]:
+  "inverse (0::ereal) = \<infinity>"
+  "inverse (1::ereal) = 1"
+  by (simp_all add: one_ereal_def zero_ereal_def)
+
+lemma ereal_divide[simp]:
+  "ereal r / ereal p = (if p = 0 then ereal r * \<infinity> else ereal (r / p))"
+  unfolding divide_ereal_def by (auto simp: divide_real_def)
+
+lemma ereal_divide_same[simp]:
+  fixes x :: ereal shows "x / x = (if \<bar>x\<bar> = \<infinity> \<or> x = 0 then 0 else 1)"
+  by (cases x)
+     (simp_all add: divide_real_def divide_ereal_def one_ereal_def)
+
+lemma ereal_inv_inv[simp]:
+  fixes x :: ereal shows "inverse (inverse x) = (if x \<noteq> -\<infinity> then x else \<infinity>)"
+  by (cases x) auto
+
+lemma ereal_inverse_minus[simp]:
+  fixes x :: ereal shows "inverse (- x) = (if x = 0 then \<infinity> else -inverse x)"
+  by (cases x) simp_all
+
+lemma ereal_uminus_divide[simp]:
+  fixes x y :: ereal shows "- x / y = - (x / y)"
+  unfolding divide_ereal_def by simp
+
+lemma ereal_divide_Infty[simp]:
+  fixes x :: ereal shows "x / \<infinity> = 0" "x / -\<infinity> = 0"
+  unfolding divide_ereal_def by simp_all
+
+lemma ereal_divide_one[simp]:
+  "x / 1 = (x::ereal)"
+  unfolding divide_ereal_def by simp
+
+lemma ereal_divide_ereal[simp]:
+  "\<infinity> / ereal r = (if 0 \<le> r then \<infinity> else -\<infinity>)"
+  unfolding divide_ereal_def by simp
+
+lemma zero_le_divide_ereal[simp]:
+  fixes a :: ereal assumes "0 \<le> a" "0 \<le> b"
+  shows "0 \<le> a / b"
+  using assms by (cases rule: ereal2_cases[of a b]) (auto simp: zero_le_divide_iff)
+
+lemma ereal_le_divide_pos:
+  fixes x y z :: ereal shows "x > 0 \<Longrightarrow> x \<noteq> \<infinity> \<Longrightarrow> y \<le> z / x \<longleftrightarrow> x * y \<le> z"
+  by (cases rule: ereal3_cases[of x y z]) (auto simp: field_simps)
+
+lemma ereal_divide_le_pos:
+  fixes x y z :: ereal shows "x > 0 \<Longrightarrow> x \<noteq> \<infinity> \<Longrightarrow> z / x \<le> y \<longleftrightarrow> z \<le> x * y"
+  by (cases rule: ereal3_cases[of x y z]) (auto simp: field_simps)
+
+lemma ereal_le_divide_neg:
+  fixes x y z :: ereal shows "x < 0 \<Longrightarrow> x \<noteq> -\<infinity> \<Longrightarrow> y \<le> z / x \<longleftrightarrow> z \<le> x * y"
+  by (cases rule: ereal3_cases[of x y z]) (auto simp: field_simps)
+
+lemma ereal_divide_le_neg:
+  fixes x y z :: ereal shows "x < 0 \<Longrightarrow> x \<noteq> -\<infinity> \<Longrightarrow> z / x \<le> y \<longleftrightarrow> x * y \<le> z"
+  by (cases rule: ereal3_cases[of x y z]) (auto simp: field_simps)
+
+lemma ereal_inverse_antimono_strict:
+  fixes x y :: ereal
+  shows "0 \<le> x \<Longrightarrow> x < y \<Longrightarrow> inverse y < inverse x"
+  by (cases rule: ereal2_cases[of x y]) auto
+
+lemma ereal_inverse_antimono:
+  fixes x y :: ereal
+  shows "0 \<le> x \<Longrightarrow> x <= y \<Longrightarrow> inverse y <= inverse x"
+  by (cases rule: ereal2_cases[of x y]) auto
+
+lemma inverse_inverse_Pinfty_iff[simp]:
+  fixes x :: ereal shows "inverse x = \<infinity> \<longleftrightarrow> x = 0"
+  by (cases x) auto
+
+lemma ereal_inverse_eq_0:
+  fixes x :: ereal shows "inverse x = 0 \<longleftrightarrow> x = \<infinity> \<or> x = -\<infinity>"
+  by (cases x) auto
+
+lemma ereal_0_gt_inverse:
+  fixes x :: ereal shows "0 < inverse x \<longleftrightarrow> x \<noteq> \<infinity> \<and> 0 \<le> x"
+  by (cases x) auto
+
+lemma ereal_mult_less_right:
+  fixes a b c :: ereal
+  assumes "b * a < c * a" "0 < a" "a < \<infinity>"
+  shows "b < c"
+  using assms
+  by (cases rule: ereal3_cases[of a b c])
+     (auto split: split_if_asm simp: zero_less_mult_iff zero_le_mult_iff)
+
+lemma ereal_power_divide:
+  fixes x y :: ereal shows "y \<noteq> 0 \<Longrightarrow> (x / y) ^ n = x^n / y^n"
+  by (cases rule: ereal2_cases[of x y])
+     (auto simp: one_ereal_def zero_ereal_def power_divide not_le
+                 power_less_zero_eq zero_le_power_iff)
+
+lemma ereal_le_mult_one_interval:
+  fixes x y :: ereal
+  assumes y: "y \<noteq> -\<infinity>"
+  assumes z: "\<And>z. \<lbrakk> 0 < z ; z < 1 \<rbrakk> \<Longrightarrow> z * x \<le> y"
+  shows "x \<le> y"
+proof (cases x)
+  case PInf with z[of "1 / 2"] show "x \<le> y" by (simp add: one_ereal_def)
+next
+  case (real r) note r = this
+  show "x \<le> y"
+  proof (cases y)
+    case (real p) note p = this
+    have "r \<le> p"
+    proof (rule field_le_mult_one_interval)
+      fix z :: real assume "0 < z" and "z < 1"
+      with z[of "ereal z"]
+      show "z * r \<le> p" using p r by (auto simp: zero_le_mult_iff one_ereal_def)
+    qed
+    then show "x \<le> y" using p r by simp
+  qed (insert y, simp_all)
+qed simp
+
+subsection "Complete lattice"
+
+instantiation ereal :: lattice
+begin
+definition [simp]: "sup x y = (max x y :: ereal)"
+definition [simp]: "inf x y = (min x y :: ereal)"
+instance proof qed simp_all
+end
+
+instantiation ereal :: complete_lattice
+begin
+
+definition "bot = (-\<infinity>::ereal)"
+definition "top = (\<infinity>::ereal)"
+
+definition "Sup S = (LEAST z. \<forall>x\<in>S. x \<le> z :: ereal)"
+definition "Inf S = (GREATEST z. \<forall>x\<in>S. z \<le> x :: ereal)"
+
+lemma ereal_complete_Sup:
+  fixes S :: "ereal set" assumes "S \<noteq> {}"
+  shows "\<exists>x. (\<forall>y\<in>S. y \<le> x) \<and> (\<forall>z. (\<forall>y\<in>S. y \<le> z) \<longrightarrow> x \<le> z)"
+proof cases
+  assume "\<exists>x. \<forall>a\<in>S. a \<le> ereal x"
+  then obtain y where y: "\<And>a. a\<in>S \<Longrightarrow> a \<le> ereal y" by auto
+  then have "\<infinity> \<notin> S" by force
+  show ?thesis
+  proof cases
+    assume "S = {-\<infinity>}"
+    then show ?thesis by (auto intro!: exI[of _ "-\<infinity>"])
+  next
+    assume "S \<noteq> {-\<infinity>}"
+    with `S \<noteq> {}` `\<infinity> \<notin> S` obtain x where "x \<in> S - {-\<infinity>}" "x \<noteq> \<infinity>" by auto
+    with y `\<infinity> \<notin> S` have "\<forall>z\<in>real ` (S - {-\<infinity>}). z \<le> y"
+      by (auto simp: real_of_ereal_ord_simps)
+    with reals_complete2[of "real ` (S - {-\<infinity>})"] `x \<in> S - {-\<infinity>}`
+    obtain s where s:
+       "\<forall>y\<in>S - {-\<infinity>}. real y \<le> s" "\<And>z. (\<forall>y\<in>S - {-\<infinity>}. real y \<le> z) \<Longrightarrow> s \<le> z"
+       by auto
+    show ?thesis
+    proof (safe intro!: exI[of _ "ereal s"])
+      fix z assume "z \<in> S" with `\<infinity> \<notin> S` show "z \<le> ereal s"
+      proof (cases z)
+        case (real r)
+        then show ?thesis
+          using s(1)[rule_format, of z] `z \<in> S` `z = ereal r` by auto
+      qed auto
+    next
+      fix z assume *: "\<forall>y\<in>S. y \<le> z"
+      with `S \<noteq> {-\<infinity>}` `S \<noteq> {}` show "ereal s \<le> z"
+      proof (cases z)
+        case (real u)
+        with * have "s \<le> u"
+          by (intro s(2)[of u]) (auto simp: real_of_ereal_ord_simps)
+        then show ?thesis using real by simp
+      qed auto
+    qed
+  qed
+next
+  assume *: "\<not> (\<exists>x. \<forall>a\<in>S. a \<le> ereal x)"
+  show ?thesis
+  proof (safe intro!: exI[of _ \<infinity>])
+    fix y assume **: "\<forall>z\<in>S. z \<le> y"
+    with * show "\<infinity> \<le> y"
+    proof (cases y)
+      case MInf with * ** show ?thesis by (force simp: not_le)
+    qed auto
+  qed simp
+qed
+
+lemma ereal_complete_Inf:
+  fixes S :: "ereal set" assumes "S ~= {}"
+  shows "EX x. (ALL y:S. x <= y) & (ALL z. (ALL y:S. z <= y) --> z <= x)"
+proof-
+def S1 == "uminus ` S"
+hence "S1 ~= {}" using assms by auto
+from this obtain x where x_def: "(ALL y:S1. y <= x) & (ALL z. (ALL y:S1. y <= z) --> x <= z)"
+   using ereal_complete_Sup[of S1] by auto
+{ fix z assume "ALL y:S. z <= y"
+  hence "ALL y:S1. y <= -z" unfolding S1_def by auto
+  hence "x <= -z" using x_def by auto
+  hence "z <= -x"
+    apply (subst ereal_uminus_uminus[symmetric])
+    unfolding ereal_minus_le_minus . }
+moreover have "(ALL y:S. -x <= y)"
+   using x_def unfolding S1_def
+   apply simp
+   apply (subst (3) ereal_uminus_uminus[symmetric])
+   unfolding ereal_minus_le_minus by simp
+ultimately show ?thesis by auto
+qed
+
+lemma ereal_complete_uminus_eq:
+  fixes S :: "ereal set"
+  shows "(\<forall>y\<in>uminus`S. y \<le> x) \<and> (\<forall>z. (\<forall>y\<in>uminus`S. y \<le> z) \<longrightarrow> x \<le> z)
+     \<longleftrightarrow> (\<forall>y\<in>S. -x \<le> y) \<and> (\<forall>z. (\<forall>y\<in>S. z \<le> y) \<longrightarrow> z \<le> -x)"
+  by simp (metis ereal_minus_le_minus ereal_uminus_uminus)
+
+lemma ereal_Sup_uminus_image_eq:
+  fixes S :: "ereal set"
+  shows "Sup (uminus ` S) = - Inf S"
+proof cases
+  assume "S = {}"
+  moreover have "(THE x. All (op \<le> x)) = (-\<infinity>::ereal)"
+    by (rule the_equality) (auto intro!: ereal_bot)
+  moreover have "(SOME x. \<forall>y. y \<le> x) = (\<infinity>::ereal)"
+    by (rule some_equality) (auto intro!: ereal_top)
+  ultimately show ?thesis unfolding Inf_ereal_def Sup_ereal_def
+    Least_def Greatest_def GreatestM_def by simp
+next
+  assume "S \<noteq> {}"
+  with ereal_complete_Sup[of "uminus`S"]
+  obtain x where x: "(\<forall>y\<in>S. -x \<le> y) \<and> (\<forall>z. (\<forall>y\<in>S. z \<le> y) \<longrightarrow> z \<le> -x)"
+    unfolding ereal_complete_uminus_eq by auto
+  show "Sup (uminus ` S) = - Inf S"
+    unfolding Inf_ereal_def Greatest_def GreatestM_def
+  proof (intro someI2[of _ _ "\<lambda>x. Sup (uminus`S) = - x"])
+    show "(\<forall>y\<in>S. -x \<le> y) \<and> (\<forall>y. (\<forall>z\<in>S. y \<le> z) \<longrightarrow> y \<le> -x)"
+      using x .
+    fix x' assume "(\<forall>y\<in>S. x' \<le> y) \<and> (\<forall>y. (\<forall>z\<in>S. y \<le> z) \<longrightarrow> y \<le> x')"
+    then have "(\<forall>y\<in>uminus`S. y \<le> - x') \<and> (\<forall>y. (\<forall>z\<in>uminus`S. z \<le> y) \<longrightarrow> - x' \<le> y)"
+      unfolding ereal_complete_uminus_eq by simp
+    then show "Sup (uminus ` S) = -x'"
+      unfolding Sup_ereal_def ereal_uminus_eq_iff
+      by (intro Least_equality) auto
+  qed
+qed
+
+instance
+proof
+  { fix x :: ereal and A
+    show "bot <= x" by (cases x) (simp_all add: bot_ereal_def)
+    show "x <= top" by (simp add: top_ereal_def) }
+
+  { fix x :: ereal and A assume "x : A"
+    with ereal_complete_Sup[of A]
+    obtain s where s: "\<forall>y\<in>A. y <= s" "\<forall>z. (\<forall>y\<in>A. y <= z) \<longrightarrow> s <= z" by auto
+    hence "x <= s" using `x : A` by auto
+    also have "... = Sup A" using s unfolding Sup_ereal_def
+      by (auto intro!: Least_equality[symmetric])
+    finally show "x <= Sup A" . }
+  note le_Sup = this
+
+  { fix x :: ereal and A assume *: "!!z. (z : A ==> z <= x)"
+    show "Sup A <= x"
+    proof (cases "A = {}")
+      case True
+      hence "Sup A = -\<infinity>" unfolding Sup_ereal_def
+        by (auto intro!: Least_equality)
+      thus "Sup A <= x" by simp
+    next
+      case False
+      with ereal_complete_Sup[of A]
+      obtain s where s: "\<forall>y\<in>A. y <= s" "\<forall>z. (\<forall>y\<in>A. y <= z) \<longrightarrow> s <= z" by auto
+      hence "Sup A = s"
+        unfolding Sup_ereal_def by (auto intro!: Least_equality)
+      also have "s <= x" using * s by auto
+      finally show "Sup A <= x" .
+    qed }
+  note Sup_le = this
+
+  { fix x :: ereal and A assume "x \<in> A"
+    with le_Sup[of "-x" "uminus`A"] show "Inf A \<le> x"
+      unfolding ereal_Sup_uminus_image_eq by simp }
+
+  { fix x :: ereal and A assume *: "!!z. (z : A ==> x <= z)"
+    with Sup_le[of "uminus`A" "-x"] show "x \<le> Inf A"
+      unfolding ereal_Sup_uminus_image_eq by force }
+qed
+end
+
+lemma ereal_SUPR_uminus:
+  fixes f :: "'a => ereal"
+  shows "(SUP i : R. -(f i)) = -(INF i : R. f i)"
+  unfolding SUPR_def INFI_def
+  using ereal_Sup_uminus_image_eq[of "f`R"]
+  by (simp add: image_image)
+
+lemma ereal_INFI_uminus:
+  fixes f :: "'a => ereal"
+  shows "(INF i : R. -(f i)) = -(SUP i : R. f i)"
+  using ereal_SUPR_uminus[of _ "\<lambda>x. - f x"] by simp
+
+lemma ereal_Inf_uminus_image_eq: "Inf (uminus ` S) = - Sup (S::ereal set)"
+  using ereal_Sup_uminus_image_eq[of "uminus ` S"] by (simp add: image_image)
+
+lemma ereal_inj_on_uminus[intro, simp]: "inj_on uminus (A :: ereal set)"
+  by (auto intro!: inj_onI)
+
+lemma ereal_image_uminus_shift:
+  fixes X Y :: "ereal set" shows "uminus ` X = Y \<longleftrightarrow> X = uminus ` Y"
+proof
+  assume "uminus ` X = Y"
+  then have "uminus ` uminus ` X = uminus ` Y"
+    by (simp add: inj_image_eq_iff)
+  then show "X = uminus ` Y" by (simp add: image_image)
+qed (simp add: image_image)
+
+lemma Inf_ereal_iff:
+  fixes z :: ereal
+  shows "(!!x. x:X ==> z <= x) ==> (EX x:X. x<y) <-> Inf X < y"
+  by (metis complete_lattice_class.Inf_greatest complete_lattice_class.Inf_lower less_le_not_le linear
+            order_less_le_trans)
+
+lemma Sup_eq_MInfty:
+  fixes S :: "ereal set" shows "Sup S = -\<infinity> \<longleftrightarrow> S = {} \<or> S = {-\<infinity>}"
+proof
+  assume a: "Sup S = -\<infinity>"
+  with complete_lattice_class.Sup_upper[of _ S]
+  show "S={} \<or> S={-\<infinity>}" by auto
+next
+  assume "S={} \<or> S={-\<infinity>}" then show "Sup S = -\<infinity>"
+    unfolding Sup_ereal_def by (auto intro!: Least_equality)
+qed
+
+lemma Inf_eq_PInfty:
+  fixes S :: "ereal set" shows "Inf S = \<infinity> \<longleftrightarrow> S = {} \<or> S = {\<infinity>}"
+  using Sup_eq_MInfty[of "uminus`S"]
+  unfolding ereal_Sup_uminus_image_eq ereal_image_uminus_shift by simp
+
+lemma Inf_eq_MInfty: 
+  fixes S :: "ereal set" shows "-\<infinity> \<in> S \<Longrightarrow> Inf S = -\<infinity>"
+  unfolding Inf_ereal_def
+  by (auto intro!: Greatest_equality)
+
+lemma Sup_eq_PInfty:
+  fixes S :: "ereal set" shows "\<infinity> \<in> S \<Longrightarrow> Sup S = \<infinity>"
+  unfolding Sup_ereal_def
+  by (auto intro!: Least_equality)
+
+lemma ereal_SUPI:
+  fixes x :: ereal
+  assumes "!!i. i : A ==> f i <= x"
+  assumes "!!y. (!!i. i : A ==> f i <= y) ==> x <= y"
+  shows "(SUP i:A. f i) = x"
+  unfolding SUPR_def Sup_ereal_def
+  using assms by (auto intro!: Least_equality)
+
+lemma ereal_INFI:
+  fixes x :: ereal
+  assumes "!!i. i : A ==> f i >= x"
+  assumes "!!y. (!!i. i : A ==> f i >= y) ==> x >= y"
+  shows "(INF i:A. f i) = x"
+  unfolding INFI_def Inf_ereal_def
+  using assms by (auto intro!: Greatest_equality)
+
+lemma Sup_ereal_close:
+  fixes e :: ereal
+  assumes "0 < e" and S: "\<bar>Sup S\<bar> \<noteq> \<infinity>" "S \<noteq> {}"
+  shows "\<exists>x\<in>S. Sup S - e < x"
+  using assms by (cases e) (auto intro!: less_Sup_iff[THEN iffD1])
+
+lemma Inf_ereal_close:
+  fixes e :: ereal assumes "\<bar>Inf X\<bar> \<noteq> \<infinity>" "0 < e"
+  shows "\<exists>x\<in>X. x < Inf X + e"
+proof (rule Inf_less_iff[THEN iffD1])
+  show "Inf X < Inf X + e" using assms
+    by (cases e) auto
+qed
+
+lemma Sup_eq_top_iff:
+  fixes A :: "'a::{complete_lattice, linorder} set"
+  shows "Sup A = top \<longleftrightarrow> (\<forall>x<top. \<exists>i\<in>A. x < i)"
+proof
+  assume *: "Sup A = top"
+  show "(\<forall>x<top. \<exists>i\<in>A. x < i)" unfolding *[symmetric]
+  proof (intro allI impI)
+    fix x assume "x < Sup A" then show "\<exists>i\<in>A. x < i"
+      unfolding less_Sup_iff by auto
+  qed
+next
+  assume *: "\<forall>x<top. \<exists>i\<in>A. x < i"
+  show "Sup A = top"
+  proof (rule ccontr)
+    assume "Sup A \<noteq> top"
+    with top_greatest[of "Sup A"]
+    have "Sup A < top" unfolding le_less by auto
+    then have "Sup A < Sup A"
+      using * unfolding less_Sup_iff by auto
+    then show False by auto
+  qed
+qed
+
+lemma SUP_eq_top_iff:
+  fixes f :: "'a \<Rightarrow> 'b::{complete_lattice, linorder}"
+  shows "(SUP i:A. f i) = top \<longleftrightarrow> (\<forall>x<top. \<exists>i\<in>A. x < f i)"
+  unfolding SUPR_def Sup_eq_top_iff by auto
+
+lemma SUP_nat_Infty: "(SUP i::nat. ereal (real i)) = \<infinity>"
+proof -
+  { fix x ::ereal assume "x \<noteq> \<infinity>"
+    then have "\<exists>k::nat. x < ereal (real k)"
+    proof (cases x)
+      case MInf then show ?thesis by (intro exI[of _ 0]) auto
+    next
+      case (real r)
+      moreover obtain k :: nat where "r < real k"
+        using ex_less_of_nat by (auto simp: real_eq_of_nat)
+      ultimately show ?thesis by auto
+    qed simp }
+  then show ?thesis
+    using SUP_eq_top_iff[of UNIV "\<lambda>n::nat. ereal (real n)"]
+    by (auto simp: top_ereal_def)
+qed
+
+lemma ereal_le_Sup:
+  fixes x :: ereal
+  shows "(x <= (SUP i:A. f i)) <-> (ALL y. y < x --> (EX i. i : A & y <= f i))"
+(is "?lhs <-> ?rhs")
+proof-
+{ assume "?rhs"
+  { assume "~(x <= (SUP i:A. f i))" hence "(SUP i:A. f i)<x" by (simp add: not_le)
+    from this obtain y where y_def: "(SUP i:A. f i)<y & y<x" using ereal_dense by auto
+    from this obtain i where "i : A & y <= f i" using `?rhs` by auto
+    hence "y <= (SUP i:A. f i)" using le_SUPI[of i A f] by auto
+    hence False using y_def by auto
+  } hence "?lhs" by auto
+}
+moreover
+{ assume "?lhs" hence "?rhs"
+  by (metis Collect_def Collect_mem_eq SUP_leI assms atLeastatMost_empty atLeastatMost_empty_iff
+      inf_sup_ord(4) linorder_le_cases sup_absorb1 xt1(8))
+} ultimately show ?thesis by auto
+qed
+
+lemma ereal_Inf_le:
+  fixes x :: ereal
+  shows "((INF i:A. f i) <= x) <-> (ALL y. x < y --> (EX i. i : A & f i <= y))"
+(is "?lhs <-> ?rhs")
+proof-
+{ assume "?rhs"
+  { assume "~((INF i:A. f i) <= x)" hence "x < (INF i:A. f i)" by (simp add: not_le)
+    from this obtain y where y_def: "x<y & y<(INF i:A. f i)" using ereal_dense by auto
+    from this obtain i where "i : A & f i <= y" using `?rhs` by auto
+    hence "(INF i:A. f i) <= y" using INF_leI[of i A f] by auto
+    hence False using y_def by auto
+  } hence "?lhs" by auto
+}
+moreover
+{ assume "?lhs" hence "?rhs"
+  by (metis Collect_def Collect_mem_eq le_INFI assms atLeastatMost_empty atLeastatMost_empty_iff
+      inf_sup_ord(4) linorder_le_cases sup_absorb1 xt1(8))
+} ultimately show ?thesis by auto
+qed
+
+lemma Inf_less:
+  fixes x :: ereal
+  assumes "(INF i:A. f i) < x"
+  shows "EX i. i : A & f i <= x"
+proof(rule ccontr)
+  assume "~ (EX i. i : A & f i <= x)"
+  hence "ALL i:A. f i > x" by auto
+  hence "(INF i:A. f i) >= x" apply (subst le_INFI) by auto
+  thus False using assms by auto
+qed
+
+lemma same_INF:
+  assumes "ALL e:A. f e = g e"
+  shows "(INF e:A. f e) = (INF e:A. g e)"
+proof-
+have "f ` A = g ` A" unfolding image_def using assms by auto
+thus ?thesis unfolding INFI_def by auto
+qed
+
+lemma same_SUP:
+  assumes "ALL e:A. f e = g e"
+  shows "(SUP e:A. f e) = (SUP e:A. g e)"
+proof-
+have "f ` A = g ` A" unfolding image_def using assms by auto
+thus ?thesis unfolding SUPR_def by auto
+qed
+
+lemma SUPR_eq:
+  assumes "\<forall>i\<in>A. \<exists>j\<in>B. f i \<le> g j"
+  assumes "\<forall>j\<in>B. \<exists>i\<in>A. g j \<le> f i"
+  shows "(SUP i:A. f i) = (SUP j:B. g j)"
+proof (intro antisym)
+  show "(SUP i:A. f i) \<le> (SUP j:B. g j)"
+    using assms by (metis SUP_leI le_SUPI2)
+  show "(SUP i:B. g i) \<le> (SUP j:A. f j)"
+    using assms by (metis SUP_leI le_SUPI2)
+qed
+
+lemma SUP_ereal_le_addI:
+  fixes f :: "'i \<Rightarrow> ereal"
+  assumes "\<And>i. f i + y \<le> z" and "y \<noteq> -\<infinity>"
+  shows "SUPR UNIV f + y \<le> z"
+proof (cases y)
+  case (real r)
+  then have "\<And>i. f i \<le> z - y" using assms by (simp add: ereal_le_minus_iff)
+  then have "SUPR UNIV f \<le> z - y" by (rule SUP_leI)
+  then show ?thesis using real by (simp add: ereal_le_minus_iff)
+qed (insert assms, auto)
+
+lemma SUPR_ereal_add:
+  fixes f g :: "nat \<Rightarrow> ereal"
+  assumes "incseq f" "incseq g" and pos: "\<And>i. f i \<noteq> -\<infinity>" "\<And>i. g i \<noteq> -\<infinity>"
+  shows "(SUP i. f i + g i) = SUPR UNIV f + SUPR UNIV g"
+proof (rule ereal_SUPI)
+  fix y assume *: "\<And>i. i \<in> UNIV \<Longrightarrow> f i + g i \<le> y"
+  have f: "SUPR UNIV f \<noteq> -\<infinity>" using pos
+    unfolding SUPR_def Sup_eq_MInfty by (auto dest: image_eqD)
+  { fix j
+    { fix i
+      have "f i + g j \<le> f i + g (max i j)"
+        using `incseq g`[THEN incseqD] by (rule add_left_mono) auto
+      also have "\<dots> \<le> f (max i j) + g (max i j)"
+        using `incseq f`[THEN incseqD] by (rule add_right_mono) auto
+      also have "\<dots> \<le> y" using * by auto
+      finally have "f i + g j \<le> y" . }
+    then have "SUPR UNIV f + g j \<le> y"
+      using assms(4)[of j] by (intro SUP_ereal_le_addI) auto
+    then have "g j + SUPR UNIV f \<le> y" by (simp add: ac_simps) }
+  then have "SUPR UNIV g + SUPR UNIV f \<le> y"
+    using f by (rule SUP_ereal_le_addI)
+  then show "SUPR UNIV f + SUPR UNIV g \<le> y" by (simp add: ac_simps)
+qed (auto intro!: add_mono le_SUPI)
+
+lemma SUPR_ereal_add_pos:
+  fixes f g :: "nat \<Rightarrow> ereal"
+  assumes inc: "incseq f" "incseq g" and pos: "\<And>i. 0 \<le> f i" "\<And>i. 0 \<le> g i"
+  shows "(SUP i. f i + g i) = SUPR UNIV f + SUPR UNIV g"
+proof (intro SUPR_ereal_add inc)
+  fix i show "f i \<noteq> -\<infinity>" "g i \<noteq> -\<infinity>" using pos[of i] by auto
+qed
+
+lemma SUPR_ereal_setsum:
+  fixes f g :: "'a \<Rightarrow> nat \<Rightarrow> ereal"
+  assumes "\<And>n. n \<in> A \<Longrightarrow> incseq (f n)" and pos: "\<And>n i. n \<in> A \<Longrightarrow> 0 \<le> f n i"
+  shows "(SUP i. \<Sum>n\<in>A. f n i) = (\<Sum>n\<in>A. SUPR UNIV (f n))"
+proof cases
+  assume "finite A" then show ?thesis using assms
+    by induct (auto simp: incseq_setsumI2 setsum_nonneg SUPR_ereal_add_pos)
+qed simp
+
+lemma SUPR_ereal_cmult:
+  fixes f :: "nat \<Rightarrow> ereal" assumes "\<And>i. 0 \<le> f i" "0 \<le> c"
+  shows "(SUP i. c * f i) = c * SUPR UNIV f"
+proof (rule ereal_SUPI)
+  fix i have "f i \<le> SUPR UNIV f" by (rule le_SUPI) auto
+  then show "c * f i \<le> c * SUPR UNIV f"
+    using `0 \<le> c` by (rule ereal_mult_left_mono)
+next
+  fix y assume *: "\<And>i. i \<in> UNIV \<Longrightarrow> c * f i \<le> y"
+  show "c * SUPR UNIV f \<le> y"
+  proof cases
+    assume c: "0 < c \<and> c \<noteq> \<infinity>"
+    with * have "SUPR UNIV f \<le> y / c"
+      by (intro SUP_leI) (auto simp: ereal_le_divide_pos)
+    with c show ?thesis
+      by (auto simp: ereal_le_divide_pos)
+  next
+    { assume "c = \<infinity>" have ?thesis
+      proof cases
+        assume "\<forall>i. f i = 0"
+        moreover then have "range f = {0}" by auto
+        ultimately show "c * SUPR UNIV f \<le> y" using * by (auto simp: SUPR_def)
+      next
+        assume "\<not> (\<forall>i. f i = 0)"
+        then obtain i where "f i \<noteq> 0" by auto
+        with *[of i] `c = \<infinity>` `0 \<le> f i` show ?thesis by (auto split: split_if_asm)
+      qed }
+    moreover assume "\<not> (0 < c \<and> c \<noteq> \<infinity>)"
+    ultimately show ?thesis using * `0 \<le> c` by auto
+  qed
+qed
+
+lemma SUP_PInfty:
+  fixes f :: "'a \<Rightarrow> ereal"
+  assumes "\<And>n::nat. \<exists>i\<in>A. ereal (real n) \<le> f i"
+  shows "(SUP i:A. f i) = \<infinity>"
+  unfolding SUPR_def Sup_eq_top_iff[where 'a=ereal, unfolded top_ereal_def]
+  apply simp
+proof safe
+  fix x :: ereal assume "x \<noteq> \<infinity>"
+  show "\<exists>i\<in>A. x < f i"
+  proof (cases x)
+    case PInf with `x \<noteq> \<infinity>` show ?thesis by simp
+  next
+    case MInf with assms[of "0"] show ?thesis by force
+  next
+    case (real r)
+    with less_PInf_Ex_of_nat[of x] obtain n :: nat where "x < ereal (real n)" by auto
+    moreover from assms[of n] guess i ..
+    ultimately show ?thesis
+      by (auto intro!: bexI[of _ i])
+  qed
+qed
+
+lemma Sup_countable_SUPR:
+  assumes "A \<noteq> {}"
+  shows "\<exists>f::nat \<Rightarrow> ereal. range f \<subseteq> A \<and> Sup A = SUPR UNIV f"
+proof (cases "Sup A")
+  case (real r)
+  have "\<forall>n::nat. \<exists>x. x \<in> A \<and> Sup A < x + 1 / ereal (real n)"
+  proof
+    fix n ::nat have "\<exists>x\<in>A. Sup A - 1 / ereal (real n) < x"
+      using assms real by (intro Sup_ereal_close) (auto simp: one_ereal_def)
+    then guess x ..
+    then show "\<exists>x. x \<in> A \<and> Sup A < x + 1 / ereal (real n)"
+      by (auto intro!: exI[of _ x] simp: ereal_minus_less_iff)
+  qed
+  from choice[OF this] guess f .. note f = this
+  have "SUPR UNIV f = Sup A"
+  proof (rule ereal_SUPI)
+    fix i show "f i \<le> Sup A" using f
+      by (auto intro!: complete_lattice_class.Sup_upper)
+  next
+    fix y assume bound: "\<And>i. i \<in> UNIV \<Longrightarrow> f i \<le> y"
+    show "Sup A \<le> y"
+    proof (rule ereal_le_epsilon, intro allI impI)
+      fix e :: ereal assume "0 < e"
+      show "Sup A \<le> y + e"
+      proof (cases e)
+        case (real r)
+        hence "0 < r" using `0 < e` by auto
+        then obtain n ::nat where *: "1 / real n < r" "0 < n"
+          using ex_inverse_of_nat_less by (auto simp: real_eq_of_nat inverse_eq_divide)
+        have "Sup A \<le> f n + 1 / ereal (real n)" using f[THEN spec, of n] by auto
+        also have "1 / ereal (real n) \<le> e" using real * by (auto simp: one_ereal_def )
+        with bound have "f n + 1 / ereal (real n) \<le> y + e" by (rule add_mono) simp
+        finally show "Sup A \<le> y + e" .
+      qed (insert `0 < e`, auto)
+    qed
+  qed
+  with f show ?thesis by (auto intro!: exI[of _ f])
+next
+  case PInf
+  from `A \<noteq> {}` obtain x where "x \<in> A" by auto
+  show ?thesis
+  proof cases
+    assume "\<infinity> \<in> A"
+    moreover then have "\<infinity> \<le> Sup A" by (intro complete_lattice_class.Sup_upper)
+    ultimately show ?thesis by (auto intro!: exI[of _ "\<lambda>x. \<infinity>"])
+  next
+    assume "\<infinity> \<notin> A"
+    have "\<exists>x\<in>A. 0 \<le> x"
+      by (metis Infty_neq_0 PInf complete_lattice_class.Sup_least ereal_infty_less_eq2 linorder_linear)
+    then obtain x where "x \<in> A" "0 \<le> x" by auto
+    have "\<forall>n::nat. \<exists>f. f \<in> A \<and> x + ereal (real n) \<le> f"
+    proof (rule ccontr)
+      assume "\<not> ?thesis"
+      then have "\<exists>n::nat. Sup A \<le> x + ereal (real n)"
+        by (simp add: Sup_le_iff not_le less_imp_le Ball_def) (metis less_imp_le)
+      then show False using `x \<in> A` `\<infinity> \<notin> A` PInf
+        by(cases x) auto
+    qed
+    from choice[OF this] guess f .. note f = this
+    have "SUPR UNIV f = \<infinity>"
+    proof (rule SUP_PInfty)
+      fix n :: nat show "\<exists>i\<in>UNIV. ereal (real n) \<le> f i"
+        using f[THEN spec, of n] `0 \<le> x`
+        by (cases rule: ereal2_cases[of "f n" x]) (auto intro!: exI[of _ n])
+    qed
+    then show ?thesis using f PInf by (auto intro!: exI[of _ f])
+  qed
+next
+  case MInf
+  with `A \<noteq> {}` have "A = {-\<infinity>}" by (auto simp: Sup_eq_MInfty)
+  then show ?thesis using MInf by (auto intro!: exI[of _ "\<lambda>x. -\<infinity>"])
+qed
+
+lemma SUPR_countable_SUPR:
+  "A \<noteq> {} \<Longrightarrow> \<exists>f::nat \<Rightarrow> ereal. range f \<subseteq> g`A \<and> SUPR A g = SUPR UNIV f"
+  using Sup_countable_SUPR[of "g`A"] by (auto simp: SUPR_def)
+
+lemma Sup_ereal_cadd:
+  fixes A :: "ereal set" assumes "A \<noteq> {}" and "a \<noteq> -\<infinity>"
+  shows "Sup ((\<lambda>x. a + x) ` A) = a + Sup A"
+proof (rule antisym)
+  have *: "\<And>a::ereal. \<And>A. Sup ((\<lambda>x. a + x) ` A) \<le> a + Sup A"
+    by (auto intro!: add_mono complete_lattice_class.Sup_least complete_lattice_class.Sup_upper)
+  then show "Sup ((\<lambda>x. a + x) ` A) \<le> a + Sup A" .
+  show "a + Sup A \<le> Sup ((\<lambda>x. a + x) ` A)"
+  proof (cases a)
+    case PInf with `A \<noteq> {}` show ?thesis by (auto simp: image_constant)
+  next
+    case (real r)
+    then have **: "op + (- a) ` op + a ` A = A"
+      by (auto simp: image_iff ac_simps zero_ereal_def[symmetric])
+    from *[of "-a" "(\<lambda>x. a + x) ` A"] real show ?thesis unfolding **
+      by (cases rule: ereal2_cases[of "Sup A" "Sup (op + a ` A)"]) auto
+  qed (insert `a \<noteq> -\<infinity>`, auto)
+qed
+
+lemma Sup_ereal_cminus:
+  fixes A :: "ereal set" assumes "A \<noteq> {}" and "a \<noteq> -\<infinity>"
+  shows "Sup ((\<lambda>x. a - x) ` A) = a - Inf A"
+  using Sup_ereal_cadd[of "uminus ` A" a] assms
+  by (simp add: comp_def image_image minus_ereal_def
+                 ereal_Sup_uminus_image_eq)
+
+lemma SUPR_ereal_cminus:
+  fixes f :: "'i \<Rightarrow> ereal"
+  fixes A assumes "A \<noteq> {}" and "a \<noteq> -\<infinity>"
+  shows "(SUP x:A. a - f x) = a - (INF x:A. f x)"
+  using Sup_ereal_cminus[of "f`A" a] assms
+  unfolding SUPR_def INFI_def image_image by auto
+
+lemma Inf_ereal_cminus:
+  fixes A :: "ereal set" assumes "A \<noteq> {}" and "\<bar>a\<bar> \<noteq> \<infinity>"
+  shows "Inf ((\<lambda>x. a - x) ` A) = a - Sup A"
+proof -
+  { fix x have "-a - -x = -(a - x)" using assms by (cases x) auto }
+  moreover then have "(\<lambda>x. -a - x)`uminus`A = uminus ` (\<lambda>x. a - x) ` A"
+    by (auto simp: image_image)
+  ultimately show ?thesis
+    using Sup_ereal_cminus[of "uminus ` A" "-a"] assms
+    by (auto simp add: ereal_Sup_uminus_image_eq ereal_Inf_uminus_image_eq)
+qed
+
+lemma INFI_ereal_cminus:
+  fixes a :: ereal assumes "A \<noteq> {}" and "\<bar>a\<bar> \<noteq> \<infinity>"
+  shows "(INF x:A. a - f x) = a - (SUP x:A. f x)"
+  using Inf_ereal_cminus[of "f`A" a] assms
+  unfolding SUPR_def INFI_def image_image
+  by auto
+
+lemma uminus_ereal_add_uminus_uminus:
+  fixes a b :: ereal shows "a \<noteq> \<infinity> \<Longrightarrow> b \<noteq> \<infinity> \<Longrightarrow> - (- a + - b) = a + b"
+  by (cases rule: ereal2_cases[of a b]) auto
+
+lemma INFI_ereal_add:
+  fixes f :: "nat \<Rightarrow> ereal"
+  assumes "decseq f" "decseq g" and fin: "\<And>i. f i \<noteq> \<infinity>" "\<And>i. g i \<noteq> \<infinity>"
+  shows "(INF i. f i + g i) = INFI UNIV f + INFI UNIV g"
+proof -
+  have INF_less: "(INF i. f i) < \<infinity>" "(INF i. g i) < \<infinity>"
+    using assms unfolding INF_less_iff by auto
+  { fix i from fin[of i] have "- ((- f i) + (- g i)) = f i + g i"
+      by (rule uminus_ereal_add_uminus_uminus) }
+  then have "(INF i. f i + g i) = (INF i. - ((- f i) + (- g i)))"
+    by simp
+  also have "\<dots> = INFI UNIV f + INFI UNIV g"
+    unfolding ereal_INFI_uminus
+    using assms INF_less
+    by (subst SUPR_ereal_add)
+       (auto simp: ereal_SUPR_uminus intro!: uminus_ereal_add_uminus_uminus)
+  finally show ?thesis .
+qed
+
+subsection "Limits on @{typ ereal}"
+
+subsubsection "Topological space"
+
+instantiation ereal :: topological_space
+begin
+
+definition "open A \<longleftrightarrow> open (ereal -` A)
+       \<and> (\<infinity> \<in> A \<longrightarrow> (\<exists>x. {ereal x <..} \<subseteq> A))
+       \<and> (-\<infinity> \<in> A \<longrightarrow> (\<exists>x. {..<ereal x} \<subseteq> A))"
+
+lemma open_PInfty: "open A \<Longrightarrow> \<infinity> \<in> A \<Longrightarrow> (\<exists>x. {ereal x<..} \<subseteq> A)"
+  unfolding open_ereal_def by auto
+
+lemma open_MInfty: "open A \<Longrightarrow> -\<infinity> \<in> A \<Longrightarrow> (\<exists>x. {..<ereal x} \<subseteq> A)"
+  unfolding open_ereal_def by auto
+
+lemma open_PInfty2: assumes "open A" "\<infinity> \<in> A" obtains x where "{ereal x<..} \<subseteq> A"
+  using open_PInfty[OF assms] by auto
+
+lemma open_MInfty2: assumes "open A" "-\<infinity> \<in> A" obtains x where "{..<ereal x} \<subseteq> A"
+  using open_MInfty[OF assms] by auto
+
+lemma ereal_openE: assumes "open A" obtains x y where
+  "open (ereal -` A)"
+  "\<infinity> \<in> A \<Longrightarrow> {ereal x<..} \<subseteq> A"
+  "-\<infinity> \<in> A \<Longrightarrow> {..<ereal y} \<subseteq> A"
+  using assms open_ereal_def by auto
+
+instance
+proof
+  let ?U = "UNIV::ereal set"
+  show "open ?U" unfolding open_ereal_def
+    by (auto intro!: exI[of _ 0])
+next
+  fix S T::"ereal set" assume "open S" and "open T"
+  from `open S`[THEN ereal_openE] guess xS yS .
+  moreover from `open T`[THEN ereal_openE] guess xT yT .
+  ultimately have
+    "open (ereal -` (S \<inter> T))"
+    "\<infinity> \<in> S \<inter> T \<Longrightarrow> {ereal (max xS xT) <..} \<subseteq> S \<inter> T"
+    "-\<infinity> \<in> S \<inter> T \<Longrightarrow> {..< ereal (min yS yT)} \<subseteq> S \<inter> T"
+    by auto
+  then show "open (S Int T)" unfolding open_ereal_def by blast
+next
+  fix K :: "ereal set set" assume "\<forall>S\<in>K. open S"
+  then have *: "\<forall>S. \<exists>x y. S \<in> K \<longrightarrow> open (ereal -` S) \<and>
+    (\<infinity> \<in> S \<longrightarrow> {ereal x <..} \<subseteq> S) \<and> (-\<infinity> \<in> S \<longrightarrow> {..< ereal y} \<subseteq> S)"
+    by (auto simp: open_ereal_def)
+  then show "open (Union K)" unfolding open_ereal_def
+  proof (intro conjI impI)
+    show "open (ereal -` \<Union>K)"
+      using *[THEN choice] by (auto simp: vimage_Union)
+  qed ((metis UnionE Union_upper subset_trans *)+)
+qed
+end
+
+lemma open_ereal: "open S \<Longrightarrow> open (ereal ` S)"
+  by (auto simp: inj_vimage_image_eq open_ereal_def)
+
+lemma open_ereal_vimage: "open S \<Longrightarrow> open (ereal -` S)"
+  unfolding open_ereal_def by auto
+
+lemma open_ereal_lessThan[intro, simp]: "open {..< a :: ereal}"
+proof -
+  have "\<And>x. ereal -` {..<ereal x} = {..< x}"
+    "ereal -` {..< \<infinity>} = UNIV" "ereal -` {..< -\<infinity>} = {}" by auto
+  then show ?thesis by (cases a) (auto simp: open_ereal_def)
+qed
+
+lemma open_ereal_greaterThan[intro, simp]:
+  "open {a :: ereal <..}"
+proof -
+  have "\<And>x. ereal -` {ereal x<..} = {x<..}"
+    "ereal -` {\<infinity><..} = {}" "ereal -` {-\<infinity><..} = UNIV" by auto
+  then show ?thesis by (cases a) (auto simp: open_ereal_def)
+qed
+
+lemma ereal_open_greaterThanLessThan[intro, simp]: "open {a::ereal <..< b}"
+  unfolding greaterThanLessThan_def by auto
+
+lemma closed_ereal_atLeast[simp, intro]: "closed {a :: ereal ..}"
+proof -
+  have "- {a ..} = {..< a}" by auto
+  then show "closed {a ..}"
+    unfolding closed_def using open_ereal_lessThan by auto
+qed
+
+lemma closed_ereal_atMost[simp, intro]: "closed {.. b :: ereal}"
+proof -
+  have "- {.. b} = {b <..}" by auto
+  then show "closed {.. b}"
+    unfolding closed_def using open_ereal_greaterThan by auto
+qed
+
+lemma closed_ereal_atLeastAtMost[simp, intro]:
+  shows "closed {a :: ereal .. b}"
+  unfolding atLeastAtMost_def by auto
+
+lemma closed_ereal_singleton:
+  "closed {a :: ereal}"
+by (metis atLeastAtMost_singleton closed_ereal_atLeastAtMost)
+
+lemma ereal_open_cont_interval:
+  fixes S :: "ereal set"
+  assumes "open S" "x \<in> S" "\<bar>x\<bar> \<noteq> \<infinity>"
+  obtains e where "e>0" "{x-e <..< x+e} \<subseteq> S"
+proof-
+  from `open S` have "open (ereal -` S)" by (rule ereal_openE)
+  then obtain e where "0 < e" and e: "\<And>y. dist y (real x) < e \<Longrightarrow> ereal y \<in> S"
+    using assms unfolding open_dist by force
+  show thesis
+  proof (intro that subsetI)
+    show "0 < ereal e" using `0 < e` by auto
+    fix y assume "y \<in> {x - ereal e<..<x + ereal e}"
+    with assms obtain t where "y = ereal t" "dist t (real x) < e"
+      apply (cases y) by (auto simp: dist_real_def)
+    then show "y \<in> S" using e[of t] by auto
+  qed
+qed
+
+lemma ereal_open_cont_interval2:
+  fixes S :: "ereal set"
+  assumes "open S" "x \<in> S" and x: "\<bar>x\<bar> \<noteq> \<infinity>"
+  obtains a b where "a < x" "x < b" "{a <..< b} \<subseteq> S"
+proof-
+  guess e using ereal_open_cont_interval[OF assms] .
+  with that[of "x-e" "x+e"] ereal_between[OF x, of e]
+  show thesis by auto
+qed
+
+instance ereal :: t2_space
+proof
+  fix x y :: ereal assume "x ~= y"
+  let "?P x (y::ereal)" = "EX U V. open U & open V & x : U & y : V & U Int V = {}"
+
+  { fix x y :: ereal assume "x < y"
+    from ereal_dense[OF this] obtain z where z: "x < z" "z < y" by auto
+    have "?P x y"
+      apply (rule exI[of _ "{..<z}"])
+      apply (rule exI[of _ "{z<..}"])
+      using z by auto }
+  note * = this
+
+  from `x ~= y`
+  show "EX U V. open U & open V & x : U & y : V & U Int V = {}"
+  proof (cases rule: linorder_cases)
+    assume "x = y" with `x ~= y` show ?thesis by simp
+  next assume "x < y" from *[OF this] show ?thesis by auto
+  next assume "y < x" from *[OF this] show ?thesis by auto
+  qed
+qed
+
+subsubsection {* Convergent sequences *}
+
+lemma lim_ereal[simp]:
+  "((\<lambda>n. ereal (f n)) ---> ereal x) net \<longleftrightarrow> (f ---> x) net" (is "?l = ?r")
+proof (intro iffI topological_tendstoI)
+  fix S assume "?l" "open S" "x \<in> S"
+  then show "eventually (\<lambda>x. f x \<in> S) net"
+    using `?l`[THEN topological_tendstoD, OF open_ereal, OF `open S`]
+    by (simp add: inj_image_mem_iff)
+next
+  fix S assume "?r" "open S" "ereal x \<in> S"
+  show "eventually (\<lambda>x. ereal (f x) \<in> S) net"
+    using `?r`[THEN topological_tendstoD, OF open_ereal_vimage, OF `open S`]
+    using `ereal x \<in> S` by auto
+qed
+
+lemma lim_real_of_ereal[simp]:
+  assumes lim: "(f ---> ereal x) net"
+  shows "((\<lambda>x. real (f x)) ---> x) net"
+proof (intro topological_tendstoI)
+  fix S assume "open S" "x \<in> S"
+  then have S: "open S" "ereal x \<in> ereal ` S"
+    by (simp_all add: inj_image_mem_iff)
+  have "\<forall>x. f x \<in> ereal ` S \<longrightarrow> real (f x) \<in> S" by auto
+  from this lim[THEN topological_tendstoD, OF open_ereal, OF S]
+  show "eventually (\<lambda>x. real (f x) \<in> S) net"
+    by (rule eventually_mono)
+qed
+
+lemma Lim_PInfty: "f ----> \<infinity> <-> (ALL B. EX N. ALL n>=N. f n >= ereal B)" (is "?l = ?r")
+proof
+  assume ?r
+  show ?l
+    apply(rule topological_tendstoI)
+    unfolding eventually_sequentially
+  proof-
+    fix S :: "ereal set" assume "open S" "\<infinity> : S"
+    from open_PInfty[OF this] guess B .. note B=this
+    from `?r`[rule_format,of "B+1"] guess N .. note N=this
+    show "EX N. ALL n>=N. f n : S" apply(rule_tac x=N in exI)
+    proof safe case goal1
+      have "ereal B < ereal (B + 1)" by auto
+      also have "... <= f n" using goal1 N by auto
+      finally show ?case using B by fastsimp
+    qed
+  qed
+next
+  assume ?l
+  show ?r
+  proof fix B::real have "open {ereal B<..}" "\<infinity> : {ereal B<..}" by auto
+    from topological_tendstoD[OF `?l` this,unfolded eventually_sequentially]
+    guess N .. note N=this
+    show "EX N. ALL n>=N. ereal B <= f n" apply(rule_tac x=N in exI) using N by auto
+  qed
+qed
+
+
+lemma Lim_MInfty: "f ----> (-\<infinity>) <-> (ALL B. EX N. ALL n>=N. f n <= ereal B)" (is "?l = ?r")
+proof
+  assume ?r
+  show ?l
+    apply(rule topological_tendstoI)
+    unfolding eventually_sequentially
+  proof-
+    fix S :: "ereal set"
+    assume "open S" "(-\<infinity>) : S"
+    from open_MInfty[OF this] guess B .. note B=this
+    from `?r`[rule_format,of "B-(1::real)"] guess N .. note N=this
+    show "EX N. ALL n>=N. f n : S" apply(rule_tac x=N in exI)
+    proof safe case goal1
+      have "ereal (B - 1) >= f n" using goal1 N by auto
+      also have "... < ereal B" by auto
+      finally show ?case using B by fastsimp
+    qed
+  qed
+next assume ?l show ?r
+  proof fix B::real have "open {..<ereal B}" "(-\<infinity>) : {..<ereal B}" by auto
+    from topological_tendstoD[OF `?l` this,unfolded eventually_sequentially]
+    guess N .. note N=this
+    show "EX N. ALL n>=N. ereal B >= f n" apply(rule_tac x=N in exI) using N by auto
+  qed
+qed
+
+
+lemma Lim_bounded_PInfty: assumes lim:"f ----> l" and "!!n. f n <= ereal B" shows "l ~= \<infinity>"
+proof(rule ccontr,unfold not_not) let ?B = "B + 1" assume as:"l=\<infinity>"
+  from lim[unfolded this Lim_PInfty,rule_format,of "?B"]
+  guess N .. note N=this[rule_format,OF le_refl]
+  hence "ereal ?B <= ereal B" using assms(2)[of N] by(rule order_trans)
+  hence "ereal ?B < ereal ?B" apply (rule le_less_trans) by auto
+  thus False by auto
+qed
+
+
+lemma Lim_bounded_MInfty: assumes lim:"f ----> l" and "!!n. f n >= ereal B" shows "l ~= (-\<infinity>)"
+proof(rule ccontr,unfold not_not) let ?B = "B - 1" assume as:"l=(-\<infinity>)"
+  from lim[unfolded this Lim_MInfty,rule_format,of "?B"]
+  guess N .. note N=this[rule_format,OF le_refl]
+  hence "ereal B <= ereal ?B" using assms(2)[of N] order_trans[of "ereal B" "f N" "ereal(B - 1)"] by blast
+  thus False by auto
+qed
+
+
+lemma tendsto_explicit:
+  "f ----> f0 <-> (ALL S. open S --> f0 : S --> (EX N. ALL n>=N. f n : S))"
+  unfolding tendsto_def eventually_sequentially by auto
+
+
+lemma tendsto_obtains_N:
+  assumes "f ----> f0"
+  assumes "open S" "f0 : S"
+  obtains N where "ALL n>=N. f n : S"
+  using tendsto_explicit[of f f0] assms by auto
+
+
+lemma tail_same_limit:
+  fixes X Y N
+  assumes "X ----> L" "ALL n>=N. X n = Y n"
+  shows "Y ----> L"
+proof-
+{ fix S assume "open S" and "L:S"
+  from this obtain N1 where "ALL n>=N1. X n : S"
+     using assms unfolding tendsto_def eventually_sequentially by auto
+  hence "ALL n>=max N N1. Y n : S" using assms by auto
+  hence "EX N. ALL n>=N. Y n : S" apply(rule_tac x="max N N1" in exI) by auto
+}
+thus ?thesis using tendsto_explicit by auto
+qed
+
+
+lemma Lim_bounded_PInfty2:
+assumes lim:"f ----> l" and "ALL n>=N. f n <= ereal B"
+shows "l ~= \<infinity>"
+proof-
+  def g == "(%n. if n>=N then f n else ereal B)"
+  hence "g ----> l" using tail_same_limit[of f l N g] lim by auto
+  moreover have "!!n. g n <= ereal B" using g_def assms by auto
+  ultimately show ?thesis using  Lim_bounded_PInfty by auto
+qed
+
+lemma Lim_bounded_ereal:
+  assumes lim:"f ----> (l :: ereal)"
+  and "ALL n>=M. f n <= C"
+  shows "l<=C"
+proof-
+{ assume "l=(-\<infinity>)" hence ?thesis by auto }
+moreover
+{ assume "~(l=(-\<infinity>))"
+  { assume "C=\<infinity>" hence ?thesis by auto }
+  moreover
+  { assume "C=(-\<infinity>)" hence "ALL n>=M. f n = (-\<infinity>)" using assms by auto
+    hence "l=(-\<infinity>)" using assms
+       tendsto_unique[OF trivial_limit_sequentially] tail_same_limit[of "\<lambda>n. -\<infinity>" "-\<infinity>" M f, OF tendsto_const] by auto
+    hence ?thesis by auto }
+  moreover
+  { assume "EX B. C = ereal B"
+    from this obtain B where B_def: "C=ereal B" by auto
+    hence "~(l=\<infinity>)" using Lim_bounded_PInfty2 assms by auto
+    from this obtain m where m_def: "ereal m=l" using `~(l=(-\<infinity>))` by (cases l) auto
+    from this obtain N where N_def: "ALL n>=N. f n : {ereal(m - 1) <..< ereal(m+1)}"
+       apply (subst tendsto_obtains_N[of f l "{ereal(m - 1) <..< ereal(m+1)}"]) using assms by auto
+    { fix n assume "n>=N"
+      hence "EX r. ereal r = f n" using N_def by (cases "f n") auto
+    } from this obtain g where g_def: "ALL n>=N. ereal (g n) = f n" by metis
+    hence "(%n. ereal (g n)) ----> l" using tail_same_limit[of f l N] assms by auto
+    hence *: "(%n. g n) ----> m" using m_def by auto
+    { fix n assume "n>=max N M"
+      hence "ereal (g n) <= ereal B" using assms g_def B_def by auto
+      hence "g n <= B" by auto
+    } hence "EX N. ALL n>=N. g n <= B" by blast
+    hence "m<=B" using * LIMSEQ_le_const2[of g m B] by auto
+    hence ?thesis using m_def B_def by auto
+  } ultimately have ?thesis by (cases C) auto
+} ultimately show ?thesis by blast
+qed
+
+lemma real_of_ereal_mult[simp]:
+  fixes a b :: ereal shows "real (a * b) = real a * real b"
+  by (cases rule: ereal2_cases[of a b]) auto
+
+lemma real_of_ereal_eq_0:
+  fixes x :: ereal shows "real x = 0 \<longleftrightarrow> x = \<infinity> \<or> x = -\<infinity> \<or> x = 0"
+  by (cases x) auto
+
+lemma tendsto_ereal_realD:
+  fixes f :: "'a \<Rightarrow> ereal"
+  assumes "x \<noteq> 0" and tendsto: "((\<lambda>x. ereal (real (f x))) ---> x) net"
+  shows "(f ---> x) net"
+proof (intro topological_tendstoI)
+  fix S assume S: "open S" "x \<in> S"
+  with `x \<noteq> 0` have "open (S - {0})" "x \<in> S - {0}" by auto
+  from tendsto[THEN topological_tendstoD, OF this]
+  show "eventually (\<lambda>x. f x \<in> S) net"
+    by (rule eventually_rev_mp) (auto simp: ereal_real real_of_ereal_0)
+qed
+
+lemma tendsto_ereal_realI:
+  fixes f :: "'a \<Rightarrow> ereal"
+  assumes x: "\<bar>x\<bar> \<noteq> \<infinity>" and tendsto: "(f ---> x) net"
+  shows "((\<lambda>x. ereal (real (f x))) ---> x) net"
+proof (intro topological_tendstoI)
+  fix S assume "open S" "x \<in> S"
+  with x have "open (S - {\<infinity>, -\<infinity>})" "x \<in> S - {\<infinity>, -\<infinity>}" by auto
+  from tendsto[THEN topological_tendstoD, OF this]
+  show "eventually (\<lambda>x. ereal (real (f x)) \<in> S) net"
+    by (elim eventually_elim1) (auto simp: ereal_real)
+qed
+
+lemma ereal_mult_cancel_left:
+  fixes a b c :: ereal shows "a * b = a * c \<longleftrightarrow>
+    ((\<bar>a\<bar> = \<infinity> \<and> 0 < b * c) \<or> a = 0 \<or> b = c)"
+  by (cases rule: ereal3_cases[of a b c])
+     (simp_all add: zero_less_mult_iff)
+
+lemma ereal_inj_affinity:
+  fixes m t :: ereal
+  assumes "\<bar>m\<bar> \<noteq> \<infinity>" "m \<noteq> 0" "\<bar>t\<bar> \<noteq> \<infinity>"
+  shows "inj_on (\<lambda>x. m * x + t) A"
+  using assms
+  by (cases rule: ereal2_cases[of m t])
+     (auto intro!: inj_onI simp: ereal_add_cancel_right ereal_mult_cancel_left)
+
+lemma ereal_PInfty_eq_plus[simp]:
+  fixes a b :: ereal
+  shows "\<infinity> = a + b \<longleftrightarrow> a = \<infinity> \<or> b = \<infinity>"
+  by (cases rule: ereal2_cases[of a b]) auto
+
+lemma ereal_MInfty_eq_plus[simp]:
+  fixes a b :: ereal
+  shows "-\<infinity> = a + b \<longleftrightarrow> (a = -\<infinity> \<and> b \<noteq> \<infinity>) \<or> (b = -\<infinity> \<and> a \<noteq> \<infinity>)"
+  by (cases rule: ereal2_cases[of a b]) auto
+
+lemma ereal_less_divide_pos:
+  fixes x y :: ereal
+  shows "x > 0 \<Longrightarrow> x \<noteq> \<infinity> \<Longrightarrow> y < z / x \<longleftrightarrow> x * y < z"
+  by (cases rule: ereal3_cases[of x y z]) (auto simp: field_simps)
+
+lemma ereal_divide_less_pos:
+  fixes x y z :: ereal
+  shows "x > 0 \<Longrightarrow> x \<noteq> \<infinity> \<Longrightarrow> y / x < z \<longleftrightarrow> y < x * z"
+  by (cases rule: ereal3_cases[of x y z]) (auto simp: field_simps)
+
+lemma ereal_divide_eq:
+  fixes a b c :: ereal
+  shows "b \<noteq> 0 \<Longrightarrow> \<bar>b\<bar> \<noteq> \<infinity> \<Longrightarrow> a / b = c \<longleftrightarrow> a = b * c"
+  by (cases rule: ereal3_cases[of a b c])
+     (simp_all add: field_simps)
+
+lemma ereal_inverse_not_MInfty[simp]: "inverse (a::ereal) \<noteq> -\<infinity>"
+  by (cases a) auto
+
+lemma ereal_mult_m1[simp]: "x * ereal (-1) = -x"
+  by (cases x) auto
+
+lemma ereal_LimI_finite:
+  fixes x :: ereal
+  assumes "\<bar>x\<bar> \<noteq> \<infinity>"
+  assumes "!!r. 0 < r ==> EX N. ALL n>=N. u n < x + r & x < u n + r"
+  shows "u ----> x"
+proof (rule topological_tendstoI, unfold eventually_sequentially)
+  obtain rx where rx_def: "x=ereal rx" using assms by (cases x) auto
+  fix S assume "open S" "x : S"
+  then have "open (ereal -` S)" unfolding open_ereal_def by auto
+  with `x \<in> S` obtain r where "0 < r" and dist: "!!y. dist y rx < r ==> ereal y \<in> S"
+    unfolding open_real_def rx_def by auto
+  then obtain n where
+    upper: "!!N. n <= N ==> u N < x + ereal r" and
+    lower: "!!N. n <= N ==> x < u N + ereal r" using assms(2)[of "ereal r"] by auto
+  show "EX N. ALL n>=N. u n : S"
+  proof (safe intro!: exI[of _ n])
+    fix N assume "n <= N"
+    from upper[OF this] lower[OF this] assms `0 < r`
+    have "u N ~: {\<infinity>,(-\<infinity>)}" by auto
+    from this obtain ra where ra_def: "(u N) = ereal ra" by (cases "u N") auto
+    hence "rx < ra + r" and "ra < rx + r"
+       using rx_def assms `0 < r` lower[OF `n <= N`] upper[OF `n <= N`] by auto
+    hence "dist (real (u N)) rx < r"
+      using rx_def ra_def
+      by (auto simp: dist_real_def abs_diff_less_iff field_simps)
+    from dist[OF this] show "u N : S" using `u N  ~: {\<infinity>, -\<infinity>}`
+      by (auto simp: ereal_real split: split_if_asm)
+  qed
+qed
+
+lemma ereal_LimI_finite_iff:
+  fixes x :: ereal
+  assumes "\<bar>x\<bar> \<noteq> \<infinity>"
+  shows "u ----> x <-> (ALL r. 0 < r --> (EX N. ALL n>=N. u n < x + r & x < u n + r))"
+  (is "?lhs <-> ?rhs")
+proof
+  assume lim: "u ----> x"
+  { fix r assume "(r::ereal)>0"
+    from this obtain N where N_def: "ALL n>=N. u n : {x - r <..< x + r}"
+       apply (subst tendsto_obtains_N[of u x "{x - r <..< x + r}"])
+       using lim ereal_between[of x r] assms `r>0` by auto
+    hence "EX N. ALL n>=N. u n < x + r & x < u n + r"
+      using ereal_minus_less[of r x] by (cases r) auto
+  } then show "?rhs" by auto
+next
+  assume ?rhs then show "u ----> x"
+    using ereal_LimI_finite[of x] assms by auto
+qed
+
+
+subsubsection {* @{text Liminf} and @{text Limsup} *}
+
+definition
+  "Liminf net f = (GREATEST l. \<forall>y<l. eventually (\<lambda>x. y < f x) net)"
+
+definition
+  "Limsup net f = (LEAST l. \<forall>y>l. eventually (\<lambda>x. f x < y) net)"
+
+lemma Liminf_Sup:
+  fixes f :: "'a => 'b::{complete_lattice, linorder}"
+  shows "Liminf net f = Sup {l. \<forall>y<l. eventually (\<lambda>x. y < f x) net}"
+  by (auto intro!: Greatest_equality complete_lattice_class.Sup_upper simp: less_Sup_iff Liminf_def)
+
+lemma Limsup_Inf:
+  fixes f :: "'a => 'b::{complete_lattice, linorder}"
+  shows "Limsup net f = Inf {l. \<forall>y>l. eventually (\<lambda>x. f x < y) net}"
+  by (auto intro!: Least_equality complete_lattice_class.Inf_lower simp: Inf_less_iff Limsup_def)
+
+lemma ereal_SupI:
+  fixes x :: ereal
+  assumes "\<And>y. y \<in> A \<Longrightarrow> y \<le> x"
+  assumes "\<And>y. (\<And>z. z \<in> A \<Longrightarrow> z \<le> y) \<Longrightarrow> x \<le> y"
+  shows "Sup A = x"
+  unfolding Sup_ereal_def
+  using assms by (auto intro!: Least_equality)
+
+lemma ereal_InfI:
+  fixes x :: ereal
+  assumes "\<And>i. i \<in> A \<Longrightarrow> x \<le> i"
+  assumes "\<And>y. (\<And>i. i \<in> A \<Longrightarrow> y \<le> i) \<Longrightarrow> y \<le> x"
+  shows "Inf A = x"
+  unfolding Inf_ereal_def
+  using assms by (auto intro!: Greatest_equality)
+
+lemma Limsup_const:
+  fixes c :: "'a::{complete_lattice, linorder}"
+  assumes ntriv: "\<not> trivial_limit net"
+  shows "Limsup net (\<lambda>x. c) = c"
+  unfolding Limsup_Inf
+proof (safe intro!: antisym complete_lattice_class.Inf_greatest complete_lattice_class.Inf_lower)
+  fix x assume *: "\<forall>y>x. eventually (\<lambda>_. c < y) net"
+  show "c \<le> x"
+  proof (rule ccontr)
+    assume "\<not> c \<le> x" then have "x < c" by auto
+    then show False using ntriv * by (auto simp: trivial_limit_def)
+  qed
+qed auto
+
+lemma Liminf_const:
+  fixes c :: "'a::{complete_lattice, linorder}"
+  assumes ntriv: "\<not> trivial_limit net"
+  shows "Liminf net (\<lambda>x. c) = c"
+  unfolding Liminf_Sup
+proof (safe intro!: antisym complete_lattice_class.Sup_least complete_lattice_class.Sup_upper)
+  fix x assume *: "\<forall>y<x. eventually (\<lambda>_. y < c) net"
+  show "x \<le> c"
+  proof (rule ccontr)
+    assume "\<not> x \<le> c" then have "c < x" by auto
+    then show False using ntriv * by (auto simp: trivial_limit_def)
+  qed
+qed auto
+
+lemma mono_set:
+  fixes S :: "('a::order) set"
+  shows "mono S \<longleftrightarrow> (\<forall>x y. x \<le> y \<longrightarrow> x \<in> S \<longrightarrow> y \<in> S)"
+  by (auto simp: mono_def mem_def)
+
+lemma mono_greaterThan[intro, simp]: "mono {B<..}" unfolding mono_set by auto
+lemma mono_atLeast[intro, simp]: "mono {B..}" unfolding mono_set by auto
+lemma mono_UNIV[intro, simp]: "mono UNIV" unfolding mono_set by auto
+lemma mono_empty[intro, simp]: "mono {}" unfolding mono_set by auto
+
+lemma mono_set_iff:
+  fixes S :: "'a::{linorder,complete_lattice} set"
+  defines "a \<equiv> Inf S"
+  shows "mono S \<longleftrightarrow> (S = {a <..} \<or> S = {a..})" (is "_ = ?c")
+proof
+  assume "mono S"
+  then have mono: "\<And>x y. x \<le> y \<Longrightarrow> x \<in> S \<Longrightarrow> y \<in> S" by (auto simp: mono_set)
+  show ?c
+  proof cases
+    assume "a \<in> S"
+    show ?c
+      using mono[OF _ `a \<in> S`]
+      by (auto intro: complete_lattice_class.Inf_lower simp: a_def)
+  next
+    assume "a \<notin> S"
+    have "S = {a <..}"
+    proof safe
+      fix x assume "x \<in> S"
+      then have "a \<le> x" unfolding a_def by (rule complete_lattice_class.Inf_lower)
+      then show "a < x" using `x \<in> S` `a \<notin> S` by (cases "a = x") auto
+    next
+      fix x assume "a < x"
+      then obtain y where "y < x" "y \<in> S" unfolding a_def Inf_less_iff ..
+      with mono[of y x] show "x \<in> S" by auto
+    qed
+    then show ?c ..
+  qed
+qed auto
+
+lemma lim_imp_Liminf:
+  fixes f :: "'a \<Rightarrow> ereal"
+  assumes ntriv: "\<not> trivial_limit net"
+  assumes lim: "(f ---> f0) net"
+  shows "Liminf net f = f0"
+  unfolding Liminf_Sup
+proof (safe intro!: ereal_SupI)
+  fix y assume *: "\<forall>y'<y. eventually (\<lambda>x. y' < f x) net"
+  show "y \<le> f0"
+  proof (rule ereal_le_ereal)
+    fix B assume "B < y"
+    { assume "f0 < B"
+      then have "eventually (\<lambda>x. f x < B \<and> B < f x) net"
+         using topological_tendstoD[OF lim, of "{..<B}"] *[rule_format, OF `B < y`]
+         by (auto intro: eventually_conj)
+      also have "(\<lambda>x. f x < B \<and> B < f x) = (\<lambda>x. False)" by (auto simp: fun_eq_iff)
+      finally have False using ntriv[unfolded trivial_limit_def] by auto
+    } then show "B \<le> f0" by (metis linorder_le_less_linear)
+  qed
+next
+  fix y assume *: "\<forall>z. z \<in> {l. \<forall>y<l. eventually (\<lambda>x. y < f x) net} \<longrightarrow> z \<le> y"
+  show "f0 \<le> y"
+  proof (safe intro!: *[rule_format])
+    fix y assume "y < f0" then show "eventually (\<lambda>x. y < f x) net"
+      using lim[THEN topological_tendstoD, of "{y <..}"] by auto
+  qed
+qed
+
+lemma ereal_Liminf_le_Limsup:
+  fixes f :: "'a \<Rightarrow> ereal"
+  assumes ntriv: "\<not> trivial_limit net"
+  shows "Liminf net f \<le> Limsup net f"
+  unfolding Limsup_Inf Liminf_Sup
+proof (safe intro!: complete_lattice_class.Inf_greatest  complete_lattice_class.Sup_least)
+  fix u v assume *: "\<forall>y<u. eventually (\<lambda>x. y < f x) net" "\<forall>y>v. eventually (\<lambda>x. f x < y) net"
+  show "u \<le> v"
+  proof (rule ccontr)
+    assume "\<not> u \<le> v"
+    then obtain t where "t < u" "v < t"
+      using ereal_dense[of v u] by (auto simp: not_le)
+    then have "eventually (\<lambda>x. t < f x \<and> f x < t) net"
+      using * by (auto intro: eventually_conj)
+    also have "(\<lambda>x. t < f x \<and> f x < t) = (\<lambda>x. False)" by (auto simp: fun_eq_iff)
+    finally show False using ntriv by (auto simp: trivial_limit_def)
+  qed
+qed
+
+lemma Liminf_mono:
+  fixes f g :: "'a => ereal"
+  assumes ev: "eventually (\<lambda>x. f x \<le> g x) net"
+  shows "Liminf net f \<le> Liminf net g"
+  unfolding Liminf_Sup
+proof (safe intro!: Sup_mono bexI)
+  fix a y assume "\<forall>y<a. eventually (\<lambda>x. y < f x) net" and "y < a"
+  then have "eventually (\<lambda>x. y < f x) net" by auto
+  then show "eventually (\<lambda>x. y < g x) net"
+    by (rule eventually_rev_mp) (rule eventually_mono[OF _ ev], auto)
+qed simp
+
+lemma Liminf_eq:
+  fixes f g :: "'a \<Rightarrow> ereal"
+  assumes "eventually (\<lambda>x. f x = g x) net"
+  shows "Liminf net f = Liminf net g"
+  by (intro antisym Liminf_mono eventually_mono[OF _ assms]) auto
+
+lemma Liminf_mono_all:
+  fixes f g :: "'a \<Rightarrow> ereal"
+  assumes "\<And>x. f x \<le> g x"
+  shows "Liminf net f \<le> Liminf net g"
+  using assms by (intro Liminf_mono always_eventually) auto
+
+lemma Limsup_mono:
+  fixes f g :: "'a \<Rightarrow> ereal"
+  assumes ev: "eventually (\<lambda>x. f x \<le> g x) net"
+  shows "Limsup net f \<le> Limsup net g"
+  unfolding Limsup_Inf
+proof (safe intro!: Inf_mono bexI)
+  fix a y assume "\<forall>y>a. eventually (\<lambda>x. g x < y) net" and "a < y"
+  then have "eventually (\<lambda>x. g x < y) net" by auto
+  then show "eventually (\<lambda>x. f x < y) net"
+    by (rule eventually_rev_mp) (rule eventually_mono[OF _ ev], auto)
+qed simp
+
+lemma Limsup_mono_all:
+  fixes f g :: "'a \<Rightarrow> ereal"
+  assumes "\<And>x. f x \<le> g x"
+  shows "Limsup net f \<le> Limsup net g"
+  using assms by (intro Limsup_mono always_eventually) auto
+
+lemma Limsup_eq:
+  fixes f g :: "'a \<Rightarrow> ereal"
+  assumes "eventually (\<lambda>x. f x = g x) net"
+  shows "Limsup net f = Limsup net g"
+  by (intro antisym Limsup_mono eventually_mono[OF _ assms]) auto
+
+abbreviation "liminf \<equiv> Liminf sequentially"
+
+abbreviation "limsup \<equiv> Limsup sequentially"
+
+lemma (in complete_lattice) less_INFD:
+  assumes "y < INFI A f"" i \<in> A" shows "y < f i"
+proof -
+  note `y < INFI A f`
+  also have "INFI A f \<le> f i" using `i \<in> A` by (rule INF_leI)
+  finally show "y < f i" .
+qed
+
+lemma liminf_SUPR_INFI:
+  fixes f :: "nat \<Rightarrow> ereal"
+  shows "liminf f = (SUP n. INF m:{n..}. f m)"
+  unfolding Liminf_Sup eventually_sequentially
+proof (safe intro!: antisym complete_lattice_class.Sup_least)
+  fix x assume *: "\<forall>y<x. \<exists>N. \<forall>n\<ge>N. y < f n" show "x \<le> (SUP n. INF m:{n..}. f m)"
+  proof (rule ereal_le_ereal)
+    fix y assume "y < x"
+    with * obtain N where "\<And>n. N \<le> n \<Longrightarrow> y < f n" by auto
+    then have "y \<le> (INF m:{N..}. f m)" by (force simp: le_INF_iff)
+    also have "\<dots> \<le> (SUP n. INF m:{n..}. f m)" by (intro le_SUPI) auto
+    finally show "y \<le> (SUP n. INF m:{n..}. f m)" .
+  qed
+next
+  show "(SUP n. INF m:{n..}. f m) \<le> Sup {l. \<forall>y<l. \<exists>N. \<forall>n\<ge>N. y < f n}"
+  proof (unfold SUPR_def, safe intro!: Sup_mono bexI)
+    fix y n assume "y < INFI {n..} f"
+    from less_INFD[OF this] show "\<exists>N. \<forall>n\<ge>N. y < f n" by (intro exI[of _ n]) auto
+  qed (rule order_refl)
+qed
+
+lemma tail_same_limsup:
+  fixes X Y :: "nat => ereal"
+  assumes "\<And>n. N \<le> n \<Longrightarrow> X n = Y n"
+  shows "limsup X = limsup Y"
+  using Limsup_eq[of X Y sequentially] eventually_sequentially assms by auto
+
+lemma tail_same_liminf:
+  fixes X Y :: "nat => ereal"
+  assumes "\<And>n. N \<le> n \<Longrightarrow> X n = Y n"
+  shows "liminf X = liminf Y"
+  using Liminf_eq[of X Y sequentially] eventually_sequentially assms by auto
+
+lemma liminf_mono:
+  fixes X Y :: "nat \<Rightarrow> ereal"
+  assumes "\<And>n. N \<le> n \<Longrightarrow> X n <= Y n"
+  shows "liminf X \<le> liminf Y"
+  using Liminf_mono[of X Y sequentially] eventually_sequentially assms by auto
+
+lemma limsup_mono:
+  fixes X Y :: "nat => ereal"
+  assumes "\<And>n. N \<le> n \<Longrightarrow> X n <= Y n"
+  shows "limsup X \<le> limsup Y"
+  using Limsup_mono[of X Y sequentially] eventually_sequentially assms by auto
+
+declare trivial_limit_sequentially[simp]
+
+lemma
+  fixes X :: "nat \<Rightarrow> ereal"
+  shows ereal_incseq_uminus[simp]: "incseq (\<lambda>i. - X i) = decseq X"
+    and ereal_decseq_uminus[simp]: "decseq (\<lambda>i. - X i) = incseq X"
+  unfolding incseq_def decseq_def by auto
+
+lemma liminf_bounded:
+  fixes X Y :: "nat \<Rightarrow> ereal"
+  assumes "\<And>n. N \<le> n \<Longrightarrow> C \<le> X n"
+  shows "C \<le> liminf X"
+  using liminf_mono[of N "\<lambda>n. C" X] assms Liminf_const[of sequentially C] by simp
+
+lemma limsup_bounded:
+  fixes X Y :: "nat => ereal"
+  assumes "\<And>n. N \<le> n \<Longrightarrow> X n <= C"
+  shows "limsup X \<le> C"
+  using limsup_mono[of N X "\<lambda>n. C"] assms Limsup_const[of sequentially C] by simp
+
+lemma liminf_bounded_iff:
+  fixes x :: "nat \<Rightarrow> ereal"
+  shows "C \<le> liminf x \<longleftrightarrow> (\<forall>B<C. \<exists>N. \<forall>n\<ge>N. B < x n)" (is "?lhs <-> ?rhs")
+proof safe
+  fix B assume "B < C" "C \<le> liminf x"
+  then have "B < liminf x" by auto
+  then obtain N where "B < (INF m:{N..}. x m)"
+    unfolding liminf_SUPR_INFI SUPR_def less_Sup_iff by auto
+  from less_INFD[OF this] show "\<exists>N. \<forall>n\<ge>N. B < x n" by auto
+next
+  assume *: "\<forall>B<C. \<exists>N. \<forall>n\<ge>N. B < x n"
+  { fix B assume "B<C"
+    then obtain N where "\<forall>n\<ge>N. B < x n" using `?rhs` by auto
+    hence "B \<le> (INF m:{N..}. x m)" by (intro le_INFI) auto
+    also have "... \<le> liminf x" unfolding liminf_SUPR_INFI by (intro le_SUPI) simp
+    finally have "B \<le> liminf x" .
+  } then show "?lhs" by (metis * leD liminf_bounded linorder_le_less_linear)
+qed
+
+lemma liminf_subseq_mono:
+  fixes X :: "nat \<Rightarrow> ereal"
+  assumes "subseq r"
+  shows "liminf X \<le> liminf (X \<circ> r) "
+proof-
+  have "\<And>n. (INF m:{n..}. X m) \<le> (INF m:{n..}. (X \<circ> r) m)"
+  proof (safe intro!: INF_mono)
+    fix n m :: nat assume "n \<le> m" then show "\<exists>ma\<in>{n..}. X ma \<le> (X \<circ> r) m"
+      using seq_suble[OF `subseq r`, of m] by (intro bexI[of _ "r m"]) auto
+  qed
+  then show ?thesis by (auto intro!: SUP_mono simp: liminf_SUPR_INFI comp_def)
+qed
+
+lemma ereal_real': assumes "\<bar>x\<bar> \<noteq> \<infinity>" shows "ereal (real x) = x"
+  using assms by auto
+
+lemma ereal_le_ereal_bounded:
+  fixes x y z :: ereal
+  assumes "z \<le> y"
+  assumes *: "\<And>B. z < B \<Longrightarrow> B < x \<Longrightarrow> B \<le> y"
+  shows "x \<le> y"
+proof (rule ereal_le_ereal)
+  fix B assume "B < x"
+  show "B \<le> y"
+  proof cases
+    assume "z < B" from *[OF this `B < x`] show "B \<le> y" .
+  next
+    assume "\<not> z < B" with `z \<le> y` show "B \<le> y" by auto
+  qed
+qed
+
+lemma fixes x y :: ereal
+  shows Sup_atMost[simp]: "Sup {.. y} = y"
+    and Sup_lessThan[simp]: "Sup {..< y} = y"
+    and Sup_atLeastAtMost[simp]: "x \<le> y \<Longrightarrow> Sup { x .. y} = y"
+    and Sup_greaterThanAtMost[simp]: "x < y \<Longrightarrow> Sup { x <.. y} = y"
+    and Sup_atLeastLessThan[simp]: "x < y \<Longrightarrow> Sup { x ..< y} = y"
+  by (auto simp: Sup_ereal_def intro!: Least_equality
+           intro: ereal_le_ereal ereal_le_ereal_bounded[of x])
+
+lemma Sup_greaterThanlessThan[simp]:
+  fixes x y :: ereal assumes "x < y" shows "Sup { x <..< y} = y"
+  unfolding Sup_ereal_def
+proof (intro Least_equality ereal_le_ereal_bounded[of _ _ y])
+  fix z assume z: "\<forall>u\<in>{x<..<y}. u \<le> z"
+  from ereal_dense[OF `x < y`] guess w .. note w = this
+  with z[THEN bspec, of w] show "x \<le> z" by auto
+qed auto
+
+lemma real_ereal_id: "real o ereal = id"
+proof-
+{ fix x have "(real o ereal) x = id x" by auto }
+from this show ?thesis using ext by blast
+qed
+
+lemma open_image_ereal: "open(UNIV-{ \<infinity> , (-\<infinity> :: ereal)})"
+by (metis range_ereal open_ereal open_UNIV)
+
+lemma ereal_le_distrib:
+  fixes a b c :: ereal shows "c * (a + b) \<le> c * a + c * b"
+  by (cases rule: ereal3_cases[of a b c])
+     (auto simp add: field_simps not_le mult_le_0_iff mult_less_0_iff)
+
+lemma ereal_pos_distrib:
+  fixes a b c :: ereal assumes "0 \<le> c" "c \<noteq> \<infinity>" shows "c * (a + b) = c * a + c * b"
+  using assms by (cases rule: ereal3_cases[of a b c])
+                 (auto simp add: field_simps not_le mult_le_0_iff mult_less_0_iff)
+
+lemma ereal_pos_le_distrib:
+fixes a b c :: ereal
+assumes "c>=0"
+shows "c * (a + b) <= c * a + c * b"
+  using assms by (cases rule: ereal3_cases[of a b c])
+                 (auto simp add: field_simps)
+
+lemma ereal_max_mono:
+  "[| (a::ereal) <= b; c <= d |] ==> max a c <= max b d"
+  by (metis sup_ereal_def sup_mono)
+
+
+lemma ereal_max_least:
+  "[| (a::ereal) <= x; c <= x |] ==> max a c <= x"
+  by (metis sup_ereal_def sup_least)
+
+end
--- a/src/HOL/Library/Extended_Reals.thy	Wed Jul 20 12:23:20 2011 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,2535 +0,0 @@
-(*  Title:      HOL/Library/Extended_Reals.thy
-    Author:     Johannes Hölzl, TU München
-    Author:     Robert Himmelmann, TU München
-    Author:     Armin Heller, TU München
-    Author:     Bogdan Grechuk, University of Edinburgh
-*)
-
-header {* Extended real number line *}
-
-theory Extended_Reals
-  imports Complex_Main
-begin
-
-text {*
-
-For more lemmas about the extended real numbers go to
-  @{text "src/HOL/Multivariate_Analysis/Extended_Real_Limits.thy"}
-
-*}
-
-lemma (in complete_lattice) atLeast_eq_UNIV_iff: "{x..} = UNIV \<longleftrightarrow> x = bot"
-proof
-  assume "{x..} = UNIV"
-  show "x = bot"
-  proof (rule ccontr)
-    assume "x \<noteq> bot" then have "bot \<notin> {x..}" by (simp add: le_less)
-    then show False using `{x..} = UNIV` by simp
-  qed
-qed auto
-
-lemma SUPR_pair:
-  "(SUP i : A. SUP j : B. f i j) = (SUP p : A \<times> B. f (fst p) (snd p))"
-  by (rule antisym) (auto intro!: SUP_leI le_SUPI2)
-
-lemma INFI_pair:
-  "(INF i : A. INF j : B. f i j) = (INF p : A \<times> B. f (fst p) (snd p))"
-  by (rule antisym) (auto intro!: le_INFI INF_leI2)
-
-subsection {* Definition and basic properties *}
-
-datatype extreal = extreal real | PInfty | MInfty
-
-notation (xsymbols)
-  PInfty  ("\<infinity>")
-
-notation (HTML output)
-  PInfty  ("\<infinity>")
-
-declare [[coercion "extreal :: real \<Rightarrow> extreal"]]
-
-instantiation extreal :: uminus
-begin
-  fun uminus_extreal where
-    "- (extreal r) = extreal (- r)"
-  | "- \<infinity> = MInfty"
-  | "- MInfty = \<infinity>"
-  instance ..
-end
-
-lemma inj_extreal[simp]: "inj_on extreal A"
-  unfolding inj_on_def by auto
-
-lemma MInfty_neq_PInfty[simp]:
-  "\<infinity> \<noteq> - \<infinity>" "- \<infinity> \<noteq> \<infinity>" by simp_all
-
-lemma MInfty_neq_extreal[simp]:
-  "extreal r \<noteq> - \<infinity>" "- \<infinity> \<noteq> extreal r" by simp_all
-
-lemma MInfinity_cases[simp]:
-  "(case - \<infinity> of extreal r \<Rightarrow> f r | \<infinity> \<Rightarrow> y | MInfinity \<Rightarrow> z) = z"
-  by simp
-
-lemma extreal_uminus_uminus[simp]:
-  fixes a :: extreal shows "- (- a) = a"
-  by (cases a) simp_all
-
-lemma MInfty_eq[simp, code_post]:
-  "MInfty = - \<infinity>" by simp
-
-declare uminus_extreal.simps(2)[code_inline, simp del]
-
-lemma extreal_cases[case_names real PInf MInf, cases type: extreal]:
-  assumes "\<And>r. x = extreal r \<Longrightarrow> P"
-  assumes "x = \<infinity> \<Longrightarrow> P"
-  assumes "x = -\<infinity> \<Longrightarrow> P"
-  shows P
-  using assms by (cases x) auto
-
-lemmas extreal2_cases = extreal_cases[case_product extreal_cases]
-lemmas extreal3_cases = extreal2_cases[case_product extreal_cases]
-
-lemma extreal_uminus_eq_iff[simp]:
-  fixes a b :: extreal shows "-a = -b \<longleftrightarrow> a = b"
-  by (cases rule: extreal2_cases[of a b]) simp_all
-
-function of_extreal :: "extreal \<Rightarrow> real" where
-"of_extreal (extreal r) = r" |
-"of_extreal \<infinity> = 0" |
-"of_extreal (-\<infinity>) = 0"
-  by (auto intro: extreal_cases)
-termination proof qed (rule wf_empty)
-
-defs (overloaded)
-  real_of_extreal_def [code_unfold]: "real \<equiv> of_extreal"
-
-lemma real_of_extreal[simp]:
-    "real (- x :: extreal) = - (real x)"
-    "real (extreal r) = r"
-    "real \<infinity> = 0"
-  by (cases x) (simp_all add: real_of_extreal_def)
-
-lemma range_extreal[simp]: "range extreal = UNIV - {\<infinity>, -\<infinity>}"
-proof safe
-  fix x assume "x \<notin> range extreal" "x \<noteq> \<infinity>"
-  then show "x = -\<infinity>" by (cases x) auto
-qed auto
-
-lemma extreal_range_uminus[simp]: "range uminus = (UNIV::extreal set)"
-proof safe
-  fix x :: extreal show "x \<in> range uminus" by (intro image_eqI[of _ _ "-x"]) auto
-qed auto
-
-instantiation extreal :: number
-begin
-definition [simp]: "number_of x = extreal (number_of x)"
-instance proof qed
-end
-
-instantiation extreal :: abs
-begin
-  function abs_extreal where
-    "\<bar>extreal r\<bar> = extreal \<bar>r\<bar>"
-  | "\<bar>-\<infinity>\<bar> = \<infinity>"
-  | "\<bar>\<infinity>\<bar> = \<infinity>"
-  by (auto intro: extreal_cases)
-  termination proof qed (rule wf_empty)
-  instance ..
-end
-
-lemma abs_eq_infinity_cases[elim!]: "\<lbrakk> \<bar>x\<bar> = \<infinity> ; x = \<infinity> \<Longrightarrow> P ; x = -\<infinity> \<Longrightarrow> P \<rbrakk> \<Longrightarrow> P"
-  by (cases x) auto
-
-lemma abs_neq_infinity_cases[elim!]: "\<lbrakk> \<bar>x\<bar> \<noteq> \<infinity> ; \<And>r. x = extreal r \<Longrightarrow> P \<rbrakk> \<Longrightarrow> P"
-  by (cases x) auto
-
-lemma abs_extreal_uminus[simp]: "\<bar>- x\<bar> = \<bar>x::extreal\<bar>"
-  by (cases x) auto
-
-subsubsection "Addition"
-
-instantiation extreal :: comm_monoid_add
-begin
-
-definition "0 = extreal 0"
-
-function plus_extreal where
-"extreal r + extreal p = extreal (r + p)" |
-"\<infinity> + a = \<infinity>" |
-"a + \<infinity> = \<infinity>" |
-"extreal r + -\<infinity> = - \<infinity>" |
-"-\<infinity> + extreal p = -\<infinity>" |
-"-\<infinity> + -\<infinity> = -\<infinity>"
-proof -
-  case (goal1 P x)
-  moreover then obtain a b where "x = (a, b)" by (cases x) auto
-  ultimately show P
-   by (cases rule: extreal2_cases[of a b]) auto
-qed auto
-termination proof qed (rule wf_empty)
-
-lemma Infty_neq_0[simp]:
-  "\<infinity> \<noteq> 0" "0 \<noteq> \<infinity>"
-  "-\<infinity> \<noteq> 0" "0 \<noteq> -\<infinity>"
-  by (simp_all add: zero_extreal_def)
-
-lemma extreal_eq_0[simp]:
-  "extreal r = 0 \<longleftrightarrow> r = 0"
-  "0 = extreal r \<longleftrightarrow> r = 0"
-  unfolding zero_extreal_def by simp_all
-
-instance
-proof
-  fix a :: extreal show "0 + a = a"
-    by (cases a) (simp_all add: zero_extreal_def)
-  fix b :: extreal show "a + b = b + a"
-    by (cases rule: extreal2_cases[of a b]) simp_all
-  fix c :: extreal show "a + b + c = a + (b + c)"
-    by (cases rule: extreal3_cases[of a b c]) simp_all
-qed
-end
-
-lemma real_of_extreal_0[simp]: "real (0::extreal) = 0"
-  unfolding real_of_extreal_def zero_extreal_def by simp
-
-lemma abs_extreal_zero[simp]: "\<bar>0\<bar> = (0::extreal)"
-  unfolding zero_extreal_def abs_extreal.simps by simp
-
-lemma extreal_uminus_zero[simp]:
-  "- 0 = (0::extreal)"
-  by (simp add: zero_extreal_def)
-
-lemma extreal_uminus_zero_iff[simp]:
-  fixes a :: extreal shows "-a = 0 \<longleftrightarrow> a = 0"
-  by (cases a) simp_all
-
-lemma extreal_plus_eq_PInfty[simp]:
-  shows "a + b = \<infinity> \<longleftrightarrow> a = \<infinity> \<or> b = \<infinity>"
-  by (cases rule: extreal2_cases[of a b]) auto
-
-lemma extreal_plus_eq_MInfty[simp]:
-  shows "a + b = -\<infinity> \<longleftrightarrow>
-    (a = -\<infinity> \<or> b = -\<infinity>) \<and> a \<noteq> \<infinity> \<and> b \<noteq> \<infinity>"
-  by (cases rule: extreal2_cases[of a b]) auto
-
-lemma extreal_add_cancel_left:
-  assumes "a \<noteq> -\<infinity>"
-  shows "a + b = a + c \<longleftrightarrow> (a = \<infinity> \<or> b = c)"
-  using assms by (cases rule: extreal3_cases[of a b c]) auto
-
-lemma extreal_add_cancel_right:
-  assumes "a \<noteq> -\<infinity>"
-  shows "b + a = c + a \<longleftrightarrow> (a = \<infinity> \<or> b = c)"
-  using assms by (cases rule: extreal3_cases[of a b c]) auto
-
-lemma extreal_real:
-  "extreal (real x) = (if \<bar>x\<bar> = \<infinity> then 0 else x)"
-  by (cases x) simp_all
-
-lemma real_of_extreal_add:
-  fixes a b :: extreal
-  shows "real (a + b) = (if (\<bar>a\<bar> = \<infinity>) \<and> (\<bar>b\<bar> = \<infinity>) \<or> (\<bar>a\<bar> \<noteq> \<infinity>) \<and> (\<bar>b\<bar> \<noteq> \<infinity>) then real a + real b else 0)"
-  by (cases rule: extreal2_cases[of a b]) auto
-
-subsubsection "Linear order on @{typ extreal}"
-
-instantiation extreal :: linorder
-begin
-
-function less_extreal where
-"extreal x < extreal y \<longleftrightarrow> x < y" |
-"        \<infinity> < a         \<longleftrightarrow> False" |
-"        a < -\<infinity>        \<longleftrightarrow> False" |
-"extreal x < \<infinity>         \<longleftrightarrow> True" |
-"       -\<infinity> < extreal r \<longleftrightarrow> True" |
-"       -\<infinity> < \<infinity>         \<longleftrightarrow> True"
-proof -
-  case (goal1 P x)
-  moreover then obtain a b where "x = (a,b)" by (cases x) auto
-  ultimately show P by (cases rule: extreal2_cases[of a b]) auto
-qed simp_all
-termination by (relation "{}") simp
-
-definition "x \<le> (y::extreal) \<longleftrightarrow> x < y \<or> x = y"
-
-lemma extreal_infty_less[simp]:
-  "x < \<infinity> \<longleftrightarrow> (x \<noteq> \<infinity>)"
-  "-\<infinity> < x \<longleftrightarrow> (x \<noteq> -\<infinity>)"
-  by (cases x, simp_all) (cases x, simp_all)
-
-lemma extreal_infty_less_eq[simp]:
-  "\<infinity> \<le> x \<longleftrightarrow> x = \<infinity>"
-  "x \<le> -\<infinity> \<longleftrightarrow> x = -\<infinity>"
-  by (auto simp add: less_eq_extreal_def)
-
-lemma extreal_less[simp]:
-  "extreal r < 0 \<longleftrightarrow> (r < 0)"
-  "0 < extreal r \<longleftrightarrow> (0 < r)"
-  "0 < \<infinity>"
-  "-\<infinity> < 0"
-  by (simp_all add: zero_extreal_def)
-
-lemma extreal_less_eq[simp]:
-  "x \<le> \<infinity>"
-  "-\<infinity> \<le> x"
-  "extreal r \<le> extreal p \<longleftrightarrow> r \<le> p"
-  "extreal r \<le> 0 \<longleftrightarrow> r \<le> 0"
-  "0 \<le> extreal r \<longleftrightarrow> 0 \<le> r"
-  by (auto simp add: less_eq_extreal_def zero_extreal_def)
-
-lemma extreal_infty_less_eq2:
-  "a \<le> b \<Longrightarrow> a = \<infinity> \<Longrightarrow> b = \<infinity>"
-  "a \<le> b \<Longrightarrow> b = -\<infinity> \<Longrightarrow> a = -\<infinity>"
-  by simp_all
-
-instance
-proof
-  fix x :: extreal show "x \<le> x"
-    by (cases x) simp_all
-  fix y :: extreal show "x < y \<longleftrightarrow> x \<le> y \<and> \<not> y \<le> x"
-    by (cases rule: extreal2_cases[of x y]) auto
-  show "x \<le> y \<or> y \<le> x "
-    by (cases rule: extreal2_cases[of x y]) auto
-  { assume "x \<le> y" "y \<le> x" then show "x = y"
-    by (cases rule: extreal2_cases[of x y]) auto }
-  { fix z assume "x \<le> y" "y \<le> z" then show "x \<le> z"
-    by (cases rule: extreal3_cases[of x y z]) auto }
-qed
-end
-
-instance extreal :: ordered_ab_semigroup_add
-proof
-  fix a b c :: extreal assume "a \<le> b" then show "c + a \<le> c + b"
-    by (cases rule: extreal3_cases[of a b c]) auto
-qed
-
-lemma real_of_extreal_positive_mono:
-  "\<lbrakk>0 \<le> x; x \<le> y; y \<noteq> \<infinity>\<rbrakk> \<Longrightarrow> real x \<le> real y"
-  by (cases rule: extreal2_cases[of x y]) auto
-
-lemma extreal_MInfty_lessI[intro, simp]:
-  "a \<noteq> -\<infinity> \<Longrightarrow> -\<infinity> < a"
-  by (cases a) auto
-
-lemma extreal_less_PInfty[intro, simp]:
-  "a \<noteq> \<infinity> \<Longrightarrow> a < \<infinity>"
-  by (cases a) auto
-
-lemma extreal_less_extreal_Ex:
-  fixes a b :: extreal
-  shows "x < extreal r \<longleftrightarrow> x = -\<infinity> \<or> (\<exists>p. p < r \<and> x = extreal p)"
-  by (cases x) auto
-
-lemma less_PInf_Ex_of_nat: "x \<noteq> \<infinity> \<longleftrightarrow> (\<exists>n::nat. x < extreal (real n))"
-proof (cases x)
-  case (real r) then show ?thesis
-    using reals_Archimedean2[of r] by simp
-qed simp_all
-
-lemma extreal_add_mono:
-  fixes a b c d :: extreal assumes "a \<le> b" "c \<le> d" shows "a + c \<le> b + d"
-  using assms
-  apply (cases a)
-  apply (cases rule: extreal3_cases[of b c d], auto)
-  apply (cases rule: extreal3_cases[of b c d], auto)
-  done
-
-lemma extreal_minus_le_minus[simp]:
-  fixes a b :: extreal shows "- a \<le> - b \<longleftrightarrow> b \<le> a"
-  by (cases rule: extreal2_cases[of a b]) auto
-
-lemma extreal_minus_less_minus[simp]:
-  fixes a b :: extreal shows "- a < - b \<longleftrightarrow> b < a"
-  by (cases rule: extreal2_cases[of a b]) auto
-
-lemma extreal_le_real_iff:
-  "x \<le> real y \<longleftrightarrow> ((\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> extreal x \<le> y) \<and> (\<bar>y\<bar> = \<infinity> \<longrightarrow> x \<le> 0))"
-  by (cases y) auto
-
-lemma real_le_extreal_iff:
-  "real y \<le> x \<longleftrightarrow> ((\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> y \<le> extreal x) \<and> (\<bar>y\<bar> = \<infinity> \<longrightarrow> 0 \<le> x))"
-  by (cases y) auto
-
-lemma extreal_less_real_iff:
-  "x < real y \<longleftrightarrow> ((\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> extreal x < y) \<and> (\<bar>y\<bar> = \<infinity> \<longrightarrow> x < 0))"
-  by (cases y) auto
-
-lemma real_less_extreal_iff:
-  "real y < x \<longleftrightarrow> ((\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> y < extreal x) \<and> (\<bar>y\<bar> = \<infinity> \<longrightarrow> 0 < x))"
-  by (cases y) auto
-
-lemma real_of_extreal_pos:
-  fixes x :: extreal shows "0 \<le> x \<Longrightarrow> 0 \<le> real x" by (cases x) auto
-
-lemmas real_of_extreal_ord_simps =
-  extreal_le_real_iff real_le_extreal_iff extreal_less_real_iff real_less_extreal_iff
-
-lemma abs_extreal_ge0[simp]: "0 \<le> x \<Longrightarrow> \<bar>x :: extreal\<bar> = x"
-  by (cases x) auto
-
-lemma abs_extreal_less0[simp]: "x < 0 \<Longrightarrow> \<bar>x :: extreal\<bar> = -x"
-  by (cases x) auto
-
-lemma abs_extreal_pos[simp]: "0 \<le> \<bar>x :: extreal\<bar>"
-  by (cases x) auto
-
-lemma real_of_extreal_le_0[simp]: "real (X :: extreal) \<le> 0 \<longleftrightarrow> (X \<le> 0 \<or> X = \<infinity>)"
-  by (cases X) auto
-
-lemma abs_real_of_extreal[simp]: "\<bar>real (X :: extreal)\<bar> = real \<bar>X\<bar>"
-  by (cases X) auto
-
-lemma zero_less_real_of_extreal: "0 < real X \<longleftrightarrow> (0 < X \<and> X \<noteq> \<infinity>)"
-  by (cases X) auto
-
-lemma extreal_0_le_uminus_iff[simp]:
-  fixes a :: extreal shows "0 \<le> -a \<longleftrightarrow> a \<le> 0"
-  by (cases rule: extreal2_cases[of a]) auto
-
-lemma extreal_uminus_le_0_iff[simp]:
-  fixes a :: extreal shows "-a \<le> 0 \<longleftrightarrow> 0 \<le> a"
-  by (cases rule: extreal2_cases[of a]) auto
-
-lemma extreal_dense:
-  fixes x y :: extreal assumes "x < y"
-  shows "EX z. x < z & z < y"
-proof -
-{ assume a: "x = (-\<infinity>)"
-  { assume "y = \<infinity>" hence ?thesis using a by (auto intro!: exI[of _ "0"]) }
-  moreover
-  { assume "y ~= \<infinity>"
-    with `x < y` obtain r where r: "y = extreal r" by (cases y) auto
-    hence ?thesis using `x < y` a by (auto intro!: exI[of _ "extreal (r - 1)"])
-  } ultimately have ?thesis by auto
-}
-moreover
-{ assume "x ~= (-\<infinity>)"
-  with `x < y` obtain p where p: "x = extreal p" by (cases x) auto
-  { assume "y = \<infinity>" hence ?thesis using `x < y` p
-       by (auto intro!: exI[of _ "extreal (p + 1)"]) }
-  moreover
-  { assume "y ~= \<infinity>"
-    with `x < y` obtain r where r: "y = extreal r" by (cases y) auto
-    with p `x < y` have "p < r" by auto
-    with dense obtain z where "p < z" "z < r" by auto
-    hence ?thesis using r p by (auto intro!: exI[of _ "extreal z"])
-  } ultimately have ?thesis by auto
-} ultimately show ?thesis by auto
-qed
-
-lemma extreal_dense2:
-  fixes x y :: extreal assumes "x < y"
-  shows "EX z. x < extreal z & extreal z < y"
-  by (metis extreal_dense[OF `x < y`] extreal_cases less_extreal.simps(2,3))
-
-lemma extreal_add_strict_mono:
-  fixes a b c d :: extreal
-  assumes "a = b" "0 \<le> a" "a \<noteq> \<infinity>" "c < d"
-  shows "a + c < b + d"
-  using assms by (cases rule: extreal3_cases[case_product extreal_cases, of a b c d]) auto
-
-lemma extreal_less_add: "\<bar>a\<bar> \<noteq> \<infinity> \<Longrightarrow> c < b \<Longrightarrow> a + c < a + b"
-  by (cases rule: extreal2_cases[of b c]) auto
-
-lemma extreal_uminus_eq_reorder: "- a = b \<longleftrightarrow> a = (-b::extreal)" by auto
-
-lemma extreal_uminus_less_reorder: "- a < b \<longleftrightarrow> -b < (a::extreal)"
-  by (subst (3) extreal_uminus_uminus[symmetric]) (simp only: extreal_minus_less_minus)
-
-lemma extreal_uminus_le_reorder: "- a \<le> b \<longleftrightarrow> -b \<le> (a::extreal)"
-  by (subst (3) extreal_uminus_uminus[symmetric]) (simp only: extreal_minus_le_minus)
-
-lemmas extreal_uminus_reorder =
-  extreal_uminus_eq_reorder extreal_uminus_less_reorder extreal_uminus_le_reorder
-
-lemma extreal_bot:
-  fixes x :: extreal assumes "\<And>B. x \<le> extreal B" shows "x = - \<infinity>"
-proof (cases x)
-  case (real r) with assms[of "r - 1"] show ?thesis by auto
-next case PInf with assms[of 0] show ?thesis by auto
-next case MInf then show ?thesis by simp
-qed
-
-lemma extreal_top:
-  fixes x :: extreal assumes "\<And>B. x \<ge> extreal B" shows "x = \<infinity>"
-proof (cases x)
-  case (real r) with assms[of "r + 1"] show ?thesis by auto
-next case MInf with assms[of 0] show ?thesis by auto
-next case PInf then show ?thesis by simp
-qed
-
-lemma
-  shows extreal_max[simp]: "extreal (max x y) = max (extreal x) (extreal y)"
-    and extreal_min[simp]: "extreal (min x y) = min (extreal x) (extreal y)"
-  by (simp_all add: min_def max_def)
-
-lemma extreal_max_0: "max 0 (extreal r) = extreal (max 0 r)"
-  by (auto simp: zero_extreal_def)
-
-lemma
-  fixes f :: "nat \<Rightarrow> extreal"
-  shows incseq_uminus[simp]: "incseq (\<lambda>x. - f x) \<longleftrightarrow> decseq f"
-  and decseq_uminus[simp]: "decseq (\<lambda>x. - f x) \<longleftrightarrow> incseq f"
-  unfolding decseq_def incseq_def by auto
-
-lemma incseq_extreal: "incseq f \<Longrightarrow> incseq (\<lambda>x. extreal (f x))"
-  unfolding incseq_def by auto
-
-lemma extreal_add_nonneg_nonneg:
-  fixes a b :: extreal shows "0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> 0 \<le> a + b"
-  using add_mono[of 0 a 0 b] by simp
-
-lemma image_eqD: "f ` A = B \<Longrightarrow> (\<forall>x\<in>A. f x \<in> B)"
-  by auto
-
-lemma incseq_setsumI:
-  fixes f :: "nat \<Rightarrow> 'a::{comm_monoid_add, ordered_ab_semigroup_add}"
-  assumes "\<And>i. 0 \<le> f i"
-  shows "incseq (\<lambda>i. setsum f {..< i})"
-proof (intro incseq_SucI)
-  fix n have "setsum f {..< n} + 0 \<le> setsum f {..<n} + f n"
-    using assms by (rule add_left_mono)
-  then show "setsum f {..< n} \<le> setsum f {..< Suc n}"
-    by auto
-qed
-
-lemma incseq_setsumI2:
-  fixes f :: "'i \<Rightarrow> nat \<Rightarrow> 'a::{comm_monoid_add, ordered_ab_semigroup_add}"
-  assumes "\<And>n. n \<in> A \<Longrightarrow> incseq (f n)"
-  shows "incseq (\<lambda>i. \<Sum>n\<in>A. f n i)"
-  using assms unfolding incseq_def by (auto intro: setsum_mono)
-
-subsubsection "Multiplication"
-
-instantiation extreal :: "{comm_monoid_mult, sgn}"
-begin
-
-definition "1 = extreal 1"
-
-function sgn_extreal where
-  "sgn (extreal r) = extreal (sgn r)"
-| "sgn \<infinity> = 1"
-| "sgn (-\<infinity>) = -1"
-by (auto intro: extreal_cases)
-termination proof qed (rule wf_empty)
-
-function times_extreal where
-"extreal r * extreal p = extreal (r * p)" |
-"extreal r * \<infinity> = (if r = 0 then 0 else if r > 0 then \<infinity> else -\<infinity>)" |
-"\<infinity> * extreal r = (if r = 0 then 0 else if r > 0 then \<infinity> else -\<infinity>)" |
-"extreal r * -\<infinity> = (if r = 0 then 0 else if r > 0 then -\<infinity> else \<infinity>)" |
-"-\<infinity> * extreal r = (if r = 0 then 0 else if r > 0 then -\<infinity> else \<infinity>)" |
-"\<infinity> * \<infinity> = \<infinity>" |
-"-\<infinity> * \<infinity> = -\<infinity>" |
-"\<infinity> * -\<infinity> = -\<infinity>" |
-"-\<infinity> * -\<infinity> = \<infinity>"
-proof -
-  case (goal1 P x)
-  moreover then obtain a b where "x = (a, b)" by (cases x) auto
-  ultimately show P by (cases rule: extreal2_cases[of a b]) auto
-qed simp_all
-termination by (relation "{}") simp
-
-instance
-proof
-  fix a :: extreal show "1 * a = a"
-    by (cases a) (simp_all add: one_extreal_def)
-  fix b :: extreal show "a * b = b * a"
-    by (cases rule: extreal2_cases[of a b]) simp_all
-  fix c :: extreal show "a * b * c = a * (b * c)"
-    by (cases rule: extreal3_cases[of a b c])
-       (simp_all add: zero_extreal_def zero_less_mult_iff)
-qed
-end
-
-lemma real_of_extreal_le_1:
-  fixes a :: extreal shows "a \<le> 1 \<Longrightarrow> real a \<le> 1"
-  by (cases a) (auto simp: one_extreal_def)
-
-lemma abs_extreal_one[simp]: "\<bar>1\<bar> = (1::extreal)"
-  unfolding one_extreal_def by simp
-
-lemma extreal_mult_zero[simp]:
-  fixes a :: extreal shows "a * 0 = 0"
-  by (cases a) (simp_all add: zero_extreal_def)
-
-lemma extreal_zero_mult[simp]:
-  fixes a :: extreal shows "0 * a = 0"
-  by (cases a) (simp_all add: zero_extreal_def)
-
-lemma extreal_m1_less_0[simp]:
-  "-(1::extreal) < 0"
-  by (simp add: zero_extreal_def one_extreal_def)
-
-lemma extreal_zero_m1[simp]:
-  "1 \<noteq> (0::extreal)"
-  by (simp add: zero_extreal_def one_extreal_def)
-
-lemma extreal_times_0[simp]:
-  fixes x :: extreal shows "0 * x = 0"
-  by (cases x) (auto simp: zero_extreal_def)
-
-lemma extreal_times[simp]:
-  "1 \<noteq> \<infinity>" "\<infinity> \<noteq> 1"
-  "1 \<noteq> -\<infinity>" "-\<infinity> \<noteq> 1"
-  by (auto simp add: times_extreal_def one_extreal_def)
-
-lemma extreal_plus_1[simp]:
-  "1 + extreal r = extreal (r + 1)" "extreal r + 1 = extreal (r + 1)"
-  "1 + -\<infinity> = -\<infinity>" "-\<infinity> + 1 = -\<infinity>"
-  unfolding one_extreal_def by auto
-
-lemma extreal_zero_times[simp]:
-  fixes a b :: extreal shows "a * b = 0 \<longleftrightarrow> a = 0 \<or> b = 0"
-  by (cases rule: extreal2_cases[of a b]) auto
-
-lemma extreal_mult_eq_PInfty[simp]:
-  shows "a * b = \<infinity> \<longleftrightarrow>
-    (a = \<infinity> \<and> b > 0) \<or> (a > 0 \<and> b = \<infinity>) \<or> (a = -\<infinity> \<and> b < 0) \<or> (a < 0 \<and> b = -\<infinity>)"
-  by (cases rule: extreal2_cases[of a b]) auto
-
-lemma extreal_mult_eq_MInfty[simp]:
-  shows "a * b = -\<infinity> \<longleftrightarrow>
-    (a = \<infinity> \<and> b < 0) \<or> (a < 0 \<and> b = \<infinity>) \<or> (a = -\<infinity> \<and> b > 0) \<or> (a > 0 \<and> b = -\<infinity>)"
-  by (cases rule: extreal2_cases[of a b]) auto
-
-lemma extreal_0_less_1[simp]: "0 < (1::extreal)"
-  by (simp_all add: zero_extreal_def one_extreal_def)
-
-lemma extreal_zero_one[simp]: "0 \<noteq> (1::extreal)"
-  by (simp_all add: zero_extreal_def one_extreal_def)
-
-lemma extreal_mult_minus_left[simp]:
-  fixes a b :: extreal shows "-a * b = - (a * b)"
-  by (cases rule: extreal2_cases[of a b]) auto
-
-lemma extreal_mult_minus_right[simp]:
-  fixes a b :: extreal shows "a * -b = - (a * b)"
-  by (cases rule: extreal2_cases[of a b]) auto
-
-lemma extreal_mult_infty[simp]:
-  "a * \<infinity> = (if a = 0 then 0 else if 0 < a then \<infinity> else - \<infinity>)"
-  by (cases a) auto
-
-lemma extreal_infty_mult[simp]:
-  "\<infinity> * a = (if a = 0 then 0 else if 0 < a then \<infinity> else - \<infinity>)"
-  by (cases a) auto
-
-lemma extreal_mult_strict_right_mono:
-  assumes "a < b" and "0 < c" "c < \<infinity>"
-  shows "a * c < b * c"
-  using assms
-  by (cases rule: extreal3_cases[of a b c])
-     (auto simp: zero_le_mult_iff extreal_less_PInfty)
-
-lemma extreal_mult_strict_left_mono:
-  "\<lbrakk> a < b ; 0 < c ; c < \<infinity>\<rbrakk> \<Longrightarrow> c * a < c * b"
-  using extreal_mult_strict_right_mono by (simp add: mult_commute[of c])
-
-lemma extreal_mult_right_mono:
-  fixes a b c :: extreal shows "\<lbrakk>a \<le> b; 0 \<le> c\<rbrakk> \<Longrightarrow> a*c \<le> b*c"
-  using assms
-  apply (cases "c = 0") apply simp
-  by (cases rule: extreal3_cases[of a b c])
-     (auto simp: zero_le_mult_iff extreal_less_PInfty)
-
-lemma extreal_mult_left_mono:
-  fixes a b c :: extreal shows "\<lbrakk>a \<le> b; 0 \<le> c\<rbrakk> \<Longrightarrow> c * a \<le> c * b"
-  using extreal_mult_right_mono by (simp add: mult_commute[of c])
-
-lemma zero_less_one_extreal[simp]: "0 \<le> (1::extreal)"
-  by (simp add: one_extreal_def zero_extreal_def)
-
-lemma extreal_0_le_mult[simp]: "0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> 0 \<le> a * (b :: extreal)"
-  by (cases rule: extreal2_cases[of a b]) (auto simp: mult_nonneg_nonneg)
-
-lemma extreal_right_distrib:
-  fixes r a b :: extreal shows "0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> r * (a + b) = r * a + r * b"
-  by (cases rule: extreal3_cases[of r a b]) (simp_all add: field_simps)
-
-lemma extreal_left_distrib:
-  fixes r a b :: extreal shows "0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> (a + b) * r = a * r + b * r"
-  by (cases rule: extreal3_cases[of r a b]) (simp_all add: field_simps)
-
-lemma extreal_mult_le_0_iff:
-  fixes a b :: extreal
-  shows "a * b \<le> 0 \<longleftrightarrow> (0 \<le> a \<and> b \<le> 0) \<or> (a \<le> 0 \<and> 0 \<le> b)"
-  by (cases rule: extreal2_cases[of a b]) (simp_all add: mult_le_0_iff)
-
-lemma extreal_zero_le_0_iff:
-  fixes a b :: extreal
-  shows "0 \<le> a * b \<longleftrightarrow> (0 \<le> a \<and> 0 \<le> b) \<or> (a \<le> 0 \<and> b \<le> 0)"
-  by (cases rule: extreal2_cases[of a b]) (simp_all add: zero_le_mult_iff)
-
-lemma extreal_mult_less_0_iff:
-  fixes a b :: extreal
-  shows "a * b < 0 \<longleftrightarrow> (0 < a \<and> b < 0) \<or> (a < 0 \<and> 0 < b)"
-  by (cases rule: extreal2_cases[of a b]) (simp_all add: mult_less_0_iff)
-
-lemma extreal_zero_less_0_iff:
-  fixes a b :: extreal
-  shows "0 < a * b \<longleftrightarrow> (0 < a \<and> 0 < b) \<or> (a < 0 \<and> b < 0)"
-  by (cases rule: extreal2_cases[of a b]) (simp_all add: zero_less_mult_iff)
-
-lemma extreal_distrib:
-  fixes a b c :: extreal
-  assumes "a \<noteq> \<infinity> \<or> b \<noteq> -\<infinity>" "a \<noteq> -\<infinity> \<or> b \<noteq> \<infinity>" "\<bar>c\<bar> \<noteq> \<infinity>"
-  shows "(a + b) * c = a * c + b * c"
-  using assms
-  by (cases rule: extreal3_cases[of a b c]) (simp_all add: field_simps)
-
-lemma extreal_le_epsilon:
-  fixes x y :: extreal
-  assumes "ALL e. 0 < e --> x <= y + e"
-  shows "x <= y"
-proof-
-{ assume a: "EX r. y = extreal r"
-  from this obtain r where r_def: "y = extreal r" by auto
-  { assume "x=(-\<infinity>)" hence ?thesis by auto }
-  moreover
-  { assume "~(x=(-\<infinity>))"
-    from this obtain p where p_def: "x = extreal p"
-    using a assms[rule_format, of 1] by (cases x) auto
-    { fix e have "0 < e --> p <= r + e"
-      using assms[rule_format, of "extreal e"] p_def r_def by auto }
-    hence "p <= r" apply (subst field_le_epsilon) by auto
-    hence ?thesis using r_def p_def by auto
-  } ultimately have ?thesis by blast
-}
-moreover
-{ assume "y=(-\<infinity>) | y=\<infinity>" hence ?thesis
-    using assms[rule_format, of 1] by (cases x) auto
-} ultimately show ?thesis by (cases y) auto
-qed
-
-
-lemma extreal_le_epsilon2:
-  fixes x y :: extreal
-  assumes "ALL e. 0 < e --> x <= y + extreal e"
-  shows "x <= y"
-proof-
-{ fix e :: extreal assume "e>0"
-  { assume "e=\<infinity>" hence "x<=y+e" by auto }
-  moreover
-  { assume "e~=\<infinity>"
-    from this obtain r where "e = extreal r" using `e>0` apply (cases e) by auto
-    hence "x<=y+e" using assms[rule_format, of r] `e>0` by auto
-  } ultimately have "x<=y+e" by blast
-} from this show ?thesis using extreal_le_epsilon by auto
-qed
-
-lemma extreal_le_real:
-  fixes x y :: extreal
-  assumes "ALL z. x <= extreal z --> y <= extreal z"
-  shows "y <= x"
-by (metis assms extreal.exhaust extreal_bot extreal_less_eq(1)
-          extreal_less_eq(2) order_refl uminus_extreal.simps(2))
-
-lemma extreal_le_extreal:
-  fixes x y :: extreal
-  assumes "\<And>B. B < x \<Longrightarrow> B <= y"
-  shows "x <= y"
-by (metis assms extreal_dense leD linorder_le_less_linear)
-
-lemma extreal_ge_extreal:
-  fixes x y :: extreal
-  assumes "ALL B. B>x --> B >= y"
-  shows "x >= y"
-by (metis assms extreal_dense leD linorder_le_less_linear)
-
-lemma setprod_extreal_0:
-  fixes f :: "'a \<Rightarrow> extreal"
-  shows "(\<Prod>i\<in>A. f i) = 0 \<longleftrightarrow> (finite A \<and> (\<exists>i\<in>A. f i = 0))"
-proof cases
-  assume "finite A"
-  then show ?thesis by (induct A) auto
-qed auto
-
-lemma setprod_extreal_pos:
-  fixes f :: "'a \<Rightarrow> extreal" assumes pos: "\<And>i. i \<in> I \<Longrightarrow> 0 \<le> f i" shows "0 \<le> (\<Prod>i\<in>I. f i)"
-proof cases
-  assume "finite I" from this pos show ?thesis by induct auto
-qed simp
-
-lemma setprod_PInf:
-  assumes "\<And>i. i \<in> I \<Longrightarrow> 0 \<le> f i"
-  shows "(\<Prod>i\<in>I. f i) = \<infinity> \<longleftrightarrow> finite I \<and> (\<exists>i\<in>I. f i = \<infinity>) \<and> (\<forall>i\<in>I. f i \<noteq> 0)"
-proof cases
-  assume "finite I" from this assms show ?thesis
-  proof (induct I)
-    case (insert i I)
-    then have pos: "0 \<le> f i" "0 \<le> setprod f I" by (auto intro!: setprod_extreal_pos)
-    from insert have "(\<Prod>j\<in>insert i I. f j) = \<infinity> \<longleftrightarrow> setprod f I * f i = \<infinity>" by auto
-    also have "\<dots> \<longleftrightarrow> (setprod f I = \<infinity> \<or> f i = \<infinity>) \<and> f i \<noteq> 0 \<and> setprod f I \<noteq> 0"
-      using setprod_extreal_pos[of I f] pos
-      by (cases rule: extreal2_cases[of "f i" "setprod f I"]) auto
-    also have "\<dots> \<longleftrightarrow> finite (insert i I) \<and> (\<exists>j\<in>insert i I. f j = \<infinity>) \<and> (\<forall>j\<in>insert i I. f j \<noteq> 0)"
-      using insert by (auto simp: setprod_extreal_0)
-    finally show ?case .
-  qed simp
-qed simp
-
-lemma setprod_extreal: "(\<Prod>i\<in>A. extreal (f i)) = extreal (setprod f A)"
-proof cases
-  assume "finite A" then show ?thesis
-    by induct (auto simp: one_extreal_def)
-qed (simp add: one_extreal_def)
-
-subsubsection {* Power *}
-
-instantiation extreal :: power
-begin
-primrec power_extreal where
-  "power_extreal x 0 = 1" |
-  "power_extreal x (Suc n) = x * x ^ n"
-instance ..
-end
-
-lemma extreal_power[simp]: "(extreal x) ^ n = extreal (x^n)"
-  by (induct n) (auto simp: one_extreal_def)
-
-lemma extreal_power_PInf[simp]: "\<infinity> ^ n = (if n = 0 then 1 else \<infinity>)"
-  by (induct n) (auto simp: one_extreal_def)
-
-lemma extreal_power_uminus[simp]:
-  fixes x :: extreal
-  shows "(- x) ^ n = (if even n then x ^ n else - (x^n))"
-  by (induct n) (auto simp: one_extreal_def)
-
-lemma extreal_power_number_of[simp]:
-  "(number_of num :: extreal) ^ n = extreal (number_of num ^ n)"
-  by (induct n) (auto simp: one_extreal_def)
-
-lemma zero_le_power_extreal[simp]:
-  fixes a :: extreal assumes "0 \<le> a"
-  shows "0 \<le> a ^ n"
-  using assms by (induct n) (auto simp: extreal_zero_le_0_iff)
-
-subsubsection {* Subtraction *}
-
-lemma extreal_minus_minus_image[simp]:
-  fixes S :: "extreal set"
-  shows "uminus ` uminus ` S = S"
-  by (auto simp: image_iff)
-
-lemma extreal_uminus_lessThan[simp]:
-  fixes a :: extreal shows "uminus ` {..<a} = {-a<..}"
-proof (safe intro!: image_eqI)
-  fix x assume "-a < x"
-  then have "- x < - (- a)" by (simp del: extreal_uminus_uminus)
-  then show "- x < a" by simp
-qed auto
-
-lemma extreal_uminus_greaterThan[simp]:
-  "uminus ` {(a::extreal)<..} = {..<-a}"
-  by (metis extreal_uminus_lessThan extreal_uminus_uminus
-            extreal_minus_minus_image)
-
-instantiation extreal :: minus
-begin
-definition "x - y = x + -(y::extreal)"
-instance ..
-end
-
-lemma extreal_minus[simp]:
-  "extreal r - extreal p = extreal (r - p)"
-  "-\<infinity> - extreal r = -\<infinity>"
-  "extreal r - \<infinity> = -\<infinity>"
-  "\<infinity> - x = \<infinity>"
-  "-\<infinity> - \<infinity> = -\<infinity>"
-  "x - -y = x + y"
-  "x - 0 = x"
-  "0 - x = -x"
-  by (simp_all add: minus_extreal_def)
-
-lemma extreal_x_minus_x[simp]:
-  "x - x = (if \<bar>x\<bar> = \<infinity> then \<infinity> else 0)"
-  by (cases x) simp_all
-
-lemma extreal_eq_minus_iff:
-  fixes x y z :: extreal
-  shows "x = z - y \<longleftrightarrow>
-    (\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> x + y = z) \<and>
-    (y = -\<infinity> \<longrightarrow> x = \<infinity>) \<and>
-    (y = \<infinity> \<longrightarrow> z = \<infinity> \<longrightarrow> x = \<infinity>) \<and>
-    (y = \<infinity> \<longrightarrow> z \<noteq> \<infinity> \<longrightarrow> x = -\<infinity>)"
-  by (cases rule: extreal3_cases[of x y z]) auto
-
-lemma extreal_eq_minus:
-  fixes x y z :: extreal
-  shows "\<bar>y\<bar> \<noteq> \<infinity> \<Longrightarrow> x = z - y \<longleftrightarrow> x + y = z"
-  by (auto simp: extreal_eq_minus_iff)
-
-lemma extreal_less_minus_iff:
-  fixes x y z :: extreal
-  shows "x < z - y \<longleftrightarrow>
-    (y = \<infinity> \<longrightarrow> z = \<infinity> \<and> x \<noteq> \<infinity>) \<and>
-    (y = -\<infinity> \<longrightarrow> x \<noteq> \<infinity>) \<and>
-    (\<bar>y\<bar> \<noteq> \<infinity>\<longrightarrow> x + y < z)"
-  by (cases rule: extreal3_cases[of x y z]) auto
-
-lemma extreal_less_minus:
-  fixes x y z :: extreal
-  shows "\<bar>y\<bar> \<noteq> \<infinity> \<Longrightarrow> x < z - y \<longleftrightarrow> x + y < z"
-  by (auto simp: extreal_less_minus_iff)
-
-lemma extreal_le_minus_iff:
-  fixes x y z :: extreal
-  shows "x \<le> z - y \<longleftrightarrow>
-    (y = \<infinity> \<longrightarrow> z \<noteq> \<infinity> \<longrightarrow> x = -\<infinity>) \<and>
-    (\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> x + y \<le> z)"
-  by (cases rule: extreal3_cases[of x y z]) auto
-
-lemma extreal_le_minus:
-  fixes x y z :: extreal
-  shows "\<bar>y\<bar> \<noteq> \<infinity> \<Longrightarrow> x \<le> z - y \<longleftrightarrow> x + y \<le> z"
-  by (auto simp: extreal_le_minus_iff)
-
-lemma extreal_minus_less_iff:
-  fixes x y z :: extreal
-  shows "x - y < z \<longleftrightarrow>
-    y \<noteq> -\<infinity> \<and> (y = \<infinity> \<longrightarrow> x \<noteq> \<infinity> \<and> z \<noteq> -\<infinity>) \<and>
-    (y \<noteq> \<infinity> \<longrightarrow> x < z + y)"
-  by (cases rule: extreal3_cases[of x y z]) auto
-
-lemma extreal_minus_less:
-  fixes x y z :: extreal
-  shows "\<bar>y\<bar> \<noteq> \<infinity> \<Longrightarrow> x - y < z \<longleftrightarrow> x < z + y"
-  by (auto simp: extreal_minus_less_iff)
-
-lemma extreal_minus_le_iff:
-  fixes x y z :: extreal
-  shows "x - y \<le> z \<longleftrightarrow>
-    (y = -\<infinity> \<longrightarrow> z = \<infinity>) \<and>
-    (y = \<infinity> \<longrightarrow> x = \<infinity> \<longrightarrow> z = \<infinity>) \<and>
-    (\<bar>y\<bar> \<noteq> \<infinity> \<longrightarrow> x \<le> z + y)"
-  by (cases rule: extreal3_cases[of x y z]) auto
-
-lemma extreal_minus_le:
-  fixes x y z :: extreal
-  shows "\<bar>y\<bar> \<noteq> \<infinity> \<Longrightarrow> x - y \<le> z \<longleftrightarrow> x \<le> z + y"
-  by (auto simp: extreal_minus_le_iff)
-
-lemma extreal_minus_eq_minus_iff:
-  fixes a b c :: extreal
-  shows "a - b = a - c \<longleftrightarrow>
-    b = c \<or> a = \<infinity> \<or> (a = -\<infinity> \<and> b \<noteq> -\<infinity> \<and> c \<noteq> -\<infinity>)"
-  by (cases rule: extreal3_cases[of a b c]) auto
-
-lemma extreal_add_le_add_iff:
-  "c + a \<le> c + b \<longleftrightarrow>
-    a \<le> b \<or> c = \<infinity> \<or> (c = -\<infinity> \<and> a \<noteq> \<infinity> \<and> b \<noteq> \<infinity>)"
-  by (cases rule: extreal3_cases[of a b c]) (simp_all add: field_simps)
-
-lemma extreal_mult_le_mult_iff:
-  "\<bar>c\<bar> \<noteq> \<infinity> \<Longrightarrow> c * a \<le> c * b \<longleftrightarrow> (0 < c \<longrightarrow> a \<le> b) \<and> (c < 0 \<longrightarrow> b \<le> a)"
-  by (cases rule: extreal3_cases[of a b c]) (simp_all add: mult_le_cancel_left)
-
-lemma extreal_minus_mono:
-  fixes A B C D :: extreal assumes "A \<le> B" "D \<le> C"
-  shows "A - C \<le> B - D"
-  using assms
-  by (cases rule: extreal3_cases[case_product extreal_cases, of A B C D]) simp_all
-
-lemma real_of_extreal_minus:
-  "real (a - b) = (if \<bar>a\<bar> = \<infinity> \<or> \<bar>b\<bar> = \<infinity> then 0 else real a - real b)"
-  by (cases rule: extreal2_cases[of a b]) auto
-
-lemma extreal_diff_positive:
-  fixes a b :: extreal shows "a \<le> b \<Longrightarrow> 0 \<le> b - a"
-  by (cases rule: extreal2_cases[of a b]) auto
-
-lemma extreal_between:
-  fixes x e :: extreal
-  assumes "\<bar>x\<bar> \<noteq> \<infinity>" "0 < e"
-  shows "x - e < x" "x < x + e"
-using assms apply (cases x, cases e) apply auto
-using assms by (cases x, cases e) auto
-
-subsubsection {* Division *}
-
-instantiation extreal :: inverse
-begin
-
-function inverse_extreal where
-"inverse (extreal r) = (if r = 0 then \<infinity> else extreal (inverse r))" |
-"inverse \<infinity> = 0" |
-"inverse (-\<infinity>) = 0"
-  by (auto intro: extreal_cases)
-termination by (relation "{}") simp
-
-definition "x / y = x * inverse (y :: extreal)"
-
-instance proof qed
-end
-
-lemma real_of_extreal_inverse[simp]:
-  fixes a :: extreal
-  shows "real (inverse a) = 1 / real a"
-  by (cases a) (auto simp: inverse_eq_divide)
-
-lemma extreal_inverse[simp]:
-  "inverse 0 = \<infinity>"
-  "inverse (1::extreal) = 1"
-  by (simp_all add: one_extreal_def zero_extreal_def)
-
-lemma extreal_divide[simp]:
-  "extreal r / extreal p = (if p = 0 then extreal r * \<infinity> else extreal (r / p))"
-  unfolding divide_extreal_def by (auto simp: divide_real_def)
-
-lemma extreal_divide_same[simp]:
-  "x / x = (if \<bar>x\<bar> = \<infinity> \<or> x = 0 then 0 else 1)"
-  by (cases x)
-     (simp_all add: divide_real_def divide_extreal_def one_extreal_def)
-
-lemma extreal_inv_inv[simp]:
-  "inverse (inverse x) = (if x \<noteq> -\<infinity> then x else \<infinity>)"
-  by (cases x) auto
-
-lemma extreal_inverse_minus[simp]:
-  "inverse (- x) = (if x = 0 then \<infinity> else -inverse x)"
-  by (cases x) simp_all
-
-lemma extreal_uminus_divide[simp]:
-  fixes x y :: extreal shows "- x / y = - (x / y)"
-  unfolding divide_extreal_def by simp
-
-lemma extreal_divide_Infty[simp]:
-  "x / \<infinity> = 0" "x / -\<infinity> = 0"
-  unfolding divide_extreal_def by simp_all
-
-lemma extreal_divide_one[simp]:
-  "x / 1 = (x::extreal)"
-  unfolding divide_extreal_def by simp
-
-lemma extreal_divide_extreal[simp]:
-  "\<infinity> / extreal r = (if 0 \<le> r then \<infinity> else -\<infinity>)"
-  unfolding divide_extreal_def by simp
-
-lemma zero_le_divide_extreal[simp]:
-  fixes a :: extreal assumes "0 \<le> a" "0 \<le> b"
-  shows "0 \<le> a / b"
-  using assms by (cases rule: extreal2_cases[of a b]) (auto simp: zero_le_divide_iff)
-
-lemma extreal_le_divide_pos:
-  "x > 0 \<Longrightarrow> x \<noteq> \<infinity> \<Longrightarrow> y \<le> z / x \<longleftrightarrow> x * y \<le> z"
-  by (cases rule: extreal3_cases[of x y z]) (auto simp: field_simps)
-
-lemma extreal_divide_le_pos:
-  "x > 0 \<Longrightarrow> x \<noteq> \<infinity> \<Longrightarrow> z / x \<le> y \<longleftrightarrow> z \<le> x * y"
-  by (cases rule: extreal3_cases[of x y z]) (auto simp: field_simps)
-
-lemma extreal_le_divide_neg:
-  "x < 0 \<Longrightarrow> x \<noteq> -\<infinity> \<Longrightarrow> y \<le> z / x \<longleftrightarrow> z \<le> x * y"
-  by (cases rule: extreal3_cases[of x y z]) (auto simp: field_simps)
-
-lemma extreal_divide_le_neg:
-  "x < 0 \<Longrightarrow> x \<noteq> -\<infinity> \<Longrightarrow> z / x \<le> y \<longleftrightarrow> x * y \<le> z"
-  by (cases rule: extreal3_cases[of x y z]) (auto simp: field_simps)
-
-lemma extreal_inverse_antimono_strict:
-  fixes x y :: extreal
-  shows "0 \<le> x \<Longrightarrow> x < y \<Longrightarrow> inverse y < inverse x"
-  by (cases rule: extreal2_cases[of x y]) auto
-
-lemma extreal_inverse_antimono:
-  fixes x y :: extreal
-  shows "0 \<le> x \<Longrightarrow> x <= y \<Longrightarrow> inverse y <= inverse x"
-  by (cases rule: extreal2_cases[of x y]) auto
-
-lemma inverse_inverse_Pinfty_iff[simp]:
-  "inverse x = \<infinity> \<longleftrightarrow> x = 0"
-  by (cases x) auto
-
-lemma extreal_inverse_eq_0:
-  "inverse x = 0 \<longleftrightarrow> x = \<infinity> \<or> x = -\<infinity>"
-  by (cases x) auto
-
-lemma extreal_0_gt_inverse:
-  fixes x :: extreal shows "0 < inverse x \<longleftrightarrow> x \<noteq> \<infinity> \<and> 0 \<le> x"
-  by (cases x) auto
-
-lemma extreal_mult_less_right:
-  assumes "b * a < c * a" "0 < a" "a < \<infinity>"
-  shows "b < c"
-  using assms
-  by (cases rule: extreal3_cases[of a b c])
-     (auto split: split_if_asm simp: zero_less_mult_iff zero_le_mult_iff)
-
-lemma extreal_power_divide:
-  "y \<noteq> 0 \<Longrightarrow> (x / y :: extreal) ^ n = x^n / y^n"
-  by (cases rule: extreal2_cases[of x y])
-     (auto simp: one_extreal_def zero_extreal_def power_divide not_le
-                 power_less_zero_eq zero_le_power_iff)
-
-lemma extreal_le_mult_one_interval:
-  fixes x y :: extreal
-  assumes y: "y \<noteq> -\<infinity>"
-  assumes z: "\<And>z. \<lbrakk> 0 < z ; z < 1 \<rbrakk> \<Longrightarrow> z * x \<le> y"
-  shows "x \<le> y"
-proof (cases x)
-  case PInf with z[of "1 / 2"] show "x \<le> y" by (simp add: one_extreal_def)
-next
-  case (real r) note r = this
-  show "x \<le> y"
-  proof (cases y)
-    case (real p) note p = this
-    have "r \<le> p"
-    proof (rule field_le_mult_one_interval)
-      fix z :: real assume "0 < z" and "z < 1"
-      with z[of "extreal z"]
-      show "z * r \<le> p" using p r by (auto simp: zero_le_mult_iff one_extreal_def)
-    qed
-    then show "x \<le> y" using p r by simp
-  qed (insert y, simp_all)
-qed simp
-
-subsection "Complete lattice"
-
-instantiation extreal :: lattice
-begin
-definition [simp]: "sup x y = (max x y :: extreal)"
-definition [simp]: "inf x y = (min x y :: extreal)"
-instance proof qed simp_all
-end
-
-instantiation extreal :: complete_lattice
-begin
-
-definition "bot = -\<infinity>"
-definition "top = \<infinity>"
-
-definition "Sup S = (LEAST z. ALL x:S. x <= z :: extreal)"
-definition "Inf S = (GREATEST z. ALL x:S. z <= x :: extreal)"
-
-lemma extreal_complete_Sup:
-  fixes S :: "extreal set" assumes "S \<noteq> {}"
-  shows "\<exists>x. (\<forall>y\<in>S. y \<le> x) \<and> (\<forall>z. (\<forall>y\<in>S. y \<le> z) \<longrightarrow> x \<le> z)"
-proof cases
-  assume "\<exists>x. \<forall>a\<in>S. a \<le> extreal x"
-  then obtain y where y: "\<And>a. a\<in>S \<Longrightarrow> a \<le> extreal y" by auto
-  then have "\<infinity> \<notin> S" by force
-  show ?thesis
-  proof cases
-    assume "S = {-\<infinity>}"
-    then show ?thesis by (auto intro!: exI[of _ "-\<infinity>"])
-  next
-    assume "S \<noteq> {-\<infinity>}"
-    with `S \<noteq> {}` `\<infinity> \<notin> S` obtain x where "x \<in> S - {-\<infinity>}" "x \<noteq> \<infinity>" by auto
-    with y `\<infinity> \<notin> S` have "\<forall>z\<in>real ` (S - {-\<infinity>}). z \<le> y"
-      by (auto simp: real_of_extreal_ord_simps)
-    with reals_complete2[of "real ` (S - {-\<infinity>})"] `x \<in> S - {-\<infinity>}`
-    obtain s where s:
-       "\<forall>y\<in>S - {-\<infinity>}. real y \<le> s" "\<And>z. (\<forall>y\<in>S - {-\<infinity>}. real y \<le> z) \<Longrightarrow> s \<le> z"
-       by auto
-    show ?thesis
-    proof (safe intro!: exI[of _ "extreal s"])
-      fix z assume "z \<in> S" with `\<infinity> \<notin> S` show "z \<le> extreal s"
-      proof (cases z)
-        case (real r)
-        then show ?thesis
-          using s(1)[rule_format, of z] `z \<in> S` `z = extreal r` by auto
-      qed auto
-    next
-      fix z assume *: "\<forall>y\<in>S. y \<le> z"
-      with `S \<noteq> {-\<infinity>}` `S \<noteq> {}` show "extreal s \<le> z"
-      proof (cases z)
-        case (real u)
-        with * have "s \<le> u"
-          by (intro s(2)[of u]) (auto simp: real_of_extreal_ord_simps)
-        then show ?thesis using real by simp
-      qed auto
-    qed
-  qed
-next
-  assume *: "\<not> (\<exists>x. \<forall>a\<in>S. a \<le> extreal x)"
-  show ?thesis
-  proof (safe intro!: exI[of _ \<infinity>])
-    fix y assume **: "\<forall>z\<in>S. z \<le> y"
-    with * show "\<infinity> \<le> y"
-    proof (cases y)
-      case MInf with * ** show ?thesis by (force simp: not_le)
-    qed auto
-  qed simp
-qed
-
-lemma extreal_complete_Inf:
-  fixes S :: "extreal set" assumes "S ~= {}"
-  shows "EX x. (ALL y:S. x <= y) & (ALL z. (ALL y:S. z <= y) --> z <= x)"
-proof-
-def S1 == "uminus ` S"
-hence "S1 ~= {}" using assms by auto
-from this obtain x where x_def: "(ALL y:S1. y <= x) & (ALL z. (ALL y:S1. y <= z) --> x <= z)"
-   using extreal_complete_Sup[of S1] by auto
-{ fix z assume "ALL y:S. z <= y"
-  hence "ALL y:S1. y <= -z" unfolding S1_def by auto
-  hence "x <= -z" using x_def by auto
-  hence "z <= -x"
-    apply (subst extreal_uminus_uminus[symmetric])
-    unfolding extreal_minus_le_minus . }
-moreover have "(ALL y:S. -x <= y)"
-   using x_def unfolding S1_def
-   apply simp
-   apply (subst (3) extreal_uminus_uminus[symmetric])
-   unfolding extreal_minus_le_minus by simp
-ultimately show ?thesis by auto
-qed
-
-lemma extreal_complete_uminus_eq:
-  fixes S :: "extreal set"
-  shows "(\<forall>y\<in>uminus`S. y \<le> x) \<and> (\<forall>z. (\<forall>y\<in>uminus`S. y \<le> z) \<longrightarrow> x \<le> z)
-     \<longleftrightarrow> (\<forall>y\<in>S. -x \<le> y) \<and> (\<forall>z. (\<forall>y\<in>S. z \<le> y) \<longrightarrow> z \<le> -x)"
-  by simp (metis extreal_minus_le_minus extreal_uminus_uminus)
-
-lemma extreal_Sup_uminus_image_eq:
-  fixes S :: "extreal set"
-  shows "Sup (uminus ` S) = - Inf S"
-proof cases
-  assume "S = {}"
-  moreover have "(THE x. All (op \<le> x)) = (-\<infinity>::extreal)"
-    by (rule the_equality) (auto intro!: extreal_bot)
-  moreover have "(SOME x. \<forall>y. y \<le> x) = (\<infinity>::extreal)"
-    by (rule some_equality) (auto intro!: extreal_top)
-  ultimately show ?thesis unfolding Inf_extreal_def Sup_extreal_def
-    Least_def Greatest_def GreatestM_def by simp
-next
-  assume "S \<noteq> {}"
-  with extreal_complete_Sup[of "uminus`S"]
-  obtain x where x: "(\<forall>y\<in>S. -x \<le> y) \<and> (\<forall>z. (\<forall>y\<in>S. z \<le> y) \<longrightarrow> z \<le> -x)"
-    unfolding extreal_complete_uminus_eq by auto
-  show "Sup (uminus ` S) = - Inf S"
-    unfolding Inf_extreal_def Greatest_def GreatestM_def
-  proof (intro someI2[of _ _ "\<lambda>x. Sup (uminus`S) = - x"])
-    show "(\<forall>y\<in>S. -x \<le> y) \<and> (\<forall>y. (\<forall>z\<in>S. y \<le> z) \<longrightarrow> y \<le> -x)"
-      using x .
-    fix x' assume "(\<forall>y\<in>S. x' \<le> y) \<and> (\<forall>y. (\<forall>z\<in>S. y \<le> z) \<longrightarrow> y \<le> x')"
-    then have "(\<forall>y\<in>uminus`S. y \<le> - x') \<and> (\<forall>y. (\<forall>z\<in>uminus`S. z \<le> y) \<longrightarrow> - x' \<le> y)"
-      unfolding extreal_complete_uminus_eq by simp
-    then show "Sup (uminus ` S) = -x'"
-      unfolding Sup_extreal_def extreal_uminus_eq_iff
-      by (intro Least_equality) auto
-  qed
-qed
-
-instance
-proof
-  { fix x :: extreal and A
-    show "bot <= x" by (cases x) (simp_all add: bot_extreal_def)
-    show "x <= top" by (simp add: top_extreal_def) }
-
-  { fix x :: extreal and A assume "x : A"
-    with extreal_complete_Sup[of A]
-    obtain s where s: "\<forall>y\<in>A. y <= s" "\<forall>z. (\<forall>y\<in>A. y <= z) \<longrightarrow> s <= z" by auto
-    hence "x <= s" using `x : A` by auto
-    also have "... = Sup A" using s unfolding Sup_extreal_def
-      by (auto intro!: Least_equality[symmetric])
-    finally show "x <= Sup A" . }
-  note le_Sup = this
-
-  { fix x :: extreal and A assume *: "!!z. (z : A ==> z <= x)"
-    show "Sup A <= x"
-    proof (cases "A = {}")
-      case True
-      hence "Sup A = -\<infinity>" unfolding Sup_extreal_def
-        by (auto intro!: Least_equality)
-      thus "Sup A <= x" by simp
-    next
-      case False
-      with extreal_complete_Sup[of A]
-      obtain s where s: "\<forall>y\<in>A. y <= s" "\<forall>z. (\<forall>y\<in>A. y <= z) \<longrightarrow> s <= z" by auto
-      hence "Sup A = s"
-        unfolding Sup_extreal_def by (auto intro!: Least_equality)
-      also have "s <= x" using * s by auto
-      finally show "Sup A <= x" .
-    qed }
-  note Sup_le = this
-
-  { fix x :: extreal and A assume "x \<in> A"
-    with le_Sup[of "-x" "uminus`A"] show "Inf A \<le> x"
-      unfolding extreal_Sup_uminus_image_eq by simp }
-
-  { fix x :: extreal and A assume *: "!!z. (z : A ==> x <= z)"
-    with Sup_le[of "uminus`A" "-x"] show "x \<le> Inf A"
-      unfolding extreal_Sup_uminus_image_eq by force }
-qed
-end
-
-lemma extreal_SUPR_uminus:
-  fixes f :: "'a => extreal"
-  shows "(SUP i : R. -(f i)) = -(INF i : R. f i)"
-  unfolding SUPR_def INFI_def
-  using extreal_Sup_uminus_image_eq[of "f`R"]
-  by (simp add: image_image)
-
-lemma extreal_INFI_uminus:
-  fixes f :: "'a => extreal"
-  shows "(INF i : R. -(f i)) = -(SUP i : R. f i)"
-  using extreal_SUPR_uminus[of _ "\<lambda>x. - f x"] by simp
-
-lemma extreal_Inf_uminus_image_eq: "Inf (uminus ` S) = - Sup (S::extreal set)"
-  using extreal_Sup_uminus_image_eq[of "uminus ` S"] by (simp add: image_image)
-
-lemma extreal_inj_on_uminus[intro, simp]: "inj_on uminus (A :: extreal set)"
-  by (auto intro!: inj_onI)
-
-lemma extreal_image_uminus_shift:
-  fixes X Y :: "extreal set" shows "uminus ` X = Y \<longleftrightarrow> X = uminus ` Y"
-proof
-  assume "uminus ` X = Y"
-  then have "uminus ` uminus ` X = uminus ` Y"
-    by (simp add: inj_image_eq_iff)
-  then show "X = uminus ` Y" by (simp add: image_image)
-qed (simp add: image_image)
-
-lemma Inf_extreal_iff:
-  fixes z :: extreal
-  shows "(!!x. x:X ==> z <= x) ==> (EX x:X. x<y) <-> Inf X < y"
-  by (metis complete_lattice_class.Inf_greatest complete_lattice_class.Inf_lower less_le_not_le linear
-            order_less_le_trans)
-
-lemma Sup_eq_MInfty:
-  fixes S :: "extreal set" shows "Sup S = -\<infinity> \<longleftrightarrow> S = {} \<or> S = {-\<infinity>}"
-proof
-  assume a: "Sup S = -\<infinity>"
-  with complete_lattice_class.Sup_upper[of _ S]
-  show "S={} \<or> S={-\<infinity>}" by auto
-next
-  assume "S={} \<or> S={-\<infinity>}" then show "Sup S = -\<infinity>"
-    unfolding Sup_extreal_def by (auto intro!: Least_equality)
-qed
-
-lemma Inf_eq_PInfty:
-  fixes S :: "extreal set" shows "Inf S = \<infinity> \<longleftrightarrow> S = {} \<or> S = {\<infinity>}"
-  using Sup_eq_MInfty[of "uminus`S"]
-  unfolding extreal_Sup_uminus_image_eq extreal_image_uminus_shift by simp
-
-lemma Inf_eq_MInfty: "-\<infinity> : S ==> Inf S = -\<infinity>"
-  unfolding Inf_extreal_def
-  by (auto intro!: Greatest_equality)
-
-lemma Sup_eq_PInfty: "\<infinity> : S ==> Sup S = \<infinity>"
-  unfolding Sup_extreal_def
-  by (auto intro!: Least_equality)
-
-lemma extreal_SUPI:
-  fixes x :: extreal
-  assumes "!!i. i : A ==> f i <= x"
-  assumes "!!y. (!!i. i : A ==> f i <= y) ==> x <= y"
-  shows "(SUP i:A. f i) = x"
-  unfolding SUPR_def Sup_extreal_def
-  using assms by (auto intro!: Least_equality)
-
-lemma extreal_INFI:
-  fixes x :: extreal
-  assumes "!!i. i : A ==> f i >= x"
-  assumes "!!y. (!!i. i : A ==> f i >= y) ==> x >= y"
-  shows "(INF i:A. f i) = x"
-  unfolding INFI_def Inf_extreal_def
-  using assms by (auto intro!: Greatest_equality)
-
-lemma Sup_extreal_close:
-  fixes e :: extreal
-  assumes "0 < e" and S: "\<bar>Sup S\<bar> \<noteq> \<infinity>" "S \<noteq> {}"
-  shows "\<exists>x\<in>S. Sup S - e < x"
-  using assms by (cases e) (auto intro!: less_Sup_iff[THEN iffD1])
-
-lemma Inf_extreal_close:
-  fixes e :: extreal assumes "\<bar>Inf X\<bar> \<noteq> \<infinity>" "0 < e"
-  shows "\<exists>x\<in>X. x < Inf X + e"
-proof (rule Inf_less_iff[THEN iffD1])
-  show "Inf X < Inf X + e" using assms
-    by (cases e) auto
-qed
-
-lemma Sup_eq_top_iff:
-  fixes A :: "'a::{complete_lattice, linorder} set"
-  shows "Sup A = top \<longleftrightarrow> (\<forall>x<top. \<exists>i\<in>A. x < i)"
-proof
-  assume *: "Sup A = top"
-  show "(\<forall>x<top. \<exists>i\<in>A. x < i)" unfolding *[symmetric]
-  proof (intro allI impI)
-    fix x assume "x < Sup A" then show "\<exists>i\<in>A. x < i"
-      unfolding less_Sup_iff by auto
-  qed
-next
-  assume *: "\<forall>x<top. \<exists>i\<in>A. x < i"
-  show "Sup A = top"
-  proof (rule ccontr)
-    assume "Sup A \<noteq> top"
-    with top_greatest[of "Sup A"]
-    have "Sup A < top" unfolding le_less by auto
-    then have "Sup A < Sup A"
-      using * unfolding less_Sup_iff by auto
-    then show False by auto
-  qed
-qed
-
-lemma SUP_eq_top_iff:
-  fixes f :: "'a \<Rightarrow> 'b::{complete_lattice, linorder}"
-  shows "(SUP i:A. f i) = top \<longleftrightarrow> (\<forall>x<top. \<exists>i\<in>A. x < f i)"
-  unfolding SUPR_def Sup_eq_top_iff by auto
-
-lemma SUP_nat_Infty: "(SUP i::nat. extreal (real i)) = \<infinity>"
-proof -
-  { fix x assume "x \<noteq> \<infinity>"
-    then have "\<exists>k::nat. x < extreal (real k)"
-    proof (cases x)
-      case MInf then show ?thesis by (intro exI[of _ 0]) auto
-    next
-      case (real r)
-      moreover obtain k :: nat where "r < real k"
-        using ex_less_of_nat by (auto simp: real_eq_of_nat)
-      ultimately show ?thesis by auto
-    qed simp }
-  then show ?thesis
-    using SUP_eq_top_iff[of UNIV "\<lambda>n::nat. extreal (real n)"]
-    by (auto simp: top_extreal_def)
-qed
-
-lemma extreal_le_Sup:
-  fixes x :: extreal
-  shows "(x <= (SUP i:A. f i)) <-> (ALL y. y < x --> (EX i. i : A & y <= f i))"
-(is "?lhs <-> ?rhs")
-proof-
-{ assume "?rhs"
-  { assume "~(x <= (SUP i:A. f i))" hence "(SUP i:A. f i)<x" by (simp add: not_le)
-    from this obtain y where y_def: "(SUP i:A. f i)<y & y<x" using extreal_dense by auto
-    from this obtain i where "i : A & y <= f i" using `?rhs` by auto
-    hence "y <= (SUP i:A. f i)" using le_SUPI[of i A f] by auto
-    hence False using y_def by auto
-  } hence "?lhs" by auto
-}
-moreover
-{ assume "?lhs" hence "?rhs"
-  by (metis Collect_def Collect_mem_eq SUP_leI assms atLeastatMost_empty atLeastatMost_empty_iff
-      inf_sup_ord(4) linorder_le_cases sup_absorb1 xt1(8))
-} ultimately show ?thesis by auto
-qed
-
-lemma extreal_Inf_le:
-  fixes x :: extreal
-  shows "((INF i:A. f i) <= x) <-> (ALL y. x < y --> (EX i. i : A & f i <= y))"
-(is "?lhs <-> ?rhs")
-proof-
-{ assume "?rhs"
-  { assume "~((INF i:A. f i) <= x)" hence "x < (INF i:A. f i)" by (simp add: not_le)
-    from this obtain y where y_def: "x<y & y<(INF i:A. f i)" using extreal_dense by auto
-    from this obtain i where "i : A & f i <= y" using `?rhs` by auto
-    hence "(INF i:A. f i) <= y" using INF_leI[of i A f] by auto
-    hence False using y_def by auto
-  } hence "?lhs" by auto
-}
-moreover
-{ assume "?lhs" hence "?rhs"
-  by (metis Collect_def Collect_mem_eq le_INFI assms atLeastatMost_empty atLeastatMost_empty_iff
-      inf_sup_ord(4) linorder_le_cases sup_absorb1 xt1(8))
-} ultimately show ?thesis by auto
-qed
-
-lemma Inf_less:
-  fixes x :: extreal
-  assumes "(INF i:A. f i) < x"
-  shows "EX i. i : A & f i <= x"
-proof(rule ccontr)
-  assume "~ (EX i. i : A & f i <= x)"
-  hence "ALL i:A. f i > x" by auto
-  hence "(INF i:A. f i) >= x" apply (subst le_INFI) by auto
-  thus False using assms by auto
-qed
-
-lemma same_INF:
-  assumes "ALL e:A. f e = g e"
-  shows "(INF e:A. f e) = (INF e:A. g e)"
-proof-
-have "f ` A = g ` A" unfolding image_def using assms by auto
-thus ?thesis unfolding INFI_def by auto
-qed
-
-lemma same_SUP:
-  assumes "ALL e:A. f e = g e"
-  shows "(SUP e:A. f e) = (SUP e:A. g e)"
-proof-
-have "f ` A = g ` A" unfolding image_def using assms by auto
-thus ?thesis unfolding SUPR_def by auto
-qed
-
-lemma SUPR_eq:
-  assumes "\<forall>i\<in>A. \<exists>j\<in>B. f i \<le> g j"
-  assumes "\<forall>j\<in>B. \<exists>i\<in>A. g j \<le> f i"
-  shows "(SUP i:A. f i) = (SUP j:B. g j)"
-proof (intro antisym)
-  show "(SUP i:A. f i) \<le> (SUP j:B. g j)"
-    using assms by (metis SUP_leI le_SUPI2)
-  show "(SUP i:B. g i) \<le> (SUP j:A. f j)"
-    using assms by (metis SUP_leI le_SUPI2)
-qed
-
-lemma SUP_extreal_le_addI:
-  assumes "\<And>i. f i + y \<le> z" and "y \<noteq> -\<infinity>"
-  shows "SUPR UNIV f + y \<le> z"
-proof (cases y)
-  case (real r)
-  then have "\<And>i. f i \<le> z - y" using assms by (simp add: extreal_le_minus_iff)
-  then have "SUPR UNIV f \<le> z - y" by (rule SUP_leI)
-  then show ?thesis using real by (simp add: extreal_le_minus_iff)
-qed (insert assms, auto)
-
-lemma SUPR_extreal_add:
-  fixes f g :: "nat \<Rightarrow> extreal"
-  assumes "incseq f" "incseq g" and pos: "\<And>i. f i \<noteq> -\<infinity>" "\<And>i. g i \<noteq> -\<infinity>"
-  shows "(SUP i. f i + g i) = SUPR UNIV f + SUPR UNIV g"
-proof (rule extreal_SUPI)
-  fix y assume *: "\<And>i. i \<in> UNIV \<Longrightarrow> f i + g i \<le> y"
-  have f: "SUPR UNIV f \<noteq> -\<infinity>" using pos
-    unfolding SUPR_def Sup_eq_MInfty by (auto dest: image_eqD)
-  { fix j
-    { fix i
-      have "f i + g j \<le> f i + g (max i j)"
-        using `incseq g`[THEN incseqD] by (rule add_left_mono) auto
-      also have "\<dots> \<le> f (max i j) + g (max i j)"
-        using `incseq f`[THEN incseqD] by (rule add_right_mono) auto
-      also have "\<dots> \<le> y" using * by auto
-      finally have "f i + g j \<le> y" . }
-    then have "SUPR UNIV f + g j \<le> y"
-      using assms(4)[of j] by (intro SUP_extreal_le_addI) auto
-    then have "g j + SUPR UNIV f \<le> y" by (simp add: ac_simps) }
-  then have "SUPR UNIV g + SUPR UNIV f \<le> y"
-    using f by (rule SUP_extreal_le_addI)
-  then show "SUPR UNIV f + SUPR UNIV g \<le> y" by (simp add: ac_simps)
-qed (auto intro!: add_mono le_SUPI)
-
-lemma SUPR_extreal_add_pos:
-  fixes f g :: "nat \<Rightarrow> extreal"
-  assumes inc: "incseq f" "incseq g" and pos: "\<And>i. 0 \<le> f i" "\<And>i. 0 \<le> g i"
-  shows "(SUP i. f i + g i) = SUPR UNIV f + SUPR UNIV g"
-proof (intro SUPR_extreal_add inc)
-  fix i show "f i \<noteq> -\<infinity>" "g i \<noteq> -\<infinity>" using pos[of i] by auto
-qed
-
-lemma SUPR_extreal_setsum:
-  fixes f g :: "'a \<Rightarrow> nat \<Rightarrow> extreal"
-  assumes "\<And>n. n \<in> A \<Longrightarrow> incseq (f n)" and pos: "\<And>n i. n \<in> A \<Longrightarrow> 0 \<le> f n i"
-  shows "(SUP i. \<Sum>n\<in>A. f n i) = (\<Sum>n\<in>A. SUPR UNIV (f n))"
-proof cases
-  assume "finite A" then show ?thesis using assms
-    by induct (auto simp: incseq_setsumI2 setsum_nonneg SUPR_extreal_add_pos)
-qed simp
-
-lemma SUPR_extreal_cmult:
-  fixes f :: "nat \<Rightarrow> extreal" assumes "\<And>i. 0 \<le> f i" "0 \<le> c"
-  shows "(SUP i. c * f i) = c * SUPR UNIV f"
-proof (rule extreal_SUPI)
-  fix i have "f i \<le> SUPR UNIV f" by (rule le_SUPI) auto
-  then show "c * f i \<le> c * SUPR UNIV f"
-    using `0 \<le> c` by (rule extreal_mult_left_mono)
-next
-  fix y assume *: "\<And>i. i \<in> UNIV \<Longrightarrow> c * f i \<le> y"
-  show "c * SUPR UNIV f \<le> y"
-  proof cases
-    assume c: "0 < c \<and> c \<noteq> \<infinity>"
-    with * have "SUPR UNIV f \<le> y / c"
-      by (intro SUP_leI) (auto simp: extreal_le_divide_pos)
-    with c show ?thesis
-      by (auto simp: extreal_le_divide_pos)
-  next
-    { assume "c = \<infinity>" have ?thesis
-      proof cases
-        assume "\<forall>i. f i = 0"
-        moreover then have "range f = {0}" by auto
-        ultimately show "c * SUPR UNIV f \<le> y" using * by (auto simp: SUPR_def)
-      next
-        assume "\<not> (\<forall>i. f i = 0)"
-        then obtain i where "f i \<noteq> 0" by auto
-        with *[of i] `c = \<infinity>` `0 \<le> f i` show ?thesis by (auto split: split_if_asm)
-      qed }
-    moreover assume "\<not> (0 < c \<and> c \<noteq> \<infinity>)"
-    ultimately show ?thesis using * `0 \<le> c` by auto
-  qed
-qed
-
-lemma SUP_PInfty:
-  fixes f :: "'a \<Rightarrow> extreal"
-  assumes "\<And>n::nat. \<exists>i\<in>A. extreal (real n) \<le> f i"
-  shows "(SUP i:A. f i) = \<infinity>"
-  unfolding SUPR_def Sup_eq_top_iff[where 'a=extreal, unfolded top_extreal_def]
-  apply simp
-proof safe
-  fix x assume "x \<noteq> \<infinity>"
-  show "\<exists>i\<in>A. x < f i"
-  proof (cases x)
-    case PInf with `x \<noteq> \<infinity>` show ?thesis by simp
-  next
-    case MInf with assms[of "0"] show ?thesis by force
-  next
-    case (real r)
-    with less_PInf_Ex_of_nat[of x] obtain n :: nat where "x < extreal (real n)" by auto
-    moreover from assms[of n] guess i ..
-    ultimately show ?thesis
-      by (auto intro!: bexI[of _ i])
-  qed
-qed
-
-lemma Sup_countable_SUPR:
-  assumes "A \<noteq> {}"
-  shows "\<exists>f::nat \<Rightarrow> extreal. range f \<subseteq> A \<and> Sup A = SUPR UNIV f"
-proof (cases "Sup A")
-  case (real r)
-  have "\<forall>n::nat. \<exists>x. x \<in> A \<and> Sup A < x + 1 / extreal (real n)"
-  proof
-    fix n ::nat have "\<exists>x\<in>A. Sup A - 1 / extreal (real n) < x"
-      using assms real by (intro Sup_extreal_close) (auto simp: one_extreal_def)
-    then guess x ..
-    then show "\<exists>x. x \<in> A \<and> Sup A < x + 1 / extreal (real n)"
-      by (auto intro!: exI[of _ x] simp: extreal_minus_less_iff)
-  qed
-  from choice[OF this] guess f .. note f = this
-  have "SUPR UNIV f = Sup A"
-  proof (rule extreal_SUPI)
-    fix i show "f i \<le> Sup A" using f
-      by (auto intro!: complete_lattice_class.Sup_upper)
-  next
-    fix y assume bound: "\<And>i. i \<in> UNIV \<Longrightarrow> f i \<le> y"
-    show "Sup A \<le> y"
-    proof (rule extreal_le_epsilon, intro allI impI)
-      fix e :: extreal assume "0 < e"
-      show "Sup A \<le> y + e"
-      proof (cases e)
-        case (real r)
-        hence "0 < r" using `0 < e` by auto
-        then obtain n ::nat where *: "1 / real n < r" "0 < n"
-          using ex_inverse_of_nat_less by (auto simp: real_eq_of_nat inverse_eq_divide)
-        have "Sup A \<le> f n + 1 / extreal (real n)" using f[THEN spec, of n] by auto
-        also have "1 / extreal (real n) \<le> e" using real * by (auto simp: one_extreal_def )
-        with bound have "f n + 1 / extreal (real n) \<le> y + e" by (rule add_mono) simp
-        finally show "Sup A \<le> y + e" .
-      qed (insert `0 < e`, auto)
-    qed
-  qed
-  with f show ?thesis by (auto intro!: exI[of _ f])
-next
-  case PInf
-  from `A \<noteq> {}` obtain x where "x \<in> A" by auto
-  show ?thesis
-  proof cases
-    assume "\<infinity> \<in> A"
-    moreover then have "\<infinity> \<le> Sup A" by (intro complete_lattice_class.Sup_upper)
-    ultimately show ?thesis by (auto intro!: exI[of _ "\<lambda>x. \<infinity>"])
-  next
-    assume "\<infinity> \<notin> A"
-    have "\<exists>x\<in>A. 0 \<le> x"
-      by (metis Infty_neq_0 PInf complete_lattice_class.Sup_least extreal_infty_less_eq2 linorder_linear)
-    then obtain x where "x \<in> A" "0 \<le> x" by auto
-    have "\<forall>n::nat. \<exists>f. f \<in> A \<and> x + extreal (real n) \<le> f"
-    proof (rule ccontr)
-      assume "\<not> ?thesis"
-      then have "\<exists>n::nat. Sup A \<le> x + extreal (real n)"
-        by (simp add: Sup_le_iff not_le less_imp_le Ball_def) (metis less_imp_le)
-      then show False using `x \<in> A` `\<infinity> \<notin> A` PInf
-        by(cases x) auto
-    qed
-    from choice[OF this] guess f .. note f = this
-    have "SUPR UNIV f = \<infinity>"
-    proof (rule SUP_PInfty)
-      fix n :: nat show "\<exists>i\<in>UNIV. extreal (real n) \<le> f i"
-        using f[THEN spec, of n] `0 \<le> x`
-        by (cases rule: extreal2_cases[of "f n" x]) (auto intro!: exI[of _ n])
-    qed
-    then show ?thesis using f PInf by (auto intro!: exI[of _ f])
-  qed
-next
-  case MInf
-  with `A \<noteq> {}` have "A = {-\<infinity>}" by (auto simp: Sup_eq_MInfty)
-  then show ?thesis using MInf by (auto intro!: exI[of _ "\<lambda>x. -\<infinity>"])
-qed
-
-lemma SUPR_countable_SUPR:
-  "A \<noteq> {} \<Longrightarrow> \<exists>f::nat \<Rightarrow> extreal. range f \<subseteq> g`A \<and> SUPR A g = SUPR UNIV f"
-  using Sup_countable_SUPR[of "g`A"] by (auto simp: SUPR_def)
-
-
-lemma Sup_extreal_cadd:
-  fixes A :: "extreal set" assumes "A \<noteq> {}" and "a \<noteq> -\<infinity>"
-  shows "Sup ((\<lambda>x. a + x) ` A) = a + Sup A"
-proof (rule antisym)
-  have *: "\<And>a::extreal. \<And>A. Sup ((\<lambda>x. a + x) ` A) \<le> a + Sup A"
-    by (auto intro!: add_mono complete_lattice_class.Sup_least complete_lattice_class.Sup_upper)
-  then show "Sup ((\<lambda>x. a + x) ` A) \<le> a + Sup A" .
-  show "a + Sup A \<le> Sup ((\<lambda>x. a + x) ` A)"
-  proof (cases a)
-    case PInf with `A \<noteq> {}` show ?thesis by (auto simp: image_constant)
-  next
-    case (real r)
-    then have **: "op + (- a) ` op + a ` A = A"
-      by (auto simp: image_iff ac_simps zero_extreal_def[symmetric])
-    from *[of "-a" "(\<lambda>x. a + x) ` A"] real show ?thesis unfolding **
-      by (cases rule: extreal2_cases[of "Sup A" "Sup (op + a ` A)"]) auto
-  qed (insert `a \<noteq> -\<infinity>`, auto)
-qed
-
-lemma Sup_extreal_cminus:
-  fixes A :: "extreal set" assumes "A \<noteq> {}" and "a \<noteq> -\<infinity>"
-  shows "Sup ((\<lambda>x. a - x) ` A) = a - Inf A"
-  using Sup_extreal_cadd[of "uminus ` A" a] assms
-  by (simp add: comp_def image_image minus_extreal_def
-                 extreal_Sup_uminus_image_eq)
-
-lemma SUPR_extreal_cminus:
-  fixes A assumes "A \<noteq> {}" and "a \<noteq> -\<infinity>"
-  shows "(SUP x:A. a - f x) = a - (INF x:A. f x)"
-  using Sup_extreal_cminus[of "f`A" a] assms
-  unfolding SUPR_def INFI_def image_image by auto
-
-lemma Inf_extreal_cminus:
-  fixes A :: "extreal set" assumes "A \<noteq> {}" and "\<bar>a\<bar> \<noteq> \<infinity>"
-  shows "Inf ((\<lambda>x. a - x) ` A) = a - Sup A"
-proof -
-  { fix x have "-a - -x = -(a - x)" using assms by (cases x) auto }
-  moreover then have "(\<lambda>x. -a - x)`uminus`A = uminus ` (\<lambda>x. a - x) ` A"
-    by (auto simp: image_image)
-  ultimately show ?thesis
-    using Sup_extreal_cminus[of "uminus ` A" "-a"] assms
-    by (auto simp add: extreal_Sup_uminus_image_eq extreal_Inf_uminus_image_eq)
-qed
-
-lemma INFI_extreal_cminus:
-  fixes A assumes "A \<noteq> {}" and "\<bar>a\<bar> \<noteq> \<infinity>"
-  shows "(INF x:A. a - f x) = a - (SUP x:A. f x)"
-  using Inf_extreal_cminus[of "f`A" a] assms
-  unfolding SUPR_def INFI_def image_image
-  by auto
-
-lemma uminus_extreal_add_uminus_uminus:
-  fixes a b :: extreal shows "a \<noteq> \<infinity> \<Longrightarrow> b \<noteq> \<infinity> \<Longrightarrow> - (- a + - b) = a + b"
-  by (cases rule: extreal2_cases[of a b]) auto
-
-lemma INFI_extreal_add:
-  assumes "decseq f" "decseq g" and fin: "\<And>i. f i \<noteq> \<infinity>" "\<And>i. g i \<noteq> \<infinity>"
-  shows "(INF i. f i + g i) = INFI UNIV f + INFI UNIV g"
-proof -
-  have INF_less: "(INF i. f i) < \<infinity>" "(INF i. g i) < \<infinity>"
-    using assms unfolding INF_less_iff by auto
-  { fix i from fin[of i] have "- ((- f i) + (- g i)) = f i + g i"
-      by (rule uminus_extreal_add_uminus_uminus) }
-  then have "(INF i. f i + g i) = (INF i. - ((- f i) + (- g i)))"
-    by simp
-  also have "\<dots> = INFI UNIV f + INFI UNIV g"
-    unfolding extreal_INFI_uminus
-    using assms INF_less
-    by (subst SUPR_extreal_add)
-       (auto simp: extreal_SUPR_uminus intro!: uminus_extreal_add_uminus_uminus)
-  finally show ?thesis .
-qed
-
-subsection "Limits on @{typ extreal}"
-
-subsubsection "Topological space"
-
-instantiation extreal :: topological_space
-begin
-
-definition "open A \<longleftrightarrow> open (extreal -` A)
-       \<and> (\<infinity> \<in> A \<longrightarrow> (\<exists>x. {extreal x <..} \<subseteq> A))
-       \<and> (-\<infinity> \<in> A \<longrightarrow> (\<exists>x. {..<extreal x} \<subseteq> A))"
-
-lemma open_PInfty: "open A \<Longrightarrow> \<infinity> \<in> A \<Longrightarrow> (\<exists>x. {extreal x<..} \<subseteq> A)"
-  unfolding open_extreal_def by auto
-
-lemma open_MInfty: "open A \<Longrightarrow> -\<infinity> \<in> A \<Longrightarrow> (\<exists>x. {..<extreal x} \<subseteq> A)"
-  unfolding open_extreal_def by auto
-
-lemma open_PInfty2: assumes "open A" "\<infinity> \<in> A" obtains x where "{extreal x<..} \<subseteq> A"
-  using open_PInfty[OF assms] by auto
-
-lemma open_MInfty2: assumes "open A" "-\<infinity> \<in> A" obtains x where "{..<extreal x} \<subseteq> A"
-  using open_MInfty[OF assms] by auto
-
-lemma extreal_openE: assumes "open A" obtains x y where
-  "open (extreal -` A)"
-  "\<infinity> \<in> A \<Longrightarrow> {extreal x<..} \<subseteq> A"
-  "-\<infinity> \<in> A \<Longrightarrow> {..<extreal y} \<subseteq> A"
-  using assms open_extreal_def by auto
-
-instance
-proof
-  let ?U = "UNIV::extreal set"
-  show "open ?U" unfolding open_extreal_def
-    by (auto intro!: exI[of _ 0])
-next
-  fix S T::"extreal set" assume "open S" and "open T"
-  from `open S`[THEN extreal_openE] guess xS yS .
-  moreover from `open T`[THEN extreal_openE] guess xT yT .
-  ultimately have
-    "open (extreal -` (S \<inter> T))"
-    "\<infinity> \<in> S \<inter> T \<Longrightarrow> {extreal (max xS xT) <..} \<subseteq> S \<inter> T"
-    "-\<infinity> \<in> S \<inter> T \<Longrightarrow> {..< extreal (min yS yT)} \<subseteq> S \<inter> T"
-    by auto
-  then show "open (S Int T)" unfolding open_extreal_def by blast
-next
-  fix K :: "extreal set set" assume "\<forall>S\<in>K. open S"
-  then have *: "\<forall>S. \<exists>x y. S \<in> K \<longrightarrow> open (extreal -` S) \<and>
-    (\<infinity> \<in> S \<longrightarrow> {extreal x <..} \<subseteq> S) \<and> (-\<infinity> \<in> S \<longrightarrow> {..< extreal y} \<subseteq> S)"
-    by (auto simp: open_extreal_def)
-  then show "open (Union K)" unfolding open_extreal_def
-  proof (intro conjI impI)
-    show "open (extreal -` \<Union>K)"
-      using *[THEN choice] by (auto simp: vimage_Union)
-  qed ((metis UnionE Union_upper subset_trans *)+)
-qed
-end
-
-lemma open_extreal: "open S \<Longrightarrow> open (extreal ` S)"
-  by (auto simp: inj_vimage_image_eq open_extreal_def)
-
-lemma open_extreal_vimage: "open S \<Longrightarrow> open (extreal -` S)"
-  unfolding open_extreal_def by auto
-
-lemma open_extreal_lessThan[intro, simp]: "open {..< a :: extreal}"
-proof -
-  have "\<And>x. extreal -` {..<extreal x} = {..< x}"
-    "extreal -` {..< \<infinity>} = UNIV" "extreal -` {..< -\<infinity>} = {}" by auto
-  then show ?thesis by (cases a) (auto simp: open_extreal_def)
-qed
-
-lemma open_extreal_greaterThan[intro, simp]:
-  "open {a :: extreal <..}"
-proof -
-  have "\<And>x. extreal -` {extreal x<..} = {x<..}"
-    "extreal -` {\<infinity><..} = {}" "extreal -` {-\<infinity><..} = UNIV" by auto
-  then show ?thesis by (cases a) (auto simp: open_extreal_def)
-qed
-
-lemma extreal_open_greaterThanLessThan[intro, simp]: "open {a::extreal <..< b}"
-  unfolding greaterThanLessThan_def by auto
-
-lemma closed_extreal_atLeast[simp, intro]: "closed {a :: extreal ..}"
-proof -
-  have "- {a ..} = {..< a}" by auto
-  then show "closed {a ..}"
-    unfolding closed_def using open_extreal_lessThan by auto
-qed
-
-lemma closed_extreal_atMost[simp, intro]: "closed {.. b :: extreal}"
-proof -
-  have "- {.. b} = {b <..}" by auto
-  then show "closed {.. b}"
-    unfolding closed_def using open_extreal_greaterThan by auto
-qed
-
-lemma closed_extreal_atLeastAtMost[simp, intro]:
-  shows "closed {a :: extreal .. b}"
-  unfolding atLeastAtMost_def by auto
-
-lemma closed_extreal_singleton:
-  "closed {a :: extreal}"
-by (metis atLeastAtMost_singleton closed_extreal_atLeastAtMost)
-
-lemma extreal_open_cont_interval:
-  assumes "open S" "x \<in> S" "\<bar>x\<bar> \<noteq> \<infinity>"
-  obtains e where "e>0" "{x-e <..< x+e} \<subseteq> S"
-proof-
-  from `open S` have "open (extreal -` S)" by (rule extreal_openE)
-  then obtain e where "0 < e" and e: "\<And>y. dist y (real x) < e \<Longrightarrow> extreal y \<in> S"
-    using assms unfolding open_dist by force
-  show thesis
-  proof (intro that subsetI)
-    show "0 < extreal e" using `0 < e` by auto
-    fix y assume "y \<in> {x - extreal e<..<x + extreal e}"
-    with assms obtain t where "y = extreal t" "dist t (real x) < e"
-      apply (cases y) by (auto simp: dist_real_def)
-    then show "y \<in> S" using e[of t] by auto
-  qed
-qed
-
-lemma extreal_open_cont_interval2:
-  assumes "open S" "x \<in> S" and x: "\<bar>x\<bar> \<noteq> \<infinity>"
-  obtains a b where "a < x" "x < b" "{a <..< b} \<subseteq> S"
-proof-
-  guess e using extreal_open_cont_interval[OF assms] .
-  with that[of "x-e" "x+e"] extreal_between[OF x, of e]
-  show thesis by auto
-qed
-
-instance extreal :: t2_space
-proof
-  fix x y :: extreal assume "x ~= y"
-  let "?P x (y::extreal)" = "EX U V. open U & open V & x : U & y : V & U Int V = {}"
-
-  { fix x y :: extreal assume "x < y"
-    from extreal_dense[OF this] obtain z where z: "x < z" "z < y" by auto
-    have "?P x y"
-      apply (rule exI[of _ "{..<z}"])
-      apply (rule exI[of _ "{z<..}"])
-      using z by auto }
-  note * = this
-
-  from `x ~= y`
-  show "EX U V. open U & open V & x : U & y : V & U Int V = {}"
-  proof (cases rule: linorder_cases)
-    assume "x = y" with `x ~= y` show ?thesis by simp
-  next assume "x < y" from *[OF this] show ?thesis by auto
-  next assume "y < x" from *[OF this] show ?thesis by auto
-  qed
-qed
-
-subsubsection {* Convergent sequences *}
-
-lemma lim_extreal[simp]:
-  "((\<lambda>n. extreal (f n)) ---> extreal x) net \<longleftrightarrow> (f ---> x) net" (is "?l = ?r")
-proof (intro iffI topological_tendstoI)
-  fix S assume "?l" "open S" "x \<in> S"
-  then show "eventually (\<lambda>x. f x \<in> S) net"
-    using `?l`[THEN topological_tendstoD, OF open_extreal, OF `open S`]
-    by (simp add: inj_image_mem_iff)
-next
-  fix S assume "?r" "open S" "extreal x \<in> S"
-  show "eventually (\<lambda>x. extreal (f x) \<in> S) net"
-    using `?r`[THEN topological_tendstoD, OF open_extreal_vimage, OF `open S`]
-    using `extreal x \<in> S` by auto
-qed
-
-lemma lim_real_of_extreal[simp]:
-  assumes lim: "(f ---> extreal x) net"
-  shows "((\<lambda>x. real (f x)) ---> x) net"
-proof (intro topological_tendstoI)
-  fix S assume "open S" "x \<in> S"
-  then have S: "open S" "extreal x \<in> extreal ` S"
-    by (simp_all add: inj_image_mem_iff)
-  have "\<forall>x. f x \<in> extreal ` S \<longrightarrow> real (f x) \<in> S" by auto
-  from this lim[THEN topological_tendstoD, OF open_extreal, OF S]
-  show "eventually (\<lambda>x. real (f x) \<in> S) net"
-    by (rule eventually_mono)
-qed
-
-lemma Lim_PInfty: "f ----> \<infinity> <-> (ALL B. EX N. ALL n>=N. f n >= extreal B)" (is "?l = ?r")
-proof assume ?r show ?l apply(rule topological_tendstoI)
-    unfolding eventually_sequentially
-  proof- fix S assume "open S" "\<infinity> : S"
-    from open_PInfty[OF this] guess B .. note B=this
-    from `?r`[rule_format,of "B+1"] guess N .. note N=this
-    show "EX N. ALL n>=N. f n : S" apply(rule_tac x=N in exI)
-    proof safe case goal1
-      have "extreal B < extreal (B + 1)" by auto
-      also have "... <= f n" using goal1 N by auto
-      finally show ?case using B by fastsimp
-    qed
-  qed
-next assume ?l show ?r
-  proof fix B::real have "open {extreal B<..}" "\<infinity> : {extreal B<..}" by auto
-    from topological_tendstoD[OF `?l` this,unfolded eventually_sequentially]
-    guess N .. note N=this
-    show "EX N. ALL n>=N. extreal B <= f n" apply(rule_tac x=N in exI) using N by auto
-  qed
-qed
-
-
-lemma Lim_MInfty: "f ----> (-\<infinity>) <-> (ALL B. EX N. ALL n>=N. f n <= extreal B)" (is "?l = ?r")
-proof assume ?r show ?l apply(rule topological_tendstoI)
-    unfolding eventually_sequentially
-  proof- fix S assume "open S" "(-\<infinity>) : S"
-    from open_MInfty[OF this] guess B .. note B=this
-    from `?r`[rule_format,of "B-(1::real)"] guess N .. note N=this
-    show "EX N. ALL n>=N. f n : S" apply(rule_tac x=N in exI)
-    proof safe case goal1
-      have "extreal (B - 1) >= f n" using goal1 N by auto
-      also have "... < extreal B" by auto
-      finally show ?case using B by fastsimp
-    qed
-  qed
-next assume ?l show ?r
-  proof fix B::real have "open {..<extreal B}" "(-\<infinity>) : {..<extreal B}" by auto
-    from topological_tendstoD[OF `?l` this,unfolded eventually_sequentially]
-    guess N .. note N=this
-    show "EX N. ALL n>=N. extreal B >= f n" apply(rule_tac x=N in exI) using N by auto
-  qed
-qed
-
-
-lemma Lim_bounded_PInfty: assumes lim:"f ----> l" and "!!n. f n <= extreal B" shows "l ~= \<infinity>"
-proof(rule ccontr,unfold not_not) let ?B = "B + 1" assume as:"l=\<infinity>"
-  from lim[unfolded this Lim_PInfty,rule_format,of "?B"]
-  guess N .. note N=this[rule_format,OF le_refl]
-  hence "extreal ?B <= extreal B" using assms(2)[of N] by(rule order_trans)
-  hence "extreal ?B < extreal ?B" apply (rule le_less_trans) by auto
-  thus False by auto
-qed
-
-
-lemma Lim_bounded_MInfty: assumes lim:"f ----> l" and "!!n. f n >= extreal B" shows "l ~= (-\<infinity>)"
-proof(rule ccontr,unfold not_not) let ?B = "B - 1" assume as:"l=(-\<infinity>)"
-  from lim[unfolded this Lim_MInfty,rule_format,of "?B"]
-  guess N .. note N=this[rule_format,OF le_refl]
-  hence "extreal B <= extreal ?B" using assms(2)[of N] order_trans[of "extreal B" "f N" "extreal(B - 1)"] by blast
-  thus False by auto
-qed
-
-
-lemma tendsto_explicit:
-  "f ----> f0 <-> (ALL S. open S --> f0 : S --> (EX N. ALL n>=N. f n : S))"
-  unfolding tendsto_def eventually_sequentially by auto
-
-
-lemma tendsto_obtains_N:
-  assumes "f ----> f0"
-  assumes "open S" "f0 : S"
-  obtains N where "ALL n>=N. f n : S"
-  using tendsto_explicit[of f f0] assms by auto
-
-
-lemma tail_same_limit:
-  fixes X Y N
-  assumes "X ----> L" "ALL n>=N. X n = Y n"
-  shows "Y ----> L"
-proof-
-{ fix S assume "open S" and "L:S"
-  from this obtain N1 where "ALL n>=N1. X n : S"
-     using assms unfolding tendsto_def eventually_sequentially by auto
-  hence "ALL n>=max N N1. Y n : S" using assms by auto
-  hence "EX N. ALL n>=N. Y n : S" apply(rule_tac x="max N N1" in exI) by auto
-}
-thus ?thesis using tendsto_explicit by auto
-qed
-
-
-lemma Lim_bounded_PInfty2:
-assumes lim:"f ----> l" and "ALL n>=N. f n <= extreal B"
-shows "l ~= \<infinity>"
-proof-
-  def g == "(%n. if n>=N then f n else extreal B)"
-  hence "g ----> l" using tail_same_limit[of f l N g] lim by auto
-  moreover have "!!n. g n <= extreal B" using g_def assms by auto
-  ultimately show ?thesis using  Lim_bounded_PInfty by auto
-qed
-
-lemma Lim_bounded_extreal:
-  assumes lim:"f ----> (l :: extreal)"
-  and "ALL n>=M. f n <= C"
-  shows "l<=C"
-proof-
-{ assume "l=(-\<infinity>)" hence ?thesis by auto }
-moreover
-{ assume "~(l=(-\<infinity>))"
-  { assume "C=\<infinity>" hence ?thesis by auto }
-  moreover
-  { assume "C=(-\<infinity>)" hence "ALL n>=M. f n = (-\<infinity>)" using assms by auto
-    hence "l=(-\<infinity>)" using assms
-       tendsto_unique[OF trivial_limit_sequentially] tail_same_limit[of "\<lambda>n. -\<infinity>" "-\<infinity>" M f, OF tendsto_const] by auto
-    hence ?thesis by auto }
-  moreover
-  { assume "EX B. C = extreal B"
-    from this obtain B where B_def: "C=extreal B" by auto
-    hence "~(l=\<infinity>)" using Lim_bounded_PInfty2 assms by auto
-    from this obtain m where m_def: "extreal m=l" using `~(l=(-\<infinity>))` by (cases l) auto
-    from this obtain N where N_def: "ALL n>=N. f n : {extreal(m - 1) <..< extreal(m+1)}"
-       apply (subst tendsto_obtains_N[of f l "{extreal(m - 1) <..< extreal(m+1)}"]) using assms by auto
-    { fix n assume "n>=N"
-      hence "EX r. extreal r = f n" using N_def by (cases "f n") auto
-    } from this obtain g where g_def: "ALL n>=N. extreal (g n) = f n" by metis
-    hence "(%n. extreal (g n)) ----> l" using tail_same_limit[of f l N] assms by auto
-    hence *: "(%n. g n) ----> m" using m_def by auto
-    { fix n assume "n>=max N M"
-      hence "extreal (g n) <= extreal B" using assms g_def B_def by auto
-      hence "g n <= B" by auto
-    } hence "EX N. ALL n>=N. g n <= B" by blast
-    hence "m<=B" using * LIMSEQ_le_const2[of g m B] by auto
-    hence ?thesis using m_def B_def by auto
-  } ultimately have ?thesis by (cases C) auto
-} ultimately show ?thesis by blast
-qed
-
-lemma real_of_extreal_mult[simp]:
-  fixes a b :: extreal shows "real (a * b) = real a * real b"
-  by (cases rule: extreal2_cases[of a b]) auto
-
-lemma real_of_extreal_eq_0:
-  "real x = 0 \<longleftrightarrow> x = \<infinity> \<or> x = -\<infinity> \<or> x = 0"
-  by (cases x) auto
-
-lemma tendsto_extreal_realD:
-  fixes f :: "'a \<Rightarrow> extreal"
-  assumes "x \<noteq> 0" and tendsto: "((\<lambda>x. extreal (real (f x))) ---> x) net"
-  shows "(f ---> x) net"
-proof (intro topological_tendstoI)
-  fix S assume S: "open S" "x \<in> S"
-  with `x \<noteq> 0` have "open (S - {0})" "x \<in> S - {0}" by auto
-  from tendsto[THEN topological_tendstoD, OF this]
-  show "eventually (\<lambda>x. f x \<in> S) net"
-    by (rule eventually_rev_mp) (auto simp: extreal_real real_of_extreal_0)
-qed
-
-lemma tendsto_extreal_realI:
-  fixes f :: "'a \<Rightarrow> extreal"
-  assumes x: "\<bar>x\<bar> \<noteq> \<infinity>" and tendsto: "(f ---> x) net"
-  shows "((\<lambda>x. extreal (real (f x))) ---> x) net"
-proof (intro topological_tendstoI)
-  fix S assume "open S" "x \<in> S"
-  with x have "open (S - {\<infinity>, -\<infinity>})" "x \<in> S - {\<infinity>, -\<infinity>}" by auto
-  from tendsto[THEN topological_tendstoD, OF this]
-  show "eventually (\<lambda>x. extreal (real (f x)) \<in> S) net"
-    by (elim eventually_elim1) (auto simp: extreal_real)
-qed
-
-lemma extreal_mult_cancel_left:
-  fixes a b c :: extreal shows "a * b = a * c \<longleftrightarrow>
-    ((\<bar>a\<bar> = \<infinity> \<and> 0 < b * c) \<or> a = 0 \<or> b = c)"
-  by (cases rule: extreal3_cases[of a b c])
-     (simp_all add: zero_less_mult_iff)
-
-lemma extreal_inj_affinity:
-  assumes "\<bar>m\<bar> \<noteq> \<infinity>" "m \<noteq> 0" "\<bar>t\<bar> \<noteq> \<infinity>"
-  shows "inj_on (\<lambda>x. m * x + t) A"
-  using assms
-  by (cases rule: extreal2_cases[of m t])
-     (auto intro!: inj_onI simp: extreal_add_cancel_right extreal_mult_cancel_left)
-
-lemma extreal_PInfty_eq_plus[simp]:
-  shows "\<infinity> = a + b \<longleftrightarrow> a = \<infinity> \<or> b = \<infinity>"
-  by (cases rule: extreal2_cases[of a b]) auto
-
-lemma extreal_MInfty_eq_plus[simp]:
-  shows "-\<infinity> = a + b \<longleftrightarrow> (a = -\<infinity> \<and> b \<noteq> \<infinity>) \<or> (b = -\<infinity> \<and> a \<noteq> \<infinity>)"
-  by (cases rule: extreal2_cases[of a b]) auto
-
-lemma extreal_less_divide_pos:
-  "x > 0 \<Longrightarrow> x \<noteq> \<infinity> \<Longrightarrow> y < z / x \<longleftrightarrow> x * y < z"
-  by (cases rule: extreal3_cases[of x y z]) (auto simp: field_simps)
-
-lemma extreal_divide_less_pos:
-  "x > 0 \<Longrightarrow> x \<noteq> \<infinity> \<Longrightarrow> y / x < z \<longleftrightarrow> y < x * z"
-  by (cases rule: extreal3_cases[of x y z]) (auto simp: field_simps)
-
-lemma extreal_divide_eq:
-  "b \<noteq> 0 \<Longrightarrow> \<bar>b\<bar> \<noteq> \<infinity> \<Longrightarrow> a / b = c \<longleftrightarrow> a = b * c"
-  by (cases rule: extreal3_cases[of a b c])
-     (simp_all add: field_simps)
-
-lemma extreal_inverse_not_MInfty[simp]: "inverse a \<noteq> -\<infinity>"
-  by (cases a) auto
-
-lemma extreal_mult_m1[simp]: "x * extreal (-1) = -x"
-  by (cases x) auto
-
-lemma extreal_LimI_finite:
-  assumes "\<bar>x\<bar> \<noteq> \<infinity>"
-  assumes "!!r. 0 < r ==> EX N. ALL n>=N. u n < x + r & x < u n + r"
-  shows "u ----> x"
-proof (rule topological_tendstoI, unfold eventually_sequentially)
-  obtain rx where rx_def: "x=extreal rx" using assms by (cases x) auto
-  fix S assume "open S" "x : S"
-  then have "open (extreal -` S)" unfolding open_extreal_def by auto
-  with `x \<in> S` obtain r where "0 < r" and dist: "!!y. dist y rx < r ==> extreal y \<in> S"
-    unfolding open_real_def rx_def by auto
-  then obtain n where
-    upper: "!!N. n <= N ==> u N < x + extreal r" and
-    lower: "!!N. n <= N ==> x < u N + extreal r" using assms(2)[of "extreal r"] by auto
-  show "EX N. ALL n>=N. u n : S"
-  proof (safe intro!: exI[of _ n])
-    fix N assume "n <= N"
-    from upper[OF this] lower[OF this] assms `0 < r`
-    have "u N ~: {\<infinity>,(-\<infinity>)}" by auto
-    from this obtain ra where ra_def: "(u N) = extreal ra" by (cases "u N") auto
-    hence "rx < ra + r" and "ra < rx + r"
-       using rx_def assms `0 < r` lower[OF `n <= N`] upper[OF `n <= N`] by auto
-    hence "dist (real (u N)) rx < r"
-      using rx_def ra_def
-      by (auto simp: dist_real_def abs_diff_less_iff field_simps)
-    from dist[OF this] show "u N : S" using `u N  ~: {\<infinity>, -\<infinity>}`
-      by (auto simp: extreal_real split: split_if_asm)
-  qed
-qed
-
-lemma extreal_LimI_finite_iff:
-  assumes "\<bar>x\<bar> \<noteq> \<infinity>"
-  shows "u ----> x <-> (ALL r. 0 < r --> (EX N. ALL n>=N. u n < x + r & x < u n + r))"
-  (is "?lhs <-> ?rhs")
-proof
-  assume lim: "u ----> x"
-  { fix r assume "(r::extreal)>0"
-    from this obtain N where N_def: "ALL n>=N. u n : {x - r <..< x + r}"
-       apply (subst tendsto_obtains_N[of u x "{x - r <..< x + r}"])
-       using lim extreal_between[of x r] assms `r>0` by auto
-    hence "EX N. ALL n>=N. u n < x + r & x < u n + r"
-      using extreal_minus_less[of r x] by (cases r) auto
-  } then show "?rhs" by auto
-next
-  assume ?rhs then show "u ----> x"
-    using extreal_LimI_finite[of x] assms by auto
-qed
-
-
-subsubsection {* @{text Liminf} and @{text Limsup} *}
-
-definition
-  "Liminf net f = (GREATEST l. \<forall>y<l. eventually (\<lambda>x. y < f x) net)"
-
-definition
-  "Limsup net f = (LEAST l. \<forall>y>l. eventually (\<lambda>x. f x < y) net)"
-
-lemma Liminf_Sup:
-  fixes f :: "'a => 'b::{complete_lattice, linorder}"
-  shows "Liminf net f = Sup {l. \<forall>y<l. eventually (\<lambda>x. y < f x) net}"
-  by (auto intro!: Greatest_equality complete_lattice_class.Sup_upper simp: less_Sup_iff Liminf_def)
-
-lemma Limsup_Inf:
-  fixes f :: "'a => 'b::{complete_lattice, linorder}"
-  shows "Limsup net f = Inf {l. \<forall>y>l. eventually (\<lambda>x. f x < y) net}"
-  by (auto intro!: Least_equality complete_lattice_class.Inf_lower simp: Inf_less_iff Limsup_def)
-
-lemma extreal_SupI:
-  fixes x :: extreal
-  assumes "\<And>y. y \<in> A \<Longrightarrow> y \<le> x"
-  assumes "\<And>y. (\<And>z. z \<in> A \<Longrightarrow> z \<le> y) \<Longrightarrow> x \<le> y"
-  shows "Sup A = x"
-  unfolding Sup_extreal_def
-  using assms by (auto intro!: Least_equality)
-
-lemma extreal_InfI:
-  fixes x :: extreal
-  assumes "\<And>i. i \<in> A \<Longrightarrow> x \<le> i"
-  assumes "\<And>y. (\<And>i. i \<in> A \<Longrightarrow> y \<le> i) \<Longrightarrow> y \<le> x"
-  shows "Inf A = x"
-  unfolding Inf_extreal_def
-  using assms by (auto intro!: Greatest_equality)
-
-lemma Limsup_const:
-  fixes c :: "'a::{complete_lattice, linorder}"
-  assumes ntriv: "\<not> trivial_limit net"
-  shows "Limsup net (\<lambda>x. c) = c"
-  unfolding Limsup_Inf
-proof (safe intro!: antisym complete_lattice_class.Inf_greatest complete_lattice_class.Inf_lower)
-  fix x assume *: "\<forall>y>x. eventually (\<lambda>_. c < y) net"
-  show "c \<le> x"
-  proof (rule ccontr)
-    assume "\<not> c \<le> x" then have "x < c" by auto
-    then show False using ntriv * by (auto simp: trivial_limit_def)
-  qed
-qed auto
-
-lemma Liminf_const:
-  fixes c :: "'a::{complete_lattice, linorder}"
-  assumes ntriv: "\<not> trivial_limit net"
-  shows "Liminf net (\<lambda>x. c) = c"
-  unfolding Liminf_Sup
-proof (safe intro!: antisym complete_lattice_class.Sup_least complete_lattice_class.Sup_upper)
-  fix x assume *: "\<forall>y<x. eventually (\<lambda>_. y < c) net"
-  show "x \<le> c"
-  proof (rule ccontr)
-    assume "\<not> x \<le> c" then have "c < x" by auto
-    then show False using ntriv * by (auto simp: trivial_limit_def)
-  qed
-qed auto
-
-lemma mono_set:
-  fixes S :: "('a::order) set"
-  shows "mono S \<longleftrightarrow> (\<forall>x y. x \<le> y \<longrightarrow> x \<in> S \<longrightarrow> y \<in> S)"
-  by (auto simp: mono_def mem_def)
-
-lemma mono_greaterThan[intro, simp]: "mono {B<..}" unfolding mono_set by auto
-lemma mono_atLeast[intro, simp]: "mono {B..}" unfolding mono_set by auto
-lemma mono_UNIV[intro, simp]: "mono UNIV" unfolding mono_set by auto
-lemma mono_empty[intro, simp]: "mono {}" unfolding mono_set by auto
-
-lemma mono_set_iff:
-  fixes S :: "'a::{linorder,complete_lattice} set"
-  defines "a \<equiv> Inf S"
-  shows "mono S \<longleftrightarrow> (S = {a <..} \<or> S = {a..})" (is "_ = ?c")
-proof
-  assume "mono S"
-  then have mono: "\<And>x y. x \<le> y \<Longrightarrow> x \<in> S \<Longrightarrow> y \<in> S" by (auto simp: mono_set)
-  show ?c
-  proof cases
-    assume "a \<in> S"
-    show ?c
-      using mono[OF _ `a \<in> S`]
-      by (auto intro: complete_lattice_class.Inf_lower simp: a_def)
-  next
-    assume "a \<notin> S"
-    have "S = {a <..}"
-    proof safe
-      fix x assume "x \<in> S"
-      then have "a \<le> x" unfolding a_def by (rule complete_lattice_class.Inf_lower)
-      then show "a < x" using `x \<in> S` `a \<notin> S` by (cases "a = x") auto
-    next
-      fix x assume "a < x"
-      then obtain y where "y < x" "y \<in> S" unfolding a_def Inf_less_iff ..
-      with mono[of y x] show "x \<in> S" by auto
-    qed
-    then show ?c ..
-  qed
-qed auto
-
-lemma lim_imp_Liminf:
-  fixes f :: "'a \<Rightarrow> extreal"
-  assumes ntriv: "\<not> trivial_limit net"
-  assumes lim: "(f ---> f0) net"
-  shows "Liminf net f = f0"
-  unfolding Liminf_Sup
-proof (safe intro!: extreal_SupI)
-  fix y assume *: "\<forall>y'<y. eventually (\<lambda>x. y' < f x) net"
-  show "y \<le> f0"
-  proof (rule extreal_le_extreal)
-    fix B assume "B < y"
-    { assume "f0 < B"
-      then have "eventually (\<lambda>x. f x < B \<and> B < f x) net"
-         using topological_tendstoD[OF lim, of "{..<B}"] *[rule_format, OF `B < y`]
-         by (auto intro: eventually_conj)
-      also have "(\<lambda>x. f x < B \<and> B < f x) = (\<lambda>x. False)" by (auto simp: fun_eq_iff)
-      finally have False using ntriv[unfolded trivial_limit_def] by auto
-    } then show "B \<le> f0" by (metis linorder_le_less_linear)
-  qed
-next
-  fix y assume *: "\<forall>z. z \<in> {l. \<forall>y<l. eventually (\<lambda>x. y < f x) net} \<longrightarrow> z \<le> y"
-  show "f0 \<le> y"
-  proof (safe intro!: *[rule_format])
-    fix y assume "y < f0" then show "eventually (\<lambda>x. y < f x) net"
-      using lim[THEN topological_tendstoD, of "{y <..}"] by auto
-  qed
-qed
-
-lemma extreal_Liminf_le_Limsup:
-  fixes f :: "'a \<Rightarrow> extreal"
-  assumes ntriv: "\<not> trivial_limit net"
-  shows "Liminf net f \<le> Limsup net f"
-  unfolding Limsup_Inf Liminf_Sup
-proof (safe intro!: complete_lattice_class.Inf_greatest  complete_lattice_class.Sup_least)
-  fix u v assume *: "\<forall>y<u. eventually (\<lambda>x. y < f x) net" "\<forall>y>v. eventually (\<lambda>x. f x < y) net"
-  show "u \<le> v"
-  proof (rule ccontr)
-    assume "\<not> u \<le> v"
-    then obtain t where "t < u" "v < t"
-      using extreal_dense[of v u] by (auto simp: not_le)
-    then have "eventually (\<lambda>x. t < f x \<and> f x < t) net"
-      using * by (auto intro: eventually_conj)
-    also have "(\<lambda>x. t < f x \<and> f x < t) = (\<lambda>x. False)" by (auto simp: fun_eq_iff)
-    finally show False using ntriv by (auto simp: trivial_limit_def)
-  qed
-qed
-
-lemma Liminf_mono:
-  fixes f g :: "'a => extreal"
-  assumes ev: "eventually (\<lambda>x. f x \<le> g x) net"
-  shows "Liminf net f \<le> Liminf net g"
-  unfolding Liminf_Sup
-proof (safe intro!: Sup_mono bexI)
-  fix a y assume "\<forall>y<a. eventually (\<lambda>x. y < f x) net" and "y < a"
-  then have "eventually (\<lambda>x. y < f x) net" by auto
-  then show "eventually (\<lambda>x. y < g x) net"
-    by (rule eventually_rev_mp) (rule eventually_mono[OF _ ev], auto)
-qed simp
-
-lemma Liminf_eq:
-  fixes f g :: "'a \<Rightarrow> extreal"
-  assumes "eventually (\<lambda>x. f x = g x) net"
-  shows "Liminf net f = Liminf net g"
-  by (intro antisym Liminf_mono eventually_mono[OF _ assms]) auto
-
-lemma Liminf_mono_all:
-  fixes f g :: "'a \<Rightarrow> extreal"
-  assumes "\<And>x. f x \<le> g x"
-  shows "Liminf net f \<le> Liminf net g"
-  using assms by (intro Liminf_mono always_eventually) auto
-
-lemma Limsup_mono:
-  fixes f g :: "'a \<Rightarrow> extreal"
-  assumes ev: "eventually (\<lambda>x. f x \<le> g x) net"
-  shows "Limsup net f \<le> Limsup net g"
-  unfolding Limsup_Inf
-proof (safe intro!: Inf_mono bexI)
-  fix a y assume "\<forall>y>a. eventually (\<lambda>x. g x < y) net" and "a < y"
-  then have "eventually (\<lambda>x. g x < y) net" by auto
-  then show "eventually (\<lambda>x. f x < y) net"
-    by (rule eventually_rev_mp) (rule eventually_mono[OF _ ev], auto)
-qed simp
-
-lemma Limsup_mono_all:
-  fixes f g :: "'a \<Rightarrow> extreal"
-  assumes "\<And>x. f x \<le> g x"
-  shows "Limsup net f \<le> Limsup net g"
-  using assms by (intro Limsup_mono always_eventually) auto
-
-lemma Limsup_eq:
-  fixes f g :: "'a \<Rightarrow> extreal"
-  assumes "eventually (\<lambda>x. f x = g x) net"
-  shows "Limsup net f = Limsup net g"
-  by (intro antisym Limsup_mono eventually_mono[OF _ assms]) auto
-
-abbreviation "liminf \<equiv> Liminf sequentially"
-
-abbreviation "limsup \<equiv> Limsup sequentially"
-
-lemma (in complete_lattice) less_INFD:
-  assumes "y < INFI A f"" i \<in> A" shows "y < f i"
-proof -
-  note `y < INFI A f`
-  also have "INFI A f \<le> f i" using `i \<in> A` by (rule INF_leI)
-  finally show "y < f i" .
-qed
-
-lemma liminf_SUPR_INFI:
-  fixes f :: "nat \<Rightarrow> extreal"
-  shows "liminf f = (SUP n. INF m:{n..}. f m)"
-  unfolding Liminf_Sup eventually_sequentially
-proof (safe intro!: antisym complete_lattice_class.Sup_least)
-  fix x assume *: "\<forall>y<x. \<exists>N. \<forall>n\<ge>N. y < f n" show "x \<le> (SUP n. INF m:{n..}. f m)"
-  proof (rule extreal_le_extreal)
-    fix y assume "y < x"
-    with * obtain N where "\<And>n. N \<le> n \<Longrightarrow> y < f n" by auto
-    then have "y \<le> (INF m:{N..}. f m)" by (force simp: le_INF_iff)
-    also have "\<dots> \<le> (SUP n. INF m:{n..}. f m)" by (intro le_SUPI) auto
-    finally show "y \<le> (SUP n. INF m:{n..}. f m)" .
-  qed
-next
-  show "(SUP n. INF m:{n..}. f m) \<le> Sup {l. \<forall>y<l. \<exists>N. \<forall>n\<ge>N. y < f n}"
-  proof (unfold SUPR_def, safe intro!: Sup_mono bexI)
-    fix y n assume "y < INFI {n..} f"
-    from less_INFD[OF this] show "\<exists>N. \<forall>n\<ge>N. y < f n" by (intro exI[of _ n]) auto
-  qed (rule order_refl)
-qed
-
-lemma tail_same_limsup:
-  fixes X Y :: "nat => extreal"
-  assumes "\<And>n. N \<le> n \<Longrightarrow> X n = Y n"
-  shows "limsup X = limsup Y"
-  using Limsup_eq[of X Y sequentially] eventually_sequentially assms by auto
-
-lemma tail_same_liminf:
-  fixes X Y :: "nat => extreal"
-  assumes "\<And>n. N \<le> n \<Longrightarrow> X n = Y n"
-  shows "liminf X = liminf Y"
-  using Liminf_eq[of X Y sequentially] eventually_sequentially assms by auto
-
-lemma liminf_mono:
-  fixes X Y :: "nat \<Rightarrow> extreal"
-  assumes "\<And>n. N \<le> n \<Longrightarrow> X n <= Y n"
-  shows "liminf X \<le> liminf Y"
-  using Liminf_mono[of X Y sequentially] eventually_sequentially assms by auto
-
-lemma limsup_mono:
-  fixes X Y :: "nat => extreal"
-  assumes "\<And>n. N \<le> n \<Longrightarrow> X n <= Y n"
-  shows "limsup X \<le> limsup Y"
-  using Limsup_mono[of X Y sequentially] eventually_sequentially assms by auto
-
-declare trivial_limit_sequentially[simp]
-
-lemma
-  fixes X :: "nat \<Rightarrow> extreal"
-  shows extreal_incseq_uminus[simp]: "incseq (\<lambda>i. - X i) = decseq X"
-    and extreal_decseq_uminus[simp]: "decseq (\<lambda>i. - X i) = incseq X"
-  unfolding incseq_def decseq_def by auto
-
-lemma liminf_bounded:
-  fixes X Y :: "nat \<Rightarrow> extreal"
-  assumes "\<And>n. N \<le> n \<Longrightarrow> C \<le> X n"
-  shows "C \<le> liminf X"
-  using liminf_mono[of N "\<lambda>n. C" X] assms Liminf_const[of sequentially C] by simp
-
-lemma limsup_bounded:
-  fixes X Y :: "nat => extreal"
-  assumes "\<And>n. N \<le> n \<Longrightarrow> X n <= C"
-  shows "limsup X \<le> C"
-  using limsup_mono[of N X "\<lambda>n. C"] assms Limsup_const[of sequentially C] by simp
-
-lemma liminf_bounded_iff:
-  fixes x :: "nat \<Rightarrow> extreal"
-  shows "C \<le> liminf x \<longleftrightarrow> (\<forall>B<C. \<exists>N. \<forall>n\<ge>N. B < x n)" (is "?lhs <-> ?rhs")
-proof safe
-  fix B assume "B < C" "C \<le> liminf x"
-  then have "B < liminf x" by auto
-  then obtain N where "B < (INF m:{N..}. x m)"
-    unfolding liminf_SUPR_INFI SUPR_def less_Sup_iff by auto
-  from less_INFD[OF this] show "\<exists>N. \<forall>n\<ge>N. B < x n" by auto
-next
-  assume *: "\<forall>B<C. \<exists>N. \<forall>n\<ge>N. B < x n"
-  { fix B assume "B<C"
-    then obtain N where "\<forall>n\<ge>N. B < x n" using `?rhs` by auto
-    hence "B \<le> (INF m:{N..}. x m)" by (intro le_INFI) auto
-    also have "... \<le> liminf x" unfolding liminf_SUPR_INFI by (intro le_SUPI) simp
-    finally have "B \<le> liminf x" .
-  } then show "?lhs" by (metis * leD liminf_bounded linorder_le_less_linear)
-qed
-
-lemma liminf_subseq_mono:
-  fixes X :: "nat \<Rightarrow> extreal"
-  assumes "subseq r"
-  shows "liminf X \<le> liminf (X \<circ> r) "
-proof-
-  have "\<And>n. (INF m:{n..}. X m) \<le> (INF m:{n..}. (X \<circ> r) m)"
-  proof (safe intro!: INF_mono)
-    fix n m :: nat assume "n \<le> m" then show "\<exists>ma\<in>{n..}. X ma \<le> (X \<circ> r) m"
-      using seq_suble[OF `subseq r`, of m] by (intro bexI[of _ "r m"]) auto
-  qed
-  then show ?thesis by (auto intro!: SUP_mono simp: liminf_SUPR_INFI comp_def)
-qed
-
-lemma extreal_real': assumes "\<bar>x\<bar> \<noteq> \<infinity>" shows "extreal (real x) = x"
-  using assms by auto
-
-lemma extreal_le_extreal_bounded:
-  fixes x y z :: extreal
-  assumes "z \<le> y"
-  assumes *: "\<And>B. z < B \<Longrightarrow> B < x \<Longrightarrow> B \<le> y"
-  shows "x \<le> y"
-proof (rule extreal_le_extreal)
-  fix B assume "B < x"
-  show "B \<le> y"
-  proof cases
-    assume "z < B" from *[OF this `B < x`] show "B \<le> y" .
-  next
-    assume "\<not> z < B" with `z \<le> y` show "B \<le> y" by auto
-  qed
-qed
-
-lemma fixes x y :: extreal
-  shows Sup_atMost[simp]: "Sup {.. y} = y"
-    and Sup_lessThan[simp]: "Sup {..< y} = y"
-    and Sup_atLeastAtMost[simp]: "x \<le> y \<Longrightarrow> Sup { x .. y} = y"
-    and Sup_greaterThanAtMost[simp]: "x < y \<Longrightarrow> Sup { x <.. y} = y"
-    and Sup_atLeastLessThan[simp]: "x < y \<Longrightarrow> Sup { x ..< y} = y"
-  by (auto simp: Sup_extreal_def intro!: Least_equality
-           intro: extreal_le_extreal extreal_le_extreal_bounded[of x])
-
-lemma Sup_greaterThanlessThan[simp]:
-  fixes x y :: extreal assumes "x < y" shows "Sup { x <..< y} = y"
-  unfolding Sup_extreal_def
-proof (intro Least_equality extreal_le_extreal_bounded[of _ _ y])
-  fix z assume z: "\<forall>u\<in>{x<..<y}. u \<le> z"
-  from extreal_dense[OF `x < y`] guess w .. note w = this
-  with z[THEN bspec, of w] show "x \<le> z" by auto
-qed auto
-
-lemma real_extreal_id: "real o extreal = id"
-proof-
-{ fix x have "(real o extreal) x = id x" by auto }
-from this show ?thesis using ext by blast
-qed
-
-lemma open_image_extreal: "open(UNIV-{\<infinity>,(-\<infinity>)})"
-by (metis range_extreal open_extreal open_UNIV)
-
-lemma extreal_le_distrib:
-  fixes a b c :: extreal shows "c * (a + b) \<le> c * a + c * b"
-  by (cases rule: extreal3_cases[of a b c])
-     (auto simp add: field_simps not_le mult_le_0_iff mult_less_0_iff)
-
-lemma extreal_pos_distrib:
-  fixes a b c :: extreal assumes "0 \<le> c" "c \<noteq> \<infinity>" shows "c * (a + b) = c * a + c * b"
-  using assms by (cases rule: extreal3_cases[of a b c])
-                 (auto simp add: field_simps not_le mult_le_0_iff mult_less_0_iff)
-
-lemma extreal_pos_le_distrib:
-fixes a b c :: extreal
-assumes "c>=0"
-shows "c * (a + b) <= c * a + c * b"
-  using assms by (cases rule: extreal3_cases[of a b c])
-                 (auto simp add: field_simps)
-
-lemma extreal_max_mono:
-  "[| (a::extreal) <= b; c <= d |] ==> max a c <= max b d"
-  by (metis sup_extreal_def sup_mono)
-
-
-lemma extreal_max_least:
-  "[| (a::extreal) <= x; c <= x |] ==> max a c <= x"
-  by (metis sup_extreal_def sup_least)
-
-end
--- a/src/HOL/Library/Library.thy	Wed Jul 20 12:23:20 2011 +0200
+++ b/src/HOL/Library/Library.thy	Wed Jul 20 13:27:01 2011 +0200
@@ -15,6 +15,7 @@
   Diagonalize
   Dlist_Cset
   Eval_Witness
+  Extended_Nat
   Float
   Formal_Power_Series
   Fraction_Field
@@ -35,7 +36,6 @@
   Monad_Syntax
   More_List
   Multiset
-  Nat_Infinity
   Nested_Environment
   Numeral_Type
   OptionalSugar
--- a/src/HOL/Library/Nat_Infinity.thy	Wed Jul 20 12:23:20 2011 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,551 +0,0 @@
-(*  Title:      HOL/Library/Nat_Infinity.thy
-    Author:     David von Oheimb, TU Muenchen;  Florian Haftmann, TU Muenchen
-    Contributions: David Trachtenherz, TU Muenchen
-*)
-
-header {* Natural numbers with infinity *}
-
-theory Nat_Infinity
-imports Main
-begin
-
-subsection {* Type definition *}
-
-text {*
-  We extend the standard natural numbers by a special value indicating
-  infinity.
-*}
-
-datatype inat = Fin nat | Infty
-
-notation (xsymbols)
-  Infty  ("\<infinity>")
-
-notation (HTML output)
-  Infty  ("\<infinity>")
-
-
-lemma not_Infty_eq[iff]: "(x ~= Infty) = (EX i. x = Fin i)"
-by (cases x) auto
-
-lemma not_Fin_eq [iff]: "(ALL y. x ~= Fin y) = (x = Infty)"
-by (cases x) auto
-
-
-primrec the_Fin :: "inat \<Rightarrow> nat"
-where "the_Fin (Fin n) = n"
-
-
-subsection {* Constructors and numbers *}
-
-instantiation inat :: "{zero, one, number}"
-begin
-
-definition
-  "0 = Fin 0"
-
-definition
-  [code_unfold]: "1 = Fin 1"
-
-definition
-  [code_unfold, code del]: "number_of k = Fin (number_of k)"
-
-instance ..
-
-end
-
-definition iSuc :: "inat \<Rightarrow> inat" where
-  "iSuc i = (case i of Fin n \<Rightarrow> Fin (Suc n) | \<infinity> \<Rightarrow> \<infinity>)"
-
-lemma Fin_0: "Fin 0 = 0"
-  by (simp add: zero_inat_def)
-
-lemma Fin_1: "Fin 1 = 1"
-  by (simp add: one_inat_def)
-
-lemma Fin_number: "Fin (number_of k) = number_of k"
-  by (simp add: number_of_inat_def)
-
-lemma one_iSuc: "1 = iSuc 0"
-  by (simp add: zero_inat_def one_inat_def iSuc_def)
-
-lemma Infty_ne_i0 [simp]: "\<infinity> \<noteq> 0"
-  by (simp add: zero_inat_def)
-
-lemma i0_ne_Infty [simp]: "0 \<noteq> \<infinity>"
-  by (simp add: zero_inat_def)
-
-lemma zero_inat_eq [simp]:
-  "number_of k = (0\<Colon>inat) \<longleftrightarrow> number_of k = (0\<Colon>nat)"
-  "(0\<Colon>inat) = number_of k \<longleftrightarrow> number_of k = (0\<Colon>nat)"
-  unfolding zero_inat_def number_of_inat_def by simp_all
-
-lemma one_inat_eq [simp]:
-  "number_of k = (1\<Colon>inat) \<longleftrightarrow> number_of k = (1\<Colon>nat)"
-  "(1\<Colon>inat) = number_of k \<longleftrightarrow> number_of k = (1\<Colon>nat)"
-  unfolding one_inat_def number_of_inat_def by simp_all
-
-lemma zero_one_inat_neq [simp]:
-  "\<not> 0 = (1\<Colon>inat)"
-  "\<not> 1 = (0\<Colon>inat)"
-  unfolding zero_inat_def one_inat_def by simp_all
-
-lemma Infty_ne_i1 [simp]: "\<infinity> \<noteq> 1"
-  by (simp add: one_inat_def)
-
-lemma i1_ne_Infty [simp]: "1 \<noteq> \<infinity>"
-  by (simp add: one_inat_def)
-
-lemma Infty_ne_number [simp]: "\<infinity> \<noteq> number_of k"
-  by (simp add: number_of_inat_def)
-
-lemma number_ne_Infty [simp]: "number_of k \<noteq> \<infinity>"
-  by (simp add: number_of_inat_def)
-
-lemma iSuc_Fin: "iSuc (Fin n) = Fin (Suc n)"
-  by (simp add: iSuc_def)
-
-lemma iSuc_number_of: "iSuc (number_of k) = Fin (Suc (number_of k))"
-  by (simp add: iSuc_Fin number_of_inat_def)
-
-lemma iSuc_Infty [simp]: "iSuc \<infinity> = \<infinity>"
-  by (simp add: iSuc_def)
-
-lemma iSuc_ne_0 [simp]: "iSuc n \<noteq> 0"
-  by (simp add: iSuc_def zero_inat_def split: inat.splits)
-
-lemma zero_ne_iSuc [simp]: "0 \<noteq> iSuc n"
-  by (rule iSuc_ne_0 [symmetric])
-
-lemma iSuc_inject [simp]: "iSuc m = iSuc n \<longleftrightarrow> m = n"
-  by (simp add: iSuc_def split: inat.splits)
-
-lemma number_of_inat_inject [simp]:
-  "(number_of k \<Colon> inat) = number_of l \<longleftrightarrow> (number_of k \<Colon> nat) = number_of l"
-  by (simp add: number_of_inat_def)
-
-
-subsection {* Addition *}
-
-instantiation inat :: comm_monoid_add
-begin
-
-definition [nitpick_simp]:
-  "m + n = (case m of \<infinity> \<Rightarrow> \<infinity> | Fin m \<Rightarrow> (case n of \<infinity> \<Rightarrow> \<infinity> | Fin n \<Rightarrow> Fin (m + n)))"
-
-lemma plus_inat_simps [simp, code]:
-  "Fin m + Fin n = Fin (m + n)"
-  "\<infinity> + q = \<infinity>"
-  "q + \<infinity> = \<infinity>"
-  by (simp_all add: plus_inat_def split: inat.splits)
-
-instance proof
-  fix n m q :: inat
-  show "n + m + q = n + (m + q)"
-    by (cases n, auto, cases m, auto, cases q, auto)
-  show "n + m = m + n"
-    by (cases n, auto, cases m, auto)
-  show "0 + n = n"
-    by (cases n) (simp_all add: zero_inat_def)
-qed
-
-end
-
-lemma plus_inat_0 [simp]:
-  "0 + (q\<Colon>inat) = q"
-  "(q\<Colon>inat) + 0 = q"
-  by (simp_all add: plus_inat_def zero_inat_def split: inat.splits)
-
-lemma plus_inat_number [simp]:
-  "(number_of k \<Colon> inat) + number_of l = (if k < Int.Pls then number_of l
-    else if l < Int.Pls then number_of k else number_of (k + l))"
-  unfolding number_of_inat_def plus_inat_simps nat_arith(1) if_distrib [symmetric, of _ Fin] ..
-
-lemma iSuc_number [simp]:
-  "iSuc (number_of k) = (if neg (number_of k \<Colon> int) then 1 else number_of (Int.succ k))"
-  unfolding iSuc_number_of
-  unfolding one_inat_def number_of_inat_def Suc_nat_number_of if_distrib [symmetric] ..
-
-lemma iSuc_plus_1:
-  "iSuc n = n + 1"
-  by (cases n) (simp_all add: iSuc_Fin one_inat_def)
-  
-lemma plus_1_iSuc:
-  "1 + q = iSuc q"
-  "q + 1 = iSuc q"
-by (simp_all add: iSuc_plus_1 add_ac)
-
-lemma iadd_Suc: "iSuc m + n = iSuc (m + n)"
-by (simp_all add: iSuc_plus_1 add_ac)
-
-lemma iadd_Suc_right: "m + iSuc n = iSuc (m + n)"
-by (simp only: add_commute[of m] iadd_Suc)
-
-lemma iadd_is_0: "(m + n = (0::inat)) = (m = 0 \<and> n = 0)"
-by (cases m, cases n, simp_all add: zero_inat_def)
-
-subsection {* Multiplication *}
-
-instantiation inat :: comm_semiring_1
-begin
-
-definition times_inat_def [nitpick_simp]:
-  "m * n = (case m of \<infinity> \<Rightarrow> if n = 0 then 0 else \<infinity> | Fin m \<Rightarrow>
-    (case n of \<infinity> \<Rightarrow> if m = 0 then 0 else \<infinity> | Fin n \<Rightarrow> Fin (m * n)))"
-
-lemma times_inat_simps [simp, code]:
-  "Fin m * Fin n = Fin (m * n)"
-  "\<infinity> * \<infinity> = \<infinity>"
-  "\<infinity> * Fin n = (if n = 0 then 0 else \<infinity>)"
-  "Fin m * \<infinity> = (if m = 0 then 0 else \<infinity>)"
-  unfolding times_inat_def zero_inat_def
-  by (simp_all split: inat.split)
-
-instance proof
-  fix a b c :: inat
-  show "(a * b) * c = a * (b * c)"
-    unfolding times_inat_def zero_inat_def
-    by (simp split: inat.split)
-  show "a * b = b * a"
-    unfolding times_inat_def zero_inat_def
-    by (simp split: inat.split)
-  show "1 * a = a"
-    unfolding times_inat_def zero_inat_def one_inat_def
-    by (simp split: inat.split)
-  show "(a + b) * c = a * c + b * c"
-    unfolding times_inat_def zero_inat_def
-    by (simp split: inat.split add: left_distrib)
-  show "0 * a = 0"
-    unfolding times_inat_def zero_inat_def
-    by (simp split: inat.split)
-  show "a * 0 = 0"
-    unfolding times_inat_def zero_inat_def
-    by (simp split: inat.split)
-  show "(0::inat) \<noteq> 1"
-    unfolding zero_inat_def one_inat_def
-    by simp
-qed
-
-end
-
-lemma mult_iSuc: "iSuc m * n = n + m * n"
-  unfolding iSuc_plus_1 by (simp add: algebra_simps)
-
-lemma mult_iSuc_right: "m * iSuc n = m + m * n"
-  unfolding iSuc_plus_1 by (simp add: algebra_simps)
-
-lemma of_nat_eq_Fin: "of_nat n = Fin n"
-  apply (induct n)
-  apply (simp add: Fin_0)
-  apply (simp add: plus_1_iSuc iSuc_Fin)
-  done
-
-instance inat :: number_semiring
-proof
-  fix n show "number_of (int n) = (of_nat n :: inat)"
-    unfolding number_of_inat_def number_of_int of_nat_id of_nat_eq_Fin ..
-qed
-
-instance inat :: semiring_char_0 proof
-  have "inj Fin" by (rule injI) simp
-  then show "inj (\<lambda>n. of_nat n :: inat)" by (simp add: of_nat_eq_Fin)
-qed
-
-lemma imult_is_0[simp]: "((m::inat) * n = 0) = (m = 0 \<or> n = 0)"
-by(auto simp add: times_inat_def zero_inat_def split: inat.split)
-
-lemma imult_is_Infty: "((a::inat) * b = \<infinity>) = (a = \<infinity> \<and> b \<noteq> 0 \<or> b = \<infinity> \<and> a \<noteq> 0)"
-by(auto simp add: times_inat_def zero_inat_def split: inat.split)
-
-
-subsection {* Subtraction *}
-
-instantiation inat :: minus
-begin
-
-definition diff_inat_def:
-"a - b = (case a of (Fin x) \<Rightarrow> (case b of (Fin y) \<Rightarrow> Fin (x - y) | \<infinity> \<Rightarrow> 0)
-          | \<infinity> \<Rightarrow> \<infinity>)"
-
-instance ..
<