MyNixOS website logo
Description

Composable concurrent computation done right.

See README.md for details

Oath: composable concurrent computation done right

Oath is an Applicative structures that makes concurrent actions composable.

newtype Oath a = Oath { runOath :: forall r. (STM a -> IO r) -> IO r }

Oath is a continuation-passing IO action which takes a transaction to obtain the final result (STM a). The continuation-passing style makes it easier to release resources in time. The easiest way to construct Oath is oath. It runs the supplied IO action in a separate thread as long as the continuation is running.

oath :: IO a -> Oath a
oath act = Oath $ \cont -> do
  v <- newEmptyTMVarIO
  tid <- forkFinally act (atomically . putTMVar v)
  let await = takeTMVar v >>= either throwSTM pure
  cont await `finally` killThread tid

evalOath :: Oath a -> IO a
evalOath m = runOath m atomically

Oath is an Applicative, so you can combine multiple Oaths using <*>. Oath combined this way kicks off computations without waiting for the results. The following code runs foo :: IO a and bar :: IO b concurrently, then applies f to these results.

main = evalOath $ f <$> oath foo <*> oath bar

It does not provide a Monad instance because it is logically impossible to define one consistent with the Applicative instance.

Usage

Oath abstracts a triple of sending a request, waiting for response, and cancelling a request. If you want to send requests in a deterministic order, you can construct Oath directly instead of calling oath.

Oath $ \cont -> bracket sendRequest cancelRequest (cont . waitForResponse)

Timeout behaviour can be easily added using the Alternative instance and delay :: Int -> Oath (). a <|> b runs both computations until one of them returns a result, then cancels the other.

-- | An 'Oath' that finishes once the given number of microseconds elapses
delay :: Int -> Oath ()

oath action <|> delay 100000

Comparison to other packages

future, caf and async seem solve the same problem. They define abstractions to asynchronous computations. async has an applicative Concurrently wrapper.

spawn does not define any datatype. Instead it provides an utility function for IO (spawn :: IO a -> IO (IO a)). It does not offer a way to cancel a computation.

promises provides a monadic interface for pure demand-driven computation. It has nothing to do with concurrency.

unsafe-promises creates an IO action that waits for the result on-demand using unsafeInterleaveIO.

futures provides a wrapper of forkIO. There is no way to terminate an action and it does not propagate exceptions.

promise has illegal Applicative and Monad instances; (<*>) is not associative and its ap is not consistent with (<*>).

Performance

bench "oath 10" $ nfIO $ O.evalOath $ traverse (O.oath . pure) [0 :: Int ..9]
bench "async 10" $ nfIO $ A.runConcurrently $ traverse (A.Concurrently . pure) [0 :: Int ..9]

Oath's overhead of (<*>) is less than Concurrently. Unlike Concurrently, <*> itself does not fork threads.

All
  oath 10:   OK (1.63s)
    5.78 μs ± 265 ns
  async 10:  OK (0.21s)
    12.3 μs ± 767 ns
  oath 100:  OK (0.22s)
    52.6 μs ± 4.4 μs
  async 100: OK (0.23s)
    109  μs ± 8.4 μs
Metadata

Version

0.1.1

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows