Artificial Intelligence: Lecture 2: First Order Logic 2 Solve Problems
Artificial Intelligence: Lecture 2: First Order Logic 2 Solve Problems
Artificial Intelligence: Lecture 2: First Order Logic 2 Solve Problems
❑- - -
❑If it is sunny on a particular day, then the sun shines on the screen. If the
sun shines on the screen on a particular day, the blinds are brought down.
The blinds are not down today.
❑Is it sunny today?
First-order logic
❑First-order logic (FOL) models the world in terms of
❑ Objects, which are things with individual identities
❑ Students, lectures, companies, cars ...
❑ color_of(Sky) = Blue
❑ green(Grass)
❑ color(Grass, Green)
FOL Provides
❑Variable symbols
❑ E.g., x, y, foo
❑Connectives
❑ Same as in propositional logic:
not (), and (), or (), implies (→), iff ()
❑Quantifiers
❑ Universal x
❑ Existential x
Sentences: built from terms and atoms
Scope of x
Scope of y
Quantifier Scope
❑Switching order of universal quantifiers does not change the meaning
❑ (x)(y)P(x,y) ↔ (y)(x) P(x,y)
❑ “Dogs hate cats”
❑A sentence is
❑ satisfiable if it is true under some interpretation
❑ valid (provably true) if it is true under all possible interpretations
❑ inconsistent (provably false ) if there does not exist any interpretation under
which the sentence is true
❑Logical consequence: S |= X if all models of S are also models of X
Quantified inference rules
❑Universal instantiation
❑ x P(x) P(A)
❑Universal generalization
❑ P(A) P(B) … x P(x)
❑Existential instantiation
❑ x P(x) P(F) skolem constant F
F must be a “new” constant not
❑Existential generalization appearing in the KB
❑ P(A) x P(x)
Universal instantiation
(a.k.a. universal elimination)
❑If (x) P(x) is true, then P(C) is true, where C is any constant in
the domain of x, e.g.:
(x) eats(John, x)
eats(John, Cheese18)
❑Premises:
❑ D sunny (D)→ screen-shines (D)
❑ D screen-shines(D) → blinds-down(D)
❑ blinds-down (today)
❑Question: sunny(today)
Translating English to FOL
Every gardener likes the sun
x gardener(x) → likes(x,Sun)
You can fool some of the people all of the time
x t person(x) time(t) → can-fool(x, t)
You can fool all of the people some of the time
x t (person(x) time(t) → can-fool(x, t)) Note 2
possible
t x (person(x) time(t) → can-fool(x, t)) readings of
NL sentence
All purple mushrooms are poisonous
x (mushroom(x) purple(x)) → poisonous(x)
Translating English to FOL
No purple mushroom is poisonous (two ways)
x purple(x) mushroom(x) poisonous(x)
x (mushroom(x) purple(x)) → poisonous(x)
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))
Obama is not short
short(Obama)
Automating FOL inference
with Generalized Modus Ponens
Generalized Modus Ponens (GMP)
❑ Apply modus ponens reasoning to generalized rules
❑ Combines And-Introduction, Universal-Elimination, and Modus Ponens
❑ From P(c) and Q(c) and (x)(P(x) Q(x)) → R(x) derive R(c)
❑ General case: Given
❑ atomic sentences P1, P2, ..., PN
❑ implication sentence (Q1 Q2 ... QN) → R
❑ Q1, ..., QN and R are atomic sentences
❑ substitution subst(θ, Pi) = subst(θ, Qi) for i=1,...,N
❑ Derive new sentence: subst(θ, R)
❑ Substitutions
❑ subst(θ, α) denotes the result of applying a set of substitutions defined by θ to the sentence α
❑ A substitution list θ = {v1/t1, v2/t2, ..., vn/tn} means to replace all occurrences of variable symbol vi by term ti
❑ Substitutions are made in left-to-right order in the list
❑ subst({x/IceCream, y/Ziggy}, eats(y,x)) = eats(Ziggy, IceCream)
p1', p2', … , pn', ( p1 p2 … pn q) where pi'θ = pi θ for all i
qθ
❑ FC is data-driven
❑ Automatic, unconscious processing
❑ E.g., object recognition, routine decisions
❑ May do lots of work that is irrelevant to the goal
❑ BC is goal-driven, appropriate for problem-solving
❑ Where are my keys? How do I get to my next class?
❑ Complexity of BC can be much less than linear in the size of the KB
Automating FOL Inference
with Resolution
Resolution
❑Resolution is a sound and complete inference procedure for
FOL
❑Reminder: Resolution rule for propositional logic:
❑ P1 P2 ... Pn
❑ P1 Q2 ... Qm
y/Felix
z/Lise
sneeze(Lise)
sneeze(Lise)
false
negated query
We need answers to the following questions
6. Remove universal quantifiers by (1) moving them all to the left end; (2) making
the scope of each the entire sentence; and (3) dropping the “prefix” part
Ex: (x)P(x) P(x)
7. Put into conjunctive normal form (conjunction of disjunctions) using
distributive and associative laws
(P Q) R (P R) (Q R)
(P Q) R (P Q R)
8. Split conjuncts into separate clauses
9. Standardize variables so each clause contains only variable names that do not
occur in any other clause
An example
(x)(P(x) → ((y)(P(y) → P(f(x,y))) (y)(Q(x,y) → P(y))))
2. Eliminate →
(x)(P(x) ((y)(P(y) P(f(x,y))) (y)(Q(x,y) P(y))))
3. Reduce scope of negation
(x)(P(x) ((y)(P(y) P(f(x,y))) (y)(Q(x,y) P(y))))
4. Standardize variables
(x)(P(x) ((y)(P(y) P(f(x,y))) (z)(Q(x,z) P(z))))
5. Eliminate existential quantification
(x)(P(x) ((y)(P(y) P(f(x,y))) (Q(x,g(x)) P(g(x)))))
6. Drop universal quantification symbols
(P(x) ((P(y) P(f(x,y))) (Q(x,g(x)) P(g(x)))))
Example
7. Convert to conjunction of disjunctions
(P(x) P(y) P(f(x,y))) (P(x) Q(x,g(x)))
(P(x) P(g(x)))
8. Create separate clauses
P(x) P(y) P(f(x,y))
P(x) Q(x,g(x))
P(x) P(g(x))
9. Standardize variables
P(x) P(y) P(f(x,y))
P(z) Q(z,g(z))
P(w) P(g(w))
Unification
Unification
❑Unification is a “pattern-matching” procedure
❑ Takes two atomic sentences, called literals, as input
❑ Returns “Failure” if they do not match and a substitution list, θ, if they
do
❑That is, unify(p,q) = θ means subst(θ, p) = subst(θ, q) for two atomic
sentences, p and q
❑θ is called the most general unifier (mgu)
❑All variables in the given two literals are implicitly universally quantified
❑To make literals match, replace (universally quantified) variables by terms
Unification algorithm
procedure unify(p, q, θ)
Scan p and q left-to-right and find the first corresponding terms where p and q “disagree”
(i.e., p and q not equal)
If there is no disagreement, return θ (success!)
Let r and s be the terms in p and q, respectively, where disagreement first occurs
If variable(r) then {
Let θ = union(θ, {r/s})
Return unify(subst(θ, p), subst(θ, q), θ)
} else if variable(s) then {
Let θ = union(θ, {s/r})
Return unify(subst(θ, p), subst(θ, q), θ)
} else return “Failure”
end
Unification: Remarks
❑Unify is a linear-time algorithm that returns the most general
unifier (mgu), i.e., the shortest-length substitution list that
makes the two literals match.
❑In general, there is not a unique minimum-length substitution
list, but unify returns one of minimum length
❑A variable can never be replaced by a term containing that
variable
Example: x/f(x) is illegal.
❑This “occurs check” should be done in the above pseudo-code
before making the recursive calls
Unification examples
❑Example:
❑ parents(x, father(x), mother(Bill))
❑ parents(Bill, father(Bill), y)
❑ {x/Bill, y/mother(Bill)}
❑Example:
❑ parents(x, father(x), mother(Bill))
❑ parents(Bill, father(y), z)
❑ {x/Bill, y/Bill, z/mother(Bill)}
❑Example:
❑ parents(x, father(x), mother(Jane))
❑ parents(Bill, father(y), mother(y))
❑ Failure
Resolution example
Example I
❑KB:
❑ allergies(X) → sneeze(X)
❑ cat(Y) allergic-to-cats(X) → allergies(X)
❑ cat(Felix)
❑ allergic-to-cats(Lise)
❑Goal:
❑ sneeze(Lise)
Refutation Resolution Proof Tree
allergies(w) v sneeze(w) cat(y) v ¬allergic-to-cats(z) allergies(z)
w/z
y/Felix
z/Lise
sneeze(Lise) sneeze(Lise)
false
negated query
Example II: the package delivery robot
❑Suppose this robot knows that all of the packages in room 27 are smaller than any
of the ones in room 28.
❑ (x,y){[Package(x) Package(y) Inroom(x,27) Inroom(y,28)] → Smaller(x,y)}
❑Prove I(A,27)
Answer Extracting
❑We know that A is in a room
❑ (u) I(A, u)
❑To extract the answer u? (which room contains A), add:
❑ (A, u) Ans(u)
Example III
❑ The law says that it is a crime for an American to sell weapons to hostile nations.
The country Nono, an enemy of America, has some missiles, and all of its missiles
were sold to it by Colonel West, who is American.
❑FOL only allows to quantify over variables, and variables can only
range over objects.
❑HOL allows us to quantify over relations
❑Example: (quantify over functions)
“two functions are equal iff they produce the same value for all arguments”
f g (f = g) (x f(x) = g(x))
❑Example: (quantify over predicates)
r transitive( r ) → (xyz) r(x,y) r(y,z) → r(x,z))
❑More expressive, but undecidable, in general
Other Examples
Example: A simple genealogy KB by FOL
❑Build a small genealogy knowledge base using FOL that
❑ contains facts of immediate family relations (spouses, parents, etc.)
❑ contains definitions of more complex relations (ancestors, relatives)
❑ is able to answer queries about relationships between people
❑Predicates:
❑ parent(x, y), child(x, y), father(x, y), daughter(x, y), etc.
❑ spouse(x, y), husband(x, y), wife(x,y)
❑ ancestor(x, y), descendant(x, y)
❑ male(x), female(y)
❑ relative(x, y)
❑Facts:
❑ husband(Joe, Mary), son(Fred, Joe)
❑ spouse(John, Nancy), male(John), son(Mark, Nancy)
❑ father(Jack, Nancy), daughter(Linda, Jack)
❑ daughter(Liz, Linda)
❑ etc.
❑ Rules for genealogical relations
❑ (x,y) parent(x, y) ↔ child (y, x)
(x,y) father(x, y) ↔ parent(x, y) male(x) ;similarly for mother(x, y)
(x,y) daughter(x, y) ↔ child(x, y) female(x) ;similarly for son(x, y)
❑ (x,y) husband(x, y) ↔ spouse(x, y) male(x) ;similarly for wife(x, y)
(x,y) spouse(x, y) ↔ spouse(y, x) ;spouse relation is symmetric
❑ (x,y) parent(x, y) → ancestor(x, y)
(x,y)(z) parent(x, z) ancestor(z, y) → ancestor(x, y)
❑ (x,y) descendant(x, y) ↔ ancestor(y, x)
❑ (x,y)(z) ancestor(z, x) ancestor(z, y) → relative(x, y)
;related by common ancestry
(x,y) spouse(x, y) → relative(x, y) ;related by marriage
(x,y)(z) relative(z, x) relative(z, y) → relative(x, y) ;transitive
(x,y) relative(x, y) ↔ relative(y, x) ;symmetric
❑ Queries
❑ ancestor(Jack, Fred) ; the answer is yes
❑ relative(Liz, Joe) ; the answer is yes
❑ relative(Nancy, Matthew)
;no answer in general, no if under closed world assumption
❑ (z) ancestor(z, Fred) ancestor(z, Liz)
Examples
❑John’s meals are spicy.
❑ X meal-of(John,X) → spicy(X)
❑Every city has a dogcatcher who has been bitten by every dog
in town.
❑ C D Z city(C) → ( dogcatcher(D,C)
(dog(Z) lives-in (Z, C) → bit (Z, D)) )
Examples (cont’d)
❑For every set x, there is a set y, such that the cardinality of y is
greater than the cardinality of x.
❑ X Y U V set(X) → (set(Y) cardinality(X,U)
cardinality(Y, V) greater-than(V,U))
Blocks world
on (c,a)
on(b,d)
ontable(a)
ontable(d)
clear(b)
clear(c)
hand_empty
c b
a d
Blocks world example
❑All blocks on top of blocks that have been moved or that are
attached to blocks that have been moved have also been
moved.
❑ X Y (block(X) block(Y)
(on(X,Y) attached (X,Y)) moved (Y)) →
moved(X)
A Logic-Based Financial Advisor
❑Gives investment advice (savings account, or the stock market,
or both).
❑Example “rule”:
❑If the amount in the savings account is inadequate, increasing
this amount should be the first priority.
Sentences
❑ 1. savings_account (inadequate) →
investment(savings)
❑ 2. savings_account (adequate)
income(adequate) → investment (stocks)
❑ 3. savings_account (adequate)
income(inadequate) → investment (combination)
❑ 4. X amount_saved(X) Y (dependents (Y)
greater(X, minsavings(Y))) →
savings_account(adequate)
❑ Y is the number of dependents, minsavings is the number of dependents multiplied by 5000.
Sentences (cont’d)
❑ 5. X amount_saved(X) Y (dependents (Y)
greater (X, minsavings(Y))) →
savings_account(inadequate)
❑ 6. X earnings(X, steady) Y (dependents (Y)
greater (X, minincome(Y))) →
income(adequate)
❑ 7. X earnings(X, steady) Y (dependents (Y)
greater (X, minincome(Y))) →
income(inadequate)
❑ Minimum income is
15,000 + (4000 * number of dependents)
❑ 8. X earnings(X, unsteady) → income(inadequate)
Sentences (cont’d)
❑ 9. amount_saved(22000)
❑ 10. earnings(25000, steady)
❑ 11. dependents (3)