Logic, Resolution and Prolog Michael Zakharyaschev Department of - - PowerPoint PPT Presentation

logic resolution and prolog
SMART_READER_LITE
LIVE PREVIEW

Logic, Resolution and Prolog Michael Zakharyaschev Department of - - PowerPoint PPT Presentation

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:


slide-1
SLIDE 1

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

slide-2
SLIDE 2
  • 5. Horn programs
slide-3
SLIDE 3

Horn programs

A Horn program, P, consists of sentences of the form – ∀X1 . . . ∀Xk (A1 ∧ A2 ∧ · · · ∧ An → A0) where the Ai are atoms usually written as A0 ← A1, . . . , An and called a rule or clause

A0 the head and A1, . . . , An the body

– ∀X1 . . . ∀Xk A where A is an atom usually written as A0 ← and called a fact – A goal is a conjunction of atoms G = C1 ∧ · · · ∧ Cl usually written as ?C1, . . . , Cl The variables Y1, . . . , Ym in G are goal variables (or answer variables) An answer to G over P is any substitution θ = {Y1/t1, . . . , Ym/tm} An answer θ is a correct answer if | = P → ∀Z1 . . . ∀ZN Gθ where Z1, . . . , ZN are all the variables in t1, . . . , tm This gives the declarative semantics of Horn programs

Logic, Resolution and Prolog (5) 1

slide-4
SLIDE 4

Example

G : ? elem(X, w.o.r.d.nil) P : elem(X, X.L) 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

slide-5
SLIDE 5

Procedural semantics

