[email protected]
[Top] [All Lists]

Re: [Haskell-cafe] How to use Control.Failure?

Subject: Re: [Haskell-cafe] How to use Control.Failure?
From: Michael Snoyman
Date: Sat, 19 Dec 2009 18:14:09 +0200


On Sat, Dec 19, 2009 at 4:46 PM, ntupel <[email protected]> wrote:
I have looked at the recently released Control.Failure library but I
admit, I couldn't understand it completely. So given the example
below, how would Control.Failure help me here?

Thanks,
nt


-- Theirs (other library code stubs)
data TheirError = TheirErrorCase deriving Show
data TheirData Â= TheirData deriving Show

theirFunc :: [String] -> Either TheirError TheirData
theirFunc = undefined


-- Mine (my own code stubs)
data MyError = MyErrorCase deriving Show
data MyData Â= MyData deriving Show

myFuncA :: TheirData -> Either MyError MyData
myFuncA = undefined


-- Ugly. How to apply Control.Failure here?
myFuncB :: IO (Either MyError MyData)
myFuncB = do
 Âlet x = theirFunc []
 Âcase x of
   ÂRight x' -> return $ myFuncA x'
   ÂLeft Â_ Â-> return . Left $ MyErrorCase
_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe

Well, here's one way of doing it. You have lots of choices here; these are the decisions I made in implementing the code:

* myFuncB no longer lives in the IO monad. I wasn't sure if you specifically wanted that, but now it can work with *any* instance of Failure.
* Since I assumed you ultimately wanted it to land in the IO monad, I defined Exception instances. However, if you were dealing with a different Failure instance (like [] or Maybe), these would be unncesary.
* I also assume that what you meant by "your code" and "their code" is that you can modify your own code, but not theirs.

If you show me what the real code is you're working on, I'd be happy to more fully develop a better solution with you. Anyway, here's the code.

Michael

{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE StandaloneDeriving #-}
import Control.Failure
import Data.Typeable (Typeable)
import Control.Exception (Exception)

-- Theirs (other library code stubs)
data TheirError = TheirErrorCase deriving Show
data TheirData = TheirData deriving Show

theirFunc :: [String] -> Either TheirError TheirData
theirFunc = undefined


-- Mine (my own code stubs)
data MyError = MyErrorCase deriving (Show, Typeable)
instance Exception MyError
deriving instance Typeable TheirError
instance Exception TheirError
data MyData = MyData deriving Show

myFuncA :: MonadFailure MyError m => TheirData -> m MyData
--myFuncA :: TheirData -> Either MyError MyData
myFuncA = undefined


myFuncB :: (MonadFailure MyError m, MonadFailure TheirError m)
ÂÂÂÂÂÂÂ => m MyData
myFuncB = do
ÂÂ x <- try $ theirFunc []
ÂÂ myFuncA x

_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe
<Prev in Thread] Current Thread [Next in Thread>