{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE StandaloneDeriving #-}
module OAlg.Structure.Additive.Definition
(
Additive(..), zero', Add, ForgetfulAdd
, Abelian(..), isZero, Abl, ForgetfulAbl
)
where
import qualified Prelude as A
import Control.Exception
import Data.List(repeat)
import Data.Foldable
import OAlg.Prelude
import OAlg.Data.Canonical
import OAlg.Structure.Exception
import OAlg.Structure.Oriented.Definition
import OAlg.Structure.Multiplicative.Definition
import OAlg.Structure.Fibred.Definition
infixl 6 +
class Fibred a => Additive a where
{-# MINIMAL zero, (+) #-}
zero :: Root a -> a
(+) :: a -> a -> a
ntimes :: N -> a -> a
ntimes N
n a
f = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr forall a. Additive a => a -> a -> a
(+) (forall a. Additive a => Root a -> a
zero (forall f. Fibred f => f -> Root f
root a
f)) forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall a. N -> [a] -> [a]
takeN N
n forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall a. a -> [a]
repeat forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ a
f
instance Additive () where
zero :: Root () -> ()
zero Root ()
_ = ()
()
_ + :: () -> () -> ()
+ ()
_ = ()
ntimes :: N -> () -> ()
ntimes N
_ ()
_ = ()
instance Additive Int where
zero :: Root Int -> Int
zero Root Int
_ = Int
0
+ :: Int -> Int -> Int
(+) = forall a. Num a => a -> a -> a
(A.+)
instance Additive Integer where
zero :: Root Integer -> Integer
zero Root Integer
_ = Integer
0
+ :: Integer -> Integer -> Integer
(+) = forall a. Num a => a -> a -> a
(A.+)
ntimes :: N -> Integer -> Integer
ntimes N
n Integer
z = forall a b. Embeddable a b => a -> b
inj N
n forall c. Multiplicative c => c -> c -> c
* Integer
z
instance Additive N where
zero :: Root N -> N
zero Root N
_ = N
0
+ :: N -> N -> N
(+) = forall a. Num a => a -> a -> a
(A.+)
ntimes :: N -> N -> N
ntimes = forall c. Multiplicative c => c -> c -> c
(*)
instance Additive Z where
zero :: Root Z -> Z
zero Root Z
_ = Z
0
+ :: Z -> Z -> Z
(+) = forall a. Num a => a -> a -> a
(A.+)
ntimes :: N -> Z -> Z
ntimes N
n Z
z = forall a b. Embeddable a b => a -> b
inj N
n forall c. Multiplicative c => c -> c -> c
* Z
z
instance Additive Q where
zero :: Root Q -> Q
zero Root Q
_ = Q
0
+ :: Q -> Q -> Q
(+) = forall a. Num a => a -> a -> a
(A.+)
ntimes :: N -> Q -> Q
ntimes N
n Q
q = forall a b. Embeddable a b => a -> b
inj N
n forall c. Multiplicative c => c -> c -> c
* Q
q
instance Entity p => Additive (Orientation p) where
zero :: Root (Orientation p) -> Orientation p
zero = forall x. x -> x
id
Orientation p
a + :: Orientation p -> Orientation p -> Orientation p
+ Orientation p
b | Orientation p
a forall a. Eq a => a -> a -> Bool
== Orientation p
b = Orientation p
a
| Bool
otherwise = forall a e. Exception e => e -> a
throw ArithmeticException
NotAddable
ntimes :: N -> Orientation p -> Orientation p
ntimes N
_ Orientation p
a = Orientation p
a
instance (Additive a, FibredOriented a) => Additive (Op a) where
zero :: Root (Op a) -> Op a
zero Root (Op a)
r = forall x. x -> Op x
Op (forall a. Additive a => Root a -> a
zero forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall x. Transposable x => x -> x
transpose Root (Op a)
r)
Op a
a + :: Op a -> Op a -> Op a
+ Op a
b = forall x. x -> Op x
Op (a
a forall a. Additive a => a -> a -> a
+ a
b)
ntimes :: N -> Op a -> Op a
ntimes N
n (Op a
a) = forall x. x -> Op x
Op (forall a. Additive a => N -> a -> a
ntimes N
n a
a)
instance Additive a => Projectible a (Sheaf a) where
prj :: Sheaf a -> a
prj Sheaf a
shf = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr forall a. Additive a => a -> a -> a
(+) (forall a. Additive a => Root a -> a
zero (forall f. Fibred f => f -> Root f
root Sheaf a
shf)) Sheaf a
shf
zero' :: Additive a => p a -> Root a -> a
zero' :: forall a (p :: * -> *). Additive a => p a -> Root a -> a
zero' p a
_ = forall a. Additive a => Root a -> a
zero
isZero :: Additive a => a -> Bool
isZero :: forall a. Additive a => a -> Bool
isZero a
a = a
a forall a. Eq a => a -> a -> Bool
== forall a. Additive a => Root a -> a
zero (forall f. Fibred f => f -> Root f
root a
a)
infixl 6 -
class Additive a => Abelian a where
{-# MINIMAL negate | (-) #-}
negate :: a -> a
negate a
f = forall a. Additive a => Root a -> a
zero (forall f. Fibred f => f -> Root f
root a
f) forall a. Abelian a => a -> a -> a
- a
f
(-) :: a -> a -> a
a
f - a
g = a
f forall a. Additive a => a -> a -> a
+ forall a. Abelian a => a -> a
negate a
g
ztimes :: Z -> a -> a
ztimes Z
z a
f = forall a. Additive a => N -> a -> a
ntimes (forall a b. Projectible a b => b -> a
prj Z
z) a
f' where f' :: a
f' = if Z
z forall a. Ord a => a -> a -> Bool
< Z
0 then forall a. Abelian a => a -> a
negate a
f else a
f
instance Abelian () where
negate :: () -> ()
negate ()
_ = ()
()
_ - :: () -> () -> ()
- ()
_ = ()
ztimes :: Z -> () -> ()
ztimes Z
_ ()
_ = ()
instance Abelian Int where
negate :: Int -> Int
negate = forall a. Num a => a -> a
A.negate
(-) = forall a. Num a => a -> a -> a
(A.-)
instance Abelian Integer where
negate :: Integer -> Integer
negate = forall a. Num a => a -> a
A.negate
(-) = forall a. Num a => a -> a -> a
(A.-)
ztimes :: Z -> Integer -> Integer
ztimes Z
z Integer
i = forall a b. Projectible a b => b -> a
prj Z
z forall c. Multiplicative c => c -> c -> c
* Integer
i
instance Abelian Z where
negate :: Z -> Z
negate = forall a. Num a => a -> a
A.negate
(-) = forall a. Num a => a -> a -> a
(A.-)
ztimes :: Z -> Z -> Z
ztimes = forall c. Multiplicative c => c -> c -> c
(*)
instance Abelian Q where
negate :: Q -> Q
negate = forall a. Num a => a -> a
A.negate
(-) = forall a. Num a => a -> a -> a
(A.-)
ztimes :: Z -> Q -> Q
ztimes Z
z Q
i = forall a b. Embeddable a b => a -> b
inj Z
z forall c. Multiplicative c => c -> c -> c
* Q
i
instance Entity p => Abelian (Orientation p) where
negate :: Orientation p -> Orientation p
negate = forall x. x -> x
id
instance (Abelian a, FibredOriented a) => Abelian (Op a) where
negate :: Op a -> Op a
negate (Op a
x) = forall x. x -> Op x
Op (forall a. Abelian a => a -> a
negate a
x)
Op a
a - :: Op a -> Op a -> Op a
- Op a
b = forall x. x -> Op x
Op (a
aforall a. Abelian a => a -> a -> a
-a
b)
ztimes :: Z -> Op a -> Op a
ztimes Z
z (Op a
a) = forall x. x -> Op x
Op (forall a. Abelian a => Z -> a -> a
ztimes Z
z a
a)
data Add
type instance Structure Add x = Additive x
instance Transformable Add Typ where tau :: forall x. Struct Add x -> Struct Typ x
tau Struct Add x
Struct = forall s x. Structure s x => Struct s x
Struct
instance Transformable Add Ent where tau :: forall x. Struct Add x -> Struct Ent x
tau Struct Add x
Struct = forall s x. Structure s x => Struct s x
Struct
instance Transformable Add Fbr where tau :: forall x. Struct Add x -> Struct Fbr x
tau Struct Add x
Struct = forall s x. Structure s x => Struct s x
Struct
class (ForgetfulFbr s, Transformable s Add) => ForgetfulAdd s
instance ForgetfulTyp Add
instance ForgetfulFbr Add
instance ForgetfulAdd Add
data Abl
type instance Structure Abl x = Abelian x
instance Transformable Abl Typ where tau :: forall x. Struct Abl x -> Struct Typ x
tau Struct Abl x
Struct = forall s x. Structure s x => Struct s x
Struct
instance Transformable Abl Ent where tau :: forall x. Struct Abl x -> Struct Ent x
tau Struct Abl x
Struct = forall s x. Structure s x => Struct s x
Struct
instance Transformable Abl Fbr where tau :: forall x. Struct Abl x -> Struct Fbr x
tau Struct Abl x
Struct = forall s x. Structure s x => Struct s x
Struct
instance Transformable Abl Add where tau :: forall x. Struct Abl x -> Struct Add x
tau Struct Abl x
Struct = forall s x. Structure s x => Struct s x
Struct
class (ForgetfulFbr s, ForgetfulAdd s, Transformable s Abl) => ForgetfulAbl s
instance ForgetfulTyp Abl
instance ForgetfulFbr Abl
instance ForgetfulAdd Abl
instance ForgetfulAbl Abl