Metaprogramming from University to Industry - PowerPoint PPT Presentation

1 / 35
About This Presentation
Title:

Metaprogramming from University to Industry

Description:

Metaprogramming from University to Industry Zolt n Porkol b gsd_at_elte.hu http://gsd.web.elte.hu Dept. of Programming Languages and Compilers, Faculty of Informatics – PowerPoint PPT presentation

Number of Views:47
Avg rating:3.0/5.0
Slides: 36
Provided by: Rend86
Category:

less

Transcript and Presenter's Notes

Title: Metaprogramming from University to Industry


1
Metaprogrammingfrom University to Industry
  • Zoltán Porkoláb
  • gsd_at_elte.hu
  • http//gsd.web.elte.hu
  • Dept. of Programming Languages and Compilers,
  • Faculty of Informatics
  • Eötvös Loránd 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
  • Parameterized types
  • C Template Metaprograms
  • Power of generative metaprograms
  • Sample usages
  • Open questions

3
Parameterized types (Generics)
  • Widely used in modern programming languages
  • ADA generics
  • Eiffel generics
  • C templates
  • Java generics Pizza, GJ, Java 1.5
  • C generics
  • Clean, Generic Haskell, other functional langs.

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
  • //
  • class date / /
  • date d max ( d1, d2)

5
Preprocessor Macro
  • 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 effects"
  • MAX( x, y)2 -gt x gt y ? x y2
  • MAX( x, y) -gt x gt y ? x y

6
Macro the limits
  • void swap( int x, int y)
  • int temp x x y y temp
  • Does not work with macro a macro is typeless.
  • We need a facility to use type parameters.

7
Templates
  • template lttypename Tgt
  • void swap( T x, T y)
  • T temp x x y y temp
  • Ada generics "a form of context-sensitive macro"
  • C templates "a clever kind of macro that obeys
    the scope, naming, and type rules of C"
  • 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.

8
C Function Templates
  • Template is not a single function
  • A schema to instantiate functions on request
  • Parameter deduction
  • Template instantiation
  • Compilaton time
  • template lttypename Tgt T max( T a, T b)
  • if ( a gt b ) return a
  • else return b
  • int i 3, j 6, k
  • double x 3.14, y 4.15, z
  • k max(i,j)
  • z max(x,y)

9
C Function Templates 2.
  • Strong type system rules are applied
  • int i 3 double y 3.14, z
  • z max(i,y) // error
  • template lttypename T, typename Sgt
  • T max( T a, S b)
  • if ( a gt b ) return a
  • else return b
  • z 3.0
  • No deduction on return type
  • No runtime information could be used

10
Explicit specialization
  • 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)

11
User specialization
  • const char s1 Hello, s2 world
  • const char s max(s1,s2)
  • template ltgt
  • const char max(const char a, const char b)
  • if ( strcmp(a,b) lt 0 ) return a
  • else return b

12
Template overloading
  • You can provide overloadd template definitions
  • The compiler selects the most specific template
  • cout ltlt max (4,5)
  • cout ltlt maxltdoublegt(3.14,6)
  • cout ltlt max (this, greater)

R max(S,T)
T max(T, T)
char max(char ,char )
13
C Class Templates 1.
  • Similar way template classes could be defined
  • template ltclass Tgt class matrix
  • public
  • matrix( int i, int j )
  • matrix operator( const matrix other)
  • T at(int i, int j)
  • //
  • private
  • vectorltTgt v
  • Created always with explicit specialisation
  • matrixltintgt m(4,5)

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

15
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

16
Power of C templates
  • 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
  • The C templates are Turing-complete
  • in compilation time

17
The Factorial example
  • The run-time solution
  • int factorial( int n)
  • if ( n 1 ) return 1
  • else return nfactorial(n-1)
  • int main()
  • cout ltlt factorial(15) ltlt endl
  • return 0

18
The Factorial example
  • The metaprogram solution
  • template ltint Ngt struct Factorial
  • enum value N FactorialltN-1gtvalue
  • template ltgt struct Factoriallt1gt
  • enum value 1
  • int main()
  • const int fact15 Factoriallt15gtvalue
  • stdcout ltlt fact15 ltlt endl
  • return 0

19
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

