[Top][All Lists]

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

Switching to a SRFI-45 compliant force/delay.

From: Rob Browning
Subject: Switching to a SRFI-45 compliant force/delay.
Date: Sun, 10 Sep 2006 16:24:56 -0700
User-agent: Gnus/5.110006 (No Gnus v0.6) Emacs/21.4 (gnu/linux)

A while ago, I finished a SRFI-45 compliant force/delay/eager
implementation for Guile, the core of which is more or less a
translation of the SRFI reference code.

(I started this in part with an eye toward lazy process streams,
i.e. "find -printf ..." -> lazy-stream-of-lines)

The implementation works (it passes all of the tests they provide),
and I believe could completely replace Guile's existing eval.c
force/delay, but I wanted to know if anyone knew of a reason it

One notable difference is that this implementation is pure Scheme
code (I have the eval.c force/delay commented out here).

Below is what the current code looks like (just to give you a rough
idea).  Of course we would s/newforce/force/g and s/newdelay/delay/g
before actually incorporating it.

(define-module (ice-9 srfi-45)
  :use-module (ice-9 threads))

(export newforce)
(export-syntax newdelay)
(export eager)
(export-syntax lazy)

(define (print-promise promise port)
  (with-mutex (promise-mutex promise)
    (display "#<promise " port)
    (display (promise-kind promise))
    (display " (value ")
    (write (promise-value promise))
    (display ")>")))

(define promise-type
  (make-record-type "promise" '(mutex data) print-promise))

(define promise-data (record-accessor promise-type 'data))
(define set-promise-data! (record-modifier promise-type 'data))

(define promise-mutex (record-accessor promise-type 'mutex))

(define make-promise
  (let ((constructor (record-constructor promise-type '(mutex data))))
    (lambda (kind val)
      (constructor (make-recursive-mutex) (cons kind val)))))

(define (promise-kind p) (car (promise-data p)))
(define (set-promise-kind! p v) (set-car! (promise-data p) v))
(define (promise-value p) (cdr (promise-data p)))
(define (set-promise-value! p v) (set-cdr! (promise-data p) v))

;; need to finish this one...
;;(define promise?
;;  "Return true if @var{obj} is a promise, i.e. a delayed 
computation\n(@pxref{Delayed evaluation,,,,The Revised^5 Report on 
;;  (record-predicate promise-type))

(define-macro (newdelay exp)
  `(lazy (eager ,exp)))

(define (eager exp)
  (make-promise 'eager exp))

(define-macro (lazy exp)
  `((@@ (ice-9 srfi-45) make-promise) 'lazy (lambda () ,exp)))

(define (newforce promise)

  (define (force-aux p)
    (case (promise-kind p)
      ((eager) (promise-value promise))
       (let* ((promise* ((promise-value p))))
         (if (not (eq? 'eager (promise-kind p)))
               (set-promise-kind! p (promise-kind promise*))
               (set-promise-value! p (promise-value promise*))
               (set-promise-data! promise* (promise-data p))
         (force-aux p)))))

  (with-mutex (promise-mutex promise)
              (force-aux promise)))

Rob Browning
rlb and; previously
GPG starting 2002-11-03 = 14DD 432F AE39 534D B592  F9A0 25C8 D377 8C7E 73A4

reply via email to

[Prev in Thread] Current Thread [Next in Thread]