Generics Akim Demaille, Etienne Renault, Roland Levillain March 29, - - PowerPoint PPT Presentation

generics
SMART_READER_LITE
LIVE PREVIEW

Generics Akim Demaille, Etienne Renault, Roland Levillain March 29, - - PowerPoint PPT Presentation

Generics Akim Demaille, Etienne Renault, Roland Levillain March 29, 2020 TYLA Generics March 29, 2020 1 / 54 Table of Contents Some definitions 1 Some history 2 Some Paradigms 3 TYLA Generics March 29, 2020 2 / 54 Problem Statement


slide-1
SLIDE 1

Generics

Akim Demaille, Etienne Renault, Roland Levillain March 29, 2020

TYLA Generics March 29, 2020 1 / 54

slide-2
SLIDE 2

Table of Contents

1

Some definitions

2

Some history

3

Some Paradigms

TYLA Generics March 29, 2020 2 / 54

slide-3
SLIDE 3

Problem Statement

How to write a data structure or algorithm that can work with elements of many different types?

TYLA Generics March 29, 2020 3 / 54

slide-4
SLIDE 4

A Definition of Generic Programming

Generic programming is a sub-discipline of computer science that deals with finding abstract representations of efficient algorithms, data structures, and other sofware concepts, and with their systematic organization. The goal of generic programming is to express algorithms and data structures in a broadly adaptable, interoperable form that allows their direct use in sofware construction. — Jazayeri et al., 2000, Garcia et al., 2003

TYLA Generics March 29, 2020 4 / 54

slide-5
SLIDE 5

A Definition of Generic Programming (cont’d)

Key ideas include: — Jazayeri et al., 2000, Garcia et al., 2003

TYLA Generics March 29, 2020 5 / 54

slide-6
SLIDE 6

A Definition of Generic Programming (cont’d)

Key ideas include: Expressing algorithms with minimal assumptions about data abstractions, and vice versa, thus making them as interoperable as possible. — Jazayeri et al., 2000, Garcia et al., 2003

TYLA Generics March 29, 2020 5 / 54

slide-7
SLIDE 7

A Definition of Generic Programming (cont’d)

Key ideas include: Expressing algorithms with minimal assumptions about data abstractions, and vice versa, thus making them as interoperable as possible. Lifing of a concrete algorithm to as general a level as possible without losing efficiency; i.e., the most abstract form such that when specialized back to the concrete case the result is just as efficient as the original algorithm. — Jazayeri et al., 2000, Garcia et al., 2003

TYLA Generics March 29, 2020 5 / 54

slide-8
SLIDE 8

A Definition of Generic Programming (cont’d)

— Jazayeri et al., 2000, Garcia et al., 2003

TYLA Generics March 29, 2020 6 / 54

slide-9
SLIDE 9

A Definition of Generic Programming (cont’d)

When the result of lifing is not general enough to cover all uses of an algorithm, additionally providing a more general form, but ensuring that the most efficient specialized form is automatically chosen when applicable. — Jazayeri et al., 2000, Garcia et al., 2003

TYLA Generics March 29, 2020 6 / 54

slide-10
SLIDE 10

A Definition of Generic Programming (cont’d)

When the result of lifing is not general enough to cover all uses of an algorithm, additionally providing a more general form, but ensuring that the most efficient specialized form is automatically chosen when applicable. Providing more than one generic algorithm for the same purpose and at the same level of abstraction, when none dominates the others in efficiency for all inputs. This introduces the necessity to provide sufficiently precise characterizations of the domain for which each algorithm is the most efficient. — Jazayeri et al., 2000, Garcia et al., 2003

TYLA Generics March 29, 2020 6 / 54

slide-11
SLIDE 11

Table of Contents

1

Some definitions

2

Some history

3

Some Paradigms

TYLA Generics March 29, 2020 7 / 54

slide-12
SLIDE 12

Table of Contents

1

Some definitions

2

Some history CLU Ada 83 C++

3

Some Paradigms

TYLA Generics March 29, 2020 8 / 54

slide-13
SLIDE 13

Barbara Liskov

TYLA Generics March 29, 2020 9 / 54

slide-14
SLIDE 14

Barbara Liskov

  • Nov. 7, 1939

Stanford PhD supervised by J. McCarthy Teaches at MIT CLU (pronounce “clue”) John von Neumann Medal (2004)

  • A. M. Turing Award (2008)

TYLA Generics March 29, 2020 10 / 54

slide-15
SLIDE 15

Genericity in CLU

First ideas of generic programming date back from CLU [HOPL’93] (in 1974, before it was named like this).

TYLA Generics March 29, 2020 11 / 54

slide-16
SLIDE 16

Genericity in CLU

First ideas of generic programming date back from CLU [HOPL’93] (in 1974, before it was named like this). Some programming concepts present in CLU:

