I will be giving a presentation about Déjà Fu at the Haskell Symposium next week.
Déjà Fu is a library for developing and testing concurrent Haskell programs, it provides a typeclass-abstraction over GHC’s regular concurrency API, allowing the concrete implementation to be swapped out.
Why do we need this? Well, concurrency is really hard to get right. Empirical studies have found that many real-world concurrency bugs can be exhibited with small test cases using as few as two threads: so it’s not just big concurrent programs that are hard, small ones are too. We as programmers just don’t seem to have a very good intuition for traditional threads-and-shared-memory-style concurrency. The typical approach to testing concurrent programs is to just run them lots of times, but that doesn’t provide any hard coverage guarantees, and then we need to wonder: how many runs do we need?
Fortunately, there has been a lot of research into testing concurrency in the past several years. Systematic concurrency testing is an approach where the source of nondeterminism, the actual scheduler, is swapped out for one under the control of the testing framework. This allows possible schedules to be systematically explored, giving real coverage guarantees for our tests.
This is a library implementing systematic concurrency testing. It provides two typeclasses, MonadConc to abstract over much of Control.Concurrent and related modules, and MonadSTM, to similarly abstract over Control.Monad.STM.
How to use it
If you’re not making use of any IO in your code other than for concurrency, the transition to using
MonadSTM will probably just be a textual substitution:
IOis replaced with
MonadConc m => m
MonadSTM m => m
- Most other things have the same name, and so can be replaced by just swapping imports around.
If you are using other IO, you will need a gentle sprinkling of
liftIO in your code as well.
Is this really just a drop-in replacement for IO/STM?
That’s the idea, yes.
More specifically, the IO instance of
MonadConc and the STM instance of
MonadSTM just use the regular IO and STM functions, and so should have no noticeable change in behaviour, except for
IORef equivalent, where
modifyCRef behaves like
There are some other differences which can lead to incorrect results when testing, but which should not affect code when used in an IO or STM context. Specifically:
Control.Monad.Conc.Class.getNumCapabilitiescan lie to encourage more concurrency when testing; and
Control.Exception.catchcan catch exceptions from pure code, but
Control.Monad.Conc.Class.catchcan’t (except for the IO instance).↩