Edited, memorised or added to reading queue

on 25-Jun-2020 (Thu)

Do you want BuboFlash to help you learning these things? Click here to log in or create user.

Typeclasses and types in Haskell are, in a sense, opposites. Where a declaration of a type defines how that type in particular is created, a declaration of a typeclass defines how a set of types are consumed or used in computations.
statusnot read reprioritisations
last reprioritisation on suggested re-reading day
started reading on finished reading on

pdf

cannot see any pdfs




it may help to think of typeclasses as being like interfaces to data that can work across multiple datatypes. The latter facility is why typeclasses are a means of ad-hoc polymorphism — “ad-hoc” because typeclass code is dispatched by type,
statusnot read reprioritisations
last reprioritisation on suggested re-reading day
started reading on finished reading on

pdf

cannot see any pdfs




typeclasses allow us to generalize over a set of types in order to define and execute a standard set of features for those types.
statusnot read reprioritisations
last reprioritisation on suggested re-reading day
started reading on finished reading on

pdf

cannot see any pdfs




instance Bounded Bool – Bounded for types that have an up- per and lower bound
statusnot read reprioritisations
last reprioritisation on suggested re-reading day
started reading on finished reading on

pdf

cannot see any pdfs




class Num a where

  • (+) :: a -> a -> a
  • (*) :: a -> a -> a
  • (-) :: a -> a -> a
  • negate :: a -> a
  • abs :: a -> a
  • signum :: a -> a
  • fromInteger :: Integer -> a
statusnot read reprioritisations
last reprioritisation on suggested re-reading day
started reading on finished reading on

pdf

cannot see any pdfs




[unknown IMAGE 5559487433996] #has-images

The typeclass constraint (Real a, Enum a) => means that any type

that implements Integral must already have instances for Real and

Enum typeclasses. In a very real sense the tuple syntax here denotes the conjunction of typeclass constraints on your type variables. An

integral type must be both a real number and enumerable (more on

Enum later) and therefore may employ the methods each of those

typeclasses. In turn, the Real typeclass itself requires an instance of

Num. So, the Integral typeclass may put the methods of Real and

Num into effect (in addition to those of Enum).

statusnot read reprioritisations
last reprioritisation on suggested re-reading day
started reading on finished reading on

pdf

cannot see any pdfs




When you have a typeclass-constrained (ad-hoc) polymorphic value and need to evaluate it, the polymorphism must be resolved to a specific concrete type. The concrete type must have an instance for all the required typeclass instances (that is, if it is required to implement Num and Fractional then the concrete type can’t be an Int)
statusnot read reprioritisations
last reprioritisation on suggested re-reading day
started reading on finished reading on

pdf

cannot see any pdfs




The Haskell Report specifies the following defaults relevant to numerical computations:
default Num Integer
default Real Integer
default Enum Integer
default Fractional Double

Double Num, Real, etc. are typeclasses, and Integer and Double are the types they default to.
This type defaulting for Fractional means that (/) :: Fractional a => a -> a -> a changes to (/) :: Double -> Double -> Double if you don’t specify the type.
statusnot read reprioritisations
last reprioritisation on suggested re-reading day
started reading on finished reading on

pdf

cannot see any pdfs




Types can be made more specific, but not more general or polymorphic.
statusnot read reprioritisations
last reprioritisation on suggested re-reading day
started reading on finished reading on

pdf

cannot see any pdfs




We can declare more specific (monomorphic) functions from more general (polymorphic) functions:
let add = (+) :: Integer -> Integer -> Integer
We cannot go in the other direction, because we lost the generality of Num when we specialized to Integer:
statusnot read reprioritisations
last reprioritisation on suggested re-reading day
started reading on finished reading on

pdf

cannot see any pdfs




[unknown IMAGE 5559502638348] #has-images
statusnot read reprioritisations
last reprioritisation on suggested re-reading day
started reading on finished reading on

pdf

cannot see any pdfs




We need to take care to avoid partial functions in general in Haskell, but this must be especially kept in mind when we have a type with multiple “cases” such as DayOfWeek.
statusnot read reprioritisations
last reprioritisation on suggested re-reading day
started reading on finished reading on

pdf

cannot see any pdfs




The good news is there is something you can do to get more help from GHC on avoiding partial functions. If we turn all warnings on with the Wall flag in our REPL or in our build configuration (such as with Cabal — more on that later), then GHC will let us know when we’re not handling all cases:
statusnot read reprioritisations
last reprioritisation on suggested re-reading day
started reading on finished reading on

pdf

cannot see any pdfs