In chapter 15 of Rea开发者_如何学Pythonl 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 class
and =>
, which in this case is the constraint Monad m
. And it's not so much that it "knows", more that it enforces it--writing an instance of MonadSupply
for a type m
that doesn't also have a Monad
instance will produce a compiler error.
精彩评论