awaitEval in Concurrent Haskell

C.Reinke C.Reinke@ukc.ac.uk
Thu, 06 Feb 2003 18:25:36 +0000


> Colin [ccing GHC users in case there are other enthusiasts out there]
> 
> | we briefly discussed the lack in
> | Concurrent Haskell of any way to set up a data-driven
> | thread -- one that works on a data structure D without
> | ever forcing its evaluation, only proceeding with the
> | computation over D as and when the needed parts get
> | evaluated by some other thread.

I'm not sure whether I understand what you have in mind
later on, but this first part sounds so remarkably like
something I've seen before, that I'll take my chances.

Do you remember Andy Gill's Hood from long ago?

Inside its implementation, it had a very similar problem:
writing information about an observed structure to some
observation trace, but without forcing premature evaluation
of the structure under observation.

The trick used in Observe.lhs is roughly this (here for (,)):

  observe label (a,b) = unsafePerformIO $ do
    sendObservation label "(,)"
    return (observe label a,observe label b)

with some position information and strictness mangling added, and
the whole nicely wrapped into a monad (see Observe.lhs for details).

Nothing happens as long as the thing under observation is not
inspected by its context. Then, and precisely then, the
unsafePerformIO kicks in to record a piece of information and to
return the outer part of the thing, wrapping its components into
fresh observers.

Andy used this to store observations in a list, to be processed at
the end of the program run, but you can just as well send the
observations during evaluation, e.g., to a concurrent thread (with
the usual caveats). In particular, the sequencing of information
becoming available was detailed enough to inspire my own GHood;-)

With no implementation slot free, something like this might get your
student's project unstuck (e.g., replace sendObservation by assert)?
After all, my favourite justification for unsafePerformIO is as an
extension hook in the runtime system..

Sorry if your intention was something else and I'm just trying to
fit a solution to the problem. Even then, you might be able to
adapt the trick to your application.

Cheers,
Claus