[Haskell] Re: Implicit type of numeric constants

Christian Sievers sievers at math2.nat.tu-bs.de
Thu Sep 21 13:54:52 EDT 2006


Robert Stroud wrote:

> Thanks - that's a helpful example. But why is the following not  
> equivalent to the untyped "k = 2" case:
> 
> let f :: Int -> Int -> Int ; f x y = x * y in (f 2 2, 1/2)
> 
> Does the type of 2 effectively get decided twice, once as an Int, and  
> once as a Fractional, and is this the "repeated computation" that the  
> monomorphism restriction is intended to prevent?

2 has the fixed polymorphic type  Num a => a, which gets resolved
at each occurance.  This resolving happens at compile time, so there
is no repeated computation at run time involved.  However, you need
to somehow get both a 2::Int and a 2::Double which may be seen as a
trivial case of this repeated computation.

> Otherwise, I would have expected that it wouldn't make any difference  
> whether I used a named 2 or an anonymous 2, but imposing the  
> monomorphism restriction on the named 2 seems to break referential  
> transparency.

Only if you expect referential transparency for implicitly typed values.
All you have to do is say

  k :: Num a => a
  k = 2

and everything is fine.


Bye
Christian Sievers


More information about the Haskell mailing list