[alicebot-style] PSYCH - Prolog in AIML

Dr. Richard S. Wallace alicebot-style@list.alicebot.org
Sun, 25 Nov 2001 03:46:52 -0800


PSYCH - Activating Prolog from AIML

This note describes a first experiment linking
an AIML script to a Prolog program.  Specifically,
we used GNU gprolog to implement a program called
PSYCH (pronounced "CYC").  The AIML script uses
the <system> command to activate the PSYCH
commonsense reasoning engine.

Everything described here took place on a Linux
(Red Hat 7.1) machine.  I have not yet tried running
this under Windows, although there is a Windows
version of gprolog available.

The goal was to create a set of AIML categories
that answer questions using a Prolog program.
This experiment implements a simple "isa"
hierarchy, so the bot can answer questions like

"Is duck a food?"
"Is rock alive?"
"Is a dog an animal?"
"Is a cat a pet?"

...and so on.

1. The <system> command

It was not too difficult to download and
install the Prolog interpreter
from http://pauillac.inria.fr/~diaz/gnu-prolog.
There are HTML documentation pages and
several directories containing programming
examples.  The associated Makefiles were
a useful guide to writing and compiling
simple Prolog programs.

The gprolog suite includes a compiler to
create executable prolog programs.  The PSYCH
pscript was compiled into an executable psych.
Normally Prolog runs in an interactive prompting
mode, but it is possible to redirect the input
from a file or with a pipe.

The obvious <system> command would be something
like

<system>echo "isa(<star index="1"/>,<star index="2"/>)" |
./psych</system>

Unfortunately the implementation of <system> in
programs B, D and dB uses the Java Runtime.exec()
method.  The exec() method has some known problems
dealing with Unix pipes ("|") and the echo command.

2. The shell script

To get around the problems with pipes and echo in
Java Runtime.exec(), an intermediate shell script was
created.  This script, called isa.sh, contains the line:

echo "isaset($1,$2)." | ./psych | tail -2 | head -1

The echo command inserts the arguments to isa.sh, $1 and $2,
into the Prolog query string isaset($1, $2).  For example if
we typed the command

isa.sh dog animal

The echo command would produce the string "isaset(dog,animal)".

The ./psych command executes the prolog program.  The final
two command, tail and head, simply strip off excess output lines
generated by Prolog.

3. Prolog program

Another minor stumbling block in this experiment turned out to
be that Prolog does not implement isa-hierarchies very cleanly.
Nevertheless, the program PSYCH implements a small isa-hierarchy
well enough to answer some basic yes-no questions.

An isa-hierarchy consists of a set of binary predicates, or
assertions, like:

isa(dog, carnivore).
isa(dog, pet).
isa(cat, carnivore).
isa(cat, pet).
isa(carnivore, animal).

...and so on.  The complete set of these assertions used
in this experiment appears in an appendix below.

In addition to the assertions, there is a general
associative rule something like:

isa(X, Y) :-
  isa(X, Z), isa(Z, Y).

The preceding Prolog fragment is syntactically correct, but
produces an infinite loop.  The intuition behind the rule is
"X is a Y provided that there exists a Z such that X is Z and
Z is Y."

The infinite loop is avoided by introducing a new predicate
isb(X, Y) defined as follows:

isb(X, Y) :- isa(X, Y).
isb(X, Y) :-
  isa(X, Z),
  isb(Z, Y).

Finally, the Prolog interpreter tries to find not just one,
but a set of all solutions, for a particular problem.  In
the interactive console mode, the user would normally step
through all solutions with carriage return.  In our
non-interactive mode, we want only to find out whether
the set of solutions is non-empty.  For this purpose
we use the Prolog built-in predicate setof():

isaset(X, Y) :- setof(_, isb(X, Y), _).

The query based on the predicate isaset() is called
by the shell script described above.  The isaset()
predicate returns "Yes" or "No" depending on whether
a set of solutions of isb(X, Y) exists.

4. The AIML categories

The category used to test the Prolog prorgam PSYCH is:

<category>
<pattern>IS A * A *</pattern>
<template>
<system>
sh /home/alicebot/isa.sh <star index="1"/> <star index="2"/>
</system>
</template>
</category>

Note that rather than just run the script directly, Java
Runtime.exec() forced us to prepend a "sh" command to
the line.  The values of <star index="1"/> and
<star index="2"/> become the arguments to isa.sh.

