1
votes

I get the following error

Ambiguous type variable ‘a0’ arising from a use of ‘print’ prevents the constraint ‘(Show a0)’ from being solved."

when I type this simple expression into the winghci console:

(1.0 * (floor 5))/(1.0 * (floor 5))

I've read in other SO posts that you can't use the "/" division for integers, but here I'm trying to divide fractional numbers.

1
The problem is not (/) itself, but the fact that floor will return an Integral type.Willem Van Onsem

1 Answers

2
votes

floor :: (RealFrac a, Integral b) => a -> b returns values of a type that is a member of the Integral typeclass. Furthermore (/) :: Fractional a => a -> a -> a requires the operands and the result to be all of same type and that type should be a member of the Fractional typeclass.

While in Haskell it is technically possible to make a type that is both a member of the Integral and the Fractional typeclass, it makes not much sense, and there is definitely no such standard type.

You thus will need to convert the result of the floor back to a type that can be fractional, for example by using fromIntegral :: (Integral a, Num b) => a -> b:

Prelude> (1.0 * fromIntegral (floor 5))/(1.0 * fromIntegral (floor 5))
1.0