Esteemed fellow haskellers,<br><br>I recently ran into a very simple real life case where Haskell's rules for inferring the types for mutually recursive definitions resulted in a type that was less general than it could be. It took me a while to realize that the type error I was getting wasn't actually a problem with my code. I
understand why Haskell does this (it infers the strongly
connected mutually recursive definitions
monomorphically), but I think it _could_ infer the more general type even
with recursive definitions like this.<br> <br>Here is a simplified example that illustrates the problem:<br><br>> import Data.Maybe<br><br>> -- The fixed point datatype<br>
> data Y f = Y (f (Y f))<br><br>> -- silly dummy function<br>> maybeToInt :: Maybe a -> Int<br>> maybeToInt = length . maybeToList<br><br>> -- f :: Y Maybe -> Int<br>> f (Y x) = g maybeToInt x<br>
<br>> g h x = h $ fmap f x<br><br>This is the type it wants to infer for g<br>g :: (Maybe Int -> Int) -> Maybe (Y Maybe) -> Int<br><br>This is the type I think it should have, note you can't force the type with a typesig without -XRelaxedPolyRec<br>
g :: (Functor f) => (f Int -> b) -> f (Y Maybe) -> b<br><br>If I use -XRelaxedPolyRec I can manually specify the more general type, but then I have to convince myself that there isn't a more general type that I'm missing.<br>
<br><br>Are there other known algorithms that yield a more general type? and if
so, what was the rational for Haskell keeping the current method?<br><br>
I worked out an alternative algorithm that would give a more general type (perhaps the
most general type) but it has factorial complexity and
probably wouldn't be good for strongly connected groups with 7 or more
members.<br>
<br>Even so, I would much rather have the inferred types always be the most general ones and be required to add type signatures
for mutually recursive groups with 7 or more members (which probably need to be
redesigned anyway) than be always required to manually figure out the more general signatures. <br>What do you think?<br><br><br>- Job<br>