# Classes and Instances --- CS 135 // 2021-03-11 ## Class Presentations - Class presentations are coming up - You don't need to master your language of choice - Instead, be sure to answer these questions: 1. why the language was created 2. what the design goals for the language were 3. what the structure of the language looks like 4. how the design goals influenced its structure. # Lab 5a Debrief # Questions ## ...about anything? # Type Classes ## Type Classes - Recall that typeclasses are similar to **Java interfaces** + They constrain an generic type to support certain operations ```haskell sort :: Ord a => [a] -> [a] sort [] = [] sort (x:xs) = left ++ [x] ++ right where left = sort (filter (<=x) xs) right = sort (filter (>x) xs) ``` ## Defining a Type Class - To define a type class, we can do: ```haskell class Eq a where (==) :: a -> a -> Bool (/=) :: a -> a -> Bool ``` - Some classes also include **default implementations** ```haskell class Eq a where (==) :: a -> a -> Bool (/=) :: a -> a -> Bool x /= y = not (x == y) ``` - Now once an implementation for `==` is provided, the `/=` operator is supported for free ## Type Class Instances - Suppose I have a type class named `Combinable`: ```haskell class Combinable a where (<+>) :: a -> a -> a combineAll :: [a] -> a ``` - We can create an **instance** of the type class using: ```haskell instance Combinable String where x <+> y = x ++ y combineAll xs = foldr (++) "" xs ``` ## Type Class Instances - Note that the `combineAll` could be implemented in terms of `<+>` as a default implementation if we had an `empty` value: ```haskell class Combinable a where empty :: a (<+>) :: a -> a -> a combineAll :: [a] -> a combineAll = foldr (<+>) empty ``` - Now our instance for `String` could be: ```haskell instance Combinable String where empty = "" (<+>) = (++) ``` # Self Checks ### Check 1 What are the functions in the `Eq` class? --- 1. `(==), (/=)` 2. `(==)` 3. `(==)`, `(<)`, `(>)` ### Check 2 - For which of the following classes can we get automatic instances with `deriving`? --- 1. `Num` 2. `Ord` 3. `Size` ### Check 3 Which of the following instance declarations is legal? --- 1. `instance Eq Maybe` 2. `instance Eq (a,a)` 3. `instance Eq (Maybe Int)` 4. `instance Eq (a,b)` ### Check 5 - The following instance declaration: ```haskell instance Num a => Eq (Pair a) ``` tells me that --- 1. All instances of `Num` are instances of `Eq` 2. `Pair a` is an instance of `Eq` if `a` is an instance of `Num` 3. The instance `Eq (Pair a)` inherits the instance `Num a` ### Check 6 - The following type declaration ```haskell class Num a => Fractional a ``` tells me that --- 1. Instances of `Fractional` must be `Num` instances 2. Instances of `Num` must be `Fractional` instances 3. If I define an instance for `Fractional`, I also get an instance for `Num` 4. If I define an instance for `Num`, I also get an instance for `Fractional` ## Lab Time - Please turn on your camera when in a breakout session if possible - Some students are hard of hearing and rely on lip-reading to follow along in conversation