generics - lrde.epita.fr

116
Generics Akim Demaille, Etienne Renault, Roland Levillain March 29, 2020 TYLA Generics March 29, 2020 1 / 54

Upload: others

Post on 03-May-2022

21 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Generics - lrde.epita.fr

Generics

Akim Demaille, Etienne Renault, Roland Levillain

March 29, 2020

TYLA Generics March 29, 2020 1 / 54

Page 2: Generics - lrde.epita.fr

Table of Contents

1 Some definitions

2 Some history

3 Some Paradigms

TYLA Generics March 29, 2020 2 / 54

Page 3: Generics - lrde.epita.fr

Problem Statement

How to write a data structure or algorithm that can work withelements of many di�erent types?

TYLA Generics March 29, 2020 3 / 54

Page 4: Generics - lrde.epita.fr

A Definition of Generic Programming

“ Generic programming is a sub-discipline of computerscience that deals with finding abstract representations ofe�icient algorithms, data structures, and other so�wareconcepts, and with their systematic organization.The goal of generic programming is to express algorithms anddata structures in a broadly adaptable, interoperable formthat allows their direct use in so�ware construction.

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

TYLA Generics March 29, 2020 4 / 54

Page 5: Generics - lrde.epita.fr

A Definition of Generic Programming (cont’d)

“ Key ideas include:

Expressing algorithms with minimal assumptions aboutdata abstractions, and vice versa, thus making them asinteroperable as possible.Li�ing of a concrete algorithm to as general a level aspossible without losing e�iciency; i.e., the most abstractform such that when specialized back to the concrete casethe result is just as e�icient as the original algorithm.

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

TYLA Generics March 29, 2020 5 / 54

Page 6: Generics - lrde.epita.fr

A Definition of Generic Programming (cont’d)

“ Key ideas include:Expressing algorithms with minimal assumptions aboutdata abstractions, and vice versa, thus making them asinteroperable as possible.

Li�ing of a concrete algorithm to as general a level aspossible without losing e�iciency; i.e., the most abstractform such that when specialized back to the concrete casethe result is just as e�icient as the original algorithm.

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

TYLA Generics March 29, 2020 5 / 54

Page 7: Generics - lrde.epita.fr

A Definition of Generic Programming (cont’d)

“ Key ideas include:Expressing algorithms with minimal assumptions aboutdata abstractions, and vice versa, thus making them asinteroperable as possible.Li�ing of a concrete algorithm to as general a level aspossible without losing e�iciency; i.e., the most abstractform such that when specialized back to the concrete casethe result is just as e�icient as the original algorithm.

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

TYLA Generics March 29, 2020 5 / 54

Page 8: Generics - lrde.epita.fr

A Definition of Generic Programming (cont’d)

When the result of li�ing is not general enough to coverall uses of an algorithm, additionally providing a moregeneral form, but ensuring that the most e�icientspecialized form is automatically chosen whenapplicable.Providing more than one generic algorithm for the samepurpose and at the same level of abstraction, when nonedominates the others in e�iciency for all inputs.This introduces the necessity to provide su�icientlyprecise characterizations of the domain for which eachalgorithm is the most e�icient.

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

TYLA Generics March 29, 2020 6 / 54

Page 9: Generics - lrde.epita.fr

A Definition of Generic Programming (cont’d)

“ When the result of li�ing is not general enough to coverall uses of an algorithm, additionally providing a moregeneral form, but ensuring that the most e�icientspecialized form is automatically chosen whenapplicable.

Providing more than one generic algorithm for the samepurpose and at the same level of abstraction, when nonedominates the others in e�iciency for all inputs.This introduces the necessity to provide su�icientlyprecise characterizations of the domain for which eachalgorithm is the most e�icient.

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

TYLA Generics March 29, 2020 6 / 54

Page 10: Generics - lrde.epita.fr

A Definition of Generic Programming (cont’d)

“ When the result of li�ing is not general enough to coverall uses of an algorithm, additionally providing a moregeneral form, but ensuring that the most e�icientspecialized form is automatically chosen whenapplicable.Providing more than one generic algorithm for the samepurpose and at the same level of abstraction, when nonedominates the others in e�iciency for all inputs.This introduces the necessity to provide su�icientlyprecise characterizations of the domain for which eachalgorithm is the most e�icient.

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

TYLA Generics March 29, 2020 6 / 54

Page 11: Generics - lrde.epita.fr

Table of Contents

1 Some definitions

2 Some history

3 Some Paradigms

