2. Key Idea: Generalize Abstractions
Generalize an abstraction because removing non-essential information
clarifies its independent meaning.
Reuse is Accidental.
3. Parametricity: Definition
parametricity is an abstract uniformity property enjoyed by parametrically
polymorphic functions, which captures the intuition that all instances of a
polymorphic function act the same way.
4. Types are documentation
Parametricity is an efficient and reliable tool to assist code-readability to
assist non-trivial software in a team environment.
A function is specified by its parametric type and its tests.
Parametric Type: proof-positive
Test: proof-negative
7. What does the function do?
def add10(n: Int): Int
Theorem: it returns one of the 2^32 values of int.
Otherwise, it would not have compiled.
8. What does the function do?
def function(elements: List[Int]): List[Int]
9. What does the function do?
def function(elements: List[Int]): List[Int]
The number of potential implementations is HUGE.
-> It can return list of prime numbers.
-> It can add 17 to every 11th element.
-> It can return empty list.
-> It can concatenate list with itself.
10. What does the function do?
def function(elements: List[Int]): List[Int]
What if we make it polymorphic?
11. What does the function do?
def function(elements: List[Int]): List[Int]
i.e. Abstract over type of list.
12. What does the function do?
def function[A](elements: List[A]): List[A]
Theorem:
Every element in output list comes from input list.
Otherwise, it would not have compiled.
13. What does the function do?
def function[A](elements: List[A]): List[A]
Theorem:
Every element in output list comes from input list.
Otherwise, it would not have compiled.
By adding freedom in types, we have constrained the implementations.
14. Parametricity works on language subset
These things break parametricity:
Null
Exception
isInstanceOf
asInstanceOf
Side effects
toString, hashCode, equals
Notify/wait
classOf
recursion