Computing Fundamentals 1 Lecture 2 Lecturer: Patrick Browne http://www.comp.dit.ie/pbrowne/ Room K308 Based on Chapter 2. A Logical approach to Discrete Math By David Gries and Fred B. Schneider Logic • ‘Formal logic’ is not a phrase that attracts. Everybody, perhaps, would like to be logical — but not too logical, in case they become unfeeling like Star Trek’s Mr Spock... Hardly anybody wants to be formal.,. We’d rather be informal, casual, easy, and logical only up to a point. • But formal logic is hot stuff, because it is the machinery in the engine room of computing. Computers do very simple formal logical reasoning, and can’t do anything else. The programming languages that drive those computers are formal logics. The protocols that drive the internet and the grid are formal logics too. Computers do what they do as well as they do because we know quite a lot about formal logic, and they keep falling over because we don’t yet know quite enough. From: Proof and Disproof in Formal Logic by Richard Boolean Expressions • Boolean constants: true and false. • Boolean variables can only have values true or false. • Boolean operators: ,, , , , ˅, ˄. • Expressions are of type Boolean (or sort Bool in CafeOBJ, or type bool in Python). Truth table for negation () and identity (id) There are as many unary truth-functions as there are different ways to fill in a two row column in a truth-table. For unary connectives, there are 2^(2^1)=4, for binary connectives, 2^(2^2)=16, for ternary connectives, 2^(2^3), and so on) Unary Ops id not Argument true true true false false false true false true false Unary Op. Truth Table • The previous TT provides a complete enumeration of the Boolean operations with one argument (unary operators). The first and last operation results do not depend on the argument, they have no names. The identity (id) operation returns the argument, and negation or not operation () returns the constant that is not the argument. The truth table for Boolean operations with two arguments is shown in the next slide. Truth table for two argument ops Operations ˅ O R I M P L I E S = E Q U A L ˄ A N D N A N D = N O R Arg1 Arg2 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 A look at implication • • 1. 2. 3. 4. While the TTs for ˅ and ˄ are symmetric and fairly intuitive the TT for implication requires some discussion. All States True States "4 is odd 4 is even" !!! T premise implies a T conclusion, therefore T T is T; T premise cannot imply a F conclusion, therefore T F is F and 4. You can conclude anything from a false assumption, so F anything is T. The value of marked conclusions (some states of Q) doesn’t affect the value of entire proposition Implies() in terms of OR(˅) • implies: • e s • OR • e ˅ s • Use the on an line truth table generator to evaluate the above expressions. http://turner.faculty.swau.edu/mathematics/materialslibrary/truth/ 8 Implication with constants T/F and variables P/Q in P Q A look at implication • Logicians claim that the TT for implication is consistent. • P is stronger than Q, the implication is true only once when P is true, twice when Q is true • Q is weaker than P • All States True States Strong and Weak assertions • Definition: If P and Q are two assertions, then P is stronger than Q if P Q. If P is stronger than Q, then Q is weaker than P. • Example: i<0 (P) is stronger than i<1(Q) because i<0 i<1. Fewer states satisfy i<0 than satisfy i<1. • Stronger means more selective or specific. • Weaker means more frequent or general. Binary Op Truth Table(1) • The TT on slide 5 provide a complete enumeration of the Boolean operations with two arguments (binary operators). Eight of the infix ops have names. • b = c “b equals c” • b c “b differs c” (AKA exclusive or, XOR) • b c “b equivales c” or b and c are equivalent. The Boolean expression b c is evaluate exactly as b=c, except that can be used only when b and c are Boolean expressions (= can be used for arithmetic equality e.g. 2 = 1 + 1). • b c “the inequivalence of b and c”, again strictly for Boolean operands. Binary Op Truth Table(2) • b ˄ c conjunction or AND, &, or && • b ˅ c disjunction or OR, ||, + • bc implication. “b implies c”, “if b then c”. b is called the antecedent c is called the consequent • bc consequence. “b follows from c” b is called the consequent c is called the antecedent Binary Op Truth Table(2) • Alternative symbols for equivalence: “x if and only if y” iff <-> • The last symbol is used in the course text. The should not be confused with the CafeOBJ equality operation ==. • CafeOBJ’s BOOL module uses iff for equivalence. • Check the definition in CafeOBJ (note spaces): • open BOOL . • show op (_ iff _) Evaluating Truth Tables • In addition to defining Boolean operators TT can be used to compute the value of a Boolean expression in every state. They offer a decision procedure. • The TT on the following slide gives the value of : p ˅ (q ˄ r) • The precedence of operators are described at: http://www.cs.cornell.edu/Info/People/gries/Logic/Prec.html • A precedence specifies the order of evaluation of various operations (lower the number the higher the precedence) • Note the precedence rules may differ in CafeOBJ. This can be check with the show op or show module command. Table of Precedence from A Logical approach to Discrete Math By David Gries and Fred B. Schneider Because of the higher precedence of * over + in mathematics 2 + 5 * 3 = 17 (and not 21) . Most programming languages use precedence levels that conform to the order commonly used in mathematics for arithmetical operations, comparisons, Boolean operations. Evaluating Truth Tables p q r t t t t f f f f t t f f t t f f t f t f t f t f r f t f t f t f t q ˄ r p f t f f f t f f t t t t f t f f ˅ (q ˄ r) Truth Table Generators • b ( b ˅ c) • b (b ˅ c) • http://turner.faculty.swau.edu/mathematics/materialslibrary/truth/ Tautologies, satisfiable, contradiction Truth Table Example • Is b (b ˅ c) a tautology? True in all cases. • One solution is to draw TT of • b (b ˅ c) b c b (b ˅ c) b (b ˅ c) t t f t t t f f t t f t t t t f f t f f Table Example Is b ( b ˅ c) satisfiable? True in at least one case. •b • • • • t t f f c t f t f b ( b ˅ c) (b ( b ˅ c)) f f t t t t t f f f t f Equality versus Equivalence • The Boolean expression a b is evaluated exactly the same as a = b, except that can only be used when b and c are Boolean expressions. • They each have different precedence indicating which operation is done 1st , 2nd , 3rd , etc.: = (arithmetic) higher precedence (done before ) (Boolean) lower precedence (done last) • Conjunctional and Associative: – Conjunctional: b = c < d abbreviation for (b = c) ˄ (c < d) – Associative: a b c, (a b) c, or a (b c) Satisfiability and validity • A Boolean expression P is satisfied in a state if its value is true in that state; P is satisfiable if there is a state in which it satisfied; and P is valid if it satisfied in every state. A valid Boolean expression is called a tautology. • If P cannot be satisfied (e.g. P= (A ˄ A)) then P is always false for all values of A. If P is unsatisfiable it is called a contradiction. • A Boolean expression that is neither a tautology nor a contradiction is called a contingency or satisfiable . Boolean Expressions and Propositions • Boolean expressions are constructed from the constants true and false, Boolean variables, which can be associated (only) with the values true and false , and the Boolean operators such as (, , , ˅, ˄). The constants true and false are often called Boolean values, and a Boolean expression is often said to be of type Boolean. • A proposition is a statement that can be interpreted as being either true or false. Propositions can be translated into a Boolean expressions. Boolean Expressions and Propositions Proposition • It is raining and 5 is greater than 3. • It is raining or 5 is greater than 3. Boolean Expression • r ˄ g • r ˅ g • It is not raining. • r • Whether or not it is raining, I am going swimming. • (r ˅ r) s A Boolean variable that can denote a proposition is sometimes called a propositional variable. The operator greater than (<) is a predicate (covered in Later). Predicates are not directly representable as propositions. Greater than can be regarded as a function of two numerical arguments and a Boolean result. Propositional Calculus • Propositional calculus is a method of calculating with Boolean expressions that involve propositional variables. Gries and Schneider call their propositional calculus equational logic E. Satisfiable and Valid1 • • • • The value of a Boolean expression is its state which can be either true or false. State is a list of variables with associated values. pq is satisfiable because it is satisfied if (p, true) is in state S1. It is not valid in the state S2: (p,false), (q,false). pp p is valid in every state. A formula is valid if and only if it is true under every interpretation (or state). Modelling English propositions • A proposition can be interpreted as being either true or false. For example: • “Henry VIII had one son and Cleopatra had two” • We wish to translate English propositions to Boolean expressions because: – English is ambiguous, computers require logical clarity. – We can automate, analyse, reason about, simplify, and prove properties of Boolean expressions. – Rules of logic provide effective computer based way to simulate human reasoning (in English). Modelling English propositions • Issues involving inclusive vs. exclusive or are encountered when translating natural language to symbolic logic. We need to decide which "or" the natural language means. • Example: 1. I worked hard or I played the piano. 2. If I worked hard, then I will get a bonus. 3. I did not get a bonus. Converting English • Lets use: R: its raining, S: I’m going swimming. • Whether or not it is raining, I am going swimming: (R ˅ R) S • If it is raining then I will not go swimming: R ¬S • I will go swimming only if it is not raining: S ¬R (see later) Converting English • If we write p -> q for the sentence “We will help you(p) only if you help us(q) ” • This is false only if “We help you”(p) is true, but “you help us”(q) is false. Logic and reality • Two ways to say the same thing? – To stay dry it is sufficient to have an umbrella. – If you have an umbrella, then you will stay dry: hu sd • We will assume common sense meanings and avoid complex interpretation issues. Coventry and Garrod1 Modelling English propositions • Let Boolean (or propositional) variable p denote the proposition: • p : Henry VIII had one son and Cleopatra had two. • The proposition p contains two subpropositions: • x: Henry VIII had one son. • y: Cleopatra had two (sons). •x ˄ y Modelling English propositions • Negated Disjunction: English sentences containing neither and nor are treated as negated disjunction (i.e. a negative version of English or). • Example: [S1] John neither washed the car nor went to the cinema. • Let W = washed car, C = went to cinema. • S1 can be written as ¬(W ˅ C) . Translating into a Boolean Expression • 1. Introduce a Boolean variable to denote sub-propositions. • 2. Replace these sub-propositions by their corresponding Boolean variables. • 3. Translate the result of step 2 into a Boolean expression, using obvious translations of English words into operators. See table on next slide. Translation of English Words and, but ˄ or ˅ not it is not the case that if p then q p q Translation Boolean variable English Sentence x: Henry VIII has one son y: Cleopatra had two sons z: I’ll eat my hat w: 1 is prime Translation Proposition translation Henry VIII has one son or I’ll eat my hat x ˅ z Henry VIII has one son and 1 is not prime x ˄ w If 1 is prime and Cleopatra had two sons, then I’ll eat my hat w ˄ y z Translating Or • We must distinguish between: • “inclusive or” (or, ˅) • “exclusive or” (xor, b c, b c ) – sometimes written ‘||’ or ⊕ • • • • Would you like coffee or tea? Would you like sugar or milk? Is it raining or not raining? Yes! Island of Liars and Truth tellers? Translating Can you express the above statement using just one logical connective? This example is from Logic Made Easy by Deborah J. Bennett Implication • “If a then b” becomes ab • “if you don’t eat your spinach I will scold you.” becomes: • es sy • In English the following have same meaning: – “if you don’t eat your spinach then I will scold you.” – “eat your spinach or I will scold you.” Implication • Two logically equivalent propositions: • e s • e ˅ s Contrapositive • Contrapositive: • e s • s e Implies in terms of disjunction • implies: • e s • OR • e ˅ s • Use the on line truth table generator to evaluate the expression in this lecture. http://turner.faculty.swau.edu/math ematics/materialslibrary/truth/ 44 Implication • In English the antecedent and the consequence can be switched, and the then dropped: • If you pay me then I will go. • I will go if you give me money. • There is a subtle difference between • ‘If’ meaning ‘in case’ or ‘in the event of’ • ‘If’ meaning ‘when’ Implication • There is a subtle difference between – ‘If’ meaning ‘in case’ or ‘in the event of’ – ‘If’ meaning ‘when’ • “When the sun is shining, I feel happy” • Also, in logic a consequential relation is not necessary. • If I drink too much, then I get sick. • If pigs can fly, then 2+2=5. • Logic usually needs a sensible interpretation to be useful. Implication And Equivalence • The English “if” should be sometimes regarded as an equivalence. • “If two sides of a triangle are equal, the triangle is isosceles.” • Which as a definition could be stated: • t : two sides of the triangle are equal • Is : the triangle is isosceles. • t is Necessity • A necessary condition for the occurrence of a specified event is a circumstance in whose absence the event cannot occur. Oxygen is necessary for combustion or oxygen is necessary for human life. A necessary implication can be used to represent sub-set-of relation (AKA subsumption or “is kind of” or “is-a”). • Example being a student implies being a person. • Student Û Person All members of B are members of A • Does “B is a kind of A” mean “All Bs are As”? • All members of a subclass can be inferred to be members of its superclass. • Given an object of type A it cannot be inferred that it is a B. • So B implies A but not visa versa. Question • On Computing Fundamentals 1 it is necessary to pass the exam to pass the module. • What is sufficient to pass the module? Sufficient • A sufficient condition for the occurrence of an event is a circumstance in whose presence the event must occur. • If a conditional statement (aÛb) is true then its antecedent is said to provide a sufficient condition for its consequent. • In contrast the consequent is the necessary condition of antecedent. Necessity & Sufficiency • Two ways to say the same thing? – To stay dry it is sufficient to wear a raincoat. – If you wear a rain coat, then you will stay dry. • wr sd Necessity & Sufficiency(2) • Two ways to say the same thing? – To stay dry it is necessary to wear a raincoat. – You will stay dry only if you wear a raincoat. • sd wr Necessity & Sufficiency(3) • “x is sufficient for y” means •x y • “x is necessary for y” means •y x • “x is necessary and sufficient for y” means • (x y) ˄ (y x) • Note: care is needed when ‘only’ is used. Necessity & Sufficiency(3) • "If X then Y", or "X is enough for Y", can also be understood as saying that X is a sufficient condition for Y. • Bob eats dessert if it is apple pie. – (If it‟s apple pie, then Bob will eat it, AÛB) • Bob eats dessert only if it is apple pie. – (If Bob eats dessert, then it must be apple pie; BÛA) Sequences of equivalences • Operator ≡ is associative, which means that b ≡ c ≡ d, (b ≡ c) ≡ d, and b ≡ (c ≡ d) are all equivalent. The Boolean expression • P0 ≡ Pi ≡ ... ≡ Pn • is true exactly when an even number of the Pi are false. Sequences of equivalences • Without any additional formal manipulation it can be determined that false ≡ false ≡ false ≡ true is false, because an odd number of its equivalents are false. The following is true. false ≡ false ≡ false ≡ false ≡ true Bob’s Eyesight • Logical equivalence () is associative. • Bob can use no eyes • Bob can use one eye • Bob can use two eyes • Generalizes to “Bob can use j eyes” • Bob0 Bob1 Bob2 • Is this always true? • Draw truth table. • What if we say “exactly n eyes” Equality (=) and Equivalence(≡) • Note that equality = can be used with or non-Boolean data (e.g. integers) and is conjunctional rather than associative. Using equivalance • None or both of p and q is true. • ¬p ≡ ¬q ˅ p ≡ q Examples Examples • Whether or not it is raining, I am going swimming. A logical equivalence Examples(*) The English use of Unless1 • ‘Unless’ generally means ‘or’. ‘I will go out unless it rains’ translates to ‘I will go out ˅ it will rain’. (Note the extra ‘will’.) • You could also use ‘¬(it will rain) I will go out’. • But you may think that ‘I will go out unless it rains’ implies that if it does rain then I won’t go out. This is a stronger reading of ‘unless’. If you take that view, you’d translate it as ‘I will go out ⇔ ¬(it will rain)’. This is a bit like ‘exclusive or’. • Deciding whether to take the strong or weak reading is done by individual context, and can be impossible: English is not always precise. You must decide what the correct logical translation is. The English use of Unless • English is sometimes difficult to translate: • “I will go out unless it rains” could be ¬(rains) I will go out or ¬(rains) ⇔ I will go out or (rains) ˅ I will go out The English use of But1 • ‘But’ can mean ‘and’. • ‘I will go out, but it is raining’ translates to • ‘I will go out ˄ it is raining’. Translating • If the bill was sent, then you will be paid and the bill was sent. • B = Bill was sent • P = You will be paid • (B->P) ˄ B Translating • If Mary is the author of the book, then the book is fiction, but the book is non-fiction implies that Mary did not write the book. • M = Mary is Author • F = Book is Fiction • Translating Translating from English Example Translating from English Example • Premise Indicators – For – Since – Because – Assuming that Translating from English Example • Conclusion Indicators – Therefore – Thus – Hence – So Expressions in CafeOBJ • These convert to in the BOOL module of CafeOBJ. • red not(b) implies (b or c) . • red not(b) iff (b or c) . Prove the following Prove the following Prove the following Prove the following Expressions in TT and CafeOBJ What results does CafeOBJ give for the following? open BOOL . -- set trace whole on . vars a b c : Bool . red not(b) iff (b or c) . red not(b) iff b or c . red not(b) implies (b or c) . red not(b) or c iff b implies c . In some case we get true in others we get a term. Monoticity What results does CafeOBJ give for the following? open BOOL . vars p q r : Bool . red (p implies q) implies ((p or r) implies (q or r)) . red (p implies q) implies ((p and r) implies (q and r)) . red (p implies q) implies ((r implies p) implies (r implies q)) . Superman Example • If Superman were able and willing to prevent evil, he would do so. If Superman were unable to prevent evil, he would be ineffective; if he were unwilling to prevent evil, he would be malevolent. Superman does not prevent evil. If Superman exists, he is neither ineffective nor malevolent. Therefore Superman does not exist. Superman Example a: Superman is able to prevent evil. w: Superman is willing to prevent evil. i: Superman is ineffective. m : Superman is malevolent. p: Superman prevents evil. e: Superman exists. 82 Superman Example 83 Superman Proof: Gries & Schnieder • Proof using G&S E logic to manipulate Boolean Expressions. 84 Superman Proof using CafeOBJ Propositional Logic. mod! SUPERMAN { ops a w i m p e : -> Bool ops aw ai nw pe ee : -> Bool eq [F0] : aw = (a and w) implies p . eq [F1a] : ai = not a implies i . eq [F1b] : nw = not w implies m . eq [F2] : pe = not p . eq [F3] : ee = e implies (not i) and (not m) . } open SUPERMAN . red (aw and ai and nw and pe and ee) implies (not e) This returns true using CafeOBJ’s equational logic that includes propositional logic. Superman Proof using CafeOBJ Theorem Prover Propositional Logic. mod! SUPERMAN { -- the basic facts ops a w i m p e : -> Bool ax [F0] : (a & w) -> p . ax [F1] : (~(a) -> i) & (~(w) -> m) . ax [F2] : ~(p) . ax [F3] : (e -> (~(i) & ~(m))) . } This returns true using CafeOBJ’s equational logic that includes propositional logic. Superman Proof truth table. ******How Many rows in TT? ********* Alternative Formalization in CafeOBJ This is the notation used by the CafeOBJ theorem prover. mod! SUPERMAN { -- the basic facts ops a w i m p e : -> Bool ax [F0] : (a & w) -> p . ax [F1] : (~(a) -> i) & (~(w) -> m) . ax [F2] : ~(p) . ax [F3] : (e -> (~(i) & ~(m))) . } 88 CafeOBJ set up for proof • open PORTIA1 • -- The goal is to prove that the portrait is in the gold casket. • option reset . • goal(gc) . -- We are only interested goal line • flag(auto, on) . • flag(very-verbose,on) . • flag (universal-symmetry,on) • param(max-proofs, 1) . • resolve . • close 89 Alternative Proof in CafeOBJ • • The expected output is ** PROOF ________________________________ • 1:[] ~(a) | ~(w) | p • 2:[] a | i • 3:[] m | w • 4:[] ~(p) • 5:[] ~(e) | ~(i) • 6:[] ~(e) | ~(m) • 7:[] e • 15:[hyper:7,5,2] a • 16:[hyper:3,1,15] m | p • 17:[hyper:16,4] m • 18:[hyper:17,6,7] • ** ______________________________________ 90 Work or Play? • Write the following statements in propositional logic: • I worked hard or I played the piano. • If I worked hard, then I will get a bonus. • I did not get a bonus. • (W ˅ P) ˄ (W ⇒ B) ˄ ¬(B) Work or Play? • Construct a TT using: • http://turner.faculty.swau.edu/mathematics/materialslibrary/truth/ • This expression is only true in one case. Work or Play? • Here are the intermediate truth tables for (W ˅ P) ˄ (W ⇒ B) ˄ ¬(B) Overall argument Work or Play? • The intermediate truth tables for (W ˅ P) ˄ (W ⇒ B) ˄ ¬(B) . • The following are useful transformations: ¬(¬(w)) = w. Expressing or as ⇒ The contrapositive rule Work or Play? • • • • • • Given I worked hard or I played the piano. If I worked hard, then I will get a bonus. I did not get a bonus. Can we conclude : I played the piano. In logical notation is the following a valid argument? • (W ˅ P) ˄ (W ⇒ B) ˄ ¬(B) Therefore or • P ∴ Work or Play? • As propositions in CafeOBJ mod WORK { ops w p b : -> Bool ops e1 e2 e3 : -> Bool eq [e1] : e1 = w or p . eq [e2] : e2 = w implies b . eq [e3] : e3 = not b . } open WORK . red (e1 and e2 and e3) implies p . Valid Argument • The following is a valid argument – [P1] If Pats’s program is less than 10 lines long then it is correct. – [P2] Pat’s program is not correct. – [C1] Therefore Pat’s program is more than 10 lines long. • The first two lines are the premisses and the last the conclusion. Recall the contrapositive rule (P ⇒ C) (¬P ⇒ ¬L) Invalid argument • The following is an invalid argument – [P1] If I am wealthy then I give away lots of money – [P2] I give away lots of money – [C1] Therefore I am wealthy • The reasoning is not valid because from the premisses you cannot derive the conclusion. (W⇒M) (M⇒W) Valid argument: Modus Ponens • An example a very general valid argument is Modus Ponens (from Latin mode that affirms) Turnstile |= • In propositional logic the double turnstile is symbol that is placed between propositions and the conclusion. • Sequent expressions assert that an entailment relation holds between the set of propositions on the left and the conclusion on right-hand sides of the double turnstile. • A |= B can be read “A entails B” • There is no interpretation which makes A true and B false. As a specification we could say A satisfies B. Single Turnstile |• The intuitive meaning of A |- B is that under the assumption of A the conclusion of B is provable. It denotes syntactic consequence • Soundness: A|-B ⇒ A|=B • Completeness: A|=B ⇒ A|-B • Soundness prevents proving things that aren't true when we interpret them. • Completeness means that everything we know to be true on interpretation, we must be able to prove. Double turnstile |= in relation to P1, ..., Pn |= Z iff |= (P1 ˄ … ˄ Pn) Z • Recall the work, piano, bonus example. (w˅p) ,(w b) ,(~b) |= p iff |= (w˅p) ˄(w b) ˄(~b) p Difference between valid arguments and implies. • An argument is valid iff it is necessary that under all interpretations (valuations in propositional logic), in which the premises are true the conclusion is true as well: P1,...,Pn |= Z • P1,...,Pn |= Z if and only if the statement of the form P1 and ... and Pn implies Z is necessarily true (a tautology): |= (P1 & … & Pn) Z 103 Model |= Theory • A formula is satisfiable if it is possible to find an interpretation (model) that makes the formula true. • A |= B read “A satisfies B”. • A valid argument is one in which , if the premises are true, then the conclusion must be true. Building And-Or in CafeOBJ -- No variables -- We turn of implicit import of built-in BOOL . set include BOOL off -- This allows us to develop our own Boolean operations mod* BOOLEAN1 { signature { [ Boolean ] op false : -> Boolean op true : -> Boolean op -_ : Boolean -> Boolean op _\/_ : Boolean Boolean -> Boolean op _/\_ : Boolean Boolean -> Boolean } axioms { eq - false = true . eq - true = false . eq false \/ false = false . eq false \/ true = true . eq true \/ false = true . eq true \/ true = true . }} eof -- Check these reductions? open BOOLEAN1 var A : Boolean red true \/ false . red true \/ A . red A \/ true . books in a library