This is a trivial problem of no importance I know, but . . .
How do I write parallel or in Concurrent Haskell, without using unsafe
features? So I want a function
por :: a -> a -> a
which takes two (presumably unevaluated) parameters and evaluates them
concurrently and returns if either of them returns. For
example if we define
bot = bot
then
por bot 0 = por 0 bot = 0.
Now if the type was allowed to be
por :: a -> a -> IO a
then it seems fairly easy:
por first second =
do
putHere <- newEmptyMVar
forkIO (
seq
first
(putMVar putHere first)
)
forkIO (
seq
second
(putMVar putHere second)
)
takeMVar putHere
But how to get rid of the IO? I would like to use the ST
monad (which I think is really neat) but it is sadly missing
two crucial features, namely fork and MVars. My question is,
would it be safe to add equivalents of fork and MVars to
ST? Or is there a much better way of doing parallel or anyway?
Of course I don't really want to learn separate names for everything
in ST. It would be nicer if ST and IO were instances of some
class HasConcurrent (say) which contained the operations for internal
state.