Sometimes GHC is far too smart (for me).
Two functions (from here).
f :: (RealFloat a) => Maybe (Complex a) -> Maybe (Complex a) -> Maybe (Complex a) -> Maybe (Complex a)
f m1 m2 r = g ⊗ m1 ⊗ m2 ⊘ sq r
where
g = to 6.67 ⊗ (to 10 ⊗⊗ negate' (to 11))
f' :: (RealFloat a, Num (Maybe (Complex a))) => Maybe (Complex a) -> Maybe (Complex a) -> Maybe (Complex a) -> Maybe (Complex a)
f' m1 m2 r = g ⊗ m1 ⊗ m2 ⊘ sq r
where
g = to 6.67 ⊗ (to 10 ⊗⊗ negate' 11)
The difference is in how the negate'
function is called.
to :: (Num a) => a -> Maybe (Complex a)
to a = Just $ a :+ 0
negate' :: (RealFloat a) => Maybe (Complex a) -> Maybe (Complex a)
negate' = fmap negate
The f'
negate call is clearly wrong, and yet somehow GHC infers a typeclass constraint which on some level must type check.
Of course when I try to execute f'
it fails because it cannot find the instance it inferred…
These type of failures are always curiosities to me. The naive solution seems easiest to find and more correct…
How should I think about this? Maybe I just misunderstand the f'
function…
In future I think I’ll just provide explicit type definitions, might make things clearer…