Escolar Documentos
Profissional Documentos
Cultura Documentos
Jeremy Siek
Spring 2010
1. Course Information
2. Overview of Discrete Mathematics
Discrete
Mathematics
Definition
Mathematics is the study of any truth regarding well-defined
concepts.
Definition
Something is discrete if is it composed of distinct, separable parts. (In
contrast to continuous.)
Discrete Continuous
integers real numbers
graphs rational numbers
state machines differential equations
digital computer radios
quantum physics Newtonian physics
Definition
Discrete Mathematics is the study of any truth regarding discrete
entities.
7 3 9 2
1 3 6 7 9
1 8 3 6
5 7 9 3
6 4 2
3 2 7 1
8 2 6 7 4 9 3
7 4 3 8 2
5 8 2 1 4
7 3 9 2
1 3 6 7 9
1 8 3 6
5 7 9 3
6 4 2
3 2 7 1
8 2 6 7 4 9 3
7 4 3 8 2
5 8 2 1 4
7 3 9 2
1 3 6 7 9
1 8 3 6
5 7 9 3
6 4 2
3 2 7 1
8 2 6 7 4 9 3
7 4 3 8 2
5 8 2 1 4
7 3 9 2
1 3 6 7 9
1 8 3 6
5 7 9 3
6 4 2
3 2 7 1
8 2 6 7 4 9 3
7 4 3 8 2
5 8 2 1 4
7 3 9 2
1 3 6 7 9
1 8 3 6
5 7 9 3
6 4 2
3 2 7 1
8 2 6 7 4 9 3
7 4 3 8 2
5 8 2 1 4
7 3 9 2
1 3 6 7 9
1 8 3 6
5 7 9 3
6 4 2
3 2 7 1
8 2 6 7 4 9 3
7 4 3 8 2
5 8 2 1 4
I Circuit design
I Computer architecture
I Computer networks
I Operating systems
I Programming: algorithms and data structures
I Programming languages
I Computer security, encryption
I Error correcting codes
I Graphics algorithms, game engines
I ...
I Read in advance.
I Do the homework.
I Form a study group.
I Form an intense love/hate relationship with Isabelle.
1. Propositional Logic
2. Syntax and Meaning of Propositional Logic
JpK(A) = A(p)
(
True if JP K(A) = False
J¬P K(A) =
False otherwise
(
True if JP K(A) = True, JQK(A) = True
JP ∧ QK(A) =
False otherwise
(
False if JP K(A) = False, JQK(A) = False
JP ∨ QK(A) =
True otherwise
(
False if JP K(A) = True, JQK(A) = False
JP −→ QK(A) =
True otherwise
Definition
A tautology is a proposition that is true in any truth assignment.
Examples:
I p −→ p
I q ∨ ¬q
I (p ∧ q) −→ (p ∨ q)
There are two ways to show that a proposition is a tautology:
1. Check the meaning of the proposition for every possible truth
assignment. This is called model checking.
2. Contruct a proof that the proposition is a tautology.
Propositional Logic:
I The kinds of propositions.
I The meaning of propositions.
I How to check that a proposition is a tautology.
Forwards
Reasoning Backwards
Reasoning
Assumption
Conclusion
Assumption
I To err is human.
I Isabelle will catch your mistakes.
I Unfortunately, Isabelle is bad at describing your mistake.
I Consider the following attempted proof
theorem "p −→ (p ∧ p)"
proof -
show "p −→ (p ∧ p)"
proof
assume 1: "p"
from 1 show "p ∧ p"
I When Isabelle gets to from 1 show "p ∧ p" (adding .. at the
end), it gives the following response:
Failed to finish proof
At command "..".
I In this case, the mistake was a missing label in the from clause.
Conjuction introduction requires two premises, not one. Here’s
the fix:
theorem "p −→ (p ∧ p)"
proof -
show "p −→ (p ∧ p)"
proof
assume 1: "p"
from 1 1 show "p ∧ p" ..
qed
qed
I When Isablle says “no”, double check the inference rule. If that
doesn’t work, get a classmate to look at it. If that doesn’t work,
email the instructor with the minimal Isabelle file that exhibits
your problem.
I The problem here is that the proposition in the show "q", does
not match what we are trying to prove, which is p.
I Natural numbers: 0, 1, 2, . . .
I Integers: . . . , −1, 0, 1, . . .
I How does Isabelle know the difference between 0 (the natural
number) and 0 (the integer)?
I Sometimes it can tell from context, sometimes it can’t. (When it
can’t, you’ll see things like 0::’a)
I You can help Isabelle by giving a type annotation, such as 0 or 0.
I We use natural numbers a lot, integers not so much.
1+1−2=0
1−2+1=1
For all-Intro
have Lk : "∀ n. P"
proof
fix n
..
.
· · · show "P" · · ·
qed
For all-Elim If Li proves ∀ n. P, then write
from Li have Lk : "[n7→m]P" ..
where m is any entity of the same type as n.
The notation [n7→m]P (called substitution) refers to the proposition
that is the same as P except that all free occurences of n in P are
replaced by m.
I [x 7→ 1]x = 1
I [x 7→ 1]x = 1
I [x 7→ 1]y = y
I [x 7→ 1]x = 1
I [x 7→ 1]y = y
I [x 7→ 1](x ∧ y) = (1 ∧ y)
I [x 7→ 1]x = 1
I [x 7→ 1]y = y
I [x 7→ 1](x ∧ y) = (1 ∧ y)
I [x 7→ 1](∀y. x) = (∀y. 1)
I [x 7→ 1]x = 1
I [x 7→ 1]y = y
I [x 7→ 1](x ∧ y) = (1 ∧ y)
I [x 7→ 1](∀y. x) = (∀y. 1)
I [x 7→ 1](∀x. x) = (∀x. x) (The x under ∀x is not free, it is bound
by ∀x.)
I [x 7→ 1]x = 1
I [x 7→ 1]y = y
I [x 7→ 1](x ∧ y) = (1 ∧ y)
I [x 7→ 1](∀y. x) = (∀y. 1)
I [x 7→ 1](∀x. x) = (∀x. x) (The x under ∀x is not free, it is bound
by ∀x.)
I [x 7→ 1]((∀x.x) ∧ x) = ((∀x. x) ∧ 1)
theorem
assumes 1: "∀ x. man(x) −→ human(x)"
and 2: "∀ x. human(x) −→ hastwolegs(x)"
shows "∀ x. man(x) −→ hastwolegs(x)"
proof
fix m
show "man(m) −→ hastwolegs(m)"
proof
assume 3: "man(m)"
from 1 have 4: "man(m) −→ human(m)" ..
from 4 3 have 5: "human(m)" ..
from 2 have 6: "human(m) −→ hastwolegs(m)" ..
from 6 5 show "hastwolegs(m)" ..
qed
qed
(∀ x. P x −→ Q x) ∧ P a −→ Q a
even(n) ≡ ∃m. n = 2m
odd(n) ≡ ∃m. n = 2m + 1
Theorem
If n and m are odd, then n + m is even.
Proof.
Because n is odd, there exists a k where n = 2k + 1. Because m is odd,
there exists a q where m = 2q + 1. So
n + m = 2k + 2q + 2 = 2(k + q + 1). Thus ∃p. n + m = 2p, and by
definition, n + m is even.
1. Proof by induction
2. Functions, defined by primitive recursion
∀ n. P n
Theorem
n(n+1)
∀n. 0 + 1 + · · · + n = 2 .
Proof.
The proof is by mathematical induction on n.
0(0+1)
I Base Step: We need to show that 0 = 2
, but that’s obviously true.
I Inductive Step: The inductive hypothesis (IH) is
0 + 1 + · · · + n = n(n+1)
2
.
n(n + 1)
0 + 1 + · · · + n + (n + 1) = (n + 1) + (by the IH)
2
2(n + 1) + n(n + 1) (n + 1)(n + 2)
= =
2 2
(n + 1)((n + 1) + 1)
= .
2
A B C
A B C
T (0) = 0
T (n) = 2T (n − 1) + 1
T (0) = 0
T (n) = 2T (n − 1) + 1
T (0) = 0
T (n) = 2T (n − 1) + 1
T (n) = 2n − 1
T (0) = 0
T (n) = 2T (n − 1) + 1
I Mathematical induction.
I New from Isabelle: by arith, primrec.
1. Use a peice of scratch paper to sketch out the main ideas of the
proof.
2. Dedicate one part of the paper to things that you know
(assumptions, stuff you’ve proven),
3. Dedicate another part of the paper to things that you’d like to
know.
4. After your sketch is complete, write a nicely organized and clean
version of the proof.
5. Now let’s look at more examples of induction.
theorem rep_diff:
assumes nm: "n ≤ m" shows "rep f (m - n) (rep f n x) = rep f m x"
oops
I Isabelle’s lists are descended from the Lisp language, they are
built up using two operations:
1. The empty list: []
2. If x is an object, and ls is a list of objects, then x # ls is a new list
with x at the front and the rest being the same as ls.
I Also, lists can be created from a comma-separated list enclosed in
brackets: [1, 2, 3, 4].
I All the objects in a list must have the same type.
xs ys xs ys
1,2,3 4,5,6 1,2,3 4,5,6
reverse app
6,5,4,3,2,1 6,5,4,3,2,1
lemma rev_app:
"reverse (app xs ys) = app (reverse ys) (reverse xs)"
proof (induct xs)
have 1: "reverse (app [] ys) = reverse ys" by simp
have 2: "app (reverse ys) (reverse []) = app (reverse ys) []"
by simp
— but no we’re stuck
show "reverse (app [] ys) = app (reverse ys) (reverse [])"
oops
lemma rev_app:
"reverse (app xs ys) = app (reverse ys) (reverse xs)"
proof (induct xs)
show "reverse (app [] ys) = app (reverse ys) (reverse [])"
using app_nil[of "reverse ys"] by simp
next
fix a xs assume IH: "reverse (app xs ys)
= app (reverse ys) (reverse xs)"
have "reverse (app (a # xs) ys)
= reverse (a # (app xs ys))" by simp
also have ". . . = app (reverse (app xs ys) ) [a]" by simp
also have ". . . = app (app (reverse ys) (reverse xs)) [a]"
using IH by simp
— We’re stuck again! What lemma do we need this time?
show "reverse (app (a # xs) ys)
= app (reverse ys) (reverse (a # xs))"
oops
lemma rev_app:
"reverse (app xs ys) = app (reverse ys) (reverse xs)"
proof (induct xs)
show "reverse (app [] ys) = app (reverse ys) (reverse [])"
using app_nil[of "reverse ys"] by simp
next
fix a xs assume IH: "reverse (app xs ys)
= app (reverse ys) (reverse xs)"
have "reverse (app (a # xs) ys)
= reverse (a # (app xs ys))" by simp
also have ". . . = app (reverse (app xs ys) ) [a]" by simp
also have ". . . = app (app (reverse ys) (reverse xs)) [a]"
using IH by simp
also have ". . . = app (reverse ys) (app (reverse xs) [a])"
using app_assoc[of "reverse ys" "reverse xs" "[a]"] by simp
also have ". . . = app (reverse ys) (reverse (a # xs))" by simp
finally show "reverse (app (a # xs) ys)
= app (reverse ys) (reverse (a # xs))" .
qed
procedure iterative_reverse(list)
xs = list
ys = []
while xs != []
ys = hd(xs) # ys
xs = tl(xs)
return ys
procedure iterative_fibonacci(n)
if n = 0 then
y := 0
else
x := 0
y : = 1
for i := 1 to n - 1
z := x + y
x := y
y := z
return y