[racket] getting one macro to tell another macro to define something

From: J. Ian Johnson (ianj at ccs.neu.edu)
Date: Sun Aug 3 20:04:37 EDT 2014

This is why I tried a delta-introducer on between orig-stx (the syntax-time parameter in match) and mstx (in the application of the transformer), but even though the documentation for make-syntax-delta-introducer says it can take two arbitrary pieces of syntax to make an introducer, it barfs on non-identifiers. You might try dumpster-diving for the head identifier in orig-stx though. That might work.
-Ian
----- Original Message -----
From: "Alexander D. Knauth" <alexander at knauth.org>
To: "J. Ian Johnson" <ianj at ccs.neu.edu>
Cc: "racket users list" <users at racket-lang.org>, "Matthias Felleisen" <matthias at ccs.neu.edu>
Sent: Sunday, August 3, 2014 7:44:31 PM GMT -05:00 US/Canada Eastern
Subject: Re: [racket] getting one macro to tell another macro to define something


I just thought: I could do syntax-local-introduce on the pattern before it gets passed to parse-id, and then do syntax-local-introduce on the bound-vars of that, except that it still doesn’t work. 


Well, I’m probably still missing the syntax-mark of the match*/derived form. Although I could write a sort of syntax-local-introduce/pattern that would recursively apply syntax-local-introduce to all of the identifiers in a parsed pattern. 



(define-syntax (match-define stx) 
(syntax-parse stx 
[(_ pat rhs:expr) 
(let ([p (parse-id (syntax-local-introduce #'pat))]) 
(with-syntax ([pat (syntax-property #'pat 'parsed-pat p)] 
[vars (map syntax-local-introduce (bound-vars p))]) 
(quasisyntax/loc stx 
(define-values vars (match*/derived (rhs) #,stx 
[(pat) (values . vars)])))))])) 


On Aug 3, 2014, at 2:12 PM, Alexander D. Knauth < alexander at knauth.org > wrote: 


Oh. 

I have no idea what to do then. I can’t even do syntax-local-introduce or (datum->syntax stx (syntax->datum stx)) on it because it’s not a syntax object. 

On Aug 3, 2014, at 1:59 PM, J. Ian Johnson < ianj at ccs.neu.edu > wrote: 



This is the problem of not introducing the right marks. He wants 
(define-values (x) (match blah [x x])) 

but is getting 

(define-values (x) (match blah [x-barf x])) 

Just pre-parsing won't get you all the way. You need to have a way to introduce to x-barf the marks between the outer match-define application and the inner match-expander transformer application. 
P.S. your match-define/values calls parse-id within the bound-vars call. You want to use the syntax-property there too. 
-Ian 
----- Original Message ----- 
From: "Matthias Felleisen" < matthias at ccs.neu.edu > 
To: "Alexander D. Knauth" < alexander at knauth.org > 
Cc: "J. Ian Johnson" < ianj at ccs.neu.edu >, "racket users list" < users at racket-lang.org > 
Sent: Sunday, August 3, 2014 1:53:49 PM GMT -05:00 US/Canada Eastern 
Subject: Re: [racket] getting one macro to tell another macro to define something 




>From where I sit your syntactic abstraction generates code of this shape: 


---> (define-values vars (match*/derived (rhs) #,stx [(pat) (values . vars ;; <---- 


This introduces vars ___and___ uses them before the right-hand side is evaluated. 


-- Matthias 









On Aug 3, 2014, at 1:50 PM, Alexander D. Knauth wrote: 




But I never defined or used those variables. 
All I did was save the parsed pattern in a syntax-property and have the parse function look at that syntax-property. 


On Aug 3, 2014, at 1:40 PM, Matthias Felleisen < matthias at ccs.neu.edu > wrote: 






That means you are defining and using recursive variables before the initialization is done. 




On Aug 3, 2014, at 1:31 PM, Alexander D. Knauth wrote: 




I tried it but got weird errors like this: 

idle-rest: undefined; 
cannot use before initialization 
And this: 

wrkr: undefined; 
cannot use before initialization 
https://github.com/AlexKnauth/racket/compare/match-define-save-parsed-pattern 


On Aug 3, 2014, at 12:32 PM, J. Ian Johnson < ianj at ccs.neu.edu > wrote: 


Try it and report back. 
-Ian 
----- Original Message ----- 
From: "Alexander D. Knauth" < alexander at knauth.org > 
To: "J. Ian Johnson" < ianj at ccs.neu.edu > 
Cc: "racket users list" < users at racket-lang.org > 
Sent: Sunday, August 3, 2014 12:22:57 PM GMT -05:00 US/Canada Eastern 
Subject: Re: [racket] getting one macro to tell another macro to define something 


What if match-define did something like this to store the parsed pat in a syntax-property?: 

(define-syntax (match-define stx) 
(syntax-parse stx 
[(_ pat rhs:expr) 
(let ([p (parse-id #'pat)]) 
(with-syntax ([pat (syntax-property #'pat 'parsed-pat p)] 
[vars (bound-vars p)]) 
(quasisyntax/loc stx 
(define-values vars (match*/derived (rhs) #,stx 
[(pat) (values . vars)])))))])) 
And parse did something like this: 

(define (parse stx) 
(or 
(syntax-property stx ‘parsed-pat) 
(let () 
... 
))) 


On Aug 1, 2014, at 9:13 PM, Alexander D. Knauth < alexander at knauth.org > wrote: 



On Aug 1, 2014, at 8:46 PM, Alexander D. Knauth < alexander at knauth.org > wrote: 



What do you mean? 
Shouldn’t it go something like this: 
(syntax-parameterize ([current-defs (mutable-set)]) 
(match-define (sender x) 1) 
(reciever) 
x) 
; => 
(syntax-parameterize ([current-defs (mutable-set #’(define x x3)]) 
(match-define x3 1) 
(reciever) 
x) 
; => 
(syntax-parameterize ([current-defs (mutable-set #’(define x x3)]) 
(define x3 (match 1 [x3 x3])) 
I just looked at the macro stepper again and saw something similar to this: (I replaced match*/derived with match and () with []) 
(define-values (x3) (match 1 [(sender x) (values x3)])) 
Why doesn’t match-define reuse the expanded match pattern instead of expanding it twice? 


(reciever) 
x) 
; => 
(syntax-parameterize ([current-defs (mutable-set #’(define x x3)]) 
(define x3 (match 1 [x3 x3])) 

(define x x3) 
x) 

The match-define form never defines “x” as anything, but the receiver should, right? 

On Aug 1, 2014, at 8:12 PM, J. Ian Johnson < ianj at ccs.neu.edu > wrote: 



Ah, okay, so... this macro expander you give is fundamentally flawed because match-define does an initial parse (which uses the match expander) to get the identifiers it is going to define. So, when you expand to x3 as the match pattern, you end up returning x3 as one of the values that the match-define -> define-values is going to define. It does not ever define "x" as anything because that was just arbitrary syntax that was given to the match expander. This x3 is x3 definition leads to a use-before-initialization error. 

Do you have a different example that doesn't fail in this way? 
-Ian 


____________________ 
Racket Users list: 
http://lists.racket-lang.org/users 


____________________ 
Racket Users list: 
http://lists.racket-lang.org/users 


____________________ 
Racket Users list: 
http://lists.racket-lang.org/users 





____________________ 
Racket Users list: 
http://lists.racket-lang.org/users 



Posted on the users mailing list.