-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
index.rkt
1664 lines (1299 loc) · 53 KB
/
index.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#lang scribble/manual
@(require racket/sandbox
scribble/eval
scribble/racket
racket/date
(for-syntax racket/base)
(for-label racket)
(for-label racket/stxparam)
(for-label syntax/parse)
(for-label racket/splicing)
(for-label racket/syntax))
@(define evaluator
(parameterize ([sandbox-output 'string]
[sandbox-error-output 'string]
[sandbox-memory-limit #f])
(make-evaluator 'racket)))
@(define typed/evaluator
(parameterize ([sandbox-output 'string]
[sandbox-error-output 'string]
[sandbox-memory-limit #f])
(make-evaluator 'typed/racket)))
@(define-syntax-rule (i body ...)
(interaction #:eval evaluator body ...))
@(define (current-year)
(number->string (date-year (current-date))))
@title[#:version ""]{Fear of Macros}
@author[@hyperlink["http://www.greghendershott.com"
"Greg Hendershott"]]
@image["fear-of-macros.jpg"]
@para{A practical guide to @hyperlink["https://www.racket-lang.org"]{Racket} macros.}
@para[@smaller{Copyright (c) 2012-@current-year[] by Greg Hendershott. All rights reserved.}]
@para[@smaller["Last updated "
(parameterize ([date-display-format 'iso-8601])
(date->string (current-date) #t))]]
@para{@hyperlink["https://github.com/greghendershott/fear-of-macros/issues" "Feedback and corrections"].}
@para{@hyperlink["https://github.com/users/greghendershott/sponsorship" "Sponsor my work"] or @hyperlink["https://www.paypal.me/greghendershott" "donate"].}
@table-of-contents{}
@; ----------------------------------------------------------------------------
@section{Preface}
I learned @hyperlink["https://www.racket-lang.org"]{Racket} after 25
years of mostly using C and C++.
Some psychic whiplash resulted.
"All the parentheses" was actually not a big deal. Instead, the first
mind warp was functional programming. Before long I wrapped my brain
around it, and went on to become comfortable and effective with many
other aspects and features of Racket.
But two final frontiers remained: Macros and continuations.
I found that simple macros were easy and understandable, plus there
were many good tutorials available. But the moment I stepped past
routine pattern-matching, I kind of fell off a cliff into a
terminology soup. I marinaded myself in material, hoping it would
eventually sink in after enough re-readings. I even found myself using
trial and error, rather than having a clear mental model what was
going on. Gah.
I'm starting to write this at the point where the shapes are slowly
emerging from the fog.
@margin-note{If you have any corrections, criticisms, complaints, or whatever,
@hyperlink["https://github.com/greghendershott/fear-of-macros/issues" "please
let me know"].}
My primary motive is selfish. Explaining something forces me to learn
it more thoroughly. Plus if I write something with mistakes, other
people will be eager to point them out and correct me. Is that a
social-engineering variation of meta-programming? Next question,
please. :)
Finally I do hope it may help other people who have a similar
background and/or learning style as me.
I want to show how Racket macro features have evolved as solutions to
problems or annoyances. I learn more quickly and deeply when I
discover the answer to a question I already have, or find the solution
to a problem whose pain I already feel. Therefore I'll give you the
questions and problems first, so that you can better appreciate and
understand the answers and solutions.
@; ----------------------------------------------------------------------------
@section{Our plan of attack}
The macro system you will mostly want to use for production-quality
macros is called @racket[syntax-parse]. And don't worry, we'll get to
that soon.
But if we start there, you're likely to feel overwhelmed by concepts
and terminology, and get very confused. I did.
1. Instead let's start with the basics: A syntax object and a function
to change it---a "transformer". We'll work at that level for a while to
get comfortable and to de-mythologize this whole macro business.
2. Soon we'll realize that pattern-matching would make life
easier. We'll learn about @racket[syntax-case] and its shorthand
cousin, @racket[define-syntax-rule]. We'll discover we can get
confused if we want to munge pattern variables before sticking them
back in the template, and learn how to do that.
3. At this point we'll be able to write many useful macros. But, what
if we want to write the ever-popular anaphoric if, with a "magic
variable"? It turns out we've been protected from making certain kind
of mistakes. When we want to do this kind of thing on purpose, we use
a syntax parameter. [There are other, older ways to do this. We won't
look at them. We also won't spend a lot of time
advocating "hygiene"---we'll just stipulate that it's good.]
4. Finally, we'll realize that our macros could be smarter when
they're used in error. Normal Racket functions optionally can have
contracts and types. These catch usage mistakes and provide clear,
useful error messages. It would be great if there were something
similar for macro. There is. One of the more-recent Racket macro
enhancements is @racket[syntax-parse].
@; ----------------------------------------------------------------------------
@; ----------------------------------------------------------------------------
@section{Transform!}
@verbatim[#:indent 2]{
YOU ARE INSIDE A ROOM.
THERE ARE KEYS ON THE GROUND.
THERE IS A SHINY BRASS LAMP NEARBY.
IF YOU GO THE WRONG WAY, YOU WILL BECOME
HOPELESSLY LOST AND CONFUSED.
> pick up the keys
YOU HAVE A SYNTAX TRANSFORMER
}
@subsection{What is a syntax transformer?}
A syntax transformer is not one of the トランスフォーマ
@hyperlink["http://en.wikipedia.org/wiki/Transformers" "transformers"].
Instead, it is simply a function. The function takes syntax and
returns syntax. It transforms syntax.
Here's a transformer function that ignores its input syntax, and
always outputs syntax for a string literal:
@margin-note{These examples assume @litchar{#lang racket}. If you want
to try them using @litchar{#lang racket/base}, you'll need to
@litchar{(require (for-syntax racket/base))}.}
@(let-syntax([syntax (make-element-id-transformer
(lambda (stx)
#'@racket[syntax]))]) ;print as syntax not #'
@i[
(define-syntax foo
(lambda (stx)
(syntax "I am foo")))
]
)
Using it:
@i[
(foo)
]
When we use @racket[define-syntax], we're making a transformer
@italic{binding}. This tells the Racket compiler, "Whenever you
encounter a chunk of syntax starting with @racket[foo], please give it
to my transformer function, and replace it with the syntax I give back
to you." So Racket will give anything that looks like @racket[(foo
...)] to our function, and we can return new syntax to use
instead. Much like a search-and-replace.
Maybe you know that the usual way to define a function in Racket:
@racketblock[(define (f x) ...)]
is shorthand for:
@racketblock[(define f (lambda (x) ...))]
That shorthand lets you avoid typing @racket[lambda] and some parentheses.
Well there is a similar shorthand for @racket[define-syntax]:
@(let-syntax([syntax (make-element-id-transformer
(lambda (stx)
#'@racket[syntax]))]) ;print as syntax not #'
@i[
(define-syntax (also-foo stx)
(syntax "I am also foo"))
(also-foo)
]
)
What we want to remember is that this is simply shorthand. We are
still defining a transformer function, which takes syntax and returns
syntax. Everything we do with macros, will be built on top of this
basic idea. It's not magic.
Speaking of shorthand, there is also a shorthand for @racket[syntax],
which is @litchar{#'}:
@margin-note{@litchar{#'} is short for @racket[syntax] much like
@litchar{'} is short for @racket[quote].}
@i[
(define-syntax (quoted-foo stx)
#'"I am also foo, using #' instead of syntax")
(quoted-foo)
]
We'll use the @litchar{#'} shorthand from now on.
Of course, we can emit syntax that is more interesting than a
string literal. How about returning @racket[(displayln "hi")]?
@i[
(define-syntax (say-hi stx)
#'(displayln "hi"))
(say-hi)
]
When Racket expands our program, it sees the occurrence of
@racket[(say-hi)], and sees it has a transformer function for that. It
calls our function with the old syntax, and we return the new syntax,
which is used to evaluate and run our program.
@; ----------------------------------------------------------------------------
@subsection{What's the input?}
Our examples so far have ignored the input syntax and output some
fixed syntax. But typically we will want to transform the input syntax
into something else.
Let's start by looking closely at what the input actually @italic{is}:
@i[
(define-syntax (show-me stx)
(print stx)
#'(void))
(show-me '(+ 1 2))
]
The @racket[(print stx)] shows what our transformer is given: a syntax
object.
A syntax object consists of several things. The first part is the
S-expression representing the code, such as @racket['(+ 1 2)].
Racket syntax is also decorated with some interesting information such
as the source file, line number, and column. Finally, it has
information about lexical scoping (which you don't need to worry about
now, but will turn out to be important later.)
There are a variety of functions available to access a syntax object.
Let's define a piece of syntax:
@i[
(define stx #'(if x (list "true") #f))
stx
]
Now let's use functions that access the syntax object. The source
information functions are:
@margin-note{@racket[(syntax-source stx)] is returning @racket['eval],
only because of how I'm generating this documentation, using an
evaluator to run code snippets in Scribble. Normally this would be
something like "my-file.rkt".}
@i[
(syntax-source stx)
(syntax-line stx)
(syntax-column stx)
]
More interesting is the syntax "stuff" itself. @racket[syntax->datum]
converts it completely into an S-expression:
@i[
(syntax->datum stx)
]
Whereas @racket[syntax-e] only goes "one level down". It may return a
list that has syntax objects:
@i[
(syntax-e stx)
]
Each of those syntax objects could be converted by @racket[syntax-e],
and so on recursively---which is what @racket[syntax->datum] does.
In most cases, @racket[syntax->list] gives the same result as
@racket[syntax-e]:
@i[
(syntax->list stx)
]
(When would @racket[syntax-e] and @racket[syntax->list] differ? Let's
not get side-tracked now.)
When we want to transform syntax, we'll generally take the pieces we
were given, maybe rearrange their order, perhaps change some of the
pieces, and often introduce brand-new pieces.
@; ----------------------------------------------------------------------------
@subsection{Actually transforming the input}
Let's write a transformer function that reverses the syntax it was
given:
@margin-note{The @racket[values] at the end of the example allows the
result to evaluate nicely. Try
@racket[(reverse-me "backwards" "am" "i")] to see why it's handy.}
@i[
(define-syntax (reverse-me stx)
(datum->syntax stx (reverse (cdr (syntax->datum stx)))))
(reverse-me "backwards" "am" "i" values)
]
Understand Yoda, we can. Great, but how does this work?
First we take the input syntax, and give it to
@racket[syntax->datum]. This converts the syntax into a plain old
list:
@i[
(syntax->datum #'(reverse-me "backwards" "am" "i" values))
]
Using @racket[cdr] slices off the first item of the list,
@racket[reverse-me], leaving the remainder:
@racket[("backwards" "am" "i" values)]. Passing that to
@racket[reverse] changes it to @racket[(values "i" "am" "backwards")]:
@i[
(reverse (cdr '(reverse-me "backwards" "am" "i" values)))
]
Finally we use @racket[datum->syntax] to convert this back to
@racket[syntax]:
@i[
(datum->syntax #f '(values "i" "am" "backwards"))
]
That's what our transformer function gives back to the Racket
compiler, and @italic{that} syntax is evaluated:
@i[
(values "i" "am" "backwards")
]
@margin-note{The first argument of @racket[datum->syntax] contains the lexical
context information that we want to associate with the @racket[syntax]
outputted by the transformer. If the first argument is set to @racket[#f] then
no lexical context will be associated.}
@; ----------------------------------------------------------------------------
@subsection{Compile time vs. run time}
@codeblock0{
(define-syntax (foo stx)
(make-pipe) ;Ce n'est pas le temps d'exécution
#'(void))
}
Normal Racket code runs at ... run time. Duh.
@margin-note{Instead of "compile time vs. run time", you may hear it
described as "syntax phase vs. runtime phase". Same difference.}
But a syntax transformer is called by Racket as part of the process of
parsing, expanding, and compiling our program. In other words, our
syntax transformer function is evaluated at compile time.
This aspect of macros lets you do things that simply aren't possible
in normal code. One of the classic examples is something like the
Racket form, @racket[if]:
@racket[(if <condition> <true-expression> <false-expression>)]
If we implemented @racket[if] as a function, all of the arguments
would be evaluated before being provided to the function.
@i[
(define (our-if condition true-expr false-expr)
(cond [condition true-expr]
[else false-expr]))
(our-if #t
"true"
"false")
]
That seems to work. However, how about this:
@i[
(define (display-and-return x)
(displayln x)
x)
(our-if #t
(display-and-return "true")
(display-and-return "false"))
]
@margin-note{One answer is that functional programming is good, and
side-effects are bad. But avoiding side-effects isn't always
practical.}
Oops. Because the expressions have a side-effect, it's obvious that
they are both evaluated. And that could be a problem---what if the
side-effect includes deleting a file on disk? You wouldn't want
@racket[(if user-wants-file-deleted? (delete-file) (void))] to delete
a file even when @racket[user-wants-file-deleted?] is @racket[#f].
So this simply can't work as a plain function. However a syntax
transformer can rearrange the syntax -- rewrite the code -- at compile
time. The pieces of syntax are moved around, but they aren't actually
evaluated until run time.
Here is one way to do this:
@i[
(define-syntax (our-if-v2 stx)
(define xs (syntax->list stx))
(datum->syntax stx `(cond [,(cadr xs) ,(caddr xs)]
[else ,(cadddr xs)])))
(our-if-v2 #t
(display-and-return "true")
(display-and-return "false"))
(our-if-v2 #f
(display-and-return "true")
(display-and-return "false"))
]
That gave the right answer. But how? Let's pull out the transformer
function itself, and see what it did. We start with an example of some
input syntax:
@i[
(define stx (syntax (our-if-v2 #t "true" "false")))
(displayln stx)
]
1. We take the original syntax, and use @racket[syntax->list] to
change it into a @racket[list] of syntax objects:
@i[
(define xs (syntax->list stx))
(displayln xs)
]
2. To change this into a Racket @racket[cond] form, we need to take
the three interesting pieces---the condition, true-expression, and
false-expression---from the list using @racket[cadr], @racket[caddr],
and @racket[cadddr] and arrange them into a @racket[cond] form:
@racketblock[
`(cond [,(cadr xs) ,(caddr xs)]
[else ,(cadddr xs)])
]
3. Finally, we change that into @racket[syntax] using
@racket[datum->syntax]:
@i[
(datum->syntax stx `(cond [,(cadr xs) ,(caddr xs)]
[else ,(cadddr xs)]))
]
So that works, but using @racket[cadddr] etc. to destructure a list is
painful and error-prone. Maybe you know Racket's @racket[match]?
Using that would let us do pattern-matching.
@margin-note{Notice that we don't care about the first item in the
syntax list. We didn't take @racket[(car xs)] in our-if-v2, and we
didn't use @racket[name] when we used pattern-matching. In general, a
syntax transformer won't care about that, because it is the name of
the transformer binding. In other words, a macro usually doesn't care
about its own name.}
Instead of:
@i[
(define-syntax (our-if-v2 stx)
(define xs (syntax->list stx))
(datum->syntax stx `(cond [,(cadr xs) ,(caddr xs)]
[else ,(cadddr xs)])))
]
We can write:
@i[
(define-syntax (our-if-using-match stx)
(match (syntax->list stx)
[(list name condition true-expr false-expr)
(datum->syntax stx `(cond [,condition ,true-expr]
[else ,false-expr]))]))]
Great. Now let's try using it:
@i[
(our-if-using-match #t "true" "false")
]
Oops. It's complaining that @racket[match] isn't defined.
Our transformer function is working at compile time, not run time. And
at compile time, only @racket[racket/base] is required for you
automatically---not the full @racket[racket].
Anything beyond @racket[racket/base], we have to require
ourselves---and require it for compile time using the
@racket[for-syntax] form of @racket[require].
In this case, instead of using plain @racket[(require racket/match)],
we want @racket[(require (for-syntax racket/match))]---the
@racket[for-syntax] part meaning, "for compile time".
So let's try that:
@i[
(require (for-syntax racket/match))
(define-syntax (our-if-using-match-v2 stx)
(match (syntax->list stx)
[(list _ condition true-expr false-expr)
(datum->syntax stx `(cond [,condition ,true-expr]
[else ,false-expr]))]))
(our-if-using-match-v2 #t "true" "false")
]
Joy.
@; ----------------------------------------------------------------------------
@subsection{@racket[begin-for-syntax]}
We used @racket[for-syntax] to @racket[require] the
@racket[racket/match] module because we needed to use @racket[match]
at compile time.
What if we wanted to define our own helper function to be used by a
macro? One way to do that is put it in another module, and
@racket[require] it using @racket[for-syntax], just like we did with
the @racket[racket/match] module.
If instead we want to put the helper in the same module, we can't
simply @racket[define] it and use it---the definition would exist at
run time, but we need it at compile time. The answer is to put the
definition of the helper function(s) inside @racket[begin-for-syntax]:
@racketblock[
(begin-for-syntax
(define (my-helper-function ....)
....))
(define-syntax (macro-using-my-helper-function stx)
(my-helper-function ....)
....)
]
In the simple case, we can also use @racket[define-for-syntax], which
composes @racket[begin-for-syntax] and @racket[define]:
@racketblock[
(define-for-syntax (my-helper-function ....)
....)
(define-syntax (macro-using-my-helper-function stx)
(my-helper-function ....)
....)
]
To review:
@itemize[
@item{Syntax transformers work at compile time, not run time. The good
news is this means we can do things like rearrange the pieces of
syntax without evaluating them. We can implement forms like
@racket[if] that simply couldn't work properly as run time functions.}
@item{More good news is that there isn't some special, weird language
for writing syntax transformers. We can write these transformer
functions using the Racket language we already know and love.}
@item{The semi-bad news is that the familiarity can make it easy to forget
that we're not working at run time. Sometimes that's important to
remember.
@itemize[
@item{For example only @racket[racket/base] is required for us
automatically. If we need other modules, we have to require them, and
do so @italic{for compile time} using @racket[for-syntax].}
@item{Similarly, if we want to define helper functions in the same
file/module as the macros that use them, we need to wrap the
definitions inside a @racket[begin-for-syntax] form. Doing so makes
them available at compile time.}
]
}
]
@; ----------------------------------------------------------------------------
@; ----------------------------------------------------------------------------
@section[#:tag "pattern-matching"]{Pattern matching: syntax-case and syntax-rules}
Most useful syntax transformers work by taking some input syntax, and
rearranging the pieces into something else. As we saw, this is
possible but tedious using list accessors such as
@racket[cadddr]. It's more convenient and less error-prone to use
@racket[match] to do pattern-matching.
@margin-note{Historically, @racket[syntax-case] and
@racket[syntax-rules] pattern matching came first. @racket[match] was
added to Racket later.}
It turns out that pattern-matching was one of the first improvements
to be added to the Racket macro system. It's called
@racket[syntax-case], and has a shorthand for simple situations called
@racket[define-syntax-rule].
Recall our previous example:
@racketblock[
(require (for-syntax racket/match))
(define-syntax (our-if-using-match-v2 stx)
(match (syntax->list stx)
[(list _ condition true-expr false-expr)
(datum->syntax stx `(cond [,condition ,true-expr]
[else ,false-expr]))]))
]
Here's what it looks like using @racket[syntax-case]:
@i[
(define-syntax (our-if-using-syntax-case stx)
(syntax-case stx ()
[(_ condition true-expr false-expr)
#'(cond [condition true-expr]
[else false-expr])]))
(our-if-using-syntax-case #t "true" "false")
]
Pretty similar, huh? The pattern matching part looks almost exactly
the same. The way we specify the new syntax is simpler. We don't need
to do quasi-quoting and unquoting. We don't need to use
@racket[datum->syntax]. Instead, we supply a "template", which uses
variables from the pattern.
There is a shorthand for simple pattern-matching cases, which expands
into @racket[syntax-case]. It's called @racket[define-syntax-rule]:
@i[
(define-syntax-rule (our-if-using-syntax-rule condition true-expr false-expr)
(cond [condition true-expr]
[else false-expr]))
(our-if-using-syntax-rule #t "true" "false")
]
Here's the thing about @racket[define-syntax-rule]. Because it's so
simple, @racket[define-syntax-rule] is often the first thing people are
taught about macros. But it's almost deceptively simple. It looks so
much like defining a normal run time function---yet it's not. It's
working at compile time, not run time. Worse, the moment you want to
do more than @racket[define-syntax-rule] can handle, you can fall off
a cliff into what feels like complicated and confusing
territory. Hopefully, because we started with a basic syntax
transformer, and worked up from that, we won't have that problem. We
can appreciate @racket[define-syntax-rule] as a convenient shorthand,
but not be scared of, or confused about, that for which it's
shorthand.
Most of the materials I found for learning macros, including the
Racket @italic{Guide}, do a very good job explaining
@hyperlink["http://docs.racket-lang.org/guide/pattern-macros.html" "how
patterns and templates work"]. So I won't regurgitate that here.
Sometimes, we need to go a step beyond the pattern and template. Let's
look at some examples, how we can get confused, and how to get it
working.
@; ----------------------------------------------------------------------------
@subsection{Pattern variable vs. template---fight!}
Let's say we want to define a function with a hyphenated name, a-b,
but we supply the a and b parts separately. The Racket @racket[struct]
macro does something like this: @racket[(struct foo (field1 field2))]
automatically defines a number of functions whose names are variations
on the name @racket[foo]---such as @racket[foo-field1],
@racket[foo-field2], @racket[foo?], and so on.
So let's pretend we're doing something like that. We want to transform
the syntax @racket[(hyphen-define a b (args) body)] to the syntax
@racket[(define (a-b args) body)].
A wrong first attempt is:
@i[
(define-syntax (hyphen-define/wrong1 stx)
(syntax-case stx ()
[(_ a b (args ...) body0 body ...)
(let ([name (string->symbol (format "~a-~a" a b))])
#'(define (name args ...)
body0 body ...))]))
]
Huh. We have no idea what this error message means. Well, let's try to
work it out. The "template" the error message refers to is the
@racket[#'(define (name args ...) body0 body ...)] portion. The
@racket[let] isn't part of that template. It sounds like we can't use
@racket[a] (or @racket[b]) in the @racket[let] part.
In fact, @racket[syntax-case] can have as many templates as you
want. The obvious, required template is the final expression supplying
the output syntax. But you can use @racket[syntax] (a.k.a. @litchar{#'}) on a
pattern variable. This makes another template, albeit a small, "fun
size" template. Let's try that:
@i[
(define-syntax (hyphen-define/wrong1.1 stx)
(syntax-case stx ()
[(_ a b (args ...) body0 body ...)
(let ([name (string->symbol (format "~a-~a" #'a #'b))])
#'(define (name args ...)
body0 body ...))]))
]
No more errors---good! Let's try to use it:
@i[
(hyphen-define/wrong1.1 foo bar () #t)
(foo-bar)
]
Apparently our macro is defining a function with some name other than
@racket[foo-bar]. Huh.
This is where the Macro Stepper in DrRacket is
invaluable. @margin-note{Even if you prefer mostly to use Emacs, this
is a situation where it's definitely worth temporarily using DrRacket
for its Macro Stepper.}
@image[#:scale 0.5 "macro-stepper.png"]
The Macro Stepper says that the use of our macro:
@racketblock[
(hyphen-define/wrong1.1 foo bar () #t)
]
expanded to:
@racketblock[
(define (name) #t)
]
Well that explains it. Instead, we wanted to expand to:
@racketblock[
(define (foo-bar) #t)
]
Our template is using the symbol @racket[name] but we wanted its
value, such as @racket[foo-bar] in this use of our macro.
Is there anything we already know that behaves like this---where using
a variable in the template yields its value? Yes: Pattern
variables. Our pattern doesn't include @racket[name] because we don't
expect it in the original syntax---indeed the whole point of this
macro is to create it. So @racket[name] can't be in the main
pattern. Fine---let's make an @italic{additional} pattern. We can do
that using an additional, nested @racket[syntax-case]:
@i[
(define-syntax (hyphen-define/wrong1.2 stx)
(syntax-case stx ()
[(_ a b (args ...) body0 body ...)
(syntax-case (datum->syntax #'a
(string->symbol (format "~a-~a" #'a #'b)))
()
[name #'(define (name args ...)
body0 body ...)])]))
]
Looks weird? Let's take a deep breath. Normally our transformer
function is given syntax by Racket, and we pass that syntax to
@racket[syntax-case]. But we can also create some syntax of our own,
on the fly, and pass @italic{that} to @racket[syntax-case]. That's all
we're doing here. The whole @racket[(datum->syntax ...)] expression is
syntax that we're creating on the fly. We can give that to
@racket[syntax-case], and match it using a pattern variable named
@racket[name]. Voila, we have a new pattern variable. We can use it in
a template, and its value will go in the template.
We might have one more---just one, I promise!---small problem left.
Let's try to use our new version:
@i[
(hyphen-define/wrong1.2 foo bar () #t)
(foo-bar)
]
Hmm. @racket[foo-bar] is @italic{still} not defined. Back to the Macro
Stepper. It says now we're expanding to:
@racketblock[(define (|#<syntax:11:24foo>-#<syntax:11:28 bar>|) #t)]
Oh right: @racket[#'a] and @racket[#'b] are syntax objects. Therefore
@racketblock[(string->symbol (format "~a-~a" #'a #'b))]
is the printed form of both syntax objects, joined by a hyphen:
@racketblock[|#<syntax:11:24foo>-#<syntax:11:28 bar>|]
Instead we want the datum in the syntax objects, such as the symbols
@racket[foo] and @racket[bar]. Which we get using
@racket[syntax->datum]:
@i[
(define-syntax (hyphen-define/ok1 stx)
(syntax-case stx ()
[(_ a b (args ...) body0 body ...)
(syntax-case (datum->syntax #'a
(string->symbol (format "~a-~a"
(syntax->datum #'a)
(syntax->datum #'b))))
()
[name #'(define (name args ...)
body0 body ...)])]))
(hyphen-define/ok1 foo bar () #t)
(foo-bar)
]
And now it works!
Next, some shortcuts.
@subsubsection{@racket[with-syntax]}
Instead of an additional, nested @racket[syntax-case], we could use
@racket[with-syntax]@margin-note*{Another name for
@racket[with-syntax] could be, "with new pattern variable".}. This
rearranges the @racket[syntax-case] to look more like a @racket[let]
statement---first the name, then the value. Also it's more convenient
if we need to define more than one pattern variable.
@i[
(define-syntax (hyphen-define/ok2 stx)
(syntax-case stx ()
[(_ a b (args ...) body0 body ...)
(with-syntax ([name (datum->syntax #'a
(string->symbol (format "~a-~a"
(syntax->datum #'a)
(syntax->datum #'b))))])
#'(define (name args ...)
body0 body ...))]))
(hyphen-define/ok2 foo bar () #t)
(foo-bar)
]
Again, @racket[with-syntax] is simply @racket[syntax-case] rearranged:
@racketblock[
(syntax-case #,(italic "<syntax>") () [#,(bold "<pattern>") <body>])
(with-syntax ([#,(bold "<pattern>") #,(italic "<syntax>")]) <body>)
]
Whether you use an additional @racket[syntax-case] or use
@racket[with-syntax], either way you are simply defining additional
pattern variables. Don't let the terminology and structure make it
seem mysterious.
@subsubsection{@racket[with-syntax*]}
We know that @racket[let] doesn't let us use a binding in a subsequent
one:
@i[
(let ([a 0]
[b a])
b)
]
Instead we can nest @racket[let]s:
@i[
(let ([a 0])
(let ([b a])
b))
]
Or use a shorthand for nesting, @racket[let*]:
@i[
(let* ([a 0]
[b a])
b)
]
Similarly, instead of writing nested @racket[with-syntax]s, we can use
@racket[with-syntax*]:
@i[
(require (for-syntax racket/syntax))
(define-syntax (foo stx)
(syntax-case stx ()
[(_ a)
(with-syntax* ([b #'a]
[c #'b])
#'c)]))
]
One gotcha is that @racket[with-syntax*] isn't provided by
@racket[racket/base]. We must @racket[(require (for-syntax
racket/syntax))]. Otherwise we may get a rather bewildering error
message:
@italic{@tt{...: ellipses not allowed as an expression in: ...}}.
@subsubsection{@racket[format-id]}
There is a utility function in @racket[racket/syntax] called
@racket[format-id] that lets us format identifier names more
succinctly than what we did above:
@i[
(require (for-syntax racket/syntax))
(define-syntax (hyphen-define/ok3 stx)
(syntax-case stx ()
[(_ a b (args ...) body0 body ...)
(with-syntax ([name (format-id #'a "~a-~a" #'a #'b)])
#'(define (name args ...)
body0 body ...))]))
(hyphen-define/ok3 bar baz () #t)
(bar-baz)
]
Using @racket[format-id] is convenient as it handles the tedium of
converting from syntax to symbol datum to string ... and all the way
back.
The first argument of @racket[format-id], @racket[lctx], is the
lexical context of the identifier that will be created. You almost
never want to supply @racket[stx]---the overall chunk of syntax that
the macro transforms. Instead you want to supply some more-specific
bit of syntax, such as an identifier that the user has provided to the
macro. In this example, we're using @racket[#'a]. The resulting
identifier will have the same scope as that which the user provided.
This is more likely to behave as the user expects, especially when our
macro is composed with other macros.
@subsubsection{Another example}
Finally, here's a variation that accepts an arbitrary number of name
parts to be joined with hyphens:
@i[
(require (for-syntax racket/string racket/syntax))
(define-syntax (hyphen-define* stx)
(syntax-case stx ()
[(_ (names ...) (args ...) body0 body ...)
(let ([name-stxs (syntax->list #'(names ...))])
(with-syntax ([name (datum->syntax (car name-stxs)
(string->symbol
(string-join (for/list ([name-stx name-stxs])
(symbol->string
(syntax-e name-stx)))
"-")))])
#'(define (name args ...)
body0 body ...)))]))
(hyphen-define* (foo bar baz) (v) (* 2 v))
(foo-bar-baz 50)
]
Just as when we used @racket[format-id], when using