Why isn't promise a data type in Scheme?
Asked Answered
M

3

6

The object returned by delay in Scheme is "a promise", but promises are not considered to be a type (so there is no promise? procedure, and it's not listed as a type in R5RS or R6RS).

Is there a strong reson why this is so? It would seem quite natural to me to do something like (if (promise? x) (force x) x), for example. (And I see that some implementations will let me force non-promises, and others will not). Also, if I can store something in a variale and pass it around, I feel like it should have a type.

Magnusson answered 23/9, 2010 at 10:37 Comment(0)
L
4

There can't be that strong a reason, since MIT/GNU scheme, defines a promise? function.

Loaf answered 23/9, 2010 at 10:46 Comment(0)
U
4

I think it allows for a more optimized implementation of delay/force. The fact that the forced value can be memoized (so that a promise is really forced only once and the resulting value is returned on subsequent force calls) blurs the distinction between a promise and its resulting value. If you have promise? you cannot substitute a forced promise by its value everywhere it is needed. Therefore, depending on the implementation, a promise can be indistinguishable from any other Scheme value.

Upheaval answered 23/9, 2010 at 13:40 Comment(2)
That makes sense! Thanks for answering. (I still think there could be a promise? predicate that would answer #t whenever you can use force on the object -- even if it always returns true.)Magnusson
Wait, what does memoization have to do with the types (force x) and x? Force will always return a value, and x is always a promise, unless I am misunderstanding this.Binns
E
0

As noted, promise? isn't in R5RS, or R6RS (Which relegated delay and force to a R5RS compatibility library)... but R7RS brought them back in the (scheme lazy) library, and adds, along with a few other routines, promise?, with the following advisory:

Note that promises are not necessarily disjoint from other Scheme types such as procedures.

which leaves lots of room for different implementation strategies including the classic let-over-lambda style where a promise is a function.

Eulogist answered 2/10, 2024 at 0:0 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.