[Haskell] [Haskell-cafe] Help with polymorphic functions

Abhay Parvate abhay.parvate at gmail.com
Fri May 9 08:28:57 EDT 2008


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 at 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 at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell/attachments/20080509/10df2022/attachment.htm


More information about the Haskell mailing list