```{-# LANGUAGE TypeOperators #-}
----------------------------------------------------------------------
-- |
-- Copyright   :  (c) Conal Elliott and Andy J Gill 2008
--
-- Maintainer  :  conal@conal.net, andygill@ku.edu
-- Stability   :  experimental
--
----------------------------------------------------------------------

(
) where

import Control.Applicative
import Data.Monoid (Monoid(..))
import Data.Complex hiding (magnitude)

import Data.MemoTrie

infixl 6 ^+^, ^-^

-- | The zero element: identity for '(^+^)'
zeroV :: v
(^+^) :: v -> v -> v
negateV :: v -> v

-- | Group subtraction
(^-^) :: AdditiveGroup v => v -> v -> v
v ^-^ v' = v ^+^ negateV v'

-- | Sum over several vectors
sumV :: AdditiveGroup v => [v] -> v
sumV = foldr (^+^) zeroV

zeroV     = ()
() ^+^ () = ()
negateV   = id

zeroV   = 0.0
(^+^)   = (+)
negateV = negate

zeroV   = 0.0
(^+^)   = (+)
negateV = negate

zeroV   = zeroV :+ zeroV
(^+^)   = (+)
negateV = negate

-- Hm.  The 'RealFloat' constraint is unfortunate here.  It's due to a
-- questionable decision to place 'RealFloat' into the definition of the
-- 'Complex' /type/, rather than in functions and instances as needed.

zeroV             = (zeroV,zeroV)
(u,v) ^+^ (u',v') = (u^+^u',v^+^v')
negateV (u,v)     = (negateV u,negateV v)

zeroV                  = (zeroV,zeroV,zeroV)
(u,v,w) ^+^ (u',v',w') = (u^+^u',v^+^v',w^+^w')
negateV (u,v,w)        = (negateV u,negateV v,negateV w)

-- Standard instance for an applicative functor applied to a vector space.
zeroV   = pure   zeroV
(^+^)   = liftA2 (^+^)
negateV = fmap   negateV

-- Memo tries
zeroV   = pure   zeroV
(^+^)   = liftA2 (^+^)
negateV = fmap   negateV

-- | Monoid under group addition.  Alternative to the @Sum@ in
newtype Sum a = Sum a
deriving (Eq, Ord, Read, Show, Bounded)

instance Functor Sum where
fmap f (Sum a) = Sum (f a)

instance Applicative Sum where
pure a = Sum a
Sum f <*> Sum x = Sum (f x)

instance AdditiveGroup a => Monoid (Sum a) where
mempty  = Sum zeroV
mappend = liftA2 (^+^)

```