Haskell - Is effect order deterministic in case of Applicative?

后端 未结 3 1688
耶瑟儿~
耶瑟儿~ 2021-01-01 21:03

When executing the IO action defined by someFun <$> (a :: IO ()) <$> (b :: IO ()), is the execution of the a and b actions

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-01 21:26

    Yes, the order is predefined by the Monad-Applicative correspondence. This is easy to see: The (*>) combinator needs to correspond to the (>>) combinator in a well-behaved Applicative instance for a monad, and its definition is:

    a *> b = liftA2 (const id) a b
    

    In other words, if b were executed before a, the Applicative instance would be ill-behaving.

    Edit: As a side note: This is not explicitly specified anywhere, but you can find many other similar correspondences like liftM2 = liftA2, etc.

提交回复
热议问题