[Haskell-cafe] Help with polymorphic functions
Hello, I'm having some trouble with a polymorphic function using another polymorphic function within. A simplified code of what I'm trying to do is as follows: main = print $ test 1 8 test :: a -> Int -> a test x n = shift x n I get the following compilation error: Could not deduce (Data.Bits.Bits a) from the context () arising from a use of `shift' at test.hs:8:11-19 Possible fix: add (Data.Bits.Bits a) to the context of the type signature for `test' In the expression: shift x n In the definition of `test': test x n = shift x n shift is defined as "a -> Int -> a" What am I doing wrong here? Thanks, Weiyuan
You gave test a type signature which gives "a" universal quantification, which means in this case that "a" is something, but you can't do anything in particular to it (since you don't know anything about it). shift has the signature a -> Int -> a, but it's within the type class Bits: Prelude> import Data.Bits Prelude Data.Bits> :i shift class (Num a) => Bits a where ... shift :: a -> Int -> a ... -- Defined in Data.Bits infixl 8 shift So in this case, "a" is actually the "a" from "Bits a" above. Your function, test, does not say that "a" is a bits, and that's what the compiler is telling you. Change your type signature to shift :: Bits a => a -> Int -> a and it should be good to go! -Ross P.S. I'm something of a haskell newbie, so if I got any of this wrong, please someone more knowledgeable correct me! On May 8, 2008, at 3:10 PM, Wei Yuan Cai wrote:
Hello,
I'm having some trouble with a polymorphic function using another polymorphic function within. A simplified code of what I'm trying to do is as follows:
main = print $ test 1 8
test :: a -> Int -> a test x n = shift x n
I get the following compilation error:
Could not deduce (Data.Bits.Bits a) from the context () arising from a use of `shift' at test.hs:8:11-19 Possible fix: add (Data.Bits.Bits a) to the context of the type signature for `test' In the expression: shift x n In the definition of `test': test x n = shift x n
shift is defined as "a -> Int -> a"
What am I doing wrong here?
Thanks, Weiyuan _______________________________________________ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell
You can also drop the type signature and see what type deduction you get in ghci :) Your program would have compiled well without the type signature! It's also a good exercise to come up with the most general type of a function that you write, and then compare it with the type that is deduced by dropping it from the code. Of course in the final code many people would recommend that you put in the type declarations as a good form of documentation, and it may be also more specialized according to your usage than what the compiler/interpreter will deduce. Abhay 2008/5/9 Wei Yuan Cai <weiyuan.cai@gmail.com>:
Hello,
I'm having some trouble with a polymorphic function using another polymorphic function within. A simplified code of what I'm trying to do is as follows:
main = print $ test 1 8
test :: a -> Int -> a test x n = shift x n
I get the following compilation error:
Could not deduce (Data.Bits.Bits a) from the context () arising from a use of `shift' at test.hs:8:11-19 Possible fix: add (Data.Bits.Bits a) to the context of the type signature for `test' In the expression: shift x n In the definition of `test': test x n = shift x n
shift is defined as "a -> Int -> a"
What am I doing wrong here?
Thanks, Weiyuan
_______________________________________________ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell
On 2008 May 9, at 8:28, Abhay Parvate wrote:
Of course in the final code many people would recommend that you put in the type declarations as a good form of documentation, and it may be also more specialized according to your usage than what the compiler/interpreter will deduce.
It's also useful to include them because it helps localize other type errors; if you leave type inference to the compiler, a type error will be reported when it is discovered, which may be far from where the actual error is. You end up having to trace back through the definitions of functions used in the failing expression to find where the actual type error is. -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH
participants (4)
-
Abhay Parvate -
Brandon S. Allbery KF8NH -
Ross Mellgren -
Wei Yuan Cai