Sie sind auf Seite 1von 75

ICS 253: Discrete Structures I

The Foundations: Logic and Proofs

ICS253 - 1The Foundations: Logic and Proofs61

The Foundations : Logic and Proofs

Acknowledgement
The

material in these slides are based on:


K. H. Rosen,
Discrete Mathematics and Its Applications,
Seventh Edition (Global Edition Adapted by
Kamala Krithivasan), McGraw-Hill, 2013.
Chapter 1, Sections 1 6, 8
Slides of Dr. Wasfi Al-Khatib and Dr. Husni Al-Muhtaseb
And other internet resources

ICS253 - 1The Foundations: Logic and Proofs61

The Foundations : Logic and Proofs Slide # 2

The Foundations: Logic and Proofs


Part I:
Propositional

Logic (S1.1)

Applications

of Propositional Logic (S1.2)

Propositional

Equivalences (S1.3)

Part II:
Predicates
Nested
Rules

and Quantifiers (S1.4)

Quantifiers (S1.5)

of Inference (S1.6)

Introduction

to Proofs (S1.8)

ICS253 - 1The Foundations: Logic and Proofs61

The Foundations : Logic and Proofs Slide # 3

Section 1.1: Propositional Logic

Traditionally, logic distinguishes valid and invalid


arguments (2-valued logic).
Propositional Logic is the logic of compound statements
built from simpler statements using so-called Boolean
connectives.
Mathematical Logic is a tool for working with compound
statements. It includes:
A formal language for expressing them.
A concise notation for writing them.
A methodology for objectively reasoning about their truth or falsity.
It is the foundation for expressing formal proofs in all branches of
mathematics.

ICS253 - 1The Foundations: Logic and Proofs61

The Foundations : Logic and Proofs Slide # 4

Section 1.1: Propositional Logic


Some applications in computer science:
Design

of digital electronic circuits.

Expressing
Queries

conditions in programs.

to databases & search engines.

ICS253 - 1The Foundations: Logic and Proofs61

The Foundations : Logic and Proofs Slide # 5

Definition of a Proposition
Definition: A proposition (denoted p, q, r, ) is
a

statement (i.e., a declarative sentence)

with some definite meaning, (not vague or ambiguous)


having

a truth value thats either true (T) or false

(F)
it is never both, neither, or somewhere in between!

ICS253 - 1The Foundations: Logic and Proofs61

The Foundations : Logic and Proofs Slide # 6

Propositional Logic
Examples:
A proposition

is a declarative statement that is


either true or false
1. Dr. Wasfi holds a Ph.D. in arts.
2. KFUPM is either a big University or a small University.
3. Do you have a van?
4. 82 = 16.
5. I will go fishing tomorrow.
6. Do not drive while you are asleep.
7. X 8 = 12

ICS253 - 1The Foundations: Logic and Proofs61

The Foundations : Logic and Proofs Slide # 7

Compound Propositions
Compound

propositions are formed from single


propositions using logical operators
Negation

NOT

Conjunction

AND

Disjunction

OR

Exclusive or

XOR

Implication

IMPLIES

Bi-conditional

IFF

An

operator or connective combines one or more operand


expressions into a larger expression.

Propositional

or Boolean operators operate on propositions


(or their truth values) instead of on numbers.

ICS253 - 1The Foundations: Logic and Proofs61

The Foundations : Logic and Proofs Slide # 8

Truth Tables
A truth

table displays the relationship between the


truth values of propositions
Usually we are interested in knowing the compound truth
value of ALL possible truth values of the input
propositions

ICS253 - 1The Foundations: Logic and Proofs61

The Foundations : Logic and Proofs Slide # 9

Truth table for negation p


P: I am going to town
P: I am not going to town;
: It is not the case that I am going to town;

p
T
F
ICS253 - 1The Foundations: Logic and Proofs61

p
F
T
Slide # 10
The Foundations : Logic and Proofs

Truth Table for Conjunction p q


P: I am going to town
Q: It is going to rain
PQ: I am going to town and it is going to rain.
p

pq

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 11
The Foundations : Logic and Proofs

Truth Table for Disjunction p q


P: I am going to town
Q: It is going to rain
P Q: I am going to town or it is going to rain.
p

pq

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 12
The Foundations : Logic and Proofs

Truth Table for Exclusive Or p q


P: I am going to town
Q: It is going to rain
P Q: Either I am going to town or it is going to
rain.
p

