{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE DataKinds #-}
module OAlg.Structure.Multiplicative.Definition
(
Multiplicative(..), one', isOne, Mlt, ForgetfulMlt
, TransposableMultiplicative
, Commutative
, Invertible(..), Inv(..)
, Cayleyan
, xosPathAt, xosPath, xosXOrtSitePath
)
where
import qualified Prelude as A
import Control.Monad
import Control.Exception
import Data.List(repeat)
import Data.Foldable
import OAlg.Control.Solver
import OAlg.Prelude
import OAlg.Data.Canonical
import OAlg.Structure.Exception
import OAlg.Structure.Oriented
infixl 7 *
class Oriented c => Multiplicative c where
{-# MINIMAL one,(*) #-}
one :: Point c -> c
(*) :: c -> c -> c
npower :: c -> N -> c
npower c
f N
1 = c
f
npower c
f N
_ | forall b. Boolean b => b -> b
not (forall q. Oriented q => q -> Bool
isEndo c
f) = forall a e. Exception e => e -> a
throw ArithmeticException
NotExponential
npower c
f N
n = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr forall c. Multiplicative c => c -> c -> c
(*) (forall c. Multiplicative c => Point c -> c
one (forall q. Oriented q => q -> Point q
start c
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
$ c
f
instance Multiplicative () where
one :: Point () -> ()
one Point ()
_ = ()
()
_ * :: () -> () -> ()
* ()
_ = ()
npower :: () -> N -> ()
npower ()
_ N
_ = ()
instance Multiplicative Int where
one :: Point Int -> Int
one Point Int
_ = Int
1
* :: Int -> Int -> Int
(*) = forall a. Num a => a -> a -> a
(A.*)
instance Multiplicative Integer where
one :: Point Integer -> Integer
one Point Integer
_ = Integer
1
* :: Integer -> Integer -> Integer
(*) = forall a. Num a => a -> a -> a
(A.*)
instance Multiplicative N where
one :: Point N -> N
one Point N
_ = N
1
* :: N -> N -> N
(*) = forall a. Num a => a -> a -> a
(A.*)
instance Multiplicative Z where
one :: Point Z -> Z
one Point Z
_ = Z
1
* :: Z -> Z -> Z
(*) = forall a. Num a => a -> a -> a
(A.*)
instance Multiplicative Q where
one :: Point Q -> Q
one Point Q
_ = Q
1
* :: Q -> Q -> Q
(*) = forall a. Num a => a -> a -> a
(A.*)
instance Entity p => Multiplicative (Orientation p) where
one :: Point (Orientation p) -> Orientation p
one Point (Orientation p)
p = Point (Orientation p)
p forall p. p -> p -> Orientation p
:> Point (Orientation p)
p
(p
c :> p
d) * :: Orientation p -> Orientation p -> Orientation p
* (p
a :> p
b) | p
b forall a. Eq a => a -> a -> Bool
== p
c = p
a forall p. p -> p -> Orientation p
:> p
d
| Bool
otherwise = forall a e. Exception e => e -> a
throw ArithmeticException
NotMultiplicable
npower :: Orientation p -> N -> Orientation p
npower Orientation p
o N
1 = Orientation p
o
npower Orientation p
o N
_ | forall q. Oriented q => q -> Bool
isEndo Orientation p
o = Orientation p
o
| Bool
otherwise = forall a e. Exception e => e -> a
throw ArithmeticException
NotExponential
instance Oriented q => Multiplicative (Path q) where
one :: Point (Path q) -> Path q
one = forall q. Point q -> Path q
pthOne
* :: Path q -> Path q -> Path q
(*) = forall q. Oriented q => Path q -> Path q -> Path q
pthMlt
instance Multiplicative c => Multiplicative (Op c) where
one :: Point (Op c) -> Op c
one = forall x. x -> Op x
Op forall (c :: * -> * -> *) y z x.
Category c =>
c y z -> c x y -> c x z
. forall c. Multiplicative c => Point c -> c
one
Op c
f * :: Op c -> Op c -> Op c
* Op c
g = forall x. x -> Op x
Op (c
g forall c. Multiplicative c => c -> c -> c
* c
f)
npower :: Op c -> N -> Op c
npower (Op c
f) N
n = forall x. x -> Op x
Op (forall c. Multiplicative c => c -> N -> c
npower c
f N
n)
instance Multiplicative c => Projectible c (Path c) where
prj :: Path c -> c
prj Path c
pth = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr forall c. Multiplicative c => c -> c -> c
(*) (forall c. Multiplicative c => Point c -> c
one (forall q. Oriented q => q -> Point q
start Path c
pth)) Path c
pth
one' :: Multiplicative c => p c -> Point c -> c
one' :: forall c (p :: * -> *). Multiplicative c => p c -> Point c -> c
one' p c
_ = forall c. Multiplicative c => Point c -> c
one
isOne :: Multiplicative c => c -> Bool
isOne :: forall c. Multiplicative c => c -> Bool
isOne c
f = c
f forall a. Eq a => a -> a -> Bool
== forall c. Multiplicative c => Point c -> c
one (forall q. Oriented q => q -> Point q
end c
f)
class (TransposableOriented c, Multiplicative c) => TransposableMultiplicative c
instance Entity p => TransposableMultiplicative (Orientation p)
instance TransposableMultiplicative N
instance TransposableMultiplicative Z
instance TransposableMultiplicative Q
class Multiplicative c => Commutative c
instance Commutative ()
instance Commutative Int
instance Commutative Integer
instance Commutative N
instance Commutative Z
instance Commutative Q
instance Commutative c => Commutative (Op c)
class Multiplicative c => Invertible c where
{-# MINIMAL tryToInvert #-}
tryToInvert :: c -> Solver c
invert :: c -> c
invert = forall x. Solver x -> x
solve forall (c :: * -> * -> *) y z x.
Category c =>
c y z -> c x y -> c x z
. forall c. Invertible c => c -> Solver c
tryToInvert
isInvertible :: c -> Bool
isInvertible = forall r. Solver r -> Bool
solvable forall (c :: * -> * -> *) y z x.
Category c =>
c y z -> c x y -> c x z
. forall c. Invertible c => c -> Solver c
tryToInvert
zpower :: c -> Z -> c
zpower c
f Z
z = forall c. Multiplicative c => c -> N -> c
npower c
f' (forall a b. Projectible a b => b -> a
prj Z
z) where f' :: c
f' = if Z
z forall a. Ord a => a -> a -> Bool
< Z
0 then forall c. Invertible c => c -> c
invert c
f else c
f
instance Invertible () where
tryToInvert :: () -> Solver ()
tryToInvert ()
_ = forall (m :: * -> *) a. Monad m => a -> m a
return ()
instance Invertible Int where
tryToInvert :: Int -> Solver Int
tryToInvert Int
n = if forall a. Num a => a -> a
A.abs Int
n forall a. Eq a => a -> a -> Bool
== Int
1 then forall (m :: * -> *) a. Monad m => a -> m a
return Int
n else forall e x. Exception e => e -> Solver x
failure ArithmeticException
NotInvertible
instance Invertible Integer where
tryToInvert :: Integer -> Solver Integer
tryToInvert Integer
z = if forall a. Num a => a -> a
A.abs Integer
z forall a. Eq a => a -> a -> Bool
== Integer
1 then forall (m :: * -> *) a. Monad m => a -> m a
return Integer
z else forall e x. Exception e => e -> Solver x
failure ArithmeticException
NotInvertible
instance Invertible N where
tryToInvert :: N -> Solver N
tryToInvert N
n = if N
n forall a. Eq a => a -> a -> Bool
== N
1 then forall (m :: * -> *) a. Monad m => a -> m a
return N
1 else forall e x. Exception e => e -> Solver x
failure ArithmeticException
NotInvertible
instance Invertible Z where
tryToInvert :: Z -> Solver Z
tryToInvert Z
z = if forall a. Num a => a -> a
A.abs Z
z forall a. Eq a => a -> a -> Bool
== Z
1 then forall (m :: * -> *) a. Monad m => a -> m a
return Z
z else forall e x. Exception e => e -> Solver x
failure ArithmeticException
NotInvertible
instance Invertible Q where
tryToInvert :: Q -> Solver Q
tryToInvert Q
q = if Q
q forall a. Eq a => a -> a -> Bool
== Q
0 then forall e x. Exception e => e -> Solver x
failure ArithmeticException
NotInvertible else forall (m :: * -> *) a. Monad m => a -> m a
return (Q
1 forall a. Fractional a => a -> a -> a
A./ Q
q)
instance Entity p => Invertible (Orientation p) where
tryToInvert :: Orientation p -> Solver (Orientation p)
tryToInvert = forall (m :: * -> *) a. Monad m => a -> m a
return forall (c :: * -> * -> *) y z x.
Category c =>
c y z -> c x y -> c x z
. forall x. Transposable x => x -> x
transpose
instance Invertible c => Invertible (Op c) where
tryToInvert :: Op c -> Solver (Op c)
tryToInvert (Op c
f) = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall x. x -> Op x
Op forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall c. Invertible c => c -> Solver c
tryToInvert c
f
class Invertible c => Cayleyan c
instance Cayleyan ()
instance Entity p => Cayleyan (Orientation p)
instance Cayleyan c => Cayleyan (Op c)
data Inv c = Inv c c deriving (Int -> Inv c -> ShowS
forall c. Show c => Int -> Inv c -> ShowS
forall c. Show c => [Inv c] -> ShowS
forall c. Show c => Inv c -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Inv c] -> ShowS
$cshowList :: forall c. Show c => [Inv c] -> ShowS
show :: Inv c -> String
$cshow :: forall c. Show c => Inv c -> String
showsPrec :: Int -> Inv c -> ShowS
$cshowsPrec :: forall c. Show c => Int -> Inv c -> ShowS
Show,Inv c -> Inv c -> Bool
forall c. Eq c => Inv c -> Inv c -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Inv c -> Inv c -> Bool
$c/= :: forall c. Eq c => Inv c -> Inv c -> Bool
== :: Inv c -> Inv c -> Bool
$c== :: forall c. Eq c => Inv c -> Inv c -> Bool
Eq)
instance Embeddable (Inv c) c where
inj :: Inv c -> c
inj (Inv c
f c
_) = c
f
instance Multiplicative c => Validable (Inv c) where
valid :: Inv c -> Statement
valid (Inv c
f c
f') = String -> Label
Label String
"Inv" Label -> Statement -> Statement
:<=>:
[Statement] -> Statement
And [ forall a. Validable a => a -> Statement
valid (c
f,c
f')
, String -> Label
Label String
"1" Label -> Statement -> Statement
:<=>: (forall q. Oriented q => q -> Orientation (Point q)
orientation c
f' forall a. Eq a => a -> a -> Bool
== forall p. Orientation p -> Orientation p
opposite (forall q. Oriented q => q -> Orientation (Point q)
orientation c
f))Bool -> Message -> Statement
:?>Message
prms
, String -> Label
Label String
"2" Label -> Statement -> Statement
:<=>: (c
f' forall c. Multiplicative c => c -> c -> c
* c
f forall a. Eq a => a -> a -> Bool
== forall c. Multiplicative c => Point c -> c
one (forall q. Oriented q => q -> Point q
start c
f))Bool -> Message -> Statement
:?>Message
prms
, String -> Label
Label String
"3" Label -> Statement -> Statement
:<=>: (c
f forall c. Multiplicative c => c -> c -> c
* c
f' forall a. Eq a => a -> a -> Bool
== forall c. Multiplicative c => Point c -> c
one (forall q. Oriented q => q -> Point q
end c
f))Bool -> Message -> Statement
:?>Message
prms
]
where prms :: Message
prms = [Parameter] -> Message
Params [String
"f"String -> String -> Parameter
:=forall a. Show a => a -> String
show c
f,String
"f'"String -> String -> Parameter
:=forall a. Show a => a -> String
show c
f']
instance Multiplicative c => Entity (Inv c)
instance Multiplicative c => Oriented (Inv c) where
type Point (Inv c) = Point c
orientation :: Inv c -> Orientation (Point (Inv c))
orientation (Inv c
f c
_) = forall q. Oriented q => q -> Orientation (Point q)
orientation c
f
instance Multiplicative c => Multiplicative (Inv c) where
one :: Point (Inv c) -> Inv c
one Point (Inv c)
p = forall c. c -> c -> Inv c
Inv c
o c
o where o :: c
o = forall c. Multiplicative c => Point c -> c
one Point (Inv c)
p
Inv c
f c
f' * :: Inv c -> Inv c -> Inv c
* Inv c
g c
g'
| forall q. Oriented q => q -> Point q
end c
g forall a. Eq a => a -> a -> Bool
== forall q. Oriented q => q -> Point q
start c
f = forall c. c -> c -> Inv c
Inv (c
fforall c. Multiplicative c => c -> c -> c
*c
g) (c
g'forall c. Multiplicative c => c -> c -> c
*c
f')
| Bool
otherwise = forall a e. Exception e => e -> a
throw ArithmeticException
NotMultiplicable
npower :: Inv c -> N -> Inv c
npower (Inv c
f c
f') N
n = forall c. c -> c -> Inv c
Inv (forall c. Multiplicative c => c -> N -> c
npower c
f N
n) (forall c. Multiplicative c => c -> N -> c
npower c
f' N
n)
instance Multiplicative c => Invertible (Inv c) where
tryToInvert :: Inv c -> Solver (Inv c)
tryToInvert (Inv c
f c
f') = forall (m :: * -> *) a. Monad m => a -> m a
return (forall c. c -> c -> Inv c
Inv c
f' c
f)
instance Multiplicative c => Cayleyan (Inv c)
instance TransposableMultiplicative c => Transposable (Inv c) where
transpose :: Inv c -> Inv c
transpose (Inv c
f c
f') = forall c. c -> c -> Inv c
Inv (forall x. Transposable x => x -> x
transpose c
f) (forall x. Transposable x => x -> x
transpose c
f')
instance TransposableMultiplicative c => TransposableOriented (Inv c)
instance TransposableMultiplicative c => TransposableMultiplicative (Inv c)
data Mlt
type instance Structure Mlt x = Multiplicative x
instance Transformable Mlt Typ where tau :: forall x. Struct Mlt x -> Struct Typ x
tau Struct Mlt x
Struct = forall s x. Structure s x => Struct s x
Struct
instance Transformable Mlt Ent where tau :: forall x. Struct Mlt x -> Struct Ent x
tau Struct Mlt x
Struct = forall s x. Structure s x => Struct s x
Struct
instance Transformable Mlt Ort where tau :: forall x. Struct Mlt x -> Struct Ort x
tau Struct Mlt x
Struct = forall s x. Structure s x => Struct s x
Struct
instance Transformable1 Op Mlt where tau1 :: forall x. Struct Mlt x -> Struct Mlt (Op x)
tau1 Struct Mlt x
Struct = forall s x. Structure s x => Struct s x
Struct
instance TransformableOp Mlt
class (ForgetfulOrt s, Transformable s Mlt) => ForgetfulMlt s
instance ForgetfulTyp Mlt
instance ForgetfulOrt Mlt
instance ForgetfulMlt Mlt
xosAdjOne :: Multiplicative c => XOrtSite s c -> XOrtSite s c
xosAdjOne :: forall c (s :: Site).
Multiplicative c =>
XOrtSite s c -> XOrtSite s c
xosAdjOne xs :: XOrtSite s c
xs@(XStart X (Point c)
xp Point c -> X c
_) = forall q. X (Point q) -> (Point q -> X q) -> XOrtSite 'From q
XStart X (Point c)
xp (forall c. Multiplicative c => XOrtSite 'From c -> Point c -> X c
xq' XOrtSite s c
xs) where
xq' :: Multiplicative c => XOrtSite From c -> Point c -> X c
xq' :: forall c. Multiplicative c => XOrtSite 'From c -> Point c -> X c
xq' (XStart X (Point c)
_ Point c -> X c
xc) Point c
p = case Point c -> X c
xc Point c
p of
X c
XEmpty -> forall (m :: * -> *) a. Monad m => a -> m a
return forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall c. Multiplicative c => Point c -> c
one Point c
p
X c
xf -> X c
xf
xosAdjOne xe :: XOrtSite s c
xe@(XEnd X (Point c)
_ Point c -> X c
_) = forall x. Dualisable x => Dual x -> x
fromDual forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall c (s :: Site).
Multiplicative c =>
XOrtSite s c -> XOrtSite s c
xosAdjOne forall (h :: * -> * -> *) a b. Applicative h => h a b -> a -> b
$ forall x. Dualisable x => x -> Dual x
toDual XOrtSite s c
xe
xosPathAt :: Multiplicative c => XOrtSite s c -> N -> Point c -> X (Path c)
xosPathAt :: forall c (s :: Site).
Multiplicative c =>
XOrtSite s c -> N -> Point c -> X (Path c)
xosPathAt XOrtSite s c
xa = forall q (s :: Site).
Oriented q =>
XOrtSite s q -> N -> Point q -> X (Path q)
xosPathMaxAt (forall c (s :: Site).
Multiplicative c =>
XOrtSite s c -> XOrtSite s c
xosAdjOne XOrtSite s c
xa)
xosPath :: Multiplicative c => XOrtSite s c -> N -> X (Path c)
xosPath :: forall c (s :: Site).
Multiplicative c =>
XOrtSite s c -> N -> X (Path c)
xosPath XOrtSite s c
xa = forall q (s :: Site). Oriented q => XOrtSite s q -> N -> X (Path q)
xosPathMax (forall c (s :: Site).
Multiplicative c =>
XOrtSite s c -> XOrtSite s c
xosAdjOne XOrtSite s c
xa)
dstPathDrcn :: Multiplicative c => Int -> N -> XOrtSite s c -> IO ()
dstPathDrcn :: forall c (s :: Site).
Multiplicative c =>
Int -> N -> XOrtSite s c -> IO ()
dstPathDrcn Int
n N
l XOrtSite s c
xa = IO Omega
getOmega forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall x. (Show x, Ord x) => Int -> X x -> Omega -> IO ()
putDistribution Int
n (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Show a => a -> String
show X (Path c)
xx) where
xx :: X (Path c)
xx = forall (s :: Site) q. XOrtSite s q -> X (Point q)
xosPoint XOrtSite s c
xa forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall c (s :: Site).
Multiplicative c =>
XOrtSite s c -> N -> Point c -> X (Path c)
xosPathAt XOrtSite s c
xa N
l
xosXOrtSitePath :: Multiplicative c
=> XOrtSite s c -> N -> XOrtSite s (Path c)
xosXOrtSitePath :: forall c (s :: Site).
Multiplicative c =>
XOrtSite s c -> N -> XOrtSite s (Path c)
xosXOrtSitePath xs :: XOrtSite s c
xs@(XStart X (Point c)
xp Point c -> X c
_) N
n = forall q. X (Point q) -> (Point q -> X q) -> XOrtSite 'From q
XStart X (Point c)
xp (forall c (s :: Site).
Multiplicative c =>
XOrtSite s c -> N -> Point c -> X (Path c)
xosPathAt XOrtSite s c
xs N
n)
xosXOrtSitePath xe :: XOrtSite s c
xe@(XEnd X (Point c)
xp Point c -> X c
_) N
n = forall q. X (Point q) -> (Point q -> X q) -> XOrtSite 'To q
XEnd X (Point c)
xp (forall c (s :: Site).
Multiplicative c =>
XOrtSite s c -> N -> Point c -> X (Path c)
xosPathAt XOrtSite s c
xe N
n)