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 Rust Values.
  • manyan-sys: Low-level FFI bindings matching manyan.h and manyan-std.h, compiled directly from the crates/manyan-sys/manyan C11 submodule using cc.

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 name
  • Value::Tuple(Vec<Value>) — sequence / tuple
  • Value::Function(Vec<(Value, Value)>) — function or record (records have Value::Str keys; use Value::field(&self, name: &str) to look up a record field)
  • Value::Set(Vec<Value>) — finite set
  • Value::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::check acquires a process-wide Mutex. Concurrent calls from multiple Rust threads serialize automatically. (Parallelism within a single check is controlled via Options::workers.)
  • Stdout capture: While manyan::check runs, file descriptor 1 (stdout) is temporarily redirected via dup/dup2 to an unlinked temporary file and restored before returning. Avoid writing to stdout from other threads concurrently with manyan::check.
  • C build (manyan-sys): crates/manyan-sys/build.rs compiles manyan/manyan.c and manyan/manyan-std.c as C11 with -DMANYAN_NO_MAIN and links -lm on non-Windows targets. Ensure the Git submodule is initialized (git submodule update --init --recursive) before building.