[racket] using a binding for-syntax in a module, and also providing it from that module

From: Ryan Culpepper (ryan at cs.utah.edu)
Date: Tue Mar 20 15:15:28 EDT 2012

If you have a fairly recent version of Racket, you can use *submodules*.

Here's library.rkt:

   #lang racket/base
   (require (for-syntax racket/base))

   ;; An auxiliary submodule for the parts to be used at
   ;; multiple phases:
   (module helper racket/base
     (require (for-template racket/base))
     (define (my-syntax-util stx)
       (quasisyntax/loc stx
         (cons 'my-syntax-util-was-here #,stx)))
     (provide my-syntax-util))

   ;; Require the submodule at the two different phases:
   (require (submod "." helper)
            (for-syntax (submod "." helper)))

   (define-syntax (my-own-syntax stx)
     (syntax-case stx ()
       ((_ CONDITION VAL)
        (quasisyntax/loc stx
          (if CONDITION
              #,(my-syntax-util (syntax VAL))
              VAL)))))

   (provide my-own-syntax
            my-syntax-util)

Ryan


On 03/20/2012 11:08 AM, Neil Van Dyke wrote:
> In the below example, can file "library.rkt" be changed (without
> splitting it up into two files), so that file "program.rkt" works
> without change? Is using "racket/load" the only way to keep
> "library.rkt" to one file like this?
>
>
> ;;;; FILE library.rkt
> #lang racket/base
>
> (require (for-syntax racket/base))
>
> (define-for-syntax (my-syntax-util stx)
> ;; Pretend that "my-syntax-util" performs some big complicated processing,
> ;; which is useful both in the implementation of "my-own-syntax" and
> ;; also directly by users of this library.
> (quasisyntax/loc stx
> (cons 'my-syntax-util-was-here #,stx)))
>
> (define-syntax (my-own-syntax stx)
> (syntax-case stx ()
> ((_ CONDITION VAL)
> (quasisyntax/loc stx
> (if CONDITION
> #,(my-syntax-util (syntax VAL))
> VAL)))))
>
> (provide my-own-syntax
> my-syntax-util)
>
>
> ;;;; FILE program.rkt
> #lang racket/base
>
> (require "library.rkt")
>
> (my-own-syntax (equal? "a" "a") 42)
>
> (my-syntax-util #'id)
>
>

Posted on the users mailing list.