libretto.examples

Type members

Classlikes

object CoffeeMachine extends StarterApp
object DiningPhilosophers extends StarterApp
object Echo extends StarterApp

Reads lines from standard input and prints them to standard output.

Reads lines from standard input and prints them to standard output.

object Fibonacci extends StarterApp
object HelloWorld extends StarterAppScala[String]
object PingPong extends StarterApp

This example implements interaction between two parties, Alice and Bob, in which

This example implements interaction between two parties, Alice and Bob, in which

  • Alice sends "ping" to Bob,
  • Bob sends "pong" back to Alice,
  • Alice sends Done to Bob,

in this order, after which no more interaction between the two takes place.

This example demonstrates:

  • A simple protocol between two parties.
  • Linearity: obligation to consume every input exactly once and fulfill every demand exactly once. Linearity is key to ensuring adherence to a protocol.
  • Inverting the flow of values from left-to-right to right-to-left and vice versa.
object PingPongN extends StarterApp

This example implements interaction between two parties, Alice and Bob, in which

This example implements interaction between two parties, Alice and Bob, in which

  • when it's Alice's turn, she can choose to send "ping" to Bob or to quit;
  • when it's Bob's turn, he can choose to send "pong" back to Alice, or to quit;
  • interaction continues until one of the parties quits.

This is an extension of the PingPong example to multiple rounds of ping-pong. It further demonstrates:

  • The choice operators |+| and |+|.
  • Recursive protocol type.
object PoolingMicroscopes extends StarterApp

N scientists sharing M microscopes (N > M).

N scientists sharing M microscopes (N > M).

This examples demonstrates:

  • concurrency (scientists operate concurrently)
  • sequencing (each scientist performs experiments sequentially, at most one at a time)
  • resource sharing via pooling (the limited number of microscopes is made available throught a pool)
object Supermarket extends StarterApp

In a pandemic, supermarkets are required to limit the number of customers in the store. A way to do this is to provide a limited number of shopping baskets and require that each customer entering the store has a shopping basket. When there are no more baskets, an incoming customre has to wait for a previous customer to leave (and return their basket).

In a pandemic, supermarkets are required to limit the number of customers in the store. A way to do this is to provide a limited number of shopping baskets and require that each customer entering the store has a shopping basket. When there are no more baskets, an incoming customre has to wait for a previous customer to leave (and return their basket).

This example demonstrates:

  • concurrency
    • customers come and shop concurrently
  • sequencing
    • a customer can shop only after obtaining a basket
    • a customer can use an item only after paying for it
    • ...
  • mutual exclusion
    • limited number of concurrently shopping customers
      • without side-effects on shared synchronization objects (such as semaphores)
  • linear & session types
    • obligation to return a basket enforced by the type-system
    • the type Shopping is a protocol between the store and the customer