packages feed

positive-integer (empty) → 0.1.0.0

raw patch · 4 files changed

+217/−0 lines, 4 filesdep +base

Dependencies added: base

Files

+ lib/Numeric/PositiveInteger.hs view
@@ -0,0 +1,162 @@+{-# LANGUAGE CPP #-}++-- | This module provides a type for the positive integers, together+--   with conversion functions and other operations.+--+--   When an operation is partial (for example, subtraction), the result+--   has a 'Maybe' type, letting the user handle such case, without the+--   need for handling exceptions during evaluation.+--   As a result, 'PositiveInteger' is not an instance of the 'Num' class+--   or any of its subclasses. Functions are provided that offer similar+--   functionality.+--+--   It is recommended that you import this module qualified, as some of+--   the function names clash with those from the Prelude.+module Numeric.PositiveInteger+  ( PositiveInteger+  , one+    -- * Conversions+  , fromNatural+  , fromIntegral+  , toNatural+  , toNum+    -- * Operations+  , next+  , add+  , sub+  , mul+  , div+    -- * Positive lengths+  , length+    -- * Non-empty lists+  , take+  , replicate+  , replicateM+    ) where++import Prelude hiding (fromIntegral, div, length, take, replicate)+import Prelude qualified+import Numeric.Natural (Natural)+import Text.Read (readPrec)+import Control.Monad qualified as Monad++#if MIN_VERSION_base(4,18,0)+import Data.Foldable1 (Foldable1)+#else+import Control.Applicative (liftA2)+#endif++import Data.List.NonEmpty (NonEmpty)+import Data.List.NonEmpty qualified as NonEmpty++-- | Unbounded positive integers.+--+--   No 'Num' instance is provided, as no negative integers are available.+newtype PositiveInteger = PositiveInteger Natural deriving (Eq, Ord)++-- | The number 1.+one :: PositiveInteger+{-# INLINE one #-}+one = PositiveInteger 1++instance Show PositiveInteger where+  show (PositiveInteger n) = show n++instance Read PositiveInteger where+  readPrec = do+    n <- readPrec+    Monad.guard (n /= 0)+    pure $ PositiveInteger n++-- | Convert from a 'Natural' number, returning 'Nothing' if the input+--   is 0.+fromNatural :: Natural -> Maybe PositiveInteger+{-# INLINE fromNatural #-}+fromNatural n = if n == 0 then Nothing else Just (PositiveInteger n)++-- | Convert from a type that's instance of 'Integral'.+--   Similar to Prelude's 'Prelude.fromIntegral', but the result is+--   a 'Maybe', since the input might not be positive.+fromIntegral :: Integral a => a -> Maybe PositiveInteger+{-# INLINE fromIntegral #-}+fromIntegral n =+  if n > 0+     then Just (PositiveInteger $ Prelude.fromIntegral n)+     else Nothing++-- | Cast a positive integer to a natural number.+toNatural :: PositiveInteger -> Natural+{-# INLINE toNatural #-}+toNatural (PositiveInteger n) = n++-- | Convert a positive integer to any type instance of 'Num'.+--   This is equivalent to Prelude's 'Prelude.fromIntegral' when the input+--   type is 'PositiveInteger'.+toNum :: Num a => PositiveInteger -> a+{-# INLINE toNum #-}+toNum (PositiveInteger n) = Prelude.fromIntegral n++-- | Return the next positive integer.+next :: PositiveInteger -> PositiveInteger+{-# INLINE next #-}+next (PositiveInteger n) = PositiveInteger (n+1)++-- | Add two positive integers.+add :: PositiveInteger -> PositiveInteger -> PositiveInteger+{-# INLINE add #-}+add (PositiveInteger n) (PositiveInteger m) = PositiveInteger (n+m)++-- | Multiply two positive integers.+mul :: PositiveInteger -> PositiveInteger -> PositiveInteger+{-# INLINE mul #-}+mul (PositiveInteger n) (PositiveInteger m) = PositiveInteger (n*m)++-- | Subtract two positive integers. If the result wouldn't be positive,+--   'Nothing' is returned.+sub :: PositiveInteger -> PositiveInteger -> Maybe PositiveInteger+{-# INLINE sub #-}+sub (PositiveInteger n) (PositiveInteger m) =+  if n > m then Just $ PositiveInteger (n - m) else Nothing++-- | Divide a positive integer by another. If the divisor is larger than+--   the dividend, 'Nothing' is returned.+div :: PositiveInteger -> PositiveInteger -> Maybe PositiveInteger+{-# INLINE div #-}+div (PositiveInteger n) (PositiveInteger m) =+  if m <= n then Just (PositiveInteger (Prelude.div n m)) else Nothing++{-++Starting on base-4.18.0.0, the type class Foldable1 becomes available.+This allows us to generalize the type of length.++-}++#if MIN_VERSION_base(4,18,0)+-- | Length of a non-empty data structure.+length :: Foldable1 t => t a -> PositiveInteger+#else+-- | Lenght of a non-empty list.+length :: NonEmpty a -> PositiveInteger+#endif+{-# INLINE length #-}+length = PositiveInteger . Prelude.fromIntegral . Prelude.length++-- | Take a positive number of elements from a non-empty list.+take :: PositiveInteger -> NonEmpty a -> NonEmpty a+take (PositiveInteger n) (x NonEmpty.:| xs) =+  x NonEmpty.:| Prelude.take (Prelude.fromIntegral $ n - 1) xs++-- | Produce a non-empty list with the same value repeated a given number of+--   times.+replicate :: PositiveInteger -> a -> NonEmpty a+{-# INLINE replicate #-}+replicate (PositiveInteger n) x =+  x NonEmpty.:| Prelude.replicate (Prelude.fromIntegral $ n - 1) x++-- | Repeat an action a given number of times and collect the+--   results in a non-empty list.+replicateM :: Applicative f => PositiveInteger -> f a -> f (NonEmpty a)+{-# INLINE replicateM #-}+replicateM (PositiveInteger n) x =+  liftA2 (NonEmpty.:|) x $ Monad.replicateM (Prelude.fromIntegral $ n - 1) x
+ license view
@@ -0,0 +1,21 @@+MIT License++Copyright (c) 2025 Daniel Díaz Casanueva++Permission is hereby granted, free of charge, to any person obtaining a copy+of this software and associated documentation files (the "Software"), to deal+in the Software without restriction, including without limitation the rights+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the Software is+furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in all+copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE+SOFTWARE.
+ positive-integer.cabal view
@@ -0,0 +1,20 @@+name: positive-integer+version: 0.1.0.0+category: Numeric+synopsis: Type of positive integers.+description: Type of positive integers together with related functions.+build-type: Simple+author: Daniel Casanueva+maintainer: Daniel Casanueva (coding `at` danielcasanueva.eu)+license: MIT+license-file: license+cabal-version: 1.18+extra-doc-files: readme.md++library+  default-language: Haskell2010+  hs-source-dirs: lib+  exposed-modules: Numeric.PositiveInteger+  ghc-options: -Wall+  default-extensions: ImportQualifiedPost+  build-depends: base >= 4.14.0.0 && < 4.22.0.0
+ readme.md view
@@ -0,0 +1,14 @@+## positive-integer++Haskell library to work with positive integers (i.e. 1, 2, 3, ...).+Some people call these the natural numbers.+However, others include 0 as a natural number.+The term _positive integer_ is always clear.+Besides, `Natural` is a widely used type in Haskell (and it _has_ 0 in it).++In addition to functions strictly related to positive integers,+functions for non-empty lists that take advantage of having positive+integers available are also included.+See Haddock documentation+[on Hackage](https://hackage.haskell.org/package/positive-integer)+for details.