With functional dependencies I can constrain type of dependent parameter in a type class using multi-parameter type classes. Just like this:
{-# LANGUAGE FunctionalDependencies, MultiParamTypeClasses,TypeSynonymInstances #-}
class (Num a, Integral b) => F a b | a -> b where
f :: a -> b
instance F Int Int where
f = id
instance F Float Integer where
f = truncate
And everything'll work perfectly.
> f (1 :: Int)
1
> f (1.9 :: Float)
1
But if I try to write something like
instance F Double String where
f = show
I'll get the following compilation error:
No instance for (Integral String)
arising from the superclasses of an instance declaration
Possible fix: add an instance declaration for (Integral String)
In the instance declaration for `F Double String'
Is there a way to approach this with type families instead of fundeps?