Search code examples
haskellmonad-transformersstate-monad

What is the best way to combine local and global state


I have this global State

type GlobalState a = State Int a

but one function needs its own local state and access to the GlobalState

type LocalState a = State [String] a

But I'm not sure now to combine them.

Right now I just add local state to the global

type GlobalState a = State (Int, [String]) a

It works fine, but I don't think it's right because I only need local state in one function. Is there a better way?


Solution

  • You may use a monad stack of two States:

    type LocalState a = [String]
    type GlobalState a = [String]
    newtype MyState a = StateT GlobalState (State LocalState) a