Concepts of Programming Languages – Copl'15

Concepts of Programming Languages – Copl'15

Sebastian Hungerecker Malte Schmitz (Eds.) • Concepts of Programming Languages – CoPL’15 CS 3702 Bachelor Seminar Informatics, CS 3703 Bachelor Seminar Medical Informatics, CS 5480 Seminar Software Systems Engineering, CS 5840 Seminar in English Lübeck, Germany, winter term 2015 Proceedings Institute for Software Engineering and Programming Language University of Lübeck 2 CONCEPTS OF PROGRAMMING LANGUAGES – COPL’15 Preface In this seminar we explored modern concepts of programming ject evaluation strategies was described with these four key- language design and implementation. This encompasses se- words: mantic aspects such as paradigms or type systems as well as implementation aspects such as parsing, intermediate represen- – lazy, by need (Haskell, . ) tations or optimization. – strict (most known) – by name (Scala, . ) The aim of this seminar was to give an overview on a selection – usage and implementation of topics within the field of programming languages. This sem- inar does not consist of tutorials for different programming lan- This open characterizations lead to much more research effort, guages, but tries to introduce at least some of the main concepts but the final contributions give a wider overview on the selected used in programming languages. The participants could choose topic and some of the results are quite impressive. The 13 a topic that appeared most interesting to them. As a result, there contributions are altogether way above average students’ home- is no clear focus on one specific aspect, but the seminar covers work. a wide range of exciting topics. February 2016 Sebastian Hungerecker Instead of pre-selected literature the students received at first Malte Schmitz only a few keywords defining their topic. For example the sub- CONTENTS 3 Contents Alexander Schramm Gilian Henke Metaprogramming .................. 4 Logic programming ................. 44 Merlin Laue Larissa von Witte Basics of Garbage Collection ............ 11 Parser ......................... 49 Malte Skambath Alexander Harms Intermediate Represantations ............ 16 Array programming ................. 54 Toni Schumacher Thiemo Bucciarelli Static vs. Dynamic Typing .............. 22 Just in time compilation ............... 59 Gunnar Bergmann Jan Pascal Maas Memory models ................... 27 Synchronous vs. asynchronous programming ... 65 Timo Luerweg Jan Niklas Rösch Stack based programming ............. 33 Aspects of object orientation ............ 72 Moritz Flucht Evaluation Strategies ................ 38 4 CONCEPTS OF PROGRAMMING LANGUAGES – COPL’15 Metaprogramming Alexander Schramm Abstract—The automatic generation of programs has been a are compile-time and runtime metaprogramming, which are research topic for many years in different Contexts. Metapro- supported in different languages and allow different concepts gramming is one way to write code that generates or manip- of code generation. General use cases of all kinds of metapro- ulates code. This paper will provide an overview of different metaprogramming concepts and their implementation in differ- gramming are to support growth of a programming language ent programming languages. (add new syntax without the need to update the compiler/run- For this purpose this paper will show examples of Reflections time), support of external Domain Specific Languages (DSLs) in Java, Lisps S-Expression and Macros, Template Haskell, C++ (e.g. SQL or JSON Fragments) and to write wrapper libraries Templates and Runtime Metaprogramming in Ruby. around other systems with changing interfaces. Furthermore metaprogramming concepts can be used to write more concise I. INTRODUCTION code and adapt the style of the language to a specific problem Automatic generation of programs and code has been a long domain [3], [4]. term research field, e.g. to generate test cases, generate soft- Another differentiation in metaprogramming is the domain- ware from specifications (UML to Java Classes) or compiler or metalanguage and the host- or object-language [1], which generation. There are different definitions of metaprogram- can be different languages or the same. The domain language ming, based on the language it is implemented in and what is the programming language, in which the metaprogram should be examined, but for this paper we will use to the is written, while the generated code is output in the host following one based on [1]: Metaprogramming is a term de- language. In this paper we will only look at metaprogramming scribing different ways to write code that generates new code systems, where the domain language is a part of the host or manipulates existing one. Metaprogramming can be used language, therefore metaprograms can be embedded in normal for language virtualization, type providers, materialization of programs of the language. An example, where the domain type class instances, type-level programming, and embedding language differs from the host, is Yet Another Compiler of external DSLs [2]. While metaprogramming is often seen Compiler (YACC)[5], which has a special kind of a Backus- as an academic approach to programming and is considered Nauer-Form (BNF) as the domain language and which outputs bad style in many languages and communities, there are also a C++ program [1]. big projects leaveraging its powers and showing how it can be put to great use. Examples we will glance at in this paper are A. Compile-Time metaprogramming the JUnit test framework from Java and the JBuilder project Metaprogramming, which is happening at compilation time, in Ruby In this Paper multiple implementations and concepts is supported in many languages like Scala ([2]), C++ ([6], of metaprogramming in different languages will be shown. [1], [7]), Haskell ([8]) or maybe most idiomatic in nearly Outline: In Chapter II there will be an overview of all Lisp dialects in the form of Macros. Most known forms metaprogramming concepts in theory and an explanation of of it are macros and templates, which offer different kind of the concept itself. In Chapter III we will look at a first, mechanisms to generate code based on defined patterns which small example of metaprogramming called Reflection in the are used by normal code. This patterns can be used to generate Java Programming Language. Chapter IV shows a technique multiple, similar methods without writing them explicitly or of compile time metaprogramming in the C++ language to transform parts of the program. using Templates and Chapter V will show compile time Compile time metaprogramming happens before the pro- metaprogramming in a purely functional and type safe man- gram runs and therefore the generation of the code doesn’t has ner. Afterwards we will look at runtime metaprogramming an impact on runtime performance of the program. This can in Ruby in Chapter VI and its rich runtime object model. be used to solve computationally intensive problems during With this knowledge we will look at the richest compile-time compilation – so only once: when the program is build – and metaprogramming model: Makros in Lisp in Chapter VII. In not every time the program is run and therefore to boost the Chapter VIII we will give an overview of further interesting performance of a program. implementations of metaprogramming in other languages and Compile time metaprogramming gives the user of a pro- talk about when metaprogramming should or shouldn’t be gramming language a mechanism to interact with the Compiler used. to allow the construction of arbitrary program fragments from user code. [4] This interface to the compiler can be used to II. WHAT IS METAPROGRAMMING modify the Abstract Syntax Tree (AST) of the compiled code, The term metaprogramming is used for different ways to and rearrange, manipulate, delete or generate new nodes in the generate and manipulate Code. Two big, different approaches tree. SCHRAMM: METAPROGRAMMING 5 B. Runtime Metaprogramming public void parse(Class<?> clazz) { Method[] methods = clazz.getMethods(); Runtime metaprogramming is all about modifying a pro- for (Method m : methods) { gram while it is running. This can lead to a very dynamic if (m.isAnnotationPresent(Test.class)) { nature of programs written using these techniques: undefined m.invoke(null); methods can be generated on the fly, so every user input } } could be translated into a new method or a program can } adapt to changing interfaces of third party services without the need to recompile it. To enable such behavior, the host Listing 2: A class parser to find and call methods in a class language has to provide a rich runtime object model. In many annotated with a @Test annotation classic programming languages, most meaning and structure (read classes, inheritance, methods) of code gets lost, when An important distinction is that the class object of a it is compiled and only sequential instructions to execute are class is not the same thing as the declared class, like it is left. Languages which support Runtime metaprogramming like the case in Ruby, shown in Chapter VI, but merely a proxy Ruby or Java have to keep some information of the code, e.g. objects which is used to collect metadata about that class. the class hierarchy or where a method is defined, to present this Therefore it can’t be used to manipulate the underlying class information towards the programmer at runtime. Generating by adding new variables or methods. This behavior doesn’t and using an object model produces overhead and slows satisfy our definition of metaprogramming, nonetheless it’s a performance at runtime, but is necessary

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    77 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us