Luka Horvat
Luka Horvat

Reputation: 4402

Haskell type error. Can't match (Int -> Int) with (Bits b => Int -> b)

I have this simple code

class Hashable a where
    hash :: Bits b => a -> b

instance Hashable Int where
    hash = id

But I get the following error

Could not deduce (b ~ Int)
from the context (Bits b)
  bound by the type signature for hash :: Bits b => Int -> b
  at Memo.hs:11:5-8
  `b' is a rigid type variable bound by
      the type signature for hash :: Bits b => Int -> b at Memo.hs:11:5
Expected type: Int -> b
  Actual type: Int -> Int
Relevant bindings include hash :: Int -> b (bound at Memo.hs:11:5)
In the expression: id :: Int -> Int
In an equation for `hash': hash = id :: Int -> Int
In the instance declaration for `Hashable Int'

I don't really get it. The class only specifies that you can get a Bits instance from an object of type a.

There exists Bits Int, so why doesn't hash = id work for Int?

Upvotes: 0

Views: 48

Answers (1)

jamshidh
jamshidh

Reputation: 12070

(Bits b=>a->b) means that you can extract any type instance of Bits, not just one. hash = id only works for type Int, the compiler is basically complaining "hey, what about all the other Bits instances?"

Upvotes: 6

Related Questions