Fixpoint partial_stream_clen {A: Type} (h: nat -> stream A) (n: nat): nat * nat :=
match n with
| 0 => (0, 0)
| S n => let (k, m) := partial_stream_clen h n in
match h k (S m) with
| Some _ => (k, S m)
| None => (S k, 0)
end
end.
Fixpoint partial_stream_clen' {A: Type} (h: nat -> stream A) (n: nat): option (nat * nat) :=
let (k, m) := partial_stream_clen h n in
match n with
| 0 => match h k m with
| Some _ => Some (k, m)
| None => None
end
| S n => match h k m, partial_stream_clen' h n with
| Some _, Some _ => Some (k, m)
| _, _ => None
end
end.

Lemma partial_stream_clen'_fin0_right {A: Type}: forall (h: nat -> stream A) (n m: nat),
n > 0 ->
is_n_stream n (h 0) ->
partial_stream_clen' h (m + n) =
match partial_stream_clen' (fun k => h (S k)) m with
| Some (k1, k2) => Some (S k1, k2)
| None => None
end.