Home > error monad > error monad

Error Monad

Contents

type classes)Safe HaskellSafeLanguageHaskell98Control.Monad.ErrorContentsMonads with error handlingThe ErrorT monad transformerExample 1: Custom Error Data TypeExample 2: Using ErrorT Monad TransformerDescriptionDeprecated: Use Control.Monad.Except insteadComputation type:Computations which may fail or throw exceptions.Binding strategy:Failure records error monad transformer information about the cause/location of the failure. Failure values bypass data monad error the bound function, other values are used as inputs to the bound function.Useful for:Building computations from either monad sequences of functions that may fail or using exception handling to structure error handling.Zero and plus:Zero is represented by an empty error and the plus operation

Either Haskell Example

executes its second argument if the first fails.Example type:Either String aThe Error monad (also called the Exception monad).Synopsisclass Monad m => MonadError e m | m -> e wherethrowError :: e -> m acatchError :: m a -> (e -> m a) -> m aclass Error a wherenoMsg :: astrMsg :: String control monad error -> anewtype ErrorT e m a :: * -> (* -> *) -> * -> * = ErrorT {runErrorT :: m (Either e a)}runErrorT :: ErrorT e m a -> m (Either e a)mapErrorT :: (m (Either e a) -> n (Either e' b)) -> ErrorT e m a -> ErrorT e' n bmodule Control.Monadmodule Control.Monad.Fixmodule Control.Monad.TransMonads with error handlingclass Monad m => MonadError e m | m -> e where SourceThe strategy of combining computations that can throw exceptions by bypassing bound functions from the point an exception is thrown to the point that it is handled.Is parameterized over the type of error information and the monad type constructor. It is common to use Either String as the monad type constructor for an error monad in which error descriptions take the form of strings. In that case and many other common cases the resulting monad is already defined as an instance of the

type classes)Safe HaskellSafeLanguageHaskell98Control.Monad.ExceptContentsMonads with error handlingThe ErrorT monad transformerExample 1: Custom Error Data TypeExample 2: Using ExceptT Monad TransformerDescriptionComputation type:Computations which may fail or throw exceptions.Binding strategy:Failure records information about the cause/location of the failure. Failure values bypass the bound function, other values haskell either monad are used as inputs to the bound function.Useful for:Building computations from sequences of

Haskell Error

functions that may fail or using exception handling to structure error handling.Example type:Either String aThe Error monad (also called

Haskell Catch Error

the Exception monad).Synopsisclass Monad m => MonadError e m | m -> e wherethrowError :: e -> m acatchError :: m a -> (e -> m a) -> m anewtype ExceptT e https://hackage.haskell.org/package/mtl/docs/Control-Monad-Error.html m a :: * -> (* -> *) -> * -> * = ExceptT (m (Either e a))type Except e = ExceptT e IdentityrunExceptT :: ExceptT e m a -> m (Either e a)mapExceptT :: (m (Either e a) -> n (Either e' b)) -> ExceptT e m a -> ExceptT e' n bwithExceptT :: Functor m => (e -> e') -> ExceptT e m a https://hackage.haskell.org/package/mtl/docs/Control-Monad-Except.html -> ExceptT e' m arunExcept :: Except e a -> Either e amapExcept :: (Either e a -> Either e' b) -> Except e a -> Except e' bwithExcept :: (e -> e') -> Except e a -> Except e' amodule Control.Monadmodule Control.Monad.Fixmodule Control.Monad.TransMonads with error handlingclass Monad m => MonadError e m | m -> e where SourceThe strategy of combining computations that can throw exceptions by bypassing bound functions from the point an exception is thrown to the point that it is handled.Is parameterized over the type of error information and the monad type constructor. It is common to use Either String as the monad type constructor for an error monad in which error descriptions take the form of strings. In that case and many other common cases the resulting monad is already defined as an instance of the MonadError class. You can also define your own error type and/or use a monad type constructor other than Either String or Either IOError. In these cases you will have to explicitly define instances of the Error and/or MonadError classes.MethodsthrowError :: e -> m a SourceIs used within

mvanier Log in Or connect using: Facebook Twitter VK Google+ Mail.ru OpenID Error Username: Error Password: Forgot password? Remember me Log in Forgot password? Create an Account Your OpenID URL: Log in Mike's World-O-ProgrammingRants and articles about programming languages Yet Another Monad Tutorial http://mvanier.livejournal.com/5103.html (part 5: error-handling monads) In the previous article we showed how to derive and use the Maybe and list monads. In this article and the next we're going to look at how to use monads for error handling, http://programmers.stackexchange.com/questions/150837/maybe-monad-vs-exceptions also known as exception handling. Error-handling computations The kind of computation that we want to model with our error-handling monads will have types that are schematically like this: a --[possibly fail with a specified error condition]--> b error monad In words, this kind of computation maps an input value of type a to an output value of type b, but instead of returning a value of type b, it may fail with a specified error condition. This is clearly very similar to the Maybe monad. One difference is that all failures in the Maybe monad are indistinguishable (they return the Nothing value). In contrast, in error monads there are usually a variety of distinguishable error monad transformer error conditions that can cause a computation to fail. Another difference is that when running error handling computations it's desirable to have a mechanism for trapping errors and recovering from error situations in a controlled way (this goes by the name exception handling in most programming languages). As we'll see, using error monads you can easily roll your own exception handling system. Error handling in Haskell A programmer who wants to write code that may fail with specific error conditions has a large number of options in Haskell (maybe too large). Some of the more common ones include: The error function Exceptions in the IO monad Extensible exceptions Monadic exceptions Let's look at these one at a time. The error function Most Haskell programmers are familiar with the error function. It is generally used just to abort a computation that cannot succeed. A good example is calling a factorial function with a negative integer: factorial :: Integer -> Integer factorial n | n < 0 = error "factorial: negative argument" factorial 0 = 1 factorial n = n * factorial (n - 1) The error function has a very interesting type: error :: String -> a This means that the error function takes a String as argument and can "return" a value of any type whatsoever. In fact, of course, the error function doesn't

here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Programmers Questions Tags Users Badges Unanswered Ask Question _ Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top Maybe monad vs exceptions up vote 22 down vote favorite 7 I wonder what are the advantages of Maybe monad over exceptions? It looks like Maybe is just explicit (and rather space-consuming) way of try..catch syntax. update Please note that I'm intentionally not mentioning Haskell. language-agnostic functional-programming exceptions exception-handling monad share|improve this question edited May 31 '12 at 14:24 gnat 21.8k1365129 asked May 30 '12 at 13:01 Vladimir 21328 add a comment| 7 Answers 7 active oldest votes up vote 36 down vote accepted Using Maybe (or its cousin Either which works basically the same way but lets you return an arbitrary value in place of Nothing) serves a slightly different purpose than exceptions. In Java terms, it's like having a checked exception rather than a runtime exception. It represents something expected which you have to deal with, rather than an error you did not expect. So a function like indexOf would return a Maybe value because you expect the possibility that the item is not in the list. This is much like returning null from a function, except in a type-safe way which forces you to deal with the null case. Either works the same way except that you can return information associated with the error case, so it's actually more similar to an exception than Maybe. So what are the advantages of the Maybe/Either approach? For one, it's a first-class citizen of the language. Let's compare a function using Either to one throwing an exception. For the exception case, your only real recourse is a try...catch statement. For the Either function, you could use existing combinators to make the flow control clearer. Here are a couple of examples: First, let's say you want to try several functions that could error out in a row until you get one that doesn't. If you don't get any without errors, you want to return a special error message. This is actually a very useful pattern but would be a horrible pain using try...catch. Hap

 

Related content

No related pages.