Checking designs in Rust (cargo test)
You can write a design model directly in Rust right next to your production code and check
it in cargo test. When a promise breaks, the test fails and prints the shrunk story as
its panic message.
1. Add elinvar as a dev-dependency
[dev-dependencies]
elinvar = "0.1"
elinvar brings the engine, the Alloy reader (elinvar::als), and the command line’s
two small dependencies, rayon and serde_json, with zero unsafe code and no C/Java
build tools.
2. Check a static design
Import elinvar::prelude::*, declare sets and fields on a Model, add rules with
m.fact(...) (or m.fact_named(...)), and assert your promise with assert_holds():
use elinvar::prelude::*;
#[test]
fn managers_always_reach_the_top() {
let mut m = Model::new();
let employee = m.set("Employee");
let manager = m.field_with("manager", &employee, Multiplicity::Lone, &employee);
// Rule: no cycles in the reporting chain.
m.fact_named(
"Acyclic",
all([&employee], |[e]| e.is_in(&e.join(&manager.closure())).not()),
);
// Promise: everyone reaches someone with no manager.
let promise = all([&employee], |[e]| {
some([&employee], |[top]| {
top.is_in(&e.join(&manager.reflexive_closure()))
.and(&top.join(&manager).no())
})
});
m.check(&promise)
.named("EveryoneHasATop")
.scope(4)
.assert_holds();
}
If you remove Acyclic and only forbid self-management, cargo test fails immediately and
prints the two-employee cycle.
3. Check a state machine over time with action
In Alloy text, every step predicate must explicitly repeat every relation that stays
unchanged. In Rust, m.action(...) handles frame conditions automatically: anything you
do not touch in an action stays the same.
Here is the sharing app from crates/elinvar/tests/readme.rs:
use elinvar::prelude::*;
fn notes_need_access() -> (Model, Formula) {
let mut m = Model::new();
let person = m.set("Person");
let item = m.set("Item");
let owner = m.field_with("owner", &item, Multiplicity::One, &person);
let follows = m.changing_field("follows", &person, &person);
let shared = m.changing_field("shared", &item, &person);
let notes = m.changing_field("notes", &item, &person);
let can_open = owner.union(&shared.intersection(&owner.join(&follows.transpose())));
m.fact(shared.no().and(¬es.no()));
m.action("share", [&item, &person], |step, [i, p]| {
step.when(i.join(&owner).is_in(&p.join(&follows)));
step.add(&shared, i.product(&p));
});
m.action("unfollow", [&person, &person], |step, [p, q]| {
step.when(q.is_in(&p.join(&follows)));
step.remove(&follows, p.product(&q));
});
let note = m.action("note", [&item, &person], |step, [i, p]| {
step.when(p.is_in(&i.join(&shared))); // forgot to check follows
step.add(¬es, i.product(&p));
});
let promise = always(all([&item, &person], |[i, p]| {
note.happens(&[&i, &p])
.implies(&p.is_in(&i.join(&can_open)))
}));
(m, promise)
}
Running m.check(&promise).named("NotesNeedAccess").scope(3).steps(6).assert_holds()
fails in cargo test with the shrunk 3-step story:
check NotesNeedAccess: COUNTEREXAMPLE in 4 states, with Person: 2, Item: 1
start
owner = {Item0 -> Person1}
follows = {Person0 -> Person1}
step 1 share(Item0, Person0) shared gains {Item0 -> Person0}
step 2 unfollow(Person0, Person1) follows loses {Person0 -> Person1}
step 3 note(Item0, Person0) notes gains {Item0 -> Person0}
broken at state 2, as step 3 begins
4. Working with reports, cores, diffs, and diagrams
Instead of panicking via assert_holds(), call .report() or .report_explained() to
inspect structured data:
let report = m.check(&promise).scope(4).report();gives a serializableReportwithverdict,instance,trace,shrunk_to,errors,stats, andstory.let report = m.check(&promise).scope(4).report_explained();also populatesreport.core(usedandunusedrule names) when the verdict isHoldsorVacuous.elinvar::diff_models(&old, &new, 3, &Options::default())compares twoModels and returns aDiffReport(Equivalent,Tightened,Loosened,Diverged).elinvar::render_report(&report, DiagramFormat::Mermaid)(orDiagramFormat::Dot) renders any counterexample or found instance as a diagram.m.check(&promise).report_with(MySolver::new)plugs in a customSolverimplementation for solving and shrinking.