CS335 Introduction to AI
Francisco Iacobelli
July 20, 2015
First Order Logic
Can intelligent systems deduce stuff?
One Sherlock Holmes
Another Sherlock
BBC One’s Sherlock (2010-) with Benedict Cumberbach and Martin Freeman.
First Order Logic
Representation Of Language
I Whorf (1956) suggest that communities determine lang.
categories
I Wanner (1975) subject remember the content of what they
read better than the actual words.
I Mitchell et al. (2008) could predict –with above chance
accuracy– the areas of the brain that would activate with
certain words(fMRI)
First Order Logic
Formal/Natural Languages
I Objects (cat, dog, house, John, etc.)
I Relations (has color, bigger than, comes between, etc.)
I Facts: (One value for a given input: has father, has head,
can swim)
Facts have a truth value. true or false
Formal Languages
Ontological and Epistemological Commitments
Language Ontological Commitment1 Epistemological Commitment2
Propositional Logic facts true/false/unknown
First-Order Logic facts, objects, relations true/false/unknown
Temporal Logic facts,objects, relations, time true/false/unknown
Probability Theory facts degree of belief ∈ [0, 1]
Fuzzy Logic facts with degree of truth known interval value
1
What exists in the world
2
Agent’s beliefs about facts
Relationships
Models for first order logic
crown
on head
brother
person person
king
brother
R $
J
left leg left leg
First-Order-Logic
Syntax
Sentence → AtomicSentence|ComplexSentence
AtomicSentence → Predicate|Predicate(Term, . . .)|Term = Term
ComplexSentence → (Sentence)|[Sentence]
| ¬ Sentence
| Sentence ∧ Sentence
| Sentence ∨ Sentence
| Sentence ⇒ Sentence
| Sentence ⇔ Sentence
| Quantifier Variable, . . . Sentence
Term → Function(Term, . . .)
| Constant
| Variable
Quantifier → ∀|∃
Constant → A|X1 |John| . . .
Variable → a|x|s| . . .
Predicate → True|False|After |Loves|Raining| . . .
Function → Mother |Left leg| . . .
Operator Precedence3 : ¬, ∧, ∨, ⇒, ↔
3
Otherwise the grammar is ambiguous
First Order Logic
More on Syntax
I Three kinds of symbols
I Constant: objects
I Predicate: relations
I Function: funcions (i.e. can return values other than truth
vals.)
I Predicate and Function have arity.
I Symbols have an interpretation
I Terms: LeftLeg(John)
I Atomic Sentences state facts: Brother (Richard, John)
I Complex Sentence: Brother (R, J) ∧ Brother (J, R) or
¬King(Richard) ⇒ King(John)
I Universal Quantifiers: ∀King(x) ⇒ Person(x)
I Existential Quantifiers: ∃Crown(x) ∧ OnHead(x, John)
First Order Logic
Try this
What is the interpretation for:
I King(Richard) ∨ King(John)
I ¬Brother (LeftLeg(Richard), John)
I ∀x∀yBrother (x, y ) ⇒ Sibling(x, y )
I In(Paris, France) ∧ In(Marseilles, France)
I ∀c Country (c) ∧ Border (c, Ecuador ) ⇒
In(c, SouthAmerica)
I ∃Country (c) ∧ Border (c, Spain) ∧ Border (c, Italy )
First Order Logic
More Facts
• Richard has only two brothers, John and Geoffrey:
Brother (John, Richard) ∧ Brother (Geoffrey , Richard) ∧ John 6= Geoffrey ∧
∀xBrother (x, Richard) ⇒ (x = John ∨ x = Geoffrey )
• No Region in South America borders any region in Europe
∀c, d In(c, SouthAmerica) ∧ In(d, Europe) ⇒ ¬Border (c, d)
• No two adjacent countries have the same map color
∀x, y Country (x) ∧ Country (y ) ∧ Border (x, y ) ⇒
¬(Color (x) = Color (y )) ∧ ¬(x = y )
First Order Logic
More Facts
• Richard has only two brothers, John and Geoffrey:
Brother (John, Richard) ∧ Brother (Geoffrey , Richard) ∧ John 6= Geoffrey ∧
∀xBrother (x, Richard) ⇒ (x = John ∨ x = Geoffrey )
• No Region in South America borders any region in Europe
∀c, d In(c, SouthAmerica) ∧ In(d, Europe) ⇒ ¬Border (c, d)
• No two adjacent countries have the same map color
∀x, y Country (x) ∧ Country (y ) ∧ Border (x, y ) ⇒
¬(Color (x) = Color (y )) ∧ ¬(x = y )
First Order Logic
More Facts
• Richard has only two brothers, John and Geoffrey:
Brother (John, Richard) ∧ Brother (Geoffrey , Richard) ∧ John 6= Geoffrey ∧
∀xBrother (x, Richard) ⇒ (x = John ∨ x = Geoffrey )
• No Region in South America borders any region in Europe
∀c, d In(c, SouthAmerica) ∧ In(d, Europe) ⇒ ¬Border (c, d)
• No two adjacent countries have the same map color
∀x, y Country (x) ∧ Country (y ) ∧ Border (x, y ) ⇒
¬(Color (x) = Color (y )) ∧ ¬(x = y )
First Order Logic
More Facts
• Richard has only two brothers, John and Geoffrey:
Brother (John, Richard) ∧ Brother (Geoffrey , Richard) ∧ John 6= Geoffrey ∧
∀xBrother (x, Richard) ⇒ (x = John ∨ x = Geoffrey )
• No Region in South America borders any region in Europe
∀c, d In(c, SouthAmerica) ∧ In(d, Europe) ⇒ ¬Border (c, d)
• No two adjacent countries have the same map color
∀x, y Country (x) ∧ Country (y ) ∧ Border (x, y ) ⇒
¬(Color (x) = Color (y )) ∧ ¬(x = y )
First Order Logic
More Facts
• Richard has only two brothers, John and Geoffrey:
Brother (John, Richard) ∧ Brother (Geoffrey , Richard) ∧ John 6= Geoffrey ∧
∀xBrother (x, Richard) ⇒ (x = John ∨ x = Geoffrey )
• No Region in South America borders any region in Europe
∀c, d In(c, SouthAmerica) ∧ In(d, Europe) ⇒ ¬Border (c, d)
• No two adjacent countries have the same map color
∀x, y Country (x) ∧ Country (y ) ∧ Border (x, y ) ⇒
¬(Color (x) = Color (y )) ∧ ¬(x = y )
First Order Logic
More Facts
• Richard has only two brothers, John and Geoffrey:
Brother (John, Richard) ∧ Brother (Geoffrey , Richard) ∧ John 6= Geoffrey ∧
∀xBrother (x, Richard) ⇒ (x = John ∨ x = Geoffrey )
• No Region in South America borders any region in Europe
∀c, d In(c, SouthAmerica) ∧ In(d, Europe) ⇒ ¬Border (c, d)
• No two adjacent countries have the same map color
∀x, y Country (x) ∧ Country (y ) ∧ Border (x, y ) ⇒
¬(Color (x) = Color (y )) ∧ ¬(x = y )
Assertions and Queries in FOL
ASK and TELL
I TELL(KB, King(John))
I TELL(KB, ∀x King(x) ⇒ Person(x))
I ASK (KB, King(John)) return True
I ASK (KB, ∃x Person(x)) return True
I ASKVARS(KB, Person(x)) yields {x/John, x/Richard}, a
binding list
First Order Logic
Kinship
“The son of my father is my brother”; “One’s grandmother is the
mother of one’s parent”; etc.
I Domain: People.
I Unary predicates: Male, Female
I Relations:
Parent, Sibling, Brother , Sister , Child, Daughter , Son, Spouse,
Wife, Husband, Grandparent, Grandchild, Cousin, Aunt, Uncle
I Functions: Mother , Father
First Order Logic
Kinship
“One’s mother is one’s female parent”
∀m, c Mother (c) = m ↔ Female(m) ∧ Parent(m, c)
“A sibling is another child of one’s parents”
∀x, y Sibling(x, y ) ↔ x 6= y ∧ ∃p Parent(p, x) ∧ Parent(p, y )
“Wendy is female”
Female(wendy )
These are axioms
First Order Logic
Kinship
“One’s mother is one’s female parent”
∀m, c Mother (c) = m ↔ Female(m) ∧ Parent(m, c)
“A sibling is another child of one’s parents”
∀x, y Sibling(x, y ) ↔ x 6= y ∧ ∃p Parent(p, x) ∧ Parent(p, y )
“Wendy is female”
Female(wendy )
These are axioms
First Order Logic
Kinship
“One’s mother is one’s female parent”
∀m, c Mother (c) = m ↔ Female(m) ∧ Parent(m, c)
“A sibling is another child of one’s parents”
∀x, y Sibling(x, y ) ↔ x 6= y ∧ ∃p Parent(p, x) ∧ Parent(p, y )
“Wendy is female”
Female(wendy )
These are axioms
First Order Logic
Kinship
“One’s mother is one’s female parent”
∀m, c Mother (c) = m ↔ Female(m) ∧ Parent(m, c)
“A sibling is another child of one’s parents”
∀x, y Sibling(x, y ) ↔ x 6= y ∧ ∃p Parent(p, x) ∧ Parent(p, y )
“Wendy is female”
Female(wendy )
These are axioms
First Order Logic
Kinship
“One’s mother is one’s female parent”
∀m, c Mother (c) = m ↔ Female(m) ∧ Parent(m, c)
“A sibling is another child of one’s parents”
∀x, y Sibling(x, y ) ↔ x 6= y ∧ ∃p Parent(p, x) ∧ Parent(p, y )
“Wendy is female”
Female(wendy )
These are axioms
First Order Logic
Wumpus: Time domain included
Can be represented more concisely
I at time step
3:Percept([Stench, Breeze, Glitter , None, None], 3)
I at time step
6:Percept([None, Breeze, None, None, Scream], 6)
I Actions can be:
Turn(Right), Turn(Left), Forward, Shoot, Grab, Climb
And we can ASK the best action at time step 5
ASKVARS(∃a BestAction(a, 5))
FOL: Wumpus
Encoding complex rules
Can encode:
I Raw percepts:
∀t, s, g, m, c Percept([s, b, Glitter , m, c], t) ⇒ Glitter (t)
I Reflex actions: ∀t Glitter (t) ⇒ BestAction(Grab, t)
Instead of encoding stuff like:
Adjacent(Square1,2 , Square1,1 )
Adjacent(Square3,4 , Square4,4 )
Encode:
∀x, y , a, b Adjacent([x, y ], [a, b]) ⇔
(x = a∧(y = b−1∨y = b+1))∨(y = b∧(x = a−1∨x = a+1))
First Order Logic
Legos
I Define pieces: Long(p), Short(p), etc.
I and restrictions: ∀p Long(p) ↔ ¬(Long(p) ∧ Short(p))
I Define rules: ∀x, y , z CanConnectWithOverlap(x, y , z) ↔
x 6= y ∧ Piece(x) ∧ Piece(y ) ∧ Number (z) ∧ Value(z) ≤
Overlap(x, y )...
I ∀x, y Short(x) ∧ Short(y ) ∧ Overlap(x, y ) < 1 ⇒
WeakLink (x, y )
Prolog
Example
Alain Colmerauer (1972)
Download SWI Prolog
And a quick tutorial
Creating a Knowledge Base
I Identify the Task
I Assemble the relevant knowledge
I Decide on a vocabulary of predicates, functions and
constants
I Encode general knowledge about the domain (rules)
I Encode a description of the problem
I Pose queries to the inference procedure and get answers
I Debug the KB
Inference
in First Order Logic
Given ∀x King(x) ∧ Greedy (x) ⇒ Evil(x)
One can infer
I King(John) ∧ Greedy (John) ⇒ Evil(John)
I King(Richard) ∧ Greedy (Richard) ⇒ Evil(Richard)
I King(Father (John)) ∧ Greedy (Father (John)) ⇒
Evil(Father (John))
Inference
in First Order Logic
I Universal Instantiation (in a ∀ rule, substitute all symbols)
I Existential Instantiation (in a ∃ rule, substitute one symbol,
use the rule and discard)
I Skolem Constants is a new variable that represents a new
inference.
Inference
in First Order Logic
Suppose KB:
I ∀x King(x) ∧ Greedy (x) ⇒ Evil(x)
I King(John)
I Greedy (John)
I Brother (Richard, John)
Apply UI using {x/John}and{x/Richard}
I King(John) ∧ Greedy (John) ⇒ Evil(John)
I King(Richard) ∧ Greedy (Richard) ⇒ Evil(Richard)
And discard the Universally quantified sentence. We can get
the KB to be propositions.
Inference
in First Order Logic
Suppose KB:
I ∀x King(x) ∧ Greedy (x) ⇒ Evil(x)
I King(John)
I ∀y Greedy (y )
Apply UI using {x/John}and{y /John}
Inference
Generalized Modus Ponens
0
for atomic sentences pi , pi and q, where there is a substitution
0
θ such that SUBST (θ, pi ) = SUBST (θ, pi ), for all i
0 0 0
p1 , p2 , . . . , pn , (p1 ∧ p2 ∧ . . . ∧ pn ⇒ q)
SUBST (θ, q)
0
p1 = King(John) p1 = King(x)
0
p2 = Greedy (y ) p2 = Greedy (x)
θ = {x/John, y /John} q = Evil(x)
SUBST (θ, q) .
Inference
Unification
UNIFY (p, q) = θ Where SUBST (θ, p) = SUBST (θ, q)
For example:
I We ask ASKVARS(Knows(John, x)) (Whom does John
know?)
I UNIFY (Knows(John, x), Knows(John, Jane)) = {x/Jane}
I UNIFY (Knows(John, x), Knows(y , Bill)) =
{y /John, x/Bill}
I UNIFY (Knows(John, x), Knows(y , Mother (y ))) =
{y /John, x/Mother (John)}
Algorithm in the book (goes variable by variable recursively unifying)
Inference
Putting it all together
“The Law says that it is a crime for an American to sell
weapons to hostile nations. The country Nono, and enemy of
America, has some missiles , and all of its missiles were sold to
it by Colonel West, who is American”
Prove that Colonel Wes is a Criminal
Inference
The KB
“The Law says that it is a crime for an American to sell
weapons to hostile nations. The country Nono, and enemy of
America, has some missiles , and all of its missiles were sold to
it by Colonel West, who is American”
I R1:
American(x) ∧ Weapon(y ) ∧ Sells(x, y .z) ∧ Hostile(z) ⇒ Criminal(x)
I R2: Owns(Nono, M1 ) Nono has some missiles
I R3: Missile(M1 )
I R4: Missile(x) ⇒ Weapon(x) A missile is a weapon
I R5: Missile(x) ∧ Owns(Nono, x) ⇒ Sells(West, x, Nono) All missiles
sold by west
I R6: Enemy (x, America) ⇒ Hostile(x) Enemies of America are hostile
I R7: American(West) West is american
I R8: Enemy (Nono, America)
Inference
Graph
Criminal(West)
Weapon(M1) Sells(West,M1,Nono) Hostile(Nono)
American(West) Missile(M1) Owns(Nono,M1) Enemy(Nono,America)
Figure 9.4 FILES: figures/[Link] (Tue Nov 3 [Link] 2009). The proof tree generated by
forward chaining on the crime example. The initial facts appear at the bottom level, facts inferred on
the first iteration in the middle level, and facts inferred on the second iteration at the top level.
Forward Chaining ASK
Iterations
Iteration 1:
I R5 satisfied with {x/M1 } and R9: Sells(West, M1 , Nono) is
added
I R4 satisfied with {x/M1 } and R10: Weapon(M1 ) is added
I R6 satisfied with {x/Nono} and R11: Hostile(Nono) is
added
Iteration 2:
I R1 is satisfied with {x/West, y /M1 , z/Nono} and
Criminal(West) is added.
Forward Chaining ASK
Iterations
Iteration 1:
I R5 satisfied with {x/M1 } and R9: Sells(West, M1 , Nono) is
added
I R4 satisfied with {x/M1 } and R10: Weapon(M1 ) is added
I R6 satisfied with {x/Nono} and R11: Hostile(Nono) is
added
Iteration 2:
I R1 is satisfied with {x/West, y /M1 , z/Nono} and
Criminal(West) is added.
Inference in First Order Logic
Discussion
I Once we have facts that evaluate to T or F
I We can apply Forward Chaining, Backwards Chaining and
Resolution
I The key is to understand Unification
I Very similar to Logical agents.