(************************************************************************)
(*         *   The Coq Proof Assistant / The Coq Development Team       *)
(*  v      *   INRIA, CNRS and contributors - Copyright 1999-2018       *)
(* <O___,, *       (see CREDITS file for the list of authors)           *)
(*   \VV/  **************************************************************)
(*    //   *    This file is distributed under the terms of the         *)
(*         *     GNU Lesser General Public License Version 2.1          *)
(*         *     (see LICENSE file for the text of the license)         *)
(************************************************************************)
Local Open Scope R_scope.
Set Implicit Arguments.
(*****************************************************)
(** * Each bounded subset of N has a maximal element *)
(*****************************************************)
Definition Nbound (I:nat -> Prop) : Prop :=
exists n : nat, (forall i:nat, I i -> (i <= n)%nat).
(*******************************************)
(** *          Step functions              *)
(*******************************************)
Definition open_interval (a b x:R) : Prop := a < x < b.
Definition co_interval (a b x:R) : Prop := a <= x < b.
Definition adapted_couple (f:R -> R) (a b:R) (l lf:Rlist) : Prop :=
ordered_Rlist l /\
pos_Rl l 0 = Rmin a b /\
pos_Rl l (pred (Rlength l)) = Rmax a b /\
Rlength l = S (Rlength lf) /\
(forall i:nat,
(i < pred (Rlength l))%nat ->
constant_D_eq f (open_interval (pos_Rl l i) (pos_Rl l (S i)))
(pos_Rl lf i)).
Definition adapted_couple_opt (f:R -> R) (a b:R) (l lf:Rlist) :=
adapted_couple f a b l lf /\
(forall i:nat,
(i < pred (Rlength lf))%nat ->
pos_Rl lf i <> pos_Rl lf (S i) \/ f (pos_Rl l (S i)) <> pos_Rl lf i) /\
(forall i:nat, (i < pred (Rlength l))%nat -> pos_Rl l i <> pos_Rl l (S i)).
Definition is_subdivision (f:R -> R) (a b:R) (l:Rlist) : Type :=
{ l0:Rlist & adapted_couple f a b l l0 }.
Definition IsStepFun (f:R -> R) (a b:R) : Type :=
{ l:Rlist & is_subdivision f a b l }.
(** ** Class of step functions *)
Record StepFun (a b:R) : Type := mkStepFun
{fe :> R -> R; pre : IsStepFun fe a b}.
Definition subdivision (a b:R) (f:StepFun a b) : Rlist := projT1 (pre f).
Definition subdivision_val (a b:R) (f:StepFun a b) : Rlist :=
match projT2 (pre f) with
| existT _ a b => a
end.
Fixpoint Int_SF (l k:Rlist) : R :=
match l with
| nil => 0
| cons a l' =>
match k with
| nil => 0
| cons x nil => 0
| cons x (cons y k') => a * (y - x) + Int_SF l' (cons y k')
end
end.
(** ** Integral of step functions *)
Definition RiemannInt_SF (a b:R) (f:StepFun a b) : R :=
match Rle_dec a b with
| left _ => Int_SF (subdivision_val f) (subdivision f)
| right _ => - Int_SF (subdivision_val f) (subdivision f)
end.
(************************************)
(** ** Properties of step functions *)
(************************************)

Lemma StepFun_P26 :
forall (a b l:R) (f g:R -> R) (l1:Rlist),
is_subdivision f a b l1 ->
is_subdivision g a b l1 ->
is_subdivision (fun x:R => f x + l * g x) a b l1.