Slides. - People - Kansas State University

advertisement

Introduction to Prolog, cont’d

Lecturer: Xinming (Simon) Ou

CIS 505: Programming Languages

Fall 2010

Kansas State University

1

Example SLD resolution ancestor(X,Y) :- parent(X,Y). ancestor(X,Y) :- parent(X,Z), ancestor(Z,Y).

parent(bill,mary). parent(mary,john).

?- ancestor(X, Y).

X=bill

Y=mary

?-

Success

?- parent(X,Y).

X=mary

Y=john

?- parent(X,Z), ancestor(Z,Y).

X=bill

Z=mary

X=mary

Z=john

?-

Success

?- ancestor(mary,Y).

?- ancestor(john,Y).

Failure

?- parent(mary,Y).

Y=john

?-

Success

?- parent(mary,Z2), ancestor(Z2,Y).

?- ancestor(john,Y).

Z2=john

Failure

2

Logic deduction as a program

• The advantage of Prolog is that it has both a logic meaning, and an execution semantics

– Ideally you do not need to think about the SLD resolution process when writing Prolog code

– A Prolog program is simply a collection of logical statements. A query is simply asking whether a fact can be derived as a logical consequence of the statements.

• However…

– When the result does not match your expectation, knowing the SLD resolution process will help in debugging.

– Moreover, Prolog is not always declarative, which we will see in this lecture.

3

Problem of SLD resolution ancestor(X,Y) :ancestor(Z,Y) , parent(X,Z).

ancestor(X,Y) :- parent(X,Y).

parent(bill,mary). parent(mary,john).

 ancestor(X, Y).

 ancestor(Z, Y), parent(X, Z).

 ancestor(Z1, Y), parent(Z, Z1), parent(X, Z).

 ancestor(Z2, Y), parent(Z1, Z2), parent(Z, Z1), parent(X, Z).

4

Problem of SLD resolution

• Termination of cyclic Prolog programs not only depends on logical semantics, but also the order of the clauses and subgoals.

– If Prolog is a declarative language, then order should not matter.

5

SLG Resolution

• Goal-oriented evaluation

• Predicates can be “tabled”

– A table stores the evaluation results of a goal.

– The results can be re-used later, i.e. dynamic programming.

– Entering an active table indicates a cycle.

– Fixpoint operation is taken at such tables.

• The XSB system implements SLG resolution

– Developed by Stony Brook ( http://xsb.sourceforge.net/ ).

– Provides full ISO Prolog compatibility.

6

SLG resolution example ancestor(X,Y) :ancestor(Z,Y) , parent(X,Z).

ancestor(X,Y) :- parent(X,Y).

parent(bill,mary). parent(mary,john).

 ancestor(X, Y). active node resolve ancestor(Z,Y) against the results in the table for ancestor(X,Y) generator node new table created for ancestor(X,Y)

 ancestor(Z, Y), parent(X, Z).

Z=bill

Y=mary parent(X, bill).

Z=bill

Y=john

Z=mary

Y=john  parent(X, bill).

Failure parent(X, mary).

Failure X=bill

Success

X=bill

Y=mary

Success

 parent(X,Y).

X=mary

Y=john

Success

7

Prolog as a programming language

• The capability to query with variables enables us to compute results. Example:

?- ancestor(X, john)

This query calculates all of john’s parents.

8

Data structures in Prolog

• List

– e.g.: [1,a,2,3,’hello world’]

– Empty list: []

– Cons operation: [A|As], e.g. [1|[2,3,4]] = [1,2,3,4]

9

Membership Function member(A, L) means A is a member of list L member(A, [A|As]).

member(A, [B|Bs]) :member(A, Bs).

10

Append Function append(L1, L2, L) appends two lists L1 and L2 to create the result L append([], L, L).

append([X|Xs], L, [X|R]) :append(Xs, L, R).

11

Calculate the sum of the integers in a list sum(L, Sum) returns in Sum the sum of the elements in L sum([], 0).

sum([A|As], Sum) :sum(As, S1),

Sum is S1+A.

12

Find the maximum number in a list max(L, Max) returns the maximum number in L max(L, Max) :max(L, 0, Max).

max([], Current, Current).

max([A|As], Current, Max) :-

A<Current, max(As, Current, Max).

max([A|As], Current, Max) :-

A>=Current, max(As, A, Max).

13

Download