Module 2 - v2

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 126

Module #2

CSE 3005:
Applied Artificial Intelligence
Contents
• Introduction to using Logic
• Proposition Logic

2
Logic
• A logic is a formal language with precisely defined syntax and
semantics, which supports sound inference.
• Different logics exist which allow you to represent different things,
and which allow more or less efficient inference.
• Logic may be of different types like propositional logic, predicate /
first-order logic, etc.

3
Properties of a Logic
• A language with concrete rules
• No ambiguity in representation (may be other errors!)
• Allows unambiguous communication and processing
• Very unlike natural languages e.g. English
• Many ways to translate between languages
• A statement can be represented in different logics
• And perhaps differently in same logic
• Expressiveness of a logic
• How much can we say in this language?
• Not to be confused with logical reasoning
• Logics are languages, reasoning is a process (may use logic)

4
Syntax and Semantics
• Syntax
• Rules for constructing legal sentences in the logic
• Which symbols we can use (English: letters, punctuation)
• How we are allowed to combine symbols
• Semantics
• How we interpret (read) sentences in the logic
• Assigns a meaning to each sentence
• Example: “All lecturers are seven foot tall”
• A valid sentence (syntax)
• And we can understand the meaning (semantics)
• This sentence happens to be false (there is a counterexample)

5
Propositional Logic
• Syntax:
• Propositions. Eg. “It is cold today”
• Connectives: and, or, not, implies, if and only if.
• Brackets, True, False.
• Semantics:
• Define how connectives affect truth
• Eg. “P and Q” is true only if P is true and Q is true
• Use truth tables to work out the truth of statements

6
Proposition
• A proposition is a declarative sentence that is either true or false, but
not both.
• Are the following sentences propositions?
• Toronto is the capital of Canada
• Read this carefully
• 1+2=3
• X+1=2
• What time is it?

7
Propositional Logic
• Propositional logic is the simplest form of logic where all statements
are propositions
• Examples:
• It is Wednesday.
• The Sun rises from the East
• 3+3=7
• 5 is a prime number

8
Propositional Variables
• Propositional Variables are variables that represent propositions.
• Example:
• P = “Today is Wednesday”
• Q = “I like ice-cream”
• Truth values:
• True / T -> The proposition is true.
• False / F -> The proposition is false.

9
Syntax
• The syntax of propositional logic defines the allowable sentences.
• The atomic sentences consist of a single proposition symbol.
• We use symbols that start with an uppercase letter and may contain
other letters or subscripts
• There are two proposition symbols with fixed meanings: True is the
always-true proposition and False is the always-false proposition.
• Complex sentences are constructed from simpler sentences, using
parentheses and logical connectives.

10
Semantics
• The semantics defines the rules for determining the truth of a
sentence with respect to a particular model.
• With three proposition symbols, there are 23 = 8 possible models. In
general, for n proposition symbols, we have 2n combinations / rows of
the truth table.

11
Logical Connectives

12
Truth Tables

13
Logical Connectives: Negation
• Let p be a proposition. The negation of p, denoted by ¬p, is the
statement “It is not the case that p.”
• The proposition ¬p is read “not p.” The truth value of the negation of
p, ¬p is the opposite of the truth value of p.
• Examples:
• Find the negation of the proposition “Today is Friday”
• Solution: “It is not the case that today is Friday” / “Today is not Friday”

14
Logical Connectives: Conjunction
• Let p and q be propositions. The conjunction of p and q, denoted by p
Λ q, is the proposition “p and q”. The conjunction p Λ q is true when
both p and q are true and is false otherwise.
• Example:
• P: “Today is Wednesday”
• Q: “It is cold today”
• P ^ Q: “Today is Wednesday AND it is cold today”

15
Logical Connectives: Disjunction
• Let p and q be propositions. The disjunction of p and q, denoted by p
ν q, is the proposition “p or q”. The disjunction p ν q is false when
both p and q are false and is true otherwise.
• Example:
• P: “India won against France yesterday”
• Q: “India won against West Indies today”
• P v Q: “India won against France yesterday OR India won against West Indies
today”

16
Logical Connectives: Implication
• Let p and q be propositions. The conditional statement p → q, is the
proposition “if p, then q.” The conditional statement is false when p is
true and q is false, and true otherwise. In the conditional statement p
→ q, p is called the hypothesis (or antecedent or premise) and q is
called the conclusion (or consequence).
• Example:
• P: “I am elected”
• Q: “I will lower taxes”
• P → Q: “If I am elected then I will lower taxes”

17
Logical Connectives: Biconditional
• Let p and q be propositions. The biconditional statement p ↔ q is the
proposition “p if and only if q.” The biconditional statement p ↔ q is
true when p and q have the same truth values, and is false otherwise.
Biconditional statements are also called bi-implications.
• p ↔ q has the same truth value as (p → q) Λ (q → p)
• Example:
• P: “You can take the flight”
• Q: “You bought a ticket”
• P ↔ Q: You can take the flight if and only if you bought a ticket.

18
Compound Propositions
• We can use connectives to build up complicated compound
propositions involving any number of propositional variables, then
use truth tables to determine the truth value of these compound
propositions.
• Example: Construct the truth table for (P^Q)v(¬P^ ¬Q)
P Q ¬P ¬Q P^Q ¬P ^ ¬Q (P ^ Q) v (¬P ^ ¬Q)
False False True True False True True
False True True False False False False
True False False True False False False
True True False False True False True

