$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
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.
Editing and testing a prolog program is primarily a matter of repeating the following steps:
| ?- ['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
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".