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

前端 未结 2 439
旧时难觅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:13

    So I assume the ap in the (<*>) = ap law is shorthand for "right-hand side of ap" and the law actually expresses a relationship between >>=, return and <*> right?

    It seems to me (<*>) = ap doesn't strictly imply anything (at least post-AMP). Presumably it's trying to express some relationship between <*> and the right-hand side of ap. Maybe I'm being pedantic.

    Speaking pedantically, I'd say the opposite: because ap is definitionally equal to its right-hand side, saying (<*>) = ap is exactly the same as saying m1 <*> m2 = do { x1 <- m1; x2 <- m2; return (x1 x2) }. It's just the normal first step of dealing with equalities like that: expanding the definitions.

    Reply to the comment:

    Right, but the definition is free to change.

    Then the law would change or be removed too. Just as when/if join is added to Monad the current definition will become a law instead.

    it wouldn't have been possible to define it literally as ap = <*>

    Do you mean it would be impossible to define ap or the law in this way?

    If ap, then you are correct: it would have the wrong type. But stating the law like this would be fine.

提交回复
热议问题