Re: Variable Names in Prolog
On 2012-02-21, Nada Sharaf <email@example.com> wrote:
> I have been working for a while and I have reached a point where I can
> extract the constraints from the term using the initial variable names
> (Since I am using the CHR library). However, there seems to be a weird
> general behavior with CHR regardless so I went back to try a simple
> example to reach the problem and here is what I found.
> First of all, this is the program I used (the leq handler)
> handler leq.
> constraints leq/2.
> reflexivity @ leq(X,X) <=> true.
> antisymmetry @ leq(X,Y), leq(Y,X) <=> print('here'),nl, X = Y.
> transitivity @ leq(X,Y), leq(Y,Z) ==> leq(X,Z).
> Here are the attempts:
> 1 ?- leq(A,B),leq(B,C),leq(C,A).
> A = B, B = C ;
This seems to be completely fine with me. I still have no clue what
you try to achieve.
> 2 ?-
What is happening here? You are trying to call into the system code to
give names to variables before activating the constraints? That won't
work, because '$VAR'('A') is not a variable and CHR's game is based on
attributed variables, which needs variables to start with.
And anyway, why would you want that? At the end of the story you have
that A==B==C. In Prolog's internals, that means there is only one variable
left. So, what name does this variable have? 'A' or 'B' or 'C'?
What you can do is create an attribute, say name using
put_attr(A, name, 'A').
then you need to define an attribute unification hook that says what
must happen if two named variables unify, etc. Then you can fetch the
name using get_attr(Var, name, Name). I don't see much point in it, and
it may affect how the program behaves if the program queries the
presense of attributes. And still, note that after A = 'Hello World', the
term is simply 'Hello World'. There is no place to stort the information
that this atom has been a variable named 'A'.
In other words, Prolog isn't a set of variables with values, but it is
a set of terms that may start as `unknown' (i.e., variable) and these
may become known (integer, atom, compound, ...). You cannot ask a
non-var term `for which variable are you the value'.
Hope this helps (either understanding a misconception or in describing
what you really want).
Cheers --- Jan
> So there seems to be a problem when using bind_vars while using the
> constraints, one of them is ignored (and it could be noticed since
> here was printed once instead of twice). I am correct or am I missing
> out on something?
> Thank you very much,