~1
                       LOGIC PROGRAMMING LESSON 2

I shall begin with a resume of what you learnt in Lesson 1. Please go on
to the next section to see it. If you can't remember how to do this,
then you might like to revise Lesson 1. To do so, please type
      script lesson1.
Remember to type the dot and a RETURN.

If you want to go on to the next section of Lesson 2, please type
      next.

~2
In Lesson 1 and the Supplement, you learnt about

    Commands for inspecting parts of a lesson:
        next.                          (go to next section)
        section 1.                     (go to section 1)
        again.                         (repeat this section)

    How to leave the Tutor:
        bye.

~3
    How to tell the Tutor facts:
        mary loves john.

    How to ask questions, with or without variables:
        mary loves john?               (does Fred love Mabel?)
        mary loves Who?                (Who does Fred love?)
        X loves Y?                     (Who loves who?)

    How to see the reason for the Tutor's answer:
        why?                           (or why. with a dot)

~4
    Conditional facts (sometimes called rules):
        john loves mary if mary loves john.
        john loves X if X cooks spinach.
        X is_a mortal if X is_a human.

    Conditional facts which contain 'and':
        suzy loves X if X cooks spinach and X sings well.

    Questions which contain 'and':
        mary loves john and anne loves george?

    Conditional facts with more than one definition:
        Skier should_go_to st_sartre  if
        Skier has_rating beginner  and
        Skier has_purpose fun.
        Skier should_go_to schloss_heidegger  if
        Skier has_rating beginner  and
        Skier has_purpose serious.

~5
You also learnt

    How to make the Tutor show you all its facts:
        show.                          (show on the terminal)
        show loves.                    (only show facts about "loves")
        show 1.                        (just show fact 1).
        show 10/230.                   (only show between these numbers)

        show ctc.                      (print at CTC)
        show ctc 10/230.               (print facts 10 to 230 at CTC)
        show oucs.                     (print at OUCS)

~6
You learnt how to modify your set of facts:

    How to delete a fact:
        delete mary loves john.
        delete.                        (delete last-entered fact)

    How to delete a fact or facts using numbers or names:
        2.                             (delete fact 2)
        delete 2.                      (as above)
        delete 1/90.                   (delete facts 1 to 90)
        delete loves.                  (delete all facts about "loves")

    How to change or insert a fact using numbers:
        2 chris likes liquorice.       (make this fact number 2)

~7
And finally, you learnt:
    How to store all your facts in a file for use in a later session.
        save myfacts.

    How to put those facts back into the knowledge base.
        restore myfacts.

~8
I also told you that the Tutor saves facts automatically every 5 changes
you make. That way, even if the computer breaks down, or you trip over
the power cable, or the Rag Hit Squad arrive in a madly-bleating crowd
and drag you off in a mass of shaving foam and sodden wool, most of your
work will have been preserved for later.

However, the disadvantage of this auto-save is that all the files it
creates have the same name (AUTO), and unless your teacher helps you,
you can only recover the most recently saved. So it's a good idea to
save your facts in files of your own naming from time to time,
especially if you change from one topic to another.

~9
In Supplement 1, I introduced the words PREDICATE and ARGUMENT,
which I shall use from now on. In "mary loves john", the predicate is
"loves" and its arguments are "mary" and "john".

I shall also use RULE to mean a conditional fact (one with IF in). I
shall write FACT on its own, or sometimes CLAUSE, when I mean either a
conditional or an unconditional fact and don't care which.       

Now, for the matter of Lesson 2.

~10                    
Please start by typing

      restore relations.

This will fill your knowledge base with the facts in a file which I have
created for you, called "relations". You can find out what is in this
file by typing "show" or "show ctc" as appropriate. Actually, it's
probably best to print these facts, because there are a lot of them.
When you've looked at them, go on to the next section.

~11
Your knowledge base now contains several kinds of fact. Here are some of
them:
    bill is_a male.
    freda is_a female.
    bill is_father_of charles.
    freda is_mother_of gertrude.

Altogether, it defines three predicates. They are "is_a",
"is_father_of", and "is_mother_of".

You will see that "is_a" is used in two ways. One is to say someone's
male; the other, to say they're female. So each fact in the knowledge
base is saying one of four things:
    That A is male;
    that A is female;
    that A is B's father;
    or that A is B's mother.

~12
In the rest of this lesson, we're going to use Logic to define what
various family relationships mean. We'll use the same kind of rules we
saw in Lesson 1 and the following exercises. But there is a difference
in the kind of knowledge we're encoding. Previously, we've concentrated
(not too seriously :-) on representing ad-hoc rules of thumb about
medicine, sport, and other messy topics. But in the rest of the current
lesson, we're going to think more about structural relationships; on
asking "what does it mean to say A is B's cousin, and how can I
formalise this notion?"

In terms of what I said in Supplement 1, we will be writing intensional
and not extensional definitions.

In A.I. terms, we're using Logic as a KNOWLEDGE REPRESENTATION LANGUAGE.
This is an important part of A.I., especially in expert systems work.
A.I. programs often mix ad-hoc and systematic knowledge. For example, a
system for diagnosing faults in radios might contain some rules of thumb
like "if it's a battery set, test for flat batteries", but it could also
contain some systematic knowledge about the properties of electronic
components, Ohm's law, etc. In medicine and psychology, I'm afraid the
rules are mainly ad-hoc...

~13
Now, back to relationships. Let's start simply. Some people in the
knowledge base are mothers, for example Amanda and Anjali. Now, I would
like to be able to ask the question
    amanda is_a mum?
or
    anjali is_a mum?
and receive the answer "Yes".

So, can you add a rule for "is_a" that will do this.

