\ch{Sets}\label{ch:sets}

In math, it's often useful to consider \xti{collections} of objects. There are
basically two types of collections: \term{sets} and \term{vectors}. Sets are
unordered, and multiplicity doesn't matter. Vectors are ordered, and
multiplicity does matter.

For instance, $\mset{3, 4}$ and $\mset{4,3}$ are the same \xti{set}, but
$\mvec{3,4}$ and $\mvec{4,3}$ are different \xti{vectors}.

Likewise, $\mset{20,38}$ and $\mset{38,20,38,20,20,20,20,20}$ are the same
\xti{set}. You guessed it, $\mvec{20,38}$ and $\mvec{38,20,38,20,20,20,20,20}$
are different \xti{vectors}. Sets are --- at least ostensibly --- much more
important. More importantly, they are much easier to understand.

Sets were first studied to extent by Georg Cantor, a German mathematician, in
the second half of the nineteenth century. Back in his own day, the results
Cantor found by studying sets were considered so thoroughly bizarre that many of
his colleagues simply refused to believe that Cantor could be right.  In the
end, Cantor turned out to be right all along. His ideas can be found in any
introductory text on mathematics---including this one.

You probably figured it out from above: the notation is $\mset{\text{Braces}}$
for sets, and $\mvec{\text{Parentheses}}$ for vectors.

If you can't remember whether to use braces \{the curly things\}, or parentheses
(the round things), remember: \xti{a \xtb{brace} is used to \xtb{set} a broken
  bone.} I don't have a horrible pun having to do with parentheses and vectors,
and for that, I apologize.

\s{Elements}

Let's invent a set.

\[ Q = \mset{7, 7, 9, 5, 10, 1, 6, 6, 2, 10} \]

There we go. Remember, order and multiplicity don't matter. But, for the sake of
clarity, let's put the elements in order, and deduplicate them.

\[ Q = \mset{1, 2, 5, 6, 7, 9, 10} \]

Yay! You may have noticed that I slipped in the word \term{element} into the
previous sentence. Objects in the set are called \term{elements}. Yay, we
figured out what that word means!

It's too strenuous on our weak mathematical hands to write ``$10$ is an element
of $Q$'', so instead we have the symbol $\in$. $\in$ is a very terrible attempt
at drawing an E. If you can't remember what $\in$ is, think
``\raisebox{1pt}{$\in$}\hspace{0pt}lement of''.\footnote{You better think this,
  because it took me 30 minutes to get the alignment right. So, you know,
  remember $\in$ this way, or else\dots\label{30minalign}}

So, I'm going to ask you a question:

\[ 11 \Qin Q \]

(See, I used that thing from earlier with the question mark. I told you it would
help.) Well, the answer is no, $11$ is not an element of $Q$. As always,
mathematicians are too weak to write ``$11$ is not an element of $Q$'' every
time they want to say it, so instead they write

\[ 11 \notin Q \]

By contrast,

\[ 6 \in Q \]

What if we want to say ``both $6$ and $2$ are elements of $Q$''? Well, again, we
could write it out like:

\[ \parens{6 \in Q} \land \parens{2 \in Q} \]

But that's too cumbersome, so instead we'll write 

\[ 2, 6 \in Q \]

