0% found this document useful (0 votes)
4 views

Lab2

Uploaded by

humiss545
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lab2

Uploaded by

humiss545
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

TON DUC THANG UNIVERSITY

Faculty of Information Technology

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.

1 Fundamentals of Logic in Python


The connection between logic, proofs and programming is a very rich and inter-
esting field that goes far byond the scope of an introductory course in discrete
mathematics. But instead of illustrating this point here by introducing loads of
material not covered in the text book, we will limit our ambitions to simply em-
phasizing the obvious: the similarities between logical truth values and Pythons
boolean type and its operations.
In Python terms, a logical statement is an expression of boolean type, such as 3
< 7. Variables can be used in such expressions, although it should be noted that
every variable will stand for some concrete value at run-time. Computing with
open statements is also possible in Python, but the techniques required will not
be described in this note.
Of the logical connectives, Python directly supports conjunction and disjunc-
tion in terms of the built-in primitives and and or, as well as negation via the
primitive not. Other connectives can easily be encoded as functions by use of
the if statement. The implication arrow, for example, may be defined as follows:

def implies (a , b ) :
if a :
return b
else :
return True

To express the logical statement x ≥ 0 ∧ y ≥ 0 → x ∗ y ≥ 0 in Python one simply


writes:
implies ( x > = 0 and y > =0 , v * y > = 0 )

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

2. Write python functions to calculate:


(a) logical implies R=lLImplies(P,Q)
(b) logical and R=lLAnd(P,Q)
(c) logical or R=lLOr(P,Q)
(d) logical xor R=lLXor(P,Q)
(e) logical not R=lLNot(P)
(f) logical equivalent R=lLEquivalent(P,Q)

where P,Q and R are 3 lists of logical variables


for example with:
P = [T rue, T rue, F alse, F alse];
Q = [T rue, F alse, T rue, F alse]:
Then we have:

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))

5. Write python programs to tell if the following formulas are equivalent:


(The programs should print “equivalent” or “Inequivalent” as result)
p ≡ ¬(¬p)
¬(¬q ∧ p) ∧ (q ∨ p) ≡ q
¬(p ∨ q) ≡ (¬p ∨ ¬q)
(p ∨ q) → r ≡ (p → r) ∧ (q → r)
¬(p ∧ q) ≡ (¬p ∧ ¬q)

Elit.tdtu.edu.vn 3
TON DUC THANG UNIVERSITY
Faculty of Information Technology

(p ∨ ¬q) → ¬p ≡ (p ∨ (¬q)) → ¬p
¬(p ∨ q) ≡ (¬p ∧ ¬q)

6. Write python programs to show the validity of the following argument:


(the programs should simply print “INVALID” or “VALID” as result)

(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

You might also like