Slides. - People - Kansas State University

Download Report

Transcript Slides. - People - Kansas State University

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).
?- ancestor(X, Y).
parent(mary,john).
?- parent(X,Y).
X=bill
Y=mary
?Success
?- parent(X,Z), ancestor(Z,Y).
X=mary
Y=john
X=mary
Z=john
X=bill
Z=mary
??- ancestor(mary,Y).
Success
?- parent(mary,Y).
Y=john
?- ancestor(john,Y).
…
Failure
?- parent(mary,Z2), ancestor(Z2,Y).
Z2=john
?Success
?- ancestor(john,Y).
…
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).
 ancestor(X, Y).
parent(mary,john).
 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)
 ancestor(Z, Y), parent(X, Z).
Z=bill
Y=mary
 parent(X, bill).
Failure
Z=bill
Y=john
Z=mary
Y=john
generator node
new table created
for ancestor(X,Y)
 parent(X,Y).
X=bill
Y=mary
 parent(X, bill).

Failure
 parent(X, mary).
Success
X=bill

Success
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