Logic, Resolution and Prolog Michael Zakharyaschev Department of Computer Science and Information Systems Birkbeck, University of London • email: zmishaz@gmail.com • homepage: http://www.dcs.bbk.ac.uk/~michael • course site: http://www.dcs.bbk.ac.uk/~michael/res/res.html
5. Horn programs
Horn programs A Horn program , P , consists of sentences of the form – ∀ X 1 . . . ∀ X k ( A 1 ∧ A 2 ∧ · · · ∧ A n → A 0 ) where the A i are atoms usually written as A 0 ← A 1 , . . . , A n and called a rule or clause A 0 the head and A 1 , . . . , A n the body – ∀ X 1 . . . ∀ X k A where A is an atom usually written as A 0 ← and called a fact – A goal is a conjunction of atoms G = C 1 ∧ · · · ∧ C l usually written as ? C 1 , . . . , C l The variables Y 1 , . . . , Y m in G are goal variables (or answer variables ) An answer to G over P is any substitution θ = { Y 1 /t 1 , . . . , Y m /t m } = � P → ∀ Z 1 . . . ∀ Z N Gθ An answer θ is a correct answer if | where Z 1 , . . . , Z N are all the variables in t 1 , . . . , t m This gives the declarative semantics of Horn programs Logic, Resolution and Prolog (5) 1
Example G : ? elem ( X, w . o . r . d . nil) elem ( X, X . L ) P : elem ( X, Y . L ) ← elem ( X, L ) concat (nil , Y, Y ) concat ( U . X, Y, U . Z ) ← concat ( X, Y, Z ) common ( X, Y ) ← elem ( U, X ) , elem ( U, Y ) There are four correct answers to G over P θ 1 = { X/w } , θ 2 = { X/o } , θ 3 = { X/r } , θ 4 = { X/d } because ∀ X ∀ Y ∀ L ( elem ( X, L ) → elem ( X, Y . L )) ∧ � | = ∀ X ∀ L elem ( X, X . L ) → elem ( X, w . o . r . d . nil) θ i � How to compute the correct answers? that is, what is the procedural (or operational ) semantics? Logic, Resolution and Prolog (5) 2
Procedural semantics Given a goal G ( Y 1 , . . . Y m ) = ? C 1 , C 2 , . . . , C m over a Horn program P = { D 1 , . . . , D N } we consider the validity problem | = D 1 ∧ · · · ∧ D N → ∃ Y 1 . . . ∃ Y k ( C 1 ∧ C 2 ∧ · · · ∧ C m ) , which is equivalent to the unsatisfiability problem for the sentence ¬ ( D 1 ∧ · · · ∧ D N → ∃ Y 1 . . . ∃ Y k ( C 1 ∧ C 2 ∧ · · · ∧ C m ) , which is equivalent to unsatisfiability of D 1 ∧ · · · ∧ D N ∧ ∀ Y 1 . . . ∀ Y k ( ¬ C 1 ∨ ¬ C 2 ∨ · · · ∨ ¬ C m ) , which is represented as the set of clauses S P ,G = { D 1 , . . . , D N , ¬ C 1 ∨ ¬ C 2 ∨ · · · ∨ ¬ C m } , which is treated by the resolution method using a special strategy Logic, Resolution and Prolog (5) 3
SLD-resoltion G 0 D i 1 ✏ ✏ ✏ ✏ ✏ ❄ ✏ ✮ G 1 D i 2 ✏ ✏ ✏ ✏ ✏ ❄ ✏ ✮ G 2 D i k q q ✏ q ✏ ✏ ✏ ✏ ❄ ✏ ✮ � L inear resolution with S election function for D efinite clauses Logic, Resolution and Prolog (5) 4
SLD-resolution: definition Suppose – G = ? C 1 , . . . , C i , . . . , C m is a goal with a selected sub-goal C i – D ′ = A ′ 0 ← A ′ 1 , A ′ 2 , . . . , A ′ n is a variant of a rule in P which does not have common variables with G – θ ∈ MGU (C i , A ′ 0 ) is an MGU of the sub-goal C i and the head A ′ 0 of D ′ Then the goal G ′ = ?( C 1 , . . . , C i − 1 , A ′ 1 , A ′ 2 , . . . , A ′ – n , C i +1 , . . . , C m ) θ is called an SLD-resolvent of D ′ and G for the selected sub-goal C i and the unifier θ In other words, we apply the resolution rule: D ′ = A ′ 0 ∨ ¬ A ′ 1 ∨ ¬ A ′ 2 ∨ · · · ∨ A ′ G = ¬ C 1 ∨ · · · ∨ ¬ C i ∨ · · · ∨ ¬ C m n θ = MGU ( C i , A ′ 0 ) G ′ = ( ¬ C 1 ∨ · · · ∨ ¬ C i − 1 ∨ ¬ A ′ 1 ∨ ¬ A ′ 2 ∨ · · · ∨ ¬ A ′ n ∨ ¬ C i +1 ∨ · · · ∨ ¬ C m ) θ Logic, Resolution and Prolog (5) 5
SLD-derivation Given a goal G 1 ( Y 1 , . . . Y m ) = ? C 1 , C 2 , . . . , C m over a Horn program P = { D 1 , . . . , D N } , an SLD-derivation from G 1 and P is a (finite or infinite) sequence of triples ( D j 1 , θ 1 , G 1 ) , ( D j 2 , θ 2 , G 2 ) , . . . , ( D j n , θ n , G n ) , . . . such that, for every i, i ≥ 1 , – D j i ∈ P , θ i is a substitution, G i is a goal – G i is an SLD-resolvent of D j i and G i − 1 with unifier θ i A finite SLD-derivation ( D j 1 , θ 1 , G 1 ) , ( D j 2 , θ 2 , G 2 ) , . . . , ( D j k , θ n , G n ) is successful (or an SLD-refutation ) if G n = � The restriction of θ = θ 1 θ 2 . . . θ n to the goal variables Y 1 , . . . , Y k is called a computed answer for P and G 1 If G n has no SLD-resolvent, then the derivation is called failed Logic, Resolution and Prolog (5) 6
Examples Program P : elem ( X, X . L ) elem ( X, Y . L ) ← elem ( X, L ) Goal G 1 : ? elem ( X, a . b . c . nil) Logic, Resolution and Prolog (5) 7
Examples Program P : elem ( X, X . L ) elem ( X, Y . L ) ← elem ( X, L ) Goal G 1 : ? elem ( X, a . b . c . nil) Goal G 2 : ? elem ( c, a . b . nil) Logic, Resolution and Prolog (5) 7
Examples Program P : elem ( X, X . L ) elem ( X, Y . L ) ← elem ( X, L ) Goal G 1 : ? elem ( X, a . b . c . nil) Goal G 2 : ? elem ( c, a . b . nil) Goal G 3 : ? elem ( a, X ) Logic, Resolution and Prolog (5) 7
Examples Program P : elem ( X, X . L ) elem ( X, Y . L ) ← elem ( X, L ) Goal G 1 : ? elem ( X, a . b . c . nil) Goal G 2 : ? elem ( c, a . b . nil) Goal G 3 : ? elem ( a, X ) Are the computed answers correct? Can all the correct answers be computed? Logic, Resolution and Prolog (5) 7
Correctness and completeness Theorem: Every computed answer is correct. For every correct answer θ , there is a computed answer η such that θ = ηρ , for some substitution ρ (this holds for any selection function for subgoals Example: Program P : P ( f ( X ) , X ) ← R ( X ) R ( Y ) ← Q ( c ) ← Goal: ? P ( U, V ) The substitution θ = { U/f ( c ) , V/c } is a correct answer, η = { U/f ( Y ) , V/Y } is the only computed answer θ = η { Y/c } Standard selection function : always take the leftmost sub-goal Logic, Resolution and Prolog (5) 8
Choosing clauses in programs Goal: ? P ( U, V ) , R ( U ) Program P : P ( X, Y ) ← R ( X ) , Q ( Y ) P ( X, X ) ← Q ( X ) R ( b ) ← Q ( c ) ← By choosing the first rule in P , we obtain a successful derivation with the answer { U/b, V/c } The second rule leads to failure Logic, Resolution and Prolog (5) 9
Derivation tree An SLD-derivation tree for a goal G 0 and a program P is a labelled tree T G 0 , P such that – G 0 the root of the tree – the immediate successors of a vertex G are all the SLD-resolvents of G (under the standard selection function for sub-goals) – The leaves of the tree are � and goals without resolvents (failures) SLD-derivation trees can be finite or infinite, with a finite or infinite sets of branches, etc. Logic, Resolution and Prolog (5) 10
Derivation tree: example ?P(X . Y) s P : (1) , { X 1 /X, L 1 /Y } P ( X . L ) ← P ( L ) , R ( X ) ❄ ?P(Y) , R ( X ) (1) , { Y/X 2 . L 2 } s � ❅ (2) , { Y/ nil } P (nil) ← � ❅ � ✠ ❅ ❘ ?P(L 2 ) , R ( X 2 ) , R ( X ) ?R(X) ✏ (1) , { L 2 /X 3 . L 3 } ✏ R ( a ) ← s s ✏ ❅ ✏ (3) , { X/a } (4) , { X/c } ✏ ✏ ❅ ✏ (2) , { L 2 / nil } ✏ ?P(L 3 ) , R ( X 3 ) , R ( c ) ← ✏ ❄ ❄ ❅ ❘ ✮ ✏ ✏ ?R(X 2 ) , R ( X ) ✏ R ( X 2 ) , R ( X ) s s s s ✏ � ❅ ✏ ✏ ? � ? � ✏ (3) , { X 1 /a } � ❅ (3) , { X 1 /a } ✏ ✏ ✮ (2) , { L 3 / nil } ❄ ✠ � ❘ ❅ ∞ ?R(X) ?R(X) s s s ❅ ❅ ?R(X 3 ) , R ( X 2 ) , R ( X ) (4) , { X/c } (4) , { X/c } ❅ ❅ (3) , { X/a } (3) , { X/a } ❄ ❅ ❘ ❄ ❘ ❅ s s s s ? � ? � ? � ? � How to search derivation trees: breadth-first? depth-first? some other way? depth-first with backtracking Standard (compromise) strategy: it is effective, but incomplete! (because termination is not guaranteed) The order of clauses in programs matters! Logic, Resolution and Prolog (5) 11
Computational power of Horn programs What can be computed by Horn programs? Horn programs are algorithmically complete in the sense that they can compute any function computable by Turing machines (or by programs in any programming language) Exercise: given a Turing machine M , construct a Horn program P M simulating the computations of M (Hint: represent words on the machine tape as lists) Theorem: There is no algorithm that can decide, given a goal G and a Horn program P , 1. whether their SLD-derivation tree is finite 2. whether their SLD-derivation tree contains at least one successful derivation 3. whether a given substitution is a computed answer Logic, Resolution and Prolog (5) 12
Recommend
More recommend