[More ch 5 John Goerzen **20070603221853] { hunk ./en/00book.xml 23 + hunk ./en/ch05-typeclasses.xml 153 + + While our definition of BasicEq2 is fine, it seems + that we're making extra work for ourselves. Logically speaking, if we + know what isEqual or + isNotEqual would return, we know how to figure out + what the other function would return, for all types. Rather than + making users of the typeclass define both functions for all types, we + can provide default implementations for them. Then, users will only + have to implement one function. shows how + to do this. + + + BasicEq3 Typeclass Definition (eqclasses.hs) + &eqclasses.hs:basiceq3; + + + People implementing this class must provide an implementation of at + least one function. They can implement both if they wish, but they + will not be required to. + + + With BasicEq3, we have provided a class that does + very much the same thing as Haskell's built-in == + and /= operators. In fact, these operators are + defined by a typeclass that looks almost identical to + BasicEq3. The Haskell 98 Report FIXME: add + reference? --jg defines a typeclass that implements equality + comparison. See for the built-in + Eq typeclass, and note how similar it is to our + BasicEq3 typeclass. + + FIXME: do we have rights to quote this? I figure it is probably + fair use in any case. -- jg + + Haskell Standard Eq Typeclass + +class Eq a where + (==), (/=) :: a -> a -> Bool + + -- Minimal complete definition: + -- (==) or (/=) + x /= y = not (x == y) + x == y = not (x /= y) + + hunk ./examples/ch05/eqclasses.hs 35 +{-- snippet basiceq3 --} +class BasicEq3 a where + isEqual3 :: a -> a -> Bool + isEqual3 x y = not (isNotEqual3 x y) + + isNotEqual3 :: a -> a -> Bool + isNotEqual3 x y = not (isEqual3 x y) +{-- /snippet basiceq3 --} }