unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* Switching to a SRFI-45 compliant force/delay.
@ 2006-09-10 23:24 Rob Browning
  2006-09-11  0:51 ` Kevin Ryde
  0 siblings, 1 reply; 3+ messages in thread
From: Rob Browning @ 2006-09-10 23:24 UTC (permalink / 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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Switching to a SRFI-45 compliant force/delay.
  2006-09-10 23:24 Switching to a SRFI-45 compliant force/delay Rob Browning
@ 2006-09-11  0:51 ` Kevin Ryde
  2006-09-11  1:33   ` Rob Browning
  0 siblings, 1 reply; 3+ messages in thread
From: Kevin Ryde @ 2006-09-11  0:51 UTC (permalink / raw)
  Cc: guile-devel

Rob Browning <rlb@defaultvalue.org> writes:
>
> 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.

I remember I thought it could go in C in the core nicely enough, or at
least just extending `force' in the core and leaving the actual
`eager' to the srfi module perhaps.  I don't remember the details
though, except it might be nice to lose the mutexes at the same time
:), for speed and size.

http://lists.gnu.org/archive/html/guile-devel/2005-12/msg00050.html


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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Switching to a SRFI-45 compliant force/delay.
  2006-09-11  0:51 ` Kevin Ryde
@ 2006-09-11  1:33   ` Rob Browning
  0 siblings, 0 replies; 3+ messages in thread
From: Rob Browning @ 2006-09-11  1:33 UTC (permalink / raw)


Kevin Ryde <user42@zip.com.au> writes:

> I remember I thought it could go in C in the core nicely enough, or at
> least just extending `force' in the core and leaving the actual
> `eager' to the srfi module perhaps.  I don't remember the details
> though, except it might be nice to lose the mutexes at the same time
> :), for speed and size.
>
> http://lists.gnu.org/archive/html/guile-devel/2005-12/msg00050.html

Wow, I had forgotten that discussion.

In any case, I'm not opposed to putting it in C, and I'm also probably
not opposed to dropping the mutexes and just documenting that if you
want sensible results, you need to add a mutex when appropriate.

However, we'd have to be very careful that the C code has identical
semantics to the srfi-45 code, which is somewhat tricky.  It may well
be that the code you posted back then will work fine, but I haven't
looked it it carefully enough to be able to be sure one way or the
other.

Of course I suppose we might also be able to just switch to the Scheme
code (plus or minus the mutexes) and plan to migrate to C later.

I'd also like to have all the srfi-45 tests automated, though many of
them are of the format '"this" should execute in bounded space'.  I
suppose we could track that via (gc-stats).

-- 
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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2006-09-11  1:33 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-09-10 23:24 Switching to a SRFI-45 compliant force/delay Rob Browning
2006-09-11  0:51 ` Kevin Ryde
2006-09-11  1:33   ` Rob Browning

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).