1 / 13

Introduction to Prolog, cont’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 ). .

marrim
Download Presentation

Introduction to Prolog, cont’d

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Prolog, cont’d Lecturer: Xinming (Simon) Ou CIS 505: Programming Languages Fall 2010 Kansas State University

  2. 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

  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.

  4. 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). …

  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.

  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/ ). • Provides full ISO Prolog compatibility.

  7. 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

  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.

  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]

  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).

  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).

  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.

  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).

More Related