◮ data abstraction (encapsulation) ◮ iterators (well, generators actually) ◮ type safe variants (oneof) ◮ multiple assignment (x, y, z = f(t)) ◮ parameterized modules TYLA Generics March 29, 2020 11 / 54

slide-17
SLIDE 17

Genericity in CLU

First ideas of generic programming date back from CLU [HOPL’93] (in 1974, before it was named like this). Some programming concepts present in CLU:

◮ data abstraction (encapsulation) ◮ iterators (well, generators actually) ◮ type safe variants (oneof) ◮ multiple assignment (x, y, z = f(t)) ◮ parameterized modules

In CLU, modules are implemented as clusters programming units grouping a data type and its operations.

TYLA Generics March 29, 2020 11 / 54

slide-18
SLIDE 18

Genericity in CLU

First ideas of generic programming date back from CLU [HOPL’93] (in 1974, before it was named like this). Some programming concepts present in CLU:

◮ data abstraction (encapsulation) ◮ iterators (well, generators actually) ◮ type safe variants (oneof) ◮ multiple assignment (x, y, z = f(t)) ◮ parameterized modules

In CLU, modules are implemented as clusters programming units grouping a data type and its operations. Notion of parametric polymorphism.

TYLA Generics March 29, 2020 11 / 54

slide-19
SLIDE 19

Parameterized modules in CLU

Initially: parameters checked at run time.

TYLA Generics March 29, 2020 12 / 54

slide-20
SLIDE 20

Parameterized modules in CLU

Initially: parameters checked at run time. Then: introduction of where-clauses (requirements on parameter(s)).

TYLA Generics March 29, 2020 12 / 54

slide-21
SLIDE 21

Parameterized modules in CLU

Initially: parameters checked at run time. Then: introduction of where-clauses (requirements on parameter(s)). Only operations of the type parameter(s) listed in the where-clause may be used.

TYLA Generics March 29, 2020 12 / 54

slide-22
SLIDE 22

Parameterized modules in CLU

Initially: parameters checked at run time. Then: introduction of where-clauses (requirements on parameter(s)). Only operations of the type parameter(s) listed in the where-clause may be used. → Complete compile-time check of parameterized modules.

TYLA Generics March 29, 2020 12 / 54

slide-23
SLIDE 23

Parameterized modules in CLU

Initially: parameters checked at run time. Then: introduction of where-clauses (requirements on parameter(s)). Only operations of the type parameter(s) listed in the where-clause may be used. → Complete compile-time check of parameterized modules. → Generation of a single code.

TYLA Generics March 29, 2020 12 / 54

slide-24
SLIDE 24

An example of parameterized module in CLU

s e t = c l u s t e r [ t : type ] i s create , member , size , i n s e r t , delete , elements where t has equal : proctype ( t , t ) r e t u r n s ( bool )

Note: Inside set, the only valid operation on t values is equal.

TYLA Generics March 29, 2020 13 / 54

slide-25
SLIDE 25

Implementation of parameterized modules in CLU

Notion of instantiation: binding a module and its parameter(s)

TYLA Generics March 29, 2020 14 / 54

slide-26
SLIDE 26

Implementation of parameterized modules in CLU

Notion of instantiation: binding a module and its parameter(s) Syntax: module[parameter]

TYLA Generics March 29, 2020 14 / 54

slide-27
SLIDE 27

Implementation of parameterized modules in CLU

Notion of instantiation: binding a module and its parameter(s) Syntax: module[parameter] Dynamic instantiation of parameterized modules.

TYLA Generics March 29, 2020 14 / 54

slide-28
SLIDE 28

Implementation of parameterized modules in CLU

Notion of instantiation: binding a module and its parameter(s) Syntax: module[parameter] Dynamic instantiation of parameterized modules. For a given module, each distinct set of parameters is represented by a (run-time) object.

TYLA Generics March 29, 2020 14 / 54

slide-29
SLIDE 29

Implementation of parameterized modules in CLU

Notion of instantiation: binding a module and its parameter(s) Syntax: module[parameter] Dynamic instantiation of parameterized modules. For a given module, each distinct set of parameters is represented by a (run-time) object. Instantiated modules derived from a non-instantiated object

  • module. Common code is shared.

TYLA Generics March 29, 2020 14 / 54

slide-30
SLIDE 30

Implementation of parameterized modules in CLU

Notion of instantiation: binding a module and its parameter(s) Syntax: module[parameter] Dynamic instantiation of parameterized modules. For a given module, each distinct set of parameters is represented by a (run-time) object. Instantiated modules derived from a non-instantiated object

  • module. Common code is shared.

Pros and cons of run- or load-time binding:

TYLA Generics March 29, 2020 14 / 54

slide-31
SLIDE 31

Implementation of parameterized modules in CLU

Notion of instantiation: binding a module and its parameter(s) Syntax: module[parameter] Dynamic instantiation of parameterized modules. For a given module, each distinct set of parameters is represented by a (run-time) object. Instantiated modules derived from a non-instantiated object

  • module. Common code is shared.

