* References/locations
@ 2008-08-08 1:50 Maciek Godek
2008-08-10 7:51 ` References/locations Stephen Compall
0 siblings, 1 reply; 10+ messages in thread
From: Maciek Godek @ 2008-08-08 1:50 UTC (permalink / raw
To: guile-user
Hi,
it's me again, asking silly questions.
This time I would like to know if there's
a simple way to store a reference to
a variable in another variable -- say,
an element of vector or hash table.
I imagine this could look like this:
(define v #(1 2 3))
(define v1 (vector-location v 1))
v1
: 2
(set! v1 10)
v
: #(1 10 3)
I've tried to do it using a "procedure with
setter", but the problem is that set! doesn't
evaluate its first argument (as long as it's a
symbol), so I'd have to wrap everything
up in macros to obtain:
(set! (vector-location v 1) 10)
Besides I think that the names "hash-ref"
and "vector-ref" are confusing, since they
don't return references, but values (therefore
the names like "vector-get" or "hash-get" would
be more apropreate)
I also wonder if there's any point for allowing
locations to any sorts of variables (similar to
pointers in C or pointers to C++), that is,
(define x 10)
(define y (location x))
(set! y 20)
x
: 20
I expect that this idea isn't new in the world of
lisp, but I didn't find any discussion on the web.
There are certainly some issues related with locations:
- they require special behavior of set! and define:
(set! y 20) could work in two different ways,
depending on whether y is a location or not
- what about "locations of locations"?
- how would it affect performance?
- what about multiple threads?
Regards
M.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: References/locations
2008-08-08 1:50 References/locations Maciek Godek
@ 2008-08-10 7:51 ` Stephen Compall
2008-08-15 9:35 ` References/locations Maciek Godek
[not found] ` <e2ceda030808141732r524fc0cehb8ae7c6007550ea6@mail.gmail.com>
0 siblings, 2 replies; 10+ messages in thread
From: Stephen Compall @ 2008-08-10 7:51 UTC (permalink / raw
To: Maciek Godek; +Cc: guile-user
"Maciek Godek" <pstrychuj@gmail.com> writes:
> I've tried to do it using a "procedure with
> setter", but the problem is that set! doesn't
> evaluate its first argument (as long as it's a
> symbol), so I'd have to wrap everything
> up in macros to obtain:
> (set! (vector-location v 1) 10)
Actually, if you (use-syntax (ice-9 syncase)), you should be able to
define lexical symbol-macros that expand a single symbol, even in a
set! place, something like:
(define v #(1 2 3))
(define-syntax v1
(lambda (stx)
(syntax-case stx ()
(_ v))))
Obviously aliasing will serve:
(define-syntax alias-syntax
(syntax-rules ()
((_ form)
(lambda (stx)
(syntax-case stx ()
(_ (syntax form)))))))
(define-syntax defalias
(syntax-rules stx ()
((_ new form)
(define-syntax new (alias-syntax form)))))
(define-syntax let-alias
(syntax-rules ()
((_ ((new form) ...) body ...)
(let-syntax ((new (alias-syntax form)) ...) body ...))))
(defalias v1 (vector-ref v 1))
(set! v1 42)
(vector-ref v 1) ; => 42
--
a Mr. Fleming wishes to study bugs in smelly cheese; a Polish woman
wishes to sift through tons of Central African ore to find minute
quantities of a substance she says will glow in the dark; a Mr. Kepler
wants to hear the songs the planets sing.
--Carl Sagan, "The Demon-Haunted World"
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: References/locations
2008-08-10 7:51 ` References/locations Stephen Compall
@ 2008-08-15 9:35 ` Maciek Godek
[not found] ` <e2ceda030808141732r524fc0cehb8ae7c6007550ea6@mail.gmail.com>
1 sibling, 0 replies; 10+ messages in thread
From: Maciek Godek @ 2008-08-15 9:35 UTC (permalink / raw
To: guile-user
Stephen Compall:
> Actually, if you (use-syntax (ice-9 syncase)), you should be able to
> define lexical symbol-macros that expand a single symbol, even in a
> set! place, something like:
>
> (define v #(1 2 3))
> (define-syntax v1
> (lambda (stx)
> (syntax-case stx ()
> (_ v))))
I don't really get this syntax-rules. Do you know of any good tutorial?
[I find the description in the revised report too difficult to understand]
> Obviously aliasing will serve:
>
> (define-syntax alias-syntax
> (syntax-rules ()
> ((_ form)
> (lambda (stx)
> (syntax-case stx ()
> (_ (syntax form)))))))
>
> (define-syntax defalias
> (syntax-rules stx ()
> ((_ new form)
> (define-syntax new (alias-syntax form)))))
ERROR: invalid syntax (syntax-rules stx () ((_ new form)
(define-syntax new (alias-syntax form))))
=> error-in-evaluation
(That's what guile says)
> (define-syntax let-alias
> (syntax-rules ()
> ((_ ((new form) ...) body ...)
> (let-syntax ((new (alias-syntax form)) ...) body ...))))
>
> (defalias v1 (vector-ref v 1))
>
> (set! v1 42)
> (vector-ref v 1) ; => 42
Could you explain it in more detail? Why should it work?
(Or at least give me a link with such explanations)
Thanks in advance
M.
^ permalink raw reply [flat|nested] 10+ messages in thread
[parent not found: <e2ceda030808141732r524fc0cehb8ae7c6007550ea6@mail.gmail.com>]
[parent not found: <cmu-lmtpd-5833-1218211546-4@mail-imap1.uio.no>]
* Re: References/locations
[not found] <cmu-lmtpd-5833-1218211546-4@mail-imap1.uio.no>
@ 2008-08-08 18:34 ` Kjetil S. Matheussen
2008-08-10 23:00 ` References/locations Maciek Godek
2008-08-15 10:14 ` References/locations Maciek Godek
0 siblings, 2 replies; 10+ messages in thread
From: Kjetil S. Matheussen @ 2008-08-08 18:34 UTC (permalink / raw
To: guile-user
"Maciek Godek":
> Subject: References/locations
> To: guile-user@gnu.org
> Message-ID:
> <e2ceda030808071850uc1640e6kf4ffc33699143fd2@mail.gmail.com>
> Content-Type: text/plain; charset=ISO-8859-1
>
> Hi,
> it's me again, asking silly questions.
Not at all, your questions are very good. :-)
> This time I would like to know if there's
> a simple way to store a reference to
> a variable in another variable -- say,
> an element of vector or hash table.
>
> I imagine this could look like this:
> (define v #(1 2 3))
> (define v1 (vector-location v 1))
> v1
> : 2
> (set! v1 10)
> v
> : #(1 10 3)
>
> I've tried to do it using a "procedure with
> setter", but the problem is that set! doesn't
> evaluate its first argument (as long as it's a
> symbol), so I'd have to wrap everything
> up in macros to obtain:
> (set! (vector-location v 1) 10)
>
> Besides I think that the names "hash-ref"
> and "vector-ref" are confusing, since they
> don't return references, but values (therefore
> the names like "vector-get" or "hash-get" would
> be more apropreate)
>
Never thought about that, but it sounds
correct. vector-get and hash-get would
probably be mroe apropreate names.
> I also wonder if there's any point for allowing
> locations to any sorts of variables (similar to
> pointers in C or pointers to C++), that is,
> (define x 10)
> (define y (location x))
> (set! y 20)
> x
> : 20
>
Well, here's a relatively clean way, I think:
(define-macro (location name)
(define new-val (gensym))
`(lambda (,new-val)
(set! ,name ,new-val)))
(define old-set! set!)
(define-macro (set! a b)
`(if (procedure? ,a) ;; Needs a better check.
(,a ,b)
(old-set! ,a ,b)))
guile> (define x 10)
guile> (define y (location x))
guile> (set! y 20)
guile> x
20
guile>
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: References/locations
2008-08-08 18:34 ` References/locations Kjetil S. Matheussen
@ 2008-08-10 23:00 ` Maciek Godek
2008-08-15 10:14 ` References/locations Maciek Godek
1 sibling, 0 replies; 10+ messages in thread
From: Maciek Godek @ 2008-08-10 23:00 UTC (permalink / raw
To: Kjetil S. Matheussen; +Cc: guile-user
Kjetil:
>> Hi,
>> it's me again, asking silly questions.
>
> Not at all, your questions are very good. :-)
That's werid. Every time I write a post I've got the
strange feeling that I'm thinking in the way that
should be forbidden :)
I can give you the background of this idea.
I've been trying to implement a vector-based
object-oriented system (I'm rather reluctant towards
goops). I imagine that it could be used in the
following manner (borrowed from C++):
(define sphere
(class
() ;; public slots
(x y radius) ;; private slots
((move (dx dy) ;; methods
(set! x (+ x dx))
(set! y (+ y dy)))
(scale (factor)
(set! radius (* radius factor))))))
Obviously, the "class" macro should convert methods
to lambdas. For "move" that would be:
(lambda(self dx dy)
(let((x (vector-loc self (get-hash-table properties 'x)))
(y (vector-loc self (get-hash-table properties 'y)))
(radius (vector-ref self (get-hash-table properties 'radius))))
(set! x (+ x dx))
(set! y (+ y dy)))
Obviously, the hash table reference would have to be
evaluated during macro expand to provide the maximum
efficiency.
(If you have any other solutions to this problem, I'm open
to suggestions)
>> Besides I think that the names "hash-ref"
>> and "vector-ref" are confusing, since they
>> don't return references, but values (therefore
>> the names like "vector-get" or "hash-get" would
>> be more apropreate)
>>
>
> Never thought about that, but it sounds
> correct. vector-get and hash-get would
> probably be mroe apropreate names.
On the other hand the world of scheme seems
to have gotten used to such convention.
> Well, here's a relatively clean way, I think:
>
> (define-macro (location name)
> (define new-val (gensym))
> `(lambda (,new-val)
> (set! ,name ,new-val)))
>
> (define old-set! set!)
>
> (define-macro (set! a b)
> `(if (procedure? ,a) ;; Needs a better check.
> (,a ,b)
> (old-set! ,a ,b)))
>
>
> guile> (define x 10)
> guile> (define y (location x))
> guile> (set! y 20)
> guile> x
> 20
> guile>
That's amazing! Whenever I think of something (and imagine
how difficult would it be to implement), you come up with a
solution taking only a few lines of code :)
(I only have to think how would it perform in practice)
Thanks
M.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: References/locations
2008-08-08 18:34 ` References/locations Kjetil S. Matheussen
2008-08-10 23:00 ` References/locations Maciek Godek
@ 2008-08-15 10:14 ` Maciek Godek
2008-08-15 13:55 ` References/locations Clinton Ebadi
1 sibling, 1 reply; 10+ messages in thread
From: Maciek Godek @ 2008-08-15 10:14 UTC (permalink / raw
To: Kjetil S. Matheussen; +Cc: guile-user
Kjetil:
> Well, here's a relatively clean way, I think:
>
> (define-macro (location name)
> (define new-val (gensym))
> `(lambda (,new-val)
> (set! ,name ,new-val)))
>
> (define old-set! set!)
>
> (define-macro (set! a b)
> `(if (procedure? ,a) ;; Needs a better check.
> (,a ,b)
> (old-set! ,a ,b)))
>
>
> guile> (define x 10)
> guile> (define y (location x))
> guile> (set! y 20)
> guile> x
> 20
> guile>
I just realized that this is only a partial solution.
Continuing the above example,
guile> y
#<procedure #f (#syntmp-\ g1336-76}#)>
I don't know how to solve it: the problem is that
you actually can't bind a function call to a variable.
But even without it, the line
(define old-set! set!)
breaks:
ERROR: invalid syntax set!
I've been thinking of implementing this "location" stuff
as a smob, but you've got the point that it is (probably)
impossible to implement the location system without
redefining set! and define.
Regards
M.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: References/locations
2008-08-15 10:14 ` References/locations Maciek Godek
@ 2008-08-15 13:55 ` Clinton Ebadi
2008-08-18 8:09 ` References/locations Maciek Godek
0 siblings, 1 reply; 10+ messages in thread
From: Clinton Ebadi @ 2008-08-15 13:55 UTC (permalink / raw
To: Maciek Godek; +Cc: guile-user, Kjetil S. Matheussen
"Maciek Godek" <pstrychuj@gmail.com> writes:
> I've been thinking of implementing this "location" stuff
> as a smob, but you've got the point that it is (probably)
> impossible to implement the location system without
> redefining set! and define.
You may want to read a few documents on functional programming to see
why your locations concept is not good. There are a number of
additional issues as well: one that comes to mind is the interaction
of the garbage collector with pointers into the middle of a vector.
What is so wrong with forms like `(set! (vector-ref foo index) ...)'?
It's generally bad practice to go around destructively modifying data
structures anyway.
--
Corinne: this is why we should have designated bath buddies
Corinne: to get places you cant reach because youre slippery and in
case you get a lil tooo slippery and crack your head open
someone can call the coast guard and save you
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: References/locations
2008-08-15 13:55 ` References/locations Clinton Ebadi
@ 2008-08-18 8:09 ` Maciek Godek
2008-08-18 16:15 ` References/locations Clinton Ebadi
0 siblings, 1 reply; 10+ messages in thread
From: Maciek Godek @ 2008-08-18 8:09 UTC (permalink / raw
To: Clinton Ebadi; +Cc: guile-user, Kjetil S. Matheussen
Clinton Ebadi:
>> I've been thinking of implementing this "location" stuff
>> as a smob, but you've got the point that it is (probably)
>> impossible to implement the location system without
>> redefining set! and define.
>
> You may want to read a few documents on functional programming to see
> why your locations concept is not good. There are a number of
> additional issues as well: one that comes to mind is the interaction
> of the garbage collector with pointers into the middle of a vector.
That's what I wanted to hear :)
(The other thing is the ambiguity: if we create a location to another
variable, say (set! y (location x)), then we can't redefine the meaning
of y easily:
(set! y 5) ; sets x to 5, which is unexpected behavior of set!
I think there would be many many more disadvantages)
> What is so wrong with forms like `(set! (vector-ref foo index) ...)'?
In scheme the only problem is that they don't work, unless we
redefine vector-ref:
(define vector-get vector-ref)
(define vector-ref (make-procedure-with-setter vector-get vector-set!))
> It's generally bad practice to go around destructively modifying data
> structures anyway.
Perhaps I'm not an experienced lisper, but I'd rather say it's generally
a bad practice to generalize overly: everything depends on what you're
doing. I'm currently working on an object system that is based on a local
state and it would be a stubbornness to implement it using purely
functional paradigm.
Regards
M.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: References/locations
2008-08-18 8:09 ` References/locations Maciek Godek
@ 2008-08-18 16:15 ` Clinton Ebadi
0 siblings, 0 replies; 10+ messages in thread
From: Clinton Ebadi @ 2008-08-18 16:15 UTC (permalink / raw
To: Maciek Godek; +Cc: guile-user, Kjetil S. Matheussen
"Maciek Godek" <pstrychuj@gmail.com> writes:
>> What is so wrong with forms like `(set! (vector-ref foo index) ...)'?
>
> In scheme the only problem is that they don't work, unless we
> redefine vector-ref:
> (define vector-get vector-ref)
> (define vector-ref (make-procedure-with-setter vector-get vector-set!))
(use-modules (srfi srfi-17))
(see "(guile)SRFI-17" in infotex)
--
emacsen: every copy of Emacs comes with a bag of pot and 5 hits of acid
emacsen: and a hotel coffee maker
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2008-08-21 5:05 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-08-08 1:50 References/locations Maciek Godek
2008-08-10 7:51 ` References/locations Stephen Compall
2008-08-15 9:35 ` References/locations Maciek Godek
[not found] ` <e2ceda030808141732r524fc0cehb8ae7c6007550ea6@mail.gmail.com>
[not found] ` <m2wsihltfv.fsf@member.fsf.org>
[not found] ` <e2ceda030808200441q52d9858bv63323055e0f6308a@mail.gmail.com>
2008-08-21 5:05 ` References/locations Stephen Compall
[not found] <cmu-lmtpd-5833-1218211546-4@mail-imap1.uio.no>
2008-08-08 18:34 ` References/locations Kjetil S. Matheussen
2008-08-10 23:00 ` References/locations Maciek Godek
2008-08-15 10:14 ` References/locations Maciek Godek
2008-08-15 13:55 ` References/locations Clinton Ebadi
2008-08-18 8:09 ` References/locations Maciek Godek
2008-08-18 16:15 ` References/locations Clinton Ebadi
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).