58
1 First Order Logic (Syntax, Semantics and Inference)

First Order Logic (Syntax, Semantics and Inference)

  • Upload
    ossie

  • View
    28

  • Download
    0

Embed Size (px)

DESCRIPTION

First Order Logic (Syntax, Semantics and Inference). Knowledge based agents can represent the world it is in and can deduce the actions to take In most programming languages the data and operations are tied closely - PowerPoint PPT Presentation

Citation preview

Page 1: First Order Logic (Syntax, Semantics and Inference)

1

First Order Logic(Syntax, Semantics and Inference)

Page 2: First Order Logic (Syntax, Semantics and Inference)

2

Knowledge based agents can represent the world it is in and can deduce the actions to take

In most programming languages the data and operations are tied closely

Each update to a data structure is done via a domain-specific procedure whose details are derived by the programmer from his own knowledge of the domain

Is there a way to say “pit is in [1,2] or [2,1]” ? Propositional logic could represent the partial

information, with negation and disjunction

Page 3: First Order Logic (Syntax, Semantics and Inference)

3

Propositional Logic is “compositional” Propositional logic is less expressive when the world

consists many objects (why?) But english/.../.. are very much expressive

Why not use engilsh for our representation? Ambiguous & not compositional

Page 4: First Order Logic (Syntax, Semantics and Inference)

4

First-order logic

Propositional logic assumes the world contains facts, (ontological commitment)

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,

Page 5: First Order Logic (Syntax, Semantics and Inference)

5

Ontological Commitments Propositional Logic : facts which hold ot does not. FOL : Objects and relations

Epistemological Commitments (possible states of knowledge w.r.t each facts)

Propositional Logic : true/false FOL : true/false

Page 6: First Order Logic (Syntax, Semantics and Inference)

6

Syntax of FOL: Basic elements

Constants KingJohn, 2, ... Predicates Brother, >,... Functions Sqrt, LeftLegOf,... Variables x, y, a, b,... Connectives , , , , Equality = Quantifiers ,

Page 7: First Order Logic (Syntax, Semantics and Inference)

7

Atomic sentences

Sentence = AtomicSentence | (Sentence Connective Sentence) | Quantifier Variables,…. Sentence | Sentence

Atomic sentence = predicate (term1,...,termn) | term1 = term2

Term = function (term1,...,termn) | constant | variable

E.g., Brother(KingJohn,RichardTheLionheart) > (Length(LeftLegOf(Richard)), Length(LeftLegOf(KingJohn)))

Page 8: First Order Logic (Syntax, Semantics and Inference)

8

Truth in first-order logic

Sentences are true with respect to a model (possible world) 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 true iff the objects referred to by term1,...,termn are in the relation referred to by predicate

Page 9: First Order Logic (Syntax, Semantics and Inference)

9

Universal quantification

<variables> <sentence>

Everyone at England is smart:x At(x, England) 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 PAt(KingJohn,England) Smart(KingJohn)

At(Richard, England) Smart(Richard) At(England, England) Smart(England) ...

Page 10: First Order Logic (Syntax, Semantics and Inference)

10

A common mistake to avoid

Typically, is the main connective with Common mistake: using as the main connective

with :x At(x, England) Smart(x)

means “Everyone is at England and everyone is smart”

Page 11: First Order Logic (Syntax, Semantics and Inference)

11

Existential quantification

<variables> <sentence>

Someone at England is smart:x At(x, England) 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 PAt(KingJohn, England) Smart(KingJohn)

At(Richard, England) Smart(Richard) At(England, England) Smart(England) ...

Page 12: First Order Logic (Syntax, Semantics and Inference)

12

Another common mistake to avoid

Typically, is the main connective with

Common mistake: using as the main connective with :

x At(x,England) Smart(x)

is true if there is anyone who is not at England!

Page 13: First Order Logic (Syntax, Semantics and Inference)

13

Properties of quantifiers

x y is the same as y x x y is the same as y x

Page 14: First Order Logic (Syntax, Semantics and Inference)

14

Properties of quantifiers

x y is the same as y xx y is the same as y x

x y is not the same as y xx 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”

Page 15: First Order Logic (Syntax, Semantics and Inference)

15

Properties of quantifiers

x y is the same as y xx y is the same as y x

x y is not the same as y xx 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 otherx Likes(x,IceCream) x Likes(x,IceCream)x Likes(x,Broccoli) x Likes(x,Broccoli)

Page 16: First Order Logic (Syntax, Semantics and Inference)

16

Interacting with FOL KBs

Suppose a wumpus-world agent is using an FOL KB and perceives a smell and a breeze (but no glitter) at t=5:Tell(KB,Percept([Smell,Breeze,None],5))Ask(KB,a BestAction(a,5))

