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.

Slides:



Advertisements
Similar presentations
Sml2java a source to source translator Justin Koser, Haakon Larsen, Jeffrey Vaughan PLI 2003 DP-COOL.
Advertisements

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.
CPSC 388 – Compiler Design and Construction
Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004.
Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
8. Introduction to Denotational Semantics. © O. Nierstrasz PS — Denotational Semantics 8.2 Roadmap Overview:  Syntax and Semantics  Semantics of Expressions.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Winter 2013.
5th MaCS Debrecen On the Turing-Completeness of Generative Metaprograms Zoltán Porkoláb, István Zólyomi Dept. of Programming.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
Encapsulation by Subprograms and Type Definitions
Sparkle A theorem prover for the functional language Clean Maarten de Mol University of Nijmegen February 2002.
Functional Programming Professor Yihjia Tsai Tamkang University.
1 Static Type Analysis of Path Expressions in XQuery Using Rho-Calculus Wang Zhen (Selina) Oct 26, 2006.
Distributed Object Computing Laboratory Washington University St. Louis, MO Rate-Monotonic Analysis in the C++ Type System Morgan Deters Model-Driven Embedded.
Paradigm Independent Software Complexity Metrics Dr. Zoltán Porkoláb Department of Programming Languages and Compilers Eötvös Loránd University, Faculty.
4 Dec 2001Kestrel1 From Patterns to Programming Languages Matthias Felleisen Northeastern University.
By Neng-Fa Zhou1 Evolution of programming languages –Machine language –Assembly language –Sub-routines and loop (Fortran) –Procedures and recursion (Algol,
Sigma Lisp Σλ Sam Davis Nick Alexander. What is Sigma Lisp? ● New dialect of Lisp ● Designed to be as expressive as possible.
1 COMP313A Functional Programming (1). 2 Main Differences with Imperative Languages Say more about what is computed as opposed to how Pure functional.
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.
Patterns in OCaml functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are.
CSC 580 – Theory of Programming Languages, Spring, 2009 Week 9: Functional Languages ML and Haskell, Dr. Dale E. Parson.
A Theory of Hygienic Macros David Herman, Mitchell Wand Northeastern University.
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.
Copyright 2005 Eric Niebler xpressive: Library Design on the Edge or, How I Learned to Stop Worrying and Love Template Meta- Programming.
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.
Compiling Functional Programs Mooly Sagiv Chapter 7
QuickCheck: A Lightweight Tool for Random Testing of Haskell Programs By Koen Claessen, Juhn Hughes ME: Mike Izbicki.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
3C-1 Purity Typing Language semantics Inheritance model  Single vs. Multiple inheritance  Common root Modular mechanisms Generics Object Oriented Languages.
Static Checking and Type Systems Chapter 6. 2 Static versus Dynamic Checking Static checking: the compiler enforces programming language’s static semantics.
First Order Haskell Neil Mitchell York University λ.
CS5205Haskell1 CS5205: Foundation in Programming Languages Basics of Functional Programming.
Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:
PROGRAMMING TECHNIQUES Based on “Modern C++ Design” BY ALEXANDRESCU BOGDAN PENKOVSKY 2010.
M.Sc. Seminar - Keren Lenz Supervisor - Dr. Yossi Gil July 1 st 2007 Simple and Safe SQL Queries with C++ Templates A RA R AT -
FUNCTIONAL PROGRAMING AT WORK - HASKELL AND DOMAIN SPECIFIC LANGUAGES Dr. John Peterson Western State Colorado University.
2000 Research Overview Dr. Kim Mens Programming Technology Lab Vrije Universiteit Brussel.
CS5205Semantics1 CS5205: Foundation in Programming Languages Semantics Static Semantics Dynamic Semantics Operational Semantics Big-step Small-Step Denotational.
CSE 332: C++ templates and generic programming II Review: Concepts and Models Templates impose requirements on type parameters –Types that are plugged.
Csontos Péter, Porkoláb Zoltán Eötvös Loránd Tudományegyetem, Budapest ECOOP 2001 On the complexity of exception handling.
1 Introduction to Functional Programming in Racket CS 270 Math Foundations of CS Jeremy Johnson.
DGrid: A Library of Large-Scale Distributed Spatial Data Structures Pieter Hooimeijer,
5th MaCS Debrecen On the Turing-Completeness of Generative Metaprograms Zoltán Porkoláb, István Zólyomi Dept. of Programming.
Functional Programming
Programming Languages
Sparkle a functional theorem prover
Towards a Multi-paradigm Complexity Measure
A lightening tour in 45 minutes
Closures and Streams cs784(Prasad) L11Clos
Emily Leland (Not Nick) Spring 2017
Introduction to Functional Programming in Racket
Evolution of programming languages
Important Concepts from Clojure
Important Concepts from Clojure
Dycore Rewrite Tobias Gysi.
Programming Languages
FP Foundations, Scheme In Text: Chapter 14.
when it needs to be done at compile-time
Adapted from slides by Nicholas Shahan and Dan Grossman
Combining Compile-Time and Run-Time Components
Introduction to Functional Programming in Racket
Adapted from slides by Nicholas Shahan, Dan Grossman, and Tam Dang
CSE 3302 Programming Languages
Nicholas Shahan Spring 2016
Important Concepts from Clojure
Presentation transcript:

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 Zsók Department of Programming Languages and Compilers Faculty of Informatics, Eötvös Loránd University, Budapest * Supported by GVOP /3.0 and SA 66ÖU2

Meta IFL 2007 Freiburg2 Contents Template Metaprogramming Metaprogramming & Functional Programming A Functional TMP interface Example: Lazy data types Evaluation

Meta IFL 2007 Freiburg3 C++ Template Metaprogramming I Template: –list, list –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

Meta IFL 2007 Freiburg4 C++ TMP: an example template struct Factorial { enum { value = N*Factorial ::value }; }; template <> struct Factorial { enum { value = 1 }; }; int main() { int r = Factorial ::value; }

Meta IFL 2007 Freiburg5 Application areas of TMP Concept checking Expression templates Compile-time code adaptation Compiler construction Active libraries Language embedding etc

Meta IFL 2007 Freiburg6 TMP is Functional Referential transparency –no mutable variables –no loops Pattern matching Higher order TMPs Lazyness?

Meta IFL 2007 Freiburg7 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

Meta IFL 2007 Freiburg8 Meta 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

Meta IFL 2007 Freiburg9 Structure of Meta Clean-like C++ Translated user code C++ TMP Clean-like Engine in C++ TMP C++ Compiled C++ Standard C++ Compiler Translator

Meta IFL 2007 Freiburg10 Components of Meta 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

Meta IFL 2007 Freiburg11 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..]))) -> …………

Meta IFL 2007 Freiburg12 Sieve: translated code template struct filter > { typedef typename if_c, Cons > >::type right; }; template struct sieve { typedef NoMatch right; }; template struct sieve > { typedef Cons > > right; };

Meta IFL 2007 Freiburg13 Lazy list and Sieve template struct EnumFrom { typedef Cons > right; }; take > take > > > Cons > > > > Cons > > > > > Cons > > > > …………

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

Meta IFL 2007 Freiburg15 Compilation times

Meta IFL 2007 Freiburg16 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

Meta IFL 2007 Freiburg17 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 - a portable way to achieve the goals Lazy data types – a proof of concepts Many open questions

Meta IFL 2007 Freiburg18 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); Functional interface

Meta IFL 2007 Freiburg19 Meta - Towards a Functional-Style Interface for C++ Template Metaprograms * Ádám Sipos, Zoltán Porkoláb, Norbert Pataki, Viktória Zsók Department of Programming Languages and Compilers Faculty of Informatics, Eötvös Loránd University, Budapest Thank you! Questions?