unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Matt Hellige <matt@immute.net>
Subject: Continuations: possible newbie question
Date: Tue, 17 Dec 2002 18:14:25 -0600	[thread overview]
Message-ID: <20021217181424.A22845@metro.immute.net> (raw)

I'm trying to use guile in a software project, and have encountered a
snag. It may be my imperfect understanding of continuations, but it
seems weird. First a little background...

I'm trying to use guile to implement key bindings, planning to bind
key-strokes to thunks. I'd like to be able to write a key binding like:
 (define (mythunk)
  (goto-item (get-string)))
 (bind-key "g" mythunk)

where goto-item and get-string are primitives. Now, the issue is that
get-string needs to do some interaction with the user, so i'd like to
be able to return to the main application loop to do the interaction,
then cause get-string to return the result.

To do this, I figured I could use a combination of exceptions and
call/cc. But I can't seem to get it to work.

I have a simple test case set up that demonstrates the problem.
Given the following definitions:
    (define call/cc call-with-current-continuation)
    (define cont #f)

    (define (get-string)
     (let ((result (call/cc (lambda (c) (set! cont c) #f))))
      (if result
       result
       (throw 'interact))))

    (define (done-reading result)
     (if cont
      (let ((tmp cont))
       (set! cont #f)
       (tmp result))))

    (define (bind)
     (display (get-string))(newline))

    (define (exec-binding thunk)
     (catch 'interact thunk (lambda (key) "escaped")))

Here's what I'd expect:
    guile> (exec-binding bind)
    "escaped"
    guile> (done-reading "hi")
    hi
    guile> 

But here's what I get:
    guile> (exec-binding bind)
    "escaped"
    guile> (done-reading "hi")
    (((()) #<eval-closure 40278f60>))
    guile> 
    guile> 
    guile> (exec-binding bind)
    "escaped"
    guile> (done-reading "hi")
    (#<unknown-immediate 0xaa8dc> #<smob 4001f000> . #i(Segmentation fault

Notice that it changes between the first and second tries, and also
please notice the seg fault... This definitely doesn't seem quite right!
It also behaves similarly if I try (exec-binding get-string), although
it doesn't always crash.

On the other hand, the following seems to work correctly:
    guile> (get-string)
    <unnamed port>: In procedure gsubr-apply in expression (throw (quote
    interact)):
    <unnamed port>: unhandled-exception: interact
    ABORT: (misc-error)

    Type "(backtrace)" to get more information or "(debug)" to enter the
    debugger.
    guile> (done-reading "hi")
    "hi"
    guile> 

The error, of course, is due to the fact that we're no longer in
exec-binding when the exception is thrown.

So, my question basically is: is this due to a bug in guile, or to my
abuse of continuations? Either way, can anyone suggest another way to
do what I'm trying to do? I'm rather new to this, so my solution may
be too convoluted to begin with. If no one here can suggest anything,
is there a more general scheme forum that I could try?

This is all using guile-1.6.0. Thanks very much!

Matt

-- 
Matt Hellige                  matt@immute.net
http://matt.immute.net


_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-user


             reply	other threads:[~2002-12-18  0:14 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-12-18  0:14 Matt Hellige [this message]
  -- strict thread matches above, loose matches on Subject: below --
2002-12-18 17:27 Continuations: possible newbie question Thien-Thi Nguyen
2002-12-18 18:14 ` Matt Hellige
2002-12-18 20:04   ` Neil Jerram
2002-12-18 23:51     ` Matt Hellige
2002-12-19  0:41       ` Marius Vollmer
2002-12-19  2:42         ` Matt Hellige
2002-12-18 18:42 Thien-Thi Nguyen

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=20021217181424.A22845@metro.immute.net \
    --to=matt@immute.net \
    /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).