\pg{But won't that get confusing?}

Only if we put parentheses or braces around $2$ or $6$.

\[ \mset{2, 6} \in Q \]

That's confusing, don't do that (yet).

There's one more thing I need to go over, which is the null set - it's the
simplest set, as it contains no elements. ``Null set'' takes too long to write,
so we use $\emptyset$ instead.

\s{Subsets and Supersets}

Remember when I said $\mset{2, 6} \in Q$, and we were really confused? In case
you don't remember, $Q = \mset{1, 2, 5, 6, 7, 9, 10}$. $\mset{2,6}$ is obviously
not an element of $Q$. However, $\mset{2, 6}$ is \xti{in} $Q$ but it's not an
element. It's weird. How do we express this notion?

The answer is with \term{subsets}. ``sub'' means ``smaller'', so a ``subset''
would be a ``smaller set''. $A$ is a subset of $B$ is all of the elements in $A$
are also in $B$. The notation is $A \subeq B$. Some people will read that as
``$A$ is contained in $B$''.

Referring to the previous example,

\[ \mset{2,6} \subeq Q \]

\pg{Wait, what is with the little line under the round half circley thing?}

So, actually, there are two types of subsets - \term{proper} and
\term{improper}. $A \subeq B$ is for improper subsets. $A \subof B$ is for
proper subsets. What's the difference, then?

$A \subeq B$ allows for the possibility that $A = B$. $A \subof B$ means that
$B$ is \xti{strictly larger} than $A$; there are some elements in $B$ that are
not in $A$.

\label{exists}
I've already defined $\forall$ in \cref{forall}. I'm now going to add another
symbol, $\exists$, which means ``exists''. I mention $\forall$, because
$\exists$ is used in the same context.

Anyway, back to business. I use $\subeq$ for improper subsets, and $\subof$ for
proper subsets. However, some people will use $\subof$ for improper subsets, and
something like $\subsetneq$ or $\subsetneqq$ for proper subsets. You have to
look out.

Here's something cool: $\nil$ has no elements, so it's a subset of every set.

\[ \nil \subeq A \sfall A \]

I'm sure you can figure this out, two sets are equal iff they have the same
elements. $A \subeq B$ means that $B$ has all of the elements that $A$ has.
$A \supeq B$ would mean that $A$ has all of the elements of $B$. So if both of
those are true, then $A = B$. That is:

\[ A = B \iff A \subeq B \land A \supeq B \]

So, what did we learn?

\begin{enumerate}
\item There are unordered collections with no multiplicity, called \term{sets}.
\item There are ordered collections with multiplicity, called \term{vectors}.
\item Given an object $o$ and a set $A$, you can ask $o \Qin A$.
\item You can pull smaller sets out of a set (I'll show you the mechanics
  below). The way to express that a set is ``embedded'' in another set, without
  being an element is with \term{subsets}. $A \Qsubeq B$ would be asking ``are
  all of the elements in $A$ also in $B$?''. (The converse doesn't necessarily
  have to be true).
\item There's a pretty easy set which has no elements, called $\nil$. An
  interesting property of $\nil$ is that it is a subset of all other sets.
\item Two sets are equal iff they have the same elements.
\end{enumerate}

\s{Combining sets together}

Next, we're going to talk about ways to combine two sets together. There are any
number of ways to do this. However, the two most common ways are through
\term{unions} and \term{intersections}.

The union symbol is pretty easy to memorize --- it looks like a giant U:
$\cup$. Think ``$\cup$nion''.\footnote{You don't have to remember it this way,
  because $\cup$ is already aligned reasonably well with the letters. Thus I
  didn't have to spend 30 minutes getting the alignment correct. (See
  \cref{30minalign}.)}.

If $A$ and $B$ are sets, then $A \cup B$ is the set of elements that are in
either $A$ or in $B$. That is:

\[ A \cup B = \mset{x \in \amb \semic \parens{x \in A} \lor \parens{x \in B}} \]

You might also remember this by the fact that the union symbol $\cup$ looks
vaguely like the or symbol $\lor$.

\pg{What the hell is that notation?}

\label{comprehensions}
That's called a \term{class comprehension}. It's a hacky way to describe a
set. Technically, it describes a \term{class}, which is different than a set.
That said, at least until chapter 5, they only describe sets!

Remember earlier when I would show you the mechanics for defining arbitrary
subsets of a set? Well, this is a common way to do it.

You should look at the expression in two pieces: before the semicolon and after
the semicolon. The term before the semicolon explains what each element looks
like, and defines it to be a member of an ambient set. In this case --- and in
most cases --- the element will just be $x$, or $a$, or something of the like.

\[ A \cup B = \mset{x \in \amb \semic \parens{x \in A} \lor \parens{x \in B}} \]

Okay, cool. The right side of the semicolon lists conditions that must be true
about the thing on the left. In this case, $x$ must be in $A$, logical-or it
must be in $B$.

The $\amb$ is shorthand for ``ambient set''. It's a set such that
$A, B \subeq \amb$. For reasons I can't quite explain yet, you need to specify
that the element variable ($x$ in this case) is an element of another set. The
consequence of this is, a set comprehension can only be used to make a
\xti{subset} of another set. $\amb$ is the shorthand for ``there's another set
out there, but I don't care what it is (and you shouldn't either)''.

Can you think of another consequence?

Well, since we are defining the union using a notation which can only be used to
describe subsets, we can only take the union of two sets if they are both
subsets of some larger set! That's annoying. Oh well.

\ss{Back to business}

