A Few Principles of Macro Design Dave Herman / David Van Horn
description
Transcript of A Few Principles of Macro Design Dave Herman / David Van Horn
![Page 1: A Few Principles of Macro Design Dave Herman / David Van Horn](https://reader035.fdocuments.us/reader035/viewer/2022070403/56813959550346895da0fb69/html5/thumbnails/1.jpg)
1
A Few Principles of Macro DesignDave Herman / David Van Horn
![Page 2: A Few Principles of Macro Design Dave Herman / David Van Horn](https://reader035.fdocuments.us/reader035/viewer/2022070403/56813959550346895da0fb69/html5/thumbnails/2.jpg)
2
Motivated by theory…
What is hygiene?
…but hopefully useful
![Page 3: A Few Principles of Macro Design Dave Herman / David Van Horn](https://reader035.fdocuments.us/reader035/viewer/2022070403/56813959550346895da0fb69/html5/thumbnails/3.jpg)
3
— Quine, Word and Object (1960)
Quotation, which thus interrupts the referential force of aterm, may be said to fail of referential transparency.
![Page 4: A Few Principles of Macro Design Dave Herman / David Van Horn](https://reader035.fdocuments.us/reader035/viewer/2022070403/56813959550346895da0fb69/html5/thumbnails/4.jpg)
4
How to substitute equals for equals?1. Hygiene alone doesn’t provide reasoning principles for
macros.Where did we go wrong?
2. Disciplined macros make reasoning possible through interfaces.What can we do about it?
![Page 5: A Few Principles of Macro Design Dave Herman / David Van Horn](https://reader035.fdocuments.us/reader035/viewer/2022070403/56813959550346895da0fb69/html5/thumbnails/5.jpg)
5
Part I. Fexprs
![Page 6: A Few Principles of Macro Design Dave Herman / David Van Horn](https://reader035.fdocuments.us/reader035/viewer/2022070403/56813959550346895da0fb69/html5/thumbnails/6.jpg)
6
Fexprs
> (define f (fexpr (form env) (printf "input: ~a\n" form) (eval (car form) env)))> (f (+ 2 3))input: ((+ 2 3))5
![Page 7: A Few Principles of Macro Design Dave Herman / David Van Horn](https://reader035.fdocuments.us/reader035/viewer/2022070403/56813959550346895da0fb69/html5/thumbnails/7.jpg)
7
The trouble with fexprs
(f (+ 2 3))
(f 5)
![Page 8: A Few Principles of Macro Design Dave Herman / David Van Horn](https://reader035.fdocuments.us/reader035/viewer/2022070403/56813959550346895da0fb69/html5/thumbnails/8.jpg)
8
Why this is bad (Pitman, Wand)• 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 '())
![Page 9: A Few Principles of Macro Design Dave Herman / David Van Horn](https://reader035.fdocuments.us/reader035/viewer/2022070403/56813959550346895da0fb69/html5/thumbnails/9.jpg)
9
Same w/macros—at compile-time
(m (+ 2 3))
(m 5)
![Page 10: A Few Principles of Macro Design Dave Herman / David Van Horn](https://reader035.fdocuments.us/reader035/viewer/2022070403/56813959550346895da0fb69/html5/thumbnails/10.jpg)
10
Macros are introspective
Even syntax-rules macros can:• Destructure their input• Compare identifiers• Copy identifiers into different contexts• Quote their input
(see Petrofsky ‘01, Kiselyov ‘02)
![Page 11: A Few Principles of Macro Design Dave Herman / David Van Horn](https://reader035.fdocuments.us/reader035/viewer/2022070403/56813959550346895da0fb69/html5/thumbnails/11.jpg)
11
(sexp=? (+ 2 3) 5 "true" "false")
(sexp=? (+ 2 3) (+ 2 3) "true" "false")
A macrological microscope
(sexp=? s1 s2 tk fk)
(+ 2 3) "true"
5 "false"
![Page 12: A Few Principles of Macro Design Dave Herman / David Van Horn](https://reader035.fdocuments.us/reader035/viewer/2022070403/56813959550346895da0fb69/html5/thumbnails/12.jpg)
12
Consequence
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.
![Page 13: A Few Principles of Macro Design Dave Herman / David Van Horn](https://reader035.fdocuments.us/reader035/viewer/2022070403/56813959550346895da0fb69/html5/thumbnails/13.jpg)
13
Are these programs the same?
(let ((x 42)) x)
(let ((y 42)) y)
![Page 14: A Few Principles of Macro Design Dave Herman / David Van Horn](https://reader035.fdocuments.us/reader035/viewer/2022070403/56813959550346895da0fb69/html5/thumbnails/14.jpg)
14
What if let is a macro?
(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 ...))))
![Page 15: A Few Principles of Macro Design Dave Herman / David Van Horn](https://reader035.fdocuments.us/reader035/viewer/2022070403/56813959550346895da0fb69/html5/thumbnails/15.jpg)
15
Expand it to understand it
(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)
![Page 16: A Few Principles of Macro Design Dave Herman / David Van Horn](https://reader035.fdocuments.us/reader035/viewer/2022070403/56813959550346895da0fb69/html5/thumbnails/16.jpg)
16
Expand it to understand it
![Page 17: A Few Principles of Macro Design Dave Herman / David Van Horn](https://reader035.fdocuments.us/reader035/viewer/2022070403/56813959550346895da0fb69/html5/thumbnails/17.jpg)
17
Syntactic abstraction
• 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.
![Page 18: A Few Principles of Macro Design Dave Herman / David Van Horn](https://reader035.fdocuments.us/reader035/viewer/2022070403/56813959550346895da0fb69/html5/thumbnails/18.jpg)
18
Part II. Interfaces
![Page 19: A Few Principles of Macro Design Dave Herman / David Van Horn](https://reader035.fdocuments.us/reader035/viewer/2022070403/56813959550346895da0fb69/html5/thumbnails/19.jpg)
19
The specification of let
![Page 20: A Few Principles of Macro Design Dave Herman / David Van Horn](https://reader035.fdocuments.us/reader035/viewer/2022070403/56813959550346895da0fb69/html5/thumbnails/20.jpg)
20
The interface of let (take 1)
(let ((ident expr) ...) expr) :: expr
New expression-context forms:
(let ((x1 e1) ... (xi □) (xi+1 ei+1) ...) e)
(let ((x e) ...) □)
![Page 21: A Few Principles of Macro Design Dave Herman / David Van Horn](https://reader035.fdocuments.us/reader035/viewer/2022070403/56813959550346895da0fb69/html5/thumbnails/21.jpg)
21
Principle: opaque subexpressions;; (peek expr) :: expr(define-syntax peek (syntax-rules (lambda) ((_ (lambda (x ...) e ...)) (begin e ...)) ((_ e) e)))
(peek id)≠
(peek (lambda (x) x))
id
![Page 22: A Few Principles of Macro Design Dave Herman / David Van Horn](https://reader035.fdocuments.us/reader035/viewer/2022070403/56813959550346895da0fb69/html5/thumbnails/22.jpg)
22
The specification of let
![Page 23: A Few Principles of Macro Design Dave Herman / David Van Horn](https://reader035.fdocuments.us/reader035/viewer/2022070403/56813959550346895da0fb69/html5/thumbnails/23.jpg)
23
The interface of let (take 2)
(let ((x:ident expr) ...) expr[x ...]) :: expr
e1 = e′1 z fresh e2[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
![Page 24: A Few Principles of Macro Design Dave Herman / David Van Horn](https://reader035.fdocuments.us/reader035/viewer/2022070403/56813959550346895da0fb69/html5/thumbnails/24.jpg)
24
Principle: opaque variables
;; (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)
![Page 25: A Few Principles of Macro Design Dave Herman / David Van Horn](https://reader035.fdocuments.us/reader035/viewer/2022070403/56813959550346895da0fb69/html5/thumbnails/25.jpg)
25
Principle: consistent identifiers;; (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)
![Page 26: A Few Principles of Macro Design Dave Herman / David Van Horn](https://reader035.fdocuments.us/reader035/viewer/2022070403/56813959550346895da0fb69/html5/thumbnails/26.jpg)
26
Principles, not laws
(define frame (subclass window (inherit width height) ;; … (define area (* width height)) ))
![Page 27: A Few Principles of Macro Design Dave Herman / David Van Horn](https://reader035.fdocuments.us/reader035/viewer/2022070403/56813959550346895da0fb69/html5/thumbnails/27.jpg)
27
Principles, not laws
(define frame (subclass window (inherit (width as window:width) (height as window:height)) ;; … (define area (* window:width window:height)) ))
![Page 28: A Few Principles of Macro Design Dave Herman / David Van Horn](https://reader035.fdocuments.us/reader035/viewer/2022070403/56813959550346895da0fb69/html5/thumbnails/28.jpg)
28
Design for program equivalences• Rational extension of program contexts.• Rational extension of program equivalences.
(let ((x (+ 2 3))) x)(let ((x 5)) x)(let ((y 5)) y)
![Page 29: A Few Principles of Macro Design Dave Herman / David Van Horn](https://reader035.fdocuments.us/reader035/viewer/2022070403/56813959550346895da0fb69/html5/thumbnails/29.jpg)
29
Moral
• 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.