[plt-scheme] recover a procedure body?

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Wed Sep 25 08:21:34 EDT 2002

There's no built-in support for recovering the procedure body or
arguments.

If you have control over the context where the procedure is created,
you can redefine `lambda' to keep that information.

 ; This example uses v202.2 procedure structures
 (module new-lambda mzscheme

  (provide (rename new-lambda lambda)
           procedure-body
           procedure-args)
  
  (define-values (struct:ap make-annotated-proc annotated-proc? ap-ref ap-set!) 
    (make-struct-type 'anotated-proc #f 3 0 #f null #f 0))
  
  (define procedure-args (make-struct-field-accessor ap-ref 1))
  (define procedure-body (make-struct-field-accessor ap-ref 2))
  
  (define-syntax new-lambda
    (syntax-rules ()
      [(_ args . body)
       (make-annotated-proc (lambda args . body) 'args 'body)])))

If you don't have control over that context, then there are lots of
problems (e.g., the procedure might be implemented in an extension).

Matthew

At Tue, 24 Sep 2002 15:34:10 -0700 (PDT), Ron Stanonik wrote:
> Is it possible to recover a procedure body and arguments?  For example,
> we'd like to do the following:
> 
> > (define p (lambda(x) (display x)(newline)))
> > p
> > #<procedure:x>
> ... sometime later ...
> > (define q (procedure-body p))
> > q
> > '((display x)(newline))
> > (define a (procedure-args p))
> > a
> > '(x)
> 
> We trying to recover the procedure body and arguments so that we
> can save them and later reload them.
> 
> Thanks,
> 
> Ron Stanonik
> stanonik at cogsci.ucsd.edu
> 



Posted on the users mailing list.