[Haskell-cafe] Building Monads from Monads

Daniel McAllansmith dagda at xtra.co.nz
Sun Mar 26 18:05:40 EST 2006

On Friday 24 March 2006 16:42, Cale Gibbard wrote:

Excellent help thanks, Cale.

A lot of my misunderstandings stemmed from not finding any 'instance 
MonadState ReaderT' when reading the code in Reader.hs, not realising that 
there was an instance defined in State.hs, and yet being able to use get on 
what I thought would just be a Reader.
I think I will now have an enduring friendship with GHCi's ':info'.

> > Q. 4) Is it possible to give a type to the lifted function so that the
> > monad of the correct class _and_ type is used?  E.g. dig into a String
> > Reader rather than an Int Reader.
> I'm not completely sure what you're after here -- basically, you just
> lift things into whichever monad you're using. If you want to be
> polymorphic, but insist on a particular instance of MonadReader,
> that's easy enough, just put a constraint like (MonadReader String m)
> or something similar on your type.

Not really a valid question now that I've cleared up my misconceptions of 
lifting, but...
I meant something along the lines of
  i <- ((lift get) :: Int) --dig into the nearest Int state
  s <- ((lift get) :: String) --dig into the nearest String state

> > Q. 6) Is it safe to always use liftIO, even in plain IO monad?
> It's safe, sure, though a little awkward. It's easy enough to lift
> whole IO computations later on anyway. The only benefit would be if
> you wanted to later intersperse actions into the code which came from
> a transformed version.

Yeah, I meant being able to go back and intersperse the use of state, or 
whatever, in a monadic expression that had, until then, only done IO.

[snip various good suggestions and improved code]

Good advice, thanks.


More information about the Haskell-Cafe mailing list