Lab2
Lab2
DISCRETE STRUCTURES
Lab 2
Fundamentals of Logic
Trần Hồng Tài
Abstract
In this Laboratory, we will practice using python on Logic expression
and statement.
def implies (a , b ) :
if a :
return b
else :
return True
Elit.tdtu.edu.vn 1
TON DUC THANG UNIVERSITY
Faculty of Information Technology
Notice, though, that the Python interpreter will merely check whether the impli-
cation statement holds for the particular values assigned to v and y at run-time.
To check the validity of the statement in general, one has to run it for every
possible combination of values for x and y — a daunting task even considering
the limited range of Python’s integers! Alternatively, one may attempt to prove
the statement by logical means, but it should be noted that this is an activity
that is quite different from repeated evaluation of boolean expressions. Whether
computers can be helpful in constructing proofs as well is a question best de-
ferred to a later course. For more information, read following lecture notes of
Discrete Structures on site elit.tdtu.edu.vn
2 Exercises
1. Write python functions to calculate:
(a) logical implies r=lImplies(p,q)
(b) logical and r=lAnd(p,q)
(c) logical or r=lOr(p,q)
(d) logical xor r=lXor(p,q)
(e) logical not r=lNot(p)
(f) logical equivalent r=lEquipvalent(p,q)
where p,q and r are 3 logical variables.
Hint: to define a function in python we use:
def functionname ( inpu tvaria bles ) :
# calculate
return o u tp ut v ar ia b le s
Elit.tdtu.edu.vn 2
TON DUC THANG UNIVERSITY
Faculty of Information Technology
R=
P Q lLImp- lLAnd- lLOr(P,Q) lLXor- lLNot(P) lLEquiva-
lies(P,Q) (P,Q) (P,Q) lent(P,Q)
T T T T T F F T
T F F F T T F F
F T T F T T T F
F F T F F F T T
3. Write python program to calculate and print truth table for following ex-
pressions:
p∧q →r
r →p∧ q
The table should contain a row for each possible combination of values
for variables p, r and q and columns for the final result as well as each
relevant Boolean subexpression.
Hint:import function product from library itertools.
4. Write python programs to calculate and print truth table for following
expressions:
p ∨ q → p ∧ q → ¬p ∨ ¬q
¬p ∨ (q ∧ r) → r
(p → q) ∧ (q → r)
(p ∨ (q ∧ r)) ↔ ((p ∨ q) ∧ (p ∨ r))
p ∨ q → ¬r ∨ t
p ∨ t → q → (r → t)
(p ∨ (q ∧ r)) ↔ (((p ∨ q) ∧ (p ∨ r)) ∧ (t ∨ ¬t))
Elit.tdtu.edu.vn 3
TON DUC THANG UNIVERSITY
Faculty of Information Technology
(p ∨ ¬q) → ¬p ≡ (p ∨ (¬q)) → ¬p
¬(p ∨ q) ≡ (¬p ∧ ¬q)
(a) p → r (c) p → q
¬p → q ¬r ∨ s
q→s p∨r
∴ ¬r → s ∴ ¬q → s
(b) p → (q → r) (d) p
p∨s p→r
t→q p → (q ∨ ¬r)
¬s ¬q ∨ ¬s
∴ ¬r → ¬t ∴s
Elit.tdtu.edu.vn 4