[Haskell-cafe] Re: simulation in the haskell way

Ertugrul Soeylemez es at ertes.de
Tue Aug 18 19:29:06 EDT 2009

Eric Wong <wsysdu at gmail.com> wrote:

> I used to think about a physical engine in a similar way, and I think
> it can work. But in some simulations that objects have lots of
> dependencies on others can be tricky. For instance, object o1 depends
> on o2, if we represent them in pure values, when we update o2, then o1
> must be updated with a new o2 value, isn't it?

This is something handled best by functional reactive programming.  See
Peter Verswyvelen's post.  It allows you to encode this purely in an
excitingly elegant way.

> Recently I want to implement the digital circuit simulation in SICP
> using Haskell as an exercise. In the Scheme version, each Wire is
> represented as a function with local states. It records the signal on
> the wire and actions it will take when the signal changes to activate
> other wires.  How to represent the Wire in haskell purely?
> If I use State Monad (yes, it's pure :) to repsent a wire with local
> state, the interaction between connected wires is really tricky to
> implement.  any ideas?

You don't.  Either you use a state monad to hold _all_ wires (objects)
in, say, a Map, a Set or an Array, or you use a completely different
approach (like FRP).  In other words, your state monad should represent
all wires, not just one, because all wires together make up the state
you want to work with.


nightmare = unsafePerformIO (getWrongWife >>= sex)

More information about the Haskell-Cafe mailing list