Docsity

Understanding Prolog: Unification and Resolution, Slides of Advanced Computer Programming

Partial preview of the text

Download Understanding Prolog: Unification and Resolution and more Slides Advanced Computer Programming in PDF only on Docsity!

A Second Look At Prolog

Chapter Twenty Modern Programming Languages, 2nd ed. (^) Docsity.com 1

Outline

 Unification

 Three views of Prolog’s execution model

– Procedural

– Implementational

– Abstract

 The lighter side of Prolog

Chapter Twenty Modern Programming Languages, 2nd ed. (^) Docsity.com 2

Unification

 Two Prolog terms t 1 and t 2 unify if there is some

substitution σ (their unifier ) that makes them

identical: σ( t 1 ) = σ( t 2 )

  • a and b do not unify
  • f(X,b) and f(a,Y) unify: a unifier is { X → a , Y → b }
  • f(X,b) and g(X,b) do not unify
  • a(X,X,b) and a(b,X,X) unify: a unifier is { X → b }
  • a(X,X,b) and a(c,X,X) do not unify
  • a(X,f) and a(X,f) do unify: a unifier is {}

Chapter Twenty Modern Programming Languages, 2nd ed. (^) Docsity.com 4

Multiple Unifiers

 parent(X,Y) and parent(fred,Y) :

– one unifier is σ 1 = { X → fred }

– another is σ 2 = { X → fred , Y → mary }

 Prolog chooses unifiers like σ 1 that do just

enough substitution to unify, and no more

 That is, it chooses the MGU—the Most

General Unifier

Chapter Twenty Modern Programming Languages, 2nd ed. (^) Docsity.com 5

Unification For Everything

 Parameter passing

– reverse([1,2,3],X)

 Binding

– X=

 Data construction

– X=.(1,[2,3])

 Data selection

– [1,2,3]=.(X,Y)

Chapter Twenty Modern Programming Languages, 2nd ed. (^) Docsity.com 7

The Occurs Check

 Any variable X and term t unify with { X → t }:

– X and b unify: an MGU is { X → b }

– X and f(a,g(b,c)) unify: an MGU is

{ X → f(a,g(b,c)) }

– X and f(a,Y) unify: an MGU is { X → f(a,Y) }

 Unless X occurs in t:

– X and f(a,X) do not unify, in particular not by

{ X → f(a,X) }

Chapter Twenty Modern Programming Languages, 2nd ed. (^) Docsity.com 8

Outline

 Unification

 Three views of Prolog’s execution model

– Procedural

– Implementational

– Abstract

 The lighter side of Prolog

Chapter Twenty Modern Programming Languages, 2nd ed. (^) Docsity.com 10

A Procedural View

 One way to think of it: each clause is a

procedure for proving goals

– p :- q, r. – To prove a goal, first unify

the goal with p , then prove q , then prove r

– s. – To prove a goal, unify it with s

 A proof may involve “calls” to other

procedures

Chapter Twenty Modern Programming Languages, 2nd ed. (^) Docsity.com 11

Backtracking

 One complication: backtracking

 Prolog explores all possible targets of each

call, until it finds as many successes as the

caller requires or runs out of possibilities

 Consider the goal p here: it succeeds, but

only after backtracking

Chapter Twenty Modern Programming Languages, 2nd ed. 13

**1. p :- q, r.

  1. q :- s.
  2. q.
  3. r.
  4. s :- 0=1.**

Docsity.com

Substitution

 Another complication: substitution

 A hidden flow of information

Chapter Twenty Modern Programming Languages, 2nd ed. 14

σ 1 = MGU( p(f(Y)) , t ) is applied to all subsequent conditions in the clause

σ 2 = substitution developed by q to prove σ 1 ( q(Y) ), is applied to all subsequent conditions in the clause

σ 3 = substitution developed by r to prove σ 2 (σ 1 ( r(Y) ))

combined substitution is returned to caller term proved: σ 3 (σ 2 (σ 1 ( t )))

original goal term t

p(f(Y)) :- q(Y) , r(Y).

Docsity.com

Resolution

 The hardwired inference step

 A clause is represented as a list of terms (a

list of one term, if it is a fact)

 Resolution step applies one clause, once, to

make progress on a list of goal terms

Chapter Twenty Modern Programming Languages, 2nd ed. 16

function resolution ( clause , goals ):

let sub = the MGU of head( clause ) and head( goals )

return sub (tail( clause ) concatenated with tail( goals ))

Docsity.com

Resolution Example

Chapter Twenty Modern Programming Languages, 2nd ed. 17

function resolution ( clause , goals ):

let sub = the MGU of head( clause ) and head( goals )

return sub (tail( clause ) concatenated with tail( goals ))

Given this list of goal terms:

[p(X),s(X)]

And this rule to apply:

p(f(Y)) :- q(Y), r(Y).

The MGU of the heads is { X → f(Y) }, and we get:

resolution([p(f(Y)),q(Y),r(Y)], [p(X),s(X)]) = [q(Y),r(Y),s(f(Y))]

Docsity.com

solve tries each of the four clauses in turn

  • The first works, so it calls itself recursively on the result of the resolution step (not shown yet)
  • The other three do not work: heads do not unify with the first goal term

Chapter Twenty Modern Programming Languages, 2nd ed. 19

Program:

1. p(f(Y)) :- **q(Y),r(Y).

  1. q(g(Z)).
  2. q(h(Z)).
  3. r(h(a)).**

A partial trace for query p(X) : solve([p(X)])

1. solve([q(Y),r(Y)]) … 2. nothing 3. nothing 4. nothing

Docsity.com

Chapter Twenty Modern Programming Languages, 2nd ed. 20

Program:

1. p(f(Y)) :- **q(Y),r(Y).

  1. q(g(Z)).
  2. q(h(Z)).
  3. r(h(a)).**

A partial trace for query p(X) , expanded: solve([p(X)])

1. solve([q(Y),r(Y)]) 1. nothing 2. solve([r(g(Z))]) … 3. solve([r(h(Z))]) … 4. nothing 2. nothing 3. nothing 4. nothing

Docsity.com