unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* Record type printers for SRFI 45 promises and SRFI 41 streams
@ 2013-04-07 16:49 Chris K. Jester-Young
  2013-04-07 17:01 ` Mark H Weaver
  2013-04-07 23:13 ` Daniel Hartwig
  0 siblings, 2 replies; 5+ messages in thread
From: Chris K. Jester-Young @ 2013-04-07 16:49 UTC (permalink / raw)
  To: guile-devel

[-- Attachment #1: Type: text/plain, Size: 581 bytes --]

Hi all,

I've attached record type printers for SRFI 45 promises and SRFI 41
streams. I've tried to make promise-visit more self-documenting with
the use of keyword arguments; let me know if you think that's an
improvement!

Also as discussed with Mark H Weaver, I've currently implemented the
format for promises as #<promise => ...> for unevaluated promises, and
#<promise = ...> for evaluated ones. Hopefully this is is easy to read
and will clearly distinguish between the two types, and still look
different from core promises too.

Comments are most welcome!

Thanks,
Chris.

[-- Attachment #2: 0001-Add-record-type-printer-for-srfi-45.patch --]
[-- Type: message/rfc822, Size: 1514 bytes --]

From: Chris K. Jester-Young <cky944@gmail.com>
Subject: [PATCH 1/2] Add record type printer for srfi-45.
Date: Sun, 7 Apr 2013 12:43:17 -0400

* module/srfi/srfi-45.scm: Add record type printer for promises.
  (promise-visit): New helper for visiting lazy promises.
---
 module/srfi/srfi-45.scm |   17 ++++++++++++++++-
 1 files changed, 16 insertions(+), 1 deletions(-)

diff --git a/module/srfi/srfi-45.scm b/module/srfi/srfi-45.scm
index 5194770..6f7ba7e 100644
--- a/module/srfi/srfi-45.scm
+++ b/module/srfi/srfi-45.scm
@@ -39,7 +39,8 @@
              eager
              promise?)
   #:replace (delay force promise?)
-  #:use-module (srfi srfi-9))
+  #:use-module (srfi srfi-9)
+  #:use-module (srfi srfi-9 gnu))
 
 (cond-expand-provide (current-module) '(srfi-45))
 
@@ -76,3 +77,17 @@
 ;; (*) These two lines re-fetch and check the original promise in case
 ;;     the first line of the let* caused it to be forced.  For an example
 ;;     where this happens, see reentrancy test 3 below.
+
+(define* (promise-visit promise #:key on-eager on-lazy)
+  (define content (promise-val promise))
+  (case (value-tag content)
+    ((eager) (on-eager (value-proc content)))
+    ((lazy)  (on-lazy (value-proc content)))))
+
+(set-record-type-printer! promise
+  (lambda (promise port)
+    (promise-visit promise
+      #:on-eager (lambda (value)
+                   (format port "#<promise = ~s>" value))
+      #:on-lazy  (lambda (proc)
+                   (format port "#<promise => ~s>" proc)))))
-- 
1.7.2.5


[-- Attachment #3: 0002-Add-record-type-printer-for-srfi-41.patch --]
[-- Type: message/rfc822, Size: 2075 bytes --]

From: Chris K. Jester-Young <cky944@gmail.com>
Subject: [PATCH 2/2] Add record type printer for srfi-41.
Date: Sun, 7 Apr 2013 12:44:18 -0400

* module/srfi/srfi-41.scm: Add record type printer for streams.
  (stream-promise-visit): New helper for visiting stream promises.
---
 module/srfi/srfi-41.scm |   23 +++++++++++++++++++++++
 1 files changed, 23 insertions(+), 0 deletions(-)

diff --git a/module/srfi/srfi-41.scm b/module/srfi/srfi-41.scm
index 6f73ce3..3589b35 100644
--- a/module/srfi/srfi-41.scm
+++ b/module/srfi/srfi-41.scm
@@ -27,6 +27,7 @@
   #:use-module (srfi srfi-1)
   #:use-module (srfi srfi-8)
   #:use-module (srfi srfi-9)
+  #:use-module (srfi srfi-9 gnu)
   #:use-module (srfi srfi-26)
   #:use-module (ice-9 match)
   #:export (stream-null stream-cons stream? stream-null? stream-pair?
@@ -180,6 +181,28 @@
 (define-syntax-rule (stream-lambda formals body0 body1 ...)
   (lambda formals (stream-lazy (begin body0 body1 ...))))
 
+(define* (stream-promise-visit promise #:key on-eager on-lazy)
+  (define content (stream-promise-val promise))
+  (case (stream-value-tag content)
+    ((eager) (on-eager (stream-value-proc content)))
+    ((lazy)  (on-lazy (stream-value-proc content)))))
+
+(set-record-type-printer! stream-promise
+  (lambda (strm port)
+    (display "#<stream" port)
+    (let loop ((strm strm))
+      (stream-promise-visit strm
+        #:on-eager (lambda (pare)
+                     (cond ((eq? pare %stream-null)
+                            (write-char #\> port))
+                           (else
+                            (write-char #\space port)
+                            (stream-promise-visit (stream-kar pare)
+                              #:on-eager (cut write <> port)
+                              #:on-lazy  (lambda (_) (write-char #\? port)))
+                            (loop (stream-kdr pare)))))
+        #:on-lazy (lambda (_) (display " ...>" port))))))
+
 ;;; Derived stream functions and macros: (streams derived)
 
 (define-syntax-rule (define-stream (name . formal) body0 body1 ...)
-- 
1.7.2.5


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

* Re: Record type printers for SRFI 45 promises and SRFI 41 streams
  2013-04-07 16:49 Record type printers for SRFI 45 promises and SRFI 41 streams Chris K. Jester-Young
@ 2013-04-07 17:01 ` Mark H Weaver
  2013-04-07 23:13 ` Daniel Hartwig
  1 sibling, 0 replies; 5+ messages in thread
From: Mark H Weaver @ 2013-04-07 17:01 UTC (permalink / raw)
  To: guile-devel

Hi Chris,

Your patches look good to me!

   Thanks,
     Mark



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

* Re: Record type printers for SRFI 45 promises and SRFI 41 streams
  2013-04-07 16:49 Record type printers for SRFI 45 promises and SRFI 41 streams Chris K. Jester-Young
  2013-04-07 17:01 ` Mark H Weaver
@ 2013-04-07 23:13 ` Daniel Hartwig
  2013-04-07 23:19   ` Daniel Hartwig
  2013-04-08 19:31   ` Mark H Weaver
  1 sibling, 2 replies; 5+ messages in thread
From: Daniel Hartwig @ 2013-04-07 23:13 UTC (permalink / raw)
  To: guile-devel

On 8 April 2013 00:49, Chris K. Jester-Young <cky944@gmail.com> wrote:
> Hi all,
>
> I've attached record type printers for SRFI 45 promises and SRFI 41
> streams. I've tried to make promise-visit more self-documenting with
> the use of keyword arguments; let me know if you think that's an
> improvement!
>
> Also as discussed with Mark H Weaver, I've currently implemented the
> format for promises as #<promise => ...> for unevaluated promises, and
> #<promise = ...> for evaluated ones. Hopefully this is is easy to read
> and will clearly distinguish between the two types, and still look
> different from core promises too.

I dont see much value in distinguishing between eager and lazy,
particularly as this leads to an unusual display format.  What
difference does it make how the object was constructed?

Also, this is not avoiding "#<procedure" as suggested?  A simple
"#<promise OBJECT-ADDRESS>" is succinct and sufficient.



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

* Re: Record type printers for SRFI 45 promises and SRFI 41 streams
  2013-04-07 23:13 ` Daniel Hartwig
@ 2013-04-07 23:19   ` Daniel Hartwig
  2013-04-08 19:31   ` Mark H Weaver
  1 sibling, 0 replies; 5+ messages in thread
From: Daniel Hartwig @ 2013-04-07 23:19 UTC (permalink / raw)
  To: guile-devel

On 8 April 2013 07:13, Daniel Hartwig <mandyke@gmail.com> wrote:
> On 8 April 2013 00:49, Chris K. Jester-Young <cky944@gmail.com> wrote:
>> Hi all,
>>
>> I've attached record type printers for SRFI 45 promises and SRFI 41
>> streams. I've tried to make promise-visit more self-documenting with
>> the use of keyword arguments; let me know if you think that's an
>> improvement!
>>
>> Also as discussed with Mark H Weaver, I've currently implemented the
>> format for promises as #<promise => ...> for unevaluated promises, and
>> #<promise = ...> for evaluated ones. Hopefully this is is easy to read
>> and will clearly distinguish between the two types, and still look
>> different from core promises too.

To distinguish from core promises a more explicit tag is preferable,
like srfi-69 uses:

#<srfi-45:promise OBJ-ADDR>

Regards



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

* Re: Record type printers for SRFI 45 promises and SRFI 41 streams
  2013-04-07 23:13 ` Daniel Hartwig
  2013-04-07 23:19   ` Daniel Hartwig
@ 2013-04-08 19:31   ` Mark H Weaver
  1 sibling, 0 replies; 5+ messages in thread
From: Mark H Weaver @ 2013-04-08 19:31 UTC (permalink / raw)
  To: Daniel Hartwig; +Cc: guile-devel

Hi Daniel,

Daniel Hartwig <mandyke@gmail.com> writes:
> I dont see much value in distinguishing between eager and lazy,
> particularly as this leads to an unusual display format.  What
> difference does it make how the object was constructed?

It doesn't, and I agree that it's confusing.  In the SRFI-45
implementation, 'eager' means "computed", and 'lazy' means "not yet
computed".  These internal meanings are distinct from the 'lazy' and
'eager' exported to users.

> Also, this is not avoiding "#<procedure" as suggested?  A simple
> "#<promise OBJECT-ADDRESS>" is succinct and sufficient.

Sorry for flip-flopping on this, but I realized that there's an
advantage to printing the full procedure, namely that it includes the
source location of the associated (delay ...) expression.

> To distinguish from core promises a more explicit tag is preferable,
> like srfi-69 uses:
> 
> #<srfi-45:promise OBJ-ADDR>

We later discussed this on IRC, but for those who weren't present:

The problem with this idea is that there's likely to be another new SRFI
that's almost identical to SRFI-45 but with 'eager' as a macro instead
of a procedure, so that it can accept a single expression that yields
multiple values.

We'll want to use the same underlying type to support both SRFIs, at
which point this idea of printing the SRFI number in the tag breaks
down.

      Regards,
        Mark



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

end of thread, other threads:[~2013-04-08 19:31 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-04-07 16:49 Record type printers for SRFI 45 promises and SRFI 41 streams Chris K. Jester-Young
2013-04-07 17:01 ` Mark H Weaver
2013-04-07 23:13 ` Daniel Hartwig
2013-04-07 23:19   ` Daniel Hartwig
2013-04-08 19:31   ` Mark H Weaver

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