\ch{Booleans, simple logic, and simple operators}\label{booleans}
\label{ch:booleans}
\label{ch:bools}

Before we get into interesting content, you have to understand some stuff. This
stuff is pretty easy. This will likely be the shortest and easiest chapter in
the book.

You might think math is about dealing with numbers and pumping out
formulas. Well, that's not what math is about. As said in \cref{intro-idris},
it's about using math as a language to express your thoughts. Most people don't
think about numbers all day; thus, we deal with things in math that aren't
numbers.

In this next section, we're going to outline some basic rules for reasoning
about things. You need to know these rules to do really cool stuff. Although, as
you will (hopefully) see, these rules can be fun to toy around with on their
own.

It's okay if you don't remember all of these rules. You can always find a list
in \cref{d-booleans}.

\s{Implications}

The first thing you need to understand is the notion that ``if x is true, then y
is also true. But, if y is true, it's not necessarily true that x is false.'' As
always, mathematicians are too lazy to write this stuff out by hand, so they
have notation for it.

\begin{enumerate}
\item $a \implies b$ means that ``$a$ implies $b$''. It doesn't necessarily mean
  that $b$ implies $a$. It means that if $a$ is true, then $b$ is also true.

  If someone is decapitated, then they will die. So,

  \[ \text{Decapitated} \implies \text{Dead} \]

  However, if someone is dead, it doesn't necessarily mean that they were
  decapitated. They could have been shot, or stabbed, or had a heart
  attack. There are endless possibilities.

\item $a \impliedby b$ is the same as writing $b \implies a$. It's sometimes
  convenient to use $a \impliedby b$ instead. $a \impliedby b$ should be read
  ``$a$ is implied by $b$''.

\item When I strike through some mathematical operator, like this:
  $\notimplies$, it means that you can semantically but ``not'' in front of
  whatever the operator says. So, $\notimplies$ means ``not implies''. That
  doesn't make much grammatical sense in English, so ``does not imply'' might be
  better. Nonetheless, you get the point.

  Moving on from the example above:

  \[ \text{Decapitated} \implies \text{Dead} \]

  If someone is decapitated, then they're also dead (at least within a few
  seconds). However, if someone is dead, it's not necessarily true that they
  were decapitated.

  \[ \text{Decapitated} \notimpliedby \text{Dead} \]

\item If something is not true, then I'll put a $\lnot$ in front of it. So, if I
  want to say that $a$ is false, then I'll write $\lnot a$.

\item If I want to pose a question, I could just ask the question. For instance, ``Is
  $\lnot\text{Decapitated}\impliedby\lnot\text{Dead}$ true?''.

  However, that quickly becomes difficult, usually when there are multiple
  assertions in a mathematical expression, and you don't know which one I'm
  asking about. Moreover, since I use the same font for text and math, if I have
  both, it might be hard to tell which is math and which is text. So, to help
  with ambiguity, I'll put a ? over the operator I'm asking about:

  \[ \lnot \text{Decapitated} \Qimpliedby \lnot\text{Dead} \]

  See, that's much easier.

\item Now, on to that question - Is ``not decapitated'' implied by ``not
  dead''. Well, let's think about it. If someone is not dead, then they couldn't
  have been decapitated, because if they were decapitated, then they would be
  dead. Therefore, if someone is not dead, then they weren't decapitated.

  That word jumble was probably confusing. Mathematicians don't like to be
  confused. I'll make it symbolic for you.

  \begin{alignmath}{c}
  \text{Decapitated} \implies \text{Dead} \\
  \Downarrow \\
  \lnot \text{Decapitated} \impliedby \lnot\text{Dead} \\
  \end{alignmath}
  
  Okay, I just used a vertical arrow. I'm sure you can figure out what it means.

\item So, hopefully you agree that
  \begin{alignmath}{c}
  \text{Decapitated} \implies \text{Dead} \\
  \Downarrow \\
  \lnot \text{Decapitated} \impliedby \lnot\text{Dead} \\
  \end{alignmath}

  However,

  \begin{alignmath}{c}
  \text{Decapitated} \implies \text{Dead} \\
  \stackrel{?}{\Uparrow} \\
  \lnot \text{Decapitated} \impliedby \lnot\text{Dead} \\
  \end{alignmath}

  Hm, the question mark doesn't work so well there. Oh well! Anyway, the answer
  is actually yes. We can figure this out by learning a rule about $\lnot$. Namely, that

  \[ \lnot\lnot a = a \]

  In this case, we know

  \[
  \lnot \text{Decapitated} \impliedby \lnot\text{Dead}
  \]

  So, if we just ``not'' both sides, and flip $\implies$ to $\impliedby$,

  \begin{alignedmath}
  \lnot\lnot \text{Decapitated} \implies \lnot\lnot\text{Dead} \\
  \text{Decapitated} \implies \text{Dead}
  \end{alignedmath}

  This is basically just the rule mentioned in \#3. Yay, we learned something!
