How exactly does the `(<*>) = ap` Applicative/Monad law relate the two classes?

前端 未结 2 437
旧时难觅i
旧时难觅i 2021-01-18 23:37

ap doesn\'t have a documented spec, and reads with a comment pointing out it could be <*>, but isn\'t for practical reasons:

         


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

    Every Monad gives rise to an Applicative, and for that induced Applicative, <*> = ap will hold definitionally. But given two structures - Monad m and Applicative m - there is no guarantee that these structures agree without the two laws <*> = ap and pure = return. For example, take the 'regular' Monad instance for lists, and the zip-list Applicative instance. While there is nothing fundamentally 'wrong' about a Monad and Applicative instance disagreeing, it would probably be confusing to most users, and so it's prohibited by the Monad laws.

    tl;dr The laws in question serve to ensure that Monad and Applicative agree in an intuitively obvious way.

提交回复
热议问题