开发者

What is context in Haskell multiparameter type class

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