We shall be using The YAP Prolog System in our course work. It is one of the fastest and most widely-used Prolog compilers. YAP stands for Yet Another Prolog.
In our department YAP Version 4.4.2 is installed in /s/std/bin. You can run it by just typing yap on any of the department's linux machines.
It is best to type prolog fact and rule definitions into a file and
then load them rather than type them in interactively. Use the
interactive mode for typing queries to test your rules.
For this assignment you are required to put your fact and rule
definitions in a file so that they can be loaded.
The .pl extension is used to denote a prolog
file, but need not be used. Files may only be loaded when
yap is in query mode (i.e. when the prompt looks like: ?-
To load the definitions found in file family.pl
or family you should type:
?- [family].
If your filename
has a .pl extension, it should not be included in the
load command. If you have modified family.pl and wish to
refresh its definitions, type the command: [-family].
To bring up a short help page within YAP, type Ctrl-c and then h Enter. To include comments in your code, include the % character and the text to the right of it will be commented out. Also note that YAP includes the not command in addition to \+. The two commands are equivalent. YAP contains numerous built in predicates and the user manual has a large section on its built-in predicates. See the user manual's Built-In Predicates section of the Table of Contents to find sections on control, term, comparison, and arithmetic predicates and much more.
Take a look at the YAP user manual's section on its
debugger.
The most useful calls are the following:
debug.
nodebug.
trace.
notrace.
spy predicate-name.
nospy predicate-name.
leash ([mode]).
Calls to trace and spy launch the debugger, as does a
call to debug. The trace command will cause all
calls to be traced, whereas spy predicate-name will only invoke
the trace mechanism when it sees the predicate predicate-name.
In the following example, rshift shifts the last element of a list to the
front of the list:
?- [user].
| rshift([X],[X]).
| rshift([Y|Tail1],[X,Y|Tail2]):- rshift(Tail1,[X|Tail2]).
| yes % this line appears because I hit Ctrl-d
?- rshift([1,2,3],X). % executes a simple query with no tracing
X = [3,1,2] ?
yes
?- trace. % turns on tracing
[ Trace mode on. ]
yes
[trace]
?- rshift([1,2,3],X). % Now re-execute the query with tracing
(1) call:rshift([1,2,3],_178) ? % yap pauses on lines ending in '?' until you hit enter, if you hit 'n' and then enter, you exit debug mode
(2) call:rshift([2,3],[_252|_255]) ? % all numbers starting with an underscore are unbounded variables created by yap for use during execution
(3) call:rshift([3],[_252|_320]) ?
(3) exit:rshift([3],[3]) ?
(2) exit:rshift([2,3],[3,2]) ?
(1) exit:rshift([1,2,3],[3,1,2]) ?
X = [3,1,2] ?
yes
[trace]
?- notrace.
(1) call:notrace ? % Press enter.
[ Debug mode off. ]
yes
?-
Using the trace command can be cumbersome because execution stops
after each call to a predicate and you must press Enter to
resume execution.
The leash command is designed to alleviate this problem.
If tracing is enabled, the call:
leash([off]). will cause predicate calls to be traced
without a pause in execution after each line. The default is
leash([full]). See the debugging section of the
user manual
for more details on leash and other debugging tools.
A convenient way to develop Prolog programs is to open two windows, one for an editor and one to run yap. You edit prolog definitions in the edit window, using your favorite text editor. To test your definitions, go to the yap window. Enter the command '[filename].' to load and compile the file you've just edited, and then type in queries to test your program. If you want to refresh the definitions in the file you've created, use '[-filename].', and do not include the .pl extension.
If you want to run Prolog on a home computer, download it from here. The most recent version available is 4.4.5, and the cs lab runs version 4.4.2. For our purposes there is not much difference between the two versions, but make sure that your code runs on the CS lab's version of YAP before you hand the assignment in.