caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Infinite Streams in Caml Light 0.5
@ 1992-10-27 19:42 Jerry Hedden
  1992-10-28  9:23 ` Michel Mauny
  0 siblings, 1 reply; 2+ messages in thread
From: Jerry Hedden @ 1992-10-27 19:42 UTC (permalink / raw)
  To: caml-list

There is a point concerning streams in Caml Light that is not
dealt with in the manual.  This involves the use of function
calls in stream patterns.  Such calls are not evaluated in a lazy
manner, and can cause the system to crash when a recursive
function is used on infinite streams.  For example, the following
function is intended to map a function over all the elements of a
stream: 

    let rec map_stream func = function
        [< 'x; (map_stream func) strm >] -> [< '(func x); strm >]
      | [< >] -> [< >]
    ;;

However, as indicated above, if `strm' is infinite, evaluation of 
the recursive call progresses ad infinitum until "out of memory"
occurs.

A version of the function that does work on infinite streams is:

    let rec map_stream func = function
        [< 'x; strm >] -> [< '(func x); map_stream func strm >]
      | [< >] -> [< >]
    ;;

In summary, caution is advised when working with function calls 
on infinite streams.  (Of course, we already knew that.  Didn't 
we?  ;-)


Jerry D. Hedden
hedden@esdsdf.dnet.ge.com





^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Infinite Streams in Caml Light 0.5
  1992-10-27 19:42 Infinite Streams in Caml Light 0.5 Jerry Hedden
@ 1992-10-28  9:23 ` Michel Mauny
  0 siblings, 0 replies; 2+ messages in thread
From: Michel Mauny @ 1992-10-28  9:23 UTC (permalink / raw)
  To: Jerry Hedden; +Cc: caml-list

> There is a point concerning streams in Caml Light that is not
> dealt with in the manual.  This involves the use of function
> calls in stream patterns.  Such calls are not evaluated in a lazy
> manner, and can cause the system to crash when a recursive
> function is used on infinite streams.  For example, the following
> function is intended to map a function over all the elements of a
> stream: 
> 
>     let rec map_stream func = function
>         [< 'x; (map_stream func) strm >] -> [< '(func x); strm >]
>       | [< >] -> [< >]
>     ;;
> 
> However, as indicated above, if `strm' is infinite, evaluation of 
> the recursive call progresses ad infinitum until "out of memory"
> occurs.

Yes, you're right: this fact should be clearly noticed in the manual.
However, this behavior of stream matching is not surprising: as
pattern-matching has a strict behavior in lazy languages,
stream-matching is also strict. One has to check completely the
left-hand part before giving control to the right-hand part of
matching rules. But I admit that it is easy to make such a mistake (I
do it quite often).

Michel Mauny




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~1992-10-28  9:26 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1992-10-27 19:42 Infinite Streams in Caml Light 0.5 Jerry Hedden
1992-10-28  9:23 ` Michel Mauny

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).