[Haskell-cafe] Generalization of types during inference

chris done haskell-cafe at chrisdone.com
Sun Jun 7 17:46:24 UTC 2020

I'm writing a type checker with type classes and for some reason thought
that the below program should be inferred and generalized automatically. I
wasn't sure whether or not GHC would actually accept this or not, so I had to test.

For both Hugs and GHC, a recursive definition's type is not generalized
by the inferer. You need a type signature. See below. I checked with
Hugs just in case there was a generalization limit in GHC due to its more exotic
capabilities (like how let generalization is disabled by type families).

I wonder: is it in general impossible or super hard to infer the most
general type for all cases like this without a type signature? Or is it just considered a 
rare case that we don't care about? Perhaps it has subtler downsides? 

It any case, I'm happy to just copy GHC and Hugs as it makes my life
easier to just require an explicit signature in this case.


-- ERROR "Q.hs":4 - Type error in application
-- *** Expression : f (n - 1) 'a'
-- *** Term : 'a'
-- *** Type : Char
-- *** Does not match : ()

-- • Couldn't match expected type ‘()’ with actual type ‘Char’
-- • In the second argument of ‘f’, namely ‘'a'’
-- In the expression: f (n - 1) 'a'

-- f :: Int -> a -> Int
f 0 x = 0
f 1 x = f 0 ()
f n x = f (n-1) 'a'
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20200607/3bd5ab26/attachment.html>

More information about the Haskell-Cafe mailing list