[racket] eginner's question on elementary textual replacement...

From: Rüdiger Asche (rac at ruediger-asche.de)
Date: Sun Mar 11 17:00:52 EDT 2012

>
> Hello,
>
> in most cases you should probably simply use a regular definition like
> this:
>
>  (define ONE 1)
>
> If that definition is not exported from the module you are writing it
> will probably be inlined anyway.
>

This doesn't appear to be true. You probably refer to this snippet of the 
docs here:

http://docs.racket-lang.org/reference/stxtrans.html?q=define#(tech._liberal._expansion)

which reads that in a liberal expansion context (which is implied in 
modules) define may translate to define-syntax. However:

(require macro-debugger/stepper-text)
(expand/step-text
#'(module alias racket
   (provide plusone)
   (define ONE 1)
   (define (plusone x) (+ x ONE))
   (plusone 2)
))

==>
...
(module
 alias
 racket
 (#%module-begin:2
  (#%provide:3 (expand:3 (provide-trampoline:3 plusone)))
  (define-values:7 (ONE) (quote 1))
  (define-values:13 (plusone) (lambda:12 (x) (#%app:17 + x ONE)))
  (#%app:16
   call-with-values:16
   (lambda:16 () (#%app:15 plusone (quote 2)))
   print-values:16)))

which generates a binding (albeit a define-values which according to the 
docs is also possible).

> If you really want a syntactic replacement, syntax-id-rules could be
> what you are looking for:
>
>  (define-syntax ONE (syntax-id-rules () [ONE 1]))
>

This yields the expected result:

(require macro-debugger/stepper-text)
(expand/step-text
#'(let ()
 (define-syntax alias
                    (syntax-rules ()
                     [(_ symbol literal)
                        (define-syntax symbol (syntax-id-rules () [symbol 
literal]))
                     ]))
  (alias ONE 1)
  (define (plusone x) (+ x ONE))
  (plusone 2)
))


==>
...
(let-values:1
 ()
 (let-values
  (((plusone) (lambda:27 (x) (#%app:30 + x '1))))
  (#%app:32 plusone (quote 2))))

So what made you think that defines within modules are inlined? Is it a doc 
bug, or were you looking at something else than liberal expansion that needs 
additional work? What does it take for define to translate into 
define-syntax within a liberal expansion context?

Thanks!


Posted on the users mailing list.