ST (st_parser v0.2.0)
View SourceDefines 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
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
@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
@type t() :: SIn.t() | SOut.t() | SEnd.t()
A session type that can be:
- An input type (receiving messages)
- An output type (sending messages)
- A termination marker
Functions
@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 typecontinue_as
: The continuation session type
Example
iex> ST.branch(:request, :binary, %ST.SEnd{})
%ST.SBranch{
label: :request,
payload: :binary,
continue_as: %ST.SEnd{}
}
@spec end_session() :: ST.SEnd.t()
Creates an end session type.
Example
iex> ST.end_session()
%ST.SEnd{}
@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{}
}]
}
@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 messagelabel
: The message labelpayload
: The payload typecontinue_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{}
}]
}
@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{}
}]
}
@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 messagelabel
: The message labelpayload
: The payload typecontinue_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{}
}]
}