unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Mark H Weaver <mhw@netris.org>
To: Amirouche Boubekki <amirouche@hypermove.net>
Cc: Guile User <guile-user@gnu.org>
Subject: Re: optimizing lazy sequences: srfi-41 vs delayed continuation with values + promise vs delayed continuation with cons + lambda
Date: Fri, 02 Mar 2018 18:22:00 -0500	[thread overview]
Message-ID: <87fu5i5ahz.fsf@netris.org> (raw)
In-Reply-To: <f560f163030595b76e32c74066e8c017@hypermove.net> (Amirouche Boubekki's message of "Sun, 25 Feb 2018 15:16:48 +0100")

Hi,

Amirouche Boubekki <amirouche@hypermove.net> writes:
[...]
> Now, I got another idea what if I replace the 'cons' returned
> by the traversi procedures by 'values' and replace the
> lambda with 'delay' with some use of 'force' at the correct
> places. It will result in the following procedures:
>
> (define-public (list->stream lst)
>   (let loop ((lst lst))
>     (if (null? lst)
>         (delay (values #f #f))
>         (delay (values (car lst) (loop (cdr lst)))))))
>
> (define-public (stream->list stream)
>   (let loop ((stream stream)
>              (out '()))
>     (call-with-values (lambda () (force stream))
>       (lambda (value next)
>         (if next
>             (loop next (cons value out))
>             (reverse! out))))))
>
> (define-public (stream-map proc stream)
>   (let loop ((stream stream))
>     (call-with-values (lambda () (force stream))
>       (lambda (value next)
>         (if next
>             (delay (values (proc value) (loop next)))
>             (delay (values #f #f)))))))

This code assumes that promises can store multiple values.  Although
Guile's legacy core promises *accidentally* support multiple values
today, there's no guarantee that they will continue to do so in the
future.  None of the standards allow this, and Guile's manual states in
the documentation for 'delay' that "The effect of <expression> returning
multiple values is unspecified."

Supporting multiple values in promises makes them more complex, and
inevitably less efficient.

SRFI-45 promises are simpler than Guile's legacy core promises in two
ways: (1) they do not include built-in thread synchronization, and (2)
they do not support multiple values.

I would recommend using SRFI-45 promises.  Although they are implemented
in Scheme, last I checked I found that they were about as fast as
Guile's legacy core promises implemented in C, presumably because of the
built-in thread synchronization in our core promises.

In any case, I would strongly recommend against writing code that
assumes that Guile's promises can hold multiple values.

     Regards,
       Mark



      parent reply	other threads:[~2018-03-02 23:22 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-25 14:16 optimizing lazy sequences: srfi-41 vs delayed continuation with values + promise vs delayed continuation with cons + lambda Amirouche Boubekki
2018-02-25 14:30 ` Amirouche Boubekki
2018-02-25 22:39   ` Amirouche Boubekki
2018-02-25 23:31     ` Amirouche Boubekki
2018-03-02 23:22 ` Mark H Weaver [this message]

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=87fu5i5ahz.fsf@netris.org \
    --to=mhw@netris.org \
    --cc=amirouche@hypermove.net \
    --cc=guile-user@gnu.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).