Can GHC warn if class instance is a loop?

后端 未结 5 1525
长情又很酷
长情又很酷 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:28

    I don't think so. I worry that you're expecting the compiler to solve the halting problem! Just because two functions are defined in terms of each other, doesn't mean it's a bad default class. Also, I've used classes in the past where I just needed to write instance MyClass MyType to add useful functionality. So asking the compiler to warn you about that class is asking it to complain about other, valid code.

    [Of course, use ghci during development and test every function after you've written it! Use HUnit and/or QuickCheck, just to make sure none of this stuff ends up in final code.]

提交回复
热议问题