Remove very dead Java backend code.
[ghc-hetmet.git] / compiler / utils / Pair.lhs
1
2 A simple homogeneous pair type with useful Functor, Applicative, and
3 Traversable instances.
4
5 \begin{code}
6 module Pair ( Pair(..), unPair, toPair, swap ) where
7
8 #include "HsVersions.h"
9
10 import Outputable
11 import Data.Monoid
12 import Control.Applicative
13 import Data.Foldable
14 import Data.Traversable
15
16 data Pair a = Pair { pFst :: a, pSnd :: a }
17 -- Note that Pair is a *unary* type constructor
18 -- whereas (,) is binary
19
20 -- The important thing about Pair is that it has a *homogenous*
21 -- Functor instance, so you can easily apply the same function
22 -- to both components
23 instance Functor Pair where
24   fmap f (Pair x y) = Pair (f x) (f y)
25
26 instance Applicative Pair where
27   pure x = Pair x x
28   (Pair f g) <*> (Pair x y) = Pair (f x) (g y)
29
30 instance Foldable Pair where
31   foldMap f (Pair x y) = f x `mappend` f y
32
33 instance Traversable Pair where
34   traverse f (Pair x y) = Pair <$> f x <*> f y
35
36 instance Outputable a => Outputable (Pair a) where
37   ppr (Pair a b) = ppr a <+> char '~' <+> ppr b
38
39 unPair :: Pair a -> (a,a)
40 unPair (Pair x y) = (x,y)
41
42 toPair :: (a,a) -> Pair a
43 toPair (x,y) = Pair x y
44
45 swap :: Pair a -> Pair a
46 swap (Pair x y) = Pair y x
47 \end{code}