Future
Monad for Rust futures in Haskell.
This is a research project which started as part
of iced-hs
Built with tokio
pure a
is
async move { a }
callA >> callB
is
call_a.await;
call_b.await;
callA >>= callB
is
let a = call_a.await;
call_b(a).await;
import Future
import Future.Time
callA :: Future Int
callA = do
delaySecs 2
pure 1
callB :: Int -> Future Int
callB a = do
delaySecs 2
pure $ a + 4
example :: Future ()
example = do
a <- callA
b <- callB a
liftIO $ putStrLn $ show b -- prints 5
main :: IO ()
main = run example
There are race
and concurrent
functions:
race :: Future a -> Future b -> Future (Either a b)
concurrent :: Future a -> Future b -> Future (a, b)
For example:
do
let ma = callA
let mb = callB
(a, b) <- concurrent ma mb
race
is a wrapper around
tokio::select!
and concurrent
uses
tokio::join!
# build libfuture_hs.a
./build_rust.sh
# call ghc
# note that -threaded is required
ghc -threaded -ipath/to/this/repo path/to/libfuture_hs.a main.hs