[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

DELAYED? predicate




...sorry, the body of my previous message was delayed. It follows:

PROPOSAL FOR A NEW PREDICATE:
----------------------------
  (DELAYED? <object>) ==> #T iff <object> is an unforced PROMISE
                             created via the DELAY procedure.
			  #F otherwise
RATIONALE:
---------
  Insofar as it is possible to create a PROMISE (using DELAY), it
is desireable to detect if an object is in fact an unforced PROMISE.
To point, I find it occasionally desirable to check if a particular
PROMISE has ever been FORCEd soas to glean some information about the
dynamic behavior of a piece of code (such as, ``has this node in a tree
ever been visited (FORCEd)?''). To my knowledge, PROMISEs are the only
instance of a data-type in Scheme that may be created but not detected.
This makes them somewhat less than first-class in my mind.

SAMPLE IMPLEMENTATION: -- based on RRRS p.27
---------------------

     (delay <expression>) --> (make-promise (lambda () <expression>))

     (define (make-promise
	(lambda (proc)
	   (let ((already-run? #F) (result #F))
	      (lambda (message)		                        ; Add a message parameter
		(case message		                        ; Dispatch on message
     		   ((:FORCE) (cond ((not already-run?)          ; Old code for FORCE
				    (set! result (proc))
				    (set! already-run? #T) ))
			     result)
		   ((:DELAYED?) (not already-run?)) ))))))       ; Expose the toggle

     (define (force
	(lambda (object) (object ':FORCE)) ))

     (define (delayed?
	(lambda (object) (object ':DELAYED?)) ))


CONSIDERATIONS:
--------------
  Noting that some implementations may chose to make PROMISEs operationally
indistinguishable from their FORCEd value (e.g. implicit forcing), DELAYED?
may have to be a special form soas NOT to implicitly FORCE its argument.

  Finally, I was very careful NOT to propose a predicate PROMISE?, noting
that some implementations may opt to make FORCE actually alter its argument
soas to henceforth be replaced (in the store) by the computed value. I
was likewise careful not to request (FORCED? <object>) which in some
implementations would NOT neccessarily be the logical inverse of
DELAYED? (e.g. may always return #F in implementations that modify the
store as noted above).
                                                            ~Ziggy