views:

72

answers:

1

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?

+3  A: 

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.

camccann
Ah, yes. I missed the part where it said that being a monad was a pre-requisite. Thanks for the fast answer :)