moonchor

Choreographic programming library for MoonBit.

Choreographic
MoonBit
moon add Milky2018/moonchor@0.18.0
Download zip
Author
Version
0.18.0
License
Apache-2.0
Last updated
10 months ago
Downloads
74
README

#Moonchor: Choreographic Programming Library for MoonBit

#Installation

moon add Milky2018/moonchor

#Examples

Check examples

#Basic APIs

trait Location: Show + Hash {
name(Self) -> String
}

fn make_local_backend(
locations : Array[&Location],
logger~ : &Logger = make_mute_logger()
) -> Backend

async fn run_choreo[T, L : Location](
backend : Backend,
choreography : (ChoreoContext) -> T!Async,
role : L,
logger? : &Logger
) -> T

For example, you can define some roles/locations by implementing the Location trait:

struct Alice {}
struct Bob {}
impl Location for Alice with name(_) { "Alice" }
impl Location for Bob with name(_) { "Bob" }
let alice : Alice = Alice::{ }
let bob : Bob = Bob::{ }

Define a simple choreography: Alice --msg-> Bob

async fn simple_choreo(ctx: ChoreoContext) -> Unit {
let msg_at_alice = ctx.locally(alice, fn (_) { "Hello" })
let msg_at_bob = ctx.comm(alice, bob, msg_at_alice)
ctx.locally(bob, fn (unwrapper) { println(unwrapper.unwrap(msg_at_bob)) })
}

Then you can run the choreography:

let backend = make_local_backend([alice, bob])
run_choreo!(backend, simple_choreo, alice)
// or run_choreo!(backend, simple_choreo, bob)

#Todo

#Further Readings

#
Choreo

typealias async (ChoreoContext) -> T as Choreo[T]

Choreography type.

#
Location

pub(open) trait Location : Show + Hash {
name(Self) -> String
}

#
Logger

pub trait Logger {
info(Self, String) -> Unit
}

#
Message

pub(open) trait Message : ToJson +
FromJson
{
}

Types that can be (de)serialized. Now only JSON is supported.

#
Backend

type Backend

#
Backend::get_locations

fn Backend::get_locations(self : Backend) -> Array[&Location]

#
Backend::get_logger

fn Backend::get_logger(self : Backend) -> &Logger

#
Backend::init_at

async fn[L : Location + Show + Hash] Backend::init_at(self : Backend, role : L) -> Unit

#
ChoreoContext

type ChoreoContext

#
ChoreoContext::announce

async fn[T : Message + ToJson +
FromJson
, L : Location + Show + Hash] ChoreoContext::announce(self : ChoreoContext, loc : L, targets : Array[&Location], located_value : Located[T, L], subchoreo : async (ChoreoContext, T) -> Unit) -> Unit

Announce a located value to all locations in the given array. This operation takes a subchoreography as an argument.

#
ChoreoContext::broadcast

async fn[T : Message + ToJson +
FromJson
, L : Location + Show + Hash] ChoreoContext::broadcast(self : ChoreoContext, loc : L, value : Located[T, L]) -> T

Broadcast a message to all locations.

#
ChoreoContext::comm

async fn[T : Message + ToJson +
FromJson
, From : Location + Show + Hash, To : Location + Show + Hash] ChoreoContext::comm(self : ChoreoContext, from : From, to : To, value : Located[T, From]) -> Located[T, To]

Communication between two locations.

#
ChoreoContext::enclave

async fn ChoreoContext::enclave(self : ChoreoContext, locations : Array[&Location], subchoreo : async (ChoreoContext) -> Unit) -> Unit

Make an enclave with a subchoreography where broadcast will only spread to the given locations.

#
ChoreoContext::locally

fn[T, L : Location + Show + Hash] ChoreoContext::locally(self : ChoreoContext, location : L, computation : (Unwrapper[L]) -> T) -> Located[T, L]

Local computation at the given location.

#
ChoreoContext::locally_broadcast

async fn[T : Message + ToJson +
FromJson
, L : Location + Show + Hash] ChoreoContext::locally_broadcast(self : ChoreoContext, loc : L, computation : (Unwrapper[L]) -> T) -> T

Locally compute and then broadcast the result.

#
ConsoleLogger

type ConsoleLogger

#
Located

type Located[T, _]

Represents a value located at a specific location.

#
Located::map

fn[T, L, R] Located::map(self : Located[T, L], f : (T) -> R) -> Located[R, L]

Mapping a located value

#
MuteLogger

type MuteLogger

#
Unwrapper

type Unwrapper[_]

The unwrapper is used to unwrap located values in the computation. It is passed to the computation function as the only argument.

#
Unwrapper::unwrap

fn[T, L] Unwrapper::unwrap(_ : Unwrapper[L], v : Located[T, L]) -> T

Unwrap a located value.

#
make_console_logger

fn make_console_logger() -> ConsoleLogger

#
make_http_backend

fn make_http_backend(location_addrs : Array[(&Location,
Addr
)], group :
TaskGroup
[Unit], logger? : &Logger) -> Backend

#
make_local_backend

fn make_local_backend(locations : Array[&Location], logger? : &Logger) -> Backend

#
make_mute_logger

fn make_mute_logger() -> MuteLogger

#
run_choreo

async fn[T, L : Location + Show + Hash] run_choreo(backend : Backend, choreography : async (ChoreoContext) -> T, role : L) -> T

Run a choreography in the given location and backend. Default logger is the backend logger.

Powered by MoonBit

Site sourceReport issuePackagesBuild queueSkillsStatistics

© 2026 mooncakes.io