Type Classes


Wealthfront Engineering
Type Class


Type system construct that supports ad-hoc
 polymorphism, achieved by adding constraints
 to type variables in parametrically
 polymorphic types
Type Class


Type system construct that supports ad-hoc
 polymorphism, achieved by adding constraints
 to type variables in parametrically
 polymorphic types
Type Class


Type system construct that supports ad-hoc
 polymorphism, achieved by adding constraints
 to type variables in parametrically
 polymorphic types
Type Class


Type system construct that supports ad-hoc
 polymorphism, achieved by adding constraints
 to type variables in parametrically
 polymorphic types
Type Class


Type system construct that supports ad-hoc
 polymorphism, achieved by adding constraints
 to type variables in parametrically
 polymorphic types
pol•y•mor•phism            ¦ päli môr fizəm¦



nounthe occurrence of something in several
  different forms	
  




                    New Oxford American Dictionary
In particular:


Polymorphism is a programming language
  feature that allows values of different data
  types to be handled using a uniform interface
Several Types of Polymorphism


•    Ad-hoc polymorphism
•    Inclusion polymorphism
•    Parametric polymorphism
•    Bounded parametric polymorphism
Several Types of Polymorphism


•  Ad-hoc polymorphism
  –  Operator and method overloading
•  Inclusion polymorphism
•  Parametric polymorphism
•  Bounded parametric polymorphism
Several Types of Polymorphism


•  Ad-hoc polymorphism
•  Inclusion polymorphism
  –  Subtyping
•  Parametric polymorphism
•  Bounded parametric polymorphism
Several Types of Polymorphism


•  Ad-hoc polymorphism
•  Inclusion polymorphism
•  Parametric polymorphism
  –  Generics
•  Bounded parametric polymorphism
Several Types of Polymorphism


•    Ad-hoc polymorphism
•    Inclusion polymorphism
•    Parametric polymorphism
•    Bounded parametric polymorphism
     –  Generics with bounded wildcards
Ad-hoc polymorphism


Control moving through one named function is
 dispatched to various other functions without
 having to specify the exact function being
 called
Ad-hoc polymorphism


String greet() {
   return "Hello, World!";
}
Ad-hoc polymorphism


String greet(String name) {
   return "Hello," + name + "!";
}
Ad-hoc polymorphism


String greet(int count) {
   return "Hello, World" + repeat("!", count);
}
Ad-hoc polymorphism


>   greet();
    Hello, World!
>   greet("Wealthfront");
    Hello, Wealthfront!
>   greet(3);
    Hello, World!!!
Parametric polymorphism


A function or a data type can be written
  generically so that it can handle values
  identically without depending on their type
Parametric polymorphism


size :: a . [a] → int
size [] = 0
size (x:xs) = 1 + size xs
Parametric polymorphism


>   size [1, 2]
    2
>   size ["hello", "hi", "yo"]
    3
Bounded parametric polymorphism


A function or a data type require some
  knowledge of its data types but can otherwise
  work parametrically
Bounded parametric polymorphism



<T extends Comparable<T>> List<T> sort(List<T> list) {
   return …
}
Type Class


Type system construct that supports ad-hoc
 polymorphism, achieved by adding constraints
 to type variables in parametrically
 polymorphic types
Type Class


Type system construct that supports ad-hoc
 polymorphism, achieved by adding constraints
 to type variables in parametrically
 polymorphic types
Type Class


Type system construct that supports ad-hoc
 polymorphism, achieved by adding constraints
 to type variables in parametrically
 polymorphic types
Type Class


Type system construct that supports ad-hoc
 polymorphism, achieved by adding constraints
 to type variables in parametrically
 polymorphic types
Type Class Eq


class Eq a where
 eq :: a → a → bool
 ne :: a → a → bool
Implementation for int


instance Eq int where
 eq m n = m == n
 ne m n = m != n
Implementation for Instrument