Next, a few <srai> categories map variations of the
"isa" question to the simple pattern IS A * A *.

<category>
<pattern>IS A * AN *</pattern>
<template><srai>IS A <star/> A <star index="2"/></srai></template>
</category>

Similar <srai> categories were defined for:

<pattern>IS AN * A *</pattern>
<pattern>IS AN * AN *</pattern>
<pattern>IS * A *</pattern>
<pattern>IS * AN *</pattern>
and
<pattern>IS * *</pattern>

5. Sample Dialog

The following is a sample dialogue between a client
and the chat robot with a Prolog based
commonsense reasoning system:

Client: Is Rich alive?
Robot: Yes
Client: Is a boy a mammal?
Robot: Yes
Client: Is a dog a vegetable?
Robot: No
Client: Is a cat a pet?
Robot: Yes
Client: Is a penguin food?
Robot: No
Client: Is a human a mammal?
Robot: Yes

Of course, such a model of reasoning is, by itself,
severely limited.  One obvious flaw is that the bot
will always answer "No" for any out-of-domain questions.

Client: Is a frog an amphibian?
Robot: No
Client: Is a watch a clock?
Robot: No
Client: Is a horse an animal?
Robot: No

This problem could be approached by adding code to the Prolog
program to make sure that the elements of the query are also
elements of the domain of "isa".

Another problem is that the pattern IS A * A * may match
other inputs for which the PSYCH program is totally
inappropriate:

Is a dollar a piece a good price?
Is a gram, an ounce, or a pound bigger?
Is A.I. a good thing?

6. Conclusion

The experiment described here demonstrates how to activate
a simple Prolog program using the <system> tag of AIML.
It shows that quite a bit of "commonsense reasoning" can
be added to an AIML chat robot robot,
without proposing or adding any new AIML features.

The PSYCH program demonstrated here is currently limited
to only isa-relations, but more commonsense predicates
can be added easily.  For example some simple Prolog
predicates defining family relationships:

parent(X, X) :- fail.
parent(X, Y) :- mother(X, Y).
parent(X, Y) :- father(X, Y).

grandmother(X, Y) :-
  mother(X, Z),
  parent(Z, Y).

Or we might consider more complex predicates such as

between(X, Y, Y) :- fail.
between(X, Y, Z) :- between(X, Z, Y).

and more complex queries such as:

"What is a mammal?"
"Give me an example of a carnivore."
"Name some pets."
"Is Pittsburgh between New York and Chicago?"
"My mother is Mary.  Her mother is Millie.
Who is my grandmother?"

Appendix A. Prolog assertions.

isa(human, mammal).
isa(man, human).
isa(woman, human).
isa(baby, human).
isa(boy, human).
isa(girl, human).
isa(rich, man).
isa(carnivore, mammal).
isa(cat, carnivore).
isa(dog, carnivore).
isa(mammal, animal).
isa(animal, alive).
isa(plant, alive).
isa(fruit, plant).
isa(tree, plant).
isa(fir, tree).
isa(maple, tree).
isa(spruce, tree).
isa(birch, tree).
isa(grass, plant).
isa(fruit, food).
isa(weed, plant).
isa(vegetable, plant).
isa(vegetable, food).
isa(alive, entity).
isa(rock, entity).
isa(sand, rock).
isa(alien, alive).
isa(cat, pet).
isa(dog, pet).
isa(chicken, food).
isa(chicken, bird).
isa(bird, animal).
isa(duck, bird).
isa(duck, food).
isa(penguin, bird).
isa(cow, cattle).
isa(bull, cattle).
isa(cattle, herbivore).
isa(cattle, food).
isa(herbivore, animal).
isa(car, vehicle).
isa(plane, vehicle).
isa(jet, plane).
isa(train, vehicle).
isa(bart, train).
isa(muni, train).
isa(tram, train).
isa(bicycle, vehicle).
isa(bus, vehicle).
isa(truck, vehicle).
isa(suv, truck).
isa(rocket, vehicle).
isa(vehicle, manmade).
isa(manmade, entity).
isa(puddle, water).
isa(ocean, water).
isa(lake, water).
isa(river, water).
isa(stream, water).
isa(sea, water).
isa(cloud, water).
isa(water, compound).
isa(compound, entity).
isa(ice, water).
isa(steam, water).
isa(aspirin, medicine).
isa(penicillin, medicine).
isa(cancer, disease).
isa(aids, disease).
isa(flu, disease).
isa(glaucoma, disease).