Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Alain Frisch <Alain.Frisch@inria.fr>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Generators/iterators and lazy evaluation?
Date: Thu, 05 Apr 2007 07:58:40 +0200	[thread overview]
Message-ID: <46149010.30505@inria.fr> (raw)
In-Reply-To: <200704050053.41426.jon@ffconsultancy.com>

Jon Harrop wrote:
> The moral is: don't try to write idiomatic Python in OCaml.

I think the moral is rather: read the OP's email more carefully. He
doesn't want to translate some Python examples into OCaml by hand, he
wants to implement a Python interpreter in OCaml.

Erik de Castro Lopo wrote:
> Typically the original poster's pow2 generators would be used like:
>
>     for p = pow2 (10):
>         print p
>
> but I really don't see how laziness Python's generators actually
> provide any benefit for this problem over the more obvious Python
> solution to this which is:

We're not discussing (I think) a specific use case for generators, but a
generic way to support them in an interpreter.

> The problem with the above is that generators (and laziness in Ocaml)
> really only make sense for sequences which are effecitvely infinite.

I don't think so: generators provide a simple control operator which is
sometimes useful, even for finite structures, and easier to grasp than
call/cc-like operators. Typically, generators allow their client to be
written in direct style. E.g. using a push XML event parser (SAX-style)
to produce a tree requires you to maintain your own stack accross
invocations of the callbacks. Instead, a pull parser
(generator-style) lets you write a very simple direct code. You can turn
a push parser into a pull parser either with some kind of control
inversion (CPS, generators, ...), or, in this case, by collecting all
the results in a list. But: (1) this works only because the callback
can't have side effects that would change the future events;  (2) you
need to keep all the events in memory, and you cannot stop the parsing
early.

Moreover, I don't really see the connection with the notion of laziness
in OCaml. If you want to turn the generator definition into something
that produces a lazy sequence, you'll also need some kind of control
inversion.


-- Alain


  parent reply	other threads:[~2007-04-05  5:58 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-04-04 16:33 Raj B
2007-04-04 20:10 ` [Caml-list] " Mathias Kende
2007-04-04 20:46 ` Alain Frisch
2007-04-04 21:16   ` Erik de Castro Lopo
2007-04-04 21:37     ` Erik de Castro Lopo
2007-04-04 22:55     ` Bill Wood
2007-04-05  2:49     ` skaller
2007-04-05 16:41     ` Richard Jones
2007-04-04 23:53 ` Jon Harrop
2007-04-05  3:13   ` Erik de Castro Lopo
2007-04-05  5:58   ` Alain Frisch [this message]
2007-04-05 20:35     ` Jon Harrop

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=46149010.30505@inria.fr \
    --to=alain.frisch@inria.fr \
    --cc=caml-list@yquem.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox