[racket] keep bindings in unhygienic macros?

From: Sam Tobin-Hochstadt (samth at ccs.neu.edu)
Date: Tue Jul 19 15:19:31 EDT 2011

On Tue, Jul 19, 2011 at 3:16 PM, Ismael Figueroa Palet
<ifigueroap at gmail.com> wrote:
> Thanks Matthias and Sam, the macro expansion now works as I intended, with
> the following change:
>
> (define-syntax-rule (with-monad (<unit-f> <bind-f>) <body>)
>     (let ((u <unit-f>)
>           (b <bind-f>))
>       (syntax-parameterize ([bind (make-rename-transformer #'b)])
>                            (syntax-parameterize ([unit
> (make-rename-transformer #'u)])
>                                                 <body>))))


You can write the above with just one use of `syntax-parameterize':

(define-syntax-rule (with-monad (<unit-f> <bind-f>) <body>)
    (let ((u <unit-f>)
          (b <bind-f>))
      (syntax-parameterize ([bind (make-rename-transformer #'b)]
                                     [unit (make-rename-transformer #'u)])
                                                <body>)))

> 2011/7/19 Sam Tobin-Hochstadt <samth at ccs.neu.edu>
>>
>> On Tue, Jul 19, 2011 at 2:06 PM, Ismael Figueroa Palet
>> <ifigueroap at gmail.com> wrote:
>> > Hi all, I'm trying to implement some simple macros to use monads in
>> > racket
>> > using a Haskell-like do notation. I have the following macros, and my
>> > problem is that I want the do macro to capture the bind identifier
>> > created
>> > by with-monad, and in each recursive step of the do macro expansion keep
>> > the
>> > same identifier. I don't know it is possible, I tried using (with-syntax
>> > ((bind (datum->syntax 'bind) ))) but it doesn't work.
>>
>> I think the right solution here is to use syntax parameters [1].  This
>> blog post by Eli provides a good intro:
>>  http://blog.racket-lang.org/2008/02/dirty-looking-hygiene.html
>>
>> [1] http://docs.racket-lang.org/reference/stxparam.html
>> --
>> sam th
>> samth at ccs.neu.edu
>
>
>
> --
> Ismael
>
>



-- 
sam th
samth at ccs.neu.edu



Posted on the users mailing list.