semirings 0.1.1 → 0.1.2
raw patch · 4 files changed
+70/−45 lines, 4 filesdep +semigroupsdep +transformersdep ~basedep ~constrictorPVP ok
version bump matches the API change (PVP)
Dependencies added: semigroups, transformers
Dependency ranges changed: base, constrictor
API changes (from Hackage documentation)
Files
- CHANGELOG.md +6/−0
- Data/Semiring.hs +50/−33
- Data/Semiring/Generic.hs +6/−6
- semirings.cabal +8/−6
CHANGELOG.md view
@@ -1,3 +1,9 @@+0.1.2: [2018.05.04]+-------------------+* `semirings` now builds back to+ GHC-7.4.1.+* many doc fixes.+ 0.1.1: [2018.04.20] ------------------- * Remove unused `coerce-util` dependency.
Data/Semiring.hs view
@@ -11,29 +11,28 @@ {-# OPTIONS_GHC -fno-warn-missing-methods #-} module Data.Semiring- ( Semiring(..)- , Ring(..)+ ( -- * Semiring typeclass+ Semiring(..) , (+) , (*)- , (-) , (^)- , minus , foldMapP , foldMapT , sum , prod , sum' , prod'+ + -- * Ring typeclass + , Ring(..)+ , (-)+ , minus ) where #if defined(VERSION_constrictor)-#if MIN_VERSION_constrictor(0,1,1) import Constrictor (Ap(..))-#else-import Control.Monad.Constrictor (Ap(..)) #endif-#endif-import Control.Applicative (Alternative(..), Applicative(..), Const(..))+import Control.Applicative (Alternative(..), Applicative(..), Const(..), liftA2) import Data.Bool (Bool(..), (||), (&&), otherwise, not) import Data.Complex (Complex(..)) import Data.Eq (Eq(..))@@ -50,18 +49,24 @@ import Data.HashSet (HashSet) import qualified Data.HashSet as HashSet #endif-#if defined(VERSION_containers) import Data.Int (Int, Int8, Int16, Int32, Int64)+import Data.Maybe (Maybe(..))+#if defined(VERSION_containers) import Data.IntMap (IntMap) import qualified Data.IntMap as IntMap import Data.IntSet (IntSet) import qualified Data.IntSet as IntSet import Data.Map (Map) import qualified Data.Map as Map-import Data.Maybe (Maybe(..)) #endif-import Data.Monoid (Monoid(..),Dual(..), Endo(..), Alt(..), Product(..), Sum(..))-import Data.Ord (Down(..), Ord(..), Ordering(..), compare)+import Data.Monoid (Monoid(..),Dual(..), Endo(..), Product(..), Sum(..))+#if MIN_VERSION_base(4,8,0)+import Data.Monoid (Alt(..))+#endif+import Data.Ord (Ord(..), Ordering(..), compare)+#if MIN_VERSION_base(4,6,0)+import Data.Ord (Down(..))+#endif import Data.Ratio (Ratio) import Data.Semigroup (Max(..), Min(..)) #if defined(VERSION_containers)@@ -91,9 +96,7 @@ import GHC.Integer (Integer) import qualified GHC.Num as Num import GHC.Real (Integral, quot, even)-#if __GLASGOW_HASKELL__ >= 710 import Numeric.Natural (Natural)-#endif import System.Posix.Types (CCc, CDev, CGid, CIno, CMode, CNlink, COff, CPid, CRLim, CSpeed, CSsize,@@ -172,8 +175,10 @@ -- | The class of semirings (types with two binary -- operations and two respective identities). One -- can think of a semiring as two monoids of the same--- underlying type: An commutative monoid and an--- associative one.+-- underlying type: A commutative monoid and an+-- associative monoid. For any type R with a 'Prelude.Num'+-- instance, the commutative monoid is (R, '(Prelude.+)', 0)+-- and the associative monoid is (R, '(Prelude.*)', 1). -- -- Instances should satisfy the following laws: --@@ -187,7 +192,7 @@ -- -- [/additive commutativity/] -- --- @x '+' y = y '+' x+-- @x '+' y = y '+' x@ -- -- [/multiplicative identity/] -- @@ -204,25 +209,29 @@ -- -- [/annihilation/] ----- @'zero' '*' x = x '*' 'zero' = 'zero'+-- @'zero' '*' x = x '*' 'zero' = 'zero'@ class Semiring a where {-# MINIMAL plus, zero, times, one #-}- plus :: a -> a -> a -- ^ Commutative Additive Operation- zero :: a -- ^ Additive Unit- times :: a -> a -> a -- ^ Associative Multiplicative Operation- one :: a -- ^ Multiplicative Unit+ plus :: a -> a -> a -- ^ Commutative Operation+ zero :: a -- ^ Commutative Unit+ times :: a -> a -> a -- ^ Associative Operation+ one :: a -- ^ Associative Unit -- useful for defining semirings over ground types- default zero :: Num.Num a => a- default one :: Num.Num a => a- default plus :: Num.Num a => a -> a -> a- default times :: Num.Num a => a -> a -> a+ default zero :: Num.Num a => a -- ^ 0+ default one :: Num.Num a => a -- ^ 1+ default plus :: Num.Num a => a -> a -> a -- ^ '(Prelude.+)'+ default times :: Num.Num a => a -> a -> a -- ^ '(Prelude.*)' zero = 0 one = 1 plus = (Num.+) times = (Num.*) +-- | The class of semirings with an additive inverse.+--+-- @'negate' a '+' a = 'zero'@+ class Semiring a => Ring a where {-# MINIMAL negate #-} negate :: a -> a@@ -230,6 +239,10 @@ default negate :: Num.Num a => a -> a negate = Num.negate +-- | Substract two 'Ring' values. For any type 'R' with+-- a 'Prelude.Num' instance, this is the same as '(Prelude.-)'.+--+-- @x `minus` y = x '+' 'negate' y@ minus :: Ring a => a -> a -> a minus x y = x + negate y @@ -324,6 +337,7 @@ instance (Monoid a, Ring a) => Ring (Endo a) where negate (Endo f) = Endo (negate f) +#if MIN_VERSION_base(4,8,0) instance (Alternative f, Semiring a) => Semiring (Alt f a) where zero = empty one = Alt (pure one)@@ -332,6 +346,7 @@ instance (Alternative f, Ring a) => Ring (Alt f a) where negate = fmap negate+#endif instance Semiring a => Semiring (Const a b) where zero = Const zero@@ -406,14 +421,14 @@ instance Semiring CMode instance Semiring CIno instance Semiring CDev-#if __GLASGOW_HASKELL__ >= 710 instance Semiring Natural-#endif instance Integral a => Semiring (Ratio a) deriving instance Semiring a => Semiring (Product a) deriving instance Semiring a => Semiring (Sum a) deriving instance Semiring a => Semiring (Identity a)+#if MIN_VERSION_base(4,6,0) deriving instance Semiring a => Semiring (Down a)+#endif deriving instance Semiring a => Semiring (Max a) deriving instance Semiring a => Semiring (Min a) instance HasResolution a => Semiring (Fixed a)@@ -472,11 +487,11 @@ instance Ring CMode instance Ring CIno instance Ring CDev-#if __GLASGOW_HASKELL__ >= 710 instance Ring Natural-#endif instance Integral a => Ring (Ratio a)+#if MIN_VERSION_base(4,6,0) deriving instance Ring a => Ring (Down a)+#endif deriving instance Ring a => Ring (Product a) deriving instance Ring a => Ring (Sum a) deriving instance Ring a => Ring (Identity a)@@ -520,7 +535,8 @@ = Map.fromListWith (+) [ (plus k l, v * u) | (k,v) <- Map.toList xs- , (l,u) <- Map.toList ys ]+ , (l,u) <- Map.toList ys+ ] instance Semiring IntSet where zero = IntSet.empty@@ -536,7 +552,8 @@ = IntMap.fromListWith (+) [ (plus k l, v * u) | (k,v) <- IntMap.toList xs- , (l,u) <- IntMap.toList ys ]+ , (l,u) <- IntMap.toList ys+ ] instance (Semiring a) => Semiring (Seq a) where zero = Seq.empty
Data/Semiring/Generic.hs view
@@ -8,7 +8,7 @@ {-# OPTIONS_GHC -fno-warn-orphans #-} -------------------------------------------------------------------- ----------+----------------------------------------------------------------------------- -- | -- Module : Data.Semiring.Generic -- Copyright : (C) 2018 chessai@@ -21,7 +21,7 @@ -- This module provides generic deriving tools for semirings and rings for -- product-like structures. ---------------------------------------------------------------------- ---------+---------------------------------------------------------------------------- module Data.Semiring.Generic ( @@ -93,7 +93,7 @@ {-# MINIMAL gnegate' #-} gnegate' :: f a -> f a --- | Generically generate a 'Semiring' 'zero' for any product-like type+-- | Generically generate a 'Semiring' 'zero' for any product-like type -- implementing 'Generic'. -- -- It is only defined for product types.@@ -104,7 +104,7 @@ gzero :: (Generic a, GSemiring (Rep a)) => a gzero = to gzero' --- | Generically generate a 'Semiring' 'one' for any product-like t ype+-- | Generically generate a 'Semiring' 'one' for any product-like type -- implementing 'Generic'. -- -- It is only defined for product types.@@ -115,7 +115,7 @@ gone :: (Generic a, GSemiring (Rep a)) => a gone = to gone' --- | Generically generate a 'Semiring' 'plus' operation for any typ e+-- | Generically generate a 'Semiring' 'plus' operation for any type -- implementing 'Generic'. It is only defined for product types. -- -- @@@ -124,7 +124,7 @@ gplus :: (Generic a, GSemiring (Rep a)) => a -> a -> a gplus x y = to $ from x `gplus'` from y --- | Generically generate a 'Semiring' 'times' operation for any ty pe+-- | Generically generate a 'Semiring' 'times' operation for any type -- implementing 'Generic'. It is only defined for product types. -- -- @
semirings.cabal view
@@ -1,6 +1,6 @@ name: semirings category: Algebra, Data, Data Structures, Math, Maths, Mathematics-version: 0.1.1+version: 0.1.2 license: BSD3 cabal-version: >= 1.10 license-file: LICENSE@@ -86,9 +86,14 @@ ghc-options: -Wall build-depends:- base >= 2 && < 5+ base >= 4.5 && < 5 , integer-gmp+ , semigroups+ , transformers + if impl(ghc < 7.10)+ build-depends: nats >= 0.1 && < 2+ if impl(ghc >= 7.2) exposed-modules: Data.Semiring@@ -97,11 +102,8 @@ exposed-modules: Data.Semiring.Generic - if impl(ghc < 7.10)- build-depends: nats >= 0.1 && < 2- if flag(constrictor)- build-depends: constrictor >= 0.1.1.0 && < 0.2.0.0+ build-depends: constrictor >= 0.1.1.1 && < 0.2.0.0 if flag(containers) build-depends: containers >= 0.3 && < 0.6