Post on 03-Jul-2020
Programação Genérica Levando a Abstração ao Limite
Fabio Galuppo, M.Sc. http://fabiogaluppo.com e http://simplycpp.com/
fabiogaluppo@acm.org
@FabioGaluppo
Microsoft MVP Visual Studio and Development Technologies https://mvp.microsoft.com/en-us/PublicProfile/9529
http://bit.ly/prog_gen_qconsp_2016
Fabio Razzo Galuppo, M.Sc. Novembro 1973
• Mestrado em Engenharia Elétrica (Universidade Presbiteriana Mackenzie)
– Ciência da Computação - Inteligência Artificial
• Por mais de 10 anos premiado com Microsoft MVP em Visual C++
• Engenheiro de Software (Programador)
• Matemática Aplicada
• Linguagens de programação prediletas: – C++
– F#
– Haskell
• Rock’n’Roll – E boa música em geral
• http://fabiogaluppo.com
• https://twitter.com/FabioGaluppo
• https://github.com/fabiogaluppo
• http://simplycpp.com
Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
Templates e Generics Polimorfismo Paramétrico
https://docs.oracle.com/javase/tutorial/java/generics/why.html
Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
Templates e Generics Polimorfismo Paramétrico e Restrições
https://msdn.microsoft.com/en-us/library/d5x73970.aspx
class Dictionary<TKey,TValue> : IDictionary<TKey,TValue> where TKey: IComparable<TKey> where TValue: IKeyProvider<TKey>, IPersistable, new() { public void Add(TKey key, TValue value) { ... } }
Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
Abstrações Custo Zero • “The aim of C++ is to help in classical systems programming tasks. It supports the use
of light-weight abstraction for resource-constrained and often mission-critical infrastructure applications. The aim is to allow a programmer to work at the highest feasible level of abstraction by providing
• A simple and direct mapping to hardware
• Zero-overhead abstraction mechanisms
• In general, C++ implementations obey the zero-overhead principle: What you don’t use, you don’t pay for” – Bjarne Stroustrup (http://www.stroustrup.com/ETAPS-corrected-draft.pdf)
http://blog.rust-lang.org/2015/05/11/traits.html
Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
https://twitter.com/FabioGaluppo/status/705191871832784896
Quando o contexto é sobre desempenho
• Não use abstração (! ou ?)
ZERO-COST ABSTRACTIONS Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x64
b com valor 299
b com valor 199
otimizado
não otimizado
Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x64
otimizado
Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x64
Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
otimizado
before:25270 14008 47948 28218 52679 29321 20101 7968 31023 36364 62B6 36B8
after :14008 25270 28218 47948 29321 52679 7968 20101 36364 31023
my_swap
Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
otimizado
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x64
No contexto da chamada da função swap_from_array_no_raw_loops, o corpo da função utilizada encontra-se inline
Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
O que é Programação Genérica?
Datatype-Generic Programming (Jeremy Gibbons)
Generic Programming (David Musser, Alexander Stepanov)
Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
Abstraindo do Concreto
Datatype-Generic Programming (Jeremy Gibbons)
Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
Abstraindo do Concreto
Datatype-Generic Programming (Jeremy Gibbons)
ABSTRACTING FROM CONCRETE Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
http://simplycpp.com/2015/12/08/por-quem-os-ponteiros-dobram-estrelando-stdaccumulate/
Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
…f(f(f(f(f(acc, 1),2),3),4),5)…, sum
append
hashing
Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
Então, o que é mesmo Programação Genérica?
http://www.fm2gp.com/slides/FM2GP_Course_Slides_Pt1.pdf
Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
Álgebra Abstrata e Teoria dos Números
http://www.fm2gp.com/slides/FM2GP_Course_Slides_Pt1.pdf
Number Theory: Branch of mathematics that deals with properties of integers, especially divisibility.
https://www.youtube.com/watch?v=ReOQ300AcSU
𝑥𝑛 + 𝑦𝑛 = 𝑧𝑛 [𝑛 > 2]
Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
Álgebra Abstrata e Estruturas Algébricas
http://www.fm2gp.com/slides/FM2GP_Course_Slides_Pt2.pdf
ORDERING Strict Totally and Strict Weak
Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
https://github.com/fabiogaluppo/samples/blob/master/events/qconsp2016/code/cpp/ordering_sample.hpp
Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
Estrutura Algébrica: Grupo
• Grupo implica: • Operação Binária Associativa • Inversa e Cancelamento • Elemento de Identidade
http://www.fm2gp.com/slides/FM2GP_Course_Slides_Pt2.pdf
GROUP https://en.wikipedia.org/wiki/Group_(mathematics)
Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
BINARY ASSOCIATIVE OPERATION
GROUP AXIOMS: • CLOSURE • ASSOCIATIVITY • IDENTITY • INVERTIBILITY
Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
Associatividade
http://www.dicio.com.br/associatividade/
AlexFest: Paul McJones - The Concept of Concept
ASSOCIATIVITY Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
https://github.com/fabiogaluppo/samples/blob/master/events/qconsp2016/code/cpp/associativity_sample.hpp
Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
http://www.fssnip.net/7OM
Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
Estruturas Algébricas
http://www.fm2gp.com/slides/FM2GP_Course_Slides_Pt2.pdf
Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
Abstração, revisitada
http://www.fm2gp.com/slides/FM2GP_Course_Slides_Pt3.pdf
Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
Valores e Tipos
http://www.fm2gp.com/slides/FM2GP_Course_Slides_Pt3.pdf
Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
Objetos
http://www.fm2gp.com/slides/FM2GP_Course_Slides_Pt3.pdf
Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
Concepts
http://www.fm2gp.com/slides/FM2GP_Course_Slides_Pt3.pdf
Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
AlexFest: Paul McJones - The Concept of Concept
The Concept of Concept
Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
O Conceito de Tipo Regular
http://www.fm2gp.com/slides/FM2GP_Course_Slides_Pt3.pdf
• Operations Requirements
• Semantic Requirements
• Space/Time Complexity Requirements • Each required operation must be no worse than linear in the area of the object
REGULAR TYPE
Copy Constructor Copy Assignment Equality (and Inequality) Destruction Default Constructor
Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
Equality/Inequality
Ordering
https://github.com/fabiogaluppo/samples/blob/master/events/qconsp2016/code/cpp/regular_type_sample.hpp
Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
https://github.com/fabiogaluppo/samples/blob/master/events/qconsp2016/code/csharp/RegularTypeSampleProgram.cs
Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
Standard Template Library (STL)
http://www.sgi.com/tech/stl/stl_introduction.html
http://www.cplusplus.com/reference/stl/
http://www.fm2gp.com/slides/FM2GP_Course_Slides_Pt2.pdf
Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
Iterators
http://www.fm2gp.com/slides/FM2GP_Course_Slides_Pt3.pdf
Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
Categoria de Iterators
http://www.cplusplus.com/reference/iterator/
array doubly‐linked list singly‐linked list
input stream (cin)
output stream (cout)
Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
Algoritmos
http://www.dicio.com.br/algoritmo/
http://open-std.org/JTC1/SC22/WG21/docs/papers/2015/n4567.pdf
STL Algorithms
• Compreensível e mais abstraido do que um raw loop
• Mantém side-effects dentro de uma interface bem definida
• Facilita o raciocínio sobre o problema
• Atua em conjunto com iterators ou left-closed interval [𝑏𝑒𝑔𝑖𝑛, 𝑒𝑛𝑑)
Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
Policy-based Design
https://en.wikipedia.org/wiki/Policy-based_design
POLICY-BASED DESIGN Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
https://github.com/fabiogaluppo/samples/blob/master/events/qconsp2016/code/cpp/policy_sample.hpp
Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
Alexander Stepanov
http://www.stepanovpapers.com/
Alexander Stepanov, thank you very much!
https://isocpp.org/blog/2016/01/alex-stepanov
• “Programming is an iterative process: studying useful problems, finding efficient algorithms for them, distilling the concepts underlying the algorithms, and organizing the concepts and algorithms into a coherent mathematical theory. Each new discovery adds to the permanent body of knowledge, but each has its limitations.”
AlexFest: Paul McJones - The Concept of Concept
• “STL is intended to be an example. An example of how you
code, not the beginning, not an end, it's an example.” CppCon 2015: Sean Parent "Better Code: Data Structures"
• “Abstração é essencial. Você não precisa abrir mão dela para alto desempenho, basta fazê-la da forma correta, de preferência com a linguagem de programação certa para isso.” – Fabio Galuppo, QCon SP 2016
Final Thought
Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
Fab
io G
alu
pp
o -
htt
p:/
/mem
ber
.acm
.org
/~fa
bio
galu
pp
o -
fab
ioga
lup
po
@ac
m.o
rg
Se quiser saber mais sobre: C++
Programação Genérica Iterators
Policy-based Design Algoritmos
…
visite:
www.simplycpp.com
http://www.simplycpp.com
Programação Genérica Levando a Abstração ao Limite
Fabio Galuppo, M.Sc. http://fabiogaluppo.com e http://simplycpp.com/
fabiogaluppo@acm.org
@FabioGaluppo
Microsoft MVP Visual Studio and Development Technologies https://mvp.microsoft.com/en-us/PublicProfile/9529
http://bit.ly/prog_gen_qconsp_2016