> ## Documentation Index
> Fetch the complete documentation index at: https://docs.expectedparrot.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Language semantics

> Shared-state language specification: language semantics.

> [!NOTE]
>
> This is the Draft 0.1 language specification. Its conformance requirements include targets beyond the current implementation, such as complete static typing and registered-algorithm isolation. Derived
> [capability manifests and preflight](https://docs.expectedparrot.com/en/latest/shared-state/machines.md#interpreter-capabilities)
> are implemented locally; live service discovery is a separate deployment task. Local
> [execution quotas](https://docs.expectedparrot.com/en/latest/shared-state/machines.md#execution-limits) are implemented;
> they do not sandbox registered Python extensions. The [execution contracts](https://docs.expectedparrot.com/en/latest/coordinated-research/execution-contracts.md) describe the guarantees implemented today.

Status: **Draft 0.1**

This specification defines the meaning of the EDSL shared-state DSL independently
of any particular local or remote implementation.

The normative words **MUST**, **MUST NOT**, **SHOULD**, **SHOULD NOT**, and **MAY**
have their usual RFC 2119 meanings. Code examples are informative unless a
section labels them normative.

## How to read this specification

Each formal section is followed where useful by an informative worked example.
The examples use one activity poll throughout:

```python theme={null}
activities = ["bike ride", "sailing", "hike", "beach day"]

fields = {
    "votes": state_field(
        StateType.map(StateType.text(), StateType.choice(activities)),
        initial={},
    ),
}

vote = Command(
    inputs={
        "voter": StateType.text(),
        "activity": StateType.choice(activities),
    },
    effects=(
        put("votes", arg("voter"), arg("activity"), once=True),
    ),
)
```

The poll is intentionally simple. It lets the reader connect the same concrete
field, command, input, state, and view to the notation in later sections.

## Artifacts

* [Data model](https://docs.expectedparrot.com/en/latest/shared-state/semantics/data-model.md) defines machines, states, scopes, and identifiers.
* [Expressions and types](https://docs.expectedparrot.com/en/latest/shared-state/semantics/expressions-and-types.md) defines expression evaluation and static typing.
* [Command transitions](https://docs.expectedparrot.com/en/latest/shared-state/semantics/command-transitions.md) defines requirements, effects, and atomic commit.
* [Concurrency and reads](https://docs.expectedparrot.com/en/latest/shared-state/semantics/concurrency-and-reads.md) defines per-scope ordering, retries, and reads.
* [Views and capabilities](https://docs.expectedparrot.com/en/latest/shared-state/semantics/views-and-capabilities.md) defines views and information access.
* [Lifecycle](https://docs.expectedparrot.com/en/latest/shared-state/semantics/lifecycle.md) distinguishes complete, terminal, closed, and settled.
* [Remote execution and security](https://docs.expectedparrot.com/en/latest/shared-state/semantics/remote-and-security.md) defines local/remote equivalence and resource limits.
* [Versioning and compatibility](https://docs.expectedparrot.com/en/latest/shared-state/semantics/versioning.md) defines compatibility and registered capabilities.
* [machine.schema.json](https://github.com/expectedparrot/edsl/blob/ecd00cb21151c56811d37e3cfe7c10906bd10fd7/docs/shared_state_semantics/machine.schema.json) defines the serialized envelope and structural grammar.
* [test-vectors/](https://github.com/expectedparrot/edsl/tree/ecd00cb21151c56811d37e3cfe7c10906bd10fd7/docs/shared_state_semantics/test-vectors/) contains implementation-independent examples.
* [run\_vectors.py](https://github.com/expectedparrot/edsl/blob/ecd00cb21151c56811d37e3cfe7c10906bd10fd7/docs/shared_state_semantics/run_vectors.py) executes the vectors against the reference interpreter.

## Central semantic promise

For one scope, committed commands form a serial history:

```text theme={null}
S0 --c1--> S1 --c2--> S2 ... --cn--> Sn
```

Each command evaluates its requirement and every effect expression against one
immutable committed input snapshot. It proposes one complete replacement state.
The runtime validates and commits all of that state or none of it.

Reads name the exact snapshot and viewer context used to render a view. Local
and remote implementations MUST produce the same observable result for the
same versioned machine, history, context, and registered capabilities.

## Notation

Expression evaluation is written:

$$
M, S, I, X, L \vdash e \Downarrow v
$$

where `M` is the machine, `S` state, `I` command inputs, `X` execution context,
`L` lexical collection locals, `e` an expression, and `v` its value.

The environments have different ownership and lifetimes:

* $S$ is the persistent, versioned machine-state snapshot. Commands may propose
  changes to its declared fields.
* $I$ is the finite map of actual arguments supplied to this command invocation.
* $X$ is read-only execution context supplied by EDSL, such as authenticated
  participant identity, group, interview ID, viewer role, run round, and
  lifecycle status. A machine cannot modify $X$.
* $L$ contains temporary bindings introduced while evaluating collection
  expressions and disappears after evaluation.

A command transition is written:

$$
M \vdash
\langle S, c, I, X \rangle
\longrightarrow
\langle S', O \rangle
$$

where `S'` is the committed or unchanged state and `O` is advisory outcome data.

Here $c$ and $I$ are intentionally separate. The command identifier $c$ selects
one definition from the machine's command map $D$; $I$ supplies the actual input
values for this invocation. For example:

$$
c = \mathit{vote}
\qquad
I = \{\mathit{voter}\mapsto\text{Amina},
      \mathit{activity}\mapsto\text{hike}\}
$$

The same command $c=\mathit{vote}$ can be invoked many times with different
input maps $I$.

$S$ is not part of $X$ because it has different semantics. $S$ is durable shared
data, has a snapshot ID, participates in concurrency control, and is the target
of atomic transitions. $X$ is immutable authority and execution metadata that
may differ for two readers of the same $S$. Keeping them separate prevents a
machine from modifying identity or authorization data and permits
viewer-specific views over one shared snapshot.

## Conformance

An implementation conforms to a DSL version only if it:

1. accepts every valid test vector for that version;
2. rejects every invalid vector with the specified error class;
3. produces the specified state, view, completion, and outcome values;
4. implements the security and resource requirements in this specification.

Passing the included reference vectors is necessary but not sufficient for
production conformance.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.