Very nice, Jon! I too think it would be great to get this into the docs.<div><br><div>(There are a few typos of filename comments vs 'require's. Also, two small comments inline below.)</div><div><br></div><div>Robby<br>
<br>On Thursday, March 1, 2012, Jon Rafkind wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Recent problems with phases have led me to investigate how they work in more detail. Here is a brief tutorial on what they are and how they work with macros. The guide and reference have something to say about phases but I don't think they go into enough detail.<br>
<br>
Bindings exist in a phase. The link between a binding and its phase is represented by an integer. Phase 0 is the phase used for "plain" definitions, so<br>
<br>
(define x 5)<br>
<br>
Will put a binding for 'x' into phase 0. 'x' can be defined at higher phases easily<br>
<br>
(begin-for-syntax<br>
(define x 5))<br>
<br>
Now 'x' is defined at phase 1. We can easily mix these two definitions in the same module, there is no clash between the two x's because they are defined at different phases.<br>
<br>
(define x 3)<br>
(begin-for-syntax<br>
(define x 9))<br>
<br>
'x' at phase 0 has a value of 3 and 'x' at phase 1 has a value of 9.<br>
<br>
Syntax objects can refer to these bindings, essentially they capture the binding as a value that can be passed around.<br>
<br>
#'x<br>
<br>
Is a syntax object that represents the 'x' binding. But which 'x' binding? In the last example there are two x's, one at phase 0 and one at phase 1. Racket will imbue #'x with lexical information for all phases, so the answer is both!<br>
<br>
Racket knows which 'x' to use when the syntax object is used. I'll use eval just for a second to prove a point.<br>
<br>
First we bind #'x to a pattern variable so we can use it in a template and then just print it.<br>
(eval (with-syntax ([x #'x])<br>
#'(printf "~a\n" x)))<br>
<br>
This will print 3 because x at phase 0 is bound to 3.<br>
<br>
(eval (with-syntax ([x #'x])<br>
#'(begin-for-syntax<br>
(printf "~a\n" x))))<br>
<br></blockquote><div><br></div><div>Does this depend on namespace-base-phase? If so, you might want to have a margin-note saying something to that effect with a pointer.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
This will print 9 because we are using x at phase 1 instead of 0. How does Racket know we wanted to use x at phase 1 instead of 0? Because of the 'begin-for-syntax'. So you can see that we started with the same syntax object, #'x, and was able to use it in two different ways -- at phase 0 and at phase 1.<br>
<br>
When a syntax object is created its lexical context is immediately set up. When a syntax object is provided from a module its lexical context will still reference the things that were around in the module it came from.<br>
<br>
This module will define 'foo' at phase 0 bound to the value 0 and 'sfoo' which binds the syntax object for 'foo'.<br>
<br>
;; a.rkt<br>
(define foo 0)<br>
(provide (for-syntax sfoo))<br>
(define-for-syntax sfoo #'foo)<br>
;; why not (define sfoo #'foo) ? I will explain later<br>
<br>
;; b.rkt<br>
(require "q.rkt")<br>
(define foo 8)<br>
(define-syntax (m stx)<br>
sfoo)<br>
(m)<br>
<br>
The result of the (m) macro will be whatever value 'sfoo' is bound to, which is #'foo. The #'foo that 'sfoo' knows that 'foo' is bound from the a.rkt module at phase 0. Even though there is another 'foo' in b.rkt this will not confuse Racket.<br>
<br>
Note that 'sfoo' is bound at phase 1. This is because (m) is a macro so its body executes at one phase higher than it was defined at. Since it was defined at phase 0 it will execute at phase 1, so any bindings it refers to also need to be bound at phase 1.<br>
<br>
Now really what I want to show is how bindings can be confused when modules are imported at different phases. Racket allows us to import a module at an arbitrary phase using require.<br>
<br>
(require "a.rkt") ;; import at phase 0<br>
(require (for-syntax "a.rkt")) ;; import at phase 1<br>
(require (for-template "a.rkt")) ;; import at phase -1<br>
(require (for-meta 5 "a.rkt" )) ;; import at phase 5<br>
<br>
What does it mean to 'import at phase 1'? Effectively it means that all the bindings from that module will have their phase increased by one.<br>
<br>
;; c.rkt<br>
(define x 0) ;; x is defined at phase 0<br>
<br>
;; d.rkt<br>
(require (for-syntax "c.rkt"))<br>
<br>
Now in d.rkt there will be a binding for 'x' at phase 1 instead of phase 0.<br>
<br>
So lets look at a.rkt from above and see what happens if we try to create a binding for the #'foo syntax object at phase 0.<br>
<br>
;; a.rkt<br>
(define foo 0)<br>
(define sfoo #'foo)<br>
(provide sfoo)<br>
<br>
Now both 'foo' and 'sfoo' are defined at phase 0. The lexical context of #'foo will know that there is a binding for 'foo' at phase 0. In fact it seems like things are working just fine, if we try to eval sfoo in a.rkt we will get 0.<br>
<br>
(eval sfoo)<br>
--> 0<br>
<br>
But now lets use sfoo in a macro.<br>
<br>
(define-syntax (m stx)<br>
sfoo)<br>
(m)<br>
<br>
We get an error 'reference to an identifier before its definition: sfoo'. Clearly 'sfoo' is not defined at phase 1 so we cannot refer to it inside the macro. Lets try to use 'sfoo' in another module by importing a.rkt at phase 1. Then we will get 'sfoo' at phase 1.<br>
<br>
;; b.rkt<br>
(require (for-syntax "a.rkt")) ;; now we have sfoo at phase 1<br>
(define-syntax (m stx)<br>
sfoo)<br>
(m)<br>
<br>
$ racket b.rkt<br>
compile: unbound identifier (and no #%top syntax transformer is bound) in: foo<br>
<br>
Racket says that 'foo' is unbound now. When 'a.rkt' is imported at phase 1 we have the following bindings<br>
<br>
foo at phase 1<br>
sfoo at phase 1<br>
<br>
So the macro 'm' can see sfoo and will return the #'foo syntax object which knows that 'foo' was bound at phase 0.</blockquote><div><br></div><div>I think that "knows" is not the right word here-- you should say that the m transformer returns the 'foo' identifier, whose phase 0 binding is then used. But in this context, thanks to the +1 of the import, we don't have a binding for 'foo' at phase 0, only phase 1.</div>
<div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"> But there is no 'foo' at phase 0 in b.rkt, there is only a 'foo' at phase 1, so we get an error. That is why 'sfoo' needed to be bound at phase 1 in a.rkt. In that case we would have had the following bindings after doing (require "a.rkt")<br>
<br>
foo at phase 0<br>
sfoo at phase 1<br>
<br>
So we can still use 'sfoo' in the macro since its bound at phase 1 and when the macro finishes it will refer to a 'foo' binding at phase 0.<br>
<br>
If we import a.rkt at phase 1 we can still manage to use 'sfoo'. The trick is to create a syntax object that will be evaluated at phase 1 instead of 0. We can do that with 'begin-for-syntax'.<br>
<br>
;; a.rkt<br>
(define foo 0)<br>
(define sfoo #'foo)<br>
(provide sfoo)<br>
<br>
;; b.rkt<br>
(require (for-syntax "a.rkt"))<br>
(define-syntax (m stx)<br>
(with-syntax ([x sfoo])<br>
#'(begin-for-syntax<br>
(printf "~a\n" x))))<br>
(m)<br>
<br>
b.rkt has 'foo' and 'sfoo' bound at phase 1. The output of the macro will be<br>
<br>
(begin-for-syntax<br>
(printf "~a\n" foo))<br>
<br>
Because 'sfoo' will turn into 'foo' when the template is expanded. Now this expression will work because 'foo' is bound at phase 1.<br>
<br>
Now you might try to cheat the phase system by importing a.rkt at both phase 0 and phase 1. Then you would have the following bindings<br>
<br>
foo at phase 0<br>
sfoo at phase 0<br>
foo at phase 1<br>
sfoo at phase 1<br>
<br>
So just using sfoo in a macro should work<br>
<br>
;; b.rkt<br>
(require "a.rkt"<br>
(for-syntax "a.rkt"))<br>
(define-syntax (m stx)<br>
sfoo)<br>
(m)<br>
<br>
The 'sfoo' inside the 'm' macro comes from the (for-syntax "a.rkt"). For this macro to work there must be a 'foo' at phase 0 bound, and there is one from the plain "a.rkt" imported at phase 0. But in fact this macro doesn't work, it says 'foo' is unbound. The key is that "a.rkt" and (for-syntax "a.rkt") are different instantiations of the same module. The 'sfoo' at phase 1 only knows that about 'foo' at phase 1, it does not know about the 'foo' bound at phase 0 from a different instantiation, even from the same file.<br>
<br>
So this means that if you have a two functions in a module, one that produces a syntax object and one that matches on it (say using syntax/parse) the module needs to be imported once at the proper phase. The module can't be imported once at phase 0 and again at phase 1 and be expected to work.<br>
<br>
;; x.rkt<br>
#lang racket<br>
<br>
(require (for-syntax syntax/parse)<br>
(for-template racket/base))<br>
<br>
(provide (all-defined-out))<br>
<br>
(define foo 0)<br>
(define (make) #'foo)<br>
(define-syntax (process stx)<br>
(define-literal-set locals (foo))<br>
(syntax-parse stx<br>
[(_ (n (~literal foo))) #'#''ok]))<br>
<br>
;; y.rkt<br>
#lang racket<br>
<br>
(require (for-meta 1 "q6.rkt")<br>
(for-meta 2 "q6.rkt" racket/base)<br>
;; (for-meta 2 racket/base)<br>
)<br>
<br>
(begin-for-syntax<br>
(define-syntax (m stx)<br>
(with-syntax ([out (make)])<br>
#'(process (0 out)))))<br>
<br>
(define-syntax (p stx)<br>
(m))<br>
<br>
(p)<br>
<br>
$ racket y.rkt<br>
process: expected the identifier `foo' at: foo in: (process (0 foo))<br>
<br>
'make' is being used in y.rkt at phase 2 and returns the #'foo syntax object which knows that foo is bound at phase 0 inside y.rkt, and at phase 2 from (for-meta 2 "q6.rkt"). The 'process' macro is imported at phase 1 from (for-meta 1 "q6.rkt") and knows that foo should be bound at phase 1 so when the syntax-parse is executed inside 'process' it is looking for 'foo' bound at phase 1 but it sees a phase 2 binding and so doesn't match.<br>
<br>
To fix this we can provide 'make' at phase 1 relative to x.rkt and just import it at phase 1 in y.rkt<br>
<br>
;; x.rkt<br>
#lang racket<br>
<br>
(require (for-syntax syntax/parse)<br>
(for-template racket/base))<br>
<br>
(provide (all-defined-out))<br>
<br>
(define foo 0)<br>
(provide (for-syntax make))<br>
(define-for-syntax (make) #'foo)<br>
(define-syntax (process stx)<br>
(define-literal-set locals (foo))<br>
(syntax-parse stx<br>
[(_ (n (~literal foo))) #'#''ok]))<br>
<br>
;; y.rkt<br>
#lang racket<br>
<br>
(require (for-meta 1 "q6.rkt")<br>
;; (for-meta 2 "q6.rkt" racket/base)<br>
(for-meta 2 racket/base)<br>
)<br>
<br>
(begin-for-syntax<br>
(define-syntax (m stx)<br>
(with-syntax ([out (make)])<br>
#'(process (0 out)))))<br>
<br>
(define-syntax (p stx)<br>
(m))<br>
<br>
(p)<br>
<br>
$ racket y.rkt<br>
'ok<br>
____________________<br>
Racket Users list:<br>
<a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/users</a><br>
</blockquote></div></div>