TYLA Generics March 29, 2020 7 / 54

Page 12: Generics - lrde.epita.fr

Table of Contents

1 Some definitions

2 Some historyCLUAda 83C++

3 Some Paradigms

TYLA Generics March 29, 2020 8 / 54

Page 13: Generics - lrde.epita.fr

Barbara Liskov

TYLA Generics March 29, 2020 9 / 54

Page 14: Generics - lrde.epita.fr

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

Page 15: Generics - lrde.epita.fr

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:I data abstraction (encapsulation)I iterators (well, generators actually)I type safe variants (oneof)I multiple assignment (x, y, z = f(t))I parameterized modules

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

Notion of parametric polymorphism.

TYLA Generics March 29, 2020 11 / 54

Page 16: Generics - lrde.epita.fr

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:

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

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

Notion of parametric polymorphism.

TYLA Generics March 29, 2020 11 / 54

Page 17: Generics - lrde.epita.fr

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:

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

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

Notion of parametric polymorphism.

TYLA Generics March 29, 2020 11 / 54

Page 18: Generics - lrde.epita.fr

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:

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

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

Notion of parametric polymorphism.

TYLA Generics March 29, 2020 11 / 54

Page 19: Generics - lrde.epita.fr

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 thewhere-clause may be used.

→ Complete compile-time check of parameterized modules.

→ Generation of a single code.

TYLA Generics March 29, 2020 12 / 54

Page 20: Generics - lrde.epita.fr

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 thewhere-clause may be used.

→ Complete compile-time check of parameterized modules.

→ Generation of a single code.

TYLA Generics March 29, 2020 12 / 54

Page 21: Generics - lrde.epita.fr

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 thewhere-clause may be used.

→ Complete compile-time check of parameterized modules.

→ Generation of a single code.

TYLA Generics March 29, 2020 12 / 54

Page 22: Generics - lrde.epita.fr

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 thewhere-clause may be used.

→ Complete compile-time check of parameterized modules.

→ Generation of a single code.

TYLA Generics March 29, 2020 12 / 54

Page 23: Generics - lrde.epita.fr

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 thewhere-clause may be used.

→ Complete compile-time check of parameterized modules.

→ Generation of a single code.

TYLA Generics March 29, 2020 12 / 54

Page 24: Generics - lrde.epita.fr

An example of parameterized module in CLU

s e t = c l u s t e r [ t : type ] i sc r e a t e , member , s i z e , i n s e r t , d e l e t e , e l ements

where t has equa l : p ro c type ( t , t ) r e t u r n s ( boo l )

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

TYLA Generics March 29, 2020 13 / 54

Page 25: Generics - lrde.epita.fr

Implementation of parameterized modules inCLU

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 isrepresented by a (run-time) object.Instantiated modules derived from a non-instantiated objectmodule. Common code is shared.Pros and cons of run- or load-time binding:

Pros No combinatorial explosion due to systematic codegeneration (as with C++ templates).

Cons Lack of static instantiation context means lessopportunities to optimize.

TYLA Generics March 29, 2020 14 / 54

Page 26: Generics - lrde.epita.fr

Implementation of parameterized modules inCLU

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 isrepresented by a (run-time) object.Instantiated modules derived from a non-instantiated objectmodule. Common code is shared.Pros and cons of run- or load-time binding:

Pros No combinatorial explosion due to systematic codegeneration (as with C++ templates).

Cons Lack of static instantiation context means lessopportunities to optimize.

TYLA Generics March 29, 2020 14 / 54

Page 27: Generics - lrde.epita.fr

Implementation of parameterized modules inCLU

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 isrepresented by a (run-time) object.Instantiated modules derived from a non-instantiated objectmodule. Common code is shared.Pros and cons of run- or load-time binding:

Pros No combinatorial explosion due to systematic codegeneration (as with C++ templates).

Cons Lack of static instantiation context means lessopportunities to optimize.

TYLA Generics March 29, 2020 14 / 54

Page 28: Generics - lrde.epita.fr

Implementation of parameterized modules inCLU

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 isrepresented by a (run-time) object.

Instantiated modules derived from a non-instantiated objectmodule. Common code is shared.Pros and cons of run- or load-time binding:

Pros No combinatorial explosion due to systematic codegeneration (as with C++ templates).

Cons Lack of static instantiation context means lessopportunities to optimize.

TYLA Generics March 29, 2020 14 / 54

Page 29: Generics - lrde.epita.fr

