On the TuringCompleteness of Generative Metaprograms - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

On the TuringCompleteness of Generative Metaprograms

Description:

I always knew C templates were the work of the Devil, and now ... Standard tools. but: Loki from Andrei Alexandrescu. Garantees. How to design. How to debug ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 29
Provided by: Rend89
Category:

less

Transcript and Presenter's Notes

Title: On the TuringCompleteness of Generative Metaprograms


1
On the Turing-Completenessof Generative
Metaprograms
  • Zoltán Porkoláb, István Zólyomi
  • gsdscamel_at_elte.hu
  • Dept. of Programming Languages and Compilers
  • Eotvos University, Budapest

2
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

3
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

4
Why generics?
  • Conventional techniques are working only for
    complete types.
  • int max( int a, int b)
  • if ( a gt b ) return a else return b
  • double max( double a, double b)
  • if ( a gt b ) return a else return b
  • Etc
  • class date
  • //
  • date d max ( d1, d2)

5
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 gt 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 gt y ? x y2
  • MAX( x, y)
  • x gt y ? x y

6
Generative constructions
  • void swap( int x, int y)
  • int temp x // !! how to detect type of
    x?
  • x y y temp
  • Does not works a macro is - typeless.
  • We need a facility to use type parameters.
  • template lttypename Tgt 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.

7
C Function Templates 1.
  • Template is not a single function
  • Rather a schema to instantiate functions on
    request
  • template lttypename Tgt T max( T a, T b)
  • if ( a gt b ) return a
  • 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

8
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 lttypename T, typename Sgt T max( T a, S
    b)
  • if ( a gt b ) return a
  • else return b
  • z 3
  • No deduction on return type
  • No runtime information could be used

9
C Function Templates 3.
  • Explicit specialization
  • int i 3 double y 3.14, z
  • template lttypename R, typename T, typename Sgt
  • R max( T a, S b)
  • if ( a gt b ) return a
  • else return b
  • z maxltdoublegt(i,y)
  • Template overloading

10
C Function Templates 4.
  • User specialization
  • const char s1 Hello
  • const char s2 world
  • template ltgt
  • const char max( const char a, const char b)
  • if ( strcmp(a,b) lt 0 ) return a
  • else return b
  • The compiler selects the most specific matching
    type
  • cout ltlt max (4,5)
  • cout ltlt maxltdoublegt(3.14,6)
  • cout ltlt max (this, greater)

11
C Class Templates 1.
  • Similar way
  • template ltclass Tgt class matrix
  • public
  • matrix( int i, int j )
  • matrix( const matrix other)
  • matrix()
  • matrix operator( const matrix other)
  • private
  • vectorltTgt v
  • Created always with explicit specialisation
  • matrixltintgt m(4,5)

12
C Class Templates 2.
  • User specialization
  • template ltgt class matrixltboolgt
  • public
  • matrix( int i, int j )
  • matrix( const matrix other)
  • matrix()
  • matrix operator( const matrix other)
  • private
  • a_better_representation v
  • Used the same way
  • matrixltboolgt m(4,5)

13
C Class Templates 3.
  • Partial specialization
  • template ltclass A, class Bgt class C
  • // ...
  • template ltclass Bgt class CltconcreateType,Bgt
  • // ...
  • The most specific matching will be applied
  • Cltint, longgt a
  • CltconcreteType, longgt b

14
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 Dlt2gt
    requested
  • unruh.cpp 30 conversion from enum to Dlt3gt
    requested
  • unruh.cpp 30 conversion from enum to Dlt5gt
    requested
  • unruh.cpp 30 conversion from enum to Dlt7gt
    requested
  • unruh.cpp 30 conversion from enum to Dlt11gt
    requested
  • unruh.cpp 30 conversion from enum to Dlt13gt
    requested
  • unruh.cpp 30 conversion from enum to Dlt17gt
    requested
  • unruh.cpp 30 conversion from enum to Dlt19gt
    requested

15
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 ltgt
    conditional and looping
    constructions

16
The Factorial example
  • int factorial( int n)
  • return (n0) ? 1 nfactorial(n-1)
  • int main()
  • cout ltlt factorial(15) ltlt endl
  • return 0
  • template ltint Ngt struct Factorial
  • enum value N FactorialltN-1gtvalue
  • template ltgt struct Factoriallt1gt
  • enum value 1
  • int main()
  • const int fact5 Factoriallt15gtvalue
  • stdcout ltlt fact5 ltlt endl
  • return 0

17
Conditional statement
  • template ltbool condition, class Then, class Elsegt
  • struct IF
  • typedef Then RET
  • template ltclass Then, class Elsegt
  • struct IFltfalse, Then, Elsegt
  • typedef Else RET
  • template lttypename T, typename Sgt
  • IFlt sizeof(T)ltsizeof(S), S, TgtRET max(T t, S s)
  • if (t gt s) return t
  • else return s

18
Higher order metaprograms
  • accumulate(n,f) f(0) f(1) ... f(n)
  • template ltint n, templateltintgt class Fgt struct
    Accumulate
  • enum RET Accumulateltn-1,FgtRET
    FltngtRET
  • template lttemplateltintgt class Fgt struct
    Accumulatelt0,Fgt
  • enum RET Flt0gtRET
  • template ltint ngt struct Square
  • enum RET nn
  • cout ltlt Accumulatelt3,SquaregtRET ltlt endl

19
Programs vs. Metaprograms
  • Function
  • (runtime) Data
  • Variable
  • Assignment
  • Condition
  • Loop
  • Class
  • Type and constant
  • Symbolic names
  • Const initialization
  • enumerated values
  • Recursion

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

21
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
  • Java
  • provides lower level reflection
  • C RTTI
  • much lower level (typeid)

22
Main areas
  • Expression templates
  • Static interface checking
  • Concept cheking
  • Extend existing type system
  • ???

23
Expression templates
  • Improve efficiency of programs
  • Save space and / or time
  • Keep the code correctly organized
  • Supercomputing, numerical computing

24
Expression templates
Array a, b, c, d, e // Object-oriented way of
a b c d e double _t1 new doubleN
for ( int i0 iltN i) _t1i bi ci
double _t2 new doubleN for ( int i0
iltN i) _t2i _t1i di double _t3
new doubleNM for ( int i0 iltN i) _t3i
_t2i ei for ( int i0 iltN i) ai
_t3i delete _t3 delete _t2 delete
_t1 // Fortran like solution for ( int
i0 iltN i) ai bi ci di ei

25
Concept checking
  • interface Cloneable
  • T TClone() const
  • template lttypename Tgt class C
  • // T must provide T TClone() const ...
  • template lttypename Tgt class C
  • public
  • void SomeFunc( const T t) t-gtClone()

26
Extendig type system
  • Family polymorphism
  • Eric Ernst
  • Generic-Beta
  • Structural subtyping

27
Structural subtyping
  • Example from Harold Osher

OpEval
OpCheck
OpDisplay
Operator
PlusCheck
PlusEval
PlusDisplay
Plus
28
Open questions
  • The real expressive power
  • Standard tools
  • but Loki from Andrei Alexandrescu
  • Garantees
  • How to design
  • How to debug
Write a Comment
User Comments (0)
About PowerShow.com