CS 330 Fall, 2002
Program #1: Prolog
H. Hamburger & D. Richards
Due: October 16
Preparing: Study
chapter 7 of the text. Section 7.2
has some practical details. First
try just starting and stopping (see table below). Then try some of the built-in capabilities of Prolog like
the list conventions and the “=”
operator for matching; e.g., type this:
[X|Y] = [a,b,c]. Also try out the is operator to force evaluation of
arithmetic expressions, like this: X is
3+2.
The arithmetic operators work too, so 5>4.
succeeds.
Files: Before entering a Prolog session, create a file
(using any editor) that contains the facts and rules you wish to test. Start
with simple predicates.
Starting: At
the Unix prompt, type the Unix command prolog
to start interacting with the Quintus
Prolog interpreter on the GMU server.
To load your facts and/or rules from a file called file1 into the
session, type [file1]. ending
with a period. If you entered
Prolog while not in the directory containing the file, give the full path,
within single quotes:
[’pro/file1’].
You can load two files at once, like this:
[factfile, rulefile]. If you unintentionally hit the <return> key
without the period, just type a period and another <return>. After loading your file(s), type in
queries and you’ll get the answers.
Stopping: Before
you start anything, know how to stop it! To halt a Prolog session, use halt. which is a Prolog predicate
with no arguments, with the side-effect of exiting Prolog. To stop an unresponsive Prolog process
that may be in an infinite loop, use <control-c> a
.
Tracing: For
debugging, you can have the Prolog processor trace the steps by which it
reached its conclusions. This can
get pretty complex, though, so you may want to turn it off. To start the process type trace. and to turn it off, use notrace.
Scripts: script is a Unix command that starts
a script session. Everything you then type, plus the computer output, is copied
to the file typescript (or to
the file given as an argument, e.g., myoutfile)
when you exit the script session by typing <control-d>.
|
Starting ... |
and Stopping |
Prolog session: |
prolog |
halt. |
Prolog process: |
pred(arg1, arg2). |
<control-c> a |
Tracing: |
trace. |
notrace. |
Script session: |
script myoutfile
|
<control-d> |
Online help: To get Prolog help, type the special predicate
help. which tells you about
the special predicate manual(…). You can also search Google for Quintus Prolog.
Assignment: You will be creating a database of airline flight
schedules, along with rules for extracting useful relationships from that data.
(Real information is easy to get, if you wish, by Google searches for
“time zones” and “flight schedules.”) The basic predicate, hop, is for a nonstop flight (that
takes off, goes somewhere and lands).
Its 5 arguments are: a flight’s name (airline and number), the
airport it comes from, the one it goes to, its local departure time and its
local arrival time. For example,
if United Airlines flight 234 flies from LAX in Los Angeles to DTW near
Detroit, the Prolog fact might be written
hop(flight(ua,234),lax,dtw,time(9,30),time(18,10)).
Prolog constants
begin with a lowercase letter, so we use ua
for United Airlines and lax for
LAX. A flight name is a structure containing an airline and a number, like flight(ua,234); a direct flight has
consecutive hops with the same flight number. Each time of day is also a structure, like time(18,10), which means 6:10 p.m.,
on a 24-hour clock, from time(0,0) to time(23,59). The duration of the above flight is 5
hours and 40 minutes because of a 3-hour time zone difference.
In addition to hop, the only other predicate for
facts is city, used for
associating airports to their cities and time zones. The fact that LAX is in Los Angeles, in time zone 16 (16
hours later than London, which is defined to be 0) is represented as follows.
city(lax,los_angeles,16).
Write Prolog facts for hop and city, as well as rules for the following predicates.
airport_hop(A1,A2)
|
There is a hop from airport A1 to airport A2. |
city_hop(C1,C2) |
There is a hop from an airport for city C1 to an airport for city C2. |
two_hop(A1,A2) |
It is possible to get from airport A1 to airport A2 in two
hops. |
time_diff(T1,T2,T) |
T states how much later T2 is than T1. Each variable is a time structure. |
duration(A1,A2,T,F) |
You can go from airport A1 to airport A2 in time T on flight F. |
no_layover(A1,A2) |
Like two_hop, but with between 0 and 2 hours between hops. |
direct(A1,A2,F) |
You can go from airport A1 to A2, staying on flight F. |
path(A1,A2,L) |
One can go from airport A1 to another one, A2, via list L of distinct airports. (You’ll need to include other rules; e.g., member and different. Use path on the right side of the rule, but not as the first predicate there. Use specific airports as inputs. Test first with non-looping data.) |
Help: You may get help from anyone with Prolog, but do
the assignment yourself. This pertains to
choosing the facts, implementing the rules, and devising the tests. See the CS Department project policy at
http://cs.gmu.edu/honor-code.html
.
Hand
in:
a discussion of your facts, rules, and
queries, including a flight graph diagram showing your facts clearly. Provide a printout of a script of a
session in which you…