In set theory and its applications to logic, mathematics, and computer science, set-builder notation is a mathematical notation for describing a set by enumerating its elements, or stating the properties that its members must satisfy.[1]
Defining sets by properties is also known as set comprehension, set abstraction or as defining a set's intension.
A set can be described directly by enumerating all of its elements between curly brackets, as in the following two examples:
\{7,3,15,31\}
\{a,c,b\}=\{a,b,c\}
This is sometimes called the "roster method" for specifying a set.[2]
When it is desired to denote a set that contains elements from a regular sequence, an ellipsis notation may be employed, as shown in the next examples:
\{1,2,3,\ldots,100\}
\{1,2,3,\ldots\}
\{\ldots,-2,-1,0,1,2,\ldots\}=\{0,1,-1,2,-2,\ldots\}
There is no order among the elements of a set (this explains and validates the equality of the last example), but with the ellipses notation, we use an ordered sequence before (or after) the ellipsis as a convenient notational vehicle for explaining which elements are in a set. The first few elements of the sequence are shown, then the ellipses indicate that the simplest interpretation should be applied for continuing the sequence. Should no terminating value appear to the right of the ellipses, then the sequence is considered to be unbounded.
In general,
\{1,...,n\}
i
1\leqi\leqn
\{1,...,n\}
[n]
n=0
[0]=\{1,...,0\}
\emptyset
\{a1,...,an\}
ai
1\leqi\leqn
In each preceding example, each set is described by enumerating its elements. Not all sets can be described in this way, or if they can, their enumeration may be too long or too complicated to be useful. Therefore, many sets are defined by a property that characterizes their elements. This characterization may be done informally using general prose, as in the following example.
\{
\}
However, the prose approach may lack accuracy or be ambiguous. Thus, set-builder notation is often used with a predicate characterizing the elements of the set being defined, as described in the following section.
Set-builder notation can be used to describe a set that is defined by a predicate, that is, a logical formula that evaluates to true for an element of the set, and false otherwise.[3] In this form, set-builder notation has three parts: a variable, a colon or vertical bar separator, and a predicate. Thus there is a variable on the left of the separator, and a rule on the right of it. These three parts are contained in curly brackets:
\{x\mid\Phi(x)\}
\{x:\Phi(x)\}.
\{x\mid\Phi(x)\}
A domain can appear on the left of the vertical bar:[5]
\{x\inE\mid\Phi(x)\},
\{x\midx\inEand\Phi(x)\} or \{x\midx\inE\land\Phi(x)\}.
\land
\Phi(x)
\Phi1(x)\land\Phi2(x)
\{x\inE\mid\Phi(x)\}
\{x\inE\mid\Phi1(x),\Phi2(x)\}
\land
In general, it is not a good idea to consider sets without defining a domain of discourse, as this would represent the subset of all possible things that may exist for which the predicate is true. This can easily lead to contradictions and paradoxes. For example, Russell's paradox shows that the expression
\{x~|~x\not\inx\},
In cases where the set is clear from context, it may be not explicitly specified. It is common in the literature for an author to state the domain ahead of time, and then not specify it in the set-builder notation. For example, an author may say something such as, "Unless otherwise stated, variables are to be taken to be natural numbers," though in less formal contexts where the domain can be assumed, a written mention is often unnecessary.
The following examples illustrate particular sets defined by set-builder notation via predicates. In each case, the domain is specified on the left side of the vertical bar, while the rule is specified on the right side.
\{x\inR\midx>0\}
(0,infty)
\{x\inR\mid|x|=1\}
\{-1,1\}
\{x\inR\midx2=1\}
Gm=\{x\inZ\midx\gem\}=\{m,m+1,m+2,\ldots\}
G3=\{x\inZ\midx\ge3\}=\{3,4,5,\ldots\}
G-2=\{-2,-1,0,\ldots\}
\{(x,y)\inR x R\mid0<y<f(x)\}
R x R
\{n\inN\mid(\existsk)[k\inN\landn=2k]\}
\land
(\existsx)P(x)
\{n\mid(\existsk\inN)[n=2k]\}
\{a\inR\mid(\existsp\inZ)(\existsq\inZ)[q\not=0\landaq=p]\}
An extension of set-builder notation replaces the single variable with an expression. So instead of
\{x\mid\Phi(x)\}
\{f(x)\mid\Phi(x)\},
\{f(x)\mid\Phi(x)\}=\{y\mid\existsx(y=f(x)\wedge\Phi(x))\}
For example:
\{2n\midn\inN\}
N
\{p/q\midp,q\inZ,q\not=0\}
Z
Q,
\{2t+1\midt\inZ\}
\{(t,2t+1)\midt\inZ\}
When inverse functions can be explicitly stated, the expression on the left can be eliminated through simple substitution. Consider the example set
\{2t+1\midt\inZ\}
u=2t+1
t=(u-1)/2
\{2t+1\midt\inZ\}=\{u\mid(u-1)/2\inZ\}.
Two sets are equal if and only if they have the same elements. Sets defined by set builder notation are equal if and only if their set builder rules, including the domain specifiers, are equivalent. That is
\{x\inA\midP(x)\}=\{x\inB\midQ(x)\}
(\forallt)[(t\inA\landP(t))\Leftrightarrow(t\inB\landQ(t))]
For example,
\{x\inR\midx2=1\}=\{x\inQ\mid|x|=1\}
(x\inR\landx2=1)\Leftrightarrow(x\inQ\land|x|=1).
x2=1
|x|=1
\{-1,1\}
In many formal set theories, such as Zermelo–Fraenkel set theory, set builder notation is not part of the formal syntax of the theory. Instead, there is a set existence axiom scheme, which states that if is a set and is a formula in the language of set theory, then there is a set whose members are exactly the elements of that satisfy :
(\forallE)(\existsY)(\forallx)[x\inY\Leftrightarrowx\inE\land\Phi(x)].
\{x\inE\mid\Phi(x)\}
See main article: article and List comprehension.
A similar notation available in a number of programming languages (notably Python and Haskell) is the list comprehension, which combines map and filter operations over one or more lists.
In Python, the set-builder's braces are replaced with square brackets, parentheses, or curly braces, giving list, generator, and set objects, respectively. Python uses an English-based syntax. Haskell replaces the set-builder's braces with square brackets and uses symbols, including the standard set-builder vertical bar.
The same can be achieved in Scala using Sequence Comprehensions, where the "for" keyword returns a list of the yielded variables using the "yield" keyword.[7]
Consider these set-builder notation examples in some programming languages:
Example 1 | Example 2 | ||
---|---|---|---|
Set-builder | \{l | l\inL\} | \{(k,x) | k\inK\wedgex\inX\wedgeP(x)\} | |
Python | |||
Haskell | |||
Scala | |||
C# | |||
SQL | |||
Prolog | |||
Erlang | |||
Julia |
The set builder notation and list comprehension notation are both instances of a more general notation known as monad comprehensions, which permits map/filter-like operations over any monad with a zero element.