Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
在 Real World Haskell 的第 15 章中,定义了一个类型类:
class (Monad m) => MonadSupply s m | m -> s where
几段后,它说 >>= 和 return 不需要定义,因为上下文。但是没有进一步解释上下文的含义。
如果只有“m”是 Monad 的实例,编译器如何知道 MonadSupply 是 Monad 的实例?
“上下文”只是和之间的部分class,=>在这种情况下是约束Monad m。与其说它“知道”,不如说它强制执行它——为没有实例的类型编写实例MonadSupply会产生编译器错误。mMonad
class
=>
Monad m
MonadSupply
m
Monad