You first need to write a prolog program. Cut and paste the following into a file called test.pl (your programs must end in .pl):
% Percent signs comment out the rest of the line % parent(X,Y) = "X is the parent of Y". % this prolog program has no rules (clauses with bodies) only facts parent(bob, joe). %fact - all prolog predicates end in a period parent(bob, tim). %fact parent(sue, joe). %fact parent(joe, sally). %fact
Note that you leave off the .pl from the end.
Note also that you must end each line with a period.
Note also that there may be warnings about singleton variables when you
load your files -- which you
can ignore.
If you forget the period, you will be given a prompt that is a vertical line. This is just a continuation of the previous line. Type a period (.) to end it. For example:
?- parent(bob,X) %oops forgot the period | . % | means I'm waiting for more of the previous line % I typed a period here X = joe ; % first answer. I typed a semicolon here % (which says, "look for more answers") X = tim ; % second answer. I typed a semicolon here % (which says, "look for more answers") No % no more answers found ?- %ready for next query ?- parent(bob,X). %same query again X = joe %this time I don't type ; instead I press return % the system stops looking and returns true (Yes) Yes ?- %ready for next query
%getsecond is a predicate that takes a list and returns the second element of %the list passed in. The second parameter should be a variable - which prolog %will construct to be the first element of the list passed in. getsecond([H|T],Y) :- gethead(T,Y). gethead([H|T],H). % no need for a body, simply assign H to the return value
?- halt.to exit prolog.
?- [fname].
[ckrintz@ella ~]$ pl
Welcome to SWI-Prolog (Multi-threaded, Version 5.2.13)
Copyright (c) 1990-2003 University of Amsterdam.
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.
For help, use ?- help(Topic). or ?- apropos(Word).
?- [tmp2].
Warning: (/cs/faculty/ckrintz/tmp2.pl:5):
Singleton variables: [X]
% tmp2 compiled 0.00 sec, 696 bytes
?- trace.
Yes
[trace] ?- len([1,2],X).
Call: (7) len([1, 2], _G290) ? creep %orig call
Call: (8) len([2], _L206) ? creep %first recursive call
Call: (9) len([], _L225) ? creep %second recursive call
Exit: (9) len([], 0) ? creep %base case
^ Call: (9) _L206 is 0+1 ? creep %second recursive call using return from base case
^ Exit: (9) 1 is 0+1 ? creep
Exit: (8) len([2], 1) ? creep %first recursive call using return from second recurs. call
^ Call: (8) _G290 is 1+1 ? creep
^ Exit: (8) 2 is 1+1 ? creep
Exit: (7) len([1, 2], 2) ? creep %orig call
X = 2 ;
No