简体   繁体   中英

Haskell: Does Num class “inherit” Eq class?

From Learn You a Haskell, I have:

lucky :: Integral a => a -> String
lucky 7 = "lucky number seven!"
lucky x = "try again"

but, when I do

lucky :: Num a => a -> String
lucky 7 = "lucky number seven!"
lucky x = "try again"

I get

Could not deduce (Eq a) arising from literal `7'

So, then I do

lucky :: (Eq a, Num a) => a -> String
lucky 7 = "lucky number seven!"
lucky x = "try again"

and, the compiler is happy.

Doesn't the Num type class "inherit" the Eq class?

Saying (Num a, Eq a) seems redundant. From the Haskell 98 report, where they have a great diagram (yeah, visuals!) of the Standard Classes it sure seems like it's "inherited." http://www.haskell.org/onlinereport/basic.html

The Haskell report suggests that Num should be a subclass of Eq and Show , but if you look at the source for the Num class in ghc, then it is not.

This change appears to have been introduced in GHC 7.4.1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM