moonchor

    Choreographic programming library for MoonBit.

    Choreographic
    MoonBit
    Download zip
    Author
    Version
    0.19.2
    License
    Apache-2.0
    Last updated
    14 days ago
    Downloads
    96

    #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

    type Choreo[T] = async (ChoreoContext) -> T

    Choreography type.

    Location

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

    Logger

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

    Message

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

    Types that can be (de)serialized. Now only JSON is supported.
    impl Message for Int

    Backend

    type Backend

    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

    async fn[T, L : Location + Show + Hash] ChoreoContext::locally(self : ChoreoContext, location : L, computation : async (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 : async (Unwrapper[L]) -> T) -> T

    Locally compute and then broadcast the result.

    ChoreoContext::spawn

    fn[X] ChoreoContext::spawn(self : ChoreoContext, f : async () -> X) ->
    Task
    [X]

    Spawn an asynchronous task.

    ConsoleLogger

    type ConsoleLogger

    ConsoleLogger::info

    fn ConsoleLogger::info(_self : ConsoleLogger, msg : String) -> Unit

    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

    MuteLogger::info

    fn MuteLogger::info(_self : MuteLogger, _msg : String) -> Unit

    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], group :
    TaskGroup
    [Unit], 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