Implementation of parameterized modules inCLU

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 isrepresented by a (run-time) object.Instantiated modules derived from a non-instantiated objectmodule. Common code is shared.

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

Pros No combinatorial explosion due to systematic codegeneration (as with C++ templates).

Cons Lack of static instantiation context means lessopportunities to optimize.

TYLA Generics March 29, 2020 14 / 54

Page 30: Generics - lrde.epita.fr

Implementation of parameterized modules inCLU

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 isrepresented by a (run-time) object.Instantiated modules derived from a non-instantiated objectmodule. Common code is shared.Pros and cons of run- or load-time binding:

Pros No combinatorial explosion due to systematic codegeneration (as with C++ templates).

Cons Lack of static instantiation context means lessopportunities to optimize.

TYLA Generics March 29, 2020 14 / 54

Page 31: Generics - lrde.epita.fr

Implementation of parameterized modules inCLU

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 isrepresented by a (run-time) object.Instantiated modules derived from a non-instantiated objectmodule. 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 lessopportunities to optimize.

TYLA Generics March 29, 2020 14 / 54

Page 32: Generics - lrde.epita.fr

Implementation of parameterized modules inCLU

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 isrepresented by a (run-time) object.Instantiated modules derived from a non-instantiated objectmodule. 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

opportunities to optimize.

TYLA Generics March 29, 2020 14 / 54

Page 33: Generics - lrde.epita.fr

TYLA Generics March 29, 2020 15 / 54

Page 34: Generics - lrde.epita.fr

Table of Contents

1 Some definitions

2 Some historyCLUAda 83C++

3 Some Paradigms

TYLA Generics March 29, 2020 16 / 54

Page 35: Generics - lrde.epita.fr

Genericity in Ada 83

Introduced with the generic keywordgeneric

type T is private ;procedure swap ( x , y : in out T ) is

t : Tbegin

t : = x ; x : = y ; y : = t ;end swap ;

-- Explicit instantiations.procedure i n t swap is new swap ( INTEGER ) ;procedure s t r 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

Page 36: Generics - lrde.epita.fr

Generic packages in Ada 83

generictype T is private ;

package STACKS istype STACK ( s i z e : POSITIVE ) is

recordspace : 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

Page 37: Generics - lrde.epita.fr

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 isbegin

if x <= y thenreturn x ;

elsereturn y ;

end if ;end minimum ;

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

TYLA Generics March 29, 2020 19 / 54

Page 38: Generics - lrde.epita.fr

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 isbegin

if x <= y thenreturn x ;

elsereturn y ;

end if ;end minimum ;

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

TYLA Generics March 29, 2020 19 / 54

Page 39: Generics - lrde.epita.fr

Constrained Genericity in Ada 83: Instantiation

Instantiation can be fully qualifiedfunction T1 minimum is new minimum ( T1 , T 1 l e ) ;

or 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

Page 40: Generics - lrde.epita.fr

Constrained Genericity in Ada 83: Instantiation

Instantiation can be fully qualifiedfunction T1 minimum is new minimum ( T1 , T 1 l e ) ;

or 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

Page 41: Generics - lrde.epita.fr

More Genericity Examples in Ada 83

Interface (“specification”):-- matrices.adageneric

type T is private ;z e r o : T ;un i t y : T ;with function ” + ” ( a , b : T ) return T is <>;with function ” ∗ ” ( a , b : T ) return T is <>;

package MATRICES istype 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

Page 42: Generics - lrde.epita.fr

More Genericity Examples in Ada 83

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

package BOOL MATRICES isnew MATRICES (BOOLEAN, f a l s e , t rue , ” or ” , ” and ” ) ;

TYLA Generics March 29, 2020 22 / 54

Page 43: Generics - lrde.epita.fr

More Genericity Examples in Ada 83

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

package BOOL MATRICES isnew MATRICES (BOOLEAN, f a l s e , t rue , ” or ” , ” and ” ) ;

TYLA Generics March 29, 2020 22 / 54

Page 44: Generics - lrde.epita.fr

More Genericity Examples in Ada 83Implementation (“body”):-- matrices.adbpackage body MATRICES is

function ” ∗ ” (m1 , m2 : MATRIX ) isr e s u l t : MATRIX (m1 ’ l i n e s , m2 ’ columns )

beginif 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 ) loopr e s u l t ( i , j ) : = z e r o ;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

Page 45: Generics - lrde.epita.fr

Table of Contents

1 Some definitions

2 Some historyCLUAda 83C++

