TEACH TRACER                                Chris Mellish December 1983

This file shows you how to use a special program to watch your Prolog
programs working in "slow motion". It assumes that you are already
familiar with the notation Prolog uses for justifications (see
TEACH * WHY).

To use the "tracer" program, you must start by asking Prolog the question:

    ?- library(tracer).

Mark that and type CTRL-D. This causes Prolog to read in the program from
a "library" of useful programs. It may take a few minutes before it
answers "yes".

Once the tracing program has been read into the Prolog database, the
behaviour when you ask a question will change. Instead of you (perhaps)
seeing a justification for each answer when Prolog has already worked it
out, you will be shown how Prolog builds up the justification, and how it
searches through the possible ways of coming to an answer by trial and
error. In order to slow things down to human speed (!!!) Prolog pauses
every so often and prints a message on the VED "command line". Just press
any key to cause it to continue.

Try it out now on the following program (repeated from TEACH * WHY). You
can mark it, and press CTRL-D to get it compiled.

    student(john).
    student(mary).
    student(jane).

    likes(john,fish).
    likes(jane,prolog).

    model_student(X) :- student(X), likes(X,prolog).
    model_student(fred).

This is intended to mean that various people are students and that various
people like various things. Someone is a model student if they are a
student and like Prolog. Also, Fred is a model student. The question is,
who is a model student? Find out by asking:

    ?- model_student(X).

Look out for the following things while Prolog is finding a solution (and
finding a second solution if you ask for an alternative):

    1. Prolog prints out a variable that doesn't yet stand for anything as
       a "_" sign followed by a number (it forgets the names that were
       used for variables in programs). If "_"s followed by the SAME
       number appear in different places, then it means that the objects
       that these two variables stand for must end up the same.

    2. When the subgoals arising from the clause are displayed, the
       variables in the two subgoals (printed as "_1") are both the same,
       and they are the same as the variable in the question. This means
       that to show that this person is a model student it must show that
       the SAME person is a student and a liker of Prolog.

    3. When Prolog reaches a goal (the little line moves up to it), it
       must use trial and error to pick a fact or rule that is
       appropriate. The number in the brackets tells you which fact or
       rule it is currently considering.

    4. When Prolog successfully uses a fact for the 'student' goal, this
       causes "_1" to be matched with "john" (the first time; next time
       "mary"; next time "jane"). All the "_1"s show this value
        simultaneously.

    5. When Prolog comes back to the 'student' goal (because it couldn't
       show that the student it chose likes Prolog or because that
       solution was rejected later on) all the "_1"s get set back again
       (because that student was no good). It can then try another
       possibility for "_1".

    6. In general, when the line is moving down the page, Prolog is being
       successful in satisfying goals. When it comes to a "dead end" (a
       goal that cannot be satisfied), the line retreats back up the page,
       looking for an alternative choice to make.

Spend some time getting familiar with the "tracer" and getting a feeling
for how much work Prolog has to do for various tasks. Try tracing
alternative questions for this program and questions for other programs
you have written. Make sure you type ";" instead of RETURN some of the
time to see what happens when you ask Prolog to try to find different
solutions. Notice that it has to remember where it had got to in finding
the original solution.

NOTE for experienced programmers: The TRACER package will not necessarily
work for programs that contain "cut"s or which perform input and output.
