Week 1 notes

Comp 163-002, Spring 2022, MW 4:15-5:30, Mondays in Cuneo 117 (starting January 31) and Wednesdays online.

The primary goal of this course is to become familiar with some of the basic mathematical ideas used in programming.


        Levin, 0.1 and 0.2

Basic logic combinations:

A and B
A or B
A=> B

From Levin, page 4:

While walking through a fictional forest, you encounter three trolls guarding a bridge. Each is either a knight, who always tells the truth, or a knave, who always lies. The trolls will not let you pass until you correctly identify each as either a knight or a knave. Each troll makes a single statement:
Troll 1: If I am a knave, then there are exactly two knights here.
Troll 2: Troll 1 is lying.
Troll 3: Either we are all knaves or at least one of us is a knight.
Which troll is which?

How do you go about analyzing this?

First, Troll 3 is telling the truth (why?) and so is a knight.

Second, we will analyze the consequences if Troll 1 is a knave. Troll 1's statement has the form A => B, where A is "Troll 1 is a knave" and B is "There are exactly two knights here". If Troll 1 is a knave, his statement is false; what does it mean for A=>B to be false? It can only happen if A is true and B is false, that is, A and ¬B. If Troll 1 is a knave (the A part), then Troll 1's statement is false, so the ¬B part must be true (because A and ¬B is true), and so B must be false; that is "there are exactly two knights" must be false.

We already know there is at least one knight. If Troll 1 is a knave (as we have been supposing), then there can be only one knight (Troll 3), and so Troll 2 is a knave. But Troll 2 told the truth if Troll 1 is a knave.

This is a contradiction. So Troll 1 must be a knight! Which means Troll 2 is a knave.

Figuring out that we should pursue the hypothesis "Troll 1 is a knave", to arrive at a contradiction, is not at all obvious. But it works!


Levin page 2:

3. After excavating for weeks, you finally arrive at the burial chamber. The room is empty except for two large chests. On each is carved a message (strangely in English):

    Chest 1: If this chest is empty, then the other chest’s message is true.
    Chest 2: This chest is filled with treasure or the other chest contains deadly scorpions.

How about rephrasing as two statements S1 and S2:

    S1: if C1 is empty, then S2
    S2: C2 is filled with treasure or C1 is filled with scorpions

You know exactly one of these messages is true. What should you do? Which chest should you open?

Suppose S1 is false, so S2 is true. The negation of S1 (which is true if S1 is false!) is that C1 is empty and not S2. This is a contradiction, because we know S2 is true!

So we know S1 is true and S2 is false. The falsity of S2 means that  C2 is not filled with treasure and C1 does not have scorpions. And also C1 is not empty. Let's open C1! (But carefully; it might be filled with spiders. Or half full of scorpions.)

Other proofs by contradiction:

A few words about Atomic statements: no variables.

A->B: if A then B, or A implies B. But with atomic statements, there is seldom any cause-and-effect.

Converse: B->A    not automatically true, though sometimes an interesting question

Contrapositive: ¬B -> ¬A. This is automatically true.

if and only if, or <->

P is sufficient for Q: P->Q.  P is necessary for Q: Q->P

An example proof: if a and b are even, then so is a+b  (Levin example 0.2.4)

Quantifiers: ∀ (for all) and ∃ (there exists)

Here are some simple two-person two-move games. In each case, player A chooses a whole number a, and then, seeing A's move, B chooses a whole number b. Based on the choices a and b, we determine which player wins.

  1. The first player to choose a 3 wins (that is, B wins if a!=3 and b=3)
  2. B wins if a+b<100
  3. Whichever player chooses the largest number wins

In each game above, who has the winning strategy?

Does every 2-move game have a winning strategy?

Negation of quantifiers

From Levin p 15. Are any of the following equivalent? Do any imply any other?

  1. You can fool some people all of the time.
  2. You can fool everyone some of the time.
  3. You can always fool some people.
  4. Sometimes you can fool everyone.

The first is (∃ person p)(∀ time t) CanFool(p,t)
The second is either ∀p∃t(CanFool(p,t)) or else ∃t∀p CanFool(p,t). What is the difference? Let's go with the first.
The third is ∃p∀t CanFool(p,t), same as the first.
The fourth is ∃t∀p CanFool(p,t)

A predicate is a formula with a free variable. We can bind that variable by sticking a quantifier for it on the front.

A few more examples of quantifier negation:

Not exists

¬∃a ∀b Win1(a,b)

lim f(x) = L as x->a: ∀e ∃d ∀x |x-a| < d => |f(x)-L| < e.

Suppose you are driving down the highway and see a sign:

    No passing zones not striped

A hyphen is missing: no-passing zones are not striped, because they just repaved. But what if we take it to mean:

    There are not any passing zones that are not striped

What does this mean?


    start with: n is even if and only if n*n is even.

    n is even if ∃k (n=2*k)

You are walking down a path when you come to two doors. Opening one of the doors will lead you to a life of prosperity and happiness, while opening the other door will lead to a life of misery and sorrow. You don't know which door leads to which life. In front of the doors are two trolls who know which door leads where. One of the trolls is a knight, the other is a knave. You don't know which is which. You are allowed to ask one single question to one of the trolls (not both) to figure out which door to open. What question should you ask?

Homework 1, due Friday Jan 28, on Sakai