Newsgroups: comp.lang.prolog
Path: cantaloupe.srv.cs.cmu.edu!rochester!udel!news.mathworks.com!news.kei.com!bloom-beacon.mit.edu!news.starnet.net!wupost!howland.reston.ans.net!math.ohio-state.edu!news.cyberstore.ca!vanbc.wimsey.com!unixg.ubc.ca!news.bc.net!newsserver.sfu.ca!fornax!cs.sfu.ca!serge
From: serge@cs.sfu.ca (Serge Le Huitouze)
Subject: setarg/3 (Was: speed of prolog)
In-Reply-To: thomasl@arnold.csd.uu.se's message of 13 Jun 1995 15:29:08 GMT
Message-ID: <SERGE.95Jun16150840@maroilles.cs.sfu.ca>
Sender: news@cs.sfu.ca
Organization: School of Computing Science, Simon Fraser University
References: <THOMASL.95Jun13172908@arnold.csd.uu.se>
Date: 16 Jun 1995 22:08:40 GMT
Lines: 42


Thomas Lindgren writes:
 > First, if you are willing to use setarg/3, then you get updatable,
 > definitely non-logical but still backtrackable vectors of size up to
 > the max.arity of terms.

First, it would be nice to have it *just* correct.

I guess the following example is not new, but might be interesting to
recall anyway.

% ?- T = f(X), setarg(1,T,33), R=result(T,X).
%
%T = f(33)
%X = _28
%R = result(f(33),_28)
%yes
% ?- [user].
%foo(R) :- T = f(X), setarg(1,T,33), R=result(T,X).
%^D
%yes
%| ?- foo(R).
%
%R = result(f(33),33)
%yes

Yes ? Well ...

In the implementation I am using, this example works properly only as a
query, not as a compiled rule.

The expected behavior is to change the first argument of T, not X !

Let me explain briefly (and vaguely) what happens for non-WAM gurus:
The problem is the variable X is not represented as an object per se. It
is just represented by the fact the first subterm of functor f/1 is a
reference to itself (that's how variables are represented in the WAM).
Then, updating the first argument of f/1 also updates the value of X :-(

By the way, this seems pretty easy to correct (maybe it has even be corrected
already in some --many ?-- versions of ISO-Prolog).