i.e., does the KB entail some best action at t=5? Answer: Yes, {a/Shoot} ← substitution (binding list) Given a sentence S and a substitution σ, Sσ denotes the result of plugging σ into S; e.g.,

S = Smarter(x,y)σ = {x/Hillary,y/Bill}Sσ = Smarter(Hillary,Bill)

• Ask(KB,S) returns some/all σ such that KB╞ Sσ

Page 17: First Order Logic (Syntax, Semantics and Inference)

17

Universal instantiation (UI)

Every instantiation of a universally quantified sentence is entailed by it:

v αSubst({v/g}, α)

for any variable v and ground term g E.g.,

x King(x) Greedy(x) Evil(x) yields:King(John) Greedy(John) Evil(John)

King(Richard) Greedy(Richard) Evil(Richard)

King(Father(John)) Greedy(Father(John)) Evil(Father(John))

.

. … substitutions are {x/Richard} , {x/John}, {x/ Father(John)} We can infer any sentence by replacing the variable with the ground term

Page 18: First Order Logic (Syntax, Semantics and Inference)

18

Existential instantiation (EI)

For any sentence α, variable v, and constant symbol k that does not appear elsewhere in the knowledge base:

v αSubst({v/k}, α)

E.g., x Crown(x) OnHead(x,John) yields:

Crown(C1) OnHead(C1,John)

provided C1 is a new constant symbol, called a Skolem constant

Page 19: First Order Logic (Syntax, Semantics and Inference)

19

Reduction to propositional inference

Suppose the KB contains just the following:x King(x) Greedy(x) Evil(x)King(John)Greedy(John)Brother(Richard,John)

Instantiating the universal sentence in all possible ways, we have:King(John) Greedy(John) Evil(John)King(Richard) Greedy(Richard) Evil(Richard)King(John)Greedy(John)Brother(Richard,John)

The new KB is propositionalized: proposition symbols are King(John), Greedy(John), Evil(John), King(Richard), etc.

Page 20: First Order Logic (Syntax, Semantics and Inference)

20

Reduction contd.

• Idea: propositionalize KB and query, apply resolution, return result

• Problem: with function symbols, there are infinitely many ground terms,

e.g., Father(Father(Father(John)))

• Theorem: Herbrand (1930). If a sentence α is entailed by an FOL KB, it is entailed by a finite subset of the propositionalized KB

• Idea: For n = 0 to ∞ do Create a propositional KB by instantiating with depth-n terms See if α is entailed by this KB.

Page 21: First Order Logic (Syntax, Semantics and Inference)

21

Reduction contd.

• Problem: Works if α is entailed, loops if α is not entailed

• Theorem: Turing (1936), Church (1936) Entailment for FOL is semidecidable

algorithms exist that say yes to every entailed sentence, but no algorithm exists that also says no to every nonentailed sentence.

Page 22: First Order Logic (Syntax, Semantics and Inference)

22

Problems with Propositionalization

Propositionalization seems to generate lots of irrelevant sentences.

E.g., from:x King(x) Greedy(x) Evil(x)King(John)y Greedy(y)Brother(Richard,John)

Page 23: First Order Logic (Syntax, Semantics and Inference)

23

Forward Chaining

Page 24: First Order Logic (Syntax, Semantics and Inference)

Unification

We can get the inference immediately if we can find a substitution θ such that King(x) and Greedy(x) match King(John) and Greedy(y)θ = {x/John,y/John} worksUnify(α,β) = θ if αθ = βθ

p q θ Knows(John,x) Knows(John,Jane) {x/Jane}Knows(John,x) Knows(y,OJ) {x/OJ, y/John}Knows(John,x) Knows(y,Mother(y)) {y/Jane, x/Mother(y)}Knows(John,x) Knows(x,OJ) {Fail}

Standardizing apart eliminates overlap of variables, e.g., Knows(z17,OJ)

Page 25: First Order Logic (Syntax, Semantics and Inference)

Unification

To unify Knows(John,x) and Knows(y,z),θ = {y/John, x/z } or θ = {y/John, x/John, z/John}

The first unifier is more general than the second, places fewer restriction on the values of the variables

There is a single most general unifier (MGU) that is unique up to renaming of variables.

MGU = { y/John, x/z }

Page 26: First Order Logic (Syntax, Semantics and Inference)

First Order Definite Clauses

Exactly one positive literalAtomic literalImplication whose antecedent is conjunction of positive literal and a single positive literal as consequent

ExamplesKing(x) Λ Greedy(x) Evil(x)King(John)Greedy(y)

Page 27: First Order Logic (Syntax, Semantics and Inference)

Generalized Modus Ponens

