First-order Logic [RN2] Sec 7.1-7.6 Chap 8-9 [RN3] Sec 7.1-7.6 Chap - - PDF document

first order logic rn2 sec 7 1 7 6 chap 8 9 rn3 sec 7 1 7
SMART_READER_LITE
LIVE PREVIEW

First-order Logic [RN2] Sec 7.1-7.6 Chap 8-9 [RN3] Sec 7.1-7.6 Chap - - PDF document

First-order Logic [RN2] Sec 7.1-7.6 Chap 8-9 [RN3] Sec 7.1-7.6 Chap 8-9 CS 486/686 University of Waterloo Lecture 20: November 15, 2012 1 CS486/686 Lecture Slides (c) 2012 P. Poupart Outline First-order logic Syntax and semantics


slide-1
SLIDE 1

1

CS486/686 Lecture Slides (c) 2012 P. Poupart

1

First-order Logic [RN2] Sec 7.1-7.6 Chap 8-9 [RN3] Sec 7.1-7.6 Chap 8-9

CS 486/686 University of Waterloo Lecture 20: November 15, 2012

CS486/686 Lecture Slides (c) 2012 P. Poupart

2

Outline

  • First-order logic

– Syntax and semantics

  • Inference

– Propositionalization with ground inference – Lifted resolution

slide-2
SLIDE 2

2

CS486/686 Lecture Slides (c) 2012 P. Poupart

3

Logic

  • General language

– Can encode any kind of deterministic and discrete knowledge – Well defined syntax and semantics – Knowledge base: store domain knowledge

  • General algorithms

– Search by inference – Can infirm or confirm conclusions

CS486/686 Lecture Slides (c) 2012 P. Poupart

4

Syntax of a very simple logic

  • Sentence  AtomicSentence | ComplexSentence
  • AtomicSentence  True | False | Symbol
  • Symbol  P | Q | R | …
  • ComplexSentence  Sentence

| (Sentence  Sentence ) | (Sentence  Sentence) | (Sentence  Sentence ) | (Sentence  Sentence) Symbols are binary variables

slide-3
SLIDE 3

3

CS486/686 Lecture Slides (c) 2012 P. Poupart

5

Logical connector semantics

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

CS486/686 Lecture Slides (c) 2012 P. Poupart

6

Recall the Street Puzzle

1 2 3 4 5

Ni = {English, Spaniard, Japanese, Italian, Norwegian} Ci = {Red, Green, White, Yellow, Blue} Di = {Tea, Coffee, Milk, Fruit-juice, Water} Ji = {Painter, Sculptor, Diplomat, Violinist, Doctor} Ai = {Dog, Snails, Fox, Horse, Zebra} The Englishman lives in the Red house The Spaniard has a Dog The Japanese is a Painter The Italian drinks Tea The Norwegian lives in the first house on the left The owner of the Green house drinks Coffee The Green house is on the right of the White house The Sculptor breeds Snails The Diplomat lives in the Yellow house The owner of the middle house drinks Milk The Norwegian lives next door to the Blue house The Violinist drinks Fruit juice The Fox is in the house next to the Doctor’s The Horse is next to the Diplomat’s

Who owns the Zebra? Who drinks Water?

slide-4
SLIDE 4

4

CS486/686 Lecture Slides (c) 2012 P. Poupart

7

Example – Street Puzzle

  • Symbols: Vin, Vic, Vid, Vij, Via

i  {1,2,3,4,5} n  {English, Spaniard, Japanese, Italian, Norwegian} c  {Red, Green, White, Yellow, Blue} d  {Tea, Coffee, Milk, Fruit-juice, Water} j  {Painter, Sculptor, Diplomat, Violinist, Doctor} a  {Dog, Snails, Fox, Horse, Zebra}

  • Example:

V2red = true: the 2nd house is red V2red = false: the 2nd house is not red

CS486/686 Lecture Slides (c) 2012 P. Poupart

8

Example – Street Puzzle

  • Constraint: the Spaniard has a dog
  • Constraint: the green house is on the

immediate left of the red house (V1spaniard  V1dog)  (V2spaniard  V2dog)  (V3spaniard  V3dog)  (V4spaniard  V4dog)  (V5spaniard  V5dog)

  • V1red  (V1green  V2red)  (V2green  V3red) 

(V3green  V4red)  (V4green  V5red)  V5green

slide-5
SLIDE 5

5

CS486/686 Lecture Slides (c) 2012 P. Poupart

9

Example: 4-Queens problem

  • Symbols: Qij i,j{1,2,3,4}
  • Qij = true: queen at

location (i,j)

  • Qij = false: no queen at

location (i,j)

1 2 3 4 1 2 3 4

CS486/686 Lecture Slides (c) 2012 P. Poupart

10

Example: 4-Queens problem

  • At least one queen in row 1:
  • At most one queen in row 1:

1 2 3 4 1 2 3 4

Q11  Q12  Q13  Q14 Q11  Q12  Q13  Q14 Q12  Q11  Q13  Q14 Q13  Q11  Q12  Q14 Q14  Q11  Q12  Q13

slide-6
SLIDE 6

6

CS486/686 Lecture Slides (c) 2012 P. Poupart

11

Knowledge Base (KB)

  • Knowledge base

– Encode problem description and any knowledge that could help solve the problem – Database of facts and constraints – Possible language: propositional logic

  • Entailment

– What can we derive/conclude from KB? – KB |= :  follows from KB

CS486/686 Lecture Slides (c) 2012 P. Poupart

12

Semantics

  • Entailment: KB |= 

–  is true in all the models in which KB is true

models KB

models KB

models KB

 KB |=  KB |  KB | 

slide-7
SLIDE 7

7

CS486/686 Lecture Slides (c) 2012 P. Poupart

13

Inference

  • Process of verifying the truth of a

formula

  • Simple inference algorithm:

– Build a truth table that enumerates all models – Verify that formula is true in all models where KB is true

CS486/686 Lecture Slides (c) 2012 P. Poupart

14

Truth table

P Q R KB true true true false true true false false true false true false true false false false false true true false false true false false false false true true false false false true KB: P  Q, Q  R,

  • Q

KB |= P? KB |= P? KB |= Q? KB |= Q? KB |= R? KB |= R? no yes no yes no no

slide-8
SLIDE 8

8

CS486/686 Lecture Slides (c) 2012 P. Poupart

15

Efficiency

  • Building a truth table is inefficient:

exponential in the number of variables

  • Alternatives: backtracking, resolution

– Rewrite “KB |= ?” as KB’ = (KB and ) – Show that KB’ is not satisfiable (e.g., there doesn’t exist any model for which KB is true, but  is false)

CS486/686 Lecture Slides (c) 2012 P. Poupart

16

Convenience

  • KB may contain any formula that follows the

syntax of our simple logic.

– Inconvenient: too many possible formula – Can we simplify syntax?

  • Conjunctive normal form (CNF)

– Only use , ,  – Conjunction of clauses, where each clause is a disjunction of (possibly negated) literals – Example: (V1  V2  V3)  (V1  V2)  (V3  V2)

slide-9
SLIDE 9

9

CS486/686 Lecture Slides (c) 2012 P. Poupart

17

Logical equivalence

  • How do we transform a KB in CNF?
  • Logical equivalence rules

– (A)  A – (A  B)  A  B – (A  B)  (A  B)  (B  A) – (A  B)  (A  B) – (A  B)  (A  B) – (A  (B  C))  ((A  B)  (A  C))

  • Can every KB be transformed in CNF in polynomial

time and space?

– No: last rule may yield exponentially many clauses

CS486/686 Lecture Slides (c) 2012 P. Poupart

18

Resolution

  • Idea: generate new sentences by implications

– Unit resolution: A  (A  B) |= B – General resolution: (A  B)  (B  C) |= (A  C)

  • How do we do this with KB in CNF?

– Unit resolution: A  (A  B) |= B – General resolution: (A  B)  (B  C) |= (A  C)

  • Algorithm: apply resolution to every possible pair of

clauses until

– Empty clause is generated: false KB – No more clauses can be generated: all generated clauses are entailed

slide-10
SLIDE 10

10

CS486/686 Lecture Slides (c) 2012 P. Poupart

19

Concise Language

  • Propositional logic provides a general language

to encode deterministic domain knowledge.

  • While the encoding is precise and well defined

it is often tedious or complicated.

  • Simple English sentences often lead to long