pq

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 13
The Foundations : Logic and Proofs

Translating English Sentences into Logical Expressions


Q

14 pp 14: For each of these sentences, determine


whether an inclusive or an exclusive or is intended.
Explain your answer
a. Experience with C++ or Java is required
b. Lunch includes soup or salad
c. To enter the country, you need a passport or a voter
registration card
d. Publish or perish

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 14
The Foundations : Logic and Proofs

Truth Table for Implication p q


P: I am going to town
Q: It is going to rain
P Q: If I am going to town then it is going to
rain.
Note: The implication is false only when P is true
and Q is false!
P = premise, hypothesis, antecedent
Q = conclusion, consequence

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 15
The Foundations : Logic and Proofs

Truth Table for Implication p q


p

pq

The

implication is false only when P is true and Q


is false!

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 16
The Foundations : Logic and Proofs

Implication p q
Implication

is expressed in many ways

If p then q
p is sufficient for q
a sufficient condition for q is p
q is necessary for p
a necessary condition for p is q
p only if q

If p, q
p implies q
q whenever p
q when p
q if p
q unless p

q follows from p

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 17
The Foundations : Logic and Proofs

Implication p q
Discuss

the validity of the following statements:

If today is Friday, then 2 + 3 = 5


If today is Friday, then 2 + 3 = 6
Note

that if then in programming languages is


different from the one used in logic.

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 18
The Foundations : Logic and Proofs

Converse, Contrapositive and Inverse


p

q
If it is raining, then the home team wins

q p

is the Converse of p q

If the home team wins, then it is raining


q p is the Contrapositive of p q
If the home team does not win, then it is not

raining

p q is the Inverse of p q
If it is not raining, then the home team does not win
N.B. Only the contrapositive is equivalent to the original
statement.
ICS253 - 1The Foundations: Logic and Proofs61

Slide # 19
The Foundations : Logic and Proofs

Truth Table for Bi-conditional p q


if and only if, iff
Example:
P - I am going to town,
Q - It is going to rain
P Q: I am going to town if and only if it is going to rain.
Note: Both P and Q must have the same truth value.
p

pq

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 20
The Foundations : Logic and Proofs

Precedence of Logical Operators

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 21
The Foundations : Logic and Proofs

Logic and Bit Operations


Computers
A bit

represent information using bits

string is a sequence of zero or more bits.

0 represents F and 1 represents T


Bit operations are carried out in exactly the same manner
as their corresponding logic operations.
Evaluate

0001110001 1001001000

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 22
The Foundations : Logic and Proofs

The Foundations: Logic and Proofs


Part I:
Propositional Logic (S1.1)
Applications of Propositional Logic (S1.2)
Propositional Equivalences (S1.3)
Part II:
Predicates and Quantifiers (S1.4)
Nested Quantifiers (S1.5)
Rules of Inference (S1.6)
Introduction to Proofs (S1.8)

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 23
The Foundations : Logic and Proofs

Section 1.2: Applications of Propositional Logic


Translating
System

English Sentences

Specifications

Boolean

Searches

Logic

Puzzles

Logic

Circuits

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 24
The Foundations : Logic and Proofs

Translating English Sentences into Logical Expressions

Q8 pp 13: Let p, q, and r be the propositions


p :You get an A on the final exam.
q :You do every exercise in this book.
r :You get an A in this class

Write these propositions using p, q, and r and logical connectives (including


negations).
a) You get an A in this class, but you do not do every exercise in this book.

a) p q

b) You get an A on the final, you do every exercise in this book, and you get an A in
b)p q
this class.
c) To get an A in this class, it is necessary for you to get an A on the final.

c) r p

d) You get an A on the final, but you dont do every exercise in this book;
nevertheless, you get an A in this class.

d)p q

e) Getting an A on the final and doing every exercise in this book is sufficient for
getting an A in this class.

e) (p q

f ) You will get an A in this class if and only if you either do every exercise in this
book or you get an A on the final.
ICS253 - 1The Foundations: Logic and Proofs61

