commutative-semigroups-0.1.1.0/0000755000000000000000000000000007346545000014570 5ustar0000000000000000commutative-semigroups-0.1.1.0/ChangeLog.md0000644000000000000000000000253307346545000016744 0ustar0000000000000000# Revision history for commutative-semigroups ## 0.1.1.0 -- 2024-03-24 - Added more trivial instances for semigroups from base ## 0.1.0.2 -- 2023-12-22 - Support GHC 9.8.1 ## 0.1.0.1 -- 2023-04-17 - Loosen version bounds - Support GHC 9.6.1 ## 0.1.0.0 -- 2022-06-12 - `Commutative (Product a)` now requires `CommutativeProduct a`. `CommutativeProduct` is a new class to indicate `(*)` from `Num` is commutative, which is not required by `Num`. (Example: multiplication on [quaternions](https://en.wikipedia.org/wiki/Quaternion) is non-commutative, and the `Quaternion a` type from the `linear` package has a valid `instance RealFloat a => Num (Quaternion a)`.) **Remark:** There is also no canonical subclass class in the `Num` hierarchy which implies commutative `(*)`, as both `Integral` and `Floating` instances work here: - `Integral` instances are customarily Euclidean Domains, which are commutative rings with extra conditions. - `Floating` instances customarily expect `(+)`, `(*)`, and `exp` to form an exponential field, which is also a commutative ring with extra conditions. ## 0.0.2.0 -- 2022-03-26 - Add `instance Ord a => Commutative (Set a)` - Add `instance Commutative IntSet` ## 0.0.1.0 -- 2021-01-28 - Add instance for `Maybe`. ## 0.0.0.0 -- 2021-01-06 Initial version, created from `groups` package. commutative-semigroups-0.1.1.0/LICENSE0000644000000000000000000000306307346545000015577 0ustar0000000000000000Copyright (c) 2013, Nathan "Taneb" van Doorn 2021, Obsidian Systems LLC 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. commutative-semigroups-0.1.1.0/ReadMe.md0000644000000000000000000000102407346545000016244 0ustar0000000000000000# Commutative Semigroup [![Haskell](https://img.shields.io/badge/language-Haskell-orange.svg)](https://haskell.org) [![Hackage](https://img.shields.io/hackage/v/commutative-semigroups.svg)](https://hackage.haskell.org/package/commutative-semigroups) [![BSD3 License](https://img.shields.io/badge/license-BSD3-blue.svg)](https://github.com/reflex-frp/commutative-semigroups/LICENSE) A commutative semigroup is a semigroup where the order of arguments to mappend does not matter. ```haskell class Semigroup g => Commutative g ``` commutative-semigroups-0.1.1.0/Setup.hs0000644000000000000000000000005607346545000016225 0ustar0000000000000000import Distribution.Simple main = defaultMain commutative-semigroups-0.1.1.0/commutative-semigroups.cabal0000644000000000000000000000212207346545000022301 0ustar0000000000000000cabal-version: 2.4 name: commutative-semigroups version: 0.1.1.0 synopsis: Commutative semigroups description: A commutative semigroup is a semigroup where the order of arguments to mappend does not matter. license: BSD-3-Clause license-file: LICENSE author: Nathan "Taneb" van Doorn maintainer: maintainer@obsidian.systems copyright: Copyright (C) 2013 Nathan van Doorn, 2021–2022 Obsidian Systems LLC category: Algebra, Data, Math build-type: Simple bug-reports: https://github.com/ObsidianSystems/commutative-semigroups/issues extra-source-files: ChangeLog.md ReadMe.md source-repository head type: git location: https://github.com/ObsidianSystems/commutative-semigroups.git library exposed-modules: Data.Semigroup.Commutative Numeric.Product.Commutative -- other-modules: build-depends: base >= 4.6 && < 4.20, containers >= 0.4 && < 0.8 hs-source-dirs: src default-language: Haskell2010 commutative-semigroups-0.1.1.0/src/Data/Semigroup/0000755000000000000000000000000007346545000020202 5ustar0000000000000000commutative-semigroups-0.1.1.0/src/Data/Semigroup/Commutative.hs0000644000000000000000000000716107346545000023040 0ustar0000000000000000{-# LANGUAGE CPP #-} #if MIN_VERSION_base(4,12,0) {-# LANGUAGE TypeOperators #-} #endif module Data.Semigroup.Commutative where import Data.IntSet (IntSet) import Data.Monoid import Data.Ord import Data.List (unfoldr) import Data.Set #if MIN_VERSION_base(4,7,0) import Data.Proxy #endif #if MIN_VERSION_base(4,8,0) import Data.Void (Void) #endif #if MIN_VERSION_base(4,9,0) && !MIN_VERSION_base(4,11,0) import Data.Semigroup (Semigroup(..)) #endif #if MIN_VERSION_base(4,9,0) import Data.Functor.Const import Data.Functor.Identity import Data.Semigroup (Max, Min, WrappedMonoid) #endif #if MIN_VERSION_base(4,10,0) import GHC.Event (Event, Lifetime) #endif #if MIN_VERSION_base(4,12,0) import Data.Functor.Contravariant (Op(Op)) import GHC.Generics #endif import Numeric.Product.Commutative ( CommutativeProduct ) -- | A 'Commutative' semigroup is a 'Semigroup' that follows the rule: -- -- @a \<> b == b \<> a@ class #if MIN_VERSION_base(4,9,0) Semigroup g #else Monoid g #endif => Commutative g -- | Trivial commutative semigroup. instance Commutative () instance Commutative All instance Commutative Any #if MIN_VERSION_base(4,8,0) instance Commutative Void #endif #if MIN_VERSION_base(4,9,0) instance Ord a => Commutative (Max a) instance Ord a => Commutative (Min a) instance (Commutative a, Monoid a) => Commutative (WrappedMonoid a) #endif #if MIN_VERSION_base(4,10,0) instance Commutative Event instance Commutative Lifetime #endif -- | @since 0.0.1.0 instance Commutative a => Commutative (Maybe a) instance Num a => Commutative (Sum a) instance CommutativeProduct a => Commutative (Product a) instance Commutative a => Commutative (Dual a) -- | Functions lift commutative semigroups pointwise. instance Commutative b => Commutative (a -> b) -- | Product commutative semigroup. -- A Pair of commutative semigroups gives rise to a commutative semigroup instance (Commutative a, Commutative b) => Commutative (a, b) instance (Commutative a, Commutative b, Commutative c) => Commutative (a, b, c) instance (Commutative a, Commutative b, Commutative c, Commutative d) => Commutative (a, b, c, d) instance (Commutative a, Commutative b, Commutative c, Commutative d, Commutative e) => Commutative (a, b, c, d, e) -- | @since 0.0.2.0 instance Ord a => Commutative (Set a) -- | @since 0.0.2.0 instance Commutative IntSet #if MIN_VERSION_base(4,11,0) instance Commutative a => Commutative (Down a) #endif #if MIN_VERSION_base(4,7,0) -- | Trivial commutative semigroup, Functor style. instance Commutative (Proxy x) #endif -- 'Const' has existed for a long time, but the Monoid instance -- arrives in base-4.9.0.0. Similarly, 'Identity' was defined in -- base-4.8.0.0 but doesn't get the Monoid instance until base-4.9.0.0 #if MIN_VERSION_base(4,9,0) -- | 'Const' lifts commutative semigroups into a functor. instance Commutative a => Commutative (Const a x) -- | 'Identity' lifts commutative semigroups pointwise (at only one point). instance Commutative a => Commutative (Identity a) #endif -- (:*:) and (:.:) exist since base-4.6.0.0 but the Monoid instances -- arrive in base-4.12.0.0. -- Also, contravariant was moved into base in this version. #if MIN_VERSION_base(4,12,0) -- | Product of commutative semigroups, Functor style. instance (Commutative (f a), Commutative (g a)) => Commutative ((f :*: g) a) -- See https://gitlab.haskell.org/ghc/ghc/issues/11135#note_111802 for the reason Compose is not also provided. -- Base does not define Monoid (Compose f g a) so this is the best we can -- really do for functor composition. instance Commutative (f (g a)) => Commutative ((f :.: g) a) instance Commutative a => Commutative (Op a b) #endif commutative-semigroups-0.1.1.0/src/Numeric/Product/0000755000000000000000000000000007346545000020401 5ustar0000000000000000commutative-semigroups-0.1.1.0/src/Numeric/Product/Commutative.hs0000644000000000000000000000561407346545000023240 0ustar0000000000000000{-# LANGUAGE CPP #-} module Numeric.Product.Commutative ( CommutativeProduct (..) ) where import Control.Applicative (Const) import Data.Complex (Complex) import Data.Fixed import Data.Functor.Identity import Data.Int import Data.Ratio (Ratio) import Data.Word import Numeric.Natural #if MIN_VERSION_base(4, 9, 0) import Data.Semigroup (Max, Min, Product, Sum) #endif #if MIN_VERSION_base(4, 11, 0) import Data.Ord (Down) #endif #if MIN_VERSION_base(4, 12, 0) import Data.Functor.Contravariant (Op) import Data.Monoid (Alt) #endif -- | Subclass of 'Num' where '(*)' is commutative. -- -- 'Num' doesn't demand commutative '(*)', and there are reasonable -- "real-world" instances with non-commutative multiplication. There -- is also no canonical subclass in @base@ that would suffice, as both -- 'Integral' and 'Floating' imply commutative '(*)' for different -- reasons. -- -- Two examples of non-commutative '(*)': -- -- * @Linear.Quaternion.Quaterion@ from the @linear@ package has a -- 'Num' instance, and quaternion multiplication is -- noncommutative. -- -- * @Data.Matrix.Matrix@ from the @matrix@ package uses '(*)' for -- matrix multiplication, which is also non-commutative (on square -- matrices, which is the only time the question makes sense). -- -- @since 0.1.0 class Num a => CommutativeProduct a instance CommutativeProduct Int8 instance CommutativeProduct Int16 instance CommutativeProduct Int32 instance CommutativeProduct Int64 instance CommutativeProduct Int instance CommutativeProduct Integer instance CommutativeProduct Word8 instance CommutativeProduct Word16 instance CommutativeProduct Word32 instance CommutativeProduct Word64 instance CommutativeProduct Word instance CommutativeProduct Natural instance CommutativeProduct Float instance CommutativeProduct Double instance (RealFloat a, CommutativeProduct a) => CommutativeProduct (Complex a) instance CommutativeProduct a => CommutativeProduct (Identity a) -- @since: base-4.7.0.0 instance CommutativeProduct a => CommutativeProduct (Sum a) -- @since: base-4.7.0.0 instance (Integral a, CommutativeProduct a) => CommutativeProduct (Ratio a) instance (HasResolution a, CommutativeProduct a) => CommutativeProduct (Fixed a) #if MIN_VERSION_base(4, 9, 0) -- @since: base-4.9.0.0 instance CommutativeProduct a => CommutativeProduct (Min a) -- @since: base-4.9.0.0 instance CommutativeProduct a => CommutativeProduct (Max a) -- @since: base-4.9.0.0 instance CommutativeProduct a => CommutativeProduct (Product a) -- @since: base-4.9.0.0 instance CommutativeProduct a => CommutativeProduct (Const a b) #endif #if MIN_VERSION_base(4, 11, 0) -- @since: base-4.11.0.0 instance CommutativeProduct a => CommutativeProduct (Down a) #endif #if MIN_VERSION_base(4, 12, 0) -- @since: base-4.12.0.0 instance CommutativeProduct a => CommutativeProduct (Op a b) -- @since: base-4.12.0.0 instance CommutativeProduct (f a) => CommutativeProduct (Alt f a) #endif