(**
This file is part of the Coquelicot formalization of real
analysis in Coq: http://coquelicot.saclay.inria.fr/

Copyright (C) 2011-2015 Sylvie Boldo
#<br />#
Copyright (C) 2011-2015 Catherine Lelay
#<br />#
Copyright (C) 2011-2015 Guillaume Melquiond

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
COPYING file for more details.
*)

(** * This file describes basic missing facts about the standard library of reals and a few concerning ssreflect.seq. It also contains a definition of the [sign] function. *)

(** Tactic for changing the last argument of a property to an evar,
    in order to apply theorems modulo equality. *)

Ltac evar_last :=
  match goal with
  | |- ?f ?x =>
    let tx := type of x in
    let tx := eval simpl in tx in
    let tmp := fresh "tmp" in
    evar (tmp : tx) ;
    refine (@eq_ind tx tmp f _ x _) ;
    unfold tmp ; clear tmp
  end.

Require Import Reals mathcomp.ssreflect.ssreflect.
Require Import Psatz.

Module MyNat.

Lemma neq_succ_0 (n : nat) : S n <> 0.
(* Goal: forall (n : nat) (_ : forall (h h' : R) (l : Rlist), Rle (f (pos_Rl (cons h' (cons h l)) (S n))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l0 : Rlist) : Rlist := match l0 with | nil => nil | cons h0 t => cons (f h0) (app t) end) l))))) (h h' : R) (l : Rlist), Rle (f (pos_Rl (cons h' (cons h l)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l0 : Rlist) : Rlist := match l0 with | nil => nil | cons h0 t => cons (f h0) (app t) end) l)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
Proof.  move=> contrad.  exact: (le_Sn_0 n).  Qed.

Lemma sub_succ (n m : nat) : S n - S m = n - m.
Proof.  done.  Qed.

Lemma sub_succ_l (n m : nat) : n <= m -> S m - n = S (m - n).
Proof.  move=> h.  by rewrite minus_Sn_m.  Qed.

Lemma lt_neq (n m : nat) : n < m -> n <> m.
Proof.
(* Goal: forall _ : lt n m, not (@eq nat n m) *)
intros H ->.
(* Goal: False *)
exact (lt_irrefl m H).
Qed.

Lemma minus_0_le (n m : nat) : n <= m -> n - m = 0.
Proof.
(* Goal: Rle (f (pos_Rl (cons h'0 (cons h0 l0)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h'0) (cons (f h0) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l0)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
case: (eq_nat_dec n m) => [-> _ | h h'].
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by rewrite minus_diag.
apply: not_le_minus_0.
(* Goal: forall (n : nat) (_ : forall (h h' : R) (l : Rlist), Rle (f (pos_Rl (cons h' (cons h l)) (S n))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l0 : Rlist) : Rlist := match l0 with | nil => nil | cons h0 t => cons (f h0) (app t) end) l))))) (h h' : R) (l : Rlist), Rle (f (pos_Rl (cons h' (cons h l)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l0 : Rlist) : Rlist := match l0 with | nil => nil | cons h0 t => cons (f h0) (app t) end) l)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
move=> h''.
apply: h.
(* Goal: forall _ : le (S n) (S m), le n m *)
exact: le_antisym.
Qed.

Lemma sub_succ_r (n m : nat) : n - S m = pred (n - m).
Proof.
(* Goal: Rle (f (pos_Rl (cons h'0 (cons h0 l0)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h'0) (cons (f h0) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l0)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
case: n => [// | n ].
(* Goal: @eq nat (Init.Nat.sub (S n) (S m)) (Init.Nat.pred (Init.Nat.sub (S n) m)) *)
case: (le_le_S_dec m n) => h; rewrite sub_succ.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite -minus_Sn_m //=.
(* Goal: forall (n : nat) (_ : forall (h h' : R) (l : Rlist), Rle (f (pos_Rl (cons h' (cons h l)) (S n))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l0 : Rlist) : Rlist := match l0 with | nil => nil | cons h0 t => cons (f h0) (app t) end) l))))) (h h' : R) (l : Rlist), Rle (f (pos_Rl (cons h' (cons h l)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l0 : Rlist) : Rlist := match l0 with | nil => nil | cons h0 t => cons (f h0) (app t) end) l)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
move: (le_S (S n) m h) => /le_S_n h'.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
by rewrite (minus_0_le n m h') (minus_0_le (S n) m h).
Qed.

Lemma sub_add (n m : nat) : n <= m -> m - n + n = m.
Proof.
(* Goal: Rle (f (pos_Rl (cons h' (cons h l)) (Init.Nat.pred (Rlength (cons h' (cons h l)))))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) *)
elim: m => [/le_n_0_eq // | m ih h].
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
by rewrite plus_comm le_plus_minus_r.
Qed.

Lemma le_pred_le_succ (n m : nat) : pred n <= m <-> n <= S m.
Proof.
(* Goal: Rle (f (pos_Rl (cons h'0 (cons h0 l0)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h'0) (cons (f h0) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l0)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
case: n m => /= [ | n m].
(* Goal: forall m : nat, iff (le O m) (le O (S m)) *)
(* Goal: iff (le n m) (le (S n) (S m)) *)
  split=> _; exact: le_0_n.
(* Goal: and (Rle (Rmin a b) x) (Rle x (Rmax a b)) *)
split.
(* Goal: forall _ : le (S n) (S m), le n m *)
  exact: le_n_S.
(* Goal: forall _ : le (S n) (S m), le n m *)
exact: le_S_n.
Qed.

End MyNat.

Require Import Even Div2.
Require Import mathcomp.ssreflect.seq mathcomp.ssreflect.ssrbool.

Open Scope R_scope.

(** * Integers *)

(** Integer part in Z *)

Lemma floor_ex : forall x : R, {n : Z | IZR n <= x < IZR n + 1}.
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  intros.
(* Goal: and (Rle (Rmin a b) x) (Rle x (Rmax a b)) *)
  exists (up (x-1)) ; split.
(* Goal: Rle (IZR (up (Rminus x (IZR (Zpos xH))))) x *)
(* Goal: Rlt x (Rplus (IZR (up (Rminus x (IZR (Zpos xH))))) (IZR (Zpos xH))) *)
  assert (Rw : x = 1 + (x-1)) ; [ring | rewrite {2}Rw => {Rw}].
  assert (Rw :IZR (up (x - 1))  = (IZR (up (x - 1)) - (x - 1)) + (x-1)) ;
    [ring | rewrite Rw ; clear Rw].
(* Goal: Rle (Rplus (Rminus (IZR (up (Rminus x (IZR (Zpos xH))))) (Rminus x (IZR (Zpos xH)))) (Rminus x (IZR (Zpos xH)))) (Rplus (IZR (Zpos xH)) (Rminus x (IZR (Zpos xH)))) *)
(* Goal: Rlt x (Rplus (IZR (up (Rminus x (IZR (Zpos xH))))) (IZR (Zpos xH))) *)
  apply Rplus_le_compat_r, (proj2 (archimed _)).
(* Goal: Rlt x (Rplus (IZR (up (Rminus x (IZR (Zpos xH))))) (IZR (Zpos xH))) *)
  assert (Rw : x = (x-1) + 1) ; [ring | rewrite {1}Rw ; clear Rw].
(* Goal: Rlt (Rplus (Rminus x (IZR (Zpos xH))) (IZR (Zpos xH))) (Rplus (IZR (up (Rminus x (IZR (Zpos xH))))) (IZR (Zpos xH))) *)
  apply Rplus_lt_compat_r, (proj1 (archimed _)).
Qed.
Definition floor x := proj1_sig (floor_ex x).

Lemma floor1_ex : forall x : R, {n : Z | IZR n < x <= IZR n + 1}.
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  intros.
(* Goal: @sig Z (fun n : Z => and (Rlt (IZR n) x) (Rle x (Rplus (IZR n) (IZR (Zpos xH))))) *)
  destruct (floor_ex x) as (n,(Hn1,Hn2)).
(* Goal: @sig Z (fun n : Z => and (Rlt (IZR n) x) (Rle x (Rplus (IZR n) (IZR (Zpos xH))))) *)
  destruct (Rle_lt_or_eq_dec (IZR n) x Hn1).
(* Goal: and (Rle (Rmin a b) x) (Rle x (Rmax a b)) *)
  exists n ; split.
(* Goal: Rle a b *)
(* Goal: forall (x : R) (_ : and (Rlt a x) (Rlt x b)), @eq R (f x) (g x) *)
(* Goal: @eq R (@RiemannInt f a b pr_f) (@RiemannInt g a b pr_g) *)
  apply r.
(* Goal: Rle x (Rplus (INR n) (IZR (Zpos xH))) *)
(* Goal: @sig nat (fun n : nat => and (Rlt (INR n) x) (Rle x (Rplus (INR n) (IZR (Zpos xH))))) *)
  apply Rlt_le, Hn2.
(* Goal: and (Rle (Rmin a b) x) (Rle x (Rmax a b)) *)
  exists (Zpred n) ; rewrite <- e ; split.
(* Goal: Rlt (IZR (Z.pred n)) (IZR n) *)
(* Goal: Rle (IZR n) (Rplus (IZR (Z.pred n)) (IZR (Zpos xH))) *)
  apply IZR_lt, Zlt_pred.
(* Goal: Rle (IZR n) (Rplus (IZR (Z.pred n)) (IZR (Zpos xH))) *)
  rewrite <- (succ_IZR), <-Zsucc_pred ; apply Rle_refl.
Qed.
Definition floor1 x := proj1_sig (floor1_ex x).

(** Interger part in nat *)

Lemma nfloor_ex : forall x : R, 0 <= x -> {n : nat | INR n <= x < INR n + 1}.
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  intros.
(* Goal: @sig nat (fun n : nat => and (Rle (INR n) x) (Rlt x (Rplus (INR n) (IZR (Zpos xH))))) *)
  destruct (floor_ex x) as (m,Hm).
(* Goal: @sig nat (fun n : nat => and (Rle (INR n) x) (Rlt x (Rplus (INR n) (IZR (Zpos xH))))) *)
  destruct (Z_lt_le_dec m 0) as [z|z].
(* Goal: @sig nat (fun n : nat => and (Rle (INR n) x) (Rlt x (Rplus (INR n) (IZR (Zpos xH))))) *)
(* Goal: @sig nat (fun n : nat => and (Rle (INR n) x) (Rlt x (Rplus (INR n) (IZR (Zpos xH))))) *)
  apply Zlt_le_succ in z.
(* Goal: @sig nat (fun n : nat => and (Rle (INR n) x) (Rlt x (Rplus (INR n) (IZR (Zpos xH))))) *)
  contradict z.
(* Goal: not (Z.le Z0 (Zneg p)) *)
  apply Zlt_not_le.
(* Goal: Z.lt Z0 (Z.succ m) *)
(* Goal: @sig nat (fun n : nat => and (Rle (INR n) x) (Rlt x (Rplus (INR n) (IZR (Zpos xH))))) *)
  apply lt_IZR.
(* Goal: Rlt (IZR Z0) (IZR (Z.succ m)) *)
(* Goal: @sig nat (fun n : nat => and (Rle (INR n) x) (Rlt x (Rplus (INR n) (IZR (Zpos xH))))) *)
  apply Rle_lt_trans with (1 := H).
(* Goal: Rlt x (IZR (Z.succ m)) *)
(* Goal: @sig nat (fun n : nat => and (Rle (INR n) x) (Rlt x (Rplus (INR n) (IZR (Zpos xH))))) *)
  rewrite succ_IZR ; apply Hm.
(* Goal: @sig nat (fun n : nat => and (Rle (INR n) x) (Rlt x (Rplus (INR n) (IZR (Zpos xH))))) *)
  destruct m.
(* Goal: @sig nat (fun n : nat => and (Rle (INR n) x) (Rlt x (Rplus (INR n) (IZR (Zpos xH))))) *)
(* Goal: @sig nat (fun n : nat => and (Rle (INR n) x) (Rlt x (Rplus (INR n) (IZR (Zpos xH))))) *)
(* Goal: @sig nat (fun n : nat => and (Rle (INR n) x) (Rlt x (Rplus (INR n) (IZR (Zpos xH))))) *)
  exists O ; apply Hm.
(* Goal: @sig nat (fun n : nat => and (Rle (INR n) x) (Rlt x (Rplus (INR n) (IZR (Zpos xH))))) *)
(* Goal: @sig nat (fun n : nat => and (Rle (INR n) x) (Rlt x (Rplus (INR n) (IZR (Zpos xH))))) *)
  exists (nat_of_P p).
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite INR_IZR_INZ.
(* Goal: and (Rle (IZR (Z.of_nat (Pos.to_nat p))) x) (Rlt x (Rplus (IZR (Z.of_nat (Pos.to_nat p))) (IZR (Zpos xH)))) *)
(* Goal: @sig nat (fun n : nat => and (Rle (INR n) x) (Rlt x (Rplus (INR n) (IZR (Zpos xH))))) *)
  rewrite <- Zpos_eq_Z_of_nat_o_nat_of_P.
(* Goal: and (Rle (IZR (Zpos p)) x) (Rlt x (Rplus (IZR (Zpos p)) (IZR (Zpos xH)))) *)
(* Goal: @sig nat (fun n : nat => and (Rle (INR n) x) (Rlt x (Rplus (INR n) (IZR (Zpos xH))))) *)
  apply Hm.
(* Goal: @sig nat (fun n : nat => and (Rle (INR n) x) (Rlt x (Rplus (INR n) (IZR (Zpos xH))))) *)
  contradict z.
(* Goal: not (Z.le Z0 (Zneg p)) *)
  apply Zlt_not_le.
(* Goal: Z.lt (Zneg p) Z0 *)
  apply Zlt_neg_0.
Qed.
Definition nfloor x pr := proj1_sig (nfloor_ex x pr).

Lemma nfloor1_ex : forall x : R, 0 < x -> {n : nat | INR n < x <= INR n + 1}.
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  intros.
(* Goal: @sig nat (fun n : nat => and (Rlt (INR n) x) (Rle x (Rplus (INR n) (IZR (Zpos xH))))) *)
  destruct (nfloor_ex x (Rlt_le _ _ H)) as (n,(Hn1,Hn2)).
(* Goal: @sig nat (fun n : nat => and (Rlt (INR n) x) (Rle x (Rplus (INR n) (IZR (Zpos xH))))) *)
  destruct (Rle_lt_or_eq_dec (INR n) x Hn1).
(* Goal: and (Rle (Rmin a b) x) (Rle x (Rmax a b)) *)
  exists n ; split.
(* Goal: Rle a b *)
(* Goal: forall (x : R) (_ : and (Rlt a x) (Rlt x b)), @eq R (f x) (g x) *)
(* Goal: @eq R (@RiemannInt f a b pr_f) (@RiemannInt g a b pr_g) *)
  apply r.
(* Goal: Rle x (Rplus (INR n) (IZR (Zpos xH))) *)
(* Goal: @sig nat (fun n : nat => and (Rlt (INR n) x) (Rle x (Rplus (INR n) (IZR (Zpos xH))))) *)
  apply Rlt_le, Hn2.
(* Goal: @sig nat (fun n : nat => and (Rlt (INR n) x) (Rle x (Rplus (INR n) (IZR (Zpos xH))))) *)
  destruct n.
(* Goal: not (Rlt a b) *)
(* Goal: @eq R (Rmult (IZR (Zneg xH)) (Rminus (Rmax a b) (Rmin a b))) (Rminus b a) *)
  contradict H.
(* Goal: not (Rlt (IZR Z0) x) *)
(* Goal: @sig nat (fun n : nat => and (Rlt (INR n) x) (Rle x (Rplus (INR n) (IZR (Zpos xH))))) *)
  rewrite <- e ; simpl ; apply Rlt_irrefl.
(* Goal: and (Rle (Rmin a b) x) (Rle x (Rmax a b)) *)
  exists n ; rewrite <- e ; split.
(* Goal: Rlt (INR n) (INR (S n)) *)
(* Goal: Rle (INR (S n)) (Rplus (INR n) (IZR (Zpos xH))) *)
  apply lt_INR, lt_n_Sn.
(* Goal: Rle (INR (S n)) (Rplus (INR n) (IZR (Zpos xH))) *)
  rewrite <- (S_INR) ; apply Rle_refl.
Qed.
Definition nfloor1 x pr := proj1_sig (nfloor1_ex x pr).

(** More theorems about INR *)

Lemma INRp1_pos : forall n, 0 < INR n + 1.
Proof.
(* Goal: forall n : nat, Rlt (IZR Z0) (Rplus (INR n) (IZR (Zpos xH))) *)
intros N.
(* Goal: Rlt (IZR Z0) (Rplus (INR N) (IZR (Zpos xH))) *)
rewrite <- S_INR.
(* Goal: Rlt (IZR Z0) (INR (S N)) *)
apply lt_0_INR.
(* Goal: lt O (S N) *)
apply lt_0_Sn.
Qed.

Lemma Rlt_nat (x : R) : (exists n : nat, x = INR (S n)) -> 0 < x.
Proof.
(* Goal: forall _ : @ex nat (fun n : nat => @eq R x (INR (S n))), Rlt (IZR Z0) x *)
  intro H ; destruct H.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite H S_INR.
(* Goal: Rlt (IZR Z0) (Rplus (INR x0) (IZR (Zpos xH))) *)
  apply INRp1_pos.
Qed.

Lemma Rle_pow_lin (a : R) (n : nat) :
  0 <= a -> 1 + INR n * a <= (1 + a) ^ n.
Proof.
(* Goal: forall _ : Rle (IZR Z0) a, Rle (Rplus (IZR (Zpos xH)) (Rmult (INR n) a)) (pow (Rplus (IZR (Zpos xH)) a) n) *)
  intro Ha.
(* Goal: Rle (Rplus (IZR (Zpos xH)) (Rmult (INR n) a)) (pow (Rplus (IZR (Zpos xH)) a) n) *)
  induction n.
(* Goal: Rle (Rplus (IZR (Zpos xH)) (Rmult (INR O) a)) (pow (Rplus (IZR (Zpos xH)) a) O) *)
(* Goal: Rle (Rplus (IZR (Zpos xH)) (Rmult (INR (S n)) a)) (pow (Rplus (IZR (Zpos xH)) a) (S n)) *)
  apply Req_le ; simpl ; ring.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite S_INR.
(* Goal: Rle (Rplus (IZR (Zpos xH)) (Rmult (Rplus (INR n) (IZR (Zpos xH))) a)) (pow (Rplus (IZR (Zpos xH)) a) (S n)) *)
  replace (1 + (INR n + 1) * a) with ((1 + INR n * a) + a) by ring.
(* Goal: Rle (Rplus (Rplus (IZR (Zpos xH)) (Rmult (INR n) a)) a) (pow (Rplus (IZR (Zpos xH)) a) (S n)) *)
  apply Rle_trans with ((1 + a) ^ n + a).
(* Goal: Rle (Rminus z y) (Rminus x y) *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
  apply Rplus_le_compat_r.
(* Goal: Rle (Rplus (IZR (Zpos xH)) (Rmult (INR n) a)) (pow (Rplus (IZR (Zpos xH)) a) n) *)
(* Goal: Rle (Rplus (pow (Rplus (IZR (Zpos xH)) a) n) a) (pow (Rplus (IZR (Zpos xH)) a) (S n)) *)
  exact IHn.
(* Goal: Rle (Rplus (pow (Rplus (IZR (Zpos xH)) a) n) a) (pow (Rplus (IZR (Zpos xH)) a) (S n)) *)
  replace ((1+a)^(S n)) with ((1+a)^n + a * (1+a)^n) by (simpl ; ring).
(* Goal: Rle (Rplus y (Rplus (Ropp y) x)) (Rplus y z) *)
(* Goal: and (Rle (Ropp z) (Rminus x y)) (Rle (Rminus x y) z) *)
(* Goal: Rle (Rabs (Rminus x y)) z *)
  apply Rplus_le_compat_l.
(* Goal: iff (Rlt a b) (Rlt (IZR Z0) (Rminus b a)) *)
  pattern a at 1.
(* Goal: (fun r : R => Rle r (Rmult a (pow (Rplus (IZR (Zpos xH)) a) n))) a *)
  rewrite <- (Rmult_1_r a).
(* Goal: Rle (Rmult a (IZR (Zpos xH))) (Rmult a (pow (Rplus (IZR (Zpos xH)) a) n)) *)
  apply Rmult_le_compat_l.
(* Goal: Rle (IZR Z0) a *)
  exact Ha.
(* Goal: Rle (IZR (Zpos xH)) (pow (Rplus (IZR (Zpos xH)) a) n) *)
  clear IHn.
(* Goal: Rle (IZR (Zpos xH)) (pow (Rplus (IZR (Zpos xH)) a) n) *)
  apply pow_R1_Rle.
(* Goal: Rle (IZR (Zpos xH)) (Rplus (IZR (Zpos xH)) a) *)
  pattern 1 at 1.
(* Goal: (fun r : R => Rle r (Rplus (IZR (Zpos xH)) a)) (IZR (Zpos xH)) *)
  rewrite <- (Rplus_0_r 1).
(* Goal: Rle (Rplus y (Rplus (Ropp y) x)) (Rplus y z) *)
(* Goal: and (Rle (Ropp z) (Rminus x y)) (Rle (Rminus x y) z) *)
(* Goal: Rle (Rabs (Rminus x y)) z *)
  apply Rplus_le_compat_l.
(* Goal: Rle (IZR Z0) a *)
  exact Ha.
Qed.

Lemma C_n_n: forall n, C n n = 1.
Proof.
(* Goal: forall n : nat, @eq R (C n O) (IZR (Zpos xH)) *)
intros n; unfold C.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
rewrite minus_diag.
(* Goal: @eq R (@last R (IZR Z0) l) (Rplus a (Rmult (INR n) (pos eps))) *)
(* Goal: @eq R (Rplus a (Rmult (INR n) (pos eps))) b *)
(* Goal: forall (i : nat) (_ : lt (S i) (@size R l)), and (Rlt (@nth R (IZR Z0) l i) (@nth R (IZR Z0) l (S i))) (Rle (@nth R (IZR Z0) l (S i)) (Rplus (@nth R (IZR Z0) l i) (pos eps))) *)
(* Goal: @sig (list R) (fun l : list R => and (@eq R (@head R (IZR Z0) l) a) (and (@eq R (@last R (IZR Z0) l) b) (forall (i : nat) (_ : lt (S i) (@size R l)), and (Rlt (@nth R (IZR Z0) l i) (@nth R (IZR Z0) l (S i))) (Rle (@nth R (IZR Z0) l (S i)) (Rplus (@nth R (IZR Z0) l i) (pos eps)))))) *)
(* Goal: Rle (IZR Z0) (Rdiv (Rminus b a) (pos eps)) *)
simpl.
(* Goal: @eq R (Rminus (Rdiv a b) c) (Rdiv (Rminus a (Rmult b c)) b) *)
field.
(* Goal: not (@eq R (INR (fact n)) (IZR Z0)) *)
apply INR_fact_neq_0.
Qed.

Lemma C_n_0: forall n, C n 0 = 1.
Proof.
(* Goal: forall n : nat, @eq R (C n O) (IZR (Zpos xH)) *)
intros n; unfold C.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
rewrite - minus_n_O.
(* Goal: @eq R (@last R (IZR Z0) l) (Rplus a (Rmult (INR n) (pos eps))) *)
(* Goal: @eq R (Rplus a (Rmult (INR n) (pos eps))) b *)
(* Goal: forall (i : nat) (_ : lt (S i) (@size R l)), and (Rlt (@nth R (IZR Z0) l i) (@nth R (IZR Z0) l (S i))) (Rle (@nth R (IZR Z0) l (S i)) (Rplus (@nth R (IZR Z0) l i) (pos eps))) *)
(* Goal: @sig (list R) (fun l : list R => and (@eq R (@head R (IZR Z0) l) a) (and (@eq R (@last R (IZR Z0) l) b) (forall (i : nat) (_ : lt (S i) (@size R l)), and (Rlt (@nth R (IZR Z0) l i) (@nth R (IZR Z0) l (S i))) (Rle (@nth R (IZR Z0) l (S i)) (Rplus (@nth R (IZR Z0) l i) (pos eps)))))) *)
(* Goal: Rle (IZR Z0) (Rdiv (Rminus b a) (pos eps)) *)
simpl.
(* Goal: @eq R (Rminus (Rdiv a b) c) (Rdiv (Rminus a (Rmult b c)) b) *)
field.
(* Goal: not (@eq R (INR (fact n)) (IZR Z0)) *)
apply INR_fact_neq_0.
Qed.

Fixpoint pow2 (n : nat) : nat :=
  match n with
    | O => 1%nat
    | S n => (2 * pow2 n)%nat
  end.

Lemma pow2_INR (n : nat) : INR (pow2 n) = 2^n.
Proof.
  elim: n => //= n IH ;
(* Goal: @eq R (Rminus (Rdiv a b) c) (Rdiv (Rminus a (Rmult b c)) b) *)
  rewrite ?plus_INR ?IH /= ; field.
Qed.

Lemma pow2_pos (n : nat) : (0 < pow2 n)%nat.
Proof.
(* Goal: lt O (pow2 n) *)
  apply INR_lt ; rewrite pow2_INR.
(* Goal: Rlt (INR O) (pow (IZR (Zpos (xO xH))) n) *)
  apply pow_lt, Rlt_0_2.
Qed.

(** * Rinv *)

Lemma Rinv_le_contravar :
  forall x y, 0 < x -> x <= y -> / y <= / x.
Proof.
(* Goal: forall (x y : R) (_ : Rlt (IZR Z0) x) (_ : Rle x y), Rle (Rinv y) (Rinv x) *)
intros x y H1 [H2|H2].
(* Goal: Rle (Rabs (Rminus x y)) eps *)
(* Goal: Rle (IZR Z0) (Rabs (Rminus x y)) *)
apply Rlt_le.
(* Goal: Rlt (Rinv y) (Rinv x) *)
(* Goal: Rle (Rinv y) (Rinv x) *)
apply Rinv_lt_contravar with (2 := H2).
(* Goal: Rlt (IZR Z0) (Rmult x y) *)
(* Goal: Rle (Rinv y) (Rinv x) *)
apply Rmult_lt_0_compat with (1 := H1).
(* Goal: Rlt (IZR Z0) y *)
(* Goal: Rle (Rinv y) (Rinv x) *)
now apply Rlt_trans with x.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
rewrite H2.
(* Goal: Rle (pos_Rl nil i) (Rl_max (IZR Z0) nil) *)
(* Goal: forall (r : R) (r0 : Rlist), Rle (pos_Rl (cons r r0) i) (Rl_max (IZR Z0) (cons r r0)) *)
(* Goal: open_interval (pos_Rl (cons h' (cons h l)) i) (pos_Rl (cons h' (cons h l)) (S i)) x *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
apply Rle_refl.
Qed.

Lemma Rinv_lt_cancel (x y : R) :
  0 < y -> / y < / x -> x < y.
Proof.
(* Goal: forall (_ : Rlt (IZR Z0) y) (_ : Rlt (Rinv y) (Rinv x)), Rlt x y *)
  intro Hy ; move/Rlt_not_le => Hxy.
(* Goal: Rlt x y *)
  apply Rnot_le_lt ; contradict Hxy.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by apply Rinv_le_contravar.
Qed.

(** * Rdiv *)
(** Rewritings *)

Lemma Rdiv_1 : forall x : R, x / 1 = x.
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  intros.
  unfold Rdiv ;
  rewrite Rinv_1 Rmult_1_r.
(* Goal: @eq R (Rmult (Rabs a) (Rinv (Rabs b))) (Rmult (Rabs a) (Rinv (Rabs b))) *)
(* Goal: not (@eq R b (IZR Z0)) *)
  reflexivity.
Qed.

Lemma Rdiv_plus : forall a b c d : R, b <> 0 -> d <> 0 ->
  a / b + c / d = (a * d + c * b) / (b * d).
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  intros.
(* Goal: @eq R (Rminus (Rdiv a b) c) (Rdiv (Rminus a (Rmult b c)) b) *)
  field.
(* Goal: and (Rle (Rmin a b) x) (Rle x (Rmax a b)) *)
  split.
(* Goal: Rlt (Rplus x (Ropp y)) z *)
(* Goal: and (Rlt (Ropp z) (Rminus x y)) (Rlt (Rminus x y) z) *)
(* Goal: Rlt (Rabs (Rminus x y)) z *)
  apply H0.
(* Goal: Rle z x *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
  apply H.
Qed.

Lemma Rdiv_minus : forall a b c d : R, b <> 0 -> d <> 0 ->
  a / b - c / d = (a * d - c * b) / (b * d).
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  intros.
(* Goal: @eq R (Rminus (Rdiv a b) c) (Rdiv (Rminus a (Rmult b c)) b) *)
  field.
(* Goal: and (Rle (Rmin a b) x) (Rle x (Rmax a b)) *)
  split.
(* Goal: Rlt (Rplus x (Ropp y)) z *)
(* Goal: and (Rlt (Ropp z) (Rminus x y)) (Rlt (Rminus x y) z) *)
(* Goal: Rlt (Rabs (Rminus x y)) z *)
  apply H0.
(* Goal: Rle z x *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
  apply H.
Qed.

(** Order *)

Lemma Rplus_lt_reg_l (x y z : R) : x + y < x + z -> y < z.
Proof.
first [
  (* 8.4 *)
  exact: Rplus_lt_reg_r
|
  (* 8.5 *)
  exact: Rplus_lt_reg_l
].
Qed.

Lemma Rplus_lt_reg_r (x y z : R) : y + x < z + x -> y < z.
Proof.
first [
  (* 8.4 *)
  intro H ;
  apply Rplus_lt_reg_r with x ;
  now rewrite 2!(Rplus_comm x)
|
  (* 8.5 *)
  exact: Rplus_lt_reg_r
].
Qed.

Lemma Rle_div_l : forall a b c, c > 0 -> (a / c <= b <-> a <= b * c).
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  split ; intros.
(* Goal: Rlt a (Rmult b c) *)
(* Goal: Rlt (Rdiv a c) b *)
  replace a with ((a/c) * c) by (field ; apply Rgt_not_eq, H).
(* Goal: Rle (Rmult a c) (Rmult (Rdiv b c) c) *)
  apply Rmult_le_compat_r.
(* Goal: Rle b a *)
(* Goal: Rle (Ropp a) (Ropp b) *)
  apply Rlt_le, H.
(* Goal: Rlt (Rplus x (Ropp y)) z *)
(* Goal: and (Rlt (Ropp z) (Rminus x y)) (Rlt (Rminus x y) z) *)
(* Goal: Rlt (Rabs (Rminus x y)) z *)
  apply H0.

  replace b with ((b*c)/c)  by (field ; apply Rgt_not_eq, H).
(* Goal: Rle (Rmult a c) (Rmult (Rdiv b c) c) *)
  apply Rmult_le_compat_r.
(* Goal: Rle (IZR Z0) (Rinv c) *)
(* Goal: Rle (Rmult a c) b *)
(* Goal: Rle (Rmult a c) b *)
  apply Rlt_le, Rinv_0_lt_compat, H.
(* Goal: Rlt (Rplus x (Ropp y)) z *)
(* Goal: and (Rlt (Ropp z) (Rminus x y)) (Rlt (Rminus x y) z) *)
(* Goal: Rlt (Rabs (Rminus x y)) z *)
  apply H0.
Qed.

Lemma Rle_div_r : forall a b c, c > 0 -> (a * c <= b <-> a <= b / c).
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  split ; intros.
(* Goal: Rlt a (Rdiv b c) *)
(* Goal: Rlt (Rmult a c) b *)
  replace a with ((a * c) / c) by (field ; apply Rgt_not_eq, H).
(* Goal: Rle (Rmult a c) (Rmult (Rdiv b c) c) *)
  apply Rmult_le_compat_r.
(* Goal: Rle (IZR Z0) (Rinv c) *)
(* Goal: Rle (Rmult a c) b *)
(* Goal: Rle (Rmult a c) b *)
  apply Rlt_le, Rinv_0_lt_compat, H.
(* Goal: Rlt (Rplus x (Ropp y)) z *)
(* Goal: and (Rlt (Ropp z) (Rminus x y)) (Rlt (Rminus x y) z) *)
(* Goal: Rlt (Rabs (Rminus x y)) z *)
  apply H0.

  replace b with ((b / c) * c)  by (field ; apply Rgt_not_eq, H).
(* Goal: Rle (Rmult a c) (Rmult (Rdiv b c) c) *)
  apply Rmult_le_compat_r.
(* Goal: Rle b a *)
(* Goal: Rle (Ropp a) (Ropp b) *)
  apply Rlt_le, H.
(* Goal: Rlt (Rplus x (Ropp y)) z *)
(* Goal: and (Rlt (Ropp z) (Rminus x y)) (Rlt (Rminus x y) z) *)
(* Goal: Rlt (Rabs (Rminus x y)) z *)
  apply H0.
Qed.

Lemma Rlt_div_l : forall a b c, c > 0 -> (a / c < b <-> a < b*c).
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  split ; intros.
(* Goal: Rlt a (Rmult b c) *)
(* Goal: Rlt (Rdiv a c) b *)
  replace a with ((a/c) * c) by (field ; apply Rgt_not_eq, H).
(* Goal: Rlt (Rmult a c) (Rmult (Rdiv b c) c) *)
  apply Rmult_lt_compat_r.
(* Goal: Rle z x *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
  apply H.
(* Goal: Rlt (Rplus x (Ropp y)) z *)
(* Goal: and (Rlt (Ropp z) (Rminus x y)) (Rlt (Rminus x y) z) *)
(* Goal: Rlt (Rabs (Rminus x y)) z *)
  apply H0.

  replace b with ((b*c)/c)  by (field ; apply Rgt_not_eq, H).
(* Goal: Rlt (Rmult a c) (Rmult (Rdiv b c) c) *)
  apply Rmult_lt_compat_r.
(* Goal: Rlt (IZR Z0) (Rinv c) *)
(* Goal: Rlt (Rmult a c) b *)
(* Goal: Rlt (Rmult a c) b *)
  apply Rinv_0_lt_compat, H.
(* Goal: Rlt (Rplus x (Ropp y)) z *)
(* Goal: and (Rlt (Ropp z) (Rminus x y)) (Rlt (Rminus x y) z) *)
(* Goal: Rlt (Rabs (Rminus x y)) z *)
  apply H0.
Qed.

Lemma Rlt_div_r : forall a b c, c > 0 -> (a * c < b <-> a < b / c).
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  split ; intros.
(* Goal: Rlt a (Rdiv b c) *)
(* Goal: Rlt (Rmult a c) b *)
  replace a with ((a * c) / c) by (field ; apply Rgt_not_eq, H).
(* Goal: Rlt (Rmult a c) (Rmult (Rdiv b c) c) *)
  apply Rmult_lt_compat_r.
(* Goal: Rlt (IZR Z0) (Rinv c) *)
(* Goal: Rlt (Rmult a c) b *)
(* Goal: Rlt (Rmult a c) b *)
  apply Rinv_0_lt_compat, H.
(* Goal: Rlt (Rplus x (Ropp y)) z *)
(* Goal: and (Rlt (Ropp z) (Rminus x y)) (Rlt (Rminus x y) z) *)
(* Goal: Rlt (Rabs (Rminus x y)) z *)
  apply H0.

  replace b with ((b / c) * c)  by (field ; apply Rgt_not_eq, H).
(* Goal: Rlt (Rmult a c) (Rmult (Rdiv b c) c) *)
  apply Rmult_lt_compat_r.
(* Goal: Rle z x *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
  apply H.
(* Goal: Rlt (Rplus x (Ropp y)) z *)
(* Goal: and (Rlt (Ropp z) (Rminus x y)) (Rlt (Rminus x y) z) *)
(* Goal: Rlt (Rabs (Rminus x y)) z *)
  apply H0.
Qed.

Lemma Rdiv_lt_0_compat : forall r1 r2 : R, 0 < r1 -> 0 < r2 -> 0 < r1 / r2.
Proof.
(* Goal: forall (r1 r2 : R) (_ : Rlt (IZR Z0) r1) (_ : Rlt (IZR Z0) r2), Rlt (IZR Z0) (Rdiv r1 r2) *)
  intros r1 r2 H1 H2.
(* Goal: Rlt (Rmult (INR n) (pos eps)) (Rminus b a) *)
(* Goal: @eq nat n i *)
(* Goal: Rlt (@nth R (IZR Z0) (@mkseq R (fun k : nat => Rplus a (Rmult (INR k) (pos eps))) (S n)) i) (IZR Z0) *)
(* Goal: Rle (@nth R (IZR Z0) l (S i)) (Rplus (@nth R (IZR Z0) l i) (pos eps)) *)
(* Goal: Rle (IZR Z0) (Rdiv (Rminus b a) (pos eps)) *)
  apply Rlt_div_r.
(* Goal: Rgt r2 (IZR Z0) *)
(* Goal: Rlt (Rmult (IZR Z0) r2) r1 *)
  apply H2.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite Rmult_0_l.
(* Goal: Rlt (IZR Z0) r1 *)
  apply H1.
Qed.

Lemma Rdiv_le_0_compat : forall r1 r2 : R, 0 <= r1 -> 0 < r2 -> 0 <= r1 / r2.
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
 intros.
(* Goal: Rle (IZR Z0) (Rdiv r1 r2) *)
 apply Rle_div_r.
(* Goal: Rlt (Rplus x (Ropp y)) z *)
(* Goal: and (Rlt (Ropp z) (Rminus x y)) (Rlt (Rminus x y) z) *)
(* Goal: Rlt (Rabs (Rminus x y)) z *)
 apply H0.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
 rewrite Rmult_0_l.
(* Goal: Rle z x *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
 apply H.
Qed.

Lemma Rdiv_lt_1 : forall r1 r2, 0 < r2 -> (r1 < r2 <-> r1 / r2 < 1).
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  intros.
(* Goal: iff (Rle r1 r2) (Rle (Rdiv r1 r2) (IZR (Zpos xH))) *)
  pattern r2 at 1.
(* Goal: (fun r : R => iff (Rle r1 r) (Rle (Rdiv r1 r2) (IZR (Zpos xH)))) r2 *)
  rewrite <- (Rmult_1_l r2).
(* Goal: Rle z x *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
  split ; apply Rlt_div_l ; apply H.
Qed.

Lemma Rdiv_le_1 : forall r1 r2, 0 < r2 -> (r1 <= r2 <-> r1/r2 <= 1).
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  intros.
(* Goal: iff (Rle r1 r2) (Rle (Rdiv r1 r2) (IZR (Zpos xH))) *)
  pattern r2 at 1.
(* Goal: (fun r : R => iff (Rle r1 r) (Rle (Rdiv r1 r2) (IZR (Zpos xH)))) r2 *)
  rewrite <- (Rmult_1_l r2).
(* Goal: Rle z x *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
  split ; apply Rle_div_l ; apply H.
Qed.

(** * Rmult *)

Lemma Rle_mult_Rlt : forall c a b : R, 0 < b -> c < 1 -> a <= b*c -> a < b.
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  intros.
(* Goal: Rlt a b *)
  apply Rle_lt_trans with (1 := H1).
(* Goal: Rlt (Rmult b c) b *)
  pattern b at 2 ; rewrite <-(Rmult_1_r b).
(* Goal: Rlt (Rmult b c) (Rmult b (IZR (Zpos xH))) *)
  apply Rmult_lt_compat_l ; auto.
Qed.

Lemma Rmult_le_0_r : forall a b, a <= 0 -> 0 <= b -> a * b <= 0.
Proof.
  intros ;
  rewrite <-(Rmult_0_r a) ;
  apply (Rmult_le_compat_neg_l a 0 b) with (1 := H) ; auto.
Qed.

Lemma Rmult_le_0_l : forall a b, 0 <= a -> b <= 0 -> a * b <= 0.
Proof.
(* Goal: forall (a b : R) (_ : Rle (IZR Z0) a) (_ : Rle b (IZR Z0)), Rle (Rmult a b) (IZR Z0) *)
  intros ; rewrite Rmult_comm ; apply Rmult_le_0_r ; auto.
Qed.

Lemma pow2_gt_0 (x : R) : x <> 0 -> 0 < x ^ 2.
Proof.
(* Goal: open_interval (pos_Rl (cons h' (cons h l)) i) (pos_Rl (cons h' (cons h l)) (S i)) x *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  destruct (pow2_ge_0 x) => // Hx.
(* Goal: Rlt (IZR Z0) (pow x (S (S O))) *)
  contradict Hx.
  apply sym_eq, Rmult_integral in H ;
  case: H => // H.
  apply Rmult_integral in H ;
  case: H => // H.
(* Goal: @eq R x (IZR Z0) *)
  contradict H ; apply Rgt_not_eq, Rlt_0_1.
Qed.

(** * Rminus *)
(** Rewritings *)

Lemma Rminus_eq_0 : forall r : R, r - r = 0.
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  intros.
(* Goal: @eq R (Rplus (ln x) (Ropp (ln y))) (Rminus (ln x) (ln y)) *)
(* Goal: Rlt (IZR Z0) y *)
(* Goal: Rlt (IZR Z0) x *)
(* Goal: Rlt (IZR Z0) (Rinv y) *)
  ring.
Qed.

Lemma Rdiv_minus_distr : forall a b c, b <> 0 -> a / b - c = (a - b * c) / b.
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
    intros.
(* Goal: @eq R (Rminus (Rdiv a b) c) (Rdiv (Rminus a (Rmult b c)) b) *)
    field.
(* Goal: Rle z x *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
    apply H.
Qed.

Lemma Rmult_minus_distr_r: forall r1 r2 r3 : R, (r1 - r2) * r3 = r1 * r3 - r2 * r3.
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  intros.
(* Goal: @eq R (@RiemannInt (fun x : R => Rminus (f x) (g x)) a b pr) (Rminus (@RiemannInt f a b pr_f) (Rmult (IZR (Zpos xH)) (@RiemannInt g a b pr_g))) *)
  unfold Rminus.
(* Goal: @eq R (@RiemannInt (fun x : R => Ropp (f x)) a b pr) (Ropp (Rmult (IZR (Zpos xH)) (@RiemannInt f a b pr_f))) *)
  rewrite <- Ropp_mult_distr_l_reverse.
(* Goal: @eq R (Rmult (Rplus r1 (Ropp r2)) r3) (Rplus (Rmult r1 r3) (Rmult (Ropp r2) r3)) *)
  apply Rmult_plus_distr_r.
Qed.

Lemma Rminus_eq_compat_l : forall r r1 r2 : R, r1 = r2 <-> r - r1 = r - r2.
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  split ; intros.
(* Goal: @eq R (Rminus r (Rminus r r1)) (Rminus r (Rminus r r2)) *)
  apply Rplus_eq_compat_l.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite H.
(* Goal: @eq R (Rmult (Rabs a) (Rinv (Rabs b))) (Rmult (Rabs a) (Rinv (Rabs b))) *)
(* Goal: not (@eq R b (IZR Z0)) *)
  reflexivity.

(* Goal: @eq R (Rplus (ln x) (Ropp (ln y))) (Rminus (ln x) (ln y)) *)
(* Goal: Rlt (IZR Z0) y *)
(* Goal: Rlt (IZR Z0) x *)
(* Goal: Rlt (IZR Z0) (Rinv y) *)
  replace r1 with (r-(r-r1)) by ring.
(* Goal: @eq R (Rplus (ln x) (Ropp (ln y))) (Rminus (ln x) (ln y)) *)
(* Goal: Rlt (IZR Z0) y *)
(* Goal: Rlt (IZR Z0) x *)
(* Goal: Rlt (IZR Z0) (Rinv y) *)
  replace r2 with (r-(r-r2)) by ring.
(* Goal: @eq R (Rminus r (Rminus r r1)) (Rminus r (Rminus r r2)) *)
  apply Rplus_eq_compat_l.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite H.
(* Goal: @eq R (Rmult (Rabs a) (Rinv (Rabs b))) (Rmult (Rabs a) (Rinv (Rabs b))) *)
(* Goal: not (@eq R b (IZR Z0)) *)
  reflexivity.
Qed.

Lemma Ropp_plus_minus_distr : forall r1 r2 : R, - (r1 + r2) = - r1 - r2.
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  intros.
(* Goal: @eq R (@RiemannInt (fun x : R => Rminus (f x) (g x)) a b pr) (Rminus (@RiemannInt f a b pr_f) (Rmult (IZR (Zpos xH)) (@RiemannInt g a b pr_g))) *)
  unfold Rminus.
(* Goal: @eq R (Ropp (Rplus r1 r2)) (Rplus (Ropp r1) (Ropp r2)) *)
  apply Ropp_plus_distr.
Qed.

(** Order *)

Lemma Rle_minus_l : forall a b c,(a - c <= b <-> a <= b + c).
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  split ; intros.
(* Goal: @eq R (Rplus (ln x) (Ropp (ln y))) (Rminus (ln x) (ln y)) *)
(* Goal: Rlt (IZR Z0) y *)
(* Goal: Rlt (IZR Z0) x *)
(* Goal: Rlt (IZR Z0) (Rinv y) *)
  replace a with ((a-c)+c) by ring.
(* Goal: Rle (Rminus z y) (Rminus x y) *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
  apply Rplus_le_compat_r.
(* Goal: Rle z x *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
  apply H.

(* Goal: @eq R (Rplus (ln x) (Ropp (ln y))) (Rminus (ln x) (ln y)) *)
(* Goal: Rlt (IZR Z0) y *)
(* Goal: Rlt (IZR Z0) x *)
(* Goal: Rlt (IZR Z0) (Rinv y) *)
  replace b with ((b+c)-c) by ring.
(* Goal: Rle (Rminus z y) (Rminus x y) *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
  apply Rplus_le_compat_r.
(* Goal: Rle z x *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
  apply H.
Qed.

Lemma Rlt_minus_r : forall a b c,(a < b - c <-> a + c < b).
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  split ; intros.
(* Goal: @eq R (Rplus (ln x) (Ropp (ln y))) (Rminus (ln x) (ln y)) *)
(* Goal: Rlt (IZR Z0) y *)
(* Goal: Rlt (IZR Z0) x *)
(* Goal: Rlt (IZR Z0) (Rinv y) *)
  replace b with ((b-c)+c) by ring.
(* Goal: Rlt (Rminus x y) (Rplus (Rplus z y) (Ropp y)) *)
  apply Rplus_lt_compat_r.
(* Goal: Rle z x *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
  apply H.

(* Goal: @eq R (Rplus (ln x) (Ropp (ln y))) (Rminus (ln x) (ln y)) *)
(* Goal: Rlt (IZR Z0) y *)
(* Goal: Rlt (IZR Z0) x *)
(* Goal: Rlt (IZR Z0) (Rinv y) *)
  replace a with ((a+c)-c) by ring.
(* Goal: Rlt (Rminus x y) (Rplus (Rplus z y) (Ropp y)) *)
  apply Rplus_lt_compat_r.
(* Goal: Rle z x *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
  apply H.
Qed.

Lemma Rlt_minus_l : forall a b c,(a - c < b <-> a < b + c).
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  split ; intros.
(* Goal: @eq R (Rplus (ln x) (Ropp (ln y))) (Rminus (ln x) (ln y)) *)
(* Goal: Rlt (IZR Z0) y *)
(* Goal: Rlt (IZR Z0) x *)
(* Goal: Rlt (IZR Z0) (Rinv y) *)
  replace a with ((a-c)+c) by ring.
(* Goal: Rlt (Rminus x y) (Rplus (Rplus z y) (Ropp y)) *)
  apply Rplus_lt_compat_r.
(* Goal: Rle z x *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
  apply H.

(* Goal: @eq R (Rplus (ln x) (Ropp (ln y))) (Rminus (ln x) (ln y)) *)
(* Goal: Rlt (IZR Z0) y *)
(* Goal: Rlt (IZR Z0) x *)
(* Goal: Rlt (IZR Z0) (Rinv y) *)
  replace b with ((b+c)-c) by ring.
(* Goal: Rlt (Rminus x y) (Rplus (Rplus z y) (Ropp y)) *)
  apply Rplus_lt_compat_r.
(* Goal: Rle z x *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
  apply H.
Qed.

Lemma Rle_minus_r : forall a b c,(a <= b - c <-> a + c <= b).
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  split ; intros.
(* Goal: @eq R (Rplus (ln x) (Ropp (ln y))) (Rminus (ln x) (ln y)) *)
(* Goal: Rlt (IZR Z0) y *)
(* Goal: Rlt (IZR Z0) x *)
(* Goal: Rlt (IZR Z0) (Rinv y) *)
  replace b with ((b-c)+c) by ring.
(* Goal: Rle (Rminus z y) (Rminus x y) *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
  apply Rplus_le_compat_r.
(* Goal: Rle z x *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
  apply H.

(* Goal: @eq R (Rplus (ln x) (Ropp (ln y))) (Rminus (ln x) (ln y)) *)
(* Goal: Rlt (IZR Z0) y *)
(* Goal: Rlt (IZR Z0) x *)
(* Goal: Rlt (IZR Z0) (Rinv y) *)
  replace a with ((a+c)-c) by ring.
(* Goal: Rle (Rminus z y) (Rminus x y) *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
  apply Rplus_le_compat_r.
(* Goal: Rle z x *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
  apply H.
Qed.

Lemma Rminus_le_0 : forall a b, a <= b <-> 0 <= b - a.
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  intros.
(* Goal: iff (Rlt a b) (Rlt (IZR Z0) (Rminus b a)) *)
  pattern a at 1.
(* Goal: (fun r : R => iff (Rlt r b) (Rlt (IZR Z0) (Rminus b a))) a *)
  rewrite <- (Rplus_0_l a).
(* Goal: iff (Rle (Rplus (IZR Z0) a) b) (Rle (IZR Z0) (Rminus b a)) *)
  split ; apply Rle_minus_r.
Qed.

Lemma Rminus_lt_0 : forall a b, a < b <-> 0 < b - a.
Proof.
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  intros.
(* Goal: iff (Rlt a b) (Rlt (IZR Z0) (Rminus b a)) *)
  pattern a at 1.
(* Goal: (fun r : R => iff (Rlt r b) (Rlt (IZR Z0) (Rminus b a))) a *)
  rewrite <- (Rplus_0_l a).
(* Goal: iff (Rlt (Rplus (IZR Z0) a) b) (Rlt (IZR Z0) (Rminus b a)) *)
  split ; apply Rlt_minus_r.
Qed.

(** * Rplus *)
(** Sums *)

Lemma sum_f_rw (a : nat -> R) (n m : nat) :
  (n < m)%nat -> sum_f (S n) m a = sum_f_R0 a m - sum_f_R0 a n.
Proof.
(* Goal: forall _ : lt n m, @eq R (sum_f (S n) m a) (Rminus (sum_f_R0 a m) (sum_f_R0 a n)) *)
  intro Hnm ; unfold sum_f.
  revert  a n Hnm.
(* Goal: forall (a : forall _ : nat, R) (n : nat) (_ : lt n m), @eq R (sum_f_R0 (fun x : nat => a (Init.Nat.add x (S n))) (Init.Nat.sub m (S n))) (Rminus (sum_f_R0 a m) (sum_f_R0 a n)) *)
  induction m ; intros a n Hnm.
(* Goal: @eq R (sum_f_R0 (fun x : nat => a (Init.Nat.add x (S n))) (Init.Nat.sub O (S n))) (Rminus (sum_f_R0 a O) (sum_f_R0 a n)) *)
(* Goal: @eq R (sum_f_R0 (fun x : nat => a (Init.Nat.add x (S n))) (Init.Nat.sub (S m) (S n))) (Rminus (sum_f_R0 a (S m)) (sum_f_R0 a n)) *)
  apply lt_n_O in Hnm ; intuition.
(* Goal: @eq R (@last R (IZR Z0) l) (Rplus a (Rmult (INR n) (pos eps))) *)
(* Goal: @eq R (Rplus a (Rmult (INR n) (pos eps))) b *)
(* Goal: forall (i : nat) (_ : lt (S i) (@size R l)), and (Rlt (@nth R (IZR Z0) l i) (@nth R (IZR Z0) l (S i))) (Rle (@nth R (IZR Z0) l (S i)) (Rplus (@nth R (IZR Z0) l i) (pos eps))) *)
(* Goal: @sig (list R) (fun l : list R => and (@eq R (@head R (IZR Z0) l) a) (and (@eq R (@last R (IZR Z0) l) b) (forall (i : nat) (_ : lt (S i) (@size R l)), and (Rlt (@nth R (IZR Z0) l i) (@nth R (IZR Z0) l (S i))) (Rle (@nth R (IZR Z0) l (S i)) (Rplus (@nth R (IZR Z0) l i) (pos eps)))))) *)
(* Goal: Rle (IZR Z0) (Rdiv (Rminus b a) (pos eps)) *)
  rewrite (decomp_sum _ _ (lt_O_Sn _)) ; simpl.
  revert Hnm ;
  destruct n ; intro Hnm.
(* Goal: @eq R (sum_f_R0 (fun x : nat => a (Init.Nat.add x (S O))) (Init.Nat.sub m O)) (Rminus (Rplus (a O) (sum_f_R0 (fun i : nat => a (S i)) m)) (sum_f_R0 a O)) *)
(* Goal: @eq R (sum_f_R0 (fun x : nat => a (Init.Nat.add x (S (S n)))) (Init.Nat.sub m (S n))) (Rminus (Rplus (a O) (sum_f_R0 (fun i : nat => a (S i)) m)) (sum_f_R0 a (S n))) *)
  rewrite <- minus_n_O ; simpl ; ring_simplify.
(* Goal: @eq R (sum_f_R0 (fun x : nat => a (Init.Nat.add x (S O))) m) (sum_f_R0 (fun i : nat => a (S i)) m) *)
(* Goal: @eq R (sum_f_R0 (fun x : nat => a (Init.Nat.add x (S (S n)))) (Init.Nat.sub m (S n))) (Rminus (Rplus (a O) (sum_f_R0 (fun i : nat => a (S i)) m)) (sum_f_R0 a (S n))) *)
  clear Hnm IHm.
(* Goal: @eq R (@last R (IZR Z0) l) (Rplus a (Rmult (INR n) (pos eps))) *)
(* Goal: @eq R (Rplus a (Rmult (INR n) (pos eps))) b *)
(* Goal: forall (i : nat) (_ : lt (S i) (@size R l)), and (Rlt (@nth R (IZR Z0) l i) (@nth R (IZR Z0) l (S i))) (Rle (@nth R (IZR Z0) l (S i)) (Rplus (@nth R (IZR Z0) l i) (pos eps))) *)
(* Goal: @sig (list R) (fun l : list R => and (@eq R (@head R (IZR Z0) l) a) (and (@eq R (@last R (IZR Z0) l) b) (forall (i : nat) (_ : lt (S i) (@size R l)), and (Rlt (@nth R (IZR Z0) l i) (@nth R (IZR Z0) l (S i))) (Rle (@nth R (IZR Z0) l (S i)) (Rplus (@nth R (IZR Z0) l i) (pos eps)))))) *)
(* Goal: Rle (IZR Z0) (Rdiv (Rminus b a) (pos eps)) *)
  induction m ; simpl.
(* Goal: @eq R (Rmult (Rabs a) (Rinv (Rabs b))) (Rmult (Rabs a) (Rinv (Rabs b))) *)
(* Goal: not (@eq R b (IZR Z0)) *)
  reflexivity.
(* Goal: @eq R (Rmult (Rabs a) (Rinv (Rabs b))) (Rmult (Rabs a) (Rinv (Rabs b))) *)
(* Goal: not (@eq R b (IZR Z0)) *)
  rewrite <- plus_n_Sm, plus_0_r, IHm ; reflexivity.
(* Goal: @eq R (sum_f_R0 (fun x : nat => a (Init.Nat.add x (S (S n)))) (Init.Nat.sub m (S n))) (Rminus (Rplus (a O) (sum_f_R0 (fun i : nat => a (S i)) m)) (sum_f_R0 a (S n))) *)
  rewrite (decomp_sum _ _ (lt_O_Sn _)) ; simpl ; ring_simplify.
(* Goal: @eq R (sum_f_R0 (fun x : nat => a (Init.Nat.add x (S (S n)))) (Init.Nat.sub m (S n))) (Rminus (sum_f_R0 (fun i : nat => a (S i)) m) (sum_f_R0 (fun i : nat => a (S i)) n)) *)
  apply lt_S_n in Hnm.
(* Goal: @eq R (sum_f_R0 (fun x : nat => a (Init.Nat.add x (S (S n)))) (Init.Nat.sub m (S n))) (Rminus (sum_f_R0 (fun i : nat => a (S i)) m) (sum_f_R0 (fun i : nat => a (S i)) n)) *)
  rewrite <- (IHm _ _ Hnm).
(* Goal: @eq R (sum_f_R0 (fun x : nat => a (Init.Nat.add x (S (S n)))) (Init.Nat.sub m (S n))) (sum_f_R0 (fun x : nat => a (S (Init.Nat.add x (S n)))) (Init.Nat.sub m (S n))) *)
  clear IHm.
(* Goal: @eq R (@last R (IZR Z0) l) (Rplus a (Rmult (INR n) (pos eps))) *)
(* Goal: @eq R (Rplus a (Rmult (INR n) (pos eps))) b *)
(* Goal: forall (i : nat) (_ : lt (S i) (@size R l)), and (Rlt (@nth R (IZR Z0) l i) (@nth R (IZR Z0) l (S i))) (Rle (@nth R (IZR Z0) l (S i)) (Rplus (@nth R (IZR Z0) l i) (pos eps))) *)
(* Goal: @sig (list R) (fun l : list R => and (@eq R (@head R (IZR Z0) l) a) (and (@eq R (@last R (IZR Z0) l) b) (forall (i : nat) (_ : lt (S i) (@size R l)), and (Rlt (@nth R (IZR Z0) l i) (@nth R (IZR Z0) l (S i))) (Rle (@nth R (IZR Z0) l (S i)) (Rplus (@nth R (IZR Z0) l i) (pos eps)))))) *)
(* Goal: Rle (IZR Z0) (Rdiv (Rminus b a) (pos eps)) *)
  induction (m - S n)%nat ; simpl.
(* Goal: @eq R (Rmult (Rabs a) (Rinv (Rabs b))) (Rmult (Rabs a) (Rinv (Rabs b))) *)
(* Goal: not (@eq R b (IZR Z0)) *)
  reflexivity.
(* Goal: @eq R (Rmult (Rabs a) (Rinv (Rabs b))) (Rmult (Rabs a) (Rinv (Rabs b))) *)
(* Goal: not (@eq R b (IZR Z0)) *)
  rewrite <- plus_n_Sm, IHn0 ; reflexivity.
Qed.

Lemma sum_f_rw_0 (u : nat -> R) (n : nat) :
  sum_f O n u = sum_f_R0 u n.
Proof.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite /sum_f -minus_n_O.
(* Goal: Rle (f (pos_Rl (cons h' (cons h l)) (Init.Nat.pred (Rlength (cons h' (cons h l)))))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) *)
  elim: n => [ | n IH] //.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite /sum_f_R0 -/sum_f_R0 //.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by rewrite plus_0_r IH.
Qed.

Lemma sum_f_n_Sm (u : nat -> R) (n m : nat) :
  (n <= m)%nat -> sum_f n (S m) u = sum_f n m u + u (S m).
Proof.
(* Goal: forall (n : nat) (_ : forall (h h' : R) (l : Rlist), Rle (f (pos_Rl (cons h' (cons h l)) (S n))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l0 : Rlist) : Rlist := match l0 with | nil => nil | cons h0 t => cons (f h0) (app t) end) l))))) (h h' : R) (l : Rlist), Rle (f (pos_Rl (cons h' (cons h l)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l0 : Rlist) : Rlist := match l0 with | nil => nil | cons h0 t => cons (f h0) (app t) end) l)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  move => H.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite /sum_f -minus_Sn_m // /sum_f_R0 -/sum_f_R0.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite plus_Sn_m.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by rewrite MyNat.sub_add.
Qed.
Lemma sum_f_u_Sk (u : nat -> R) (n m : nat) :
  (n <= m)%nat -> sum_f (S n) (S m) u = sum_f n m (fun k => u (S k)).
Proof.
(* Goal: Rle b (Rplus (Rplus a (Rmult (INR n) (pos eps))) (pos eps)) *)
(* Goal: @eq nat n i *)
(* Goal: Rle (IZR Z0) (Rplus (@nth R (IZR Z0) (@mkseq R (fun k : nat => Rplus a (Rmult (INR k) (pos eps))) (S n)) i) (pos eps)) *)
(* Goal: Rle (IZR Z0) (Rdiv (Rminus b a) (pos eps)) *)
  move => H ; rewrite /sum_f.
(* Goal: @eq R (sum_f_R0 (fun x : nat => u (Init.Nat.add x (S n))) (Init.Nat.sub (S m) (S n))) (sum_f_R0 (fun x : nat => u (S (Init.Nat.add x n))) (Init.Nat.sub m n)) *)
  simpl minus.
(* Goal: Rle (f (pos_Rl (cons h' (cons h l)) (Init.Nat.pred (Rlength (cons h' (cons h l)))))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) *)
  elim: (m - n)%nat => [ | k IH] //=.
(* Goal: @eq R (Rplus (sum_f_R0 (fun x : nat => u (Init.Nat.add x (Nat.add p n))) k) (u (S (Init.Nat.add k (Nat.add p n))))) (Rplus (sum_f_R0 (fun x : nat => u (Init.Nat.add (Init.Nat.add x n) p)) k) (u (S (Init.Nat.add (Init.Nat.add k n) p)))) *)
  rewrite IH ; repeat apply f_equal.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by rewrite plus_n_Sm.
Qed.
Lemma sum_f_u_add (u : nat -> R) (p n m : nat) :
  (n <= m)%nat -> sum_f (n + p)%nat (m + p)%nat u = sum_f n m (fun k => u (k + p)%nat).
Proof.
(* Goal: Rle b (Rplus (Rplus a (Rmult (INR n) (pos eps))) (pos eps)) *)
(* Goal: @eq nat n i *)
(* Goal: Rle (IZR Z0) (Rplus (@nth R (IZR Z0) (@mkseq R (fun k : nat => Rplus a (Rmult (INR k) (pos eps))) (S n)) i) (pos eps)) *)
(* Goal: Rle (IZR Z0) (Rdiv (Rminus b a) (pos eps)) *)
  move => H ; rewrite /sum_f.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite ?(plus_comm _ p) -minus_plus_simpl_l_reverse.
(* Goal: Rle (f (pos_Rl (cons h' (cons h l)) (Init.Nat.pred (Rlength (cons h' (cons h l)))))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) *)
  elim: (m - n)%nat => [ | k IH] //=.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by rewrite plus_comm.
(* Goal: @eq R (Rplus (sum_f_R0 (fun x : nat => u (Init.Nat.add x (Nat.add p n))) k) (u (S (Init.Nat.add k (Nat.add p n))))) (Rplus (sum_f_R0 (fun x : nat => u (Init.Nat.add (Init.Nat.add x n) p)) k) (u (S (Init.Nat.add (Init.Nat.add k n) p)))) *)
  rewrite IH ; repeat apply f_equal.
(* Goal: @eq R (Rplus (ln x) (Ropp (ln y))) (Rminus (ln x) (ln y)) *)
(* Goal: Rlt (IZR Z0) y *)
(* Goal: Rlt (IZR Z0) x *)
(* Goal: Rlt (IZR Z0) (Rinv y) *)
  ring.
Qed.


Lemma sum_f_Sn_m (u : nat -> R) (n m : nat) :
  (n < m)%nat -> sum_f (S n) m u = sum_f n m u - u n.
Proof.
(* Goal: forall (n : nat) (_ : forall (h h' : R) (l : Rlist), Rle (f (pos_Rl (cons h' (cons h l)) (S n))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l0 : Rlist) : Rlist := match l0 with | nil => nil | cons h0 t => cons (f h0) (app t) end) l))))) (h h' : R) (l : Rlist), Rle (f (pos_Rl (cons h' (cons h l)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l0 : Rlist) : Rlist := match l0 with | nil => nil | cons h0 t => cons (f h0) (app t) end) l)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  move => H.
(* Goal: Rle (f (pos_Rl (cons h' (cons h l)) (Init.Nat.pred (Rlength (cons h' (cons h l)))))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) *)
  elim: m n H => [ | m IH] // n H.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by apply lt_n_O in H.
(* Goal: @eq R (sum_f_R0 (fun x : nat => u (Init.Nat.add (Init.Nat.sub (S m) (Init.Nat.add x m)) m)) (Init.Nat.sub (S m) m)) (sum_f_R0 (fun x : nat => u (Init.Nat.add x m)) (Init.Nat.sub (S m) m)) *)
  rewrite sum_f_u_Sk ; try by intuition.
(* Goal: @eq R (sum_f_R0 (fun x : nat => u (Init.Nat.add (Init.Nat.sub (S m) (Init.Nat.add x m)) m)) (Init.Nat.sub (S m) m)) (sum_f_R0 (fun x : nat => u (Init.Nat.add x m)) (Init.Nat.sub (S m) m)) *)
  rewrite sum_f_n_Sm ; try by intuition.
  replace (sum_f n m u + u (S m) - u n)
(* Goal: @eq R (Rplus (ln x) (Ropp (ln y))) (Rminus (ln x) (ln y)) *)
(* Goal: Rlt (IZR Z0) y *)
(* Goal: Rlt (IZR Z0) x *)
(* Goal: Rlt (IZR Z0) (Rinv y) *)
    with ((sum_f n m u - u n) + u (S m)) by ring.
(* Goal: @eq R (sum_f n m (fun k : nat => u (S k))) (Rplus (Rminus (sum_f n m u) (u n)) (u (S m))) *)
  apply lt_n_Sm_le, le_lt_eq_dec in H.
(* Goal: Rle (f (pos_Rl (cons h'0 (cons h0 l0)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h'0) (cons (f h0) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l0)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  case: H => [ H | -> {n} ] //.
(* Goal: open_interval (pos_Rl (cons h' (cons h l)) i) (pos_Rl (cons h' (cons h l)) (S i)) x *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  rewrite -IH => //.
(* Goal: @eq R (@last R (IZR Z0) l) (Rplus a (Rmult (INR n) (pos eps))) *)
(* Goal: @eq R (Rplus a (Rmult (INR n) (pos eps))) b *)
(* Goal: forall (i : nat) (_ : lt (S i) (@size R l)), and (Rlt (@nth R (IZR Z0) l i) (@nth R (IZR Z0) l (S i))) (Rle (@nth R (IZR Z0) l (S i)) (Rplus (@nth R (IZR Z0) l i) (pos eps))) *)
(* Goal: @sig (list R) (fun l : list R => and (@eq R (@head R (IZR Z0) l) a) (and (@eq R (@last R (IZR Z0) l) b) (forall (i : nat) (_ : lt (S i) (@size R l)), and (Rlt (@nth R (IZR Z0) l i) (@nth R (IZR Z0) l (S i))) (Rle (@nth R (IZR Z0) l (S i)) (Rplus (@nth R (IZR Z0) l i) (pos eps)))))) *)
(* Goal: Rle (IZR Z0) (Rdiv (Rminus b a) (pos eps)) *)
  rewrite /sum_f ; simpl.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite MyNat.sub_succ_r.
(* Goal: @eq R (sum_f_R0 (fun x : nat => u (S (Init.Nat.add x n))) (Init.Nat.sub m n)) (Rplus (sum_f_R0 (fun x : nat => u (Init.Nat.add x (S n))) (Init.Nat.pred (Init.Nat.sub m n))) (u (S m))) *)
(* Goal: @eq R (sum_f m m (fun k : nat => u (S k))) (Rplus (Rminus (sum_f m m u) (u m)) (u (S m))) *)
  apply lt_minus_O_lt in H.
(* Goal: @eq R (sum_f_R0 (fun x : nat => u (Init.Nat.add (Init.Nat.sub (S m) (Init.Nat.add x m)) m)) (Init.Nat.sub (S m) m)) (sum_f_R0 (fun x : nat => u (Init.Nat.add x m)) (Init.Nat.sub (S m) m)) *)
  rewrite -{3}(MyNat.sub_add n m) ; try by intuition.
(* Goal: Rle (f (pos_Rl (cons h'0 (cons h0 l0)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h'0) (cons (f h0) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l0)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  case: (m-n)%nat H => {IH} [ | k] //= H.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by apply lt_n_O in H.
(* Goal: @eq R (Rplus (sum_f_R0 (fun x : nat => u (S (Init.Nat.add x n))) k) (u (S (S (Init.Nat.add k n))))) (Rplus (sum_f_R0 (fun x : nat => u (Init.Nat.add x (S n))) k) (u (S (S (Init.Nat.add k n))))) *)
(* Goal: @eq R (sum_f m m (fun k : nat => u (S k))) (Rplus (Rminus (sum_f m m u) (u m)) (u (S m))) *)
  apply (f_equal (fun y => y + _)).
(* Goal: Rle (f (pos_Rl (cons h' (cons h l)) (Init.Nat.pred (Rlength (cons h' (cons h l)))))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) *)
  elim: k {H} => [ | k IH] //.
(* Goal: @eq R (sum_f_R0 (fun x : nat => u (S (Init.Nat.add x n))) (S k)) (sum_f_R0 (fun x : nat => u (Init.Nat.add x (S n))) (S k)) *)
(* Goal: @eq R (sum_f m m (fun k : nat => u (S k))) (Rplus (Rminus (sum_f m m u) (u m)) (u (S m))) *)
  rewrite /sum_f_R0 -/sum_f_R0 IH ; repeat apply f_equal ; intuition.
(* Goal: @eq R (Rplus (ln x) (Ropp (ln y))) (Rminus (ln x) (ln y)) *)
(* Goal: Rlt (IZR Z0) y *)
(* Goal: Rlt (IZR Z0) x *)
(* Goal: Rlt (IZR Z0) (Rinv y) *)
  rewrite /sum_f minus_diag /= ; ring.
Qed.

Lemma sum_f_R0_skip (u : nat -> R) (n : nat) :
  sum_f_R0 (fun k => u (n - k)%nat) n = sum_f_R0 u n.
Proof.
  suff H : forall n m, (n < m)%nat
    -> sum_f n m (fun k => u ((m - k) + n)%nat) = sum_f n m u.

(* Goal: Rle (f (pos_Rl (cons h'0 (cons h0 l0)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h'0) (cons (f h0) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l0)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  case: n => [ | n] //.
(* Goal: forall (n : nat) (_ : forall (h h' : R) (l : Rlist), Rle (f (pos_Rl (cons h' (cons h l)) (S n))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l0 : Rlist) : Rlist := match l0 with | nil => nil | cons h0 t => cons (f h0) (app t) end) l))))) (h h' : R) (l : Rlist), Rle (f (pos_Rl (cons h' (cons h l)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l0 : Rlist) : Rlist := match l0 with | nil => nil | cons h0 t => cons (f h0) (app t) end) l)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  move: (H _ _ (lt_O_Sn n)) => {H} H.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite /sum_f in H.
(* Goal: @eq R (sum_f_R0 (fun k : nat => u (Init.Nat.sub (S n) k)) (S n)) (sum_f_R0 u (S n)) *)
(* Goal: forall (n m : nat) (_ : lt n m), @eq R (sum_f n m (fun k : nat => u (Init.Nat.add (Init.Nat.sub m k) n))) (sum_f n m u) *)
  transitivity (sum_f_R0 (fun x : nat => u (S n - (x + 0) + 0)%nat) (S n - 0)).
(* Goal: @eq R (sum_f_R0 (fun x : nat => u (Init.Nat.add x O)) (Init.Nat.sub (S n) O)) (sum_f_R0 u (S n)) *)
(* Goal: forall (n m : nat) (_ : lt n m), @eq R (sum_f n m (fun k : nat => u (Init.Nat.add (Init.Nat.sub m k) n))) (sum_f n m u) *)
    replace (S n - 0)%nat with (S n) by auto.
(* Goal: Rle (f (pos_Rl (cons h' (cons h l)) (Init.Nat.pred (Rlength (cons h' (cons h l)))))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) *)
    elim: {2 4}(S n) => [ | m IH] //.
(* Goal: @eq R (@last R (IZR Z0) l) b *)
(* Goal: forall (i : nat) (_ : lt (S i) (@size R l)), and (Rlt (@nth R (IZR Z0) l i) (@nth R (IZR Z0) l (S i))) (Rle (@nth R (IZR Z0) l (S i)) (Rplus (@nth R (IZR Z0) l i) (pos eps))) *)
(* Goal: Rle (IZR Z0) (Rdiv (Rminus b a) (pos eps)) *)
    simpl ; by rewrite plus_0_r.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
    rewrite /sum_f_R0 -/sum_f_R0 -IH.
(* Goal: @eq R (u (Init.Nat.add (Init.Nat.sub (S m) (Init.Nat.add O n)) n)) (u (S (Init.Nat.add (Init.Nat.sub m (Init.Nat.add O n)) n))) *)
(* Goal: @eq R (Rplus (sum_f_R0 (fun x : nat => u (Init.Nat.add (Init.Nat.sub (S m) (Init.Nat.add x n)) n)) k) (u (Init.Nat.add (Init.Nat.sub (S m) (Init.Nat.add (S k) n)) n))) (Rplus (sum_f_R0 (fun x : nat => u (S (Init.Nat.add (Init.Nat.sub m (Init.Nat.add x n)) n))) k) (u (S (Init.Nat.add (Init.Nat.sub m (Init.Nat.add (S k) n)) n)))) *)
(* Goal: @eq R (u (Init.Nat.add (Init.Nat.sub (S m) (S m)) n)) (u n) *)
(* Goal: @eq R (sum_f m (S m) (fun k : nat => u (Init.Nat.add (Init.Nat.sub (S m) k) m))) (sum_f m (S m) u) *)
    apply f_equal.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
    by rewrite ?plus_0_r.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite H.
(* Goal: @eq R (sum_f_R0 (fun x : nat => u (Init.Nat.add x O)) (Init.Nat.sub (S n) O)) (sum_f_R0 u (S n)) *)
(* Goal: forall (n m : nat) (_ : lt n m), @eq R (sum_f n m (fun k : nat => u (Init.Nat.add (Init.Nat.sub m k) n))) (sum_f n m u) *)
  replace (S n - 0)%nat with (S n) by auto.
(* Goal: Rle (f (pos_Rl (cons h' (cons h l)) (Init.Nat.pred (Rlength (cons h' (cons h l)))))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) *)
  elim: (S n) => [ | m IH] //.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite /sum_f_R0 -/sum_f_R0 -IH.
