Title: FirstOrder Logic
1First-Order Logic
- Craig A. Struble, Ph.D.
- Department of Mathematics, Statistics, and
Computer Science
2Overview
- Review of Knowledge Based Agents
- First-Order Logic (FOL)
- Syntax and semantics
- Using FOL
- Introduction to Prolog
- Knowledge Engineering
- Inferencing in FOL
- Unification, forward chaining, backward chaining,
etc.
3Knowledge Based Agents
Percepts
Sensors
State
How the world evolves
What the world is like now
What my actions do
What will it be like if I do action A
Environment
Knowledge Base
Reasoning
What action I should do now
Goals
Actions
Actuators
Agent
4Applications
- Expert systems
- Medical diagnosis
- Fault identification
- Bioinformatics
- Protein and RNA structure prediction
- Natural Language Processing
5Knowledge Representation Approaches
- Procedural
- Data structure and processing are closely tied
together - Declarative
- Data and processing are completely independent
- Declare things known to be true in the
environment, and let inference algorithm tell you
what else is true (or false).
6Knowledge Based Agents
- Spend most effort representing knowledge
- Defining propositions and sentences for KB
- Declaring knowledge
- Inferencing allows agent to derive new knowledge
- If sound, new knowledge is guaranteed to be true
if everything in KB is true - If complete, then all possible knowledge from
knowledge base can be derived
7Propositional Logic
- Effective representation of goals
- Semantics based on truth of sentences in possible
worlds - Can work with partial information
- Compositionality
- Complete algorithms exist
- Resolution algorithm (requires CNF)
- Efficient algorithms exist for restricted
representations - Horn clauses
8Limitations of Propositional Logic
- Generalizing rules is expensive
- e.g., locations on minesweeper board, time
- Difficult to represent relationships between
entities/objects/etc. in the environment - Have to keep creating symbols, adding indices,
etc.
9Ideas from Natural Languages
- Natural languages are extremely expressive
- But, they are ambiguous, making them difficult to
use from computation - Exercise Define spring.
- Provide many useful representational ideas
- Objects (nouns)
- Relations (verbs)
- Functions (one value for an input)
- All of these ideas appear in first order logic
10Models for First Order Logic
- Contains objects and relations
11Terminology
- The domain of a model is the set of objects it
contains - e.g., Richard, John, ,
- Objects are sometimes called domain elements
- Relations describe related objects
- Formally defined by tuples of objects
- e.g., on head lt , Johngt
- crown lt gt
binary
unary
12Terminology
- A function is a relation that has a single
value for a given input - e.g. left leg of John is
- Could be represented as
- left leg ltJohn, gt,
- except a relation does not represent an object.
For example, might want - body part ltleft leg of Johngt,
- In general, inputs to functions are tuples
- ltMilwaukee, 3/16/2003gt 67
13Syntax of FOL
14Terms
- Terms refer to objects (constants, variables,
functions) - Arguments for predicates and functions
- Brother(John, Richard)
- LeftLegOf(John)
- ?Brother(LeftLegOf(Richard),John)
- ?x Parent(Father(x), x)
15Interpretations
- An atomic sentence consists of a single relation,
given by a predicate or equality - e.g., Brother(John, Richard)
- The atomic sentence is true if the relation
referred to by the predicate holds the arguments - e.g. Brother , ltJohn, Richardgt,
16Interpretations
- Complex sentences are constructed using
connectives, like in propositional logic - e.g., Person(Richard) ? Brother(Richard, John)
- Brother(Richard,John) ?
Person(Richard)
17Interpretations
- Quantifiers allow us to express properties about
collections of objects - The universal quantifier ? allow us to express
things that are true about all objects - Read as for all
- e.g. ?x Doctor(x) ? Person(x)
- means that if for every object in the domain,
the sentence Doctor(x) ? Person(x) is true, then
the whole sentence with the quantifier is true - Consider letting x be LeftLegOf(Richard), is the
sentence true?
18Interpretations
- The existential quantifier ? lets us state
properties about some object, without needed to
specify precisely what it is - Read as there exists
- e.g. ?x IsGrass(x) ?IsGreen(x)
- The whole sentence is true if there is some
object x in the model such that IsGrass(x) ?
IsGreen(x) is true. - Is this what you would expect? Suppose x is
Richard, is the whole sentence true?
19Nested Quantifiers
- The order of quantification is important
- Everybody loves somebody
- ?x ?y Loves(x,y)
- Somebody is loved by everybody
- ?y ?x Loves(x,y)
20Negation of Quantifiers
- The quantifiers are related to one another by
negation - ??x IsGreen(x) is equivalent to
?x ?IsGreen(x) - ??x Likes(x, SpoiledMilk)
?x ?Likes(x,SpoiledMilk) - Be careful to negate everything inside
- ??x ?y Loves(x, y) ? Loves(y, x)
- ?x ?(?y Loves(x, y) ? Loves(y, x))
- ?x ?y ?(Loves(x, y) ? Loves(y, x))
- ?x ?y ?Loves(x, y) ? ? Loves(y, x)
21Exercises
- Represent the following in FOL
- Every student who takes French passes it.
- The best score in Greek is always higher than the
best score in French. - No person buys an expensive policy.
- Politicians can fool some of the people all of
the time, and they can fool all of the people
some of the time, but they cant fool all of the
people all of the time.
22Inferencing in FOL
- Well take a look at the foundations for how
Prolog works. - Recall that an inference algorithm is used to
derive new sentences that are entailed by our
knowledge base - In PL, we could enumerate all possible worlds
- Cant do this in FOL, as could be infinite
- We can still use inference rules
23Inference Rules in FOL
- Similar to PL
- Important differences
- Quantifiers
- Variables
- First concept is substitution
Sentence
Substitution
24Universal Instantiation
- Infer any sentence obtained from substituting a
ground term (no variables) for the variable
Whats the substitution?
25Existential Instantiation
- More complicated. Infer any sentence substituting
variable v with a constant symbol k not in the
knowledge base - We know something exists, but not what it is.
Skolem Constant
26Lifting
- Generalized Modus Ponens
- pi,pi,q are atomic sentences
- q is a substitution where
- Subst(q, pi)Subst(q, pi)
- for all i
27Example
28Lifting and Unification
- Using substitutions, we can lift propositional
inference rules to FOL - Requires finding substitutions that make
different logical expressions look identical - Unification
- Unify algorithm
unifier
29Unification examples
30Unification
- Variables are standardized
- We want our unifier to return the most general
unifier - The one that places the least restrictions on the
variables - Wed like to check for things like xf(x)
- Occur check
- Makes unification O(n2), n is size of sentences
- Prolog omits this check, making it unsound!
31Reduction to Propositional Inference
- Universal instantiation can be applied several
times - Remove all variables necessary to show sentence
is entailed - When variables removed, the interpretation is
known - Functions cause a potential problem
- FatherOf(FatherOf(FatherOf()))
- But its been shown that only finitely many
applications of a function are needed to show
entailment. - This process is called propositionalization
- We can always answer yes when a sentence is
entailed, but we cant always answer no - Can we be more efficient though?
32Forward Chaining
- Assume that knowledge base represented as
definite clauses (Horn clauses). - Similar to FC in propositional case.
- Standardize apart variables
- Check for renamings
- Return unifier
33Basic Algorithm
34Sample Proof
35Speeding Up Forward Chaining
- Matching against known facts
- Consider 100 students taken Cosc060, 20 taken
Math090, which to find first? - Incremental forward chaining
- Simple algorithm matches already matched
sentences - Irrelevant facts
- What happens if KB contains
36Backward Chaining
- Again, similar to propositional version
- Goal driven
- This is how Prolog works (essentially)
- Uses composition of substitutions
37Algorithm
38Sample Proof
39Resolution
- Again, similar to PL resolution
- Convert sentences to CNF
- Negate query
- Apply resolution inference rule repeatedly until
empty clause is obtained - One change for completeness, resolve subsets of
literals instead of just two literals - Then query is entailed by KB
40Conjunctive Normal Form
- Essentially same as propositional logic
- Eliminate implications
- Move negation inwards
- Standardize variables
- Skolemize
- Drop universal quantifiers
- Distribute ?over ?
41Skolemization
- Process of removing existential quantifiers
- Somebody is loved by everybody
- ?y ?x Loves(x,y)
- Everybody loves somebody
- ?x ?y Loves(x,y)
- ?x Loves(x,Y1)
42Skolemization
- When removing an existential quantifier within
the scope of a universal quantifier, the variable
is replaced with a Skolem function - Use name not already used
- Maps universally quantified variable(s) to some
object - Everybody loves somebody
- ?x ?y Loves(x,y)
- ?x Loves(x,Y1(x))
Some person x loves
43CNF Example
44Resolution Rule
45Sample Proof
46Prolog
- Interpreted language based on logic
- PROgramming in LOGic
- Provides infrastructure for knowledge base
constuction and inference - Most popular language for logic programming
paradigm
47Prolog Programs
- Programs in Prolog consist of a database (our
knowledge base) made up of facts and rules - Facts are simply statements of what is true in
any model of the knowledge base - green(grass).
constant
predicate
48Rules in Prolog
- Rules in Prolog are essentially FOL Horn clauses
(or definite clauses) - Disjunction of literals with exactly one positive
- Alternatively, implication with a conjunction of
positive literals for antecedent (body) and one
positive literal for the consequent (head) - Variables are assumed to be universally
quantified - King(x) ? Greedy(x) ? Evil(x)
- evil(X) - king(X), greedy(X).
49Prolog Database
Every king who is greedy is evil. evil(X) -
king(X), greedy(X). John is a
king. king(john). In RN, Greedy(y) (everything
is greedy) greedy(_).
50Prolog Programs
- After constructing the database, (using your
favorite text editor), you execute the Prolog
interpreter and consult the database.
cstruble_at_studsys - 501gt yap Restoring file
startup YAP version Yap-4.3.20 ?-
consult(king). Some stuff is printed
yes The file loaded successfully.
51Interacting With Prolog
- After consulting the database, the interpreter is
ready to answer queries. - This is indicated with the -? prompt.
- Prolog answers queries with yes or no.
?- king(john). Yes
52How Prolog Answers Queries
- Essentially, depth-first backward chaining
algorithm. Consider - Scan database from top to bottom. Look for
something that matches evil(john). - The rule matches, so try to match the body of the
rule
-? evil(john). Is john evil?
53More About Answering Queries
- Prolog operates under the closed world assumption
- If a match cannot be found in the database, the
result is assumed false. (answers no) - Thus, Prolog has no real notion of unknown.
- Trace the query evil(richard).
54Knowledge Engineering
- Process of constructing a knowledge base
- Steps
- Identify the task
- Assemble relevant knowledge
- Decide on vocabulary
- Encode general knowledge
- Encode specific problem instance knowledge
- Pose queries
- Debug the knowledge base
55Knowledge Engineering Case Study
- Identify the task
- Determine if a student has met the requirements
for graduation. - Assemble relevant knowledge
- Undergraduate Bulletin
- Departmental Web Pages
- Etc.
56Decide on Vocabulary
- Courses
- engl001, cosc159, math080, etc.
- Students
- john, paul, george, ringo, etc.
- Required core class
- core(Class, required)
- Elective core class
- core(Class, elective)
57Decide on Vocabulary
- Course X is a prerequisite for course Y
- prereq(X,Y)
- Student S took course X in term T, and got grade
G - taken(S, X, T, G)
- Student S has passed course X
- passed(S, X)
- Student S can take course X
- can_take(S, X)
58Encode General Knowledge
- What is the general knowledge of this problem?
- Courses, prerequisites, core requirements, etc.
- Information about which courses students have
taken, and their grades is not general knowledge.
59Encode General Knowledge
- Example A course can be taken if all of the
prerequistes have been passed. - ?S,X can_take(S, X) ??Y prereq(Y, X) ? passed(S,
Y)
can_take(S, X) - \(prereq(Y, X), \(passed(S,
Y))).
Negation by failure
60Encode Problem Instance Specific Knowledge
- Encode the classes each student has taken.
John taken(john, cosc051, fall2000,
b). taken(john, engl001, fall2000,
c). taken(john, math080, fall2000, bc).
61Pose Queries
- Now ask the knowledge base some information
- -? can_take(john, math090).
- -? can_graduate(john, cosc).
- -? can_graduate(sally, cosc).
- -? can_graduate(sally, engl).
- Debug the knowledge base
- If queries are giving incorrect answers, trace
through the execution of the inference technique
to find errors in the knowledge base.
62Lists and Abstractions
- Prolog supports lists with the notation.
- prereq(math090, cosc051, math147).
- Can split a list into two parts with the vertical
bar HT
63Passing Classes
- A class can be taken if it has no prerequisites,
or if all have been passed.
No prerequisites in database. can_take(S, X) -
\(prereq(_,X)). can_take(S, X) - prereq(L, X),
passed_all(S,L). If the list is empty, all
passed passed_all(S, ). The head of the list
must be passed, then all of the tail of the
list. passed_all(S, HT) - passed(S,H),
passed_all(S,T).
64Membership
- An element X is a member of a list if its at the
head of the list, or a member of the tail of the
list.
member(X,X_). member(X,_T) - member(X,T).
65Quicksort
- Recall that quicksort works by selecting a pivot,
then splitting the list to a less than (L) have
and greater than or equal to (M) list, then
sorting L, M recursively and concatenating the
results. - How do we do this in Prolog?
66Quicksort
- Task Sort a list
- Vocabulary
- order(A,B) - true if A is in proper order with B
(e.g. A lt B). - split(H,T,L,M) - true if elements of L are less
than H, elements in M are greater than or equal
to H, and the order of elements in L and M are
the same as in HT. - quisort(X,Y) - true if Y is sorted version of X.
67Quicksort
If the head A of the input list tail is less
than H, then split is true if A is at the head
of the L list, and the split of the rest of the
list is true. split(H,AX,AY,Z) -
order(A,H), split(H,X,Y,Z). Similarly if A is
not less than H split(H,AX,Y,AZ) -
not(order(A,H)), split(H,X,Y,Z). Splitting the
empty list should just be empty
lists split(_,,,).
68Quicksort
The empty list is sorted as the empty
list quisort(,). Otherwise, S is the sorted
version of HT if T is split into A, and B
parts, A1 and B1 are sorted versions of A and B
by quisort, and S is the result of appending
HB1 to A1. quisort(HT,S) -
split(H,T,A,B), quisort(A,A1),
quisort(B,B1), append(A1,HB1,S).
69Summary
- First Order Logic provides a richer language for
representing knowledge - Relations between objects
- Functions
- Reason about sets of objects
- Entailment is semidecidable
- We can know when something is entailed, but we
cant always know when something isnt entailed - Inference techniques similar to PL
70Summary
- Process of building a knowledge base is knowledge
engineering - Dealing with variables
- Universal/existential instantiation
- Substitutions
- Unification
- Standardizing apart
- Prolog
- Language based on FOL
- Logic/Declarative programming