Sets

Sets are one of the most fundamental data structures in TLA+. Unlike programming languages, TLA+ is built on set theory.

For finite set operations like Cardinality and IsFiniteSet, TLA+ provides the FiniteSets standard module.

Set Literals and Membership

SyntaxMeaningUnicode
{1, 2, 3}A set of numbers
{"a", "b", "c"}A set of strings
{}The empty set
x \in Sx is a member of Sx ∈ S
x \notin Sx is NOT a member of Sx ∉ S

Set Operators

OperatorMeaningUnicode
S \union TUnion - elements in S or TS ∪ T
S \intersect TIntersection - elements in bothS ∩ T
S \ TSet difference - elements in S but not T
SUBSET SPower set - all subsets of S
S \subseteq TS is a subset of TS ⊆ T

FiniteSets Module

To use these operators, add EXTENDS FiniteSets to your module.

OperatorMeaning
IsFiniteSet(S)TRUE if S is a finite set
Cardinality(S)Number of elements in a finite set S

Set Comprehension (Filtering)

{x \in S : P(x)}   \* elements of S satisfying predicate P

Example: {x \in 1..10 : x > 5} gives {6, 7, 8, 9, 10}

Set Mapping

{f(x) : x \in S}   \* apply f to each element of S

Example: {x * 2 : x \in 1..3} gives {2, 4, 6}

Ranges

1..5               \* the set {1, 2, 3, 4, 5}

Try It

The spec on the right demonstrates various set operations. Run TLC to verify the invariants.