简体   繁体   中英

If compiler of Haskell always needs polymorphic parameters of polymorphic functions to be specified with “::Int”,why “show 2” is legal

If we type show 2 ,then we will get "2".But the question is show satisfies show :: Show a => a -> String ,and 2 is polymorphic, if unfortunately show 2::Int differs from show 2::Integer we'd have to write show 2::Int and show 2::Integer rather than simply show 2 .

I refuse to assume that the compiler is enough intelligent to know when (A a)=>show a ,all current instances of A are of Show, gives the same result, we needn't to specify show a::X and when (A a)=>show a ,all current instances of A are of Show, gives different results, we have to specify show a::X .

This is due to defaulting rules. So show 2 is actually show (2::Integer) . You can read this in haskell 2010 report here in section 4.3.4 .

To answer your second question, compiler is not intelligent enough. It happens due to type defaulting.

You can check

 number = 2

In ghci

*Main> :t number 
 number :: Integer

Now your custom default signature

 default (Int)
 number = 2

In ghci

*Main> :t number
number :: Int

You can read about when a type is defaultable in the document I referenced.

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