ST (st_parser v0.3.0)

View Source

Defines the core data structures for Session Types.

Session Types (ST) provide a formal way to describe communication protocols between different roles in a distributed system. This module defines the data structures that represent these session types after parsing.

The main session type constructs are:

  • SIn - For receiving messages (external choice)
  • SOut - For sending messages (internal choice)
  • SEnd - For terminating a session

Each of these has convenience constructor functions available in this module.

Summary

Types

A payload type that can be

t()

A session type that can be

Functions

Creates a branch in a session type.

Creates an end session type.

Creates an input session type (for receiving messages).

Convenience function to create an input with a single branch.

Creates an output session type (for sending messages).

Convenience function to create an output with a single branch.

Types

payload_type()

@type payload_type() :: atom() | {:list, [atom()]} | {:tuple, [payload_type()]}

A payload type that can be:

  • A basic type (:binary, :number, :boolean, :unit)
  • A list of a basic type
  • A tuple containing multiple payload types

t()

@type t() :: ST.SIn.t() | ST.SOut.t() | ST.SEnd.t()

A session type that can be:

  • An input type (receiving messages)
  • An output type (sending messages)
  • A termination marker

Functions

branch(label, payload, continue_as)

@spec branch(atom(), payload_type(), t()) :: ST.SBranch.t()

Creates a branch in a session type.

Parameters

  • label: The message label (atom)
  • payload: The payload type
  • continue_as: The continuation session type

Example

iex> ST.branch(:request, :binary, %ST.SEnd{})
%ST.SBranch{
  label: :request,
  payload: :binary,
  continue_as: %ST.SEnd{}
}

end_session()

@spec end_session() :: ST.SEnd.t()

Creates an end session type.

Example

iex> ST.end_session()
%ST.SEnd{}

input(from, branches)

@spec input(atom(), [ST.SBranch.t()]) :: ST.SIn.t()

Creates an input session type (for receiving messages).

Parameters

  • from: The role sending the message(s)
  • branches: List of possible branches that can be received

Example

iex> branch = ST.branch(:ack, :unit, %ST.SEnd{})
iex> ST.input(:server, [branch])
%ST.SIn{
  from: :server,
  branches: [%ST.SBranch{
    label: :ack,
    payload: :unit,
    continue_as: %ST.SEnd{}
  }]
}

input_one(from, label, payload, continue_as)

@spec input_one(atom(), atom(), payload_type(), t()) :: ST.SIn.t()

Convenience function to create an input with a single branch.

Parameters

  • from: The role sending the message
  • label: The message label
  • payload: The payload type
  • continue_as: The continuation session type

Example

iex> ST.input_one(:server, :ack, :unit, %ST.SEnd{})
%ST.SIn{
  from: :server,
  branches: [%ST.SBranch{
    label: :ack,
    payload: :unit,
    continue_as: %ST.SEnd{}
  }]
}

output(to, branches)

@spec output(atom(), [ST.SBranch.t()]) :: ST.SOut.t()

Creates an output session type (for sending messages).

Parameters

  • to: The role receiving the message(s)
  • branches: List of possible branches that can be sent

Example

iex> branch = ST.branch(:request, :binary, %ST.SEnd{})
iex> ST.output(:client, [branch])
%ST.SOut{
  to: :client,
  branches: [%ST.SBranch{
    label: :request,
    payload: :binary,
    continue_as: %ST.SEnd{}
  }]
}

output_one(to, label, payload, continue_as)

@spec output_one(atom(), atom(), payload_type(), t()) :: ST.SOut.t()

Convenience function to create an output with a single branch.

Parameters

  • to: The role receiving the message
  • label: The message label
  • payload: The payload type
  • continue_as: The continuation session type

Example

iex> ST.output_one(:client, :request, :binary, %ST.SEnd{})
%ST.SOut{
  to: :client,
  branches: [%ST.SBranch{
    label: :request,
    payload: :binary,
    continue_as: %ST.SEnd{}
  }]
}