Skip to content

Commit d0924e6

Browse files
committed
Merge pull request #47 from purescript/rws-rearrange
Only expose RWS functions via the appropriate classes
2 parents 04741ef + ad15b37 commit d0924e6

File tree

4 files changed

+8
-70
lines changed

4 files changed

+8
-70
lines changed

src/Control/Monad/RWS.purs

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import Data.Tuple
1313
-- | to the `Identity` monad.
1414
type RWS r w s = RWST r w s Identity
1515

16-
-- | Create an action in the `RWS` monad from a function which uses the
16+
-- | Create an action in the `RWS` monad from a function which uses the
1717
-- | global context and state explicitly.
1818
rws :: forall r w s a. (r -> s -> See s a w) -> RWS r w s a
1919
rws f = RWST \r s -> return $ f r s
@@ -37,59 +37,3 @@ mapRWS f = mapRWST (runIdentity >>> f >>> Identity)
3737
-- | Change the type of the context in a `RWS` action
3838
withRWS :: forall r1 r2 w s a. (r2 -> s -> Tuple r1 s) -> RWS r1 w s a -> RWS r2 w s a
3939
withRWS = withRWST
40-
41-
-- | Get the context of a `RWS` action
42-
ask :: forall r w s m. (Applicative m, Monoid w) => RWST r w s m r
43-
ask = RWST \r s -> pure $ mkSee s r mempty
44-
45-
-- | Locally change the context of a `RWS` action.
46-
local :: forall r w s m a. (r -> r) -> RWST r w s m a -> RWST r w s m a
47-
local f m = RWST \r s -> runRWST m (f r) s
48-
49-
-- | Read a value which depends on the context in a `RWS` action.
50-
reader :: forall r w s m a. (Applicative m, Monoid w) => (r -> a) -> RWST r w s m a
51-
reader f = RWST \r s -> pure $ mkSee s (f r) mempty
52-
53-
-- | Write to the accumulator in a `RWS` action
54-
writer :: forall r w s m a. (Applicative m) => Tuple a w -> RWST r w s m a
55-
writer (Tuple a w) = RWST \_ s -> pure $ {state: s, result: a, log: w}
56-
57-
-- | Execute a `RWS` action, and return the changes to the accumulator along with the return value
58-
listen :: forall r w s m a. (Monad m) => RWST r w s m a -> RWST r w s m (Tuple a w)
59-
listen m = RWST \r s -> runRWST m r s >>= \{state = s', result = a, log = w} -> pure $ {state: s', result: Tuple a w, log: w}
60-
61-
-- | Execute a `RWS` action and modify the accumulator
62-
pass :: forall r w s m a. (Monad m) => RWST r w s m (Tuple a (w -> w)) -> RWST r w s m a
63-
pass m = RWST \r s -> runRWST m r s >>= \{result = Tuple a f, state = s', log = w} -> pure $ {state: s', result: a, log: f w}
64-
65-
-- | Append a value to the accumulator in a `RWS` action
66-
tell :: forall r w s m. (Applicative m) => w -> RWST r w s m Unit
67-
tell w = writer (Tuple unit w)
68-
69-
-- | Execute a `RWS` action, and return a value which depends on the accumulator along with the return value
70-
listens :: forall r w s m a b. (Monad m) => (w -> b) -> RWST r w s m a -> RWST r w s m (Tuple a b)
71-
listens f m = RWST \r s -> runRWST m r s >>= \{state = s', result = a, log = w} -> pure $ {state: s', result: Tuple a (f w), log: w}
72-
73-
-- | Modify the accumulator in a `RWS` action
74-
censor :: forall r w s m a. (Monad m) => (w -> w) -> RWST r w s m a -> RWST r w s m a
75-
censor f m = RWST \r s -> runRWST m r s >>= \see -> pure $ see{log = f see.log}
76-
77-
-- | Get or modify the state in a `RWS` action
78-
state :: forall r w s m a. (Applicative m, Monoid w) => (s -> Tuple a s) -> RWST r w s m a
79-
state f = RWST \_ s -> case f s of Tuple a s' -> pure $ mkSee s' a mempty
80-
81-
-- | Get the state in a `RWS` action
82-
get :: forall r w s m. (Applicative m, Monoid w) => RWST r w s m s
83-
get = state \s -> Tuple s s
84-
85-
-- | Get a value which depends on the state in a `RWS` action
86-
gets :: forall r w s m a. (Applicative m, Monoid w) => (s -> a) -> RWST r w s m a
87-
gets f = state \s -> Tuple (f s) s
88-
89-
-- | Set the state in a `RWS` action
90-
put :: forall r w s m. (Applicative m, Monoid w) => s -> RWST r w s m Unit
91-
put s = state \_ -> Tuple unit s
92-
93-
-- | Modify the state in a `RWS` action
94-
modify :: forall r w s m. (Applicative m, Monoid w) => (s -> s) -> RWST r w s m Unit
95-
modify f = state \s -> Tuple unit (f s)

