Automatically generate TLA+ specifications from program code, repositories, or system implementations. Use when asked to generate TLA+ spec, create TLA+ specification from code, convert program to TLA+, formalize system in TLA+, extract TLA+ model from code, or when working with formal specification of concurrent systems, distributed systems, protocols, algorithms, or state machines that need to be verified.
This skill enables automatic generation of TLA+ specifications from source code. It analyzes program structure, identifies state variables and transitions, and produces well-formed TLA+ modules with proper syntax and semantics.
Follow this sequential process to generate TLA+ specifications from code:
Read and understand the source code:
Ask clarifying questions if needed:
Identify variables that represent system state:
Look for:
Determine types:
Output: List of state variables with TLA+ type declarations
Extract operations that modify state:
Look for:
For each action, identify:
Output: List of actions with their effects
Identify how the system starts:
Output: Initial state predicate (Init)
Understand execution patterns:
Output: Understanding of how actions compose
Identify correctness conditions:
Safety properties (something bad never happens):
Liveness properties (something good eventually happens):
Output: List of properties to specify
Construct the specification following TLA+ syntax:
Module structure:
---- MODULE ModuleName ----
EXTENDS Naturals, Sequences, FiniteSets
CONSTANTS [constants]
VARIABLES [state variables]
vars == <<var1, var2, ...>>
Init == [initial state predicate]
Action1 == [action definition]
Action2 == [action definition]
...
Next == Action1 \/ Action2 \/ ...
Spec == Init /\ [][Next]_vars
TypeInvariant == [type constraints]
SafetyProperty == [safety properties]
====
Key elements:
See references/tlaplus_syntax.md for detailed syntax guide.
Document how program maps to TLA+:
State variable mapping:
Program Variable -> TLA+ Variable
---------------------------------
counter (int) -> counter \in Nat
buffer (array) -> buffer \in Seq(Data)
lock (bool) -> lock \in BOOLEAN
Action mapping:
Program Function -> TLA+ Action
--------------------------------
increment() -> Increment
send(msg) -> Send(msg)
acquire_lock() -> AcquireLock
Abstractions applied:
Assumptions made:
Create model checking configuration:
SPECIFICATION Spec
CONSTANTS
MaxValue = 10
NumProcesses = 3
INVARIANTS
TypeInvariant
SafetyProperty
PROPERTIES
LivenessProperty
Provide outputs in this structure:
[Complete .tla file content]
State Variables:
program_var → tla_var: [explanation]Actions:
program_function() → TLAAction: [explanation]Abstractions:
Assumptions:
[.cfg file content]
Program characteristics:
TLA+ approach:
Program characteristics:
TLA+ approach:
Program characteristics:
TLA+ approach:
Program characteristics:
TLA+ approach:
What to abstract:
What to preserve:
Abstraction levels:
Choose abstraction level based on verification goals.
C/C++:
Java:
Go:
Python:
Rust:
For detailed patterns, see references/language_patterns.md.