first commit
mhe authored 1 week ago
more-typeclasses.md 5.56 KiB
More on type classes and instances
Copyright By PowCoder代写 加微信 powcoder
In this section, we study in detail two important type classes: 1. The type class Ord for ordered types.
2. The type class Num for numeric types.
We also study an example of an instance declaration with constraints: instance Ord a => Ord [a] . This
instance of Ord , then so is [a] .
The type Ordering and the typeclass is also a video on this section.
The type class Ord implements the idea that elements of a type can be compared not only for equality, bu
on a type a is a map compare : a -> a -> Ordering , where the type Ordering is defined as follows: data Ordering =
deriving (Eq, Ord, Enum, Read, Show, Bounded)
Here, LT stands for less than, EQ stands for equal, and GT stands for greater than.
The following defines the Ord class for types a which are in the Eq class.
class (Eq a) => Ord a where
compare :: a -> a -> Ordering (<), (<=), (>=), (>) :: a -> a -> Bool max, min :: a -> a -> a
— Minimal complete definition:
— (<=) or compare
-- Using compare can be more efficient for complex types.
compare x y
|x<=y =LT | otherwise = GT
LT | EQ | GT
x <= y =comparexy/=GT
x< y x>=y x> y
= comparexy==LT = comparexy/=LT = comparexy==GT
— note that (min x y, max x y) = (x,y) or (y,x)
| otherwise = x min x y
|x<=y =x | otherwise = y
There is a seeming circularity between the definition of Ordering and Ord , in that each one refers to the other recursive definition.
For example, the compare function on lists over a (where a is already equipped with a function compare instance (Ord a) => Ord [a] where
compare [] []
compare [] (_:_)
compare (_:_) []
compare (x:xs) (y:ys) = case compare x y of
EQ -> compare xs ys other -> other
1. From reading the code, explain how two lists are compared.
2. Run some examples of comparisons between lists to confirm or refute your explanation.
This video gives an explanation of the implementation of compare :: [a] -> [a] -> Ordering .
The type class Num Consider the following examples
Prelude> 5 + 3
Prelude> 3.14159 + 2.71828 5.85987
Here, the operator + acts on any type that is an instance of the Num typeclass: for any such type a , the functio used as an infix operator.
= EQ = LT = GT
We can use the :info command to find out what operations the Num typeclass provides:
Prelude> :info Num 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
{-# MINIMAL (+), (*), abs, signum, fromInteger, (negate | (-)) #-}
— Defined in ‘GHC.Num’
instance Num Word — Defined in ‘GHC.Num’ instance Num Integer — Defined in ‘GHC.Num’ instance Num Int — Defined in ‘GHC.Num’ instance Num Float — Defined in ‘GHC.Float’ instance Num Double — Defined in ‘GHC.Float’
This shows that any type a that is an instance of Num comes with operations (+) , (-) , (*) ,…, fromInteger annotation is not an integer, but instead an element of any type a that is an instance of Num ; specifically, the ima under the function fromInteger :: Integer -> a .)
We also see that there are five instances of the type class Num defined, for the types Word , Integer , Int consider 1 as an element of a particular numeric type, we can do that by annotating it with that type, e.g.,
Prelude> :type 1 :: Word 1 :: Word :: Word
Here, we check that 1 :: Word instead of asking ghci to infer the type of 1 for us. ghci then just needs t Num . Similarly, for (+) we can check
Prelude> :type (+) :: Integer -> Integer -> Integer
(+) :: Integer -> Integer -> Integer
:: Integer -> Integer -> Integer
This check fails for types for which no instance of Num has been declared, e.g., for Char :
Prelude> :type (+) :: Char -> Char -> Char
• No instance for (Num Char) arising from a use of ‘+’ • In the expression: (+) :: Char -> Char -> Char
3. A blog post comparing Java and Haskell: https://mmhaskell.com/blog/2019/1/28/why-haskell-iv-typeclasses it? Why (not)?
1. We have studied in detail one function that uses both pattern matching on lists and a case expression (a mo matching).
2. We have seen how instances can be derived automatically from other instances, using the example instanc
3. We have taken a look at the type class Num for number types.
4. We have seen how to use a type annotation to force a Haskell expression to have a particular type, e.g.,
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com