How can I bind the same variables together in prolog

Viewed 45

So, I'm relatively new to prolog and was wondering if it is possible to bind 2 variables together in a matrix and would they update simultaneously.

For example, I have this

X = [[_,_], [_,_]].
X = [[_23838, _23844], [_23856, _23862]].

But, I was wondering is it possible to do something like this, almost unifying the diagonal elements in the matrix

X = [[_,_], [_,_]].
X = [[_23838, _23844], [_23856, _23838]].

Assuming the above is possible if _23838 was later bound then would they be the same value?

For example

X = [[5,_], [_,5]].
X = [[5, _23844], [_23856, 5]].
1 Answers

Yes you can.

Just name the variables instead of using the don't care name (aka. "the anonymous variable"), i.e. _:

X = [[A,B], [C,A]].

This expresses the constraint that the value at (1,1) must be the same as the value at (2,2).

You can also start off with "all variables different" and later force them to be equal by unification:

X = [[A,B], [C,D]], A=D

Conversely, you can state that you do not want to see equality in a result (all proofs that can only continue by making A and B equal will fail after dif/2):

X = [[A,B], [C,D]], dif(A,B).

dif/2 is of some interest.

Related