(** This file describes defineitions and properties of continuity on
[R] and on uniform spaces. It also contains many results about the
limit of a real function (predicates [is_lim] and [ex_lim] and total
function [Lim]). This limit may be either a real or an extended
real. *)

(** * Limit of fonctions *)

(** ** Definition *)

Definition is_lim (f : R -> R) (x l : Rbar) :=
  filterlim f (Rbar_locally' x) (Rbar_locally l).Definition is_lim' (f : R -> R) (x l : Rbar) :=
  match l with
    | Finite l =>
      forall eps : posreal, Rbar_locally' x (fun y => Rabs (f y - l) < eps)
    | p_infty => forall M : R, Rbar_locally' x (fun y => M < f y)
    | m_infty => forall M : R, Rbar_locally' x (fun y => f y < M)
  end.Definition ex_lim (f : R -> R) (x : Rbar) := exists l : Rbar, is_lim f x l.Definition ex_finite_lim (f : R -> R) (x : Rbar) := exists l : R, is_lim f x l.Definition Lim (f : R -> R) (x : Rbar) := Lim_seq (fun n => f (Rbar_loc_seq x n)).

(* Specifications: *)
Lemma is_lim_spec : forall f x l, is_lim' f x l <-> is_lim f x l.
Lemma is_lim_Reals_0 (f : R -> R) (x l : R) : is_lim f x l -> limit1_in f (fun y => y <> x) l x.
Lemma is_lim_Reals_1 (f : R -> R) (x l : R) : limit1_in f (fun y => y <> x) l x -> is_lim f x l.
Lemma is_lim_Reals (f : R -> R) (x l : R) : is_lim f x l <-> limit1_in f (fun y => y <> x) l x.
Lemma is_lim_comp' : forall {T} {F} {FF : @Filter T F} (f : T -> R) (g : R -> R) (x l : Rbar), filterlim f F (Rbar_locally x) -> is_lim g x l -> F (fun y => Finite (f y) <> x) -> filterlim (fun y => g (f y)) F (Rbar_locally l).
Lemma is_lim_comp_seq (f : R -> R) (u : nat -> R) (x l : Rbar) : is_lim f x l -> eventually (fun n => Finite (u n) <> x) -> is_lim_seq u x -> is_lim_seq (fun n => f (u n)) l.
Lemma is_lim_unique (f : R -> R) (x l : Rbar) : is_lim f x l -> Lim f x = l.
Lemma Lim_correct (f : R -> R) (x : Rbar) : ex_lim f x -> is_lim f x (Lim f x).
Lemma ex_finite_lim_correct (f : R -> R) (x : Rbar) : ex_finite_lim f x <-> ex_lim f x /\ is_finite (Lim f x).
Lemma Lim_correct' (f : R -> R) (x : Rbar) : ex_finite_lim f x -> is_lim f x (real (Lim f x)).
Lemma is_lim_ext_loc (f g : R -> R) (x l : Rbar) : Rbar_locally' x (fun y => f y = g y) -> is_lim f x l -> is_lim g x l.
Lemma ex_lim_ext_loc (f g : R -> R) (x : Rbar) : Rbar_locally' x (fun y => f y = g y) -> ex_lim f x -> ex_lim g x.
Lemma Lim_ext_loc (f g : R -> R) (x : Rbar) : Rbar_locally' x (fun y => f y = g y) -> Lim g x = Lim f x.
Lemma is_lim_ext (f g : R -> R) (x l : Rbar) : (forall y, f y = g y) -> is_lim f x l -> is_lim g x l.
Lemma ex_lim_ext (f g : R -> R) (x : Rbar) : (forall y, f y = g y) -> ex_lim f x -> ex_lim g x.
Lemma Lim_ext (f g : R -> R) (x : Rbar) : (forall y, f y = g y) -> Lim g x = Lim f x.
Lemma is_lim_comp (f g : R -> R) (x k l : Rbar) : is_lim f l k -> is_lim g x l -> Rbar_locally' x (fun y => Finite (g y) <> l) -> is_lim (fun x => f (g x)) x k.
Lemma ex_lim_comp (f g : R -> R) (x : Rbar) : ex_lim f (Lim g x) -> ex_lim g x -> Rbar_locally' x (fun y => Finite (g y) <> Lim g x) -> ex_lim (fun x => f (g x)) x.
Lemma Lim_comp (f g : R -> R) (x : Rbar) : ex_lim f (Lim g x) -> ex_lim g x -> Rbar_locally' x (fun y => Finite (g y) <> Lim g x) -> Lim (fun x => f (g x)) x = Lim f (Lim g x).
Lemma is_lim_id (x : Rbar) : is_lim (fun y => y) x x.
Lemma ex_lim_id (x : Rbar) : ex_lim (fun y => y) x.
Lemma Lim_id (x : Rbar) : Lim (fun y => y) x = x.
Lemma is_lim_const (a : R) (x : Rbar) : is_lim (fun _ => a) x a.
Lemma ex_lim_const (a : R) (x : Rbar) : ex_lim (fun _ => a) x.
Lemma Lim_const (a : R) (x : Rbar) : Lim (fun _ => a) x = a.
Lemma is_lim_opp (f : R -> R) (x l : Rbar) : is_lim f x l -> is_lim (fun y => - f y) x (Rbar_opp l).
Lemma ex_lim_opp (f : R -> R) (x : Rbar) : ex_lim f x -> ex_lim (fun y => - f y) x.
Lemma Lim_opp (f : R -> R) (x : Rbar) : Lim (fun y => - f y) x = Rbar_opp (Lim f x).
Lemma is_lim_plus (f g : R -> R) (x lf lg l : Rbar) : is_lim f x lf -> is_lim g x lg -> is_Rbar_plus lf lg l -> is_lim (fun y => f y + g y) x l.
Lemma is_lim_plus' (f g : R -> R) (x : Rbar) (lf lg : R) : is_lim f x lf -> is_lim g x lg -> is_lim (fun y => f y + g y) x (lf + lg).
Lemma ex_lim_plus (f g : R -> R) (x : Rbar) : ex_lim f x -> ex_lim g x -> ex_Rbar_plus (Lim f x) (Lim g x) -> ex_lim (fun y => f y + g y) x.
Lemma Lim_plus (f g : R -> R) (x : Rbar) : ex_lim f x -> ex_lim g x -> ex_Rbar_plus (Lim f x) (Lim g x) -> Lim (fun y => f y + g y) x = Rbar_plus (Lim f x) (Lim g x).
Lemma is_lim_minus (f g : R -> R) (x lf lg l : Rbar) : is_lim f x lf -> is_lim g x lg -> is_Rbar_minus lf lg l -> is_lim (fun y => f y - g y) x l.
Lemma is_lim_minus' (f g : R -> R) (x : Rbar) (lf lg : R) : is_lim f x lf -> is_lim g x lg -> is_lim (fun y => f y - g y) x (lf - lg).
Lemma ex_lim_minus (f g : R -> R) (x : Rbar) : ex_lim f x -> ex_lim g x -> ex_Rbar_minus (Lim f x) (Lim g x) -> ex_lim (fun y => f y - g y) x.
Lemma Lim_minus (f g : R -> R) (x : Rbar) : ex_lim f x -> ex_lim g x -> ex_Rbar_minus (Lim f x) (Lim g x) -> Lim (fun y => f y - g y) x = Rbar_minus (Lim f x) (Lim g x).
Lemma is_lim_inv (f : R -> R) (x l : Rbar) : is_lim f x l -> l <> 0 -> is_lim (fun y => / f y) x (Rbar_inv l).
Lemma ex_lim_inv (f : R -> R) (x : Rbar) : ex_lim f x -> Lim f x <> 0 -> ex_lim (fun y => / f y) x.
Lemma Lim_inv (f : R -> R) (x : Rbar) : ex_lim f x -> Lim f x <> 0 -> Lim (fun y => / f y) x = Rbar_inv (Lim f x).
Lemma is_lim_mult (f g : R -> R) (x lf lg : Rbar) : is_lim f x lf -> is_lim g x lg -> ex_Rbar_mult lf lg -> is_lim (fun y => f y * g y) x (Rbar_mult lf lg).
Lemma ex_lim_mult (f g : R -> R) (x : Rbar) : ex_lim f x -> ex_lim g x -> ex_Rbar_mult (Lim f x) (Lim g x) -> ex_lim (fun y => f y * g y) x.
Lemma Lim_mult (f g : R -> R) (x : Rbar) : ex_lim f x -> ex_lim g x -> ex_Rbar_mult (Lim f x) (Lim g x) -> Lim (fun y => f y * g y) x = Rbar_mult (Lim f x) (Lim g x).
Lemma is_lim_scal_l (f : R -> R) (a : R) (x l : Rbar) : is_lim f x l -> is_lim (fun y => a * f y) x (Rbar_mult a l).
Lemma ex_lim_scal_l (f : R -> R) (a : R) (x : Rbar) : ex_lim f x -> ex_lim (fun y => a * f y) x.
Lemma Lim_scal_l (f : R -> R) (a : R) (x : Rbar) : Lim (fun y => a * f y) x = Rbar_mult a (Lim f x).
Lemma is_lim_scal_r (f : R -> R) (a : R) (x l : Rbar) : is_lim f x l -> is_lim (fun y => f y * a) x (Rbar_mult l a).
Lemma ex_lim_scal_r (f : R -> R) (a : R) (x : Rbar) : ex_lim f x -> ex_lim (fun y => f y * a) x.
Lemma Lim_scal_r (f : R -> R) (a : R) (x : Rbar) : Lim (fun y => f y * a) x = Rbar_mult (Lim f x) a.
Lemma is_lim_div (f g : R -> R) (x lf lg : Rbar) : is_lim f x lf -> is_lim g x lg -> lg <> 0 -> ex_Rbar_div lf lg -> is_lim (fun y => f y / g y) x (Rbar_div lf lg).
Lemma ex_lim_div (f g : R -> R) (x : Rbar) : ex_lim f x -> ex_lim g x -> Lim g x <> 0 -> ex_Rbar_div (Lim f x) (Lim g x) -> ex_lim (fun y => f y / g y) x.
Lemma Lim_div (f g : R -> R) (x : Rbar) : ex_lim f x -> ex_lim g x -> Lim g x <> 0 -> ex_Rbar_div (Lim f x) (Lim g x) -> Lim (fun y => f y / g y) x = Rbar_div (Lim f x) (Lim g x).
Lemma is_lim_comp_lin (f : R -> R) (a b : R) (x l : Rbar) : is_lim f (Rbar_plus (Rbar_mult a x) b) l -> a <> 0 -> is_lim (fun y => f (a * y + b)) x l.
Lemma ex_lim_comp_lin (f : R -> R) (a b : R) (x : Rbar) : ex_lim f (Rbar_plus (Rbar_mult a x) b) -> ex_lim (fun y => f (a * y + b)) x.
Lemma Lim_comp_lin (f : R -> R) (a b : R) (x : Rbar) : ex_lim f (Rbar_plus (Rbar_mult a x) b) -> a <> 0 -> Lim (fun y => f (a * y + b)) x = Lim f (Rbar_plus (Rbar_mult a x) b).
Lemma is_lim_continuity (f : R -> R) (x : R) : continuity_pt f x -> is_lim f x (f x).
Lemma ex_lim_continuity (f : R -> R) (x : R) : continuity_pt f x -> ex_finite_lim f x.
Lemma Lim_continuity (f : R -> R) (x : R) : continuity_pt f x -> Lim f x = f x.
Lemma C0_extension_right {T : UniformSpace} (f : R -> T) lb (a b : R) : a < b -> (forall c : R, a < c < b -> filterlim f (locally c) (locally (f c))) -> (filterlim f (at_left b) (locally lb)) -> {g : R -> T | (forall c, a < c -> filterlim g (locally c) (locally (g c))) /\ (forall c : R, c < b -> g c = f c) /\ g b = lb}.
Lemma filterlim_Ropp_left (x : R) : filterlim Ropp (at_left x) (at_right (- x)).
Lemma filterlim_Ropp_right (x : R) : filterlim Ropp (at_right x) (at_left (- x)).
Lemma C0_extension_left {T : UniformSpace} (f : R -> T) la (a b : R) : a < b -> (forall c : R, a < c < b -> filterlim f (locally c) (locally (f c))) -> (filterlim f (at_right a) (locally la)) -> {g : R -> T | (forall c, c < b -> filterlim g (locally c) (locally (g c))) /\ (forall c : R, a < c -> g c = f c) /\ g a = la}.
Lemma C0_extension_lt {T : UniformSpace} (f : R -> T) la lb (a b : R) : a < b -> (forall c : R, a < c < b -> filterlim f (locally c) (locally (f c))) -> (filterlim f (at_right a) (locally la)) -> (filterlim f (at_left b) (locally lb)) -> {g : R -> T | (forall c, filterlim g (locally c) (locally (g c))) /\ (forall c : R, a < c < b -> g c = f c) /\ g a = la /\ g b = lb}.
Lemma C0_extension_le {T : UniformSpace} (f : R -> T) (a b : R) : (forall c : R, a <= c <= b -> filterlim f (locally c) (locally (f c))) -> {g : R -> T | (forall c, filterlim g (locally c) (locally (g c))) /\ (forall c : R, a <= c <= b -> g c = f c)}.
Lemma bounded_continuity {K : AbsRing} {V : NormedModule K} (f : R -> V) a b : (forall x, a <= x <= b -> filterlim f (locally x) (locally (f x))) -> {M : R | forall x, a <= x <= b -> norm (f x) < M}.
Lemma is_lim_le_loc (f g : R -> R) (x lf lg : Rbar) : Rbar_locally' x (fun y => f y <= g y) -> is_lim f x lf -> is_lim g x lg -> Rbar_le lf lg.
Lemma is_lim_le_p_loc (f g : R -> R) (x : Rbar) : Rbar_locally' x (fun y => f y <= g y) -> is_lim f x p_infty -> is_lim g x p_infty.
Lemma is_lim_le_m_loc (f g : R -> R) (x : Rbar) : Rbar_locally' x (fun y => g y <= f y) -> is_lim f x m_infty -> is_lim g x m_infty.
Lemma is_lim_le_le_loc (f g h : R -> R) (x : Rbar) (l : Rbar) : Rbar_locally' x (fun y => f y <= h y <= g y) -> is_lim f x l -> is_lim g x l -> is_lim h x l.
Lemma IVT_gen (f : R -> R) (a b y : R) : Ranalysis1.continuity f -> Rmin (f a) (f b) <= y <= Rmax (f a) (f b) -> { x : R | Rmin a b <= x <= Rmax a b /\ f x = y }.
Lemma IVT_Rbar_incr (f : R -> R) (a b la lb : Rbar) (y : R) : is_lim f a la -> is_lim f b lb -> (forall (x : R), Rbar_lt a x -> Rbar_lt x b -> continuity_pt f x) -> Rbar_lt a b -> Rbar_lt la y /\ Rbar_lt y lb -> {x : R | Rbar_lt a x /\ Rbar_lt x b /\ f x = y}.
Lemma IVT_Rbar_decr (f : R -> R) (a b la lb : Rbar) (y : R) : is_lim f a la -> is_lim f b lb -> (forall (x : R), Rbar_lt a x -> Rbar_lt x b -> continuity_pt f x) -> Rbar_lt a b -> Rbar_lt lb y /\ Rbar_lt y la -> {x : R | Rbar_lt a x /\ Rbar_lt x b /\ f x = y}.