[Haskell-cafe] Why does Haskell not infer most general type?

Ross Paterson ross at soi.city.ac.uk
Tue Apr 6 17:12:05 EDT 2010


On Tue, Apr 06, 2010 at 03:56:32PM -0400, Job Vranish wrote:
> f _ = undefined
>   where
>     _ = y :: Int -> Int
> 
> y x = undefined
>   where
>     _ = f x

Because f and y are mutually recursive, their types are inferred together,
so y gets the type Int -> Int (as given), which forces f :: Int -> a.

If you add the type signature f :: a -> b, you break the cycle: that
type is used in inferring the type of y (namely a -> b), which is then
used in checking the typeof f.  Ditto if you add y :: a -> b instead.
(This is not Haskell 98, but the implementations have done this for
years, and it will be in Haskell 2010.)


More information about the Haskell-Cafe mailing list