SLIDE 1
Algebraic Specialization of Generic Functions for Recursive Types
By: Alcino Cunha, Hugo Pacheco
1 Presented by: Joseph Tsun Kiet Man
SLIDE 2 Index
- 1. Introduction
- 1. Problem
- 2. Solutions
- 2. Specialization: Non-recursive Types
- 1. Theory
- 2. Example
- 3. Specialization: Recursive Types
- 1. Theory
- 2. Example
- 4. Concluding remarks
- 5. Summary
2
SLIDE 3
Introduction | Problem
IMDB Movie Actor Year Title Director Review Box Office Name Played Year Title Role Award
Given the following data structure:
Country Value 3
SLIDE 4
Introduction | Problem
4
SLIDE 5 Introduction | Problem
𝑑𝑑𝑑𝑑𝑑 = 𝑓𝑓𝑓𝑓𝑓𝑑𝑓𝑓𝑑𝑓 (𝑛𝑛𝑅𝑆𝑆𝑆𝑆𝑆𝑆 𝑡𝑓𝑡𝑓) wher ere 𝑡𝑓𝑡𝑓 𝑆𝑓𝑓𝑓𝑓𝑆 𝑓 = 𝑚𝑓𝑑𝑓𝑑𝑓 𝑓
Performance loss due to:
- Run-time checks to determine at each node whether to
apply specific / generic behaviour.
- Traversal through whole data structure.
5
SLIDE 6 Introduction | Solutions
How to solve the inefficiencies?
- Restricting the power of traversal combinators.
- Offsets run-time checks to compile-time.
- Specialization
Generic function Transformation Specialized function 𝑑𝑑𝑑𝑑𝑑′ = 𝑡𝑑𝑛 ∘ 𝑛𝑛𝑛 𝑡𝑑𝑛 ∘ 𝑛𝑛𝑛 𝑡𝑓𝑡𝑓 ∘ 𝑓𝑓𝑓𝑓𝑓𝑆𝑡 ∘ 𝑛𝑑𝑓𝑓𝑓𝑡 wher ere 𝑛𝑑𝑓𝑓𝑓𝑡 𝐽𝑛𝐽𝐽 𝑛 𝑛 = 𝑛 𝑓𝑓𝑓𝑓𝑓𝑆𝑡 𝑆𝑓𝑓𝑓𝑓𝑆 𝑓 = 𝑓 6
SLIDE 7
Specialization: Non-recursive Types
Theory
Recapitulation SYB:
7
SLIDE 8
Specialization: Non-recursive Types
Theory
Recapitulation SYB:
8
SLIDE 9
Specialization: Non-recursive Types
Theory
Point-free combinators and functions:
9
SLIDE 10
Specialization: Non-recursive Types
Theory
Algebraic Transformation Laws:
10
SLIDE 11
Specialization: Non-recursive Types
Theory
Algebraic Query Laws:
11
SLIDE 12
Specialization: Non-recursive Types
Example
𝑑𝑑𝑑𝑑𝑑 = 𝑓𝑓𝑓𝑓𝑓𝑑𝑓𝑓𝑑𝑓 (𝑛𝑛𝑅𝑆𝑆𝑆𝑆𝑆𝑆 𝑡𝑓𝑡𝑓) wher ere 𝑡𝑓𝑡𝑓 𝑆𝑓𝑓𝑓𝑓𝑆 𝑓 = 𝑚𝑓𝑑𝑓𝑑𝑓 𝑓 Rewrite: 12
SLIDE 13
Specialization: Non-recursive Types
Example
𝑑𝑑𝑑𝑑𝑑 = 𝑓𝑓𝑓𝑓𝑓𝑑𝑓𝑓𝑑𝑓 (𝑛𝑛𝑅𝑆𝑆𝑆𝑆𝑆𝑆 𝑡𝑓𝑡𝑓) wher ere 𝑡𝑓𝑡𝑓 𝑆𝑓𝑓𝑓𝑓𝑆 𝑓 = 𝑚𝑓𝑑𝑓𝑑𝑓 𝑓 Rewrite: 13
SLIDE 14
Specialization: Non-recursive Types
Example
𝑑𝑑𝑑𝑑𝑑 = 𝑓𝑓𝑓𝑓𝑓𝑑𝑓𝑓𝑑𝑓 (𝑛𝑛𝑅𝑆𝑆𝑆𝑆𝑆𝑆 𝑡𝑓𝑡𝑓) wher ere 𝑡𝑓𝑡𝑓 𝑆𝑓𝑓𝑓𝑓𝑆 𝑓 = 𝑚𝑓𝑑𝑓𝑑𝑓 𝑓 Rewrite: 14
SLIDE 15
Specialization: Non-recursive Types
Example
𝑑𝑑𝑑𝑑𝑑 = 𝑓𝑓𝑓𝑓𝑓𝑑𝑓𝑓𝑑𝑓 (𝑛𝑛𝑅𝑆𝑆𝑆𝑆𝑆𝑆 𝑡𝑓𝑡𝑓) wher ere 𝑡𝑓𝑡𝑓 𝑆𝑓𝑓𝑓𝑓𝑆 𝑓 = 𝑚𝑓𝑑𝑓𝑑𝑓 𝑓 Rewrite: 15
SLIDE 16
Specialization: Recursive Types
Theory
Apply same concept to catamorphisms and paramorphisms:
16
SLIDE 17
Specialization: Recursive Types
Theory
Catamorphisms: functions defined by iteration.
17
SLIDE 18
Specialization: Recursive Types
Theory
Paramorphisms: all functions that can be defined by primitive recursion.
18
SLIDE 19 Specialization: Recursive Types
Theory
Idea:
- Rewrite the everywhere function.
- Rewrite the everything function
19
SLIDE 20
Specialization: Recursive Types
Theory
Everywhere function redefined:
20
SLIDE 21
Specialization: Recursive Types
Theory
Everywhere function redefined:
21
SLIDE 22
Specialization: Recursive Types
Theory
Recap - Algebraic Transformation Laws:
22
SLIDE 23
Specialization: Recursive Types
Theory
New additional laws for catamorphisms and paramorphisms. 23
SLIDE 24
Specialization: Recursive Types
Example
Function 24
SLIDE 25
Specialization: Recursive Types
Example
Function 25
SLIDE 26
Specialization: Recursive Types
Application
26
SLIDE 27
Specialization: Performance
27
SLIDE 28 Specialization: Improvements
Improvements:
- Support for non-regular data types (e.g. nested).
- Added symbolic evaluation (intermediate structures are
completely removed during optimization).
28
SLIDE 29 Specialization: Conclusion
- Specialization comes near handwritten code.
- Specialized code can be optimized by compiler
- ptimization methods.
- Specialization by rewriting generic function to
specialized functions.
29