Section RInt_val.
Context {V : ModuleSpace R_Ring}.
Definition RInt_val (f : R -> V) (a b : R) (n : nat) :=
Riemann_sum f (SF_seq_f2 (fun x y => (x + y) / 2) (unif_part a b n)).
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).
(** Build StepFun using SF_seq *)
Definition SF_compat_le (s : @SF_seq R) (pr : SF_sorted Rle s) :
StepFun (head 0 (SF_lx s)) (last 0 (SF_lx s)).
(** Build StepFun using uniform partition *)
Definition sf_SF_val_fun (f : R -> R) (a b : R) (n : nat) : StepFun a b.
(** ** Upper and lower step functions *)
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.
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.
(** SF_sup and SF_inf *)
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).
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).
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.
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.
(** ** SF_sup_real is a StepFun *)
Definition SF_sup_r (f : R -> R) (a b : R) (n : nat) : StepFun a b.
(** ** SF_inf_real is a StepFun *)
Definition SF_inf_r (f : R -> R) (a b : R) (n : nat) : StepFun 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.