Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of...

68
Boolean Logic Chapter 4 (Sections 4.1 and 4.2)

Transcript of Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of...

Page 1: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Boolean Logic

Chapter 4 (Sections 4.1 and 4.2)

Page 2: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

The Roots: Logic1848 George Boole The Calculus of Logic

chocolate and nuts and mint

Page 3: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

The Roots: Logic

cheese and (pepperoni or sausage)

Page 4: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Boolean Searching

• crane silk

• Washington (pin,button,charm)

Page 5: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

What’s the “Native Language” of Our Computers?

The first “computers” were actually people who crunched numbers.

The Mathematical Tables Project

NY in the 1940’s

http://gridtalk-project.blogspot.com/2010/09/when-computers-were-human.html Top Secret Rosies: http://www.cnn.com/2011/TECH/innovation/02/08/women.rosies.math/index.html?hpt=C2

Page 6: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Would You Like the Job?

Page 7: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Babbage’s Analytical Engine

In 1833 Charles Babbage (1791-1871) conceived a plan for a general purpose calculating machine. It was designed to contain a store, a mill, capable of performing the four operations of arithmetic, an input/output system which used punched cards, and a printer to display the results. The engine would have been steam-driven and programmed by the punched cards. It was designed in great detail on paper but it was never completed. This is a portion of the mill with a printing mechanism.

Page 8: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

What’s the “Native Language” of Our Computers?

CDC 6600 c. 1980

Page 9: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Computing Today

Page 10: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Computing Is About Boolean Logic

The rules of the logic tell us how to manipulate inputs and produce outputs.

We define the rules so that we get answers that are useful to us.

Page 11: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Boolean Operators

NOT

P

True

False

Page 12: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Boolean Operators

NOT

P

True False

False True

Page 13: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Boolean Operators

AND

P Q P Q

True True

True False

False True

False False

Page 14: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Boolean Operators

AND

P Q P Q

True True True

True False False

False True False

False False False

Page 15: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Boolean Operators

OR

P Q P Q

True True

True False

False True

False False

Page 16: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Boolean Operators

OR

P Q P Q

True True True

True False True

False True True

False False False

Page 17: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Boolean Operators

IMPLIES

P Q P Q

True True

True False

False True

False False

Page 18: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Boolean Operators

IMPLIES

P Q P Q

True True True

True False False

False True True

False False True

Page 19: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Boolean Operators

EQUIVALENCE

P Q P Q

True True

True False

False True

False False

Page 20: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Boolean Operators

EQUIVALENCE

P Q P Q

True True True

True False False

False True False

False False True

Page 21: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Boolean Logic

P Q P P Q P Q P Q P QTrue True False True True True True

True False False True False False False

False True True True False True False

False False True False False True True

Page 22: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Using Boolean Logic

P Q P

True True

True False

False True

False False

P ((P Q) Q)

Page 23: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Using Boolean Logic

P Q P P Q

True True False

True False False

False True True

False False True

P ((P Q) Q)

Page 24: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Using Boolean Logic

P Q P P Q P Q

True True False True

True False False True

False True True True

False False True False

P ((P Q) Q)

Page 25: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Using Boolean Logic

P Q P P Q A B (P Q) Q

True True False True True

True False False True False

False True True True True

False False True False True

P ((P Q) Q)

Page 26: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Using Boolean Logic

P Q P P Q P Q (P Q) Q P ((P Q) Q)

True True False True True True

True False False True False False

False True True True True True

False False True False True True

P ((P Q) Q)

Page 27: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Using Boolean Logic

P Q P P Q P Q (P Q) Q P ((P Q) Q)

True True False True True True False

True False False True False False False

False True True True True True True

False False True False True True True

P ((P Q) Q)

