<<

& 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 • Representation of clauses in • 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 q q ¬s r ¬r t

York University- CSE 3401 02-CNF & Horn 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 )  ( p  q)  ( p  r )

York University- CSE 3401 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))  ((r  s)  p )  ( p  r )  ( p  s)  ( r   s  p )

York University- CSE 3401 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: p  p  ...  p  q  q  ...  q 1 2 m 1 2 n

or q1  q 2  ...  q n  p1  p 2  ...  p m

if q 1  q 2  ...  q n is true, then at least one of

p1 , p 2 ,..., p m is true.

York University- CSE 3401 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)

convert to CNF : ( p  r )  ( p  s)  ( r   s  p ) 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  r )  q   (r  t ) 1- Conversion to CNF:   p  ( s  r )  q   ( r  t)

2- Symmetry of ˄  ( p  q)  ( s  r  q)  ( r  t) allows for set notation of a CNF ( p  q ), ( s  r  q ), ( r  t )

3- Symmetry of ˅ allows for set notation  p, q , q,  s, r ,  r , t  of clauses

4- Logic Prog. notation p; q : . q; r :  s. t :  r.

York University- CSE 3401 02-CNF & Horn 12 Horn Clause

• A Horn clause is a clause with at most one positive literal:

– Rules “head:- body.” e.g. p1:-q1, q2, ..., qn. – Facts “head :-.” e.g. p2:-. – Queries (or goals) “:-body.” e.g. :- r1, r2, ..., rm.

• Horn clauses simplify the implementation of logic programming languages and are therefore used in .

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. r1  r2  ...  rn

• 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 :  r 1 , r 2 ,..., r n .

which is  r 1   r 2  ...   r n or (r1  r2  ...  rn )

• Why? “Proof by contradiction” is used to answer queries:

iff P    ( r 1  r 2  ...  r n )  is inconsistent

York University- CSE 3401 02-CNF & Horn 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:

(X )(Y )m ( X ,Y )  n( X ) Step 1. Remove implication and equivalence

(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 front (PNF) (X )( Y ) m ( X ,Y )  n( X )

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: n( X ) :  m ( X ,Y ).

York University- CSE 3401 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 ( g1)  mother (eve , g1)

– 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 ) 1. Remove imp. and equiv. (X )(Y )m ( X ,Y )  (Y ) p (Y , X ) 2. Move negations inwards (X )(Y ) m ( X ,Y )  (Y ) p (Y , X ) 3. Rename variables (X )(Y ) m ( X ,Y )  (Z ) p (Z , X ) 4. Move quantifiers to front (X )( Y )( Z )  m ( X ,Y )  p (Z , X )   5. Skolemize (X )( Y ) m ( X ,Y )  p ( g ( X ), X ) 6. Distribute OR over AND  m ( X ,Y )  p ( g ( X ), X ) 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