[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Guile-commits] GNU Guile branch, stable-2.0, updated. v2.0.7-209-g1d64b
From: |
Mark H Weaver |
Subject: |
[Guile-commits] GNU Guile branch, stable-2.0, updated. v2.0.7-209-g1d64b4e |
Date: |
Tue, 19 Mar 2013 14:30:27 +0000 |
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "GNU Guile".
http://git.savannah.gnu.org/cgit/guile.git/commit/?id=1d64b4edb9da4011ad06c0fab1c6225ec20b0876
The branch, stable-2.0 has been updated
via 1d64b4edb9da4011ad06c0fab1c6225ec20b0876 (commit)
from 8150dfa1f2f84d151ced5f723dc69ce0cae1cd32 (commit)
Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.
- Log -----------------------------------------------------------------
commit 1d64b4edb9da4011ad06c0fab1c6225ec20b0876
Author: Mark H Weaver <address@hidden>
Date: Mon Mar 18 20:01:12 2013 -0400
SRFI-45: Support multiple values; add promise? predicate.
* module/srfi/srfi-45.scm (eager): Accept any number of arguments.
Store the list of arguments in the value record. Previously, only one
argument was accepted, and that value was stored in the value record.
(delay): Support expressions that return any number of arguments.
(force): Return the list of values stored in the value record.
(promise?): Export.
* doc/ref/srfi-modules.texi (SRFI-45): Update docs. Remove typing
for simplicity in discussing multiple values.
* test-suite/tests/srfi-45.test: Add tests. Add FSF copyright for 2010
and 2013. Add missing year to André van Tonder's copyright notice.
-----------------------------------------------------------------------
Summary of changes:
doc/ref/srfi-modules.texi | 57 +++++++++++++++++++++++++----------------
module/srfi/srfi-45.scm | 21 ++++++++------
test-suite/tests/srfi-45.test | 43 ++++++++++++++++++++++++++++++-
3 files changed, 89 insertions(+), 32 deletions(-)
diff --git a/doc/ref/srfi-modules.texi b/doc/ref/srfi-modules.texi
index af1afc0..e60dbb4 100644
--- a/doc/ref/srfi-modules.texi
+++ b/doc/ref/srfi-modules.texi
@@ -3833,45 +3833,58 @@ words, no program that uses the R5RS definitions of
delay and force will
break if those definition are replaced by the SRFI-45 definitions of
delay and force.
+Guile compatibly extends SRFI-45 to support multiple values. It also
+adds @code{promise?} to the list of exports.
+
address@hidden {Scheme Procedure} promise? obj
+Return true if @var{obj} is an SRFI-45 promise, otherwise return false.
address@hidden deffn
+
@deffn {Scheme Syntax} delay expression
-Takes an expression of arbitrary type @var{a} and returns a promise of
-type @code{(Promise @var{a})} which at some point in the future may be
-asked (by the @code{force} procedure) to evaluate the expression and
-deliver the resulting value.
+Takes an expression and returns a promise which at some point in the
+future may be asked (by the @code{force} procedure) to evaluate the
+expression and deliver the resulting value(s).
@end deffn
@deffn {Scheme Syntax} lazy expression
-Takes an expression of type @code{(Promise @var{a})} and returns a
-promise of type @code{(Promise @var{a})} which at some point in the
-future may be asked (by the @code{force} procedure) to evaluate the
-expression and deliver the resulting promise.
+Takes an expression (which must evaluate to a promise) and returns a
+promise which at some point in the future may be asked (by the
address@hidden procedure) to evaluate the expression and deliver the
+resulting promise.
@end deffn
address@hidden {Scheme Procedure} force expression
-Takes an argument of type @code{(Promise @var{a})} and returns a value
-of type @var{a} as follows: If a value of type @var{a} has been computed
-for the promise, this value is returned. Otherwise, the promise is
-first evaluated, then overwritten by the obtained promise or value, and
-then force is again applied (iteratively) to the promise.
address@hidden {Scheme Procedure} force promise
+Takes a promise and returns the associated value(s) as follows: If
+value(s) have been computed for the promise, these value(s) are
+returned. Otherwise, the promise is first evaluated, then overwritten
+by the obtained promise or value(s), and then force is again applied
+(iteratively) to the promise.
@end deffn
address@hidden {Scheme Procedure} eager expression
-Takes an argument of type @var{a} and returns a value of type
address@hidden(Promise @var{a})}. As opposed to @code{delay}, the argument is
-evaluated eagerly. Semantically, writing @code{(eager expression)} is
-equivalent to writing
address@hidden {Scheme Procedure} eager obj ...
+Takes any number of argument(s) and returns a promise. As opposed to
address@hidden, the argument(s) are evaluated eagerly. Semantically,
+writing @code{(eager expression)} is equivalent to writing
@lisp
(let ((value expression)) (delay value)).
@end lisp
However, the former is more efficient since it does not require
-unnecessary creation and evaluation of thunks. We also have the
-equivalence
+unnecessary creation and evaluation of thunks. For expressions that
+return a single value, we also have the equivalence
@lisp
(delay expression) = (lazy (eager expression))
@end lisp
+
+More generally, the following equivalence holds:
+
address@hidden
+(delay expression) = (lazy (call-with-values
+ (lambda () expression)
+ eager))
address@hidden lisp
@end deffn
The following reduction rules may be helpful for reasoning about these
@@ -3881,7 +3894,7 @@ usage semantics specified above:
@lisp
(force (delay expression)) -> expression
(force (lazy expression)) -> (force expression)
-(force (eager value)) -> value
+(force (eager obj ...)) -> (values obj ...)
@end lisp
@subsubheading Correct usage
diff --git a/module/srfi/srfi-45.scm b/module/srfi/srfi-45.scm
index 29b0393..47e3ba6 100644
--- a/module/srfi/srfi-45.scm
+++ b/module/srfi/srfi-45.scm
@@ -1,6 +1,6 @@
;;; srfi-45.scm -- Primitives for Expressing Iterative Lazy Algorithms
-;; Copyright (C) 2010, 2011 Free Software Foundation, Inc.
+;; Copyright (C) 2010, 2011, 2013 Free Software Foundation, Inc.
;; Copyright (C) 2003 André van Tonder. All Rights Reserved.
;; Permission is hereby granted, free of charge, to any person
@@ -25,8 +25,8 @@
;;; Commentary:
-;; This is the code of the reference implementation of SRFI-45, slightly
-;; modified to use SRFI-9.
+;; This is the code of the reference implementation of SRFI-45,
+;; modified to use SRFI-9 and to support multiple values.
;; This module is documented in the Guile Reference Manual.
@@ -36,8 +36,9 @@
#:export (delay
lazy
force
- eager)
- #:replace (delay force)
+ eager
+ promise?)
+ #:replace (delay force promise?)
#:use-module (srfi srfi-9))
(define-record-type promise (make-promise val) promise?
@@ -50,16 +51,18 @@
(define-syntax-rule (lazy exp)
(make-promise (make-value 'lazy (lambda () exp))))
-(define (eager x)
- (make-promise (make-value 'eager x)))
+(define (eager . xs)
+ (make-promise (make-value 'eager xs)))
(define-syntax-rule (delay exp)
- (lazy (eager exp)))
+ (lazy (call-with-values
+ (lambda () exp)
+ eager)))
(define (force promise)
(let ((content (promise-val promise)))
(case (value-tag content)
- ((eager) (value-proc content))
+ ((eager) (apply values (value-proc content)))
((lazy) (let* ((promise* ((value-proc content)))
(content (promise-val promise))) ; *
(if (not (eqv? (value-tag content) 'eager)) ; *
diff --git a/test-suite/tests/srfi-45.test b/test-suite/tests/srfi-45.test
index 573eea0..cb3f790 100644
--- a/test-suite/tests/srfi-45.test
+++ b/test-suite/tests/srfi-45.test
@@ -1,6 +1,7 @@
;;; -*- mode: scheme; coding: utf-8; -*-
-;; Copyright André van Tonder. All Rights Reserved.
+;; Copyright (C) 2010, 2013 Free Software Foundation, Inc.
+;; Copyright (C) 2003 André van Tonder. All Rights Reserved.
;;
;; Permission is hereby granted, free of charge, to any person
;; obtaining a copy of this software and associated documentation
@@ -258,3 +259,43 @@
;; Commented out since it takes too long
#;
(test-equal 300000000 (force (times3 100000000))) ;==> bounded space
+
+
+;======================================================================
+; Test promise? predicate (non-standard Guile extension)
+
+(pass-if "promise? predicate"
+ (promise? (delay 1)))
+
+;======================================================================
+; Test memoization of multiple values (non-standard Guile extension)
+
+(with-test-prefix "Multiple values (non-standard)"
+
+ (let ((promise (delay (values 1 2 3))))
+ (pass-if-equal "Multiple values delay"
+ '(1 2 3)
+ (call-with-values
+ (lambda () (force promise))
+ list)))
+
+ (let ((promise (eager 1 2 3)))
+ (pass-if-equal "Multiple values eager"
+ '(1 2 3)
+ (call-with-values
+ (lambda () (force promise))
+ list)))
+
+ (let ((promise (delay (values))))
+ (pass-if-equal "Zero values delay"
+ '()
+ (call-with-values
+ (lambda () (force promise))
+ list)))
+
+ (let ((promise (eager)))
+ (pass-if-equal "Zero values eager"
+ '()
+ (call-with-values
+ (lambda () (force promise))
+ list))))
hooks/post-receive
--
GNU Guile
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Guile-commits] GNU Guile branch, stable-2.0, updated. v2.0.7-209-g1d64b4e,
Mark H Weaver <=