[Haskell] Re: Implicit type of numeric constants
Robert Stroud
R.J.Stroud at ncl.ac.uk
Thu Sep 21 06:32:20 EDT 2006
On 21 Sep 2006, at 10:46, Robert Stroud wrote:
>> So k gets a monotype which is determined by its usage, you cannot
>> do e.g.
>>
>> let k = 2 ; f :: Int -> Int -> Int ; f x y = x * y in (f k k, 1/k)
>>
>> whereas let k :: Num a => a; k = 2; ... is possible.
>
> 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?
>
> 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.
Aha - light begins to dawn... :-)
Each 2 is a different 2, so the type of 2 can be different each time
it's used, whereas there's only one k so it can only have one type.
Is that right?
Robert
More information about the Haskell
mailing list