Pros and cons of run- or load-time binding: Pros No combinatorial explosion due to systematic code generation (as with C++ templates).

TYLA Generics March 29, 2020 14 / 54

slide-32
SLIDE 32

Implementation of parameterized modules in CLU

Notion of instantiation: binding a module and its parameter(s) Syntax: module[parameter] Dynamic instantiation of parameterized modules. For a given module, each distinct set of parameters is represented by a (run-time) object. Instantiated modules derived from a non-instantiated object

  • module. Common code is shared.

Pros and cons of run- or load-time binding: Pros No combinatorial explosion due to systematic code generation (as with C++ templates). Cons Lack of static instantiation context means less

  • pportunities to optimize.

TYLA Generics March 29, 2020 14 / 54

slide-33
SLIDE 33

TYLA Generics March 29, 2020 15 / 54

slide-34
SLIDE 34

Table of Contents

1

Some definitions

2

Some history CLU Ada 83 C++

3

Some Paradigms

TYLA Generics March 29, 2020 16 / 54

slide-35
SLIDE 35

Genericity in Ada 83

Introduced with the generic keyword

generic type T is private ; procedure swap ( x , y : in out T ) is t : T begin t : = x ; x : = y ; y : = t ; end swap ;

  • - Explicit instantiations.

procedure int swap is new swap ( INTEGER ) ; procedure str swap is new swap ( STRING ) ;

Example of unconstrained genericity. Instantiation of generic clauses is explicit (no implicit instantiation as in C++).

TYLA Generics March 29, 2020 17 / 54

slide-36
SLIDE 36

Generic packages in Ada 83

generic type T is private ; package STACKS is type STACK ( s i z e : POSITIVE ) is record space : array ( 1 . . s i z e ) of T ; index : NATURAL end record ; function empty ( s : in STACK ) return BOOLEAN ; procedure push ( t : in T ; s : in out STACK ) ; procedure pop ( s : in out STACK ) ; function top ( s : in STACK ) return T ; end STACKS ; package INT STACKS is new STACKS ( INTEGER ) ; package STR STACKS is new STACKS ( STRING ) ;

TYLA Generics March 29, 2020 18 / 54

slide-37
SLIDE 37

Constrained Genericity in Ada 83

Constrained genericity imposes restrictions on generic types:

generic type T is private ; with function ”<=” ( a , b : T ) return BOOLEAN is <>; function minimum ( x , y : T ) return T is begin if x <= y then return x ; else return y ; end if ; end minimum ;

TYLA Generics March 29, 2020 19 / 54

slide-38
SLIDE 38

Constrained Genericity in Ada 83

Constrained genericity imposes restrictions on generic types:

generic type T is private ; with function ”<=” ( a , b : T ) return BOOLEAN is <>; function minimum ( x , y : T ) return T is begin if x <= y then return x ; else return y ; end if ; end minimum ;

Constraints are only of syntactic nature (no formal constraints expressing semantic assertions)

TYLA Generics March 29, 2020 19 / 54

slide-39
SLIDE 39

Constrained Genericity in Ada 83: Instantiation

Instantiation can be fully qualified

function T1 minimum is new minimum ( T1 , T 1 l e ) ;

TYLA Generics March 29, 2020 20 / 54

slide-40
SLIDE 40

Constrained Genericity in Ada 83: Instantiation

Instantiation can be fully qualified

function T1 minimum is new minimum ( T1 , T 1 l e ) ;

  • r take advantage of implicit names:

function int minimum is new minimum ( INTEGER ) ;

Here, the comparison function is already known as <=.

TYLA Generics March 29, 2020 20 / 54

slide-41
SLIDE 41

More Genericity Examples in Ada 83

Interface (“specification”):

  • - matrices.ada

generic type T is private ; zero : T ; unity : T ; with function ”+” ( a , b : T ) return T is <>; with function ” ∗ ” ( a , b : T ) return T is <>; package MATRICES is type MATRIX ( l i n e s , columns : POSITIVE ) is array ( 1 . . l i n e s , 1 . . columns ) of T ; function ”+” (m1 , m2 : MATRIX ) return MATRIX ; function ” ∗ ” (m1 , m2 : MATRIX ) return MATRIX ; end MATRICES ;

TYLA Generics March 29, 2020 21 / 54

slide-42
SLIDE 42

More Genericity Examples in Ada 83

Instantiations:

package FLOAT MATRICES is new MATRICES ( FLOAT , 0 . 0 , 1 . 0 ) ;

TYLA Generics March 29, 2020 22 / 54

slide-43
SLIDE 43

More Genericity Examples in Ada 83

Instantiations:

package FLOAT MATRICES is new MATRICES ( FLOAT , 0 . 0 , 1 . 0 ) ; package BOOL MATRICES is new MATRICES (BOOLEAN, f a l s e , true , ” or ” , ” and ” ) ;

