This file is indexed.

/usr/share/doc/libghc-mtl-doc/html/src/Control-Monad-Error.html is in libghc-mtl-doc 2.2.1-5.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

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