tools for meta-programming

22
Tools for Meta-Programming Paul Feautrier [email protected]. ENS Lyon Dagsthul-2003 – 1/22

Upload: elliando-dias

Post on 18-Dec-2014

706 views

Category:

Technology


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Tools for Meta-Programming

Tools for Meta-ProgrammingPaul Feautrier

[email protected].

ENS Lyon

Dagsthul-2003 – 1/22

Page 2: Tools for Meta-Programming

What is Metaprogramming?

Writing programs acting on other programs.

All compilers do metaprogramming.

Generative programming, Skeleton programming, DomainSpecific Optimization, Partial Evaluation, Specialization etc. areinstances of metaprogramming.

Dagsthul-2003 – 2/22

Page 3: Tools for Meta-Programming

Why Metaprogramming?

Compilers and other elements of the program generation chainwere created to lighten the burden of programming.

This idea has had so much success that every user communitywants its own programming language or system.

Building a compiler is difficult and time consuming. Economically,it can only be considered if the user base is very large.

Domain specific languages must be built using simpler tools andshould reuse existing software as much as possible.

Dagsthul-2003 – 3/22

Page 4: Tools for Meta-Programming

Is Metaprogramming Different?

In metaprogramming, the emphasis is on composition ofprograms from large pieces.

Compilers do this too, but with much smaller components.

Part of the job of a compiler (e.g. machine code generation) isirrelevant to metaprogramming. Metaprogramming systemsshould work source-to-source and be as much machineindependent as possibe.

Warning: some optimizations cannot be expressed at the sourcelevel: consider for instance array bound checking in the JVM.

Dagsthul-2003 – 4/22

Page 5: Tools for Meta-Programming

Who is metaprogramming?

Today, a few hundreds of compiler writers.

Tomorrow, a few thousand domain specific experts.

In a few years, every programmer will want to dometaprogramming.

Dagsthul-2003 – 5/22

Page 6: Tools for Meta-Programming

Tools Wanted!

Writing a compiler is a highly specialized task: you just have toweight recent books on the subject to get a feeling!

Compare with Parallel Programming: it was not accepted untiltools for writing parallel programs (e.g. the MPI library) wheredesigned.

Similarly, tools are needed for enabling metaprogramming.

Dagsthul-2003 – 6/22

Page 7: Tools for Meta-Programming

State of the Art

Lexer and Parser Generators

Code Generation Generators

The C preprocessor

Tree Rewriting Systems

C++ Templates

Polytopes Tools

Dagsthul-2003 – 7/22

Page 8: Tools for Meta-Programming

The C preprocessor

Probably the oldest metaprogramming system.

Macros (user defined rewrite rules) with the #definepseudo-instruction.

Program generation via the #include pseudo-instruction.However, no way of checking that the result of the composition islegal.

Conditional compilation via the #if #else #endifconstruction.

The power of the system is limited, because redefining a macro isnot allowed. Hence, recursion is possible but will never succeeds.

Dagsthul-2003 – 8/22

Page 9: Tools for Meta-Programming

Tree Rewriting Systems

Example: Stratego

Uses rewrite rules � � ��� � � � � � � � � � � � � � � � � .

Application of rewrite rules is controlled by a metalanguage.

Problem I: are trees the best representation for programs? Whatabout control flow?

Problem II: program analyses, input and output.

Dagsthul-2003 – 9/22

Page 10: Tools for Meta-Programming

C++ Templates

Templates in C++ were designed to write parametric classes, theparameters being types.

A template class can be instantiated by supplying actual valuesfor type parameters.

The notation for instantiation is similar to function application.

This is typical meta-programming:A template class is a data object which represents a whole family ofprograms.

Instantiating the template is a computation (substitution of names by values),which builds one of the programs in the family.

Template programming is difficult and is usually left to experts,but is much easier than writing a compiler!

Similar mechanisms exist in other languages: e.g. the functors inOcaml.

Dagsthul-2003 – 10/22

Page 11: Tools for Meta-Programming

C++ Templates, II

The template instanciation mechanism is more or less equivalentto

-reduction in

-calculus. Hence, a C++ compiler has Turingpower and templates can be used as a GP language!

But as a metaprogramming device, templates are quite limited.

A function cannot be a parameter of a template; consider ageneric sorting routine in which the comparison function is aparameter.

Dagsthul-2003 – 11/22

Page 12: Tools for Meta-Programming

Z-Polyhedra: How and Why

A Z-polyhedron is the set of integer solutions to a system of linearinequalities.

Z-Polyhedra are a convenient model for loops and arrays.

Used for checking program correctness, finding parallelism,optimizing locality.

Range: so-called regular programs only.

Drawback: they give specifications for program transformations,but not the result of the transformation.

Dagsthul-2003 – 12/22

Page 13: Tools for Meta-Programming

