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

**URL:** <https://swi-prolog.discourse.group/t/succ-x-y-alternative-if-x-and-y-are-var/5278>\
**Category:** Help!\
**Created:** [April 14, 2022, 3:32pm UTC](https://swi-prolog.discourse.group/t/succ-x-y-alternative-if-x-and-y-are-var/5278 "2022-04-14T15:32:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Losbarthos](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/losbarthos/32/4037_2.png) [@Losbarthos](https://swi-prolog.discourse.group/u/Losbarthos)\
**Post date:** [April 14, 2022, 3:32pm UTC](https://swi-prolog.discourse.group/t/succ-x-y-alternative-if-x-and-y-are-var/5278/1 "2022-04-14T15:32:16Z")

</div>

I’m searching some alternative version of the method [succ/2](https://www.swi-prolog.org/pldoc/man?predicate=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?

---

<div class="post-metadata">

**Author:** ![brebs](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@brebs](https://swi-prolog.discourse.group/u/brebs)\
**Post date:** [April 14, 2022, 4:00pm UTC](https://swi-prolog.discourse.group/t/succ-x-y-alternative-if-x-and-y-are-var/5278/2 "2022-04-14T16:00:05Z")

</div>

clpfd: [SWI-Prolog manual](https://www.swi-prolog.org/man/clpfd.html)

```prolog
?- 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](https://stackoverflow.com/questions/47142113/prolog-successor-arithmetic)

---

<div class="post-metadata">

**Author:** ![Losbarthos](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/losbarthos/32/4037_2.png) [@Losbarthos](https://swi-prolog.discourse.group/u/Losbarthos)\
**Post date:** [April 14, 2022, 4:33pm UTC](https://swi-prolog.discourse.group/t/succ-x-y-alternative-if-x-and-y-are-var/5278/3 "2022-04-14T16:33:53Z")

</div>

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:

```prolog

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

```

desired output:

```prolog

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

```

without changing the order of the commands?

---

<div class="post-metadata">

**Author:** ![dgelessus](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/dgelessus/32/3106_2.png) [@dgelessus](https://swi-prolog.discourse.group/u/dgelessus)\
**Post date:** [April 14, 2022, 5:18pm UTC](https://swi-prolog.discourse.group/t/succ-x-y-alternative-if-x-and-y-are-var/5278/4 "2022-04-14T17:18:55Z")

</div>

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

```prolog
?- 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.

---

<div class="post-metadata">

**Author:** ![Losbarthos](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/losbarthos/32/4037_2.png) [@Losbarthos](https://swi-prolog.discourse.group/u/Losbarthos)\
**Post date:** [April 14, 2022, 5:28pm UTC](https://swi-prolog.discourse.group/t/succ-x-y-alternative-if-x-and-y-are-var/5278/5 "2022-04-14T17:28:15Z")

</div>

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