Before, we were trying to make division between integrals and fractionals transparent to the students by using a typeclass of our own (Fractionable) which supported converting Int, Integer, Float and Double to Float. That way we were able to implement (/) in a way that 1 :: Int / 2 :: Float would type. The problem with this is that some definitions could be ambiguous, for example:
Float -> Float
half :: (/2)
What's the 2 there? It could be an Int, and Integer, a Float or a Double and it'd be valid, and since the type of (/) is a -> b -> c, we don't give the compiler enough information to deduce it, and it fails with an ambiguous type error.
Solution
Keep the division from the Prelude instead of redefining it, add a function to convert integrals to Floats (since we think that fromIntegral may be a bit conterintuitive to beginners) and add custom type errors instances for Integral Float, Integral Double, Fractional Int, Fractional Integer.
This way, the students will still get type errors when doing things like length [1,2,3] / 3, but we think they will be more likely to solve the type error.
Problem
Before, we were trying to make division between integrals and fractionals transparent to the students by using a typeclass of our own (Fractionable) which supported converting Int, Integer, Float and Double to Float. That way we were able to implement (/) in a way that
1 :: Int / 2 :: Float
would type. The problem with this is that some definitions could be ambiguous, for example:What's the 2 there? It could be an Int, and Integer, a Float or a Double and it'd be valid, and since the type of (/) is a -> b -> c, we don't give the compiler enough information to deduce it, and it fails with an ambiguous type error.
Solution
Keep the division from the Prelude instead of redefining it, add a function to convert integrals to Floats (since we think that fromIntegral may be a bit conterintuitive to beginners) and add custom type errors instances for
Integral Float
,Integral Double
,Fractional Int
,Fractional Integer
. This way, the students will still get type errors when doing things likelength [1,2,3] / 3
, but we think they will be more likely to solve the type error.