\documentstyle[a4,alltt]{article}
 
 
\topmargin 0.9 in         %   Nominal distance from top of paper
                          %   to top of page - 1in
\textheight 7.3 in        %   Height of body of page
\textwidth  5.25 in       %   Width of text area
\oddsidemargin  0.5 in    %   (Left margin on odd-numbered pages-1in)
\evensidemargin 0.5 in    %   (Left margin on even-numbered pages-1in)
\marginparwidth 1 in      %   Width of marginal notes.
\parindent 0.25 in        %   Amount of indent on first line of para
 
\begin{document}
 
\newcommand{\idx}[1]{}
 
\title{Supplement 8}
\author{Jocelyn Ireson-Paine}
\maketitle
 
 
\section{Introduction}
 
  These exercises give some more practice in using Prolog as a control
language. After that, it's time to finish with the Tutor and move on to
Prolog proper, and the screen editor. The final section says goodbye to
the Tutor and shows you what to do next.
 
 
\section{The caves world}
 
\idx{caves world|(ee}
 
Remember that this is described by knowledge base DD\idx{DD|ee}, which contains
three kinds of fact:
\begin{alltt}
    is_a( a, cave )\idx{is\_a/2@\verb#is_a/2#|ee}
    meets( b, a ).\idx{meets/2@\verb#meets/2#|ee}
    is_at( dragon, b ).\idx{is\_at/2@\verb#is_at/2#|ee}
\end{alltt}
Supplement 2 described what these mean.
 
Assume that the knowledge base also contains a clause for each player
giving their score: \verb#score(fred,500)#\idx{score/2@\verb#score/2#|ee}. In Supplement 7, we saw how this
might be updated as a player kills more of his enemies.
 
Write a predicate which has  no arguments called \verb#check#\idx{check/0@\verb#check/0#|ee}. When \verb#check#
is  called, it  examines the  score: if  it is  greater than  2000, it
writes a message saying ``You have won'', otherwise it does nothing.
 
When you have done that, enter some \verb#is_worth#\idx{is\_worth/2@\verb#is_worth/2#|ee} facts defining the
values of the enemies, as suggested in Supplement 5:
\verb#is_worth(dragon,500)# and so on.
 
Then write a predicate called \verb#kill(P,A)#\idx{kill/2@\verb#kill/2#|(ee}.
Here P is a player (e.g.
\verb#fred#) and A is an animal (e.g. \verb#dragon#). What \verb#kill# should do is to
assume that P has just killed A, and to remove the old \verb#score#\idx{score/2@\verb#score/2#|ee} fact,
replacing it by a new one in which the worth of A has been added to P's
score. You can do this with \verb#gain#\idx{gain/2@\verb#gain/2#|ee}, as described in Supplement 7.
Because A is now dead, \verb#kill# should also replace the old \verb#worth# fact
for A by one giving it a worth of 0.
\idx{kill/2@\verb#kill/2#|)ee}
 
You can build on this and the other cave world exercises to write a
complete Adventure game. Its players could be human, or they could be
automatic like the traders in Traveller\idx{Traveller|ee}. In the former case, the program
would ask what action the player wanted to take. This could involve some
natural language analysis: it would be nice if a player could type {\tt
Throw firebomb at Wumpus}\idx{Wumpus|ee}, {\tt Eat packed lunch} or {\tt Run South-West
to the blasted tree}. In the latter case, the behaviour of the players
would be dictated by predicates, defined perhaps like \verb#will_be#\idx{will\_be/2@\verb#will_be/2#|ee} in
Supplement 2, but more complicated. The file DDMOVE\idx{DDMOVE|ee}, which you can
restore, is just a start in this direction.
 
\idx{caves world|)ee}
 
 
\section{Links}
 
Please restore knowledge base LINKS2\idx{LINKS2|ee}. This is like LINKS\idx{LINKS|ee}, but instead of
describing just one straight chain, it describes a tree-like network.
As before, you have clauses for \verb#goes_to(X,Y)#\idx{goes\_to/2@\verb#goes_to/2#|ee} if X leads to Y, but now
X may have more than one successor.
 
You also have clauses for \verb#weight(X,W)#\idx{weight/2@\verb#weight/2#|ee}, where X is a square and W is a
numerical weight associated with it. The idea is that squares with the
lowest weights define a preferred route. Your job is to write a
predicate like \verb#look#\idx{look/1@\verb#look/1#|ee}, but it has to take the lowest-weight successor at
each square.
 
This is not as hard as it sounds. First, assume you have a predicate
\verb#best(X,Y)#\idx{best/2@\verb#best/2#|ee} which, given square X, returns the lowest-weight successor
in Y. If you have this, you can easily write \verb#look#, just by copying the
code in the lesson.
 
So how do you write \verb#best#? Look back at the end of Supplement 6. This
should give you enough information.
 
 
\subsection{Search}
 
\idx{search methods|(ee}
 
The previous section involves a kind of search known as {\it
hill-climbing}\idx{search methods!hill-climbing|ee}, where if you have several choices at any point, you
calculate a numerical measure (a {\it weight}) for each, pick the best,
and explore that, proceeding in the same way for all subsequent choices.
In LINKS2\idx{LINKS2|ee}, the weights were pre-calculated by me, and they have no
obvious relevance to anything else. However, you can apply the same
method to problems where the weights are not known in advance.
 
Consider the problem of synthesising\idx{organic synthesis|ee} some
new wonder-compound, say interferon. There is usually more than one
reaction
that will produce a given compound. The chemical
literature is full of lists of reactions, the circumstances under which
they can be applied, and the chemical needs and costs of each. So --- we
start at interferon. We make a list of all reactions that can plausibly
be used. Think of each as a path leading off from interferon. From
general principles, we can then assign a weight to each. Some, such as
the one where the reactants are so unstable that their molecules need to
be isolated inside individual micro black holes,
and the one that requires you to boil the reactants in a
50:50 hydrofluoric acid/liquid radium mixture inside a 10 cubic meter
diamond reaction vessel, will be rather costly --- i.e. high weight.
Others, like the one where you distil the reactants from a kilo of
fermented garden slugs, will be less so. Having assigned the weights,
pick the lowest. That gives you the cheapest reaction. You then proceed
in the same way with its starting materials, and so on. A similar
procedure is in fact used in commercial programs for synthesis planning.
 
My first attempt at the
\verb#route#\idx{route/3@\verb#route/3#|ee}\idx{route-finding|ee} predicate of
Supplement 5 worked in this way. At each square, it looked at all those
leading from it. It then worked out the distance of each to the
destination, picked the nearest, and then moved on to it as the next
square. Because of bends in the streets, this did not always give the
shortest path, but it was a good first approximation.
 
\idx{search methods|)ee}
 
 
\section{Lists again}
 
\idx{lists|(ee}
For our final example, I will look at lists again, and show how they
can be combined with the other things you have learnt. During the
17th Century, ``canting academies'', or books of criminal slang were
popular. One such, available in the Bodleian Library, and
written by Oxford author Richard Head --- best known for his
debased and much-banned autobiographical {\it The English Rogue}
--- is entitled just that: {\it The Canting Academy}. It contains a
number of entries which will tell you for example that the slang term
for a sheep was ``bleating cheat'', and for prison, ``queer ken''.
 
I have copied part of {\it The Canting Academy} into the file
HEAD\idx{HEAD|ee}, which defines the predicate
\verb#dictionary(Normal,Slang)#\idx{dictionary/2@\verb#dictionary/2#|ee}. Now the
obvious way to write such a predicate is to use atoms: for example,
if we were converting between English and French, and ignoring niceties
like gender:
\begin{verbatim}
    dictionary( chat, cat ).
    dictionary( chien, dog ).
\end{verbatim}
However, because many of Head's entries consist of more than one word,
it is better to use lists\idx{atoms!lists better than|ee}, which are
the ideal way to store variable-length sequences.
 
To see how these are used, have a look at, and experiment with, the
files HEAD\idx{HEAD|ee} and READER\idx{READER|ee}. These contain the
dictionary, together with a simple program that reads normal words or
phrases from the terminal and looks up the slang equivalent. These
combine all the features of Prolog you have seen so far, including
lists, command predicates, logic predicates, input, and output. Unlike
in Lesson 7, I have not used \verb#read#\idx{read/1@\verb#read/1#|ee} and
\verb#write#\idx{write/1@\verb#write/1#|ee} for input and output: as I said
then, these are not very convenient. Instead, I have written my own
input-output predicates. By looking at the program, and the predicate
names, see whether you can work out which these are, and how they are
used.
\idx{lists|)ee}
 
%sann
%{\it This is another Alpha-sting\idx{alpha-sting} exercise for the
%quick student. The program can be adapted to a number of other purposes;
%by putting appropriate pattern templates into the dictionary, it could
%for example form the basis of an Eliza, as described in Chapter 3
%of~\cite{scott}.
%}
 
 
\section{Epilogue}
 
\idx{Prolog!without Tutor|(ee}
\idx{restrictions!of Tutor|(ee}
This is the last you will see of the Logic Programming Tutor. Although
it is suitable for small programs, and for the kinds of experiment you
have been doing to discover how Prolog works, it is now time to move to
something more professional. The Tutor is not intended for editing big
programs. It does not provide a way to change individual characters in a
fact; it imposes its own layout on your facts; and it does not allow you
to enter comments\idx{comments|ee}. These are vital in any program to
explain what it does, what the atom\idx{atoms!must be documented|ee}
names mean, what the arguments\idx{arguments!must be documented|ee} to
each predicate do, and so on. Furthermore, the Tutor occupies quite a
lot of memory --- memory which you will find your own programs needing
as they grow bigger.
\idx{restrictions!of Tutor|)ee}
 
There are no differences between the Prolog language itself and what you
have used with the Tutor in Prolog
mode\idx{Prolog mode!differences from naked Prolog|ee}.
However, there are differences
in how you ask questions and change the knowledge base; also, you must
invoke Prolog in a different way.
 
In the Tutor, you ended a question\idx{questions!in naked Prolog|(ee} with a question
mark, and a fact with a dot. In Prolog proper, you end a question with a
dot. Try this when you have read up to the end of the current section.
 
To invoke Prolog, type
\begin{verbatim}
    prolog
\end{verbatim}
(in either upper or  lower case) when you see a dollar  prompt ---
i.e. in
the same context that you used to type \verb#logic lesson8#.
 
You will see a line or two of messages about ``Sussex Poplog'',
and then a
question mark and minus, like this \verb#?-#.
 
Prolog is now ready for you to ask it questions. There are not many you
can try, because there will be no facts pre-stored in the database\idx{database!in naked Prolog|ee}.
But you can try using the built-in predicates\idx{predicates!built-in!in naked Prolog|ee}. Try the questions below,
when you see the \verb#?-# prompt. You must type a dot and RETURN at the end.
Note that, unlike the Tutor, pure Prolog will not display three dots to
remind you that you have forgotten
to end your question in a dot. Instead,
it will just sit and wait.
 
Here are the questions. Try them now:
\begin{verbatim}
    write(hello).
    X is 5+6.
    2 > 3.
\end{verbatim}
 
\idx{questions!in naked Prolog|)ee}
 
 
\subsection{Inside Prolog}
 
You are now  in Prolog, and you  have asked three questions.  How do you
get out? There are two ways. The neater is to call the special predicate
\verb#halt#\idx{halt/0@\verb#halt/0#|ee}. This is, like \verb#write#, a {\it non-logical} predicate --- what's
important is  its {\it side-effect}\idx{side-effects|ee}. This side-effect  is about as  drastic as
you can get: it exits from Prolog, so you'll see a dollar prompt.
 
Try calling \verb#halt# when you get to the next section. Again, don't
forget the dot and RETURN.
 
 
\subsection{Back in the operating system}
 
\idx{operating system|(ee}
 
You are now out of Prolog, and talking to the operating system,
known as VMS on the VAX. This is the program which responds with a
dollar prompt.
 
Above, I said there were two ways to exit from Prolog. What is the other
one? Answer --- to type what we  call a CONTROL-Y signal. You type this by
pressing  the key  marked CTRL,  and then  pressing the  letter Y.
Make sure you press them both at the same time. Then
release both keys.
 
Try this at the next row of minuses. Invoke Prolog by typing \verb#prolog#
when you see the dollar prompt. Wait until you see Prolog's \verb#?-# and
then ask some random question. Then type CONTROL-Y. The word
INTERRUPT\idx{interrupts!in naked Prolog|ee}
will appear, and you will then see a dollar prompt. Again, you will be
talking to VMS.
 
You can in fact interrupt\idx{interrupts!in naked Prolog|ee} almost
any program with CONTROL-Y, including one which is running out of
control.
 
You are now in VMS again. When you get to the end of this section, I
would like you to try logging out, logging back in, and starting up
Prolog. Read to the end of this section first.
 
To log out, type \verb#logout# to VMS (when you see the \verb#$# prompt).
 
Ask the demonstrator how to log in (I have to say this to you, because
the method changes from year to year, and sometimes from week to week
--- if I wrote instructions here, they might soon go out of date).
 
When you've logged in, give your identification as usual, and then
invoke Prolog.
 
You are now talking to Prolog again.
 
\idx{operating system|)ee}
 
 
\subsection{Entering facts}
 
How can you get facts\idx{facts!in naked Prolog|(ee} into Prolog's
database? As you have found, statements which end with a dot are
questions\idx{questions!in naked Prolog|ee}, not facts to be added.
There is no equivalent to the Tutor's number-editor. What we do is to
use something called {\it VED}. With it, you can examine and change
files stored on magnetic disc. In particular, you can build Prolog
program files with VED, and then tell it to load them into Prolog's
database. If you want to change them later, you can do that too. You can
also read documentation files with it, and we have many of these on both
A.I. and Prolog, some written by me and some by Sussex.
 
VED is an example of an {\it editor}\idx{facts!editing!in naked
Prolog|ee} --- a program for changing and making files. This particular
editor is a {\it screen editor}\idx{screen editor!in naked Prolog|ee}
--- it displays
part of your file on the screen. You can move around the screen by
pressing certain keys, and add and delete characters, remove lines, etc.
If you have already used a wordprocessor (or the Tutor's screen editor),
you have already used a screen editor, though the instructions you give
VED will not be the same as for other editors.
 
\idx{facts!in naked Prolog|)ee}
 
%sann
%{\it This last section --- converting from the Tutor to naked Prolog ---
%is necessary to show how the two differ. It is of course, very
%machine-dependent, so I have stopped it at this point; at Oxford, it
%carries on with Poplog-dependent demonstrations of using VED to read and
%edit files.
%}
\idx{Prolog!without Tutor|)ee}
 
 
\end{document}
