[Haskell-cafe] Stacking monads - beginner design question

Adam Smyczek adam.smyczek at gmail.com
Wed Jan 30 00:44:48 EST 2008


Hi,

My application has to manage a data set. I assume the state monad is  
designed for this.
The state changes in functions that:
a. perform IO actions and
b. return execution status and execution trace (right now I'm using  
WriteT for this).

Is the best solution:
1. to build a monad stack (for example State -> Writer -> IO) or
2. to use IORef for the data set or
3. something else?

Are monad stacks with 3 and more monads common?
How could an example implementation look like?

What I have for now is:

-- Status
data Status = OK | FAILED deriving (Show, Read, Enum)

-- Example data set manages by state
type Config = [String]

-- WriterT transformer
type OutputWriter = WriterT [String] IO Status

-- example execute function
execute :: [String] -> OutputWriter
execute fs = do
	rs <- liftIO loadData fs
	tell $ map show rs
	return OK

-- run it inside e.g. main
(s, os) <- runWriterT $ execute files

How do I bring a state into this, for example for:
execute fs = do
	?? conf <- get ?? -- get Config from state
	rs <- liftIO loadData conf fs
	?? set conf ?? -- change Config and set to state
	tell "new state:"
	tell $ show conf
	return OK

Do I have to use and how do I use StateT in this context:
data DataState = StateT Config OutputWriter ??
and how do I run it runStateT . runWriterT?

Thanks for help,

Adam




More information about the Haskell-Cafe mailing list