(** * 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).

(* Specifications: *)
Lemma SF_cut_down_step s x eps :
SF_h s <= x <= last (SF_h s) (SF_lx s) ->
seq_step (SF_lx s) < eps
-> seq_step (SF_lx (SF_cut_down s x)) < eps.
Lemma SF_cut_up_step s x eps :
SF_h s <= x <= last (SF_h s) (SF_lx s) ->
seq_step (SF_lx s) < eps
-> seq_step (SF_lx (SF_cut_up s x)) < eps.
Lemma SF_cut_down_pointed s x :
SF_h s <= x -> pointed_subdiv s
-> pointed_subdiv (SF_cut_down s x).
Lemma SF_cut_up_pointed s x :
SF_h s <= x ->
pointed_subdiv s
-> pointed_subdiv (SF_cut_up s x).
Lemma SF_cut_down_h s x :
SF_h s <= x -> SF_h (SF_cut_down s x) = SF_h s.
Lemma SF_cut_up_h s x :
SF_h (SF_cut_up s x) = x.
Lemma SF_cut_down_l s x :
last (SF_h (SF_cut_down s x)) (SF_lx (SF_cut_down s x)) = x.
Lemma SF_cut_up_l s x :
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).
Lemma SF_cut_down_cons_0 h ptd x :
x < fst h -> SF_cut_down (SF_cons h ptd) x = SF_nil x.
Lemma SF_cut_up_cons_0 h ptd x :
x < fst h -> SF_cut_up (SF_cons h ptd) x = SF_cons (x,Rmax (fst h) x) (SF_cons h ptd).
Lemma SF_cut_down_cons_1 h ptd x :
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).
Lemma SF_cut_up_cons_1 h ptd x :
fst h <= x < SF_h ptd -> SF_cut_up (SF_cons h ptd) x = SF_cons (x,Rmax (snd h) x) ptd.
Lemma SF_cut_down_cons_2 h ptd x :
fst h <= SF_h ptd <= x -> SF_cut_down (SF_cons h ptd) x = SF_cons h (SF_cut_down ptd x).
Lemma SF_cut_up_cons_2 h ptd x :
fst h <= SF_h ptd <= x -> SF_cut_up (SF_cons h ptd) x = SF_cut_up ptd x.