![]() ![]() ![]() |
![]() |
:=
Entity: predicate
<>=
Entity: predicate
Usage:
X is Y
X := Y
X <>= Y
The predicates is, := (colon equal) and <>= (diamond equal) calculate the expression Y and bound it with the variable X. The predicates := and <>= can also be used to set the value of elements of array (example: array(N) := Y) or to set properties of OLE server (example: Server.property := Y).
Where is the difference?
The evaluation := (colon equal) is the simplest one. It simply throws out the old value of the variable X and sets its new value to the value of the expression Y. The effect of this evaluation will remain after backtracking (i.e. it is stable on backtracking). So := works in the same way as the normal evaluation which you have in common programming languages like C and Basic. This evaluation is not typical for Prolog where you usually make calculations with backtracking. Anyway, this predicate is useful because it gives you the possibility to keep some values after the backtracking (usually you keep these values in global variables or in arrays).
More typical for Prolog are the evaluations is and <>= because after the backtracking they will return the previous value of X. This will give you the possibility to make calculations with backtracking. Usually in one Prolog program you are looking for the answer by searching in the tree of all possible answers (name of this tree is the SLD tree). When you find answer in the SLD tree you will like to see only this answer and the effect of the program to be the same as if it found this answer directly without wandering in the SLD tree. So, if you make calculations you will like to see only the result of these calculations which were made on the right path. All calculations which were made on the wrong (backtracking) paths should be ignored. So this will be the effect if you use the evaluation is or <>=.
The difference between is and <>= is that is requires from X to be free variable in order to bind it with the value of Y. On the contrary, <>= does not need this because it will ignore the old value of X and will replace it with the value of Y (in the same way as := is doing). Anyway, <>= is different from := because after backtracking <>= will restore the old value of X district from :=, which will not do anything on backtracking.
How does <>= set property of OLE server. First, it calls the method GET in order to get the old value of the property. After that, it calls method SET in order to set the new value. After that, on backtracking, it calls the method SET again in order to restore the old value.
The evaluation <>= is typical for Strawberry Prolog. It is in some sense between is and :=. In some sense <>= is more powerful than is because it can change the old value of the variable X. For example, if you want to make a counter which is restored after backtracking you cannot use is because C is C+1 will fail but C <>= C+1 will work as a counter whose value is restored after backtracking. A good example of the use of <>= is the Sudoku.pro game where <>= is used in order to generate the Sudoku task.
Which one of these three evaluations to use?
If you don't know which one or if it doesn't matter which one use the predicate is. This is the most typical Prolog evaluation and very often it gives the result that the program works despite the fact that we do not know why. In other programming languages you cannot make a program which works accidentally but in Prolog it is possible and this is the main advantage of this language.
Example for <>= (Try to replace <>= with := and with is.)
?- s(X), X=a, s(X), X<>=b, s(X), fail.
s(X).
s(X) :- write(X), nl, fail.