instance Eq Instrument where
 eq x {id = xid} y {id = yid} = xid == yid
 ne x {id = xid} y {id = yid} = xid != yid
Usage


member :: (Eq a) ⇒ a → [a] → bool
member y [] = false
member y (x:xs) = (eq x y) || member y xs
Ad-hoc polymorphism


member :: (Eq a) ⇒ a → [a] → bool
member y [] = false
member y (x:xs) = (eq x y) || member y xs
Parametric polymorphism


member :: (Eq a) ⇒ a → [a] → bool
member y [] = false
member y (x:xs) = (eq x y) || member y xs
Constraint on type variable


member :: (Eq a) ⇒ a → [a] → bool
member y [] = false
member y (x:xs) = (eq x y) || member y xs
Type Classes vs. Interfaces?
Similar, yet different:
•  Implementation is separate
•  Subtyping is not entailed
•  Static dispatch (ad-hoc polymorphism) vs.
   dynamic single dispatch
Return type-based dispatch


foo :: (Parseable a) ⇒ a → String → a
foo x s = x + parse s
Return type-based dispatch


>   foo "Hello, " "World!"
    Hello, World!
>   foo 2 "3"
    5
Return type-based dispatch
class String implements Parseable {
   String parse() {
      return this;
   }
   int parse() {
      return Integer.parseInt(this);
   }
}
Type Class in Scala


trait Parseable[T] {
   def parse(s: String): T
}
Implementation in Scala for Int


implicit object ParseableInt extends Parseable[Int] {
   def parse(s: String) = Integer.parseInt(s)
}
Usage in Scala


def foo[T: Parseable](t: T, s: String): T = 
  t + implicitly[Parseable[T]].parse(s)
Usage in Scala


>   foo("Hello, ", "World!");
    Hello, World! : String
>   foo(2, "3");
    5 : Int
Usage in Scala


def foo[T: Parseable](s: String): T = 
  implicitly[Parseable[T]].parse(s)
Usage in Scala


>   "Hello, " + foo("World!");
    Hello, World! : String
>   2 + foo("3");
    5 : Int
>   foo[Int]("3");
    3 : Int
The End




FYI, Monad is a Type Class.

