[Haskell-cafe] Problem with type signature in local function
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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Problem with type signature in local function
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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Problem with type signature in local function
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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Problem with type signature in local function
Thank you all for your answers! I indeed do not want to use CVS HEAD, but it's nice to know that there will be a better solution some time. Until then I will write s.th. like loop (residual :: MutableGraph state node weight) flow absValue = ... which is completely sufficient for my purposes (implementing my Java homework in the imperative language of my choice)... Merry christmas, Chris Am 24.12.2004 um 14:33 schrieb Arthur Baars: 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 ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe