unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* `lazy-catch' and `dynamic-wind'
@ 2008-11-23 17:25 Ludovic Courtès
  2008-11-23 21:34 ` Neil Jerram
  0 siblings, 1 reply; 5+ messages in thread
From: Ludovic Courtès @ 2008-11-23 17:25 UTC (permalink / raw)
  To: guile-user

Hello Guilers!

I noticed the following subtle difference between fluids and SRFI-39
parameters when accessed from a `lazy-catch' handler:

  (define fl (make-fluid))
  (fluid-set! fl 'outside)

  (lazy-catch #t
              (lambda ()
                (fluid-set! fl 'inside)
                (throw 'foobar))
              (lambda (key . args)
                (format #t "fluid = ~A~%" (fluid-ref fl))
                (exit 1)))

  => PRINTS: fluid = inside

... compared to:

  (use-modules (srfi srfi-39))

  (define fl (make-parameter 'outside))

  (lazy-catch #t
              (lambda ()
                (parameterize ((fl 'inside))
                  (throw 'foobar)))
              (lambda (key . args)
                (format #t "fluid = ~A~%" (fl))
                (exit 1)))

  => PRINTS: fluid = outside

This comes from the fact that `parameterize' sets up a `dynamic-wind'
whose unwinder is called *before* the `lazy-catch' handler.  I find it a
bit counter-intuitive since the `lazy-catch' is documented as follows:

  This behaves exactly like `catch', except that it does not unwind
  the stack before invoking HANDLER.

The stack is indeed not unwinded, but the unwinders have already been
invoked, as illustrated here:

  (lazy-catch #t
              (lambda ()
                (dynamic-wind
                  (lambda () #t)
                  (lambda () (throw 'foobar))
                  (lambda () (format #t "unwind~%"))))
              (lambda (key . args)
                (let ((stack (make-stack #t)))
                  (display-backtrace stack (current-output-port)))))

  => PRINTS:
     unwind
     [...]
       30: 5* [gsubr-apply #<primitive-procedure throw> foobar]
     In unknown file:
        ?: 6* [#<procedure #f (key . args)> foobar]
     In ../../src/lazy-catch+fluid.scm:
       33: 7* (let* ((stack #)) (display-backtrace stack (current-output-port)))

Am I missing something or should we consider it a bug?

Thanks,
Ludo'.





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

* Re: `lazy-catch' and `dynamic-wind'
  2008-11-23 17:25 `lazy-catch' and `dynamic-wind' Ludovic Courtès
@ 2008-11-23 21:34 ` Neil Jerram
  2008-11-23 22:56   ` Ludovic Courtès
  0 siblings, 1 reply; 5+ messages in thread
From: Neil Jerram @ 2008-11-23 21:34 UTC (permalink / raw)
  To: Ludovic Courtès; +Cc: guile-user

2008/11/23 Ludovic Courtès <ludo@gnu.org>:
> Hello Guilers!
>
> I noticed the following subtle difference between fluids and SRFI-39
> parameters when accessed from a `lazy-catch' handler:
>
>  (define fl (make-fluid))
>  (fluid-set! fl 'outside)
>
>  (lazy-catch #t
>              (lambda ()
>                (fluid-set! fl 'inside)
>                (throw 'foobar))
>              (lambda (key . args)
>                (format #t "fluid = ~A~%" (fluid-ref fl))
>                (exit 1)))
>
>  => PRINTS: fluid = inside

This is as expected.  Note that there is nothing like dynamic-wind
inside fluid-set!  Did you perhaps mean with-fluids instead?  If you
used with-fluids, I would expect the same behaviour as you've
described for parameterize.

> ... compared to:
>
>  (use-modules (srfi srfi-39))
>
>  (define fl (make-parameter 'outside))
>
>  (lazy-catch #t
>              (lambda ()
>                (parameterize ((fl 'inside))
>                  (throw 'foobar)))
>              (lambda (key . args)
>                (format #t "fluid = ~A~%" (fl))
>                (exit 1)))
>
>  => PRINTS: fluid = outside
>
> This comes from the fact that `parameterize' sets up a `dynamic-wind'
> whose unwinder is called *before* the `lazy-catch' handler.  I find it a
> bit counter-intuitive since the `lazy-catch' is documented as follows:
>
>  This behaves exactly like `catch', except that it does not unwind
>  the stack before invoking HANDLER.

That text is misleading and should be improved.  See the manual
section [1] for the whole story, which explains that it is actually
only the call stack that is not unwound.

[1] http://www.gnu.org/software/guile/manual/html_node/Lazy-Catch.html#Lazy-Catch

This is why I invented with-throw-handler and the optional
pre-unwind-handler parameter of `catch'.  Perhaps you need to use one
of those instead.

Regards,
     Neil




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

* Re: `lazy-catch' and `dynamic-wind'
  2008-11-23 21:34 ` Neil Jerram
@ 2008-11-23 22:56   ` Ludovic Courtès
  2008-11-23 23:25     ` Neil Jerram
  0 siblings, 1 reply; 5+ messages in thread
From: Ludovic Courtès @ 2008-11-23 22:56 UTC (permalink / raw)
  To: guile-user

Hi Neil,

"Neil Jerram" <neiljerram@googlemail.com> writes:

> This is as expected.  Note that there is nothing like dynamic-wind
> inside fluid-set!  Did you perhaps mean with-fluids instead?  If you
> used with-fluids, I would expect the same behaviour as you've
> described for parameterize.

Yes, that comparison to native fluids was inappropriate.

> That text is misleading and should be improved.  See the manual
> section [1] for the whole story, which explains that it is actually
> only the call stack that is not unwound.
>
> [1] http://www.gnu.org/software/guile/manual/html_node/Lazy-Catch.html#Lazy-Catch
>
> This is why I invented with-throw-handler and the optional
> pre-unwind-handler parameter of `catch'.  Perhaps you need to use one
> of those instead.

Exactly, thanks!  Now, I was actually using SRFI-34's
`with-exception-handler', which I expected to behave like
`with-throw-handler'.  Should we change `with-exception-handler' to use
`with-throw-handler' instead of `lazy-catch'?

(In fact, I don't understand when the `lazy-catch' semantics could be
preferable over the `with-throw-handler' semantics'.)

Thanks,
Ludo'.





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

* Re: `lazy-catch' and `dynamic-wind'
  2008-11-23 22:56   ` Ludovic Courtès
@ 2008-11-23 23:25     ` Neil Jerram
  2008-11-24  8:43       ` Ludovic Courtès
  0 siblings, 1 reply; 5+ messages in thread
From: Neil Jerram @ 2008-11-23 23:25 UTC (permalink / raw)
  To: Ludovic Courtès; +Cc: guile-user

2008/11/23 Ludovic Courtès <ludo@gnu.org>:
>
> Exactly, thanks!  Now, I was actually using SRFI-34's
> `with-exception-handler', which I expected to behave like
> `with-throw-handler'.  Should we change `with-exception-handler' to use
> `with-throw-handler' instead of `lazy-catch'?

Yes, I think we should, since SRFI-34 says that "The handler is called
in the dynamic environment of the call to raise, except that the
current exception handler is that in place for the call to
with-exception-handler that installed the handler being called."

(I recall now being aware of this as a discrepancy at the time I wrote
(srfi srfi-34) using lazy-catch; but I let it slip because we didn't
have with-throw-handler at that time, and because none of the SRFI-34
reference test cases at that time differentiated between unwinding or
not unwinding the dynamic context, hence the lazy-catch implemented
passed all of those cases.)

Could you make that change?

> (In fact, I don't understand when the `lazy-catch' semantics could be
> preferable over the `with-throw-handler' semantics'.)

I agree.  To be fair, there is a slight messiness in the
with-throw-handler semantics, because of the handler context being
unwound before the handler is called, but there is a lot more
messiness in the lazy-catch semantics.

Regards,
       Neil




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

* Re: `lazy-catch' and `dynamic-wind'
  2008-11-23 23:25     ` Neil Jerram
@ 2008-11-24  8:43       ` Ludovic Courtès
  0 siblings, 0 replies; 5+ messages in thread
From: Ludovic Courtès @ 2008-11-24  8:43 UTC (permalink / raw)
  To: guile-user

Hi,

"Neil Jerram" <neiljerram@googlemail.com> writes:

> Could you make that change?

Yes, done:

  http://git.savannah.gnu.org/gitweb/?p=guile.git;a=commitdiff;h=7635043239e9ac2786fec54df3eff73c7f213518

Thanks!

Ludo'.





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

end of thread, other threads:[~2008-11-24  8:43 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-11-23 17:25 `lazy-catch' and `dynamic-wind' Ludovic Courtès
2008-11-23 21:34 ` Neil Jerram
2008-11-23 22:56   ` Ludovic Courtès
2008-11-23 23:25     ` Neil Jerram
2008-11-24  8:43       ` Ludovic Courtès

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