liboleg-2010.1.6: An evolving collection of Oleg Kiselyov's Haskell modules

Control.VarStateM

Description

Variable state monad

http://okmij.org/ftp/Computation/monads.html#param-monad

The familiar State monad lets us represent computations with a state that can be queried and updated. The state must have the same type during the entire computation however. One sometimes wants to express a computation where not only the value but also the type of the state can be updated -- while maintaining static typing. We wish for a parameterized monad that indexes each monadic type by an initial (type)state and a final (type)state. The effect of an effectful computation thus becomes apparent in the type of the computation, and so can be statically reasoned about.

Synopsis

Documentation

class Monadish m whereSource

A parameterized monad

Methods

gret :: a -> m p p aSource

gbind :: m p q a -> (a -> m q r b) -> m p r bSource

Instances

Monadish LIO 
Monad m => Monadish (VST m) 
Monad m => Monadish (MW m) 

newtype MW m p q a Source

Inject regular monads to be monadish things too

Constructors

MW 

Fields

unMW :: m a
 

Instances

Monad m => Monadish (MW m) 

newtype VST m si so v Source

First, use the regular Monad.State

Now, wrap in MW

Introduce the variable-type state

Constructors

VST 

Fields

runVST :: si -> m (so, v)
 

Instances

Monad m => Monadish (VST m) 

vsget :: Monad m => VST m si si siSource

vsput :: Monad m => so -> VST m si so ()Source

crec1 :: (Enum si, Monad m) => VST m si si IntSource

Try polymorphic recursion, over the state. crec1 invokes itself, and changes the type of the state from some si to Bool.

data Locked Source

Another example, to illustrate locking and static reasoning about the locking state

Constructors

Locked 

data Unlocked Source

Constructors

Unlocked 

data LIO p q a Source

Constructors

LIO 

Fields

unLIO :: IO a
 

Instances