Can I automatically implement classes?

后端 未结 2 1802
情歌与酒
情歌与酒 2021-01-17 21:42

In Scalaz every Monad instance is automatically an instance of Applicative.

implicit val listInstance = new Monad[List] {
  def po         


        
2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-17 22:02

    The problem comes when there are two places from which to derive the Applicative instance. For instance, suppose m is the type a b where Arrow a. Then there's an obvious instance of Applicative from this definition as well. Which one should the compiler use? It should work out the same, of course, but Haskell has no way to check this. By making us write out the instances, Haskell at least forces us to think about the consistency of our definitions.

    If you want, there's the WrappedMonad class in Control.Applicative, which provides all the obvious instances with a newtype wrapper, but using WrapMonad and unwrapMonad all the time isn't that attractive either.

提交回复
热议问题