19
Propositional Logic – Translating English Sentences
• English (and every other human language) is often ambiguous. Translating sentences into
compound statements removes the ambiguity.
• Example: How can this English sentence be translated into a logical expression?
• “You cannot ride the roller coaster if you are under 4 feet tall unless you are older than 16 years old.”
• Solution:
• Let q, r, and s represent “You can ride the roller coaster”, “You are under 4 feet tall”, and “You are older
than 16 years old.” The sentence can be translated into:
• (r Λ ¬ s) → ¬q.

20
Propositional Logic
• Example: How can this English sentence be translated into a logical expression?
• “You can access the Internet from campus only if you are a computer science major or you are
not a freshman.”
• Solution: Let a, c, and f represent “You can access the Internet from campus,” “You are a
computer science major,” and “You are a freshman.”
• The sentence can be translated into:
• a → (c ν ¬f).

21
Interpretation of Logic Formulae
• When we see a logic formula, we do not care about the original meaning of each proposition. Rather,
we care about the logic relation between the propositions.
• The main concern is to know if a logic formula is “true” or “false” when the values of the atomic
formulas contained in this formula are given.
• The process for determining the “true/false” of a logic formula based on the “true/false” of the
atomic formulas is called “interpretation”.

22
Well Formed Formulae (WFF)
• A well-formed formula, wff, is a finite sequence of symbols from a given alphabet that is
part of a formal language. A formal language can be identified with the set of formulas in
the language. We represent propositions by formulas called well-formed formulas (wffs).
• A sentence (wff- well formed formula) is defined as follows:
• A symbol is a sentence
• If S is a sentence, then S is a sentence
• If S is a sentence, then (S) is a sentence
• If S and T are sentences, then (S  T), (S  T), (S  T), and (S ↔ T) are sentences
• A sentence results from a finite number of applications of the rules.

23
Properties of WFF in Propositional Logic
• Tautology: A tautology is a logic formula that is always true regardless of the
true/false of the atomic formulas. A tautology is always “valid”.
• Contradiction : If a logic formula is always false regardless of the true/false of
the atomic formulas, we say it is a contradiction. A contradiction is
unsatisfiable.
• Satisfiable: If there exists a set of values for the atomic formulas that makes
the logic formula true, this formula is satisfiable. This set of values is called an
interpretation of the formula.
• Equivalence: The wff V is equivalent to the wff W (written V  W) iff V and W
have the same truth value for each assignment of truth values to the
propositional variables occurring in V and W.
• Example. ¬ A  (B  A)  ¬ A  B and A  ¬ A  B  ¬ B

24
Logical Equivalence
• Logical equivalence is one of the features of propositional logic. Two
propositions are said to be logically equivalent if and only if the
columns in the truth table are identical to each other.
• Let's take two propositions A and B, so for logical equivalence, we can
write it as A⇔B. In below truth table we can see that column for ¬A v
B and A→B, are identical hence A is Equivalent to B

25
Properties of WFFs in Propositional Logic

26
Example

p It is sunny this afternoon


q It is colder tha n yesterday
1. p  q
r We go swimming
s We will take a canoe trip 2. r p
t We will be home by sunset (the conclusion ) 3. r  s
4. st
5. t
propositions

hypotheses
27
Inference Rules
• Logical inference creates new sentences that logically follow from a set of sentences (KB)
• An inference rule is sound if every sentence X it produces when operating on a KB
logically follows from the KB
• i.e., inference rule creates no contradictions
• An inference rule is complete if it can produce every expression that logically follows
from (is entailed by) the KB.
• Logical inference is used to create new sentences that logically follow from a given set of
sentences (KB).
• In artificial intelligence, we need intelligent computers which can create new logic from
old logic or by evidence, so generating the conclusions from evidence and facts is termed
as Inference.

28
Rules of Inference
• Inference rules are the templates for generating valid arguments. Inference
rules are applied to derive proofs in artificial intelligence, and the proof is a
sequence of the conclusion that leads to the desired goal.
• Following are some terminologies related to inference rules:
• Implication: It is one of the logical connectives which can be represented as P → Q.
It is a Boolean expression.
• Converse: The converse of implication, which means the right-hand side
proposition goes to the left-hand side and vice-versa. It can be written as Q → P.
• Contrapositive: The negation of converse is termed as contrapositive, and it can be
represented as ¬ Q → ¬ P.
• Inverse: The negation of implication is called inverse. It can be represented as ¬ P
→ ¬ Q.

29
Common Rules of Inference
• Modus Ponens
• Modus Tollens
• Hypothetical Syllogism
• Disjunctive Syllogism
• Addition
• Simplification
• Resolution

30
1. Modus Ponens:
• it states that if P and P → Q is true, then we can infer that Q will be true. It can be represented as:

Example:
• Statement-1: "If I am sleepy then I go to bed" ==> P→ Q
• Statement-2: "I am sleepy" ==> P
• Conclusion: "I go to bed." ==> Q.
• Hence, we can say that, if P→ Q is true and P is true then Q will be true.
Proof by Truth table: P Q P Q P Q
1 1 1 1 1
1 0 0 1 No need
0 1 1 0 No need
0 0 1 0 No need

31
2. Modus Tollens:
• The Modus Tollens rule state that if P→ Q is true and ¬ Q is true, then ¬ P will also true. It can be
represented as:

• Statement-1: "If I am sleepy then I go to bed" ==> P→ Q


• Statement-2: "I do not go to the bed."==> ~Q
• Statement-3: Which infers that "I am not sleepy" => ~P
• Proof by Truth table:

32
3. Hypothetical Syllogism:
• The Hypothetical Syllogism rule state that if P→R is true whenever P→Q is true, and Q→R is true. It can
be represented as the following notation:
• Example:
• Statement-1: If you have my home key then you can unlock my home. P→Q
Statement-2: If you can unlock my home then you can take my money. Q→R
Conclusion: If you have my home key then you can take my money. P→R

