Wei Yuan Cai | 8 May 21:09

[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

_______________________________________________
Haskell mailing list
Haskell <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell
Bulat Ziganshin | 8 May 21:23

Re: [Haskell] Help with polymorphic functions

Hello Wei,

Thursday, May 8, 2008, 11:10:08 PM, you wrote:

> test :: a -> Int -> a

> shift is defined as "a -> Int -> a"

not exactly ;)  this type signature is given inside class Bits, where
'a' isn't a free variable (as in standalone signature declaration),
but means 'a' from type class header:

class Num a => Bits a where
    shift             :: a -> Int -> a

so, this declaration is equivalent to the following standalone one:

shift :: (Bits a) =>  a -> Int -> a

from the common sense POV, you can't shift *ANY* type 'a', but only
types that belong to the Bits class. so, shift cannot have signature
w/o class, and the same remains true for `test`

--

-- 
Best regards,
 Bulat                            mailto:Bulat.Ziganshin <at> gmail.com
Reinier Lamers | 8 May 21:32

Re: [Haskell] Help with polymorphic functions

Op Thursday 08 May 2008 21:10:08 schreef Wei Yuan Cai:
> shift is defined as "a -> Int -> a"
It's not. It's defined as "(Bits a) => a -> Int -> a" or something along those 
lines. So there is a restriction that the type a must be a member of the Bits 
typeclass.

Because "test" is essentially just "shift", its type must also be  "(Bits a) 
=> a -> Int -> a".

Reinier
Ross Mellgren | 8 May 21:34

Re: [Haskell-cafe] Help with polymorphic functions

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 <at> haskell.org
> http://www.haskell.org/mailman/listinfo/haskell
Abhay Parvate | 9 May 14:28

Re: [Haskell-cafe] Help with polymorphic functions

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


_______________________________________________
Haskell mailing list
Haskell <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell
Favicon

Re: [Haskell-cafe] Help with polymorphic functions


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 <at> kf8nh.com
system administrator [openafs,heimdal,too many hats] allbery <at> ece.cmu.edu
electrical and computer engineering, carnegie mellon university    KF8NH


_______________________________________________
Haskell mailing list
Haskell <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell

Gmane