From 06c8df4932b9a46e48b3fc99c3bc44e787b182fc Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Sat, 13 Oct 2012 22:36:49 -0400 Subject: [PATCH] Begin work on a simple 2-tuple type. --- src/TwoTuple.hs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/TwoTuple.hs diff --git a/src/TwoTuple.hs b/src/TwoTuple.hs new file mode 100644 index 0000000..8493114 --- /dev/null +++ b/src/TwoTuple.hs @@ -0,0 +1,31 @@ +-- | Implement ordered pairs all over again for fun (and to make sure +-- that we can manipulate them algebraically). +-- +module TwoTuple +where + +data TwoTuple a = TwoTuple a a + deriving (Eq, Show) + +instance Functor TwoTuple where + f `fmap` (TwoTuple x1 y1) = TwoTuple (f x1) (f y1) + + +instance Num a => Num (TwoTuple a) where + -- Standard componentwise addition. + (TwoTuple x1 y1) + (TwoTuple x2 y2) = + TwoTuple (x1 + x2) (y1 + y2) + + -- Standard componentwise subtraction. + (TwoTuple x1 y1) - (TwoTuple x2 y2) = + TwoTuple (x1 - x2) (y1 - y2) + + -- Left undefined to prevent mistakes. One sane definition + -- would be componentwise multiplication. + (*) _ _ = error "multiplication of vectors is undefined" + + abs _ = error "absolute value of vectors is undefined" + + signum _ = error "signum of vectors is undefined" + + fromInteger x = TwoTuple (fromInteger x) (fromInteger x) -- 2.43.2