33
4. Disjunctive Syllogism:
• The Disjunctive syllogism rule state that if P∨Q is true, and ¬P is true, then Q will be true. It can be
represented as:

• Statement-1: Today is Sunday or Monday. ==>P∨Q


• Statement-2: Today is not Sunday. ==> ¬P
• Conclusion: Today is Monday. ==> Q

34
5. Addition:
• The Addition rule is one the common inference rule, and it states that If P is true, then P ∨Q will be true.

Example:
• Statement: I have a vanilla ice-cream. ==> P
• Statement-2: I have Chocolate ice-cream.
• Conclusion: I have vanilla or chocolate ice-cream. ==> (P∨Q)

35
6. Simplification:
• The simplification rule state that if P∧ Q is true, then Q or P will also be true. It can be represented as:

36
7. Resolution:
• The Resolution rule state that if P∨Q and ¬ P∧R is true, then Q∨R will also be true. It can be
represented as

37
Limitations of Propositional Logic
• We cannot represent relations like ALL, some, or none with propositional logic. Example:
• All the girls are intelligent.
• Some apples are sweet.
• Hard to identify “individuals.” E.g., Mary, 3
• Can’t directly talk about properties of individuals or relations between individuals.
E.g. “Bill is tall”
• Generalizations, patterns, regularities can’t easily be represented. E.g., all
triangles have 3 sides.
• First-Order Logic (abbreviated FOL or FOPC) is expressive enough to concisely
represent this kind of situation. FOL adds relations, variables, and quantifiers, e.g.,
• “Every elephant is gray”:  x (elephant(x) → gray(x))
• “There is a white alligator”:  x (alligator(X) ^ white(X))

38
Propositional Resolution
• Recall…
• Resolution
• If P v ~Q is true, and Q v R is true, then we can infer P v R.

39
Resolution Principle
• Suppose we have the clauses (P v Q) and (~Q v R).
• Then, using Resolution, we can infer (P v R).
• For Resolution, we merge 2 clauses which differ by 1 atom, where the
atom is in one clause, and its negation is in the other clause.
• Then, that atom (and its negation) can be eliminated when merging
the 2 clauses.

40
Resolution Principle
• Also recall that A v A = A
• Hence, if we have the atom occurring multiple times in the merge
clause, we consider it only once.
• Also note that when we have 2 singleton clauses (P) and (~P), we can
eliminate both and end up with a null clause.

41
Steps for Resolution
• 1. Convert the English propositions into well-formed formulae.
• 2. Convert the well-formed formulae into clauses.
• 3. Negate the conclusion and add it as separate clause(s).
• 4. Perform resolution till you reach the empty clause.

42
Example
• Statements:
• 1. It is not sunny in the afternoon and it is colder than yesterday.
• 2. If we go swimming, it is sunny.
• 3. If we do not go swimming, then we will take a canoe trip.
• 4. If we take a canoe trip, then we will be home by sunset.
• Conclusion: We will be home by sunset.

43
Example – Propositions
• P: It is sunny this afternoon
• Q: It is colder than yesterday
• R: We go swimming
• S: We will take a canoe trip
• T: We will be home by sunset

44
Solution
• Statements:
• 1. It is not sunny in the afternoon and it is colder than yesterday.
• ~P ^ Q
• 2. If we go swimming, it is sunny.
• R -> P
• 3. If we do not go swimming, then we will take a canoe trip.
• ~R -> S
• 4. If we take a canoe trip, then we will be home by sunset.
• S -> T
• Conclusion: We will be home by sunset.
• T
• Negation: ~T
45
List of WFFs
• 1. ~P ^ Q
• ~P
•Q
• 2. R -> P
• ~R v P
• 3. ~R -> S
• RvS
• 4. S -> T
• ~S v T
• 5. ~T
46
List of Clauses
• 1A. ~P
• 1B. Q
• 2. ~R v P
• 3. R v S
• 4. ~S v T
• 5. ~T

47
Resolution by Combining Clauses
• Combining 4 & 5, we get:
• 6. ~S
• Combining 3 & 6, we get:
• 7. R
• Combining 7 & 2, we get:
• 8. P
• Combining 8 & 1A, we get:
• 9. NULL

48
Example #2
• Consider the following clauses:
• 1. {~P v R}
• 2. {~Q v R}
• 3. {P v Q}
• Derive the conclusion {R} from them.

49
Convert to WFFs
• Statements:
• {~P v R}
• {~Q v R}
• {P v Q}
• Conclusion: {R}
• Negation of Conclusion: {~R}

50
Convert to Clauses
• 1. {~P v R}
• 2. {~Q v R}
• 3. {P v Q}
• 4. {~R}

51
Perform Resolution
• Combining 4 & 1, we get:
• 5. ~P
• Combining 5 & 3, we get:
• 6. Q
• Combining 6 & 2, we get:
• 7. R
• Combining 4 & 7, we get:
• 8. NULL

52
Example #3
• Consider the following clauses:
• 1. {P v Q}
• 2. {P v ~Q}
• 3. {~P v Q}
• 4. {~P v ~Q}
• Derive the conclusion {P} from them.

53
Convert to WFFs
• Statements:
• 1. {P v Q}
• 2. {P v ~Q}
• 3. {~P v Q}
• 4. {~P v ~Q}
• Conclusion: {P}

54
Convert to Clauses
• 1. {P v Q}
• 2. {P v ~Q}
• 3. {~P v Q}
• 4. {~P v ~Q}
• 5. {~P}

55
Perform Resolution
• Combining 5 & 2, we get:
• 6. ~Q
• Combining 6 & 1, we get:
• 7. P
• Combining 7 & 5, we get:
• 8. NULL