\end{enumerate}

\begin{ExcList}
  \Exercise{\nm{A \notimplies B}

    \begin{displaymath}
      A \Qimplies \lnot B
    \end{displaymath}
  }

  \Answer{No. $A \notimplies B$ means that $A$ doesn't imply $B$. It doesn't
    necessarily mean that $\lnot B$ is false --- although that could very well
    be the case.}
\end{ExcList}

\ss{And and or}

So, sometimes we need to combine two pieces of logic together. There are two
ways we can do this - logical-or and logical-and.

I put logical- in front of them, because the mathematical meaning is slightly
different than the colloquial meaning.

Mathematicians are lazy, so we don't like to write ``logical-and'' whenever we
want to say it, so instead we use the symbol $\land$.

$A \land B$ is true if (and only if) both $A$ and $B$ are true. If one of them
is false, then the entire thing is false.

On the other side, we have logical-or. The symbol for logical-or is $\lor$.
$A \lor B$ is true if either $A$ or $B$ is true, or if both of them are
true. You could think of logical-or as being equivalent to the colloquial
``and/or''.

\begin{center}
  \begin{tabu}{|c|c||c||c|} \hline
    $\mathbf{A}$ & $\mathbf{B}$ & $\mathbf{A \land B}$ & $\mathbf{A \lor B}$ \\ \hline
    $\true$ & $\true$ & $\true$ & $\true$ \\ \hline
    $\true$ & $\false$ & $\false$ & $\true$ \\ \hline
    $\false$ & $\true$ & $\false$ & $\true$ \\ \hline
    $\false$ & $\false$ & $\false$ & $\false$ \\ \hline
  \end{tabu}
\end{center}

This is pretty simple. If you're having trouble remembering which symbol is
logical-and and which one is logical-or, remember that the logical-and symbol
--- $\land$ --- looks vaguely like an A.

I'm going to introduce some new notation: the $\iff$ symbol.

\[ \parens{A \iff B} = \parens{A \implies B} \land \parens{A \impliedby B} \]

\nm\iff\ should be read as ``if (and only if)''. Sometimes I'll write the word
iff --- with two 'f's --- that's the same as ``if (and only if)''.

In order to do the exercises (yes, there are exercises), you need to know these
properties of \nm\implies .

\begin{enumerate}
\item For all $a$, $a \implies a$.
\item For all $a$ and $b$, and $c$,
  $\parens{\parens{a \implies b} \land \parens{b \implies c}} \implies \parens{a
    \implies c}$.
  For this reason, we can write $a \implies b \implies c$ without any ambiguity.
\end{enumerate}

\label{forall}
I'm too lazy to type ``For all'' each time, so I'm going to use the $\forall$
symbol; it's a common symbol in math that means ``for all''. $\forall$ looks
like an upside-down A, so it should be easy to remember.

Here are some properties you need to remember:

\begin{description}
\item[Cancellative property] $\lnot\lnot a = a \sfall a$

  You know what, that's too hard. Instead from now on, instead of saying
  $a = b \sfall a,b$, I'm just going to write $a \equiv b$. Good?
\item[Reflexive property] $a \land a \equiv a$
\item[Associative property]
  $a \land \parens{b \land c} \equiv \parens{a \land b} \land c$
\item[Commutative property] $a \land b \equiv b \land a$
\item[Distributive property]
  $a \land \parens{b \lor c} \equiv \parens{a \land b} \lor \parens{b \land c}$
