Quick Start

This walkthrough builds a bounded counter, checks that its Python implementation conforms to its specification, and demonstrates how a checker catches a bug with a counterexample trace.

1. Write a Specification on a Dataclass#

Save the following as counter.py:

counter.py
from dataclasses import dataclass
from typing import Annotated

from temporal_typing import Action, Invariant, Model, Requires, State, prime, spec


@spec(bounded=Invariant(lambda s: 0 <= s.x <= 5))
@dataclass
class Counter:
    x: State[int] = 0

    def increment(self) -> Annotated[
        None,
        Requires(lambda s: s.x < 5),
        Action(lambda s: prime(s.x) == s.x + 1),
    ]:
        self.x += 1

    def reset(self) -> Annotated[
        None,
        Action(lambda s: prime(s.x) == 0),
    ]:
        self.x = 0


counter_model = Model(Counter)

Notice three things:

  • x: State[int] = 0 declares a state variable whose class-level default defines the initial state Init.
  • @spec(bounded=Invariant(...)) declares a safety invariant checked in every reachable state.
  • increment has both an Action(...) predicate and a Python body — declaring a conformance obligation that the body matches the predicate.

2. Validate with the Front-End#

bash
$ python -m temporal_typing counter.py
counter.py:26: counter_model: Counter; 2 actions; invariants bounded
1 file: 1 specification, 0 contracts, 1 model; 0 errors, 0 warnings

3. Model-Check with Tanny#

bash
$ tanny check counter.py
PASS counter.py::counter_model  6 states

4. Inspect the Generated TLA⁺#

bash
$ tanny translate counter.py counter_model

tanny translate prints the exact Main.tla module and Main.cfg passed to Manyan. You can also inspect this live in your browser using the TLA⁺ tab in the Interactive Tour.

5. Catch Code/Spec Drift#

Suppose someone later changes self.x += 1 to self.x += 2 in increment without updating the specification. Running tanny check counter.py immediately catches the mismatch:

text
FAIL counter.py::counter_model  conformance increment
    1. init: x=0
    2. increment(): x=2

And because Counter is plain Python, Counter().increment() still runs with zero overhead and type-checks under mypy --strict.