Skip to content
Proyectos

Acerca de

Formally verify TypeScript code with TLA+

Otros proyectos

Cómo se miden estas cifras

Crecimiento de estrellas medido

in 24h
+3
in 7 days
+12
in 15 days
+25
in 1 month
Recopilando historial

Cambios netos en períodos que se solapan, no totales diarios. Los datos no disponibles se dejan en blanco. Las estrellas indican interés, no calidad.

README.md

Original

TLA-ts

version downloads

Formally verify TypeScript code with TLA+.

Under the hood, TLA-ts is a transpiler that turns TypeScript/JavaScript into TLA+. It uses a 1:1 reimplementation of TLC/TLAPM in TypeScript that runs 2× faster.

View playground

How it works

  1. Run npx tla [file].ts.
  2. TLA-ts transpiles TypeScript into TLA+.
  3. TLA-ts model checks TypeScript code and finds violations.

Here, TLA-ts checks a counter:

// counter.ts
let count = 0;

export const increment = () => {
  count += 1;
};

export const small = () => count < 3;

TLA-ts transpiles it into this TLA+ module:

---- MODULE Counter ----
EXTENDS Integers, Sequences, FiniteSets, TLC
VARIABLES count
vars == <<count>>
Init == count = 0
increment == count' = count + 1
Next == increment
Spec == /\ Init
        /\ [][Next]_vars
small == count < 3 \* violated after three calls to increment
====

Install

Paste this prompt into your coding agent:

Read https://tla-ts.com/llms.txt, then use TLA-ts to formally verify the TypeScript code in this project. Show me each violation it finds with the steps that cause it, and ask before you change any code.

Or install it yourself:

npm install tla

You can also skip the install and run npx tla [file].ts.

CLI

Pass a TypeScript file, a TLA+ spec, or a TLA+ module with proofs:

# transpile TypeScript to TLA+ and check it
npx tla counter.ts

# check a TLA+ spec
npx tla Counter.tla

# prove a TLA+ module with TLAPM
npx tla tlapm Module.tla

For a TLA+ spec, TLA-ts reads Counter.cfg from the same folder and takes TLC's options.

Name a TypeScript model that's separate from your code [name].tla.ts, such as counter.tla.ts.

API

let

let balances = { alice: 2, bob: 2 };
let bit: 0 | 1;
Declaration Description
With a value A state variable that starts at the value
Without a value A state variable that starts at every value of its type

Only a top-level let is state.

export

export const transfer = (from: "alice" | "bob", to: "alice" | "bob", amount: 1 | 2) => {
  if (balances[from] < amount) return;
  balances[from] -= amount;
  balances[to] += amount;
};

export const noOverdraft = () => balances.alice >= 0 && balances.bob >= 0;
Exported function Description
Changes state An action. TLA-ts calls it from every state, with every value of its parameters' types
Takes no parameters and returns a boolean An invariant. The check fails in a state where it returns false
Returns any other value A check. TLA-ts calls it in every state, and the check fails where it throws

A call that returns without changing state isn't a step, so if (…) return; says when an action can run.

Types

Type Values
A union of literals Each literal
boolean true and false
number The integers from -3 to 3
string "", "a" and "b"
T[] Every array up to 3 long
An object or a tuple of them Every combination

Tags

/** @fair */
export const step = (by: 1 | 2) => {
  if (count < 4) count += by;
};

/** @always */
export const neverDecreases = (before: { count: number }) => count >= before.count;
Tag Description
@initially The model starts only in states where the function returns true
@always The function returns true for every step. It takes the state before the step
@eventually The function returns true at some point
@alwaysEventually The function returns true again and again, forever
@eventuallyAlways From some point on, the function returns true forever
@leadsTo other After the function returns true, other returns true too
@fair, @strongFair TLA-ts ignores runs that could call the action forever but never do
@terminating The model may stop in states where the function returns true
@noDeadlock The model may stop in any state. It goes on any top-level statement
@constraint TLA-ts doesn't search past states where the function returns false
@actionConstraint TLA-ts skips steps where the function returns false. It takes the state before
@postcondition TLA-ts checks the function once, after the search
@view Two states are the same when the function returns the same value

compile

const spec = await compile({ source, fileName: "counter.ts" });
const result = await check(spec);
Option Description
source The text of the TypeScript file
fileName The path of the file. Imports resolve from it
libraries TypeScript's built-in lib.*.d.ts files by name. Pass them only in a browser

compile runs in Node only. In a browser it throws a TlaTypeScriptError.

check

const result = await check({ source, config });
result.status; // "success", "deadlock", "invariant-violation", ...
result.violation?.trace;
Option Description
source The TLA+ module
config A TLC configuration
loadModule Returns the source of an imported module
workers The number of worker threads. Defaults to 1

prove

const result = await prove({ source });
result.obligations[0].status; // "proved", "trivial", "failed", or "unsupported"
Option Description
source The TLA+ module
loadModule Returns the source of an imported module
onObligation Receives each obligation as soon as it's decided
workers The number of worker threads. Defaults to 1

parseModule

const module = parseModule(source);
module.name; // "Spec"
module.definitions;

parseConfig

const config = parseConfig(configSource);
config.invariants.push("TypeOK");
await check({ source, config });

formatValue

const step = result.violation.trace[0];
formatValue(step.state.queue); // "<<1, 2>>"

Errors

Error Thrown when
TlaError Any of the errors below
TlaSyntaxError The module doesn't parse
TlaConfigError The configuration is invalid
TlaEvaluationError Evaluating the spec fails
TlaUnsupportedError The spec or call uses something TLA-ts can't do

Benchmarks

Wall time on one worker, including JVM and Node startup. Both checkers report the same state counts on every model.

Spec Distinct states TLC TLA-ts Speedup
Two-phase commit 282,251 4.32 s 2.01 s 2.2×
Dining philosophers 128,889 1.06 s 0.71 s 1.5×
Bounded queue 44,044 0.37 s 0.17 s 2.2×
542 conformance models 986.6 s 403.3 s 2.4×

Wall time on 8 workers, also including startup. Each time is the median of 3 runs on an Apple M4 Pro.

Spec Distinct states TLC TLA-ts Speedup
Raft with reconfiguration 298,493 10.50 s 8.96 s 1.2×
Sailfish 109,604 4.34 s 4.07 s 1.1×
Fast Paxos 25,617 3.91 s 2.57 s 1.5×
Buffered random access file 3,316 4.70 s 3.61 s 1.3×

Development

The toolchain needs Node 26. vite-plus runs the version in .node-version once you enable its managed mode with vp env on node.

pnpm install
pnpm test
pnpm dev

pnpm dev starts the website in packages/website.

Run the conformance suite against TLC's recorded results:

pnpm --filter tla conformance:fetch
pnpm --filter tla conformance

Run the TLC and tlapm conformance suites in packages/conformance against the built CLI:

pnpm build
pnpm --filter @tla-ts/conformance fetch-tlc
pnpm --filter @tla-ts/conformance fetch-tlapm
pnpm --filter @tla-ts/conformance conformance

Run every tracked JavaScript and TypeScript source/test file from the 100 selected npm packages at pinned upstream revisions:

pnpm --filter @tla-ts/conformance fetch-npm
pnpm --filter @tla-ts/conformance conformance:npm

Both commands accept --package <name>. Install each upstream project's dependencies before checking it. Results in packages/conformance/.results/npm include failure notes and incomplete export coverage. Known gaps are in packages/conformance/npm-notes.json. Checks use finite input domains and report thrown exceptions.

Rerun the benchmarks with TLA2TOOLS_JAR set to the path of tla2tools.jar:

pnpm --filter tla bench
pnpm --filter tla bench:corpus

License

MIT

Thanks to Tierney Cyren for donating the tla package name.

Explorar proyectos relacionados

Cargando…