r
f) r (q

Slide # 25
The Foundations : Logic and Proofs

Translating English Sentences into Logical Expressions


You

cannot ride the roller coaster if you are under


4 feet tall unless you are older than 16 years old
q: you can ride the roller coaster
r: you are under 4 feet tall
s: you are older than 16 years old

(r s) q

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 26
The Foundations : Logic and Proofs

Group Activity
System

Specifications

Or
Logic

Puzzles

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 27
The Foundations : Logic and Proofs

System Specifications
System

specifications should be consistent, i.e. not


contain conflicting requirements
i.e. all requirements are true

(Q

5 pp 21).

The router can send packets to the edge system only if it


supports the new address space.
For the router to support the new address space, it is
necessary that the latest software release be installed.
The router can send packets to the edge system if the
latest software release is installed.
The router does not support the new address space.
ICS253 - 1The Foundations: Logic and Proofs61

Slide # 28
The Foundations : Logic and Proofs

(Q5 pp21).
1. The router can send packets to the edge
system (S) only if it supports the new
address space (N).
2. For the router to support the new
address space(N), it is necessary that
the latest software release be installed
(L).

1. SN

2. N L

3. The router can send packets to the edge


system (S) if the latest software release
is installed (L).

3. L S

4. The router does not support the new


address space (N).

4. N

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 29
The Foundations : Logic and Proofs

(Q5 pp21).
N true then N is F
1. SN
If N is F then S should be F for 1 to be true
2. N L
Since N is F then 2 is True, L could be T or F
3. L S
Since S is F then we take L to be F
Hence, Consistent

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 30
The Foundations : Logic and Proofs

Example on Logic Puzzles

In [Sm78] Smullyan posed many puzzles about an island that has two
kinds of inhabitants, knights, who always tell the truth, and their
opposites, knaves, who always lie.
A says At least one of us is a knave and B says nothing.

Let P(x): x is a knight

and

P(x): x is a knave

Suppose A is a knave: P(A) T


What A says must be false
Contradiction
P(A) P(B) F
Check:
P(A) P(B) T P(B) T
Suppose A is a knight and what A says
must be true.
P(A)
P(A) P(B)
P(B)
A is a knight and B is a knave.
ICS253 - 1The Foundations: Logic and Proofs61

Slide # 31
The Foundations : Logic and Proofs

Example on Logic Puzzles


You encounter two people A and B. What are A and B if
A says "B is a knight" and
B says "The two of us are opposite types"?
Suppose A is a knight, so
P(A) T
P(B) T
Since "The two of us are opposite types
P(A) P(B) P(A) P(B) T since P(B) is T
But if P(A) and P(B) then
P(A) P(B) P(A) P(B) F
Suppose A is a knave
So P(B) is F so
P(A) P(B) P(A) P(B) F
So they are both knaves
ICS253 - 1The Foundations: Logic and Proofs61

Slide # 32
The Foundations : Logic and Proofs

Logic Circuits
Basic

An

Logic Gates

Example Combinatorial Circuit

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 33
The Foundations : Logic and Proofs

Logic Circuits (Individual Activity)


Q

: Find the output of each of these combinatorial circuits

Q:

Construct a combinatorial circuit using inverters, OR


gates, and AND gates that produces the output (p r)
( q r) from input bits p, q, and r.

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 34
The Foundations : Logic and Proofs

The Foundations: Logic and Proofs


Part I:
Propositional Logic (S1.1)
Applications of Propositional Logic (S1.2)
Propositional Equivalences (S1.3)
Part II:
Predicates and Quantifiers (S1.4)
Nested Quantifiers (S1.5)
Rules of Inference (S1.6)
Introduction to Proofs (S1.8)

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 35
The Foundations : Logic and Proofs

Section 1.3: Propositional Equivalences

Tautology: A compound proposition that is


always true, regardless of the truth values of the
single/simple propositions in it.

Propositions p and q are called logically equivalent if


pq is a tautology, denoted by p q

Contradiction: A compound proposition that is


always false.

Contingency: Neither a tautology nor a


contradiction

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 36
The Foundations : Logic and Proofs

Example

Show that the following proposition is a


tautology:
(pq) p q

pq (pq) p q p q

ICS253 - 1The Foundations: Logic and Proofs61

(pq)
p q

Slide # 37
The Foundations : Logic and Proofs

38

Logical Equivalences (Individual Activity)


Using

truth tables, show that


(p q) (pq)

ICS253 - 1The Foundations: Logic and Proofs61

The Foundations : Logic and Proofs

Some Useful Equivalences (1)


pT p
pF p
pT T

Identity
Laws
Domination

pF F
pp p
pp p
pq qp

Laws
Idempotent
Laws
Commutative

pq qp
(p) p

Laws

ICS253 - 1The Foundations: Logic and Proofs61

Double Negation Law


Slide # 39
The Foundations : Logic and Proofs

Some Useful Equivalences (2)


(pq)r p(qr)

Associative

(pq)r p(qr)

Laws

(pq) pq

DeMorgan

(pq) pq

Laws

p(qr) (pq)(pr)

Distributive

p(qr) (pq)(pr)

Laws

p p T
p p F
(p q) (pq)
p (pq) p
ICS253 - 1The Foundations: Logic and Proofs61

Negation
Laws
Absorption
Laws
Slide # 40
The Foundations : Logic and Proofs

Some Useful Equivalences (3)


pq pq
pq qp
pq pq
p q (p q)
(p q) p q
(p q) (p r) p (q r)
(p r) (q r) (p q) r
(p q) (p r) p (q r)
(p r) (q r) (p q) r

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 41
The Foundations : Logic and Proofs

Some Useful Equivalences (4)


p q (p q) (q p)
pq pq
p q (p q) ( p q)
(p q) p q

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 42
The Foundations : Logic and Proofs

Why Know These Equivalences?

Instead of using truth tables, we can use these


equivalences to arrive to the same conclusion

Any good reasons for not using truth tables?

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 43
The Foundations : Logic and Proofs

Examples
Show that
1.

(p(pq)) (p q)

2. (pq)r is not equivalent to p(q r)


1. ((pp) (pq))
( T (pq))
( pq)

2.

F,T,F will yield different answers

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 44
The Foundations : Logic and Proofs

Examples
3. Show that

(pq) (pq) (p q)

(p q) (p q) [(p q) p] [(p q) q]
[(p p) (q p)] [(p q) (q q)]
[(T (q p)] [(p q) T]
(q p) (p q) (p q) (q p)
(pq)
(distribute and continue)

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 45
The Foundations : Logic and Proofs

The Foundations: Logic and Proofs


Part I:
Propositional Logic (S1.1)
Applications of Propositional Logic (S1.2)
Propositional Equivalences (S1.3)
Part II:
Predicates and Quantifiers (S1.4)
Nested Quantifiers (S1.5)
Rules of Inference (S1.6)
Introduction to Proofs (S1.8)

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 46
The Foundations : Logic and Proofs

Section 1.4: Predicates and Quantifiers


Propositional

logic (Sections 1.1 and 1.2) cannot


adequately express the meaning of statements in
mathematics and in natural language.
E.g., suppose that we know that
Every student at KFUPM has a computer account.
Are there any rules in propositional logic that can make
us conclude the truth of the statement that
Ahmad, who is a student at KFUPM, has a computer
account

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 47
The Foundations : Logic and Proofs

Predicates

Predicates: propositions which contain variables


Once value(s) has/have been assigned to the
variable(s), the predicate becomes a proposition.
Examples

P(x) x > 1
\\ P is called a propositional function
Q(x,y) x = y 5
R(x,y,z) z > x y
if P(x) then
x = x - 1;
else
x = x2;
end if;

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 48
The Foundations : Logic and Proofs

Examples
Let

P(x) denote the statement (x>3).

What are the truth values of P(4) and P(2)?


Let

Q(x, y) denote the statement x = y + 3.

What are the truth values of Q(1,2) and Q(3,0)?

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 49
The Foundations : Logic and Proofs

Quantifiers

Universal quantification of P(x) is the proposition


P(x) is true for all values of x in the universe
of discourse (domain of x)
x P(x)

Example: Let P(x) be the statement x + 1 > x and


the domain of x consists of all real numbers.
What is the truth value of x P(x)?
ICS253 - 1The Foundations: Logic and Proofs61

Slide # 50
The Foundations : Logic and Proofs

Quantifiers

Existential quantification of P(x) is the


proposition:
There exists an element x in the universe of
discourse (domain of x) such that P(x) is true
x P(x)

Example: Let P(x) be the statement x > 3 and the


domain of x consists of all real numbers.
What is the truth value of x P(x)?
ICS253 - 1The Foundations: Logic and Proofs61

Slide # 51
The Foundations : Logic and Proofs

Quantifiers
Uniqueness

quantifier of P(x) is the proposition

that
there exists a unique element x, in the
of x, such that P(x) is true,

domain

!x P(x) or 1x P(x)
P(x)

is true for one and only one x in the universe


of discourse.

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 52
The Foundations : Logic and Proofs

REMEMBER!
A predicate

is not a proposition until all variables


have been bound either by quantification or
assignment of a value!

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 53
The Foundations : Logic and Proofs

Examples

Express the following statement using proper


quantification

Every student in this class has studied calculus

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 54
The Foundations : Logic and Proofs

Question 7 pp 50
P(x) x can speak Russian,Q(x) x knows
C++
For the universe of discourse all students at
your school
1.There is a student at your school who can 1. x (P(x) Q(x))
speak Russian and who knows C++
2.There is a student at your school who can 2. x (P(x) Q(x))
speak Russian but who doesnt know C++
3.Every student at your school either can
speak Russian or knows C++
3. x (P(x)Q(x))
4.No student at your school can speak
Russian or knows C++
4. !x (P(x) Q(x))
x (P(x)Q(x))

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 55
The Foundations : Logic and Proofs

Truth of Quantifiers
Statement

When True?

When False?

x P(x)
x P(x)

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 56
The Foundations : Logic and Proofs

Truth of Quantifiers
Statement

x P(x)
x P(x)

When True?

When False?

There is an x for
P(X) is true for
which P(x) is
every X
false
There is an x
P(X) is false for
for which P(x)
every X
is true

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 57
The Foundations : Logic and Proofs

Precedence of Quantifiers and Logical Operators

Operator

Precedence

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 58
The Foundations : Logic and Proofs

Example
quantifiers and have higher precedence
than all logical operators from propositional
calculus.

The

E.g., x P(x) Q(x)


means..
does not mean

It means
(x P(x)) Q(x)
Does not mean
x (P(x) Q(x))

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 59
The Foundations : Logic and Proofs

Binding Variables
The

occurrence of a variable x is bound iff a value


is assigned to x or when a quantifier is used on x.
Otherwise, it is called free.

Example: x (x + y) = 1; x is bound, y is free.

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 60
The Foundations : Logic and Proofs

Logical Equivalences Involving Quantifiers


Statements

involving predicates and quantifiers are


logically equivalent if and only if they have the
same truth value no matter which predicates are
substituted into these statements and which domain
of discourse is used for the variables in these
propositional functions.
ST

indicate that two statements involving predicates and


quantifiers are logically equivalent.
Example: x(P(x) Q(x) ) and xP(x) xQ(x)

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 61
The Foundations : Logic and Proofs

Logical Equivalences Involving Quantifiers


Q

29 pp 52: Establish these logical equivalences,


where x does not occur as a free variable in A.
Assume that the domain is nonempty.
(x P(x)) A x (P(x) A )

Prove that each side implies the other. and


hold.
If A is

T, then the LHS And RHS become equal to

true.
If A is

false, both become equal to x P(x)

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 62
The Foundations : Logic and Proofs

Logical Equivalences Involving Quantifiers


Q:

Establish these logical equivalences, where x


does not occur as a free variable in A. Assume that
the domain is nonempty.
a) (xP(x)) A x(P(x) A)
b) (xP(x)) A x(P(x) A)

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 63
The Foundations : Logic and Proofs

