1 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac Lecture 01: Boolean Logic Sections 1.1 and 1.2 Jarek Rossignac CS1050: Understanding and Constructing Proofs Spring
2 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac Lecture Objectives Terminology Operators and their properties Converting English into Logic Propositions Understand some applications Tools for verifying equivalences of propositions
3 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac Motivation Proofs are important in most CS applications Be able to judge whether whether a mathematical argument (proof) is correct (valid) Learn tools for constructing correct arguments (logic)
4 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac What is a proposition? Declarative sentence that is either true or false, but not both –Propositions: 1+1=2 (true) 2+2=3 (false) –Not propositions What time is it? (question) Eat! (not declarative) x:=x+1; (computer instruction) y=2x+4 (constraint, implicit equation that may be verified by some pairs)
5 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac How do we denote propositions? Use letters: p, q, r, s… –Let p denote “It is raining today” –Let q denote “I will be wet”
6 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac What is the truth value of a proposition? The truth values of a true proposition is “true” and is denoted T –Let p be “x 2 0”. The truth value of p is T The truth value of a false proposition is “false” and is denoted F –Let q be “–x 2 0”. The truth value of q is F
7 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac What are compound propositions? Formed by transforming or combining other propositions –Proposition p: “Today is Tuesday” –Proposition q: “I am teaching today” –Compound propositions: “Today is not Friday” (negation) “Today is not Friday” AND “I am teaching today”(conjunction) Reasoning about compound propositions is called propositional calculus or propositional logic
8 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac What are the main logical operators? Connectives that transform or combine propositions –Proposition p: “Today is Tuesday” –Proposition q: “I am teaching today” Negation ( p). Read “Not p” –“Today is not Tuesday” Conjunction (p q). Read “p and q” –“Today is Tuesday” AND “I am teaching today” Disjunction (p q). Read “p or q (or both)” –“Today is Tuesday” OR “I am teaching today”
9 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac What is the Exclusive Or? XOR (p q). Read “exclusive or” –Interpret as “p and q have different truth values” –EITHER “Today is Tuesday” OR “I am teaching today”, NOT BOTH –In English, “or” may mean inclusive OR ( ) or exclusive OR ( ) –“Would you like tea or coffee?”…”Yes, please” XOR (p q) is the same as DIFFERENT (p ≠ q) –“The truth values of p and q are different” Team exercise: –What does (p q) (r s) express? –Is it the same as ((p q) r) s ?
10 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac What are implications? Implication (p q) is false only when p is true and q false –p is the hypothesis or premise –q is the conclusion or consequence Many equivalent ways to interpret p q –“If p, then q”, “p implies q”, “q follows from p”, “p is sufficient for q”, –“q is necessary for p”, “p only if q” Implications are propositions (they need not be true) –“If today is Friday, then 2+3=5” is true everyday except Friday Do not confuse implications with IF-THEN statements –if (x>3) {x=3;};// caps x at 3
11 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac What are the derivatives of an implication? Consider the Implication proposition p q –“We win whenever it is raining” –IF “it is raining” THEN “we win” –IF p THEN q Its contrapositive is q p –IF “we do not win” THEN “it is not raining” –Note that it say the same thing (equivalent to the implication p q Its converse is q p –IF “we win” THEN “it is raining” –Note that it is not equivalent to the implication p q Its inverse is p q –IF “it is not raining” THEN “we don’t win” –Note that it is equivalent to the converse q p
12 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac What are biconditionals (iff)? The biconditional p q is the proposition that is true if and only if p and q have the same truth value –p q is true if and only if both p q and q p are true –p q can be written as (p q) (q p) –I like to denote it by “p==q”, as in several programming languages p q should be read as –“p is necessary and sufficient for q” –“if p, then q, and conversely” –“p implies q and vice versa” –“p when q” –“q iff q” (“iff” stands for “if and only if”)
13 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac What are truth tables? A compound proposition may, but needs not be true. –Its true value depends on the true values of its primitive propositions pq pppqpqpqpqp q p ≠ q pqpqp q p==q p q p–q TTFTTFTTF TFFFTTFFT FTTFTTTFF FFTFFFTTF
14 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac Precedence of logical operators To reduce the number of parentheses, we use the following precedence: 1) Negation 2) , Conjunction, 3) , Disjunction, Xor 4) , Implications Example: –p q s t p t –Is interpreted as:
15 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac Translating English into Logic This is not easy! (Study examples p 10-11) Do not attempt it at home! Your family may not appreciate it. Take a complex declarative English sentence –“You may access the Internet on campus only if you are a computer science major or if you are not a freshman.” Separate it into parts and assign them variable names –a stands for “you may access the Internet on campus” –c stands for “you are a computer science major –f stands for “you are a freshman” Combine using logical connective to capture the meaning –(a c f)
16 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac When are system specifications consistent? Software or hardware requirements are often specified using a natural language. System and software engineers translate each requirement into logical propositions. Then, one wishes to build a system that satisfies ALL of the requirements (i.e., assign values to all variables so that all requirements are met). A system specification is consistent if there exists an assignment of truth values to all variables that satisfies all propositions (make them true)
17 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac What are tautologies and contradictions? A compound proposition that is always true is a tautology (p p) A compound proposition that is always false is a contradiction (p p) A compound proposition that is neither a contradiction nor a tautology is a contingency
18 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac When are two propositions equivalent? When they have the same truth tables –Denoted (p q) or (p q) p and q are logically equivalent if p q is a tautology Examples The implication and its contrapositive are equivalent ( q p) (p q) The converse and inverse of an implication are equivalent ( p q) (q p) An implication is not equivalent to its converse (p q) ! (q p)
19 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac What are other useful logical equivalences? Idempotence: p p p, p p p Negation: p p F, p p T Identity: p T p, p F p Domination: p F F, p T T Double negation: ( p) p Negation of disjunction (DeMorgan): (p q) p q Negation of conjunction (DeMorgan): (p q) p q Commutativity: p q q p, p q q p Associativity: (p q) r q (p r), (p q) r q (p r) Distributivity: (p q) r (q r) (q r), (p q) r q r q r Absorption: (p q) p p, (p q) p p
20 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac What equivalences involve implications? p q p q p q p q p q (p q) (p q) p q (p q) (p r) p (q r) (p q) (p r) p (q r) (p r) (q r) p q r (p r) (q r) p q r p q (p q) (q p) p q p q p q p q p q (p q) p q
21 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac Show that p q p q is a tautology We want to demonstrate that p q p q T –p q p q # equivalence law p q p q (p q) (p q) – (p q) p q # DeMorgan p q p q ( p q) (p q) –p q q p # commute p q p q p p q q –p p T # negation p q p q T T –p T T# domination p q p q T
22 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac What are the logical operators in Processing? Boolean variables may be combined using logical operators –!pmeans NOT p –p && qmeans p AND q (p q) –p || qmeans p OR q (p q) –p != qmeans p XOR q (p q, p ≠ q) –p == qmeans p iff q (p q) –p&& ! qmeans p AND NOT q (p q)
23 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac What are logical operators on bit strings A Boolean variable may be represented by 1 bit –1 means true, 0 means false A bit string S is an ordered sequence of bits The length of S is the number of bits it has We can perform operations on bit-strings S and T –By performing the operations on all pairs of corresponding bits S | T performs a bitwise OR (at least one bit has to be 1) S & T performs a bitwise AND (both have to be 1) S << 3shifts all bits left and inserts three 0 on the right In processing, these operations work on –intintegers –bytestrings of 8 bits –charbinary representations of characters See
24 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac Reading assignment Sections 1.1 and 1.2 for this class Sections 1.3 and 1.4 for the next class
25 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac Assigned Exercises for Lecture 1 You must know how to solve the following exercises for the quiz on lecture 2 – e on p 1-17 – f on p 1-18 – b on p 1-18 – on p 1-19 – d on p 1-19 – on p 1-20 –1.2-9 on p 1-26
26 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac Logical operators (summary) T, Ftrue, false ¬p, !p, p not p p qp and q p qp or q p q, p ≠ q p xor q p qp implies q p q, p==q p same as q p q, p qp equivalent to q nfor each n nfor at least one n empty set s Ss is an element of S S elements not in S S Telements in S and T S Telements in S or T S–TS T S T S T – S T S=TS equals T S TS included in T PropositionsSets S | T bitwise OR S & T bitwise AND S << nshift left by n bits Bit strings
27 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac Work to do before the next lecture (part 1) Get the book and study sections 1.1 and 1.2 Study these slides by looking at the question in the slide title and trying to answer it without looking –Mark what you do not understand and check it in the book Do the exercises assigned for sections 1.1 and 1.2 –Do other exercises as needed for practice Learn how to solve these exercises by heart –We will have a practice quiz during the next lecture –It will cover these slides, the reading, and the exercises
28 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac Work to do before the next lecture (part 2) Read Sections 1.3 and 1.4 –One question on the practice quiz will be on Sections 1.3 or 1.4 Print and study the slides (L02) for lecture 2 –See if you can answer the questions (slide titles) –Write down what you do not understand Try to do the exercises assigned in the slides for lecture 2 –Mark what you have trouble with Bring it all to lecture 2 As a Team (only due Tu Jan 17, but START NOW!!!) –Print the slides (P0) for project 0 –Follow the instructions and install Processing –Play with it, write your question, problems –Try to complete P0 (including posting a web page and doing your PPP)