p1', p2', … , pn', ( p1 p2 … pn q) qθ

p1' is King(John) p1 is King(x)

p2' is Greedy(y) p2 is Greedy(x) θ is {x/John,y/John} q is Evil(x) q θ is Evil(John)

Lifted version of modus ponens GMP used with KB of definite clausesAll variables assumed universally quantified

Page 28: First Order Logic (Syntax, Semantics and Inference)

An Example KB

Consider the passageThe 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.

The KB should answer the queryIs Col. West a criminal?

Page 29: First Order Logic (Syntax, Semantics and Inference)

An Example KB (contd…)

... it is a crime for an American to sell weapons to hostile nations:American(x) Weapon(y) Sells(x,y,z) Hostile(z) Criminal(x)

Nono … has some missiles, i.e., x Owns(Nono,x) Missile(x):Owns(Nono,M1) and Missile(M1)

… all of its missiles were sold to it by Colonel WestMissile(x) Owns(Nono,x) Sells(West,x,Nono)

Missiles are weapons:Missile(x) Weapon(x)

An enemy of America counts as "hostile“:Enemy(x,America) Hostile(x)

West, who is American …American(West)

The country Nono, an enemy of America …Enemy(Nono,America)

No Function Symbols in the KB>> Datalog KB

Page 30: First Order Logic (Syntax, Semantics and Inference)

Forward Chaining Algorithm

Page 31: First Order Logic (Syntax, Semantics and Inference)

Forward chaining - Example

Page 32: First Order Logic (Syntax, Semantics and Inference)

Forward chaining - Example

Page 33: First Order Logic (Syntax, Semantics and Inference)

Forward chaining - Example

Page 34: First Order Logic (Syntax, Semantics and Inference)

Forward Chaining Algorithm - discussion

No new sentences can be added to the KB after it has generated criminal(west)Fixed point of inference process

SoundComplete (?)Datalog KB

• k- maximum arity of predicates, n – constant symbols, p- predicates pnk distinct ground facts

KB with function symbols• Use Herbrand’s theorem, if the query has answer.

Remember, entailment with FOL is semidecidable

Page 35: First Order Logic (Syntax, Semantics and Inference)

Forward Chaining Algorithm - discussion

Efficiency concernsNotice the inner-loop generate all possible θ

• Expensive pattern matching

Algorithm rechecks every rule in every iteration to see if the premises are satisfiedGenerates many facts which are irrelevant to the goal

Page 36: First Order Logic (Syntax, Semantics and Inference)

Addressing Efficiency ConcernsMatching against known rules only

To apply the rule, Missile(x) Weapon (x)Look for the rules that unify only with Missile(x)Use Indexed KB

Missile(x) Λ Owns(Nono,x) Sells(West, x, Nono)Owns(Nono,x)Nono may own thousands of objectsFind all missiles first, then see if these missiles are owned by Nono : conjunct orderingRemember heuristic : Most constrained variable

Page 37: First Order Logic (Syntax, Semantics and Inference)

Addressing Efficiency ConcernsMatching against known rules only

Colorable() is inferred iff the CSP has a solutionCSPs include 3SAT as a special case, hence matching is NP-hardForward chaining has an NP Hard Matching Problem in its inner loop

Diff(wa,nt) Diff(wa,sa) Diff(nt,q) Diff(nt,sa) Diff(q,nsw) Diff(q,sa) Diff(nsw,v) Diff(nsw,sa) Diff(v,sa) Colorable()

Diff(Red,Blue) Diff (Red,Green) Diff(Green,Red) Diff(Green,Blue) Diff(Blue,Red) Diff(Blue,Green)

Page 38: First Order Logic (Syntax, Semantics and Inference)

Addressing Efficiency ConcernsMatching against known rules only

> Most Rules in real world are small and simple, upper bounds on the rule size and arity.> Data complexity> Consider only subclasses of databases for which matching is efficient datalog KBUse better algorithm that avoids redundant matchings

Page 39: First Order Logic (Syntax, Semantics and Inference)

Addressing Efficiency ConcernsIncremental Forward Chaining

Every fact inferred at iteration t will use at least one fact derived at iteration t-1At iteration t, consider only rules whose premise include conjuncts which unifies with a fact derived at iteration t-1With indexing, we may find out all rules which may trigger at iteration t

Rete Algorithm

Page 40: First Order Logic (Syntax, Semantics and Inference)

40

Backward Chaining

Page 41: First Order Logic (Syntax, Semantics and Inference)

Backward chaining algorithm

SUBST(COMPOSE(θ1, θ2), p) = SUBST(θ2, SUBST(θ1, p))

Page 42: First Order Logic (Syntax, Semantics and Inference)

