positive (empty) → 0.1
raw patch · 4 files changed
+113/−0 lines, 4 filesdep +basedep +natssetup-changed
Dependencies added: base, nats
Files
- COPYING +26/−0
- Setup.hs +4/−0
- positive.cabal +28/−0
- src/Numeric/Positive.hs +55/−0
+ COPYING view
@@ -0,0 +1,26 @@++Copyright (c) 2013, Hans Höglund+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 the <organization> nor the+ names of its 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 <COPYRIGHT HOLDER> 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,4 @@+#! /usr/bin/env runhaskell++import Distribution.Simple+main = defaultMain
+ positive.cabal view
@@ -0,0 +1,28 @@++name: positive+version: 0.1+cabal-version: >= 1.10+author: Hans Hoglund+maintainer: Hans Hoglund <hans@hanshoglund.se>+license: BSD3+license-file: COPYING+synopsis: Positive numbers.+category: +tested-with: GHC+build-type: Simple++description:+ To be written.++source-repository head+ type: git+ location: git://github.com/hanshoglund/foobar.git++library+ build-depends:+ base >= 4 && < 5,+ nats >= 0.2 && < 1+ hs-source-dirs: src+ default-language: Haskell2010+ exposed-modules:+ Numeric.Positive
+ src/Numeric/Positive.hs view
@@ -0,0 +1,55 @@++{-# LANGUAGE GeneralizedNewtypeDeriving #-}++module Numeric.Positive (+ Positive,+ positive+ ) where+import Numeric.Natural++newtype Positive = Positive { getPositive :: Natural } deriving+ ( + Eq,+ Ord,+ -- Data,+ Real,+ -- Ix,+ -- Typeable,+ -- Bits,+ -- Hashable,+ Whole+ )++instance Show Positive where+ show (Positive n) = show n++instance Read Positive where+ readsPrec d = map (\(n, s) -> (fromInteger n, s)) . readsPrec d++instance Integral Positive where+ toInteger (Positive a) = toInteger a+ Positive a `quotRem` Positive b = (fromIntegral $ a `quot` b, fromIntegral $ a `rem` b)++instance Enum Positive where+ toEnum = fromIntegral+ fromEnum = fromIntegral+ +instance Num Positive where+ Positive n + Positive m = Positive (n + m)+ Positive n * Positive m = Positive (n * m)+ Positive n - Positive m+ | r < 0 = error "Positive.(-): negative result"+ | r == 0 = error "Positive.(-): result zero"+ | otherwise = Positive r+ where+ r = n - m+ abs = id+ signum _ = 1+ fromInteger n+ | n > 0 = Positive (fromInteger n)+ | n == 0 = error "Positive.fromInteger: zero"+ | n < 0 = error "Positive.fromInteger: negative"++positive :: a -> (a -> a) -> Positive -> a+positive a f 1 = f a+positive a f n = f (positive a f $ pred n)