Practical examples
Elinvar shines whenever several simple rules interact and you want to know whether an edge case slips through the cracks. Below are five everyday problems and how to check them.
1. Permissions and access control
The problem: A document system has groups, users, and documents. Groups grant access to documents, and users belong to groups. A developer writes the check: “every document a user can access must be granted by some group.” Does that guarantee users only access documents granted by their own groups?
Model (corpus/holes/access.als):
sig Group { grants: set Doc }
sig User { member: set Group, access: set Doc }
sig Doc {}
fact EveryAccessIsGranted {
all u: User, d: Doc | d in u.access implies some g: Group | d in g.grants
}
assert UsersOnlyReachOwnGroups {
all u: User, d: Doc | d in u.access implies some g: u.member | d in g.grants
}
check UsersOnlyReachOwnGroups for 3
Run it:
elinvar check corpus/holes/access.als
Output:
check check$1: COUNTEREXAMPLE with Group: 1, User: 1, Doc: 1
Group = {Group0}
User = {User0}
Doc = {Doc0}
grants = {Group0 -> Doc0}
member = {}
access = {User0 -> Doc0}
searched every case up to Group: 3, User: 3, Doc: 3.
What Elinvar caught: User0 belongs to no groups (member = {}), yet can access
Doc0 because Group0 happens to grant it. Tightening the rule to some g: u.member
fixes the hole and the second check in corpus/holes/access.als reports holds.
2. Workflows and state machines over time
The problem: A sharing app has three actions:
share: a reader shares an item with someone who follows the owner.unfollow: a user stops following someone.note: someone an item was shared with leaves a note on it.
The promise: Nobody can leave a note on an item they can no longer open (opening requires being the owner, or having it shared and still following the owner).
Mark changing links with var, describe each action (including what stays the same), and
check over up to 6 steps (corpus/holes/sharing.als):
elinvar check corpus/holes/sharing.als NotesNeedAccess
Output:
check NotesNeedAccess: COUNTEREXAMPLE in 4 states, with Person: 2, Item: 1
start
Person = {Person0, Person1}
Item = {Item0}
owner = {Item0 -> Person1}
follows = {Person0 -> Person1}
step 1 shared gains {Item0 -> Person0}
step 2 follows loses {Person0 -> Person1}
step 3 notes gains {Item0 -> Person0}
step 4 nothing changes, back to state 3
broken at state 2, as step 3 begins
searched every case up to Person: 3, Item: 3, steps: 6.
What Elinvar caught: Share the item, unfollow the owner, then leave a note. Action 3
checked shared but forgot to re-check follows.
Inspecting a counterexample without solving again
Save the JSON result and ask questions about any state with elinvar eval:
elinvar check corpus/holes/sharing.als NotesNeedAccess --json > result.json
elinvar eval corpus/holes/sharing.als result.json 'Item.canOpen' --state 2
# {Person1}
elinvar eval corpus/holes/sharing.als result.json 'some notes' --state 3
# true
And render state 2 as a Mermaid diagram:
elinvar render result.json --mermaid --state 2
3. Checking a SQL database schema
The problem: Your PostgreSQL or SQLite schema has tables, foreign keys, NOT NULL,
UNIQUE, and CHECK constraints. You want to turn it straight into a checkable model
without rewriting it by hand.
Save schema.sql (or pipe via -):
CREATE TABLE users (
id INTEGER PRIMARY KEY,
email TEXT NOT NULL UNIQUE
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
user_id INTEGER NOT NULL REFERENCES users(id),
status TEXT NOT NULL CHECK (status IN ('open', 'closed'))
);
Import it:
elinvar import schema.sql > schema.als
Elinvar generates a ready-to-run Alloy 6 model:
-- Generated by Elinvar from SQL DDL
enum orders_status_enum { orders_status_open_sql, orders_status_closed }
sig users_email_val {}
sig users {
id: one Int,
email: one users_email_val
}
sig orders {
id: one Int,
user_id: one users,
status: one orders_status_enum
} {
status in (orders_status_open_sql + orders_status_closed)
}
fact users_unique_id {
all disj a, b: users | some a.id implies a.id != b.id
}
fact users_unique_email {
all disj a, b: users | some a.email implies a.email != b.email
}
fact orders_unique_id {
all disj a, b: orders | some a.id implies a.id != b.id
}
run Show {} for 3
Now append any business invariant you want to test against your schema (for example,
“can two open orders belong to the same user?” or “does every user have at least one
order?”) and run elinvar check schema.als.
4. Comparing two versions of a design
The problem: You refactored a rule or tightened a policy in v2.als. Did you actually
narrow what is allowed, accidentally allow new bad cases, or keep the two designs
equivalent?
v1.als:
sig Employee { manager: lone Employee }
fact { no e: Employee | e = e.manager }
v2.als:
sig Employee { manager: lone Employee }
fact { no e: Employee | e in e.^manager }
Compare them with elinvar diff:
elinvar diff v1.als v2.als
Output (exit code 1):
diff: tightened
allowed by old, forbidden by new:
check OnlyInOld: COUNTEREXAMPLE with Employee: 2
Employee = {Employee0, Employee1}
manager = {Employee0 -> Employee1, Employee1 -> Employee0}
searched every case up to Employee: 3.
elinvar diff classifies any pair of models sharing signatures and fields as one of:
equivalent(exit0): both allow the exact same cases up to the scope.tightened(exit1): the new model forbids at least one old case (and shows the smallest one) while allowing nothing new.loosened(exit1): the new model allows at least one new case (and shows it).diverged(exit1): each allows cases the other forbids (and shows both).vacuous(exit2,--ofonly): the two models together allow no case, so their functions cannot be compared.
5. Catching contradictory rules (vacuous)
The problem: A scheduler requires that:
- There is at least one task (
some Task). - Every task depends on at least one task (
all t: Task | some t.depends). - Dependencies have no cycles (
no t: Task | t in t.^depends).
Those three rules are impossible to satisfy on any finite set of tasks. A naive checker would find zero counterexamples to any assertion and happily report “passed”.
Run it with --explain:
elinvar check - --explain <<'EOF'
sig Task { depends: set Task }
fact NonEmpty { some Task }
fact EveryTaskDepends { all t: Task | some t.depends }
fact Acyclic { no t: Task | t in t.^depends }
assert OneDep { all t: Task | one t.depends }
check OneDep for 3
EOF
Output (exit code 2):
check OneDep: VACUOUS
the rules allow no case at all up to Task: 3, so nothing can break the promise.
conflicting rules: field depends, fact NonEmpty, fact EveryTaskDepends, fact Acyclic
Elinvar refuses to report holds, exits with status 2, and points straight at the
minimal set of conflicting rules.