what does the A stand for in sequenceA?

前端 未结 1 1828
感动是毒
感动是毒 2021-01-19 09:39

What does sequenceA from Traversable stand for? Why is there capital A at the end? I\'ve been learning Haskell for a few months now and this is one of those thi

相关标签:
1条回答
  • 2021-01-19 10:03

    The "A" stands for Applicative, as in the constraint in sequenceA's type:

    sequenceA :: (Traversable t, Applicative f) => t (f a) -> f (t a)
    

    That the "A" is there is fruit of a historical accident. Once upon a time, neither Applicative nor Traversable existed in Haskell. Nonetheless, a function exactly like sequenceA already existed -- except that it had a much more specific type:

    sequence :: Monad m => [m a] -> m [a]
    

    When Applicative and Traversable were introduced, the function was generalised from lists to any Traversable [1]:

    sequence :: (Traversable t, Monad m) => t (m a) -> m (t a)
    

    sequence's Monad constraint is unnecessarily restrictive. Back then, however, generalising it further to Applicative was not an option. The problem was that until early last year Applicative was not a superclass of Monad as it is supposed to be, and therefore generalising the signature to Applicative would break any use of sequence with monads that lacked an Applicative instance. That being so, an extra "A" was added to the name of the general version.


    [1]: Note, however, that the Prelude continued to carry the list-specific version until quite recently.

    0 讨论(0)
提交回复
热议问题