14-Knowledge Representation - Part2
14-Knowledge Representation - Part2
Spring Semester
Natural Language
Processing
Dr. Wafaa Samy
Dr. Hanaa Eissa
Knowledge Representation
(Part 2)
Lecture (14)
2
Contents
• Knowledge Representation (KR)
o First Order Predicate Logic (FOPL)
3
First Order Predicate Logic (FOPL)
• More expressive logic than propositional logic.
• First-order predicate logic (FOPL) models the world
in terms of FOPL Symbols:
1. Constants (or objects)
2. Predicates
3. Functions
4. Variables
5. Quantifiers
4
Constants
• Constants (or objects) refer to a single object
in our domain.
o Objects, which are things with individual
identities.
o Constants are objects: John, apples, etc.
• Examples:
o Adel, Bicycle, Scooter, Cairo, Students, lectures,
companies, cars , etc.
5
Predicates
• Expresses a relationship between objects in a given
domain, or defines properties of those objects.
1. Relations that hold among sets of objects.
2. Properties of objects that distinguish them from other objects.
• With our constants and predicates defined, we can now use the
predicates to define relationships and properties of the constants
(also called Atomic sentences).
• Predicates are properties and relations:
John likes apples.
likes(John, apples)
• Examples:
o Relationships: owns, rides, knows, brother-of, bigger-than,
outside, part-of, has-color, occurs-after, visits, precedes, etc.
o Properties: person, blue, oval, even, large, sunny, book, two-
6 wheeled, etc.
Predicates: Examples
• Adel is a person.
o Person(Adel)
• Ali is a person.
o Person (Ali)
• Adel and Ali both know each other.
o Knows( Adel , Ali )
o Knows( Ali , Adel )
• Ahmed rides a bicycle.
o Rides (Ahmed, Bicycle)
• Ali rides a scooter.
o Rides (Ali, Scooter)
• A Bicycle is two-wheeled.
o Two-Wheeled (Bicycle)
7
Example (1)
• Represent the following statement in FOPL:
ElShabrawy serves vegetarian food.
• Solution:
Serves (ElShabrawy, VegetarianFood)
8
Functions
• Functions, which are a subset of relations where there is
only one “value” for any given “input”.
• Functions transform objects:
o Allows to transform a constant into another constant.
• Examples of Functions:
o father-of, best-friend, second-half, one-more-than,
fruit_of, etc.
• Examples:
o The brother_of function is demonstrated as:
Ahmed knows Hasan's brother
Knows(Ahmed, brother_of(Hasan))
9
o likes(John, fruit_of(apple_tree))
Variables
• Abstract sentence that don’t specify specific
objects.
• Variables represent any object:
likes(X, apples)
• For example:
If x knows Ali then x is a person.
Knows(x, Ali) ⇒ Person(x)
10
Compound Sentences
• A Compound sentence can be built from atomic
sentences using the connectives:
∧ AND
∨ OR
¬ NOT
⇒ Implication or Logical Conditional (if … then …)
⇔ Equivalence (if and only if)
11
Examples
• Ahmed and Ali know one another.
Knows (Ahmed, Ali) ∧ Knows (Ali, Ahmed)
12
Operator Precedence
()
¬ Highest
∧
∨
⇒
⇔ Lowest
• Person (Ahmed) ∧ Rides (Ahmed, Scooter) ∨ Rides
(Ahmed, Bicycle)
• Grouped as:
( Person (Ahmed) ∧ Rides (Ahmed, Scooter) ) ∨ Rides
(Ahmed, Bicycle)
13
Semantics of FOPL
• FOPL sentences can be assigned a value True
or False based on whether the proposition
they encode are in accordance with the world
or not.
14
Atomic and Logical Connectives
• Atomic formulas are taken to be true if they are literally
present in the knowledge base or if they can be inferred from
other formula that are in the knowledge base.
T F F F T F F
15
T T F T T T T
Example (2)
• Write the truth table of the following formulas:
¬p ∧ q
(p ∨ q) ∧ ¬p
16
Example (2): Solution
• Write the truth table of the following formulas:
¬p ∧ q
(p ∨ q) ∧ ¬p
p q ¬p ¬p∧q p∨q (p ∨ q) ∧ ¬ p
F F T F F F
F T T T T T
T F F F T F
T T F F T F
Since, the truth table values of both statements is the same. Thus,
17
the two statements ¬p ∧ q and (p ∨ q) ∧ ¬p are logically equivalent.
Quantifiers
• In first-order predicate logic, there are two quantifiers:
o For all: ∀
o There exists: ∃
• Quantifiers qualify values of variables.
o True for all objects (Universal): X. likes(X, apples)
o Exists at least one object (Existential): X. likes(X, apples)
• Examples:
o There exists x, such that x is a person.
∃ x. Person (x)
o For all persons, there exists someone that knows Ahmed
or Ali.
18
∀x. ∃ y. Person (x) ∧ (Knows (y, Ali) ∨ Knows (y, Ahmed))
Quantifiers (Cont.)
• Universal quantification
o x. P(x) means that P is true for all values of x in the
domain associated with that variable.
o Example: x. dolphin(x) ⇒ mammal(x)
• Existential quantification
o x. P(x) means that P is true for some values of x in the
domain associated with that variable.
o Example: x. mammal(x) lays-eggs(x)
o Permits one to make a statement about some object
without naming it.
19
Quantifiers (Cont.)
• 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 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 object:
x. student(x) smart(x) means “There is a student who is smart”.
• A common mistake is to represent this English sentence as the FOPL
sentence: x. student(x) ⇒ smart(x)
But what happens when there is a person who is not a student?
20
Common mistakes to avoid.
Quantifier Scope
• Switching the order of universal quantifiers does not
change the meaning:
x. y. P(x,y) ⇔ y. x. P(x,y)
22
User provides
• Constant symbols, which represent individuals in the world:
o Mary
o 3
o Green
• Non-Logical Symbols:
o Function symbols
o Predicate symbols
24
How Sentences are built?
• A term (denoting a real-world individual) is a constant
symbol, a variable symbol, or a function.
x and f(x1, ..., xn) are terms, where each xi is a term.
• An atomic sentence which has value true or false.
• A complex sentence is formed from atomic sentences
connected by the logical connectives:
P, P Q, P Q, P ⇒ Q, P ⇔ Q where P and Q are sentences.
• A quantified sentence adds quantifiers and .
• A well-formed formula (wff) is a sentence containing no
“free” variables.
o That is, all variables are “bound” by universal or existential quantifiers.
x. P(x,y) has x bound as a universally quantified variable,
25
but y is free.
Context-Free Grammar for FOPL
Formula → AtomicFormula
| Formula Connective Formula
| Quantifier Variable . Formula
| ¬ Formula
| (Formula)
AtomicFormula → Predicate (Term,…)
Term → Function (Term,…)
| Constant
| Variable
26
Context-Free Grammar for FOPL
(Cont.)
• Connective → ∨ | ∧ | ⇒ | ⇔
• Quantifier → ∀ | ∃
• Constant → Adel | Cairo|…
• Variable → x | y | …
• Predicate → Person |…
• Function → locationOf | …
27
Example (3)
• Represent the following statement in FOPL:
28
Semantics of FOPL
• FOPL sentences can be assigned a value True or False based
on whether the proposition they encode are in accordance
with the world or not.
• Example: Giza is near Cairo.
Near (locationOf(Giza), locationOf(Cairo))
• The meaning of this formula arises from the relationship
between the terms LocationOf (Giza), LocationOf (Cairo), the
predicate Near, and the objects and relation they correspond to
in the world being modeled.
• Specifically, this sentence can be assigned a value of True or
False based on whether or not the real Giza is actually close to
29 Cairo or not.
FOPL: Variables and Existential
Quantifier
• A restaurant that serves grilled food near Pyramids.
∃x. Restaurant(x) ∧ Serves (x, GrilledFood)
∧ Near (locationOf(x),locationOf(Pyramids))
• Note: for this sentence to be true there must be at least one object
such that if we were to substitute it for the variable x, the resulting
sentence would be true. We can substitute variable by a constant ,
predicate, function or another variable. So,
same expression can be used many times
by substitute the variables in expression.
Note: We removed ∃x and replace x by
• Substitute x by Tikka ( { Tikka/x} ) Tikka.
Restaurant(Tikka) ∧ Serves (Tikka, GrilledFood)
∧ Near (locationOf(Tikka),locationOf(Pyramids))
Note: Based on the ∧ operator, this sentence will be true if
30
all of its three component atomic formulas are true.
FOPL: Variables and the Universal
Quantifier
• All fast-food restaurants serve grilled food.
∀x. FastFoodRestaurant(x) ⇒ Serves(x, GrilledFood)
• Note: for this sentence to be true, it must be the case that every
substitution of a known object for x must result in a sentence that
is true. We can substitute variable by a
constant, predicate, function, or
another variable. Note: We removed
∀x and replace x by Tikka. Then,
• Substitute x by Tikka ( { Tikka/x} ) removed ∀x and replace x by Chilis.
Given α, α ⇒ β, then β
32
Example (4): Inference in First Order
Predicate Logic using Modus Ponens
FastFoodRestaurant(Tikka)
∀x. FastFoodRestaurant (x) ⇒ Serves(x, GrilledFood)
___________________________________________
First, unification of these expressions by
find necessary substitutions of variables
Serves (Tikka, GrilledFood) to make these expressions matched. We
removed ∀x and replace x by Tikka.
Second, apply Modus Ponens inference
rule.
• The formula FastFoodRestaurant(Tikka) matches the
antecedent of the rule, thus allowing us to use modus
33 ponens to conclude Serves(Tikka , GrilledFood).
Unification
• Unification is an algorithm for determining the
substitution needed to make 2 predicate calculus
expressions match.
• The algorithm returns fail if the 2 expressions cannot be
matched.
• Substitution is finite set of bindings of the form: E/x
put the expression E in place of the variable x.
We can substitute variables only.
• Examples of valid Substitutions: We can not substitute constant.
o {Adel/x, Maged/y} We can substitute variable by a
constant, predicate, function, or
o {x/y} another variable.