3. Parametricity
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. Parametricity
def sum[A](elements: List[A]): A
It picks any value from the list.
How do we add a possibility of adding the elements?
We go one level down in abstraction, i.e. we add extra information.
def sum[A: Numeric](elements: List[A]): A
We have added information that this function is defined only when A is a Numeric.
If A is not a Numeric, it will not compile.
5. Generic Programming
In generic programming, Typeclass is used to encode a
constraint/requirement/capability on type parameter.
def sum[A: Monoid](elements: List[A]): A
def sort[A: Ord] (xs: List[A]) : List[A]
We can add multiple constraints on types.
def sum[A: Monoid: Numeric](elements: List[A]): A
6. Retroactive Extension
the ability to extend existing software modules with new functionality without
needing to touch or re-compile the original source.
7. Retroactive Extension
class Fruit (name: String)
def sort[A: Ord](xs: List[A]): List[A]
How do we make a generalized algorithm work on a particular type?
How to make sort work on Fruit?
8. Type level programming
Abstract Member Types
Path Dependent Methods
Generalized Constraints