summaryrefslogtreecommitdiff
path: root/compiler/utils
diff options
context:
space:
mode:
authorJose Pedro Magalhaes <jpm@cs.uu.nl>2011-05-12 13:26:03 +0200
committerJose Pedro Magalhaes <jpm@cs.uu.nl>2011-05-12 13:26:03 +0200
commit1b381af863d64aaa0a4dd9c816170c58e6131a9e (patch)
tree52fa1f1af2d5256e5f475e3c6dd00630d53fb35d /compiler/utils
parentc25b934ef544fa3eba0a9f9da41b363c470156cb (diff)
parentc8c2f6bb7d79a2a6aeaa3233363fdf0bbbfad205 (diff)
downloadhaskell-1b381af863d64aaa0a4dd9c816170c58e6131a9e.tar.gz
Merge branch 'master' of http://darcs.haskell.org/ghc into ghc-generics
Resolved conflicts: compiler/typecheck/TcTyClsDecls.lhs
Diffstat (limited to 'compiler/utils')
-rw-r--r--compiler/utils/Pair.lhs47
1 files changed, 47 insertions, 0 deletions
diff --git a/compiler/utils/Pair.lhs b/compiler/utils/Pair.lhs
new file mode 100644
index 0000000000..9e847d6950
--- /dev/null
+++ b/compiler/utils/Pair.lhs
@@ -0,0 +1,47 @@
+
+A simple homogeneous pair type with useful Functor, Applicative, and
+Traversable instances.
+
+\begin{code}
+module Pair ( Pair(..), unPair, toPair, swap ) where
+
+#include "HsVersions.h"
+
+import Outputable
+import Data.Monoid
+import Control.Applicative
+import Data.Foldable
+import Data.Traversable
+
+data Pair a = Pair { pFst :: a, pSnd :: a }
+-- Note that Pair is a *unary* type constructor
+-- whereas (,) is binary
+
+-- The important thing about Pair is that it has a *homogenous*
+-- Functor instance, so you can easily apply the same function
+-- to both components
+instance Functor Pair where
+ fmap f (Pair x y) = Pair (f x) (f y)
+
+instance Applicative Pair where
+ pure x = Pair x x
+ (Pair f g) <*> (Pair x y) = Pair (f x) (g y)
+
+instance Foldable Pair where
+ foldMap f (Pair x y) = f x `mappend` f y
+
+instance Traversable Pair where
+ traverse f (Pair x y) = Pair <$> f x <*> f y
+
+instance Outputable a => Outputable (Pair a) where
+ ppr (Pair a b) = ppr a <+> char '~' <+> ppr b
+
+unPair :: Pair a -> (a,a)
+unPair (Pair x y) = (x,y)
+
+toPair :: (a,a) -> Pair a
+toPair (x,y) = Pair x y
+
+swap :: Pair a -> Pair a
+swap (Pair x y) = Pair y x
+\end{code} \ No newline at end of file