Based on slides by Patrice Belleville CPSC 121: Models of Computation Unit 10: A Working Computer.
CPSC 121: Models of Computation 2012 Summer Term 2
description
Transcript of CPSC 121: Models of Computation 2012 Summer Term 2
snick
snack
CPSC 121: Models of Computation2012 Summer Term 2
Propositional Logic: Conditionals and Logical Equivalence
Steve Wolfman, based on notes by Patrice Belleville and others
1
This work is licensed under a Creative Commons Attribution 3.0 Unported License.
Outline
• Prereqs, Learning Goals, and Quiz Notes
• Problems and Discussion– Logical equivalence practice– How to write a logical equivalence proof– A flaw in our model
• Next Lecture Notes
2
Learning Goals: Pre-Class
By the start of class, you should be able to:– Translate back and forth between simple natural
language statements and propositional logic, now with conditionals and biconditionals.
– Evaluate the truth of propositional logical statements that include conditionals and biconditionals using truth tables.
– Given a propositional logic statement and an equivalence rule, apply the rule to create an equivalent statement.
Example: given (u s) s, apply p q ~p q.Note: p maps to (u s) and q maps to s.Result: ~(u s) s 11
Learning Goals: In-Class
By the end of this unit, you should be able to:– Explore alternate forms of propositional logic
statements by application of equivalence rules, especially in order to simplify complex statements or massage statements into a desired form.
– Evaluate propositional logic as a “model of computation” for combinational circuits, including at least one explicit shortfall (e.g., referencing gate delays, fan-out, transistor count, wire length, instabilities, shared sub-circuits, etc.).
12
Where We Are inThe Big Stories
Theory
How do we model computational systems?
Now: practicing our second technique for formally establishing the truth of a statement (logical equivalence proofs).
(The first technique was truth tables.)
Hardware
How do we build devices to compute?
Now: learning to modify circuit designs using our logical model, gaining more practice designing circuits, and identifying a flaw in our model for circuits.
13
Motivating Problem
Simplify this code, proving your simplification correct:
14
List version:
;; ListOfNatural -> Boolean;; Produces true if all numbers in lon ;; are positive and false otherwise.(define (all-positive? lon) (cond [(empty? lon) true] [else (if (<= (first lon) 0) false (all-positive? (rest lon)))]))
Image version:
;; Image -> Boolean;; Produces true if image is too large to ;; fit on the HEIGHT-by-WIDTH screen.(define (too-large? image) (if (> (image-width image) WIDTH) true (if (> (image-height image) HEIGHT) true false)))
Outline
• Prereqs, Learning Goals, and Quiz Notes
• Problems and Discussion– Logical equivalence practice– How to write a logical equivalence proof– A flaw in our model
• Next Lecture Notes
15
Problem: 4-Segment LED Display
Problem: Build a circuit that displays the numbers 1 through 9 represented by four Boolean values a, b, c, and d on a 4-segment Boolean display.
1 2 3 4 5
6 7 8 9
16
Problem: 4-Segment LED Display
Problem: Build a circuit that displays the numbers 1 through 9 represented by four Boolean values a, b, c, and d on a 4-segment Boolean display.
1 2 3 4 5
6 7 8 9
17
321
654
987
RECALL: Representing Positive Integers
18
This is the convention we (and computers) use for the positive integers 0-9, which requires 4 variables:
# a b c d
0 F F F F
1 F F F T
2 F F T F
3 F F T T
4 F T F F
5 F T F T
6 F T T F
7 F T T T
8 T F F F
9 T F F T
...
Problem: Equivalent Circuits
Problem: Consider these circuits for the top LED in the 4-segment display (assuming inputs abcd from the previous table):
1.(~a b) (a ~b ~c)
2.(~a b) a3.a b
4.a b
19
321
654
987
Problem: Equivalent Circuits
Problem: Consider these circuits for the top LED in the 4-segment display (assuming inputs abcd from the previous table).
1.(~a b) (a ~b ~c)
2.(~a b) a3.a b
4.a b
Which of 2, 3, and 4 are equivalent?a.None of them.b.2 and 3 but not 4.c.2 and 4 but not 3.d.3 and 4 but not 2.e.2, 3, and 4.
20
321
654
987
Problem: Equivalent Circuits
Problem: Consider these circuits for the top LED in the 4-segment display (assuming inputs abcd from the previous table).
1.(~a b) (a ~b ~c)
2.(~a b) a3.a b
4.a b
How many of these are correct?a.None of them.b.One of them.c.Two of them.d.Three of them.e.All four of them.
21
321
654
987
Problem: Equivalent Circuits
Problem: Consider these circuits for the top LED in the 4-segment display (assuming inputs abcd from the previous table).
1.(~a b) (a ~b ~c)
2.(~a b) a3.a b
4.a b
Which of these proves that circuit 1 is not equivalent to circuit 3:a.2 and 4 are equivalentb.1 mentions c but 3 does not.c.1 is false with a=T b=F c=T.d.None of these.
22
321
654
987
Outline
• Prereqs, Learning Goals, and Quiz Notes
• Problems and Discussion– Logical equivalence practice– How to write a logical equivalence proof– A flaw in our model
• Next Lecture Notes
23
Writing an Equiv Proof: Prove (~a b) a a b
Theorem: (~a b) a a bProof:
(~a b) a
a (~a b) by commutativity
(a ~a) (a b) by distribution
...to be filled in...
a b by identity
QED24
Writing an Equiv Proof: Prove (~a b) a a b
Theorem: (~a b) a a bProof:
(~a b) a
a (~a b) by commutativity
(a ~a) (a b) by distribution
...to be filled in...
a b by identity
QED25
State your theorem.
Explicitly start the proof.
Start with one side...
Writing an Equiv Proof: Prove (~a b) a a b
Theorem: (~a b) a a bProof:
(~a b) a
a (~a b) by commutativity
(a ~a) (a b) by distribution
...to be filled in...
a b by identity
QED26
Start with one side...
Each line starts with to indicate it’s equivalent to the previous line.
...and work to the other.
End with QED!
Writing an Equiv Proof: Prove (~a b) a a b
Theorem: (~a b) a a bProof:
(~a b) a
a (~a b) by commutativity
(a ~a) (a b) by distribution
...
a b by identity
QED27
Give the next statement. And justify how you got it.
Problem: Prove (~a b) a a b
Theorem: (~a b) a a bProof:
(~a b) a
a (~a b) by commutativity
(a ~a) (a b) by distribution
?????? by negation
a b by identity
QED
28
What’s missing?
a.(a b)b.F (a b)c.a (a b)d.None of these, but I know what it is.e.None of these, and there’s not enough information to tell.
PRACTICE Problem: Arbitrary Logic Expression
Problem: Aliens hold the Earth hostage, and only you can save it by proving (a b) ~(b a) ~a b.
Reminder (“IMP” on “Dave’s Formula Sheet”): p q ~p q29
Collected WisdomSo far:•Try getting rid of : p q ~p q•Try moving negations inward: ~(p q) ~p ~q•Work from the simpler side first, BUT•Switch to different strategies/sides when you get stuck•In the end, write the proof in clean “one-side-to-the-other” form and double-check steps
Also:•Try distribution to pull out common terms OR•Try distribution to rearrange terms•Try working with a simpler part of the statement first•Try getting rid of : p q p q q p•Try getting rid of : p q ~(p q)
30
Outline
• Prereqs, Learning Goals, and Quiz Notes
• Problems and Discussion– Logical equivalence practice– How to write a logical equivalence proof– A flaw in our model
• Next Lecture Notes
31
“Multiplexer”
A circuit that, given three inputs a, b, and c (the “control” signal), outputs a’s value when c is F and b’s when c is T.
This circuit is called a multiplexer.
a
bout
c
0
1
32
(~a b c) ( a ~b ~c) ( a b ~c) ( a b c)
a b c out
F F F F
F F T F
F T F F
F T T T
T F F T
T F T F
T T F T
T T T T
MUX Design
Here’s one implementation of the multiplexer (MUX) circuit.
(Is this equivalentto the previousslide’s formula?Good question...prove that it is forpractice at home!)
33
a
bout
c
0
1
Truthy MUX
What is the intended output if both a and b are T?
a. T
b. F
c. Unknown... but could be answered given a value for c.
d. Unknown... and might still be unknown even given a value for c.
34
a
bout
c
0
1
Glitch in MUX Design
Imagine the circuit is in steady-state with a, b, and c all T.
Trace how changes flow when we change c to F, if each gate takes 10ns to operate.
35PRACTICE: Prove that this circuit’s model is logically
equivalent to the previous slide’s statement.
a
bout
c
0
1
Trace: -1 ns
36
T
F
T
TT
T
F
a
bout
c
0
1
Trace: 0 ns
37
T
F
T
TT
F
F
OFF
*
*
a
bout
c
0
1
Trace: 10 ns
38
T
F
T
FT
F
T
OFF
*
*
a
bout
c
0
1
Tracing a MUX
Assume the output starts at 1. We want it to end up at 1. How long does it take before it’s stable? (Choose the best answer.)
a.0 ns (it never varies from 1)
b.10 ns (it varies before 10ns, but not after)
c.20 ns
d.30 ns
e.40 ns39
a
bout
c
0
1
Trace: -1 ns
40
T
F
T
TT
T
F
a
bout
c
0
1
Trace: 0 ns
41
T
F
T
TT
F
F
OFF
*
*
a
bout
c
0
1
Trace: 10 ns
42
T
F
T
FT
F
T
OFF
*
*
a
bout
c
0
1
Trace: 20 ns
43
T
T
F
FT
F
T
OFF
*
a
bout
c
0
1
Trace: 30 ns
44
T
T
T
FT
F
T
OFF
a
bout
c
0
1
Trace: 40 ns
45
T
T
T
FT
F
T
OFF
a
bout
c
0
1
Other MUX Glitches?
The mux glitches because information from c travels two paths with different delays. While the longer path “catches up” the circuit can be incorrect.
PRACTICE: Trace the circuit to show why none of these can cause a glitch:• Changing a (keeping b and c constant)• Changing b (keeping a and c constant)• Changing c (keeping a and b constant if at least
one of them is F)
46
a
bout
c
0
1
A Glitchless MUX
This circuit uses what we know when a = b = T.
PRACTICE: Prove that it’s logically equivalent to the original MUX.
Hint: break a b up into two cases, one where c is true and one where c is false: a b c and a b ~c.
47
a
bout
c
0
1
Outline
• Prereqs, Learning Goals, and Quiz Notes
• Problems and Discussion– Logical equivalence practice– How to write a logical equivalence proof– A flaw in our model
• Next Lecture Notes
48
Learning Goals: In-Class
By the end of this unit, you should be able to:– Explore alternate forms of propositional logic
statements by application of equivalence rules, especially in order to simplify complex statements or massage statements into a desired form.
– Evaluate propositional logic as a “model of computation” for combinational circuits, including at least one explicit shortfall (e.g., referencing gate delays, fan-out, transistor count, wire length, instabilities, shared sub-circuits, etc.).
49
Motivating Problem
Simplify this code, proving your simplification correct:
50
List version:
;; ListOfNatural -> Boolean;; Produces true if all numbers in lon ;; are positive and false otherwise.(define (all-positive? lon) (cond [(empty? lon) true] [else (if (<= (first lon) 0) false (all-positive? (rest lon)))]))
Image version:
;; Image -> Boolean;; Produces true if image is too large to ;; fit on the HEIGHT-by-WIDTH screen.(define (too-large? image) (if (> (image-width image) WIDTH) true (if (> (image-height image) HEIGHT) true false)))
We’ll focus only on this one. Left as an exercise. But…Note: if you simplify
away the cond, you losethe structure that reflects the
ListOfNatural data representation.
Doing in class if we have time.
Motivating Problem
Simplify the code, proving your simplification correct.
51
Let w = (> (image-width image) WIDTH).Let h = (> (image-height image) HEIGHT).
Then, what does the code mean?
(if w then true, else (if h then true, else false))
How do we express (if x then y, else z) in propositional logic?
(x y) (~x z)
Which gives us:
(w T) (~w ((h T) (~h F)))
Motivating Problem
Simplify the code, proving your simplification correct.
52
Simplify: (w T) (~w ((h T) (~h F)))
How should we proceed? What’s a promising starting point?
Motivating Problem
Simplify the code, proving your simplification correct.
53
Simplify: (w T) (~w ((h T) (~h F)))
How should we proceed? What’s a promising starting point?
Which of these is relevant?•Try getting rid of : p q ~p q•Try moving negations inward: ~(p q) ~p ~q•Work from the simpler side first, BUT•Switch to different strategies/sides when you get stuck•In the end, write the proof in clean “one-side-to-the-other” form and double-check steps•Try distribution to pull out common terms OR•Try distribution to rearrange terms•Try working with a simpler part of the statement first•Try getting rid of : p q p q q p•Try getting rid of : p q ~(p q)
Motivating Problem
Simplify the code, proving your simplification correct.
54
Simplify: (w T) (~w ((h T) (~h F)))
How should we proceed? What’s a promising starting point?
“Lemma 1”: (h T) (~h F) h(h T) (~h F) (~h T) (~h F) (by IMP)
(~h T) (~~h F) (by IMP) (~h T) (h F) (by DNEG) skippable T (h F) (by UB) T h (by I) h (by I)
Motivating Problem
Simplify the code, proving your simplification correct.
55
Simplify: (w T) (~w ((h T) (~h F)))
How should we proceed? What’s a promising starting point?
(w T) (~w ((h T) (~h F))) (w T) (~w h) (by Lemma 1) (~w T) (~w h) (by IMP) (~w T) (w h) (by IMP) T (w h) (by UB) w h (by I)
Motivating Problem
Simplify the code, proving your simplification correct.
56
What can we conclude from: (w T) (~w ((h T) (~h F))) w h
This code: (if (> (image-width image) WIDTH) true (if (> (image-height image) HEIGHT) true false)))
Is equivalent to this code:
(or (> (image-width image) WIDTH) (> (image-height image) HEIGHT))
WARNING: Be careful as you use proofs like this if evaluating part of the code matters for more than its result (e.g., for printing something to the screen).
Motivating ProblemWe’ve proven that this:
(if (> (image-width image) WIDTH)
true
(if (> (image-height image) HEIGHT)
true
false)))
Is equivalent to:
(or (> (image-width image) WIDTH)
(> (image-height image) HEIGHT))
Should you “even bother” running your tests, since we proved it?
57
Of course! Your tests give you confidence that your proof was correct, and that your model was accurate.
By the way, notice something interesting: our proof prvoved the code transformation correct. Do passing tests prove the code they test is correct? Tests and proofs serve different purposes.
Next Lecture Learning Goals: Pre-Class
By the start of class, you should be able to:– Convert positive numbers from decimal to binary
and back.
– Convert positive numbers from hexadecimal to binary and back.
– Take the two’s complement of a binary number.
– Convert signed (either positive or negative) numbers to binary and back.
– Add binary numbers.
58
Next Lecture Prerequisites
Read Section 1.5 and the supplemental handout: http://www.ugrad.cs.ubc.ca/~cs121/2009W1/Handouts/signed-binary-decimal-conversions.html
Solve problems like Exercise Set 1.5, #1-16, 27-36, and 41-46.
NOTE: the binary numbers in problems 27-30 are signed (that is, they use the two’s complement representation scheme).
Complete the open-book, untimed quiz on Vista that’s due before the next class.
59
snick
snack
Some Things to Try...
(on your own if you have time, not required)
60
Problem: String equals
Consider the following from the Java 6 API documentation for the String class’s equals method:...The [method returns] true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.
Let n1 mean “this string is null”, n2 mean “the argument is null”, r mean “the method returns true”, and s mean “the two strings are objects that represent the same sequence of characters”.
Presumably any two null strings are equal to each other. Then, equality would become something like “the method returns true if and only if the two strings both null or are objects that represent the same sequence of characters”.
Problem: Is that logically equivalent to the statement from the API? Why or why not?
61
Problem: Decisions in Code
Consider the following code, part of a “binary bounds search”:if target equals value then
if lean-left-mode is true
call the go-left routine
otherwise
call the go-right routine
otherwise if target is less than value then
call the go-left routine
otherwise
call the go-right routine
62
Problem: Decisions in Code
Let gl mean “the go-left routine is called”
Problem: complete the logical expression
gl _____________________
Problem: Use your logic to simplify the pseudocode so it requires just one “if/otherwise”.
63
Problem: Prove (~a b) a a b
Theorem: (~a b) a a bProof:
(~a b) a
a (~a b) by commutativity
(a ~a) (a b) by distribution
...
a b by identity
QED64
State your theorem.
Explicitly start the proof.
Start with one side...
Each line starts with to indicate it’s equivalent to the previous line.
Give the next statement. And justify how you got it.
End with QED!
...and work to the other.