caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Streams.from
@ 2002-12-09 19:04 Nick Grey
  2002-12-09 22:14 ` Jean-Christophe Filliatre
  0 siblings, 1 reply; 5+ messages in thread
From: Nick Grey @ 2002-12-09 19:04 UTC (permalink / raw)
  To: caml-list

Hi,

When a stream is created using Stream.from, is the function f garunteed
to be called with the stream count parameter 0, 1, 2, ...

I would say that it is, but the current documentation doesn't seem to
discount the possibility that the function would say, be called twice
with the same parameter, or with parameters out of sequence.  (I can
think of no reason why this would actually happen, I'm just saying the
docs don't discount it.)

The reason that I ask is because it is sometimes useful to pass to from
a function which simply returns the next value in the stream, without
referring to the stream count.  (E.g. because the value can't actually
be computed from the stream count, but comes from some internal state.)
In this case, if the function were to be called repeatedly with the same
parameter, or with the parameters out of sequence, bad things would
happen.

I know this is a bit (very? :-) ) pedantic, I'm just slightly uneasy writing code
that makes an assumption about the way a module works.  (After all, the
point of modular programming is that one should not make assumptions
other than those given in the interface/docs.)

Possibly remedies would be to tighten up the documentation of
Stream.from, or to add another version of from which doesn't take the
stream count as a parameter.

Suggestion for change in documentation:

Add at end:
f is called exactly once for each stream element created, and a stream
element is never created until all preceding elements have been created.

Regards,
N
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Streams.from
  2002-12-09 19:04 [Caml-list] Streams.from Nick Grey
@ 2002-12-09 22:14 ` Jean-Christophe Filliatre
  2002-12-09 23:54   ` Remi VANICAT
                     ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Jean-Christophe Filliatre @ 2002-12-09 22:14 UTC (permalink / raw)
  To: Nick Grey; +Cc: caml-list


Nick Grey writes:
 > 
 > When a stream is created using Stream.from, is the function f garunteed
 > to be called with the stream count parameter 0, 1, 2, ...

No. For instance, repeated calls to Stream.peek will clearly call f
several times on the same count value (see the code in stdlib/stream.ml)

-- 
Jean-Christophe Filliâtre (http://www.lri.fr/~filliatr)

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Streams.from
  2002-12-09 22:14 ` Jean-Christophe Filliatre
@ 2002-12-09 23:54   ` Remi VANICAT
  2002-12-10  0:00   ` Nick Grey
  2002-12-10 12:29   ` Daniel de Rauglaudre
  2 siblings, 0 replies; 5+ messages in thread
From: Remi VANICAT @ 2002-12-09 23:54 UTC (permalink / raw)
  To: caml-list

Jean-Christophe Filliatre <Jean-Christophe.Filliatre@lri.fr> writes:

> Nick Grey writes:
>  > 
>  > When a stream is created using Stream.from, is the function f garunteed
>  > to be called with the stream count parameter 0, 1, 2, ...
>
> No. For instance, repeated calls to Stream.peek will clearly call f
> several times on the same count value (see the code in
> stdlib/stream.ml)

Stream.peek cache value from parser created by Stream.from. So the
answer is that with the current implementation of all the function of
the module Stream, f will be call one, and only one time for each
count parameter, and in the good order. But this is not written in the
specification. 



-- 
Rémi Vanicat
vanicat@labri.u-bordeaux.fr
http://dept-info.labri.u-bordeaux.fr/~vanicat
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Streams.from
  2002-12-09 22:14 ` Jean-Christophe Filliatre
  2002-12-09 23:54   ` Remi VANICAT
@ 2002-12-10  0:00   ` Nick Grey
  2002-12-10 12:29   ` Daniel de Rauglaudre
  2 siblings, 0 replies; 5+ messages in thread
From: Nick Grey @ 2002-12-10  0:00 UTC (permalink / raw)
  To: caml-list; +Cc: Jean-Christophe Filliatre

On Mon, Dec 09, 2002 at 11:14:57PM +0100, Jean-Christophe Filliatre wrote:

> No. For instance, repeated calls to Stream.peek will clearly call f
> several times on the same count value (see the code in stdlib/stream.ml)

It seems to me that the values are cached, and that
repeated calls to peek do not cause f to be called again.

Output below demonstrates what I mean.  Note that f is called on the
first peek but not the second.  Similarly with npeek.  Also, the
parameters to f are 0, 1, 2, 3 ..., with no repeats.

        Objective Caml version 3.06

# open Stream ;;
# let f i =
  begin
  print_string "f " ;
  print_int i ;
  print_string "\n" ;
  Some (i*i)
  end ;;
val f : int -> int option = <fun>
# f 1 ;;
f 1
- : int option = Some 1
# f 2 ;;
f 2
- : int option = Some 4
# let s = from f ;;
val s : int Stream.t = <abstr>
# next s ;;
f 0
- : int = 0
# next s ;;
f 1
- : int = 1
# peek s ;;
f 2
- : int option = Some 4
# peek s ;;
- : int option = Some 4
# peek s ;;
- : int option = Some 4
# npeek 5 s ;;
f 3
f 4
f 5
f 6
- : int list = [4; 9; 16; 25; 36]
# npeek 5 s ;;
- : int list = [4; 9; 16; 25; 36]
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Streams.from
  2002-12-09 22:14 ` Jean-Christophe Filliatre
  2002-12-09 23:54   ` Remi VANICAT
  2002-12-10  0:00   ` Nick Grey
@ 2002-12-10 12:29   ` Daniel de Rauglaudre
  2 siblings, 0 replies; 5+ messages in thread
From: Daniel de Rauglaudre @ 2002-12-10 12:29 UTC (permalink / raw)
  To: caml-list

Hi,

On Mon, Dec 09, 2002 at 11:14:57PM +0100, Jean-Christophe Filliatre wrote:
> 
> No. For instance, repeated calls to Stream.peek will clearly call f
> several times on the same count value (see the code in stdlib/stream.ml)

No it does not! Fortunately! Otherwise, all the parsing stuff would
not work.

-- 
Daniel de RAUGLAUDRE
http://cristal.inria.fr/~ddr/
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

end of thread, other threads:[~2002-12-10 12:29 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-12-09 19:04 [Caml-list] Streams.from Nick Grey
2002-12-09 22:14 ` Jean-Christophe Filliatre
2002-12-09 23:54   ` Remi VANICAT
2002-12-10  0:00   ` Nick Grey
2002-12-10 12:29   ` Daniel de Rauglaudre

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).