
























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

























Metaprogramming is writing programs that manipulate programs, as data or processes. For example,
Metaprogramming is an art, even just to keep all the levels straight. This course gives some recipes for a more principled approach to meta-programming, from art to science. Why is metaprogramming relevant?
Beyond traditional meta-programs such as interpreters and compilers, metapro- gramming taken more broadly encompasses programming programming, and manipulating processes that manipulate processes. Reflection comes into play.
def ev ( e : Term , m: Env ) : Val = e match { case v@Var ( x ) => m( v ) case c@Const ( n ) => c case l@Lam( p , b ) => Clo ( l , m) case App( a , b ) => ev ( a , m) match { case Clo (Lam( param , body ) , clo_env ) => val arg = ev ( b , m) ev ( body , clo_env + ( param −> arg ) ) } }
A case class provides a few helpers in the companion object. Here is how the desugaring works.
case c l a s s Foo ( a : I n t ) // p r o d u c e s companion o b j e c t object Foo { def apply ( a : I n t ) : Foo = Foo ( a ) def unapply ( x : Foo ) : Some [ I n t ] = Some ( x. a ) }
For more, see https://github.com/namin/metaprogramming/blob/ master/lectures/0-scala-intro/intro_done.scala
0.3 Further Reading
We will be using the Scala programming language for assignments. There are many books that could be useful but none are required. I recommend Odersky [2014] as well as the book by Odersky et al. [2016]. TAPL [Pierce, 2002], SICP [Abelson and Sussman, 1996], PAIP [Norvig, 1992], Art of Prolog [Sterling and Shapiro, 1994], Building Problem Solvers [For- bus and Kleer, 1993], Test Driven Development by Example [Beck, 2002], Ex- pert F# [Cisternino et al., 2008], Valentino Braitenberg’s Vehicles [Braitenberg, 1986], the Reasoned Schemer [Friedman et al., 2005, 2018] — these are a few of my favorite books.
1.2 Interpreters
1.3 Meta-Circularity
See https://github.com/namin/metaprogramming/tree/master/lectures/ 1-lisp
See https://github.com/namin/metaprogramming/tree/master/lectures/ 1-prolog
1.4 Further Reading
SICP [Abelson and Sussman, 1996], PAIP [Norvig, 1992], Art of Prolog [Sterling and Shapiro, 1994] all have examples of interpreters. Seminal papers on definitional interpreters and meta-circularity are those of McCarthy [1960] and Reynolds [1972]. McCarthy [1981] gives a fun history of lisp. Ferguson [1981] gives a fun introduction to Prolog. Piumarta [2011a] shows how to bootstrap a flexible core interpreter in a simple and elegant serie of stepping stones. Interpretation can lead to abstract interpretation quite naturally [Codish and Sondergaard, 2002, Darais et al., 2017]. Meta-circularity poses punny questions of trust [Thompson, 1984]. Yet CakeML is a proven-correct compiler that can bootstrap itself [Kumar et al., 2014, Kumar, 2016].
Reification turns a program structure into data that can be reasoned or acted upon.
Reflection turns data back into a program structure so that it can affect the computation.
Reflective towers of interpreters have a long history, starting with Brian Cantwell Smith’s LISP-3, then brown, blond and black. Each level (including the user level) has a meta level. So each level can be reasoned about and acted upon.
You can play with the Black reflective tower here: http://io.livecode. ch/learn/readevalprintlove/black
Metaobject protocols provide an object-oriented system with a landscape of design options rather than focusing on a single point in the design space.
See examples in Java, where reflection breaks basic expectations: https:// github.com/namin/metaprogramming/tree/master/lectures/2-reflection-dangers
Partial evaluation optimizes a program by specializing to some known argu- ments. The known arguments are static, while unknown arguments are dynamic. Binding-Time Analysis (BTA) decides whether an expression in the program is static or dynamic by propagation. BTA can be done offline (in advance) or online (while specializing)
Notation from Jones et al. [1990]. Let L be a meta function from a program to the function it computes. Let S and T be programming languages. Equation for partial evaluator mix:
(P ) L p [d 1 , d 2 ] = L (L mix [p, d 1 ]) d 2
Equation for an S-interpreter int written in L:
(I) S pgm data = L int [pgm, data]
Equation for an S-to-T -compiler comp written in L:
(C) S pgm data = T (L comp pgm) data
Futamura projections:
(1) L mix [int, pgm] = target
(2) L mix [mix, int] = compiler
(3) L mix [mix, mix] = compiler generator
The equations are easily verified using the equations for mix (P ), and for interpreters (I) and compilers (C) above.
Verify (1): S pgm data = L int [pgm, data] by (I) L int [pgm, data] = L(L mix [int, pgm]) data by (P )
Therefore, (L mix [int, pgm]) acts as target.
Verify (2): L mix [int, pgm] = target by (1) L mix [int, pgm] = L(L mix [mix, int]) pgm by (P )
Therefore, (L mix [mix, int]) acts as a compiler.
Verify (3): L mix [mix, int] = compiler by (2) L mix [mix, int] = L(L mix [mix, mix]) int by (P )
Therefore, (L mix [mix, mix]) acts as a compiler generator.
3.2 Multi-Stage Programming
Multi-stage programming explicitly separates a program, turned into a program generator, into stages – “now” / static / code generator stage vs “later” / dy- namic / generated code stage. This distinction can be done syntactically, as in MetaOcaml, or driven by types, as in LMS. In contrast to partial evaluation, the binding times are thus explicit and manual. See https://scala-lms.github.io/tutorials/index.html for an introduction to LMS.
3.3 Turning Interpreters into Compilers
An interpreter can be mechanically turned into a (naive) compiler using staging by making the program static and only the input to the program dynamic. For example, a staged regular expression matcher makes the regular expres- sion static and the matched string dynamic, generating code specialized to one regular expression. See https://github.com/namin/metaprogramming/ tree/master/lectures/3-regexp as a starting point in LMS-verify [Amin and Rompf, 2017b].
Examples of embedding logic programming into a functional host include Spivey and Seres [1999], Byrd et al. [2012], Hemann and Friedman [2013]. Embedding a functional interpreter into a relational (purely logical) host results in interesting applications [Byrd et al., 2017]. Probabilities form a monad plus [Ramsey and Pfeffer, 2002, Cisternino et al., 2008]. Hansei [Kiselyov and Shan, 2009] is a language that sports weighted non- determinism. DSLs abound. Some fun examples include Halide [Ragan-Kelley et al., 2013] for image processing, Pan [Elliott, 2003, Elliott et al., 2003] for functional imag- ing well-suited for fractals Jones [2004], and OMeta/Ohm [Warth, 2018] for parsing.
Gulwani et al. [2015] review inductive programming. Sketching is an approach to program synthesis [Solar-Lezama, 2008] based on specification and holes. Osera and Zdancewic [2015] propose a type-and- example directed approach.