[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Procedure definition



Quoting Manuel Giraud:
>         Is there a way to retrieve the "definition" of a function in
>         MzScheme? I explain : 
> 
>         > (define (f n) (* n n))
>         > (something-like-global-defined-value 'f)
>           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>         Here, I want to get '(lambda (n) (* n n)) and not just
>         #<procedure:f>

No. The best strategy for getting that information depends on what you
want to do with it.

Below is one approach in v200. A more general approach would be
something like the way errortrace works: set the eval handler to one
that expands expressions, then replaces each `lambda' in the expanded
expression with something like `wrapper-lambda', then calls the
original eval handler.

Matthew

----------------------------------------

(module m mzscheme
  (define ht (make-hash-table 'weak))
  
  (define (save-source v src)
    (hash-table-put! ht v src)
    v)
  
  (define (get-source v)
    (hash-table-get ht v))  
  
  (define-syntax (wrapper-lambda stx)
    (syntax-case stx ()
      [(_ . rest)
       (syntax (save-source (lambda . rest) '(lambda . rest)))]))
  
  (define-syntax (wrapper-define stx)
    (syntax-case stx ()
      [(_ (id . formals) . body)
       (syntax (define id (wrapper-lambda formals . body)))]
      [(_ . rest)
       (syntax (define . rest))]))
  
  (provide get-source
           (rename wrapper-lambda lambda)
           (rename wrapper-define define)))

(require m)

(define (f n) (* n n))

(get-source f) ; => '(lambda (n) (* n n))