Javatpoint Logo
Javatpoint Logo

Evaluating Goals in Prolog

We will give a goal like cat(A) and go. Prolog searches from to bottom in the database. It examines the clauses that have heads with the same functor and arity. It finds that the head unifies with the goal or not. The goal fails if Prolog is unable to make successful unification. If it does, the output depends on whether the clause is a fact or rule.

The goal will immediately succeed if the clause is a fact. If the clause is a rule, one by one, the goal is evaluated in the body of the rule from left to right. The original goal succeeds if they all succeed.

A phrase 'a goal matches a clause' is used by the Prolog. It means that it unifies with the head of the clause. The following example shows a goal:

?-pred(india, X)

In the database, the first clause is with predicated pred/2. Using the following rule, the head can unify with the above goal. We will call this rule as Rule 1.

In unification, the variable A is bound to atom india, and variable X is bound to atom 'asia capital'. When A is bound to india, it affects all occurrence of A in the rule. The diagrammatical representation of this is as follow:

Evaluating Goals in Prolog

Now in Rule 1, Prolog examines the goals one by one. This process will work from left to right. To succeed the original goal, all the goals have to be satisfied in order. Evaluating each of these goals is the same as evaluating the original goals of the user.

Now will assume that the first clause, which is matched by goal capital(india, B) is the fact capital(india, delhi). In the body of Rule 1, the first goal is satisfied with variable B bound to delhi. Due to this binding, all the occurrence of B is affected in the body of Rule 1. Now we have the following:

Evaluating Goals in Prolog

In the body of Rule 1, we will try to satisfy the second goal. The rewritten form is asia(delhi).

Now we will call this above rule as Rule 2.

In the body of Rule 2, Prolog tries to satisfy the goals: write('PM Modi is Best') and nl. This will process successfully, and as a side effect, the following line shows as output:

PM Modi is Best

In the body of Rule 1, the first two goals have been satisfied. Two more rewritten form of goals are write(india) and nl. Both the goals succeed, and as a side effect, the following line shows as output:


In the body of Rule 1, all the goals have now succeeded. That means the goal that form its head succeeds like pred(india, 'asia capital').

?-pred(india, X) 

This specifies that original goals succeed, which is entered by the user, with variable X bound to atom 'asia capital'.

Prolog system produces the output as follows:

?-pred(india, X).
PM Modi is Best
X = 'asia capital'

Prolog system focus on the evaluation of goals with the head of clauses using the unification. In the process of satisfying the goal of the user, we will create a linkage between the goal, clause's head, and the goals in the body of rules. Although the description of this process is very lengthy, but visualizing the linkages is very easy.

Evaluating Goals in Prolog

A goal of the user is shown below:

?- pred(india, X)

In the above diagram, the user's goal has been placed on the right side. That is because it has much in common with a goal in the rule's body. The following example shows the goals which are entered by the user at the prompt.

?- owns(A, B), cat(B), write(A), nl. 

The above sequence of goals will treat in the same way as goals in the imaginary rule. It will say succeed:- owns(A, B), cat(B), write(A), nl.

The following flowchart shows the process of evaluating a goal.

Evaluating Goals in Prolog

Fig: Evaluating a Sequence of Goals

Evaluating Goals in Prolog

Fig: Evaluating a Goal

The left part shows what happens if any of the goals fails in evaluation. If the goal fails, Prolog tries to find another way of satisfying the most recently satisfied previous goal. This process is called backtracking. Unification and backtracking together comprise the mechanism. Prolog uses that mechanism to evaluate the goal, whether the goal is entered in the body rule or by the user at the prompt.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA