Jump to content

Prolog/Variables

From Wikibooks, open books for an open world

A PROLOG variable can represent anything; a number, a name, a structure, an array, something as complicated as the known universe. A PROLOG program works by constraining the variables until eventually they have particular values; then telling you what the values are. A simple program might be

  X is 3+2.

and when you run it, the result will be

  X=5
  Yes.

The program might not go as far as to constrain the variables to have exact values, so you might get

  equal(A,A).   % Explains that things are equal to themselves
  X is 3+2, equal(f(X,Z),Y).
  X=5
  Y=f(5,_)
  Yes

where the '_' means that you have a variable remaining as part of the solution.

You can also get a 'Yes' result for more than one value of the variables; this is called 'nondeterminism', and is OK. If no values of the variables will make a solution, PROLOG will say 'No'.


prev: Recursive Rules next: Lists