5th MaCS Debrecen 2004 1 On the Turing-Completeness of Generative Metaprograms Zoltán Porkoláb, István Zólyomi Dept. of Programming.

Slides:



Advertisements
Similar presentations
1 An anomaly of subtype relations at component refinement and a generative solution in C++ Zoltán Porkoláb, István Zólyomi {gsd | Eötvös.
Advertisements

Java for C++ Programmers Second Night. Overview First Night –Basics –Classes and Objects Second Night –Enumerations –Exceptions –Input/Output –Templates.
The C ++ Language BY Shery khan. The C++ Language Bjarne Stroupstrup, the language’s creator C++ was designed to provide Simula’s facilities for program.
Template. 2 Using templates, it is possible to create generic functions and classes. In a generic function or class, the type of data upon which the function.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Chapter 17 Templates. Generic Algorithms Algorithms in which the actions or steps are defined, but the data types of the items being manipulated are not.
5th MaCS Debrecen On the Turing-Completeness of Generative Metaprograms Zoltán Porkoláb, István Zólyomi Dept. of Programming.
Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Encapsulation by Subprograms and Type Definitions
How is Java different from other languages Less than you think: –Java is an imperative language (like C++, Ada, C, Pascal) –Java is interpreted (like LISP,
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Maya: Multiple-Dispatch Syntax Extension in Java Jason Baker and Wilson C. Hsieh University of Utah.
1Genericity  C++ Templates. 2 Parametric Polymorphism Allows definitions to be parametrized at compile-time Such a definition is actually a “function”
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Programming Language Concepts
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
Programming Languages and Design Lecture 7 Subroutines and Control Abstraction Instructor: Li Ma Department of Computer Science Texas Southern University,
Runtime Environments Compiler Construction Chapter 7.
Bindings and scope  Bindings and environments  Scope and block structure  Declarations Programming Languages 3 © 2012 David A Watt, University.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Compiler Support for Profiling C++ Template Metaprograms József Mihalicza, Norbert Pataki, Zoltán Porkoláb Eötvös Loránd University Faculty of Informatics.
C++ Template Metaprogramming Why, When and How? Zoltán Porkoláb Dept. of Programming Languages and Compilers, Faculty of Informatics Eötvös.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Templates.
Object Oriented Programming Elhanan Borenstein copyrights © Elhanan Borenstein.
CNS  Executive Overview  Template Parameters  Function Template Issues 2 CNS Templates.
Metaprogramming from University to Industry Zoltán Porkoláb Dept. of Programming Languages and Compilers, Faculty of.
Programming Language Support for Generic Libraries Jeremy Siek and Walid Taha Abstract The generic programming methodology is revolutionizing the way we.
1 Scope Scope describes the region where an identifier is known, and semantic rules for this.
GPCE'04, Vancouver 1 Towards a General Template Introspection Library in C++ István Zólyomi, Zoltán Porkoláb Department of Programming Languages and Compilers.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Meta IFL 2007 Freiburg1 Meta - Towards a Functional-Style Interface for C++ Template Metaprograms * Ádám Sipos, Zoltán Porkoláb, Norbert Pataki, Viktória.
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Xin Yuan.
Object-Oriented Programming in C++
Object Oriented Programming Elhanan Borenstein Lecture #10 copyrights © Elhanan Borenstein.
Types in programming languages1 What are types, and why do we need them?
Templates L. Grewe. 2 Goals Often want to do basically the same thing w/diff things –functions work on variables only types specified –  algorithmic.
3C-1 Purity Typing Language semantics Inheritance model  Single vs. Multiple inheritance  Common root Modular mechanisms Generics Object Oriented Languages.
Functions Sujana Jyothi C++ Workshop Day 2. Functions 3 Parameter transmission modes pass by value (default) pass by reference (&) pass by const reference.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Templates.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 4 is due Nov. 20 (next Friday). After today you should know everything you need for assignment.
CS 152: Programming Language Paradigms March 19 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
CSCE 314 Programming Languages Reflection Dr. Hyunyoung Lee 1.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
CS212: Object Oriented Analysis and Design Lecture 22: Generic Class Design.
1 CSE Programming in C++. 2 Overview Sign roster list Syllabus and Course Policies Introduction to C++ About Lab 1 Fill Questionnaire.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
LECTURE LECTURE 17 Templates 19 An abstract recipe for producing concrete code.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 4 is due Nov. 20 (this Friday). Today: –Templates. –STL –Smart Pointers.
Copyright © 2012 Pearson Education, Inc. Chapter 10 Advanced Topics.
Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 1 Concordia University Department of Computer Science and Software Engineering SOEN6441 –
CS 342: C++ Overloading Copyright © 2004 Dept. of Computer Science and Engineering, Washington University Overview of C++ Overloading Overloading occurs.
Chapter 1: Preliminaries Lecture # 2. Chapter 1: Preliminaries Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
Template Metaprogramming in C++ Giuseppe Attardi and Haoyuan Li Dipartimento di Informatica Università di Pisa.
C++ REVIEW – TEMPLATES. GENERIC PROGRAMMING Programming/developing algorithms with the abstraction of types Algorithms/data is expressed “without type”
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Semantic Analysis Chapter 6.
CMSC 202 Lesson 22 Templates I.
Semantic Analysis Chapter 6.
Templates I CMSC 202.
Presentation transcript:

5th MaCS Debrecen On the Turing-Completeness of Generative Metaprograms Zoltán Porkoláb, István Zólyomi Dept. of Programming Languages and Compilers Eotvos University, Budapest

25th MaCSDebrecen 2004 Agenda I always knew C++ templates were the work of the Devil, and now I'm sure... - Cliff Click cited by Todd Veldhuisen Generative program constructions Metaprograms Power of generative metaprograms Sample usages Open questions

35th MaCSDebrecen 2004 Generative constructions Widely used in modern programming languages: Smalltalk, Lisp, other functional langs. Eiffel ADA generics C++ templates Java generics: Pizza, GJ, Java 1.5 C#: soon

45th MaCSDebrecen 2004 Why generics? Conventional techniques are working only for complete types. int max( int a, int b) { if ( a > b ) return a; else return b; if ( a > b ) return a; else return b;} double max( double a, double b) { if ( a > b ) return a; else return b; if ( a > b ) return a; else return b;}Etc… class date { //… //… }; date d = max ( d1, d2);

55th MaCSDebrecen 2004 Preprocessor Macro? Ada generics : "a restricted form of context-sensitive macro facility" C++ templates: "a clever kind of macro that obeys the scope, naming, and type rules of C++" #define MAX(a,b) a > b ? a : b Works, because a macro is - typeless. Processed not by the compiler, therefore there are a number of "secondary effect": MAX( x, y)*2 x > y ? x : y*2 x > y ? x : y*2 MAX( ++x, y) ++x > y ? ++x : y

65th MaCSDebrecen 2004 Generative constructions void swap( int& x, int& y) void swap( int& x, int& y) { int temp = x; // !! how to detect type of x? int temp = x; // !! how to detect type of x? x = y; y = temp; x = y; y = temp; } Does not works: a macro is - typeless. We need a facility to use type parameters. template void swap( T& x, T& y) { T temp = x; x = y; y = temp; } Template depends only on the properties that is actually used Does not require different types used as arguments to be explicitly related. In particular, the argument types used as a template need not be from a single inheritance hierarchy.

75th MaCSDebrecen 2004 C++ Function Templates 1. Template is not a single function Rather a schema to instantiate functions on request template T max( T a, T b) { if ( a > b ) return a; if ( a > b ) return a; else return b; else return b;} int i = 3, j = 6, k; k = max(i,j); double x = 3.14, y = 4.15, z; z = max(x,y); Parameter deduction Template instantiation

85th MaCSDebrecen 2004 C++ Function Templates 2. Instantiation in compile-time Then strong type system rules are applied int i = 3; double y = 3.14, z; z = max(i,y); template T max( T a, S b) { if ( a > b ) return a; if ( a > b ) return a; else return b; else return b;} z == 3; No deduction on return type No runtime information could be used

95th MaCSDebrecen 2004 C++ Function Templates 3. Explicit specialization int i = 3; double y = 3.14, z; template template R max( T a, S b) { if ( a > b ) return a; if ( a > b ) return a; else return b; else return b;} z = max (i,y); Template overloading

105th MaCSDebrecen 2004 C++ Function Templates 4. User specialization const char *s1 = “Hello”; const char *s2 = “world”; template <> const char * max( const char *a, const char *b) { if ( strcmp(a,b) < 0 ) return a; if ( strcmp(a,b) < 0 ) return a; else return b; else return b;} The compiler selects the most specific matching type cout << max (4,5); cout (3.14,’6’); cout << max (“this”, “greater”);

115th MaCSDebrecen 2004 C++ Class Templates 1. Similar way template class matrix { public: matrix( int i, int j ); matrix( int i, int j ); matrix( const matrix &other) matrix( const matrix &other) ~matrix(); ~matrix(); matrix& operator=( const matrix &other); matrix& operator=( const matrix &other);private: vector v; vector v;}; Created always with explicit specialisation matrix m(4,5);

125th MaCSDebrecen 2004 C++ Class Templates 2. User specialization template <> class matrix template <> class matrix { public: matrix( int i, int j ); matrix( int i, int j ); matrix( const matrix &other) matrix( const matrix &other) ~matrix(); ~matrix(); matrix& operator=( const matrix &other); matrix& operator=( const matrix &other);private: a_better_representation v; a_better_representation v;}; Used the same way matrix m(4,5);

135th MaCSDebrecen 2004 C++ Class Templates 3. Partial specialization template class C { //... //... }; template class C template class C { //... //... }; The most specific matching will be applied C a; C b;

145th MaCSDebrecen 2004 C++ Templates The C++ templates were first implemented in the early ’90s Accepted as part of the ANSI/ISO in 1994 Erwin Unruh: 1994 unruh.cpp 30: conversion from enum to D requested

155th MaCSDebrecen 2004 Power of C++ templates The C++ templates are Turing-complete The Compiler “executes” template metaprograms The result is a non-templated program executed in “run-time” In 1966 Böhm and Jacopini proved: Turing machine implementation conditional and looping constructions Turing machine implementation conditional and looping constructions

165th MaCSDebrecen 2004 The Factorial example int factorial( int n) { return (n==0) ? 1 : n*factorial(n-1); return (n==0) ? 1 : n*factorial(n-1);} int main() { cout << factorial(15) << endl; cout << factorial(15) << endl; return 0; return 0;} template struct Factorial { enum { value = N * Factorial ::value }; enum { value = N * Factorial ::value };}; template <> struct Factorial { enum { value = 1 }; enum { value = 1 };}; int main() { const int fact5 = Factorial ::value; const int fact5 = Factorial ::value; std::cout << fact5 << endl; std::cout << fact5 << endl; return 0; return 0;}

175th MaCSDebrecen 2004 Conditional statement template template struct IF { typedef Then RET; typedef Then RET;}; template template struct IF struct IF { typedef Else RET; typedef Else RET;}; template template IF ::RET max(T t, S s) { if (t > s) return t; if (t > s) return t; else return s; else return s;}

185th MaCSDebrecen 2004 Higher order metaprograms accumulate(n,f) := f(0) + f(1) f(n) template class F> struct Accumulate { enum { RET = Accumulate ::RET + F ::RET }; enum { RET = Accumulate ::RET + F ::RET };}; template class F> struct Accumulate template class F> struct Accumulate { enum { RET = F ::RET }; enum { RET = F ::RET };}; template struct Square { enum { RET = n*n }; enum { RET = n*n };}; cout ::RET ::RET << endl;

195th MaCSDebrecen 2004 Programs vs. Metaprograms Function (runtime) Data VariableAssignmentConditionLoopClass Type and constant Symbolic names Const initialization enumerated values enumerated valuesRecursion

205th MaCSDebrecen 2004 Generative Metaprograms 1. metaprogramming: writing programs that represent and manipulate other programs or themselves (ie:reflection). Metaprograms are programs about programs. introspection: the ability of a program to observe its own state the ability of a program to observe its own state intercession: the ability to modify its own state the ability to modify its own state Open compilers: transformations on AST Hygenic macros Two level languages: Aspect J

215th MaCSDebrecen 2004 Generative Metaprograms 2. Dynamic languages (Smalltalk, Lisp, etc.) high level reflection Smalltalk: classes are objects of metaobjects methods, execution stack, etc. are represented by objects high level reflection Smalltalk: classes are objects of metaobjects methods, execution stack, etc. are represented by objectsJava provides lower level reflection C++ RTTI much lower level: (typeid)

225th MaCSDebrecen 2004 Main areas Expression templates Static interface checking Concept cheking Extend existing type system ???

235th MaCSDebrecen 2004 Expression templates Improve efficiency of programs Save space and / or time Keep the code correctly organized Supercomputing, numerical computing

245th MaCSDebrecen 2004 Expression templates Array a, b, c, d, e; // Object-oriented way of a = b + c + d + e double* _t1 = new double[N]; for ( int i=0; i<N; ++i) _t1[i] = b[i] + c[i]; double* _t2 = new double[N]; for ( int i=0; i<N; ++i) _t2[i] = _t1[i] + d[i]; double* _t3 = new double[N*M]; for ( int i=0; i<N; ++i) _t3[i] = _t2[i] + e[i]; for ( int i=0; i<N; ++i) a[i] = _t3[i]; delete [] _t3; delete [] _t2; delete [] _t1; // Fortran like solution: for ( int i=0; i<N; ++i) a[i] = b[i] + c[i] + d[i] + e[i];

255th MaCSDebrecen 2004 Static interface cheking Array a, b, c, d, e; // Object-oriented way of a = b + c + d + e double* _t1 = new double[N]; for ( int i=0; i<N; ++i) _t1[i] = b[i] + c[i]; double* _t2 = new double[N]; for ( int i=0; i<N; ++i) _t2[i] = _t1[i] + d[i]; double* _t3 = new double[N*M]; for ( int i=0; i<N; ++i) _t3[i] = _t2[i] + e[i]; for ( int i=0; i<N; ++i) a[i] = _t3[i]; delete [] _t3; delete [] _t2; delete [] _t1; // Fortran like solution: for ( int i=0; i<N; ++i) a[i] = b[i] + c[i] + d[i] + e[i];