SlideShare a Scribd company logo
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.

More Related Content

PPTX
Quick Scala
PDF
Demystifying Shapeless
PDF
Scala Types of Types @ Lambda Days
PDF
Scala jargon cheatsheet
PDF
Few simple-type-tricks in scala
PPTX
Learning core java
PPT
Java core - Detailed Overview
PDF
Lecture 1 - Objects and classes
Quick Scala
Demystifying Shapeless
Scala Types of Types @ Lambda Days
Scala jargon cheatsheet
Few simple-type-tricks in scala
Learning core java
Java core - Detailed Overview
Lecture 1 - Objects and classes

What's hot (19)

PPTX
Object Oriented Programming_Lecture 2
PPTX
Introduction to java
PDF
From android/java to swift (1)
PDF
Introduction to type classes in Scala
PPTX
L2 datatypes and variables
PDF
Scala Type Classes: Basics and More
PPTX
‫‫Chapter4 Polymorphism
PDF
Miles Sabin Introduction To Scala For Java Developers
PPTX
Ruby basics
PDF
Classy Monad Transformers (Stop Eff'ing Around)
PDF
What are Sealed Classes in Scala?
PPTX
Type Systems
PPTX
5variables in c#
PPTX
PDF
Object oriented programming With C#
PPTX
Javascript analysis
PPTX
Generics In and Out
PPT
Vhdl identifiers,data types
PDF
An introduction to functional programming with Swift
Object Oriented Programming_Lecture 2
Introduction to java
From android/java to swift (1)
Introduction to type classes in Scala
L2 datatypes and variables
Scala Type Classes: Basics and More
‫‫Chapter4 Polymorphism
Miles Sabin Introduction To Scala For Java Developers
Ruby basics
Classy Monad Transformers (Stop Eff'ing Around)
What are Sealed Classes in Scala?
Type Systems
5variables in c#
Object oriented programming With C#
Javascript analysis
Generics In and Out
Vhdl identifiers,data types
An introduction to functional programming with Swift
Ad

Similar to Type Classes (20)

PPTX
Polymorphism
 
PDF
Peyton jones-2009-fun with-type_functions-slide
PDF
Object-oriented Design: Polymorphism via Inheritance (vs. Delegation)
PDF
Lecture 4: Data Types
PDF
DTS s03e04 Typing
PDF
Compiler Construction | Lecture 7 | Type Checking
PDF
Study of the Subtyping Machine of Nominal Subtyping with Variance
PDF
07. haskell Membership
PPT
Type Checking(Compiler Design) #ShareThisIfYouLike
PDF
Software analysis and testing
PDF
12TypeSystem.pdf
PDF
The Ceylon Type System - Gavin King presentation at QCon Beijing 2011
PDF
The Ceylon Type System
PPTX
Type checking compiler construction Chapter #6
PDF
A Brief Introduction to Type Constraints
PPT
Learning typescript
PDF
Polymorphism (Ad-hoc and Universal)
PPT
chapter7.ppt java programming lecture notes
PPT
chapter 7: DATA TYPES - IN PROGRAMMING LANGUAGES
Polymorphism
 
Peyton jones-2009-fun with-type_functions-slide
Object-oriented Design: Polymorphism via Inheritance (vs. Delegation)
Lecture 4: Data Types
DTS s03e04 Typing
Compiler Construction | Lecture 7 | Type Checking
Study of the Subtyping Machine of Nominal Subtyping with Variance
07. haskell Membership
Type Checking(Compiler Design) #ShareThisIfYouLike
Software analysis and testing
12TypeSystem.pdf
The Ceylon Type System - Gavin King presentation at QCon Beijing 2011
The Ceylon Type System
Type checking compiler construction Chapter #6
A Brief Introduction to Type Constraints
Learning typescript
Polymorphism (Ad-hoc and Universal)
chapter7.ppt java programming lecture notes
chapter 7: DATA TYPES - IN PROGRAMMING LANGUAGES
Ad

Recently uploaded (20)

PPTX
A Presentation on Artificial Intelligence
PDF
Approach and Philosophy of On baking technology
PDF
Getting Started with Data Integration: FME Form 101
PDF
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
PPTX
OMC Textile Division Presentation 2021.pptx
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
PDF
August Patch Tuesday
PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PDF
Mushroom cultivation and it's methods.pdf
PPTX
TLE Review Electricity (Electricity).pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Hybrid model detection and classification of lung cancer
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PDF
DP Operators-handbook-extract for the Mautical Institute
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
project resource management chapter-09.pdf
PPTX
Tartificialntelligence_presentation.pptx
A Presentation on Artificial Intelligence
Approach and Philosophy of On baking technology
Getting Started with Data Integration: FME Form 101
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
OMC Textile Division Presentation 2021.pptx
A comparative analysis of optical character recognition models for extracting...
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
August Patch Tuesday
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
Mushroom cultivation and it's methods.pdf
TLE Review Electricity (Electricity).pptx
Building Integrated photovoltaic BIPV_UPV.pdf
Hybrid model detection and classification of lung cancer
Accuracy of neural networks in brain wave diagnosis of schizophrenia
DP Operators-handbook-extract for the Mautical Institute
NewMind AI Weekly Chronicles - August'25-Week II
Unlocking AI with Model Context Protocol (MCP)
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
project resource management chapter-09.pdf
Tartificialntelligence_presentation.pptx

Type Classes

  • 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
  • 15. Ad-hoc polymorphism String greet() { return "Hello, World!"; }
  • 16. Ad-hoc polymorphism String greet(String name) { return "Hello," + name + "!"; }
  • 17. Ad-hoc polymorphism String greet(int count) { return "Hello, World" + repeat("!", count); }
  • 18. Ad-hoc polymorphism > greet(); Hello, World! > greet("Wealthfront"); Hello, Wealthfront! > greet(3); Hello, World!!!
  • 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
  • 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 A function or a data type require some knowledge of its data types but can otherwise work parametrically
  • 23. Bounded parametric polymorphism <T extends Comparable<T>> List<T> sort(List<T> list) { return … }
  • 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
  • 28. Type Class Eq class Eq a where eq :: a → a → bool ne :: a → a → bool
  • 29. Implementation for int instance Eq int where eq m n = m == n ne m n = m != n
  • 30. 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
  • 31. Usage member :: (Eq a) ⇒ 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 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
  • 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 class String implements Parseable { String parse() { return this; } int parse() { return Integer.parseInt(this); } }
  • 39. Type Class in Scala trait Parseable[T] { def parse(s: String): T }
  • 40. Implementation in Scala for Int implicit object ParseableInt extends Parseable[Int] { def parse(s: String) = Integer.parseInt(s) }
  • 41. Usage in Scala def foo[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 def foo[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, Monad is a Type Class.