From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.1.3 Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 0362ABC0A for ; Thu, 5 Apr 2007 07:58:42 +0200 (CEST) Received: from smtp3-g19.free.fr (smtp3-g19.free.fr [212.27.42.29]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id l355wfPt002559 for ; Thu, 5 Apr 2007 07:58:41 +0200 Received: from [192.168.0.1] (rke75-3-82-229-183-156.fbx.proxad.net [82.229.183.156]) by smtp3-g19.free.fr (Postfix) with ESMTP id 646345DF9B for ; Thu, 5 Apr 2007 07:58:41 +0200 (CEST) Message-ID: <46149010.30505@inria.fr> Date: Thu, 05 Apr 2007 07:58:40 +0200 From: Alain Frisch User-Agent: Icedove 1.5.0.8 (X11/20061116) MIME-Version: 1.0 To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Generators/iterators and lazy evaluation? References: <200704050053.41426.jon@ffconsultancy.com> In-Reply-To: <200704050053.41426.jon@ffconsultancy.com> X-Enigmail-Version: 0.94.0.0 Content-Type: text/plain; charset=ISO-8859-15 Content-Transfer-Encoding: 7bit X-Miltered: at concorde with ID 46149011.001 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; frisch:01 frisch:01 ocaml:01 ocaml:01 python's:01 parser:01 stack:01 callbacks:01 parser:01 parsing:01 iterators:01 wrote:01 wrote:01 caml-list:01 alain:01 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