Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 693
TorosFanny Avatar asked Dec 08 '22 18:12

TorosFanny


1 Answers

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.

like image 182
Satvik Avatar answered Apr 29 '23 19:04

Satvik