2021 Lecture08 FirstOrderLogic PDF
2021 Lecture08 FirstOrderLogic PDF
FIRST-ORDER LOGIC
2
Representation
revisited
3
Programming languages
• By far the largest class of formal languages in common use
• E.g., C++, Java or Lisp, etc.
• Data structures represent facts and programs express
computational processes
• E.g., implement the Wumpus world as a 4 × 4 array, “World[2,2] ←
Pit” states that “There is a pit in square [2,2].”
4
Programming languages
• Lack of general mechanisms to derive facts from other facts
• Update to a data structure is done by a domain-specific procedure
• Lack of expressiveness to handle partial information
• E.g., to say “There is a pit in [2,2] or [3,1]”, a program stores a single
value for each variable and allows the value to be “unknown”
• The propositional logic sentence, 𝑃2,2 ∨ 𝑃3,1 , is more intuitive
5
Propositional logic
• Propositional logic is a declarative language.
• Semantics is based on the truth relation between sentences and
possible worlds.
• Handle partial information using disjunction and negation.
• Compositional: desirable in representation languages
• The meaning of a sentence is a function of the meaning of its parts
• E.g., The meaning of 𝑆1,4 ∧ 𝑆1,2 relates the meanings of 𝑆1,4 and 𝑆1,2 .
6
Propositional logic
• Meaning in propositional logic is context-independent.
• The natural language, on the other hand, are dependent on context.
• Limited expressive power
• E.g., we cannot say “Pits cause breezes in adjacent squares“, except
by writing one sentence for each square
𝐵1,1 ⇔ 𝑃1,2 ∨ 𝑃2,1 , 𝐵2,2 ⇔ 𝑃1,2 ∨ 𝑃2,1 ∨ 𝑃3,2 ∨ 𝑃3,1 , etc.
7
First-order logic
9
Types of logics
Epistemological
Ontological Commitment Commitment
Language
(What exists in the world) (What an agent believes
about facts)
10
FOL
Syntax and
Semantics
1
1
Models for a logic language
• A model for first-order logic contains a set of objects
(domain) and relations among them.
• Each object is called a domain element.
• Nonempty — Every possible world must contain at least one
object
• It doesn’t matter what these objects are but how many there are in
each model.
12
Models for FOL: A concrete example
• 5 objects
• 2 binary relations
• 3 unary relations
• 1 unary function
13
Models for FOL: A concrete example
• 5 objects
Richard the Lionheart King John The left The left A crown
(King of England (King of England leg of leg of
1189-1199) 1199-1215) Richard John
14
Models for FOL: A concrete example
• Binary relations
• The brotherhood relation
{ Richard the Lionheart, King John, King John, Richard the Lionheart }
• The “on head” relation
{ The crown, King John }
• Unary relations: “person”, “king”, “crown”
• Functions: “left leg”
• Richard the Lionheart → Richard’s left leg
• King John → John’s left leg
15
FOL concepts: Symbols
• Constant symbols represents objects.
• E.g., Richard, John, etc.
• Predicate symbols stand for relations.
• E.g., Brother , OnHead, Person, King, and Crown, etc.
• Function symbols stand for functions.
• E.g., LeftLeg
• Each predicate or function symbol comes with an arity that
fixes the number of arguments.
• E.g., Brother(x,y) → binary, LeftLeg(x) → unary, etc.
• These symbols begins with uppercase letters by convention.
16
Symbols and intended interpretation
17
First-order logic: Syntax
18
18
First-order logic: Syntax
19
Possible models in FOL
• Like propositional logic, entailment, validity, and so on are
defined in terms of all possible models.
20
FOL concepts: Term and Variable
• A term is a logical expression that refers to an object.
• E.g., John, LeftLeg(John), etc.
Complex term
22
FOL concepts: Atomic sentence
• An atomic sentence states facts by using a predicate symbol
followed by a parenthesized list of terms.
• E.g., Brother(Richard, John), Married(Father(Richard), Mother(John))
23
FOL concepts: Complex sentence
• A complex sentence is made from atomic sentences using
logical connectives.
• For example,
• 𝐵𝑟𝑜𝑡ℎ𝑒𝑟 (𝐿𝑒𝑓𝑡𝐿𝑒𝑔(𝑅𝑖𝑐ℎ𝑎𝑟𝑑), 𝐽𝑜ℎ𝑛)
• 𝐵𝑟𝑜𝑡ℎ𝑒𝑟 (𝑅𝑖𝑐ℎ𝑎𝑟𝑑 , 𝐽𝑜ℎ𝑛) ∧ 𝐵𝑟𝑜𝑡ℎ𝑒𝑟 (𝐽𝑜ℎ𝑛, 𝑅𝑖𝑐ℎ𝑎𝑟𝑑)
• 𝐾𝑖𝑛𝑔(𝑅𝑖𝑐ℎ𝑎𝑟𝑑) ∨ 𝐾𝑖𝑛𝑔(𝐽𝑜ℎ𝑛)
• 𝐾𝑖𝑛𝑔(𝑅𝑖𝑐ℎ𝑎𝑟𝑑) ⇒ 𝐾𝑖𝑛𝑔(𝐽𝑜ℎ𝑛)
• …
• Sentences are true with respect to a model and an
interpretation.
24
Quantifiers: Universal quantification
27
Quantifiers: A common mistake to avoid
28
Quantifiers: Nested quantifiers
• Multiple quantifiers enable more complex sentences.
• Simplest cases: Quantifiers are of the same type
• ∀𝑥∀𝑦 𝐵𝑟𝑜𝑡ℎ𝑒𝑟 𝑥, 𝑦 ⇒ 𝑆𝑖𝑏𝑙𝑖𝑛𝑔(𝑥, 𝑦)
• ∀𝑥∀𝑦 𝑆𝑖𝑏𝑙𝑖𝑛𝑔(𝑥, 𝑦) ⇔ 𝑆𝑖𝑏𝑙𝑖𝑛𝑔(𝑦, 𝑥)
• Mixtures
• ∀𝑥∃𝑦 𝐿𝑜𝑣𝑒𝑠(𝑥, 𝑦) − “Everybody loves somebody.”
• ∃𝑥∀𝑦 𝐿𝑜𝑣𝑒𝑠 𝑦, 𝑥 − “There is someone loved by everyone.”
• The order of quantification is therefore very important.
29
Quantifiers: Nested quantifiers
• Two quantifiers used with the same variable name leads to
confusion.
∀𝑥 (𝐶𝑟𝑜𝑤𝑛 𝑥 ∨ ∃𝑥 𝐵𝑟𝑜𝑡ℎ𝑒𝑟 𝑅𝑖𝑐ℎ𝑎𝑟𝑑, 𝑥 )
• Rule: The variable belongs to the innermost quantifier that
mentions it.
• Workaround: Use different variable names with nested
quantifiers, e.g., ∃𝑧 𝐵𝑟𝑜𝑡ℎ𝑒𝑟 𝑅𝑖𝑐ℎ𝑎𝑟𝑑, 𝑧
30
Quantifiers: Rules for duality
• and relate to each other through negation.
• For example,
• ∀𝑥 𝐿𝑖𝑘𝑒𝑠 𝑥, 𝐼𝑐𝑒𝐶𝑟𝑒𝑎𝑚 ¬∃𝑥 ¬𝐿𝑖𝑘𝑒𝑠 𝑥, 𝐼𝑐𝑒𝐶𝑟𝑒𝑎𝑚
• ∃𝑥 𝐿𝑖𝑘𝑒𝑠 𝑥, 𝐵𝑟𝑜𝑐𝑜𝑙𝑖 ¬∀𝑥 ¬𝐿𝑖𝑘𝑒𝑠 𝑥, 𝐼𝑐𝑒𝐶𝑟𝑒𝑎𝑚
• De Morgan’s rules
31
Equality symbol =
• 𝑡𝑒𝑟𝑚1 = 𝑡𝑒𝑟𝑚2 is true under a given interpretation iff 𝑡𝑒𝑟𝑚1
and 𝑡𝑒𝑟𝑚2 refer to the same object
• E.g., 𝐹𝑎𝑡ℎ𝑒𝑟 𝐽𝑜ℎ𝑛 = 𝐻𝑒𝑛𝑟𝑦 means that 𝐹𝑎𝑡ℎ𝑒𝑟(𝐽𝑜ℎ𝑛) and 𝐻𝑒𝑛𝑟𝑦
refer to the same object.
• It states facts about a given function
• The negation insists that two terms are not the same.
∃𝑥, 𝑦 𝐵𝑟𝑜𝑡ℎ𝑒𝑟(𝑥, 𝑅𝑖𝑐ℎ𝑎𝑟𝑑) ∧ 𝐵𝑟𝑜𝑡ℎ𝑒𝑟(𝑦, 𝑅𝑖𝑐ℎ𝑎𝑟𝑑) ∧ ¬(𝑥 = 𝑦)
32
Writing FOL sentences:
Case studies
33
The kinship
domain
• Unary predicates
• Male and Female
• Binary predicates represent kinship relations.
• Parenthood, brotherhood, marriage, etc.
• Parent, Sibling, Brother , Sister, Child, Daughter, Son, Spouse, Wife,
Husband, Grandparent , Grandchild , Cousin, Aunt, and Uncle.
• Functions
• Mother and Father, each person has exactly one of each of these.
34
The kinship domain: Axioms
• One’s mother is one’s female parent
∀𝑚, 𝑐 𝑀𝑜𝑡ℎ𝑒𝑟(𝑐) = 𝑚 ⇔ 𝐹𝑒𝑚𝑎𝑙𝑒(𝑚) ∧ 𝑃𝑎𝑟𝑒𝑛𝑡(𝑚, 𝑐)
• One’s husband is one’s male spouse:
∀𝑤, ℎ 𝐻𝑢𝑠𝑏𝑎𝑛𝑑(ℎ, 𝑤) ⇔ 𝑀𝑎𝑙𝑒(ℎ) ∧ 𝑆𝑝𝑜𝑢𝑠𝑒(ℎ, 𝑤)
• Male and female are disjoint categories:
∀𝑥 𝑀𝑎𝑙𝑒(𝑥) ⇔ ¬𝐹𝑒𝑚𝑎𝑙𝑒(𝑥)
• Parent and child are inverse relations:
∀𝑝, 𝑐 𝑃𝑎𝑟𝑒𝑛𝑡(𝑝, 𝑐) ⇔ 𝐶ℎ𝑖𝑙𝑑(𝑐, 𝑝)
• A grandparent is a parent of one’s parent:
∀𝑔, 𝑐 𝐺𝑟𝑎𝑛𝑑𝑝𝑎𝑟𝑒𝑛𝑡(𝑔, 𝑐) ⇔ ∃𝑝 𝑃𝑎𝑟𝑒𝑛𝑡(𝑔, 𝑝) ∧ 𝑃𝑎𝑟𝑒𝑛𝑡(𝑝, 𝑐)
• A sibling is another child of one’s parents:
∀𝑥, 𝑦 𝑆𝑖𝑏𝑙𝑖𝑛𝑔(𝑥, 𝑦) ⇔ ¬(𝑥 = 𝑦) ∧ ∃𝑝 𝑃𝑎𝑟𝑒𝑛𝑡(𝑝, 𝑥) ∧ 𝑃𝑎𝑟𝑒𝑛𝑡(𝑝, 𝑦)
35
The kinship domain: Theorems
• Theorems: logical sentences that are entailed by the axioms
• E.g., ∀𝑥, 𝑦 𝑆𝑖𝑏𝑙𝑖𝑛𝑔(𝑥, 𝑦) ⇔ 𝑆𝑖𝑏𝑙𝑖𝑛𝑔(𝑦, 𝑥)
• Theorems reduce the cost of deriving new sentences.
• They do not increase the set of conclusions that follow from 𝐾𝐵 → no
value from a pure logical point of view
• They are essential from a practical point of view.
36
The set domain
• Sets are the empty set and those made by adjoining something to a set.
• 𝑠 𝑆𝑒𝑡(𝑠) ⇔ (𝑠 = { } ) ∨ (∃𝑥, 𝑠2 𝑆𝑒𝑡(𝑠2) 𝑠 = {𝑥|𝑠2})
• The empty set has no elements adjoined into it.
• 𝑥, 𝑠 {𝑥|𝑠} = { }
• Adjoining an element already in the set has no effect:
• 𝑥, 𝑠 𝑥 ∈ 𝑠 ⇔ 𝑠 = {𝑥|𝑠}
• The only members of a set are the elements that were adjoined into it.
• 𝑥, 𝑠 𝑥 ∈ 𝑠 ⇔ ∃𝑦, 𝑠2 (𝑠 = {𝑦|𝑠2} (𝑥 = 𝑦 ∨ 𝑥 ∈ 𝑠2))]
37
The set domain
• Can you interpret the following sentences?
• 𝑠1 , 𝑠2 𝑠1 ⊑ 𝑠2 ⇔ (𝑥 𝑥 ∈ 𝑠1 ⇒ 𝑥 ∈ 𝑠2 )
• 𝑠1 , 𝑠2 𝑠1 = 𝑠2 ⇔ (𝑠1 ⊑ 𝑠2 ∧ 𝑠2 ⊑ 𝑠1 )
• 𝑥, 𝑠1 , 𝑠2 𝑥 ∈ (𝑠1 ∩ 𝑠2 ) ⇔ (𝑥 ∈ 𝑠1 ∧ 𝑥 ∈ 𝑠2 )
• 𝑥, 𝑠1 , 𝑠2 𝑥 ∈ (𝑠1 ∪ 𝑠2 ) ⇔ (𝑥 ∈ 𝑠1 ∨ 𝑥 ∈ 𝑠2 )
38
The Wumpus world: Input – Output
39
The Wumpus world: The KB
• Perception
• 𝑡, 𝑠, 𝑔, 𝑚, 𝑐 𝑃𝑒𝑟𝑐𝑒𝑝𝑡 ([𝑠, 𝐵𝑟𝑒𝑒𝑧𝑒, 𝑔, 𝑚, 𝑐], 𝑡) ⇒ 𝐵𝑟𝑒𝑒𝑧𝑒(𝑡)
• 𝑡, 𝑠, 𝑏, 𝑚, 𝑐 𝑃𝑒𝑟𝑐𝑒𝑝𝑡 ([𝑠, 𝑏, 𝐺𝑙𝑖𝑡𝑡𝑒𝑟, 𝑚, 𝑐], 𝑡) ⇒ 𝐺𝑙𝑖𝑡𝑡𝑒𝑟 (𝑡) …
• Reflex
• 𝑡 𝐺𝑙𝑖𝑡𝑡𝑒𝑟(𝑡) ⇒ 𝐵𝑒𝑠𝑡𝐴𝑐𝑡𝑖𝑜𝑛(𝐺𝑟𝑎𝑏, 𝑡)
• Environment definition:
𝑥, 𝑦, 𝑎, 𝑏 𝐴𝑑𝑗𝑎𝑐𝑒𝑛𝑡([𝑥, 𝑦], [𝑎, 𝑏]) ⇔
𝑥 =𝑎 ∧ 𝑦 =𝑏−1 ∨ 𝑦 =𝑏 + 1
∨ (𝑦 = 𝑏 ∧ (𝑥 = 𝑎 − 1 ∨ 𝑥 = 𝑎 + 1))
40
The Wumpus world: Hidden properties
• Properties of squares
𝑠, 𝑡 𝐴𝑡(𝐴𝑔𝑒𝑛𝑡, 𝑠, 𝑡) ∧ 𝐵𝑟𝑒𝑒𝑧𝑒(𝑡) ⇒ 𝐵𝑟𝑒𝑒𝑧𝑦(𝑠)
• Squares are breezy near a pit
• Diagnostic rule --- infer cause from effect
𝑠 𝐵𝑟𝑒𝑒𝑧𝑦(𝑠) ⇔ ∃𝑟 𝐴𝑑𝑗𝑎𝑐𝑒𝑛𝑡(𝑟, 𝑠) ∧ 𝑃𝑖𝑡(𝑟)
• Causal rule --- infer effect from cause
𝑟 𝑃𝑖𝑡(𝑟) ⇔ [∀𝑠 𝐴𝑑𝑗𝑎𝑐𝑒𝑛𝑡(𝑟, 𝑠) ⇒ 𝐵𝑟𝑒𝑒𝑧𝑦(𝑠)]
41
Quiz 01: Writing FOL sentences
• Represent the following sentences with first-order logic using
the given predicates
• 𝑆𝑡𝑢𝑑𝑒𝑛𝑡(𝑥) means 𝑥 is student.
• 𝑆𝑚𝑎𝑟𝑡(𝑥) means 𝑥 is smart.
• 𝐿𝑜𝑣𝑒𝑠(𝑥, 𝑦) means 𝑥 loves 𝑦.
43
Universal Instantiation (UI)
• It is possible to infer any sentence obtained by substituting a
ground term for the variable.
• Let 𝑆𝑈𝐵𝑆𝑇(𝜃, 𝛼) be the result of applying the substitution 𝜃 to
the sentence 𝛼.
• Then the rule of Universal Instantiation is written as
𝒗 𝜶
𝑺𝑼𝑩𝑺𝑻({𝒗/𝒈}, 𝜶)
• for any variable 𝑣 and ground term 𝑔.
44
Universal Instantiation: An example
• Suppose our knowledge base contains
∀𝒙 𝑲𝒊𝒏𝒈 𝒙 ∧ 𝑮𝒓𝒆𝒆𝒅𝒚(𝒙) ⇒ 𝑬𝒗𝒊𝒍(𝒙)
• Then it is permissible to infer any of the following sentences
𝐾𝑖𝑛𝑔 𝐽𝑜ℎ𝑛 ∧ 𝐺𝑟𝑒𝑒𝑑𝑦(𝐽𝑜ℎ𝑛) ⇒ 𝐸𝑣𝑖𝑙(𝐽𝑜ℎ𝑛)
𝐾𝑖𝑛𝑔 𝑅𝑖𝑐ℎ𝑎𝑟𝑑 ∧ 𝐺𝑟𝑒𝑒𝑑𝑦(𝑅𝑖𝑐ℎ𝑎𝑟𝑑) ⇒ 𝐸𝑣𝑖𝑙(𝑅𝑖𝑐ℎ𝑎𝑟𝑑)
𝐾𝑖𝑛𝑔 𝐹𝑎𝑡ℎ𝑒𝑟(𝐽𝑜ℎ𝑛) ∧ 𝐺𝑟𝑒𝑒𝑑𝑦 𝐹𝑎𝑡ℎ𝑒𝑟 𝐽𝑜ℎ𝑛
⇒ 𝐸𝑣𝑖𝑙(𝐹𝑎𝑡ℎ𝑒𝑟(𝐽𝑜ℎ𝑛))
…
with substitutions {𝑥/𝐽𝑜ℎ𝑛}, {𝑥/𝑅𝑖𝑐ℎ𝑎𝑟𝑑}, and
{𝑥/𝐹𝑎𝑡ℎ𝑒𝑟(𝐽𝑜ℎ𝑛)}, respectively
45
Existential Instantiation (EI)
• It is possible to replace the variable by a single new
constant symbol.
• The rule of Existential Instantiation is written as
∃𝒗 𝜶
𝑺𝑼𝑩𝑺𝑻({𝒗/𝒌}, 𝜶)
• for any sentence 𝛼, variable 𝑣, and constant symbol 𝑘 that does not
appear elsewhere in 𝐾𝐵.
46
Universal / Existential Instantiation
• The UI rule can be applied many times to produce different
consequences.
• The EI rule can be applied once, and then the existentially
quantified sentence is discarded.
• E.g., discard ∃𝑥 𝐾𝑖𝑙𝑙(𝑥, 𝑉𝑖𝑐𝑡𝑖𝑚) after adding 𝐾𝑖𝑙𝑙(𝑀𝑢𝑟𝑑𝑒𝑟𝑒𝑟, 𝑉𝑖𝑐𝑡𝑖𝑚)
• The new 𝐾𝐵 is not logically equivalent to the old but shown to be
inferentially equivalent.
47
Reduction to propositional inference
50
Unification
and Lifting
A first-order inference rule
• If there is some substitution 𝜃 making each of the conjuncts
of the premise identical to sentences already in the 𝐾𝐵
• Then the conclusion can be asserted after applying 𝜃.
• For example,
∀𝑥 𝐾𝑖𝑛𝑔 𝑥 ∧ 𝐺𝑟𝑒𝑒𝑑𝑦 𝑥 ⇒ 𝐸𝑣𝑖𝑙 𝑥
∀𝑦 𝐺𝑟𝑒𝑒𝑑𝑦(𝑦)
𝐾𝑖𝑛𝑔(𝐽𝑜ℎ𝑛)
𝐵𝑟𝑜𝑡ℎ𝑒𝑟 𝑅𝑖𝑐ℎ𝑎𝑟𝑑, 𝐽𝑜ℎ𝑛
• Applying the substitution {𝑥/𝐽𝑜ℎ𝑛, 𝑦/𝐽𝑜ℎ𝑛} to 𝐾𝑖𝑛𝑔(𝑥) and 𝐺𝑟𝑒𝑒𝑑𝑦(𝑥),
𝐾𝑖𝑛𝑔(𝐽𝑜ℎ𝑛) and 𝐺𝑟𝑒𝑒𝑑𝑦(𝑦) will make them identical in pairs.
• Thus, infer the conclusion of the implication
52
Generalized Modus Ponens (GMP)
• For atomic sentences 𝑝𝑖 , 𝑝𝑖′ and 𝑞, where there exists 𝜃 such
that 𝑆𝑈𝐵𝑆𝑇(𝜃, 𝑝𝑖′ ) = 𝑆𝑈𝐵𝑆𝑇(𝜃, 𝑝𝑖 ), for all 𝑖,
𝒑′𝟏 , 𝒑′𝟐 , … , 𝒑′𝒏 , (𝒑𝟏 , 𝒑𝟐 , … , 𝒑𝒏 ⇒ 𝒒)
𝑺𝑼𝑩𝑺𝑻(𝜽, 𝒒)
′
• For example, 𝑝1 is 𝐾𝑖𝑛𝑔(𝐽𝑜ℎ𝑛) 𝑝1 is 𝐾𝑖𝑛𝑔(𝑥)
𝑝2′ is 𝐺𝑟𝑒𝑒𝑑𝑦(𝑦) 𝑝2 is 𝐺𝑟𝑒𝑒𝑑𝑦(𝑥)
𝜃 is {𝑥/𝐽𝑜ℎ𝑛, 𝑦/𝐽𝑜ℎ𝑛} 𝑞 is 𝐸𝑣𝑖𝑙(𝑥)
𝑆𝑈𝐵𝑆𝑇(𝜃, 𝑞) is 𝐸𝑣𝑖𝑙(𝐽𝑜ℎ𝑛)
• For example,
p q θ
Knows(John, x) Knows(John, Jane) {x/Jane}
Knows(John, x) Knows(y, Steve) {x/Steve, y/John}
Knows(John, x) Knows(y, Mother(y)) {x/Mother(John), y/John}
Knows(John, x) Knows(x, Steve) fail
55
MGU: Some examples
𝝎𝟏 𝝎𝟐 𝑴𝑮𝑼
𝐴(𝐵, 𝐶) 𝐴(𝑥, 𝑦) {𝑥/𝐵, 𝑦/𝐶)
𝐴(𝑥, 𝑓(𝐷, 𝑥)) 𝐴(𝐸, 𝑓(𝐷, 𝑦)) {𝑥/𝐸, 𝑦/𝐸}
𝐴(𝑥, 𝑦) 𝐴(𝑓(𝐶, 𝑦), 𝑧) {𝑥/𝑓(𝐶, 𝑦), 𝑦/𝑧}
𝑃(𝐴, 𝑥, 𝑓(𝑔(𝑦))) 𝑃(𝑦, 𝑓(𝑧), 𝑓(𝑧)) {𝑦/𝐴, 𝑥/𝑓(𝑧), 𝑧/𝑔(𝐴)}
𝑃(𝑥, 𝑔(𝑓(𝐴)), 𝑓(𝑥)) 𝑃(𝑓(𝑦), 𝑧, 𝑦) No MGU
𝑃(𝑥, 𝑓(𝑦)) 𝑃(𝑧, 𝑔(𝑤)) No MGU
56
The unification algorithm
function UNIFY(x , y, θ) returns a substitution to make x and y identical
inputs: x , a variable, constant, list, or compound expression
y, a variable, constant, list, or compound expression
θ, the substitution built up so far (optional, defaults to empty)
if θ = failure then return failure
else if x = y then return θ
else if VARIABLE?(x) then return UNIFY-VAR(x , y, θ)
else if VARIABLE?(y) then return UNIFY-VAR(y, x , θ)
else if COMPOUND?(x) and COMPOUND?(y) then
return UNIFY(x.ARGS, y.ARGS, UNIFY(x.OP, y.OP, θ))
else if LIST?(x) and LIST?(y) then
return UNIFY(x.REST, y.REST, UNIFY(x.FIRST, y.FIRST, θ))
else return failure
57
The unification algorithm
58
a is a constant
59
60
Quiz 02: Find the MGU
• Find the MGU when performing 𝑈𝑁𝐼𝐹𝑌(𝑝, 𝑞)
𝝎𝟏 𝝎𝟐 𝑴𝑮𝑼
𝑃(𝑓 𝐴 , 𝑔(𝑥)) 𝑃(𝑦, 𝑦) ?
𝑃(𝐴, 𝑥, ℎ(𝑔 𝑧 )) 𝑃(𝑧, ℎ 𝑦 , ℎ 𝑦 ) ?
𝑃(𝑥, 𝑓(𝑥), 𝑧) 𝑃(𝑔(𝑦), 𝑓(𝑔(𝑏)), 𝑦) ?
𝑃(𝑥, 𝑓(𝑥)) 𝑃(𝑓(𝑦), 𝑦) ?
𝑃(𝑥, 𝑓(𝑧)) 𝑃(𝑓 𝑦 , 𝑦) ?
61
Forward Chaining
62
First-order definite clause
• A definite clause is a disjunction of literals of which exactly
one is positive.
• It is either atomic or an implication whose antecedent is a
conjunctions of positive literals and consequent is a positive literal.
• E.g., 𝐾𝑖𝑛𝑔(𝑥) ∧ 𝐺𝑟𝑒𝑒𝑑𝑦(𝑥) ⇒ 𝐸𝑣𝑖𝑙(𝑥), 𝐾𝑖𝑛𝑔(𝐽𝑜ℎ𝑛), 𝐺𝑟𝑒𝑒𝑑𝑦(𝑦)
• First-order literals can include variables, which are assumed
to be universally quantified.
• Not every 𝐾𝐵 can be converted into a set of definite clauses
due to the single-positive-literal restriction.
63
FOL definite clause: An example
• Consider the following problem
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.
𝑂𝑤𝑛𝑠 𝑁𝑜𝑛𝑜, 𝑀1
∃ 𝑥 𝑂𝑤𝑛𝑠(𝑁𝑜𝑛𝑜, 𝑥) ∧ 𝑀𝑖𝑠𝑠𝑖𝑙𝑒(𝑥)
𝑀𝑖𝑠𝑠𝑖𝑙𝑒 𝑀1
66
Forward chaining: An example
𝑀𝑖𝑠𝑠𝑖𝑙𝑒(𝑥) ∧ 𝑂𝑤𝑛𝑠(𝑁𝑜𝑛𝑜, 𝑥) ⇒ 𝑆𝑒𝑙𝑙𝑠(𝑊𝑒𝑠𝑡, 𝑥, 𝑁𝑜𝑛𝑜)
𝑀𝑖𝑠𝑠𝑖𝑙𝑒(𝑥) ⇒ 𝑊𝑒𝑎𝑝𝑜𝑛(𝑥)
𝐸𝑛𝑒𝑚𝑦(𝑥, 𝐴𝑚𝑒𝑟𝑖𝑐𝑎) ⇒ 𝐻𝑜𝑠𝑡𝑖𝑙𝑒(𝑥)
67
Forward chaining: An example
𝐴𝑚𝑒𝑟𝑖𝑐𝑎𝑛(𝑥) ∧ 𝑊𝑒𝑎𝑝𝑜𝑛(𝑦) ∧ 𝑆𝑒𝑙𝑙𝑠(𝑥, 𝑦, 𝑧) ∧ 𝐻𝑜𝑠𝑡𝑖𝑙𝑒(𝑧) ⇒ 𝐶𝑟𝑖𝑚𝑖𝑛𝑎𝑙(𝑥)
Fixed point
68
Forward chaining
• Soundness?
• YES, every inference is just an application of GMP.
• Completeness?
• YES for definite clause knowledge bases.
• It answers every query whose answers are entailed by any 𝐾𝐵 of
definite clauses.
• Terminate for Datalog in finite number of iterations
• Datalog = first-order definite clauses + no functions
• Entailment with definite clauses is semidecidable.
• May not terminate in general if 𝛼 is not entailed, unavoidable.
69
Renaming
• A fact is not “new” if it is just a renaming of a known fact.
• One sentence is a renaming of another if they are identical
except for the names of the variables.
• E.g., Likes(x, IceCream) vs. Likes(y, IceCream)
70
Definite clauses with function symbols
71
Efficient forward chaining
• Incremental forward chaining
• No need to match a rule on iteration 𝑘 if a premise was not added on
iteration 𝑘 − 1 → match each rule whose premise contains a newly
added positive literal
• Matching itself can be expensive
• Database indexing allows 𝑂(1) retrieval of known facts
• E.g., query 𝑀𝑖𝑠𝑠𝑖𝑙𝑒(𝑥) retrieves 𝑀𝑖𝑠𝑠𝑖𝑙𝑒(𝑀1)
• Widely used in deductive databases
72
Quiz 03: Forward chaining
• Given a KB containing the following sentence
1. 𝑃𝑎𝑟𝑒𝑛𝑡(𝑥, 𝑦) ∧ 𝑀𝑎𝑙𝑒(𝑥) ⇒ 𝐹𝑎𝑡ℎ𝑒𝑟(𝑥, 𝑦)
2. 𝐹𝑎𝑡ℎ𝑒𝑟(𝑥, 𝑦) ∧ 𝐹𝑎𝑡ℎ𝑒𝑟(𝑥, 𝑧) ⇒ 𝑆𝑖𝑏𝑙𝑖𝑛𝑔(𝑦, 𝑧)
3. 𝑃𝑎𝑟𝑒𝑛𝑡(𝑇𝑜𝑚, 𝐽𝑜ℎ𝑛)
4. 𝑀𝑎𝑙𝑒(𝑇𝑜𝑚)
5. 𝑃𝑎𝑟𝑒𝑛𝑡(𝑇𝑜𝑚, 𝐹𝑟𝑒𝑑)
• Perform the forward chaining until a fixed point is reached.
73
Backward Chaining
74
A backward chaining algorithm
function FOL-BC-ASK(KB,query) returns a generator of substitutions
return FOL-BC-OR(KB,query, { })
generator FOL-BC-OR(KB,goal, θ) yields a substitution
for each rule (lhs rhs) in FETCH-RULES-FOR-GOAL(KB, goal) do
(lhs, rhs) ← STANDARDIZE-VARIABLES((lhs, rhs))
for each θ’ in FOL-BC-AND(KB,lhs, UNIFY(rhs, goal, θ)) do
yield θ’
generator FOL-BC-AND(KB,goals, θ) yields a substitution
if θ = failure then return
else if LENGTH(goals) = 0 then yield θ
else do
first,rest ← FIRST(goals), REST(goals)
for each θ’ in F'OL-BC-OR(KB, SUBST(θ, first), θ) do
for each θ’’ in FOL-BC-AND(KB,rest, θ’) do
yield θ’’ 75
Backward chaining: An example
76
Backward chaining: An example
77
Backward chaining: An example
78
Backward chaining: An example
79
Backward chaining: An example
80
Backward chaining: An example
81
Backward chaining: An example
82
Properties of backward chaining
• Depth-first recursive proof search
• Space is linear in size of proof
• Incomplete due to infinite loops
• Fix by checking current goal against every goal on stack
• Inefficient due to repeated subgoals (success and failure)
• Fix using caching of previous results (extra space)
• Widely used for logic programming
83
Quiz 04: Backward chaining
• Given a KB containing the following sentence
1. 𝑃𝑎𝑟𝑒𝑛𝑡(𝑥, 𝑦) ∧ 𝑀𝑎𝑙𝑒(𝑥) ⇒ 𝐹𝑎𝑡ℎ𝑒𝑟(𝑥, 𝑦)
2. 𝐹𝑎𝑡ℎ𝑒𝑟(𝑥, 𝑦) ∧ 𝐹𝑎𝑡ℎ𝑒𝑟(𝑥, 𝑧) ⇒ 𝑆𝑖𝑏𝑙𝑖𝑛𝑔(𝑦, 𝑧)
3. 𝑃𝑎𝑟𝑒𝑛𝑡(𝑇𝑜𝑚, 𝐽𝑜ℎ𝑛)
4. 𝑀𝑎𝑙𝑒(𝑇𝑜𝑚)
5. 𝑃𝑎𝑟𝑒𝑛𝑡(𝑇𝑜𝑚, 𝐹𝑟𝑒𝑑)
• Find solution(s) to each of the following queries
• Parent(Tom, x) • Father(f, s)
• Father(Tom, s) • Sibling(a, b)
84
Quiz 04: Backward chaining
• Query: Parent(Tom,x)
• Answers: ( {x/John}, {x/Fred})
• Query: Father(Tom,s)
• Subgoal: Parent(Tom,s) ∧ Male(Tom)
• {s/John}
• Subgoal: Male(Tom)
• Answer: {s/John}
• {s/Fred}
• Subgoal: Male(Tom)
• Answer: {s/Fred}
87
Conversion to CNF
Everyone who loves all animals is loved by someone.
∀𝑥 ∀𝑦 𝐴𝑛𝑖𝑚𝑎𝑙 𝑦 ⇒ 𝐿𝑜𝑣𝑒𝑠(𝑥, 𝑦) ⇒ ∃𝑦 𝐿𝑜𝑣𝑒𝑠(𝑦, 𝑥)
1. Eliminate implications
∀𝑥 ¬∀𝑦 ¬𝐴𝑛𝑖𝑚𝑎𝑙 𝑦 ∨ 𝐿𝑜𝑣𝑒𝑠(𝑥, 𝑦) ∨ ∃𝑦 𝐿𝑜𝑣𝑒𝑠(𝑦, 𝑥)
2. Move inwards: ¬∀𝑥 𝑝 ≡ ∃𝑥¬𝑝, ¬∃𝑥 𝑝 ≡ ∀𝑥 ¬𝑝
88
Conversion to CNF
Everyone who loves all animals is loved by someone.
∀𝑥 ∀𝑦 𝐴𝑛𝑖𝑚𝑎𝑙 𝑦 ⇒ 𝐿𝑜𝑣𝑒𝑠(𝑥, 𝑦) ⇒ ∃𝑦 𝐿𝑜𝑣𝑒𝑠(𝑦, 𝑥)
6. Distribute ∨ over ∧
𝐴𝑛𝑖𝑚𝑎𝑙 𝐹(𝑥) ∨ 𝐿𝑜𝑣𝑒𝑠(𝐺(𝑥), 𝑥) ∧
¬𝐿𝑜𝑣𝑒𝑠 𝑥, 𝐹(𝑥) ∨ 𝐿𝑜𝑣𝑒𝑠(𝐺(𝑥), 𝑥)
90
The resolution inference rule
• Simply a lifted version of the propositional resolution rule
• Formulation 𝑙1 ∨ ⋯ ∨ 𝑙𝑘
𝑚1 ∨ ⋯ ∨ 𝑚𝑛
𝐒𝐔𝐁𝐒𝐓(𝜽, 𝑙1 ∨ ⋯ ∨ 𝑙𝑖−1 ∨ 𝑙𝑖+1 ∨ ⋯ ∨ 𝑙𝑘 ∨ 𝑚1 ∨ ⋯ ∨ 𝑚𝑗−1 ∨ 𝑚𝑗+1 ∨ ⋯ ∨ 𝑚𝑛
• For example,
𝐴𝑛𝑖𝑚𝑎𝑙 𝐹(𝑥) ∨ 𝐿𝑜𝑣𝑒𝑠 𝐺 𝑥 , 𝑥
¬𝐿𝑜𝑣𝑒𝑠 𝑢, 𝑣 ∨ ¬𝐾𝑖𝑙𝑙𝑠 𝑢, 𝑣
𝜃 = {𝑢Τ𝐺 𝑥 , 𝑣Τ𝑥}
𝐴𝑛𝑖𝑚𝑎𝑙 𝐹(𝑥) ∨ ¬𝐾𝑖𝑙𝑙𝑠 𝐺(𝑥), 𝑥
91
Resolution: An example
92
Resolution: Another example
Everyone who loves all animals is loved by someone.
Anyone who kills an animal is loved by no one.
Jack loves all animals.
Either Jack or Curiosity killed the cat, who is named Tuna.
Did Curiosity kill the cat?
A. 𝐴𝑛𝑖𝑚𝑎𝑙(𝐹(𝑥) ∨ 𝐿𝑜𝑣𝑒𝑠(𝐺(𝑥), 𝑥)
¬𝐿𝑜𝑣𝑒𝑠 𝑥, 𝐹(𝑥) ∨ 𝐿𝑜𝑣𝑒𝑠(𝐺(𝑥), 𝑥)
B. ¬𝐿𝑜𝑣𝑒𝑠(𝑦, 𝑥) ∨ ¬𝐴𝑛𝑖𝑚𝑎𝑙 𝑧 ∨ ¬𝐾𝑖𝑙𝑙𝑠(𝑥, 𝑧)
C. ¬𝐴𝑛𝑖𝑚𝑎𝑙 𝑥 ∨ 𝐿𝑜𝑣𝑒𝑠(𝐽𝑎𝑐𝑘, 𝑥)
D. 𝐾𝑖𝑙𝑙𝑠 𝐽𝑎𝑐𝑘, 𝑇𝑢𝑛𝑎 ∨ 𝐾𝑖𝑙𝑙𝑠(𝐶𝑢𝑟𝑖𝑜𝑢𝑠𝑖𝑡𝑦, 𝑇𝑢𝑛𝑎)
E. 𝐶𝑎𝑡(𝑇𝑢𝑛𝑎)
F. ¬𝐶𝑎𝑡(𝑥) ∨ 𝐴𝑛𝑖𝑚𝑎𝑙 𝑥
G. ¬ 𝐾𝑖𝑙𝑙𝑠(𝐶𝑢𝑟𝑖𝑜𝑠𝑖𝑡𝑦, 𝑇𝑢𝑛𝑎) 94
Resolution: Another example
Suppose Curiosity did not kill Tuna. We know that either Jack or Curiosity
did; thus Jack must have. Now, Tuna is a cat and cats are animals, so
Tuna is an animal. Because anyone who kills an animal is loved by no
one, we know that no one loves Jack. On the other hand, Jack loves all
animals, so someone loves him; so we have a contradiction.
Therefore, Curiosity killed the cat.
95
Quiz 05: Resolution
• Given a KB of the following sentences
• Anyone whom Mary loves is a football star.
• Any student who does not pass does not play.
• John is a student.
• Any student who does not study does not pass.
• Anyone who does not play is not a football star.
• Prove that If John does not study, Mary does not love John.
• Write the FOL sentences using only the given predicates
Loves(x, y): “x loves y” Star(x): “x is a football star”
Student(x): “x is a student” Pass(x): “x passes”
Play(x): “x plays” Study(x): “x studies”
96
THE END
97