[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

PSYCH - Activating Prolog from AIML

From: Dr. Richard S. Wallace
Subject: PSYCH - Activating Prolog from AIML
Date: Mon, 26 Nov 2001 12:38:30 -0800

PSYCH - Activating Prolog from AIML

Richard Wallace
ALICE A.I. Foundation
November, 2001

This note describes a first experiment linking
an Artificial Intelligence Markup Language
(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.  We 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

The basic approach was to interface AIML to
Prolog through the AIML <system> tag.  The Prolog
program ran as a separate execuatble activated
by the <system> tag.

It was not too difficult to download and
install the GNU gprolog interpreter and compiler
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
script was compiled into an executable called "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 basic form of a prolog query is like

isa(dog, animal).

or more generally


The ending period is important, playing much the same role
as the ending ";" in Java or C++.  Thus we can query
the psych program from the shell with the command:

% echo "isa(dog,animal)." | ./psych

which will print some text including the Prolog
reply "Yes" or "No".

The obvious <system> command would be something

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

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, contains the line:

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

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

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

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

3. The 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 assertions about binary
predicates, 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).

Through the associative rule the program can reason about
"isa" questions and answer even ones that are not explicity
stated.  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." Through the
associative rule the bot can reason that since a dog is a
carnivore, and carnivore is an animal, and an animal is alive,
then therfore a dog is alive.

Although the preceding Prolog fragment is syntactically correct,
unfortunately it produces an infinite loop.  This appears to
be a side effect of the order of evaluation in Prolog's built-in

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 engine tries to find not just one,
but the 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 AIML category used to test the Prolog prorgam PSYCH is:

<pattern>IS A * A *</pattern>
sh /home/alicebot/ <star index="1"/> <star index="2"/>

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

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

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

Similar <srai> categories were defined for:

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

5. A 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

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?"

In the end however it remains unclear whether interfacing
ALICE and AIML to a commonsense reasoning engine provides
any real practical advantages over a pure AIML pattern
targeting approach.  Instead of using Prolog to derive
answers, over a finite domain of terms, we could just as
well explicitly store all the answers in AIML:

<pattern>IS A DOG A MAMMAL</pattern>

<pattern>IS A DOG A BIRD</pattern>

<pattern>IS A DOG ALIVE</pattern>

...and so on.

The number of combinations of all terms may produce a large number
of combinations, possibly requiring more memory than the AIML
interpreter provides.   But even these patters have their own
Zipf distribution.  Some of them are much more likely to be asked
than others.  In this respect, one can imagine an AIML knowledge
base not quite equivalent to the output of a Prolog program, but
close enough for all practical purposes.


A. Prolog assertions.

In addition to the associative rules for the isa hierarchy,
the PSYCH program inlcudes the following basic commonsense

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).

B. Refernces

Prolog References:
1. Prolog Tutorial
2. A Short Tutorial on Prolog
3. Prolog Programming: A First Course
4. GNU Prolog site

AIML References:
1. Documentation
2. Don't Read Me: Program dB
3. AIML Specification (Working Draft)

C. Acknowledgements

This research was funded by donations to, and carried out
by volunteers of, the ALICE A.I. Foundation, Inc., a
nonprofit charitable research corporation.

Copyright 2001 ALICE A.I. Foundation, Inc.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]