Conjunctive Normal Form & Horn Clauses
Total Page:16
File Type:pdf, Size:1020Kb
Conjunctive Normal Form & Horn Clauses York University CSE 3401 Vida Movahedi York University‐ CSE 3401‐ V. Movahedi 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‐ V. Movahedi 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 – The empty clause, shown as □, :‐ or {} – e.g. p, p ∨ ¬q ∨ r • 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 CNF (example) ( p ∨ q) ∧ (q ∨ ¬s ∨ r) ∧ (¬r ∨ t) ˄ ˅ ˅ ˅ p q q ¬s r ¬r t York University‐ CSE 3401‐ V. Movahedi 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‐ V. Movahedi 5 Conversion to CNF 1. Remove implication and equivalence Example: – Use ( p → q) ⇒ (¬p ∨ q) ( p ≡ q) ⇒ ( p → q) ∧ (q → p) p ≡ (r ∧ s) ⇒ (¬p ∨ q) ∧ (¬q ∨ p) ⇒ (¬p ∨ (r ∧ s)) ∧ (¬(r ∧ s) ∨ p) 2. Move negations inwards – Use De M’Morgan’s ⇒ (¬p ∨ r) ∧ ¬( p ∧ q) ⇒ (¬p ∨ ¬q) (¬p ∨ s) ∧ ¬( p ∨ q) ⇒ (¬p ∧ ¬q) (¬r ∨ ¬s ∨ p) 3. Distribute OR over AND p ∨ (q ∧ r) ⇒ ( p ∨ q) ∧ ( p ∨ r) York University‐ CSE 3401‐ V. Movahedi 6 Representing a clause • Consider this clause: ¬p ∨ q ∨ ¬r ∨ s • It can be written as: ¬( 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‐ V. Movahedi 7 Logic Programming Clause • A clause in the form: p1; p2 ;...; pm : −q1,q2 ,...,qn . is equivalent to: p1 ∨ p2 ∨ ... ∨ pm ∨ ¬q1 ∨ ¬q2 ∨ ... ∨ ¬qn or q1 ∧ q2 ∧ ... ∧ qn → p1 ∨ p2 ∨ ... ∨ pm if q1 ∧ q2 ∧ ... ∧ qn is true, then at least one of p1 , p2 ,..., pm is true. York University‐ CSE 3401‐ V. Movahedi 8 Another Example Write the following expression as LLogicogic 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 sets notation of a CNF {( p ∨ q),(¬s ∨ r ∨ q),(¬r ∨ t)} 3‐ StSymmetry of ˅ allows for set notation { {p,q}{ , q,¬s,r } , {¬r,t} } of clauses 4‐ As Logic Prog. p;q : −. q;r : −s. t : −r. York University‐ CSE 3401‐ V. Movahedi 9 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 simp lify the ilimplementat ion of lilogic programming languages and are therefore used in Prolog. York University‐ CSE 3401‐ V. Movahedi 10 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‐ V. Movahedi 11 Query • A computational qqyuery (or gg)oal) 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 : −r1,r2 ,...,rn . which is ¬r1 ∨ ¬r2 ∨ ...∨ ¬rn or ¬(r1 ∧ r2 ∧...∧ rn ) • Why? In logic programming theorem proving is used to answer queries: P | − r1 ∧ r2 ∧...∧ rn iff P U{¬(r1 ∧ r2 ∧...∧ rn )} is inconsistent York University‐ CSE 3401‐ V. Movahedi 12 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‐ V. Movahedi 13 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 dldeal wihith quantifiers and thiheir object variables when converting to CNF York University‐ CSE 3401‐ V. Movahedi 14 Conversion to CNF in Predicate Logic 1. Remove implication and equivalence 2. Move negations inwards Note ¬(∃x) p(x) ≡ (∀x)¬p(x) 3. Rename variables so that variables of each quantifier are unique 4. Move all quantifiers to the front (Prenex Normal Form) York University‐ CSE 3401‐ V. Movahedi 15 Conversion to CNF (cont.) 5. Skolemizing (get rid of existential qq)uantifiers) 5. Skolem constants ((∃X ) female(X ) ∧ motherof (X ,eve)) ⇒ female(g1) ∧ motherof (g1,eve) 6. Skolem functions (∀X )(∃Y )¬human(X ) ∨ motherof (X ,Y ) ⇒ (∀X )¬human(X ) ∨ motherof (X , g2(X )) 6. Distribute OR over AND to have conjunctions of disjunctions as the body of the formula 7. Remove all universal quantifiers York University‐ CSE 3401‐ V. Movahedi 16 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‐ V. Movahedi 17.