{-# LANGUAGE PatternSynonyms #-}
{-# language Trustworthy #-}
module Data.Group.Free
(
FreeGroup(..)
, simplify
, interpret
, interpret'
, present
, FreeAbelianGroup
, pattern FreeAbelianGroup
, mkFreeAbelianGroup
, runFreeAbelianGroup
, abfoldMap
, abmap
, abjoin
, singleton
, abInterpret
) where
import Control.Applicative
import Control.Monad
import Data.Bifunctor
import Data.List (foldl')
import Data.Map (Map)
import qualified Data.Map.Strict as Map
import Data.Group
import Data.Group.Free.Internal
import Data.Group.Order
newtype FreeGroup a = FreeGroup { FreeGroup a -> [Either a a]
runFreeGroup :: [Either a a] }
deriving (Int -> FreeGroup a -> ShowS
[FreeGroup a] -> ShowS
FreeGroup a -> String
(Int -> FreeGroup a -> ShowS)
-> (FreeGroup a -> String)
-> ([FreeGroup a] -> ShowS)
-> Show (FreeGroup a)
forall a. Show a => Int -> FreeGroup a -> ShowS
forall a. Show a => [FreeGroup a] -> ShowS
forall a. Show a => FreeGroup a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [FreeGroup a] -> ShowS
$cshowList :: forall a. Show a => [FreeGroup a] -> ShowS
show :: FreeGroup a -> String
$cshow :: forall a. Show a => FreeGroup a -> String
showsPrec :: Int -> FreeGroup a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> FreeGroup a -> ShowS
Show, FreeGroup a -> FreeGroup a -> Bool
(FreeGroup a -> FreeGroup a -> Bool)
-> (FreeGroup a -> FreeGroup a -> Bool) -> Eq (FreeGroup a)
forall a. Eq a => FreeGroup a -> FreeGroup a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: FreeGroup a -> FreeGroup a -> Bool
$c/= :: forall a. Eq a => FreeGroup a -> FreeGroup a -> Bool
== :: FreeGroup a -> FreeGroup a -> Bool
$c== :: forall a. Eq a => FreeGroup a -> FreeGroup a -> Bool
Eq, Eq (FreeGroup a)
Eq (FreeGroup a)
-> (FreeGroup a -> FreeGroup a -> Ordering)
-> (FreeGroup a -> FreeGroup a -> Bool)
-> (FreeGroup a -> FreeGroup a -> Bool)
-> (FreeGroup a -> FreeGroup a -> Bool)
-> (FreeGroup a -> FreeGroup a -> Bool)
-> (FreeGroup a -> FreeGroup a -> FreeGroup a)
-> (FreeGroup a -> FreeGroup a -> FreeGroup a)
-> Ord (FreeGroup a)
FreeGroup a -> FreeGroup a -> Bool
FreeGroup a -> FreeGroup a -> Ordering
FreeGroup a -> FreeGroup a -> FreeGroup 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 (FreeGroup a)
forall a. Ord a => FreeGroup a -> FreeGroup a -> Bool
forall a. Ord a => FreeGroup a -> FreeGroup a -> Ordering
forall a. Ord a => FreeGroup a -> FreeGroup a -> FreeGroup a
min :: FreeGroup a -> FreeGroup a -> FreeGroup a
$cmin :: forall a. Ord a => FreeGroup a -> FreeGroup a -> FreeGroup a
max :: FreeGroup a -> FreeGroup a -> FreeGroup a
$cmax :: forall a. Ord a => FreeGroup a -> FreeGroup a -> FreeGroup a
>= :: FreeGroup a -> FreeGroup a -> Bool
$c>= :: forall a. Ord a => FreeGroup a -> FreeGroup a -> Bool
> :: FreeGroup a -> FreeGroup a -> Bool
$c> :: forall a. Ord a => FreeGroup a -> FreeGroup a -> Bool
<= :: FreeGroup a -> FreeGroup a -> Bool
$c<= :: forall a. Ord a => FreeGroup a -> FreeGroup a -> Bool
< :: FreeGroup a -> FreeGroup a -> Bool
$c< :: forall a. Ord a => FreeGroup a -> FreeGroup a -> Bool
compare :: FreeGroup a -> FreeGroup a -> Ordering
$ccompare :: forall a. Ord a => FreeGroup a -> FreeGroup a -> Ordering
$cp1Ord :: forall a. Ord a => Eq (FreeGroup a)
Ord)
instance Semigroup (FreeGroup a) where
(FreeGroup [Either a a]
g) <> :: FreeGroup a -> FreeGroup a -> FreeGroup a
<> (FreeGroup [Either a a]
g') = [Either a a] -> FreeGroup a
forall a. [Either a a] -> FreeGroup a
FreeGroup ([Either a a]
g [Either a a] -> [Either a a] -> [Either a a]
forall a. [a] -> [a] -> [a]
++ [Either a a]
g')
instance Monoid (FreeGroup a) where
mempty :: FreeGroup a
mempty = [Either a a] -> FreeGroup a
forall a. [Either a a] -> FreeGroup a
FreeGroup []
instance Group (FreeGroup a) where
invert :: FreeGroup a -> FreeGroup a
invert = [Either a a] -> FreeGroup a
forall a. [Either a a] -> FreeGroup a
FreeGroup
([Either a a] -> FreeGroup a)
-> (FreeGroup a -> [Either a a]) -> FreeGroup a -> FreeGroup a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Either a a -> Either a a) -> [Either a a] -> [Either a a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((a -> Either a a) -> (a -> Either a a) -> Either a a -> Either a a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either a -> Either a a
forall a b. b -> Either a b
Right a -> Either a a
forall a b. a -> Either a b
Left)
([Either a a] -> [Either a a])
-> (FreeGroup a -> [Either a a]) -> FreeGroup a -> [Either a a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Either a a] -> [Either a a]
forall a. [a] -> [a]
reverse
([Either a a] -> [Either a a])
-> (FreeGroup a -> [Either a a]) -> FreeGroup a -> [Either a a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FreeGroup a -> [Either a a]
forall a. FreeGroup a -> [Either a a]
runFreeGroup
instance Eq a => GroupOrder (FreeGroup a) where
order :: FreeGroup a -> Order
order FreeGroup a
g | FreeGroup a -> FreeGroup a
forall a. Eq a => FreeGroup a -> FreeGroup a
simplify FreeGroup a
g FreeGroup a -> FreeGroup a -> Bool
forall a. Eq a => a -> a -> Bool
== FreeGroup a
forall a. Monoid a => a
mempty = Natural -> Order
Finite Natural
1
| Bool
otherwise = Order
Infinite
instance Functor FreeGroup where
fmap :: (a -> b) -> FreeGroup a -> FreeGroup b
fmap a -> b
f (FreeGroup [Either a a]
g) = [Either b b] -> FreeGroup b
forall a. [Either a a] -> FreeGroup a
FreeGroup ([Either b b] -> FreeGroup b) -> [Either b b] -> FreeGroup b
forall a b. (a -> b) -> a -> b
$ (Either a a -> Either b b) -> [Either a a] -> [Either b b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((a -> b) -> (a -> b) -> Either a a -> Either b b
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap a -> b
f a -> b
f) [Either a a]
g
instance Applicative FreeGroup where
pure :: a -> FreeGroup a
pure a
a = [Either a a] -> FreeGroup a
forall a. [Either a a] -> FreeGroup a
FreeGroup ([Either a a] -> FreeGroup a) -> [Either a a] -> FreeGroup a
forall a b. (a -> b) -> a -> b
$ Either a a -> [Either a a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either a a -> [Either a a]) -> Either a a -> [Either a a]
forall a b. (a -> b) -> a -> b
$ a -> Either a a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
a
<*> :: FreeGroup (a -> b) -> FreeGroup a -> FreeGroup b
(<*>) = FreeGroup (a -> b) -> FreeGroup a -> FreeGroup b
forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap
instance Monad FreeGroup where
return :: a -> FreeGroup a
return = a -> FreeGroup a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
(FreeGroup [Either a a]
g) >>= :: FreeGroup a -> (a -> FreeGroup b) -> FreeGroup b
>>= a -> FreeGroup b
f = [Either b b] -> FreeGroup b
forall a. [Either a a] -> FreeGroup a
FreeGroup ([Either b b] -> FreeGroup b) -> [Either b b] -> FreeGroup b
forall a b. (a -> b) -> a -> b
$ (Either a a -> [Either b b]) -> [Either a a] -> [Either b b]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Either a a -> [Either b b]
go [Either a a]
g
where
go :: Either a a -> [Either b b]
go (Left a
a) = FreeGroup b -> [Either b b]
forall a. FreeGroup a -> [Either a a]
runFreeGroup (FreeGroup b -> [Either b b]) -> FreeGroup b -> [Either b b]
forall a b. (a -> b) -> a -> b
$ FreeGroup b -> FreeGroup b
forall m. Group m => m -> m
invert (a -> FreeGroup b
f a
a)
go (Right a
a) = FreeGroup b -> [Either b b]
forall a. FreeGroup a -> [Either a a]
runFreeGroup (FreeGroup b -> [Either b b]) -> FreeGroup b -> [Either b b]
forall a b. (a -> b) -> a -> b
$ a -> FreeGroup b
f a
a
instance Alternative FreeGroup where
empty :: FreeGroup a
empty = FreeGroup a
forall a. Monoid a => a
mempty
<|> :: FreeGroup a -> FreeGroup a -> FreeGroup a
(<|>) = FreeGroup a -> FreeGroup a -> FreeGroup a
forall a. Semigroup a => a -> a -> a
(<>)
simplify :: (Eq a) => FreeGroup a -> FreeGroup a
simplify :: FreeGroup a -> FreeGroup a
simplify (FreeGroup [Either a a]
g) = [Either a a] -> FreeGroup a
forall a. [Either a a] -> FreeGroup a
FreeGroup ([Either a a] -> FreeGroup a) -> [Either a a] -> FreeGroup a
forall a b. (a -> b) -> a -> b
$ (Either a a -> [Either a a] -> [Either a a])
-> [Either a a] -> [Either a a] -> [Either a a]
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Either a a -> [Either a a] -> [Either a a]
forall a. Eq a => Either a a -> [Either a a] -> [Either a a]
go [] [Either a a]
g
where
go :: Either a a -> [Either a a] -> [Either a a]
go (Left a
a) ((Right a
a'):[Either a a]
as) | a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
a' = [Either a a]
as
go (Right a
a) ((Left a
a'):[Either a a]
as) | a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
a' = [Either a a]
as
go Either a a
a [Either a a]
as = Either a a
aEither a a -> [Either a a] -> [Either a a]
forall a. a -> [a] -> [a]
:[Either a a]
as
interpret :: (Group g) => FreeGroup g -> g
interpret :: FreeGroup g -> g
interpret (FreeGroup [Either g g]
g) = (Either g g -> g -> g) -> g -> [Either g g] -> g
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Either g g -> g -> g
forall a. Group a => Either a a -> a -> a
go g
forall a. Monoid a => a
mempty [Either g g]
g
where
go :: Either a a -> a -> a
go (Left a
a) a
acc = a -> a
forall m. Group m => m -> m
invert a
a a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
acc
go (Right a
a) a
acc = a
a a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
acc
interpret' :: (Group g) => FreeGroup g -> g
interpret' :: FreeGroup g -> g
interpret' (FreeGroup [Either g g]
g) = (g -> Either g g -> g) -> g -> [Either g g] -> g
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' g -> Either g g -> g
forall a. Group a => a -> Either a a -> a
go g
forall a. Monoid a => a
mempty [Either g g]
g
where
go :: a -> Either a a -> a
go a
acc (Left a
a) = a
acc a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a -> a
forall m. Group m => m -> m
invert a
a
go a
acc (Right a
a) = a
acc a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
a
present :: Group g => FreeGroup g -> (FreeGroup g -> g) -> g
present :: FreeGroup g -> (FreeGroup g -> g) -> g
present = ((FreeGroup g -> g) -> FreeGroup g -> g)
-> FreeGroup g -> (FreeGroup g -> g) -> g
forall a b c. (a -> b -> c) -> b -> a -> c
flip (FreeGroup g -> g) -> FreeGroup g -> g
forall a b. (a -> b) -> a -> b
($)
{-# inline present #-}
mkFreeAbelianGroup :: Ord a => Map a Integer -> FreeAbelianGroup a
mkFreeAbelianGroup :: Map a Integer -> FreeAbelianGroup a
mkFreeAbelianGroup = Map a Integer -> FreeAbelianGroup a
forall a. Map a Integer -> FreeAbelianGroup a
MkFreeAbelianGroup (Map a Integer -> FreeAbelianGroup a)
-> (Map a Integer -> Map a Integer)
-> Map a Integer
-> FreeAbelianGroup a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Integer -> Bool) -> Map a Integer -> Map a Integer
forall a k. (a -> Bool) -> Map k a -> Map k a
Map.filter (Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
/= Integer
0)
runFreeAbelianGroup :: FreeAbelianGroup a -> Map a Integer
runFreeAbelianGroup :: FreeAbelianGroup a -> Map a Integer
runFreeAbelianGroup (MkFreeAbelianGroup Map a Integer
g) = Map a Integer
g
abfoldMap :: (Abelian g) => (a -> g) -> FreeAbelianGroup a -> g
abfoldMap :: (a -> g) -> FreeAbelianGroup a -> g
abfoldMap a -> g
f = (g -> a -> Integer -> g) -> g -> Map a Integer -> g
forall a k b. (a -> k -> b -> a) -> a -> Map k b -> a
Map.foldlWithKey' g -> a -> Integer -> g
forall x. Integral x => g -> a -> x -> g
step g
forall a. Monoid a => a
mempty (Map a Integer -> g)
-> (FreeAbelianGroup a -> Map a Integer) -> FreeAbelianGroup a -> g
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FreeAbelianGroup a -> Map a Integer
forall a. FreeAbelianGroup a -> Map a Integer
runFreeAbelianGroup
where
step :: g -> a -> x -> g
step g
g a
a x
n = g
g g -> g -> g
forall a. Semigroup a => a -> a -> a
<> g -> x -> g
forall m x. (Group m, Integral x) => m -> x -> m
pow (a -> g
f a
a) x
n
abmap :: (Ord b) => (a -> b) -> FreeAbelianGroup a -> FreeAbelianGroup b
abmap :: (a -> b) -> FreeAbelianGroup a -> FreeAbelianGroup b
abmap a -> b
f = (a -> FreeAbelianGroup b)
-> FreeAbelianGroup a -> FreeAbelianGroup b
forall g a. Abelian g => (a -> g) -> FreeAbelianGroup a -> g
abfoldMap (b -> FreeAbelianGroup b
forall a. a -> FreeAbelianGroup a
singleton (b -> FreeAbelianGroup b) -> (a -> b) -> a -> FreeAbelianGroup b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f)
singleton :: a -> FreeAbelianGroup a
singleton :: a -> FreeAbelianGroup a
singleton a
a = Map a Integer -> FreeAbelianGroup a
forall a. Map a Integer -> FreeAbelianGroup a
MkFreeAbelianGroup (Map a Integer -> FreeAbelianGroup a)
-> Map a Integer -> FreeAbelianGroup a
forall a b. (a -> b) -> a -> b
$ a -> Integer -> Map a Integer
forall k a. k -> a -> Map k a
Map.singleton a
a Integer
1
abjoin :: (Ord a) => FreeAbelianGroup (FreeAbelianGroup a) -> FreeAbelianGroup a
abjoin :: FreeAbelianGroup (FreeAbelianGroup a) -> FreeAbelianGroup a
abjoin = FreeAbelianGroup (FreeAbelianGroup a) -> FreeAbelianGroup a
forall g. Abelian g => FreeAbelianGroup g -> g
abInterpret
abInterpret :: (Abelian g) => FreeAbelianGroup g -> g
abInterpret :: FreeAbelianGroup g -> g
abInterpret = (g -> g) -> FreeAbelianGroup g -> g
forall g a. Abelian g => (a -> g) -> FreeAbelianGroup a -> g
abfoldMap g -> g
forall a. a -> a
id
pattern FreeAbelianGroup :: Ord a => Map a Integer -> FreeAbelianGroup a
pattern $bFreeAbelianGroup :: Map a Integer -> FreeAbelianGroup a
$mFreeAbelianGroup :: forall r a.
Ord a =>
FreeAbelianGroup a -> (Map a Integer -> r) -> (Void# -> r) -> r
FreeAbelianGroup g <- MkFreeAbelianGroup g where
FreeAbelianGroup Map a Integer
g = Map a Integer -> FreeAbelianGroup a
forall a. Ord a => Map a Integer -> FreeAbelianGroup a
mkFreeAbelianGroup Map a Integer
g
{-# complete FreeAbelianGroup #-}