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'_None_iff {A: Type}: forall (h: nat -> stream A) (n: nat),
match partial_stream_clen' h n return option A with
| Some (k, m) => h k m
| None => None
end = None <->
partial_stream_clen' h n = None.