introspection in Scheme?

This is a discussion on introspection in Scheme? within the Scheme forums in Programming Languages category; Is there any nice way to perform procedure introspection in Scheme? That is, I'd like to be able to inspect, at runtime, the internal structure of a procedure; what arguments does it take and what does the procedure body look like? PLT Scheme seems to have procedure-arity so perhaps there's some hope for me, but that's of course not sufficient, what I'd need would be something like get-procedure-body-as-a-data-structure... Is it possible, or is there a better approach to the whole problem (runtime introspection)?...

Go Back   Application Development Forum > Programming Languages > Scheme

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-26-2008, 12:55 PM
Nyang A. Phra
Guest
 
Default introspection in Scheme?

Is there any nice way to perform procedure introspection in Scheme?
That is, I'd like to be able to inspect, at runtime, the internal
structure of a procedure; what arguments does it take and what does
the procedure body look like?

PLT Scheme seems to have procedure-arity so perhaps there's some hope
for me, but that's of course not sufficient, what I'd need would be
something like get-procedure-body-as-a-data-structure... Is it
possible, or is there a better approach to the whole problem (runtime
introspection)?
Reply With Quote
  #2  
Old 08-26-2008, 01:53 PM
Pascal J. Bourguignon
Guest
 
Default Re: introspection in Scheme?

"Nyang A. Phra" <naphra@gmail.com> writes:

> Is there any nice way to perform procedure introspection in Scheme?
> That is, I'd like to be able to inspect, at runtime, the internal
> structure of a procedure; what arguments does it take and what does
> the procedure body look like?
>
> PLT Scheme seems to have procedure-arity so perhaps there's some hope
> for me, but that's of course not sufficient, what I'd need would be
> something like get-procedure-body-as-a-data-structure... Is it
> possible, or is there a better approach to the whole problem (runtime
> introspection)?


There is nothing in the R5RS standard to help here. If your
implementation provides an usable API for introspection, then be
happy. Otherwise, thanks to the metalinguistic properties of scheme,
you can easily implement your own introspective scheme, in scheme.


--
__Pascal Bourguignon__ http://www.informatimago.com/

HEALTH WARNING: Care should be taken when lifting this product,
since its mass, and thus its weight, is dependent on its velocity
relative to the user.
Reply With Quote
  #3  
Old 08-26-2008, 02:05 PM
Nyang A. Phra
Guest
 
Default Re: introspection in Scheme?

On Aug 26, 8:53 pm, p...@informatimago.com (Pascal J. Bourguignon)
wrote:
> "Nyang A. Phra" <nap...@gmail.com> writes:
>
> > Is there any nice way to perform procedure introspection in Scheme?
> > That is, I'd like to be able to inspect, at runtime, the internal
> > structure of a procedure; what arguments does it take and what does
> > the procedure body look like?

>
> > PLT Scheme seems to have procedure-arity so perhaps there's some hope
> > for me, but that's of course not sufficient, what I'd need would be
> > something like get-procedure-body-as-a-data-structure... Is it
> > possible, or is there a better approach to the whole problem (runtime
> > introspection)?

>
> There is nothing in the R5RS standard to help here. If your
> implementation provides an usable API for introspection, then be
> happy.


So I suspected. Thanks.

> Otherwise, thanks to the metalinguistic properties of scheme,
> you can easily implement your own introspective scheme, in scheme.


Could you (or anyone else) give me some pointers or ideas as to how to
get started with that? I'm an utter newbie with Scheme and it's really
not obvious how to go about it.
Reply With Quote
  #4  
Old 08-26-2008, 02:28 PM
Lauri Alanko
Guest
 
Default Re: introspection in Scheme?

In article <c0183450-51fd-4ec4-bbad-c0815cbabe79@a70g2000hsh.googlegroups.com>,
Nyang A. Phra <naphra@gmail.com> wrote:
> > Otherwise, thanks to the metalinguistic properties of scheme,
> > you can easily implement your own introspective scheme, in scheme.

>
> Could you (or anyone else) give me some pointers or ideas as to how to
> get started with that? I'm an utter newbie with Scheme and it's really
> not obvious how to go about it.


For practical purposes, you're probably better off using the
facilities provided by some "real" Scheme implementation, but still,
if you're interested in how to implement an introspective Scheme in
Scheme, then Queinnec's "Lisp in Small Pieces" is probably an ideal
introduction.


Lauri
Reply With Quote
  #5  
Old 08-26-2008, 03:22 PM
jrtm
Guest
 
Default Re: introspection in Scheme?

On 26 elo, 22:25, p...@informatimago.com (Pascal J. Bourguignon)
wrote:
> "Nyang A. Phra" <nap...@gmail.com> writes:
>
>
>
> > On Aug 26, 8:53 pm, p...@informatimago.com (Pascal J. Bourguignon)
> > wrote:
> >> "Nyang A. Phra" <nap...@gmail.com> writes:

>
> >> > Is there any nice way to perform procedure introspection in Scheme?
> >> > That is, I'd like to be able to inspect, at runtime, the internal
> >> > structure of a procedure; what arguments does it take and what does
> >> > the procedure body look like?

>
> >> > PLT Scheme seems to have procedure-arity so perhaps there's some hope
> >> > for me, but that's of course not sufficient, what I'd need would be
> >> > something like get-procedure-body-as-a-data-structure... Is it
> >> > possible, or is there a better approach to the whole problem (runtime
> >> > introspection)?

>
> >> There is nothing in the R5RS standard to help here. *If your
> >> implementation provides an usable API for introspection, then be
> >> happy.

>
> > So I suspected. Thanks.

>
> >> Otherwise, thanks to the metalinguistic properties of scheme,
> >> you can easily implement your own introspective scheme, in scheme.

>
> > Could you (or anyone else) give me some pointers or ideas as to how to
> > get started with that? I'm an utter newbie with Scheme and it's really
> > not obvious how to go about it.

