groups-0.4.1.0/0000755000000000000000000000000013205372251011357 5ustar0000000000000000groups-0.4.1.0/Setup.hs0000644000000000000000000000005613205372251013014 0ustar0000000000000000import Distribution.Simple main = defaultMain groups-0.4.1.0/groups.cabal0000644000000000000000000000113013205372251013655 0ustar0000000000000000name: groups version: 0.4.1.0 synopsis: Haskell 98 groups description: Haskell 98 groups. A group is a monoid with invertibility. license: BSD3 license-file: LICENSE author: Nathan "Taneb" van Doorn maintainer: nvd1234@gmail.com copyright: Copyright (C) 2013 Nathan van Doorn category: Algebra, Data, Math build-type: Simple cabal-version: >=1.8 library exposed-modules: Data.Group -- other-modules: build-depends: base <5 hs-source-dirs: src groups-0.4.1.0/LICENSE0000644000000000000000000000301213205372251012360 0ustar0000000000000000Copyright (c) 2013, Nathan "Taneb" van Doorn All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Nathan "Taneb" van Doorn nor the names of other contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. groups-0.4.1.0/src/0000755000000000000000000000000013205372251012146 5ustar0000000000000000groups-0.4.1.0/src/Data/0000755000000000000000000000000013205372251013017 5ustar0000000000000000groups-0.4.1.0/src/Data/Group.hs0000644000000000000000000000522113205372251014447 0ustar0000000000000000module Data.Group where import Data.Monoid -- |A 'Group' is a 'Monoid' plus a function, 'invert', such that: -- -- @a \<> invert a == mempty@ -- -- @invert a \<> a == mempty@ class Monoid m => Group m where invert :: m -> m -- |@'pow' a n == a \<> a \<> ... \<> a @ -- -- @ (n lots of a) @ -- -- If n is negative, the result is inverted. pow :: Integral x => m -> x -> m pow x0 n0 = case compare n0 0 of LT -> invert . f x0 $ negate n0 EQ -> mempty GT -> f x0 n0 where f x n | even n = f (x `mappend` x) (n `quot` 2) | n == 1 = x | otherwise = g (x `mappend` x) (n `quot` 2) x g x n c | even n = g (x `mappend` x) (n `quot` 2) c | n == 1 = x `mappend` c | otherwise = g (x `mappend` x) (n `quot` 2) (x `mappend` c) instance Group () where invert () = () pow () _ = () instance Num a => Group (Sum a) where invert = Sum . negate . getSum {-# INLINE invert #-} pow (Sum a) b = Sum (a * fromIntegral b) instance Fractional a => Group (Product a) where invert = Product . recip . getProduct {-# INLINE invert #-} pow (Product a) b = Product (a ^^ b) instance Group a => Group (Dual a) where invert = Dual . invert . getDual {-# INLINE invert #-} pow (Dual a) n = Dual (pow a n) instance Group b => Group (a -> b) where invert f = invert . f pow f n e = pow (f e) n instance (Group a, Group b) => Group (a, b) where invert (a, b) = (invert a, invert b) pow (a, b) n = (pow a n, pow b n) instance (Group a, Group b, Group c) => Group (a, b, c) where invert (a, b, c) = (invert a, invert b, invert c) pow (a, b, c) n = (pow a n, pow b n, pow c n) instance (Group a, Group b, Group c, Group d) => Group (a, b, c, d) where invert (a, b, c, d) = (invert a, invert b, invert c, invert d) pow (a, b, c, d) n = (pow a n, pow b n, pow c n, pow d n) instance (Group a, Group b, Group c, Group d, Group e) => Group (a, b, c, d, e) where invert (a, b, c, d, e) = (invert a, invert b, invert c, invert d, invert e) pow (a, b, c, d, e) n = (pow a n, pow b n, pow c n, pow d n, pow e n) -- |An 'Abelian' group is a 'Group' that follows the rule: -- -- @a \<> b == b \<> a@ class Group g => Abelian g instance Abelian () instance Num a => Abelian (Sum a) instance Fractional a => Abelian (Product a) instance Abelian a => Abelian (Dual a) instance Abelian b => Abelian (a -> b) instance (Abelian a, Abelian b) => Abelian (a, b) instance (Abelian a, Abelian b, Abelian c) => Abelian (a, b, c) instance (Abelian a, Abelian b, Abelian c, Abelian d) => Abelian (a, b, c, d) instance (Abelian a, Abelian b, Abelian c, Abelian d, Abelian e) => Abelian (a, b, c, d, e)