Conversion to CNF

advertisement
Conjunctive Normal Form
& Horn Clauses
York University CSE 3401
Vida Movahedi
York University- CSE 3401- V. Movahedi
02-CNF & Horn
1
Overview
• Definition of literals, clauses, and CNF
• Conversion to CNF- Propositional logic
• Representation of clauses in logic programming
• Horn clauses and Programs
– Facts
– Rules
– Queries (goals)
• Conversion to CNF- Predicate logic
[ref.: Clocksin- Chap. 10 and Nilsson- Chap. 2]
York University- CSE 3401
02-CNF & Horn
2
Conjunctive Normal Form
• A literal is either an atomic formula (called a positive
literal) or a negated atomic formula (called a negated
literal)
– e.g. p, ¬q
• A clause is
–
–
–
–
A literal, or
Disjunction of two or more literals, or
e.g. p, p   q  r
A special clause: The empty clause, shown as □, :- or {}
• A formula  is said to be in Conjunctive Normal Form
(CNF) if it is the conjunction of some number of clauses
York University- CSE 3401
02-CNF & Horn
3
CNF (example)
( p  q )  (q   s  r )  ( r  t )
˄
˅
p
York University- CSE 3401
˅
q
q
¬s
02-CNF & Horn
˅
r
¬r
t
4
CNF- Facts
• For every formula  of propositional logic, there
exists a formula A in CNF such that A is a tautology
• A polynomial algorithm exists for converting  to A
• For practical purposes, we use CNFs in Logic
Programming
York University- CSE 3401
02-CNF & Horn
5
Conversion to CNF
1. Remove implication and equivalence
– Use
( p  q)
 ( p  q )
( p  q)
 ( p  q )  (q  p )
 ( p  q )  ( q  p )
2. Move negations inwards
–
Use De Morgan’s
 ( p  q )  ( p   q )
 ( p  q )  ( p   q )
3. Distribute OR over AND
p  (q  r )
York University- CSE 3401
 ( p  q)  ( p  r)
02-CNF & Horn
6
Conversion to CNF- example
• Example:
Convert the following formula to CNF
p  (r  s)

( p  ( r  s ))
 (( r  s )  p )

(  p  ( r  s ))


( p  r )  ( p  s )  ( r   s  p )
York University- CSE 3401
( (r  s )  p )
02-CNF & Horn
7
Representing a clause
• Consider this clause:
p  q  r  s
 ( p  r )  q  s
 ( p  r )  (q  s)
• In Logic programming, it is shown as:
(q  s)  ( p  r )
q; s :  p, r.
• Easy way: positive literals on the left, negative literals
on the right
York University- CSE 3401
02-CNF & Horn
8
Logic Programming Notation
• A clause in the form:
p1 ; p 2 ;...; p m :  q1 , q 2 ,..., q n .
is equivalent to:
p1  p 2  ...  p m   q1   q 2  ...   q n
or
if
q1  q 2  ...  q n  p1  p 2  ...  p m
q1  q 2  ...  q n
p1 , p 2 ,..., p m
York University- CSE 3401
is true, then at least one of
is true.
02-CNF & Horn
9
Logic Programming Notation- cont.
• A formula in CNF is written as conjunction (or a set
of) clauses.
• Example:
( p  r )  ( p  s )  ( r   s  p )
 r :  p.

  s :  p.
 p :  r , s.

York University- CSE 3401
02-CNF & Horn
10
Example- summary
• Example:
Write the following formula in logic programming notation
p  (r  s)
( p  r )  ( p  s )  ( r   s  p )
convert to
CNF :
convert to
logic programmin
g notation
:
 r :  p.

 s :  p.
 p :  r , s.

York University- CSE 3401
02-CNF & Horn
11
Another Example
Write the following expression as Logic Programming Clauses:
 p  ( s 
1- Conversion to CNF:
2- Symmetry of ˄
allows for set notation
of a CNF
3- Symmetry of ˅
allows for set notation
of clauses
4- Logic Prog. notation
York University- CSE 3401
r )  q   (r  t )

 p  (  s  r )   q   (  r  t )
 ( p  q )  ( s  r  q )  ( r  t )
( p  q ), (  s  r  q ), (  r  t )
  p , q  , q ,  s , r  ,  r , t  
