AC-HalfInteger (empty) → 1.1.1
raw patch · 4 files changed
+146/−0 lines, 4 filesdep +basesetup-changed
Dependencies added: base
Files
- AC-HalfInteger.cabal +24/−0
- Data/HalfInteger.hs +109/−0
- Licence.txt +10/−0
- Setup.hs +3/−0
+ AC-HalfInteger.cabal view
@@ -0,0 +1,24 @@+Cabal-Version: >= 1.6 +Name: AC-HalfInteger +Version: 1.1.1 +Stability: Experimental +Synopsis: Efficient half-integer type. + +Description: + + This Haskell library is a simple implementation of + /half-integers/. That is, it provides a type that can + represent both normal integers and integers plus a half. + +Category: Data, Math, Numerical +License: BSD3 +License-file: Licence.txt +Author: Andrew Coppin +Maintainer: MathematicalOrchid@hotmail.com +Build-Type: Simple +Tested-With: GHC == 6.10.3 + +Library + Exposed-modules: Data.HalfInteger + Build-Depends: base >= 4 && < 5 + HS-Source-Dirs: .
+ Data/HalfInteger.hs view
@@ -0,0 +1,109 @@+{- | + This module provides efficient /half-integers/. That is, + normal integers and integers plus or minus 1/2. These are + sometimes useful for when a value can be an integer, or midway + between two integers. +-} + +module Data.HalfInteger + ( + HalfInteger (), + half, halve, double, + toHalfInteger, fromHalfInteger, + isInteger + ) + where + +{- | + The type of half-integers. The bounds are determined by the type + variable. For example, @HalfInteger Int@ has a range half as large + as @Int@ itself. (The @Bounded@ instance correctly reflects this.) + Note that @HalfInteger Integer@ is unbounded, like @Integer@ + itself. + + @HalfInteger@ values can be constructed as numeric literals + (e.g., @5 :: HalfInteger Int@), by conversions such as + @fromInteger@ or @fromIntegral@, or by several functions in this + module. Another common pattern is to write (say) @5 + half@ to + represent 5 1/2. Indeed, the @Show@ instance represents values + in this way. + + Beware: The half-integers are /not/ closed under multiplication! + For example, @half * half@ should yield 1/4, which is not a valid + @HalfInteger@. (Currently it yields zero.) Addition and + subtraction, however, /are/ closed, and thus yield exact results. +-} +newtype HalfInteger i = HalfInteger i + +{- | + Represents 1/2 as a @HalfInteger@. You can add this to integral + @HalfInteger@ values created in various ways to get the + half-part in. +-} +half :: (Num i) => HalfInteger i +half = HalfInteger 1 + +{- | + Take an integer and halve its value, yielding a @HalfInteger@. + This conversion is always exact, and @half . double == id@. +-} +halve :: i -> HalfInteger i +halve = HalfInteger + +{- | + Take a @HalfInteger@ and double its value, yielding a normal integer. + This conversion is always exact, and @double . half == id@. +-} +double :: HalfInteger i -> i +double (HalfInteger x) = x + +instance (Eq i) => Eq (HalfInteger i) where + (HalfInteger x) == (HalfInteger y) = x == y + +instance (Ord i) => Ord (HalfInteger i) where + compare (HalfInteger x) (HalfInteger y) = compare x y + +instance (Bounded i) => Bounded (HalfInteger i) where + minBound = HalfInteger minBound + maxBound = HalfInteger maxBound + +instance (Integral i) => Show (HalfInteger i) where + showsPrec p (HalfInteger x) = + let + i = signum x * (abs x `div` 2) + s = if x < 0 then "-" else "+" + in + showParen (p > 6) $ + ("fromInteger " ++) . + showsPrec 10 i . + if odd x then (" " ++ ) . (s ++) . (" half" ++) else id + +instance (Integral i) => Num (HalfInteger i) where + (HalfInteger x) + (HalfInteger y) = HalfInteger (x + y) + (HalfInteger x) - (HalfInteger y) = HalfInteger (x - y) + (HalfInteger x) * (HalfInteger y) = HalfInteger (x * y `div` 2) + negate (HalfInteger x) = HalfInteger ( negate x) + abs (HalfInteger x) = HalfInteger ( abs x) + signum (HalfInteger x) = HalfInteger (2 * signum x) + fromInteger x = HalfInteger (2 * fromInteger x) + +{- | + Convert any number into a @HalfInteger@. The rounding is somewhat + unpredictable, but +-} +toHalfInteger :: (RealFrac x, Integral i) => x -> HalfInteger i +toHalfInteger x = HalfInteger (round $ 2 * x) + +{- | + Convert a @HalfInteger@ into some other kind of number. + This conversion is always exact. +-} +fromHalfInteger :: (Integral i, Fractional x) => HalfInteger i -> x +fromHalfInteger (HalfInteger x) = fromIntegral x / 2 + +{- | + Returns @True@ if this @HalfInteger@ can be exactly represented + as an ordinary integer, and @False@ if there is a half offset. +-} +isInteger :: (Integral i) => HalfInteger i -> Bool +isInteger (HalfInteger x) = even x
+ Licence.txt view
@@ -0,0 +1,10 @@+Copyright (c) 2009, Andrew Coppin +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 Andrew Coppin nor the names of the 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 HOLDER 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,3 @@+import Distribution.Simple + +main = defaultMain