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.

(* Specifications: *)
Lemma partial_stream_len_mono1: forall {A: Type} (h: stream A) (x y: nat), x <= y -> partial_stream_len h y = None -> partial_stream_len h x = None.
Lemma partial_stream_len_mono2: forall {A: Type} (h: stream A) (x y n: nat), x <= y -> partial_stream_len h x = Some n -> partial_stream_len h y = Some n.
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.
Lemma partial_stream_len_None {A: Type}: forall (h: stream A) (m: nat), is_at_least_n_stream (S m) h -> partial_stream_len h m = None.