3 Some Paradigms

TYLA Generics March 29, 2020 24 / 54

Page 46: Generics - lrde.epita.fr

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:

I Poor error messagesreferring to the code wri�en by cpp, not by the programmer.

I Need to instantiate templates once per compile unit, manually.I No support for recurrence.

TYLA Generics March 29, 2020 25 / 54

Page 47: Generics - lrde.epita.fr

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:

I Poor error messagesreferring to the code wri�en by cpp, not by the programmer.

I Need to instantiate templates once per compile unit, manually.I No support for recurrence.

TYLA Generics March 29, 2020 25 / 54

Page 48: Generics - lrde.epita.fr

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:

I Poor error messagesreferring to the code wri�en by cpp, not by the programmer.

I Need to instantiate templates once per compile unit, manually.I No support for recurrence.

TYLA Generics March 29, 2020 25 / 54

Page 49: Generics - lrde.epita.fr

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:

I Poor error messagesreferring to the code wri�en by cpp, not by the programmer.

I Need to instantiate templates once per compile unit, manually.I No support for recurrence.

TYLA Generics March 29, 2020 25 / 54

Page 50: Generics - lrde.epita.fr

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:

I Poor error messagesreferring to the code wri�en by cpp, not by the programmer.

I Need to instantiate templates once per compile unit, manually.

I No support for recurrence.

TYLA Generics March 29, 2020 25 / 54

Page 51: Generics - lrde.epita.fr

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:

I Poor error messagesreferring to the code wri�en by cpp, not by the programmer.

I Need to instantiate templates once per compile unit, manually.I No support for recurrence.

TYLA Generics March 29, 2020 25 / 54

Page 52: Generics - lrde.epita.fr

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 v a l u e t y p e ; \VECTOR( T ) ( ) { /* ... */ } \VECTOR( T ) (int i ) { /* ... */ } \v a l u e t y p e& 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

Page 53: Generics - lrde.epita.fr

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 oftemplates definitions.

A powerful system allowing metaprogramming techniques(though not designed for that in the first place!)

TYLA Generics March 29, 2020 27 / 54

Page 54: Generics - lrde.epita.fr

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 oftemplates definitions.

A powerful system allowing metaprogramming techniques(though not designed for that in the first place!)

TYLA Generics March 29, 2020 27 / 54

Page 55: Generics - lrde.epita.fr

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 oftemplates definitions.

A powerful system allowing metaprogramming techniques(though not designed for that in the first place!)

TYLA Generics March 29, 2020 27 / 54

Page 56: Generics - lrde.epita.fr

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 oftemplates definitions.

A powerful system allowing metaprogramming techniques(though not designed for that in the first place!)

TYLA Generics March 29, 2020 27 / 54

Page 57: Generics - lrde.epita.fr

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 oftemplates definitions.

A powerful system allowing metaprogramming techniques(though not designed for that in the first place!)

TYLA Generics March 29, 2020 27 / 54

Page 58: Generics - lrde.epita.fr

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 oftemplates definitions.

A powerful system allowing metaprogramming techniques(though not designed for that in the first place!)

TYLA Generics March 29, 2020 27 / 54

Page 59: Generics - lrde.epita.fr

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 oftemplates definitions.

A powerful system allowing metaprogramming techniques(though not designed for that in the first place!)

TYLA Generics March 29, 2020 27 / 54

Page 60: Generics - lrde.epita.fr

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 oftemplates definitions.

A powerful system allowing metaprogramming techniques(though not designed for that in the first place!)

TYLA Generics March 29, 2020 27 / 54

Page 61: Generics - lrde.epita.fr

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 oftemplates definitions.

A powerful system allowing metaprogramming techniques(though not designed for that in the first place!)

TYLA Generics March 29, 2020 27 / 54

Page 62: Generics - lrde.epita.fr

Class Templates

template <typename T>class v e c t o r {public :

typedef T v a l u e t y p e ;v e c t o r ( ) { /* ... */ }v e c t o r (int i ) { /* ... */ }v a l u e t y p e& operator [ ] (int i ) { /* ... */ }/* ... */} ;

// No need for explicit template instantiations.

int main ( ) {vec to r<int> v i ;v e c t o r<long> v l ;

}

TYLA Generics March 29, 2020 28 / 54

Page 63: Generics - lrde.epita.fr

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 inmost uses cases (through fonctor).Ei�el does not feature generic function at all.

Java and C-sharp provide only generic member functions.

TYLA Generics March 29, 2020 29 / 54

