Home » Variables in Prolog

Variables in Prolog

by Online Tutorials Library

Variables in Prolog

In the head or body of the clause, the variables are used. Variables are also used in goals, and those goals are entered at the system prompt.

Variables in Goals

In goals, the variable can be interpreted as meaning ‘find variable’s value that makes the goal satisfied’.

For example:

The above goal will be read as ‘find the X’s value such that large_animal(X) is satisfied’.

The following example, Animal Programs 3 shows the third version of Animals Programs. This example is same as Animals Program 2, but it has some additional rules as shown below:

The following shows a goal:

The above goal describes that to satisfy chases(A, B), find the value of A and B.

To satisfy the above goal, Prolog will search through all the clauses until a matching clause is found. The search for clauses will be done from top to bottom. In the body of that clause, it will work through the goals one by one, it will work from left to right, and attempting to satisfy each one in turn.

The following example shows the entering of some typical goals at the prompt and the output of Animal Programs 3.

We can’t prevent to generate the same answer more than once by backtracking.

For example:

Using the backtracking, two identical answers will be produced out of three by the query ?-chases(dane, A).

Lexical Scope of Variables

The following clause shows that

In the above clause, the occurrence of A and B variables can be replaced consistently by any other variables like First_person and Second_person.

The above can’t change the program of the user or meaning of the clause.

Universally Quantified Variables

In the fact or rule, if a variable appears, it indicates that the fact or rule applies for all possible variable value.

For example:

Existentially Quantified Variables

Assume that the database contains various clauses:

The above example has six clauses. These clauses define the definition of person/5 predicate. It has 5 arguments with an obvious interpretation like firstname, lastname, gender, age, and occupation of the person.

The rule is defined by the last clause. It has a predicate ‘person’. The rules mean that ‘for all X, X is a man if A is a man if X is a person whose sex is male.’ In the head of the clause, variable X stands for ‘for all X’, and it is known as universally quantified.


You may also like