TEACH WHY                                       Chris Mellish December 1983
                                                          Altered A. Sloman

WARNING: Since the printer can't print funny characters which the -why-
comand you may find that the command proceduces slightly different
'line-drawing' characters from those shown in this file.

The Prolog system being used for the Computers and Thought course is
capable of giving JUSTIFICATIONS for the answers it come up with. When
Prolog has just answered a question with "yes", you can get an impression
for WHY Prolog could show this by asking the special question:

    ?- why.

Prolog draws a picture to show the things it had to show to answer the
question with "yes", and what choices it had to make. Here is an example
to explain what the pictures show. Suppose we have a Prolog program as
follows (the facts and rules for each predicate have been numbered for the
sake of this discussion):

    student(john).                                      ;;; (1)
    student(mary).                                      ;;; (2)
    student(jane).                                      ;;; (3)

    likes(john,fish).                                   ;;; (1)
    likes(jane,prolog).                                 ;;; (2)

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

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.

You can send those assertions to Prolog by using the VED mark keys to mark
the whole range, then type CTRL-D.

The question is, who is a model student? To find out we ask:

    ?- model_student(X).

Prolog answers: X = jane
                yes               (assuming we are content with this answer)

Before you read on, work out for yourself WHY it follows from the above
facts and rules that Jane is a model student. Which of the facts and rules
are needed to show this, and how are they used?

There are two things that would help one to infer that people are model
students - the rule about students who like Prolog and the fact about
Fred. Obviously, the fact that Fred is a model student is not of much use
for showing that Jane is one, so Prolog must have used the general rule
instead. This is rule (1) about 'model_student'. Having chosen this rule,
there are two subgoals to be satisfied. This rule will only help us decide
that Jane is a model student if it is true that Jane is a student and Jane
likes Prolog. To show that Jane is a student, Prolog uses fact (3) about
'student's, and to show that she likes Prolog it uses fact (2) about
'likes'. So that's why it was possible to show that Jane is a model
student.

That justification, expressed in English, was rather long and cumbersome.
Here's how Prolog explains it, if we ask '?- why.':

 model_student(jane)
 \--(1)----\
           student(jane)
           \--(3)----\
           /---------/
           likes(jane,prolog)
           \--(2)----\
           /---------/
 /---------/

For each thing it has to show, Prolog must pick a fact or rule from the
database. The number of the one it picked is shown in brackets under the
fact it helps to prove. So, to prove the 'model_student' fact it used rule
(1) (the first fact or rule about model students). Sometimes using a rule
means that other things (subgoals) must be shown as well. In this case,
Prolog had to show that Jane is a student and likes Prolog. These subgoals
are shown in a vertical line below and on the right of the goal they
establish. In this case, neither of the subgoals required sub-subgoals to
be shown, and so there are no other goals to the right of the 'student' or
'likes' facts.

Try giving Prolog the above program and understanding the justification it
gives. Try accepting alternative answers and looking at the explanations
for them:

    ?- model_student(X).

    X = jane
    ? ;

    X = fred
    ?

    yes
    ?- why.

Finally, look at the justifications that are produced when Prolog answers
questions for other programs you have written.

The Prolog system that we have can only justify "yes" answers. It can't
say why it was NOT able to show something. Why is is easier for Prolog to
justify a "yes" answer than a "no" answer?
