Ticket #1127: proposal-1127-3.patch

File proposal-1127-3.patch, 106.6 KB (added by igloo, 7 years ago)
Line 
1-----BEGIN PGP SIGNED MESSAGE-----
2Hash: SHA1
3
4
5New patches:
6
7[Rejig mtl; trac proposal #1127
8Ian Lynagh <igloo@earth.li>**20070205000253
9 
10 Moved the MonadFoo classes to Control.Monad.Foo.Class.
11 
12 Put the instances consistently in the module which defines the type they
13 give an instance for.
14 
15 Made the existing transformers lazy to match the existing monads, and put
16 them in Control.Monad.Foo.Lazy. Also added Control.Monad.Foo.Strict with
17 strict monads and transformers. Control.Monad.Foo still exports what it
18 used to.
19 
20 Created a MonadRWS class.
21 
22 Made the MonadWriter w (ErrorT e m) instance strict to match everything
23 else.
24 
25] {
26adddir ./Control/Monad/Cont
27adddir ./Control/Monad/Error
28adddir ./Control/Monad/RWS
29adddir ./Control/Monad/Reader
30adddir ./Control/Monad/State
31adddir ./Control/Monad/Writer
32addfile ./Control/Monad/Cont/Class.hs
33addfile ./Control/Monad/Error/Class.hs
34addfile ./Control/Monad/RWS/Class.hs
35addfile ./Control/Monad/RWS/Lazy.hs
36addfile ./Control/Monad/RWS/Strict.hs
37addfile ./Control/Monad/Reader/Class.hs
38addfile ./Control/Monad/State/Class.hs
39addfile ./Control/Monad/State/Lazy.hs
40addfile ./Control/Monad/State/Strict.hs
41addfile ./Control/Monad/Writer/Class.hs
42addfile ./Control/Monad/Writer/Lazy.hs
43addfile ./Control/Monad/Writer/Strict.hs
44hunk ./Control/Monad/Cont/Class.hs 1
45+{-# OPTIONS -fallow-undecidable-instances #-}
46+-- Search for -fallow-undecidable-instances to see why this is needed
47+
48+-----------------------------------------------------------------------------
49+-- |
50+-- Module      :  Control.Monad.Cont.Class
51+-- Copyright   :  (c) The University of Glasgow 2001
52+-- License     :  BSD-style (see the file libraries/base/LICENSE)
53+--
54+-- Maintainer  :  libraries@haskell.org
55+-- Stability   :  experimental
56+-- Portability :  non-portable (multi-parameter type classes)
57+--
58+-- Continuation monad class
59+--
60+-----------------------------------------------------------------------------
61+
62+module Control.Monad.Cont.Class (
63+    MonadCont(..),
64+  ) where
65+
66+class (Monad m) => MonadCont m where
67+    callCC :: ((a -> m b) -> m a) -> m a
68+
69hunk ./Control/Monad/Cont.hs 9
70- ---
71+--
72hunk ./Control/Monad/Cont.hs 19
73- -     MonadCont(..),
74- -     Cont(..),
75- -     mapCont,
76- -     withCont,
77- -     ContT(..),
78- -     mapContT,
79- -     withContT,
80- -     module Control.Monad,
81- -     module Control.Monad.Trans,
82+    module Control.Monad.Cont.Class,
83+    Cont(..),
84+    mapCont,
85+    withCont,
86+    ContT(..),
87+    mapContT,
88+    withContT,
89+    module Control.Monad,
90+    module Control.Monad.Trans,
91hunk ./Control/Monad/Cont.hs 33
92+import Control.Monad.Cont.Class
93+import Control.Monad.RWS.Class
94+import Control.Monad.Reader.Class
95+import Control.Monad.State.Class
96hunk ./Control/Monad/Cont.hs 38
97- -import Control.Monad.Reader
98- -import Control.Monad.Writer
99- -import Control.Monad.State
100- -import Control.Monad.RWS
101- -
102- -class (Monad m) => MonadCont m where
103- -     callCC :: ((a -> m b) -> m a) -> m a
104hunk ./Control/Monad/Cont.hs 44
105- -instance Functor (Cont r) where
106- -     fmap f m = Cont $ \c -> runCont m (c . f)
107- -
108- -instance Monad (Cont r) where
109- -     return a = Cont ($ a)
110- -     m >>= k  = Cont $ \c -> runCont m $ \a -> runCont (k a) c
111- -
112- -instance MonadCont (Cont r) where
113- -     callCC f = Cont $ \c -> runCont (f (\a -> Cont $ \_ -> c a)) c
114- -
115hunk ./Control/Monad/Cont.hs 50
116+instance Functor (Cont r) where
117+    fmap f m = Cont $ \c -> runCont m (c . f)
118+
119+instance Monad (Cont r) where
120+    return a = Cont ($ a)
121+    m >>= k  = Cont $ \c -> runCont m $ \a -> runCont (k a) c
122+
123+instance MonadCont (Cont r) where
124+    callCC f = Cont $ \c -> runCont (f (\a -> Cont $ \_ -> c a)) c
125+
126hunk ./Control/Monad/Cont.hs 65
127+mapContT :: (m r -> m r) -> ContT r m a -> ContT r m a
128+mapContT f m = ContT $ f . runContT m
129+
130+withContT :: ((b -> m r) -> (a -> m r)) -> ContT r m a -> ContT r m b
131+withContT f m = ContT $ runContT m . f
132+
133hunk ./Control/Monad/Cont.hs 72
134- -     fmap f m = ContT $ \c -> runContT m (c . f)
135+    fmap f m = ContT $ \c -> runContT m (c . f)
136hunk ./Control/Monad/Cont.hs 75
137- -     return a = ContT ($ a)
138- -     m >>= k  = ContT $ \c -> runContT m (\a -> runContT (k a) c)
139+    return a = ContT ($ a)
140+    m >>= k  = ContT $ \c -> runContT m (\a -> runContT (k a) c)
141hunk ./Control/Monad/Cont.hs 79
142- -     callCC f = ContT $ \c -> runContT (f (\a -> ContT $ \_ -> c a)) c
143+    callCC f = ContT $ \c -> runContT (f (\a -> ContT $ \_ -> c a)) c
144+
145+-- ---------------------------------------------------------------------------
146+-- Instances for other mtl transformers
147hunk ./Control/Monad/Cont.hs 85
148- -     lift m = ContT (m >>=)
149+    lift m = ContT (m >>=)
150hunk ./Control/Monad/Cont.hs 88
151- -     liftIO = lift . liftIO
152+    liftIO = lift . liftIO
153hunk ./Control/Monad/Cont.hs 92
154- -     ask       = lift ask
155- -     local f m = ContT $ \c -> do
156- -             r <- ask
157- -             local f (runContT m (local (const r) . c))
158+    ask       = lift ask
159+    local f m = ContT $ \c -> do
160+        r <- ask
161+        local f (runContT m (local (const r) . c))
162hunk ./Control/Monad/Cont.hs 99
163- -     get = lift get
164- -     put = lift . put
165- -
166- --- -----------------------------------------------------------------------------
167- --- MonadCont instances for other monad transformers
168- -
169- -instance (MonadCont m) => MonadCont (ReaderT r m) where
170- -     callCC f = ReaderT $ \r ->
171- -             callCC $ \c ->
172- -             runReaderT (f (\a -> ReaderT $ \_ -> c a)) r
173+    get = lift get
174+    put = lift . put
175hunk ./Control/Monad/Cont.hs 102
176- -instance (MonadCont m) => MonadCont (StateT s m) where
177- -     callCC f = StateT $ \s ->
178- -             callCC $ \c ->
179- -             runStateT (f (\a -> StateT $ \s' -> c (a, s'))) s
180- -
181- -instance (Monoid w, MonadCont m) => MonadCont (WriterT w m) where
182- -     callCC f = WriterT $
183- -             callCC $ \c ->
184- -             runWriterT (f (\a -> WriterT $ c (a, mempty)))
185- -
186- -instance (Monoid w, MonadCont m) => MonadCont (RWST r w s m) where
187- -     callCC f = RWST $ \r s ->
188- -             callCC $ \c ->
189- -             runRWST (f (\a -> RWST $ \_ s' -> c (a, s', mempty))) r s
190- -
191- -mapContT :: (m r -> m r) -> ContT r m a -> ContT r m a
192- -mapContT f m = ContT $ f . runContT m
193- -
194- -withContT :: ((b -> m r) -> (a -> m r)) -> ContT r m a -> ContT r m b
195- -withContT f m = ContT $ runContT m . f
196hunk ./Control/Monad/Error/Class.hs 1
197+{-# OPTIONS -fallow-undecidable-instances #-}
198+-- Needed for the same reasons as in Reader, State etc
199+
200+{- |
201+Module      :  Control.Monad.Error.Class
202+Copyright   :  (c) Michael Weber <michael.weber@post.rwth-aachen.de> 2001,
203+               (c) Jeff Newbern 2003-2006,
204+               (c) Andriy Palamarchuk 2006
205+License     :  BSD-style (see the file libraries/base/LICENSE)
206+
207+Maintainer  :  libraries@haskell.org
208+Stability   :  experimental
209+Portability :  non-portable (multi-parameter type classes)
210+
211+[Computation type:] Computations which may fail or throw exceptions.
212+
213+[Binding strategy:] Failure records information about the cause\/location
214+of the failure. Failure values bypass the bound function,
215+other values are used as inputs to the bound function.
216+
217+[Useful for:] Building computations from sequences of functions that may fail
218+or using exception handling to structure error handling.
219+
220+[Zero and plus:] Zero is represented by an empty error and the plus operation
221+executes its second argument if the first fails.
222+
223+[Example type:] @'Data.Either' String a@
224+
225+The Error monad (also called the Exception monad).
226+-}
227+
228+{-
229+  Rendered by Michael Weber <mailto:michael.weber@post.rwth-aachen.de>,
230+  inspired by the Haskell Monad Template Library from
231+    Andy Gill (<http://www.cse.ogi.edu/~andy/>)
232+-}
233+module Control.Monad.Error.Class (
234+    Error(..),
235+    MonadError(..),
236+  ) where
237+
238+import Prelude
239+
240+-- | An exception to be thrown.
241+-- An instance must redefine at least one of 'noMsg', 'strMsg'.
242+class Error a where
243+    -- | Creates an exception without a message.
244+    -- Default implementation is @'strMsg' \"\"@.
245+    noMsg  :: a
246+    -- | Creates an exception with a message.
247+    -- Default implementation is 'noMsg'.
248+    strMsg :: String -> a
249+
250+    noMsg    = strMsg ""
251+    strMsg _ = noMsg
252+
253+-- | A string can be thrown as an error.
254+instance Error String where
255+    noMsg  = ""
256+    strMsg = id
257+
258+instance Error IOError where
259+    strMsg = userError
260+
261+{- |
262+The strategy of combining computations that can throw exceptions
263+by bypassing bound functions
264+from the point an exception is thrown to the point that it is handled.
265+
266+Is parameterized over the type of error information and
267+the monad type constructor.
268+It is common to use @'Data.Either' String@ as the monad type constructor
269+for an error monad in which error descriptions take the form of strings.
270+In that case and many other common cases the resulting monad is already defined
271+as an instance of the 'MonadError' class.
272+You can also define your own error type and\/or use a monad type constructor
273+other than @'Data.Either' String@ or @'Data.Either' IOError@.
274+In these cases you will have to explicitly define instances of the 'Error'
275+and\/or 'MonadError' classes.
276+-}
277+class (Monad m) => MonadError e m | m -> e where
278+    -- | Is used within a monadic computation to begin exception processing.
279+    throwError :: e -> m a
280+
281+    {- |
282+    A handler function to handle previous errors and return to normal execution.
283+    A common idiom is:
284+
285+    > do { action1; action2; action3 } `catchError` handler
286+
287+    where the @action@ functions can call 'throwError'.
288+    Note that @handler@ and the do-block must have the same return type.
289+    -}
290+    catchError :: m a -> (e -> m a) -> m a
291+
292hunk ./Control/Monad/Error.hs 38
293- -     Error(..),
294- -     MonadError(..),
295- -     ErrorT(..),
296- -     mapErrorT,
297- -     module Control.Monad,
298- -     module Control.Monad.Fix,
299- -     module Control.Monad.Trans,
300+    module Control.Monad.Error.Class,
301+    ErrorT(..),
302+    mapErrorT,
303+    module Control.Monad,
304+    module Control.Monad.Fix,
305+    module Control.Monad.Trans,
306hunk ./Control/Monad/Error.hs 54
307+import Control.Monad.Cont.Class
308+import Control.Monad.Error.Class
309hunk ./Control/Monad/Error.hs 57
310+import Control.Monad.RWS.Class
311+import Control.Monad.Reader.Class
312+import Control.Monad.State.Class
313hunk ./Control/Monad/Error.hs 61
314- -import Control.Monad.Reader
315- -import Control.Monad.Writer
316- -import Control.Monad.State
317- -import Control.Monad.RWS
318- -import Control.Monad.Cont
319+import Control.Monad.Writer.Class
320hunk ./Control/Monad/Error.hs 66
321- --- | An exception to be thrown.
322- --- An instance must redefine at least one of 'noMsg', 'strMsg'.
323- -class Error a where
324- -     -- | Creates an exception without a message.
325- -     -- Default implementation is @'strMsg' \"\"@.
326- -     noMsg  :: a
327- -     -- | Creates an exception with a message.
328- -     -- Default implementation is 'noMsg'.
329- -     strMsg :: String -> a
330- -
331- -     noMsg    = strMsg ""
332- -     strMsg _ = noMsg
333- -
334- --- | A string can be thrown as an error.
335- -instance Error String where
336- -     noMsg  = ""
337- -     strMsg = id
338- -
339- -instance Error IOError where
340- -     strMsg = userError
341- -
342- -{- |
343- -The strategy of combining computations that can throw exceptions
344- -by bypassing bound functions
345- -from the point an exception is thrown to the point that it is handled.
346- -
347- -Is parameterized over the type of error information and
348- -the monad type constructor.
349- -It is common to use @'Data.Either' String@ as the monad type constructor
350- -for an error monad in which error descriptions take the form of strings.
351- -In that case and many other common cases the resulting monad is already defined
352- -as an instance of the 'MonadError' class.
353- -You can also define your own error type and\/or use a monad type constructor
354- -other than @'Data.Either' String@ or @'Data.Either' IOError@.
355- -In these cases you will have to explicitly define instances of the 'Error'
356- -and\/or 'MonadError' classes.
357- --}
358- -class (Monad m) => MonadError e m | m -> e where
359- -     -- | Is used within a monadic computation to begin exception processing.
360- -     throwError :: e -> m a
361- -
362- -     {- |
363- -     A handler function to handle previous errors and return to normal execution.
364- -     A common idiom is:
365- -
366- -     > do { action1; action2; action3 } `catchError` handler
367- -
368- -     where the @action@ functions can call 'throwError'.
369- -     Note that @handler@ and the do-block must have the same return type.
370- -     -}
371- -     catchError :: m a -> (e -> m a) -> m a
372- -
373hunk ./Control/Monad/Error.hs 67
374- -     mzero       = ioError (userError "mzero")
375- -     m `mplus` n = m `catch` \_ -> n
376+    mzero       = ioError (userError "mzero")
377+    m `mplus` n = m `catch` \_ -> n
378hunk ./Control/Monad/Error.hs 71
379- -     throwError = ioError
380- -     catchError = catch
381+    throwError = ioError
382+    catchError = catch
383hunk ./Control/Monad/Error.hs 78
384- -     return        = Right
385- -     Left  l >>= _ = Left l
386- -     Right r >>= k = k r
387- -     fail msg      = Left (strMsg msg)
388+    return        = Right
389+    Left  l >>= _ = Left l
390+    Right r >>= k = k r
391+    fail msg      = Left (strMsg msg)
392hunk ./Control/Monad/Error.hs 84
393- -     mzero            = Left noMsg
394- -     Left _ `mplus` n = n
395- -     m      `mplus` _ = m
396+    mzero            = Left noMsg
397+    Left _ `mplus` n = n
398+    m      `mplus` _ = m
399hunk ./Control/Monad/Error.hs 89
400- -     mfix f = let
401- -             a = f $ case a of
402- -                     Right r -> r
403- -                     _       -> error "empty mfix argument"
404- -             in a
405+    mfix f = let
406+        a = f $ case a of
407+            Right r -> r
408+            _       -> error "empty mfix argument"
409+        in a
410hunk ./Control/Monad/Error.hs 96
411- -     throwError             = Left
412- -     Left  l `catchError` h = h l
413- -     Right r `catchError` _ = Right r
414+    throwError             = Left
415+    Left  l `catchError` h = h l
416+    Right r `catchError` _ = Right r
417hunk ./Control/Monad/Error.hs 124
418+mapErrorT :: (m (Either e a) -> n (Either e' b))
419+          -> ErrorT e m a
420+          -> ErrorT e' n b
421+mapErrorT f m = ErrorT $ f (runErrorT m)
422+
423hunk ./Control/Monad/Error.hs 130
424- -     fmap f m = ErrorT $ do
425- -             a <- runErrorT m
426- -             case a of
427- -                     Left  l -> return (Left  l)
428- -                     Right r -> return (Right (f r))
429+    fmap f m = ErrorT $ do
430+        a <- runErrorT m
431+        case a of
432+            Left  l -> return (Left  l)
433+            Right r -> return (Right (f r))
434hunk ./Control/Monad/Error.hs 137
435- -     return a = ErrorT $ return (Right a)
436- -     m >>= k  = ErrorT $ do
437- -             a <- runErrorT m
438- -             case a of
439- -                     Left  l -> return (Left l)
440- -                     Right r -> runErrorT (k r)
441- -     fail msg = ErrorT $ return (Left (strMsg msg))
442+    return a = ErrorT $ return (Right a)
443+    m >>= k  = ErrorT $ do
444+        a <- runErrorT m
445+        case a of
446+            Left  l -> return (Left l)
447+            Right r -> runErrorT (k r)
448+    fail msg = ErrorT $ return (Left (strMsg msg))
449hunk ./Control/Monad/Error.hs 146
450- -     mzero       = ErrorT $ return (Left noMsg)
451- -     m `mplus` n = ErrorT $ do
452- -             a <- runErrorT m
453- -             case a of
454- -                     Left  _ -> runErrorT n
455- -                     Right r -> return (Right r)
456+    mzero       = ErrorT $ return (Left noMsg)
457+    m `mplus` n = ErrorT $ do
458+        a <- runErrorT m
459+        case a of
460+            Left  _ -> runErrorT n
461+            Right r -> return (Right r)
462hunk ./Control/Monad/Error.hs 154
463- -     mfix f = ErrorT $ mfix $ \a -> runErrorT $ f $ case a of
464- -             Right r -> r
465- -             _       -> error "empty mfix argument"
466+    mfix f = ErrorT $ mfix $ \a -> runErrorT $ f $ case a of
467+        Right r -> r
468+        _       -> error "empty mfix argument"
469hunk ./Control/Monad/Error.hs 159
470- -     throwError l     = ErrorT $ return (Left l)
471- -     m `catchError` h = ErrorT $ do
472- -             a <- runErrorT m
473- -             case a of
474- -                     Left  l -> runErrorT (h l)
475- -                     Right r -> return (Right r)
476+    throwError l     = ErrorT $ return (Left l)
477+    m `catchError` h = ErrorT $ do
478+        a <- runErrorT m
479+        case a of
480+            Left  l -> runErrorT (h l)
481+            Right r -> return (Right r)
482+
483+-- ---------------------------------------------------------------------------
484+-- Instances for other mtl transformers
485hunk ./Control/Monad/Error.hs 170
486- -     lift m = ErrorT $ do
487- -             a <- m
488- -             return (Right a)
489+    lift m = ErrorT $ do
490+        a <- m
491+        return (Right a)
492hunk ./Control/Monad/Error.hs 175
493- -     liftIO = lift . liftIO
494- -
495- -instance (Error e, MonadReader r m) => MonadReader r (ErrorT e m) where
496- -     ask       = lift ask
497- -     local f m = ErrorT $ local f (runErrorT m)
498- -
499- -instance (Error e, MonadWriter w m) => MonadWriter w (ErrorT e m) where
500- -     tell     = lift . tell
501- -     listen m = ErrorT $ do
502- -             (a, w) <- listen (runErrorT m)
503- -             return $ case a of
504- -                     Left  l -> Left  l
505- -                     Right r -> Right (r, w)
506- -     pass   m = ErrorT $ pass $ do
507- -             a <- runErrorT m
508- -             return $ case a of
509- -                     Left  l      -> (Left  l, id)
510- -                     Right (r, f) -> (Right r, f)
511- -
512- -instance (Error e, MonadState s m) => MonadState s (ErrorT e m) where
513- -     get = lift get
514- -     put = lift . put
515+    liftIO = lift . liftIO
516hunk ./Control/Monad/Error.hs 178
517- -     callCC f = ErrorT $
518- -             callCC $ \c ->
519- -             runErrorT (f (\a -> ErrorT $ c (Right a)))
520+    callCC f = ErrorT $
521+        callCC $ \c ->
522+        runErrorT (f (\a -> ErrorT $ c (Right a)))
523hunk ./Control/Monad/Error.hs 182
524- -mapErrorT :: (m (Either e a) -> n (Either e' b)) -> ErrorT e m a -> ErrorT e' n b
525- -mapErrorT f m = ErrorT $ f (runErrorT m)
526- -
527- --- ---------------------------------------------------------------------------
528- --- MonadError instances for other monad transformers
529- -
530- -instance (MonadError e m) => MonadError e (ReaderT r m) where
531- -     throwError       = lift . throwError
532- -     m `catchError` h = ReaderT $ \r -> runReaderT m r
533- -             `catchError` \e -> runReaderT (h e) r
534+instance (Error e, MonadRWS r w s m) => MonadRWS r w s (ErrorT e m)
535hunk ./Control/Monad/Error.hs 184
536- -instance (Monoid w, MonadError e m) => MonadError e (WriterT w m) where
537- -     throwError       = lift . throwError
538- -     m `catchError` h = WriterT $ runWriterT m
539- -             `catchError` \e -> runWriterT (h e)
540+instance (Error e, MonadReader r m) => MonadReader r (ErrorT e m) where
541+    ask       = lift ask
542+    local f m = ErrorT $ local f (runErrorT m)
543hunk ./Control/Monad/Error.hs 188
544- -instance (MonadError e m) => MonadError e (StateT s m) where
545- -     throwError       = lift . throwError
546- -     m `catchError` h = StateT $ \s -> runStateT m s
547- -             `catchError` \e -> runStateT (h e) s
548+instance (Error e, MonadState s m) => MonadState s (ErrorT e m) where
549+    get = lift get
550+    put = lift . put
551hunk ./Control/Monad/Error.hs 192
552- -instance (Monoid w, MonadError e m) => MonadError e (RWST r w s m) where
553- -     throwError       = lift . throwError
554- -     m `catchError` h = RWST $ \r s -> runRWST m r s
555- -             `catchError` \e -> runRWST (h e) r s
556+instance (Error e, MonadWriter w m) => MonadWriter w (ErrorT e m) where
557+    tell     = lift . tell
558+    listen m = ErrorT $ do
559+        (a, w) <- listen (runErrorT m)
560+        case a of
561+            Left  l -> return $ Left  l
562+            Right r -> return $ Right (r, w)
563+    pass   m = ErrorT $ pass $ do
564+        a <- runErrorT m
565+        case a of
566+            Left  l      -> return (Left  l, id)
567+            Right (r, f) -> return (Right r, f)
568hunk ./Control/Monad/Identity.hs 4
569- -      (c) Oregon Graduate Institute of Science and Technology 2001,
570- -      (c) Jeff Newbern 2003-2006,
571- -      (c) Andriy Palamarchuk 2006
572+               (c) Oregon Graduate Institute of Science and Technology 2001,
573+               (c) Jeff Newbern 2003-2006,
574+               (c) Andriy Palamarchuk 2006
575hunk ./Control/Monad/Identity.hs 36
576- -      Higher-Order Polymorphism/,
577+      Higher-Order Polymorphism/,
578hunk ./Control/Monad/Identity.hs 38
579- -       Advanced School of Functional Programming, 1995.
580+      Advanced School of Functional Programming, 1995.
581hunk ./Control/Monad/Identity.hs 42
582- -     Identity(..),
583+    Identity(..),
584hunk ./Control/Monad/Identity.hs 44
585- -     module Control.Monad,
586- -     module Control.Monad.Fix,
587+    module Control.Monad,
588+    module Control.Monad.Fix,
589hunk ./Control/Monad/Identity.hs 56
590- - 
591+
592hunk ./Control/Monad/Identity.hs 59
593- - 
594+
595hunk ./Control/Monad/Identity.hs 90
596- -     fmap f m = Identity (f (runIdentity m))
597+    fmap f m = Identity (f (runIdentity m))
598hunk ./Control/Monad/Identity.hs 93
599- -     return a = Identity a
600- -     m >>= k  = k (runIdentity m)
601+    return a = Identity a
602+    m >>= k  = k (runIdentity m)
603hunk ./Control/Monad/Identity.hs 97
604- -     mfix f = Identity (fix (runIdentity . f))
605+    mfix f = Identity (fix (runIdentity . f))
606hunk ./Control/Monad/List.hs 8
607- ---             (c) Oregon Graduate Institute of Science and Technology, 2001
608+--                (c) Oregon Graduate Institute of Science and Technology, 2001
609hunk ./Control/Monad/List.hs 10
610- ---
611+--
612hunk ./Control/Monad/List.hs 20
613- -     ListT(..),
614- -     mapListT,
615- -     module Control.Monad,
616- -     module Control.Monad.Trans,
617+    ListT(..),
618+    mapListT,
619+    module Control.Monad,
620+    module Control.Monad.Trans,
621hunk ./Control/Monad/List.hs 29
622+import Control.Monad.Cont.Class
623+import Control.Monad.Error.Class
624+import Control.Monad.Reader.Class
625+import Control.Monad.State.Class
626hunk ./Control/Monad/List.hs 34
627- -import Control.Monad.Reader
628- -import Control.Monad.State
629- -import Control.Monad.Cont
630- -import Control.Monad.Error
631hunk ./Control/Monad/List.hs 40
632+mapListT :: (m [a] -> n [b]) -> ListT m a -> ListT n b
633+mapListT f m = ListT $ f (runListT m)
634+
635hunk ./Control/Monad/List.hs 44
636- -     fmap f m = ListT $ do
637- -             a <- runListT m
638- -             return (map f a)
639+    fmap f m = ListT $ do
640+        a <- runListT m
641+        return (map f a)
642hunk ./Control/Monad/List.hs 49
643- -     return a = ListT $ return [a]
644- -     m >>= k  = ListT $ do
645- -             a <- runListT m
646- -             b <- mapM (runListT . k) a
647- -             return (concat b)
648- -     fail _ = ListT $ return []
649+    return a = ListT $ return [a]
650+    m >>= k  = ListT $ do
651+        a <- runListT m
652+        b <- mapM (runListT . k) a
653+        return (concat b)
654+    fail _ = ListT $ return []
655hunk ./Control/Monad/List.hs 57
656- -     mzero       = ListT $ return []
657- -     m `mplus` n = ListT $ do
658- -             a <- runListT m
659- -             b <- runListT n
660- -             return (a ++ b)
661+    mzero       = ListT $ return []
662+    m `mplus` n = ListT $ do
663+        a <- runListT m
664+        b <- runListT n
665+        return (a ++ b)
666+
667+-- ---------------------------------------------------------------------------
668+-- Instances for other mtl transformers
669hunk ./Control/Monad/List.hs 67
670- -     lift m = ListT $ do
671- -             a <- m
672- -             return [a]
673+    lift m = ListT $ do
674+        a <- m
675+        return [a]
676hunk ./Control/Monad/List.hs 72
677- -     liftIO = lift . liftIO
678- -
679- -instance (MonadReader s m) => MonadReader s (ListT m) where
680- -     ask       = lift ask
681- -     local f m = ListT $ local f (runListT m)
682- -
683- -instance (MonadState s m) => MonadState s (ListT m) where
684- -     get = lift get
685- -     put = lift . put
686+    liftIO = lift . liftIO
687hunk ./Control/Monad/List.hs 75
688- -     callCC f = ListT $
689- -             callCC $ \c ->
690- -             runListT (f (\a -> ListT $ c [a]))
691+    callCC f = ListT $
692+        callCC $ \c ->
693+        runListT (f (\a -> ListT $ c [a]))
694hunk ./Control/Monad/List.hs 80
695- -     throwError       = lift . throwError
696- -     m `catchError` h = ListT $ runListT m
697- -             `catchError` \e -> runListT (h e)
698+    throwError       = lift . throwError
699+    m `catchError` h = ListT $ runListT m
700+        `catchError` \e -> runListT (h e)
701+
702+instance (MonadReader s m) => MonadReader s (ListT m) where
703+    ask       = lift ask
704+    local f m = ListT $ local f (runListT m)
705+
706+instance (MonadState s m) => MonadState s (ListT m) where
707+    get = lift get
708+    put = lift . put
709hunk ./Control/Monad/List.hs 92
710- -mapListT :: (m [a] -> n [b]) -> ListT m a -> ListT n b
711- -mapListT f m = ListT $ f (runListT m)
712hunk ./Control/Monad/RWS/Class.hs 1
713+-----------------------------------------------------------------------------
714+-- |
715+-- Module      :  Control.Monad.RWS.Class
716+-- Copyright   :  (c) Andy Gill 2001,
717+--                (c) Oregon Graduate Institute of Science and Technology, 2001
718+-- License     :  BSD-style (see the file libraries/base/LICENSE)
719+--
720+-- Maintainer  :  libraries@haskell.org
721+-- Stability   :  experimental
722+-- Portability :  non-portable (multi-param classes, functional dependencies)
723+--
724+-- Declaration of the MonadRWS class.
725+--
726+--      Inspired by the paper
727+--      /Functional Programming with Overloading and
728+--          Higher-Order Polymorphism/,
729+--        Mark P Jones (<http://www.cse.ogi.edu/~mpj/>)
730+--          Advanced School of Functional Programming, 1995.
731+-----------------------------------------------------------------------------
732+
733+module Control.Monad.RWS.Class (
734+    MonadRWS,
735+    module Control.Monad.Reader.Class,
736+    module Control.Monad.State.Class,
737+    module Control.Monad.Writer.Class,
738+  ) where
739+
740+import Control.Monad.Reader.Class
741+import Control.Monad.State.Class
742+import Control.Monad.Writer.Class
743+import Data.Monoid
744+
745+class (Monoid w, MonadReader r m, MonadWriter w m, MonadState s m)
746+   => MonadRWS r w s m | m -> r, m -> w, m -> s
747+
748hunk ./Control/Monad/RWS/Lazy.hs 1
749+{-# OPTIONS -fallow-undecidable-instances #-}
750+-----------------------------------------------------------------------------
751+-- |
752+-- Module      :  Control.Monad.RWS.Lazy
753+-- Copyright   :  (c) Andy Gill 2001,
754+--                (c) Oregon Graduate Institute of Science and Technology, 2001
755+-- License     :  BSD-style (see the file libraries/base/LICENSE)
756+--
757+-- Maintainer  :  libraries@haskell.org
758+-- Stability   :  experimental
759+-- Portability :  non-portable (multi-param classes, functional dependencies)
760+--
761+-- Lazy RWS monad.
762+--
763+--      Inspired by the paper
764+--      /Functional Programming with Overloading and
765+--          Higher-Order Polymorphism/,
766+--        Mark P Jones (<http://www.cse.ogi.edu/~mpj/>)
767+--          Advanced School of Functional Programming, 1995.
768+-----------------------------------------------------------------------------
769+
770+module Control.Monad.RWS.Lazy (
771+    RWS(..),
772+    evalRWS,
773+    execRWS,
774+    mapRWS,
775+    withRWS,
776+    RWST(..),
777+    evalRWST,
778+    execRWST,
779+    mapRWST,
780+    withRWST,
781+    module Control.Monad.RWS.Class,
782+  ) where
783+
784+import Prelude
785+
786+import Control.Monad
787+import Control.Monad.Cont.Class
788+import Control.Monad.Error.Class
789+import Control.Monad.Fix
790+import Control.Monad.RWS.Class
791+import Control.Monad.Reader.Class
792+import Control.Monad.State.Class
793+import Control.Monad.Trans
794+import Control.Monad.Writer.Class
795+import Data.Monoid
796+
797+newtype RWS r w s a = RWS { runRWS :: r -> s -> (a, s, w) }
798+
799+evalRWS :: RWS r w s a -> r -> s -> (a, w)
800+evalRWS m r s = let
801+    (a, _, w) = runRWS m r s
802+    in (a, w)
803+
804+execRWS :: RWS r w s a -> r -> s -> (s, w)
805+execRWS m r s = let
806+    (_, s', w) = runRWS m r s
807+    in (s', w)
808+
809+mapRWS :: ((a, s, w) -> (b, s, w')) -> RWS r w s a -> RWS r w' s b
810+mapRWS f m = RWS $ \r s -> f (runRWS m r s)
811+
812+withRWS :: (r' -> s -> (r, s)) -> RWS r w s a -> RWS r' w s a
813+withRWS f m = RWS $ \r s -> uncurry (runRWS m) (f r s)
814+
815+instance Functor (RWS r w s) where
816+    fmap f m = RWS $ \r s -> let
817+        (a, s', w) = runRWS m r s
818+        in (f a, s', w)
819+
820+instance (Monoid w) => Monad (RWS r w s) where
821+    return a = RWS $ \_ s -> (a, s, mempty)
822+    m >>= k  = RWS $ \r s -> let
823+        (a, s',  w)  = runRWS m r s
824+        (b, s'', w') = runRWS (k a) r s'
825+        in (b, s'', w `mappend` w')
826+
827+instance (Monoid w) => MonadFix (RWS r w s) where
828+    mfix f = RWS $ \r s -> let (a, s', w) = runRWS (f a) r s in (a, s', w)
829+
830+instance (Monoid w) => MonadReader r (RWS r w s) where
831+    ask       = RWS $ \r s -> (r, s, mempty)
832+    local f m = RWS $ \r s -> runRWS m (f r) s
833+
834+instance (Monoid w) => MonadWriter w (RWS r w s) where
835+    tell   w = RWS $ \_ s -> ((), s, w)
836+    listen m = RWS $ \r s -> let
837+        (a, s', w) = runRWS m r s
838+        in ((a, w), s', w)
839+    pass   m = RWS $ \r s -> let
840+        ((a, f), s', w) = runRWS m r s
841+        in (a, s', f w)
842+
843+instance (Monoid w) => MonadState s (RWS r w s) where
844+    get   = RWS $ \_ s -> (s, s, mempty)
845+    put s = RWS $ \_ _ -> ((), s, mempty)
846+
847+instance (Monoid w) => MonadRWS r w s (RWS r w s)
848+
849+-- ---------------------------------------------------------------------------
850+-- Our parameterizable RWS monad, with an inner monad
851+
852+newtype RWST r w s m a = RWST { runRWST :: r -> s -> m (a, s, w) }
853+
854+evalRWST :: (Monad m) => RWST r w s m a -> r -> s -> m (a, w)
855+evalRWST m r s = do
856+    ~(a, _, w) <- runRWST m r s
857+    return (a, w)
858+
859+execRWST :: (Monad m) => RWST r w s m a -> r -> s -> m (s, w)
860+execRWST m r s = do
861+    ~(_, s', w) <- runRWST m r s
862+    return (s', w)
863+
864+mapRWST :: (m (a, s, w) -> n (b, s, w')) -> RWST r w s m a -> RWST r w' s n b
865+mapRWST f m = RWST $ \r s -> f (runRWST m r s)
866+
867+withRWST :: (r' -> s -> (r, s)) -> RWST r w s m a -> RWST r' w s m a
868+withRWST f m = RWST $ \r s -> uncurry (runRWST m) (f r s)
869+
870+instance (Monad m) => Functor (RWST r w s m) where
871+    fmap f m = RWST $ \r s -> do
872+        ~(a, s', w) <- runRWST m r s
873+        return (f a, s', w)
874+
875+instance (Monoid w, Monad m) => Monad (RWST r w s m) where
876+    return a = RWST $ \_ s -> return (a, s, mempty)
877+    m >>= k  = RWST $ \r s -> do
878+        ~(a, s', w)  <- runRWST m r s
879+        ~(b, s'',w') <- runRWST (k a) r s'
880+        return (b, s'', w `mappend` w')
881+    fail msg = RWST $ \_ _ -> fail msg
882+
883+instance (Monoid w, MonadPlus m) => MonadPlus (RWST r w s m) where
884+    mzero       = RWST $ \_ _ -> mzero
885+    m `mplus` n = RWST $ \r s -> runRWST m r s `mplus` runRWST n r s
886+
887+instance (Monoid w, MonadFix m) => MonadFix (RWST r w s m) where
888+    mfix f = RWST $ \r s -> mfix $ \ ~(a, _, _) -> runRWST (f a) r s
889+
890+instance (Monoid w, Monad m) => MonadReader r (RWST r w s m) where
891+    ask       = RWST $ \r s -> return (r, s, mempty)
892+    local f m = RWST $ \r s -> runRWST m (f r) s
893+
894+instance (Monoid w, Monad m) => MonadWriter w (RWST r w s m) where
895+    tell   w = RWST $ \_ s -> return ((),s,w)
896+    listen m = RWST $ \r s -> do
897+        ~(a, s', w) <- runRWST m r s
898+        return ((a, w), s', w)
899+    pass   m = RWST $ \r s -> do
900+        ~((a, f), s', w) <- runRWST m r s
901+        return (a, s', f w)
902+
903+instance (Monoid w, Monad m) => MonadState s (RWST r w s m) where
904+    get   = RWST $ \_ s -> return (s, s, mempty)
905+    put s = RWST $ \_ _ -> return ((), s, mempty)
906+
907+instance (Monoid w, Monad m) => MonadRWS r w s (RWST r w s m)
908+
909+-- ---------------------------------------------------------------------------
910+-- Instances for other mtl transformers
911+
912+instance (Monoid w) => MonadTrans (RWST r w s) where
913+    lift m = RWST $ \_ s -> do
914+        a <- m
915+        return (a, s, mempty)
916+
917+instance (Monoid w, MonadIO m) => MonadIO (RWST r w s m) where
918+    liftIO = lift . liftIO
919+
920+instance (Monoid w, MonadCont m) => MonadCont (RWST r w s m) where
921+    callCC f = RWST $ \r s ->
922+        callCC $ \c ->
923+        runRWST (f (\a -> RWST $ \_ s' -> c (a, s', mempty))) r s
924+
925+instance (Monoid w, MonadError e m) => MonadError e (RWST r w s m) where
926+    throwError       = lift . throwError
927+    m `catchError` h = RWST $ \r s -> runRWST m r s
928+        `catchError` \e -> runRWST (h e) r s
929+
930hunk ./Control/Monad/RWS/Strict.hs 1
931+{-# OPTIONS -fallow-undecidable-instances #-}
932+-----------------------------------------------------------------------------
933+-- |
934+-- Module      :  Control.Monad.RWS.Strict
935+-- Copyright   :  (c) Andy Gill 2001,
936+--                (c) Oregon Graduate Institute of Science and Technology, 2001
937+-- License     :  BSD-style (see the file libraries/base/LICENSE)
938+--
939+-- Maintainer  :  libraries@haskell.org
940+-- Stability   :  experimental
941+-- Portability :  non-portable (multi-param classes, functional dependencies)
942+--
943+-- Strict RWS Monad.
944+--
945+--      Inspired by the paper
946+--      /Functional Programming with Overloading and
947+--          Higher-Order Polymorphism/,
948+--        Mark P Jones (<http://www.cse.ogi.edu/~mpj/>)
949+--          Advanced School of Functional Programming, 1995.
950+-----------------------------------------------------------------------------
951+
952+module Control.Monad.RWS.Strict (
953+    RWS(..),
954+    evalRWS,
955+    execRWS,
956+    mapRWS,
957+    withRWS,
958+    RWST(..),
959+    evalRWST,
960+    execRWST,
961+    mapRWST,
962+    withRWST,
963+    module Control.Monad.RWS.Class,
964+  ) where
965+
966+import Prelude
967+
968+import Control.Monad
969+import Control.Monad.Cont.Class
970+import Control.Monad.Error.Class
971+import Control.Monad.Fix
972+import Control.Monad.RWS.Class
973+import Control.Monad.Reader.Class
974+import Control.Monad.State.Class
975+import Control.Monad.Trans
976+import Control.Monad.Writer.Class
977+import Data.Monoid
978+
979+newtype RWS r w s a = RWS { runRWS :: r -> s -> (a, s, w) }
980+
981+evalRWS :: RWS r w s a -> r -> s -> (a, w)
982+evalRWS m r s = case runRWS m r s of
983+                    (a, _, w) -> (a, w)
984+
985+execRWS :: RWS r w s a -> r -> s -> (s, w)
986+execRWS m r s = case runRWS m r s of
987+                    (_, s', w) -> (s', w)
988+
989+mapRWS :: ((a, s, w) -> (b, s, w')) -> RWS r w s a -> RWS r w' s b
990+mapRWS f m = RWS $ \r s -> f (runRWS m r s)
991+
992+withRWS :: (r' -> s -> (r, s)) -> RWS r w s a -> RWS r' w s a
993+withRWS f m = RWS $ \r s -> uncurry (runRWS m) (f r s)
994+
995+instance Functor (RWS r w s) where
996+    fmap f m = RWS $ \r s -> case runRWS m r s of
997+                                 (a, s', w) -> (f a, s', w)
998+
999+instance (Monoid w) => Monad (RWS r w s) where
1000+    return a = RWS $ \_ s -> (a, s, mempty)
1001+    m >>= k  = RWS $ \r s -> case runRWS m r s of
1002+                                 (a, s',  w) ->
1003+                                     case runRWS (k a) r s' of
1004+                                         (b, s'', w') ->
1005+                                             (b, s'', w `mappend` w')
1006+
1007+instance (Monoid w) => MonadFix (RWS r w s) where
1008+    mfix f = RWS $ \r s -> let (a, s', w) = runRWS (f a) r s in (a, s', w)
1009+
1010+instance (Monoid w) => MonadReader r (RWS r w s) where
1011+    ask       = RWS $ \r s -> (r, s, mempty)
1012+    local f m = RWS $ \r s -> runRWS m (f r) s
1013+
1014+instance (Monoid w) => MonadWriter w (RWS r w s) where
1015+    tell   w = RWS $ \_ s -> ((), s, w)
1016+    listen m = RWS $ \r s -> case runRWS m r s of
1017+                                 (a, s', w) -> ((a, w), s', w)
1018+    pass   m = RWS $ \r s -> case runRWS m r s of
1019+                                 ((a, f), s', w) -> (a, s', f w)
1020+
1021+instance (Monoid w) => MonadState s (RWS r w s) where
1022+    get   = RWS $ \_ s -> (s, s, mempty)
1023+    put s = RWS $ \_ _ -> ((), s, mempty)
1024+
1025+instance (Monoid w) => MonadRWS r w s (RWS r w s)
1026+
1027+-- ---------------------------------------------------------------------------
1028+-- Our parameterizable RWS monad, with an inner monad
1029+
1030+newtype RWST r w s m a = RWST { runRWST :: r -> s -> m (a, s, w) }
1031+
1032+evalRWST :: (Monad m) => RWST r w s m a -> r -> s -> m (a, w)
1033+evalRWST m r s = do
1034+    (a, _, w) <- runRWST m r s
1035+    return (a, w)
1036+
1037+execRWST :: (Monad m) => RWST r w s m a -> r -> s -> m (s, w)
1038+execRWST m r s = do
1039+    (_, s', w) <- runRWST m r s
1040+    return (s', w)
1041+
1042+mapRWST :: (m (a, s, w) -> n (b, s, w')) -> RWST r w s m a -> RWST r w' s n b
1043+mapRWST f m = RWST $ \r s -> f (runRWST m r s)
1044+
1045+withRWST :: (r' -> s -> (r, s)) -> RWST r w s m a -> RWST r' w s m a
1046+withRWST f m = RWST $ \r s -> uncurry (runRWST m) (f r s)
1047+
1048+instance (Monad m) => Functor (RWST r w s m) where
1049+    fmap f m = RWST $ \r s -> do
1050+        (a, s', w) <- runRWST m r s
1051+        return (f a, s', w)
1052+
1053+instance (Monoid w, Monad m) => Monad (RWST r w s m) where
1054+    return a = RWST $ \_ s -> return (a, s, mempty)
1055+    m >>= k  = RWST $ \r s -> do
1056+        (a, s', w)  <- runRWST m r s
1057+        (b, s'',w') <- runRWST (k a) r s'
1058+        return (b, s'', w `mappend` w')
1059+    fail msg = RWST $ \_ _ -> fail msg
1060+
1061+instance (Monoid w, MonadPlus m) => MonadPlus (RWST r w s m) where
1062+    mzero       = RWST $ \_ _ -> mzero
1063+    m `mplus` n = RWST $ \r s -> runRWST m r s `mplus` runRWST n r s
1064+
1065+instance (Monoid w, MonadFix m) => MonadFix (RWST r w s m) where
1066+    mfix f = RWST $ \r s -> mfix $ \ ~(a, _, _) -> runRWST (f a) r s
1067+
1068+instance (Monoid w, Monad m) => MonadReader r (RWST r w s m) where
1069+    ask       = RWST $ \r s -> return (r, s, mempty)
1070+    local f m = RWST $ \r s -> runRWST m (f r) s
1071+
1072+instance (Monoid w, Monad m) => MonadWriter w (RWST r w s m) where
1073+    tell   w = RWST $ \_ s -> return ((),s,w)
1074+    listen m = RWST $ \r s -> do
1075+        (a, s', w) <- runRWST m r s
1076+        return ((a, w), s', w)
1077+    pass   m = RWST $ \r s -> do
1078+        ((a, f), s', w) <- runRWST m r s
1079+        return (a, s', f w)
1080+
1081+instance (Monoid w, Monad m) => MonadState s (RWST r w s m) where
1082+    get   = RWST $ \_ s -> return (s, s, mempty)
1083+    put s = RWST $ \_ _ -> return ((), s, mempty)
1084+
1085+instance (Monoid w, Monad m) => MonadRWS r w s (RWST r w s m)
1086+
1087+-- ---------------------------------------------------------------------------
1088+-- Instances for other mtl transformers
1089+
1090+instance (Monoid w) => MonadTrans (RWST r w s) where
1091+    lift m = RWST $ \_ s -> do
1092+        a <- m
1093+        return (a, s, mempty)
1094+
1095+instance (Monoid w, MonadIO m) => MonadIO (RWST r w s m) where
1096+    liftIO = lift . liftIO
1097+
1098+instance (Monoid w, MonadCont m) => MonadCont (RWST r w s m) where
1099+    callCC f = RWST $ \r s ->
1100+        callCC $ \c ->
1101+        runRWST (f (\a -> RWST $ \_ s' -> c (a, s', mempty))) r s
1102+
1103+instance (Monoid w, MonadError e m) => MonadError e (RWST r w s m) where
1104+    throwError       = lift . throwError
1105+    m `catchError` h = RWST $ \r s -> runRWST m r s
1106+        `catchError` \e -> runRWST (h e) r s
1107+
1108hunk ./Control/Monad/RWS.hs 5
1109- ---             (c) Oregon Graduate Institute of Science and Technology, 2001
1110+--                (c) Oregon Graduate Institute of Science and Technology, 2001
1111hunk ./Control/Monad/RWS.hs 7
1112- ---
1113+--
1114hunk ./Control/Monad/RWS.hs 14
1115- ---     Inspired by the paper
1116- ---     /Functional Programming with Overloading and
1117- ---         Higher-Order Polymorphism/,
1118- ---       Mark P Jones (<http://www.cse.ogi.edu/~mpj/>)
1119- ---             Advanced School of Functional Programming, 1995.
1120+--      Inspired by the paper
1121+--      /Functional Programming with Overloading and
1122+--          Higher-Order Polymorphism/,
1123+--        Mark P Jones (<http://www.cse.ogi.edu/~mpj/>)
1124+--          Advanced School of Functional Programming, 1995.
1125hunk ./Control/Monad/RWS.hs 22
1126- -     RWS(..),
1127- -     evalRWS,
1128- -     execRWS,
1129- -     mapRWS,
1130- -     withRWS,
1131- -     RWST(..),
1132- -     evalRWST,
1133- -     execRWST,
1134- -     mapRWST,
1135- -     withRWST,
1136- -     module Control.Monad.Reader,
1137- -     module Control.Monad.Writer,
1138- -     module Control.Monad.State,
1139+    module Control.Monad.RWS.Lazy
1140hunk ./Control/Monad/RWS.hs 25
1141- -import Prelude
1142- -
1143- -import Control.Monad
1144- -import Control.Monad.Fix
1145- -import Control.Monad.Trans
1146- -import Control.Monad.Reader
1147- -import Control.Monad.Writer
1148- -import Control.Monad.State
1149- -import Data.Monoid
1150- -
1151- -newtype RWS r w s a = RWS { runRWS :: r -> s -> (a, s, w) }
1152- -
1153- -instance Functor (RWS r w s) where
1154- -     fmap f m = RWS $ \r s -> let
1155- -             (a, s', w) = runRWS m r s
1156- -             in (f a, s', w)
1157- -
1158- -instance (Monoid w) => Monad (RWS r w s) where
1159- -     return a = RWS $ \_ s -> (a, s, mempty)
1160- -     m >>= k  = RWS $ \r s -> let
1161- -             (a, s',  w)  = runRWS m r s
1162- -             (b, s'', w') = runRWS (k a) r s'
1163- -             in (b, s'', w `mappend` w')
1164- -
1165- -instance (Monoid w) => MonadFix (RWS r w s) where
1166- -     mfix f = RWS $ \r s -> let (a, s', w) = runRWS (f a) r s in (a, s', w)
1167- -
1168- -instance (Monoid w) => MonadReader r (RWS r w s) where
1169- -     ask       = RWS $ \r s -> (r, s, mempty)
1170- -     local f m = RWS $ \r s -> runRWS m (f r) s
1171- -
1172- -instance (Monoid w) => MonadWriter w (RWS r w s) where
1173- -     tell   w = RWS $ \_ s -> ((), s, w)
1174- -     listen m = RWS $ \r s -> let
1175- -             (a, s', w) = runRWS m r s
1176- -             in ((a, w), s', w)
1177- -     pass   m = RWS $ \r s -> let
1178- -             ((a, f), s', w) = runRWS m r s
1179- -             in (a, s', f w)
1180- -
1181- -instance (Monoid w) => MonadState s (RWS r w s) where
1182- -     get   = RWS $ \_ s -> (s, s, mempty)
1183- -     put s = RWS $ \_ _ -> ((), s, mempty)
1184- -
1185- -
1186- -evalRWS :: RWS r w s a -> r -> s -> (a, w)
1187- -evalRWS m r s = let
1188- -    (a, _, w) = runRWS m r s
1189- -    in (a, w)
1190- -
1191- -execRWS :: RWS r w s a -> r -> s -> (s, w)
1192- -execRWS m r s = let
1193- -    (_, s', w) = runRWS m r s
1194- -    in (s', w)
1195- -
1196- -mapRWS :: ((a, s, w) -> (b, s, w')) -> RWS r w s a -> RWS r w' s b
1197- -mapRWS f m = RWS $ \r s -> f (runRWS m r s)
1198- -
1199- -withRWS :: (r' -> s -> (r, s)) -> RWS r w s a -> RWS r' w s a
1200- -withRWS f m = RWS $ \r s -> uncurry (runRWS m) (f r s)
1201- -
1202- -
1203- -newtype RWST r w s m a = RWST { runRWST :: r -> s -> m (a, s, w) }
1204- -
1205- -instance (Monad m) => Functor (RWST r w s m) where
1206- -     fmap f m = RWST $ \r s -> do
1207- -             (a, s', w) <- runRWST m r s
1208- -             return (f a, s', w)
1209- -
1210- -instance (Monoid w, Monad m) => Monad (RWST r w s m) where
1211- -     return a = RWST $ \_ s -> return (a, s, mempty)
1212- -     m >>= k  = RWST $ \r s -> do
1213- -             (a, s', w)  <- runRWST m r s
1214- -             (b, s'',w') <- runRWST (k a) r s'
1215- -             return (b, s'', w `mappend` w')
1216- -     fail msg = RWST $ \_ _ -> fail msg
1217- -
1218- -instance (Monoid w, MonadPlus m) => MonadPlus (RWST r w s m) where
1219- -     mzero       = RWST $ \_ _ -> mzero
1220- -     m `mplus` n = RWST $ \r s -> runRWST m r s `mplus` runRWST n r s
1221- -
1222- -instance (Monoid w, MonadFix m) => MonadFix (RWST r w s m) where
1223- -     mfix f = RWST $ \r s -> mfix $ \ ~(a, _, _) -> runRWST (f a) r s
1224- -
1225- -instance (Monoid w, Monad m) => MonadReader r (RWST r w s m) where
1226- -     ask       = RWST $ \r s -> return (r, s, mempty)
1227- -     local f m = RWST $ \r s -> runRWST m (f r) s
1228- -
1229- -instance (Monoid w, Monad m) => MonadWriter w (RWST r w s m) where
1230- -     tell   w = RWST $ \_ s -> return ((),s,w)
1231- -     listen m = RWST $ \r s -> do
1232- -             (a, s', w) <- runRWST m r s
1233- -             return ((a, w), s', w)
1234- -     pass   m = RWST $ \r s -> do
1235- -             ((a, f), s', w) <- runRWST m r s
1236- -             return (a, s', f w)
1237- -
1238- -instance (Monoid w, Monad m) => MonadState s (RWST r w s m) where
1239- -     get   = RWST $ \_ s -> return (s, s, mempty)
1240- -     put s = RWST $ \_ _ -> return ((), s, mempty)
1241- -
1242- -instance (Monoid w) => MonadTrans (RWST r w s) where
1243- -     lift m = RWST $ \_ s -> do
1244- -             a <- m
1245- -             return (a, s, mempty)
1246- -
1247- -instance (Monoid w, MonadIO m) => MonadIO (RWST r w s m) where
1248- -     liftIO = lift . liftIO
1249- -
1250- -
1251- -evalRWST :: (Monad m) => RWST r w s m a -> r -> s -> m (a, w)
1252- -evalRWST m r s = do
1253- -    (a, _, w) <- runRWST m r s
1254- -    return (a, w)
1255- -
1256- -execRWST :: (Monad m) => RWST r w s m a -> r -> s -> m (s, w)
1257- -execRWST m r s = do
1258- -    (_, s', w) <- runRWST m r s
1259- -    return (s', w)
1260- -
1261- -mapRWST :: (m (a, s, w) -> n (b, s, w')) -> RWST r w s m a -> RWST r w' s n b
1262- -mapRWST f m = RWST $ \r s -> f (runRWST m r s)
1263+import Control.Monad.RWS.Lazy
1264hunk ./Control/Monad/RWS.hs 27
1265- -withRWST :: (r' -> s -> (r, s)) -> RWST r w s m a -> RWST r' w s m a
1266- -withRWST f m = RWST $ \r s -> uncurry (runRWST m) (f r s)
1267hunk ./Control/Monad/Reader/Class.hs 1
1268+{-# OPTIONS -fallow-undecidable-instances #-}
1269+-- Search for -fallow-undecidable-instances to see why this is needed
1270+-----------------------------------------------------------------------------
1271+-- |
1272+-- Module      :  Control.Monad.Reader.Class
1273+-- Copyright   :  (c) Andy Gill 2001,
1274+--                (c) Oregon Graduate Institute of Science and Technology, 2001
1275+-- License     :  BSD-style (see the file libraries/base/LICENSE)
1276+--
1277+-- Maintainer  :  libraries@haskell.org
1278+-- Stability   :  experimental
1279+-- Portability :  non-portable (multi-param classes, functional dependencies)
1280+--
1281+-- MonadReader class.
1282+--
1283+--      Inspired by the paper
1284+--      /Functional Programming with Overloading and
1285+--          Higher-Order Polymorphism/,
1286+--        Mark P Jones (<http://www.cse.ogi.edu/~mpj/>)
1287+--          Advanced School of Functional Programming, 1995.
1288+-----------------------------------------------------------------------------
1289+
1290+module Control.Monad.Reader.Class (
1291+    MonadReader(..),
1292+    asks,
1293+    ) where
1294+
1295+import Prelude
1296+
1297+-- ----------------------------------------------------------------------------
1298+-- class MonadReader
1299+--  asks for the internal (non-mutable) state.
1300+
1301+class (Monad m) => MonadReader r m | m -> r where
1302+    ask   :: m r
1303+    local :: (r -> r) -> m a -> m a
1304+
1305+-- This allows you to provide a projection function.
1306+
1307+asks :: (MonadReader r m) => (r -> a) -> m a
1308+asks f = do
1309+    r <- ask
1310+    return (f r)
1311+
1312hunk ./Control/Monad/Reader.hs 1
1313+{-# OPTIONS -fallow-undecidable-instances #-}
1314+-- Search for -fallow-undecidable-instances to see why this is needed
1315hunk ./Control/Monad/Reader.hs 7
1316- ---             (c) Oregon Graduate Institute of Science and Technology, 2001
1317+--                (c) Oregon Graduate Institute of Science and Technology, 2001
1318hunk ./Control/Monad/Reader.hs 9
1319- ---
1320+--
1321hunk ./Control/Monad/Reader.hs 14
1322- --- Declaration of the Monoid class,and instances for list and functions
1323+-- Declaration of the MonadReader class
1324hunk ./Control/Monad/Reader.hs 16
1325- ---     Inspired by the paper
1326- ---     /Functional Programming with Overloading and
1327- ---         Higher-Order Polymorphism/,
1328- ---       Mark P Jones (<http://www.cse.ogi.edu/~mpj/>)
1329- ---             Advanced School of Functional Programming, 1995.
1330+--      Inspired by the paper
1331+--      /Functional Programming with Overloading and
1332+--          Higher-Order Polymorphism/,
1333+--        Mark P Jones (<http://www.cse.ogi.edu/~mpj/>)
1334+--          Advanced School of Functional Programming, 1995.
1335hunk ./Control/Monad/Reader.hs 24
1336- -     MonadReader(..),
1337- -     asks,
1338- -     Reader(..),
1339- -     mapReader,
1340- -     withReader,
1341- -     ReaderT(..),
1342- -     mapReaderT,
1343- -     withReaderT,
1344- -     module Control.Monad,
1345- -     module Control.Monad.Fix,
1346- -     module Control.Monad.Trans,
1347- -     ) where
1348+    module Control.Monad.Reader.Class,
1349+    Reader(..),
1350+    mapReader,
1351+    withReader,
1352+    ReaderT(..),
1353+    mapReaderT,
1354+    withReaderT,
1355+    module Control.Monad,
1356+    module Control.Monad.Fix,
1357+    module Control.Monad.Trans,
1358+    ) where
1359hunk ./Control/Monad/Reader.hs 39
1360+import Control.Monad.Cont.Class
1361+import Control.Monad.Error.Class
1362hunk ./Control/Monad/Reader.hs 42
1363- -import Control.Monad.Trans
1364hunk ./Control/Monad/Reader.hs 43
1365- -
1366- --- ----------------------------------------------------------------------------
1367- --- class MonadReader
1368- ---  asks for the internal (non-mutable) state.
1369- -
1370- -class (Monad m) => MonadReader r m | m -> r where
1371- -     ask   :: m r
1372- -     local :: (r -> r) -> m a -> m a
1373- -
1374- --- This allows you to provide a projection function.
1375- -
1376- -asks :: (MonadReader r m) => (r -> a) -> m a
1377- -asks f = do
1378- -     r <- ask
1379- -     return (f r)
1380+import Control.Monad.Reader.Class
1381+import Control.Monad.State.Class
1382+import Control.Monad.Trans
1383+import Control.Monad.Writer.Class
1384hunk ./Control/Monad/Reader.hs 52
1385- -     ask       = id
1386- -     local f m = m . f
1387+    ask       = id
1388+    local f m = m . f
1389hunk ./Control/Monad/Reader.hs 60
1390- -instance Functor (Reader r) where
1391- -     fmap f m = Reader $ \r -> f (runReader m r)
1392- -
1393- -instance Monad (Reader r) where
1394- -     return a = Reader $ \_ -> a
1395- -     m >>= k  = Reader $ \r -> runReader (k (runReader m r)) r
1396- -
1397- -instance MonadFix (Reader r) where
1398- -     mfix f = Reader $ \r -> let a = runReader (f a) r in a
1399- -
1400- -instance MonadReader r (Reader r) where
1401- -     ask       = Reader id
1402- -     local f m = Reader $ runReader m . f
1403- -
1404hunk ./Control/Monad/Reader.hs 68
1405+instance Functor (Reader r) where
1406+    fmap f m = Reader $ \r -> f (runReader m r)
1407+
1408+instance Monad (Reader r) where
1409+    return a = Reader $ \_ -> a
1410+    m >>= k  = Reader $ \r -> runReader (k (runReader m r)) r
1411+
1412+instance MonadFix (Reader r) where
1413+    mfix f = Reader $ \r -> let a = runReader (f a) r in a
1414+
1415+instance MonadReader r (Reader r) where
1416+    ask       = Reader id
1417+    local f m = Reader $ runReader m . f
1418+
1419hunk ./Control/Monad/Reader.hs 87
1420+mapReaderT :: (m a -> n b) -> ReaderT w m a -> ReaderT w n b
1421+mapReaderT f m = ReaderT $ f . runReaderT m
1422+
1423+withReaderT :: (r' -> r) -> ReaderT r m a -> ReaderT r' m a
1424+withReaderT f m = ReaderT $ runReaderT m . f
1425+
1426hunk ./Control/Monad/Reader.hs 94
1427- -     fmap f m = ReaderT $ \r -> do
1428- -             a <- runReaderT m r
1429- -             return (f a)
1430+    fmap f m = ReaderT $ \r -> do
1431+        a <- runReaderT m r
1432+        return (f a)
1433hunk ./Control/Monad/Reader.hs 99
1434- -     return a = ReaderT $ \_ -> return a
1435- -     m >>= k  = ReaderT $ \r -> do
1436- -             a <- runReaderT m r
1437- -             runReaderT (k a) r
1438- -     fail msg = ReaderT $ \_ -> fail msg
1439+    return a = ReaderT $ \_ -> return a
1440+    m >>= k  = ReaderT $ \r -> do
1441+        a <- runReaderT m r
1442+        runReaderT (k a) r
1443+    fail msg = ReaderT $ \_ -> fail msg
1444hunk ./Control/Monad/Reader.hs 106
1445- -     mzero       = ReaderT $ \_ -> mzero
1446- -     m `mplus` n = ReaderT $ \r -> runReaderT m r `mplus` runReaderT n r
1447+    mzero       = ReaderT $ \_ -> mzero
1448+    m `mplus` n = ReaderT $ \r -> runReaderT m r `mplus` runReaderT n r
1449hunk ./Control/Monad/Reader.hs 110
1450- -     mfix f = ReaderT $ \r -> mfix $ \a -> runReaderT (f a) r
1451+    mfix f = ReaderT $ \r -> mfix $ \a -> runReaderT (f a) r
1452hunk ./Control/Monad/Reader.hs 113
1453- -     ask       = ReaderT return
1454- -     local f m = ReaderT $ \r -> runReaderT m (f r)
1455+    ask       = ReaderT return
1456+    local f m = ReaderT $ \r -> runReaderT m (f r)
1457+
1458+-- ---------------------------------------------------------------------------
1459+-- Instances for other mtl transformers
1460hunk ./Control/Monad/Reader.hs 120
1461- -     lift m = ReaderT $ \_ -> m
1462+    lift m = ReaderT $ \_ -> m
1463hunk ./Control/Monad/Reader.hs 123
1464- -     liftIO = lift . liftIO
1465+    liftIO = lift . liftIO
1466hunk ./Control/Monad/Reader.hs 125
1467- -mapReaderT :: (m a -> n b) -> ReaderT w m a -> ReaderT w n b
1468- -mapReaderT f m = ReaderT $ f . runReaderT m
1469+instance (MonadCont m) => MonadCont (ReaderT r m) where
1470+    callCC f = ReaderT $ \r ->
1471+        callCC $ \c ->
1472+        runReaderT (f (\a -> ReaderT $ \_ -> c a)) r
1473+
1474+instance (MonadError e m) => MonadError e (ReaderT r m) where
1475+    throwError       = lift . throwError
1476+    m `catchError` h = ReaderT $ \r -> runReaderT m r
1477+        `catchError` \e -> runReaderT (h e) r
1478+
1479+-- Needs -fallow-undecidable-instances
1480+instance (MonadState s m) => MonadState s (ReaderT r m) where
1481+    get = lift get
1482+    put = lift . put
1483+
1484+-- This instance needs -fallow-undecidable-instances, because
1485+-- it does not satisfy the coverage condition
1486+instance (MonadWriter w m) => MonadWriter w (ReaderT r m) where
1487+    tell     = lift . tell
1488+    listen m = ReaderT $ \w -> listen (runReaderT m w)
1489+    pass   m = ReaderT $ \w -> pass   (runReaderT m w)
1490hunk ./Control/Monad/Reader.hs 147
1491- -withReaderT :: (r' -> r) -> ReaderT r m a -> ReaderT r' m a
1492- -withReaderT f m = ReaderT $ runReaderT m . f
1493hunk ./Control/Monad/State/Class.hs 1
1494+-----------------------------------------------------------------------------
1495+-- |
1496+-- Module      :  Control.Monad.State.Class
1497+-- Copyright   :  (c) Andy Gill 2001,
1498+--                (c) Oregon Graduate Institute of Science and Technology, 2001
1499+-- License     :  BSD-style (see the file libraries/base/LICENSE)
1500+--
1501+-- Maintainer  :  libraries@haskell.org
1502+-- Stability   :  experimental
1503+-- Portability :  non-portable (multi-param classes, functional dependencies)
1504+--
1505+-- MonadState class.
1506+--
1507+--      This module is inspired by the paper
1508+--      /Functional Programming with Overloading and
1509+--          Higher-Order Polymorphism/,
1510+--        Mark P Jones (<http://www.cse.ogi.edu/~mpj/>)
1511+--          Advanced School of Functional Programming, 1995.
1512+
1513+-----------------------------------------------------------------------------
1514+
1515+module Control.Monad.State.Class (
1516+    -- * MonadState class
1517+    MonadState(..),
1518+    modify,
1519+    gets,
1520+  ) where
1521+
1522+-- ---------------------------------------------------------------------------
1523+-- | /get/ returns the state from the internals of the monad.
1524+--
1525+-- /put/ replaces the state inside the monad.
1526+
1527+class (Monad m) => MonadState s m | m -> s where
1528+    get :: m s
1529+    put :: s -> m ()
1530+
1531+-- | Monadic state transformer.
1532+--
1533+--      Maps an old state to a new state inside a state monad.
1534+--      The old state is thrown away.
1535+--
1536+-- >      Main> :t modify ((+1) :: Int -> Int)
1537+-- >      modify (...) :: (MonadState Int a) => a ()
1538+--
1539+--    This says that @modify (+1)@ acts over any
1540+--    Monad that is a member of the @MonadState@ class,
1541+--    with an @Int@ state.
1542+
1543+modify :: (MonadState s m) => (s -> s) -> m ()
1544+modify f = do
1545+    s <- get
1546+    put (f s)
1547+
1548+-- | Gets specific component of the state, using a projection function
1549+-- supplied.
1550+
1551+gets :: (MonadState s m) => (s -> a) -> m a
1552+gets f = do
1553+    s <- get
1554+    return (f s)
1555+
1556hunk ./Control/Monad/State/Lazy.hs 1
1557+{-# OPTIONS -fallow-undecidable-instances #-}
1558+-- Search for -fallow-undecidable-instances to see why this is needed
1559+
1560+-----------------------------------------------------------------------------
1561+-- |
1562+-- Module      :  Control.Monad.State.Lazy
1563+-- Copyright   :  (c) Andy Gill 2001,
1564+--                (c) Oregon Graduate Institute of Science and Technology, 2001
1565+-- License     :  BSD-style (see the file libraries/base/LICENSE)
1566+--
1567+-- Maintainer  :  libraries@haskell.org
1568+-- Stability   :  experimental
1569+-- Portability :  non-portable (multi-param classes, functional dependencies)
1570+--
1571+-- Lazy state monads.
1572+--
1573+--      This module is inspired by the paper
1574+--      /Functional Programming with Overloading and
1575+--          Higher-Order Polymorphism/,
1576+--        Mark P Jones (<http://www.cse.ogi.edu/~mpj/>)
1577+--          Advanced School of Functional Programming, 1995.
1578+--
1579+-- See below for examples.
1580+
1581+-----------------------------------------------------------------------------
1582+
1583+module Control.Monad.State.Lazy (
1584+    module Control.Monad.State.Class,
1585+    -- * The State Monad
1586+    State(..),
1587+    evalState,
1588+    execState,
1589+    mapState,
1590+    withState,
1591+    -- * The StateT Monad
1592+    StateT(..),
1593+    evalStateT,
1594+    execStateT,
1595+    mapStateT,
1596+    withStateT,
1597+    module Control.Monad,
1598+    module Control.Monad.Fix,
1599+    module Control.Monad.Trans,
1600+    -- * Examples
1601+    -- $examples
1602+  ) where
1603+
1604+import Prelude
1605+
1606+import Control.Monad
1607+import Control.Monad.Cont.Class
1608+import Control.Monad.Error.Class
1609+import Control.Monad.Fix
1610+import Control.Monad.Reader.Class
1611+import Control.Monad.State.Class
1612+import Control.Monad.Trans
1613+import Control.Monad.Writer.Class
1614+
1615+-- ---------------------------------------------------------------------------
1616+-- | A parameterizable state monad where /s/ is the type of the state
1617+-- to carry and /a/ is the type of the /return value/.
1618+
1619+newtype State s a = State { runState :: s -> (a, s) }
1620+
1621+-- |Evaluate this state monad with the given initial state,throwing
1622+-- away the final state.  Very much like @fst@ composed with
1623+-- @runstate@.
1624+
1625+evalState :: State s a -- ^The state to evaluate
1626+          -> s         -- ^An initial value
1627+          -> a         -- ^The return value of the state application
1628+evalState m s = fst (runState m s)
1629+
1630+-- |Execute this state and return the new state, throwing away the
1631+-- return value.  Very much like @snd@ composed with
1632+-- @runstate@.
1633+
1634+execState :: State s a -- ^The state to evaluate
1635+          -> s         -- ^An initial value
1636+          -> s         -- ^The new state
1637+execState m s = snd (runState m s)
1638+
1639+-- |Map a stateful computation from one (return value, state) pair to
1640+-- another.  For instance, to convert numberTree from a function that
1641+-- returns a tree to a function that returns the sum of the numbered
1642+-- tree (see the Examples section for numberTree and sumTree) you may
1643+-- write:
1644+--
1645+-- > sumNumberedTree :: (Eq a) => Tree a -> State (Table a) Int
1646+-- > sumNumberedTree = mapState (\ (t, tab) -> (sumTree t, tab))  . numberTree
1647+
1648+mapState :: ((a, s) -> (b, s)) -> State s a -> State s b
1649+mapState f m = State $ f . runState m
1650+
1651+-- |Apply this function to this state and return the resulting state.
1652+withState :: (s -> s) -> State s a -> State s a
1653+withState f m = State $ runState m . f
1654+
1655+instance Functor (State s) where
1656+    fmap f m = State $ \s -> let
1657+        (a, s') = runState m s
1658+        in (f a, s')
1659+
1660+instance Monad (State s) where
1661+    return a = State $ \s -> (a, s)
1662+    m >>= k  = State $ \s -> let
1663+        (a, s') = runState m s
1664+        in runState (k a) s'
1665+
1666+instance MonadFix (State s) where
1667+    mfix f = State $ \s -> let (a, s') = runState (f a) s in (a, s')
1668+
1669+instance MonadState s (State s) where
1670+    get   = State $ \s -> (s, s)
1671+    put s = State $ \_ -> ((), s)
1672+
1673+-- ---------------------------------------------------------------------------
1674+-- | A parameterizable state monad for encapsulating an inner
1675+-- monad.
1676+--
1677+-- The StateT Monad structure is parameterized over two things:
1678+--
1679+--   * s - The state.
1680+--
1681+--   * m - The inner monad.
1682+--
1683+-- Here are some examples of use:
1684+--
1685+-- (Parser from ParseLib with Hugs)
1686+--
1687+-- >  type Parser a = StateT String [] a
1688+-- >     ==> StateT (String -> [(a,String)])
1689+--
1690+-- For example, item can be written as:
1691+--
1692+-- >   item = do (x:xs) <- get
1693+-- >          put xs
1694+-- >          return x
1695+-- >
1696+-- >   type BoringState s a = StateT s Indentity a
1697+-- >        ==> StateT (s -> Identity (a,s))
1698+-- >
1699+-- >   type StateWithIO s a = StateT s IO a
1700+-- >        ==> StateT (s -> IO (a,s))
1701+-- >
1702+-- >   type StateWithErr s a = StateT s Maybe a
1703+-- >        ==> StateT (s -> Maybe (a,s))
1704+
1705+newtype StateT s m a = StateT { runStateT :: s -> m (a,s) }
1706+
1707+-- |Similar to 'evalState'
1708+evalStateT :: (Monad m) => StateT s m a -> s -> m a
1709+evalStateT m s = do
1710+    ~(a, _) <- runStateT m s
1711+    return a
1712+
1713+-- |Similar to 'execState'
1714+execStateT :: (Monad m) => StateT s m a -> s -> m s
1715+execStateT m s = do
1716+    ~(_, s') <- runStateT m s
1717+    return s'
1718+
1719+-- |Similar to 'mapState'
1720+mapStateT :: (m (a, s) -> n (b, s)) -> StateT s m a -> StateT s n b
1721+mapStateT f m = StateT $ f . runStateT m
1722+
1723+-- |Similar to 'withState'
1724+withStateT :: (s -> s) -> StateT s m a -> StateT s m a
1725+withStateT f m = StateT $ runStateT m . f
1726+
1727+instance (Monad m) => Functor (StateT s m) where
1728+    fmap f m = StateT $ \s -> do
1729+        ~(x, s') <- runStateT m s
1730+        return (f x, s')
1731+
1732+instance (Monad m) => Monad (StateT s m) where
1733+    return a = StateT $ \s -> return (a, s)
1734+    m >>= k  = StateT $ \s -> do
1735+        ~(a, s') <- runStateT m s
1736+        runStateT (k a) s'
1737+    fail str = StateT $ \_ -> fail str
1738+
1739+instance (MonadPlus m) => MonadPlus (StateT s m) where
1740+    mzero       = StateT $ \_ -> mzero
1741+    m `mplus` n = StateT $ \s -> runStateT m s `mplus` runStateT n s
1742+
1743+instance (MonadFix m) => MonadFix (StateT s m) where
1744+    mfix f = StateT $ \s -> mfix $ \ ~(a, _) -> runStateT (f a) s
1745+
1746+instance (Monad m) => MonadState s (StateT s m) where
1747+    get   = StateT $ \s -> return (s, s)
1748+    put s = StateT $ \_ -> return ((), s)
1749+
1750+-- ---------------------------------------------------------------------------
1751+-- Instances for other mtl transformers
1752+
1753+instance MonadTrans (StateT s) where
1754+    lift m = StateT $ \s -> do
1755+        a <- m
1756+        return (a, s)
1757+
1758+instance (MonadIO m) => MonadIO (StateT s m) where
1759+    liftIO = lift . liftIO
1760+
1761+instance (MonadCont m) => MonadCont (StateT s m) where
1762+    callCC f = StateT $ \s ->
1763+        callCC $ \c ->
1764+        runStateT (f (\a -> StateT $ \s' -> c (a, s'))) s
1765+
1766+instance (MonadError e m) => MonadError e (StateT s m) where
1767+    throwError       = lift . throwError
1768+    m `catchError` h = StateT $ \s -> runStateT m s
1769+        `catchError` \e -> runStateT (h e) s
1770+
1771+-- Needs -fallow-undecidable-instances
1772+instance (MonadReader r m) => MonadReader r (StateT s m) where
1773+    ask       = lift ask
1774+    local f m = StateT $ \s -> local f (runStateT m s)
1775+
1776+-- Needs -fallow-undecidable-instances
1777+instance (MonadWriter w m) => MonadWriter w (StateT s m) where
1778+    tell     = lift . tell
1779+    listen m = StateT $ \s -> do
1780+        ~((a, s'), w) <- listen (runStateT m s)
1781+        return ((a, w), s')
1782+    pass   m = StateT $ \s -> pass $ do
1783+        ~((a, f), s') <- runStateT m s
1784+        return ((a, s'), f)
1785+
1786+-- ---------------------------------------------------------------------------
1787+-- $examples
1788+-- A function to increment a counter.  Taken from the paper
1789+-- /Generalising Monads to Arrows/, John
1790+-- Hughes (<http://www.math.chalmers.se/~rjmh/>), November 1998:
1791+--
1792+-- > tick :: State Int Int
1793+-- > tick = do n <- get
1794+-- >           put (n+1)
1795+-- >           return n
1796+--
1797+-- Add one to the given number using the state monad:
1798+--
1799+-- > plusOne :: Int -> Int
1800+-- > plusOne n = execState tick n
1801+--
1802+-- A contrived addition example. Works only with positive numbers:
1803+--
1804+-- > plus :: Int -> Int -> Int
1805+-- > plus n x = execState (sequence $ replicate n tick) x
1806+--
1807+-- An example from /The Craft of Functional Programming/, Simon
1808+-- Thompson (<http://www.cs.kent.ac.uk/people/staff/sjt/>),
1809+-- Addison-Wesley 1999: \"Given an arbitrary tree, transform it to a
1810+-- tree of integers in which the original elements are replaced by
1811+-- natural numbers, starting from 0.  The same element has to be
1812+-- replaced by the same number at every occurrence, and when we meet
1813+-- an as-yet-unvisited element we have to find a \'new\' number to match
1814+-- it with:\"
1815+--
1816+-- > data Tree a = Nil | Node a (Tree a) (Tree a) deriving (Show, Eq)
1817+-- > type Table a = [a]
1818+--
1819+-- > numberTree :: Eq a => Tree a -> State (Table a) (Tree Int)
1820+-- > numberTree Nil = return Nil
1821+-- > numberTree (Node x t1 t2)
1822+-- >        =  do num <- numberNode x
1823+-- >              nt1 <- numberTree t1
1824+-- >              nt2 <- numberTree t2
1825+-- >              return (Node num nt1 nt2)
1826+-- >     where
1827+-- >     numberNode :: Eq a => a -> State (Table a) Int
1828+-- >     numberNode x
1829+-- >        = do table <- get
1830+-- >             (newTable, newPos) <- return (nNode x table)
1831+-- >             put newTable
1832+-- >             return newPos
1833+-- >     nNode::  (Eq a) => a -> Table a -> (Table a, Int)
1834+-- >     nNode x table
1835+-- >        = case (findIndexInList (== x) table) of
1836+-- >          Nothing -> (table ++ [x], length table)
1837+-- >          Just i  -> (table, i)
1838+-- >     findIndexInList :: (a -> Bool) -> [a] -> Maybe Int
1839+-- >     findIndexInList = findIndexInListHelp 0
1840+-- >     findIndexInListHelp _ _ [] = Nothing
1841+-- >     findIndexInListHelp count f (h:t)
1842+-- >        = if (f h)
1843+-- >          then Just count
1844+-- >          else findIndexInListHelp (count+1) f t
1845+--
1846+-- numTree applies numberTree with an initial state:
1847+--
1848+-- > numTree :: (Eq a) => Tree a -> Tree Int
1849+-- > numTree t = evalState (numberTree t) []
1850+--
1851+-- > testTree = Node "Zero" (Node "One" (Node "Two" Nil Nil) (Node "One" (Node "Zero" Nil Nil) Nil)) Nil
1852+-- > numTree testTree => Node 0 (Node 1 (Node 2 Nil Nil) (Node 1 (Node 0 Nil Nil) Nil)) Nil
1853+--
1854+-- sumTree is a little helper function that does not use the State monad:
1855+--
1856+-- > sumTree :: (Num a) => Tree a -> a
1857+-- > sumTree Nil = 0
1858+-- > sumTree (Node e t1 t2) = e + (sumTree t1) + (sumTree t2)
1859hunk ./Control/Monad/State/Strict.hs 1
1860+{-# OPTIONS -fallow-undecidable-instances #-}
1861+-- Search for -fallow-undecidable-instances to see why this is needed
1862+
1863+-----------------------------------------------------------------------------
1864+-- |
1865+-- Module      :  Control.Monad.State.Strict
1866+-- Copyright   :  (c) Andy Gill 2001,
1867+--           (c) Oregon Graduate Institute of Science and Technology, 2001
1868+-- License     :  BSD-style (see the file libraries/base/LICENSE)
1869+--
1870+-- Maintainer  :  libraries@haskell.org
1871+-- Stability   :  experimental
1872+-- Portability :  non-portable (multi-param classes, functional dependencies)
1873+--
1874+-- Strict state monads.
1875+--
1876+--      This module is inspired by the paper
1877+--      /Functional Programming with Overloading and
1878+--          Higher-Order Polymorphism/,
1879+--        Mark P Jones (<http://www.cse.ogi.edu/~mpj/>)
1880+--          Advanced School of Functional Programming, 1995.
1881+--
1882+-- See below for examples.
1883+
1884+-----------------------------------------------------------------------------
1885+
1886+module Control.Monad.State.Strict (
1887+    module Control.Monad.State.Class,
1888+    -- * The State Monad
1889+    State(..),
1890+    evalState,
1891+    execState,
1892+    mapState,
1893+    withState,
1894+    -- * The StateT Monad
1895+    StateT(..),
1896+    evalStateT,
1897+    execStateT,
1898+    mapStateT,
1899+    withStateT,
1900+    module Control.Monad,
1901+    module Control.Monad.Fix,
1902+    module Control.Monad.Trans,
1903+    -- * Examples
1904+    -- $examples
1905+  ) where
1906+
1907+import Prelude
1908+
1909+import Control.Monad
1910+import Control.Monad.Cont.Class
1911+import Control.Monad.Error.Class
1912+import Control.Monad.Fix
1913+import Control.Monad.Reader.Class
1914+import Control.Monad.State.Class
1915+import Control.Monad.Trans
1916+import Control.Monad.Writer.Class
1917+
1918+-- ---------------------------------------------------------------------------
1919+-- | A parameterizable state monad where /s/ is the type of the state
1920+-- to carry and /a/ is the type of the /return value/.
1921+
1922+newtype State s a = State { runState :: s -> (a, s) }
1923+
1924+-- |Evaluate this state monad with the given initial state,throwing
1925+-- away the final state.  Very much like @fst@ composed with
1926+-- @runstate@.
1927+
1928+evalState :: State s a -- ^The state to evaluate
1929+          -> s         -- ^An initial value
1930+          -> a         -- ^The return value of the state application
1931+evalState m s = fst (runState m s)
1932+
1933+-- |Execute this state and return the new state, throwing away the
1934+-- return value.  Very much like @snd@ composed with
1935+-- @runstate@.
1936+
1937+execState :: State s a -- ^The state to evaluate
1938+          -> s         -- ^An initial value
1939+          -> s         -- ^The new state
1940+execState m s = snd (runState m s)
1941+
1942+-- |Map a stateful computation from one (return value, state) pair to
1943+-- another.  For instance, to convert numberTree from a function that
1944+-- returns a tree to a function that returns the sum of the numbered
1945+-- tree (see the Examples section for numberTree and sumTree) you may
1946+-- write:
1947+--
1948+-- > sumNumberedTree :: (Eq a) => Tree a -> State (Table a) Int
1949+-- > sumNumberedTree = mapState (\ (t, tab) -> (sumTree t, tab))  . numberTree
1950+
1951+mapState :: ((a, s) -> (b, s)) -> State s a -> State s b
1952+mapState f m = State $ f . runState m
1953+
1954+-- |Apply this function to this state and return the resulting state.
1955+withState :: (s -> s) -> State s a -> State s a
1956+withState f m = State $ runState m . f
1957+
1958+
1959+instance Functor (State s) where
1960+    fmap f m = State $ \s -> case runState m s of
1961+                                 (a, s') -> (f a, s')
1962+
1963+instance Monad (State s) where
1964+    return a = State $ \s -> (a, s)
1965+    m >>= k  = State $ \s -> case runState m s of
1966+                                 (a, s') -> runState (k a) s'
1967+
1968+instance MonadFix (State s) where
1969+    mfix f = State $ \s -> let (a, s') = runState (f a) s in (a, s')
1970+
1971+instance MonadState s (State s) where
1972+    get   = State $ \s -> (s, s)
1973+    put s = State $ \_ -> ((), s)
1974+
1975+-- ---------------------------------------------------------------------------
1976+-- | A parameterizable state monad for encapsulating an inner
1977+-- monad.
1978+--
1979+-- The StateT Monad structure is parameterized over two things:
1980+--
1981+--   * s - The state.
1982+--
1983+--   * m - The inner monad.
1984+--
1985+-- Here are some examples of use:
1986+--
1987+-- (Parser from ParseLib with Hugs)
1988+--
1989+-- >  type Parser a = StateT String [] a
1990+-- >     ==> StateT (String -> [(a,String)])
1991+--
1992+-- For example, item can be written as:
1993+--
1994+-- >   item = do (x:xs) <- get
1995+-- >          put xs
1996+-- >          return x
1997+-- >
1998+-- >   type BoringState s a = StateT s Indentity a
1999+-- >        ==> StateT (s -> Identity (a,s))
2000+-- >
2001+-- >   type StateWithIO s a = StateT s IO a
2002+-- >        ==> StateT (s -> IO (a,s))
2003+-- >
2004+-- >   type StateWithErr s a = StateT s Maybe a
2005+-- >        ==> StateT (s -> Maybe (a,s))
2006+
2007+newtype StateT s m a = StateT { runStateT :: s -> m (a,s) }
2008+
2009+-- |Similar to 'evalState'
2010+evalStateT :: (Monad m) => StateT s m a -> s -> m a
2011+evalStateT m s = do
2012+    (a, _) <- runStateT m s
2013+    return a
2014+
2015+-- |Similar to 'execState'
2016+execStateT :: (Monad m) => StateT s m a -> s -> m s
2017+execStateT m s = do
2018+    (_, s') <- runStateT m s
2019+    return s'
2020+
2021+-- |Similar to 'mapState'
2022+mapStateT :: (m (a, s) -> n (b, s)) -> StateT s m a -> StateT s n b
2023+mapStateT f m = StateT $ f . runStateT m
2024+
2025+-- |Similar to 'withState'
2026+withStateT :: (s -> s) -> StateT s m a -> StateT s m a
2027+withStateT f m = StateT $ runStateT m . f
2028+
2029+instance (Monad m) => Functor (StateT s m) where
2030+    fmap f m = StateT $ \s -> do
2031+        (x, s') <- runStateT m s
2032+        return (f x, s')
2033+
2034+instance (Monad m) => Monad (StateT s m) where
2035+    return a = StateT $ \s -> return (a, s)
2036+    m >>= k  = StateT $ \s -> do
2037+        (a, s') <- runStateT m s
2038+        runStateT (k a) s'
2039+    fail str = StateT $ \_ -> fail str
2040+
2041+instance (MonadPlus m) => MonadPlus (StateT s m) where
2042+    mzero       = StateT $ \_ -> mzero
2043+    m `mplus` n = StateT $ \s -> runStateT m s `mplus` runStateT n s
2044+
2045+instance (MonadFix m) => MonadFix (StateT s m) where
2046+    mfix f = StateT $ \s -> mfix $ \ ~(a, _) -> runStateT (f a) s
2047+
2048+instance (Monad m) => MonadState s (StateT s m) where
2049+    get   = StateT $ \s -> return (s, s)
2050+    put s = StateT $ \_ -> return ((), s)
2051+
2052+-- ---------------------------------------------------------------------------
2053+-- Instances for other mtl transformers
2054+
2055+instance MonadTrans (StateT s) where
2056+    lift m = StateT $ \s -> do
2057+        a <- m
2058+        return (a, s)
2059+
2060+instance (MonadIO m) => MonadIO (StateT s m) where
2061+    liftIO = lift . liftIO
2062+
2063+instance (MonadCont m) => MonadCont (StateT s m) where
2064+    callCC f = StateT $ \s ->
2065+        callCC $ \c ->
2066+        runStateT (f (\a -> StateT $ \s' -> c (a, s'))) s
2067+
2068+instance (MonadError e m) => MonadError e (StateT s m) where
2069+    throwError       = lift . throwError
2070+    m `catchError` h = StateT $ \s -> runStateT m s
2071+        `catchError` \e -> runStateT (h e) s
2072+
2073+-- Needs -fallow-undecidable-instances
2074+instance (MonadReader r m) => MonadReader r (StateT s m) where
2075+    ask       = lift ask
2076+    local f m = StateT $ \s -> local f (runStateT m s)
2077+
2078+-- Needs -fallow-undecidable-instances
2079+instance (MonadWriter w m) => MonadWriter w (StateT s m) where
2080+    tell     = lift . tell
2081+    listen m = StateT $ \s -> do
2082+        ((a, s'), w) <- listen (runStateT m s)
2083+        return ((a, w), s')
2084+    pass   m = StateT $ \s -> pass $ do
2085+        ((a, f), s') <- runStateT m s
2086+        return ((a, s'), f)
2087+
2088+-- ---------------------------------------------------------------------------
2089+-- $examples
2090+-- A function to increment a counter.  Taken from the paper
2091+-- /Generalising Monads to Arrows/, John
2092+-- Hughes (<http://www.math.chalmers.se/~rjmh/>), November 1998:
2093+--
2094+-- > tick :: State Int Int
2095+-- > tick = do n <- get
2096+-- >           put (n+1)
2097+-- >           return n
2098+--
2099+-- Add one to the given number using the state monad:
2100+--
2101+-- > plusOne :: Int -> Int
2102+-- > plusOne n = execState tick n
2103+--
2104+-- A contrived addition example. Works only with positive numbers:
2105+--
2106+-- > plus :: Int -> Int -> Int
2107+-- > plus n x = execState (sequence $ replicate n tick) x
2108+--
2109+-- An example from /The Craft of Functional Programming/, Simon
2110+-- Thompson (<http://www.cs.kent.ac.uk/people/staff/sjt/>),
2111+-- Addison-Wesley 1999: \"Given an arbitrary tree, transform it to a
2112+-- tree of integers in which the original elements are replaced by
2113+-- natural numbers, starting from 0.  The same element has to be
2114+-- replaced by the same number at every occurrence, and when we meet
2115+-- an as-yet-unvisited element we have to find a \'new\' number to match
2116+-- it with:\"
2117+--
2118+-- > data Tree a = Nil | Node a (Tree a) (Tree a) deriving (Show, Eq)
2119+-- > type Table a = [a]
2120+--
2121+-- > numberTree :: Eq a => Tree a -> State (Table a) (Tree Int)
2122+-- > numberTree Nil = return Nil
2123+-- > numberTree (Node x t1 t2)
2124+-- >        =  do num <- numberNode x
2125+-- >              nt1 <- numberTree t1
2126+-- >              nt2 <- numberTree t2
2127+-- >              return (Node num nt1 nt2)
2128+-- >     where
2129+-- >     numberNode :: Eq a => a -> State (Table a) Int
2130+-- >     numberNode x
2131+-- >        = do table <- get
2132+-- >             (newTable, newPos) <- return (nNode x table)
2133+-- >             put newTable
2134+-- >             return newPos
2135+-- >     nNode::  (Eq a) => a -> Table a -> (Table a, Int)
2136+-- >     nNode x table
2137+-- >        = case (findIndexInList (== x) table) of
2138+-- >          Nothing -> (table ++ [x], length table)
2139+-- >          Just i  -> (table, i)
2140+-- >     findIndexInList :: (a -> Bool) -> [a] -> Maybe Int
2141+-- >     findIndexInList = findIndexInListHelp 0
2142+-- >     findIndexInListHelp _ _ [] = Nothing
2143+-- >     findIndexInListHelp count f (h:t)
2144+-- >        = if (f h)
2145+-- >          then Just count
2146+-- >          else findIndexInListHelp (count+1) f t
2147+--
2148+-- numTree applies numberTree with an initial state:
2149+--
2150+-- > numTree :: (Eq a) => Tree a -> Tree Int
2151+-- > numTree t = evalState (numberTree t) []
2152+--
2153+-- > testTree = Node "Zero" (Node "One" (Node "Two" Nil Nil) (Node "One" (Node "Zero" Nil Nil) Nil)) Nil
2154+-- > numTree testTree => Node 0 (Node 1 (Node 2 Nil Nil) (Node 1 (Node 0 Nil Nil) Nil)) Nil
2155+--
2156+-- sumTree is a little helper function that does not use the State monad:
2157+--
2158+-- > sumTree :: (Num a) => Tree a -> a
2159+-- > sumTree Nil = 0
2160+-- > sumTree (Node e t1 t2) = e + (sumTree t1) + (sumTree t2)
2161hunk ./Control/Monad/State.hs 1
2162- -{-# OPTIONS -fallow-undecidable-instances #-}
2163- --- Search for -fallow-undecidable-instances to see why this is needed
2164- -
2165hunk ./Control/Monad/State.hs 5
2166- ---             (c) Oregon Graduate Institute of Science and Technology, 2001
2167+--                (c) Oregon Graduate Institute of Science and Technology, 2001
2168hunk ./Control/Monad/State.hs 7
2169- ---
2170+--
2171hunk ./Control/Monad/State.hs 14
2172- ---     This module is inspired by the paper
2173- ---     /Functional Programming with Overloading and
2174- ---         Higher-Order Polymorphism/,
2175- ---       Mark P Jones (<http://www.cse.ogi.edu/~mpj/>)
2176- ---             Advanced School of Functional Programming, 1995.
2177- ---
2178- --- See below for examples.
2179+--      This module is inspired by the paper
2180+--      /Functional Programming with Overloading and
2181+--          Higher-Order Polymorphism/,
2182+--        Mark P Jones (<http://www.cse.ogi.edu/~mpj/>)
2183+--          Advanced School of Functional Programming, 1995.
2184hunk ./Control/Monad/State.hs 23
2185- -     -- * MonadState class
2186- -     MonadState(..),
2187- -     modify,
2188- -     gets,
2189- -     -- * The State Monad
2190- -     State(..),
2191- -     evalState,
2192- -     execState,
2193- -     mapState,
2194- -     withState,
2195- -     -- * The StateT Monad
2196- -     StateT(..),
2197- -     evalStateT,
2198- -     execStateT,
2199- -     mapStateT,
2200- -     withStateT,
2201- -     module Control.Monad,
2202- -     module Control.Monad.Fix,
2203- -     module Control.Monad.Trans,
2204- -     -- * Examples
2205- -     -- $examples
2206+  module Control.Monad.State.Lazy
2207hunk ./Control/Monad/State.hs 26
2208- -import Prelude
2209- -
2210- -import Control.Monad
2211- -import Control.Monad.Fix
2212- -import Control.Monad.Trans
2213- -import Control.Monad.Reader
2214- -import Control.Monad.Writer
2215- -
2216- --- ---------------------------------------------------------------------------
2217- --- | /get/ returns the state from the internals of the monad.
2218- ---
2219- --- /put/ replaces the state inside the monad.
2220- -
2221- -class (Monad m) => MonadState s m | m -> s where
2222- -     get :: m s
2223- -     put :: s -> m ()
2224- -
2225- --- | Monadic state transformer.
2226- ---
2227- ---      Maps an old state to a new state inside a state monad.
2228- ---      The old state is thrown away.
2229- ---
2230- --- >   Main> :t modify ((+1) :: Int -> Int)
2231- --- >   modify (...) :: (MonadState Int a) => a ()
2232- ---
2233- ---   This says that @modify (+1)@ acts over any
2234- ---   Monad that is a member of the @MonadState@ class,
2235- ---   with an @Int@ state.
2236- -
2237- -modify :: (MonadState s m) => (s -> s) -> m ()
2238- -modify f = do
2239- -     s <- get
2240- -     put (f s)
2241- -
2242- --- | Gets specific component of the state, using a projection function
2243- --- supplied.
2244- -
2245- -gets :: (MonadState s m) => (s -> a) -> m a
2246- -gets f = do
2247- -     s <- get
2248- -     return (f s)
2249- -
2250- --- ---------------------------------------------------------------------------
2251- --- | A parameterizable state monad where /s/ is the type of the state
2252- --- to carry and /a/ is the type of the /return value/.
2253- -
2254- -newtype State s a = State { runState :: s -> (a, s) }
2255- -
2256- --- The State Monad structure is parameterized over just the state.
2257- -
2258- -instance Functor (State s) where
2259- -     fmap f m = State $ \s -> let
2260- -             (a, s') = runState m s
2261- -             in (f a, s')
2262- -
2263- -instance Monad (State s) where
2264- -     return a = State $ \s -> (a, s)
2265- -     m >>= k  = State $ \s -> let
2266- -             (a, s') = runState m s
2267- -             in runState (k a) s'
2268- -
2269- -instance MonadFix (State s) where
2270- -     mfix f = State $ \s -> let (a, s') = runState (f a) s in (a, s')
2271- -
2272- -instance MonadState s (State s) where
2273- -     get   = State $ \s -> (s, s)
2274- -     put s = State $ \_ -> ((), s)
2275- -
2276- --- |Evaluate this state monad with the given initial state,throwing
2277- --- away the final state.  Very much like @fst@ composed with
2278- --- @runstate@.
2279- -
2280- -evalState :: State s a -- ^The state to evaluate
2281- -       -> s         -- ^An initial value
2282- -       -> a         -- ^The return value of the state application
2283- -evalState m s = fst (runState m s)
2284- -
2285- --- |Execute this state and return the new state, throwing away the
2286- --- return value.  Very much like @snd@ composed with
2287- --- @runstate@.
2288- -
2289- -execState :: State s a -- ^The state to evaluate
2290- -       -> s         -- ^An initial value
2291- -       -> s         -- ^The new state
2292- -execState m s = snd (runState m s)
2293- -
2294- --- |Map a stateful computation from one (return value, state) pair to
2295- --- another.  For instance, to convert numberTree from a function that
2296- --- returns a tree to a function that returns the sum of the numbered
2297- --- tree (see the Examples section for numberTree and sumTree) you may
2298- --- write:
2299- ---
2300- --- > sumNumberedTree :: (Eq a) => Tree a -> State (Table a) Int
2301- --- > sumNumberedTree = mapState (\ (t, tab) -> (sumTree t, tab))  . numberTree
2302- -
2303- -mapState :: ((a, s) -> (b, s)) -> State s a -> State s b
2304- -mapState f m = State $ f . runState m
2305- -
2306- --- |Apply this function to this state and return the resulting state.
2307- -withState :: (s -> s) -> State s a -> State s a
2308- -withState f m = State $ runState m . f
2309- -
2310- --- ---------------------------------------------------------------------------
2311- --- | A parameterizable state monad for encapsulating an inner
2312- --- monad.
2313- ---
2314- --- The StateT Monad structure is parameterized over two things:
2315- ---
2316- ---   * s - The state.
2317- ---
2318- ---   * m - The inner monad.
2319- ---
2320- --- Here are some examples of use:
2321- ---
2322- --- (Parser from ParseLib with Hugs)
2323- ---
2324- --- >  type Parser a = StateT String [] a
2325- --- >     ==> StateT (String -> [(a,String)])
2326- ---
2327- --- For example, item can be written as:
2328- ---
2329- --- >   item = do (x:xs) <- get
2330- --- >          put xs
2331- --- >          return x
2332- --- >
2333- --- >   type BoringState s a = StateT s Indentity a
2334- --- >        ==> StateT (s -> Identity (a,s))
2335- --- >
2336- --- >   type StateWithIO s a = StateT s IO a
2337- --- >        ==> StateT (s -> IO (a,s))
2338- --- >
2339- --- >   type StateWithErr s a = StateT s Maybe a
2340- --- >        ==> StateT (s -> Maybe (a,s))
2341- -
2342- -newtype StateT s m a = StateT { runStateT :: s -> m (a,s) }
2343- -
2344- -instance (Monad m) => Functor (StateT s m) where
2345- -     fmap f m = StateT $ \s -> do
2346- -             (x, s') <- runStateT m s
2347- -             return (f x, s')
2348- -
2349- -instance (Monad m) => Monad (StateT s m) where
2350- -     return a = StateT $ \s -> return (a, s)
2351- -     m >>= k  = StateT $ \s -> do
2352- -             (a, s') <- runStateT m s
2353- -             runStateT (k a) s'
2354- -     fail str = StateT $ \_ -> fail str
2355- -
2356- -instance (MonadPlus m) => MonadPlus (StateT s m) where
2357- -     mzero       = StateT $ \_ -> mzero
2358- -     m `mplus` n = StateT $ \s -> runStateT m s `mplus` runStateT n s
2359- -
2360- -instance (MonadFix m) => MonadFix (StateT s m) where
2361- -     mfix f = StateT $ \s -> mfix $ \ ~(a, _) -> runStateT (f a) s
2362- -
2363- -instance (Monad m) => MonadState s (StateT s m) where
2364- -     get   = StateT $ \s -> return (s, s)
2365- -     put s = StateT $ \_ -> return ((), s)
2366- -
2367- -instance MonadTrans (StateT s) where
2368- -     lift m = StateT $ \s -> do
2369- -             a <- m
2370- -             return (a, s)
2371- -
2372- -instance (MonadIO m) => MonadIO (StateT s m) where
2373- -     liftIO = lift . liftIO
2374- -
2375- --- Needs -fallow-undecidable-instances
2376- -instance (MonadReader r m) => MonadReader r (StateT s m) where
2377- -     ask       = lift ask
2378- -     local f m = StateT $ \s -> local f (runStateT m s)
2379- -
2380- --- Needs -fallow-undecidable-instances
2381- -instance (MonadWriter w m) => MonadWriter w (StateT s m) where
2382- -     tell     = lift . tell
2383- -     listen m = StateT $ \s -> do
2384- -             ((a, s'), w) <- listen (runStateT m s)
2385- -             return ((a, w), s')
2386- -     pass   m = StateT $ \s -> pass $ do
2387- -             ((a, f), s') <- runStateT m s
2388- -             return ((a, s'), f)
2389- -
2390- --- |Similar to 'evalState'
2391- -evalStateT :: (Monad m) => StateT s m a -> s -> m a
2392- -evalStateT m s = do
2393- -     (a, _) <- runStateT m s
2394- -     return a
2395- -
2396- --- |Similar to 'execState'
2397- -execStateT :: (Monad m) => StateT s m a -> s -> m s
2398- -execStateT m s = do
2399- -     (_, s') <- runStateT m s
2400- -     return s'
2401- -
2402- --- |Similar to 'mapState'
2403- -mapStateT :: (m (a, s) -> n (b, s)) -> StateT s m a -> StateT s n b
2404- -mapStateT f m = StateT $ f . runStateT m
2405+import Control.Monad.State.Lazy
2406hunk ./Control/Monad/State.hs 28
2407- --- |Similar to 'withState'
2408- -withStateT :: (s -> s) -> StateT s m a -> StateT s m a
2409- -withStateT f m = StateT $ runStateT m . f
2410- -
2411- --- ---------------------------------------------------------------------------
2412- --- MonadState instances for other monad transformers
2413- -
2414- --- Needs -fallow-undecidable-instances
2415- -instance (MonadState s m) => MonadState s (ReaderT r m) where
2416- -     get = lift get
2417- -     put = lift . put
2418- -
2419- --- Needs -fallow-undecidable-instances
2420- -instance (Monoid w, MonadState s m) => MonadState s (WriterT w m) where
2421- -     get = lift get
2422- -     put = lift . put
2423- -
2424- --- ---------------------------------------------------------------------------
2425- --- $examples
2426- --- A function to increment a counter.  Taken from the paper
2427- --- /Generalising Monads to Arrows/, John
2428- --- Hughes (<http://www.math.chalmers.se/~rjmh/>), November 1998:
2429- ---
2430- --- > tick :: State Int Int
2431- --- > tick = do n <- get
2432- --- >           put (n+1)
2433- --- >           return n
2434- ---
2435- --- Add one to the given number using the state monad:
2436- ---
2437- --- > plusOne :: Int -> Int
2438- --- > plusOne n = execState tick n
2439- ---
2440- --- A contrived addition example. Works only with positive numbers:
2441- ---
2442- --- > plus :: Int -> Int -> Int
2443- --- > plus n x = execState (sequence $ replicate n tick) x
2444- ---
2445- --- An example from /The Craft of Functional Programming/, Simon
2446- --- Thompson (<http://www.cs.kent.ac.uk/people/staff/sjt/>),
2447- --- Addison-Wesley 1999: \"Given an arbitrary tree, transform it to a
2448- --- tree of integers in which the original elements are replaced by
2449- --- natural numbers, starting from 0.  The same element has to be
2450- --- replaced by the same number at every occurrence, and when we meet
2451- --- an as-yet-unvisited element we have to find a \'new\' number to match
2452- --- it with:\"
2453- ---
2454- --- > data Tree a = Nil | Node a (Tree a) (Tree a) deriving (Show, Eq)
2455- --- > type Table a = [a]
2456- ---
2457- --- > numberTree :: Eq a => Tree a -> State (Table a) (Tree Int)
2458- --- > numberTree Nil = return Nil
2459- --- > numberTree (Node x t1 t2)
2460- --- >        =  do num <- numberNode x
2461- --- >              nt1 <- numberTree t1
2462- --- >              nt2 <- numberTree t2
2463- --- >              return (Node num nt1 nt2)
2464- --- >     where
2465- --- >     numberNode :: Eq a => a -> State (Table a) Int
2466- --- >     numberNode x
2467- --- >        = do table <- get
2468- --- >             (newTable, newPos) <- return (nNode x table)
2469- --- >             put newTable
2470- --- >             return newPos
2471- --- >     nNode::  (Eq a) => a -> Table a -> (Table a, Int)
2472- --- >     nNode x table
2473- --- >        = case (findIndexInList (== x) table) of
2474- --- >          Nothing -> (table ++ [x], length table)
2475- --- >          Just i  -> (table, i)
2476- --- >     findIndexInList :: (a -> Bool) -> [a] -> Maybe Int
2477- --- >     findIndexInList = findIndexInListHelp 0
2478- --- >     findIndexInListHelp _ _ [] = Nothing
2479- --- >     findIndexInListHelp count f (h:t)
2480- --- >        = if (f h)
2481- --- >          then Just count
2482- --- >          else findIndexInListHelp (count+1) f t
2483- ---
2484- --- numTree applies numberTree with an initial state:
2485- ---
2486- --- > numTree :: (Eq a) => Tree a -> Tree Int
2487- --- > numTree t = evalState (numberTree t) []
2488- ---
2489- --- > testTree = Node "Zero" (Node "One" (Node "Two" Nil Nil) (Node "One" (Node "Zero" Nil Nil) Nil)) Nil
2490- --- > numTree testTree => Node 0 (Node 1 (Node 2 Nil Nil) (Node 1 (Node 0 Nil Nil) Nil)) Nil
2491- ---
2492- --- sumTree is a little helper function that does not use the State monad:
2493- ---
2494- --- > sumTree :: (Num a) => Tree a -> a
2495- --- > sumTree Nil = 0
2496- --- > sumTree (Node e t1 t2) = e + (sumTree t1) + (sumTree t2)
2497hunk ./Control/Monad/Trans.hs 5
2498- ---             (c) Oregon Graduate Institute of Science and Technology, 2001
2499+--                (c) Oregon Graduate Institute of Science and Technology, 2001
2500hunk ./Control/Monad/Trans.hs 7
2501- ---
2502+--
2503hunk ./Control/Monad/Trans.hs 14
2504- ---     Inspired by the paper
2505- ---     /Functional Programming with Overloading and
2506- ---         Higher-Order Polymorphism/,
2507- ---       Mark P Jones (<http://www.cse.ogi.edu/~mpj/>)
2508- ---             Advanced School of Functional Programming, 1995.
2509+--      Inspired by the paper
2510+--      /Functional Programming with Overloading and
2511+--          Higher-Order Polymorphism/,
2512+--        Mark P Jones (<http://www.cse.ogi.edu/~mpj/>)
2513+--          Advanced School of Functional Programming, 1995.
2514hunk ./Control/Monad/Trans.hs 22
2515- -     MonadTrans(..),
2516- -     MonadIO(..), 
2517+    MonadTrans(..),
2518+    MonadIO(..),
2519hunk ./Control/Monad/Trans.hs 38
2520- -     lift :: Monad m => m a -> t m a
2521+    lift :: Monad m => m a -> t m a
2522hunk ./Control/Monad/Trans.hs 41
2523- -     liftIO :: IO a -> m a
2524+    liftIO :: IO a -> m a
2525hunk ./Control/Monad/Trans.hs 44
2526- -     liftIO = id
2527+    liftIO = id
2528hunk ./Control/Monad/Writer/Class.hs 1
2529+{-# OPTIONS -fallow-undecidable-instances #-}
2530+-- Search for -fallow-undecidable-instances to see why this is needed
2531+
2532+-----------------------------------------------------------------------------
2533+-- |
2534+-- Module      :  Control.Monad.Writer.Class
2535+-- Copyright   :  (c) Andy Gill 2001,
2536+--                (c) Oregon Graduate Institute of Science and Technology, 2001
2537+-- License     :  BSD-style (see the file libraries/base/LICENSE)
2538+--
2539+-- Maintainer  :  libraries@haskell.org
2540+-- Stability   :  experimental
2541+-- Portability :  non-portable (multi-param classes, functional dependencies)
2542+--
2543+-- The MonadWriter class.
2544+--
2545+--      Inspired by the paper
2546+--      /Functional Programming with Overloading and
2547+--          Higher-Order Polymorphism/,
2548+--        Mark P Jones (<http://web.cecs.pdx.edu/~mpj/pubs/springschool.html>)
2549+--          Advanced School of Functional Programming, 1995.
2550+-----------------------------------------------------------------------------
2551+
2552+module Control.Monad.Writer.Class (
2553+    MonadWriter(..),
2554+    listens,
2555+    censor,
2556+  ) where
2557+
2558+import Prelude
2559+
2560+import Data.Monoid
2561+
2562+-- ---------------------------------------------------------------------------
2563+-- MonadWriter class
2564+--
2565+-- tell is like tell on the MUD's it shouts to monad
2566+-- what you want to be heard. The monad carries this 'packet'
2567+-- upwards, merging it if needed (hence the Monoid requirement)}
2568+--
2569+-- listen listens to a monad acting, and returns what the monad "said".
2570+--
2571+-- pass lets you provide a writer transformer which changes internals of
2572+-- the written object.
2573+
2574+class (Monoid w, Monad m) => MonadWriter w m | m -> w where
2575+    tell   :: w -> m ()
2576+    listen :: m a -> m (a, w)
2577+    pass   :: m (a, w -> w) -> m a
2578+
2579+listens :: (MonadWriter w m) => (w -> b) -> m a -> m (a, b)
2580+listens f m = do
2581+    ~(a, w) <- listen m
2582+    return (a, f w)
2583+
2584+censor :: (MonadWriter w m) => (w -> w) -> m a -> m a
2585+censor f m = pass $ do
2586+    a <- m
2587+    return (a, f)
2588+
2589hunk ./Control/Monad/Writer/Lazy.hs 1
2590+{-# OPTIONS -fallow-undecidable-instances #-}
2591+-- Search for -fallow-undecidable-instances to see why this is needed
2592+
2593+-----------------------------------------------------------------------------
2594+-- |
2595+-- Module      :  Control.Monad.Writer.Lazy
2596+-- Copyright   :  (c) Andy Gill 2001,
2597+--                (c) Oregon Graduate Institute of Science and Technology, 2001
2598+-- License     :  BSD-style (see the file libraries/base/LICENSE)
2599+--
2600+-- Maintainer  :  libraries@haskell.org
2601+-- Stability   :  experimental
2602+-- Portability :  non-portable (multi-param classes, functional dependencies)
2603+--
2604+-- Lazy writer monads.
2605+--
2606+--      Inspired by the paper
2607+--      /Functional Programming with Overloading and
2608+--          Higher-Order Polymorphism/,
2609+--        Mark P Jones (<http://web.cecs.pdx.edu/~mpj/pubs/springschool.html>)
2610+--          Advanced School of Functional Programming, 1995.
2611+-----------------------------------------------------------------------------
2612+
2613+module Control.Monad.Writer.Lazy (
2614+    module Control.Monad.Writer.Class,
2615+    Writer(..),
2616+    execWriter,
2617+    mapWriter,
2618+    WriterT(..),
2619+    execWriterT,
2620+    mapWriterT,
2621+    module Control.Monad,
2622+    module Control.Monad.Fix,
2623+    module Control.Monad.Trans,
2624+    module Data.Monoid,
2625+  ) where
2626+
2627+import Prelude
2628+
2629+import Control.Monad
2630+import Control.Monad.Cont.Class
2631+import Control.Monad.Error.Class
2632+import Control.Monad.Fix
2633+import Control.Monad.Reader.Class
2634+import Control.Monad.State.Class
2635+import Control.Monad.Trans
2636+import Control.Monad.Writer.Class
2637+import Data.Monoid
2638+
2639+-- ---------------------------------------------------------------------------
2640+-- Our parameterizable writer monad
2641+
2642+newtype Writer w a = Writer { runWriter :: (a, w) }
2643+
2644+execWriter :: Writer w a -> w
2645+execWriter m = snd (runWriter m)
2646+
2647+mapWriter :: ((a, w) -> (b, w')) -> Writer w a -> Writer w' b
2648+mapWriter f m = Writer $ f (runWriter m)
2649+
2650+instance Functor (Writer w) where
2651+    fmap f m = Writer $ let (a, w) = runWriter m in (f a, w)
2652+
2653+instance (Monoid w) => Monad (Writer w) where
2654+    return a = Writer (a, mempty)
2655+    m >>= k  = Writer $ let
2656+        (a, w)  = runWriter m
2657+        (b, w') = runWriter (k a)
2658+        in (b, w `mappend` w')
2659+
2660+instance (Monoid w) => MonadFix (Writer w) where
2661+    mfix m = Writer $ let (a, w) = runWriter (m a) in (a, w)
2662+
2663+instance (Monoid w) => MonadWriter w (Writer w) where
2664+    tell   w = Writer ((), w)
2665+    listen m = Writer $ let (a, w) = runWriter m in ((a, w), w)
2666+    pass   m = Writer $ let ((a, f), w) = runWriter m in (a, f w)
2667+
2668+-- ---------------------------------------------------------------------------
2669+-- Our parameterizable writer monad, with an inner monad
2670+
2671+newtype WriterT w m a = WriterT { runWriterT :: m (a, w) }
2672+
2673+execWriterT :: Monad m => WriterT w m a -> m w
2674+execWriterT m = do
2675+    ~(_, w) <- runWriterT m
2676+    return w
2677+
2678+mapWriterT :: (m (a, w) -> n (b, w')) -> WriterT w m a -> WriterT w' n b
2679+mapWriterT f m = WriterT $ f (runWriterT m)
2680+
2681+instance (Monad m) => Functor (WriterT w m) where
2682+    fmap f m = WriterT $ do
2683+        ~(a, w) <- runWriterT m
2684+        return (f a, w)
2685+
2686+instance (Monoid w, Monad m) => Monad (WriterT w m) where
2687+    return a = WriterT $ return (a, mempty)
2688+    m >>= k  = WriterT $ do
2689+        ~(a, w)  <- runWriterT m
2690+        ~(b, w') <- runWriterT (k a)
2691+        return (b, w `mappend` w')
2692+    fail msg = WriterT $ fail msg
2693+
2694+instance (Monoid w, MonadPlus m) => MonadPlus (WriterT w m) where
2695+    mzero       = WriterT mzero
2696+    m `mplus` n = WriterT $ runWriterT m `mplus` runWriterT n
2697+
2698+instance (Monoid w, MonadFix m) => MonadFix (WriterT w m) where
2699+    mfix m = WriterT $ mfix $ \ ~(a, _) -> runWriterT (m a)
2700+
2701+instance (Monoid w, Monad m) => MonadWriter w (WriterT w m) where
2702+    tell   w = WriterT $ return ((), w)
2703+    listen m = WriterT $ do
2704+        ~(a, w) <- runWriterT m
2705+        return ((a, w), w)
2706+    pass   m = WriterT $ do
2707+        ~((a, f), w) <- runWriterT m
2708+        return (a, f w)
2709+
2710+-- ---------------------------------------------------------------------------
2711+-- Instances for other mtl transformers
2712+
2713+instance (Monoid w) => MonadTrans (WriterT w) where
2714+    lift m = WriterT $ do
2715+        a <- m
2716+        return (a, mempty)
2717+
2718+instance (Monoid w, MonadIO m) => MonadIO (WriterT w m) where
2719+    liftIO = lift . liftIO
2720+
2721+instance (Monoid w, MonadCont m) => MonadCont (WriterT w m) where
2722+    callCC f = WriterT $
2723+        callCC $ \c ->
2724+        runWriterT (f (\a -> WriterT $ c (a, mempty)))
2725+
2726+instance (Monoid w, MonadError e m) => MonadError e (WriterT w m) where
2727+    throwError       = lift . throwError
2728+    m `catchError` h = WriterT $ runWriterT m
2729+        `catchError` \e -> runWriterT (h e)
2730+
2731+-- This instance needs -fallow-undecidable-instances, because
2732+-- it does not satisfy the coverage condition
2733+instance (Monoid w, MonadReader r m) => MonadReader r (WriterT w m) where
2734+    ask       = lift ask
2735+    local f m = WriterT $ local f (runWriterT m)
2736+
2737+-- Needs -fallow-undecidable-instances
2738+instance (Monoid w, MonadState s m) => MonadState s (WriterT w m) where
2739+    get = lift get
2740+    put = lift . put
2741+
2742hunk ./Control/Monad/Writer/Strict.hs 1
2743+{-# OPTIONS -fallow-undecidable-instances #-}
2744+-- Search for -fallow-undecidable-instances to see why this is needed
2745+
2746+-----------------------------------------------------------------------------
2747+-- |
2748+-- Module      :  Control.Monad.Writer.Strict
2749+-- Copyright   :  (c) Andy Gill 2001,
2750+--                (c) Oregon Graduate Institute of Science and Technology, 2001
2751+-- License     :  BSD-style (see the file libraries/base/LICENSE)
2752+--
2753+-- Maintainer  :  libraries@haskell.org
2754+-- Stability   :  experimental
2755+-- Portability :  non-portable (multi-param classes, functional dependencies)
2756+--
2757+-- Strict writer monads.
2758+--
2759+--      Inspired by the paper
2760+--      /Functional Programming with Overloading and
2761+--          Higher-Order Polymorphism/,
2762+--        Mark P Jones (<http://web.cecs.pdx.edu/~mpj/pubs/springschool.html>)
2763+--          Advanced School of Functional Programming, 1995.
2764+-----------------------------------------------------------------------------
2765+
2766+module Control.Monad.Writer.Strict (
2767+    module Control.Monad.Writer.Class,
2768+    Writer(..),
2769+    execWriter,
2770+    mapWriter,
2771+    WriterT(..),
2772+    execWriterT,
2773+    mapWriterT,
2774+    module Control.Monad,
2775+    module Control.Monad.Fix,
2776+    module Control.Monad.Trans,
2777+    module Data.Monoid,
2778+  ) where
2779+
2780+import Prelude
2781+
2782+import Control.Monad
2783+import Control.Monad.Cont.Class
2784+import Control.Monad.Error.Class
2785+import Control.Monad.Fix
2786+import Control.Monad.Reader.Class
2787+import Control.Monad.State.Class
2788+import Control.Monad.Trans
2789+import Control.Monad.Writer.Class
2790+import Data.Monoid
2791+
2792+-- ---------------------------------------------------------------------------
2793+-- Our parameterizable writer monad
2794+
2795+newtype Writer w a = Writer { runWriter :: (a, w) }
2796+
2797+execWriter :: Writer w a -> w
2798+execWriter m = snd (runWriter m)
2799+
2800+mapWriter :: ((a, w) -> (b, w')) -> Writer w a -> Writer w' b
2801+mapWriter f m = Writer $ f (runWriter m)
2802+
2803+instance Functor (Writer w) where
2804+    fmap f m = Writer $ case runWriter m of
2805+                            (a, w) -> (f a, w)
2806+
2807+instance (Monoid w) => Monad (Writer w) where
2808+    return a = Writer (a, mempty)
2809+    m >>= k  = Writer $ case runWriter m of
2810+                            (a, w) -> case runWriter (k a) of
2811+                                (b, w') -> (b, w `mappend` w')
2812+
2813+instance (Monoid w) => MonadFix (Writer w) where
2814+    mfix m = Writer $ let (a, w) = runWriter (m a) in (a, w)
2815+
2816+instance (Monoid w) => MonadWriter w (Writer w) where
2817+    tell   w = Writer ((), w)
2818+    listen m = Writer $ case runWriter m of
2819+                            (a, w) -> ((a, w), w)
2820+    pass   m = Writer $ case runWriter m of
2821+                            ((a, f), w) -> (a, f w)
2822+
2823+-- ---------------------------------------------------------------------------
2824+-- Our parameterizable writer monad, with an inner monad
2825+
2826+newtype WriterT w m a = WriterT { runWriterT :: m (a, w) }
2827+
2828+execWriterT :: Monad m => WriterT w m a -> m w
2829+execWriterT m = do
2830+    (_, w) <- runWriterT m
2831+    return w
2832+
2833+mapWriterT :: (m (a, w) -> n (b, w')) -> WriterT w m a -> WriterT w' n b
2834+mapWriterT f m = WriterT $ f (runWriterT m)
2835+
2836+instance (Monad m) => Functor (WriterT w m) where
2837+    fmap f m = WriterT $ do
2838+        (a, w) <- runWriterT m
2839+        return (f a, w)
2840+
2841+instance (Monoid w, Monad m) => Monad (WriterT w m) where
2842+    return a = WriterT $ return (a, mempty)
2843+    m >>= k  = WriterT $ do
2844+        (a, w)  <- runWriterT m
2845+        (b, w') <- runWriterT (k a)
2846+        return (b, w `mappend` w')
2847+    fail msg = WriterT $ fail msg
2848+
2849+instance (Monoid w, MonadPlus m) => MonadPlus (WriterT w m) where
2850+    mzero       = WriterT mzero
2851+    m `mplus` n = WriterT $ runWriterT m `mplus` runWriterT n
2852+
2853+instance (Monoid w, MonadFix m) => MonadFix (WriterT w m) where
2854+    mfix m = WriterT $ mfix $ \ ~(a, _) -> runWriterT (m a)
2855+
2856+instance (Monoid w, Monad m) => MonadWriter w (WriterT w m) where
2857+    tell   w = WriterT $ return ((), w)
2858+    listen m = WriterT $ do
2859+        (a, w) <- runWriterT m
2860+        return ((a, w), w)
2861+    pass   m = WriterT $ do
2862+        ((a, f), w) <- runWriterT m
2863+        return (a, f w)
2864+
2865+-- ---------------------------------------------------------------------------
2866+-- Instances for other mtl transformers
2867+
2868+instance (Monoid w) => MonadTrans (WriterT w) where
2869+    lift m = WriterT $ do
2870+        a <- m
2871+        return (a, mempty)
2872+
2873+instance (Monoid w, MonadIO m) => MonadIO (WriterT w m) where
2874+    liftIO = lift . liftIO
2875+
2876+instance (Monoid w, MonadCont m) => MonadCont (WriterT w m) where
2877+    callCC f = WriterT $
2878+        callCC $ \c ->
2879+        runWriterT (f (\a -> WriterT $ c (a, mempty)))
2880+
2881+instance (Monoid w, MonadError e m) => MonadError e (WriterT w m) where
2882+    throwError       = lift . throwError
2883+    m `catchError` h = WriterT $ runWriterT m
2884+        `catchError` \e -> runWriterT (h e)
2885+
2886+-- This instance needs -fallow-undecidable-instances, because
2887+-- it does not satisfy the coverage condition
2888+instance (Monoid w, MonadReader r m) => MonadReader r (WriterT w m) where
2889+    ask       = lift ask
2890+    local f m = WriterT $ local f (runWriterT m)
2891+
2892+-- Needs -fallow-undecidable-instances
2893+instance (Monoid w, MonadState s m) => MonadState s (WriterT w m) where
2894+    get = lift get
2895+    put = lift . put
2896+
2897hunk ./Control/Monad/Writer.hs 8
2898- ---             (c) Oregon Graduate Institute of Science and Technology, 2001
2899+--                (c) Oregon Graduate Institute of Science and Technology, 2001
2900hunk ./Control/Monad/Writer.hs 10
2901- ---
2902+--
2903hunk ./Control/Monad/Writer.hs 17
2904- ---     Inspired by the paper
2905- ---     /Functional Programming with Overloading and
2906- ---         Higher-Order Polymorphism/,
2907- ---       Mark P Jones (<http://web.cecs.pdx.edu/~mpj/pubs/springschool.html>)
2908- ---             Advanced School of Functional Programming, 1995.
2909+--      Inspired by the paper
2910+--      /Functional Programming with Overloading and
2911+--          Higher-Order Polymorphism/,
2912+--        Mark P Jones (<http://web.cecs.pdx.edu/~mpj/pubs/springschool.html>)
2913+--          Advanced School of Functional Programming, 1995.
2914hunk ./Control/Monad/Writer.hs 25
2915- -     MonadWriter(..),
2916- -     listens,
2917- -     censor,
2918- -     Writer(..),
2919- -     execWriter,
2920- -     mapWriter,
2921- -     WriterT(..),
2922- -     execWriterT,
2923- -     mapWriterT,
2924- -     module Control.Monad,
2925- -     module Control.Monad.Fix,
2926- -     module Control.Monad.Trans,
2927- -     module Data.Monoid,
2928+    module Control.Monad.Writer.Lazy
2929hunk ./Control/Monad/Writer.hs 28
2930- -import Prelude
2931- -
2932- -import Control.Monad
2933- -import Control.Monad.Fix
2934- -import Control.Monad.Trans
2935- -import Control.Monad.Reader
2936- -import Data.Monoid
2937- -
2938- --- ---------------------------------------------------------------------------
2939- --- MonadWriter class
2940- ---
2941- --- tell is like tell on the MUD's it shouts to monad
2942- --- what you want to be heard. The monad carries this 'packet'
2943- --- upwards, merging it if needed (hence the Monoid requirement)}
2944- ---
2945- --- listen listens to a monad acting, and returns what the monad "said".
2946- ---
2947- --- pass lets you provide a writer transformer which changes internals of
2948- --- the written object.
2949- -
2950- -class (Monoid w, Monad m) => MonadWriter w m | m -> w where
2951- -     tell   :: w -> m ()
2952- -     listen :: m a -> m (a, w)
2953- -     pass   :: m (a, w -> w) -> m a
2954- -
2955- -listens :: (MonadWriter w m) => (w -> b) -> m a -> m (a, b)
2956- -listens f m = do
2957- -     (a, w) <- listen m
2958- -     return (a, f w)
2959- -
2960- -censor :: (MonadWriter w m) => (w -> w) -> m a -> m a
2961- -censor f m = pass $ do
2962- -     a <- m
2963- -     return (a, f)
2964- -
2965- --- ---------------------------------------------------------------------------
2966- --- Our parameterizable writer monad
2967- -
2968- -newtype Writer w a = Writer { runWriter :: (a, w) }
2969- -
2970- -
2971- -instance Functor (Writer w) where
2972- -     fmap f m = Writer $ let (a, w) = runWriter m in (f a, w)
2973- -
2974- -instance (Monoid w) => Monad (Writer w) where
2975- -     return a = Writer (a, mempty)
2976- -     m >>= k  = Writer $ let
2977- -             (a, w)  = runWriter m
2978- -             (b, w') = runWriter (k a)
2979- -             in (b, w `mappend` w')
2980- -
2981- -instance (Monoid w) => MonadFix (Writer w) where
2982- -     mfix m = Writer $ let (a, w) = runWriter (m a) in (a, w)
2983- -
2984- -instance (Monoid w) => MonadWriter w (Writer w) where
2985- -     tell   w = Writer ((), w)
2986- -     listen m = Writer $ let (a, w) = runWriter m in ((a, w), w)
2987- -     pass   m = Writer $ let ((a, f), w) = runWriter m in (a, f w)
2988- -
2989- -
2990- -execWriter :: Writer w a -> w
2991- -execWriter m = snd (runWriter m)
2992- -
2993- -mapWriter :: ((a, w) -> (b, w')) -> Writer w a -> Writer w' b
2994- -mapWriter f m = Writer $ f (runWriter m)
2995- -
2996- --- ---------------------------------------------------------------------------
2997- --- Our parameterizable writer monad, with an inner monad
2998- -
2999- -newtype WriterT w m a = WriterT { runWriterT :: m (a, w) }
3000- -
3001- -
3002- -instance (Monad m) => Functor (WriterT w m) where
3003- -     fmap f m = WriterT $ do
3004- -             (a, w) <- runWriterT m
3005- -             return (f a, w)
3006- -
3007- -instance (Monoid w, Monad m) => Monad (WriterT w m) where
3008- -     return a = WriterT $ return (a, mempty)
3009- -     m >>= k  = WriterT $ do
3010- -             (a, w)  <- runWriterT m
3011- -             (b, w') <- runWriterT (k a)
3012- -             return (b, w `mappend` w')
3013- -     fail msg = WriterT $ fail msg
3014- -
3015- -instance (Monoid w, MonadPlus m) => MonadPlus (WriterT w m) where
3016- -     mzero       = WriterT mzero
3017- -     m `mplus` n = WriterT $ runWriterT m `mplus` runWriterT n
3018- -
3019- -instance (Monoid w, MonadFix m) => MonadFix (WriterT w m) where
3020- -     mfix m = WriterT $ mfix $ \ ~(a, _) -> runWriterT (m a)
3021- -
3022- -instance (Monoid w, Monad m) => MonadWriter w (WriterT w m) where
3023- -     tell   w = WriterT $ return ((), w)
3024- -     listen m = WriterT $ do
3025- -             (a, w) <- runWriterT m
3026- -             return ((a, w), w)
3027- -     pass   m = WriterT $ do
3028- -             ((a, f), w) <- runWriterT m
3029- -             return (a, f w)
3030- -
3031- -instance (Monoid w) => MonadTrans (WriterT w) where
3032- -     lift m = WriterT $ do
3033- -             a <- m
3034- -             return (a, mempty)
3035- -
3036- -instance (Monoid w, MonadIO m) => MonadIO (WriterT w m) where
3037- -     liftIO = lift . liftIO
3038- -
3039- --- This instance needs -fallow-undecidable-instances, because
3040- --- it does not satisfy the coverage condition
3041- -instance (Monoid w, MonadReader r m) => MonadReader r (WriterT w m) where
3042- -     ask       = lift ask
3043- -     local f m = WriterT $ local f (runWriterT m)
3044- -
3045- -
3046- -execWriterT :: Monad m => WriterT w m a -> m w
3047- -execWriterT m = do
3048- -     (_, w) <- runWriterT m
3049- -     return w
3050- -
3051- -mapWriterT :: (m (a, w) -> n (b, w')) -> WriterT w m a -> WriterT w' n b
3052- -mapWriterT f m = WriterT $ f (runWriterT m)
3053- -
3054- --- ---------------------------------------------------------------------------
3055- --- MonadWriter instances for other monad transformers
3056+import Control.Monad.Writer.Lazy
3057hunk ./Control/Monad/Writer.hs 30
3058- --- This instance needs -fallow-undecidable-instances, because
3059- --- it does not satisfy the coverage condition
3060- -instance (MonadWriter w m) => MonadWriter w (ReaderT r m) where
3061- -     tell     = lift . tell
3062- -     listen m = ReaderT $ \w -> listen (runReaderT m w)
3063- -     pass   m = ReaderT $ \w -> pass   (runReaderT m w)
3064hunk ./LICENSE 3
3065- -Copyright 2004, The University Court of the University of Glasgow.
3066+Copyright 2004, The University Court of the University of Glasgow.
3067hunk ./LICENSE 11
3068- -
3069+
3070hunk ./LICENSE 15
3071- -
3072+
3073hunk ./LICENSE 18
3074- -specific prior written permission.
3075+specific prior written permission.
3076hunk ./Makefile 4
3077- -SUBDIRS =
3078+SUBDIRS =
3079hunk ./Makefile 6
3080- -ALL_DIRS = \
3081- -     Control/Monad
3082+ALL_DIRS =                  \
3083+       Control/Monad           \
3084+       Control/Monad/Cont      \
3085+       Control/Monad/Error     \
3086+       Control/Monad/RWS       \
3087+       Control/Monad/Reader    \
3088+       Control/Monad/State     \
3089+       Control/Monad/Writer
3090hunk ./Makefile 22
3091+SRC_HC_OPTS += -Wall -fno-warn-duplicate-exports -fno-warn-unused-imports
3092hunk ./mtl.cabal 15
3093- -     Control.Monad.Error,
3094- -     Control.Monad.Cont,
3095- -     Control.Monad.Identity,
3096- -     Control.Monad.List,
3097- -     Control.Monad.RWS,
3098- -     Control.Monad.Reader,
3099- -     Control.Monad.State,
3100- -     Control.Monad.Trans,
3101- -     Control.Monad.Writer
3102+    Control.Monad.Cont,
3103+    Control.Monad.Cont.Class,
3104+    Control.Monad.Error,
3105+    Control.Monad.Error.Class,
3106+    Control.Monad.Identity,
3107+    Control.Monad.List,
3108+    Control.Monad.RWS,
3109+    Control.Monad.RWS.Class,
3110+    Control.Monad.RWS.Lazy,
3111+    Control.Monad.RWS.Strict,
3112+    Control.Monad.Reader,
3113+    Control.Monad.Reader.Class,
3114+    Control.Monad.State,
3115+    Control.Monad.State.Class,
3116+    Control.Monad.State.Lazy,
3117+    Control.Monad.State.Strict,
3118+    Control.Monad.Trans,
3119+    Control.Monad.Writer,
3120+    Control.Monad.Writer.Class,
3121+    Control.Monad.Writer.Lazy,
3122+    Control.Monad.Writer.Strict
3123hunk ./package.conf.in 1
3124- -name:                PACKAGE
3125- -version:     VERSION
3126- -license:     BSD3
3127- -maintainer:  libraries@haskell.org
3128- -exposed:     True
3129+name:       PACKAGE
3130+version:    VERSION
3131+license:    BSD3
3132+maintainer: libraries@haskell.org
3133+exposed:    True
3134hunk ./package.conf.in 8
3135- -     Control.Monad.Error,
3136- -     Control.Monad.Cont,
3137- -     Control.Monad.Identity,
3138- -     Control.Monad.List,
3139- -     Control.Monad.RWS,
3140- -     Control.Monad.Reader,
3141- -     Control.Monad.State,
3142- -     Control.Monad.Trans,
3143- -     Control.Monad.Writer
3144+    Control.Monad.Cont,
3145+    Control.Monad.Cont.Class,
3146+    Control.Monad.Error,
3147+    Control.Monad.Error.Class,
3148+    Control.Monad.Identity,
3149+    Control.Monad.List,
3150+    Control.Monad.RWS,
3151+    Control.Monad.RWS.Class,
3152+    Control.Monad.RWS.Lazy,
3153+    Control.Monad.RWS.Strict,
3154+    Control.Monad.Reader,
3155+    Control.Monad.Reader.Class,
3156+    Control.Monad.State,
3157+    Control.Monad.State.Class,
3158+    Control.Monad.State.Lazy,
3159+    Control.Monad.State.Strict,
3160+    Control.Monad.Trans,
3161+    Control.Monad.Writer,
3162+    Control.Monad.Writer.Class,
3163+    Control.Monad.Writer.Lazy,
3164+    Control.Monad.Writer.Strict
3165hunk ./package.conf.in 32
3166- -import-dirs: IMPORT_DIR
3167- -library-dirs:        LIB_DIR
3168- -hs-libraries:   "HSmtl"
3169+import-dirs:  IMPORT_DIR
3170+library-dirs: LIB_DIR
3171+hs-libraries: "HSmtl"
3172hunk ./package.conf.in 37
3173- -includes:           
3174- -depends:      base
3175+includes:
3176+depends: base
3177hunk ./package.conf.in 44
3178- -haddock-interfaces:  HADDOCK_IFACE
3179- -haddock-html:                HTML_DIR
3180+haddock-interfaces: HADDOCK_IFACE
3181+haddock-html:       HTML_DIR
3182hunk ./prologue.txt 2
3183- -/Functional Programming with Overloading and Higher-Order Polymorphism/,
3184+/Functional Programming with Overloading and Higher-Order Polymorphism/,
3185}
3186
3187Context:
3188
3189[Proposal in trac #1059: Control.Monad.Error documentation
3190Ian Lynagh <igloo@earth.li>**20070131013720
3191 Andriy Palamarchuk writes:
3192 * Converted the module documentation to Haddock format.
3193 * Per Jeff Newbern's gracious permission included relevant information
3194   from his cool tutorial "All About Monads" http://www.nomaware.com/monads/.
3195 * Added examples for custom error type, ErrorT.
3196 * Use String instead of [Char].
3197]
3198[update broken URL as noticed by schneegloeckchen@gmx.li
3199Simon Marlow <simonmar@microsoft.com>**20070118094739]
3200[Added Haddock documentation. Converted the module documentation to Haddock format. Per Jeff Newbern's permission included parts his tutorial "All About Monads" http://www.nomaware.com/monads/.
3201Andriy Palamarchuk <apa3a@yahoo.com>**20061218165621]
3202[add boilerplate Setup.hs
3203Ross Paterson <ross@soi.city.ac.uk>**20060928231525]
3204[use Control.Monad.Instances from base
3205Ross Paterson <ross@soi.city.ac.uk>**20060410112533]
3206[Add -fallow-undecidable-instances to some Control.Monad modules
3207simonpj@microsoft.com**20060209121334
3208 
3209 I have recently tightened up GHC's implementation of the coverage
3210 condition.  As a result some of the Control.Monad modules are rejected.
3211 
3212 Example:
3213   class (Monad m) => MonadReader r m | m -> r where
3214   instance (Monoid w, MonadReader r m) => MonadReader r (WriterT w m)
3215 Here, fv(Writer w m) is not a superset of fv(r).
3216 
3217 The flag allows it.  I wonder if it's possible to use these modules
3218 to send the type cheker into a loop.
3219 
3220 
3221]
3222[TAG Initial conversion from CVS complete
3223John Goerzen <jgoerzen@complete.org>**20060112154134]
3224Patch bundle hash:
3225f190057cf1e4e86033d367c15473df80eb71adda
3226-----BEGIN PGP SIGNATURE-----
3227Version: GnuPG v1.4.1 (GNU/Linux)
3228
3229iD8DBQFFxnTS0y4LitoVXIcRApE9AJ9SF0yDP6uQEFygiLnzD4V4JUCUHQCfUHfl
3230XMpjGhRevGmgtabQCFw2xf4=
3231=QfTW
3232-----END PGP SIGNATURE-----