caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] lazy lists
@ 2002-07-03  9:45 Michael Vanier
  2002-07-03 22:04 ` Remi VANICAT
  0 siblings, 1 reply; 11+ messages in thread
From: Michael Vanier @ 2002-07-03  9:45 UTC (permalink / raw)
  To: caml-list


What's the best way to write a lazy list data type in ocaml?  I've been
playing around with this datatype (from an old mailing list posting):

type 'a stream =
  Nil
| Cons of 'a Lazy.t * 'a stream Lazy.t

but I can't figure out how to write a "stream_cons" function.  Also, it
appears that the Lazy.t data type uses references; why is this?

Mike
-------------------
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] 11+ messages in thread

* Re: [Caml-list] lazy lists
  2002-07-03  9:45 [Caml-list] lazy lists Michael Vanier
@ 2002-07-03 22:04 ` Remi VANICAT
  0 siblings, 0 replies; 11+ messages in thread
From: Remi VANICAT @ 2002-07-03 22:04 UTC (permalink / raw)
  To: caml-list

Michael Vanier <mvanier@cs.caltech.edu> writes:

> What's the best way to write a lazy list data type in ocaml?  I've been
> playing around with this datatype (from an old mailing list posting):
>
> type 'a stream =
>   Nil
> | Cons of 'a Lazy.t * 'a stream Lazy.t
>
> but I can't figure out how to write a "stream_cons" function.  

you mean :

let stream_cons x y =
  lazy Cons (x, y)


> Also, it
> appears that the Lazy.t data type uses references; why is this?

Because it is the standard way of doing it with a non lazy
language... 
-- 
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] 11+ messages in thread

* Re: [Caml-list] lazy lists
  2007-05-02 10:03       ` Loup Vaillant
@ 2007-05-02 10:15         ` Daniel de Rauglaudre
  0 siblings, 0 replies; 11+ messages in thread
From: Daniel de Rauglaudre @ 2007-05-02 10:15 UTC (permalink / raw)
  To: caml-list

Hi,

On Wed, May 02, 2007 at 12:03:29PM +0200, Loup Vaillant wrote:

> Regular streams are destructive. I was talking about "functional"
> ones. I didn't try them myself, but the manual says one can perform
> left recursion with them (thanks to their non destructive nature).

Use camlp4s : the library "fstream" and the syntax extension "pa_fstream"
are still there. Access and download from my home page below.

-- 
Daniel de Rauglaudre
http://pauillac.inria.fr/~ddr/index-english.html


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

* Re: [Caml-list] lazy lists
  2007-05-02  7:16     ` Nicolas Pouillard
@ 2007-05-02 10:03       ` Loup Vaillant
  2007-05-02 10:15         ` Daniel de Rauglaudre
  0 siblings, 1 reply; 11+ messages in thread
From: Loup Vaillant @ 2007-05-02 10:03 UTC (permalink / raw)
  To: Caml mailing list

2007/5/2, Nicolas Pouillard <nicolas.pouillard@gmail.com>:
> On 5/1/07, Jon Harrop <jon@ffconsultancy.com> wrote:
> > On Tuesday 01 May 2007 16:47, Loup Vaillant wrote:
> > > The equivalent is the functionnal streams, describe briefly in the
> > > camlp4 manual.
> >
> > Aren't ocaml stream destructive?
>
> Yes they are.

Regular streams are destructive. I was talking about "functional"
ones. I didn't try them myself, but the manual says one can perform
left recursion with them (thanks to their non destructive nature).
Mind the pattern matching, though: it is quite different from the one
used with regular streams.

the syntax looks like:

let f s = match s with fparser (* and not just 'parser' *)
...

If I remember well.

Loup Vaillant


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

* Re: [Caml-list] lazy lists
  2007-05-01 19:43   ` Jon Harrop
@ 2007-05-02  7:16     ` Nicolas Pouillard
  2007-05-02 10:03       ` Loup Vaillant
  0 siblings, 1 reply; 11+ messages in thread
From: Nicolas Pouillard @ 2007-05-02  7:16 UTC (permalink / raw)
  To: Jon Harrop; +Cc: caml-list

On 5/1/07, Jon Harrop <jon@ffconsultancy.com> wrote:
> On Tuesday 01 May 2007 16:47, Loup Vaillant wrote:
> > The equivalent is the functionnal streams, describe briefly in the
> > camlp4 manual.
>
> Aren't ocaml stream destructive?

Yes they are.

-- 
Nicolas Pouillard


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

* Re: [Caml-list] lazy lists
  2007-05-01 15:47 ` [Caml-list] " Loup Vaillant
@ 2007-05-01 19:43   ` Jon Harrop
  2007-05-02  7:16     ` Nicolas Pouillard
  0 siblings, 1 reply; 11+ messages in thread
From: Jon Harrop @ 2007-05-01 19:43 UTC (permalink / raw)
  To: caml-list

On Tuesday 01 May 2007 16:47, Loup Vaillant wrote:
> The equivalent is the functionnal streams, describe briefly in the
> camlp4 manual.

Aren't ocaml stream destructive?

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
The F#.NET Journal
http://www.ffconsultancy.com/products/fsharp_journal/?e


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

* Re: [Caml-list] lazy lists
  2007-05-01 14:10 Yitzhak Mandelbaum
@ 2007-05-01 15:47 ` Loup Vaillant
  2007-05-01 19:43   ` Jon Harrop
  0 siblings, 1 reply; 11+ messages in thread
From: Loup Vaillant @ 2007-05-01 15:47 UTC (permalink / raw)
  To: caml-list

2007/5/1, Yitzhak Mandelbaum <yitzhak@research.att.com>:
> Hi,
>
> Does anyone have/know of a lazy list module for ocaml? I couldn't find
> anything on the hump.

The equivalent is the functionnal streams, describe briefly in the
camlp4 manual.

Another possibilty is to code it yourself. I think you can imitate
most of the List module code.

Regards,
Loup


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

* Re: [Caml-list] lazy lists
  2005-08-26 11:04 ` Richard Jones
