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=HTML_MESSAGE autolearn=disabled version=3.1.3 Received: from discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id 638A1BC0A for ; Wed, 4 Apr 2007 18:33:04 +0200 (CEST) Received: from mail.cs.rice.edu (mail.cs.rice.edu [128.42.1.31]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id l34GX3tc012340 for ; Wed, 4 Apr 2007 18:33:03 +0200 Received: from mail.cs.rice.edu (localhost.localdomain [127.0.0.1]) by mail.cs.rice.edu (Postfix) with ESMTP id C00472C2ABD for ; Wed, 4 Apr 2007 11:33:02 -0500 (CDT) X-Virus-Scanned: by amavis-2.4.0 at mail.cs.rice.edu Received: from mail.cs.rice.edu ([127.0.0.1]) by mail.cs.rice.edu (mail.cs.rice.edu [127.0.0.1]) (amavisd-new, port 10024) with LMTP id CGib95S712yU for ; Wed, 4 Apr 2007 11:33:02 -0500 (CDT) Received: from [10.251.249.237] (register.rice.edu [128.42.95.253]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client certificate requested) by mail.cs.rice.edu (Postfix) with ESMTP id 7430D2C2AAE for ; Wed, 4 Apr 2007 11:33:00 -0500 (CDT) Mime-Version: 1.0 (Apple Message framework v750) To: caml-list@yquem.inria.fr Message-Id: Content-Type: multipart/alternative; boundary=Apple-Mail-16--171296971 From: Raj B Subject: Generators/iterators and lazy evaluation? Date: Wed, 4 Apr 2007 11:33:18 -0500 X-Mailer: Apple Mail (2.750) X-j-chkmail-Score: MSGID : 4613D33F.000 on discorde : j-chkmail score : XXX : 5/20 1 0.000 -> 3 X-Miltered: at discorde with ID 4613D33F.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; ocaml:01 compiler:01 arbitrarily:01 def:01 ocaml:01 compiler:01 arbitrarily:01 def:01 iterators:01 functions:01 functions:01 lazy:02 lazy:02 python:02 python:02 --Apple-Mail-16--171296971 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Hi all I've been using OCaml for a year or so building an interpreter/ compiler for the Python programming language as a research project. It's worked pretty well for most part, but I'm trying to implement some of the features of Python involving 'lazy' features, such as generator functions and list comprehensions. A generator in Python can be thought of as an arbitrarily generated 'lazy list'. As an example the following is a generator capable of generating powers of 2 upto a max value. def pow2(max): start = 0 while (start .lt. max): yield 2^start start += 1 The 'yield' statement is the point where the function returns the next value and suspends itself until the next time it is 'forced'. At that time it resumes execution where it left off. OCaml makes this particularly hard to implement this due to lack of 'control flow' features, including even a call/cc. The only way I can see right now is breaking this code down into little functions, saving the current execution environment during the suspend and keeping track of the right function to call the next time. I've been thinking about whether I can express this in some elegant way in some lazy construct in OCaml, since this is basically a form of 'lazy' evaluation. However, I come from the C world and am not quite used to 'lazy' thinking :) Any ideas? Thanks in advance Raj -- The Python charmer _ .-=-. .-==-. { } __ .' O o '. / -<' ) { } .' O'. / o .-. O \ / .--v` { } / .-. o\ /O / \ o\ /O / \ `-` / \ O`-'o / \ O`-`o / jgs `-.-` '.____.' `.____.' --Apple-Mail-16--171296971 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=ISO-8859-1
Hi = all

I've been using OCaml for a year or so building an = interpreter/compiler for the Python programming language as a research = project.

It's worked pretty well for most part, but I'm = trying to implement some of the features of Python involving 'lazy' = features, such as generator functions and list comprehensions.

A generator = in Python can be thought of as an arbitrarily generated 'lazy list'. As = an example
the following is a generator = capable of generating powers of 2 upto a max value.

def = pow2(max):
=A0 =A0start =3D 0
=A0 =A0while (start .lt. max):
=A0 =A0=A0 =A0yield 2^start
=A0 =A0=A0 =A0start +=3D 1

The 'yield' = statement is the point where the function returns the next value and = suspends itself until the next time it is 'forced'. At that time it = resumes execution where it left off.

OCaml makes this particularly hard to implement this = due to lack of 'control flow' features, including even a call/cc. The = only way I can see right now is breaking this code down into little = functions, saving the current execution environment during the suspend = and keeping track of the right function to call the next time.

I've been = thinking about whether I can express this in some elegant way in some = lazy construct in OCaml, since this is basically a form of 'lazy' = evaluation. However, I come from the C world and am not quite used to = 'lazy' thinking :) Any ideas?

Thanks in = advance
Raj

The = Python charmer

=A0 =A0 =A0_ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = .-=3D-.=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = __=A0 =A0 =A0 =A0 .' O o '. =A0 =A0 =A0 /-<' = )
=A0 =A0 .' O'. / o .-. O \ = =A0 =A0 /=A0 / .-. o\ /O=A0 =A0 \o\ =A0 =A0 \ O`-'o/ =A0 =A0 =A0 O`-`o /
jgs=A0 =A0 =A0 '.____.'


=

= --Apple-Mail-16--171296971--