(*******************************************)
(** *          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:list R) : Prop :=
  ordered_Rlist l /\
  pos_Rl l 0 = Rmin a b /\
  pos_Rl l (pred (length l)) = Rmax a b /\
  length l = S (length lf) /\
  (forall i:nat,
    (i < pred (length 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:list R) :=
  adapted_couple f a b l lf /\
  (forall i:nat,
    (i < pred (length 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 (length l))%nat -> pos_Rl l i <> pos_Rl l (S i)).Definition is_subdivision (f:R -> R) (a b:R) (l:list R) : Type :=
  { l0:list R & adapted_couple f a b l l0 }.Definition IsStepFun (f:R -> R) (a b:R) : Type :=
  { l:list R & 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}.(** ** Class of step functions *)
Record StepFun (a b:R) : Type := mkStepFun
  {fe :> R -> R; pre : IsStepFun fe a b}.(** ** Class of step functions *)
Record StepFun (a b:R) : Type := mkStepFun
  {fe :> R -> R; pre : IsStepFun fe a b}.(** ** 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) : list R := projT1 (pre f).Definition subdivision_val (a b:R) (f:StepFun a b) : list R :=
  match projT2 (pre f) with
    | existT _ a b => a
  end.Fixpoint Int_SF (l k:list R) : 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.

Lemma StepFun_P11 : forall (a b r r1 r3 s1 s2 r4:R) (r2 lf1 s3 lf2:list R) (f:R -> R), a < b -> adapted_couple f a b (cons r (cons r1 r2)) (cons r3 lf1) -> adapted_couple_opt f a b (cons s1 (cons s2 s3)) (cons r4 lf2) -> r1 <= s2.