2. Type Class
Type system construct that supports ad-hoc
polymorphism, achieved by adding constraints
to type variables in parametrically
polymorphic types
3. Type Class
Type system construct that supports ad-hoc
polymorphism, achieved by adding constraints
to type variables in parametrically
polymorphic types
4. Type Class
Type system construct that supports ad-hoc
polymorphism, achieved by adding constraints
to type variables in parametrically
polymorphic types
5. Type Class
Type system construct that supports ad-hoc
polymorphism, achieved by adding constraints
to type variables in parametrically
polymorphic types
6. Type Class
Type system construct that supports ad-hoc
polymorphism, achieved by adding constraints
to type variables in parametrically
polymorphic types
7. pol•y•mor•phism ¦ päli môr fizəm¦
nounthe occurrence of something in several
different forms
New Oxford American Dictionary
8. In particular:
Polymorphism is a programming language
feature that allows values of different data
types to be handled using a uniform interface
9. Several Types of Polymorphism
• Ad-hoc polymorphism
• Inclusion polymorphism
• Parametric polymorphism
• Bounded parametric polymorphism
10. Several Types of Polymorphism
• Ad-hoc polymorphism
– Operator and method overloading
• Inclusion polymorphism
• Parametric polymorphism
• Bounded parametric polymorphism
11. Several Types of Polymorphism
• Ad-hoc polymorphism
• Inclusion polymorphism
– Subtyping
• Parametric polymorphism
• Bounded parametric polymorphism
12. Several Types of Polymorphism
• Ad-hoc polymorphism
• Inclusion polymorphism
• Parametric polymorphism
– Generics
• Bounded parametric polymorphism
13. Several Types of Polymorphism
• Ad-hoc polymorphism
• Inclusion polymorphism
• Parametric polymorphism
• Bounded parametric polymorphism
– Generics with bounded wildcards
14. Ad-hoc polymorphism
Control moving through one named function is
dispatched to various other functions without
having to specify the exact function being
called
19. Parametric polymorphism
A function or a data type can be written
generically so that it can handle values
identically without depending on their type
24. Type Class
Type system construct that supports ad-hoc
polymorphism, achieved by adding constraints
to type variables in parametrically
polymorphic types
25. Type Class
Type system construct that supports ad-hoc
polymorphism, achieved by adding constraints
to type variables in parametrically
polymorphic types
26. Type Class
Type system construct that supports ad-hoc
polymorphism, achieved by adding constraints
to type variables in parametrically
polymorphic types
27. Type Class
Type system construct that supports ad-hoc
polymorphism, achieved by adding constraints
to type variables in parametrically
polymorphic types
34. Constraint on type variable
member :: (Eq a) ⇒ a → [a] → bool
member y [] = false
member y (x:xs) = (eq x y) || member y xs
35. Type Classes vs. Interfaces?
Similar, yet different:
• Implementation is separate
• Subtyping is not entailed
• Static dispatch (ad-hoc polymorphism) vs.
dynamic single dispatch