CSC 173 Tues. Nov 19, 2002 ======================================= Read chapters 12 and 13 Some review from Math 150 ======================================================================== Propositional Logic Propositional logic is a mathematical model (or algebra) for reasoning about the truth of logical expressions (propositions). Logical expressions can be manipulated according to algebraic laws, allowing us to reason formally (using deductive reasoning) about a set of premises. Resolution is one particularly important technique that can be used to prove a hypothesis from a set of premises. Propositional logic can be used in: * the composition of logical expressions in programs * automatic reasoning systems * deductive proofs of program correctness * the programming language Prolog, as a model of computation * the design of digital circuits Although propositional logic is powerful enough to be used in these many different contexts, like any formal system, there are limits to its applicability. ------------------------------------------------------------------------ Logical Expressions We can define logical expressions using a recursive definition: 1. Propositional variables (whose value is TRUE or FALSE) and the propositional constants TRUE and FALSE are logical expressions. 2. If LE1 and LE2 are logical expressions, then LE1 AND LE2 is a logical expression, whose value is TRUE if both LE1 and LE2 have the value TRUE, and is FALSE otherwise. 3. If LE1 and LE2 are logical expressions, then LE1 OR LE2 is a logical expression, whose value is TRUE if either LE1 or LE2 have the value TRUE, and is FALSE otherwise. 4. If LE1 is a logical expression, then NOT LE1 is a logical expression, whose value is TRUE is LE1 has the value FALSE, and is FALSE otherwise. The precedence levels of logical operators is: 1. NOT \not ~ ! 2. AND \wedge & && 3. OR \vee | || As usual, we use parentheses to override precedence. By assigning values to the variables in a logical expression, we also assign a value to the expression itself. Example: given the expression "(p AND q) OR r", if p=TRUE, q=TRUE, and r=FALSE, then the value of the expression is TRUE. ------------------------------------------------------------------------ Boolean Functions and Truth Tables The meaning (or value) of a logical expression is a Boolean function from the set of possible assignments of truth values for the variables in the expression to the values {TRUE,FALSE}. Example: given the expression "(p AND q) OR r", we can describe the Boolean function that determines the value of the expression by considering all combinations of value assignments for p, q, and r. p q r (p AND q) OR r ------------------------------ T T T T T T F T T F T T T F F F F T T T F T F F F F T T F F F F The above table describing the Boolean function "(p AND q) OR r" is called a truth table. In a truth table, there is a column for each variable in the expression, and each row in the table corresponds to an assignment of values to variables. The final column gives the value of the expression for the particular set of variable assignments given in the row. We can define the basic operators in terms of a truth table as follows (where we use 1 and 0 instead of TRUE and FALSE): p q p AND q p OR q NOT p ------------------------------------ 0 0 0 0 1 0 1 0 1 1 1 0 0 1 0 1 1 1 1 0 Note that a truth table for a function of N variables has 2^N rows. ------------------------------------------------------------------------ Other Functions of Two Variables We have seen a few functions of two variables, but there are many more. In fact, there are 16 different Boolean functions of two variables. Why 16? Because each function corresponds to a different assignment of values to the last column of a truth table with four rows, and there are 16 different such assignments. In general, there are 2^2^N different Boolean functions of N variables. Here are some additional functions of two variables that are frequently used: p q p->q p==q p NAND q p NOR q ------------------------------------------- 0 0 1 1 1 1 0 1 1 0 1 0 1 0 0 0 1 0 1 1 1 1 0 0 Comments on these functions: * Implication: "p -> q" or "p implies q" means that whenever p is TRUE, so is q. The only way "p -> q" can be FALSE, is if p is TRUE, and q is FALSE. p->q == ~p | q * Equivalence: "p == q" means that p and q have the same value. * NAND: "p NAND q" is the same as "NOT (p AND q)". * NOR: "p NOR q" is the same as "NOT (p OR q)". * NAND and NOR have the very nice property that they are easily implemented by simple electronic circuits (take ECE to learn how). They are also *complete* (see below): you can build any Boolean function out of NAND or NOR. * Equivalence is generally assumed to have lowest precedence. We can add NAND and NOR to the hierarchy, but that gets confusing enough that your safest just sticking in the parentheses. ------------------------------------------------------------------------ Computing Expressions with Truth Tables We can compute the value of an expression using truth tables. We create a table for all possible values of the variables, and all subexpressions in the expression. Compute the value of the following expression E for all possible truth assignments: (p -> q) -> (q -> r) p q r p->q q->r E --------------------------- 0 0 0 1 1 1 0 0 1 1 1 1 0 1 0 1 0 0 0 1 1 1 1 1 1 0 0 0 1 1 1 0 1 0 1 1 1 1 0 1 0 0 1 1 1 1 1 1 Note that the column for the value of q->r is the same as the column for the value of the entire expression E. We've established the equivalence of those two columns, which means: (p -> q) -> (q -> r) == q->r. If we draw an expression tree for E, we build truth table columns from the leaves up. ------------------------------------------------------------------------ Generating Expressions from Functions We are often given a Boolean function in truth table form and must derive a corresponding logical expression. For example, digital circuits are constructed from digital elements that compute the basic Boolean operations (such as NOT and NAND). Given a specific function expressed as the results of variable assignment (such as a function that adds two bits together and produces a sum and a carry bit), expressed in truth table form, we'd like the corresponding logical expression so that we can construct a circuit for the function. We can construct such a logical expression directly from the truth table for the function. The resulting expression uses only AND, OR, and NOT as operators. ------------------------------------------------------------------------ Example: Exclusive Or Expression from Truth Table Exclusive or (XOR) is another well-known function of two variables. The truth table for XOR is: p q XOR --------------- 0 0 0 0 1 1 1 0 1 1 1 0 We can construct an expression for XOR in terms of AND, OR, and NOT, using the following reasoning: * The second row tells us that p XOR q is TRUE when p is FALSE and q is TRUE. In other words, p XOR q is TRUE if NOT p AND q is TRUE. * The third row tells us that p XOR q is TRUE when p is TRUE and q is FALSE. In other words, p XOR q is TRUE if p AND NOT q is TRUE. * The other rows tell us that p XOR q is FALSE in all other cases. * So p XOR q is TRUE if NOT p AND q is TRUE, or if p AND NOT q is true. From the above, we determine that the following is a logical expression for the function p XOR q: (NOT p AND q) OR (p AND NOT q) ------------------------------------------------------------------------ Conjunctive and Disjunctive Normal Forms A Boolean expression is in disjunctive normal form if it is expressed as the sum (OR) of products (AND). That is, a Boolean expression B is in disjunctive normal form if it is written as: A1 OR A2 OR A3 OR ... An where each Ai is expressed as T1 AND T2 AND ... AND Tm where each Ti is either a simple variable, or the negation (NOT) of a simple variable. Each of the terms Ai is called a minterm. A Boolean expression is in conjunctive normal form if it is expressed as the product (AND) of sums (OR). That is, a Boolean expression B is in conjunctive normal form if it is written as: O1 AND O2 AND O3 AND ... On where each Oi is expressed as T1 OR T2 OR ... OR Tm where each Ti is either a simple variable, or the negation (NOT) of a simple variable. Each of the terms Oi is called a maxterm. ------------------------------------------------------------------------ Normal Forms in Truth Tables Conjunctive and disjunctive normal forms are duals of each other; either one may be used to generate a logical expression from a truth table. With these normal forms, we can be more precise about how to generate a logical expression from a truth table. To construct a logical expression in disjunctive normal form from a truth table: * Build a minterm for each row of the table where the function is true. * For each variable whose value is 1 in that row, we include the variable in the minterm; if a variable is 0 in that row, we include the negation of the variable in the minterm. * The expression consists of the OR of all the minterms. We could, of course, construct an expression in conjunctive normal form by using maxterms and AND. (One term for every *false* row, each of which says how to prevent that row.) ------------------------------------------------------------------------ A Logical Expression for an Adder A binary integer adder can be constructed from a series of one-bit adders. A one-bit adder takes two one-bit operands (x and y) and a carry bit from the previous one-bit adder (ci), and produces the sum of those bits, and a carry-out bit (co). We can define a Boolean function corresponding to the adder in the form of a truth table. We can then construct logical expressions for s and co, from which we could build a circuit. x y ci co s --------------------- 0 0 0 0 0 0 0 1 0 1 s : NOT x AND NOT y AND ci 0 1 0 0 1 s : NOT x AND y AND NOT ci 0 1 1 1 0 co: NOT x AND y AND ci 1 0 0 0 1 s : x AND NOT y AND NOT ci 1 0 1 1 0 co: x AND NOT y AND ci 1 1 0 1 0 co: x AND y AND NOT ci 1 1 1 1 1 co,s: x AND y AND ci The logical expression for s is: (NOT x AND y AND ci) OR (x AND NOT y AND ci) OR (x AND y AND NOT ci) OR (x AND y AND ci) The logical expression for co is: (NOT x AND NOT y AND ci) OR (NOT x AND y AND NOT ci) OR (x AND NOT y AND NOT ci) OR (x AND y AND ci) ------------------------------------------------------------------------ Completeness of Boolean Operators The previous construction of a logical expression from a truth table showed how to represent any Boolean function as a sum of products or product of sums. Since we can represent any Boolean function using AND, OR, and NOT, these operators form a complete set for Boolean functions. We can show that the NAND operator alone is sufficient to generate every Boolean function, by showing how to implement AND, OR, and NOT in terms of NAND. p q p NAND 1 q NAND 1 p NAND q E1 E2 -------------------------------------------- 0 0 1 1 1 0 0 0 1 1 0 1 0 1 1 0 0 1 1 0 1 1 1 0 0 0 1 1 E1: p AND q == ((p NAND q) NAND TRUE) E2: p OR q == ((p NAND TRUE) NAND (q NAND TRUE)) E3: (NOT p) == (p NAND TRUE) A similar construction can be used to show that NOR is also sufficient to generate every Boolean function. Thus, we can generate digital circuits for any Boolean function using just NAND or NOR operators (or components).