Articial Intelligence:
First-Order Logic
Course V231
Department of Computing
Imperial College, London
Jeremy Gow
AI & Logic
Logic is a good KR language (lecture 4)
provides formal basis for many AI techniques
Not the only approach!
alternative symbolic KR schemes
sub-symbolic approaches
e.g. neural networks (see lectures 12 & 13)
Logic-Based Agents
Knowledge base (KB)
Set of logical sentences describing environment
Adds new sentences via...
Observation of environment
Reasoning with existing knowledge
Logical reasoning
Deduction: can X be explained by what I know?
Abduction: what fact would explain X?
Induction: what rule would explain X?
Logical Reasoning
Deduction (mathematician)
{A, AB} so B
Abduction (detective)
{B, AB} so A
Induction (scientist)
Always see B with A so AB
Only deduction is guaranteed
Automated Reasoning
Enables logic-based AI agents
Usually refers to automated deduction
This and next three lectures
Basis for logic programming, e.g. Prolog
Also logic-based machine learning (induction)
See later lectures (inc. inductive LP)
And automated abduction
Outside this course (as is abductive LP)
First-Order Logic
Central to automated deduction in AI
This lecture
Syntax & Semantics
Propositional then first-order
From FOL to Prolog
Lecture 7: Deduction in FOL
Lecture 8-9: Automated deduction in FOL
Propositional Syntax
Constants: true, false, (T/J, T/F, 1/0) truth values
Variables: represent propositions (P, Q, ...)
Brackets: ( and )
Connectives
not (negation) also -
/ and (conjunction) also & , . (or just PQ)
\ or (disjunction) also | ; +
if then (implication) also = .
if & only if (equivalence) also =
Propositional Sentence
A sentence is either
1. A constant or variable
2. P, P / Q, P \ Q, P Q, P Q or (P)
for formulae P and Q
(A / B) (B / A)
A / A
/ A A
Propositional Semantics
Which possible worlds is a sentence true?
A model defines a possible world
Assigns true or false to each variable
Truth table below define connectives
Now know truth of a sentence for any model
P Q P P / Q P \ Q P Q P Q
false false true false false true true
false true true false true true false
true false false false true false false
true true false true true true true
Using Truth Tables
When is (P Q) (P \ Q) true?
Draw truth table showing subformulae vs models
Brackets: good places to stop and work out
It is always true: it is a valid sentence (see L7)
P Q P Q P \ Q ... ...
false false true true true
false true true true true
true false false false true
true true true true true
Propositional Pitfalls
False implies anything
P: 5 is even, Q: 7 is even
P Q is false false is true
Implication does not indicate relevance
P: 5 is odd, Q: Tokyo is capital of Japan
P Q is true true is true
More Propositional Pitfalls
Lecture 4 example: Every Monday and
Wednesday I go to Johns house for dinner
M = Is Monday, W = Is Wednesday
J = I go to Johns house for dinner
M \ W J
and became or
\ is not exclusive: can be Monday and Wednesday
First-Order Syntax
A term is
a constant (lower-case), e.g. apple, red
a variable (upper-case), e.g. X, Y, ...
a function applied to terms, e.g. colour(apple)
A proposition is: a predicate applied to terms
A formula is propositions combined with
propositional connectives (as before)
quantifiers V, H
A sentence is a properly quantified formula
Ground Terms
Constants directly represent objects
physical objects (apple) or concepts (red)
Functions indirectly represent objects
father(john), colour(apple), (1 + 2) + 3
Arity = number of arguments of function
unary, binary, ... n-ary
Ground terms = constants + functions + brackets
They represent specific objects
Semantics of Ground Terms
A first-order model is a pair (, )
is a domain, a non-empty set
is an interpretation, associates ground
terms with elements of
(c) e for constant, (f):
n
for n-ary f
So every ground term mapped to an element of
father(john) and jack are different terms
but could map them to same element
Unwanted Terms
No restrictions on applying functions
Things we want like father(john) and 1 + 1
But also father(1) and red + john
Solution 1: arbitrary interpretation (father(1) to 0)
Solution 2: includes undefined element
maps unwanted applications to undefined
Function with undef. argument is undef.
Practical approach: avoid using these terms
Other solutions in type theory, multi-valued logics,...
Predicates
Predicates are relationships between objects
Relate ground terms: brother(bob, father(bill))
Define arity as before (brother is binary)
A proposition that is true or false
Semantics defined by interpretation
(p):
n
{true, false} for predicate p
False when any argument is undefined
= (equality) is a predicate
Ground Formulae
Ground formulae = ground terms + predicates + connectives
Statements about specific objects
Model (, ) tells us whether true or false
(1 < 2) / (3 < 2)
lectures(simon, ai) / lectures(jeremy, ai)
odd(3 + 2) capital(japan, tokyo)
brother(bob, father(bill)) \ mother(jane, bill)
pred and pred are positive and negative literals
Choosing Predicates & Terms
The cost of an omelette at the Red Lion is 5
cost_at_red_lion_is(omelette, five_pounds)
cost_is(omelette, red_lion, five_pounds)
cost(omelette, red_lion) = five_pounds
Omelettes cost less than pies
cost_is(omelette, a) / cost_is(pie, b) / a < b
cost(omelette) < cost(pie)
Variables
Variables allow us to talk about objects in general
A variable is just another kind of term
Terms: father(X), (X + 2) + Y
Propositions: brother(X, father(Y)), X < (Y + 1)
Formulae: lectures(X, ai) / lectures(Y, ai),
cost(X, red_lion) = 3
Variables make formulae etc. non-ground
Quantiers
For formula f with X e Var(f) (X is free variable)
VX.f is a formula (universal quantification)
HX.f is a formula (existential quantification)
Free variables Var defined by
Var(f) = all variables, for quantifier-free f
Var(VX. f) = Var(HX.f) = Var(f) {X}
Var(X < Y + 1) = {X, Y}
Var(HX. X < Y + 1) = {Y} (X is bound)
Semantics of Quantiers
Substitution {t/X} replaces X with term t
f.{t/X} is a formula f with free var X replaced
For model (, )
VX. f is true iff f.{t/X} is true for all t in
HX.f is true iff f.{t/X} is true for some t in
Formulae = terms + predicates + connectives + quantifiers
A sentence is a formula with no free variables
Only sentences are true or false for a model
Translation Pitfalls
There is a meal at the Red lion which cost 3
HX.(meal(X) / cost(X, red_lion) = 3
All the meals at the Red lion cost 3 (H to V?)
VX.(meal(X) / cost(X, red_lion) = 3)
VX.(meal(X) cost(X, red_lion) = 3)
VX.(meal(X) / serves(red_lion, X) cost(X, red_lion) = 3
Be careful with order and type of quantifiers
Prolog
Declarative programming language
Not procedural
Tell what to compute not how to compute
Logic programming
Algorithm = Logic + Control (Kowalski)
Logic: the representation
Control: the search techniques
Prolog = (FOL Horn clauses) + (SLD resolution)
Horn Clauses
Horn clauses are subset of FOL sentences
VX
1
...VX
n
.((P
1
/ ... / P
m
) H)
for positive literals P
i
and H
P
1
/ ... / P
m
is the body, H is the head
For m = 0 this is a fact VX
1
...VX
n
.H
Often assume Vs
brother(X, Y) / father(Y, Z) uncle(X, Z)
brother(john, X)
Prolog Programs
A Prolog program is a list of FOL Horn clauses
Translate each clause...
1. Drop universal quantifiers: P
1
/ ... / P
m
H
2. Rotate around implication: H P
1
/ ... / P
m
3. Write as :- and /s as commas, and always
end with a full stop: H :- P
1
, ... , P
m
.
Example Translation
If the lecture has a good lecturer and the subject is
interesting then students are awake and listening
VX.(good_lecturer(X) / interesting_subject(X) students_awake(X))
VX.(good_lecturer(X) / interesting_subject(X) students_listening(X))
In Prolog:
students_awake(X) :- good_lecturer(X), interesting_subject(X)
students_listening(X) :- good_lecturer(X), interesting_subject(X)
Search in Prolog
Given a Prolog program (database of Horn
clauses) and a query q(t
1
, ..., t
n
)
Scans database for clauses with n-ary q as head
For head q(s
1
, ..., s
n
) tries to find variable
assignment such that t
i
= s
i
(see next lecture)
Using variable assignment any literals in body
of clause becomes new queries
Succeeds when all queries proved (via facts)
Returns variable assignment for original query
Negation As Failure
Given knowledge pm(thatcher) and pm(blair) we
cant prove pm(brown), but it might be true
Alternately, make the closed world assumption
If we cant prove it then it is false
So pm(brown) is false
Prolog uses negation as failure based on CWA
A query \+q is proved by failing to prove q
Prolog Search Example
uncle(U, N) :- brother(U, F), father(F, N).
uncle(U, N) :- brother(U, M), mother(M, N).
brother(bob, bill).
brother(barry, betty).
father(bill, bruce).
mother(betty, bruce).
?- uncle(X, bruce).
X = bob
?- \+ uncle(brian, bruce).
Yes
Parallelism and Prolog
Two processors P1 and P2
OR-parallelism for query uncle(X, bruce).
P1 takes uncle(U, N) :- brother(U, F), father(F, N).
P2 takes uncle(U, N) :- brother(U, M), mother(M, N).
AND-parallelism
Use uncle(X, bruce) :- brother(X, F), father(F, bruce).
P1 takes brother(X, F)
P2 takes father(F, bruce)
Trickier because values for F must agree
More on Prolog
See online notes (and Russell & Norvig):
How arithmetic is carried out
How performance is measured
Logical Inferences per Second (LIPS)
How performance is improved
By compiling code, e.g. to WAM
LIPS now in millions
Case study: an expert system in Prolog