Program Definition stream_capp {A: Type} (h: nat -> stream A): stream A :=
fun n: nat =>
match partial_stream_clen' h n return option A with
| Some (k, m) => h k m
| None => None
end.
Next Obligation.
assert (x <= y) by omega; clear H.
rewrite partial_stream_clen'_None_iff in H0 |- *.
induction H1; auto.
clear H0 H1 x.
Opaque partial_stream_clen.
simpl.
Transparent partial_stream_clen.
destruct (partial_stream_clen h (S m)) as [k n].
rewrite IHle.
destruct (h k n); auto.
Qed.

(* Specifications: *)
Lemma stream_capp_spec1 {A: Type}: forall (h: nat -> stream A) (m: nat),
is_at_least_n_stream (S m) (h 0) ->
stream_capp h m = h 0 m.
Lemma stream_capp_spec1' {A: Type}: forall (h: nat -> stream A) (m: nat) a,
h 0 m = Some a ->
stream_capp h m = Some a.
Lemma stream_capp_spec2 {A: Type}: forall (h: nat -> stream A) (n m: nat),
n > 0 ->
is_n_stream n (h 0) ->
stream_capp h (m + n) = stream_capp (fun n => h (S n)) m.
Lemma stream_capp_empty0 {A: Type}: forall (h: nat -> stream A),
is_empty_stream (h 0) ->
is_empty_stream (stream_capp h).
Lemma stream_capp_cons {A: Type}: forall (h: nat -> stream A),
~ is_empty_stream (h 0) ->
stream_capp h = stream_app (h 0) (stream_capp (fun n => h (S n))).
Lemma stream_map_stream_capp {A B: Type}: forall (f: A -> B) (h: nat -> stream A),
stream_map f (stream_capp h) = stream_capp (fun n => stream_map f (h n)).