Haskell and C++ Template Metaprogramming. Bartosz Milewski

Similar documents
C++ Template Metaprogramming

template <typename T> // unless it s an unqualified pointer struct IsPtr<T *> { enum { r = true }; };

} else if( Ellipse *e = dynamic_cast<ellipse *>(shape) ) { } else if( Square *s = dynamic_cast<square *>(shape) ) {

CSC324 Principles of Programming Languages

Haskell Overview II (2A) Young Won Lim 8/9/16

Variadic Templates: Exploring the Design Space

Haskell Overview II (2A) Young Won Lim 8/23/16

Deducing the type of variable from its initializer expression Programming Language C++ Document no: N1721=

An Introduction to Template Metaprogramming

Design Patterns in C++

Design Patterns in C++

On the correctness of template metaprograms

UNIVERSITY OF TORONTO Faculty of Arts and Science. Midterm Sample Solutions CSC324H1 Duration: 50 minutes Instructor(s): David Liu.

Functional Programming Mid-term exam Tuesday 3/10/2017

CSCE 314 Programming Languages

Haskell Overview II (2A) Young Won Lim 9/26/16

Intrinsic Currying for C++ Template Metaprograms

Deducing the type of variable from its initializer expression (revision 3)

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking

Shell CSCE 314 TAMU. Higher Order Functions

P0949R0. Adding support for type-based metaprogramming to the standard library. Peter Dimov

Proposed Wording for Variadic Templates (Revision 1)

Deducing the type of variable from its initializer expression (revision 4)

Proposed Wording for Variadic Templates

Generic Programming and First-Class Libraries

An introduction to C++ template programming

Lambdas in unevaluated contexts

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08013 INFORMATICS 1 - FUNCTIONAL PROGRAMMING

Simulating Partial Specialization

Functional Programming and Haskell

C++ Programming Lecture 7 Software Engineering Group

Concurrent Programming in the D Programming Language. by Walter Bright Digital Mars

Programming at Compile Time. Rainer Grimm Training, Coaching, and Technology Consulting

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

Lambda Calculi With Polymorphism

Lambdas in unevaluated contexts

Template metaprograms

September 10,

REFLECTIVE METAPROGRAMMING IN C++ Дэвид Вандевурд Edison Design Group

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Template metaprogramming

Introduction. Three Cool Things About D code::dive Conference, Wrocław, Poland. Andrei Alexandrescu, Ph.D.

Haskell An Introduction

MetaProgramming. Programming from above. Python. C++ (Template MetaProgramming TMP)

FUNCTIONAL PROGRAMMING

CS115 - Module 9 - filter, map, and friends

C++ Programming Lecture 6 Software Engineering Group

Variadic Templates. Andrei Alexandrescu Research Scientist Facebook. c 2012 Andrei Alexandrescu, Facebook. 1 / 34

Programming Paradigms and Languages Introduction to Haskell. dr Robert Kowalczyk WMiI UŁ

INTRODUCTION TO HASKELL

Introduction to Compilers Professor Jeremy Siek

Introduction to Functional Programming

Haskell Types COMP360

Decltype (revision 6): proposed wording

D Programming Language: The Sudden

C++ Programming Lecture 7 Software Engineering Group

Generic Programming Galore Using D

A traditional typelist is a nested type structure that represents an ordered sequence of zero or more types. They look like this:

Debugging and Profiling C++ Template Metaprograms

Map and Fold. Prof. Clarkson Fall Today s music: Selections from the soundtrack to 2001: A Space Odyssey

Standard prelude. Appendix A. A.1 Classes

Using FPLs to Facilitate C++MP. Seyed Hossein Haeri Sibylle Schupp Jonathan Hüser

Red Code / Green Code: Generalizing const

Precompilation: an alternative approach to provide native Generic Programming support in C++

Francesco Nidito. Programmazione Avanzata AA 2005/06

Informatics 1 Functional Programming Lecture 7. Map, filter, fold. Don Sannella University of Edinburgh

Module 10: Imperative Programming, Modularization, and The Future

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

CS 440: Programming Languages and Translators, Spring 2019 Mon

Exercises on ML. Programming Languages. Chanseok Oh

Programming at Compile Time. Rainer Grimm Training, Coaching, and Technology Consulting

Tuples. CMSC 330: Organization of Programming Languages. Examples With Tuples. Another Example

Functional Programming in C++

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08013 INFORMATICS 1 - FUNCTIONAL PROGRAMMING

CERTIUserDocumentation

Functional Programming in Haskell for A level teachers

C++ Programming Lecture 13 Software Engineering Group

Functional Programming in C++

Proposal for adding tuple types into the standard library

Introduction to Standard C++

Metaprogramming. Concepts of Programming Languages. Alexander Schramm. 2. November Institut für Softwaretechnik und Programmiersprachen

Structuur van Computerprogramma s 2

Using Enum Structs as Bitfields

C++11 METAPROGRAMMING APPLIED TO SOFTWARE OBFUSCATION SEBASTIEN ANDRIVET

Introduction to the D programming language. Marc Fuentes - SED

C++ Metastring Library

Pointers, Dynamic Data, and Reference Types

C++ for numerical computing - part 2

Functional abstraction. What is abstraction? Eating apples. Readings: HtDP, sections Language level: Intermediate Student With Lambda

Functional abstraction

UNIT TESTING OF C++ TEMPLATE METAPROGRAMS

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Continuation Passing Style. Continuation Passing Style

CS205: Scalable Software Systems

Types and Type Inference

List Functions, and Higher-Order Functions

Logic - CM0845 Introduction to Haskell

Data Abstraction. An Abstraction for Inductive Data Types. Philip W. L. Fong.

Other C++11/14 features

An Oddysey in C++ A Guided Tour of Modern C++ Per Karlström Upplysningen

Transcription:

Haskell and C++ Template Metaprogramming Bartosz Milewski

Why Haskell? Easy syntax Almost one-to-one match with C++ TMP Differences Runtime vs. compile-time Regular data vs. types

Plan Functions and Metafunctions Lists Higher-Order Functions Closures Variadic Templates and TPPs List Comprehension Continuations Alternative Paradigms Bibliography

Teaser template<template<class...> class cont, template<class> class f, class... lst> struct map_cont { static const int value = cont<typename f<lst>::type... >::value;

Functions fact 0 = 1 fact n = n * fact (n - 1) fact 4 template<int n> struct fact { static const int value = n * fact<n - 1>::value; template<> struct fact<0> { // specialization for n = 0 static const int value = 1; fact<4>::value

Predicates is_zero 0 = True is_zero x = False template<class T> struct isptr { static const bool value = false; template<class U> struct isptr<u*> { static const bool value = true;

Lists count [] = 0 count (head:tail) = 1 + count tail template<class... list> struct count; template<> struct count<> { static const int value = 0; template<class head, class... tail> struct count<head, tail...> { static const int value = 1 + count<tail...>::value; int n = count<int, char, long>::value;

Higher-Order Functions and Closures or_combinator f1 f2 = λ x -> (f1 x) (f2 x) (or_combinator is_zero is_one) 2 template<template<class> class f1, template<class> class f2> struct or_combinator { template<class T> struct lambda { static const bool value = f1<t>::value f2<t>::value; or_combinator<isptr, isconst>::lambda<const int>::value

Higher-Order Functions on Lists all pred [] = True all pred (head:tail) = (pred head) && (all pred tail) all is_zero [0, 0, 1] template<template<class> class predicate, class... list> struct all; template<template<class> class predicate> struct all<predicate> { static const bool value = true; Continued

all pred (head:tail) = (pred head) && (all pred tail) template< template<class> class predicate, class head, class... tail> struct all<predicate, head, tail...> { static const bool value = predicate<head>::value && all<predicate, tail...>::value;

Fold Right foldr f init [] = init foldr f init (head:tail) = f head (foldr f init tail) template<template<class, int> class, int, class...> struct fold_right; template<template<class, int> class f, int init> struct fold_right<f, init> { static const int value = init; template<template<class, int> class f, int init, class head, class...tail> struct fold_right<f, init, head, tail...> { static const int value = f<head, fold_right<f, init, tail...>::value>::value;

Lists of Numbers sum [] = 0 sum (head:tail) = head + (sum tail) template<int...> struct sum; template<> struct sum<> { static const int value = 0; template<int i, int... tail> struct sum<i, tail...> { static const int value = i + sum<tail...>::value;

List Comprehension [x * x x <- [3, 4, 5]] count lst = sum [1 x <- lst] one x = 1 count lst = sum [one x x <- lst] template<class T> struct one { static const int value = 1; template<class... lst> struct count { static const int value = sum<one<lst>::value...>::value;

Pattern Expansion count lst = sum [one x x <- lst] template<class... lst> struct count { static const int value = sum<one<lst>::value... >::value; int n = count<int, char, void*>::value; // Expansion: // sum<one<int>::value, one<char>::value, one<void*>::value>::value // Not: // sum<one<int, char, void*>::value> // That would be: // sum<one<lst >::value>

Map map f lst = [f x x <- lst] // Does not compile! template<template<class> class f, class... lst> struct map { typedef f<lst>... type;

Continuations map_cont cont f lst = cont [f x x <- lst] count_cont lst = map_cont sum one lst template<template<class...> class cont, template<class> class f, class... lst> struct map_cont { static const int value = cont<typename f<lst>::type... >::value;

Alternative Paradigms all pred [] = True all pred (head:tail) = (pred head) && (all pred tail) metacode all (Pred, T...) { foreach (t; T) if (!Pred(t)) return false; return true; } template allsatisfy(alias F, T...) { static if (T.length == 1) alias F!(T[0]) allsatisfy; else enum bool allsatisfy = F!(T[0]) && allsatisfy!(f, T[1.. $]); }

Conclusions C++ Template Metaprogramming = Subset of Haskell + Maximally Obfuscated Syntax C++ TMP not designed but discovered Functional paradigm just a fluke. Everything could be done using imperative paradigm (see Daveed Vandevoorde s proposal)

Bibliography http://bartoszmilewski.wordpress.com contains the blog version of this talk Andrei Alexandrescu, Modern C++ Design David Abrahams and Aleksey Gurtvoy, C++ Template Metaprogramming Variadic Templates, Douglas Gregor, Jaakko Järvi, and Gary Powell Daveed Vandevoorde, Reflective Metaprogramming in C++.