# Type Classes --- CS 135 // 2021-02-25 ## Administrivia - Lab 3... + Am allowing late submissions - It is **imperative** that you have access to VSCode and real-time Haskell compiling + CS Desktop provides this but has been unreliable + Reach out to me ASAP if you don't have access to a stable VSCode environment - Work incrementally! If you have a compiler error, fix it before moving on - Start early and ask questions! # Lab 3 Questions # Questions ## ...about anything? # Type Classes ## Type Classes - You might have wondered what the "Floating" in the following type means: ```haskell sqrt :: Floating a => a -> a ``` - `Floating` is a **typeclass** that constrains the type `a` to ensure that it supports floating point arithmetic - Typeclasses are like **Java interfaces** ## Standard Type Classes - `Eq`: supports equality checks ```haskell (==) :: Eq a => a -> a -> Bool (/=) :: Eq a => a -> a -> Bool ``` - `Ord`: supports comparison ```haskell compare :: Ord a => a -> a -> Ordering (<) :: Ord a => a -> a -> Bool (>) :: Ord a => a -> a -> Bool (>=) :: Ord a => a -> a -> Bool (<=) :: Ord a => a -> a -> Bool max :: Ord a => a -> a -> a min :: Ord a => a -> a -> a ``` ## Standard Type Classes - `Num`: supports basic numeric operations ```haskell (+) :: Num a => a -> a -> a (-) :: Num a => a -> a -> a (*) :: Num a => a -> a -> a negate :: Num a => a -> a -- 0-x abs :: Num a => a -> a -- absolute value signum :: Num a => a -> a -- -1, 0, or 1 fromInteger :: Num a => Integer -> a ``` - `Integral`: supports integer division and modulo ```haskell div :: Integral a => a -> a -> a mod :: Integral a => a -> a -> a ``` ## Standard Type Classes - `Floating`: support floating-point division ```haskell (/) :: Fractional a => a -> a -> a ``` - `Show` and `Read`: to string and from string ```haskell show :: Show a => a -> String read :: Read a => String -> a ``` # Folding ## Folding - Many functions take a list as input and "smashes the elements together" into a single return value ```haskell sumNumbers :: Num a => [a] -> a sumNumbers [] = 0 sumNumbers (x:xs) = x + sumNumbers xs ``` - This is called "folding" and can be written: ```haskell sumNumbers = foldr (+) 0 ``` ## Folding - What if I wanted to concatenate a list of strings? ```haskell concatenateAll :: [String] -> String concatenateAll [] = "" concatenateAll (x:xs) = x ++ concatenateAll xs ``` - Similarly, this can be written using `foldr`: ```haskell concatenateAll = foldr (++) "" ``` ## Folding - If working with a list, we can think of `foldr` having the following type: ```haskell foldr :: (a -> b -> b) -> b -> [a] -> b ``` - The first parameter is a "combining" function - The second parameter is the "initial value" that the fold starts with ## Foldable Type Class - This "foldable" property doesn't only apply to lists, but all sorts of data structures, such as trees - As a result, the true type of `foldr` is: ```haskell Foldable t => (a -> b -> b) -> b -> t a -> b ``` # Self Checks ### Check 1 ```haskell swap :: (a,b) -> (b,a) swap (x,y) = (y,x) ``` --- What is the type of `swap . swap`? 1. `(a, b) -> (a, b)` 2. `(a, b) -> (b, a)` 3. `a -> a` ### Check 2 What is the type of `\f g x -> (f x, g x)`? 1. `(a->b)->(c->d)->(a,c)->(b, d)` 2. `(a->b)->(a->c)->a->(b, c)` 3. `(a->b)->(b->a)->a->(b, a)` ### Check 3 What is the type of: ```haskell \t -> (fst . fst $ t, (snd . fst $ t, snd t)) ``` 1. `(a, (b, c)) -> (a, (b, c))` 2. `(a, (b, c)) -> ((a, b), c)` 3. `((a, b), c) -> (a, (b, c))` ### Check 4 What does this function do: ```haskell foldr (\x xs -> xs ++ [x]) [] ``` 1. It doesn't change its input list at all 2. It changes the associativity of a list from left to right 3. It reverses its input list ### Check 5 What does this function do: ```haskell foldr (\(x, y) zs -> x : y : zs) [] ``` 1. It turns a list of pairs into a pair of lists 2. It turns a pair of lists into a list of pairs 3. It turns a list of pairs into a list of elements ### Check 6 What is the type of: ```haskell foldr (\n b -> n == 3 && b) ``` ```haskell (Foldable t, Eq a, Num a) => Bool -> t a -> Bool -- 1 (Foldable t, Eq a, Num a, Bool b) => b -> t a -> b -- 2 (Foldable t, Eq a, Num a) => Bool -> [ a ] -> Bool -- 3 ``` ### Check 7 What is the type of: ```haskell \x -> case x of (True, "Foo") -> show True ++ "Foo" ``` 1. `Either Bool String -> String` 2. `(Bool, String) -> String` 3. `Show a => (Bool, String) -> a` ## 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