A Theory of Hygienic Macros David Herman Northeastern University.

Slides:



Advertisements
Similar presentations
Substitution & Evaluation Order cos 441 David Walker.
Advertisements

Semantics Static semantics Dynamic semantics attribute grammars
The lambda calculus David Walker CS 441. the lambda calculus Originally, the lambda calculus was developed as a logic by Alonzo Church in 1932 –Church.
Elements of Lambda Calculus Functional Programming Academic Year Alessandro Cimatti
Recap 1.Programmer enters expression 2.ML checks if expression is “well-typed” Using a precise set of rules, ML tries to find a unique type for the expression.
Programming Paradigms for Concurrency Lecture 11 Part III – Message Passing Concurrency TexPoint fonts used in EMF. Read the TexPoint manual before you.
Cs784(TK)1 Semantics of Procedures and Scopes. Kinds of Scope Static or Lexical scope –determined by structure of program –Scheme, C++, Java, and many.
1 Meta-Programming through Typeful Code Representation Chiyan Chen and Hongwei Xi Boston University.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Fall 2011.
Foundations of Programming Languages: Introduction to Lambda Calculus
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Defining practical programming languages Carlos Varela RPI.
School of Computing and Mathematics, University of Huddersfield CAS810: WEEK 3 LECTURE: LAMBDA CALCULUS PRACTICAL/TUTORIAL: (i) Do exercises given out.
The lambda calculus David Walker CS 441. the lambda calculus Originally, the lambda calculus was developed as a logic by Alonzo Church in 1932 –Church.
Syntax With Binders COS 441 Princeton University Fall 2004.
Chair of Software Engineering 1 Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan.
CSE 130 : Winter 2006 Programming Languages Ranjit Jhala UC San Diego Lecture 7: Polymorphism.
1 Scheme Although Scheme is syntactically a simple language, it supports sophisticated modeling: higher-order functions are a powerful tool in describing.
Tim Sheard Oregon Graduate Institute Lecture 11: A Reduction Semantics for MetaML CS510 Section FSC Winter 2005 Winter 2005.
Describing Syntax and Semantics
C. Varela1 Lambda Calculus alpha-renaming, beta reduction, applicative and normal evaluation orders, Church-Rosser theorem, combinators Carlos Varela Rennselaer.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
A Theory of Hygienic Macros PhD Thesis Proposal David Herman.
4 Dec 2001Kestrel1 From Patterns to Programming Languages Matthias Felleisen Northeastern University.
Cs7100(Prasad)L8Proc1 Procedures. cs7100(Prasad)L8Proc2 Primitive procedures  etc User-defined procedures –Naming a sequence of operations.
CSE S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
CSC 580 – Theory of Programming Languages, Spring, 2009 Week 9: Functional Languages ML and Haskell, Dr. Dale E. Parson.
A Theory of Hygienic Macros David Herman, Mitchell Wand Northeastern University.
Lexicografie computationala Feb., 2012 Anca Dinu University of Bucharest.
A Locally Nameless Theory of Objects 1.Introduction:  -calculus and De Bruijn notation 2.locally nameless technique 3.formalization in Isabelle and proofs.
CS 363 Comparative Programming Languages Semantics.
1 ML fun x -> e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes exceptions structures functors let f x = e variables These.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Semantics In Text: Chapter 3.
Types and Programming Languages Lecture 6 Simon Gay Department of Computing Science University of Glasgow 2006/07.
1 Formal Semantics. 2 Why formalize? ML is tricky, particularly in corner cases generalizable type variables? polymorphic references? exceptions? Some.
Implementing a Dependently Typed λ -Calculus Ali Assaf Abbie Desrosiers Alexandre Tomberg.
-Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH.
Lambda Calculus CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Mechanizing Metatheory without Typing Contexts TYPES 2011 September 10, 2011 Jonghyun Park, Jeongbong Seo, Sungwoo Park, Gyesik Lee* Pohang University.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:
CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 5: Functions and Closures.
Spring 2008Programming Development Techniques 1 Topic 5.5 Higher Order Procedures (This goes back and picks up section 1.3 and then sections in Chapter.
Interpreters and Higher-Order Functions CSE 413 Autumn 2008 Credit: CSE341 notes by Dan Grossman.
Cs3180 (Prasad)L156HOF1 Higher-Order Functions. cs3180 (Prasad)L156HOF2 Equivalent Notations (define (f x y) (… body …)) = (define f (lambda (x y) (…
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L04Ext-1 September 21, 2006http:// Some more thoughts.
CSE-321 Programming Languages -Calculus (II) POSTECH March 26, 2007 박성우.
Cs776 (Prasad)L2HOF1 Higher-Order Functions. cs776 (Prasad)L2HOF2 Higher-Order Functions A function that takes a function as argument and/or returns a.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
6/21/20161 Programming Languages and Compilers (CS 421) Reza Zamani Based in part on slides by Mattox Beckman,
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L03-1 September 14, 2006http:// -calculus: A Basis for.
Reasoning about Hygienic Macros David Herman Northeastern University
Chapter 2: Lambda Calculus
CS 550 Programming Languages Jeremy Johnson
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Lambda Calculus CSE 340 – Principles of Programming Languages
Carlos Varela Rennselaer Polytechnic Institute September 5, 2017
CS 611: Lecture 9 More Lambda Calculus: Recursion, Scope, and Substitution September 17, 1999 Cornell University Computer Science Department Andrew Myers.
Original material by Eric Grimson
Is everyone signed up on piazza?
Carlos Varela Rennselaer Polytechnic Institute September 4, 2015
Programming Languages and Compilers (CS 421)
FP Foundations, Scheme In Text: Chapter 14.
6.001 SICP Variations on a Scheme
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
CSE S. Tanimoto Lambda Calculus
Carlos Varela Rennselaer Polytechnic Institute September 6, 2019
Presentation transcript:

A Theory of Hygienic Macros David Herman Northeastern University

2 The power of macros Derived (user-defined) syntactic constructs Defined by rewriting to existing constructs Translated at compile-time (define-macro (increment! a) (set! a (+ a 1)))

3 The power of macros Abstractions for scope, control, program structure Powerful meta-programming idioms Allow for a minimal language core functions, recursion, iteration, primitive datatypes custom loop forms, derived datatypes, first-class module systems, object- orientation, test harnesses, data constructors, laziness, short-circuit boolean operators, iteration protocols, static analyses, parser generators, partial evaluation, debuggers, coroutines, exceptions, threads, type checkers, introspection, etc… local bindings, software contracts, language designer language user

4 The need for hygiene (define-macro (or e1 e2) (with ([t e1]) (if t t e2))) (with ([x 1]) (or #f x)) => (with ([x 1]) (with ([t #f]) (if t t x))) (with ([t 1]) (or #f t)) => (with ([t 1]) (with ([t #f]) (if t t t)))

5 The need for hygiene (define-macro (or e1 e2) (with ([t e1]) (if t t e2))) (with ([x 1]) (or #f x)) => (with ([x 1]) (with ([t #f]) (if t t x))) (with ([t 1]) (or #f t)) => (with ([t 1]) (with ([t ′ #f]) (if t ′ t ′ t)))

6 What are hygienic macros?

7 (define-macro (or e1 e2) (with ([t e1]) (if t t e2))) (with ([x 1]) (or #f x)) => (with ([x 1]) (with ([t #f]) (if t t x))) (with ([t 1]) (or #f t)) => (with ([t 1]) (with ([t #f]) (if t t t)))

8 What are hygienic macros?

9 KFFD ’86: Generated identifiers that become binding instances in the completely expanded program must only bind variables that are generated at the same transcription step. i.e., not provided as the macro’s input i.e., a single rewriting of a macro application

10 What are hygienic macros? Clinger and Rees ’91: 1. It is impossible to write a macro that inserts a binding that can capture references other than those inserted by the macro. 2. It is impossible to write a macro that inserts a reference that can be captured by bindings other than those inserted by the macro. again: not provided as the macro’s input

11 Why isn’t this enough?

12 Exotic macros in Scheme Impossible with hygienic macros, right? (loop (begin (when (prime? i) (break i)) (increment! i)))

13 Exotic macros in Scheme Contrast with the explicit version: (loop/explicit break (begin (when (prime? i) (break i)) (increment! i))) provided as input

14 Exotic macros in Scheme Petrofsky extraction: (loop (begin (when (prime? i) (break i)) (increment! i)))

15 Exotic macros in Scheme Dumpster diving: (loop (begin (when (prime? i) (break i)) (increment! i)))

16 Exotic macros in Scheme Dumpster diving: (loop/explicit break (begin (when (prime? i) (break i)) (increment! i)))

17 Exotic macros in Scheme (define-macro (murky a e) (begin (set! a e) (lambda (a) e))) (let ([foo 10]) (murky foo (+ foo 1)))

18 Exotic macros in Scheme (define-macro (indecisive ([a e]) body) (if-zero …complicated computation… ((lambda (a) body) e) ((lambda (a) e) body)))

19 What do we know about hygiene?

20 Hygiene and lexical scope (define-macro (or e1 e2) (with ([t e1]) (if t t e2))) (with ([x 1]) (or #f x)) => (with ([x 1]) (with ([t #f]) (if t t x))) (with ([t 1]) (or #f t)) => (with ([t 1]) (with ([t ′ #f]) (if t ′ t ′ t))) ==

21 Hygienic macro expansion is insensitive to  -conversion. The essence of hygienic macros In other words, hygienic = respects  !

22 What is the scope of with ? (define-macro (with ([a e1]) e2) …) First try: look at results of expansion (with ([x 1]) (or #f x)) => ((lambda (x) ((lambda (t) (if t t x)) #f) 1) (with ([t 1]) (or #f t)) => ((lambda (t) ((lambda (t ′ ) (if t ′ t ′ t)) #f) 1)

23 Using expansion to define scope Goal: define hygienic macro expansion in terms of  -equivalence. Strategy: define  -equivalence in terms of hygienic macro expansion. Oops.

24 What is the scope of with ? (define-macro (with ([a e1]) e2) …) Better idea: provide a binding specification with consumes an argument of shape (with ([a e1]) e2) and produces an expression, where: a is an identifier e1 and e2 are expressions a is bound in e2

25 What is the scope of with ? (define-macro (with ([a e1]) e2) …) Better idea: provide a binding specification with : (with ([ expr ]) expr a ) → expr

26 Annotated with macro (define-macro (with ([a e1]) e2) : expr [(a : binder) (e1 : expr) (e2 : expr a)] …) States macro writer’s intention about scope Admits rigorous  -equivalence definition Provides correctness criterion for hygienic macro expansion

27 A Theory of Hygienic Macros m, a model of hygienic macros 1. Macro binding specifications (types) 2. Theory of  -equivalence 3. High-level expansion semantics 4. Definition and proof of hygiene

28 A Theory of Hygienic Macros m, a model of hygienic macros 1. Macro binding specifications (types) 2. Theory of  -equivalence 3. High-level expansion semantics 4. Definition and proof of hygiene

29 m : a model of hygienic macros (with ([x 1]) x) ( x.x ) 1 S-expressions -calculus

30 m : a model of hygienic macros →  (with ([x 1]) x) →  ((lambda (x) x) 1) →  ( ( x.x ) 1) →  ( x.x ) 1

31 m : a model of hygienic macros let syntax fun = macro (( a ) e ) : (( ) expr a ) => a.e with = macro (([ a e ]) body ) : (([ expr ]) expr a ) => (( a.body) e) in (with ([f (fun (x) 42 )]) ( f y. y ) ) end

32 A Theory of Hygienic Macros m, a model of hygienic macros 1. Macro binding specifications (types) 2. Theory of  -equivalence 3. High-level expansion semantics 4. Definition and proof of hygiene

33 1. Macro binding specifications Shape types based on Culpepper and Felleisen ’03: with : (with ([ id expr ]) expr ) → expr Types for reasoning about scope inspired by nominal datatypes and nominal logic of Gabbay and Pitts ’01

34 1. Type checking let syntax m = macro p :  => … in x. e end [ m :  → expr] ⊢ x. e : expr

35 1. Type checking let syntax m = macro p :  => … in x. e end [ m :  → expr, x : expr] ⊢ e : expr

36 1. Meta-level scope let syntax m = macro ( e1 e2 ) : ( expr expr ) => t. (e1 e2) in … end [ t : expr], [e1 : expr, e2 : expr] ⊢ e : expr

37 1. Two dimensions of binding  : program bindings (object-level)  : macro pattern bindings (meta-level) ,  ⊢ s : 

38 1. Syntactic types Base types:  ::= expr |  → expr Binding types:  ::= | expr a,a… Shape types:  ::=  |  | (  … )

39 A Theory of Hygienic Macros m, a model of hygienic macros 1. Macro binding specifications (types) 2. Theory of  -equivalence 3. High-level expansion semantics 4. Definition and proof of hygiene

40 2. Theory of  -equivalence Adapted to macro applications by exploiting shape type annotations Relies on novel shape-directed  -conversion Based on Gabbay/Pitts “swapping” formulation of  -equivalence

41 2.  -equivalence via swapping let val x = x in x (fn x => x) end =  let val y = x in y (fn w => w) end binding occurrence

42 2.  -equivalence via swapping let val z = x in x (fn x => x) end =  let val z = x in y (fn w => w) end free

43 2.  -equivalence via swapping let val z = x in x (fn x => x) end =  let val z = x in y (fn w => w) end

44 2.  -equivalence via swapping let val z = x in z (fn z => z) end =  let val z = x in z (fn w => w) end

45 2.  -equivalence via swapping let val z = x in z (fn z ′ => z ′ ) end =  let val z = x in z (fn z ′ => z ′ ) end

46 2.  -equivalence via swapping For each binder x, pick fresh name z Rename the binding occurrence of x In the body, swap all occurrences of x with z Recur on subterms Compare results syntactically

47 2.  -equivalence for m (with ([x x]) (x (fun (x) x))) =  (with ([y x]) (y (fun (w) w))) ?

48 2. Shape-directed  -conversion (with ([x x]) (x (fun (x) x))) (with ([ expr ]) expr a ) (with ([y x]) (y (fun (w) w))) binding occurrence

49 2. Shape-directed  -conversion (with ([z x]) (x (fun (x) x))) (with ([ expr ]) expr a ) (with ([z x]) (y (fun (w) w))) not in scope of a ; not converted

50 2. Shape-directed  -conversion (with ([z x]) (x (fun (x) x))) (with ([ expr ]) expr a ) (with ([z x]) (y (fun (w) w))) entering scope of a ; swap all occurrences of x / y with z

51 2. Shape-directed  -conversion (with ([z x]) (z (fun (z) z))) (with ([ expr ]) expr a ) (with ([z x]) (z (fun (w) w)))

52 2. Shape-directed  -conversion (with ([z x]) (z (fun (z) z))) (fun ( ) expr b ) (with ([z x]) (z (fun (w) w)))

53 2. Shape-directed  -conversion (with ([z x]) (z (fun (z ′ ) z ′ ))) (fun ( ) expr b ) (with ([z x]) (z (fun (z ′ ) z ′ )))

54 2.  -equivalence for m (with ([z x]) (z (fun (z ′ ) z ′ ))) =  (with ([z x]) (z (fun (z ′ ) z ′ )))

55 But what is hygienic expansion?

56 A Theory of Hygienic Macros m, a model of hygienic macros 1. Macro binding specifications (types) 2. Theory of  -equivalence 3. High-level expansion semantics 4. Definition and proof of hygiene

57 3. Hygienic macro expansion Simple substitution semantics: let syntax x = m in e end →  e[m/x] if BV(e) ∩ FV(m) =  ( m. s ) →  transcribe(e, match(p, s)) if m = ( macro p :  => e) and BV(s) ∩ FV(e) =  and BV(e) # s

58 3. Expansion up to  x.let syntax m = macro ( e ) : ( expr ) => (e x ) in x.(m x) end

59 3. Expansion up to  x.let syntax m = macro ( e ) : ( expr ) => (e x ) in x ′.(m x ′ ) end →→ x. x ′.( ( macro (e) : ( expr ) => (e x )) x ′ ) →→ x. x ′. ( x ′ x )

60 3. Confluence (with ([x 1]) (or #f x)) (( x.(with ([t #f]) (if t t x)) ) 1 ) (( x.(or #f x) ) 1 ) (with ([x 1]) (with ([t #f]) (if t t x)))    

61 3. Confluence Theorem: Let s be a term such that ,  ⊢ s : . If s →  s 1 and s →  s 2 then there exists an s ′ such that s 1 →  * s ′ and s 2 →  * s ′.   ** ** s s1s1 s2s2 s′s′

62 A Theory of Hygienic Macros m, a model of hygienic macros 1. Macro binding specifications (types) 2. Theory of  -equivalence 3. High-level expansion semantics 4. Definition and proof of hygiene

63 4. Hygiene The expansion relation →  is hygienic if for any well- typed expressions e 0 and e ′ 0 such that e 0 =  e ′ 0 and fully-expanded expressions e and e ′ such that e 0 →  * e and e ′ 0 →  * e ′, then e =  e ′. e0e0 e′0e′0   e e′e′ ** ** Hygienic = respects  !

64 4. Hygiene Remember confluence?   ** ** s up to  -equivalence s1s1 s2s2 s′s′

65 4. Hygiene Theorem: the relation →  is hygienic. e0e0 e′0e′0     e′e′ e

66 Related and Future Work

67 Related work Hygienic macro systems Kohlbecker et al ’86: Hygienic macro expansion Kohlbecker and Wand ’87: Macro-by-example Bawden and Rees ’88: Syntactic closures Clinger and Rees ’91: Macros that work Dybvig et al ’93: Syntactic abstraction in Scheme Krishnamurthi ’01: Linguistic reuse Models of macros Griffin ’88: Notational definitions Bove and Arbilla ’92: Confluent calculus of expansion Gasbichler ’06: Modules with hygienic macros

68 Related work Macro types Culpepper and Felleisen ’03: Well-shaped macros Variables and binding Higher-order abstract syntax, de Bruijn, “locally nameless” Gabbay, Pitts: Nominal logic Staged programming Davies and Pfenning: Modal analysis of staged computation Taha et al: Multi-stage programming, MetaML, MacroML Nanevski: Meta-programming with names and necessity. Kim et al ’06: Polymorphic modal type system for Lisp-like multi-staged languages

69 Future work Core features: Recursive macros Case dispatch Advanced features: Macro-defining macros Conveniences: Variable-length sequences Ellipsis patterns (Kohlbecker and Wand ’87)

70 Future work Joint confluence with evaluation: (with ([x (( y.y ) 1 ) ]) x) (with ([x 1]) x) (( x.x ) (( y.y ) 1 )) (( x.x ) 1 )    

71 Hygienic = respects  ! Thank you.