Can GHC warn if class instance is a loop?

后端 未结 5 1690
长情又很酷
长情又很酷 2021-01-17 11:54

Real World Haskell has this example:

class BasicEq3 a where
    isEqual3 :: a -> a -> Bool
    isEqual3 x y = not (isNotEqual3 x y)

    isNotEqual3 ::         


        
5条回答
  •  长情又很酷
    2021-01-17 12:09

    No, I'm afraid GHC doesn't do anything like that. Also that isn't possible in general.

    You see, the methods of a type class could be mutually recursive in a useful way. Here's a contrived example of such a type class. It's perfectly fine to not define either sumOdds or sumEvens, even though their default implementations are in terms of each other.

    class Weird a where
        measure :: a -> Int
    
        sumOdds :: [a] -> Int
        sumOdds [] = 0
        sumOdds (_:xs) = sumEvens xs
    
        sumEvens :: [a] -> Int
        sumEvens [] = 0
        sumEvens (x:xs) = measure x + sumOdds xs
    

提交回复
热议问题