zero (empty) → 0.1
raw patch · 5 files changed
+153/−0 lines, 5 filesdep +basedep +semigroupssetup-changed
Dependencies added: base, semigroups
Files
- CHANGELOG.md +3/−0
- LICENSE +30/−0
- Setup.hs +2/−0
- src/Data/Zero.hs +72/−0
- zero.cabal +46/−0
+ CHANGELOG.md view
@@ -0,0 +1,3 @@+## 0.1++- Initial revision.
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2015, Dimitri Sabadie <dimitri.sabadie@gmail.com>++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 Dimitri Sabadie <dimitri.sabadie@gmail.com> 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.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ src/Data/Zero.hs view
@@ -0,0 +1,72 @@+-----------------------------------------------------------------------------+-- |+-- Copyright : (C) 2015 Dimitri Sabadie+-- License : BSD3+--+-- Maintainer : Dimitri Sabadie <dimitri.sabadie@gmail.com>+-- Stability : experimental+-- Portability : portable+-----------------------------------------------------------------------------++module Data.Zero where++import Control.Monad.Fix ( MonadFix )+import Data.Semigroup ( Semigroup(..) )+import GHC.Generics ( Generic )++-- |'Semigroup' with a 'zero' element. It’s important to understand that the+-- standard 'Semigroup' types – i.e. 'Maybe' and so on – are already biased,+-- because they’re 'Monoid's. That’s why you’ll find a few 'Zero' instances.+--+-- Should satisfies the following laws:+--+-- ==== Annhilation+--+-- @ a '<>' 'zero' = 'zero' '<>' a = 'zero' @+--+-- ==== Associativity+--+-- @ a '<>' b '<>' c = (a '<>' b) '<>' c = a '<>' (b '<>' c) @+class (Semigroup a) => Zero a where+ -- |The zero element.+ zero :: a+ -- |Concat all the elements according to ('<>') and 'zero'.+ zconcat :: [a] -> a++ default zconcat :: (Semigroup a) => [a] -> a+ zconcat [] = zero+ zconcat (x:xs) = foldr (<>) x xs+ {-# MINIMAL zero #-}++instance Zero () where+ zero = ()++-- |'Zero' under multiplication.+newtype Product a = Product { getProduct :: a }+ deriving (Bounded,Eq,Generic,Num,Ord,Read,Show)++instance (Num a) => Semigroup (Product a) where+ Product a <> Product b = Product $ a * b++instance (Num a) => Zero (Product a) where+ zero = Product 0++-- |'Zero' under boolean logical and.+newtype All = All { getAll :: Bool} deriving (Bounded,Eq,Generic,Ord,Read,Show)++instance Semigroup All where+ All a <> All b = All $ a && b++instance Zero All where+ zero = All False++-- |'Zero' for 'Maybe'.+newtype Success a = Success { getSuccess :: Maybe a }+ deriving (Applicative,Eq,Foldable,Functor,Monad,MonadFix,Ord,Traversable,Read,Show)++instance (Semigroup a) => Semigroup (Success a) where+ Success (Just a) <> Success (Just b) = Success . Just $ a <> b+ _ <> _ = zero++instance (Semigroup a) => Zero (Success a) where+ zero = Success Nothing
+ zero.cabal view
@@ -0,0 +1,46 @@+name: zero+version: 0.1+synopsis: Semigroups with absorption+description: 'Monoid' is a 'Semigroup' glued with a neutral element+ called 'mempty'. In the same idea, 'Zero' is a 'Semigroup'+ glued with an absorbing element called 'zero'.+ .+ Keep in mind that 'Zero' requires 'Semigroup'. If you have+ 'Semigroup' defined to work with 'Monoid', you might end up+ with no way to implement 'Zero'. That’s why the 'Semigroup'+ instance for 'Maybe' is confusing, because it relies on+ 'Monoid', and cannot be used with 'Zero'. 'Success' is the+ 'Zero' equivalent of 'Maybe' + 'Monoid'.+license: BSD3+license-file: LICENSE+author: Dimitri Sabadie <dimitri.sabadie@gmail.com>+maintainer: Dimitri Sabadie <dimitri.sabadie@gmail.com>+copyright: Dimitri Sabadie+category: Data+extra-source-files: CHANGELOG.md+homepage: https://github.com/phaazon/zero+bug-reports: https://github.com/phaazon/zero/issues++build-type: Simple+cabal-version: >= 1.10++source-repository head+ type: git+ location: https://github.com/phaazon/zero.git++library+ ghc-options: -W -Wall++ exposed-modules: Data.Zero++ default-extensions: DefaultSignatures+ , DeriveGeneric+ , DeriveTraversable+ , GeneralizedNewtypeDeriving++ build-depends: base >= 4.8 && < 4.9+ , semigroups >= 0.16 && < 0.17++ hs-source-dirs: src++ default-language: Haskell2010