Download presentation
Presentation is loading. Please wait.
Published byTeresa Goodgion Modified over 10 years ago
1
Introduction to Prolog, cont’d Lecturer: Xinming (Simon) Ou CIS 505: Programming Languages Fall 2010 Kansas State University 1
2
Z2=john X=mary Y=john X=bill Y=mary Example SLD resolution ancestor(X,Y) :- parent(X,Y). ancestor(X,Y) :- parent(X,Z), ancestor(Z,Y). parent(bill,mary). parent(mary,john). ?- parent(X,Y). ?- Success ?- Success ?- parent(X,Z), ancestor(Z,Y). ?- ancestor(X, Y). X=bill Z=mary ?- ancestor(mary,Y). ?- parent(mary,Y). ?- Success ?- parent(mary,Z2), ancestor(Z2,Y). … Failure … Failure ?- ancestor(john,Y). X=mary Z=john ?- ancestor(john,Y). 2
3
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
4
Problem of SLD resolution ancestor(X, Y). ancestor(X,Y) :- ancestor(Z,Y), parent(X,Z). ancestor(X,Y) :- parent(X,Y). parent(bill,mary). 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
5
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
6
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/ ). http://xsb.sourceforge.net/ – Provides full ISO Prolog compatibility. 6
7
Z=bill Y=mary SLG resolution example ancestor(X, Y). ancestor(X,Y) :- ancestor(Z,Y), parent(X,Z). ancestor(X,Y) :- parent(X,Y). parent(bill,mary). parent(mary,john). ancestor(Z, Y), parent(X, Z). 7 generator node new table created for ancestor(X,Y) active node resolve ancestor(Z,Y) against the results in the table for ancestor(X,Y) parent(X, bill). parent(X,Y). X=mary Y=john X=bill Y=mary Success Success Failure Z=mary Y=john parent(X, mary). X=bill Success Z=bill Y=john parent(X, bill). Failure
8
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
9
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
10
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
11
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
12
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
13
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.