Any programmer with some experience in Prolog knows the advantages of use unary notation for numbers. By example, if we represent a number as list of 1" ("4" is list "[1,1,1,1]" and so on), we can define:
unary_succ(X,[1|X]).
the following queries does what is expected:
?- X=[1,1],unary_succ(X,Y).
X = [1, 1],
Y = [1, 1, 1].
?- unary_succ(X,Y),X=[1,1].
X = [1, 1],
Y = [1, 1, 1].
?- unary_succ(X,Y),Y=[1,1].
X = [1],
Y = [1, 1].
In this way, statement unary_succ(X,Y) "binds" X and Y in a way that, if after the fact is stated, one of these variables is bound to a value, the other one does.
However, this behaviour is not possible if we use the internal number representation:
?- X=2,succ(X,Y).
X = 2,
Y = 3.
?- succ(X,Y),X=2.
ERROR: succ/2: Arguments are not sufficiently instantiated
?- succ(X,Y),Y=2.
ERROR: succ/2: Arguments are not sufficiently instantiated
In my opinion, it will be very useful that previous statements and similar ones does what is expected. That is, we need to link two variables in a way that, when one of them is bound to a value, the other does following the previously established rule.
My questions are:
a) some easy way to do that in Prolog.
b) if not possible, any other programming language that supports this feature?
Any comment is welcome.
Thanks to everybody.
* Addendum I *
Another example is:
user_id(john,1234).
user_id(tom,5678).
and queries:
X=john,user_id(X,Y).
user_id(X,Y),X=john
that currently are solved by backtracking.