Why isn't every type part of Eq in Haskell?

后端 未结 7 909
故里飘歌
故里飘歌 2020-12-31 04:48

Or rather, why isn\'t (==) usable on every data type? Why do we have to derive Eq ourseleves? In other languages, such as Python, C++, and surely o

相关标签:
7条回答
  • 2020-12-31 05:36

    You can't imagine a noncomparable type? Well, the classic example are functions. Consider functions [()]->Bool. Two such functions are equal when they return the same value for every possible input. But "unfortunately", there are infinitely many such lists: since Haskell is lazy, the list size isn't even bound by memory. Of course you can compare, for every list input with a length less than some fixed lMax, but where will you draw the line? It's impossible to ever be sure that the functions you compare won't, after 1000000000 equal returns, suddenly return different results for replicate 1000000001 (). So (==) :: ([()]->Bool) -> ([()]->Bool) -> Bool could never actually return True, only either False (if an input for which the functions differ is found) or (if the functions are actually equal). But you can't evaluate .

    0 讨论(0)
提交回复
热议问题