logical formula.

  • Can we make propositional logic more concise

and more natural?

CS486/686 Lecture Slides (c) 2012 P. Poupart

20

Recall: 4-Queens problem

  • At least one queen in row 1:
  • At most one queen in row 1:

1 2 3 4 1 2 3 4

Q11  Q12  Q13  Q14 Q11  Q12  Q13  Q14 Q12  Q11  Q13  Q14 Q13  Q11  Q12  Q14 Q14  Q11  Q12  Q13

Repeat for every row, column and diagonal. This is too tedious! Can we be more concise?

slide-11
SLIDE 11

11

CS486/686 Lecture Slides (c) 2012 P. Poupart

21

First-order Logic

  • World

– consist of objects and relations – Has many objects

  • First-order logic:

– Natural: use predicates to encode relations and constants to encode objects – Concise: use quantifiers (e.g., , ) to talk about many objects simultaneously

CS486/686 Lecture Slides (c) 2012 P. Poupart

22

First-order Logic Syntax

  • Sentence  Predicate(Term, …)

| Term = Term | (Sentence Connective Sentence) | Quantifier Variable, … Sentence | Sentence

  • Term  Function(Term, …) | Constant | Variable
  • Connective   |  |  | 
  • Quantifier   | 
  • Constant  A | X1 | John | …
  • Variable  a | x | s | …
  • Predicate  Before | HasColor | Raining | …
  • Function  Mother | LeftLeg | …
slide-12
SLIDE 12

12

CS486/686 Lecture Slides (c) 2012 P. Poupart

23

Example: kinship domain

  • Elizabeth is the mother of Charles

Mother(Elizabeth,Charles)

  • Charles is the father of William

Father(Charles,William)

  • One’s grandmother is the mother of
  • ne’s parent

x,z y Grandmother(x,z)  Mother(x,y)  Parent(y,z)

CS486/686 Lecture Slides (c) 2012 P. Poupart

24

Symbols

  • Constant symbols: objects

– E.g. William, Elizabeth, Charles

  • Predicate symbols: relationships

– Binary: Mother( , ), Grandmother( , ) – Unary: Female( ) – Predicates have a truth value

  • Function symbols: functions

– Denote an object – E.g.: MotherOf(William) = Elizabeth

slide-13
SLIDE 13

13

CS486/686 Lecture Slides (c) 2012 P. Poupart

25

Quantifiers

  • Universal quantifier: 

– For all – x P(x)  P(const1)  P(const2)  …

  • Existential quantifier: 

– There exists – x P(x)  P(const1)  P(const2)  …

CS486/686 Lecture Slides (c) 2012 P. Poupart

26

Nested Quantifiers

  • Order of identical quantifiers doesn’t

matter

  • Brothers are siblings

– x y Brother(x,y)  siblings(x,y) – y x Brother(x,y)  siblings(x,y)

  • Similarly for existential quantifiers

– x y P(x,y)  y x P(x,y)

slide-14
SLIDE 14

14

CS486/686 Lecture Slides (c) 2012 P. Poupart

27

Nested Quantifiers

  • Order of different quantifiers matters
  • x y Loves(x,y)

– Everyone loves someone – Conjunction of disjunctions (CNF)

  • x y Loves(x,y)

– There is a person that loves everyone – Disjunction of conjunction (DNF)

CS486/686 Lecture Slides (c) 2012 P. Poupart

28

Connections between  and 

  • We only need one of the quantifiers
  • De Morgan’s rule

– x P  x P

  • P  Q  (P  Q)

– x P  x P

  • (P  Q)  P  Q

– x P  x P P  Q  (P  Q) – x P  x P P  Q  (P  Q)

slide-15
SLIDE 15

15

CS486/686 Lecture Slides (c) 2012 P. Poupart

29

Equality

  • Special relation
  • We could define an “Equality” predicate

– x = y  Equality(x,y)

  • True: when x and y are the same
  • False: otherwise

CS486/686 Lecture Slides (c) 2012 P. Poupart

30

Practice: 4-Queens problem

  • 4-queens problem in first-order logic
  • Predicates:

– Queen( , )

  • Constants:

– 1, 2, 3, 4 (column and row numbers)

  • Column constraints:

