{-# LANGUAGE CPP #-}
{-# LANGUAGE Safe #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE RebindableSyntax #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE RankNTypes #-}
module Data.Semimodule.Transform where
import safe Control.Arrow
import safe Control.Applicative
import safe Control.Category (Category, (>>>), (<<<))
import safe Data.Functor.Compose
import safe Data.Functor.Product
import safe Data.Functor.Rep hiding (Co)
import safe Data.Foldable (foldl')
import safe Data.Semiring
import safe Data.Semimodule
import safe Data.Semimodule.Algebra
import safe Data.Tuple (swap)
import safe Prelude hiding (Num(..), Fractional(..), init, negate, sum, product)
import safe Test.Logic hiding (join)
import safe qualified Control.Category as C
import safe qualified Control.Monad as M
import safe Control.Monad (MonadPlus(..))
infixr 2 **
infixr 1 ++
type (f ** g) = Compose f g
type (f ++ g) = Product f g
infixr 3 `runDual`
newtype Dual a c = Dual { runDual :: (c -> a) -> a }
image' :: Semiring a => Foldable f => f (a, c) -> Dual a c
image' f = Dual $ \k -> foldl' (\acc (a, c) -> acc + a * k c) zero f
toTran :: (b -> Dual a c) -> Tran a b c
toTran f = Tran $ \k b -> f b !* k
fromTran :: Tran a b c -> b -> Dual a c
fromTran m b = Dual $ \k -> (m !# k) b
infixr 3 !*
(!*) :: Free f => Dual a (Rep f) -> f a -> a
(!*) f x = runDual f $ index x
infixl 3 *!
(*!) :: Free f => f a -> Dual a (Rep f) -> a
(*!) = flip (!*)
type Endo a b = Tran a b b
image :: Semiring a => (b -> [(a, c)]) -> Tran a b c
image f = Tran $ \k b -> sum [ a * k c | (a, c) <- f b ]
infixr 2 !#
(!#) :: Free f => Free g => Tran a (Rep f) (Rep g) -> g a -> f a
(!#) t = tabulate . runTran t . index
infixl 2 #!
(#!) :: Free f => Free g => g a -> Tran a (Rep f) (Rep g) -> f a
(#!) = flip (!#)
infix 2 !#!
(!#!) :: Tran a c d -> Tran a b c -> Tran a b d
(!#!) = (C..)
dimap' :: (b1 -> b2) -> (c1 -> c2) -> Tran a b2 c1 -> Tran a b1 c2
dimap' l r f = arr r <<< f <<< arr l
lmap' :: (b1 -> b2) -> Tran a b2 c -> Tran a b1 c
lmap' l = dimap' l id
rmap' :: (c1 -> c2) -> Tran a b c1 -> Tran a b c2
rmap' = dimap' id
invmap :: (a1 -> a2) -> (a2 -> a1) -> Tran a1 b c -> Tran a2 b c
invmap f g (Tran t) = Tran $ \x -> t (x >>> g) >>> f
braid :: Tran a (b , c) (c , b)
braid = arr swap
{-# INLINE braid #-}
cobraid :: Tran a (b + c) (c + b)
cobraid = arr eswap
{-# INLINE cobraid #-}
commutator :: (Additive-Group) a => Endo a b -> Endo a b -> Endo a b
commutator x y = (x <<< y) - (y <<< x)
split :: (b -> (b1 , b2)) -> Tran a b1 c -> Tran a b2 c -> Tran a b c
split f x y = dimap' f fst $ x *** y
{-# INLINE split #-}
cosplit :: ((c1 + c2) -> c) -> Tran a b c1 -> Tran a b c2 -> Tran a b c
cosplit f x y = dimap' Left f $ x +++ y
{-# INLINE cosplit #-}
projl :: Free f => Free g => (f++g) a -> f a
projl fg = arr Left !# fg
{-# INLINE projl #-}
projr :: Free f => Free g => (f++g) a -> g a
projr fg = arr Right !# fg
{-# INLINE projr #-}
compl :: Free f1 => Free f2 => Free g => Tran a (Rep f1) (Rep f2) -> (f2**g) a -> (f1**g) a
compl t fg = first t !# fg
compr :: Free f => Free g1 => Free g2 => Tran a (Rep g1) (Rep g2) -> (f**g2) a -> (f**g1) a
compr t fg = second t !# fg
complr :: Free f1 => Free f2 => Free g1 => Free g2 => Tran a (Rep f1) (Rep f2) -> Tran a (Rep g1) (Rep g2) -> (f2**g2) a -> (f1**g1) a
complr t1 t2 fg = t1 *** t2 !# fg
instance Functor (Dual a) where
fmap f m = Dual $ \k -> m `runDual` k . f
instance Applicative (Dual a) where
pure a = Dual $ \k -> k a
mf <*> ma = Dual $ \k -> mf `runDual` \f -> ma `runDual` k . f
instance Monad (Dual a) where
return a = Dual $ \k -> k a
m >>= f = Dual $ \k -> m `runDual` \a -> f a `runDual` k
instance (Additive-Monoid) a => Alternative (Dual a) where
Dual m <|> Dual n = Dual $ m + n
empty = Dual zero
instance (Additive-Monoid) a => MonadPlus (Dual a) where
Dual m `mplus` Dual n = Dual $ m + n
mzero = Dual zero
instance (Additive-Semigroup) a => Semigroup (Additive (Dual a b)) where
(<>) = liftA2 $ \(Dual m) (Dual n) -> Dual $ m + n
instance (Additive-Monoid) a => Monoid (Additive (Dual a b)) where
mempty = Additive $ Dual zero
instance Coalgebra a b => Semigroup (Multiplicative (Dual a b)) where
(<>) = liftA2 $ \(Dual f) (Dual g) -> Dual $ \k -> f (\m -> g (cojoined k m))
instance Counital a b => Monoid (Multiplicative (Dual a b)) where
mempty = Multiplicative $ Dual counital
instance Coalgebra a b => Presemiring (Dual a b)
instance Counital a b => Semiring (Dual a b)
instance (Additive-Group) a => Magma (Additive (Dual a b)) where
(<<) = liftA2 $ \(Dual m) (Dual n) -> Dual $ m - n
instance (Additive-Group) a => Quasigroup (Additive (Dual a b)) where
instance (Additive-Group) a => Loop (Additive (Dual a b)) where
instance (Additive-Group) a => Group (Additive (Dual a b)) where
instance (Ring a, Counital a b) => Ring (Dual a b)
instance Counital r m => LeftSemimodule (Dual r m) (Dual r m) where
lscale = (*)
instance LeftSemimodule r s => LeftSemimodule r (Dual s m) where
lscale s m = Dual $ \k -> s *. runDual m k
instance Counital r m => RightSemimodule (Dual r m) (Dual r m) where
rscale = (*)
instance RightSemimodule r s => RightSemimodule r (Dual s m) where
rscale s m = Dual $ \k -> runDual m k .* s