Christopher Howard | 10 Feb 18:33 2014

Lisp Style Restarts in Haskell

Hi. I was curious if Haskell has something like "restarts" in Common
Lisp. My search engines didn't give relevant results for "Haskell
restarts". However, there were several results for "Haskell coroutines"
and, from what I can gather, restarts and coroutines are a similar or
related subject.

--

-- 
http://www.lugod.org/presentations/pgp/why.html

Attachment (0x25C6C407.asc): application/pgp-keys, 1752 bytes
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Erik Dominikus | 12 Feb 19:16 2014
Picon

Re: Lisp Style Restarts in Haskell

How about the Control.Retry module in the 'retry' package?

http://hackage.haskell.org/package/retry

On Feb 11, 2014 12:51 AM, "Christopher Howard" <christopher.howard <at> frigidcode.com> wrote:
Hi. I was curious if Haskell has something like "restarts" in Common
Lisp. My search engines didn't give relevant results for "Haskell
restarts". However, there were several results for "Haskell coroutines"
and, from what I can gather, restarts and coroutines are a similar or
related subject.

--
http://www.lugod.org/presentations/pgp/why.html


_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Henk-Jan van Tuyl | 12 Feb 23:17 2014
Picon

Re: Lisp Style Restarts in Haskell


Or Control.Exception in the base package?

On Wed, 12 Feb 2014 19:16:39 +0100, Erik Dominikus  
<erik.dominikus71 <at> gmail.com> wrote:

> How about the Control.Retry module in the 'retry' package?
>
> http://hackage.haskell.org/package/retry
> On Feb 11, 2014 12:51 AM, "Christopher Howard" <
> christopher.howard <at> frigidcode.com> wrote:
>
>> Hi. I was curious if Haskell has something like "restarts" in Common
>> Lisp. My search engines didn't give relevant results for "Haskell
>> restarts". However, there were several results for "Haskell coroutines"
>> and, from what I can gather, restarts and coroutines are a similar or
>> related subject.
>>

Regards,
Henk-Jan van Tuyl

--

-- 
Folding <at> home
What if you could share your unused computer power to help find a cure? In  
just 5 minutes you can join the world's biggest networked computer and get  
us closer sooner. Watch the video.
http://folding.stanford.edu/

http://Van.Tuyl.eu/
http://members.chello.nl/hjgtuyl/tourdemonad.html
Haskell programming
--
Mikael Brockman | 13 Feb 12:10 2014

Re: Lisp Style Restarts in Haskell

"Henk-Jan van Tuyl" <hjgtuyl <at> chello.nl> writes:

> Or Control.Exception in the base package?

With Lisp-style restarts, exceptions (Common Lisp calls them
"conditions") don't necessarily "unwind the stack."  Instead, they
provide a set of alternatives for how to proceed.  Calls to the throwing
function can be wrapped in a handler that chooses, say, whether to skip
or abort.

To take a Haskell example, Data.Text.Encoding has an API for doing
Unicode decoding with "controllable error handling."  It's pretty
simple, and not very flexible.

> type OnDecodeError = String -> Maybe Word8 -> Maybe Char
>
> decodeUtf8With :: OnDecodeError -> ByteString -> Text

Considering some different possibilities for this API...  Something like
this (a kind of defunctionalized version) might be more familiar to a CL
programmer:

> data DecodeCondition = InvalidWord Word8 | UnexpectedEOF
> data DecodeRestart   = Ignore | UseChar Char
>
> decodeUtf8With :: (DecodeCondition -> DecodeRestart)
>                -> ByteString -> Text

We can use ImplicitParams to approximate the dynamic scope behavior, and
LambdaCase to write what CL calls the "restart-case":

> decodeUtf8 :: (?restart :: DecodeCondition -> DecodeRestart)
>            -> ByteString -> Text

Usage:

> myDecode s =
>   let ?restart = \case InvalidWord _ -> UseChar '*'
>                        UnexpectedEOF -> Ignore
>   in decodeUtf8 s

                                 * * *

One of the cool things about CL's condition system that this
implementation doesn't capture is the way the runtime environment can
provide interactive prompts for restarting uncaught conditions.  An
example session:

> CL-USER 6 > (restartable-gethash 'mango *fruits-and-vegetables*)
>
>  Error: RESTARTABLE-GETHASH error getting MANGO [...]
>    1 (continue) Return not having found the value.
>    2 Try getting the key from the hash again.
>    3 Use a new key.
>    4 Use a new hash.
>    5 (abort) Return to level 0.
>    6 Return to top loop level 0.
>
>  Type :b for backtrace, :c <option number> to proceed,
>    or :? for other options

To increase the flexibility of our purely functional restarts, we can
use monads:

> decodeUtf8With :: Monad m => (DecodeCondition -> m DecodeRestart)
>                -> ByteString -> m Text
>
> myDecode :: ByteString -> IO Text
> myDecode = decodeUtf8With (\c -> askUserAbout c >>= decide)

We can also use other monads:

> cautiousDecode :: ByteString -> Maybe Text
> cautiousDecode = decodeUtf8With (const Nothing)

This of course opens up a whole world of bizarre control flow
possibilities.

--
Mikael Brockman
 <at> mbrock
Kim-Ee Yeoh | 12 Feb 23:58 2014

Re: Lisp Style Restarts in Haskell


On Tue, Feb 11, 2014 at 12:33 AM, Christopher Howard <christopher.howard <at> frigidcode.com> wrote:
I was curious if Haskell has something like "restarts" in Common
Lisp. My search engines didn't give relevant results for "Haskell
restarts".

Searching "lisp style restarts" brings up this Haskell-relevant link:

http://lambda-the-ultimate.org/node/1544

-- Kim-Ee
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Gmane