1 / 19

Meta<Fun> - Towards a Functional-Style Interface for C++ Template Metaprograms *

Meta<Fun> - Towards a Functional-Style Interface for C++ Template Metaprograms *. Ádám Sipos, Zoltán Porkoláb , Norbert Pataki, Viktória Zsók {shp|gsd|patakino|zsv}@elte.hu Department of Programming Languages and Compilers Faculty of Informatics , Eötvös Loránd University , Budapest

march
Télécharger la présentation

Meta<Fun> - Towards a Functional-Style Interface for C++ Template Metaprograms *

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Meta<Fun> - Towards a Functional-Style Interface for C++ Template Metaprograms* Ádám Sipos,Zoltán Porkoláb, Norbert Pataki, Viktória Zsók {shp|gsd|patakino|zsv}@elte.hu Department of Programming Languages and Compilers Faculty of Informatics, Eötvös Loránd University, Budapest * Supported by GVOP-3.2.2.-2004-07-0005/3.0 and SA 66ÖU2 IFL 2007 Freiburg

  2. Contents • Template Metaprogramming • Metaprogramming & Functional Programming • A Functional TMP interface • Example: Lazy data types • Evaluation IFL 2007 Freiburg

  3. C++ Template Metaprogramming I • Template: • list<int>, list<double> • Tool for generic programming • Unconstrained (in current standard) • Concepts(in C0x) • Implementation • Instantiation (in compile-time) • Specialization • Pattern-matching • Compile-time algorithms • Recursions, conditional statements => Turing-completeness IFL 2007 Freiburg

  4. C++ TMP: an example template <int N> struct Factorial { enum { value = N*Factorial<N-1>::value }; }; template <> struct Factorial<1> { enum { value = 1 }; }; int main() { int r = Factorial<5>::value; } IFL 2007 Freiburg

  5. Application areas of TMP • Concept checking • Expression templates • Compile-time code adaptation • Compiler construction • Active libraries • Language embedding • etc IFL 2007 Freiburg

  6. TMP is Functional • Referential transparency • no mutable variables • no loops • Pattern matching • Higher order TMPs • Lazyness? IFL 2007 Freiburg

  7. Motivation • Terrible syntax • Terrible diagnostics • Metaprogramming in itself is difficult and ugly • Most metaprograms are handcrafted • Non-functional style libraries (like boost::mpl) • Hard to debug • Hard to predict the development process • Aim: C++ template metaprogramming interface which explicitly expresses the programmer’s intentions IFL 2007 Freiburg

  8. Meta<Fun> • Aim: C++ template metaprogramming interface which explicitly expresses the programmer’s intentions • Embedded functional-style interface for C++ Template Metaprograms • Embedded language example: Clean • Pure functional language based on graph rewriting with outermost tree reduction • Lazy evaluation IFL 2007 Freiburg

  9. Structure of Meta<Fun> Translated user code C++ TMP Compiled C++ Translator Clean-like Standard C++ Compiler C++ Clean-like Engine in C++ TMP C++ IFL 2007 Freiburg

  10. Components of Meta<Fun> • Clean-TMP translator • Work in progress • Clean-like metaprogram library • Separating the user-written code from the graph-rewriting engine • Equivalent semantics to Clean programs • Library based on standard C++ • Portable IFL 2007 Freiburg

  11. An example: Sieve take 0 xs = [] take n [x:xs] = [x:take (n-1) xs] sieve [prime:rest] = [prime : sieve (filter prime rest)] filter p [h:tl] | h rem p == 0 = filter p tl = [h : filter p tl] filter p [] = [] Start = take 10 (sieve [2..]) -> take 10 ([2, sieve (filter 2 [3..])]) -> [2, take 9 (sieve (filter 2 [3..]))] -> [2, take 9 (sieve [3, filter 2 [4..])] -> [2, take 9 [3, sieve (filter 3 (filter 2 [4..]))] -> [2, 3, take 8 (sieve (filter 3 (filter 2 [4..]))) -> ………… IFL 2007 Freiburg

  12. Sieve: translated code template <int p, int x, class xs> struct filter<p, Cons<x,xs> > { typedef typename if_c<x%p==0,Filter<p,xs>,Cons<x,filter<p,xs> > >::type right; }; template <class xs> struct sieve { typedef NoMatch right; }; template <int p, class xs> struct sieve<Cons<p,xs> > { typedef Cons<p,sieve<filter<p,xs> > > right; }; IFL 2007 Freiburg

  13. Lazy list and Sieve template <int r> struct EnumFrom { typedef Cons<r, EnumFrom<r+1> > right; }; take<10,sieve<EnumFrom<2> > take<10,Cons<2,sieve<filter<2,EnumFrom<3> > > > Cons<2,take<9,sieve<filter<2,EnumFrom<3> > > > > Cons<2,take<9,sieve<3,filter<2,EnumFrom<4> > > > > Cons<2,take<9,Cons<3,sieve<filter<3,EnumFrom<4> > > > > > Cons<2,3,take<8,filter<3,filter<2,EnumFrom<4> > > > > ………… IFL 2007 Freiburg

  14. The engine template <class T1, template <class> class Expr> struct Eval<Expr<T1> > { typedef typename if_c<is_same<typename Expr<T1>::right,NoMatch>::value, typename if_c<!Eval<T1>::second, Expr<T1>, Expr<typename Eval<T1>::result> >::type, typename Expr<T1>::right >::type result; static const bool second = !(is_same<typename Expr<T1>::right,NoMatch>::value && !Eval<T1>::second); }; IFL 2007 Freiburg

  15. Compilation times IFL 2007 Freiburg

  16. Related/Future work • C++ Template Metaprogram Libraries • Loki (Alexandrescu 2000) • boost::mpl (Gurtovoy, Abrahams, 2002) • Incl: TMP Lambda • Runtime functional library • fc++ (McNamara, Smaragdakis, 2000) • Func – C++ template mapping • From Haskell type classes to C++ concepts (Zalewski, Priesnitz, Ionescu, Botta, Schupp, 2007) • Closer to Clean syntax • Order of rules (priorities) • Optimizations IFL 2007 Freiburg

  17. Conclusion • C++ Template Metaprogramming is emerging • Functional-style programming • Current syntax is unsuitable and source of errors • Functional syntax would better express the programmers’ intensions • Meta<Fun> - a portable way to achieve the goals • Lazy data types – a proof of concepts • Many open questions IFL 2007 Freiburg

  18. Functional interface BEGIN_CLEAN(sievesum10) take 0 xs = [] take n [x:xs] = [x:take (n-1) xs] sieve [prime:rest] = [prime : sieve (filter prime rest)] filter p [h:tl] | h rem p == 0 = filter p tl = [h : filter p tl] filter p [] = [] Start = sum ( take 10 (sieve [2..]) ) END_CLEAN static const int prime10 = cleanstart(sievesum10); IFL 2007 Freiburg

  19. Thank you! Questions? Meta<Fun> - Towards a Functional-Style Interface for C++ Template Metaprograms* Ádám Sipos,Zoltán Porkoláb, Norbert Pataki, Viktória Zsók {shp|gsd|patakino|zsv}@elte.hu Department of Programming Languages and Compilers Faculty of Informatics, Eötvös Loránd University, Budapest IFL 2007 Freiburg

More Related