Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/romac/choreo
Choreographic programming in Scala
https://github.com/romac/choreo
haschor scala
Last synced: 1 day ago
JSON representation
Choreographic programming in Scala
- Host: GitHub
- URL: https://github.com/romac/choreo
- Owner: romac
- License: other
- Created: 2024-01-29T15:44:26.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2024-02-08T17:07:25.000Z (9 months ago)
- Last Synced: 2024-04-14T07:35:40.580Z (7 months ago)
- Topics: haschor, scala
- Language: Scala
- Homepage:
- Size: 43.9 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![Continuous Integration][ci-badge]][ci-link]
# Choreo
A library for choregraphic programming in Scala.
The implementation is based on the paper [_HasChor: Functional Choreographic Programming for All_][haschor-paper] by Gan Shen, Shun Kashiwa and Lindsey Kuper,
and its [Haskell implementation][haschor-github].## Example
```scala
package choreo
package examples
package kvimport cats.effect.IO
import cats.effect.IO.asyncForIO
import cats.effect.kernel.Ref
import cats.syntax.all.*type State = Map[String, String]
enum Request:
case Get(key: String)
case Put(key: String, value: String)type Response = Option[String]
val client: "client" = "client"
val server: "server" = "server"def main: IO[Unit] =
for
backend <- Backend.local(List(client, server))
clientTask = choreo.run(backend, client)
serverTask = choreo.run(backend, server)
_ <- (clientTask, serverTask).parTupled
yield ()def choreo: Choreo[IO, Unit] =
for
stateS <- server.locally(Ref.of[IO, State](Map.empty))
_ <- step(stateS).foreverM
yield ()def step(stateS: Ref[IO, State] @@ "server"): Choreo[IO, Unit] =
for
reqC <- client.locally(readRequest)
resC <- kvs(reqC, stateS)
_ <- client.locally:
resC.!.fold(IO.println("Key not found")):
IO.print("> ") *> IO.println(_)
yield ()def kvs(
reqC: Request @@ "client",
stateS: Ref[IO, State] @@ "server"
): Choreo[IO, Response @@ "client"] =
for
reqS <- client.send(reqC).to(server)
resS <- server.locally(handleRequest(reqS.!, stateS.!))
resC <- server.send(resS).to(client)
yield resCdef handleRequest(
req: Request,
state: Ref[IO, State]
): IO[Response] =
req match
case Request.Get(key) =>
state.get.map(_.get(key))case Request.Put(key, value) =>
state.update(_.updated(key, value)).as(Some(value))def readRequest: IO[Request] =
for
_ <- IO.print("> ")
line <- IO.readLine
req <- line.split(" ") match
case Array("GET", key) =>
IO.pure(Request.Get(key))case Array("PUT", key, value) =>
IO.pure(Request.Put(key, value))case _ =>
IO.raiseError(new Exception("Invalid request"))
yield req
```## License
This library is released under the same license as HasChor, namely the BSD-3 license.
Please see the [`LICENSE`](./LICENSE) within this repository for the full text of the license.
[ci-badge]: https://github.com/romac/choreo/actions/workflows/ci.yml/badge.svg
[ci-link]: https://github.com/romac/choreo/actions/workflows/ci.yml
[haschor-paper]: https://dl.acm.org/doi/10.1145/3607849
[haschor-github]: https://github.com/gshen42/HasChor