CPSC352 -- Artificial Intelligence -- Fall 2005
Very Short Guide to Using PROLOG

Links to PROLOG Resources

  • GProlog Manual (PDF)
  • GProlog Manual (Postscript)
  • SWI Prolog (Windows)
  • Prolog Interpreter for Windows

    Overview of GNU PROLOG

    The version of PROLOG in the department's labs is GNU PROLOG.

    Logging In

    Log in to your unix account on any of the workstations in MCEC 136 (cobol, fortran, lisp, prolog, etc. ). If you prefer to work from your room, you can use putty to telnet using ssh (a secure shell) to one of these workstations. The telnet address is host.cs.trincoll.edu where host is the name of the workstation.

    Starting and Exiting Prolog

    It's a good idea to make a subdirectory in your account to store your prolog programs and to make that subdirectory your working directory. In the following examples the $ is the unix prompt and should not be typed. The %% means that the rest of the command line is a comment. The prolog prompt is | ?-. To start the prolog interpreter you type gprolog at the unix prompt. To exit prolog you type CTRL-D -- i.e., you hold down both the control key and D at the same time.
    $mkdir cs352      %% make a directory named cs352
    $cd cs352         %% make cs352 your working directory
    $gprolog          %% run the prolog interpreter
    
    GNU Prolog 1.3.0
    By Daniel Diaz
    Copyright (C) 1999-2007 Daniel Diaz
    | ?-             %% this is the prolog prompt
    | ?- ^D          %% CTRL-D exits Prolog
    

    Editing a PROLOG Program

    Prolog files should be named file.pl. They may be created using any text editor.

    If you are working from your dorm room, you can use any text editor on your Mac (e.g., SimpleText or BBEdit) or PC (WinEdit, Notes) and save the files on your desktop and then sftp the files to your unix account. Before running a PROLOG program, the program must be stored in your prolog subdirectory in your unix account.

    Running a PROLOG Program

    Prolog is an interactive environment. Commands are typed on a command line at the prolog prompt and they are immediately interpreted. If there is some kind of error, Prolog will switch into debug mode. To get out of debug mode and back to the Prolog prompt, type a for abort,

    Editing and testing a prolog program is primarily a matter of repeating the following steps:

    The following is a trace from an actual PROLOG session:
    | ?- ['family.pl'].             %% load the family.pl file
    {consulting /home2/ram/prolog/family.pl...}
    {/home2/ram/prolog/family.pl consulted, 133 msec 2064 bytes}
     
    yes
    | ?- male(ralph).                %% Q: is ralph a male?
    yes                              %% A: responds yes
    | ?- female(Y).                  %% Q: who is female?
    Y = choonglan ? ;                %% A: choonglan  Q: who else?
    Y = meisha ? ;                   %% A: meisha     Q: who else?
    Y = alicia ? ;                   %% A: alicia     Q: who else?
    no                               %% A: No one
    | ?- listing.                    %% list the program 
    
    daughter_of(A, B) :-
            parent_of(B, A),
            female(A).
     
    father_of(A, B) :-
            parent_of(A, B),
            male(A).
     
    parent_of(ralph, alicia).
    parent_of(ralph, meisha).
    parent_of(choonglan, alicia).
    parent_of(choonglan, meisha).
     
    female(choonglan).
    female(meisha).
    female(alicia).
     
    male(ralph).
     
    yes
    | -? consult('family.pl').          %% reload the program (after editing)
    
    {consulting /home2/ram/prolog/family.pl...}
    {/home2/ram/prolog/family.pl consulted, 117 msec 336 bytes}
    
    | -?  ^D                              %% CTRL-D exits prolog
    $                                     %% back to Unix
    

    How PROLOG Works: Queries, Facts, and Rules

    The prolog interpreter responds to queries, of which several examples are given in the above trace. Given a query, the interpreter attempts to unify the query with a fact in your program. If it succeeds, it answers yes and reports any variable bindings that it made in satisfying the query. If it fails, it answers no.

    For example, the query male(ralph) is an exact match for the male(ralph) fact in the family.pl program. The query female(Y) matches several facts. It matches female(alicia) if the binding Y=alicia is made. And so on.

    If the query matches the left hand side of a rule, then PROLOG will replace the query with the rule's right hand side and continue its search for matching facts. For example, given the query father_of(X,alicia), the interpreter finds the rule

       father_of(X,Y) :- parent_of(X,Y), male(X).
    
    and makes the binding alicia = Y. It then applies this binding to the right hand side of the rule getting:
       parent_of(X,alicia),male(X).
    
    The first conjunct in this predicate matches parent_of(ralph,alicia) if the binding X=ralph is made, and given this binding the second conjunct matches male(ralph). So the query succeeds, provided X is bound to ralph. So prolog's response to the query "Who is alicia's father?" is "ralph".