Skip to main content

SessionType

Enum SessionType 

Source
pub enum SessionType {
    End,
    Send {
        payload: Payload,
        credit: Option<u64>,
        cont: Box<SessionType>,
    },
    Recv {
        payload: Payload,
        credit: Option<u64>,
        cont: Box<SessionType>,
    },
    Select(BTreeMap<String, SessionType>),
    Branch(BTreeMap<String, SessionType>),
    Rec(String, Box<SessionType>),
    Var(String),
}
Expand description

A session type — the protocol of one endpoint of a connection (§3.1 of the paper). Select/Branch carry their labelled continuations in a BTreeMap so the label set is canonically ordered (deterministic duality + equality).

Serialize + Deserialize — §Fase 41.g sealed-snapshot resume needs the residual cursor + the protocol schema serialisable. The encoding is stable across the algebra layer + the enterprise persistence layer: the same JSON shape goes into the AAD-bound cognitive_states ciphertext and comes back out via [SessionRuntime::resume].

Variants§

§

End

end — the dialogue is complete.

§

Send

!ⁿA.S — send a value of type A, then behave as S. The optional credit is the Fase 41.c index n (paper §4.2): Some(n) types a send that requires n > 0 available credit (the “no rule at n = 0” axiom makes Some(0) unprovable); None is the unbounded fragment !∞A.S.

Fields

§payload: Payload
§credit: Option<u64>
§

Recv

?ⁿA.S — receive a value of type A, then behave as S. Symmetric to SessionType::Send: the index n bounds the receiver-side window.

Fields

§payload: Payload
§credit: Option<u64>
§

Select(BTreeMap<String, SessionType>)

⊕{ℓᵢ:Sᵢ} — internal choice: this endpoint selects a label.

§

Branch(BTreeMap<String, SessionType>)

&{ℓᵢ:Sᵢ} — external choice: this endpoint offers the branches.

§

Rec(String, Box<SessionType>)

μX.S — recursive session (equirecursive: μX.S ≡ S[μX.S/X]).

§

Var(String)

X — a recursion variable (bound by an enclosing Rec).

Implementations§

Source§

impl SessionType

Source

pub fn send(payload: impl Into<String>, then: SessionType) -> SessionType

!A.S — unbounded send (credit = None, the pre-41.c fragment).

Source

pub fn recv(payload: impl Into<String>, then: SessionType) -> SessionType

?A.S — unbounded receive (credit = None).

Source

pub fn send_credit( payload: impl Into<String>, n: u64, then: SessionType, ) -> SessionType

!ⁿA.S — credit-refined send (Fase 41.c, paper §4.2). The continuation then runs in the same window — the budget is global to the socket; the n here is the snapshot of available credit demanded at this step.

Source

pub fn recv_credit( payload: impl Into<String>, n: u64, then: SessionType, ) -> SessionType

?ⁿA.S — credit-refined receive (Fase 41.c).

Source

pub fn select( branches: impl IntoIterator<Item = (String, SessionType)>, ) -> SessionType

Source

pub fn branch( branches: impl IntoIterator<Item = (String, SessionType)>, ) -> SessionType

Source

pub fn rec(var: impl Into<String>, body: SessionType) -> SessionType

Source

pub fn var(name: impl Into<String>) -> SessionType

Source

pub fn dual(&self) -> SessionType

The dual S⊥: swaps sendrecv and selectbranch, recursing into continuations; end, Rec binders and Vars are preserved. Payloads and the credit index n are unchanged — (!ⁿA.S)⊥ = ?ⁿA.S⊥ (same A, same n, opposite direction). Symmetric credit is the standard credit-flow semantics (Rast lineage): the sender’s window-of-n is exactly what the receiver-side is sized to absorb.

Source

pub fn unfold_head(&self) -> SessionType

Unfold every leading Rec so the head constructor is exposed: μX.S ↦ S[μX.S/X], repeated. Terminates for contractive types (a guard appears under each Rec before the variable recurs).

Public so the 41.d runtime can drive the session-type cursor over a live connection: after every operational step the continuation is re-unfolded so the cursor never carries a leading Rec for the state machine to interpret.

Source

pub fn equiv(&self, other: &SessionType) -> bool

Equirecursive equality: S ≡ T iff their infinite unfoldings coincide. Decided by the standard coinductive algorithm — assume the pair equal, unfold leading Recs, compare heads, recurse; a re-encountered pair is discharged by the assumption (the greatest fixed point). Terminates because a regular type has finitely many distinct sub-pairs.

Source

pub fn is_dual_to(&self, peer: &SessionType) -> bool

