Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Short way to write (\(x, y) -> (f x, g y))

Tags:

haskell

tuples

For functions f :: a → b, g :: c → d, how do I write the lambda

\(x, y) → (f x, g y) :: (a, b) → (c, d)

more concisely? I tried (f, g) but – as was to be expected I guess – without success.

like image 328
fabian789 Avatar asked Aug 09 '16 11:08

fabian789


2 Answers

The Bifunctor instance of (,) is what you are looking for:

instance Bifunctor (,) where
  bimap f g (a, b) = (f a, g b)

bimap applies two functions to a tuple, one to each element.

> import Data.Bifunctor
> bimap (+1) (*5) (1,1)
(2, 5)

You might be wondering what the difference between bimap and (***) is.

> :t bimap
bimap :: Bifunctor p => (a -> b) -> (c -> d) -> p a c -> p b d
> :t (***)
(***) :: Arrow a => a b c -> a b' c' -> a (b, b') (c, c')

With bimap, you can restrict the type to tuples rather than an arbitrary bifunctor p, so that with p ~ (,), the type of bimap becomes

 (a -> b) -> (c -> d) -> (a, c) -> (b, d).

With (***), you can restrict the type to functions rather than an arbitrary arrow a, so that with a ~ (->), the type of (***) becomes

 (b -> c) -> (b' -> c') -> (b, b') -> (c, c')

A close look reveals the two restricted types are equivalent.

like image 64
chepner Avatar answered Oct 29 '22 19:10

chepner


You can use (***) from Control.Arrow i.e.

f *** g
like image 9
Lee Avatar answered Oct 29 '22 17:10

Lee