Page 64: Generics - lrde.epita.fr

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 inmost uses cases (through fonctor).

Ei�el does not feature generic function at all.

Java and C-sharp provide only generic member functions.

TYLA Generics March 29, 2020 29 / 54

Page 65: Generics - lrde.epita.fr

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 inmost uses cases (through fonctor).Ei�el does not feature generic function at all.

Java and C-sharp provide only generic member functions.

TYLA Generics March 29, 2020 29 / 54

Page 66: Generics - lrde.epita.fr

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 inmost uses cases (through fonctor).Ei�el does not feature generic function at all.

Java and C-sharp provide only generic member functions.

TYLA Generics March 29, 2020 29 / 54

Page 67: Generics - lrde.epita.fr

Specialization of Template Definitions

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

Motivation: provide (harder,) be�er, faster, strongerimplementations for a given template class or function.

Example: boolean vector has its own definition, di�erent fromtype T vector

Mechanism close to function overloading in spirit, but distinct.

TYLA Generics March 29, 2020 30 / 54

Page 68: Generics - lrde.epita.fr

Specialization of Template Definitions

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

Motivation: provide (harder,) be�er, faster, strongerimplementations for a given template class or function.

Example: boolean vector has its own definition, di�erent fromtype T vector

Mechanism close to function overloading in spirit, but distinct.

TYLA Generics March 29, 2020 30 / 54

Page 69: Generics - lrde.epita.fr

Specialization of Template Definitions

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

Motivation: provide (harder,) be�er, faster, strongerimplementations for a given template class or function.

Example: boolean vector has its own definition, di�erent fromtype T vector

Mechanism close to function overloading in spirit, but distinct.

TYLA Generics March 29, 2020 30 / 54

Page 70: Generics - lrde.epita.fr

Specialization of Template Definitions

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

Motivation: provide (harder,) be�er, faster, strongerimplementations for a given template class or function.

Example: boolean vector has its own definition, di�erent fromtype T vector

Mechanism close to function overloading in spirit, but distinct.

TYLA Generics March 29, 2020 30 / 54

Page 71: Generics - lrde.epita.fr

Alexander Alexandrovich Stepanov (Nov. 16,1950)

TYLA Generics March 29, 2020 31 / 54

Page 72: Generics - lrde.epita.fr

Alexander Alexandrovich Stepanov (Nov. 16,1950)

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

TYLA Generics March 29, 2020 32 / 54

Page 73: Generics - lrde.epita.fr

The Standard Template Library (STL)

A library of containers, iterators, fundamental algorithms andtools, 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 semanticrequirements over one (or several) types.

But the language does not enforce them.

Initially planned as a language extension in the C++11/14/17standard. . .

