unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Rob Browning <rlb@defaultvalue.org>
Subject: Switching to a SRFI-45 compliant force/delay.
Date: Sun, 10 Sep 2006 16:24:56 -0700	[thread overview]
Message-ID: <87ejujpanr.fsf@raven.defaultvalue.org> (raw)


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
shouldn't.

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,,,r5rs.info,The Revised^5 Report on Scheme})."
;;  (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))
      ((lazy)
       (let* ((promise* ((promise-value p))))
         (if (not (eq? 'eager (promise-kind p)))
             (begin
               (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 @defaultvalue.org and @debian.org; previously @cs.utexas.edu
GPG starting 2002-11-03 = 14DD 432F AE39 534D B592  F9A0 25C8 D377 8C7E 73A4


_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-devel


             reply	other threads:[~2006-09-10 23:24 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-09-10 23:24 Rob Browning [this message]
2006-09-11  0:51 ` Switching to a SRFI-45 compliant force/delay Kevin Ryde
2006-09-11  1:33   ` Rob Browning

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87ejujpanr.fsf@raven.defaultvalue.org \
    --to=rlb@defaultvalue.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).