Given a goal G(Y1, . . . Ym) = ? C1, C2, . . . , Cm over a Horn program P = {D1, . . . , DN} we consider the validity problem | = D1 ∧ · · · ∧ DN → ∃Y1 . . . ∃Yk (C1 ∧ C2 ∧ · · · ∧ Cm), which is equivalent to the unsatisfiability problem for the sentence ¬(D1 ∧ · · · ∧ DN → ∃Y1 . . . ∃Yk(C1 ∧ C2 ∧ · · · ∧ Cm), which is equivalent to unsatisfiability of D1 ∧ · · · ∧ DN ∧ ∀Y1 . . . ∀Yk(¬C1 ∨ ¬C2 ∨ · · · ∨ ¬Cm), which is represented as the set of clauses SP,G = {D1, . . . , DN, ¬C1 ∨ ¬C2 ∨ · · · ∨ ¬Cm}, which is treated by the resolution method using a special strategy

Logic, Resolution and Prolog (5) 3

slide-6
SLIDE 6

SLD-resoltion G0 Di1

❄ ✏ ✏ ✏ ✏ ✏ ✏ ✮

G1 Di2

❄ ✏ ✏ ✏ ✏ ✏ ✏ ✮

G2

q q q

Dik

❄ ✏ ✏ ✏ ✏ ✏ ✏ ✮

  • Linear resolution with Selection function for Definite clauses

Logic, Resolution and Prolog (5) 4

slide-7
SLIDE 7

SLD-resolution: definition

Suppose – G = ? C1, . . . , Ci, . . . , Cm is a goal with a selected sub-goal Ci – 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(Ci, A′

0) is an MGU of the sub-goal Ci and the head A′ 0 of D′

Then the goal – G′ = ?(C1, . . . , Ci−1, A′

1, A′ 2, . . . , A′ n, Ci+1, . . . , Cm)θ

is called an SLD-resolvent of D′ and G for the selected sub-goal Ci and the unifier θ In other words, we apply the resolution rule: G = ¬C1 ∨ · · · ∨ ¬Ci ∨ · · · ∨ ¬Cm D′ = A′

0 ∨ ¬A′ 1 ∨ ¬A′ 2 ∨ · · · ∨ A′ n

θ = MGU(Ci, A′

0)

G′ = (¬C1 ∨ · · · ∨ ¬Ci−1 ∨ ¬A′

1 ∨ ¬A′ 2 ∨ · · · ∨ ¬A′ n ∨ ¬Ci+1 ∨ · · · ∨ ¬Cm)θ Logic, Resolution and Prolog (5) 5

slide-8
SLIDE 8

SLD-derivation

Given a goal G1(Y1, . . . Ym) = ? C1, C2, . . . , Cm over a Horn program P = {D1, . . . , DN}, an SLD-derivation from G1 and P is a (finite or infinite) sequence of triples (Dj1, θ1, G1), (Dj2, θ2, G2), . . . , (Djn, θn, Gn), . . . such that, for every i, i ≥ 1, – Dji ∈ P, θi is a substitution, Gi is a goal – Gi is an SLD-resolvent of Dji and Gi−1 with unifier θi A finite SLD-derivation (Dj1, θ1, G1), (Dj2, θ2, G2), . . . , (Djk, θn, Gn) is successful (or an SLD-refutation) if Gn = The restriction of θ = θ1θ2 . . . θn to the goal variables Y1, . . . , Yk is called a computed answer for P and G1 If Gn has no SLD-resolvent, then the derivation is called failed

Logic, Resolution and Prolog (5) 6

slide-9
SLIDE 9

Examples

Program P: elem(X, X.L) elem(X, Y.L) ← elem(X, L) Goal G1: ? elem(X, a.b.c.nil)

Logic, Resolution and Prolog (5) 7

slide-10
SLIDE 10

Examples

Program P: elem(X, X.L) elem(X, Y.L) ← elem(X, L) Goal G1: ? elem(X, a.b.c.nil) Goal G2: ? elem(c, a.b.nil)

Logic, Resolution and Prolog (5) 7

slide-11
SLIDE 11

Examples

Program P: elem(X, X.L) elem(X, Y.L) ← elem(X, L) Goal G1: ? elem(X, a.b.c.nil) Goal G2: ? elem(c, a.b.nil) Goal G3: ? elem(a, X)

Logic, Resolution and Prolog (5) 7

slide-12
SLIDE 12

Examples

Program P: elem(X, X.L) elem(X, Y.L) ← elem(X, L) Goal G1: ? elem(X, a.b.c.nil) Goal G2: ? elem(c, a.b.nil) Goal G3: ? elem(a, X) Are the computed answers correct? Can all the correct answers be computed?

Logic, Resolution and Prolog (5) 7

slide-13
SLIDE 13

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

slide-14
SLIDE 14

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

slide-15
SLIDE 15

Derivation tree

An SLD-derivation tree for a goal G0 and a program P is a labelled tree TG0,P such that – G0 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

slide-16
SLIDE 16

Derivation tree: example

P: P (X.L) ← P (L), R(X) P (nil) ← R(a) ← R(c) ←

?P(X.Y) s ❄ ?P(Y), R(X)

(1), {X1/X, L1/Y }

s

❅ ❅ ❅ ❘ s s ?P(L2), R(X2), R(X) ?R(X)

(1), {Y/X2.L2} (2), {Y/nil}

❄ ✏ ✏ ✏ ✏ ✏ ✏ ✏ ✏ ✏ ✏ ✮ ❄ ❅ ❅ ❅ ❘ s s s s ?R(X2), R(X) ?P(L3), R(X3), R(X2), R(X) ? ?

(2), {L2/nil} (1), {L2/X3.L3} (3), {X/a} (4), {X/c}

✏ ✏ ✏ ✏ ✏ ✏ ✏ ✏ ✮ ❄

❅ ❅ ❅ ❘ s s s ?R(X3), R(X2), R(X) ∞ ?R(X) ?R(X)

(3), {X1/a} (3), {X1/a} (2), {L3/nil}

❄ ❅ ❅ ❅ ❘ s s ? ?

(3), {X/a} (4), {X/c}

❄ ❅ ❅ ❅ ❘ s s ? ?

(3), {X/a} (4), {X/c}

How to search derivation trees: breadth-first? depth-first? some other way? Standard (compromise) strategy:

depth-first with backtracking

it is effective, but incomplete! (because termination is not guaranteed) The order of clauses in programs matters!

Logic, Resolution and Prolog (5) 11

slide-17
SLIDE 17

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 PM 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