COMP9020
Foundations of Computer Science
Lecture 12: Boolean Logic
Topic 3: Logic
Logic
[LLM] [RW] [Rosen]
Week 8 Boolean Logic Ch. 3 Ch. 2, 10 Ch. 12
Week 8 Propositional Logic Ch. 3 Ch. 2 Ch. 1
1
What is logic?
Logic is about formalizing reasoning and defining truth
Adding rigour
Removing ambiguity
Mechanizing the process of reasoning
2
Loose history of logic
(Ancient times): Logic exlusive to philosophy
Mid-19th Century: Logical foundations of Mathematics
(Boole, Jevons, Schröder, etc)
1910: Russell and Whitehead’s Principia Mathematica
1928: Hilbert proposes Entscheidungsproblem
1931: Gödel’s Incompleteness Theorem
1935: Church’s Lambda calculus
1936: Turing’s Machine-based approach
1930s: Shannon develops Circuit logic
1960s: Formal verification; Relational databases
3
Logic in Computer Science
Computation = Calculation + Symbolic manipulation
4
Logic in Computer Science
Computation = Calculation + Symbolic manipulation
Logic as 2-valued computation (Boolean logic):
Circuit design
Code optimization
Boolean algebra
Nand game
4
Logic in Computer Science
Computation = Calculation + Symbolic manipulation
Logic as symbolic reasoning (Propositional logic, and beyond):
Formal verification
Proof assistance
Knowledge Representation and Reasoning
Automated reasoning
Databases
4
Outline
Boolean Logic
Boolean Functions
Conjunctive and Disjunctive Normal Form
Karnaugh Maps
Boolean Algebras
5
Outline
Boolean Logic
Boolean Functions
Conjunctive and Disjunctive Normal Form
Karnaugh Maps
Boolean Algebras
6
Boolean logic
Boolean logic is about performing calculations in a “simple”
mathematical structure.
complex calculations can be built entirely from these simple
ones
can help identify simplifications that improve performance at
the circuit level
can help identify simplifications that improve presentation at
the programming level
7
The Boolean Algebra B
Definition
The (two-element) Boolean algebra is defined to be the set
B = {0, 1}, together with the functions ! : B → B, &&: B2 → B,
and ∥: B2 → B, defined as follows:
!x = (1 − x) x && y = min{x, y } x ∥ y = max{x, y }
8
The Boolean Algebra B – Alternative definition
Definition
The (two-element) Boolean algebra is defined to be the set
B = {false, true}, together with the functions ! : B → B,
&&: B2 → B, and ∥: B2 → B, defined as follows:
x y x && y x y x ∥y
x !x false false false false false false
false true false true false false true true
true false true false false true false true
true true true true true true
9
Alternative notation
Commonly, the following alternative notation is used:
For B: {F , T }
For !x: x, x ′ , ∼x, ¬x
For x && y : xy , x ∧ y
For x ∥ y : x + y, x ∨ y
10
Properties
We observe that !, &&, and ∥ satisfy the following:
For all x, y , z ∈ B:
Commutativity x ∥y =y ∥x
x && y = y && x
Associativity (x ∥ y ) ∥ z = x ∥ (y ∥ z)
(x && y ) && z = x && (y && z)
Distribution x ∥ (y && z) = (x ∥ y ) && (x ∥ z)
x && (y ∥ z) = (x && y ) ∥ (x && z)
Identity x ∥0=x
x && 1 = x
Complementation x ∥ (!x) = 1
x && (!x) = 0
11
Examples
Examples
Calculate x && x for all x ∈ B
Calculate ((1 && 0) ∥ ((!1) && (!0))
12
Outline
Boolean Logic
Boolean Functions
Conjunctive and Disjunctive Normal Form
Karnaugh Maps
Boolean Algebras
13
Boolean Functions
Definition
An n-ary Boolean function is a map f : Bn → B.
Question
How many unary Boolean functions are there?
How many binary functions?
n-ary?
14
Examples
Examples
! is a unary Boolean function
&&, ∥ are binary Boolean functions
f (x, y ) =!(x && y ) is a binary boolean function (NAND)
And(x0 , x1 , . . .) = (· · · ((x0 && x1 ) && x2 ) · · · ) is a (family)
of Boolean functions
Or(x0 , x1 , . . .) = (· · · ((x0 ∥ x1 ) ∥ x2 ) · · · ) is a (family) of
Boolean functions
15
Application: Adding two one-bit numbers
How can we implement:
add : B2 → B2
defined as
x y add(x, y )
0 0 00
0 1 01
1 0 01
1 1 10
Use two Boolean functions!
NB
Digital circuits are just sequences of Boolean functions.
16
Outline
Boolean Logic
Boolean Functions
Conjunctive and Disjunctive Normal Form
Karnaugh Maps
Boolean Algebras
17
Conjunctive and Disjunctive normal form
Definition
A literal is a unary Boolean function
A minterm is a Boolean function of the form
And(l1 (x1 ), l2 (x2 ), . . . , ln (xn )) where the li are literals
A maxterm is a Boolean function of the form
Or(l1 (x1 ), l2 (x2 ), . . . , ln (xn )) where the li are literals
A CNF Boolean function is a function of the form
And(m1 , m2 , . . .), where the mi are maxterms.
A DNF Boolean function is a function of the form
Or(m1 , m2 , . . .), where the mi are minterms.
Important!
CNF/DNF describe how a function is written. It is not a property
of the function itself.
CNF: product of sums; DNF: sum of products
18
Examples
Examples
f (x, y , z) = (x && (!y ) && z) ∥ (x && (!y ) && (!z)) =
x y z + x y z: DNF , but not CNF
g (x, y , z) = (x ∥ (!y ) ∥ z) && (x ∥ (!y ) ∥ (!z)) =
(x + y + z)(x + y + z): CNF function, but not DNF
h(x, y , z) = (x && (!y ) && z) = x y z: both CNF and DNF
j(x, y , z) = x + y (z + x): Neither CNF nor DNF
19
Theorem
Every Boolean function can be written as a function in DNF/CNF
Proof...
20
Canonical DNF
Given an n-ary boolean function f : Bn → B we construct an
equivalent DNF boolean function as follows:
For each b = (b1 , . . . , bn ) ∈ Bn we define the minterm
mb = And(l1 (x1 ), l2 (x2 ), . . . , ln (xn ))
where
xi if bi = 1
li (xi ) =
!xi if bi = 0
We then define the DNF formula:
X
fDNF = mb ,
f (b)=1
that is, fDNF is the disjunction (or) over all minterms corresponding
to elements b ∈ B where f (b) = 1.
21
Canonical DNF
Theorem
f and fDNF are the same function.
22
Exercise
Exercises
Find the canonical DNF form of each of the following expressions
in variables x, y , z
xy
z
xy + z
f (x, y , z) = 1
23
Outline
Boolean Logic
Boolean Functions
Conjunctive and Disjunctive Normal Form
Karnaugh Maps
Boolean Algebras
24
Karnaugh Maps
For up to four variables (propositional symbols) a diagrammatic method
of simplification called Karnaugh maps works quite well.
For every propositional function of k = 2, 3, 4 variables we construct a
rectangular array of 2k cells. We mark the squares corresponding to the
value true with eg “+” and try to cover these squares with as few rect-
angles with sides 1 or 2 or 4 as possible.
Example
yz y z̄ ȳ z̄ ȳ z
x + + +
x̄ + + +
E = (xy ) ∨ (x̄ ȳ ) ∨ z
Canonical form would consist of writing all cells separately (6 clauses).
25
Karnaugh Maps
For optimisation, the idea is to cover the + squares with the minimum
number of rectangles. One cannot cover any empty cells.
The rectangles can go ‘around the corner’/the actual map should be
seen as a torus.
Rectangles must have sides of 1, 2 or 4 squares (three adjacent cells
are useless).
Example
yz y z̄ ȳ z̄ ȳ z
x + + +
x̄ + + +
E = (xy ) ∨ (x̄ ȳ ) ∨ z
Canonical form would consist of writing all cells separately (6 clauses).
25
Exercise
Exercise
yz y z̄ ȳ z̄ ȳ z
wx + + +
w x̄ + + + +
w̄ x̄ + +
w̄ x + +
26
Outline
Boolean Logic
Boolean Functions
Conjunctive and Disjunctive Normal Form
Karnaugh Maps
Boolean Algebras
27
Definition: Boolean Algebra
Definition
A Boolean algebra is a structure (T , ∨, ∧,′ , 0, 1) where
0, 1 ∈ T
∨, ∧ : T × T → T (called join and meet respectively)
′
: T → T (called complementation)
and the following laws hold for all x, y , z ∈ T :
Commutativity: x ∨ y = y ∨ x, x ∧y =y ∧x
Associativity: (x ∨ y ) ∨ z = x ∨ (y ∨ z)
(x ∧ y ) ∧ z = x ∧ (y ∧ z)
Distributivity: x ∨ (y ∧ z) = (x ∨ y ) ∧ (x ∨ z)
x ∧ (y ∨ z) = (x ∧ y ) ∨ (x ∧ z)
Identity: x ∨ 0 = x, x ∧1=x
Complementation: x ∨ x ′ = 1, x ∧ x′ = 0
28
Examples of Boolean Algebras
Example
The set of subsets of a set X :
T : Pow(X )
∨ (join) : ∪
∧ (meet) : ∩
′ (complement) : ·c
0: ∅
1: X (U)
The Laws of Boolean algebra follow from the Laws of Set
Operations.
29
Examples of Boolean Algebras
Example
The two element Boolean Algebra :
B = ({true, false}, ∥, &&, !, false, true)
where !, &&, ∥ are defined as:
!true = false; !false = true,
true && true = true; ...
true ∥ true = true; ...
30
Examples of Boolean Algebras
Example
Cartesian products of B, that is n-tuples of 0’s and 1’s with
Boolean operations, e.g. B4 :
join: (1, 0, 0, 1) ∨ (1, 1, 0, 0) = (1, 1, 0, 1)
meet: (1, 0, 0, 1) ∧ (1, 1, 0, 0) = (1, 0, 0, 0)
complement: (1, 0, 0, 1)′ = (0, 1, 1, 0)
0 : (0, 0, 0, 0)
1 : (1, 1, 1, 1).
31
Examples of Boolean Algebras
Example
Functions from any set S to B; that is, BS
If f , g : S −→ B then
(f ∨ g ) : S → B defined by s 7→ f (s) ∥ g (s)
(f ∧ g ) : S → B defined by s 7→ f (s) && g (s)
f′ :S →B defined by s 7→!f (s)
0:S →B is the function s 7→ 0
1:S →B is the function s 7→ 1
32
Proofs in Boolean Algebras
Show an identity holds using the laws of Boolean Algebra, then
that identity holds in all Boolean Algebras.
Example
In all Boolean Algebras
x ∧x =x
for all x ∈ T .
Proof:
x =x ∧1 [Identity]
= x ∧ (x ∨ x ′ ) [Complement]
= (x ∧ x) ∨ (x ∧ x ′ ) [Distributivity]
= (x ∧ x) ∨ 0 [Complement]
= (x ∧ x) [Identity]
33
Duality
Definition
If E is an expression defined using variables (x, y , z, etc),
constants (0 and 1), and the operations of Boolean Algebra (∧, ∨,
and ′ ) then dual(E ) is the expression obtained by replacing ∧ with
∨ (and vice-versa) and 0 with 1 (and vice-versa).
Definition
If (T , ∨, ∧,′ , 0, 1) is a Boolean Algebra, then (T , ∧, ∨,′ , 1, 0) is
also a Boolean algebra, known as the dual Boolean algebra.
Theorem (Principle of duality)
If you can show E1 = E2 using the laws of Boolean Algebra, then
dual(E1 ) = dual(E2 ).
34
Duality
Example
We have shown x ∧ x = x.
By duality: x ∨ x = x.
35