[Haskell-cafe] Re: 'Proper' use of the State monad

Yitzchak Gale gale at sefer.org
Tue May 1 18:10:51 EDT 2007


DavidA wrote:
> What I mean is, it seems like good design would mean that you could write and
> test the game logic totally independently of any IO. Game functions such
> as "makeMove" ought to have type signatures that don't involve any IO. Can this
> be achieved in option 2?

Here is one way:

For functions that do not need IO, use types that look like:

MonadState GameState m => ... -> m a

For functions that only do IO without refering to the
game state, use:

MonadIO m => ... -> m a

For functions that do both, use:

(MonadIO m, MonadState GameState m) => ... -> m a

Testing of the pure game functions can use
State GameState, testing of pure IO functions can
use IO, and production can use StateT GameState IO.

-Yitz


More information about the Haskell-Cafe mailing list