https://github.com/ibaryshnikov/future-hs
Future Monad
https://github.com/ibaryshnikov/future-hs
haskell rust tokio
Last synced: 7 months ago
JSON representation
Future Monad
- Host: GitHub
- URL: https://github.com/ibaryshnikov/future-hs
- Owner: ibaryshnikov
- License: mit
- Created: 2024-04-17T12:59:52.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2025-02-21T14:35:20.000Z (11 months ago)
- Last Synced: 2025-02-21T15:32:58.228Z (11 months ago)
- Topics: haskell, rust, tokio
- Language: Haskell
- Homepage:
- Size: 19.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# future-hs
`Future` Monad for Rust futures in Haskell.
This is a research project which started as part
of [iced-hs](https://github.com/ibaryshnikov/iced-hs)
Built with [tokio](https://github.com/tokio-rs/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
```haskell
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:
```haskell
race :: Future a -> Future b -> Future (Either a b)
concurrent :: Future a -> Future b -> Future (a, b)
```
For example:
```haskell
do
let ma = callA
let mb = callB
(a, b) <- concurrent ma mb
```
`race` is a wrapper around
[tokio::select!](https://docs.rs/tokio/1.37.0/tokio/macro.select.html)
and `concurrent` uses
[tokio::join!](https://docs.rs/tokio/1.37.0/tokio/macro.join.html)
## Usage
```bash
# 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
```