(Last modified Thu Feb 14 16:20 2008)
The Alloy logic is a first-order logic in which the domain is the set of all relations, and terms include relational expressions such as joins.
Everything in Alloy is a relation!
As a result, the operators apply to relations, sets, and scalars, and there are very few cases that produce no result.
Page numbers refer to Daniel Jackson, Software Abstractions, MIT Press 2006.
How to update the book for Alloy 4
| univ | The universal set |
| none | The empty set |
| iden | The identity relation |
| Sym | Name | Result |
|---|---|---|
| + | Union | A set |
| & | Intersection | |
| - | Difference | |
| in | Subset | T or F |
| = | Equality |
| Sym | Name | Syntax |
|---|---|---|
| -> | (Arrow) product | R1 -> R2 |
| . | Join | R1 . R2 |
| [] | Join (a second notation for it) | R2 [R1] |
| ~ | Transpose | ~ R |
| ^ | Transitive closure | ^ R |
| * | Reflexive transitive closure | * R |
| <: | Domain restriction | Set <: R |
| :> | Range restriction | R :> Set |
| ++ | Override | R1 ++ R2 |
| Sym | Keyword | Name or result |
|---|---|---|
| ! | not | negation |
| && | and | conjunction |
| || | or | disjunction |
| => | implies | implication |
| <=> | iff | logical equivalence |
| else | A=>B else C ≡ (A&&B)||(!A&&C) |
| Quantification Q var:set | formula | Predicate on relations Q e | |
|---|---|---|
| all | universal | — |
| some | existential | size is 1 or greater |
| no | ¬∃ | size is 0 |
| lone | zero or one exists | size is 0 or 1 |
| one | exactly one exists | singleton |
| let x = e | A | A with every occurrence of x replaced by expression e |
(Parts of this subsection describe the Alloy language.)
Each set of atoms is defined by a signature, with keyword sig.
A signature can contain zero or more relation declarations, separated by commas. Each declaration names a (binary) relation between the set defined by the signature and a set or relation.
// Simple example
abstract sig Person { // Signature
father: lone Man, // A declaration
mother: lone Woman // Another declaration
}
sig Man extends Person {
wife: lone Woman
}
sig Woman extends Person {
husband: lone Man
}
| S in
T
U in T | subset | Every
S
is a
T,
and every U is a T | An S can also be a U |
| S extends
T
U extends T | extension | An S cannot also be a U |
There are two ways:
(The fact keyword may be omitted if the fact is only about the relations of a single signature, and it immediately follows that signature — then it is a signature fact, and is implicitly universally quantified over the signature's set, and may use this as if it were the variable of this implied quantification.)
A more extensive example of signatures, declarations, and constraints
| e is a expression producing a set (arity 1) | |
|---|---|
| x: set e | x a subset of e |
| x: lone e | x empty or a singleton subset of e |
| x: some e | x a nonempty subset of e |
| x: one e | x a singleton subset of e (i.e. a scalar) |
| x: e | x a singleton subset of e (equivalent to one) |
|
A and B
are expressions producing a relation
m and n are some, lone, one, or not present (which is equivalent to set) | |
|---|---|
| r: A m -> n B | m elements of A map to each element of B |
| each element of A maps to n elements of B | |
A fact contains a formula in the Alloy logic that is assumed to always be true. See the Alloy language for more details.
disj before a list of variables restricts their bindings to be disjoint.
The prefix operator # (cardinality) on a relation produces the relation's size. The result can be operated on with + - = < > =< >=. Positive integer literals can appear in cardinality expressions.
sum x: e | ie sums the value of ie for each x in set e.
The Alloy language uses the Alloy logic plus some other constructs to make models. In Alloy, a model is "a description of a software abstraction" 4.
(Recall that in FOL a model means something different.)
The Alloy language adds these constructs to the Alloy logic:
You define the scope
that the analyzer checks
by saying things like "run for 3" or
"run for 3 but 4 Dog".
The analyzer will then check only possible examples
that contain no more than that many
of atoms from each set.
If it finds an example, then the predicate is satisfiable.
If it finds no examples, the predicate may be either invalid (false for all possible examples); or it may be satisfiable but not within the scope you used.
You define the scope as for a run command.
If it finds a counterexample, then the predicate is unsatifiable.
If it finds no counterexamples, the predicate may be either valid (true for all possible examples); or it may be unsatifiable but not within the scope you used.
| sig A {fields} | Declares a set A of atoms |
| sig A extends B {fields} | Declares a subset A of set B,
disjoint from all other extends subsets of B |
| sig A in B {fields} | Declares a subset A of B |
| sig A in B + C {fields} | Declares a subset A of the union (+) of sets B and C |
| abstract sig A {fields} | Declares a set A that contains no atoms other than the ones in its subsets (if any) |
| one sig A {fields} | Declares a singleton set A |
| lone sig A {fields} | Declares a set A of 0 or 1 atom |
| some sig A {fields} | Declares a nonempty set A |
| sig A, B {fields} | Declares two sets A and
B of atoms Wherever A appeared above, a list of names can appear |
| f: e | Declares a relation f that's a subset of
A->e. e can be any expression that produces a set — union, intersection, ... , any combination. |
| f: lone e | Each A is related to no e or one e. |
| f: one e | Each A is related to exactly one e. |
| f: some e | Each A is related to at least one e. |
| f: g->h | Each A is related to a relation from g to h. |
| f: one g lone -> some h | The multiplicities have their
usual meanings. Here, each A is related to exactly one relation relating each g to 1 or more h's, and each h is related to 0 or 1 g. |
| fun Name [parameters] : type {e} | Defines a function, with the given name
and (possibly empty) parameters, and producing a relation (or set, or scalar) of the given type. The result is defined by the expression e, which may reference the parameters. |
| pred Name [parameters] {f} | Defines a predicate, with the given name
and (possibly empty) parameters. A predicate always produces true or false, so no type is needed. The result is defined by the formula f, which may reference the parameters. |
| fact {e} | The expression e
is a constraint that the analyzer will assume is always true. |
| fact Name {e} | You can name a fact if you wish; the analyzer will ignore the name. |
| assert Name {f} | Defines a assertion, with the given name.
Assertions take no parameters. An assertion always produces true or false, so no type is needed. The result is defined by the formula f. |