FuSe — A Simple Library Implementation of Binary Sessions

Overview

FuSe is a lightweight OCaml module that implements the session-based communication primitives in [1] and enables session type checking and inference. It works with any out-of-the-box installation of OCaml and supports the following features:

For questions, suggestions and bug reports please contact Luca Padovani.

Download

Installation Instructions

You need OCaml to compile FuSe.

  1. Compile the library and the examples:

    make
    
  2. Test an example:

    ./examples/Tmath
    
  3. Test session type inference:

    cd examples
    make Tmath.st
    

Documentation

Users may refer to the API documentation extracted with ocamldoc and to the tutorial.

The principles and internals of FuSe are described in [5].

Example

Below is an almost verbatim transcription in OCaml+FuSe of the bookshop example found in [1].

module Session = Session.Bare

let rec shopLoop s order =
  match Session.branch s with
  | `Add s -> let book, s = Session.receive s in
              shopLoop s (book :: order)
  | `CheckOut s -> let card, s = Session.receive s in
                   let address, s = Session.receive s in
                   Session.close s

let shop shopAccess =
  shopLoop (Service.accept shopAccess) []

let isChildrensBook book = true

let voucher card address c book =
  let c = if isChildrensBook book then
            let c = Session.select (fun x -> `Add x) c in
            Session.send book c
          else c in
  let c = Session.select (fun x -> `CheckOut x) c in
  let c = Session.send card c in
  let c = Session.send address c in
  Session.close c;
  book

let mother card address shopAccess sonAccess book =
  let c = Service.request shopAccess in
  let c = Session.select (fun x -> `Add x) c in
  let c = Session.send book c in
  let s = Service.request sonAccess in
  let s = Session.send (voucher card address c) s in
  let sonBook, s = Session.receive s in
  Session.close s

let son sonAccess book =
  let s = Service.accept sonAccess in
  let f, s = Session.receive s in
  let s = Session.send (f book) s in
  Session.close s

let _ =
  let mCard = "0123 4567 7654 3210" in
  let mAddress = "17 session type rd" in
  let mBook = "Life of Ada Lovelace" in
  let sBook = "Types and programming languages" in
  let shopAccess = Service.create () in
  let sonAccess = Service.create () in
  let _ = Thread.create shop shopAccess in
  let _ = Thread.create (son sonAccess) sBook in
  mother mCard mAddress shopAccess sonAccess mBook

Here are the (session) types inferred by OCaml, pretty printed in a more readable form by the rosetta utility that accompanies the library (the tutorial gives the correspondence between OCaml types and this notation). Note the amount of parametric polymorphism compared to the typing of the example as given in [1].

val shopLoop : rec X.&[ Add: ?α.X | CheckOut: ?β.?γ ] → α list → unit
val shop : rec X.&[ Add: ?α.X | CheckOut: ?β.?γ ] Service.t → unit
val isChildrensBook : α → bool
val voucher : α → β → rec X.⊕[ Add: !γ.X | CheckOut: !α.!β ] → γ → γ
val mother : α → β → &[ Add: ?γ.rec X.&[ Add: ?δ.X | CheckOut: ?α.?β ] ] Service.t →
             ?(δ → δ).!ε Service.t → γ → unit
val son : ?(α → β).!β Service.t → α → unit

  1. Simon J. Gay, Vasco T. Vasconcelos, Linear type theory for asynchronous session types, Journal of Functional Programming, 2010.

  2. Peter Thiemann and Vasco T. Vasconcelos: Context-Free Session Types, Proceedings of ICFP’16, 2016.

  3. Luca Padovani: Context-Free Session Type Inference, Proceedings of ESOP’17, 2017.

  4. Simon J. Gay and Malcolm Hole: Subtyping for session types in the pi calculus, Acta Informatica, 2005.

  5. Luca Padovani: A Simple Library Implementation of Binary Sessions, Journal of Functional Programming, 2017.