ErrorT and catchError question

Hi, I'm trying to use ErrorT transformer based on Don Stewart's Shell example: http://cgi.cse.unsw.edu.au/~dons/blog/2007/03/10#programmable-semicolons It's probably a trivial question, but I cannot figure out how to implement the catchError function in: instance MonadError String Shell where throwError = error . ("Shell failed: "++) catchError l h = ??? Thanks for help, Adam

On Jan 15, 2008, at 0:28 , Adam Smyczek wrote:
It's probably a trivial question, but I cannot figure out how to implement the catchError function in:
instance MonadError String Shell where throwError = error . ("Shell failed: "++) catchError l h = ???
Take a look at Control.Exception.catch for starters. -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

Ups, resend, first response did not make into the list. On Jan 14, 2008, at 9:33 PM, Brandon S. Allbery KF8NH wrote:
On Jan 15, 2008, at 0:28 , Adam Smyczek wrote:
It's probably a trivial question, but I cannot figure out how to implement the catchError function in:
instance MonadError String Shell where throwError = error . ("Shell failed: "++) catchError l h = ???
Take a look at Control.Exception.catch for starters.
No, did not help and going over the source code of Control.Monad.Error did not help as well. Does someone have other tips for me? Thanks, Adam
-- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

On Jan 15, 2008, at 22:05 , Adam Smyczek wrote:
Ups, resend, first response did not make into the list. On Jan 14, 2008, at 9:33 PM, Brandon S. Allbery KF8NH wrote:
On Jan 15, 2008, at 0:28 , Adam Smyczek wrote:
It's probably a trivial question, but I cannot figure out how to implement the catchError function in:
instance MonadError String Shell where throwError = error . ("Shell failed: "++) catchError l h = ???
Take a look at Control.Exception.catch for starters.
No, did not help and going over the source code of Control.Monad.Error did not help as well. Does someone have other tips for me?
Perhaps you could explain what you're looking for? Your typeclass doesn't tell us anything about the semantics. -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

On Jan 15, 2008, at 7:34 PM, Brandon S. Allbery KF8NH wrote:
On Jan 15, 2008, at 22:05 , Adam Smyczek wrote:
Ups, resend, first response did not make into the list. On Jan 14, 2008, at 9:33 PM, Brandon S. Allbery KF8NH wrote:
On Jan 15, 2008, at 0:28 , Adam Smyczek wrote:
It's probably a trivial question, but I cannot figure out how to implement the catchError function in:
instance MonadError String Shell where throwError = error . ("Shell failed: "++) catchError l h = ???
Take a look at Control.Exception.catch for starters.
No, did not help and going over the source code of Control.Monad.Error did not help as well. Does someone have other tips for me?
Perhaps you could explain what you're looking for? Your typeclass doesn't tell us anything about the semantics.
The type declaration: newtype Loader a = Loader { load :: ErrorT String IO a } deriving (Functor, Monad, MonadIO) instance MonadError String Loader where throwError = error . ("Error: " ++) l `catchError` h = ??? how do I implement this ??? -- Example usage data Attribute = Attribute { a_name :: Name, a_value :: Value } deriving Show -- Find a required attribute by name and throw an -- exception it if does not exist findRequired :: Name -> [Attribute] -> Loader Attribute findRequired n as = case find (\a -> a_name a == n) as of Just a -> return a Nothing -> throwError $ "Missing required '" ++ n ++ "' attribute!" -- I would like to use catchException for -- findOptional and provide default value -- if findRequired fails findOptional :: Name -> [Attribute] -> Value -> Loader Attribute findOptional n as defaultValue = catchError (findRequired n as) (\_ -> return $ Attribute n defaultValue) As you probably can see on this code, I'm a haskell newbie and open for all tips how to improve this code. Thanks, Adam
-- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

On 15 Jan 2008, at 7:54 PM, Adam Smyczek wrote:
On Jan 15, 2008, at 7:34 PM, Brandon S. Allbery KF8NH wrote:
On Jan 15, 2008, at 22:05 , Adam Smyczek wrote:
Ups, resend, first response did not make into the list. On Jan 14, 2008, at 9:33 PM, Brandon S. Allbery KF8NH wrote:
On Jan 15, 2008, at 0:28 , Adam Smyczek wrote:
It's probably a trivial question, but I cannot figure out how to implement the catchError function in:
instance MonadError String Shell where throwError = error . ("Shell failed: "++) catchError l h = ???
Take a look at Control.Exception.catch for starters.
No, did not help and going over the source code of Control.Monad.Error did not help as well. Does someone have other tips for me?
Perhaps you could explain what you're looking for? Your typeclass doesn't tell us anything about the semantics.
The type declaration:
newtype Loader a = Loader { load :: ErrorT String IO a } deriving (Functor, Monad, MonadIO)
instance MonadError String Loader where throwError = error . ("Error: " ++)
I don't think this is what you want; you're throwing away the benefit of using ErrorT. You probably want throwError = Loader . throwError
l `catchError` h = ??? how do I implement this ???
l `catchError` h = Loader (load l `catchError` load . h) jcc

On Jan 15, 2008, at 8:07 PM, Jonathan Cast wrote:
On 15 Jan 2008, at 7:54 PM, Adam Smyczek wrote:
On Jan 15, 2008, at 7:34 PM, Brandon S. Allbery KF8NH wrote:
On Jan 15, 2008, at 22:05 , Adam Smyczek wrote:
Ups, resend, first response did not make into the list. On Jan 14, 2008, at 9:33 PM, Brandon S. Allbery KF8NH wrote:
On Jan 15, 2008, at 0:28 , Adam Smyczek wrote:
It's probably a trivial question, but I cannot figure out how to implement the catchError function in:
instance MonadError String Shell where throwError = error . ("Shell failed: "++) catchError l h = ???
Take a look at Control.Exception.catch for starters.
No, did not help and going over the source code of Control.Monad.Error did not help as well. Does someone have other tips for me?
Perhaps you could explain what you're looking for? Your typeclass doesn't tell us anything about the semantics.
The type declaration:
newtype Loader a = Loader { load :: ErrorT String IO a } deriving (Functor, Monad, MonadIO)
instance MonadError String Loader where throwError = error . ("Error: " ++)
I don't think this is what you want; you're throwing away the benefit of using ErrorT. You probably want
throwError = Loader . throwError
l `catchError` h = ??? how do I implement this ???
l `catchError` h = Loader (load l `catchError` load . h)
Thanks Jonathan, this is exactly what I was looking for and I got the concept of ErrorT now, I think.
jcc
participants (3)
-
Adam Smyczek
-
Brandon S. Allbery KF8NH
-
Jonathan Cast