<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <!-- Generated by HsColour, http://www.cs.york.ac.uk/fp/darcs/hscolour/ --> <title>Control/Monad/Error.hs</title> <link type='text/css' rel='stylesheet' href='hscolour.css' /> </head> <body> <pre><a name="line-1"></a><span class='hs-comment'>{- | <a name="line-2"></a>Module : Control.Monad.Error <a name="line-3"></a>Copyright : (c) Michael Weber <michael.weber@post.rwth-aachen.de> 2001, <a name="line-4"></a> (c) Jeff Newbern 2003-2006, <a name="line-5"></a> (c) Andriy Palamarchuk 2006 <a name="line-6"></a>License : BSD-style (see the file LICENSE) <a name="line-7"></a> <a name="line-8"></a>Maintainer : libraries@haskell.org <a name="line-9"></a>Stability : experimental <a name="line-10"></a>Portability : non-portable (multi-parameter type classes) <a name="line-11"></a> <a name="line-12"></a>[Computation type:] Computations which may fail or throw exceptions. <a name="line-13"></a> <a name="line-14"></a>[Binding strategy:] Failure records information about the cause\/location <a name="line-15"></a>of the failure. Failure values bypass the bound function, <a name="line-16"></a>other values are used as inputs to the bound function. <a name="line-17"></a> <a name="line-18"></a>[Useful for:] Building computations from sequences of functions that may fail <a name="line-19"></a>or using exception handling to structure error handling. <a name="line-20"></a> <a name="line-21"></a>[Zero and plus:] Zero is represented by an empty error and the plus operation <a name="line-22"></a>executes its second argument if the first fails. <a name="line-23"></a> <a name="line-24"></a>[Example type:] @'Data.Either' String a@ <a name="line-25"></a> <a name="line-26"></a>The Error monad (also called the Exception monad). <a name="line-27"></a>-}</span> <a name="line-28"></a> <a name="line-29"></a><span class='hs-comment'>{- <a name="line-30"></a> Rendered by Michael Weber <mailto:michael.weber@post.rwth-aachen.de>, <a name="line-31"></a> inspired by the Haskell Monad Template Library from <a name="line-32"></a> Andy Gill (<<a href="http://web.cecs.pdx.edu/~andy/">http://web.cecs.pdx.edu/~andy/</a>>) <a name="line-33"></a>-}</span> <a name="line-34"></a><span class='hs-keyword'>module</span> <span class='hs-conid'>Control</span><span class='hs-varop'>.</span><span class='hs-conid'>Monad</span><span class='hs-varop'>.</span><span class='hs-conid'>Error</span> <span class='hs-layout'>(</span> <a name="line-35"></a> <span class='hs-comment'>-- * Monads with error handling</span> <a name="line-36"></a> <span class='hs-conid'>MonadError</span><span class='hs-layout'>(</span><span class='hs-keyglyph'>..</span><span class='hs-layout'>)</span><span class='hs-layout'>,</span> <a name="line-37"></a> <span class='hs-conid'>Error</span><span class='hs-layout'>(</span><span class='hs-keyglyph'>..</span><span class='hs-layout'>)</span><span class='hs-layout'>,</span> <a name="line-38"></a> <span class='hs-comment'>-- * The ErrorT monad transformer</span> <a name="line-39"></a> <span class='hs-conid'>ErrorT</span><span class='hs-layout'>(</span><span class='hs-keyglyph'>..</span><span class='hs-layout'>)</span><span class='hs-layout'>,</span> <a name="line-40"></a> <span class='hs-varid'>mapErrorT</span><span class='hs-layout'>,</span> <a name="line-41"></a> <span class='hs-keyword'>module</span> <span class='hs-conid'>Control</span><span class='hs-varop'>.</span><span class='hs-conid'>Monad</span><span class='hs-layout'>,</span> <a name="line-42"></a> <span class='hs-keyword'>module</span> <span class='hs-conid'>Control</span><span class='hs-varop'>.</span><span class='hs-conid'>Monad</span><span class='hs-varop'>.</span><span class='hs-conid'>Fix</span><span class='hs-layout'>,</span> <a name="line-43"></a> <span class='hs-keyword'>module</span> <span class='hs-conid'>Control</span><span class='hs-varop'>.</span><span class='hs-conid'>Monad</span><span class='hs-varop'>.</span><span class='hs-conid'>Trans</span><span class='hs-layout'>,</span> <a name="line-44"></a> <span class='hs-comment'>-- * Example 1: Custom Error Data Type</span> <a name="line-45"></a> <span class='hs-comment'>-- $customErrorExample</span> <a name="line-46"></a> <a name="line-47"></a> <span class='hs-comment'>-- * Example 2: Using ErrorT Monad Transformer</span> <a name="line-48"></a> <span class='hs-comment'>-- $ErrorTExample</span> <a name="line-49"></a> <span class='hs-layout'>)</span> <span class='hs-keyword'>where</span> <a name="line-50"></a> <a name="line-51"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Control</span><span class='hs-varop'>.</span><span class='hs-conid'>Monad</span><span class='hs-varop'>.</span><span class='hs-conid'>Error</span><span class='hs-varop'>.</span><span class='hs-conid'>Class</span> <a name="line-52"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Control</span><span class='hs-varop'>.</span><span class='hs-conid'>Monad</span><span class='hs-varop'>.</span><span class='hs-conid'>Trans</span> <a name="line-53"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Control</span><span class='hs-varop'>.</span><span class='hs-conid'>Monad</span><span class='hs-varop'>.</span><span class='hs-conid'>Trans</span><span class='hs-varop'>.</span><span class='hs-conid'>Error</span> <span class='hs-layout'>(</span><span class='hs-conid'>ErrorT</span><span class='hs-layout'>(</span><span class='hs-keyglyph'>..</span><span class='hs-layout'>)</span><span class='hs-layout'>,</span> <span class='hs-varid'>mapErrorT</span><span class='hs-layout'>)</span> <a name="line-54"></a> <a name="line-55"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Control</span><span class='hs-varop'>.</span><span class='hs-conid'>Monad</span> <a name="line-56"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Control</span><span class='hs-varop'>.</span><span class='hs-conid'>Monad</span><span class='hs-varop'>.</span><span class='hs-conid'>Fix</span> <a name="line-57"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Control</span><span class='hs-varop'>.</span><span class='hs-conid'>Monad</span><span class='hs-varop'>.</span><span class='hs-conid'>Instances</span> <span class='hs-conid'>()</span> <a name="line-58"></a> <a name="line-59"></a><span class='hs-comment'>{- $customErrorExample <a name="line-60"></a>Here is an example that demonstrates the use of a custom 'Error' data type with <a name="line-61"></a>the 'throwError' and 'catchError' exception mechanism from 'MonadError'. <a name="line-62"></a>The example throws an exception if the user enters an empty string <a name="line-63"></a>or a string longer than 5 characters. Otherwise it prints length of the string. <a name="line-64"></a> <a name="line-65"></a>>-- This is the type to represent length calculation error. <a name="line-66"></a>>data LengthError = EmptyString -- Entered string was empty. <a name="line-67"></a>> | StringTooLong Int -- A string is longer than 5 characters. <a name="line-68"></a>> -- Records a length of the string. <a name="line-69"></a>> | OtherError String -- Other error, stores the problem description. <a name="line-70"></a>> <a name="line-71"></a>>-- We make LengthError an instance of the Error class <a name="line-72"></a>>-- to be able to throw it as an exception. <a name="line-73"></a>>instance Error LengthError where <a name="line-74"></a>> noMsg = OtherError "A String Error!" <a name="line-75"></a>> strMsg s = OtherError s <a name="line-76"></a>> <a name="line-77"></a>>-- Converts LengthError to a readable message. <a name="line-78"></a>>instance Show LengthError where <a name="line-79"></a>> show EmptyString = "The string was empty!" <a name="line-80"></a>> show (StringTooLong len) = <a name="line-81"></a>> "The length of the string (" ++ (show len) ++ ") is bigger than 5!" <a name="line-82"></a>> show (OtherError msg) = msg <a name="line-83"></a>> <a name="line-84"></a>>-- For our monad type constructor, we use Either LengthError <a name="line-85"></a>>-- which represents failure using Left LengthError <a name="line-86"></a>>-- or a successful result of type a using Right a. <a name="line-87"></a>>type LengthMonad = Either LengthError <a name="line-88"></a>> <a name="line-89"></a>>main = do <a name="line-90"></a>> putStrLn "Please enter a string:" <a name="line-91"></a>> s <- getLine <a name="line-92"></a>> reportResult (calculateLength s) <a name="line-93"></a>> <a name="line-94"></a>>-- Wraps length calculation to catch the errors. <a name="line-95"></a>>-- Returns either length of the string or an error. <a name="line-96"></a>>calculateLength :: String -> LengthMonad Int <a name="line-97"></a>>calculateLength s = (calculateLengthOrFail s) `catchError` Left <a name="line-98"></a>> <a name="line-99"></a>>-- Attempts to calculate length and throws an error if the provided string is <a name="line-100"></a>>-- empty or longer than 5 characters. <a name="line-101"></a>>-- The processing is done in Either monad. <a name="line-102"></a>>calculateLengthOrFail :: String -> LengthMonad Int <a name="line-103"></a>>calculateLengthOrFail [] = throwError EmptyString <a name="line-104"></a>>calculateLengthOrFail s | len > 5 = throwError (StringTooLong len) <a name="line-105"></a>> | otherwise = return len <a name="line-106"></a>> where len = length s <a name="line-107"></a>> <a name="line-108"></a>>-- Prints result of the string length calculation. <a name="line-109"></a>>reportResult :: LengthMonad Int -> IO () <a name="line-110"></a>>reportResult (Right len) = putStrLn ("The length of the string is " ++ (show len)) <a name="line-111"></a>>reportResult (Left e) = putStrLn ("Length calculation failed with error: " ++ (show e)) <a name="line-112"></a>-}</span> <a name="line-113"></a> <a name="line-114"></a><span class='hs-comment'>{- $ErrorTExample <a name="line-115"></a>@'ErrorT'@ monad transformer can be used to add error handling to another monad. <a name="line-116"></a>Here is an example how to combine it with an @IO@ monad: <a name="line-117"></a> <a name="line-118"></a>>import Control.Monad.Error <a name="line-119"></a>> <a name="line-120"></a>>-- An IO monad which can return String failure. <a name="line-121"></a>>-- It is convenient to define the monad type of the combined monad, <a name="line-122"></a>>-- especially if we combine more monad transformers. <a name="line-123"></a>>type LengthMonad = ErrorT String IO <a name="line-124"></a>> <a name="line-125"></a>>main = do <a name="line-126"></a>> -- runErrorT removes the ErrorT wrapper <a name="line-127"></a>> r <- runErrorT calculateLength <a name="line-128"></a>> reportResult r <a name="line-129"></a>> <a name="line-130"></a>>-- Asks user for a non-empty string and returns its length. <a name="line-131"></a>>-- Throws an error if user enters an empty string. <a name="line-132"></a>>calculateLength :: LengthMonad Int <a name="line-133"></a>>calculateLength = do <a name="line-134"></a>> -- all the IO operations have to be lifted to the IO monad in the monad stack <a name="line-135"></a>> liftIO $ putStrLn "Please enter a non-empty string: " <a name="line-136"></a>> s <- liftIO getLine <a name="line-137"></a>> if null s <a name="line-138"></a>> then throwError "The string was empty!" <a name="line-139"></a>> else return $ length s <a name="line-140"></a>> <a name="line-141"></a>>-- Prints result of the string length calculation. <a name="line-142"></a>>reportResult :: Either String Int -> IO () <a name="line-143"></a>>reportResult (Right len) = putStrLn ("The length of the string is " ++ (show len)) <a name="line-144"></a>>reportResult (Left e) = putStrLn ("Length calculation failed with error: " ++ (show e)) <a name="line-145"></a>-}</span> </pre></body> </html>