Language Lab3-Visual Prolog

advertisement
CSC-430: Programming Languages
Language Lab #3: Visual Prolog
Project: Our project for Language Lab #3 is designed to introduce us to logic programming
using the PIE environment as supplied by Visual Prolog. The lab is in two parts.
Part I: Visit this web page http://www.generation5.org/content/2002/prg01.asp and attempt to
implement the system. Create a Prolog source file named: Lab3-PartI.pro. Issue several queries
similar to or as suggested on the web page in the Dialog window.
Part II: Visit this web page
http://www.bitwisemag.com/copy/programming/prolog/intro/firststeps.html and learn more
about Prolog programming. Having done so, create the following fact/rule base in a file named:
Lab3-PartII Original.pro.
/* build a tree. Adapted from "Programming Prolog", 2e, Clocksin & Mellish, Springer-Verlag, 1984.
pp. 61-63 and pp. 139-142. */
/* Mike Qualls */
aless(X, Y) :- str_atom(L, X), str_atom(M, Y), alessx(L, M).
alessx([], [_|_]).
alessx([X|_], [Y|_]) :- X < Y.
alessx([P|Q], [R|S]) :- P = R, alessx(Q, S).
lookup(H, w(H, G, _, _), G) :- !.
lookup(H, w(H1, _, Before, _), G) :aless(H, H1), lookup(H, Before, G).
lookup(H, w(H1, _, _, After), G) :not(aless(H, H1)), lookup(H, After, G).
Turning ‘trace’ on, examine the results of issuing the following goal in the Dialog window:
lookup(massinga, X, 858), lookup(braemar, X, 385), lookup(nettleweed, X, 579),
lookup(panorama, X, 158), lookup(braemar, X, Value).
The first four clauses instantiate the tree with four nodes, one for each of the contestants. The fifth
clause performs an actual search and lookup in the tree looking for the node whose name instantiates to
braemar and resulting in the instantiation of Value.
The purpose of the ‘lookup’ predicate is to build a tree of sorted nodes where each node will be named
‘w’ and will consist of four values:
w(N, W, L, R). where
 N is the name of a contestant
 W is the winnings of the contestant
 L is a left subtree consisting of nodes whose name is
alphabetically less than the name of the current node, and
 R is a right subtree consisting of nodes whose name is
alphabetically greater than the name of the current node.
The purpose of the ‘aless’ predicate is to compare the names of two contestants and return yes (or true)
if the name of the first is alphabetically less than the name of the second. As written, ‘aless’ attempts to
convert the name of an atom into a list of characters (or character codes) and then to compare
character lists character by character. And, it doesn’t work. It appears that the built-in predicates of
PIE are not sufficient.
1)
As requested, create the program (facts, rules) and issue the goal. Examine the trace. Explain
what type of tree is being built.
2) Modify ‘aless’ s.t. it works. Issue the same goal again and describe the resultant tree.
Deliverables:
Part I:
1) The file Lab3-PartI.pro.
2) A Word file (Lab3-PartI.doc) containing a screen shot of the Dialog window
after issuing several goals. (Use ALT-PrtScn and paste contents into a Word
document.)
Part II:
1) The file Lab3-PartII Original.pro.
2) A Word file (Lab3-Part II Original.doc) containing a screen shot of the Dialog
window. In the same document, explain the tree built and why it is or is not
sorted as desired into left and right subtrees.
3) The file Lab3-PartII Corrected.pro.
4) A Word file (Lab3-PartII Corrected.doc) containing screen shot of corrected
output and a discussion of that tree.
Due Date: Place your files (there should be six of them) in a Zip archive and put that file
(Lab3.zip) in the Doc Sharing view by the beginning of class on Tuesday, 10/21/08. Only place
one set of files there. (No need for all three team members to do the same thing.)
Demonstrations will be conducted as the first order of business at that class session, with the
team leader describing the work performed.
Download