performance of monads
John Hughes
rjmh@cs.chalmers.se
Thu, 24 Jan 2002 15:09:38 +0100 (MET)
I've felt need to use State monads in two distinct situations:
...
And I've seen two distict aproaches. Using a State monad like one
provided with GHC (1), or a state monad like the one defined in the
paper "Moands for the Working Haskell Programmer" (2).
In (1), I can see how I can take advantage of the monad and make
updates in place using referencies. But then you'll have to either: -
pass the referencies as a parameter to the functions where you use
them (needless too say that this defeats the (my) main purpose of
using a state monad which is in case A, keep track of data with
minimal signature change )
You can do this conveniently using implicit parameters bound to your
references: that way you don't have to add parameters to all your function
definitions just to pass in a reference. (The types do change, though).
I would probably use one implicit parameter bound to a record of references.
...
Another state issue, sometimes I have some values that I want to keep
constant through the whole algorithm.
Example:
(some very simple NNs for instance)
- learning rate
- activation function
- maximum number of steps
- minimum error required
So I'll just declare them as 'constants'. But what if I decide I want
the user to be able to chose? Now I got two options: - pass them
around as values to all the functions - And signatures get HUGE - just
pass them to a higher level function that will encapsulate the
functions that use them... which is ugly and complicates everything
because you can't test the lower level functions in the interpreter.
Am I missing something here or is this really the best you can do?
Implicit parameters solve this too.
Read
http://www.cse.ogi.edu/~mbs/pub/implicit_parameters/
I've written a short article on global variables in Haskell, which explains
this particular application.
http://www.md.chalmers.se/~rjmh/Globals.ps
John Hughes