(*******************************************)
(** *          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_P38 : forall (l:list R) (a b:R) (f:R -> R), ordered_Rlist l -> pos_Rl l 0 = a -> pos_Rl l (pred (length l)) = b -> { g:StepFun a b | g b = f b /\ (forall i:nat, (i < pred (length l))%nat -> constant_D_eq g (co_interval (pos_Rl l i) (pos_Rl l (S i))) (f (pos_Rl l i))) }.