The connection law (§3.2): a connection whose two endpoints are typed self and peer is well-formed iff peer ≡ self⊥. Symmetric up to involutivity ((S⊥)⊥ ≡ S).

Source

pub fn with_credit(&self, n: u64) -> SessionType

Stamp every (recursively-reachable) Send and Recv with the credit index n. Idempotent on already-stamped types. Used by the type checker to lift the socket’s backpressure: credit(k) annotation onto the bare session protocol so the algebra-level analysis can discharge the constraint.

Source

pub fn has_send_at_zero(&self) -> Option<Payload>

The “no rule at n = 0” axiom (paper §4.2): an explicit !⁰A.S in the type is unprovable — there is no typing rule for a send at zero available credit. Returns the offending payload of the first such send (in a deterministic left-to-right walk) if any.

Decidable in linear time over the type structure.

Source

pub fn credit_analyse(&self, budget: u64) -> Result<(), CreditError>

Decide the credit conformance of self against a budget k (the socket’s backpressure: credit(k) window). This is the Presburger discharge — the constraints are linear arithmetic over the naturals, so satisfiability is decidable; the algorithm here is the direct fixpoint formulation specialised to closed, contractive session types (Rast lineage, §4.2 of the paper).

The check fires three kinds of error:

  1. Send at zero — an explicit !⁰A.S in the type. Unprovable by construction (no typing rule applies).
  2. Burst overflow — a straight-line send burst exceeding the available window. With initial budget k, the abstract trace must never reach available_credit < 0 at a send.
  3. Loop unsustainability — a recursive body whose per-iteration net send count Δ = #send − #recv is strictly positive: each iteration drains the window, so unbounded iteration is unsound under any finite budget. (Δ ≤ 0 is the Presburger fixpoint inequality.)

Returns Ok(()) if the protocol is conformant, or CreditError with the offending witness. Total over closed, contractive session types.

Source

pub fn recurring_paths(&self, x: &str) -> Vec<(u64, u64)>

Enumerate the recurring paths of self w.r.t. recursion variable x — every trace from the root that reaches Var(x). Each path is reported as (#send, #recv); terminating paths (reaching End or a different free variable) are dropped (they don’t iterate, so they don’t constrain unbounded sustainability). Shadowing Rec(x, …) cuts the descent — references inside refer to the inner binder.

Total in time linear in the size of self; the path count is bounded by the number of leaves of the choice tree.

Source

pub fn credit_delta(&self, x: &str) -> (u64, u64)

Worst-case (maximum-Δ) recurring path of self w.r.t. x. Used by the type checker to report the offending iteration count. Returns (0, 0) if there are no recurring paths.

Source

pub fn projects_to_sse(&self) -> bool

True iff self lies in the SSE producer fragment: the connection only sends to its peer. Concretely the type contains only End, Send, internal-Select, Rec, and Var — no Recv (would mean the producer expects client input) and no Branch (would mean the producer offers a choice the client picks). For such a type the §4.4 identity S_SSE = Π↓(S_WS) holds with Π↓ = id: the protocol is already the SSE fragment, runnable over W3C SSE without WebSocket bidirectionality.

Total over closed, contractive session types; linear in the size of self.

Source

pub fn projects_to_sse_consumer(&self) -> bool

Dual of [projects_to_sse] — the SSE consumer fragment: the connection only receives from its peer (End, Recv, external-Branch, Rec, Var). The §4.4 theorem Π↓(S)⊥ = Π↑(S⊥) ties this to projects_to_sse via duality: S.projects_to_sse() ⇔ S.dual().projects_to_sse_consumer().

Source

pub fn has_polarity(&self, p: Polarity) -> bool

Unified polarity test. The two SSE fragments are exactly the two inhabitants of Polarity: Producer = !/⊕/end/μ/var-only and Consumer = ?/&/end/μ/var-only.

Trait Implementations§

Source§

impl Clone for SessionType

Source§

fn clone(&self) -> SessionType

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SessionType

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for SessionType

Source§

fn deserialize<__D>( __deserializer: __D, ) -> Result<SessionType, <__D as Deserializer<'de>>::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for SessionType

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Eq for SessionType

Source§

impl Hash for SessionType

Source§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for SessionType

Source§

fn eq(&self, other: &SessionType) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for SessionType

Source§

fn serialize<__S>( &self, __serializer: __S, ) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for SessionType

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<Unshared, Shared> IntoShared<Shared> for Unshared
where Shared: FromUnshared<Unshared>,

Source§

fn into_shared(self) -> Shared

Creates a shared type from an unshared type.
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more