
On Mon, Nov 3, 2008 at 2:55 PM, Daniel Fischer
class Multiplicable a b c where mult :: a -> b -> c
Use functional dependencies {-# LANGUAGE FunctionalDependencies #-},
class Multiplicable a b c | a b -> c where ...
which states that the result type of multiplication is determined by the argument types
or type families
That's exactly what I was looking for, thank you. Now that I got that working, I've noticed that it can be tedious making sure the arguments to sumProduct are in the correct order. Since multiplication is commutative, is there any way of automatically having the Multiplicable instances generate a "flip" mult?
sumProduct :: (Addable c, Multiplicable a b c) => [a] -> [b] -> c
sumProduct x y = sum $ product x y
weightedAverage x y = (sumProduct y x) `divide` (sum y)
class Dividable a b c where divide :: c -> a -> b
FunDep here, too, but which one?
I did class Dividable a b c | c a -> b where... Michael