Tools for Polyhedra

In many cases, a Z-polyhedron can be approximated by theenclosing polyhedron. Hence, two types of tools: rational andintegral solvers.

The master problem: deciding if a Z-polyhedron is empty or not.

Omega: rational and integral, handles Pressburger logic.

Pip: rational and integral, handles parametric problems.

the Polylib: rational. Extension for counting the integer points in apolyhedron.

Cloog: Constructing loop nests from polyhedra; based on analgorithm by F. Quilleré.

Dagsthul-2003 – 13/22

Page 14: Tools for Meta-Programming

Some Proposals

Dagsthul-2003 – 14/22

Page 15: Tools for Meta-Programming

Overview

What are the components of a metaprogramming tool?

There must be a representation for programs and programpieces.

There must be methods for building and transforming programs.

Program transformations are not universally applicable. Theremust be methods for analyzing a program, the aim being todecide whether a transformation is applicable or not.

Lastly, there must be a way of driving the metaprogrammingprocess.

Dagsthul-2003 – 15/22

Page 16: Tools for Meta-Programming

A Menagerie of Data Structures

Abstract Syntax Tree

Control Graph

Call Graph

Dependence Graph(s)

Symbol Tables

Polyhedra for iteration domains, dependences, schedules, ...

Each method for building, analysing or transforming programs has apreferred data structure. Examples:

Parsing, dependence analysis, code generation are best done on the AST.

Some forms of dataflow analysis prefer the Control Graph and/or the Call Graph.

Parallelization uses both the AST and the dependence graph.

Dagsthul-2003 – 16/22

Page 17: Tools for Meta-Programming

Coherence

One may either use several data structures, converting back and forthas needed, or try to define a kind of lowest common multiple.

If the program is transformed, all data structures must be keptcoherent.

E.g. if two loops are exchanged, one must adjust the AST, thecontrol graph and the dependence graph.

This is best done in a lazy way, whether the implementationlanguage is lazy or not.

Dagsthul-2003 – 17/22

Page 18: Tools for Meta-Programming

Is There a Universal Representation?

Denotational semantics is an attempt to represent a program as afunction in the mathematical sense.

The thrust of denotational semantics has been about proving byhand properties of programming languages and programs.

However, denotational equations can be represented as formulas(e.g. inside a computer algebra system).

Program building is done by a system of combinators on thesefunctions. E.g. the sequence is represented by functioncomposition.

Program transformations, optimizations, specialization areequational reasoning on the definition of the semantical functions.

One drawback: Denotational Semantics is not well suited forparallel programs.

Dagsthul-2003 – 18/22

Page 19: Tools for Meta-Programming

Program Analysis

The results of program analysis are in the form of assertions, notequations.

For instance, saying that a variable � is of type � asserts that allvalues of � belong to the carrier of �.

There must be a logic for reasoning on these assertions.

However, for efficiency reasons, parts of this logic may beimplemented as special purpose algorithms. Example are:

Type systems, which use efficient algorithms based on unification.

The polytope model, in which linear programming algorithms replacereasoning on linear inequalities.

Abstract interpretation.

The design of such a partially decidable logic is a difficultproblem.

Dagsthul-2003 – 19/22

Page 20: Tools for Meta-Programming

Oracles

Many of these analysis tools behave like oracles: they are given aproblem (e.g. testing a dependence) without reference to theoriginating program.

Most of these oracles are independent stand alone programs,which are written in as many programming languages (C, C++,Java, Ocaml, MuPAD, Mathematica among others).

Talking with oracles is a difficult problem in code coupling.

Since most of these tools have some kind of command language,one solution is to have the metaprogramming systemcommunicate with the oracles by sending programs to them.

Dagsthul-2003 – 20/22

Page 21: Tools for Meta-Programming

The Metalanguage

The metalanguage must be imperative (or else there will be a need foran imperative meta-meta-language, etc.).

Beside ordinary features, it must have special constructors anddestructors/observers for acting on the program representation.

An equational representation is specially adapted to a lazyimplementation.

One important concept: the focus. At any given time, themetaprogramming system is considering one particular part of theprogram, where properties are examined and transformations areapplied.

There must be provisions for moving the focus around according to theresults of previous investigations.

There may be several unrelated focuses (handy for moving informationaround), or a hierarchy of focuses (a procedure, a statement and avariable).

Dagsthul-2003 – 21/22

Page 22: Tools for Meta-Programming

Future Work

Specify a concrete representation for denotational semantics.

Investigate extensions for parallel programs (parallel programs asrelations, operational semantics, powerset, execution orders?).

Specify constructors and observers.

Start playing with the system by hand. Try to reproduce wellknown metaprogramming operations (e.g. partial evaluation).

Design a way of communicating with oracles.

Design the metalanguage.

Dagsthul-2003 – 22/22