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 163 164 165 166 167 168 169 170 171 172
|
<title>The Haskell 1.3 Library Report: Monad Utilities</title>
<body bgcolor="#ffffff"> <i>The Haskell 1.4 Library Report</i><br> <a href="index.html">top</a> | <a href="char.html">back</a> | <a href="io.html">next</a> | <a href="libindex.html">contents</a> <br><hr>
<a name="Monad"></a><a name="sect10"></a>
<h2>10<tt> </tt>Monad Utilities</h2>
<p>
<table border=2 cellpadding=3>
<tr><td>
<tt><br>
module Monad (<br>
join, mapAndUnzipM, zipWithM, foldM, when, unless, ap,<br>
liftM, liftM2, liftM3, liftM4, liftM5<br>
) where<br>
<br>
join :: (Monad m) => m (m a) -> m a<br>
mapAndUnzipM :: (Monad m) => (a -> m (b,c)) -> [a] -> m ([b], [c])<br>
zipWithM :: (Monad m) => (a -> b -> m c) -> [a] -> [b] -> m [c]<br>
foldM :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m a<br>
zeroOrMore :: (MonadPlus m) => m a -> m [a]<br>
oneOrMore :: (MonadPlus m) => m a -> m [a]<br>
when :: (Monad m) => Bool -> m () -> m ()<br>
unless :: (Monad m) => Bool -> m () -> m ()<br>
ap :: (Monad a) => (m (a -> b)) -> (m a) -> m b<br>
liftM :: (Monad m) => (a -> b) -> (m a -> m b)<br>
liftM2 :: (Monad m) => (a -> b -> c) -> (m a -> m b -> m c)<br>
liftM3 :: (Monad m) => (a -> b -> c -> d) -><br>
(m a -> m b -> m c -> m d)<br>
liftM4 :: (Monad m) => (a -> b -> c -> d -> e) -><br>
(m a -> m b -> m c -> m d -> m e)<br>
liftM5 :: (Monad m) => (a -> b -> c -> d -> e -> f) -><br>
(m a -> m b -> m c -> m d -> m e -> m f)<br>
<br>
</tt></td></tr></table>
<p>
These utilities provide some useful operations on monads.<p>
The <tt>join</tt> function is the conventional monad join operator. It is
used to remove one level of monadic structure, projecting its bound
argument into the outer level.<p>
The <tt>mapAndUnzipM</tt> function maps its first argument over a list,
returning the result as a pair of lists. This function is mainly used
with complicated data structures or a state-transforming monad.<p>
The <tt>zipWithM</tt> function generalises <tt>zipWith</tt> to arbitrary monads.
For instance the following function displays a file, prefixing
each line with its line number,
<tt><br>
<br>
listFile :: String -> IO ()<br>
listFile nm =<br>
do cts <- openFile nm<br>
zipWithM_ (\i line -> do putStr (show i); putStr ": "; putStrLn line)<br>
[1..]<br>
(lines cts)<br>
<br>
<p>
</tt>The <tt>foldM</tt> function is analogous to <tt>foldl</tt>, except that its result
is encapsulated in a monad. Note that <tt>foldM</tt> works from
left-to-right over the list arguments. This could be an issue where
<tt>(>>)</tt> and the "folded function" are not commutative.
<tt><br>
<br>
foldM f a1 [x1, x2, ..., xm ]<br>
== <br>
do<br>
a2 <- f a1 x1<br>
a3 <- f a2 x2<br>
...<br>
f am xm<br>
<br>
</tt>If right-to-left
evaluation is required, the input list should be reversed.<p>
The <tt>when</tt> and <tt>unless</tt> functions provide conditional execution of
monadic expressions. For example,
<tt><br>
<br>
when debug (putStr "Debugging\n")<br>
<br>
</tt>will output the string <tt>"Debugging\n"</tt> if the Boolean value <tt>debug</tt> is
<tt>True</tt>, and otherwise do nothing.<p>
The monadic lifting operators promote a function to a monad. The
function arguments are scanned left to right. For example,
<tt><br>
<br>
liftM2 (+) [0,1] [0,2] = [0,2,1,3]<br>
liftM2 (+) (Just 1) Nothing = Nothing<br>
<br>
<p>
</tt>In many situations, the liftM operations can be replaced by uses
of <tt>ap</tt>, which promotes function application.
<tt><br>
<br>
return f `ap` x1 `ap` ... `ap` xn<br>
<br>
</tt>is equivalent to
<tt><br>
<br>
liftMn f x1 x2 ... xn<br>
<br>
<p>
</tt><a name="sect10.1"></a>
<h3>10.1<tt> </tt>Library <tt>Monad</tt></h3>
<tt><br>
module Monad (<br>
join, mapAndUnzipM, zipWithM, zipWithM_, foldM, when, unless, ap,<br>
liftM, liftM2, liftM3, liftM4, liftM5<br>
) where<br>
<br>
join :: (Monad m) => m (m a) -> m a<br>
join x = x >>= id<br>
<br>
mapAndUnzipM :: (Monad m) => (a -> m (b,c)) -> [a] -> m ([b], [c])<br>
mapAndUnzipM f xs = accumulate (map f xs) >>= return . unzip<br>
<br>
zipWithM :: (Monad m) => (a -> b -> m c) -> [a] -> [b] -> m [c]<br>
zipWithM f xs ys = accumulate (zipWith f xs ys)<br>
<br>
zipWithM_ :: (Monad m) => (a -> b -> m c) -> [a] -> [b] -> m ()<br>
zipWithM_ f xs ys = sequence (zipWith f xs ys)<br>
<br>
foldM :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m a<br>
foldM f a [] = return a<br>
foldM f a (x:xs) = f a x >>= \ y -> foldM f y xs<br>
<br>
when :: (Monad m) => Bool -> m () -> m ()<br>
when p s = if p then s else return ()<br>
<br>
unless :: (Monad m) => Bool -> m () -> m ()<br>
unless p s = when (not p) s<br>
<br>
ap :: (Monad m) => m (a -> b) -> m a -> m b<br>
ap = liftM2 ($)<br>
<br>
liftM :: (Monad m) => (a -> b) -> (m a -> m b)<br>
liftM f = \a -> [f a' | a' <- a]<br>
<br>
liftM2 :: (Monad m) => (a -> b -> c) -> (m a -> m b -> m c)<br>
liftM2 f = \a b -> [f a' b' | a' <- a, b' <- b] <br>
<br>
liftM3 :: (Monad m) => (a -> b -> c -> d) -><br>
(m a -> m b -> m c -> m d)<br>
liftM3 f = \a b c -> [f a' b' c' | a' <- a, b' <- b, c' <- c] <br>
<br>
liftM4 :: (Monad m) => (a -> b -> c -> d -> e) -><br>
(m a -> m b -> m c -> m d -> m e)<br>
liftM4 f = \a b c d -> [f a' b' c' d' |<br>
a' <- a, b' <- b, c' <- c, d' <- d] <br>
<br>
liftM5 :: (Monad m) => (a -> b -> c -> d -> e -> f) -><br>
(m a -> m b -> m c -> m d -> m e -> m f)<br>
liftM5 f = \a b c d e -> [f a' b' c' d' e' |<br>
a' <- a, b' <- b,<br>
c' <- c, d' <- d, e <- e']<br>
<p>
<hr><i>The Haskell 1.4 Library Report</i><br><a href="index.html">top</a> | <a href="char.html">back</a> | <a href="io.html">next</a> | <a href="libindex.html">contents</a> <br><font size=2>April 4, 1997</font>
<p>
</tt>
|