sign 0.1.0 → 0.2.0
raw patch · 2 files changed
+45/−6 lines, 2 filesdep ~QuickCheckdep ~test-framework-quickcheck2
Dependency ranges changed: QuickCheck, test-framework-quickcheck2
Files
- sign.cabal +13/−2
- src/Data/Sign.hs +32/−4
sign.cabal view
@@ -1,5 +1,5 @@ Name: sign-Version: 0.1.0+Version: 0.2.0 License: BSD3 License-File: LICENSE Author: Masahiro Sakai (masahiro.sakai@gmail.com)@@ -35,7 +35,18 @@ Type: exitcode-stdio-1.0 HS-Source-Dirs: test Main-is: TestSign.hs- Build-depends: base >=4 && <5, containers, sign, test-framework, test-framework-th, test-framework-hunit, test-framework-quickcheck2, HUnit, QuickCheck >=2 && <3+ Build-depends:+ base >=4 && <5,+ containers,+ sign,+ test-framework,+ test-framework-th,+ test-framework-hunit,+ -- NOTE: test-framework-quickcheck2 >=0.2.12.3 is necessary for QuickCheck >=2.5+ test-framework-quickcheck2 >=0.2.12.3 && <0.4.0,+ HUnit,+ -- NOTE: arbitraryBoundedEnum and coarbitraryEnum exist after QuickCheck-2.5+ QuickCheck >=2.5 && <3 Default-Language: Haskell2010 Other-Extensions: TemplateHaskell
src/Data/Sign.hs view
@@ -9,13 +9,18 @@ -- Stability : provisional -- Portability : non-portable (FlexibleInstances, DeriveDataTypeable, CPP) ----- Algebra of Signs.---+-- This module provides arithmetic over signs (i.e. {-, 0, +}) and set of signs.+-- +-- For the purpose of abstract interpretation, it might be convenient to use+-- 'L.Lattice' instance. See also lattices package+-- (<http://hackage.haskell.org/package/lattices>).+-- ----------------------------------------------------------------------------- module Data.Sign (- -- * Algebra of Sign+ -- * The Sign data type Sign (..)+ -- * Operations over signs , negate , abs , mult@@ -24,6 +29,8 @@ , pow , signOf , symbol+ -- * Operations over sets of signs+ -- $SET ) where import qualified Prelude as P@@ -38,7 +45,11 @@ import Data.Data import qualified Numeric.Algebra as Alg -data Sign = Neg | Zero | Pos+-- | Signs of real numbers.+data Sign+ = Neg -- ^ negative+ | Zero -- ^ zero+ | Pos -- ^ positive deriving (Eq, Ord, Show, Read, Enum, Bounded, Typeable, Data) instance NFData Sign@@ -64,16 +75,19 @@ (\\) = flip div (^) = pow +-- | Unary negation. negate :: Sign -> Sign negate Neg = Pos negate Zero = Zero negate Pos = Neg +-- | Absolute value. abs :: Sign -> Sign abs Neg = Pos abs Zero = Zero abs Pos = Pos +-- | Multiplication. mult :: Sign -> Sign -> Sign mult Pos s = s mult s Pos = s@@ -81,22 +95,26 @@ mult s Neg = negate s mult _ _ = Zero +-- | Reciprocal fraction. recip :: Sign -> Sign recip Pos = Pos recip Zero = error "Data.Sign.recip: division by Zero" recip Neg = Neg +-- | Fractional division. div :: Sign -> Sign -> Sign div s Pos = s div _ Zero = error "Data.Sign.div: division by Zero" div s Neg = negate s +-- | Fractional division. pow :: Integral x => Sign -> x -> Sign pow _ 0 = Pos pow Pos _ = Pos pow Zero _ = Zero pow Neg n = if even n then Pos else Neg +-- | Sign of a number. signOf :: Real a => a -> Sign signOf r = case r `compare` 0 of@@ -104,10 +122,20 @@ EQ -> Zero GT -> Pos +-- | Mnemonic symbol of a number.+--+-- This function returns @\"-\"@, @\"0\"@, @\"+\"@ respectively for 'Neg', 'Zero', 'Pos'. symbol :: Sign -> String symbol Pos = "+" symbol Neg = "-" symbol Zero = "0"++-- $SET+-- @'Set' 'Sign'@ is equipped with instances of 'Num' and 'Fractional'.+-- Therefore arithmetic operations can be applied to @'Set' 'Sign'@.+-- +-- Instances of 'L.Lattice' and 'L.BoundedLattice' are also provided for+-- the purpose of abstract interpretation. instance L.MeetSemiLattice (Set Sign) where meet = Set.intersection