6
votes

As an example, consider the trivial function

f :: (Integral b) => a -> b
f x = 3 :: Int

GHC complains that it cannot deduce (b ~ Int). The definition matches the signature in the sense that it returns something that is Integral (namely an Int). Why would/should GHC force me to use a more specific type signature?

Thanks

3
This signature says that the function can return any Integral type, which is obviously not true.n. 1.8e9-where's-my-share m.

3 Answers

17
votes

Type variables in Haskell are universally quantified, so Integral b => b doesn't just mean some Integral type, it means any Integral type. In other words, the caller gets to pick which concrete types should be used. Therefore, it is obviously a type error for the function to always return an Int when the type signature says I should be able to choose any Integral type, e.g. Integer or Word64.

There are extensions which allow you to use existentially quantified type variables, but they are more cumbersome to work with, since they require a wrapper type (in order to store the type class dictionary). Most of the time, it is best to avoid them. But if you did want to use existential types, it would look something like this:

{-# LANGUAGE ExistentialQuantification #-}

data SomeIntegral = forall a. Integral a => SomeIntegral a

f :: a -> SomeIntegral
f x = SomeIntegral (3 :: Int)

Code using this function would then have to be polymorphic enough to work with any Integral type. We also have to pattern match using case instead of let to keep GHC's brain from exploding.

> case f True of SomeIntegral x -> toInteger x
3
> :t toInteger
toInteger :: Integral a => a -> Integer

In the above example, you can think of x as having the type exists b. Integral b => b, i.e. some unknown Integral type.

2
votes

The most general type of your function is

f :: a -> Int

With a type annotation, you can only demand that you want a more specific type, for example

f :: Bool -> Int

but you cannot declare a less specific type. The Haskell type system does not allow you to make promises that are not warranted by your code.

0
votes

As others have said, in Haskell if a function returns a result of type x, that means that the caller gets to decide what the actual type is. Not the function itself. In other words, the function must be able to return any possible type matching the signature.

This is different to most OOP languages, where a signature like this would mean that the function gets to choose what it returns. Apparently this confuses a few people...