\item[De Morgan's first law]
  $\lnot\parens{a \land b} \equiv \lnot a \lor \lnot b$
\item[Reflexive property] $a \lor a \equiv a$
\item[Associative property]
  $a \lor \parens{b \lor c} \equiv \parens{a \lor b} \lor c$
\item[Commutative property] $a \lor b \equiv b \lor a$
\end{description}

These values, true and false, are called \term{Booleans}. They are named after a
mathematician named George Boole who studied them to
extent.\cite{w-boolean-algebra}

\begin{ExcList}
  \Exercise[label={demorgan2}]{
    $\lnot\parens{A \lor B} \Qequiv \lnot A \land \lnot B$

    I'll spoil it for you. This is called \term{De Morgan's second law}, and
    it's true. You can prove it using the cancellative property and the first
    law.  }
  \Answer{
      Start with the first law

      \[ \lnot\parens{a \land b} \equiv \lnot a \lor \lnot b \]

      Let $p = \lnot a$, $q = \lnot b$. One of the rules of algebra is, if
      $x = y$, then you can substitute one in for the other.

      \[ p \lor q \equiv \lnot\parens{\lnot p \land \lnot q} \]

      Another rule is, if you have an equation, and you do something to one side
      of the =, you have to do the same thing to the other side. Here, we're
      going to apply $\lnot$ to both sides of $\equiv$

      \[ \lnot\parens{p \lor q} \equiv {\lnot p \land \lnot q} \]
  }

  \Exercise{
    Here's another one for you.

    \begin{displaymath}
       a \lor \parens{b \land c} \Qequiv \parens{a \lor b} \land \parens{b \lor c}
    \end{displaymath}

    You need the distributive property, as well as the proof from
    \cref{demorgan2}.

    While we are at it, these proofs can be found in \cref{d-booleans}, as well
    as the solutions to these problems.  }

  \Answer{
    Start with the first property

    \begin{displaymath}
      a \land \parens{b \lor c} \equiv \parens{a \land b} \lor \parens{b \land c}
    \end{displaymath}

    Apply $\lnot$ to both sides

    \begin{displaymath}
      \lnot\parens{a \land \parens{b \lor c}} \equiv \lnot\parens{\parens{a \land b} \lor \parens{b \land c}}
    \end{displaymath}

    Apply DeMorgan's laws

    \begin{displaymath}
      \lnot a \lor \lnot\parens{b \lor c} \equiv \lnot\parens{a \land b} \land \lnot\parens{b \land c}
    \end{displaymath}

    Do it again (inside the parentheses).

    \begin{displaymath}
      \lnot a \lor \parens{\lnot b \land \lnot c} \equiv \parens{\lnot a \lor \lnot b} \land \parens{\lnot b \lor \lnot c}
    \end{displaymath}

    Let $p,q,r = \lnot a, \lnot b, \lnot c$, respectively.

    \begin{displaymath}
      p \lor \parens{q \land r} \equiv \parens{p \lor q} \land \parens{q \lor r}
    \end{displaymath}
  }

  \Exercise{$\lnot A \implies B \implies \lnot C$

    \begin{displaymath}
      C \Qimplies A
    \end{displaymath}
  }

  \Answer{Yes.

    \begin{proof}
        By transition,

      \begin{alignmath}{ccccc}
        \lnot A & \implies & B & \implies & \lnot C \\
        \lnot A &          &   & \implies & \lnot C \\
      \end{alignmath}

      By the reversal property:
      \begin{displaymath}
        A \impliedby C
      \end{displaymath}
    \end{proof}
  }

  \Exercise{$A \notimplies B \implies \lnot C$

    \begin{displaymath}
      A \Qimplies C
    \end{displaymath}
  }

  \Answer{Not by necessity.
    \begin{proof}
      By transition:
      \begin{alignmath}{ccccc}
        A & \notimplies & B & \implies & \lnot C \\
        A & \notimplies &   &          & \lnot C \\
      \end{alignmath}

      This does not imply

      \begin{displaymath}
        A \implies C
      \end{displaymath}
      
      To put it another way
      \begin{rclmath}
        A & \notimplies & \lnot C \\
          & \centernot\Downarrow &  \\
        A & \implies & C
      \end{rclmath}
    \end{proof}
  }

  \Exercise{$A \implies B \impliedby C$

    \begin{displaymath}
      A \Qiff C
    \end{displaymath}
  }

  \Answer{No, there's nothing to indicate that $A$ and $C$ have any relation
    whatsoever.}

  \Exercise{$A \land \lnot\parens{B \land \parens{C \lor D}} \Qequiv A
    \land \parens{B \lor C} \land \parens{B \lor D}$ }

  \Answer{No
    \begin{proof}
      Let's assume that it's true.
      \begin{rclmath}
        A \land \lnot\brackets{B \land \parens{C \lor D}}
          & \equiv & A \land \parens{B \lor C} \land \parens{B \lor D} \\
      \end{rclmath}
      Let $B, C, D$ all be true. Then:

      \begin{rclmath}
        A \land \lnot\brackets{\true \land \parens{\true \lor \true}}
          & \equiv & A \land \parens{\true \lor \true} \land \parens{\true \lor \true} \\
        A \land \lnot\brackets{\true \land \parens{\true \lor \true}}
          & \equiv & A \land \true \land \true \\
        A \land \lnot\brackets{\true \land \parens{\true \lor \true}}
          & \equiv & A \land \true \\
        A \land \lnot\brackets{\true \land \true}
          & \equiv & A \land \true \\
        A \land \lnot\true
          & \equiv & A \land \true \\
        A \land \lnot\true
          & \equiv & A \land \true \\
        \false
          & \equiv & \true \\
      \end{rclmath}

      Which is obviously false.
    \end{proof}
  }
\end{ExcList}
