This document discusses type classes in Haskell. It describes several basic type classes like Eq, Ord, Show, Read, Num, Integral, and Fractional. Eq is for equality types, Ord is for ordered types, Show is for types that can be converted to strings, Read is for types that can be read from strings, Num is for numeric types, Integral adds integer division/remainder to Num, and Fractional adds fractional division/reciprocation to Num. It provides examples of types that are instances of these classes and how their methods work. It concludes with exercises asking about the types of values, functions, and reasons why function types can't always be Eq instances.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
29 views13 pages
PF S5 Chap3Classes
This document discusses type classes in Haskell. It describes several basic type classes like Eq, Ord, Show, Read, Num, Integral, and Fractional. Eq is for equality types, Ord is for ordered types, Show is for types that can be converted to strings, Read is for types that can be read from strings, Num is for numeric types, Integral adds integer division/remainder to Num, and Fractional adds fractional division/reciprocation to Num. It provides examples of types that are instances of these classes and how their methods work. It concludes with exercises asking about the types of values, functions, and reasons why function types can't always be Eq instances.
support certain overloaded operations called methods. Basic classes Eq – equality types This class contains types whose values can be compared for equality and inequality using the following two methods: • (==) :: a → a → Bool • (/=) :: a → a → Bool All the basic types Bool, Char, String, Int, Integer and Float are instances of the Eq class, as are list and tuple types, provided that their element and component types are instances of the class. Note Function types are not in general instances of the Eq class, because it is not feasible in general to compare two functions for equality. Basic classes Ord – ordered types This class contains types that are instances of the equality class Eq, but in addition whose values are totally (linearly) ordered, and as such can be compared and processed using the following six methods: • (<) :: a → a → Bool • (<=) :: a → a → Bool • (>) :: a → a → Bool • (>=) :: a → a → Bool • min :: a → a → a • max :: a → a → a All the basic types Bool, Char, String, Int, Integer and Float are instances of the Ord class, as are list and tuple types, provided that their element and component types are instances of the class. Note Strings, lists and tuples are ordered lexicographically. Basic classes Show – showable types This class contains types whose values can be converted into strings of characters using the following method: • show :: a → String All the basic types Bool, Char, String, Int, Integer and Float are instances of the Show class, as are list and tuple types, provided that their element and component types are instances of the class. Basic classes Show – showable types (cont.) e.g. > show 2 “2”
> show "123"
“\”123\””
> show 1+2
Error… > show (1+2) > “3” Basic classes Read – readable types This class is dual to Show and contains types whose values can be converted from strings of characters using the following method: • read :: String → a All the basic types Bool, Char, String, Int, Integer and Float are instances of the Show class, as are list and tuple types, provided that their element and component types are instances of the class. Note The result of read is undefined if its argument is not syntactically valid. Basic classes Read – readable types (cont.) e.g. > read "False" :: Bool False
> read "123" :: Int
123 The use of :: in these examples resolves the type of the result. In practice, however, the necessary type information can usually be inferred automatically from the context. e.g. the expression not (read "False") requires no explicit type information, because the application of the logical negation function not implies that read "False" must have type Bool. e.g. The expression not (read "hello") produces an error when evaluated, because "hello" cannot be read as a logical value. Basic classes Num – numeric types This class contains types that are instances of the equality class Eq and showable class Show, but in addition whose values are numeric, and as such can be processed using the following six methods: • (+) :: a → a → a • (−) :: a → a → a • (∗) :: a → a → a • negate :: a → a • abs :: a → a • signum :: a → a The numeric basic types Int, Integer and Float are instances of the Num class. Note The Num class does not provide a division method, but division is handled separately using two special classes, one for integral numbers and one for fractional numbers (see the next classes). Basic classes Integral – integral types This class contains types that are instances of the numeric class Num, but in addition whose values are integers, and as such support the methods of integer division and integer remainder: • div :: a → a → a • mod :: a → a → a The basic types Int and Integer are instances of the Integral class. Note In practice, these two methods are often written between their two arguments by enclosing their names in single back quotes, e.g. 7 `div` 2 or 7 `mod` 2. Basic classes Fractional – fractional types This class contains types that are instances of the numeric class Num, but in addition whose values are non-integral, and as such support the methods of fractional division and fractional reciprocation: • (/) :: a → a → a • recip :: a → a The basic type Float is an instance of the Fractional class. e.g. >7/2 3.5 > recip 2 0.5 > recip 3 0.3333333333333333 Exercises 1. What are the types of the following values? [’a’, ’b’, ’c’] (’a’, ’b’, ’c’) [(False, ’O’), (True, ’1’)] ([False,True ], [’0’, ’1’]) [tail , init, reverse ] 2. What are the types of the following functions? second xs = head (tail xs) Hint: take care to swap (x , y) = (y, x) include the necessary pair x y = (x , y) class constraints if the double x = x ∗ 2 functions are defined using overloaded palindrome xs = reverse xs == xs operators. twice f x = f (f x) 3. Check your answers to the preceding two questions using Haskell Platform. 4. Why is it not feasible in general for function types to be instances of the Eq class? When is it feasible? Hint: two functions of the same type are equal if they always return equal results for equal arguments.