[Haskell-cafe] Defining methods generically for a class

Jeff Heard jefferson.r.heard at gmail.com
Thu Jan 8 12:04:40 EST 2009


I have the following class:

class Region a where
    numDimensions :: a -> Int
    dim :: Int -> a -> (Double,Double)
    merge :: a -> a -> a

and several ancillary methods defined, the most importance of which is:

bounds :: Region a => a -> [(Double,Double)]
bounds r = take (numDimensions r) . map dim . iterate (+1) $ 0

Let's say that I want all Regions to also be of class Eq, where

regiona == regionb = all $ zipWith (==)  (bounds regiona) (bounds regionb)

How do I declare all Regions to be Eqs without putting it in the class
body (since I define a function over all Regions that is independent
of datatype that is an instance of Region)?  Is it merely:

instance Region a => Eq a where
  regiona == regionb = all $ zipWith (==)  (bounds regiona) (bounds regionb)

-- Jeff


More information about the Haskell-Cafe mailing list