Docsity

Metaprogramming Lecture Notes, Lecture notes of Programming Languages

Partial preview of the text

Download Metaprogramming Lecture Notes and more Lecture notes Programming Languages in PDF only on Docsity!

Metaprogramming Lecture Notes

Nada Amin (na482)

October 2018

  • 0 Introduction
    • 0.1 Metaprogramming
    • 0.2 Scala, Quickly
    • 0.3 Further Reading
  • 1 Interpretation
    • 1.1 Programs as Data / Data as Programs
      • 1.1.1 Programs as Data
      • 1.1.2 Data as Programs
    • 1.2 Interpreters
    • 1.3 Meta-Circularity
      • 1.3.1 Meta-Interpreters in Lisp
      • 1.3.2 Meta-Interpreters in Prolog
    • 1.4 Further Reading
  • 2 Reflection
    • 2.1 Reification / Reflection
    • 2.2 Reflective Towers of Interpreters
    • 2.3 Metaobject Protocols
    • 2.4 Reflection vs. Abstraction
    • 2.5 Further Reading
  • 3 Compilation
    • 3.1 Partial Evaluation
      • 3.1.1 Futamura projections
    • 3.2 Multi-Stage Programming
    • 3.3 Turning Interpreters into Compilers
    • 3.4 Collapsing Towers of Interpreters
    • 3.5 Further Reading
  • 4 Embedding
    • 4.1 Domain-Specific Languages
    • 4.2 Finally-Tagless
    • 4.3 Examples
      • 4.3.1 Non-determinism 4 CONTENTS
      • 4.3.2 Relational programming
      • 4.3.3 Probabilistic programming
    • 4.4 Further Reading
  • 5 Synthesis
    • 5.1 Further Reading
  • 6 Explore!
    • 6.1 Unconventional Models of Computation
    • 6.2 Relaxing from Symbolic to Neural
    • 6.3 Reversible Computing
    • 6.4 Further Reading

Chapter 0

Introduction

0.1 Metaprogramming

Metaprogramming is writing programs that manipulate programs, as data or processes. For example,

  • an interpreter takes a program as input data and then turns this descrip- tion into a process;
  • a compiler takes a program as input data and then produces an equivalent program as output data – translating the description from one language to another.

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?

  1. Meta-computation is at the heart of computation, theoretically and prac- tically even in the possibility of a universal machine and the realization of a computer.
  2. A useful way of programming is starting with a domain, and building a little language to express a problem and a solution in that domain. Metaprogramming gives the tooling.
  3. Thinking principally about metaprogramming will give you new approaches to programming. For example, you will have a recipe to mechanically turn an interpreter into a compiler. You will acquire new principles, new short- cuts or lenses or tools for thought and implementation if you will.

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.

0.3. FURTHER READING 7

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.

8 CHAPTER 0. INTRODUCTION

10 CHAPTER 1. INTERPRETATION

1.2 Interpreters

1.3 Meta-Circularity

1.3.1 Meta-Interpreters in Lisp

See https://github.com/namin/metaprogramming/tree/master/lectures/ 1-lisp

1.3.2 Meta-Interpreters in Prolog

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

Chapter 2

Reflection

2.1 Reification / Reflection

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.

2.2 Reflective Towers of Interpreters

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

2.3 Metaobject Protocols

Metaobject protocols provide an object-oriented system with a landscape of design options rather than focusing on a single point in the design space.

2.4 Reflection vs. Abstraction

See examples in Java, where reflection breaks basic expectations: https:// github.com/namin/metaprogramming/tree/master/lectures/2-reflection-dangers

Chapter 3

Compilation

3.1 Partial Evaluation

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)

3.1.1 Futamura projections

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

14 CHAPTER 3. COMPILATION

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

16 CHAPTER 3. COMPILATION

Chapter 4

Embedding

4.1 Domain-Specific Languages

4.2 Finally-Tagless

4.3 Examples

4.3.1 Non-determinism

4.3.2 Relational programming

4.3.3 Probabilistic programming

4.4 Further Reading

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.

Chapter 5

Synthesis

5.1 Further Reading

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.

20 CHAPTER 5. SYNTHESIS