Objectmix
Tags Register Mark Forums Read

How to get structured data as "describe" does? (instead of string) : lisp

This is a discussion on How to get structured data as "describe" does? (instead of string) within the lisp forums in Programming Languages category; I'm trying to do some self-reflection on closures. I read http://groups.google.com/group/comp....5d4373ef8704bc (defun make-thing (name) (flet ((local-function (message &rest args) (case message ( rint (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 ...


Object Mix > Programming Languages > lisp > How to get structured data as "describe" does? (instead of string)

Reply

 

LinkBack Thread Tools
  #1  
Old 11-11-2008, 04:34 AM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default How to get structured data as "describe" does? (instead of string)

I'm trying to do some self-reflection on closures. I read

http://groups.google.com/group/comp....5d4373ef8704bc

(defun make-thing (name)
(flet ((local-function (message &rest args)
(case message
(rint (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.
  #2  
Old 11-11-2008, 05:30 AM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: How to get structured data as "describe" does? (instead ofstring)

> ==========================================
> ? (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.
  #3  
Old 11-11-2008, 06:49 AM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: How to get structured data as "describe" does? (instead ofstring)

On Nov 11, 6:30 pm, budden <budde...@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
(rint (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}>

======================================================
  #4  
Old 11-11-2008, 07:46 AM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: How to get structured data as "describe" does? (instead ofstring)

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.
  #5  
Old 11-11-2008, 12:05 PM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: How to get structured data as "describe" does? (instead of string)

In article
<0ce9b549-5d8e-4fd6-baba-470bc6a51778@40g2000prx.googlegroups.com>,
wm <mingwu@gmail.com> wrote:

> On Nov 11, 6:30 pm, budden <budde...@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
> (rint (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
(rint (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/
Reply

Thread Tools



All times are GMT -5. The time now is 12:48 AM.

Managed by Infnx Pvt Ltd.