Ticket #1410: Class.hs

File Class.hs, 2.2 KB (added by guest, 8 years ago)

Control.Monad.Reader.Class source

Line 
1{-# OPTIONS -fallow-undecidable-instances #-}
2{- |
3Module      :  Control.Monad.Reader.Class
4Copyright   :  (c) Andy Gill 2001,
5               (c) Oregon Graduate Institute of Science and Technology 2001,
6               (c) Jeff Newbern 2003-2007,
7               (c) Andriy Palamarchuk 2007
8License     :  BSD-style (see the file libraries/base/LICENSE)
9
10Maintainer  :  [email protected]
11Stability   :  experimental
12Portability :  non-portable (multi-param classes, functional dependencies)
13
14[Computation type:] Computations which read values from a shared environment.
15
16[Binding strategy:] Monad values are functions from the environment to a value.
17The bound function is applied to the bound value, and both have access
18to the shared environment.
19
20[Useful for:] Maintaining variable bindings, or other shared environment.
21
22[Zero and plus:] None.
23
24[Example type:] @'Reader' [(String,Value)] a@
25
26The 'Reader' monad (also called the Environment monad).
27Represents a computation, which can read values from
28a shared environment, pass values from function to function,
29and execute sub-computations in a modified environment.
30Using 'Reader' monad for such computations is often clearer and easier
31than using the 'Control.Monad.State.State' monad.
32
33  Inspired by the paper
34  /Functional Programming with Overloading and
35      Higher-Order Polymorphism/,
36    Mark P Jones (<http://www.cse.ogi.edu/~mpj/>)
37    Advanced School of Functional Programming, 1995.
38-}
39
40module Control.Monad.Reader.Class (
41    MonadReader(..),
42    asks,
43    ) where
44
45{- |
46Note, the partially applied function type @(->) r@ is a simple reader monad.
47See the @instance@ declaration below.
48See examples in "Control.Monad.Reader".
49-}
50class (Monad m) => MonadReader r m | m -> r where
51    -- | Retrieves the monad environment.
52    ask   :: m r
53    {- | Executes a computation in a modified environment. Parameters:
54
55    * The function to modify the environment.
56
57    * @Reader@ to run.
58
59    * The resulting @Reader@.
60    -}
61    local :: (r -> r) -> m a -> m a
62
63{- |
64Retrieves a function of the current environment. Parameters:
65
66* The selector function to apply to the environment.
67
68See an example in "Control.Monad.Reader".
69-}
70asks :: (MonadReader r m) => (r -> a) -> m a
71asks f = do
72    r <- ask
73    return (f r)
74