Section 1.5 Logic Programming
81
infoodchain, a match occurs with the fact eat(littlefish, algae). This results in response 6, algae. There are no other facts of the form eat(littlefish, Y ), so the next
thing to try is the recursive case of infoodchain(littlefish, Y ):
infoodchain(littlefish, Y ) <= eat(littlefish, Z ) and infoodchain(Z, Y )
A match of eat(littlefish, Z ) occurs with Z equal to “algae.” Prolog then looks for
all solutions to the relation infoodchain(algae, Y ). A search of the entire database
reveals no facts of the form eat(algae, Y ) (or eat(algae, Z )), so neither the simple
case nor the recursive case of infoodchain(algae, Y ) can be pursued further.
bear
bear
bear
bear
bear
fish
raccoon
fox
deer
fish
fish
littlefish
littlefish algae
Figure 1.2 shows the situation at this point. Prolog has reached a dead-end with
infoodchain(algae, Y ) and will backtrack up the path. Because there are no other
facts of the form eat(littlefish, Z ), the search for solutions to infoodchain(littlefish,
Y ) terminates. Then, because there are no other facts of the form eat(fish, Z), the
search for solutions to infoodchain(fish, Y ) terminates. Backing up still further,
there is another match of eat(bear, Z ) with Z equal to “raccoon” that will generate
another search path.
Figure 1.2
In Example 40, once Prolog began to investigate infoodchain(fish, Y ), all query answers that could be obtained from exploring this path (responses 5 and 6)
were generated before other answers (responses 7–12). Exploring as far as possible
down a given path and then backtracking up that path before exploring other paths
is called a depth-first search strategy.
pRaCtiCe 30 Trace the execution of the Prolog program of Example 40 and explain why responses 7–12
occur.
expert systems
Many interesting applications programs have been developed, in Prolog and similar logic programming languages, that gather a database of facts and rules about
some domain and then use the database to draw conclusions. Such programs are
known as expert systems, knowledge-based systems, or rule-based systems.
81
infoodchain, a match occurs with the fact eat(littlefish, algae). This results in response 6, algae. There are no other facts of the form eat(littlefish, Y ), so the next
thing to try is the recursive case of infoodchain(littlefish, Y ):
infoodchain(littlefish, Y ) <= eat(littlefish, Z ) and infoodchain(Z, Y )
A match of eat(littlefish, Z ) occurs with Z equal to “algae.” Prolog then looks for
all solutions to the relation infoodchain(algae, Y ). A search of the entire database
reveals no facts of the form eat(algae, Y ) (or eat(algae, Z )), so neither the simple
case nor the recursive case of infoodchain(algae, Y ) can be pursued further.
bear
bear
bear
bear
bear
fish
raccoon
fox
deer
fish
fish
littlefish
littlefish algae
Figure 1.2 shows the situation at this point. Prolog has reached a dead-end with
infoodchain(algae, Y ) and will backtrack up the path. Because there are no other
facts of the form eat(littlefish, Z ), the search for solutions to infoodchain(littlefish,
Y ) terminates. Then, because there are no other facts of the form eat(fish, Z), the
search for solutions to infoodchain(fish, Y ) terminates. Backing up still further,
there is another match of eat(bear, Z ) with Z equal to “raccoon” that will generate
another search path.
Figure 1.2
In Example 40, once Prolog began to investigate infoodchain(fish, Y ), all query answers that could be obtained from exploring this path (responses 5 and 6)
were generated before other answers (responses 7–12). Exploring as far as possible
down a given path and then backtracking up that path before exploring other paths
is called a depth-first search strategy.
pRaCtiCe 30 Trace the execution of the Prolog program of Example 40 and explain why responses 7–12
occur.
expert systems
Many interesting applications programs have been developed, in Prolog and similar logic programming languages, that gather a database of facts and rules about
some domain and then use the database to draw conclusions. Such programs are
known as expert systems, knowledge-based systems, or rule-based systems.