Let's look at that definition of the union again:

\[ A \cup B = \mset{x \in \amb \semic \parens{x \in A} \lor \parens{x \in B}} \]

You know already that $\lor$ is commutative (the order doesn't matter), so you
can write this

\[ A \cup B = \mset{x \in \amb \semic \parens{x \in B}} \lor \parens{x \in A} \]

You've probably figured out, any property of $\lor$ is also true of $\cup$

\begin{description}
\item[Reflexive property] $a \union a \equiv a$
\item[Associative property] $a \union \parens{b \union c} \equiv \parens{a \union b} \union c$
\item[Commutative property] $a \union b \equiv b \union a$
\end{description}

\ss{The intersection}

The intersection is, you guessed it - what happens when we use $\land$ in the
above definition instead of $\lor$. Can you guess what the symbol for
``intersection'' is? If you guessed $\cap$, then you are right!

\[ A \cap B = \mset{x \in \amb \semic \parens{x \in A} \land \parens{x \in B}} \]

Since $\land$ is also reflexive, associative, and commutative, the same
properties exist for $\cap$.

\begin{description}
\item[Reflexive property] $a \intersect a \equiv a$
\item[Associative property] $a \intersect \parens{b \intersect c} \equiv \parens{a \intersect b} \intersect c$
\item[Commutative property] $a \intersect b \equiv b \intersect a$
\end{description}

You can actually reduce the definition of $\cap$ to 

\[ A \cap B = \mset{x \in A \st \parens{x \in B}} \]

(I do this in the proofs a lot)

The standard example of unions and intersections is to use ``Venn diagrams''. I
drew some myself in \cref{fig:venns}.\footnote{It's a tradition from Learn You a
  Haskell that each book include poorly-drawn explanatory graphics by the
  author.}

\begin{figure}[ht]
  \centering
  \includegraphics[width=0.8\textwidth]{venn-union.png}
  \includegraphics[width=0.8\textwidth]{venn-intersect.png}
  \caption{The Venn diagrams. That symbol in the left circle is supposed to be a
    Q. My handwriting sucks, deal with it.}
  \label{fig:venns}
\end{figure}

\ss{More on the comprehension}

I use the comprehension quite a lot (other books do as well), so I should at
least take a subsubsection to explain it.

I'm going to list some \xti{axioms} about comprehensions, which you would do
well to remember:

Let $p$ be a \text{unary predicate}. Basically, it takes the $x$, and determines
whether or not that $x$ is to be included in the comprehended set.

\begin{enumerate}
\item
  $\mset{x \in A \semic x \in \mset{y \in B; \evalat{p}{y}}} \equiv \mset{x \in
    A \semic x \in B \land \evalat{p}{x}}$
\item
  $\mset{x \in A \semic x \notin \mset{y \in B; \evalat{p}{y}}} \equiv \mset{x \in
    A \semic x \notin B \lor \lnot\evalat{p}{x}}$
\end{enumerate}

\ss{Back to unions and intersects}

The final piece of the puzzle is this:

\begin{description}
\item \mtb{A \cap \parens{B \cup C} \equiv \parens{A \cap B} \cup \parens{A \cap C}} 

  \begin{proof}
    \begin{rclmath}
      A \cap \parens{B \cup C}
      & \ce & \mset{
        x \in A \st
        x \in B \lor
        x \in C
      } \\
      \parens{A \cap B} \cup \parens{A \cap C}
      & \ce & \mset{
        x \in \amb \st
        \parens{
          x \in A \land
          x \in B
        }\lor
        \parens{
          x \in A \land
          x \in C
        }
      } \\
      & \ce & \mset{
        x \in \amb \st
        x \in A \land
        \parens{
          x \in B \lor
          x \in C
        }
      } \\
      & \ce & \mset{
        x \in A \st
        x \in B \lor
        x \in C
      } \\
    \end{rclmath}
  \end{proof}
\end{description}

You're probably wondering what that white box is. It's mathematical shorthand
for ``I'm done proving stuff''.

I can't faithfully discuss sets any more without talking about functions. So,
you get a lucky break! No exercises (because the fun exercises require that you
know about functions.)

I would tell you to look in \cref{d-sets} if you don't remember stuff. However,
\cref{d-sets} assumes you have read both \cref{ch:functions} and
\cref{ch:more-sets}. So, in conclusion, don't look at \cref{d-sets} quite yet.
