<div dir="ltr"><br><br><div class="gmail_quote">On Sat, Dec 19, 2009 at 4:46 PM, ntupel <span dir="ltr">&lt;<a href="mailto:ntupel@googlemail.com">ntupel@googlemail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I have looked at the recently released Control.Failure library but I<br>
admit, I couldn&#39;t understand it completely. So given the example<br>
below, how would Control.Failure help me here?<br>
<br>
Thanks,<br>
nt<br>
<br>
<br>
-- Theirs (other library code stubs)<br>
data TheirError = TheirErrorCase deriving Show<br>
data TheirData  = TheirData deriving Show<br>
<br>
theirFunc :: [String] -&gt; Either TheirError TheirData<br>
theirFunc = undefined<br>
<br>
<br>
-- Mine (my own code stubs)<br>
data MyError = MyErrorCase deriving Show<br>
data MyData  = MyData deriving Show<br>
<br>
myFuncA :: TheirData -&gt; Either MyError MyData<br>
myFuncA = undefined<br>
<br>
<br>
-- Ugly. How to apply Control.Failure here?<br>
myFuncB :: IO (Either MyError MyData)<br>
myFuncB = do<br>
    let x = theirFunc []<br>
    case x of<br>
        Right x&#39; -&gt; return $ myFuncA x&#39;<br>
        Left  _  -&gt; return . Left $ MyErrorCase<br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
</blockquote></div><br>Well, here&#39;s one way of doing it. You have lots of choices here; these are the decisions I made in implementing the code:<br><br>* myFuncB no longer lives in the IO monad. I wasn&#39;t sure if you specifically wanted that, but now it can work with *any* instance of Failure.<br>
* 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.<br>* I also assume that what you meant by &quot;your code&quot; and &quot;their code&quot; is that you can modify your own code, but not theirs.<br>
<br>If you show me what the real code is you&#39;re working on, I&#39;d be happy to more fully develop a better solution with you. Anyway, here&#39;s the code.<br><br>Michael<br><br>{-# LANGUAGE FlexibleContexts #-}<br>{-# LANGUAGE DeriveDataTypeable #-}<br>
{-# LANGUAGE StandaloneDeriving #-}<br>import Control.Failure<br>import Data.Typeable (Typeable)<br>import Control.Exception (Exception)<br><br>-- Theirs (other library code stubs)<br>data TheirError = TheirErrorCase deriving Show<br>
data TheirData  = TheirData deriving Show<br><br>theirFunc :: [String] -&gt; Either TheirError TheirData<br>theirFunc = undefined<br><br><br>-- Mine (my own code stubs)<br>data MyError = MyErrorCase deriving (Show, Typeable)<br>
instance Exception MyError<br>deriving instance Typeable TheirError<br>instance Exception TheirError<br>data MyData  = MyData deriving Show<br><br>myFuncA :: MonadFailure MyError m =&gt; TheirData -&gt; m MyData<br>--myFuncA :: TheirData -&gt; Either MyError MyData<br>
myFuncA = undefined<br><br><br>myFuncB :: (MonadFailure MyError m, MonadFailure TheirError m)<br>        =&gt; m MyData<br>myFuncB = do<br>   x &lt;- try $ theirFunc []<br>   myFuncA x<br><br></div>