[racket] Macros that use local-require (hygiene issues?)

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Thu Nov 6 21:50:00 EST 2014

Let's remove /load from this world so we can see the expansion and let's play: 

#lang racket

;;provider
(module T racket
  (provide a)
  (define a 1))

;; requirer
(module R racket
  (require (for-syntax syntax/parse))
  (define-syntax (test-binding stx)
    (syntax-parse stx
      [(_ path)
       #'(let () (local-require (only-in path a)) a)]))
  ;; if you remove the next line, drracket shows an error. Why? 
  #; 
  (test-binding (submod ".." T))
  ;; "expanded" by hand, and it works 
  (let () (local-require (only-in (submod ".." T) a)) a))

;; go
(require 'R)



The two comments show that a does not get bound thru the binding position in only-in. You can also run the macro expander now and see that the expansion "looks like" what I did but the binding information shows that the a is done correctly for the "expanded" version. 

This tells you that local-require is doing some hygiene/binding manipulation that can't be done inside a let context. 





On Nov 6, 2014, at 6:33 PM, Spencer Florence wrote:

> I understand why the begin and [a a] work, but what I'm wondering is why the others didn't work. For example, why didn't `begin` work in my second example? Is the lexical information for the bindings just coming form somewhere really weird?
> 
> On Thu Nov 06 2014 at 5:18:59 PM Jay McCarthy <jay.mccarthy at gmail.com> wrote:
> The begin works because the macro use is at the top-level and the
> thing gets pulled up and spliced in, I believe.
> 
> The [a a] works because the second a is detected as a binder so it
> gets matched up mark-wise with the second one rather than having them
> erased? Hygiene? Marks? Dye packets?
> 
> Jay
> 
> On Thu, Nov 6, 2014 at 4:36 PM, Spencer Florence <spencer at florence.io> wrote:
> > Thanks! That fixed it!
> >
> > Although I'm still curious as to why the error happens in the first place.
> >
> > On Thu Nov 06 2014 at 3:32:02 PM Alexander D. Knauth <alexander at knauth.org>
> > wrote:
> >>
> >> I think I had a similar sort of problem about a year ago (I was at either
> >> RacketCon or a Hackathon that same weekend and Mathew Flatt figured it out),
> >> and the solution was changing (only-in path a) to (only-in path [a a]).
> >>
> >> #lang racket/load
> >> ;;provider
> >> (module T racket
> >>   (provide a)
> >>   (define a 1))
> >> ;; requirer
> >> (module R racket
> >>   (require (for-syntax syntax/parse))
> >>   (define-syntax (test-binding stx)
> >>     (syntax-parse stx
> >>       [(_ path)
> >>        #'(let () (local-require (only-in path [a a])) a)]))
> >>   (test-binding 'T))
> >> ;; go
> >> (require ‘R)
> >>
> >>
> >> On Nov 6, 2014, at 4:09 PM, Spencer Florence <spencer at florence.io> wrote:
> >>
> >> > Hi All,
> >> >
> >> > I've been struggling to have a macro be given a path then require
> >> > something from that path. The following code produces and "Unbound
> >> > Identifier" error, although I would expect it to evaluate to 1. Could
> >> > someone provide insight into what is going on?
> >> >
> >> > #lang racket/load
> >> > ;;provider
> >> > (module T racket
> >> >   (provide a)
> >> >   (define a 1))
> >> > ;; requirer
> >> > (module R racket
> >> >   (require (for-syntax syntax/parse))
> >> >   (define-syntax (test-binding stx)
> >> >     (syntax-parse stx
> >> >       [(_ path)
> >> >        #'(let () (local-require (only-in path a)) a)]))
> >> >   (test-binding 'T))
> >> > ;; go
> >> > (require 'R)
> >> >
> >> >
> >> > P.S. I attempted to do this with `dynamic-require,` but that failed
> >> > because in my actual program `path` is sometimes `(submod "..")`, which
> >> > caused an error about not having a base path.
> >> >
> >> > ____________________
> >> >  Racket Users list:
> >> >  http://lists.racket-lang.org/users
> >>
> >
> > ____________________
> >   Racket Users list:
> >   http://lists.racket-lang.org/users
> >
> 
> 
> 
> --
> Jay McCarthy
> http://jeapostrophe.github.io
> 
>            "Wherefore, be not weary in well-doing,
>       for ye are laying the foundation of a great work.
> And out of small things proceedeth that which is great."
>                           - D&C 64:33
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20141106/2f05fb1e/attachment.html>

Posted on the users mailing list.