guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Continuations: possible newbie question


From: Matt Hellige
Subject: Continuations: possible newbie question
Date: Tue, 17 Dec 2002 18:14:25 -0600
User-agent: Mutt/1.2.5i

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                  address@hidden
http://matt.immute.net



reply via email to

[Prev in Thread] Current Thread [Next in Thread]