Search code examples
haskellmonadstraversal

How do you use two mappings to traverse a 2-tuple in Haskell?


I have a monad m that supports the following operation:

someName :: (t1 -> m u1) -> (t2 -> m u2) -> ((t1, t2) -> m (u1, u2))

In something more like English: given a mapping that could be used with bind to turn an m t1 into m u1 and another mapping for another pair of types, return such a mapping for pairs of the two types.

Does this concept have a name? Is it well-defined for all monads? Only some? None, and I have my facts wrong for the one I'm working on?


This is reminiscent of the traverse operation on Traversables, except there are two mappings involved. Plus, traverse for 2-tuples only seems to apply the mapping to the second element:

ghci> f a = Just (a + 1)
ghci> traverse f (0, 1)
Just (0,2)
ghci> traverse f ("Hello", 1)
Just ("Hello",2)

Solution

  • It's called bitraverse and comes standard with your favorite compiler.