Provide a test implementation for the monad stack

Here's the constructed monad stack in my program:

type Px a = ReaderT PConf (State PState) a

      

Where PConf and PState are arbitrary data types containing application configuration and state.

I need to provide a default implementation so that I can test my functions given "PConf" and "PState":

p4 :: Px ()
p4 = ???
  where
    conf  = PConf 4 10 10 [0, 1]
    state = PState 0 0 $ M.fromList [((x, y), Nothing) | x <- [0..9], y <- [0..9]]

      

I don't know what to write in "p4 =" so that the implementation uses "conf" and "state". Thank.

+3


source to share


1 answer


If it p4

is an action Px ()

, then it is not the point you define for conf

, and if state

it is supposed to be an initial state, not one. Instead, you should put them in a function that checks p4

:

p4 :: Px ()
p4 = ...

test = runState (runReaderT p4 conf) state
  where
    conf  = PConf 4 10 10 [0, 1]
    state = PState 0 0 $ M.fromList [((x, y), Nothing) | x <- [0..9], y <- [0..9]]

      



EDIT: In case your problem is still write-related p4

, here's a small layout p4

that changes some of the state to contain some of the original ones conf

:

p4 = do
   PConf x y z _ <- ask
   PState m n mp <- get
   put $ PState m n (M.insert (x, y) (Just z) mp)

      

+4


source







All Articles