From: wm
Subject: How to get structured data as "describe" does? (instead of string)
Date: 
Message-ID: <76f4156b-a31d-4e8d-bf51-9ba5a29b0f92@v39g2000pro.googlegroups.com>
I'm trying to do some self-reflection on closures.  I read

http://groups.google.com/group/comp.lang.lisp/browse_frm/thread/634ae034c6a71c66/485d4373ef8704bc?lnk=gst&q=describe++closed+over#485d4373ef8704bc

(defun make-thing (name)
   (flet ((local-function (message &rest args)
            (case message
              (:print (format t "Hi, I am ~a" name))
              (:rename (setf name (first args))))))
     #'local-function))

==========================================
? (describe (make-thing "Red Chair"))
#<COMPILED-LEXICAL-CLOSURE LOCAL-FUNCTION #x2AA04BE>
Name: LOCAL-FUNCTION
Inner lfun: #<Compiled-function LOCAL-FUNCTION (Non-Global)
#x2A9AFD6>
Closed over values
(0): "Red Chair"
==========================================

I like the output, but "describe" only print string to the standard
output. I wonder is there a way to get the same output as structured
data, then I can even further manipulate the returned data.

I understand this is implementation dependent, e.g. the above works in
sbcl; but in clisp, it doesn't show much useful info.

So my question: is there a way to do this? or in which common lisp
implementation it can be done most easily?

Thanks.

From: budden
Subject: Re: How to get structured data as "describe" does? (instead of 	string)
Date: 
Message-ID: <3e025832-50c9-4702-8c6b-c63ce8b987f1@k36g2000pri.googlegroups.com>
> ==========================================
> ? (describe (make-thing "Red Chair"))
> #<COMPILED-LEXICAL-CLOSURE LOCAL-FUNCTION #x2AA04BE>
> Name: LOCAL-FUNCTION
> Inner lfun: #<Compiled-function LOCAL-FUNCTION (Non-Global)
> #x2A9AFD6>
> Closed over values
> (0): "Red Chair"
> ==========================================
Did you try
M-x slime-edit-definition describe?
in your SLIME session?
You need sbcl to be built from sources.
From: wm
Subject: Re: How to get structured data as "describe" does? (instead of 	string)
Date: 
Message-ID: <0ce9b549-5d8e-4fd6-baba-470bc6a51778@40g2000prx.googlegroups.com>
On Nov 11, 6:30 pm, budden <········@mtu-net.ru> wrote:
>
> Did you try
> M-x slime-edit-definition describe?
> in your SLIME session?
> You need sbcl to be built from sources.

Yes, I know I can always dig into how 'describe' is implemented in the
CL system itself (SBCL in this case); I'm wondering if there is any
more or less standard APIs to do this, e.g. so I can define a function
that can be used across different common lisp systems (SBCL & CLISP,
etc. ...), that will be better.

BTW, SBCL even prints out the code for the function:
======================================================
* (defun make-thing (name)
   (flet ((local-function (message &rest args)
            (case message
              (:print (format t "Hi, I am ~a" name))
              (:rename (setf name (first args))))))
     #'local-function))

MAKE-THING
* (describe (make-thing "Red Chair"))

#<CLOSURE (FLET LOCAL-FUNCTION) {A89E02D}> is a function.
Its associated name (as in FUNCTION-LAMBDA-EXPRESSION) is (FLET LOCAL-
FUNCTION).
The function's arguments are:  (MESSAGE &REST ARGS)
Its defined argument types are:
  (T &REST T)
Its result type is:
  (VALUES T &OPTIONAL)
On Tue, Nov 11, 2008 07:46:47 PM [-8] it was compiled from:
  (LAMBDA ()
    (PROGN
     (SB-INT:NAMED-LAMBDA MAKE-THING (NAME)
                          (BLOCK MAKE-THING
                            (FLET ((LOCAL-FUNCTION (MESSAGE &REST
ARGS)
                                     (CASE MESSAGE
                                       (:PRINT (FORMAT T "Hi, I am ~a"
NAME))
                                       (:RENAME (SETF NAME (FIRST
ARGS))))))
                              #'LOCAL-FUNCTION)))))
Its closure environment is:
  0: #<value cell "Red Chair" {A89E027}>

======================================================
From: budden
Subject: Re: How to get structured data as "describe" does? (instead of 	string)
Date: 
Message-ID: <87d637bb-2d08-4922-8c7a-b5a68ebca797@o40g2000prn.googlegroups.com>
I don't know but I think there is no such an API... But maybe you
might port SBCL "describe" code to other implementations. Such things
as disassemble might show which functions are called even when their
source is unavailable.
Then you might try to find references to that functions on the web. If
such an API exists already, it should have many
#+some-implementation conditionals. And, under these conditionals,
functions from implementation-specific APIs are called. Search engines
might help to find a references.

So, using describe, apropos, disassemble and web search, you might be
able to find that.

But wait...
Maybe

SWANK::INSPECT-FUNCTION

would help?
Slime is a cross-implementation thing.
From: Rainer Joswig
Subject: Re: How to get structured data as "describe" does? (instead of string)
Date: 
Message-ID: <joswig-825B08.18055211112008@news-europe.giganews.com>
In article 
<····································@40g2000prx.googlegroups.com>,
 wm <······@gmail.com> wrote:

> On Nov 11, 6:30�pm, budden <········@mtu-net.ru> wrote:
> >
> > Did you try
> > M-x slime-edit-definition describe?
> > in your SLIME session?
> > You need sbcl to be built from sources.
> 
> Yes, I know I can always dig into how 'describe' is implemented in the
> CL system itself (SBCL in this case); I'm wondering if there is any
> more or less standard APIs to do this, e.g. so I can define a function
> that can be used across different common lisp systems (SBCL & CLISP,
> etc. ...), that will be better.
> 
> BTW, SBCL even prints out the code for the function:
> ======================================================
> * (defun make-thing (name)
>    (flet ((local-function (message &rest args)
>             (case message
>               (:print (format t "Hi, I am ~a" name))
>               (:rename (setf name (first args))))))
>      #'local-function))
> 
> MAKE-THING
> * (describe (make-thing "Red Chair"))
> 
> #<CLOSURE (FLET LOCAL-FUNCTION) {A89E02D}> is a function.
> Its associated name (as in FUNCTION-LAMBDA-EXPRESSION) is (FLET LOCAL-
> FUNCTION).
> The function's arguments are:  (MESSAGE &REST ARGS)
> Its defined argument types are:
>   (T &REST T)
> Its result type is:
>   (VALUES T &OPTIONAL)
> On Tue, Nov 11, 2008 07:46:47 PM [-8] it was compiled from:
>   (LAMBDA ()
>     (PROGN
>      (SB-INT:NAMED-LAMBDA MAKE-THING (NAME)
>                           (BLOCK MAKE-THING
>                             (FLET ((LOCAL-FUNCTION (MESSAGE &REST
> ARGS)
>                                      (CASE MESSAGE
>                                        (:PRINT (FORMAT T "Hi, I am ~a"
> NAME))
>                                        (:RENAME (SETF NAME (FIRST
> ARGS))))))
>                               #'LOCAL-FUNCTION)))))
> Its closure environment is:
>   0: #<value cell "Red Chair" {A89E027}>
> 
> ======================================================


There is nothing to get the closure environment and inspect it.
If the source code is compiled away, there won't be any
source code either. 

CL-USER 12 > (defun make-thing (name)
               (flet ((local-function (message &rest args)
                        (case message
                          (:print (format t "Hi, I am ~a" name))
                          (:rename (setf name (first args))))))
                 #'local-function))
MAKE-THING

CL-USER 13 > (function-lambda-expression (make-thing "Red Chair"))
(LAMBDA (MESSAGE &REST ARGS) (DECLARE (LAMBDA-NAME LOCAL-FUNCTION)) (BLOCK LOCAL-FUNCTION (CASE MESSAGE (:PRINT (FORMAT T "Hi, I am ~a" NAME)) (:RENAME (SETF NAME (FIRST ARGS))))))
T
LOCAL-FUNCTION

-- 
http://lispm.dyndns.org/