[cells-devel] A question about the use of vectors in CELLS
Larry Clapp
larry at theclapp.org
Sun May 17 22:05:49 UTC 2009
On Sat, May 16, 2009 at 04:23:41PM +0200, Peder Chr. Nørgaard wrote:
> I am attempting to use a vector as the value in a slot. It does not
> work well in first attempt:
>
> CL-USER> (use-package :cells)
> T
> CL-USER> (defparameter a1 (make-instance 'model :value (c-in #(1 2 3 4))))
> A1
> CL-USER> (defparameter a2 (make-instance 'model :value (c? (apply #'+
> (map 'list #'identity (value a1))))))
> A2
> CL-USER> (value a2)
> 10
> CL-USER> (setf (elt (value a1) 1) 5)
> 5
> CL-USER> (value a1)
> #(1 5 3 4)
> CL-USER> (value a2)
> 10
>
> The VALUE slot of A2, depending on (VALUE A1) is not changing when
> (VALUE A1) is, shall we call it "changed in place".
>
> The cells version is
> from :pserver:anonymous:anonymous at common-lisp.net:/project/cells/cvsroot.
>
> The Lisp system is SBCL is 1.0.18.0-2.
>
> I would like to enquire whether I am simply doing something
> trivially wrong, or whether this is something that simply cannot be
> done without a major CELLS extension for support of vector values
> (like, FAMILY, can be viewed as a kind of support for list values).
> The latter answer may force me to reconsider an implementation that
> I had in mind.
I asked a similar question almost exactly a year ago. Hmmm. :)
Anyway, see
http://common-lisp.net/pipermail/cells-devel/2008-May/002035.html .
The general answer hasn't changed since then: either don't do that, or
recons the sequence, or make the cell formula depend on something else
that changes directly in addition to the stuff it actually depends on.
(In last year's thread, Peter Hildebrandt noted that this last option
is how cells-store works.)
I played with the last option a bit today, and came up with this
(which may or may not work very well in real usage, but the toy
example below works):
(defmodel change-counter ()
((count :initform (c-in 0) :accessor count-of)))
(defmethod kick ((self change-counter))
(incf (count-of self)))
(defmacro change-watcher ((model) &body body)
`(progn
(count-of ,model)
, at body))
(defmacro with-changes ((&rest models) &body body)
`(unwind-protect
(progn , at body)
(with-integrity (:change)
(dolist (model (list , at models))
(kick model)))))
Example (Lispworks, if it matters):
CELLS-PLAY 217 > (defmodel test (change-counter)
((slot :initarg :slot :accessor slot-of)))
NIL
CELLS-PLAY 218 > (defparameter a1 (make-instance 'test :slot (c-in #(1 2 3 4))))
A1
CELLS-PLAY 219 > (defparameter a2 (make-instance 'test :slot (c?
(change-watcher (a1)
(format t "recalc a2's s~%")
(apply '+ (coerce (slot-of a1) 'list))))))
recalc a2's s
A2
CELLS-PLAY 220 > (slot-of a2)
10
CELLS-PLAY 221 > (with-changes (a1)
(setf (aref (slot-of a1) 1) 5))
recalc a2's s
5
CELLS-PLAY 222 > (slot-of a2)
13
CELLS-PLAY 223 > (slot-of a1)
#(1 5 3 4)
Hope that's helpful.
-- Larry
More information about the cells-devel
mailing list