– i,j1,j2 Queen(i,j1)  j1  j2  Queen(i,j2) – i j Queen(i,j)

slide-16
SLIDE 16

16

CS486/686 Lecture Slides (c) 2012 P. Poupart

31

Practice: street puzzle

  • Predicates:

– House(), Person( ), Color( ), Drink( ), Job( ), Animal( ) – Attr( , ) (attribute of)

  • Constants:

– 1, 2, 3, 4, 5 (house number) – English, Spaniard, Japanese, Italian, Norwegian – Red, Green, White, Yellow, Blue – Tea, Coffee, Milk, Juice, Water – Painter, Sculptor, Diplomat, Violinist, Doctor – Dog, Snails, Fox, Horse, Zebra

  • Function:

– Left(x): number of the house to the immediate left of x

CS486/686 Lecture Slides (c) 2012 P. Poupart

32

Practice: street puzzle

  • The Spaniard has a dog

– x House(x)  (Attr(x,Spaniard)  Attr(x,Dog))

  • the green house is on the immediate left of

the red house

– Attr(1,Red)  – Attr(5,Green)  – x House(x)  x1  (Attr(x,Red)  Attr(Left(x),Green))

slide-17
SLIDE 17

17

CS486/686 Lecture Slides (c) 2012 P. Poupart

33

Propositional vs first-order logic

  • Propositional logic:

– variables

  • First-order logic:

– Quantifiers, predicates, constants, functions

  • Syntactically different!
  • Are they equally expressive?

– Prop logic  1st-order logic: – 1st-order logic  prop logic: yes yes (finite domains) no (infinite domains)

CS486/686 Lecture Slides (c) 2012 P. Poupart

34

Propositional  first-order logic

  • Variables  predicates
  • Indices  constants
  • 4-queens problem:

– Q11  Q12  Q13  Q14 – Q(1,1)  Q(1,2)  Q(1,3)  Q(1,4)

slide-18
SLIDE 18

18

CS486/686 Lecture Slides (c) 2012 P. Poupart

35

First-order  propositional logic

  • Quantifiers

–   conjunction –   disjunction

  • Predicates  variables
  • Constants  indices
  • Functions of constants  functions of

indices

CS486/686 Lecture Slides (c) 2012 P. Poupart

36

First-order  propositional logic

  • x House(x)  x1 

(Attr(x,Red)  Attr(Left(x),Green))

  • x Housex  x1  (Attrx,RedAttrLeft(x),Green)
  • (House1  11  (Attr1,Red  AttrLeft(1),Green))

(House2  21  (Attr2,Red  AttrLeft(2),Green)) (House3  31  (Attr3,Red  AttrLeft(3),Green)) (House4  41  (Attr4,Red  AttrLeft(4),Green)) (House5  51  (Attr5,Red  AttrLeft(5),Green))

slide-19
SLIDE 19

19

CS486/686 Lecture Slides (c) 2012 P. Poupart

37

Inference

  • Propositionalize KB

– Run favorite ground inference algorithm (e.g., backtracking (DPLL), resolution) – Efficient?

  • No: propositionalizing may yield an exponentially large

formula

  • x y z P(x,y,z)  conjunction of disjunctions of

conjunctions

  • O(nm) where n is # of constants and m is # of quantifiers
  • Alternative: lifted inference

– Work directly with first-order formula

CS486/686 Lecture Slides (c) 2012 P. Poupart

38

Lifted Inference

  • First-order logic:

– Quantifiers allow us to characterize several

  • bjects simultaneously
  • Lifted inference:

– Do inference by reasoning about several terms simultaneously – Ground terms only when necessary – Hope: determine truth value of goal formula without grounding all terms

slide-20
SLIDE 20

20

CS486/686 Lecture Slides (c) 2012 P. Poupart

39

Lifted Resolution

Two phases 1. Reduce to lifted conjunctive normal form 2. Resolution with unification

CS486/686 Lecture Slides (c) 2012 P. Poupart

40

Reduction to Lifted CNF

Six steps 1. Eliminate implications 2. Move negations inwards 3. Standardize variables 4. Skolemize 5. Drop universal quantifiers 6. Distribute  over 

slide-21
SLIDE 21

21

CS486/686 Lecture Slides (c) 2012 P. Poupart

41

Reduction to Lifted CNF

