Title: Using Prolog's Inference Engine
1Using Prolog's Inference Engine
- From Chapter 2, Building Expert Systems in
Prolog , http//www.amzi.com/ExpertSystemsInProlog
/xsipfrtop.htm
2Contents
- The Bird Identification System
- User Interface
- A Simple Shell
- Summary
3The Bird Identification System
- A system which identifies birds will be used to
illustrate a native Prolog expert system. - The expertise in the system is a small subset of
that contained in Birds of North America by
Robbins, Bruum, Zim, and Singer. - The rules of the system were designed to
illustrate how to represent various types of
knowledge, rather than to provide accurate
identification.
4Rule formats
IF first premise, and second premise,
and ... THEN conclusion
conclusion - first_premise, second_premise, ...
5Rules about birds
- The most fundamental rules in the system identify
the various species of birds. - We can begin to build the system immediately by
writing some rules. Using the normal IF THEN
format, a rule for identifying a particular
albatross is
bird(black_footed_albatross)- family(albatross),
color(dark). bird(whistling_swan)
- family(swan), voice(muffled_musical_whistle).
bird(trumpeter_swan) - family(swan),
voice(loud_trumpeting).
IF family is albatross and color is
white THEN bird is laysan_albatross
bird(laysan_albatross) - family(albatross),
color(white).
6Rules about birds
- In order for these rules to succeed in
distinguishing the two birds, we would have to
store facts about a particular bird that needed
identification in the program. For example if we
added the following facts to the program
family(albatross). color(dark).
?- bird(X). X black_footed_albatross
7Rules for hierarchical relationships
- The next step in building the system would be to
represent the natural hierarchy of a bird
classification system. - These would include rules for identifying the
family and the order of a bird.
order(tubenose) - nostrils(external_tubular),
live(at_sea), bill(hooked). order(waterfowl
) - feet(webbed), bill(flat).
family(albatross) - order(tubenose),
size(large), wings(long_narrow). family(swan)
- order(waterfowl), neck(long),
color(white), flight(ponderous).
8Rules for hierarchical relationships
nostrils(external_tubular). live(at_sea). bill(hoo
ked). size(large). wings(long_narrow). color(dark)
.
?- bird(X). X black_footed_albatross
9bird
lysan_albatros
black_footed_albatros
trumpter_swan
family(albatros)
color(white)
voice(loud)
family(swan)
color(dark)
family(albatros)
family(albatros)
family(swan)
order(tubenose)
live(at_sea)
bill(hooked)
color(white)
Relationships between some of the rules in the
Bird identification system
order(waterfowl)
neck(long)
flight(ponderous)
10Rules for other relationships
- The Canada goose can be used to add some
complexity to the system. - Since it spends its summers in Canada, and its
winters in the United States, its identification
includes where it was seen and in what season.
bird(canada_goose)- family(goose),
season(winter), country(united_states),
head(black), cheek(white).
bird(canada_goose)- family(goose),
season(summer), country(canada),
head(black), cheek(white).
11Rules for other relationships
country(united_states)- region(mid_west). country
(united_states)- region(south_west). country(unit
ed_states)- region(north_west). country(united_st
ates)- region(mid_atlantic). country(canada)-
province(ontario). country(canada)-
province(quebec). region(new_england)-
state(X), member(X, massachusetts, vermont,
....). region(south_east)- state(X),
member(X, florida, mississippi, ....).
bird(mallard)- family(duck), voice(quack),
head(green). bird(mallard)- family(duck),
voice(quack), color(mottled_brown).
12User Interface
- The system can be dramatically improved by
providing a user interface which prompts for
information when it is needed, rather than
forcing the user to enter it beforehand. - The predicate ask will provide this function.
13Attribute Value pairs
- Before looking at ask, it is necessary to
understand the structure of the data which will
be asked about. - All of the data has been of the form
"attribute-value". - For example, a bird is a mallard if it has the
following values for these selected bird
attributes
Attribute Value family duck voice
quack head green
14Attribute Value pairs
- This is one of the simplest forms of representing
data in an expert system, but is sufficient for
many applications. - More complex representations can have
"object-attribute-value" triples, where the
attribute-values are tied to various objects in
the system. - Still more complex information can be associated
with an object and this will be covered in the
chapter on frames. - For now the simple attribute-value data model
will suffice.
15Asking the user
- The ask predicate will have to determine from the
user whether or not a given attribute-value pair
is true. - The program needs to be modified to specify which
attributes are askable. - This is easily done by making rules for those
attributes which call ask.
eats(X)- ask(eats, X). feet(X)- ask(feet,
X). wings(X)- ask(wings, X). neck(X)- ask(neck,
X). color(X)- ask(color, X).
- Now if the system has the goal of finding
color(white) it will call ask, rather than look
in the program. If ask(color, white) succeeds,
color(white) succeeds
16Asking the user
- The simplest version of ask
ask(Attr, Val)- write(AttrVal),write('? '),
read(yes).
?- bird(X). nostrils external_tubular?
yes. live at_sea? yes. bill hooked? yes. size
large? yes. wings long_narrow? yes. color
white? yes. X laysan_albatross
17Asking the user
- There is a problem with this approach.
- If the user answered "no" to the last question,
then the rule for bird(laysan_albatross) would
have failed and backtracking would have caused
the next rule for bird(black_footed_albatross) to
be tried. - The first subgoal of the new rule causes Prolog
to try to prove family(albatross) again, and ask
the same questions it already asked. - It would be better if the system remembered the
answers to questions and did not ask again.
18Remembering the answer
ask(A, V)- known(yes, A, V), succeed if
true !. stop looking ask(A, V)-
known(_, A, V), fail if false !,
fail. ask(A, V)- write(AV), ask user
write('? '), read(Y), get the answer
asserta(known(Y, A, V)), remember it Y
yes. succeed or fail
19Multi-valued answers
- The ask predicate now assumes that each
particular attribute value pair is either true or
false. - This means that the user could respond with a
"yes" to both colorwhite and colorblack. - In effect, we are letting the attributes be
multi-valued. - This might make sense for some attributes such as
voice but not others such as bill, which only
take a single value.
20Multi-valued answers
- The best way to handle this is to add an
additional predicate to the program which
specifies which attributes are multi-valued
multivalued(voice).multivalued(feed).
21Menus for the user
- The user interface can further be improved by
adding a menu capability which gives the user a
list of possible values for an attribute. - It can further enforce that the user enter a
value on the menu.
size(X)- menuask(size, X, large, plump, medium,
small). flight(X)- menuask(flight, X,
ponderous, agile, flap_glide).
22Menus for the user
can be implemented using a sophisticated
windowing interface
menuask(A, V, MenuList) - write('What is the
value for'), write(A), write('?'), nl,
write(MenuList), nl, read(X),
check_val(X, A, V, MenuList), asserta(
known(yes, A, X) ), X V. check_val(X, A,
V, MenuList) - member(X, MenuList),
!. check_val(X, A, V, MenuList) - write(X),
write(' is not a legal value, try again.'), nl,
menuask(A, V, MenuList).
23A Simple Shell
- The bird identification program has two distinct
parts - the knowledge base, which contains the specific
information about bird identification and - the predicates which control the user interface.
- By separating the two parts, a shell can be
created which can be used with any other
knowledge base. - For example, a new expert system could be written
which identified fish. - It could be used with the same user interface
code developed for the bird identification system.
24A Simple Shell
- The minimal change needed to break the two parts
into two modules is a high level predicate which
starts the identification process.
top_goal(X) - bird(X).
- The shell has a predicate called solve, which
does some housekeeping and then solves for the
top_goal. It looks like
solve - abolish(known, 3), define(known,
3), top_goal(X), write('The answer is '),
write(X), nl. solve - write('No answer
found.'), nl.
25A Simple Shell
- In summary, the predicates of the bird
identification system have been divided into two
modules. The predicates which are in the shell
called Native, are
solve - starts the consultation ask - poses
simple questions to the users and
remembers the answers menuask - presents the
user with a menu of choices supporting
predicates for the above three predicates.
26A Simple Shell
- The predicates which are in the knowledge base
are
top_goal - specifies the top goal in the
knowledge base rules for identifying or
selecting whatever it is the knowledge
base was built for (for example bird,
order, family, and region) rules for
attributes which must be user supplied (for
example size, color, eats, and
wings) multivalued - defines which attributes
might have multiple values.
27A Simple Shell
- To use this shell with a Prolog interpreter, both
the shell and the birds knowledge base must be
consulted. Then the query for solve is started.
?- consult(native). yes ?- consult('birds.kb'). ye
s ?- solve. nostrils external_tubular?
28Command loop
- The shell can be further enhanced to have a top
level command loop called go. To begin with, go
should recognize three commands - load - Load a knowledge base.
- consult - Consult the knowledge base by
satisfying the top goal of the knowledge base. - quit - Exit from the shell.
go - greeting, repeat, write('gt '),
read(X), do(X), X
quit. greeting - write('This is the Native
Prolog shell.'), nl, write('Enter load,
consult, or quit at the prompt.'), nl.
do(load) - load_kb, !. do(consult) - solve,
!. do(quit). do(X) - write(X), write('is
not a legal command.'), nl, fail.
29Command loop
load_kb - write('Enter file name '),
read(F), reconsult(F).
- Two other commands which could be added at this
point are - help - provide a list of legal commands
- list - list all of the knowns derived during the
consultation (useful for debugging).
30User interface go ask menuask
Major predicates of Native Prolog shell
Inference engine solve load
Knowledge base top_goal rules multi_valued askable
Working storage known
31Command loop
- Using an interpreter the system would run as
follows
?- consult(native). yes ?- go. This is the native
Prolog shell. Enter load, consult, or quit at the
prompt. gtload. Enter file name
'birds.kb'. gtconsult. nostrils external_tubular
? yes. ... The answer is black_footed_albatross gtq
uit. ?-