src/Control/Monad/Reader/Class.purs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ import Control.Monad.State.Trans
1414
import Control.Monad.Writer.Trans
1515
import Data.Monoid
1616

17-
import qualified Control.Monad.RWS as RWS
18-
1917
-- | The `MonadReader` type class represents those monads which support a global context via
2018
-- | `ask` and `local`.
2119
-- |
@@ -30,7 +28,7 @@ import qualified Control.Monad.RWS as RWS
3028
-- | - `do { ask ; ask } = ask`
3129
-- | - `local f ask = f <$> ask`
3230
-- | - `local _ (pure a) = pure a`
33-
-- | - `local f (do { a <- x ; y }) = do { a <- local f x ; local f y }`
31+
-- | - `local f (do { a <- x ; y }) = do { a <- local f x ; local f y }`
3432
class MonadReader r m where
3533
ask :: m r
3634
local :: forall a. (r -> r) -> m a -> m a
@@ -64,5 +62,5 @@ instance monadReaderStateT :: (Monad m, MonadReader r m) => MonadReader r (State
6462
local f = mapStateT (local f)
6563

6664
instance monadReaderRWST :: (Monad m, Monoid w) => MonadReader r (RWST r w s m) where
67-
ask = RWS.ask
68-
local = RWS.local
65+
ask = RWST \r s -> pure $ mkSee s r mempty
66+
local f m = RWST \r s -> runRWST m (f r) s

src/Control/Monad/State/Class.purs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ import Control.Monad.Writer.Trans
1515
import Data.Monoid
1616
import Data.Tuple
1717

18-
import qualified Control.Monad.RWS as RWS
19-
2018
-- | The `MonadState s` type class represents those monads which support a single piece of mutable
2119
-- | state of type `s`.
2220
-- |
@@ -70,4 +68,4 @@ instance monadStateWriterT :: (Monad m, Monoid w, MonadState s m) => MonadState
7068
state f = lift (state f)
7169

7270
instance monadStateRWST :: (Monad m, Monoid w) => MonadState s (RWST r w s m) where
73-
state = RWS.state
71+
state f = RWST \_ s -> case f s of Tuple a s' -> pure $ mkSee s' a mempty

src/Control/Monad/Writer/Class.purs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ import Control.Monad.State.Trans
1515
import Data.Monoid
1616
import Data.Tuple
1717

18-
import qualified Control.Monad.RWS as RWS
19-
2018
-- | The `MonadWriter w` type class represents those monads which support a monoidal accumulator
2119
-- | of type `w`.
2220
-- |
@@ -85,6 +83,6 @@ instance monadWriterReaderT :: (Monad m, MonadWriter w m) => MonadWriter w (Read
8583
pass = mapReaderT pass
8684

8785
instance monadWriterRWST :: (Monad m, Monoid w) => MonadWriter w (RWST r w s m) where
88-
writer = RWS.writer
89-
listen = RWS.listen
90-
pass = RWS.pass
86+
writer (Tuple a w) = RWST \_ s -> pure $ {state: s, result: a, log: w}
87+
listen m = RWST \r s -> runRWST m r s >>= \{state = s', result = a, log = w} -> pure $ {state: s', result: Tuple a w, log: w}
88+
pass m = RWST \r s -> runRWST m r s >>= \{result = Tuple a f, state = s', log = w} -> pure $ {state: s', result: a, log: f w}

0 commit comments

Comments
 (0)