Quick Start

This walkthrough checks a bounded counter, then breaks its code to see the counterexample Tanny gives. It needs Tanny and the temporal-typing package: pip install tanny installs both.

1. Write a Specification#

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)

2. Check It#

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

Tanny explored all 6 reachable states: the invariant bounded holds in each, and each method body does what its Action says.

3. Break It#

Change self.x += 1 to self.x += 2 and check again:

bash
$ tanny check counter.py
FAIL counter.py::counter_model  conformance increment: predicate
    1. init: x=0

From the initial state, increment's body no longer satisfies its Action predicate. The exit code is 1, so a CI step fails.

4. See the TLA⁺#

bash
$ tanny translate counter.py counter_model

tanny translate prints Main.tla and Main.cfg, the exact module and configuration Tanny gives Manyan. Each method body is a TLA⁺ operator, and each obligation a tagged Assert:

Main.tla (excerpt)
A_increment ==
  /\ (v_x < 5)
  /\ LET r == Run_increment IN
     /\ Assert(r.o # "assert", "tanny|assertion|increment|" \o ToString(r.line))
     /\ r.o # "raise"
     /\ v_x' = r.s_x
     /\ Assert((v_x' = (v_x + 1)), "tanny|conformance|increment|predicate")

5. Machine-Readable Verdicts#

bash
$ tanny check --json counter.py
json
{
  "diagnostics": [],
  "unread": [],
  "verdicts": [
    {
      "detail": "predicate",
      "file": "counter.py",
      "kind": "conformance",
      "model": "counter_model",
      "name": "increment",
      "trace": ["1. init: x=0"],
      "verdict": "FAIL",
      "warnings": []
    }
  ]
}

Next: the Commands reference, or the Tour to learn the specification language.