Lecture 3
Lecture 3
Answer at
https://cs103.stanford.edu/pollev
●
Here are a few music recs of our own:
●
Jami Sieber - Timeless.
●
Aaron Parks - Little Big and Little Big II.
●
Arthur Moon - NPR Music Tiny Desk Concert.
●
Shakey Graves – Roll the Bones (check out Audiotree Live version).
Propositional Logic
Question: How do we formalize the
definitions and reasoning we use in our
proofs?
Where We're Going
●
Propositional Logic (Today)
●
Reasoning about Boolean values.
●
First-Order Logic (Wednesday/Friday)
●
Reasoning about properties of multiple
objects.
Outline for Today
●
Propositional Variables
●
Booleans, math edition!
●
Propositional Connectives
●
Linking things together.
●
Truth Tables
●
Rigorously defining connectives.
●
Simplifying Negations
●
Mechanically computing negations.
Propositional Logic
TakeMath51 ∨ TakeCME100
¬FirstSucceed → TryAgain
IsCardinal ∧ IsWhite
TakeMath51 ∨ TakeCME100
¬FirstSucceed → TryAgain
IsCardinal ∧ IsWhite
¬FirstSucceed → TryAgain
IsCardinal ∧ IsWhite
●
Each variable can take one one of two
values: true or false. You can think of
them as bool values.
Propositional Connectives
●
There are seven propositional connectives, five
of which will be familiar from programming.
●
First, there’s the logical “NOT” operation:
¬p
●
You’d read this out loud as “not p.”
●
The fancy name for this operation is logical
negation.
Truth Tables
●
A truth table is a table showing the
truth value of a propositional logic
formula as a function of its inputs.
●
Let’s examine the truth tables for the
connectives we’re exploring today!
“I don’t love cupcakes.”
¬LoveCupcakes
Propositional Variables
●
In propositional logic, individual propositions
are represented by propositional variables.
●
Each variable can take one one of two values:
true or false. You can think of them as bool
values.
●
In a move that contravenes programming style
conventions, propositional variables are usually
represented as lower-case letters, such as p, q,
r, s, etc.
●
That said, there’s nothing stopping you from using
multiletter names!
“I don’t love cupcakes.”
¬LoveCupcakes
Propositional Connectives
●
There are seven propositional connectives, five
of which will be familiar from programming.
●
Next, there’s the logical “AND” operation:
p∧q
●
You’d read this out loud as “p and q.”
●
The fancy name for this operation is logical
conjunction.
“It’s cardinal and white.”
IsCardinal ∧ IsWhite
Propositional Connectives
●
There are seven propositional connectives, five
of which will be familiar from programming.
●
Then, there’s the logical “OR” operation:
p∨q
●
You’d read this out loud as “p or q.”
●
The fancy name for this operation is logical
disjunction. This is an inclusive or.
“You must take Math 51 or CME 100.”
TakeMath51 ∨ TakeCME100
Propositional Connectives
●
There are seven propositional connectives, five
of which will be familiar from programming.
●
There’s also the “truth” connective:
⊤
●
You’d read this out loud as “true.”
●
Although this is technically considered a a
connective, it “connects” zero things and
behaves like a variable that’s always true.
Propositional Connectives
●
There are seven propositional connectives, five
of which will be familiar from programming.
●
Finally, there’s the “false” connective.
⊥
●
You’d read this out loud as “false.”
●
Like ⊤, this is technically a connective, but
acts like a variable that’s always false.
Inclusive and Exclusive OR
●
The ∨ connective is an inclusive “or.” It's true if at
least one of the operands is true.
●
It’s similar to the || operator in C, C++, Java, etc. and
the or operator in Python.
●
Sometimes we need an exclusive “or,” which isn’t
true if both inputs are true.
●
We can build this out of what we already have.
Answer at
https://cs103.stanford.edu/pollev
Quick Question:
¬(p ∧ q) is equivalent to ??
¬(p ∨ q) is equivalent to ??
de Morgan’s Laws in Code
●
Pro tip: Don't write this:
if (!(p() && q())) {
/* … */
}
●
Write this instead:
if (!p() || !q()) {
/* … */
}
●
(This even short-circuits correctly: if p()
returns false, q() is never evaluated.)
Mathematical Implication
Implication
●
We can represent implications using this
connective:
p→q
●
You’d read this out loud as “p implies q.”
●
The fancy name for this is the material
conditional.
●
Question: What should the truth table for
p → q look like?
p q p→q
F F T
F T T
T F F
T T T
p q p→q
F F T
F T T
T F F
T T T
Important observation:
The statement p → q is true
whenever p ∧ ¬q is false.
p q p→q
F F T
F T T
T F F
T T T
¬FirstSucceed → TryAgain
JerseyMikes : It’s Jersey Mike’s.
FreshlySliced : It’s freshly sliced.
¬FreshlySliced → ¬JerseyMikes
Contrapositive?
JerseyMikes : It’s Jersey Mike’s.
FreshlySliced : It’s freshly sliced.
¬FreshlySliced → ¬JerseyMikes
JerseyMikes → FreshlySliced
An Important Equivalence
●
The truth table for for p → q is chosen so
that the following is true:
p→q is equivalent to ¬(p ∧ ¬q)
●
Later on, this equivalence will be
incredibly useful:
¬(p → q) is equivalent to p ∧ ¬q
Side Note: Contrapositive
p q p → q ¬q ¬p ¬q → ¬p
F F T T T T
F T T F T T
T F F T F F
T T T F F T
same :)
The Biconditional Connective
The Biconditional Connective
●
In our previous lecture, we saw that the
statement “p if and only if q” means both that
p → q and q → p.
●
We can write this in propositional logic using
the biconditional connective:
p↔q
●
This connective’s truth table has the same
meaning as “p implies q and q implies p.”
●
Based on that, what should its truth table look
like?
Biconditionals
●
The biconditional connective p ↔ q has
the same truth table as (p → q) ∧ (q → p).
●
Here’s what that looks like:
p q p↔q
One interpretation of ↔
F F T is to think of it as
T T T
Negating a Biconditional
●
How do we simplify
¬(p ↔ q)
using the tools we’ve seen so far?
●
There are many options, but here are our
two favorites:
p ↔ ¬q ¬p ↔ q
Question to ponder: what is
the truth table for these
statements, and where have
you seen it before?
Operator Precedence
●
How do we parse this statement?
¬x → y ∨ z → x ∨ y ∧ z
●
Operator precedence for propositional logic:
¬
∧
∨
→
↔
●
All operators are right-associative.
●
We can use parentheses to disambiguate.
Operator Precedence
●
The main points to remember:
●
¬ binds to whatever immediately follows it.
●
∧ and ∨ bind more tightly than →.
●
We will commonly write expressions like
p ∧ q → r without adding parentheses.
●
For more complex expressions, we'll try to
add parentheses.
●
Confused? Please ask!
The Big Table
Connective Read Aloud As C++ Version Fancy Name Negation
¬p “not” ! Negation p
¬p ∨ ¬q
p∧q “and” && Conjunction
p → ¬q
p ↔ ¬q
p↔q “if and only if” see PS2! Biconditional
¬p ↔ q
Time-Out for Announcements!
✉️ Submitting Work ✉️
●
All assignments should be submitted through GradeScope.
●
The programming portion of the assignment is submitted separately
from the written component.
●
The written component must be typed; handwritten solutions don’t
scan well and get mangled in GradeScope.
●
All assignments are due at 1:00PM. You have three “late days”
you can use throughout the quarter. Each automagically extends
assignment deadlines from Friday at 1:00PM to Saturday at
1:00PM; at most one late day can be used per assignment.
●
Very good idea: Leave at least two hours buffer time for your first
assignment submission, just in case something goes wrong.
●
Very bad idea: Wait until the last minute to submit.
●
Your score on the problem sets is the square root of your raw
score. So an 81% maps to a 90%, a 50% maps to a 71%, etc. This
gives a huge boost even if you need to turn something in that
isn’t done.
Office Hours
●
Office hours have started (as of Sunday)! Think of them
as “drop-in help hours” where you can ask questions on
problem sets, lecture topics, etc.
●
Check the Guide to Office Hours on the course website for the
schedule.
●
TA office hours are held in person in the Huang
basement. Keith’s are in Durand 317. Sean’s are in
Durand 331-B.
●
Once you arrive, sign up on QueueStatus so that we can
help people in the order they arrived:
https://queuestatus.com/queues/782
●
Office hours are much less crowded earlier in the week
than later. Stop by on Sunday, Monday, and Tuesday!
Back to CS103!
Recap So Far
●
A propositional variable is a variable that is
either true or false.
●
The propositional connectives are
●
Negation: ¬p
●
Conjunction: p ∧ q
●
Disjunction: p ∨ q
●
Truth: ⊤
●
Falsity: ⊥
●
Implication: p → q
●
Biconditional: p ↔ q
Why All This Matters
Why All This Matters
●
Suppose we want to prove the following
statement:
“If x + y = 16, then x ≥ 8 or y ≥ 8”
Why All This Matters
●
Suppose we want to prove the following
statement:
“If x + y = 16, then x ≥ 8 or y ≥ 8”
x < 8 ∧ y < 8 → x + y ≠ 16