integer-types

Integer, Natural, and Positive

https://github.com/typeclasses/integer-types

LTS Haskell 23.27:0.1.4.1
Stackage Nightly 2024-12-09:0.1.4.1
Latest on Hackage:0.1.4.1

See all snapshotsinteger-types appears in

Apache-2.0 licensedbyChris Martin
Maintained byChris Martin, Julie Moronuki
This version can be pinned in stack with:integer-types-0.1.4.1@sha256:4b56a906d054c41ac64578f46435947e11a69314b831c74543384da8e7061502,2254

Core types: Integer, Natural, Positive

The primary module of theinteger-types package isInteger, which exportsthe following integer-like types:

TypeRange
Integer(-∞, ∞)
Natural(0, ∞)
Positive(1, ∞)

The Signed type

In addition toInteger, there is also an equivalent type calledSigned thatis represented as:

data Signed = Zero | NonZero Sign Positivedata Sign = MinusSign | PlusSign

Signed also comes with bundled pattern synonyms that allow it to be used as ifit had the following definition:

data Signed = Minus Positive | Zero | Plus Positive

Monomorphic conversions

The following modules contain monomorphic conversion functions:

  • Integer.Integer
  • Integer.Natural
  • Integer.Positive
  • Integer.Signed

For example, you can convert fromPositive toInteger using eitherInteger.Positive.toInteger orInteger.Integer.fromPositive, which are twonames for the same function of typePositive -> Integer.

Since not all integers are positive, the corresponding function in the reversedirection has aMaybe codomain.Integer.Integer.toPositive andInteger.Positive.fromInteger have the typeInteger -> Maybe Positive.

Polymorphic conversions

TheInteger module exports two polymorphic conversion functions. The first isfor conversions that always succeed, such asPositive -> Integer.

convert :: IntegerConvert a b => a -> b

The second is for conversions that may fail because they convert to a subset ofthe domain, such asInteger -> Maybe Positive.

narrow :: IntegerNarrow a b => a -> Maybe b

Finite integer subsets

In addition to the conversion utilities discussed above, this library alsoprovides some minimal support for converting to/from theWord andInt types.These are system-dependent finite subsets ofInteger that are sometimes usedfor performance reasons.

toFinite   :: (ConvertWithFinite a, Finite b) => a -> Maybe bfromFinite :: (ConvertWithFinite a, Finite b) => b -> Maybe a

For example,toFinite may specialize asPositive -> Maybe Int, andfromFinite may specialize asInt -> Maybe Positive.

Monomorphic subtraction

For theInteger andSigned types that represent the full range of integers,the standard arithmetic operations in theNum andIntegral classes aresuitable.

ForNatural andPositive, which are subsets of the integers, the standardclasses are not entirely appropriate. Consider, for example, subtraction.

(-) :: Num a => a -> a -> a

Natural andPositive do belong to theNum class, but subtraction and someother operations are partial; the expression1 - 2 throws instead of returninga value, because the integer result-1 is negative and not representable byeitherNatural orPositive.

For this reason,Natural andPositive have their own subtraction functionsthat returnSigned.

-- from Integer.Positivesubtract :: Positive -> Positive -> Signed-- from Integer.Naturalsubtract :: Natural -> Natural -> Signed

Polymorphic subtraction

In addition to the(-) method from theNum class and thesubtractfunctions forNatural andPositive, there are some polymorphic subtractionfunctions in theInteger module.subtractSigned generalizes the twomonomorphic functions discussed in the previous section. Its codomain isSigned.

subtractSigned :: forall a. Subtraction a =>    a -> a -> Signed

subtractInteger does the same thing, but gives the result asInteger insteadofSigned.

subtractInteger :: forall a. Subtraction a =>    a -> a -> Integer

Thesubtract function generalizes further. Its domain is any subtractable type(Natural,Positive,Integer, orSigned) and its codomain is any typethat can represent the full range of integers (Integer orSigned).

subtract :: forall b a. (Subtraction' b, Subtraction a) =>    a -> a -> b

Changes

0.1.4.1

Support GHC 9.8

Date: 2024-11-15

0.1.4.0

Added moduleInteger.AbsoluteDifference

Added to theInteger module:AbsoluteDifference (absoluteDifference)

Date: 2023-07-15

0.1.3.0

Added modulesInteger.Increase,Integer.StrictlyIncrease

Added classes to theInteger module:Increase (increase),StrictlyIncrease (strictlyIncrease)

Added to theInteger.Integer module:increase,strictlyIncrease

Added to theInteger.Natural module:strictlyIncrease

Added to theInteger.Positive module:increase

Added to theInteger.Signed module:increase,strictlyIncrease,one,addOne,subtractOne

Date: 2023-07-14

0.1.2.0

AddRead instance forPositive

Date: 2023-06-26

0.1.1.0

AddHashable instances forPositive,Sign, andSigned

AddEnum andBounded instances forSign

Date: 2023-04-22

0.1.0.0

Change type ofInteger.Natural.addOne fromInteger -> Integer toNatural -> Positive

New functions:

Integer.Natural.length :: [a] -> NaturalInteger.Positive.length :: NonEmpty a -> Positive

Date: 2023-02-09

0.0.0.1

Consolidate all the test suites into one

RemoveSafe pragmas

Date: 2023-01-16

0.0.0.0

Initial release

Date: 2022-11-29