What is the motivation of having functional dependencies in Haskell ?
One example of a functional dependency:
class (Monad m) => MonadSupply s m | m -> s where
next :: m (Maybe s)
It is stated in the RWH book, that functional dependency helps the type checker. How does it actually help ?
Also, this piece of code actually compiles:
class (Monad m) => MonadSupply s m where
next :: m (Maybe s)
But I guess, it will produce an runtime error.
It's perfectly fine to write code not using functional dependencies, it's just a pain to use since the inference sucks.
Basically without FDs, the function
get :: MonadState m s => m s
will have to figure outm
ands
independently. Usuallym
is quite easily inferred, but oftens
would require an explicit annotation.Moreover, this is much more general than we need, so instead we can restrict our typechecker to say "For
m
, there is exactly 1s
", this way, oncem
is inferred,s
is obvious to the type inference algorithm