zslevi
zslevi

Reputation: 409

REPL arithmetic - Ambiguous type variable ‘a0’ arising from a use of ‘print’

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.

Upvotes: 1

Views: 111

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477180

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

Upvotes: 2

Related Questions