[Haskell-cafe] Retrieving inner state from outside the transformer

Antoine Latter aslatter at gmail.com
Thu Jul 30 23:11:49 EDT 2009


On Thu, Jul 30, 2009 at 7:06 PM, Phil<phil at beadling.co.uk> wrote:
> Hi,
>
> I've hit a brick wall trying to work out, what should be (and probably is!)
> a simple problem.
>
> I have a StateT stack (1 State monad, 2 StateT transformers) which works
> fine and returns the result of the outer monad.  I thought I understood this
> fine, but perhaps not.  My understanding is that the result returned by the
> inner-most monad is always 'transformed' by the outer monads and thus the
> result you get is that computed in the outer transformer.
>

What I would do is not use transformers - I would have computations of
type 'State MyState a' where 'MyState' would be something like:

data MyState
   = MyState
     { component_one :: TypeOne
     , component_two :: TypeTwo
     , component_three :: TypeThree
     }

instead of three separate transformers/monads for each data type.

I would then have helper functions:

getThingOne :: State MyState TypeOne
setThingOne :: TypeOne -> State MyState ()
modifyThingOne :: (TypeOne -> TypeOne) -> State MyState TypeOne

etc.

You'll want to pick better names for your types and helper functions
as suits your program.

Antoine


More information about the Haskell-Cafe mailing list