Variables in Prolog

In Prolog, variables are typically represented by atoms starting with an uppercase letter or underscore. Unlike Go, Prolog is a declarative language, so the concept of variable declaration and initialization is different.

main :-
    % In Prolog, we use atoms (starting with lowercase) for strings
    A = initial,
    writeln(A),

    % Multiple assignments can be done using multiple predicates
    B = 1,
    C = 2,
    format('~w ~w~n', [B, C]),

    % Prolog uses 'true' and 'false' for boolean values
    D = true,
    writeln(D),

    % Uninstantiated variables in Prolog are similar to zero-valued variables
    % They are represented by an underscore (_) or a unique variable name
    writeln(_),

    % In Prolog, we don't have a shorthand for declaration and initialization
    % We simply assign values to variables
    F = apple,
    writeln(F).

To run this Prolog program:

?- main.
initial
1 2
true
_G3401
apple
true.

In Prolog:

  1. Variables are represented by atoms starting with an uppercase letter or underscore.
  2. We use = for unification (assignment).
  3. The writeln/1 predicate is used to print values followed by a newline.
  4. The format/2 predicate is used for formatted output.
  5. Uninstantiated variables (similar to zero-valued variables in some languages) are represented by an underscore (_) or a unique variable name.
  6. Prolog doesn’t have a concept of variable declaration separate from assignment. Variables are instantiated when they are unified with a value.
  7. Prolog is a declarative language, so the concept of “declaring” variables is different from imperative languages.

Note that Prolog’s paradigm is quite different from imperative languages, so some concepts don’t have direct equivalents. This example demonstrates how to achieve similar functionality in a Prolog-like way.