9.3 Goals and subgoals
Again, we reprint the example program:
student(Lisa, 5). student(Martin, 3). student(John, 3). student(Edward, 7).
Consider the following query:
?- student(Lisa, 5).
In this query, the structure
is a goal. Prolog tries to satisfy the goal when answering the query. The process involved when Prolog tries to satisfy a goal will be explained in detail later.
Goals and subgoals
There can be more than one goal in a query. In this case, we talk about the different subgoals of the query. Subgoals must be separated by commas, and again the whole query must be terminated with a period.
Example with two subgoals
For example, the following would be a query with two subgoals:
?- student(Lisa, 5), student(John, 3).
The answer to this query is:
meaning "yes". The answer is "yes" because both of the subgoals can be satisfied.
All subgoals must be satisfied
If even just one subgoal could not be satisfied, the whole of the query would fail. For example, the answer to the following query:
?- student(Lisa, 5), student(Abraham, 3).
because the second subgoal would fail even though the first would succeed.
Thus a query is made up of a comma-separated sequence of structures, terminated by a period. Each structure is called a subgoal. When answering a query, Prolog tries to satisfy each subgoal in turn, from left to right. If all of the subgoals can be satisfied, the query succeeds. If just one subgoal fails, the whole query fails.
Now that we know that Prolog tries to satisfy subgoals in a query, we need to look at how this is done. Explaining this will require us to look at a concept called "matching". We look at this next, albeit not in detail yet.
PrevLite: 9.2 Queries
NextLite: 9.4 Matching
Prev: 9.2 Queries
Up: 9 A simple Prolog program
Next: 9.4 Matching