Simple monads

Christian Maeder maeder@tzi.de
Fri, 27 Jun 2003 12:55:48 +0200


>>not deeply
>>understanding the use of Haskell extensions in the State source, 
> 
> 
> I'm assuming Control.Monad.State's source in which case -no- extensions
> are used for -State- (well, at least I don't see any quickly glancing). 
> Extensions are used for the -MonadState class-.  

The portable parts of Control.Monad.State (that are sufficient for most 
cases) should be in an extra module (maybe called 
Control.Monad.StateTypes). In addition further non-overloaded names for 
put, get, gets and modify would be needed (maybe putState, getState, etc.)

There's no point to write your own "instance Monad ..." just because you 
want (or have) to be Haskell98 compliant.

The previous "newtype Labeller a = Labeller (Int -> (Int, a))" (the 
result tuple is reversed within Control.Monad.State) would simply become 
(untested):

newtype Labeller a = State Int a

newLabel = do { n <- get; put (n + 1); return (Label n) }

runLabeller l = execState l minBound

Christian