Type Classes

  • 1.
  • 2.
    Type Class Type systemconstruct that supports ad-hoc polymorphism, achieved by adding constraints to type variables in parametrically polymorphic types
  • 3.
    Type Class Type systemconstruct that supports ad-hoc polymorphism, achieved by adding constraints to type variables in parametrically polymorphic types
  • 4.
    Type Class Type systemconstruct that supports ad-hoc polymorphism, achieved by adding constraints to type variables in parametrically polymorphic types
  • 5.
    Type Class Type systemconstruct that supports ad-hoc polymorphism, achieved by adding constraints to type variables in parametrically polymorphic types
  • 6.
    Type Class Type systemconstruct 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 isa programming language feature that allows values of different data types to be handled using a uniform interface
  • 9.
    Several Types ofPolymorphism •  Ad-hoc polymorphism •  Inclusion polymorphism •  Parametric polymorphism •  Bounded parametric polymorphism
  • 10.
    Several Types ofPolymorphism •  Ad-hoc polymorphism –  Operator and method overloading •  Inclusion polymorphism •  Parametric polymorphism •  Bounded parametric polymorphism
  • 11.
    Several Types ofPolymorphism •  Ad-hoc polymorphism •  Inclusion polymorphism –  Subtyping •  Parametric polymorphism •  Bounded parametric polymorphism
  • 12.
    Several Types ofPolymorphism •  Ad-hoc polymorphism •  Inclusion polymorphism •  Parametric polymorphism –  Generics •  Bounded parametric polymorphism
  • 13.
    Several Types ofPolymorphism •  Ad-hoc polymorphism •  Inclusion polymorphism •  Parametric polymorphism •  Bounded parametric polymorphism –  Generics with bounded wildcards
  • 14.
    Ad-hoc polymorphism Control movingthrough one named function is dispatched to various other functions without having to specify the exact function being called
  • 15.
    Ad-hoc polymorphism String greet(){ return "Hello, World!"; }
  • 16.
    Ad-hoc polymorphism String greet(Stringname) { return "Hello," + name + "!"; }
  • 17.
    Ad-hoc polymorphism String greet(intcount) { return "Hello, World" + repeat("!", count); }
  • 18.
    Ad-hoc polymorphism > greet(); Hello, World! > greet("Wealthfront"); Hello, Wealthfront! > greet(3); Hello, World!!!
  • 19.
    Parametric polymorphism A functionor a data type can be written generically so that it can handle values identically without depending on their type
  • 20.
    Parametric polymorphism size ::a . [a] → int size [] = 0 size (x:xs) = 1 + size xs
  • 21.
    Parametric polymorphism > size [1, 2] 2 > size ["hello", "hi", "yo"] 3
  • 22.
    Bounded parametric polymorphism Afunction or a data type require some knowledge of its data types but can otherwise work parametrically
  • 23.
    Bounded parametric polymorphism <Textends Comparable<T>> List<T> sort(List<T> list) { return … }
  • 24.
    Type Class Type systemconstruct that supports ad-hoc polymorphism, achieved by adding constraints to type variables in parametrically polymorphic types
  • 25.
    Type Class Type systemconstruct that supports ad-hoc polymorphism, achieved by adding constraints to type variables in parametrically polymorphic types
  • 26.
    Type Class Type systemconstruct that supports ad-hoc polymorphism, achieved by adding constraints to type variables in parametrically polymorphic types
  • 27.
    Type Class Type systemconstruct that supports ad-hoc polymorphism, achieved by adding constraints to type variables in parametrically polymorphic types
  • 28.
    Type Class Eq classEq a where eq :: a → a → bool ne :: a → a → bool
  • 29.
    Implementation for int instanceEq int where eq m n = m == n ne m n = m != n
  • 30.
    Implementation for Instrument instanceEq Instrument where eq x {id = xid} y {id = yid} = xid == yid ne x {id = xid} y {id = yid} = xid != yid
  • 31.
    Usage member :: (Eqa) ⇒ a → [a] → bool member y [] = false member y (x:xs) = (eq x y) || member y xs
  • 32.
    Ad-hoc polymorphism member ::(Eq a) ⇒ a → [a] → bool member y [] = false member y (x:xs) = (eq x y) || member y xs
  • 33.
    Parametric polymorphism member ::(Eq a) ⇒ a → [a] → bool member y [] = false member y (x:xs) = (eq x y) || member y xs
  • 34.
    Constraint on typevariable 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
  • 36.
    Return type-based dispatch foo:: (Parseable a) ⇒ a → String → a foo x s = x + parse s
  • 37.
    Return type-based dispatch > foo "Hello, " "World!" Hello, World! > foo 2 "3" 5
  • 38.
    Return type-based dispatch classString implements Parseable { String parse() { return this; } int parse() { return Integer.parseInt(this); } }
  • 39.
    Type Class inScala trait Parseable[T] { def parse(s: String): T }
  • 40.
    Implementation in Scalafor Int implicit object ParseableInt extends Parseable[Int] { def parse(s: String) = Integer.parseInt(s) }
  • 41.
    Usage in Scala deffoo[T: Parseable](t: T, s: String): T = t + implicitly[Parseable[T]].parse(s)
  • 42.
    Usage in Scala > foo("Hello, ", "World!"); Hello, World! : String > foo(2, "3"); 5 : Int
  • 43.
    Usage in Scala deffoo[T: Parseable](s: String): T = implicitly[Parseable[T]].parse(s)
  • 44.
    Usage in Scala > "Hello, " + foo("World!"); Hello, World! : String > 2 + foo("3"); 5 : Int > foo[Int]("3"); 3 : Int
  • 45.
    The End FYI, Monadis a Type Class.