Specifications & State

A specification is a Python class — a plain class, a @dataclass, a typing.Protocol, or a Generic. Its fields declare constants and state variables, @spec(...) attaches class-level clauses, and its methods declare actions, pure operators, and refinement mappings.

Field Roles#

AnnotationTLA⁺Meaning
c: Constant[T]CONSTANT cFixed by the Model; identical in every state. Cannot be written by any action body.
v: State[T]VARIABLE vMutable state variable kept by the implementation and included in vars.
g: Ghost[T]VARIABLE gSpecification-only state variable (e.g., an audit history or prophecy). Included in vars; method bodies need not maintain it.
Decorator ordering with @dataclass(slots=True)

Always place @spec(...) above @dataclass(...). Putting @dataclass(slots=True) above @spec replaces the class object after @spec runs and is rejected as misplaced-clause.

Class Clauses in @spec(...)#

Every keyword argument to @spec(...) names a clause. The clause's predicate takes a single parameter s representing the state:

python
@spec(
    valid_owner=Assume(lambda s: s.owner != ""),
    init=Init(lambda s: s.balance == 0 and s.ledger == ()),
    within_limit=Invariant(lambda s: s.balance >= -s.credit_limit),
    ledger_append_only=ActionInvariant(lambda s: prime(s.ledger)[: len(s.ledger)] == s.ledger),
    fair=Fairness(lambda s: weak_fairness(s.release())),
    progress=Property(lambda s: leads_to(s.available == 0, s.available > 0)),
)

@spec stores clauses externally (retrievable via clauses_of(cls)) and adds nothing to the class's attributes or type, so a Protocol keeps only its declared members.

Initial States & Defaults#

The initial-state predicate Init is the conjunction of:

  1. Every Init(lambda s: ...) clause in @spec.
  2. Every class-level or dataclass default on a State or Ghost field (x: State[int] = 0 means s.x == 0).

__init__ is ordinary Python and does not participate in Init. A concrete specification variable with no default and no Init clause mentioning it is rejected with no-init. An Init clause may permit multiple initial values (e.g., s.hr in range(1, 13)), exploring from every starting state.

Inheritance (Composition, Not Refinement)#

A subclass inherits the declarations and clauses of its bases in Python's Method Resolution Order (MRO). A method overriding a base action keeps the base's clauses: its body must conform to the base's Action predicate and satisfy its Requires, Ensures, and Modifies. Inheritance is composition; to claim a concrete class implements an abstract specification with a different state representation, use Refines[A].