[racket] syntax-case literals matching in a procedure defined via begin-for-syntax vs (require (for-syntax ...))

From: Ryan Culpepper (ryanc at ccs.neu.edu)
Date: Mon Aug 9 19:35:11 EDT 2010

YC wrote:
> 
> On Mon, Aug 9, 2010 at 6:00 AM, rafkind <rafkind at cs.utah.edu 
> <mailto:rafkind at cs.utah.edu>> wrote:
> 
> 
>     You need a (require (for-template scheme/base)) in the 'literal.ss' file
>     because the function is at phase 0 producing syntax at a lower phase
>     (-1,
>     which is template), so you need to import the bindings from
>     scheme/base for
>     that phase.
> 
> 
> Ah - that solved it. Thanks! 
> 
> So does (begin-for-syntax ...) automatically manage the above for the 
> function?  I am trying to grok the difference but not sure if I got it: 
> 
> 1) when begin-for-syntax is used - the function is introduced in phase 
> 1, which handles syntax matching at phase 0, so it works correctly

There are only three things that matter: 1) the phase of comparison, 2) 
the binding of the macro transformer's literal at that phase, and 3) the 
binding of the macro uses's literal at that phase.

In both cases, the macro is used at phase 0. More precisely, the macro 
extends the phase-0 environment, which means it's implemented with a 
phase-1 expression, but the important thing is that since it's used at 
phase 0, the phase for literal comparison defaults to phase 0.

When begin-for-syntax is used, the macro's literal (the one in the 
literals list) has a binding at phase 0 from the surrounding module. So 
does the use, and so it works.

> 2) when (require (for-syntax)) is used - the function was defined in 
> phase 0, but is imported into phase 1... would it not handle syntax 
> matching at phase 0? 

When require for-syntax is used, the module where the literal occurs is 
at phase 1, so it only has a binding for the literal at phase 1. (The 
function is at phase 0 *relative to its enclosing module*, but it's at 
"absolute" phase 1.) Adding the require for-template (relative phase -1) 
to the auxiliary module (at phase 1) adds a binding at the right 
absolute phase, 0.

Ryan


> Then is it the case that for-syntax does not "shift" the phase of the 
> function in question? So the function stays at phase 0 so it needs phase 
> -1 defined. 
> 
> 3) Is it a best practice then all such module should all have 
> (for-template ...) whether the functions have syntax-case literals 
> defined?  It seems that the syntax-case matches the syntax shapes if 
> they do not involve literals (in the literal.ss it results in matching 
> of both the empty and the if clause), i.e. the only time I've noticed it 
> not working is when literals are involved. 
> 
> Thanks. 
> yc



Posted on the users mailing list.