[plt-scheme] procedure instrospection

From: Ryan Culpepper (ryanc at ccs.neu.edu)
Date: Fri Apr 17 18:35:44 EDT 2009

On Apr 17, 2009, at 4:48 PM, Jose A. Ortega Ruiz wrote:

> hi,
>
> i haven't been able to find in the documentation procedures to get
> information about defined procedures or macros, other than arity.  
> i'd be
> interested in getting, given a (exported) procedure/macro's name and
> module:
>
> - argument names (as given in its definition's formals) (or,
>  alternatively, the proc source code, from which i can get that).
> - filename and line number of definition (some time ago i found
>  something similar, but cannot anymore).
> - callees and callers of the given procedure (given the former i could
>  compute the later, i guess).
>
> is there a way of getting any of the metadata above from the MzScheme
> REPL?

For the most part, no. That information isn't attached to procedures.

The easiest question is "What file is a name defined in?"

   ;; definition-source : identifier -> (U symbol path)
   ;; Returns a symbol or path for the module that contains
   ;; the definition for a given name.
   (define (definition-source id)
     (let ([binding (identifier-binding id)])
       (and (list? binding)
            (resolved-module-path-name
             (module-path-index-resolve (car binding))))))

For example:
   (definition-source #'map)
   => #<path:/Users/ryanc/projects/plt/collects/scheme/private/map.ss>

The map procedure is defined in the file at that path, on my system  
anyway.

   (definition-source #'+)
   => #%kernel

The + procedure is defined in the built-in kernel module (it has no  
Scheme source file).

--

Since you can get the module path where a name is defined, you can  
read the module in from the file and try to search it for the  
definition.

Ryan



Posted on the users mailing list.