Careful with the $/1

This API raised my attention. Was expecting it has only 3 arguments, but it has 4 arguments as it stands. I don’t get it exactly why this is needed, but seems to be related to $Var expansion.

expand_query(+Query, -Expanded, +Bindings, -ExpandedBindings)
https://www.swi-prolog.org/pldoc/doc_for?object=expand_query/4

Now there is a funny incoherence, substitution of equals for equals doesn’t work anymore. Since $/1 has also been recently introduced as a built-in. I now get:

?- X = (1=1;2=2).
X =  (1=1;2=2).

?- Y = $X, $Y.
Y = X, X = Y, Y =  (1=1;2=2) .

?- Y = $X, $((1=1;2=2)).
ERROR: Goal 1=1;2=2 succeeded with a choice point
ERROR: In:
ERROR:   [10] '<meta-call>'(user:user: ...)
ERROR:    [9] toplevel_call(user:user: ...) at /opt/Software/Logic2/SWI-Prolog.app/Contents/swipl/boot/toplevel.pl:1115

Somehow goal position $Var is differently expanded than term position $Var? So that the $Y doesn’t get executed? But here below $X gets executed? And $/1 built-in is ignored!!!

?- $X.
X =  (1=1;2=2) ;
X =  (1=1;2=2).

omg I never knew about the $X functionality before! That’s amazing. And also the caution about using $/1 at the toplevel is useful to keep in mind, thanks :grin: