typesafe non-local returns in the IO monad

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

typesafe non-local returns in the IO monad

John Meacham
Although I am sure I am not the first to discover this, I thought it was
a neat application of the runST trick. Safe non-local returns for
the IO monad. What I mean by safe is that it is impossible to return to
a context that no longer exists. the api is simple:

> module System.IO.Continuation(IOCont(),newContinuation,callContinuation) where
>
> data IOCont s a = ....
>
> newContinuation :: (forall s . IOCont s a -> IO b) -> (a -> IO b) -> IO b
> newContinuation act cc = ...
>
> callContinuation :: IOCont s a -> a -> IO b
> callContinuation cont x = ...

newContinuation runs its first argument passing a fresh jumppoint into
it, if it is jumped to then the argument is passed to the second
argument of newContinuation.

the universal quantification means that the continuation is unable to
escape the action so you know its context is still available on the
stack.

in jhc these are just implemented as straight FFI calls to setjmp(2) and
longjmp(2) and an IORef.

I feel using the term 'continuation' is something of a misnomer as true
continuations would allow jumping between multiple stacks I would think
but am unsure what a good name for this is then. Is there a fully
typesafe interface for 'true' IO continuations for some definition of
'true'? hmm....

        John

--
John Meacham - ⑆repetae.net⑆john⑈
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

RE: typesafe non-local returns in the IO monad

Simon Peyton Jones
I'm not sure this works. Consider this

        newContinuation (\k -> return (callContinuation k)) ...

The partial application (callContinuation k) has no 's' in its type, and so can go anywhere.

Simon

| -----Original Message-----
| From: [hidden email] [mailto:[hidden email]] On Behalf Of John
| Meacham
| Sent: 21 February 2006 12:40
| To: [hidden email]
| Subject: [Haskell-cafe] typesafe non-local returns in the IO monad
|
| Although I am sure I am not the first to discover this, I thought it was
| a neat application of the runST trick. Safe non-local returns for
| the IO monad. What I mean by safe is that it is impossible to return to
| a context that no longer exists. the api is simple:
|
| > module System.IO.Continuation(IOCont(),newContinuation,callContinuation) where
| >
| > data IOCont s a = ....
| >
| > newContinuation :: (forall s . IOCont s a -> IO b) -> (a -> IO b) -> IO b
| > newContinuation act cc = ...
| >
| > callContinuation :: IOCont s a -> a -> IO b
| > callContinuation cont x = ...
|
| newContinuation runs its first argument passing a fresh jumppoint into
| it, if it is jumped to then the argument is passed to the second
| argument of newContinuation.
|
| the universal quantification means that the continuation is unable to
| escape the action so you know its context is still available on the
| stack.
|
| in jhc these are just implemented as straight FFI calls to setjmp(2) and
| longjmp(2) and an IORef.
|
| I feel using the term 'continuation' is something of a misnomer as true
| continuations would allow jumping between multiple stacks I would think
| but am unsure what a good name for this is then. Is there a fully
| typesafe interface for 'true' IO continuations for some definition of
| 'true'? hmm....
|
|         John
|
| --
| John Meacham - ⑆repetae.net⑆john⑈
| _______________________________________________
| Haskell-Cafe mailing list
| [hidden email]
| http://www.haskell.org/mailman/listinfo/haskell-cafe

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: typesafe non-local returns in the IO monad

John Meacham
On Tue, Feb 21, 2006 at 01:07:51PM -0000, Simon Peyton-Jones wrote:
> I'm not sure this works. Consider this
>
> newContinuation (\k -> return (callContinuation k)) ...
>
> The partial application (callContinuation k) has no 's' in its type, and so can go anywhere.

Ah, you are right. silly me, I thought I had come up with something
clever.. I don't see any good way to fix this in the IO monad proper off
the top of my head.. I think I will still provide the primitives in jhc
for now but put a big caveat in the docs since they might still be
useful to use internally in some safe library (and you can already shoot
yourself in the IO monad).

but I am much less happy about them.

        John

--
John Meacham - ⑆repetae.net⑆john⑈
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe