/usr/share/doc/libghc-monads-tf-doc/html/src/Control-Monad-Reader.html is in libghc-monads-tf-doc 0.1.0.3-3build1.
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 | <?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/Reader.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.Reader
<a name="line-3"></a>Copyright : (c) Andy Gill 2001,
<a name="line-4"></a> (c) Oregon Graduate Institute of Science and Technology 2001,
<a name="line-5"></a> (c) Jeff Newbern 2003-2007,
<a name="line-6"></a> (c) Andriy Palamarchuk 2007
<a name="line-7"></a>License : BSD-style (see the file LICENSE)
<a name="line-8"></a>
<a name="line-9"></a>Maintainer : ross@soi.city.ac.uk
<a name="line-10"></a>Stability : experimental
<a name="line-11"></a>Portability : non-portable (type families)
<a name="line-12"></a>
<a name="line-13"></a>[Computation type:] Computations which read values from a shared environment.
<a name="line-14"></a>
<a name="line-15"></a>[Binding strategy:] Monad values are functions from the environment to a value.
<a name="line-16"></a>The bound function is applied to the bound value, and both have access
<a name="line-17"></a>to the shared environment.
<a name="line-18"></a>
<a name="line-19"></a>[Useful for:] Maintaining variable bindings, or other shared environment.
<a name="line-20"></a>
<a name="line-21"></a>[Zero and plus:] None.
<a name="line-22"></a>
<a name="line-23"></a>[Example type:] @'Reader' [(String,Value)] a@
<a name="line-24"></a>
<a name="line-25"></a>The 'Reader' monad (also called the Environment monad).
<a name="line-26"></a>Represents a computation, which can read values from
<a name="line-27"></a>a shared environment, pass values from function to function,
<a name="line-28"></a>and execute sub-computations in a modified environment.
<a name="line-29"></a>Using 'Reader' monad for such computations is often clearer and easier
<a name="line-30"></a>than using the 'Control.Monad.State.State' monad.
<a name="line-31"></a>
<a name="line-32"></a> Inspired by the paper
<a name="line-33"></a> /Functional Programming with Overloading and
<a name="line-34"></a> Higher-Order Polymorphism/,
<a name="line-35"></a> Mark P Jones (<<a href="http://web.cecs.pdx.edu/~mpj/">http://web.cecs.pdx.edu/~mpj/</a>>)
<a name="line-36"></a> Advanced School of Functional Programming, 1995.
<a name="line-37"></a>-}</span>
<a name="line-38"></a>
<a name="line-39"></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'>Reader</span> <span class='hs-layout'>(</span>
<a name="line-40"></a> <span class='hs-comment'>-- * MonadReader class</span>
<a name="line-41"></a> <span class='hs-conid'>MonadReader</span><span class='hs-layout'>(</span><span class='hs-keyglyph'>..</span><span class='hs-layout'>)</span><span class='hs-layout'>,</span>
<a name="line-42"></a> <span class='hs-varid'>asks</span><span class='hs-layout'>,</span>
<a name="line-43"></a> <span class='hs-comment'>-- * The Reader monad</span>
<a name="line-44"></a> <span class='hs-conid'>Reader</span><span class='hs-layout'>,</span>
<a name="line-45"></a> <span class='hs-varid'>runReader</span><span class='hs-layout'>,</span>
<a name="line-46"></a> <span class='hs-varid'>mapReader</span><span class='hs-layout'>,</span>
<a name="line-47"></a> <span class='hs-varid'>withReader</span><span class='hs-layout'>,</span>
<a name="line-48"></a> <span class='hs-comment'>-- * The ReaderT monad transformer</span>
<a name="line-49"></a> <span class='hs-conid'>ReaderT</span><span class='hs-layout'>(</span><span class='hs-keyglyph'>..</span><span class='hs-layout'>)</span><span class='hs-layout'>,</span>
<a name="line-50"></a> <span class='hs-varid'>mapReaderT</span><span class='hs-layout'>,</span>
<a name="line-51"></a> <span class='hs-varid'>withReaderT</span><span class='hs-layout'>,</span>
<a name="line-52"></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-53"></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-54"></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-55"></a> <span class='hs-comment'>-- * Example 1: Simple Reader Usage</span>
<a name="line-56"></a> <span class='hs-comment'>-- $simpleReaderExample</span>
<a name="line-57"></a>
<a name="line-58"></a> <span class='hs-comment'>-- * Example 2: Modifying Reader Content With @local@</span>
<a name="line-59"></a> <span class='hs-comment'>-- $localExample</span>
<a name="line-60"></a>
<a name="line-61"></a> <span class='hs-comment'>-- * Example 3: @ReaderT@ Monad Transformer</span>
<a name="line-62"></a> <span class='hs-comment'>-- $ReaderTExample</span>
<a name="line-63"></a> <span class='hs-layout'>)</span> <span class='hs-keyword'>where</span>
<a name="line-64"></a>
<a name="line-65"></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'>Reader</span><span class='hs-varop'>.</span><span class='hs-conid'>Class</span>
<a name="line-66"></a>
<a name="line-67"></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'>Reader</span> <span class='hs-layout'>(</span>
<a name="line-68"></a> <span class='hs-conid'>Reader</span><span class='hs-layout'>,</span> <span class='hs-varid'>runReader</span><span class='hs-layout'>,</span> <span class='hs-varid'>mapReader</span><span class='hs-layout'>,</span> <span class='hs-varid'>withReader</span><span class='hs-layout'>,</span>
<a name="line-69"></a> <span class='hs-conid'>ReaderT</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'>mapReaderT</span><span class='hs-layout'>,</span> <span class='hs-varid'>withReaderT</span><span class='hs-layout'>)</span>
<a name="line-70"></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-71"></a>
<a name="line-72"></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-73"></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-74"></a>
<a name="line-75"></a><span class='hs-comment'>{- $simpleReaderExample
<a name="line-76"></a>
<a name="line-77"></a>In this example the @Reader@ monad provides access to variable bindings.
<a name="line-78"></a>Bindings are a @Map@ of integer variables.
<a name="line-79"></a>The variable @count@ contains number of variables in the bindings.
<a name="line-80"></a>You can see how to run a Reader monad and retrieve data from it
<a name="line-81"></a>with 'runReader', how to access the Reader data with 'ask' and 'asks'.
<a name="line-82"></a>
<a name="line-83"></a>> type Bindings = Map String Int;
<a name="line-84"></a>>
<a name="line-85"></a>>-- Returns True if the "count" variable contains correct bindings size.
<a name="line-86"></a>>isCountCorrect :: Bindings -> Bool
<a name="line-87"></a>>isCountCorrect bindings = runReader calc_isCountCorrect bindings
<a name="line-88"></a>>
<a name="line-89"></a>>-- The Reader monad, which implements this complicated check.
<a name="line-90"></a>>calc_isCountCorrect :: Reader Bindings Bool
<a name="line-91"></a>>calc_isCountCorrect = do
<a name="line-92"></a>> count <- asks (lookupVar "count")
<a name="line-93"></a>> bindings <- ask
<a name="line-94"></a>> return (count == (Map.size bindings))
<a name="line-95"></a>>
<a name="line-96"></a>>-- The selector function to use with 'asks'.
<a name="line-97"></a>>-- Returns value of the variable with specified name.
<a name="line-98"></a>>lookupVar :: String -> Bindings -> Int
<a name="line-99"></a>>lookupVar name bindings = fromJust (Map.lookup name bindings)
<a name="line-100"></a>>
<a name="line-101"></a>>sampleBindings = Map.fromList [("count",3), ("1",1), ("b",2)]
<a name="line-102"></a>>
<a name="line-103"></a>>main = do
<a name="line-104"></a>> putStr $ "Count is correct for bindings " ++ (show sampleBindings) ++ ": ";
<a name="line-105"></a>> putStrLn $ show (isCountCorrect sampleBindings);
<a name="line-106"></a>-}</span>
<a name="line-107"></a>
<a name="line-108"></a><span class='hs-comment'>{- $localExample
<a name="line-109"></a>
<a name="line-110"></a>Shows how to modify Reader content with 'local'.
<a name="line-111"></a>
<a name="line-112"></a>>calculateContentLen :: Reader String Int
<a name="line-113"></a>>calculateContentLen = do
<a name="line-114"></a>> content <- ask
<a name="line-115"></a>> return (length content);
<a name="line-116"></a>>
<a name="line-117"></a>>-- Calls calculateContentLen after adding a prefix to the Reader content.
<a name="line-118"></a>>calculateModifiedContentLen :: Reader String Int
<a name="line-119"></a>>calculateModifiedContentLen = local ("Prefix " ++) calculateContentLen
<a name="line-120"></a>>
<a name="line-121"></a>>main = do
<a name="line-122"></a>> let s = "12345";
<a name="line-123"></a>> let modifiedLen = runReader calculateModifiedContentLen s
<a name="line-124"></a>> let len = runReader calculateContentLen s
<a name="line-125"></a>> putStrLn $ "Modified 's' length: " ++ (show modifiedLen)
<a name="line-126"></a>> putStrLn $ "Original 's' length: " ++ (show len)
<a name="line-127"></a>-}</span>
<a name="line-128"></a>
<a name="line-129"></a><span class='hs-comment'>{- $ReaderTExample
<a name="line-130"></a>
<a name="line-131"></a>Now you are thinking: 'Wow, what a great monad! I wish I could use
<a name="line-132"></a>Reader functionality in MyFavoriteComplexMonad!'. Don't worry.
<a name="line-133"></a>This can be easy done with the 'ReaderT' monad transformer.
<a name="line-134"></a>This example shows how to combine @ReaderT@ with the IO monad.
<a name="line-135"></a>
<a name="line-136"></a>>-- The Reader/IO combined monad, where Reader stores a string.
<a name="line-137"></a>>printReaderContent :: ReaderT String IO ()
<a name="line-138"></a>>printReaderContent = do
<a name="line-139"></a>> content <- ask
<a name="line-140"></a>> liftIO $ putStrLn ("The Reader Content: " ++ content)
<a name="line-141"></a>>
<a name="line-142"></a>>main = do
<a name="line-143"></a>> runReaderT printReaderContent "Some Content"
<a name="line-144"></a>-}</span>
</pre></body>
</html>
|