Conjunctive Normal Form & Horn Clauses York University CSE 3401 - - PowerPoint PPT Presentation

conjunctive normal form horn clauses
SMART_READER_LITE
LIVE PREVIEW

Conjunctive Normal Form & Horn Clauses York University CSE 3401 - - PowerPoint PPT Presentation

Conjunctive Normal Form & Horn Clauses York University CSE 3401 Vida Movahedi 1 York University CSE 3401 V. Movahedi Overview Overview Definition of literals, clauses, and CNF , , Conversion to CNF Propositional logic


slide-1
SLIDE 1

Conjunctive Normal Form & Horn Clauses

York University CSE 3401 Vida Movahedi

York University‐ CSE 3401‐ V. Movahedi

1

slide-2
SLIDE 2

Overview Overview

  • Definition of literals, clauses, and CNF

, ,

  • Conversion to CNF‐ Propositional logic
  • Representation of clauses in logic programming
  • Representation of clauses in logic programming
  • Horn clauses and Programs

– Facts – Facts – Rules – Queries (goals)

  • Conversion to CNF‐ Predicate logic

[ref.: Clocksin‐ Chap. 10 and Nilsson‐ Chap. 2]

York University‐ CSE 3401‐ V. Movahedi

2

slide-3
SLIDE 3