Negation

x P(x) x P(x)

x P(x) x P(x)

These are called De Morgans Laws for Quantifiers

Find the negation of


1. There is an honest politician
x ( x2 > x )

ICS253 - 1The Foundations: Logic and Proofs61

x H(x)
2. x (x2 x)
x (x2 x)

Slide # 64
The Foundations : Logic and Proofs

Example Q 13 pp 51
Translate in two ways each of these statements into logical expressions
using predicates, quantifiers, and logical connectives. Let the domain
consist of the students in your class and
a. Someone in your class can speak Hindi
b. Everyone in your class is friendly
e. No student in your class has taken a course in logic programming

Let: C(x) = x is in my class; H(x) = x can speak Hindi; F(x)= x is


friendly

a. x (C(x) and H(x)),

x H(x)

b. x (C(x) F(x)),

x F(x)

e. x (C(x) and L(x)) x(C(x) or L(x))


x(C(x)L(x))
ICS253 - 1The Foundations: Logic and Proofs61

Slide # 65
The Foundations : Logic and Proofs

Example Q 18 pp 51
Translate in two ways each of these statements into logical expressions
using predicates , quantifiers, and logical connectives.
a. Something is not in the correct place
b. All tools are in the correct place and are in excellent condition
c. Everything is in the correct place and in excellent condition
d. Nothing is in the correct place and is in excellent condition
e. One of your tools is not in the correct place, but it is in excellent
condition

