Reputation: 48644
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.
Upvotes: 8
Views: 396
Reputation: 53881
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 out m
and s
independently. Usually m
is quite easily inferred, but often s
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 1 s
", this way, once m
is inferred, s
is obvious to the type inference algorithm
Upvotes: 8