p; q : .
02-CNF & Horn
q ; r :  s.
t :  r.
12
Horn Clause
• A Horn clause is a clause with at most one positive
literal:
– Rules “head:- body.”
– Facts “head :-.”
– Queries (or goals) “:-body.”
e.g. p1:-q1, q2, ..., qn.
e.g. p2:-.
e.g. :- r1, r2, ..., rm.
• Horn clauses simplify the implementation of logic
programming languages and are therefore used in Prolog.
York University- CSE 3401
02-CNF & Horn
13
A Program
• A logic programming program P is defined as a finite
set of rules and facts.
– For example, P={p:-q,r., q:-., r:-a., a:-.}
rule1 fact1 rule2 fact2
• Rules and facts (with exactly one positive literal) are
called definite clauses and therefore a program defined
by them is called a definite program.
York University- CSE 3401
02-CNF & Horn
14
Query
• A computational query (or goal) is the conjunction of some
positive literals (called subgoals) , e.g. r  r  ...  r
1
2
n
• A query is deductible from P if it can be proven on the basis
of P: P |  r1  r2  ...  rn
• Note this query is written as :  r1 , r2 ,..., rn .
which is  r1   r2  ...   rn or  ( r1  r2  ...  rn )
• Why? “Proof by contradiction” is used to answer queries:
P |  r1  r2  ...  rn
York University- CSE 3401
iff
P   ( r1  r2  ...  rn )
02-CNF & Horn
is inconsistent
15
Example
• P: { p:-q. , q:-.}
• If we want to know about p, we will ask the query:
:-p.
• Note that the set { p:-q., q:-., :-p.} is inconsistent.
(Reminder: truth table for above clauses does not
have even one row where all the clauses are true)
• Therefore p is provable and your theorem proving
program (e.g. Prolog) will return true.
York University- CSE 3401
02-CNF & Horn
16
‘Predicate Logic’ Clauses
• Same definition for literals, clauses, and CNF except
now each literal is more complicated since an atomic
formula is more complicated in predicate logic
• We need to deal with quantifiers and their object
variables when converting to CNF
York University- CSE 3401
02-CNF & Horn
17
Conversion to CNF in Predicate Logic
1. Remove implication and equivalence
2. Move negations inwards
3. Rename variables so that variables of each
quantifier are unique
4. Move all quantifiers to the front (conversion to
Prenex Normal Form or PNF)
5. Skolemize (get rid of existential quantifiers)
6. Distribute OR over AND
7. Remove all universal quantifiers
York University- CSE 3401
02-CNF & Horn
18
Example
Example:
Convert the following formula to CNF:
Step 1. Remove implication and
equivalence
(  X ) (  Y ) m ( X , Y )  n ( X ) 
(  X )  (  Y ) m ( X , Y )  n ( X ) 
Step 2. Move negations inwards
Note
 (  x ) p ( x )  ( x )  p ( x )
(  X ) (  Y )  m ( X , Y )  n ( X ) 
Step 3. Rename variables so that
variables of each quantifier are
unique
Step 4. Move all quantifiers to the
(  X )(  Y )  m ( X , Y )  n ( X ) 
front (PNF)
York University- CSE 3401
02-CNF & Horn
19
Example- cont.
Step 5. Skolemizing (get rid of existential quantifiers)
(  X )(  Y )  m ( X , Y )  n ( X ) 
Step 6. Distribute OR over AND to have conjunctions of
disjunctions as the body of the formula
Step 7. Remove all universal quantifiers
m ( X ,Y )  n( X )
Logic Programming notation:
York University- CSE 3401
n ( X ) :  m ( X , Y ).
02-CNF & Horn
20
Skolems
•
Skolems are used to get rid of existential
quantifiers:
– Skolem constants:
When NOT in scope of another quantifier
((  X ) female ( X )  mother ( eve , X ))
 female ( g 1)  mother ( eve , g 1)
– Skolem functions:
When in scope of another quantifier
(  X )(  Y )  human ( X )  mother (Y , X )
 (  X )  human ( X )  mother ( g 2 ( X ), X )
York University- CSE 3401
02-CNF & Horn
21
Another example
(  X ) (  Y ) m ( X , Y )  (  Y ) p (Y , X ) 
(  X )  (  Y ) m ( X , Y )  (  Y ) p (Y , X ) 
(  X ) (  Y )  m ( X , Y )  (  Y ) p (Y , X ) 
(  X ) (  Y )  m ( X , Y )  (  Z ) p ( Z , X ) 
(  X )(  Y )(  Z )  m ( X , Y )  p ( Z , X ) 
(  X )(  Y )  m ( X , Y )  p ( g ( X ), X ) 
 m ( X , Y )  p ( g ( X ), X )
1. Remove imp. and equiv.
2. Move negations inwards
3. Rename variables
4. Move quantifiers to front
5. Skolemize
6. Distribute OR over AND
7. Remove quantifiers
p ( g ( X ), X ) :  m ( X , Y ).
York University- CSE 3401
02-CNF & Horn
22
Example
• All Martians like to eat some kind of spiced food.
[from Advanced Prolog Techniques and examples- Peter Ross]
 (  X )( martian ( X )  (  Y )(  Z )( food (Y )  spice ( Z )  contains (Y , Z )  likes ( X , Y )))
 (  X )(  martian ( X )  (  Y )(  Z )( food (Y )  spice ( Z )  contains (Y , Z )  likes ( X , Y )))
 (  X )(  Y )(  Z )(  martian ( X )  ( food (Y )  spice ( Z )  contains (Y , Z )  likes ( X , Y )))
 (  X )(  martian ( X )  ( food ( f ( X ))  spice ( s ( X ))  contains ( f ( X ), s ( X ))  likes ( X , f ( X ))))
 (  X )((  martian ( X )  food ( f ( X )))  (  martian ( X )  spice ( s ( X ))) 
(  martian ( X )  contains ( f ( X ), s ( X )))  (  martian ( X )  likes ( X , f ( X ))))
 (  martian ( X )  food ( f ( X ))) 
(  martian ( X )  spice ( s ( X ))) 
(  martian ( X )  contains ( f ( X ), s ( X ))) 
(  martian ( X )  likes ( X , f ( X )))
York University- CSE 3401
02-CNF & Horn
23
Download