@ 2005-08-26 11:16   ` Jun Mukai
  0 siblings, 0 replies; 11+ messages in thread
From: Jun Mukai @ 2005-08-26 11:16 UTC (permalink / raw)
  To: caml-list

Hi, Rich

> What you probably want are Streams (in camlp4).  This thread is
> interesting:

It is true that Stream is lazy evaluated, but Stream is not functional
at all. We cannot use Streams as same as lists in Haskell. For example,
--
# let s = [< '1 >];;
val s : int Stream.t = <abstr>
# let f = parser
  | [< 'n >] -> n
  | [< >] -> raise Not_found;;
val f : 'a Stream.t -> 'a = <fun>
# f s;;
- : int = 1
# f s;;
Exception: Not_found.
--

This code confuses many users (such like me).


BTW, I wrote a lazy list implementation in pure OCaml. It uses lazy
mechanism of OCaml and has some useful functions like haskell.
If you are interested in, see 
http://www.jmuk.org/prog/lazyList.tar.gz

The implementation is so naive that its performance will be low. And
it has no camlp4 extentions.
Feel free to use, but without any warranty.

Best Regards,
--
Jun Mukai


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

* Re: [Caml-list] lazy lists
  2005-08-25 23:56 Jonathan Roewen
  2005-08-26 10:56 ` Marius Nita
@ 2005-08-26 11:04 ` Richard Jones
  2005-08-26 11:16   ` Jun Mukai
  1 sibling, 1 reply; 11+ messages in thread
From: Richard Jones @ 2005-08-26 11:04 UTC (permalink / raw)
  To: Jonathan Roewen; +Cc: caml-list

On Fri, Aug 26, 2005 at 11:56:33AM +1200, Jonathan Roewen wrote:
> Is the underlying implementation of the builtin lists in OCaml lazy?
> If not, is performance the reason for them not being lazy? I think
> lazy lists are a very strong point of Haskell, and am wondering if
> there's a lazy list implementation with all the standard list
> operations in the case that lists aren't lazy in OCaml.

What you probably want are Streams (in camlp4).  This thread is
interesting:

http://caml.inria.fr/pub/ml-archives/caml-list/2004/09/9d5e1141c4018b995480ce1630f4879c.en.html

Rich.

-- 
Richard Jones, CTO Merjis Ltd.
Merjis - web marketing and technology - http://merjis.com
Team Notepad - intranets and extranets for business - http://team-notepad.com


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

* Re: [Caml-list] lazy lists
  2005-08-25 23:56 Jonathan Roewen
@ 2005-08-26 10:56 ` Marius Nita
  2005-08-26 11:04 ` Richard Jones
  1 sibling, 0 replies; 11+ messages in thread
From: Marius Nita @ 2005-08-26 10:56 UTC (permalink / raw)
  To: Jonathan Roewen; +Cc: caml-list

Jonathan Roewen wrote:
> Hi,
> 
> Is the underlying implementation of the builtin lists in OCaml lazy?
> If not, is performance the reason for them not being lazy? I think
> lazy lists are a very strong point of Haskell, and am wondering if
> there's a lazy list implementation with all the standard list
> operations in the case that lists aren't lazy in OCaml.
> 
> Also, if they aren't, can someone give me some insight into the
> reasons INRIA chose not to?

It's not just a question of the "underlying implementation." OCaml and
Haskell use the same list definition:

   type 'a list = Cons of 'a * 'a list | Nil

but in Haskell it is lazy due to Haskell's lazy evaluation semantics.
OCaml has a strict evaluation semantics, so its lists, as defined above,
are strict.

If OCaml had chosen to implement lists lazily "behind the scenes," this
would be easily observable (and very counter-intuitive!) due to OCaml's
imperative features:

   let f _ = 5
   in f [print_string "calling f\n"]

would produce no output.

The good news is that you can define your own lazy data structures
pretty easily. OCaml offers the Lazy module, which makes working with
lazy suspensions easier.

	marius


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

* [Caml-list] lazy lists
@ 2005-08-25 23:56 Jonathan Roewen
  2005-08-26 10:56 ` Marius Nita
  2005-08-26 11:04 ` Richard Jones
  0 siblings, 2 replies; 11+ messages in thread
From: Jonathan Roewen @ 2005-08-25 23:56 UTC (permalink / raw)
  To: caml-list

Hi,

Is the underlying implementation of the builtin lists in OCaml lazy?
If not, is performance the reason for them not being lazy? I think
lazy lists are a very strong point of Haskell, and am wondering if
there's a lazy list implementation with all the standard list
operations in the case that lists aren't lazy in OCaml.

Also, if they aren't, can someone give me some insight into the
reasons INRIA chose not to?

Kindest Regards,

Jonathan


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

end of thread, other threads:[~2007-05-02 10:15 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-07-03  9:45 [Caml-list] lazy lists Michael Vanier
2002-07-03 22:04 ` Remi VANICAT
2005-08-25 23:56 Jonathan Roewen
2005-08-26 10:56 ` Marius Nita
2005-08-26 11:04 ` Richard Jones
2005-08-26 11:16   ` Jun Mukai
2007-05-01 14:10 Yitzhak Mandelbaum
2007-05-01 15:47 ` [Caml-list] " Loup Vaillant
2007-05-01 19:43   ` Jon Harrop
2007-05-02  7:16     ` Nicolas Pouillard
2007-05-02 10:03       ` Loup Vaillant
2007-05-02 10:15         ` 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).