150 likes | 396 Views
Introduction to Prolog, cont’d. Lecturer: Xinming (Simon) Ou CIS 505: Programming Languages Fall 2010 Kansas State University. Example SLD resolution. ancestor(X,Y ) :- parent(X,Y ). ancestor(X,Y ) :- parent(X,Z ), ancestor(Z,Y ) . parent(bill,mary ). parent(mary,john ). .
E N D
Introduction to Prolog, cont’d Lecturer: Xinming (Simon) Ou CIS 505: Programming Languages Fall 2010 Kansas State University
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). • ?- parent(X,Y). • ?- parent(X,Z), ancestor(Z,Y). • X=bill • Y=mary • X=mary • Y=john • X=mary • Z=john • X=bill • Z=mary • ?- • Success ?- ancestor(john,Y). • ?- • Success ?- ancestor(mary,Y). • … • Failure ?- parent(mary,Y). ?- parent(mary,Z2), ancestor(Z2,Y). • Y=john • Z2=john ?- ancestor(john,Y). • ?- • Success • … • Failure
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.
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). …
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.
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.
SLG resolution example ancestor(X,Y) :- ancestor(Z,Y), parent(X,Z). ancestor(X,Y) :- parent(X,Y). parent(bill,mary). parent(mary,john). generator node new table created for ancestor(X,Y) • ancestor(X, Y). active node resolve ancestor(Z,Y) against the results in the table for ancestor(X,Y) • parent(X,Y). • ancestor(Z, Y), parent(X, Z). • Z=bill • Y=john • X=bill • Y=mary • X=mary • Y=john • Z=bill • Y=mary • Z=mary • Y=john • parent(X, bill). • • Success • • Success • Failure • parent(X, bill). • parent(X, mary). • Failure • • Success X=bill
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.
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]
Membership Function member(A, L) means A is a member of list L member(A, [A|As]). member(A, [B|Bs]) :- member(A, Bs).
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).
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.
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).