CS 531 Dr M M Awais 1
FOL
Formal LogicFormal Logic
• The most widely used formal logic method is
FIRST-ORDER PREDICATE LOGIC
Reference: Chapter Two The predicate Calculus
Luger’s Book
Examples included from Norvig and Russel.
CS 531 Dr M M Awais 2
FOL
First-order logic
• Whereas propositional logic assumes the world contains facts,
• first-order logic (like natural language) assumes the world contains– Objects: people, houses, numbers, colors, baseball
games, wars, …– Relations: red, round, prime, brother of, bigger than,
part of, comes between, …– Functions: father of, best friend, one more than, plus,
…
CS 531 Dr M M Awais 3
FOL
FOL
• PL only deals with facts• PL cannot have variables• Variables refer to things in the world• You can deliberate over them• How would represent the following fact in
PLWhen you sterilize a jar all the bacteria are
dead
CS 531 Dr M M Awais 4
FOL
Syntax of FOL: Basic elements
• Constants john, 2, lums,...
• Predicates brother, >,...
• Functions sqrt, leftsideOf,...
• Variables X,Y,A,B...
• Connectives , , , , • Equality =
• Quantifiers ,
CS 531 Dr M M Awais 5
FOL
Syntax of FOL: Basic elements
• Constants John, Lums,...
• Predicates Brother, >,...
• Functions Sqrt, LeftsideOf,...
• Variables x,y,a,b...
• Connectives , , , , • Equality =
• Quantifiers ,
Alternate Convention also exists (Norvig uses the alternate)
Alternate Convention also exists (Norvig uses the alternate)
CS 531 Dr M M Awais 6
FOL
Truth in first-order logic• Sentences are true with respect to a model and an
interpretation
• Model contains objects (domain elements) and relations among them
• Interpretation specifies referents forconstant symbols → objectspredicate symbols → relationsfunction symbols → functional relations
• An atomic sentence predicate(term1,...,termn) is trueiff the objects referred to by term,,..., term ,are in the relation referred to by predicate
CS 531 Dr M M Awais 7
FOL
Alphabets-IAlphabets-I
Predicates, variables, functions,constants, connectives, quantifiers, and delimiters
Constants: (first letter small)bLUE a colorsanTRO a carcrow a birdVariables: (first letter capital)Dog: an element that is a dog, but unspecifiedColor: an unspecified color
CS 531 Dr M M Awais 8
FOL
Alphabets-IIAlphabets-IIFunction (evaluates to a constant / variable):
Maps Sentences to Objects
Ali is father of Babar father_of(ali) = baber father(babar) = ali
Babar is son of Ali son_of(babar) = ali
• Interpretation has to be very clear.• If you write father(baber), the answer should be ali• For the above functions the arity is 1 (number of arguments to
the function)
CS 531 Dr M M Awais 9
FOL
Alphabets-IIAlphabets-IIFunctions:1) shahid likes zahid likes(shahid) =
zahid
2) atif likes abid likes(atif) = abid
3) Constants to Variables likes(X) = Y
{X,Y} have two possible BINDINGSBINDINGS
{X, Y} could be {shahid, zahid}
Or
{X,Y} could be {atif, abid}
likes(X) =Y
Substitutions:
For 1 to be true:{shahid/X, zahid/Y}
For 2 to be true:{atif/X, abid/Y}
CS 531 Dr M M Awais 10
FOL
Alphabets-IIAlphabets-IIPredicate
Maps Sentences to Truth Values (True/False)
1) Shahid is student student(shahid)
2) Sana is a girl girl(sana)
3) Father of baber is elder than Hamza
elder(father(babar), hamza)
For 1 and 2 arity is 1 and for 3 the arity is 2
CS 531 Dr M M Awais 11
FOL
Alphabets-IIAlphabets-IIPredicate1) Shahid is a good studentstudent(shahid,good) or good_student(shahid) oris_good(shahid,student)
2) Sana is a friend of Saima, Sana and Saima both are girls
friend_of(sana,saima)^girl(sana)^girl(saima)3) Bill helps Fredhelps(bill,fred)
CS 531 Dr M M Awais 12
FOL
Atomic sentencesAtomic sentence = predicate (term1,...,termn)
or term1 = term2
term = function (term1,...,termn) or constant
or variable • brother(john,richard)• greater(length(leftsideOf(squareA)), length(leftsideOf(squareB)))• >(length(leftsideOf(squareA)), length(leftsideOf(squareB)))
Functions cannot be atomic sentences
CS 531 Dr M M Awais 13
FOL
Alphabets-IIIAlphabets-IIIConnectives:^ andv or~ not
ImplicationQuantificationAll persons can seeThere is a person who cannot see
Universal quantifiers (ALL)Existential quantifiers (There exists)
CS 531 Dr M M Awais 14
FOL
Complex sentences
• Complex sentences are made from atomic sentences using connectives
S, S1 S2, S1 S2, S1 S2, S1 S2,
sibling(ali,hamza) sibling(hamza,ali) >(1,2) ≤ (1,2) (1 is greater than 2 or less than equal to 2)
>(1,2) >(1,2) (1 is greater than 2 and is not greater than equal to 2)
CS 531 Dr M M Awais 15
FOL
ExamplesExamples
My house is a blue, two-story, with red shutters, and is a corner houseblue(my-house)^two-story(my-house)^red-shutters(my-
house)^corner(my-house)
Ali bought a scooter or a carbought(ali , car) v bought(ali , scooter)
IF fuel, air and spark are present the fuel will combustpresent(spark)^present(fuel)^present(air)
combustion(fuel)
CS 531 Dr M M Awais 16
FOL
Universal quantification <variables> <sentence>Everyone at LUMS is smart:
X at(X, lums) smart(X)
X P is true in a model m iff P is true with X being each possible object in the model
• Roughly speaking, equivalent to the conjunction of instantiations of P
at(rabia,lums) smart(rabia) at(shahid,lums) smart(shahid) at(lums,lums) smart(lums) ...
•
•
CS 531 Dr M M Awais 17
FOL ExamplesExamplesAll people need air
X[person(X) need_AIR(X)]
The owner of the car also owns the boat
[owner(X , car) ^ car(X , boat)]
Formulate the following expression in the PC:“Ali is a computer science student but not a pilot or
a football player”
cs_STUDENT(ali) ( pilot(ali) ft_PLAYER(ali) )
CS 531 Dr M M Awais 18
FOL
ExamplesExamples
Restate the sentence in the following way:
1. Ali is a computer science (CS) student
2. Ali is not a pilot
3. Ali is not a football player
cs_student(ali)^
~pilot(ali)^
~football_player(ali)
CS 531 Dr M M Awais 19
FOL
ExamplesExamplesStudying fuzzy systems is exciting and applying logic is great
fun if you are not going to spend all of your time slaving over the terminal
X(~slave_terminal(X) [fs_eciting(X)^logic_fun(X)])
Every voter either favors the amendment or despises it
X[voter(X) [favor(X , amendment) v despise(X,amendment)]
^ ~[favor(X , amendment) v
despise(X , amendment)] (this part simply endorses the statement, may not be required)
CS 531 Dr M M Awais 20
FOL
Undecidable Predicate
• For which exhaustive testing is required• Example:
X likes(zahra, X)• This sentence is computationally impossible to
calculate• Scope of problem domain is to be limited to
remove this problem, – i.e., X is a variable representing final year female
student in the AI class, compared to an X representing all the people in the city of Lahore
CS 531 Dr M M Awais 21
FOL
Robotic Arm Example
•Represent the initial details of the system•Generate sentences of descriptive and or implicative nature•Modify the facts using new sentences
a
b
c
d
CS 531 Dr M M Awais 22
FOL
Example: Robotic Arm
• Represent the initial details of the systems
a
b
c
d
on(a,b)on( c,d)ontable(b)ontable(d)clear(a)clear(c)hand_empty
CS 531 Dr M M Awais 23
FOL
Goal:To pick a block and place it over another block
Define predicate: stack_on(X,Y)General sentence:
Conditions ConclusionsWhat could the conditions be?
hand_emptyclear (X)clear (Y)pick (X)put_on (X,Y)
a
b
c
d
hand_empty ^ clear (X) ^ clear (Y) ^ pick (X) ^ put_on (X,Y) stack_on (X,Y)
CS 531 Dr M M Awais 24
FOL
Goal:To pick a block and place it over another block
hand_empty ^ clear (X) ^ clear (Y) ^ pick (X) ^ put_on (X,Y) stack_on (X,Y)
hand_empty ^ clear (X) pick (X) clear(Y) ^ pick (X) put_on (X,Y)
put_on (X,Y) stack_on (X,Y)
Semantically more correct
hand_empty could be written as empty(hand), if hand_empty is in the knowledge base, then hand is empty otherwise false.
put_on (X,Y) stack_on (X,Y)
CS 531 Dr M M Awais 25
FOL
Example: Robotic Arm• Modify details of the systems
a
b
c
d
on(b,a)on( c,d)ontable(b)ontable(d)clear(a)clear(c)hand_empty
a
b
c
d
on(b,a)on( c,d)on(e,a)ontable(b)ontable(d)clear(c)clear(e)hand_empty
e
CS 531 Dr M M Awais 26
FOL
Models for FOL: Example
CS 531 Dr M M Awais 27
FOL
A common mistake to avoid
• Represent: Everyone at LUMS is smart
X at(X,lums) smart(X)X at(X, lums) smart(X)• Common mistake:
using as the main connective with : means“Everyone is at LUMS and everyone is smart”
“Everyone at LUMS is smart” • Typically, is the main connective with
CS 531 Dr M M Awais 28
FOL
Existential quantification <variables> <sentence>
• Someone at LUMS is smart: X at(X,lums) smart(X)
X P is true in a model m iff P is true with X being some possible object in the model
• Roughly speaking, equivalent to the disjunction of instantiations of P
at(sana,lums) smart(sana) at(bashir,lums) smart(bashir) at(lums,lums) smart(lums) ...
CS 531 Dr M M Awais 29
FOL
Another common mistake to avoid
• Typically, is the main connective with
• Common mistake: using as the main connective with :
X at(X,lums) smart(X)
is true even if there is anyone who is not at LUMS!• Even if the antecedent is false the sentence can still
be true (see the truth table of implication).
CS 531 Dr M M Awais 30
FOL
Properties of quantifiers X Y is the same as Y X X Y is the same as Y X
X Y is not the same as Y X X Y loves(X,Y)
“There is a person who loves everyone in the world” Y X loves(X,Y)
“Everyone in the world is loved by at least one person”
• Quantifier duality: each can be expressed using the other X likes(X,car) X likes(X,car) X likes(X,bread) X likes(X,bread)
CS 531 Dr M M Awais 31
FOL
Equality
• term1 = term2 is true under a given interpretation if and only if term1 and term2 refer to the same object
• Sibling in terms of Parent:X,Y sibling(X,Y)
[(X = Y) M,F (M = F) parent(M,X) parent(F,X) parent(M,Y) parent(F,Y)]
CS 531 Dr M M Awais 32
FOL
Using FOL
The kinship domain:• Brothers are siblings
X,Y brother(X,Y) sibling(X,Y)• One's mother is one's female parent
M,C mother(C) = M (female(M) parent(M,C))
• “Sibling” is symmetric
X,Y sibling(X,Y) sibling(Y,X)
CS 531 Dr M M Awais 33
FOL
Rules: Wumpus worldNorvig and Russel book for details(will provide you with handouts)
• Actions– Turn(Right), Turn(Left), Forward, Shoot, Grab,
Release, Climb
• Reflex T glitter(T) bestAction(grab,T)
–
CS 531 Dr M M Awais 34
FOL
Rules: Wumpus worldNorvig and Russel book for details(will provide you with handouts)
• Perception T,S,B percept([S,B,glitter],T) glitter(T)
• Reflex T glitter(T) bestAction(grab,T)
–
CS 531 Dr M M Awais 35
FOL
Rules: Wumpus worldNorvig and Russel book for details(will provide you with handouts)
• Perception T,S,B percept([S,B,glitter],T) glitter(T)
• Reflex T glitter(T) bestAction(grab,T)
–
CS 531 Dr M M Awais 36
FOL
Rules: Wumpus worldNorvig and Russel book for details(will provide you with handouts)
• Perception T,S,B percept([S,B,glitter],T) glitter(T)
• Reflex T glitter(T) bestAction(grab,T)
–
CS 531 Dr M M Awais 37
FOL
Deducing Squares/PropertiesWhat are Adjacent Squares
X,Y,A,B adjacent([X,Y],[A,B]) [A,B] {[X+1,Y], [X-1,Y],[X,Y+1],[X,Y-1]}
Properties of squares:S,T at(agent,S,T) breeze(T) breezy(S)
Squares are breezy near a pit:Diagnostic rule---infer cause from effect
S breezy(S) adjacent(R,S) pit(R)Causal rule---infer effect from cause
R pit(R) [S adjacent(R,S) breezy(S)]
CS 531 Dr M M Awais 38
FOL
Deducing Squares/Properties
Diagnostic rule---infer cause from effect
S breezy(S) adjacent(R,S) pit(R)
Causal rule---infer effect from cause
R pit(R) [S adjacent(R,S) breezy(S)]
CS 531 Dr M M Awais 39
FOL
Knowledge engineering in FOL
1. Identify the task2. Assemble the relevant knowledge3. Decide on a vocabulary of predicates, functions,
and constants4. Encode general knowledge about the domain5. Encode a description of the specific problem
instance6. Pose queries to the inference procedure and get
answers7. Debug the knowledge base
8.9.10.11.12.13.
•
CS 531 Dr M M Awais 40
FOL
Inferencing in FOL
• Interpretation of the objects/variables etc…
• Denotation of terms
• Find whether an interpretation holds for a particular sentence
CS 531 Dr M M Awais 41
FOL
Interpretation I/Denotation
• ‘U’ set of Objects ( U universe of discourse)• Map constants symbols to objects in U
I(zahid), U={zahid, shahid, raza, sana, …} ‘T’
I(X) what happens now?• Map predicates symbols to the relations between the
objects on U• Map function symbols to functions on U
I(predicate(t1, t2)) I(predicate)(I(t1), I(t2))
CS 531 Dr M M Awais 42
FOL
Holds (meta rule)
holds (predicate(t1,t2), I)
iff
<I(t1),I(t2)> I(predicate)
CS 531 Dr M M Awais 43
FOL
Example: Robotic ArmFind if the following sentence holds or not
a
b
c
d
on(a,b)on( c,d)ontable(b)ontable(d)clear(a)clear(c)hand_empty
stack_on (X,Y)
I(X)
I(Y)
I(stack_on)
holds (stack_on(X,Y),I) iff <I(X),I(Y)> I(stack_on))
???
No
stack_on is not in ‘U’
‘on’ Holds but under the following interpretations
{a/X, b/Y} or {c/X, d/Y}
clear(a) holds but clear(b) does not hold
CS 531 Dr M M Awais 44
FOL
Quantifiers
CS 531 Dr M M Awais 45
FOL
FOL: Example What is U?Define the following:Predicates: above 2 circle 1oval 1square 1triangle 1 Function;hat 2
CS 531 Dr M M Awais 46
FOL
FOL: Example
What is U?
I(object) =
I(above) =
Find whether I(square) = ????
CS 531 Dr M M Awais 47
FOL
Reason it Out
• holds (sqaure(object),I) ?
• YES
• holds (above(object,hat(object)),I) ?
• I(hat(object))= sqaure
• holds(above(triangle,sqaure),I)?
• NO• Hence I(square) = FALSE
CS 531 Dr M M Awais 48
FOL
Find the truth of
• holds ( X, oval(X)), I) ?• I(sqaure)={<triangle>}
Note for the studentsShould have solved it when you come next time.
CS 531 Dr M M Awais 49
FOL
The electronic circuits domain
One-bit full adder
•
CS 531 Dr M M Awais 50
FOL
The electronic circuits domain
1. Identify the task– Does the circuit actually add properly?
(circuit verification)2. Assemble the relevant knowledge
– Composed of wires and gates; Types of gates (AND, OR, XOR, NOT)
– Irrelevant: size, shape, color, cost of gates3. Decide on a vocabulary
– Alternatives:type(x1) = xortype(x1, xor)xor(x1)
•
–•
–
–•–
•
CS 531 Dr M M Awais 51
FOL
The electronic circuits domain4. Encode general knowledge of the domain
T1,T2 connected(T1, T2) signal(T1) = signal(T2) T signal(T) = 1 signal(T) = 0– 1 ≠ 0 T1,T2 connected(T1, T2) connected(T2, T1) G type(G) = OR signal(out(1,G)) = 1 N
signal(in(N,G)) = 1 G type(G) = AND signal(out(1,G)) = 0 N
signal(in(N,G)) = 0 G type(G) = XOR signal(out(1,G)) = 1
signal(in(1,G)) ≠ signal(in(2,G)) G type(G) = NOT signal(out(1,G)) ≠
signal(in(1,G))–
•
CS 531 Dr M M Awais 52
FOL
The electronic circuits domain
5. Encode the specific problem instancetype(x1) = xor type(x2) = xor
type(a1) = and type(a2) = and
type(o1) = or
connected(out(1,x1),in(1,x2)) connected(in(1,c1),in(1,x1))
connected(out(1,x1),in(2,a2)) connected(in(1,c1),in(1,a1))
connected(out(1,o2),in(1,o1)) connected(in(2,c1),in(2,x1))
connected(out(1,a1),in(2,o1)) connected(in(2,c1),in(2,a1))
connected(out(1,x2),out(1,c1)) connected(in(3,c1),in(2,x2))
connected(out(1,o1),out(2,c1)) connected(in(3,c1),in(1,a2))
•
CS 531 Dr M M Awais 53
FOL
The electronic circuits domain
6. Pose queries to the inference procedureWhat are the possible sets of values of all the
terminals for the adder circuit? I1,I2,I3,O1,O2 signal(in(1,c_1)) = I1 signal(in(2,c1)) = I2 signal(in(3,c1)) = I3 signal(out(1,c1)) = O1 signal(out(2,o1)) = O2
7. Debug the knowledge baseMay have omitted assertions like 1 ≠ 0
–•
•
–
•
CS 531 Dr M M Awais 54
FOL
Summary
• First-order logic:– objects and relations are semantic primitives– syntax: constants, functions, predicates,
equality, quantifiers
• Increased expressive power: sufficient to define wumpus world
•
–
•
CS 531 Dr M M Awais 55
FOL
Operations/Processes• Entailment: • Unification: Algorithm for determining the
substitutions needed to make two predicate calculus expressions match
• Skolemization: A method of removing or replacing existential quantifiers using Skolem function
• Composition: If S and S` are two substitutions sets, then the composition of S and S` (SS`) is obtained by applying the elements of S’ to the elements of ` and adding the S’ to S
CS 531 Dr M M Awais 56
FOL
Examples (Alternate Convention)
Superscript represents arity of predicates/functions
CS 531 Dr M M Awais 57
FOL
Entailment
A knowledge base entails a sentence IFF the sentence holds in every interpretation in
which the knowledge base holds
In Propositional Logic:A knowledge base entails a sentence IFF the
sentence is TRUE in every interpretation that makes the knowledge base TRUE
CS 531 Dr M M Awais 58
FOL
Entailment (follows from)
KB entails S: for every Interpretation I,
if KB holds in I,
then
S holds in I
CS 531 Dr M M Awais 59
FOL
Entailment
•Computing entailment is impossible in general, because there are Infinitely many possible interpretations•Even computing holds is impossible for interpretations with infinite universes
CS 531 Dr M M Awais 60
FOL
Example
Alternate Convention
CS 531 Dr M M Awais 61
FOL
Example
Alternate Convention
CS 531 Dr M M Awais 62
FOL
Example
Alternate Convention