Subject: Scheme promises [was: Re: "Programming is FUN again"...]
From: rpw3@rigden.engr.sgi.com (Rob Warnock)
Date: 1998/03/13
Newsgroups: comp.lang.scheme,comp.lang.lisp
Message-ID: <6ea4k6$2emdp@fido.asd.sgi.com>

<wanker@exploited.barmy.army> wrote:
+---------------
| >simulate lazy evaluation like you do in Scheme - with promises.)
| 
| Are promises a completely accurate simulation of lazy 
| evaluation ala Haskell?  I mean in Haskell you can
| use things like infinite sized lists without a problem
| because the system will only access as much of the
| list as is needed.  Do promises do that as well?
+---------------

Well, in some ways they're even lazier, since you *must*
"force" a promise to get it to evaluate itself. [To be more
precise, the standard doesn't *require* auto-forcing, but
it does *allow* implicit forcing by primitives. But few
implementations do that.]

But in general, yes, Scheme promises allow straigtforward construction
of virtually-infinite objects of which only the forced subset is manifest
(e.g., a list of the positive integers, or a list of primes).

However, note that Scheme requires that a forced value be memoized
(cached) so that if it's forced again it's not recomputed. Thus Scheme's
promises may differ from forms of lazy evaluation which allow unrestricted
side effects, e.g.:

	> (define foo        
	  (let ((x 0))
	    (delay (begin (set! x (1+ x)) x))))
	> foo
	#<promise>
	> (force foo)
	1
	> (force foo)
	1

Contrast this with a closure with local state ("forced" by calling it):

	> (define bar
	    (let ((x 0))
	      (lambda () (set! x (1+ x)) x)))
	> (bar)
	1
	> (bar)
	2


-Rob

p.s. IMHO, Scheme promises are a gross hack which would have been totally
unecessary if Scheme had had even a simplistic macro facility required
in the base language [before R5RS]. The only thing "delay" *really* gives
you is a little syntactic sugar for writing an unevaluated expression.
R5RS recogizes this by downgrading "delay" & "force" from "syntax" &
"procedure" to "library syntax" & "library procedure" (meaning they can
be readily expressed in terms of "more primitive" required features).

-----
Rob Warnock, 7L-551		rpw3@sgi.com   http://reality.sgi.com/rpw3/
Silicon Graphics, Inc.		Phone: 650-933-1673 [New area code!]
2011 N. Shoreline Blvd.		FAX: 650-933-4392
Mountain View, CA  94043	PP-ASEL-IA