Search code examples
prologswi-prolog

SWI: '+', '-', '--', '?' marks


Are there any examples of how argument mode indicators work?

There's something about them in SWI documentation.

But I have no idea how they can be applied.


Solution

  • Those are mode indicators and are used to indicate the mode each argument should be used when calling a goal and upon successful completion of the goal.

    They may be enforced on runtime (for example using a mode directive) or just used as a hint of for documentation purposes (for example in the documentation of SWI).

    The exact meaning of those indicators also depend on your prolog processor. For example in SWI:

    • + means that the argument should not be a free variable when calling the goal
    • -- means that the argument should be a free variable when calling the goal and would be instantiated there

    To use them for documentation purposes and to keep the code readable you would use it like this (example taken from some old SWI implementation for sum_list/2):

    %!  sum_list(+List, -Sum) is det.
    %
    %   Sum is the result of adding all numbers in List.
    sum_list(Xs, Sum) :-
        sum_list(Xs, 0, Sum).
    
    sum_list([], Sum, Sum).
    sum_list([X|Xs], Sum0, Sum) :-
        Sum1 is Sum0 + X,
        sum_list(Xs, Sum1, Sum).