7.26 Numbered Variables

A widespread convention in Prolog is using a common prefix with a numeric suffix to name related variables, such as Foo0, Foo1, etc. Sweep provides convenient commands for managing such numbered variable sequences consistently:

C-c C-+

Prompt for a numbered variable and increment it and all numbered variables with the same base name and a greater number in the current clause (sweeprolog-increment-numbered-variables).

C-c C--

Prompt for a numbered variable and decrement it and all numbered variables with the same base name and a greater number in the current clause (sweeprolog-decrement-numbered-variables).

Numbering variables is often used to convey the order in which they are bound. For example:

%!  process(+State0, -State) is det.

process(State0, State) :-
    foo(State0, State1),
    bar(State2, State1),
    baz(State2, State).

Here State0 and State are respectively the input and output arguments of process/2, and State1 and State2 represent intermediary stages between them.

The command C-c C-+ (sweeprolog-increment-numbered-variables) prompts you for a numbered variable in the current clause, and increments the number of that variable along with all other numbered variables with the same base name and a greater number. You can use it to “make room” for another intermediary variable between two sequentially numbered variables. If you call this command with point on a numeric variable, it suggests that variable as the default choice. If you call this command with a prefix argument, it increments by the numeric value of the prefix argument, otherwise it increments by one.

For instance, typing C-c C-+ State1 RET with point anywhere in the definition of process/2 from the above example results in the following code:

process(State0, State) :-
    foo(State0, State2),
    bar(State3, State2),
    baz(State3, State).

Note how sweeprolog-increment-numbered-variables replaced all occurrences of State1 with State2, while the original occurrences of State2 are replaced with State3. The overall semantics of the clause doesn’t change, but you can now replace the call to foo/2 with two goals and reintroduce State1 as an intermediary result between them while keeping your numbering consistent, e.g.:

process(State0, State) :-
    one(State0, State1), two(State1, State2),
    bar(State3, State2),
    baz(State3, State).

If Context Menu mode is enabled, you can also invoke sweeprolog-increment-numbered-variables by right-clicking on a numbered variables and selecting ‘Increment Variable Numbers’ from the context menu. See Context Menu.

The command C-c C-- (sweeprolog-decrement-numbered-variables) is similar to C-c C-+ except it decrements all numbered variables starting with a given numbered variable rather than incrementing them. When you delete an intermediary numbered variable and end with a gap in the variable numbering sequence, you can use this command to close the gap by decrementing the following numbered variables.

After invoking either C-c C-- or C-c C-+, you can continue to decrement or increment the same set of numbered variables by repeating with - and +.