\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 5}
\author{Jocelyn Ireson-Paine}
\maketitle
 
 
\section{Introduction}
 
\idx{comparisons|(ee}
 
This supplement gives you some practice with comparisons. It then moves
on to a more advanced section (which you may want to skip if you start
having difficulties) which talks rather more about how Prolog is
executed\idx{Prolog execution|ee} and how it differs from logic.
 
 
\section{The caves world}
 
\idx{caves world|(ee}
 
The first set of exercises continues with the world of caves from
Supplement 2. Suppose that, as in many computer Adventure games, the
world is populated by vicious animals, all inimical to human life. Each
animal has a worth:
\begin{alltt}
    is_worth(dragon,500).       is_worth(gryphon,200).\idx{is\_worth/2@\verb#is_worth/2#|ee}
    is_worth(lion,50).          is_worth(vole,0.00172).
\end{alltt}
 
There are weapons, each with its own destructive power:
\begin{alltt}
    has_power( scud_missile, 10000 ).   has_power( supergun, 5000 ).\idx{has\_power/2@\verb#has_power/2#|ee}
    has_power( gun, 250 ).              has_power( grenade, 600 ).
    has_power( water_pistol, 0.003 ).
\end{alltt}
 
We say, in a way typical of such games, that a weapon W can
kill an animal A
if W has more power than A is worth. Express this as
a predicate \verb#can_kill(W,A)#\idx{can\_kill/2@\verb#can_kill/2#|ee} which is true if weapon W can kill animal
A.
 
Assume also that the player can pick up and use weapons. Let the fact
\begin{alltt}
    holds( fred, gun )\idx{holds/2@\verb#holds/2#|ee}
\end{alltt}
mean that Fred is holding a gun: in general, let \verb#holds(P,W)# be true if
player P is holding weapon W. Can you now define a predicate
\verb#scorns(P,A)#\idx{scorns/2@\verb#scorns/2#|ee} which is true if player P holds a strong enough weapon to
kill animal A?
 
These are all applications of comparisons and ``and''.
 
\idx{caves world|)ee}
 
%sann
%{\it I have also used the knowledge bases in SAXON.PL\idx{SAXON|ee} and STUDENTS.PL\idx{STUDENTS|ee}
%for examples of comparisons
%and ``and''\idx{and@\verb#and#}. Sample questions are included
%in comments at the top of each file.}
 
 
\section{Sisters}
 
In Lesson 2, I asked you to  write a fact defining sisterhood. Can you
repair that fact by using \verb#\=#\idx{\\=/2@\verb#\=/2#|ee},  so that it prevents someone being their
own sister?
 
 
\section{Traveller}
 
\idx{Traveller|(ee}
 
\subsection{Improving the coal trader}
 
At the end of Supplement 4, I asked you to write a version of \verb#act#\idx{act/4@\verb#act/4#|ee} that
would drive a coal trader, but not to worry about fuel. Can you add a
clause that will buy fuel if he has fewer than 10 units remaining?
 
You will find one possible answer in COLLIER2\idx{COLLIER2|ee}. The comparison operators
allow you to program a test on the amount of fuel, but not to calculate
the amount to buy as a function of other quantities.
So thus far, you will have to write the amount in as a
constant number.
The next lesson deals with arithmetic\idx{arithmetic|ee}, and shows you how to do
such
calculations.
 
\idx{comparisons|)ee}
\idx{Traveller|)ee}
 
 
\subsection{More departures from logic}
 
\idx{facts!conditional!mutually exclusive|(ee}
At the end of the skiing example\idx{ski advisor|ee}
in Supplement 1, I talked about the
importance of writing mutually exclusive
rules\idx{facts!conditional|ee}
and showed two that
aren't:
\begin{alltt}
    has_fitness( Skier, poor ) :-\idx{has\_fitness/2@\verb#has_fitness/2#|ee}
        max_pressups( Skier, P ),\idx{max\_pressups/2@\verb#max_pressups/2#|ee}
        P < 15.
 
    has_fitness( Skier, good ) :-
        max_pressups( Skier, P ),
        P >= 10.
\end{alltt}
 
The problem with non mutually-exclusive rules is that they are logically
inconsistent, since they state that two inconsistent things are both
true. Logically, if the skier above could do a maximum of 13 pressups,
he would have to be of both poor and good fitness!
 
Similarly, in COLLIER\idx{COLLIER|ee},
\begin{alltt}
    act( collier, buy, coal, 20 ) :-\idx{act/4@\verb#act/4#|ee}
        at_seller( collier, coal, Price ),\idx{at\_seller/3@\verb#at_seller/3#|ee}
        carries( collier, coal, 0 ).\idx{carries/3@\verb#carries/3#|ee}
 
    act( collier, sell, coal, 20 ) :-
        at_buyer( collier, coal, Price ),\idx{at\_buyer/3@\verb#at_buyer/3#|ee}
        carries( collier, coal, 1000 ).
 
    act( collier, move, M, dummy ) :-
        at( collier, N ),\idx{at/2@\verb#at/2#|ee}
        clockwise( N, M ).\idx{clockwise/2@\verb#clockwise/2#|ee}
\end{alltt}
the third rule, taken logically,
states that ``For all N, if the collier is at N, his act
must be a move to the next clockwise square M''. This is inconsistent
with the preceding rules which say that sometimes the act must be to buy
or sell.
 
So why does this work? The reason is that, as mentioned at the end of
Lesson 5, Prolog works through the clauses\idx{facts!order of!and execution|ee} from the top down, and
chooses the first one that succeeds. It may later
{\it backtrack}\idx{backtracking!and non-exclusive conditions|(ee} and
look for another solution, as it did with \verb#loves(X,Y)# in
\begin{verbatim}
      loves(X,Y), Y \= mary?
\end{verbatim}
If this were to happen in either of the knowledge bases above, it would
indeed produce two mutually inconsistent solutions: a fitness that's
both good and poor, or an act that's both a move and a buy (or sell).
 
This doesn't matter in Traveller\idx{Traveller!backtracking and \verb#act/4#|ee}, because I have written the
program surrounding \verb#act#\idx{act/4@\verb#act/4#!backtracking in|ee}
so that it swallows the first solution \verb#act#
yields up, and does not backtrack and ask for any others. Hence you can
get away with mutually inconsistent rules, as long as you remember that
they are tested from the top down, the first solution coming from the
first clause to
succeed. I have written all the examples used with the Tutor so
that this is true.
 
However, you should be aware that this is a departure from logic, and
that we're using Prolog's execution\idx{Prolog execution|ee} order to resolve inconsistencies.
This will become obvious if you restore COLLIER\idx{COLLIER|ee},
move the third clause
to the top (which you can do via a \verb#copy#\idx{copy@\verb#copy# command!demonstrating Prolog execution with|ee} and a \verb#delete#\idx{delete@\verb#delete# command|ee}),
and then call \verb#run#\idx{run/2@\verb#run/2#|ee}.
 
There are various ways around the problem of inconsistencies.
The purest approach is always to write conditions in
full. Were we to do so with COLLIER\idx{COLLIER|ee}, the third rule would become
\begin{alltt}
    act( collier, move, M, dummy ) :-\idx{act/4@\verb#act/4#|ee} \idx{not/1@\verb#not/1#!and mutually exclusive conditions|(ee}
        not((
                at_seller( collier, coal, Price ),\idx{at\_seller/3@\verb#at_seller/3#|ee}
                carries( collier, coal, 0 )
        )),
        not((
                at_buyer( collier, coal, Price ),\idx{at\_buyer/3@\verb#at_buyer/3#|ee}
                carries( collier, coal, 1000 )  \idx{carries/3@\verb#carries/3#|ee}
        )),
        at( collier, N ),\idx{at/2@\verb#at/2#|ee}
        clockwise( N, M ).\idx{clockwise/2@\verb#clockwise/2#|ee}
\end{alltt}
Here I have used \verb#not#\idx{not/1@\verb#not/1#|ee}
(mentioned briefly in the animals classifier\idx{animal classifier|ee} of
Supplement 4) to negate the first two conditions. This approach is
logically good because the third rule now carries with it all the
information it needs, and no longer has to rely on its position relative
to the others. It makes the program easier to read, because a reader can
understand the third rule in isolation, without having to examine the
preceding ones. \idx{act/4@\verb#act/4#|ee} \idx{not/1@\verb#not/1#!and mutually exclusive conditions|)ee}
 
\idx{backtracking!and cut|(ee}
 
Unfortunately, writing all conditions in full can result in very
long-winded programs, and in inefficiencies. This is because Prolog has to test
some conditions more than once. An alternative approach is to use a
special symbol called {\it cut}\idx{cut!for cutting non-exclusive conditions|(ee}
and written as an exclamation mark:
\begin{alltt}
    act( collier, buy, coal, 20 ) :-\idx{act/4@\verb#act/4#|ee}
        at_seller( collier, coal, Price ),\idx{at\_seller/3@\verb#at_seller/3#|ee}
        carries( collier, coal, 0 ),\idx{carries/3@\verb#carries/3#|ee}
        !.
 
    act( collier, sell, coal, 20 ) :-
        at_buyer( collier, coal, Price ),\idx{at\_buyer/3@\verb#at_buyer/3#|ee}
        carries( collier, coal, 1000 ),
        !.
 
    act( collier, move, M, dummy ) :-
        at( collier, N ),\idx{at/2@\verb#at/2#|ee}
        clockwise( N, M ).\idx{clockwise/2@\verb#clockwise/2#|ee}
\end{alltt}
 
Textbooks often make a great fuss about the cut, trying to give the
impression that it is rather more complex than, say, quantum mechanics,
or the justification for monetarism (the first is false; the second may
well be true). In this context though, there's no mystery. The cut tells
Prolog ``when you have got here, do not consider any alternative
solutions''. So the first cut is saying, in effect, ``if buy was one
solution, sell and move are completely ruled out''.
 
You can test cut's effect by setting up \verb#collier#'s\idx{COLLIER|ee} load and position
(using \verb#at#\idx{at/2@\verb#at/2#|ee}
and \verb#carries#\idx{carries/3@\verb#carries/3#|ee})
so that he is at a retailer with a load to sell.
Then ask the question
\begin{verbatim}
    act( collier, Act, Arg1, Arg2 )?
\end{verbatim}
and reply \verb#y# every time Prolog asks whether you want another solution.
Without the cuts, you will get two solutions, a \verb#sell# and a \verb#move#. With
them, only one.
 
This is one of cut's two uses; in the other, it's inserted between goals\idx{goals!backtracking between prevented by cut|ee}
to stop the earlier one backtracking. I shall
say no more about cut
until the final lesson. In the meantime, I may
write rules that look inconsistent, but I will rely on any predicates that
call them never to ask for more than one solution. You may do the same
\ldots but you should know when you are doing it.
 
When I return to the cut, I shall present it as a kind of internal ``if'',
which can be used in writing predicates that perform one action or another
depending on the result of a condition. This will be a fiction, but a benign one:
as long as you follow the programming style I recommend, no problems
will arise.
 
%sann
%{\it When I started teaching, I swept this matter under the carpet,
%trusting to luck and infrequent backtracking. Students will usually not
%write mutually exclusive conditions, but in applications like the animal
%classifier, this does not matter too much. However, it is important once
%one begins using predicates for their side-effects\idx{side-effects!and backtracking}; if \verb#test#
%in a predicate of the form \verb#doit :- test, action# is resatisfiable,
%\verb#action# may be called more than once. In any case, some students
%see the extra solutions, notice that the first one is correct but the
%others are not, and become confused about the reason.}
 
\idx{backtracking!and cut|)ee}
 
\idx{cut!for cutting non-exclusive conditions|)ee}
\idx{backtracking!and non-exclusive conditions|)ee}
\idx{facts!conditional!mutually exclusive|)ee}
 
 
\end{document}
