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

Ralf Laemmel Ralf.Laemmel at cwi.nl
Fri Dec 24 07:04:54 EST 2004


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





More information about the Haskell-Cafe mailing list