Why do all Haskell typeclasses have laws?

后端 未结 3 1346
既然无缘
既然无缘 2021-02-07 06:16

All the typeclasses in Typeclassopedia have associated laws, such as associativity or commutativity for certain operators. The definition of a \"law\" seems to be a constraint t

3条回答
  •  不知归路
    2021-02-07 06:58

    A typeclass doesn't need to have laws, but it often will be more useful if it has them. Many typeclasses are expected to function in a certain way, the laws codify user expectations. The laws let users make assumptions about the way that an instance of a typeclass will work. If you break the typeclass laws, you don't get arrested by the Haskell police, you just end up with confused users.

提交回复
热议问题