TYLA Generics March 29, 2020 22 / 54

slide-44
SLIDE 44

More Genericity Examples in Ada 83

Implementation (“body”):

  • - matrices.adb

package body MATRICES is function ” ∗ ” (m1 , m2 : MATRIX ) is r e s u l t : MATRIX (m1’ l i n e s , m2’ columns ) begin if m1’ columns /= m2’ l i n e s then raise INCOMPATIBLE SIZES ; end if ; for i in m1’RANGE ( 1 ) loop for j in m2’RANGE ( 2 ) loop r e s u l t ( i , j ) : = zero ; for k in m1’RANGE ( 2 ) loop r e s u l t ( i , j ) : = r e s u l t ( i , j ) + m1 ( i , k ) ∗ m2 ( k , j ) ; end loop ; end loop ; end loop ; end ” ∗ ” ;

  • - Other declarations...

end MATRICES ;

TYLA Generics March 29, 2020 23 / 54

slide-45
SLIDE 45

Table of Contents

1

Some definitions

2

Some history CLU Ada 83 C++

3

Some Paradigms

TYLA Generics March 29, 2020 24 / 54

slide-46
SLIDE 46

A History of C++ Templates

Initial motivation: provide parameterized containers.

TYLA Generics March 29, 2020 25 / 54

slide-47
SLIDE 47

A History of C++ Templates

Initial motivation: provide parameterized containers. Previously, macros were used to provide such containers (in C and C with classes).

TYLA Generics March 29, 2020 25 / 54

slide-48
SLIDE 48

A History of C++ Templates

Initial motivation: provide parameterized containers. Previously, macros were used to provide such containers (in C and C with classes). Many limitations, inherent to the nature of macros:

TYLA Generics March 29, 2020 25 / 54

slide-49
SLIDE 49

A History of C++ Templates

Initial motivation: provide parameterized containers. Previously, macros were used to provide such containers (in C and C with classes). Many limitations, inherent to the nature of macros:

◮ Poor error messages

referring to the code writen by cpp, not by the programmer.

TYLA Generics March 29, 2020 25 / 54

slide-50
SLIDE 50

A History of C++ Templates

Initial motivation: provide parameterized containers. Previously, macros were used to provide such containers (in C and C with classes). Many limitations, inherent to the nature of macros:

◮ Poor error messages

referring to the code writen by cpp, not by the programmer.

◮ Need to instantiate templates once per compile unit, manually. TYLA Generics March 29, 2020 25 / 54

slide-51
SLIDE 51

A History of C++ Templates

Initial motivation: provide parameterized containers. Previously, macros were used to provide such containers (in C and C with classes). Many limitations, inherent to the nature of macros:

◮ Poor error messages

referring to the code writen by cpp, not by the programmer.

◮ Need to instantiate templates once per compile unit, manually. ◮ No support for recurrence. TYLA Generics March 29, 2020 25 / 54

slide-52
SLIDE 52

Simulating parameterized types with macros

#define VECTOR( T ) v e c t o r ## T #define GEN VECTOR( T ) \ class VECTOR( T ) { \ public : \ typedef T value type ; \ VECTOR( T ) ( ) { /* ... */ } \ VECTOR( T ) ( int i ) { /* ... */ } \ value type& operator [ ] ( int i ) { /* ... */ } \ /* ... */ \ } // Explicit instantiations. GEN VECTOR(int ) ; GEN VECTOR(long ) ; int main ( ) { VECTOR(int) v i ; VECTOR(long) v l ; }

TYLA Generics March 29, 2020 26 / 54

slide-53
SLIDE 53

A History of C++ Templates (cont.)

Introduction of a template mechanism around 1990, later refined (1993) before the standardization of C++ in 1998.

TYLA Generics March 29, 2020 27 / 54

slide-54
SLIDE 54

A History of C++ Templates (cont.)

Introduction of a template mechanism around 1990, later refined (1993) before the standardization of C++ in 1998. Class templates.

TYLA Generics March 29, 2020 27 / 54

slide-55
SLIDE 55

A History of C++ Templates (cont.)

Introduction of a template mechanism around 1990, later refined (1993) before the standardization of C++ in 1998. Class templates. Function templates (and member function templates).

TYLA Generics March 29, 2020 27 / 54

slide-56
SLIDE 56

A History of C++ Templates (cont.)

Introduction of a template mechanism around 1990, later refined (1993) before the standardization of C++ in 1998. Class templates. Function templates (and member function templates). Automatic deduction of parameters of template functions.

TYLA Generics March 29, 2020 27 / 54

slide-57
SLIDE 57

A History of C++ Templates (cont.)

Introduction of a template mechanism around 1990, later refined (1993) before the standardization of C++ in 1998. Class templates. Function templates (and member function templates). Automatic deduction of parameters of template functions. Type and non-type template parameters.

TYLA Generics March 29, 2020 27 / 54

slide-58
SLIDE 58