Let: T(x): x is a tool; P(x): x is in the correct place;


Q(x): x is in excellent condition

x ( P(x))
x(T(x) P(x) and Q(x))
x(P(x) and Q(x))
x (P(x) or Q(x))
x(T(x) and P(x) and Q(x))

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 66
The Foundations : Logic and Proofs

Examples Q 19 pp 51
Suppose

that the domain of Q(x,y,z) consist of


triples x, y, z, where x=0, 1, or 2, y=0 or 1, and z=0
or 1. Write out these propositions using
disjunctions and conjunctions.
a. y Q(0,y,0)
b. x Q(x,1,1)
a: Q(0,0,0) and Q(0,1,0).
b. Q(0,1,1) or Q(1,1,1) or Q(2,1,1).

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 67
The Foundations : Logic and Proofs

Logic Programming
There

are some programming languages that are based


entirely on predicate logic!

The

most famous one is called Prolog.

A Prolog

program is a set of propositions (facts) and


(rules) in predicate logic.

The

input to the program is a query proposition.


Want to know if it is true or false.

The

Prolog interpreter does some automated deduction to


determine whether the query follows from the facts.

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 70
The Foundations : Logic and Proofs

Facts in Prolog
A fact

in Prolog represents a simple, non-compound

proposition in predicate logic.