Backward chaining example

Page 43: First Order Logic (Syntax, Semantics and Inference)

Backward chaining example

Page 44: First Order Logic (Syntax, Semantics and Inference)

Backward chaining example

Page 45: First Order Logic (Syntax, Semantics and Inference)

Backward chaining example

Page 46: First Order Logic (Syntax, Semantics and Inference)

Backward chaining example

Page 47: First Order Logic (Syntax, Semantics and Inference)

Backward chaining example

Page 48: First Order Logic (Syntax, Semantics and Inference)

Backward chaining example

Page 49: First Order Logic (Syntax, Semantics and Inference)

Backward chaining example

Page 50: First Order Logic (Syntax, Semantics and Inference)

Properties of backward chaining

Depth-first recursive proof search: space is linear in size of proofIncomplete due to infinite loops fix by checking current goal against every goal on stack

Inefficient due to repeated subgoals (both success and failure) fix using caching of previous results (extra space)

Widely used for logic programming

Page 51: First Order Logic (Syntax, Semantics and Inference)

Resolution

Page 52: First Order Logic (Syntax, Semantics and Inference)

Converting sentences to CNF

1. Eliminate all ↔ connectives (P ↔ Q) ((P Q) ^ (Q P))

2. Eliminate all connectives (P Q) (P Q)

3. Reduce the scope of each negation symbol to a single predicate P P

(P Q) P Q

(P Q) P Q

(x)P (x)P

(x)P (x)P

4. Standardize variables: rename all variables so that each quantifier has its own unique variable name

Page 53: First Order Logic (Syntax, Semantics and Inference)

Converting sentences to clausal form

5. Eliminate existential quantification by introducing Skolem constants/functions(x)P(x) P(c)

c is a Skolem constant (a brand-new constant symbol that is not used in any other sentence)(x)(y)P(x,y) (x)P(x, f(x))since is within the scope of a universally quantified variable, use a Skolem function f to construct a new value that depends on the universally quantified variable

f must be a brand-new function name not occurring in any other sentence in the KB.

E.g., (x)(y)loves(x,y) (x)loves(x,f(x)) In this case, f(x) specifies the person that x loves

Page 54: First Order Logic (Syntax, Semantics and Inference)

Converting sentences to clausal form

6. Remove universal quantifiers by (1) moving them all to the left end; (2) making the scope of each the entire sentence; and (3) dropping the “prefix” partEx: (x)P(x) P(x)

7. Put into conjunctive normal form (conjunction of disjunctions) using distributive and associative laws(P Q) R (P R) (Q R)(P Q) R (P Q R)

8. Split conjuncts into separate clauses9. Standardize variables so each clause contains only variable names

that do not occur in any other clause

Page 55: First Order Logic (Syntax, Semantics and Inference)

An example

(x)(P(x) ((y)(P(y) P(f(x,y))) (y)(Q(x,y) P(y)))) 2. Eliminate

(x)(P(x) ((y)(P(y) P(f(x,y))) (y)(Q(x,y) P(y))))

3. Reduce scope of negation(x)(P(x) ((y)(P(y) P(f(x,y))) (y)(Q(x,y) P(y))))

4. Standardize variables(x)(P(x) ((y)(P(y) P(f(x,y))) (z)(Q(x,z) P(z))))

5. Eliminate existential quantification(x)(P(x) ((y)(P(y) P(f(x,y))) (Q(x,g(x)) P(g(x)))))

6. Drop universal quantification symbols(P(x) ((P(y) P(f(x,y))) (Q(x,g(x)) P(g(x)))))

Page 56: First Order Logic (Syntax, Semantics and Inference)

Example

7. Convert to conjunction of disjunctions(P(x) P(y) P(f(x,y))) (P(x) Q(x,g(x))) (P(x) P(g(x)))

8. Create separate clausesP(x) P(y) P(f(x,y))

P(x) Q(x,g(x))

P(x) P(g(x))

9. Standardize variablesP(x) P(y) P(f(x,y))

P(z) Q(z,g(z))

P(w) P(g(w))

Page 57: First Order Logic (Syntax, Semantics and Inference)

Resolution: brief summary

Full first-order version:l1 ··· lk, m1 ··· mn

(l1 ··· li-1 li+1 ··· lk m1 ··· mj-1 mj+1 ··· mn)θwhere Unify(li, mj) = θ.

The two clauses are assumed to be standardized apart so that they share no variables.For example,

Rich(x) Unhappy(x) , Rich(Ken)Unhappy(Ken)

with θ = {x/Ken}

Apply resolution steps to CNF(KB α); complete for FOL

Page 58: First Order Logic (Syntax, Semantics and Inference)

Resolution proof: definite clauses