What you need is
foo a = evalState (fooC a (C 0)) (MyState "whatever")
You construct the State MyState B
action fooC a (C 0)
, unwrap it to get a function, and apply that function to an initial state. Since the state isn't used in this example, you could here also use undefined
instead of MyState "whatever"
, but in general, you need to provide a meaningful initial state.
State MyState B
is not a tuple, it is isomorphic to a function
MyState -> (B, MyState)
but that function is wrapped in a newtype
(details vary by monad transformer library package and version), so to access the result of that function applied to an initial state, you need an unwrapping function. For State
, there are
runState :: State s r -> (s -> (r,s))
which gives you the function returning the pair,
evalState :: State s r -> (s -> r)
that gives you the function composed with fst
, so the final state is discarded, and
execState :: State s r -> (s -> s)
that composes the function with snd
, so only the final state is returned.