Embrace `/` against 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 double. When I uncomment a signature like f2, it works fine.

Shouldn't you care exactly about the type of output?

Is this a mistake in the arms?

I checked the type / , and the Double and Float - / instances work on Fractional s, and there is a Fractional instance for Double and Float , so this should not be a problem.

+4
source share
1 answer

When binding without a signature, type f2 has a polymorphic assumed type ( Fractional a => a ), which must be monomorphized due to the restriction of monomorphism (unless this is disabled). In the absence of other default declarations, the variable type with the Fractional constraint is set to Double by default. The hugs seem to default at first, and then suffocate from trying to bind the declared Float f1 to the value of Double f2 . The GHC looks further and sees that f2 used with the type Float and, therefore, monomorphizes f2 to Float .

I'm not sure what kind of behavior the locale sets in this situation, but I think GHC is correct because monomorphization should only happen after entering the binding group, and I think that f1 and f2 together form the binding group, because f1 uses f2 .

+7
source

Source: https://habr.com/ru/post/1403489/


All Articles