. . . but abandonned shortly before the standardization. :-(

TYLA Generics March 29, 2020 33 / 54

Page 74: Generics - lrde.epita.fr

The Standard Template Library (STL)

A library of containers, iterators, fundamental algorithms andtools, 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 semanticrequirements over one (or several) types.

But the language does not enforce them.

Initially planned as a language extension in the C++11/14/17standard. . .

. . . but abandonned shortly before the standardization. :-(

TYLA Generics March 29, 2020 33 / 54

Page 75: Generics - lrde.epita.fr

The Standard Template Library (STL)

A library of containers, iterators, fundamental algorithms andtools, 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 semanticrequirements over one (or several) types.

But the language does not enforce them.

Initially planned as a language extension in the C++11/14/17standard. . .

. . . but abandonned shortly before the standardization. :-(

TYLA Generics March 29, 2020 33 / 54

Page 76: Generics - lrde.epita.fr

The Standard Template Library (STL)

A library of containers, iterators, fundamental algorithms andtools, 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 semanticrequirements over one (or several) types.

But the language does not enforce them.

Initially planned as a language extension in the C++11/14/17standard. . .

. . . but abandonned shortly before the standardization. :-(

TYLA Generics March 29, 2020 33 / 54

Page 77: Generics - lrde.epita.fr

The Standard Template Library (STL)

A library of containers, iterators, fundamental algorithms andtools, 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 semanticrequirements over one (or several) types.

But the language does not enforce them.

Initially planned as a language extension in the C++11/14/17standard. . .

. . . but abandonned shortly before the standardization. :-(

TYLA Generics March 29, 2020 33 / 54

Page 78: Generics - lrde.epita.fr

The Standard Template Library (STL)

A library of containers, iterators, fundamental algorithms andtools, 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 semanticrequirements over one (or several) types.

But the language does not enforce them.

Initially planned as a language extension in the C++11/14/17standard. . .

. . . but abandonned shortly before the standardization. :-(

TYLA Generics March 29, 2020 33 / 54

Page 79: Generics - lrde.epita.fr

The Standard Template Library (STL)

A library of containers, iterators, fundamental algorithms andtools, 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 semanticrequirements over one (or several) types.

But the language does not enforce them.

Initially planned as a language extension in the C++11/14/17standard. . .

. . . but abandonned shortly before the standardization. :-(

TYLA Generics March 29, 2020 33 / 54

Page 80: Generics - lrde.epita.fr

Example

template<typename T>concept Hashable = r e q u i r e s ( T a ) {

{ s td : : hash<T>{}(a ) } −> s td : : c o n v e r t i b l e t o<s td : : 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

Page 81: Generics - lrde.epita.fr

Table of Contents

1 Some definitions

2 Some history

3 Some Paradigms

TYLA Generics March 29, 2020 35 / 54

Page 82: Generics - lrde.epita.fr

Problem Statement

How to implement Generics?

TYLA Generics March 29, 2020 36 / 54

Page 83: Generics - lrde.epita.fr

Table of Contents

1 Some definitions

2 Some history

3 Some ParadigmsBoxingMonomorphization

TYLA Generics March 29, 2020 37 / 54

Page 84: Generics - lrde.epita.fr

Boxing: main idea

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

The data structure only handles pointers

Pointers to di�erent 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

Page 85: Generics - lrde.epita.fr

Boxing: main idea

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

The data structure only handles pointers

Pointers to di�erent 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

Page 86: Generics - lrde.epita.fr

Boxing: main idea

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

The data structure only handles pointers

Pointers to di�erent 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

Page 87: Generics - lrde.epita.fr

Boxing: main idea

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

The data structure only handles pointers

Pointers to di�erent 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

Page 88: Generics - lrde.epita.fr

Go example

type Stack struct {v a l u e s [ ] i n t e r f a c e {}

}

func ( t h i s ∗ Stack ) Push ( va l u e i n t e r f a c e {} ) {t h i s . v a l u e s = append ( t h i s . va lue s , v a l u e )

}

TYLA Generics March 29, 2020 39 / 54

Page 89: Generics - lrde.epita.fr

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 pu�ing elements of

di�erent types into the structure

TYLA Generics March 29, 2020 40 / 54

Page 90: Generics - lrde.epita.fr

Type-erased boxed generics

Ideaadd generics functionality to the type system

BUT use the basic boxing method exactly as before at runtime.

→ This approach is o�en called type erasure, because thetypes in the generics system are ”erased” and all become thesame 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

Page 91: Generics - lrde.epita.fr

Type-erased boxed generics

Ideaadd generics functionality to the type system

BUT use the basic boxing method exactly as before at runtime.

→ This approach is o�en called type erasure, because thetypes in the generics system are ”erased” and all become thesame 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

Page 92: Generics - lrde.epita.fr

Type-erased boxed generics

Ideaadd generics functionality to the type system

BUT use the basic boxing method exactly as before at runtime.

→ This approach is o�en called type erasure, because thetypes in the generics system are ”erased” and all become thesame 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

Page 93: Generics - lrde.epita.fr

Java Example

Without Generics (pre Java 4.0)Throws java.lang.ClassCastExceptionL 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 IntegerI n t e g e r i = ( I n t e g e r ) v . ge t ( 0 ) ; // Run time error

With GenericsFails at compile timeL i s t<S t r i ng> v = new Ar r ayL i s t<S t r i ng > ( ) ;v . add ( ” t e s t ” ) ;I n t e g e r i = v . ge t ( 0 ) ; // (type error) compilation-time error

TYLA Generics March 29, 2020 42 / 54

Page 94: Generics - lrde.epita.fr

Java Example

Without Generics (pre Java 4.0)Throws java.lang.ClassCastExceptionL 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 IntegerI n t e g e r i = ( I n t e g e r ) v . ge t ( 0 ) ; // Run time error

With GenericsFails at compile timeL i s t<S t r i ng> v = new Ar r ayL i s t<S t r i ng > ( ) ;v . add ( ” t e s t ” ) ;I n t e g e r i = v . ge t ( 0 ) ; // (type error) compilation-time error

TYLA Generics March 29, 2020 42 / 54

Page 95: Generics - lrde.epita.fr

Todo Wildcard?

TYLA Generics March 29, 2020 43 / 54

Page 96: Generics - lrde.epita.fr

Inferred boxed generics with a uniformrepresentation

Problem with simple boxingIn the previous approach, generic data structures cannot

hold primitive types!

Ocaml’s SolutionUniform representation where there are no primitive types that

requires an additional boxing allocation !

TYLA Generics March 29, 2020 44 / 54

Page 97: Generics - lrde.epita.fr

Inferred boxed generics with a uniformrepresentation

Problem with simple boxingIn the previous approach, generic data structures cannot

hold primitive types!

Ocaml’s SolutionUniform representation where there are no primitive types that

requires an additional boxing allocation !

TYLA Generics March 29, 2020 44 / 54

Page 98: Generics - lrde.epita.fr

Inferred boxed generics with a uniformrepresentation (cont’d)Ocaml’s apporach:

no additional boxing allocation (like int needing to be turnedinto an Integer

everything is either already boxed or represented by apointer-sized integer=⇒ everything is one machine word

Problem :garbage collector needs to distinguish pointers fromintegers… there is a reserved bit in machine word

I integer size is only 31/63 bitsI pointer size is only 31/63 bitsI the 32/64 bit for integer is 1I the 32/64 bit for valid aligned pointers is 0

TYLA Generics March 29, 2020 45 / 54

Page 99: Generics - lrde.epita.fr

Inferred boxed generics with a uniformrepresentation (cont’d)Ocaml’s apporach:

no additional boxing allocation (like int needing to be turnedinto an Integer

everything is either already boxed or represented by apointer-sized integer=⇒ everything is one machine word

Problem :garbage collector needs to distinguish pointers fromintegers… there is a reserved bit in machine word

I integer size is only 31/63 bitsI pointer size is only 31/63 bitsI the 32/64 bit for integer is 1I the 32/64 bit for valid aligned pointers is 0

TYLA Generics March 29, 2020 45 / 54

Page 100: Generics - lrde.epita.fr

Inferred boxed generics with a uniformrepresentation (cont’d)Ocaml’s apporach:

no additional boxing allocation (like int needing to be turnedinto an Integer

everything is either already boxed or represented by apointer-sized integer=⇒ everything is one machine word

Problem :garbage collector needs to distinguish pointers fromintegers… there is a reserved bit in machine word

I integer size is only 31/63 bitsI pointer size is only 31/63 bitsI the 32/64 bit for integer is 1I the 32/64 bit for valid aligned pointers is 0

TYLA Generics March 29, 2020 45 / 54

Page 101: Generics - lrde.epita.fr

Inferred boxed generics with a uniformrepresentation (cont’d)Ocaml’s apporach:

no additional boxing allocation (like int needing to be turnedinto an Integer

everything is either already boxed or represented by apointer-sized integer=⇒ everything is one machine word

Problem :garbage collector needs to distinguish pointers fromintegers

… there is a reserved bit in machine word

I integer size is only 31/63 bitsI pointer size is only 31/63 bitsI the 32/64 bit for integer is 1I the 32/64 bit for valid aligned pointers is 0

TYLA Generics March 29, 2020 45 / 54

Page 102: Generics - lrde.epita.fr

Inferred boxed generics with a uniformrepresentation (cont’d)Ocaml’s apporach:

no additional boxing allocation (like int needing to be turnedinto an Integer

everything is either already boxed or represented by apointer-sized integer=⇒ everything is one machine word

Problem :garbage collector needs to distinguish pointers fromintegers… there is a reserved bit in machine word

I integer size is only 31/63 bitsI pointer size is only 31/63 bitsI the 32/64 bit for integer is 1I the 32/64 bit for valid aligned pointers is 0

TYLA Generics March 29, 2020 45 / 54

Page 103: Generics - lrde.epita.fr

Inferred boxed generics with a uniformrepresentation (cont’d)Ocaml’s apporach:

no additional boxing allocation (like int needing to be turnedinto an Integer

everything is either already boxed or represented by apointer-sized integer=⇒ everything is one machine word

Problem :garbage collector needs to distinguish pointers fromintegers… there is a reserved bit in machine word

I integer size is only 31/63 bitsI pointer size is only 31/63 bits

I the 32/64 bit for integer is 1I the 32/64 bit for valid aligned pointers is 0

TYLA Generics March 29, 2020 45 / 54

Page 104: Generics - lrde.epita.fr

Inferred boxed generics with a uniformrepresentation (cont’d)Ocaml’s apporach:

no additional boxing allocation (like int needing to be turnedinto an Integer

everything is either already boxed or represented by apointer-sized integer=⇒ everything is one machine word

Problem :garbage collector needs to distinguish pointers fromintegers… there is a reserved bit in machine word

I integer size is only 31/63 bitsI pointer size is only 31/63 bitsI the 32/64 bit for integer is 1I the 32/64 bit for valid aligned pointers is 0

TYLA Generics March 29, 2020 45 / 54

Page 105: Generics - lrde.epita.fr

Introducing InterfacesLimitation with boxing

The boxed types are completely opaque!(generic sorting function need some extra functionality,

like a type-specific comparison function.)

Two families of solutionsDictionary passing: Haskell (type class) and Ocaml (modules)

I Pass a table of the required function pointers along to genericfunctions that need them

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

Interface vtables: Rust (dyn traits) & Golang (interface)I When casting to interface type it creates a wrapperI 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

Page 106: Generics - lrde.epita.fr

Introducing InterfacesLimitation with boxing

The boxed types are completely opaque!(generic sorting function need some extra functionality,

like a type-specific comparison function.)

Two families of solutionsDictionary passing: Haskell (type class) and Ocaml (modules)

I Pass a table of the required function pointers along to genericfunctions that need them

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

Interface vtables: Rust (dyn traits) & Golang (interface)I When casting to interface type it creates a wrapperI 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

Page 107: Generics - lrde.epita.fr

A note on Dictionnary passingSwi� Witness Tables

Use dictionary passing and put the size of types and how tomove, copy and free them into the tables,

Provide all the information required to work with any type in auniform way

…without boxing them.

→ swi� uses monomorphization (later in lecture)

Going furtherHave 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

Page 108: Generics - lrde.epita.fr

A note on Dictionnary passingSwi� Witness Tables

Use dictionary passing and put the size of types and how tomove, copy and free them into the tables,

Provide all the information required to work with any type in auniform way

…without boxing them.

→ swi� uses monomorphization (later in lecture)

Going furtherHave 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

Page 109: Generics - lrde.epita.fr

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 entirelywith these object vtables

→ construct new interface types with indirection is no longerrequired.

ReflectionWith vtables, it�s not di�icult to have reflection since the compilercan generates tables of other type information like field names, typesand locations

TYLA Generics March 29, 2020 48 / 54

Page 110: Generics - lrde.epita.fr

From interface vtables to Reflection (2/3)

Reflection is the ability of a program to examine, introspect, andmodify 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 areused for everything.

TYLA Generics March 29, 2020 49 / 54

Page 111: Generics - lrde.epita.fr

From interface vtables to Reflection (2/3)

Reflection is the ability of a program to examine, introspect, andmodify 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 areused for everything.

TYLA Generics March 29, 2020 49 / 54

Page 112: Generics - lrde.epita.fr

From interface vtables to Reflection (3/3)

Introspection: ability to observe and therefore reason about itsown state.public boolean c l a s s e q u a l ( Ob jec t o1 , Ob jec t o2 ){

Cla s s c1 , c2 ; c1 = o1 . g e tC l a s s ( ) ;c2 = o2 . g e tC l a s s ( ) ; return ( c1 == c2 ) ;

}

Intercession: ability to modify its execution state or alter its owninterpretationCla s s c = ob j . g e tC l a s s ( ) ;Ob jec t o = c . newIns tance ( ) ;

S t r i n g s = ” FooBar ” .C l a s s c = C la s s . forName ( s ) ;Ob jec t o = c . newIns tance ( ) ;

TYLA Generics March 29, 2020 50 / 54

Page 113: Generics - lrde.epita.fr

Table of Contents

1 Some definitions

2 Some history

3 Some ParadigmsBoxingMonomorphization

TYLA Generics March 29, 2020 51 / 54

Page 114: Generics - lrde.epita.fr

Monomorphization

The monomorphization approach outputs multiple versions of thecode for each type we want to use it with

C++ template

Rust procedural macros

D

TYLA Generics March 29, 2020 52 / 54

Page 115: Generics - lrde.epita.fr

MetaprogrammingWriting programs that write programs.

Some language a clean way of doing code generation

Syntax tree macros: the ability to produce AST types in macroswri�en in the language

Template: reason about types and type substitution

Compile time functions

TYLA Generics March 29, 2020 53 / 54

Page 116: Generics - lrde.epita.fr

TYLA Generics March 29, 2020 54 / 54