λ λ CS 251 Fall 2019 CS 251 Fall 2019 Topics Principles of Programming Languages Principles of Programming Languages Ben Wood Ben Wood Macros Macros: • Design considerations • Style considerations User-Extensible Syntax • Hygiene https://cs.wellesley.edu/~cs251/f19/ 1 Hygienic Macros 2 Hygienic Macros Example Racket Macros Macro = user-defined syntactic sugar Definitions: • A ma macro o definition on describes how to – Expand (my-if e1 then e2 else e3) to (if e1 e2 e3) transform some new syntax into different – Expand (comment-out e1 e2) syntax in the source language. to e2 It is like we added keywords to our language • A ma macro o system is a language (or part of a – Other keywords only keywords in uses of that macro – Syntax error if keywords misused larger language) for defining macros. – Rewriting (“expansion”) happens before execution Uses: • Mac Macro expan ansi sion is the process of rewriting the syntax for each ma macro o use. (my-if x then y else z) ; (if x y z) (my-if x then y then z) ; syntax error – Be Before a a program am is run n (or even n compiled) (comment-out (car null) #f) Hygienic Macros 3 Hygienic Macros 4
Macro system design: Overuse Tokenization Macros sometimes get a bad wrap for being Macro systems generally work at the level of tokens not sequences of characters overused. – So must know how programming language tokenizes text Example: “macro expand head to car ” Rule of thumb: – Would not rewrite (+ headt foo) to (+ cart foo) Use macros only where functions would be – Would not rewrite head-door to car-door • But would in C where head-door is subtraction awkward or impossible. They can be useful! Hygienic Macros 5 Hygienic Macros 6 Macro system design: Macro system design: Associativity and Parenthesization Local bindings and shadowing Suppose macros also apply to variable bindings. C/C++ preprocessor example: Then: “macro expand head to car ” #define ADD(x,y) x+y (let ([head 0][car 1]) head) ; 0 (let* ([head 0][car 1]) head) ; 0 ADD(1,2/3)*4 means 1 + 2 / 3 * 4 not (1 + 2 / 3) * 4 Would become: (let ([car 0][car 1]) car) ; error (let* ([car 0][car 1]) car) ; 1 "Solved" with emphatic parenthesization by the programmer: C/C++ "safety by convention": all-caps macros and non- #define ADD(x,y) ((x)+(y)) all-caps everything else Racket S-expression syntax trivially avoids this problem! Racket does not work this way – it gets scope “right”! Hygienic Macros 7 Hygienic Macros 8
Macro style Example Racket macro definitions Two simple macros Equivalent functions that double their argument: (define (dbl x) (+ x x)) (define-syntax my-if ; macro name (define (dbl x) (* 2 x)) (syntax-rules (then else) ; other keywords [(my-if e1 then e2 else e3) ; macro use (if e1 e2 e3)])) ; form of expansion Bad sty Ba style as a macro: (define-syntax comment-out ; macro name (define-syntax dbl (syntax-rules()[(dbl x)(+ x x)])) (syntax-rules () ; other keywords (define-syntax dbl (syntax-rules()[(dbl x)(* 2 x)])) [(comment-out ignore instead) ; macro use instead])) ; form of expansion 1. If a function works fine, don't use a macro. If a syntactic form matches, do the corresponding expansion 2. Avoid surprising reevaluation: – In these examples, list of possible use forms has length 1 (dbl (begin (print "hi") 42)) – Else syntax error Hygienic Macros 9 Hygienic Macros 10 Macro style Hygiene: avoid accidental shadowing Avoid surprising reevaluation. Use a local binding: In C/C++, defining local variables inside macros is unwise – When needed done with hacks like __strange_name34 (define-syntax dbl (syntax-rules () Example: [(dbl x) Macro: (let ([y x]) (+ y y))])) define-syntax dbl (syntax-rules () [(dbl x) (let ([y 1]) (* 2 x y))])) Avoid surprising evaluation order, such as: Use: (define-syntax take (let ([y 7]) (dbl y)) (syntax-rules (from) [(take e1 from e2) Naïve expansion: (let ([y 7]) (let ([y 1]) (- e2 e1)])) (* 2 y y))) Fix with a local binding as above. Racket hygienic macros avoid this problem. Hygienic Macros 11 Hygienic Macros 12
Maintaining macro hygiene Hygiene: avoid accidental shadowing A hygienic macro system: Example: 1. Secretly renames local variables in macros with fresh names Macro: 2. Looks up variables used in macros where the macro is (define-syntax dbl defined (syntax-rules () [(dbl x) (* 2 x)])) Neither of these rules are followed by the “naïve expansion” most macro systems use Use: – Without hygiene, macros are much more brittle (non-modular) (let ([* +]) (dbl 42)) On rare occasions, hygiene is not what you want Naïve expansion: – Racket has somewhat complicated support for that (let ([* +]) (* 2 42)) Sound familiar? Analogous to ____________ vs. ____________. Racket hygienic macros avoid this problem. More examples in code: for loop, less parensy lets, let* as sugar. Hygienic Macros 13 Hygienic Macros 14
Recommend
More recommend