[Haskell-cafe] Gentle introduction questions / comments
ryani.spam at gmail.com
Tue Jan 27 13:47:10 EST 2009
2009/1/27 Matthijs Kooijman <matthijs at stdin.nl>:
> Hi all,
> I've been reading the gentle introduction to Haskell a bit more closely today
> and there a few things which I can't quite understand (presumably because they
> are typo's). I've found two issues with the "Using monads" section . Not
> sure if this is the right place to report this, but there's probably somewhere
> here who can fix it :-)
> The first one is in the state monad data type. It reads:
> data SM a = SM (S -> (a,S)) -- The monadic type
> I can't seem to find out what "S" is exactly here. It seems to be a
> universally quantified type variable, but then it should be lowercase.
> Considering that it is referred to later on as "s", I suspect this is a type?
Often when working through a concept for the first time, it's simpler
to fix particular pieces; in this case, S is some fixed state type.
Then, once you get everything working, you notice that you don't make
any assumptions about S; then you are free to generalize which lets
you universally quantify.
Perhaps the tutorial should make this more clear, but this is a
process I've gone through with my own code many times; start with the
specific and generalize as needed.
In addition, type signatures are simpler with as few type variables as possible:
> get :: SM S
> put :: S -> SM ()
as opposed to
> get :: SM s s
> put :: s -> SM s ()
In the state monad case this isn't much of a difference, but I've
often found that my code wants three or four additional type variables
on a structure. In these cases I do wish for some kind of
"parametrized module" syntax, where the module itself is quantified
over certain type variables, and the code does not need to explicitly
list them everywhere.
More information about the Haskell-Cafe