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

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Copyright (C) 2011-2015 Sylvie Boldo
#<br />#
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Copyright (C) 2011-2015 Catherine Lelay
#<br />#
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
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.
*)

Require Import Reals Psatz.
Require Import mathcomp.ssreflect.ssreflect mathcomp.ssreflect.seq mathcomp.ssreflect.ssrbool.
Require Import Rcomplements Rbar Lub.
Require Import Hierarchy.

(** This file describes many properties about sequences of real
numbers. Several formalizations are provided. They are mainly used for
defining pointed subvivision in order to define Riemann sums. *)


Open Scope R_scope.

(** * Complements abouts lists *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
(** seq R <-> Rlist *)
Fixpoint seq2Rlist (s : seq R) :=
  match s with
    | [::] => RList.nil
    | h::t => RList.cons h (seq2Rlist t)
  end.
Fixpoint Rlist2seq (s : Rlist) : seq R :=
  match s with
    | RList.nil => [::]
    | RList.cons h t => h::(Rlist2seq t)
  end.

Lemma seq2Rlist_bij (s : Rlist) :
  seq2Rlist (Rlist2seq s) = s.
Proof.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by elim: s => //= h s ->.
Qed.
Lemma Rlist2seq_bij (s : seq R) :
  Rlist2seq (seq2Rlist s) = s.
Proof.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by elim: s => //= h s ->.
Qed.

Lemma size_compat (s : seq R) :
  Rlength (seq2Rlist s) = size s.
Proof.
(* Goal: @eq nat (Rlength (seq2Rlist s)) (@size R s) *)
  elim: s => // t s IHs /= ; by rewrite IHs.
Qed.

Lemma nth_compat (s : seq R) (n : nat) :
  pos_Rl (seq2Rlist s) n = nth 0 s n.
Proof.
  elim: s n => [n|t s IHs n] /= ;
  case: n => //=.
Qed.

(** Various properties *)

Lemma rev_rev {T} (l : seq T) : rev (rev l) = l.
Proof.
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) x) (Rlt x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i)) *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  elim: l => /= [ | h l IH].
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by [].
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by rewrite rev_cons rev_rcons IH.
Qed.
Lemma head_rev {T} (x0 : T) (l : seq T) :
  head x0 (rev l) = last x0 l.
Proof.
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) x) (Rlt x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i)) *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  elim: l x0 => /= [ | x1 l IH] x0.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by [].
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by rewrite rev_cons head_rcons.
Qed.
Lemma last_rev {T} (x0 : T) (l : seq T) :
  last x0 (rev l) = head x0 l.
Proof.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by rewrite -head_rev rev_rev.
Qed.

Lemma last_unzip1 {S T} x0 y0 (s : seq (S * T)) :
  last x0 (unzip1 s) = fst (last (x0,y0) s).
Proof.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: s => [ | h s] //= .
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) x) (Rlt x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i)) *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  elim: s h => [ | h0 s IH] h //=.
Qed.

(** sorted *)

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Fixpoint sorted {T : Type} (Ord : T -> T -> Prop) (s : seq T) :=
  match s with
    | [::] | [:: _] => True
    | h0 :: (h1 :: t1) as t0 => Ord h0 h1 /\ sorted Ord t0
  end.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma sorted_nth {T : Type} (Ord : T -> T -> Prop) (s : seq T) :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  sorted Ord s <-> (forall i : nat,
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
    (i < Peano.pred (size s))%nat -> forall x0 : T, Ord (nth x0 s i) (nth x0 s (S i))).
Proof.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: s.
(* Goal: iff (@sorted R Rle (@Datatypes.cons R h (@Datatypes.nil R))) (ordered_Rlist (seq2Rlist (@Datatypes.cons R h (@Datatypes.nil R)))) *)
(* Goal: iff (@sorted R Rle (@Datatypes.cons R h' (@Datatypes.cons R h s))) (ordered_Rlist (seq2Rlist (@Datatypes.cons R h' (@Datatypes.cons R h s)))) *)
  split => // _ i Hi ; contradict Hi ; apply lt_n_O.
(* Goal: forall (a : T) (l : list T), iff (@sorted T Ord (@Datatypes.cons T a l)) (forall (i : nat) (_ : lt i (Init.Nat.pred (@size T (@Datatypes.cons T a l)))) (x0 : T), Ord (@nth T x0 (@Datatypes.cons T a l) i) (@nth T x0 (@Datatypes.cons T a l) (S i))) *)
  move => t s ; elim: s t => [ t | t s IHs t0] ; split => // H.
(* Goal: forall (i : nat) (_ : lt i (Init.Nat.pred (@size T (@Datatypes.cons T t (@Datatypes.nil T))))) (x0 : T), Ord (@nth T x0 (@Datatypes.cons T t (@Datatypes.nil T)) i) (@nth T x0 (@Datatypes.cons T t (@Datatypes.nil T)) (S i)) *)
(* Goal: forall (i : nat) (_ : lt i (Init.Nat.pred (@size T (@Datatypes.cons T t0 (@Datatypes.cons T t s))))) (x0 : T), Ord (@nth T x0 (@Datatypes.cons T t0 (@Datatypes.cons T t s)) i) (@nth T x0 (@Datatypes.cons T t0 (@Datatypes.cons T t s)) (S i)) *)
(* Goal: @sorted T Ord (@Datatypes.cons T t0 (@Datatypes.cons T t s)) *)
  move => i Hi ; contradict Hi ; apply lt_n_O.
(* Goal: forall (i : nat) (_ : lt i (Init.Nat.pred (@size T (@Datatypes.cons T t0 (@Datatypes.cons T t s))))) (x0 : T), Ord (@nth T x0 (@Datatypes.cons T t0 (@Datatypes.cons T t s)) i) (@nth T x0 (@Datatypes.cons T t0 (@Datatypes.cons T t s)) (S i)) *)
(* Goal: @sorted T Ord (@Datatypes.cons T t0 (@Datatypes.cons T t s)) *)
  case => [| i] Hi x0 ; simpl in Hi.
(* Goal: @eq R (@nth R (IZR Z0) (@behead R unif_part0) n0) (@nth R (Rplus a (Rdiv (Rmult (IZR Z0) (Rminus b a)) (Rplus (INR n) (IZR (Zpos xH))))) (@behead R unif_part0) n0) *)
(* Goal: @eq (list R) (@Datatypes.cons R (@head R (IZR Z0) unif_part0) (@behead R unif_part0)) unif_part0 *)
(* Goal: lt O (@size R (unif_part a b n)) *)
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@Riemann_sum V g l) (@Riemann_sum V f l) *)
  apply H.
  case: (IHs t) => {IHs} IHs _ ;
  apply (IHs (proj2 H) i (lt_S_n _ _ Hi) x0).
(* Goal: and (Rle u u) (Rle u v) *)
(* Goal: Rle (Rmin a b) (Rmax a b) *)
(* Goal: P (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n)) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  split.
(* Goal: Ord t0 t *)
(* Goal: @sorted T Ord (@Datatypes.cons T t s) *)
  apply (H O (lt_0_Sn _) t).
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: (IHs t) => {IHs} _ IHs.
(* Goal: @sorted T Ord (@Datatypes.cons T t s) *)
  apply: IHs => i Hi x0 ; apply: (H (S i)) ; simpl ; apply lt_n_S, Hi.
Qed.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma sorted_cat  {T : Type} (Ord : T -> T -> Prop) (s1 s2 : seq T) x0 :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  sorted Ord s1 -> sorted Ord s2 -> Ord (last x0 s1)  (head x0 s2)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  -> sorted Ord (s1 ++ s2).
Proof.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  move/sorted_nth => H1.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  move/sorted_nth => H2 H0.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  apply sorted_nth => i Hi => x1.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite ?nth_cat.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite ?SSR_minus.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: (le_dec (S i) (size s1)) => Hi0.
  move: (proj2 (SSR_leq _ _) Hi0) ;
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: (ssrnat.leq (S i) (size s1)) => // _.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: (le_dec (S (S i)) (size s1)) => Hi1.
  move: (proj2 (SSR_leq _ _) Hi1) ;
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: (ssrnat.leq (S (S i)) (size s1)) => // _.
(* Goal: Ord (@nth T x1 s1 i) (@nth T x1 s1 (S i)) *)
(* Goal: Ord (@nth T x1 s1 i) (if ssrnat.leq (S (S i)) (@size T s1) then @nth T x1 s1 (S i) else @nth T x1 s2 (Init.Nat.sub (S i) (@size T s1))) *)
(* Goal: Ord (if ssrnat.leq (S i) (@size T s1) then @nth T x1 s1 i else @nth T x1 s2 (Init.Nat.sub i (@size T s1))) (if ssrnat.leq (S (S i)) (@size T s1) then @nth T x1 s1 (S i) else @nth T x1 s2 (Init.Nat.sub (S i) (@size T s1))) *)
  apply H1 ; intuition.
(* Goal: adapted_couple (@SF_fun R s z0) (@head R (IZR Z0) (@SF_lx R s)) (@last R (IZR Z0) (@SF_lx R s)) (seq2Rlist (@SF_lx R s)) (seq2Rlist (@SF_ly R s)) *)
  have : ~ (ssrnat.leq (S (S i)) (size s1)).
(* Goal: not (is_true (ssrnat.leq (S (S i)) (@size T s1))) *)
(* Goal: forall _ : not (is_true (ssrnat.leq (S (S i)) (@size T s1))), Ord (@nth T x1 s1 i) (if ssrnat.leq (S (S i)) (@size T s1) then @nth T x1 s1 (S i) else @nth T x1 s2 (Init.Nat.sub (S i) (@size T s1))) *)
(* Goal: Ord (if ssrnat.leq (S i) (@size T s1) then @nth T x1 s1 i else @nth T x1 s2 (Init.Nat.sub i (@size T s1))) (if ssrnat.leq (S (S i)) (@size T s1) then @nth T x1 s1 (S i) else @nth T x1 s2 (Init.Nat.sub (S i) (@size T s1))) *)
  contradict Hi1 ; by apply SSR_leq.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: (ssrnat.leq (S (S i)) (size s1)) => // _.
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (unif_part a b n) n) x) (Rle x (@nth R (IZR Z0) (unif_part a b n) (S n)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  suff Hi' : i = Peano.pred (size s1).
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite Hi' nth_last.
(* Goal: Ord (@last T x1 s1) (@nth T x1 s2 (Init.Nat.sub (S (Init.Nat.pred (@size T s1))) (@size T s1))) *)
(* Goal: @eq nat i (Init.Nat.pred (@size T s1)) *)
(* Goal: Ord (if ssrnat.leq (S i) (@size T s1) then @nth T x1 s1 i else @nth T x1 s2 (Init.Nat.sub i (@size T s1))) (if ssrnat.leq (S (S i)) (@size T s1) then @nth T x1 s1 (S i) else @nth T x1 s2 (Init.Nat.sub (S i) (@size T s1))) *)
  replace (S (Peano.pred (size s1)) - size s1)%nat with O.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite nth0.
(* Goal: le (@size T s1) (S i) *)
(* Goal: le i (Init.Nat.pred (@size T s1)) *)
(* Goal: Ord (if ssrnat.leq (S i) (@size T s1) then @nth T x1 s1 i else @nth T x1 s2 (Init.Nat.sub i (@size T s1))) (if ssrnat.leq (S (S i)) (@size T s1) then @nth T x1 s1 (S i) else @nth T x1 s2 (Init.Nat.sub (S i) (@size T s1))) *)
  apply not_le in Hi1.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: (s1) H0 Hi Hi' Hi0 Hi1 => [ | x2 s1'] //= H0 Hi Hi' Hi0 Hi1.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply le_Sn_O in Hi0.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: (s2) H0 Hi0 Hi => [ | x3 s2'] //= H0 Hi0 Hi.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite cats0 /= in Hi.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite Hi' in Hi.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply lt_irrefl in Hi.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: (s1) Hi0 => //= [ | x2 s0] Hi0.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply le_Sn_O in Hi0.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by rewrite minus_diag.
(* Goal: @eq nat i (Init.Nat.pred (@size T s1)) *)
(* Goal: Ord (if ssrnat.leq (S i) (@size T s1) then @nth T x1 s1 i else @nth T x1 s2 (Init.Nat.sub i (@size T s1))) (if ssrnat.leq (S (S i)) (@size T s1) then @nth T x1 s1 (S i) else @nth T x1 s2 (Init.Nat.sub (S i) (@size T s1))) *)
  apply sym_eq, le_antisym.
(* Goal: le (Init.Nat.pred (@size T s1)) i *)
(* Goal: le i (Init.Nat.pred (@size T s1)) *)
(* Goal: Ord (if ssrnat.leq (S i) (@size T s1) then @nth T x1 s1 i else @nth T x1 s2 (Init.Nat.sub i (@size T s1))) (if ssrnat.leq (S (S i)) (@size T s1) then @nth T x1 s1 (S i) else @nth T x1 s2 (Init.Nat.sub (S i) (@size T s1))) *)
  apply MyNat.le_pred_le_succ.
(* Goal: le (@size T s1) (S i) *)
(* Goal: le i (Init.Nat.pred (@size T s1)) *)
(* Goal: Ord (if ssrnat.leq (S i) (@size T s1) then @nth T x1 s1 i else @nth T x1 s2 (Init.Nat.sub i (@size T s1))) (if ssrnat.leq (S (S i)) (@size T s1) then @nth T x1 s1 (S i) else @nth T x1 s2 (Init.Nat.sub (S i) (@size T s1))) *)
  apply not_le in Hi1.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply lt_n_Sm_le.
(* Goal: le i (Init.Nat.pred (@size T s1)) *)
(* Goal: Ord (if ssrnat.leq (S i) (@size T s1) then @nth T x1 s1 i else @nth T x1 s2 (Init.Nat.sub i (@size T s1))) (if ssrnat.leq (S (S i)) (@size T s1) then @nth T x1 s1 (S i) else @nth T x1 s2 (Init.Nat.sub (S i) (@size T s1))) *)
  replace i with (Peano.pred (S i)) by auto.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply le_pred.
(* Goal: adapted_couple (@SF_fun R s z0) (@head R (IZR Z0) (@SF_lx R s)) (@last R (IZR Z0) (@SF_lx R s)) (seq2Rlist (@SF_lx R s)) (seq2Rlist (@SF_ly R s)) *)
  have : ~ (ssrnat.leq (S i) (size s1)).
(* Goal: not (is_true (ssrnat.leq (S i) (@size T s1))) *)
(* Goal: forall _ : not (is_true (ssrnat.leq (S i) (@size T s1))), Ord (if ssrnat.leq (S i) (@size T s1) then @nth T x1 s1 i else @nth T x1 s2 (Init.Nat.sub i (@size T s1))) (if ssrnat.leq (S (S i)) (@size T s1) then @nth T x1 s1 (S i) else @nth T x1 s2 (Init.Nat.sub (S i) (@size T s1))) *)
  contradict Hi0 ; by apply SSR_leq.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: (ssrnat.leq (S i) (size s1)) => // _.
(* Goal: adapted_couple (@SF_fun R s z0) (@head R (IZR Z0) (@SF_lx R s)) (@last R (IZR Z0) (@SF_lx R s)) (seq2Rlist (@SF_lx R s)) (seq2Rlist (@SF_ly R s)) *)
  have : ~ssrnat.leq (S (S i)) (size s1).
(* Goal: not (is_true (ssrnat.leq (S (S i)) (@size T s1))) *)
(* Goal: forall _ : not (is_true (ssrnat.leq (S (S i)) (@size T s1))), Ord (@nth T x1 s2 (Init.Nat.sub i (@size T s1))) (if ssrnat.leq (S (S i)) (@size T s1) then @nth T x1 s1 (S i) else @nth T x1 s2 (Init.Nat.sub (S i) (@size T s1))) *)
  contradict Hi0.
(* Goal: le (S i) (@size T s1) *)
(* Goal: forall _ : not (is_true (ssrnat.leq (S (S i)) (@size T s1))), Ord (@nth T x1 s2 (Init.Nat.sub i (@size T s1))) (if ssrnat.leq (S (S i)) (@size T s1) then @nth T x1 s1 (S i) else @nth T x1 s2 (Init.Nat.sub (S i) (@size T s1))) *)
  apply SSR_leq in Hi0.
(* Goal: le i (Init.Nat.pred (@size R (@Datatypes.cons R h s))) *)
  intuition.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: (ssrnat.leq (S (S i)) (size s1)) => // _.
(* Goal: Ord (@nth T x1 s2 (Init.Nat.sub i (@size T s1))) (@nth T x1 s2 (Init.Nat.sub (S i) (@size T s1))) *)
  replace (S i - size s1)%nat with (S (i - size s1)).
(* Goal: Ord (@nth T x1 s2 (Init.Nat.sub i (@size T s1))) (@nth T x1 s2 (S (Init.Nat.sub i (@size T s1)))) *)
(* Goal: @eq nat (S (Init.Nat.sub i (@size T s1))) (Init.Nat.sub (S i) (@size T s1)) *)
  apply H2.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite size_cat in Hi.
(* Goal: @eq nat (S (Init.Nat.sub i (@size T s1))) (Init.Nat.sub (S i) (@size T s1)) *)
  apply not_le in Hi0.
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) x) (Rlt x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i)) *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  elim: (size s1) i Hi Hi0 => [ | n IH] /= i Hi Hi0.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite -minus_n_O.
(* Goal: lt i (Init.Nat.pred (@size T s2)) *)
(* Goal: lt (Init.Nat.sub i (S n)) (Init.Nat.pred (@size T s2)) *)
(* Goal: @eq nat (S (Init.Nat.sub i (@size T s1))) (Init.Nat.sub (S i) (@size T s1)) *)
  unfold ssrnat.addn, ssrnat.addn_rec in Hi.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by rewrite plus_0_l in Hi.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: i Hi Hi0 => [ | i] /= Hi Hi0.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply lt_S_n, lt_n_O in Hi0.
(* Goal: le i (Init.Nat.pred (@size R (@Datatypes.cons R h s))) *)
  apply IH ; by intuition.
(* Goal: @eq nat (S (Init.Nat.sub i (@size T s1))) (Init.Nat.sub (S i) (@size T s1)) *)
  apply not_le in Hi0.
(* Goal: le i (Init.Nat.pred (@size R (@Datatypes.cons R h s))) *)
  rewrite minus_Sn_m ; by intuition.
Qed.

Lemma sorted_head (s : seq R) i :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  sorted Rle s -> (i < size s)%nat -> forall x0, head x0 s <= nth x0 s i.
Proof.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: s => [| h s].
(* Goal: forall x : R, @eq R (@fe a b (SF_inf_r f a b n) x) (@fe b a (SF_inf_r f b a n) x) *)
   move => _ Hi ; by apply lt_n_O in Hi.
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) x) (Rlt x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i)) *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  elim: s h i => [| h0 s IH] h i Hs Hi x0.
(* Goal: Rle (@head R x0 (@Datatypes.cons R h (@Datatypes.nil R))) (@nth R x0 (@Datatypes.cons R h (@Datatypes.nil R)) i) *)
(* Goal: Rle (@head R x0 (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (@nth R x0 (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) *)
    apply lt_n_Sm_le, le_n_O_eq in Hi ; rewrite -Hi ; apply Rle_refl.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: i Hi => [| i] Hi.
(* Goal: Rle (@SF_h R s) (@SF_h R s) *)
(* Goal: Rle (@SF_h R s) (@nth R (IZR Z0) (@unzip1 R R (@map (prod R R) (prod R R) (fun x : prod R R => @pair R R (@fst R R x) (f (@snd R R x))) (@SF_t R s))) i) *)
  apply Rle_refl.
(* Goal: Rle (@head R x0 (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (@nth R x0 (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (S i)) *)
  apply Rle_trans with (r2 := head x0 (h0::s)).
(* Goal: @SF_sorted R Rle (@SF_cons R h0 s) *)
(* Goal: lt i (S (@size R (@unzip1 R R (@SF_t R s)))) *)
  apply Hs.
(* Goal: Rle (@Riemann_sum R_ModuleSpace f s) (@Riemann_sum R_ModuleSpace g s) *)
  apply IH.
(* Goal: @SF_sorted R Rle (@SF_cons R h0 s) *)
(* Goal: lt i (S (@size R (@unzip1 R R (@SF_t R s)))) *)
  apply Hs.
(* Goal: lt (S (S i)) (@size R (@Datatypes.cons R h1 (@Datatypes.cons R h2 s))) *)
(* Goal: @sorted R Rle (@Datatypes.cons R h0 (@Datatypes.cons R h1 (@Datatypes.cons R h2 s))) *)
(* Goal: @eq T (@nth T y0 (@SF_ly T (@SF_cons T h0 s)) (@size (prod R T) (@SF_t T s))) (@nth T y0 (@SF_ly T (@SF_cons T h (@SF_cons T h0 s))) j) *)
  apply lt_S_n, Hi.
Qed.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma sorted_incr (s : seq R) i j : sorted Rle s -> (i <= j)%nat -> (j < size s)%nat
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  -> forall x0, nth x0 s i <= nth x0 s j.
Proof.
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) x) (Rlt x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i)) *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  elim: i j s => [| i IH] j s Hs Hij Hj x0.
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite nth0 ; by apply sorted_head.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: j Hij Hj => [| j] Hij Hj.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply le_Sn_O in Hij.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: s Hs Hj => [| h s] Hs Hj.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply lt_n_O in Hj.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  apply (IH j s) with (x0 := x0) => //.
(* Goal: @SF_sorted R Rle (@SF_cons R h0 s) *)
(* Goal: lt i (S (@size R (@unzip1 R R (@SF_t R s)))) *)
  case: (s) Hs => {s Hj} [| h0 s] Hs ; apply Hs.
(* Goal: le i j *)
(* Goal: lt j (@size R s) *)
  apply le_S_n, Hij.
(* Goal: lt j (@size R s) *)
  apply le_S_n, Hj.
Qed.

Lemma sorted_last (s : seq R) i :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  sorted Rle s -> (i < size s)%nat -> forall x0, nth x0 s i <= last x0 s.
Proof.
(* Goal: forall _ : and (Rlt (Rmin a b) x) (Rlt x (Rmax a b)), Rbar_le (Inf_fct f a b) (Finite (f x)) *)
  move => Hs Hi x0 ; rewrite -nth_last.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: s Hi Hs => [| h s] Hi Hs //.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply lt_n_O in Hi.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  apply sorted_incr => //.
(* Goal: le i (Init.Nat.pred (@size R (@Datatypes.cons R h s))) *)
  intuition.
Qed.

Lemma sorted_dec (s : seq R) x0 (x : R) :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  sorted Rle s -> head x0 s <= x <= last x0 s ->
    {i : nat | nth x0 s i <= x < nth x0 s (S i) /\ (S (S i) < size s)%nat}
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
    + {nth x0 s (size s - 2)%nat <= x <= nth x0 s (size s - 1)%nat}.
Proof.
(* Goal: forall (_ : @sorted R Rle (@Datatypes.cons R h s)) (_ : and (Rle (@head R x0 (@Datatypes.cons R h s)) x) (Rle x (@last R x0 (@Datatypes.cons R h s)))), sumor (@sig nat (fun i : nat => and (and (Rle (@nth R x0 (@Datatypes.cons R h s) i) x) (Rlt x (@nth R x0 (@Datatypes.cons R h s) (S i)))) (lt (S (S i)) (@size R (@Datatypes.cons R h s))))) (and (Rle (@nth R x0 (@Datatypes.cons R h s) (Init.Nat.sub (@size R (@Datatypes.cons R h s)) (S (S O)))) x) (Rle x (@nth R x0 (@Datatypes.cons R h s) (Init.Nat.sub (@size R (@Datatypes.cons R h s)) (S O))))) *)
  case: s => [/= _ Hx| h s] ; simpl minus ; rewrite -?minus_n_O.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    by right.
(* Goal: forall (_ : @sorted R Rle (@Datatypes.cons R h s)) (_ : and (Rle (@head R x0 (@Datatypes.cons R h s)) x) (Rle x (@last R x0 (@Datatypes.cons R h s)))), sumor (@sig nat (fun i : nat => and (and (Rle (@nth R x0 (@Datatypes.cons R h s) i) x) (Rlt x (@nth R x0 (@Datatypes.cons R h s) (S i)))) (lt (S (S i)) (@size R (@Datatypes.cons R h s))))) (and (Rle (@nth R x0 (@Datatypes.cons R h s) (Init.Nat.sub (@size R (@Datatypes.cons R h s)) (S (S O)))) x) (Rle x (@nth R x0 (@Datatypes.cons R h s) (Init.Nat.sub (@size R (@Datatypes.cons R h s)) (S O))))) *)
  case: s => [/= _ Hx| h0 s] ; simpl minus ; rewrite -?minus_n_O.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    by right.
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) x) (Rlt x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i)) *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  elim: s h h0 => [/= | h1 s IH] h h0 Hs Hx.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    by right.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: (Rlt_le_dec x h0) => Hx'.
(* Goal: le i (Init.Nat.pred (@size R (@Datatypes.cons R h s))) *)
    left ; exists O => /= ; intuition.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: (IH h0 h1) => [ | |[i Hi]|Hi].
(* Goal: @SF_sorted R Rle (@SF_cons R h0 s) *)
(* Goal: lt i (S (@size R (@unzip1 R R (@SF_t R s)))) *)
  apply Hs.
(* Goal: and (Rle (@head R x0 (@Datatypes.cons R h0 (@Datatypes.cons R h1 s))) x) (Rle x (@last R x0 (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) *)
(* Goal: sumor (@sig nat (fun i : nat => and (and (Rle (@nth R x0 (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s))) i) x) (Rlt x (@nth R x0 (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s))) (S i)))) (lt (S (S i)) (@size R (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s))))))) (and (Rle (@nth R x0 (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s))) (Init.Nat.sub (@size R (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (S (S O)))) x) (Rle x (@nth R x0 (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s))) (Init.Nat.sub (@size R (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (S O))))) *)
(* Goal: sumor (@sig nat (fun i : nat => and (and (Rle (@nth R x0 (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s))) i) x) (Rlt x (@nth R x0 (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s))) (S i)))) (lt (S (S i)) (@size R (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s))))))) (and (Rle (@nth R x0 (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s))) (Init.Nat.sub (@size R (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (S (S O)))) x) (Rle x (@nth R x0 (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s))) (Init.Nat.sub (@size R (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (S O))))) *)
  split ; [apply Hx'|apply Hx].
(* Goal: le i (Init.Nat.pred (@size R (@Datatypes.cons R h s))) *)
  left ; exists (S i) => /= ; intuition.
(* Goal: sumor (@sig nat (fun i : nat => and (and (Rle (@nth R x0 (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s))) i) x) (Rlt x (@nth R x0 (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s))) (S i)))) (lt (S (S i)) (@size R (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s))))))) (and (Rle (@nth R x0 (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s))) (Init.Nat.sub (@size R (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (S (S O)))) x) (Rle x (@nth R x0 (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s))) (Init.Nat.sub (@size R (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (S O))))) *)
  right => /= ; simpl in Hi.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by rewrite -minus_n_O in Hi.
Qed.

Lemma sorted_compat (s : seq R) :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  sorted Rle s <-> ordered_Rlist (seq2Rlist s).
Proof.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: s => [| h s].
(* s = [::] *)
(* Goal: iff (@sorted R Rle (@Datatypes.cons R h (@Datatypes.nil R))) (ordered_Rlist (seq2Rlist (@Datatypes.cons R h (@Datatypes.nil R)))) *)
(* Goal: iff (@sorted R Rle (@Datatypes.cons R h' (@Datatypes.cons R h s))) (ordered_Rlist (seq2Rlist (@Datatypes.cons R h' (@Datatypes.cons R h s)))) *)
  split => // H i /= Hi ; contradict Hi ; apply lt_n_O.
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) x) (Rlt x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i)) *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  elim: s h => [h | h s IHs h'].
(* s = [::_] *)
(* Goal: iff (@sorted R Rle (@Datatypes.cons R h (@Datatypes.nil R))) (ordered_Rlist (seq2Rlist (@Datatypes.cons R h (@Datatypes.nil R)))) *)
(* Goal: iff (@sorted R Rle (@Datatypes.cons R h' (@Datatypes.cons R h s))) (ordered_Rlist (seq2Rlist (@Datatypes.cons R h' (@Datatypes.cons R h s)))) *)
  split => // H i /= Hi ; contradict Hi ; apply lt_n_O.
(* s = _::(_::_) *)
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  split => H.
(* Goal: ordered_Rlist (seq2Rlist (@Datatypes.cons R h' (@Datatypes.cons R h s))) *)
(* Goal: @sorted R Rle (@Datatypes.cons R h' (@Datatypes.cons R h s)) *)
  case => [ /= | i] ; rewrite size_compat => Hi ; simpl in Hi.
(* Goal: @eq R (@nth R (IZR Z0) (@behead R unif_part0) n0) (@nth R (Rplus a (Rdiv (Rmult (IZR Z0) (Rminus b a)) (Rplus (INR n) (IZR (Zpos xH))))) (@behead R unif_part0) n0) *)
(* Goal: @eq (list R) (@Datatypes.cons R (@head R (IZR Z0) unif_part0) (@behead R unif_part0)) unif_part0 *)
(* Goal: lt O (@size R (unif_part a b n)) *)
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@Riemann_sum V g l) (@Riemann_sum V f l) *)
  apply H.
(* Goal: Rle (pos_Rl (seq2Rlist (@Datatypes.cons R h' (@Datatypes.cons R h s))) (S i)) (pos_Rl (seq2Rlist (@Datatypes.cons R h' (@Datatypes.cons R h s))) (S (S i))) *)
(* Goal: @sorted R Rle (@Datatypes.cons R h' (@Datatypes.cons R h s)) *)
  apply (proj1 (IHs h) (proj2 H) i) ; rewrite size_compat /= ; apply lt_S_n => //.
(* Goal: and (Rle u u) (Rle u v) *)
(* Goal: Rle (Rmin a b) (Rmax a b) *)
(* Goal: P (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n)) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  split.
(* Goal: Rle h' h *)
(* Goal: @sorted R Rle (@Datatypes.cons R h s) *)
  apply (H O) ; rewrite size_compat /= ; apply lt_O_Sn.
  apply IHs => i ; rewrite size_compat /= => Hi ; apply (H (S i)) ;
  rewrite size_compat /= ; apply lt_n_S, Hi.
Qed.

(** seq_step *)

Definition seq_step (s : seq R) :=
  foldr Rmax 0 (pairmap (fun x y => Rabs (Rminus y x)) (head 0 s) (behead s)).

Lemma seq_step_ge_0 x : (0 <= seq_step x).
Proof.
(* Goal: Rle (IZR Z0) (seq_step x) *)
  clear ; unfold seq_step ; case: x => [ | x0 x] //= .
(* Goal: Rle (@SF_h R s) (@SF_h R s) *)
(* Goal: Rle (@SF_h R s) (@nth R (IZR Z0) (@unzip1 R R (@map (prod R R) (prod R R) (fun x : prod R R => @pair R R (@fst R R x) (f (@snd R R x))) (@SF_t R s))) i) *)
  by apply Rle_refl.
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) x) (Rlt x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i)) *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  elim: x x0 => [ | x1 x IH] //= x0.
(* Goal: Rle (@SF_h R s) (@SF_h R s) *)
(* Goal: Rle (@SF_h R s) (@nth R (IZR Z0) (@unzip1 R R (@map (prod R R) (prod R R) (fun x : prod R R => @pair R R (@fst R R x) (f (@snd R R x))) (@SF_t R s))) i) *)
  by apply Rle_refl.
(* Goal: Rle (Rmin a b) (Rmax a b) *)
(* Goal: @ex (@SF_seq R) (fun x : @SF_seq R => P x) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  apply Rmax_case.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply Rabs_pos.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by [].
Qed.
Lemma seq_step_cat (x y : seq R) :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  (0 < size x)%nat -> (0 < size y)%nat ->
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  last 0 x = head 0 y ->
  seq_step (cat x (behead y)) = Rmax (seq_step x) (seq_step y).
Proof.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: x => /= [ H | x0 x _].
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply lt_irrefl in H.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: y => /= [ H | y0 y _].
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply lt_irrefl in H.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  move => <-.
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) x) (Rlt x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i)) *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  elim: x y x0 {y0} => /= [ | x1 x IH] y x0.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite {2}/seq_step /=.
(* Goal: @eq Rlist (@subdivision_val a b (SF_inf_r f a b n)) (if Rle_dec a b then seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part a b n))) else seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part b a n)))) *)
  rewrite /Rmax ; case: Rle_dec (seq_step_ge_0 (x0 :: y)) => // _ _.
(* Goal: @eq R (seq_step (@Datatypes.cons R x0 (@Datatypes.cons R x1 (@cat R x y)))) (Rmax (seq_step (@Datatypes.cons R x0 (@Datatypes.cons R x1 x))) (seq_step (@Datatypes.cons R (@last R x1 x) y))) *)
  unfold seq_step ; simpl.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite -Rmax_assoc.
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus (@SF_h R s) x0) (f y0)) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus (@SF_h R s) x0) (g y0)) (@plus (ModuleSpace.AbelianGroup R_Ring V) (Riemann_sum f s) (Riemann_sum g s)))) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus (@SF_h R s) x0) (f y0)) (@plus (ModuleSpace.AbelianGroup R_Ring V) (Riemann_sum f s) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus (@SF_h R s) x0) (g y0)) (Riemann_sum g s)))) *)
  apply f_equal.
(* Goal: Rle (@Riemann_sum R_ModuleSpace f s) (@Riemann_sum R_ModuleSpace g s) *)
  by apply IH.
Qed.

Lemma seq_step_rev (l : seq R) : seq_step (rev l) = seq_step l.
Proof.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite /seq_step.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite head_rev behead_rev /=.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: l => [ | x0 l] //=.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: l => [ | x1 l] //=.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite rev_cons.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: l => [ | x2 l] //=.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by rewrite -Rabs_Ropp Ropp_minus_distr'.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite rev_cons pairmap_rcons foldr_rcons.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite -Rabs_Ropp Ropp_minus_distr'.
(* Goal: @eq R (@foldr R R Rmax (Rmax (Rabs (Rminus x1 x0)) (IZR Z0)) (@pairmap R R (fun x y : R => Rabs (Rminus y x)) (@last R x2 l) (@rcons R (@rev R (@seq.belast R x2 l)) x1))) (Rmax (Rabs (Rminus x1 x0)) (Rmax (Rabs (Rminus x2 x1)) (@foldr R R Rmax (IZR Z0) (@pairmap R R (fun x y : R => Rabs (Rminus y x)) x2 l)))) *)
  generalize (Rabs (x1 - x0)) ; clear.
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) x) (Rlt x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i)) *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  elim: l x1 x2 => [ | x2 l IH] x0 x1 r //=.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite -Rabs_Ropp Ropp_minus_distr' !Rmax_assoc.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  apply f_equal2 => //.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply Rmax_comm.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite rev_cons pairmap_rcons foldr_rcons.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite -Rabs_Ropp Ropp_minus_distr' Rmax_assoc IH.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by rewrite (Rmax_comm _ r) !Rmax_assoc.
Qed.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma nth_le_seq_step x0 (l : seq R) (i : nat) : (S i < size l)%nat ->
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  Rabs (nth x0 l (S i) - nth x0 l i) <= seq_step l.
Proof.
(* Goal: @eq R (pos_Rl (seq2Rlist s) n) (@nth R (IZR Z0) s n) *)
  elim: i l => [ | i IH] ; case => [ | x1 l] /= Hi.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply lt_n_O in Hi.
  apply lt_S_n in Hi.
  destruct l as [ | x2 l].
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply lt_n_O in Hi.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply Rmax_l.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply lt_n_O in Hi.
  apply lt_S_n in Hi.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  move: (IH l Hi).
  destruct l as [ | x2 l] ; simpl.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply lt_n_O in Hi.
  simpl in Hi ; apply lt_S_n in Hi.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  move => {IH} IH.
  eapply Rle_trans.
(* Goal: Rle (@Riemann_sum R_ModuleSpace f s) (@Riemann_sum R_ModuleSpace g s) *)
  by apply IH.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply Rmax_r.
Qed.

(** * Definitions of SF_seq *)

Section SF_seq.
Context {T : Type}.

Record SF_seq := mkSF_seq {SF_h : R ; SF_t : seq (R * T)}.
Definition SF_lx (s : SF_seq) : seq R := (SF_h s)::(unzip1 (SF_t s)).
Definition SF_ly (s : SF_seq) : seq T := unzip2 (SF_t s).
Definition SF_make (lx : seq R) (ly : seq T) (Hs : size lx = S (size ly)) : SF_seq :=
  mkSF_seq (head 0 lx) (zip (behead lx) ly).

Lemma SF_size_lx_ly (s : SF_seq) : size (SF_lx s) = S (size (SF_ly s)).
Proof.
  case: s => sh st ;
  rewrite /SF_lx /SF_ly /= ;
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  elim: st => //= t s -> //.
Qed.
Lemma SF_seq_bij (s : SF_seq) :
  SF_make (SF_lx s) (SF_ly s) (SF_size_lx_ly s) = s.
Proof.
(* Goal: @eq nat (@SF_size T (@SF_seq_f2 T f2 P)) (Init.Nat.pred (@size R P)) *)
  case: s => sh st ; by rewrite /SF_make (zip_unzip st).
Qed.
Lemma SF_seq_bij_lx (lx : seq R) (ly : seq T)
  (Hs : size lx = S (size ly)) : SF_lx (SF_make lx ly Hs) = lx.
Proof.
  case: lx Hs => // x lx Hs ;
  rewrite /SF_make / SF_lx unzip1_zip //= ;
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  apply SSR_leq, le_S_n ; rewrite -Hs => //.
Qed.
Lemma SF_seq_bij_ly (lx : seq R) (ly : seq T)
  (Hs : size lx = S (size ly)) : SF_ly (SF_make lx ly Hs) = ly.
Proof.
  case: lx Hs => // x lx Hs ;
  rewrite /SF_make / SF_ly unzip2_zip //= ;
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  apply SSR_leq, le_S_n ; rewrite -Hs => //.
Qed.

(** ** Constructors *)

Definition SF_nil (x0 : R) : SF_seq := mkSF_seq x0 [::].
Definition SF_cons (h : R*T) (s : SF_seq) :=
  mkSF_seq (fst h) ((SF_h s,snd h)::(SF_t s)).
Definition SF_rcons (s : SF_seq) (t : R*T) :=
  mkSF_seq (SF_h s) (rcons (SF_t s) t).

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_cons_dec (P : SF_seq -> Type) :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  (forall x0 : R, P (SF_nil x0)) -> (forall h s, P (SF_cons h s))
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
    -> (forall s, P s).
Proof.
(* Goal: forall (_ : forall x0 : R, P (SF_nil x0)) (_ : forall (h : prod R T) (s : SF_seq), P (SF_cons h s)) (s : SF_seq), P s *)
  move => Hnil Hcons [sh st] ; case: st => [| h sf].
(* Goal: P (mkSF_seq sh (@Datatypes.nil (prod R T))) *)
(* Goal: P (mkSF_seq sh (@rcons (prod R T) st t)) *)
  apply Hnil.
  move: (Hcons (sh,snd h) (mkSF_seq (fst h) sf)) => {Hcons} ;
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  rewrite /SF_cons -surjective_pairing //=.
Qed.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_cons_ind (P : SF_seq -> Type) :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  (forall x0 : R, P (SF_nil x0)) -> (forall h s, P s -> P (SF_cons h s))
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
    -> (forall s, P s).
Proof.
(* Goal: forall (_ : forall x0 : R, P (SF_nil x0)) (_ : forall (h : prod R T) (s : SF_seq) (_ : P s), P (SF_cons h s)) (s : SF_seq), P s *)
  move => Hnil Hcons [sh st] ; elim: st sh => [sh |h sf IHst sh].
(* Goal: P (mkSF_seq sh (@Datatypes.nil (prod R T))) *)
(* Goal: P (mkSF_seq sh (@rcons (prod R T) st t)) *)
  apply Hnil.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  move: (IHst (fst h)) => {IHst} IHst.
  move: (Hcons (sh,snd h) (mkSF_seq (fst h) sf) IHst) => {Hcons} ;
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  rewrite /SF_cons -surjective_pairing //=.
Qed.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_rcons_dec (P : SF_seq -> Type) :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  (forall x0 : R, P (SF_nil x0)) -> (forall s t, P (SF_rcons s t))
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
    -> (forall s, P s).
Proof.
(* Goal: forall (_ : forall x0 : R, P (SF_nil x0)) (_ : forall (s : SF_seq) (t : prod R T) (_ : P s), P (SF_rcons s t)) (s : SF_seq), P s *)
  move => Hnil Hrcons [sh st] ; move: st ; apply rcons_dec => [| st t].
(* Goal: P (mkSF_seq sh (@Datatypes.nil (prod R T))) *)
(* Goal: P (mkSF_seq sh (@rcons (prod R T) st t)) *)
  apply Hnil.
(* Goal: P (mkSF_seq sh (@rcons (prod R T) st t)) *)
  apply (Hrcons (mkSF_seq sh st) t).
Qed.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_rcons_ind (P : SF_seq -> Type) :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  (forall x0 : R, P (SF_nil x0)) -> (forall s t, P s -> P (SF_rcons s t))
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
    -> (forall s, P s).
Proof.
  move => Hnil Hrcons [sh st] ; move: st sh ;
  apply (rcons_ind (fun st => forall sh, P {| SF_h := sh; SF_t := st |})) => [sh | st t IHst sh].
(* Goal: P (mkSF_seq sh (@Datatypes.nil (prod R T))) *)
(* Goal: P (mkSF_seq sh (@rcons (prod R T) st t)) *)
  apply Hnil.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  apply (Hrcons (mkSF_seq sh st) t) => //.
Qed.

Lemma SF_cons_rcons (h : R*T) (s : SF_seq) (l : R*T) :
  SF_cons h (SF_rcons s l) = SF_rcons (SF_cons h s) l.
Proof.
  case: h => hx hy ;
  case: l => lx ly ;
  case: s => sh st //.
Qed.

(** ** SF_seq and seq *)

Lemma SF_lx_nil (x0 : R) :
  SF_lx (SF_nil x0) = [:: x0].
Proof.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by [].
Qed.
Lemma SF_ly_nil (x0 : R) :
  SF_ly (SF_nil x0) = [::].
Proof.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by [].
Qed.

Lemma SF_lx_cons (h : R*T) (s : SF_seq) :
  SF_lx (SF_cons h s) = (fst h) :: (SF_lx s).
Proof.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by [].
Qed.
Lemma SF_ly_cons (h : R*T) (s : SF_seq) :
  SF_ly (SF_cons h s) = (snd h) :: (SF_ly s).
Proof.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by [].
Qed.
Lemma SF_lx_rcons (s : SF_seq) (h : R*T) :
  SF_lx (SF_rcons s h) = rcons (SF_lx s) (fst h).
Proof.
  case: s => sh st ; rewrite /SF_lx /SF_rcons /= ; elim: st sh => // [[x y] st] IHst sh /= ;
  by rewrite (IHst x).
Qed.
Lemma SF_ly_rcons (s : SF_seq) (h : R*T) :
  SF_ly (SF_rcons s h) = rcons (SF_ly s) (snd h).
Proof.
  case: s => sh st ; rewrite /SF_ly /SF_rcons /= ; elim: st sh => // [[x y] st] IHst sh /= ;
  by rewrite (IHst x).
Qed.

Lemma SF_lx_surj (s s0 : SF_seq) :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  s = s0 -> SF_lx s = SF_lx s0.
Proof.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by move => ->.
Qed.
Lemma SF_ly_surj (s s0 : SF_seq) :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  s = s0 -> SF_ly s = SF_ly s0.
Proof.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by move => ->.
Qed.
Lemma SF_lx_ly_inj (s s0 : SF_seq) :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  SF_lx s = SF_lx s0 -> SF_ly s = SF_ly s0 -> s = s0.
Proof.
  move: s0 ; apply SF_cons_ind with (s := s) => {s} [x | h s IH] s0 ;
    apply SF_cons_dec with (s := s0) => {s0} [x0 | h0 s0] Hx Hy //.
(* s = SF_nil _ *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite !SF_lx_nil in Hx.
  replace x with (head 0 ([::x])) by intuition ;
  by rewrite Hx.
(* s = SF_cons _ _*)
(* Goal: @eq (ModuleSpace.sort R_Ring V) (Riemann_sum (fun x : R => @plus (ModuleSpace.AbelianGroup R_Ring V) (f x) (g x)) (@SF_cons R (@pair R R x0 y0) s)) (@plus (ModuleSpace.AbelianGroup R_Ring V) (Riemann_sum f (@SF_cons R (@pair R R x0 y0) s)) (Riemann_sum g (@SF_cons R (@pair R R x0 y0) s))) *)
  rewrite !SF_lx_cons in Hx ; rewrite !SF_ly_cons in Hy.
  replace h with (head (fst h) (fst h :: SF_lx s),head (snd h) (snd h :: SF_ly s)) ;
    [ rewrite Hx Hy (IH s0) //= | move => /= ; by apply injective_projections].
(* Goal: @eq (list R) (SF_lx s) (SF_lx s0) *)
(* Goal: @eq (list T) (SF_ly s) (SF_ly s0) *)
  replace (SF_lx s) with (behead (fst h :: SF_lx s)) by intuition ; by rewrite Hx.
(* Goal: @eq (list T) (SF_ly s) (SF_ly s0) *)
  replace (SF_ly s) with (behead (snd h :: SF_ly s)) by intuition ; by rewrite Hy.
Qed.

(** ** SF_size *)

Definition SF_size (s : SF_seq) := size (SF_t s).

Lemma SF_size_cons (h : R*T) (s : SF_seq) : SF_size (SF_cons h s) = S (SF_size s).
Proof.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite /SF_cons /SF_size //=.
Qed.

Lemma SF_size_rcons (s : SF_seq) (t : R*T) : SF_size (SF_rcons s t) = S (SF_size s).
Proof.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite /SF_rcons /SF_size size_rcons //=.
Qed.

Lemma SF_size_lx (s : SF_seq) : size (SF_lx s) = S (SF_size s).
Proof.
(* Goal: @eq nat (@size T (SF_ly s)) (SF_size s) *)
  case: s => sh st ; rewrite /SF_size /= ; elim: st => //= _ st -> //.
Qed.
Lemma SF_size_ly (s : SF_seq) : size (SF_ly s) = SF_size s.
Proof.
(* Goal: @eq nat (@size T (SF_ly s)) (SF_size s) *)
  case: s => sh st ; rewrite /SF_size /= ; elim: st => //= _ st -> //.
Qed.

(** ** SF_rev *)

Lemma SF_rev_0 (s : SF_seq) :
  size (rev (SF_lx s)) = S (size (rev (SF_ly s))).
Proof.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by rewrite ?size_rev SF_size_lx SF_size_ly.
Qed.
Definition SF_rev (s : SF_seq) : SF_seq :=
  SF_make (rev (SF_lx s)) (rev (SF_ly s)) (SF_rev_0 s).

Lemma SF_rev_cons (h : R*T) (s : SF_seq) :
  SF_rev (SF_cons h s) = SF_rcons (SF_rev s) h.
Proof.
(* Goal: @eq (@SF_seq T0) (@SF_rev T0 (SF_map f s)) (SF_map f (@SF_rev T s)) *)
  apply SF_lx_ly_inj.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by rewrite SF_lx_rcons !SF_seq_bij_lx SF_lx_cons rev_cons.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by rewrite SF_ly_rcons !SF_seq_bij_ly SF_ly_cons rev_cons.
Qed.
Lemma SF_rev_rcons (s : SF_seq) (t : R*T) :
  SF_rev (SF_rcons s t) = SF_cons t (SF_rev s).
Proof.
(* Goal: @eq (@SF_seq T0) (@SF_rev T0 (SF_map f s)) (SF_map f (@SF_rev T s)) *)
  apply SF_lx_ly_inj.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by rewrite SF_lx_cons !SF_seq_bij_lx SF_lx_rcons rev_rcons.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by rewrite SF_ly_cons !SF_seq_bij_ly SF_ly_rcons rev_rcons.
Qed.

Lemma SF_rev_invol (s : SF_seq) :
  SF_rev (SF_rev s) = s.
Proof.
(* Goal: @eq (@SF_seq T0) (@SF_rev T0 (SF_map f s)) (SF_map f (@SF_rev T s)) *)
  apply SF_lx_ly_inj.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by rewrite /SF_rev ?SF_seq_bij_lx revK.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by rewrite /SF_rev ?SF_seq_bij_ly revK.
Qed.

Lemma SF_lx_rev (s : SF_seq) : SF_lx (SF_rev s) = rev (SF_lx s).
Proof.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by rewrite /SF_rev ?SF_seq_bij_lx.
Qed.
Lemma SF_ly_rev (s : SF_seq) : SF_ly (SF_rev s) = rev (SF_ly s).
Proof.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by rewrite /SF_rev ?SF_seq_bij_ly.
Qed.

Lemma SF_size_rev (s : SF_seq) : SF_size (SF_rev s) = SF_size s.
Proof.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by rewrite -?SF_size_ly SF_ly_rev size_rev.
Qed.

Lemma SF_rev_surj (s s0 : SF_seq) :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  s = s0 -> SF_rev s = SF_rev s0.
Proof.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by move => ->.
Qed.
Lemma SF_rev_inj (s s0 : SF_seq) :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  SF_rev s = SF_rev s0 -> s = s0.
Proof.
(* Goal: forall x : R, @eq R (@fe a b (SF_inf_r f a b n) x) (@fe b a (SF_inf_r f b a n) x) *)
  move => H ; by rewrite -(SF_rev_invol s) -(SF_rev_invol s0) H.
Qed.

(** ** SF_cat *)

Definition SF_cat (x y : SF_seq) := mkSF_seq (SF_h x) ((SF_t x) ++ (SF_t y)).
Lemma SF_lx_cat (x y : SF_seq) :
(* Goal: forall (i : nat) (_ : lt (S i) (@size R (unif_part a b n))), @eq R (Rminus (@nth R (IZR Z0) (unif_part a b n) (S i)) (@nth R (IZR Z0) (unif_part a b n) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
  SF_lx (SF_cat x y) = (SF_lx x) ++ (behead (SF_lx y)).
Proof.
(* Goal: @eq (list R) (SF_lx (SF_cat x y)) (@cat R (SF_lx x) (@behead R (SF_lx y))) *)
  unfold SF_cat, SF_lx ; simpl.
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus (@SF_h R s) x0) (f y0)) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus (@SF_h R s) x0) (g y0)) (@plus (ModuleSpace.AbelianGroup R_Ring V) (Riemann_sum f s) (Riemann_sum g s)))) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus (@SF_h R s) x0) (f y0)) (@plus (ModuleSpace.AbelianGroup R_Ring V) (Riemann_sum f s) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus (@SF_h R s) x0) (g y0)) (Riemann_sum g s)))) *)
  apply f_equal.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by elim: (SF_t x) => //= t h ->.
Qed.
Lemma SF_last_cat (x y : SF_seq) :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  last (SF_h x) (SF_lx x) = head (SF_h y) (SF_lx y) ->
  last (SF_h (SF_cat x y)) (SF_lx (SF_cat x y)) = (last (SF_h y) (SF_lx y)).
Proof.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite SF_lx_cat.
(* Goal: forall _ : @eq R (@last R (SF_h x) (SF_lx x)) (@head R (SF_h y) (SF_lx y)), @eq R (@last R (SF_h (SF_cat x y)) (@cat R (SF_lx x) (@behead R (SF_lx y)))) (@last R (SF_h y) (SF_lx y)) *)
  unfold SF_cat, SF_lx ; simpl => <- /=.
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) x) (Rlt x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i)) *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  elim: (SF_t x) (SF_h x) => //= {x} x1 x x0.
Qed.
Lemma SF_cons_cat x0 (x y : SF_seq) :
  SF_cons x0 (SF_cat x y) = SF_cat (SF_cons x0 x) y.
Proof.
(* Goal: @eq SF_seq (SF_cons x0 (SF_cat x y)) (SF_cat (SF_cons x0 x) y) *)
  reflexivity.
Qed.

(** ** first and last pair *)

Definition SF_head (y0 : T) (s : SF_seq) := (SF_h s, head y0 (SF_ly s)).
Definition SF_behead (s : SF_seq) :=
  mkSF_seq (head (SF_h s) (unzip1 (SF_t s))) (behead (SF_t s)).

Definition SF_last y0 (s : SF_seq) : (R*T) :=
  last (SF_h s,y0) (SF_t s).
Definition SF_belast (s : SF_seq) : SF_seq :=
  mkSF_seq (SF_h s) (Rcomplements.belast (SF_t s)).

Lemma SF_last_lx x0 (s : SF_seq) :
  fst (SF_last x0 s) = last 0 (SF_lx s).
Proof.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite /SF_last /=.
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@opp (ModuleSpace.AbelianGroup R_Ring V) (RInt_val f b a n)) (RInt_val f a b n) *)
(* Goal: forall _ : forall (x : R) (_ : and (Rle (Rmin a b) x) (Rle x (Rmax a b))), @eq (ModuleSpace.sort R_Ring V) (f x) (g x), @eq (ModuleSpace.sort R_Ring V) (RInt_val g a b n) (RInt_val f a b n) *)
  apply sym_eq ; by apply last_unzip1.
Qed.

(** ** SF_sorted *)

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Definition SF_sorted (Ord : R -> R -> Prop) (s : SF_seq) :=
  sorted Ord (SF_lx s).

End SF_seq.

(** ** SF_map *)

Section SF_map.

Context {T T0 : Type}.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Definition SF_map  (f : T -> T0) (s : SF_seq) : SF_seq :=
  mkSF_seq (SF_h s) (map (fun x => (fst x,f (snd x))) (SF_t s)).

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_map_cons (f : T -> T0) (h : R*T) (s : SF_seq) :
  SF_map f (SF_cons h s) = SF_cons (fst h, f (snd h)) (SF_map f s).
Proof.
(* Goal: @eq (@SF_seq T0) (SF_map f (@SF_cons T h s)) (@SF_cons T0 (@pair R T0 (@fst R T h) (f (@snd R T h))) (SF_map f s)) *)
  case: s => sh ; elim => // h st ; rewrite /SF_map => //.
Qed.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_map_rcons (f : T -> T0) (s : SF_seq) (h : R*T) :
  SF_map f (SF_rcons s h) = SF_rcons (SF_map f s) (fst h, f (snd h)).
Proof.
(* Goal: @eq (ModuleSpace.sort R_Ring R_ModuleSpace) (@Riemann_sum R_ModuleSpace f s) (Int_SF (@subdivision_val (@head R (IZR Z0) (@SF_lx R (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s))) (@last R (IZR Z0) (@SF_lx R (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s))) (SF_compat_le (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s) (@SF_map_sort R (ModuleSpace.sort R_Ring R_ModuleSpace) f s Rle pr))) (@subdivision (@head R (IZR Z0) (@SF_lx R (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s))) (@last R (IZR Z0) (@SF_lx R (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s))) (SF_compat_le (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s) (@SF_map_sort R (ModuleSpace.sort R_Ring R_ModuleSpace) f s Rle pr)))) *)
(* Goal: @eq (ModuleSpace.sort R_Ring R_ModuleSpace) (@Riemann_sum R_ModuleSpace f s) (Ropp (Int_SF (@subdivision_val (@head R (IZR Z0) (@SF_lx R (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s))) (@last R (IZR Z0) (@SF_lx R (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s))) (SF_compat_le (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s) (@SF_map_sort R (ModuleSpace.sort R_Ring R_ModuleSpace) f s Rle pr))) (@subdivision (@head R (IZR Z0) (@SF_lx R (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s))) (@last R (IZR Z0) (@SF_lx R (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s))) (SF_compat_le (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s) (@SF_map_sort R (ModuleSpace.sort R_Ring R_ModuleSpace) f s Rle pr))))) *)
  move: h ; apply SF_cons_ind with (s := s) => {s} [x0 | h0 s IH] //= h.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite SF_map_cons.
  replace (SF_rcons (SF_cons h0 s) h) with
(* Goal: @eq (@SF_seq T0) (@SF_cons T0 (@pair R T0 (@fst R T h0) (f (@snd R T h0))) (@SF_rcons T0 (SF_map f s) (@pair R T0 (@fst R T h) (f (@snd R T h))))) (@SF_rcons T0 (@SF_cons T0 (@pair R T0 (@fst R T h0) (f (@snd R T h0))) (SF_map f s)) (@pair R T0 (@fst R T h) (f (@snd R T h)))) *)
    (SF_cons h0 (SF_rcons s h)) by auto.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite SF_map_cons.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite IH.
(* Goal: @eq (@SF_seq T0) (@SF_cons T0 (@pair R T0 (@fst R T h0) (f (@snd R T h0))) (@SF_rcons T0 (SF_map f s) (@pair R T0 (@fst R T h) (f (@snd R T h))))) (@SF_rcons T0 (@SF_cons T0 (@pair R T0 (@fst R T h0) (f (@snd R T h0))) (SF_map f s)) (@pair R T0 (@fst R T h) (f (@snd R T h)))) *)
  auto.
Qed.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_map_lx (f : T -> T0) (s : SF_seq) :
  SF_lx (SF_map f s) = SF_lx s.
Proof.
  apply SF_cons_ind with (s := s) => {s} //= h s IH ;
  by rewrite SF_map_cons ?SF_lx_cons IH.
Qed.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_map_ly (f : T -> T0) (s : SF_seq) :
  SF_ly (SF_map f s) = map f (SF_ly s).
Proof.
  apply SF_cons_ind with (s := s) => {s} //= h s IH ;
  by rewrite SF_map_cons ?SF_ly_cons IH.
Qed.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_map_rev (f : T -> T0) s :
  SF_rev (SF_map f s) = SF_map f (SF_rev s).
Proof.
(* Goal: @eq (@SF_seq T0) (@SF_rev T0 (SF_map f s)) (SF_map f (@SF_rev T s)) *)
  apply SF_lx_ly_inj.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by rewrite SF_lx_rev ?SF_map_lx ?SF_lx_rev.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by rewrite SF_ly_rev ?SF_map_ly ?SF_ly_rev map_rev.
Qed.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_map_sort (f : T -> T0) (s : SF_seq) (Ord : R -> R -> Prop) :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  SF_sorted Ord s -> SF_sorted Ord (SF_map f s).
Proof.
  unfold SF_sorted ;
  apply SF_cons_ind with (s := s) => {s} /= [x0 | [x0 _] /= s IH] Hs.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by [].
(* Goal: and (Rle u u) (Rle u v) *)
(* Goal: Rle (Rmin a b) (Rmax a b) *)
(* Goal: P (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n)) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  split.
(* Goal: @SF_sorted R Rle (@SF_cons R h0 s) *)
(* Goal: lt i (S (@size R (@unzip1 R R (@SF_t R s)))) *)
  by apply Hs.
(* Goal: Rle (@Riemann_sum R_ModuleSpace f s) (@Riemann_sum R_ModuleSpace g s) *)
  now apply IH.
Qed.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_size_map (f : T -> T0) s :
  SF_size (SF_map f s) = SF_size s.
Proof.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by rewrite -!SF_size_ly SF_map_ly size_map.
Qed.

End SF_map.

(** * Pointed subvivision *)

Definition pointed_subdiv (ptd : @SF_seq R) :=
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  forall i : nat, (i < SF_size ptd)%nat ->
    nth 0 (SF_lx ptd) i <= nth 0 (SF_ly ptd) i <= nth 0 (SF_lx ptd) (S i).

Lemma ptd_cons h s : pointed_subdiv (SF_cons h s) -> pointed_subdiv s.
Proof.
(* Goal: le i (Init.Nat.pred (@size R (@Datatypes.cons R h s))) *)
  move => H i Hi ; apply (H (S i)) ; rewrite SF_size_cons ; intuition.
Qed.
Lemma ptd_sort ptd :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  pointed_subdiv ptd -> SF_sorted Rle ptd.
Proof.
  apply SF_cons_ind with (s := ptd) => {ptd} [x0 | [x0 y0] ptd] ;
  [ | apply SF_cons_dec with (s := ptd) => {ptd} [ x1 | [x1 y1] ptd] IH] =>
  Hptd ; try split => //=.
(* Goal: Rle x0 x1 *)
(* Goal: and (Rle x1 (@SF_h R ptd)) match @unzip1 R R (@SF_t R ptd) with | Datatypes.nil => True | Datatypes.cons h1 t1 => and (Rle (@SF_h R ptd) h1) (@sorted R Rle (@unzip1 R R (@SF_t R ptd))) end *)
  apply Rle_trans with y0 ; apply (Hptd O) ; rewrite SF_size_cons ; apply lt_O_Sn.
(* Goal: Rle x0 x1 *)
(* Goal: and (Rle x1 (@SF_h R ptd)) match @unzip1 R R (@SF_t R ptd) with | Datatypes.nil => True | Datatypes.cons h1 t1 => and (Rle (@SF_h R ptd) h1) (@sorted R Rle (@unzip1 R R (@SF_t R ptd))) end *)
  apply Rle_trans with y0 ; apply (Hptd O) ; rewrite SF_size_cons ; apply lt_O_Sn.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  apply IH, (ptd_cons (x0,y0)) => //.
Qed.
Lemma ptd_sort' ptd :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  pointed_subdiv ptd -> sorted Rle (SF_ly ptd).
Proof.
  apply SF_cons_ind with (s := ptd) => {ptd} [x0 | [x0 y0] ptd] ;
  [ | apply SF_cons_dec with (s := ptd) => {ptd} [ x1 | [x1 y1] ptd] IH] =>
(* Goal: and (Rle u u) (Rle u v) *)
(* Goal: Rle (Rmin a b) (Rmax a b) *)
(* Goal: P (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n)) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  Hptd ; try split.
  apply Rle_trans with x1 ; [apply (Hptd O) | apply (Hptd 1%nat)] ;
(* Goal: lt O (@size R (@Datatypes.cons R (@SF_h R ptd) (@map (prod R R) R (@fst R R) (@SF_t R ptd)))) *)
(* Goal: Rle (@last R (@SF_h R ptd) (@unzip1 R R (@SF_t R ptd))) (@SF_h R ptd) *)
  rewrite ?SF_size_cons ; repeat apply lt_n_S ; apply lt_O_Sn.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  apply IH, (ptd_cons (x0,y0)) => //.
Qed.

Lemma SF_cat_pointed (x y : SF_seq) :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  last (SF_h x) (SF_lx x) = head (SF_h y) (SF_lx y) ->
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  pointed_subdiv x -> pointed_subdiv y
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  -> pointed_subdiv (SF_cat x y).
Proof.
(* Goal: forall (_ : @eq R (@last R (@SF_h R x) (@SF_lx R x)) (@head R (@SF_h R y) (@SF_lx R y))) (_ : pointed_subdiv x) (_ : pointed_subdiv y), pointed_subdiv (@SF_cat R x y) *)
  intros Hxy Hx Hy.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  move: Hxy Hx.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  apply (SF_cons_ind (fun x => last (SF_h x) (SF_lx x) = head (SF_h y) (SF_lx y) -> pointed_subdiv x -> pointed_subdiv (SF_cat x y)))
    => {x} /= [x0 | x0 x IH] Hxy Hx.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite Hxy.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply Hy.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite -SF_cons_cat.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case => [ | i] Hi.
(* Goal: and (Rle (@nth R (IZR Z0) (@SF_lx R (@SF_cons R x0 (@SF_cat R x y))) O) (@nth R (IZR Z0) (@SF_ly R (@SF_cons R x0 (@SF_cat R x y))) O)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_cons R x0 (@SF_cat R x y))) O) (@nth R (IZR Z0) (@SF_lx R (@SF_cons R x0 (@SF_cat R x y))) (S O))) *)
(* Goal: and (Rle (@nth R (IZR Z0) (@SF_lx R (@SF_cons R x0 (@SF_cat R x y))) (S i)) (@nth R (IZR Z0) (@SF_ly R (@SF_cons R x0 (@SF_cat R x y))) (S i))) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_cons R x0 (@SF_cat R x y))) (S i)) (@nth R (IZR Z0) (@SF_lx R (@SF_cons R x0 (@SF_cat R x y))) (S (S i)))) *)
  apply (Hx O), lt_O_Sn.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  apply IH =>//.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply ptd_cons with x0.
(* Goal: lt (S (S i)) (@size R (@Datatypes.cons R h1 (@Datatypes.cons R h2 s))) *)
(* Goal: @sorted R Rle (@Datatypes.cons R h0 (@Datatypes.cons R h1 (@Datatypes.cons R h2 s))) *)
(* Goal: @eq T (@nth T y0 (@SF_ly T (@SF_cons T h0 s)) (@size (prod R T) (@SF_t T s))) (@nth T y0 (@SF_ly T (@SF_cons T h (@SF_cons T h0 s))) j) *)
  by apply lt_S_n, Hi.
Qed.

(** * SF_seq for Chasles *)

Fixpoint seq_cut_down (s : seq (R*R)) (x : R) : seq (R*R) :=
  match s with
    | [::] => [:: (x,x)]
    | h :: t =>
        match Rle_dec (fst h) x with
          | right _ => [:: (x,Rmin (snd h) x)]
          | left _ => h :: (seq_cut_down t x)
        end
  end.
Fixpoint seq_cut_up (s : seq (R*R)) (x : R) : seq (R*R) :=
  match s with
    | [::] => [:: (x,x)]
    | h :: t =>
        match Rle_dec (fst h) x with
          | right _ => (x,x)::(fst h,Rmax (snd h) x)::t
          | left _ => seq_cut_up t x
        end
  end.

Definition SF_cut_down (sf : @SF_seq R) (x : R) :=
  let s := seq_cut_down ((SF_h sf,SF_h sf) :: (SF_t sf)) x in
  mkSF_seq (fst (head (SF_h sf,SF_h sf) s)) (behead s).
Definition SF_cut_up (sf : @SF_seq R) (x : R) :=
  let s := seq_cut_up ((SF_h sf,SF_h sf) :: (SF_t sf)) x in
  mkSF_seq (fst (head (SF_h sf,SF_h sf) s)) (behead s).

Lemma SF_cut_down_step s x eps :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  SF_h s <= x <= last (SF_h s) (SF_lx s) ->
  seq_step (SF_lx s) < eps
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
    -> seq_step (SF_lx (SF_cut_down s x)) < eps.
Proof.
(* Goal: forall (_ : and (Rle (@SF_h R s) x) (Rle x (@last R (@SF_h R s) (@SF_lx R s)))) (_ : Rlt (seq_step (@SF_lx R s)) eps), Rlt (seq_step (@SF_lx R (SF_cut_up s x))) eps *)
  unfold SF_cut_down, seq_step ; simpl.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case => Hh Hl.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Rle_dec => //= _.
  move: Hh Hl ;
  apply SF_cons_ind with (s := s) => {s} [ x1 | [x1 y0] s IH ] /= Hx Hh Hl.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite (Rle_antisym _ _ Hx Hh) Rminus_eq_0 Rabs_R0.
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite /Rmax ; by case: Rle_dec.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Rle_dec => //= Hx'.
(* Goal: Rle (Rmin a b) (Rmax a b) *)
(* Goal: @ex (@SF_seq R) (fun x : @SF_seq R => P x) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  apply Rmax_case.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  apply Rle_lt_trans with (2 := Hl) ; by apply Rmax_l.
(* Goal: Rlt (@foldr R R Rmax (IZR Z0) (@pairmap R R (fun x y : R => Rabs (Rminus y x)) (@SF_h R s) (@unzip1 R R (seq_cut_down (@SF_t R s) x)))) eps *)
(* Goal: Rlt (Rmax (Rabs (Rminus x x1)) (IZR Z0)) eps *)
  apply IH ; try assumption.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  apply Rle_lt_trans with (2 := Hl) ; by apply Rmax_r.
(* Goal: Rlt (Rmax (Rabs (Rminus x x1)) (IZR Z0)) eps *)
  apply Rle_lt_trans with (2 := Hl).
  apply Rmax_case ;
(* Goal: Rle (Rabs (Rminus (@SF_h R s) x)) (Rmax (Rabs (Rminus (@SF_h R s) x1)) (@foldr R R Rmax (IZR Z0) (@pairmap R R (fun x y : R => Rabs (Rminus y x)) (@SF_h R s) (@unzip1 R R (@SF_t R s))))) *)
(* Goal: Rle (@foldr R R Rmax (IZR Z0) (@pairmap R R (fun x y : R => Rabs (Rminus y x)) (@SF_h R s) (@unzip1 R R (@SF_t R s)))) (Rmax (Rabs (Rminus (@SF_h R s) x1)) (@foldr R R Rmax (IZR Z0) (@pairmap R R (fun x y : R => Rabs (Rminus y x)) (@SF_h R s) (@unzip1 R R (@SF_t R s))))) *)
  apply Rle_trans with (2 := Rmax_l _ _).
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite ?Rabs_pos_eq.
(* Goal: Rle (Rminus x x1) (Rminus (@SF_h R s) x1) *)
(* Goal: Rle (IZR Z0) (Rminus (@SF_h R s) x1) *)
(* Goal: Rle (IZR Z0) (Rminus x x1) *)
(* Goal: Rle (IZR Z0) (Rabs (Rminus (@SF_h R s) x1)) *)
  apply Rplus_le_compat_r.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply Rlt_le, Rnot_le_lt.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite -Rminus_le_0.
(* Goal: Rle x1 (@SF_h R s) *)
(* Goal: Rle (IZR Z0) (Rminus x x1) *)
(* Goal: Rle (IZR Z0) (Rabs (Rminus (@SF_h R s) x1)) *)
  apply Rle_trans with x.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by [].
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply Rlt_le, Rnot_le_lt.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by rewrite -Rminus_le_0.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply Rabs_pos.
Qed.
Lemma SF_cut_up_step s x eps :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  SF_h s <= x <= last (SF_h s) (SF_lx s) ->
  seq_step (SF_lx s) < eps
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
    -> seq_step (SF_lx (SF_cut_up s x)) < eps.
Proof.
(* Goal: forall (_ : and (Rle (@SF_h R s) x) (Rle x (@last R (@SF_h R s) (@SF_lx R s)))) (_ : Rlt (seq_step (@SF_lx R s)) eps), Rlt (seq_step (@SF_lx R (SF_cut_up s x))) eps *)
  unfold SF_cut_down, seq_step ; simpl.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case => Hh Hl.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Rle_dec => //= _.
  move: {4 5}(SF_h s) Hh Hl ;
  apply SF_cons_ind with (s := s) => {s} [ x1 | [x1 y0] s IH ] /= x0 Hh Hl He.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply He.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Rle_dec => //= Hx.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  apply (IH x0) => //=.
(* Goal: Rlt (Rmax (Rabs (Rminus (@SF_h R s) x)) (@foldr R R Rmax (IZR Z0) (@pairmap R R (fun x y : R => Rabs (Rminus y x)) (@SF_h R s) (@unzip1 R R (@SF_t R s))))) eps *)
  apply Rle_lt_trans with (2 := He).
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply Rmax_r.
(* Goal: Rlt (Rmax (Rabs (Rminus (@SF_h R s) x)) (@foldr R R Rmax (IZR Z0) (@pairmap R R (fun x y : R => Rabs (Rminus y x)) (@SF_h R s) (@unzip1 R R (@SF_t R s))))) eps *)
  apply Rle_lt_trans with (2 := He).
(* Goal: Rle (Rmax (Rabs (Rminus (@SF_h R s) x)) (@foldr R R Rmax (IZR Z0) (@pairmap R R (fun x y : R => Rabs (Rminus y x)) (@SF_h R s) (@unzip1 R R (@SF_t R s))))) (Rmax (Rabs (Rminus (@SF_h R s) x1)) (@foldr R R Rmax (IZR Z0) (@pairmap R R (fun x y : R => Rabs (Rminus y x)) (@SF_h R s) (@unzip1 R R (@SF_t R s))))) *)
  apply Rnot_le_lt in Hx.
(* Goal: Rle (Rmin a b) (Rmax a b) *)
(* Goal: @ex (@SF_seq R) (fun x : @SF_seq R => P x) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  apply Rmax_case.
(* Goal: Rle (Rabs (Rminus (@SF_h R s) x)) (Rmax (Rabs (Rminus (@SF_h R s) x1)) (@foldr R R Rmax (IZR Z0) (@pairmap R R (fun x y : R => Rabs (Rminus y x)) (@SF_h R s) (@unzip1 R R (@SF_t R s))))) *)
(* Goal: Rle (@foldr R R Rmax (IZR Z0) (@pairmap R R (fun x y : R => Rabs (Rminus y x)) (@SF_h R s) (@unzip1 R R (@SF_t R s)))) (Rmax (Rabs (Rminus (@SF_h R s) x1)) (@foldr R R Rmax (IZR Z0) (@pairmap R R (fun x y : R => Rabs (Rminus y x)) (@SF_h R s) (@unzip1 R R (@SF_t R s))))) *)
  apply Rle_trans with (2 := Rmax_l _ _).
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite ?Rabs_pos_eq.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply Rplus_le_compat_l, Ropp_le_contravar.
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite -Rminus_le_0 ; by apply Rlt_le, Rle_lt_trans with x.
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite -Rminus_le_0 ; by apply Rlt_le.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply Rmax_r.
Qed.

Lemma SF_cut_down_pointed s x :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  SF_h s <= x -> pointed_subdiv s
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  -> pointed_subdiv (SF_cut_down s x).
Proof.
(* Goal: forall _ : Rle x (@last R (@SF_h R s) (@SF_lx R s)), @eq R (@last R (@SF_h R (SF_cut_up s x)) (@SF_lx R (SF_cut_up s x))) (@last R (@SF_h R s) (@SF_lx R s)) *)
  unfold SF_cut_down ; simpl.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Rle_dec => //= _.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  apply SF_cons_ind with (s := s) => {s} [x0 | [x1 y1] s IH] /= Hx0 H.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  move => i /= Hi.
(* Goal: and (Rle (@nth R (IZR Z0) (@SF_lx R (@mkSF_seq R x1 (@Datatypes.cons (prod R R) (@pair R R x (Rmin y1 x)) (@Datatypes.nil (prod R R))))) i) (@nth R (IZR Z0) (@SF_ly R (@mkSF_seq R x1 (@Datatypes.cons (prod R R) (@pair R R x (Rmin y1 x)) (@Datatypes.nil (prod R R))))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@mkSF_seq R x1 (@Datatypes.cons (prod R R) (@pair R R x (Rmin y1 x)) (@Datatypes.nil (prod R R))))) i) (@nth R (IZR Z0) (@Datatypes.cons R x (@Datatypes.nil R)) i)) *)
  unfold SF_size in Hi ; simpl in Hi.
(* Goal: and (Rle (@nth R (IZR Z0) (@SF_lx R (@mkSF_seq R x1 (@Datatypes.cons (prod R R) (@pair R R x (Rmin y1 x)) (@Datatypes.nil (prod R R))))) i) (@nth R (IZR Z0) (@SF_ly R (@mkSF_seq R x1 (@Datatypes.cons (prod R R) (@pair R R x (Rmin y1 x)) (@Datatypes.nil (prod R R))))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@mkSF_seq R x1 (@Datatypes.cons (prod R R) (@pair R R x (Rmin y1 x)) (@Datatypes.nil (prod R R))))) i) (@nth R (IZR Z0) (@Datatypes.cons R x (@Datatypes.nil R)) i)) *)
  apply lt_n_Sm_le, le_n_O_eq in Hi.
(* Goal: and (Rle u u) (Rle u v) *)
(* Goal: Rle (Rmin a b) (Rmax a b) *)
(* Goal: P (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n)) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  rewrite -Hi ; simpl ; split.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by [].
(* Goal: Rle (@SF_h R s) (@SF_h R s) *)
(* Goal: Rle (@SF_h R s) (@nth R (IZR Z0) (@unzip1 R R (@map (prod R R) (prod R R) (fun x : prod R R => @pair R R (@fst R R x) (f (@snd R R x))) (@SF_t R s))) i) *)
  by apply Rle_refl.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Rle_dec => //= Hx1.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  move: (H O (lt_O_Sn _)) => /= H0.
(* Goal: pointed_subdiv (@mkSF_seq R x1 (@Datatypes.cons (prod R R) (@pair R R (@SF_h R s) y1) (seq_cut_down (@SF_t R s) x))) *)
(* Goal: pointed_subdiv (@mkSF_seq R x1 (@Datatypes.cons (prod R R) (@pair R R x (Rmin y1 x)) (@Datatypes.nil (prod R R)))) *)
  apply ptd_cons in H.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  move: (IH Hx1 H) => {IH} IH.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  rewrite /pointed_subdiv => i.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  destruct i => /= Hi.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply H0.
(* Goal: and (Rle (@nth R (IZR Z0) (@Datatypes.cons R (@SF_h R s) (@unzip1 R R (seq_cut_down (@SF_t R s) x))) i) (@nth R (IZR Z0) (@unzip2 R R (seq_cut_down (@SF_t R s) x)) i)) (Rle (@nth R (IZR Z0) (@unzip2 R R (seq_cut_down (@SF_t R s) x)) i) (@nth R (IZR Z0) (@unzip1 R R (seq_cut_down (@SF_t R s) x)) i)) *)
(* Goal: pointed_subdiv (@mkSF_seq R x1 (@Datatypes.cons (prod R R) (@pair R R x (Rmin y1 x)) (@Datatypes.nil (prod R R)))) *)
  apply (IH i).
(* Goal: lt (S (S i)) (@size R (@Datatypes.cons R h1 (@Datatypes.cons R h2 s))) *)
(* Goal: @sorted R Rle (@Datatypes.cons R h0 (@Datatypes.cons R h1 (@Datatypes.cons R h2 s))) *)
(* Goal: @eq T (@nth T y0 (@SF_ly T (@SF_cons T h0 s)) (@size (prod R T) (@SF_t T s))) (@nth T y0 (@SF_ly T (@SF_cons T h (@SF_cons T h0 s))) j) *)
  apply lt_S_n, Hi.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  move => i /= Hi.
(* Goal: and (Rle (@nth R (IZR Z0) (@SF_lx R (@mkSF_seq R x1 (@Datatypes.cons (prod R R) (@pair R R x (Rmin y1 x)) (@Datatypes.nil (prod R R))))) i) (@nth R (IZR Z0) (@SF_ly R (@mkSF_seq R x1 (@Datatypes.cons (prod R R) (@pair R R x (Rmin y1 x)) (@Datatypes.nil (prod R R))))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@mkSF_seq R x1 (@Datatypes.cons (prod R R) (@pair R R x (Rmin y1 x)) (@Datatypes.nil (prod R R))))) i) (@nth R (IZR Z0) (@Datatypes.cons R x (@Datatypes.nil R)) i)) *)
  unfold SF_size in Hi ; simpl in Hi.
(* Goal: and (Rle (@nth R (IZR Z0) (@SF_lx R (@mkSF_seq R x1 (@Datatypes.cons (prod R R) (@pair R R x (Rmin y1 x)) (@Datatypes.nil (prod R R))))) i) (@nth R (IZR Z0) (@SF_ly R (@mkSF_seq R x1 (@Datatypes.cons (prod R R) (@pair R R x (Rmin y1 x)) (@Datatypes.nil (prod R R))))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@mkSF_seq R x1 (@Datatypes.cons (prod R R) (@pair R R x (Rmin y1 x)) (@Datatypes.nil (prod R R))))) i) (@nth R (IZR Z0) (@Datatypes.cons R x (@Datatypes.nil R)) i)) *)
  apply lt_n_Sm_le, le_n_O_eq in Hi.
(* Goal: and (Rle u u) (Rle u v) *)
(* Goal: Rle (Rmin a b) (Rmax a b) *)
(* Goal: P (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n)) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  rewrite -Hi ; simpl ; split.
(* Goal: Rle x1 (Rmin y1 x) *)
(* Goal: Rle (Rmin y1 x) x *)
  apply Rmin_case.
(* Goal: Rle x1 y1 *)
(* Goal: Rle x1 x *)
(* Goal: Rle (Rmin y1 x) x *)
  apply (H O).
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply lt_O_Sn.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by [].
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply Rmin_r.
Qed.

Lemma SF_cut_up_pointed s x :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  SF_h s <= x ->
  pointed_subdiv s
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  -> pointed_subdiv (SF_cut_up s x).
Proof.
(* Goal: @eq R (@SF_h R (SF_cut_up s x)) x *)
  unfold SF_cut_up ; simpl.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Rle_dec => //= _.
  move: {2 3}(SF_h s) ;
  apply SF_cons_ind with (s := s) => {s} [ x1 | [x1 y0] s IH] /= x0 Hx0 H i Hi.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply lt_n_O in Hi.
(* Goal: and (Rle (@nth R (IZR Z0) (@SF_lx R (@mkSF_seq R (@fst R R (@head (prod R R) (@pair R R x0 x0) (if Rle_dec (@SF_h R s) x then seq_cut_up (@SF_t R s) x else @Datatypes.cons (prod R R) (@pair R R x x) (@Datatypes.cons (prod R R) (@pair R R (@SF_h R s) (Rmax y0 x)) (@SF_t R s))))) (@behead (prod R R) (if Rle_dec (@SF_h R s) x then seq_cut_up (@SF_t R s) x else @Datatypes.cons (prod R R) (@pair R R x x) (@Datatypes.cons (prod R R) (@pair R R (@SF_h R s) (Rmax y0 x)) (@SF_t R s)))))) i) (@nth R (IZR Z0) (@SF_ly R (@mkSF_seq R (@fst R R (@head (prod R R) (@pair R R x0 x0) (if Rle_dec (@SF_h R s) x then seq_cut_up (@SF_t R s) x else @Datatypes.cons (prod R R) (@pair R R x x) (@Datatypes.cons (prod R R) (@pair R R (@SF_h R s) (Rmax y0 x)) (@SF_t R s))))) (@behead (prod R R) (if Rle_dec (@SF_h R s) x then seq_cut_up (@SF_t R s) x else @Datatypes.cons (prod R R) (@pair R R x x) (@Datatypes.cons (prod R R) (@pair R R (@SF_h R s) (Rmax y0 x)) (@SF_t R s)))))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@mkSF_seq R (@fst R R (@head (prod R R) (@pair R R x0 x0) (if Rle_dec (@SF_h R s) x then seq_cut_up (@SF_t R s) x else @Datatypes.cons (prod R R) (@pair R R x x) (@Datatypes.cons (prod R R) (@pair R R (@SF_h R s) (Rmax y0 x)) (@SF_t R s))))) (@behead (prod R R) (if Rle_dec (@SF_h R s) x then seq_cut_up (@SF_t R s) x else @Datatypes.cons (prod R R) (@pair R R x x) (@Datatypes.cons (prod R R) (@pair R R (@SF_h R s) (Rmax y0 x)) (@SF_t R s)))))) i) (@nth R (IZR Z0) (@SF_lx R (@mkSF_seq R (@fst R R (@head (prod R R) (@pair R R x0 x0) (if Rle_dec (@SF_h R s) x then seq_cut_up (@SF_t R s) x else @Datatypes.cons (prod R R) (@pair R R x x) (@Datatypes.cons (prod R R) (@pair R R (@SF_h R s) (Rmax y0 x)) (@SF_t R s))))) (@behead (prod R R) (if Rle_dec (@SF_h R s) x then seq_cut_up (@SF_t R s) x else @Datatypes.cons (prod R R) (@pair R R x x) (@Datatypes.cons (prod R R) (@pair R R (@SF_h R s) (Rmax y0 x)) (@SF_t R s)))))) (S i))) *)
  destruct (Rle_dec (SF_h s) x) as [Hx1|Hx1].
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  apply IH => //=.
(* Goal: forall x : R, @eq R (@fe a b (SF_inf_r f a b n) x) (@fe b a (SF_inf_r f b a n) x) *)
  move: H ; by apply ptd_cons.
(* Goal: and (Rle (@nth R (IZR Z0) (@SF_lx R (@mkSF_seq R (@fst R R (@head (prod R R) (@pair R R x0 x0) (@Datatypes.cons (prod R R) (@pair R R x x) (@Datatypes.cons (prod R R) (@pair R R (@SF_h R s) (Rmax y0 x)) (@SF_t R s))))) (@behead (prod R R) (@Datatypes.cons (prod R R) (@pair R R x x) (@Datatypes.cons (prod R R) (@pair R R (@SF_h R s) (Rmax y0 x)) (@SF_t R s)))))) i) (@nth R (IZR Z0) (@SF_ly R (@mkSF_seq R (@fst R R (@head (prod R R) (@pair R R x0 x0) (@Datatypes.cons (prod R R) (@pair R R x x) (@Datatypes.cons (prod R R) (@pair R R (@SF_h R s) (Rmax y0 x)) (@SF_t R s))))) (@behead (prod R R) (@Datatypes.cons (prod R R) (@pair R R x x) (@Datatypes.cons (prod R R) (@pair R R (@SF_h R s) (Rmax y0 x)) (@SF_t R s)))))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@mkSF_seq R (@fst R R (@head (prod R R) (@pair R R x0 x0) (@Datatypes.cons (prod R R) (@pair R R x x) (@Datatypes.cons (prod R R) (@pair R R (@SF_h R s) (Rmax y0 x)) (@SF_t R s))))) (@behead (prod R R) (@Datatypes.cons (prod R R) (@pair R R x x) (@Datatypes.cons (prod R R) (@pair R R (@SF_h R s) (Rmax y0 x)) (@SF_t R s)))))) i) (@nth R (IZR Z0) (@SF_lx R (@mkSF_seq R (@fst R R (@head (prod R R) (@pair R R x0 x0) (@Datatypes.cons (prod R R) (@pair R R x x) (@Datatypes.cons (prod R R) (@pair R R (@SF_h R s) (Rmax y0 x)) (@SF_t R s))))) (@behead (prod R R) (@Datatypes.cons (prod R R) (@pair R R x x) (@Datatypes.cons (prod R R) (@pair R R (@SF_h R s) (Rmax y0 x)) (@SF_t R s)))))) (S i))) *)
  destruct i ; simpl.
(* Goal: and (Rle u u) (Rle u v) *)
(* Goal: Rle (Rmin a b) (Rmax a b) *)
(* Goal: P (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n)) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  split.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply Rmax_r.
(* Goal: Rle (Rmin a b) (Rmax a b) *)
(* Goal: @ex (@SF_seq R) (fun x : @SF_seq R => P x) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  apply Rmax_case.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply (H O), lt_O_Sn.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply Rlt_le, Rnot_le_lt.
(* Goal: and (Rle (@nth R (IZR Z0) (@Datatypes.cons R (@SF_h R s) (@unzip1 R R (@SF_t R s))) i) (@nth R (IZR Z0) (@unzip2 R R (@SF_t R s)) i)) (Rle (@nth R (IZR Z0) (@unzip2 R R (@SF_t R s)) i) (@nth R (IZR Z0) (@unzip1 R R (@SF_t R s)) i)) *)
  apply (H (S i)), Hi.
Qed.
Lemma SF_cut_down_h s x :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  SF_h s <= x -> SF_h (SF_cut_down s x) = SF_h s.
Proof.
(* Goal: forall _ : Rle x (@last R (@SF_h R s) (@SF_lx R s)), @eq R (@last R (@SF_h R (SF_cut_up s x)) (@SF_lx R (SF_cut_up s x))) (@last R (@SF_h R s) (@SF_lx R s)) *)
  unfold SF_cut_down ; simpl.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by case: Rle_dec.
Qed.
Lemma SF_cut_up_h s x :
  SF_h (SF_cut_up s x) = x.
Proof.
(* Goal: @eq R (@SF_h R (SF_cut_up s x)) x *)
  unfold SF_cut_up ; simpl.
(* Goal: @eq R (@last R (@fst R R (@head (prod R R) (@pair R R (@SF_h R s) (@SF_h R s)) (if Rle_dec (@SF_h R s) x then @Datatypes.cons (prod R R) (@pair R R (@SF_h R s) (@SF_h R s)) (seq_cut_down (@SF_t R s) x) else @Datatypes.cons (prod R R) (@pair R R x (Rmin (@SF_h R s) x)) (@Datatypes.nil (prod R R))))) (@unzip1 R R (@behead (prod R R) (if Rle_dec (@SF_h R s) x then @Datatypes.cons (prod R R) (@pair R R (@SF_h R s) (@SF_h R s)) (seq_cut_down (@SF_t R s) x) else @Datatypes.cons (prod R R) (@pair R R x (Rmin (@SF_h R s) x)) (@Datatypes.nil (prod R R)))))) x *)
  case: Rle_dec => //= ; simpl.
  move: {2 3}(SF_h s) ;
  apply SF_cons_ind with (s := s) => {s} [x1 | [x1 y1] s IH ] /= x0 Hx.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by [].
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Rle_dec => //= Hx'.
(* Goal: Rle (@Riemann_sum R_ModuleSpace f s) (@Riemann_sum R_ModuleSpace g s) *)
  by apply IH.
Qed.
Lemma SF_cut_down_l s x :
  last (SF_h (SF_cut_down s x)) (SF_lx (SF_cut_down s x)) = x.
Proof.
(* Goal: forall _ : Rle x (@last R (@SF_h R s) (@SF_lx R s)), @eq R (@last R (@SF_h R (SF_cut_up s x)) (@SF_lx R (SF_cut_up s x))) (@last R (@SF_h R s) (@SF_lx R s)) *)
  unfold SF_cut_down ; simpl.
(* Goal: @eq R (@last R (@fst R R (@head (prod R R) (@pair R R (@SF_h R s) (@SF_h R s)) (if Rle_dec (@SF_h R s) x then @Datatypes.cons (prod R R) (@pair R R (@SF_h R s) (@SF_h R s)) (seq_cut_down (@SF_t R s) x) else @Datatypes.cons (prod R R) (@pair R R x (Rmin (@SF_h R s) x)) (@Datatypes.nil (prod R R))))) (@unzip1 R R (@behead (prod R R) (if Rle_dec (@SF_h R s) x then @Datatypes.cons (prod R R) (@pair R R (@SF_h R s) (@SF_h R s)) (seq_cut_down (@SF_t R s) x) else @Datatypes.cons (prod R R) (@pair R R x (Rmin (@SF_h R s) x)) (@Datatypes.nil (prod R R)))))) x *)
  case: Rle_dec => //= ; simpl.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  apply SF_cons_ind with (s := s) => {s} [x1 | [x1 y1] s IH ] /= Hx.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by [].
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Rle_dec => //= Hx'.
Qed.
Lemma SF_cut_up_l s x :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  x <= last (SF_h s) (SF_lx s) ->
  last (SF_h (SF_cut_up s x)) (SF_lx (SF_cut_up s x)) = last (SF_h s) (SF_lx s).
Proof.
(* Goal: forall _ : Rle x (@last R (@SF_h R s) (@SF_lx R s)), @eq R (@last R (@SF_h R (SF_cut_up s x)) (@SF_lx R (SF_cut_up s x))) (@last R (@SF_h R s) (@SF_lx R s)) *)
  unfold SF_cut_down ; simpl.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Rle_dec => //=.
  move: {3 4}(SF_h s);
  apply SF_cons_ind with (s := s) => {s} [x1 | [x1 y1] s IH ] /= x0 Hx Hx'.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply Rle_antisym.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Rle_dec => //= {Hx} Hx.
(* Goal: Rle (@Riemann_sum R_ModuleSpace f s) (@Riemann_sum R_ModuleSpace g s) *)
  by apply IH.
Qed.

Lemma SF_cut_down_cons_0 h ptd x :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  x < fst h -> SF_cut_down (SF_cons h ptd) x = SF_nil x.
Proof.
(* Goal: forall _ : Rlt x (@fst R R h), @eq (@SF_seq R) (SF_cut_up (@SF_cons R h ptd) x) (@SF_cons R (@pair R R x (Rmax (@fst R R h) x)) (@SF_cons R h ptd)) *)
  intros H0.
(* Goal: @eq (@SF_seq R) (SF_cut_up (@SF_cons R h ptd) x) (@SF_cons R (@pair R R x (Rmax (@fst R R h) x)) (@SF_cons R h ptd)) *)
  apply Rlt_not_le in H0.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite /SF_cut_down /=.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by case: Rle_dec.
Qed.
Lemma SF_cut_up_cons_0 h ptd x :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  x < fst h -> SF_cut_up (SF_cons h ptd) x = SF_cons (x,Rmax (fst h) x) (SF_cons h ptd).
Proof.
(* Goal: forall _ : Rlt x (@fst R R h), @eq (@SF_seq R) (SF_cut_up (@SF_cons R h ptd) x) (@SF_cons R (@pair R R x (Rmax (@fst R R h) x)) (@SF_cons R h ptd)) *)
  intros H0.
(* Goal: @eq (@SF_seq R) (SF_cut_up (@SF_cons R h ptd) x) (@SF_cons R (@pair R R x (Rmax (@fst R R h) x)) (@SF_cons R h ptd)) *)
  apply Rlt_not_le in H0.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite /SF_cut_up /=.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by case: Rle_dec.
Qed.
Lemma SF_cut_down_cons_1 h ptd x :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  fst h <= x < SF_h ptd -> SF_cut_down (SF_cons h ptd) x = SF_cons (fst h, Rmin (snd h) x) (SF_nil x).
Proof.
(* Goal: forall _ : and (Rle (@fst R R h) (@SF_h R ptd)) (Rle (@SF_h R ptd) x), @eq (@SF_seq R) (SF_cut_up (@SF_cons R h ptd) x) (SF_cut_up ptd x) *)
  intros [H0 Hx0].
(* Goal: @eq (@SF_seq R) (SF_cut_up (@SF_cons R h ptd) x) (@SF_cons R (@pair R R x (Rmax (@snd R R h) x)) ptd) *)
  apply Rlt_not_le in Hx0.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite /SF_cut_down /=.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Rle_dec => //= _.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by case: Rle_dec.
Qed.
Lemma SF_cut_up_cons_1 h ptd x :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  fst h <= x < SF_h ptd -> SF_cut_up (SF_cons h ptd) x = SF_cons (x,Rmax (snd h) x) ptd.
Proof.
(* Goal: forall _ : and (Rle (@fst R R h) (@SF_h R ptd)) (Rle (@SF_h R ptd) x), @eq (@SF_seq R) (SF_cut_up (@SF_cons R h ptd) x) (SF_cut_up ptd x) *)
  intros [H0 Hx0].
(* Goal: @eq (@SF_seq R) (SF_cut_up (@SF_cons R h ptd) x) (@SF_cons R (@pair R R x (Rmax (@snd R R h) x)) ptd) *)
  apply Rlt_not_le in Hx0.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite /SF_cut_up /=.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Rle_dec => //= _.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by case: Rle_dec.
Qed.
Lemma SF_cut_down_cons_2 h ptd x :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  fst h <= SF_h ptd <= x -> SF_cut_down (SF_cons h ptd) x = SF_cons h (SF_cut_down ptd x).
Proof.
(* Goal: forall _ : and (Rle (@fst R R h) (@SF_h R ptd)) (Rle (@SF_h R ptd) x), @eq (@SF_seq R) (SF_cut_up (@SF_cons R h ptd) x) (SF_cut_up ptd x) *)
  intros [H0 Hx0].
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite /SF_cut_down /=.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Rle_dec (Rle_trans _ _ _ H0 Hx0) => //= _ _.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by case: Rle_dec.
Qed.
Lemma SF_cut_up_cons_2 h ptd x :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  fst h <= SF_h ptd <= x -> SF_cut_up (SF_cons h ptd) x = SF_cut_up ptd x.
Proof.
(* Goal: forall _ : and (Rle (@fst R R h) (@SF_h R ptd)) (Rle (@SF_h R ptd) x), @eq (@SF_seq R) (SF_cut_up (@SF_cons R h ptd) x) (SF_cut_up ptd x) *)
  intros [H0 Hx0].
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite /SF_cut_up /=.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Rle_dec (Rle_trans _ _ _ H0 Hx0) => //= _ _.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Rle_dec => //= _.
  move: {2 3}(SF_h ptd) Hx0 ;
  apply SF_cons_ind with (s := ptd) => {ptd H0} [ x0 | [x0 y0] ptd IH ] //= x0' Hx0.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Rle_dec => //= Hx1.
(* Goal: Rle (@Riemann_sum R_ModuleSpace f s) (@Riemann_sum R_ModuleSpace g s) *)
  by apply IH.
Qed.

(** * Definition of SF_fun *)

Section SF_fun.

Context {T : Type}.

Fixpoint SF_fun_aux (h : R*T) (s : seq (R*T)) (y0 : T) (x : R) :=
  match s with
    | [::] => match Rle_dec x (fst h) with
        | left _ => snd h
        | right _ => y0
      end
    | h0 :: s0 => match Rlt_dec x (fst h) with
        | left _ => snd h
        | right _ => SF_fun_aux h0 s0 y0 x
      end
  end.
Definition SF_fun (s : SF_seq) (y0 : T) (x : R) :=
  SF_fun_aux (SF_h s,y0) (SF_t s) y0 x.

Lemma SF_fun_incr (s : SF_seq) (y0 : T) (x : R) Hs Hx :
  SF_fun s y0 x =
  match (sorted_dec (SF_lx s) 0 x Hs Hx) with
    | inleft H => nth y0 (SF_ly s) (proj1_sig H)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
    | inright _ =>  nth y0 (SF_ly s) (SF_size s -1)%nat
  end.
Proof.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite /SF_fun /=.
(* s = SF_nil _ *)
(* Goal: @eq (ModuleSpace.sort R_Ring R_ModuleSpace) (@Riemann_sum R_ModuleSpace f s) (Int_SF (@subdivision_val (@head R (IZR Z0) (@SF_lx R (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s))) (@last R (IZR Z0) (@SF_lx R (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s))) (SF_compat_le (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s) (@SF_map_sort R (ModuleSpace.sort R_Ring R_ModuleSpace) f s Rle pr))) (@subdivision (@head R (IZR Z0) (@SF_lx R (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s))) (@last R (IZR Z0) (@SF_lx R (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s))) (SF_compat_le (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s) (@SF_map_sort R (ModuleSpace.sort R_Ring R_ModuleSpace) f s Rle pr)))) *)
(* Goal: @eq (ModuleSpace.sort R_Ring R_ModuleSpace) (@Riemann_sum R_ModuleSpace f s) (Ropp (Int_SF (@subdivision_val (@head R (IZR Z0) (@SF_lx R (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s))) (@last R (IZR Z0) (@SF_lx R (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s))) (SF_compat_le (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s) (@SF_map_sort R (ModuleSpace.sort R_Ring R_ModuleSpace) f s Rle pr))) (@subdivision (@head R (IZR Z0) (@SF_lx R (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s))) (@last R (IZR Z0) (@SF_lx R (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s))) (SF_compat_le (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s) (@SF_map_sort R (ModuleSpace.sort R_Ring R_ModuleSpace) f s Rle pr))))) *)
  move: Hs Hx ; apply SF_cons_dec with (s := s) => {s} [/= x1 | h s] Hs /= Hx.
    case: sorted_dec => /= [[i Hi]|Hi] ; rewrite /SF_ly ; case: Rle_dec => //= ;
    case: i Hi => //.
(* s = SF_cons _ (SF_nil _) *)
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Rlt_dec => [Hx' | _].
(* Goal: @eq T y0 match sorted_dec (@SF_lx T (@SF_cons T h s)) (IZR Z0) x Hs Hx with | inleft H => @nth T y0 (@SF_ly T (@SF_cons T h s)) (@proj1_sig nat (fun i : nat => and (and (Rle (@nth R (IZR Z0) (@SF_lx T (@SF_cons T h s)) i) x) (Rlt x (@nth R (IZR Z0) (@Datatypes.cons R (@SF_h T s) (@unzip1 R T (@SF_t T s))) i))) (lt (S (S i)) (S (S (@size R (@unzip1 R T (@SF_t T s))))))) H) | inright a => @nth T y0 (@SF_ly T (@SF_cons T h s)) (Init.Nat.sub (@size (prod R T) (@SF_t T s)) O) end *)
(* Goal: @eq T (SF_fun_aux (@pair R T (@SF_h T s) (@snd R T h)) (@SF_t T s) y0 x) match sorted_dec (@SF_lx T (@SF_cons T h s)) (IZR Z0) x Hs Hx with | inleft H => @nth T y0 (@SF_ly T (@SF_cons T h s)) (@proj1_sig nat (fun i : nat => and (and (Rle (@nth R (IZR Z0) (@SF_lx T (@SF_cons T h s)) i) x) (Rlt x (@nth R (IZR Z0) (@Datatypes.cons R (@SF_h T s) (@unzip1 R T (@SF_t T s))) i))) (lt (S (S i)) (S (S (@size R (@unzip1 R T (@SF_t T s))))))) H) | inright a => @nth T y0 (@SF_ly T (@SF_cons T h s)) (Init.Nat.sub (@size (prod R T) (@SF_t T s)) O) end *)
  contradict Hx' ; apply Rle_not_lt, Hx.
(* Goal: @eq (ModuleSpace.sort R_Ring R_ModuleSpace) (@Riemann_sum R_ModuleSpace f s) (Int_SF (@subdivision_val (@head R (IZR Z0) (@SF_lx R (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s))) (@last R (IZR Z0) (@SF_lx R (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s))) (SF_compat_le (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s) (@SF_map_sort R (ModuleSpace.sort R_Ring R_ModuleSpace) f s Rle pr))) (@subdivision (@head R (IZR Z0) (@SF_lx R (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s))) (@last R (IZR Z0) (@SF_lx R (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s))) (SF_compat_le (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s) (@SF_map_sort R (ModuleSpace.sort R_Ring R_ModuleSpace) f s Rle pr)))) *)
(* Goal: @eq (ModuleSpace.sort R_Ring R_ModuleSpace) (@Riemann_sum R_ModuleSpace f s) (Ropp (Int_SF (@subdivision_val (@head R (IZR Z0) (@SF_lx R (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s))) (@last R (IZR Z0) (@SF_lx R (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s))) (SF_compat_le (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s) (@SF_map_sort R (ModuleSpace.sort R_Ring R_ModuleSpace) f s Rle pr))) (@subdivision (@head R (IZR Z0) (@SF_lx R (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s))) (@last R (IZR Z0) (@SF_lx R (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s))) (SF_compat_le (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s) (@SF_map_sort R (ModuleSpace.sort R_Ring R_ModuleSpace) f s Rle pr))))) *)
  move: h Hs Hx ; apply SF_cons_ind with (s := s) => {s} [x1 | h0 s IH] h Hs /= Hx.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    case: sorted_dec => [/= [i [Hi' Hi]] /= |Hi].
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    by apply lt_S_n, lt_S_n, lt_n_O in Hi.
(* Goal: @eq T (if Rle_dec x x1 then @snd R T h else y0) (@snd R T h) *)
(* Goal: @eq T (if Rlt_dec x (@fst R T h0) then @snd R T h else SF_fun_aux (@pair R T (@SF_h T s) (@snd R T h0)) (@SF_t T s) y0 x) match sorted_dec (@SF_lx T (@SF_cons T h (@SF_cons T h0 s))) (IZR Z0) x Hs Hx with | inleft H => @nth T y0 (@SF_ly T (@SF_cons T h (@SF_cons T h0 s))) (@proj1_sig nat (fun i : nat => and (and (Rle (@nth R (IZR Z0) (@SF_lx T (@SF_cons T h (@SF_cons T h0 s))) i) x) (Rlt x (@nth R (IZR Z0) (@Datatypes.cons R (@fst R T h0) (@Datatypes.cons R (@SF_h T s) (@unzip1 R T (@SF_t T s)))) i))) (lt (S (S i)) (S (S (S (@size R (@unzip1 R T (@SF_t T s)))))))) H) | inright a => @nth T y0 (@Datatypes.cons T (@snd R T h0) (@unzip2 R T (@SF_t T s))) (@size (prod R T) (@SF_t T s)) end *)
  case: Hx => Hx Hx' ; apply Rle_not_lt in Hx ; case: Rle_dec => //.
(* s = SF_cons _ (SF_cons _ _) *)
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Rlt_dec => Hx'.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: sorted_dec => /= [[i Hi]|Hi]/=.
  case: i Hi => //= i Hi ; contradict Hx' ;
  apply Rle_not_lt, Rle_trans with (2 := proj1 (proj1 Hi)).
  simpl in Hs ; elim: (unzip1 (SF_t s)) (fst h0) (SF_h s) (i) (proj2 Hs) (proj2 Hi)
    => {s IH Hs Hx Hi h h0} [| h1 s IH] h h0 n Hs Hn.
(* Goal: Rle h (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.nil R))) n) *)
(* Goal: Rle h (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s))) n) *)
(* Goal: @eq T (@snd R T h) (@nth T y0 (@Datatypes.cons T (@snd R T h0) (@unzip2 R T (@SF_t T s))) (@size (prod R T) (@SF_t T s))) *)
(* Goal: @eq T (SF_fun_aux (@pair R T (@SF_h T s) (@snd R T h0)) (@SF_t T s) y0 x) match sorted_dec (@SF_lx T (@SF_cons T h (@SF_cons T h0 s))) (IZR Z0) x Hs Hx with | inleft H => @nth T y0 (@SF_ly T (@SF_cons T h (@SF_cons T h0 s))) (@proj1_sig nat (fun i : nat => and (and (Rle (@nth R (IZR Z0) (@SF_lx T (@SF_cons T h (@SF_cons T h0 s))) i) x) (Rlt x (@nth R (IZR Z0) (@Datatypes.cons R (@fst R T h0) (@Datatypes.cons R (@SF_h T s) (@unzip1 R T (@SF_t T s)))) i))) (lt (S (S i)) (S (S (S (@size R (@unzip1 R T (@SF_t T s)))))))) H) | inright a => @nth T y0 (@Datatypes.cons T (@snd R T h0) (@unzip2 R T (@SF_t T s))) (@size (prod R T) (@SF_t T s)) end *)
    repeat apply lt_S_n in Hn ; by apply lt_n_O in Hn.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    case: n Hn => [| n] Hn.
(* Goal: Rle (@SF_h R s) (@SF_h R s) *)
(* Goal: Rle (@SF_h R s) (@nth R (IZR Z0) (@unzip1 R R (@map (prod R R) (prod R R) (fun x : prod R R => @pair R R (@fst R R x) (f (@snd R R x))) (@SF_t R s))) i) *)
    apply Rle_refl.
(* Goal: le i (Init.Nat.pred (@size R (@Datatypes.cons R h s))) *)
  apply Rle_trans with (1 := proj1 Hs) => //= ; intuition.
(* Goal: @eq T (@snd R T h) (@nth T y0 (@Datatypes.cons T (@snd R T h0) (@unzip2 R T (@SF_t T s))) (@size (prod R T) (@SF_t T s))) *)
(* Goal: @eq T (SF_fun_aux (@pair R T (@SF_h T s) (@snd R T h0)) (@SF_t T s) y0 x) match sorted_dec (@SF_lx T (@SF_cons T h (@SF_cons T h0 s))) (IZR Z0) x Hs Hx with | inleft H => @nth T y0 (@SF_ly T (@SF_cons T h (@SF_cons T h0 s))) (@proj1_sig nat (fun i : nat => and (and (Rle (@nth R (IZR Z0) (@SF_lx T (@SF_cons T h (@SF_cons T h0 s))) i) x) (Rlt x (@nth R (IZR Z0) (@Datatypes.cons R (@fst R T h0) (@Datatypes.cons R (@SF_h T s) (@unzip1 R T (@SF_t T s)))) i))) (lt (S (S i)) (S (S (S (@size R (@unzip1 R T (@SF_t T s)))))))) H) | inright a => @nth T y0 (@Datatypes.cons T (@snd R T h0) (@unzip2 R T (@SF_t T s))) (@size (prod R T) (@SF_t T s)) end *)
  contradict Hx' ; apply Rle_not_lt, Rle_trans with (2 := proj1 Hi).
  simpl in Hs ; elim: (unzip1 (SF_t s)) (fst h0) (SF_h s) (proj2 Hs)
    => {s IH Hs Hx Hi h h0} [| h1 s IH] h h0 Hs.
(* Goal: Rle (@SF_h R s) (@SF_h R s) *)
(* Goal: Rle (@SF_h R s) (@nth R (IZR Z0) (@unzip1 R R (@map (prod R R) (prod R R) (fun x : prod R R => @pair R R (@fst R R x) (f (@snd R R x))) (@SF_t R s))) i) *)
    apply Rle_refl.
(* Goal: le i (Init.Nat.pred (@size R (@Datatypes.cons R h s))) *)
    apply Rle_trans with (1 := proj1 Hs) => //= ; intuition.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  have : fst h0 <= x <= last (SF_h s) (unzip1 (SF_t s)) => [ | {Hx'} Hx'].
(* Goal: and (Rle (@fst R T h0) x) (Rle x (@last R (@SF_h T s) (@unzip1 R T (@SF_t T s)))) *)
(* Goal: @eq T (SF_fun_aux (@pair R T (@SF_h T s) (@snd R T h0)) (@SF_t T s) y0 x) match sorted_dec (@SF_lx T (@SF_cons T h (@SF_cons T h0 s))) (IZR Z0) x Hs Hx with | inleft H => @nth T y0 (@SF_ly T (@SF_cons T h (@SF_cons T h0 s))) (@proj1_sig nat (fun i : nat => and (and (Rle (@nth R (IZR Z0) (@SF_lx T (@SF_cons T h (@SF_cons T h0 s))) i) x) (Rlt x (@nth R (IZR Z0) (@Datatypes.cons R (@fst R T h0) (@Datatypes.cons R (@SF_h T s) (@unzip1 R T (@SF_t T s)))) i))) (lt (S (S i)) (S (S (S (@size R (@unzip1 R T (@SF_t T s)))))))) H) | inright a => @nth T y0 (@Datatypes.cons T (@snd R T h0) (@unzip2 R T (@SF_t T s))) (@size (prod R T) (@SF_t T s)) end *)
    split ; [by apply Rnot_lt_le | by apply Hx].
  rewrite (IH h0 (proj2 Hs) Hx') => {IH} ;
  case: sorted_dec => [[i [Hxi Hi]]|Hi] ; case: sorted_dec => [[j [Hxj Hj]]|Hj] ;
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  rewrite -?minus_n_O //=.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
(* i,j < size s - 2 *)
  move : h h0 i j Hs {Hx Hx'} Hxi Hi Hxj Hj ; apply SF_cons_ind with (s := s)
    => {s} [x1 | h1 s IH] h h0 i j Hs //= Hxi Hi Hxj Hj.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    by apply lt_S_n, lt_S_n, lt_n_O in Hi.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: j Hxj Hj => [/= | j] Hxj Hj.
(* Goal: @eq T (@nth T y0 (@SF_ly T (@SF_cons T h0 (@SF_cons T h1 s))) i) (@snd R T h) *)
(* Goal: @eq T (@nth T y0 (@SF_ly T (@SF_cons T h0 (@SF_cons T h1 s))) i) (@nth T y0 (@SF_ly T (@SF_cons T h (@SF_cons T h0 (@SF_cons T h1 s)))) (S j)) *)
(* Goal: @eq T (@nth T y0 (@SF_ly T (@SF_cons T h0 s)) i) (@nth T y0 (@Datatypes.cons T (@snd R T h0) (@unzip2 R T (@SF_t T s))) (@size (prod R T) (@SF_t T s))) *)
(* Goal: @eq T (@nth T y0 (@SF_ly T (@SF_cons T h0 s)) (@size (prod R T) (@SF_t T s))) (@nth T y0 (@SF_ly T (@SF_cons T h (@SF_cons T h0 s))) j) *)
  case: Hxj => _ Hxj ; contradict Hxj ; apply Rle_not_lt, Rle_trans with (2 := proj1 Hxi).
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) x) (Rlt x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i)) *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  elim: (i) Hi => {i Hxi IH} //= [| i IH] Hi.
(* Goal: Rle (@SF_h R s) (@SF_h R s) *)
(* Goal: Rle (@SF_h R s) (@nth R (IZR Z0) (@unzip1 R R (@map (prod R R) (prod R R) (fun x : prod R R => @pair R R (@fst R R x) (f (@snd R R x))) (@SF_t R s))) i) *)
  apply Rle_refl.
  apply Rle_trans with (1 := IH (lt_trans _ _ _ (lt_n_Sn _) Hi)), (sorted_nth Rle) ;
  [apply Hs | simpl ; intuition].
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: i Hxi Hi => [/= | i] Hxi Hi.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: j Hxj Hj => [//= | j] Hxj Hj.
  case: Hxi => _ Hxi ; contradict Hxi ;
  apply Rle_not_lt, Rle_trans with (2 := proj1 Hxj) ;
  elim: (j) Hj => {j Hxj IH} //= [| j IH] Hj.
(* Goal: Rle (@SF_h R s) (@SF_h R s) *)
(* Goal: Rle (@SF_h R s) (@nth R (IZR Z0) (@unzip1 R R (@map (prod R R) (prod R R) (fun x : prod R R => @pair R R (@fst R R x) (f (@snd R R x))) (@SF_t R s))) i) *)
  apply Rle_refl.
  apply Rle_trans with (1 := IH (lt_trans _ _ _ (lt_n_Sn _) Hj)), (sorted_nth Rle) ;
  [apply Hs | simpl ; intuition].
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  apply (IH h0 h1 i j) => //.
(* Goal: @SF_sorted R Rle (@SF_cons R h0 s) *)
(* Goal: lt i (S (@size R (@unzip1 R R (@SF_t R s)))) *)
  apply Hs.
(* Goal: lt (S (S i)) (@size R (@Datatypes.cons R h1 (@Datatypes.cons R h2 s))) *)
(* Goal: @sorted R Rle (@Datatypes.cons R h0 (@Datatypes.cons R h1 (@Datatypes.cons R h2 s))) *)
(* Goal: @eq T (@nth T y0 (@SF_ly T (@SF_cons T h0 s)) (@size (prod R T) (@SF_t T s))) (@nth T y0 (@SF_ly T (@SF_cons T h (@SF_cons T h0 s))) j) *)
  apply lt_S_n, Hi.
(* Goal: lt (S (S j)) (@size R (@Datatypes.cons R h0 (@Datatypes.cons R h1 (@Datatypes.cons R h2 s)))) *)
(* Goal: @sorted R Rle (@Datatypes.cons R h0 (@Datatypes.cons R h1 (@Datatypes.cons R h2 s))) *)
  apply lt_S_n, Hj.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
(* i < j = size s - 2 *)
  simpl in Hxi, Hj ; case: Hxi => _ Hxi ; contradict Hxi ;
  apply Rle_not_lt, Rle_trans with (2 := proj1 Hj).
(* Goal: forall _ : and (Rlt (Rmin a b) x) (Rlt x (Rmax a b)), Rbar_le (Inf_fct f a b) (Finite (f x)) *)
  move: Hi Hs ; rewrite ?SF_lx_cons /SF_lx.
  elim: i (fst h) (fst h0) (SF_h s) (unzip1 (SF_t s))
    => {s Hx Hx' Hj h y0 h0} [| i IH] h h0 h1 s Hi Hs.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    case: s Hi Hs => [| h2 s] Hi Hs /=.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    by apply lt_S_n, lt_S_n, lt_n_O in Hi.
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) x) (Rlt x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i)) *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
    elim: s h h0 h1 h2 {Hi} Hs => [| h3 s IH] h h0 h1 h2 Hs /=.
(* Goal: Rle (@SF_h R s) (@SF_h R s) *)
(* Goal: Rle (@SF_h R s) (@nth R (IZR Z0) (@unzip1 R R (@map (prod R R) (prod R R) (fun x : prod R R => @pair R R (@fst R R x) (f (@snd R R x))) (@SF_t R s))) i) *)
    apply Rle_refl.
(* Goal: Rle h1 (@nth R (IZR Z0) (@Datatypes.cons R h2 (@Datatypes.cons R h3 s)) (@size R s)) *)
(* Goal: Rle (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (S i)) (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) *)
(* Goal: @eq T (@nth T y0 (@SF_ly T (@SF_cons T h0 s)) (@size (prod R T) (@SF_t T s))) (@nth T y0 (@SF_ly T (@SF_cons T h (@SF_cons T h0 s))) j) *)
    apply Rle_trans with (r2 := h2).
(* Goal: @SF_sorted R Rle (@SF_cons R h0 s) *)
(* Goal: lt i (S (@size R (@unzip1 R R (@SF_t R s)))) *)
    apply Hs.
(* Goal: Rle h2 (@nth R (IZR Z0) (@Datatypes.cons R h2 (@Datatypes.cons R h3 s)) (@size R s)) *)
(* Goal: Rle (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (S i)) (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) *)
(* Goal: @eq T (@nth T y0 (@SF_ly T (@SF_cons T h0 s)) (@size (prod R T) (@SF_t T s))) (@nth T y0 (@SF_ly T (@SF_cons T h (@SF_cons T h0 s))) j) *)
    apply (IH h0 h1).
(* Goal: @sorted R Rle (@Datatypes.cons R h0 (@Datatypes.cons R h1 (@Datatypes.cons R h2 s))) *)
(* Goal: Rle (@nth R (IZR Z0) (@Datatypes.cons R h1 s) j) (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) *)
    apply (proj2 Hs).
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: s Hi Hs => [| h2 s] Hi Hs.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    by apply lt_S_n, lt_S_n, lt_n_O in Hi.
(* Goal: Rle (@nth R (IZR Z0) (@Datatypes.cons R h1 (@Datatypes.cons R h2 s)) j) (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 (@Datatypes.cons R h2 s))) (@size R (@Datatypes.cons R h2 s))) *)
  apply (IH h0 h1 h2 s).
(* Goal: lt (S (S i)) (@size R (@Datatypes.cons R h1 (@Datatypes.cons R h2 s))) *)
(* Goal: @sorted R Rle (@Datatypes.cons R h0 (@Datatypes.cons R h1 (@Datatypes.cons R h2 s))) *)
(* Goal: @eq T (@nth T y0 (@SF_ly T (@SF_cons T h0 s)) (@size (prod R T) (@SF_t T s))) (@nth T y0 (@SF_ly T (@SF_cons T h (@SF_cons T h0 s))) j) *)
  apply lt_S_n, Hi.
(* Goal: @SF_sorted R Rle (@SF_cons R h0 s) *)
(* Goal: lt i (S (@size R (@unzip1 R R (@SF_t R s)))) *)
  apply Hs.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
(* j < i = size s - 2 *)
  simpl in Hxj, Hi ; case: Hxj => _ Hxj ; contradict Hxj ;
  apply Rle_not_lt, Rle_trans with (2 := proj1 Hi).
(* Goal: forall _ : and (Rlt (Rmin a b) x) (Rlt x (Rmax a b)), Rbar_le (Inf_fct f a b) (Finite (f x)) *)
  move: Hj Hs ; rewrite ?SF_lx_cons /SF_lx.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  rewrite -minus_n_O ;
  elim: j (fst h) (fst h0) (SF_h s) (unzip1 (SF_t s))
    => {s Hx Hx' Hi h y0 h0} [ | j IH] h h0 h1 s Hj Hs /=.
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) x) (Rlt x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i)) *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
    elim: s h h0 h1 {Hj} Hs => [| h2 s IH] h h0 h1 Hs /=.
(* Goal: Rle (@SF_h R s) (@SF_h R s) *)
(* Goal: Rle (@SF_h R s) (@nth R (IZR Z0) (@unzip1 R R (@map (prod R R) (prod R R) (fun x : prod R R => @pair R R (@fst R R x) (f (@snd R R x))) (@SF_t R s))) i) *)
    apply Rle_refl.
(* Goal: Rle h0 (@nth R (IZR Z0) (@Datatypes.cons R h1 (@Datatypes.cons R h2 s)) (@size R s)) *)
(* Goal: Rle (@nth R (IZR Z0) (@Datatypes.cons R h1 s) j) (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) *)
    apply Rle_trans with (r2 := h1).
(* Goal: @SF_sorted R Rle (@SF_cons R h0 s) *)
(* Goal: lt i (S (@size R (@unzip1 R R (@SF_t R s)))) *)
    apply Hs.
(* Goal: Rle h1 (@nth R (IZR Z0) (@Datatypes.cons R h1 (@Datatypes.cons R h2 s)) (@size R s)) *)
(* Goal: Rle (@nth R (IZR Z0) (@Datatypes.cons R h1 s) j) (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) *)
    apply (IH h0 h1 h2).
(* Goal: @sorted R Rle (@Datatypes.cons R h0 (@Datatypes.cons R h1 (@Datatypes.cons R h2 s))) *)
(* Goal: Rle (@nth R (IZR Z0) (@Datatypes.cons R h1 s) j) (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) *)
    apply (proj2 Hs).
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: s Hj Hs => [| h2 s] Hj Hs.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    by apply lt_S_n, lt_S_n, lt_S_n, lt_n_O in Hj.
(* Goal: Rle (@nth R (IZR Z0) (@Datatypes.cons R h1 (@Datatypes.cons R h2 s)) j) (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 (@Datatypes.cons R h2 s))) (@size R (@Datatypes.cons R h2 s))) *)
  apply (IH h0 h1 h2 s).
(* Goal: lt (S (S j)) (@size R (@Datatypes.cons R h0 (@Datatypes.cons R h1 (@Datatypes.cons R h2 s)))) *)
(* Goal: @sorted R Rle (@Datatypes.cons R h0 (@Datatypes.cons R h1 (@Datatypes.cons R h2 s))) *)
  apply lt_S_n, Hj.
(* Goal: @SF_sorted R Rle (@SF_cons R h0 s) *)
(* Goal: lt i (S (@size R (@unzip1 R R (@SF_t R s)))) *)
  apply Hs.
Qed.

End SF_fun.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_fun_map {T T0 : Type} (f : T -> T0) (s : SF_seq) y0 :
  forall x, SF_fun (SF_map f s) (f y0) x = f (SF_fun s y0 x).
Proof.
(* Goal: forall x : R, @eq T0 (@SF_fun T0 (@SF_map T T0 f s) (f y0) x) (f (@SF_fun T s y0 x)) *)
  case: s => sh st ; rewrite /SF_fun /SF_map /= ; case: st => [| h st] x /=.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by case: Rle_dec.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Rlt_dec => //.
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) x) (Rlt x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i)) *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  elim: st sh h y0 x => [| h0 st IH] sh h y0 x Hx //=.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by case: Rle_dec.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Rlt_dec => // {Hx} Hx.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply: (IH (fst h)).
Qed.

(** * Particular SF_seq *)

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Definition SF_seq_f1 {T : Type} (f1 : R -> T) (P : seq R) : SF_seq :=
  mkSF_seq (head 0 P) (pairmap (fun x y => (y, f1 x)) (head 0 P) (behead P)).
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Definition SF_seq_f2 {T : Type} (f2 : R -> R -> T) (P : seq R) : SF_seq :=
  mkSF_seq (head 0 P) (pairmap (fun x y => (y, f2 x y)) (head 0 P) (behead P)).

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_cons_f1 {T : Type} (f1 : R -> T) (h : R) (P : seq R) :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  (0 < size P)%nat -> SF_seq_f1 f1 (h::P) = SF_cons (h,f1 h) (SF_seq_f1 f1 P).
Proof.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: P => [ H | h0 P _] //.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply lt_n_O in H.
Qed.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_cons_f2 {T : Type} (f2 : R -> R -> T) (h : R) (P : seq R) :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  (0 < size P)%nat ->
    SF_seq_f2 f2 (h::P) = SF_cons (h,f2 h (head 0 P)) (SF_seq_f2 f2 P).
Proof.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: P => [ H | h0 P _] //.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply lt_n_O in H.
Qed.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_size_f1 {T : Type} (f1 : R -> T) P :
  SF_size (SF_seq_f1 f1 P) = Peano.pred (size P).
Proof.
(* Goal: @eq nat (@SF_size T (@SF_seq_f2 T f2 P)) (Init.Nat.pred (@size R P)) *)
  case: P => [| h P] //= ; by rewrite /SF_size /= size_pairmap.
Qed.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_size_f2 {T : Type} (f2 : R -> R -> T) P :
  SF_size (SF_seq_f2 f2 P) = Peano.pred (size P).
Proof.
(* Goal: @eq nat (@SF_size T (@SF_seq_f2 T f2 P)) (Init.Nat.pred (@size R P)) *)
  case: P => [| h P] //= ; by rewrite /SF_size /= size_pairmap.
Qed.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_lx_f1 {T : Type} (f1 : R -> T) P :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  (0 < size P)%nat -> SF_lx (SF_seq_f1 f1 P) = P.
Proof.
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) x) (Rlt x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i)) *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  elim: P => [ H | h l IH _] //=.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply lt_n_O in H.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: l IH => [ | h' l] //= IH.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite -{2}IH //.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply lt_O_Sn.
Qed.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_lx_f2 {T : Type} (f2 : R -> R -> T) P :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  (0 < size P)%nat ->
  SF_lx (SF_seq_f2 f2 P) = P.
Proof.
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) x) (Rlt x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i)) *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  elim: P => [ H | h l IH _] //=.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply lt_n_O in H.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: l IH => [ | h' l] //= IH.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite -{2}IH //.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply lt_O_Sn.
Qed.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_ly_f1 {T : Type} (f1 : R -> T) P :
  SF_ly (SF_seq_f1 f1 P) = Rcomplements.belast (map f1 P).
Proof.
  case: P => [| h P] // ; elim: P h =>  //= h P IH h0 ;
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  by rewrite -(IH h).
Qed.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_ly_f2 {T : Type} (f2 : R -> R -> T) P :
  SF_ly (SF_seq_f2 f2 P) = behead (pairmap f2 0 P).
Proof.
  case: P => [| h P] // ; elim: P h =>  //= h P IH h0 ;
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  by rewrite -(IH h).
Qed.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_sorted_f1 {T : Type} (f1 : R -> T) P Ord :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  (sorted Ord P) <-> (SF_sorted Ord (SF_seq_f1 f1 P)).
Proof.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: P => [ | h P] //.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite /SF_sorted SF_lx_f1 //.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply lt_O_Sn.
Qed.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_sorted_f2 {T : Type} (f2 : R -> R -> T) P Ord :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  (sorted Ord P) <-> (SF_sorted Ord (SF_seq_f2 f2 P)).
Proof.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: P => [ | h P] //.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite /SF_sorted SF_lx_f2 //.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply lt_O_Sn.
Qed.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_rev_f2 {T : Type} (f2 : R -> R -> T) P : (forall x y, f2 x y = f2 y x) ->
  SF_rev (SF_seq_f2 f2 P) = SF_seq_f2 f2 (rev P).
Proof.
  move => Hf2 ; apply SF_lx_ly_inj ;
  case: P => [ | h P] //=.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite SF_lx_rev !SF_lx_f2 ?rev_cons /= 1?headI // ; by apply lt_O_Sn.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite SF_ly_rev !SF_ly_f2 /= ?rev_cons.
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) x) (Rlt x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i)) *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  elim: P h => [ | h0 P IH] h //=.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite !rev_cons pairmap_rcons behead_rcons ?(IH h0) ?(Hf2 h h0) //.
(* Goal: lt O (@size T (@pairmap R T (fun x y : R => f (Rdiv (Rplus x y) (IZR (Zpos (xO xH))))) (IZR Z0) (@rcons R (@rev R s) h0))) *)
(* Goal: @eq (list T) (@rcons T (@pairmap R T (fun x y : R => f (Rdiv (Rplus x y) (IZR (Zpos (xO xH))))) (IZR Z0) (@rcons R (@rev R s) h0)) (f (Rdiv (Rplus h0 h) (IZR (Zpos (xO xH)))))) (@pairmap R T (fun x y : R => f (Rdiv (Rplus x y) (IZR (Zpos (xO xH))))) (IZR Z0) (@rcons R (@rcons R (@rev R s) h0) h)) *)
  rewrite size_pairmap size_rcons ; apply lt_O_Sn.
Qed.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_map_f1 {T T0 : Type} (f : T -> T0) (f1 : R -> T) P :
  SF_map f (SF_seq_f1 f1 P) = SF_seq_f1 (fun x => f (f1 x)) P.
Proof.
(* Goal: @eq (list T) (@behead T (@pairmap R T (fun x y : R => f (Rdiv (Rplus x y) (IZR (Zpos (xO xH))))) (IZR Z0) (unif_part a b n))) (@rev T (@behead T (@pairmap R T (fun x y : R => f (Rdiv (Rplus x y) (IZR (Zpos (xO xH))))) (IZR Z0) (@rev R (unif_part a b n))))) *)
  case: P => [| h P] // ; elim: P h => [| h0 P IH] h //.
(* Goal: le i (Init.Nat.pred (@size R (@Datatypes.cons R h s))) *)
  rewrite ?(SF_cons_f1 _ _ (h0::P)) /= ; try intuition.
(* Goal: le i (Init.Nat.pred (@size R (@Datatypes.cons R h s))) *)
  rewrite SF_map_cons IH ; intuition.
Qed.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_map_f2 {T T0 : Type} (f : T -> T0) (f2 : R -> R -> T) P :
  SF_map f (SF_seq_f2 f2 P) = SF_seq_f2 (fun x y => f (f2 x y)) P.
Proof.
(* Goal: @eq (list T) (@behead T (@pairmap R T (fun x y : R => f (Rdiv (Rplus x y) (IZR (Zpos (xO xH))))) (IZR Z0) (unif_part a b n))) (@rev T (@behead T (@pairmap R T (fun x y : R => f (Rdiv (Rplus x y) (IZR (Zpos (xO xH))))) (IZR Z0) (@rev R (unif_part a b n))))) *)
  case: P => [| h P] // ; elim: P h => [| h0 P IH] h //.
(* Goal: le i (Init.Nat.pred (@size R (@Datatypes.cons R h s))) *)
  rewrite ?(SF_cons_f2 _ _ (h0::P)) /= ; try intuition.
(* Goal: le i (Init.Nat.pred (@size R (@Datatypes.cons R h s))) *)
  rewrite SF_map_cons IH ; intuition.
Qed.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma ptd_f2 (f : R -> R -> R) s :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  sorted Rle s -> (forall x y, x <= y -> x <= f x y <= y)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  -> pointed_subdiv (SF_seq_f2 f s).
Proof.
(* Goal: forall (_ : @sorted R Rle s) (_ : forall (x y : R) (_ : Rle x y), and (Rle x (f x y)) (Rle (f x y) y)), pointed_subdiv (@SF_seq_f2 R f s) *)
  intros Hs Hf.
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) x) (Rlt x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i)) *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  elim: s Hs => [ _ | h s].
(* Goal: forall (i : nat) (_ : lt i (Init.Nat.pred (S (S n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
  intros i Hi.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply lt_n_O in Hi.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: s => [ | h' s] IH Hs.
(* Goal: forall (i : nat) (_ : lt i (Init.Nat.pred (S (S n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
  intros i Hi.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply lt_n_O in Hi.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case => [ | i] Hi.
(* Goal: and (Rle (@nth R (IZR Z0) (@SF_lx R (@SF_seq_f2 R f (@Datatypes.cons R h (@Datatypes.cons R h' s)))) O) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (@Datatypes.cons R h (@Datatypes.cons R h' s)))) O)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (@Datatypes.cons R h (@Datatypes.cons R h' s)))) O) (@nth R (IZR Z0) (@SF_lx R (@SF_seq_f2 R f (@Datatypes.cons R h (@Datatypes.cons R h' s)))) (S O))) *)
(* Goal: and (Rle (@nth R (IZR Z0) (@SF_lx R (@SF_seq_f2 R f (@Datatypes.cons R h (@Datatypes.cons R h' s)))) (S i)) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (@Datatypes.cons R h (@Datatypes.cons R h' s)))) (S i))) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (@Datatypes.cons R h (@Datatypes.cons R h' s)))) (S i)) (@nth R (IZR Z0) (@SF_lx R (@SF_seq_f2 R f (@Datatypes.cons R h (@Datatypes.cons R h' s)))) (S (S i)))) *)
  apply Hf, Hs.
(* Goal: Rle (@Riemann_sum R_ModuleSpace f s) (@Riemann_sum R_ModuleSpace g s) *)
  apply IH.
(* Goal: @SF_sorted R Rle (@SF_cons R h0 s) *)
(* Goal: lt i (S (@size R (@unzip1 R R (@SF_t R s)))) *)
  apply Hs.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply lt_S_n.
Qed.

(** ** SF_fun *)

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Definition SF_fun_f1 {T : Type} (f1 : R -> T) (P : seq R) x : T :=
  SF_fun (SF_seq_f1 f1 P) (f1 0) x.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Definition SF_fun_f2 {T : Type} (f2 : R -> R -> T) (P : seq R) x :=
  SF_fun (SF_seq_f2 f2 P) (f2 0 0) x.

(** * Uniform partition *)

Definition unif_part (a b : R) (n : nat) : seq R :=
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  mkseq (fun i => a + (INR i) * (b - a) / (INR n + 1)) (S (S n)).

Lemma unif_part_bound (a b : R) (n : nat) :
  unif_part a b n = rev (unif_part b a n).
Proof.
  apply (@eq_from_nth R 0) ; rewrite ?size_rev ?size_mkseq => // ;
  move => i Hi ; apply SSR_leq in Hi.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite nth_rev ?SSR_minus ?size_mkseq.
  2: now apply SSR_leq.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite ?nth_mkseq.
  3: now apply SSR_leq.
  rewrite minus_INR ?S_INR => // ; field.
  apply Rgt_not_eq, INRp1_pos.
  apply SSR_leq, INR_le ; rewrite ?S_INR minus_INR ?S_INR => //.
  apply Rminus_le_0 ; ring_simplify.
  apply pos_INR.
Qed.

Lemma unif_part_sort (a b : R) (n : nat) : a <= b -> sorted Rle (unif_part a b n).
Proof.
  move => Hab ; apply sorted_nth => i Hi x0 ;
  rewrite ?size_mkseq in Hi ; rewrite ?nth_mkseq ?S_INR ;
  [ |apply SSR_leq ; intuition | apply SSR_leq ; intuition ].
  apply Rminus_le_0 ; field_simplify ;
  [| apply Rgt_not_eq ; intuition] ; rewrite ?Rdiv_1 ;
(* Goal: le i (Init.Nat.pred (@size R (@Datatypes.cons R h s))) *)
  apply Rdiv_le_0_compat ; intuition.
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite Rplus_comm ; by apply (proj1 (Rminus_le_0 _ _)).
Qed.

Lemma head_unif_part x0 (a b : R) (n : nat) :
  head x0 (unif_part a b n) = a.
Proof.
(* Goal: @eq (Ring.sort R_Ring) (Rminus (@SF_h R s) x0) (@plus (Ring.AbelianGroup R_Ring) (Rminus x x0) (Rminus (@SF_h R s) x)) *)
  rewrite /= Rmult_0_l /Rdiv ; ring.
Qed.

Lemma last_unif_part x0 (a b : R) (n : nat) :
  last x0 (unif_part a b n) = b.
Proof.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite (last_nth b) size_mkseq.
  replace (nth b (x0 :: unif_part a b n) (S (S n)))
(* Goal: @eq (@SF_seq T0) (@SF_cons T0 (@pair R T0 (@fst R T h0) (f (@snd R T h0))) (@SF_rcons T0 (SF_map f s) (@pair R T0 (@fst R T h) (f (@snd R T h))))) (@SF_rcons T0 (@SF_cons T0 (@pair R T0 (@fst R T h0) (f (@snd R T h0))) (SF_map f s)) (@pair R T0 (@fst R T h) (f (@snd R T h)))) *)
    with (nth b (unif_part a b n) (S n)) by auto.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite nth_mkseq.
(* Goal: @eq R (Rplus a (Rdiv (Rmult (INR (S i)) (Rminus b a)) (Rplus (INR (S n)) (IZR (Zpos xH))))) (Rplus (Rdiv (Rplus (Rmult a (INR (S n))) b) (INR (S (S n)))) (Rdiv (Rmult (INR i) (Rminus b (Rdiv (Rplus (Rmult a (INR (S n))) b) (INR (S (S n)))))) (Rplus (INR n) (IZR (Zpos xH))))) *)
  rewrite S_INR ; field.
  by apply Rgt_not_eq, INRp1_pos.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by [].
Qed.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma unif_part_nat (a b : R) (n : nat) (x : R) : (a <= x <= b) ->
  {i : nat |
  nth 0 (unif_part a b n) i <= x < nth 0 (unif_part a b n) (S i) /\
(* Goal: forall (i : nat) (_ : lt (S i) (@size R (unif_part a b n))), @eq R (Rminus (@nth R (IZR Z0) (unif_part a b n) (S i)) (@nth R (IZR Z0) (unif_part a b n) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
  (S (S i) < size (unif_part a b n))%nat} +
  {nth 0 (unif_part a b n) (n) <= x <=
   nth 0 (unif_part a b n) (S n)}.
Proof.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  move: (sorted_dec (unif_part a b n) 0 x) => Hdec Hx.
  have Hs : sorted Rle (unif_part a b n) ;
    [ apply unif_part_sort, Rle_trans with (r2 := x) ; intuition
    | move: (Hdec Hs) => {Hdec Hs} Hdec].
(* Goal: adapted_couple (@SF_fun R s z0) (@head R (IZR Z0) (@SF_lx R s)) (@last R (IZR Z0) (@SF_lx R s)) (seq2Rlist (@SF_lx R s)) (seq2Rlist (@SF_ly R s)) *)
  have Hx' : (head 0 (unif_part a b n) <= x <= last 0 (unif_part a b n)).
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    by rewrite head_unif_part last_unif_part.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: (Hdec Hx') => {Hdec Hx'} [[i Hi]|Hi].
(* Goal: sumor (@sig nat (fun i : nat => and (and (Rle (@nth R (IZR Z0) (unif_part a b n) i) x) (Rlt x (@nth R (IZR Z0) (unif_part a b n) (S i)))) (lt (S (S i)) (@size R (unif_part a b n))))) (and (Rle (@nth R (IZR Z0) (unif_part a b n) n) x) (Rle x (@nth R (IZR Z0) (unif_part a b n) (S n)))) *)
(* Goal: sumor (@sig nat (fun i : nat => and (and (Rle (@nth R (IZR Z0) (unif_part a b n) i) x) (Rlt x (@nth R (IZR Z0) (unif_part a b n) (S i)))) (lt (S (S i)) (@size R (unif_part a b n))))) (and (Rle (@nth R (IZR Z0) (unif_part a b n) n) x) (Rle x (@nth R (IZR Z0) (unif_part a b n) (S n)))) *)
  left ; by exists i.
(* Goal: le i (Init.Nat.pred (@size R (@Datatypes.cons R h s))) *)
  right ; rewrite size_mkseq /= in Hi ; intuition.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by rewrite -minus_n_O in H1.
Qed.

Lemma seq_step_unif_part (a b : R) (n : nat) :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  seq_step (unif_part a b n) = Rabs ((b - a) / (INR n + 1)).
Proof.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  assert (forall i, (S i < size (unif_part a b n))%nat ->
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
    (nth 0 (unif_part a b n) (S i) - nth 0 (unif_part a b n) i = (b - a) / (INR n + 1))%R).
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
    rewrite size_mkseq => i Hi.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    rewrite !nth_mkseq.
(* Goal: @eq (Ring.sort R_Ring) (Rminus (@SF_h R s) x0) (@plus (Ring.AbelianGroup R_Ring) (Rminus x x0) (Rminus (@SF_h R s) x)) *)
    rewrite S_INR /Rdiv /= ; ring.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    by apply SSR_leq, lt_le_weak.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    by apply SSR_leq.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  move: (eq_refl (size (unif_part a b n))).
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite {2}size_mkseq.
  rewrite /seq_step ;
  elim: {2}(n) (unif_part a b n) H => [ | m IH] l //= ;
  destruct l as [ | x0 l] => //= ;
  destruct l as [ | x1 l] => //= ;
  destruct l as [ | x2 l] => //= ; intros.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite (H O).
(* Goal: @eq Rlist (@subdivision_val a b (SF_inf_r f a b n)) (if Rle_dec a b then seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part a b n))) else seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part b a n)))) *)
  rewrite Rmax_comm /Rmax ; case: Rle_dec => // H1.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  contradict H1 ; by apply Rabs_pos.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply lt_n_S, lt_O_Sn.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite -(IH (x1::x2::l)) /=.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite (H O).
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite (H 1%nat).
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite Rmax_assoc.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  apply f_equal2 => //.
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite /Rmax ; by case: Rle_dec.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply lt_n_S, lt_n_S, lt_O_Sn.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply lt_n_S, lt_O_Sn.
(* Goal: forall (i : nat) (_ : lt (S i) (S (S (@size R l)))), @eq R (Rminus (@nth R (IZR Z0) (@Datatypes.cons R x2 l) i) (@nth R (IZR Z0) (@Datatypes.cons R x1 (@Datatypes.cons R x2 l)) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: @eq nat (S (S (@size R l))) (S (S m)) *)
  now intros ; apply (H (S i)), lt_n_S.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply eq_add_S.
Qed.

Lemma seq_step_unif_part_ex (a b : R) (eps : posreal) :
  {n : nat | seq_step (unif_part a b n) < eps}.
Proof.
(* Goal: @sig nat (fun n : nat => Rlt (seq_step (unif_part a b n)) (pos eps)) *)
  destruct (nfloor_ex (Rabs ((b - a) / eps))) as [n Hn].
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    by apply Rabs_pos.
(* Goal: @sig nat (fun n : nat => Rlt (seq_step (unif_part a b n)) (pos eps)) *)
  exists n.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite seq_step_unif_part.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite Rabs_div.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite (Rabs_pos_eq (INR n + 1)).
(* Goal: Rlt (Rminus (Rmax a b) (Rmin a b)) (Rmult (Rplus (INR n') (IZR (Zpos xH))) (pos alpha)) *)
(* Goal: and (pointed_subdiv (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n))) (and (@eq R (@SF_h R (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n))) (Rmin a b)) (@eq R (@last R (@SF_h R (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n))) (@SF_lx R (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n)))) (Rmax a b))) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  apply Rlt_div_l.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply INRp1_pos.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite Rmult_comm -Rlt_div_l.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite -(Rabs_pos_eq eps).
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite -Rabs_div.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply Hn.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply Rgt_not_eq, eps.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply Rlt_le, eps.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply eps.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply Rlt_le, INRp1_pos.
  by apply Rgt_not_eq, INRp1_pos.
Qed.

Lemma unif_part_S a b n :
(* Goal: forall (i : nat) (_ : lt (S i) (@size R (unif_part a b n))), @eq R (Rminus (@nth R (IZR Z0) (unif_part a b n) (S i)) (@nth R (IZR Z0) (unif_part a b n) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
  unif_part a b (S n) = a :: unif_part ((a * INR (S n) + b) / INR (S (S n))) b n.
Proof.
(* Goal: @eq (list R) (@map R R (fun x : R => Rplus (Rmult u x) v) (unif_part a b n)) (unif_part (Rplus (Rmult u a) v) (Rplus (Rmult u b) v) n) *)
  apply eq_from_nth with 0.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by rewrite /= !size_map !size_iota.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case => [ | i] Hi.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by rewrite nth0 head_unif_part.
(* Goal: forall (i : nat) (_ : lt (S i) (@size R (unif_part a b n))), @eq R (Rminus (@nth R (IZR Z0) (unif_part a b n) (S i)) (@nth R (IZR Z0) (unif_part a b n) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
  change (nth 0 (a :: unif_part ((a * INR (S n) + b) / INR (S (S n))) b n) (S i))
(* Goal: forall (i : nat) (_ : lt (S i) (@size R (unif_part a b n))), @eq R (Rminus (@nth R (IZR Z0) (unif_part a b n) (S i)) (@nth R (IZR Z0) (unif_part a b n) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
    with (nth 0 (unif_part ((a * INR (S n) + b) / INR (S (S n))) b n) i).
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite /unif_part size_mkseq in Hi.
(* Goal: le i (Init.Nat.pred (@size R (@Datatypes.cons R h s))) *)
  rewrite /unif_part !nth_mkseq ; try by intuition.
(* Goal: @eq R (Rplus a (Rdiv (Rmult (INR (S i)) (Rminus b a)) (Rplus (INR (S n)) (IZR (Zpos xH))))) (Rplus (Rdiv (Rplus (Rmult a (INR (S n))) b) (INR (S (S n)))) (Rdiv (Rmult (INR i) (Rminus b (Rdiv (Rplus (Rmult a (INR (S n))) b) (INR (S (S n)))))) (Rplus (INR n) (IZR (Zpos xH))))) *)
  rewrite !S_INR ; field.
(* Goal: and (not (@eq R (Rplus (Rplus (INR n) (IZR (Zpos xH))) (IZR (Zpos xH))) (IZR Z0))) (not (@eq R (Rplus (INR n) (IZR (Zpos xH))) (IZR Z0))) *)
  rewrite -!S_INR ; split ; apply sym_not_eq, (not_INR 0), O_S.
Qed.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Definition SF_val_seq {T} (f : R -> T) (a b : R) (n : nat) : SF_seq :=
(* Goal: forall (i : nat) (_ : lt (S i) (@size R (unif_part a b n))), @eq R (Rminus (@nth R (IZR Z0) (unif_part a b n) (S i)) (@nth R (IZR Z0) (unif_part a b n) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
  SF_seq_f2 (fun x y => f ((x+y)/2)) (unif_part a b n).
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Definition SF_val_fun {T} (f : R -> T) (a b : R) (n : nat) (x : R) : T :=
(* Goal: forall (i : nat) (_ : lt (S i) (@size R (unif_part a b n))), @eq R (Rminus (@nth R (IZR Z0) (unif_part a b n) (S i)) (@nth R (IZR Z0) (unif_part a b n) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
  SF_fun_f2 (fun x y => f ((x+y)/2)) (unif_part a b n) x.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Definition SF_val_ly {T} (f : R -> T) (a b : R) (n : nat) : seq T :=
(* Goal: forall (i : nat) (_ : lt (S i) (@size R (unif_part a b n))), @eq R (Rminus (@nth R (IZR Z0) (unif_part a b n) (S i)) (@nth R (IZR Z0) (unif_part a b n) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
  behead (pairmap (fun x y => f ((x+y)/2)) 0 (unif_part a b n)).

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_val_ly_bound {T} (f : R -> T) (a b : R) (n : nat) :
  SF_val_ly f a b n = rev (SF_val_ly f b a n).
Proof.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite /SF_val_ly (unif_part_bound b a).
(* Goal: @eq (list T) (@behead T (@pairmap R T (fun x y : R => f (Rdiv (Rplus x y) (IZR (Zpos (xO xH))))) (IZR Z0) (unif_part a b n))) (@rev T (@behead T (@pairmap R T (fun x y : R => f (Rdiv (Rplus x y) (IZR (Zpos (xO xH))))) (IZR Z0) (@rev R (unif_part a b n))))) *)
  case: (unif_part a b n) => [| h s] // ; elim: s h => [| h0 s IH] h //=.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite ?rev_cons.
(* Goal: forall (i : nat) (_ : lt (S i) (@size R (unif_part a b n))), @eq R (Rminus (@nth R (IZR Z0) (unif_part a b n) (S i)) (@nth R (IZR Z0) (unif_part a b n) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
  replace (pairmap (fun x y : R => f ((x + y) / 2)) 0 (rcons (rcons (rev s) h0) h))
(* Goal: forall (i : nat) (_ : lt (S i) (@size R (unif_part a b n))), @eq R (Rminus (@nth R (IZR Z0) (unif_part a b n) (S i)) (@nth R (IZR Z0) (unif_part a b n) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
    with (rcons (pairmap (fun x y : R => f ((x + y) / 2)) 0 (rcons (rev s) h0)) (f ((h0+h)/2))).
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite behead_rcons.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite rev_rcons Rplus_comm -rev_cons -IH //.
(* Goal: lt O (@size T (@pairmap R T (fun x y : R => f (Rdiv (Rplus x y) (IZR (Zpos (xO xH))))) (IZR Z0) (@rcons R (@rev R s) h0))) *)
(* Goal: @eq (list T) (@rcons T (@pairmap R T (fun x y : R => f (Rdiv (Rplus x y) (IZR (Zpos (xO xH))))) (IZR Z0) (@rcons R (@rev R s) h0)) (f (Rdiv (Rplus h0 h) (IZR (Zpos (xO xH)))))) (@pairmap R T (fun x y : R => f (Rdiv (Rplus x y) (IZR (Zpos (xO xH))))) (IZR Z0) (@rcons R (@rcons R (@rev R s) h0) h)) *)
  rewrite size_pairmap size_rcons ; apply lt_O_Sn.
(* Goal: @eq (ModuleSpace.sort R_Ring R_ModuleSpace) (@Riemann_sum R_ModuleSpace f s) (Int_SF (@subdivision_val (@head R (IZR Z0) (@SF_lx R (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s))) (@last R (IZR Z0) (@SF_lx R (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s))) (SF_compat_le (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s) (@SF_map_sort R (ModuleSpace.sort R_Ring R_ModuleSpace) f s Rle pr))) (@subdivision (@head R (IZR Z0) (@SF_lx R (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s))) (@last R (IZR Z0) (@SF_lx R (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s))) (SF_compat_le (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s) (@SF_map_sort R (ModuleSpace.sort R_Ring R_ModuleSpace) f s Rle pr)))) *)
(* Goal: @eq (ModuleSpace.sort R_Ring R_ModuleSpace) (@Riemann_sum R_ModuleSpace f s) (Ropp (Int_SF (@subdivision_val (@head R (IZR Z0) (@SF_lx R (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s))) (@last R (IZR Z0) (@SF_lx R (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s))) (SF_compat_le (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s) (@SF_map_sort R (ModuleSpace.sort R_Ring R_ModuleSpace) f s Rle pr))) (@subdivision (@head R (IZR Z0) (@SF_lx R (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s))) (@last R (IZR Z0) (@SF_lx R (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s))) (SF_compat_le (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s) (@SF_map_sort R (ModuleSpace.sort R_Ring R_ModuleSpace) f s Rle pr))))) *)
  move: (0) h h0 {IH} ; apply rcons_ind with (s := s) => {s} [| s h1 IH] x0 h h0 //.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite ?rev_rcons /= IH //.
Qed.

(** ** A proper filter on SF_seq *)

Lemma Riemann_fine_unif_part :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  forall (f : R -> R -> R) (a b : R) (n : nat),
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  (forall a b, a <= b -> a <= f a b <= b) ->
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  a <= b ->
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  seq_step (SF_lx (SF_seq_f2 f (unif_part a b n))) <= (b - a) / (INR n + 1) /\
  pointed_subdiv (SF_seq_f2 f (unif_part a b n)) /\
  SF_h (SF_seq_f2 f (unif_part a b n)) = a /\
  last (SF_h (SF_seq_f2 f (unif_part a b n))) (SF_lx (SF_seq_f2 f (unif_part a b n))) = b.
Proof.
(* Goal: forall (f : forall (_ : R) (_ : R), R) (a b : R) (n : nat) (_ : forall (a0 b0 : R) (_ : Rle a0 b0), and (Rle a0 (f a0 b0)) (Rle (f a0 b0) b0)) (_ : Rle a b), and (Rle (seq_step (@SF_lx R (@SF_seq_f2 R f (unif_part a b n)))) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH))))) (and (pointed_subdiv (@SF_seq_f2 R f (unif_part a b n))) (and (@eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a) (@eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (@SF_lx R (@SF_seq_f2 R f (unif_part a b n)))) b))) *)
intros f a b n Hf Hab.
(* Goal: and (Rle (seq_step (@SF_lx R (@SF_seq_f2 R f (unif_part a b n)))) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH))))) (and (pointed_subdiv (@SF_seq_f2 R f (unif_part a b n))) (and (@eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a) (@eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (@SF_lx R (@SF_seq_f2 R f (unif_part a b n)))) b))) *)
assert (Hab' : 0 <= (b - a) / (INR n + 1)).
(* Goal: Rle (IZR Z0) (Rdiv (Rminus (Rmax a b) (Rmin a b)) (pos alpha)) *)
(* Goal: @ex (@SF_seq R) (fun x : @SF_seq R => P x) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  apply Rdiv_le_0_compat.
(* Goal: Rle (IZR Z0) (Rminus (@SF_h R s) x0) *)
(* Goal: Rle (f y0) (g y0) *)
(* Goal: lt O (@SF_size R (@SF_cons R (@pair R R x0 y0) s)) *)
(* Goal: Rle (@Riemann_sum R_ModuleSpace f s) (@Riemann_sum R_ModuleSpace g s) *)
  apply -> Rminus_le_0.
(* Goal: Rle (Rmin a b) (Rmax a b) *)
(* Goal: Rlt (IZR Z0) (pos alpha) *)
(* Goal: @ex (@SF_seq R) (fun x : @SF_seq R => P x) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  apply Hab.
(* Goal: Rgt (Rplus (INR n) (IZR (Zpos xH))) (IZR Z0) *)
(* Goal: Rlt (Rminus (Rmax a b) (Rmin a b)) (Rmult (pos alpha) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: and (pointed_subdiv (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n))) (and (@eq R (@SF_h R (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n))) (Rmin a b)) (@eq R (@last R (@SF_h R (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n))) (@SF_lx R (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n)))) (Rmax a b))) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  apply INRp1_pos.
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
unfold pointed_subdiv.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
rewrite SF_lx_f2.
(* Goal: and (Rle (seq_step (unif_part a b n)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH))))) (and (forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i)))) (and (@eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a) (@eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b))) *)
(* Goal: lt O (@size R (unif_part a b n)) *)
change (head 0 (unif_part a b n) :: behead (unif_part a b n)) with (unif_part a b n).
(* Goal: and (Rle (seq_step (unif_part a b n)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH))))) (and (forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i)))) (and (@eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a) (@eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b))) *)
(* Goal: lt O (@size R (unif_part a b n)) *)
split ; [|split ; [|split]].
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
- cut (forall i, (S i < size (unif_part a b n))%nat ->
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
    nth 0 (unif_part a b n) (S i) - nth 0 (unif_part a b n) i = (b - a) / (INR n + 1)).
(* Goal: forall (i : nat) (_ : lt (S i) (@size R (unif_part a b n))), @eq R (Rminus (@nth R (IZR Z0) (unif_part a b n) (S i)) (@nth R (IZR Z0) (unif_part a b n) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
  + induction (unif_part a b n) as [|x0 l IHl].
(* Goal: forall _ : forall (i : nat) (_ : lt (S i) (@size R (@Datatypes.nil R))), @eq R (Rminus (@nth R (IZR Z0) (@Datatypes.nil R) (S i)) (@nth R (IZR Z0) (@Datatypes.nil R) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))), Rle (seq_step (@Datatypes.nil R)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall _ : forall (i : nat) (_ : lt (S i) (@size R (@Datatypes.cons R x0 l))), @eq R (Rminus (@nth R (IZR Z0) (@Datatypes.cons R x0 l) (S i)) (@nth R (IZR Z0) (@Datatypes.cons R x0 l) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))), Rle (seq_step (@Datatypes.cons R x0 l)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt (S i) (@size R (unif_part a b n))), @eq R (Rminus (@nth R (IZR Z0) (unif_part a b n) (S i)) (@nth R (IZR Z0) (unif_part a b n) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
    now intros _.
(* Goal: forall _ : forall (i : nat) (_ : lt (S i) (@size R (@Datatypes.cons R x0 l))), @eq R (Rminus (@nth R (IZR Z0) (@Datatypes.cons R x0 l) (S i)) (@nth R (IZR Z0) (@Datatypes.cons R x0 l) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))), Rle (seq_step (@Datatypes.cons R x0 l)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt (S i) (@size R (unif_part a b n))), @eq R (Rminus (@nth R (IZR Z0) (unif_part a b n) (S i)) (@nth R (IZR Z0) (unif_part a b n) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
    intros H.
(* Goal: Rle (seq_step (@Datatypes.cons R x0 l)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt (S i) (@size R (unif_part a b n))), @eq R (Rminus (@nth R (IZR Z0) (unif_part a b n) (S i)) (@nth R (IZR Z0) (unif_part a b n) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
    destruct l as [|x1 l].
(* Goal: Rle (seq_step (@Datatypes.cons R x0 (@Datatypes.nil R))) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: Rle (seq_step (@Datatypes.cons R x0 (@Datatypes.cons R x1 l))) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt (S i) (@size R (unif_part a b n))), @eq R (Rminus (@nth R (IZR Z0) (unif_part a b n) (S i)) (@nth R (IZR Z0) (unif_part a b n) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
    easy.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
    change (seq_step _) with (Rmax (Rabs (x1 - x0)) (seq_step (x1 :: l))).
(* Goal: Rle (Rmin a b) (Rmax a b) *)
(* Goal: @ex (@SF_seq R) (fun x : @SF_seq R => P x) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
    apply Rmax_case.
(* Goal: Rle (Rabs (Rminus x1 x0)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: Rle (seq_step (@Datatypes.cons R x1 l)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt (S i) (@size R (unif_part a b n))), @eq R (Rminus (@nth R (IZR Z0) (unif_part a b n) (S i)) (@nth R (IZR Z0) (unif_part a b n) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
    apply Req_le.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    rewrite (H 0%nat).
(* Goal: @eq R (Rabs (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH))))) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: lt (S O) (@size R (@Datatypes.cons R x0 (@Datatypes.cons R x1 l))) *)
(* Goal: Rle (seq_step (@Datatypes.cons R x1 l)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt (S i) (@size R (unif_part a b n))), @eq R (Rminus (@nth R (IZR Z0) (unif_part a b n) (S i)) (@nth R (IZR Z0) (unif_part a b n) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
    now apply Rabs_pos_eq.
(* Goal: lt (S O) (@size R (@Datatypes.cons R x0 (@Datatypes.cons R x1 l))) *)
(* Goal: Rle (seq_step (@Datatypes.cons R x1 l)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt (S i) (@size R (unif_part a b n))), @eq R (Rminus (@nth R (IZR Z0) (unif_part a b n) (S i)) (@nth R (IZR Z0) (unif_part a b n) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
    apply lt_n_S.
(* Goal: lt O (S ((fix size (s : list R) : nat := match s with | Datatypes.nil => O | Datatypes.cons t s' => S (size s') end) l)) *)
(* Goal: Rle (seq_step (@Datatypes.cons R x1 l)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt (S i) (@size R (unif_part a b n))), @eq R (Rminus (@nth R (IZR Z0) (unif_part a b n) (S i)) (@nth R (IZR Z0) (unif_part a b n) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
    apply lt_0_Sn.
(* Goal: Rle (seq_step (@Datatypes.cons R x1 l)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt (S i) (@size R (unif_part a b n))), @eq R (Rminus (@nth R (IZR Z0) (unif_part a b n) (S i)) (@nth R (IZR Z0) (unif_part a b n) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
    apply IHl.
(* Goal: forall (i : nat) (_ : lt i (Init.Nat.pred (S (S n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
    intros i Hi.
(* Goal: @eq R (Rminus (@nth R (IZR Z0) (@Datatypes.cons R x1 l) (S i)) (@nth R (IZR Z0) (@Datatypes.cons R x1 l) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt (S i) (@size R (unif_part a b n))), @eq R (Rminus (@nth R (IZR Z0) (unif_part a b n) (S i)) (@nth R (IZR Z0) (unif_part a b n) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
    apply (H (S i)).
(* Goal: lt (S O) (@size R (@Datatypes.cons R x0 (@Datatypes.cons R x1 l))) *)
(* Goal: Rle (seq_step (@Datatypes.cons R x1 l)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt (S i) (@size R (unif_part a b n))), @eq R (Rminus (@nth R (IZR Z0) (unif_part a b n) (S i)) (@nth R (IZR Z0) (unif_part a b n) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
    now apply lt_n_S.
(* Goal: forall (i : nat) (_ : lt (S i) (@size R (unif_part a b n))), @eq R (Rminus (@nth R (IZR Z0) (unif_part a b n) (S i)) (@nth R (IZR Z0) (unif_part a b n) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
  + rewrite size_mkseq.
(* Goal: forall (i : nat) (_ : lt i (Init.Nat.pred (S (S n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
    intros i Hi.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    rewrite !nth_mkseq.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    rewrite S_INR.
(* Goal: @eq R (Rminus (Rplus a (Rdiv (Rmult (Rplus (INR i) (IZR (Zpos xH))) (Rminus b a)) (Rplus (INR n) (IZR (Zpos xH))))) (Rplus a (Rdiv (Rmult (INR i) (Rminus b a)) (Rplus (INR n) (IZR (Zpos xH)))))) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: is_true (ssrnat.leq (S i) (S (S n))) *)
(* Goal: is_true (ssrnat.leq (S (S i)) (S (S n))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
    unfold Rdiv.
(* Goal: @eq R (Rplus (Rminus (@SF_h R s) x0) (Rminus (@last R (@SF_h R s) (@unzip1 R R (@SF_t R s))) (@SF_h R s))) (Rminus (@last R (@SF_h R s) (@unzip1 R R (@SF_t R s))) x0) *)
    ring.
(* Goal: is_true (ssrnat.leq (S i) (S (S n))) *)
(* Goal: is_true (ssrnat.leq (S (S i)) (S (S n))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
    apply SSR_leq.
(* Goal: le (S i) (S (S n)) *)
(* Goal: is_true (ssrnat.leq (S (S i)) (S (S n))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
    now apply lt_le_weak.
(* Goal: is_true (ssrnat.leq (S i) (S (S n))) *)
(* Goal: is_true (ssrnat.leq (S (S i)) (S (S n))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
    now apply SSR_leq.
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
- unfold pointed_subdiv.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite SF_size_f2.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite size_mkseq.
(* Goal: forall (i : nat) (_ : lt i (Init.Nat.pred (S (S n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
  intros i Hi.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite SF_ly_f2.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite nth_behead.
(* Goal: and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@pairmap R R f (IZR Z0) (unif_part a b n)) (S i))) (Rle (@nth R (IZR Z0) (@pairmap R R f (IZR Z0) (unif_part a b n)) (S i)) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
  apply gt_S_le, SSR_leq in Hi.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite (nth_pairmap 0).
(* Goal: and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (f (@nth R (IZR Z0) (@Datatypes.cons R (IZR Z0) (unif_part a b n)) (S i)) (@nth R (IZR Z0) (unif_part a b n) (S i)))) (Rle (f (@nth R (IZR Z0) (@Datatypes.cons R (IZR Z0) (unif_part a b n)) (S i)) (@nth R (IZR Z0) (unif_part a b n) (S i))) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: is_true (ssrnat.leq (S (S i)) (@size R (unif_part a b n))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
  change (nth 0 (0 :: unif_part a b n) (S i)) with (nth 0 (unif_part a b n) i).
(* Goal: and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (f (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (unif_part a b n) (S i)))) (Rle (f (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: is_true (ssrnat.leq (S (S i)) (@size R (unif_part a b n))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
  apply Hf.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite !nth_mkseq //.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite S_INR.
(* Goal: and (Rle a (Rdiv (Rplus a b) (IZR (Zpos (xO xH))))) (Rle (Rdiv (Rplus a b) (IZR (Zpos (xO xH)))) b) *)
(* Goal: and (Rle a (@nth R (IZR Z0) (@SF_ly R l) i)) (Rle (@nth R (IZR Z0) (@SF_ly R l) i) b) *)
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@Riemann_sum V g l) (@Riemann_sum V f l) *)
  lra.
(* Goal: is_true (ssrnat.leq (S i) (S (S n))) *)
(* Goal: is_true (ssrnat.leq (S (S i)) (@size R (unif_part a b n))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
  now apply ssrnat.leqW.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by rewrite size_mkseq.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
- apply head_unif_part.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
- apply last_unif_part.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
rewrite size_mkseq ; by apply lt_O_Sn.
Qed.

Definition Riemann_fine (a b : R) :=
  within (fun ptd => pointed_subdiv ptd /\ SF_h ptd = Rmin a b /\ last (SF_h ptd) (SF_lx ptd) = Rmax a b)
    (locally_dist (fun ptd => seq_step (SF_lx ptd))).

Global Instance Riemann_fine_filter :
  forall a b, ProperFilter (Riemann_fine a b).
Proof.
(* Goal: forall a b : R, @ProperFilter (@SF_seq R) (Riemann_fine a b) *)
intros a b.
(* Goal: @ProperFilter (@SF_seq R) (Riemann_fine a b) *)
constructor.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
- intros P [alpha H].
(* Goal: @ex (@SF_seq R) (fun x : @SF_seq R => P x) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  assert (Hab : Rmin a b <= Rmax a b).
(* Goal: Rle (Rmin a b) (Rmax a b) *)
(* Goal: @ex (@SF_seq R) (fun x : @SF_seq R => P x) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
    apply Rmax_case.
(* Goal: Rle (Rmin a b) a *)
(* Goal: Rle (Rmin a b) b *)
(* Goal: @ex (@SF_seq R) (fun x : @SF_seq R => P x) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
    apply Rmin_l.
(* Goal: Rle (Rmin a b) b *)
(* Goal: @ex (@SF_seq R) (fun x : @SF_seq R => P x) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
    apply Rmin_r.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  assert (Hn : 0 <= ((Rmax a b - Rmin a b) / alpha)).
(* Goal: Rle (IZR Z0) (Rdiv (Rminus (Rmax a b) (Rmin a b)) (pos alpha)) *)
(* Goal: @ex (@SF_seq R) (fun x : @SF_seq R => P x) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
    apply Rdiv_le_0_compat.
(* Goal: Rle (IZR Z0) (Rminus (@SF_h R s) x0) *)
(* Goal: Rle (f y0) (g y0) *)
(* Goal: lt O (@SF_size R (@SF_cons R (@pair R R x0 y0) s)) *)
(* Goal: Rle (@Riemann_sum R_ModuleSpace f s) (@Riemann_sum R_ModuleSpace g s) *)
    apply -> Rminus_le_0.
(* Goal: Rle (Rmin a b) (Rmax a b) *)
(* Goal: Rlt (IZR Z0) (pos alpha) *)
(* Goal: @ex (@SF_seq R) (fun x : @SF_seq R => P x) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
    apply Hab.
(* Goal: Rgt (pos alpha) (IZR Z0) *)
(* Goal: Rlt (Rdiv (Rminus (Rmax a b) (Rmin a b)) (pos alpha)) (Rplus (INR n') (IZR (Zpos xH))) *)
(* Goal: and (pointed_subdiv (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n))) (and (@eq R (@SF_h R (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n))) (Rmin a b)) (@eq R (@last R (@SF_h R (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n))) (@SF_lx R (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n)))) (Rmax a b))) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
    apply cond_pos.
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@Riemann_sum V g (@SF_seq_f2 R (fun x y : R => Rdiv (Rplus x y) (IZR (Zpos (xO xH)))) (unif_part a b n))) (@Riemann_sum V f (@SF_seq_f2 R (fun x y : R => Rdiv (Rplus x y) (IZR (Zpos (xO xH)))) (unif_part a b n))) *)
  set n := (nfloor _ Hn).
(* Goal: @ex (@SF_seq R) (fun x : @SF_seq R => P x) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  exists (SF_seq_f2 (fun x y => x) (unif_part (Rmin a b) (Rmax a b) n)).
(* Goal: P (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n)) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  destruct (Riemann_fine_unif_part (fun x y => x) (Rmin a b) (Rmax a b) n).
(* Goal: forall (a b : R) (_ : Rle a b), and (Rle a a) (Rle a b) *)
(* Goal: Rle (Rmin a b) (Rmax a b) *)
(* Goal: P (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n)) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  intros u v Huv.
(* Goal: and (Rle u u) (Rle u v) *)
(* Goal: Rle (Rmin a b) (Rmax a b) *)
(* Goal: P (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n)) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  split.
(* Goal: Rle (@SF_h R s) (@SF_h R s) *)
(* Goal: Rle (@SF_h R s) (@nth R (IZR Z0) (@unzip1 R R (@map (prod R R) (prod R R) (fun x : prod R R => @pair R R (@fst R R x) (f (@snd R R x))) (@SF_t R s))) i) *)
  apply Rle_refl.
(* Goal: Rle u v *)
(* Goal: Rle (Rmin a b) (Rmax a b) *)
(* Goal: P (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n)) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  exact Huv.
(* Goal: Rle (Rmin a b) (Rmax a b) *)
(* Goal: P (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n)) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  exact Hab.
(* Goal: @eq R (@nth R (IZR Z0) (@behead R unif_part0) n0) (@nth R (Rplus a (Rdiv (Rmult (IZR Z0) (Rminus b a)) (Rplus (INR n) (IZR (Zpos xH))))) (@behead R unif_part0) n0) *)
(* Goal: @eq (list R) (@Datatypes.cons R (@head R (IZR Z0) unif_part0) (@behead R unif_part0)) unif_part0 *)
(* Goal: lt O (@size R (unif_part a b n)) *)
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@Riemann_sum V g l) (@Riemann_sum V f l) *)
  apply H.
(* Goal: Rlt (seq_step (@SF_lx R (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n)))) (pos alpha) *)
(* Goal: and (pointed_subdiv (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n))) (and (@eq R (@SF_h R (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n))) (Rmin a b)) (@eq R (@last R (@SF_h R (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n))) (@SF_lx R (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n)))) (Rmax a b))) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  apply Rle_lt_trans with (1 := H0).
(* Goal: Rlt (Rminus (Rmax a b) (Rmin a b)) (Rmult (Rplus (INR n') (IZR (Zpos xH))) (pos alpha)) *)
(* Goal: and (pointed_subdiv (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n))) (and (@eq R (@SF_h R (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n))) (Rmin a b)) (@eq R (@last R (@SF_h R (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n))) (@SF_lx R (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n)))) (Rmax a b))) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  apply Rlt_div_l.
(* Goal: Rgt (Rplus (INR n) (IZR (Zpos xH))) (IZR Z0) *)
(* Goal: Rlt (Rminus (Rmax a b) (Rmin a b)) (Rmult (pos alpha) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: and (pointed_subdiv (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n))) (and (@eq R (@SF_h R (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n))) (Rmin a b)) (@eq R (@last R (@SF_h R (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n))) (@SF_lx R (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n)))) (Rmax a b))) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  apply INRp1_pos.
(* Goal: Rlt (Rminus (Rmax a b) (Rmin a b)) (Rmult (pos alpha) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: and (pointed_subdiv (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n))) (and (@eq R (@SF_h R (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n))) (Rmin a b)) (@eq R (@last R (@SF_h R (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n))) (@SF_lx R (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n)))) (Rmax a b))) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  unfold n, nfloor.
(* Goal: Rlt (Rminus (Rmax a b) (Rmin a b)) (Rmult (pos alpha) (Rplus (INR (@proj1_sig nat (fun n : nat => and (Rle (INR n) (Rdiv (Rminus (Rmax a b) (Rmin a b)) (pos alpha))) (Rlt (Rdiv (Rminus (Rmax a b) (Rmin a b)) (pos alpha)) (Rplus (INR n) (IZR (Zpos xH))))) (nfloor_ex (Rdiv (Rminus (Rmax a b) (Rmin a b)) (pos alpha)) Hn))) (IZR (Zpos xH)))) *)
(* Goal: and (pointed_subdiv (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n))) (and (@eq R (@SF_h R (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n))) (Rmin a b)) (@eq R (@last R (@SF_h R (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n))) (@SF_lx R (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n)))) (Rmax a b))) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  destruct nfloor_ex as [n' Hn'].
(* Goal: @eq R (@SF_fun_aux R (@pair R R (@SF_h R (@SF_cons R h s)) z0) (@SF_t R (@SF_cons R h s)) z0 x) (@nth R (IZR Z0) (@SF_ly R (@SF_cons R h s)) i) *)
  simpl.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite Rmult_comm.
(* Goal: Rlt (Rminus (Rmax a b) (Rmin a b)) (Rmult (Rplus (INR n') (IZR (Zpos xH))) (pos alpha)) *)
(* Goal: and (pointed_subdiv (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n))) (and (@eq R (@SF_h R (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n))) (Rmin a b)) (@eq R (@last R (@SF_h R (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n))) (@SF_lx R (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n)))) (Rmax a b))) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  apply Rlt_div_l.
(* Goal: Rgt (pos alpha) (IZR Z0) *)
(* Goal: Rlt (Rdiv (Rminus (Rmax a b) (Rmin a b)) (pos alpha)) (Rplus (INR n') (IZR (Zpos xH))) *)
(* Goal: and (pointed_subdiv (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n))) (and (@eq R (@SF_h R (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n))) (Rmin a b)) (@eq R (@last R (@SF_h R (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n))) (@SF_lx R (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n)))) (Rmax a b))) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  apply cond_pos.
(* Goal: Rlt (Rdiv (Rminus (Rmax a b) (Rmin a b)) (pos alpha)) (Rplus (INR n') (IZR (Zpos xH))) *)
(* Goal: and (pointed_subdiv (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n))) (and (@eq R (@SF_h R (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n))) (Rmin a b)) (@eq R (@last R (@SF_h R (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n))) (@SF_lx R (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n)))) (Rmax a b))) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  apply Hn'.
(* Goal: and (pointed_subdiv (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n))) (and (@eq R (@SF_h R (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n))) (Rmin a b)) (@eq R (@last R (@SF_h R (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n))) (@SF_lx R (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n)))) (Rmax a b))) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  exact H1.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
- apply within_filter.
(* Goal: @Filter (@SF_seq R) (@locally_dist (@SF_seq R) (fun ptd : @SF_seq R => seq_step (@SF_lx R ptd))) *)
  apply locally_dist_filter.
Qed.

(** * Riemann sums *)

(** Riemann_sum *)

Section Riemann_sum.

Context {V : ModuleSpace R_Ring}.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Definition Riemann_sum (f : R -> V) (ptd : SF_seq) : V :=
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  foldr plus zero (pairmap (fun x y => (scal (fst y - fst x) (f (snd y)))) (SF_h ptd,zero) (SF_t ptd)).

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma Riemann_sum_cons (f : R -> V) (h0 : R * R) (ptd : SF_seq) :
  Riemann_sum f (SF_cons h0 ptd) =
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
    plus (scal (SF_h ptd - fst h0) (f (snd h0))) (Riemann_sum f ptd).
Proof.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite /Riemann_sum /=.
  case: h0 => x0 y0 ;
  apply SF_cons_dec with (s := ptd) => {ptd} [ x1 | [x1 y1] ptd ] //=.
Qed.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma Riemann_sum_rcons (f : R -> V) ptd l0 :
  Riemann_sum f (SF_rcons ptd l0) =
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
    plus (Riemann_sum f ptd) (scal (fst l0 - last (SF_h ptd) (SF_lx ptd)) (f (snd l0))).
Proof.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite /Riemann_sum .
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: l0 => x0 y0.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  apply SF_rcons_dec with (s := ptd) => {ptd} [ x1 | ptd [x1 y1]].
(* Goal: @eq (AbelianGroup.sort (ModuleSpace.AbelianGroup R_Ring V)) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus (@SF_h R s) x0) (g y0)) (Riemann_sum f s)) (@plus (ModuleSpace.AbelianGroup R_Ring V) (Riemann_sum f s) (@scal R_Ring V (Rminus (@SF_h R s) x0) (g y0))) *)
  apply plus_comm.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite ?SF_map_rcons /=.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite pairmap_rcons foldr_rcons /=.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite unzip1_rcons last_rcons /=.
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@Riemann_sum V g (@SF_seq_f2 R (fun x y : R => Rdiv (Rplus x y) (IZR (Zpos (xO xH)))) (unif_part a b n))) (@Riemann_sum V f (@SF_seq_f2 R (fun x y : R => Rdiv (Rplus x y) (IZR (Zpos (xO xH)))) (unif_part a b n))) *)
  set l := pairmap _ _ _.
(* Goal: @eq R (@SF_fun_aux R (@pair R R (@SF_h R (@SF_cons R h s)) z0) (@SF_t R (@SF_cons R h s)) z0 x) (@nth R (IZR Z0) (@SF_ly R (@SF_cons R h s)) i) *)
  induction l ; simpl.
(* Goal: @eq (AbelianGroup.sort (ModuleSpace.AbelianGroup R_Ring V)) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus (@SF_h R s) x0) (g y0)) (Riemann_sum f s)) (@plus (ModuleSpace.AbelianGroup R_Ring V) (Riemann_sum f s) (@scal R_Ring V (Rminus (@SF_h R s) x0) (g y0))) *)
  apply plus_comm.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite IHl.
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@plus (ModuleSpace.AbelianGroup R_Ring V) a (@plus (ModuleSpace.AbelianGroup R_Ring V) (@foldr (ModuleSpace.sort R_Ring V) (ModuleSpace.sort R_Ring V) (@plus (ModuleSpace.AbelianGroup R_Ring V)) (@zero (ModuleSpace.AbelianGroup R_Ring V)) l) (@scal R_Ring V (Rminus x0 x1) (f y0)))) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@plus (ModuleSpace.AbelianGroup R_Ring V) a (@foldr (ModuleSpace.sort R_Ring V) (ModuleSpace.sort R_Ring V) (@plus (ModuleSpace.AbelianGroup R_Ring V)) (@zero (ModuleSpace.AbelianGroup R_Ring V)) l)) (@scal R_Ring V (Rminus x0 x1) (f y0))) *)
  apply plus_assoc.
Qed.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma Riemann_sum_zero (f : R -> V) ptd :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  SF_sorted Rle ptd ->
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  SF_h ptd = last (SF_h ptd) (SF_lx ptd) ->
  Riemann_sum f ptd = zero.
Proof.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  apply SF_cons_ind with (s := ptd) => {ptd} [x0 | [x0 y0] ptd IH] //= Hs Hhl.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  rewrite Riemann_sum_cons IH /= => {IH}.
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus (@SF_h R ptd) x0) (f y0)) (@zero (ModuleSpace.AbelianGroup R_Ring V))) (@zero (ModuleSpace.AbelianGroup R_Ring V)) *)
(* Goal: @SF_sorted R Rle ptd *)
(* Goal: @eq R (@SF_h R ptd) (@last R (@SF_h R ptd) (@unzip1 R R (@SF_t R ptd))) *)
  replace x0 with (SF_h ptd).
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite Rminus_eq_0.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite plus_zero_r.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply: scal_zero_l.
(* Goal: @eq R (@SF_h R ptd) (@last R (@SF_h R ptd) (@unzip1 R R (@SF_t R ptd))) *)
  apply Rle_antisym.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  rewrite Hhl => {Hhl} /=.
(* Goal: Rle (@SF_h R ptd) (@last R (@SF_h R ptd) (@unzip1 R R (@SF_t R ptd))) *)
(* Goal: Rle (@last R (@SF_h R ptd) (@unzip1 R R (@SF_t R ptd))) (@SF_h R ptd) *)
  apply (sorted_last (SF_h ptd :: @map (R*R) R (@fst R R) (SF_t ptd)) O) with (x0 := 0).
(* Goal: @sorted R Rle (@Datatypes.cons R (@SF_h R ptd) (@map (prod R R) R (@fst R R) (@SF_t R ptd))) *)
(* Goal: lt O (@size R (@Datatypes.cons R (@SF_h R ptd) (@map (prod R R) R (@fst R R) (@SF_t R ptd)))) *)
(* Goal: Rle (@last R (@SF_h R ptd) (@unzip1 R R (@SF_t R ptd))) (@SF_h R ptd) *)
  replace ((SF_h ptd) :: map _ _) with (SF_lx ptd).
(* Goal: @SF_sorted R Rle (@SF_cons R h0 s) *)
(* Goal: lt i (S (@size R (@unzip1 R R (@SF_t R s)))) *)
  apply Hs.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  apply SF_cons_ind with (s := ptd) => {ptd Hs} [x1 | [x1 y1] ptd IH] //=.
(* Goal: lt O (@size R (@Datatypes.cons R (@SF_h R ptd) (@map (prod R R) R (@fst R R) (@SF_t R ptd)))) *)
(* Goal: Rle (@last R (@SF_h R ptd) (@unzip1 R R (@SF_t R ptd))) (@SF_h R ptd) *)
  apply lt_O_Sn.
(* Goal: @SF_sorted R Rle (@SF_cons R h0 s) *)
(* Goal: lt i (S (@size R (@unzip1 R R (@SF_t R s)))) *)
  apply Hs.
(* Goal: @SF_sorted R Rle (@SF_cons R h0 s) *)
(* Goal: lt i (S (@size R (@unzip1 R R (@SF_t R s)))) *)
  apply Hs.
(* Goal: @eq R (@SF_h R ptd) (@last R (@SF_h R ptd) (@unzip1 R R (@SF_t R ptd))) *)
  apply Rle_antisym.
(* Goal: Rle (@SF_h R ptd) (@last R (@SF_h R ptd) (@unzip1 R R (@SF_t R ptd))) *)
(* Goal: Rle (@last R (@SF_h R ptd) (@unzip1 R R (@SF_t R ptd))) (@SF_h R ptd) *)
  apply (sorted_last (SF_h ptd :: @map (R*R) R (@fst R R) (SF_t ptd)) O) with (x0 := 0).
(* Goal: @sorted R Rle (@Datatypes.cons R (@SF_h R ptd) (@map (prod R R) R (@fst R R) (@SF_t R ptd))) *)
(* Goal: lt O (@size R (@Datatypes.cons R (@SF_h R ptd) (@map (prod R R) R (@fst R R) (@SF_t R ptd)))) *)
(* Goal: Rle (@last R (@SF_h R ptd) (@unzip1 R R (@SF_t R ptd))) (@SF_h R ptd) *)
  replace ((SF_h ptd) :: map _ _) with (SF_lx ptd).
(* Goal: @SF_sorted R Rle (@SF_cons R h0 s) *)
(* Goal: lt i (S (@size R (@unzip1 R R (@SF_t R s)))) *)
  apply Hs.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  apply SF_cons_ind with (s := ptd) => {ptd Hs Hhl} [x1 | [x1 y1] ptd IH] //=.
(* Goal: lt O (@size R (@Datatypes.cons R (@SF_h R ptd) (@map (prod R R) R (@fst R R) (@SF_t R ptd)))) *)
(* Goal: Rle (@last R (@SF_h R ptd) (@unzip1 R R (@SF_t R ptd))) (@SF_h R ptd) *)
  apply lt_O_Sn.
(* Goal: @SF_sorted R Rle (@SF_cons R h0 s) *)
(* Goal: lt i (S (@size R (@unzip1 R R (@SF_t R s)))) *)
  move: Hhl ; rewrite -?(last_map (@fst R R)) /= => <- ; apply Hs.
Qed.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma Riemann_sum_map (f : R -> V) (g : R -> R) ptd :
  Riemann_sum (fun x => f (g x)) ptd = Riemann_sum f (SF_map g ptd).
Proof.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  apply SF_cons_ind with (s := ptd) => {ptd} [x0 | h ptd IH].
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by [].
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite SF_map_cons !Riemann_sum_cons /=.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by rewrite IH.
Qed.

Lemma Riemann_sum_const (v : V) ptd :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  Riemann_sum (fun _ => v) ptd = scal (last (SF_h ptd) (SF_lx ptd) - SF_h ptd) v.
Proof.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
apply SF_cons_ind with (s := ptd) => {ptd} [x0 | [x0 y0] s IH] /=.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
by rewrite /Riemann_sum /= Rminus_eq_0 scal_zero_l.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
rewrite Riemann_sum_cons IH /=.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
rewrite -scal_distr_r /=.
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@scal R_Ring V (@plus (Ring.AbelianGroup R_Ring) (Rminus (@SF_h R s) x0) (Rminus (@last R (@SF_h R s) (@unzip1 R R (@SF_t R s))) (@SF_h R s))) v) (@scal R_Ring V (Rminus (@last R (@SF_h R s) (@unzip1 R R (@SF_t R s))) x0) v) *)
apply (f_equal (fun x => scal x v)).
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
rewrite /plus /=.
(* Goal: @eq R (Rplus (Rminus (@SF_h R s) x0) (Rminus (@last R (@SF_h R s) (@unzip1 R R (@SF_t R s))) (@SF_h R s))) (Rminus (@last R (@SF_h R s) (@unzip1 R R (@SF_t R s))) x0) *)
ring.
Qed.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma Riemann_sum_scal (a : R) (f : R -> V) ptd :
  Riemann_sum (fun x => scal a (f x)) ptd = scal a (Riemann_sum f ptd).
Proof.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
apply SF_cons_ind with (s := ptd) => {ptd} /= [x0 | [x0 y0] s IH].
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
rewrite /Riemann_sum /=.
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@zero (ModuleSpace.AbelianGroup R_Ring V)) (@scal R_Ring V a (@zero (ModuleSpace.AbelianGroup R_Ring V))) *)
(* Goal: @eq (ModuleSpace.sort R_Ring V) (Riemann_sum (fun x : R => @scal R_Ring V a (f x)) (@SF_cons R (@pair R R x0 y0) s)) (@scal R_Ring V a (Riemann_sum f (@SF_cons R (@pair R R x0 y0) s))) *)
apply sym_eq. apply @scal_zero_r.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
rewrite !Riemann_sum_cons /= IH.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
rewrite scal_distr_l.
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus (@SF_h R s) x0) (@scal R_Ring V a (f y0))) (@scal R_Ring V a (Riemann_sum f s))) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V a (@scal R_Ring V (Rminus (@SF_h R s) x0) (f y0))) (@scal R_Ring V a (Riemann_sum f s))) *)
apply f_equal with (f := fun v => plus v _).
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
rewrite 2!scal_assoc.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
by rewrite /mult /= Rmult_comm.
Qed.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma Riemann_sum_opp (f : R -> V) ptd :
  Riemann_sum (fun x => opp (f x)) ptd = opp (Riemann_sum f ptd).
Proof.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
apply SF_cons_ind with (s := ptd) => {ptd} /= [x0 | [x0 y0] s IH].
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
rewrite /Riemann_sum /=.
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@zero (ModuleSpace.AbelianGroup R_Ring V)) (@opp (ModuleSpace.AbelianGroup R_Ring V) (@zero (ModuleSpace.AbelianGroup R_Ring V))) *)
(* Goal: @eq (ModuleSpace.sort R_Ring V) (Riemann_sum (fun x : R => @opp (ModuleSpace.AbelianGroup R_Ring V) (f x)) (@SF_cons R (@pair R R x0 y0) s)) (@opp (ModuleSpace.AbelianGroup R_Ring V) (Riemann_sum f (@SF_cons R (@pair R R x0 y0) s))) *)
apply sym_eq, @opp_zero.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
rewrite !Riemann_sum_cons /= IH.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
rewrite opp_plus.
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus (@SF_h R s) x0) (@opp (ModuleSpace.AbelianGroup R_Ring V) (f y0))) (@opp (ModuleSpace.AbelianGroup R_Ring V) (Riemann_sum f s))) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@opp (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus (@SF_h R s) x0) (f y0))) (@opp (ModuleSpace.AbelianGroup R_Ring V) (Riemann_sum f s))) *)
apply f_equal with (f := fun v => plus v (opp (Riemann_sum f s))).
(* Goal: @eq (AbelianGroup.sort (ModuleSpace.AbelianGroup R_Ring V)) (@scal R_Ring V (Rminus (@SF_h R s) x0) (@opp (ModuleSpace.AbelianGroup R_Ring V) (f y0))) (@opp (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus (@SF_h R s) x0) (f y0))) *)
apply scal_opp_r.
Qed.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma Riemann_sum_plus (f g : R -> V) ptd :
  Riemann_sum (fun x => plus (f x) (g x)) ptd =
    plus (Riemann_sum f ptd) (Riemann_sum g ptd).
Proof.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  apply SF_cons_ind with (s := ptd) => {ptd} /= [x0 | [x0 y0] s IH].
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite /Riemann_sum /=.
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@zero (ModuleSpace.AbelianGroup R_Ring V)) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@zero (ModuleSpace.AbelianGroup R_Ring V)) (@zero (ModuleSpace.AbelianGroup R_Ring V))) *)
(* Goal: @eq (ModuleSpace.sort R_Ring V) (Riemann_sum (fun x : R => @plus (ModuleSpace.AbelianGroup R_Ring V) (f x) (g x)) (@SF_cons R (@pair R R x0 y0) s)) (@plus (ModuleSpace.AbelianGroup R_Ring V) (Riemann_sum f (@SF_cons R (@pair R R x0 y0) s)) (Riemann_sum g (@SF_cons R (@pair R R x0 y0) s))) *)
  apply sym_eq, @plus_zero_l.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite !Riemann_sum_cons /= ; rewrite IH.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite scal_distr_l.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  rewrite -!plus_assoc.
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus (@SF_h R s) x0) (f y0)) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus (@SF_h R s) x0) (g y0)) (@plus (ModuleSpace.AbelianGroup R_Ring V) (Riemann_sum f s) (Riemann_sum g s)))) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus (@SF_h R s) x0) (f y0)) (@plus (ModuleSpace.AbelianGroup R_Ring V) (Riemann_sum f s) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus (@SF_h R s) x0) (g y0)) (Riemann_sum g s)))) *)
  apply f_equal.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite !plus_assoc.
(* Goal: @eq (AbelianGroup.sort (ModuleSpace.AbelianGroup R_Ring V)) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus (@SF_h R s) x0) (g y0)) (Riemann_sum f s)) (Riemann_sum g s)) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@plus (ModuleSpace.AbelianGroup R_Ring V) (Riemann_sum f s) (@scal R_Ring V (Rminus (@SF_h R s) x0) (g y0))) (Riemann_sum g s)) *)
  apply (f_equal (fun x => plus x (Riemann_sum g s))).
(* Goal: @eq (AbelianGroup.sort (ModuleSpace.AbelianGroup R_Ring V)) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus (@SF_h R s) x0) (g y0)) (Riemann_sum f s)) (@plus (ModuleSpace.AbelianGroup R_Ring V) (Riemann_sum f s) (@scal R_Ring V (Rminus (@SF_h R s) x0) (g y0))) *)
  apply plus_comm.
Qed.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma Riemann_sum_minus (f g : R -> V) ptd :
  Riemann_sum (fun x => minus (f x) (g x)) ptd =
    minus (Riemann_sum f ptd) (Riemann_sum g ptd).
Proof.
(* Goal: @eq (ModuleSpace.sort R_Ring V) (Riemann_sum (fun x : R => @minus (ModuleSpace.AbelianGroup R_Ring V) (f x) (g x)) ptd) (@minus (ModuleSpace.AbelianGroup R_Ring V) (Riemann_sum f ptd) (Riemann_sum g ptd)) *)
unfold minus.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
rewrite -Riemann_sum_opp.
(* Goal: @eq (ModuleSpace.sort R_Ring V) (Riemann_sum (fun x : R => @plus (ModuleSpace.AbelianGroup R_Ring V) (f x) (@opp (ModuleSpace.AbelianGroup R_Ring V) (g x))) ptd) (@plus (ModuleSpace.AbelianGroup R_Ring V) (Riemann_sum f ptd) (Riemann_sum (fun x : R => @opp (ModuleSpace.AbelianGroup R_Ring V) (g x)) ptd)) *)
apply Riemann_sum_plus.
Qed.

End Riemann_sum.

Section Riemann_sum_Normed.

Context {V : NormedModule R_AbsRing}.

Lemma Riemann_sum_Chasles_0
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  (f : R -> V) (M : R) (x : R) ptd :
  forall (eps : posreal),
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  (forall x, SF_h ptd <= x <= last (SF_h ptd) (SF_lx ptd) -> norm (f x) < M) ->
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  SF_h ptd <= x <= last (SF_h ptd) (SF_lx ptd) ->
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  pointed_subdiv ptd ->
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  seq_step (SF_lx ptd) < eps ->
  norm (minus (plus (Riemann_sum f (SF_cut_down ptd x)) (Riemann_sum f (SF_cut_up ptd x)))
    (Riemann_sum f ptd)) < 2 * eps * M.
Proof.
  intros eps.
  apply (SF_cons_ind (T := R)) with (s := ptd)
    => {ptd} /= [ x0 | [x0 y1] ptd IH] /= Hfx [ Hx0 Hl] Hptd Hstep.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  + rewrite (Rle_antisym _ _ Hx0 Hl) ; clear -Hfx.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    rewrite /Riemann_sum /=.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    case: Rle_dec (Rle_refl x) => //= _ _.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    rewrite ?plus_zero_r Rminus_eq_0.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    rewrite scal_zero_l.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    rewrite /minus plus_zero_l norm_opp norm_zero.
    apply Rmult_lt_0_compat.
    apply Rmult_lt_0_compat.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    by apply Rlt_0_2.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    by apply eps.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    by apply Rle_lt_trans with (2:= (Hfx x0 (conj (Rle_refl _) (Rle_refl _)))), norm_ge_0.
(* Goal: forall (i : nat) (_ : lt (S i) (@size R (unif_part a b n))), @eq R (Rminus (@nth R (IZR Z0) (unif_part a b n) (S i)) (@nth R (IZR Z0) (unif_part a b n) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
  + case: (Rle_dec (SF_h ptd) x) => Hx1.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
    - replace (minus (plus (Riemann_sum f (SF_cut_down (SF_cons (x0, y1) ptd) x))
        (Riemann_sum f (SF_cut_up (SF_cons (x0, y1) ptd) x)))
        (Riemann_sum f (SF_cons (x0, y1) ptd)))
        with (minus (plus (Riemann_sum f (SF_cut_down ptd x))
          (Riemann_sum f (SF_cut_up ptd x))) (Riemann_sum f ptd)).
(* Goal: Rle (@Riemann_sum R_ModuleSpace f s) (@Riemann_sum R_ModuleSpace g s) *)
      apply IH.
      intros y Hy.
      apply Hfx.
(* Goal: and (Rle u u) (Rle u v) *)
(* Goal: Rle (Rmin a b) (Rmax a b) *)
(* Goal: P (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n)) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
      split.
      apply Rle_trans with y1.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
      by apply (Hptd O (lt_O_Sn _)).
      apply Rle_trans with (SF_h ptd).
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
      by apply (Hptd O (lt_O_Sn _)).
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
      by apply Hy.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
      by apply Hy.
(* Goal: and (Rle u u) (Rle u v) *)
(* Goal: Rle (Rmin a b) (Rmax a b) *)
(* Goal: P (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n)) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
      by split.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
      by apply ptd_cons in Hptd.
      apply Rle_lt_trans with (2 := Hstep).
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
      by apply Rmax_r.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
      rewrite SF_cut_down_cons_2.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
      rewrite SF_cut_up_cons_2.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
      rewrite /minus 2?(Riemann_sum_cons _ (x0, y1)) SF_cut_down_h.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
      rewrite opp_plus plus_assoc /=.
      apply (f_equal (fun x => plus x _)).
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
      rewrite (plus_comm (scal (SF_h ptd - x0) (f y1))) -2!plus_assoc.
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus (@SF_h R s) x0) (f y0)) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus (@SF_h R s) x0) (g y0)) (@plus (ModuleSpace.AbelianGroup R_Ring V) (Riemann_sum f s) (Riemann_sum g s)))) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus (@SF_h R s) x0) (f y0)) (@plus (ModuleSpace.AbelianGroup R_Ring V) (Riemann_sum f s) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus (@SF_h R s) x0) (g y0)) (Riemann_sum g s)))) *)
      apply f_equal.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
      by rewrite plus_comm -plus_assoc plus_opp_l plus_zero_r.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
      by [].
      split ; [ | apply Hx1].
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
      apply Rle_trans with y1 ; by apply (Hptd O (lt_O_Sn _)).
      split ; [ | apply Hx1].
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
      apply Rle_trans with y1 ; by apply (Hptd O (lt_O_Sn _)).
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
    - apply Rnot_le_lt in Hx1.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
      rewrite SF_cut_down_cons_1 /=.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
      rewrite SF_cut_up_cons_1 /=.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
      rewrite 3!Riemann_sum_cons /= => {IH}.
(* Goal: @eq (@SF_seq T0) (@SF_cons T0 (@pair R T0 (@fst R T h0) (f (@snd R T h0))) (@SF_rcons T0 (SF_map f s) (@pair R T0 (@fst R T h) (f (@snd R T h))))) (@SF_rcons T0 (@SF_cons T0 (@pair R T0 (@fst R T h0) (f (@snd R T h0))) (SF_map f s)) (@pair R T0 (@fst R T h) (f (@snd R T h)))) *)
      replace (Riemann_sum f (SF_nil x) : V) with (zero : V) by auto.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
      rewrite plus_zero_r /minus opp_plus.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
      rewrite (plus_comm (opp (scal (SF_h ptd - x0) (f y1)))).
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
      rewrite ?plus_assoc -(plus_assoc _ _ (opp (Riemann_sum f ptd))).
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
      rewrite plus_opp_r plus_zero_r.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
      rewrite -scal_opp_l.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
      rewrite /opp /= Ropp_minus_distr.
(* Goal: @eq Rlist (@subdivision_val a b (SF_inf_r f a b n)) (if Rle_dec a b then seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part a b n))) else seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part b a n)))) *)
      rewrite /Rmin /Rmax ; case: Rle_dec => _.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
      rewrite (plus_comm (scal (x - x0) (f y1))) -plus_assoc.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
      rewrite -scal_distr_r /plus /= -/plus.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
      ring_simplify (x - x0 + (x0 - SF_h ptd)).
      eapply Rle_lt_trans.
      apply @norm_triangle.
(* Goal: forall (i : nat) (_ : lt (S i) (@size R (unif_part a b n))), @eq R (Rminus (@nth R (IZR Z0) (unif_part a b n) (S i)) (@nth R (IZR Z0) (unif_part a b n) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
      replace (2 * eps * M) with (eps * M + eps * M) by ring.
      apply Rplus_lt_compat ;
        eapply Rle_lt_trans ; try (apply @norm_scal) ;
        apply Rmult_le_0_lt_compat.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
      by apply Rabs_pos.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
      by apply norm_ge_0.
      apply Rle_lt_trans with (2 := Hstep).
(* Goal: Rle (Rabs (Rminus (@SF_h R s) x)) (Rmax (Rabs (Rminus (@SF_h R s) x1)) (@foldr R R Rmax (IZR Z0) (@pairmap R R (fun x y : R => Rabs (Rminus y x)) (@SF_h R s) (@unzip1 R R (@SF_t R s))))) *)
(* Goal: Rle (@foldr R R Rmax (IZR Z0) (@pairmap R R (fun x y : R => Rabs (Rminus y x)) (@SF_h R s) (@unzip1 R R (@SF_t R s)))) (Rmax (Rabs (Rminus (@SF_h R s) x1)) (@foldr R R Rmax (IZR Z0) (@pairmap R R (fun x y : R => Rabs (Rminus y x)) (@SF_h R s) (@unzip1 R R (@SF_t R s))))) *)
      apply Rle_trans with (2 := Rmax_l _ _).
(* Goal: @eq R (@SF_fun_aux R (@pair R R (@SF_h R (@SF_cons R h s)) z0) (@SF_t R (@SF_cons R h s)) z0 x) (@nth R (IZR Z0) (@SF_ly R (@SF_cons R h s)) i) *)
      simpl.
      apply Rlt_le in Hx1.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
      move: (Rle_trans _ _ _ Hx0 Hx1) => Hx0'.
      apply Rminus_le_0 in Hx1.
      apply Rminus_le_0 in Hx0'.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
      rewrite /abs /= ?Rabs_pos_eq //.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
      by apply Rplus_le_compat_l, Ropp_le_contravar.
      apply Hfx.
(* Goal: and (Rle u u) (Rle u v) *)
(* Goal: Rle (Rmin a b) (Rmax a b) *)
(* Goal: P (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n)) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
      by split.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
      by apply Rabs_pos.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
      by apply norm_ge_0.
      apply Rle_lt_trans with (2 := Hstep).
(* Goal: Rle (Rabs (Rminus (@SF_h R s) x)) (Rmax (Rabs (Rminus (@SF_h R s) x1)) (@foldr R R Rmax (IZR Z0) (@pairmap R R (fun x y : R => Rabs (Rminus y x)) (@SF_h R s) (@unzip1 R R (@SF_t R s))))) *)
(* Goal: Rle (@foldr R R Rmax (IZR Z0) (@pairmap R R (fun x y : R => Rabs (Rminus y x)) (@SF_h R s) (@unzip1 R R (@SF_t R s)))) (Rmax (Rabs (Rminus (@SF_h R s) x1)) (@foldr R R Rmax (IZR Z0) (@pairmap R R (fun x y : R => Rabs (Rminus y x)) (@SF_h R s) (@unzip1 R R (@SF_t R s))))) *)
      apply Rle_trans with (2 := Rmax_l _ _).
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
      rewrite /abs /plus /= -Ropp_minus_distr Rabs_Ropp.
      apply Rlt_le in Hx1.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
      move: (Rle_trans _ _ _ Hx0 Hx1) => Hx0'.
      apply Rminus_le_0 in Hx1.
      apply Rminus_le_0 in Hx0'.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
      rewrite ?Rabs_pos_eq //.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
      by apply Rplus_le_compat_l, Ropp_le_contravar.
      apply Hfx.
(* Goal: and (Rle u u) (Rle u v) *)
(* Goal: Rle (Rmin a b) (Rmax a b) *)
(* Goal: P (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n)) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
      split.
      apply (Hptd O (lt_O_Sn _)).
      apply Rle_trans with (SF_h ptd).
      apply (Hptd O (lt_O_Sn _)).
      apply (fun H => sorted_last ((SF_h ptd) :: (unzip1 (SF_t ptd))) O H (lt_O_Sn _) (SF_h ptd)).
      apply ptd_sort in Hptd.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
      by apply Hptd.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
      rewrite -plus_assoc -scal_distr_r /plus /= -/plus.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
      replace (SF_h ptd - x + (x0 - SF_h ptd)) with (opp (x - x0)) by (rewrite /opp /= ; ring).
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
      rewrite scal_opp_l -scal_opp_r.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
      rewrite -scal_distr_l.
      eapply Rle_lt_trans. apply @norm_scal.
(* Goal: forall (i : nat) (_ : lt (S i) (@size R (unif_part a b n))), @eq R (Rminus (@nth R (IZR Z0) (unif_part a b n) (S i)) (@nth R (IZR Z0) (unif_part a b n) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
      replace (2 * eps * M) with (eps * (M + M)) by ring.
      apply Rmult_le_0_lt_compat.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
      by apply Rabs_pos.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
      by apply norm_ge_0.
      apply Rle_lt_trans with (2 := Hstep).
(* Goal: Rle (Rabs (Rminus (@SF_h R s) x)) (Rmax (Rabs (Rminus (@SF_h R s) x1)) (@foldr R R Rmax (IZR Z0) (@pairmap R R (fun x y : R => Rabs (Rminus y x)) (@SF_h R s) (@unzip1 R R (@SF_t R s))))) *)
(* Goal: Rle (@foldr R R Rmax (IZR Z0) (@pairmap R R (fun x y : R => Rabs (Rminus y x)) (@SF_h R s) (@unzip1 R R (@SF_t R s)))) (Rmax (Rabs (Rminus (@SF_h R s) x1)) (@foldr R R Rmax (IZR Z0) (@pairmap R R (fun x y : R => Rabs (Rminus y x)) (@SF_h R s) (@unzip1 R R (@SF_t R s))))) *)
      apply Rle_trans with (2 := Rmax_l _ _).
(* Goal: @eq R (@SF_fun_aux R (@pair R R (@SF_h R (@SF_cons R h s)) z0) (@SF_t R (@SF_cons R h s)) z0 x) (@nth R (IZR Z0) (@SF_ly R (@SF_cons R h s)) i) *)
      simpl.
      apply Rlt_le in Hx1.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
      move: (Rle_trans _ _ _ Hx0 Hx1) => Hx0'.
      apply Rminus_le_0 in Hx0.
      apply Rminus_le_0 in Hx0'.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
      rewrite /abs /= ?Rabs_pos_eq //.
(* Goal: Rle (Rminus x x1) (Rminus (@SF_h R s) x1) *)
(* Goal: Rle (IZR Z0) (Rminus (@SF_h R s) x1) *)
(* Goal: Rle (IZR Z0) (Rminus x x1) *)
(* Goal: Rle (IZR Z0) (Rabs (Rminus (@SF_h R s) x1)) *)
      by apply Rplus_le_compat_r.
(* Goal: forall (i : nat) (_ : lt (S i) (@size R (unif_part a b n))), @eq R (Rminus (@nth R (IZR Z0) (unif_part a b n) (S i)) (@nth R (IZR Z0) (unif_part a b n) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
      apply Rle_lt_trans with (norm (f x) + norm (opp (f y1))).
      apply @norm_triangle.
      apply Rplus_lt_compat.
      apply Hfx.
(* Goal: and (Rle u u) (Rle u v) *)
(* Goal: Rle (Rmin a b) (Rmax a b) *)
(* Goal: P (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n)) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
      by split.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
      rewrite norm_opp.
      apply Hfx.
(* Goal: and (Rle u u) (Rle u v) *)
(* Goal: Rle (Rmin a b) (Rmax a b) *)
(* Goal: P (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n)) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
      split.
      apply (Hptd O (lt_O_Sn _)).
      apply Rle_trans with (SF_h ptd).
      apply (Hptd O (lt_O_Sn _)).
      apply (fun H => sorted_last ((SF_h ptd) :: (unzip1 (SF_t ptd))) O H (lt_O_Sn _) (SF_h ptd)).
      apply ptd_sort in Hptd.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
      by apply Hptd.
(* Goal: and (Rle u u) (Rle u v) *)
(* Goal: Rle (Rmin a b) (Rmax a b) *)
(* Goal: P (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n)) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
      by split.
(* Goal: and (Rle u u) (Rle u v) *)
(* Goal: Rle (Rmin a b) (Rmax a b) *)
(* Goal: P (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n)) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
      by split.
Qed.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma Riemann_sum_norm (f : R -> V) (g : R -> R) ptd :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  pointed_subdiv ptd ->
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  (forall t, SF_h ptd <= t <= last (SF_h ptd) (SF_lx ptd) -> norm (f t) <= g t)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  -> norm (Riemann_sum f ptd) <= Riemann_sum g ptd.
Proof.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  apply SF_cons_ind with (s := ptd) => {ptd} /= [x0 | [x0 y0] s IH] /= Hs H.
  rewrite norm_zero ; exact: Rle_refl.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite !Riemann_sum_cons /=.
  eapply Rle_trans.
  by apply @norm_triangle.
(* Goal: Rle (@plus (ModuleSpace.AbelianGroup R_Ring R_ModuleSpace) (@scal R_Ring R_ModuleSpace (Rminus (@SF_h R s) x0) (f y0)) (@Riemann_sum R_ModuleSpace f s)) (@plus (ModuleSpace.AbelianGroup R_Ring R_ModuleSpace) (@scal R_Ring R_ModuleSpace (Rminus (@SF_h R s) x0) (g y0)) (@Riemann_sum R_ModuleSpace g s)) *)
  apply Rplus_le_compat.
  eapply Rle_trans. apply @norm_scal.
(* Goal: Rle (@scal R_Ring R_ModuleSpace (Rminus (@SF_h R s) x0) (f y0)) (@scal R_Ring R_ModuleSpace (Rminus (@SF_h R s) x0) (g y0)) *)
(* Goal: Rle (@Riemann_sum R_ModuleSpace f s) (@Riemann_sum R_ModuleSpace g s) *)
  refine (_ (Hs O _)).
(* Goal: @eq R (@SF_fun_aux R (@pair R R (@SF_h R (@SF_cons R h s)) z0) (@SF_t R (@SF_cons R h s)) z0 x) (@nth R (IZR Z0) (@SF_ly R (@SF_cons R h s)) i) *)
  simpl.
(* Goal: forall _ : and (Rle x0 y0) (Rle y0 (@SF_h R s)), Rle (@scal R_Ring R_ModuleSpace (Rminus (@SF_h R s) x0) (f y0)) (@scal R_Ring R_ModuleSpace (Rminus (@SF_h R s) x0) (g y0)) *)
(* Goal: lt O (@SF_size R (@SF_cons R (@pair R R x0 y0) s)) *)
(* Goal: Rle (@Riemann_sum R_ModuleSpace f s) (@Riemann_sum R_ModuleSpace g s) *)
  intros [H1 H2].
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite /abs /= Rabs_pos_eq.
(* Goal: Rle (@scal R_Ring R_ModuleSpace (Rminus (@SF_h R s) x0) (f y0)) (@scal R_Ring R_ModuleSpace (Rminus (@SF_h R s) x0) (g y0)) *)
(* Goal: lt O (@SF_size R (@SF_cons R (@pair R R x0 y0) s)) *)
(* Goal: Rle (@Riemann_sum R_ModuleSpace f s) (@Riemann_sum R_ModuleSpace g s) *)
  apply Rmult_le_compat_l.
(* Goal: Rle (IZR Z0) (Rminus (@SF_h R s) x0) *)
(* Goal: Rle (f y0) (g y0) *)
(* Goal: lt O (@SF_size R (@SF_cons R (@pair R R x0 y0) s)) *)
(* Goal: Rle (@Riemann_sum R_ModuleSpace f s) (@Riemann_sum R_ModuleSpace g s) *)
  apply -> Rminus_le_0.
(* Goal: Rle x0 (@SF_h R s) *)
(* Goal: Rle (f y0) (g y0) *)
(* Goal: lt O (@SF_size R (@SF_cons R (@pair R R x0 y0) s)) *)
(* Goal: Rle (@Riemann_sum R_ModuleSpace f s) (@Riemann_sum R_ModuleSpace g s) *)
  now apply Rle_trans with y0.
(* Goal: @eq R (@nth R (IZR Z0) (@behead R unif_part0) n0) (@nth R (Rplus a (Rdiv (Rmult (IZR Z0) (Rminus b a)) (Rplus (INR n) (IZR (Zpos xH))))) (@behead R unif_part0) n0) *)
(* Goal: @eq (list R) (@Datatypes.cons R (@head R (IZR Z0) unif_part0) (@behead R unif_part0)) unif_part0 *)
(* Goal: lt O (@size R (unif_part a b n)) *)
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@Riemann_sum V g l) (@Riemann_sum V f l) *)
  apply H.
(* Goal: and (Rle x0 y0) (Rle y0 (@last R (@SF_h R s) (@unzip1 R R (@SF_t R s)))) *)
(* Goal: lt O (@SF_size R (@SF_cons R (@pair R R x0 y0) s)) *)
(* Goal: Rle (@Riemann_sum R_ModuleSpace f s) (@Riemann_sum R_ModuleSpace g s) *)
  apply (conj H1).
(* Goal: Rle y0 (@last R (@SF_h R s) (@unzip1 R R (@SF_t R s))) *)
(* Goal: lt O (@SF_size R (@SF_cons R (@pair R R x0 y0) s)) *)
(* Goal: Rle (@Riemann_sum R_ModuleSpace f s) (@Riemann_sum R_ModuleSpace g s) *)
  apply Rle_trans with (1 := H2).
(* Goal: Rle (@SF_h R s) (@last R (@SF_h R s) (@unzip1 R R (@SF_t R s))) *)
(* Goal: lt O (@SF_size R (@SF_cons R (@pair R R x0 y0) s)) *)
(* Goal: Rle (@Riemann_sum R_ModuleSpace f s) (@Riemann_sum R_ModuleSpace g s) *)
  apply (sorted_last (SF_lx s) O) with (x0 := 0).
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply (ptd_sort _ Hs).
(* Goal: lt O (@SF_size R (@SF_cons R (@pair R R x0 y0) s)) *)
(* Goal: Rle (@Riemann_sum R_ModuleSpace f s) (@Riemann_sum R_ModuleSpace g s) *)
  exact: lt_O_Sn.
(* Goal: Rle (IZR Z0) (Rminus (@SF_h R s) x0) *)
(* Goal: Rle (f y0) (g y0) *)
(* Goal: lt O (@SF_size R (@SF_cons R (@pair R R x0 y0) s)) *)
(* Goal: Rle (@Riemann_sum R_ModuleSpace f s) (@Riemann_sum R_ModuleSpace g s) *)
  apply -> Rminus_le_0.
(* Goal: Rle x0 (@SF_h R s) *)
(* Goal: Rle (f y0) (g y0) *)
(* Goal: lt O (@SF_size R (@SF_cons R (@pair R R x0 y0) s)) *)
(* Goal: Rle (@Riemann_sum R_ModuleSpace f s) (@Riemann_sum R_ModuleSpace g s) *)
  now apply Rle_trans with y0.
(* Goal: lt O (@SF_size R (@SF_cons R (@pair R R x0 y0) s)) *)
(* Goal: Rle (@Riemann_sum R_ModuleSpace f s) (@Riemann_sum R_ModuleSpace g s) *)
  exact: lt_O_Sn.
(* Goal: Rle (@Riemann_sum R_ModuleSpace f s) (@Riemann_sum R_ModuleSpace g s) *)
  apply IH.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply ptd_cons with (h := (x0,y0)).
(* Goal: and (Rle u u) (Rle u v) *)
(* Goal: Rle (Rmin a b) (Rmax a b) *)
(* Goal: P (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n)) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  move => t Ht ; apply H ; split.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply Rle_trans with (2 := proj1 Ht), (ptd_sort _ Hs).
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply Ht.
Qed.

End Riemann_sum_Normed.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma Riemann_sum_le (f : R -> R) (g : R -> R) ptd :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  pointed_subdiv ptd ->
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  (forall t, SF_h ptd <= t <= last (SF_h ptd) (SF_lx ptd) -> f t <= g t) ->
  Riemann_sum f ptd <= Riemann_sum g ptd.
Proof.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  apply SF_cons_ind with (s := ptd) => {ptd} /= [x0 | [x0 y0] s IH] /= Hs H.
(* Goal: Rle (@SF_h R s) (@SF_h R s) *)
(* Goal: Rle (@SF_h R s) (@nth R (IZR Z0) (@unzip1 R R (@map (prod R R) (prod R R) (fun x : prod R R => @pair R R (@fst R R x) (f (@snd R R x))) (@SF_t R s))) i) *)
  apply Rle_refl.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite !Riemann_sum_cons /=.
(* Goal: Rle (@plus (ModuleSpace.AbelianGroup R_Ring R_ModuleSpace) (@scal R_Ring R_ModuleSpace (Rminus (@SF_h R s) x0) (f y0)) (@Riemann_sum R_ModuleSpace f s)) (@plus (ModuleSpace.AbelianGroup R_Ring R_ModuleSpace) (@scal R_Ring R_ModuleSpace (Rminus (@SF_h R s) x0) (g y0)) (@Riemann_sum R_ModuleSpace g s)) *)
  apply Rplus_le_compat.
(* Goal: Rle (@scal R_Ring R_ModuleSpace (Rminus (@SF_h R s) x0) (f y0)) (@scal R_Ring R_ModuleSpace (Rminus (@SF_h R s) x0) (g y0)) *)
(* Goal: Rle (@Riemann_sum R_ModuleSpace f s) (@Riemann_sum R_ModuleSpace g s) *)
  refine (_ (Hs O _)).
(* Goal: @eq R (@SF_fun_aux R (@pair R R (@SF_h R (@SF_cons R h s)) z0) (@SF_t R (@SF_cons R h s)) z0 x) (@nth R (IZR Z0) (@SF_ly R (@SF_cons R h s)) i) *)
  simpl.
(* Goal: forall _ : and (Rle x0 y0) (Rle y0 (@SF_h R s)), Rle (@scal R_Ring R_ModuleSpace (Rminus (@SF_h R s) x0) (f y0)) (@scal R_Ring R_ModuleSpace (Rminus (@SF_h R s) x0) (g y0)) *)
(* Goal: lt O (@SF_size R (@SF_cons R (@pair R R x0 y0) s)) *)
(* Goal: Rle (@Riemann_sum R_ModuleSpace f s) (@Riemann_sum R_ModuleSpace g s) *)
  intros [H1 H2].
(* Goal: Rle (@scal R_Ring R_ModuleSpace (Rminus (@SF_h R s) x0) (f y0)) (@scal R_Ring R_ModuleSpace (Rminus (@SF_h R s) x0) (g y0)) *)
(* Goal: lt O (@SF_size R (@SF_cons R (@pair R R x0 y0) s)) *)
(* Goal: Rle (@Riemann_sum R_ModuleSpace f s) (@Riemann_sum R_ModuleSpace g s) *)
  apply Rmult_le_compat_l.
(* Goal: Rle (IZR Z0) (Rminus (@SF_h R s) x0) *)
(* Goal: Rle (f y0) (g y0) *)
(* Goal: lt O (@SF_size R (@SF_cons R (@pair R R x0 y0) s)) *)
(* Goal: Rle (@Riemann_sum R_ModuleSpace f s) (@Riemann_sum R_ModuleSpace g s) *)
  apply -> Rminus_le_0.
(* Goal: Rle x0 (@SF_h R s) *)
(* Goal: Rle (f y0) (g y0) *)
(* Goal: lt O (@SF_size R (@SF_cons R (@pair R R x0 y0) s)) *)
(* Goal: Rle (@Riemann_sum R_ModuleSpace f s) (@Riemann_sum R_ModuleSpace g s) *)
  now apply Rle_trans with y0.
(* Goal: @eq R (@nth R (IZR Z0) (@behead R unif_part0) n0) (@nth R (Rplus a (Rdiv (Rmult (IZR Z0) (Rminus b a)) (Rplus (INR n) (IZR (Zpos xH))))) (@behead R unif_part0) n0) *)
(* Goal: @eq (list R) (@Datatypes.cons R (@head R (IZR Z0) unif_part0) (@behead R unif_part0)) unif_part0 *)
(* Goal: lt O (@size R (unif_part a b n)) *)
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@Riemann_sum V g l) (@Riemann_sum V f l) *)
  apply H.
(* Goal: and (Rle x0 y0) (Rle y0 (@last R (@SF_h R s) (@unzip1 R R (@SF_t R s)))) *)
(* Goal: lt O (@SF_size R (@SF_cons R (@pair R R x0 y0) s)) *)
(* Goal: Rle (@Riemann_sum R_ModuleSpace f s) (@Riemann_sum R_ModuleSpace g s) *)
  apply (conj H1).
(* Goal: Rle y0 (@last R (@SF_h R s) (@unzip1 R R (@SF_t R s))) *)
(* Goal: lt O (@SF_size R (@SF_cons R (@pair R R x0 y0) s)) *)
(* Goal: Rle (@Riemann_sum R_ModuleSpace f s) (@Riemann_sum R_ModuleSpace g s) *)
  apply Rle_trans with (1 := H2).
(* Goal: Rle (@SF_h R s) (@last R (@SF_h R s) (@unzip1 R R (@SF_t R s))) *)
(* Goal: lt O (@SF_size R (@SF_cons R (@pair R R x0 y0) s)) *)
(* Goal: Rle (@Riemann_sum R_ModuleSpace f s) (@Riemann_sum R_ModuleSpace g s) *)
  apply (sorted_last (SF_lx s) O) with (x0 := 0).
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply (ptd_sort _ Hs).
(* Goal: lt O (@SF_size R (@SF_cons R (@pair R R x0 y0) s)) *)
(* Goal: Rle (@Riemann_sum R_ModuleSpace f s) (@Riemann_sum R_ModuleSpace g s) *)
  exact: lt_O_Sn.
(* Goal: lt O (@SF_size R (@SF_cons R (@pair R R x0 y0) s)) *)
(* Goal: Rle (@Riemann_sum R_ModuleSpace f s) (@Riemann_sum R_ModuleSpace g s) *)
  exact: lt_O_Sn.
(* Goal: Rle (@Riemann_sum R_ModuleSpace f s) (@Riemann_sum R_ModuleSpace g s) *)
  apply IH.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply ptd_cons with (h := (x0,y0)).
(* Goal: and (Rle u u) (Rle u v) *)
(* Goal: Rle (Rmin a b) (Rmax a b) *)
(* Goal: P (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n)) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  move => t Ht ; apply H ; split.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply Rle_trans with (2 := proj1 Ht), (ptd_sort _ Hs).
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply Ht.
Qed.

(** Structures *)

Lemma Riemann_sum_pair {U : ModuleSpace R_Ring} {V : ModuleSpace R_Ring}
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  (f : R -> U * V) ptd :
  Riemann_sum f ptd =
    (Riemann_sum (fun t => fst (f t)) ptd, Riemann_sum (fun t => snd (f t)) ptd).
Proof.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  apply SF_cons_ind with (s := ptd) => {ptd} [x0 | h0 ptd IH].
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by [].
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite !Riemann_sum_cons IH.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply injective_projections.
Qed.

(** RInt_val *)

Section RInt_val.

Context {V : ModuleSpace R_Ring}.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Definition RInt_val (f : R -> V) (a b : R) (n : nat) :=
(* Goal: forall (i : nat) (_ : lt (S i) (@size R (unif_part a b n))), @eq R (Rminus (@nth R (IZR Z0) (unif_part a b n) (S i)) (@nth R (IZR Z0) (unif_part a b n) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
  Riemann_sum f (SF_seq_f2 (fun x y => (x + y) / 2) (unif_part a b n)).

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma RInt_val_point (f : R -> V) (a : R) (n : nat) :
  RInt_val f a a n = zero.
Proof.
(* Goal: @eq (ModuleSpace.sort R_Ring V) (RInt_val f a a n) (@zero (ModuleSpace.AbelianGroup R_Ring V)) *)
  unfold RInt_val ; apply Riemann_sum_zero.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite /SF_sorted SF_lx_f2.
(* Goal: Rle (@SF_h R s) (@SF_h R s) *)
(* Goal: Rle (@SF_h R s) (@nth R (IZR Z0) (@unzip1 R R (@map (prod R R) (prod R R) (fun x : prod R R => @pair R R (@fst R R x) (f (@snd R R x))) (@SF_t R s))) i) *)
  apply unif_part_sort ; apply Rle_refl.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite size_mkseq ; by apply lt_O_Sn.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite SF_lx_f2 /=.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  rewrite -{2}[1]/(INR 1) last_map.
(* Goal: @eq R (Rplus (Rminus (@SF_h R s) x0) (Rminus (@last R (@SF_h R s) (@unzip1 R R (@SF_t R s))) (@SF_h R s))) (Rminus (@last R (@SF_h R s) (@unzip1 R R (@SF_t R s))) x0) *)
  unfold Rdiv ; ring.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply lt_O_Sn.
Qed.

Lemma RInt_val_swap :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  forall (f : R -> V) (a b : R) (n : nat),
  RInt_val f a b n = opp (RInt_val f b a n).
Proof.
  intros f a b n.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite /RInt_val.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  rewrite -Riemann_sum_opp.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite unif_part_bound.
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) x) (Rlt x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i)) *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  elim: (unif_part b a n) => [ | x1 s IH] /=.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by [].
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  clear -IH.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite rev_cons.
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@scal R_Ring V u (@Riemann_sum V (fun x : R => f (Rplus (Rmult u x) v)) (@SF_seq_f2 R (fun x y : R => Rdiv (Rplus x y) (IZR (Zpos (xO xH)))) (@Datatypes.cons R x1 s)))) (@Riemann_sum V f (@SF_seq_f2 R (fun x y : R => Rdiv (Rplus x y) (IZR (Zpos (xO xH)))) (@Datatypes.cons R (Rplus (Rmult u x1) v) (@map R R (fun x : R => Rplus (Rmult u x) v) s)))) *)
(* Goal: @eq (list R) (@map R R (fun x : R => Rplus (Rmult u x) v) (unif_part a b n)) (unif_part (Rplus (Rmult u a) v) (Rplus (Rmult u b) v) n) *)
  destruct s as [ | x2 s].
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by [].
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite SF_cons_f2.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  2: by apply lt_O_Sn.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  rewrite Riemann_sum_cons /= -IH => {IH}.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  rewrite scal_opp_r -scal_opp_l /=.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite rev_cons.
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) x) (Rlt x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i)) *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  elim: (rev s) => {s} /= [ | x3 s IH].
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite /Riemann_sum /=.
  apply (f_equal2 (fun x y => plus (scal x (f y)) _)) ;
(* Goal: @eq R (Rplus (Rminus (@SF_h R s) x0) (Rminus (@last R (@SF_h R s) (@unzip1 R R (@SF_t R s))) (@SF_h R s))) (Rminus (@last R (@SF_h R s) (@unzip1 R R (@SF_t R s))) x0) *)
    rewrite /Rdiv /opp /= ; ring.
(* Goal: adapted_couple (@SF_val_fun R f a b n) (@head R (IZR Z0) (@SF_lx R (@SF_val_seq R f a b n))) (@last R (IZR Z0) (@SF_lx R (@SF_val_seq R f a b n))) (seq2Rlist (unif_part a b n)) (seq2Rlist (@SF_val_ly R f a b n)) *)
(* Goal: @eq R (@last R (IZR Z0) (@SF_lx R (@SF_val_seq R f a b n))) b *)
  rewrite !SF_cons_f2 ; try (by rewrite size_rcons ; apply lt_O_Sn).
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  rewrite !Riemann_sum_cons /= IH !plus_assoc => {IH}.
  apply (f_equal (fun x => plus x _)).
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite plus_comm.
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus (@SF_h R s) x0) (f y0)) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus (@SF_h R s) x0) (g y0)) (@plus (ModuleSpace.AbelianGroup R_Ring V) (Riemann_sum f s) (Riemann_sum g s)))) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus (@SF_h R s) x0) (f y0)) (@plus (ModuleSpace.AbelianGroup R_Ring V) (Riemann_sum f s) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus (@SF_h R s) x0) (g y0)) (Riemann_sum g s)))) *)
  apply f_equal.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  apply (f_equal2 (fun x y => scal (x - x3)
(* Goal: forall (i : nat) (_ : lt (S i) (@size R (unif_part a b n))), @eq R (Rminus (@nth R (IZR Z0) (unif_part a b n) (S i)) (@nth R (IZR Z0) (unif_part a b n) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
  (f ((x3 + y) / 2)))) ; clear ;
  by elim: s.
Qed.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma RInt_val_ext (f g : R -> V) (a b : R) (n : nat) :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  (forall x, Rmin a b <= x <= Rmax a b -> f x = g x)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  -> RInt_val g a b n = RInt_val f a b n.
Proof.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  wlog: a b / (a <= b) => [Hw | Hab].
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    case: (Rle_lt_dec a b) => Hab.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    by apply Hw.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
    rewrite Rmin_comm Rmax_comm => Heq.
(* Goal: @eq (ModuleSpace.sort R_Ring V) (RInt_val g a b n) (RInt_val f a b n) *)
(* Goal: forall _ : forall (x : R) (_ : and (Rle (Rmin a b) x) (Rle x (Rmax a b))), @eq (ModuleSpace.sort R_Ring V) (f x) (g x), @eq (ModuleSpace.sort R_Ring V) (RInt_val g a b n) (RInt_val f a b n) *)
    apply Rlt_le in Hab.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
    rewrite RInt_val_swap Hw => //=.
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@opp (ModuleSpace.AbelianGroup R_Ring V) (RInt_val f b a n)) (RInt_val f a b n) *)
(* Goal: forall _ : forall (x : R) (_ : and (Rle (Rmin a b) x) (Rle x (Rmax a b))), @eq (ModuleSpace.sort R_Ring V) (f x) (g x), @eq (ModuleSpace.sort R_Ring V) (RInt_val g a b n) (RInt_val f a b n) *)
    apply sym_eq ; by apply RInt_val_swap.
(* Goal: @eq Rlist (@subdivision_val a b (SF_inf_r f a b n)) (if Rle_dec a b then seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part a b n))) else seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part b a n)))) *)
  rewrite /Rmin /Rmax ; case: Rle_dec => //= _ Heq.
(* Goal: @eq (ModuleSpace.sort R_Ring V) (RInt_val g a b n) (RInt_val f a b n) *)
  unfold RInt_val.
(* Goal: forall (i : nat) (_ : lt (S i) (@size R (unif_part a b n))), @eq R (Rminus (@nth R (IZR Z0) (unif_part a b n) (S i)) (@nth R (IZR Z0) (unif_part a b n) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
  set l := (SF_seq_f2 (fun x y : R => (x + y) / 2) (unif_part a b n)).
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  assert (forall i, (i < size (SF_ly l))%nat -> f (nth 0 (SF_ly l) i) = g (nth 0 (SF_ly l) i)).
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
    move => i Hi.
(* Goal: @eq (ModuleSpace.sort R_Ring V) (f (@nth R (IZR Z0) (@SF_ly R l) i)) (g (@nth R (IZR Z0) (@SF_ly R l) i)) *)
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@Riemann_sum V g l) (@Riemann_sum V f l) *)
    apply Heq.
(* Goal: forall (i : nat) (_ : lt (S i) (@size R (unif_part a b n))), @eq R (Rminus (@nth R (IZR Z0) (unif_part a b n) (S i)) (@nth R (IZR Z0) (unif_part a b n) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
    destruct (fun H0 => Riemann_fine_unif_part (fun x y : R => (x + y) / 2) a b n H0 Hab) as [H [H0 [H1 H2]]].
(* Goal: forall (a b : R) (_ : Rle a b), and (Rle a (Rdiv (Rplus a b) (IZR (Zpos (xO xH))))) (Rle (Rdiv (Rplus a b) (IZR (Zpos (xO xH)))) b) *)
(* Goal: and (Rle a (@nth R (IZR Z0) (@SF_ly R l) i)) (Rle (@nth R (IZR Z0) (@SF_ly R l) i) b) *)
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@Riemann_sum V g l) (@Riemann_sum V f l) *)
    clear.
(* Goal: forall (a b : R) (_ : Rle a b), and (Rle a (Rdiv (Rplus a b) (IZR (Zpos (xO xH))))) (Rle (Rdiv (Rplus a b) (IZR (Zpos (xO xH)))) b) *)
(* Goal: and (Rle a (@nth R (IZR Z0) (@SF_ly R l) i)) (Rle (@nth R (IZR Z0) (@SF_ly R l) i) b) *)
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@Riemann_sum V g l) (@Riemann_sum V f l) *)
    intros a b Hab.
(* Goal: and (Rle a (Rdiv (Rplus a b) (IZR (Zpos (xO xH))))) (Rle (Rdiv (Rplus a b) (IZR (Zpos (xO xH)))) b) *)
(* Goal: and (Rle a (@nth R (IZR Z0) (@SF_ly R l) i)) (Rle (@nth R (IZR Z0) (@SF_ly R l) i) b) *)
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@Riemann_sum V g l) (@Riemann_sum V f l) *)
    lra.
(* Goal: and (Rle a (@nth R (IZR Z0) (@SF_ly R l) i)) (Rle (@nth R (IZR Z0) (@SF_ly R l) i) b) *)
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@Riemann_sum V g l) (@Riemann_sum V f l) *)
    fold l in H, H0, H1, H2.
(* Goal: and (Rle u u) (Rle u v) *)
(* Goal: Rle (Rmin a b) (Rmax a b) *)
(* Goal: P (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n)) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
    rewrite -H1 -H2 ; split.
(* Goal: Rle (@SF_h R l) (@nth R (IZR Z0) (@SF_ly R l) i) *)
(* Goal: Rle (@nth R (IZR Z0) (@SF_ly R l) i) (@last R (@SF_h R l) (@SF_lx R l)) *)
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@Riemann_sum V g l) (@Riemann_sum V f l) *)
    apply Rle_trans with (head 0 (SF_ly l)).
(* Goal: Rle (@SF_h R l) (@head R (IZR Z0) (@SF_ly R l)) *)
(* Goal: Rle (@head R (IZR Z0) (@SF_ly R l)) (@nth R (IZR Z0) (@SF_ly R l) i) *)
(* Goal: Rle (@nth R (IZR Z0) (@SF_ly R l) i) (@last R (@SF_h R l) (@SF_lx R l)) *)
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@Riemann_sum V g l) (@Riemann_sum V f l) *)
    apply (H0 O).
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    by apply lt_O_Sn.
(* Goal: Rle (@head R (IZR Z0) (@SF_ly R l)) (@nth R (IZR Z0) (@SF_ly R l) i) *)
(* Goal: Rle (@nth R (IZR Z0) (@SF_ly R l) i) (@last R (@SF_h R l) (@SF_lx R l)) *)
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@Riemann_sum V g l) (@Riemann_sum V f l) *)
    apply sorted_head.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    by apply ptd_sort'.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    by [].
(* Goal: Rle (@nth R (IZR Z0) (@SF_ly R l) i) (@last R (@SF_h R l) (@SF_lx R l)) *)
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@Riemann_sum V g l) (@Riemann_sum V f l) *)
    apply Rle_trans with (last 0 (SF_ly l)).
(* Goal: Rle (@nth R (IZR Z0) (@SF_ly R l) i) (@last R (IZR Z0) (@SF_ly R l)) *)
(* Goal: Rle (@last R (IZR Z0) (@SF_ly R l)) (@last R (@SF_h R l) (@SF_lx R l)) *)
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@Riemann_sum V g l) (@Riemann_sum V f l) *)
    apply sorted_last.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    by apply ptd_sort'.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    by [].
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
    rewrite -!nth_last SF_size_ly SF_size_lx SF_size_f2 size_mkseq.
(* Goal: Rle (@nth R (IZR Z0) (@SF_ly R l) (Init.Nat.pred (Init.Nat.pred (S (S n))))) (@nth R (@SF_h R l) (@SF_lx R l) (Init.Nat.pred (S (Init.Nat.pred (S (S n)))))) *)
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@Riemann_sum V g l) (@Riemann_sum V f l) *)
    simpl Peano.pred.
(* Goal: Rle (@nth R (IZR Z0) (@SF_ly R l) n) (@nth R (@SF_h R l) (@SF_lx R l) (S n)) *)
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@Riemann_sum V g l) (@Riemann_sum V f l) *)
    replace (nth (SF_h l) (SF_lx l) (S n)) with (nth 0 (SF_lx l) (S n)).
(* Goal: Rle (@nth R (IZR Z0) (@SF_ly R l) n) (@nth R (IZR Z0) (@SF_lx R l) (S n)) *)
(* Goal: @eq R (@nth R (IZR Z0) (@SF_lx R l) (S n)) (@nth R (@SF_h R l) (@SF_lx R l) (S n)) *)
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@Riemann_sum V g l) (@Riemann_sum V f l) *)
    apply (H0 n).
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    rewrite SF_size_f2 size_mkseq /=.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    by apply lt_n_Sn.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    rewrite SF_lx_f2.
(* Goal: @eq R (@nth R (IZR Z0) (unif_part a b n) (S n)) (@nth R (@SF_h R l) (unif_part a b n) (S n)) *)
(* Goal: lt O (@size R (unif_part a b n)) *)
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@Riemann_sum V g l) (@Riemann_sum V f l) *)
    assert (size (unif_part a b n) = S (S n)).
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
      by apply size_mkseq.
(* Goal: @eq R (@nth R (IZR Z0) (unif_part a b n) (S n)) (@nth R (@SF_h R l) (unif_part a b n) (S n)) *)
(* Goal: lt O (@size R (unif_part a b n)) *)
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@Riemann_sum V g l) (@Riemann_sum V f l) *)
    elim: (S n) (unif_part a b n) H3 ; simpl ; clear ; intros.
(* Goal: @eq R match unif_part0 with | Datatypes.nil => IZR Z0 | Datatypes.cons x s' => x end match unif_part0 with | Datatypes.nil => Rplus a (Rdiv (Rmult (IZR Z0) (Rminus b a)) (Rplus (INR n) (IZR (Zpos xH)))) | Datatypes.cons x s' => x end *)
(* Goal: @eq R match unif_part0 with | Datatypes.nil => IZR Z0 | Datatypes.cons x s' => @nth R (IZR Z0) s' n0 end match unif_part0 with | Datatypes.nil => Rplus a (Rdiv (Rmult (IZR Z0) (Rminus b a)) (Rplus (INR n) (IZR (Zpos xH)))) | Datatypes.cons x s' => @nth R (Rplus a (Rdiv (Rmult (IZR Z0) (Rminus b a)) (Rplus (INR n) (IZR (Zpos xH))))) s' n0 end *)
(* Goal: lt O (@size R (unif_part a b n)) *)
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@Riemann_sum V g l) (@Riemann_sum V f l) *)
    destruct unif_part0 ; simpl => //.
(* Goal: @eq R match unif_part0 with | Datatypes.nil => IZR Z0 | Datatypes.cons x s' => @nth R (IZR Z0) s' n0 end match unif_part0 with | Datatypes.nil => Rplus a (Rdiv (Rmult (IZR Z0) (Rminus b a)) (Rplus (INR n) (IZR (Zpos xH)))) | Datatypes.cons x s' => @nth R (Rplus a (Rdiv (Rmult (IZR Z0) (Rminus b a)) (Rplus (INR n) (IZR (Zpos xH))))) s' n0 end *)
(* Goal: lt O (@size R (unif_part a b n)) *)
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@Riemann_sum V g l) (@Riemann_sum V f l) *)
    replace unif_part0 with (head 0 unif_part0 :: behead unif_part0).
(* Goal: @eq R (@nth R (IZR Z0) (@behead R unif_part0) n0) (@nth R (Rplus a (Rdiv (Rmult (IZR Z0) (Rminus b a)) (Rplus (INR n) (IZR (Zpos xH))))) (@behead R unif_part0) n0) *)
(* Goal: @eq (list R) (@Datatypes.cons R (@head R (IZR Z0) unif_part0) (@behead R unif_part0)) unif_part0 *)
(* Goal: lt O (@size R (unif_part a b n)) *)
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@Riemann_sum V g l) (@Riemann_sum V f l) *)
    apply H.
(* Goal: le i (Init.Nat.pred (@size R (@Datatypes.cons R h s))) *)
    destruct unif_part0 ; by intuition.
(* Goal: le i (Init.Nat.pred (@size R (@Datatypes.cons R h s))) *)
    destruct unif_part0 ; by intuition.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    by apply lt_O_Sn.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  move: H => {Heq}.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  apply SF_cons_ind with (s := l) => {l} [x0 | h0 s IH] /= Heq.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by [].
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite !Riemann_sum_cons.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  apply (f_equal2 (fun x y => plus (scal (SF_h s - fst h0) x) y)).
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply sym_eq, (Heq O), lt_O_Sn.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  apply IH => i Hi.
(* Goal: @eq (ModuleSpace.sort R_Ring V) (f (@nth R (IZR Z0) (@SF_ly R s) i)) (g (@nth R (IZR Z0) (@SF_ly R s) i)) *)
  now apply (Heq (S i)), lt_n_S.
Qed.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma RInt_val_comp_opp (f : R -> V) (a b : R) (n : nat) :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  RInt_val (fun x => f (- x)) a b n = opp (RInt_val f (- a) (- b) n).
Proof.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite /RInt_val.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  replace (unif_part (- a) (- b) n) with (map Ropp (unif_part a b n)).
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) x) (Rlt x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i)) *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  elim: (unif_part a b n) {1}0 {2}0 => /= [ | x1 s IH] x0 x0'.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite /Riemann_sum /=.
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@zero (ModuleSpace.AbelianGroup R_Ring V)) (@opp (ModuleSpace.AbelianGroup R_Ring V) (@zero (ModuleSpace.AbelianGroup R_Ring V))) *)
(* Goal: @eq (ModuleSpace.sort R_Ring V) (Riemann_sum (fun x : R => @opp (ModuleSpace.AbelianGroup R_Ring V) (f x)) (@SF_cons R (@pair R R x0 y0) s)) (@opp (ModuleSpace.AbelianGroup R_Ring V) (Riemann_sum f (@SF_cons R (@pair R R x0 y0) s))) *)
  by apply sym_eq, @opp_zero.
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@scal R_Ring V u (@Riemann_sum V (fun x : R => f (Rplus (Rmult u x) v)) (@SF_seq_f2 R (fun x y : R => Rdiv (Rplus x y) (IZR (Zpos (xO xH)))) (@Datatypes.cons R x1 s)))) (@Riemann_sum V f (@SF_seq_f2 R (fun x y : R => Rdiv (Rplus x y) (IZR (Zpos (xO xH)))) (@Datatypes.cons R (Rplus (Rmult u x1) v) (@map R R (fun x : R => Rplus (Rmult u x) v) s)))) *)
(* Goal: @eq (list R) (@map R R (fun x : R => Rplus (Rmult u x) v) (unif_part a b n)) (unif_part (Rplus (Rmult u a) v) (Rplus (Rmult u b) v) n) *)
  destruct s as [ | x2 s].
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite /Riemann_sum /=.
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@zero (ModuleSpace.AbelianGroup R_Ring V)) (@opp (ModuleSpace.AbelianGroup R_Ring V) (@zero (ModuleSpace.AbelianGroup R_Ring V))) *)
(* Goal: @eq (ModuleSpace.sort R_Ring V) (Riemann_sum (fun x : R => @opp (ModuleSpace.AbelianGroup R_Ring V) (f x)) (@SF_cons R (@pair R R x0 y0) s)) (@opp (ModuleSpace.AbelianGroup R_Ring V) (Riemann_sum f (@SF_cons R (@pair R R x0 y0) s))) *)
  by apply sym_eq, @opp_zero.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite (SF_cons_f2 _ x1) ; try by apply lt_O_Sn.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite (SF_cons_f2 _ (- x1)) ; try by apply lt_O_Sn.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite !Riemann_sum_cons /=.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite opp_plus.
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V u (@scal R_Ring V (Rminus x2 x1) (f (Rplus (Rmult u (Rdiv (Rplus x1 x2) (IZR (Zpos (xO xH))))) v)))) (@scal R_Ring V u (@Riemann_sum V (fun x : R => f (Rplus (Rmult u x) v)) (@SF_seq_f2 R (fun x y : R => Rdiv (Rplus x y) (IZR (Zpos (xO xH)))) (@Datatypes.cons R x2 s))))) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus (Rplus (Rmult u x2) v) (Rplus (Rmult u x1) v)) (f (Rdiv (Rplus (Rplus (Rmult u x1) v) (Rplus (Rmult u x2) v)) (IZR (Zpos (xO xH)))))) (@Riemann_sum V f (@SF_seq_f2 R (fun x y : R => Rdiv (Rplus x y) (IZR (Zpos (xO xH)))) (@Datatypes.cons R (Rplus (Rmult u x2) v) (@map R R (fun x : R => Rplus (Rmult u x) v) s))))) *)
(* Goal: @eq (list R) (@map R R (fun x : R => Rplus (Rmult u x) v) (unif_part a b n)) (unif_part (Rplus (Rmult u a) v) (Rplus (Rmult u b) v) n) *)
  apply f_equal2.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  rewrite -scal_opp_l.
  apply (f_equal2 (fun x y => scal x (f y))) ;
(* Goal: @eq R (Rplus (Rmult u (Rplus a (Rdiv (Rmult (INR i) (Rminus b a)) (Rplus (INR n) (IZR (Zpos xH)))))) v) (Rplus (Rplus (Rmult u a) v) (Rdiv (Rmult (INR i) (Rminus (Rplus (Rmult u b) v) (Rplus (Rmult u a) v))) (Rplus (INR n) (IZR (Zpos xH))))) *)
    rewrite /Rdiv /opp /= ; field.
(* Goal: Rle (@Riemann_sum R_ModuleSpace f s) (@Riemann_sum R_ModuleSpace g s) *)
  by apply IH.
(* Goal: @eq (list R) (@map R R (fun x : R => Rplus (Rmult u x) v) (unif_part a b n)) (unif_part (Rplus (Rmult u a) v) (Rplus (Rmult u b) v) n) *)
  apply eq_from_nth with 0.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by rewrite size_map !size_mkseq.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  rewrite size_map => i Hi.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  rewrite (nth_map 0 0) => //.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite size_mkseq in Hi.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  rewrite !nth_mkseq => //.
(* Goal: @eq R (Rplus (Rmult u (Rplus a (Rdiv (Rmult (INR i) (Rminus b a)) (Rplus (INR n) (IZR (Zpos xH)))))) v) (Rplus (Rplus (Rmult u a) v) (Rdiv (Rmult (INR i) (Rminus (Rplus (Rmult u b) v) (Rplus (Rmult u a) v))) (Rplus (INR n) (IZR (Zpos xH))))) *)
  field.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  now rewrite -S_INR ; apply not_0_INR, sym_not_eq, O_S.
Qed.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma RInt_val_comp_lin (f : R -> V) (u v : R) (a b : R) (n : nat) :
(* Goal: forall (i : nat) (_ : lt (S i) (@size R (unif_part a b n))), @eq R (Rminus (@nth R (IZR Z0) (unif_part a b n) (S i)) (@nth R (IZR Z0) (unif_part a b n) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
  scal u (RInt_val (fun x => f (u * x + v)) a b n) = RInt_val f (u * a + v) (u * b + v) n.
Proof.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite /RInt_val.
(* Goal: forall (i : nat) (_ : lt (S i) (@size R (unif_part a b n))), @eq R (Rminus (@nth R (IZR Z0) (unif_part a b n) (S i)) (@nth R (IZR Z0) (unif_part a b n) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
  replace (unif_part (u * a + v) (u * b + v) n) with (map (fun x => u * x + v) (unif_part a b n)).
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) x) (Rlt x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i)) *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  elim: (unif_part a b n) {1}0 {2}0 => /= [ | x1 s IH] x0 x0'.
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@scal R_Ring V a (@zero (ModuleSpace.AbelianGroup R_Ring V))) (@zero (ModuleSpace.AbelianGroup R_Ring V)) *)
(* Goal: @eq (ModuleSpace.sort R_Ring V) (Riemann_sum (fun x : R => @scal R_Ring V a (f x)) (@SF_cons R (@pair R R x0 y0) s)) (@scal R_Ring V a (Riemann_sum f (@SF_cons R (@pair R R x0 y0) s))) *)
  by apply @scal_zero_r.
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@scal R_Ring V u (@Riemann_sum V (fun x : R => f (Rplus (Rmult u x) v)) (@SF_seq_f2 R (fun x y : R => Rdiv (Rplus x y) (IZR (Zpos (xO xH)))) (@Datatypes.cons R x1 s)))) (@Riemann_sum V f (@SF_seq_f2 R (fun x y : R => Rdiv (Rplus x y) (IZR (Zpos (xO xH)))) (@Datatypes.cons R (Rplus (Rmult u x1) v) (@map R R (fun x : R => Rplus (Rmult u x) v) s)))) *)
(* Goal: @eq (list R) (@map R R (fun x : R => Rplus (Rmult u x) v) (unif_part a b n)) (unif_part (Rplus (Rmult u a) v) (Rplus (Rmult u b) v) n) *)
  destruct s as [ | x2 s].
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@scal R_Ring V a (@zero (ModuleSpace.AbelianGroup R_Ring V))) (@zero (ModuleSpace.AbelianGroup R_Ring V)) *)
(* Goal: @eq (ModuleSpace.sort R_Ring V) (Riemann_sum (fun x : R => @scal R_Ring V a (f x)) (@SF_cons R (@pair R R x0 y0) s)) (@scal R_Ring V a (Riemann_sum f (@SF_cons R (@pair R R x0 y0) s))) *)
  by apply @scal_zero_r.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite (SF_cons_f2 _ x1) ; try by apply lt_O_Sn.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite (SF_cons_f2 _ (u * x1 + v)) ; try by apply lt_O_Sn.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite !Riemann_sum_cons /=.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite scal_distr_l.
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V u (@scal R_Ring V (Rminus x2 x1) (f (Rplus (Rmult u (Rdiv (Rplus x1 x2) (IZR (Zpos (xO xH))))) v)))) (@scal R_Ring V u (@Riemann_sum V (fun x : R => f (Rplus (Rmult u x) v)) (@SF_seq_f2 R (fun x y : R => Rdiv (Rplus x y) (IZR (Zpos (xO xH)))) (@Datatypes.cons R x2 s))))) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus (Rplus (Rmult u x2) v) (Rplus (Rmult u x1) v)) (f (Rdiv (Rplus (Rplus (Rmult u x1) v) (Rplus (Rmult u x2) v)) (IZR (Zpos (xO xH)))))) (@Riemann_sum V f (@SF_seq_f2 R (fun x y : R => Rdiv (Rplus x y) (IZR (Zpos (xO xH)))) (@Datatypes.cons R (Rplus (Rmult u x2) v) (@map R R (fun x : R => Rplus (Rmult u x) v) s))))) *)
(* Goal: @eq (list R) (@map R R (fun x : R => Rplus (Rmult u x) v) (unif_part a b n)) (unif_part (Rplus (Rmult u a) v) (Rplus (Rmult u b) v) n) *)
  apply f_equal2.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite scal_assoc.
  apply (f_equal2 (fun x y => scal x (f y))) ;
(* Goal: @eq R (Rplus (Rmult u (Rplus a (Rdiv (Rmult (INR i) (Rminus b a)) (Rplus (INR n) (IZR (Zpos xH)))))) v) (Rplus (Rplus (Rmult u a) v) (Rdiv (Rmult (INR i) (Rminus (Rplus (Rmult u b) v) (Rplus (Rmult u a) v))) (Rplus (INR n) (IZR (Zpos xH))))) *)
    rewrite /mult /= ; field.
(* Goal: Rle (@Riemann_sum R_ModuleSpace f s) (@Riemann_sum R_ModuleSpace g s) *)
  by apply IH.
(* Goal: @eq (list R) (@map R R (fun x : R => Rplus (Rmult u x) v) (unif_part a b n)) (unif_part (Rplus (Rmult u a) v) (Rplus (Rmult u b) v) n) *)
  apply eq_from_nth with 0.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by rewrite size_map !size_mkseq.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  rewrite size_map => i Hi.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  rewrite (nth_map 0 0) => //.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite size_mkseq in Hi.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  rewrite !nth_mkseq => //.
(* Goal: @eq R (Rplus (Rmult u (Rplus a (Rdiv (Rmult (INR i) (Rminus b a)) (Rplus (INR n) (IZR (Zpos xH)))))) v) (Rplus (Rplus (Rmult u a) v) (Rdiv (Rmult (INR i) (Rminus (Rplus (Rmult u b) v) (Rplus (Rmult u a) v))) (Rplus (INR n) (IZR (Zpos xH))))) *)
  field.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  now rewrite -S_INR ; apply not_0_INR, sym_not_eq, O_S.
Qed.

End RInt_val.

(** ** From SF_seq to StepFun *)

(** Alternative Chasles relation *)

Fixpoint seq_cut_down' (s : seq (R*R)) (x x0 : R) : seq (R*R) :=
  match s with
    | [::] => [:: (x,x0)]
    | h :: t =>
        match Rle_dec (fst h) x with
          | right _ => [:: (x,snd h)]
          | left _ => h :: (seq_cut_down' t x (snd h))
        end
  end.
Fixpoint seq_cut_up' (s : seq (R*R)) (x x0 : R) : seq (R*R) :=
  match s with
    | [::] => [:: (x,x0)]
    | h :: t =>
        match Rle_dec (fst h) x with
          | right _ => (x,x0)::h::t
          | left _ => seq_cut_up' t x (snd h)
        end
  end.

Definition SF_cut_down' (sf : @SF_seq R) (x : R) x0 :=
  let s := seq_cut_down' ((SF_h sf,x0) :: (SF_t sf)) x x0 in
  mkSF_seq (fst (head (SF_h sf,x0) s)) (behead s).
Definition SF_cut_up' (sf : @SF_seq R) (x : R) x0 :=
  let s := seq_cut_up' ((SF_h sf,x0) :: (SF_t sf)) x x0 in
  mkSF_seq (fst (head (SF_h sf,x0) s)) (behead s).
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_Chasles {V : ModuleSpace R_AbsRing} (f : R -> V) (s : SF_seq) x x0 :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  (SF_h s <= x <= last (SF_h s) (unzip1 (SF_t s))) ->
  Riemann_sum f s =
  plus (Riemann_sum f (SF_cut_down' s x x0)) (Riemann_sum f (SF_cut_up' s x x0)).
Proof.
(* Goal: forall _ : and (Rle (@SF_h R s) x) (Rle x (@last R (@SF_h R s) (@unzip1 R R (@SF_t R s)))), @eq (ModuleSpace.sort R_Ring V) (@Riemann_sum V f s) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@Riemann_sum V f (SF_cut_down' s x x0)) (@Riemann_sum V f (SF_cut_up' s x x0))) *)
  rename x0 into z0.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  apply SF_cons_ind with (s := s) => {s} /= [ x0 | [x0 y0] s IH] /= Hx.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite (Rle_antisym _ _ (proj1 Hx) (proj2 Hx)).
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  move: (Rle_refl x).
(* Goal: @eq Rlist (@subdivision_val a b (SF_inf_r f a b n)) (if Rle_dec a b then seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part a b n))) else seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part b a n)))) *)
  rewrite /SF_cut_down' /SF_cut_up' /= ; case: Rle_dec => //= _ _.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by rewrite /Riemann_sum /= Rminus_eq_0 scal_zero_l !plus_zero_l.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  move: (fun Hx1 => IH (conj Hx1 (proj2 Hx))) => {IH}.
  rewrite /SF_cut_down' /SF_cut_up' /= ;
  case: (Rle_dec x0 _) (proj1 Hx) => //= Hx0 _.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: (Rle_dec (SF_h s) x) => //= Hx1 IH.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  move: (IH Hx1) => {IH} IH.
  rewrite (Riemann_sum_cons _ (x0,y0))
    (Riemann_sum_cons _ (x0,y0) (mkSF_seq (SF_h s) (seq_cut_down' (SF_t s) x y0)))
    IH /= => {IH}.
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus (@SF_h R s) x0) (f y0)) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus (@SF_h R s) x0) (g y0)) (@plus (ModuleSpace.AbelianGroup R_Ring V) (Riemann_sum f s) (Riemann_sum g s)))) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus (@SF_h R s) x0) (f y0)) (@plus (ModuleSpace.AbelianGroup R_Ring V) (Riemann_sum f s) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus (@SF_h R s) x0) (g y0)) (Riemann_sum g s)))) *)
  rewrite -!plus_assoc ; apply f_equal.
(* Goal: @eq (AbelianGroup.sort (ModuleSpace.AbelianGroup R_Ring V)) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@Riemann_sum V f (@mkSF_seq R (@SF_h R s) (seq_cut_down' (@SF_t R s) x z0))) (@Riemann_sum V f (@mkSF_seq R (@fst R R (@head (prod R R) (@pair R R (@SF_h R s) z0) (seq_cut_up' (@SF_t R s) x z0))) (@behead (prod R R) (seq_cut_up' (@SF_t R s) x z0))))) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@Riemann_sum V f (@mkSF_seq R (@SF_h R s) (seq_cut_down' (@SF_t R s) x y0))) (@Riemann_sum V f (@mkSF_seq R (@fst R R (@head (prod R R) (@pair R R x0 z0) (seq_cut_up' (@SF_t R s) x y0))) (@behead (prod R R) (seq_cut_up' (@SF_t R s) x y0))))) *)
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@Riemann_sum V f (@SF_cons R (@pair R R x0 y0) s)) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@Riemann_sum V f (@mkSF_seq R x0 (@Datatypes.cons (prod R R) (@pair R R x y0) (@Datatypes.nil (prod R R))))) (@Riemann_sum V f (@mkSF_seq R x (@Datatypes.cons (prod R R) (@pair R R (@SF_h R s) y0) (@SF_t R s))))) *)
  assert (forall x0 y0, fst (head (x0, z0) (seq_cut_up' (SF_t s) x y0)) = x).
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) x) (Rlt x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i)) *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
    elim: (SF_t s) => [ | x2 t IH] x1 y1 //=.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    by case: Rle_dec.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite ?H.
  move: (proj2 Hx) Hx1 => {Hx} ;
  apply SF_cons_dec with (s := s) => {s H} /= [x1 | [x1 y1] s] //= Hx Hx1.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by rewrite /Riemann_sum /= (Rle_antisym _ _ Hx Hx1) Rminus_eq_0 !scal_zero_l !plus_zero_l.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Rle_dec => //.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite Riemann_sum_cons (Riemann_sum_cons _ (x,y0) s) {2}/Riemann_sum /=.
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus (@SF_h R s) x0) (f y0)) (@Riemann_sum V f s)) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus x x0) (f y0)) (@zero (ModuleSpace.AbelianGroup R_Ring V))) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus (@SF_h R s) x) (f y0)) (@Riemann_sum V f s))) *)
  clear IH.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite plus_zero_r !plus_assoc.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  apply f_equal2 => //.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  rewrite -scal_distr_r.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  apply f_equal2 => //.
(* Goal: @eq R (Rplus (Rminus (@SF_h R s) x0) (Rminus (@last R (@SF_h R s) (@unzip1 R R (@SF_t R s))) (@SF_h R s))) (Rminus (@last R (@SF_h R s) (@unzip1 R R (@SF_t R s))) x0) *)
  rewrite /plus /= ; ring.
Qed.

Lemma seq_cut_up_head' (s : seq (R*R)) x x0 z :
  fst (head z (seq_cut_up' s x x0)) = x.
Proof.
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) x) (Rlt x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i)) *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  elim: s z x0 => [ | x1 s IH] //= z x0.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by case: Rle_dec.
Qed.

(** Build StepFun using SF_seq *)

Lemma ad_SF_compat z0 (s : SF_seq) (pr : SF_sorted Rle s) :
  adapted_couple (SF_fun s z0) (head 0 (SF_lx s)) (last 0 (SF_lx s))
    (seq2Rlist (SF_lx s)) (seq2Rlist (SF_ly s)).
Proof.
(* head and last *)
(* Goal: adapted_couple (@SF_fun R s z0) (@head R (IZR Z0) (@SF_lx R s)) (@last R (IZR Z0) (@SF_lx R s)) (seq2Rlist (@SF_lx R s)) (seq2Rlist (@SF_ly R s)) *)
  have H : ((head 0 (SF_lx s)) <= (last 0 (SF_lx s))).
(* Goal: forall _ : and (Rlt (Rmin a b) x) (Rlt x (Rmax a b)), Rbar_le (Inf_fct f a b) (Finite (f x)) *)
    move: pr ; rewrite /SF_sorted.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    case: (SF_lx s) => {s} [| h s] Hs.
(* Goal: Rle (@SF_h R s) (@SF_h R s) *)
(* Goal: Rle (@SF_h R s) (@nth R (IZR Z0) (@unzip1 R R (@map (prod R R) (prod R R) (fun x : prod R R => @pair R R (@fst R R x) (f (@snd R R x))) (@SF_t R s))) i) *)
    apply Rle_refl.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
    rewrite -nth0 ; apply sorted_last => // ; apply lt_O_Sn.
  rewrite /adapted_couple ?nth_compat ?size_compat ?nth0 ?nth_last
  /Rmin /Rmax ?SF_size_lx ?SF_size_ly ;
(* Goal: le i (Init.Nat.pred (@size R (@Datatypes.cons R h s))) *)
  case: (Rle_dec (head 0 (SF_lx s)) (last 0 (SF_lx s))) => // {H} _ ; intuition.
(* sorted *)
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  apply sorted_compat => //.
(* adapted *)
  move: i pr H ; apply SF_cons_dec with (s := s)
    => {s} [x0 | h s] i Hs Hi x [Hx0 Hx1].
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    by apply lt_n_O in Hi.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  rewrite /SF_fun ?SF_size_cons ?nth_compat -?SF_size_lx ?SF_lx_cons in Hi, Hx0, Hx1 |- *.
(* Goal: @eq R (@SF_fun_aux R (@pair R R (@SF_h R (@SF_cons R h s)) z0) (@SF_t R (@SF_cons R h s)) z0 x) (@nth R (IZR Z0) (@SF_ly R (@SF_cons R h s)) i) *)
  simpl.
  move: h i x {1}z0 Hs Hi Hx0 Hx1 ; apply SF_cons_ind with (s := s)
    => {s} [x1 | h0 s IH] h ; case => [| i ] x z0' Hs Hi Hx0 Hx1 //= ; case: Rlt_dec => Hx' //.
(* Goal: @eq R z0' (@snd R R h) *)
(* Goal: @eq R (if Rlt_dec x (@fst R R h0) then @snd R R h else @SF_fun_aux R (@pair R R (@SF_h R s) (@snd R R h0)) (@SF_t R s) z0 x) (@snd R R h) *)
(* Goal: @eq R z0' (@nth R (IZR Z0) (@Datatypes.cons R (@snd R R h0) (@unzip2 R R (@SF_t R s))) i) *)
(* Goal: @eq R (if Rlt_dec x (@fst R R h0) then @snd R R h else @SF_fun_aux R (@pair R R (@SF_h R s) (@snd R R h0)) (@SF_t R s) z0 x) (@nth R (IZR Z0) (@Datatypes.cons R (@snd R R h0) (@unzip2 R R (@SF_t R s))) i) *)
  now contradict Hx' ; apply Rle_not_lt, Rlt_le, Hx0.
(* Goal: @eq R (if Rle_dec x x1 then @snd R R h else z0) (@snd R R h) *)
(* Goal: @eq R z0' (@nth R (IZR Z0) (@Datatypes.nil R) i) *)
(* Goal: @eq R (if Rle_dec x x1 then @snd R R h else z0) (@nth R (IZR Z0) (@Datatypes.nil R) i) *)
(* Goal: @eq R z0' (@snd R R h) *)
(* Goal: @eq R (if Rlt_dec x (@fst R R h0) then @snd R R h else @SF_fun_aux R (@pair R R (@SF_h R s) (@snd R R h0)) (@SF_t R s) z0 x) (@snd R R h) *)
(* Goal: @eq R z0' (@nth R (IZR Z0) (@Datatypes.cons R (@snd R R h0) (@unzip2 R R (@SF_t R s))) i) *)
(* Goal: @eq R (if Rlt_dec x (@fst R R h0) then @snd R R h else @SF_fun_aux R (@pair R R (@SF_h R s) (@snd R R h0)) (@SF_t R s) z0 x) (@nth R (IZR Z0) (@Datatypes.cons R (@snd R R h0) (@unzip2 R R (@SF_t R s))) i) *)
  now case: Rle_dec => Hx'' // ; contradict Hx'' ; apply Rlt_le, Hx1.
(* Goal: @eq R (if Rle_dec x x1 then @snd R R h else z0) (@nth R (IZR Z0) (@Datatypes.nil R) i) *)
(* Goal: @eq R z0' (@snd R R h) *)
(* Goal: @eq R (if Rlt_dec x (@fst R R h0) then @snd R R h else @SF_fun_aux R (@pair R R (@SF_h R s) (@snd R R h0)) (@SF_t R s) z0 x) (@snd R R h) *)
(* Goal: @eq R z0' (@nth R (IZR Z0) (@Datatypes.cons R (@snd R R h0) (@unzip2 R R (@SF_t R s))) i) *)
(* Goal: @eq R (if Rlt_dec x (@fst R R h0) then @snd R R h else @SF_fun_aux R (@pair R R (@SF_h R s) (@snd R R h0)) (@SF_t R s) z0 x) (@nth R (IZR Z0) (@Datatypes.cons R (@snd R R h0) (@unzip2 R R (@SF_t R s))) i) *)
  now rewrite /= in Hi ; by apply lt_S_n, lt_n_O in Hi.
(* Goal: @eq R (if Rle_dec x x1 then @snd R R h else z0) (@nth R (IZR Z0) (@Datatypes.nil R) i) *)
(* Goal: @eq R z0' (@snd R R h) *)
(* Goal: @eq R (if Rlt_dec x (@fst R R h0) then @snd R R h else @SF_fun_aux R (@pair R R (@SF_h R s) (@snd R R h0)) (@SF_t R s) z0 x) (@snd R R h) *)
(* Goal: @eq R z0' (@nth R (IZR Z0) (@Datatypes.cons R (@snd R R h0) (@unzip2 R R (@SF_t R s))) i) *)
(* Goal: @eq R (if Rlt_dec x (@fst R R h0) then @snd R R h else @SF_fun_aux R (@pair R R (@SF_h R s) (@snd R R h0)) (@SF_t R s) z0 x) (@nth R (IZR Z0) (@Datatypes.cons R (@snd R R h0) (@unzip2 R R (@SF_t R s))) i) *)
  now rewrite /= in Hi ; by apply lt_S_n, lt_n_O in Hi.
(* Goal: @eq R z0' (@snd R R h) *)
(* Goal: @eq R (if Rlt_dec x (@fst R R h0) then @snd R R h else @SF_fun_aux R (@pair R R (@SF_h R s) (@snd R R h0)) (@SF_t R s) z0 x) (@snd R R h) *)
(* Goal: @eq R z0' (@nth R (IZR Z0) (@Datatypes.cons R (@snd R R h0) (@unzip2 R R (@SF_t R s))) i) *)
(* Goal: @eq R (if Rlt_dec x (@fst R R h0) then @snd R R h else @SF_fun_aux R (@pair R R (@SF_h R s) (@snd R R h0)) (@SF_t R s) z0 x) (@nth R (IZR Z0) (@Datatypes.cons R (@snd R R h0) (@unzip2 R R (@SF_t R s))) i) *)
  now contradict Hx' ; apply Rle_not_lt, Rlt_le, Hx0.
(* Goal: @eq R (if Rlt_dec x (@fst R R h0) then @snd R R h else @SF_fun_aux R (@pair R R (@SF_h R s) (@snd R R h0)) (@SF_t R s) z0 x) (@snd R R h) *)
(* Goal: @eq R z0' (@nth R (IZR Z0) (@Datatypes.cons R (@snd R R h0) (@unzip2 R R (@SF_t R s))) i) *)
(* Goal: @eq R (if Rlt_dec x (@fst R R h0) then @snd R R h else @SF_fun_aux R (@pair R R (@SF_h R s) (@snd R R h0)) (@SF_t R s) z0 x) (@nth R (IZR Z0) (@Datatypes.cons R (@snd R R h0) (@unzip2 R R (@SF_t R s))) i) *)
  now case: Rlt_dec => Hx'' //.
  now contradict Hx' ; apply Rle_not_lt, Rlt_le, Rle_lt_trans with (2 := Hx0) ;
  have Hi' : (S i < size (SF_lx (SF_cons h (SF_cons h0 s))))%nat ;
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  [ rewrite ?SF_lx_cons /= in Hi |-* ; apply lt_trans with (1 := Hi), lt_n_Sn | ] ;
  apply (sorted_head (SF_lx (SF_cons h (SF_cons h0 s))) (S i) Hs Hi' 0).
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  rewrite -(IH h0 i x (snd h)) //=.
(* Goal: @SF_sorted R Rle (@SF_cons R h0 s) *)
(* Goal: lt i (S (@size R (@unzip1 R R (@SF_t R s)))) *)
  apply Hs.
(* Goal: lt (S (S i)) (@size R (@Datatypes.cons R h1 (@Datatypes.cons R h2 s))) *)
(* Goal: @sorted R Rle (@Datatypes.cons R h0 (@Datatypes.cons R h1 (@Datatypes.cons R h2 s))) *)
(* Goal: @eq T (@nth T y0 (@SF_ly T (@SF_cons T h0 s)) (@size (prod R T) (@SF_t T s))) (@nth T y0 (@SF_ly T (@SF_cons T h (@SF_cons T h0 s))) j) *)
  rewrite ?SF_lx_cons /= in Hi |-* ; apply lt_S_n, Hi.
Qed.

Definition SF_compat_le (s : @SF_seq R) (pr : SF_sorted Rle s) :
  StepFun (head 0 (SF_lx s)) (last 0 (SF_lx s)).
Proof.
(* Goal: StepFun (@head R (IZR Z0) (@SF_lx R s)) (@last R (IZR Z0) (@SF_lx R s)) *)
  exists (SF_fun s 0) ; exists (seq2Rlist (SF_lx s)) ; exists (seq2Rlist (SF_ly s)).
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply ad_SF_compat.
Defined.

Lemma Riemann_sum_compat f (s : SF_seq) (pr : SF_sorted Rle s) :
  Riemann_sum f s = RiemannInt_SF (SF_compat_le (SF_map f s) (SF_map_sort f s _ pr)).
Proof.
(* Goal: @eq Rlist (@subdivision_val a b (SF_inf_r f a b n)) (if Rle_dec a b then seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part a b n))) else seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part b a n)))) *)
  rewrite /RiemannInt_SF ; case: Rle_dec => // [_ | H].
(* Goal: @eq (ModuleSpace.sort R_Ring R_ModuleSpace) (@Riemann_sum R_ModuleSpace f s) (Int_SF (@subdivision_val (@head R (IZR Z0) (@SF_lx R (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s))) (@last R (IZR Z0) (@SF_lx R (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s))) (SF_compat_le (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s) (@SF_map_sort R (ModuleSpace.sort R_Ring R_ModuleSpace) f s Rle pr))) (@subdivision (@head R (IZR Z0) (@SF_lx R (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s))) (@last R (IZR Z0) (@SF_lx R (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s))) (SF_compat_le (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s) (@SF_map_sort R (ModuleSpace.sort R_Ring R_ModuleSpace) f s Rle pr)))) *)
(* Goal: @eq (ModuleSpace.sort R_Ring R_ModuleSpace) (@Riemann_sum R_ModuleSpace f s) (Ropp (Int_SF (@subdivision_val (@head R (IZR Z0) (@SF_lx R (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s))) (@last R (IZR Z0) (@SF_lx R (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s))) (SF_compat_le (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s) (@SF_map_sort R (ModuleSpace.sort R_Ring R_ModuleSpace) f s Rle pr))) (@subdivision (@head R (IZR Z0) (@SF_lx R (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s))) (@last R (IZR Z0) (@SF_lx R (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s))) (SF_compat_le (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s) (@SF_map_sort R (ModuleSpace.sort R_Ring R_ModuleSpace) f s Rle pr))))) *)
  move: pr ; apply SF_cons_ind with (s := s) => {s} [x0 | h s IH] pr //=.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  rewrite /= -IH /Riemann_sum /SF_map /= => {IH}.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite Rmult_comm.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply SF_cons_dec with (s := s).
(* Goal: @SF_sorted R Rle s *)
(* Goal: @eq (ModuleSpace.sort R_Ring R_ModuleSpace) (@Riemann_sum R_ModuleSpace f s) (Ropp (Int_SF (@subdivision_val (@head R (IZR Z0) (@SF_lx R (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s))) (@last R (IZR Z0) (@SF_lx R (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s))) (SF_compat_le (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s) (@SF_map_sort R (ModuleSpace.sort R_Ring R_ModuleSpace) f s Rle pr))) (@subdivision (@head R (IZR Z0) (@SF_lx R (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s))) (@last R (IZR Z0) (@SF_lx R (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s))) (SF_compat_le (@SF_map R (ModuleSpace.sort R_Ring R_ModuleSpace) f s) (@SF_map_sort R (ModuleSpace.sort R_Ring R_ModuleSpace) f s Rle pr))))) *)
  apply pr.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  contradict H ; rewrite -nth_last -nth0 ; move: (le_refl (ssrnat.predn (size (SF_lx (SF_map f s))))) ;
  elim: {1 3}(ssrnat.predn (size (SF_lx (SF_map f s)))) => /= [| i IH] Hi.
(* Goal: Rle (@SF_h R s) (@SF_h R s) *)
(* Goal: Rle (@SF_h R s) (@nth R (IZR Z0) (@unzip1 R R (@map (prod R R) (prod R R) (fun x : prod R R => @pair R R (@fst R R x) (f (@snd R R x))) (@SF_t R s))) i) *)
  apply Rle_refl.
  apply Rle_trans with (1 := IH (le_trans _ _ _ (le_n_Sn i) Hi)), (sorted_nth Rle) ;
(* Goal: le i (Init.Nat.pred (@size R (@Datatypes.cons R h s))) *)
  intuition.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply SF_map_sort.
Qed.

(** Build StepFun using uniform partition *)

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma ad_SF_val_fun (f : R -> R) (a b : R) (n : nat) :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  ((a <= b) -> adapted_couple (SF_val_fun f a b n) a b
      (seq2Rlist (unif_part a b n)) (seq2Rlist (SF_val_ly f a b n)))
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  /\ (~(a <= b) -> adapted_couple (SF_val_fun f b a n) a b
      (seq2Rlist (unif_part b a n)) (seq2Rlist (SF_val_ly f b a n))).
Proof.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  wlog : a b / (a <= b) => Hw.
(* Goal: and (forall _ : Rle a b, adapted_couple (@SF_val_fun R f a b n) a b (seq2Rlist (unif_part a b n)) (seq2Rlist (@SF_val_ly R f a b n))) (forall _ : not (Rle a b), adapted_couple (@SF_val_fun R f b a n) a b (seq2Rlist (unif_part b a n)) (seq2Rlist (@SF_val_ly R f b a n))) *)
    split ; case: (Rle_dec a b) => // Hab _.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    by apply Hw.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    apply StepFun_P2 ; apply Hw ; by apply Rlt_le, Rnot_le_lt.
(* Goal: and (forall _ : Rle a b, adapted_couple (@SF_val_fun R f a b n) a b (seq2Rlist (unif_part a b n)) (seq2Rlist (@SF_val_ly R f a b n))) (forall _ : not (Rle a b), adapted_couple (@SF_val_fun R f b a n) a b (seq2Rlist (unif_part b a n)) (seq2Rlist (@SF_val_ly R f b a n))) *)
  split ; case: (Rle_dec a b) => // {Hw} Hab _.

  have : (a = head 0 (SF_lx (SF_val_seq f a b n))) ;
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  [rewrite SF_lx_f2 /= ; (try by apply lt_O_Sn) ; field ; apply Rgt_not_eq ; intuition | move => {2}->].
(* Goal: adapted_couple (@SF_val_fun R f a b n) (@head R (IZR Z0) (@SF_lx R (@SF_val_seq R f a b n))) b (seq2Rlist (unif_part a b n)) (seq2Rlist (@SF_val_ly R f a b n)) *)
  pattern b at 3 ; replace b with (last 0 (SF_lx (SF_val_seq f a b n))).
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite -(SF_lx_f2 (fun x y => f ((x+y)/2)) (unif_part a b n)) ; try by apply lt_O_Sn.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  rewrite /SF_val_ly -SF_ly_f2.
(* Goal: adapted_couple (@SF_val_fun R f a b n) (@head R (IZR Z0) (@SF_lx R (@SF_val_seq R f a b n))) (@last R (IZR Z0) (@SF_lx R (@SF_val_seq R f a b n))) (seq2Rlist (@SF_lx R (@SF_seq_f2 R (fun x y : R => f (Rdiv (Rplus x y) (IZR (Zpos (xO xH))))) (unif_part a b n)))) (seq2Rlist (@SF_ly R (@SF_seq_f2 R (fun x y : R => f (Rdiv (Rplus x y) (IZR (Zpos (xO xH))))) (unif_part a b n)))) *)
(* Goal: @eq R (@last R (IZR Z0) (@SF_lx R (@SF_val_seq R f a b n))) b *)
  unfold SF_val_fun, SF_fun_f2.
(* Goal: forall (i : nat) (_ : lt (S i) (@size R (unif_part a b n))), @eq R (Rminus (@nth R (IZR Z0) (unif_part a b n) (S i)) (@nth R (IZR Z0) (unif_part a b n) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
  replace (SF_seq_f2 (fun x y : R => f ((x + y) / 2)) (unif_part a b n))
(* Goal: @eq (@SF_seq T0) (@SF_cons T0 (@pair R T0 (@fst R T h0) (f (@snd R T h0))) (@SF_rcons T0 (SF_map f s) (@pair R T0 (@fst R T h) (f (@snd R T h))))) (@SF_rcons T0 (@SF_cons T0 (@pair R T0 (@fst R T h0) (f (@snd R T h0))) (SF_map f s)) (@pair R T0 (@fst R T h) (f (@snd R T h)))) *)
    with (SF_val_seq f a b n) by auto.
(* Goal: adapted_couple (fun x : R => @SF_fun R (@SF_val_seq R f a b n) (f (Rdiv (Rplus (IZR Z0) (IZR Z0)) (IZR (Zpos (xO xH))))) x) (@head R (IZR Z0) (@SF_lx R (@SF_val_seq R f a b n))) (@last R (IZR Z0) (@SF_lx R (@SF_val_seq R f a b n))) (seq2Rlist (@SF_lx R (@SF_val_seq R f a b n))) (seq2Rlist (@SF_ly R (@SF_val_seq R f a b n))) *)
(* Goal: @eq R (@last R (IZR Z0) (@SF_lx R (@SF_val_seq R f a b n))) b *)
  apply (ad_SF_compat _ (SF_val_seq f a b n)).
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply SF_sorted_f2, unif_part_sort.
  rewrite SF_lx_f2 ;
    replace (head 0 (unif_part a b n) :: behead (unif_part a b n))
(* Goal: @eq (@SF_seq T0) (@SF_cons T0 (@pair R T0 (@fst R T h0) (f (@snd R T h0))) (@SF_rcons T0 (SF_map f s) (@pair R T0 (@fst R T h) (f (@snd R T h))))) (@SF_rcons T0 (@SF_cons T0 (@pair R T0 (@fst R T h0) (f (@snd R T h0))) (SF_map f s)) (@pair R T0 (@fst R T h) (f (@snd R T h)))) *)
    with (unif_part a b n) by auto.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
    rewrite -nth_last size_mkseq nth_mkseq ?S_INR //= ;
    field ; apply Rgt_not_eq, INRp1_pos.
(* Goal: lt O (@size R (@Datatypes.cons R (@SF_h R ptd) (@map (prod R R) R (@fst R R) (@SF_t R ptd)))) *)
(* Goal: Rle (@last R (@SF_h R ptd) (@unzip1 R R (@SF_t R ptd))) (@SF_h R ptd) *)
  now rewrite size_mkseq ; apply lt_O_Sn.
Qed.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Definition sf_SF_val_fun (f : R -> R) (a b : R) (n : nat) : StepFun a b.
Proof.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case : (Rle_dec a b) => Hab.
  exists (SF_val_fun f a b n) ;
  exists (seq2Rlist (unif_part a b n)) ;
  exists (seq2Rlist (SF_val_ly f a b n)) ; by apply ad_SF_val_fun.
  exists (SF_val_fun f b a n) ;
  exists (seq2Rlist (unif_part b a n)) ;
  exists (seq2Rlist (SF_val_ly f b a n)) ; by apply ad_SF_val_fun.
Defined.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_val_subdiv (f : R -> R) (a b : R) (n : nat) :
  subdivision (sf_SF_val_fun f a b n) =
  match (Rle_dec a b) with
    | left _ => seq2Rlist (unif_part a b n)
    | right _ => seq2Rlist (unif_part b a n)
  end.
Proof.
(* Goal: @eq Rlist (@subdivision_val a b (SF_inf_r f a b n)) (if Rle_dec a b then seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part a b n))) else seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part b a n)))) *)
  rewrite /sf_SF_val_fun ; case: (Rle_dec a b) => Hab //.
Qed.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_val_subdiv_val (f : R -> R) (a b : R) (n : nat) :
  subdivision_val (sf_SF_val_fun f a b n) =
  match (Rle_dec a b) with
    | left _ => seq2Rlist (SF_val_ly f a b n)
    | right _ => seq2Rlist (SF_val_ly f b a n)
  end.
Proof.
(* Goal: @eq Rlist (@subdivision_val a b (SF_inf_r f a b n)) (if Rle_dec a b then seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part a b n))) else seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part b a n)))) *)
  rewrite /sf_SF_val_fun ; case: (Rle_dec a b) => Hab //.
Qed.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_val_fun_rw (f : R -> R) (a b : R) (n : nat) (x : R) (Hx : a <= x <= b) :
  SF_val_fun f a b n x =
    match (unif_part_nat a b n x Hx) with
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
      | inleft H => f (a + (INR (proj1_sig H) + /2) * (b-a) / (INR n + 1))
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
      | inright _ => f (a + (INR n + /2) * (b-a) / (INR n + 1))
    end.
Proof.
(* Goal: @eq Rbar (SF_inf_fun f a b n x) match unif_part_nat a b n x Hx with | inleft H => Inf_fct f (@nth R (IZR Z0) (unif_part a b n) (@proj1_sig nat (fun i : nat => and (and (Rle (@nth R (IZR Z0) (unif_part a b n) i) x) (Rlt x (@nth R (IZR Z0) (unif_part a b n) (S i)))) (lt (S (S i)) (@size R (unif_part a b n)))) H)) (@nth R (IZR Z0) (unif_part a b n) (S (@proj1_sig nat (fun i : nat => and (and (Rle (@nth R (IZR Z0) (unif_part a b n) i) x) (Rlt x (@nth R (IZR Z0) (unif_part a b n) (S i)))) (lt (S (S i)) (@size R (unif_part a b n)))) H))) | inright a0 => Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n)) end *)
  have Hab : (a <= b) ; [by apply Rle_trans with (1 := proj1 Hx), Hx | ].
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: unif_part_nat => {Hx} [ [ i [Hx Hi] ] | Hx] /=.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
(* i < 2^n - 1 *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite /SF_val_fun /SF_fun_f2.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  replace (a + (INR i + /2) * (b - a) / (INR n+1))
(* Goal: forall (i : nat) (_ : lt (S i) (@size R (unif_part a b n))), @eq R (Rminus (@nth R (IZR Z0) (unif_part a b n) (S i)) (@nth R (IZR Z0) (unif_part a b n) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
    with ((nth 0 (unif_part a b n) i + nth 0 (unif_part a b n) (S i)) / 2) ;
    [ | rewrite size_mkseq in Hi ; rewrite ?nth_mkseq ?S_INR ;
    [field ; apply Rgt_not_eq | apply SSR_leq | apply SSR_leq ] ; intuition].
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: (unif_part a b n) (unif_part_sort a b n Hab) i Hi x Hx => {a b Hab n} [| h s] Hs /= i Hi.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    by apply lt_n_O in Hi.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: (s) Hs (i) (lt_S_n _ _ Hi) => {s i Hi} [| h0 s] Hs /= i Hi.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    by apply lt_n_O in Hi.
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) x) (Rlt x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i)) *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  elim: (s) h h0 Hs (i) (lt_S_n _ _ Hi) => {s i Hi} [|h1 s IH] h h0 Hs /= i Hi x Hx.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    by apply lt_n_O in Hi.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: i Hx Hi => [|i]/= Hx Hi.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite /SF_fun /=.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Rlt_dec => [Hx0 | _ ].
(* Goal: @eq Rbar (Finite (IZR Z0)) (Inf_fct f h h0) *)
(* Goal: @eq Rbar (if Rle_dec x h0 then Inf_fct f h h0 else Finite (IZR Z0)) (Inf_fct f h h0) *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  contradict Hx0 ; apply Rle_not_lt, Hx.
(* Goal: @eq Rbar (if Rle_dec x h0 then Inf_fct f h h0 else Finite (IZR Z0)) (Inf_fct f h h0) *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Rlt_dec => // Hx0 ; contradict Hx0 ; apply Hx.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  rewrite -(IH h0 h1 (proj2 Hs) i (lt_S_n _ _ Hi) x Hx).
(* Goal: @eq Rlist (@subdivision_val a b (SF_inf_r f a b n)) (if Rle_dec a b then seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part a b n))) else seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part b a n)))) *)
  rewrite /SF_fun /= ; case: Rlt_dec => [ Hx0 | _ ] //.
  contradict Hx0 ; apply Rle_not_lt, Rle_trans with (1 := proj1 Hs),
  Rle_trans with (2 := proj1 Hx), (sorted_head [:: h0, h1 & s] _ (proj2 Hs)) ;
(* Goal: le i (Init.Nat.pred (@size R (@Datatypes.cons R h s))) *)
  simpl; intuition.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Rlt_dec => [ Hx0 | _ ] //.
  contradict Hx0 ; apply Rle_not_lt, Rle_trans with (2 := proj1 Hx),
(* Goal: le i (Init.Nat.pred (@size R (@Datatypes.cons R h s))) *)
  (sorted_head [:: h0, h1 & s] _ (proj2 Hs)) ; simpl; intuition.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
(* i = 2^n - 1 *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  replace (a + (INR n + /2) * (b - a) / (INR n + 1))
(* Goal: forall (i : nat) (_ : lt (S i) (@size R (unif_part a b n))), @eq R (Rminus (@nth R (IZR Z0) (unif_part a b n) (S i)) (@nth R (IZR Z0) (unif_part a b n) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
    with ((nth 0 (unif_part a b n) (n) + nth 0 (unif_part a b n) (S n)) / 2) ;
    [ | rewrite ?nth_mkseq ?minus_INR ?S_INR /= ;
    [field ; apply Rgt_not_eq |
    apply SSR_leq | apply SSR_leq ] ; intuition].
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (unif_part a b n) n) x) (Rle x (@nth R (IZR Z0) (unif_part a b n) (S n)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  suff : (1 < size (unif_part a b n))%nat.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  move: x Hx ; have: (n = size (unif_part a b n) - 2)%nat ;
    [ rewrite size_mkseq ; intuition | ].
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
    move => {2 4 8 10}->.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite /SF_val_fun /SF_fun_f2.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: (unif_part a b n) (unif_part_sort a b n Hab) => {a b Hab n} [| h s Hs x Hx /= Hi] .
(* Goal: forall (_ : @sorted R Rle (@Datatypes.nil R)) (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.nil R) (Init.Nat.sub (@size R (@Datatypes.nil R)) (S (S O)))) x) (Rle x (@nth R (IZR Z0) (@Datatypes.nil R) (S (Init.Nat.sub (@size R (@Datatypes.nil R)) (S (S O))))))) (_ : lt (S O) (@size R (@Datatypes.nil R))), @eq R (@SF_fun R (@SF_seq_f2 R (fun x1 y : R => f (Rdiv (Rplus x1 y) (IZR (Zpos (xO xH))))) (@Datatypes.nil R)) (f (Rdiv (Rplus (IZR Z0) (IZR Z0)) (IZR (Zpos (xO xH))))) x) (f (Rdiv (Rplus (@nth R (IZR Z0) (@Datatypes.nil R) (Init.Nat.sub (@size R (@Datatypes.nil R)) (S (S O)))) (@nth R (IZR Z0) (@Datatypes.nil R) (S (Init.Nat.sub (@size R (@Datatypes.nil R)) (S (S O)))))) (IZR (Zpos (xO xH))))) *)
(* Goal: @eq R (@SF_fun R (@SF_seq_f2 R (fun x y : R => f (Rdiv (Rplus x y) (IZR (Zpos (xO xH))))) (@Datatypes.cons R h s)) (f (Rdiv (Rplus (IZR Z0) (IZR Z0)) (IZR (Zpos (xO xH))))) x) (f (Rdiv (Rplus (@nth R (IZR Z0) (@Datatypes.cons R h s) (Init.Nat.sub (@size R s) (S O))) (@nth R (IZR Z0) s (Init.Nat.sub (@size R s) (S O)))) (IZR (Zpos (xO xH))))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  intros _ x Hx Hi.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply lt_n_O in Hi.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: s h Hs Hi x Hx => [| h0 s] h Hs /= Hi.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply lt_irrefl in Hi.
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) x) (Rlt x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i)) *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  elim: s h h0 Hs {Hi} => [| h1 s IH] h h0 Hs /= x Hx.
(* Goal: @eq Rlist (@subdivision_val a b (SF_inf_r f a b n)) (if Rle_dec a b then seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part a b n))) else seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part b a n)))) *)
  rewrite /SF_fun /= ; case: Rlt_dec => [Hx0 | _].
(* Goal: @eq Rbar (Finite (IZR Z0)) (Inf_fct f h h0) *)
(* Goal: @eq Rbar (if Rle_dec x h0 then Inf_fct f h h0 else Finite (IZR Z0)) (Inf_fct f h h0) *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  contradict Hx0 ; apply Rle_not_lt, Hx.
(* Goal: @eq Rbar (if Rle_dec x h0 then Inf_fct f h h0 else Finite (IZR Z0)) (Inf_fct f h h0) *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Rle_dec => [| Hx0] // ; contradict Hx0 ; apply Hx.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  rewrite -minus_n_O in IH.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  rewrite -(IH h0 h1 (proj2 Hs) x Hx ).
(* Goal: @eq Rlist (@subdivision_val a b (SF_inf_r f a b n)) (if Rle_dec a b then seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part a b n))) else seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part b a n)))) *)
  rewrite /SF_fun /= ; case: Rlt_dec => [ Hx0 | _ ] //.
  contradict Hx0 ; apply Rle_not_lt, Rle_trans with (1 := proj1 Hs),
  Rle_trans with (2 := proj1 Hx), (sorted_head [:: h0, h1 & s] _ (proj2 Hs)) ;
(* Goal: le i (Init.Nat.pred (@size R (@Datatypes.cons R h s))) *)
  simpl; intuition.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Rlt_dec => [ Hx0 | _ ] //.
  contradict Hx0 ; apply Rle_not_lt, Rle_trans with (2 := proj1 Hx),
(* Goal: le i (Init.Nat.pred (@size R (@Datatypes.cons R h s))) *)
  (sorted_head [:: h0, h1 & s] _ (proj2 Hs)) ; simpl; intuition.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite size_mkseq ; by apply lt_n_S, lt_O_Sn.
Qed.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma RInt_val_Reals (f : R -> R) (a b : R) (n : nat) :
  RInt_val f a b n = RiemannInt_SF (sf_SF_val_fun f a b n).
Proof.
rewrite /RiemannInt_SF SF_val_subdiv SF_val_subdiv_val ;
case: Rle_dec => Hab.
(* a <= b *)
(* Goal: @eq Rlist (@subdivision_val a b (SF_inf_r f a b n)) (if Rle_dec a b then seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part a b n))) else seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part b a n)))) *)
  rewrite /RInt_val /SF_val_ly ; case: (unif_part a b n) => [| h s] /=.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by [].
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) x) (Rlt x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i)) *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  elim: s h => [|h0 s IH] h /=.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by [].
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite (SF_cons_f2 _ h).
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  2: by apply lt_O_Sn.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite Riemann_sum_cons /= IH /plus /scal /= /mult /=.
(* Goal: @eq R (Rplus (Rminus (@SF_h R s) x0) (Rminus (@last R (@SF_h R s) (@unzip1 R R (@SF_t R s))) (@SF_h R s))) (Rminus (@last R (@SF_h R s) (@unzip1 R R (@SF_t R s))) x0) *)
  ring.
(* ~ a <= Rmult_plus_distr_l (b )*)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite RInt_val_swap /SF_val_ly /RInt_val.
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus (@SF_h R s) x0) (f y0)) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus (@SF_h R s) x0) (g y0)) (@plus (ModuleSpace.AbelianGroup R_Ring V) (Riemann_sum f s) (Riemann_sum g s)))) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus (@SF_h R s) x0) (f y0)) (@plus (ModuleSpace.AbelianGroup R_Ring V) (Riemann_sum f s) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus (@SF_h R s) x0) (g y0)) (Riemann_sum g s)))) *)
  simpl opp ; apply f_equal.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: (unif_part b a n) => [| h s] /=.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by [].
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) x) (Rlt x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i)) *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  elim: s h => [|h0 s IH] h /=.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by [].
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite SF_cons_f2.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  2: by apply lt_O_Sn.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite Riemann_sum_cons IH /= /plus /scal /= /mult /=.
(* Goal: @eq R (Rplus (Rminus (@SF_h R s) x0) (Rminus (@last R (@SF_h R s) (@unzip1 R R (@SF_t R s))) (@SF_h R s))) (Rminus (@last R (@SF_h R s) (@unzip1 R R (@SF_t R s))) x0) *)
  ring.
Qed.

(** ** Upper and lower step functions *)

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma ex_Im_fct (f : R -> R) (a b : R) : a <> b ->
  exists x, (fun y => exists x, y = f x /\ Rmin a b < x < Rmax a b) x.
Proof.
(* Goal: and (Rle u u) (Rle u v) *)
(* Goal: Rle (Rmin a b) (Rmax a b) *)
(* Goal: P (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n)) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  exists (f ((a+b)/2)) ; exists ((a+b)/2) ; split.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by [].
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite /Rmin /Rmax.
(* Goal: and (Rle a (Rdiv (Rplus a b) (IZR (Zpos (xO xH))))) (Rle (Rdiv (Rplus a b) (IZR (Zpos (xO xH)))) b) *)
(* Goal: and (Rle a (@nth R (IZR Z0) (@SF_ly R l) i)) (Rle (@nth R (IZR Z0) (@SF_ly R l) i) b) *)
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@Riemann_sum V g l) (@Riemann_sum V f l) *)
  case Rle_dec ; lra.
Qed.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Definition Sup_fct (f : R -> R) (a b : R) : Rbar :=
  match Req_EM_T a b with
    | right Hab => Lub_Rbar (fun y => exists x, y = f x /\ Rmin a b < x < Rmax a b)
    | left _ => Finite 0
  end.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Definition Inf_fct (f : R -> R) (a b : R) : Rbar :=
  match Req_EM_T a b with
    | right Hab => Glb_Rbar (fun y => exists x, y = f x /\ Rmin a b < x < Rmax a b)
    | left _ => Finite 0
  end.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma Sup_fct_bound (f : R -> R) (a b : R) :
  Sup_fct f a b = Sup_fct f b a.
Proof.
  rewrite /Sup_fct /= ;
  case: Req_EM_T => Hab ;
  case: Req_EM_T => Hba.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by [].
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply sym_equal in Hab.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply sym_equal in Hba.
  apply Lub_Rbar_eqset => x ;
  by rewrite Rmin_comm Rmax_comm.
Qed.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma Inf_fct_bound (f : R -> R) (a b : R) :
  Inf_fct f a b = Inf_fct f b a.
Proof.
  rewrite /Inf_fct /= ;
  case: Req_EM_T => Hab ;
  case: Req_EM_T => Hba.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by [].
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply sym_equal in Hab.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply sym_equal in Hba.
  apply Glb_Rbar_eqset => x ;
  by rewrite Rmin_comm Rmax_comm.
Qed.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma Sup_fct_le (f : R -> R) (a b : R) (x : R) :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  (Rmin a b < x < Rmax a b) ->
    Rbar_le (Finite (f x)) (Sup_fct f a b).
Proof.
(* Goal: forall _ : and (Rlt (Rmin a b) x) (Rlt x (Rmax a b)), Rbar_le (Inf_fct f a b) (Finite (f x)) *)
  move => Hx ; rewrite /Sup_fct.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Req_EM_T => Hab.
  move: (Rlt_trans _ _ _ (proj1 Hx) (proj2 Hx)) => {Hx} ;
  rewrite /Rmin /Rmax ;
  case: Rle_dec (Req_le _ _ Hab) => //= _ _ Hx.
(* Goal: Rle (IZR Z0) (f x) *)
(* Goal: Rbar_le (Glb_Rbar (fun y : R => @ex R (fun x : R => and (@eq R y (f x)) (and (Rlt (Rmin a b) x) (Rlt x (Rmax a b)))))) (Finite (f x)) *)
  contradict Hx ; by apply Rle_not_lt, Req_le.
  rewrite /Lub_Rbar ;
  case: ex_lub_Rbar => l lub ;
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  apply lub ; exists x ; split ; by [].
Qed.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma Inf_fct_le (f : R -> R) (a b : R) (x : R) : (Rmin a b < x < Rmax a b) ->
  Rbar_le (Inf_fct f a b) (Finite (f x)).
Proof.
(* Goal: forall _ : and (Rlt (Rmin a b) x) (Rlt x (Rmax a b)), Rbar_le (Inf_fct f a b) (Finite (f x)) *)
  move => Hx ; rewrite /Inf_fct.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Req_EM_T => Hab.
  move: (Rlt_trans _ _ _ (proj1 Hx) (proj2 Hx)) => {Hx} ;
  rewrite /Rmin /Rmax ;
  case: Rle_dec (Req_le _ _ Hab) => //= _ _ Hx.
(* Goal: Rle (IZR Z0) (f x) *)
(* Goal: Rbar_le (Glb_Rbar (fun y : R => @ex R (fun x : R => and (@eq R y (f x)) (and (Rlt (Rmin a b) x) (Rlt x (Rmax a b)))))) (Finite (f x)) *)
  contradict Hx ; by apply Rle_not_lt, Req_le.
  rewrite /Glb_Rbar ;
  case: ex_glb_Rbar => l lub ;
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  apply lub ; exists x ; split ; by [].
Qed.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma Sup_fct_maj (f : R -> R) (a b : R) (M : R) :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  (forall x, Rmin a b < x < Rmax a b -> f x <= M) ->
  is_finite (Sup_fct f a b).
Proof.
(* Goal: @eq Rlist (@subdivision_val a b (SF_inf_r f a b n)) (if Rle_dec a b then seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part a b n))) else seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part b a n)))) *)
  rewrite /Sup_fct ; case: Req_EM_T => Hab Hf.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by [].
  rewrite /Lub_Rbar ;
  case: ex_lub_Rbar ; case => [l | | ] [lub ub] /=.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by [].
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: (ub (Finite M)) => //.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  move => _ [x [-> Hx]].
(* Goal: and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (f (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (unif_part a b n) (S i)))) (Rle (f (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: is_true (ssrnat.leq (S (S i)) (@size R (unif_part a b n))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
  by apply Hf.
(* Goal: forall (i : nat) (_ : lt (S i) (@size R (unif_part a b n))), @eq R (Rminus (@nth R (IZR Z0) (unif_part a b n) (S i)) (@nth R (IZR Z0) (unif_part a b n) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
  case: (lub (f((a+b)/2))) => //.
(* Goal: and (Rle u u) (Rle u v) *)
(* Goal: Rle (Rmin a b) (Rmax a b) *)
(* Goal: P (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n)) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  exists ((a + b) / 2) ; split.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by [].
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite /Rmin /Rmax.
(* Goal: and (Rle a (Rdiv (Rplus a b) (IZR (Zpos (xO xH))))) (Rle (Rdiv (Rplus a b) (IZR (Zpos (xO xH)))) b) *)
(* Goal: and (Rle a (@nth R (IZR Z0) (@SF_ly R l) i)) (Rle (@nth R (IZR Z0) (@SF_ly R l) i) b) *)
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@Riemann_sum V g l) (@Riemann_sum V f l) *)
  case Rle_dec ; lra.
Qed.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma Inf_fct_min (f : R -> R) (a b : R) (m : R) :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  (forall x, Rmin a b < x < Rmax a b -> m <= f x) ->
  is_finite (Inf_fct f a b).
Proof.
(* Goal: @eq Rlist (@subdivision_val a b (SF_inf_r f a b n)) (if Rle_dec a b then seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part a b n))) else seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part b a n)))) *)
  rewrite /Inf_fct ; case: Req_EM_T => Hab Hf.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by [].
  rewrite /Glb_Rbar ;
  case: ex_glb_Rbar ; case => [l | | ] [lub ub] /=.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by [].
(* Goal: forall (i : nat) (_ : lt (S i) (@size R (unif_part a b n))), @eq R (Rminus (@nth R (IZR Z0) (unif_part a b n) (S i)) (@nth R (IZR Z0) (unif_part a b n) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
  case: (lub (f((a+b)/2))) => //.
(* Goal: and (Rle u u) (Rle u v) *)
(* Goal: Rle (Rmin a b) (Rmax a b) *)
(* Goal: P (@SF_seq_f2 R (fun x _ : R => x) (unif_part (Rmin a b) (Rmax a b) n)) *)
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  exists ((a + b) / 2) ; split.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by [].
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite /Rmin /Rmax.
(* Goal: and (Rle a (Rdiv (Rplus a b) (IZR (Zpos (xO xH))))) (Rle (Rdiv (Rplus a b) (IZR (Zpos (xO xH)))) b) *)
(* Goal: and (Rle a (@nth R (IZR Z0) (@SF_ly R l) i)) (Rle (@nth R (IZR Z0) (@SF_ly R l) i) b) *)
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@Riemann_sum V g l) (@Riemann_sum V f l) *)
  case Rle_dec ; lra.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: (ub (Finite m)) => //.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  move => _ [x [-> Hx]].
(* Goal: and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (f (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (unif_part a b n) (S i)))) (Rle (f (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: is_true (ssrnat.leq (S (S i)) (@size R (unif_part a b n))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
  by apply Hf.
Qed.

(** SF_sup and SF_inf *)

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Definition SF_sup_seq (f : R -> R) (a b : R) (n : nat) : SF_seq :=
  SF_seq_f2 (Sup_fct f) (unif_part a b n).
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_sup_lx (f : R -> R) (a b : R) (n : nat) :
  SF_lx (SF_sup_seq f a b n) = unif_part a b n.
Proof.
(* Goal: @eq (list R) (@SF_lx Rbar (SF_sup_seq f a b n)) (unif_part a b n) *)
  apply SF_lx_f2.
(* Goal: lt O (@size R (@Datatypes.cons R (@SF_h R ptd) (@map (prod R R) R (@fst R R) (@SF_t R ptd)))) *)
(* Goal: Rle (@last R (@SF_h R ptd) (@unzip1 R R (@SF_t R ptd))) (@SF_h R ptd) *)
  now apply lt_O_Sn.
Qed.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_sup_ly (f : R -> R) (a b : R) (n : nat) :
  SF_ly (SF_sup_seq f a b n) = behead (pairmap (Sup_fct f) 0 (unif_part a b n)).
Proof.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply SF_ly_f2.
Qed.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Definition SF_inf_seq (f : R -> R) (a b : R) (n : nat) : SF_seq :=
  SF_seq_f2 (Inf_fct f) (unif_part a b n).
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_inf_lx (f : R -> R) (a b : R) (n : nat) :
  SF_lx (SF_inf_seq f a b n) = unif_part a b n.
Proof.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply SF_lx_f2, lt_O_Sn.
Qed.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_inf_ly (f : R -> R) (a b : R) (n : nat) :
  SF_ly (SF_inf_seq f a b n) = behead (pairmap (Inf_fct f) 0 (unif_part a b n)).
Proof.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply SF_ly_f2.
Qed.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_sup_bound (f : R -> R) (a b : R) (n : nat) :
  SF_rev (SF_sup_seq f a b n) = SF_sup_seq f b a n.
Proof.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  rewrite /SF_sup_seq unif_part_bound => //.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite SF_rev_f2 ?revK //.
(* Goal: forall x y : R, @eq Rbar (Sup_fct f x y) (Sup_fct f y x) *)
  move => x y ; apply Sup_fct_bound.
Qed.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_inf_bound (f : R -> R) (a b : R) (n : nat) :
  SF_rev (SF_inf_seq f a b n) = SF_inf_seq f b a n.
Proof.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  rewrite /SF_inf_seq unif_part_bound => //.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite SF_rev_f2 ?revK //.
(* Goal: forall x y : R, @eq Rbar (Inf_fct f x y) (Inf_fct f y x) *)
  move => x y ; apply Inf_fct_bound.
Qed.


(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Definition SF_sup_fun (f : R -> R) (a b : R) (n : nat) (x : R) : Rbar :=
  match (Rle_dec a b) with
    | left _ => SF_fun (SF_sup_seq f a b n) (Finite 0) x
    | right _ => SF_fun (SF_sup_seq f b a n) (Finite 0) x
  end.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Definition SF_inf_fun (f : R -> R) (a b : R) (n : nat) (x : R) : Rbar :=
  match (Rle_dec a b) with
    | left _ => SF_fun (SF_inf_seq f a b n) (Finite 0) x
    | right _ => SF_fun (SF_inf_seq f b a n) (Finite 0) x
  end.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_sup_fun_bound (f : R -> R) (a b : R) (n : nat) (x : R) :
  SF_sup_fun f a b n x = SF_sup_fun f b a n x.
Proof.
(* Goal: @eq Rbar (SF_inf_fun f a b n x) (SF_inf_fun f b a n x) *)
  rewrite /SF_sup_fun ; case: (Rle_dec a b) => Hab ; case : (Rle_dec b a) => Hba //.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by rewrite (Rle_antisym _ _ Hab Hba).
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by contradict Hba ; apply Rlt_le, Rnot_le_lt.
Qed.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_inf_fun_bound (f : R -> R) (a b : R) (n : nat) (x : R) :
  SF_inf_fun f a b n x = SF_inf_fun f b a n x.
Proof.
(* Goal: @eq Rbar (SF_inf_fun f a b n x) (SF_inf_fun f b a n x) *)
  rewrite /SF_inf_fun ; case: (Rle_dec a b) => Hab ; case : (Rle_dec b a) => Hba //.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by rewrite (Rle_antisym _ _ Hab Hba).
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by contradict Hba ; apply Rlt_le, Rnot_le_lt.
Qed.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_sup_fun_rw (f : R -> R) (a b : R) (n : nat) (x : R) (Hx : a <= x <= b) :
  SF_sup_fun f a b n x =
    match (unif_part_nat a b n x Hx) with
      | inleft H => Sup_fct f (nth 0 (unif_part a b n) (proj1_sig H))
          (nth 0 (unif_part a b n) (S (proj1_sig H)))
      | inright _ => Sup_fct f (nth 0 (unif_part a b n) (n))
          (nth 0 (unif_part a b n) (S n))
    end.
Proof.
(* Goal: @eq Rbar (SF_inf_fun f a b n x) match unif_part_nat a b n x Hx with | inleft H => Inf_fct f (@nth R (IZR Z0) (unif_part a b n) (@proj1_sig nat (fun i : nat => and (and (Rle (@nth R (IZR Z0) (unif_part a b n) i) x) (Rlt x (@nth R (IZR Z0) (unif_part a b n) (S i)))) (lt (S (S i)) (@size R (unif_part a b n)))) H)) (@nth R (IZR Z0) (unif_part a b n) (S (@proj1_sig nat (fun i : nat => and (and (Rle (@nth R (IZR Z0) (unif_part a b n) i) x) (Rlt x (@nth R (IZR Z0) (unif_part a b n) (S i)))) (lt (S (S i)) (@size R (unif_part a b n)))) H))) | inright a0 => Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n)) end *)
  have Hab : (a <= b) ; [by apply Rle_trans with (1 := proj1 Hx), Hx | ].
(* Goal: @eq Rlist (@subdivision_val a b (SF_inf_r f a b n)) (if Rle_dec a b then seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part a b n))) else seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part b a n)))) *)
  rewrite /SF_sup_fun /SF_sup_seq ; case: Rle_dec => // _.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) match unif_part_nat a b n x Hx with | inleft H => Inf_fct f (@nth R (IZR Z0) (unif_part a b n) (@proj1_sig nat (fun i : nat => and (and (Rle (@nth R (IZR Z0) (unif_part a b n) i) x) (Rlt x (@nth R (IZR Z0) (unif_part a b n) (S i)))) (lt (S (S i)) (@size R (unif_part a b n)))) H)) (@nth R (IZR Z0) (unif_part a b n) (S (@proj1_sig nat (fun i : nat => and (and (Rle (@nth R (IZR Z0) (unif_part a b n) i) x) (Rlt x (@nth R (IZR Z0) (unif_part a b n) (S i)))) (lt (S (S i)) (@size R (unif_part a b n)))) H))) | inright a0 => Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n)) end *)
  case: unif_part_nat => {Hx} [ [ i [Hx Hi] ] | Hx] ; simpl proj1_sig.
(* i < n *)
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: (unif_part a b n) (unif_part_sort a b n Hab) i Hi x Hx => {a b Hab n} [| h s] Hs /= i Hi.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    by apply lt_n_O in Hi.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: (s) Hs (i) (lt_S_n _ _ Hi) => {s i Hi} [| h0 s] Hs /= i Hi.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    by apply lt_n_O in Hi.
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) x) (Rlt x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i)) *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  elim: (s) h h0 Hs (i) (lt_S_n _ _ Hi) => {s i Hi} [|h1 s IH] h h0 Hs /= i Hi x Hx.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    by apply lt_n_O in Hi.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: i Hx Hi => [|i]/= Hx Hi.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite /SF_fun /=.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Rlt_dec => [Hx0 | _ ].
(* Goal: @eq Rbar (Finite (IZR Z0)) (Inf_fct f h h0) *)
(* Goal: @eq Rbar (if Rle_dec x h0 then Inf_fct f h h0 else Finite (IZR Z0)) (Inf_fct f h h0) *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  contradict Hx0 ; apply Rle_not_lt, Hx.
(* Goal: @eq Rbar (if Rle_dec x h0 then Inf_fct f h h0 else Finite (IZR Z0)) (Inf_fct f h h0) *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Rlt_dec => // Hx0 ; contradict Hx0 ; apply Hx.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  rewrite -(IH h0 h1 (proj2 Hs) i (lt_S_n _ _ Hi) x Hx).
(* Goal: @eq Rlist (@subdivision_val a b (SF_inf_r f a b n)) (if Rle_dec a b then seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part a b n))) else seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part b a n)))) *)
  rewrite /SF_fun /= ; case: Rlt_dec => [ Hx0 | _ ] //.
  contradict Hx0 ; apply Rle_not_lt, Rle_trans with (1 := proj1 Hs),
  Rle_trans with (2 := proj1 Hx), (sorted_head [:: h0, h1 & s] _ (proj2 Hs)) ;
(* Goal: le i (Init.Nat.pred (@size R (@Datatypes.cons R h s))) *)
  simpl; intuition.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Rlt_dec => [ Hx0 | _ ] //.
  contradict Hx0 ; apply Rle_not_lt, Rle_trans with (2 := proj1 Hx),
(* Goal: le i (Init.Nat.pred (@size R (@Datatypes.cons R h s))) *)
  (sorted_head [:: h0, h1 & s] _ (proj2 Hs)) ; simpl; intuition.
(* i = n *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  move: x Hx.
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (unif_part a b n) n) x) (Rle x (@nth R (IZR Z0) (unif_part a b n) (S n)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  suff : (1 < size (unif_part a b n))%nat.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  have: (n = size (unif_part a b n) - 2)%nat ;
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
    [ rewrite size_mkseq ; intuition | move => {3 5 8 10}->].
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: (unif_part a b n) (unif_part_sort a b n Hab) => {a b Hab n} [| h s] Hs /= Hi.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply lt_n_O in Hi.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: s h Hs Hi => [| h0 s] h Hs /= Hi.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply lt_irrefl in Hi.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  rewrite -minus_n_O ; elim: s h h0 Hs {Hi} => [| h1 s IH] h h0 Hs /= x Hx.
(* Goal: @eq Rlist (@subdivision_val a b (SF_inf_r f a b n)) (if Rle_dec a b then seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part a b n))) else seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part b a n)))) *)
  rewrite /SF_fun /= ; case: Rlt_dec => [Hx0 | _].
(* Goal: @eq Rbar (Finite (IZR Z0)) (Inf_fct f h h0) *)
(* Goal: @eq Rbar (if Rle_dec x h0 then Inf_fct f h h0 else Finite (IZR Z0)) (Inf_fct f h h0) *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  contradict Hx0 ; apply Rle_not_lt, Hx.
(* Goal: @eq Rbar (if Rle_dec x h0 then Inf_fct f h h0 else Finite (IZR Z0)) (Inf_fct f h h0) *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Rle_dec => [| Hx0] // ; contradict Hx0 ; apply Hx.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  rewrite -(IH h0 h1 (proj2 Hs) x Hx).
(* Goal: @eq Rlist (@subdivision_val a b (SF_inf_r f a b n)) (if Rle_dec a b then seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part a b n))) else seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part b a n)))) *)
  rewrite /SF_fun /= ; case: Rlt_dec => [ Hx0 | _ ] //.
  contradict Hx0 ; apply Rle_not_lt, Rle_trans with (1 := proj1 Hs),
  Rle_trans with (2 := proj1 Hx), (sorted_head [:: h0, h1 & s] _ (proj2 Hs)) ;
(* Goal: le i (Init.Nat.pred (@size R (@Datatypes.cons R h s))) *)
  simpl; intuition.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Rlt_dec => [ Hx0 | _ ] //.
  contradict Hx0 ; apply Rle_not_lt, Rle_trans with (2 := proj1 Hx),
(* Goal: le i (Init.Nat.pred (@size R (@Datatypes.cons R h s))) *)
  (sorted_head [:: h0, h1 & s] _ (proj2 Hs)) ; simpl; intuition.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite size_mkseq ; by apply lt_n_S, lt_O_Sn.
Qed.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_inf_fun_rw (f : R -> R) (a b : R) (n : nat) (x : R) (Hx : a <= x <= b) :
  SF_inf_fun f a b n x =
    match (unif_part_nat a b n x Hx) with
      | inleft H => Inf_fct f (nth 0 (unif_part a b n) (proj1_sig H))
          (nth 0 (unif_part a b n) (S (proj1_sig H)))
      | inright _ => Inf_fct f (nth 0 (unif_part a b n) (n))
          (nth 0 (unif_part a b n) (S n))
    end.
Proof.
(* Goal: @eq Rbar (SF_inf_fun f a b n x) match unif_part_nat a b n x Hx with | inleft H => Inf_fct f (@nth R (IZR Z0) (unif_part a b n) (@proj1_sig nat (fun i : nat => and (and (Rle (@nth R (IZR Z0) (unif_part a b n) i) x) (Rlt x (@nth R (IZR Z0) (unif_part a b n) (S i)))) (lt (S (S i)) (@size R (unif_part a b n)))) H)) (@nth R (IZR Z0) (unif_part a b n) (S (@proj1_sig nat (fun i : nat => and (and (Rle (@nth R (IZR Z0) (unif_part a b n) i) x) (Rlt x (@nth R (IZR Z0) (unif_part a b n) (S i)))) (lt (S (S i)) (@size R (unif_part a b n)))) H))) | inright a0 => Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n)) end *)
  have Hab : (a <= b) ; [by apply Rle_trans with (1 := proj1 Hx), Hx | ].
(* Goal: @eq Rlist (@subdivision_val a b (SF_inf_r f a b n)) (if Rle_dec a b then seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part a b n))) else seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part b a n)))) *)
  rewrite /SF_inf_fun /SF_inf_seq ; case: Rle_dec => // _.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) match unif_part_nat a b n x Hx with | inleft H => Inf_fct f (@nth R (IZR Z0) (unif_part a b n) (@proj1_sig nat (fun i : nat => and (and (Rle (@nth R (IZR Z0) (unif_part a b n) i) x) (Rlt x (@nth R (IZR Z0) (unif_part a b n) (S i)))) (lt (S (S i)) (@size R (unif_part a b n)))) H)) (@nth R (IZR Z0) (unif_part a b n) (S (@proj1_sig nat (fun i : nat => and (and (Rle (@nth R (IZR Z0) (unif_part a b n) i) x) (Rlt x (@nth R (IZR Z0) (unif_part a b n) (S i)))) (lt (S (S i)) (@size R (unif_part a b n)))) H))) | inright a0 => Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n)) end *)
  case: unif_part_nat => {Hx} [ [ i [Hx Hi] ] | Hx] ; simpl proj1_sig.
(* i < n *)
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: (unif_part a b n) (unif_part_sort a b n Hab) i Hi x Hx => {a b Hab n} [| h s] Hs /= i Hi.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    by apply lt_n_O in Hi.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: (s) Hs (i) (lt_S_n _ _ Hi) => {s i Hi} [| h0 s] Hs /= i Hi.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    by apply lt_n_O in Hi.
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) x) (Rlt x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) i) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) i)) *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  elim: (s) h h0 Hs (i) (lt_S_n _ _ Hi) => {s i Hi} [|h1 s IH] h h0 Hs /= i Hi x Hx.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    by apply lt_n_O in Hi.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: i Hx Hi => [|i]/= Hx Hi.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite /SF_fun /=.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Rlt_dec => [Hx0 | _ ].
(* Goal: @eq Rbar (Finite (IZR Z0)) (Inf_fct f h h0) *)
(* Goal: @eq Rbar (if Rle_dec x h0 then Inf_fct f h h0 else Finite (IZR Z0)) (Inf_fct f h h0) *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  contradict Hx0 ; apply Rle_not_lt, Hx.
(* Goal: @eq Rbar (if Rle_dec x h0 then Inf_fct f h h0 else Finite (IZR Z0)) (Inf_fct f h h0) *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Rlt_dec => // Hx0 ; contradict Hx0 ; apply Hx.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  rewrite -(IH h0 h1 (proj2 Hs) i (lt_S_n _ _ Hi) x Hx).
(* Goal: @eq Rlist (@subdivision_val a b (SF_inf_r f a b n)) (if Rle_dec a b then seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part a b n))) else seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part b a n)))) *)
  rewrite /SF_fun /= ; case: Rlt_dec => [ Hx0 | _ ] //.
  contradict Hx0 ; apply Rle_not_lt, Rle_trans with (1 := proj1 Hs),
  Rle_trans with (2 := proj1 Hx), (sorted_head [:: h0, h1 & s] _ (proj2 Hs)) ;
(* Goal: le i (Init.Nat.pred (@size R (@Datatypes.cons R h s))) *)
  simpl; intuition.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Rlt_dec => [ Hx0 | _ ] //.
  contradict Hx0 ; apply Rle_not_lt, Rle_trans with (2 := proj1 Hx),
(* Goal: le i (Init.Nat.pred (@size R (@Datatypes.cons R h s))) *)
  (sorted_head [:: h0, h1 & s] _ (proj2 Hs)) ; simpl; intuition.
(* i = n *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  move: x Hx.
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (unif_part a b n) n) x) (Rle x (@nth R (IZR Z0) (unif_part a b n) (S n)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  suff : (1 < size (unif_part a b n))%nat.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  have: (n = size (unif_part a b n) - 2)%nat ;
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
    [ rewrite size_mkseq ; intuition | move => {3 5 8 10}->].
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: (unif_part a b n) (unif_part_sort a b n Hab) => {a b Hab n} [| h s] Hs /= Hi.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply lt_n_O in Hi.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: s h Hs Hi => [| h0 s] h Hs /= Hi.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply lt_irrefl in Hi.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  rewrite -minus_n_O ; elim: s h h0 Hs {Hi} => [| h1 s IH] h h0 Hs /= x Hx.
(* Goal: @eq Rlist (@subdivision_val a b (SF_inf_r f a b n)) (if Rle_dec a b then seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part a b n))) else seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part b a n)))) *)
  rewrite /SF_fun /= ; case: Rlt_dec => [Hx0 | _].
(* Goal: @eq Rbar (Finite (IZR Z0)) (Inf_fct f h h0) *)
(* Goal: @eq Rbar (if Rle_dec x h0 then Inf_fct f h h0 else Finite (IZR Z0)) (Inf_fct f h h0) *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  contradict Hx0 ; apply Rle_not_lt, Hx.
(* Goal: @eq Rbar (if Rle_dec x h0 then Inf_fct f h h0 else Finite (IZR Z0)) (Inf_fct f h h0) *)
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Rle_dec => [| Hx0] // ; contradict Hx0 ; apply Hx.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  rewrite -(IH h0 h1 (proj2 Hs) x Hx).
(* Goal: @eq Rlist (@subdivision_val a b (SF_inf_r f a b n)) (if Rle_dec a b then seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part a b n))) else seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part b a n)))) *)
  rewrite /SF_fun /= ; case: Rlt_dec => [ Hx0 | _ ] //.
  contradict Hx0 ; apply Rle_not_lt, Rle_trans with (1 := proj1 Hs),
  Rle_trans with (2 := proj1 Hx), (sorted_head [:: h0, h1 & s] _ (proj2 Hs)) ;
(* Goal: le i (Init.Nat.pred (@size R (@Datatypes.cons R h s))) *)
  simpl; intuition.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Rlt_dec => [ Hx0 | _ ] //.
  contradict Hx0 ; apply Rle_not_lt, Rle_trans with (2 := proj1 Hx),
(* Goal: le i (Init.Nat.pred (@size R (@Datatypes.cons R h s))) *)
  (sorted_head [:: h0, h1 & s] _ (proj2 Hs)) ; simpl; intuition.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite size_mkseq ; by apply lt_n_S, lt_O_Sn.
Qed.

(** ** SF_sup_real is a StepFun *)

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma ad_SF_sup_r (f : R -> R) (a b : R) (n : nat) :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  ((a <= b) -> adapted_couple (fun x => real (SF_sup_fun f a b n x)) a b
      (seq2Rlist (unif_part a b n))
      (seq2Rlist (behead (pairmap (fun x y => real (Sup_fct f x y)) 0 (unif_part a b n)))))
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  /\ (~(a <= b) -> adapted_couple (fun x => real (SF_sup_fun f a b n x)) a b
      (seq2Rlist (unif_part b a n))
      (seq2Rlist (behead (pairmap (fun x y => real (Sup_fct f x y)) 0 (unif_part b a n))))).
Proof.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  wlog : a b / (a <= b) => [Hw|Hab].
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus (@SF_h R ptd) (@fst R R h0)) (f (@snd R R h0))) (@foldr (ModuleSpace.sort R_Ring V) (ModuleSpace.sort R_Ring V) (@plus (ModuleSpace.AbelianGroup R_Ring V)) (@zero (ModuleSpace.AbelianGroup R_Ring V)) (@pairmap (prod R R) (ModuleSpace.sort R_Ring V) (fun x y : prod R R => @scal R_Ring V (Rminus (@fst R R y) (@fst R R x)) (f (@snd R R y))) (@pair R R (@SF_h R ptd) (@snd R R h0)) (@SF_t R ptd)))) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus (@SF_h R ptd) (@fst R R h0)) (f (@snd R R h0))) (@foldr (ModuleSpace.sort R_Ring V) (ModuleSpace.sort R_Ring V) (@plus (ModuleSpace.AbelianGroup R_Ring V)) (@zero (ModuleSpace.AbelianGroup R_Ring V)) (@pairmap (prod R R) (ModuleSpace.sort R_Ring V) (fun x y : prod R R => @scal R_Ring V (Rminus (@fst R R y) (@fst R R x)) (f (@snd R R y))) (@pair R R (@SF_h R ptd) (@zero R_AbelianGroup)) (@SF_t R ptd)))) *)
  case: (Rle_dec a b) => // Hab ; split => // _.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    by apply (Hw a b).
    apply Rnot_le_lt, Rlt_le in Hab ;
    case : (Hw b a Hab) => {Hw} Hw _ ;
    move: (Hw Hab) => {Hw} Hw ;
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
    rewrite /adapted_couple in Hw |-* ; rewrite Rmin_comm Rmax_comm ;
    intuition => x Hx ; rewrite SF_sup_fun_bound ; by apply H4.
(* Goal: and (forall _ : Rle a b, adapted_couple (@SF_val_fun R f a b n) a b (seq2Rlist (unif_part a b n)) (seq2Rlist (@SF_val_ly R f a b n))) (forall _ : not (Rle a b), adapted_couple (@SF_val_fun R f b a n) a b (seq2Rlist (unif_part b a n)) (seq2Rlist (@SF_val_ly R f b a n))) *)
  split ; case: (Rle_dec a b)=> // _ _.
(* Goal: @eq Rlist (@subdivision_val a b (SF_inf_r f a b n)) (if Rle_dec a b then seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part a b n))) else seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part b a n)))) *)
  rewrite /SF_sup_fun ; case: (Rle_dec a b) => // _.
(* Goal: adapted_couple (@SF_fun R s z0) (@head R (IZR Z0) (@SF_lx R s)) (@last R (IZR Z0) (@SF_lx R s)) (seq2Rlist (@SF_lx R s)) (seq2Rlist (@SF_ly R s)) *)
  have Hs : (SF_sorted Rle (SF_map real (SF_sup_seq f a b n))).
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    rewrite /SF_sorted SF_map_lx SF_lx_f2.
    replace (head 0 (unif_part a b n) :: behead (unif_part a b n))
(* Goal: le i (Init.Nat.pred (@size R (@Datatypes.cons R h s))) *)
    with (unif_part a b n) by intuition.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    by apply unif_part_sort.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    by apply lt_O_Sn.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  have {2}<-: head 0 (unif_part a b n) = a.
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
    apply head_unif_part.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  have {3}<-: last 0 (unif_part a b n) = b.
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
    apply last_unif_part.
  replace (behead
    (pairmap (fun x y : R => real (Sup_fct f x y)) 0 (unif_part a b n)))
    with (SF_ly (SF_map real (SF_sup_seq f a b n))).
  replace (unif_part a b n)
  with (SF_lx (SF_map real (SF_sup_seq f a b n))).
(* Goal: forall (i : nat) (_ : lt (S i) (@size R (unif_part a b n))), @eq R (Rminus (@nth R (IZR Z0) (unif_part a b n) (S i)) (@nth R (IZR Z0) (unif_part a b n) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
  move: (ad_SF_compat (f ((0+0)/2)) (SF_map real (SF_sup_seq f a b n)) Hs) ;
(* Goal: le i (Init.Nat.pred (@size R (@Datatypes.cons R h s))) *)
  rewrite /adapted_couple => Had ; intuition.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  move: (H4 i H3) => {H4} H3' x H4.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  move: (H3' x H4) => {H3'} <-.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  rewrite -(SF_fun_map real).

(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  2: rewrite SF_map_lx SF_lx_f2 // ; by apply lt_O_Sn.
  2: rewrite SF_map_ly SF_ly_f2 ;
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  by rewrite -behead_map map_pairmap.

(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  move: H3 H4.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite /SF_sup_seq.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite !nth_compat size_compat SF_map_lx SF_lx_f2.
(* Goal: lt O (@size R (@Datatypes.cons R (@SF_h R ptd) (@map (prod R R) R (@fst R R) (@SF_t R ptd)))) *)
(* Goal: Rle (@last R (@SF_h R ptd) (@unzip1 R R (@SF_t R ptd))) (@SF_h R ptd) *)
  2: apply lt_O_Sn.
  unfold SF_fun.
(* Goal: forall (i : nat) (_ : lt (S i) (@size R (unif_part a b n))), @eq R (Rminus (@nth R (IZR Z0) (unif_part a b n) (S i)) (@nth R (IZR Z0) (unif_part a b n) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
  elim: (unif_part a b n) (unif_part_sort a b n Hab) {3}(0) {1}(f ((0 + 0) / 2)) i => [ | x0 l IH] Hl z0 z1 i Hi Hx.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply lt_n_O in Hi.
  simpl in Hi.
(* Goal: Rle (seq_step (@Datatypes.cons R x0 l)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt (S i) (@size R (unif_part a b n))), @eq R (Rminus (@nth R (IZR Z0) (unif_part a b n) (S i)) (@nth R (IZR Z0) (unif_part a b n) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
  destruct l as [ | x1 l].
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply lt_n_O in Hi.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite SF_cons_f2.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  2: by apply lt_O_Sn.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite SF_map_cons.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: i Hi Hx => [ | i] Hi /= Hx.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Rlt_dec => Hx0 //.
  contradict Hx0 ; apply Rle_not_lt, Rlt_le, Hx.
  case: (l)  => [ | x2 l'] /=.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Rle_dec => // Hx1.
  contradict Hx1 ; by apply Rlt_le, Hx.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Rlt_dec (proj2 Hx) => //.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Rlt_dec => //= Hx0.
  contradict Hx0.
  apply Rle_not_lt, Rlt_le.
  eapply Rle_lt_trans, Hx.
  eapply Rle_trans, sorted_head.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply Hl.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply Hl.
  eapply lt_trans, Hi.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply lt_n_Sn.
  eapply (IH (proj2 Hl) (Sup_fct f x0 x1) (Sup_fct f x0 x1)).
  2: apply Hx.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  simpl ; by apply lt_S_n.
Qed.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Definition SF_sup_r (f : R -> R) (a b : R) (n : nat) : StepFun a b.
Proof.
  exists (fun x => real (SF_sup_fun f a b n x)) ;
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case : (Rle_dec a b) => Hab.
  exists (seq2Rlist (unif_part a b n)) ;
  exists (seq2Rlist (behead (pairmap (fun x y => real (Sup_fct f x y)) 0 (unif_part a b n)))) ;
  by apply ad_SF_sup_r.
  exists (seq2Rlist ((unif_part b a n))) ;
  exists (seq2Rlist (behead (pairmap (fun x y => real (Sup_fct f x y)) 0 (unif_part b a n)))) ;
  by apply ad_SF_sup_r.
Defined.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_sup_subdiv (f : R -> R) (a b : R) (n : nat) :
  subdivision (SF_sup_r f a b n) =
  match (Rle_dec a b) with
    | left _ => seq2Rlist (unif_part a b n)
    | right _ => seq2Rlist (unif_part b a n)
  end.
Proof.
(* Goal: @eq Rlist (@subdivision_val a b (SF_inf_r f a b n)) (if Rle_dec a b then seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part a b n))) else seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part b a n)))) *)
  rewrite /SF_sup_r ; case: (Rle_dec a b) => Hab //.
Qed.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_sup_subdiv_val (f : R -> R) (a b : R) (n : nat) :
  subdivision_val (SF_sup_r f a b n) =
  match (Rle_dec a b) with
    | left _ => (seq2Rlist (behead (pairmap (fun x y => real (Sup_fct f x y)) 0 (unif_part a b n))))
    | right _ => (seq2Rlist (behead (pairmap (fun x y => real (Sup_fct f x y)) 0 (unif_part b a n))))
  end.
Proof.
(* Goal: @eq Rlist (@subdivision_val a b (SF_inf_r f a b n)) (if Rle_dec a b then seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part a b n))) else seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part b a n)))) *)
  rewrite /SF_sup_r ; case: (Rle_dec a b) => Hab //.
Qed.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_sup_r_bound (f : R -> R) (a b : R) (n : nat) :
  forall x, SF_sup_r f a b n x = SF_sup_r f b a n x.
Proof.
(* Goal: forall x : R, @eq R (@fe a b (SF_inf_r f a b n) x) (@fe b a (SF_inf_r f b a n) x) *)
  move => x /= ; by rewrite SF_sup_fun_bound.
Qed.

(** ** SF_inf_real is a StepFun *)

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma ad_SF_inf_r (f : R -> R) (a b : R) (n : nat) :
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  ((a <= b) -> adapted_couple (fun x => real (SF_inf_fun f a b n x)) a b
      (seq2Rlist (unif_part a b n))
      (seq2Rlist (behead (pairmap (fun x y => real (Inf_fct f x y)) 0 (unif_part a b n)))))
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  /\ (~(a <= b) -> adapted_couple (fun x => real (SF_inf_fun f a b n x)) a b
      (seq2Rlist (unif_part b a n))
      (seq2Rlist (behead (pairmap (fun x y => real (Inf_fct f x y)) 0 (unif_part b a n))))).
Proof.
(* Goal: @eq (@SF_seq Rbar) (@SF_rev Rbar (SF_inf_seq f a b n)) (SF_inf_seq f b a n) *)
  wlog : a b / (a <= b) => [Hw|Hab].
(* Goal: @eq (ModuleSpace.sort R_Ring V) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus (@SF_h R ptd) (@fst R R h0)) (f (@snd R R h0))) (@foldr (ModuleSpace.sort R_Ring V) (ModuleSpace.sort R_Ring V) (@plus (ModuleSpace.AbelianGroup R_Ring V)) (@zero (ModuleSpace.AbelianGroup R_Ring V)) (@pairmap (prod R R) (ModuleSpace.sort R_Ring V) (fun x y : prod R R => @scal R_Ring V (Rminus (@fst R R y) (@fst R R x)) (f (@snd R R y))) (@pair R R (@SF_h R ptd) (@snd R R h0)) (@SF_t R ptd)))) (@plus (ModuleSpace.AbelianGroup R_Ring V) (@scal R_Ring V (Rminus (@SF_h R ptd) (@fst R R h0)) (f (@snd R R h0))) (@foldr (ModuleSpace.sort R_Ring V) (ModuleSpace.sort R_Ring V) (@plus (ModuleSpace.AbelianGroup R_Ring V)) (@zero (ModuleSpace.AbelianGroup R_Ring V)) (@pairmap (prod R R) (ModuleSpace.sort R_Ring V) (fun x y : prod R R => @scal R_Ring V (Rminus (@fst R R y) (@fst R R x)) (f (@snd R R y))) (@pair R R (@SF_h R ptd) (@zero R_AbelianGroup)) (@SF_t R ptd)))) *)
  case: (Rle_dec a b) => // Hab ; split => // _.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    by apply (Hw a b).
    apply Rnot_le_lt, Rlt_le in Hab ;
    case : (Hw b a Hab) => {Hw} Hw _ ;
    move: (Hw Hab) => {Hw} Hw ;
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
    rewrite /adapted_couple in Hw |-* ; rewrite Rmin_comm Rmax_comm ;
    intuition => x Hx ; rewrite SF_inf_fun_bound ; by apply H4.
(* Goal: and (forall _ : Rle a b, adapted_couple (@SF_val_fun R f a b n) a b (seq2Rlist (unif_part a b n)) (seq2Rlist (@SF_val_ly R f a b n))) (forall _ : not (Rle a b), adapted_couple (@SF_val_fun R f b a n) a b (seq2Rlist (unif_part b a n)) (seq2Rlist (@SF_val_ly R f b a n))) *)
  split ; case: (Rle_dec a b)=> // _ _.
(* Goal: @eq Rlist (@subdivision_val a b (SF_inf_r f a b n)) (if Rle_dec a b then seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part a b n))) else seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part b a n)))) *)
  rewrite /SF_inf_fun ; case: (Rle_dec a b) => // _.
(* Goal: adapted_couple (@SF_fun R s z0) (@head R (IZR Z0) (@SF_lx R s)) (@last R (IZR Z0) (@SF_lx R s)) (seq2Rlist (@SF_lx R s)) (seq2Rlist (@SF_ly R s)) *)
  have Hs : (SF_sorted Rle (SF_map real (SF_inf_seq f a b n))).
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    rewrite /SF_sorted SF_map_lx SF_lx_f2.
    replace (head 0 (unif_part a b n) :: behead (unif_part a b n))
(* Goal: le i (Init.Nat.pred (@size R (@Datatypes.cons R h s))) *)
    with (unif_part a b n) by intuition.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    by apply unif_part_sort.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
    by apply lt_O_Sn.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  have {2}<-: head 0 (unif_part a b n) = a.
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
    apply head_unif_part.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  have {3}<-: last 0 (unif_part a b n) = b.
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
    apply last_unif_part.
  replace (behead
    (pairmap (fun x y : R => real (Inf_fct f x y)) 0 (unif_part a b n)))
    with (SF_ly (SF_map real (SF_inf_seq f a b n))).
  replace (unif_part a b n)
  with (SF_lx (SF_map real (SF_inf_seq f a b n))).
(* Goal: forall (i : nat) (_ : lt (S i) (@size R (unif_part a b n))), @eq R (Rminus (@nth R (IZR Z0) (unif_part a b n) (S i)) (@nth R (IZR Z0) (unif_part a b n) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
  move: (ad_SF_compat (f ((0+0)/2)) (SF_map real (SF_inf_seq f a b n)) Hs) ;
(* Goal: le i (Init.Nat.pred (@size R (@Datatypes.cons R h s))) *)
  rewrite /adapted_couple => Had ; intuition.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  move: (H4 i H3) => {H4} H3' x H4.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  move: (H3' x H4) => {H3'} <-.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  rewrite -(SF_fun_map real).

(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  2: rewrite SF_map_lx SF_lx_f2 // ; by apply lt_O_Sn.
  2: rewrite SF_map_ly SF_ly_f2 ;
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
  by rewrite -behead_map map_pairmap.

(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (unif_part a b n)) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (unif_part a b n) n) (@nth R (IZR Z0) (unif_part a b n) (S n))) *)
  move: H3 H4.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite /SF_inf_seq.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite !nth_compat size_compat SF_map_lx SF_lx_f2.
(* Goal: lt O (@size R (@Datatypes.cons R (@SF_h R ptd) (@map (prod R R) R (@fst R R) (@SF_t R ptd)))) *)
(* Goal: Rle (@last R (@SF_h R ptd) (@unzip1 R R (@SF_t R ptd))) (@SF_h R ptd) *)
  2: apply lt_O_Sn.
  unfold SF_fun.
(* Goal: forall (i : nat) (_ : lt (S i) (@size R (unif_part a b n))), @eq R (Rminus (@nth R (IZR Z0) (unif_part a b n) (S i)) (@nth R (IZR Z0) (unif_part a b n) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
  elim: (unif_part a b n) (unif_part_sort a b n Hab) {3}(0) {1}(f ((0 + 0) / 2)) i => [ | x0 l IH] Hl z0 z1 i Hi Hx.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply lt_n_O in Hi.
  simpl in Hi.
(* Goal: Rle (seq_step (@Datatypes.cons R x0 l)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt (S i) (@size R (unif_part a b n))), @eq R (Rminus (@nth R (IZR Z0) (unif_part a b n) (S i)) (@nth R (IZR Z0) (unif_part a b n) i)) (Rdiv (Rminus b a) (Rplus (INR n) (IZR (Zpos xH)))) *)
(* Goal: forall (i : nat) (_ : lt i (@SF_size R (@SF_seq_f2 R f (unif_part a b n)))), and (Rle (@nth R (IZR Z0) (unif_part a b n) i) (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i)) (Rle (@nth R (IZR Z0) (@SF_ly R (@SF_seq_f2 R f (unif_part a b n))) i) (@nth R (IZR Z0) (unif_part a b n) (S i))) *)
(* Goal: @eq R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) a *)
(* Goal: @eq R (@last R (@SF_h R (@SF_seq_f2 R f (unif_part a b n))) (unif_part a b n)) b *)
(* Goal: lt O (@size R (unif_part a b n)) *)
  destruct l as [ | x1 l].
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply lt_n_O in Hi.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite SF_cons_f2.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  2: by apply lt_O_Sn.
(* Goal: @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h0 (@Datatypes.cons R h1 s)) (@size R s)) (@nth R (IZR Z0) (@Datatypes.cons R h1 s) (@size R s))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  rewrite SF_map_cons.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: i Hi Hx => [ | i] Hi /= Hx.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Rlt_dec => Hx0 //.
  contradict Hx0 ; apply Rle_not_lt, Rlt_le, Hx.
  case: (l)  => [ | x2 l'] /=.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Rle_dec => // Hx1.
  contradict Hx1 ; by apply Rlt_le, Hx.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Rlt_dec (proj2 Hx) => //.
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case: Rlt_dec => //= Hx0.
  contradict Hx0.
  apply Rle_not_lt, Rlt_le.
  eapply Rle_lt_trans, Hx.
  eapply Rle_trans, sorted_head.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply Hl.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply Hl.
  eapply lt_trans, Hi.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  by apply lt_n_Sn.
  eapply (IH (proj2 Hl) (Inf_fct f x0 x1) (Inf_fct f x0 x1)).
  2: apply Hx.
(* Goal: forall (x : R) (_ : and (Rle h x) (Rle x (IZR Z0))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.nil R))) (Finite (IZR Z0)) x) (Inf_fct f h (IZR Z0)) *)
(* Goal: forall (x : R) (_ : and (Rle (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) x) (Rle x (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O)))), @eq Rbar (@SF_fun Rbar (@SF_seq_f2 Rbar (Inf_fct f) (@Datatypes.cons R h (@Datatypes.cons R h0 s))) (Finite (IZR Z0)) x) (Inf_fct f (@nth R (IZR Z0) (@Datatypes.cons R h (@Datatypes.cons R h0 s)) (Init.Nat.sub (@size R s) O)) (@nth R (IZR Z0) (@Datatypes.cons R h0 s) (Init.Nat.sub (@size R s) O))) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  simpl ; by apply lt_S_n.
Qed.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Definition SF_inf_r (f : R -> R) (a b : R) (n : nat) : StepFun a b.
Proof.
  exists (fun x => real (SF_inf_fun f a b n x)) ;
(* Goal: @eq Rbar (if Rlt_dec x h0 then Inf_fct f h h0 else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) (if Rlt_dec x h0 then Finite (IZR Z0) else @SF_fun_aux Rbar (@pair R Rbar h1 (Inf_fct f h0 h1)) (@pairmap R (prod R Rbar) (fun x y : R => @pair R Rbar y (Inf_fct f x y)) h1 s) (Finite (IZR Z0)) x) *)
(* Goal: lt (S O) (@size R (unif_part a b n)) *)
  case : (Rle_dec a b) => Hab.
  exists (seq2Rlist (unif_part a b n)) ;
  exists (seq2Rlist (behead (pairmap (fun x y => real (Inf_fct f x y)) 0 (unif_part a b n)))) ;
  by apply ad_SF_inf_r.
  exists (seq2Rlist ((unif_part b a n))) ;
  exists (seq2Rlist (behead (pairmap (fun x y => real (Inf_fct f x y)) 0 (unif_part b a n)))) ;
  by apply ad_SF_inf_r.
Defined.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_inf_subdiv (f : R -> R) (a b : R) (n : nat) :
  subdivision (SF_inf_r f a b n) =
  match (Rle_dec a b) with
    | left _ => seq2Rlist (unif_part a b n)
    | right _ => seq2Rlist (unif_part b a n)
  end.
Proof.
(* Goal: @eq Rlist (@subdivision_val a b (SF_inf_r f a b n)) (if Rle_dec a b then seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part a b n))) else seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part b a n)))) *)
  rewrite /SF_inf_r ; case: (Rle_dec a b) => Hab //.
Qed.
(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_inf_subdiv_val (f : R -> R) (a b : R) (n : nat) :
  subdivision_val (SF_inf_r f a b n) =
  match (Rle_dec a b) with
    | left _ => (seq2Rlist (behead (pairmap (fun x y => real (Inf_fct f x y)) 0 (unif_part a b n))))
    | right _ => (seq2Rlist (behead (pairmap (fun x y => real (Inf_fct f x y)) 0 (unif_part b a n))))
  end.
Proof.
(* Goal: @eq Rlist (@subdivision_val a b (SF_inf_r f a b n)) (if Rle_dec a b then seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part a b n))) else seq2Rlist (@behead R (@pairmap R R (fun x y : R => real (Inf_fct f x y)) (IZR Z0) (unif_part b a n)))) *)
  rewrite /SF_inf_r ; case: (Rle_dec a b) => Hab //.
Qed.

(* Goal: @Filter (@SF_seq R) (Riemann_fine a b) *)
Lemma SF_inf_r_bound (f : R -> R) (a b : R) (n : nat) :
  forall x, SF_inf_r f a b n x = SF_inf_r f b a n x.
Proof.
(* Goal: forall x : R, @eq R (@fe a b (SF_inf_r f a b n) x) (@fe b a (SF_inf_r f b a n) x) *)
  move => x /= ; by rewrite SF_inf_fun_bound.
Qed.