Conjunctive Normal Form Conjunctive Normal Form

  • A literal is either an atomic formula (called a positive

( p literal) or a negated atomic formula (called a negated literal)

– e.g. p, ¬q e.g. p, q

  • A clause is

– A literal, or – Disjunction of two or more literals, or – The empty clause, shown as □, :‐ or {} – e.g. p,

r q p ∨ ¬ ∨

  • 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‐ V. Movahedi

3

slide-4
SLIDE 4

CNF (example) CNF (example)

) ( ) ( ) ( t r r s q q p ∨ ¬ ∧ ∨ ¬ ∨ ∧ ∨ ) ( ) ( ) ( t r r s q q p ∨ ¬ ∧ ∨ ¬ ∨ ∧ ∨

˄ ˅ ˅ ˅ p q q ¬s r ¬r t

York University‐ CSE 3401‐ V. Movahedi

4

slide-5
SLIDE 5

CNF‐ Facts CNF Facts

  • For every formula α of propositional logic, there

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
  • A polynomial algorithm exists for converting α to A
  • For practical purposes, we use CNFs in Logic

i Programming

York University‐ CSE 3401‐ V. Movahedi

5

slide-6
SLIDE 6

Conversion to CNF Conversion to CNF

  • 1. Remove implication and equivalence

Example:

p q

– Use

) ( ) ( ) ( ) ( ) ( ) ( ) ( p q q p q p q p q p → ∧ → ⇒ ≡ ∨ ¬ ⇒ →

)) ( ( ) ( s r p s r p ∧ ∧ ∨ ⇒ ∧ ≡

Example:

  • 2. Move negations inwards

U D M ’

) ( ) ( p q q p ∨ ¬ ∧ ∨ ¬ ⇒

) ( ) ) ( ( )) ( ( r p p s r s r p ∧ ∨ ⇒ ∨ ∧ ¬ ∧ ∧ ∨ ¬ ⇒

– Use De Morgan’s

) ( ) ( ) ( ) ( q p q p q p q p ¬ ∧ ¬ ⇒ ∨ ¬ ¬ ∨ ¬ ⇒ ∧ ¬

) ( ) ( ) ( s p r p ∨ ∨ ∧ ∨ ¬ ∧ ∨ ¬ ⇒

  • 3. Distribute OR over AND

) ( ) ( q p q p ¬ ∧ ¬ ⇒ ∨ ¬

) ( ) ( ) ( r p q p r q p ∨ ∧ ∨ ⇒ ∧ ∨ ) ( p s r ∨ ¬ ∨ ¬

York University‐ CSE 3401‐ V. Movahedi

6

) ( ) ( ) ( r p q p r q p ∨ ∧ ∨ ⇒ ∧ ∨

slide-7
SLIDE 7

Representing a clause Representing a clause

  • Consider this clause:

s r q p ∨ ¬ ∨ ∨ ¬

Consider this clause:

  • It can be written as:

q p

) ( ) ( ) ( s q r p s q r p ∨ → ∧ ⇒ ∨ ∨ ∧ ¬

  • In Logic programming, it is shown as:

) ( ) ( r p s q ∧ ← ∨ . , : ; ) ( ) ( r p s q p s q − ←

  • Easy way: positive literals on the left, negative literals
  • n the right

York University‐ CSE 3401‐ V. Movahedi

7

slide-8
SLIDE 8

Logic Programming Clause Logic Programming Clause

  • A clause in the form:

A clause in the form: is equivalent to:

. ,..., , : ;...; ;

2 1 2 1 n m

q q q p p p −

is equivalent to:

  • r

n m

p p p q q q q q q p p p ∨ ∨ ∨ → ∧ ∧ ∧ ¬ ∨ ∨ ¬ ∨ ¬ ∨ ∨ ∨ ∨ ... ...

2 1 2 1

  • r

if is true then at least one of

m n

p p p q q q ∨ ∨ ∨ → ∧ ∧ ∧ ... ...

2 1 2 1

q q q ∧ ∧ ∧

if is true, then at least one of is true.

n

q q q ∧ ∧ ∧ ...

2 1 m

p p p ,..., ,

2 1

York University‐ CSE 3401‐ V. Movahedi

8

slide-9
SLIDE 9

Another Example Another Example

Write the following expression as Logic Programming Clauses: Write the following expression as Logic Programming Clauses:

1‐ Conversion to CNF:

( ) ( )

) ( ) ( t r q r s p → ∧ ∨ → ∧

( ) ( )

1 Conversion to CNF: 2‐ Symmetry of ˄

( ) ( )

) ( ) ( ) ( ) ( ) ( t r q r s q p t r q r s p ∨ ¬ ∧ ∨ ∨ ¬ ∧ ∨ ⇒ ∨ ¬ ∧ ∨ ∨ ¬ ∧ ⇒

allows for sets notation

  • f a CNF

3 S t f ˅

{ }

) ( ), ( ), ( t r q r s q p ∨ ¬ ∨ ∨ ¬ ∨

3‐ Symmetry of ˅ allows for set notation

  • f clauses

{ } { } { } { }

, , , , , , t r r s q q p ¬ ¬

4‐ As Logic Prog.

York University‐ CSE 3401‐ V. Movahedi

9

. : . : ; . : ; r t s r q q p − − −

slide-10
SLIDE 10

Horn Clause Horn Clause

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

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. Queries (or goals) : body. e.g. : r1, r2, ..., rm. H l i lif h i l i f l i

  • Horn clauses simplify the implementation of logic

programming languages and are therefore used in Prolog.

York University‐ CSE 3401‐ V. Movahedi

10

slide-11
SLIDE 11

A Program A Program

  • A logic programming program P is defined as a finite

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 called definite clauses and therefore a program defined by them is called a definite program.

York University‐ CSE 3401‐ V. Movahedi

11

slide-12
SLIDE 12

Query Query

  • A computational query (or goal) is the conjunction of some

p q y ( g ) j positive literals (called subgoals) , e.g.

n

r r r ∧ ∧ ∧ ...

2 1

  • A query is deductible from P if it can be proven on the basis
  • f P:

n

r r r P ∧ ∧ ∧ − ... |

2 1

  • Note this query is written as

which is or

) ... (

2 1 n

r r r ∧ ∧ ∧ ¬

n

r r r ¬ ∨ ∨ ¬ ∨ ¬ ...

2 1

.

n

r r r ,..., , :

2 1

  • Why? In logic programming theorem proving is used to

answer queries: iff is inconsistent

York University‐ CSE 3401‐ V. Movahedi

12

n

r r r P ∧ ∧ ∧ − ... |

2 1

{ }

) ... (

2 1 n

r r r P ∧ ∧ ∧ ¬ U

slide-13
SLIDE 13

Example Example

  • P: { p:‐q. , q:‐.}

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.

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. program (e.g. Prolog) will return true.

York University‐ CSE 3401‐ V. Movahedi

13

slide-14
SLIDE 14

Predicate Logic Clauses Predicate Logic Clauses

  • Same definition for literals, clauses, and CNF except

Same definition for literals, clauses, and CNF except now each literal is more complicated since an atomic formula is more complicated in predicate logic d d l i h ifi d h i bj

  • We need to deal with quantifiers and their object

variables when converting to CNF

York University‐ CSE 3401‐ V. Movahedi

14

slide-15
SLIDE 15

Conversion to CNF in Predicate Logic Conversion to CNF in Predicate Logic

1. Remove implication and equivalence p q 2. Move negations inwards

Note

) ( ) ( ) ( ) ( x p x x p x ∀ ≡ ∃

Note

3. Rename variables so that variables of each quantifier are

) ( ) ( ) ( ) ( x p x x p x ¬ ∀ ≡ ∃ ¬

unique 4. Move all quantifiers to the front (Prenex Normal Form) q ( )

York University‐ CSE 3401‐ V. Movahedi

15

slide-16
SLIDE 16

Conversion to CNF (cont.) Conversion to CNF (cont.)

5. Skolemizing (get rid of existential quantifiers) g (g q )

5. Skolem constants 6 Skolem functions ) , 1 ( ) 1 ( )) , ( ) ( ) (( eve g motherof g female eve X motherof X female X ∧ ⇒ ∧ ∃ 6. Skolem functions

)) ( 2 , ( ) ( ) ( ) , ( ) ( ) )( ( X g X motherof X human X Y X motherof X human Y X ∨ ¬ ∀ ⇒ ∨ ¬ ∃ ∀

  • 6. Distribute OR over AND to have conjunctions of

disjunctions as the body of the formula

)) ( , ( ) ( ) ( g f

j y

  • 7. Remove all universal quantifiers

York University‐ CSE 3401‐ V. Movahedi

16

slide-17
SLIDE 17

Example Example

  • All Martians like to eat some kind of spiced food.

All Martians like to eat some kind of spiced food.

[from Advanced Prolog Techniques and examples‐ Peter Ross]

))) , ( ) , ( ) ( ) ( )( )( ( ) ( )( ( Y X likes Z Y contains Z spice Y food Z Y X martian X ∧ ∧ ∧ ∃ ∃ → ∀ ⇒ )))) ( , ( )) ( ), ( ( )) ( ( )) ( ( ( ) ( )( ( ))) , ( ) , ( ) ( ) ( ( ) ( )( )( )( ( ))) , ( ) , ( ) ( ) ( )( )( ( ) ( )( ( X f X likes X s X f contains X s spice X f food X martian X Y X likes Z Y contains Z spice Y food X martian Z Y X Y X likes Z Y contains Z spice Y food Z Y X martian X ∧ ∧ ∧ ∨ ¬ ∀ ⇒ ∧ ∧ ∧ ∨ ¬ ∃ ∃ ∀ ⇒ ∧ ∧ ∧ ∃ ∃ ∨ ¬ ∀ ⇒ ))) ( ( ) ( ( )))) ( , ( ) ( ( ))) ( ), ( ( ) ( ( ))) ( ( ) ( ( ))) ( ( ) ( )(( ( )))) ( , ( )) ( ), ( ( )) ( ( )) ( ( ( ) ( )( ( X f food X martian X f X likes X martian X s X f contains X martian X s spice X martian X f food X martian X f f p f f ∧ ∨ ¬ ⇒ ∨ ¬ ∧ ∨ ¬ ∧ ∨ ¬ ∧ ∨ ¬ ∀ ⇒ ))) ( ( ) ( ( ))) ( ), ( ( ) ( ( ))) ( ( ) ( ( ))) ( ( ) ( ( X f X likes X martian X s X f contains X martian X s spice X martian X f food X martian ∨ ∧ ∨ ¬ ∧ ∨ ¬ ∧ ∨ ¬ ⇒

York University‐ CSE 3401‐ V. Movahedi

17

))) ( , ( ) ( ( X f X likes X martian ∨ ¬