Prolog

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.

  • YAP Prolog Homepage
  • YAP User Manual
  • Online Guide to Prolog Programming

    YAP and Prolog Reference Material

    The best source of information for learning how to use YAP is the YAP User Manual. It contains information on YAP's built-in functions and much, much more. The YAP Prolog Homepage has information detailing different available versions and how to download YAP onto your personal computer. Here is a nice online guide to programming in Prolog.

    Starting and Exiting YAP Prolog

    YAP Prolog is a prolog compiler that works interactively. You can type in code and interactively see its output as it is run. To open YAP, type yap at the command prompt (you must be on a linux computer). You should see something like this come up :

    % yap
    [ Restoring file /s/Yap-4.4.2/i386_rh72/lib/Yap/startup ]
    [ YAP version Yap-4.4.2 ]
       ?-


    The '?-' is the YAP prompt for query mode. There are two modes in Prolog, definition mode, and query mode. To enter definitions interactively at the command prompt you must first type ?- [user]. The prompt will change to |. When you have finished typing in rules and definitions, type Ctrl-d and you will return to query mode. See the next section for information about storing definitions in a file and loading them. There is some yap code below.
    ?- [user].
    | % this is a comment
    | man(adam).
    | man(cain).
    | woman(eve).
    | parent(eve).
    | parent(adam,cain).
    | parent(eve,cain).
    | father(F,C):-man(F),parent(F,C). % at this point I typed Ctrl-d
    | yes
    ?- father(adam,cain).
    yes
    ?- father(eve,cain).
    no
    ?-

    To exit, type Ctrl-d.

    Reading Prolog Definitions from a File

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

    Miscellaneous Information About YAP

    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 to find sections on control, term, comparison, and arithmetic predicates and much more.

    Using YAP's Debugger

    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.

    The Edit, Compile, Debug Cycle in Prolog

    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.

    Running YAP Prolog at Home

    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.