Sophie

Sophie

distrib > Fedora > 15 > i386 > by-pkgid > 6e7b8b29c9973053a0fbeb2899ecd9cb > files > 157

ghc-mtl-devel-2.0.1.0-8.fc15.i686.rpm

<?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 &lt;michael.weber@post.rwth-aachen.de&gt; 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 &lt;mailto:michael.weber@post.rwth-aachen.de&gt;,
<a name="line-31"></a>  inspired by the Haskell Monad Template Library from
<a name="line-32"></a>    Andy Gill (&lt;<a href="http://web.cecs.pdx.edu/~andy/">http://web.cecs.pdx.edu/~andy/</a>&gt;)
<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>&gt;-- This is the type to represent length calculation error.
<a name="line-66"></a>&gt;data LengthError = EmptyString  -- Entered string was empty.
<a name="line-67"></a>&gt;          | StringTooLong Int   -- A string is longer than 5 characters.
<a name="line-68"></a>&gt;                                -- Records a length of the string.
<a name="line-69"></a>&gt;          | OtherError String   -- Other error, stores the problem description.
<a name="line-70"></a>&gt;
<a name="line-71"></a>&gt;-- We make LengthError an instance of the Error class
<a name="line-72"></a>&gt;-- to be able to throw it as an exception.
<a name="line-73"></a>&gt;instance Error LengthError where
<a name="line-74"></a>&gt;  noMsg    = OtherError "A String Error!"
<a name="line-75"></a>&gt;  strMsg s = OtherError s
<a name="line-76"></a>&gt;
<a name="line-77"></a>&gt;-- Converts LengthError to a readable message.
<a name="line-78"></a>&gt;instance Show LengthError where
<a name="line-79"></a>&gt;  show EmptyString = "The string was empty!"
<a name="line-80"></a>&gt;  show (StringTooLong len) =
<a name="line-81"></a>&gt;      "The length of the string (" ++ (show len) ++ ") is bigger than 5!"
<a name="line-82"></a>&gt;  show (OtherError msg) = msg
<a name="line-83"></a>&gt;
<a name="line-84"></a>&gt;-- For our monad type constructor, we use Either LengthError
<a name="line-85"></a>&gt;-- which represents failure using Left LengthError
<a name="line-86"></a>&gt;-- or a successful result of type a using Right a.
<a name="line-87"></a>&gt;type LengthMonad = Either LengthError
<a name="line-88"></a>&gt;
<a name="line-89"></a>&gt;main = do
<a name="line-90"></a>&gt;  putStrLn "Please enter a string:"
<a name="line-91"></a>&gt;  s &lt;- getLine
<a name="line-92"></a>&gt;  reportResult (calculateLength s)
<a name="line-93"></a>&gt;
<a name="line-94"></a>&gt;-- Wraps length calculation to catch the errors.
<a name="line-95"></a>&gt;-- Returns either length of the string or an error.
<a name="line-96"></a>&gt;calculateLength :: String -&gt; LengthMonad Int
<a name="line-97"></a>&gt;calculateLength s = (calculateLengthOrFail s) `catchError` Left
<a name="line-98"></a>&gt;
<a name="line-99"></a>&gt;-- Attempts to calculate length and throws an error if the provided string is
<a name="line-100"></a>&gt;-- empty or longer than 5 characters.
<a name="line-101"></a>&gt;-- The processing is done in Either monad.
<a name="line-102"></a>&gt;calculateLengthOrFail :: String -&gt; LengthMonad Int
<a name="line-103"></a>&gt;calculateLengthOrFail [] = throwError EmptyString
<a name="line-104"></a>&gt;calculateLengthOrFail s | len &gt; 5 = throwError (StringTooLong len)
<a name="line-105"></a>&gt;                        | otherwise = return len
<a name="line-106"></a>&gt;  where len = length s
<a name="line-107"></a>&gt;
<a name="line-108"></a>&gt;-- Prints result of the string length calculation.
<a name="line-109"></a>&gt;reportResult :: LengthMonad Int -&gt; IO ()
<a name="line-110"></a>&gt;reportResult (Right len) = putStrLn ("The length of the string is " ++ (show len))
<a name="line-111"></a>&gt;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>&gt;import Control.Monad.Error
<a name="line-119"></a>&gt;
<a name="line-120"></a>&gt;-- An IO monad which can return String failure.
<a name="line-121"></a>&gt;-- It is convenient to define the monad type of the combined monad,
<a name="line-122"></a>&gt;-- especially if we combine more monad transformers.
<a name="line-123"></a>&gt;type LengthMonad = ErrorT String IO
<a name="line-124"></a>&gt;
<a name="line-125"></a>&gt;main = do
<a name="line-126"></a>&gt;  -- runErrorT removes the ErrorT wrapper
<a name="line-127"></a>&gt;  r &lt;- runErrorT calculateLength
<a name="line-128"></a>&gt;  reportResult r
<a name="line-129"></a>&gt;
<a name="line-130"></a>&gt;-- Asks user for a non-empty string and returns its length.
<a name="line-131"></a>&gt;-- Throws an error if user enters an empty string.
<a name="line-132"></a>&gt;calculateLength :: LengthMonad Int
<a name="line-133"></a>&gt;calculateLength = do
<a name="line-134"></a>&gt;  -- all the IO operations have to be lifted to the IO monad in the monad stack
<a name="line-135"></a>&gt;  liftIO $ putStrLn "Please enter a non-empty string: "
<a name="line-136"></a>&gt;  s &lt;- liftIO getLine
<a name="line-137"></a>&gt;  if null s
<a name="line-138"></a>&gt;    then throwError "The string was empty!"
<a name="line-139"></a>&gt;    else return $ length s
<a name="line-140"></a>&gt;
<a name="line-141"></a>&gt;-- Prints result of the string length calculation.
<a name="line-142"></a>&gt;reportResult :: Either String Int -&gt; IO ()
<a name="line-143"></a>&gt;reportResult (Right len) = putStrLn ("The length of the string is " ++ (show len))
<a name="line-144"></a>&gt;reportResult (Left e) = putStrLn ("Length calculation failed with error: " ++ (show e))
<a name="line-145"></a>-}</span>
</pre></body>
</html>