Definition partial_stream_len {A: Type} (h: stream A) (n: nat): option nat :=
match h n with
| Some _ => None
| None => Some
((fix f (m: nat): nat :=
match h m with
| Some _ => S m
| None => match m with
| 0 => 0
| S m0 => f m0
end
end) n)
end.

Lemma partial_stream_len_Some {A: Type}: forall (h: stream A) (n m: nat),
n <= m ->
is_n_stream n h ->
partial_stream_len h m = Some n.