positive-integer 0.1.0.0 → 0.1.1.0
raw patch · 2 files changed
+38/−2 lines, 2 filesdep ~basePVP ok
version bump matches the API change (PVP)
Dependency ranges changed: base
API changes (from Hackage documentation)
+ Numeric.PositiveInteger: fromTypeNat :: forall (n :: Nat) proxy. (KnownNat n, n > 0) => proxy n -> PositiveInteger
+ Numeric.PositiveInteger: withTypeNat :: PositiveInteger -> (forall (proxy :: Nat -> Type) (n :: Nat). (KnownNat n, n > 0) => proxy n -> a) -> a
Files
- lib/Numeric/PositiveInteger.hs +35/−0
- positive-integer.cabal +3/−2
lib/Numeric/PositiveInteger.hs view
@@ -1,4 +1,9 @@ {-# LANGUAGE CPP #-}+{-# LANGUAGE TypeOperators #-}+{-# LANGUAGE DataKinds #-}+{-# LANGUAGE GADTs #-}+{-# LANGUAGE RankNTypes #-}+{-# LANGUAGE TypeApplications #-} -- | This module provides a type for the positive integers, together -- with conversion functions and other operations.@@ -16,10 +21,15 @@ ( PositiveInteger , one -- * Conversions+ -- ** To @PositiveInteger@ , fromNatural , fromIntegral+ -- ** From @PositiveInteger@ , toNatural , toNum+ -- ** Type-level conversions+ , fromTypeNat+ , withTypeNat -- * Operations , next , add@@ -39,6 +49,9 @@ import Numeric.Natural (Natural) import Text.Read (readPrec) import Control.Monad qualified as Monad+import GHC.TypeNats (KnownNat, SomeNat (..), someNatVal, natVal, cmpNat)+import Data.Type.Ord (type (>), OrderingI (..))+import Data.Proxy (Proxy (..)) #if MIN_VERSION_base(4,18,0) import Data.Foldable1 (Foldable1)@@ -95,6 +108,28 @@ toNum :: Num a => PositiveInteger -> a {-# INLINE toNum #-} toNum (PositiveInteger n) = Prelude.fromIntegral n++-- | Convert a type-level natural to a positive integer.+-- Equivalent to 'natVal' for positive integers.+--+-- @since 0.1.1.0+fromTypeNat :: (KnownNat n, n > 0) => proxy n -> PositiveInteger+{-# INLINE fromTypeNat #-}+fromTypeNat = PositiveInteger . natVal++-- | Convert a positive integer to a type-level natural and apply+-- a function to it.+--+-- @since 0.1.1.0+withTypeNat :: PositiveInteger -> (forall proxy n . (KnownNat n, n > 0) => proxy n -> a) -> a+{-# INLINE withTypeNat #-}+withTypeNat (PositiveInteger n) f =+ case someNatVal n of+ SomeNat proxy ->+ case cmpNat proxy (Proxy @0) of+ GTI -> f proxy+ -- This case should be impossible.+ _ -> error "Positive integer is not positive!" -- | Return the next positive integer. next :: PositiveInteger -> PositiveInteger
positive-integer.cabal view
@@ -1,11 +1,12 @@ name: positive-integer-version: 0.1.0.0+version: 0.1.1.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)+homepage: https://codeberg.org/daniel-casanueva/positive-integer license: MIT license-file: license cabal-version: 1.18@@ -17,4 +18,4 @@ exposed-modules: Numeric.PositiveInteger ghc-options: -Wall default-extensions: ImportQualifiedPost- build-depends: base >= 4.14.0.0 && < 4.22.0.0+ build-depends: base >= 4.16.0.0 && < 4.22.0.0