courses.cs.washington.edu

courses.cs.washington.edu

CSE341: Programming Languages Section 7 Macros Winter 2019 What is a macro A macro definition describes how to transform some new syntax into different syntax in the source language A macro is one way to implement syntactic sugar Replace any syntax of the form e1 andalso e2 with if e1 then e2 else false A macro system is a language (or part of a larger language) for defining macros

Macro expansion is the process of rewriting the syntax for each macro use Before a program is run (or even compiled) Winter 2019 CSE341: Programming Languages 2 Using Racket Macros If you define a macro m in Racket, then m becomes a new special form: Use (m ) gets expanded according to definition

Example definitions (actual definitions coming later): Expand (my-if e1 then e2 else e3) to (if e1 e2 e3) Expand (comment-out e1 e2) to e2 Expand (my-delay e) to (mcons #f (lambda () e)) Winter 2019 CSE341: Programming Languages 3 Example uses It is like we added keywords to our language Other keywords only keywords in uses of that macro Syntax error if keywords misused Rewriting (expansion) happens before execution (my-if x then y else z)

(my-if x then y then z) ; (if x y z) ; syntax error (comment-out (car null) #f) (my-delay (begin (print "hi") (foo 15))) Winter 2019 CSE341: Programming Languages 4 Overuse Macros often deserve a bad reputation because they are often overused or used when functions would be better When in doubt, resist defining a macro? But they can be used well Winter 2019

CSE341: Programming Languages 5 Now How any macro system must deal with tokens, parentheses, and scope How to define macros in Racket How macro definitions must deal with expression evaluation carefully Order expressions evaluate and how many times

The key issue of variable bindings in macros and the notion of hygiene Racket is superior to most languages here Winter 2019 CSE341: Programming Languages 6 Tokenization First question for a macro system: How does it tokenize? Macro systems generally work at the level of tokens not sequences of characters So must know how programming language tokenizes text

Example: macro expand head to car Would not rewrite (+ headt foo) to (+ cart foo) Would not rewrite head-door to car-door But would in C where head-door is subtraction Winter 2019 CSE341: Programming Languages 7 Parenthesization Second question for a macro system: How does associativity work? C/C++ basic example: #define ADD(x,y) x+y Probably not what you wanted:1 + 2 / 3 * 4 ADD(1,2/3)*4

means not (1 + 2 / 3) * 4 So C macro writers use lots of parentheses, which is fine: #define ADD(x,y) ((x)+(y)) Racket wont have this problem: Macro use: (macro-name ) After expansion: something else in same place Winter 2019 CSE341: Programming Languages 8 Local bindings Third question for a macro system: Can variables shadow macros? Suppose macros also apply to variable bindings. Then: (let ([head 0][car 1]) head) ; 0

(let* ([head 0][car 1]) head) ; 0 Would become: (let ([car 0][car 1]) car) ; error (let* ([car 0][car 1]) car) ; 1 This is why C/C++ convention is all-caps macros and non-all-caps for everything else Racket does not work this way it gets scope right! Winter 2019 CSE341: Programming Languages 9 Example Racket macro definitions Two simple macros (define-syntax my-if ; macro name (syntax-rules (then else) ; other keywords [(my-if e1 then e2 else e3) ; macro use (if e1 e2 e3)]))

; form of expansion (define-syntax comment-out ; macro name (syntax-rules () ; other keywords [(comment-out ignore instead) ; macro use instead])) ; form of expansion If the form of the use matches, do the corresponding expansion In these examples, list of possible use forms has length 1 Else syntax error Winter 2019 CSE341: Programming Languages 10 Revisiting delay and force Recall our definition of promises from earlier Should we use a macro instead to avoid clients explicit thunk? (define (my-delay th)

(mcons #f th)) (define (my-force p) (if (mcar p) (mcdr p) (begin (set-mcar! p #t) (set-mcdr! p ((mcdr p))) (mcdr p)))) (f (my-delay (lambda () e))) (define (f p) ( (my-force p) )) Winter 2019 CSE341: Programming Languages 11 A delay macro

A macro can put an expression under a thunk Delays evaluation without explicit thunk Cannot implement this with a function Now client should not use a thunk (that would double-thunk) Rackets pre-defined delay is a similar macro (define-syntax my-delay (syntax-rules () [(my-delay e) (mcons #f (lambda() e))])) (f (my-delay e)) Winter 2019 CSE341: Programming Languages 12 What about a force macro? We could define my-force with a macro too Good macro style would be to evaluate the argument exactly once (use x below, not multiple evaluations of e)

Which shows it is bad style to use a macro at all here! Do not use macros when functions do what you want (define-syntax my-force (syntax-rules () [(my-force e) (let([x e]) (if (mcar x) (mcdr x) (begin (set-mcar! x #t) (set-mcdr! p ((mcdr p))) (mcdr p))))])) Winter 2019 CSE341: Programming Languages 13 Another bad macro Any function that doubles its argument is fine for clients (define (dbl x) (+ x x)) (define (dbl x) (* 2 x))

These are equivalent to each other So macros for doubling are bad style but instructive examples: (define-syntax dbl (syntax-rules()[(dbl x)(+ x x)])) (define-syntax dbl (syntax-rules()[(dbl x)(* 2 x)])) These are not equivalent to each other. Consider: (dbl (begin (print "hi") 42)) Winter 2019 CSE341: Programming Languages 14 More examples Sometimes a macro should re-evaluate an argument it is passed If not, as in dbl, then use a local binding as needed: (define-syntax dbl (syntax-rules () [(dbl x) (let ([y x]) (+ y y))])) Also good style for macros not to have surprising evaluation order Good rule of thumb to preserve left-to-right

Bad example (fix with a local binding): (define-syntax take (syntax-rules (from) [(take e1 from e2) (- e2 e1)])) Winter 2019 CSE341: Programming Languages 15 Local variables in macros In C/C++, defining local variables inside macros is unwise When needed done with hacks like __strange_name34 Here is why with a silly example: Macro: (define-syntax dbl (syntax-rules () [(dbl x) (let ([y 1]) (* 2 x y))])) Use:

(let ([y 7]) (dbl y)) Nave expansion: (let ([y 7]) (let ([y 1]) (* 2 y y))) But instead Racket gets it right, which is part of hygiene Winter 2019 CSE341: Programming Languages 16 The other side of hygiene This also looks like it would do the wrong thing Macro: Use: (define-syntax dbl (syntax-rules () [(dbl x) (* 2 x)]))

(let ([* +]) (dbl 42)) Nave expansion: (let ([* +]) (* 2 42)) But again Rackets hygienic macros get this right! Winter 2019 CSE341: Programming Languages 17 How hygienic macros work A hygienic macro system: 1. Secretly renames local variables in macros with fresh names 2. Looks up variables used in macros where the macro is defined Neither of these rules are followed by the nave expansion most macro systems use Without hygiene, macros are much more brittle (non-modular) On rare occasions, hygiene is not what you want Racket has somewhat complicated support for that

Winter 2019 CSE341: Programming Languages 18 More examples See the code for macros that: A for loop for executing a body a fixed number of times Shows a macro that purposely re-evaluates some expressions and not others Allow 0, 1, or 2 local bindings with fewer parens than let* Shows a macro with multiple cases

A re-implementation of let* in terms of let Shows a macro taking any number of arguments Shows a recursive macro Winter 2019 CSE341: Programming Languages 19

Recently Viewed Presentations

  • Disturbi Schizofrenici

    Disturbi Schizofrenici

    Il ruolo del glutammato, sebbene non del tutto chiaro nella fisiopatologia della depressione, costituisce oggetto di recenti studi con particolare riferimento al ruolo dei recettori NMDA nella modulazione della plasticità sinaptica a livello di aree cerebrali coinvolte nella depressione come...
  • November 4 & 5, 2015 Air Quality Committee

    November 4 & 5, 2015 Air Quality Committee

    Does the technology and/or work practices result in increases in non-GHG pollutants (e.g., SO 2, NOx, and mercury)?3. Technical feasibility. Based on age, size, type, class, and design, canthe technology and/or work practices be implemented at the EGU? 4. Non-air...
  • AP Rhetorical Analysis - PC\|MAC

    AP Rhetorical Analysis - PC\|MAC

    Body paragraph: Analysis of all devices and appeals in the middle of the essay. IV. Body paragraph: A discussion of the end of the essay including an analysis of the conclusion. V. Conclusion: Restate your thesis (OR bring your last...
  • jobs.nsw Guidelines for Conveners: 6 Submit short list

    jobs.nsw Guidelines for Conveners: 6 Submit short list

    Click on 'Submit Short List HRSO' to complete the action to release the requisition to Staffing Services for review and approval of the candidates short listed. 1. NSW DEPARTMENT OF EDUCATION AND COMMUNITIES WWW.DEC.NSW.GOV.AU V4. 21Oct13. There are a number...
  • Fracp Program Assessment

    Fracp Program Assessment

    * Mentors & Supervisors Member of Senior Staff, individual PDAs (Professional Development Advisors) & ESs (Education Supervisors) Alfred Health Director of Physician Education: Dr Michael Oldmeadow (contact through Alfred switchboard) Alfred Health Director of Clinical Training & Consortium Director of...
  • Emily Carr Secondary School Scholarships, Bursaries, Awards, Grants,

    Emily Carr Secondary School Scholarships, Bursaries, Awards, Grants,

    Emily Carr Secondary School Scholarships, Bursaries, Awards, Grants, Loans and Financial Aid Today's Agenda Where to Find Useful Resources Financing Your Education Definitions of Terms Variety of Ways to Qualify Diverse Criteria How to Begin the Search Specific Upcoming Application...
  • Slide sem título

    Slide sem título

    به نام خدا اپیدمیولوژی بیماریهای عفونی Dr Seyyed Alireza Moravveji MD Community Medicine specialist * Dr Seyyed Alireza Moravveji MD Community Medicine specialist * Dr Seyyed Alireza Moravveji MD Community Medicine specialist * مراحل بررسی اپیدمیها تجزیه تحلیل داده های...
  • Computational Intelligence 696i

    Computational Intelligence 696i

    John is too stubborn [someone] to talk to [John] John is too stubborn [John] to talk to Bill Case Study: Gap Filling hope I've convinced you all that gap filling is something you do correctly without even thinking about it...