[Haskell] Discrete event simulation

Chris Kuklewicz haskell at list.mightyreason.com
Thu Jan 26 19:19:09 EST 2006

I don't have an answer.  But I do have questions which may help.

Paul Johnson wrote:
> Hi, I'm going slowly nuts here.  Maybe someone can help me out.

You could also try the IRC channel #haskell on freenode.irc.net

> I want to do some fairly straightforward discrete event simulation. 

But I do not know your terms, so I will ask you about them.

> Tasks do side effects, probably in the ST monad.  Every so often the
> current task calls "delay n" where n is a number of seconds. This puts
> the task back on a list of schedulable processes that is ordered by
> time, and then takes the head of the list and starts executing it.

So side effects means setting STRef's or ST(U)Arrays.

"delay n" means simulated seconds or wall clock seconds?

Also tasks are sounding like threads...Could you define event and task and
scheduler for us? How many events or tasks are there?

How does the scheduler interact with the events or tasks?

> Part of this will be some kind of synchronisation primitive.  I don't
> much care what it is, but somewhere I need a way to make a process wait
> until something happens rather than just a constant time.

Is a task is polling: calling "delay n" over and over again, checking for
something interesting each time?

If you are not in ST, then you can be in STM or IO and have a task in a thread
that will block waiting for a TMVar or an MVar (or a QSem), which represents
something interesting happening.  The forkIO threads are really cheap, and STM
is very cool if the optimistic assumption is valid.

> I think I want to use something like
>   type Task r s a =  ContT r (ST s) a
> But I can't see how to actually do it.  I've read All About Monads. 
> I've googled for anything to do with continuations and coroutines.  I'm
> stuck.  Can someone show me how to do it.

Now continuations and a scheduler sounds like the Zipper-base file server/OS:


That code uses partial continuations to manage tasks that talk to a scheduler.
The code is quite short and it knows more than I do.

> Thanks,
> Paul.

Good Luck,


More information about the Haskell mailing list