If I want to construct of list of all (obviously finite) prefixes of a list, I can define this function:
Fixpoint list_prefixes {A : Type} (l : list A) : list (list A) :=
match l with
| [] => [[]]
| a :: l' => [] :: map (cons a) (list_prefixes l')
end.
I tried to do the same for streams, and the most natural way for me is:
CoFixpoint stream_prefixes {A : Type} (s : Stream A) : Stream (list A) :=
match s with
| Streams.Cons a s' => Streams.Cons [] (Streams.map (cons a) (stream_prefixes s'))
end.
Which naturally fails, since I'm applying stream s'
to a function which is not a constructor (Streams.map
).
How can I construct a stream of all finite prefixes?