unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: amirouche@hyper.dev
To: guile-user gnu <guile-user@gnu.org>
Subject: prompt and generator
Date: Sun, 05 May 2019 18:48:52 +0200	[thread overview]
Message-ID: <ec0818b366d668a767d49a8b643390cf@hyper.dev> (raw)

I am trying to replace the use call/cc with prompts.

Here is the definition of 'make-coroutine-generator':

(define (make-coroutine-generator thunk)
   (define tag (make-prompt-tag))
   (define (run)
     (thunk (lambda (val) (abort-to-prompt tag val)))
     (eof-object))
   (lambda ()
     (call-with-prompt tag
                       run
                       (lambda (k ret)
                         (set! run k)
                         ret))))

Its unit tests pass correctly e.g.:

       (define g
         (make-coroutine-generator
          (lambda (yield) (let loop ((i 0))
                            (when (< i 3) (yield i) (loop (+ i 1)))))))

       (test '(0 1 2) (generator->list g))

Somewhere else, I have the following procedure:

(define (%mapping<=? comparator mapping1 mapping2)
   (assume (comparator? comparator))
   (assume (mapping? mapping1))
   (assume (mapping? mapping2))
   (pk 'gen1 (generator->list (tree-generator (mapping-tree mapping1))))
   ;;(pk 'gen2 (generator->list (tree-generator (mapping-tree 
mapping2))))
   (let ((less? (comparator-ordering-predicate (mapping-key-comparator 
mapping1)))
         (equality-predicate (comparator-equality-predicate comparator))
         (gen1 (tree-generator (mapping-tree mapping1)))
         (gen2 (tree-generator (mapping-tree mapping2))))
     (let loop ((item1 (gen1))
                (item2 (gen2)))
       (pk 'loop item1 item2)
       (cond
        ((eof-object? item1)
         #t)
        ((eof-object? item2)
         #f)
        (else
         (let ((key1 (car item1)) (value1 (cadr item1))
               (key2 (car item2)) (value2 (cadr item2)))
           (cond
            ((less? key1 key2)
             #f)
            ((less? key2 key1)
             (loop item1 (gen2)))
            ((equality-predicate value1 value2)
             (let* ((item1 (gen1))
                   (item2 (gen2)))
               (loop item1 item2)))
            (else
             #f))))))))

The above rely on a procedure 'generator-fold':

(define (generator-fold f seed . gs)
   (define (inner-fold seedx)
     (pk 'inner-fold seedx)
     (let ((vs (map (lambda (g) (g)) gs)))
       (pk 'inner-fold-vs vs)
       (if (any eof-object? vs)
           (pk 'out seedx)
           (inner-fold (pk 'new (apply f (pk 'args f seedx (append vs 
(list seedx)))))))))
   (pk 'generator-fold f seed gs)
   (inner-fold seed))

Which outputs the following:

;;; (generator-fold #<procedure cons (_ _)> () (#<procedure 22e9600 at 
scheme/generator.scm:132:2 ()>))

;;; (inner-fold ())

;;; (inner-fold-vs ((a 1)))

;;; (args #<procedure cons (_ _)> () ((a 1) ()))

;;; (new ((a 1)))

;;; (inner-fold ((a 1)))

;;; (inner-fold-vs ((b 2)))

;;; (args #<procedure cons (_ _)> () ((b 2) ()))

;;; (new ((b 2)))

;;; (inner-fold ((b 2)))

;;; (inner-fold-vs ((c 3)))

;;; (args #<procedure cons (_ _)> ((b 2)) ((c 3) ((b 2))))

;;; (new ((c 3) (b 2)))

;;; (inner-fold ((c 3) (b 2)))

;;; (inner-fold-vs (#<eof>))

;;; (out ())

;;; (gen1 ())

;;; (loop (a 1) (a 1))

;;; (loop (a 1) (b 2))

;;; (mapping=? #f)

The lines starting with ``;;; inner-fold'' should display the 
accumulator
of ``generator-fold'' called seedx. Here is only those:

;;; (inner-fold ())

;;; (inner-fold ((a 1)))

;;; (inner-fold ((b 2)))

;;; (inner-fold ((c 3) (b 2)))

At some point the (a 1) is lost.

Also in the end 'generator-fold' returns the empty list:

;;; (out ())

Is my definition of 'make-coroutine-generator' buggy or is it something 
else?

If you want to reproduce the issue live you can do the following:

   $ git clone https://git.sr.ht/~amz3/guile-r7rs r7rs
   $ cd r7rs
   $ git checkout wip-delimited-continuation
   $ ./bootstrap && ./configure && make
   $ ./pre-inst-env guile tests.scm



             reply	other threads:[~2019-05-05 16:48 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-05 16:48 amirouche [this message]
2019-05-05 16:54 ` prompt and generator amirouche
2019-05-08 21:48   ` Amirouche Boubekki

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=ec0818b366d668a767d49a8b643390cf@hyper.dev \
    --to=amirouche@hyper.dev \
    --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).