When to Use Alloy
Alloy excels at finding counterexamples to structural properties. Use it when you need to answer:- “Can this bad state exist?” - Alloy searches for violations
- “Is this constraint sufficient?” - Alloy finds gaps
- “What does this data model actually allow?” - Alloy generates instances
Signatures
Signatures define the types of atoms in your model. They’re like classes in OOP or tables in a database, but with relational semantics.Basic Signatures
Signature Modifiers
one, lone, and some modifiers constrain cardinality:
| Modifier | Meaning | Atoms |
|---|---|---|
| (none) | Any number | 0, 1, 2, … |
one | Exactly one | 1 |
lone | At most one | 0 or 1 |
some | At least one | 1, 2, 3, … |
abstract | None directly | 0 (only subtypes) |
Signature Extension
Signature Subsets
in keyword creates subsets that aren’t necessarily unique. An atom can be both an Employee and something else that extends Person.
Disjoint Subsets
Fields and Multiplicity
Fields define relations between signatures. Every field is a relation, even “attributes” are relations to value atoms.Field Multiplicity
| Keyword | Meaning | Count |
|---|---|---|
one | Exactly one | 1 |
lone | At most one | 0-1 |
set | Any number | 0+ |
some | At least one | 1+ |
one is assumed for simple relations. For relations with arrow (->), set is the default.
Binary Relations
Most fields are binary relations (from this signature to another):Ternary and Higher Relations
Relations can have higher arity:p.membership[org] returns the roles person p has in organization org.
Relation Constraints
Relational Operators
Alloy is fundamentally relational. Understanding these operators is essential.Join (.)
The dot operator navigates relations:r: A -> B and s: B -> C, then r.s: A -> C.
Union (+)
Combines sets or relations:Intersection (&)
Elements in both:Difference (-)
Elements in first but not second:Transpose (~)
Reverses a relation:r: A -> B, then ~r: B -> A.
Transitive Closure (^)
One or more applications of a relation:Reflexive Transitive Closure (*)
Zero or more applications (includes identity):^r requires at least one step; *r includes staying in place.
Product (->)
Creates pairs:Domain Restriction (<:)
Limits a relation to a domain subset:
r: A -> B and s: set A, then s <: r: A -> B contains only pairs where the first element is in s.
Range Restriction (:>)
Limits a relation to a range subset:
r: A -> B and s: set B, then r :> s: A -> B contains only pairs where the second element is in s.
Override (++)
Replaces mappings:++ updates entries. If key exists in oldState, the new value replaces it.
Cardinality (#)
Counts elements:Quantifiers
Quantifiers express properties over sets.Universal (all)
Existential (some)
None (no)
not some.
Unique (one)
At Most One (lone)
Disjoint Quantification
disj modifier ensures quantified variables are distinct.
Logical Operators
Combine quantified expressions:| Operator | Meaning |
|---|---|
and | Both true |
or | At least one true |
implies / => | If-then |
iff / <=> | If and only if |
not / ! | Negation |
Facts, Predicates, Functions, Assertions
These organize constraints and queries.Facts
Facts are constraints that always hold. Alloy only considers instances satisfying all facts.Predicates
Predicates are named, parameterized formulas. They can be true or false.run commands.
Functions
Functions return values (sets/relations). They’re expressions, not constraints.Assertions
Assertions are properties you claim should hold. Thecheck command searches for violations.
Comprehensions
Set comprehensions build sets from expressions.Basic Comprehension
Expression Comprehension
In Predicates and Functions
Let Expressions
let binds names to expressions for clarity and reuse.
Commands
Commands tell the Analyzer what to do.Run
Therun command searches for instances.
Check
Thecheck command searches for assertion violations.
Scope
Bounds limit the search space. By default, bounds mean “at most N atoms” (0 to N), not “exactly N.”exactly when you need a specific count. Without it, Alloy explores all sizes up to the bound, which is usually what you want for finding counterexamples.
Expect
Expect indicates anticipated results (for documentation/testing).Module System
Modules organize specifications into reusable units.Basic Modules
Opening Modules
Private Declarations
Parameterized Modules
Standard Library
Alloy includes utility modules:Temporal Operators (Alloy 6)
Alloy 6 introduced temporal logic for modeling state over time.Variable Fields
var keyword marks mutable relations.
Priming
The prime operator (') refers to the next state:
Temporal Quantifiers
Traces and Liveness
Temporal Example: Mutex
Built-in Relations and Constants
Universal Set
Empty Set
Identity Relation
Integer Operations
for N Int to set bitwidth (e.g., for 5 Int gives 5-bit integers: -16 to 15). Integer overflow wraps silently, so increase bitwidth if your model uses larger numbers.
Common Patterns
State Machine
Graph Reachability
Ownership / Containment
Access Control
Ordered Elements
Complete Example: Key-Value Store
A specification combining multiple concepts:Quick Reference
Signature Modifiers
| Modifier | Meaning |
|---|---|
one | Exactly one atom |
lone | Zero or one atom |
some | At least one atom |
abstract | No direct atoms |
extends | Disjoint subtype |
in | Subset (possibly overlapping) |
Field Multiplicity
| Keyword | Meaning |
|---|---|
one | Exactly one |
lone | Zero or one |
some | One or more |
set | Zero or more (default) |
Relational Operators
| Operator | Name | Purpose |
|---|---|---|
. | Join | Navigate relations |
+ | Union | Combine sets |
& | Intersection | Common elements |
- | Difference | Remove elements |
~ | Transpose | Reverse relation |
^ | Transitive closure | One+ steps |
* | Reflexive transitive closure | Zero+ steps |
-> | Product | Create tuples |
<: | Domain restriction | Filter by domain |
:> | Range restriction | Filter by range |
++ | Override | Replace mappings |
# | Cardinality | Count elements |
Logical Operators
| Operator | Meaning |
|---|---|
and | Conjunction |
or | Disjunction |
not / ! | Negation |
implies / => | Implication |
iff / <=> | Biconditional |
Quantifiers
| Quantifier | Meaning |
|---|---|
all | For every |
some | There exists |
no | For none |
one | Exactly one |
lone | At most one |
Temporal Operators (Alloy 6)
| Operator | Meaning |
|---|---|
' | Next state value |
always | In all future states |
eventually | In some future state |
after | In the next state |
until | P holds until Q |
releases | Q holds until P (or forever) |
historically | In all past states |
once | In some past state |
before | In the previous state |
since | P has held since Q |
Commands
| Command | Purpose |
|---|---|
run P | Find instance where P holds |
run {} for N | Find any instance with scope N |
check A | Find counterexample to assertion A |
check A for N | Check with scope N |
Where to Go Next
This guide covered Alloy syntax comprehensively. For practical application:- Alloy Basics — If you skipped it, the gentle introduction provides context
- MacAlloy — Native macOS tool for running specifications
- AD Security Model — Real-world Alloy for Active Directory security
- Software Abstractions — Daniel Jackson’s definitive book
- Alloy Documentation — Official reference