Page 28: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Another ExampleE H N S E H ((E H) N S

True True True True True

True True True True True

True False True True

True False True True

True True True True

True True True True

True False True

True False True

Page 29: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Another ExampleE H N S E H ((E H) N S

True True True True True

True True True True True

True False True True

True False True True

True True True True

True True True True

True False True

True False True

((Exhausted HidingPlaceNearby) Nightime) StopToSleep

Page 30: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Boolean Logic

P Q P P Q P Q P Q P QTrue True False True True True True

True False False True False False False

False True True True False True False

False False True False False True True

Let’s practice.

Page 31: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Booleans in Python

def chocolate(): password = input("Type your password: ") while password != "chocolate": password = input("Try again: ") print("Got it!!")

Page 32: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Booleans in Python

def for_dummies(): password = input("Type your password: ") tries = 0 while password != "chocolate" and tries < 5: password = input("Try again: ") tries +=1 if tries == 5: print("Okay, you've tried hard enough") else: print("Got it!!")

Page 33: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Booleans in Python

def for_dummies1(): password = input("Type your password: ") tries = 0 while not(password == "chocolate" or tries >= 5): password = input("Try again: ") tries +=1 if tries == 5: print("Okay, you've tried hard enough") else: print("Got it!!")

Page 34: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Boolean Identities

This notation:

• Multiply for AND• Add for OR

Page 35: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Proving These Things

A B A B (A B)

True True True False

True False False True

False True False True

False False False True

A B A B A B

True True False False False

True False False True True

False True True False True

False False True True True

Prove the first of deMorgan’s laws:

(A B) A B

Page 36: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Proving These Things

A B A B (A B)

True True True False

True False False True

False True False True

False False False True

A B A B A B

True True False False False

True False False True True

False True True False True

False False True True True

Prove the first of deMorgan’s laws:

(A B) A B

Page 37: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Satisfiability

A Boolean formula is satisfiable if and only if there is some row of the truth table that is T.

P Q P P Q P Q (P Q) Q P ((P Q) Q)

True True False True True True False

True False False True False False False

False True True True True True True

False False True False True True True

The job of a SAT solver is to determine satisfiability.

Page 38: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Using Boolean Expressions

(W C D) (W A D)

Is this expression satisfiable?

Page 39: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Using Boolean Expressions

(Wounded CanRun Daylight) (Wounded InAmbulance Daylight)

Is this expression satisfiable?

Page 40: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Binary Boolean Operators

P Q

T T T T T T T T T T F F F F F F F F

T F T T T T F F F F T T T T F F F F

F T T T F F T T F F T T F F T T F F

F F T F T F T F T F T F T F T F T F

What about the other 12 columns?

Page 41: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Boolean Operators

Exclusive Or XOR

P Q P Q

True True

True False

False True

False False

Chips OR Fries

Page 42: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Boolean Operators

Exclusive Or XOR

P Q P Q

True True False

True False True

False True True

False False False

Chips OR Fries

Page 43: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Boolean Operators

Not And NAND

P Q NAND

True True True

True False False

False True False

False False False

Page 44: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Boolean Operators

Not And NAND

P Q NAND

True True True False

True False False True

False True False True

False False False True

Page 45: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Boolean Operators

Not Or NOR

P Q NOR

True True True

True False True

False True True

False False False

Page 46: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Boolean Operators

Not Or NOR

P Q NOR

True True True False

True False True False

False True True False

False False False True

Page 47: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Binary Boolean Operators

P Q NAND NOR

T T T T T T T T T T F F F F F F F F

T F T T T T F F F F T T T T F F F F

F T T T F F T T F F T T F F T T F F

F F T F T F T F T F T F T F T F T F

Page 48: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Boolean Circuits

NOT

Page 49: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Boolean Circuits

AND

Page 50: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Boolean Circuits

OR

Page 51: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Boolean Circuits

XOR

Page 52: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Boolean Circuits

NAND

Page 53: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Boolean Circuits

NOR

Page 54: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Boolean Gates• Not

• And

• Or

• XOR

• NAND

• NOR

Page 55: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Circuits That ComputeBuilding an Adder

0 0 1 1+ 0 + 1 + 0 + 1 0 1 1 1 0

A half adder:

Page 56: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Circuits That ComputeBuilding an Adder

0 0 1 1+ 0 + 1 + 0 + 1 0 1 1 1 0

A half adder:

Page 57: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Circuits That ComputeBuilding an Adder

0 0 1 1+ 0 + 1 + 0 + 1 0 1 1 1 0

A full adder:

Page 58: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Reasoning About Circuits (and Programs)

• CircuitA Specification

• ProgramB Specification

Page 59: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Satisfiability

Recall: A Boolean formula is satisfiable if and only if there is some row of the truth table that is T.

P Q P P Q P Q (P Q) Q P ((P Q) Q)

True True False True True True False

True False False True False False False

False True True True True True True

False False True False True True True

The job of a SAT solver is to determine satisfiability.

Page 60: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Reasoning About Circuits (and Programs)

• CircuitA Specification

• ProgramB Specification

So we want to assure that:

(CircuitA Specification)

is not satisfiable.

Page 61: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Other Applications of SAT Solvers

• Cryptography

• Artificial Intelligence:

• Planc Problem solved

• Is new fact1 consistent with what we already know?

Page 62: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Other Applications of SAT Solvers

• Is new fact1 consistent with what we already know?

(T A) L H TD TU A

U H L

Put another way, is the following formula satisfiable?

((T A) L) (H T) (D T ) (U A ) (U H L )

Page 63: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Other Applications of SAT Solvers

• Is new fact1 consistent with what we already know?

So what’s the problem? Write out the truth table and we are done.

(T A) L (Texan Aggie) Longhorn H T Houston TexanD T Dallas TexanU A UT Aggie

U H L UT Houston Longhorn

Page 64: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

How Big Are the Truth Tables?

P Q RTrue True True

True True False

True False True

True False False

False True True

False True False

False False True

False False False

Page 65: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

Back to the Longhorn Problem

(T A) L H TD TU A

U H L

How many rows in the truth table for this?

Page 66: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

The Longhorn Problem

T A L H D UTrue True True True True True

True True True True True

True False True True True

True False True True

True True True True True

True True True True

True False True True

True False True

Page 67: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

2n

0

200000

400000

600000

800000

1000000

1200000

1 3 5 7 9 11 13 15 17 19 21

Page 68: Boolean Logic Chapter 4 (Sections 4.1 and 4.2). The Roots: Logic 1848 George Boole The Calculus of Logic chocolate and  nuts and mint.

But Practical Solutions Exist

They routinely solve problems with hundreds of thousands of variables.