A History of C++ Templates (cont.)

Introduction of a template mechanism around 1990, later refined (1993) before the standardization of C++ in 1998. Class templates. Function templates (and member function templates). Automatic deduction of parameters of template functions. Type and non-type template parameters. No explicit constraints on parameters.

TYLA Generics March 29, 2020 27 / 54

slide-59
SLIDE 59

A History of C++ Templates (cont.)

Introduction of a template mechanism around 1990, later refined (1993) before the standardization of C++ in 1998. Class templates. Function templates (and member function templates). Automatic deduction of parameters of template functions. Type and non-type template parameters. No explicit constraints on parameters. Implicit (automatic) template instantiation (though explicit instantiation is still possible).

TYLA Generics March 29, 2020 27 / 54

slide-60
SLIDE 60

A History of C++ Templates (cont.)

Introduction of a template mechanism around 1990, later refined (1993) before the standardization of C++ in 1998. Class templates. Function templates (and member function templates). Automatic deduction of parameters of template functions. Type and non-type template parameters. No explicit constraints on parameters. Implicit (automatic) template instantiation (though explicit instantiation is still possible). Full (classes, functions) and partial (classes) specializations of templates definitions.

TYLA Generics March 29, 2020 27 / 54

slide-61
SLIDE 61

A History of C++ Templates (cont.)

Introduction of a template mechanism around 1990, later refined (1993) before the standardization of C++ in 1998. Class templates. Function templates (and member function templates). Automatic deduction of parameters of template functions. Type and non-type template parameters. No explicit constraints on parameters. Implicit (automatic) template instantiation (though explicit instantiation is still possible). Full (classes, functions) and partial (classes) specializations of templates definitions. A powerful system allowing metaprogramming techniques (though not designed for that in the first place!)

TYLA Generics March 29, 2020 27 / 54

slide-62
SLIDE 62

Class Templates

template <typename T> class v e c t o r { public : typedef T value type ; v e c t o r ( ) { /* ... */ } v e c t o r (int i ) { /* ... */ } value type& operator [ ] ( int i ) { /* ... */ } /* ... */ }; // No need for explicit template instantiations. int main ( ) { vector <int > v i ; vector <long > v l ; }

TYLA Generics March 29, 2020 28 / 54

slide-63
SLIDE 63

Function Templates

Natural in a language with non-member functions (such as C++).

template <typename T> void swap ( T& a , T& b ) { T tmp = a ; a = b ; b = tmp ; }

TYLA Generics March 29, 2020 29 / 54

slide-64
SLIDE 64

Function Templates

Natural in a language with non-member functions (such as C++).

template <typename T> void swap ( T& a , T& b ) { T tmp = a ; a = b ; b = tmp ; }

Class templates can make up for the lack of generic functions in most uses cases (through fonctor).

TYLA Generics March 29, 2020 29 / 54

slide-65
SLIDE 65

Function Templates

Natural in a language with non-member functions (such as C++).

template <typename T> void swap ( T& a , T& b ) { T tmp = a ; a = b ; b = tmp ; }

Class templates can make up for the lack of generic functions in most uses cases (through fonctor). Eiffel does not feature generic function at all.

TYLA Generics March 29, 2020 29 / 54

slide-66
SLIDE 66

Function Templates

Natural in a language with non-member functions (such as C++).

template <typename T> void swap ( T& a , T& b ) { T tmp = a ; a = b ; b = tmp ; }

Class templates can make up for the lack of generic functions in most uses cases (through fonctor). Eiffel does not feature generic function at all. Java and C-sharp provide only generic member functions.

TYLA Generics March 29, 2020 29 / 54

slide-67
SLIDE 67

Specialization of Template Definitions

Idea: provide another definition for a subset of the parameters.

TYLA Generics March 29, 2020 30 / 54

slide-68
SLIDE 68

Specialization of Template Definitions

Idea: provide another definition for a subset of the parameters. Motivation: provide (harder,) beter, faster, stronger implementations for a given template class or function.

TYLA Generics March 29, 2020 30 / 54

slide-69
SLIDE 69

Specialization of Template Definitions

Idea: provide another definition for a subset of the parameters. Motivation: provide (harder,) beter, faster, stronger implementations for a given template class or function. Example: boolean vector has its own definition, different from type T vector

TYLA Generics March 29, 2020 30 / 54

slide-70
SLIDE 70

Specialization of Template Definitions

Idea: provide another definition for a subset of the parameters. Motivation: provide (harder,) beter, faster, stronger implementations for a given template class or function. Example: boolean vector has its own definition, different from type T vector Mechanism close to function overloading in spirit, but distinct.

TYLA Generics March 29, 2020 30 / 54

slide-71
SLIDE 71

Alexander Alexandrovich Stepanov (Nov. 16, 1950)

TYLA Generics March 29, 2020 31 / 54

slide-72
SLIDE 72

Alexander Alexandrovich Stepanov (Nov. 16, 1950)

