Loading in 5 sec....

A Few Principles of Macro Design Dave Herman / David Van HornPowerPoint Presentation

A Few Principles of Macro Design Dave Herman / David Van Horn

Download Presentation

A Few Principles of Macro Design Dave Herman / David Van Horn

Loading in 2 Seconds...

- 106 Views
- Uploaded on
- Presentation posted in: General

A Few Principles of Macro Design Dave Herman / David Van Horn

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

A Few Principles of Macro DesignDave Herman / David Van Horn

What is hygiene?

…but hopefully useful

Quotation, which thus interrupts the referential force of aterm, may be said to fail of referential transparency.

— Quine, Word and Object (1960)

- Hygiene alone doesn’t provide reasoning principles for macros.Where did we go wrong?
- Disciplined macros make reasoning possible through interfaces.What can we do about it?

> (define f (fexpr (form env)

(printf "input: ~a\n" form)

(eval (car form) env)))

> (f (+ 23))

input: ((+ 2 3))

5

(f (+ 2 3))

(f 5)

- Compilers: can’t do source-to-source optimizations
- Programmers: refactoring is brittle
(+ 2 3) ≠ 5

(lambda (x) x) ≠ (lambda (y) y)

(list 1) ≠ (cons 1 '())

(m (+ 2 3))

(m 5)

Even syntax-rules macros can:

- Destructure their input
- Compare identifiers
- Copy identifiers into different contexts
- Quote their input
(see Petrofsky ‘01, Kiselyov ‘02)

(sexp=? (+ 2 3) (+ 2 3) "true" "false")

(sexp=? (+ 2 3) 5 "true" "false")

(sexp=? s1 s2 tk fk)

(+ 2 3)

"true"

5

"false"

No interesting equivalence for unexpanded Scheme respects all possible contexts.

Referentially opaque contexts:

(sexp=? s □ tk fk)

(quote □)

Turing-completeness: undecidable whether such contexts will arise during expansion.

(let ((x 42)) x)

(let ((y 42)) y)

(define-syntax let

(syntax-rules ()

((let ((name val) ...) body1 body2 ...)

((lambda (name ...) body1 body2 ...)

val ...))

((let tag ((name val) ...) body1 body2 ...)

((letrec ((tag (lambda (name ...)

body1 body2 ...)))

tag)

val ...))))

(let ((x 42)) x)

((lambda (x) x) 42)

((lambda (x) x[x := x]) 42)

((lambda (x) x) 42)

(let ((y 42)) y)

((lambda (y) y) 42)

((lambda (y) y[y := y]) 42)

((lambda (y) y) 42)

- We don’t force clients of procedures to step through evaluation.
- We don’t want to force clients of macros to, either!
- Clients shouldn’t—and don’t—rely exclusively on tools.
- Extending Scheme new equivalences, contexts.
- Good macros document this through their interfaces.

(let ((identexpr) ...) expr) :: expr

New expression-context forms:

(let ((x1 e1) ... (xi□) (xi+1 ei+1) ...) e)

(let ((x e) ...) □)

;; (peek expr) :: expr

(define-syntax peek

(syntax-rules (lambda)

((_ (lambda (x ...) e ...))

(begin e ...))

((_ e)

e)))

(peek id)

≠

(peek (lambda (x) x))

id

(let ((x:identexpr) ...) expr[x ...]) :: expr

e1 = e′1zfreshe2[x := z] = e′2[x′ := z]

(let ((x e1))e2) =(let ((x′ e′1))e′2)

- Such a relation will not respect all contexts
- But perhaps contexts from macros with good interfaces

;; (lambda* (x:ident) expr[x]) :: expr

(define-syntax lambda*

(syntax-rules (foo)

((_ (foo) e)

(error 'm "I don't like the name foo"))

((_ (x) e)

(lambda (x) e))))

(lambda* (a) a)

≠

(lambda* (foo) foo)

;; (set-lambda (x:ident) expr[x]) :: expr

(define-syntax set-lambda

(syntax-rules ()

((_ (x) e)

(begin (set! x e)

(lambda (x) e)))))

(set-lambda (x) x)

≠

(set-lambda (cons) cons)

(define frame

(subclass window

(inherit width

height)

;; …

(define area (* width height))

))

(define frame

(subclass window

(inherit (width as window:width)

(height as window:height))

;; …

(define area (* window:width window:height))

))

- Rational extension of program contexts.
- Rational extension of program equivalences.

(let ((x (+ 2 3))) x)

(let ((x 5)) x)

(let ((y 5)) y)

- Like fexprs, even hygienic macros provide very fine syntactic introspection.
- Syntactic abstractions should be comprehensible without inspecting their expansion.
- Designing macros with good interfaces allows programmers to reason about unexpanded programs.

Thank you.