Logic and Truth Tables These notes are background reading for CP1068 Lecture 3 and should be worked through prior to the lecture. The lecture will not cover "implies" or "if and only if", but these concepts are included in these notes for completeness. You should concentrate on making sure you understand "and", "or", "not" and truth tables. In programming we often want the computer to carry out a set of instructions only when various conditions are true. These conditions may be quite complicated, as in “If (Age is less than 60 and Queue is not full or Bribe is greater than 2000) then (carry out instructions)” In order to avoid mistakes in the logic when dealing with conditions like this we need to study the uses of propositions. A proposition is a statement that is either true or false. Propositions can be found in many situations, e.g. today is Wednesday, x < 12, there is a current flowing in that wire. The basic operators described below apply in all of the contexts in which we have things whose values can be true or false. The set of rules governing propositions and their operators is often called Boolean algebra after George Boole (1815-1864) the mathematician who first proposed them, long before there was a practical application in computing. Boolean values in computing are named in his honour and can only take the value true or false. We usually denote simple propositions by letters p, q, r, ..... and form compound ones by means of operators. There are several operators for propositions. These take one or two propositions and produce a new proposition from it. Some are normal English words they are used in the same sense as in English. NOT ¬p "not p" AND p q "p and q" OR p q "p or q" (the "inclusive" or - allowing both to be true) IMPLIES p q "p implies q" or "If p then q" IF & ONLY IF p q "p if and only if q". These are formally defined using truth tables where each possible combination of truth values, T = TRUE or F = FALSE is considered : p ¬p F T p T T F F q T F T F p q T F F F p q T T T F p q T F T T p q T F F T Example Let A be the proposition 'the door is red' and B be the proposition 'the door is made of wood'. Then ¬A is the proposition 'the door is not red' A B is the proposition ' the door is a red wooden one', 1 A A A B is the proposition 'the door is red or it is made of wood, or both' B is the proposition 'if the door is red then it is made of wood' B is 'the door is red if and only if it is made of wood'. Eliminating Implies We saw above that A B is the proposition 'if the door is red then it is made of wood.' We can see that 'if the door is red then it is made of wood', is the same as saying at least one of 'the door isn't red' or 'it is made of wood' must be true. i.e. ¬A B is true This result A B = ¬A B is a general one and can always be used to write IMPLIES in terms of NOT and OR We can formally prove the result using a truth table. In a truth table we produce a row for all possible combinations of true and false of the simple propositions involved. N.B. If there are n simple propositions then there will be 2n rows in the truth table. We then show that the two expressions that we are trying to show are equivalent have the same result in each row. Using a truth table to show that A B = ¬A B. We have 2 simple propositions, A and 2 B, so there will be 2 = 4 rows: A B A B ¬A ¬A B T T T F T T F F F F F T T T T F F T T T The first two columns give all four possible combinations for A and B. The third and fifth columns have the same value in each row, proving that A B and ¬A B are the same (i.e. they are logically equivalent). Tautologies and Contradictions A proposition that is always true (such as p ¬p) is called a tautology and one that is always false (such as p ¬p) is called a contradiction. Exercises 1. Using a truth table show that (¬ q (p q)) ¬ p is a tautology. 2. Is p (q r) logically equivalent to (p q) r? Simplifying propositions There are many rules, which enable us to simplify propositions. These are stated below. Basic rules: This first set are all things you would probably assume to be true anyway. e.g. p ¬p = true is saying that one of p and ¬p must always be true. ¬¬p=p p ¬p = true p ¬p = false p p=p p true = true p false = p p p=p p true = p p false = false p p = true 2 For and it doesn’t matter which way round you write the operands - we say they are commutative. Note that is not commutative. p q=q p p q=q p We have already met: Elimination of implies: p q = ¬p q De Morgan's Laws ¬ (p q) = ¬p ¬ q ¬ (p q) = ¬p ¬q These two laws are of particular importance when constructing compound propositions as conditions in a program. Suppose you only want to carry out certain instructions when someone’s age is not between 18 and 65. Now “age between 18 and 65” may have been (pseudo)coded as “Age is greater than 18 AND Age is less than 65” We want to negate this. So we could simply use “NOT(Age is greater than 18 AND Age is less than 65)” But if we mistakenly modify this to “Age is NOT greater than 18 AND Age is NOT less than 65” we get an impossible condition (no-one is both under 18 and over 65!). The first of De Morgan’s laws above shows that the correct modification should be “Age is NOT greater than 18 OR Age is NOT less than 65” This particular mistake is a very common source of logic errors in programs. Associative Laws p (q r) = (p q) r = p q r p (q r) = (p q) r = p q r We use these to tell us that, with and , if we have two of the same operation we can do them in any order - again this is stating the obvious. Distributive Laws p (q r) = (p q) (p r) p (q r) = (p q) (p r) These tell us that, if we have both an and an to do we can do the ‘outer’ one first, to each thing in the bracket, then do the ‘inner’ one. They work both ways (unlike + and *) so you don’t have to remember which way it works! Implication Law p q = ¬q ¬p This says that “if p is true then q is true” is the same as saying “if q isn’t true then p isn’t true”. Note that it is not the same as saying “if q is true then p is true”. Forming propositions We need to translate English statements into propositions, necessitating an understanding of English as well as an understanding of the logical operators , , ¬, , . There are a few ' rules', but since English, especially colloquial English, is not very logical or consistent the ' rules' are not guaranteed. We use brackets liberally where necessary. 3 Guidelines: AND - usually . (But, 'I go swimming every Tuesday and every Wednesday' means 'I go swimming if the day of the week is Tuesday or Wednesday' .) OR - usually , occasionally the exclusive or is meant. (But, ' English is not very logical or consistent' means ' English is not very logical' ' English is not very consistent'. You may recognise De Morgan's law at work here.) BUT - usually . (“I like football but I can’t stand rugby”) IF … THEN, IF, WHENEVER, EVERY - usually . - but always note carefully which way round the implication goes. IF AND ONLY IF, IF ... BUT NOT OTHERWISE - always Examples Let: R = It is raining W = The pavement is wet S = The sun is shining C = There are clouds in the sky Then we have: It is raining and the pavement is wet. R W If it is raining then the pavement is wet. R W The pavement is wet but it is not raining. W ¬R Either the sun is shining or there are clouds in the sky. S C If it is raining then the sun is not shining and there are clouds in the sky. The sun is shining whenever it is not raining. ¬R S R ( ¬S C) Exercises 3. With the propositions as above express these sentences as compound propositions: (a) There are no clouds in the sky and the sun is shining. (b). The sun is shining but it is raining (c). If the sun is shining and it is not raining then the pavement is not wet. (d) The sun is not shining whenever it is raining (e). If there are clouds in the sky or it is raining then the sun is not shining. Some applications in computing. Java (and similar languages) Java (like all high level languages) has operators that correspond to , and ¬. These are &&, || and ! respectively. Expert Systems These are built up of rules such as 'If p then q’, ( or p q ) meaning q is deduced to be true if it is known that p is true. Forward Chaining uses a rule called Modus Ponens (p ( p q)) q to deduce new propositions from the known ones. Backward Chaining uses a rule called Modus Tollens (¬q ( p q)) ¬p to eliminate a proposition (prove it false) by checking others. Z (and other formal specification notations) Z is a formal specification language that is based upon set theory and propositional calculus. Formal specification languages are often used at the customer requirements stage of the software development process to help the programmer (and the customer!) understand exactly what the required program is to do in all circumstances. 4 Program Proving There is a body of knowledge about proving programs to be correct. Attempts are being made to automate part of this process. If tool support becomes available then these techniques, which rely heavily on propositional calculus, may become more widespread. Solutions to Exercises P Q ¬Q P Q ¬Q (P Q) ¬P (¬Q (P Q)) ¬P T T F T F F T T F T F F F T F T F T F T T F F T T T T T Since the expression evaluates to true for all possible values of P and Q it is a tautology 1. 2. p q r p q (p q) r q r p (q r) T T T T T T T T F T T F F T T T T F T F T T F T F F F T T T F T T T T F F F T F F T F F F F T F F F F F F F F F The two bold columns are not the same so the propositions are not logically equivalent – i.e. the bracketing is necessary to avoid ambiguity. 3. (a) ¬C S (b) S R (c) (S ¬R ) ¬W (d) R ¬S (e). (C R) ¬S 5
© Copyright 2026 Paperzz