projog

3.53. numbervars(Term,Start,End) - unifies free variables of a term.

Unifies the free variables in Term with a structure of the form $VAR(N) where N is the number of the variable. Numbering of the variables starts with the numeric value represented by Start. End is unified with the number that would of been given to the next variable.

Examples

?- numbervars(a,0,Y).
Y = 0

yes

?- numbervars(X,0,Y).
X = $VAR(0)
Y = 1

yes

?- numbervars(X,42,Y).
X = $VAR(42)
Y = 43

yes

?- X=p(A,B,p(C,B,C,D,[E,A,F,F,G,D])), numbervars(X,0,Y).
A = $VAR(0)
B = $VAR(1)
C = $VAR(2)
D = $VAR(3)
E = $VAR(4)
F = $VAR(5)
G = $VAR(6)
X = p($VAR(0), $VAR(1), p($VAR(2), $VAR(1), $VAR(2), $VAR(3), [$VAR(4),$VAR(0),$VAR(5),$VAR(5),$VAR(6),$VAR(3)]))
Y = 7

yes

?- X=p(A,B,p(C,B,C,D,[E,A,F,F,G,D])), numbervars(X,42,Y).
A = $VAR(42)
B = $VAR(43)
C = $VAR(44)
D = $VAR(45)
E = $VAR(46)
F = $VAR(47)
G = $VAR(48)
X = p($VAR(42), $VAR(43), p($VAR(44), $VAR(43), $VAR(44), $VAR(45), [$VAR(46),$VAR(42),$VAR(47),$VAR(47),$VAR(48),$VAR(45)]))
Y = 49

yes

numbervars(X) operates in the same way as numbervars(X,0,_)

?- X=p(A,B,p(C,B,C,D,[E,A,F,F,G,D])), numbervars(X).
A = $VAR(0)
B = $VAR(1)
C = $VAR(2)
D = $VAR(3)
E = $VAR(4)
F = $VAR(5)
G = $VAR(6)
X = p($VAR(0), $VAR(1), p($VAR(2), $VAR(1), $VAR(2), $VAR(3), [$VAR(4),$VAR(0),$VAR(5),$VAR(5),$VAR(6),$VAR(3)]))

yes

?- X=p(A,B,p(x(x(x(A,p(C),B,p(D,B))))),E), numbervars(X,0,Y).
A = $VAR(0)
B = $VAR(1)
C = $VAR(2)
D = $VAR(3)
E = $VAR(4)
X = p($VAR(0), $VAR(1), p(x(x(x($VAR(0), p($VAR(2)), $VAR(1), p($VAR(3), $VAR(1)))))), $VAR(4))
Y = 5

yes

?- X=p(A,B,p(x(x(x(A,p(C),B,p(D,B))))),E), numbervars(X,-42,Y).
A = $VAR(-42)
B = $VAR(-41)
C = $VAR(-40)
D = $VAR(-39)
E = $VAR(-38)
X = p($VAR(-42), $VAR(-41), p(x(x(x($VAR(-42), p($VAR(-40)), $VAR(-41), p($VAR(-39), $VAR(-41)))))), $VAR(-38))
Y = -37

yes