Propositional and First-Order Logic: Andreas Geyer-Schulz and Chuck Dyer
Propositional and First-Order Logic: Andreas Geyer-Schulz and Chuck Dyer
Propositional and First-Order Logic: Andreas Geyer-Schulz and Chuck Dyer
First-Order Logic
1
Some material adopted from notes by Andreas Geyer-Schulz and Chuck Dyer
Logic roadmap overview
• Propositional logic (review)
• Problems with propositional logic
• First-order logic (review)
– Properties, relations, functions, quantifiers, …
– Terms, sentences, wffs, axioms, theories, proofs,
…
• Extensions to first-order logic
• Logical agents
– Reflex agents
– Representing change: situation calculus, frame
problem
– Preferences on actions
2
– Goal-based agents
Propositional
Logic:
Review
3
Big Ideas
• Logic is a great knowledge representation
language for many AI problems
• Propositional logic is the simple
foundation and fine for some AI problems
• First order logic (FOL) is much more
expressive as a KR language and more
commonly used in AI
• There are many variations: horn logic,
higher order logic, three-valued logic,
probabilistic logics, etc.
4
Propositional logic
• Logical constants: true, false
• Propositional symbols: P, Q,... (atomic
sentences)
• Wrapping parentheses: ( … )
• Sentences are combined by connectives:
and [conjunction]
or [disjunction]
implies [implication / conditional]
is equivalent [biconditional]
not [negation]
• Literal: atomic sentence or negated atomic
sentence
P, P
5
Examples of PL sentences
• (P Q) R
“If it is hot and humid, then it is raining”
•QP
“If it is humid, then it is hot”
•Q
“It is humid.”
• We’re free to choose better symbols, btw:
Ho = “It is hot”
Hu = “It is humid”
R = “It is raining”
6
Propositional logic (PL)
• Simple language for showing key ideas and definitions
• User defines set of propositional symbols, like P and Q
• User defines semantics of each propositional
symbol:
– P means “It is hot”, Q means “It is humid”, etc.
• A sentence (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
7
rules
Some terms
• The meaning or semantics of a sentence
determines its interpretation
• Given the truth values of all symbols in a
sentence, it can be “evaluated” to
determine its truth value (True or False)
• A model for a KB is a possible world – an
assignment of truth values to
propositional symbols that makes each
sentence in the KB True
8
Model for a KB
• Let the KB be [PQR, Q P]
• What are the possible models? Consider all
possible assignments of T|F to P, Q and R and
check truth tables
– FFF: OK
– FFT: OK P: it’s hot
– FTF: NO Q: it’s humid
– FTT: NO R: it’s raining
– TFF: OK
– TFT: OK
– TTF: NO
– TTT: OK
• If KB is [PQR, Q P, Q], then the only model
9
is TTT
More terms
• A valid sentence or tautology is a sentence
that is True under all interpretations, no matter
what the world is actually like or what the
semantics is. Example: “It’s raining or it’s not
raining”
• An inconsistent sentence or contradiction
is a sentence that is False under all
interpretations. The world is never like what it
describes, as in “It’s raining and it’s not raining.”
• P entails Q, written P |= Q, means that
whenever P is True, so is Q. In other words, all
models of P are also models of Q.
10
Truth tables
• Truth tables are used to define logical connectives
• and to determine when a complex sentence is true
given the values of the symbols in it
Truth tables for the five logical connectives
11
On the implies connective: P
Q
• Note that is a logical connective
• So PQ is a logical sentence and has a
truth value, i.e., is either true or false
• If we add this sentence to the KB, it can
be used by an inference rule, Modes
Ponens, to derive/infer/prove Q if P is
also in the KB
• Given a KB where P=True and Q=True,
we can also derive/infer/prove that PQ
is True 12
PQ
• When is PQ true? Check all that
apply
P=Q=true
P=Q=false
P=true, Q=false
P=false, Q=true
13
PQ
• When is PQ true? Check all that apply
✔ P=Q=true
✔ P=Q=false
P=true, Q=false
✔ P=false, Q=true
• We can get this from the truth table for
• Note: in FOL it’s much harder to prove
that a conditional true.
– Consider proving prime(x) odd(x)
14
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.
– Note analogy to complete search algorithms
15
Sound rules of inference
• Here are some examples of sound rules of inference
• Each can be shown to be sound using a truth table
RULE PREMISE CONCLUSION
Modus Ponens A, A B B
And Introduction A, B AB
And Elimination AB A
Double Negation A A
Unit Resolution A B, B A
Resolution A B, B C A C
16
Soundness of modus ponens
A B A→B OK?
True True True
True False False
False True True
False False True
17
Resolution
• Resolution is a valid inference rule producing
a new clause implied by two clauses containing
complementary literals
– A literal is an atomic symbol or its negation, i.e., P,
~P
• Amazingly, this is the only interference rule you
need to build a sound and complete theorem
prover
– Based on proof by contradiction and usually called
resolution refutation
• The resolution rule was discovered by Alan
Robinson (CS, U. of Syracuse) in the mid 60s 18
Resolution
• A KB is actually a set of sentences all of which
are true, i.e., a conjunction of sentences.
• To use resolution, put KB into conjunctive normal
form (CNF), where each sentence written as a
disjunc- tion of (one or more) literals
Tautologies
Example (AB)↔(~AB)
• KB: [PQ , QRS] (A(BC)) ↔(AB)(AC)
• KB in CNF: [~PQ , ~QR , ~QS]
• Resolve KB(1) and KB(2) producing: ~PR (i.e., PR)
• Resolve KB(1) and KB(3) producing: ~PS (i.e., PS)
• New KB: [~PQ , ~Q~R~S , ~PR , ~PS]
19
Soundness of the
resolution inference rule
23
Entailment and derivation
• Entailment: KB |= Q
– Q is entailed by KB (set sentences) iff there is
no logically possible world where Q is false
while all the sentences in KB are true
– Or, stated positively, Q is entailed by KB iff the
conclusion is true in every logically possible
world in which all the premises in KB are true
• Derivation: KB |- Q
– We can derive Q from KB if there’s a proof
consisting of a sequence of valid inference
steps starting from the premises in KB and
resulting in Q 24
Two important properties for inference
Soundness: If KB |- Q then KB |= Q
– If Q is derived from KB using a given set of rules
of inference, then Q is entailed by KB
– Hence, inference produces only real entailments,
or any sentence that follows deductively from the
premises is valid
Completeness: If KB |= Q then KB |- Q
– If Q is entailed by KB, then Q can be derived from
KB using the rules of inference
– Hence, inference produces all entailments, or all
valid sentences can be proved from the premises
25
Problems with
Propositional
Logic
26
Propositional logic: pro and con
• Advantages
– Simple KR language sufficient for some problems
– Lays the foundation for higher logics (e.g., FOL)
– Reasoning is decidable, though NP complete, and
efficient techniques exist for many problems
• Disadvantages
– Not expressive enough for most problems
– Even when it is, it can very “un-concise”
27
PL is a weak KR language
• 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 (FOL) is expressive enough to
represent this kind of information using 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))
28
PL Example
• Consider the problem of representing the
following information:
– Every person is mortal.
– Confucius is a person.
– Confucius is mortal.
• How can these sentences be represented so
that we can infer the third sentence from the
first two?
29
PL Example
• In PL we have to create propositional symbols to stand
for all or part of each sentence, e.g.:
P = “person”; Q = “mortal”; R = “Confucius”
• The above 3 sentences are represented as:
P Q; R P; R Q
• The 3rd sentence is entailed by the first two, but we
need an explicit symbol, R, to represent an individual,
Confucius, who is a member of the classes person and
mortal
• Representing other individuals requires introducing
separate symbols for each, with some way to represent
the fact that all individuals who are “people” are also
“mortal” 30
Hunt the Wumpus domain
• Some atomic propositions:
S12 = There is a stench in cell (1,2)
B34 = There is a breeze in cell (3,4)
W22 = Wumpus is in cell (2,2)
V11 = We’ve visited cell (1,1)
OK11 = Cell (1,1) is safe.
…
• Some rules:
(R1) S11 W11 W12 W21
(R2) S21 W11 W21 W22 W31
(R3) S12 W11 W12 W22 W13
(R4) S12 W13 W12 W22 W11
…
We can prove
that the Wumpus
is in (1,3) using
the four rules
given.
32
Proving W13
Apply MP with S11 and R1:
W11 W12 W21
Apply And-Elimination to this, yielding 3 sentences:
W11, W12, W21
Apply MP to ~S21 and R2, then apply And-elimination:
W22, W21, W31
Apply MP to S12 and R4 to obtain:
W13 W12 W22 W11
Apply Unit resolution on (W13 W12 W22 W11) and W11:
W13 W12 W22
Apply Unit Resolution with (W13 W12 W22) and W22:
W13 W12
Apply UR with (W13 W12) and W12:
33
W13
Propositional Wumpus hunter problems
36
First-Order Logic
• AKA First-Order Predicate Logic
• AKA First-Order Predicate Calculus
37
Pros and Cons of Propositional
Logic
• + Propositional logic is declarative: pieces of syntax
correspond to facts
• + Propositional logic allows for partial / disjunctive /
negated information (unlike most data structures and DB
• + Propositional logic is compositional: the meaning of
B11 ^ P12 is derived from the meaning of B11 and P12
• + Meaning of propositional logic is context independent:
(unlike natural language, where the meaning depends
on the context)
39
Logics in General
Language Ontological Epistemological
Commitment Commitment
Propositional Logic Facts True / False /
Unknown
First-Order Logic Fact, objects, relations True / False /
Unknown
Temporal Logic Facts, objects, True / False /
relations, times Unknown
Probability Theory Facts Degree of belief
[0,1]
Fuzzy Logic Degree of truth Known interval value
[0,1]
40
Syntax of First-Order Logic
• Constants KingJohn, 2, …
• Predicates Brother, >, …
• Functions Sqrt, LeftArmOf, …
• Variables x, y, a, b, …
• Connectives ¬
• Equality =
• Quantifiers
41
Components of First-Order Logic
• Term
– Constant, e.g. Red
– Function of constant, e.g. Color(Block1)
• Atomic Sentence
– Predicate relating objects (no variable)
• Brother (John, Richard)
• Married (Mother(John), Father(John))
• Complex Sentences
– Atomic sentences + logical connectives
• Brother (John, Richard) Brother (John, Father(John))
42
Components of First-Order Logic
• Quantifiers
– Each quantifier defines a variable for the duration of
the following expression, and indicates the truth of
the expression…
45
Universal Quantification
<variables> <sentence>
x P is true in a model m iff P with x being
each possible object in the model
46
A Common Mistake to Avoid
• Typically is the main connective with
47
Existential Quantification
<variables> <sentence>
x P is true in a model m iff P with x being at
least one possible object in the model
48
Another Common Mistake to Avoid
• Typically, is the main connective with
49
Examples
• Everyone likes McDonalds
x, likes(x, McDonalds)
53
Fun with Sentences
• Brothers are siblings
x,y Brother(x,y) Sibling(x, y)
• Sibling is “symmetric”
x,y Sibling(x,y) Sibling(y, x)
54
Fun with Sentences
• One’s mother is one’s female parent
x,y Mother(x,y) (Female(x) Parent(x,y))
55
Other Comments About
Quantification
• To say “everyone likes McDonalds”, the following is too
broad!
x, likes(x, McDonalds)
– Rush’s example: likes (McDonalds, McDonalds)
58
Interacting with FOL KBs
• Tell the system assertions
– Facts :
• Tell (KB, person (John) )
– Rules:
• Tell (KB,x, person(x) likes(x, McDonalds))
• Ask questions
– Ask (KB, person(John))
– Ask (KB, likes(John, McDonalds))
– Ask (KB, likes(x, McDonalds))
59
Types of Answers
• Fact is in the KB
– Yes.
• Fact is not in the KB
– Yes (if it can be proven from the KB)
– No (otherwise)
• Fact contains variables
– List of bindings for which the fact can be
proven, e.g. ((x Fred) (x Mary) … )
60
Interacting with FOL KBs
• Suppose a wumpus-world agent is using a FOL
KB and perceive a smell and breeze (but no
glitter) at t=5
• Example:
– S = Taller( x, y )
– = {x/Mike, y/Laurie}
– S = Taller( Mike, Laurie )
• “Reflex”
t AtGold(t) Action(Grab, t)
• “Reflex with internal state”
t AtGold(t) ¬Holding(Gold, t) Action(Grab, t)
• Neither is complete
– E.g. the causal rule doesn’t say whether squares far away from
pits can be breezy
64
Deducing Hidden Properties
• Definition for the Breezy predicate:
– If a square is breezy, some adjacent square must
contain a pit
y Breezy(y) x Pit(x) Adjacent( x, y )
65
Keeping Track of Change
• Often, facts hold in situations, rather than
eternally
– E.g. Holding( Gold, now ) rather than just
Holding( Gold )
• Situation calculus is one way to represent
change in FOL:
– Adds a situation argument to each non-
eternal predicate
• E.g. Now in Holding(Gold, Now) denotes a
situation
66
Keeping Track of Change
• Situations are
connected by the
Result function
Result( a, s ) is the
situation that results
from doing a in s
67
Describing Actions
• “Effect” axiom – describe changes due to
action
• “Frame” axiom – describe non-changes
due to action
• Frame problem:
– Find an elegant way to handle non-change
• (A) representation – avoid frame axioms
• (B) inference – avoid repeated “copy-overs” to
keep track of state
68
Describing Actions
• “Effect” axiom – describe changes due to
action
• “Frame” axiom – describe non-changes
due to action
• Frame problem:
– Find an elegant way to handle non-change
• (A) representation – avoid frame axioms
• (B) inference – avoid repeated “copy-overs” to
keep track of state
69
Describing Actions
• Qualification Problem:
– True descriptions of real actions require
endless caveats…
• “What if the gold is slippery or nailed down or…”
• Ramification Problem:
– Real actions have many secondary
consequences…
• “What about the dust on the gold, wear and tear
on gloves,…”
70
Describing Actions
• Successor-state axioms solve the
representational frame problem
71
Making Plans
• Initial condition in KB:
– At( Agent, [1,1], S0 )
– At( Gold, [1,2], S0 )
• Query:
– ASK( KB, s Holding( Gold, s ))
• i.e. in what situation will I be holding the gold?
• Answer:
– {s / Result(Grab , Result(Forward, S0))}
• i.e. go forward and then grab the gold
– This assumes that the agent is interested in plans starting at S0
and that S0 is the only situation described in the KB
72
Making Plans
• A better way to make plans is to represent plans
as a sequence of actions [a1, a2, …, an]
73
Making Plans
• Definition of PlanResult in terms of Result:
s PlanResult([], s) = s
a,p,s PlanResult([a|p],s) = PlanResult(p,
Result(a,s))