In chapter 15 of Real World Haskell, a type class is defined:
class (Monad m) => MonadSupply s m | m -> s where
A couple paragraphs later, it says that >>= and return don’t need to be defined because of the context. But there’s no further explanation of what it means by context.
How does the compiler know MonadSupply is an instance of Monad if only ‘m’ is an instance of Monad?
The “context” is just the part between
classand=>, which in this case is the constraintMonad m. And it’s not so much that it “knows”, more that it enforces it–writing an instance ofMonadSupplyfor a typemthat doesn’t also have aMonadinstance will produce a compiler error.