[racket] Extending syntax-parameters?

From: Eli Barzilay (eli at barzilay.org)
Date: Tue Oct 16 22:16:14 EDT 2012

20 minutes ago, J. Ian Johnson wrote:
> I have a syntax parameter that is bound to a transformer. I want to
> (at certain points) change the parameterization of this parameter to
> a new transformer that depends on the old one.
> I can't do this:
> #'(syntax-parameterize ([p (syntax-parser
>                             [pat (some-combination-of (syntax-parameter-value #'p) pat))])])
>     blah)
> Because the value fetched is the inner most one, but I want what it
> is currently.

So you need to grab it outside of the new tansformer, just like with
plain parameters and closures.

#lang racket/base

;; for fun
(require (for-syntax racket/base) racket/stxparam)
(define-syntax-parameter foo #'0)
(define-syntax (the-foo stx)
  #`'#,(syntax-parameter-value #'foo))
(define-syntax (foo-add stx)
  (syntax-case stx ()
    [(_ stuff body ...)
     #`(syntax-parameterize ([foo #'(stuff #,(syntax-parameter-value #'foo))])
         body ...)]))
(foo-add 1 (foo-add 2 the-foo))

;; IIUC, this is what you want
(define-syntax-parameter bar (λ (stx) #'0))
(define-syntax (bar-add stx)
  (syntax-case stx ()
    [(_ stuff body ...)
     (let* ([cur (syntax-parameter-value #'bar)]
            [new (λ (stx) #`(stuff #,(cur stx)))])
       #`(syntax-parameterize ([bar #,new])
           body ...))]))
(bar-add add1 (bar-add list bar))

          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                    http://barzilay.org/                   Maze is Life!

Posted on the users mailing list.