I’m reading up on Monad tutorials, and the one I’m working on now is http://www.muitovar.com/monad/moncow.xhtml , but I ran on a problem with the state Monad, or to be more precise the runState accessor function.
The type is defined as
newtype State s a = State { runState :: (s -> (a,s)) }
and it’s called e.g.
runState (chncasewst3 'e' 'd' 'f') False
I don’t know how to read the definition for getting to the second line, especially because of the “State s a” part. If it where “State a s”, I could deduce that the accessor has been curried ‘as far’ as the ‘s’.
So the question is; how to read the type definition so that I could see how to call the accessor function in this situation, and if possible how to read accessor functions per se.
When you have a data type defined as
read it like
with two helper functions defined automatically:
When you apply
getAto the value ofT, the result is of typea.Now your
Statetype consists of only one element, which type is a function (:: s -> (a, s)).runStateconverts a value of typeState s ato a function of this type.Every time you apply
runStateto the value of typeState s a, the result is a function of types -> (a,s). And the first argument of this function is an initial value of the state variable (of types).In the tutorial example,
chncasewst3 'e' 'd' 'f'has typeState Bool String.runState (chncasewst3 'e' 'd' 'f')has typeBool -> (String, Bool).runState (chncasewst3 'e' 'd' 'f') Falsehas type(String, Bool).Further reading: