Newsgroups: comp.lang.prolog
Path: cantaloupe.srv.cs.cmu.edu!das-news2.harvard.edu!news2.near.net!news.mathworks.com!news.alpha.net!uwm.edu!spool.mu.edu!torn!news.bc.net!newsserver.sfu.ca!fornax!jamie
From: jamie@cs.sfu.ca (Jamie Andrews)
Subject: Re: [Q] OO implementation
Message-ID: <1995Mar28.180626.26887@cs.sfu.ca>
Organization: Faculty of Applied Science, Simon Fraser University
References: <3krgjh$m3t@erinews.ericsson.se>
Date: Tue, 28 Mar 1995 18:06:26 GMT
Lines: 34

In article <3krgjh$m3t@erinews.ericsson.se>,
Jonas Nygren <ehsjony@ehs.ericsson.se> wrote:
>How can object state alteration be implemented?

     Markus Fromherz's approach in OL is to represent the object
state as an association list, ie. as
[field1=value1,field2=value2,...].  For any given field, there
may be more than one value such that field=value is in the list;
if so, the first one is taken to be the current value.  Update
is then effected by just adding the new field=value pair to the
front of the list.  You pass the update predicate the current
a-list, and get a new a-list back.

     Fromherz says that this is by far the most efficient
implementation.  I tend to agree:  no structures have to get
altered drastically, you just have to create a few more cons
cells.  Previous versions of the object may be hanging around on
the backtrack stack, but they point into the same structure.
And trying to do all this with assert/retract or some other
imperative means doesn't result in as much savings as you might
think.

     The only thing you have to worry about is if you are doing
a very large amount of updates; in this case you may want to
"compact" the object states occasionally by deleting duplicate
values.  This is what I did for my OL application.  I think it
only helped because I was not backtracking a lot, though; if you
were backtracking a lot, it might even hurt performance due to
your having to build a new copy of the a-list each time you
compact.

--Jamie.
  jamie@cs.sfu.ca
"Simon Fraser University would make an elegant ruin" -- Bruno Freschi
