74
Formal Logic
“grass.” It is up to the user to maintain a consistent understanding and use of the
predicates in a Prolog program. Thus
eat(bear, fish)
can be used either to represent the fact that bears eat fish or the fact that fish eat
bears!
We impose the convention that eat(X, Y ) means “X eats Y.” We could add
descriptions of two unary predicates, animal and plant, to the database by adding
the facts
animal(bear)
animal(fish)
animal(fox)
animal(deer)
plant(grass)
Armed with this Prolog program (database), we can pose some simple queries.
eXAMPLe 37
The query
?animal(bear)
merely asks if the fact animal(bear) is in the database. Because this fact is in the
database, Prolog would respond to the query by answering yes. (This is a one-step
proof sequence—no rules of inference are required). Further dialogue with Prolog
could include
?eat(deer, grass)
yes
?eat(bear, rabbit)
no
Queries may include variables, as shown in the next example.
eXAMPLe 38
The query
?eat(bear, X )
produces
fish
fox
as a response. Prolog has answered the query by searching the database for all
facts that match the pattern eat(bear, X ), where X is a variable. The answer “fish”
is given first because the rules are searched in order from top to bottom.
Précédent

- 91/986

Suivant