Manyan Rust Crates
This repository includes two general-purpose Rust crates for embedding the Manyan TLA⁺ model checker in process without spawning a JVM or subprocess:
manyan: Safe, idiomatic Rust interface that runs a check, captures Manyan's report, and decodes counterexample states and actions into RustValues.manyan-sys: Low-level FFI bindings matchingmanyan.handmanyan-std.h, compiled directly from thecrates/manyan-sys/manyanC11 submodule usingcc.
Quick Example#
text
use std::path::Path;
use manyan::{Options, Status};
fn main() -> Result<(), manyan::Error> {
let options = Options {
check_deadlock: true,
workers: 1,
..Options::default()
};
let outcome = manyan::check(Path::new("DieHard.tla"), &options)?;
match outcome.status {
Status::Ok => {
println!("PASS ({} distinct states)", outcome.distinct.unwrap_or(0));
}
Status::Safety => {
println!("Violated: {:?}", outcome.violated);
for (i, state) in outcome.states.iter().enumerate() {
println!("State {}: {}", i + 1, state);
}
}
other => {
println!("Status: {:?}\n{}", other, outcome.output);
}
}
Ok(())
}
spec may be passed with or without the .tla extension. When options.config is None, Manyan looks for the sibling .cfg file next to the module.
Safe API (manyan)#
manyan::check#
text
pub fn check(spec: &Path, options: &Options) -> Result<Outcome, Error>
Initializes Manyan's standard modules (manyan_std_init), invokes manyan_check, captures stdout into Outcome::output, and converts the C result into owned Rust values.
Options#
| Field | Type | Default | Description |
|---|---|---|---|
config |
Option<PathBuf> |
None |
Path to a .cfg file; None uses the sibling <spec>.cfg. |
check_deadlock |
bool |
true |
Report deadlock when a reachable state has no enabled next step. |
workers |
u32 |
1 |
Number of worker processes expanding states (0 or 1 runs single-worker). |
invariants |
Vec<String> |
[] |
Additional TLA⁺ expressions checked as invariants beyond those in .cfg. |
Outcome#
| Field | Type | Description |
|---|---|---|
status |
Status |
Outcome mapped from TLC/Manyan exit codes. |
states |
Vec<Value> |
Counterexample states (each a Value::Function record mapping variable names to values). |
actions |
Vec<Value> |
Action records for each step (from TLCGet("action")); includes the back-edge step for lasso counterexamples. |
loop_to |
Option<usize> |
1-based state index that the last step loops back to in a liveness violation, or None. |
violated |
Vec<String> |
Names of violated invariants or temporal properties. |
generated |
Option<u64> |
Total states generated. |
distinct |
Option<u64> |
Distinct states found. |
left |
Option<u64> |
Unexplored states remaining in the queue when checking stopped. |
depth |
Option<u64> |
Maximum BFS depth reached. |
output |
String |
Full TLC-compatible text report emitted by Manyan. |
Status#
Matches TLC and Manyan's exit codes:
| Variant | C constant | Code | Meaning |
|---|---|---|---|
Status::Ok |
MANYAN_OK |
0 |
Model checking succeeded with no violations. |
Status::Assumption |
MANYAN_VIOLATION_ASSUMPTION |
10 |
An ASSUME failed. |
Status::Deadlock |
MANYAN_VIOLATION_DEADLOCK |
11 |
Deadlock detected. |
Status::Safety |
MANYAN_VIOLATION_SAFETY |
12 |
Invariant or action property violated. |
Status::Liveness |
MANYAN_VIOLATION_LIVENESS |
13 |
Temporal property violated. |
Status::Assert |
MANYAN_VIOLATION_ASSERT |
14 |
TLC!Assert failed. |
Status::Eval |
MANYAN_FAILURE_EVAL |
75 |
Evaluation error. |
Status::SafetyEval |
MANYAN_FAILURE_SAFETY_EVAL |
76 |
Evaluation error while checking an invariant. |
Status::LivenessEval |
MANYAN_FAILURE_LIVENESS_EVAL |
77 |
Evaluation error while checking a temporal property. |
Status::Parse |
MANYAN_ERROR_PARSE |
150 |
TLA⁺ parse or level error. |
Status::Config |
MANYAN_ERROR_CONFIG |
151 |
.cfg configuration error. |
Status::Error(i32) |
MANYAN_ERROR / other |
255 |
Any other error code. |
Value#
TLA⁺ values decoded from ManyanValue, ordered canonically (as in TLC):
Value::Bool(bool)Value::Int(i64)Value::Str(String)Value::Model(String)— model value nameValue::Tuple(Vec<Value>)— sequence / tupleValue::Function(Vec<(Value, Value)>)— function or record (records haveValue::Strkeys; useValue::field(&self, name: &str)to look up a record field)Value::Set(Vec<Value>)— finite setValue::Other(String)— printed fallback for non-enumerable or other values
Error#
Error::Io(std::io::Error)— failed to set up temporary file or redirect stdout.Error::Nul— a path or invariant expression contained an interior NUL byte.Error::Init—manyan_std_init()failed (out of memory).
Runtime Behavior Notes#
- Single check per process: Manyan uses process-global state, so
manyan::checkacquires a process-wideMutex. Concurrent calls from multiple Rust threads serialize automatically. (Parallelism within a single check is controlled viaOptions::workers.) - Stdout capture: While
manyan::checkruns, file descriptor1(stdout) is temporarily redirected viadup/dup2to an unlinked temporary file and restored before returning. Avoid writing to stdout from other threads concurrently withmanyan::check. - C build (
manyan-sys):crates/manyan-sys/build.rscompilesmanyan/manyan.candmanyan/manyan-std.cas C11 with-DMANYAN_NO_MAINand links-lmon non-Windows targets. Ensure the Git submodule is initialized (git submodule update --init --recursive) before building.