[Haskell-cafe] State Variables

Matthew Bromberg mattcbro at earthlink.net
Sun May 28 17:38:38 EDT 2006

I've been toying with the idea of using Haskell to write a medium sized 
of a wireless network.  However  I have a number of concerns as I've 
begun to
program a bit more in Haskell. 

The first concern is that the simulation needs to be fast, much faster 
than Matlab
for example, to justify the use of Haskell.  Given GHCs speed 
improvement in recent
releases, I have high hopes that this is possible.  To be honest, I'd 
probably pick Clean
here, but I've been unable to consistently link to third party DLLs with 
the Clean IDE.

The second concern is that the simulation  requires
heavy use of complex valued matrices.  Since I'm on windows and the
only reasonably well developed matrix library doesn't run on windows I
would be forced to use the FFI to bring in various BLAS and LAPACK 
routines to
support this.  I've convinced myself that this is quite possible, if 
somewhat tedious by
virtue of the unpleasant array access syntax for mutable arrays.

The third and final concern is the point of this post.  How does one 
handle state efficiently in Haskell?
I've seen the various state monads and gained a superficial enough 
understanding to be concerned about
how useful they are, for the following reason:

I would like to aggregrate a large number of state variables (possibly 
in the hundreds of variables) into a few
data structures.  These structures would not only contain basic variable 
types, but probably some rather large
mutable arrays.  So lets say I have such a structure called world and I 
wish to update just 1 entry.  If I do
w=world{entry=newentry}, the entire enormous world structure is copied to w.

If I bury world in a state monad, then inside a do loop it appears that 
the get function extracts the entire world state and
the set function writes it back into the monad after I've altered it.  
It still seems to me that the entire structure is copied
again to achieve this.  I'm not sure if this is true or not.  Perhaps 
the compiler can pull apart the aggregation to fix this issue, but I'm
not sure if such an optimization is performed.  The only other thing I 
could think of would be to somehow create get and set
functions for every variable in the aggregation, which also would be 
impossibly tedious.

It is possible for me to structure the program so that most of the 
simulation parameters are set once by the user and then
never changed during the rest of the program operation, although 
incremental state updates are a bit more efficient than this approach.
To make this reasonably clean I would probably do some kind of unsafe 
extraction out of the IO monad to get my input variables, which
would then not be changed for the rest of the simulation, although many 
of the matrices, themselves embedded in the IO monad would have to
change incrementally as the simulation progresses.  Right now I would 
favor the latter approach, if I stay with Haskell, but so far it's not clear
to me that Haskell is 'convenient' for this kind of development, though 
I would like to be proven wrong.  I am also looking at D or possibly
LUA+C  for this task, but LUA is not much faster than Matlab.

More information about the Haskell-Cafe mailing list