56
Example #4
• Show that (P -> (Q -> P)) using the Resolution Principle.

57
Convert to WFF
• Conclusion: (P -> (Q -> P))
• Negation of Conclusion: ~(P -> (Q -> P)) (~A v B = A->B)
• ~(~P v (Q->P))
• ~(~P v (~Q v P)) (~(A v B) = ~A ^ ~B)
• ~~P ^ ~(~Q v P)
• P ^ (~~Q ^ ~P)
• P ^ Q ^ ~P

58
Convert to Clauses
• 1A. P
• 1B. Q
• 1C. ~P

59
Perform Resolution
• Combining 1A & 1C, we get:
• 2. NULL

60
Revision – Convert the following to clausal
form
• (P ^ Q) -> (R v S)
• ~(P ^ Q) v (R v S)
• (~P v ~Q) v (R v S)
• (P v Q) -> (R v S)
• ~(P v Q v R)
• ~(P ^ Q ^ R)
• (P ^ Q) <-> R

61
Revision: Application of Resolution
• {P v Q v ~R} and {R v S}
• P v Q v ~R + R v S
• PvQvS
• {P v Q v R} and {R v ~S v ~T}
• {Q v ~Q} and {Q v ~Q}
• {~P v Q v R} and {P v ~Q v ~R}

62
Contents
• First Order Logic

63
First Order Logic / Predicate Logic
• First-order logic is another way of knowledge representation in AI. It is
an extension to propositional logic.
• FOPL is also known as Predicate logic or First-order predicate logic.
• First-order logic is a powerful language that develops information
about the objects in a more easy way and can also express the
relationship between those objects.

64
First Order Logic
• Constants are objects: john, apples
• Predicates are properties and relations:
• likes(john, apples)
• Functions transform objects:
• likes(john, fruit_of(apple_tree))
• Variables represent any object: likes(X, apples)
• Quantifiers qualify values of variables
• True for all objects (Universal): X. likes(X, apples)
• Exists at least one object (Existential): X. likes(X, apples)

65
Example: FOL Sentence
• “Every rose has a thorn”

• For all X
• if (X is a rose)
• then there exists Y
• (X has Y) and (Y is a thorn)

66
Elements and Syntax of First Order Logic
• Constant
• 1, 2, A, John, Mumbai, cat,....
• Variables
• x, y, z, a, b,....
• Predicates
• isBrother(x, y), isFather(x, y), likes(x, y), rose(x), …
• Function
• Sqrt(x), fruit_of(apple_tree), ....
• Connectives
• ∧, ∨, ¬, ⇒, ⇔
• Equality
• ==, !=
• Quantifier
• ∀, ∃

67
Usage
• Constants are used for labelling specific entities in FOL
• Predicates mainly are verbs.
• NOTE: Constants are NOT predicates
• Eg. King(x) is a predicate (let it be “x is a King”).
• If x = John, the predicate becomes King(John), which maps to the
proposition “John is a King”.
• However, John(x) is NOT a predicate (i.e. there is no such thing as “x is
a John”).

68
Terms and Atomic Sentences
• A term is a logical expression that refers TERM to an object. Constant
symbols are therefore terms, but it is not always convenient to have a
distinct symbol to name every object.
• Example: “King John’s left leg” = LeftLeg(King John) != LeftLeg(King(John))
• Atomic sentences are the most basic sentences of first-order logic.
• These sentences are formed from a predicate symbol followed by a
parenthesis with a sequence of terms.
• Atomic sentences are represented as “Predicate(term1, term2, …, termN).
• Example:
• Ravi and Ajay are brothers = Brothers(Ravi, Ajay).
• Tom is a cat = cat (Tom)

69
Subjects and Predicates
• Complex sentences are made by combining atomic sentences using connectives.
• First-order logic statements can be divided into two parts:
• Subject: Subject is the main part of the statement.
• Predicate: A predicate can be defined as a relation, which binds two atoms together
in a statement.
• Consider the statement: “x is an integer.” Subject = “x”, and Predicate = “is an integer”.
• In the sentence “The dog is sleeping”, Subject = “The dog”, and Predicate = “is sleeping”.

70
Complex Sentences
• Complex sentences are made by combining atomic sentences using
connectives.
• Sandeep lectures in AI and NLP.
• Lectures(Sandeep, AI) ^ Lectures(Sandeep, NLP)
• If Sandeep isn’t lecturing AI, he is substituted by Mausam.
• ~Lectures(Sandeep, AI) -> Lectures(Mausam, AI)
• If Sandeep isn’t lecturing NLP, he is substituted by Partha.
• ~Lectures(Sandeep, NLP) -> Lectures(Partha, NLP)

71
Functions
• FOL also uses functions.
• Functions take constants / variables as input and return a constant /
value as the output.
• Examples:
• 1. “The cost of a salmon platter at Wasabi Sushi is 18.50 €.”
• costOf(salmon platter, Wasabi Sushi) = 18.50 €

