A few principles of macro design dave herman david van horn
Download
1 / 29

A Few Principles of Macro Design Dave Herman / David Van Horn - PowerPoint PPT Presentation


  • 117 Views
  • Uploaded on

A Few Principles of Macro Design Dave Herman / David Van Horn. Motivated by theory…. What is hygiene?. …but hopefully useful. Quotation, which thus interrupts the referential force of a term, may be said to fail of referential transparency. — Quine, Word and Object (1960).

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' A Few Principles of Macro Design Dave Herman / David Van Horn' - gazit


An Image/Link below is provided (as is) to download presentation

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 - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
A few principles of macro design dave herman david van horn

A Few Principles of Macro DesignDave Herman / David Van Horn


Motivated by theory
Motivated by theory…

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)


How to substitute equals for equals
How to substitute equals for equals?

  • 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?



Fexprs
Fexprs

> (define f (fexpr (form env)

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

(eval (car form) env)))

> (f (+ 23))

input: ((+ 2 3))

5


The trouble with fexprs
The trouble with fexprs

(f (+ 2 3))

(f 5)


Why this is bad pitman wand
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 '())


Same w macros at compile time
Same w/macros—at compile-time

(m (+ 2 3))

(m 5)


Macros are introspective
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)


A macrological microscope

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

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

A macrological microscope

(sexp=? s1 s2 tk fk)

(+ 2 3)

 "true"

5

 "false"


Consequence
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.


Are these programs the same
Are these programs the same?

(let ((x 42)) x)

(let ((y 42)) y)


What if let is a macro
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 ...))))


Expand it to understand it
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)



Syntactic abstraction
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.




The interface of let take 1
The interface of let (take 1)

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

New expression-context forms:

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

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


Principle opaque subexpressions
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



The interface of let take 2
The interface of let (take 2)

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

e1 = e′1 zfreshe2[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


Principle opaque variables
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)


Principle consistent identifiers
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)


Principles not laws
Principles, not laws

(define frame

(subclass window

(inherit width

height)

;; …

(define area (* width height))

))


Principles not laws1
Principles, not laws

(define frame

(subclass window

(inherit (width as window:width)

(height as window:height))

;; …

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

))


Design for program equivalences
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)


Moral
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.


ad