Алекса́ндр Алекса́ндрович Степа́нов

TYLA Generics March 29, 2020 32 / 54

slide-73
SLIDE 73

The Standard Template Library (STL)

A library of containers, iterators, fundamental algorithms and tools, using C++ templates.

TYLA Generics March 29, 2020 33 / 54

slide-74
SLIDE 74

The Standard Template Library (STL)

A library of containers, iterators, fundamental algorithms and tools, using C++ templates. Designed by Alexander Stepanov at HP.

TYLA Generics March 29, 2020 33 / 54

slide-75
SLIDE 75

The Standard Template Library (STL)

A library of containers, iterators, fundamental algorithms and tools, using C++ templates. Designed by Alexander Stepanov at HP. The STL is not the Standard C++Library (nor is one a subset of the other) although most of it is part of the standard

TYLA Generics March 29, 2020 33 / 54

slide-76
SLIDE 76

The Standard Template Library (STL)

A library of containers, iterators, fundamental algorithms and tools, using C++ templates. Designed by Alexander Stepanov at HP. The STL is not the Standard C++Library (nor is one a subset of the other) although most of it is part of the standard Introduces the notion of concept: a set of syntactic and semantic requirements over one (or several) types.

TYLA Generics March 29, 2020 33 / 54

slide-77
SLIDE 77

The Standard Template Library (STL)

A library of containers, iterators, fundamental algorithms and tools, using C++ templates. Designed by Alexander Stepanov at HP. The STL is not the Standard C++Library (nor is one a subset of the other) although most of it is part of the standard Introduces the notion of concept: a set of syntactic and semantic requirements over one (or several) types. But the language does not enforce them.

TYLA Generics March 29, 2020 33 / 54

slide-78
SLIDE 78

The Standard Template Library (STL)

A library of containers, iterators, fundamental algorithms and tools, using C++ templates. Designed by Alexander Stepanov at HP. The STL is not the Standard C++Library (nor is one a subset of the other) although most of it is part of the standard Introduces the notion of concept: a set of syntactic and semantic requirements over one (or several) types. But the language does not enforce them. Initially planned as a language extension in the C++11/14/17 standard...

TYLA Generics March 29, 2020 33 / 54

slide-79
SLIDE 79

The Standard Template Library (STL)