72
Function (Example #2)
• 2. “The cost of the salmon platter is more than the cost of the spindle
platter at Wasabi Sushi”.
• greaterThan(costOf(salmon platter, Wasabi Sushi), costOf(spindle platter,
Wasabi Sushi))

73
Quantifiers
• There are two types of quantifier:
• Universal Quantifier(∀)- (for all, everyone, everything)
• Specifies that the statement within its range is true for everything or every
instance of a particular thing.
• In universal quantifier we use "→" or implication.
• Existential quantifier(∃) - (for some, at least one)
• Express that the statement within its scope is true for at least one instance of
something.
• In Existential quantifier we always use AND or Conjunction symbol (∧)

74
Quantifiers
• Universal quantifiers are often used with “implies” to form “rules”:
(x) student(x)  smart(x) means “All students are smart”
• Universal quantification is rarely used to make blanket statements
about every individual in the world:
(x)student(x)smart(x) means “Everyone in the world is a student and is
smart”
• Existential quantifiers are usually used with “and” to specify a list of
properties about an individual:
(x) student(x)  smart(x) means “There is a student who is smart”.

75
Quantifier Scope
• Switching the order of universal quantifiers does not change the
meaning:
• (x)(y)P(x,y) ↔ (y)(x) P(x,y)
• Similarly, you can switch the order of existential quantifiers:
• (x)(y)P(x,y) ↔ (y)(x) P(x,y)
• Switching the order of universals and existentials does change
meaning:
• Everyone likes someone: (x)(y) likes(x,y)
• Someone is liked by everyone: (y)(x) likes(x,y)

76
Combining Quantifiers
• x y Loves(x,y)
• For everyone (“x”) there is someone (“y”) that they love.
• y x Loves(x,y)
• There is someone (“y”) who is loved by everyone (“X”)
• Clearer with parentheses: y(x Loves(x,y) )

77
Translate English to FOL
1. Every gardener likes the sun.
x gardener(x)  likes(x,Sun)
2. You can fool some of the people all of the time.
x t person(x) time(t)  can-fool(x,t)
3. You can fool all of the people some of the time.
x t (person(x)  time(t) can-fool(x,t))
x (person(x)  t (time(t) can-fool(x,t)))
4. All purple mushrooms are poisonous.
x (mushroom(x)  purple(x))  poisonous(x)
5. No purple mushroom is poisonous.
x purple(x)  mushroom(x)  poisonous(x)
x (mushroom(x)  purple(x))  poisonous(x)
6. There are exactly two purple mushrooms.
x y mushroom(x)  purple(x)  mushroom(y)  purple(y) ^ (x=y)  z (mushroom(z)  purple(z))  ((x=z)  (y=z))
7. Clinton is not tall.
tall(Clinton)
8. X is above Y iff X is on directly on top of Y or there is a pile of one or more other objects directly on top of one another starting with X and ending with Y.
x y above(x,y) ↔ (on(x,y)  z (on(x,z)  above(z,y)))

78
HW: Translate to First Order Logic
1. Everything is bitter or sweet
2. Either everything is bitter or everything is sweet
3. There is somebody who is loved by everyone
4. Nobody is loved by no one
5. If someone is noisy, everybody is annoyed
6. Frogs are green.
7. Frogs are not green.
8. No frog is green.
9. Some frogs are not green.
10. A mechanic likes Bob.
11. A mechanic likes herself.
12. Every mechanic likes Bob.
13. Some mechanic likes every nurse.
14. There is a mechanic who is liked by every nurse.
79
Relationship between ∀ and ∃
• The two quantifiers are actually intimately connected with each other, through
negation.
• ∀x ¬ Likes(x, Parsnips) ≡ ¬∃ x Likes(x, Parsnips)
• ∀x Likes(x, IceCream) ≡ ¬∃ x ¬ Likes(x, IceCream)
• Because ∀ is really a conjunction over the universe of objects and ∃ is a
disjunction, they obey De Morgan’s rules.
• The De Morgan rules for quantified and unquantified sentences are as follows:
• ∀ x ¬ P ≡ ¬∃ x P ¬ (P ∨ Q) ≡ ¬ P ∧ ¬ Q
• ¬∀ x P ≡ ∃ x ¬ P ¬ (P ∧ Q) ≡ ¬ P ∨ ¬ Q
• ∀x P ≡ ¬∃ x ¬ P P∧ Q ≡ ¬ ( ¬ P ∨ ¬ Q)
• ∃ x P ≡ ¬∀ x ¬ P P∨ Q ≡ ¬ ( ¬ P ∧ ¬ Q)

80
Revision
“All things in the bag are red”
Which of these translations is correct?

1. ∃ X (in_bag(X) → red(X))

2. ∀ X (red(X) → in_bag(X))

3. ∀ X (∀ Y (bag(X) ∧ in_bag(Y,X) → red(Y)))

81
Using FOL – Assertions and Queries
• Sentences are added to a knowledge base using TELL. Such sentences are called
assertions. For example, we can assert that John is a king, Richard is a person,
and all kings are persons:
• TELL(KB, King(John))
• TELL(KB, Person(Richard))
• TELL(KB, ∀ x King(x) ⇒ Person(x))
• We can ask questions of the knowledge base using ASK. For example, ASK(KB,
King(John)) returns true.
• Questions asked with ASK are called queries or goals.

82
Using FOL – Assertions and Queries
• Any query that is logically entailed by the knowledge base should be
answered affirmatively.
• Example: ASK(KB, Person(John)) = True
• We can also ask quantified queries like ASK(KB, ∃x Person(x)). However,
that is not quite useful. In order to find out whether the sentence is true,
we use the function ASKVARS
• ASKVARS(KB, Person(x))
• ASKVARS(KB, Person(x)) returns true for the substitutions x=John and x =
Richard.
• These answers are called substitution and the list of such answers is
called the binding list.

83
Revision – Scenario
• Consider the following situation.
• Alice likes mathematics and she also likes stories.
• If someone likes mathematics, then they also like algebra
• If someone likes algebra and also likes physics, then they will graduate
• Alice does not like stories or she likes physics.
• Alice does not like chemistry and also history.
• If someone will graduate, their GPA is more than 4.

84
Revision – Question
• For each of the following, classify it as either
• A function,
• A variable,
• A predicate, or
• A constant.
• For functions and predicates, define / describe them.

85
Revision – Terms
• Alice
• likes
• mathematics
• stories
• algebra
• someone
• physics
• graduate
• GPA

86
Revision – Solution
• Alice – Constant
• Likes – Predicate. Eg. Likes(x, y) = “x likes y”.
• Mathematics – Constant
• Stories – Constant (Can also be a predicate of the form story(x) = “x is a story”.)
• Algebra – Constant
• Someone – Variable (in FOL, it is usually given as “x”)
• Physics – Constant
• Graduate – Predicate. Eg. Graduate(x) = “x graduates”
• GPA – Function. Eg. GPA(x) will return the GPA of the variable x.

87
HW: Translate to First Order Logic
1. Everything is bitter or sweet
2. Either everything is bitter or everything is sweet
3. There is somebody who is loved by everyone
4. Nobody is loved by no one
5. If someone is noisy, everybody is annoyed
6. Frogs are green.
7. Frogs are not green.
8. No frog is green.
9. Some frogs are not green.
10. A mechanic likes Bob.
11. A mechanic likes herself.
12. Every mechanic likes Bob.
13. Some mechanic likes every nurse.
14. There is a mechanic who is liked by every nurse.
88
Solutions – Bitter / Sweet
1. Everything is bitter or sweet
1. ∀x bitter(x) v sweet(x)
2. Either everything is bitter or everything is sweet
1. (∀x bitter(x) ^ ~sweet(x)) v (∀y ~bitter(y) ^ sweet(y))

89
Solution – Loves and Noise
1. There is somebody who is loved by everyone
1. ∃x∀y loves(x, y)
2. Nobody is loved by no one
1. ~∃x ~∃ y loves(x, y)
3. If someone is noisy, everybody is annoyed
1. ∃x(noisy(x) -> ∀y annoyed(y))

90
Solution – Green Frogs
1. Frogs are green.
1. Frog(x) -> Green(x)
2. Frogs are not green.
1. ∀x (Frog(x) -> ~Green(x))
3. No frog is green.
1. ~∃x (Frog(x) ^ Green(x))
4. Some frogs are not green.
1. ∃x (Frog(x) ^ ~Green(x))

91
Solutions – Mechanics and Nurses
1. A mechanic likes Bob.
1. ∃x (Mechanic(x) ^ Likes(x, Bob))
2. A mechanic likes herself.
1. ∃x (Mechanic(x) ^ Likes(x, x))
3. Every mechanic likes Bob.
1. ∀x (Mechanic(x) -> Likes(x, Bob))
4. Some mechanic likes every nurse.
1. ∃x (Mechanic(x) ^ ∀y (Nurse(y) -> Likes(x, y))
5. There is a mechanic who is liked by every nurse.
1. ∃x (Mechanic(x) ^ ∀y (Nurse(y) -> Likes(y, x))
92
Inference Rules in First Order Logic
• FOL has the same set of inference rules as Propositional Logic.
• In addition to the above, FOL also has the following inference rules:
• Universal generalization
• Universal instantiation
• Existential instantiation
• Existential Introduction

93
Universal Generalization
• If P(c) is true for any element c, then ∀x, P(x) is true.
• Example:
• P(c): “A byte, c, contains 8 bits”.
• ∀x, P(x): “All bytes contain 8 bits.”

94
Universal Instantiation
• If ∀x P(x) is true, then P(c) is also true, where c is in the domain of x.
• Example:
• P(x): “x likes ice-cream”
• ∀x P(x): “Everyone likes ice-cream”
• P(John): “John likes ice-cream”

95
Existential Instantiation
• P(c) can be inferred from ∃x P(x) for a new symbol c, for which P(c) is
true.
• Example:
• P(x): “There exists a crown on John’s head”
• ∃x P(x): Crown(x) ^ OnHead(x, John)
• P(c): “Crown(c) ^ OnHead(c, John)” as long as c is not in the KB.

96
Existential Introduction
• If there is an element c in the universe that has a property P(c), then,
we can infer that ∃x P(x).
• Example:
• P(c): “C scored more than 20 marks in Test 2 of CSE 5006”.
• ∃x P(x): “X scored more than 20 marks in Test 2 of CSE 5006”.

97
CNF and DNF
• Every logic expression can be represented as either a conjunctive
normal form (CNF) or a disjunctive normal form (DNF).
• CNF is a conjunction of disjunctions, where each disjunction is called
a clause.
• DNF is a disjunction of conjunctions, where each conjunction is called
a term.

98
Resolution Principle
• Resolution takes 2 clauses and produces a new clause containing all
the literals of the 2 original clauses except the complimentary
literals.

99
Steps for Resolution
• 1. Convert the English statements to FOL.
• 2. Convert the FOL statements to CNF.
• 3. Negate the conclusion and add the corresponding clauses to the
KB.
• 4. Resolve using Resolution to get a NULL clause.

100
Steps to convert FOL to CNF
• 1. Eliminate biconditional implication
• A <-> B = (A -> B) ^ (B -> A)
• 2. Eliminate implications
• A -> B = ~A v B
• 3. Move negation inwards:
• ~(~A) = ~~A = A
• ~(A v B) = ~A ^ ~B
• 4. Use the distributive law to create more inferences.
• A v (B ^ C) = (A v B) ^ (A v C)
• 5. Rename / standardize variables
• 6. Eliminate existential instantiation
• 7. Drop Universal quantifiers

101
Eliminating Existential instantiation
• Recall:
• ∃x ~P(x) = ~∀x P(x)
• ~∃x ~P(x) = ∀x P(x)
• We convert the existential quantifiers to universal quantifiers in this
step.

102
Example #1
• Statements:
• Ravi likes all food
• Apples and chicken are food
• Anything anyone eats and is not killed is food
• Ajay eats peanuts and is still alive
• Rita eats everything that Ajay eats.
• Everything which is not killed is alive
• Conclusion: Ravi likes peanuts.

103
Conversion to FOL
• Ravi likes all food
• ∀x Food(x) -> likes(Ravi, x)
• Apples and chicken are food
• Food(Apples) ^ Food(chicken)
• Anything anyone eats and is not killed is food
• ∀x∀y (eats(x, y) ^ ~killed(x)) -> Food(y)
• Ajay eats peanuts and is still alive
• Eats(Ajay, peanuts) ^ ~killed(Ajay)
• Rita eats everything that Ajay eats.
• ∀x eats(Ajay, x) -> eats(Rita, x)
• Everything which is not killed is alive.
• ∀x ~killed(x) -> alive(x)
• Conclusion: Ravi likes peanuts.
• Likes(Ravi, peanuts)
• ~Likes(Ravi, peanuts)

104
Conversion to CNF
• ∀x Food(x) -> likes(Ravi, x)
• ∀x ~Food(x) v likes(Ravi, x)
• ~Food(x) v likes(Ravi, x)
• Food(Apples) ^ Food(chicken)
• Food(Apples)
• Food(Chicken)
• ∀x∀y (eats(x, y) ^ ~killed(x)) -> Food(y)
• ∀x∀y ~(eats(x, y) ^ ~killed(x)) v Food(y)
• ∀x∀y (~eats(x, y) v ~~killed(x)) v Food(y)
• ∀x∀y ~eats(x, y) v killed(x) v Food(y)
• ~eats(x, y) v killed(x) v Food(y)
105
Conversion to CNF (Contd.)
• Eats(Ajay, peanuts) ^ ~killed(Ajay)
• Eats(Ajay, peanuts)
• ~killed(Ajay)
• ∀x eats(Ajay, x) -> eats(Rita, x)
• ∀x ~eats(Ajay, x) v eats(Rita, x)
• ∀x ~killed(x) -> alive(x)
• ∀x ~~killed(x) v alive(x)
• killed(x) v alive(x)
• ~Likes(Ravi, peanuts

106
Clauses
• 1. ~Food(x) v likes(Ravi, x)
• 2A. Food(Apples)
• 2B. Food(Chicken)
• 3. ~eats(x, y) v killed(x) v Food(y)
• 4A. Eats(Ajay, peanuts)
• 4B. ~killed(Ajay)
• 5. ∀x ~eats(Ajay, x) v eats(Rita, x)
• 6. killed(x) v alive(x)
• 7. ~Likes(Ravi, peanuts)
107
Resolution
• Combining 7 & 1, and substitute x = peanuts, we get:
• 8. ~Food(peanuts)
• Combining 8 & 3, and substitute x = Ajay and y = peanuts, we get:
• 9. ~eats(Ajay, peanuts) v killed(Ajay)
• Combining 9 & 4A, we get:
• 10. killed(Ajay)
• Combining 10 & 4B, we get:
• 11. NULL

108
Example #2
• Premises:
• All men are mortal
• Socrates is a man
• Conclusion: Socrates is mortal.

109
Conversion to FOL
• ∀x Man(x) -> Mortal(x)
• Man(Socrates)
• Negation of Conclusion:
• ~Mortal(Socrates)

110
Conversion to CNF
• 1. ~Man(x) v Mortal(x)
• 2. Man(Socrates)
• 3. ~Mortal(Socrates)

111
Resolution Man (Socrates)
~Man(Socrates) V
Mortal (Socrates)

~Mortal (Socrates) Mortal(Socrates)

Empty

112
Resolution 2 ~Mortal (Socrates)
~Man(Socrates) v
Mortal (Socrates)

Man(Socrates) ~Man(Socrates)

Empty

113
Example #3
• Axioms:
• All hounds howl at night.
• Anyone who has any cats will not have any mice.
• Light sleepers do not have anything which howls at night.
• John has either a cat or a hound.
• To prove: If John is a light sleeper, then John does not have any mice.

114
Solution
• 1. Convert to FOL
• 2. Convert to CNF
• 3. Negate Conclusion and List Clauses
• 4. Do resolution

115
Conversion to FOL
• 1. All hounds howl at night.
• FOL: ForAll x hound(x) -> howl(x)
• 2. Anyone who has any cats will not have any mice.
• FOL: ForAll x ForAll y (cat(y) ^ has(x,y)) -> ~Exists z (mouse(z) ^ has(x,z))
• 3. Light sleepers do not have anything which howls at night.
• FOL: ForAll x LS(x) -> ~Exists y (howl(y) ^ has(x,y))
• 4. John has either a cat or a hound.
• FOL: Exists x ((cat(x) v hound(x)) ^ has(John, x))
• Conclusion: If John is a light sleeper, then John does not have any mice
• FOL: LS(John) -> ~Exists x (mouse(x) ^ has(John, x))
• NEG: ~[LS(John) -> ~Exists x (mouse(x) ^ has(John, x))]

116
Conversion to CNF
• 1. FOL: ForAll x hound(x) -> howl(x)
• CNF: ~hound(x) v howl(x)
• 2. ForAll x ForAll y (cat(y) ^ has(x,y)) -> ~Exists z (mouse(z) ^ has(x,z))
• CNF: ForAll x ForAll y (cat(y) ^ has(x,y)) -> ForAll z ~(mouse(z) ^ has(x,z))
• CNF: ForAll x ForAll y ForAll z (cat(y) ^ has(x,y)) -> (~(mouse(z) ^ has(x,z)))
• CNF: ~(cat(y) ^ has(x,y)) v (~(mouse(z) ^ has(x,z)))
• CNF: ~cat(y) v ~has(x,y) v ~mouse(z) v ~has(x,z)

117
Conversion to CNF
• 3. ForAll x LS(x) -> ~Exists y (howl(y) ^ has(x,y))
• CNF: LS(x) -> ForAll y ~(howl(y) ^ has(x, y))
• CNF: ForAll y LS(x) ->(~howl(y) v ~has(x, y))
• CNF: ~LS(x) v ~howl(y) v ~has(x, y)
• 4. Exists x ((cat(x) v hound(x)) ^ has(John, x))
• CNF: has(John, P) ^ (cat(P) v hound(P))

118
Conversion to CNF
• 5. ~[LS(John) -> ~Exists x (mouse(x) ^ has(John, x))]
• CNF: ~[LS(John) -> ~Exists x (mouse(x) ^ has(John, x))]
• CNF: ~[LS(John) -> ForAll x ~(mouse(x) ^ has(John, x))]
• CNF: ~[ForAll x LS(John) -> ~(mouse(x) ^ has(John, x))]
• CNF: ~[~LS(John) v ~mouse(x) v ~has(John, x)]
• CNF: LS(John) ^ mouse(x) ^ has(John, x)

119
List of Clauses
• 1. ~hound(x1) v howl(x1)
• 2. ~cat(y2) v ~has(x2, y2) v ~mouse(z2) v ~has(x2, z2)
• 3. ~LS(x3) v ~howl(y3) v ~has(x3, y3)
• 4A. has(John, P)
• 4B. cat(P) v hound(P)
• 5A. LS(John)
• 5B. mouse(x5)
• 5C. has(John, x5)

120
Resolution
• Combining 1 & 4B, substituting x1 = P, we get:
• 6. howl(P) v cat(P)
• Combining 2 & 5B, substituting x5 = z2, we get:
• 7. ~cat(y2) v ~has(x2, y2) v ~has(x2, z2)
• Combining 7 & 5C, substituting x2 = John, x5 = z2, we get:
• 8. ~cat(y2) v ~has(John, y2)
• Combining 6 & 8, y2 = P, we get:
• 9. howl(P) v ~has(John, P)
• Combining 4A & 9, we get:
• 10. howl(P)
• Combining 10 & 3, substituting y3 = P, we get:
• 11. ~LS(x3) v ~has(x3, P)
• Combining 11 & 4A, substituting x3 = John, we get:
• 12. ~LS(John)
• Combining 12 & 5a, we get NULL

121
Example #4
• Statements:
• Marcus was a Man
• Marcus was a Pompeian
• All Pompeians were Romans
• Caesar was a ruler
• All Romans were either loyal to Caesar or hated him
• Everyone is loyal to someone
• Romans only try to assassinate rulers that they aren’t loyal to
• Marcus tried to assassinate Caesar.
• Prove that: Marcus hated Caesar.

122
Conversion to FOL
• Marcus was a Man
• Man(Marcus)
• Marcus was a Pompeian
• Pompeian(Marcus)
• All Pompeians were Romans
• ForAll x Pompeian(x) -> Roman(x)
• Caesar was a ruler
• Ruler(Caesar)
• All Romans were either loyal to Caesar or hated him
• ForAll x Roman(x) -> (loyal(x, Caesar) v hate(x, Caesar)
• Everyone is loyal to someone
• ForAll x ThereExists y loyal(x, y)
• Romans only try to assassinate rulers that they aren’t loyal to
• ForAll x ForAll y roman (x) ^ ruler(y) ^ assassinate(x, y) -> ~loyal(x, y)
• Marcus tried to assassinate Caesar.
• Assassinate(Marcus, Caesar)

123
Conversion to CNF
• Man(Marcus)
• Pompeian(Marcus)
• ~Pompeian(x1) v Roman(x1)
• Ruler(Caesar)
• ~Roman(x2) v loyal(x2, Caesar) v hate(x2, Caesar)
• loyal(x3, Y1)
• ~Roman (x) v ~Ruler(y) v ~Assassinate(x, y) v ~loyal(x, y)
• Assassinate(Marcus, Caesar)

124
Clauses
• 1. Man(Marcus)
• 2. Pompeian(Marcus)
• 3. ~Pompeian(x1) v Roman(x1)
• 4. Ruler(Caesar)
• 5. ~Roman(x2) v loyal(x2, Caesar) v hate(x2, Caesar)
• 6. loyal(x3, Y1)
• 7. ~Roman (x4) v ~Ruler(y2) v ~Assassinate(x4, y2) v ~loyal(x4, y2)
• 8. Assassinate(Marcus, Caesar)
• 9. ~hate(Marcus, Caesar)
125
Resolution
• Combining 9 with 5 and substituting x2 = Marcus, we get:
• 10. ~Roman(Marcus) v loyal(Marcus, Caesar)
• Combining 10 with 3 and substituting x1 = Marcus, we get;
• 11. ~Pompeian(Marcus) v loyal(Marcus, Caesar)
• Combining 11 with 2, we get:
• 12. loyal(Marcus, Caesar)
• Combining 2 with 3 and substituting x1 = Marcus, we get:
• 13. Roman(Marcus)
• Combining 4 with 7 and substituting y2 = Caesar, and x4 = Marcus, we get;
• 14. ~Roman (Marcus) v ~Assassinate(Marcus, Caesar) v ~loyal(Marcus, Caesar)
• Combining 14 with 13, we get:
• 15. ~Assassinate(Marcus, Caesar) v ~loyal(Marcus, Caesar)
• Combining 15 with 12, we get:
• 16. ~Assassinate(Marcus, Caesar)
• Combining 16 with 8, we get NULL
126

You might also like