guile-user
[Top][All Lists]
Advanced

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

Re: procedure-source availability


From: Mark H Weaver
Subject: Re: procedure-source availability
Date: Mon, 08 Oct 2012 13:57:27 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.2 (gnu/linux)

Panicz Maciej Godek <address@hidden> writes:
> The strange thing was that I had to define the macro ``function''
> using define-macro -- the define-syntax counterpart for some reason
> wouldn't work. So for example, if I wrote
> (define f (let ((y 5)) (function x (set! y (apply + y x)) y))
> then if ``function'' was defined by means of define-syntax/syntax-rules, ie
>
> (define-syntax function
>   (syntax-rules ()
>     ((_ args body ...)
>      (let ((environment (the-environment))
>            (lexical-names (lexical-names))
>            (procedure (lambda args body ...)))
>        (set-procedure-property! procedure 'source '(function args body ...))
>        (set-procedure-property! procedure 'environment environment)
>        (set-procedure-property! procedure 'lexical-names lexical-names)
>        procedure))))
>
> then the ``environment'' variable wouldn't capture the ``y'' (or
> anything else, for that matter).

(the-environment) captures the lexical environment where it is found,
and in this case that means the lexical environment of this macro
definition.

Although it is not documented, 'the-environment' accepts an optional
argument, which must be an identifier.  If provided, the lexical
environment captured is the one where that identifier was created.

In theory, this means that this should do what you want:

--8<---------------cut here---------------start------------->8---
(define-syntax function
  (syntax-rules ()
    ((function args body ...)
     (let ((environment (the-environment function))
           (lexical-names (map car (lexicals function)))
           (procedure (lambda args body ...)))
       (set-procedure-property! procedure 'source '(function args body ...))
       (set-procedure-property! procedure 'environment environment)
       (set-procedure-property! procedure 'lexical-names lexical-names)
       procedure))))
--8<---------------cut here---------------end--------------->8---

Unfortunately, our definition of 'syntax-rules' needlessly discards the
keyword identifier.  This should probably be fixed, but in the meantime
you can use 'syntax-case' instead:

--8<---------------cut here---------------start------------->8---
(define-syntax function
  (lambda (x)
    (syntax-case x ()
      ((function args body ...)
       #'(let ((environment (the-environment function))
               (lexical-names (map car (lexicals function)))
               (procedure (lambda args body ...)))
           (set-procedure-property! procedure 'source '(function args body ...))
           (set-procedure-property! procedure 'environment environment)
           (set-procedure-property! procedure 'lexical-names lexical-names)
           procedure)))))
--8<---------------cut here---------------end--------------->8---

Having said all this, I agree with Ludovic that this is probably not a
good approach.  'the-environment' inhibits almost all optimizations in
the compiler.

     Regards,
       Mark



reply via email to

[Prev in Thread] Current Thread [Next in Thread]