succ(X,Y) - alternative if X and Y are var

I’m searching some alternative version of the method succ/2 in that way the call of succ(X,Y) don’t give some instantiation_error, so it is possible to recursivly call succ(Xi,Xi+1) multiple times first and instantiate the variable later one. Is their a way to do it?

1 Like

clpfd: SWI-Prolog manual

?- use_module(library(clpfd)).
true.

?- X #= Y + 1.
Y+1#=X.

There is also the simplistic successor (“peano”) arithmetic - example: Prolog Successor Arithmetic - Stack Overflow

1 Like

Thanks, but is their a possibility to combine it with some format output like:

?- X #= Y+1, format(atom(A), ‘~w’, [Y]), Y #= 1.

actual output:


?- X #= Y+1, format(atom(A), '~w', [Y]), Y #= 1.
X = 2,
Y = 1,
A = '_48684'.

desired output:


?- X #= Y+1, format(atom(A), '~w', [Y]), Y #= 1.
X = 2,
Y = 1,
A = '1'.

without changing the order of the commands?

You could use freeze/2 to delay the format/2 call until Y is bound. Maybe this is what you’re looking for?

?- X #= Y+1, freeze(Y, format(atom(A), '~w', [Y])), Y #= 1.
X = 2,
Y = 1,
A = '1'.

Note that if you do this, A will remain a variable until Y receives a value. If that never happens, A will also remain unbound forever.

2 Likes

Thanks, the combination of that two solutions helped me a lot.