Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Provide a testing implementation of a monad stack

Tags:

haskell

Here's a constructed monad stack in my program:

type Px a = ReaderT PConf (State PState) a

Where PConf and PState are arbitrary data types holding the configuration and the state of the app.

I need to provide a default implementation so i can test my functions, given a 'PConf' and a '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'. Thanks.

like image 884
qleguennec Avatar asked May 15 '26 18:05

qleguennec


1 Answers

If p4 is a Px () action, then that is not the point you define the conf to pass in, and if state is supposed to be the initial state, not that one either. Instead you should put those in the function that tests 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 with writing p4 itself, here's a small mockup p4 that changes some of the state to contain some of the original conf:

p4 = do
   PConf x y z _ <- ask
   PState m n mp <- get
   put $ PState m n (M.insert (x, y) (Just z) mp)
like image 58
Ørjan Johansen Avatar answered May 17 '26 17:05

Ørjan Johansen