performance of monads
Jorge Adriano
jadrian@mat.uc.pt
Sun, 27 Jan 2002 13:38:02 +0000
On Thursday 24 January 2002 14:09, John Hughes wrote:
> 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.
Yes, I followed you suggestion and used a record, with all the optional
parameters, then the projection functions to get the fields I needed. Still
spartial specification of contexts would probably be a good thing.
> 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?
> I've written a short article on global variables in Haskell, which explains
> this particular application.
>
> http://www.md.chalmers.se/~rjmh/Globals.ps
I really liked reading you article, in fact it was kind of funny because I
had been following all the steps you describe, trying to figure out the best
option to simulate global variables... it felt like some sort of deja-vu.
Before posting my question I had been searching for a unsafePerformST, and
then thought about unsing unsafePerformIO and convert it to ST...
References were still in my "to check" list.
So your article really helped me save a lot of time, thanks.
J.A.