[racket] rudimentary macro Q.

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Wed Feb 26 15:20:22 EST 2014

Instead of manipulating the lexical information I would explicitly pass macros around to inform the reader of my program what's happening: 

;;;;;;;;;;;;;;;;;;;;;;;;;
#lang racket

(module inner racket
 (provide public-proc)
 (define-syntax public-proc
   (syntax-rules ()
     [(public-proc x private-proc) (private-proc x)]
     [(public-proc x) (private-proc x)]))
     
 (define (private-proc x) (format "Inner private proc says ~a" x)))

(require 'inner)

(define (private-proc x) (format "Outer private proc says ~a" x))

(public-proc 19)
(public-proc 19 private-proc)

-- Matthias





On Feb 26, 2014, at 1:54 PM, Matthew Butterick <mb at mbtype.com> wrote:

> Thank you. Maybe I can make the question a tiny bit less dumb (though still really dumb). 
> 
> In this example, when I use the syntax macro 'public-proc', it keeps the 'private-proc' binding from the 'inner' submodule. 
> 
> Q: How could I invoke 'public-proc' for its syntax-generating effect in the main module, yet have it adopt the binding of 'private-proc' in the main module? 
> 
> I gather roughly that it requires tampering with the lexical information of the syntax objects. Then things get hazy.
> 
> ;;;;;;;;;;;;;;;;;;;;;;;;;
> #lang racket
> 
> (module inner racket
>  (provide public-proc)
>  (define-syntax-rule (public-proc x) (private-proc x))
>  (define (private-proc x) (format "Inner private proc says ~a" x)))
> 
> (require 'inner)
> 
> (define (private-proc x) (format "Outer private proc says ~a" x))
> 
> (syntax->datum (expand #'(public-proc 'hi)))
> ;> '(#%app private-proc 'hi)
> (public-proc 'hi)
> ;> "Inner private proc says hi"
> 
> (syntax->datum (expand #'(private-proc 'hi)))
> ;> '(#%app private-proc 'hi)
> (private-proc 'hi)
> ;> "Outer private proc says hi"
> ;;;;;;;;;;;;;;;;;;;;;;;;;
> 
> 
> 
> On Feb 26, 2014, at 10:22 AM, Matthias Felleisen <matthias at ccs.neu.edu> wrote:
> 
>> 
>> That's precisely the behavior you want: 
>> -- what's exported is the public name, which would usually check/do things before it calls private 
>> -- while the unprotected private one is not provided and should not be visilble. 
>> 
>> How does the mechanics work? Well it delivers path names to these private identifier that in principle you can find too. So you can get around provided/not-provided but you don't want to 
>> 
>> -- Matthias
>> 
>> 
>> 
>> 
>> 
>> 
>> On Feb 26, 2014, at 11:51 AM, Matthew Butterick <mb at mbtype.com> wrote:
>> 
>>> In this example, why can syntax produced via macro reach the private function, while the ordinary syntax cannot?
>>> 
>>> 
>>> ;;;;;;;;;;;;;;;;;;;;;;;;
>>> ;; one.rkt
>>> #lang racket/base
>>> 
>>> (provide public-proc)
>>> 
>>> (define (private-proc x)
>>> (format "The private proc says ~a" x))
>>> 
>>> (define-syntax-rule (public-proc x)
>>> (private-proc x))
>>> ;;;;;;;;;;;;;;;;;;;;;;;;
>>> 
>>> ;;;;;;;;;;;;;;;;;;;;;;;;
>>> ;; two.rkt
>>> #lang racket/base
>>> (require "one.rkt")
>>> ;;;;;;;;;;;;;;;;;;;;;;;;
>>> 
>>> Running two.rkt:
>>>> (public-proc 'hi)
>>> "The private proc says hi"
>>>> (private-proc 'hi)
>>> private-proc: undefined
>>> ____________________
>>> Racket Users list:
>>> http://lists.racket-lang.org/users
>> 
> 
> 
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users



Posted on the users mailing list.