Search code examples
haskellhugs

Hugs `/` vs. Type Inference


In GHCi the following code works fine:

f1 :: Float
f1 = f2

-- f2 :: Float
f2 = 1/1

But in Hugs, I get a type error - it wants to be a Double. When I uncomment the type signature of f2, it works fine.

Shouldn't exactly this be taken care of the type inference?

Is this a bug in Hugs?

I checked out the type of / and the instances of Double and Float - / works on Fractionals, and there is a Fractional instance for both Double and Float, so this should not be the problem.


Solution

  • When bound without type signature, f2 has a polymorphic inferred type (Fractional a => a), that has to be monomorphised because of the monomorphism restriction (unless that is disabled). In the absence of other default declarations, a type variable with a Fractional constraint defaults to Double. Hugs seems to do the defaulting first, and then choke on the attempt of binding the declared Float f1 to the Double value f2. GHC looks further and sees that f2 is used at type Float, and hence monomorphises f2 to Float.

    I'm not sure what behaviour the language standard mandates in this situation, but I think GHC's is correct, because the monomorphising should only occur after typing a binding group, and I think that f1 and f2 together form a binding group, because f1 uses f2.