A library of containers, iterators, fundamental algorithms and tools, using C++ templates. Designed by Alexander Stepanov at HP. The STL is not the Standard C++Library (nor is one a subset of the other) although most of it is part of the standard Introduces the notion of concept: a set of syntactic and semantic requirements over one (or several) types. But the language does not enforce them. Initially planned as a language extension in the C++11/14/17 standard... ...but abandonned shortly before the standardization. :-(

TYLA Generics March 29, 2020 33 / 54

slide-80
SLIDE 80

Example

template <typename T> concept Hashable = r e q u i r e s ( T a ) { { std : : hash<T>{}(a ) } − > std : : c o n v e r t i b l e t o <std : : s i z e t >; }; struct meow {}; template <Hashable T> void f ( T ) ; // constrained C++20 function template

TYLA Generics March 29, 2020 34 / 54

slide-81
SLIDE 81

Table of Contents

1

Some definitions

2

Some history

3

Some Paradigms

TYLA Generics March 29, 2020 35 / 54

slide-82
SLIDE 82

Problem Statement

How to implement Generics?

TYLA Generics March 29, 2020 36 / 54

slide-83
SLIDE 83

Table of Contents

1

Some definitions

2

Some history

3

Some Paradigms Boxing Monomorphization

TYLA Generics March 29, 2020 37 / 54

slide-84
SLIDE 84

Boxing: main idea

Put everything in uniform ”boxes” so that they all act the same way

TYLA Generics March 29, 2020 38 / 54

slide-85
SLIDE 85

Boxing: main idea

Put everything in uniform ”boxes” so that they all act the same way The data structure only handles pointers Wideley used strategy: C: use void pointers + dynamic cast Go: interface Java (pre-generics): Objects Objective-C (pre-generics): id

TYLA Generics March 29, 2020 38 / 54

slide-86
SLIDE 86

Boxing: main idea

Put everything in uniform ”boxes” so that they all act the same way The data structure only handles pointers Pointers to different types act the same way Wideley used strategy: C: use void pointers + dynamic cast Go: interface Java (pre-generics): Objects Objective-C (pre-generics): id

TYLA Generics March 29, 2020 38 / 54

slide-87
SLIDE 87

Boxing: main idea

Put everything in uniform ”boxes” so that they all act the same way The data structure only handles pointers Pointers to different types act the same way … so the same code can deal with all data types! Wideley used strategy: C: use void pointers + dynamic cast Go: interface Java (pre-generics): Objects Objective-C (pre-generics): id

TYLA Generics March 29, 2020 38 / 54

slide-88
SLIDE 88

Go example

type Stack struct { values [ ] i n t e r f a c e {} } func ( t h i s ∗ Stack ) Push ( value i n t e r f a c e {}) { t h i s . values = append ( t h i s . values , value ) }

TYLA Generics March 29, 2020 39 / 54

slide-89
SLIDE 89

Pro/cons with the boxing approach

Pros: Easy to implement in (any) language Cons: Casts for every read/write in the structure = ⇒ runtime overhead! Error-prone: type-checking = ⇒ No mechanism to prevent us puting elements of different types into the structure

TYLA Generics March 29, 2020 40 / 54

slide-90
SLIDE 90

Type-erased boxed generics

Idea

add generics functionality to the type system BUT use the basic boxing method exactly as before at runtime.

TYLA Generics March 29, 2020 41 / 54

slide-91
SLIDE 91

Type-erased boxed generics

Idea

add generics functionality to the type system BUT use the basic boxing method exactly as before at runtime. → This approach is ofen called type erasure, because the types in the generics system are ”erased” and all become the same type

TYLA Generics March 29, 2020 41 / 54

slide-92
SLIDE 92

Type-erased boxed generics

Idea

add generics functionality to the type system BUT use the basic boxing method exactly as before at runtime. → This approach is ofen called type erasure, because the types in the generics system are ”erased” and all become the same type Java and Objective-C both started out with basic boxing … but add features for generics with type erasure

TYLA Generics March 29, 2020 41 / 54

slide-93
SLIDE 93

Java Example

Without Generics (pre Java 4.0) Throws java.lang.ClassCastException

L i s t v = new A r r a y L i s t ( ) ; v . add ( ” t e s t ” ) ; // A String that cannot be cast to an Integer I n t e g e r i = ( I n t e g e r ) v . get ( 0 ) ; // Run time error

TYLA Generics March 29, 2020 42 / 54

slide-94
SLIDE 94

Java Example

Without Generics (pre Java 4.0) Throws java.lang.ClassCastException

L i s t v = new A r r a y L i s t ( ) ; v . add ( ” t e s t ” ) ; // A String that cannot be cast to an Integer I n t e g e r i = ( I n t e g e r ) v . get ( 0 ) ; // Run time error

With Generics Fails at compile time

List <String > v = new ArrayList <String > ( ) ; v . add ( ” t e s t ” ) ; I n t e g e r i = v . get ( 0 ) ; // (type error) compilation-time error

TYLA Generics March 29, 2020 42 / 54

slide-95
SLIDE 95

Todo Wildcard?

TYLA Generics March 29, 2020 43 / 54

slide-96
SLIDE 96

Inferred boxed generics with a uniform representation

Problem with simple boxing

In the previous approach, generic data structures cannot hold primitive types!

TYLA Generics March 29, 2020 44 / 54

slide-97
SLIDE 97

Inferred boxed generics with a uniform representation

Problem with simple boxing

In the previous approach, generic data structures cannot hold primitive types!

Ocaml’s Solution

Uniform representation where there are no primitive types that requires an additional boxing allocation !

TYLA Generics March 29, 2020 44 / 54

slide-98
SLIDE 98

Inferred boxed generics with a uniform representation (cont’d)

Ocaml’s apporach:

TYLA Generics March 29, 2020 45 / 54

slide-99
SLIDE 99

Inferred boxed generics with a uniform representation (cont’d)

Ocaml’s apporach: no additional boxing allocation (like int needing to be turned into an Integer

TYLA Generics March 29, 2020 45 / 54

slide-100
SLIDE 100

Inferred boxed generics with a uniform representation (cont’d)

Ocaml’s apporach: no additional boxing allocation (like int needing to be turned into an Integer everything is either already boxed or represented by a pointer-sized integer = ⇒ everything is one machine word

TYLA Generics March 29, 2020 45 / 54

slide-101
SLIDE 101

Inferred boxed generics with a uniform representation (cont’d)

Ocaml’s apporach: no additional boxing allocation (like int needing to be turned into an Integer everything is either already boxed or represented by a pointer-sized integer = ⇒ everything is one machine word Problem :garbage collector needs to distinguish pointers from integers

TYLA Generics March 29, 2020 45 / 54

slide-102
SLIDE 102

Inferred boxed generics with a uniform representation (cont’d)

Ocaml’s apporach: no additional boxing allocation (like int needing to be turned into an Integer everything is either already boxed or represented by a pointer-sized integer = ⇒ everything is one machine word Problem :garbage collector needs to distinguish pointers from integers … there is a reserved bit in machine word

TYLA Generics March 29, 2020 45 / 54

slide-103
SLIDE 103

Inferred boxed generics with a uniform representation (cont’d)

Ocaml’s apporach: no additional boxing allocation (like int needing to be turned into an Integer everything is either already boxed or represented by a pointer-sized integer = ⇒ everything is one machine word Problem :garbage collector needs to distinguish pointers from integers … there is a reserved bit in machine word

◮ integer size is only 31/63 bits ◮ pointer size is only 31/63 bits TYLA Generics March 29, 2020 45 / 54

slide-104
SLIDE 104

Inferred boxed generics with a uniform representation (cont’d)

Ocaml’s apporach: no additional boxing allocation (like int needing to be turned into an Integer everything is either already boxed or represented by a pointer-sized integer = ⇒ everything is one machine word Problem :garbage collector needs to distinguish pointers from integers … there is a reserved bit in machine word

◮ integer size is only 31/63 bits ◮ pointer size is only 31/63 bits ◮ the 32/64 bit for integer is 1 ◮ the 32/64 bit for valid aligned pointers is 0 TYLA Generics March 29, 2020 45 / 54

slide-105
SLIDE 105

Introducing Interfaces

Limitation with boxing

The boxed types are completely opaque! (generic sorting function need some extra functionality, like a type-specific comparison function.)

TYLA Generics March 29, 2020 46 / 54

slide-106
SLIDE 106

Introducing Interfaces

Limitation with boxing

The boxed types are completely opaque! (generic sorting function need some extra functionality, like a type-specific comparison function.)

Two families of solutions

Dictionary passing: Haskell (type class) and Ocaml (modules)

◮ Pass a table of the required function pointers along to generic

functions that need them

◮ similar to constructing Go-style interface objects at the call site

Interface vtables: Rust (dyn traits) & Golang (interface)

◮ When casting to interface type it creates a wrapper ◮ The wrapper contains (1) a pointer to the original object and (2) a

pointer to a vtable of the type-specific functions for that interface

TYLA Generics March 29, 2020 46 / 54

slide-107
SLIDE 107

A note on Dictionnary passing

Swif Witness Tables Use dictionary passing and put the size of types and how to move, copy and free them into the tables, Provide all the information required to work with any type in a uniform way …without boxing them. → swif uses monomorphization (later in lecture)

TYLA Generics March 29, 2020 47 / 54

slide-108
SLIDE 108

A note on Dictionnary passing

Swif Witness Tables Use dictionary passing and put the size of types and how to move, copy and free them into the tables, Provide all the information required to work with any type in a uniform way …without boxing them. → swif uses monomorphization (later in lecture)

Going further

Have a look to Intensional Type Analysis. boxed types is augmented to add a type ID generate functions for each interface method Dispatch using big switch statement over all the types

TYLA Generics March 29, 2020 47 / 54

slide-109
SLIDE 109

From interface vtables to Reflection (1/3)

In Object-oriented programming (like Java) No need to have separate interface objects the vtable pointer is embedded at the start of every object inheritance and interfaces that can be implemented entirely with these object vtables → construct new interface types with indirection is no longer required.

Reflection

With vtables, itffs not difficult to have reflection since the compiler can generates tables of other type information like field names, types and locations

TYLA Generics March 29, 2020 48 / 54

slide-110
SLIDE 110

From interface vtables to Reflection (2/3)

Reflection is the ability of a program to examine, introspect, and modify its own structure and behavior at runtime.

TYLA Generics March 29, 2020 49 / 54

slide-111
SLIDE 111

From interface vtables to Reflection (2/3)

Reflection is the ability of a program to examine, introspect, and modify its own structure and behavior at runtime. Reflection is not limited to OOP! and most functionnal languages can create new types! Python and Ruby have super-powered reflection systems that are used for everything.

TYLA Generics March 29, 2020 49 / 54

slide-112
SLIDE 112

From interface vtables to Reflection (3/3)

Introspection: ability to observe and therefore reason about its

  • wn state.

public boolean c l a s s e q u a l ( Object o1 , Object

  • 2 ){

Class c1 , c2 ; c1 = o1 . getClass ( ) ; c2 = o2 . getClass ( ) ; return ( c1 == c2 ) ; }

Intercession: ability to modify its execution state or alter its own interpretation

Class c = obj . getClass ( ) ; Object o = c . newInstance ( ) ; S t r i n g s = ” FooBar ” . Class c = Class . forName ( s ) ; Object o = c . newInstance ( ) ;

TYLA Generics March 29, 2020 50 / 54

slide-113
SLIDE 113

Table of Contents

1

Some definitions

2

Some history

3

Some Paradigms Boxing Monomorphization

TYLA Generics March 29, 2020 51 / 54

slide-114
SLIDE 114

Monomorphization

The monomorphization approach outputs multiple versions of the code for each type we want to use it with C++ template Rust procedural macros D

TYLA Generics March 29, 2020 52 / 54

slide-115
SLIDE 115

Metaprogramming

Writing programs that write programs. Some language a clean way of doing code generation Syntax tree macros: the ability to produce AST types in macros writen in the language Template: reason about types and type substitution Compile time functions

TYLA Generics March 29, 2020 53 / 54

slide-116
SLIDE 116

TYLA Generics March 29, 2020 54 / 54