* Req for help on objects and environments
@ 2004-09-01 8:28 Alp Öztarhan
2004-09-03 18:46 ` Andy Wingo
0 siblings, 1 reply; 6+ messages in thread
From: Alp Öztarhan @ 2004-09-01 8:28 UTC (permalink / raw)
I have been digging the documentation to find answers to two questions,
but could not find any answers.
First, I want to play with the properties on objects.
I want to be able to serialize an object along with its properties.
I *can* make and retrieve properties of objects by use of
(make-object-property). But how do I get a list of the properties of a
given object?
Second, I want to manipulate the environment in eval.
I could not even do what the documentation tells.
I could not get a handle on (null-environment) etc. (undefined symbol
:-(
Besides, I want to manipulate it, giving extra symbols etc.
Could anybody give me any hint on these?
thanks.
- alp
_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Req for help on objects and environments
2004-09-01 8:28 Req for help on objects and environments Alp Öztarhan
@ 2004-09-03 18:46 ` Andy Wingo
2004-09-06 6:17 ` tomas
2004-09-21 20:47 ` Marius Vollmer
0 siblings, 2 replies; 6+ messages in thread
From: Andy Wingo @ 2004-09-03 18:46 UTC (permalink / raw)
Hi Alp,
I know you've got a lot of answers already, but I think the environment
question is still not covered.
On Wed, 2004-09-01 at 11:28 +0300, Alp Öztarhan wrote:
> First, I want to play with the properties on objects.
> I want to be able to serialize an object along with its properties.
> I *can* make and retrieve properties of objects by use of
> (make-object-property). But how do I get a list of the properties of a
> given object?
In case you didn't figure it out, you can't, except from C with
scm_properties_whash.
> Second, I want to manipulate the environment in eval.
> I could not even do what the documentation tells.
> I could not get a handle on (null-environment) etc. (undefined symbol
> :-(
> Besides, I want to manipulate it, giving extra symbols etc.
> Could anybody give me any hint on these?
Sure. Scheme actually has two kinds of environments: toplevel and
lexical. The first is a simple association of names and values. The
concept is very similar to that of "modules", so in guile, toplevel
environments *are* modules[0]:
(module? (null-environment 5)) => #t
By default, guile is not r5rs-compliant when it loads up. I think this
is because (ice-9 syncase) is very slow to load up. Anyway, using
(ice-9 r5rs) will give you everything in the report, including
null-environment.
Modules are what can be passed as the second argument to `eval'. They
should be accessed only with documented functions for maximum
portability between guile versions, and to other schemes. Unfortunately,
modules expose their guts too much because everyone sees boot-9.scm.
So, to opaquely add a:=5 to a module, you can say:
(define env (null-environment 5))
(eval '(define a 5) env)
(eval 'a env) => 5
If you feel more dangerous, you can use module-define, module-defined?,
module-ref, module-variable, etc. See boot-9.scm for more. Dunno what
Marius thinks about this, but I like first-class modules :)
[0] This is complicated by the existence of environment SMOBs, from
environments.c / environment-*. I have no idea whether this code is on
the way in, or the way out. Marius, Dirk?
Lexical environments are a different beast. You cannot access them with
r5rs, I don't think. However, with guile you can, although it's
definitely not documented. I tried to be less hacky with the following
macro, but define-macro's memoizing behaviour caused guile 1.6 to
segfault, so I had to use low-level macros:
(define make-environment
(procedure->syntax
(lambda (exp env)
(local-eval `(let* ,(let lp ((in (cdr exp)) (out '()))
(if (null? in)
(reverse out)
(lp (cddr in) (cons
(list (car in) (cadr in))
out))))
(the-environment))
env))))
;; e.g.
(make-environment a 5 b 6 c a)
=> <a lexical environment with a=5, b=6, c=5
This environment is created with let*, so later bindings can access the
values of previous ones. `the-environment' is a syntax that returns the
current lexical environment. local-eval is a form of eval that takes a
lexical environment instead of a toplevel. Indeed, the `env' argument to
a low-level macro is a lexical environment.
Then,
(define-macro (with-environment env . body)
`(local-eval
,(list 'quote `(begin ,@body))
,env))
(with-environment (make-environment a 5)
a) => 5
(define (bind sym val env)
(if (defined? sym env)
(begin (local-eval `(set! ,sym ',val) env)
env)
(local-eval `(let ((,sym ,val)) (the-environment)) env)))
There's more in (soundscrape environments), in my arch repository. I
wrote this when I just started with guile; the pattern of lexical
environments applied to a problem I had. Now I worry about the low-level
access. A problem for later, I guess...
Hope that helped,
--
Andy Wingo <wingo@pobox.com>
http://ambient.2y.net/wingo/
_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Req for help on objects and environments
2004-09-02 11:53 ` tomas
@ 2004-09-03 19:10 ` Andy Wingo
0 siblings, 0 replies; 6+ messages in thread
From: Andy Wingo @ 2004-09-03 19:10 UTC (permalink / raw)
Hi Tomas,
On Thu, 2004-09-02 at 13:53 +0200, tomas@fabula.de wrote:
> (Note as well that the properties get attached to the *value*, not to the
> *variable*):
>
> | guile> (object-property 42 'friends)
> | (alfred bert carol)
I didn't realize this! Thanks for the tip. I guess it has to be this
way.
It trips up some documentation thoughts I had, though... I was using the
'documentation object-property to attach docs to variables, whereas it
actually puts it on the value. I wonder if there's a better way.
> Does this help?
It helped me ;)
Cheers,
--
Andy Wingo <wingo@pobox.com>
http://ambient.2y.net/wingo/
_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Req for help on objects and environments
2004-09-03 18:46 ` Andy Wingo
@ 2004-09-06 6:17 ` tomas
2004-09-21 20:47 ` Marius Vollmer
1 sibling, 0 replies; 6+ messages in thread
From: tomas @ 2004-09-06 6:17 UTC (permalink / raw)
Cc: Guile Users
On Fri, Sep 03, 2004 at 07:46:49PM +0100, Andy Wingo wrote:
> Hi Alp,
>
[...]
> Lexical environments are a different beast. You cannot access them with
> r5rs, I don't think. However, with guile you can, although it's
> definitely not documented. I tried to be less hacky with the following
> macro, but define-macro's memoizing behaviour caused guile 1.6 to
> segfault, so I had to use low-level macros:
>
> (define make-environment
> (procedure->syntax
> (lambda (exp env)
> (local-eval `(let* ,(let lp ((in (cdr exp)) (out '()))
> (if (null? in)
> (reverse out)
> (lp (cddr in) (cons
> (list (car in) (cadr in))
> out))))
> (the-environment))
> env))))
Thanks for your insightful post. As a (mostly) lurker and (currently) non-
schemer (shame!) I appreciate this.
Suggestion: Maybe call your macro make-local-environment -- that might help
newbies to keep apart those two beasts better.
Regards
-- tomás
_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Req for help on objects and environments
2004-09-03 18:46 ` Andy Wingo
2004-09-06 6:17 ` tomas
@ 2004-09-21 20:47 ` Marius Vollmer
2004-09-22 17:07 ` Andy Wingo
1 sibling, 1 reply; 6+ messages in thread
From: Marius Vollmer @ 2004-09-21 20:47 UTC (permalink / raw)
Cc: Guile Users
Andy Wingo <wingo@pobox.com> writes:
> [0] This is complicated by the existence of environment SMOBs, from
> environments.c / environment-*. I have no idea whether this code is
> on the way in, or the way out. Marius, Dirk?
They are not used by Guile at the moment, but maybe they will be in
the future. Or maybe not. Their existence has caused a few
confusions, so maybe it is best to remove them...
> Lexical environments are a different beast. You cannot access them with
> r5rs, I don't think. However, with guile you can, although it's
> definitely not documented.
It is best not to fiddle with the implementation of lexical
environments (expect when you are writing a debugger, say). Something
like the following might be sufficient and is cleaner.
(define (make-environment . bindings)
(let loop ((b bindings)
(env '()))
(cond ((null? b)
env)
(else
(loop (cddr b) (cons (list (car b) (cadr b)) env))))))
(define-macro (with-env env . body)
(let ((module (current-module)))
`(let ((code (cons* 'let ,env ',body)))
(eval code ',module))))
(define env (make-environment 'a 12 'b 13))
(pk (with-env env (+ a b)))
--
GPG: D5D4E405 - 2F9B BCCC 8527 692A 04E3 331E FAF8 226A D5D4 E405
_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Req for help on objects and environments
2004-09-21 20:47 ` Marius Vollmer
@ 2004-09-22 17:07 ` Andy Wingo
0 siblings, 0 replies; 6+ messages in thread
From: Andy Wingo @ 2004-09-22 17:07 UTC (permalink / raw)
Cc: Guile Users
Hey Marius,
On Tue, 2004-09-21 at 22:47 +0200, Marius Vollmer wrote:
> It is best not to fiddle with the implementation of lexical
> environments (expect when you are writing a debugger, say). Something
> like the following might be sufficient and is cleaner.
[...]
> (define-macro (with-env env . body)
> (let ((module (current-module)))
> `(let ((code (cons* 'let ,env ',body)))
> (eval code ',module))))
>
> (define env (make-environment 'a 12 'b 13))
>
> (pk (with-env env (+ a b)))
Yeah, I think I would have done this if I hadn't needed to keep state in
the environment. I agree it's ugly though -- it would be impossible to
compile this code, for instance, because it violates the
implementation's prerogative to implement environments as they like. I
should probably implement my stuff with anonymous modules.
Regards,
--
Andy Wingo <wingo@pobox.com>
http://ambient.2y.net/wingo/
_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2004-09-22 17:07 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-09-01 8:28 Req for help on objects and environments Alp Öztarhan
2004-09-03 18:46 ` Andy Wingo
2004-09-06 6:17 ` tomas
2004-09-21 20:47 ` Marius Vollmer
2004-09-22 17:07 ` Andy Wingo
-- strict thread matches above, loose matches on Subject: below --
2004-09-02 11:09 [Fwd: Req for help on objects and environments] Alp Öztarhan
2004-09-02 11:53 ` tomas
2004-09-03 19:10 ` Req for help on objects and environments Andy Wingo
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).