Example: x [y Animal(y)  Loves(x,y)] [y Loves(y,x)] 1. Eliminate implications x [y Animal(y)  Loves(x,y)]  [y Loves(y,x)] 2. Move negations inwards (apply DeMorgan’s rules) x [y (Animal(y)  Loves(x,y))]  [y Loves(y,x)] x [y Animal(y)  Loves(x,y)]  [y Loves(y,x)] x [y Animal(y)  Loves(x,y)]  [y Loves(y,x)]

CS486/686 Lecture Slides (c) 2012 P. Poupart

42

Reduction to Lifted CNF

x [y Animal(y)  Loves(x,y)] [y Loves(y,x)] 3. Standardize variables (use different names) x [y Animal(y)  Loves(x,y)]  [z Loves(z,x)] 4. Skolemize (replace existential variables by new constants or functions) x [Animal(F(x))  Loves(x,F(x))]  [Loves(G(x),x)]

slide-22
SLIDE 22

22

CS486/686 Lecture Slides (c) 2012 P. Poupart

43

Skolemization

  • x P(x)

– There is at least one term x that satisfies P(x) – We don’t care what that term is

  • Idea: create a new constant

– Let A be a new constant which could correspond to any object, but since we don’t care what that

  • bject is, just denote it by this new constant.

– x P(x)  P(A)

CS486/686 Lecture Slides (c) 2012 P. Poupart

44

Skolemization

  • x y P(x,y)

– For each x, there is at least one term y that satisfies P(x,y) – We don’t care what that term is but it may be different for different x

  • Idea: create a new function

– Let f be a new function that denotes the satisfying term for each x – x y P(x,y)  P(x,f(x))

slide-23
SLIDE 23

23

CS486/686 Lecture Slides (c) 2012 P. Poupart

45

Reduction to Lifted CNF

x [Animal(F(x))  Loves(x,F(x))]  [Loves(G(x),x)] 5. Drop universal quantifiers (since all remaining variables are universally quantified) [Animal(F(x))  Loves(x,F(x))]  [Loves(G(x),x)] 6. Distribute  over  (to get a CNF) [Animal(F(x))  Loves(G(x),x)]  [Loves(x,F(x))  Loves(G(x),x)]

CS486/686 Lecture Slides (c) 2012 P. Poupart

46

Resolution with Unification

  • Recall

– Unit resolution:

  • A  (A  B) |= B
  • A  (A  B) |= B

– General resolution:

  • (A  B)  (B  C) |= (A  C)
  • (A  B)  (B  C) |= (A  C)
  • Lifted resolution: terms may not match

exactly because of variables

slide-24
SLIDE 24

24

CS486/686 Lecture Slides (c) 2012 P. Poupart

47

Unification

  • Variables are like “wild cards” that can be

anything

  • Find unifier:

– Unify Knows(John,x) and Knows(John,Jane)  Knows(John,Jane) {x/Jane} – Unify Knows(John,x) and Knows(y,Bill)  Knows(John,Bill) {x/Bill, y/John} – Unify Knows(John,x) and Knows(y,Mother(y))  Knows(John,Mother(John)) {x/Mother(John), y/John}

CS486/686 Lecture Slides (c) 2012 P. Poupart

48

Most General Unifier

  • Find unifier that is as general as possible (i.e.,

preserves as many variables as possible)

  • Find most general unifier:

– Unify Knows(John,x) and Knows(y,Mother(z))  Knows(John,Mother(z)) {y/John, x/Mother(z)}

slide-25
SLIDE 25

25

CS486/686 Lecture Slides (c) 2012 P. Poupart

49

Resolution with Unification

  • Find clauses with positive and negative terms that can

be unified

  • Eliminate unifying terms and perform substitutions in

remaining terms according to most general unifier

  • Unit resolution:

– A(f(x),x))  [A(y,g(z))  B(y,z)] |= B(f(g(z)),z) – {x/g(z),y/f(g(z))}

  • General resolution:

– [A(x,y)  B(y,f(z))]  [B(u,v)  C(v,w)] |= [A(x,y)  C(f(z),w)] – {u/y,v/f(z)}

CS486/686 Lecture Slides (c) 2012 P. Poupart

50

Next class

  • Markov Logic Networks