{-# LANGUAGE TypeOperators, CPP #-}
{-# LANGUAGE FlexibleInstances  #-}
{-# LANGUAGE FlexibleContexts   #-}
{-# LANGUAGE DefaultSignatures   #-}
{-# LANGUAGE ScopedTypeVariables #-}
----------------------------------------------------------------------
-- |
-- Module      :   Data.AdditiveGroup
-- Copyright   :  (c) Conal Elliott and Andy J Gill 2008
-- License     :  BSD3
--
-- Maintainer  :  conal@conal.net, andygill@ku.edu
-- Stability   :  experimental
--
-- Groups: zero, addition, and negation (additive inverse)
----------------------------------------------------------------------

module Data.AdditiveGroup
  (
    AdditiveGroup(..), sumV
  , Sum(..), inSum, inSum2
  ) where

import Prelude hiding (foldr)

import Control.Applicative
#if !(MIN_VERSION_base(4,8,0))
import Data.Monoid (Monoid(..))
import Data.Foldable (Foldable)
#endif
import Data.Foldable (foldr)
import Data.Complex hiding (magnitude)
import Data.Ratio
#if !(MIN_VERSION_base(4,11,0))
import Data.Semigroup (Semigroup(..))
#endif
import Foreign.C.Types (CSChar, CInt, CShort, CLong, CLLong, CIntMax, CFloat, CDouble)

import Data.MemoTrie

import Data.VectorSpace.Generic
import qualified GHC.Generics as Gnrx
import GHC.Generics (Generic, (:*:)(..))

infixl 6 ^+^, ^-^

-- | Additive group @v@.
class AdditiveGroup v where
  -- | The zero element: identity for '(^+^)'
  zeroV :: v
  default zeroV :: (Generic v, AdditiveGroup (VRep v)) => v
  zeroV = Rep v Void -> v
forall a x. Generic a => Rep a x -> a
Gnrx.to (Rep v Void
forall v. AdditiveGroup v => v
zeroV :: VRep v)
  {-# INLINE zeroV #-}
  -- | Add vectors
  (^+^) :: v -> v -> v
  default (^+^) :: (Generic v, AdditiveGroup (VRep v)) => v -> v -> v
  v
v ^+^ v
v' = Rep v Void -> v
forall a x. Generic a => Rep a x -> a
Gnrx.to (v -> Rep v Void
forall a x. Generic a => a -> Rep a x
Gnrx.from v
v Rep v Void -> Rep v Void -> Rep v Void
forall v. AdditiveGroup v => v -> v -> v
^+^ v -> Rep v Void
forall a x. Generic a => a -> Rep a x
Gnrx.from v
v' :: VRep v)
  {-# INLINE (^+^) #-}
  -- | Additive inverse
  negateV :: v -> v
  default negateV :: (Generic v, AdditiveGroup (VRep v)) => v -> v
  negateV v
v = Rep v Void -> v
forall a x. Generic a => Rep a x -> a
Gnrx.to (Rep v Void -> Rep v Void
forall v. AdditiveGroup v => v -> v
negateV (Rep v Void -> Rep v Void) -> Rep v Void -> Rep v Void
forall a b. (a -> b) -> a -> b
$ v -> Rep v Void
forall a x. Generic a => a -> Rep a x
Gnrx.from v
v :: VRep v)
  {-# INLINE negateV #-}
  -- | Group subtraction
  (^-^) :: v -> v -> v
  v
v ^-^ v
v' = v
v v -> v -> v
forall v. AdditiveGroup v => v -> v -> v
^+^ v -> v
forall v. AdditiveGroup v => v -> v
negateV v
v'

-- | Sum over several vectors
sumV :: (Foldable f, AdditiveGroup v) => f v -> v
sumV :: f v -> v
sumV = (v -> v -> v) -> v -> f v -> v
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr v -> v -> v
forall v. AdditiveGroup v => v -> v -> v
(^+^) v
forall v. AdditiveGroup v => v
zeroV
{-# INLINE sumV #-}

instance AdditiveGroup () where
  zeroV :: ()
zeroV     = ()
  () ^+^ :: () -> () -> ()
^+^ () = ()
  negateV :: () -> ()
negateV   = () -> ()
forall a. a -> a
id

-- For 'Num' types:
--
-- instance AdditiveGroup n where {zeroV=0; (^+^) = (+); negateV = negate}

#define ScalarTypeCon(con,t) \
  instance con => AdditiveGroup (t) where {zeroV=0; (^+^) = (+); negateV = negate}

#define ScalarType(t) ScalarTypeCon((),t)

ScalarType(Int)
ScalarType(Integer)
ScalarType(Float)
ScalarType(Double)
ScalarType(CSChar)
ScalarType(CInt)
ScalarType(CShort)
ScalarType(CLong)
ScalarType(CLLong)
ScalarType(CIntMax)
ScalarType(CFloat)
ScalarType(CDouble)
ScalarTypeCon(Integral a,Ratio a)

instance (RealFloat v, AdditiveGroup v) => AdditiveGroup (Complex v) where
  zeroV :: Complex v
zeroV   = v
forall v. AdditiveGroup v => v
zeroV v -> v -> Complex v
forall a. a -> a -> Complex a
:+ v
forall v. AdditiveGroup v => v
zeroV
  ^+^ :: Complex v -> Complex v -> Complex v
(^+^)   = Complex v -> Complex v -> Complex v
forall a. Num a => a -> a -> a
(+)
  negateV :: Complex v -> Complex v
negateV = Complex v -> Complex v
forall a. Num a => a -> a
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.

instance (AdditiveGroup u,AdditiveGroup v) => AdditiveGroup (u,v) where
  zeroV :: (u, v)
zeroV             = (u
forall v. AdditiveGroup v => v
zeroV,v
forall v. AdditiveGroup v => v
zeroV)
  (u
u,v
v) ^+^ :: (u, v) -> (u, v) -> (u, v)
^+^ (u
u',v
v') = (u
uu -> u -> u
forall v. AdditiveGroup v => v -> v -> v
^+^u
u',v
vv -> v -> v
forall v. AdditiveGroup v => v -> v -> v
^+^v
v')
  negateV :: (u, v) -> (u, v)
negateV (u
u,v
v)     = (u -> u
forall v. AdditiveGroup v => v -> v
negateV u
u,v -> v
forall v. AdditiveGroup v => v -> v
negateV v
v)

instance (AdditiveGroup u,AdditiveGroup v,AdditiveGroup w)
    => AdditiveGroup (u,v,w) where
  zeroV :: (u, v, w)
zeroV                  = (u
forall v. AdditiveGroup v => v
zeroV,v
forall v. AdditiveGroup v => v
zeroV,w
forall v. AdditiveGroup v => v
zeroV)
  (u
u,v
v,w
w) ^+^ :: (u, v, w) -> (u, v, w) -> (u, v, w)
^+^ (u
u',v
v',w
w') = (u
uu -> u -> u
forall v. AdditiveGroup v => v -> v -> v
^+^u
u',v
vv -> v -> v
forall v. AdditiveGroup v => v -> v -> v
^+^v
v',w
ww -> w -> w
forall v. AdditiveGroup v => v -> v -> v
^+^w
w')
  negateV :: (u, v, w) -> (u, v, w)
negateV (u
u,v
v,w
w)        = (u -> u
forall v. AdditiveGroup v => v -> v
negateV u
u,v -> v
forall v. AdditiveGroup v => v -> v
negateV v
v,w -> w
forall v. AdditiveGroup v => v -> v
negateV w
w)

instance (AdditiveGroup u,AdditiveGroup v,AdditiveGroup w,AdditiveGroup x)
    => AdditiveGroup (u,v,w,x) where
  zeroV :: (u, v, w, x)
zeroV                       = (u
forall v. AdditiveGroup v => v
zeroV,v
forall v. AdditiveGroup v => v
zeroV,w
forall v. AdditiveGroup v => v
zeroV,x
forall v. AdditiveGroup v => v
zeroV)
  (u
u,v
v,w
w,x
x) ^+^ :: (u, v, w, x) -> (u, v, w, x) -> (u, v, w, x)
^+^ (u
u',v
v',w
w',x
x') = (u
uu -> u -> u
forall v. AdditiveGroup v => v -> v -> v
^+^u
u',v
vv -> v -> v
forall v. AdditiveGroup v => v -> v -> v
^+^v
v',w
ww -> w -> w
forall v. AdditiveGroup v => v -> v -> v
^+^w
w',x
xx -> x -> x
forall v. AdditiveGroup v => v -> v -> v
^+^x
x')
  negateV :: (u, v, w, x) -> (u, v, w, x)
negateV (u
u,v
v,w
w,x
x)           = (u -> u
forall v. AdditiveGroup v => v -> v
negateV u
u,v -> v
forall v. AdditiveGroup v => v -> v
negateV v
v,w -> w
forall v. AdditiveGroup v => v -> v
negateV w
w,x -> x
forall v. AdditiveGroup v => v -> v
negateV x
x)


-- Standard instance for an applicative functor applied to a vector space.
instance AdditiveGroup v => AdditiveGroup (a -> v) where
  zeroV :: a -> v
zeroV   = v -> a -> v
forall (f :: * -> *) a. Applicative f => a -> f a
pure   v
forall v. AdditiveGroup v => v
zeroV
  ^+^ :: (a -> v) -> (a -> v) -> a -> v
(^+^)   = (v -> v -> v) -> (a -> v) -> (a -> v) -> a -> v
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 v -> v -> v
forall v. AdditiveGroup v => v -> v -> v
(^+^)
  negateV :: (a -> v) -> a -> v
negateV = (v -> v) -> (a -> v) -> a -> v
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap   v -> v
forall v. AdditiveGroup v => v -> v
negateV


-- Maybe is handled like the Maybe-of-Sum monoid
instance AdditiveGroup a => AdditiveGroup (Maybe a) where
  zeroV :: Maybe a
zeroV = Maybe a
forall a. Maybe a
Nothing
  Maybe a
Nothing ^+^ :: Maybe a -> Maybe a -> Maybe a
^+^ Maybe a
b'      = Maybe a
b'
  Maybe a
a' ^+^ Maybe a
Nothing      = Maybe a
a'
  Just a
a' ^+^ Just a
b' = a -> Maybe a
forall a. a -> Maybe a
Just (a
a' a -> a -> a
forall v. AdditiveGroup v => v -> v -> v
^+^ a
b')
  negateV :: Maybe a -> Maybe a
negateV = (a -> a) -> Maybe a -> Maybe a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall v. AdditiveGroup v => v -> v
negateV

{-

Alexey Khudyakov wrote:

  I looked through vector-space package and found lawless instance. Namely Maybe's AdditiveGroup instance

  It's group so following relation is expected to hold. Otherwise it's not a group.
  > x ^+^ negateV x == zeroV

  Here is counterexample:

  > let x = Just 2 in x ^+^ negateV x == zeroV
  False

  I think it's not possible to sensibly define group instance for
  Maybe a at all.


I see that the problem here is in distinguishing 'Just zeroV' from
Nothing. I could fix the Just + Just line to use Nothing instead of Just
zeroV when a' ^+^ b' == zeroV, although doing so would require Eq a and
hence lose some generality. Even so, the abstraction leak would probably
show up elsewhere.

Hm.

-}




-- Memo tries
instance (HasTrie u, AdditiveGroup v) => AdditiveGroup (u :->: v) where
  zeroV :: u :->: v
zeroV   = v -> u :->: v
forall (f :: * -> *) a. Applicative f => a -> f a
pure   v
forall v. AdditiveGroup v => v
zeroV
  ^+^ :: (u :->: v) -> (u :->: v) -> u :->: v
(^+^)   = (v -> v -> v) -> (u :->: v) -> (u :->: v) -> u :->: v
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 v -> v -> v
forall v. AdditiveGroup v => v -> v -> v
(^+^)
  negateV :: (u :->: v) -> u :->: v
negateV = (v -> v) -> (u :->: v) -> u :->: v
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap   v -> v
forall v. AdditiveGroup v => v -> v
negateV


-- | Monoid under group addition.  Alternative to the @Sum@ in
-- "Data.Monoid", which uses 'Num' instead of 'AdditiveGroup'.
newtype Sum a = Sum { Sum a -> a
getSum :: a }
  deriving (Sum a -> Sum a -> Bool
(Sum a -> Sum a -> Bool) -> (Sum a -> Sum a -> Bool) -> Eq (Sum a)
forall a. Eq a => Sum a -> Sum a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Sum a -> Sum a -> Bool
$c/= :: forall a. Eq a => Sum a -> Sum a -> Bool
== :: Sum a -> Sum a -> Bool
$c== :: forall a. Eq a => Sum a -> Sum a -> Bool
Eq, Eq (Sum a)
Eq (Sum a)
-> (Sum a -> Sum a -> Ordering)
-> (Sum a -> Sum a -> Bool)
-> (Sum a -> Sum a -> Bool)
-> (Sum a -> Sum a -> Bool)
-> (Sum a -> Sum a -> Bool)
-> (Sum a -> Sum a -> Sum a)
-> (Sum a -> Sum a -> Sum a)
-> Ord (Sum a)
Sum a -> Sum a -> Bool
Sum a -> Sum a -> Ordering
Sum a -> Sum a -> Sum a
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall a. Ord a => Eq (Sum a)
forall a. Ord a => Sum a -> Sum a -> Bool
forall a. Ord a => Sum a -> Sum a -> Ordering
forall a. Ord a => Sum a -> Sum a -> Sum a
min :: Sum a -> Sum a -> Sum a
$cmin :: forall a. Ord a => Sum a -> Sum a -> Sum a
max :: Sum a -> Sum a -> Sum a
$cmax :: forall a. Ord a => Sum a -> Sum a -> Sum a
>= :: Sum a -> Sum a -> Bool
$c>= :: forall a. Ord a => Sum a -> Sum a -> Bool
> :: Sum a -> Sum a -> Bool
$c> :: forall a. Ord a => Sum a -> Sum a -> Bool
<= :: Sum a -> Sum a -> Bool
$c<= :: forall a. Ord a => Sum a -> Sum a -> Bool
< :: Sum a -> Sum a -> Bool
$c< :: forall a. Ord a => Sum a -> Sum a -> Bool
compare :: Sum a -> Sum a -> Ordering
$ccompare :: forall a. Ord a => Sum a -> Sum a -> Ordering
$cp1Ord :: forall a. Ord a => Eq (Sum a)
Ord, ReadPrec [Sum a]
ReadPrec (Sum a)
Int -> ReadS (Sum a)
ReadS [Sum a]
(Int -> ReadS (Sum a))
-> ReadS [Sum a]
-> ReadPrec (Sum a)
-> ReadPrec [Sum a]
-> Read (Sum a)
forall a. Read a => ReadPrec [Sum a]
forall a. Read a => ReadPrec (Sum a)
forall a. Read a => Int -> ReadS (Sum a)
forall a. Read a => ReadS [Sum a]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Sum a]
$creadListPrec :: forall a. Read a => ReadPrec [Sum a]
readPrec :: ReadPrec (Sum a)
$creadPrec :: forall a. Read a => ReadPrec (Sum a)
readList :: ReadS [Sum a]
$creadList :: forall a. Read a => ReadS [Sum a]
readsPrec :: Int -> ReadS (Sum a)
$creadsPrec :: forall a. Read a => Int -> ReadS (Sum a)
Read, Int -> Sum a -> ShowS
[Sum a] -> ShowS
Sum a -> String
(Int -> Sum a -> ShowS)
-> (Sum a -> String) -> ([Sum a] -> ShowS) -> Show (Sum a)
forall a. Show a => Int -> Sum a -> ShowS
forall a. Show a => [Sum a] -> ShowS
forall a. Show a => Sum a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Sum a] -> ShowS
$cshowList :: forall a. Show a => [Sum a] -> ShowS
show :: Sum a -> String
$cshow :: forall a. Show a => Sum a -> String
showsPrec :: Int -> Sum a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> Sum a -> ShowS
Show, Sum a
Sum a -> Sum a -> Bounded (Sum a)
forall a. a -> a -> Bounded a
forall a. Bounded a => Sum a
maxBound :: Sum a
$cmaxBound :: forall a. Bounded a => Sum a
minBound :: Sum a
$cminBound :: forall a. Bounded a => Sum a
Bounded)

instance Functor Sum where
  fmap :: (a -> b) -> Sum a -> Sum b
fmap a -> b
f (Sum a
a) = b -> Sum b
forall a. a -> Sum a
Sum (a -> b
f a
a)
  {-# INLINE fmap #-}

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

instance Applicative Sum where
  pure :: a -> Sum a
pure  = a -> Sum a
forall a. a -> Sum a
Sum
  {-# INLINE pure #-}
  <*> :: Sum (a -> b) -> Sum a -> Sum b
(<*>) = ((a -> b) -> a -> b) -> Sum (a -> b) -> Sum a -> Sum b
forall a b c. (a -> b -> c) -> Sum a -> Sum b -> Sum c
inSum2 (a -> b) -> a -> b
forall a b. (a -> b) -> a -> b
($)
  {-# INLINE (<*>) #-}

instance AdditiveGroup a => Semigroup (Sum a) where
  <> :: Sum a -> Sum a -> Sum a
(<>) = (a -> a -> a) -> Sum a -> Sum a -> Sum a
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> a -> a
forall v. AdditiveGroup v => v -> v -> v
(^+^)
  {-# INLINE (<>) #-}

instance AdditiveGroup a => Monoid (Sum a) where
  mempty :: Sum a
mempty  = a -> Sum a
forall a. a -> Sum a
Sum a
forall v. AdditiveGroup v => v
zeroV
#if !(MIN_VERSION_base(4,11,0))
  mappend = (<>)
#endif

-- | Application a unary function inside a 'Sum'
inSum :: (a -> b) -> (Sum a -> Sum b)
inSum :: (a -> b) -> Sum a -> Sum b
inSum = Sum a -> a
forall a. Sum a -> a
getSum (Sum a -> a) -> (b -> Sum b) -> (a -> b) -> Sum a -> Sum b
forall a' a b b'. (a' -> a) -> (b -> b') -> (a -> b) -> a' -> b'
~> b -> Sum b
forall a. a -> Sum a
Sum
{-# INLINE inSum #-}

-- | Application a binary function inside a 'Sum'
inSum2 :: (a -> b -> c) -> (Sum a -> Sum b -> Sum c)
inSum2 :: (a -> b -> c) -> Sum a -> Sum b -> Sum c
inSum2 = Sum a -> a
forall a. Sum a -> a
getSum (Sum a -> a)
-> ((b -> c) -> Sum b -> Sum c)
-> (a -> b -> c)
-> Sum a
-> Sum b
-> Sum c
forall a' a b b'. (a' -> a) -> (b -> b') -> (a -> b) -> a' -> b'
~> (b -> c) -> Sum b -> Sum c
forall a b. (a -> b) -> Sum a -> Sum b
inSum
{-# INLINE inSum2 #-}

instance AdditiveGroup a => AdditiveGroup (Sum a) where
  zeroV :: Sum a
zeroV   = a -> Sum a
forall a. a -> Sum a
Sum a
forall v. AdditiveGroup v => v
zeroV
  ^+^ :: Sum a -> Sum a -> Sum a
(^+^)   = Sum a -> Sum a -> Sum a
forall a. Semigroup a => a -> a -> a
(<>)
  negateV :: Sum a -> Sum a
negateV = (a -> a) -> Sum a -> Sum a
forall a b. (a -> b) -> Sum a -> Sum b
inSum a -> a
forall v. AdditiveGroup v => v -> v
negateV

---- to go elsewhere

(~>) :: (a' -> a) -> (b -> b') -> ((a -> b) -> (a' -> b'))
(a' -> a
i ~> :: (a' -> a) -> (b -> b') -> (a -> b) -> a' -> b'
~> b -> b'
o) a -> b
f = b -> b'
o (b -> b') -> (a' -> b) -> a' -> b'
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f (a -> b) -> (a' -> a) -> a' -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a' -> a
i
{-# INLINE (~>) #-}

-- result :: (b -> b') -> ((a -> b) -> (a -> b'))
-- result = (.)

-- argument :: (a' -> a) -> ((a -> b) -> (a' -> b))
-- argument = flip (.)

-- g ~> f = result g . argument f



instance AdditiveGroup a => AdditiveGroup (Gnrx.Rec0 a s) where
  zeroV :: Rec0 a s
zeroV = a -> Rec0 a s
forall k i c (p :: k). c -> K1 i c p
Gnrx.K1 a
forall v. AdditiveGroup v => v
zeroV
  {-# INLINE zeroV #-}
  negateV :: Rec0 a s -> Rec0 a s
negateV (Gnrx.K1 a
v) = a -> Rec0 a s
forall k i c (p :: k). c -> K1 i c p
Gnrx.K1 (a -> Rec0 a s) -> a -> Rec0 a s
forall a b. (a -> b) -> a -> b
$ a -> a
forall v. AdditiveGroup v => v -> v
negateV a
v
  {-# INLINE negateV #-}
  Gnrx.K1 a
v ^+^ :: Rec0 a s -> Rec0 a s -> Rec0 a s
^+^ Gnrx.K1 a
w = a -> Rec0 a s
forall k i c (p :: k). c -> K1 i c p
Gnrx.K1 (a -> Rec0 a s) -> a -> Rec0 a s
forall a b. (a -> b) -> a -> b
$ a
v a -> a -> a
forall v. AdditiveGroup v => v -> v -> v
^+^ a
w
  {-# INLINE (^+^) #-}
  Gnrx.K1 a
v ^-^ :: Rec0 a s -> Rec0 a s -> Rec0 a s
^-^ Gnrx.K1 a
w = a -> Rec0 a s
forall k i c (p :: k). c -> K1 i c p
Gnrx.K1 (a -> Rec0 a s) -> a -> Rec0 a s
forall a b. (a -> b) -> a -> b
$ a
v a -> a -> a
forall v. AdditiveGroup v => v -> v -> v
^-^ a
w
  {-# INLINE (^-^) #-}
instance AdditiveGroup (f p) => AdditiveGroup (Gnrx.M1 i c f p) where
  zeroV :: M1 i c f p
zeroV = f p -> M1 i c f p
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
Gnrx.M1 f p
forall v. AdditiveGroup v => v
zeroV
  {-# INLINE zeroV #-}
  negateV :: M1 i c f p -> M1 i c f p
negateV (Gnrx.M1 f p
v) = f p -> M1 i c f p
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
Gnrx.M1 (f p -> M1 i c f p) -> f p -> M1 i c f p
forall a b. (a -> b) -> a -> b
$ f p -> f p
forall v. AdditiveGroup v => v -> v
negateV f p
v
  {-# INLINE negateV #-}
  Gnrx.M1 f p
v ^+^ :: M1 i c f p -> M1 i c f p -> M1 i c f p
^+^ Gnrx.M1 f p
w = f p -> M1 i c f p
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
Gnrx.M1 (f p -> M1 i c f p) -> f p -> M1 i c f p
forall a b. (a -> b) -> a -> b
$ f p
v f p -> f p -> f p
forall v. AdditiveGroup v => v -> v -> v
^+^ f p
w
  {-# INLINE (^+^) #-}
  Gnrx.M1 f p
v ^-^ :: M1 i c f p -> M1 i c f p -> M1 i c f p
^-^ Gnrx.M1 f p
w = f p -> M1 i c f p
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
Gnrx.M1 (f p -> M1 i c f p) -> f p -> M1 i c f p
forall a b. (a -> b) -> a -> b
$ f p
v f p -> f p -> f p
forall v. AdditiveGroup v => v -> v -> v
^-^ f p
w
  {-# INLINE (^-^) #-}
instance (AdditiveGroup (f p), AdditiveGroup (g p)) => AdditiveGroup ((f :*: g) p) where
  zeroV :: (:*:) f g p
zeroV = f p
forall v. AdditiveGroup v => v
zeroV f p -> g p -> (:*:) f g p
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: g p
forall v. AdditiveGroup v => v
zeroV
  {-# INLINE zeroV #-}
  negateV :: (:*:) f g p -> (:*:) f g p
negateV (f p
x:*:g p
y) = f p -> f p
forall v. AdditiveGroup v => v -> v
negateV f p
x f p -> g p -> (:*:) f g p
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: g p -> g p
forall v. AdditiveGroup v => v -> v
negateV g p
y
  {-# INLINE negateV #-}
  (f p
x:*:g p
y) ^+^ :: (:*:) f g p -> (:*:) f g p -> (:*:) f g p
^+^ (f p
ξ:*:g p
υ) = (f p
xf p -> f p -> f p
forall v. AdditiveGroup v => v -> v -> v
^+^f p
ξ) f p -> g p -> (:*:) f g p
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: (g p
yg p -> g p -> g p
forall v. AdditiveGroup v => v -> v -> v
^+^g p
υ)
  {-# INLINE (^+^) #-}
  (f p
x:*:g p
y) ^-^ :: (:*:) f g p -> (:*:) f g p -> (:*:) f g p
^-^ (f p
ξ:*:g p
υ) = (f p
xf p -> f p -> f p
forall v. AdditiveGroup v => v -> v -> v
^-^f p
ξ) f p -> g p -> (:*:) f g p
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: (g p
yg p -> g p -> g p
forall v. AdditiveGroup v => v -> v -> v
^-^g p
υ)
  {-# INLINE (^-^) #-}