Haskell why does “Num x” require “Show x”?

好久不见. 提交于 2019-11-29 13:14:42

The information in LYAH is old. The release notes for GHC 7.4.1 say that:

The Num class no longer has Eq or Show superclasses.

You will need to write,

foo :: (Num x, Show x) => x -> String

(In fact, the foo you wrote doesn't require Num x, so you can omit that to avoid an unnecessary constraint.)

It used to be that an instance of Num was also an instance of Show and Eq , but that's no longer the case.

You'll need to add a Show constraint as well.

Haskell, both 98 and 2010 both require all instances of Num to also be instances on Show and Eq. This is largely an accident of history.

GHC, the most popular Haskell compiler, diverges from the standard here without requiring any pragma. This was done to allow applicative functors to be instances of Num and enjoy the benefits of overloaded syntax.

Shouldn't you write:

(Num x) => x -> String

Instead of

(Num x) x -> String

And as far as I know this inheritance is at least outdated.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!