20
(Run-time) Programs vs. Metaprograms
  • Function
  • (runtime) Data
  • Variable
  • Condition
  • Loop
  • Assignment
  • Class
  • Type and constant
  • Symbolic names
  • Type selection
  • Recursion
  • No assignment

21
Data in Template Metaprograms
  • Referential transparency No assignment
  • Still possible to store, modify, and retrieve
    data
  • Typelist
  • struct NullType
  • typedef Typelistlt char, Typelistltsigned char,
  • Typelistltunsigned char, NullTypegt gt gt
    Charlist

char

signed char

unsigned char
NullType
22
Data handling
  • template ltclass TListgt struct Length
  • template ltgt
  • struct LengthltNullTypegt
  • enum value 0
  • template ltclass T, class Ugt
  • struct Length ltTypelistltT,Ugt gt
  • enum value 1 LengthltUgtvalue

23
Motivation
  • int main()
  • const unsigned int di 12
  • const unsigned int oi 014
  • const unsigned int hi 0xc
  • const unsigned int bi0 binary_value("1101")
  • const unsigned int bi1 binarylt1100gtvalue

templateltunsigned long Ngt struct binary //
prepend higher bits to lowest bit static const
int valuebinaryltN/10gtvalue2N10
templateltgt struct binarylt0gt // specialization
static unsigned const value 0
24
Motivation
  • Constant expression array size, case label,
    etc
  • Better code compiled
  • Faster in run-time
  • Syntactically checked the language semantic is
    extended

Design time
Compilation time
Run-time
t
No change in type system Change in run-time values
Template metaprograms apply, automatic config
of the program
Decisions on Stategies, policies
25
Generative Metaprograms
  • 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 (Scheme)
  • Two level languages AspectJ, Template Haskell

26
Areas of Template Metaprogramming
  • Expression templates
  • Blitz, PETE
  • Static interface checking
  • Early catch of syntactical/semantical errors
  • Extending the C type system
  • Introspection
  • Code adaption/optimalization
  • Language embedding

27
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

28
Language embedding
  • SQL
  • Gil, et.al. AraRat
  • XML parsing
  • Jarvi, et.al. type-safe XML library
  • Regular expressions
  • BoostXpressive
  • Compiler embedding
  • boostspirit

29
Language embedding
  • SQL example
  • string s select form tName where 11
  • if ( cond1 )
  • s and fName1 field1
  • if ( cond2 )
  • s and fName2 lt field2
  • if ( cond3 )
  • s and fName3 field3
  • Run-time errors
  • Injection attacks
  • Conversion problems

30
Language embedding
  • SQL example
  • void f()
  • const string s
  • (
  • (tName / (fName1 field1
  • fName2 lt field2)
  • )
  • fName1, fName3
  • ).asSQL()

31
Language embedding
  • BoostXpressive example
  • stdstring hello( "hello world!" )
  • sregex rex1 sregexcompile( "(\\w)(\\w)!" )
  • sregex rex2 (s1 _w) gtgt ' ' gtgt (s2 _w) gtgt
    '!'
  • smatch what
  • if( regex_match( hello, what, rex1 ) )
  • stdcout ltlt what0 ltlt '\n' // whole match
  • stdcout ltlt what1 ltlt '\n' // first capture
  • stdcout ltlt what2 ltlt '\n' // second capture

32
Embedding alternatives
  • If you are (accidently) not a C programmer
  • .NET platform, VB, C
  • LINQ project
  • Java platform
  • Eric Van Wijk et.al. Attribute Grammar-based
    Language Extensions for Java
  • Stratego
  • TU Delft
  • Charles Simonyi Intentional programming

33
Intentional programming
  • Charles Simonyi
  • XEROX Palo Alto Bravo
  • Microsoft Word, Excel
  • Intentional software http//www.intentsoft.com

34
Open questions
  • The real expressive power
  • Standard tools
  • but Loki from Andrei Alexandrescu
  • boostmpl
  • Garantees (number of instantiations, etc)
  • How to design?
  • How to debug?

35
Metaprogrammingfrom University to Industry
  • Zoltán Porkoláb
  • gsd_at_elte.hu
  • http//gsd.web.elte.hu
  • Dept. of Programming Languages and Compilers,
  • Faculty of Informatics
  • Eötvös Loránd University, Budapest

Thank you! Questions?
Write a Comment
User Comments (0)
About PowerShow.com