using prolog s inference engine l.
Skip this Video
Loading SlideShow in 5 Seconds..
Using Prolog's Inference Engine PowerPoint Presentation
Download Presentation
Using Prolog's Inference Engine

Loading in 2 Seconds...

play fullscreen
1 / 31

Using Prolog's Inference Engine - PowerPoint PPT Presentation

  • Uploaded on

Using Prolog's Inference Engine . From: Chapter 2, Building Expert Systems in Prolog , Contents. The Bird Identification System User Interface A Simple Shell Summary. The Bird Identification System .

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'Using Prolog's Inference Engine' - LeeJohn

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
using prolog s inference engine

Using Prolog's Inference Engine

From: Chapter 2, Building Expert Systems in Prolog ,

  • The Bird Identification System
  • User Interface
  • A Simple Shell
  • Summary

Using Prolog's Inference Engine

the bird identification system
The 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.

Using Prolog's Inference Engine

rule formats
Rule formats


first premise, and

second premise, and




conclusion :-




Using Prolog's Inference Engine

rules about birds
Rules 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(whistling_swan) :-



bird(trumpeter_swan) :-




family is albatross and

color is white


bird is laysan_albatross

bird(laysan_albatross) :-



Using Prolog's Inference Engine

rules about birds6
Rules 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:



?- bird(X).

X = black_footed_albatross

Using Prolog's Inference Engine

rules for hierarchical relationships
Rules 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) :-




order(waterfowl) :-



family(albatross) :-




family(swan) :-





Using Prolog's Inference Engine

rules for hierarchical relationships8
Rules for hierarchical relationships







?- bird(X).

X = black_footed_albatross

Using Prolog's Inference Engine


















Relationships between some of the rules in the Bird identification system




Using Prolog's Inference Engine

rules for other relationships
Rules 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).

Using Prolog's Inference Engine

rules for other relationships11
Rules for other relationships

country(united_states):- region(mid_west).

country(united_states):- region(south_west).

country(united_states):- region(north_west).

country(united_states):- 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).

Using Prolog's Inference Engine

user interface
User 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.

Using Prolog's Inference Engine

attribute value pairs
Attribute 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:


family duck

voice quack

head green

Using Prolog's Inference Engine

attribute value pairs14
Attribute 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.

Using Prolog's Inference Engine

asking the user
Asking 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

Using Prolog's Inference Engine

asking the user16
Asking the user
  • The simplest version of ask

ask(Attr, Val):- write(Attr:Val),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

Using Prolog's Inference Engine

asking the user17
Asking 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.

Using Prolog's Inference Engine

remembering the answer
Remembering 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(A:V), % ask user write('? : '), read(Y), % get the answer asserta(known(Y, A, V)), % remember it Y == yes. % succeed or fail

Using Prolog's Inference Engine

multi valued answers
Multi-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 color:white and color:black.
  • 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.

Using Prolog's Inference Engine

multi valued answers20
Multi-valued answers
  • The best way to handle this is to add an additional predicate to the program which specifies which attributes are multi-valued:


Using Prolog's Inference Engine

menus for the user
Menus 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]).

Using Prolog's Inference Engine

menus for the user22
Menus 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).

Using Prolog's Inference Engine

a simple shell
A 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.

Using Prolog's Inference Engine

a simple shell24
A 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.

Using Prolog's Inference Engine

a simple shell25
A 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.

Using Prolog's Inference Engine

a simple shell26
A 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.

Using Prolog's Inference Engine

a simple shell27
A 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).


?- consult('birds.kb').


?- solve.

nostrils : external_tubular?

Using Prolog's Inference Engine

command loop
Command 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('> '), 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(X) :- write(X), write('is not a legal command.'), nl, fail.

Using Prolog's Inference Engine

command loop29
Command loop
  • 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).

load_kb :- write('Enter file name: '), read(F), reconsult(F).

Using Prolog's Inference Engine


User interface




Major predicates of Native Prolog shell














Using Prolog's Inference Engine

command loop31
Command loop
  • Using an interpreter the system would run as follows:

?- consult(native).


?- go.

This is the native Prolog shell.

Enter load, consult, or quit at the prompt.


Enter file name: 'birds.kb'.


nostrils : external_tubular ? yes.


The answer is black_footed_albatross



Using Prolog's Inference Engine