>
> Study SICP.
>
> Then in the scheme implementation it gives, modify eval-assignment and
> set-variable-value! to keep in addition to the value (evaluated), the
> source form.
>
> Basically:
>
> (define (eval-assignment exp env)
> * (set-variable-value! (assignment-variable exp)
> * * * * * * * * * * * *(eval (assignment-value exp) env)
> * * * * * * * * * * * *env)
> * 'ok)
>
> becomes:
>
> (define (eval-assignment exp env)
> * (set-variable-value! (assignment-variable exp)
> * * * * * * * * * * * *(assignment-value exp) ; the source form
> * * * * * * * * * * * *(eval (assignment-value exp) env) ; its value
> * * * * * * * * * * * *env)
> * 'ok)
>
> Then add a primitive to retrieve the source form you'll have kept in
> the environment.
>
> This would allow to write:
>
> (define a (* 2 3))
> a * * * * * * * *--> 6
> (source-form 'a) --> (* 2 3)
>
> (define (f x) (* 2 x))
> (f 3) * * * * * *--> 6
> (source-form 'f) --> (lambda (x) (* 2 x))
>
> Another way would be to keep the source form along with the procedures
> [assuming you don't care about (* 2 3) vs. (+ 2 4)], thus allowing this:
>
> (define (f x) (* 2 x))
> (f 3) * * * * * * * * * * * * * * *--> 6
> (source-form f) * * * * * * * * * *--> (lambda (x) (*2 x))
> (source-form (lambda (x) (+ x x))) --> (lambda (x) (+ x x))
>
> In this case,
>
> (source-form 'f) * * * * * * * * * --> f
>
> Alternatively, if your implementation provides the optional procedure:
> (interaction-environment) then you could add the needed bookkeeping
> implementing just a REPL, somewhat like I did for Common Lisp in:http://www.informatimago.com/develop...-cl-pgms/ibcl/
>
> --
> __Pascal Bourguignon__ * * * * * * * * * *http://www.informatimago.com/


Thanks very much, that looks like something that'll get me started.
And thanks, Lauri, for the pointer as well.
Reply With Quote
  #6  
Old 08-26-2008, 03:25 PM
Pascal J. Bourguignon
Guest
 
Default Re: introspection in Scheme?

"Nyang A. Phra" <naphra@gmail.com> writes:

> On Aug 26, 8:53 pm, p...@informatimago.com (Pascal J. Bourguignon)
> wrote:
>> "Nyang A. Phra" <nap...@gmail.com> writes:
>>
>> > Is there any nice way to perform procedure introspection in Scheme?
>> > That is, I'd like to be able to inspect, at runtime, the internal
>> > structure of a procedure; what arguments does it take and what does
>> > the procedure body look like?

>>
>> > PLT Scheme seems to have procedure-arity so perhaps there's some hope
>> > for me, but that's of course not sufficient, what I'd need would be
>> > something like get-procedure-body-as-a-data-structure... Is it
>> > possible, or is there a better approach to the whole problem (runtime
>> > introspection)?

>>
>> There is nothing in the R5RS standard to help here. If your
>> implementation provides an usable API for introspection, then be
>> happy.

>
> So I suspected. Thanks.
>
>> Otherwise, thanks to the metalinguistic properties of scheme,
>> you can easily implement your own introspective scheme, in scheme.

>
> Could you (or anyone else) give me some pointers or ideas as to how to
> get started with that? I'm an utter newbie with Scheme and it's really
> not obvious how to go about it.



Study SICP.

Then in the scheme implementation it gives, modify eval-assignment and
set-variable-value! to keep in addition to the value (evaluated), the
source form.


Basically:

(define (eval-assignment exp env)
(set-variable-value! (assignment-variable exp)
(eval (assignment-value exp) env)
env)
'ok)

becomes:

(define (eval-assignment exp env)
(set-variable-value! (assignment-variable exp)
(assignment-value exp) ; the source form
(eval (assignment-value exp) env) ; its value
env)
'ok)


Then add a primitive to retrieve the source form you'll have kept in
the environment.

This would allow to write:

(define a (* 2 3))
a --> 6
(source-form 'a) --> (* 2 3)

(define (f x) (* 2 x))
(f 3) --> 6
(source-form 'f) --> (lambda (x) (* 2 x))





Another way would be to keep the source form along with the procedures
[assuming you don't care about (* 2 3) vs. (+ 2 4)], thus allowing this:

(define (f x) (* 2 x))
(f 3) --> 6
(source-form f) --> (lambda (x) (* 2 x))
(source-form (lambda (x) (+ x x))) --> (lambda (x) (+ x x))

In this case,

(source-form 'f) --> f



Alternatively, if your implementation provides the optional procedure:
(interaction-environment) then you could add the needed bookkeeping
implementing just a REPL, somewhat like I did for Common Lisp in:
http://www.informatimago.com/develop...-cl-pgms/ibcl/


--
__Pascal Bourguignon__ http://www.informatimago.com/
Reply With Quote
  #7  
Old 08-26-2008, 03:32 PM
Nyang A. Phra
Guest
 
Default Re: introspection in Scheme?

On 26 elo, 22:22, jrtm <juk...@gmail.com> wrote:
> On 26 elo, 22:25, p...@informatimago.com (Pascal J. Bourguignon)
> wrote:
>
>
>
> > "Nyang A. Phra" <nap...@gmail.com> writes:

>
> > > On Aug 26, 8:53 pm, p...@informatimago.com (Pascal J. Bourguignon)
> > > wrote:
> > >> "Nyang A. Phra" <nap...@gmail.com> writes:

>
> > >> > Is there any nice way to perform procedure introspection in Scheme?
> > >> > That is, I'd like to be able to inspect, at runtime, the internal
> > >> > structure of a procedure; what arguments does it take and what does
> > >> > the procedure body look like?

>
> > >> > PLT Scheme seems to have procedure-arity so perhaps there's some hope
> > >> > for me, but that's of course not sufficient, what I'd need would be
> > >> > something like get-procedure-body-as-a-data-structure... Is it
> > >> > possible, or is there a better approach to the whole problem (runtime
> > >> > introspection)?

>
> > >> There is nothing in the R5RS standard to help here. *If your
> > >> implementation provides an usable API for introspection, then be
> > >> happy.

>
> > > So I suspected. Thanks.

>
> > >> Otherwise, thanks to the metalinguistic properties of scheme,
> > >> you can easily implement your own introspective scheme, in scheme.

>
> > > Could you (or anyone else) give me some pointers or ideas as to how to
> > > get started with that? I'm an utter newbie with Scheme and it's really
> > > not obvious how to go about it.

>
> > Study SICP.

>
> > Then in the scheme implementation it gives, modify eval-assignment and
> > set-variable-value! to keep in addition to the value (evaluated), the
> > source form.

>
> > Basically:

>
> > (define (eval-assignment exp env)
> > * (set-variable-value! (assignment-variable exp)
> > * * * * * * * * * * * *(eval (assignment-value exp) env)
> > * * * * * * * * * * * *env)
> > * 'ok)

>
> > becomes:

>
> > (define (eval-assignment exp env)
> > * (set-variable-value! (assignment-variable exp)
> > * * * * * * * * * * * *(assignment-value exp) ;the source form
> > * * * * * * * * * * * *(eval (assignment-value exp) env) ; its value
> > * * * * * * * * * * * *env)
> > * 'ok)

>
> > Then add a primitive to retrieve the source form you'll have kept in
> > the environment.

>
> > This would allow to write:

>
> > (define a (* 2 3))
> > a * * * * * * * *--> 6
> > (source-form 'a) --> (* 2 3)

>
> > (define (f x) (* 2 x))
> > (f 3) * * * * * *--> 6
> > (source-form 'f) --> (lambda (x) (* 2 x))

>
> > Another way would be to keep the source form along with the procedures
> > [assuming you don't care about (* 2 3) vs. (+ 2 4)], thus allowing this:

>
> > (define (f x) (* 2 x))
> > (f 3) * * * * * * * * * * * * * * *--> 6
> > (source-form f) * * * * * * * * * *--> (lambda (x) (* 2 x))
> > (source-form (lambda (x) (+ x x))) --> (lambda (x) (+ x x))

>
> > In this case,

>
> > (source-form 'f) * * * * * * * * * --> f

>
> > Alternatively, if your implementation provides the optional procedure:
> > (interaction-environment) then you could add the needed bookkeeping
> > implementing just a REPL, somewhat like I did for Common Lisp in:http://www.informatimago.com/develop...-cl-pgms/ibcl/

>
> > --
> > __Pascal Bourguignon__ * * * * * * * * * *http://www.informatimago.com/

>
> Thanks very much, that looks like something that'll get me started.
> And thanks, Lauri, for the pointer as well.


(...And damn the Google interface, the above was me on a friend's
laptop.)
Reply With Quote
  #8  
Old 08-26-2008, 04:00 PM
Pascal Costanza
Guest
 
Default Re: introspection in Scheme?

Nyang A. Phra wrote:
> Is there any nice way to perform procedure introspection in Scheme?
> That is, I'd like to be able to inspect, at runtime, the internal
> structure of a procedure; what arguments does it take and what does
> the procedure body look like?
>
> PLT Scheme seems to have procedure-arity so perhaps there's some hope
> for me, but that's of course not sufficient, what I'd need would be
> something like get-procedure-body-as-a-data-structure... Is it
> possible, or is there a better approach to the whole problem (runtime
> introspection)?


What you can do is implement your own lambda macro, which allows you to
record whatever information inside the lambda form is interesting to
you. For example, you can associate that information with the resulting
function object(s) in an association list or hashtable (if they are weak
tables, then you don't even have problems with garbage collection).

However, in the general case you need information about the lexical
environment that is captured in the function object. This is not
something that you can add on top of Scheme without implementing a new
Scheme interpreter with first-class lexical environments from scratch.

If you want to use existing implementations with support for first-class
environments, your best bets, to the best of my knowledge, are Guile and
clisp in interpreted mode (the latter of which is not a Scheme, but a
Common Lisp). I am not aware of any other implementation that provides
first-class lexical environments.


Pascal

--
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
Reply With Quote
  #9  
Old 08-26-2008, 08:09 PM
William D Clinger
Guest
 
Default Re: introspection in Scheme?

Nyang A Phra wrote:
> Is there any nice way to perform procedure introspection in Scheme?
> That is, I'd like to be able to inspect, at runtime, the internal
> structure of a procedure; what arguments does it take and what does
> the procedure body look like?


You might be interested in an API that dates back to 1996,
and has been implemented (at least partially) in Larceny
and in several other implementations of Scheme:

http://larceny.ccs.neu.edu/doc/user-...rocedure-arity

Note that this API is intended only for heuristic uses.
If you need something truly reliable, you'll probably have
to roll your own.

Will
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 08:40 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.