e.g., John likes Mary
can be written Likes(John, Mary) in predicate logic.
can be written likes(john, mary). in Prolog!

Lowercase symbols must be used for all constants


and predicates,
Uppercase is reserved for variable names.

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 71
The Foundations : Logic and Proofs

Rules in Prolog
A rule

in Prolog represents a universally quanitifed


proposition of the general form
x: [y P(x,y)]Q(x),
where x and y might be compound variables
x=(z,w) and P,Q compound propositions.

In

Prolog, this is written as the rule:

q(X) :- p(X,Y). Variables must be capitalized


i.e., the , quantifiers are implicit.
Example:

likable(X) :- likes(Y,X).

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 72
The Foundations : Logic and Proofs

Conjunction and Disjunction


Logical

conjunction is encoded using multiple


comma-separated terms in a rule.

Logical

disjunction is encoded using multiple

rules.
E.g.,

x [(P(x)Q(x))R(x)]S(x)

can be rendered in Prolog as:


s(X) :- p(X), q(X)
s(X) :- r(X)

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 73
The Foundations : Logic and Proofs

Deduction in Prolog
When

a query is input to the Prolog interpreter,

it searches its database to determine if the query can be


proven true from the available facts.
if so, it returns yes, if not, no.
If the query contains any variables, all values
that make the query true are printed.

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 74
The Foundations : Logic and Proofs

Simple Prolog Example


An

example input program:

likes (john, mary).


likes (mary, fred).
likes (fred, mary).
likable (X) :- likes (Y,X).
An

example query: ? likable(Z)

returns:

mary
fred

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 75
The Foundations : Logic and Proofs

Example
Facts
instructor(Wasfi, ics253)
instructor(Yahya, ics324)
instructor(Marwan,coe202)
enrolled(ahmed, ics253)
enrolled(adel, ics324)
enrolled(rida, coe202)
enrolled(osamah, coe202)
enrolled(rida, ics253)
enrolled(osamah, ics324)
enrolled(ahmed, coe202)
enrolled(adel, ics253)

Queries
The query Is ahmed instructor of ics324 ?,

instructor(ahmed , ics324)
The query Is osamah enrolled in coe202 ?,

enrolled(osamah, coe202)
The query who is enrolled in coe202 ?,
enrolled(X, coe202)
The query who teaches rida?,
teaches(X, rida)

Rule
Teaches(P, S) :- instructor(P, C), enrolled(S, C)
ICS253 - 1The Foundations: Logic and Proofs61

Slide # 76
The Foundations : Logic and Proofs

Summary
Predicate logic notation & conventions
Conversions: predicate logic clear English
Meaning of quantifiers, equivalences
Simple reasoning with quantifiers

ICS253 - 1The Foundations: Logic and Proofs61

Slide # 77
The Foundations : Logic and Proofs

Das könnte Ihnen auch gefallen