[Haskell-cafe] Problem with type signature in local function

Arthur Baars arthurb at cs.uu.nl
Fri Dec 24 08:33:17 EST 2004


You could also do it with the current implementation of scoped type 
variables. So you don't need to compile the CVS HEAD version. Note that 
you need the pattern (n1::a) and/or (n2::a) to introduce the scoped 
type variable, which was not necessary in Ralf's solution.

{-# OPTIONS -fglasgow-exts #-}
add :: Num a => a -> a -> a
add (n1::a) n2 = addToN1 n2
    where addToN1 :: a -> a
          addToN1 number = n1 + number

Cheers,

Arthur
On 24-dec-04, at 13:04, Ralf Laemmel wrote:

> Timely question. :-)
>
> GHC supports lexically scoped type variables.
> And this support has just been _extended_ to be readily useful for you
> (see discussion on the ghc list just a few days back).
> With GHC CVS HEAD the following program works fine.
> (Note that I removed the inner Num constraint.)
>
> {-# OPTIONS -fglasgow-exts #-}
> add :: Num a => a -> a -> a
> add n1 n2 = addToN1 n2
>   where addToN1 :: a -> a
>         addToN1 number = n1 + number
>
> ... even though I would not claim that this is a really strong example
> of lexically scoped variables, but you probably just simplied a real 
> code example.
> And most of my recent code I use lexically scoped type variables
> extensively. They are great.
>
> (In principle, one does not need them at the cost of encoding. Using 
> asTypeOf and friends ...)
>
> Merry Xmas,
> Ralf
>
>
> Christian Hofer wrote:
>
>> Hi,
>>
>> in order to understand the reason for strange compiler error 
>> messages, I often find it convenient to add type signatures to every 
>> function. But sometimes this seems to be impossible when using type 
>> variables.
>>
>> I try to give a very easy example:
>>
>> add :: Num a => a -> a -> a
>> add n1 n2 = addToN1 n2
>>    where addToN1 :: Num a => a -> a
>>          addToN1 number = n1 + number
>>
>> ghc says "Inferred type is less polymorphic than expected", and 
>> justly so, because the type variable "a" in addToN1 must of course be 
>> the same as in add. Is there a way to specify this?
>>
>> Regards,
>> Chris
>>
>> _______________________________________________
>> Haskell-Cafe mailing list
>> Haskell-Cafe at haskell.org
>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>



More information about the Haskell-Cafe mailing list