Search code examples
haskellapplicative

Is there a family of Haskell functions for sequencing tuples of applicatives?


Is there a standard function, or family of functions, for sequencing tuples of applicatives, as a generalization of sequenceA? Like the following, except for all reasonable tuple lengths:

sequence3TupleA :: Applicative f => (f a1, f a2, f a3) -> f (a1, a2, a3)

I believe this should be possible to implement (it is for the applicatives I'm working with anyway).

I found SequenceT from Data.Tuple.Sequence, but it appears to require a monad and also it doesn't seem to actually contain the sequence function (I am probably misreading the documentation somehow).


Solution

  • The SequenceT typeclass and its method sequenceT from the tuple package does indeed do what you're looking for. Unfortunately, this package seems to have been abandoned since 2014, before the Applicative typeclass was introduced, so only works with Monad instances. An issue to sort this has been open since 2017, and an associated pull request was closed by the contributor.

    As @lsmor mentions in a comment, this function is not difficult, its definition is:

    sequence3TupleA (a, b, c) = (,,) <$> a <*> b <*> c