Newsgroups: comp.lang.prolog
Path: cantaloupe.srv.cs.cmu.edu!rochester!udel!gatech!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.95Jun16143017@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 21:30:17 GMT
Lines: 48


In article <THOMASL.95Jun13172908@arnold.csd.uu.se> thomasl@arnold.csd.uu.se (Thomas Lindgren) writes:

 > > be inherently slow because you have to implement arrays using lists,
 > > and the most efficient methods for doing so involve logarithmic access
 > > time, where constant access time is available with arrays in other
 > > languages.
 >
 > 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.

Sorry, but it would still be nice if setarg/3 was *just* correct !

I don't think the following example is new, but I guess many setarg' users
are not aware of it.

% ?- [user].
% ?- T = f(X), setarg(1,T,33), R=result(T,X).
%
%T = f(33)
%X = _28
%R = result(f(33),_28)
%yes
%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 ...

The problem is that the variable X is affected by the update in T, which
obviously should not be the case.

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

The problem is X doesn't exist per se. It is just represented by the first
cell of functor f/1 pointing to itself. Thus, updating the first argument
of f/1 has the undesirable effect of also updating X :-(

Maybe some systems have corrected that (this seems quite simple thing to fix:
in 'setarg/3' just test if the updated location represents a free variable),
but some haven't.