(* Goal: @eq R (u (Init.Nat.add (Init.Nat.sub (S m) (Init.Nat.add O n)) n)) (u (S (Init.Nat.add (Init.Nat.sub m (Init.Nat.add O n)) n))) *)
(* Goal: @eq R (Rplus (sum_f_R0 (fun x : nat => u (Init.Nat.add (Init.Nat.sub (S m) (Init.Nat.add x n)) n)) k) (u (Init.Nat.add (Init.Nat.sub (S m) (Init.Nat.add (S k) n)) n))) (Rplus (sum_f_R0 (fun x : nat => u (S (Init.Nat.add (Init.Nat.sub m (Init.Nat.add x n)) n))) k) (u (S (Init.Nat.add (Init.Nat.sub m (Init.Nat.add (S k) n)) n)))) *)
(* Goal: @eq R (u (Init.Nat.add (Init.Nat.sub (S m) (S m)) n)) (u n) *)
(* Goal: @eq R (sum_f m (S m) (fun k : nat => u (Init.Nat.add (Init.Nat.sub (S m) k) m))) (sum_f m (S m) u) *)
  apply f_equal.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by rewrite plus_0_r.

(* Goal: forall (n : nat) (_ : forall (h h' : R) (l : Rlist), Rle (f (pos_Rl (cons h' (cons h l)) (S n))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l0 : Rlist) : Rlist := match l0 with | nil => nil | cons h0 t => cons (f h0) (app t) end) l))))) (h h' : R) (l : Rlist), Rle (f (pos_Rl (cons h' (cons h l)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l0 : Rlist) : Rlist := match l0 with | nil => nil | cons h0 t => cons (f h0) (app t) end) l)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  move => {n} n m H.
(* Goal: Rle (f (pos_Rl (cons h' (cons h l)) (Init.Nat.pred (Rlength (cons h' (cons h l)))))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) *)
  elim: m u H => [ | m IH] u H //.
(* Goal: @eq R (sum_f n (S m) (fun k : nat => u (Init.Nat.add (Init.Nat.sub (S m) k) n))) (sum_f n (S m) u) *)
  apply lt_n_Sm_le, le_lt_eq_dec in H ; case: H IH => [H IH | -> _ {n}] //.
(* Goal: @eq R (sum_f_R0 (fun x : nat => u (Init.Nat.add (Init.Nat.sub (S m) (Init.Nat.add x m)) m)) (Init.Nat.sub (S m) m)) (sum_f_R0 (fun x : nat => u (Init.Nat.add x m)) (Init.Nat.sub (S m) m)) *)
  rewrite sum_f_n_Sm ; try by intuition.
(* Goal: @eq R (Rplus (ln x) (Ropp (ln y))) (Rminus (ln x) (ln y)) *)
(* Goal: Rlt (IZR Z0) y *)
(* Goal: Rlt (IZR Z0) x *)
(* Goal: Rlt (IZR Z0) (Rinv y) *)
  replace (sum_f n (S m) u) with (sum_f n (S m) u - u n + u n) by ring.
(* Goal: @eq R (sum_f_R0 (fun x : nat => u (Init.Nat.add (Init.Nat.sub (S m) (Init.Nat.add x m)) m)) (Init.Nat.sub (S m) m)) (sum_f_R0 (fun x : nat => u (Init.Nat.add x m)) (Init.Nat.sub (S m) m)) *)
  rewrite -sum_f_Sn_m ; try by intuition.
(* Goal: @eq R (sum_f_R0 (fun x : nat => u (Init.Nat.add (Init.Nat.sub (S m) (Init.Nat.add x m)) m)) (Init.Nat.sub (S m) m)) (sum_f_R0 (fun x : nat => u (Init.Nat.add x m)) (Init.Nat.sub (S m) m)) *)
  rewrite sum_f_u_Sk ; try by intuition.
(* Goal: @eq R (Rplus (sum_f n m (fun k : nat => u (Init.Nat.add (Init.Nat.sub (S m) k) n))) (u (Init.Nat.add (Init.Nat.sub (S m) (S m)) n))) (Rplus (sum_f n m (fun k : nat => u (S k))) (u n)) *)
(* Goal: @eq R (sum_f m (S m) (fun k : nat => u (Init.Nat.add (Init.Nat.sub (S m) k) m))) (sum_f m (S m) u) *)
  rewrite -(IH (fun k => u (S k))) => {IH} ; try by intuition.
(* Goal: @eq R (Rplus (sum_f n m (fun k : nat => u (Init.Nat.add (Init.Nat.sub (S m) k) n))) (u (Init.Nat.add (Init.Nat.sub (S m) (S m)) n))) (Rplus (sum_f n m (fun k : nat => u (S (Init.Nat.add (Init.Nat.sub m k) n)))) (u n)) *)
(* Goal: @eq R (sum_f m (S m) (fun k : nat => u (Init.Nat.add (Init.Nat.sub (S m) k) m))) (sum_f m (S m) u) *)
  apply f_equal2.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite /sum_f.
  elim: {1 3 4}(m - n)%nat (le_refl (m-n)%nat) => [ | k IH] // Hk ;
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite /sum_f_R0 -/sum_f_R0.
(* Goal: @eq R (u (Init.Nat.add (Init.Nat.sub (S m) (Init.Nat.add O n)) n)) (u (S (Init.Nat.add (Init.Nat.sub m (Init.Nat.add O n)) n))) *)
(* Goal: @eq R (Rplus (sum_f_R0 (fun x : nat => u (Init.Nat.add (Init.Nat.sub (S m) (Init.Nat.add x n)) n)) k) (u (Init.Nat.add (Init.Nat.sub (S m) (Init.Nat.add (S k) n)) n))) (Rplus (sum_f_R0 (fun x : nat => u (S (Init.Nat.add (Init.Nat.sub m (Init.Nat.add x n)) n))) k) (u (S (Init.Nat.add (Init.Nat.sub m (Init.Nat.add (S k) n)) n)))) *)
(* Goal: @eq R (u (Init.Nat.add (Init.Nat.sub (S m) (S m)) n)) (u n) *)
(* Goal: @eq R (sum_f m (S m) (fun k : nat => u (Init.Nat.add (Init.Nat.sub (S m) k) m))) (sum_f m (S m) u) *)
  apply f_equal.
(* Goal: @eq R (Rplus (u (Init.Nat.add (Init.Nat.sub (S m) (Init.Nat.add O m)) m)) (u (Init.Nat.add (Init.Nat.sub (S m) (S m)) m))) (Rplus (u (Init.Nat.add O m)) (u (S m))) *)
  rewrite plus_0_l MyNat.sub_add ; intuition.
(* Goal: @eq R (sum_f_R0 (fun x : nat => u (Init.Nat.add (Init.Nat.sub (S m) (Init.Nat.add x m)) m)) (Init.Nat.sub (S m) m)) (sum_f_R0 (fun x : nat => u (Init.Nat.add x m)) (Init.Nat.sub (S m) m)) *)
  rewrite IH ; try by intuition.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by rewrite minus_diag plus_0_l.

(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite /sum_f.
(* Goal: @eq R (sum_f_R0 (fun x : nat => u (Init.Nat.add (Init.Nat.sub (S m) (Init.Nat.add x m)) m)) (Init.Nat.sub (S m) m)) (sum_f_R0 (fun x : nat => u (Init.Nat.add x m)) (Init.Nat.sub (S m) m)) *)
  rewrite -minus_Sn_m ; try by intuition.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite minus_diag.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite /sum_f_R0 -/sum_f_R0.
(* Goal: @eq R (Rplus (ln x) (Ropp (ln y))) (Rminus (ln x) (ln y)) *)
(* Goal: Rlt (IZR Z0) y *)
(* Goal: Rlt (IZR Z0) x *)
(* Goal: Rlt (IZR Z0) (Rinv y) *)
  replace (1+m)%nat with (S m) by ring.
(* Goal: @eq R (Rplus (u (Init.Nat.add (Init.Nat.sub (S m) (Init.Nat.add O m)) m)) (u (Init.Nat.add (Init.Nat.sub (S m) (S m)) m))) (Rplus (u (Init.Nat.add O m)) (u (S m))) *)
  rewrite plus_0_l minus_diag MyNat.sub_add ; intuition.
Qed.

Lemma sum_f_chasles (u : nat -> R) (n m k : nat) :
  (n < m)%nat -> (m < k)%nat ->
  sum_f (S n) k u = sum_f (S n) m u + sum_f (S m) k u.
Proof.
(* Goal: forall (n : nat) (_ : forall (h h' : R) (l : Rlist), Rle (f (pos_Rl (cons h' (cons h l)) (S n))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l0 : Rlist) : Rlist := match l0 with | nil => nil | cons h0 t => cons (f h0) (app t) end) l))))) (h h' : R) (l : Rlist), Rle (f (pos_Rl (cons h' (cons h l)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l0 : Rlist) : Rlist := match l0 with | nil => nil | cons h0 t => cons (f h0) (app t) end) l)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  move => Hnm Hmk.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite ?sum_f_rw //.
(* Goal: @eq R (Rplus (ln x) (Ropp (ln y))) (Rminus (ln x) (ln y)) *)
(* Goal: Rlt (IZR Z0) y *)
(* Goal: Rlt (IZR Z0) x *)
(* Goal: Rlt (IZR Z0) (Rinv y) *)
  ring.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by apply lt_trans with m.
Qed.

(** * Rmin and Rmax *)
(** Rewritings *)

Lemma Rplus_max_distr_l :
  forall a b c, a + Rmax b c = Rmax (a + b) (a + c).
Proof.
(* Goal: forall (a b c : R) (_ : Rle (IZR Z0) a), @eq R (Rmult (Rmin b c) a) (Rmin (Rmult b a) (Rmult c a)) *)
intros a b c.
(* Goal: Rle (Rmax a c) (Rmax b d) *)
unfold Rmax.
(* Goal: @eq R (Rplus a (if Rle_dec b c then b else c)) (if Rle_dec (Rplus a b) (Rplus a c) then Rplus a b else Rplus a c) *)
case Rle_dec ; intros H ; case Rle_dec ; intros H' ; try easy.
(* Goal: @eq R (Rplus a b) (Rplus a c) *)
(* Goal: @eq R (Rplus a c) (Rplus a b) *)
elim H'.
(* Goal: Rle (Rplus a b) (Rplus a c) *)
(* Goal: @eq R (Rplus a c) (Rplus a b) *)
apply Rplus_le_compat_l with (1 := H).
(* Goal: @eq R (Rplus a c) (Rplus a b) *)
elim H.
(* Goal: Rle b c *)
apply Rplus_le_reg_l with (1 := H').
Qed.

Lemma Rplus_max_distr_r :
  forall a b c, Rmax b c + a = Rmax (b + a) (c + a).
Proof.
(* Goal: forall (a b c : R) (_ : Rle (IZR Z0) a), @eq R (Rmult (Rmin b c) a) (Rmin (Rmult b a) (Rmult c a)) *)
intros a b c.
(* Goal: @eq R (Rplus (Rmin b c) a) (Rmin (Rplus b a) (Rplus c a)) *)
rewrite <- 3!(Rplus_comm a).
(* Goal: @eq R (Rplus a (Rmax b c)) (Rmax (Rplus a b) (Rplus a c)) *)
apply Rplus_max_distr_l.
Qed.

Lemma Rplus_min_distr_l :
  forall a b c, a + Rmin b c = Rmin (a + b) (a + c).
Proof.
(* Goal: forall (a b c : R) (_ : Rle (IZR Z0) a), @eq R (Rmult (Rmin b c) a) (Rmin (Rmult b a) (Rmult c a)) *)
intros a b c.
(* Goal: @eq R (Rmult a (Rmin b c)) (Rmin (Rmult a b) (Rmult a c)) *)
(* Goal: @eq R (Rmult a (Rmin b c)) (Rmin (Rmult a b) (Rmult a c)) *)
unfold Rmin.
(* Goal: @eq R (Rplus a (if Rle_dec b c then b else c)) (if Rle_dec (Rplus a b) (Rplus a c) then Rplus a b else Rplus a c) *)
case Rle_dec ; intros H ; case Rle_dec ; intros H' ; try easy.
(* Goal: @eq R (Rplus a b) (Rplus a c) *)
(* Goal: @eq R (Rplus a c) (Rplus a b) *)
elim H'.
(* Goal: Rle (Rplus a b) (Rplus a c) *)
(* Goal: @eq R (Rplus a c) (Rplus a b) *)
apply Rplus_le_compat_l with (1 := H).
(* Goal: @eq R (Rplus a c) (Rplus a b) *)
elim H.
(* Goal: Rle b c *)
apply Rplus_le_reg_l with (1 := H').
Qed.

Lemma Rplus_min_distr_r :
  forall a b c, Rmin b c + a = Rmin (b + a) (c + a).
Proof.
(* Goal: forall (a b c : R) (_ : Rle (IZR Z0) a), @eq R (Rmult (Rmin b c) a) (Rmin (Rmult b a) (Rmult c a)) *)
intros a b c.
(* Goal: @eq R (Rplus (Rmin b c) a) (Rmin (Rplus b a) (Rplus c a)) *)
rewrite <- 3!(Rplus_comm a).
(* Goal: @eq R (Rplus a (Rmin b c)) (Rmin (Rplus a b) (Rplus a c)) *)
apply Rplus_min_distr_l.
Qed.

Lemma Rmult_max_distr_l :
  forall a b c, 0 <= a -> a * Rmax b c = Rmax (a * b) (a * c).
Proof.
(* Goal: forall (a b c : R) (_ : Rle (IZR Z0) a), @eq R (Rmult a (Rmin b c)) (Rmin (Rmult a b) (Rmult a c)) *)
intros a b c Ha.
(* Goal: @eq R (Rmult a (Rmin b c)) (Rmin (Rmult a b) (Rmult a c)) *)
destruct Ha as [Ha|Ha].
(* Goal: Rle (Rmax a c) (Rmax b d) *)
unfold Rmax.
(* Goal: @eq R (Rmult a (if Rle_dec b c then b else c)) (if Rle_dec (Rmult a b) (Rmult a c) then Rmult a b else Rmult a c) *)
(* Goal: @eq R (Rmult a (Rmin b c)) (Rmin (Rmult a b) (Rmult a c)) *)
case Rle_dec ; intros H.
(* Goal: @eq R (Rmult a b) (if Rle_dec (Rmult a b) (Rmult a c) then Rmult a b else Rmult a c) *)
(* Goal: @eq R (Rmult a c) (if Rle_dec (Rmult a b) (Rmult a c) then Rmult a b else Rmult a c) *)
(* Goal: @eq R (Rmult a (Rmin b c)) (Rmin (Rmult a b) (Rmult a c)) *)
apply (Rmult_le_compat_l _ _ _ (Rlt_le _ _ Ha)) in H.
(* Goal: @eq R (IZR Z0) (if Rle_dec (IZR Z0) (IZR Z0) then IZR Z0 else IZR Z0) *)
case Rle_dec ; intuition.
(* Goal: @eq R (Rmult a c) (if Rle_dec (Rmult a b) (Rmult a c) then Rmult a b else Rmult a c) *)
(* Goal: @eq R (Rmult a (Rmin b c)) (Rmin (Rmult a b) (Rmult a c)) *)
apply Rnot_le_lt, (Rmult_lt_compat_l _ _ _ Ha), Rlt_not_le in H.
(* Goal: @eq R (IZR Z0) (if Rle_dec (IZR Z0) (IZR Z0) then IZR Z0 else IZR Z0) *)
case Rle_dec ; intuition.
rewrite <- Ha  ; clear a Ha.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
repeat rewrite Rmult_0_l.
(* Goal: @eq R (IZR Z0) (Rmax (IZR Z0) (IZR Z0)) *)
unfold Rmax ; assert (H := Rle_refl 0).
(* Goal: @eq R (IZR Z0) (if Rle_dec (IZR Z0) (IZR Z0) then IZR Z0 else IZR Z0) *)
case Rle_dec ; intuition.
Qed.

Lemma Rmult_max_distr_r :
  forall a b c, 0 <= a -> Rmax b c * a = Rmax (b * a) (c * a).
Proof.
(* Goal: forall (a b c : R) (_ : Rle (IZR Z0) a), @eq R (Rmult (Rmin b c) a) (Rmin (Rmult b a) (Rmult c a)) *)
intros a b c.
(* Goal: forall _ : Rle (IZR Z0) a, @eq R (Rmult (Rmin b c) a) (Rmin (Rmult b a) (Rmult c a)) *)
rewrite <- 3!(Rmult_comm a).
(* Goal: forall _ : Rle (IZR Z0) a, @eq R (Rmult a (Rmax b c)) (Rmax (Rmult a b) (Rmult a c)) *)
apply Rmult_max_distr_l.
Qed.

Lemma Rmult_min_distr_l :
  forall a b c, 0 <= a -> a * Rmin b c = Rmin (a * b) (a * c).
Proof.
(* Goal: forall (a b c : R) (_ : Rle (IZR Z0) a), @eq R (Rmult a (Rmin b c)) (Rmin (Rmult a b) (Rmult a c)) *)
intros a b c Ha.
(* Goal: @eq R (Rmult a (Rmin b c)) (Rmin (Rmult a b) (Rmult a c)) *)
destruct Ha as [Ha|Ha].
(* Goal: @eq R (Rmult a (Rmin b c)) (Rmin (Rmult a b) (Rmult a c)) *)
(* Goal: @eq R (Rmult a (Rmin b c)) (Rmin (Rmult a b) (Rmult a c)) *)
unfold Rmin.
(* Goal: @eq R (Rmult a (if Rle_dec b c then b else c)) (if Rle_dec (Rmult a b) (Rmult a c) then Rmult a b else Rmult a c) *)
(* Goal: @eq R (Rmult a (Rmin b c)) (Rmin (Rmult a b) (Rmult a c)) *)
case Rle_dec ; intros H.
(* Goal: @eq R (Rmult a b) (if Rle_dec (Rmult a b) (Rmult a c) then Rmult a b else Rmult a c) *)
(* Goal: @eq R (Rmult a c) (if Rle_dec (Rmult a b) (Rmult a c) then Rmult a b else Rmult a c) *)
(* Goal: @eq R (Rmult a (Rmin b c)) (Rmin (Rmult a b) (Rmult a c)) *)
apply (Rmult_le_compat_l _ _ _ (Rlt_le _ _ Ha)) in H.
(* Goal: @eq R (IZR Z0) (if Rle_dec (IZR Z0) (IZR Z0) then IZR Z0 else IZR Z0) *)
case Rle_dec ; intuition.
(* Goal: @eq R (Rmult a c) (if Rle_dec (Rmult a b) (Rmult a c) then Rmult a b else Rmult a c) *)
(* Goal: @eq R (Rmult a (Rmin b c)) (Rmin (Rmult a b) (Rmult a c)) *)
apply Rnot_le_lt, (Rmult_lt_compat_l _ _ _ Ha), Rlt_not_le in H.
(* Goal: @eq R (IZR Z0) (if Rle_dec (IZR Z0) (IZR Z0) then IZR Z0 else IZR Z0) *)
case Rle_dec ; intuition.
rewrite <- Ha  ; clear a Ha.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
repeat rewrite Rmult_0_l.
(* Goal: @eq R (IZR Z0) (Rmin (IZR Z0) (IZR Z0)) *)
unfold Rmin ; assert (H := Rle_refl 0).
(* Goal: @eq R (IZR Z0) (if Rle_dec (IZR Z0) (IZR Z0) then IZR Z0 else IZR Z0) *)
case Rle_dec ; intuition.
Qed.

Lemma Rmult_min_distr_r :
  forall a b c, 0 <= a -> Rmin b c * a = Rmin (b * a) (c * a).
Proof.
(* Goal: forall (a b c : R) (_ : Rle (IZR Z0) a), @eq R (Rmult (Rmin b c) a) (Rmin (Rmult b a) (Rmult c a)) *)
intros a b c.
(* Goal: forall _ : Rle (IZR Z0) a, @eq R (Rmult (Rmin b c) a) (Rmin (Rmult b a) (Rmult c a)) *)
rewrite <- 3!(Rmult_comm a).
(* Goal: forall _ : Rle (IZR Z0) a, @eq R (Rmult a (Rmin b c)) (Rmin (Rmult a b) (Rmult a c)) *)
apply Rmult_min_distr_l.
Qed.

Lemma Rmin_assoc : forall x y z, Rmin x (Rmin y z) =
  Rmin (Rmin x y) z.
(* Goal: @eq R (Rmult a (Rmin b c)) (Rmin (Rmult a b) (Rmult a c)) *)
(* Goal: @eq R (Rmult a (Rmin b c)) (Rmin (Rmult a b) (Rmult a c)) *)
intros x y z; unfold Rmin.
destruct (Rle_dec y z);
destruct (Rle_dec x y);
destruct (Rle_dec x z);
destruct (Rle_dec y z) ; try intuition.
(* Goal: @eq R x z *)
(* Goal: @eq R x z *)
contradict n.
(* Goal: Rle x z *)
(* Goal: @eq R x z *)
apply Rle_trans with y ; auto.
(* Goal: @eq R x z *)
contradict r.
(* Goal: not (Rle x z) *)
apply Rlt_not_le, Rlt_trans with y ; apply Rnot_le_lt ; auto.
Qed.

Lemma Rmax_assoc : forall x y z, Rmax x (Rmax y z) =
  Rmax (Rmax x y) z.
(* Goal: Rle (Rmax a c) (Rmax b d) *)
intros x y z; unfold Rmax.
destruct (Rle_dec y z);
destruct (Rle_dec x y);
destruct (Rle_dec x z);
destruct (Rle_dec y z) ; try intuition.
(* Goal: @eq R x z *)
(* Goal: @eq R x z *)
contradict n.
(* Goal: Rle x z *)
(* Goal: @eq R x z *)
apply Rle_trans with y ; auto.
(* Goal: @eq R x z *)
contradict r.
(* Goal: not (Rle x z) *)
apply Rlt_not_le, Rlt_trans with y ; apply Rnot_le_lt ; auto.
Qed.

(** Order *)

Lemma Rmax_le_compat : forall a b c d, a <= b -> c <= d -> Rmax a c <= Rmax b d.
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  intros.
(* Goal: Rle (Rmax a c) (Rmax b d) *)
  unfold Rmax.
(* Goal: Rle (if Rle_dec a c then c else a) (if Rle_dec b d then d else b) *)
  destruct (Rle_dec a c).
(* Goal: Rle a (if Rle_dec b d then d else b) *)
  destruct (Rle_dec b d).
(* Goal: Rlt (Rplus x (Ropp y)) z *)
(* Goal: and (Rlt (Ropp z) (Rminus x y)) (Rlt (Rminus x y) z) *)
(* Goal: Rlt (Rabs (Rminus x y)) z *)
    apply H0.
(* Goal: Rle c b *)
(* Goal: Rle a (if Rle_dec b d then d else b) *)
    apply Rnot_le_lt in n.
(* Goal: Rle c b *)
(* Goal: Rle a (if Rle_dec b d then d else b) *)
    apply (Rle_trans _ d).
(* Goal: Rlt (Rplus x (Ropp y)) z *)
(* Goal: and (Rlt (Ropp z) (Rminus x y)) (Rlt (Rminus x y) z) *)
(* Goal: Rlt (Rabs (Rminus x y)) z *)
    apply H0.
(* Goal: Rle d b *)
(* Goal: Rle a (if Rle_dec b d then d else b) *)
    apply (Rlt_le _ _ n).
(* Goal: Rle a (if Rle_dec b d then d else b) *)
  destruct (Rle_dec b d).
(* Goal: Rle a d *)
(* Goal: Rle a b *)
    apply (Rle_trans _ b).
(* Goal: Rle z x *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
    apply H.
(* Goal: Rle a b *)
(* Goal: forall (x : R) (_ : and (Rlt a x) (Rlt x b)), @eq R (f x) (g x) *)
(* Goal: @eq R (@RiemannInt f a b pr_f) (@RiemannInt g a b pr_g) *)
    apply r.
(* Goal: Rle z x *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
    apply H.
Qed.

Lemma Rmax_opp_Rmin : forall a b, Rmax (-a) (-b) = - Rmin a b.
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  intros.
(* Goal: @eq R (Rmax (Ropp a) (Ropp b)) (Ropp (Rmin a b)) *)
  destruct (Rle_or_lt a b).

(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite Rmax_left.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite Rmin_left.
(* Goal: @eq R (Rmult (Rabs a) (Rinv (Rabs b))) (Rmult (Rabs a) (Rinv (Rabs b))) *)
(* Goal: not (@eq R b (IZR Z0)) *)
  reflexivity.
(* Goal: Rle z x *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
  apply H.
(* Goal: Rle (Ropp (Rminus z y)) (Ropp (Rminus x y)) *)
(* Goal: Rle (Rminus x y) (IZR Z0) *)
(* Goal: Rle (Rminus z y) (IZR Z0) *)
(* Goal: Rle (Rabs (Rminus z y)) (Rabs (Rminus x y)) *)
  apply Ropp_le_contravar.
(* Goal: Rle z x *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
  apply H.

(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite Rmax_right.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite Rmin_right.
(* Goal: @eq R (Rmult (Rabs a) (Rinv (Rabs b))) (Rmult (Rabs a) (Rinv (Rabs b))) *)
(* Goal: not (@eq R b (IZR Z0)) *)
  reflexivity.
(* Goal: Rle b a *)
(* Goal: Rle (Ropp a) (Ropp b) *)
  apply Rlt_le, H.
(* Goal: Rle (Ropp (Rminus z y)) (Ropp (Rminus x y)) *)
(* Goal: Rle (Rminus x y) (IZR Z0) *)
(* Goal: Rle (Rminus z y) (IZR Z0) *)
(* Goal: Rle (Rabs (Rminus z y)) (Rabs (Rminus x y)) *)
  apply Ropp_le_contravar.
(* Goal: Rle (Rabs (Rminus x y)) eps *)
(* Goal: Rle (IZR Z0) (Rabs (Rminus x y)) *)
  apply Rlt_le.
(* Goal: Rle z x *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
  apply H.
Qed.
Lemma Rmin_opp_Rmax : forall a b, Rmin (-a) (-b) = - Rmax a b.
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  intros.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite Rmax_comm.
(* Goal: @eq R (Rmin (Ropp a) (Ropp b)) (Ropp (Rmax b a)) *)
  unfold Rmin ; case Rle_dec ; intro Hab.
(* Goal: @eq R (Ropp a) (Ropp (Rmax b a)) *)
(* Goal: @eq R (Ropp b) (Ropp (Rmax b a)) *)
  apply Ropp_le_cancel in Hab.
(* Goal: @eq R (IZR Z0) (if Rle_dec (IZR Z0) (IZR Z0) then IZR Z0 else IZR Z0) *)
  unfold Rmax ; case Rle_dec ; intuition.
(* Goal: @eq R (Ropp b) (Ropp (Rmax b a)) *)
  apply Rnot_le_lt, Ropp_lt_cancel, Rlt_not_le in Hab.
(* Goal: @eq R (IZR Z0) (if Rle_dec (IZR Z0) (IZR Z0) then IZR Z0 else IZR Z0) *)
  unfold Rmax ; case Rle_dec ; intuition.
Qed.


Lemma Rmax_mult : forall a b c, 0 <= c -> Rmax a b * c = Rmax (a * c) (b * c).
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  intros.
(* Goal: @eq R (Rmult (Rmax a b) c) (Rmax (Rmult a c) (Rmult b c)) *)
  repeat rewrite (Rmult_comm _ c).
(* Goal: @eq R (Rmult c (Rmax a b)) (Rmax (Rmult c a) (Rmult c b)) *)
  apply sym_eq.
(* Goal: @eq R (Rmax (Rmult c a) (Rmult c b)) (Rmult c (Rmax a b)) *)
  apply RmaxRmult.
(* Goal: Rle z x *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
  apply H.
Qed.

Lemma Rmax_le_Rplus : forall a b : R, 0 <= a -> 0 <= b -> Rmax a b <= a + b.
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  intros.
(* Goal: @eq R (@RiemannInt f a b pr_f) (@RiemannInt g a b pr_g) *)
  destruct (Rle_lt_dec a b).
(* Goal: Rle (Rmax a b) (Rplus a b) *)
(* Goal: Rle (Rmax a b) (Rplus a b) *)
  rewrite <- (Rplus_0_l (Rmax a b)).
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite Rmax_right.
(* Goal: Rle (Rminus z y) (Rminus x y) *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
  apply Rplus_le_compat_r.
(* Goal: Rle z x *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
  apply H.
(* Goal: Rle a b *)
(* Goal: forall (x : R) (_ : and (Rlt a x) (Rlt x b)), @eq R (f x) (g x) *)
(* Goal: @eq R (@RiemannInt f a b pr_f) (@RiemannInt g a b pr_g) *)
  apply r.
(* Goal: Rle (Rmax a b) (Rplus a b) *)
  rewrite <- (Rplus_0_r (Rmax a b)).
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite Rmax_left.
(* Goal: Rle (Rplus y (Rplus (Ropp y) x)) (Rplus y z) *)
(* Goal: and (Rle (Ropp z) (Rminus x y)) (Rle (Rminus x y) z) *)
(* Goal: Rle (Rabs (Rminus x y)) z *)
  apply Rplus_le_compat_l.
(* Goal: Rlt (Rplus x (Ropp y)) z *)
(* Goal: and (Rlt (Ropp z) (Rminus x y)) (Rlt (Rminus x y) z) *)
(* Goal: Rlt (Rabs (Rminus x y)) z *)
  apply H0.
(* Goal: Rle b a *)
  apply Rlt_le, r.
Qed.

Lemma Rplus_le_Rmax : forall a b : R, a + b <= 2*Rmax a b.
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  intros.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite RIneq.double.
(* Goal: @eq R (@RiemannInt f a b pr_f) (@RiemannInt g a b pr_g) *)
  destruct (Rle_lt_dec a b).
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite Rmax_right.
(* Goal: Rle (Rminus z y) (Rminus x y) *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
  apply Rplus_le_compat_r.
(* Goal: Rle a b *)
(* Goal: forall (x : R) (_ : and (Rlt a x) (Rlt x b)), @eq R (f x) (g x) *)
(* Goal: @eq R (@RiemannInt f a b pr_f) (@RiemannInt g a b pr_g) *)
  apply r.
(* Goal: Rle a b *)
(* Goal: forall (x : R) (_ : and (Rlt a x) (Rlt x b)), @eq R (f x) (g x) *)
(* Goal: @eq R (@RiemannInt f a b pr_f) (@RiemannInt g a b pr_g) *)
  apply r.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite Rmax_left.
(* Goal: Rle (Rplus y (Rplus (Ropp y) x)) (Rplus y z) *)
(* Goal: and (Rle (Ropp z) (Rminus x y)) (Rle (Rminus x y) z) *)
(* Goal: Rle (Rabs (Rminus x y)) z *)
  apply Rplus_le_compat_l.
(* Goal: Rle (Rabs (Rminus x y)) eps *)
(* Goal: Rle (IZR Z0) (Rabs (Rminus x y)) *)
  apply Rlt_le.
(* Goal: Rle a b *)
(* Goal: forall (x : R) (_ : and (Rlt a x) (Rlt x b)), @eq R (f x) (g x) *)
(* Goal: @eq R (@RiemannInt f a b pr_f) (@RiemannInt g a b pr_g) *)
  apply r.
(* Goal: Rle b a *)
  apply Rlt_le, r.
Qed.



Lemma Rmin_Rmax_l : forall a b, Rmin a b <= a <= Rmax a b.
Proof.
(* Goal: and (Rle (Rmin a b) x) (Rle x (Rmax a b)) *)
  split.
(* Goal: Rle (Rmin a b) a *)
(* Goal: Rle a (Rmax a b) *)
  apply Rmin_l.
(* Goal: Rle (f (pos_Rl (cons h' (cons h l)) O)) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) *)
(* Goal: forall n : nat, Rle (f (pos_Rl (cons h' (cons h l)) (S n))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  apply RmaxLess1.
Qed.

Lemma Rmin_Rmax_r : forall a b, Rmin a b <= b <= Rmax a b.
Proof.
(* Goal: and (Rle (Rmin a b) x) (Rle x (Rmax a b)) *)
  split.
(* Goal: Rle (Rmin a b) b *)
(* Goal: Rle b (Rmax a b) *)
  apply Rmin_r.
(* Goal: Rle b (Rmax a b) *)
  apply RmaxLess2.
Qed.

Lemma Rmin_Rmax : forall a b, Rmin a b <= Rmax a b.
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  intros.
(* Goal: Rle (Rmin a b) (Rmax a b) *)
  apply Rle_trans with a; apply Rmin_Rmax_l.
Qed.


(** * Rabs *)
(** Rewritings *)

Lemma Rabs_div : forall a b : R, b <> 0 -> Rabs (a/b) = (Rabs a) / (Rabs b).
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  intros.
(* Goal: @eq R (Rabs (Rdiv a b)) (Rdiv (Rabs a) (Rabs b)) *)
  unfold Rdiv.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite Rabs_mult.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite Rabs_Rinv.
(* Goal: @eq R (Rmult (Rabs a) (Rinv (Rabs b))) (Rmult (Rabs a) (Rinv (Rabs b))) *)
(* Goal: not (@eq R b (IZR Z0)) *)
  reflexivity.
(* Goal: Rle z x *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
  apply H.
Qed.

Lemma Rabs_eq_0 : forall x, Rabs x = 0 -> x = 0.
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  intros.
(* Goal: @eq R x (IZR Z0) *)
  unfold Rabs in H.
(* Goal: @eq R x (IZR Z0) *)
  destruct Rcase_abs.
(* Goal: Rle (Ropp y) x *)
(* Goal: Rle x y *)
(* Goal: forall _ : and (Rle (Ropp y) x) (Rle x y), Rle (Rabs x) y *)
  rewrite <- (Ropp_involutive x).
(* Goal: @eq R (Ropp (Ropp x)) (IZR Z0) *)
(* Goal: @eq R x (IZR Z0) *)
  apply Ropp_eq_0_compat.
(* Goal: Rle z x *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
  apply H.
(* Goal: Rle z x *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
  apply H.
Qed.

(** Order *)

Lemma Rabs_le_between : forall x y, (Rabs x <= y <-> -y <= x <= y).
Proof.
(* Goal: and (Rle (Rmin a b) x) (Rle x (Rmax a b)) *)
  split.

(* Goal: and (Rle (Rmin a b) x) (Rle x (Rmax a b)) *)
  split.
(* Goal: Rle (Ropp y) x *)
(* Goal: Rle x y *)
(* Goal: forall _ : and (Rle (Ropp y) x) (Rle x y), Rle (Rabs x) y *)
  rewrite <-(Ropp_involutive x).
(* Goal: Rle (Ropp (Rminus z y)) (Ropp (Rminus x y)) *)
(* Goal: Rle (Rminus x y) (IZR Z0) *)
(* Goal: Rle (Rminus z y) (IZR Z0) *)
(* Goal: Rle (Rabs (Rminus z y)) (Rabs (Rminus x y)) *)
  apply Ropp_le_contravar.
(* Goal: Rle x y *)
(* Goal: forall _ : and (Rle (Ropp y) x) (Rle x y), Rle (Rabs x) y *)
  apply (Rle_trans _ (Rabs x)).
(* Goal: Rle (Ropp x) (Rabs x) *)
  rewrite <-Rabs_Ropp.
(* Goal: Rle x (Rabs x) *)
(* Goal: Rle (Rabs x) y *)
(* Goal: forall _ : and (Rle (Ropp y) x) (Rle x y), Rle (Rabs x) y *)
  apply RRle_abs.
(* Goal: Rle z x *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
  apply H.
(* Goal: Rle x y *)
(* Goal: forall _ : and (Rle (Ropp y) x) (Rle x y), Rle (Rabs x) y *)
  apply (Rle_trans _ (Rabs x)).
(* Goal: Rle x (Rabs x) *)
(* Goal: Rle (Rabs x) y *)
(* Goal: forall _ : and (Rle (Ropp y) x) (Rle x y), Rle (Rabs x) y *)
  apply RRle_abs.
(* Goal: Rle z x *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
  apply H.

(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  intros.
(* Goal: Rle (Rabs x) y *)
  unfold Rabs.
(* Goal: Rle (if Rcase_abs x then Ropp x else x) y *)
  destruct (Rcase_abs x).
(* Goal: Rle (Ropp x) y *)
(* Goal: Rle x y *)
  rewrite <-(Ropp_involutive y).
(* Goal: Rle (Ropp (Rminus z y)) (Ropp (Rminus x y)) *)
(* Goal: Rle (Rminus x y) (IZR Z0) *)
(* Goal: Rle (Rminus z y) (IZR Z0) *)
(* Goal: Rle (Rabs (Rminus z y)) (Rabs (Rminus x y)) *)
  apply Ropp_le_contravar.
(* Goal: Rle z x *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
  apply H.
(* Goal: Rle z x *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
  apply H.
Qed.

Lemma Rabs_le_between' : forall x y z, Rabs (x - y) <= z <-> y-z <= x <= y+z.
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  split ; intros.
(* Goal: and (Rle (Rminus y z) x) (Rle x (Rplus y z)) *)
(* Goal: Rle (Rabs (Rminus x y)) z *)
  cut (-z <= x-y <= z).
(* Goal: and (Rle (Rmin a b) x) (Rle x (Rmax a b)) *)
  intros ; split.
(* Goal: Rlt x (Rplus y z) *)
(* Goal: and (Rlt (Ropp z) (Rminus x y)) (Rlt (Rminus x y) z) *)
(* Goal: Rlt (Rabs (Rminus x y)) z *)
  rewrite <- (Rplus_0_l x).
(* Goal: Rlt (Rminus x y) (Rplus z (IZR Z0)) *)
  rewrite <-(Rplus_opp_r y).
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite Rplus_assoc.
(* Goal: Rle (Rplus y (Rplus (Ropp y) x)) (Rplus y z) *)
(* Goal: and (Rle (Ropp z) (Rminus x y)) (Rle (Rminus x y) z) *)
(* Goal: Rle (Rabs (Rminus x y)) z *)
  apply Rplus_le_compat_l.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite Rplus_comm.
(* Goal: Rlt (Rplus x (Ropp y)) z *)
(* Goal: and (Rlt (Ropp z) (Rminus x y)) (Rlt (Rminus x y) z) *)
(* Goal: Rlt (Rabs (Rminus x y)) z *)
  apply H0.
(* Goal: Rlt x (Rplus y z) *)
(* Goal: and (Rlt (Ropp z) (Rminus x y)) (Rlt (Rminus x y) z) *)
(* Goal: Rlt (Rabs (Rminus x y)) z *)
  rewrite <- (Rplus_0_l x).
(* Goal: Rlt (Rminus x y) (Rplus z (IZR Z0)) *)
  rewrite <-(Rplus_opp_r y).
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite Rplus_assoc.
(* Goal: Rle (Rplus y (Rplus (Ropp y) x)) (Rplus y z) *)
(* Goal: and (Rle (Ropp z) (Rminus x y)) (Rle (Rminus x y) z) *)
(* Goal: Rle (Rabs (Rminus x y)) z *)
  apply Rplus_le_compat_l.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite Rplus_comm.
(* Goal: Rlt (Rplus x (Ropp y)) z *)
(* Goal: and (Rlt (Ropp z) (Rminus x y)) (Rlt (Rminus x y) z) *)
(* Goal: Rlt (Rabs (Rminus x y)) z *)
  apply H0.
(* Goal: Rle (Rabs (Rminus x y)) z *)
  apply (Rabs_le_between (x-y) z).
(* Goal: Rle z x *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
  apply H.

(* Goal: Rle (Rabs (Rminus x y)) z *)
  apply (Rabs_le_between (x-y) z).
(* Goal: and (Rle (Rmin a b) x) (Rle x (Rmax a b)) *)
  split.
(* Goal: Rlt (Ropp z) (Rminus x y) *)
(* Goal: Rlt (Rminus x y) z *)
  rewrite <- (Rplus_0_r (-z)).
(* Goal: Rlt (Rminus x y) (Rplus z (IZR Z0)) *)
  rewrite <-(Rplus_opp_r y).
(* Goal: Rlt (Rminus x y) (Rplus z (Rplus y (Ropp y))) *)
  rewrite <- Rplus_assoc.
(* Goal: Rle (Rminus z y) (Rminus x y) *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
  apply Rplus_le_compat_r.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite Rplus_comm.
(* Goal: Rle z x *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
  apply H.
(* Goal: Rlt (Rminus x y) z *)
  rewrite <- (Rplus_0_r z).
(* Goal: Rlt (Rminus x y) (Rplus z (IZR Z0)) *)
  rewrite <-(Rplus_opp_r y).
(* Goal: Rlt (Rminus x y) (Rplus z (Rplus y (Ropp y))) *)
  rewrite <- Rplus_assoc.
(* Goal: Rle (Rminus z y) (Rminus x y) *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
  apply Rplus_le_compat_r.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite Rplus_comm.
(* Goal: Rle z x *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
  apply H.
Qed.

Lemma Rabs_lt_between : forall x y, (Rabs x < y <-> -y < x < y).
Proof.
(* Goal: and (Rle (Rmin a b) x) (Rle x (Rmax a b)) *)
  split.
(* Goal: forall _ : Rlt (Rabs x) y, and (Rlt (Ropp y) x) (Rlt x y) *)
(* Goal: forall _ : and (Rlt (Ropp y) x) (Rlt x y), Rlt (Rabs x) y *)
  intros; split; now apply Rabs_def2.
(* Goal: forall _ : and (Rlt (Ropp y) x) (Rlt x y), Rlt (Rabs x) y *)
  intros (H1,H2); now apply Rabs_def1.
Qed.

Lemma Rabs_lt_between' : forall x y z, Rabs (x - y) < z <-> y-z < x < y+z.
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  split ; intros.
(* Goal: and (Rlt (Rminus y z) x) (Rlt x (Rplus y z)) *)
(* Goal: Rlt (Rabs (Rminus x y)) z *)
  cut (-z < x-y < z).
(* Goal: and (Rle (Rmin a b) x) (Rle x (Rmax a b)) *)
  intros ; split.
(* Goal: Rlt x (Rplus y z) *)
(* Goal: and (Rlt (Ropp z) (Rminus x y)) (Rlt (Rminus x y) z) *)
(* Goal: Rlt (Rabs (Rminus x y)) z *)
  rewrite <- (Rplus_0_l x).
(* Goal: Rlt (Rminus x y) (Rplus z (IZR Z0)) *)
  rewrite <-(Rplus_opp_r y).
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite Rplus_assoc.
(* Goal: Rlt (Rplus y (Rplus (Ropp y) x)) (Rplus y z) *)
(* Goal: and (Rlt (Ropp z) (Rminus x y)) (Rlt (Rminus x y) z) *)
(* Goal: Rlt (Rabs (Rminus x y)) z *)
  apply Rplus_lt_compat_l.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite Rplus_comm.
(* Goal: Rlt (Rplus x (Ropp y)) z *)
(* Goal: and (Rlt (Ropp z) (Rminus x y)) (Rlt (Rminus x y) z) *)
(* Goal: Rlt (Rabs (Rminus x y)) z *)
  apply H0.
(* Goal: Rlt x (Rplus y z) *)
(* Goal: and (Rlt (Ropp z) (Rminus x y)) (Rlt (Rminus x y) z) *)
(* Goal: Rlt (Rabs (Rminus x y)) z *)
  rewrite <- (Rplus_0_l x).
(* Goal: Rlt (Rminus x y) (Rplus z (IZR Z0)) *)
  rewrite <-(Rplus_opp_r y).
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite Rplus_assoc.
(* Goal: Rlt (Rplus y (Rplus (Ropp y) x)) (Rplus y z) *)
(* Goal: and (Rlt (Ropp z) (Rminus x y)) (Rlt (Rminus x y) z) *)
(* Goal: Rlt (Rabs (Rminus x y)) z *)
  apply Rplus_lt_compat_l.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite Rplus_comm.
(* Goal: Rlt (Rplus x (Ropp y)) z *)
(* Goal: and (Rlt (Ropp z) (Rminus x y)) (Rlt (Rminus x y) z) *)
(* Goal: Rlt (Rabs (Rminus x y)) z *)
  apply H0.
(* Goal: Rlt (Rabs (Rminus x y)) z *)
  apply (Rabs_lt_between (x-y) z).
(* Goal: Rle z x *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
  apply H.

(* Goal: Rlt (Rabs (Rminus x y)) z *)
  apply (Rabs_lt_between (x-y) z).
(* Goal: and (Rle (Rmin a b) x) (Rle x (Rmax a b)) *)
  split.
(* Goal: Rlt (Ropp z) (Rminus x y) *)
(* Goal: Rlt (Rminus x y) z *)
  rewrite <- (Rplus_0_r (-z)).
(* Goal: Rlt (Rminus x y) (Rplus z (IZR Z0)) *)
  rewrite <-(Rplus_opp_r y).
(* Goal: Rlt (Rminus x y) (Rplus z (Rplus y (Ropp y))) *)
  rewrite <- Rplus_assoc.
(* Goal: Rlt (Rminus x y) (Rplus (Rplus z y) (Ropp y)) *)
  apply Rplus_lt_compat_r.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite Rplus_comm.
(* Goal: Rle z x *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
  apply H.
(* Goal: Rlt (Rminus x y) z *)
  rewrite <- (Rplus_0_r z).
(* Goal: Rlt (Rminus x y) (Rplus z (IZR Z0)) *)
  rewrite <-(Rplus_opp_r y).
(* Goal: Rlt (Rminus x y) (Rplus z (Rplus y (Ropp y))) *)
  rewrite <- Rplus_assoc.
(* Goal: Rlt (Rminus x y) (Rplus (Rplus z y) (Ropp y)) *)
  apply Rplus_lt_compat_r.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite Rplus_comm.
(* Goal: Rle z x *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
  apply H.
Qed.

Lemma Rabs_le_between_min_max : forall x y z, Rmin x y <= z <= Rmax x y -> Rabs (z - y) <= Rabs (x - y).
Proof.
(* Goal: forall (x y z : R) (_ : and (Rle (Rmin x y) z) (Rle z (Rmax x y))), Rle (Rabs (Rminus z y)) (Rabs (Rminus x y)) *)
 intros x y z H.
(* Goal: Rle (Rabs (Rminus z y)) (Rabs (Rminus x y)) *)
 case (Rle_or_lt x y); intros H'.
 (* *)
(* Goal: Rle (Rabs (Rminus z y)) (Rabs (Rminus x y)) *)
(* Goal: Rle (Rabs (Rminus z y)) (Rabs (Rminus x y)) *)
 rewrite Rmin_left in H;[idtac|exact H'].
(* Goal: Rle (Rabs (Rminus z y)) (Rabs (Rminus x y)) *)
(* Goal: Rle (Rabs (Rminus z y)) (Rabs (Rminus x y)) *)
 rewrite Rmax_right in H;[idtac|exact H'].
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
 rewrite Rabs_left1.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
 rewrite Rabs_left1.
(* Goal: Rle (Ropp (Rminus z y)) (Ropp (Rminus x y)) *)
(* Goal: Rle (Rminus x y) (IZR Z0) *)
(* Goal: Rle (Rminus z y) (IZR Z0) *)
(* Goal: Rle (Rabs (Rminus z y)) (Rabs (Rminus x y)) *)
 apply Ropp_le_contravar.
(* Goal: Rle (Rminus z y) (Rminus x y) *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
 apply Rplus_le_compat_r.
(* Goal: Rle z x *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
 apply H.
(* Goal: Rle (Rminus x y) (IZR Z0) *)
(* Goal: Rle (Rminus z y) (IZR Z0) *)
(* Goal: Rle (Rabs (Rminus z y)) (Rabs (Rminus x y)) *)
 apply Rle_minus; exact H'.
(* Goal: Rle z x *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
 apply Rle_minus; apply H.
 (* *)
(* Goal: Rle (Rabs (Rminus z y)) (Rabs (Rminus x y)) *)
 rewrite Rmin_right in H;[idtac|left; exact H'].
(* Goal: Rle (Rabs (Rminus z y)) (Rabs (Rminus x y)) *)
 rewrite Rmax_left in H;[idtac|left; exact H'].
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
 rewrite Rabs_right.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
 rewrite Rabs_right.
(* Goal: Rle (Rminus z y) (Rminus x y) *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
 apply Rplus_le_compat_r.
(* Goal: Rle z x *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
 apply H.
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
 apply Rge_minus; left; apply H'.
(* Goal: Rle z x *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
 apply Rge_minus, Rle_ge; apply H.
 Qed.

Lemma Rabs_le_between_Rmax : forall x m M,
  m <= x <= M -> Rabs x <= Rmax M (-m).
Proof.
(* Goal: forall (x m M : R) (_ : and (Rlt m x) (Rlt x M)), Rlt (Rabs x) (Rmax M (Ropp m)) *)
  intros x m M Hx.
  unfold Rabs ;
  destruct Rcase_abs as [H|H].
(* Goal: Rle (pos_Rl ly i) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
(* Goal: open_interval (pos_Rl (cons h' (cons h l)) i) (pos_Rl (cons h' (cons h l)) (S i)) x *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  apply Rle_trans with (2 := RmaxLess2 _ _).
(* Goal: Rle (Ropp x) (Ropp m) *)
(* Goal: Rle x (Rmax M (Ropp m)) *)
  apply Ropp_le_contravar, Hx.
(* Goal: Rle (f (pos_Rl (cons h' (cons h l)) (Init.Nat.pred (Rlength (cons h' (cons h l)))))) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  apply Rle_trans with (2 := RmaxLess1 _ _).
(* Goal: Rlt x M *)
  apply Hx.
Qed.

Lemma Rabs_lt_between_Rmax : forall x m M,
  m < x < M -> Rabs x < Rmax M (-m).
Proof.
(* Goal: forall (x m M : R) (_ : and (Rlt m x) (Rlt x M)), Rlt (Rabs x) (Rmax M (Ropp m)) *)
  intros x m M Hx.
  unfold Rabs ;
  destruct Rcase_abs as [H|H].
(* Goal: Rlt (Ropp x) (Rmax M (Ropp m)) *)
(* Goal: Rlt x (Rmax M (Ropp m)) *)
  apply Rlt_le_trans with (2 := RmaxLess2 _ _).
(* Goal: Rlt (Ropp x) (Ropp m) *)
(* Goal: Rlt x (Rmax M (Ropp m)) *)
  apply Ropp_lt_contravar, Hx.
(* Goal: Rlt x (Rmax M (Ropp m)) *)
  apply Rlt_le_trans with (2 := RmaxLess1 _ _).
(* Goal: Rlt x M *)
  apply Hx.
Qed.


Lemma Rabs_maj2 : forall x, -x <= Rabs x.
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  intros.
(* Goal: Rle (Ropp x) (Rabs x) *)
  rewrite <- Rabs_Ropp.
(* Goal: Rle (Ropp x) (Rabs (Ropp x)) *)
  apply Rle_abs.
Qed.


(** * Req *)

Lemma Req_lt_aux : forall x y, (forall eps : posreal, Rabs (x - y) < eps) -> x = y.
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  intros.
(* Goal: @eq R x y *)
  apply Rminus_diag_uniq.
(* Goal: @eq R (Rminus x y) (IZR Z0) *)
  apply Rabs_eq_0.
(* Goal: @eq R (Rabs (Rminus x y)) (IZR Z0) *)
  apply Rle_antisym.
(* Goal: Rle (Rabs (Rminus x y)) (IZR Z0) *)
(* Goal: Rle (IZR Z0) (Rabs (Rminus x y)) *)
  apply le_epsilon.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  intros.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite Rplus_0_l.
(* Goal: Rle (Rabs (Rminus x y)) eps *)
(* Goal: Rle (IZR Z0) (Rabs (Rminus x y)) *)
  apply Rlt_le.
(* Goal: Rle (Rabs (Rminus x y)) eps *)
(* Goal: Rle (IZR Z0) (Rabs (Rminus x y)) *)
  apply (H (mkposreal eps H0)).
(* Goal: Rle (IZR Z0) (Rabs (Rminus x y)) *)
  apply Rabs_pos.
Qed.

Lemma Req_le_aux : forall x y, (forall eps : posreal, Rabs (x - y) <= eps) -> x = y.
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  intros.
(* Goal: @eq R x y *)
  apply Rminus_diag_uniq.
(* Goal: @eq R (Rminus x y) (IZR Z0) *)
  apply Rabs_eq_0.
(* Goal: @eq R (Rabs (Rminus x y)) (IZR Z0) *)
  apply Rle_antisym.
(* Goal: Rle (Rabs (Rminus x y)) (IZR Z0) *)
(* Goal: Rle (IZR Z0) (Rabs (Rminus x y)) *)
  apply le_epsilon.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  intros.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite Rplus_0_l.
(* Goal: Rle (Rabs (Rminus x y)) eps *)
(* Goal: Rle (IZR Z0) (Rabs (Rminus x y)) *)
  apply (H (mkposreal eps H0)).
(* Goal: Rle (IZR Z0) (Rabs (Rminus x y)) *)
  apply Rabs_pos.
Qed.


(** * posreal *)

Lemma is_pos_div_2 (eps : posreal) : 0 < eps / 2.
Proof.
  unfold Rdiv ; apply Rmult_lt_0_compat ;
  [apply eps | apply Rinv_0_lt_compat, Rlt_0_2].
Qed.
Definition pos_div_2 (eps : posreal) := mkposreal _ (is_pos_div_2 eps).

(** * The sign function *)

Definition sign (x : R) :=
  match total_order_T 0 x with
  | inleft (left _) => 1
  | inleft (right _) => 0
  | inright _ => -1
  end.

Lemma sign_0 : sign 0 = 0.
Proof.
(* Goal: @eq R (Rmult (sign (Rminus b a)) (Rminus (Rmax a b) (Rmin a b))) (Rminus b a) *)
unfold sign.
(* Goal: @eq R (Rmult match total_order_T (IZR Z0) (Rminus b a) with | inleft (left r as s) => IZR (Zpos xH) | inleft (right e as s) => IZR Z0 | inright r => IZR (Zneg xH) end (Rminus (Rmax a b) (Rmin a b))) (Rminus b a) *)
case total_order_T as [[H|H]|H].
(* Goal: @eq R (IZR (Zneg xH)) (IZR Z0) *)
elim (Rlt_irrefl _ H).
(* Goal: Rlt x y *)
(* Goal: Rle (ln x) (ln y) *)
exact H.
(* Goal: @eq R (IZR (Zneg xH)) (IZR Z0) *)
elim (Rlt_irrefl _ H).
Qed.

Lemma sign_opp (x : R) : sign (-x) = - sign x.
Proof.
(* Goal: @eq R (Rmult (sign (Rminus b a)) (Rminus (Rmax a b) (Rmin a b))) (Rminus b a) *)
unfold sign.
case total_order_T as [[H|H]|H] ;
  case total_order_T as [[H'|H']|H'] ;
  lra.
Qed.

Lemma sign_eq_1 (x : R) : 0 < x -> sign x = 1.
Proof.
(* Goal: forall _ : not (@eq R x (IZR Z0)), not (@eq R (sign x) (IZR Z0)) *)
intros Hx.
(* Goal: @eq R (Rmult (sign (Rminus b a)) (Rminus (Rmax a b) (Rmin a b))) (Rminus b a) *)
unfold sign.
(* Goal: not (@eq R match total_order_T (IZR Z0) x with | inleft (left r as s) => IZR (Zpos xH) | inleft (right e as s) => IZR Z0 | inright r => IZR (Zneg xH) end (IZR Z0)) *)
case total_order_T as [[H|H]|H] ; lra.
Qed.

Lemma sign_eq_m1 (x : R) : x < 0 -> sign x = -1.
Proof.
(* Goal: forall _ : not (@eq R x (IZR Z0)), not (@eq R (sign x) (IZR Z0)) *)
intros Hx.
(* Goal: @eq R (Rmult (sign (Rminus b a)) (Rminus (Rmax a b) (Rmin a b))) (Rminus b a) *)
unfold sign.
(* Goal: not (@eq R match total_order_T (IZR Z0) x with | inleft (left r as s) => IZR (Zpos xH) | inleft (right e as s) => IZR Z0 | inright r => IZR (Zneg xH) end (IZR Z0)) *)
case total_order_T as [[H|H]|H] ; lra.
Qed.

Lemma sign_le (x y : R) : x <= y -> sign x <= sign y.
Proof.
(* Goal: forall _ : not (@eq R x (IZR Z0)), not (@eq R (sign x) (IZR Z0)) *)
intros Hx.
(* Goal: @eq R (Rmult (sign (Rminus b a)) (Rminus (Rmax a b) (Rmin a b))) (Rminus b a) *)
unfold sign.
case total_order_T as [[H|H]|H] ;
  case total_order_T as [[H'|H']|H'] ;
  lra.
Qed.

Lemma sign_ge_0 (x : R) : 0 <= x -> 0 <= sign x.
Proof.
(* Goal: forall _ : not (@eq R x (IZR Z0)), not (@eq R (sign x) (IZR Z0)) *)
intros Hx.
(* Goal: Rle (sign x) (IZR Z0) *)
rewrite <- sign_0.
(* Goal: Rle (sign x) (sign (IZR Z0)) *)
now apply sign_le.
Qed.

Lemma sign_le_0 (x : R) : x <= 0 -> sign x <= 0.
Proof.
(* Goal: forall _ : not (@eq R x (IZR Z0)), not (@eq R (sign x) (IZR Z0)) *)
intros Hx.
(* Goal: Rle (sign x) (IZR Z0) *)
rewrite <- sign_0.
(* Goal: Rle (sign x) (sign (IZR Z0)) *)
now apply sign_le.
Qed.

Lemma sign_neq_0 (x : R) : x <> 0 -> sign x <> 0.
Proof.
(* Goal: forall _ : not (@eq R x (IZR Z0)), not (@eq R (sign x) (IZR Z0)) *)
intros Hx.
(* Goal: @eq R (Rmult (sign (Rminus b a)) (Rminus (Rmax a b) (Rmin a b))) (Rminus b a) *)
unfold sign.
(* Goal: not (@eq R match total_order_T (IZR Z0) x with | inleft (left r as s) => IZR (Zpos xH) | inleft (right e as s) => IZR Z0 | inright r => IZR (Zneg xH) end (IZR Z0)) *)
case total_order_T as [[H|H]|H] ; lra.
Qed.

Lemma sign_mult (x y : R) : sign (x * y) = sign x * sign y.
Proof.
(* Goal: open_interval (pos_Rl (cons h' (cons h l)) i) (pos_Rl (cons h' (cons h l)) (S i)) x *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  wlog: x / (0 < x) => [Hw | Hx].
(* Goal: Rle (f (pos_Rl (cons h'0 (cons h0 l0)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h'0) (cons (f h0) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l0)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
    case: (Rle_lt_dec 0 x) => Hx.
(* Goal: Rle (f (pos_Rl (cons h'0 (cons h0 l0)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h'0) (cons (f h0) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l0)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
    case: Hx => Hx.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
    by apply Hw.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
    rewrite -Hx Rmult_0_l.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
    rewrite sign_0.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
    by rewrite Rmult_0_l.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
    rewrite -(Ropp_involutive x).
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
    rewrite sign_opp Ropp_mult_distr_l_reverse sign_opp Hw.
(* Goal: @eq R (Rplus (ln x) (Ropp (ln y))) (Rminus (ln x) (ln y)) *)
(* Goal: Rlt (IZR Z0) y *)
(* Goal: Rlt (IZR Z0) x *)
(* Goal: Rlt (IZR Z0) (Rinv y) *)
    ring.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
    by apply Ropp_0_gt_lt_contravar.
(* Goal: open_interval (pos_Rl (cons h' (cons h l)) i) (pos_Rl (cons h' (cons h l)) (S i)) x *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  wlog: y / (0 < y) => [Hw | Hy].
(* Goal: Rle (f (pos_Rl (cons h'0 (cons h0 l0)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h'0) (cons (f h0) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l0)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
    case: (Rle_lt_dec 0 y) => Hy.
(* Goal: Rle (f (pos_Rl (cons h'0 (cons h0 l0)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h'0) (cons (f h0) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l0)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
    case: Hy => Hy.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
    by apply Hw.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
    rewrite -Hy Rmult_0_r.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
    rewrite sign_0.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
    by rewrite Rmult_0_r.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
    rewrite -(Ropp_involutive y).
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
    rewrite sign_opp Ropp_mult_distr_r_reverse sign_opp Hw.
(* Goal: @eq R (Rplus (ln x) (Ropp (ln y))) (Rminus (ln x) (ln y)) *)
(* Goal: Rlt (IZR Z0) y *)
(* Goal: Rlt (IZR Z0) x *)
(* Goal: Rlt (IZR Z0) (Rinv y) *)
    ring.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
    by apply Ropp_0_gt_lt_contravar.
(* Goal: @ex nat (fun i : nat => and (lt i (S (Rlength (cons h'' l)))) (and (Rle (pos_Rl (cons h' (cons h (cons h'' l))) i) x) (Rlt x (pos_Rl (cons h' (cons h (cons h'' l))) (S i))))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  have Hxy : 0 < x * y.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
    by apply Rmult_lt_0_compat.
(* Goal: @eq R (sign (Rmult x y)) (Rmult (sign x) (sign y)) *)
  rewrite -> 3!sign_eq_1 by easy.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by rewrite Rmult_1_l.
Qed.

Lemma sign_min_max (a b : R) :
  sign (b - a) * (Rmax a b - Rmin a b) = b - a.
Proof.
(* Goal: @eq R (Rmult (sign (Rminus b a)) (Rminus (Rmax a b) (Rmin a b))) (Rminus b a) *)
unfold sign.
(* Goal: @eq R (Rmult match total_order_T (IZR Z0) (Rminus b a) with | inleft (left r as s) => IZR (Zpos xH) | inleft (right e as s) => IZR Z0 | inright r => IZR (Zneg xH) end (Rminus (Rmax a b) (Rmin a b))) (Rminus b a) *)
case total_order_T as [[H|H]|H].
(* Goal: @eq R (Rmult (IZR (Zpos xH)) (Rminus (Rmax a b) (Rmin a b))) (Rminus b a) *)
(* Goal: @eq R (Rmult (IZR Z0) (Rminus (Rmax a b) (Rmin a b))) (Rminus b a) *)
(* Goal: @eq R (Rmult (IZR (Zneg xH)) (Rminus (Rmax a b) (Rmin a b))) (Rminus b a) *)
assert (K := proj2 (Rminus_le_0 a b) (Rlt_le _ _ H)).
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
rewrite (Rmax_right _ _ K) (Rmin_left _ _ K).
(* Goal: @eq R (Rmult (IZR (Zpos xH)) (Rminus b a)) (Rminus b a) *)
(* Goal: @eq R (Rmult (IZR Z0) (Rminus (Rmax a b) (Rmin a b))) (Rminus b a) *)
(* Goal: @eq R (Rmult (IZR (Zneg xH)) (Rminus (Rmax a b) (Rmin a b))) (Rminus b a) *)
apply Rmult_1_l.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
rewrite -H.
(* Goal: @eq R (Rmult (IZR Z0) (Rminus (Rmax a b) (Rmin a b))) (IZR Z0) *)
(* Goal: @eq R (Rmult (IZR (Zneg xH)) (Rminus (Rmax a b) (Rmin a b))) (Rminus b a) *)
apply Rmult_0_l.
(* Goal: @eq R (Rmult (IZR (Zneg xH)) (Rminus (Rmax a b) (Rmin a b))) (Rminus b a) *)
assert (K : b <= a).
(* Goal: Rle b a *)
(* Goal: @eq R (Rmult (IZR (Zneg xH)) (Rminus (Rmax a b) (Rmin a b))) (Rminus b a) *)
apply Rnot_lt_le.
(* Goal: not (Rlt a b) *)
(* Goal: @eq R (Rmult (IZR (Zneg xH)) (Rminus (Rmax a b) (Rmin a b))) (Rminus b a) *)
contradict H.
(* Goal: not (Rgt (IZR Z0) (Rminus b a)) *)
(* Goal: @eq R (Rmult (IZR (Zneg xH)) (Rminus (Rmax a b) (Rmin a b))) (Rminus b a) *)
apply Rle_not_lt.
(* Goal: Rle (IZR Z0) (Rminus b a) *)
(* Goal: @eq R (Rmult (IZR (Zneg xH)) (Rminus (Rmax a b) (Rmin a b))) (Rminus b a) *)
apply -> Rminus_le_0.
(* Goal: Rle (Rabs (Rminus x y)) eps *)
(* Goal: Rle (IZR Z0) (Rabs (Rminus x y)) *)
now apply Rlt_le.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
rewrite (Rmax_left _ _ K) (Rmin_right _ _ K).
(* Goal: @eq R (Rplus (ln x) (Ropp (ln y))) (Rminus (ln x) (ln y)) *)
(* Goal: Rlt (IZR Z0) y *)
(* Goal: Rlt (IZR Z0) x *)
(* Goal: Rlt (IZR Z0) (Rinv y) *)
ring.
Qed.

Lemma sum_INR : forall n, sum_f_R0 INR n = INR n * (INR n + 1) / 2.
Proof.
(* Goal: forall n : nat, @eq R (sum_f_R0 INR n) (Rdiv (Rmult (INR n) (Rplus (INR n) (IZR (Zpos xH)))) (IZR (Zpos (xO xH)))) *)
  elim => [ | n IH] ; rewrite /sum_f_R0 -/sum_f_R0 ?S_INR /=.
(* Goal: @eq R (Rplus (ln x) (Ropp (ln y))) (Rminus (ln x) (ln y)) *)
(* Goal: Rlt (IZR Z0) y *)
(* Goal: Rlt (IZR Z0) x *)
(* Goal: Rlt (IZR Z0) (Rinv y) *)
  rewrite /Rdiv ; ring.
(* Goal: @eq R (Rminus (Rdiv a b) c) (Rdiv (Rminus a (Rmult b c)) b) *)
  rewrite IH ; field.
Qed.

(** * ssreflect.seq *)
(** Finite subdivision *)

Lemma interval_finite_subdiv (a b : R) (eps : posreal) : (a <= b) ->
  {l : seq R | head 0 l = a /\ last 0 l = b /\
    forall i, (S i < size l)%nat -> nth 0 l i < nth 0 l (S i) <= nth 0 l i + eps}.
Proof.
(* Goal: forall (n : nat) (_ : forall (h h' : R) (l : Rlist), Rle (f (pos_Rl (cons h' (cons h l)) (S n))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l0 : Rlist) : Rlist := match l0 with | nil => nil | cons h0 t => cons (f h0) (app t) end) l))))) (h h' : R) (l : Rlist), Rle (f (pos_Rl (cons h' (cons h l)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l0 : Rlist) : Rlist := match l0 with | nil => nil | cons h0 t => cons (f h0) (app t) end) l)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  move => Hab.
(* Goal: and (Rle (@nth R (IZR Z0) l O) (@nth R (IZR Z0) l i)) (Rle (@nth R (IZR Z0) l i) (@nth R (IZR Z0) (@Datatypes.cons R (IZR Z0) l) (@size R l))) *)
  suff Hn : 0 <= (b - a) / eps.
(* Goal: @sig (list R) (fun l : list R => and (@eq R (@head R (IZR Z0) l) a) (and (@eq R (@last R (IZR Z0) l) b) (forall (i : nat) (_ : lt (S i) (@size R l)), and (Rlt (@nth R (IZR Z0) l i) (@nth R (IZR Z0) l (S i))) (Rle (@nth R (IZR Z0) l (S i)) (Rplus (@nth R (IZR Z0) l i) (pos eps)))))) *)
(* Goal: Rle (IZR Z0) (Rdiv (Rminus b a) (pos eps)) *)
  set n : nat := nfloor ((b - a) / eps) Hn.

(* Goal: Rle (f (pos_Rl (cons h'0 (cons h0 l0)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h'0) (cons (f h0) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l0)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  case: (Req_EM_T (INR n) ((b - a) / eps)) => Hdec.

(* Goal: @sig (list R) (fun l : list R => and (@eq R (@head R (IZR Z0) l) a) (and (@eq R (@last R (IZR Z0) l) b) (forall (i : nat) (_ : lt (S i) (@size R l)), and (Rlt (@nth R (IZR Z0) l i) (@nth R (IZR Z0) l (S i))) (Rle (@nth R (IZR Z0) l (S i)) (Rplus (@nth R (IZR Z0) l i) (pos eps)))))) *)
(* Goal: Rle (IZR Z0) (Rdiv (Rminus b a) (pos eps)) *)
  set l : seq R := mkseq (fun k => a + INR k * eps) (S n).
(* Goal: @sig (list R) (fun l : list R => and (@eq R (@head R (IZR Z0) l) a) (and (@eq R (@last R (IZR Z0) l) b) (forall (i : nat) (_ : lt (S i) (@size R l)), and (Rlt (@nth R (IZR Z0) l i) (@nth R (IZR Z0) l (S i))) (Rle (@nth R (IZR Z0) l (S i)) (Rplus (@nth R (IZR Z0) l i) (pos eps)))))) *)
(* Goal: Rle (IZR Z0) (Rdiv (Rminus b a) (pos eps)) *)
  exists l.
(* Goal: and (Rle (Rmin a b) x) (Rle x (Rmax a b)) *)
  split.
(* Goal: @eq R (Rplus (ln x) (Ropp (ln y))) (Rminus (ln x) (ln y)) *)
(* Goal: Rlt (IZR Z0) y *)
(* Goal: Rlt (IZR Z0) x *)
(* Goal: Rlt (IZR Z0) (Rinv y) *)
  simpl ; rewrite /Rdiv ; ring.
(* Goal: and (Rle (Rmin a b) x) (Rle x (Rmax a b)) *)
  split.
(* Goal: @eq R (@last R (IZR Z0) l) b *)
(* Goal: forall (i : nat) (_ : lt (S i) (@size R l)), and (Rlt (@nth R (IZR Z0) l i) (@nth R (IZR Z0) l (S i))) (Rle (@nth R (IZR Z0) l (S i)) (Rplus (@nth R (IZR Z0) l i) (pos eps))) *)
(* Goal: @sig (list R) (fun l : list R => and (@eq R (@head R (IZR Z0) l) a) (and (@eq R (@last R (IZR Z0) l) b) (forall (i : nat) (_ : lt (S i) (@size R l)), and (Rlt (@nth R (IZR Z0) l i) (@nth R (IZR Z0) l (S i))) (Rle (@nth R (IZR Z0) l (S i)) (Rplus (@nth R (IZR Z0) l i) (pos eps)))))) *)
(* Goal: Rle (IZR Z0) (Rdiv (Rminus b a) (pos eps)) *)
  replace b with (a + INR n * eps).
(* Goal: @eq R (@last R (IZR Z0) l) (Rplus a (Rmult (INR n) (pos eps))) *)
(* Goal: @eq R (Rplus a (Rmult (INR n) (pos eps))) b *)
(* Goal: forall (i : nat) (_ : lt (S i) (@size R l)), and (Rlt (@nth R (IZR Z0) l i) (@nth R (IZR Z0) l (S i))) (Rle (@nth R (IZR Z0) l (S i)) (Rplus (@nth R (IZR Z0) l i) (pos eps))) *)
(* Goal: @sig (list R) (fun l : list R => and (@eq R (@head R (IZR Z0) l) a) (and (@eq R (@last R (IZR Z0) l) b) (forall (i : nat) (_ : lt (S i) (@size R l)), and (Rlt (@nth R (IZR Z0) l i) (@nth R (IZR Z0) l (S i))) (Rle (@nth R (IZR Z0) l (S i)) (Rplus (@nth R (IZR Z0) l i) (pos eps)))))) *)
(* Goal: Rle (IZR Z0) (Rdiv (Rminus b a) (pos eps)) *)
  simpl.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite (last_map (fun k => a + INR k * eps) _ O) /=.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite (last_nth O) size_iota /=.
(* Goal: Rle (f (pos_Rl (cons h'0 (cons h0 l0)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h'0) (cons (f h0) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l0)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  case H : n => [ | m].
(* Goal: @eq R (@last R (IZR Z0) l) (Rplus a (Rmult (INR n) (pos eps))) *)
(* Goal: @eq R (Rplus a (Rmult (INR n) (pos eps))) b *)
(* Goal: forall (i : nat) (_ : lt (S i) (@size R l)), and (Rlt (@nth R (IZR Z0) l i) (@nth R (IZR Z0) l (S i))) (Rle (@nth R (IZR Z0) l (S i)) (Rplus (@nth R (IZR Z0) l i) (pos eps))) *)
(* Goal: @sig (list R) (fun l : list R => and (@eq R (@head R (IZR Z0) l) a) (and (@eq R (@last R (IZR Z0) l) b) (forall (i : nat) (_ : lt (S i) (@size R l)), and (Rlt (@nth R (IZR Z0) l i) (@nth R (IZR Z0) l (S i))) (Rle (@nth R (IZR Z0) l (S i)) (Rplus (@nth R (IZR Z0) l i) (pos eps)))))) *)
(* Goal: Rle (IZR Z0) (Rdiv (Rminus b a) (pos eps)) *)
  by simpl.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by rewrite /nth -/(nth _ _ m) nth_iota.
(* Goal: @eq R (Rminus (Rdiv a b) c) (Rdiv (Rminus a (Rmult b c)) b) *)
  rewrite Hdec ; field.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by apply Rgt_not_eq, eps.
  move => i Hi ;
  rewrite size_mkseq in Hi.
(* Goal: and (Rle (Rmin a b) x) (Rle x (Rmax a b)) *)
  split.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite ?nth_mkseq //.
(* Goal: Rle (Rplus a (Rmult (INR (S i)) (pos eps))) (Rplus (Rplus a (Rmult (INR i) (pos eps))) (pos eps)) *)
(* Goal: Rle (if @eqtype.eq_op ssrnat.nat_eqType (S i) (S n) then b else IZR Z0) (Rplus (@nth R (IZR Z0) (@mkseq R (fun k : nat => Rplus a (Rmult (INR k) (pos eps))) (S n)) i) (pos eps)) *)
(* Goal: Rle (IZR Z0) (Rdiv (Rminus b a) (pos eps)) *)
  rewrite S_INR Rminus_lt_0 ; ring_simplify.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by apply eps.
  elim: (S n) (S i) Hi => /= [ | m IH] ;
  case => /= [ | j] Hj //.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by apply lt_irrefl in Hj.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by apply lt_n_O in Hj.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by apply IH, lt_S_n.
  elim: (S n) (S i) Hi => /= [ | m IH] ;
  case => /= [ | j] Hj //.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by apply lt_n_O in Hj.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by apply IH, lt_S_n.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite ?nth_mkseq //.
(* Goal: Rle (Rplus a (Rmult (INR (S i)) (pos eps))) (Rplus (Rplus a (Rmult (INR i) (pos eps))) (pos eps)) *)
(* Goal: Rle (if @eqtype.eq_op ssrnat.nat_eqType (S i) (S n) then b else IZR Z0) (Rplus (@nth R (IZR Z0) (@mkseq R (fun k : nat => Rplus a (Rmult (INR k) (pos eps))) (S n)) i) (pos eps)) *)
(* Goal: Rle (IZR Z0) (Rdiv (Rminus b a) (pos eps)) *)
  rewrite S_INR Rminus_le_0 ; ring_simplify.
(* Goal: Rle (pos_Rl nil i) (Rl_max (IZR Z0) nil) *)
(* Goal: forall (r : R) (r0 : Rlist), Rle (pos_Rl (cons r r0) i) (Rl_max (IZR Z0) (cons r r0)) *)
(* Goal: open_interval (pos_Rl (cons h' (cons h l)) i) (pos_Rl (cons h' (cons h l)) (S i)) x *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  by apply Rle_refl.
  elim: (S n) (S i) Hi => /= [ | m IH] ;
  case => /= [ | j] Hj //.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by apply lt_n_O in Hj.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by apply IH, lt_S_n.
  elim: (S n) (S i) Hi => /= [ | m IH] ;
  case => /= [ | j] Hj //.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by apply lt_n_O in Hj.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by apply lt_n_O in Hj.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by apply IH, lt_S_n.

(* Goal: @sig (list R) (fun l : list R => and (@eq R (@head R (IZR Z0) l) a) (and (@eq R (@last R (IZR Z0) l) b) (forall (i : nat) (_ : lt (S i) (@size R l)), and (Rlt (@nth R (IZR Z0) l i) (@nth R (IZR Z0) l (S i))) (Rle (@nth R (IZR Z0) l (S i)) (Rplus (@nth R (IZR Z0) l i) (pos eps)))))) *)
(* Goal: Rle (IZR Z0) (Rdiv (Rminus b a) (pos eps)) *)
  set l : seq R := rcons (mkseq (fun k => a + INR k * eps) (S n)) b.
(* Goal: @sig (list R) (fun l : list R => and (@eq R (@head R (IZR Z0) l) a) (and (@eq R (@last R (IZR Z0) l) b) (forall (i : nat) (_ : lt (S i) (@size R l)), and (Rlt (@nth R (IZR Z0) l i) (@nth R (IZR Z0) l (S i))) (Rle (@nth R (IZR Z0) l (S i)) (Rplus (@nth R (IZR Z0) l i) (pos eps)))))) *)
(* Goal: Rle (IZR Z0) (Rdiv (Rminus b a) (pos eps)) *)
  exists l.
(* Goal: and (Rle (Rmin a b) x) (Rle x (Rmax a b)) *)
  split.
(* Goal: @eq R (Rplus (ln x) (Ropp (ln y))) (Rminus (ln x) (ln y)) *)
(* Goal: Rlt (IZR Z0) y *)
(* Goal: Rlt (IZR Z0) x *)
(* Goal: Rlt (IZR Z0) (Rinv y) *)
  simpl ; rewrite /Rdiv ; ring.
(* Goal: and (Rle (Rmin a b) x) (Rle x (Rmax a b)) *)
  split.
(* Goal: @eq R (@last R (IZR Z0) l) b *)
(* Goal: forall (i : nat) (_ : lt (S i) (@size R l)), and (Rlt (@nth R (IZR Z0) l i) (@nth R (IZR Z0) l (S i))) (Rle (@nth R (IZR Z0) l (S i)) (Rplus (@nth R (IZR Z0) l i) (pos eps))) *)
(* Goal: Rle (IZR Z0) (Rdiv (Rminus b a) (pos eps)) *)
  simpl ; by rewrite last_rcons.
  move => i Hi ;
  rewrite size_rcons size_mkseq in Hi ;
  apply lt_n_Sm_le, le_S_n in Hi.
(* Goal: and (Rle (Rmin a b) x) (Rle x (Rmax a b)) *)
  split.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite ?nth_rcons size_mkseq.
(* Goal: @ex nat (fun i : nat => and (lt i (S (Rlength (cons h'' l)))) (and (Rle (pos_Rl (cons h' (cons h (cons h'' l))) i) x) (Rlt x (pos_Rl (cons h' (cons h (cons h'' l))) (S i))))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  have H : ssrnat.leq (S i) (S n) = true.
    apply le_n_S in Hi ;
    elim: (S i) (S n) Hi => //= j IH ;
    case => //= [ | m] Hi.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
    by apply le_Sn_O in Hi.
(* Goal: @eq bool (ssrnat.leq (S j) (S m)) true *)
(* Goal: Rle (if ssrnat.leq (S (S i)) (S n) then @nth R (IZR Z0) (@mkseq R (fun k : nat => Rplus a (Rmult (INR k) (pos eps))) (S n)) (S i) else if @eqtype.eq_op ssrnat.nat_eqType (S i) (S n) then b else IZR Z0) (Rplus (if ssrnat.leq (S i) (S n) then @nth R (IZR Z0) (@mkseq R (fun k : nat => Rplus a (Rmult (INR k) (pos eps))) (S n)) i else if @eqtype.eq_op ssrnat.nat_eqType i (S n) then b else IZR Z0) (pos eps)) *)
(* Goal: Rle (IZR Z0) (Rdiv (Rminus b a) (pos eps)) *)
    apply IH ; by apply le_S_n.
(* Goal: Rle (f (pos_Rl (cons h'0 (cons h0 l0)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h'0) (cons (f h0) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l0)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  case: (ssrnat.leq (S i) (S n)) (H) => // _.
(* Goal: Rle (f (pos_Rl (cons h'0 (cons h0 l0)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h'0) (cons (f h0) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l0)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  case H0 : (ssrnat.leq (S (S i)) (S n)) => //.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite ?nth_mkseq //.
(* Goal: Rle (Rplus a (Rmult (INR (S i)) (pos eps))) (Rplus (Rplus a (Rmult (INR i) (pos eps))) (pos eps)) *)
(* Goal: Rle (if @eqtype.eq_op ssrnat.nat_eqType (S i) (S n) then b else IZR Z0) (Rplus (@nth R (IZR Z0) (@mkseq R (fun k : nat => Rplus a (Rmult (INR k) (pos eps))) (S n)) i) (pos eps)) *)
(* Goal: Rle (IZR Z0) (Rdiv (Rminus b a) (pos eps)) *)
  rewrite S_INR Rminus_lt_0 ; ring_simplify.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by apply eps.
(* Goal: Rle (if @eqtype.eq_op ssrnat.nat_eqType (S i) (S n) then b else IZR Z0) (Rplus (@nth R (IZR Z0) (@mkseq R (fun k : nat => Rplus a (Rmult (INR k) (pos eps))) (S n)) i) (pos eps)) *)
(* Goal: Rle (IZR Z0) (Rdiv (Rminus b a) (pos eps)) *)
  apply (f_equal negb) in H0 ; simpl in H0.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite -ssrnat.leqNgt in H0.
(* Goal: Rle (f (pos_Rl (cons h'0 (cons h0 l0)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h'0) (cons (f h0) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l0)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  case H1 : (@eqtype.eq_op ssrnat.nat_eqType (S i) (S n)) => //.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite ssrnat.eqSS /= in H1.
(* Goal: Rle b (Rplus (@nth R (IZR Z0) (@mkseq R (fun k : nat => Rplus a (Rmult (INR k) (pos eps))) (S n)) i) (pos eps)) *)
(* Goal: Rle (IZR Z0) (Rplus (@nth R (IZR Z0) (@mkseq R (fun k : nat => Rplus a (Rmult (INR k) (pos eps))) (S n)) i) (pos eps)) *)
(* Goal: Rle (IZR Z0) (Rdiv (Rminus b a) (pos eps)) *)
  replace i with n.
(* Goal: open_interval (pos_Rl (cons h' (cons h l)) i) (pos_Rl (cons h' (cons h l)) (S i)) x *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  rewrite nth_mkseq => //.
(* Goal: Rle b (Rplus (Rplus a (Rmult (INR n) (pos eps))) (pos eps)) *)
(* Goal: @eq nat n i *)
(* Goal: Rle (IZR Z0) (Rplus (@nth R (IZR Z0) (@mkseq R (fun k : nat => Rplus a (Rmult (INR k) (pos eps))) (S n)) i) (pos eps)) *)
(* Goal: Rle (IZR Z0) (Rdiv (Rminus b a) (pos eps)) *)
  move: Hdec ; rewrite /n /nfloor.
(* Goal: Rle (f (pos_Rl (cons h'0 (cons h0 l0)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h'0) (cons (f h0) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l0)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  case: nfloor_ex => {n Hn l Hi H H0 H1} n Hn /= Hdec.
(* Goal: Rlt (Rplus a (Rmult (INR n) (pos eps))) b *)
(* Goal: @eq nat n i *)
(* Goal: Rlt (@nth R (IZR Z0) (@mkseq R (fun k : nat => Rplus a (Rmult (INR k) (pos eps))) (S n)) i) (IZR Z0) *)
(* Goal: Rle (@nth R (IZR Z0) l (S i)) (Rplus (@nth R (IZR Z0) l i) (pos eps)) *)
(* Goal: Rle (IZR Z0) (Rdiv (Rminus b a) (pos eps)) *)
  rewrite Rplus_comm ; apply Rlt_minus_r.
(* Goal: Rlt (Rmult (INR n) (pos eps)) (Rminus b a) *)
(* Goal: @eq nat n i *)
(* Goal: Rlt (@nth R (IZR Z0) (@mkseq R (fun k : nat => Rplus a (Rmult (INR k) (pos eps))) (S n)) i) (IZR Z0) *)
(* Goal: Rle (@nth R (IZR Z0) l (S i)) (Rplus (@nth R (IZR Z0) l i) (pos eps)) *)
(* Goal: Rle (IZR Z0) (Rdiv (Rminus b a) (pos eps)) *)
  apply Rlt_div_r.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by apply eps.
(* Goal: and (Rle a (@nth R (IZR Z0) l i)) (Rle (@nth R (IZR Z0) l i) b) *)
  case: Hn => Hn _ ; case: Hn => // Hn.
  elim: n i H1 {Hi H H0 l Hdec} => [ | n IH] ;
  case => [ | i] // H.
(* Goal: @eq nat (S n) (S i) *)
(* Goal: Rle (IZR Z0) (Rplus (@nth R (IZR Z0) (@mkseq R (fun k : nat => Rplus a (Rmult (INR k) (pos eps))) (S n)) i) (pos eps)) *)
(* Goal: Rle (IZR Z0) (Rdiv (Rminus b a) (pos eps)) *)
  apply f_equal, IH ; intuition.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by rewrite ssrnat.eqn_leq H H0 in H1.

(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite ?nth_rcons size_mkseq.
(* Goal: @ex nat (fun i : nat => and (lt i (S (Rlength (cons h'' l)))) (and (Rle (pos_Rl (cons h' (cons h (cons h'' l))) i) x) (Rlt x (pos_Rl (cons h' (cons h (cons h'' l))) (S i))))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  have H : ssrnat.leq (S i) (S n) = true.
    apply le_n_S in Hi ;
    elim: (S i) (S n) Hi => //= j IH ;
    case => //= [ | m] Hi.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
    by apply le_Sn_O in Hi.
(* Goal: @eq bool (ssrnat.leq (S j) (S m)) true *)
(* Goal: Rle (if ssrnat.leq (S (S i)) (S n) then @nth R (IZR Z0) (@mkseq R (fun k : nat => Rplus a (Rmult (INR k) (pos eps))) (S n)) (S i) else if @eqtype.eq_op ssrnat.nat_eqType (S i) (S n) then b else IZR Z0) (Rplus (if ssrnat.leq (S i) (S n) then @nth R (IZR Z0) (@mkseq R (fun k : nat => Rplus a (Rmult (INR k) (pos eps))) (S n)) i else if @eqtype.eq_op ssrnat.nat_eqType i (S n) then b else IZR Z0) (pos eps)) *)
(* Goal: Rle (IZR Z0) (Rdiv (Rminus b a) (pos eps)) *)
    apply IH ; by apply le_S_n.
(* Goal: Rle (f (pos_Rl (cons h'0 (cons h0 l0)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h'0) (cons (f h0) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l0)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  case: (ssrnat.leq (S i) (S n)) (H) => // _.
(* Goal: Rle (f (pos_Rl (cons h'0 (cons h0 l0)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h'0) (cons (f h0) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l0)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  case H0 : (ssrnat.leq (S (S i)) (S n)) => //.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite ?nth_mkseq //.
(* Goal: Rle (Rplus a (Rmult (INR (S i)) (pos eps))) (Rplus (Rplus a (Rmult (INR i) (pos eps))) (pos eps)) *)
(* Goal: Rle (if @eqtype.eq_op ssrnat.nat_eqType (S i) (S n) then b else IZR Z0) (Rplus (@nth R (IZR Z0) (@mkseq R (fun k : nat => Rplus a (Rmult (INR k) (pos eps))) (S n)) i) (pos eps)) *)
(* Goal: Rle (IZR Z0) (Rdiv (Rminus b a) (pos eps)) *)
  rewrite S_INR Rminus_le_0 ; ring_simplify.
(* Goal: Rle (pos_Rl nil i) (Rl_max (IZR Z0) nil) *)
(* Goal: forall (r : R) (r0 : Rlist), Rle (pos_Rl (cons r r0) i) (Rl_max (IZR Z0) (cons r r0)) *)
(* Goal: open_interval (pos_Rl (cons h' (cons h l)) i) (pos_Rl (cons h' (cons h l)) (S i)) x *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  by apply Rle_refl.
(* Goal: Rle (if @eqtype.eq_op ssrnat.nat_eqType (S i) (S n) then b else IZR Z0) (Rplus (@nth R (IZR Z0) (@mkseq R (fun k : nat => Rplus a (Rmult (INR k) (pos eps))) (S n)) i) (pos eps)) *)
(* Goal: Rle (IZR Z0) (Rdiv (Rminus b a) (pos eps)) *)
  apply (f_equal negb) in H0 ; simpl in H0.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite -ssrnat.leqNgt in H0.
(* Goal: Rle (f (pos_Rl (cons h'0 (cons h0 l0)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h'0) (cons (f h0) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l0)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  case H1 : (@eqtype.eq_op ssrnat.nat_eqType (S i) (S n)) => //.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite ssrnat.eqSS /= in H1.
(* Goal: Rle b (Rplus (@nth R (IZR Z0) (@mkseq R (fun k : nat => Rplus a (Rmult (INR k) (pos eps))) (S n)) i) (pos eps)) *)
(* Goal: Rle (IZR Z0) (Rplus (@nth R (IZR Z0) (@mkseq R (fun k : nat => Rplus a (Rmult (INR k) (pos eps))) (S n)) i) (pos eps)) *)
(* Goal: Rle (IZR Z0) (Rdiv (Rminus b a) (pos eps)) *)
  replace i with n.
(* Goal: open_interval (pos_Rl (cons h' (cons h l)) i) (pos_Rl (cons h' (cons h l)) (S i)) x *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  rewrite nth_mkseq => //.
  move: Hdec ;
  rewrite /n /nfloor.
(* Goal: Rle (f (pos_Rl (cons h'0 (cons h0 l0)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h'0) (cons (f h0) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l0)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  case: nfloor_ex => {n Hn l Hi H H0 H1} n Hn /= Hdec.
(* Goal: Rle b (Rplus (Rplus a (Rmult (INR n) (pos eps))) (pos eps)) *)
(* Goal: @eq nat n i *)
(* Goal: Rle (IZR Z0) (Rplus (@nth R (IZR Z0) (@mkseq R (fun k : nat => Rplus a (Rmult (INR k) (pos eps))) (S n)) i) (pos eps)) *)
(* Goal: Rle (IZR Z0) (Rdiv (Rminus b a) (pos eps)) *)
  rewrite Rplus_assoc Rplus_comm ; apply Rle_minus_l.
(* Goal: @eq R (Rplus (ln x) (Ropp (ln y))) (Rminus (ln x) (ln y)) *)
(* Goal: Rlt (IZR Z0) y *)
(* Goal: Rlt (IZR Z0) x *)
(* Goal: Rlt (IZR Z0) (Rinv y) *)
  replace (INR n * eps + eps) with ((INR n + 1) * eps) by ring.
(* Goal: Rle (Rminus b a) (Rmult (Rplus (INR n) (IZR (Zpos xH))) (pos eps)) *)
(* Goal: @eq nat n i *)
(* Goal: Rle (IZR Z0) (Rplus (@nth R (IZR Z0) (@mkseq R (fun k : nat => Rplus a (Rmult (INR k) (pos eps))) (S n)) i) (pos eps)) *)
(* Goal: Rle (IZR Z0) (Rdiv (Rminus b a) (pos eps)) *)
  apply Rle_div_l.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by apply eps.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by apply Rlt_le, Hn.
  elim: n i H1 {Hi H H0 l Hdec} => [ | n IH] ;
  case => [ | i] // H.
(* Goal: @eq nat (S n) (S i) *)
(* Goal: Rle (IZR Z0) (Rplus (@nth R (IZR Z0) (@mkseq R (fun k : nat => Rplus a (Rmult (INR k) (pos eps))) (S n)) i) (pos eps)) *)
(* Goal: Rle (IZR Z0) (Rdiv (Rminus b a) (pos eps)) *)
  apply f_equal, IH ; intuition.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by rewrite ssrnat.eqn_leq H H0 in H1.
(* Goal: Rle (IZR Z0) (Rdiv (Rminus b a) (pos eps)) *)
  apply Rdiv_le_0_compat.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by apply Rminus_le_0 in Hab.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by apply eps.
Qed.

Lemma interval_finite_subdiv_between (a b : R) (eps : posreal) (Hab : a <= b) :
  let l := proj1_sig (interval_finite_subdiv a b eps Hab) in
  forall i, (i < size l)%nat -> a <= nth 0 l i <= b.
Proof.
(* Goal: Rle (f (pos_Rl (cons h'0 (cons h0 l0)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h'0) (cons (f h0) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l0)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  case: interval_finite_subdiv => l Hl /= i Hi.
(* Goal: and (Rle a (@nth R (IZR Z0) l i)) (Rle (@nth R (IZR Z0) l i) b) *)
  case: Hl => <- ; case => <- Hl.
(* Goal: forall (n : nat) (_ : forall (h h' : R) (l : Rlist), Rle (f (pos_Rl (cons h' (cons h l)) (S n))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l0 : Rlist) : Rlist := match l0 with | nil => nil | cons h0 t => cons (f h0) (app t) end) l))))) (h h' : R) (l : Rlist), Rle (f (pos_Rl (cons h' (cons h l)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l0 : Rlist) : Rlist := match l0 with | nil => nil | cons h0 t => cons (f h0) (app t) end) l)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  move: (fun i Hi => proj1 (Hl i Hi)) => {Hl} Hl.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite -nth0 (last_nth 0).

  suff : forall n m, (n <= m)%nat -> (m < size l)%nat
    -> nth 0 l n <= nth 0 l m.
(* Goal: and (Rle (Rmin a b) x) (Rle x (Rmax a b)) *)
  move => {Hl} Hl ; split.
(* Goal: Rle (@nth R (IZR Z0) (@Datatypes.cons R x0 l) i) (@nth R (IZR Z0) (@Datatypes.cons R x0 l) (@size R l)) *)
(* Goal: forall (n m : nat) (_ : le n m) (_ : lt m (@size R l)), Rle (@nth R (IZR Z0) l n) (@nth R (IZR Z0) l m) *)
  apply Hl ; by intuition.
(* Goal: Rle (f (pos_Rl (cons h'0 (cons h0 l0)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h'0) (cons (f h0) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l0)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  case: l Hi Hl => /= [ | x0 l] Hi Hl.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by apply lt_n_O in Hi.
(* Goal: Rle (@nth R (IZR Z0) (@Datatypes.cons R x0 l) i) (@nth R (IZR Z0) (@Datatypes.cons R x0 l) (@size R l)) *)
(* Goal: forall (n m : nat) (_ : le n m) (_ : lt m (@size R l)), Rle (@nth R (IZR Z0) l n) (@nth R (IZR Z0) l m) *)
  apply Hl ; by intuition.

(* Goal: Rle (f (pos_Rl (cons h' (cons h l)) (Init.Nat.pred (Rlength (cons h' (cons h l)))))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) *)
  elim: l Hl {i Hi} => [ | x0 l IH] Hl n m Hnm Hm.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by apply lt_n_O in Hm.
(* Goal: Rle (f (pos_Rl (cons h'0 (cons h0 l0)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h'0) (cons (f h0) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l0)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  case: n m Hnm Hm => [ | n] m //= Hnm Hm.
(* Goal: Rle x0 (@nth R (IZR Z0) (@Datatypes.cons R x0 l) m) *)
(* Goal: Rle (@nth R (IZR Z0) l n) (@nth R (IZR Z0) (@Datatypes.cons R x0 l) m) *)
  clear Hnm ; elim: m Hm => {IH} /= [ | m IH] Hm.
(* Goal: Rle (pos_Rl nil i) (Rl_max (IZR Z0) nil) *)
(* Goal: forall (r : R) (r0 : Rlist), Rle (pos_Rl (cons r r0) i) (Rl_max (IZR Z0) (cons r r0)) *)
(* Goal: open_interval (pos_Rl (cons h' (cons h l)) i) (pos_Rl (cons h' (cons h l)) (S i)) x *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  by apply Rle_refl.
(* Goal: Rle x0 (@nth R (IZR Z0) l m) *)
(* Goal: Rle (@nth R (IZR Z0) l n) (@nth R (IZR Z0) (@Datatypes.cons R x0 l) m) *)
  apply Rle_trans with (nth 0 (x0 :: l) m).
(* Goal: Rle x0 (@nth R (IZR Z0) (@Datatypes.cons R x0 l) m) *)
(* Goal: Rle (@nth R (IZR Z0) (@Datatypes.cons R x0 l) m) (@nth R (IZR Z0) l m) *)
(* Goal: Rle (@nth R (IZR Z0) l n) (@nth R (IZR Z0) (@Datatypes.cons R x0 l) m) *)
  apply IH ; intuition.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by apply Rlt_le, Hl.
(* Goal: Rle (f (pos_Rl (cons h'0 (cons h0 l0)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h'0) (cons (f h0) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l0)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  case: m Hnm Hm => /= [ | m] Hnm Hm.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by apply le_Sn_O in Hnm.
(* Goal: Rle (@nth R (IZR Z0) l n) (@nth R (IZR Z0) l m) *)
  apply IH ; try by intuition.
(* Goal: forall (n : nat) (_ : forall (h h' : R) (l : Rlist), Rle (f (pos_Rl (cons h' (cons h l)) (S n))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l0 : Rlist) : Rlist := match l0 with | nil => nil | cons h0 t => cons (f h0) (app t) end) l))))) (h h' : R) (l : Rlist), Rle (f (pos_Rl (cons h' (cons h l)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l0 : Rlist) : Rlist := match l0 with | nil => nil | cons h0 t => cons (f h0) (app t) end) l)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  move => i Hi.
(* Goal: Rlt (@nth R (IZR Z0) l i) (@nth R (IZR Z0) l (S i)) *)
  apply (Hl (S i)).
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by apply lt_n_S.
Qed.

(** Notations *)
Lemma SSR_leq (n m : nat) : is_true (ssrnat.leq n m) <-> (n <= m)%nat.
Proof.
(* Goal: iff (is_true (ssrnat.leq n m)) (le n m) *)
  set H := (@ssrnat.leP n m) ; case: H => H //=.
Qed.
Lemma SSR_minus (n m : nat) : ssrnat.subn n m = (n - m)%nat.
Proof.
(* Goal: Rle (f (pos_Rl (cons h' (cons h l)) (Init.Nat.pred (Rlength (cons h' (cons h l)))))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) *)
  elim: m n => //.
Qed.
(** rcons *)
Lemma rcons_ind {T : Type} (P : seq T -> Type) :
  P [::] -> (forall (s : seq T) (t : T), P s -> P (rcons s t)) -> forall s, P s.
Proof.
(* Goal: forall (_ : P (@Datatypes.nil T)) (_ : forall (s : list T) (t : T) (_ : P s), P (@rcons T s t)) (s : list T), P s *)
  move => H0 Hr s ; move: (refl_equal (size s)).
  move: {1}(size s) => n ; elim: n s => // [| n IH] s Hn ;
  case: s Hn => [| h s] Hn //.
  have: ({s0 : _&{ t0 | h::s = rcons s0 t0}}) ;
    [| case => s0 [t0 H]].
(* Goal: Rle (f (pos_Rl (cons h' (cons h l)) (Init.Nat.pred (Rlength (cons h' (cons h l)))))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) *)
    elim: (s) (h) => {s h Hn IH} [| h s IH] h0.
(* Goal: @sigT (list T) (fun s0 : list T => @sig T (fun t0 : T => @eq (list T) (@Datatypes.cons T h0 (@Datatypes.nil T)) (@rcons T s0 t0))) *)
(* Goal: @sigT (list T) (fun s0 : list T => @sig T (fun t0 : T => @eq (list T) (@Datatypes.cons T h0 (@Datatypes.cons T h s)) (@rcons T s0 t0))) *)
(* Goal: P (@Datatypes.cons T h s) *)
      exists [::] ; by exists h0.
    case: (IH h) => s0 [t0 H] ; exists (h0::s0) ; exists t0 ;
    by rewrite rcons_cons -H.
(* Goal: P (@Datatypes.cons T h s) *)
  rewrite H ; apply Hr, IH, eq_add_S ; by rewrite -(size_rcons s0 t0) -H.
Qed.
Lemma rcons_dec {T : Type} (P : seq T -> Type) :
  (P [::]) -> (forall s t, P (rcons s t)) -> forall s, P s.
Proof.
(* Goal: forall (r : R) (r0 : Rlist) (_ : forall (i : nat) (y : R), Rle (pos_Rl (cons y r0) i) (Rl_max (IZR Z0) (cons y r0))) (i : nat) (y : R), Rle (pos_Rl (cons y (cons r r0)) i) (Rl_max (IZR Z0) (cons y (cons r r0))) *)
(* Goal: open_interval (pos_Rl (cons h' (cons h l)) i) (pos_Rl (cons h' (cons h l)) (S i)) x *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  move => H0 Hr ; case => [| h s] //.
  have: ({s0 : _&{ t0 | h::s = rcons s0 t0}}) ;
    [| case => s0 [t0 H]].
(* Goal: Rle (f (pos_Rl (cons h' (cons h l)) (Init.Nat.pred (Rlength (cons h' (cons h l)))))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) *)
    elim: s h => [| h s IH] h0.
(* Goal: @sigT (list T) (fun s0 : list T => @sig T (fun t0 : T => @eq (list T) (@Datatypes.cons T h0 (@Datatypes.nil T)) (@rcons T s0 t0))) *)
(* Goal: @sigT (list T) (fun s0 : list T => @sig T (fun t0 : T => @eq (list T) (@Datatypes.cons T h0 (@Datatypes.cons T h s)) (@rcons T s0 t0))) *)
(* Goal: P (@Datatypes.cons T h s) *)
      exists [::] ; by exists h0.
    case: (IH h) => s0 [t0 H] ; exists (h0::s0) ; exists t0 ;
    by rewrite rcons_cons -H.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  by rewrite H.
Qed.
Lemma size_rcons_pos {T : Type} (s : seq T) (t : T) : (0 < size (rcons s t))%nat.
Proof.
(* Goal: lt O (@size T (@rcons T s t)) *)
  rewrite size_rcons /= ; apply lt_O_Sn.
Qed.

Lemma foldr_rcons {T T0 : Type} : forall (f : T0 -> T -> T) x0 s t,
  foldr f x0 (rcons s t) = foldr f (f t x0) s.
Proof.
  move => f x0 s ; elim: s x0 => //= t s IH x0 t0 ;
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by rewrite IH.
Qed.
Lemma foldl_rcons {T T0 : Type} : forall (f : T -> T0 -> T) x0 s t,
  foldl f x0 (rcons s t) = f (foldl f x0 s) t.
Proof.
  move => f x0 s ; elim: s x0 => //= t s IH x0 t0 ;
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by rewrite IH.
Qed.

(* head, last, behead and belast *)
Lemma head_rcons {T : Type} (x0 : T) (s : seq T) (t : T) : head x0 (rcons s t) = head t s.
Proof.
(* Goal: Rle (f (pos_Rl (cons h'0 (cons h0 l0)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h'0) (cons (f h0) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l0)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  case: s x0 t => //.
Qed.
Lemma behead_rcons {T : Type} (s : seq T) (t : T) :
  (0 < size s)%nat ->  behead (rcons s t) = rcons (behead s) t.
Proof.
(* Goal: forall _ : lt O (@size T s), @eq (list T) (@behead T (@rcons T s t)) (@rcons T (@behead T s) t) *)
  case: s t => // t Hi ; contradict Hi ; apply lt_n_O.
Qed.
Definition belast {T : Type} (s : seq T) :=
  match s with
    | [::] => [::]
    | h :: s => belast h s
  end.
Lemma behead_rev {T : Type} (s : seq T) : behead (rev s) = rev (belast s).
Proof.
(* Goal: @eq (list T) (@behead T (@rev T s)) (@rev T (@belast T s)) *)
  case: s => // t s ; elim: s t => // t s IHs t0.
(* Goal: lt O (S N) *)
  rewrite rev_cons behead_rcons ?IHs ?size_rev -?rev_cons //= ; by apply lt_0_Sn.
Qed.

Lemma pairmap_rcons {T T0 : Type} (f : T -> T -> T0) (s : seq T) h0 h x0 :
  pairmap f x0 (rcons (rcons s h0) h) = rcons (pairmap f x0 (rcons s h0)) (f h0 h).
Proof.
(* Goal: @eq (list T) (@unzip2 S T (@rcons (prod S T) s h)) (@rcons T (@unzip2 S T s) (@snd S T h)) *)
  elim: s x0 h h0 => [| h1 s IH] x0 h h0 //= ; by rewrite IH.
Qed.
Lemma map_pairmap {T T0 T1 : Type} (f : T0 -> T1) (g : T -> T -> T0) (s : seq T) (x0 : T) :
  map f (pairmap g x0 s) = pairmap (fun x y => f (g x y)) x0 s.
Proof.
(* Goal: Rle (f (pos_Rl (cons h' (cons h l)) (Init.Nat.pred (Rlength (cons h' (cons h l)))))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) *)
  elim: s x0 => [| h s IH] x0 //=.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by rewrite IH.
Qed.
Lemma pairmap_map {T T0 T1 : Type} (f : T0 -> T0 -> T1) (g : T -> T0) (s : seq T) (x0 : T) :
  pairmap f (g x0) (map g s) = pairmap (fun x y => f (g x) (g y)) x0 s.
Proof.
(* Goal: Rle (f (pos_Rl (cons h' (cons h l)) (Init.Nat.pred (Rlength (cons h' (cons h l)))))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) *)
  elim: s x0 => [| h s IH] x0 //=.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by rewrite IH.
Qed.
(** zip and unzip *)
Lemma size_unzip1 {T T0 : Type} (s : seq (T * T0)) : size (unzip1 s) = size s.
Proof.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by elim: s => //= _ s0 ->.
Qed.
Lemma size_unzip2 {T T0 : Type} (s : seq (T * T0)) : size (unzip2 s) = size s.
Proof.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by elim: s => //= _ s0 ->.
Qed.
Lemma zip_cons {S T : Type} hs ht (s : seq S) (t : seq T) :
  zip (hs :: s) (ht :: t) = (hs,ht) :: zip s t.
Proof.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by [].
Qed.
Lemma zip_rcons {S T : Type} (s : seq S) (t : seq T) hs ht : size s = size t ->
  zip (rcons s hs) (rcons t ht) = rcons (zip s t) (hs,ht).
Proof.
(* Goal: @eq (list T) (@unzip2 S T (@behead (prod S T) s)) (@behead T (@unzip2 S T s)) *)
  elim: s t hs ht => [| hs s IHs] ; case => //= ht t hs' ht' Hs.
(* Goal: @eq (list (prod S T)) (@Datatypes.cons (prod S T) (@pair S T hs ht) (@zip S T (@rcons S s hs') (@rcons T t ht'))) (@Datatypes.cons (prod S T) (@pair S T hs ht) (@rcons (prod S T) (@zip S T s t) (@pair S T hs' ht'))) *)
  rewrite IHs => // ; by apply eq_add_S.
Qed.
Lemma unzip1_rcons {S T : Type} (s : seq (S*T)) (h : S*T) :
  unzip1 (rcons s h) = rcons (unzip1 s) (fst h).
Proof.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  elim: s => [ | h0 s IH] //= ; by rewrite IH.
Qed.
Lemma unzip2_rcons {S T : Type} (s : seq (S*T)) (h : S*T) :
  unzip2 (rcons s h) = rcons (unzip2 s) (snd h).
Proof.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  elim: s => [ | h0 s IH] //= ; by rewrite IH.
Qed.
Lemma unzip1_belast {S T : Type} (s : seq (S*T)) :
  unzip1 (belast s) = belast (unzip1 s).
Proof.
(* Goal: @eq (list T) (@unzip2 S T (@behead (prod S T) s)) (@behead T (@unzip2 S T s)) *)
  elim: s => //= h0 ; case => //= h1 s -> //.
Qed.
Lemma unzip2_belast {S T : Type} (s : seq (S*T)) :
  unzip2 (belast s) = belast (unzip2 s).
Proof.
(* Goal: @eq (list T) (@unzip2 S T (@behead (prod S T) s)) (@behead T (@unzip2 S T s)) *)
  elim: s => //= h0 ; case => //= h1 s -> //.
Qed.
Lemma unzip1_behead {S T : Type} (s : seq (S*T)) :
  unzip1 (behead s) = behead (unzip1 s).
Proof.
(* Goal: @eq (list T) (@unzip2 S T (@behead (prod S T) s)) (@behead T (@unzip2 S T s)) *)
  elim: s => //= h0 ; case => //= h1 s -> //.
Qed.
Lemma unzip2_behead {S T : Type} (s : seq (S*T)) :
  unzip2 (behead s) = behead (unzip2 s).
Proof.
(* Goal: @eq (list T) (@unzip2 S T (@behead (prod S T) s)) (@behead T (@unzip2 S T s)) *)
  elim: s => //= h0 ; case => //= h1 s -> //.
Qed.
Lemma unzip1_fst {S T : Type} (s : seq (S*T)) :
  unzip1 s = map (@fst S T) s.
Proof.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by elim: s.
Qed.
Lemma unzip2_snd {S T : Type} (s : seq (S*T)) :
  unzip2 s = map (@snd S T) s.
Proof.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by elim: s.
Qed.
Lemma size_belast' {T : Type} (s : seq T) :
  size (belast s) = Peano.pred (size s).
Proof.
(* Goal: Rle (f (pos_Rl (cons h'0 (cons h0 l0)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h'0) (cons (f h0) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l0)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  case: s => /= [ | x0 s] //.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by rewrite size_belast.
Qed.
Lemma head_map {T1 T2 : Type} (f : T1 -> T2) (s : seq T1) (x : T1) :
  head (f x) (map f s) = f (head x s).
Proof.
(* Goal: @eq T2 (@head T2 (f x) (@map T1 T2 f s)) (f (@head T1 x s)) *)
  by case: s.
Qed.

(** * Operations on the Riemann integral *)

Lemma StepFun_bound {a b : R} (f : StepFun a b) :
  exists s : R, forall x, Rmin a b <= x <= Rmax a b -> f x <= s.
Proof.
  case: f => /= f [lx [ly [Hsort [Hhead [Hlast [Hsize Hval]]]]]];
  rename a into a0 ; rename b into b0 ; set a := Rmin a0 b0 ; set b := Rmax a0 b0 ;
  set Rl_max := fun x0 => fix f l := match l with
    | RList.nil => x0
    | RList.cons h t => Rmax h (f t)
  end ;
  set f_lx := (fix app l := match l with
    | RList.nil => RList.nil
    | RList.cons h t => RList.cons (f h) (app t)
  end) lx ;
  set M_f_lx := Rl_max (f 0) f_lx ;
  set M_ly := Rl_max 0 ly.
(* Goal: open_interval (pos_Rl (cons h' (cons h l)) i) (pos_Rl (cons h' (cons h l)) (S i)) x *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  exists (Rmax M_f_lx M_ly) => x [Hx Hx'].
  rewrite /M_f_lx /f_lx ;
  case: lx Hsort Hhead Hlast Hsize Hval {f_lx M_f_lx}.
(* lx = [::] *)
(* Goal: forall (_ : ordered_Rlist nil) (_ : @eq R (pos_Rl nil O) (Rmin a0 b0)) (_ : @eq R (pos_Rl nil (Init.Nat.pred (Rlength nil))) (Rmax a0 b0)) (_ : @eq nat (Rlength nil) (S (Rlength ly))) (_ : forall (i : nat) (_ : lt i (Init.Nat.pred (Rlength nil))), constant_D_eq f (open_interval (pos_Rl nil i) (pos_Rl nil (S i))) (pos_Rl ly i)), Rle (f x) (Rmax (Rl_max (f (IZR Z0)) nil) M_ly) *)
(* Goal: forall (r : R) (r0 : Rlist) (_ : ordered_Rlist (cons r r0)) (_ : @eq R (pos_Rl (cons r r0) O) (Rmin a0 b0)) (_ : @eq R (pos_Rl (cons r r0) (Init.Nat.pred (Rlength (cons r r0)))) (Rmax a0 b0)) (_ : @eq nat (Rlength (cons r r0)) (S (Rlength ly))) (_ : forall (i : nat) (_ : lt i (Init.Nat.pred (Rlength (cons r r0)))), constant_D_eq f (open_interval (pos_Rl (cons r r0) i) (pos_Rl (cons r r0) (S i))) (pos_Rl ly i)), Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f r) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) r0))) M_ly) *)
  move => _ _ _ H ; contradict H ; apply O_S.
(* Goal: forall (r : R) (r0 : Rlist) (_ : forall (i : nat) (y : R), Rle (pos_Rl (cons y r0) i) (Rl_max (IZR Z0) (cons y r0))) (i : nat) (y : R), Rle (pos_Rl (cons y (cons r r0)) i) (Rl_max (IZR Z0) (cons y (cons r r0))) *)
(* Goal: open_interval (pos_Rl (cons h' (cons h l)) i) (pos_Rl (cons h' (cons h l)) (S i)) x *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  move => h l ; case: l h.
(* lx = [:: h] *)
(* Goal: forall (h : R) (_ : ordered_Rlist (cons h nil)) (_ : @eq R (pos_Rl (cons h nil) O) (Rmin a0 b0)) (_ : @eq R (pos_Rl (cons h nil) (Init.Nat.pred (Rlength (cons h nil)))) (Rmax a0 b0)) (_ : @eq nat (Rlength (cons h nil)) (S (Rlength ly))) (_ : forall (i : nat) (_ : lt i (Init.Nat.pred (Rlength (cons h nil)))), constant_D_eq f (open_interval (pos_Rl (cons h nil) i) (pos_Rl (cons h nil) (S i))) (pos_Rl ly i)), Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h) nil)) M_ly) *)
(* Goal: forall (r : R) (r0 : Rlist) (h : R) (_ : ordered_Rlist (cons h (cons r r0))) (_ : @eq R (pos_Rl (cons h (cons r r0)) O) (Rmin a0 b0)) (_ : @eq R (pos_Rl (cons h (cons r r0)) (Init.Nat.pred (Rlength (cons h (cons r r0))))) (Rmax a0 b0)) (_ : @eq nat (Rlength (cons h (cons r r0))) (S (Rlength ly))) (_ : forall (i : nat) (_ : lt i (Init.Nat.pred (Rlength (cons h (cons r r0))))), constant_D_eq f (open_interval (pos_Rl (cons h (cons r r0)) i) (pos_Rl (cons h (cons r r0)) (S i))) (pos_Rl ly i)), Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h) (cons (f r) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h0 t => cons (f h0) (app t) end) r0)))) M_ly) *)
  move => h _ Ha Hb _ _ ; simpl in Ha, Hb.
(* Goal: @ex nat (fun i : nat => and (lt i (S (Rlength l))) (and (Rle (pos_Rl (cons h' (cons h l)) i) x) (Rlt x (pos_Rl (cons h' (cons h l)) (S i))))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  rewrite /a -Ha in Hx ; rewrite /b -Hb in Hx'.
  rewrite (Rle_antisym _ _ Hx Hx') /= ; apply Rle_trans with (2 := RmaxLess1 _ _) ;
(* Goal: Rle (f (pos_Rl (cons h' (cons h l)) O)) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) *)
(* Goal: forall n : nat, Rle (f (pos_Rl (cons h' (cons h l)) (S n))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  apply RmaxLess1.
(* lx = [:: h,h'::l] *)
(* Goal: forall (n : nat) (_ : forall (h h' : R) (l : Rlist), Rle (f (pos_Rl (cons h' (cons h l)) (S n))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l0 : Rlist) : Rlist := match l0 with | nil => nil | cons h0 t => cons (f h0) (app t) end) l))))) (h h' : R) (l : Rlist), Rle (f (pos_Rl (cons h' (cons h l)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l0 : Rlist) : Rlist := match l0 with | nil => nil | cons h0 t => cons (f h0) (app t) end) l)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  move => h l h' Hsort Hhead Hlast Hsize Hval.
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  apply Rle_lt_or_eq_dec in Hx' ; case: Hx' => Hx'.
  have H : exists i : nat, (i < S (Rlength l))%nat /\
    (pos_Rl (RList.cons h' (RList.cons h l)) i) <= x
    < (pos_Rl (RList.cons h' (RList.cons h l)) (S i)).
(* Goal: @ex nat (fun i : nat => and (lt i (S (Rlength l))) (and (Rle (pos_Rl (cons h' (cons h l)) i) x) (Rlt x (pos_Rl (cons h' (cons h l)) (S i))))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
    rewrite /a -Hhead in Hx ; rewrite /b -Hlast in Hx'.
(* Goal: @ex nat (fun i : nat => and (lt i (S (Rlength l))) (and (Rle (pos_Rl (cons h' (cons h l)) i) x) (Rlt x (pos_Rl (cons h' (cons h l)) (S i))))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
    elim: l h' h Hx Hx' Hsort {Hhead Hlast Hsize Hval} => [| h'' l IH] h' h Hx Hx' Hsort ; simpl in Hx, Hsort.
(* Goal: Rle (f (pos_Rl (cons h'0 (cons h0 l0)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h'0) (cons (f h0) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l0)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
    case: (Rlt_le_dec x h) => H.
(* Goal: @ex nat (fun i : nat => and (lt i (S (Rlength nil))) (and (Rle (pos_Rl (cons h' (cons h nil)) i) x) (Rlt x (pos_Rl (cons h' (cons h nil)) (S i))))) *)
(* Goal: @ex nat (fun i : nat => and (lt i (S (Rlength nil))) (and (Rle (pos_Rl (cons h' (cons h nil)) i) x) (Rlt x (pos_Rl (cons h' (cons h nil)) (S i))))) *)
(* Goal: @ex nat (fun i : nat => and (lt i (S (Rlength (cons h'' l)))) (and (Rle (pos_Rl (cons h' (cons h (cons h'' l))) i) x) (Rlt x (pos_Rl (cons h' (cons h (cons h'' l))) (S i))))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
    exists O ; intuition.
(* Goal: @ex nat (fun i : nat => and (lt i (S (Rlength (cons h'' l)))) (and (Rle (pos_Rl (cons h' (cons h (cons h'' l))) i) x) (Rlt x (pos_Rl (cons h' (cons h (cons h'' l))) (S i))))) *)
(* Goal: @ex nat (fun i : nat => and (lt i (S (Rlength (cons h'' l)))) (and (Rle (pos_Rl (cons h' (cons h (cons h'' l))) i) x) (Rlt x (pos_Rl (cons h' (cons h (cons h'' l))) (S i))))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
    exists O => /= ; intuition.
(* Goal: Rle (f (pos_Rl (cons h'0 (cons h0 l0)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h'0) (cons (f h0) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l0)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
    case: (Rlt_le_dec x h) => H.
(* Goal: @ex nat (fun i : nat => and (lt i (S (Rlength (cons h'' l)))) (and (Rle (pos_Rl (cons h' (cons h (cons h'' l))) i) x) (Rlt x (pos_Rl (cons h' (cons h (cons h'' l))) (S i))))) *)
(* Goal: @ex nat (fun i : nat => and (lt i (S (Rlength (cons h'' l)))) (and (Rle (pos_Rl (cons h' (cons h (cons h'' l))) i) x) (Rlt x (pos_Rl (cons h' (cons h (cons h'' l))) (S i))))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
    exists O => /= ; intuition.
(* Goal: @ex nat (fun i : nat => and (lt i (S (Rlength (cons h'' l)))) (and (Rle (pos_Rl (cons h' (cons h (cons h'' l))) i) x) (Rlt x (pos_Rl (cons h' (cons h (cons h'' l))) (S i))))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
    have H0 : ordered_Rlist (RList.cons h (RList.cons h'' l)).
(* Goal: ordered_Rlist (cons h (cons h'' l)) *)
(* Goal: @ex nat (fun i : nat => and (lt i (S (Rlength (cons h'' l)))) (and (Rle (pos_Rl (cons h' (cons h (cons h'' l))) i) x) (Rlt x (pos_Rl (cons h' (cons h (cons h'' l))) (S i))))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
    move => i Hi ; apply (Hsort (S i)) => /= ; apply lt_n_S, Hi.
(* Goal: Rle (f (pos_Rl (cons h'0 (cons h0 l0)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h'0) (cons (f h0) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l0)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
    case: (IH _ _ H Hx' H0) => {IH} i Hi.
(* Goal: and (Rle (Rmin a b) x) (Rle x (Rmax a b)) *)
    exists (S i) ; split.
(* Goal: lt (S i) (S (Rlength (cons h'' l))) *)
(* Goal: and (Rle (pos_Rl (cons h' (cons h (cons h'' l))) (S i)) x) (Rlt x (pos_Rl (cons h' (cons h (cons h'' l))) (S (S i)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
    simpl ; apply lt_n_S, Hi => /=.
(* Goal: and (Rle (pos_Rl (cons h' (cons h (cons h'' l))) (S i)) x) (Rlt x (pos_Rl (cons h' (cons h (cons h'' l))) (S (S i)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
    apply Hi.
(* Goal: Rle (f (pos_Rl (cons h'0 (cons h0 l0)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h'0) (cons (f h0) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l0)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  case: H => i [Hi [Ht Ht']].
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  apply Rle_lt_or_eq_dec in Ht ; case: Ht => Ht.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite (Hval i Hi x).
(* Goal: Rle (pos_Rl ly i) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
(* Goal: open_interval (pos_Rl (cons h' (cons h l)) i) (pos_Rl (cons h' (cons h l)) (S i)) x *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  apply Rle_trans with (2 := RmaxLess2 _ _).
(* Goal: Rle (pos_Rl ly i) M_ly *)
(* Goal: open_interval (pos_Rl (cons h' (cons h l)) i) (pos_Rl (cons h' (cons h l)) (S i)) x *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  rewrite /M_ly ; case: (ly).
(* Goal: Rle (pos_Rl nil i) (Rl_max (IZR Z0) nil) *)
(* Goal: forall (r : R) (r0 : Rlist), Rle (pos_Rl (cons r r0) i) (Rl_max (IZR Z0) (cons r r0)) *)
(* Goal: open_interval (pos_Rl (cons h' (cons h l)) i) (pos_Rl (cons h' (cons h l)) (S i)) x *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  apply Rle_refl.
(* Goal: forall n : nat, Rle (f (pos_Rl (cons h' (cons h l)) (S n))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  move => y ly' ; elim: ly' (i) y.
(* Goal: forall (r : R) (r0 : Rlist) (_ : forall (i : nat) (y : R), Rle (pos_Rl (cons y r0) i) (Rl_max (IZR Z0) (cons y r0))) (i : nat) (y : R), Rle (pos_Rl (cons y (cons r r0)) i) (Rl_max (IZR Z0) (cons y (cons r r0))) *)
(* Goal: open_interval (pos_Rl (cons h' (cons h l)) i) (pos_Rl (cons h' (cons h l)) (S i)) x *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  move => i0 y ; case: i0 => //=.
(* Goal: Rle (f (pos_Rl (cons h' (cons h l)) O)) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) *)
(* Goal: forall n : nat, Rle (f (pos_Rl (cons h' (cons h l)) (S n))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  apply RmaxLess1.
(* Goal: Rle b (Rmax a b) *)
  move => _; apply RmaxLess2.
(* Goal: forall (r : R) (r0 : Rlist) (_ : forall (i : nat) (y : R), Rle (pos_Rl (cons y r0) i) (Rl_max (IZR Z0) (cons y r0))) (i : nat) (y : R), Rle (pos_Rl (cons y (cons r r0)) i) (Rl_max (IZR Z0) (cons y (cons r r0))) *)
(* Goal: open_interval (pos_Rl (cons h' (cons h l)) i) (pos_Rl (cons h' (cons h l)) (S i)) x *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  move => y ly' IH i0 y' ; case: i0.
(* Goal: Rle (f (pos_Rl (cons h' (cons h l)) O)) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) *)
(* Goal: forall n : nat, Rle (f (pos_Rl (cons h' (cons h l)) (S n))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  apply RmaxLess1.
(* Goal: Rle b (Rmax a b) *)
  move => n ; apply Rle_trans with (1 := IH n y) ; apply RmaxLess2.
(* Goal: open_interval (pos_Rl (cons h' (cons h l)) i) (pos_Rl (cons h' (cons h l)) (S i)) x *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  split => //.
(* Goal: Rle (f (pos_Rl (cons h' (cons h l)) (Init.Nat.pred (Rlength (cons h' (cons h l)))))) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  rewrite -Ht ; apply Rle_trans with (2 := RmaxLess1 _ _).
(* Goal: Rle (f (pos_Rl (cons h'0 (cons h0 l0)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h'0) (cons (f h0) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l0)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  case: (i).
(* Goal: Rle (f (pos_Rl (cons h' (cons h l)) O)) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) *)
(* Goal: forall n : nat, Rle (f (pos_Rl (cons h' (cons h l)) (S n))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  apply RmaxLess1.
(* Goal: forall n : nat, Rle (f (pos_Rl (cons h' (cons h l)) (S n))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  move => n ; elim: n (h) (h') (l).
(* Goal: forall (h h' : R) (l : Rlist), Rle (f (pos_Rl (cons h' (cons h l)) (S O))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l0 : Rlist) : Rlist := match l0 with | nil => nil | cons h0 t => cons (f h0) (app t) end) l)))) *)
(* Goal: forall (n : nat) (_ : forall (h h' : R) (l : Rlist), Rle (f (pos_Rl (cons h' (cons h l)) (S n))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l0 : Rlist) : Rlist := match l0 with | nil => nil | cons h0 t => cons (f h0) (app t) end) l))))) (h h' : R) (l : Rlist), Rle (f (pos_Rl (cons h' (cons h l)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l0 : Rlist) : Rlist := match l0 with | nil => nil | cons h0 t => cons (f h0) (app t) end) l)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  move => h0 h'0 l0 ; apply Rle_trans with (2 := RmaxLess2 _ _), RmaxLess1.
(* Goal: forall (n : nat) (_ : forall (h h' : R) (l : Rlist), Rle (f (pos_Rl (cons h' (cons h l)) (S n))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l0 : Rlist) : Rlist := match l0 with | nil => nil | cons h0 t => cons (f h0) (app t) end) l))))) (h h' : R) (l : Rlist), Rle (f (pos_Rl (cons h' (cons h l)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l0 : Rlist) : Rlist := match l0 with | nil => nil | cons h0 t => cons (f h0) (app t) end) l)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  move => n IH h0 h'0 l0.
(* Goal: Rle (f (pos_Rl (cons h'0 (cons h0 l0)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h'0) (cons (f h0) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l0)))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  case: l0.
(* Goal: Rle (f (pos_Rl (cons h'0 (cons h0 nil)) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h'0) (cons (f h0) nil))) *)
(* Goal: forall (r : R) (r0 : Rlist), Rle (f (pos_Rl (cons h'0 (cons h0 (cons r r0))) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h'0) (cons (f h0) (cons (f r) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) r0))))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  apply Rle_trans with (2 := RmaxLess2 _ _), RmaxLess2.
(* Goal: forall (r : R) (r0 : Rlist), Rle (f (pos_Rl (cons h'0 (cons h0 (cons r r0))) (S (S n)))) (Rl_max (f (IZR Z0)) (cons (f h'0) (cons (f h0) (cons (f r) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) r0))))) *)
(* Goal: Rle (f x) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  move => h''0 l0 ; apply Rle_trans with (1 := IH h''0 h0 l0), RmaxLess2.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite Hx' /b -Hlast.
(* Goal: Rle (f (pos_Rl (cons h' (cons h l)) (Init.Nat.pred (Rlength (cons h' (cons h l)))))) (Rmax (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) M_ly) *)
  apply Rle_trans with (2 := RmaxLess1 _ _).
(* Goal: Rle (f (pos_Rl (cons h' (cons h l)) (Init.Nat.pred (Rlength (cons h' (cons h l)))))) (Rl_max (f (IZR Z0)) (cons (f h') (cons (f h) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l)))) *)
  elim: (l) (h') (h) => [| h''0 l0 IH] h'0 h0.
(* Goal: Rle (f (pos_Rl (cons h'0 (cons h0 nil)) (Init.Nat.pred (Rlength (cons h'0 (cons h0 nil)))))) (Rl_max (f (IZR Z0)) (cons (f h'0) (cons (f h0) nil))) *)
(* Goal: Rle (f (pos_Rl (cons h'0 (cons h0 (cons h''0 l0))) (Init.Nat.pred (Rlength (cons h'0 (cons h0 (cons h''0 l0))))))) (Rl_max (f (IZR Z0)) (cons (f h'0) (cons (f h0) (cons (f h''0) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l0))))) *)
  apply Rle_trans with (2 := RmaxLess2 _ _), RmaxLess1.
(* Goal: Rle (f (pos_Rl (cons h'0 (cons h0 (cons h''0 l0))) (Init.Nat.pred (Rlength (cons h'0 (cons h0 (cons h''0 l0))))))) (Rl_max (f (IZR Z0)) (cons (f h'0) (cons (f h0) (cons (f h''0) ((fix app (l : Rlist) : Rlist := match l with | nil => nil | cons h t => cons (f h) (app t) end) l0))))) *)
  apply Rle_trans with (1 := IH h0 h''0), RmaxLess2.
Qed.

Lemma Riemann_integrable_bound (f : R -> R) (a b : R) :
  Riemann_integrable f a b -> exists s : R, forall x, Rmin a b <= x <= Rmax a b -> f x <= s.
Proof.
  move => pr ;
  case (pr (mkposreal _ Rlt_0_1)) => {pr} phi [psi [pr _]] ;
  case: (StepFun_bound phi) => M_phi H_phi ;
  case: (StepFun_bound psi) => M_psi H_psi ;
  exists (M_psi + M_phi) => x Hx.
(* Goal: Rle (f x) (Rplus M_psi M_phi) *)
  apply Rle_trans with (2 := Rplus_le_compat _ _ _ _ (H_psi _ Hx) (H_phi _ Hx)).
(* Goal: @eq R (Rplus (ln x) (Ropp (ln y))) (Rminus (ln x) (ln y)) *)
(* Goal: Rlt (IZR Z0) y *)
(* Goal: Rlt (IZR Z0) x *)
(* Goal: Rlt (IZR Z0) (Rinv y) *)
  have: (f x = (f x - phi x) + phi x) ; first by ring.
(* Goal: forall _ : @eq R (f x) (Rplus (Rminus (f x) (@fe a b phi x)) (@fe a b phi x)), Rle (f x) (Rplus (@fe a b psi x) (@fe a b phi x)) *)
  move => -> ; apply Rplus_le_compat_r, Rle_trans with (1 := Rle_abs _), pr, Hx.
Qed.

(** Extensionality *)

Lemma Riemann_integrable_ext : forall (f g : R -> R) (a b : R),
  (forall x, Rmin a b <= x <= Rmax a b -> f x = g x)
    -> Riemann_integrable f a b -> Riemann_integrable g a b.
Proof.
(* Goal: forall (f g : forall _ : R, R) (a b : R) (_ : forall (x : R) (_ : and (Rle (Rmin a b) x) (Rle x (Rmax a b))), @eq R (f x) (g x)) (_ : Riemann_integrable f a b), Riemann_integrable g a b *)
  intros f g a b Heq pr_f.
(* Goal: Riemann_integrable g a b *)
  intro eps.
(* Goal: @sigT (StepFun a b) (fun phi : StepFun a b => @sig (StepFun a b) (fun psi : StepFun a b => and (forall (t : R) (_ : and (Rle (Rmin a b) t) (Rle t (Rmax a b))), Rle (Rabs (Rminus (g t) (@fe a b phi t))) (@fe a b psi t)) (Rlt (Rabs (@RiemannInt_SF a b psi)) (pos eps)))) *)
  elim (pr_f eps) ; clear pr_f ; intros phi (psi, pr_f).
(* Goal: @sigT (StepFun a b) (fun phi : StepFun a b => @sig (StepFun a b) (fun psi : StepFun a b => and (forall (t : R) (_ : and (Rle (Rmin a b) t) (Rle t (Rmax a b))), Rle (Rabs (Rminus (g t) (@fe a b phi t))) (@fe a b psi t)) (Rlt (Rabs (@RiemannInt_SF a b psi)) (pos eps)))) *)
  exists phi.
(* Goal: @sig (StepFun a b) (fun psi : StepFun a b => and (forall (t : R) (_ : and (Rle (Rmin a b) t) (Rle t (Rmax a b))), Rle (Rabs (Rminus (g t) (@fe a b phi t))) (@fe a b psi t)) (Rlt (Rabs (@RiemannInt_SF a b psi)) (pos eps))) *)
  exists psi.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  split ; intros.
(* Goal: Rle (Rabs (Rminus (g t) (@fe a b phi t))) (@fe a b psi t) *)
(* Goal: Rlt (Rabs (@RiemannInt_SF a b psi)) (pos eps) *)
  rewrite <- (Heq t H).
(* Goal: Rle (Rabs (Rminus (f t) (@fe a b phi t))) (@fe a b psi t) *)
(* Goal: Rlt (Rabs (@RiemannInt_SF a b psi)) (pos eps) *)
  apply (proj1 pr_f t H).
(* Goal: Rlt (Rabs (@RiemannInt_SF a b psi)) (pos eps) *)
  apply pr_f.
Qed.

Lemma RiemannInt_ext : forall (f g : R -> R) (a b : R)
  (pr_f : Riemann_integrable f a b) (pr_g : Riemann_integrable g a b)
  (Heq : forall x, Rmin a b <= x <= Rmax a b -> f x = g x),
    RiemannInt pr_f = RiemannInt pr_g.
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  intros.
(* Goal: @eq R (@RiemannInt f a b pr_f) (@RiemannInt g a b pr_g) *)
  destruct (Rle_lt_dec a b).
(* Goal: @eq R (@RiemannInt f b a (@RiemannInt_P1 f a b pr_f)) (@RiemannInt g b a (@RiemannInt_P1 g a b pr_g)) *)
  apply RiemannInt_P18.
(* Goal: Rle a b *)
(* Goal: forall (x : R) (_ : and (Rlt a x) (Rlt x b)), @eq R (f x) (g x) *)
(* Goal: @eq R (@RiemannInt f a b pr_f) (@RiemannInt g a b pr_g) *)
  apply r.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  intros.
(* Goal: @eq R (f x) (g x) *)
  apply Heq.
(* Goal: and (Rle (Rmin a b) x) (Rle x (Rmax a b)) *)
  split.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite (Rmin_left _ _ r).
(* Goal: Rle z x *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
  apply Rlt_le ; apply H.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite (Rmax_right _ _ r).
(* Goal: Rle z x *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
  apply Rlt_le ; apply H.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite (RiemannInt_P8 pr_f (RiemannInt_P1 pr_f)).
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite (RiemannInt_P8 pr_g (RiemannInt_P1 pr_g)).
(* Goal: @eq R (Ropp (@RiemannInt f b a (@RiemannInt_P1 f a b pr_f))) (Ropp (@RiemannInt g b a (@RiemannInt_P1 g a b pr_g))) *)
  apply Ropp_eq_compat.
(* Goal: @eq R (@RiemannInt f b a (@RiemannInt_P1 f a b pr_f)) (@RiemannInt g b a (@RiemannInt_P1 g a b pr_g)) *)
  apply RiemannInt_P18.
(* Goal: Rle a b *)
(* Goal: forall (x : R) (_ : and (Rlt a x) (Rlt x b)), @eq R (f x) (g x) *)
(* Goal: @eq R (@RiemannInt f a b pr_f) (@RiemannInt g a b pr_g) *)
  apply Rlt_le ; apply r.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  intros.
(* Goal: @eq R (f x) (g x) *)
  apply Heq.
(* Goal: and (Rle (Rmin a b) x) (Rle x (Rmax a b)) *)
  split.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite (Rmin_right _ _ (Rlt_le _ _ r)).
(* Goal: Rle z x *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
  apply Rlt_le ; apply H.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite (Rmax_left _ _ (Rlt_le _ _ r)).
(* Goal: Rle z x *)
(* Goal: Rge (Rminus x y) (IZR Z0) *)
(* Goal: Rge (Rminus z y) (IZR Z0) *)
  apply Rlt_le ; apply H.
Qed.

(** Constant function *)

Lemma Riemann_integrable_const : forall (c a b : R),
  Riemann_integrable (fun x => c) a b.
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  intros.
(* Goal: Riemann_integrable (fun _ : R => c) a b *)
  apply RiemannInt_P14.
Qed.

Lemma RiemannInt_const : forall (c a b : R) (pr : Riemann_integrable (fun x => c) a b),
  RiemannInt pr = c * (b-a).
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  intros.
(* Goal: @eq R (@RiemannInt (fun _ : R => c) a b pr) (Rmult c (Rminus b a)) *)
  apply RiemannInt_P15.
Qed.

(** Addition *)

Lemma Riemann_integrable_plus : forall (f g : R -> R) (a b : R),
  Riemann_integrable f a b -> Riemann_integrable g a b ->
    Riemann_integrable (fun x => f x + g x) a b.
Proof.
(* Goal: forall (f g : forall _ : R, R) (a b : R) (_ : Riemann_integrable f a b) (_ : Riemann_integrable g a b), Riemann_integrable (fun x : R => Rminus (f x) (g x)) a b *)
  intros f g a b pr_f pr_g.
(* Goal: Riemann_integrable (fun x : R => Rplus (f x) (g x)) a b *)
  apply (Riemann_integrable_ext (fun x => f x + 1 * g x)).
(* Goal: @eq R (Rplus (ln x) (Ropp (ln y))) (Rminus (ln x) (ln y)) *)
(* Goal: Rlt (IZR Z0) y *)
(* Goal: Rlt (IZR Z0) x *)
(* Goal: Rlt (IZR Z0) (Rinv y) *)
  intros ; ring.
(* Goal: Riemann_integrable (fun x : R => Rplus (f x) (Rmult (IZR (Zpos xH)) (g x))) a b *)
  apply (RiemannInt_P10 1 pr_f pr_g).
Qed.

Lemma RiemannInt_plus : forall (f g : R -> R) (a b : R)
  (pr_f : Riemann_integrable f a b) (pr_g : Riemann_integrable g a b)
  (pr : Riemann_integrable (fun x => f x + g x) a b),
  RiemannInt pr = RiemannInt pr_f + RiemannInt pr_g.
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  intros.
(* Goal: @eq R (@RiemannInt (fun x : R => Rminus (f x) (g x)) a b pr) (Rminus (@RiemannInt f a b pr_f) (@RiemannInt g a b pr_g)) *)
  rewrite <- (Rmult_1_l (RiemannInt pr_g)).
(* Goal: @eq R (@RiemannInt (fun x : R => Rplus (f x) (g x)) a b pr) (Rplus (@RiemannInt f a b pr_f) (Rmult (IZR (Zpos xH)) (@RiemannInt g a b pr_g))) *)
  rewrite <- (RiemannInt_P13 pr_f pr_g (RiemannInt_P10 1 pr_f pr_g)).
(* Goal: @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (@RiemannInt (fun x : R => Rplus (IZR Z0) (Rmult c (f x))) a b (@RiemannInt_P10 (fun _ : R => IZR Z0) f a b c (Riemann_integrable_const (IZR Z0) a b) pr_f)) *)
  apply RiemannInt_ext.
(* Goal: @eq R (Rplus (ln x) (Ropp (ln y))) (Rminus (ln x) (ln y)) *)
(* Goal: Rlt (IZR Z0) y *)
(* Goal: Rlt (IZR Z0) x *)
(* Goal: Rlt (IZR Z0) (Rinv y) *)
  intros ; ring.
Qed.

(** Subtraction *)

Lemma Riemann_integrable_minus : forall (f g : R -> R) (a b : R),
  Riemann_integrable f a b -> Riemann_integrable g a b ->
    Riemann_integrable (fun x => f x - g x) a b.
Proof.
(* Goal: forall (f g : forall _ : R, R) (a b : R) (_ : Riemann_integrable f a b) (_ : Riemann_integrable g a b), Riemann_integrable (fun x : R => Rminus (f x) (g x)) a b *)
  intros f g a b pr_f pr_g.
(* Goal: Riemann_integrable (fun x : R => Rminus (f x) (g x)) a b *)
  apply (Riemann_integrable_ext (fun x => f x + (-1) * g x)).
(* Goal: @eq R (Rplus (ln x) (Ropp (ln y))) (Rminus (ln x) (ln y)) *)
(* Goal: Rlt (IZR Z0) y *)
(* Goal: Rlt (IZR Z0) x *)
(* Goal: Rlt (IZR Z0) (Rinv y) *)
  intros ; ring.
(* Goal: Riemann_integrable (fun x : R => Rplus (f x) (Rmult (IZR (Zneg xH)) (g x))) a b *)
  apply (RiemannInt_P10 (-1) pr_f pr_g).
Qed.

Lemma RiemannInt_minus : forall (f g : R -> R) (a b : R)
  (pr_f : Riemann_integrable f a b) (pr_g : Riemann_integrable g a b)
  (pr : Riemann_integrable (fun x => f x - g x) a b),
  RiemannInt pr = RiemannInt pr_f - RiemannInt pr_g.
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  intros.
(* Goal: @eq R (@RiemannInt (fun x : R => Rminus (f x) (g x)) a b pr) (Rminus (@RiemannInt f a b pr_f) (@RiemannInt g a b pr_g)) *)
  rewrite <- (Rmult_1_l (RiemannInt pr_g)).
(* Goal: @eq R (@RiemannInt (fun x : R => Rminus (f x) (g x)) a b pr) (Rminus (@RiemannInt f a b pr_f) (Rmult (IZR (Zpos xH)) (@RiemannInt g a b pr_g))) *)
  unfold Rminus. rewrite <- Ropp_mult_distr_l_reverse.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite -(RiemannInt_P13 pr_f pr_g (RiemannInt_P10 (-1) pr_f pr_g)).
(* Goal: @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (@RiemannInt (fun x : R => Rplus (IZR Z0) (Rmult c (f x))) a b (@RiemannInt_P10 (fun _ : R => IZR Z0) f a b c (Riemann_integrable_const (IZR Z0) a b) pr_f)) *)
  apply RiemannInt_ext.
(* Goal: @eq R (Rplus (ln x) (Ropp (ln y))) (Rminus (ln x) (ln y)) *)
(* Goal: Rlt (IZR Z0) y *)
(* Goal: Rlt (IZR Z0) x *)
(* Goal: Rlt (IZR Z0) (Rinv y) *)
  intros ; ring.
Qed.

(** Opposite *)

Lemma Riemann_integrable_opp : forall (f : R -> R) (a b : R),
  Riemann_integrable f a b ->
    Riemann_integrable (fun x => - f x) a b.
Proof.
(* Goal: forall (f : forall _ : R, R) (a b : R) (_ : Riemann_integrable f a b), Riemann_integrable (fun x : R => Ropp (f x)) a b *)
  intros f a b pr_f.
(* Goal: Riemann_integrable (fun x : R => Ropp (f x)) a b *)
  apply (Riemann_integrable_ext (fun x => 0 + (-1) * f x)).
(* Goal: @eq R (Rplus (ln x) (Ropp (ln y))) (Rminus (ln x) (ln y)) *)
(* Goal: Rlt (IZR Z0) y *)
(* Goal: Rlt (IZR Z0) x *)
(* Goal: Rlt (IZR Z0) (Rinv y) *)
  intros ; ring.
(* Goal: Riemann_integrable (fun x : R => Rplus (IZR Z0) (Rmult (IZR (Zneg xH)) (f x))) a b *)
  apply (RiemannInt_P10 (-1) (Riemann_integrable_const _ _ _) pr_f).
Qed.

Lemma RiemannInt_opp : forall (f : R -> R) (a b : R)
  (pr_f : Riemann_integrable f a b)
  (pr : Riemann_integrable (fun x => - f x) a b),
  RiemannInt pr = - RiemannInt pr_f.
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  intros.
(* Goal: @eq R (@RiemannInt (fun x : R => Ropp (f x)) a b pr) (Ropp (@RiemannInt f a b pr_f)) *)
  rewrite <- (Rmult_1_l (RiemannInt pr_f)).
(* Goal: @eq R (@RiemannInt (fun x : R => Ropp (f x)) a b pr) (Ropp (Rmult (IZR (Zpos xH)) (@RiemannInt f a b pr_f))) *)
  rewrite <- Ropp_mult_distr_l_reverse.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite -(Rplus_0_l (-1 * RiemannInt pr_f)).
(* Goal: @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rplus (IZR Z0) (Rmult c (@RiemannInt f a b pr_f))) *)
  assert (0 = RiemannInt (Riemann_integrable_const 0 a b)).
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
    rewrite RiemannInt_const.
(* Goal: @eq R (Rplus (ln x) (Ropp (ln y))) (Rminus (ln x) (ln y)) *)
(* Goal: Rlt (IZR Z0) y *)
(* Goal: Rlt (IZR Z0) x *)
(* Goal: Rlt (IZR Z0) (Rinv y) *)
    ring.
(* Goal: @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rplus (IZR Z0) (Rmult c (@RiemannInt f a b pr_f))) *)
    rewrite H ; clear H.
(* Goal: @eq R (@RiemannInt (fun x : R => Ropp (f x)) a b pr) (Rplus (@RiemannInt (fun _ : R => IZR Z0) a b (Riemann_integrable_const (IZR Z0) a b)) (Rmult (IZR (Zneg xH)) (@RiemannInt f a b pr_f))) *)
  rewrite <- (RiemannInt_P13 (Riemann_integrable_const 0 _ _) pr_f (RiemannInt_P10 (-1) (Riemann_integrable_const 0 a b) pr_f)).
(* Goal: @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (@RiemannInt (fun x : R => Rplus (IZR Z0) (Rmult c (f x))) a b (@RiemannInt_P10 (fun _ : R => IZR Z0) f a b c (Riemann_integrable_const (IZR Z0) a b) pr_f)) *)
  apply RiemannInt_ext.
(* Goal: @eq R (Rplus (ln x) (Ropp (ln y))) (Rminus (ln x) (ln y)) *)
(* Goal: Rlt (IZR Z0) y *)
(* Goal: Rlt (IZR Z0) x *)
(* Goal: Rlt (IZR Z0) (Rinv y) *)
  intros ; ring.
Qed.

(** Multiplication by a scalar *)

Lemma Riemann_integrable_scal : forall (f : R -> R) (a b c : R),
  Riemann_integrable f a b ->
    Riemann_integrable (fun x => c * f x) a b.
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (_ : Riemann_integrable f a b), Riemann_integrable (fun x : R => Rmult c (f x)) a b *)
  intros f a b c pr_f.
(* Goal: Riemann_integrable (fun x : R => Rmult c (f x)) a b *)
  apply (Riemann_integrable_ext (fun x => 0 + c * f x)).
(* Goal: @eq R (Rplus (ln x) (Ropp (ln y))) (Rminus (ln x) (ln y)) *)
(* Goal: Rlt (IZR Z0) y *)
(* Goal: Rlt (IZR Z0) x *)
(* Goal: Rlt (IZR Z0) (Rinv y) *)
  intros ; ring.
(* Goal: Riemann_integrable (fun x : R => Rplus (IZR Z0) (Rmult c (f x))) a b *)
  apply (RiemannInt_P10 (c) (Riemann_integrable_const _ _ _) pr_f).
Qed.

Lemma RiemannInt_scal : forall (f : R -> R) (a b c : R)
  (pr_f : Riemann_integrable f a b)
  (pr : Riemann_integrable (fun x => c * f x) a b),
  RiemannInt pr = c * RiemannInt pr_f.
Proof.
(* Goal: forall (f : forall _ : R, R) (a b c : R) (pr_f : Riemann_integrable f a b) (pr : Riemann_integrable (fun x : R => Rmult c (f x)) a b), @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  intros.
(* Goal: @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rmult c (@RiemannInt f a b pr_f)) *)
  rewrite <- (Rplus_0_l (c * RiemannInt pr_f)).
(* Goal: @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rplus (IZR Z0) (Rmult c (@RiemannInt f a b pr_f))) *)
  assert (0 = RiemannInt (Riemann_integrable_const 0 a b)).
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
    rewrite RiemannInt_const.
(* Goal: @eq R (Rplus (ln x) (Ropp (ln y))) (Rminus (ln x) (ln y)) *)
(* Goal: Rlt (IZR Z0) y *)
(* Goal: Rlt (IZR Z0) x *)
(* Goal: Rlt (IZR Z0) (Rinv y) *)
    ring.
(* Goal: @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rplus (IZR Z0) (Rmult c (@RiemannInt f a b pr_f))) *)
    rewrite H ; clear H.
(* Goal: @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (Rplus (@RiemannInt (fun _ : R => IZR Z0) a b (Riemann_integrable_const (IZR Z0) a b)) (Rmult c (@RiemannInt f a b pr_f))) *)
  rewrite <- (RiemannInt_P13 (Riemann_integrable_const 0 _ _) pr_f (RiemannInt_P10 (c) (Riemann_integrable_const 0 a b) pr_f)).
(* Goal: @eq R (@RiemannInt (fun x : R => Rmult c (f x)) a b pr) (@RiemannInt (fun x : R => Rplus (IZR Z0) (Rmult c (f x))) a b (@RiemannInt_P10 (fun _ : R => IZR Z0) f a b c (Riemann_integrable_const (IZR Z0) a b) pr_f)) *)
  apply RiemannInt_ext.
(* Goal: @eq R (Rplus (ln x) (Ropp (ln y))) (Rminus (ln x) (ln y)) *)
(* Goal: Rlt (IZR Z0) y *)
(* Goal: Rlt (IZR Z0) x *)
(* Goal: Rlt (IZR Z0) (Rinv y) *)
  intros ; ring.
Qed.

(** * Natural logarithm *)

Lemma ln_pow x n : 0 < x -> ln (x^n) = INR n * ln x.
  intro Hx ;
  induction n as [ | n IH].
(* Goal: @eq R (Rplus (ln x) (Ropp (ln y))) (Rminus (ln x) (ln y)) *)
(* Goal: Rlt (IZR Z0) y *)
(* Goal: Rlt (IZR Z0) x *)
(* Goal: Rlt (IZR Z0) (Rinv y) *)
  rewrite pow_O ln_1 ; simpl ; ring.
(* Goal: @eq R (ln (pow x (S n))) (Rmult (INR (S n)) (ln x)) *)
  rewrite S_INR ; simpl ; rewrite ln_mult ; try intuition.
(* Goal: @eq R (Rplus (ln x) (Ropp (ln y))) (Rminus (ln x) (ln y)) *)
(* Goal: Rlt (IZR Z0) y *)
(* Goal: Rlt (IZR Z0) x *)
(* Goal: Rlt (IZR Z0) (Rinv y) *)
  rewrite IH ; ring.
Qed.

Lemma ln_le x y : 0 < x -> x <= y -> ln x <= ln y.
Proof.
(* Goal: forall (_ : Rlt (IZR Z0) x) (_ : Rle x y), Rle (ln x) (ln y) *)
  intros Hx Hxy ; destruct Hxy.
(* Goal: Rle (ln x) (ln y) *)
(* Goal: Rle (ln x) (ln y) *)
  left ; apply ln_increasing.
(* Goal: Rlt (IZR Z0) x *)
(* Goal: Rlt (IZR Z0) (Rinv y) *)
  exact Hx.
(* Goal: Rlt x y *)
(* Goal: Rle (ln x) (ln y) *)
  exact H.
(* Goal: Rle (ln x) (ln y) *)
  rewrite H ; exact (Rle_refl _).
Qed.

Lemma ln_div x y : 0 < x -> 0 < y -> ln (x / y) = ln x - ln y.
Proof.
(* Goal: @eq R (Rabs (Rdiv a b)) (Rdiv (Rabs a) (Rabs b)) *)
  intros Hx Hy ; unfold Rdiv.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite ln_mult.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
  rewrite ln_Rinv.
(* Goal: @eq R (Rplus (ln x) (Ropp (ln y))) (Rminus (ln x) (ln y)) *)
(* Goal: Rlt (IZR Z0) y *)
(* Goal: Rlt (IZR Z0) x *)
(* Goal: Rlt (IZR Z0) (Rinv y) *)
  ring.
(* Goal: Rlt (IZR Z0) y *)
(* Goal: Rlt (IZR Z0) x *)
(* Goal: Rlt (IZR Z0) (Rinv y) *)
  exact Hy.
(* Goal: Rlt (IZR Z0) x *)
(* Goal: Rlt (IZR Z0) (Rinv y) *)
  exact Hx.
(* Goal: Rlt (IZR Z0) y *)
(* Goal: Rlt (IZR Z0) x *)
(* Goal: Rlt (IZR Z0) (Rinv y) *)
  apply Rinv_0_lt_compat ; exact Hy.
Qed.

(** * Other *)

Lemma derivable_pt_lim_atan :
  forall x, derivable_pt_lim atan x (/(1 + x^2)).
Proof.
(* Goal: forall x : R, derivable_pt_lim atan x (Rinv (Rplus (IZR (Zpos xH)) (pow x (S (S O))))) *)
intros x.
(* Goal: derivable_pt_lim atan x (Rinv (Rplus (IZR (Zpos xH)) (pow x (S (S O))))) *)
apply derive_pt_eq_1 with (derivable_pt_atan x).
(* Goal: @eq R (Rplus (ln x) (Ropp (ln y))) (Rminus (ln x) (ln y)) *)
(* Goal: Rlt (IZR Z0) y *)
(* Goal: Rlt (IZR Z0) x *)
(* Goal: Rlt (IZR Z0) (Rinv y) *)
replace (x ^ 2) with (x * x) by ring.
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x))) *)
rewrite -(Rmult_1_l (Rinv _)).
(* Goal: @eq R (derive_pt atan x (derivable_pt_atan x)) (Rmult (IZR (Zpos xH)) (Rinv (Rplus (IZR (Zpos xH)) (Rmult x x)))) *)
apply derive_pt_atan.
Qed.
