Skip to content

ibaryshnikov/future-hs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

future-hs

Future Monad for Rust futures in Haskell. This is a research project which started as part of iced-hs

Built with tokio

Semantics

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; 

Example

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!

Usage

# 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