That is, can you write a rule which begins

    X is_a mum if

and which ends with a dot, and where there is some text between the "if"
and the dot which states under what conditions X can be a mum? Write
this rule in terms of those which you already have in the knowledge
base, and then type it into the Tutor. If you get it wrong, you can
always delete it later, so don't worry about mistakes.

~14
When trying these problems, remember that the Tutor knows only those
facts which are in its knowledge base (i.e. the set of facts which you
see on typing "show"). From which of these facts is it possible to infer
that someone might be a mother?

Well, a fact like "bill is_a male" isn't much use for this, though it
does tell you that Bill CAN'T be a mother.

How about "freda is_a female"? Not much use either: although Freda is
female, she is not automatically going to have children (unless, like an
aphid, she's been born with two generations of small but perfectly
formed offspring inside her).

~15                     
We've now rejected two kinds of fact as unhelpful: those like

      bill is_a male.
      freda is_a female.

The third kind of fact you see on doing "show" is exemplified by

      bill is_father_of charles.

This is not much help either. So all we are left with is facts like

      freda is_mother_of gertrude.

which is what we want. If Freda is the mother of Gertrude, she must be a
mum.

Indeed, if X is the mother of Y, whoever Y is, then X is necessarily a
mum. Can you work out how to express this in Logic, and type it in?

~16
You should have typed

      X is_a mum  if  X is_mother_of Y.

Following this, how would you complete the rule

      X is_a dad  if  ...

~17
Now, how about defining parenthood? Firstly, can you devise a rule or
rules to define
      X is_parent_of Y

You will probably need to type in two rules for this.

Remember: when doing these problems, examine the facts which Logic
already knows. Ask yourself which facts tell you something positive
about parenthood. Ignore ones that don't.

You are now left with a few facts from which you can infer parenthood.
Must more than one of them be true at the same time? If so, you need an
"and" (or perhaps several "and"s) after an "if":
      X is_parent_of Y  if  ... and ... .

On the other hand, perhaps there are several facts, any one of which is
sufficient in each case. Then you need several definitions of
"is_parent_of":
      X is_parent_of Y  if  ... .
      X is_parent_of Y  if  ... .

~18
In this case, you have two relevant kinds of fact. The facts about
maleness and femaleness are (as before) no help. The ones about
motherhood and fatherhood are of help.

X can be Y's parent if X is Y's mother. X can also be Y's parent if X is
Y's father. Unless X is a hermaphrodite (unlikely), X does not have to
be both father and mother at the same time to be a parent.

We can therefore write:

      X is_parent_of Y  if  X is_mother_of Y.
      X is_parent_of Y  if  X is_father_of Y.

i.e. X is Y's parent if EITHER X is Y's mother, OR X is Y's father.

~19
How could we define a rule
      X has children
?

Try it. Again, we want one (or more) rules of the form
      X has children  if  ... .

~20
There are actually three ways you could have done this.

This is the first.
      X has children  if  X is_parent_of Y.

This is the second.
      X has children  if  X is_a mum.
      X has children  if  X is_a dad.

And this is the third.
      X has children  if  X is_mother_of Y.
      X has children  if  X is_father_of Y.

The first two methods use rules you yourself wrote. The third method
sticks with what was originally in the knowledge base. In general, there
is often more than one method to solve such problems, and some methods
may use auxiliary definitions (as the first two do here).

~21
Now, let's try reversing the relationships. Can you write some
rules which define

      X is_son_of Y  if  ...

      X is_daughter_of Y  if  ...

      X is_a child of Y  if  ...

Remember, first find those facts that contain relevant information.
Secondly, work out how to combine them: do you need "and"s? do you need
more than one definition, as for parent?

Don't worry if there seems to be more than one way to solve the problem
- in the last section, there were three. There are often several equally
good solutions.

~22                     
Probably the easiest way to deal with these is to write rules
which talk about parenthood and sex. A son must be male. A son
must also (at the same time) have a parent:

      X is_son_of Y  if  X is_a male  and  Y is_parent_of X.

Alternatively, you could write

      X is_son_of Y  if  Y is_parent_of X  and  X is_a male.


Note that this is WRONG:
      X is_son_of Y  if  X is_a male.
      X is_son_of Y  if  Y is_parent_of X.
because it says that X is Y's son if X is EITHER male, OR if Y is X's
parent.

~23
Now, can you define what it is to be a sister? Try completing the
rule
      X is_sister_of Y  if  ...

Hints:
1)   X must be a female. The sex of Y does not matter.
2)   X and Y must have the same parent (if step-sister) or parents
     (if strict sister). I don't mind whether you take "sister" to
     include step-sisters, but make sure that you know whether you
     are doing so.

~24
Try asking a few questions about who is a sister of who. What is odd
about the answers? You would need to change your definition slightly to
exclude some of the answers which you now see. You don't know enough of
Logic to do this yet - what in particular don't you know?

~25
Next, please try defining a few more relationships: grandfather,
niece, great-uncle, cousin. Could you also define the predicate

      X is_married_to Y  if  ...

which would be true if X and Y were married?

Save your facts in a file if you want to keep them - don't call it
"relations" because you will then overwrite that set of examples I gave
you.

~26
Finally, how would you define the predicate "X ancestor_of Y",
meaning X is Y's ancestor? You may find this much harder than the other
problems: if you can't do it in twenty minutes or so, ask your teacher for
advice.

~27
You have now reached the end of the second lesson. Before proceeding to
Lesson 3, please ask your teacher for Supplement 2. This gives you some
more examples of representing knowledge in Logic. After that, you can go
to Lesson 3 by typing the command

      script lesson3.

after which you will see the first section of Lesson 3.
